diff --git a/app/models/derived_variables/case_log_variables.rb b/app/models/derived_variables/case_log_variables.rb index 16fff3fd0..99f5307e2 100644 --- a/app/models/derived_variables/case_log_variables.rb +++ b/app/models/derived_variables/case_log_variables.rb @@ -34,15 +34,11 @@ module DerivedVariables::CaseLogVariables self.wpschrge = weekly_value(pscharge) if pscharge.present? self.wsupchrg = weekly_value(supcharg) if supcharg.present? self.wtcharge = weekly_value(tcharge) if tcharge.present? - if is_supported_housing? && chcharge.present? - self.wchchrg = weekly_value(chcharge) - end + self.wtshortfall = weekly_value(tshortfall) if tshortfall && receives_housing_related_benefits? + self.wchchrg = weekly_value(chcharge) if is_supported_housing? && chcharge.present? end self.has_benefits = get_has_benefits self.tshortfall_known = 0 if tshortfall - self.wtshortfall = if tshortfall && receives_housing_related_benefits? - weekly_value(tshortfall) - end self.nocharge = household_charge&.zero? ? 1 : 0 self.housingneeds = get_housingneeds if is_renewal? diff --git a/app/models/validations/financial_validations.rb b/app/models/validations/financial_validations.rb index c286e23ca..9bf63456e 100644 --- a/app/models/validations/financial_validations.rb +++ b/app/models/validations/financial_validations.rb @@ -71,9 +71,13 @@ module Validations::FinancialValidations end def validate_rent_amount(record) - if record.brent.present? && record.tshortfall.present? && record.brent < record.tshortfall * 2 - 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") + if record.wtshortfall + if record.wrent && (record.wtshortfall > record.wrent) + record.errors.add :tshortfall, I18n.t("validations.financial.tshortfall.more_than_rent") + record.errors.add :brent, I18n.t("validations.financial.rent.less_than_shortfall") + elsif record.wtshortfall < 0.01 + record.errors.add :tshortfall, I18n.t("validations.financial.tshortfall.must_be_positive") + end end if record.tcharge.present? && weekly_value_in_range(record, "tcharge", 0, 9.99) diff --git a/config/locales/en.yml b/config/locales/en.yml index 37d04a013..c654c9e77 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -97,7 +97,8 @@ en: financial: tshortfall: outstanding_amount_not_required: "You must not answer the outstanding amount question if you don’t have outstanding rent or charges" - more_than_rent: "Answer must be less than half of the basic rent amount" + more_than_rent: "Answer must be less than the basic rent amount" + must_be_positive: "Answer must be more than £0.01 as you told us there is an outstanding amount" hbrentshortfall: outstanding_no_benefits: "Answer cannot be ‘yes’ to outstanding amount for basic rent or charges if tenant does not receive housing benefit or Universal Credit or you‘re not sure" benefits: @@ -109,7 +110,7 @@ en: earnings_missing: "Enter how much income the household has in total" negative_currency: "Enter an amount above 0" rent: - less_than_double_shortfall: "Answer must be more than double the shortfall in basic rent" + less_than_shortfall: "Answer must be more than the shortfall in basic rent" scharge: private_registered_provider: general_needs: "Service charge must be between £0 and £55 per week if the landlord is a private registered provider and it is a general needs letting" diff --git a/spec/models/validations/financial_validations_spec.rb b/spec/models/validations/financial_validations_spec.rb index 7b398d077..2e114bfb4 100644 --- a/spec/models/validations/financial_validations_spec.rb +++ b/spec/models/validations/financial_validations_spec.rb @@ -77,14 +77,43 @@ RSpec.describe Validations::FinancialValidations do end context "when outstanding rent or charges is yes" do + let(:record) { FactoryBot.create(:case_log, :about_completed) } + it "expects that a shortfall is provided" do record.hbrentshortfall = 1 record.tshortfall = 99 financial_validator.validate_outstanding_rent_amount(record) expect(record.errors["tshortfall"]).to be_empty end + + it "validates that the shortfall is a positive £ amount" do + record.hb = 6 + record.hbrentshortfall = 1 + record.tshortfall_known = 0 + record.tshortfall = 0 + record.period = 2 + record.set_derived_fields! + financial_validator.validate_rent_amount(record) + expect(record.errors["tshortfall"]) + .to include(match I18n.t("validations.financial.tshortfall.must_be_positive")) + end + + it "validates that basic rent is no less than the shortfall" do + record.hb = 6 + record.hbrentshortfall = 1 + record.tshortfall_known = 0 + record.tshortfall = 299.50 + record.brent = 198 + record.period = 2 + record.set_derived_fields! + financial_validator.validate_rent_amount(record) + expect(record.errors["brent"]) + .to include(match I18n.t("validations.financial.rent.less_than_shortfall")) + expect(record.errors["tshortfall"]) + .to include(match I18n.t("validations.financial.tshortfall.more_than_rent")) + end + end end - end describe "rent period validations" do let(:organisation) { FactoryBot.create(:organisation) } @@ -213,19 +242,6 @@ RSpec.describe Validations::FinancialValidations do end describe "rent and charges validations" do - context "when shortfall amount is provided" do - it "validates that basic rent is no less than double the shortfall" do - record.hbrentshortfall = 2 - record.tshortfall = 99.50 - record.brent = 198 - financial_validator.validate_rent_amount(record) - expect(record.errors["brent"]) - .to include(match I18n.t("validations.financial.rent.less_than_double_shortfall", shortfall: 198)) - expect(record.errors["tshortfall"]) - .to include(match I18n.t("validations.financial.tshortfall.more_than_rent")) - end - end - context "when the owning organisation is a private registered provider" do before { record.owning_organisation.provider_type = 2 }