From c0efefe6459584d6ff7f9f3e347d91d707870d0c Mon Sep 17 00:00:00 2001 From: baarkerlounger Date: Mon, 14 Mar 2022 14:12:08 +0000 Subject: [PATCH] Derive Weekly shortfall value --- app/models/case_log.rb | 29 ++ .../validations/financial_validations.rb | 9 +- spec/models/case_log_spec.rb | 261 ++++++++++++++++++ 3 files changed, 294 insertions(+), 5 deletions(-) diff --git a/app/models/case_log.rb b/app/models/case_log.rb index a1d7a28ea..faf8a702f 100644 --- a/app/models/case_log.rb +++ b/app/models/case_log.rb @@ -239,6 +239,30 @@ class CaseLog < ApplicationRecord reason == 1 end + def receives_housing_benefit_only? + hb == 1 + end + + def receives_housing_benefit_and_universal_credit? + hb == 8 + end + + def receives_uc_with_housing_element_excl_housing_benefit? + hb == 6 + end + + def receives_no_benefits? + hb == 9 + end + + def receives_universal_credit_but_no_housing_benefit? + hb == 7 + end + + def benefits_unknown? + hb == 3 + end + private PIO = Postcodes::IO.new @@ -327,6 +351,11 @@ private self.wtcharge = weekly_value(tcharge) if tcharge.present? end self.has_benefits = get_has_benefits + if tshortfall && (receives_housing_benefit_only? || + receives_housing_benefit_and_universal_credit? || + receives_uc_with_housing_element_excl_housing_benefit?) + self.wtshortfall = weekly_value(tshortfall) + end self.nocharge = household_charge&.zero? ? 1 : 0 self.underoccupation_benefitcap = 3 if renewal == 1 && year == 2021 self.ethnic = ethnic || ethnic_group diff --git a/app/models/validations/financial_validations.rb b/app/models/validations/financial_validations.rb index 7721ce9f7..59a38e3a7 100644 --- a/app/models/validations/financial_validations.rb +++ b/app/models/validations/financial_validations.rb @@ -51,11 +51,10 @@ module Validations::FinancialValidations end def validate_tshortfall(record) - hb_donotknow = record.hb == 3 - hb_none = record.hb == 9 - hb_uc_no_hb = record.hb == 7 - - if record.has_hbrentshortfall? && (hb_donotknow || hb_none || hb_uc_no_hb) + if record.has_hbrentshortfall? && + (record.benefits_unknown? || + record.receives_no_benefits? || + record.receives_universal_credit_but_no_housing_benefit?) record.errors.add :tshortfall, I18n.t("validations.financial.hbrentshortfall.outstanding_no_benefits") end end diff --git a/spec/models/case_log_spec.rb b/spec/models/case_log_spec.rb index a70aecc7b..ab9e44a90 100644 --- a/spec/models/case_log_spec.rb +++ b/spec/models/case_log_spec.rb @@ -334,6 +334,35 @@ RSpec.describe CaseLog do expect(case_log.wtcharge).to eq(50.0) expect(record_from_db["wtcharge"]).to eq(50.0) end + + context "when the tenant has an outstanding amount after benefits" do + context "when tenant is in receipt of housing benefit" do + it "correctly derives and saves weekly total shortfall" do + case_log.update!(hbrentshortfall: 0, tshortfall: 100, period: 0, hb: 1) + record_from_db = ActiveRecord::Base.connection.execute("select wtshortfall from case_logs where id=#{case_log.id}").to_a[0] + expect(case_log.wtshortfall).to eq(50.0) + expect(record_from_db["wtshortfall"]).to eq(50.0) + end + end + + context "when tenant is in receipt of universal credit with housing element exc. housing benefit" do + it "correctly derives and saves weekly total shortfall" do + case_log.update!(hbrentshortfall: 0, tshortfall: 100, period: 0, hb: 6) + record_from_db = ActiveRecord::Base.connection.execute("select wtshortfall from case_logs where id=#{case_log.id}").to_a[0] + expect(case_log.wtshortfall).to eq(50.0) + expect(record_from_db["wtshortfall"]).to eq(50.0) + end + end + + context "when tenant is in receipt of housing benefit and universal credit" do + it "correctly derives and saves weekly total shortfall" do + case_log.update!(hbrentshortfall: 0, tshortfall: 100, period: 0, hb: 8) + record_from_db = ActiveRecord::Base.connection.execute("select wtshortfall from case_logs where id=#{case_log.id}").to_a[0] + expect(case_log.wtshortfall).to eq(50.0) + expect(record_from_db["wtshortfall"]).to eq(50.0) + end + end + end end context "when rent is paid every 4 weeks" do @@ -371,6 +400,35 @@ RSpec.describe CaseLog do expect(case_log.wtcharge).to eq(30.0) expect(record_from_db["wtcharge"]).to eq(30.0) end + + context "when the tenant has an outstanding amount after benefits" do + context "when tenant is in receipt of housing benefit" do + it "correctly derives and saves weekly total shortfall" do + case_log.update!(hbrentshortfall: 0, tshortfall: 120, period: 1, hb: 1) + record_from_db = ActiveRecord::Base.connection.execute("select wtshortfall from case_logs where id=#{case_log.id}").to_a[0] + expect(case_log.wtshortfall).to eq(30.0) + expect(record_from_db["wtshortfall"]).to eq(30.0) + end + end + + context "when tenant is in receipt of universal credit with housing element exc. housing benefit" do + it "correctly derives and saves weekly total shortfall" do + case_log.update!(hbrentshortfall: 0, tshortfall: 120, period: 1, hb: 6) + record_from_db = ActiveRecord::Base.connection.execute("select wtshortfall from case_logs where id=#{case_log.id}").to_a[0] + expect(case_log.wtshortfall).to eq(30.0) + expect(record_from_db["wtshortfall"]).to eq(30.0) + end + end + + context "when tenant is in receipt of housing benefit and universal credit" do + it "correctly derives and saves weekly total shortfall" do + case_log.update!(hbrentshortfall: 0, tshortfall: 120, period: 1, hb: 8) + record_from_db = ActiveRecord::Base.connection.execute("select wtshortfall from case_logs where id=#{case_log.id}").to_a[0] + expect(case_log.wtshortfall).to eq(30.0) + expect(record_from_db["wtshortfall"]).to eq(30.0) + end + end + end end context "when rent is paid every calendar month" do @@ -408,6 +466,35 @@ RSpec.describe CaseLog do expect(case_log.wtcharge).to eq(30.0) expect(record_from_db["wtcharge"]).to eq(30.0) end + + context "when the tenant has an outstanding amount after benefits" do + context "when tenant is in receipt of housing benefit" do + it "correctly derives and saves weekly total shortfall" do + case_log.update!(hbrentshortfall: 0, tshortfall: 130, period: 2, hb: 1) + record_from_db = ActiveRecord::Base.connection.execute("select wtshortfall from case_logs where id=#{case_log.id}").to_a[0] + expect(case_log.wtshortfall).to eq(30.0) + expect(record_from_db["wtshortfall"]).to eq(30.0) + end + end + + context "when tenant is in receipt of universal credit with housing element exc. housing benefit" do + it "correctly derives and saves weekly total shortfall" do + case_log.update!(hbrentshortfall: 0, tshortfall: 130, period: 2, hb: 6) + record_from_db = ActiveRecord::Base.connection.execute("select wtshortfall from case_logs where id=#{case_log.id}").to_a[0] + expect(case_log.wtshortfall).to eq(30.0) + expect(record_from_db["wtshortfall"]).to eq(30.0) + end + end + + context "when tenant is in receipt of housing benefit and universal credit" do + it "correctly derives and saves weekly total shortfall" do + case_log.update!(hbrentshortfall: 0, tshortfall: 130, period: 2, hb: 8) + record_from_db = ActiveRecord::Base.connection.execute("select wtshortfall from case_logs where id=#{case_log.id}").to_a[0] + expect(case_log.wtshortfall).to eq(30.0) + expect(record_from_db["wtshortfall"]).to eq(30.0) + end + end + end end context "when rent is paid weekly for 50 weeks" do @@ -445,6 +532,35 @@ RSpec.describe CaseLog do expect(case_log.wtcharge).to eq(125.0) expect(record_from_db["wtcharge"]).to eq(125.0) end + + context "when the tenant has an outstanding amount after benefits" do + context "when tenant is in receipt of housing benefit" do + it "correctly derives and saves weekly total shortfall" do + case_log.update!(hbrentshortfall: 0, tshortfall: 130, period: 3, hb: 1) + record_from_db = ActiveRecord::Base.connection.execute("select wtshortfall from case_logs where id=#{case_log.id}").to_a[0] + expect(case_log.wtshortfall).to eq(125.0) + expect(record_from_db["wtshortfall"]).to eq(125.0) + end + end + + context "when tenant is in receipt of universal credit with housing element exc. housing benefit" do + it "correctly derives and saves weekly total shortfall" do + case_log.update!(hbrentshortfall: 0, tshortfall: 130, period: 3, hb: 6) + record_from_db = ActiveRecord::Base.connection.execute("select wtshortfall from case_logs where id=#{case_log.id}").to_a[0] + expect(case_log.wtshortfall).to eq(125.0) + expect(record_from_db["wtshortfall"]).to eq(125.0) + end + end + + context "when tenant is in receipt of housing benefit and universal credit" do + it "correctly derives and saves weekly total shortfall" do + case_log.update!(hbrentshortfall: 0, tshortfall: 130, period: 3, hb: 8) + record_from_db = ActiveRecord::Base.connection.execute("select wtshortfall from case_logs where id=#{case_log.id}").to_a[0] + expect(case_log.wtshortfall).to eq(125.0) + expect(record_from_db["wtshortfall"]).to eq(125.0) + end + end + end end context "when rent is paid weekly for 49 weeks" do @@ -482,6 +598,35 @@ RSpec.describe CaseLog do expect(case_log.wtcharge).to eq(122.5) expect(record_from_db["wtcharge"]).to eq(122.5) end + + context "when the tenant has an outstanding amount after benefits" do + context "when tenant is in receipt of housing benefit" do + it "correctly derives and saves weekly total shortfall" do + case_log.update!(hbrentshortfall: 0, tshortfall: 130, period: 4, hb: 1) + record_from_db = ActiveRecord::Base.connection.execute("select wtshortfall from case_logs where id=#{case_log.id}").to_a[0] + expect(case_log.wtshortfall).to eq(122.5) + expect(record_from_db["wtshortfall"]).to eq(122.5) + end + end + + context "when tenant is in receipt of universal credit with housing element exc. housing benefit" do + it "correctly derives and saves weekly total shortfall" do + case_log.update!(hbrentshortfall: 0, tshortfall: 130, period: 4, hb: 6) + record_from_db = ActiveRecord::Base.connection.execute("select wtshortfall from case_logs where id=#{case_log.id}").to_a[0] + expect(case_log.wtshortfall).to eq(122.5) + expect(record_from_db["wtshortfall"]).to eq(122.5) + end + end + + context "when tenant is in receipt of housing benefit and universal credit" do + it "correctly derives and saves weekly total shortfall" do + case_log.update!(hbrentshortfall: 0, tshortfall: 130, period: 4, hb: 8) + record_from_db = ActiveRecord::Base.connection.execute("select wtshortfall from case_logs where id=#{case_log.id}").to_a[0] + expect(case_log.wtshortfall).to eq(122.5) + expect(record_from_db["wtshortfall"]).to eq(122.5) + end + end + end end context "when rent is paid weekly for 48 weeks" do @@ -519,6 +664,35 @@ RSpec.describe CaseLog do expect(case_log.wtcharge).to eq(120.0) expect(record_from_db["wtcharge"]).to eq(120.0) end + + context "when the tenant has an outstanding amount after benefits" do + context "when tenant is in receipt of housing benefit" do + it "correctly derives and saves weekly total shortfall" do + case_log.update!(hbrentshortfall: 0, tshortfall: 130, period: 5, hb: 1) + record_from_db = ActiveRecord::Base.connection.execute("select wtshortfall from case_logs where id=#{case_log.id}").to_a[0] + expect(case_log.wtshortfall).to eq(120.0) + expect(record_from_db["wtshortfall"]).to eq(120.0) + end + end + + context "when tenant is in receipt of universal credit with housing element exc. housing benefit" do + it "correctly derives and saves weekly total shortfall" do + case_log.update!(hbrentshortfall: 0, tshortfall: 130, period: 5, hb: 6) + record_from_db = ActiveRecord::Base.connection.execute("select wtshortfall from case_logs where id=#{case_log.id}").to_a[0] + expect(case_log.wtshortfall).to eq(120.0) + expect(record_from_db["wtshortfall"]).to eq(120.0) + end + end + + context "when tenant is in receipt of housing benefit and universal credit" do + it "correctly derives and saves weekly total shortfall" do + case_log.update!(hbrentshortfall: 0, tshortfall: 130, period: 5, hb: 8) + record_from_db = ActiveRecord::Base.connection.execute("select wtshortfall from case_logs where id=#{case_log.id}").to_a[0] + expect(case_log.wtshortfall).to eq(120.0) + expect(record_from_db["wtshortfall"]).to eq(120.0) + end + end + end end context "when rent is paid weekly for 47 weeks" do @@ -556,6 +730,35 @@ RSpec.describe CaseLog do expect(case_log.wtcharge).to eq(117.5) expect(record_from_db["wtcharge"]).to eq(117.5) end + + context "when the tenant has an outstanding amount after benefits" do + context "when tenant is in receipt of housing benefit" do + it "correctly derives and saves weekly total shortfall" do + case_log.update!(hbrentshortfall: 0, tshortfall: 130, period: 6, hb: 1) + record_from_db = ActiveRecord::Base.connection.execute("select wtshortfall from case_logs where id=#{case_log.id}").to_a[0] + expect(case_log.wtshortfall).to eq(117.5) + expect(record_from_db["wtshortfall"]).to eq(117.5) + end + end + + context "when tenant is in receipt of universal credit with housing element exc. housing benefit" do + it "correctly derives and saves weekly total shortfall" do + case_log.update!(hbrentshortfall: 0, tshortfall: 130, period: 6, hb: 6) + record_from_db = ActiveRecord::Base.connection.execute("select wtshortfall from case_logs where id=#{case_log.id}").to_a[0] + expect(case_log.wtshortfall).to eq(117.5) + expect(record_from_db["wtshortfall"]).to eq(117.5) + end + end + + context "when tenant is in receipt of housing benefit and universal credit" do + it "correctly derives and saves weekly total shortfall" do + case_log.update!(hbrentshortfall: 0, tshortfall: 130, period: 6, hb: 8) + record_from_db = ActiveRecord::Base.connection.execute("select wtshortfall from case_logs where id=#{case_log.id}").to_a[0] + expect(case_log.wtshortfall).to eq(117.5) + expect(record_from_db["wtshortfall"]).to eq(117.5) + end + end + end end context "when rent is paid weekly for 46 weeks" do @@ -593,6 +796,35 @@ RSpec.describe CaseLog do expect(case_log.wtcharge).to eq(115.0) expect(record_from_db["wtcharge"]).to eq(115.0) end + + context "when the tenant has an outstanding amount after benefits" do + context "when tenant is in receipt of housing benefit" do + it "correctly derives and saves weekly total shortfall" do + case_log.update!(hbrentshortfall: 0, tshortfall: 130, period: 7, hb: 1) + record_from_db = ActiveRecord::Base.connection.execute("select wtshortfall from case_logs where id=#{case_log.id}").to_a[0] + expect(case_log.wtshortfall).to eq(115.0) + expect(record_from_db["wtshortfall"]).to eq(115.0) + end + end + + context "when tenant is in receipt of universal credit with housing element exc. housing benefit" do + it "correctly derives and saves weekly total shortfall" do + case_log.update!(hbrentshortfall: 0, tshortfall: 130, period: 7, hb: 6) + record_from_db = ActiveRecord::Base.connection.execute("select wtshortfall from case_logs where id=#{case_log.id}").to_a[0] + expect(case_log.wtshortfall).to eq(115.0) + expect(record_from_db["wtshortfall"]).to eq(115.0) + end + end + + context "when tenant is in receipt of housing benefit and universal credit" do + it "correctly derives and saves weekly total shortfall" do + case_log.update!(hbrentshortfall: 0, tshortfall: 130, period: 7, hb: 8) + record_from_db = ActiveRecord::Base.connection.execute("select wtshortfall from case_logs where id=#{case_log.id}").to_a[0] + expect(case_log.wtshortfall).to eq(115.0) + expect(record_from_db["wtshortfall"]).to eq(115.0) + end + end + end end context "when rent is paid weekly for 52 weeks" do @@ -630,6 +862,35 @@ RSpec.describe CaseLog do expect(case_log.wtcharge).to eq(130.0) expect(record_from_db["wtcharge"]).to eq(130.0) end + + context "when the tenant has an outstanding amount after benefits" do + context "when tenant is in receipt of housing benefit" do + it "correctly derives and saves weekly total shortfall" do + case_log.update!(hbrentshortfall: 0, tshortfall: 130, period: 8, hb: 1) + record_from_db = ActiveRecord::Base.connection.execute("select wtshortfall from case_logs where id=#{case_log.id}").to_a[0] + expect(case_log.wtshortfall).to eq(130.0) + expect(record_from_db["wtshortfall"]).to eq(130.0) + end + end + + context "when tenant is in receipt of universal credit with housing element exc. housing benefit" do + it "correctly derives and saves weekly total shortfall" do + case_log.update!(hbrentshortfall: 0, tshortfall: 130, period: 8, hb: 6) + record_from_db = ActiveRecord::Base.connection.execute("select wtshortfall from case_logs where id=#{case_log.id}").to_a[0] + expect(case_log.wtshortfall).to eq(130.0) + expect(record_from_db["wtshortfall"]).to eq(130.0) + end + end + + context "when tenant is in receipt of housing benefit and universal credit" do + it "correctly derives and saves weekly total shortfall" do + case_log.update!(hbrentshortfall: 0, tshortfall: 130, period: 8, hb: 8) + record_from_db = ActiveRecord::Base.connection.execute("select wtshortfall from case_logs where id=#{case_log.id}").to_a[0] + expect(case_log.wtshortfall).to eq(130.0) + expect(record_from_db["wtshortfall"]).to eq(130.0) + end + end + end end end