require_dependency "educode_sales/application_controller" module EducodeSales class MoneyPlanRecordsController < ApplicationController 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 def add render layout: false end 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 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 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 def edit @money_plan_record = MoneyPlanRecord.find(params[:id]) render layout: false end 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 def destroy money_plan_record = MoneyPlanRecord.find(params[:id]) money_plan_record.destroy render json: { succcess: true} end 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 private def money_plan_record_params params.permit(:amount, :payer_name, :date_at, :business_id) end end end