8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
|
# File 'app/controllers/educode_sales/projects_controller.rb', line 8
def index
authorize! :contract_business, Business
respond_to do |format|
format.html do
common = Common.find_by(clazz: 'staff_type', name: '销售')
@staffs = Staff.joins(:user).where(job_type: common.id).where.not(role_id: 11).map { |d| [d.user.real_name, d.id] }
gon.staffs = Staff.joins(:user).where(job_type: common.id).where.not(role_id: 11).map { |d| { name: d.user.real_name, value: d.id } }
@more = true
gon. = []
gon. = []
gon.place = params[:place_id].present? ? [{ value: params[:place_id], name: EducodeSales::Place.find(params[:place_id]).name }] : []
bussiness_type = []
if params[:type]
bussiness_type = EducodeSales::Business.include_types(params[:type]) || []
end
business_step = []
if params[:step]
business_step = EducodeSales::Business.include_steps(params[:step]) || []
end
if params[:business_year].present? && params[:business_year] != '全部'
gon.business_year = params[:business_year]
end
if params[:bidded_date_year].present? && params[:bidded_date_year] != '全部'
if params[:step] == '应收款'
gon.bidded_date = "2015-01-01" + " - " + Time.new(params[:bidded_date_year]).end_of_year.strftime("%Y-%m-%d")
else
gon.bidded_date = Time.new(params[:bidded_date_year]).strftime("%Y-%m-%d") + " - " + Time.new(params[:bidded_date_year]).end_of_year.strftime("%Y-%m-%d")
end
end
if params[:signed_date_year].present? && params[:signed_date_year] != '全部'
gon.signed_date = Time.new(params[:signed_date_year]).strftime("%Y-%m-%d") + " - " + Time.new(params[:signed_date_year]).end_of_year.strftime("%Y-%m-%d")
end
if params[:date_at].present? && params[:date_at] != '全部'
gon.date_at = Time.new(params[:date_at]).strftime("%Y-%m-%d") + " - " + Time.new(params[:date_at]).end_of_year.strftime("%Y-%m-%d")
end
gon.business_type = Common.where(clazz: 'business_type').map do |d|
{ value: d.id, name: d.name, selected: bussiness_type.include?(d.) }
end
gon.business_step = Common.where(clazz: 'business_step', name: ['已中标', '已签单', '已验收', '回款中', '服务中', '已结束']).order("position").map do |d|
{ value: d.id, name: d.name, selected: business_step.include?(d.name) }
end
filter = Filter.find_or_create_by(staff_id: @current_admin.id, clazz: "businesses_list")
if filter..present?
if filter.['area'].nil?
filter.['area'] = 1
filter.['property'] = 1
filter.['sale_way'] = 1
end
filter.save
gon.filter = filter.
else
gon.filter = { 'area': 1, 'property': 1, 'sale_way': 1 }
end
end
format.json do
if @current_admin.is_admin?
@businesses = Business
else
level = @current_admin.role.role_areas.find_by(clazz: '商机管理').level
case level
when '自己'
business_ids = Business.joins(last_follow_up: :assign_follow_ups).where("educode_sales_assign_follow_ups.staff_id = ?", @current_admin.id).pluck(:id)
@businesses = Business.where("educode_sales_businesses.staff_id = ? OR educode_sales_businesses.id in (?)", @current_admin.id, business_ids)
when '区域'
other_staff_school_id = EducodeSales::StaffSchool.where.not(staff_id: @current_admin.id).where("school_id IN (SELECT school_id FROM educode_sales_staff_schools WHERE staff_id = #{@current_admin.id}) IS NOT TRUE").distinct.pluck :school_id
school_ids = School.where(province: @current_admin.areas.pluck(:name)).pluck(:id) + StaffSchool.where(staff_id: @current_admin.id).pluck(:school_id) - other_staff_school_id
if can?(:special, EducodeSales::Business)
school_tag_ids = @current_admin.staff_school_tags.pluck(:school_tag_id)
tag_school_ids = SchoolTagMiddle.where(school_tag_id: school_tag_ids).pluck(:school_id)
school_ids += tag_school_ids
end
business_ids = Business.joins(last_follow_up: :assign_follow_ups).where("educode_sales_assign_follow_ups.staff_id = ?", @current_admin.id).pluck(:id)
@businesses = Business.joins("JOIN departments ON educode_sales_businesses.department_id = departments.id").where("departments.school_id in (?) OR educode_sales_businesses.staff_id = #{@current_admin.id} OR educode_sales_businesses.id in (?)", school_ids, business_ids)
else
@businesses = Business
end
end
@businesses =
if params[:is_deleted].to_s.present?
@businesses.where.not(p_deleted_at: nil)
else
@businesses.where(p_deleted_at: nil)
end
@is_deleted = params[:is_deleted].to_s.present?
gon.is_deleted = @is_deleted ? 1 : 0
if params[:q].present? && params[:q][:name].present?
@year = params[:q][:name].split("-")[1].present? ? params[:q][:name].split("-")[1] : ''
end
if params[:q] && params[:q][:signed_date].present?
date = params[:q][:signed_date].split(" - ")
@businesses = @businesses.joins("
JOIN educode_sales_follow_ups ON educode_sales_businesses.last_follow_up_id = educode_sales_follow_ups.id
").where("educode_sales_follow_ups.signed_date >= ? AND educode_sales_follow_ups.signed_date <= ?", date[0], date[1])
end
if params[:q] && params[:q][:date_at].present?
date = params[:q][:date_at].split(" - ")
x_id = Common.find_by(extras: EducodeSales::Common::XTYPE)&.id
@businesses = @businesses.joins(follow_ups: :money_plans).where("educode_sales_follow_ups.clazz_id != ?", x_id).where.not("educode_sales_money_plans.clazz!= 1").where("educode_sales_money_plans.date_at >= ? AND educode_sales_money_plans.date_at <= ? ", date[0] + " 00:00:00", date[1] + " 23:59:59").distinct
end
if params[:q] && params[:q][:bidded_date].present?
date = params[:q][:bidded_date].split(" - ")
@businesses = @businesses.joins("
JOIN educode_sales_follow_ups ON educode_sales_businesses.last_follow_up_id = educode_sales_follow_ups.id
").where("educode_sales_follow_ups.bidded_date >= ? AND educode_sales_follow_ups.bidded_date <= ?", date[0], date[1])
end
if params[:q].present? && params[:q][:all].present?
@businesses = @businesses
end
if params[:q].present? && params[:q][:name].present?
@businesses = @businesses.where("educode_sales_businesses.name like ?", "%#{params[:q][:name]}%")
end
if params[:q].present? && params[:q][:p_stage].present?
@businesses = @businesses.where(p_stage: "#{params[:q][:p_stage].to_s.strip}")
end
if params[:q].present? && params[:q][:p_staff_id].present?
@businesses = @businesses.where(p_staff_id: params[:q][:p_staff_id])
end
if params[:q].present? && params[:q][:p_sale_staff_id].present?
@businesses = @businesses.where(p_sale_staff_id: params[:q][:p_sale_staff_id])
end
if params[:q].present? && params[:q][:p_special].present?
@businesses = @businesses.where(p_special: "#{params[:q][:p_special]}")
end
if params[:q].present? && params[:q][:p_difficulty].present?
@businesses = @businesses.where(p_difficulty: "#{params[:q][:p_difficulty]}")
end
if params[:q].present? && params[:q][:p_state].present?
p_state = params[:q][:p_state].to_i
late_ids = Business.joins("JOIN educode_sales_follow_ups ON educode_sales_businesses.last_follow_up_id = educode_sales_follow_ups.id JOIN educode_sales_commons on educode_sales_commons.id = educode_sales_follow_ups.stage_id")
.where("educode_sales_follow_ups.bidded_date is not null")
.where("NOW() > DATE_ADD(educode_sales_follow_ups.bidded_date, INTERVAL 1 MONTH)")
.where("educode_sales_businesses.p_stage is null")
.where("educode_sales_commons.name = '已中标'")
.or(Business.joins("JOIN educode_sales_follow_ups ON educode_sales_businesses.last_follow_up_id = educode_sales_follow_ups.id JOIN educode_sales_commons on educode_sales_commons.id = educode_sales_follow_ups.stage_id")
.where("NOW() > educode_sales_businesses.p_pre_money_time")
.where("educode_sales_businesses.p_pre_money_time is not null")
.where("educode_sales_businesses.p_actual_money_time is null")
).or(Business.joins("JOIN educode_sales_follow_ups ON educode_sales_businesses.last_follow_up_id = educode_sales_follow_ups.id JOIN educode_sales_commons on educode_sales_commons.id = educode_sales_follow_ups.stage_id")
.where("NOW() > educode_sales_businesses.p_pre_accept_time")
.where("educode_sales_businesses.p_pre_accept_time is not null")
.where("educode_sales_businesses.p_accept_time is null")).ids
if p_state == 0 @businesses = @businesses.where.not(id: late_ids)
else
@businesses = @businesses.where(id: late_ids)
end
end
if params[:q].present? && params[:q][:department].present?
departments_ids = Department.joins(:school).where("schools.name like ?", "%#{params[:q][:department]}%").pluck(:id)
@businesses = @businesses.joins(:department).where("departments.id in (?)", departments_ids)
end
if params[:q].present? && params[:q][:staff_id].present?
part_a_ids = @businesses.where("educode_sales_businesses.last_follow_up_id is null AND educode_sales_businesses.staff_id = ?", params[:q][:staff_id]).ids
part_b_ids = @businesses.where("educode_sales_businesses.last_follow_up_id is not null").joins("
JOIN educode_sales_follow_ups ON educode_sales_businesses.last_follow_up_id = educode_sales_follow_ups.id
").where("educode_sales_follow_ups.staff_id = ?", params[:q][:staff_id]).ids
business_ids = part_a_ids + part_b_ids
if params[:q][:clazz] == 'delete_list'
@businesses = EducodeSales::Business.unscoped.where(id: business_ids)
else
@businesses = EducodeSales::Business.where(id: business_ids)
end
end
if params[:q].present? && params[:q][:business_type].present?
@businesses = @businesses.where("educode_sales_businesses.clazz_id in (?)", params[:q][:business_type].split(",").map(&:to_i))
end
contract_ids = Common.where(clazz: '商机阶段', name: ['已中标', '已签单', '已验收', '回款中', '服务中', '已结束']).pluck(:id)
@businesses = @businesses.joins("
JOIN educode_sales_follow_ups ON educode_sales_businesses.last_follow_up_id = educode_sales_follow_ups.id
").where("educode_sales_follow_ups.stage_id in (?)", contract_ids)
if params[:q].present? && params[:q][:business_step].present?
@businesses = @businesses.joins("
JOIN educode_sales_follow_ups ON educode_sales_businesses.last_follow_up_id = educode_sales_follow_ups.id
").where("educode_sales_follow_ups.stage_id in (?)", params[:q][:business_step].split(",").map(&:to_i))
end
if params[:q].present? && params[:q][:place_id].present?
@businesses = @businesses.joins("
JOIN educode_sales_follow_ups ON educode_sales_businesses.last_follow_up_id = educode_sales_follow_ups.id
").where("educode_sales_follow_ups.place_id = ?", params[:q][:place_id])
end
if params[:q].present? && params[:q][:business_year].present?
@businesses = @businesses.joins("
JOIN educode_sales_follow_ups ON educode_sales_businesses.last_follow_up_id = educode_sales_follow_ups.id
").where("educode_sales_follow_ups.year = ?", params[:q][:business_year])
end
if params[:q].present? && params[:q][:year].present?
@businesses = @businesses.joins("
JOIN educode_sales_follow_ups ON educode_sales_businesses.last_follow_up_id = educode_sales_follow_ups.id
").where("educode_sales_follow_ups.year <= ?", params[:q][:year])
end
if params[:q].present? && params[:q][:o_business_deployment].present?
@businesses = @businesses.joins("
JOIN educode_sales_follow_ups ON educode_sales_businesses.last_follow_up_id = educode_sales_follow_ups.id
").where("educode_sales_follow_ups.o_business_deployment = ?", params[:q][:o_business_deployment])
end
if params[:q].present? && params[:q][:area].present?
p = EducodeSales::Common.find(params[:q][:area]).name
if @current_admin.is_admin?
@businesses = @businesses.joins("
JOIN departments ON educode_sales_businesses.department_id = departments.id
JOIN schools ON departments.school_id = schools.id
").where("province = ?", p)
else
level = @current_admin.role.role_areas.find_by(clazz: '商机管理').level
if level == "区域"
@businesses = @businesses.joins("
JOIN schools ON departments.school_id = schools.id
").where("province = ?", p)
else
@businesses = @businesses.joins("
JOIN departments ON educode_sales_businesses.department_id = departments.id
JOIN schools ON departments.school_id = schools.id
").where("province = ?", p)
end
end
end
if params[:q].present? && params[:q][:date].present?
date = params[:q][:date].split(" - ")
@businesses = @businesses.where("educode_sales_businesses.created_at > ? AND educode_sales_businesses.created_at < ?", date[0], date[1] + '23:59:59')
end
if params[:q].present? && params[:q][:search_p_actual_money_time].present?
date = params[:q][:search_p_actual_money_time].split(" - ")
@businesses = @businesses.where("educode_sales_businesses.p_actual_money_time > ? AND educode_sales_businesses.p_actual_money_time < ?", date[0], date[1] + '23:59:59')
end
if params[:q].present? && params[:q][:invitation_at].present?
date = params[:q][:invitation_at].split(" - ")
@businesses = @businesses.joins("
JOIN educode_sales_follow_ups ON educode_sales_businesses.last_follow_up_id = educode_sales_follow_ups.id
").where("educode_sales_follow_ups.invitation_at > ? AND educode_sales_follow_ups.invitation_at < ?", date[0] + '00:00:00', date[1] + '23:59:59')
end
if params[:q].present? && params[:q][:number].present?
@businesses = @businesses.where("number like :data", data: "%#{params[:q][:number].strip}%")
end
if params[:q].present? && params[:q][:clazz].present?
if params[:q][:clazz] == 'followes'
my_ids = BusinessRelationShip.where(user_id: @current_admin.user_id).pluck(:business_id)
@businesses = @businesses.where(id: my_ids)
elsif params[:q][:clazz] == 'me'
ids = AssignFollowUp.joins("JOIN educode_sales_follow_ups ON educode_sales_follow_ups.id = educode_sales_assign_follow_ups.follow_up_id").where(staff_id: @current_admin.id).pluck(:business_id)
@businesses = @businesses.where("educode_sales_businesses.id in (?) or educode_sales_businesses.staff_id = ?", ids, @current_admin.id)
end
end
if params[:q][:clazz_changes].present?
clazz_changes = EducodeSales::BusinessClazzChange.clazz_changes_value[params[:q][:clazz_changes].to_s]
@businesses = @businesses.joins("JOIN educode_sales_business_clazz_changes ON educode_sales_business_clazz_changes.business_id = educode_sales_businesses.id").where("educode_sales_business_clazz_changes.clazz_changed in (?)", clazz_changes)
end
if params[:sort].present? && params[:sort][:field]
if params[:sort][:field] == "service_end_time"
@businesses = @businesses.order("service_time_long #{params[:sort][:order]}")
else
@businesses = @businesses.order("#{params[:sort][:field]} #{params[:sort][:order]}")
end
else
@businesses = @businesses.order("educode_sales_businesses.created_at desc")
end
@business_count = @businesses.distinct.count
@businesses = @businesses.select("
distinct
educode_sales_businesses.*,
last_follow.invitation_at,
last_follow.budget_amount,
last_follow.o_business_deployment,
last_follow.service_time_long,
last_follow.service_end_time,
last_follow.service_start_time,
last_follow.reception_at,
last_follow.bidded_date,
last_follow.signed_date,
last_follow.year,
last_follow.created_at as latest_time,
last_follow.actual_amount,
last_follow.divide_rate,
last_follow.divide_amount,
(last_follow.total_amount - last_follow.actual_amount) as divide_money,
(last_follow.actual_amount - educode_sales_businesses.return_money) as wait_return_money,
last_follow.total_amount,
total_follow_ups.follow_ups_counts
").joins("
LEFT JOIN educode_sales_follow_ups AS last_follow ON educode_sales_businesses.last_follow_up_id = last_follow.id AND last_follow.deleted_at IS NULL
LEFT JOIN (
SELECT COUNT(*) AS follow_ups_counts, educode_sales_follow_ups.business_id
FROM educode_sales_follow_ups
WHERE educode_sales_follow_ups.deleted_at IS NULL
GROUP BY educode_sales_follow_ups.business_id
) AS total_follow_ups ON educode_sales_businesses.id = total_follow_ups.business_id
").includes(:users, last_follow_up: [:clazz, :assign_follow_ups]).page(params[:page]).per(params[:limit])
end
end
end
|