Module: Validations::Sales::FinancialValidations

Defined in:
sales/financial_validations.rb

Instance Method Summary collapse

Instance Method Details

#validate_child_income(record) ⇒ Object



80
81
82
83
84
85
86
87
# File 'sales/financial_validations.rb', line 80

def validate_child_income(record)
  return unless record.income2 && record.ecstat2

  if record.income2.positive? && is_economic_status_child?(record.ecstat2) && record.form.start_date.year >= 2023
    record.errors.add :ecstat2, I18n.t("validations.financial.income.child_has_income")
    record.errors.add :income2, I18n.t("validations.financial.income.child_has_income")
  end
end

#validate_combined_income(record) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
# File 'sales/financial_validations.rb', line 27

def validate_combined_income(record)
  return unless record.income1 && record.income2 && record.la && record.shared_ownership_scheme?

  combined_income = record.income1 + record.income2
  relevant_fields = %i[income1 income2 ownershipsch uprn la postcode_full]
  if record.london_property? && combined_income > 90_000
    relevant_fields.each { |field| record.errors.add field, :over_combined_hard_max_for_london, message: I18n.t("validations.financial.income.combined_over_hard_max_for_london") }
  elsif record.property_not_in_london? && combined_income > 80_000
    relevant_fields.each { |field| record.errors.add field, :over_combined_hard_max_for_outside_london, message: I18n.t("validations.financial.income.combined_over_hard_max_for_outside_london") }
  end
end

#validate_equity_in_range_for_year_and_type(record) ⇒ Object



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'sales/financial_validations.rb', line 89

def validate_equity_in_range_for_year_and_type(record)
  return unless record.type && record.equity && record.collection_start_year

  ranges = EQUITY_RANGES_BY_YEAR.fetch(record.collection_start_year, DEFAULT_EQUITY_RANGES)

  return unless (range = ranges[record.type])

  if record.equity < range.min
    record.errors.add :type, I18n.t("validations.financial.equity.under_min", min_equity: range.min)
    record.errors.add :equity, :under_min, message: I18n.t("validations.financial.equity.under_min", min_equity: range.min)
  elsif record.equity > range.max
    record.errors.add :type, I18n.t("validations.financial.equity.over_max", max_equity: range.max)
    record.errors.add :equity, :over_max, message: I18n.t("validations.financial.equity.over_max", max_equity: range.max)
  end
end

#validate_equity_less_than_staircase_difference(record) ⇒ Object



105
106
107
108
109
110
111
112
113
114
115
# File 'sales/financial_validations.rb', line 105

def validate_equity_less_than_staircase_difference(record)
  return unless record.equity && record.stairbought && record.stairowned
  return unless record.saledate && record.form.start_year_after_2024?

  if record.equity > record.stairowned - record.stairbought
    formatted_equity = sprintf("%g", record.equity)
    record.errors.add :equity, I18n.t("validations.financial.equity.over_stairowned_minus_stairbought", equity: formatted_equity, staircase_difference: record.stairowned - record.stairbought, buyer_owns: record.joint_purchase? ? "buyers own" : "buyer owns")
    record.errors.add :stairowned, I18n.t("validations.financial.equity.over_stairowned_minus_stairbought", equity: formatted_equity, staircase_difference: record.stairowned - record.stairbought, buyer_owns: record.joint_purchase? ? "buyers own" : "buyer owns")
    record.errors.add :stairbought, I18n.t("validations.financial.equity.over_stairowned_minus_stairbought", equity: formatted_equity, staircase_difference: record.stairowned - record.stairbought, buyer_owns: record.joint_purchase? ? "buyers own" : "buyer owns")
  end
end

#validate_income1(record) ⇒ Object

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



5
6
7
8
9
10
11
12
13
14
# File 'sales/financial_validations.rb', line 5

