Class: EducodeSales::ProjectsController

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

#destroyObject



363
364
365
366
# File 'app/controllers/educode_sales/projects_controller.rb', line 363

def destroy
  @project.p_soft_destroy(current_user.id)
  render_success
end

#detailObject



344
345
346
347
348
349
350
# File 'app/controllers/educode_sales/projects_controller.rb', line 344

def detail
  staffs = Staff.where.not(role_id: 11).includes(:user)
  gon.staffs = staffs.map { |d| { name: d.user.real_name, value: d.id } }
  gon.staff_value = @project.p_staff_id ? [{ name: @project.p_staff&.user&.real_name, value: @project.p_staff_id }] : []
  gon.sale_staff_value = @project.p_sale_staff_id ? [{ name: @project.p_sale_staff&.user&.real_name, value: @project.p_sale_staff_id }] : []
  render layout: false
end

#editObject



368
369
370
371
372
373
374
# File 'app/controllers/educode_sales/projects_controller.rb', line 368

def edit
  staffs = Staff.where.not(role_id: 11).includes(:user)
  gon.staffs = staffs.map { |d| { name: d.user.real_name, value: d.id } }
  gon.staff_value = @project.p_staff_id ? [{ name: @project.p_staff&.user&.real_name, value: @project.p_staff_id }] : []
  gon.sale_staff_value = @project.p_sale_staff_id ? [{ name: @project.p_sale_staff&.user&.real_name, value: @project.p_sale_staff_id }] : []
  render layout: false
end

#historyObject



352
353
354
# File 'app/controllers/educode_sales/projects_controller.rb', line 352

def history
  render layout: false
end

#indexObject



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.menus = []
      gon.export_menus = []
      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.extras) }
      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.extras.present?
        if filter.extras['area'].nil?
          filter.extras['area'] = 1
          filter.extras['property'] = 1
          filter.extras['sale_way'] = 1
        end
        filter.save
        gon.filter = filter.extras
      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
      # @budget_stage_ids = Common.where(clazz: '商机阶段', name: ['初步接洽', '准备方案','已交方案', '已立项']).pluck(:id)
      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

      # bidded_date = d.last_follow_up&.bidded_date
      # stage = d.last_follow_up&.stage&.name
      #   (d.p_pre_money_time.present? && Time.now > d.p_pre_money_time && d.p_actual_money_time.blank?) ||
      #   (d.p_pre_accept_time.present? && Time.now > d.p_pre_accept_time && d.p_accept_time.blank?) ? "逾期" : "正常"
      # SELECT * FROM users WHERE NOW() > DATE_ADD(bidded_date, INTERVAL 1 MONTH)

      if params[:q].present? && params[:q][:p_state].present?
        p_state = params[:q][:p_state].to_i
        # 逾期id
        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
      # mysql -select
      @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

#recycleObject



376
377
378
379
# File 'app/controllers/educode_sales/projects_controller.rb', line 376

def recycle
  @project.p_recycle
  render_success
end

#updateObject



356
357
358
359
360
361
# File 'app/controllers/educode_sales/projects_controller.rb', line 356

def update
  @project.assign_attributes(project_params)
  check_changes
  @project.save
  render_success
end