diff --git a/app/models/form_handler.rb b/app/models/form_handler.rb index 4ba300552..13df48af4 100644 --- a/app/models/form_handler.rb +++ b/app/models/form_handler.rb @@ -79,12 +79,12 @@ class FormHandler def lettings_in_crossover_period?(now: Time.zone.now) forms = lettings_forms.values - forms.count { |form| form.start_date < now && now < form.end_date } > 1 + forms.count { |form| form.start_date <= now && now < form.end_date } > 1 end def sales_in_crossover_period?(now: Time.zone.now) forms = sales_forms.values - forms.count { |form| form.start_date < now && now < form.end_date } > 1 + forms.count { |form| form.start_date <= now && now < form.end_date } > 1 end def use_fake_forms! diff --git a/app/models/validations/date_validations.rb b/app/models/validations/date_validations.rb index 1884560b7..299146c44 100644 --- a/app/models/validations/date_validations.rb +++ b/app/models/validations/date_validations.rb @@ -68,6 +68,37 @@ module Validations::DateValidations private + def active_collection_start_date + if FormHandler.instance.lettings_in_crossover_period? + previous_collection_start_date + else + current_collection_start_date + end + end + + def validation_error_message + current_end_year_long = current_collection_end_date.strftime("#{current_collection_end_date.day.ordinalize} %B %Y") + + if FormHandler.instance.lettings_in_crossover_period? + I18n.t( + "validations.setup.startdate.previous_and_current_financial_year", + previous_start_year_short: previous_collection_start_date.strftime("%y"), + previous_end_year_short: previous_collection_end_date.strftime("%y"), + previous_start_year_long: previous_collection_start_date.strftime("#{previous_collection_start_date.day.ordinalize} %B %Y"), + current_end_year_short: current_collection_end_date.strftime("%y"), + current_end_year_long:, + ) + else + I18n.t( + "validations.setup.startdate.current_financial_year", + current_start_year_short: current_collection_start_date.strftime("%y"), + current_end_year_short: current_collection_end_date.strftime("%y"), + current_start_year_long: current_collection_start_date.strftime("#{current_collection_start_date.day.ordinalize} %B %Y"), + current_end_year_long:, + ) + end + end + def previous_collection_start_suffix previous_collection_start_date.year % 100 end diff --git a/config/locales/en.yml b/config/locales/en.yml index 77c3bd997..5e55fb16f 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -136,9 +136,6 @@ en: above_min: "%{field} must be at least %{min}" date: invalid_date: "Enter a date in the correct format, for example 31 1 2022" - outside_collection_window: - crossover_period: "Enter a date within the %{previous_collection} or %{current_collection} financial years, which is between 1st April %{previous_collection_start_year} and 31st March %{current_collection_end_year}" - not_crossover_period: "Enter a date within the %{current_collection} financial year, which is between 1st April %{current_collection_start_year} and 31st March %{current_collection_end_year}" postcode: "Enter a postcode in the correct format, for example AA1 1AA" location_admin_district: "Select a local authority" email: @@ -158,6 +155,10 @@ en: "Enter a date within the %{previous_start_year_short}/%{previous_end_year_short} or %{previous_end_year_short}/%{current_end_year_short} financial years, which is between %{previous_start_year_long} and %{current_end_year_long}" startdate: + current_financial_year: + Enter a date within the %{current_start_year_short}/%{current_end_year_short} financial year, which is between %{current_start_year_long} and %{current_end_year_long} + previous_and_current_financial_year: + "Enter a date within the %{previous_start_year_short}/%{previous_end_year_short} or %{previous_end_year_short}/%{current_end_year_short} financial years, which is between %{previous_start_year_long} and %{current_end_year_long}" later_than_14_days_after: "The tenancy start date must not be later than 14 days from today’s date" before_scheme_end_date: "The tenancy start date must be before the end date for this supported housing scheme" after_void_date: "Enter a tenancy start date that is after the void date" diff --git a/spec/models/validations/date_validations_spec.rb b/spec/models/validations/date_validations_spec.rb index f6b4fc1bf..07f10b25b 100644 --- a/spec/models/validations/date_validations_spec.rb +++ b/spec/models/validations/date_validations_spec.rb @@ -19,13 +19,13 @@ RSpec.describe Validations::DateValidations do it "cannot be before the first collection window start date" do record.startdate = Time.zone.local(2021, 1, 1) date_validator.validate_startdate(record) - expect(record.errors["startdate"]).to include(match "Enter a date within the 21/22 or 22/23 financial years, which is between 1st April 2021 and 31st March 2023") + expect(record.errors["startdate"]).to include(match "Enter a date within the 21/22 or 22/23 financial years, which is between 1st April 2021 and 1st July 2023") end it "cannot be after the second collection window end date" do record.startdate = Time.zone.local(2023, 7, 1, 6) date_validator.validate_startdate(record) - expect(record.errors["startdate"]).to include(match "Enter a date within the 21/22 or 22/23 financial years, which is between 1st April 2021 and 31st March 2023") + expect(record.errors["startdate"]).to include(match "Enter a date within the 21/22 or 22/23 financial years, which is between 1st April 2021 and 1st July 2023") end end @@ -38,13 +38,13 @@ RSpec.describe Validations::DateValidations do it "cannot be before the first collection window start date" do record.startdate = Time.zone.local(2022, 1, 1) date_validator.validate_startdate(record) - expect(record.errors["startdate"]).to include(match "Enter a date within the 22/23 financial year, which is between 1st April 2022 and 31st March 2023") + expect(record.errors["startdate"]).to include(match "Enter a date within the 22/23 financial year, which is between 1st April 2022 and 1st July 2023") end it "cannot be after the second collection window end date" do record.startdate = Time.zone.local(2023, 7, 1, 6) date_validator.validate_startdate(record) - expect(record.errors["startdate"]).to include(match "Enter a date within the 22/23 financial year, which is between 1st April 2022 and 31st March 2023") + expect(record.errors["startdate"]).to include(match "Enter a date within the 22/23 financial year, which is between 1st April 2022 and 1st July 2023") end end end @@ -59,13 +59,13 @@ RSpec.describe Validations::DateValidations do it "cannot be before the first collection window start date" do record.startdate = Time.zone.local(2022, 1, 1) date_validator.validate_startdate(record) - expect(record.errors["startdate"]).to include(match "Enter a date within the 22/23 or 23/24 financial years, which is between 1st April 2022 and 31st March 2024") + expect(record.errors["startdate"]).to include(match "Enter a date within the 22/23 or 23/24 financial years, which is between 1st April 2022 and 9th July 2024") end it "cannot be after the second collection window end date" do record.startdate = Time.zone.local(2024, 7, 1, 6) date_validator.validate_startdate(record) - expect(record.errors["startdate"]).to include(match "Enter a date within the 22/23 or 23/24 financial years, which is between 1st April 2022 and 31st March 2024") + expect(record.errors["startdate"]).to include(match "Enter a date within the 22/23 or 23/24 financial years, which is between 1st April 2022 and 9th July 2024") end end @@ -78,13 +78,13 @@ RSpec.describe Validations::DateValidations do it "cannot be before the first collection window start date" do record.startdate = Time.zone.local(2023, 1, 1) date_validator.validate_startdate(record) - expect(record.errors["startdate"]).to include(match "Enter a date within the 23/24 financial year, which is between 1st April 2023 and 31st March 2024") + expect(record.errors["startdate"]).to include(match "Enter a date within the 23/24 financial year, which is between 1st April 2023 and 9th July 2024") end it "cannot be after the second collection window end date" do record.startdate = Time.zone.local(2024, 7, 1, 6) date_validator.validate_startdate(record) - expect(record.errors["startdate"]).to include(match "Enter a date within the 23/24 financial year, which is between 1st April 2023 and 31st March 2024") + expect(record.errors["startdate"]).to include(match "Enter a date within the 23/24 financial year, which is between 1st April 2023 and 9th July 2024") end end end