Class: EducodeSales::BusinessesController

Inherits:
ApplicationController show all
Defined in:
app/controllers/educode_sales/businesses_controller.rb

Instance Method Summary collapse

Methods inherited from ApplicationController

#authenticate_admin, #authenticate_request, #current_user, #render_failure, #render_success

Instance Method Details

#add_export_recordsObject



389
390
391
392
393
394
395
396
397
398
399
400
# File 'app/controllers/educode_sales/businesses_controller.rb', line 389

def add_export_records
  if params[:type] == 'csv'
    record = BusinessExportRecord.new(name: '商机数据表.csv', staff_id: @current_admin.id)
  else params[:type] == 'excel'
    record = BusinessExportRecord.new(name: '商机数据表.xls', staff_id: @current_admin.id)
  end
  if record.save
    render_success
  else
    render_failure business
  end
end

#add_keysObject



284
285
286
# File 'app/controllers/educode_sales/businesses_controller.rb', line 284

def add_keys
  render layout: false
end

#createObject



232
233
234
235
236
237
238
239
240
# File 'app/controllers/educode_sales/businesses_controller.rb', line 232

def create
  department = Department.find(params[:department_id])
  business = @current_admin.businesses.build(name: params[:name], department_id: department.id, source: params[:source], school_id: department.school_id)
  if business.save
    render_success
  else
    render_failure business
  end
end

#destroyObject



272
273
274
275
276
277
278
# File 'app/controllers/educode_sales/businesses_controller.rb', line 272

def destroy
  business = Business.find(params[:id])
  business.soft_destroy(@current_admin.id)
  render_success
rescue ActiveRecord::DeleteRestrictionError => e
  render_failure '该商机已其它关联数据产生,暂不能删除'
end

#editObject



246
247
248
249
250
251
# File 'app/controllers/educode_sales/businesses_controller.rb', line 246

def edit
  @business = Business.find(params[:id])
  gon.department = {value: @business.department_id, name: "#{@business.department.school.name}-#{@business.department.name}"}
  gon.value = @business.department_id
  render layout: false
end

#edit_follow_recordObject



344
345
346
347
348
349
350
351
352
353
354
355
356
# File 'app/controllers/educode_sales/businesses_controller.rb', line 344

def edit_follow_record
  @years = (2015..Time.now.year).to_a.reverse
  @o_name = EducodeSales::Common.find_by(extras: EducodeSales::Common::OTYPE)&.name
  @follow_up = FollowUp.find(params[:follow_up_id])
  @clazz = Common.where(clazz: 'business_type').order("position").pluck(:name, :id)
  @stages = Common.where(clazz: 'business_step').order("position").pluck(:name, :id)
  @places = Place.order("created_at desc").pluck(:name, :id)
  gon.sales = @follow_up.assign_follow_ups.map do |d|
    {value: d.staff_id, name: d.staff.user.full_name}
  end
  gon.place = @follow_up.place.present? ? [{ value: @follow_up.place&.id, name: @follow_up.place&.name }] : [value: '', name: '']
  render layout: false
end

#edit_planObject



358
359
360
# File 'app/controllers/educode_sales/businesses_controller.rb', line 358

def edit_plan
  render layout: false
end

#export_recordsObject



379
380
381
382
383
384
385
386
387
388
# File 'app/controllers/educode_sales/businesses_controller.rb', line 379

def export_records
  respond_to do |format|
    format.html do
      render layout: false
    end
    format.json do
      @records = BusinessExportRecord.all.order(id: :desc).page(params[:page]).per(params[:limit])
    end
  end
end

#fileObject



362
363
364
365
# File 'app/controllers/educode_sales/businesses_controller.rb', line 362

def file
  gon.folder = edu_setting('attachment_folder') + '/'
  render layout: false
end

#get_export_dataObject



402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
# File 'app/controllers/educode_sales/businesses_controller.rb', line 402

