Class: EducodeSales::PlansController

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

Instance Method Summary collapse

Methods inherited from ApplicationController

#authenticate_admin, #authenticate_request, #current_user, #filter, #paginate, #render_failure, #render_success

Instance Method Details

#business_infosObject



396
397
398
399
400
401
402
403
404
405
# File 'app/controllers/educode_sales/plans_controller.rb', line 396

def business_infos
  @data = @current_admin.business_infos.where(sale_plan_id: nil, year: params[:year])

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

#createObject



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
# File 'app/controllers/educode_sales/plans_controller.rb', line 68

def create
  if params["year"].present?
    business = Business.find_by(id: params[:business_id])
    plan = @current_admin.sale_plans.build(year: params[:year], business_id: business.id, clazz: params[:clazz], common_id: params[:common_id] )
    if params[:assign_id].present?
      plan.staff_id = params[:assign_id]
      plan.assign_id = @current_admin.id
    end
    if plan.save
      render_success
    else
      render_failure plan
    end
  else
    plan = @current_admin.sale_plans.build(plan_params)
    plan.month = params["month"] + "-1"
    plan.business = Business.find_by(id: params[:business_id])

    if params[:assign_id].present?
      plan.staff_id = params[:assign_id]
      plan.assign_id = @current_admin.id
    end
    if plan.save
      render_success
    else
      render_failure plan
    end
  end
  
end

#create_business_infoObject



99
100
101
102
103
104
105
106
107
# File 'app/controllers/educode_sales/plans_controller.rb', line 99

def create_business_info
  business = Business.find_by(id: params[:business_id])
  info = @current_admin.business_infos.find_or_initialize_by(year: params[:year], business_id: business.id, clazz: params[:clazz])
  if info.save
    render_success
  else
    render_failure info
  end
end

#create_sales_planObject



460
461
462
463
464
465
466
# File 'app/controllers/educode_sales/plans_controller.rb', line 460

def create_sales_plan
  @current_admin.business_infos.where(sale_plan_id: nil).each do |d|
    plan = @current_admin.sale_plans.create(year: d.year, business_id: d.business_id, clazz: d.clazz)
    d.update(sale_plan_id: plan.id)
  end
  render_success
end

#delete_business_infoObject



468
469
470
471
# File 'app/controllers/educode_sales/plans_controller.rb', line 468

def delete_business_info
  @current_admin.business_infos.where(id: params[:id]).destroy_all
  render_success
end

#destroyObject



274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
# File 'app/controllers/educode_sales/plans_controller.rb', line 274

def destroy
  if params[:year].present?
    # 删除年度计划
    sale_plans = SalePlan.where(year: params[:year], clazz: EducodeSales::SalePlan::CLAZZ_NAME[params[:clazz]])
    sale_plans.each do |d|
      EducodeSales::Recycle.create(source: d, deleter_id: @current_admin.id)
    end
    sale_plans.update_all(deleted_at: Time.now)
    sale_plans.business_infos.delete_all
    render_success
  else
    sale_plan = SalePlan.find(params[:id])
    if sale_plan.soft_destroy(@current_admin.id)
      render_success
    else
      render_failure sale_plan
    end
  end
end

#edit_bussiness_infoObject



407
408
409
410
# File 'app/controllers/educode_sales/plans_controller.rb', line 407

def edit_bussiness_info
  @business_info = @current_admin.business_infos.find(params[:id])
  render layout: false
end

#edit_bussiness_info_extraObject



412
413
414
415
# File 'app/controllers/educode_sales/plans_controller.rb', line 412

def edit_bussiness_info_extra
  @business_info = EducodeSales::BusinessInfo.find(params[:id])
  render layout: false
end

#edit_monthObject



298
299
300
301
302
303
304
305
306
307
# File 'app/controllers/educode_sales/plans_controller.rb', line 298

