Class: EducodeSales::FollowUpsController

Inherits:
ApplicationController show all
Defined in:
app/controllers/educode_sales/follow_ups_controller.rb

Instance Method Summary collapse

Methods inherited from ApplicationController

#authenticate_admin, #authenticate_request, #current_user, #filter, #is_commissioner_above?, #paginate, #render_failure, #render_success, #subject_members, #subject_staffs, #subject_url

Methods included from ApplicationHelper

#add_businesses_score, #base_url, #collection_amount_score, #completion_rate, #current?, #disk_filename, #get_businesses_chart, #handled_data, #handled_time_data, #handled_time_data_accurate, #relative_path, #signed_amount_score, #storage_path, #url_to_avatar, #visits_score

Instance Method Details

#add_assignObject



379
380
381
382
383
384
385
386
# File 'app/controllers/educode_sales/follow_ups_controller.rb', line 379

def add_assign
  follow_up = FollowUp.find(params[:id])
  staff_ids = follow_up.watch_follow_ups.pluck(:staff_id)

  common = Common.find_by(clazz: 'staff_type', name: '销售')
  gon.assign_watch= Staff.joins(:user).where.not(job_type: common.id).where.not(role_id: 11).map { |d| {name: d.user.real_name, value: d.id, selected: staff_ids.include?(d.id)} }
  render layout: false
end

#add_keysObject



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
# File 'app/controllers/educode_sales/follow_ups_controller.rb', line 284

def add_keys
  follow_up = FollowUp.find(params[:id])
  if params[:name].blank?
    return render_failure '请从平台里选择教师'
  end

  names = params[:name].split("-")
  if names[0] == 't'
    # 从教师列表里选择的用户
    teacher = Teacher.find(names[1])
    key_peprson = KeyPerson.new(teacher_params)
    key_peprson.name = teacher.name
    key_peprson.teacher = teacher
  elsif names[0] == 'u'
    # 从头歌平台选择的用户
    user = User.find(names[1])
    key_peprson = KeyPerson.new(teacher_params)
    teacher = EducodeSales::Teacher.find_or_initialize_by(name: user.real_name, department_id: user.department_id, user_id: user.id)
    
    teacher.staff = @current_admin
    teacher.professional_title = params[:professional_title]
    teacher.job = params[:job]
    teacher.is_key = true
    teacher.save
    key_peprson.name = teacher.name
    key_peprson.teacher_id = teacher.id
  else
    key_peprson = KeyPerson.new(teacher_params)
  end

  follow_up.key_person << key_peprson
  if follow_up.save!
    render_success
  else
    render_failure follow_up
  end
 rescue ActiveRecord::RecordInvalid => e
   if e.message.include?("Teacher已经被使用") || e.message.include?('Key person是无效的')
    render_failure '该老师已在关键人列表中'
  else
    render_failure e.message
  end
end

#add_moneyObject



253
254
255
256
257
258
259
260
261
262
# File 'app/controllers/educode_sales/follow_ups_controller.rb', line 253

def add_money
  follow_up = FollowUp.find(params[:id])
  money_plan = follow_up.money_plans.build(date_at: params[:date_at], amount: params[:amount], clazz: params[:clazz])
  money_plan.staff = @current_admin
  if money_plan.save
    render_success
  else
    render_failure money_plan
  end
end

#add_tagObject



349
350
351
352
353
354
355
356
357
358
# File 'app/controllers/educode_sales/follow_ups_controller.rb', line 349

def add_tag
  followup = FollowUp.find(params[:id])
  gon.tags = []
  tags = Tag.all.pluck(:name)
  selecte_tags = followup.follow_up_tags.includes(:tag).map { |d| d.tag.name }
  tags.each do |d|
    gon.tags << {name: d, value: d, selected: selecte_tags.include?(d)}
  end
  render layout: false
end

#commentObject



334
335
336
337
338
# File 'app/controllers/educode_sales/follow_ups_controller.rb', line 334

def comment
  followup = FollowUp.find(params[:id])
  followup.update(comment: params[:comment])
  render_success
end

#createObject



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
# File 'app/controllers/educode_sales/follow_ups_controller.rb', line 98

