Class: EducodeSales::MoneyPlanRecordsController

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

#addObject



96
97
98
# File 'app/controllers/educode_sales/money_plan_records_controller.rb', line 96

def add
  render layout: false
end

#confirmObject



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

def confirm
  money_plan_record = MoneyPlanRecord.find(params[:id])
  money_plan_record.money_plan_claims.where.not(money_plan_id: params[:money_plan_ids]).delete_all
  businesses = []
  (params[:num].to_i + 1).times do |i|
    claim = money_plan_record.money_plan_claims.find_or_initialize_by(money_plan_id: params[:money_plan_ids][i])
    claim.amount = params["amount[#{i}]"]
    claim.staff =  @current_admin
    claim.save
    business = claim.money_plan.business
    if business
      businesses << business
      money_plan_record.update(business_id: business.id)
      # business.update(return_money: claim.money_plan.money_plan_claims.sum(:amount))
    end
  end
  businesses.uniq.each do |d|
    d.update(return_money: d.money_plans.joins(:money_plan_claims).sum("educode_sales_money_plan_claims.amount").round(6) )

    # 更新对应的计划完成度
    staff_manage_ids = d&.last_follow_up&.assign_follow_ups.present? ? d.last_follow_up.assign_follow_ups.pluck(:staff_id) : [d.staff_id]
    common_id = EducodeSales::Common.find_by(clazz: '计划类型', name: '回款计划')&.id
    EducodeSales::SalePlan.where(month: Time.now.beginning_of_month, business: d, staff_id: staff_manage_ids, common_id: common_id).update_all(finish_rate: 100)
 

    # 生成跟进信息
    last_follow_up = d.last_follow_up
    if last_follow_up.present?
      follow_up = last_follow_up.dup
      follow_up.description = "认领了回款记录"
      follow_up.staff = @current_admin

      last_follow_up.assign_follow_ups.each do |d|
        follow_up.assign_follow_ups.build(staff_id: d.staff_id)
      end

      if follow_up.save!
        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
         # 上次的回款计划的跟进也要同步到最新的跟进id,不然回款计划列表会找不到
         EducodeSales::MoneyPlan.where(business_id: d.id, follow_up_id: last_follow_up.id).update_all(follow_up_id: follow_up.id)

        # 合同里签到日期全同步到最新跟进
        last_follow_up.contract_date_lists.update_all(follow_up_id: follow_up.id)
        
        follow_up.follow_up_trends.create(clazz: '跟进信息', content: "认领了回款记录", staff_id: @current_admin.id)
        d.update(last_follow_up_id: follow_up.id)
      end
    end
  end
  render_success
end

#confirm_planObject



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'app/controllers/educode_sales/money_plan_records_controller.rb', line 100

def confirm_plan
  @money_plan_record = MoneyPlanRecord.find(params[:id])
  gon.money_plan_records = []
  gon.money_plan_ids = []
  gon.money_plan_claim_amount = []
  @money_plan_claim = nil
  @money_plan_claims = @money_plan_record.money_plan_claims.each_with_index do |d, i|
    if i == 0
      @money_plan_claim = d
    end
    gon.money_plan_records << [{value: d.money_plan_id.to_s, name: "#{d.money_plan.business&.number}/#{d.money_plan.business&.name}/#{d.money_plan.business&.school&.name}/ #{d.money_plan.business.last_follow_up&.actual_amount}万 / #{d.money_plan.date_at.to_s(:date)}"}]
    gon.money_plan_ids << d.money_plan_id.to_s
    gon.money_plan_claim_amount << [d.amount]
  end

  gon.edit_record = can?(:update_record, EducodeSales::MoneyPlan)
  gon.edit_record_self = can?(:update_record_self, EducodeSales::MoneyPlan)
  gon.staff_id = @current_admin.id
  render layout: false
end

#createObject



121
122
123
124
125
126
127
128
129
# File 'app/controllers/educode_sales/money_plan_records_controller.rb', line 121

def create
  business_id = Business.find_by(id: params[:business_id])&.id
  record = MoneyPlanRecord.new(amount: params[:amount], payer_name: params[:payer_name], date_at: params[:date_at], staff_id: @current_admin.id, business_id: business_id)
  if record.save
    render_success
  else
    render_failure record
  end
end

#destroyObject



158
159
160
161
162
# File 'app/controllers/educode_sales/money_plan_records_controller.rb', line 158

def destroy
  money_plan_record = MoneyPlanRecord.find(params[:id])
  money_plan_record.destroy
  render json: { succcess: true}
end

#editObject



144
145
146
147
# File 'app/controllers/educode_sales/money_plan_records_controller.rb', line 144

def edit
  @money_plan_record = MoneyPlanRecord.find(params[:id])
  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
# File 'app/controllers/educode_sales/money_plan_records_controller.rb', line 6

