educode_sales 1.10.59 → 1.10.70

Sign up to get free protection for your applications and to get access to all the features.
Files changed (48) hide show
  1. checksums.yaml +4 -4
  2. data/app/assets/javascripts/educode_sales/application.js +1 -0
  3. data/app/assets/javascripts/educode_sales/functions.js +24 -0
  4. data/app/controllers/educode_sales/application_controller.rb +11 -3
  5. data/app/controllers/educode_sales/business_courses_controller.rb +19 -8
  6. data/app/controllers/educode_sales/follow_ups_controller.rb +4 -0
  7. data/app/controllers/educode_sales/plans_controller.rb +448 -124
  8. data/app/controllers/educode_sales/shixuns_controller.rb +20 -10
  9. data/app/controllers/educode_sales/subjects_controller.rb +10 -6
  10. data/app/controllers/educode_sales/teacher_follows_controller.rb +4 -0
  11. data/app/controllers/educode_sales/teachers_controller.rb +3 -0
  12. data/app/models/educode_sales/business.rb +5 -5
  13. data/app/models/educode_sales/sales_target.rb +10 -0
  14. data/app/models/educode_sales/sales_target_log.rb +11 -0
  15. data/app/views/educode_sales/business_courses/index.html.erb +1 -1
  16. data/app/views/educode_sales/business_courses/new.html.erb +2 -2
  17. data/app/views/educode_sales/businesses/_follows.html.erb +107 -100
  18. data/app/views/educode_sales/businesses/index.html.erb +6 -3
  19. data/app/views/educode_sales/businesses/index.json.jbuilder +2 -2
  20. data/app/views/educode_sales/businesses/show_follow.json.jbuilder +1 -1
  21. data/app/views/educode_sales/contracts/_follows.html.erb +75 -69
  22. data/app/views/educode_sales/contracts/_list.html.erb +283 -249
  23. data/app/views/educode_sales/contracts/index.json.jbuilder +1 -1
  24. data/app/views/educode_sales/follow_ups/index.json.jbuilder +1 -1
  25. data/app/views/educode_sales/plans/_year_execute.html.erb +294 -159
  26. data/app/views/educode_sales/plans/_year_target.html.erb +330 -215
  27. data/app/views/educode_sales/plans/index.html.erb +3 -4
  28. data/app/views/educode_sales/plans/staff_year_business.html.erb +96 -0
  29. data/app/views/educode_sales/plans/staff_year_business.json.jbuilder +22 -0
  30. data/app/views/educode_sales/plans/staff_year_target.json.jbuilder +35 -0
  31. data/app/views/educode_sales/plans/staff_year_target_log.html.erb +62 -0
  32. data/app/views/educode_sales/plans/staff_year_target_log.json.jbuilder +34 -0
  33. data/app/views/educode_sales/plans/years_all_target.json.jbuilder +5 -4
  34. data/app/views/educode_sales/sales_details/_index.html.erb +6 -3
  35. data/app/views/educode_sales/shixun_dectects/markdown.html.erb +40 -1
  36. data/app/views/educode_sales/shixuns/index.html.erb +106 -97
  37. data/app/views/educode_sales/shixuns/new.html.erb +2 -2
  38. data/app/views/educode_sales/staffs/index.json.jbuilder +1 -1
  39. data/app/views/educode_sales/subjects/index.html.erb +1 -1
  40. data/app/views/educode_sales/subjects/new.html.erb +2 -2
  41. data/app/views/educode_sales/teachers/_follows.html.erb +24 -17
  42. data/app/views/educode_sales/teachers/_index.html.erb +6 -0
  43. data/app/views/layouts/educode_sales/application.html.erb +6 -3
  44. data/config/environments/development.rb +16 -0
  45. data/config/routes.rb +7 -0
  46. data/db/migrate/20230729085359_create_educode_sales_sales_target_histories.rb +1 -1
  47. data/lib/educode_sales/version.rb +1 -1
  48. metadata +11 -2
