From 06b9669aa03255de40fc46a1c698f26c39c01c12 Mon Sep 17 00:00:00 2001 From: Kat Date: Wed, 31 Jan 2024 14:54:55 +0000 Subject: [PATCH] Validate stairowned is not equal stairbought --- .../sales/financial_validations.rb | 10 ++++ config/locales/en.yml | 1 + .../sales/financial_validations_spec.rb | 47 +++++++++++++++++++ 3 files changed, 58 insertions(+) diff --git a/app/models/validations/sales/financial_validations.rb b/app/models/validations/sales/financial_validations.rb index 410f6d250..476878c4a 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: record.field_formatted_as_currency("stairbought"), stairowned: record.field_formatted_as_currency("stairowned")) + record.errors.add :stairowned, I18n.t("validations.financial.staircasing.percentage_bought_equal_percentage_owned", stairbought: record.field_formatted_as_currency("stairbought"), stairowned: record.field_formatted_as_currency("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 8838c6a80..29b4b7bc3 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 68cf19a14..cbf34afc9 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.00 and the percentage owned in total is £30.00. These figures cannot be the same.") + expect(record.errors["stairbought"]).to include("The percentage bought is £30.00 and the percentage owned in total is £30.00. 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) }