educode_sales 1.10.58 → 1.10.70

Sign up to get free protection for your applications and to get access to all the features.
Files changed (56) hide show
  1. checksums.yaml +4 -4
  2. data/app/assets/images/educode_sales/indexlogo.png +0 -0
  3. data/app/assets/javascripts/educode_sales/application.js +1 -0
  4. data/app/assets/javascripts/educode_sales/functions.js +24 -0
  5. data/app/controllers/educode_sales/application_controller.rb +11 -3
  6. data/app/controllers/educode_sales/business_courses_controller.rb +19 -8
  7. data/app/controllers/educode_sales/follow_ups_controller.rb +4 -0
  8. data/app/controllers/educode_sales/plans_controller.rb +718 -11
  9. data/app/controllers/educode_sales/shixuns_controller.rb +20 -10
  10. data/app/controllers/educode_sales/subjects_controller.rb +10 -6
  11. data/app/controllers/educode_sales/teacher_follows_controller.rb +4 -0
  12. data/app/controllers/educode_sales/teachers_controller.rb +3 -0
  13. data/app/models/educode_sales/business.rb +5 -5
  14. data/app/models/educode_sales/permission.rb +7 -0
  15. data/app/models/educode_sales/sales_target.rb +10 -0
  16. data/app/models/educode_sales/sales_target_history.rb +11 -0
  17. data/app/models/educode_sales/sales_target_log.rb +11 -0
  18. data/app/views/educode_sales/business_courses/index.html.erb +1 -1
  19. data/app/views/educode_sales/business_courses/new.html.erb +2 -2
  20. data/app/views/educode_sales/businesses/_follows.html.erb +107 -100
  21. data/app/views/educode_sales/businesses/index.html.erb +6 -3
  22. data/app/views/educode_sales/businesses/index.json.jbuilder +2 -2
  23. data/app/views/educode_sales/businesses/show_follow.json.jbuilder +1 -1
  24. data/app/views/educode_sales/contracts/_follows.html.erb +75 -69
  25. data/app/views/educode_sales/contracts/_list.html.erb +283 -249
  26. data/app/views/educode_sales/contracts/index.json.jbuilder +1 -1
  27. data/app/views/educode_sales/follow_ups/index.json.jbuilder +1 -1
  28. data/app/views/educode_sales/plans/_year_execute.html.erb +428 -0
  29. data/app/views/educode_sales/plans/_year_target.html.erb +792 -0
  30. data/app/views/educode_sales/plans/index.html.erb +105 -83
  31. data/app/views/educode_sales/plans/staff_year_business.html.erb +96 -0
  32. data/app/views/educode_sales/plans/staff_year_business.json.jbuilder +22 -0
  33. data/app/views/educode_sales/plans/staff_year_target.json.jbuilder +35 -0
  34. data/app/views/educode_sales/plans/staff_year_target_log.html.erb +62 -0
  35. data/app/views/educode_sales/plans/staff_year_target_log.json.jbuilder +34 -0
  36. data/app/views/educode_sales/plans/year_execute.js.erb +1 -0
  37. data/app/views/educode_sales/plans/year_target.js.erb +1 -0
  38. data/app/views/educode_sales/plans/year_target.json.jbuilder +48 -0
  39. data/app/views/educode_sales/plans/years_all_target.json.jbuilder +54 -0
  40. data/app/views/educode_sales/sales_details/_index.html.erb +6 -3
  41. data/app/views/educode_sales/shixun_dectects/markdown.html.erb +40 -1
  42. data/app/views/educode_sales/shixuns/index.html.erb +106 -97
  43. data/app/views/educode_sales/shixuns/new.html.erb +2 -2
  44. data/app/views/educode_sales/staffs/index.json.jbuilder +1 -1
  45. data/app/views/educode_sales/subjects/index.html.erb +1 -1
  46. data/app/views/educode_sales/subjects/new.html.erb +2 -2
  47. data/app/views/educode_sales/teachers/_follows.html.erb +24 -17
  48. data/app/views/educode_sales/teachers/_index.html.erb +6 -0
  49. data/app/views/layouts/educode_sales/application.html.erb +7 -4
  50. data/config/environments/development.rb +16 -0
  51. data/config/routes.rb +14 -2
  52. data/db/migrate/20230729085359_create_educode_sales_sales_target_histories.rb +45 -0
  53. data/lib/educode_sales/version.rb +1 -1
  54. metadata +24 -8
  55. data/app/views/educode_sales/plans/_target_track.html.erb +0 -178
  56. data/app/views/educode_sales/plans/target_track.js.erb +0 -1
@@ -2,7 +2,7 @@ require_dependency "educode_sales/application_controller"
2
2
 
3
3
  module EducodeSales
4
4
  class PlansController < ApplicationController
5
-
5
+ protect_from_forgery
6
6
  def index
7
7
  # authorize! :read, SalePlan
8
8
  respond_to do |format|
@@ -93,7 +93,7 @@ module EducodeSales
93
93
  render_failure plan
94
94
  end
95
95
  end
96
-
96
+
97
97
  end
98
98
 
99
99
  def create_business_info
@@ -112,7 +112,7 @@ module EducodeSales
112
112
  common = Common.find_by(clazz: 'staff_type', name: '销售')
113
113
  @staffs = Staff.includes(:user).where(job_type: common.id).map { |d| [d.user.real_name, d.id] }
114
114
 
115
-
115
+
116
116
  @week = Time.now.strftime('%W').to_i
117
117
  week = Time.now.strftime('%W').to_i
118
118
  if week > 3
@@ -153,7 +153,7 @@ module EducodeSales
153
153
  @commons = Common.where(clazz: '计划类型').pluck(:name, :id)
154
154
  common = Common.find_by(clazz: 'staff_type', name: '销售')
155
155
  @staffs = Staff.includes(:user).where(job_type: common.id).map { |d| [d.user.real_name, d.id] }
156
-
156
+
157
157
  @business = Business.find_by(id: params[:business_id])
158
158
  business_ids = EducodeSales::AssignFollowUp.joins(:follow_up).where(staff_id: @current_admin.id).pluck :business_id
159
159
  @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)
@@ -181,7 +181,7 @@ module EducodeSales
181
181
  @businesses = @businesses.limit(10)
182
182
  end
183
183
  @businesses = @businesses.order(created_at: :desc)
184
-
184
+
185
185
  @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} }
186
186
  render json: { data: @businesses, code: 0, msg: "success" }
187
187
  end
@@ -233,7 +233,7 @@ module EducodeSales
233
233
  business_ids << @sale_plan.business_id
234
234
  @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)
235
235
  @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] }
236
-
236
+
237
237
  if @sale_plan.weekly.present?
