diff --git a/app/models/case_log.rb b/app/models/case_log.rb index 8bb851f9e..934841492 100644 --- a/app/models/case_log.rb +++ b/app/models/case_log.rb @@ -349,7 +349,7 @@ class CaseLog < ApplicationRecord referral == 3 end - def is_prevten_la_general_needs? + def is_prevten_general_needs_tenancy? # 30: Fixed term Local Authority General Needs tenancy # 31: Lifetime Local Authority General Needs tenancy # 32: Fixed term Private Registered Provider General Needs tenancy @@ -473,7 +473,7 @@ private self.referral = 0 self.layear = 1 if is_general_needs? - # TODO: Check value since 30/31 are LA and 32/33 are PRP (fixed term VS lifetime) + # fixed term self.prevten = 32 if managing_organisation.provider_type == "PRP" self.prevten = 30 if managing_organisation.provider_type == "LA" end @@ -485,8 +485,11 @@ private self["ecstat#{idx}"] = nil end end - self.landlord = 1 if owning_organisation.provider_type == "LA" - self.landlord = 2 if owning_organisation.provider_type == "PRP" + if owning_organisation == managing_organisation + self.landlord = 1 + else + self.landlord = 2 + end end def process_postcode_changes! diff --git a/app/models/validations/household_validations.rb b/app/models/validations/household_validations.rb index 1e5954ab3..bcdbe01ba 100644 --- a/app/models/validations/household_validations.rb +++ b/app/models/validations/household_validations.rb @@ -105,7 +105,7 @@ module Validations::HouseholdValidations record.errors.add :homeless, I18n.t("validations.household.homeless.other.internal_transfer") end - if record.is_internal_transfer? && record.this_landlord? && record.is_prevten_la_general_needs? + if record.is_internal_transfer? && record.this_landlord? && record.is_prevten_general_needs_tenancy? record.errors.add :referral, I18n.t("validations.household.referral.la_general_needs.internal_transfer") record.errors.add :prevten, I18n.t("validations.household.prevten.la_general_needs.internal_transfer") end diff --git a/app/services/imports/case_logs_import_service.rb b/app/services/imports/case_logs_import_service.rb index bc11d0ef5..1f91bd35e 100644 --- a/app/services/imports/case_logs_import_service.rb +++ b/app/services/imports/case_logs_import_service.rb @@ -45,8 +45,8 @@ module Imports # Required fields for status complete or logic to work # Note: order matters when we derive from previous values (attributes parameter) attributes["startdate"] = compose_date(xml_doc, "DAY", "MONTH", "YEAR") - attributes["owning_organisation_id"] = find_organisation_id(xml_doc, "OWNINGORGID") - attributes["managing_organisation_id"] = find_organisation_id(xml_doc, "MANINGORGID") + attributes["owning_organisation_id"] = find_organisation_id(xml_doc, "OWNINGORGID", "OWNINGORGNAME", "HCNUM") + attributes["managing_organisation_id"] = find_organisation_id(xml_doc, "MANINGORGID", "MANINGORGNAME", "MANHCNUM") 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") @@ -230,79 +230,22 @@ module Imports end end - # def let_type(xml_doc, attributes) - # # "1 Private Registered Provider" or "2 Local Authority" - # # We do not store providertype since it comes from the organisation import - # landlord = field_value(xml_doc, "xmlns", "Landlord").to_i - # - # if attributes["renttype"] == SR_AR_IR[:social_rent] && - # attributes["needstype"] == GN_SH[:general_needs] && - # landlord == PRP_LA[:private_registered_provider] - # 1 - # elsif attributes["renttype"] == SR_AR_IR[:social_rent] && - # attributes["needstype"] == GN_SH[:supported_housing] && - # landlord == PRP_LA[:private_registered_provider] - # 2 - # elsif attributes["renttype"] == SR_AR_IR[:social_rent] && - # attributes["needstype"] == GN_SH[:general_needs] && - # landlord == PRP_LA[:local_authority] - # 3 - # elsif attributes["renttype"] == SR_AR_IR[:social_rent] && - # attributes["needstype"] == GN_SH[:supported_housing] && - # landlord == PRP_LA[:local_authority] - # 4 - # elsif attributes["renttype"] == SR_AR_IR[:affordable_rent] && - # attributes["needstype"] == GN_SH[:general_needs] && - # landlord == PRP_LA[:private_registered_provider] - # 5 - # elsif attributes["renttype"] == SR_AR_IR[:affordable_rent] && - # attributes["needstype"] == GN_SH[:supported_housing] && - # landlord == PRP_LA[:private_registered_provider] - # 6 - # elsif attributes["renttype"] == SR_AR_IR[:affordable_rent] && - # attributes["needstype"] == GN_SH[:general_needs] && - # landlord == PRP_LA[:local_authority] - # 7 - # elsif attributes["renttype"] == SR_AR_IR[:affordable_rent] && - # attributes["needstype"] == GN_SH[:supported_housing] && - # landlord == PRP_LA[:local_authority] - # 8 - # elsif attributes["renttype"] == SR_AR_IR[:intermediate_rent] && - # attributes["needstype"] == GN_SH[:general_needs] && - # landlord == PRP_LA[:private_registered_provider] - # 9 - # elsif attributes["renttype"] == SR_AR_IR[:intermediate_rent] && - # attributes["needstype"] == GN_SH[:supported_housing] && - # landlord == PRP_LA[:private_registered_provider] - # 10 - # elsif attributes["renttype"] == SR_AR_IR[:intermediate_rent] && - # attributes["needstype"] == GN_SH[:general_needs] && - # landlord == PRP_LA[:local_authority] - # 11 - # elsif attributes["renttype"] == SR_AR_IR[:intermediate_rent] && - # attributes["needstype"] == GN_SH[:supported_housing] && - # landlord == PRP_LA[:local_authority] - # 12 - # else - # raise "Could not infer rent type with rentype:#{attributes['renttype']} / needstype:#{attributes['needstype']} / landlord:#{landlord}" - # end - # end - - def find_organisation_id(xml_doc, field) - old_visible_id = field_value(xml_doc, "xmlns", field).to_i - landlord = field_value(xml_doc, "xmlns", "Landlord").to_i - + def find_organisation_id(xml_doc, id_field, name_field, reg_field) + old_visible_id = unsafe_string_as_integer(xml_doc, id_field) organisation = Organisation.find_by(old_visible_id:) # Quick hack: should be removed when all organisations are imported # Will fail in the future if the organisation is missing if organisation.nil? organisation = Organisation.new organisation.old_visible_id = old_visible_id - organisation.provider_type = if landlord == 2 + let_type = unsafe_string_as_integer(xml_doc, "landlord") + organisation.provider_type = if let_type == PRP_LA[:local_authority] 1 else 2 end + organisation.name = string_or_nil(xml_doc, name_field) + organisation.housing_registration_no = string_or_nil(xml_doc, reg_field) organisation.save! end organisation.id diff --git a/spec/factories/case_log.rb b/spec/factories/case_log.rb index 45d069af5..4ea55dd40 100644 --- a/spec/factories/case_log.rb +++ b/spec/factories/case_log.rb @@ -2,6 +2,9 @@ FactoryBot.define do factory :case_log do owning_organisation { FactoryBot.create(:organisation) } managing_organisation { FactoryBot.create(:organisation) } + trait :same_landlord do + managing_organisation { owning_organisation } + end trait :about_completed do renewal { 0 } needstype { 1 } diff --git a/spec/fixtures/exports/case_logs.xml b/spec/fixtures/exports/case_logs.xml index e30083c94..72feac2f4 100644 --- a/spec/fixtures/exports/case_logs.xml +++ b/spec/fixtures/exports/case_logs.xml @@ -44,7 +44,7 @@ 0 5 1 - 1 + 3 SE26RT 6 7 diff --git a/spec/models/case_log_spec.rb b/spec/models/case_log_spec.rb index 67c8446b6..96e3be018 100644 --- a/spec/models/case_log_spec.rb +++ b/spec/models/case_log_spec.rb @@ -2,7 +2,7 @@ require "rails_helper" RSpec.describe CaseLog do let(:owning_organisation) { FactoryBot.create(:organisation) } - let(:managing_organisation) { owning_organisation } + let(:different_managing_organisation) { FactoryBot.create(:organisation) } describe "#form" do let(:case_log) { FactoryBot.build(:case_log) } @@ -33,7 +33,7 @@ RSpec.describe CaseLog do let(:case_log) do described_class.create( owning_organisation:, - managing_organisation:, + managing_organisation: owning_organisation, ) end @@ -191,11 +191,10 @@ RSpec.describe CaseLog do end describe "derived variables" do - let(:organisation) { FactoryBot.create(:organisation, provider_type: "PRP") } let!(:case_log) do described_class.create({ - managing_organisation: organisation, - owning_organisation: organisation, + managing_organisation: owning_organisation, + owning_organisation:, postcode_full: "M1 1AE", ppostcode_full: "M2 2AE", startdate: Time.gm(2021, 10, 10), @@ -235,17 +234,15 @@ RSpec.describe CaseLog do expect(record_from_db["renttype"]).to eq(3) end - context "when the owning organisation is a PRP" do - it "correctly derives and saves landlord based on owning_organisation provider_type" do + context "when the owning and managing organisations are different" do + it "correctly derives and saves landlord" do record_from_db = ActiveRecord::Base.connection.execute("select landlord from case_logs where id=#{case_log.id}").to_a[0] expect(case_log.landlord).to eq(2) expect(record_from_db["landlord"]).to eq(2) end end - context "when the owning organisation is an LA" do - let(:organisation) { FactoryBot.create(:organisation) } - + context "when the owning and managing organisations are the same" do it "correctly derives and saves landlord based on owning_organisation provider_type" do record_from_db = ActiveRecord::Base.connection.execute("select landlord from case_logs where id=#{case_log.id}").to_a[0] expect(case_log.landlord).to eq(1) @@ -255,6 +252,7 @@ RSpec.describe CaseLog do context "when deriving lettype" do context "when the owning organisation is a PRP" do + before { case_log.owning_organisation.update!(provider_type: 2) } context "when the rent type is intermediate rent and supported housing" do it "correctly derives and saves lettype" do case_log.update!(rent_type: 4, needstype: 0) @@ -1145,8 +1143,8 @@ RSpec.describe CaseLog do let!(:address_case_log) do described_class.create({ - managing_organisation: organisation, - owning_organisation: organisation, + managing_organisation: owning_organisation, + owning_organisation:, postcode_known: 1, postcode_full: "M1 1AE", }) @@ -1232,8 +1230,8 @@ RSpec.describe CaseLog do let!(:address_case_log) do described_class.create({ - managing_organisation: organisation, - owning_organisation: organisation, + managing_organisation: owning_organisation, + owning_organisation:, previous_postcode_known: 1, ppostcode_full: "M1 1AE", }) @@ -1316,8 +1314,8 @@ RSpec.describe CaseLog do context "when saving rent and charges" do let!(:case_log) do described_class.create({ - managing_organisation: organisation, - owning_organisation: organisation, + managing_organisation: owning_organisation, + owning_organisation:, brent: 5.77, scharge: 10.01, pscharge: 3, @@ -1334,8 +1332,8 @@ RSpec.describe CaseLog do context "when validating household members derived vars" do let!(:household_case_log) do described_class.create!({ - managing_organisation: organisation, - owning_organisation: organisation, + managing_organisation: owning_organisation, + owning_organisation:, hhmemb: 3, relat2: "X", relat3: "C", @@ -1387,8 +1385,8 @@ RSpec.describe CaseLog do context "when it is a renewal" do let!(:case_log) do described_class.create({ - managing_organisation: organisation, - owning_organisation: organisation, + managing_organisation: owning_organisation, + owning_organisation:, renewal: 1, startdate: Time.zone.local(2021, 4, 10), }) @@ -1431,8 +1429,8 @@ RSpec.describe CaseLog do context "when answering the household characteristics questions" do let!(:case_log) do described_class.create({ - managing_organisation: organisation, - owning_organisation: organisation, + managing_organisation: owning_organisation, + owning_organisation:, age1_known: 1, sex1: "R", relat2: "R", @@ -1450,8 +1448,8 @@ RSpec.describe CaseLog do context "when the data provider is filling in household needs" do let!(:case_log) do described_class.create({ - managing_organisation: organisation, - owning_organisation: organisation, + managing_organisation: owning_organisation, + owning_organisation:, }) end @@ -1477,8 +1475,8 @@ RSpec.describe CaseLog do context "when it is supported housing and a care home charge has been supplied" do let!(:case_log) do described_class.create({ - managing_organisation: organisation, - owning_organisation: organisation, + managing_organisation: owning_organisation, + owning_organisation:, needstype: 0, }) end @@ -1631,16 +1629,16 @@ RSpec.describe CaseLog do context "when the data provider is filling in the reason for the property being vacant" do let!(:first_let_case_log) do described_class.create({ - managing_organisation: organisation, - owning_organisation: organisation, + managing_organisation: owning_organisation, + owning_organisation:, first_time_property_let_as_social_housing: 1, }) end let!(:relet_case_log) do described_class.create({ - managing_organisation: organisation, - owning_organisation: organisation, + managing_organisation: owning_organisation, + owning_organisation:, first_time_property_let_as_social_housing: 0, }) end diff --git a/spec/models/validations/household_validations_spec.rb b/spec/models/validations/household_validations_spec.rb index 4b5cbd557..992ccf59f 100644 --- a/spec/models/validations/household_validations_spec.rb +++ b/spec/models/validations/household_validations_spec.rb @@ -4,7 +4,7 @@ RSpec.describe Validations::HouseholdValidations do subject(:household_validator) { validator_class.new } let(:validator_class) { Class.new { include Validations::HouseholdValidations } } - let(:record) { FactoryBot.create(:case_log) } + let(:record) { FactoryBot.create(:case_log, :same_landlord) } describe "reasonable preference validations" do context "when reasonable preference is homeless" do