diff --git a/app/models/validations/financial_validations.rb b/app/models/validations/financial_validations.rb index 048957acf..a06328c25 100644 --- a/app/models/validations/financial_validations.rb +++ b/app/models/validations/financial_validations.rb @@ -21,14 +21,22 @@ module Validations::FinancialValidations end def validate_net_income(record) - return unless record.ecstat1 && record.weekly_net_income + if record.ecstat1 && record.weekly_net_income + if record.weekly_net_income > record.applicable_income_range.hard_max + record.errors.add :earnings, I18n.t("validations.financial.earnings.under_hard_max", hard_max: record.applicable_income_range.hard_max) + end + + if record.weekly_net_income < record.applicable_income_range.hard_min + record.errors.add :earnings, I18n.t("validations.financial.earnings.over_hard_min", hard_min: record.applicable_income_range.hard_min) + end + end - if record.weekly_net_income > record.applicable_income_range.hard_max - record.errors.add :earnings, I18n.t("validations.financial.earnings.under_hard_max", hard_max: record.applicable_income_range.hard_max) + if record.earnings.present? && record.incfreq.blank? + record.errors.add :incfreq, I18n.t("validations.financial.earnings.freq_missing") end - if record.weekly_net_income < record.applicable_income_range.hard_min - record.errors.add :earnings, I18n.t("validations.financial.earnings.over_hard_min", hard_min: record.applicable_income_range.hard_min) + if record.incfreq.present? && record.earnings.blank? + record.errors.add :earnings, I18n.t("validations.financial.earnings.earnings_missing") end end diff --git a/config/locales/en.yml b/config/locales/en.yml index c40da4bda..5d1ef486b 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -68,6 +68,8 @@ en: earnings: under_hard_max: "Net income cannot be greater than %{hard_max} given the tenant’s working situation" over_hard_min: "Net income cannot be less than %{hard_min} given the tenant’s working situation" + freq_missing: "How often the household receives this amount must be provided" + earnings_missing: "How much income the household has in total must be provided" household: reasonpref: diff --git a/spec/models/validations/financial_validations.rb b/spec/models/validations/financial_validations.rb new file mode 100644 index 000000000..1cbd1cde1 --- /dev/null +++ b/spec/models/validations/financial_validations.rb @@ -0,0 +1,24 @@ +require "rails_helper" + +RSpec.describe Validations::FinancialValidations do + subject(:financial_validator) { validator_class.new } + + let(:validator_class) { Class.new { include Validations::FinancialValidations } } + let(:record) { FactoryBot.create(:case_log) } + + describe "earnings and income frequency" do + it "when earnings are provided it validates that income frequency must be provided" do + record.earnings = 500 + record.incfreq = nil + financial_validator.validate_net_income(record) + expect(record.errors["incfreq"]).to include(match I18n.t("validations.financial.earnings.freq_missing")) + end + + it "when income frequency is provided it validates that earnings must be provided" do + record.earnings = nil + record.incfreq = "Weekly" + financial_validator.validate_net_income(record) + expect(record.errors["earnings"]).to include(match I18n.t("validations.financial.earnings.earnings_missing")) + end + end +end diff --git a/spec/models/validations/property_validations_spec.rb b/spec/models/validations/property_validations_spec.rb index 15e981036..003208cf4 100644 --- a/spec/models/validations/property_validations_spec.rb +++ b/spec/models/validations/property_validations_spec.rb @@ -1,5 +1,4 @@ require "rails_helper" -require_relative "../../request_helper" RSpec.describe Validations::PropertyValidations do subject(:property_validator) { property_validator_class.new }