diff --git a/app/models/validations/sales/sale_information_validations.rb b/app/models/validations/sales/sale_information_validations.rb index 54d49b7e0..17fbc95b4 100644 --- a/app/models/validations/sales/sale_information_validations.rb +++ b/app/models/validations/sales/sale_information_validations.rb @@ -64,4 +64,23 @@ module Validations::Sales::SaleInformationValidations record.errors.add :grant, I18n.t("validations.sale_information.grant.out_of_range") end end + + def validate_stairbought(record) + return unless record.stairbought && record.type + return unless record.saledate && record.form.start_year_after_2024? + + max_stairbought = case record.type + when 30, 16, 28, 31, 32 + 90 + when 2, 18 + 75 + when 24 + 50 + end + + if max_stairbought && record.stairbought > max_stairbought + record.errors.add :stairbought, I18n.t("validations.sale_information.stairbought.over_max", max_stairbought:, type: record.form.get_question("type", record).answer_label(record)) + record.errors.add :type, I18n.t("validations.sale_information.stairbought.over_max", max_stairbought:, type: record.form.get_question("type", record).answer_label(record)) + end + end end diff --git a/config/locales/en.yml b/config/locales/en.yml index 7fc5f9d57..c7d8c9da2 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -617,6 +617,8 @@ en: higher_than_expected: "Basic monthly rent must be between £0.00 and £9,999.00" grant: out_of_range: "Loan, grants or subsidies must be between £9,000 and £16,000" + stairbought: + over_max: "The percentage bought in this staircasing transaction cannot be higher than %{max_stairbought}% for %{type} sales." merge_request: organisation_part_of_another_merge: "This organisation is part of another merge - select a different one" organisation_not_selected: "Select an organisation from the search list" diff --git a/spec/models/validations/sales/sale_information_validations_spec.rb b/spec/models/validations/sales/sale_information_validations_spec.rb index c890afed1..25474e55b 100644 --- a/spec/models/validations/sales/sale_information_validations_spec.rb +++ b/spec/models/validations/sales/sale_information_validations_spec.rb @@ -539,4 +539,73 @@ RSpec.describe Validations::Sales::SaleInformationValidations do end end end + + describe "#validate_stairbought" do + let(:now) { Time.zone.local(2024, 4, 4) } + + before do + Timecop.freeze(now) + Singleton.__init__(FormHandler) + end + + after do + Timecop.return + Singleton.__init__(FormHandler) + end + + [ + ["Shared Ownership (new model lease)", 30, 90], + ["Home Ownership for people with Long-Term Disabilities (HOLD)", 16, 90], + ["Rent to Buy — Shared Ownership", 28, 90], + ["Right to Shared Ownership (RtSO)", 31, 90], + ["London Living Rent — Shared Ownership", 32, 90], + ["Shared Ownership (old model lease)", 2, 75], + ["Social HomeBuy — shared ownership purchase", 18, 75], + ["Older Persons Shared Ownership", 24, 50], + ].each do |label, type, max| + context "when ownership type is #{label}" do + let(:record) { build(:sales_log, ownershipsch: 1, type:, saledate: now) } + + it "does not add an error if stairbought is under #{max}%" do + record.stairbought = max - 1 + sale_information_validator.validate_stairbought(record) + + expect(record.errors).to be_empty + end + + it "does not add an error if stairbought is #{max}%" do + record.stairbought = max + sale_information_validator.validate_stairbought(record) + + expect(record.errors).to be_empty + end + + it "does not add an error if stairbought is not given" do + record.stairbought = nil + sale_information_validator.validate_stairbought(record) + + expect(record.errors).to be_empty + end + + it "adds an error if stairbought is over #{max}%" do + record.stairbought = max + 2 + sale_information_validator.validate_stairbought(record) + + expect(record.errors[:stairbought]).to include("The percentage bought in this staircasing transaction cannot be higher than #{max}% for #{label} sales.") + expect(record.errors[:type]).to include("The percentage bought in this staircasing transaction cannot be higher than #{max}% for #{label} sales.") + end + end + end + + context "when the collection year is before 2024" do + let(:record) { build(:sales_log, ownershipsch: 1, type: 24, saledate: now, stairbought: 90) } + let(:now) { Time.zone.local(2023, 4, 4) } + + it "does not add an error" do + sale_information_validator.validate_stairbought(record) + + expect(record.errors).to be_empty + end + end + end end