From 53028894174250faf6ce1bd06a39b70a605cb791 Mon Sep 17 00:00:00 2001 From: kosiakkatrina <54268893+kosiakkatrina@users.noreply.github.com> Date: Mon, 5 Feb 2024 15:02:42 +0000 Subject: [PATCH] CLDC-3173 Add equity and staircase validation (#2201) * Add equity and staircase validation * Update BU tests --- .../sales/financial_validations.rb | 12 +++ config/locales/en.yml | 1 + .../sales/financial_validations_spec.rb | 79 +++++++++++++++++++ .../sales/year2024/row_parser_spec.rb | 2 +- 4 files changed, 93 insertions(+), 1 deletion(-) 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 8d23821a4..b9638ba94 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 diff --git a/spec/services/bulk_upload/sales/year2024/row_parser_spec.rb b/spec/services/bulk_upload/sales/year2024/row_parser_spec.rb index fa5ad2ee7..25e6a33f9 100644 --- a/spec/services/bulk_upload/sales/year2024/row_parser_spec.rb +++ b/spec/services/bulk_upload/sales/year2024/row_parser_spec.rb @@ -88,7 +88,7 @@ RSpec.describe BulkUpload::Sales::Year2024::RowParser do field_85: "5", field_86: "1", field_87: "10", - field_88: "11", + field_88: "40", field_89: "1", field_91: "30", field_92: "3",