Skip to content

Commit 97410e1

Browse files
authored
Merge pull request #2834 from alphagov/automate-date-ranges-for-redundancy-pay-calculators
Automate date ranges for redundancy pay calculators
2 parents 1d5d742 + 38d7e01 commit 97410e1

File tree

5 files changed

+98
-6
lines changed

5 files changed

+98
-6
lines changed

lib/smart_answer/calculators/redundancy_calculator.rb

+38
Original file line numberDiff line numberDiff line change
@@ -39,5 +39,43 @@ def self.redundancy_rates(date)
3939
def self.northern_ireland_redundancy_rates(date)
4040
RatesQuery.from_file('redundancy_pay_northern_ireland').rates(date)
4141
end
42+
43+
def self.first_selectable_date
44+
if between_january_and_august?
45+
four_years_ago
46+
else
47+
three_years_ago
48+
end
49+
end
50+
51+
def self.last_selectable_date
52+
if between_january_and_august?
53+
end_of_current_year
54+
else
55+
end_of_next_year
56+
end
57+
end
58+
59+
def self.between_january_and_august?
60+
Date.today.month < 9
61+
end
62+
63+
def self.four_years_ago
64+
4.years.ago.beginning_of_year
65+
end
66+
67+
def self.three_years_ago
68+
3.years.ago.beginning_of_year
69+
end
70+
71+
def self.end_of_current_year
72+
Date.today.end_of_year
73+
end
74+
75+
def self.end_of_next_year
76+
Date.today.next_year.end_of_year
77+
end
78+
79+
private_class_method :between_january_and_august?, :four_years_ago, :three_years_ago, :end_of_current_year, :end_of_next_year
4280
end
4381
end

lib/smart_answer_flows/shared/redundancy_pay_flow.rb

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ module Shared
33
class RedundancyPayFlow < Flow
44
def define
55
date_question :date_of_redundancy? do
6-
from { Date.civil(2013, 1, 1) }
7-
to { 1.year.since }
6+
from { Calculators::RedundancyCalculator.first_selectable_date }
7+
to { Calculators::RedundancyCalculator.last_selectable_date }
88
validate_in_range
99

1010
calculate :rates do |response|

test/data/calculate-employee-redundancy-pay-files.yml

+2-2
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ lib/smart_answer_flows/calculate-employee-redundancy-pay/questions/age_of_employ
99
lib/smart_answer_flows/calculate-employee-redundancy-pay/questions/date_of_redundancy.govspeak.erb: e6ba7dfca27ad55335fb2cd7dda62d5f
1010
lib/smart_answer_flows/calculate-employee-redundancy-pay/questions/weekly_pay_before_tax.govspeak.erb: 805f2283eecf730779d648b2998db59f
1111
lib/smart_answer_flows/calculate-employee-redundancy-pay/questions/years_employed.govspeak.erb: 6840413403d9331d79e3b60f04d244a9
12-
lib/smart_answer_flows/shared/redundancy_pay_flow.rb: 944b61e48afdcecde98c8f179ec5b90a
13-
lib/smart_answer/calculators/redundancy_calculator.rb: 102729e2e21790e3f3691cc01d1c1d95
12+
lib/smart_answer_flows/shared/redundancy_pay_flow.rb: 91e04ab20e95e6a5b92bed8b41928038
13+
lib/smart_answer/calculators/redundancy_calculator.rb: 9ec0274471599d20b95a7a7dd42b6fe1
1414
lib/data/rates/redundancy_pay.yml: a214c01e46e92847f26b1de107997c78
1515
lib/data/rates/redundancy_pay_northern_ireland.yml: 83dcea5aa4048b6fb5d4aa222343e5c5

test/data/calculate-your-redundancy-pay-files.yml

