|
|
|
@ -96,8 +96,8 @@ module Imports
|
|
|
|
|
|
|
|
|
|
attributes["prevten"] = unsafe_string_as_integer(xml_doc, "Q11") |
|
|
|
|
attributes["prevloc"] = string_or_nil(xml_doc, "Q12aONS") |
|
|
|
|
attributes["previous_postcode_known"] = previous_postcode_known(xml_doc) |
|
|
|
|
attributes["ppostcode_full"] = compose_postcode(xml_doc, "PPOSTC1", "PPOSTC2") |
|
|
|
|
attributes["previous_postcode_known"] = previous_postcode_known(xml_doc, attributes["ppostcode_full"]) |
|
|
|
|
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") |
|
|
|
@ -142,15 +142,17 @@ module Imports
|
|
|
|
|
attributes["postcode_known"] = attributes["postcode_full"].nil? ? 0 : 1 |
|
|
|
|
|
|
|
|
|
# Not specific to our form but required for consistency (present in import) |
|
|
|
|
attributes["old_form_id"] = Integer(field_value(xml_doc, "xmlns", "FORM")) |
|
|
|
|
attributes["old_form_id"] = safe_string_as_integer(xml_doc, "FORM") |
|
|
|
|
attributes["created_at"] = Time.zone.parse(field_value(xml_doc, "meta", "created-date")) |
|
|
|
|
attributes["updated_at"] = Time.zone.parse(field_value(xml_doc, "meta", "modified-date")) |
|
|
|
|
attributes["old_id"] = field_value(xml_doc, "meta", "document-id") |
|
|
|
|
|
|
|
|
|
# Required for our form invalidated questions (not present in import) |
|
|
|
|
attributes["previous_la_known"] = 1 # Defaulting to Yes (Required) |
|
|
|
|
attributes["la_known"] = 1 # Defaulting to Yes (Required) |
|
|
|
|
attributes["is_la_inferred"] = false # Always keep the given LA |
|
|
|
|
attributes["first_time_property_let_as_social_housing"] = first_time_let(attributes["rsnvac"]) |
|
|
|
|
attributes["declaration"] = declaration(xml_doc) |
|
|
|
|
|
|
|
|
|
unless attributes["brent"].nil? && |
|
|
|
|
attributes["scharge"].nil? && |
|
|
|
@ -163,8 +165,19 @@ module Imports
|
|
|
|
|
end |
|
|
|
|
|
|
|
|
|
case_log = CaseLog.new(attributes) |
|
|
|
|
case_log.save! |
|
|
|
|
save_case_log(case_log, attributes) |
|
|
|
|
compute_differences(case_log, attributes) |
|
|
|
|
check_status_completed(case_log) |
|
|
|
|
end |
|
|
|
|
|
|
|
|
|
def save_case_log(case_log, attributes) |
|
|
|
|
case_log.save! |
|
|
|
|
rescue ActiveRecord::RecordNotUnique |
|
|
|
|
unless attributes["old_id"].nil? |
|
|
|
|
record = CaseLog.find_by(old_id: attributes["old_id"]) |
|
|
|
|
@logger.info "Updating case log #{case_log.id} with legacy ID #{attributes['old_id']}" |
|
|
|
|
record.update!(attributes) |
|
|
|
|
end |
|
|
|
|
end |
|
|
|
|
|
|
|
|
|
def compute_differences(case_log, attributes) |
|
|
|
@ -172,10 +185,16 @@ module Imports
|
|
|
|
|
attributes.each do |key, value| |
|
|
|
|
case_log_value = case_log.send(key.to_sym) |
|
|
|
|
if value != case_log_value |
|
|
|
|
differences.push("#{key} #{value} #{case_log_value}") |
|
|
|
|
differences.push("#{key} #{value.inspect} #{case_log_value.inspect}") |
|
|
|
|
end |
|
|
|
|
end |
|
|
|
|
@logger.warn "Differences found when saving log #{case_log.id}: #{differences}" unless differences.empty? |
|
|
|
|
end |
|
|
|
|
|
|
|
|
|
def check_status_completed(case_log) |
|
|
|
|
unless case_log.status == "completed" |
|
|
|
|
@logger.warn "Case log #{case_log.id} is not completed" |
|
|
|
|
end |
|
|
|
|
raise "Differences found when saving log #{case_log.id}: #{differences}" unless differences.empty? |
|
|
|
|
end |
|
|
|
|
|
|
|
|
|
# Safe: A string that represents only an integer (or empty/nil) |
|
|
|
@ -186,8 +205,8 @@ module Imports
|
|
|
|
|
|
|
|
|
|
# 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) |
|
|
|
|
if str.blank? |
|
|
|
|
str = string_or_nil(xml_doc, attribute) |
|
|
|
|
if str.nil? |
|
|
|
|
nil |
|
|
|
|
else |
|
|
|
|
BigDecimal(str, exception: false) |
|
|
|
@ -196,8 +215,8 @@ module Imports
|
|
|
|
|
|
|
|
|
|
# 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? |
|
|
|
|
str = string_or_nil(xml_doc, attribute) |
|
|
|
|
if str.nil? |
|
|
|
|
nil |
|
|
|
|
else |
|
|
|
|
str.to_i |
|
|
|
@ -281,7 +300,7 @@ module Imports
|
|
|
|
|
end |
|
|
|
|
|
|
|
|
|
def sex(xml_doc, index) |
|
|
|
|
sex = field_value(xml_doc, "xmlns", "P#{index}Sex") |
|
|
|
|
sex = string_or_nil(xml_doc, "P#{index}Sex") |
|
|
|
|
case sex |
|
|
|
|
when "Male" |
|
|
|
|
"M" |
|
|
|
@ -295,7 +314,7 @@ module Imports
|
|
|
|
|
end |
|
|
|
|
|
|
|
|
|
def relat(xml_doc, index) |
|
|
|
|
relat = field_value(xml_doc, "xmlns", "P#{index}Rel") |
|
|
|
|
relat = string_or_nil(xml_doc, "P#{index}Rel") |
|
|
|
|
case relat |
|
|
|
|
when "Child" |
|
|
|
|
"C" |
|
|
|
@ -311,7 +330,7 @@ module Imports
|
|
|
|
|
def age_known(xml_doc, index, hhmemb) |
|
|
|
|
return nil if index > hhmemb |
|
|
|
|
|
|
|
|
|
age_refused = field_value(xml_doc, "xmlns", "P#{index}AR") |
|
|
|
|
age_refused = string_or_nil(xml_doc, "P#{index}AR") |
|
|
|
|
if age_refused == "AGE_REFUSED" |
|
|
|
|
1 # No |
|
|
|
|
else |
|
|
|
@ -332,19 +351,21 @@ module Imports
|
|
|
|
|
end |
|
|
|
|
end |
|
|
|
|
|
|
|
|
|
def previous_postcode_known(xml_doc) |
|
|
|
|
previous_postcode_known = field_value(xml_doc, "xmlns", "Q12bnot") |
|
|
|
|
def previous_postcode_known(xml_doc, previous_postcode) |
|
|
|
|
previous_postcode_known = string_or_nil(xml_doc, "Q12bnot") |
|
|
|
|
if previous_postcode_known == "Temporary or Unknown" |
|
|
|
|
0 |
|
|
|
|
elsif previous_postcode.nil? |
|
|
|
|
nil |
|
|
|
|
else |
|
|
|
|
1 |
|
|
|
|
end |
|
|
|
|
end |
|
|
|
|
|
|
|
|
|
def compose_postcode(xml_doc, outcode, incode) |
|
|
|
|
outcode_value = field_value(xml_doc, "xmlns", outcode) |
|
|
|
|
incode_value = field_value(xml_doc, "xmlns", incode) |
|
|
|
|
if outcode_value.blank? || incode_value.blank? |
|
|
|
|
outcode_value = string_or_nil(xml_doc, outcode) |
|
|
|
|
incode_value = string_or_nil(xml_doc, incode) |
|
|
|
|
if outcode_value.nil? || incode_value.nil? |
|
|
|
|
nil |
|
|
|
|
else |
|
|
|
|
"#{outcode_value}#{incode_value}" |
|
|
|
@ -400,7 +421,7 @@ module Imports
|
|
|
|
|
|
|
|
|
|
# Letters should be lowercase to match case |
|
|
|
|
def housing_needs(xml_doc, letter) |
|
|
|
|
housing_need = field_value(xml_doc, "xmlns", "Q10-#{letter}") |
|
|
|
|
housing_need = string_or_nil(xml_doc, "Q10-#{letter}") |
|
|
|
|
if housing_need == "Yes" |
|
|
|
|
1 |
|
|
|
|
else |
|
|
|
@ -409,7 +430,7 @@ module Imports
|
|
|
|
|
end |
|
|
|
|
|
|
|
|
|
def net_income_known(xml_doc, earnings) |
|
|
|
|
incref = field_value(xml_doc, "xmlns", "Q8Refused") |
|
|
|
|
incref = string_or_nil(xml_doc, "Q8Refused") |
|
|
|
|
if incref == "Refused" |
|
|
|
|
# Tenant prefers not to say |
|
|
|
|
2 |
|
|
|
@ -438,5 +459,12 @@ module Imports
|
|
|
|
|
0 |
|
|
|
|
end |
|
|
|
|
end |
|
|
|
|
|
|
|
|
|
def declaration(xml_doc) |
|
|
|
|
declaration = string_or_nil(xml_doc, "Qdp") |
|
|
|
|
if declaration == "Yes" |
|
|
|
|
1 |
|
|
|
|
end |
|
|
|
|
end |
|
|
|
|
end |
|
|
|
|
end |
|
|
|
|