educode_sales 0.9.3 → 0.9.4

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: d6a4ffe19355fdf757cb3714a1332b6159b0c3825f2b788cdc85c27a2ea35ce1
4
- data.tar.gz: 31407d9b5ac98caff94bd770a06bf90c4b0326bd98a1813eb7e8405808b02d12
3
+ metadata.gz: ebca1b78e4b0b3998f9584ccf0252df25b0cbe341a2e57e69e27e3c2d9ce78d0
4
+ data.tar.gz: 6ae3c8a027f5388a093dc08d8586162b37d59e807ec39e3caf01b60174aec629
5
5
  SHA512:
6
- metadata.gz: 3246c013b2e6a35a0fe3ed9c9fba8076000569c86e9df03b945e0a65918a25db265a63d730f1e0a9a4ccec56b9186ef9c464b8e7d6424cc71fcb7300ec941abc
7
- data.tar.gz: 983547ec16762c558b4b5162c7c95b0cdb53530b58f74bf536a990e816954d7690539c5d033eca1baba5603939f214b34689dcfed9e5c518404d4ebf9f737b58
6
+ metadata.gz: 9408e570a47a640475921a88c573674692f14c7e0f91e9203004c3314b7b6180ddb76976a1048c2d9e6ed43b8730afbc752ebdfa5352cf8ce6f70870c33c3259
7
+ data.tar.gz: e56f1e509d44449d67cc32428b2024aea8a2a9a4984370795b6b74de671e242b1649aebf0163ee9369e98592148f8727ac7c0cbc7c79a4c9659205c01743c471
@@ -8,9 +8,9 @@ module EducodeSales
8
8
  # authorize! :trends, EducodeSales::SaleTrend
9
9
  @year = params[:year] ? params[:year] : Time.now.year
10
10
  @years = ['全部'] + (1..(Time.now.year - 2014)).reverse_each.map { |d| 2014 + d }
11
+ x = Common.find_by(extras: EducodeSales::Common::XTYPE)&.id
12
+ o = Common.find_by(extras: EducodeSales::Common::OTYPE)&.id
11
13
  if (@year == '全部')
12
- x = Common.find_by(extras: EducodeSales::Common::XTYPE)&.id
13
- o = Common.find_by(extras: EducodeSales::Common::OTYPE)&.id
14
14
  @business_amount = Business.joins(:last_follow_up).where("educode_sales_follow_ups.clazz_id != ?", x).sum(:total_amount).round(2)
15
15
  stage_ids = Common.where(clazz: '商机阶段', name: ['已中标', '已签单', '已验收', '回款中', '服务中', '已结束']).pluck(:id)
16
16
  @goal_amount = Business.joins(:last_follow_up).where("educode_sales_follow_ups.clazz_id != ?", x).where("educode_sales_follow_ups.stage_id IN (?)", stage_ids).sum(:total_amount).round(2)
@@ -26,8 +26,7 @@ module EducodeSales
26
26
  else
27
27
  year_time = "#{@year}-01-01 00:00:00"
28
28
  year_over_time = "#{@year}-12-31 23:59:59"
29
- x = Common.find_by(extras: EducodeSales::Common::XTYPE)&.id
30
- o = Common.find_by(extras: EducodeSales::Common::OTYPE)&.id
29
+
31
30
  @sale_trend = SaleTrend.find_or_create_by(year: @year)
32
31
  @business_amount = Business.joins(:last_follow_up).where("educode_sales_follow_ups.clazz_id != ?", x).where("educode_sales_follow_ups.year = ?", @year).sum(:total_amount).round(2)
33
32
  stage_ids = Common.where(clazz: '商机阶段', name: ['已中标', '已签单', '已验收', '回款中', '服务中', '已结束']).pluck(:id)
@@ -43,254 +42,301 @@ module EducodeSales
43
42
  @b_amount = Business.joins(:last_follow_up).where("educode_sales_follow_ups.clazz_id IN (?)", b).where("educode_sales_follow_ups.year = ?", @year).sum(:total_amount).round(2)
44
43
  end
45
44
 
