educode_sales 1.0.5 → 1.0.6

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: b94517f09287290104a7f5b99842db680f96336a78f7261a7478c4c9d8ca66af
4
- data.tar.gz: 9dd809965284e5d22f39ebf18110ad1c54514391d1e2d75fef04c2a686f29ef9
3
+ metadata.gz: 6b8eb210d7095cfc2bfc9eded67a0ccd365c69ea32a1d40ff9708611a4d6e914
4
+ data.tar.gz: 4aef89a38ff10ea255e252ae0a10ce02918b4c23797445d87c0f02d09acc7147
5
5
  SHA512:
6
- metadata.gz: 662c0d5a18f51954d831ce9d935b603d595a9b255325a44f56a2edeb6f7e018a10ee779e3b994a8a3903782a0d9367bc82d16b437eae21e6fa2736721ab419cb
7
- data.tar.gz: 573ff431ba910018a90d61a568a4f1cc25ec4ff1667a8222918c560bcdf673e0f9521256f773959bdada669c4efa339d9827cf899ccf5dcef96849ed22ae4471
6
+ metadata.gz: d941248d1f719724eefc1ece705b18054b55b6408b358c90c30d698baf479894f8dd44a0c21d13add7a92dbb1cd7257438f727379e679db01b3564d993956c46
7
+ data.tar.gz: 618d22270adcf2a882eae5cbdaf2770bc1f747a65dde8201336fcbe40a66be8e023127a865b9e631fcfb7305ee2ebc78ad47a11a2d5985280c269ca46297d891
@@ -9,7 +9,8 @@ module EducodeSales
9
9
  format.html do
10
10
  end
11
11
  format.json do
12
- @money_plans = MoneyPlan.joins(:business).left_joins(:money_plan_claims).group("educode_sales_money_plans.id").select("educode_sales_money_plans.*, COUNT(educode_sales_money_plan_claims.id) AS claim_num")
12
+ last_follow_up_ids = Business.all.pluck(:last_follow_up_id)
13
+ @money_plans = MoneyPlan.where(follow_up_id: last_follow_up_ids).joins(:business).left_joins(:money_plan_claims).group("educode_sales_money_plans.id").select("educode_sales_money_plans.*, COUNT(educode_sales_money_plan_claims.id) AS claim_num")
13
14
  if @current_admin.is_admin?
14
15
  else
15
16
  level = @current_admin.role.role_areas.find_by(clazz: '回款管理').level
@@ -4,6 +4,7 @@ module EducodeSales
4
4
  class SaleTrendsController < ApplicationController
5
5
  # authorize_resource class: false
6
6
  include SaleTrendsHelper
7
+ include SaleTrendsCountHelper
7
8
  def trends
8
9
  # authorize! :trends, EducodeSales::SaleTrend
9
10
  @year = params[:year] ? params[:year] : Time.now.year
@@ -389,60 +390,15 @@ module EducodeSales
389
390
  def goal_forecast
390
391
  respond_to do |format|
391
392
  format.html do
392
-
393
393
  end
394
394
  format.js do
