diff --git a/app/models/validations/sales/sale_information_validations.rb b/app/models/validations/sales/sale_information_validations.rb index 024272ac3..86295ceb3 100644 --- a/app/models/validations/sales/sale_information_validations.rb +++ b/app/models/validations/sales/sale_information_validations.rb @@ -54,6 +54,20 @@ module Validations::Sales::SaleInformationValidations end end + def validate_outright_sale_value_matches_mortgage_plus_deposit(record) + return unless record.saledate && record.form.start_year_after_2024? + return unless record.outright_sale? + return unless record.mortgage_used? && record.mortgage + return unless record.deposit && record.value + + if over_tolerance?(record.mortgage_and_deposit_total, record.value, 1) + %i[mortgageused mortgage value deposit].each do |field| + record.errors.add field, I18n.t("validations.sale_information.outright_sale_value", mortgage_and_deposit_total: record.field_formatted_as_currency("mortgage_and_deposit_total"), value: record.field_formatted_as_currency("value")) + end + record.errors.add :ownershipsch, :skip_bu_error, message: I18n.t("validations.sale_information.outright_sale_value", mortgage_and_deposit_total: record.field_formatted_as_currency("mortgage_and_deposit_total"), value: record.field_formatted_as_currency("value")) + end + end + def validate_basic_monthly_rent(record) return unless record.mrent && record.ownershipsch && record.type diff --git a/config/locales/en.yml b/config/locales/en.yml index 6a105b213..48f1168ca 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -633,6 +633,7 @@ en: previous_property_type: property_type_bedsit: "A bedsit cannot have more than 1 bedroom" discounted_ownership_value: "The mortgage, deposit, and grant when added together is %{mortgage_deposit_and_grant_total}, and the purchase purchase price times by the discount is %{value_with_discount}. These figures should be the same" + outright_sale_value: "The mortgage and deposit when added together is %{mortgage_and_deposit_total}, and the purchase price is %{value}. These figures should be the same." monthly_rent: higher_than_expected: "Basic monthly rent must be between £0.00 and £9,999.00" grant: diff --git a/spec/models/validations/sales/sale_information_validations_spec.rb b/spec/models/validations/sales/sale_information_validations_spec.rb index 4859aca2d..969f2c16f 100644 --- a/spec/models/validations/sales/sale_information_validations_spec.rb +++ b/spec/models/validations/sales/sale_information_validations_spec.rb @@ -505,6 +505,94 @@ RSpec.describe Validations::Sales::SaleInformationValidations do end end + describe "#validate_outright_sale_value_matches_mortgage_plus_deposit" do + context "with a 2024 outright sale log" do + let(:record) { FactoryBot.build(:sales_log, value: 300_000, ownershipsch: 3, saledate: Time.zone.local(2024, 5, 1)) } + + context "when a mortgage is used" do + before do + record.mortgageused = 1 + end + + context "and the mortgage plus deposit match the value" do + before do + record.mortgage = 200_000 + record.deposit = 100_000 + end + + it "does not add errors" do + sale_information_validator.validate_outright_sale_value_matches_mortgage_plus_deposit(record) + expect(record.errors).to be_empty + end + end + + context "and the mortgage plus deposit don't match the value" do + before do + record.mortgage = 100_000 + record.deposit = 100_000 + end + + it "adds errors" do + sale_information_validator.validate_outright_sale_value_matches_mortgage_plus_deposit(record) + expect(record.errors["mortgageused"]).to include("The mortgage and deposit when added together is £200,000.00, and the purchase price is £300,000.00. These figures should be the same.") + expect(record.errors["mortgage"]).to include("The mortgage and deposit when added together is £200,000.00, and the purchase price is £300,000.00. These figures should be the same.") + expect(record.errors["deposit"]).to include("The mortgage and deposit when added together is £200,000.00, and the purchase price is £300,000.00. These figures should be the same.") + expect(record.errors["value"]).to include("The mortgage and deposit when added together is £200,000.00, and the purchase price is £300,000.00. These figures should be the same.") + expect(record.errors["ownershipsch"]).to include("The mortgage and deposit when added together is £200,000.00, and the purchase price is £300,000.00. These figures should be the same.") + end + end + + context "and deposit is not provided" do + before do + record.mortgage = 100_000 + record.deposit = nil + end + + it "does not add errors" do + sale_information_validator.validate_outright_sale_value_matches_mortgage_plus_deposit(record) + expect(record.errors).to be_empty + end + end + + context "and mortgage is not provided" do + before do + record.mortgage = nil + record.deposit = 100_000 + end + + it "does not add errors" do + sale_information_validator.validate_outright_sale_value_matches_mortgage_plus_deposit(record) + expect(record.errors).to be_empty + end + end + end + end + + context "with a 2024 log that is not an outright sale" do + let(:record) { FactoryBot.build(:sales_log, value: 300_000, ownershipsch: 2, saledate: Time.zone.local(2024, 5, 1)) } + + it "does not add errors" do + record.mortgageused = 1 + record.mortgage = 100_000 + record.deposit = 100_000 + sale_information_validator.validate_outright_sale_value_matches_mortgage_plus_deposit(record) + expect(record.errors).to be_empty + end + end + + context "with a 2023 outright sale log" do + let(:record) { FactoryBot.build(:sales_log, value: 300_000, ownershipsch: 3, saledate: Time.zone.local(2023, 5, 1)) } + + it "does not add errors" do + record.mortgageused = 1 + record.mortgage = 100_000 + record.deposit = 100_000 + sale_information_validator.validate_outright_sale_value_matches_mortgage_plus_deposit(record) + expect(record.errors).to be_empty + end + end + end + describe "#validate_basic_monthly_rent" do context "when within permitted bounds" do let(:record) { build(:sales_log, mrent: 9998, ownershipsch: 1, type: 2) }