238
238
  week = Time.now.strftime('%W').to_i
239
239
  if week > 3
@@ -389,6 +389,60 @@ module EducodeSales
389
389
  @sale_plans = @sale_plans.page(params[:page]).per(params[:limit])
390
390
  end
391
391
 
392
+ def years_all_target
393
+ #目标类别
394
+ target_clazz = params[:target_clazz]
395
+ #年份
396
+ year = params[:year].present? ? params[:year].to_i : Date.today.year.to_i
397
+ type = params[:type] || 'staff'
398
+ #员工id
399
+ staff_id = params[:staff_id].present? ? params[:staff_id] : nil
400
+ #客户类型
401
+ school_tag_id = params[:school_tag_id]
402
+
403
+ staffs = get_sales_staffs_by_id(staff_id, params[:page], params[:limit])
404
+ if staffs.nil?
405
+ return render json: {success: false, msg: '无此数据或权限不足'}
406
+ end
407
+ if type == 'all'
408
+ @data = get_sales_all_plan(staffs, type, year, school_tag_id)
409
+ unless @data.empty?
410
+ @data[0]['id'] = 0
411
+ end
412
+ @total_count = 1
413
+ else
414
+ @total_count = staffs.count
415
+ @data = []
416
+ staffs.each do |staff|
417
+ @data += get_sales_all_plan([staff], type, year, school_tag_id)
418
+ end
419
+ end
420
+ unless @data.empty?
421
+ @data = @data.reject { |item| item['target_clazz'].nil? || (target_clazz.present? && !target_clazz.include?(item['target_clazz']))}
422
+ current_months = Time.now.month
423
+ @data.each do |item|
424
+ item['year_plan'] = 0
425
+ (13 - current_months).times do |i|
426
+ column = 'plan_' + (i + current_months).to_s
427
+ item['year_plan'] += item[column].present? ? item[column] : 0
428
+ end
429
+ end
430
+
431
+ if type != 'all'
432
+ tmp_id = 1
433
+ last_id = 0
434
+ @data.each do |item|
435
+ if last_id > 0 && item['id'] != last_id
436
+ tmp_id += 1
437
+ end
438
+ last_id = item['id']
439
+ item['staff_id'] = last_id
440
+ item['id'] = tmp_id + (params[:page].to_i-1)*params[:limit].to_i
441
+ end
442
+ end
443
+ end
444
+ end
445
+
392
446
  def my_years_plan
393
447
  @sale_plans = SalePlan.where(staff_id: @current_admin.id)
394
448
  end
@@ -511,15 +565,314 @@ module EducodeSales
511
565
  end
512
566
 
513
567
 
514
- def target_track
568
+ def year_execute
515
569
  respond_to do |format|
516
570
  format.html do
517
-
571
+ @staffs = get_sales_staffs_by_id().map { |d| [d.user.real_name, d.id] }
572
+ gon.school_tags = SchoolTag.where(for_missions: true).map { |d| {value: d.id, name: d.name } }
518
573
  end
519
574
  format.js do
