From 65c6b52c5d370cc64ca83277ea00ba0505a6ad5d Mon Sep 17 00:00:00 2001 From: Arthur Campbell Date: Mon, 20 Mar 2023 17:11:09 +0000 Subject: [PATCH] add a validation against the case where monthly leashold charges are zero, test this, remove unnecessary validation on cash discount as this is covered by validate_numeric_min_max --- .../form/sales/questions/leasehold_charges.rb | 2 +- .../sales/financial_validations.rb | 8 +--- config/locales/en.yml | 2 + .../sales/financial_validations_spec.rb | 38 ++++++++----------- 4 files changed, 21 insertions(+), 29 deletions(-) diff --git a/app/models/form/sales/questions/leasehold_charges.rb b/app/models/form/sales/questions/leasehold_charges.rb index 697a91b26..22ed7246e 100644 --- a/app/models/form/sales/questions/leasehold_charges.rb +++ b/app/models/form/sales/questions/leasehold_charges.rb @@ -5,7 +5,7 @@ class Form::Sales::Questions::LeaseholdCharges < ::Form::Question @check_answer_label = "Monthly leasehold charges" @header = "Enter the total monthly charge" @type = "numeric" - @min = 0 + @min = 1 @width = 5 @prefix = "£" @ownershipsch = ownershipsch diff --git a/app/models/validations/sales/financial_validations.rb b/app/models/validations/sales/financial_validations.rb index 2cd06b9ba..dbd0762f7 100644 --- a/app/models/validations/sales/financial_validations.rb +++ b/app/models/validations/sales/financial_validations.rb @@ -40,12 +40,8 @@ module Validations::Sales::FinancialValidations record.errors.add :mortgage, :cannot_be_0, message: I18n.t("validations.financial.mortgage") if record.mortgage_used? && record.mortgage&.zero? end - def validate_cash_discount(record) - return unless record.cashdis - - unless record.cashdis.between?(0, 999_999) - record.errors.add :cashdis, I18n.t("validations.financial.cash_discount_invalid") - end + def validate_monthly_leasehold_charges(record) + record.errors.add :mscharge, I18n.t("validations.financial.monthly_leasehold_charges.not_zero") if record.mscharge == 0 end def validate_percentage_bought_not_greater_than_percentage_owned(record) diff --git a/config/locales/en.yml b/config/locales/en.yml index 45cbde94a..88f51874e 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -320,6 +320,8 @@ en: 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" percentage_bought_must_be_at_least_threshold: "The minimum increase in equity while staircasing is %{threshold}%" + monthly_leasehold_charges: + not_zero: "Monthly leasehold charges cannot be £0 if the property has monthly charges" 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}%" diff --git a/spec/models/validations/sales/financial_validations_spec.rb b/spec/models/validations/sales/financial_validations_spec.rb index e1eb81dea..6fc080f8a 100644 --- a/spec/models/validations/sales/financial_validations_spec.rb +++ b/spec/models/validations/sales/financial_validations_spec.rb @@ -131,28 +131,6 @@ RSpec.describe Validations::Sales::FinancialValidations do end end - describe "#validate_cash_discount" do - let(:record) { FactoryBot.create(:sales_log) } - - it "adds an error if the cash discount is below zero" do - record.cashdis = -1 - financial_validator.validate_cash_discount(record) - expect(record.errors["cashdis"]).to include(match I18n.t("validations.financial.cash_discount_invalid")) - end - - it "adds an error if the cash discount is one million or more" do - record.cashdis = 1_000_000 - financial_validator.validate_cash_discount(record) - expect(record.errors["cashdis"]).to include(match I18n.t("validations.financial.cash_discount_invalid")) - end - - it "does not add an error if the cash discount is in the expected range" do - record.cashdis = 10_000 - financial_validator.validate_cash_discount(record) - expect(record.errors).to be_empty - end - end - describe "#validate_percentage_bought_not_greater_than_percentage_owned" do let(:record) { FactoryBot.create(:sales_log) } @@ -178,6 +156,22 @@ RSpec.describe Validations::Sales::FinancialValidations do end end + describe "#validate_monthly_leasehold_charges", focus: true do + let(:record) { FactoryBot.create(:sales_log) } + + it "does not add an error if monthly leasehold charges are positive" do + record.mscharge = 2345 + financial_validator.validate_monthly_leasehold_charges(record) + expect(record.errors).to be_empty + end + + it "adds an error if monthly leasehold charges are zero" do + record.mscharge = 0 + financial_validator.validate_monthly_leasehold_charges(record) + expect(record.errors[:mscharge]).to include I18n.t("validations.financial.monthly_leasehold_charges.not_zero") + end + end + describe "#validate_percentage_bought_at_least_threshold" do let(:record) { FactoryBot.create(:sales_log) }