diff --git a/app/models/validations/financial_validations.rb b/app/models/validations/financial_validations.rb index d3b173a96..b6fbddf64 100644 --- a/app/models/validations/financial_validations.rb +++ b/app/models/validations/financial_validations.rb @@ -133,10 +133,18 @@ private end def validate_rent_range(record) - rent_range = LaRentRange.find_by(start_year: record.year, la: record.la, beds: record.beds, lettype: record.lettype) + return if record.startdate.blank? + + window_end_date = Time.zone.local(record.year, 4, 1) + collection_year = record.startdate < window_end_date ? record.year - 1 : record.year + rent_range = LaRentRange.find_by(start_year: collection_year, la: record.la, beds: record.beds, lettype: record.lettype) if rent_range.present? && !weekly_value_in_range(record, "brent", rent_range.hard_min, rent_range.hard_max) record.errors.add :brent, I18n.t("validations.financial.brent.not_in_range") + record.errors.add :beds, I18n.t("validations.financial.brent.beds.not_in_range") + record.errors.add :la, I18n.t("validations.financial.brent.la.not_in_range") + record.errors.add :rent_type, I18n.t("validations.financial.brent.rent_type.not_in_range") + record.errors.add :needstype, I18n.t("validations.financial.brent.needstype.not_in_range") end end end diff --git a/spec/models/validations/financial_validations_spec.rb b/spec/models/validations/financial_validations_spec.rb index 7ed417df5..25df5c327 100644 --- a/spec/models/validations/financial_validations_spec.rb +++ b/spec/models/validations/financial_validations_spec.rb @@ -735,6 +735,7 @@ RSpec.describe Validations::FinancialValidations do record.la = "E07000223" record.beds = 1 record.year = 2021 + record.startdate = Time.zone.local(2021, 9, 17) record.brent = 9.17 financial_validator.validate_rent_amount(record) @@ -747,17 +748,51 @@ RSpec.describe Validations::FinancialValidations do record.period = 1 record.la = "E07000223" record.beds = 1 + record.startdate = Time.zone.local(2021, 9, 17) record.year = 2021 record.brent = 200 financial_validator.validate_rent_amount(record) expect(record.errors["brent"]) .to include(match I18n.t("validations.financial.brent.not_in_range")) + expect(record.errors["beds"]) + .to include(match I18n.t("validations.financial.brent.beds.not_in_range")) + expect(record.errors["la"]) + .to include(match I18n.t("validations.financial.brent.la.not_in_range")) + expect(record.errors["rent_type"]) + .to include(match I18n.t("validations.financial.brent.rent_type.not_in_range")) + expect(record.errors["needstype"]) + .to include(match I18n.t("validations.financial.brent.needstype.not_in_range")) + end + + it "validates hard max for correct collection year" do + record.lettype = 1 + record.period = 1 + record.la = "E07000223" + record.startdate = Time.zone.local(2022, 2, 5) + record.beds = 1 + record.year = 2022 + record.month = 2 + record.day = 5 + record.brent = 200 + + financial_validator.validate_rent_amount(record) + expect(record.errors["brent"]) + .to include(match I18n.t("validations.financial.brent.not_in_range")) + expect(record.errors["beds"]) + .to include(match I18n.t("validations.financial.brent.beds.not_in_range")) + expect(record.errors["la"]) + .to include(match I18n.t("validations.financial.brent.la.not_in_range")) + expect(record.errors["rent_type"]) + .to include(match I18n.t("validations.financial.brent.rent_type.not_in_range")) + expect(record.errors["needstype"]) + .to include(match I18n.t("validations.financial.brent.needstype.not_in_range")) end it "does not error if some of the fields are missing" do record.managing_organisation.provider_type = 2 record.year = 2021 + record.startdate = Time.zone.local(2021, 9, 17) record.brent = 200 financial_validator.validate_rent_amount(record)