From ef90eef6440ad8abbcc83852d77e549994530181 Mon Sep 17 00:00:00 2001 From: Arthur Campbell Date: Wed, 25 Jan 2023 17:21:31 +0000 Subject: [PATCH] add compound validation to ensure that logs of type older person shared ownership may not have a stairbought value of greater than 75% \n associated tests included --- app/models/sales_log.rb | 4 +++ .../sales/financial_validations.rb | 9 ++++++ config/locales/en.yml | 1 + .../sales/financial_validations_spec.rb | 32 +++++++++++++++++++ 4 files changed, 46 insertions(+) diff --git a/app/models/sales_log.rb b/app/models/sales_log.rb index 7a57be8ee..73b460ee9 100644 --- a/app/models/sales_log.rb +++ b/app/models/sales_log.rb @@ -136,6 +136,10 @@ class SalesLog < Log type == 18 end + def is_older_persons_shared_ownership? + type == 24 + end + def ppostcode_full=(postcode) if postcode super UKPostcode.parse(postcode).to_s diff --git a/app/models/validations/sales/financial_validations.rb b/app/models/validations/sales/financial_validations.rb index b6b478478..63f1e0f4e 100644 --- a/app/models/validations/sales/financial_validations.rb +++ b/app/models/validations/sales/financial_validations.rb @@ -35,4 +35,13 @@ module Validations::Sales::FinancialValidations record.errors.add :stairowned, I18n.t("validations.financial.staircasing.percentage_bought_must_be_greater_than_percentage_owned") end end + + def validate_percentage_owned_not_too_much_if_older_person(record) + return unless record.is_older_persons_shared_ownership? && record.stairowned + + if record.stairowned > 75 + record.errors.add :stairowned, I18n.t("validations.financial.staircasing.older_person_percentage_owned_maximum_75") + record.errors.add :type, I18n.t("validations.financial.staircasing.older_person_percentage_owned_maximum_75") + end + end end diff --git a/config/locales/en.yml b/config/locales/en.yml index 2775b77c6..98f57ae73 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -279,6 +279,7 @@ en: cash_discount_invalid: "Cash discount must be £0 - £999,999" staircasing: percentage_bought_must_be_greater_than_percentage_owned: "Total percentage buyer now owns must be more than percentage bought in this transaction" + older_person_percentage_owned_maximum_75: "Percentage cannot be above 75% under Older Person's Shared Ownership" household: reasonpref: diff --git a/spec/models/validations/sales/financial_validations_spec.rb b/spec/models/validations/sales/financial_validations_spec.rb index 38ccaced9..88f36943f 100644 --- a/spec/models/validations/sales/financial_validations_spec.rb +++ b/spec/models/validations/sales/financial_validations_spec.rb @@ -126,4 +126,36 @@ RSpec.describe Validations::Sales::FinancialValidations do expect(record.errors["stairowned"]).to include(match I18n.t("validations.financial.staircasing.percentage_bought_must_be_greater_than_percentage_owned")) end end + + describe "#validate_percentage_owned_not_too_much_if_older_person" do + let(:record) { FactoryBot.create(:sales_log) } + + context "when log type is not older persons shared ownership" do + it "does not add an error when percentage owned after staircasing transaction exceeds 75%" do + record.type = 2 + record.stairowned = 80 + financial_validator.validate_percentage_owned_not_too_much_if_older_person(record) + expect(record.errors["stairowned"]).to be_empty + expect(record.errors["type"]).to be_empty + end + end + + context "when log type is older persons shared ownership" do + it "does not add an error when percentage owned after staircasing transaction is less than 75%" do + record.type = 24 + record.stairowned = 50 + financial_validator.validate_percentage_owned_not_too_much_if_older_person(record) + expect(record.errors["stairowned"]).to be_empty + expect(record.errors["type"]).to be_empty + end + + it "adds an error when percentage owned after staircasing transaction exceeds 75%" do + record.type = 24 + record.stairowned = 90 + financial_validator.validate_percentage_owned_not_too_much_if_older_person(record) + expect(record.errors["stairowned"]).to include(match I18n.t("validations.financial.staircasing.older_person_percentage_owned_maximum_75")) + expect(record.errors["type"]).to include(match I18n.t("validations.financial.staircasing.older_person_percentage_owned_maximum_75")) + end + end + end end