-
Notifications
You must be signed in to change notification settings - Fork 120
/
Copy pathcalculate-your-holiday-entitlement.rb
332 lines (299 loc) · 10.3 KB
/
calculate-your-holiday-entitlement.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
module SmartAnswer
class CalculateYourHolidayEntitlementFlow < Flow
def define
content_id "deedf6f8-389b-4b34-a5b1-faa9ef909a70"
name 'calculate-your-holiday-entitlement'
status :published
satisfies_need "100143"
# Q1
multiple_choice :basis_of_calculation? do
option "days-worked-per-week"
option "hours-worked-per-week"
option "casual-or-irregular-hours"
option "annualised-hours"
option "compressed-hours"
option "shift-worker"
save_input_as :calculation_basis
calculate :leaving_date do
nil
end
calculate :leave_year_start_date do
nil
end
calculate :start_date do
nil
end
next_node(permitted: :auto) do |response|
case response
when 'days-worked-per-week', 'hours-worked-per-week'
question :calculation_period?
when 'casual-or-irregular-hours'
question :casual_or_irregular_hours?
when 'annualised-hours'
question :annualised_hours?
when 'compressed-hours'
question :compressed_hours_how_many_hours_per_week?
when 'shift-worker'
question :shift_worker_basis?
end
end
end
# Q2
multiple_choice :calculation_period? do
option "full-year"
option "starting"
option "leaving"
option "starting-and-leaving"
save_input_as :holiday_period
next_node(permitted: :auto) do |response|
case response
when "starting", "starting-and-leaving"
question :what_is_your_starting_date?
when "leaving"
question :what_is_your_leaving_date?
else
if calculation_basis == "days-worked-per-week"
question :how_many_days_per_week?
else
question :how_many_hours_per_week?
end
end
end
end
# Q3
value_question :how_many_days_per_week?, parse: Float do
calculate :days_per_week do |response|
days_per_week = response
raise InvalidResponse if days_per_week <= 0 or days_per_week > 7
days_per_week
end
next_node :days_per_week_done
end
# Q4
date_question :what_is_your_starting_date? do
from { Date.civil(1.year.ago.year, 1, 1) }
to { Date.civil(1.year.since(Date.today).year, 12, 31) }
save_input_as :start_date
next_node(permitted: :auto) do
if holiday_period == "starting-and-leaving"
question :what_is_your_leaving_date?
else
question :when_does_your_leave_year_start?
end
end
end
# Q5
date_question :what_is_your_leaving_date? do
from { Date.civil(1.year.ago.year, 1, 1) }
to { Date.civil(1.year.since(Date.today).year, 12, 31) }
save_input_as :leaving_date
next_node(permitted: :auto) do
if holiday_period == "starting-and-leaving"
case calculation_basis
when "days-worked-per-week"
question :how_many_days_per_week?
when "hours-worked-per-week"
question :how_many_hours_per_week?
when "shift-worker"
question :shift_worker_hours_per_shift?
end
else
question :when_does_your_leave_year_start?
end
end
end
# Q21
date_question :when_does_your_leave_year_start? do
from { Date.civil(1.year.ago.year, 1, 1) }
to { Date.civil(1.year.since(Date.today).year, 12, 31) }
save_input_as :leave_year_start_date
next_node(permitted: :auto) do
case calculation_basis
when "days-worked-per-week"
question :how_many_days_per_week?
when "hours-worked-per-week"
question :how_many_hours_per_week?
when "shift-worker"
question :shift_worker_hours_per_shift?
end
end
end
# Q10
value_question :how_many_hours_per_week?, parse: Float do
save_input_as :hours_per_week
next_node :hours_per_week_done
end
value_question :casual_or_irregular_hours?, parse: Float do
calculate :total_hours do |response|
hours = response
raise InvalidResponse if hours <= 0
hours
end
next_node :casual_or_irregular_hours_done
end
value_question :annualised_hours?, parse: Float do
calculate :total_hours do |response|
hours = response
raise InvalidResponse if hours <= 0
hours
end
next_node :annualised_hours_done
end
value_question :compressed_hours_how_many_hours_per_week?, parse: Float do
calculate :hours_per_week do |response|
hours = response
raise InvalidResponse if hours <= 0 or hours > 168
hours
end
next_node :compressed_hours_how_many_days_per_week?
end
value_question :compressed_hours_how_many_days_per_week?, parse: Float do
calculate :days_per_week do |response|
days = response
raise InvalidResponse if days <= 0 or days > 7
days
end
next_node :compressed_hours_done
end
multiple_choice :shift_worker_basis? do
option "full-year"
option "starting"
option "leaving"
option "starting-and-leaving"
save_input_as :holiday_period
next_node(permitted: :auto) do |response|
case response
when 'full-year'
question :shift_worker_hours_per_shift?
when 'starting', 'starting-and-leaving'
question :what_is_your_starting_date?
when 'leaving'
question :what_is_your_leaving_date?
end
end
end
value_question :shift_worker_hours_per_shift?, parse: Float do
calculate :hours_per_shift do |response|
hours_per_shift = response
raise InvalidResponse if hours_per_shift <= 0
hours_per_shift
end
next_node :shift_worker_shifts_per_shift_pattern?
end
value_question :shift_worker_shifts_per_shift_pattern?, parse: Integer do
calculate :shifts_per_shift_pattern do |response|
shifts = response
raise InvalidResponse if shifts <= 0
shifts
end
next_node :shift_worker_days_per_shift_pattern?
end
value_question :shift_worker_days_per_shift_pattern?, parse: Float do
calculate :days_per_shift_pattern do |response|
days = response
raise InvalidResponse if days < shifts_per_shift_pattern
days
end
next_node :shift_worker_done
end
outcome :shift_worker_done do
precalculate :calculator do
Calculators::HolidayEntitlement.new(
start_date: start_date,
leaving_date: leaving_date,
leave_year_start_date: leave_year_start_date,
hours_per_shift: hours_per_shift,
shifts_per_shift_pattern: shifts_per_shift_pattern,
days_per_shift_pattern: days_per_shift_pattern
)
end
precalculate :holiday_entitlement_shifts do
calculator.formatted_shift_entitlement
end
precalculate :hours_per_shift do
calculator.strip_zeros hours_per_shift
end
end
outcome :days_per_week_done do
precalculate :days_per_week_calculated do
(days_per_week < 5 ? days_per_week : 5)
end
precalculate :calculator do
Calculators::HolidayEntitlement.new(
days_per_week: (leave_year_start_date.nil? ? days_per_week : days_per_week_calculated),
start_date: start_date,
leaving_date: leaving_date,
leave_year_start_date: leave_year_start_date
)
end
precalculate :holiday_entitlement_days do
calculator.formatted_full_time_part_time_days
end
end
outcome :hours_per_week_done do
precalculate :calculator do |response|
Calculators::HolidayEntitlement.new(
hours_per_week: hours_per_week,
start_date: start_date,
leaving_date: leaving_date,
leave_year_start_date: leave_year_start_date
)
end
precalculate :holiday_entitlement_hours_and_minutes do
calculator.full_time_part_time_hours_and_minutes
end
precalculate :holiday_entitlement_hours do
holiday_entitlement_hours_and_minutes.first
end
precalculate :holiday_entitlement_minutes do
holiday_entitlement_hours_and_minutes.last
end
end
outcome :casual_or_irregular_hours_done do
precalculate :calculator do
Calculators::HolidayEntitlement.new(total_hours: total_hours)
end
precalculate :holiday_entitlement_hours do
calculator.casual_irregular_entitlement.first
end
precalculate :holiday_entitlement_minutes do
calculator.casual_irregular_entitlement.last
end
end
outcome :compressed_hours_done do
precalculate :calculator do
Calculators::HolidayEntitlement.new(
hours_per_week: hours_per_week,
days_per_week: days_per_week
)
end
precalculate :holiday_entitlement_hours do
calculator.compressed_hours_entitlement.first
end
precalculate :holiday_entitlement_minutes do
calculator.compressed_hours_entitlement.last
end
precalculate :hours_daily do
calculator.compressed_hours_daily_average.first
end
precalculate :minutes_daily do
calculator.compressed_hours_daily_average.last
end
end
outcome :annualised_hours_done do
precalculate :calculator do
Calculators::HolidayEntitlement.new(total_hours: total_hours)
end
precalculate :average_hours_per_week do
calculator.formatted_annualised_hours_per_week
end
precalculate :holiday_entitlement_hours do
calculator.annualised_entitlement.first
end
precalculate :holiday_entitlement_minutes do
calculator.annualised_entitlement.last
end
end
end
end
end