diff --git a/app/models/derived_variables/sales_log_variables.rb b/app/models/derived_variables/sales_log_variables.rb index f662ab3df..ec8d21b30 100644 --- a/app/models/derived_variables/sales_log_variables.rb +++ b/app/models/derived_variables/sales_log_variables.rb @@ -17,6 +17,15 @@ module DerivedVariables::SalesLogVariables self.hoyear = hodate.year end self.deposit = value if outright_sale? && mortgage_not_used? + + if saledate && form.start_year_after_2024? && discounted_ownership_sale? + self.ppostcode_full = postcode_full + self.ppcodenk = 0 if postcode_full.present? + self.prevloc = la + self.is_previous_la_inferred = is_la_inferred + self.previous_la_known = la_known + end + self.pcode1, self.pcode2 = postcode_full.split if postcode_full.present? self.ppostc1, self.ppostc2 = ppostcode_full.split if ppostcode_full.present? self.totchild = total_child diff --git a/app/models/form/sales/pages/last_accommodation.rb b/app/models/form/sales/pages/last_accommodation.rb index 373a20511..2d3448399 100644 --- a/app/models/form/sales/pages/last_accommodation.rb +++ b/app/models/form/sales/pages/last_accommodation.rb @@ -10,4 +10,10 @@ class Form::Sales::Pages::LastAccommodation < ::Form::Page Form::Sales::Questions::PreviousPostcode.new(nil, nil, self), ] end + + def routed_to?(log, _user) + return false if log.form.start_year_after_2024? && log.discounted_ownership_sale? + + super + end end diff --git a/app/models/form/sales/pages/last_accommodation_la.rb b/app/models/form/sales/pages/last_accommodation_la.rb index 6e4211e12..615a30196 100644 --- a/app/models/form/sales/pages/last_accommodation_la.rb +++ b/app/models/form/sales/pages/last_accommodation_la.rb @@ -13,4 +13,10 @@ class Form::Sales::Pages::LastAccommodationLa < ::Form::Page Form::Sales::Questions::Prevloc.new(nil, nil, self), ] end + + def routed_to?(log, _user) + return false if log.form.start_year_after_2024? && log.discounted_ownership_sale? + + super + end end diff --git a/app/models/validations/sales/household_validations.rb b/app/models/validations/sales/household_validations.rb index 6e10b89b5..c23a72609 100644 --- a/app/models/validations/sales/household_validations.rb +++ b/app/models/validations/sales/household_validations.rb @@ -11,15 +11,6 @@ module Validations::Sales::HouseholdValidations shared_validate_partner_count(record, 6) end - def validate_previous_postcode(record) - return unless record.postcode_full && record.ppostcode_full && record.discounted_ownership_sale? - - unless record.postcode_full == record.ppostcode_full - record.errors.add :postcode_full, :postcodes_not_matching, message: I18n.t("validations.household.postcode.discounted_ownership") - record.errors.add :ppostcode_full, :postcodes_not_matching, message: I18n.t("validations.household.postcode.discounted_ownership") - end - end - def validate_buyers_living_in_property(record) return unless record.form.start_date.year >= 2023 diff --git a/app/models/validations/sales/property_validations.rb b/app/models/validations/sales/property_validations.rb index 3a8f6a463..fc8e4759b 100644 --- a/app/models/validations/sales/property_validations.rb +++ b/app/models/validations/sales/property_validations.rb @@ -1,5 +1,6 @@ module Validations::Sales::PropertyValidations def validate_postcodes_match_if_discounted_ownership(record) + return unless record.saledate && !record.form.start_year_after_2024? return unless record.ppostcode_full.present? && record.postcode_full.present? if record.discounted_ownership_sale? && record.ppostcode_full != record.postcode_full diff --git a/spec/models/form/sales/pages/last_accommodation_la_spec.rb b/spec/models/form/sales/pages/last_accommodation_la_spec.rb index 20daf0525..54df5a365 100644 --- a/spec/models/form/sales/pages/last_accommodation_la_spec.rb +++ b/spec/models/form/sales/pages/last_accommodation_la_spec.rb @@ -5,8 +5,22 @@ RSpec.describe Form::Sales::Pages::LastAccommodationLa, type: :model do let(:page_id) { nil } let(:page_definition) { nil } - let(:subsection) { instance_double(Form::Subsection, form: instance_double(Form, start_date:)) } + let(:subsection) { instance_double(Form::Subsection, form: instance_double(Form, depends_on_met: true)) } let(:start_date) { Time.utc(2022, 4, 1) } + let(:log) { create(:sales_log, :completed, saledate: now) } + let(:now) { Time.zone.local(2023, 4, 4) } + + before do + Timecop.freeze(now) + Singleton.__init__(FormHandler) + allow(subsection).to receive(:depends_on).and_return(nil) + allow(subsection).to receive(:enabled?).and_return(true) + end + + after do + Timecop.return + Singleton.__init__(FormHandler) + end it "has correct subsection" do expect(page.subsection).to eq(subsection) @@ -33,4 +47,23 @@ RSpec.describe Form::Sales::Pages::LastAccommodationLa, type: :model do "is_previous_la_inferred" => false, }]) end + + it "is routed to" do + log.ownershipsch = 2 + expect(page).to be_routed_to(log, nil) + end + + context "with 2024 form" do + let(:now) { Time.zone.local(2024, 4, 4) } + + it "is routed to for 2024 non discounted sale logs" do + log.update!(ownershipsch: 1) + expect(page).to be_routed_to(log, nil) + end + + it "is not routed to for 2024 discounted sale logs" do + log.update!(ownershipsch: 2) + expect(page).not_to be_routed_to(log, nil) + end + end end diff --git a/spec/models/form/sales/pages/last_accommodation_spec.rb b/spec/models/form/sales/pages/last_accommodation_spec.rb index 753aa7dc8..a972d96c5 100644 --- a/spec/models/form/sales/pages/last_accommodation_spec.rb +++ b/spec/models/form/sales/pages/last_accommodation_spec.rb @@ -3,10 +3,24 @@ require "rails_helper" RSpec.describe Form::Sales::Pages::LastAccommodation, type: :model do subject(:page) { described_class.new(page_id, page_definition, subsection) } + let(:log) { create(:sales_log, :completed, saledate: now) } + let(:now) { Time.zone.local(2023, 4, 4) } + let(:page_id) { nil } let(:page_definition) { nil } let(:subsection) { instance_double(Form::Subsection) } + before do + Timecop.freeze(now) + Singleton.__init__(FormHandler) + allow(subsection).to receive(:depends_on).and_return(nil) + end + + after do + Timecop.return + Singleton.__init__(FormHandler) + end + it "has correct subsection" do expect(page.subsection).to eq(subsection) end @@ -30,4 +44,23 @@ RSpec.describe Form::Sales::Pages::LastAccommodation, type: :model do it "has correct depends_on" do expect(page.depends_on).to be_nil end + + it "is routed to" do + log.ownershipsch = 2 + expect(page).to be_routed_to(log, nil) + end + + context "with 2024 form" do + let(:now) { Time.zone.local(2024, 4, 4) } + + it "is routed to for 2024 non discounted sale logs" do + log.update!(ownershipsch: 1) + expect(page).to be_routed_to(log, nil) + end + + it "is not routed to for 2024 discounted sale logs" do + log.update!(ownershipsch: 2) + expect(page).not_to be_routed_to(log, nil) + end + end end diff --git a/spec/models/sales_log_spec.rb b/spec/models/sales_log_spec.rb index 42aa64fec..00792b0bd 100644 --- a/spec/models/sales_log_spec.rb +++ b/spec/models/sales_log_spec.rb @@ -657,7 +657,7 @@ RSpec.describe SalesLog, type: :model do end before do - WebMock.stub_request(:get, /api.postcodes.io\/postcodes\/CA101AA/) + WebMock.stub_request(:get, /api\.postcodes\.io\/postcodes\/CA101AA/) .to_return(status: 200, body: '{"status":200,"result":{"admin_district":"Cumberland","codes":{"admin_district":"E06000064"}}}', headers: {}) Timecop.freeze(2023, 5, 1) @@ -687,7 +687,7 @@ RSpec.describe SalesLog, type: :model do end before do - WebMock.stub_request(:get, /api.postcodes.io\/postcodes\/CA101AA/) + WebMock.stub_request(:get, /api\.postcodes\.io\/postcodes\/CA101AA/) .to_return(status: 200, body: '{"status":200,"result":{"admin_district":"Eden","codes":{"admin_district":"E07000030"}}}', headers: {}) Timecop.freeze(2023, 5, 2) @@ -703,6 +703,56 @@ RSpec.describe SalesLog, type: :model do expect(address_sales_log_23_24.la).to eq("E06000064") expect(record_from_db["la"]).to eq("E06000064") end + + it "does not set previous postcode or previous la for discounted sale" do + address_sales_log_23_24.update!(ownershipsch: 2, ppostcode_full: nil, prevloc: nil) + record_from_db = described_class.find(address_sales_log_23_24.id) + expect(address_sales_log_23_24.ppostcode_full).to eq(nil) + expect(record_from_db["ppostcode_full"]).to eq(nil) + expect(record_from_db["prevloc"]).to eq(nil) + end + end + + context "with 24/25 logs" do + let(:address_sales_log_24_25) do + described_class.create({ + owning_organisation:, + created_by: created_by_user, + ppcodenk: 1, + postcode_full: "CA10 1AA", + ppostcode_full: nil, + prevloc: nil, + saledate: Time.zone.local(2024, 5, 2), + }) + end + + before do + WebMock.stub_request(:get, /api\.postcodes\.io\/postcodes\/CA101AA/) + .to_return(status: 200, body: '{"status":200,"result":{"admin_district":"Eden","codes":{"admin_district":"E07000030"}}}', headers: {}) + + Timecop.freeze(2024, 5, 2) + Singleton.__init__(FormHandler) + end + + after do + Timecop.unfreeze + end + + it "sets previous postcode for discounted sale" do + address_sales_log_24_25.update!(ownershipsch: 2, ppostcode_full: nil) + record_from_db = described_class.find(address_sales_log_24_25.id) + expect(address_sales_log_24_25.ppostcode_full).to eq("CA10 1AA") + expect(record_from_db["ppostcode_full"]).to eq("CA10 1AA") + expect(record_from_db["prevloc"]).to eq("E06000064") + end + + it "does not set previous postcode for non discounted sale" do + address_sales_log_24_25.update!(ownershipsch: 1, ppostcode_full: nil) + record_from_db = described_class.find(address_sales_log_24_25.id) + expect(address_sales_log_24_25.ppostcode_full).to eq(nil) + expect(record_from_db["ppostcode_full"]).to eq(nil) + expect(record_from_db["prevloc"]).to eq(nil) + end end it "errors if the property postcode is emptied" do diff --git a/spec/models/validations/sales/household_validations_spec.rb b/spec/models/validations/sales/household_validations_spec.rb index 898a47fcc..deddfa8ef 100644 --- a/spec/models/validations/sales/household_validations_spec.rb +++ b/spec/models/validations/sales/household_validations_spec.rb @@ -154,64 +154,6 @@ RSpec.describe Validations::Sales::HouseholdValidations do end end - describe "previous postcode validations" do - let(:record) { build(:sales_log) } - - context "with a discounted sale" do - before do - record.ownershipsch = 2 - end - - it "adds an error when previous and current postcodes are not the same" do - record.postcode_full = "SO32 3PT" - record.ppostcode_full = "DN6 7FB" - household_validator.validate_previous_postcode(record) - expect(record.errors["postcode_full"]) - .to include(match I18n.t("validations.household.postcode.discounted_ownership")) - expect(record.errors["ppostcode_full"]) - .to include(match I18n.t("validations.household.postcode.discounted_ownership")) - end - - it "allows same postcodes" do - record.postcode_full = "SO32 3PT" - record.ppostcode_full = "SO32 3PT" - household_validator.validate_previous_postcode(record) - expect(record.errors["postcode_full"]).to be_empty - expect(record.errors["ppostcode_full"]).to be_empty - end - - it "does not add an error when postcode is missing" do - record.postcode_full = nil - record.ppostcode_full = "SO32 3PT" - household_validator.validate_previous_postcode(record) - expect(record.errors["postcode_full"]).to be_empty - expect(record.errors["ppostcode_full"]).to be_empty - end - - it "does not add an error when previous postcode is missing" do - record.postcode_full = "SO32 3PT" - record.ppostcode_full = nil - household_validator.validate_previous_postcode(record) - expect(record.errors["postcode_full"]).to be_empty - expect(record.errors["ppostcode_full"]).to be_empty - end - end - - context "without a discounted sale" do - before do - record.ownershipsch = 1 - end - - it "allows different postcodes" do - record.postcode_full = "SO32 3PT" - record.ppostcode_full = "DN6 7FB" - household_validator.validate_previous_postcode(record) - expect(record.errors["postcode_full"]).to be_empty - expect(record.errors["ppostcode_full"]).to be_empty - end - end - end - describe "validating fields about buyers living in the property" do let(:sales_log) { FactoryBot.create(:sales_log, :outright_sale_setup_complete, noint: 1, companybuy: 2, buylivein:, jointpur:, jointmore:, buy1livein:) } diff --git a/spec/models/validations/sales/property_validations_spec.rb b/spec/models/validations/sales/property_validations_spec.rb index 0152428d8..f84f65966 100644 --- a/spec/models/validations/sales/property_validations_spec.rb +++ b/spec/models/validations/sales/property_validations_spec.rb @@ -19,7 +19,7 @@ RSpec.describe Validations::Sales::PropertyValidations do end context "when ownership scheme is discounted ownership" do - let(:record) { build(:sales_log, ownershipsch: 2) } + let(:record) { build(:sales_log, ownershipsch: 2, saledate: Time.zone.local(2023, 4, 5)) } it "when ppostcode_full is not present no error is added" do record.postcode_full = "SW1A 1AA" @@ -54,6 +54,16 @@ RSpec.describe Validations::Sales::PropertyValidations do expect(record.errors["ppostcode_full"]).to include(match I18n.t("validations.property.postcode.must_match_previous")) expect(record.errors["ownershipsch"]).to include(match I18n.t("validations.property.postcode.must_match_previous")) end + + it "does not add error for 2024 log" do + record.postcode_full = "SW1A 1AA" + record.ppostcode_full = "SW1A 0AA" + record.saledate = Time.zone.local(2024, 4, 5) + property_validator.validate_postcodes_match_if_discounted_ownership(record) + expect(record.errors["postcode_full"]).to be_empty + expect(record.errors["ppostcode_full"]).to be_empty + expect(record.errors["ownershipsch"]).to be_empty + end end end