From 869bb454382329ca611e9c082d1a3dd2cb0fcb9a Mon Sep 17 00:00:00 2001 From: kosiakkatrina <54268893+kosiakkatrina@users.noreply.github.com> Date: Fri, 2 Feb 2024 08:32:45 +0000 Subject: [PATCH] CLDC-3174 Validate stairowned is not equal stairbought (#2198) * Validate stairowned is not equal stairbought * Update BU tests * Update error message --- .../sales/financial_validations.rb | 10 ++++ config/locales/en.yml | 1 + .../sales/financial_validations_spec.rb | 47 +++++++++++++++++++ .../sales/year2024/row_parser_spec.rb | 2 +- 4 files changed, 59 insertions(+), 1 deletion(-) diff --git a/app/models/validations/sales/financial_validations.rb b/app/models/validations/sales/financial_validations.rb index 59b84f7e3..f66618e16 100644 --- a/app/models/validations/sales/financial_validations.rb +++ b/app/models/validations/sales/financial_validations.rb @@ -52,6 +52,16 @@ module Validations::Sales::FinancialValidations end end + def validate_percentage_bought_not_equal_percentage_owned(record) + return unless record.stairbought && record.stairowned + return unless record.saledate && record.form.start_year_after_2024? + + if record.stairbought == record.stairowned + record.errors.add :stairbought, I18n.t("validations.financial.staircasing.percentage_bought_equal_percentage_owned", stairbought: sprintf("%g", record.stairbought), stairowned: sprintf("%g", record.stairowned)) + record.errors.add :stairowned, I18n.t("validations.financial.staircasing.percentage_bought_equal_percentage_owned", stairbought: sprintf("%g", record.stairbought), stairowned: sprintf("%g", record.stairowned)) + end + end + def validate_percentage_bought_at_least_threshold(record) return unless record.stairbought && record.type diff --git a/config/locales/en.yml b/config/locales/en.yml index e136b3253..e04d9465b 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -427,6 +427,7 @@ en: staircasing: percentage_bought_must_be_greater_than_percentage_owned: "Total percentage buyer now owns must be more than percentage bought in this transaction" percentage_bought_must_be_at_least_threshold: "The minimum increase in equity while staircasing is %{threshold}%" + percentage_bought_equal_percentage_owned: "The percentage bought is %{stairbought}% and the percentage owned in total is %{stairowned}%. These figures cannot be the same." monthly_leasehold_charges: not_zero: "Monthly leasehold charges cannot be £0 if the property has monthly charges" equity: diff --git a/spec/models/validations/sales/financial_validations_spec.rb b/spec/models/validations/sales/financial_validations_spec.rb index 295b11c2c..00dc9f850 100644 --- a/spec/models/validations/sales/financial_validations_spec.rb +++ b/spec/models/validations/sales/financial_validations_spec.rb @@ -156,6 +156,53 @@ RSpec.describe Validations::Sales::FinancialValidations do end end + describe "#validate_percentage_bought_not_equal_percentage_owned" do + let(:record) { FactoryBot.create(:sales_log) } + + context "with 24/25 logs" do + before do + record.saledate = Time.zone.local(2024, 4, 3) + record.save!(validate: false) + end + + it "does not add an error if the percentage bought is less than the percentage owned" do + record.stairbought = 20 + record.stairowned = 40 + financial_validator.validate_percentage_bought_not_equal_percentage_owned(record) + expect(record.errors).to be_empty + end + + it "adds an error if the percentage bought is equal to the percentage owned" do + record.stairbought = 30 + record.stairowned = 30 + financial_validator.validate_percentage_bought_not_equal_percentage_owned(record) + expect(record.errors["stairowned"]).to include("The percentage bought is 30% and the percentage owned in total is 30%. These figures cannot be the same.") + expect(record.errors["stairbought"]).to include("The percentage bought is 30% and the percentage owned in total is 30%. These figures cannot be the same.") + end + + it "does not add an error to stairowned and not stairbought if the percentage bought is more than the percentage owned" do + record.stairbought = 50 + record.stairowned = 40 + financial_validator.validate_percentage_bought_not_equal_percentage_owned(record) + expect(record.errors).to be_empty + end + end + + context "with 23/24 logs" do + before do + record.saledate = Time.zone.local(2023, 4, 3) + record.save!(validate: false) + end + + it "does not add an error if the percentage bought is equal to the percentage owned" do + record.stairbought = 30 + record.stairowned = 30 + financial_validator.validate_percentage_bought_not_equal_percentage_owned(record) + expect(record.errors).to be_empty + end + end + end + describe "#validate_monthly_leasehold_charges" do let(:record) { FactoryBot.create(:sales_log) } 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 5280c5be2..0f4fc5898 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: "10", + field_88: "11", field_89: "1", field_91: "30", field_92: "3",