From 5c104d36d5b51b482fa3e8fcfbc303e511a85c22 Mon Sep 17 00:00:00 2001 From: Kat Date: Mon, 14 Mar 2022 16:44:50 +0000 Subject: [PATCH] Add scharge validation --- app/models/case_log.rb | 2 + .../validations/financial_validations.rb | 5 ++ config/locales/en.yml | 3 + .../validations/financial_validations_spec.rb | 62 +++++++++++++++++++ 4 files changed, 72 insertions(+) diff --git a/app/models/case_log.rb b/app/models/case_log.rb index 09cac5d5a..9b6350cbf 100644 --- a/app/models/case_log.rb +++ b/app/models/case_log.rb @@ -266,6 +266,8 @@ class CaseLog < ApplicationRecord def benefits_unknown? hb == 3 + def this_landlord? + landlord == 1 end private diff --git a/app/models/validations/financial_validations.rb b/app/models/validations/financial_validations.rb index 5d3d13bc1..223a48880 100644 --- a/app/models/validations/financial_validations.rb +++ b/app/models/validations/financial_validations.rb @@ -64,5 +64,10 @@ module Validations::FinancialValidations record.errors.add :brent, I18n.t("validations.financial.rent.less_than_double_shortfall", tshortfall: record.tshortfall * 2) 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? && !record.weekly_value(record.scharge).between?(0, 55) + record.errors.add :scharge, I18n.t("validations.financial.rent.scharge.this_landlord.general_needs") + record.errors.add :landlord, I18n.t("validations.organisation.landlord.invalid_scharge") + end end end diff --git a/config/locales/en.yml b/config/locales/en.yml index 5cb80710f..fccf6ffe6 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -92,6 +92,9 @@ en: negative_currency: "Enter an amount above 0" rent: less_than_double_shortfall: "Answer cannot be less than double shortfall in basic rent" + scharge: + this_landlord: + general_needs: "Service charge must be between 0 and 55 per week if the landlord is this landlord" household: reasonpref: diff --git a/spec/models/validations/financial_validations_spec.rb b/spec/models/validations/financial_validations_spec.rb index 63d107275..10060464f 100644 --- a/spec/models/validations/financial_validations_spec.rb +++ b/spec/models/validations/financial_validations_spec.rb @@ -200,5 +200,67 @@ RSpec.describe Validations::FinancialValidations do .to include(match I18n.t("validations.financial.tshortfall.more_than_rent")) end end + + context "when the landlord is this landlord and needstype is general needs" do + it "does not allow the scharge to be outside of 0 and 55 range per week when period is weekly" do + record.needstype = 1 + record.landlord = 1 + record.period = 7 + record.scharge = 56 + financial_validator.validate_rent_amount(record) + expect(record.errors["scharge"]) + .to include(match I18n.t("validations.financial.rent.scharge.this_landlord.general_needs")) + end + + it "does allow the scharge to be between of 0 and 55 per week when period is weekly" do + record.needstype = 1 + record.landlord = 1 + record.period = 7 + record.scharge = 54 + 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 55 range per week when period is monthly" do + record.needstype = 1 + record.landlord = 1 + record.period = 4 + record.scharge = 300 + financial_validator.validate_rent_amount(record) + expect(record.errors["scharge"]) + .to include(match I18n.t("validations.financial.rent.scharge.this_landlord.general_needs")) + end + + it "does allow the scharge to be between of 0 and 55 per week when period is monthly" do + record.needstype = 1 + record.landlord = 1 + record.period = 4 + record.scharge = 220 + 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 55 range per week when period is every 2 weeks" do + record.needstype = 1 + record.landlord = 1 + record.period = 2 + record.scharge = 111 + financial_validator.validate_rent_amount(record) + expect(record.errors["scharge"]) + .to include(match I18n.t("validations.financial.rent.scharge.this_landlord.general_needs")) + end + + it "does allow the scharge to be between of 0 and 55 per week when period is every 2 weeks" do + record.needstype = 1 + record.landlord = 1 + record.period = 2 + record.scharge = 109 + financial_validator.validate_rent_amount(record) + expect(record.errors["scharge"]) + .to be_empty + end + end end end