@@ -394,43 +394,52 @@ module EducodeSales
394
394
  target_clazz = params[:target_clazz]
395
395
  #年份
396
396
  year = params[:year].present? ? params[:year].to_i : Date.today.year.to_i
397
- type = params[:type]
397
+ type = params[:type] || 'staff'
398
398
  #员工id
399
- staff_id = params[:staff_id]
399
+ staff_id = params[:staff_id].present? ? params[:staff_id] : nil
400
400
  #客户类型
401
401
  school_tag_id = params[:school_tag_id]
402
402
 
403
- if @current_admin.is_admin?
404
- else
405
- level = @current_admin.role.role_areas.find_by(clazz: '销售计划').level
406
- case level
407
- when '自己'
408
- staff_id = @current_admin.id
409
- when '区域'
410
- staff_id = @current_admin.id
411
- else
412
- end
403
+ staffs = get_sales_staffs_by_id(staff_id, params[:page], params[:limit])
404
+ if staffs.nil?
405
+ return render json: {success: false, msg: '无此数据或权限不足'}
413
406
  end
414
-
415
- if type.present? && type == 'all'
416
- @data = get_sales_all_plan(0, '', year, school_tag_id)
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
417
412
  @total_count = 1
418
413
  else
419
- staffs =
420
- if staff_id.present?
421
- EducodeSales::Staff.where(id: staff_id)
422
- else
423
- EducodeSales::Staff.all
424
- end
425
414
  @total_count = staffs.count
426
- staffs = staffs.page(params[:page]).per(params[:limit])
427
415
  @data = []
428
416
  staffs.each do |staff|
429
- @data += get_sales_all_plan(staff.id, staff.name, year, school_tag_id)
417
+ @data += get_sales_all_plan([staff], type, year, school_tag_id)
430
418
  end
431
419
  end
432
- if target_clazz.present?
433
- @data.delete_if { |item| !target_clazz.include?(item['target_clazz'])}
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
434
443
  end
435
444
  end
436
445
 
@@ -559,14 +568,13 @@ module EducodeSales
559
568
  def year_execute
560
569
  respond_to do |format|
561
570
  format.html do
562
- common = Common.find_by(clazz: 'staff_type', name: '销售')
563
- @staffs = Staff.joins(:user).where(job_type: common.id).map { |d| [d.user.real_name, d.id]}
571
+ @staffs = get_sales_staffs_by_id().map { |d| [d.user.real_name, d.id] }
564
572
  gon.school_tags = SchoolTag.where(for_missions: true).map { |d| {value: d.id, name: d.name } }
565
573
  end
566
574
  format.js do
567
- common = Common.find_by(clazz: 'staff_type', name: '销售')
568
- @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] }
569
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: '全部类型'})
570
578
  end
571
579
  end
572
580
  end
@@ -576,13 +584,12 @@ module EducodeSales
576
584
  format.html do
577
585
  end
578
586
  format.js do
579
- common = Common.find_by(clazz: 'staff_type', name: '销售')
580
- @staffs = Staff.joins(:user).where(job_type: common.id).map { |d| [d.user.real_name, d.id]}
587
+ @staffs = get_sales_staffs_by_id().map { |d| [d.user.real_name, d.id] }
581
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: '全部类型'})
582
590
  gon.month = Time.now.month
583
591
  end
584
592
  format.json do
585
-
586
593
  if @current_admin.is_admin?
587
594
  @data = EducodeSales::SalesTargetHistory.all
588
595
  else
@@ -692,46 +699,183 @@ module EducodeSales
692
699
  end
693
700
  end
694
701
 
695
- def update_year_target
696
- target = EducodeSales::SalesTargetHistory.find(params[:id])
697
- if target.state == '待审核'
698
- if params[:state] == '已通过'
699
- other_uses = EducodeSales::SalesTargetHistory.where(num: target.num, is_use: true).order(id: :desc)
700
- if other_uses
701
- other_uses.update(is_use: false)
702
- last_use = other_uses.last
703
- # 最近采用的月份目标复制过来
704
- 12.times do |i|
705
- if last_use.send("target_#{i+1}").present?
706
- target.send("target_#{i+1}=", last_use.send("target_#{i+1}") )
707
- end
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] }
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
708
725
  end