def edit_month
  @commons = Common.where(clazz: '计划类型').pluck(:name, :id)
  business_ids = EducodeSales::AssignFollowUp.joins(:follow_up).where(staff_id: @current_admin.id).pluck :business_id
  @businesses = Business.includes(:department, :last_follow_up).where("educode_sales_businesses.id in (?) or educode_sales_businesses.staff_id = ?", business_ids, @current_admin.id).order(created_at: :desc)
  @businesses = @businesses.map { |b| [b.name + "(" + b.department&.school&.name.to_s + ")" + "---" + "最新跟进:" + (b.last_follow_up.present? ? b.last_follow_up.created_at.to_s : b.created_at.to_s), b.id] }
  @sale_plan = SalePlan.find(params[:id])
  @finish_rates = []
  11.times { |d| @finish_rates << ["#{d*10}%", d * 10]}
  render layout: false
end

#edit_monthlyObject



309
310
311
# File 'app/controllers/educode_sales/plans_controller.rb', line 309

def edit_monthly
  render layout: false
end

#edit_weekObject



228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
# File 'app/controllers/educode_sales/plans_controller.rb', line 228

def edit_week
  @commons = Common.where(clazz: '计划类型').pluck(:name, :id)
  business_ids = EducodeSales::AssignFollowUp.joins(:follow_up).where(staff_id: @current_admin.id).pluck :business_id
  @sale_plan = SalePlan.find(params[:id])
  # 加入指派的商机id
  business_ids << @sale_plan.business_id
  @businesses = Business.includes(:department, :last_follow_up).where("educode_sales_businesses.id in (?) or educode_sales_businesses.staff_id = ?", business_ids, @current_admin.id).order(created_at: :desc)
  @businesses = @businesses.map { |b| [b.name + "(" + b.department&.school&.name.to_s + ")" + "---" + "最新跟进:" + (b.last_follow_up.present? ? b.last_follow_up.created_at.to_s : b.created_at.to_s), b.id] }
  
  if @sale_plan.weekly.present?
    week = Time.now.strftime('%W').to_i
    if week > 3
      @weeks = [week+2, week+1, week, week - 1, week - 2]
    elsif week == 2
      @weeks = [week, week - 1]
    else
      @weeks = [week]
    end
    @weeks << @sale_plan.weekly
    @weeks = @weeks.uniq.sort
    @weeks = @weeks.map do |d|
      if d == week
        ["#{d}  本周", d]
      else
        [d, d]
      end
    end
  end
  @months = [Time.now.strftime("%Y-%m"), Time.now.months_ago(1).strftime("%Y-%m")]
  @months << @sale_plan.month.strftime("%Y-%m")
  @months = @months.uniq.sort
  @finish_rates = []
  11.times { |d| @finish_rates << ["#{d * 10}%", d * 10] }
  render layout: false
end

#edit_weeklyObject



294
295
296
# File 'app/controllers/educode_sales/plans_controller.rb', line 294

def edit_weekly
  render layout: false
end

#edit_year_planObject



473
474
475
476
# File 'app/controllers/educode_sales/plans_controller.rb', line 473

def edit_year_plan
  @clazz = EducodeSales::SalePlan::CLAZZ_NAME
  render layout: false
end

#indexObject



6
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
# File 'app/controllers/educode_sales/plans_controller.rb', line 6