def index
  respond_to do |format|
    format.html do

    end
    format.js do
      gon.edit_record = can?(:update_record, EducodeSales::MoneyPlan)
      gon.edit_record_self = can?(:update_record_self, EducodeSales::MoneyPlan)
      gon.staff_id = @current_admin.id
      common = Common.find_by(clazz: 'staff_type', name: '销售')
      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 } }

      if params[:date_at].present? && params[:date_at] != '全部'
        gon.date = Time.new(params[:date_at]).beginning_of_year.strftime("%Y-%m-%d") + " - " + Time.new(params[:date_at]).end_of_year.strftime("%Y-%m-%d")
      end
    end
    format.json do
      @money_plan_records = MoneyPlanRecord.left_joins(:money_plan_claims).group("educode_sales_money_plan_records.id").select("educode_sales_money_plan_records.*, COUNT(educode_sales_money_plan_claims.id) AS claim_num")
      if @current_admin.is_admin?
        @money_plan_records = @money_plan_records
      else
        level = @current_admin.role.role_areas.find_by(clazz: '回款管理').level
        case level
        when '自己'
          @money_plan_records = @money_plan_records.where(staff_id: @current_admin.id)
        when '区域'
          school_ids = School.where(province: @current_admin.areas.pluck(:name)).pluck(:id) + StaffSchool.where(staff_id: @current_admin.id).pluck(:school_id)
          if @current_admin.staff_school_tags.present?
            # 如果设置专项客户类型,则视为专项经理,根据专项客户查看对应商机
            school_ids += School.joins(:school_tags).where("school_tags.id in (?)", @current_admin.staff_school_tags.pluck(:school_tag_id)).pluck(:id)
          end
          b_ids = Business.where(school_id: school_ids.uniq).pluck(:id)
          @money_plan_records = @money_plan_records.joins(:business).where("educode_sales_businesses.id in (?) OR educode_sales_money_plan_records.staff_id = ?", b_ids, @current_admin.id)
        end
      end

      if params[:q].present? && params[:q][:payer_name].present?
        @money_plan_records = @money_plan_records.where("payer_name like ?", "%#{params[:q][:payer_name]}%")
      end

      if params[:q].present? && params[:q][:school].present?
        @money_plan_records = @money_plan_records.joins(business: [department: :school]).where("schools.name like ?", "%#{params[:q][:school]}%")
      end

      if params[:q].present? && params[:q][:date_at].present?
        date = params[:q][:date_at].split(" - ")
        @money_plan_records = @money_plan_records.where("date_at BETWEEN ? AND ?", date[0] + ' 00:00:00', date[1] + ' 23:59:59')
      end
      if params[:q].present? && params[:q][:state].present?
        if params[:q][:state] == '已认领'
          @money_plan_records = @money_plan_records.having("claim_num > 0")
        elsif params[:q][:state] == '待认领'
          @money_plan_records = @money_plan_records.having("claim_num = 0 AND business_id IS NOT NULL")
        elsif params[:q][:state] == '无对应合同'
          @money_plan_records = @money_plan_records.having("claim_num = 0 AND business_id IS NULL")
        end

      end

      if params[:q].present? && params[:q][:property].present?
        # 客户类型
        @money_plan_records = @money_plan_records.joins(business: [department: [school: :school_tags]]).where("school_tags.id = ?", params[:q][:property])
      end

      if params[:q].present? && params[:q][:staff_manages].present?
        # 销售经理
        @money_plan_records = @money_plan_records.joins(:business).joins("
          LEFT JOIN educode_sales_follow_ups AS last_follow_up ON educode_sales_businesses.last_follow_up_id = last_follow_up.id AND last_follow_up.deleted_at IS NULL
          LEFT JOIN educode_sales_assign_follow_ups ON educode_sales_assign_follow_ups.follow_up_id = last_follow_up.id").
       where("(educode_sales_assign_follow_ups.id IS NOT NULL AND educode_sales_assign_follow_ups.staff_id = ?) OR (educode_sales_assign_follow_ups.id IS NULL AND educode_sales_businesses.staff_id = ?)", params[:q][:staff_manages], params[:q][:staff_manages])
      end

      if params[:q].present? && params[:q][:amount].present?
        @money_plan_records = @money_plan_records.where(amount: params[:q][:amount])
      end

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

      if params[:page].present?
        @money_plan_records = @money_plan_records.page(params[:page]).per(params[:limit])
      end
      @money_plan_records = @money_plan_records
    end
  end
end

#updateObject



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

def update
  money_plan_record = MoneyPlanRecord.find(params[:id])
  if money_plan_record.update(money_plan_record_params)
    render json: { succcess: true}
  else
    render_failure money_plan_record
  end
end

#uploadObject



131
132
133
134
135
136
137
138
139
140
141
142
# File 'app/controllers/educode_sales/money_plan_records_controller.rb', line 131

def upload
  xlsx = Roo::Spreadsheet.open(params[:file])
  ods = xlsx.sheet(0)
  rows = ods.last_row - 1
  rows.times do |r|  #行数
    next unless ods.row(r+2)[0]
    business_id = Business.find_by(number: ods.row(r+2)[3].to_s.strip)&.id
    EducodeSales::MoneyPlanRecord.create(payer_name: ods.row(r+2)[0].to_s.strip, amount: ods.row(r+2)[1].to_s.strip, date_at: ods.row(r+2)[2].to_s.strip, business_id: business_id, staff_id: @current_admin.id) if business_id
  end

  render json: { succcess: true}
end