diff --git a/app/models/sales_log.rb b/app/models/sales_log.rb index 2285aa53d..77b83e446 100644 --- a/app/models/sales_log.rb +++ b/app/models/sales_log.rb @@ -1,6 +1,7 @@ class SalesLogValidator < ActiveModel::Validator include Validations::Sales::HouseholdValidations include Validations::SharedValidations + include Validations::Sales::FinancialValidations def validate(record) validation_methods = public_methods.select { |method| method.starts_with?("validate_") } @@ -61,4 +62,8 @@ class SalesLog < Log def unresolved false end + + def buyer_1_child? + ecstat1 == 9 + end end diff --git a/app/models/validations/sales/financial_validations.rb b/app/models/validations/sales/financial_validations.rb new file mode 100644 index 000000000..a85b274b0 --- /dev/null +++ b/app/models/validations/sales/financial_validations.rb @@ -0,0 +1,14 @@ +module Validations::Sales::FinancialValidations + # Validations methods need to be called 'validate_' to run on model save + # or 'validate_' to run on submit as well + + def validate_income1(record) + if record.ecstat1 && record.income1 && record.ownershipsch == 1 + if record.buyer_1_child? && record.income1.positive? + record.errors.add :income1, I18n.t("validations.financial.income1.child_income") + elsif record.income1 > 80_000 + record.errors.add :income1, I18n.t("validations.financial.income1.over_hard_max", hard_max: 80_000) + end + end + end +end diff --git a/config/locales/en.yml b/config/locales/en.yml index 05a828dea..e4724af35 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -194,6 +194,9 @@ en: under_hard_min: "Net income cannot be less than £%{hard_min} per week given the tenant’s working situation" freq_missing: "Select how often the household receives income" earnings_missing: "Enter how much income the household has in total" + income1: + over_hard_max: "Income must be lower than £%{hard_max}" + child_income: "A child‘s income must be 0" negative_currency: "Enter an amount above 0" rent: less_than_shortfall: "Enter an amount that is more than the shortfall in basic rent" diff --git a/spec/models/validations/sales/financial_validations_spec.rb b/spec/models/validations/sales/financial_validations_spec.rb new file mode 100644 index 000000000..2e9c14bd6 --- /dev/null +++ b/spec/models/validations/sales/financial_validations_spec.rb @@ -0,0 +1,38 @@ +require "rails_helper" + +RSpec.describe Validations::Sales::FinancialValidations do + subject(:financial_validator) { validator_class.new } + + let(:validator_class) { Class.new { include Validations::Sales::FinancialValidations } } + + describe "income validations" do + let(:record) { FactoryBot.create(:sales_log, ownershipsch: 1) } + + context "with shared ownership" do + (0..8).each do |ecstat| + it "adds an error when buyer 1 income is over hard max for ecstat #{ecstat}" do + record.income1 = 85_000 + record.ecstat1 = ecstat + financial_validator.validate_income1(record) + expect(record.errors["income1"]) + .to include(match I18n.t("validations.financial.income1.over_hard_max", hard_max: 80_000)) + end + end + + it "validates that the income is within the expected range for the tenant’s employment status" do + record.income1 = 75_000 + record.ecstat1 = 1 + financial_validator.validate_income1(record) + expect(record.errors["income1"]).to be_empty + end + + it "validates income correctly if the ecstat is child" do + record.income1 = 1 + record.ecstat1 = 9 + financial_validator.validate_income1(record) + expect(record.errors["income1"]) + .to include(match I18n.t("validations.financial.income1.child_income")) + end + end + end +end