diff --git a/app/services/imports/sales_logs_import_service.rb b/app/services/imports/sales_logs_import_service.rb index 3593a1227..d9ac0b0d4 100644 --- a/app/services/imports/sales_logs_import_service.rb +++ b/app/services/imports/sales_logs_import_service.rb @@ -16,7 +16,9 @@ module Imports private def create_log(xml_doc) + # only import sales logs from 22/23 collection period onwards return unless meta_field_value(xml_doc, "form-name").include?("Sales") + return unless compose_date(xml_doc, "DAY", "MONTH", "YEAR") >= Time.zone.local(2022, 4, 1) attributes = {} @@ -76,7 +78,7 @@ module Imports attributes["inc2mort"] = unsafe_string_as_integer(xml_doc, "Q2Person2MortApplication") attributes["hb"] = unsafe_string_as_integer(xml_doc, "Q2a") attributes["frombeds"] = safe_string_as_integer(xml_doc, "Q20Bedrooms") - attributes["staircase"] = unsafe_string_as_integer(xml_doc, "Q17aStaircase") + attributes["staircase"] = unsafe_string_as_integer(xml_doc, "Q17aStaircase") if attributes["ownershipsch"] == 1 attributes["stairbought"] = safe_string_as_integer(xml_doc, "PercentBought") attributes["stairowned"] = safe_string_as_integer(xml_doc, "PercentOwns") if attributes["staircase"] == 1 attributes["mrent"] = safe_string_as_decimal(xml_doc, "Q28MonthlyRent") @@ -102,7 +104,6 @@ module Imports attributes["ppcodenk"] = previous_postcode_known(xml_doc, attributes["ppostcode_full"], attributes["prevloc"]) # Q7UNKNOWNPOSTCODE check mapping attributes["ppostc1"] = string_or_nil(xml_doc, "PPOSTC1") attributes["ppostc2"] = string_or_nil(xml_doc, "PPOSTC2") - attributes["previous_la_known"] = nil attributes["hhregres"] = unsafe_string_as_integer(xml_doc, "ArmedF") attributes["hhregresstill"] = still_serving(xml_doc) attributes["proplen"] = safe_string_as_integer(xml_doc, "Q16aProplen2") || safe_string_as_integer(xml_doc, "Q16aProplensec2") @@ -111,7 +112,7 @@ module Imports attributes["prevten"] = unsafe_string_as_integer(xml_doc, "Q6PrevTenure") attributes["mortlen"] = mortgage_length(xml_doc, attributes) attributes["extrabor"] = borrowing(xml_doc, attributes) - attributes["mortgageused"] = unsafe_string_as_integer(xml_doc, "MORTGAGEUSED") + attributes["mortgageused"] = mortgage_used(xml_doc, attributes) attributes["wchair"] = unsafe_string_as_integer(xml_doc, "Q15Wheelchair") attributes["armedforcesspouse"] = unsafe_string_as_integer(xml_doc, "ARMEDFORCESSPOUSE") attributes["hodate"] = compose_date(xml_doc, "HODAY", "HOMONTH", "HOYEAR") @@ -130,9 +131,8 @@ module Imports attributes["prevshared"] = nil # 23/24 variable attributes["staircasesale"] = nil # 23/24 variable - # Required for our form invalidated questions (not present in import) - attributes["previous_la_known"] = 1 if attributes["prevloc"].present? && attributes["ppostcode_full"].blank? - if attributes["la"].present? && attributes["postcode_full"].blank? + attributes["previous_la_known"] = 1 if attributes["prevloc"].present? + if attributes["la"].present? attributes["la_known"] = 1 attributes["is_la_inferred"] = false end @@ -446,6 +446,17 @@ module Imports UKPostcode.parse(postcode).to_s end + def mortgage_used(xml_doc, attributes) + mortgageused = unsafe_string_as_integer(xml_doc, "MORTGAGEUSED") + return mortgageused unless mortgageused == 3 + + if attributes["mortgage"].present? || attributes["mortlen"].present? || attributes["extrabor"].present? + 1 # yes + else + 3 # don't know + end + end + def set_default_values(attributes) attributes["armedforcesspouse"] ||= 7 attributes["hhregres"] ||= 8 diff --git a/spec/services/imports/sales_logs_import_service_spec.rb b/spec/services/imports/sales_logs_import_service_spec.rb index d2dc4ad94..056d492de 100644 --- a/spec/services/imports/sales_logs_import_service_spec.rb +++ b/spec/services/imports/sales_logs_import_service_spec.rb @@ -17,14 +17,6 @@ RSpec.describe Imports::SalesLogsImportService do end before do - { "GL519EX" => "E07000078", - "SW1A2AA" => "E09000033", - "SW1A1AA" => "E09000033", - "SW147QP" => "E09000027", - "B955HZ" => "E07000221" }.each do |postcode, district_code| - WebMock.stub_request(:get, /api.postcodes.io\/postcodes\/#{postcode}/).to_return(status: 200, body: "{\"status\":200,\"result\":{\"admin_district\":\"#{district_code}\",\"codes\":{\"admin_district\":\"#{district_code}\"}}}", headers: {}) - end - allow(Organisation).to receive(:find_by).and_return(nil) allow(Organisation).to receive(:find_by).with(old_visible_id: organisation.old_visible_id).and_return(organisation) allow(Organisation).to receive(:find_by).with(old_visible_id: managing_organisation.old_visible_id).and_return(managing_organisation) @@ -119,6 +111,29 @@ RSpec.describe Imports::SalesLogsImportService do end end + context "and the log startdate is before 22/23 collection period" do + let(:sales_log_id) { "shared_ownership_sales_log" } + + before do + sales_log_xml.at_xpath("//xmlns:DAY").content = 10 + sales_log_xml.at_xpath("//xmlns:MONTH").content = 10 + sales_log_xml.at_xpath("//xmlns:YEAR").content = 2021 + sales_log_xml.at_xpath("//xmlns:HODAY").content = 9 + sales_log_xml.at_xpath("//xmlns:HOMONTH").content = 10 + sales_log_xml.at_xpath("//xmlns:HOYEAR").content = 2021 + sales_log_xml.at_xpath("//xmlns:EXDAY").content = 9 + sales_log_xml.at_xpath("//xmlns:EXMONTH").content = 10 + sales_log_xml.at_xpath("//xmlns:EXYEAR").content = 2021 + end + + it "does not create the log" do + expect(logger).not_to receive(:error) + expect(logger).not_to receive(:warn) + expect { sales_log_service.send(:create_log, sales_log_xml) } + .to change(SalesLog, :count).by(0) + end + end + context "when the mortgage lender is set to an existing option" do let(:sales_log_id) { "discounted_ownership_sales_log" } @@ -799,6 +814,18 @@ RSpec.describe Imports::SalesLogsImportService do expect(sales_log&.ppostcode_full).to eq("GL51 9EX") expect(sales_log&.status).to eq("completed") end + + it "correctly sets location fields for when location cannot be inferred from postcode" do + sales_log_xml.at_xpath("//xmlns:Q14ONSLACode").content = "E07000142" + sales_log_xml.at_xpath("//xmlns:Q14Postcode").content = "A11AA" + sales_log_service.send(:create_log, sales_log_xml) + + sales_log = SalesLog.find_by(old_id: sales_log_id) + expect(sales_log&.pcodenk).to eq(0) # postcode known + expect(sales_log&.la_known).to eq(1) # la known + expect(sales_log&.la).to eq("E07000142") + expect(sales_log&.status).to eq("completed") + end end context "when setting default buyer 1 previous tenancy" do @@ -824,6 +851,59 @@ RSpec.describe Imports::SalesLogsImportService do expect(sales_log&.prevten).to eq(2) end end + + context "when mortgage used is don't know" do + let(:sales_log_id) { "discounted_ownership_sales_log" } + + before do + allow(logger).to receive(:warn).and_return(nil) + end + + it "sets mortgageused to don't know if mortlen, mortgage and extrabor are blank" do + sales_log_xml.at_xpath("//xmlns:MORTGAGEUSED").content = "3 Don't know" + sales_log_xml.at_xpath("//xmlns:Q35Borrowing").content = "" + sales_log_xml.at_xpath("//xmlns:Q34b").content = "" + sales_log_xml.at_xpath("//xmlns:CALCMORT").content = "" + sales_log_xml.at_xpath("//xmlns:Q36CashDeposit").content = "134750" + sales_log_service.send(:create_log, sales_log_xml) + + sales_log = SalesLog.find_by(old_id: sales_log_id) + expect(sales_log&.mortgageused).to eq(3) + end + + it "sets mortgageused to yes if mortgage is given" do + sales_log_xml.at_xpath("//xmlns:MORTGAGEUSED").content = "3 Don't know" + sales_log_xml.at_xpath("//xmlns:Q35Borrowing").content = "" + sales_log_xml.at_xpath("//xmlns:Q34b").content = "" + sales_log_xml.at_xpath("//xmlns:CALCMORT").content = "134750" + sales_log_service.send(:create_log, sales_log_xml) + + sales_log = SalesLog.find_by(old_id: sales_log_id) + expect(sales_log&.mortgageused).to eq(1) + end + + it "sets mortgageused to yes if mortlen is given" do + sales_log_xml.at_xpath("//xmlns:MORTGAGEUSED").content = "3 Don't know" + sales_log_xml.at_xpath("//xmlns:Q35Borrowing").content = "" + sales_log_xml.at_xpath("//xmlns:Q34b").content = "10" + sales_log_xml.at_xpath("//xmlns:CALCMORT").content = "" + sales_log_service.send(:create_log, sales_log_xml) + + sales_log = SalesLog.find_by(old_id: sales_log_id) + expect(sales_log&.mortgageused).to eq(1) + end + + it "sets mortgageused to yes if extrabor is given" do + sales_log_xml.at_xpath("//xmlns:MORTGAGEUSED").content = "3 Don't know" + sales_log_xml.at_xpath("//xmlns:Q35Borrowing").content = "3000" + sales_log_xml.at_xpath("//xmlns:Q34b").content = "" + sales_log_xml.at_xpath("//xmlns:CALCMORT").content = "" + sales_log_service.send(:create_log, sales_log_xml) + + sales_log = SalesLog.find_by(old_id: sales_log_id) + expect(sales_log&.mortgageused).to eq(1) + end + end end end end