Module: Validations::FinancialValidations

Includes:
ChargesHelper, MoneyFormattingHelper, SharedValidations
Defined in:
financial_validations.rb

Constant Summary collapse

EMPLOYED_STATUSES =
[1, 0].freeze

Instance Method Summary collapse

Instance Method Details

#no_known_benefits?(record) ⇒ Boolean

Returns:

  • (Boolean)


108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'financial_validations.rb', line 108

def no_known_benefits?(record)
  return true unless record.collection_start_year

  if record.collection_start_year <= 2021
    record.benefits_unknown? ||
      record.receives_no_benefits? ||
      record.receives_universal_credit_but_no_housing_benefit?
  else
    record.benefits_unknown? ||
      record.receives_no_benefits? ||
      record.tenant_refuses_to_say_benefits?
  end
end

#validate_care_home_charges(record) ⇒ Object



158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
# File 'financial_validations.rb', line 158

def validate_care_home_charges(record)
  if record.is_carehome?
    period = record.form.get_question("period", record).label_from_value(record.period).downcase
    # NOTE: This is a temporary change to allow `ccharge` values despite `is_carehome` being true. This value
    # is going to be moved to a soft validation in CLDC-2074, so we can safely do this.
    if record.chcharge.blank?
      # record.errors.add :is_carehome, I18n.t("validations.financial.carehome.not_provided", period:)
      # record.errors.add :chcharge, I18n.t("validations.financial.carehome.not_provided", period:)
    elsif !weekly_value_in_range(record, "chcharge", 10, 5000)
      max_chcharge = record.weekly_to_value_per_period(5000)
      min_chcharge = record.weekly_to_value_per_period(10)
      message = I18n.t("validations.financial.carehome.out_of_range", period:, min_chcharge:, max_chcharge:)

      record.errors.add :period, message
      record.errors.add :chcharge, :out_of_range, message:
    end
  end
end

#validate_negative_currency(record) ⇒ Object



93
94
95
96
97
98
99
100
# File 'financial_validations.rb', line 93

def validate_negative_currency(record)
  t = %w[earnings brent scharge pscharge supcharg]
  t.each do |x|
    if record[x].present? && record[x].negative?
      record.errors.add x.to_sym, I18n.t("validations.financial.negative_currency")
    end
  end
end

#validate_net_income(record) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'financial_validations.rb', line 27

def validate_net_income(record)
  if record.ecstat1 && record.hhmemb && record.weekly_net_income && record.startdate && record.form.start_date.year >= 2023
    if record.weekly_net_income > record.applicable_income_range.hard_max
      frequency = record.form.get_question("incfreq", record).label_from_value(record.incfreq).downcase
      hard_max = format_as_currency(record.applicable_income_range.hard_max)
      record.errors.add(
        :earnings,
        :over_hard_max,
        message: I18n.t("validations.financial.earnings.over_hard_max", hard_max:),
      )
      record.errors.add(
        :hhmemb,
        :over_hard_max,
        message: I18n.t("validations.financial.hhmemb.earnings.over_hard_max", earnings: format_as_currency(record.earnings), frequency:),
      )
      (1..record.hhmemb).each do |n|
        record.errors.add(
          "ecstat#{n}",
          :over_hard_max,
          message: I18n.t("validations.financial.ecstat.over_hard_max", earnings: format_as_currency(record.earnings), frequency:),
        )
        next unless record["ecstat#{n}"] == 9

        record.errors.add(
          "age#{n}",
          :over_hard_max,
          message: I18n.t("validations.financial.age.earnings_over_hard_max", earnings: format_as_currency(record.earnings), frequency:),
        )
      end
    end

    if record.weekly_net_income < record.applicable_income_range.hard_min
      hard_min = format_as_currency(record.applicable_income_range.hard_min)
      frequency = record.form.get_question("incfreq", record).label_from_value(record.incfreq).downcase
      record.errors.add(
        :earnings,
        :under_hard_min,
        message: I18n.t("validations.financial.earnings.under_hard_min", hard_min:),
      )
      record.errors.add(
        :hhmemb,
        :under_hard_min,
        message: I18n.t("validations.financial.hhmemb.earnings.under_hard_min", earnings: format_as_currency(record.earnings), frequency:),
      )
      (1..record.hhmemb).each do |n|
        record.errors.add(
          "ecstat#{n}",
          :under_hard_min,
          message: I18n.t("validations.financial.ecstat.under_hard_min", earnings: format_as_currency(record.earnings), frequency:),
        )
        # N.B. It is not possible for a change to an age field to increase the hard min
      end
    end
  end

  if record.earnings.present? && record.incfreq.blank?
    record.errors.add :incfreq, I18n.t("validations.financial.earnings.freq_missing")
    record.errors.add :earnings, I18n.t("validations.financial.earnings.freq_missing")
  end

  if record.incfreq.present? && record.earnings.blank?
    record.errors.add :earnings, I18n.t("validations.financial.earnings.earnings_missing")
    record.errors.add :incfreq, I18n.t("validations.financial.earnings.earnings_missing")
  end