def create
  load_business
  if @business.last_follow_up
    follow_up =  @business.last_follow_up.dup
    follow_up.assign_attributes(follow_up_params)
  else
    follow_up = @business.follow_ups.build(follow_up_params)
  end
  if params[:service_time].present?
    date = params[:service_time].split(" - ")
    follow_up.service_start_time = date[0]
    follow_up.service_end_time = date[1]
    follow_up.service_time_long = (date[1].to_date - date[0].to_date).to_i
  end
  follow_up.staff = @current_admin
  params[:assign_follow_up].each do |d|
    follow_up.assign_follow_ups.build(staff_id: d)
  end

  follow_up.profit_amount = follow_up.actual_amount - (follow_up.divide_amount.to_i) if follow_up.actual_amount
  last_follow_up = @business.last_follow_up
  if @business.clazz.present?
    follow_up.clazz_changed = @business.clazz_id != follow_up.clazz_id
    if follow_up.clazz_changed
      if @business.clazz.extras == 'a_class'
        @business.state_id = 3
      end
      if @business.clazz.extras.present? && follow_up.clazz.extras.present?
        clazz_changes = "#{@business.clazz.extras.split('_')[0]}-#{follow_up.clazz.extras.split('_')[0]}"
      else
        clazz_changes = "-"
      end
      business_clazz_change = EducodeSales::BusinessClazzChange.find_or_initialize_by(business_id: @business.id, clazz_changed: clazz_changes)
      business_clazz_change.save unless business_clazz_change.persisted?
    end
  end

  # 计划投标时间有变化且是当前周的话,添加当周/当月的销售计划
  current_week = Time.now.strftime('%W').to_i
  if follow_up.invitation_at&.strftime('%W').to_i == current_week
    if last_follow_up && last_follow_up.invitation_at&.strftime('%W').to_i == current_week
      current_week = nil
    end
  else
    current_week = nil
  end

  if follow_up.save
    if last_follow_up.present?
      last_follow_up.key_person.each do |d|
        key_person = d.dup
        key_person.follow_up_id = follow_up.id
        key_person.save
      end
      last_follow_up.money_plans.each do |d|
        money = d.dup
        money.staff = @current_admin
        money.follow_up_id = follow_up.id
        money.save
      end
      # 合同里签到日期全同步到最新跟进
      last_follow_up.contract_date_lists.update_all(follow_up_id: follow_up.id)
    end
    @business.update(last_follow_up_id: follow_up.id, clazz_id: follow_up.clazz_id, p_deploy_time: params[:deploy_time])

    if current_week.present?
      staff_manage_ids = @business&.last_follow_up&.assign_follow_ups.present? ? @business.last_follow_up.assign_follow_ups.pluck(:staff_id) : [@business.staff_id]
      common_id = EducodeSales::Common.find_by(clazz: '计划类型', name: '中标计划')&.id
      staff_manage_ids.each do |staff_id|
        EducodeSales::SalePlan.create(month: Time.now.beginning_of_month, weekly: current_week, content: "提醒:请补充中标计划内容!", business: @business, staff_id: staff_id, finish_rate: '0', common_id: common_id )
        EducodeSales::SalePlan.create(month: Time.now.beginning_of_month, content: "提醒:请补充中标计划内容!", business: @business, staff_id: staff_id, finish_rate: '0', common_id: common_id )
      end
    end

    # 更新对应的计划完成度
    if follow_up.bidded_date.present?
      staff_manage_ids = @business&.last_follow_up&.assign_follow_ups.present? ? @business.last_follow_up.assign_follow_ups.pluck(:staff_id) : [@business.staff_id]
      common_id = EducodeSales::Common.find_by(clazz: '计划类型', name: '中标计划')&.id
      if follow_up.bidded_date.strftime('%W') == Time.now.strftime('%W')
        EducodeSales::SalePlan.where(month: Time.now.beginning_of_month, weekly: Time.now.strftime('%W').to_i, business: @business, staff_id: staff_manage_ids, finish_rate: 0, common_id: common_id).update_all(finish_rate: 100)
      end
      if follow_up.bidded_date.strftime('%Y-%m') == Time.now.strftime('%Y-%m')
        EducodeSales::SalePlan.where(month: Time.now.beginning_of_month, weekly: nil, business: @business, staff_id: staff_manage_ids, finish_rate: 0, common_id: common_id).update_all(finish_rate: 100)
      end
    end


    # 增加o商机编号
    add_business_number
    update_department
    render_success
  else
    render_failure follow_up
  end
end

#delete_moneyObject



274
275
276
277
278
279
280
281
282
# File 'app/controllers/educode_sales/follow_ups_controller.rb', line 274

def delete_money
  follow_up = FollowUp.find_by(id: params[:id])
  money_plan = follow_up.money_plans.find(params[:plan_id])
  if money_plan.destroy
    render_success
  else
    render_failure money_plan
  end
end

#destroyObject



194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
# File 'app/controllers/educode_sales/follow_ups_controller.rb', line 194

