Browse Source

CLDC-3110: Make income validations apply to all ecstat fields and take account of details not known

pull/2136/head
Rachael Booth 2 years ago
parent
commit
8c8f8d2b7c
  1. 23
      app/models/lettings_log.rb
  2. 7
      app/models/validations/financial_validations.rb
  3. 4
      app/models/validations/soft_validations.rb
  4. 29
      spec/models/lettings_log_spec.rb
  5. 16
      spec/models/validations/financial_validations_spec.rb

23
app/models/lettings_log.rb

@ -213,14 +213,28 @@ class LettingsLog < Log
format_as_currency((field_value * 52) / num_of_weeks) format_as_currency((field_value * 52) / num_of_weeks)
end end
def all_relevant_ecstat_provided
return false unless ecstat1
if hhmemb && hhmemb > 1
(2..hhmemb).each do |person_index|
next if details_not_known_for_person?(person_index)
return false unless self["ecstat#{person_index}"]
end
end
true
end
def applicable_income_range def applicable_income_range
return unless ecstat1 return unless all_relevant_ecstat_provided
range = ALLOWED_INCOME_RANGES[ecstat1].clone range = ALLOWED_INCOME_RANGES[ecstat1].clone
(2..8).each do |person_index| if hhmemb && hhmemb > 1
ecstat = self["ecstat#{person_index}"] (2..hhmemb).each do |person_index|
next unless ecstat ecstat = details_not_known_for_person?(person_index) ? 10 : self["ecstat#{person_index}"]
person_range = ALLOWED_INCOME_RANGES[ecstat] person_range = ALLOWED_INCOME_RANGES[ecstat]
range.soft_min += person_range.soft_min range.soft_min += person_range.soft_min
@ -228,6 +242,7 @@ class LettingsLog < Log
range.soft_max += person_range.soft_max range.soft_max += person_range.soft_max
range.hard_max += person_range.hard_max range.hard_max += person_range.hard_max
end end
end
range range
end end

7
app/models/validations/financial_validations.rb

@ -24,7 +24,7 @@ module Validations::FinancialValidations
end end
def validate_net_income(record) def validate_net_income(record)
if record.ecstat1 && record.weekly_net_income if record.all_relevant_ecstat_provided && record.weekly_net_income
if record.weekly_net_income > record.applicable_income_range.hard_max if record.weekly_net_income > record.applicable_income_range.hard_max
hard_max = format_as_currency(record.applicable_income_range.hard_max) hard_max = format_as_currency(record.applicable_income_range.hard_max)
frequency = record.form.get_question("incfreq", record).label_from_value(record.incfreq).downcase frequency = record.form.get_question("incfreq", record).label_from_value(record.incfreq).downcase
@ -33,12 +33,15 @@ module Validations::FinancialValidations
:over_hard_max, :over_hard_max,
message: I18n.t("validations.financial.earnings.over_hard_max", hard_max:), message: I18n.t("validations.financial.earnings.over_hard_max", hard_max:),
) )
ecstat_fields = %i[ecstat1 ecstat2 ecstat3 ecstat4 ecstat5 ecstat6 ecstat7 ecstat8]
ecstat_fields.each do |field|
record.errors.add( record.errors.add(
:ecstat1, field,
:over_hard_max, :over_hard_max,
message: I18n.t("validations.financial.ecstat.over_hard_max", earnings: format_as_currency(record.earnings), frequency:), message: I18n.t("validations.financial.ecstat.over_hard_max", earnings: format_as_currency(record.earnings), frequency:),
) )
end end
end
if record.weekly_net_income < record.applicable_income_range.hard_min if record.weekly_net_income < record.applicable_income_range.hard_min
hard_min = format_as_currency(record.applicable_income_range.hard_min) hard_min = format_as_currency(record.applicable_income_range.hard_min)

4
app/models/validations/soft_validations.rb

@ -14,13 +14,13 @@ module Validations::SoftValidations
}.freeze }.freeze
def net_income_in_soft_max_range? def net_income_in_soft_max_range?
return unless weekly_net_income && ecstat1 return unless weekly_net_income && all_relevant_ecstat_provided
weekly_net_income.between?(applicable_income_range.soft_max, applicable_income_range.hard_max) weekly_net_income.between?(applicable_income_range.soft_max, applicable_income_range.hard_max)
end end
def net_income_in_soft_min_range? def net_income_in_soft_min_range?
return unless weekly_net_income && ecstat1 return unless weekly_net_income && all_relevant_ecstat_provided
weekly_net_income.between?(applicable_income_range.hard_min, applicable_income_range.soft_min) weekly_net_income.between?(applicable_income_range.hard_min, applicable_income_range.soft_min)
end end

29
spec/models/lettings_log_spec.rb

@ -3411,5 +3411,34 @@ RSpec.describe LettingsLog do
end end
end end
end end
describe "#all_relevant_ecstat_provided" do
context "when details for a non-lead tenant are not known" do
let(:lettings_log) { build(:lettings_log, hhmemb: 2, ecstat1: 1, details_known_2: 1) }
it "returns true" do
expect(lettings_log.all_relevant_ecstat_provided).to be(true)
end
end
context "when details for a non-lead tenant are known" do
context "and that person's ecstat is provided" do
let(:lettings_log) { build(:lettings_log, hhmemb: 2, ecstat1: 1, details_known_2: 0, ecstat2: 2) }
it "returns true" do
expect(lettings_log.all_relevant_ecstat_provided).to be(true)
end
end
context "and that person's ecstat is not provided" do
let(:lettings_log) { build(:lettings_log, hhmemb: 2, ecstat1: 1, details_known_2: 0) }
it "returns true" do
expect(lettings_log.all_relevant_ecstat_provided).to be(false)
end
end
end
end
end end
# rubocop:enable RSpec/MessageChain # rubocop:enable RSpec/MessageChain

16
spec/models/validations/financial_validations_spec.rb

@ -223,6 +223,7 @@ RSpec.describe Validations::FinancialValidations do
it "allows income levels based on all working situations combined" do it "allows income levels based on all working situations combined" do
record.earnings = 5000 record.earnings = 5000
record.incfreq = 1 record.incfreq = 1
record.hhmemb = 4
record.ecstat1 = 1 record.ecstat1 = 1
record.ecstat2 = 1 record.ecstat2 = 1
record.ecstat3 = 8 record.ecstat3 = 8
@ -234,6 +235,7 @@ RSpec.describe Validations::FinancialValidations do
it "uses the combined value in error messages" do it "uses the combined value in error messages" do
record.earnings = 100 record.earnings = 100
record.incfreq = 1 record.incfreq = 1
record.hhmemb = 3
record.ecstat1 = 1 record.ecstat1 = 1
record.ecstat2 = 2 record.ecstat2 = 2
record.ecstat3 = 9 record.ecstat3 = 9
@ -241,6 +243,20 @@ RSpec.describe Validations::FinancialValidations do
expect(record.errors["earnings"]) expect(record.errors["earnings"])
.to eq(["Net income cannot be less than £150.00 per week given the household’s working situation"]) .to eq(["Net income cannot be less than £150.00 per week given the household’s working situation"])
end end
it "adds errors to all tenant ecstat fields when income is too high" do
record.earnings = 5000
record.incfreq = 1
record.hhmemb = 3
record.ecstat1 = 1
record.ecstat2 = 2
record.ecstat3 = 9
financial_validator.validate_net_income(record)
(1..8).each do |n|
expect(record.errors["ecstat#{n}"])
.to eq(["Net household income of £5,000.00 weekly is too high given the household’s working situation"])
end
end
end end
end end

Loading…
Cancel
Save