From a86b036941cb94fcc29aff3af3aef13d277efd3e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phane=20Meny?= Date: Wed, 13 Apr 2022 18:01:35 +0100 Subject: [PATCH] April 13 changes --- app/models/bulk_upload.rb | 2 +- .../imports/case_logs_import_service.rb | 140 ++++++++++++++---- config/forms/2021_2022.json | 2 +- .../20220411092231_update_case_logs_fields.rb | 3 +- db/schema.rb | 3 +- spec/factories/case_log.rb | 2 +- spec/features/form/check_answers_page_spec.rb | 2 +- 7 files changed, 122 insertions(+), 32 deletions(-) diff --git a/app/models/bulk_upload.rb b/app/models/bulk_upload.rb index 3067baa91..120943a53 100644 --- a/app/models/bulk_upload.rb +++ b/app/models/bulk_upload.rb @@ -124,7 +124,7 @@ class BulkUpload # ppostc2: row[64], # prevpco_unknown: row[65], layear: row[66], - lawaitlist: row[67], + waityear: row[67], homeless: row[68], reasonpref: row[69], rp_homeless: row[70], diff --git a/app/services/imports/case_logs_import_service.rb b/app/services/imports/case_logs_import_service.rb index 6a4d9e655..803d53fed 100644 --- a/app/services/imports/case_logs_import_service.rb +++ b/app/services/imports/case_logs_import_service.rb @@ -39,38 +39,82 @@ module Imports other_intermediate_rent_product: 5, }.freeze - # Order matters since we derive from previous values (uses attributes) def create_log(xml_doc) attributes = {} # Required fields for status complete or logic to work + # Note: order matters when we derive from previous values (attributes parameter) attributes["startdate"] = start_date(xml_doc) attributes["owning_organisation_id"] = find_organisation_id(xml_doc, "OWNINGORGID") attributes["managing_organisation_id"] = find_organisation_id(xml_doc, "MANINGORGID") attributes["previous_postcode_known"] = previous_postcode_known(xml_doc) - attributes["ppostcode_full"] = previous_postcode(xml_doc, attributes) + attributes["ppostcode_full"] = previous_postcode(xml_doc, attributes["previous_postcode_known"]) attributes["needstype"] = needs_type(xml_doc) attributes["lar"] = london_affordable_rent(xml_doc) attributes["irproduct"] = unsafe_string_as_integer(xml_doc, "IRPRODUCT") attributes["irproduct_other"] = field_value(xml_doc, "xmlns", "IRPRODUCTOTHER") - attributes["rent_type"] = rent_type(xml_doc, attributes) + attributes["rent_type"] = rent_type(xml_doc, attributes["lar"], attributes["irproduct"]) attributes["rsnvac"] = unsafe_string_as_integer(xml_doc, "Q27") - attributes["renewal"] = renewal(attributes) + attributes["renewal"] = renewal(attributes["rsnvac"]) + attributes["hhmemb"] = safe_string_as_integer(xml_doc, "HHMEMB") (1..8).each do |index| - attributes["age#{index}"] = age(xml_doc, index) + attributes["age#{index}"] = safe_string_as_integer(xml_doc, "P#{index}Age") + attributes["age#{index}_known"] = age_known(xml_doc, index, attributes["hhmemb"]) attributes["sex#{index}"] = sex(xml_doc, index) attributes["ecstat#{index}"] = unsafe_string_as_integer(xml_doc, "P#{index}Eco") end - # attributes["hhmemb"] = + attributes["ethnic"] = unsafe_string_as_integer(xml_doc, "P1Eth") + attributes["ethnic_group"] = ethnic_group(attributes["ethnic"]) + attributes["national"] = unsafe_string_as_integer(xml_doc, "P1Nat") + attributes["startertenancy"] = unsafe_string_as_integer(xml_doc, "_2a") + attributes["armedforces"] = unsafe_string_as_integer(xml_doc, "ArmedF") + attributes["preg_occ"] = unsafe_string_as_integer(xml_doc, "Preg") + %w[a b c f g h].each do |letter| + attributes["housingneeds_#{letter}"] = housing_needs(xml_doc, letter) + end + + attributes["hb"] = unsafe_string_as_integer(xml_doc, "Q6Ben") + attributes["benefits"] = unsafe_string_as_integer(xml_doc, "Q7Ben") + + attributes["underoccupation_benefitcap"] = unsafe_string_as_integer(xml_doc, "_9b") + attributes["illness"] = unsafe_string_as_integer(xml_doc, "Q10ia") + attributes["layear"] = unsafe_string_as_integer(xml_doc, "Q12c") + attributes["waityear"] = unsafe_string_as_integer(xml_doc, "Q12d") + attributes["homeless"] = unsafe_string_as_integer(xml_doc, "Q13") + + # Note recheck boolean values for 0 instead of nil + attributes["reasonpref"] = unsafe_string_as_integer(xml_doc, "Q14a") + attributes["rp_homeless"] = unsafe_string_as_integer(xml_doc, "Q14b1") + attributes["rp_insan_unsat"] = unsafe_string_as_integer(xml_doc, "Q14b2") + attributes["rp_medwel"] = unsafe_string_as_integer(xml_doc, "Q14b3") + attributes["rp_hardship"] = unsafe_string_as_integer(xml_doc, "Q14b4") + attributes["rp_dontknow"] = unsafe_string_as_integer(xml_doc, "Q14b5") + + attributes["cbl"] = unsafe_string_as_integer(xml_doc, "Q15CBL") + attributes["chr"] = unsafe_string_as_integer(xml_doc, "Q15CHR") + attributes["cap"] = unsafe_string_as_integer(xml_doc, "Q15CAP") + + attributes["period"] = unsafe_string_as_integer(xml_doc, "Q17") + + attributes["beds"] = safe_string_as_integer(xml_doc, "Q22") + attributes["unittype_gn"] = unsafe_string_as_integer(xml_doc, "Q23") + attributes["builtype"] = unsafe_string_as_integer(xml_doc, "Q24") + attributes["wchair"] = unsafe_string_as_integer(xml_doc, "Q25") + attributes["net_income_known"] = "" # Not specific to our form but required for CDS and can't be inferred - # attributes["form"] = Integer(field_value(xml_doc, "xmlns", "FORM")) - # attributes["lettype"] = let_type(xml_doc, attributes) + attributes["old_form_id"] = Integer(field_value(xml_doc, "xmlns", "FORM")) - case_log = CaseLog.new(attributes) - case_log.save! + # Required for us + attributes["previous_la_known"] = 1 # Defaulting to Yes (Required) + attributes["la_known"] = 1 # Defaulting to Yes (Required) - # pp attributes + # Derived and compared + attributes["earnings"] = "" + + pp attributes + # case_log = CaseLog.new(attributes) + # case_log.save! # pp case_log.status # pp case_log.send(:mandatory_fields) end @@ -100,27 +144,29 @@ module Imports end end - # This does not match renttype (CDS) which is derived by case logs logic - def rent_type(xml_doc, attributes) + # 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) case sr_ar_ir when "SR" RENT_TYPE[:social_rent] when "AR" - if attributes["lar"] == 1 + if lar == 1 RENT_TYPE[:london_affordable_rent] else RENT_TYPE[:affordable_rent] end when "IR" - if attributes["irproduct"] == IRPRODUCT[:rent_to_buy] + if irproduct == IRPRODUCT[:rent_to_buy] RENT_TYPE[:rent_to_buy] - elsif attributes["irproduct"] == IRPRODUCT[:london_living_rent] + elsif irproduct == IRPRODUCT[:london_living_rent] RENT_TYPE[:london_living_rent] - elsif attributes["irproduct"] == IRPRODUCT[:other_intermediate_rent_product] + elsif irproduct == IRPRODUCT[:other_intermediate_rent_product] RENT_TYPE[:other_intermediate_rent_product] end + else + raise "Could not infer rent type with '#{sr_ar_ir}'" end end @@ -202,10 +248,6 @@ module Imports organisation.id end - def age(xml_doc, index) - Integer(field_value(xml_doc, "xmlns", "P#{index}Age"), exception: false) - end - def sex(xml_doc, index) sex = field_value(xml_doc, "xmlns", "P#{index}Sex") case sex @@ -217,6 +259,19 @@ module Imports "X" when "Refused" "R" + else + nil + end + end + + def age_known(xml_doc, index, hhmemb) + return nil if index > hhmemb + + age_refused = field_value(xml_doc, "xmlns", "P#{index}AR") + if age_refused == "AGE_REFUSED" + 1 # No + else + 0 # Yes end end @@ -229,8 +284,7 @@ module Imports end end - def previous_postcode(xml_doc, attributes) - previous_postcode_known = attributes["previous_postcode_known"] + def previous_postcode(xml_doc, previous_postcode_known) if previous_postcode_known.zero? nil else @@ -250,9 +304,9 @@ module Imports end end - def renewal(attributes) + def renewal(rsnvac) # Relet – renewal of fixed-term tenancy - if attributes["rsnvac"] == 14 + if rsnvac == 14 1 else 0 @@ -274,5 +328,41 @@ module Imports str.to_i end end + + def ethnic_group(ethnic) + case ethnic + when 1,2,3,18 + # White + 0 + when 4,5,6,7 + # Mixed + 1 + when 8,9,10,11,15 + # Asian + 2 + when 12,13,14 + # Black + 3 + when 16,19 + # Others + 4 + when 17 + # Refused + 5 + else + nil + end + end + + # Letters should be lowercase to match case + def housing_needs(xml_doc, letter) + housing_need = field_value(xml_doc, "xmlns", "Q10-#{letter}") + if housing_need == "Yes" + 1 + else + 0 + end + end + end end diff --git a/config/forms/2021_2022.json b/config/forms/2021_2022.json index 2b4ce0bfc..fac00e5d0 100644 --- a/config/forms/2021_2022.json +++ b/config/forms/2021_2022.json @@ -3279,7 +3279,7 @@ "header": "", "description": "", "questions": { - "lawaitlist": { + "waityear": { "check_answer_label": "Length of time on local authority waiting list", "header": "How long has the household been on the local authority waiting list for the new letting?", "hint_text": "", diff --git a/db/migrate/20220411092231_update_case_logs_fields.rb b/db/migrate/20220411092231_update_case_logs_fields.rb index f74d3d020..8fd89b858 100644 --- a/db/migrate/20220411092231_update_case_logs_fields.rb +++ b/db/migrate/20220411092231_update_case_logs_fields.rb @@ -2,9 +2,10 @@ class UpdateCaseLogsFields < ActiveRecord::Migration[7.0] def change change_table :case_logs, bulk: true do |t| t.integer :old_form_id, :lar, :irproduct - t.remove :day, :month, :year, :other_hhmemb, type: :integer + t.remove :day, :month, :year, :other_hhmemb, :accessibility_requirements_prefer_not_to_say, type: :integer t.remove :ppostc1, :ppostc2, type: :string t.rename :intermediate_rent_product_name, :irproduct_other + t.rename :lawaitlist, :waityear end end end diff --git a/db/schema.rb b/db/schema.rb index 5d1be1537..4964c735c 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -98,7 +98,7 @@ ActiveRecord::Schema[7.0].define(version: 2022_04_11_092231) do t.integer "benefits" t.integer "period" t.integer "layear" - t.integer "lawaitlist" + t.integer "waityear" t.string "postcode_full" t.integer "reasonpref" t.integer "cbl" @@ -111,7 +111,6 @@ ActiveRecord::Schema[7.0].define(version: 2022_04_11_092231) do t.integer "housingneeds_f" t.integer "housingneeds_g" t.integer "housingneeds_h" - t.integer "accessibility_requirements_prefer_not_to_say" t.integer "illness_type_1" t.integer "illness_type_2" t.integer "illness_type_3" diff --git a/spec/factories/case_log.rb b/spec/factories/case_log.rb index 4f27af26c..71a208b97 100644 --- a/spec/factories/case_log.rb +++ b/spec/factories/case_log.rb @@ -77,7 +77,7 @@ FactoryBot.define do supcharg { 35 } tcharge { 325 } layear { 2 } - lawaitlist { 1 } + waityear { 1 } postcode_full { "NW1 5TY" } reasonpref { 1 } cbl { 1 } diff --git a/spec/features/form/check_answers_page_spec.rb b/spec/features/form/check_answers_page_spec.rb index 9cdd19e78..dbeb9fa42 100644 --- a/spec/features/form/check_answers_page_spec.rb +++ b/spec/features/form/check_answers_page_spec.rb @@ -197,7 +197,7 @@ RSpec.describe "Form Check Answers Page" do tenant_code: nil, age1: nil, layear: 2, - lawaitlist: 1, + waityear: 1, postcode_full: "NW1 5TY", reason: 4, ppostcode_full: "SE2 6RT",