520
- common = Common.find_by(clazz: 'staff_type', name: '销售')
521
- @staffs = Staff.joins(:user).where(job_type: common.id).map { |d| [d.user.real_name, d.id]}
575
+ @staffs = get_sales_staffs_by_id().map { |d| [d.user.real_name, d.id] }
576
+ gon.school_tags = SchoolTag.where(for_missions: true).map { |d| {value: d.id, name: d.name } }
577
+ gon.school_tags.unshift({value: 0, name: '全部类型'})
578
+ end
579
+ end
580
+ end
581
+
582
+ def year_target
583
+ respond_to do |format|
584
+ format.html do
585
+ end
586
+ format.js do
587
+ @staffs = get_sales_staffs_by_id().map { |d| [d.user.real_name, d.id] }
588
+ gon.school_tags = SchoolTag.where(for_missions: true).map { |d| {value: d.id, name: d.name } }
589
+ gon.school_tags.unshift({value: 0, name: '全部类型'})
590
+ gon.month = Time.now.month
591
+ end
592
+ format.json do
593
+ if @current_admin.is_admin?
594
+ @data = EducodeSales::SalesTargetHistory.all
595
+ else
596
+ level = @current_admin.role.role_areas.find_by(clazz: '销售计划').level
597
+ case level
598
+ when '自己'
599
+ @data = EducodeSales::SalesTargetHistory.where(staff_id: @current_admin.id)
600
+ when '区域'
601
+ @data = EducodeSales::SalesTargetHistory.all
602
+ staff_ids = Staff.joins(user: [user_extension: [department: :school]]).where("schools.province in (?)", @current_admin.areas.pluck(:name)).pluck(:id)
603
+ @data = @data.where(staff_id: staff_ids)
604
+ else
605
+ @data = EducodeSales::SalesTargetHistory.all
606
+ end
607
+ end
608
+ @data = @data.order("staff_id asc,num desc, is_use desc,field(state, 1,0,2), id desc")
609
+ if params[:year].present?
610
+ @data = @data.where(year: params[:year])
611
+ end
612
+ if params[:school_tag_id].present?
613
+ @data = @data.where(school_tag_id: params[:school_tag_id])
614
+ end
615
+ if params[:staff_id].present?
616
+ @data = @data.where(staff_id: params[:staff_id])
617
+ end
618
+ if params[:year_target].present?
619
+ @data = @data.where(year_target: params[:year_target])
620
+ end
621
+ if params[:target_clazz].present?
622
+ @data = @data.where(target_clazz: params[:target_clazz])
623
+ end
624
+ @data = @data.page(params[:page]).per(params[:limit])
625
+ end
626
+ end
627
+ end
628
+
629
+ def cal_finish_amount
630
+ start_at = params[:year] + "-01-01"
631
+ end_at = params[:year] + "-12-31"
632
+ amount = 0
633
+ case params[:target_clazz]
634
+ when "回款"
635
+ # 对应销售经理下回款时间的回款金额
636
+ amount = EducodeSales::MoneyPlanRecord.joins("
637
+ LEFT JOIN educode_sales_follow_ups AS last_follow_up ON educode_sales_businesses.last_follow_up_id = last_follow_up.id AND last_follow_up.deleted_at IS NULL
638
+ LEFT JOIN educode_sales_assign_follow_ups ON educode_sales_assign_follow_ups.follow_up_id = last_follow_up.id").
639
+ joins(business: [department: [school: :school_tags]]).where("school_tags.id = ?", params[:school_tag_id]).
640
+ where("(educode_sales_assign_follow_ups.id IS NOT NULL AND educode_sales_assign_follow_ups.staff_id = ?) OR (educode_sales_assign_follow_ups.id IS NULL AND educode_sales_businesses.staff_id = ?)", params[:staff_id], params[:staff_id]).
641
+ where("date_at >= ? AND date_at <= ?", start_at, end_at).sum("amount")
642
+
643
+ when '商机'
644
+ a_clazz = EducodeSales::Common.find_by(extras: 'a_class')
645
+ start_at = "#{params[:year]}-01-01 00:00:01"
646
+ end_at = "#{params[:year]}-12-31 23:59:59"
647
+ staff_id = params[:staff_id]
648
+ # 对应销售经理下A类商机预算金额
649
+ amount = EducodeSales::Business.from("(
650
+ SELECT distinct educode_sales_businesses.id, last_follow_up.budget_amount, educode_sales_businesses.deleted_at, educode_sales_businesses.department_id
651
+ FROM educode_sales_businesses
652
+ JOIN educode_sales_follow_ups ON educode_sales_follow_ups.business_id = educode_sales_businesses.id AND educode_sales_businesses.clazz_id = #{a_clazz.id}
653
+ AND educode_sales_follow_ups.created_at > '#{start_at}' AND educode_sales_follow_ups.created_at < '#{end_at}'
654
+ JOIN educode_sales_follow_ups AS last_follow_up ON educode_sales_businesses.last_follow_up_id = last_follow_up.id AND last_follow_up.deleted_at IS NULL AND last_follow_up.clazz_id = #{a_clazz.id}
655
+ LEFT JOIN educode_sales_assign_follow_ups ON educode_sales_assign_follow_ups.follow_up_id = last_follow_up.id
656
+ WHERE (educode_sales_assign_follow_ups.id IS NOT NULL AND educode_sales_assign_follow_ups.staff_id = '#{staff_id}') OR (educode_sales_assign_follow_ups.id IS NULL AND educode_sales_businesses.staff_id = '#{staff_id}')
657
+ ) AS educode_sales_businesses
658
+ ").joins(department: [school: :school_tags]).where("school_tags.id = ?", params[:school_tag_id]).sum(:budget_amount)
659
+ when '中标'
660
+ # 对应销售经理下合同列表的中标时间下合同金额
661
+ contract_ids = EducodeSales::Common.where(clazz: '商机阶段', name: ['已中标', '已签单', '已验收', '回款中', '服务中', '已结束']).pluck(:id)
662
+ businesses = EducodeSales::Business.joins("
663
+ JOIN educode_sales_follow_ups ON educode_sales_businesses.last_follow_up_id = educode_sales_follow_ups.id
664
+ ").joins("LEFT JOIN educode_sales_assign_follow_ups ON educode_sales_assign_follow_ups.follow_up_id = educode_sales_businesses.last_follow_up_id").
665
+ joins(department: [school: :school_tags]).where("school_tags.id = ?", params[:school_tag_id]).
666
+ where("(educode_sales_assign_follow_ups.id IS NOT NULL AND educode_sales_assign_follow_ups.staff_id = ?) OR (educode_sales_assign_follow_ups.id IS NULL AND educode_sales_businesses.staff_id = ?)", params[:staff_id], params[:staff_id]).
667
+ where("educode_sales_follow_ups.stage_id in (?)", contract_ids).
668
+ where("educode_sales_follow_ups.bidded_date >= ? AND educode_sales_follow_ups.bidded_date <= ?", start_at, end_at).sum(:actual_amount)
669
+ end
670
+ render json: {amount: amount}
671
+ end
672
+
673
+ def add_year_target
674
+ history = EducodeSales::SalesTargetHistory.find_by(staff_id: params[:staff_id], year: params[:year], school_tag_id: params[:school_tag_id], target_clazz: params[:target_clazz])
675
+ # history.update_attributes(params.permit(:m_target_1, :m_target_2, :m_target_3, :m_target_4, :m_target_5, :m_target_6, :m_target_7, :m_target_8, :m_target_9, :m_target_10, :m_target_11, :m_target_12)) if history
676
+ # 不能重复添加
677
+ #if EducodeSales::SalesTargetHistory.exists?(staff_id: params[:staff_id], year: params[:year], school_tag_id: params[:school_tag_id], target_clazz: params[:target_clazz])
678
+ # return render_failure "不能重复添加"
679
+ #end
680
+ target = EducodeSales::SalesTargetHistory.new(year_target_param)
681
+ if history
682
+ use_old_attributes = %w[m_target_1 m_target_2 m_target_3 m_target_4 m_target_5 m_target_6 m_target_7 m_target_8 m_target_9 m_target_10 m_target_11 m_target_12] - params.keys
683
+ use_old_attributes.each do |attr|
684
+ target[attr] = history[attr]
685
+ end
686
+ end
687
+ if history
688
+ target.num = history.num
689
+ else
690
+ target.num = EducodeSales::SalesTargetHistory.maximum(:id).to_i + 1
691
+ end
692
+
693
+ target.creator_id = @current_admin.id
694
+ target.state = '待审核'
695
+ if target.save
696
+ render json: { id: target.id, state: target.state, updated_at: target.updated_at.to_s(:date) }
697
+ else
698
+ render_failure target
699
+ end
700
+ end
701
+
702
+ def staff_year_target
703
+ respond_to do |format|
704
+ format.html do
705
+ end
706
+ format.js do
707
+ @staffs = get_sales_staffs_by_id().map { |d| [d.user.real_name, d.id] }
522
708
  gon.school_tags = SchoolTag.where(for_missions: true).map { |d| {value: d.id, name: d.name } }
709
+ gon.month = Time.now.month
710
+ end
711
+ format.json do
712
+ if @current_admin.is_admin?
713
+ @data = EducodeSales::SalesTarget.all
714
+ else
715
+ level = @current_admin.role.role_areas.find_by(clazz: '销售计划').level
716
+ case level
717
+ when '自己'
718
+ @data = EducodeSales::SalesTarget.where(staff_id: @current_admin.id)
719
+ when '区域'
720
+ @data = EducodeSales::SalesTarget.all
721
+ staff_ids = Staff.joins(user: [user_extension: [department: :school]]).where("schools.province in (?)", @current_admin.areas.pluck(:name)).pluck(:id)
722
+ @data = @data.where(staff_id: staff_ids)
723
+ else
724
+ @data = EducodeSales::SalesTarget.all
725
+ end
726
+ end
727
+ @data = @data.order("staff_id asc,field(state, 1,2,0), id desc")
728
+ if params[:year].present?
729
+ @data = @data.where(year: params[:year])
730
+ end
731
+ if params[:school_tag_id].present?
732
+ @data = @data.where(school_tag_id: params[:school_tag_id])
733
+ end
734
+ if params[:staff_id].present?
735
+ @data = @data.where(staff_id: params[:staff_id])
736
+ end
737
+ if params[:target_clazz].present?
738
+ @data = @data.where(target_clazz: params[:target_clazz])
739
+ end
740
+ @data = @data.page(params[:page]).per(params[:limit])
741
+ end
742
+ end
743
+ end
744
+
745
+ def save_staff_year_target
746
+ unless params[:id].present?
747
+ history = EducodeSales::SalesTarget.find_by(staff_id: params[:staff_id], year: params[:year], school_tag_id: params[:school_tag_id], target_clazz: params[:target_clazz])
748
+ # 不能重复添加
749
+ unless history.nil?
750
+ return render_failure "同类型数据已存在,无法添加"
751
+ end
752
+ target = EducodeSales::SalesTarget.new(year_target_param)
753
+ else
754
+ target = EducodeSales::SalesTarget.find(params[:id])
755
+ if target.nil?
756
+ return render_failure "数据不存在,无法编辑"
757
+ end
758
+ target.update_attributes(year_target_param)
759
+ end
760
+
761
+ target.creator_id = @current_admin.id
762
+ target.state = '待审核'
763
+ if target.save
764
+ create_target_operation_log(target.attributes.dup)
765
+ render json: { id: target.id, state: target.state, updated_at: target.updated_at.to_s(:date) }
766
+ else
767
+ render_failure target
768
+ end
769
+ end
770
+
771
+ def audit_staff_year_target
772
+ target = EducodeSales::SalesTarget.find(params[:id])
773
+ if target.state == '待审核'
774
+ target.state = params[:state]
775
+ target.comment = params[:comment]
776
+ target.reviewer_id = @current_admin.id
777
+ if target.save
778
+ create_target_operation_log(target.attributes.dup)
779
+ p
780
+ render json: {id: target.id, comment: target.comment, reviewer: @current_admin.user&.real_name, state: target.state, updated_at: target.updated_at.to_s(:date)}
781
+ else
782
+ render_failure target
783
+ end
784
+ else
785
+ render json: {success: false, msg: '该状态不能编辑'}
786
+ end
787
+ end
788
+
789
+ def staff_year_target_log
790
+ @data = EducodeSales::SalesTargetLog.where(target_id: params[:target_id]).order('id desc').page(params[:page]).per(params[:limit])
791
+ respond_to do |format|
792
+ format.html do
793
+ render layout: false
794
+ end
795
+ format.json do
796
+ end
797
+ end
798
+ end
799
+
800
+ def staff_year_business
801
+ respond_to do |format|
802
+ format.html do
803
+ @staff_id = (params[:staff_id].blank? || params[:staff_id] == 'null') ? nil : params[:staff_id]
804
+ @year = params[:year].present? ? params[:year].to_i : Date.today.year.to_i
805
+ @target_clazz = params[:target_clazz].present? ? params[:target_clazz] : '中标'
806
+ @type = params[:type]
807
+ @school_tag_id = params[:school_tag_id]
808
+ render layout: false
809
+ end
810
+ format.json do
811
+ staff_id = (params[:staff_id].blank? || params[:staff_id] == 'null') ? nil : params[:staff_id]
812
+ staffs = get_sales_staffs_by_id(staff_id)
813
+ if staffs.nil?
814
+ return render json: {success: false, msg: '无此数据或权限不足'}
815
+ end
816
+ @year = params[:year].present? ? params[:year].to_i : Date.today.year.to_i
817
+ month = params[:month].present? ? params[:month].to_i : nil
818
+ @target_clazz = params[:target_clazz].present? ? params[:target_clazz] : '中标'
819
+ school_tag_id = params[:school_tag_id].present? && params[:school_tag_id]!='0' ? params[:school_tag_id] : nil
820
+ type = params[:type].present? ? params[:type] : 'plan'
821
+
822
+ if month.present? && month != 0
823
+ first_day = Date.new(@year, month, 1)
824
+ last_day = Date.new(@year, month, -1)
825
+ else
826
+ first_day = Date.new(@year, 1, 1)
827
+ last_day = Date.new(@year, 12, -1)
828
+ end
829
+
830
+ #staffs = get_sales_staffs_by_id(staff_id)
831
+ staff_ids = staffs.pluck(:id).compact.join(',')
832
+ follow_up_ids = get_follow_up_id(staff_ids)
833
+ if follow_up_ids.empty?
834
+ return
835
+ end
836
+ school_where = ''
837
+ unless follow_up_ids.empty?
838
+ ##查出哪些学校既属于本科又属于军事院校
839
+ school_ids = EducodeSales::MoneyPlanRecord.from(
840
+ "(SELECT TM1.school_id
841
+ FROM school_tag_middles AS TM1
842
+ LEFT JOIN school_tag_middles AS TM2
843
+ ON TM1.school_id = TM2.school_id AND TM1.school_tag_id=1 AND TM2.school_tag_id=10
844
+ WHERE TM1.school_tag_id IS NOT NULL AND TM2.school_tag_id IS NOT NULL AND TM1.school_id IN (
845
+ SELECT school_id FROM educode_sales_businesses WHERE last_follow_up_id in (#{follow_up_ids})
846
+ )) AS school"
847
+ ).pluck(:school_id).join(',')
848
+ unless school_ids.empty?
849
+ school_where = " AND (TM.school_id in (#{school_ids}) AND TM.school_tag_id = 10 OR TM.school_id not in (#{school_ids}))"
850
+ end
851
+ end
852
+ follow_up_ids_where = follow_up_ids.present? ? " AND B.last_follow_up_id in (#{follow_up_ids})" : " AND B.id in(0)"
853
+ if type == 'plan'
854
+ case @target_clazz
855
+ when '中标'
856
+ @data = get_staff_bidden_plan(follow_up_ids_where, school_where, first_day, last_day, school_tag_id)
857
+ when '回款'
858
+ @data = get_staff_money_plan(follow_up_ids_where, school_where, first_day, last_day, school_tag_id)
859
+ when '商机'
860
+ @data = get_staff_business_plan(follow_up_ids_where, school_where, first_day, last_day, school_tag_id)
861
+ else
862
+ # type code here
863
+ end
864
+ else
865
+ case @target_clazz
866
+ when '中标'
867
+ @data = get_staff_bidden_finish(follow_up_ids_where, school_where, first_day, last_day, school_tag_id)
868
+ when '回款'
869
+ @data = get_staff_money_finish(follow_up_ids_where, school_where, first_day, last_day, school_tag_id)
870
+ when '商机'
871
+ @data = get_staff_business_finish(follow_up_ids_where, school_where, first_day, last_day, school_tag_id)
872
+ else
873
+ end
874
+ end
875
+ @data = @data.page(params[:page]).per(params[:limit])
523
876
  end
524
877
  end
525
878
  end
@@ -536,8 +889,362 @@ module EducodeSales
536
889
 
537
890
  def update_business_info_extra_params
538
891
  params.permit(:plan_bid_on, :actual_bidded_on, :bidded_amount, :plan_sign_on, :actual_sign_on, :sign_amount, :plan_deploy_on, :actual_deploy_on, :plan_check_on,
539
- :actual_check_on, :prepayment_plan_on, :prepayment_actual_on, :prepayment_amount, :check_fee_plan_on, :check_fee_actual_on, :check_fee, :qa_plan_on, :qa_actual_on, :qa_amount, :account_receivable)
892
+ :actual_check_on, :prepayment_plan_on, :prepayment_actual_on, :prepayment_amount, :check_fee_plan_on, :check_fee_actual_on, :check_fee, :qa_plan_on, :qa_actual_on, :qa_amount, :account_receivable)
893
+ end
894
+
895
+ def year_target_param
896
+ params.permit(:staff_id, :year, :target_clazz, :school_tag_id, :year_finish, :year_target, :year_diff, :target_1, :target_2, :target_3, :target_4, :target_5, :target_6, :target_7, :target_8, :target_9, :target_10, :target_11, :target_12)
540
897
  end
541
898
 
899
+ def make_target_data(sales_all_plan, data, column, type)
900
+ data.each do |item|
901
+ _column = "#{column}_#{item.m.to_i.to_s}"
902
+ if sales_all_plan[type][item.school_tag_id].present?
903
+ if sales_all_plan[type][item.school_tag_id][_column].present?
904
+ sales_all_plan[type][item.school_tag_id][_column] += item.amount.to_i
905
+ else
906
+ sales_all_plan[type][item.school_tag_id][_column] = item.amount.to_i
907
+ end
908
+ else
909
+ sales_all_plan[type][item.school_tag_id] = {"school_tag_id" => item.school_tag_id, "school_tag_name" => item.school_tag_name}
910
+ sales_all_plan[type][item.school_tag_id][_column] = item.amount.to_i
911
+ end
912
+ sales_all_plan[type][item.school_tag_id]['type'] = type
913
+ end
914
+ sales_all_plan
915
+ end
916
+
917
+
918
+ def get_sales_all_plan(staffs, type, year, school_tag_id)
919
+ if staffs.empty?
920
+ return []
921
+ end
922
+ sales_all_plan = {:bidder => {}, :business => {}, :money => {}}
923
+ staff_ids = staffs.pluck(:id).join(',')
924
+ select_school_tag_id = school_tag_id.blank? ? '0 as school_tag_id' : 'school_tag_id'
925
+ and_where_school_tag_id = school_tag_id.present? && school_tag_id !='0' ? " AND school_tag_id in (#{school_tag_id})" : ""
926
+ # 中标&商机&回款 - 目标按月
927
+ sales_target_histories = EducodeSales::SalesTarget.from("
928
+ (SELECT H.id,target_clazz,#{select_school_tag_id},T.name as school_tag_name,sum(target_1) as target_1, sum(target_2) target_2, sum(target_3) target_3, sum(target_4) target_4, sum(target_5) target_5, sum(target_6) target_6, sum(target_7) target_7,sum(target_8) target_8,sum(target_9) target_9, sum(target_10) target_10, sum(target_11) target_11, sum(target_12) target_12
929
+ FROM educode_sales_sales_targets as H
930
+ LEFT JOIN school_tags AS T ON H.school_tag_id = T.id
931
+ WHERE state=1 AND year = #{year} AND T.for_missions = 1
932
+ AND staff_id in( #{staff_ids})
933
+ #{and_where_school_tag_id}
934
+ GROUP BY target_clazz #{school_tag_id.present? ? ",school_tag_id" : ""}
935
+ ) as educode_sales_sales_targets
936
+ ").order('target_clazz asc, school_tag_id asc,id desc')
937
+
938
+ sales_target_histories.each do |history|
939
+ case history.target_clazz
940
+ when '中标'
941
+ sales_all_plan[:bidder][history.school_tag_id] = history.attributes.except('id')
942
+ when '商机'
943
+ sales_all_plan[:business][history.school_tag_id] = history.attributes.except('id')
944
+ when '回款'
945
+ sales_all_plan[:money][history.school_tag_id] = history.attributes.except('id')
946
+ else
947
+ end
948
+ end
949
+
950
+ ##follow_up_ids = EducodeSales::AssignFollowUp.where(staff_id: staffs.pluck(:id)).pluck(:follow_up_id).compact.join(',')
951
+ follow_up_ids = EducodeSales::Business
952
+ .left_joins(last_follow_up: :assign_follow_ups)
953
+ .where("educode_sales_businesses.last_follow_up_id IS NOT NULL AND (educode_sales_businesses.staff_id in (#{staff_ids}) AND educode_sales_assign_follow_ups.id is null) OR educode_sales_assign_follow_ups.staff_id in (#{staff_ids})")
954
+ .pluck(:last_follow_up_id)
955
+ .compact
956
+ .join(',')
957
+
958
+ school_where = ''
959
+ unless follow_up_ids.empty?
960
+ ##查出哪些学校既属于本科又属于军事院校
961
+ school_ids = EducodeSales::MoneyPlanRecord.from(
962
+ "(SELECT TM1.school_id
963
+ FROM school_tag_middles AS TM1
964
+ LEFT JOIN school_tag_middles AS TM2
965
+ ON TM1.school_id = TM2.school_id AND TM1.school_tag_id=1 AND TM2.school_tag_id=10
966
+ WHERE TM1.school_tag_id IS NOT NULL AND TM2.school_tag_id IS NOT NULL AND TM1.school_id IN (
967
+ SELECT school_id FROM educode_sales_businesses WHERE last_follow_up_id in (#{follow_up_ids})
968
+ )) AS school"
969
+ ).pluck(:school_id).join(',')
970
+
971
+ unless school_ids.empty?
972
+ school_where = "AND ((TM.school_id in (#{school_ids}) AND TM.school_tag_id = 10) OR TM.school_id not in (#{school_ids}))"
973
+ end
974
+ end
975
+ start_at = Date.new(year, 1, 1)
976
+ end_at = Date.new(year, 12, -1)
977
+ follow_up_ids_where = follow_up_ids.present? ? " AND B.last_follow_up_id in (#{follow_up_ids})" : " AND B.id in(0)"
978
+ select_columns = 'amount,m,' + select_school_tag_id + ',school_tag_name';
979
+ group_by = school_tag_id == '0' ? 'm' : 'school_tag_id, m'
980
+ # 中标 - 完成 - 合同额(actual_amount) 中标时间 bidded_date
981
+ bidder_finish = EducodeSales::FollowUp.select(select_columns).from(
982
+ "(SELECT sum(F.actual_amount) as amount, date_format(F.bidded_date, '%m') as m,T.name as school_tag_name,
983
+ TM.school_tag_id as school_tag_id,F.deleted_at
984
+ FROM educode_sales_businesses AS B
985
+ LEFT JOIN educode_sales_follow_ups as F ON B.last_follow_up_id=F.id
986
+ LEFT JOIN school_tag_middles AS TM ON TM.school_id=B.school_id
987
+ LEFT JOIN school_tags as T ON TM.school_tag_id=T.id
988
+ WHERE B.deleted_at IS NULL AND F.deleted_at IS NULL AND T.for_missions = 1 AND F.bidded_date BETWEEN '#{start_at}' AND '#{end_at}'
989
+ #{follow_up_ids_where.present? ? follow_up_ids_where : ''}
990
+ #{school_where.present? ? school_where : ''}
991
+ #{and_where_school_tag_id}
992
+ GROUP BY #{group_by}
993
+ ) AS educode_sales_follow_ups"
994
+ ).order('school_tag_id ASC, m asc')
995
+
996
+ # 中标 - 计划 金额invitation_money 时间invitation_at
997
+ bidder_plan = EducodeSales::FollowUp.select(select_columns).from(
998
+ "(SELECT sum(invitation_money) as amount, date_format(invitation_at, '%m') as m,T.name as school_tag_name,
999
+ TM.school_tag_id as school_tag_id,F.deleted_at
1000
+ FROM educode_sales_businesses AS B
1001
+ LEFT JOIN educode_sales_follow_ups as F ON B.last_follow_up_id=F.id
1002
+ LEFT JOIN school_tag_middles AS TM ON TM.school_id=B.school_id
1003
+ LEFT JOIN school_tags as T ON TM.school_tag_id=T.id AND T.for_missions = 1
1004
+ WHERE B.deleted_at IS NULL AND F.deleted_at IS NULL AND T.for_missions = 1 AND F.invitation_at BETWEEN '#{start_at}' AND '#{end_at}'
1005
+ #{follow_up_ids_where.present? ? follow_up_ids_where : ''}
1006
+ #{school_where.present? ? school_where : ''}
1007
+ #{and_where_school_tag_id}
1008
+ GROUP BY #{group_by}
1009
+ ORDER BY school_tag_id ASC
1010
+ ) AS educode_sales_follow_ups"
1011
+ ).order('school_tag_id ASC, m asc')
1012
+ # 回款 - 完成
1013
+ money_finish = EducodeSales::MoneyPlanRecord.select(select_columns).from(
1014
+ "(SELECT sum(amount) as amount, date_format(R.date_at, '%m') as m,T.name as school_tag_name,
1015
+ TM.school_tag_id as school_tag_id
1016
+ FROM educode_sales_money_plan_records as R
1017
+ LEFT JOIN educode_sales_businesses AS B ON R.business_id=B.id
1018
+ LEFT JOIN school_tag_middles AS TM ON TM.school_id=B.school_id
1019
+ LEFT JOIN school_tags as T ON TM.school_tag_id=T.id AND T.for_missions = 1
1020
+ WHERE B.deleted_at IS NULL AND T.for_missions = 1 AND R.date_at BETWEEN '#{start_at}' AND '#{end_at}'
1021
+ #{follow_up_ids_where.present? ? follow_up_ids_where : ''}
1022
+ #{school_where.present? ? school_where : ''}
1023
+ #{and_where_school_tag_id}
1024
+ GROUP BY #{group_by}
1025
+ ) AS educode_sales_money_plan_records"
1026
+ ).order('school_tag_id ASC, m asc')
1027
+ # 回款 - 计划 .group_by { |d| d.m }
1028
+ money_plan = EducodeSales::FollowUp.select(select_columns).from(
1029
+ "(SELECT sum(plan_return_money) as amount, date_format(plan_return_date, '%m') as m,T.name as school_tag_name,
1030
+ F.deleted_at,TM.school_tag_id as school_tag_id
1031
+ FROM educode_sales_businesses AS B
1032
+ LEFT JOIN educode_sales_follow_ups as F ON B.last_follow_up_id=F.id
1033
+ LEFT JOIN school_tag_middles AS TM ON TM.school_id=B.school_id
1034
+ LEFT JOIN school_tags as T ON TM.school_tag_id=T.id AND T.for_missions = 1
1035
+ WHERE B.deleted_at IS NULL AND F.deleted_at IS NULL AND T.for_missions = 1 AND plan_return_date BETWEEN '#{start_at}' AND '#{end_at}'
1036
+ #{follow_up_ids_where.present? ? follow_up_ids_where : ''}
1037
+ #{school_where.present? ? school_where : ''}
1038
+ #{and_where_school_tag_id}
1039
+ GROUP BY #{group_by}
1040
+ ORDER BY school_tag_id ASC
1041
+ ) AS educode_sales_follow_ups"
1042
+ ).order('school_tag_id ASC, m asc')
1043
+ # 商机-完成 .where(:bidded_date => start_datetime..end_datetime)
1044
+ business_finish = EducodeSales::FollowUp.select(select_columns).from(
1045
+ "(SELECT sum(budget_amount) as amount, date_format(bidded_date, '%m') as m,T.name as school_tag_name,
1046
+ F.deleted_at,TM.school_tag_id as school_tag_id
1047
+ FROM educode_sales_businesses AS B
1048
+ LEFT JOIN educode_sales_follow_ups as F ON B.last_follow_up_id=F.id
1049
+ LEFT JOIN school_tag_middles AS TM ON TM.school_id=B.school_id
1050
+ LEFT JOIN school_tags as T ON TM.school_tag_id=T.id AND T.for_missions = 1
1051
+ WHERE B.deleted_at IS NULL AND F.deleted_at IS NULL AND T.for_missions = 1 AND bidded_date BETWEEN '#{start_at}' AND '#{end_at}'
1052
+ #{follow_up_ids_where.present? ? follow_up_ids_where : ''}
1053
+ #{school_where.present? ? school_where : ''}
1054
+ #{and_where_school_tag_id}
1055
+ GROUP BY #{group_by}
1056
+ ) AS educode_sales_follow_ups
1057
+ "
1058
+ ).order('school_tag_id ASC, m asc')
1059
+ # 商机-计划
1060
+ business_plan = EducodeSales::FollowUp.select(select_columns).from(
1061
+ "(SELECT sum(plan_a_money) as amount, date_format(plan_a_date, '%m') as m,T.name as school_tag_name,
1062
+ F.deleted_at,TM.school_tag_id as school_tag_id
1063
+ FROM educode_sales_businesses AS B
1064
+ LEFT JOIN educode_sales_follow_ups as F ON B.last_follow_up_id=F.id
1065
+ LEFT JOIN school_tag_middles AS TM ON TM.school_id=B.school_id
1066
+ LEFT JOIN school_tags as T ON TM.school_tag_id=T.id AND T.for_missions = 1
1067
+ WHERE B.deleted_at IS NULL AND F.deleted_at IS NULL AND T.for_missions = 1 AND plan_a_date BETWEEN '#{start_at}' AND '#{end_at}'
1068
+ #{follow_up_ids_where.present? ? follow_up_ids_where : ''}
1069
+ #{school_where.present? ? school_where : ''}
1070
+ #{and_where_school_tag_id}
1071
+ GROUP BY #{group_by}
1072
+ ORDER BY school_tag_id ASC
1073
+ ) AS educode_sales_follow_ups"
1074
+ ).order('school_tag_id ASC, m asc')
1075
+ #中标完成
1076
+ sales_all_plan = make_target_data(sales_all_plan, bidder_finish, 'finish', :bidder)
1077
+ #中标计划
1078
+ sales_all_plan = make_target_data(sales_all_plan, bidder_plan, 'plan', :bidder)
1079
+ #回款完成
1080
+ sales_all_plan = make_target_data(sales_all_plan, money_finish, 'finish', :money)
1081
+ #回款计划
1082
+ sales_all_plan = make_target_data(sales_all_plan, money_plan, 'plan', :money)
1083
+ #商机完成
1084
+ sales_all_plan = make_target_data(sales_all_plan, business_finish, 'finish', :business)
1085
+ #商机计划
1086
+ sales_all_plan = make_target_data(sales_all_plan, business_plan, 'plan', :business)
1087
+
1088
+ data = sales_all_plan[:bidder].values + sales_all_plan[:business].values + sales_all_plan[:money].values
1089
+
1090
+ if data.empty?
1091
+ return []
1092
+ else
1093
+ p school_tag_id
1094
+ data.each do |item|
1095
+ item['id'] = type=='staff' ? staffs[0].id : 0
1096
+ item['name'] = type=='staff' ? staffs[0].name : ''
1097
+ item['target_clazz'] = item['target_clazz'].present? ? item['target_clazz'] : SalesTargetHistory::TARGET_CLAZZ_NAME[item['type']]
1098
+ item['school_tag_id'] = school_tag_id.present? ? item['school_tag_id'] : 0
1099
+ item['school_tag_name'] = school_tag_id.present? ? item['school_tag_name'] : '全部'
1100
+ end
1101
+ end
1102
+ end
1103
+
1104
+ def get_sales_staffs_by_id(staff_id=nil, page=nil, limit=nil)
1105
+ role_ids = EducodeSales::Role.where(name: ['销售总监','销售区域总监', '销售经理']).pluck(:id)
1106
+ staffs = Staff.where(role_id: role_ids)
1107
+ unless staff_id.nil?
1108
+ staffs = staffs.where(id: staff_id)
1109
+ end
1110
+ unless staffs.nil?
1111
+ if @current_admin.is_admin?
1112
+ else
1113
+ level = @current_admin.role.role_areas.find_by(clazz: '销售计划').level
1114
+ case level
1115
+ when '自己'
1116
+ staffs = staffs.where(id: @current_admin.id)
1117
+ when '区域'
1118
+ staffs = staffs.joins(user: [user_extension: [department: :school]]).where("schools.province in (?)", @current_admin.areas.pluck(:name))
1119
+ else
1120
+ end
1121
+ end
1122
+ end
1123
+ if !page.nil? && !limit.nil?
1124
+ staffs.page(params[:page]).per(params[:limit])
1125
+ end
1126
+ staffs
1127
+ end
1128
+
1129
+ def create_target_operation_log(target)
1130
+ id = target['id']
1131
+ target.delete('id')
1132
+ target.delete('created_at')
1133
+ target.delete('updated_at')
1134
+ log_model = EducodeSales::SalesTargetLog.new(target)
1135
+ log_model.target_id = id
1136
+ log_model.save
1137
+ end
1138
+
1139
+ def get_staff_bidden_plan(follow_up_ids_where, school_where, start_at, end_at, school_tag_id)
1140
+ EducodeSales::Business.from(
1141
+ "(SELECT B.id,B.name,B.school_id,B.department_id,B.clazz_id,B.last_follow_up_id,B.staff_id,F.invitation_money as money, F.invitation_at as time,
1142
+ F.created_at as created_at,T.name as school_tag_name,TM.school_tag_id as school_tag_id,F.deleted_at
1143
+ FROM educode_sales_businesses AS B
1144
+ LEFT JOIN educode_sales_follow_ups as F ON B.last_follow_up_id=F.id
1145
+ LEFT JOIN school_tag_middles AS TM ON TM.school_id=B.school_id
1146
+ LEFT JOIN school_tags as T ON TM.school_tag_id=T.id AND T.for_missions = 1
1147
+ WHERE B.deleted_at IS NULL AND F.deleted_at IS NULL AND T.for_missions = 1 AND F.invitation_at BETWEEN '#{start_at}' AND '#{end_at}'
1148
+ #{follow_up_ids_where.present? ? follow_up_ids_where : ''}
1149
+ #{school_where.present? ? school_where : ''}
1150
+ #{school_tag_id.present? ? " AND school_tag_id in (#{school_tag_id})" : ""}
1151
+ ORDER BY F.invitation_at ASC
1152
+ ) AS educode_sales_businesses"
1153
+ )
1154
+ end
1155
+
1156
+ def get_staff_money_plan(follow_up_ids_where, school_where, start_at, end_at, school_tag_id)
1157
+ EducodeSales::Business.from(
1158
+ "(SELECT B.id,B.name,B.school_id,B.department_id,B.clazz_id,B.last_follow_up_id,B.staff_id,F.plan_return_money as money, F.plan_return_date as time,
1159
+ F.created_at as created_at,T.name as school_tag_name,F.deleted_at,TM.school_tag_id as school_tag_id
1160
+ FROM educode_sales_businesses AS B
1161
+ LEFT JOIN educode_sales_follow_ups as F ON B.last_follow_up_id=F.id
1162
+ LEFT JOIN school_tag_middles AS TM ON TM.school_id=B.school_id
1163
+ LEFT JOIN school_tags as T ON TM.school_tag_id=T.id AND T.for_missions = 1
1164
+ WHERE B.deleted_at IS NULL AND F.deleted_at IS NULL AND T.for_missions = 1 AND plan_return_date BETWEEN '#{start_at}' AND '#{end_at}'
1165
+ #{follow_up_ids_where.present? ? follow_up_ids_where : ''}
1166
+ #{school_where.present? ? school_where : ''}
1167
+ #{school_tag_id.present? ? " AND school_tag_id in (#{school_tag_id})" : ""}
1168
+ ORDER BY plan_return_date ASC
1169
+ ) AS educode_sales_businesses"
1170
+ )
1171
+ end
1172
+
1173
+ def get_staff_business_plan(follow_up_ids_where, school_where, start_at, end_at, school_tag_id)
1174
+ EducodeSales::Business.from(
1175
+ "(SELECT B.id,B.name,B.school_id,B.department_id,B.clazz_id,B.last_follow_up_id,B.staff_id,F.plan_a_money as money, F.plan_a_date as time,
1176
+ F.created_at as created_at,T.name as school_tag_name,F.deleted_at,TM.school_tag_id as school_tag_id
1177
+ FROM educode_sales_businesses AS B
1178
+ LEFT JOIN educode_sales_follow_ups as F ON B.last_follow_up_id=F.id
1179
+ LEFT JOIN school_tag_middles AS TM ON TM.school_id=B.school_id
1180
+ LEFT JOIN school_tags as T ON TM.school_tag_id=T.id AND T.for_missions = 1
1181
+ WHERE B.deleted_at IS NULL AND F.deleted_at IS NULL AND T.for_missions = 1 AND plan_a_date BETWEEN '#{start_at}' AND '#{end_at}'
1182
+ #{follow_up_ids_where.present? ? follow_up_ids_where : ''}
1183
+ #{school_where.present? ? school_where : ''}
1184
+ #{school_tag_id.present? ? " AND school_tag_id in (#{school_tag_id})" : ""}
1185
+ ORDER BY plan_a_date ASC
1186
+ ) AS educode_sales_businesses"
1187
+ )
1188
+ end
1189
+
1190
+ def get_staff_bidden_finish(follow_up_ids_where, school_where, start_at, end_at, school_tag_id)
1191
+ EducodeSales::Business.from(
1192
+ "(SELECT B.id,B.name,B.school_id,B.department_id,B.clazz_id,B.last_follow_up_id,B.staff_id,F.actual_amount as money, F.bidded_date as time,
1193
+ F.created_at as created_at,T.name as school_tag_name,F.deleted_at,TM.school_tag_id as school_tag_id
1194
+ FROM educode_sales_businesses AS B
1195
+ LEFT JOIN educode_sales_follow_ups as F ON B.last_follow_up_id=F.id
1196
+ LEFT JOIN school_tag_middles AS TM ON TM.school_id=B.school_id
1197
+ LEFT JOIN school_tags as T ON TM.school_tag_id=T.id
1198
+ WHERE B.deleted_at IS NULL AND F.deleted_at IS NULL AND T.for_missions = 1 AND F.bidded_date BETWEEN '#{start_at}' AND '#{end_at}'
1199
+ #{follow_up_ids_where.present? ? follow_up_ids_where : ''}
1200
+ #{school_where.present? ? school_where : ''}
1201
+ #{school_tag_id.present? ? " AND school_tag_id in (#{school_tag_id})" : ""}
1202
+ ORDER BY bidded_date ASC
1203
+ ) AS educode_sales_businesses"
1204
+ )
1205
+ end
1206
+
1207
+ def get_staff_money_finish(follow_up_ids_where, school_where, start_at, end_at, school_tag_id)
1208
+ EducodeSales::Business.from(
1209
+ "(SELECT B.id,B.name,B.school_id,B.department_id,B.clazz_id,B.deleted_at,B.last_follow_up_id,B.staff_id,R.amount as money, R.date_at as time,
1210
+ R.created_at as created_at,T.name as school_tag_name,TM.school_tag_id as school_tag_id
1211
+ FROM educode_sales_money_plan_records as R
1212
+ LEFT JOIN educode_sales_businesses AS B ON R.business_id=B.id
1213
+ LEFT JOIN school_tag_middles AS TM ON TM.school_id=B.school_id
1214
+ LEFT JOIN school_tags as T ON TM.school_tag_id=T.id AND T.for_missions = 1
1215
+ WHERE B.deleted_at IS NULL AND T.for_missions = 1 AND R.date_at BETWEEN '#{start_at}' AND '#{end_at}'
1216
+ #{follow_up_ids_where.present? ? follow_up_ids_where : ''}
1217
+ #{school_where.present? ? school_where : ''}
1218
+ #{school_tag_id.present? ? " AND school_tag_id in (#{school_tag_id})" : ""}
1219
+ ORDER BY R.date_at ASC
1220
+ ) AS educode_sales_businesses"
1221
+ )
1222
+ end
1223
+
1224
+ def get_staff_business_finish(follow_up_ids_where, school_where, start_at, end_at, school_tag_id)
1225
+ EducodeSales::Business.from(
1226
+ "(SELECT B.id,B.name,B.school_id,B.department_id,B.clazz_id,B.last_follow_up_id,B.staff_id,F.budget_amount as money, F.bidded_date as time,
1227
+ F.created_at as created_at,T.name as school_tag_name,F.deleted_at,TM.school_tag_id as school_tag_id
1228
+ FROM educode_sales_businesses AS B
1229
+ LEFT JOIN educode_sales_follow_ups as F ON B.last_follow_up_id=F.id
1230
+ LEFT JOIN school_tag_middles AS TM ON TM.school_id=B.school_id
1231
+ LEFT JOIN school_tags as T ON TM.school_tag_id=T.id AND T.for_missions = 1
1232
+ WHERE B.deleted_at IS NULL AND F.deleted_at IS NULL AND T.for_missions = 1 AND bidded_date BETWEEN '#{start_at}' AND '#{end_at}'
1233
+ #{follow_up_ids_where.present? ? follow_up_ids_where : ''}
1234
+ #{school_where.present? ? school_where : ''}
1235
+ #{school_tag_id.present? ? " AND school_tag_id in (#{school_tag_id})" : ""}
1236
+ ORDER BY F.bidded_date ASC
1237
+ ) AS educode_sales_businesses"
1238
+ )
1239
+ end
1240
+
1241
+ def get_follow_up_id(staff_ids)
1242
+ EducodeSales::Business
1243
+ .left_joins(last_follow_up: :assign_follow_ups)
1244
+ .where("educode_sales_businesses.last_follow_up_id IS NOT NULL AND (educode_sales_businesses.staff_id in (#{staff_ids}) AND educode_sales_assign_follow_ups.id is null) OR educode_sales_assign_follow_ups.staff_id in (#{staff_ids})")
1245
+ .pluck(:last_follow_up_id)
1246
+ .compact
1247
+ .join(',')
1248
+ end
542
1249
  end
543
1250
  end