From a4b510056ae8c3870678a7bcf5158940bd4bfce7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phane=20Meny?= Date: Fri, 15 Jul 2022 10:53:17 +0100 Subject: [PATCH] Handles household charges --- .../derived_variables/case_log_variables.rb | 5 +- app/models/location.rb | 2 +- .../imports/case_logs_import_service.rb | 77 ++- spec/factories/location.rb | 2 +- .../0b4a68df-30cc-474a-93c0-a56ce8fdad3b.xml | 524 ++++++++++++++++++ .../imports/case_logs_import_service_spec.rb | 32 +- 6 files changed, 622 insertions(+), 20 deletions(-) create mode 100644 spec/fixtures/imports/case_logs/0b4a68df-30cc-474a-93c0-a56ce8fdad3b.xml diff --git a/app/models/derived_variables/case_log_variables.rb b/app/models/derived_variables/case_log_variables.rb index 8a8d8429a..e679d9058 100644 --- a/app/models/derived_variables/case_log_variables.rb +++ b/app/models/derived_variables/case_log_variables.rb @@ -73,10 +73,9 @@ module DerivedVariables::CaseLogVariables self.location = scheme.locations.first end if location - self.la = location.county self.postcode_full = location.postcode - wheelchair_adaptation_map = { 1 => 1, 0 => 2 } - self.wchair = wheelchair_adaptation_map[location.wheelchair_adaptation.to_i] + # TODO: Remove and replace with mobility type + self.wchair = location.wheelchair_adaptation_before_type_cast end if is_renewal? self.voiddate = startdate diff --git a/app/models/location.rb b/app/models/location.rb index ca5063215..0eb929938 100644 --- a/app/models/location.rb +++ b/app/models/location.rb @@ -6,7 +6,7 @@ class Location < ApplicationRecord WHEELCHAIR_ADAPTATIONS = { Yes: 1, - No: 0, + No: 2, }.freeze enum wheelchair_adaptation: WHEELCHAIR_ADAPTATIONS diff --git a/app/services/imports/case_logs_import_service.rb b/app/services/imports/case_logs_import_service.rb index 2a3f129df..e01e138ed 100644 --- a/app/services/imports/case_logs_import_service.rb +++ b/app/services/imports/case_logs_import_service.rb @@ -15,6 +15,12 @@ module Imports private + FORM_NAME_INDEX = { + start_year: 0, + rent_type: 2, + needs_type: 3 + }.freeze + GN_SH = { general_needs: 1, supported_housing: 2, @@ -175,15 +181,20 @@ module Imports attributes["first_time_property_let_as_social_housing"] = first_time_let(attributes["rsnvac"]) attributes["declaration"] = declaration(xml_doc) - # Set charges to 0 if others are partially populated - unless attributes["brent"].nil? && - attributes["scharge"].nil? && - attributes["pscharge"].nil? && - attributes["supcharg"].nil? - attributes["brent"] ||= BigDecimal("0.0") - attributes["scharge"] ||= BigDecimal("0.0") - attributes["pscharge"] ||= BigDecimal("0.0") - attributes["supcharg"] ||= BigDecimal("0.0") + set_partial_charges_to_zero(attributes) + + # Supported Housing fields + if attributes["needstype"] == GN_SH[:supported_housing] + old_visible_id = safe_string_as_integer(xml_doc, "_1cschemecode") + location = Location.find_by(old_visible_id:) + scheme = location.scheme + # Set the scheme via location, because the scheme old visible ID is not unique + attributes["location_id"] = location.id + attributes["scheme_id"] = scheme.id + attributes["sheltered"] = unsafe_string_as_integer(xml_doc, "Q1e") + attributes["chcharge"] = safe_string_as_decimal(xml_doc, "Q18b") + attributes["household_charge"] = household_charge(xml_doc) + attributes["is_carehome"] = is_carehome(scheme) end # Handles confidential schemes @@ -306,7 +317,7 @@ module Imports end def needs_type(xml_doc) - gn_sh = get_form_name_component(xml_doc, -1) + gn_sh = get_form_name_component(xml_doc, FORM_NAME_INDEX[:needs_type]) case gn_sh when "GN" GN_SH[:general_needs] @@ -319,7 +330,7 @@ module Imports # This does not match renttype (CDS) which is derived by case log logic def rent_type(xml_doc, lar, irproduct) - sr_ar_ir = get_form_name_component(xml_doc, -2) + sr_ar_ir = get_form_name_component(xml_doc, FORM_NAME_INDEX[:rent_type]) case sr_ar_ir when "SR" @@ -590,5 +601,49 @@ module Imports end end end + + def household_charge(xml_doc) + value = string_or_nil(xml_doc, "Q18c") + start_year = Integer(get_form_name_component(xml_doc, FORM_NAME_INDEX[:start_year])) + + if start_year <= 2021 + # Yes means that there are no charges (2021 or earlier) + value && value.include?("Yes") ? 1 : 0 + else + # Yes means that there are charges (2022 onwards) + value && value.include?("Yes") ? 0 : 1 + end + end + + def set_partial_charges_to_zero(attributes) + unless attributes["brent"].nil? && + attributes["scharge"].nil? && + attributes["pscharge"].nil? && + attributes["supcharg"].nil? + attributes["brent"] ||= BigDecimal("0.0") + attributes["scharge"] ||= BigDecimal("0.0") + attributes["pscharge"] ||= BigDecimal("0.0") + attributes["supcharg"] ||= BigDecimal("0.0") + end + end + + def clear_household_charges(attributes) + attributes["period"] = nil + attributes["brent"] = nil + attributes["scharge"] = nil + attributes["pscharge"] = nil + attributes["supcharg"] = nil + attributes["tcharge"] = nil + end + + def is_carehome(scheme) + return nil unless scheme + + if [2, 3, 4].include?(scheme.registered_under_care_act_before_type_cast) + 1 + else + 0 + end + end end end diff --git a/spec/factories/location.rb b/spec/factories/location.rb index 532a29da6..21663a86a 100644 --- a/spec/factories/location.rb +++ b/spec/factories/location.rb @@ -6,7 +6,7 @@ FactoryBot.define do type_of_unit { [1, 2, 3, 4, 6, 7].sample } type_of_building { "Purpose built" } mobility_type { %w[A M N W X].sample } - wheelchair_adaptation { 0 } + wheelchair_adaptation { 2 } county { Faker::Address.state } scheme end diff --git a/spec/fixtures/imports/case_logs/0b4a68df-30cc-474a-93c0-a56ce8fdad3b.xml b/spec/fixtures/imports/case_logs/0b4a68df-30cc-474a-93c0-a56ce8fdad3b.xml new file mode 100644 index 000000000..59c1eada7 --- /dev/null +++ b/spec/fixtures/imports/case_logs/0b4a68df-30cc-474a-93c0-a56ce8fdad3b.xml @@ -0,0 +1,524 @@ + + + 2021-CORE-SR-SH + 0b4a68df-30cc-474a-93c0-a56ce8fdad3b + c3061a2e6ea0b702e6f6210d5c52d2a92612d2aa + 7c5bd5fb549c09z2c55d9cb90d7ba84927e64618 + 7c5bd5fb549c09z2c55d9cb90d7ba84927e64618 + 2022-01-05T12:50:20.39153Z + 2022-01-05T12:50:20.39153Z + submitted-valid + 2021 + Manual Entry + + + + + Yes + 2021-11-05 +
123456
+ 2 Local Authority + + <_1cmangroupcode>123 + <_1cschemecode>10 + + 3 No + +
+ + <_2a>2 No + 1 Secure (inc flexible) + + <_2bTenCode>14044912001 + <_2cYears>2 + + + 72 + + Female + 5) Retired + 1 White: English/Scottish/Welsh/Northern Irish/British + 1 UK national resident in UK + 74 + + Male + Partner + 5) Retired + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + 2 No + + + 2 No + + + 9 Not in receipt of either UC or HB + + + 2 Some + Refused + + + + + 13 Property unsuitable because of ill health / disability + + + + <_9b override-field="">2 No + + + + + Yes + + 1 Yes + + + Yes + + + + + + + + 26 Owner occupation (private) + DLUHC + E08000035 + S80 4DJ + + 5 5 years or more + 9 3 years but under 4 years + + + 1 Not homeless + 2 No + + + + + + + + 1 Yes + 1 Yes + 1 Yes + + + 2 Tenant applied direct (no referral or nomination) + + + + 7 Weekly for 48 weeks + 125.00 + + + 7.00 + 132.00 + + + + + 2021-08-24 + + + 0 + 14044912 + + + 1 Yes + 15 First let of newbuild property + + + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 1 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 1 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + <_100>0 + 1 + 1 + 0 + 0 + 0 + 0 + 0 + 0 + <_70>1 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 1 + 0 + 0 + 0 + 0 + 0 + 0 + 1 + 1 + 1 + 2 + 1 + 1 + 0 + 0 + 0 + 0 + 0 + 0 + 2 + 74 + 0 + 0 + 0 + 0 + 0 + 0 + 74 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 72 + 0 + 74 + 2022-01-05Z + 2022-01-20Z + + + + + + + + + + + + + 0 + 0 + 0 + 0 + 20 + 0 + 20 + A + 1 + 1 + 1 + 1 + 4 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + + + 0.00 + 2 + 1 New Tenant + + 73 + 2 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 1 + 1 + 0 + 0 + 0 + 0 + 0 + 0 + 2 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 1 + 0 + 0 + 1 + 2 = 2 Adults at least one is an Elder + + + 15.00 + + + 2 Local Authority + + + E12000004 + 1 + DLUHC + DLUHC + N/A + N/A + + 1 + false + + + + + + + + + + + + + + + 15 + 15 + 000001005048 + D + 15 + 6 + 7 + 1 + 2 + A + P + M + + DLUHC + E08000035 + S80 4QE + + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + + + + + 115.38 + 121.85 + + + 6.46 + + 115.38 + 121.85 + + + 6.46 + + + 1 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 1 + + 115.38 + 0 + + 0 + 117.4 + 10 + + + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 5 + 11 + 2021 + 24 + 8 + 2021 + + + + LS16 + 6FT + LS16 + 6FT + +
diff --git a/spec/services/imports/case_logs_import_service_spec.rb b/spec/services/imports/case_logs_import_service_spec.rb index e44d15f31..b67f92481 100644 --- a/spec/services/imports/case_logs_import_service_spec.rb +++ b/spec/services/imports/case_logs_import_service_spec.rb @@ -23,6 +23,14 @@ RSpec.describe Imports::CaseLogsImportService do FactoryBot.create(:user, old_user_id: "c3061a2e6ea0b702e6f6210d5c52d2a92612d2aa", organisation:) FactoryBot.create(:user, old_user_id: "e29c492473446dca4d50224f2bb7cf965a261d6f", organisation:) + # Scheme and Location + scheme = FactoryBot.create(:scheme, old_visible_id: 123) + FactoryBot.create(:location, + old_visible_id: 10, + scheme_id: scheme.id, + wheelchair_adaptation: 1, + postcode: "LS166FT") + # Stub the form handler to use the real form allow(FormHandler.instance).to receive(:get_form).with("2021_2022").and_return(real_2021_2022_form) allow(FormHandler.instance).to receive(:get_form).with("2022_2023").and_return(real_2022_2023_form) @@ -36,11 +44,12 @@ RSpec.describe Imports::CaseLogsImportService do let(:case_log_id) { "0ead17cb-1668-442d-898c-0d52879ff592" } let(:case_log_id2) { "166fc004-392e-47a8-acb8-1c018734882b" } let(:case_log_id3) { "00d2343e-d5fa-4c89-8400-ec3854b0f2b4" } + let(:case_log_id4) { "0b4a68df-30cc-474a-93c0-a56ce8fdad3b" } before do # Stub the S3 file listing and download allow(storage_service).to receive(:list_files) - .and_return(%W[#{remote_folder}/#{case_log_id}.xml #{remote_folder}/#{case_log_id2}.xml #{remote_folder}/#{case_log_id3}.xml]) + .and_return(%W[#{remote_folder}/#{case_log_id}.xml #{remote_folder}/#{case_log_id2}.xml #{remote_folder}/#{case_log_id3}.xml #{remote_folder}/#{case_log_id4}.xml]) allow(storage_service).to receive(:get_file_io) .with("#{remote_folder}/#{case_log_id}.xml") .and_return(open_file(fixture_directory, case_log_id), open_file(fixture_directory, case_log_id)) @@ -50,6 +59,9 @@ RSpec.describe Imports::CaseLogsImportService do allow(storage_service).to receive(:get_file_io) .with("#{remote_folder}/#{case_log_id3}.xml") .and_return(open_file(fixture_directory, case_log_id3), open_file(fixture_directory, case_log_id3)) + allow(storage_service).to receive(:get_file_io) + .with("#{remote_folder}/#{case_log_id4}.xml") + .and_return(open_file(fixture_directory, case_log_id4), open_file(fixture_directory, case_log_id4)) end it "successfully create all case logs" do @@ -111,8 +123,8 @@ RSpec.describe Imports::CaseLogsImportService do before { case_log_xml.at_xpath("//xmlns:VYEAR").content = 2023 } it "does not import the voiddate" do - allow(logger).to receive(:warn).with(/is not completed/) - allow(logger).to receive(:warn).with(/Case log with old id:#{case_log_id} is incomplete but status should be complete/) + expect(logger).to receive(:warn).with(/is not completed/) + expect(logger).to receive(:warn).with(/Case log with old id:#{case_log_id} is incomplete but status should be complete/) case_log_service.send(:create_log, case_log_xml) @@ -138,7 +150,7 @@ RSpec.describe Imports::CaseLogsImportService do it "sets the economic status to child under 16" do # The update is done when calculating derived variables - allow(logger).to receive(:warn).with(/Differences found when saving log/) + expect(logger).to receive(:warn).with(/Differences found when saving log/) case_log_service.send(:create_log, case_log_xml) case_log = CaseLog.where(old_id: case_log_id).first @@ -203,5 +215,17 @@ RSpec.describe Imports::CaseLogsImportService do end end end + + context "and this is a supported housing log" do + let(:case_log_id) { "0b4a68df-30cc-474a-93c0-a56ce8fdad3b" } + + it "sets the scheme and location values" do + case_log_service.send(:create_log, case_log_xml) + case_log = CaseLog.find_by(old_id: case_log_id) + + expect(case_log.scheme_id).not_to be_nil + expect(case_log.location_id).not_to be_nil + end + end end end