Class: EducodeSales::ActivitiesController

Inherits:
ApplicationController show all
Defined in:
app/controllers/educode_sales/activities_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_adviseObject



283
284
285
286
287
# File 'app/controllers/educode_sales/activities_controller.rb', line 283

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

#createObject



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

def create
  activity = @current_admin.activities.new(activity_params)
  activity.sales_id = @current_admin.id
  if activity.save
    params[:staff_manage_id].each do |d|
      EducodeSales::ActivityStaff.create(staff_id: d, clazz_id: 'manage', activity_id: activity.id)
    end

    params[:staff_assist_id].each do |d|
      if d[0].to_i > 0
        u = User.find(d[0].to_i)
        EducodeSales::ActivityStaff.create(name: u.real_name, clazz_id: 'assist', user_id: d[0].to_i, activity_id: activity.id)
      else
        EducodeSales::ActivityStaff.create(name: d[1], clazz_id: 'assist', activity_id: activity.id)
      end
    end
    params[:invitation_ids].each do |d|
      if d[0].to_i > 0
        u = User.find(d[0].to_i)
        EducodeSales::ActivityStaff.create(name: u.real_name, clazz_id: 'invitation', user_id: d[0].to_i, activity_id: activity.id)
      else
        EducodeSales::ActivityStaff.create(name: d[1], clazz_id: 'invitation', activity_id: activity.id)
      end
    end
    render_success
  else
    render_failure activity
  end
end

#destroyObject



211
212
213
214
215
216
217
218
219
220
221
222
# File 'app/controllers/educode_sales/activities_controller.rb', line 211

def destroy
  activity = Activity.find(params[:id])
  if activity.attachment
    file_path = File.join(Rails.root, "public", "files", 'educode_sales', activity.attachment.disk_filename)
    File.delete(file_path) if File.exist?(file_path)
    activity.attachment.destroy
  end
  activity.destroy
  render_success
rescue ActiveRecord::DeleteRestrictionError => e
  render_failure '该活动已有数据产生,暂不能删除'
end

#editObject



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'app/controllers/educode_sales/activities_controller.rb', line 57

def edit
  @activity = Activity.find(params[:id])
  @clazz = ['全国会议', '区域会议', '单校会议', '国赛', '省赛', '夏令营']
  manage_ids = @activity.manages.pluck(:staff_id)
  assist_ids = @activity.assists.pluck(:staff_id)
  gon.staff_manage = Staff.all.map { |d| {name: d.user&.real_name, value: d.id, selected: manage_ids.include?(d.id)}}
  gon.assistss = @activity.assists.map { |d| {name: d.name, value: d.user_id || d.name, selected: true}}

  gon.invitation_list = @activity.invitations.map { |d| {name: d.name, value: d.user_id || d.name, selected: true}}

  sale_id = @activity.sales_id || @activity.staff_id

  gon.sales_manage = Staff.all.map { |d| {name: d.user&.real_name, value: d.id, selected: d.id == sale_id }}
  render layout: false
end

#filesObject



290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
# File 'app/controllers/educode_sales/activities_controller.rb', line 290

def files
  respond_to do |format|
    format.html do
      render layout: false
    end
    format.json do
      activity = Activity.find(params[:id])
      @files = activity.attachments
      if params[:sort].present? && params[:sort][:field]
        @files = @files.order("#{params[:sort][:field]} #{params[:sort][:order]}")
      else
        @files = @files.order("created_on desc")
      end
      @files = @files.page(params[:page]).per(params[:limit])
    end
  end
end

#follow_upObject



249
250
251
252
253
254
255
256
257
258
259
260
# File 'app/controllers/educode_sales/activities_controller.rb', line 249

def follow_up
  activity = Activity.find(params[:id])
  follow_up =  EducodeSales::ActivityFollowUp.new(activity_id: activity.id, staff_id: @current_admin.id, description: params[:description], info_id: params[:info_id])
  if follow_up.save
    activity.last_follow_up_id = follow_up.id
    activity.save
    render_success
  else
    render_failure follow_up
  end
  