395
- property = params[:property] || nil
396
- staff_id = params[:staff_id] || nil
397
- @forecast_count_range = params[:forecast_count_range] || "month"
398
- forecast_default_dates = ("#{Time.now.year}-01-01".to_date.."#{Time.now.year}-#{Time.now.month}-01".to_date).map { |d| d.strftime("%Y-%m") }.uniq
399
- sale_names = %w[计划投标额 计划投标额累计 实际中标额 实际中标额累计]
400
- case @forecast_count_range
401
- when "week"
402
- # 年初第一天周数是0会导致周数重复计算
403
- if params[:forecast_date_day].present?
404
- date = params[:forecast_date_day].split(" - ")
405
- dates = (date[0].to_date..date[1].to_date).map { |d| d.strftime("%Y-%W") }.uniq.select { |d| d.split("-")[1] != '00' }
406
- @forecast_count_data = goal_forecast_week(dates, sale_names, staff_id, property)
407
- else
408
- forecast_default_dates = ("#{Time.now.year}-01-01".to_date.."#{Time.now.year}-#{Time.now.month}-01".to_date).map { |d| d.strftime("%Y-%W") }.uniq.select { |d| d.split("-")[1] != '00' }
409
- @forecast_count_data = goal_forecast_week(forecast_default_dates, sale_names, staff_id, property)
410
- end
411
- when "quarter"
412
- # 按年
413
- if params[:forecast_date_year].present?
414
- date = params[:forecast_date_year].split(" - ")
415
- dates = (date[0]..date[1]).to_a
416
- dates = dates.map { |d| [d.to_s + "-" + "1", d.to_s + "-" + "2", d.to_s + "-" + "3", d.to_s + "-" + "4"] }.flatten
417
- @forecast_count_data = goal_forecast_quarter(dates, sale_names, staff_id, property)
418
- else
419
- forecast_default_dates = ("#{Time.now.year}-01-01".to_date.."#{Time.now.year}-#{Time.now.month}-01".to_date).map { |d| d.strftime("%Y") }.uniq
420
- forecast_default_dates = forecast_default_dates.map { |d| [d.to_s + "-" + "1", d.to_s + "-" + "2", d.to_s + "-" + "3", d.to_s + "-" + "4"] }.flatten
421
- @forecast_count_data = goal_forecast_quarter(forecast_default_dates, sale_names, staff_id, property)
422
- end
423
- when "month" # 按月
424
- if params[:forecast_date_month].present?
425
- date = params[:forecast_date_month].split(" - ")
426
- date[0] = (date[0] + "-01").to_date.to_s
427
- date[1] = (date[1] + "-01").to_date.end_of_month.to_s
428
- dates = ((date[0] + "-01").to_date..(date[1] + "-01").to_date).map { |d| d.strftime("%Y-%m") }.uniq
429
- @forecast_count_data = goal_forecast_month(dates, sale_names, staff_id, property)
430
- else
431
- @forecast_count_data = goal_forecast_month(forecast_default_dates, sale_names, staff_id, property)
432
- end
395
+ if params[:forecast_type].blank? || params[:forecast_type] == "money"
396
+ p "按金额"
397
+ goal_forecast_money
433
398
  else
434
- # 按年
435
- if params[:forecast_date_year].present?
436
- date = params[:forecast_date_year].split(" - ")
437
- dates = (date[0]..date[1]).to_a
438
- @forecast_count_data = goal_forecast_year(dates, sale_names, staff_id, property)
439
- else
440
- forecast_default_dates = ("#{Time.now.year}-01-01".to_date.."#{Time.now.year}-#{Time.now.month}-01".to_date).map { |d| d.strftime("%Y") }.uniq
441
- @forecast_count_data = goal_forecast_year(forecast_default_dates, sale_names, staff_id, property)
442
- end
399
+ p "按数量"
400
+ goal_forecast_count
443
401
  end
444
- @forecast_count_data_0 = @forecast_count_data[0]
445
- @forecast_count_data_1 = @forecast_count_data[1]
446
402
  end
447
403
  end
448
404
  end
@@ -738,5 +694,113 @@ module EducodeSales
738
694
  }
739
695
  end
740
696
 
