diff --git a/app/models/validations/financial_validations.rb b/app/models/validations/financial_validations.rb index 29c22eb62..879cf8cb5 100644 --- a/app/models/validations/financial_validations.rb +++ b/app/models/validations/financial_validations.rb @@ -146,7 +146,7 @@ module Validations::FinancialValidations private - CHARGE_MAXIMUMS = { + CHARGE_MAXIMA_PER_WEEK = { scharge: { private_registered_provider: { general_needs: 800, @@ -181,22 +181,30 @@ private PROVIDER_TYPE = { 1 => :local_authority, 2 => :private_registered_provider }.freeze NEEDSTYPE_VALUES = { 2 => :supported_housing, 1 => :general_needs }.freeze + CHARGE_NAMES = { scharge: "service charge", pscharge: "personal service charge", supcharg: "support charge" }.freeze def validate_charges(record) return unless record.owning_organisation provider_type = record.owning_organisation.provider_type_before_type_cast %i[scharge pscharge supcharg].each do |charge| - maximum = CHARGE_MAXIMUMS.dig(charge, PROVIDER_TYPE[provider_type], NEEDSTYPE_VALUES[record.needstype]) + maximum_per_week = CHARGE_MAXIMA_PER_WEEK.dig(charge, PROVIDER_TYPE[provider_type], NEEDSTYPE_VALUES[record.needstype]) - if maximum.present? && record[:period].present? && record[charge].present? && !weekly_value_in_range(record, charge, 0.0, maximum) - record.errors.add charge, :outside_the_range, message: I18n.t("validations.financial.rent.#{charge}.#{PROVIDER_TYPE[provider_type]}.#{NEEDSTYPE_VALUES[record.needstype]}") - end + next unless maximum_per_week.present? && record[:period].present? && record[charge].present? && !weekly_value_in_range(record, charge, 0.0, maximum_per_week) + + charge_name = CHARGE_NAMES[charge] + frequency = record.form.get_question("period", record).label_from_value(record.period).downcase + letting_type = NEEDSTYPE_VALUES[record.needstype].to_s.humanize(capitalize: false) + provider_type_label = PROVIDER_TYPE[provider_type].to_s.humanize(capitalize: false) + maximum_per_period = record.weekly_to_value_per_period(maximum_per_week) + + record.errors.add charge, :outside_the_range, message: I18n.t("validations.financial.rent.out_of_range", charge_name:, maximum_per_period:, frequency:, letting_type:, provider_type: provider_type_label) + record.errors.add :period, :outside_the_range, message: I18n.t("validations.financial.rent.out_of_range", charge_name:, maximum_per_period:, frequency:, letting_type:, provider_type: provider_type_label) end end def weekly_value_in_range(record, field, min, max) - record[field].present? && record.weekly_value(record[field]).present? && record.weekly_value(record[field]).between?(min, max) + record.weekly_value(record[field])&.between?(min, max) end def validate_rent_range(record) diff --git a/config/locales/en.yml b/config/locales/en.yml index 2890f3bde..69a4a8bb9 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -265,7 +265,7 @@ en: owning_organisation: "Enter a date when the owning organisation was active. %{owning_organisation} became inactive on %{owning_organisation_merge_date} and was replaced by %{owning_absorbing_organisation}." managing_organisation: "Enter a date when the managing organisation was active. %{managing_organisation} became inactive on %{managing_organisation_merge_date} and was replaced by %{managing_absorbing_organisation}." different_merge: "Enter a date when the owning and managing organisations were active. %{owning_organisation} became inactive on %{owning_organisation_merge_date} and was replaced by %{owning_absorbing_organisation}. %{managing_organisation} became inactive on %{managing_organisation_merge_date} and was replaced by %{managing_absorbing_organisation}." - invalid_absorbing_organisations_start_date: + invalid_absorbing_organisations_start_date: same_organisation: "Enter a date when the owning and managing organisation was active. %{owning_organisation} became active on %{owning_organisation_available_from}." owning_organisation: "Enter a date when the owning organisation was active. %{owning_organisation} became active on %{owning_organisation_available_from}." managing_organisation: "Enter a date when the managing organisation was active. %{managing_organisation} became active on %{managing_organisation_available_from}." @@ -367,27 +367,7 @@ en: negative_currency: "Enter an amount above 0" rent: less_than_shortfall: "Enter an amount that is more than the shortfall in basic rent" - scharge: - private_registered_provider: - general_needs: "Enter a value for the service charge between £0 and £800 per week if the landlord is a private registered provider and it is a general needs letting" - supported_housing: "Enter a value for the service charge between £0 and £800 per week if the landlord is a private registered provider and it is a supported housing letting" - local_authority: - general_needs: "Enter a value for the service charge between £0 and £500 per week if the landlord is a local authority and it is a general needs letting" - supported_housing: "Enter a value for the service charge between £0 and £500 per week if the landlord is a local authority and it is a supported housing letting" - pscharge: - private_registered_provider: - general_needs: "Enter a value for the personal service charge between £0 and £700 per week if the landlord is a private registered provider and it is a general needs letting" - supported_housing: "Enter a value for the personal service charge between £0 and £700 per week if the landlord is a private registered provider and it is a supported housing letting" - local_authority: - general_needs: "Enter a value for the personal service charge between £0 and £200 per week if the landlord is a local authority and it is a general needs letting" - supported_housing: "Enter a value for the personal service charge between £0 and £200 per week if the landlord is a local authority and it is a supported housing letting" - supcharg: - private_registered_provider: - general_needs: "Enter a value for the support charge between £0 and £800 per week if the landlord is a private registered provider and it is a general needs letting" - supported_housing: "Enter a value for the support charge between £0 and £800 per week if the landlord is a private registered provider and it is a supported housing letting" - local_authority: - general_needs: "Enter a value for the support charge between £0 and £200 per week if the landlord is a local authority and it is a general needs letting" - supported_housing: "Enter a value for the support charge between £0 and £200 per week if the landlord is a local authority and it is a supported housing letting" + out_of_range: "Enter a value for the %{charge_name} between £0 and %{maximum_per_period} paid %{frequency}. %{maximum_per_period} is the max limit for rent and charges paid %{frequency} for %{letting_type} lettings owned by a %{provider_type}." ecstat: over_hard_max: "Net income of %{hard_max} per week is too high given the tenant’s working situation" brent: @@ -722,7 +702,7 @@ Make sure these answers are correct." charges: informative_text: "This is higher than we would expect." hint_text: "Check the following:" - + devise: email: diff --git a/spec/models/validations/financial_validations_spec.rb b/spec/models/validations/financial_validations_spec.rb index 3e645f164..f419137a2 100644 --- a/spec/models/validations/financial_validations_spec.rb +++ b/spec/models/validations/financial_validations_spec.rb @@ -262,56 +262,74 @@ RSpec.describe Validations::FinancialValidations do before { record.needstype = 1 } [{ - period: { label: "weekly", value: 1 }, + period: { label: "weekly for 52 weeks", value: 1 }, charge: { field: "scharge", value: 801 }, + charge_name: "service charge", + maximum_per_period: "£800.00", }, { - period: { label: "monthly", value: 4 }, + period: { label: "every calendar month", value: 4 }, charge: { field: "scharge", value: 3471 }, + charge_name: "service charge", + maximum_per_period: "£3,466.00", }, { period: { label: "every 2 weeks", value: 2 }, charge: { field: "scharge", value: 1601 }, + charge_name: "service charge", + maximum_per_period: "£1,600.00", }, { - period: { label: "weekly", value: 1 }, + period: { label: "weekly for 52 weeks", value: 1 }, charge: { field: "pscharge", value: 701 }, + charge_name: "personal service charge", + maximum_per_period: "£700.00", }, { - period: { label: "monthly", value: 4 }, + period: { label: "every calendar month", value: 4 }, charge: { field: "pscharge", value: 3200 }, + charge_name: "personal service charge", + maximum_per_period: "£3,033.00", }, { period: { label: "every 2 weeks", value: 2 }, charge: { field: "pscharge", value: 1401 }, + charge_name: "personal service charge", + maximum_per_period: "£1,400.00", }, { - period: { label: "weekly", value: 1 }, + period: { label: "weekly for 52 weeks", value: 1 }, charge: { field: "supcharg", value: 801 }, + charge_name: "support charge", + maximum_per_period: "£800.00", }, { - period: { label: "monthly", value: 4 }, + period: { label: "every calendar month", value: 4 }, charge: { field: "supcharg", value: 3471 }, + charge_name: "support charge", + maximum_per_period: "£3,466.00", }, { period: { label: "every 2 weeks", value: 2 }, charge: { field: "supcharg", value: 1601 }, + charge_name: "support charge", + maximum_per_period: "£1,600.00", }].each do |test_case| it "does not allow charges outside the range when period is #{test_case[:period][:label]}" do record.period = test_case[:period][:value] record[test_case[:charge][:field]] = test_case[:charge][:value] financial_validator.validate_rent_amount(record) expect(record.errors[test_case[:charge][:field]]) - .to include(match I18n.t("validations.financial.rent.#{test_case[:charge][:field]}.private_registered_provider.general_needs")) + .to include(match I18n.t("validations.financial.rent.out_of_range", charge_name: test_case[:charge_name], maximum_per_period: test_case[:maximum_per_period], frequency: test_case[:period][:label], letting_type: "general needs", provider_type: "private registered provider")) end end [{ - period: { label: "weekly", value: 1 }, + period: { label: "weekly for 52 weeks", value: 1 }, charge: { field: "scharge", value: 799 }, }, { - period: { label: "monthly", value: 4 }, + period: { label: "every calendar month", value: 4 }, charge: { field: "scharge", value: 3400 }, }, { @@ -319,11 +337,11 @@ RSpec.describe Validations::FinancialValidations do charge: { field: "scharge", value: 1599 }, }, { - period: { label: "weekly", value: 1 }, + period: { label: "weekly for 52 weeks", value: 1 }, charge: { field: "pscharge", value: 699 }, }, { - period: { label: "monthly", value: 4 }, + period: { label: "every calendar month", value: 4 }, charge: { field: "pscharge", value: 2500 }, }, { @@ -331,11 +349,11 @@ RSpec.describe Validations::FinancialValidations do charge: { field: "pscharge", value: 1399 }, }, { - period: { label: "weekly", value: 1 }, + period: { label: "weekly for 52 weeks", value: 1 }, charge: { field: "supcharg", value: 799 }, }, { - period: { label: "monthly", value: 4 }, + period: { label: "every calendar month", value: 4 }, charge: { field: "supcharg", value: 3000 }, }, { @@ -356,56 +374,74 @@ RSpec.describe Validations::FinancialValidations do before { record.needstype = 2 } [{ - period: { label: "weekly", value: 1 }, + period: { label: "weekly for 52 weeks", value: 1 }, charge: { field: "scharge", value: 801 }, + charge_name: "service charge", + maximum_per_period: "£800.00", }, { - period: { label: "monthly", value: 4 }, + period: { label: "every calendar month", value: 4 }, charge: { field: "scharge", value: 3471 }, + charge_name: "service charge", + maximum_per_period: "£3,466.00", }, { period: { label: "every 2 weeks", value: 2 }, charge: { field: "scharge", value: 1601 }, + charge_name: "service charge", + maximum_per_period: "£1,600.00", }, { - period: { label: "weekly", value: 1 }, + period: { label: "weekly for 52 weeks", value: 1 }, charge: { field: "pscharge", value: 701 }, + charge_name: "personal service charge", + maximum_per_period: "£700.00", }, { - period: { label: "monthly", value: 4 }, + period: { label: "every calendar month", value: 4 }, charge: { field: "pscharge", value: 3200 }, + charge_name: "personal service charge", + maximum_per_period: "£3,033.00", }, { period: { label: "every 2 weeks", value: 2 }, charge: { field: "pscharge", value: 1401 }, + charge_name: "personal service charge", + maximum_per_period: "£1,400.00", }, { - period: { label: "weekly", value: 1 }, + period: { label: "weekly for 52 weeks", value: 1 }, charge: { field: "supcharg", value: 801 }, + charge_name: "support charge", + maximum_per_period: "£800.00", }, { - period: { label: "monthly", value: 4 }, + period: { label: "every calendar month", value: 4 }, charge: { field: "supcharg", value: 3471 }, + charge_name: "support charge", + maximum_per_period: "£3,466.00", }, { period: { label: "every 2 weeks", value: 2 }, charge: { field: "supcharg", value: 1601 }, + charge_name: "support charge", + maximum_per_period: "£1,600.00", }].each do |test_case| it "does not allow charges outside the range when period is #{test_case[:period][:label]}" do record.period = test_case[:period][:value] record[test_case[:charge][:field]] = test_case[:charge][:value] financial_validator.validate_rent_amount(record) expect(record.errors[test_case[:charge][:field]]) - .to include(match I18n.t("validations.financial.rent.#{test_case[:charge][:field]}.private_registered_provider.supported_housing")) + .to include(match I18n.t("validations.financial.rent.out_of_range", charge_name: test_case[:charge_name], maximum_per_period: test_case[:maximum_per_period], frequency: test_case[:period][:label], letting_type: "supported housing", provider_type: "private registered provider")) end end [{ - period: { label: "weekly", value: 1 }, + period: { label: "weekly for 52 weeks", value: 1 }, charge: { field: "scharge", value: 799 }, }, { - period: { label: "monthly", value: 4 }, + period: { label: "every calendar month", value: 4 }, charge: { field: "scharge", value: 3400 }, }, { @@ -413,11 +449,11 @@ RSpec.describe Validations::FinancialValidations do charge: { field: "scharge", value: 1599 }, }, { - period: { label: "weekly", value: 1 }, + period: { label: "weekly for 52 weeks", value: 1 }, charge: { field: "pscharge", value: 699 }, }, { - period: { label: "monthly", value: 4 }, + period: { label: "every calendar month", value: 4 }, charge: { field: "pscharge", value: 2500 }, }, { @@ -425,11 +461,11 @@ RSpec.describe Validations::FinancialValidations do charge: { field: "pscharge", value: 1399 }, }, { - period: { label: "weekly", value: 1 }, + period: { label: "weekly for 52 weeks", value: 1 }, charge: { field: "supcharg", value: 799 }, }, { - period: { label: "monthly", value: 4 }, + period: { label: "every calendar month", value: 4 }, charge: { field: "supcharg", value: 3400 }, }, { @@ -454,56 +490,74 @@ RSpec.describe Validations::FinancialValidations do before { record.needstype = 1 } [{ - period: { label: "weekly", value: 1 }, + period: { label: "weekly for 52 weeks", value: 1 }, charge: { field: "scharge", value: 501 }, + charge_name: "service charge", + maximum_per_period: "£500.00", }, { - period: { label: "monthly", value: 4 }, + period: { label: "every calendar month", value: 4 }, charge: { field: "scharge", value: 2300 }, + charge_name: "service charge", + maximum_per_period: "£2,166.00", }, { period: { label: "every 2 weeks", value: 2 }, charge: { field: "scharge", value: 1001 }, + charge_name: "service charge", + maximum_per_period: "£1,000.00", }, { - period: { label: "weekly", value: 1 }, + period: { label: "weekly for 52 weeks", value: 1 }, charge: { field: "pscharge", value: 201 }, + charge_name: "personal service charge", + maximum_per_period: "£200.00", }, { - period: { label: "monthly", value: 4 }, + period: { label: "every calendar month", value: 4 }, charge: { field: "pscharge", value: 1000 }, + charge_name: "personal service charge", + maximum_per_period: "£866.00", }, { period: { label: "every 2 weeks", value: 2 }, charge: { field: "pscharge", value: 401 }, + charge_name: "personal service charge", + maximum_per_period: "£400.00", }, { - period: { label: "weekly", value: 1 }, + period: { label: "weekly for 52 weeks", value: 1 }, charge: { field: "supcharg", value: 201 }, + charge_name: "support charge", + maximum_per_period: "£200.00", }, { - period: { label: "monthly", value: 4 }, + period: { label: "every calendar month", value: 4 }, charge: { field: "supcharg", value: 1000 }, + charge_name: "support charge", + maximum_per_period: "£866.00", }, { period: { label: "every 2 weeks", value: 2 }, charge: { field: "supcharg", value: 401 }, + charge_name: "support charge", + maximum_per_period: "£400.00", }].each do |test_case| it "does not allow charges outside the range when period is #{test_case[:period][:label]}" do record.period = test_case[:period][:value] record[test_case[:charge][:field]] = test_case[:charge][:value] financial_validator.validate_rent_amount(record) expect(record.errors[test_case[:charge][:field]]) - .to include(match I18n.t("validations.financial.rent.#{test_case[:charge][:field]}.local_authority.general_needs")) + .to include(match I18n.t("validations.financial.rent.out_of_range", charge_name: test_case[:charge_name], maximum_per_period: test_case[:maximum_per_period], frequency: test_case[:period][:label], letting_type: "general needs", provider_type: "local authority")) end end [{ - period: { label: "weekly", value: 1 }, + period: { label: "weekly for 52 weeks", value: 1 }, charge: { field: "scharge", value: 499 }, }, { - period: { label: "monthly", value: 4 }, + period: { label: "every calendar month", value: 4 }, charge: { field: "scharge", value: 2000 }, }, { @@ -511,11 +565,11 @@ RSpec.describe Validations::FinancialValidations do charge: { field: "scharge", value: 999 }, }, { - period: { label: "weekly", value: 1 }, + period: { label: "weekly for 52 weeks", value: 1 }, charge: { field: "pscharge", value: 199 }, }, { - period: { label: "monthly", value: 4 }, + period: { label: "every calendar month", value: 4 }, charge: { field: "pscharge", value: 800 }, }, { @@ -523,11 +577,11 @@ RSpec.describe Validations::FinancialValidations do charge: { field: "pscharge", value: 399 }, }, { - period: { label: "weekly", value: 1 }, + period: { label: "weekly for 52 weeks", value: 1 }, charge: { field: "supcharg", value: 199.99 }, }, { - period: { label: "monthly", value: 4 }, + period: { label: "every calendar month", value: 4 }, charge: { field: "supcharg", value: 800 }, }, { @@ -548,61 +602,79 @@ RSpec.describe Validations::FinancialValidations do before { record.needstype = 2 } [{ - period: { label: "weekly", value: 1 }, + period: { label: "weekly for 52 weeks", value: 1 }, charge: { field: "scharge", value: 501 }, + charge_name: "service charge", + maximum_per_period: "£500.00", }, { - period: { label: "monthly", value: 4 }, + period: { label: "every calendar month", value: 4 }, charge: { field: "scharge", value: 2300 }, + charge_name: "service charge", + maximum_per_period: "£2,166.00", }, { period: { label: "every 2 weeks", value: 2 }, charge: { field: "scharge", value: 1001 }, + charge_name: "service charge", + maximum_per_period: "£1,000.00", }, { - period: { label: "weekly", value: 1 }, + period: { label: "weekly for 52 weeks", value: 1 }, charge: { field: "pscharge", value: 201 }, + charge_name: "personal service charge", + maximum_per_period: "£200.00", }, { - period: { label: "monthly", value: 4 }, + period: { label: "every calendar month", value: 4 }, charge: { field: "pscharge", value: 1000 }, + charge_name: "personal service charge", + maximum_per_period: "£866.00", }, { period: { label: "every 2 weeks", value: 2 }, charge: { field: "pscharge", value: 401 }, + charge_name: "personal service charge", + maximum_per_period: "£400.00", }, { - period: { label: "weekly", value: 1 }, + period: { label: "weekly for 52 weeks", value: 1 }, charge: { field: "supcharg", value: 201 }, + charge_name: "support charge", + maximum_per_period: "£200.00", }, { - period: { label: "monthly", value: 4 }, + period: { label: "every calendar month", value: 4 }, charge: { field: "supcharg", value: 1000 }, + charge_name: "support charge", + maximum_per_period: "£866.00", }, { period: { label: "every 2 weeks", value: 2 }, charge: { field: "supcharg", value: 401 }, + charge_name: "support charge", + maximum_per_period: "£400.00", }].each do |test_case| it "does not allow charges outside the range when period is #{test_case[:period][:label]}" do record.period = test_case[:period][:value] record[test_case[:charge][:field]] = test_case[:charge][:value] financial_validator.validate_rent_amount(record) expect(record.errors[test_case[:charge][:field]]) - .to include(match I18n.t("validations.financial.rent.#{test_case[:charge][:field]}.local_authority.supported_housing")) + .to include(match I18n.t("validations.financial.rent.out_of_range", charge_name: test_case[:charge_name], maximum_per_period: test_case[:maximum_per_period], frequency: test_case[:period][:label], letting_type: "supported housing", provider_type: "local authority")) end end context "when charges are not given" do [{ - period: { label: "weekly", value: 1 }, + period: { label: "weekly for 52 weeks", value: 1 }, charge: { field: "scharge", value: nil }, }, { - period: { label: "weekly", value: 1 }, + period: { label: "weekly for 52 weeks", value: 1 }, charge: { field: "pscharge", value: nil }, }, { - period: { label: "weekly", value: 1 }, + period: { label: "weekly for 52 weeks", value: 1 }, charge: { field: "supcharg", value: nil }, }].each do |test_case| it "does not error" do @@ -616,11 +688,11 @@ RSpec.describe Validations::FinancialValidations do end [{ - period: { label: "weekly", value: 1 }, + period: { label: "weekly for 52 weeks", value: 1 }, charge: { field: "scharge", value: 499 }, }, { - period: { label: "monthly", value: 4 }, + period: { label: "every calendar month", value: 4 }, charge: { field: "scharge", value: 2000 }, }, { @@ -628,11 +700,11 @@ RSpec.describe Validations::FinancialValidations do charge: { field: "scharge", value: 999 }, }, { - period: { label: "weekly", value: 1 }, + period: { label: "weekly for 52 weeks", value: 1 }, charge: { field: "pscharge", value: 199 }, }, { - period: { label: "monthly", value: 4 }, + period: { label: "every calendar month", value: 4 }, charge: { field: "pscharge", value: 800 }, }, { @@ -640,11 +712,11 @@ RSpec.describe Validations::FinancialValidations do charge: { field: "pscharge", value: 399 }, }, { - period: { label: "weekly", value: 1 }, + period: { label: "weekly for 52 weeks", value: 1 }, charge: { field: "supcharg", value: 199.99 }, }, { - period: { label: "monthly", value: 4 }, + period: { label: "every calendar month", value: 4 }, charge: { field: "supcharg", value: 800 }, }, {