def index
  # authorize! :read, SalePlan
  respond_to do |format|
    format.html do
      common = Common.find_by(clazz: 'staff_type', name: '销售')
      @staffs = Staff.includes(:user).where(job_type: common.id).map { |d| [d.user.real_name, d.id] }
      @years = (1..(Time.now.year - 2020)).map { |d| 2020 + d }
      @months = (1..12).map { |d| d }
    end
    format.json do
      if @current_admin.is_admin?
        @sale_plans = SalePlan
      else
        level = @current_admin.role.role_areas.find_by(clazz: '销售计划').level
        case level
        when '自己'
          @sale_plans = SalePlan.where(staff_id: @current_admin.id)
        when '区域'
          staff_ids = Staff.joins(user: [user_extension: [department: :school]]).where("schools.province in (?)", @current_admin.areas.pluck(:name)).pluck(:id)

          school_ids = StaffSchool.where(staff_id: @current_admin.id).pluck(:school_id)
          if @current_admin.staff_school_tags.present?
            # 如果设置专项客户类型,则视为专项经理,根据专项客户查看对应商机
            school_ids += School.joins(:school_tags).where("school_tags.id in (?)", @current_admin.staff_school_tags.pluck(:school_tag_id)).pluck(:id)
          end

          business_ids = Business.where(school_id: school_ids.uniq).pluck(:id)
          @sale_plans = SalePlan.where("staff_id in (?) OR educode_sales_sale_plans.staff_id = ? OR educode_sales_sale_plans.business_id in (?)", staff_ids, @current_admin.id,business_ids)
        else
          @sale_plans = SalePlan
        end
      end
      if params[:clazz] == 'week'
        @sale_plans = @sale_plans.where.not(weekly: nil).where.not(month: nil)
      elsif params[:clazz] == 'month'
        @sale_plans = @sale_plans.where(weekly: nil).where.not(month: nil)
      end
      if params[:q].present? && params[:q][:staff_id].present?
        @sale_plans = @sale_plans.where(staff_id: params[:q][:staff_id])
      end
      # p "#{params[:q][:year]}-01-01 00:00:00".to_date, "#{params[:q][:year]}-12-31 23:59:00".to_date
      if params[:q].present? && params[:q][:year].present?
        @sale_plans = @sale_plans.where("month >= ? and month <= ?", "#{params[:q][:year]}-01-01 00:00:00".to_date, "#{params[:q][:year]}-12-31 23:59:00".to_date)
      end
      if params[:q].present? && params[:q][:month].present?
        @sale_plans = @sale_plans.where("month = ?", "#{params[:q][:month]}-01 00:00:00".to_date)
      end
      if params[:sort].present? && params[:sort][:field]
        @sale_plans = @sale_plans.order("#{params[:sort][:field]} #{params[:sort][:order]}")
      else
        @sale_plans = @sale_plans.order("created_at desc")
      end

      @sale_plans = @sale_plans.page(params[:page]).per(params[:limit])
    end
  end
end

#month_planObject



499
500
501
502
503
504
# File 'app/controllers/educode_sales/plans_controller.rb', line 499

def month_plan
  common = Common.find_by(clazz: 'staff_type', name: '销售')
  @staffs = Staff.includes(:user).where(job_type: common.id).map { |d| [d.user.real_name, d.id] }
  @years = (1..(Time.now.year - 2020)).map { |d| 2020 + d }
  @months = (1..12).map { |d| d }
end

#monthlyObject



492
493
494
495
496
497
# File 'app/controllers/educode_sales/plans_controller.rb', line 492

def monthly
  common = Common.find_by(clazz: 'staff_type', name: '销售')
  @staffs = Staff.includes(:user).where(job_type: common.id).map { |d| [d.user.real_name, d.id] }
  @years = (1..(Time.now.year - 2020)).map { |d| 2020 + d }
  @months = (1..12).map { |d| d }
end

#my_monthObject



323
324
325
326
327
328
329
330
331
# File 'app/controllers/educode_sales/plans_controller.rb', line 323

def my_month
  @sale_plans = SalePlan.where(staff_id: @current_admin.id, month: Time.now.strftime("%Y-%m") + "-01 00:00:00", weekly: nil)
  if  params[:sort].present? && params[:sort][:field]
    @sale_plans = @sale_plans.order("#{params[:sort][:field]} #{params[:sort][:order]}")
  else
    @sale_plans = @sale_plans.order("updated_at desc")
  end
  @sale_plans =  @sale_plans.page(params[:page]).per(params[:limit])
end

#my_weekObject



313
314
315
316
317
318
319
320
321
# File 'app/controllers/educode_sales/plans_controller.rb', line 313

