diff --git a/app/models/validations/sales/financial_validations.rb b/app/models/validations/sales/financial_validations.rb index 410f6d250..a4814675a 100644 --- a/app/models/validations/sales/financial_validations.rb +++ b/app/models/validations/sales/financial_validations.rb @@ -92,6 +92,19 @@ module Validations::Sales::FinancialValidations end end + def validate_discounted_ownership_value(record) + return unless record.saledate && record.form.start_year_after_2024? + return unless record.value && record.deposit && record.ownershipsch + return unless record.mortgage || record.mortgageused == 2 || record.mortgageused == 3 + return unless record.discount || record.grant || record.type == 29 + + if record.mortgage_deposit_and_grant_total != record.value_with_discount && record.discounted_ownership_sale? + %i[mortgageused mortgage value deposit ownershipsch discount grant].each do |field| + record.errors.add field, I18n.t("validations.financial.discounted_sale_value", value_with_discount: record.field_formatted_as_currency("value_with_discount")) + end + end + end + private def is_relationship_child?(relationship) diff --git a/config/locales/en.yml b/config/locales/en.yml index 8a3e8cd77..c6718856b 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -433,6 +433,7 @@ en: under_min: "The minimum initial equity stake for this type of shared ownership sale is %{min_equity}%" over_max: "The maximum initial equity stake is %{max_equity}%" mortgage: "Mortgage value cannot be £0 if a mortgage was used for the purchase of this property" + discounted_sale_value: "Mortgage, deposit, and grant total must equal %{value_with_discount}" household: reasonable_preference_reason: diff --git a/spec/models/validations/sales/financial_validations_spec.rb b/spec/models/validations/sales/financial_validations_spec.rb index 68cf19a14..57758654f 100644 --- a/spec/models/validations/sales/financial_validations_spec.rb +++ b/spec/models/validations/sales/financial_validations_spec.rb @@ -339,4 +339,244 @@ RSpec.describe Validations::Sales::FinancialValidations do end end end + + describe "#validate_discounted_ownership_value" do + let(:record) { FactoryBot.build(:sales_log, mortgage: 10_000, deposit: 5_000, value: 30_000, ownershipsch: 2, type: 8, saledate: now) } + + around do |example| + Timecop.freeze(now) do + example.run + end + Timecop.return + end + + context "with a log in the 24/25 collection year" do + let(:now) { Time.zone.local(2024, 4, 1) } + + context "when grant is routed to" do + context "and not provided" do + before do + record.grant = nil + end + + it "returns false" do + financial_validator.validate_discounted_ownership_value(record) + expect(record.errors["mortgageused"]).to be_empty + expect(record.errors["mortgage"]).to be_empty + expect(record.errors["value"]).to be_empty + expect(record.errors["deposit"]).to be_empty + expect(record.errors["ownershipsch"]).to be_empty + expect(record.errors["discount"]).to be_empty + expect(record.errors["grant"]).to be_empty + end + end + + context "and is provided" do + it "returns true if mortgage, deposit and grant total does not equal market value" do + record.grant = 3_000 + financial_validator.validate_discounted_ownership_value(record) + expect(record.errors["mortgageused"]).to include("Mortgage, deposit, and grant total must equal £30,000.00") + expect(record.errors["mortgage"]).to include("Mortgage, deposit, and grant total must equal £30,000.00") + expect(record.errors["value"]).to include("Mortgage, deposit, and grant total must equal £30,000.00") + expect(record.errors["deposit"]).to include("Mortgage, deposit, and grant total must equal £30,000.00") + expect(record.errors["ownershipsch"]).to include("Mortgage, deposit, and grant total must equal £30,000.00") + expect(record.errors["discount"]).to include("Mortgage, deposit, and grant total must equal £30,000.00") + expect(record.errors["grant"]).to include("Mortgage, deposit, and grant total must equal £30,000.00") + end + + it "returns false if mortgage, deposit and grant total equals market value" do + record.grant = 15_000 + financial_validator.validate_discounted_ownership_value(record) + expect(record.errors["mortgageused"]).to be_empty + expect(record.errors["mortgage"]).to be_empty + expect(record.errors["value"]).to be_empty + expect(record.errors["deposit"]).to be_empty + expect(record.errors["ownershipsch"]).to be_empty + expect(record.errors["discount"]).to be_empty + expect(record.errors["grant"]).to be_empty + end + end + end + + context "when discount is routed to" do + let(:record) { FactoryBot.build(:sales_log, mortgage: 10_000, deposit: 5_000, value: 30_000, ownershipsch: 2, type: 9, saledate: now) } + + context "and not provided" do + before do + record.discount = nil + end + + it "returns false" do + financial_validator.validate_discounted_ownership_value(record) + expect(record.errors["mortgageused"]).to be_empty + expect(record.errors["mortgage"]).to be_empty + expect(record.errors["value"]).to be_empty + expect(record.errors["deposit"]).to be_empty + expect(record.errors["ownershipsch"]).to be_empty + expect(record.errors["discount"]).to be_empty + expect(record.errors["grant"]).to be_empty + end + end + + context "and is provided" do + it "returns true if mortgage and deposit total does not equal market value - discount" do + record.discount = 10 + financial_validator.validate_discounted_ownership_value(record) + expect(record.errors["mortgageused"]).to include("Mortgage, deposit, and grant total must equal £27,000.00") + expect(record.errors["mortgage"]).to include("Mortgage, deposit, and grant total must equal £27,000.00") + expect(record.errors["value"]).to include("Mortgage, deposit, and grant total must equal £27,000.00") + expect(record.errors["deposit"]).to include("Mortgage, deposit, and grant total must equal £27,000.00") + expect(record.errors["ownershipsch"]).to include("Mortgage, deposit, and grant total must equal £27,000.00") + expect(record.errors["discount"]).to include("Mortgage, deposit, and grant total must equal £27,000.00") + expect(record.errors["grant"]).to include("Mortgage, deposit, and grant total must equal £27,000.00") + end + + it "returns false if mortgage and deposit total equals market value - discount" do + record.discount = 50 + financial_validator.validate_discounted_ownership_value(record) + expect(record.errors["mortgageused"]).to be_empty + expect(record.errors["mortgage"]).to be_empty + expect(record.errors["value"]).to be_empty + expect(record.errors["deposit"]).to be_empty + expect(record.errors["ownershipsch"]).to be_empty + expect(record.errors["discount"]).to be_empty + expect(record.errors["grant"]).to be_empty + end + end + end + + context "when neither discount nor grant is routed to" do + let(:record) { FactoryBot.build(:sales_log, mortgage: 10_000, value: 30_000, ownershipsch: 2, type: 29, saledate: now) } + + it "returns true if mortgage and deposit total does not equal market value" do + record.deposit = 2_000 + financial_validator.validate_discounted_ownership_value(record) + expect(record.errors["mortgageused"]).to include("Mortgage, deposit, and grant total must equal £30,000.00") + expect(record.errors["mortgage"]).to include("Mortgage, deposit, and grant total must equal £30,000.00") + expect(record.errors["value"]).to include("Mortgage, deposit, and grant total must equal £30,000.00") + expect(record.errors["deposit"]).to include("Mortgage, deposit, and grant total must equal £30,000.00") + expect(record.errors["ownershipsch"]).to include("Mortgage, deposit, and grant total must equal £30,000.00") + expect(record.errors["discount"]).to include("Mortgage, deposit, and grant total must equal £30,000.00") + expect(record.errors["grant"]).to include("Mortgage, deposit, and grant total must equal £30,000.00") + end + + it "returns false if mortgage and deposit total equals market value" do + record.deposit = 20_000 + financial_validator.validate_discounted_ownership_value(record) + expect(record.errors["mortgageused"]).to be_empty + expect(record.errors["mortgage"]).to be_empty + expect(record.errors["value"]).to be_empty + expect(record.errors["deposit"]).to be_empty + expect(record.errors["ownershipsch"]).to be_empty + expect(record.errors["discount"]).to be_empty + expect(record.errors["grant"]).to be_empty + end + end + + context "when mortgage is routed to" do + let(:record) { FactoryBot.build(:sales_log, mortgageused: 1, deposit: 5_000, grant: 3_000, value: 20_000, discount: 10, ownershipsch: 2, saledate: now) } + + context "and not provided" do + before do + record.mortgage = nil + end + + it "returns false" do + financial_validator.validate_discounted_ownership_value(record) + expect(record.errors["mortgageused"]).to be_empty + expect(record.errors["mortgage"]).to be_empty + expect(record.errors["value"]).to be_empty + expect(record.errors["deposit"]).to be_empty + expect(record.errors["ownershipsch"]).to be_empty + expect(record.errors["discount"]).to be_empty + expect(record.errors["grant"]).to be_empty + end + end + + context "and is provided" do + it "returns true if mortgage, grant and deposit total does not equal market value - discount" do + record.mortgage = 10 + financial_validator.validate_discounted_ownership_value(record) + expect(record.errors["mortgageused"]).to include("Mortgage, deposit, and grant total must equal £18,000.00") + expect(record.errors["mortgage"]).to include("Mortgage, deposit, and grant total must equal £18,000.00") + expect(record.errors["value"]).to include("Mortgage, deposit, and grant total must equal £18,000.00") + expect(record.errors["deposit"]).to include("Mortgage, deposit, and grant total must equal £18,000.00") + expect(record.errors["ownershipsch"]).to include("Mortgage, deposit, and grant total must equal £18,000.00") + expect(record.errors["discount"]).to include("Mortgage, deposit, and grant total must equal £18,000.00") + expect(record.errors["grant"]).to include("Mortgage, deposit, and grant total must equal £18,000.00") + end + + it "returns false if mortgage, grant and deposit total equals market value - discount" do + record.mortgage = 10_000 + financial_validator.validate_discounted_ownership_value(record) + expect(record.errors["mortgageused"]).to be_empty + expect(record.errors["mortgage"]).to be_empty + expect(record.errors["value"]).to be_empty + expect(record.errors["deposit"]).to be_empty + expect(record.errors["ownershipsch"]).to be_empty + expect(record.errors["discount"]).to be_empty + expect(record.errors["grant"]).to be_empty + end + end + end + + context "when mortgage is not routed to" do + let(:record) { FactoryBot.build(:sales_log, mortgageused: 2, deposit: 5_000, grant: 3_000, value: 20_000, discount: 10, ownershipsch: 2, saledate: now) } + + it "returns true if grant and deposit total does not equal market value - discount" do + financial_validator.validate_discounted_ownership_value(record) + expect(record.errors["mortgageused"]).to include("Mortgage, deposit, and grant total must equal £18,000.00") + expect(record.errors["mortgage"]).to include("Mortgage, deposit, and grant total must equal £18,000.00") + expect(record.errors["value"]).to include("Mortgage, deposit, and grant total must equal £18,000.00") + expect(record.errors["deposit"]).to include("Mortgage, deposit, and grant total must equal £18,000.00") + expect(record.errors["ownershipsch"]).to include("Mortgage, deposit, and grant total must equal £18,000.00") + expect(record.errors["discount"]).to include("Mortgage, deposit, and grant total must equal £18,000.00") + expect(record.errors["grant"]).to include("Mortgage, deposit, and grant total must equal £18,000.00") + end + + it "returns false if mortgage, grant and deposit total equals market value - discount" do + record.grant = 13_000 + financial_validator.validate_discounted_ownership_value(record) + expect(record.errors["mortgageused"]).to be_empty + expect(record.errors["mortgage"]).to be_empty + expect(record.errors["value"]).to be_empty + expect(record.errors["deposit"]).to be_empty + expect(record.errors["ownershipsch"]).to be_empty + expect(record.errors["discount"]).to be_empty + expect(record.errors["grant"]).to be_empty + end + end + + context "when ownership is not discounted" do + let(:record) { FactoryBot.build(:sales_log, mortgage: 10_000, deposit: 5_000, grant: 3_000, value: 20_000, discount: 10, ownershipsch: 1, saledate: Time.zone.local(2023, 4, 1)) } + + it "returns false" do + financial_validator.validate_discounted_ownership_value(record) + expect(record.errors["mortgageused"]).to be_empty + expect(record.errors["mortgage"]).to be_empty + expect(record.errors["value"]).to be_empty + expect(record.errors["deposit"]).to be_empty + expect(record.errors["ownershipsch"]).to be_empty + expect(record.errors["discount"]).to be_empty + expect(record.errors["grant"]).to be_empty + end + end + + context "when it is a 2023 log" do + let(:record) { FactoryBot.build(:sales_log, mortgageused: 1, deposit: 5_000, grant: 3_000, value: 20_000, discount: 10, ownershipsch: 2, saledate: Time.zone.local(2023, 4, 1)) } + + it "returns false" do + record.mortgage = 10 + financial_validator.validate_discounted_ownership_value(record) + expect(record.errors["mortgageused"]).to be_empty + expect(record.errors["mortgage"]).to be_empty + expect(record.errors["value"]).to be_empty + expect(record.errors["deposit"]).to be_empty + expect(record.errors["ownershipsch"]).to be_empty + expect(record.errors["discount"]).to be_empty + expect(record.errors["grant"]).to be_empty + end + end + end + end end diff --git a/spec/models/validations/sales/soft_validations_spec.rb b/spec/models/validations/sales/soft_validations_spec.rb index 4d0c1024a..0209aea02 100644 --- a/spec/models/validations/sales/soft_validations_spec.rb +++ b/spec/models/validations/sales/soft_validations_spec.rb @@ -924,6 +924,15 @@ RSpec.describe Validations::Sales::SoftValidations do expect(record).not_to be_discounted_ownership_value_invalid end end + + context "when it is a 2024 log" do + let(:record) { FactoryBot.build(:sales_log, mortgageused: 1, deposit: 5_000, grant: 3_000, value: 20_000, discount: 10, ownershipsch: 2, saledate: Time.zone.local(2024, 4, 3)) } + + it "returns true if mortgage, grant and deposit total does not equal market value - discount" do + record.mortgage = 10 + expect(record).not_to be_discounted_ownership_value_invalid + end + end end describe "#buyer1_livein_wrong_for_ownership_type?" do