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, #render_failure, #render_success

Instance Method Details

#add_keysObject



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

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.teacher = teacher
  else
    # 从头歌平台选择的用户
    user = User.find(names[1])
    teacher = Teacher.new(staff: @current_admin,
      professional_title: params[:professional_title],
      job: params[:job],
      user_id: user.id,
      is_key: true,
      department_id: user.department_id,
      name: user.real_name
    )
    teacher.save
    key_peprson = KeyPerson.new(teacher_params)
    teacher_id = EducodeSales::Teacher.find_by(name: user.real_name, department_id: user.department_id).id
    key_peprson.teacher_id = teacher_id
  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



148
149
150
151
152
153
154
155
156
157
# File 'app/controllers/educode_sales/follow_ups_controller.rb', line 148

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

#createObject



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

def create
  load_business
  follow_up = @business.follow_ups.build(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
  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
  if follow_up.save
    if @business.last_follow_up.present?
      @business.last_follow_up.key_person.each do |d|
        key_person = d.dup
        key_person.follow_up_id = follow_up.id
        key_person.save
      end
      @business.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
    end
    @business.update(last_follow_up_id: follow_up.id)
    render_success
  else
    render_failure follow_up
  end
end

#delete_moneyObject



169
170
171
172
173
174
175
176
177
# File 'app/controllers/educode_sales/follow_ups_controller.rb', line 169

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



99
100
101
102
103
104
105
106
107
108
109
110
# File 'app/controllers/educode_sales/follow_ups_controller.rb', line 99

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
      business.update(last_follow_up: business.follow_ups.last, return_money: MoneyPlan.where(clazz: '实际回款', follow_up_id: business.follow_ups.last&.id).sum(:amount))
    end
    render_success
  else
    render_failure follow_up
  end
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
# 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
        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)
          business_ids = @businesses.pluck(:id)
          @follow_ups = FollowUp.where(business_id: business_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_ups = FollowUp.where(business_id: business_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][: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).joins("
          JOIN departments ON educode_sales_businesses.department_id = departments.id
          JOIN schools ON departments.school_id = schools.id
        ").where("province = ?", p)
      end
      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



143
144
145
146
# File 'app/controllers/educode_sales/follow_ups_controller.rb', line 143

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



134
135
136
137
138
139
140
141
# File 'app/controllers/educode_sales/follow_ups_controller.rb', line 134

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



112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'app/controllers/educode_sales/follow_ups_controller.rb', line 112

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
    render_success
  else
    render_failure follow_up
  end
end

#update_moneyObject



159
160
161
162
163
164
165
166
167
# File 'app/controllers/educode_sales/follow_ups_controller.rb', line 159

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