From 9fb3bac3896fed08a5b2186ae7da2933c43d4011 Mon Sep 17 00:00:00 2001 From: Arthur Campbell <51094020+arfacamble@users.noreply.github.com> Date: Mon, 17 Apr 2023 13:08:50 +0100 Subject: [PATCH] CLDC-1852 add weekly rent period and reorder options (#1507) * add in a new option for the period question and reorder existing answer options * ensure that the lettings log calculates values correctly that relate to the period * ensure that depends on conditions related to period take account of the new option relatedly create methods on LettingsLog to allow improved readability in these depends on conditions refactor depends on conditions with these new method * update the 22_23 json form to use the new option as well * update tests to cover deriving variables that depend on period update test fixture to allow this * refactor some depends on and amend LEttingsLog method to improve readability * remove forgotten focus in spec file found in linting check * amend context descriptino in new tests * refactor a couple more depends on for readability --- .../form/lettings/pages/care_home_weekly.rb | 14 +- .../form/lettings/pages/outstanding_amount.rb | 2 +- .../form/lettings/pages/rent_4_weekly.rb | 6 +- .../form/lettings/pages/rent_bi_weekly.rb | 6 +- .../form/lettings/pages/rent_monthly.rb | 6 +- app/models/form/lettings/pages/rent_weekly.rb | 26 +-- app/models/form/lettings/questions/period.rb | 9 +- .../form/lettings/questions/tshortfall.rb | 1 + app/models/lettings_log.rb | 26 ++- .../validations/financial_validations.rb | 4 +- config/forms/2022_2023.json | 201 +++--------------- spec/fixtures/forms/2021_2022.json | 19 +- spec/models/lettings_log_spec.rb | 81 +++++++ 13 files changed, 159 insertions(+), 242 deletions(-) diff --git a/app/models/form/lettings/pages/care_home_weekly.rb b/app/models/form/lettings/pages/care_home_weekly.rb index ee8f7962e..56165ce62 100644 --- a/app/models/form/lettings/pages/care_home_weekly.rb +++ b/app/models/form/lettings/pages/care_home_weekly.rb @@ -3,18 +3,8 @@ class Form::Lettings::Pages::CareHomeWeekly < ::Form::Page super @id = "care_home_weekly" @depends_on = [ - { "period" => 1, "needstype" => 2, "household_charge" => 0 }, - { "period" => 1, "needstype" => 2, "household_charge" => nil }, - { "period" => 5, "needstype" => 2, "household_charge" => 0 }, - { "period" => 5, "needstype" => 2, "household_charge" => nil }, - { "period" => 6, "needstype" => 2, "household_charge" => 0 }, - { "period" => 6, "needstype" => 2, "household_charge" => nil }, - { "period" => 7, "needstype" => 2, "household_charge" => 0 }, - { "period" => 7, "needstype" => 2, "household_charge" => nil }, - { "period" => 8, "needstype" => 2, "household_charge" => 0 }, - { "period" => 8, "needstype" => 2, "household_charge" => nil }, - { "period" => 9, "needstype" => 2, "household_charge" => 0 }, - { "period" => 9, "needstype" => 2, "household_charge" => nil }, + { "rent_and_charges_paid_weekly?" => true, "is_supported_housing?" => true, "household_charge" => 0 }, + { "rent_and_charges_paid_weekly?" => true, "is_supported_housing?" => true, "household_charge" => nil }, ] end diff --git a/app/models/form/lettings/pages/outstanding_amount.rb b/app/models/form/lettings/pages/outstanding_amount.rb index d624433ea..22d06c2c8 100644 --- a/app/models/form/lettings/pages/outstanding_amount.rb +++ b/app/models/form/lettings/pages/outstanding_amount.rb @@ -2,7 +2,7 @@ class Form::Lettings::Pages::OutstandingAmount < ::Form::Page def initialize(id, hsh, subsection) super @id = "outstanding_amount" - @depends_on = [{ "hb" => 1, "hbrentshortfall" => 1 }, { "hb" => 6, "hbrentshortfall" => 1 }] + @depends_on = [{ "receives_housing_related_benefits?" => true, "has_housing_benefit_rent_shortfall?" => true }] end def questions diff --git a/app/models/form/lettings/pages/rent_4_weekly.rb b/app/models/form/lettings/pages/rent_4_weekly.rb index fd8518e8e..e71cd7ffa 100644 --- a/app/models/form/lettings/pages/rent_4_weekly.rb +++ b/app/models/form/lettings/pages/rent_4_weekly.rb @@ -4,10 +4,8 @@ class Form::Lettings::Pages::Rent4Weekly < ::Form::Page @id = "rent_4_weekly" @header = "Household rent and charges" @depends_on = [ - { "household_charge" => 0, "period" => 3, "is_carehome" => 0 }, - { "household_charge" => nil, "period" => 3, "is_carehome" => 0 }, - { "household_charge" => 0, "period" => 3, "is_carehome" => nil }, - { "household_charge" => nil, "period" => 3, "is_carehome" => nil }, + { "household_charge" => 0, "rent_and_charges_paid_every_4_weeks?" => true, "is_carehome?" => false }, + { "household_charge" => nil, "rent_and_charges_paid_every_4_weeks?" => true, "is_carehome?" => false }, ] end diff --git a/app/models/form/lettings/pages/rent_bi_weekly.rb b/app/models/form/lettings/pages/rent_bi_weekly.rb index 66ebef76b..0b1b9c884 100644 --- a/app/models/form/lettings/pages/rent_bi_weekly.rb +++ b/app/models/form/lettings/pages/rent_bi_weekly.rb @@ -4,10 +4,8 @@ class Form::Lettings::Pages::RentBiWeekly < ::Form::Page @id = "rent_bi_weekly" @header = "Household rent and charges" @depends_on = [ - { "household_charge" => 0, "period" => 2, "is_carehome" => 0 }, - { "household_charge" => nil, "period" => 2, "is_carehome" => 0 }, - { "household_charge" => 0, "period" => 2, "is_carehome" => nil }, - { "household_charge" => nil, "period" => 2, "is_carehome" => nil }, + { "household_charge" => nil, "rent_and_charges_paid_every_2_weeks?" => true, "is_carehome?" => false }, + { "household_charge" => 0, "rent_and_charges_paid_every_2_weeks?" => true, "is_carehome?" => false }, ] end diff --git a/app/models/form/lettings/pages/rent_monthly.rb b/app/models/form/lettings/pages/rent_monthly.rb index 04664bc93..d6af7a08c 100644 --- a/app/models/form/lettings/pages/rent_monthly.rb +++ b/app/models/form/lettings/pages/rent_monthly.rb @@ -4,10 +4,8 @@ class Form::Lettings::Pages::RentMonthly < ::Form::Page @id = "rent_monthly" @header = "Household rent and charges" @depends_on = [ - { "household_charge" => 0, "period" => 4, "is_carehome" => 0 }, - { "household_charge" => nil, "period" => 4, "is_carehome" => 0 }, - { "household_charge" => 0, "period" => 4, "is_carehome" => nil }, - { "household_charge" => nil, "period" => 4, "is_carehome" => nil }, + { "household_charge" => nil, "rent_and_charges_paid_monthly?" => true, "is_carehome?" => false }, + { "household_charge" => 0, "rent_and_charges_paid_monthly?" => true, "is_carehome?" => false }, ] end diff --git a/app/models/form/lettings/pages/rent_weekly.rb b/app/models/form/lettings/pages/rent_weekly.rb index ab89b31af..4f1709820 100644 --- a/app/models/form/lettings/pages/rent_weekly.rb +++ b/app/models/form/lettings/pages/rent_weekly.rb @@ -4,30 +4,8 @@ class Form::Lettings::Pages::RentWeekly < ::Form::Page @id = "rent_weekly" @header = "Household rent and charges" @depends_on = [ - { "period" => 1, "household_charge" => 0, "is_carehome" => 0 }, - { "period" => 1, "household_charge" => nil, "is_carehome" => 0 }, - { "period" => 5, "household_charge" => 0, "is_carehome" => 0 }, - { "period" => 5, "household_charge" => nil, "is_carehome" => 0 }, - { "period" => 6, "household_charge" => 0, "is_carehome" => 0 }, - { "period" => 6, "household_charge" => nil, "is_carehome" => 0 }, - { "period" => 7, "household_charge" => 0, "is_carehome" => 0 }, - { "period" => 7, "household_charge" => nil, "is_carehome" => 0 }, - { "period" => 8, "household_charge" => 0, "is_carehome" => 0 }, - { "period" => 8, "household_charge" => nil, "is_carehome" => 0 }, - { "period" => 9, "household_charge" => 0, "is_carehome" => 0 }, - { "period" => 9, "household_charge" => nil, "is_carehome" => 0 }, - { "period" => 1, "household_charge" => 0, "is_carehome" => nil }, - { "period" => 1, "household_charge" => nil, "is_carehome" => nil }, - { "period" => 5, "household_charge" => 0, "is_carehome" => nil }, - { "period" => 5, "household_charge" => nil, "is_carehome" => nil }, - { "period" => 6, "household_charge" => 0, "is_carehome" => nil }, - { "period" => 6, "household_charge" => nil, "is_carehome" => nil }, - { "period" => 7, "household_charge" => 0, "is_carehome" => nil }, - { "period" => 7, "household_charge" => nil, "is_carehome" => nil }, - { "period" => 8, "household_charge" => 0, "is_carehome" => nil }, - { "period" => 8, "household_charge" => nil, "is_carehome" => nil }, - { "period" => 9, "household_charge" => 0, "is_carehome" => nil }, - { "period" => 9, "household_charge" => nil, "is_carehome" => nil }, + { "rent_and_charges_paid_weekly?" => true, "household_charge" => 0, "is_carehome?" => false }, + { "rent_and_charges_paid_weekly?" => true, "household_charge" => nil, "is_carehome?" => false }, ] end diff --git a/app/models/form/lettings/questions/period.rb b/app/models/form/lettings/questions/period.rb index 08a8510c4..c4f476c7f 100644 --- a/app/models/form/lettings/questions/period.rb +++ b/app/models/form/lettings/questions/period.rb @@ -15,11 +15,12 @@ class Form::Lettings::Questions::Period < ::Form::Question "2" => { "value" => "Every 2 weeks" }, "3" => { "value" => "Every 4 weeks" }, "4" => { "value" => "Every calendar month" }, - "5" => { "value" => "Weekly for 50 weeks" }, - "6" => { "value" => "Weekly for 49 weeks" }, - "7" => { "value" => "Weekly for 48 weeks" }, - "8" => { "value" => "Weekly for 47 weeks" }, "9" => { "value" => "Weekly for 46 weeks" }, + "8" => { "value" => "Weekly for 47 weeks" }, + "7" => { "value" => "Weekly for 48 weeks" }, + "6" => { "value" => "Weekly for 49 weeks" }, + "5" => { "value" => "Weekly for 50 weeks" }, "1" => { "value" => "Weekly for 52 weeks" }, + "10" => { "value" => "Weekly for 53 weeks" }, }.freeze end diff --git a/app/models/form/lettings/questions/tshortfall.rb b/app/models/form/lettings/questions/tshortfall.rb index af8d32c9c..69697ca44 100644 --- a/app/models/form/lettings/questions/tshortfall.rb +++ b/app/models/form/lettings/questions/tshortfall.rb @@ -20,6 +20,7 @@ class Form::Lettings::Questions::Tshortfall < ::Form::Question { "label" => " every week for 47 weeks", "depends_on" => { "period" => 8 } }, { "label" => " every week for 46 weeks", "depends_on" => { "period" => 9 } }, { "label" => " every week for 52 weeks", "depends_on" => { "period" => 1 } }, + { "label" => " every week for 53 weeks", "depends_on" => { "period" => 10 } }, ] @question_number = 100 end diff --git a/app/models/lettings_log.rb b/app/models/lettings_log.rb index 0ddc921be..cd1fffd22 100644 --- a/app/models/lettings_log.rb +++ b/app/models/lettings_log.rb @@ -58,7 +58,7 @@ class LettingsLog < Log OPTIONAL_FIELDS = %w[first_time_property_let_as_social_housing tenancycode propcode chcharge].freeze RENT_TYPE_MAPPING_LABELS = { 1 => "Social Rent", 2 => "Affordable Rent", 3 => "Intermediate Rent" }.freeze HAS_BENEFITS_OPTIONS = [1, 6, 8, 7].freeze - NUM_OF_WEEKS_FROM_PERIOD = { 2 => 26, 3 => 13, 4 => 12, 5 => 50, 6 => 49, 7 => 48, 8 => 47, 9 => 46, 1 => 52 }.freeze + NUM_OF_WEEKS_FROM_PERIOD = { 2 => 26, 3 => 13, 4 => 12, 5 => 50, 6 => 49, 7 => 48, 8 => 47, 9 => 46, 1 => 52, 10 => 53 }.freeze SUFFIX_FROM_PERIOD = { 2 => "every 2 weeks", 3 => "every 4 weeks", 4 => "every month" }.freeze RETIREMENT_AGES = { "M" => 67, "F" => 60, "X" => 67 }.freeze @@ -203,7 +203,7 @@ class LettingsLog < Log needstype == 2 end - def has_hbrentshortfall? + def has_housing_benefit_rent_shortfall? # 1: Yes hbrentshortfall == 1 end @@ -496,7 +496,27 @@ class LettingsLog < Log end def care_home_charge_expected_not_provided? - is_carehome == 1 && chcharge.blank? + is_carehome? && chcharge.blank? + end + + def rent_and_charges_paid_weekly? + [1, 5, 6, 7, 8, 9, 10].include? period + end + + def rent_and_charges_paid_every_4_weeks? + period == 3 + end + + def rent_and_charges_paid_every_2_weeks? + period == 2 + end + + def rent_and_charges_paid_monthly? + period == 4 + end + + def is_carehome? + is_carehome == 1 end private diff --git a/app/models/validations/financial_validations.rb b/app/models/validations/financial_validations.rb index 82fdf56f3..d328054e9 100644 --- a/app/models/validations/financial_validations.rb +++ b/app/models/validations/financial_validations.rb @@ -4,7 +4,7 @@ module Validations::FinancialValidations # Validations methods need to be called 'validate_' to run on model save # or 'validate_' to run on submit as well def validate_outstanding_rent_amount(record) - if !record.has_hbrentshortfall? && record.tshortfall.present? + if !record.has_housing_benefit_rent_shortfall? && record.tshortfall.present? record.errors.add :tshortfall, :no_outstanding_charges, message: I18n.t("validations.financial.tshortfall.outstanding_amount_not_required") end end @@ -69,7 +69,7 @@ module Validations::FinancialValidations end def validate_tshortfall(record) - if record.has_hbrentshortfall? && no_known_benefits?(record) + if record.has_housing_benefit_rent_shortfall? && no_known_benefits?(record) record.errors.add :tshortfall, I18n.t("validations.financial.hbrentshortfall.outstanding_no_benefits") end end diff --git a/config/forms/2022_2023.json b/config/forms/2022_2023.json index 7c189849d..181d8a19c 100644 --- a/config/forms/2022_2023.json +++ b/config/forms/2022_2023.json @@ -7492,23 +7492,26 @@ "4": { "value": "Every calendar month" }, - "5": { - "value": "Weekly for 50 weeks" + "9": { + "value": "Weekly for 46 weeks" }, - "6": { - "value": "Weekly for 49 weeks" + "8": { + "value": "Weekly for 47 weeks" }, "7": { "value": "Weekly for 48 weeks" }, - "8": { - "value": "Weekly for 47 weeks" + "6": { + "value": "Weekly for 49 weeks" }, - "9": { - "value": "Weekly for 46 weeks" + "5": { + "value": "Weekly for 50 weeks" }, "1": { "value": "Weekly for 52 weeks" + }, + "10": { + "value": "Weekly for 53 weeks" } } } @@ -7558,63 +7561,13 @@ }, "depends_on": [ { - "period": 1, - "needstype": 2, + "rent_and_charges_paid_weekly?": true, + "is_supported_housing?": true, "household_charge": 0 }, { - "period": 1, - "needstype": 2, - "household_charge": null - }, - { - "period": 5, - "needstype": 2, - "household_charge": 0 - }, - { - "period": 5, - "needstype": 2, - "household_charge": null - }, - { - "period": 6, - "needstype": 2, - "household_charge": 0 - }, - { - "period": 6, - "needstype": 2, - "household_charge": null - }, - { - "period": 7, - "needstype": 2, - "household_charge": 0 - }, - { - "period": 7, - "needstype": 2, - "household_charge": null - }, - { - "period": 8, - "needstype": 2, - "household_charge": 0 - }, - { - "period": 8, - "needstype": 2, - "household_charge": null - }, - { - "period": 9, - "needstype": 2, - "household_charge": 0 - }, - { - "period": 9, - "needstype": 2, + "rent_and_charges_paid_weekly?": true, + "is_supported_housing?": true, "household_charge": null } ] @@ -7898,124 +7851,14 @@ }, "depends_on": [ { - "period": 1, - "household_charge": 0, - "is_carehome": 0 - }, - { - "period": 1, - "household_charge": null, - "is_carehome": 0 - }, - { - "period": 5, - "household_charge": 0, - "is_carehome": 0 - }, - { - "period": 5, - "household_charge": null, - "is_carehome": 0 - }, - { - "period": 6, - "household_charge": 0, - "is_carehome": 0 - }, - { - "period": 6, - "household_charge": null, - "is_carehome": 0 - }, - { - "period": 7, - "household_charge": 0, - "is_carehome": 0 - }, - { - "period": 7, - "household_charge": null, - "is_carehome": 0 - }, - { - "period": 8, + "rent_and_charges_paid_weekly?": true, "household_charge": 0, - "is_carehome": 0 + "is_carehome?": false }, { - "period": 8, + "rent_and_charges_paid_weekly?": true, "household_charge": null, - "is_carehome": 0 - }, - { - "period": 9, - "household_charge": 0, - "is_carehome": 0 - }, - { - "period": 9, - "household_charge": null, - "is_carehome": 0 - }, - { - "period": 1, - "household_charge": 0, - "is_carehome": null - }, - { - "period": 1, - "household_charge": null, - "is_carehome": null - }, - { - "period": 5, - "household_charge": 0, - "is_carehome": null - }, - { - "period": 5, - "household_charge": null, - "is_carehome": null - }, - { - "period": 6, - "household_charge": 0, - "is_carehome": null - }, - { - "period": 6, - "household_charge": null, - "is_carehome": null - }, - { - "period": 7, - "household_charge": 0, - "is_carehome": null - }, - { - "period": 7, - "household_charge": null, - "is_carehome": null - }, - { - "period": 8, - "household_charge": 0, - "is_carehome": null - }, - { - "period": 8, - "household_charge": null, - "is_carehome": null - }, - { - "period": 9, - "household_charge": 0, - "is_carehome": null - }, - { - "period": 9, - "household_charge": null, - "is_carehome": null + "is_carehome?": false } ] }, @@ -8602,6 +8445,12 @@ "depends_on": { "period": 1 } + }, + { + "label": " every week for 53 weeks", + "depends_on": { + "period": 10 + } } ] } diff --git a/spec/fixtures/forms/2021_2022.json b/spec/fixtures/forms/2021_2022.json index 9ea11f671..d25b575cc 100644 --- a/spec/fixtures/forms/2021_2022.json +++ b/spec/fixtures/forms/2021_2022.json @@ -879,23 +879,26 @@ "4": { "value": "Every calendar month" }, - "5": { - "value": "Weekly for 50 weeks" + "9": { + "value": "Weekly for 46 weeks" }, - "6": { - "value": "Weekly for 49 weeks" + "8": { + "value": "Weekly for 47 weeks" }, "7": { "value": "Weekly for 48 weeks" }, - "8": { - "value": "Weekly for 47 weeks" + "6": { + "value": "Weekly for 49 weeks" }, - "9": { - "value": "Weekly for 46 weeks" + "5": { + "value": "Weekly for 50 weeks" }, "1": { "value": "Weekly for 52 weeks" + }, + "10": { + "value": "Weekly for 53 weeks" } } }, diff --git a/spec/models/lettings_log_spec.rb b/spec/models/lettings_log_spec.rb index 291af1622..6d45406b2 100644 --- a/spec/models/lettings_log_spec.rb +++ b/spec/models/lettings_log_spec.rb @@ -1079,6 +1079,87 @@ RSpec.describe LettingsLog do expect(record_from_db["wtcharge"]).to eq(187.2) end end + + context "when rent is paid weekly for 53 weeks" do + it "correctly derives and saves weekly rent" do + lettings_log.update!(brent: 130, period: 10) + record_from_db = ActiveRecord::Base.connection.execute("select wrent from lettings_logs where id=#{lettings_log.id}").to_a[0] + expect(lettings_log.wrent).to eq(132.5) + expect(record_from_db["wrent"]).to eq(132.5) + end + + it "correctly derives and saves weekly service charge" do + lettings_log.update!(scharge: 30, period: 10) + record_from_db = ActiveRecord::Base.connection.execute("select wscharge from lettings_logs where id=#{lettings_log.id}").to_a[0] + expect(lettings_log.wscharge).to eq(30.58) + expect(record_from_db["wscharge"]).to eq(30.58) + end + + it "correctly derives and saves weekly personal service charge" do + lettings_log.update!(pscharge: 30, period: 10) + record_from_db = ActiveRecord::Base.connection.execute("select wpschrge from lettings_logs where id=#{lettings_log.id}").to_a[0] + expect(lettings_log.wpschrge).to eq(30.58) + expect(record_from_db["wpschrge"]).to eq(30.58) + end + + it "correctly derives and saves weekly support charge" do + lettings_log.update!(supcharg: 30, period: 10) + record_from_db = ActiveRecord::Base.connection.execute("select wsupchrg from lettings_logs where id=#{lettings_log.id}").to_a[0] + expect(lettings_log.wsupchrg).to eq(30.58) + expect(record_from_db["wsupchrg"]).to eq(30.58) + end + + it "correctly derives and saves weekly total charge" do + lettings_log.update!(tcharge: 30, period: 10) + record_from_db = ActiveRecord::Base.connection.execute("select wtcharge from lettings_logs where id=#{lettings_log.id}").to_a[0] + expect(lettings_log.wtcharge).to eq(30.58) + expect(record_from_db["wtcharge"]).to eq(30.58) + 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 + lettings_log.update!(hbrentshortfall: 1, tshortfall: 130, period: 10, hb: 1) + record_from_db = ActiveRecord::Base.connection.execute("select wtshortfall from lettings_logs where id=#{lettings_log.id}").to_a[0] + expect(lettings_log.wtshortfall).to eq(132.5) + expect(record_from_db["wtshortfall"]).to eq(132.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 + lettings_log.update!(hbrentshortfall: 1, tshortfall: 130, period: 10, hb: 6) + record_from_db = ActiveRecord::Base.connection.execute("select wtshortfall from lettings_logs where id=#{lettings_log.id}").to_a[0] + expect(lettings_log.wtshortfall).to eq(132.5) + expect(record_from_db["wtshortfall"]).to eq(132.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 + lettings_log.update!(hbrentshortfall: 1, tshortfall: 130, period: 10, hb: 8) + record_from_db = ActiveRecord::Base.connection.execute("select wtshortfall from lettings_logs where id=#{lettings_log.id}").to_a[0] + expect(lettings_log.wtshortfall).to eq(132.5) + expect(record_from_db["wtshortfall"]).to eq(132.5) + end + end + end + + it "correctly derives floats" do + lettings_log.update!(supcharg: 30.12, pscharge: 25.13, scharge: 30.98, brent: 100.97, period: 10) + record_from_db = ActiveRecord::Base.connection.execute("select wtcharge, wsupchrg, wpschrge, wscharge, wrent from lettings_logs where id=#{lettings_log.id}").to_a[0] + expect(lettings_log.wsupchrg).to eq(30.7) + expect(lettings_log.wpschrge).to eq(25.61) + expect(lettings_log.wscharge).to eq(31.58) + expect(lettings_log.wrent).to eq(102.91) + expect(lettings_log.wtcharge).to eq(190.8) + expect(record_from_db["wsupchrg"]).to eq(30.7) + expect(record_from_db["wpschrge"]).to eq(25.61) + expect(record_from_db["wscharge"]).to eq(31.58) + expect(record_from_db["wrent"]).to eq(102.91) + expect(record_from_db["wtcharge"]).to eq(190.8) + end + end end context "when the owning organisation is a LA" do