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, #render_failure, #render_success

Instance Method Details

#addObject



63
64
65
# File 'app/controllers/educode_sales/money_plan_records_controller.rb', line 63

def add
  render layout: false
end

#confirmObject



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

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

#confirm_planObject



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'app/controllers/educode_sales/money_plan_records_controller.rb', line 67

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

#createObject



84
85
86
87
88
89
90
91
92
# File 'app/controllers/educode_sales/money_plan_records_controller.rb', line 84

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



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

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

#editObject



107
108
109
110
# File 'app/controllers/educode_sales/money_plan_records_controller.rb', line 107

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
# 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
    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

#updateObject



112
113
114
115
116
117
118
119
# File 'app/controllers/educode_sales/money_plan_records_controller.rb', line 112

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



94
95
96
97
98
99
100
101
102
103
104
105
# File 'app/controllers/educode_sales/money_plan_records_controller.rb', line 94

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