709
726
  end
710
- # 采用修改的月份目标
711
- 12.times do |i|
712
- if target.send("m_target_#{i+1}").present?
713
- target.send("target_#{i+1}=", target.send("m_target_#{i+1}") )
714
- 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])
715
733
  end
716
- target.state = '已通过'
717
- target.is_use = true
718
- elsif params[:state] == '已驳回'
719
- target.state = '已驳回'
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 "同类型数据已存在,无法添加"
720
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
721
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]
722
775
  target.comment = params[:comment]
723
776
  target.reviewer_id = @current_admin.id
724
777
  if target.save
725
- render json: {state: target.state, updated_at: target.updated_at.to_s(:date)}
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)}
726
781
  else
727
782
  render_failure target
728
783
  end
729
784
  else
730
785
  render json: {success: false, msg: '该状态不能编辑'}
731
786
  end
787
+ end
732
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
733
798
  end
734
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])
876
+ end
877
+ end
878
+ end
735
879
 
736
880
  private
737
881
 
@@ -749,154 +893,185 @@ module EducodeSales
749
893
  end
750
894
 
751
895
  def year_target_param
752
- params.permit(:staff_id, :year, :target_clazz, :school_tag_id, :year_finish, :year_target, :year_diff, :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)
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)
753
897
  end
754
898
 
755
899
  def make_target_data(sales_all_plan, data, column, type)
756
900
  data.each do |item|
757
- _column = "#{column}_#{item.m.gsub("0", '')}"
901
+ _column = "#{column}_#{item.m.to_i.to_s}"
758
902
  if sales_all_plan[type][item.school_tag_id].present?
759
903
  if sales_all_plan[type][item.school_tag_id][_column].present?
760
- sales_all_plan[type][item.school_tag_id][_column] += item.amount
904
+ sales_all_plan[type][item.school_tag_id][_column] += item.amount.to_i
761
905
  else
762
- sales_all_plan[type][item.school_tag_id][_column] = item.amount
906
+ sales_all_plan[type][item.school_tag_id][_column] = item.amount.to_i
763
907
  end
764
908
  else
765
909
  sales_all_plan[type][item.school_tag_id] = {"school_tag_id" => item.school_tag_id, "school_tag_name" => item.school_tag_name}
766
- sales_all_plan[type][item.school_tag_id][_column] = item.amount
910
+ sales_all_plan[type][item.school_tag_id][_column] = item.amount.to_i
767
911
  end
768
912
  sales_all_plan[type][item.school_tag_id]['type'] = type
769
913
  end
770
914
  sales_all_plan
771
915
  end
772
916
 
773
- def get_sales_all_plan(staff_id, staff_name, year, school_tag_id)
774
917
 
775
- follow_up_ids = ''
776
- if staff_id.present?
777
- follow_up_ids = EducodeSales::AssignFollowUp.where(staff_id: staff_id).pluck(:follow_up_id).compact.join(',')
918
+ def get_sales_all_plan(staffs, type, year, school_tag_id)
919
+ if staffs.empty?
920
+ return []
778
921
  end