def validate_income1(record)
  return unless record.income1 && record.la && record.shared_ownership_scheme?

  relevant_fields = %i[income1 ownershipsch uprn la postcode_full]
  if record.london_property? && !record.income1.between?(0, 90_000)
    relevant_fields.each { |field| record.errors.add field, :outside_london_income_range, message: I18n.t("validations.financial.income.outside_london_income_range") }
  elsif record.property_not_in_london? && !record.income1.between?(0, 80_000)
    relevant_fields.each { |field| record.errors.add field, :outside_non_london_income_range, message: I18n.t("validations.financial.income.outside_non_london_income_range") }
  end
end

#validate_income2(record) ⇒ Object



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

def validate_income2(record)
  return unless record.income2 && record.la && record.shared_ownership_scheme?

  relevant_fields = %i[income2 ownershipsch uprn la postcode_full]
  if record.london_property? && !record.income2.between?(0, 90_000)
    relevant_fields.each { |field| record.errors.add field, :outside_london_income_range, message: I18n.t("validations.financial.income.outside_london_income_range") }
  elsif record.property_not_in_london? && !record.income2.between?(0, 80_000)
    relevant_fields.each { |field| record.errors.add field, :outside_non_london_income_range, message: I18n.t("validations.financial.income.outside_non_london_income_range") }
  end
end

#validate_monthly_leasehold_charges(record) ⇒ Object



43
44
45
# File 'sales/financial_validations.rb', line 43

def validate_monthly_leasehold_charges(record)
  record.errors.add :mscharge, I18n.t("validations.financial.monthly_leasehold_charges.not_zero") if record.mscharge&.zero?
end

#validate_mortgage(record) ⇒ Object



39
40
41
# File 'sales/financial_validations.rb', line 39

def validate_mortgage(record)
  record.errors.add :mortgage, :cannot_be_0, message: I18n.t("validations.financial.mortgage") if record.mortgage_used? && record.mortgage&.zero?
end

#validate_percentage_bought_at_least_threshold(record) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'sales/financial_validations.rb', line 65

def validate_percentage_bought_at_least_threshold(record)
  return unless record.stairbought && record.type

  threshold = if [2, 16, 18, 24].include? record.type
                10
              else
                1
              end

  if threshold && record.stairbought < threshold
    record.errors.add :stairbought, I18n.t("validations.financial.staircasing.percentage_bought_must_be_at_least_threshold", threshold:)
    record.errors.add :type, I18n.t("validations.setup.type.percentage_bought_must_be_at_least_threshold", threshold:)
  end
end

#validate_percentage_bought_not_equal_percentage_owned(record) ⇒ Object



55
56
57
58
59
60
61
62
63
# File 'sales/financial_validations.rb', line 55

def validate_percentage_bought_not_equal_percentage_owned(record)
  return unless record.stairbought && record.stairowned
  return unless record.saledate && record.form.start_year_after_2024?

  if record.stairbought == record.stairowned
    record.errors.add :stairbought, I18n.t("validations.financial.staircasing.percentage_bought_equal_percentage_owned", stairbought: sprintf("%g", record.stairbought), stairowned: sprintf("%g", record.stairowned))
    record.errors.add :stairowned, I18n.t("validations.financial.staircasing.percentage_bought_equal_percentage_owned", stairbought: sprintf("%g", record.stairbought), stairowned: sprintf("%g", record.stairowned))
  end
end

#validate_percentage_bought_not_greater_than_percentage_owned(record) ⇒ Object



47
48
49
50
51
52
53
# File 'sales/financial_validations.rb', line 47

def validate_percentage_bought_not_greater_than_percentage_owned(record)
  return unless record.stairbought && record.stairowned

  if record.stairbought > record.stairowned
    record.errors.add :stairowned, I18n.t("validations.financial.staircasing.percentage_bought_must_be_greater_than_percentage_owned", buyer_now_owns: record.joint_purchase? ? "buyers now own" : "buyer now owns")
  end
end