Class: EducodeSales::MoneyPlanRecordsController
Instance Method Summary
collapse
#authenticate_admin, #authenticate_request, #current_user, #filter, #render_failure, #render_success
Instance Method Details
#add ⇒ Object
67
68
69
|
# File 'app/controllers/educode_sales/money_plan_records_controller.rb', line 67
def add
render layout: false
end
|
#confirm ⇒ Object
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
|
# File 'app/controllers/educode_sales/money_plan_records_controller.rb', line 135
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)
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") )
end
render_success
end
|
#confirm_plan ⇒ Object
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
|
# File 'app/controllers/educode_sales/money_plan_records_controller.rb', line 71
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
|
#create ⇒ Object
92
93
94
95
96
97
98
99
100
|
# File 'app/controllers/educode_sales/money_plan_records_controller.rb', line 92
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
|
#destroy ⇒ Object
129
130
131
132
133
|
# File 'app/controllers/educode_sales/money_plan_records_controller.rb', line 129
def destroy
money_plan_record = MoneyPlanRecord.find(params[:id])
money_plan_record.destroy
render json: { succcess: true}
end
|
#edit ⇒ Object
115
116
117
118
|
# File 'app/controllers/educode_sales/money_plan_records_controller.rb', line 115
def edit
@money_plan_record = MoneyPlanRecord.find(params[:id])
render layout: false
end
|
#index ⇒ Object
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
|
# 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][: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], 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
|
#update ⇒ Object
120
121
122
123
124
125
126
127
|
# File 'app/controllers/educode_sales/money_plan_records_controller.rb', line 120
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
|
#upload ⇒ Object
102
103
104
105
106
107
108
109
110
111
112
113
|
# File 'app/controllers/educode_sales/money_plan_records_controller.rb', line 102
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
|