end

#follow_upsObject



262
263
264
265
266
267
268
269
270
271
# File 'app/controllers/educode_sales/activities_controller.rb', line 262

def follow_ups
  @data = ActivityFollowUp.includes(:staff, activity: :staff)

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

#importObject



103
104
105
# File 'app/controllers/educode_sales/activities_controller.rb', line 103

def import
  render layout: false
end

#import_dataObject



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

def import_data
  doc = SimpleXlsxReader.open(params[:file])

  doc.sheets[0].rows.each_with_index do |d, i|
    next if i == 0 || d[1].blank?
    activity = @current_admin.activities.new({
      name: d[0],
      state_id: d[1],
      clazz_id: d[2],
      sponsor: d[5],
      linkman: d[6],
      phone: d[7],
      scale: d[8],
      address: d[9],
      meeting_form: d[10],
      start_at: d[13],
      days: d[14]
    })
    activity.save
    
    if d[4].present?
      # 生态经理
      d[4].gsub(/、|,|,/, "_").split("_").each do |c|
        staff = EducodeSales::Staff.joins(:user).where("users.lastname =?", c).first
        EducodeSales::ActivityStaff.create(staff_id: staff.id, clazz_id: 'manage', activity_id: activity.id) if staff
      end
    end

    if d[11].present?
      # 协助人员
      d[11].gsub(/、|,|,/, "_").split("_").each do |c|
        u = User.find_by(lastname: c)
        if u
          EducodeSales::ActivityStaff.create(name: u.real_name, clazz_id: 'assist', user_id: u.id, activity_id: activity.id)
        else
          EducodeSales::ActivityStaff.create(name: c, clazz_id: 'assist', activity_id: activity.id)
        end
      end
    end

    if d[12].present?
      # 邀请专家
      d[12].gsub(/、|,|,/, "_").split("_").each do |c|
        u = User.find_by(lastname: c)
        if u
          EducodeSales::ActivityStaff.create(name: u.real_name, clazz_id: 'invitation', user_id: u.id, activity_id: activity.id)
        else
          EducodeSales::ActivityStaff.create(name: c, clazz_id: 'invitation', activity_id: activity.id)
        end
      end
    end
  end

  render_success
end

#indexObject



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

def index
  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]}

    role = EducodeSales::Role.find_by(name: '生态经理')
    @staff_manage = Staff.joins(:user).where(role_id: role.id).map { |d| [d.user.real_name, d.id]}
    end
    format.json do
      @activities = Activity
      if params[:q].present?
        if params[:q][:staff_id].present?
          @activities = @activities.where(sales_id: params[:q][:staff_id])
        end
        if params[:q][:clazz_id].present?
          @activities = @activities.where(clazz_id: params[:q][:clazz_id])
        end

        if params[:q][:state_id].present?
          @activities = @activities.where(state_id: params[:q][:state_id])
        end
        
        if params[:q][:name].present?
          @activities = @activities.where("educode_sales_activities.name like ?", "%#{params[:q][:name]}%")
        end
        if params[:q][:manage].present?
          @activities = @activities.joins("JOIN educode_sales_activity_staffs AS m ON m.activity_id = educode_sales_activities.id AND m.clazz_id = 0").where("m.staff_id = ?", params[:q][:manage].to_i)
        end
        if params[:q][:assists].present?
          @activities = @activities.joins("JOIN educode_sales_activity_staffs AS a ON a.activity_id = educode_sales_activities.id AND a.clazz_id = 1").where("a.staff_id = ?", params[:q][:assists].to_i)
        end
        if params[:q][:expert].present?
          @activities = @activities.joins("JOIN educode_sales_activity_staffs AS e ON e.activity_id = educode_sales_activities.id AND e.clazz_id = 2").where("e.name like ?", "%#{params[:q][:expert]}%")
        end
      end
      if params[:sort].present? && params[:sort][:field]
        @activities = @activities.order("#{params[:sort][:field]} #{params[:sort][:order]}")
      else
        @activities = @activities.order("created_at desc")
      end
      @activities = @activities.distinct.page(params[:page]).per(params[:limit])
    end
  end
