educode_sales 1.10.58 → 1.10.59

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 666b9ec9b763cfce87df4b2b6a2304625100ba52d94df0cd806fe36266062b3c
4
- data.tar.gz: 494c7222e60d7114c95cd6f213fd11bb5070e2b6f40d45bfdaa3fa880c7e4a0f
3
+ metadata.gz: 0e79e3bb70dfa210936c30a10cbf201a4fda9065f8d6243f5404cb18e39f4a6c
4
+ data.tar.gz: f57a3be46b0e597d85025e1bf97cfeb4851cc2a2f76fe6937692063cbb5a0fde
5
5
  SHA512:
6
- metadata.gz: 9cc7914ec32c5e3cda9fa353bf35ddba314450b75f7401d1063469ab4f10b117437a4a93392783ddd7049e21f6012b1faba947be5b05874fd4b235c327c553c4
7
- data.tar.gz: c5276a5f2d6ea22604cea8d6f81acb320f8e8ec7975138741e8e5ab6326f7f368f210a9b983fa0ab48688709de0053ad923a1043eab701b3ab19dac08910c1fc
6
+ metadata.gz: c166871e6c22988d3811c17cd50c6cbea1aee2c4b76ac1b0c908a8c8accabe29dabb75154415a17ee9342291a501e9479a4016611676bef70a8778cba2c8a2d3
7
+ data.tar.gz: a856cd0c13fae3647dc01acfb2ddcaf95e7c33a27f9075eaa951e723756491e7928b5c541a2988b683ffd7506a46708a81072bfd44204323cef0c6b1bb42a257
@@ -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,51 @@ 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]
398
+ #员工id
399
+ staff_id = params[:staff_id]
400
+ #客户类型
401
+ school_tag_id = params[:school_tag_id]
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
413
+ end
414
+
415
+ if type.present? && type == 'all'
416
+ @data = get_sales_all_plan(0, '', year, school_tag_id)
417
+ @total_count = 1
418
+ else
419
+ staffs =
420
+ if staff_id.present?
421
+ EducodeSales::Staff.where(id: staff_id)
422
+ else
423
+ EducodeSales::Staff.all
424
+ end
425
+ @total_count = staffs.count
426
+ staffs = staffs.page(params[:page]).per(params[:limit])
427
+ @data = []
428
+ staffs.each do |staff|
429
+ @data += get_sales_all_plan(staff.id, staff.name, year, school_tag_id)
430
+ end
431
+ end
432
+ if target_clazz.present?
433
+ @data.delete_if { |item| !target_clazz.include?(item['target_clazz'])}
434
+ end
435
+ end
436
+
392
437
  def my_years_plan
393
438
  @sale_plans = SalePlan.where(staff_id: @current_admin.id)
394
439
  end
@@ -511,19 +556,183 @@ module EducodeSales
511
556
  end
512
557
 
513
558
 
514
- def target_track
559
+ def year_execute
560
+ respond_to do |format|
561
+ 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]}
564
+ gon.school_tags = SchoolTag.where(for_missions: true).map { |d| {value: d.id, name: d.name } }
565
+ end
566
+ 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]}
569
+ gon.school_tags = SchoolTag.where(for_missions: true).map { |d| {value: d.id, name: d.name } }
570
+ end
571
+ end
572
+ end
573
+
574
+ def year_target
515
575
  respond_to do |format|
516
576
  format.html do
517
-
518
577
  end
519
578
  format.js do
520
579
  common = Common.find_by(clazz: 'staff_type', name: '销售')
521
580
  @staffs = Staff.joins(:user).where(job_type: common.id).map { |d| [d.user.real_name, d.id]}
522
581
  gon.school_tags = SchoolTag.where(for_missions: true).map { |d| {value: d.id, name: d.name } }
582
+ gon.month = Time.now.month
583
+ end
584
+ format.json do
585
+
586
+ if @current_admin.is_admin?
587
+ @data = EducodeSales::SalesTargetHistory.all
588
+ else
589
+ level = @current_admin.role.role_areas.find_by(clazz: '销售计划').level
590
+ case level
591
+ when '自己'
592
+ @data = EducodeSales::SalesTargetHistory.where(staff_id: @current_admin.id)
593
+ when '区域'
594
+ @data = EducodeSales::SalesTargetHistory.all
595
+ staff_ids = Staff.joins(user: [user_extension: [department: :school]]).where("schools.province in (?)", @current_admin.areas.pluck(:name)).pluck(:id)
596
+ @data = @data.where(staff_id: staff_ids)
597
+ else
598
+ @data = EducodeSales::SalesTargetHistory.all
599
+ end
600
+ end
601
+ @data = @data.order("staff_id asc,num desc, is_use desc,field(state, 1,0,2), id desc")
602
+ if params[:year].present?
603
+ @data = @data.where(year: params[:year])
604
+ end
605
+ if params[:school_tag_id].present?
606
+ @data = @data.where(school_tag_id: params[:school_tag_id])
607
+ end
608
+ if params[:staff_id].present?
609
+ @data = @data.where(staff_id: params[:staff_id])
610
+ end
611
+ if params[:year_target].present?
612
+ @data = @data.where(year_target: params[:year_target])
613
+ end
614
+ if params[:target_clazz].present?
615
+ @data = @data.where(target_clazz: params[:target_clazz])
616
+ end
617
+ @data = @data.page(params[:page]).per(params[:limit])
523
618
  end