779
- # 中标&商机&回款 - 目标按月
780
- sales_target_histories = EducodeSales::SalesTargetHistory.from("
781
- (SELECT H.id,clazz,school_tag_id,T.name as school_tag_name,sum(m_target_1) as target_1, sum(m_target_2) target_2, sum(m_target_3) target_3, sum(m_target_4) target_4, sum(m_target_5) target_5, sum(m_target_6) target_6, sum(m_target_7) target_7,sum(m_target_8) target_8,sum(m_target_9) target_9, sum(m_target_10) target_10, sum(m_target_11) target_11, sum(m_target_12) target_12
782
- FROM educode_sales_sales_target_histories as H
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
783
930
  LEFT JOIN school_tags AS T ON H.school_tag_id = T.id
784
- WHERE is_use = 1 AND year = #{year} AND T.for_missions = 1
785
- #{staff_id.present? ? " AND staff_id = #{staff_id}" : ""}
786
- #{school_tag_id.present? ? " AND school_tag_id in (#{school_tag_id})" : ""}
787
- GROUP BY clazz,school_tag_id) as educode_sales_sales_target_histories
788
- ").order('clazz asc, school_tag_id asc,id desc')
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')
789
937
 
790
- start_at = Date.new(year, 1, 1)
791
- end_at = Date.new((year + 1), 1, 1)
792
- follow_up_ids_where =
793
- if staff_id.present?
794
- follow_up_ids_where = follow_up_ids.present? ? " AND B.last_follow_up_id in (#{follow_up_ids})" : " AND B.id in(0)"
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
795
948
  end
796
949
 
797
- # 中标 - 完成
798
- bidder_finish = EducodeSales::FollowUp.select('amount,m,school_tag_id,school_tag_name').from(
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(
799
982
  "(SELECT sum(F.actual_amount) as amount, date_format(F.bidded_date, '%m') as m,T.name as school_tag_name,
800
983
  TM.school_tag_id as school_tag_id,F.deleted_at
801
984
  FROM educode_sales_businesses AS B
802
985
  LEFT JOIN educode_sales_follow_ups as F ON B.last_follow_up_id=F.id
803
986
  LEFT JOIN school_tag_middles AS TM ON TM.school_id=B.school_id
804
987
  LEFT JOIN school_tags as T ON TM.school_tag_id=T.id
805
- WHERE F.deleted_at IS NULL AND T.for_missions = 1 AND F.bidded_date BETWEEN '#{start_at}' AND '#{end_at}'
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}'
806
989
  #{follow_up_ids_where.present? ? follow_up_ids_where : ''}
807
- #{school_tag_id.present? ? " AND school_tag_id in (#{school_tag_id})" : ""}
808
- GROUP BY school_tag_id,m
990
+ #{school_where.present? ? school_where : ''}
991
+ #{and_where_school_tag_id}
992
+ GROUP BY #{group_by}
809
993
  ) AS educode_sales_follow_ups"
810
994
  ).order('school_tag_id ASC, m asc')
811
995
 
812
- # 中标 - 计划
813
- bidder_plan = EducodeSales::FollowUp.select('amount,m,school_tag_id,school_tag_name').from(
814
- "(SELECT sum(plan_return_money) as amount, date_format(plan_return_date, '%m') as m,T.name as school_tag_name,
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,
815
999
  TM.school_tag_id as school_tag_id,F.deleted_at
816
1000
  FROM educode_sales_businesses AS B
817
1001
  LEFT JOIN educode_sales_follow_ups as F ON B.last_follow_up_id=F.id
818
1002
  LEFT JOIN school_tag_middles AS TM ON TM.school_id=B.school_id
819
1003
  LEFT JOIN school_tags as T ON TM.school_tag_id=T.id AND T.for_missions = 1
820
- WHERE F.deleted_at IS NULL AND T.for_missions = 1 AND F.plan_return_date BETWEEN '#{start_at}' AND '#{end_at}'
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}'
821
1005
  #{follow_up_ids_where.present? ? follow_up_ids_where : ''}
822
- #{school_tag_id.present? ? " AND school_tag_id in (#{school_tag_id})" : ""}
823
- GROUP BY school_tag_id,m
1006
+ #{school_where.present? ? school_where : ''}
1007
+ #{and_where_school_tag_id}
1008
+ GROUP BY #{group_by}
824
1009
  ORDER BY school_tag_id ASC
825
1010
  ) AS educode_sales_follow_ups"
826
1011
  ).order('school_tag_id ASC, m asc')
