diff --git a/app/models/derived_variables/sales_log_variables.rb b/app/models/derived_variables/sales_log_variables.rb index 12d860492..f662ab3df 100644 --- a/app/models/derived_variables/sales_log_variables.rb +++ b/app/models/derived_variables/sales_log_variables.rb @@ -24,6 +24,10 @@ module DerivedVariables::SalesLogVariables self.hhmemb = number_of_household_members self.hhtype = household_type + if saledate && form.start_year_after_2024? + self.soctenant = soctenant_from_prevten_values + end + self.uprn_known = 0 if address_answered_without_uprn? if uprn_known&.zero? @@ -157,4 +161,14 @@ private def address_answered_without_uprn? [address_line1, town_or_city].all?(&:present?) && uprn.nil? && form.start_date.year >= 2023 end + + def soctenant_from_prevten_values + return unless prevten && shared_ownership_scheme? + + prevten_was_social_housing? ? 1 : 2 + end + + def prevten_was_social_housing? + [1, 2].include?(prevten) || [1, 2].include?(prevtenbuy2) + end end diff --git a/app/models/form/sales/pages/buyer_previous.rb b/app/models/form/sales/pages/buyer_previous.rb index ed28cea00..3a4618d8f 100644 --- a/app/models/form/sales/pages/buyer_previous.rb +++ b/app/models/form/sales/pages/buyer_previous.rb @@ -2,7 +2,7 @@ class Form::Sales::Pages::BuyerPrevious < ::Form::Page def initialize(id, hsh, subsection, joint_purchase:) super(id, hsh, subsection) @joint_purchase = joint_purchase - @depends_on = [{ "joint_purchase?" => joint_purchase }] + @depends_on = [{ "joint_purchase?" => joint_purchase, "soctenant_is_inferred?" => false }] end def questions diff --git a/app/models/form/sales/questions/buyer_previous.rb b/app/models/form/sales/questions/buyer_previous.rb index aaea81cfc..cc16ce107 100644 --- a/app/models/form/sales/questions/buyer_previous.rb +++ b/app/models/form/sales/questions/buyer_previous.rb @@ -21,4 +21,8 @@ class Form::Sales::Questions::BuyerPrevious < ::Form::Question "2" => { "value" => "No" }, } end + + def derived? + form.start_year_after_2024? + end end diff --git a/app/models/sales_log.rb b/app/models/sales_log.rb index c0202c1df..3e15c832d 100644 --- a/app/models/sales_log.rb +++ b/app/models/sales_log.rb @@ -470,6 +470,10 @@ class SalesLog < Log form.start_date.year >= 2023 && uprn.present? ? "uprn" : nil].compact end + def soctenant_is_inferred? + form.start_year_after_2024? + end + def duplicates SalesLog.where.not(duplicate_set_id: nil).where(duplicate_set_id:).where.not(id:) end diff --git a/app/services/bulk_upload/sales/year2024/row_parser.rb b/app/services/bulk_upload/sales/year2024/row_parser.rb index 037fb8eef..28e7ef100 100644 --- a/app/services/bulk_upload/sales/year2024/row_parser.rb +++ b/app/services/bulk_upload/sales/year2024/row_parser.rb @@ -717,7 +717,6 @@ private lanomagr: %i[field_97], frombeds: %i[field_98], fromprop: %i[field_99], - soctenant: %i[field_98 field_99 field_100], value: %i[field_101 field_114 field_125], equity: %i[field_102], mortgage: %i[field_104 field_118 field_127], @@ -927,7 +926,7 @@ private attributes["stairbought"] = field_87 attributes["stairowned"] = field_88 attributes["socprevten"] = field_100 - attributes["soctenant"] = [attributes["socprevten"], attributes["frombeds"], attributes["fromprop"]].any?(&:present?) ? 1 : 0 + attributes["soctenant"] = infer_soctenant_from_prevten_and_prevtenbuy2 attributes["mortgageused"] = mortgageused attributes["uprn"] = field_22 @@ -1123,6 +1122,16 @@ private 0 if field_62 == 1 end + def infer_soctenant_from_prevten_and_prevtenbuy2 + return unless shared_ownership? + + if [1, 2].include?(field_61) || [1, 2].include?(field_71.to_i) + 1 + else + 2 + end + end + def block_log_creation! self.block_log_creation = true end diff --git a/spec/models/form/sales/pages/buyer_previous_spec.rb b/spec/models/form/sales/pages/buyer_previous_spec.rb index 4d7df21a6..188fac7a4 100644 --- a/spec/models/form/sales/pages/buyer_previous_spec.rb +++ b/spec/models/form/sales/pages/buyer_previous_spec.rb @@ -42,13 +42,13 @@ RSpec.describe Form::Sales::Pages::BuyerPrevious, type: :model do let(:joint_purchase) { true } it "has the correct depends on" do - expect(page.depends_on).to eq([{ "joint_purchase?" => true }]) + expect(page.depends_on).to eq([{ "joint_purchase?" => true, "soctenant_is_inferred?" => false }]) end end context "when sales is not a joint purchase" do it "has the correct depends on" do - expect(page.depends_on).to eq([{ "joint_purchase?" => false }]) + expect(page.depends_on).to eq([{ "joint_purchase?" => false, "soctenant_is_inferred?" => false }]) end end diff --git a/spec/models/form/sales/questions/buyer_previous_spec.rb b/spec/models/form/sales/questions/buyer_previous_spec.rb index 06aaca38f..e52a514d3 100644 --- a/spec/models/form/sales/questions/buyer_previous_spec.rb +++ b/spec/models/form/sales/questions/buyer_previous_spec.rb @@ -6,8 +6,15 @@ RSpec.describe Form::Sales::Questions::BuyerPrevious, type: :model do let(:question_id) { nil } let(:question_definition) { nil } let(:page) { instance_double(Form::Page) } + let(:subsection) { instance_double(Form::Subsection) } + let(:form) { instance_double(Form) } let(:joint_purchase) { true } + before do + allow(page).to receive(:subsection).and_return(subsection) + allow(subsection).to receive(:form).and_return(form) + end + it "has correct page" do expect(question.page).to eq(page) end @@ -42,10 +49,6 @@ RSpec.describe Form::Sales::Questions::BuyerPrevious, type: :model do expect(question.type).to eq("radio") end - it "is not marked as derived" do - expect(question.derived?).to be false - end - it "has the correct displayed_answer_options" do expect(question.displayed_answer_options(nil)).to eq({ "1" => { "value" => "Yes" }, @@ -68,4 +71,24 @@ RSpec.describe Form::Sales::Questions::BuyerPrevious, type: :model do it "has the correct hint" do expect(question.hint_text).to eq(nil) end + + context "when form year is before 2024" do + before do + allow(form).to receive(:start_year_after_2024?).and_return(false) + end + + it "is not marked as derived" do + expect(question.derived?).to be false + end + end + + context "when form year is >= 2024" do + before do + allow(form).to receive(:start_year_after_2024?).and_return(true) + end + + it "is marked as derived" do + expect(question.derived?).to be true + end + end end diff --git a/spec/services/bulk_upload/sales/year2024/row_parser_spec.rb b/spec/services/bulk_upload/sales/year2024/row_parser_spec.rb index 473967791..93e6fdca1 100644 --- a/spec/services/bulk_upload/sales/year2024/row_parser_spec.rb +++ b/spec/services/bulk_upload/sales/year2024/row_parser_spec.rb @@ -1264,10 +1264,56 @@ RSpec.describe BulkUpload::Sales::Year2024::RowParser do end describe "#soctenant" do - let(:attributes) { setup_section_params.merge({ field_99: "1" }) } + context "when discounted ownership" do + let(:attributes) { valid_attributes.merge({ field_8: "2" }) } - it "is correctly set" do - expect(parser.log.soctenant).to be(1) + it "is set to nil" do + expect(parser.log.soctenant).to be_nil + end + end + + context "when outright sale" do + let(:attributes) { valid_attributes.merge({ field_8: "3" }) } + + it "is set to nil" do + expect(parser.log.soctenant).to be_nil + end + end + + context "when shared ownership" do + context "when prevten is a social housing type" do + let(:attributes) { valid_attributes.merge({ field_8: "1", field_61: "1" }) } + + it "is set to yes" do + expect(parser.log.soctenant).to be(1) + end + end + + context "when prevten is not a social housing type" do + context "and prevtenbuy2 is a social housing type" do + let(:attributes) { valid_attributes.merge({ field_8: "1", field_61: "3", field_71: "2" }) } + + it "is set to yes" do + expect(parser.log.soctenant).to be(1) + end + end + + context "and prevtenbuy2 is not a social housing type" do + let(:attributes) { valid_attributes.merge({ field_8: "1", field_61: "3", field_71: "4" }) } + + it "is set to no" do + expect(parser.log.soctenant).to be(2) + end + end + + context "and prevtenbuy2 is blank" do + let(:attributes) { valid_attributes.merge({ field_8: "1", field_61: "3", field_71: nil }) } + + it "is set to no" do + expect(parser.log.soctenant).to be(2) + end + end + end end end