diff --git a/app/models/derived_variables/sales_log_variables.rb b/app/models/derived_variables/sales_log_variables.rb index 5b83be8a8..7ed399fcb 100644 --- a/app/models/derived_variables/sales_log_variables.rb +++ b/app/models/derived_variables/sales_log_variables.rb @@ -17,7 +17,7 @@ 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? 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/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