524
619
  end
525
620
  end
526
621
 
622
+ def cal_finish_amount
623
+ start_at = params[:year] + "-01-01"
624
+ end_at = params[:year] + "-12-31"
625
+ amount = 0
626
+ case params[:target_clazz]
627
+ when "回款"
628
+ # 对应销售经理下回款时间的回款金额
629
+ amount = EducodeSales::MoneyPlanRecord.joins("
630
+ 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
631
+ LEFT JOIN educode_sales_assign_follow_ups ON educode_sales_assign_follow_ups.follow_up_id = last_follow_up.id").
632
+ joins(business: [department: [school: :school_tags]]).where("school_tags.id = ?", params[:school_tag_id]).
633
+ 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]).
634
+ where("date_at >= ? AND date_at <= ?", start_at, end_at).sum("amount")
635
+
636
+ when '商机'
637
+ a_clazz = EducodeSales::Common.find_by(extras: 'a_class')
638
+ start_at = "#{params[:year]}-01-01 00:00:01"
639
+ end_at = "#{params[:year]}-12-31 23:59:59"
640
+ staff_id = params[:staff_id]
641
+ # 对应销售经理下A类商机预算金额
642
+ amount = EducodeSales::Business.from("(
643
+ SELECT distinct educode_sales_businesses.id, last_follow_up.budget_amount, educode_sales_businesses.deleted_at, educode_sales_businesses.department_id
644
+ FROM educode_sales_businesses
645
+ 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}
646
+ AND educode_sales_follow_ups.created_at > '#{start_at}' AND educode_sales_follow_ups.created_at < '#{end_at}'
647
+ 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}
648
+ LEFT JOIN educode_sales_assign_follow_ups ON educode_sales_assign_follow_ups.follow_up_id = last_follow_up.id
649
+ 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}')
650
+ ) AS educode_sales_businesses
651
+ ").joins(department: [school: :school_tags]).where("school_tags.id = ?", params[:school_tag_id]).sum(:budget_amount)
652
+ when '中标'
653
+ # 对应销售经理下合同列表的中标时间下合同金额
654
+ contract_ids = EducodeSales::Common.where(clazz: '商机阶段', name: ['已中标', '已签单', '已验收', '回款中', '服务中', '已结束']).pluck(:id)
655
+ businesses = EducodeSales::Business.joins("
656
+ JOIN educode_sales_follow_ups ON educode_sales_businesses.last_follow_up_id = educode_sales_follow_ups.id
657
+ ").joins("LEFT JOIN educode_sales_assign_follow_ups ON educode_sales_assign_follow_ups.follow_up_id = educode_sales_businesses.last_follow_up_id").
658
+ joins(department: [school: :school_tags]).where("school_tags.id = ?", params[:school_tag_id]).
659
+ 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]).
660
+ where("educode_sales_follow_ups.stage_id in (?)", contract_ids).
661
+ where("educode_sales_follow_ups.bidded_date >= ? AND educode_sales_follow_ups.bidded_date <= ?", start_at, end_at).sum(:actual_amount)
662
+ end
663
+ render json: {amount: amount}
664
+ end
665
+
666
+ def add_year_target
667
+ 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])
668
+ # 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
669
+ # 不能重复添加
670
+ #if EducodeSales::SalesTargetHistory.exists?(staff_id: params[:staff_id], year: params[:year], school_tag_id: params[:school_tag_id], target_clazz: params[:target_clazz])
671
+ # return render_failure "不能重复添加"
672
+ #end
673
+ target = EducodeSales::SalesTargetHistory.new(year_target_param)
674
+ if history
675
+ 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
676
+ use_old_attributes.each do |attr|
677
+ target[attr] = history[attr]
678
+ end
679
+ end
680
+ if history
681
+ target.num = history.num
682
+ else
683
+ target.num = EducodeSales::SalesTargetHistory.maximum(:id).to_i + 1
684
+ end
685
+
686
+ target.creator_id = @current_admin.id
687
+ target.state = '待审核'
688
+ if target.save
689
+ render json: { id: target.id, state: target.state, updated_at: target.updated_at.to_s(:date) }
690
+ else
691
+ render_failure target
692
+ end
693
+ end
694
+
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
708
+ end
709
+ 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
715
+ end
716
+ target.state = '已通过'
717
+ target.is_use = true
718
+ elsif params[:state] == '已驳回'
719
+ target.state = '已驳回'
720
+ end
721
+
722
+ target.comment = params[:comment]
723
+ target.reviewer_id = @current_admin.id
724
+ if target.save
725
+ render json: {state: target.state, updated_at: target.updated_at.to_s(:date)}
726
+ else
727
+ render_failure target
728
+ end
729
+ else
730
+ render json: {success: false, msg: '该状态不能编辑'}
731
+ end
732
+
733
+ end
734
+
735
+
527
736
  private