+2-2
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ lib/smart_answer_flows/calculate-your-redundancy-pay/questions/age_of_employee.g
99
lib/smart_answer_flows/calculate-your-redundancy-pay/questions/date_of_redundancy.govspeak.erb: bdb2975cce5bd91c754ac13fad1af4b8
1010
lib/smart_answer_flows/calculate-your-redundancy-pay/questions/weekly_pay_before_tax.govspeak.erb: 9662fd7b09d3722850ede311bf27b844
1111
lib/smart_answer_flows/calculate-your-redundancy-pay/questions/years_employed.govspeak.erb: 6bf4ef75048732fab207104b21cdc6e6
12-
lib/smart_answer_flows/shared/redundancy_pay_flow.rb: 944b61e48afdcecde98c8f179ec5b90a
13-
lib/smart_answer/calculators/redundancy_calculator.rb: 102729e2e21790e3f3691cc01d1c1d95
12+
lib/smart_answer_flows/shared/redundancy_pay_flow.rb: 91e04ab20e95e6a5b92bed8b41928038
13+
lib/smart_answer/calculators/redundancy_calculator.rb: 9ec0274471599d20b95a7a7dd42b6fe1
1414
lib/data/rates/redundancy_pay.yml: a214c01e46e92847f26b1de107997c78
1515
lib/data/rates/redundancy_pay_northern_ireland.yml: 83dcea5aa4048b6fb5d4aa222343e5c5

test/unit/calculators/redundancy_calculator_test.rb

+54
Original file line numberDiff line numberDiff line change
@@ -121,5 +121,59 @@ class RedundancyCalculatorTest < ActiveSupport::TestCase
121121
assert_equal 17.5, @calculator.number_of_weeks_entitlement
122122
end
123123
end
124+
125+
context "Redundancy date selector" do
126+
context "Earliest selectable date" do
127+
context "Months January to August" do
128+
should "return the start of the year, four years ago if date is January 1st" do
129+
Timecop.freeze('2016-01-01')
130+
assert_equal Date.parse('2012-01-01'), RedundancyCalculator.first_selectable_date
131+
end
132+
133+
should "return the start of the year, four years ago if date is August 31st" do
134+
Timecop.freeze('2016-08-31')
135+
assert_equal Date.parse('2012-01-01'), RedundancyCalculator.first_selectable_date
136+
end
137+
end
138+
139+
context "Months September to December" do
140+
should "return the start of the year, three years ago if date is September 1st" do
141+
Timecop.freeze('2016-09-01')
142+
assert_equal Date.parse('2013-01-01'), RedundancyCalculator.first_selectable_date
143+
end
144+
145+
should "return the start of the year, three years ago if date is December 31st" do
146+
Timecop.freeze('2016-12-31')
147+
assert_equal Date.parse('2013-01-01'), RedundancyCalculator.first_selectable_date
148+
end
149+
end
150+
end
151+
152+
context "Last selectable date" do
153+
context "Months January to August" do
154+
should "return the end of the current year if the date is January 1st" do
155+
Timecop.freeze('2016-01-01')
156+
assert_equal Date.parse('2016-12-31'), RedundancyCalculator.last_selectable_date
157+
end
158+
159+
should "return the end of the current year if the date is August 31st" do
160+
Timecop.freeze('2016-08-31')
161+
assert_equal Date.parse('2016-12-31'), RedundancyCalculator.last_selectable_date
162+
end
163+
end
164+
165+
context "Months September to December" do
166+
should "return end of the next year if the date is September 1st" do
167+
Timecop.freeze('2016-09-01')
168+
assert_equal Date.parse('2017-12-31'), RedundancyCalculator.last_selectable_date
169+
end
170+
171+
should "return end of the next year if the date is December 31st" do
172+
Timecop.freeze('2016-12-31')
173+
assert_equal Date.parse('2017-12-31'), RedundancyCalculator.last_selectable_date
174+
end
175+
end
176+
end
177+
end
124178
end
125179
end

0 commit comments

Comments
 (0)