827
1012
  # 回款 - 完成
828
- money_finish = EducodeSales::MoneyPlanRecord.select('amount,m,school_tag_id,school_tag_name').from(
1013
+ money_finish = EducodeSales::MoneyPlanRecord.select(select_columns).from(
829
1014
  "(SELECT sum(amount) as amount, date_format(R.date_at, '%m') as m,T.name as school_tag_name,
830
1015
  TM.school_tag_id as school_tag_id
831
1016
  FROM educode_sales_money_plan_records as R
832
1017
  LEFT JOIN educode_sales_businesses AS B ON R.business_id=B.id
833
1018
  LEFT JOIN school_tag_middles AS TM ON TM.school_id=B.school_id
834
1019
  LEFT JOIN school_tags as T ON TM.school_tag_id=T.id AND T.for_missions = 1
835
- WHERE T.for_missions = 1 AND R.date_at BETWEEN '#{start_at}' AND '#{end_at}'
1020
+ WHERE B.deleted_at IS NULL AND T.for_missions = 1 AND R.date_at BETWEEN '#{start_at}' AND '#{end_at}'
836
1021
  #{follow_up_ids_where.present? ? follow_up_ids_where : ''}
837
- #{school_tag_id.present? ? " AND school_tag_id in (#{school_tag_id})" : ""}
838
- GROUP BY school_tag_id,m
839
- ORDER BY school_tag_id ASC
1022
+ #{school_where.present? ? school_where : ''}
1023
+ #{and_where_school_tag_id}
1024
+ GROUP BY #{group_by}
840
1025
  ) AS educode_sales_money_plan_records"
841
1026
  ).order('school_tag_id ASC, m asc')
842
1027
  # 回款 - 计划 .group_by { |d| d.m }
843
- money_plan = EducodeSales::FollowUp.select('amount,m,school_tag_id,school_tag_name').from(
1028
+ money_plan = EducodeSales::FollowUp.select(select_columns).from(
844
1029
  "(SELECT sum(plan_return_money) as amount, date_format(plan_return_date, '%m') as m,T.name as school_tag_name,
845
1030
  F.deleted_at,TM.school_tag_id as school_tag_id
846
1031
  FROM educode_sales_businesses AS B
847
1032
  LEFT JOIN educode_sales_follow_ups as F ON B.last_follow_up_id=F.id
848
1033
  LEFT JOIN school_tag_middles AS TM ON TM.school_id=B.school_id
849
1034
  LEFT JOIN school_tags as T ON TM.school_tag_id=T.id AND T.for_missions = 1
850
- WHERE F.deleted_at IS NULL AND T.for_missions = 1 AND plan_return_date BETWEEN '#{start_at}' AND '#{end_at}'
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}'
851
1036
  #{follow_up_ids_where.present? ? follow_up_ids_where : ''}
852
- #{school_tag_id.present? ? " AND school_tag_id in (#{school_tag_id})" : ""}
853
- GROUP BY school_tag_id,m
1037
+ #{school_where.present? ? school_where : ''}
1038
+ #{and_where_school_tag_id}
1039
+ GROUP BY #{group_by}
854
1040
  ORDER BY school_tag_id ASC
855
1041
  ) AS educode_sales_follow_ups"
856
1042
  ).order('school_tag_id ASC, m asc')
857
1043
  # 商机-完成 .where(:bidded_date => start_datetime..end_datetime)