46
- count_type = params[:count_type] || "actual_amount"
47
- common = Common.find_by(clazz: 'staff_type', name: '销售')
48
- names = Staff.includes(:user).where(job_type: common.id).map { |d| [d.user.real_name, d.id, d.role_id] }
49
- x_business_ids = []
50
- default_dates = (30.day.ago.to_date..Date.today).map { |d| d.strftime("%Y-%W") }.uniq #默认时间范围
51
- Business.all.each do |d|
52
- if d.last_follow_up&.clazz&.id == x
53
- x_business_ids << d.id
54
- end
45
+ end
46
+
47
+ def sale_trends
48
+ sale_trend = SaleTrend.find_by(year: params[:year])
49
+ if sale_trend.update(sale_trend_params)
50
+ render_success
51
+ else
52
+ render_failure sale_trend
55
53
  end
56
- colors = ['#44D7B6', '#4CACFF', '#F7B500', '#FF0000', '#FF7F00', '#FFFF00', '#00FF00', '#00FFFF', '#0000FF', '#8B00FF', 'rgba(255, 99, 132, 1)', 'rgba(54, 162, 235, 1)', 'rgba(255, 206, 86, 1)', 'rgba(75, 192, 192, 1)', 'rgba(153, 102, 255, 1)', 'rgba(255, 159, 64, 1)', 'rgba(255, 0, 250, 1)']
54
+ end
55
+
56
+ def operations
57
+
58
+ end
59
+
60
+ # 商机跟进数
61
+ def business_followup_analysis
62
+ common = EducodeSales::Common.find_by(clazz: 'staff_type', name: '销售')
63
+ names = EducodeSales::Staff.includes(:user).where(job_type: common.id).map { |d| [d.user.real_name, d.id, d.role_id] }
64
+ x = EducodeSales::Common.find_by(extras: EducodeSales::Common::XTYPE)&.id
65
+ x_business_ids = EducodeSales::Business.joins(last_follow_up: :clazz).where("educode_sales_commons.id = ?",x).pluck :id
66
+
57
67
  @follow_count_range = params[:follow_count_range] || "week"
58
- @goal_count_range = params[:goal_count_range] || "month"
59
68
  # 商机跟进数图表
60
- @follow_count_data = week_follow_count_chart(default_dates, names, x_business_ids, colors)
61
- if params[:follow_count_range].present?
62
- case params[:follow_count_range]
63
- when "week" #按周
64
- if params[:date_week].present?
65
- date = params[:date_week].split(" - ")
66
- dates = (date[0].to_date..date[1].to_date).map { |d|
67
- d.strftime("%Y-%W") }.uniq
68
- @follow_count_data = week_follow_count_chart(dates, names, x_business_ids, colors)
69
- end
70
- when "month" #按月
71
- if params[:date_month].present?
72
- # (Date.new(2020,12,1)..Date.new(2021,12,1)).map { |d| d.strftime("%Y-%m")}.uniq
73
- date = params[:date_month].split(" - ")
74
- dates = ((date[0] + "-01").to_date..(date[1] + "-01").to_date).map { |d| d.strftime("%Y-%m") }.uniq
75
- @follow_count_data = month_follow_count_chart(dates, names, x_business_ids, colors)
76
- end
77
- when "year" #按年
78
- if params[:date_year].present?
79
- date = params[:date_year].split(" - ")
80
- dates = (date[0]..date[1]).to_a
81
- @follow_count_data = year_follow_count_chart(dates, names, x_business_ids, colors)
69
+ respond_to do |format|
70
+ format.html do
71
+
72
+ end
73
+ format.js do
74
+ default_dates = (30.day.ago.to_date..Date.today).map { |d| d.strftime("%Y-%W") }.uniq #默认时间范围
75
+ @follow_count_data = business_followup_charts(default_dates, names, x_business_ids, SaleTrend::COLORS, [30.day.ago.to_date.beginning_of_week, Date.today], "%Y-%u") do |default_dates|
76
+ default_dates.map { |date|
77
+ d = date.split("-")
78
+ year = d[0].to_i
79
+ week = d[1].to_i
80
+ if week == 0
81
+ 0
82
+ else
83
+ month = Date.commercial(year, week).strftime("%m月%d日")
84
+ day = Date.commercial(year, week, 7).strftime("%m月%d日")
85
+ date.to_s + "(#{month}-#{day})"
86
+ end
87
+ } - [0]
82
88
  end