528
737
 
529
738
  def plan_params
@@ -536,7 +745,181 @@ module EducodeSales
536
745
 
537
746
  def update_business_info_extra_params
538
747
  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)
748
+ :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)
749
+ end
750
+
751
+ 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)
753
+ end
754
+
755
+ def make_target_data(sales_all_plan, data, column, type)
756
+ data.each do |item|
757
+ _column = "#{column}_#{item.m.gsub("0", '')}"
758
+ if sales_all_plan[type][item.school_tag_id].present?
759
+ if sales_all_plan[type][item.school_tag_id][_column].present?
760
+ sales_all_plan[type][item.school_tag_id][_column] += item.amount
761
+ else
762
+ sales_all_plan[type][item.school_tag_id][_column] = item.amount
763
+ end
764
+ else
765
+ 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
767
+ end
768
+ sales_all_plan[type][item.school_tag_id]['type'] = type
769
+ end
770
+ sales_all_plan
771
+ end
772
+
773
+ def get_sales_all_plan(staff_id, staff_name, year, school_tag_id)
774
+
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(',')
778
+ 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
783
+ 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')
789
+
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)"
795
+ end
796
+
797
+ # 中标 - 完成
798
+ bidder_finish = EducodeSales::FollowUp.select('amount,m,school_tag_id,school_tag_name').from(
799
+ "(SELECT sum(F.actual_amount) as amount, date_format(F.bidded_date, '%m') as m,T.name as school_tag_name,
800
+ TM.school_tag_id as school_tag_id,F.deleted_at
801
+ FROM educode_sales_businesses AS B
802
+ LEFT JOIN educode_sales_follow_ups as F ON B.last_follow_up_id=F.id
803
+ LEFT JOIN school_tag_middles AS TM ON TM.school_id=B.school_id
804
+ 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}'
806
+ #{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
809
+ ) AS educode_sales_follow_ups"
810
+ ).order('school_tag_id ASC, m asc')
811
+
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,
815
+ TM.school_tag_id as school_tag_id,F.deleted_at
816
+ FROM educode_sales_businesses AS B
817
+ LEFT JOIN educode_sales_follow_ups as F ON B.last_follow_up_id=F.id
818
+ LEFT JOIN school_tag_middles AS TM ON TM.school_id=B.school_id
819
+ 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}'
821
+ #{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
824
+ ORDER BY school_tag_id ASC
825
+ ) AS educode_sales_follow_ups"
826
+ ).order('school_tag_id ASC, m asc')
827
+ # 回款 - 完成
828
+ money_finish = EducodeSales::MoneyPlanRecord.select('amount,m,school_tag_id,school_tag_name').from(
829
+ "(SELECT sum(amount) as amount, date_format(R.date_at, '%m') as m,T.name as school_tag_name,
830
+ TM.school_tag_id as school_tag_id
831
+ FROM educode_sales_money_plan_records as R
832
+ LEFT JOIN educode_sales_businesses AS B ON R.business_id=B.id
833
+ LEFT JOIN school_tag_middles AS TM ON TM.school_id=B.school_id
834
+ 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}'
836
+ #{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
840
+ ) AS educode_sales_money_plan_records"
841
+ ).order('school_tag_id ASC, m asc')
842
+ # 回款 - 计划 .group_by { |d| d.m }
843
+ money_plan = EducodeSales::FollowUp.select('amount,m,school_tag_id,school_tag_name').from(
844
+ "(SELECT sum(plan_return_money) as amount, date_format(plan_return_date, '%m') as m,T.name as school_tag_name,
845
+ F.deleted_at,TM.school_tag_id as school_tag_id
846
+ FROM educode_sales_businesses AS B
847
+ LEFT JOIN educode_sales_follow_ups as F ON B.last_follow_up_id=F.id
848
+ LEFT JOIN school_tag_middles AS TM ON TM.school_id=B.school_id
849
+ 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}'
851
+ #{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
854
+ ORDER BY school_tag_id ASC
855
+ ) AS educode_sales_follow_ups"
856
+ ).order('school_tag_id ASC, m asc')
857
+ # 商机-完成 .where(:bidded_date => start_datetime..end_datetime)
858
+ business_finish = EducodeSales::FollowUp.select('amount,m,school_tag_id,school_tag_name').from(
859
+ "(SELECT sum(budget_amount) as amount, date_format(bidded_date, '%m') as m,T.name as school_tag_name,
860
+ F.deleted_at,TM.school_tag_id as school_tag_id
861
+ FROM educode_sales_businesses AS B
862
+ LEFT JOIN educode_sales_follow_ups as F ON B.last_follow_up_id=F.id
863
+ LEFT JOIN school_tag_middles AS TM ON TM.school_id=B.school_id
864
+ 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}'
866
+ #{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
869
+ ) AS educode_sales_follow_ups
870
+ "
871
+ ).order('school_tag_id ASC, m asc')
872
+ # 商机-计划
873
+ business_plan = EducodeSales::FollowUp.select('amount,m,school_tag_id,school_tag_name').from(
874
+ "(SELECT sum(plan_a_money) as amount, date_format(plan_a_date, '%m') as m,T.name as school_tag_name,
875
+ F.deleted_at,TM.school_tag_id as school_tag_id
876
+ FROM educode_sales_businesses AS B
877
+ LEFT JOIN educode_sales_follow_ups as F ON B.last_follow_up_id=F.id
878
+ LEFT JOIN school_tag_middles AS TM ON TM.school_id=B.school_id
879
+ 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}'
881
+ #{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
884
+ ORDER BY school_tag_id ASC
885
+ ) AS educode_sales_follow_ups"
886
+ ).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
+ #中标完成
901
+ sales_all_plan = make_target_data(sales_all_plan, bidder_finish, 'finish', :bidder)
902
+ #中标计划
903
+ sales_all_plan = make_target_data(sales_all_plan, bidder_plan, 'plan', :bidder)
904
+ #回款完成
905
+ sales_all_plan = make_target_data(sales_all_plan, money_finish, 'finish', :money)
906
+ #回款计划
907
+ sales_all_plan = make_target_data(sales_all_plan, money_plan, 'plan', :money)
908
+ #商机完成
909
+ sales_all_plan = make_target_data(sales_all_plan, business_finish, 'finish', :business)
910
+ #商机计划
911
+ sales_all_plan = make_target_data(sales_all_plan, business_plan, 'plan', :business)
912
+
913
+ data = sales_all_plan[:bidder].values + sales_all_plan[:business].values + sales_all_plan[:money].values
914
+
915
+ if data.empty?
916
+ data = ['id' => staff_id, 'name' => staff_name, 'target_clazz' => '商机']
917
+ 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']]
922
+ end
540
923
  end
