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 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? 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) b_ids = Business.where(school_id: school_ids).pluck(:id) @money_plan_records = @money_plan_records.joins(:business).where("educode_sales_businesses.id in (?) OR educode_sales_money_plans.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][:date_at].present? date = params[:q][:date_at].split(" - ") @money_plan_records = @money_plan_records.where("date_at BETWEEN ? AND ?", date[0], date[1]) 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][: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 @money_plan_records = @money_plan_records.page(params[:page]).per(params[:limit]) 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 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 (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 money_plan_record.update(business_id: business.id) business.update(return_money: claim.money_plan.money_plan_claims.sum(:amount)) end end render_success end private def money_plan_record_params params.permit(:amount, :payer_name, :date_at) end end end