diff --git a/app/models/validations/sales/financial_validations.rb b/app/models/validations/sales/financial_validations.rb index f66618e16..9e30d49f3 100644 --- a/app/models/validations/sales/financial_validations.rb +++ b/app/models/validations/sales/financial_validations.rb @@ -119,6 +119,18 @@ module Validations::Sales::FinancialValidations end end + def validate_equity_less_than_staircase_difference(record) + return unless record.equity && record.stairbought && record.stairowned + return unless record.saledate && record.form.start_year_after_2024? + + if record.equity > record.stairowned - record.stairbought + formatted_equity = sprintf("%g", record.equity) + record.errors.add :equity, I18n.t("validations.financial.equity.over_stairowned_minus_stairbought", equity: formatted_equity, staircase_difference: record.stairowned - record.stairbought) + record.errors.add :stairowned, I18n.t("validations.financial.equity.over_stairowned_minus_stairbought", equity: formatted_equity, staircase_difference: record.stairowned - record.stairbought) + record.errors.add :stairbought, I18n.t("validations.financial.equity.over_stairowned_minus_stairbought", equity: formatted_equity, staircase_difference: record.stairowned - record.stairbought) + end + end + private def is_relationship_child?(relationship) diff --git a/config/locales/en.yml b/config/locales/en.yml index e04d9465b..cdb3ef8d7 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -433,6 +433,7 @@ en: equity: 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}%" + over_stairowned_minus_stairbought: "The initial equity stake is %{equity}% and the percentage owned in total minus the percentage bought is %{staircase_difference}%. In a staircasing transaction, the equity stake purchased cannot be larger than the percentage the buyer owns minus the percentage bought." mortgage: "Mortgage value cannot be £0 if a mortgage was used for the purchase of this property" shared_ownership_deposit: "The %{mortgage_deposit_and_discount_error_fields} added together is %{mortgage_deposit_and_discount_total}. The value times the equity percentage is %{value_times_equity}. These figures should be the same" diff --git a/spec/models/validations/sales/financial_validations_spec.rb b/spec/models/validations/sales/financial_validations_spec.rb index 00dc9f850..8a57df6b6 100644 --- a/spec/models/validations/sales/financial_validations_spec.rb +++ b/spec/models/validations/sales/financial_validations_spec.rb @@ -561,4 +561,83 @@ RSpec.describe Validations::Sales::FinancialValidations do end end end + + describe "#validate_equity_less_than_staircase_difference" do + let(:record) { FactoryBot.create(:sales_log, 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 23/24 collection year" do + let(:now) { Time.zone.local(2023, 4, 1) } + + it "does not add an error" do + record.stairbought = 2 + record.stairowned = 3 + record.equity = 2 + financial_validator.validate_equity_less_than_staircase_difference(record) + expect(record.errors).to be_empty + end + end + + context "with a log in 24/25 collection year" do + let(:now) { Time.zone.local(2024, 4, 1) } + + it "adds errors if equity is more than stairowned - stairbought" do + record.stairbought = 2 + record.stairowned = 3 + record.equity = 2 + financial_validator.validate_equity_less_than_staircase_difference(record) + expect(record.errors["equity"]).to include("The initial equity stake is 2% and the percentage owned in total minus the percentage bought is 1%. In a staircasing transaction, the equity stake purchased cannot be larger than the percentage the buyer owns minus the percentage bought.") + expect(record.errors["stairowned"]).to include("The initial equity stake is 2% and the percentage owned in total minus the percentage bought is 1%. In a staircasing transaction, the equity stake purchased cannot be larger than the percentage the buyer owns minus the percentage bought.") + expect(record.errors["stairbought"]).to include("The initial equity stake is 2% and the percentage owned in total minus the percentage bought is 1%. In a staircasing transaction, the equity stake purchased cannot be larger than the percentage the buyer owns minus the percentage bought.") + end + + it "does not add errors if equity is less than stairowned - stairbought" do + record.stairbought = 2 + record.stairowned = 10 + record.equity = 2 + financial_validator.validate_equity_less_than_staircase_difference(record) + expect(record.errors).to be_empty + end + + it "does not add errors if equity is equal stairowned - stairbought" do + record.stairbought = 2 + record.stairowned = 10 + record.equity = 8 + financial_validator.validate_equity_less_than_staircase_difference(record) + expect(record.errors).to be_empty + end + + it "does not add errors if stairbought is not given" do + record.stairbought = nil + record.stairowned = 10 + record.equity = 2 + financial_validator.validate_equity_less_than_staircase_difference(record) + expect(record.errors).to be_empty + end + + it "does not add errors if stairowned is not given" do + record.stairbought = 2 + record.stairowned = nil + record.equity = 2 + financial_validator.validate_equity_less_than_staircase_difference(record) + expect(record.errors).to be_empty + end + + it "does not add errors if equity is not given" do + record.stairbought = 2 + record.stairowned = 10 + record.equity = 0 + financial_validator.validate_equity_less_than_staircase_difference(record) + expect(record.errors).to be_empty + end + end + end end