From a08c1f27a14ef9e4c1a16a0c9ae5c2aa4dd79b25 Mon Sep 17 00:00:00 2001 From: Arthur Campbell Date: Wed, 25 Jan 2023 16:40:47 +0000 Subject: [PATCH] add hard validation that percentage owned after a staircasing transaction cannot be less than the percentage bought in that transaction, with associated tests --- .../pages/staircase_bought_value_check.rb | 4 +-- .../sales/financial_validations.rb | 8 ++++++ config/locales/en.yml | 3 +++ .../sales/financial_validations_spec.rb | 27 +++++++++++++++++++ 4 files changed, 40 insertions(+), 2 deletions(-) diff --git a/app/models/form/sales/pages/staircase_bought_value_check.rb b/app/models/form/sales/pages/staircase_bought_value_check.rb index 98762dff2..b7b8178ff 100644 --- a/app/models/form/sales/pages/staircase_bought_value_check.rb +++ b/app/models/form/sales/pages/staircase_bought_value_check.rb @@ -13,8 +13,8 @@ class Form::Sales::Pages::StaircaseBoughtValueCheck < ::Form::Page { "key" => "stairbought", "i18n_template" => "percentage", - } - ] + }, + ], } @informative_text = {} end diff --git a/app/models/validations/sales/financial_validations.rb b/app/models/validations/sales/financial_validations.rb index 233f32b6b..b6b478478 100644 --- a/app/models/validations/sales/financial_validations.rb +++ b/app/models/validations/sales/financial_validations.rb @@ -27,4 +27,12 @@ module Validations::Sales::FinancialValidations record.errors.add :cashdis, I18n.t("validations.financial.cash_discount_invalid") end end + + def validate_percentage_bought_not_greater_than_percentage_owned(record) + return unless record.stairbought && record.stairowned + + if record.stairbought > record.stairowned + record.errors.add :stairowned, I18n.t("validations.financial.staircasing.percentage_bought_must_be_greater_than_percentage_owned") + end + end end diff --git a/config/locales/en.yml b/config/locales/en.yml index 1ec13a304..2775b77c6 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -277,6 +277,9 @@ en: out_of_range: "Household rent and other charges must be between %{min_chcharge} and %{max_chcharge} if paying %{period}" not_provided: "Enter how much rent and other charges the household pays %{period}" 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" + household: reasonpref: not_homeless: "Answer cannot be ‘homeless or about to lose their home’ as the tenant was not homeless immediately prior to this letting" diff --git a/spec/models/validations/sales/financial_validations_spec.rb b/spec/models/validations/sales/financial_validations_spec.rb index 4bc19afbe..38ccaced9 100644 --- a/spec/models/validations/sales/financial_validations_spec.rb +++ b/spec/models/validations/sales/financial_validations_spec.rb @@ -99,4 +99,31 @@ RSpec.describe Validations::Sales::FinancialValidations do expect(record.errors["cashdis"]).to be_empty end end + + describe "#validate_percentage_bought_not_greater_than_percentage_owned" do + let(:record) { FactoryBot.create(:sales_log) } + + 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_greater_than_percentage_owned(record) + expect(record.errors["stairbought"]).to be_empty + expect(record.errors["stairowned"]).to be_empty + 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_greater_than_percentage_owned(record) + expect(record.errors["stairbought"]).to be_empty + expect(record.errors["stairowned"]).to be_empty + end + + it "adds 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_greater_than_percentage_owned(record) + expect(record.errors["stairowned"]).to include(match I18n.t("validations.financial.staircasing.percentage_bought_must_be_greater_than_percentage_owned")) + end + end end