def destroy
  follow_up = FollowUp.find(params[:id])
  business = follow_up.business
  if follow_up.soft_destroy(@current_admin.id)
    if follow_up.id == business.last_follow_up_id
      if business.follow_ups.last&.clazz_id
        business.clazz_id = business.follow_ups.last&.clazz_id
      end
      business.update(last_follow_up: business.follow_ups.last, return_money: MoneyPlan.where(clazz: '实际回款', follow_up_id: business.follow_ups.last&.id).sum(:amount))
      # 合同里签到日期全同步到最新跟进
      follow_up.contract_date_lists.update_all(follow_up_id: business.follow_ups.last.id) if business.follow_ups.last.present?
    end
    render_success
  else
    render_failure follow_up
  end
end

#fileObject



340
341
342
343
# File 'app/controllers/educode_sales/follow_ups_controller.rb', line 340

def file
  gon.folder = edu_setting('attachment_folder') + '/'
  render layout: false
end

#indexObject



6
7
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
# File 'app/controllers/educode_sales/follow_ups_controller.rb', line 6

def index
  authorize! :read, Business
  respond_to do |format|
    format.html do
    end
    format.json do
      if @current_admin.is_admin?
        @follow_ups = FollowUp.all
      else
        permissions = @current_admin.permissions.pluck(:name)
        area_business_ids = []
        if permissions.include?("专项管理商机")
          # 按客户类型查看商机下跟进记录
          school_tag_ids  = EducodeSales::StaffSchoolTag.where(staff_id: @current_admin.id).pluck :school_tag_id
          school_ids = SchoolTagMiddle.where(school_tag_id: school_tag_ids).pluck :school_id
          area_business_ids = Business.joins("JOIN departments ON educode_sales_businesses.department_id = departments.id").where("departments.school_id in (?)", school_ids).pluck(:id)
        end
       
        level = @current_admin.role.role_areas.find_by(clazz: '商机管理').level
        case level
        when '自己'
          if permissions.include?("区域管理商机")
            # 按负责区域查看商机下跟进记录
            school_ids = School.where(province: @current_admin.areas.pluck(:name)).pluck(:id)
            area_business_ids += Business.joins("JOIN departments ON educode_sales_businesses.department_id = departments.id").where("departments.school_id in (?)", school_ids).pluck(:id)
          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.where("educode_sales_businesses.staff_id = ? OR educode_sales_businesses.id in (?)", @current_admin.id, business_ids)
          business_ids = @businesses.pluck(:id)

          # 指定查看人
          follow_up_ids = EducodeSales::WatchFollowUp.where(staff_id: @current_admin.id).pluck(:follow_up_id)
          @follow_ups = FollowUp.where(business_id: business_ids + area_business_ids).or(FollowUp.where(id: follow_up_ids))
        when '区域'
          school_ids = School.where(province: @current_admin.areas.pluck(:name)).pluck(:id) + StaffSchool.where(staff_id: @current_admin.id).pluck(:school_id)
          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)
          business_ids = @businesses.pluck(:id)
          follow_up_ids = EducodeSales::WatchFollowUp.where(staff_id: @current_admin.id).pluck(:follow_up_id)
          @follow_ups = FollowUp.where(business_id: business_ids + area_business_ids).or(FollowUp.where(id: follow_up_ids))
        else
          @follow_ups = FollowUp.all
        end
      end
      if params[:q].present? && params[:q][:name].present?
        @follow_ups = @follow_ups.joins(:business).where("educode_sales_businesses.name LIKE ?", "%#{params[:q][:name]}%")
      end
      if params[:q].present? && params[:q][:follows_date].present?
        date = params[:q][:follows_date].split(" - ")
        @follow_ups = @follow_ups.where("educode_sales_follow_ups.created_at > ? AND educode_sales_follow_ups.created_at < ?", date[0], date[1] + '23:59:59')
      end
      if params[:q].present? && params[:q][:property].present?
        # 客户类型
        @follow_ups = @follow_ups.joins(business: [department: [school: :school_tags]]).where("school_tags.id = ?", params[:q][:property])
      end
      if params[:q].present? && params[:q][:staff_id].present?
        @follow_ups = @follow_ups.where(staff_id: params[:q][:staff_id])
      end
      if params[:q].present? && params[:q][:department].present?
        departments_ids = Department.joins(:school).where("schools.name like ?", "%#{params[:q][:department]}%").pluck(:id)
        @follow_ups = @follow_ups.joins(business: :department).where("departments.id in (?)", departments_ids)
      end
      if params[:q].present? && params[:q][:area].present?
        p = EducodeSales::Common.find(params[:q][:area]).name
        @follow_ups = @follow_ups.joins(business: :department).joins("
          JOIN schools ON departments.school_id = schools.id
        ").where("province = ?", p)
      end

      if params[:q].present? && params[:q][:description].present?
        @follow_ups = @follow_ups.joins(:business).joins("JOIN educode_sales_businesses ON educode_sales_businesses.id = educode_sales_follow_ups.business_id
                ").where("educode_sales_follow_ups.description LIKE ?" ,"%#{params[:q][:description]}%")

      end

      if params[:q].present? && params[:q][:staff].present?
        @follow_ups = @follow_ups.joins(business: [staff: :user]).where("CONCAT(users.lastname, users.firstname) like ?", "%#{params[:q][:staff]}%")
      end

      @follow_ups = @follow_ups.includes(:staff, :clazz, :stage, key_person: :teacher, business: [department: :school])

      if params[:sort].present? && params[:sort][:field]
        @follow_ups = @follow_ups.order("#{params[:sort][:field]} #{params[:sort][:order]}")
      else
        @follow_ups = @follow_ups.order("educode_sales_follow_ups.created_at desc")
      end
      @follow_ups = @follow_ups.page(params[:page]).per(params[:limit])
    end
end
end

#money_plansObject



248
249
250
251
# File 'app/controllers/educode_sales/follow_ups_controller.rb', line 248

def money_plans
  follow_up = FollowUp.find_by(id: params[:id])
  @money_plans = follow_up.money_plans.page(params[:page]).per(params[:limit])
end

#teachersObject



239
240
241
242
243
244
245
246
# File 'app/controllers/educode_sales/follow_ups_controller.rb', line 239

def teachers
  follow_up = FollowUp.find_by(id: params[:id])
  if follow_up.present?
    @teachers = follow_up.key_person.includes(:teacher).page(params[:page]).per(params[:limit])
  else
    @teachers = FollowUp.none
  end
end

#updateObject



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
# File 'app/controllers/educode_sales/follow_ups_controller.rb', line 212

def update
  follow_up = FollowUp.find(params[:id])
  follow_up.assign_attributes(follow_up_params)
  if params[:service_time].present?
    date = params[:service_time].split(" - ")
    follow_up.service_start_time = date[0]
    follow_up.service_end_time = date[1]
    follow_up.service_time_long = (date[1].to_date - date[0].to_date).to_i
  end
  assign_follow_ups = []
  params[:assign_follow_up].each do |d|
    assign_follow_ups << follow_up.assign_follow_ups.find_or_initialize_by(staff_id: d)
  end
  follow_up.assign_follow_ups = assign_follow_ups
  follow_up.profit_amount = follow_up.actual_amount - (follow_up.divide_amount.to_i) * 0.01 if follow_up.actual_amount
  if follow_up.save
    # 增加o类商机
    @business = follow_up.business
    @business.update(clazz_id: follow_up.clazz_id, p_deploy_time: params[:deploy_time])
    add_business_number
    update_department
    render_success
  else
    render_failure follow_up
  end
end

#update_adviseObject



328
329
330
331
332
# File 'app/controllers/educode_sales/follow_ups_controller.rb', line 328

def update_advise
  followup = FollowUp.find(params[:id])
  followup.update(advise: params[:content])
  render_success
end

#update_assignObject



388
389
390
391
392
393
394
395
396
397
# File 'app/controllers/educode_sales/follow_ups_controller.rb', line 388

def update_assign
  follow_up = FollowUp.find(params[:id])
  staffs = []
  params[:staff_ids].split(",").each do |staff_id|
    staffs << WatchFollowUp.find_or_initialize_by(staff_id: staff_id, follow_up_id: follow_up.id)
  end
  follow_up.watch_follow_ups = staffs
  follow_up.save
  render json: {success: true, tags: staffs.map { |d| d.staff.user.real_name }}
end

#update_moneyObject



264
265
266
267
268
269
270
271
272
# File 'app/controllers/educode_sales/follow_ups_controller.rb', line 264

def update_money
  follow_up = FollowUp.find_by(id: params[:id])
  money_plan = follow_up.money_plans.find(params[:plan_id])
  if money_plan.update(date_at: params[:date_at], amount: params[:amount], clazz: params[:clazz])
    render_success
  else
    render_failure money_plan
  end
end

#update_tagsObject



360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
# File 'app/controllers/educode_sales/follow_ups_controller.rb', line 360

def update_tags
  follow_up = FollowUp.find(params[:id])
  tags = []
  tags_name = []
  params[:tags].split(",").uniq.each do |d|
    tag = Tag.find_or_initialize_by(name: d) do |d|
      d.staff = @current_admin
    end
    tags_name << tag.name
    tag.save unless tag.persisted?
    t = FollowUpTag.find_or_initialize_by(tag_id: tag.id, follow_up_id: follow_up.id)
    t.staff = @current_admin
    tags << t
  end
  follow_up.follow_up_tags = tags
  follow_up.save
  render json: {success: true, tags: tags_name}
end

#upload_fileObject



345
346
347
# File 'app/controllers/educode_sales/follow_ups_controller.rb', line 345

def upload_file
  render layout: false
end