diff --git a/app/models/sales_log.rb b/app/models/sales_log.rb index 4de9699ba..13d16a786 100644 --- a/app/models/sales_log.rb +++ b/app/models/sales_log.rb @@ -496,7 +496,7 @@ class SalesLog < Log def is_not_staircasing? staircase == 2 || staircase == 3 end - + def stairowned_100? stairowned == 100 end diff --git a/app/models/validations/sales/sale_information_validations.rb b/app/models/validations/sales/sale_information_validations.rb index 0aaa77a93..c8a4e46dc 100644 --- a/app/models/validations/sales/sale_information_validations.rb +++ b/app/models/validations/sales/sale_information_validations.rb @@ -110,4 +110,14 @@ module Validations::Sales::SaleInformationValidations end end end + + def validate_mortgage_used_and_stairbought(record) + return unless record.stairowned && record.mortgageused + return unless record.saledate && record.form.start_year_after_2024? + + if !record.stairowned_100? && record.mortgageused == 3 + record.errors.add :stairowned, I18n.t("validations.sale_information.stairowned.mortgageused_dont_know") + record.errors.add :mortgageused, I18n.t("validations.sale_information.stairowned.mortgageused_dont_know") + end + end end diff --git a/config/locales/en.yml b/config/locales/en.yml index 204a20b4e..37239d234 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -623,6 +623,8 @@ en: over_discounted_london_max: "The percentage discount multiplied by the purchase price is %{discount_value}. This figure should not be more than £136,400 for properties in London." over_discounted_max: "The percentage discount multiplied by the purchase price is %{discount_value}. This figure should not be more than £102,400 for properties outside of London." non_staircasing_mortgage: "The mortgage and deposit added together is %{mortgage_and_deposit_total} and the purchase price times by the equity is %{expected_shared_ownership_deposit_value}. These figures should be the same." + stairowned: + mortgageused_dont_know: "The percentage owned has to be 100% if the mortgage used is 'Don’t know'" 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 73243e9fa..3d323112e 100644 --- a/spec/models/validations/sales/sale_information_validations_spec.rb +++ b/spec/models/validations/sales/sale_information_validations_spec.rb @@ -756,4 +756,48 @@ RSpec.describe Validations::Sales::SaleInformationValidations do end end end + + describe "#validate_mortgage_used_and_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 + + context "when mortgageused don't know" do + let(:record) { build(:sales_log, ownershipsch: 1, type: 9, saledate: now, mortgageused: 3) } + + it "does not add an error if stairowned 100" do + record.stairowned = 100 + sale_information_validator.validate_mortgage_used_and_stairbought(record) + + expect(record.errors).to be_empty + end + + it "adds an error if stairowned is not 100" do + record.stairowned = 90 + sale_information_validator.validate_mortgage_used_and_stairbought(record) + + expect(record.errors[:stairowned]).to include("The percentage owned has to be 100% if the mortgage used is 'Don’t know'") + expect(record.errors[:mortgageused]).to include("The percentage owned has to be 100% if the mortgage used is 'Don’t know'") + end + end + + context "when the collection year is before 2024" do + let(:record) { build(:sales_log, ownershipsch: 1, type: 9, saledate: now, mortgageused: 3, stairowned: 90) } + let(:now) { Time.zone.local(2023, 4, 4) } + + it "does not add an error" do + sale_information_validator.validate_mortgage_used_and_stairbought(record) + + expect(record.errors).to be_empty + end + end + end end