def my_week
  @sale_plans = SalePlan.where(staff_id: @current_admin.id, month: Time.now.strftime("%Y-%m") + "-01 00:00:00", weekly: Time.now.strftime('%W').to_i)
  if  params[:sort].present? && params[:sort][:field]
    @sale_plans = @sale_plans.order("#{params[:sort][:field]} #{params[:sort][:order]}")
  else
    @sale_plans = @sale_plans.order("updated_at desc")
  end
  @sale_plans =  @sale_plans.page(params[:page]).per(params[:limit])
end

#my_years_planObject



392
393
394
# File 'app/controllers/educode_sales/plans_controller.rb', line 392

def my_years_plan
  @sale_plans = SalePlan.where(staff_id: @current_admin.id)
end

#newObject



64
65
66
# File 'app/controllers/educode_sales/plans_controller.rb', line 64

def new
  render layout: false
end

#new_monthObject



152
153
154
155
156
157
158
159
160
161
162
# File 'app/controllers/educode_sales/plans_controller.rb', line 152

def new_month
  @commons = Common.where(clazz: '计划类型').pluck(:name, :id)
  common = Common.find_by(clazz: 'staff_type', name: '销售')
  @staffs = Staff.includes(:user).where(job_type: common.id).map { |d| [d.user.real_name, d.id] }
  
  @business = Business.find_by(id: params[:business_id])
  business_ids = EducodeSales::AssignFollowUp.joins(:follow_up).where(staff_id: @current_admin.id).pluck :business_id
  @businesses =Business.includes(:department, :last_follow_up).where("educode_sales_businesses.id in (?) or educode_sales_businesses.staff_id = ?", business_ids, @current_admin.id).order(created_at: :desc)
  @businesses = @businesses.map { |b| [b.name + "(#{b.department&.school&.name} #{b.department&.name})" + "---" + "最新跟进:" + (b.last_follow_up.present? ? b.last_follow_up.created_at.to_s : b.created_at.to_s), b.id] }
  render layout: false
end

#new_monthlyObject



190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
# File 'app/controllers/educode_sales/plans_controller.rb', line 190

def new_monthly
  d = @current_admin
  area_ids = EducodeSales::Common.where(clazz: 'area').ids.sort.to_s
  if d.areas.present?
    if d.areas.ids.sort.to_s == area_ids
      @area = '全国'
    else
      @area =  d.areas.pluck(:name).join("、")
    end
  else
    @area =  ''
  end
  @name = @current_admin.user.real_name
  sale_plans = SalePlan.where(month: Time.now.strftime("%Y-%m") + "-01 00:00:00", staff_id: @current_admin.id, weekly: nil)
  @rate = sale_plans.present? ? (sale_plans.sum("finish_rate") / sale_plans.size) : 0
  @departments_count = sale_plans.joins(:business).count("distinct(department_id)")
  @total_amount = sale_plans.joins(business: :last_follow_up).distinct.sum("total_amount")
  render layout: false
end

#new_weekObject



110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'app/controllers/educode_sales/plans_controller.rb', line 110

def new_week
  @business = Business.find_by(id: params[:business_id])
  common = Common.find_by(clazz: 'staff_type', name: '销售')
  @staffs = Staff.includes(:user).where(job_type: common.id).map { |d| [d.user.real_name, d.id] }

  
  @week = Time.now.strftime('%W').to_i
  week = Time.now.strftime('%W').to_i
  if week > 3
    @weeks = [[week+2,week+2], [week+1,week+1], [week.to_s + '  本周',week], [week - 1,week - 1], [week - 2,week - 2]]
  elsif week == 2
    @weeks = [[week.to_s + '  本周',week], [week - 1,week - 1]]
  else
    @weeks = [[week.to_s + '  本周',week]]
  end
  @commons = Common.where(clazz: '计划类型').pluck(:name, :id)
  business_ids = EducodeSales::AssignFollowUp.joins(:follow_up).where(staff_id: @current_admin.id).pluck :business_id
  @businesses = Business.includes(:department, :last_follow_up).where("educode_sales_businesses.id in (?) or educode_sales_businesses.staff_id = ?", business_ids, @current_admin.id).order(created_at: :desc)
  @businesses = @businesses.map { |b| [b.name + "(#{b.department&.school&.name} #{b.department&.name})" + "---" + "最新跟进:" + (b.last_follow_up.present? ? b.last_follow_up.created_at.to_s : b.created_at.to_s), b.id] }
  render layout: false
