-
Notifications
You must be signed in to change notification settings - Fork 120
/
Copy pathmaternity-calculator.rb
336 lines (284 loc) · 8.18 KB
/
maternity-calculator.rb
1
2
3
4
5
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
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
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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
days_of_the_week = Calculators::MaternityPaternityCalculator::DAYS_OF_THE_WEEK
## QM1
date_question :baby_due_date_maternity? do
from { 1.year.ago(Date.today) }
to { 2.years.since(Date.today) }
calculate :calculator do |response|
Calculators::MaternityPaternityCalculator.new(response)
end
next_node :employment_contract?
end
## QM2
multiple_choice :employment_contract? do
option :yes
option :no
save_input_as :has_employment_contract
next_node :date_leave_starts?
end
## QM3
date_question :date_leave_starts? do
from { 2.years.ago(Date.today) }
to { 2.years.since(Date.today) }
precalculate :leave_earliest_start_date do
calculator.leave_earliest_start_date
end
calculate :leave_start_date do |response|
ls_date = response
raise SmartAnswer::InvalidResponse if ls_date < leave_earliest_start_date
calculator.leave_start_date = ls_date
calculator.leave_start_date
end
calculate :leave_end_date do
calculator.leave_end_date
end
calculate :leave_earliest_start_date do
calculator.leave_earliest_start_date
end
calculate :notice_of_leave_deadline do
calculator.notice_of_leave_deadline
end
calculate :pay_start_date do
calculator.pay_start_date
end
calculate :pay_end_date do
calculator.pay_end_date
end
calculate :employment_start do
calculator.employment_start
end
calculate :qualifying_week_start do
calculator.qualifying_week.first
end
calculate :ssp_stop do
calculator.ssp_stop
end
next_node :did_the_employee_work_for_you?
end
## QM4
multiple_choice :did_the_employee_work_for_you? do
option :yes
option :no
calculate :not_entitled_to_pay_reason do |response|
response == 'no' ? :not_worked_long_enough : nil
end
next_node(permitted: :auto) do |response|
case response
when 'yes'
question :is_the_employee_on_your_payroll?
when 'no'
outcome :maternity_leave_and_pay_result
end
end
end
## QM5
multiple_choice :is_the_employee_on_your_payroll? do
option :yes
option :no
calculate :not_entitled_to_pay_reason do |response|
response == 'no' ? :must_be_on_payroll : nil
end
calculate :to_saturday do
calculator.qualifying_week.last
end
calculate :to_saturday_formatted do
calculator.format_date_day to_saturday
end
next_node(permitted: :auto) do |response|
case response
when 'yes'
question :last_normal_payday? # NOTE: goes to shared questions
when 'no'
outcome :maternity_leave_and_pay_result
end
end
end
## QM6
date_question :last_normal_payday? do
from { 2.years.ago(Date.today) }
to { 2.years.since(Date.today) }
calculate :last_payday do |response|
calculator.last_payday = response
raise SmartAnswer::InvalidResponse if calculator.last_payday > to_saturday
calculator.last_payday
end
next_node :payday_eight_weeks?
end
## QM7
date_question :payday_eight_weeks? do
from { 2.year.ago(Date.today) }
to { 2.years.since(Date.today) }
precalculate :payday_offset do
calculator.payday_offset
end
precalculate :payday_offset_formatted do
calculator.format_date_day payday_offset
end
calculate :last_payday_eight_weeks do |response|
payday = response + 1.day
raise SmartAnswer::InvalidResponse if payday > payday_offset
calculator.pre_offset_payday = payday
payday
end
calculate :relevant_period do
calculator.formatted_relevant_period
end
next_node :pay_frequency?
end
## QM8
multiple_choice :pay_frequency? do
save_input_as :pay_pattern
option :weekly
option :every_2_weeks
option :every_4_weeks
option :monthly
next_node(:earnings_for_pay_period?)
end
## QM9 Maternity only onwards
money_question :earnings_for_pay_period? do
calculate :calculator do |response|
calculator.calculate_average_weekly_pay(pay_pattern, response)
calculator
end
calculate :average_weekly_earnings do
calculator.average_weekly_earnings
end
next_node :how_do_you_want_the_smp_calculated?
end
## QM10
multiple_choice :how_do_you_want_the_smp_calculated? do
option :weekly_starting
option :usual_paydates
save_input_as :smp_calculation_method
next_node(permitted: :auto) do |response|
if response == 'usual_paydates'
if pay_pattern == 'monthly'
question :when_in_the_month_is_the_employee_paid?
else
question :when_is_your_employees_next_pay_day?
end
else
outcome :maternity_leave_and_pay_result
end
end
end
## QM11
date_question :when_is_your_employees_next_pay_day? do
calculate :next_pay_day do |response|
calculator.pay_date = response
calculator.pay_date
end
next_node :maternity_leave_and_pay_result
end
## QM12
multiple_choice :when_in_the_month_is_the_employee_paid? do
option :first_day_of_the_month
option :last_day_of_the_month
option :specific_date_each_month
option :last_working_day_of_the_month
option :a_certain_week_day_each_month
save_input_as :monthly_pay_method
next_node(permitted: :auto) do |response|
case response
when 'first_day_of_the_month', 'last_day_of_the_month'
outcome :maternity_leave_and_pay_result
when 'specific_date_each_month'
question :what_specific_date_each_month_is_the_employee_paid?
when 'last_working_day_of_the_month'
question :what_days_does_the_employee_work?
when 'a_certain_week_day_each_month'
question :what_particular_day_of_the_month_is_the_employee_paid?
end
end
end
## QM13
value_question :what_specific_date_each_month_is_the_employee_paid?, parse: :to_i do
calculate :pay_day_in_month do |response|
day = response
raise InvalidResponse unless day > 0 and day < 32
calculator.pay_day_in_month = day
end
next_node :maternity_leave_and_pay_result
end
## QM14
checkbox_question :what_days_does_the_employee_work? do
(0...days_of_the_week.size).each { |i| option i.to_s.to_sym }
calculate :last_day_in_week_worked do |response|
calculator.work_days = response.split(",").map(&:to_i)
calculator.pay_day_in_week = response.split(",").sort.last.to_i
end
next_node :maternity_leave_and_pay_result
end
## QM15
multiple_choice :what_particular_day_of_the_month_is_the_employee_paid? do
days_of_the_week.each { |d| option d.to_sym }
calculate :pay_day_in_week do |response|
calculator.pay_day_in_week = days_of_the_week.index(response)
response
end
next_node :which_week_in_month_is_the_employee_paid?
end
## QM16
multiple_choice :which_week_in_month_is_the_employee_paid? do
option :"first"
option :"second"
option :"third"
option :"fourth"
option :"last"
calculate :pay_week_in_month do |response|
calculator.pay_week_in_month = response
end
next_node :maternity_leave_and_pay_result
end
## Maternity outcomes
outcome :maternity_leave_and_pay_result do
precalculate :pay_method do
calculator.pay_method = (
if monthly_pay_method
if monthly_pay_method == 'specific_date_each_month' and pay_day_in_month > 28
'last_day_of_the_month'
else
monthly_pay_method
end
elsif smp_calculation_method == 'weekly_starting'
smp_calculation_method
elsif pay_pattern
pay_pattern
end
)
end
precalculate :smp_a do
sprintf("%.2f", calculator.statutory_maternity_rate_a)
end
precalculate :smp_b do
sprintf("%.2f", calculator.statutory_maternity_rate_b)
end
precalculate :lower_earning_limit do
sprintf("%.2f", calculator.lower_earning_limit)
end
precalculate :notice_request_pay do
calculator.notice_request_pay
end
precalculate :below_threshold do
calculator.average_weekly_earnings and
calculator.average_weekly_earnings < calculator.lower_earning_limit
end
precalculate :not_entitled_to_pay_reason do
if below_threshold
:must_earn_over_threshold
else
not_entitled_to_pay_reason
end
end
precalculate :total_smp do
unless not_entitled_to_pay_reason.present?
sprintf("%.2f", calculator.total_statutory_pay)
end
end
precalculate :pay_dates_and_pay do
unless not_entitled_to_pay_reason.present?
calculator.paydates_and_pay.map do |date_and_pay|
%Q(#{date_and_pay[:date].strftime("%e %B %Y")}|£#{sprintf("%.2f", date_and_pay[:pay])})
end.join("\n")
end
end
end