end

#newObject



51
52
53
54
55
# File 'app/controllers/educode_sales/activities_controller.rb', line 51

def new
  @clazz = ['全国会议', '区域会议', '单校会议', '国赛', '省赛', '夏令营']
  gon.staff_manage = Staff.all.map { |d| {name: d.user&.real_name, value: d.id}}
  render layout: false
end

#new_follow_upObject



244
245
246
247
# File 'app/controllers/educode_sales/activities_controller.rb', line 244

def new_follow_up
  @activity = Activity.find(params[:id])
  render layout: false
end

#show_schoolsObject



232
233
234
235
236
237
238
239
240
241
242
# File 'app/controllers/educode_sales/activities_controller.rb', line 232

def show_schools
  respond_to do |format|
    format.html do
      render layout: false
    end
    format.json do
      @data = EducodeSales::ActivityTeacher.joins(teacher: [department: :school]).where(activity_id: params[:id]).select("count(teacher_id) AS teachers_count, departments.name AS department, schools.name AS school, school_id").group("school_id")
      @data = @data.distinct.page(params[:page]).per(params[:limit])
    end
  end
end

#show_teachersObject



224
225
226
# File 'app/controllers/educode_sales/activities_controller.rb', line 224

def show_teachers
  render layout: false
end

#updateObject



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

def update
  activity = Activity.find(params[:id])
  activity.assign_attributes(
    state_id: params[:state_id],
    name: params[:name], 
    start_at: params[:start_at], days: params[:days],
    sponsor: params[:sponsor],
    linkman: params[:linkman],
    phone: params[:phone],
    scale: params[:scale],
    address: params[:address],
    meeting_form: params[:meeting_form],
    sales_id: params[:sales_id] || @current_admin.id
   )
  manages = []
  assists = []
  invitations = []
  params[:staff_manage_id].each do |d|
    manages << EducodeSales::ActivityStaff.find_or_initialize_by(staff_id: d, clazz_id: 'manage', activity_id: activity.id)
  end
   
  params[:staff_assist_id].each do |d|
    if d[0].to_i > 0
      u = User.find(d[0].to_i)
      assists << EducodeSales::ActivityStaff.find_or_initialize_by(name: u.real_name, clazz_id: 'assist', user_id: d[0].to_i, activity_id: activity.id)
    else
      assists << EducodeSales::ActivityStaff.find_or_initialize_by(name: d[1], clazz_id: 'assist', activity_id: activity.id)
    end
  end
  params[:invitation_ids].each do |d|
    if d[0].to_i > 0
      u = User.find(d[0].to_i)
      invitations << EducodeSales::ActivityStaff.find_or_initialize_by(name: u.real_name, clazz_id: 'invitation', user_id: d[0].to_i, activity_id: activity.id)
    else
      invitations << EducodeSales::ActivityStaff.find_or_initialize_by(name: d[1], clazz_id: 'invitation', activity_id: activity.id)
    end
  end
  activity.manages = manages
  activity.assists = assists
  activity.invitations = invitations
  
  if activity.save
    render_success
  else
    render_failure activity
  end
end

#update_adviseObject



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

def update_advise
  activity = Activity.find(params[:id])
  if activity.last_follow_up
    activity.last_follow_up.update(advise: params[:content])
    render_success
  else
    render_failure '无跟进动态,暂不能添加建议'
  end
end

#upload_fileObject



228
229
230
# File 'app/controllers/educode_sales/activities_controller.rb', line 228

def upload_file
  render layout: false
end