541
924
 
542
925
  end
@@ -1,5 +1,12 @@
1
1
  module EducodeSales
2
2
  class Permission < ApplicationRecord
3
+ # EducodeSales::Permission.create(name: '查看数据', subject: 'SalePlan', action: 'new_index', clazz: 'sale_plan')
4
+ # EducodeSales::Permission.create(name: '分类查看', subject: 'SalePlan', action: 'new_type_index', clazz: 'sale_plan')
5
+ # EducodeSales::Permission.create(name: '添加目标', subject: 'SalePlan', action: 'new_create', clazz: 'sale_plan')
6
+ # EducodeSales::Permission.create(name: '编辑目标', subject: 'SalePlan', action: 'new_update', clazz: 'sale_plan')
7
+ # EducodeSales::Permission.create(name: '审核目标', subject: 'SalePlan', action: 'new_audit', clazz: 'sale_plan')
8
+ # EducodeSales::Permission.create(name: '查看记录', subject: 'SalePlan', action: 'new_record_index', clazz: 'sale_plan')
9
+ default_scope { where(hidden: false) }
3
10
  enum clazz: {
4
11
  '销售态势': 'market',
5
12
  '销售分工': 'sale_job',
@@ -0,0 +1,11 @@
1
+ module EducodeSales
2
+ class SalesTargetHistory < ApplicationRecord
3
+ belongs_to :staff
4
+ belongs_to :reviewer, class_name: 'Staff', optional: true
5
+ belongs_to :school_tag
6
+
7
+ enum clazz: { '中标': 18, '回款': 2, '商机': 7 }
8
+ TARGET_CLAZZ_NAME = { 'bidder': '中标', 'money': '回款', 'business': '商机' }
9
+ enum state: %w[待审核 已通过 已驳回]
10
+ end
11
+ end