697
+ def goal_forecast_money
698
+ property = params[:property] || nil
699
+ staff_id = params[:staff_id] || nil
700
+ @forecast_count_range = params[:forecast_count_range] || "month"
701
+ forecast_default_dates = ("#{Time.now.year}-01-01".to_date.."#{Time.now.year}-#{Time.now.month}-01".to_date).map { |d| d.strftime("%Y-%m") }.uniq
702
+ sale_names = %w[计划投标额 计划投标额累计 实际中标额 实际中标额累计]
703
+ case @forecast_count_range
704
+ when "week"
705
+ # 年初第一天周数是0会导致周数重复计算
706
+ if params[:forecast_date_day].present?
707
+ date = params[:forecast_date_day].split(" - ")
708
+ dates = (date[0].to_date..date[1].to_date).map { |d| d.strftime("%Y-%W") }.uniq.select { |d| d.split("-")[1] != '00' }
709
+ @forecast_count_data = goal_forecast_week(dates, sale_names, staff_id, property)
710
+ else
711
+ forecast_default_dates = ("#{Time.now.year}-01-01".to_date.."#{Time.now.year}-#{Time.now.month}-01".to_date).map { |d| d.strftime("%Y-%W") }.uniq.select { |d| d.split("-")[1] != '00' }
712
+ @forecast_count_data = goal_forecast_week(forecast_default_dates, sale_names, staff_id, property)
713
+ end
714
+ when "quarter"
715
+ # 按年
716
+ if params[:forecast_date_year].present?
717
+ date = params[:forecast_date_year].split(" - ")
718
+ dates = (date[0]..date[1]).to_a
719
+ dates = dates.map { |d| [d.to_s + "-" + "1", d.to_s + "-" + "2", d.to_s + "-" + "3", d.to_s + "-" + "4"] }.flatten
720
+ @forecast_count_data = goal_forecast_quarter(dates, sale_names, staff_id, property)
721
+ else
722
+ forecast_default_dates = ("#{Time.now.year}-01-01".to_date.."#{Time.now.year}-#{Time.now.month}-01".to_date).map { |d| d.strftime("%Y") }.uniq
723
+ forecast_default_dates = forecast_default_dates.map { |d| [d.to_s + "-" + "1", d.to_s + "-" + "2", d.to_s + "-" + "3", d.to_s + "-" + "4"] }.flatten
724
+ @forecast_count_data = goal_forecast_quarter(forecast_default_dates, sale_names, staff_id, property)
725
+ end
726
+ when "month" # 按月
727
+ if params[:forecast_date_month].present?
728
+ date = params[:forecast_date_month].split(" - ")
729
+ date[0] = (date[0] + "-01").to_date.to_s
730
+ date[1] = (date[1] + "-01").to_date.end_of_month.to_s
731
+ dates = ((date[0] + "-01").to_date..(date[1] + "-01").to_date).map { |d| d.strftime("%Y-%m") }.uniq
732
+ @forecast_count_data = goal_forecast_month(dates, sale_names, staff_id, property)
733
+ else
734
+ @forecast_count_data = goal_forecast_month(forecast_default_dates, sale_names, staff_id, property)
735
+ end
736
+ else
737
+ # 按年
738
+ if params[:forecast_date_year].present?
739
+ date = params[:forecast_date_year].split(" - ")
740
+ dates = (date[0]..date[1]).to_a
741
+ @forecast_count_data = goal_forecast_year(dates, sale_names, staff_id, property)
742
+ else
743
+ forecast_default_dates = ("#{Time.now.year}-01-01".to_date.."#{Time.now.year}-#{Time.now.month}-01".to_date).map { |d| d.strftime("%Y") }.uniq
744
+ @forecast_count_data = goal_forecast_year(forecast_default_dates, sale_names, staff_id, property)
745
+ end
746
+ end
747
+ @forecast_count_data_0 = @forecast_count_data[0]
748
+ @forecast_count_data_1 = @forecast_count_data[1]
749
+ end
750
+
751
+ def goal_forecast_count
752
+ property = params[:property] || nil
753
+ staff_id = params[:staff_id] || nil
754
+ @forecast_count_range = params[:forecast_count_range] || "month"
755
+ forecast_default_dates = ("#{Time.now.year}-01-01".to_date.."#{Time.now.year}-#{Time.now.month}-01".to_date).map { |d| d.strftime("%Y-%m") }.uniq
756
+ sale_names = %w[计划投标额 计划投标额累计 实际中标额 实际中标额累计]
757
+ case @forecast_count_range
758
+ when "week"
759
+ # 年初第一天周数是0会导致周数重复计算
760
+ if params[:forecast_date_day].present?
761
+ date = params[:forecast_date_day].split(" - ")
762
+ dates = (date[0].to_date..date[1].to_date).map { |d| d.strftime("%Y-%W") }.uniq.select { |d| d.split("-")[1] != '00' }
763
+ @forecast_count_data = goal_forecast_week_count(dates, sale_names, staff_id, property)
764
+ else
765
+ forecast_default_dates = ("#{Time.now.year}-01-01".to_date.."#{Time.now.year}-#{Time.now.month}-01".to_date).map { |d| d.strftime("%Y-%W") }.uniq.select { |d| d.split("-")[1] != '00' }
766
+ @forecast_count_data = goal_forecast_week_count(forecast_default_dates, sale_names, staff_id, property)
767
+ end
768
+ when "quarter"
769
+ # 按年
770
+ if params[:forecast_date_year].present?
771
+ date = params[:forecast_date_year].split(" - ")
772
+ dates = (date[0]..date[1]).to_a
773
+ dates = dates.map { |d| [d.to_s + "-" + "1", d.to_s + "-" + "2", d.to_s + "-" + "3", d.to_s + "-" + "4"] }.flatten
774
+ @forecast_count_data = goal_forecast_quarter_count(dates, sale_names, staff_id, property)
775
+ else
776
+ forecast_default_dates = ("#{Time.now.year}-01-01".to_date.."#{Time.now.year}-#{Time.now.month}-01".to_date).map { |d| d.strftime("%Y") }.uniq
777
+ forecast_default_dates = forecast_default_dates.map { |d| [d.to_s + "-" + "1", d.to_s + "-" + "2", d.to_s + "-" + "3", d.to_s + "-" + "4"] }.flatten
778
+ @forecast_count_data = goal_forecast_quarter_count(forecast_default_dates, sale_names, staff_id, property)
779
+ end
780
+ when "month" # 按月
781
+ if params[:forecast_date_month].present?
782
+ date = params[:forecast_date_month].split(" - ")
783
+ date[0] = (date[0] + "-01").to_date.to_s
784
+ date[1] = (date[1] + "-01").to_date.end_of_month.to_s
785
+ dates = ((date[0] + "-01").to_date..(date[1] + "-01").to_date).map { |d| d.strftime("%Y-%m") }.uniq
786
+ @forecast_count_data = goal_forecast_month_count(dates, sale_names, staff_id, property)
787
+ else
788
+ @forecast_count_data = goal_forecast_month_count(forecast_default_dates, sale_names, staff_id, property)
789
+ end
790
+ else
791
+ # 按年
792
+ if params[:forecast_date_year].present?
793
+ date = params[:forecast_date_year].split(" - ")
794
+ dates = (date[0]..date[1]).to_a
795
+ @forecast_count_data = goal_forecast_year_count(dates, sale_names, staff_id, property)
796
+ else
797
+ forecast_default_dates = ("#{Time.now.year}-01-01".to_date.."#{Time.now.year}-#{Time.now.month}-01".to_date).map { |d| d.strftime("%Y") }.uniq
798
+ @forecast_count_data = goal_forecast_year_count(forecast_default_dates, sale_names, staff_id, property)
799
+ end
800
+ end
801
+ @forecast_count_data_0 = @forecast_count_data[0]
802
+ @forecast_count_data_1 = @forecast_count_data[1]
803
+ end
804
+
741
805
  end