end

#new_weeklyObject



132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'app/controllers/educode_sales/plans_controller.rb', line 132

def new_weekly
  d = @current_admin
  area_ids = EducodeSales::Common.where(clazz: 'area').ids.sort.to_s
  if d.areas.present?
    if d.areas.ids.sort.to_s == area_ids
      @area = '全国'
    else
      @area =  d.areas.pluck(:name).join("、")
    end
  else
    @area =  ''
  end
  @name = @current_admin.user.real_name
  sale_plans = SalePlan.where(month: Time.now.strftime("%Y-%m") + "-01 00:00:00", weekly: Time.now.strftime('%W').to_i, staff_id: @current_admin.id)
  @rate = sale_plans.present? ? (sale_plans.sum("finish_rate") / sale_plans.size) : 0
  @departments_count = sale_plans.joins(:business).count("distinct(department_id)")
  @total_amount = sale_plans.joins(business: :last_follow_up).distinct.sum("total_amount")
  render layout: false
end

#new_yearObject



164
165
166
167
168
169
170
171
# File 'app/controllers/educode_sales/plans_controller.rb', line 164

def new_year
  @business = Business.find_by(id: params[:business_id])
  business_ids = EducodeSales::AssignFollowUp.joins(:follow_up).where(staff_id: @current_admin.id).pluck :business_id
  @businesses =Business.includes(:department, :last_follow_up).where("educode_sales_businesses.id in (?) or educode_sales_businesses.staff_id = ?", business_ids, @current_admin.id).order(created_at: :desc)
  @businesses = @businesses.map { |b| [ b.name + "(#{b.department&.school&.name} #{b.department&.name})" + "---" + "最新跟进:" + (b.last_follow_up.present? ? b.last_follow_up.created_at.to_s : b.created_at.to_s), b.id] }
  @clazz = EducodeSales::SalePlan::CLAZZ_NAME
  render layout: false
end

#plan_business_infosObject



417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
# File 'app/controllers/educode_sales/plans_controller.rb', line 417

def plan_business_infos
  if params[:q] && params[:q][:year_search].present?
    @data = EducodeSales::BusinessInfo.where(year: params[:q][:year_search], staff_id: params[:staff_id])
  else
    @data = EducodeSales::BusinessInfo.where(year: params[:year], staff_id: params[:staff_id])
  end

  @data = @data.where.not(sale_plan_id: nil)

  if params[:q] && params[:q][:clazz].present?
    @data = @data.where(clazz: params[:q][:clazz])
  end

  if params[:q] && params[:q][:department].present?
    @data = @data.joins(business: [department: :school]).where("schools.name like ?", "%#{params[:q][:department]}%")
  end

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

#show_monthObject



219
220
221
222
# File 'app/controllers/educode_sales/plans_controller.rb', line 219

def show_month
  @sale_plan = SalePlan.find(params[:id])
  render layout: false
end

#show_monthlyObject



224
225
226
# File 'app/controllers/educode_sales/plans_controller.rb', line 224

def show_monthly
  render layout: false
end

#show_weekObject



210
211
212
213
# File 'app/controllers/educode_sales/plans_controller.rb', line 210

def show_week
  @sale_plan = SalePlan.find(params[:id])
  render layout: false
end

#show_weeklyObject



215
216
217
# File 'app/controllers/educode_sales/plans_controller.rb', line 215

def show_weekly
  render layout: false
end

#staff_businessesObject

暂时不用



174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
# File 'app/controllers/educode_sales/plans_controller.rb', line 174

