-
Notifications
You must be signed in to change notification settings - Fork 120
/
Copy pathadoption-calculator.rb
289 lines (236 loc) · 6.61 KB
/
adoption-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
## QA0
multiple_choice :taking_paternity_leave_for_adoption? do
option :yes
option :no
next_node(permitted: :auto) do |response|
case response
when 'yes'
question :employee_date_matched_paternity_adoption? #QAP1
when 'no'
question :date_of_adoption_match? # QA1
end
end
end
## QA1
date_question :date_of_adoption_match? do
calculate :match_date do |response|
response
end
calculate :calculator do
Calculators::MaternityPaternityCalculator.new(match_date, "adoption")
end
next_node :date_of_adoption_placement?
end
## QA2
date_question :date_of_adoption_placement? do
calculate :adoption_placement_date do |response|
placement_date = response
raise SmartAnswer::InvalidResponse if placement_date < match_date
calculator.adoption_placement_date = placement_date
placement_date
end
calculate :a_leave_earliest_start do
adoption_placement_date - 14
end
calculate :a_leave_earliest_start_formatted do
calculator.format_date a_leave_earliest_start
end
calculate :employment_start do
calculator.a_employment_start
end
calculate :qualifying_week_start do
calculator.qualifying_week.first
end
next_node :adoption_did_the_employee_work_for_you?
end
## QA3
multiple_choice :adoption_did_the_employee_work_for_you? do
option :yes
option :no
next_node(permitted: :auto) do |response|
case response
when 'yes'
question :adoption_employment_contract?
when 'no'
outcome :adoption_not_entitled_to_leave_or_pay
end
end
end
## QA4
multiple_choice :adoption_employment_contract? do
option :yes
option :no
save_input_as :employee_has_contract_adoption
next_node :adoption_is_the_employee_on_your_payroll?
end
## QA5
multiple_choice :adoption_is_the_employee_on_your_payroll? do
option :yes
option :no
save_input_as :on_payroll
calculate :to_saturday do
calculator.matched_week.last
end
calculate :to_saturday_formatted do
calculator.format_date_day to_saturday
end
next_node_calculation(:no_contract_not_on_payroll) do |response|
employee_has_contract_adoption == 'no' && response == 'no'
end
next_node(permitted: :auto) do
if no_contract_not_on_payroll
outcome :adoption_not_entitled_to_leave_or_pay
else
question :adoption_date_leave_starts?
end
end
end
## QA6
date_question :adoption_date_leave_starts? do
calculate :adoption_date_leave_starts do |response|
ald_start = response
raise SmartAnswer::InvalidResponse if ald_start < a_leave_earliest_start
calculator.leave_start_date = ald_start
end
calculate :leave_start_date do
calculator.leave_start_date
end
calculate :leave_end_date do
calculator.leave_end_date
end
calculate :pay_start_date do
calculator.pay_start_date
end
calculate :pay_end_date do
calculator.pay_end_date
end
calculate :a_notice_leave do
calculator.format_date calculator.a_notice_leave
end
next_node_calculation(:has_contract_not_on_payroll) do
employee_has_contract_adoption == 'yes' && on_payroll == 'no'
end
next_node(permitted: :auto) do
if has_contract_not_on_payroll
outcome :adoption_leave_and_pay
else
question :last_normal_payday_adoption?
end
end
end
# QA7
date_question :last_normal_payday_adoption? 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_adoption?
end
# QA8
date_question :payday_eight_weeks_adoption? 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_adoption?
end
# QA9
multiple_choice :pay_frequency_adoption? do
option :weekly
option :every_2_weeks
option :every_4_weeks
option :monthly
save_input_as :pay_pattern
calculate :calculator do |response|
calculator.pay_method = response
calculator
end
next_node :earnings_for_pay_period_adoption?
end
## QA10
money_question :earnings_for_pay_period_adoption? do
calculate :lower_earning_limit do
sprintf("%.2f", calculator.lower_earning_limit)
end
calculate :average_weekly_earnings do
sprintf("%.2f", calculator.average_weekly_earnings)
end
calculate :above_lower_earning_limit do
calculator.average_weekly_earnings > calculator.lower_earning_limit
end
next_node_calculation :calculator do |response|
calculator.calculate_average_weekly_pay(pay_pattern, response)
calculator
end
next_node_calculation(:average_weekly_earnings_under_lower_earning_limit) do
calculator.average_weekly_earnings < calculator.lower_earning_limit
end
next_node(permitted: :auto) do
if average_weekly_earnings_under_lower_earning_limit
outcome :adoption_leave_and_pay
else
question :how_do_you_want_the_sap_calculated?
end
end
end
## QA11
multiple_choice :how_do_you_want_the_sap_calculated? do
option :weekly_starting
option :usual_paydates
save_input_as :sap_calculation_method
next_node(permitted: :auto) do |response|
if response == 'weekly_starting'
outcome :adoption_leave_and_pay
elsif pay_pattern == 'monthly'
question :monthly_pay_paternity?
else
question :next_pay_day_paternity?
end
end
end
outcome :adoption_leave_and_pay 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 sap_calculation_method == 'weekly_starting'
sap_calculation_method
else
pay_pattern
end
)
end
precalculate :pay_dates_and_pay do
if above_lower_earning_limit
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
precalculate :total_sap do
if above_lower_earning_limit
sprintf("%.2f", calculator.total_statutory_pay)
end
end
end
outcome :adoption_not_entitled_to_leave_or_pay