858
- business_finish = EducodeSales::FollowUp.select('amount,m,school_tag_id,school_tag_name').from(
1044
+ business_finish = EducodeSales::FollowUp.select(select_columns).from(
859
1045
  "(SELECT sum(budget_amount) as amount, date_format(bidded_date, '%m') as m,T.name as school_tag_name,
860
1046
  F.deleted_at,TM.school_tag_id as school_tag_id
861
1047
  FROM educode_sales_businesses AS B
862
1048
  LEFT JOIN educode_sales_follow_ups as F ON B.last_follow_up_id=F.id
863
1049
  LEFT JOIN school_tag_middles AS TM ON TM.school_id=B.school_id
864
1050
  LEFT JOIN school_tags as T ON TM.school_tag_id=T.id AND T.for_missions = 1
865
- WHERE F.deleted_at IS NULL AND T.for_missions = 1 AND bidded_date BETWEEN '#{start_at}' AND '#{end_at}'
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}'
866
1052
  #{follow_up_ids_where.present? ? follow_up_ids_where : ''}
867
- #{school_tag_id.present? ? " AND school_tag_id in (#{school_tag_id})" : ""}
868
- GROUP BY school_tag_id,m
1053
+ #{school_where.present? ? school_where : ''}
1054
+ #{and_where_school_tag_id}
1055
+ GROUP BY #{group_by}
869
1056
  ) AS educode_sales_follow_ups
870
1057
  "
871
1058
  ).order('school_tag_id ASC, m asc')
872
1059
  # 商机-计划
873
- business_plan = EducodeSales::FollowUp.select('amount,m,school_tag_id,school_tag_name').from(
1060
+ business_plan = EducodeSales::FollowUp.select(select_columns).from(
874
1061
  "(SELECT sum(plan_a_money) as amount, date_format(plan_a_date, '%m') as m,T.name as school_tag_name,
875
1062
  F.deleted_at,TM.school_tag_id as school_tag_id
876
1063
  FROM educode_sales_businesses AS B
877
1064
  LEFT JOIN educode_sales_follow_ups as F ON B.last_follow_up_id=F.id
878
1065
  LEFT JOIN school_tag_middles AS TM ON TM.school_id=B.school_id
879
1066
  LEFT JOIN school_tags as T ON TM.school_tag_id=T.id AND T.for_missions = 1
880
- WHERE F.deleted_at IS NULL AND T.for_missions = 1 AND plan_a_date BETWEEN '#{start_at}' AND '#{end_at}'
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}'
881
1068
  #{follow_up_ids_where.present? ? follow_up_ids_where : ''}
882
- #{school_tag_id.present? ? " AND school_tag_id in (#{school_tag_id})" : ""}
883
- GROUP BY school_tag_id,m
1069
+ #{school_where.present? ? school_where : ''}
1070
+ #{and_where_school_tag_id}
1071
+ GROUP BY #{group_by}
884
1072
  ORDER BY school_tag_id ASC
885
1073
  ) AS educode_sales_follow_ups"
886
1074
  ).order('school_tag_id ASC, m asc')
887
-
888
- sales_all_plan = {:bidder => {}, :business => {}, :money => {}}
889
- sales_target_histories.each do |history|
890
- case history.clazz
891
- when '中标'
892
- sales_all_plan[:bidder][history.school_tag_id] = history.attributes.except('id')
893
- when '商机'
894
- sales_all_plan[:business][history.school_tag_id] = history.attributes.except('id')
895
- when '回款'
896
- sales_all_plan[:money][history.school_tag_id] = history.attributes.except('id')
897
- else
898
- end
899
- end
900
1075
  #中标完成
901
1076
  sales_all_plan = make_target_data(sales_all_plan, bidder_finish, 'finish', :bidder)
902
1077
  #中标计划
@@ -913,14 +1088,163 @@ module EducodeSales
913
1088
  data = sales_all_plan[:bidder].values + sales_all_plan[:business].values + sales_all_plan[:money].values
914
1089
 
915
1090
  if data.empty?
916
- data = ['id' => staff_id, 'name' => staff_name, 'target_clazz' => '商机']
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
917
1101
  end
918
- data.each do |item|
919
- item['id'] = staff_id
920
- item['name'] = staff_name
921
- item['target_clazz'] = item['clazz'].present? ? item['clazz'] : SalesTargetHistory::TARGET_CLAZZ_NAME[item['type']]
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])
922
1125
  end
1126
+ staffs
923
1127
  end
924
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
925
1249
  end
926
1250
  end