def staff_businesses
  if params[:q].present?
    business_ids = EducodeSales::AssignFollowUp.joins(:follow_up).where(staff_id: @current_admin.id).pluck :business_id
    @businesses = Business.includes(:department, :last_follow_up).where("educode_sales_businesses.id in (?) or educode_sales_businesses.staff_id = ?", business_ids, @current_admin.id)
    if params[:q].present?
      @businesses =  @businesses.where("educode_sales_businesses.name like ?", "%#{params[:q]}%")
    else
      @businesses =  @businesses.limit(10)
    end
    @businesses =  @businesses.order(created_at: :desc)
   
    @businesses = @businesses.map { |b| {name: b.name + "(#{b.department&.school&.name} #{b.department&.name})" + "---" + "最新跟进:" + (b.last_follow_up.present? ? b.last_follow_up.created_at.to_s : b.created_at.to_s), value: b.id} }
    render json: { data: @businesses, code: 0, msg: "success" }
  end
end

#target_trackObject



514
515
516
517
518
519
520
521
522
523
524
525
# File 'app/controllers/educode_sales/plans_controller.rb', line 514

def target_track
  respond_to do |format|
    format.html do
     
    end
    format.js 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]}
      gon.school_tags = SchoolTag.where(for_missions: true).map { |d| {value: d.id, name: d.name } }
    end
  end
end

#updateObject



264
265
266
267
268
269
270
271
272
# File 'app/controllers/educode_sales/plans_controller.rb', line 264

def update
  sale_plan = SalePlan.find(params[:id])
  params["month"] = params["month"] + "-1"
  if sale_plan.update(plan_params)
    render_success
  else
    render_failure sale_plan
  end
end

#update_business_infoObject



442
443
444
445
446
447
448
449
# File 'app/controllers/educode_sales/plans_controller.rb', line 442

def update_business_info
  business_info = @current_admin.business_infos.find(params[:id])
  if business_info.update(business_info_params)
    render_success
  else
    render_failure business_info
  end
end

#update_business_info_extraObject



451
452
453
454
455
456
457
458
# File 'app/controllers/educode_sales/plans_controller.rb', line 451

def update_business_info_extra
  business_info = EducodeSales::BusinessInfo.find(params[:id])
  if business_info.update(update_business_info_extra_params)
    render_success
  else
    render_failure business_info
  end
end

#week_planObject



478
479
480
481
482
483
# File 'app/controllers/educode_sales/plans_controller.rb', line 478

def week_plan
  common = Common.find_by(clazz: 'staff_type', name: '销售')
  @staffs = Staff.includes(:user).where(job_type: common.id).map { |d| [d.user.real_name, d.id] }
  @years = (1..(Time.now.year - 2020)).map { |d| 2020 + d }
  @months = (1..12).map { |d| d }
end

#weeklyObject



485
486
487
488
489
490
# File 'app/controllers/educode_sales/plans_controller.rb', line 485

def weekly
  common = Common.find_by(clazz: 'staff_type', name: '销售')
  @staffs = Staff.includes(:user).where(job_type: common.id).map { |d| [d.user.real_name, d.id] }
  @years = (1..(Time.now.year - 2020)).map { |d| 2020 + d }
  @months = (1..12).map { |d| d }
end

#year_planObject



506
507
508
509
510
511
# File 'app/controllers/educode_sales/plans_controller.rb', line 506

def year_plan
  common = Common.find_by(clazz: 'staff_type', name: '销售')
  @staffs = Staff.includes(:user).where(job_type: common.id).map { |d| [d.user.real_name, d.id] }
  @years = (1..(Time.now.year - 2020)).map { |d| 2020 + d }
  @months = (1..12).map { |d| d }
end

#years_planObject



333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
# File 'app/controllers/educode_sales/plans_controller.rb', line 333