def get_export_data
  respond_to do |format|
    format.html do
    end
    format.json do
      if @current_admin.is_admin?
        @businesses = Business
      else
        level = @current_admin.role.role_areas.find_by(clazz: '商机管理').level
        case level
        when '自己'
          business_ids = Business.joins(last_follow_up: :assign_follow_ups).where("educode_sales_assign_follow_ups.staff_id = ?", @current_admin.id).pluck(:id)
          @businesses = Business.where("educode_sales_businesses.staff_id = ? OR educode_sales_businesses.id in (?)", @current_admin.id, business_ids)
        when '区域'
          school_ids = School.where(province: @current_admin.areas.pluck(:name)).pluck(:id)
          business_ids = Business.joins(last_follow_up: :assign_follow_ups).where("educode_sales_assign_follow_ups.staff_id = ?", @current_admin.id).pluck(:id)
          @businesses = Business.joins("JOIN departments ON educode_sales_businesses.department_id = departments.id").where("departments.school_id in (?) OR educode_sales_businesses.staff_id = #{@current_admin.id}  OR educode_sales_businesses.id in (?)", school_ids, business_ids)
        else
          @businesses = Business
        end
      end

      if params[:name].present?
        @year = params[:name].split("-")[1].present? ? params[:name].split("-")[1] : ''
      end

      if params[:name].present? && params[:name] == "(销售态势-" + @year + "-现有商机)"
        ids = Common.where(extras: %w[a_class b_class c_class d_class e_class o_class]).pluck(:id)
        if @year == '全部'
          @businesses = @businesses.joins("
          JOIN educode_sales_follow_ups ON educode_sales_businesses.last_follow_up_id = educode_sales_follow_ups.id
        ").where("educode_sales_follow_ups.clazz_id in (?)",ids)
        else
          @businesses = @businesses.joins("
          JOIN educode_sales_follow_ups ON educode_sales_businesses.last_follow_up_id = educode_sales_follow_ups.id
        ").where("educode_sales_follow_ups.clazz_id in (?)",ids).where("educode_sales_follow_ups.year = ?",@year)
        end

      end

      if params[:name].present? && params[:name] == "(销售态势-" + @year + "-已中标商机)"
        stage_ids = Common.where(clazz: '商机阶段', name: ['已中标','已签单','已验收','回款中', '服务中','已结束']).pluck(:id)
        if @year == '全部'
          @businesses = @businesses.joins("
          JOIN educode_sales_follow_ups ON educode_sales_businesses.last_follow_up_id = educode_sales_follow_ups.id
        ").where("educode_sales_follow_ups.stage_id IN (?)",stage_ids)
        else
          @businesses = @businesses.joins("
          JOIN educode_sales_follow_ups ON educode_sales_businesses.last_follow_up_id = educode_sales_follow_ups.id
        ").where("educode_sales_follow_ups.bidded_date > ? AND educode_sales_follow_ups.bidded_date < ? AND educode_sales_follow_ups.stage_id IN (?)", "#{@year}-01-01", "#{@year}-12-31", stage_ids)
        end

      end

      if params[:name].present? && params[:name] == "(销售态势-" + @year + "-已签单商机)"
        s_stage_ids = Common.where(clazz: '商机阶段', name: ['已签单','已验收','回款中', '服务中','已结束']).pluck(:id)
        if @year == '全部'
          @businesses = @businesses.joins("
          JOIN educode_sales_follow_ups ON educode_sales_businesses.last_follow_up_id = educode_sales_follow_ups.id
        ").where("educode_sales_follow_ups.stage_id IN (?)",s_stage_ids)
        else
          @businesses = @businesses.joins("
          JOIN educode_sales_follow_ups ON educode_sales_businesses.last_follow_up_id = educode_sales_follow_ups.id
        ").where("educode_sales_follow_ups.signed_date > ? AND educode_sales_follow_ups.signed_date < ? AND educode_sales_follow_ups.stage_id IN (?)", "#{@year}-01-01", "#{@year}-12-31", s_stage_ids)
        end
      end

      if params[:name].present? && params[:name] == "(销售态势-" + @year + "-已回款商机)"
        x_id = Common.find_by(extras: EducodeSales::Common::XTYPE)&.id
        year_time = "#{@year}-01-01 00:00:00"
        year_over_time = "#{@year}-12-31 23:59:59"
        if @year == '全部'
          @businesses = @businesses.joins(follow_ups: :money_plans).where("educode_sales_follow_ups.clazz_id != ?", x_id).where.not("educode_sales_money_plans.clazz!= ?", 1).distinct
        else
          @businesses = @businesses.joins(follow_ups: :money_plans).where("educode_sales_follow_ups.clazz_id != ?", x_id).where.not("educode_sales_money_plans.clazz!= ?", 1).where("educode_sales_money_plans.date_at >= ?  AND educode_sales_money_plans.date_at <= ? ", year_time, year_over_time).distinct
        end
      end

      if params[:all].present?
        @businesses = @businesses
      end

      if params[:mine].present?
        ids = AssignFollowUp.where(staff_id: @current_admin.id).pluck(:follow_up_id)
        ids1 = FollowUp.where(id: ids).pluck(:business_id)
        @businesses =@businesses.where("educode_sales_businesses.id in (?) or educode_sales_businesses.staff_id = ?", ids1, @current_admin.id)
      end

      @businesses = @businesses
      if params[:name].present? && params[:name] != "(销售态势-" + @year + "-已中标商机)" && params[:name] != "(销售态势-" + @year + "-已签单商机)" && params[:name] != "(销售态势-" + @year + "-已回款商机)" && params[:name] != "(销售态势-" + @year + "-现有商机)"
        @businesses = @businesses.where("educode_sales_businesses.name like ?", "%#{params[:name]}%")
      end
      if params[:department].present?
        departments_ids = Department.joins(:school).where("schools.name like ?", "%#{params[:department]}%").pluck(:id)
        @businesses = @businesses.joins(:department).where("departments.id in (?)", departments_ids)
      end
      if params[:staff_id].present?
        part_a_ids = @businesses.where("educode_sales_businesses.last_follow_up_id is null AND educode_sales_businesses.staff_id = ?",params[:staff_id]).ids
        part_b_ids = @businesses.where("educode_sales_businesses.last_follow_up_id is not null").joins("
          JOIN educode_sales_follow_ups ON educode_sales_businesses.last_follow_up_id = educode_sales_follow_ups.id
        ").where("educode_sales_follow_ups.staff_id = ?", params[:staff_id]).ids
        business_ids = part_a_ids + part_b_ids
        @businesses = EducodeSales::Business.where(id:business_ids)
      end
      if params[:business_type].present?
        @businesses = @businesses.joins("
          JOIN educode_sales_follow_ups ON educode_sales_businesses.last_follow_up_id = educode_sales_follow_ups.id
        ").where("educode_sales_follow_ups.clazz_id in (?)", params[:business_type].split(",").map(&:to_i))
      end
      if params[:business_step].present?
        @businesses = @businesses.joins("
          JOIN educode_sales_follow_ups ON educode_sales_businesses.last_follow_up_id = educode_sales_follow_ups.id
        ").where("educode_sales_follow_ups.stage_id in (?)", params[:business_step].split(",").map(&:to_i))
      end
      if  params[:place_id].present?
        @businesses = @businesses.joins("
          JOIN educode_sales_follow_ups ON educode_sales_businesses.last_follow_up_id = educode_sales_follow_ups.id
        ").where("educode_sales_follow_ups.place_id = ?", params[:place_id])
      end

      if params[:area].present?
        p = EducodeSales::Common.find(params[:area]).name
        if @current_admin.is_admin?
          @businesses = @businesses.joins("
          JOIN departments ON educode_sales_businesses.department_id = departments.id
          JOIN schools ON departments.school_id = schools.id
        ").where("province = ?", p)
        else
          level = @current_admin.role.role_areas.find_by(clazz: '商机管理').level
          if level == "区域"
            @businesses = @businesses.joins("
          JOIN schools ON departments.school_id = schools.id
        ").where("province = ?", p)
          else
            @businesses = @businesses.joins("
          JOIN departments ON educode_sales_businesses.department_id = departments.id
          JOIN schools ON departments.school_id = schools.id
        ").where("province = ?", p)
          end
        end
      end

      if params[:date].present?
        date = params[:date].split(" - ")
        @businesses = @businesses.where("educode_sales_businesses.created_at > ? AND educode_sales_businesses.created_at < ?", date[0], date[1] + '23:59:59')
      end

      if params[:invitation_at].present?
        date = params[:invitation_at].split(" - ")
        @businesses = @businesses.joins("
          JOIN educode_sales_follow_ups ON educode_sales_businesses.last_follow_up_id = educode_sales_follow_ups.id
        ").where("educode_sales_follow_ups.invitation_at > ? AND educode_sales_follow_ups.invitation_at < ?", date[0] + '00:00:00', date[1] + '23:59:59')
      end

      if params[:sort].present? && params[:sort][:field]
        @businesses = @businesses.order("#{params[:sort][:field]} #{params[:sort][:order]}")
      else
        @businesses = @businesses.order("educode_sales_businesses.created_at desc")
      end

      @business_count = @businesses.count

      @businesses = @businesses.select("
        educode_sales_businesses.*,
        last_follow.invitation_at,
        last_follow.reception_at,
        last_follow.service_time_long,
        last_follow.service_end_time,
        last_follow.service_start_time,
        last_follow.bidded_date,
        last_follow.signed_date,
        last_follow.created_at as latest_time,
        last_follow.actual_amount,
        (last_follow.actual_amount - educode_sales_businesses.return_money) as wait_return_money,
        last_follow.total_amount").joins("
        LEFT JOIN educode_sales_follow_ups AS last_follow ON educode_sales_businesses.last_follow_up_id = last_follow.id
      ")

    end
  end
end

#indexObject



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
# File 'app/controllers/educode_sales/businesses_controller.rb', line 7

def index
  authorize! :read, Business
  respond_to do |format|
    format.html do
      common = Common.find_by(clazz: 'staff_type', name: '销售')
      @staffs = Staff.joins(:user).where(job_type: common.id).map { |d| [d.user.real_name, d.id]}  # [[], [], ............]
      # @more = can?(:create, EducodeSales::SalePlan) || can?(:update, EducodeSales::Business) || can?(:destroy, EducodeSales::Business) || can?(:show_file, EducodeSales::Business)
      @more = true
      gon.menus = []
      gon.export_menus = []
      gon.place = params[:place_id].present? ? [{ value: params[:place_id], name: EducodeSales::Place.find(params[:place_id]).name }] : []
      gon.business_type = Common.where(clazz: 'business_type').map do |d|
        {value: d.id, name: d.name}
      end
      gon.type = params[:clazz_id].present? ? [{ value: params[:clazz_id], name: Common.find(params[:clazz_id]).name }] : []
      gon.business_step = Common.where(clazz: 'business_step').map do |d|
        {value: d.id, name: d.name}
      end
      if can?(:create, EducodeSales::SalePlan)
        gon.menus << { title: '添加周计划', event: 'week' }
        gon.menus << { title: '添加月计划', event: 'month' }
      end
      gon.menus << { title: '跟进时间线', event: 'time_line' }
      if can?(:show_file, EducodeSales::Business)
        gon.menus << { title: '附件管理', event: 'file' }
      end
      if can?(:update, EducodeSales::Business)
        # gon.menus << { title: '编辑', event: 'edit' }
      end
      if can?(:destroy, EducodeSales::Business)
        gon.menus << { title: '删除', event: 'delete' }
      end
      if can?(:export_business, EducodeSales::Business)
        gon.export_menus << { title: '导出到Csv文件', event: 'export_csv' }
        gon.export_menus << { title: '导出到Excel文件', event: 'export_excel' }
      end
      gon.export_menus << { title: '导出记录', event: 'export_records' }
    end
    format.json do
      if @current_admin.is_admin?
        @businesses = Business
      else
        level = @current_admin.role.role_areas.find_by(clazz: '商机管理').level
        case level
        when '自己'
          business_ids = Business.joins(last_follow_up: :assign_follow_ups).where("educode_sales_assign_follow_ups.staff_id = ?", @current_admin.id).pluck(:id)
          @businesses = Business.where("educode_sales_businesses.staff_id = ? OR educode_sales_businesses.id in (?)", @current_admin.id, business_ids)
        when '区域'
          school_ids = School.where(province: @current_admin.areas.pluck(:name)).pluck(:id) + StaffSchool.where(staff_id: @current_admin.id).pluck(:school_id)
          business_ids = Business.joins(last_follow_up: :assign_follow_ups).where("educode_sales_assign_follow_ups.staff_id = ?", @current_admin.id).pluck(:id)
          @businesses = Business.joins("JOIN departments ON educode_sales_businesses.department_id = departments.id").where("departments.school_id in (?) OR educode_sales_businesses.staff_id = #{@current_admin.id}  OR educode_sales_businesses.id in (?)", school_ids, business_ids)
        else
          @businesses = Business
        end
      end
      if params[:q].present? && params[:q][:name].present?
        @year = params[:q][:name].split("-")[1].present? ? params[:q][:name].split("-")[1] : ''
      end

      if params[:q].present? && params[:q][:name].present? && params[:q][:name] == "(销售态势-" + @year + "-现有商机)"
        ids = Common.where(extras: %w[a_class b_class c_class d_class e_class o_class]).pluck(:id)
        if @year == '全部'
          @businesses = @businesses.joins("
          JOIN educode_sales_follow_ups ON educode_sales_businesses.last_follow_up_id = educode_sales_follow_ups.id
        ").where("educode_sales_follow_ups.clazz_id in (?)",ids)
        else
          @businesses = @businesses.joins("
          JOIN educode_sales_follow_ups ON educode_sales_businesses.last_follow_up_id = educode_sales_follow_ups.id
        ").where("educode_sales_follow_ups.clazz_id in (?)",ids).where("educode_sales_follow_ups.year = ?",@year)
        end

      end

      if params[:q].present? && params[:q][:name].present? && params[:q][:name] == "(销售态势-" + @year + "-已中标商机)"
        stage_ids = Common.where(clazz: '商机阶段', name: ['已中标','已签单','已验收','回款中', '服务中','已结束']).pluck(:id)
        if @year == '全部'
          @businesses = @businesses.joins("
          JOIN educode_sales_follow_ups ON educode_sales_businesses.last_follow_up_id = educode_sales_follow_ups.id
        ").where("educode_sales_follow_ups.stage_id IN (?)",stage_ids)
        else
          @businesses = @businesses.joins("
          JOIN educode_sales_follow_ups ON educode_sales_businesses.last_follow_up_id = educode_sales_follow_ups.id
        ").where("educode_sales_follow_ups.bidded_date > ? AND educode_sales_follow_ups.bidded_date < ? AND educode_sales_follow_ups.stage_id IN (?)", "#{@year}-01-01", "#{@year}-12-31", stage_ids)
        end

      end

      if params[:q].present? && params[:q][:name].present? && params[:q][:name] == "(销售态势-" + @year + "-已签单商机)"
        s_stage_ids = Common.where(clazz: '商机阶段', name: ['已签单','已验收','回款中', '服务中','已结束']).pluck(:id)
        if @year == '全部'
          @businesses = @businesses.joins("
          JOIN educode_sales_follow_ups ON educode_sales_businesses.last_follow_up_id = educode_sales_follow_ups.id
        ").where("educode_sales_follow_ups.stage_id IN (?)",s_stage_ids)
        else
          @businesses = @businesses.joins("
          JOIN educode_sales_follow_ups ON educode_sales_businesses.last_follow_up_id = educode_sales_follow_ups.id
        ").where("educode_sales_follow_ups.signed_date > ? AND educode_sales_follow_ups.signed_date < ? AND educode_sales_follow_ups.stage_id IN (?)", "#{@year}-01-01", "#{@year}-12-31", s_stage_ids)
        end
      end

      if params[:q].present? && params[:q][:name].present? && params[:q][:name] == "(销售态势-" + @year + "-已回款商机)"
        x_id = Common.find_by(extras: EducodeSales::Common::XTYPE)&.id
        year_time = "#{@year}-01-01 00:00:00"
        year_over_time = "#{@year}-12-31 23:59:59"
        if @year == '全部'
          @businesses = @businesses.joins(follow_ups: :money_plans).where("educode_sales_follow_ups.clazz_id != ?", x_id).where.not("educode_sales_money_plans.clazz!= ?", 1).distinct
        else
          @businesses = @businesses.joins(follow_ups: :money_plans).where("educode_sales_follow_ups.clazz_id != ?", x_id).where.not("educode_sales_money_plans.clazz!= ?", 1).where("educode_sales_money_plans.date_at >= ?  AND educode_sales_money_plans.date_at <= ? ", year_time, year_over_time).distinct
        end
      end

      if params[:q].present? && params[:q][:all].present?
        @businesses = @businesses
      end

      if params[:q].present? && params[:q][:mine].present?
        ids = AssignFollowUp.where(staff_id: @current_admin.id).pluck(:follow_up_id)
        ids1 = FollowUp.where(id: ids).pluck(:business_id)
        @businesses =@businesses.where("educode_sales_businesses.id in (?) or educode_sales_businesses.staff_id = ?", ids1, @current_admin.id)
      end

      @businesses = @businesses
      if params[:q].present? && params[:q][:name].present? && params[:q][:name] != "(销售态势-" + @year + "-已中标商机)" && params[:q][:name] != "(销售态势-" + @year + "-已签单商机)" && params[:q][:name] != "(销售态势-" + @year + "-已回款商机)" && params[:q][:name] != "(销售态势-" + @year + "-现有商机)"
        @businesses = @businesses.where("educode_sales_businesses.name like ?", "%#{params[:q][:name]}%")
      end
      if params[:q].present? && params[:q][:department].present?
        departments_ids = Department.joins(:school).where("schools.name like ?", "%#{params[:q][:department]}%").pluck(:id)
        @businesses = @businesses.joins(:department).where("departments.id in (?)", departments_ids)
      end
      if params[:q].present? && params[:q][:staff_id].present?
        part_a_ids = @businesses.where("educode_sales_businesses.last_follow_up_id is null AND educode_sales_businesses.staff_id = ?",params[:q][:staff_id]).ids
        part_b_ids = @businesses.where("educode_sales_businesses.last_follow_up_id is not null").joins("
          JOIN educode_sales_follow_ups ON educode_sales_businesses.last_follow_up_id = educode_sales_follow_ups.id
        ").where("educode_sales_follow_ups.staff_id = ?", params[:q][:staff_id]).ids
        business_ids = part_a_ids + part_b_ids
        @businesses = EducodeSales::Business.where(id:business_ids)
      end
      if params[:q].present? && params[:q][:business_type].present?
        @businesses = @businesses.joins("
          JOIN educode_sales_follow_ups ON educode_sales_businesses.last_follow_up_id = educode_sales_follow_ups.id
        ").where("educode_sales_follow_ups.clazz_id in (?)", params[:q][:business_type].split(",").map(&:to_i))
      end
      if params[:q].present? && params[:q][:business_step].present?
        @businesses = @businesses.joins("
          JOIN educode_sales_follow_ups ON educode_sales_businesses.last_follow_up_id = educode_sales_follow_ups.id
        ").where("educode_sales_follow_ups.stage_id in (?)", params[:q][:business_step].split(",").map(&:to_i))
      end
      if params[:q].present? && params[:q][:place_id].present?
        @businesses = @businesses.joins("
          JOIN educode_sales_follow_ups ON educode_sales_businesses.last_follow_up_id = educode_sales_follow_ups.id
        ").where("educode_sales_follow_ups.place_id = ?", params[:q][:place_id])
      end
      if params[:q].present? && params[:q][:business_year].present?
        @businesses = @businesses.joins("
          JOIN educode_sales_follow_ups ON educode_sales_businesses.last_follow_up_id = educode_sales_follow_ups.id
        ").where("educode_sales_follow_ups.year = ?", params[:q][:business_year])
      end

      if params[:q].present? && params[:q][:area].present?
        p = EducodeSales::Common.find(params[:q][:area]).name
        if @current_admin.is_admin?
          @businesses = @businesses.joins("
          JOIN departments ON educode_sales_businesses.department_id = departments.id
          JOIN schools ON departments.school_id = schools.id
        ").where("province = ?", p)
        else
          level = @current_admin.role.role_areas.find_by(clazz: '商机管理').level
          if level == "区域"
            @businesses = @businesses.joins("
          JOIN schools ON departments.school_id = schools.id
        ").where("province = ?", p)
          else
            @businesses = @businesses.joins("
          JOIN departments ON educode_sales_businesses.department_id = departments.id
          JOIN schools ON departments.school_id = schools.id
        ").where("province = ?", p)
          end
        end
      end

      if params[:q].present? && params[:q][:date].present?
        date = params[:q][:date].split(" - ")
        @businesses = @businesses.where("educode_sales_businesses.created_at > ? AND educode_sales_businesses.created_at < ?", date[0], date[1] + '23:59:59')
      end

      if params[:q].present? && params[:q][:invitation_at].present?
        date = params[:q][:invitation_at].split(" - ")
        @businesses = @businesses.joins("
          JOIN educode_sales_follow_ups ON educode_sales_businesses.last_follow_up_id = educode_sales_follow_ups.id
        ").where("educode_sales_follow_ups.invitation_at > ? AND educode_sales_follow_ups.invitation_at < ?", date[0] + '00:00:00', date[1] + '23:59:59')
      end

      if params[:sort].present? && params[:sort][:field]
        if params[:sort][:field] = "service_end_time"
          @businesses = @businesses.order("service_time_long #{params[:sort][:order]}")
        else
          @businesses = @businesses.order("#{params[:sort][:field]} #{params[:sort][:order]}")
        end
      else
        @businesses = @businesses.order("educode_sales_businesses.created_at desc")
      end

      @business_count = @businesses.count
      # mysql -select
      @businesses = @businesses.select("
        educode_sales_businesses.*,
        last_follow.invitation_at,
        last_follow.service_time_long,
        last_follow.service_end_time,
        last_follow.service_start_time,
        last_follow.reception_at,
        last_follow.bidded_date,
        last_follow.signed_date,
        last_follow.year,
        last_follow.created_at as latest_time,
        last_follow.actual_amount,
        (last_follow.actual_amount - educode_sales_businesses.return_money) as wait_return_money,
        last_follow.total_amount").joins("
        LEFT JOIN educode_sales_follow_ups AS last_follow ON educode_sales_businesses.last_follow_up_id = last_follow.id
      ").page(params[:page]).per(params[:limit])

    end
  end
end

#newObject



242
243
244
# File 'app/controllers/educode_sales/businesses_controller.rb', line 242

def new
  render layout: false
end

#new_follow_recordObject



307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
# File 'app/controllers/educode_sales/businesses_controller.rb', line 307

def new_follow_record
  load_business
  @years = (2015..Time.now.year).to_a.reverse
  @o_name = EducodeSales::Common.find_by(extras: EducodeSales::Common::OTYPE)&.name
  @clazz = Common.where(clazz: 'business_type').order("position").pluck(:name, :id)
  @stages = Common.where(clazz: 'business_step').order("position").pluck(:name, :id)
  @places = Place.order("created_at desc").pluck(:name, :id)
  @last_follow_up = @business.last_follow_up
  if @last_follow_up
    gon.sales = @last_follow_up.assign_follow_ups.map do |d|
      {value: d.staff_id, name: d.staff.user.full_name}
    end
    gon.place = @last_follow_up.place.present? ? [{ value: @last_follow_up.place&.id, name: @last_follow_up.place&.name }] : []
  end
  render layout: false
end

#show_followObject



288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
# File 'app/controllers/educode_sales/businesses_controller.rb', line 288

def show_follow
  load_business
  respond_to do |format|
    format.html do
      render layout: false
    end
    format.json do
      @follow_ups = @business.follow_ups
      @latest = @follow_ups.order(created_at: :desc).first
      if params[:field]
        @follow_ups = @follow_ups.order("#{params[:field]} #{params[:order]}")
      else
        @follow_ups = @follow_ups.order("created_at desc")
      end
      @follow_ups = @follow_ups.page(params[:page]).per(params[:limit])
    end
  end
end

#show_follow_recordObject



330
331
332
333
334
335
336
337
338
339
340
341
342
# File 'app/controllers/educode_sales/businesses_controller.rb', line 330

def show_follow_record
  follow_up_id = params[:follow_up_id]
  @follow_up = FollowUp.find(follow_up_id)
  id = @follow_up.id
  @follow_ups = Business.find(@follow_up.business_id).follow_ups
  ids = @follow_ups.ids
  size = ids.size
  ids = ids.reverse
  index = ids.index(id)
  @next_id = (index != size - 1 && size > 1) ? ids[index + 1] : 0
  @pre_id = (index != 0 && size > 1) ? ids[index - 1] : 0
  render layout: false
end

#show_keysObject



280
281
282
# File 'app/controllers/educode_sales/businesses_controller.rb', line 280

def show_keys
  render layout: false
end

#time_lineObject



369
370
371
372
373
374
375
376
377
# File 'app/controllers/educode_sales/businesses_controller.rb', line 369

def time_line
  @business = Business.find(params[:business_id])
  respond_to do |format|
    format.html do
      @follow_ups = @business.follow_ups.order(created_at: :desc)
      render layout: false
    end
  end
end

#unfinish_plansObject



325
326
327
328
# File 'app/controllers/educode_sales/businesses_controller.rb', line 325

def unfinish_plans
  load_business
  @plans = @business.sale_plans.where.not(finish_rate: 100).page(params[:page]).per(params[:limit])
end

#updateObject



253
254
255
256
257
258
259
260
261
# File 'app/controllers/educode_sales/businesses_controller.rb', line 253

def update
  business = Business.find(params[:id])
  department = Department.find(params[:department_id])
  if business.update(name: params[:name], department_id: department.id, source: params[:source], school_id: department.school_id)
    render_success
  else
    render_failure business
  end
end

#updateMoneyObject



263
264
265
266
267
268
269
270
# File 'app/controllers/educode_sales/businesses_controller.rb', line 263

def updateMoney
  business = Business.find(params[:id])
  if business.update(return_money: params[:return_money])
    render_success
  else
    render_failure business
  end
end

#upload_fileObject



366
367
368
# File 'app/controllers/educode_sales/businesses_controller.rb', line 366

def upload_file
  render layout: false
end