From e8f43e2e3651183775d4e8e4cf31e051adc9b2c0 Mon Sep 17 00:00:00 2001 From: Kat Date: Tue, 15 Mar 2022 14:47:46 +0000 Subject: [PATCH] add other landlord validations --- app/models/case_log.rb | 4 + .../validations/financial_validations.rb | 18 ++- config/locales/en.yml | 3 + .../validations/financial_validations_spec.rb | 126 ++++++++++++++++++ 4 files changed, 146 insertions(+), 5 deletions(-) diff --git a/app/models/case_log.rb b/app/models/case_log.rb index 01bc93eaf..ec44ccfe3 100644 --- a/app/models/case_log.rb +++ b/app/models/case_log.rb @@ -272,6 +272,10 @@ class CaseLog < ApplicationRecord landlord == 1 end + def other_landlord? + landlord == 2 + end + private PIO = Postcodes::IO.new diff --git a/app/models/validations/financial_validations.rb b/app/models/validations/financial_validations.rb index 30c03a420..48dea2582 100644 --- a/app/models/validations/financial_validations.rb +++ b/app/models/validations/financial_validations.rb @@ -65,11 +65,19 @@ module Validations::FinancialValidations record.errors.add :tshortfall, I18n.t("validations.financial.tshortfall.more_than_rent") end - if record.scharge.present? && record.this_landlord? && record.weekly_value(record.scharge).present? - if !record.weekly_value(record.scharge).between?(0, 55) && record.is_general_needs? - record.errors.add :scharge, I18n.t("validations.financial.rent.scharge.this_landlord.general_needs") - elsif !record.weekly_value(record.scharge).between?(0, 280) && record.is_supported_housing? - record.errors.add :scharge, I18n.t("validations.financial.rent.scharge.this_landlord.supported_housing") + if record.scharge.present? && record.weekly_value(record.scharge).present? + if record.this_landlord? + if !record.weekly_value(record.scharge).between?(0, 55) && record.is_general_needs? + record.errors.add :scharge, I18n.t("validations.financial.rent.scharge.this_landlord.general_needs") + elsif !record.weekly_value(record.scharge).between?(0, 280) && record.is_supported_housing? + record.errors.add :scharge, I18n.t("validations.financial.rent.scharge.this_landlord.supported_housing") + end + elsif record.other_landlord? + if !record.weekly_value(record.scharge).between?(0, 45) && record.is_general_needs? + record.errors.add :scharge, I18n.t("validations.financial.rent.scharge.other_landlord.general_needs") + elsif !record.weekly_value(record.scharge).between?(0, 165) && record.is_supported_housing? + record.errors.add :scharge, I18n.t("validations.financial.rent.scharge.other_landlord.supported_housing") + end end end end diff --git a/config/locales/en.yml b/config/locales/en.yml index 272280bcf..1e1eb5e29 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -96,6 +96,9 @@ en: this_landlord: general_needs: "Service charge must be between 0 and 55 per week if the landlord is this landlord and it is a general needs letting" supported_housing: "Service charge must be between 0 and 280 per week if the landlord is this landlord and it is a suported housing letting" + other_landlord: + general_needs: "Service charge must be between 0 and 45 per week if the landlord is another RP and it is a general needs letting" + supported_housing: "Service charge must be between 0 and 165 per week if the landlord is another RP and it is a suported housing letting" household: reasonpref: diff --git a/spec/models/validations/financial_validations_spec.rb b/spec/models/validations/financial_validations_spec.rb index 9a758ac27..8542283c9 100644 --- a/spec/models/validations/financial_validations_spec.rb +++ b/spec/models/validations/financial_validations_spec.rb @@ -326,5 +326,131 @@ RSpec.describe Validations::FinancialValidations do end end end + + context "when the landlord is another RP" do + context "when needstype is general needs" do + it "does not allow the scharge to be outside of 0 and 45 range per week when period is weekly" do + record.needstype = 1 + record.landlord = 2 + record.period = 1 + record.scharge = 46 + financial_validator.validate_rent_amount(record) + expect(record.errors["scharge"]) + .to include(match I18n.t("validations.financial.rent.scharge.other_landlord.general_needs")) + end + + it "does allow the scharge to be between of 0 and 45 per week when period is weekly" do + record.needstype = 1 + record.landlord = 2 + record.period = 1 + record.scharge = 44 + financial_validator.validate_rent_amount(record) + expect(record.errors["scharge"]) + .to be_empty + end + + it "does not allow the scharge to be outside of 0 and 45 range per week when period is monthly" do + record.needstype = 1 + record.landlord = 2 + record.period = 4 + record.scharge = 200 + financial_validator.validate_rent_amount(record) + expect(record.errors["scharge"]) + .to include(match I18n.t("validations.financial.rent.scharge.other_landlord.general_needs")) + end + + it "does allow the scharge to be between of 0 and 45 per week when period is monthly" do + record.needstype = 1 + record.landlord = 2 + record.period = 4 + record.scharge = 160 + financial_validator.validate_rent_amount(record) + expect(record.errors["scharge"]) + .to be_empty + end + + it "does not allow the scharge to be outside of 0 and 45 range per week when period is every 2 weeks" do + record.needstype = 1 + record.landlord = 2 + record.period = 2 + record.scharge = 91 + financial_validator.validate_rent_amount(record) + expect(record.errors["scharge"]) + .to include(match I18n.t("validations.financial.rent.scharge.other_landlord.general_needs")) + end + + it "does allow the scharge to be between of 0 and 45 per week when period is every 2 weeks" do + record.needstype = 1 + record.landlord = 2 + record.period = 2 + record.scharge = 89 + financial_validator.validate_rent_amount(record) + expect(record.errors["scharge"]) + .to be_empty + end + end + + context "when needstype is supported housing" do + it "does not allow the scharge to be outside of 0 and 165 range per week when period is weekly" do + record.needstype = 0 + record.landlord = 2 + record.period = 1 + record.scharge = 165.90 + financial_validator.validate_rent_amount(record) + expect(record.errors["scharge"]) + .to include(match I18n.t("validations.financial.rent.scharge.other_landlord.supported_housing")) + end + + it "does allow the scharge to be between of 0 and 165 per week when period is weekly" do + record.needstype = 0 + record.landlord = 2 + record.period = 1 + record.scharge = 120.88 + financial_validator.validate_rent_amount(record) + expect(record.errors["scharge"]) + .to be_empty + end + + it "does not allow the scharge to be outside of 0 and 165 range per week when period is monthly" do + record.needstype = 0 + record.landlord = 2 + record.period = 4 + record.scharge = 750 + financial_validator.validate_rent_amount(record) + expect(record.errors["scharge"]) + .to include(match I18n.t("validations.financial.rent.scharge.other_landlord.supported_housing")) + end + + it "does allow the scharge to be between of 0 and 165 per week when period is monthly" do + record.needstype = 0 + record.landlord = 2 + record.period = 4 + record.scharge = 608 + financial_validator.validate_rent_amount(record) + expect(record.errors["scharge"]) + .to be_empty + end + + it "does not allow the scharge to be outside of 0 and 165 range per week when period is every 2 weeks" do + record.needstype = 0 + record.landlord = 2 + record.period = 2 + record.scharge = 330.50 + financial_validator.validate_rent_amount(record) + expect(record.errors["scharge"]) + .to include(match I18n.t("validations.financial.rent.scharge.other_landlord.supported_housing")) + end + + it "does allow the scharge to be between of 0 and 165 per week when period is every 2 weeks" do + record.needstype = 0 + record.landlord = 2 + record.period = 2 + record.scharge = 329.99 + financial_validator.validate_rent_amount(record) + expect(record.errors["scharge"]) + .to be_empty + end + end + end end end