83
- else
84
- #按天
85
- if params[:date].present?
86
- date = params[:date].split(" - ")
87
- dates = (date[0].to_date..date[1].to_date).to_a
88
- @follow_count_data = day_follow_count_chart(dates, names, x_business_ids, colors)
89
+ end
90
+ format.json do
91
+ if params[:follow_count_range].present?
92
+ case params[:follow_count_range]
93
+ when "week" #按周
94
+ if params[:date_week].present?
95
+ date = params[:date_week].split(" - ")
96
+ dates = (date[0].to_date..date[1].to_date).map { |d| d.strftime("%Y-%W") }.uniq
97
+ @follow_count_data = business_followup_charts(dates, names, x_business_ids, SaleTrend::COLORS, date, "%Y%u") do |dates|
98
+ dates.map { |date|
99
+ d = date.split("-")
100
+ year = d[0].to_i
101
+ week = d[1].to_i
102
+ if week == 0
103
+ 0
104
+ else
105
+ month = Date.commercial(year, week).strftime("%m月%d日")
106
+ day = Date.commercial(year, week, 7).strftime("%m月%d日")
107
+ date.to_s + "(#{month}-#{day})"
108
+ end
109
+ } - [0]
110
+ end
111
+ else
112
+ return render json: {msg: '请选择时间范围', success: false }
113
+ end
114
+ when "month" #按月
115
+ if params[:date_month].present?
116
+ date = params[:date_month].split(" - ")
117
+ dates = ((date[0] + "-01").to_date..(date[1] + "-01").to_date).map { |d| d.strftime("%Y-%m") }.uniq
118
+ @follow_count_data = business_followup_charts(dates, names, x_business_ids, SaleTrend::COLORS, [(date[0] + "-01").to_date, (date[1] + "-01").to_date.end_of_month], '%Y%m')
119
+ else
120
+ return render json: {msg: '请选择时间范围', success: false }
121
+ end
122
+ when "year" #按年
123
+ if params[:date_year].present?
124
+ date = params[:date_year].split(" - ")
125
+ dates = (date[0]..date[1]).to_a.map { |d| d.to_s}
126
+ @follow_count_data = business_followup_charts(dates, names, x_business_ids, SaleTrend::COLORS, ["#{date[0]}-01-01".to_date.beginning_of_year, "#{date[1]}-01-01".to_date.end_of_year], '%Y')
127
+ else
128
+ return render json: {msg: '请选择时间范围', success: false }
129
+ end
130
+ else
131
+ #按天
132
+ if params[:date].present?
133
+ date = params[:date].split(" - ")
134
+ dates = (date[0].to_date..date[1].to_date).to_a.map { |d| d.to_s}
135
+ @follow_count_data = business_followup_charts(dates, names, x_business_ids, SaleTrend::COLORS, [date[0].to_date, date[1].to_date], '%Y%m%d')
136
+ else
137
+ return render json: {msg: '请选择时间范围', success: false }
138
+ end
139
+ end
89
140
  end
141
+ render json: {data: @follow_count_data }
90
142
  end
91
143
  end
144
+
145
+ end
92
146
 
