Repository navigation
Expand file tree
/
Copy pathcalculate_your_holiday_entitlement_flow.rb
More file actions
331 lines (281 loc) · 9.85 KB
/
Copy pathcalculate_your_holiday_entitlement_flow.rb
File metadata and controls
331 lines (281 loc) · 9.85 KB
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
class CalculateYourHolidayEntitlementFlow < SmartAnswer::Flow
def define
content_id "deedf6f8-389b-4b34-a5b1-faa9ef909a70"
name "calculate-your-holiday-entitlement"
status :published
# Q1
radio :regular_or_irregular_hours? do
option "irregular-hours-and-part-year"
option "regular"
on_response do |response|
self.calculator = SmartAnswer::Calculators::HolidayEntitlement.new
calculator.regular_or_irregular_hours = response
end
next_node do
if calculator.regular_or_irregular_hours == "regular"
question :basis_of_calculation?
elsif calculator.regular_or_irregular_hours == "irregular-hours-and-part-year"
question :when_does_your_leave_year_start?
end
end
end
radio :basis_of_calculation? do
option "days-worked-per-week"
option "hours-worked-per-week"
option "annualised-hours"
option "compressed-hours"
option "shift-worker"
on_response do |response|
calculator.calculation_basis = response
end
next_node do
case calculator.calculation_basis
when "days-worked-per-week", "hours-worked-per-week", "compressed-hours", "annualised-hours"
question :calculation_period?
when "shift-worker"
question :shift_worker_basis?
end
end
end
# Q2, Q35
radio :calculation_period? do
option "full-year"
option "starting"
option "leaving"
option "starting-and-leaving"
on_response do |response|
calculator.holiday_period = response
end
next_node do
case calculator.holiday_period
when "starting", "starting-and-leaving"
question :what_is_your_starting_date?
when "leaving"
question :what_is_your_leaving_date?
when "full-year"
case calculator.calculation_basis
when "annualised-hours"
outcome :annualised_done
when "days-worked-per-week"
question :how_many_days_per_week?
else
question :how_many_hours_per_week?
end
end
end
end
# Q3 - Q7 - Q8
value_question :how_many_days_per_week?, parse: Float do
on_response do |response|
calculator.working_days_per_week = response
end
validate :error_message do
calculator.working_days_per_week.between?(1, 7)
end
next_node do
outcome :days_per_week_done
end
end
# Q4 - Q12 - Q20 - Q29 - Q36
date_question :what_is_your_starting_date? do
on_response do |response|
calculator.start_date = response
end
next_node do
if calculator.holiday_period == "starting-and-leaving"
question :what_is_your_leaving_date?
elsif calculator.regular_or_irregular_hours == "irregular-hours-and-part-year" && calculator.leave_year_start_date < Date.new(2024, 4, 1)
case calculator.calculation_basis
when "days-worked-per-week"
question :how_many_days_per_week?
when "hours-worked-per-week", "compressed-hours"
question :how_many_hours_per_week?
when "annualised-hours"
outcome :annualised_done
when "shift-worker"
question :shift_worker_hours_per_shift?
end
else
question :when_does_your_leave_year_start?
end
end
end
# Q5 - Q13 - Q21 - Q29 - Q30 - Q37
date_question :what_is_your_leaving_date? do
on_response do |response|
calculator.leaving_date = response
end
validate :error_end_date_before_start_date do
calculator.holiday_period != "starting-and-leaving" || calculator.start_date.before?(calculator.leaving_date)
end
validate :error_end_date_outside_year_range do
calculator.holiday_period != "starting-and-leaving" || SmartAnswer::YearRange.new(begins_on: calculator.start_date).include?(calculator.leaving_date)
end
next_node do
if calculator.holiday_period == "starting-and-leaving" || (calculator.regular_or_irregular_hours == "irregular-hours-and-part-year" && calculator.leave_year_start_date < Date.new(2024, 4, 1))
case calculator.calculation_basis
when "days-worked-per-week"
question :how_many_days_per_week?
when "hours-worked-per-week", "compressed-hours"
question :how_many_hours_per_week?
when "shift-worker"
question :shift_worker_hours_per_shift?
when "annualised-hours"
outcome :annualised_done
end
else
question :when_does_your_leave_year_start?
end
end
end
date_question :when_does_your_leave_year_start? do
on_response do |response|
calculator.leave_year_start_date = response
end
validate :error_end_date_before_start_date do
calculator.leaving_date.blank? || calculator.leave_year_start_date.before?(calculator.leaving_date)
end
validate :error_end_date_outside_leave_year_range do
calculator.leaving_date.blank? || SmartAnswer::YearRange.new(begins_on: calculator.leave_year_start_date).include?(calculator.leaving_date)
end
validate :error_start_date_before_start_leave_year_date do
calculator.start_date.nil? || calculator.leave_year_start_date.before?(calculator.start_date)
end
validate :error_start_date_outside_leave_year_range do
calculator.start_date.nil? || SmartAnswer::YearRange.new(begins_on: calculator.leave_year_start_date).include?(calculator.start_date)
end
next_node do
if calculator.regular_or_irregular_hours == "irregular-hours-and-part-year"
if calculator.leave_year_start_date >= Date.new(2024, 4, 1)
question :hours_in_pay_period?
else
question :basis_of_calculation?
end
else
case calculator.calculation_basis
when "days-worked-per-week"
question :how_many_days_per_week?
when "hours-worked-per-week", "compressed-hours"
question :how_many_hours_per_week?
when "annualised-hours"
outcome :annualised_done
when "shift-worker"
question :shift_worker_hours_per_shift?
end
end
end
end
value_question :hours_in_pay_period?, parse: Float do
on_response do |response|
calculator.hours_in_pay_period = response
end
validate :error_no_hours_worked do
calculator.hours_in_pay_period.positive?
end
next_node do
outcome :irregular_hours_and_part_year_done
end
end
# Q10 - Q15 - Q18
value_question :how_many_hours_per_week?, parse: Float do
on_response do |response|
calculator.hours_per_week = response
end
validate :error_no_hours_worked do
calculator.hours_per_week.positive?
end
validate :error_over_168_hours_worked do
calculator.hours_per_week <= 168
end
next_node do
question :how_many_days_per_week_for_hours?
end
end
# Q11 - Q16 - Q19
value_question :how_many_days_per_week_for_hours?, parse: Float do
on_response do |response|
calculator.working_days_per_week = response
end
validate :error_over_7_days_per_week do
calculator.working_days_per_week.between?(1, 7)
end
validate :error_over_24_hours_per_day do
calculator.hours_per_week.blank? || (calculator.hours_per_week / calculator.working_days_per_week) <= 24
end
next_node do
if calculator.calculation_basis == "compressed-hours"
outcome :compressed_hours_done
else
outcome :hours_per_week_done
end
end
end
radio :shift_worker_basis? do
option "full-year"
option "starting"
option "leaving"
option "starting-and-leaving"
on_response do |response|
calculator.holiday_period = response
end
next_node do
case calculator.holiday_period
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
# Q26 - Q32
value_question :shift_worker_hours_per_shift?, parse: Float do
on_response do |response|
calculator.hours_per_shift = response
end
validate :error_no_hours_worked do
calculator.hours_per_shift.positive?
end
validate :error_over_24_hours_worked do
calculator.hours_per_shift <= 24
end
next_node do
question :shift_worker_shifts_per_shift_pattern?
end
end
# Q27 - Q33
value_question :shift_worker_shifts_per_shift_pattern?, parse: Integer do
on_response do |response|
calculator.shifts_per_shift_pattern = response
end
validate :error_message do
calculator.shifts_per_shift_pattern.positive?
end
next_node do
question :shift_worker_days_per_shift_pattern?
end
end
# Q28 - Q34
value_question :shift_worker_days_per_shift_pattern?, parse: Float do
on_response do |response|
calculator.days_per_shift_pattern = response
end
validate :error_message do
calculator.days_per_shift_pattern >= calculator.shifts_per_shift_pattern
end
next_node do
outcome :shift_worker_done
end
end
# ======================================================================
# Results
# ======================================================================
outcome :shift_worker_done
outcome :days_per_week_done
outcome :hours_per_week_done
outcome :compressed_hours_done
outcome :annualised_done
outcome :irregular_hours_and_part_year_done
end
end