742
806
  end
@@ -132,7 +132,7 @@ module EducodeSales
132
132
  end
133
133
 
134
134
  def follow_up_params
135
- params.permit(:attitude_id, :follow_id, :course_plan_id, :course_build_id, :description, :advise)
135
+ params.permit(:attitude_id, :follow_id, :course_plan_id, :course_build_id, :description, :advise, :is_contact)
136
136
  end
137
137
 
138
138
  end
@@ -48,6 +48,21 @@ module EducodeSales
48
48
  if can?(:destroy, EducodeSales::Teacher)
49
49
  gon.menus << { title: '删除', event: 'delete' }
50
50
  end
51
+
52
+ filter = Filter.find_or_create_by(staff_id: @current_admin.id, clazz: "teachers")
53
+ gon.filter = filter.extras || {}
54
+ # if filter.extras.present?
55
+ # # if filter.extras['area'].nil?
56
+ # # filter.extras['area'] = 1
57
+ # # filter.extras['property'] = 1
58
+ # # filter.extras['sale_way'] = 1
59
+ # # end
60
+ # # filter.save
61
+ # gon.filter = filter.extras
62
+ # else
63
+ # gon.filter = {'area': 1, 'property': 1, 'sale_way': 1}
64
+ # end
65
+
51
66
  end
52
67
  format.json do
