diff --git a/app/models/form/sales/questions/deposit_discount.rb b/app/models/form/sales/questions/deposit_discount.rb index dd3f98939..b23a3e1b4 100644 --- a/app/models/form/sales/questions/deposit_discount.rb +++ b/app/models/form/sales/questions/deposit_discount.rb @@ -6,6 +6,7 @@ class Form::Sales::Questions::DepositDiscount < ::Form::Question @header = "How much cash discount was given through Social HomeBuy?" @type = "numeric" @min = 0 + @max = 999_999 @width = 5 @prefix = "£" @hint_text = "Enter the total cash discount given on the property being purchased through the Social HomeBuy scheme" diff --git a/app/models/validations/sales/financial_validations.rb b/app/models/validations/sales/financial_validations.rb index 04f6a1d06..116ecf301 100644 --- a/app/models/validations/sales/financial_validations.rb +++ b/app/models/validations/sales/financial_validations.rb @@ -11,4 +11,12 @@ module Validations::Sales::FinancialValidations end end 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 + end end diff --git a/config/locales/en.yml b/config/locales/en.yml index ac6a2fa7f..d84429637 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -274,6 +274,7 @@ en: carehome: 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" 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 b736878dc..7b5b8868b 100644 --- a/spec/models/validations/sales/financial_validations_spec.rb +++ b/spec/models/validations/sales/financial_validations_spec.rb @@ -53,4 +53,26 @@ RSpec.describe Validations::Sales::FinancialValidations do end 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["cashdis"]).to be_empty + end + end end