From bf8587e9fb9ac9d6115f051fd42303623aedc00d Mon Sep 17 00:00:00 2001 From: kosiakkatrina <54268893+kosiakkatrina@users.noreply.github.com> Date: Tue, 21 Jun 2022 10:19:31 +0100 Subject: [PATCH] Add care home charges validation (#682) --- app/models/case_log.rb | 6 ++ .../validations/financial_validations.rb | 14 +++ config/locales/en.yml | 4 +- spec/fixtures/complete_case_log.json | 2 +- spec/fixtures/forms/2021_2022.json | 4 +- spec/models/rent_period_spec.rb | 2 +- .../validations/financial_validations_spec.rb | 94 ++++++++++++++++++- 7 files changed, 120 insertions(+), 6 deletions(-) diff --git a/app/models/case_log.rb b/app/models/case_log.rb index f3976fb06..32d53182a 100644 --- a/app/models/case_log.rb +++ b/app/models/case_log.rb @@ -131,6 +131,12 @@ class CaseLog < ApplicationRecord (field_value / 52 * num_of_weeks).round(2) end + def weekly_to_value_per_period(field_value) + num_of_weeks = NUM_OF_WEEKS_FROM_PERIOD[period] + + ((field_value * 52) / num_of_weeks).round(2) + end + def applicable_income_range return unless ecstat1 diff --git a/app/models/validations/financial_validations.rb b/app/models/validations/financial_validations.rb index bd3d008dd..c286e23ca 100644 --- a/app/models/validations/financial_validations.rb +++ b/app/models/validations/financial_validations.rb @@ -102,6 +102,20 @@ module Validations::FinancialValidations end end + def validate_care_home_charges(record) + if record.is_carehome? + period = record.form.get_question("period", record).label_from_value(record.period).downcase + if record.chcharge.blank? + record.errors.add :chcharge, I18n.t("validations.financial.carehome.not_provided", period:) + elsif !weekly_value_in_range(record, "chcharge", 0, 1000) + max_chcharge = record.weekly_to_value_per_period(1000) + max_chcharge = [record.form.get_question("chcharge", record).prefix, max_chcharge].join("") if record.form.get_question("chcharge", record).present? + record.errors.add :period, I18n.t("validations.financial.carehome.over_1000", period:, min_chcharge: "£0", max_chcharge:) + record.errors.add :chcharge, I18n.t("validations.financial.carehome.over_1000", period:, min_chcharge: "£0", max_chcharge:) + end + end + end + private CHARGE_MAXIMUMS = { diff --git a/config/locales/en.yml b/config/locales/en.yml index 881179317..726dcacc0 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -145,7 +145,9 @@ en: under_10: "Total charge must be at least £10 per week" rent_period: invalid_for_org: "%{org_name} does not charge rent %{rent_period}" - + carehome: + over_1000: "Household rent and other charges must be between %{min_chcharge} and %{max_chcharge} if paying %{period}" + not_provided: "Enter how much rent and other charges the household pays %{period}" household: reasonpref: not_homeless: "Answer cannot be ‘homeless or about to lose their home’ as you already told us the tenant was not homeless immediately prior to this letting" diff --git a/spec/fixtures/complete_case_log.json b/spec/fixtures/complete_case_log.json index d63e92160..9cb1fc62f 100644 --- a/spec/fixtures/complete_case_log.json +++ b/spec/fixtures/complete_case_log.json @@ -130,7 +130,7 @@ "new_build_handover_date": "01/01/2019", "has_benefits": 1, "household_charge": 0, - "is_carehome": 1, + "is_carehome": 0, "sheltered": 0, "declaration": 1, "referral": 1 diff --git a/spec/fixtures/forms/2021_2022.json b/spec/fixtures/forms/2021_2022.json index 97e150b4f..51fe74714 100644 --- a/spec/fixtures/forms/2021_2022.json +++ b/spec/fixtures/forms/2021_2022.json @@ -703,11 +703,11 @@ "header": "Which period are rent and other charges due?", "type": "radio", "answer_options": { - "2": { + "1": { "value": "Weekly for 52 weeks" }, "3": { - "value": "Every 2 weeks" + "value": "Every 4 weeks" } } }, diff --git a/spec/models/rent_period_spec.rb b/spec/models/rent_period_spec.rb index 55252977a..5749d7660 100644 --- a/spec/models/rent_period_spec.rb +++ b/spec/models/rent_period_spec.rb @@ -10,7 +10,7 @@ RSpec.describe RentPeriod, type: :model do it "maps rent period id to display names" do expect(described_class.rent_period_mappings).to be_a(Hash) - expect(described_class.rent_period_mappings["2"]).to eq({ "value" => "Weekly for 52 weeks" }) + expect(described_class.rent_period_mappings["1"]).to eq({ "value" => "Weekly for 52 weeks" }) end end end diff --git a/spec/models/validations/financial_validations_spec.rb b/spec/models/validations/financial_validations_spec.rb index 7c00a1580..7b398d077 100644 --- a/spec/models/validations/financial_validations_spec.rb +++ b/spec/models/validations/financial_validations_spec.rb @@ -102,7 +102,7 @@ RSpec.describe Validations::FinancialValidations do .to include(match I18n.t( "validations.financial.rent_period.invalid_for_org", org_name: organisation.name, - rent_period: "every 2 weeks", + rent_period: "every 4 weeks", )) end end @@ -835,5 +835,97 @@ RSpec.describe Validations::FinancialValidations do end end end + + context "when the accommodation is care home" do + before do + record.is_carehome = 1 + end + + context "and charges are over the valid limit (£1000 per week)" do + it "validates charge when period is weekly for 52 weeks" do + record.period = 1 + record.chcharge = 1001 + financial_validator.validate_care_home_charges(record) + expect(record.errors["chcharge"]) + .to include(match I18n.t("validations.financial.carehome.over_1000", min_chcharge: "£0", period: "weekly for 52 weeks", max_chcharge: 1000)) + expect(record.errors["period"]) + .to include(match I18n.t("validations.financial.carehome.over_1000", min_chcharge: "£0", period: "weekly for 52 weeks", max_chcharge: 1000)) + end + + it "validates charge when period is monthly" do + record.period = 4 + record.chcharge = 4334 + financial_validator.validate_care_home_charges(record) + expect(record.errors["chcharge"]) + .to include(match I18n.t("validations.financial.carehome.over_1000", min_chcharge: "£0", period: "4", max_chcharge: 4333)) + expect(record.errors["period"]) + .to include(match I18n.t("validations.financial.carehome.over_1000", min_chcharge: "£0", period: "4", max_chcharge: 4333)) + end + + it "validates charge when period is every 2 weeks" do + record.period = 2 + record.chcharge = 2001 + financial_validator.validate_care_home_charges(record) + expect(record.errors["chcharge"]) + .to include(match I18n.t("validations.financial.carehome.over_1000", min_chcharge: "£0", period: "2", max_chcharge: 2000)) + expect(record.errors["period"]) + .to include(match I18n.t("validations.financial.carehome.over_1000", min_chcharge: "£0", period: "2", max_chcharge: 2000)) + end + + it "validates charge when period is every 4 weeks" do + record.period = 3 + record.chcharge = 4001 + financial_validator.validate_care_home_charges(record) + expect(record.errors["chcharge"]) + .to include(match I18n.t("validations.financial.carehome.over_1000", min_chcharge: "£0", period: "every 4 weeks", max_chcharge: 4000)) + expect(record.errors["period"]) + .to include(match I18n.t("validations.financial.carehome.over_1000", min_chcharge: "£0", period: "every 4 weeks", max_chcharge: 4000)) + end + end + + context "and charges are within the valid limit (£1000 per week)" do + it "does not throw error when period is weekly for 52 weeks" do + record.period = 1 + record.chcharge = 999 + financial_validator.validate_care_home_charges(record) + expect(record.errors["chcharge"]).to be_empty + expect(record.errors["period"]).to be_empty + end + + it "does not throw error when period is monthly" do + record.period = 4 + record.chcharge = 4333 + financial_validator.validate_care_home_charges(record) + expect(record.errors["chcharge"]).to be_empty + expect(record.errors["period"]).to be_empty + end + + it "does not throw error when period is every 2 weeks" do + record.period = 2 + record.chcharge = 1999.99 + financial_validator.validate_care_home_charges(record) + expect(record.errors["chcharge"]).to be_empty + expect(record.errors["period"]).to be_empty + end + + it "does not throw error when period is every 4 weeks" do + record.period = 3 + record.chcharge = 3999 + financial_validator.validate_care_home_charges(record) + expect(record.errors["chcharge"]).to be_empty + expect(record.errors["period"]).to be_empty + end + end + + context "and charges are not provided" do + it "throws and error" do + record.period = 3 + record.chcharge = nil + financial_validator.validate_care_home_charges(record) + expect(record.errors["chcharge"]) + .to include(match I18n.t("validations.financial.carehome.not_provided", period: "every 4 weeks")) + end + end + end end end