end

#validate_net_income_uc_proportion(record) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
# File 'financial_validations.rb', line 15

def validate_net_income_uc_proportion(record)
  (1..8).any? do |n|
    economic_status = record["ecstat#{n}"]
    is_employed = EMPLOYED_STATUSES.include?(economic_status)
    relationship = record["relat#{n}"]
    is_partner_or_main = relationship == "P" || (relationship.nil? && economic_status.present?)
    if is_employed && is_partner_or_main && record.benefits&.zero?
      record.errors.add :benefits, I18n.t("validations.financial.benefits.part_or_full_time")
    end
  end
end

#validate_outstanding_rent_amount(record) ⇒ Object

Validations methods need to be called ‘validate_<page_name>’ to run on model save or ‘validate_’ to run on submit as well



7
8
9
10
11
12
# File 'financial_validations.rb', line 7

def validate_outstanding_rent_amount(record)
  if !record.has_housing_benefit_rent_shortfall? && record.tshortfall.present?
    record.errors.add :tshortfall, :no_outstanding_charges, message: I18n.t("validations.financial.tshortfall.outstanding_amount_not_expected")
    record.errors.add :hbrentshortfall, :no_outstanding_charges, message: I18n.t("validations.financial.hbrentshortfall.outstanding_amount_not_expected")
  end
end

#validate_rent_amount(record) ⇒ Object



122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'financial_validations.rb', line 122

def validate_rent_amount(record)
  if record.wtshortfall
    if record.wrent && (record.wtshortfall > record.wrent)
      record.errors.add :tshortfall, :more_than_rent, message: I18n.t("validations.financial.tshortfall.more_than_rent")
      record.errors.add :brent, I18n.t("validations.financial.rent.less_than_shortfall")
    elsif record.wtshortfall < 0.01
      record.errors.add :tshortfall, :must_be_positive, message: I18n.t("validations.financial.tshortfall.must_be_positive")
    end
  end

  if record.tcharge.present? && weekly_value_in_range(record, "tcharge", 0, 9.99)
    record.errors.add :tcharge, :under_10, message: I18n.t("validations.financial.tcharge.under_10")
  end

  answered_questions = [record.tcharge, record.chcharge].concat(record.household_charge && record.household_charge == 1 ? [record.household_charge] : [])
  if answered_questions.count(&:present?) > 1
    record.errors.add :tcharge, :complete_1_of_3, message: I18n.t("validations.financial.charges.complete_1_of_3") if record.tcharge.present?
    record.errors.add :chcharge, I18n.t("validations.financial.charges.complete_1_of_3") if record.chcharge.present?
    record.errors.add :household_charge, I18n.t("validations.financial.charges.complete_1_of_3") if record.household_charge.present?
  end

  validate_charges(record)
  validate_rent_range(record)
end

#validate_rent_period(record) ⇒ Object



147
148
149
150
151
152
153
154
155
156
# File 'financial_validations.rb', line 147

def validate_rent_period(record)
  if record.managing_organisation.present? && record.managing_organisation.rent_periods.present? &&
      record.period && !record.managing_organisation.rent_periods.include?(record.period)
    record.errors.add :period, :wrong_rent_period, message: I18n.t(
      "validations.financial.rent_period.invalid_for_org",
      org_name: record.managing_organisation.name,
      rent_period: record.form.get_question("period", record).label_from_value(record.period).downcase,
    )
  end
end

#validate_tshortfall(record) ⇒ Object



102
103
104
105
106
# File 'financial_validations.rb', line 102

def validate_tshortfall(record)
  if record.has_housing_benefit_rent_shortfall? && no_known_benefits?(record)
    record.errors.add :tshortfall, I18n.t("validations.financial.hbrentshortfall.outstanding_no_benefits")
  end
end