93
- #商机区域分布图表
94
- provinces = EducodeSales::Common.where(clazz: 'area').pluck(:name)
95
- if params[:business_count_type] == 'money' || params[:business_count_type].blank?
96
- @business_data = {
97
- labels: provinces,
98
- datasets: [
99
- {
100
- label: "商机总额",
101
- data: provinces.map { |province|
102
- if params[:business_type].blank?
103
- Business.joins(:last_follow_up).where("educode_sales_businesses.department_id in (?)", School.joins(:departments).where(province: province).pluck("departments.id")).where("educode_sales_follow_ups.clazz_id != ?", x).sum(:total_amount).round(2)
104
- else
105
- Business.joins(:last_follow_up).where("educode_sales_businesses.department_id in (?)", School.joins(:departments).where(province: province).pluck("departments.id")).where("educode_sales_follow_ups.clazz_id = ?", params[:business_type]).sum(:total_amount).round(2)
106
- end
107
- },
108
- backgroundColor: colors[0],
109
- borderColor: colors[0],
110
- borderWidth: 1
111
- }
112
- ]
113
- }
114
- else
115
- @business_data = {
116
- labels: provinces,
117
- datasets: [
118
- {
119
- label: "商机数量",
120
- data: provinces.map { |province|
121
- if params[:business_type].blank?
122
- Business.joins(:last_follow_up).where("educode_sales_businesses.department_id in (?)", School.joins(:departments).where(province: province).pluck("departments.id")).where("educode_sales_follow_ups.clazz_id != ?", x).count
123
- else
124
- Business.joins(:last_follow_up).where("educode_sales_businesses.department_id in (?)", School.joins(:departments).where(province: province).pluck("departments.id")).where("educode_sales_follow_ups.clazz_id = ?", params[:business_type]).count
125
- end
126
- },
127
- backgroundColor: colors[0],
128
- borderColor: colors[0],
129
- borderWidth: 1
130
- }
131
- ]
132
- }
133
- end
147
+ # 销售额分析
148
+ def sales_analysis
149
+ respond_to do |format|
150
+ format.html do
151
+
152
+ end
153
+ format.js do
154
+ x = EducodeSales::Common.find_by(extras: EducodeSales::Common::XTYPE)&.id
155
+ count_type = params[:count_type] || "actual_amount"
156
+ stage_ids = Common.where(clazz: '商机阶段', name: ['已中标', '已签单', '已验收', '回款中', '服务中', '已结束']).pluck(:id)
157
+ s_stage_ids = Common.where(clazz: '商机阶段', name: ['已签单', '已验收', '回款中', '服务中', '已结束']).pluck(:id)
158
+ @goal_count_range = params[:goal_count_range] || "month"
134
159
 
135
- # 销售人员跟进分析
136
- begin_time = Time.now.at_beginning_of_year.to_s
137
- end_time = Time.now.at_end_of_year.to_s
138
- customer_time_range = params[:customer_time_range]
139
- case customer_time_range
140
- when 'last_week'
141
- begin_time = (Time.now - 1.week).at_beginning_of_week.to_s
142
- end_time = (Time.now - 1.week).at_end_of_week.to_s
143
- when 'this_week'
144
- begin_time = Time.now.at_beginning_of_week.to_s
145
- end_time = Time.now.at_end_of_week.to_s
146
- when 'last_month'
147
- begin_time = (Time.now - 1.month).at_beginning_of_month.to_s
148
- end_time = (Time.now - 1.month).at_end_of_month.to_s
149
- when 'this_month'
150
- begin_time = Time.now.at_beginning_of_month.to_s
151
- end_time = Time.now.at_end_of_month.to_s
152
- when 'last_year'
153
- begin_time = (Time.now - 1.year).at_beginning_of_year.to_s
154
- end_time = (Time.now - 1.year).at_end_of_year.to_s
155
- when 'this_year'
156
- begin_time = Time.now.at_beginning_of_year.to_s
157
- end_time = Time.now.at_end_of_year.to_s
158
- when 'all'
159
- begin_time = (Time.now - 5.year).at_beginning_of_year.to_s
160
- end_time = (Time.now + 5.year).at_end_of_year.to_s
161
- when 'diy'
162
- if params[:customer_date].present?
163
- date = params[:customer_date].split(" - ")
164
- begin_time = date[0] + "00:00:00"
165
- end_time = date[1] + "23:59:59"
160
+ goal_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
161
+ sale_names = ['已中标', '已签单', '已回款']
162
+ @goal_count_data = month_sale_chart(goal_default_dates, sale_names, SaleTrend::COLORS, x, stage_ids, s_stage_ids, count_type)
163
+ if params[:goal_count_range].present?
164
+ case params[:goal_count_range]
165
+ when "week"
166
+ when "month" #按月
167
+ if params[:goal_date_month].present?
168
+ date = params[:goal_date_month].split(" - ")
169
+ dates = ((date[0] + "-01").to_date..(date[1] + "-01").to_date).map { |d| d.strftime("%Y-%m") }.uniq
170
+ @goal_count_data = month_sale_chart(dates, sale_names, SaleTrend::COLORS, x, stage_ids, s_stage_ids, count_type)
171
+ end
172
+ else
173
+ #按年
174
+ if params[:goal_date_year].present?
175
+ date = params[:goal_date_year].split(" - ")
176
+ dates = (date[0]..date[1]).to_a
177
+ @goal_count_data = year_sale_chart(dates, sale_names, SaleTrend::COLORS, x, stage_ids, s_stage_ids, count_type)
178
+ end
179
+ end
180
+ end
166
181
  end
