From bf91a6f293224f6d8a7dedc2cae9926a487982eb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phane=20Meny?= Date: Thu, 14 Apr 2022 12:14:54 +0100 Subject: [PATCH] April 14 morning changes --- .../imports/case_logs_import_service.rb | 94 +++++++++++++++---- config/forms/2021_2022.json | 6 +- .../20220411092231_update_case_logs_fields.rb | 1 + db/schema.rb | 2 +- 4 files changed, 81 insertions(+), 22 deletions(-) diff --git a/app/services/imports/case_logs_import_service.rb b/app/services/imports/case_logs_import_service.rb index 803d53fed..e959798dd 100644 --- a/app/services/imports/case_logs_import_service.rb +++ b/app/services/imports/case_logs_import_service.rb @@ -47,15 +47,17 @@ module Imports 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["startertenancy"] = unsafe_string_as_integer(xml_doc, "_2a") + attributes["tenancy"] = unsafe_string_as_integer(xml_doc, "Q2b") + attributes["tenancyother"] = string_or_nil(xml_doc, "Q2ba") + attributes["tenancylength"] = safe_string_as_integer(xml_doc, "_2cYears") attributes["previous_postcode_known"] = previous_postcode_known(xml_doc) 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["irproduct_other"] = string_or_nil(xml_doc, "IRPRODUCTOTHER") attributes["rent_type"] = rent_type(xml_doc, attributes["lar"], attributes["irproduct"]) - attributes["rsnvac"] = unsafe_string_as_integer(xml_doc, "Q27") - attributes["renewal"] = renewal(attributes["rsnvac"]) attributes["hhmemb"] = safe_string_as_integer(xml_doc, "HHMEMB") (1..8).each do |index| attributes["age#{index}"] = safe_string_as_integer(xml_doc, "P#{index}Age") @@ -63,19 +65,29 @@ module Imports attributes["sex#{index}"] = sex(xml_doc, index) attributes["ecstat#{index}"] = unsafe_string_as_integer(xml_doc, "P#{index}Eco") end + (2..8).each do |index| + attributes["relat#{index}"] = relat(xml_doc, index) + end 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["armedforces"] = unsafe_string_as_integer(xml_doc, "ArmedF") + attributes["leftreg"] = unsafe_string_as_integer(xml_doc, "LeftAF") + attributes["reservist"] = unsafe_string_as_integer(xml_doc, "Inj") + attributes["hb"] = unsafe_string_as_integer(xml_doc, "Q6Ben") attributes["benefits"] = unsafe_string_as_integer(xml_doc, "Q7Ben") + attributes["earnings"] = safe_string_as_decimal(xml_doc, "Q8Money") + attributes["net_income_known"] = net_income_known(xml_doc, attributes["earnings"]) + attributes["incfreq"] = unsafe_string_as_integer(xml_doc, "Q8a") + attributes["reason"] = unsafe_string_as_integer(xml_doc, "Q9a") + attributes["reasonother"] = string_or_nil(xml_doc, "Q9aa") 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") @@ -100,17 +112,18 @@ module Imports 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"] = "" + attributes["rsnvac"] = unsafe_string_as_integer(xml_doc, "Q27") + attributes["renewal"] = renewal(attributes["rsnvac"]) # Not specific to our form but required for CDS and can't be inferred attributes["old_form_id"] = Integer(field_value(xml_doc, "xmlns", "FORM")) - # Required for us + # Specific to us attributes["previous_la_known"] = 1 # Defaulting to Yes (Required) attributes["la_known"] = 1 # Defaulting to Yes (Required) + attributes["created_at"] = Date.parse(field_value(xml_doc, "meta", "created-date")) + attributes["updated_at"] = Date.parse(field_value(xml_doc, "meta", "modified-date")) - # Derived and compared - attributes["earnings"] = "" pp attributes # case_log = CaseLog.new(attributes) @@ -119,6 +132,28 @@ module Imports # pp case_log.send(:mandatory_fields) end + # Safe: A string that represents only an integer (or empty/nil) + def safe_string_as_integer(xml_doc, attribute) + str = field_value(xml_doc, "xmlns", attribute) + Integer(str, exception: false) + end + + # Safe: A string that represents only a decimal (or empty/nil) + def safe_string_as_decimal(xml_doc, attribute) + str = field_value(xml_doc, "xmlns", attribute) + BigDecimal(str, exception: false) + end + + # Unsafe: A string that has more than just the integer value + def unsafe_string_as_integer(xml_doc, attribute) + str = field_value(xml_doc, "xmlns", attribute) + if str.blank? + nil + else + str.to_i + end + end + def start_date(xml_doc) day = Integer(field_value(xml_doc, "xmlns", "DAY")) month = Integer(field_value(xml_doc, "xmlns", "MONTH")) @@ -264,6 +299,22 @@ module Imports end end + def relat(xml_doc, index) + relat = field_value(xml_doc, "xmlns", "P#{index}Rel") + case relat + when "Child" + "C" + when "Partner" + "P" + when "Other", "Non-binary" + "X" + when "Refused" + "R" + else + nil + end + end + def age_known(xml_doc, index, hhmemb) return nil if index > hhmemb @@ -313,19 +364,12 @@ module Imports end end - # Safe: A string that represents only an integer (or empty/nil) - def safe_string_as_integer(xml_doc, attribute) - str = field_value(xml_doc, "xmlns", attribute) - Integer(str, exception: false) - end - - # Unsafe: A string that has more than just the integer value - def unsafe_string_as_integer(xml_doc, attribute) + def string_or_nil(xml_doc, attribute) str = field_value(xml_doc, "xmlns", attribute) if str.blank? nil else - str.to_i + str end end @@ -364,5 +408,19 @@ module Imports end end + def net_income_known(xml_doc, earnings) + incref = field_value(xml_doc, "xmlns", "Q8Refused") + if incref == "Refused" + # Tenant prefers not to say + 2 + elsif earnings.nil? + # No + 1 + else + # Yes + 0 + end + end + end end diff --git a/config/forms/2021_2022.json b/config/forms/2021_2022.json index fac00e5d0..f0ceea7dc 100644 --- a/config/forms/2021_2022.json +++ b/config/forms/2021_2022.json @@ -3438,12 +3438,12 @@ } }, "conditional_for": { - "other_reason_for_leaving_last_settled_home": [ - 31 + "reasonother": [ + 20 ] } }, - "other_reason_for_leaving_last_settled_home": { + "reasonother": { "header": "What is the reason?", "hint_text": "", "type": "text" diff --git a/db/migrate/20220411092231_update_case_logs_fields.rb b/db/migrate/20220411092231_update_case_logs_fields.rb index 8fd89b858..a66e5e3ff 100644 --- a/db/migrate/20220411092231_update_case_logs_fields.rb +++ b/db/migrate/20220411092231_update_case_logs_fields.rb @@ -6,6 +6,7 @@ class UpdateCaseLogsFields < ActiveRecord::Migration[7.0] t.remove :ppostc1, :ppostc2, type: :string t.rename :intermediate_rent_product_name, :irproduct_other t.rename :lawaitlist, :waityear + t.rename :other_reason_for_leaving_last_settled_home, :reasonother end end end diff --git a/db/schema.rb b/db/schema.rb index 4964c735c..5322fe1d4 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -104,7 +104,7 @@ ActiveRecord::Schema[7.0].define(version: 2022_04_11_092231) do t.integer "cbl" t.integer "chr" t.integer "cap" - t.string "other_reason_for_leaving_last_settled_home" + t.string "reasonother" t.integer "housingneeds_a" t.integer "housingneeds_b" t.integer "housingneeds_c"