From 60c64d60f5ef490487e13f51623da9969c2463ff Mon Sep 17 00:00:00 2001 From: Arthur Campbell Date: Wed, 22 May 2024 13:49:14 +0100 Subject: [PATCH] extract a good chunk of tests out of lettings_log_spec and into a dedicated derived fields spec file. in many cases refactor tests --- .../lettings_log_derived_fields_spec.rb | 1178 ++++++++++ spec/models/lettings_log_spec.rb | 1924 ++--------------- 2 files changed, 1338 insertions(+), 1764 deletions(-) create mode 100644 spec/models/lettings_log_derived_fields_spec.rb diff --git a/spec/models/lettings_log_derived_fields_spec.rb b/spec/models/lettings_log_derived_fields_spec.rb new file mode 100644 index 000000000..561c0024b --- /dev/null +++ b/spec/models/lettings_log_derived_fields_spec.rb @@ -0,0 +1,1178 @@ +require "rails_helper" +require "shared/shared_examples_for_derived_fields" + +# rubocop:disable RSpec/BeforeAfterAll +# rubocop:disable RSpec/InstanceVariable +RSpec.describe LettingsLog, type: :model do + before(:context) do + owning_organisation = build(:organisation) + @log = build(:lettings_log, :startdate_today, owning_organisation:, managing_organisation: owning_organisation) + end + + after(:context) do + @log.destroy + end + + include_examples "shared examples for derived fields", :lettings_log + + it "correctly derives incref from net_income_known" do + @log.net_income_known = 0 + expect { @log.set_derived_fields! }.to change(@log, :incref).to 0 + + @log.net_income_known = 1 + expect { @log.set_derived_fields! }.to change(@log, :incref).to 2 + + @log.net_income_known = 2 + expect { @log.set_derived_fields! }.to change(@log, :incref).to 1 + end + + it "derives shortfall_known when tshortfall is set" do + @log.tshortfall = 10 + + expect { @log.set_derived_fields! }.to change(@log, :tshortfall_known).to 0 + end + + describe "deriving has_benefits" do + it "correctly derives when the household receives benefits" do + benefits_codes = [1, 6, 8, 7] + @log.hb = benefits_codes.sample + @log.set_derived_fields! + + expect(@log.has_benefits).to be 1 + end + + it "correctly derives when the household does not receive benefits" do + no_benefits_codes = [9, 3, 10, 1, 4] + @log.hb = no_benefits_codes.sample + @log.set_derived_fields! + + expect(@log.has_benefits).to be 0 + end + end + + describe "deriving vacant days" do + it "correctly derives vacdays from startdate and mrcdate" do + day_count = 8 + @log.startdate = Time.zone.today + @log.mrcdate = Time.zone.today - day_count.days + + @log.set_derived_fields! + + expect(@log.vacdays).to be day_count + end + + it "correctly derives vacdays from startdate and voiddate if mrcdate is nil" do + day_count = 3 + @log.startdate = Time.zone.today + @log.voiddate = Time.zone.today - day_count.days + @log.mrcdate = nil + + @log.set_derived_fields! + + expect(@log.vacdays).to be day_count + end + + it "does not derive vacdays if voiddate and mrcdate are blank" do + @log.startdate = Time.zone.today + @log.voiddate = nil + @log.mrcdate = nil + + @log.set_derived_fields! + + expect(@log.vacdays).to be nil + end + + it "does not derive vacdays if startdate is blank" do + @log.startdate = nil + @log.voiddate = Time.zone.today + @log.mrcdate = Time.zone.today + + @log.set_derived_fields! + + expect(@log.vacdays).to be nil + end + end + + describe "deriving household member fields" do + before(:context) do + @log.relat2 = "X" + @log.relat3 = "C" + @log.relat4 = "X" + @log.relat5 = "C" + @log.relat7 = "C" + @log.relat8 = "X" + @log.age1 = 22 + @log.age2 = 16 + @log.age4 = 60 + @log.age6 = 88 + @log.age7 = 14 + @log.age8 = 42 + + @log.set_derived_fields! + end + + it "correctly derives totchild" do + expect(@log.totchild).to eq 3 + end + + it "correctly derives totelder" do + expect(@log.totelder).to eq 2 + end + + it "correctly derives totadult" do + expect(@log.totadult).to eq 3 + end + + it "correctly derives economic status for tenants under 16" do + expect(@log.ecstat7).to eq 9 + end + end + + describe "deriving lettype" do + context "when the owning organisation is a PRP" do + before(:context) do + @log.owning_organisation.provider_type = "PRP" + end + + [ + { + context: "when the rent type is intermediate rent and supported housing", + rent_type: 4, + needstype: 2, + expected_lettype: 10, + }, + { + context: "when the rent type is intermediate rent and general needs housing", + rent_type: 4, + needstype: 1, + expected_lettype: 9, + }, + { + context: "when the rent type is affordable rent and supported housing", + rent_type: 2, + needstype: 2, + expected_lettype: 6, + }, + { + context: "when the rent type is affordable rent and general needs housing", + rent_type: 2, + needstype: 1, + expected_lettype: 5, + }, + { + context: "when the rent type is social rent and supported housing", + rent_type: 0, + needstype: 2, + expected_lettype: 2, + }, + { + context: "when the rent type is social rent and general needs housing", + rent_type: 0, + needstype: 1, + expected_lettype: 1, + }, + ].each do |test_case| + context test_case[:context] do + it "correctly derives lettype" do + @log.rent_type = test_case[:rent_type] + @log.needstype = test_case[:needstype] + expect { @log.set_derived_fields! }.to change(@log, :lettype).to test_case[:expected_lettype] + end + end + end + end + + context "when the owning organisation is an LA" do + before(:context) do + @log.owning_organisation.provider_type = "LA" + end + + [ + { + context: "when the rent type is intermediate rent and supported housing", + rent_type: 4, + needstype: 2, + expected_lettype: 12, + }, + { + context: "when the rent type is intermediate rent and general needs housing", + rent_type: 4, + needstype: 1, + expected_lettype: 11, + }, + { + context: "when the rent type is affordable rent and supported housing", + rent_type: 2, + needstype: 2, + expected_lettype: 8, + }, + { + context: "when the rent type is affordable rent and general needs housing", + rent_type: 2, + needstype: 1, + expected_lettype: 7, + }, + { + context: "when the rent type is social rent and supported housing", + rent_type: 0, + needstype: 2, + expected_lettype: 4, + }, + { + context: "when the rent type is social rent and general needs housing", + rent_type: 0, + needstype: 1, + expected_lettype: 3, + }, + ].each do |test_case| + context test_case[:context] do + it "correctly derives lettype" do + @log.rent_type = test_case[:rent_type] + @log.needstype = test_case[:needstype] + expect { @log.set_derived_fields! }.to change(@log, :lettype).to test_case[:expected_lettype] + end + end + end + end + end + + describe "deriving newprop" do + it "updates newprop correctly when this is the first time the property has been let" do + first_time_let_codes = [15, 16, 17] + @log.rsnvac = first_time_let_codes.sample + + @log.set_derived_fields! + + expect(@log.newprop).to eq 1 + end + + it "updates newprop correctly when this is not the first time the property has been let" do + not_first_time_let_codes = [14, 9, 13, 12, 8, 18, 20, 5, 19, 6, 10, 11, 21, 22] + @log.rsnvac = not_first_time_let_codes.sample + + @log.set_derived_fields! + + expect(@log.newprop).to eq 2 + end + end + + describe "deriving charges based on rent period" do + context "when rent is paid bi-weekly" do + before(:context) do + @log.period = 2 + end + + [ + { + test_title: "correctly derives weekly rent", + fields_to_set: { brent: 100 }, + expected_values: { wrent: 50.0 }, + }, + { + test_title: "correctly derives weekly service charge", + fields_to_set: { scharge: 70 }, + expected_values: { wscharge: 35.0 }, + }, + { + test_title: "correctly derives weekly personal service charge", + fields_to_set: { pscharge: 60 }, + expected_values: { wpschrge: 30.0 }, + }, + { + test_title: "correctly derives weekly support charge", + fields_to_set: { supcharg: 80 }, + expected_values: { wsupchrg: 40.0 }, + }, + { + test_title: "correctly derives weekly total shortfall", + fields_to_set: { hbrentshortfall: 1, tshortfall: 100, hb: 6 }, + expected_values: { wtshortfall: 50.0 }, + }, + { + test_title: "correctly clears weekly total shortfall if the tenant does not receive applicable benefits", + fields_to_set: { hbrentshortfall: 1, tshortfall: 100, hb: 9 }, + expected_values: { wtshortfall: nil }, + }, + { + test_title: "correctly derives floats and weekly total charge", + fields_to_set: { supcharg: 60.12, pscharge: 50.13, scharge: 60.98, brent: 60.97 }, + expected_values: { wsupchrg: 30.06, wpschrge: 25.06, wscharge: 30.49, wrent: 30.49, wtcharge: 116.1 }, + }, + { + test_title: "correctly derives weekly care home charge when the letting is supported housing", + fields_to_set: { needstype: 2, chcharge: 57.86 }, + expected_values: { wchchrg: 28.93 }, + }, + ].each do |test_case| + it test_case[:test_title] do + test_case[:fields_to_set].each { |field, value| @log[field] = value } + @log.set_derived_fields! + test_case[:expected_values].each do |field, expected_value| + expect(@log[field]).to eq expected_value + end + end + end + end + + context "when rent is paid every 4 weeks" do + before(:context) do + @log.period = 3 + end + + [ + { + test_title: "correctly derives weekly rent", + fields_to_set: { brent: 120 }, + expected_values: { wrent: 30.0 }, + }, + { + test_title: "correctly derives weekly service charge", + fields_to_set: { scharge: 120 }, + expected_values: { wscharge: 30.0 }, + }, + { + test_title: "correctly derives weekly personal service charge", + fields_to_set: { pscharge: 120 }, + expected_values: { wpschrge: 30.0 }, + }, + { + test_title: "correctly derives weekly support charge", + fields_to_set: { supcharg: 120 }, + expected_values: { wsupchrg: 30.0 }, + }, + { + test_title: "correctly derives weekly total shortfall", + fields_to_set: { hbrentshortfall: 1, tshortfall: 120, hb: 6 }, + expected_values: { wtshortfall: 30.0 }, + }, + { + test_title: "correctly clears weekly total shortfall if the tenant does not receive applicable benefits", + fields_to_set: { hbrentshortfall: 1, tshortfall: 100, hb: 9 }, + expected_values: { wtshortfall: nil }, + }, + { + test_title: "correctly derives floats and weekly total charge", + fields_to_set: { supcharg: 100.12, pscharge: 100.13, scharge: 100.98, brent: 100.97 }, + expected_values: { wsupchrg: 25.03, wpschrge: 25.03, wscharge: 25.24, wrent: 25.24, wtcharge: 100.55 }, + }, + { + test_title: "correctly derives weekly care home charge when the letting is supported housing", + fields_to_set: { needstype: 2, chcharge: 57.86 }, + expected_values: { wchchrg: 14.46 }, + }, + ].each do |test_case| + it test_case[:test_title] do + test_case[:fields_to_set].each { |field, value| @log[field] = value } + @log.set_derived_fields! + test_case[:expected_values].each do |field, expected_value| + expect(@log[field]).to eq expected_value + end + end + end + end + + context "when rent is paid every calendar month" do + before(:context) do + @log.period = 4 + end + + [ + { + test_title: "correctly derives weekly rent", + fields_to_set: { brent: 130 }, + expected_values: { wrent: 30.0 }, + }, + { + test_title: "correctly derives weekly service charge", + fields_to_set: { scharge: 130 }, + expected_values: { wscharge: 30.0 }, + }, + { + test_title: "correctly derives weekly personal service charge", + fields_to_set: { pscharge: 130 }, + expected_values: { wpschrge: 30.0 }, + }, + { + test_title: "correctly derives weekly support charge", + fields_to_set: { supcharg: 130 }, + expected_values: { wsupchrg: 30.0 }, + }, + { + test_title: "correctly derives weekly total shortfall", + fields_to_set: { hbrentshortfall: 1, tshortfall: 130, hb: 6 }, + expected_values: { wtshortfall: 30.0 }, + }, + { + test_title: "correctly clears weekly total shortfall if the tenant does not receive applicable benefits", + fields_to_set: { hbrentshortfall: 1, tshortfall: 100, hb: 9 }, + expected_values: { wtshortfall: nil }, + }, + { + test_title: "correctly derives floats and weekly total charge", + fields_to_set: { supcharg: 100.12, pscharge: 100.13, scharge: 100.98, brent: 100.97 }, + expected_values: { wsupchrg: 23.10, wpschrge: 23.11, wscharge: 23.30, wrent: 23.30, wtcharge: 92.82 }, + }, + { + test_title: "correctly derives weekly care home charge when the letting is supported housing", + fields_to_set: { needstype: 2, chcharge: 57.86 }, + expected_values: { wchchrg: 13.35 }, + }, + ].each do |test_case| + it test_case[:test_title] do + test_case[:fields_to_set].each { |field, value| @log[field] = value } + @log.set_derived_fields! + test_case[:expected_values].each do |field, expected_value| + expect(@log[field]).to eq expected_value + end + end + end + end + + context "when rent is paid weekly for 50 weeks" do + before(:context) do + @log.period = 5 + end + + [ + { + test_title: "correctly derives weekly rent", + fields_to_set: { brent: 130 }, + expected_values: { wrent: 125.0 }, + }, + { + test_title: "correctly derives weekly service charge", + fields_to_set: { scharge: 130 }, + expected_values: { wscharge: 125.0 }, + }, + { + test_title: "correctly derives weekly personal service charge", + fields_to_set: { pscharge: 130 }, + expected_values: { wpschrge: 125.0 }, + }, + { + test_title: "correctly derives weekly support charge", + fields_to_set: { supcharg: 130 }, + expected_values: { wsupchrg: 125.0 }, + }, + { + test_title: "correctly derives weekly total shortfall", + fields_to_set: { hbrentshortfall: 1, tshortfall: 130, hb: 6 }, + expected_values: { wtshortfall: 125.0 }, + }, + { + test_title: "correctly clears weekly total shortfall if the tenant does not receive applicable benefits", + fields_to_set: { hbrentshortfall: 1, tshortfall: 100, hb: 9 }, + expected_values: { wtshortfall: nil }, + }, + { + test_title: "correctly derives floats and weekly total charge", + fields_to_set: { supcharg: 20.12, pscharge: 20.13, scharge: 20.98, brent: 100.97 }, + expected_values: { wsupchrg: 19.35, wpschrge: 19.36, wscharge: 20.17, wrent: 97.09, wtcharge: 155.96 }, + }, + { + test_title: "correctly derives weekly care home charge when the letting is supported housing", + fields_to_set: { needstype: 2, chcharge: 57.86 }, + expected_values: { wchchrg: 55.63 }, + }, + ].each do |test_case| + it test_case[:test_title] do + test_case[:fields_to_set].each { |field, value| @log[field] = value } + @log.set_derived_fields! + test_case[:expected_values].each do |field, expected_value| + expect(@log[field]).to eq expected_value + end + end + end + end + + context "when rent is paid weekly for 49 weeks" do + before(:context) do + @log.period = 6 + end + + [ + { + test_title: "correctly derives weekly rent", + fields_to_set: { brent: 130 }, + expected_values: { wrent: 122.5 }, + }, + { + test_title: "correctly derives weekly service charge", + fields_to_set: { scharge: 130 }, + expected_values: { wscharge: 122.5 }, + }, + { + test_title: "correctly derives weekly personal service charge", + fields_to_set: { pscharge: 130 }, + expected_values: { wpschrge: 122.5 }, + }, + { + test_title: "correctly derives weekly support charge", + fields_to_set: { supcharg: 130 }, + expected_values: { wsupchrg: 122.5 }, + }, + { + test_title: "correctly derives weekly total shortfall", + fields_to_set: { hbrentshortfall: 1, tshortfall: 130, hb: 6 }, + expected_values: { wtshortfall: 122.5 }, + }, + { + test_title: "correctly clears weekly total shortfall if the tenant does not receive applicable benefits", + fields_to_set: { hbrentshortfall: 1, tshortfall: 100, hb: 9 }, + expected_values: { wtshortfall: nil }, + }, + { + test_title: "correctly derives floats and weekly total charge", + fields_to_set: { supcharg: 30.12, pscharge: 30.13, scharge: 30.98, brent: 100.97 }, + expected_values: { wsupchrg: 28.38, wpschrge: 28.39, wscharge: 29.19, wrent: 95.14, wtcharge: 181.11 }, + }, + { + test_title: "correctly derives weekly care home charge when the letting is supported housing", + fields_to_set: { needstype: 2, chcharge: 57.86 }, + expected_values: { wchchrg: 54.52 }, + }, + ].each do |test_case| + it test_case[:test_title] do + test_case[:fields_to_set].each { |field, value| @log[field] = value } + @log.set_derived_fields! + test_case[:expected_values].each do |field, expected_value| + expect(@log[field]).to eq expected_value + end + end + end + end + + context "when rent is paid weekly for 48 weeks" do + before(:context) do + @log.period = 7 + end + + [ + { + test_title: "correctly derives weekly rent", + fields_to_set: { brent: 130 }, + expected_values: { wrent: 120 }, + }, + { + test_title: "correctly derives weekly service charge", + fields_to_set: { scharge: 130 }, + expected_values: { wscharge: 120 }, + }, + { + test_title: "correctly derives weekly personal service charge", + fields_to_set: { pscharge: 130 }, + expected_values: { wpschrge: 120 }, + }, + { + test_title: "correctly derives weekly support charge", + fields_to_set: { supcharg: 130 }, + expected_values: { wsupchrg: 120 }, + }, + { + test_title: "correctly derives weekly total shortfall", + fields_to_set: { hbrentshortfall: 1, tshortfall: 130, hb: 6 }, + expected_values: { wtshortfall: 120 }, + }, + { + test_title: "correctly clears weekly total shortfall if the tenant does not receive applicable benefits", + fields_to_set: { hbrentshortfall: 1, tshortfall: 100, hb: 9 }, + expected_values: { wtshortfall: nil }, + }, + { + test_title: "correctly derives floats and weekly total charge", + fields_to_set: { supcharg: 30.12, pscharge: 30.13, scharge: 30.98, brent: 100.97 }, + expected_values: { wsupchrg: 27.8, wpschrge: 27.81, wscharge: 28.6, wrent: 93.20, wtcharge: 177.42 }, + }, + { + test_title: "correctly derives weekly care home charge when the letting is supported housing", + fields_to_set: { needstype: 2, chcharge: 57.86 }, + expected_values: { wchchrg: 53.41 }, + }, + ].each do |test_case| + it test_case[:test_title] do + test_case[:fields_to_set].each { |field, value| @log[field] = value } + @log.set_derived_fields! + test_case[:expected_values].each do |field, expected_value| + expect(@log[field]).to eq expected_value + end + end + end + end + + context "when rent is paid weekly for 47 weeks" do + before(:context) do + @log.period = 8 + end + + [ + { + test_title: "correctly derives weekly rent", + fields_to_set: { brent: 130 }, + expected_values: { wrent: 117.5 }, + }, + { + test_title: "correctly derives weekly service charge", + fields_to_set: { scharge: 130 }, + expected_values: { wscharge: 117.5 }, + }, + { + test_title: "correctly derives weekly personal service charge", + fields_to_set: { pscharge: 130 }, + expected_values: { wpschrge: 117.5 }, + }, + { + test_title: "correctly derives weekly support charge", + fields_to_set: { supcharg: 130 }, + expected_values: { wsupchrg: 117.5 }, + }, + { + test_title: "correctly derives weekly total shortfall", + fields_to_set: { hbrentshortfall: 1, tshortfall: 130, hb: 6 }, + expected_values: { wtshortfall: 117.5 }, + }, + { + test_title: "correctly clears weekly total shortfall if the tenant does not receive applicable benefits", + fields_to_set: { hbrentshortfall: 1, tshortfall: 100, hb: 9 }, + expected_values: { wtshortfall: nil }, + }, + { + test_title: "correctly derives floats and weekly total charge", + fields_to_set: { supcharg: 30.12, pscharge: 30.13, scharge: 30.98, brent: 100.97 }, + expected_values: { wsupchrg: 27.22, wpschrge: 27.23, wscharge: 28, wrent: 91.26, wtcharge: 173.72 }, + }, + { + test_title: "correctly derives weekly care home charge when the letting is supported housing", + fields_to_set: { needstype: 2, chcharge: 57.86 }, + expected_values: { wchchrg: 52.3 }, + }, + ].each do |test_case| + it test_case[:test_title] do + test_case[:fields_to_set].each { |field, value| @log[field] = value } + @log.set_derived_fields! + test_case[:expected_values].each do |field, expected_value| + expect(@log[field]).to eq expected_value + end + end + end + end + + context "when rent is paid weekly for 46 weeks" do + before(:context) do + @log.period = 9 + end + + [ + { + test_title: "correctly derives weekly rent", + fields_to_set: { brent: 130 }, + expected_values: { wrent: 115 }, + }, + { + test_title: "correctly derives weekly service charge", + fields_to_set: { scharge: 130 }, + expected_values: { wscharge: 115 }, + }, + { + test_title: "correctly derives weekly personal service charge", + fields_to_set: { pscharge: 130 }, + expected_values: { wpschrge: 115 }, + }, + { + test_title: "correctly derives weekly support charge", + fields_to_set: { supcharg: 130 }, + expected_values: { wsupchrg: 115 }, + }, + { + test_title: "correctly derives weekly total shortfall", + fields_to_set: { hbrentshortfall: 1, tshortfall: 130, hb: 6 }, + expected_values: { wtshortfall: 115 }, + }, + { + test_title: "correctly clears weekly total shortfall if the tenant does not receive applicable benefits", + fields_to_set: { hbrentshortfall: 1, tshortfall: 100, hb: 9 }, + expected_values: { wtshortfall: nil }, + }, + { + test_title: "correctly derives floats and weekly total charge", + fields_to_set: { supcharg: 30.12, pscharge: 30.13, scharge: 30.98, brent: 100.97 }, + expected_values: { wsupchrg: 26.64, wpschrge: 26.65, wscharge: 27.41, wrent: 89.32, wtcharge: 170.02 }, + }, + { + test_title: "correctly derives weekly care home charge when the letting is supported housing", + fields_to_set: { needstype: 2, chcharge: 57.86 }, + expected_values: { wchchrg: 51.18 }, + }, + ].each do |test_case| + it test_case[:test_title] do + test_case[:fields_to_set].each { |field, value| @log[field] = value } + @log.set_derived_fields! + test_case[:expected_values].each do |field, expected_value| + expect(@log[field]).to eq expected_value + end + end + end + end + + context "when rent is paid weekly for 52 weeks" do + before(:context) do + @log.period = 1 + end + + [ + { + test_title: "correctly derives weekly rent", + fields_to_set: { brent: 130 }, + expected_values: { wrent: 130 }, + }, + { + test_title: "correctly derives weekly service charge", + fields_to_set: { scharge: 130 }, + expected_values: { wscharge: 130 }, + }, + { + test_title: "correctly derives weekly personal service charge", + fields_to_set: { pscharge: 130 }, + expected_values: { wpschrge: 130 }, + }, + { + test_title: "correctly derives weekly support charge", + fields_to_set: { supcharg: 130 }, + expected_values: { wsupchrg: 130 }, + }, + { + test_title: "correctly derives weekly total shortfall", + fields_to_set: { hbrentshortfall: 1, tshortfall: 130, hb: 6 }, + expected_values: { wtshortfall: 130 }, + }, + { + test_title: "correctly clears weekly total shortfall if the tenant does not receive applicable benefits", + fields_to_set: { hbrentshortfall: 1, tshortfall: 100, hb: 9 }, + expected_values: { wtshortfall: nil }, + }, + { + test_title: "correctly derives floats and weekly total charge", + fields_to_set: { supcharg: 30.12, pscharge: 25.13, scharge: 30.98, brent: 100.97 }, + expected_values: { wsupchrg: 30.12, wpschrge: 25.13, wscharge: 30.98, wrent: 100.97, wtcharge: 187.2 }, + }, + { + test_title: "correctly derives weekly care home charge when the letting is supported housing", + fields_to_set: { needstype: 2, chcharge: 57.86 }, + expected_values: { wchchrg: 57.86 }, + }, + ].each do |test_case| + it test_case[:test_title] do + test_case[:fields_to_set].each { |field, value| @log[field] = value } + @log.set_derived_fields! + test_case[:expected_values].each do |field, expected_value| + expect(@log[field]).to eq expected_value + end + end + end + end + + context "when rent is paid weekly for 53 weeks" do + before(:context) do + @log.period = 10 + end + + [ + { + test_title: "correctly derives weekly rent", + fields_to_set: { brent: 130 }, + expected_values: { wrent: 132.5 }, + }, + { + test_title: "correctly derives weekly service charge", + fields_to_set: { scharge: 130 }, + expected_values: { wscharge: 132.5 }, + }, + { + test_title: "correctly derives weekly personal service charge", + fields_to_set: { pscharge: 130 }, + expected_values: { wpschrge: 132.5 }, + }, + { + test_title: "correctly derives weekly support charge", + fields_to_set: { supcharg: 130 }, + expected_values: { wsupchrg: 132.5 }, + }, + { + test_title: "correctly derives weekly total shortfall", + fields_to_set: { hbrentshortfall: 1, tshortfall: 130, hb: 6 }, + expected_values: { wtshortfall: 132.5 }, + }, + { + test_title: "correctly clears weekly total shortfall if the tenant does not receive applicable benefits", + fields_to_set: { hbrentshortfall: 1, tshortfall: 100, hb: 9 }, + expected_values: { wtshortfall: nil }, + }, + { + test_title: "correctly derives floats and weekly total charge", + fields_to_set: { supcharg: 30.12, pscharge: 25.13, scharge: 30.98, brent: 100.97 }, + expected_values: { wsupchrg: 30.7, wpschrge: 25.61, wscharge: 31.58, wrent: 102.91, wtcharge: 190.8 }, + }, + { + test_title: "correctly derives weekly care home charge when the letting is supported housing", + fields_to_set: { needstype: 2, chcharge: 57.86 }, + expected_values: { wchchrg: 58.97 }, + }, + ].each do |test_case| + it test_case[:test_title] do + test_case[:fields_to_set].each { |field, value| @log[field] = value } + @log.set_derived_fields! + test_case[:expected_values].each do |field, expected_value| + expect(@log[field]).to eq expected_value + end + end + end + end + end + + describe "deriving charges" do + describe "deriving the total charge" do + it "sums all the charges" do + brent_value = 5.77 + scharge_value = 10.01 + pscharge_value = 3 + supcharg_value = 12.2 + @log.brent = brent_value + @log.scharge = scharge_value + @log.pscharge = pscharge_value + @log.supcharg = supcharg_value + + @log.set_derived_fields! + + expect(@log.tcharge).to eq(brent_value + scharge_value + pscharge_value + supcharg_value) + end + + it "takes nil values to be zero" do + brent_value = 5.77 + scharge_value = nil + pscharge_value = nil + supcharg_value = 12.2 + @log.brent = brent_value + @log.scharge = scharge_value + @log.pscharge = pscharge_value + @log.supcharg = supcharg_value + + @log.set_derived_fields! + + expect(@log.tcharge).to eq(brent_value + supcharg_value) + end + end + + it "when any charge field is set all blank charge fields are set to 0, non-blank fields are left the same" do + %i[brent scharge pscharge supcharg].each { |field| @log[field] = nil } + + @log.set_derived_fields! + %i[brent scharge pscharge supcharg].each do |field| + expect(@log[field]).to be nil + end + + brent_val = 111 + @log.brent = brent_val + @log.set_derived_fields! + %i[scharge pscharge supcharg].each do |field| + expect(@log[field]).to eq 0 + end + + @log.scharge = 22 + @log.set_derived_fields! + expect(@log.brent).to eq brent_val + end + end + + describe "deriving refused" do + it "derives refused when any age field is refused or details field is unknown" do + age_and_details_fields = %i[age1_known age2_known age3_known age4_known age5_known age6_known age7_known age8_known details_known_2 details_known_3 details_known_4 details_known_5 details_known_6 details_known_7 details_known_8] + + @log[age_and_details_fields.sample] = 1 + @log.set_derived_fields! + + expect(@log.refused).to eq 1 + end + + it "derives refused when any sex or relationship field is refused" do + age_fields = %i[sex1 sex2 sex3 sex4 sex5 sex6 sex7 sex8 relat2 relat3 relat4 relat5 relat6 relat7 relat8] + + @log[age_fields.sample] = "R" + @log.set_derived_fields! + + expect(@log.refused).to eq 1 + end + + it "derives refused when any economic status field is refused" do + economic_status_fields = %i[ecstat1 ecstat2 ecstat3 ecstat4 ecstat5 ecstat6 ecstat7 ecstat8] + + @log[economic_status_fields.sample] = 10 + @log.set_derived_fields! + + expect(@log.refused).to eq 1 + end + end + + describe "deriving renttype from rent_type" do + before do + @log.renttype = nil + end + + it "when rent_type is Social Rent derives renttype as Social Rent" do + @log.rent_type = 0 + expect { @log.set_derived_fields! }.to change(@log, :renttype).to 1 + end + + it "when rent_type is Affordable Rent derives renttype as Affordable Rent" do + @log.rent_type = 1 + expect { @log.set_derived_fields! }.to change(@log, :renttype).to 2 + end + + it "when rent_type is London Affordable Rent derives renttype as Affordable Rent" do + @log.rent_type = 2 + expect { @log.set_derived_fields! }.to change(@log, :renttype).to 2 + end + + it "when rent_type is Rent to Buy derives renttype as Intermediate Rent" do + @log.rent_type = 3 + expect { @log.set_derived_fields! }.to change(@log, :renttype).to 3 + end + + it "when rent_type is London Living Rent derives renttype as Intermediate Rent" do + @log.rent_type = 4 + expect { @log.set_derived_fields! }.to change(@log, :renttype).to 3 + end + + it "when rent_type is Other intermediate rent product derives renttype as Intermediate Rent" do + @log.rent_type = 5 + expect { @log.set_derived_fields! }.to change(@log, :renttype).to 3 + end + end + + describe "variables dependent on whether a letting is a renewal" do + let(:lettings_log) { create(:lettings_log, :setup_completed) } + + [ + { + test_title: "correctly derives the length of time on local authority waiting list", + field: :waityear, + value: 2, + }, + { + test_title: "correctly derives the number of times previously offered since becoming available", + field: :offered, + value: 0, + }, + { + test_title: "correctly derives referral if the letting is a renewal and clears it if it is not", + field: :referral, + value: 1, + }, + { + test_title: "correctly derives first_time_property_let_as_social_housing and clears it if it is not", + field: :first_time_property_let_as_social_housing, + value: 0, + }, + { + test_title: "correctly derives vacancy reason and clears it if it is not", + field: :rsnvac, + value: 14, + }, + ].each do |test_case| + it test_case[:test_title] do + expect { lettings_log.update!(renewal: 1) }.to change(lettings_log, test_case[:field]).to test_case[:value] + expect { lettings_log.update!(renewal: 0) }.to change(lettings_log, test_case[:field]).from(test_case[:value]).to nil + end + end + + it "correctly derives voiddate if the letting is a renewal and clears it if it is not" do + startdate = Time.zone.now + lettings_log.update!(startdate:) + expect { lettings_log.update!(renewal: 1) }.to change(lettings_log, :voiddate).to startdate + expect { lettings_log.update!(renewal: 0) }.to change(lettings_log, :voiddate).from(startdate).to nil + end + + it "derives values for local authority and previous location if postcode is set and log is a renewal" do + postcode = "SW1A 1AA" + expected_la = "E09000033" + expect { lettings_log.update!(renewal: 1, postcode_full: postcode, postcode_known: 1) } + .to change(lettings_log, :la).to(expected_la) + .and change(lettings_log, :ppostcode_full).to(postcode) + .and change(lettings_log, :ppcodenk).to(0) + .and change(lettings_log, :prevloc).to(expected_la) + end + + context "when the log is general needs" do + context "and the managing organisation is a private registered provider" do + before do + lettings_log.managing_organisation.update!(provider_type: "PRP") + lettings_log.update!(needstype: 1, renewal: 1) + end + + it "correctly derives prevten" do + expect(lettings_log.prevten).to be 32 + end + + it "clears prevten if the log is marked as supported housing" do + lettings_log.update!(needstype: 2) + expect(lettings_log.prevten).to be nil + end + + it "clears prevten if renewal is update to no" do + lettings_log.update!(renewal: 0) + expect(lettings_log.prevten).to be nil + end + end + + context "and the managing organisation is a local authority" do + before do + lettings_log.managing_organisation.update!(provider_type: "LA") + lettings_log.update!(needstype: 1, renewal: 1) + end + + it "correctly derives prevten" do + expect(lettings_log.prevten).to be 30 + end + + it "clears prevten if the log is marked as supported housing" do + expect { lettings_log.update!(needstype: 2) }.to change(lettings_log, :prevten).to nil + end + + it "clears prevten if renewal is update to no" do + expect { lettings_log.update!(renewal: 0) }.to change(lettings_log, :prevten).to nil + end + end + end + + context "and updating rent_type" do + let(:irproduct_other) { nil } + + around do |example| + Timecop.freeze(now) do + Singleton.__init__(FormHandler) + lettings_log.update!(rent_type:, irproduct_other:, startdate: now) + example.run + end + end + + context "when collection year is 2022/23 or earlier" do + let(:now) { Time.zone.local(2023, 1, 1) } + + context "when rent_type is Social Rent" do + let(:rent_type) { 0 } + + it "derives the most recent let type as Social Rent basis if it is a renewal and clears it if it is not" do + expect { lettings_log.update!(renewal: 1) }.to change(lettings_log, :unitletas).to 1 + expect { lettings_log.update!(renewal: 0) }.to change(lettings_log, :unitletas).from(1).to nil + end + end + + context "when rent_type is Affordable Rent" do + let(:rent_type) { 1 } + + it "derives the most recent let type as Affordable Rent basis if it is a renewal and clears it if it is not" do + expect { lettings_log.update!(renewal: 1) }.to change(lettings_log, :unitletas).to 2 + expect { lettings_log.update!(renewal: 0) }.to change(lettings_log, :unitletas).from(2).to nil + end + end + + context "when rent_type is London Affordable Rent" do + let(:rent_type) { 2 } + + it "derives the most recent let type as Affordable Rent basis if it is a renewal and clears it if it is not" do + expect { lettings_log.update!(renewal: 1) }.to change(lettings_log, :unitletas).to 2 + expect { lettings_log.update!(renewal: 0) }.to change(lettings_log, :unitletas).from(2).to nil + end + end + + context "when rent_type is Rent to Buy" do + let(:rent_type) { 3 } + + it "derives the most recent let type as Intermediate Rent basis if it is a renewal and clears it if it is not" do + expect { lettings_log.update!(renewal: 1) }.to change(lettings_log, :unitletas).to 4 + expect { lettings_log.update!(renewal: 0) }.to change(lettings_log, :unitletas).from(4).to nil + end + end + + context "when rent_type is London Living Rent" do + let(:rent_type) { 4 } + + it "derives the most recent let type as Intermediate Rent basis if it is a renewal and clears it if it is not" do + expect { lettings_log.update!(renewal: 1) }.to change(lettings_log, :unitletas).to 4 + expect { lettings_log.update!(renewal: 0) }.to change(lettings_log, :unitletas).from(4).to nil + end + end + + context "when rent_type is Other intermediate rent product" do + let(:rent_type) { 5 } + let(:irproduct_other) { "Rent first" } + + it "derives the most recent let type as Intermediate Rent basis if it is a renewal and clears it if it is not" do + expect { lettings_log.update!(renewal: 1) }.to change(lettings_log, :unitletas).to 4 + expect { lettings_log.update!(renewal: 0) }.to change(lettings_log, :unitletas).from(4).to nil + end + end + end + + context "when collection year is 2023/24 or later" do + let(:now) { Time.zone.local(2024, 1, 1) } + + context "when rent_type is Social Rent" do + let(:rent_type) { 0 } + + it "derives the most recent let type as Social Rent basis if it is a renewal and clears it if it is not" do + expect { lettings_log.update!(renewal: 1) }.to change(lettings_log, :unitletas).to 1 + expect { lettings_log.update!(renewal: 0) }.to change(lettings_log, :unitletas).from(1).to nil + end + end + + context "when rent_type is Affordable Rent" do + let(:rent_type) { 1 } + + it "derives the most recent let type as Affordable Rent basis if it is a renewal and clears it if it is not" do + expect { lettings_log.update!(renewal: 1) }.to change(lettings_log, :unitletas).to 2 + expect { lettings_log.update!(renewal: 0) }.to change(lettings_log, :unitletas).from(2).to nil + end + end + + context "when rent_type is London Affordable Rent" do + let(:rent_type) { 2 } + + it "derives the most recent let type as London Affordable Rent basis if it is a renewal and clears it if it is not" do + expect { lettings_log.update!(renewal: 1) }.to change(lettings_log, :unitletas).to 5 + expect { lettings_log.update!(renewal: 0) }.to change(lettings_log, :unitletas).from(5).to nil + end + end + + context "when rent_type is Rent to Buy" do + let(:rent_type) { 3 } + + it "derives the most recent let type as Rent to Buy basis if it is a renewal and clears it if it is not" do + expect { lettings_log.update!(renewal: 1) }.to change(lettings_log, :unitletas).to 6 + expect { lettings_log.update!(renewal: 0) }.to change(lettings_log, :unitletas).from(6).to nil + end + end + + context "when rent_type is London Living Rent" do + let(:rent_type) { 4 } + + it "derives the most recent let type as London Living Rent basis if it is a renewal and clears it if it is not" do + expect { lettings_log.update!(renewal: 1) }.to change(lettings_log, :unitletas).to 7 + expect { lettings_log.update!(renewal: 0) }.to change(lettings_log, :unitletas).from(7).to nil + end + end + + context "when rent_type is Other intermediate rent product" do + let(:rent_type) { 5 } + let(:irproduct_other) { "Rent first" } + + it "derives the most recent let type as Another Intermediate Rent basis if it is a renewal and clears it if it is not" do + expect { lettings_log.update!(renewal: 1) }.to change(lettings_log, :unitletas).to 8 + expect { lettings_log.update!(renewal: 0) }.to change(lettings_log, :unitletas).from(8).to nil + end + end + end + end + end +end + +# rubocop:enable RSpec/BeforeAfterAll +# rubocop:enable RSpec/InstanceVariable diff --git a/spec/models/lettings_log_spec.rb b/spec/models/lettings_log_spec.rb index a0281af45..67bcc10e5 100644 --- a/spec/models/lettings_log_spec.rb +++ b/spec/models/lettings_log_spec.rb @@ -241,7 +241,8 @@ RSpec.describe LettingsLog do describe "derived variables" do let!(:lettings_log) do - described_class.create({ + create( + :lettings_log, managing_organisation: owning_organisation, owning_organisation:, assigned_to: assigned_to_user, @@ -250,1841 +251,236 @@ RSpec.describe LettingsLog do startdate: Time.gm(2021, 10, 10), mrcdate: Time.gm(2021, 5, 4), voiddate: Time.gm(2021, 3, 3), - net_income_known: 2, + net_income_known: 2, # refused hhmemb: 7, rent_type: 4, hb: 1, hbrentshortfall: 1, created_at: Time.utc(2022, 2, 8, 16, 52, 15), - }) - end - - it "correctly derives and saves partial and full major repairs date" do - record_from_db = described_class.find(lettings_log.id) - expect(record_from_db["mrcdate"].day).to eq(4) - expect(record_from_db["mrcdate"].month).to eq(5) - expect(record_from_db["mrcdate"].year).to eq(2021) - end - - it "correctly derives and saves incref" do - expect(lettings_log.reload.incref).to eq(1) - - lettings_log.update!(net_income_known: 1) - expect(lettings_log.reload.incref).to eq(2) - - lettings_log.update!(net_income_known: 0) - expect(lettings_log.reload.incref).to eq(0) - end - - it "correctly derives and saves renttype" do - record_from_db = described_class.find(lettings_log.id) - expect(lettings_log.renttype).to eq(3) - expect(record_from_db["renttype"]).to eq(3) - end - - context "when deriving lettype" do - context "when the owning organisation is a PRP" do - before { lettings_log.owning_organisation.update!(provider_type: 2) } - - context "when the rent type is intermediate rent and supported housing" do - it "correctly derives and saves lettype" do - lettings_log.update!(rent_type: 4, needstype: 2) - record_from_db = described_class.find(lettings_log.id) - expect(lettings_log.lettype).to eq(10) - expect(record_from_db["lettype"]).to eq(10) - end - end - - context "when the rent type is intermediate rent and general needs housing" do - it "correctly derives and saves lettype" do - lettings_log.update!(rent_type: 4, needstype: 1) - record_from_db = described_class.find(lettings_log.id) - expect(lettings_log.lettype).to eq(9) - expect(record_from_db["lettype"]).to eq(9) - end - end - - context "when the rent type is affordable rent and supported housing" do - it "correctly derives and saves lettype" do - lettings_log.update!(rent_type: 2, needstype: 2) - record_from_db = described_class.find(lettings_log.id) - expect(lettings_log.lettype).to eq(6) - expect(record_from_db["lettype"]).to eq(6) - end - end - - context "when the rent type is affordable rent and general needs housing" do - it "correctly derives and saves lettype" do - lettings_log.update!(rent_type: 2, needstype: 1) - record_from_db = described_class.find(lettings_log.id) - expect(lettings_log.lettype).to eq(5) - expect(record_from_db["lettype"]).to eq(5) - end - end - - context "when the rent type is social rent and supported housing" do - it "correctly derives and saves lettype" do - lettings_log.update!(rent_type: 0, needstype: 2) - record_from_db = described_class.find(lettings_log.id) - expect(lettings_log.lettype).to eq(2) - expect(record_from_db["lettype"]).to eq(2) - end - end - - context "when the rent type is social rent and general needs housing" do - it "correctly derives and saves lettype" do - lettings_log.update!(rent_type: 0, needstype: 1) - record_from_db = described_class.find(lettings_log.id) - expect(lettings_log.lettype).to eq(1) - expect(record_from_db["lettype"]).to eq(1) - end - end - - context "when the tenant is not in receipt of applicable benefits" do - it "correctly resets total shortfall" do - lettings_log.update!(hbrentshortfall: 2, wtshortfall: 100, hb: 9) - record_from_db = described_class.find(lettings_log.id) - expect(lettings_log.wtshortfall).to be_nil - expect(record_from_db["wtshortfall"]).to be_nil - end - end - - context "when rent is paid bi-weekly" do - it "correctly derives and saves weekly rent" do - lettings_log.update!(brent: 100, period: 2) - record_from_db = described_class.find(lettings_log.id) - expect(lettings_log.wrent).to eq(50.0) - expect(record_from_db["wrent"]).to eq(50.0) - end - - it "correctly derives and saves weekly service charge" do - lettings_log.update!(scharge: 70, period: 2) - record_from_db = described_class.find(lettings_log.id) - expect(lettings_log.wscharge).to eq(35.0) - expect(record_from_db["wscharge"]).to eq(35.0) - end - - it "correctly derives and saves weekly personal service charge" do - lettings_log.update!(pscharge: 60, period: 2) - record_from_db = described_class.find(lettings_log.id) - expect(lettings_log.wpschrge).to eq(30.0) - expect(record_from_db["wpschrge"]).to eq(30.0) - end - - it "correctly derives and saves weekly support charge" do - lettings_log.update!(supcharg: 80, period: 2) - record_from_db = described_class.find(lettings_log.id) - expect(lettings_log.wsupchrg).to eq(40.0) - expect(record_from_db["wsupchrg"]).to eq(40.0) - end - - it "correctly derives and saves weekly total charge" do - lettings_log.update!(tcharge: 100, period: 2) - record_from_db = described_class.find(lettings_log.id) - expect(lettings_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 - lettings_log.update!(hbrentshortfall: 1, tshortfall: 100, period: 2, hb: 1) - record_from_db = described_class.find(lettings_log.id) - expect(lettings_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 - lettings_log.update!(hbrentshortfall: 1, tshortfall: 100, period: 2, hb: 6) - record_from_db = described_class.find(lettings_log.id) - expect(lettings_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 - lettings_log.update!(hbrentshortfall: 1, tshortfall: 100, period: 2, hb: 8) - record_from_db = described_class.find(lettings_log.id) - expect(lettings_log.wtshortfall).to eq(50.0) - expect(record_from_db["wtshortfall"]).to eq(50.0) - end - end - end - - it "correctly derives floats" do - lettings_log.update!(supcharg: 60.12, pscharge: 50.13, scharge: 60.98, brent: 60.97, period: 2) - record_from_db = described_class.find(lettings_log.id) - expect(lettings_log.wsupchrg).to eq(30.06) - expect(lettings_log.wpschrge).to eq(25.06) - expect(lettings_log.wscharge).to eq(30.49) - expect(lettings_log.wrent).to eq(30.49) - expect(lettings_log.wtcharge).to eq(116.1) - expect(record_from_db["wsupchrg"]).to eq(30.06) - expect(record_from_db["wpschrge"]).to eq(25.06) - expect(record_from_db["wscharge"]).to eq(30.49) - expect(record_from_db["wrent"]).to eq(30.49) - expect(record_from_db["wtcharge"]).to eq(116.1) - end - end - - context "when rent is paid every 4 weeks" do - it "correctly derives and saves weekly rent" do - lettings_log.update!(brent: 120, period: 3) - record_from_db = described_class.find(lettings_log.id) - expect(lettings_log.wrent).to eq(30.0) - expect(record_from_db["wrent"]).to eq(30.0) - end - - it "correctly derives and saves weekly service charge" do - lettings_log.update!(scharge: 120, period: 3) - record_from_db = described_class.find(lettings_log.id) - expect(lettings_log.wscharge).to eq(30.0) - expect(record_from_db["wscharge"]).to eq(30.0) - end - - it "correctly derives and saves weekly personal service charge" do - lettings_log.update!(pscharge: 120, period: 3) - record_from_db = described_class.find(lettings_log.id) - expect(lettings_log.wpschrge).to eq(30.0) - expect(record_from_db["wpschrge"]).to eq(30.0) - end - - it "correctly derives and saves weekly support charge" do - lettings_log.update!(supcharg: 120, period: 3) - record_from_db = described_class.find(lettings_log.id) - expect(lettings_log.wsupchrg).to eq(30.0) - expect(record_from_db["wsupchrg"]).to eq(30.0) - end - - it "correctly derives and saves weekly total charge" do - lettings_log.update!(tcharge: 120, period: 3) - record_from_db = described_class.find(lettings_log.id) - expect(lettings_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 - lettings_log.update!(hbrentshortfall: 1, tshortfall: 120, period: 3, hb: 1) - record_from_db = described_class.find(lettings_log.id) - expect(lettings_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 - lettings_log.update!(hbrentshortfall: 1, tshortfall: 120, period: 3, hb: 6) - record_from_db = described_class.find(lettings_log.id) - expect(lettings_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 - lettings_log.update!(hbrentshortfall: 1, tshortfall: 120, period: 3, hb: 8) - record_from_db = described_class.find(lettings_log.id) - expect(lettings_log.wtshortfall).to eq(30.0) - expect(record_from_db["wtshortfall"]).to eq(30.0) - end - end - end - - it "correctly derives floats" do - lettings_log.update!(supcharg: 100.12, pscharge: 100.13, scharge: 100.98, brent: 100.97, period: 3) - record_from_db = described_class.find(lettings_log.id) - expect(lettings_log.wsupchrg).to eq(25.03) - expect(lettings_log.wpschrge).to eq(25.03) - expect(lettings_log.wscharge).to eq(25.24) - expect(lettings_log.wrent).to eq(25.24) - expect(lettings_log.wtcharge).to eq(100.55) - expect(record_from_db["wsupchrg"]).to eq(25.03) - expect(record_from_db["wpschrge"]).to eq(25.03) - expect(record_from_db["wscharge"]).to eq(25.24) - expect(record_from_db["wrent"]).to eq(25.24) - expect(record_from_db["wtcharge"]).to eq(100.55) - end - end - - context "when rent is paid every calendar month" do - it "correctly derives and saves weekly rent" do - lettings_log.update!(brent: 130, period: 4) - record_from_db = described_class.find(lettings_log.id) - expect(lettings_log.wrent).to eq(30.0) - expect(record_from_db["wrent"]).to eq(30.0) - end - - it "correctly derives and saves weekly service charge" do - lettings_log.update!(scharge: 130, period: 4) - record_from_db = described_class.find(lettings_log.id) - expect(lettings_log.wscharge).to eq(30.0) - expect(record_from_db["wscharge"]).to eq(30.0) - end - - it "correctly derives and saves weekly personal service charge" do - lettings_log.update!(pscharge: 130, period: 4) - record_from_db = described_class.find(lettings_log.id) - expect(lettings_log.wpschrge).to eq(30.0) - expect(record_from_db["wpschrge"]).to eq(30.0) - end - - it "correctly derives and saves weekly support charge" do - lettings_log.update!(supcharg: 130, period: 4) - record_from_db = described_class.find(lettings_log.id) - expect(lettings_log.wsupchrg).to eq(30.0) - expect(record_from_db["wsupchrg"]).to eq(30.0) - end - - it "correctly derives and saves weekly total charge" do - lettings_log.update!(tcharge: 130, period: 4) - record_from_db = described_class.find(lettings_log.id) - expect(lettings_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 - lettings_log.update!(hbrentshortfall: 1, tshortfall: 130, period: 4, hb: 1) - record_from_db = described_class.find(lettings_log.id) - expect(lettings_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 - lettings_log.update!(hbrentshortfall: 1, tshortfall: 130, period: 4, hb: 6) - record_from_db = described_class.find(lettings_log.id) - expect(lettings_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 - lettings_log.update!(hbrentshortfall: 1, tshortfall: 130, period: 4, hb: 8) - record_from_db = described_class.find(lettings_log.id) - expect(lettings_log.wtshortfall).to eq(30.0) - expect(record_from_db["wtshortfall"]).to eq(30.0) - end - end - end - - it "correctly derives floats" do - lettings_log.update!(supcharg: 100.12, pscharge: 100.13, scharge: 100.98, brent: 100.97, period: 4) - record_from_db = described_class.find(lettings_log.id) - expect(lettings_log.wsupchrg).to eq(23.10) - expect(lettings_log.wpschrge).to eq(23.11) - expect(lettings_log.wscharge).to eq(23.30) - expect(lettings_log.wrent).to eq(23.30) - expect(lettings_log.wtcharge).to eq(92.82) - expect(record_from_db["wsupchrg"]).to eq(23.10) - expect(record_from_db["wpschrge"]).to eq(23.11) - expect(record_from_db["wscharge"]).to eq(23.30) - expect(record_from_db["wrent"]).to eq(23.30) - expect(record_from_db["wtcharge"]).to eq(92.82) - end - end - - context "when rent is paid weekly for 50 weeks" do - it "correctly derives and saves weekly rent" do - lettings_log.update!(brent: 130, period: 5) - record_from_db = described_class.find(lettings_log.id) - expect(lettings_log.wrent).to eq(125.0) - expect(record_from_db["wrent"]).to eq(125.0) - end - - it "correctly derives and saves weekly service charge" do - lettings_log.update!(scharge: 20, period: 5) - record_from_db = described_class.find(lettings_log.id) - expect(lettings_log.wscharge).to eq(19.23) - expect(record_from_db["wscharge"]).to eq(19.23) - end - - it "correctly derives and saves weekly personal service charge" do - lettings_log.update!(pscharge: 20, period: 5) - record_from_db = described_class.find(lettings_log.id) - expect(lettings_log.wpschrge).to eq(19.23) - expect(record_from_db["wpschrge"]).to eq(19.23) - end - - it "correctly derives and saves weekly support charge" do - lettings_log.update!(supcharg: 20, period: 5) - record_from_db = described_class.find(lettings_log.id) - expect(lettings_log.wsupchrg).to eq(19.23) - expect(record_from_db["wsupchrg"]).to eq(19.23) - end - - it "correctly derives and saves weekly total charge" do - lettings_log.update!(tcharge: 130, period: 5) - record_from_db = described_class.find(lettings_log.id) - expect(lettings_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 - lettings_log.update!(hbrentshortfall: 1, tshortfall: 130, period: 5, hb: 1) - record_from_db = described_class.find(lettings_log.id) - expect(lettings_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 - lettings_log.update!(hbrentshortfall: 1, tshortfall: 130, period: 5, hb: 6) - record_from_db = described_class.find(lettings_log.id) - expect(lettings_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 - lettings_log.update!(hbrentshortfall: 1, tshortfall: 130, period: 5, hb: 8) - record_from_db = described_class.find(lettings_log.id) - expect(lettings_log.wtshortfall).to eq(125.0) - expect(record_from_db["wtshortfall"]).to eq(125.0) - end - end - end - - it "correctly derives floats" do - lettings_log.update!(supcharg: 20.12, pscharge: 20.13, scharge: 20.98, brent: 100.97, period: 5) - record_from_db = described_class.find(lettings_log.id) - expect(lettings_log.wsupchrg).to eq(19.35) - expect(lettings_log.wpschrge).to eq(19.36) - expect(lettings_log.wscharge).to eq(20.17) - expect(lettings_log.wrent).to eq(97.09) - expect(lettings_log.wtcharge).to eq(155.96) - expect(record_from_db["wsupchrg"]).to eq(19.35) - expect(record_from_db["wpschrge"]).to eq(19.36) - expect(record_from_db["wscharge"]).to eq(20.17) - expect(record_from_db["wrent"]).to eq(97.09) - expect(record_from_db["wtcharge"]).to eq(155.96) - end - end - - context "when rent is paid weekly for 49 weeks" do - it "correctly derives and saves weekly rent" do - lettings_log.update!(brent: 130, period: 6) - record_from_db = described_class.find(lettings_log.id) - expect(lettings_log.wrent).to eq(122.5) - expect(record_from_db["wrent"]).to eq(122.5) - end - - it "correctly derives and saves weekly service charge" do - lettings_log.update!(scharge: 30, period: 6) - record_from_db = described_class.find(lettings_log.id) - expect(lettings_log.wscharge).to eq(28.27) - expect(record_from_db["wscharge"]).to eq(28.27) - end - - it "correctly derives and saves weekly personal service charge" do - lettings_log.update!(pscharge: 30, period: 6) - record_from_db = described_class.find(lettings_log.id) - expect(lettings_log.wpschrge).to eq(28.27) - expect(record_from_db["wpschrge"]).to eq(28.27) - end - - it "correctly derives and saves weekly support charge" do - lettings_log.update!(supcharg: 30, period: 6) - record_from_db = described_class.find(lettings_log.id) - expect(lettings_log.wsupchrg).to eq(28.27) - expect(record_from_db["wsupchrg"]).to eq(28.27) - end - - it "correctly derives and saves weekly total charge" do - lettings_log.update!(tcharge: 130, period: 6) - record_from_db = described_class.find(lettings_log.id) - expect(lettings_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 - lettings_log.update!(hbrentshortfall: 1, tshortfall: 130, period: 6, hb: 1) - record_from_db = described_class.find(lettings_log.id) - expect(lettings_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 - lettings_log.update!(hbrentshortfall: 1, tshortfall: 130, period: 6, hb: 6) - record_from_db = described_class.find(lettings_log.id) - expect(lettings_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 - lettings_log.update!(hbrentshortfall: 1, tshortfall: 130, period: 6, hb: 8) - lettings_log.reload - expect(lettings_log.wtshortfall).to eq(122.5) - end - end - end - - it "correctly derives floats" do - lettings_log.update!(supcharg: 30.12, pscharge: 30.13, scharge: 30.98, brent: 100.97, period: 6) - record_from_db = described_class.find(lettings_log.id) - expect(lettings_log.wsupchrg).to eq(28.38) - expect(lettings_log.wpschrge).to eq(28.39) - expect(lettings_log.wscharge).to eq(29.19) - expect(lettings_log.wrent).to eq(95.14) - expect(lettings_log.wtcharge).to eq(181.11) - expect(record_from_db["wsupchrg"]).to eq(28.38) - expect(record_from_db["wpschrge"]).to eq(28.39) - expect(record_from_db["wscharge"]).to eq(29.19) - expect(record_from_db["wrent"]).to eq(95.14) - expect(record_from_db["wtcharge"]).to eq(181.11) - end - end - - context "when rent is paid weekly for 48 weeks" do - it "correctly derives and saves weekly rent" do - lettings_log.update!(brent: 130, period: 7) - record_from_db = described_class.find(lettings_log.id) - expect(lettings_log.wrent).to eq(120.0) - expect(record_from_db["wrent"]).to eq(120.0) - end - - it "correctly derives and saves weekly service charge" do - lettings_log.update!(scharge: 30, period: 7) - record_from_db = described_class.find(lettings_log.id) - expect(lettings_log.wscharge).to eq(27.69) - expect(record_from_db["wscharge"]).to eq(27.69) - end - - it "correctly derives and saves weekly personal service charge" do - lettings_log.update!(pscharge: 30, period: 7) - record_from_db = described_class.find(lettings_log.id) - expect(lettings_log.wpschrge).to eq(27.69) - expect(record_from_db["wpschrge"]).to eq(27.69) - end - - it "correctly derives and saves weekly support charge" do - lettings_log.update!(supcharg: 30, period: 7) - record_from_db = described_class.find(lettings_log.id) - expect(lettings_log.wsupchrg).to eq(27.69) - expect(record_from_db["wsupchrg"]).to eq(27.69) - end - - it "correctly derives and saves weekly total charge" do - lettings_log.update!(tcharge: 130, period: 7) - record_from_db = described_class.find(lettings_log.id) - expect(lettings_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 - lettings_log.update!(hbrentshortfall: 1, tshortfall: 130, period: 7, hb: 1) - record_from_db = described_class.find(lettings_log.id) - expect(lettings_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 - lettings_log.update!(hbrentshortfall: 1, tshortfall: 130, period: 7, hb: 6) - record_from_db = described_class.find(lettings_log.id) - expect(lettings_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 - lettings_log.update!(hbrentshortfall: 1, tshortfall: 130, period: 7, hb: 8) - record_from_db = described_class.find(lettings_log.id) - expect(lettings_log.wtshortfall).to eq(120.0) - expect(record_from_db["wtshortfall"]).to eq(120.0) - end - end - end - - it "correctly derives floats" do - lettings_log.update!(supcharg: 30.12, pscharge: 30.13, scharge: 30.98, brent: 100.97, period: 7) - record_from_db = described_class.find(lettings_log.id) - expect(lettings_log.wsupchrg).to eq(27.8) - expect(lettings_log.wpschrge).to eq(27.81) - expect(lettings_log.wscharge).to eq(28.6) - expect(lettings_log.wrent).to eq(93.20) - expect(lettings_log.wtcharge).to eq(177.42) - expect(record_from_db["wsupchrg"]).to eq(27.8) - expect(record_from_db["wpschrge"]).to eq(27.81) - expect(record_from_db["wscharge"]).to eq(28.6) - expect(record_from_db["wrent"]).to eq(93.20) - expect(record_from_db["wtcharge"]).to eq(177.42) - end - end - - context "when rent is paid weekly for 47 weeks" do - it "correctly derives and saves weekly rent" do - lettings_log.update!(brent: 130, period: 8) - record_from_db = described_class.find(lettings_log.id) - expect(lettings_log.wrent).to eq(117.5) - expect(record_from_db["wrent"]).to eq(117.5) - end - - it "correctly derives and saves weekly service charge" do - lettings_log.update!(scharge: 30, period: 8) - record_from_db = described_class.find(lettings_log.id) - expect(lettings_log.wscharge).to eq(27.12) - expect(record_from_db["wscharge"]).to eq(27.12) - end - - it "correctly derives and saves weekly personal service charge" do - lettings_log.update!(pscharge: 30, period: 8) - record_from_db = described_class.find(lettings_log.id) - expect(lettings_log.wpschrge).to eq(27.12) - expect(record_from_db["wpschrge"]).to eq(27.12) - end - - it "correctly derives and saves weekly support charge" do - lettings_log.update!(supcharg: 30, period: 8) - record_from_db = described_class.find(lettings_log.id) - expect(lettings_log.wsupchrg).to eq(27.12) - expect(record_from_db["wsupchrg"]).to eq(27.12) - end - - it "correctly derives and saves weekly total charge" do - lettings_log.update!(tcharge: 130, period: 8) - record_from_db = described_class.find(lettings_log.id) - expect(lettings_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 - lettings_log.update!(hbrentshortfall: 1, tshortfall: 130, period: 8, hb: 1) - record_from_db = described_class.find(lettings_log.id) - expect(lettings_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 - lettings_log.update!(hbrentshortfall: 1, tshortfall: 130, period: 8, hb: 6) - record_from_db = described_class.find(lettings_log.id) - expect(lettings_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 - lettings_log.update!(hbrentshortfall: 1, tshortfall: 130, period: 8, hb: 8) - record_from_db = described_class.find(lettings_log.id) - expect(lettings_log.wtshortfall).to eq(117.5) - expect(record_from_db["wtshortfall"]).to eq(117.5) - end - end - end - - it "correctly derives floats" do - lettings_log.update!(supcharg: 30.12, pscharge: 30.13, scharge: 30.98, brent: 100.97, period: 8) - record_from_db = described_class.find(lettings_log.id) - expect(lettings_log.wsupchrg).to eq(27.22) - expect(lettings_log.wpschrge).to eq(27.23) - expect(lettings_log.wscharge).to eq(28) - expect(lettings_log.wrent).to eq(91.26) - expect(lettings_log.wtcharge).to eq(173.72) - expect(record_from_db["wsupchrg"]).to eq(27.22) - expect(record_from_db["wpschrge"]).to eq(27.23) - expect(record_from_db["wscharge"]).to eq(28) - expect(record_from_db["wrent"]).to eq(91.26) - expect(record_from_db["wtcharge"]).to eq(173.72) - end - end - - context "when rent is paid weekly for 46 weeks" do - it "correctly derives and saves weekly rent" do - lettings_log.update!(brent: 130, period: 9) - record_from_db = described_class.find(lettings_log.id) - expect(lettings_log.wrent).to eq(115.0) - expect(record_from_db["wrent"]).to eq(115.0) - end - - it "correctly derives and saves weekly service charge" do - lettings_log.update!(scharge: 30, period: 9) - record_from_db = described_class.find(lettings_log.id) - expect(lettings_log.wscharge).to eq(26.54) - expect(record_from_db["wscharge"]).to eq(26.54) - end - - it "correctly derives and saves weekly personal service charge" do - lettings_log.update!(pscharge: 30, period: 9) - record_from_db = described_class.find(lettings_log.id) - expect(lettings_log.wpschrge).to eq(26.54) - expect(record_from_db["wpschrge"]).to eq(26.54) - end - - it "correctly derives and saves weekly support charge" do - lettings_log.update!(supcharg: 30, period: 9) - record_from_db = described_class.find(lettings_log.id) - expect(lettings_log.wsupchrg).to eq(26.54) - expect(record_from_db["wsupchrg"]).to eq(26.54) - end - - it "correctly derives and saves weekly total charge" do - lettings_log.update!(tcharge: 130, period: 9) - record_from_db = described_class.find(lettings_log.id) - expect(lettings_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 - lettings_log.update!(hbrentshortfall: 1, tshortfall: 130, period: 9, hb: 1) - record_from_db = described_class.find(lettings_log.id) - expect(lettings_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 - lettings_log.update!(hbrentshortfall: 1, tshortfall: 130, period: 9, hb: 6) - record_from_db = described_class.find(lettings_log.id) - expect(lettings_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 - lettings_log.update!(hbrentshortfall: 1, tshortfall: 130, period: 9, hb: 8) - record_from_db = described_class.find(lettings_log.id) - expect(lettings_log.wtshortfall).to eq(115.0) - expect(record_from_db["wtshortfall"]).to eq(115.0) - end - end - end - - it "correctly derives floats" do - lettings_log.update!(supcharg: 30.12, pscharge: 30.13, scharge: 30.98, brent: 100.97, period: 9) - record_from_db = described_class.find(lettings_log.id) - expect(lettings_log.wsupchrg).to eq(26.64) - expect(lettings_log.wpschrge).to eq(26.65) - expect(lettings_log.wscharge).to eq(27.41) - expect(lettings_log.wrent).to eq(89.32) - expect(lettings_log.wtcharge).to eq(170.02) - expect(record_from_db["wsupchrg"]).to eq(26.64) - expect(record_from_db["wpschrge"]).to eq(26.65) - expect(record_from_db["wscharge"]).to eq(27.41) - expect(record_from_db["wrent"]).to eq(89.32) - expect(record_from_db["wtcharge"]).to eq(170.02) - end - end - - context "when rent is paid weekly for 52 weeks" do - it "correctly derives and saves weekly rent" do - lettings_log.update!(brent: 130, period: 1) - record_from_db = described_class.find(lettings_log.id) - expect(lettings_log.wrent).to eq(130.0) - expect(record_from_db["wrent"]).to eq(130.0) - end - - it "correctly derives and saves weekly service charge" do - lettings_log.update!(scharge: 30, period: 1) - record_from_db = described_class.find(lettings_log.id) - expect(lettings_log.wscharge).to eq(30.0) - expect(record_from_db["wscharge"]).to eq(30.0) - end - - it "correctly derives and saves weekly personal service charge" do - lettings_log.update!(pscharge: 30, period: 1) - record_from_db = described_class.find(lettings_log.id) - expect(lettings_log.wpschrge).to eq(30.0) - expect(record_from_db["wpschrge"]).to eq(30.0) - end - - it "correctly derives and saves weekly support charge" do - lettings_log.update!(supcharg: 30, period: 1) - record_from_db = described_class.find(lettings_log.id) - expect(lettings_log.wsupchrg).to eq(30.0) - expect(record_from_db["wsupchrg"]).to eq(30.0) - end - - it "correctly derives and saves weekly total charge" do - lettings_log.update!(tcharge: 30, period: 1) - record_from_db = described_class.find(lettings_log.id) - expect(lettings_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 - lettings_log.update!(hbrentshortfall: 1, tshortfall: 130, period: 1, hb: 1) - record_from_db = described_class.find(lettings_log.id) - expect(lettings_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 - lettings_log.update!(hbrentshortfall: 1, tshortfall: 130, period: 1, hb: 6) - record_from_db = described_class.find(lettings_log.id) - expect(lettings_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 - lettings_log.update!(hbrentshortfall: 1, tshortfall: 130, period: 1, hb: 8) - record_from_db = described_class.find(lettings_log.id) - expect(lettings_log.wtshortfall).to eq(130.0) - expect(record_from_db["wtshortfall"]).to eq(130.0) - end - end - end - - it "correctly derives floats" do - lettings_log.update!(supcharg: 30.12, pscharge: 25.13, scharge: 30.98, brent: 100.97, period: 1) - record_from_db = described_class.find(lettings_log.id) - expect(lettings_log.wsupchrg).to eq(30.12) - expect(lettings_log.wpschrge).to eq(25.13) - expect(lettings_log.wscharge).to eq(30.98) - expect(lettings_log.wrent).to eq(100.97) - expect(lettings_log.wtcharge).to eq(187.2) - expect(record_from_db["wsupchrg"]).to eq(30.12) - expect(record_from_db["wpschrge"]).to eq(25.13) - expect(record_from_db["wscharge"]).to eq(30.98) - expect(record_from_db["wrent"]).to eq(100.97) - 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 = described_class.find(lettings_log.id) - 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 = described_class.find(lettings_log.id) - 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 = described_class.find(lettings_log.id) - 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 = described_class.find(lettings_log.id) - 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 = described_class.find(lettings_log.id) - 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 = described_class.find(lettings_log.id) - 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 = described_class.find(lettings_log.id) - 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 = described_class.find(lettings_log.id) - 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 = described_class.find(lettings_log.id) - 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 - before { lettings_log.owning_organisation.update!(provider_type: "LA") } - - context "when the rent type is intermediate rent and supported housing" do - it "correctly derives and saves lettype" do - lettings_log.update!(rent_type: 4, needstype: 2) - record_from_db = described_class.find(lettings_log.id) - expect(lettings_log.lettype).to eq(12) - expect(record_from_db["lettype"]).to eq(12) - end - end - - context "when the rent type is intermediate rent and general needs housing" do - it "correctly derives and saves lettype" do - lettings_log.update!(rent_type: 4, needstype: 1) - record_from_db = described_class.find(lettings_log.id) - expect(lettings_log.lettype).to eq(11) - expect(record_from_db["lettype"]).to eq(11) - end - end - - context "when the rent type is affordable rent and supported housing" do - it "correctly derives and saves lettype" do - lettings_log.update!(rent_type: 2, needstype: 2) - record_from_db = described_class.find(lettings_log.id) - expect(lettings_log.lettype).to eq(8) - expect(record_from_db["lettype"]).to eq(8) - end - end - - context "when the rent type is affordable rent and general needs housing" do - it "correctly derives and saves lettype" do - lettings_log.update!(rent_type: 2, needstype: 1) - record_from_db = described_class.find(lettings_log.id) - expect(lettings_log.lettype).to eq(7) - expect(record_from_db["lettype"]).to eq(7) - end - end - - context "when the rent type is social rent and supported housing" do - it "correctly derives and saves lettype" do - lettings_log.update!(rent_type: 0, needstype: 2) - record_from_db = described_class.find(lettings_log.id) - expect(lettings_log.lettype).to eq(4) - expect(record_from_db["lettype"]).to eq(4) - end - end - - context "when the rent type is social rent and general needs housing" do - it "correctly derives and saves lettype" do - lettings_log.update!(rent_type: 0, needstype: 1) - record_from_db = described_class.find(lettings_log.id) - expect(lettings_log.lettype).to eq(3) - expect(record_from_db["lettype"]).to eq(3) - end - end - end - end - - it "correctly derives and saves day, month, year from start date" do - record_from_db = described_class.find(lettings_log.id) - expect(record_from_db["startdate"].day).to eq(10) - expect(record_from_db["startdate"].month).to eq(10) - expect(record_from_db["startdate"].year).to eq(2021) - end - - context "when any charge field is set" do - before do - lettings_log.update!(pscharge: 10) - end - - it "derives that any blank ones are 0" do - record_from_db = described_class.find(lettings_log.id) - expect(record_from_db["supcharg"].to_f).to eq(0.0) - expect(record_from_db["scharge"].to_f).to eq(0.0) - end - end - - def check_postcode_fields(postcode_field) - record_from_db = described_class.find(lettings_log.id) - expect(address_lettings_log[postcode_field]).to eq("M1 1AE") - expect(record_from_db[postcode_field]).to eq("M1 1AE") - end - - def check_previous_postcode_fields(postcode_field) - record_from_db = described_class.find(address_lettings_log.id) - expect(address_lettings_log[postcode_field]).to eq("M1 1AE") - expect(record_from_db[postcode_field]).to eq("M1 1AE") - end - - context "when saving addresses" do - before do - stub_request(:get, /api.postcodes.io/) - .to_return(status: 200, body: "{\"status\":200,\"result\":{\"admin_district\":\"Manchester\",\"codes\":{\"admin_district\": \"E08000003\"}}}", headers: {}) - end - - let!(:address_lettings_log) do - described_class.create({ - managing_organisation: owning_organisation, - owning_organisation:, - assigned_to: assigned_to_user, - postcode_known: 1, - postcode_full: "M1 1AE", - }) - end - - def check_property_postcode_fields - check_postcode_fields("postcode_full") - end - - it "correctly formats previous postcode" do - address_lettings_log.update!(postcode_full: "M1 1AE") - check_property_postcode_fields - - address_lettings_log.update!(postcode_full: "m1 1ae") - check_property_postcode_fields - - address_lettings_log.update!(postcode_full: "m11Ae") - check_property_postcode_fields - - address_lettings_log.update!(postcode_full: "m11ae") - check_property_postcode_fields - end - - it "correctly infers la" do - record_from_db = described_class.find(address_lettings_log.id) - expect(address_lettings_log.la).to eq("E08000003") - expect(record_from_db["la"]).to eq("E08000003") - end - - it "errors if the property postcode is emptied" do - expect { address_lettings_log.update!({ postcode_full: "" }) } - .to raise_error(ActiveRecord::RecordInvalid, /#{I18n.t("validations.postcode")}/) - end - - it "errors if the property postcode is not valid" do - expect { address_lettings_log.update!({ postcode_full: "invalid_postcode" }) } - .to raise_error(ActiveRecord::RecordInvalid, /#{I18n.t("validations.postcode")}/) - end - - context "when the local authority lookup times out" do - before do - allow(Timeout).to receive(:timeout).and_raise(Timeout::Error) - end - - it "logs a warning" do - expect(Rails.logger).to receive(:warn).with("Postcodes.io lookup timed out") - address_lettings_log.update!({ postcode_known: 1, postcode_full: "M1 1AD" }) - end - end - - it "correctly resets all fields if property postcode not known" do - address_lettings_log.update!({ postcode_known: 0 }) - - record_from_db = described_class.find(address_lettings_log.id) - expect(record_from_db["postcode_full"]).to eq(nil) - expect(address_lettings_log.la).to eq(nil) - expect(record_from_db["la"]).to eq(nil) - end - - it "changes the LA if property postcode changes from not known to known and provided" do - address_lettings_log.update!({ postcode_known: 0 }) - address_lettings_log.update!({ la: "E09000033" }) - - record_from_db = described_class.find(address_lettings_log.id) - expect(record_from_db["postcode_full"]).to eq(nil) - expect(address_lettings_log.la).to eq("E09000033") - expect(record_from_db["la"]).to eq("E09000033") - - address_lettings_log.update!({ postcode_known: 1, postcode_full: "M1 1AD" }) - - record_from_db = described_class.find(address_lettings_log.id) - expect(record_from_db["postcode_full"]).to eq("M1 1AD") - expect(address_lettings_log.la).to eq("E08000003") - expect(record_from_db["la"]).to eq("E08000003") - end - end - - context "when uprn is not confirmed" do - it "clears previous address on renewal logs" do - log = FactoryBot.build(:lettings_log, uprn_known: 1, uprn: 1, uprn_confirmed: 0, renewal: 1, prevloc: "E08000003", ppostcode_full: "A1 1AA", ppcodenk: 0, previous_la_known: 1) - - expect { log.set_derived_fields! }.to change(log, :prevloc).from("E08000003").to(nil) - .and change(log, :ppostcode_full).from("A1 1AA").to(nil) - .and change(log, :ppcodenk).from(0).to(nil) - .and change(log, :previous_la_known).from(1).to(nil) - end - - it "does not clear previous address on non renewal logs" do - log = FactoryBot.build(:lettings_log, uprn_known: 1, uprn: 1, uprn_confirmed: 0, renewal: 0, prevloc: "E08000003", ppostcode_full: "A1 1AA", ppcodenk: 0, previous_la_known: 1) - log.set_derived_fields! - expect(log.prevloc).to eq("E08000003") - expect(log.ppostcode_full).to eq("A1 1AA") - expect(log.ppcodenk).to eq(0) - expect(log.previous_la_known).to eq(1) - end - end - - context "when saving previous address" do - before do - stub_request(:get, /api.postcodes.io/) - .to_return(status: 200, body: "{\"status\":200,\"result\":{\"admin_district\":\"Manchester\", \"codes\":{\"admin_district\": \"E08000003\"}}}", headers: {}) - end - - let!(:address_lettings_log) do - described_class.create({ - managing_organisation: owning_organisation, - owning_organisation:, - assigned_to: assigned_to_user, - ppcodenk: 0, - ppostcode_full: "M1 1AE", - }) - end - - def previous_postcode_fields - check_previous_postcode_fields("ppostcode_full") - end - - it "correctly formats previous postcode" do - address_lettings_log.update!(ppostcode_full: "M1 1AE") - previous_postcode_fields - - address_lettings_log.update!(ppostcode_full: "m1 1ae") - previous_postcode_fields - - address_lettings_log.update!(ppostcode_full: "m11Ae") - previous_postcode_fields - - address_lettings_log.update!(ppostcode_full: "m11ae") - previous_postcode_fields - end - - it "correctly infers prevloc" do - record_from_db = described_class.find(address_lettings_log.id) - expect(address_lettings_log.prevloc).to eq("E08000003") - expect(record_from_db["prevloc"]).to eq("E08000003") - end - - it "errors if the previous postcode is emptied" do - expect { address_lettings_log.update!({ ppostcode_full: "" }) } - .to raise_error(ActiveRecord::RecordInvalid, /#{I18n.t("validations.postcode")}/) - end - - it "errors if the previous postcode is not valid" do - expect { address_lettings_log.update!({ ppostcode_full: "invalid_postcode" }) } - .to raise_error(ActiveRecord::RecordInvalid, /#{I18n.t("validations.postcode")}/) - end - - it "correctly resets all fields if previous postcode not known" do - address_lettings_log.update!({ ppcodenk: 1 }) - - record_from_db = described_class.find(address_lettings_log.id) - expect(record_from_db["ppostcode_full"]).to eq(nil) - expect(address_lettings_log.prevloc).to eq(nil) - expect(record_from_db["prevloc"]).to eq(nil) - end - - it "correctly resets la if la is not known" do - address_lettings_log.update!({ ppcodenk: 1 }) - address_lettings_log.update!({ previous_la_known: 1, prevloc: "S92000003" }) - record_from_db = described_class.find(address_lettings_log.id) - expect(record_from_db["prevloc"]).to eq("S92000003") - expect(address_lettings_log.prevloc).to eq("S92000003") - - address_lettings_log.update!({ previous_la_known: 0 }) - record_from_db = described_class.find(address_lettings_log.id) - expect(address_lettings_log.prevloc).to eq(nil) - expect(record_from_db["prevloc"]).to eq(nil) - end - - it "changes the prevloc if previous postcode changes from not known to known and provided" do - address_lettings_log.update!({ ppcodenk: 1 }) - address_lettings_log.update!({ previous_la_known: 1, prevloc: "E09000033" }) - - record_from_db = described_class.find(address_lettings_log.id) - expect(record_from_db["ppostcode_full"]).to eq(nil) - expect(address_lettings_log.prevloc).to eq("E09000033") - expect(record_from_db["prevloc"]).to eq("E09000033") - - address_lettings_log.update!({ ppcodenk: 0, ppostcode_full: "M1 1AD" }) - - record_from_db = described_class.find(address_lettings_log.id) - expect(record_from_db["ppostcode_full"]).to eq("M1 1AD") - expect(address_lettings_log.prevloc).to eq("E08000003") - expect(record_from_db["prevloc"]).to eq("E08000003") - end - end - - context "when saving rent and charges" do - let!(:lettings_log) do - described_class.create({ - managing_organisation: owning_organisation, - owning_organisation:, - assigned_to: assigned_to_user, - brent: 5.77, - scharge: 10.01, - pscharge: 3, - supcharg: 12.2, - }) - end - - it "correctly sums rental charges" do - record_from_db = described_class.find(lettings_log.id) - expect(record_from_db["tcharge"]).to eq(30.98) - end - end - - context "when validating household members derived vars" do - let!(:household_lettings_log) do - described_class.create!({ - managing_organisation: owning_organisation, - owning_organisation:, - assigned_to: assigned_to_user, - hhmemb: 3, - relat2: "X", - relat3: "C", - relat4: "X", - relat5: "C", - relat7: "C", - relat8: "X", - age1: 22, - age2: 16, - age4: 60, - age6: 88, - age7: 14, - age8: 42, - }) - end - - it "correctly derives and saves totchild" do - record_from_db = described_class.find(household_lettings_log.id) - expect(record_from_db["totchild"]).to eq(3) - end - - it "correctly derives and saves totelder" do - record_from_db = described_class.find(household_lettings_log.id) - expect(record_from_db["totelder"]).to eq(2) - end - - it "correctly derives and saves totadult" do - record_from_db = described_class.find(household_lettings_log.id) - expect(record_from_db["totadult"]).to eq(3) - end - - it "correctly derives economic status for tenants under 16" do - record_from_db = described_class.find(household_lettings_log.id) - expect(record_from_db["ecstat7"]).to eq(9) - end - end - - it "correctly derives and saves has_benefits" do - record_from_db = described_class.find(lettings_log.id) - expect(record_from_db["has_benefits"]).to eq(1) - end - - context "when updating values that derive vacdays" do - let(:lettings_log) { create(:lettings_log, startdate:) } - - context "when start date is set" do - let(:startdate) { Time.zone.now } - - it "correctly derives vacdays when voiddate is set" do - day_count = 3 - expect { lettings_log.update!(voiddate: startdate - day_count.days) }.to change(lettings_log, :vacdays).to day_count - expect { lettings_log.update!(voiddate: nil) }.to change(lettings_log, :vacdays).from(day_count).to nil - end - - it "correctly derives vacdays when mrcdate is set" do - day_count = 3 - expect { lettings_log.update!(mrcdate: startdate - day_count.days) }.to change(lettings_log, :vacdays).to day_count - expect { lettings_log.update!(mrcdate: nil) }.to change(lettings_log, :vacdays).from(day_count).to nil - end - end - - context "when start date is not set" do - let(:startdate) { nil } - - it "correctly derives vacdays when voiddate is set" do - day_count = 3 - lettings_log.update!(voiddate: Time.zone.now - day_count.days) - expect(lettings_log.vacdays).to be nil - end - - it "correctly derives vacdays when mrcdate is set" do - day_count = 3 - lettings_log.update!(mrcdate: Time.zone.now - day_count.days) - expect(lettings_log.vacdays).to be nil - end - end - end - - context "when updating renewal" do - let!(:lettings_log) do - described_class.create({ - managing_organisation: owning_organisation, - owning_organisation:, - assigned_to: assigned_to_user, - startdate: Time.zone.local(2021, 4, 10), - created_at: Time.utc(2022, 2, 8, 16, 52, 15), - }) - end - - it "correctly derives the length of time on local authority waiting list" do - expect { lettings_log.update!(renewal: 1) }.to change(lettings_log, :waityear).to 2 - expect { lettings_log.update!(renewal: 0) }.to change(lettings_log, :waityear).from(2).to nil - end - - it "correctly derives the number of times previously offered since becoming available" do - expect { lettings_log.update!(renewal: 1) }.to change(lettings_log, :offered).to 0 - expect { lettings_log.update!(renewal: 0) }.to change(lettings_log, :offered).from(0).to nil - end - - it "correctly derives referral if the letting is a renewal and clears it if it is not" do - expect { lettings_log.update!(renewal: 1) }.to change(lettings_log, :referral).to 1 - expect { lettings_log.update!(renewal: 0) }.to change(lettings_log, :referral).from(1).to nil - end - - it "correctly derives voiddate if the letting is a renewal and clears it if it is not" do - expect { lettings_log.update!(renewal: 1) }.to change(lettings_log, :voiddate).to lettings_log.startdate - expect { lettings_log.update!(renewal: 0) }.to change(lettings_log, :voiddate).from(lettings_log.startdate).to nil - end - - it "correctly derives first_time_property_let_as_social_housing and clears it if it is not" do - expect { lettings_log.update!(renewal: 1) }.to change(lettings_log, :first_time_property_let_as_social_housing).to 0 - expect { lettings_log.update!(renewal: 0) }.to change(lettings_log, :first_time_property_let_as_social_housing).from(0).to nil - end - - it "correctly derives vacancy reason and clears it if it is not" do - expect { lettings_log.update!(renewal: 1) }.to change(lettings_log, :rsnvac).to 14 - expect { lettings_log.update!(renewal: 0) }.to change(lettings_log, :rsnvac).from(14).to nil - end - - it "derives vacdays as 0 if log is renewal" do - expect { lettings_log.update!(renewal: 1) }.to change(lettings_log, :vacdays).to 0 - end - - it "correctly derives underoccupation_benefitcap if log is a renewal from 2021/22" do - lettings_log.update!(renewal: 1) - expect(lettings_log.underoccupation_benefitcap).to be 2 - end - - it "clears underoccupation_benefitcap if log is no longer a renewal" do - expect { lettings_log.update!(renewal: 1) }.to change(lettings_log, :underoccupation_benefitcap).to 2 - expect { lettings_log.update!(renewal: 0) }.to change(lettings_log, :underoccupation_benefitcap).from(2).to nil - end - - it "clears underoccupation_benefitcap if log is no longer in 2021/22" do - expect { lettings_log.update!(renewal: 1) }.to change(lettings_log, :underoccupation_benefitcap).to 2 - Timecop.return - Singleton.__init__(FormHandler) - expect { lettings_log.update!(startdate: Time.zone.local(2023, 4, 1)) }.to change(lettings_log, :underoccupation_benefitcap).from(2).to nil - end - - it "derives ppostcode_full as postcode_full if log is renewal" do - lettings_log.update!(renewal: 0, postcode_full: "M1 1AE", postcode_known: 1, ppostcode_full: "M1 1AD") - lettings_log.update!(renewal: 1) - lettings_log.reload - expect(lettings_log.ppostcode_full).to eq("M1 1AE") - expect(lettings_log.ppcodenk).to eq(0) - expect(lettings_log.prevloc).to eq(lettings_log.la) - end - - context "when the log is general needs" do - context "and the managing organisation is a private registered provider" do - before do - lettings_log.managing_organisation.update!(provider_type: "PRP") - lettings_log.update!(needstype: 1, renewal: 1) - end - - it "correctly derives prevten" do - expect(lettings_log.prevten).to be 32 - end - - it "clears prevten if the log is marked as supported housing" do - lettings_log.update!(needstype: 2) - expect(lettings_log.prevten).to be nil - end - - it "clears prevten if renewal is update to no" do - lettings_log.update!(renewal: 0) - expect(lettings_log.prevten).to be nil - end - end - - context "and the managing organisation is a local authority" do - before do - lettings_log.managing_organisation.update!(provider_type: "LA") - lettings_log.update!(needstype: 1, renewal: 1) - end - - it "correctly derives prevten" do - expect(lettings_log.prevten).to be 30 - end - - it "clears prevten if the log is marked as supported housing" do - expect { lettings_log.update!(needstype: 2) }.to change(lettings_log, :prevten).to nil - end - - it "clears prevten if renewal is update to no" do - expect { lettings_log.update!(renewal: 0) }.to change(lettings_log, :prevten).to nil - end - end - end - - context "and updating rent_type" do - let(:irproduct_other) { nil } - - around do |example| - Timecop.freeze(now) do - Singleton.__init__(FormHandler) - lettings_log.update!(rent_type:, irproduct_other:, startdate: now) - example.run - end - end - - context "when collection year is 2022/23 or earlier" do - let(:now) { Time.zone.local(2023, 1, 1) } - - context "when rent_type is Social Rent" do - let(:rent_type) { 0 } - - it "derives the most recent let type as Social Rent basis if it is a renewal and clears it if it is not" do - expect { lettings_log.update!(renewal: 1) }.to change(lettings_log, :unitletas).to 1 - expect { lettings_log.update!(renewal: 0) }.to change(lettings_log, :unitletas).from(1).to nil - end - end - - context "when rent_type is Affordable Rent" do - let(:rent_type) { 1 } - - it "derives the most recent let type as Affordable Rent basis if it is a renewal and clears it if it is not" do - expect { lettings_log.update!(renewal: 1) }.to change(lettings_log, :unitletas).to 2 - expect { lettings_log.update!(renewal: 0) }.to change(lettings_log, :unitletas).from(2).to nil - end - end - - context "when rent_type is London Affordable Rent" do - let(:rent_type) { 2 } - - it "derives the most recent let type as Affordable Rent basis if it is a renewal and clears it if it is not" do - expect { lettings_log.update!(renewal: 1) }.to change(lettings_log, :unitletas).to 2 - expect { lettings_log.update!(renewal: 0) }.to change(lettings_log, :unitletas).from(2).to nil - end - end - - context "when rent_type is Rent to Buy" do - let(:rent_type) { 3 } - - it "derives the most recent let type as Intermediate Rent basis if it is a renewal and clears it if it is not" do - expect { lettings_log.update!(renewal: 1) }.to change(lettings_log, :unitletas).to 4 - expect { lettings_log.update!(renewal: 0) }.to change(lettings_log, :unitletas).from(4).to nil - end - end - - context "when rent_type is London Living Rent" do - let(:rent_type) { 4 } - - it "derives the most recent let type as Intermediate Rent basis if it is a renewal and clears it if it is not" do - expect { lettings_log.update!(renewal: 1) }.to change(lettings_log, :unitletas).to 4 - expect { lettings_log.update!(renewal: 0) }.to change(lettings_log, :unitletas).from(4).to nil - end - end - - context "when rent_type is Other intermediate rent product" do - let(:rent_type) { 5 } - let(:irproduct_other) { "Rent first" } - - it "derives the most recent let type as Intermediate Rent basis if it is a renewal and clears it if it is not" do - expect { lettings_log.update!(renewal: 1) }.to change(lettings_log, :unitletas).to 4 - expect { lettings_log.update!(renewal: 0) }.to change(lettings_log, :unitletas).from(4).to nil - end - end - end - - context "when collection year is 2023/24 or later" do - let(:now) { Time.zone.local(2024, 1, 1) } - - context "when rent_type is Social Rent" do - let(:rent_type) { 0 } + ) + end - it "derives the most recent let type as Social Rent basis if it is a renewal and clears it if it is not" do - expect { lettings_log.update!(renewal: 1) }.to change(lettings_log, :unitletas).to 1 - expect { lettings_log.update!(renewal: 0) }.to change(lettings_log, :unitletas).from(1).to nil - end - end + it "correctly derives and saves partial and full major repairs date" do # weird test, am I missing something? + record_from_db = described_class.find(lettings_log.id) + expect(record_from_db["mrcdate"].day).to eq(4) + expect(record_from_db["mrcdate"].month).to eq(5) + expect(record_from_db["mrcdate"].year).to eq(2021) + end - context "when rent_type is Affordable Rent" do - let(:rent_type) { 1 } + it "correctly derives and saves day, month, year from start date" do # weird test, am I missing something? + record_from_db = described_class.find(lettings_log.id) + expect(record_from_db["startdate"].day).to eq(10) + expect(record_from_db["startdate"].month).to eq(10) + expect(record_from_db["startdate"].year).to eq(2021) + end - it "derives the most recent let type as Affordable Rent basis if it is a renewal and clears it if it is not" do - expect { lettings_log.update!(renewal: 1) }.to change(lettings_log, :unitletas).to 2 - expect { lettings_log.update!(renewal: 0) }.to change(lettings_log, :unitletas).from(2).to nil - end - end + def check_postcode_fields(postcode_field) + record_from_db = described_class.find(lettings_log.id) + expect(address_lettings_log[postcode_field]).to eq("M1 1AE") + expect(record_from_db[postcode_field]).to eq("M1 1AE") + end - context "when rent_type is London Affordable Rent" do - let(:rent_type) { 2 } + def check_previous_postcode_fields(postcode_field) + record_from_db = described_class.find(address_lettings_log.id) + expect(address_lettings_log[postcode_field]).to eq("M1 1AE") + expect(record_from_db[postcode_field]).to eq("M1 1AE") + end - it "derives the most recent let type as London Affordable Rent basis if it is a renewal and clears it if it is not" do - expect { lettings_log.update!(renewal: 1) }.to change(lettings_log, :unitletas).to 5 - expect { lettings_log.update!(renewal: 0) }.to change(lettings_log, :unitletas).from(5).to nil - end - end + context "when saving addresses" do + before do + stub_request(:get, /api.postcodes.io/) + .to_return(status: 200, body: "{\"status\":200,\"result\":{\"admin_district\":\"Manchester\",\"codes\":{\"admin_district\": \"E08000003\"}}}", headers: {}) + end - context "when rent_type is Rent to Buy" do - let(:rent_type) { 3 } + let!(:address_lettings_log) do + described_class.create({ + managing_organisation: owning_organisation, + owning_organisation:, + assigned_to: assigned_to_user, + postcode_known: 1, + postcode_full: "M1 1AE", + }) + end - it "derives the most recent let type as Rent to Buy basis if it is a renewal and clears it if it is not" do - expect { lettings_log.update!(renewal: 1) }.to change(lettings_log, :unitletas).to 6 - expect { lettings_log.update!(renewal: 0) }.to change(lettings_log, :unitletas).from(6).to nil - end - end + def check_property_postcode_fields + check_postcode_fields("postcode_full") + end - context "when rent_type is London Living Rent" do - let(:rent_type) { 4 } + it "correctly formats previous postcode" do + address_lettings_log.update!(postcode_full: "M1 1AE") + check_property_postcode_fields - it "derives the most recent let type as London Living Rent basis if it is a renewal and clears it if it is not" do - expect { lettings_log.update!(renewal: 1) }.to change(lettings_log, :unitletas).to 7 - expect { lettings_log.update!(renewal: 0) }.to change(lettings_log, :unitletas).from(7).to nil - end - end + address_lettings_log.update!(postcode_full: "m1 1ae") + check_property_postcode_fields - context "when rent_type is Other intermediate rent product" do - let(:rent_type) { 5 } - let(:irproduct_other) { "Rent first" } + address_lettings_log.update!(postcode_full: "m11Ae") + check_property_postcode_fields - it "derives the most recent let type as Another Intermediate Rent basis if it is a renewal and clears it if it is not" do - expect { lettings_log.update!(renewal: 1) }.to change(lettings_log, :unitletas).to 8 - expect { lettings_log.update!(renewal: 0) }.to change(lettings_log, :unitletas).from(8).to nil - end - end - end + address_lettings_log.update!(postcode_full: "m11ae") + check_property_postcode_fields end - end - - context "when updating rent type" do - let(:irproduct_other) { nil } - before do - lettings_log.update!(rent_type:, irproduct_other:) + it "correctly infers la" do + record_from_db = described_class.find(address_lettings_log.id) + expect(address_lettings_log.la).to eq("E08000003") + expect(record_from_db["la"]).to eq("E08000003") end - context "when rent_type is Social Rent" do - let(:rent_type) { 0 } - - it "derives renttype as Social Rent" do - expect(lettings_log.renttype).to be 1 - end + it "errors if the property postcode is emptied" do + expect { address_lettings_log.update!({ postcode_full: "" }) } + .to raise_error(ActiveRecord::RecordInvalid, /#{I18n.t("validations.postcode")}/) end - context "when rent_type is Affordable Rent" do - let(:rent_type) { 1 } - - it "derives renttype as Affordable Rent" do - expect(lettings_log.renttype).to be 2 - end + it "errors if the property postcode is not valid" do + expect { address_lettings_log.update!({ postcode_full: "invalid_postcode" }) } + .to raise_error(ActiveRecord::RecordInvalid, /#{I18n.t("validations.postcode")}/) end - context "when rent_type is London Affordable Rent" do - let(:rent_type) { 2 } + context "when the local authority lookup times out" do + before do + allow(Timeout).to receive(:timeout).and_raise(Timeout::Error) + end - it "derives renttype as Affordable Rent" do - expect(lettings_log.renttype).to be 2 + it "logs a warning" do + expect(Rails.logger).to receive(:warn).with("Postcodes.io lookup timed out") + address_lettings_log.update!({ postcode_known: 1, postcode_full: "M1 1AD" }) end end - context "when rent_type is Rent to Buy" do - let(:rent_type) { 3 } + it "correctly resets all fields if property postcode not known" do + address_lettings_log.update!({ postcode_known: 0 }) - it "derives renttype as Intermediate Rent" do - expect(lettings_log.renttype).to be 3 - end + record_from_db = described_class.find(address_lettings_log.id) + expect(record_from_db["postcode_full"]).to eq(nil) + expect(address_lettings_log.la).to eq(nil) + expect(record_from_db["la"]).to eq(nil) end - context "when rent_type is London Living Rent" do - let(:rent_type) { 4 } + it "changes the LA if property postcode changes from not known to known and provided" do + address_lettings_log.update!({ postcode_known: 0 }) + address_lettings_log.update!({ la: "E09000033" }) - it "derives renttype as Intermediate Rent" do - expect(lettings_log.renttype).to be 3 - end - end + record_from_db = described_class.find(address_lettings_log.id) + expect(record_from_db["postcode_full"]).to eq(nil) + expect(address_lettings_log.la).to eq("E09000033") + expect(record_from_db["la"]).to eq("E09000033") - context "when rent_type is Other intermediate rent product" do - let(:rent_type) { 5 } - let(:irproduct_other) { "Rent first" } + address_lettings_log.update!({ postcode_known: 1, postcode_full: "M1 1AD" }) - it "derives renttype as Intermediate Rent" do - expect(lettings_log.renttype).to be 3 - end + record_from_db = described_class.find(address_lettings_log.id) + expect(record_from_db["postcode_full"]).to eq("M1 1AD") + expect(address_lettings_log.la).to eq("E08000003") + expect(record_from_db["la"]).to eq("E08000003") end end - context "when answering the household characteristics questions" do - context "and some person details are refused" do - let!(:lettings_log) do - described_class.create({ - managing_organisation: owning_organisation, - owning_organisation:, - assigned_to: assigned_to_user, - age1_known: 1, - sex1: "R", - relat2: "R", - ecstat1: 10, - }) - end + context "when uprn is not confirmed" do + it "clears previous address on renewal logs" do + log = FactoryBot.build(:lettings_log, uprn_known: 1, uprn: 1, uprn_confirmed: 0, renewal: 1, prevloc: "E08000003", ppostcode_full: "A1 1AA", ppcodenk: 0, previous_la_known: 1) - it "correctly derives and saves refused" do - record_from_db = described_class.find(lettings_log.id) - expect(record_from_db["refused"]).to eq(1) - expect(lettings_log["refused"]).to eq(1) - end + expect { log.set_derived_fields! }.to change(log, :prevloc).from("E08000003").to(nil) + .and change(log, :ppostcode_full).from("A1 1AA").to(nil) + .and change(log, :ppcodenk).from(0).to(nil) + .and change(log, :previous_la_known).from(1).to(nil) end - context "and some person details are not known" do - let!(:lettings_log) do - described_class.create({ - managing_organisation: owning_organisation, - owning_organisation:, - assigned_to: assigned_to_user, - details_known_2: 1, - }) - end - - it "correctly derives and saves refused" do - record_from_db = described_class.find(lettings_log.id) - expect(record_from_db["refused"]).to eq(1) - expect(lettings_log["refused"]).to eq(1) - end + it "does not clear previous address on non renewal logs" do + log = FactoryBot.build(:lettings_log, uprn_known: 1, uprn: 1, uprn_confirmed: 0, renewal: 0, prevloc: "E08000003", ppostcode_full: "A1 1AA", ppcodenk: 0, previous_la_known: 1) + log.set_derived_fields! + expect(log.prevloc).to eq("E08000003") + expect(log.ppostcode_full).to eq("A1 1AA") + expect(log.ppcodenk).to eq(0) + expect(log.previous_la_known).to eq(1) end end - context "when it is supported housing and a care home charge has been supplied" do - let!(:lettings_log) do + context "when saving previous address" do + before do + stub_request(:get, /api.postcodes.io/) + .to_return(status: 200, body: "{\"status\":200,\"result\":{\"admin_district\":\"Manchester\", \"codes\":{\"admin_district\": \"E08000003\"}}}", headers: {}) + end + + let!(:address_lettings_log) do described_class.create({ managing_organisation: owning_organisation, owning_organisation:, assigned_to: assigned_to_user, - needstype: 2, + ppcodenk: 0, + ppostcode_full: "M1 1AE", }) end - context "when the care home charge is paid bi-weekly" do - it "correctly derives and saves weekly care home charge" do - lettings_log.update!(chcharge: 100, period: 2) - record_from_db = described_class.find(lettings_log.id) - expect(lettings_log.wchchrg).to eq(50.0) - expect(record_from_db["wchchrg"]).to eq(50.0) - end - - it "correctly derives floats" do - lettings_log.update!(chcharge: 100.12, period: 2) - record_from_db = described_class.find(lettings_log.id) - expect(lettings_log.wchchrg).to eq(50.06) - expect(record_from_db["wchchrg"]).to eq(50.06) - end - end - - context "when the care home charge is paid every 4 weeks" do - it "correctly derives and saves weekly care home charge" do - lettings_log.update!(chcharge: 120, period: 3) - record_from_db = described_class.find(lettings_log.id) - expect(lettings_log.wchchrg).to eq(30.0) - expect(record_from_db["wchchrg"]).to eq(30.0) - end - - it "correctly derives floats" do - lettings_log.update!(chcharge: 100.12, period: 3) - record_from_db = described_class.find(lettings_log.id) - expect(lettings_log.wchchrg).to eq(25.03) - expect(record_from_db["wchchrg"]).to eq(25.03) - end + def previous_postcode_fields + check_previous_postcode_fields("ppostcode_full") end - context "when the care home charge is paid every calendar month" do - it "correctly derives and saves weekly care home charge" do - lettings_log.update!(chcharge: 130, period: 4) - record_from_db = described_class.find(lettings_log.id) - expect(lettings_log.wchchrg).to eq(30.0) - expect(record_from_db["wchchrg"]).to eq(30.0) - end + it "correctly formats previous postcode" do + address_lettings_log.update!(ppostcode_full: "M1 1AE") + previous_postcode_fields - it "correctly derives floats" do - lettings_log.update!(chcharge: 100.12, period: 4) - record_from_db = described_class.find(lettings_log.id) - expect(lettings_log.wchchrg).to eq(23.10) - expect(record_from_db["wchchrg"]).to eq(23.10) - end - end + address_lettings_log.update!(ppostcode_full: "m1 1ae") + previous_postcode_fields - context "when the care home charge is paid weekly for 50 weeks" do - it "correctly derives and saves weekly care home charge" do - lettings_log.update!(chcharge: 130, period: 5) - record_from_db = described_class.find(lettings_log.id) - expect(lettings_log.wchchrg).to eq(125.0) - expect(record_from_db["wchchrg"]).to eq(125.0) - end + address_lettings_log.update!(ppostcode_full: "m11Ae") + previous_postcode_fields - it "correctly derives floats" do - lettings_log.update!(chcharge: 100.12, period: 5) - record_from_db = described_class.find(lettings_log.id) - expect(lettings_log.wchchrg).to eq(96.27) - expect(record_from_db["wchchrg"]).to eq(96.27) - end + address_lettings_log.update!(ppostcode_full: "m11ae") + previous_postcode_fields end - context "when the care home charge is paid weekly for 49 weeks" do - it "correctly derives and saves weekly care home charge" do - lettings_log.update!(chcharge: 130, period: 6) - record_from_db = described_class.find(lettings_log.id) - expect(lettings_log.wchchrg).to eq(122.5) - expect(record_from_db["wchchrg"]).to eq(122.5) - end - - it "correctly derives floats" do - lettings_log.update!(chcharge: 100.12, period: 6) - record_from_db = described_class.find(lettings_log.id) - expect(lettings_log.wchchrg).to eq(94.34) - expect(record_from_db["wchchrg"]).to eq(94.34) - end + it "correctly infers prevloc" do + record_from_db = described_class.find(address_lettings_log.id) + expect(address_lettings_log.prevloc).to eq("E08000003") + expect(record_from_db["prevloc"]).to eq("E08000003") end - context "when the care home charge is paid weekly for 48 weeks" do - it "correctly derives and saves weekly care home charge" do - lettings_log.update!(chcharge: 130, period: 7) - record_from_db = described_class.find(lettings_log.id) - expect(lettings_log.wchchrg).to eq(120.0) - expect(record_from_db["wchchrg"]).to eq(120.0) - end - - it "correctly derives floats" do - lettings_log.update!(chcharge: 100.12, period: 7) - record_from_db = described_class.find(lettings_log.id) - expect(lettings_log.wchchrg).to eq(92.42) - expect(record_from_db["wchchrg"]).to eq(92.42) - end + it "errors if the previous postcode is emptied" do + expect { address_lettings_log.update!({ ppostcode_full: "" }) } + .to raise_error(ActiveRecord::RecordInvalid, /#{I18n.t("validations.postcode")}/) end - context "when the care home charge is paid weekly for 47 weeks" do - it "correctly derives and saves weekly care home charge" do - lettings_log.update!(chcharge: 130, period: 8) - record_from_db = described_class.find(lettings_log.id) - expect(lettings_log.wchchrg).to eq(117.5) - expect(record_from_db["wchchrg"]).to eq(117.5) - end - - it "correctly derives floats" do - lettings_log.update!(chcharge: 100.12, period: 8) - record_from_db = described_class.find(lettings_log.id) - expect(lettings_log.wchchrg).to eq(90.49) - expect(record_from_db["wchchrg"]).to eq(90.49) - end + it "errors if the previous postcode is not valid" do + expect { address_lettings_log.update!({ ppostcode_full: "invalid_postcode" }) } + .to raise_error(ActiveRecord::RecordInvalid, /#{I18n.t("validations.postcode")}/) end - context "when the care home charge is paid weekly for 46 weeks" do - it "correctly derives and saves weekly care home charge" do - lettings_log.update!(chcharge: 130, period: 9) - record_from_db = described_class.find(lettings_log.id) - expect(lettings_log.wchchrg).to eq(115.0) - expect(record_from_db["wchchrg"]).to eq(115.0) - end + it "correctly resets all fields if previous postcode not known" do + address_lettings_log.update!({ ppcodenk: 1 }) - it "correctly derives floats" do - lettings_log.update!(chcharge: 100.12, period: 9) - record_from_db = described_class.find(lettings_log.id) - expect(lettings_log.wchchrg).to eq(88.57) - expect(record_from_db["wchchrg"]).to eq(88.57) - end + record_from_db = described_class.find(address_lettings_log.id) + expect(record_from_db["ppostcode_full"]).to eq(nil) + expect(address_lettings_log.prevloc).to eq(nil) + expect(record_from_db["prevloc"]).to eq(nil) end - context "when the care home charge is paid weekly for 52 weeks" do - it "correctly derives and saves weekly care home charge" do - lettings_log.update!(chcharge: 130, period: 1) - record_from_db = described_class.find(lettings_log.id) - expect(lettings_log.wchchrg).to eq(130.0) - expect(record_from_db["wchchrg"]).to eq(130.0) - end - - it "correctly derives floats" do - lettings_log.update!(chcharge: 100.12, period: 1) - record_from_db = described_class.find(lettings_log.id) - expect(lettings_log.wchchrg).to eq(100.12) - expect(record_from_db["wchchrg"]).to eq(100.12) - end - end - end + it "correctly resets la if la is not known" do + address_lettings_log.update!({ ppcodenk: 1 }) + address_lettings_log.update!({ previous_la_known: 1, prevloc: "S92000003" }) + record_from_db = described_class.find(address_lettings_log.id) + expect(record_from_db["prevloc"]).to eq("S92000003") + expect(address_lettings_log.prevloc).to eq("S92000003") - context "when the data provider is filling in the reason for the property being vacant" do - let!(:first_let_lettings_log) do - described_class.create({ - managing_organisation: owning_organisation, - owning_organisation:, - assigned_to: assigned_to_user, - first_time_property_let_as_social_housing: 1, - }) + address_lettings_log.update!({ previous_la_known: 0 }) + record_from_db = described_class.find(address_lettings_log.id) + expect(address_lettings_log.prevloc).to eq(nil) + expect(record_from_db["prevloc"]).to eq(nil) end - let!(:relet_lettings_log) do - described_class.create({ - managing_organisation: owning_organisation, - owning_organisation:, - assigned_to: assigned_to_user, - first_time_property_let_as_social_housing: 0, - }) - end + it "changes the prevloc if previous postcode changes from not known to known and provided" do + address_lettings_log.update!({ ppcodenk: 1 }) + address_lettings_log.update!({ previous_la_known: 1, prevloc: "E09000033" }) - it "the newprop variable is correctly derived and saved as 1 for a first let vacancy reason" do - first_let_lettings_log.update!({ rsnvac: 15 }) - record_from_db = described_class.find(first_let_lettings_log.id) - expect(record_from_db["newprop"]).to eq(1) - expect(first_let_lettings_log["newprop"]).to eq(1) - end + record_from_db = described_class.find(address_lettings_log.id) + expect(record_from_db["ppostcode_full"]).to eq(nil) + expect(address_lettings_log.prevloc).to eq("E09000033") + expect(record_from_db["prevloc"]).to eq("E09000033") - it "the newprop variable is correctly derived and saved as 2 for anything that is not a first let vacancy reason" do - relet_lettings_log.update!({ rsnvac: 2 }) - record_from_db = described_class.find(relet_lettings_log.id) - expect(record_from_db["newprop"]).to eq(2) - expect(relet_lettings_log["newprop"]).to eq(2) - end - end + address_lettings_log.update!({ ppcodenk: 0, ppostcode_full: "M1 1AD" }) - context "when a total shortfall is provided" do - it "derives that tshortfall is known" do - lettings_log.update!({ tshortfall: 10 }) - record_from_db = described_class.find(lettings_log.id) - expect(record_from_db["tshortfall_known"]).to eq(0) - expect(lettings_log["tshortfall_known"]).to eq(0) + record_from_db = described_class.find(address_lettings_log.id) + expect(record_from_db["ppostcode_full"]).to eq("M1 1AD") + expect(address_lettings_log.prevloc).to eq("E08000003") + expect(record_from_db["prevloc"]).to eq("E08000003") end end