167
182
  end
168
- common = Common.find_by(clazz: 'staff_type', name: '销售')
169
- staff_names = Staff.joins(:user).where(job_type: common.id).map { |d| d.user.real_name }
170
- staff_ids = Staff.joins(:user).where(job_type: common.id).pluck(:id)
171
- if params[:customer_count_type] == 'money' || params[:customer_count_type].blank?
172
- @customer_data = {
173
- labels: staff_names,
174
- datasets: [
175
- {
176
- label: "跟进客户数",
177
- data: staff_ids.map { |staff_id|
178
- (EducodeSales::CustomerFollow.where(staff_id: staff_id).where("created_at >= ? AND created_at <= ?", begin_time, end_time).pluck(:school_id) + EducodeSales::Business.where(id: EducodeSales::FollowUp.where(staff_id: staff_id).where("created_at >= ? AND created_at <= ?", begin_time, end_time).pluck(:business_id).uniq).pluck(:school_id)).uniq.size
179
- },
180
- backgroundColor: colors[0],
181
- borderColor: colors[0],
182
- borderWidth: 1
183
+ end
184
+
185
+ # 商机区域分布
186
+ def business_area
187
+ respond_to do |format|
188
+ format.html do
189
+
190
+ end
191
+ format.js do
192
+ x = EducodeSales::Common.find_by(extras: EducodeSales::Common::XTYPE)&.id
193
+ provinces = EducodeSales::Common.where(clazz: 'area').pluck(:name)
194
+ if params[:business_count_type] == 'money' || params[:business_count_type].blank?
195
+ @business_data = {
196
+ labels: provinces,
197
+ datasets: [
198
+ {
199
+ label: "商机总额",
200
+ data: provinces.map { |province|
201
+ if params[:business_type].blank?
202
+ Business.joins(:last_follow_up).where("educode_sales_businesses.department_id in (?)", School.joins(:departments).where(province: province).pluck("departments.id")).where("educode_sales_follow_ups.clazz_id != ?", x).sum(:total_amount).round(2)
203
+ else
204
+ Business.joins(:last_follow_up).where("educode_sales_businesses.department_id in (?)", School.joins(:departments).where(province: province).pluck("departments.id")).where("educode_sales_follow_ups.clazz_id = ?", params[:business_type]).sum(:total_amount).round(2)
205
+ end
206
+ },
207
+ backgroundColor: SaleTrend::COLORS[0],
208
+ borderColor: SaleTrend::COLORS[0],
209
+ borderWidth: 1
210
+ }
211
+ ]
183
212
  }
184
- ]
185
- }
186
- else
187
- @customer_data = {
188
- labels: staff_names,
189
- datasets: [
190
- {
191
- label: "跟进客户次数",
192
- data: staff_ids.map { |staff_id|
193
- EducodeSales::CustomerFollow.where(staff_id: staff_id).where("created_at >= ? AND created_at <= ?", begin_time, end_time).size + EducodeSales::FollowUp.where(staff_id: staff_id).where("created_at >= ? AND created_at <= ?", begin_time, end_time).size
194
- },
195
- backgroundColor: colors[0],
196
- borderColor: colors[0],
197
- borderWidth: 1
213
+ else
214
+ @business_data = {
215
+ labels: provinces,
216
+ datasets: [
217
+ {
218
+ label: "商机数量",
219
+ data: provinces.map { |province|
220
+ if params[:business_type].blank?
221
+ Business.joins(:last_follow_up).where("educode_sales_businesses.department_id in (?)", School.joins(:departments).where(province: province).pluck("departments.id")).where("educode_sales_follow_ups.clazz_id != ?", x).count
222
+ else
223
+ Business.joins(:last_follow_up).where("educode_sales_businesses.department_id in (?)", School.joins(:departments).where(province: province).pluck("departments.id")).where("educode_sales_follow_ups.clazz_id = ?", params[:business_type]).count
224
+ end
225
+ },
226
+ backgroundColor: SaleTrend::COLORS[0],
227
+ borderColor: SaleTrend::COLORS[0],
228
+ borderWidth: 1
229
+ }
230
+ ]
198
231
  }
199
- ]
200
- }
201
- end
202
-
203
- # 销售额分析
204
- goal_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
205
- sale_names = ['已中标', '已签单', '已回款']
206
- @goal_count_data = month_sale_chart(goal_default_dates, sale_names, colors, x, stage_ids, s_stage_ids, count_type)
207
- if params[:goal_count_range].present?
208
- case params[:goal_count_range]
209
- when "week"
210
- when "month" #按月
211
- if params[:goal_date_month].present?
212
- date = params[:goal_date_month].split(" - ")
213
- dates = ((date[0] + "-01").to_date..(date[1] + "-01").to_date).map { |d| d.strftime("%Y-%m") }.uniq
214
- @goal_count_data = month_sale_chart(dates, sale_names, colors, x, stage_ids, s_stage_ids, count_type)
215
- end
216
- else
217
- #按年
218
- if params[:goal_date_year].present?
219
- date = params[:goal_date_year].split(" - ")
220
- dates = (date[0]..date[1]).to_a
221
- @goal_count_data = year_sale_chart(dates, sale_names, colors, x, stage_ids, s_stage_ids, count_type)
222
232
  end
223
233
  end
224
234
  end
225
235
  end
226
236
 
227
- def sale_trends
228
- sale_trend = SaleTrend.find_by(year: params[:year])
229
- if sale_trend.update(sale_trend_params)
230
- render_success
231
- else
232
- render_failure sale_trend
237
+ # 销售人员跟进分析
238
+ def sales_followup_analysis
239
+ respond_to do |format|
240
+ format.html do
241
+
242
+ end
243
+ format.js do
244
+ # 销售人员跟进分析
245
+ @goal_count_range = params[:goal_count_range] || "month"
246
+ begin_time = Time.now.at_beginning_of_year.to_s
247
+ end_time = Time.now.at_end_of_year.to_s
248
+ customer_time_range = params[:customer_time_range]
249
+ case customer_time_range
250
+ when 'last_week'
251
+ begin_time = (Time.now - 1.week).at_beginning_of_week.to_s
252
+ end_time = (Time.now - 1.week).at_end_of_week.to_s
253
+ when 'this_week'
254
+ begin_time = Time.now.at_beginning_of_week.to_s
255
+ end_time = Time.now.at_end_of_week.to_s
256
+ when 'last_month'
257
+ begin_time = (Time.now - 1.month).at_beginning_of_month.to_s
258
+ end_time = (Time.now - 1.month).at_end_of_month.to_s
259
+ when 'this_month'
260
+ begin_time = Time.now.at_beginning_of_month.to_s
261
+ end_time = Time.now.at_end_of_month.to_s
262
+ when 'last_year'
263
+ begin_time = (Time.now - 1.year).at_beginning_of_year.to_s
264
+ end_time = (Time.now - 1.year).at_end_of_year.to_s
265
+ when 'this_year'
266
+ begin_time = Time.now.at_beginning_of_year.to_s
267
+ end_time = Time.now.at_end_of_year.to_s
268
+ when 'all'
269
+ begin_time = (Time.now - 5.year).at_beginning_of_year.to_s
270
+ end_time = (Time.now + 5.year).at_end_of_year.to_s
271
+ when 'diy'
272
+ if params[:customer_date].present?
273
+ date = params[:customer_date].split(" - ")
274
+ begin_time = date[0] + " 00:00:00"
275
+ end_time = date[1] + " 23:59:59"
276
+ end
277
+ end
278
+ common = Common.find_by(clazz: 'staff_type', name: '销售')
279
+ staff_names = Staff.joins(:user).where(job_type: common.id).map { |d| d.user.real_name }
280
+ staff_ids = Staff.joins(:user).where(job_type: common.id).pluck(:id)
281
+ if params[:customer_count_type] == 'money' || params[:customer_count_type].blank?
282
+ @customer_data = {
283
+ labels: staff_names,
284
+ datasets: [
285
+ {
286
+ label: "跟进客户数",
287
+ data: staff_ids.map { |staff_id|
288
+ (EducodeSales::CustomerFollow.where(staff_id: staff_id).where("created_at >= ? AND created_at <= ?", begin_time, end_time).pluck(:school_id) + EducodeSales::Business.where(id: EducodeSales::FollowUp.where(staff_id: staff_id).where("created_at >= ? AND created_at <= ?", begin_time, end_time).pluck(:business_id).uniq).pluck(:school_id)).uniq.size
289
+ },
290
+ backgroundColor: SaleTrend::COLORS[0],
291
+ borderColor: SaleTrend::COLORS[0],
292
+ borderWidth: 1
293
+ }
294
+ ]
295
+ }
296
+ else
297
+ @customer_data = {
298
+ labels: staff_names,
299
+ datasets: [
300
+ {
301
+ label: "跟进客户次数",
302
+ data: staff_ids.map { |staff_id|
303
+ EducodeSales::CustomerFollow.where(staff_id: staff_id).where("created_at >= ? AND created_at <= ?", begin_time, end_time).size + EducodeSales::FollowUp.where(staff_id: staff_id).where("created_at >= ? AND created_at <= ?", begin_time, end_time).size
304
+ },
305
+ backgroundColor: SaleTrend::COLORS[0],
306
+ borderColor: SaleTrend::COLORS[0],
307
+ borderWidth: 1
308
+ }
309
+ ]
310
+ }
311
+ end
312
+ end
233
313
  end
234
314
  end
235
315
 
236
- def operations
237
-
238
- end
239
-
240
316
  private
241
317
 
242
318
  def sale_trend_params
243
319
  params.permit(:chance_money, :ballot_money, :sign_money, :returned_money)
244
320
  end
245
321
 
246
- def day_follow_count_chart(dates, names, x_business_ids, colors)
247
- {
248
- labels: dates,
249
- datasets: names.map.with_index do |name, i|
250
- {
251
- # hidden: i < 3 ? false : true,
252
- hidden: name[2] == 11,
253
- label: name[0],
254
- data: dates.map { |d| @follow_up = FollowUp.where(staff_id: name[1], created_at: d.beginning_of_day..d.end_of_day).where.not(business: x_business_ids).count },
255
- backgroundColor: "#fff",
256
- pointBorderColor: colors[i % 15 + 1],
257
- borderColor: colors[i % 15 + 1],
258
- pointBackgroundColor: colors[i % 15 + 1],
259
- borderWidth: 2
260
- }
261
- end
262
- }
263
- end
322
+ def business_followup_charts(dates, names, x_business_ids, colors, date, clazz)
323
+ not_business = x_business_ids.present? ? x_business_ids.join(',') : ''
324
+ staff_ids = names.present? ? names.map { |d| d[1]}.join(",") : ''
264
325
 
265
- def week_follow_count_chart(dates, names, x_business_ids, colors)
326
+ sql = "SELECT DATE_FORMAT(created_at, '#{clazz}') as week, count(*) as counts, staff_id
327
+ FROM educode_sales_follow_ups
328
+ WHERE date(created_at) >= '#{date[0]}' and date(created_at) <= '#{date[1]}'
329
+ AND business_id NOT IN (#{not_business}) and educode_sales_follow_ups.deleted_at IS NULL
330
+ AND staff_id IN (#{staff_ids})
331
+ GROUP BY DATE_FORMAT(created_at, '#{clazz}'), staff_id"
332
+ data = EducodeSales::FollowUp.find_by_sql(sql).group_by { |d| "#{d['staff_id']}-#{d['week']}" }
266
333
  {
267
- labels: dates.map { |date|
268
- d = date.split("-")
269
- year = d[0].to_i
270
- week = d[1].to_i
271
- if week == 0
272
- 0
273
- else
274
- month = Date.commercial(year, week).strftime("%m月%d日")
275
- day = Date.commercial(year, week, 7).strftime("%m月%d日")
276
- date.to_s + "(#{month}-#{day})"
277
- end
278
- } - [0],
334
+ labels: block_given? ? yield(dates) : dates,
279
335
  datasets: names.map.with_index do |name, i|
280
336
  {
281
- # hidden: i < 3 ? false : true,
282
337
  label: name[0],
283
338
  hidden: name[2] == 11, #role_id:11 离职角色
284
- data: dates.map { |d|
285
- d = d.split("-")
286
- year = d[0].to_i
287
- week = d[1].to_i
288
- if week == 0
289
- @follow_up = 999999
290
- else
291
- @follow_up = FollowUp.where(staff_id: name[1]).where("created_at >= ? AND created_at <= ?", Date.commercial(year, week).strftime("%Y-%m-%d"), Date.commercial(year, week, 7).strftime("%Y-%m-%d")).where.not(business: x_business_ids).count
292
- end
293
- } - [999999],
339
+ data: dates.map { |date| data["#{name[1]}-#{ date.gsub("-", '')}"]&.[](0)&.[]('counts') || 0 },
294
340
  backgroundColor: "#fff",
295
341
  pointBorderColor: colors[i % 15 + 1],
296
342
  borderColor: colors[i % 15 + 1],
@@ -301,43 +347,6 @@ module EducodeSales
301
347
  }
302
348
  end
303
349
 
304
- def month_follow_count_chart(dates, names, x_business_ids, colors)
305
- {
306
- labels: dates,
307
- datasets: names.map.with_index do |name, i|
308
- {
309
- # hidden: i < 3 ? false : true,
310
- label: name[0],
311
- hidden: name[2] == 11,
312
- data: dates.map { |d| @follow_up = FollowUp.where(staff_id: name[1]).where("created_at >= ? AND created_at <= ?", "#{d}-01", "#{d}-31").where.not(business: x_business_ids).count },
313
- backgroundColor: "#fff",
314
- pointBorderColor: colors[i % 15 + 1],
315
- borderColor: colors[i % 15 + 1],
316
- pointBackgroundColor: colors[i % 15 + 1],
317
- borderWidth: 2,
318
- }
319
- end
320
- }
321
- end
322
-
323
- def year_follow_count_chart(dates, names, x_business_ids, colors)
324
- {
325
- labels: dates,
326
- datasets: names.map.with_index do |name, i|
327
- {
328
- # hidden: i < 3 ? false : true,
329
- label: name[0],
330
- hidden: name[2] == 11,
331
- data: dates.map { |d| @follow_up = FollowUp.where(staff_id: name[1]).where("created_at >= ? AND created_at <= ?", "#{d}-01-01", "#{d}-12-31").where.not(business: x_business_ids).count },
332
- backgroundColor: "#fff",
333
- pointBorderColor: colors[i % 15 + 1],
334
- borderColor: colors[i % 15 + 1],
335
- pointBackgroundColor: colors[i % 15 + 1],
336
- borderWidth: 2
337
- }
338
- end
339
- }
340
- end
341
350
 
342
351
  def month_sale_chart(dates, names, colors, x, stage_ids, s_stage_ids, count_type)
343
352
  {
@@ -1,4 +1,6 @@
1
1
  module EducodeSales
2
2
  class SaleTrend < ApplicationRecord
3
+
4
+ COLORS = ['#44D7B6', '#4CACFF', '#F7B500', '#FF0000', '#FF7F00', '#FFFF00', '#00FF00', '#00FFFF', '#0000FF', '#8B00FF', 'rgba(255, 99, 132, 1)', 'rgba(54, 162, 235, 1)', 'rgba(255, 206, 86, 1)', 'rgba(75, 192, 192, 1)', 'rgba(153, 102, 255, 1)', 'rgba(255, 159, 64, 1)', 'rgba(255, 0, 250, 1)']
3
5
  end
4
6
  end