def years_plan
  @sale_plans = SalePlan.from("(
                                SELECT SUM(last_follow.budget_amount) AS budget_amounts, educode_sales_sale_plans.*, ss.annual,
                                       SUM(IF(educode_sales_sale_plans.clazz = 1 AND educode_sales_business_infos.plan_sign_on IS NOT NULL, last_follow.budget_amount, 0)) AS clazz_1,
                                       SUM(IF(educode_sales_sale_plans.clazz = 18 and plan_bid_on IS NOT NULL, last_follow.budget_amount, 0)) AS clazz_18,

                                       (SUM(IF(educode_sales_sale_plans.clazz = 2 AND prepayment_plan_on IS NOT NULL AND check_fee_plan_on IS NOT NULL AND qa_plan_on IS NOT NULL, prepayment_amount, 0)) +
                                       SUM(IF(educode_sales_sale_plans.clazz = 2 AND prepayment_plan_on IS NOT NULL AND check_fee_plan_on IS NOT NULL AND qa_plan_on IS NOT NULL, check_fee, 0)) +
                                       SUM(IF(educode_sales_sale_plans.clazz = 2 AND prepayment_plan_on IS NOT NULL AND check_fee_plan_on IS NOT NULL AND qa_plan_on IS NOT NULL, qa_amount, 0)) ) AS clazz_2,
                                       SUM(IF(educode_sales_sale_plans.clazz = 7, last_follow.budget_amount, 0)) AS clazz_7,

                                       SUM(IF(last_follow.bidded_date IS NOT NULL, last_follow.actual_amount, 0)) AS finish_bid,
                                       SUM(IF(last_follow.signed_date IS NOT NULL, last_follow.actual_amount, 0)) AS finish_sign,
                                       SUM(educode_sales_businesses.return_money) AS finish_return_money

                                FROM educode_sales_sale_plans
                                JOIN educode_sales_businesses ON educode_sales_businesses.id = educode_sales_sale_plans.business_id
                                LEFT JOIN educode_sales_business_infos ON educode_sales_business_infos.sale_plan_id = educode_sales_sale_plans.id
                                LEFT JOIN educode_sales_follow_ups AS last_follow ON educode_sales_businesses.last_follow_up_id = last_follow.id AND last_follow.deleted_at IS NULL
                                LEFT JOIN educode_sales_assessments_settings AS ss ON ss.staff_id = educode_sales_sale_plans.staff_id
                                     AND YEAR(ss.assessment_year) = educode_sales_sale_plans.year AND ss.assessment = educode_sales_sale_plans.clazz
                                WHERE educode_sales_sale_plans.deleted_at IS NULL
                                GROUP BY educode_sales_sale_plans.staff_id, educode_sales_sale_plans.clazz, educode_sales_sale_plans.year
                              ) AS educode_sales_sale_plans
  ")
  if @current_admin.is_admin?
    # @sale_plans = SalePlan
  else
    level = @current_admin.role.role_areas.find_by(clazz: '销售计划').level
    case level
    when '自己'
      @sale_plans = @sale_plans.where(staff_id: @current_admin.id)
    when '区域'
      staff_ids = Staff.joins(user: [user_extension: [department: :school]]).where("schools.province in (?)", @current_admin.areas.pluck(:name)).pluck(:id)
      business_ids = Business.where(school_id: StaffSchool.where(staff_id: @current_admin.id).pluck(:school_id)).pluck(:id)
      @sale_plans = @sale_plans.where("educode_sales_sale_plans.staff_id in (?) OR educode_sales_sale_plans.staff_id = ? OR educode_sales_sale_plans.business_id in (?)", staff_ids, @current_admin.id,business_ids)
    else
      # @sale_plans = SalePlan
    end
  end
  @sale_plans = @sale_plans.where.not(year: nil)

  @sale_plans = @sale_plans.where(deleted_at: nil)

  if params[:q].present? && params[:q][:year].present?
    @sale_plans = @sale_plans.where(year: params[:q][:year])
  end
  if params[:q].present? && params[:q][:staff_id].present?
    @sale_plans = @sale_plans.where(staff_id: params[:q][:staff_id])
  end
  if params[:sort].present? && params[:sort][:field]
    @sale_plans = @sale_plans.order("#{params[:sort][:field]} #{params[:sort][:order]}")
  else
    @sale_plans = @sale_plans.order("created_at desc")
  end

  @sale_plans = @sale_plans.page(params[:page]).per(params[:limit])
end