53
68
  @staff_manages = {}
@@ -181,6 +196,11 @@ module EducodeSales
181
196
  LEFT JOIN educode_sales_teacher_follows AS last_follow ON last_follow.id = educode_sales_teachers.follow_up_id AND last_follow.deleted_at IS NULL
182
197
  ")
183
198
  end
199
+
200
+ if params[:q].present? && params[:q][:is_contact].present?
201
+ @teachers = @teachers.where("last_follow.is_contact = ?" , params[:q][:is_contact].to_i)
202
+ end
203
+
184
204
  if params[:sort].present? && params[:sort][:field]
185
205
  @teachers = @teachers.order("#{params[:sort][:field]} #{params[:sort][:order]}")
186
206
  else
@@ -0,0 +1,347 @@
1
+ module EducodeSales
2
+ module SaleTrendsCountHelper
3
+ NAMES = %w[计划投标额 计划投标额累计 实际中标额 实际中标额累计]
4
+
5
+ def goal_forecast_quarter_count(labels, selects, staff_id = nil, property)
6
+ plan_get_count = plan_get_count(staff_id, labels, "quarter", property)
7
+ actual_get_count = actual_get_count(staff_id, labels, "quarter", property)
8
+ datasets = selects.map.with_index do |select, i|
9
+ data = if select == NAMES[0]
10
+ labels.map do |d|
11
+ quarter = d.split("-").last.to_i
12
+ year = d.split("-").first.to_i
13
+ plan_get_count[[year, quarter]]&.pluck(:business_id)&.uniq&.reject(&:blank?)&.size.to_f.round(3)
14
+ end
15
+ elsif select == NAMES[1]
16
+ arr = labels.map do |d|
17
+ quarter = d.split("-").last.to_i
18
+ year = d.split("-").first.to_i
19
+ plan_get_count[[year, quarter]]&.pluck(:business_id)&.uniq&.reject(&:blank?)&.size.to_f.round(3)
20
+ end
21
+ arr.map.with_index(1) { |num, i| arr.first(i).inject(:+) }
22
+ elsif select == NAMES[2]
23
+ labels.map do |d|
24
+ quarter = d.split("-").last.to_i
25
+ year = d.split("-").first.to_i
26
+ actual_get_count[[year, quarter]]&.pluck(:business_id)&.uniq&.reject(&:blank?)&.size.to_f.round(3)
27
+ end
28
+ elsif select == NAMES[3]
29
+ arr = labels.map do |d|
30
+ quarter = d.split("-").last.to_i
31
+ year = d.split("-").first.to_i
32
+ actual_get_count[[year, quarter]]&.pluck(:business_id)&.uniq&.reject(&:blank?)&.size.to_f.round(3)
33
+ end
34
+ arr.map.with_index(1) { |num, i| arr.first(i).inject(:+) }
35
+ end
36
+ type = if select == NAMES[3] || select == NAMES[1]
37
+ 'line'
38
+ else
39
+ "bar"
40
+ end
41
+ backgroundColor = if select == NAMES[3] || select == NAMES[1]
42
+ 'rgba(54, 162, 235, 0.2)'
43
+ else
44
+ SaleTrend::COLORS[i]
45
+ end
46
+ {
47
+ type: type,
48
+ label: select,
49
+ data: data.map { |d| d.round(2) },
50
+ backgroundColor:
51
+ backgroundColor,
52
+ borderColor:
53
+ SaleTrend::COLORS[i],
54
+ borderWidth: 1
55
+ }
56
+ end
57
+ hash_a = {
58
+ labels: labels,
59
+ datasets: [datasets[0], datasets[2]]
60
+ }
61
+ hash_b = {
62
+ labels: labels,
63
+ datasets: [datasets[1], datasets[3]]
64
+ }
65
+ [hash_a, hash_b]
66
+ end
67
+
68
+ def goal_forecast_week_count(labels, selects, staff_id = nil, property)
69
+
70
+ plan_get_count = plan_get_count(staff_id, labels, "week", property)
71
+ actual_get_count = actual_get_count(staff_id, labels, "week", property)
72
+ datasets = selects.map.with_index do |select, i|
73
+ data = if select == NAMES[0]
74
+ labels.map do |d|
75
+ week = d.split("-").last
76
+ year = d.split("-").first
77
+ yearweek = "#{year}#{week}".to_i
78
+ plan_get_count[yearweek].to_f.round(3)
79
+ end
80
+ elsif select == NAMES[1]
81
+ arr = labels.map do |d|
82
+ week = d.split("-").last
83
+ year = d.split("-").first
84
+ yearweek = "#{year}#{week}".to_i
85
+ plan_get_count[yearweek].to_f.round(3)
86
+ end
87
+ arr.map.with_index(1) { |num, i| arr.first(i).inject(:+) }
88
+ elsif select == NAMES[2]
89
+ labels.map do |d|
90
+ week = d.split("-").last
91
+ year = d.split("-").first
92
+ yearweek = "#{year}#{week}".to_i
93
+ actual_get_count[yearweek].to_f.round(3)
94
+ end
95
+ elsif select == NAMES[3]
96
+ arr = labels.map do |d|
97
+ week = d.split("-").last
98
+ year = d.split("-").first
99
+ yearweek = "#{year}#{week}".to_i
100
+ actual_get_count[yearweek].to_f.round(3)
101
+ end
102
+ arr.map.with_index(1) { |num, i| arr.first(i).inject(:+) }
103
+ end
104
+ type = if select == NAMES[0] || select == NAMES[2]
105
+ 'bar'
106
+ else
107
+ "line"
108
+ end
109
+ backgroundColor = if select == NAMES[0] || select == NAMES[2]
110
+ SaleTrend::COLORS[i]
111
+ else
112
+ 'rgba(54, 162, 235, 0.2)'
113
+ end
114
+ {
115
+ type: type,
116
+ label: select,
117
+ data: data.map { |d| d.round(2) },
118
+ backgroundColor:
119
+ backgroundColor,
120
+ borderColor:
121
+ SaleTrend::COLORS[i],
122
+ borderWidth: 1
123
+ }
124
+ end
125
+
126
+ hash_a = {
127
+ labels: labels,
128
+ datasets: [datasets[0], datasets[2]]
129
+ }
130
+ hash_b = {
131
+ labels: labels,
132
+ datasets: [datasets[1], datasets[3]]
133
+ }
134
+ [hash_a, hash_b]
135
+ end
136
+
137
+ def goal_forecast_month_count(labels, selects, staff_id = nil, property)
138
+ plan_get_count = plan_get_count(staff_id, labels, "month", property)
139
+ actual_get_count = actual_get_count(staff_id, labels, "month", property)
140
+ datasets = selects.map.with_index do |select, i|
141
+ data = if select == NAMES[0]
142
+ labels.map do |d|
143
+ month = d.split("-").last.to_i
144
+ year = d.split("-").first.to_i
145
+ plan_get_count[[year, month]]&.pluck(:business_id)&.uniq&.reject(&:blank?)&.size.to_f.round(3)
146
+ end
147
+ elsif select == NAMES[1]
148
+ arr = labels.map do |d|
149
+ month = d.split("-").last.to_i
150
+ year = d.split("-").first.to_i
151
+ plan_get_count[[year, month]]&.pluck(:business_id)&.uniq&.reject(&:blank?)&.size.to_f.round(3)
152
+ end
153
+ arr.map.with_index(1) { |num, i| arr.first(i).inject(:+) }
154
+ elsif select == NAMES[2]
155
+ labels.map do |d|
156
+ month = d.split("-").last.to_i
157
+ year = d.split("-").first.to_i
158
+ actual_get_count[[year, month]]&.pluck(:business_id)&.uniq&.reject(&:blank?)&.size.to_f.round(3)
159
+ end
160
+ elsif select == NAMES[3]
161
+ arr = labels.map do |d|
162
+ month = d.split("-").last.to_i
163
+ year = d.split("-").first.to_i
164
+ actual_get_count[[year, month]]&.pluck(:business_id)&.uniq&.reject(&:blank?)&.size.to_f.round(3)
165
+ end
166
+ arr.map.with_index(1) { |num, i| arr.first(i).inject(:+) }
167
+ end
168
+
169
+ type = if select == NAMES[3] || select == NAMES[1]
170
+ 'line'
171
+ else
172
+ "bar"
173
+ end
174
+ backgroundColor = if select == NAMES[3] || select == NAMES[1]
175
+ 'rgba(54, 162, 235, 0.2)'
176
+ else
177
+ SaleTrend::COLORS[i]
178
+ end
179
+ {
180
+ type: type,
181
+ label: select,
182
+ data: data.map { |d| d.round(2) },
183
+ backgroundColor:
184
+ backgroundColor,
185
+ borderColor:
186
+ SaleTrend::COLORS[i],
187
+ borderWidth: 1
188
+ }
189
+ end
190
+
191
+ hash_a = {
192
+ labels: labels,
193
+ datasets: [datasets[0], datasets[2]]
194
+ }
195
+ hash_b = {
196
+ labels: labels,
197
+ datasets: [datasets[1], datasets[3]]
198
+ }
199
+ [hash_a, hash_b]
200
+ end
201
+
202
+ def goal_forecast_year_count(labels, selects, staff_id = nil, property)
203
+ plan_get_count = plan_get_count(staff_id, labels, "year", property)
204
+ actual_get_count = actual_get_count(staff_id, labels, "year", property)
205
+ datasets = selects.map.with_index do |select, i|
206
+ data = if select == NAMES[0]
207
+ labels.map { |d| plan_get_count[d.to_i] || 0 }
208
+ elsif select == NAMES[1]
209
+ arr = labels.map { |d| plan_get_count[d.to_i] || 0 }
210
+ arr.map.with_index(1) { |num, i| arr.first(i).inject(:+) }
211
+ elsif select == NAMES[2]
212
+ labels.map { |d| actual_get_count[d.to_i] || 0 }
213
+ elsif select == NAMES[3]
214
+ arr = labels.map { |d| actual_get_count[d.to_i] || 0 }
215
+ arr.map.with_index(1) { |num, i| arr.first(i).inject(:+) }
216
+ end
217
+
218
+ type = if select == NAMES[3] || select == NAMES[1]
219
+ 'line'
220
+ else
221
+ "bar"
222
+ end
223
+ backgroundColor = if select == NAMES[3] || select == NAMES[1]
224
+ 'rgba(54, 162, 235, 0.2)'
225
+ else
226
+ SaleTrend::COLORS[i]
227
+ end
228
+ {
229
+ type: type,
230
+ label: select,
231
+ data: data.map { |d| d.round(2) },
232
+ backgroundColor:
233
+ backgroundColor,
234
+ borderColor:
235
+ SaleTrend::COLORS[i],
236
+ borderWidth: 1
237
+ }
238
+ end
239
+ hash_a = {
240
+ labels: labels,
241
+ datasets: [datasets[0], datasets[2]]
242
+ }
243
+ hash_b = {
244
+ labels: labels,
245
+ datasets: [datasets[1], datasets[3]]
246
+ }
247
+ [hash_a, hash_b]
248
+ end
249
+
250
+ private
251
+
252
+ def plan_get_count(staff_id, time_range, type, property)
253
+ # budget_amount 预算额
254
+ start_time = case type
255
+ when "month"
256
+ time_range.first + "-01"
257
+ when "year"
258
+ time_range.first + "-01-01"
259
+ when "week"
260
+ time_range.first.split("-").first + "-01-01"
261
+ when "quarter"
262
+ time_range.first.split("-").first + "-01-01"
263
+ end
264
+ end_time = case type
265
+ when "month"
266
+ time_range.last + "-31"
267
+ when "year"
268
+ time_range.last + "-12-31"
269
+ when "week"
270
+ time_range.last.split("-").first + "-12-31"
271
+ when "quarter"
272
+ time_range.last.split("-").first + "-12-31"
273
+ end
274
+ staff_id = staff_id.present? ? Array(staff_id) : nil
275
+ data = Business.joins(:last_follow_up)
276
+ .where("educode_sales_follow_ups.invitation_at >= ? and educode_sales_follow_ups.invitation_at <= ?", start_time, end_time)
277
+ if staff_id.present?
278
+ data = data.joins("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
279
+ LEFT JOIN educode_sales_assign_follow_ups ON educode_sales_assign_follow_ups.follow_up_id = last_follow_up.id").
280
+ 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 = ?)", staff_id, staff_id)
281
+ end
282
+ if property.present?
283
+ data = data.joins(department: [school: :school_tags]).where("school_tags.id = ?", property)
284
+ end
285
+ case type
286
+ when "week"
287
+ data.select("educode_sales_follow_ups.*, yearweek(educode_sales_follow_ups.invitation_at) as week").group("yearweek(educode_sales_follow_ups.invitation_at)").count("distinct educode_sales_follow_ups.business_id")
288
+ when "quarter"
289
+ data = data.select("educode_sales_follow_ups.*, quarter(educode_sales_follow_ups.invitation_at) as quarter, year(educode_sales_follow_ups.invitation_at) as year")
290
+ data.group_by { |d| [d.year, d.quarter] }
291
+ when "month"
292
+ data = data.select("educode_sales_follow_ups.*, year(educode_sales_follow_ups.invitation_at) as year, month(educode_sales_follow_ups.invitation_at) as month")
293
+ data.group_by { |d| [d.year, d.month] }
294
+ when "year"
295
+ data.select("educode_sales_follow_ups.*, year(educode_sales_follow_ups.invitation_at) as year").group("year(educode_sales_follow_ups.invitation_at)").count("distinct educode_sales_follow_ups.business_id")
296
+ end
297
+ end
298
+
299
+ def actual_get_count(staff_id, time_range, type, property)
300
+ start_time = case type
301
+ when "month"
302
+ time_range.first + "-01"
303
+ when "year"
304
+ time_range.first + "-01-01"
305
+ when "week"
306
+ time_range.first.split("-").first + "-01-01"
307
+ when "quarter"
308
+ time_range.first.split("-").first + "-01-01"
309
+ end
310
+ end_time = case type
311
+ when "month"
312
+ time_range.last + "-31"
313
+ when "year"
314
+ time_range.last + "-12-31"
315
+ when "week"
316
+ time_range.last.split("-").first + "-12-31"
317
+ when "quarter"
318
+ time_range.last.split("-").first + "-12-31"
319
+ end
320
+ # actual_amount 合同额
321
+ staff_id = staff_id.present? ? Array(staff_id) : nil
322
+ data = Business.joins(:last_follow_up)
323
+ .where("educode_sales_follow_ups.bidded_date >= ? and educode_sales_follow_ups.bidded_date <= ? ", start_time, end_time)
324
+ if staff_id.present?
325
+ data = data.joins("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
326
+ LEFT JOIN educode_sales_assign_follow_ups ON educode_sales_assign_follow_ups.follow_up_id = last_follow_up.id").
327
+ 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 = ?)", staff_id, staff_id)
328
+ end
329
+ if property.present?
330
+ data = data.joins(department: [school: :school_tags]).where("school_tags.id = ?", property)
331
+ end
332
+ case type
333
+ when "week"
334
+ Business.joins(:last_follow_up).select("educode_sales_follow_ups.*, yearweek(educode_sales_follow_ups.bidded_date) as week").group("yearweek(educode_sales_follow_ups.bidded_date)").count("distinct educode_sales_follow_ups.business_id")
335
+ when "quarter"
336
+ data = data.select("educode_sales_follow_ups.*, quarter(educode_sales_follow_ups.bidded_date) as quarter, year(educode_sales_follow_ups.bidded_date) as year")
337
+ data.group_by { |d| [d.year, d.quarter] }
338
+ when "month"
339
+ data = data.select("educode_sales_follow_ups.*, year(educode_sales_follow_ups.bidded_date) as year, month(educode_sales_follow_ups.bidded_date) as month")
340
+ data.group_by { |d| [d.year, d.month] }
341
+ when "year"
342
+ data.select("educode_sales_follow_ups.*, year(educode_sales_follow_ups.bidded_date) as year").group("year(educode_sales_follow_ups.bidded_date)").count("distinct educode_sales_follow_ups.business_id")
343
+ end
344
+ end
345
+
346
+ end
347
+ end