diff --git a/app/models/sales_log.rb b/app/models/sales_log.rb index dae70c729..76c43b09e 100644 --- a/app/models/sales_log.rb +++ b/app/models/sales_log.rb @@ -491,4 +491,8 @@ class SalesLog < Log value * discount / 100 end + + def is_not_staircasing? + staircase == 2 || staircase == 3 + end end diff --git a/app/models/validations/sales/sale_information_validations.rb b/app/models/validations/sales/sale_information_validations.rb index c2036227a..0aaa77a93 100644 --- a/app/models/validations/sales/sale_information_validations.rb +++ b/app/models/validations/sales/sale_information_validations.rb @@ -98,4 +98,16 @@ module Validations::Sales::SaleInformationValidations end end end + + def validate_non_staircasing_mortgage(record) + return unless record.mortgage && record.value && record.deposit && record.equity + return unless record.is_not_staircasing? + return unless record.saledate && record.form.start_year_after_2024? + + if record.mortgage_and_deposit_total != record.expected_shared_ownership_deposit_value + %i[mortgage value deposit equity].each do |field| + record.errors.add field, I18n.t("validations.sale_information.non_staircasing_mortgage", mortgage_and_deposit_total: record.field_formatted_as_currency("mortgage_and_deposit_total"), expected_shared_ownership_deposit_value: record.field_formatted_as_currency("expected_shared_ownership_deposit_value")) + end + end + end end diff --git a/config/locales/en.yml b/config/locales/en.yml index cd2f373ec..204a20b4e 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -622,6 +622,7 @@ en: value: 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." 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 24f2e22ee..73243e9fa 100644 --- a/spec/models/validations/sales/sale_information_validations_spec.rb +++ b/spec/models/validations/sales/sale_information_validations_spec.rb @@ -596,7 +596,6 @@ RSpec.describe Validations::Sales::SaleInformationValidations do 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) } @@ -685,4 +684,76 @@ RSpec.describe Validations::Sales::SaleInformationValidations do end end end + + describe "#validate_non_staircasing_mortgage" do + let(:record) { FactoryBot.build(:sales_log, mortgage: 10_000, deposit: 5_000, value: 30_000, equity: 28, ownershipsch: 1, type: 30, saledate: now) } + + around do |example| + Timecop.freeze(now) do + Singleton.__init__(FormHandler) + example.run + end + Timecop.return + Singleton.__init__(FormHandler) + end + + context "with a log in the 24/25 collection year" do + let(:now) { Time.zone.local(2024, 4, 4) } + + context "when MORTGAGE + DEPOSIT does not equal VALUE * EQUITY/100 " do + context "and it is not a staircase transaction" do + before do + record.staircase = 2 + end + + it "adds an error" do + sale_information_validator.validate_non_staircasing_mortgage(record) + expect(record.errors["mortgage"]).to include("The mortgage and deposit added together is £15,000.00 and the purchase price times by the equity is £8,400.00. These figures should be the same.") + expect(record.errors["value"]).to include("The mortgage and deposit added together is £15,000.00 and the purchase price times by the equity is £8,400.00. These figures should be the same.") + expect(record.errors["deposit"]).to include("The mortgage and deposit added together is £15,000.00 and the purchase price times by the equity is £8,400.00. These figures should be the same.") + expect(record.errors["equity"]).to include("The mortgage and deposit added together is £15,000.00 and the purchase price times by the equity is £8,400.00. These figures should be the same.") + end + end + + context "and it is a staircase transaction" do + before do + record.staircase = 1 + end + + it "does not add an error" do + sale_information_validator.validate_non_staircasing_mortgage(record) + expect(record.errors["mortgage"]).to be_empty + expect(record.errors["value"]).to be_empty + expect(record.errors["deposit"]).to be_empty + expect(record.errors["equity"]).to be_empty + end + end + end + + context "when MORTGAGE + DEPOSIT equals VALUE * EQUITY/100" do + let(:record) { FactoryBot.build(:sales_log, mortgage: 10_000, staircase: 2, deposit: 5_000, value: 30_000, equity: 50, ownershipsch: 1, type: 30, saledate: now) } + + it "does not add an error" do + sale_information_validator.validate_non_staircasing_mortgage(record) + expect(record.errors["mortgage"]).to be_empty + expect(record.errors["value"]).to be_empty + expect(record.errors["deposit"]).to be_empty + expect(record.errors["equity"]).to be_empty + end + end + end + + context "when it is a 2023 log" do + let(:now) { Time.zone.local(2023, 4, 1) } + let(:record) { FactoryBot.build(:sales_log, mortgage: 10_000, staircase: 2, deposit: 5_000, value: 30_000, equity: 28, ownershipsch: 1, type: 30, saledate: now) } + + it "does not add an error" do + sale_information_validator.validate_non_staircasing_mortgage(record) + expect(record.errors["mortgage"]).to be_empty + expect(record.errors["value"]).to be_empty + expect(record.errors["deposit"]).to be_empty + expect(record.errors["equity"]).to be_empty + end + end + end end