diff --git a/app/models/derived_variables/lettings_log_variables.rb b/app/models/derived_variables/lettings_log_variables.rb index 9219392f7..4692a0e6b 100644 --- a/app/models/derived_variables/lettings_log_variables.rb +++ b/app/models/derived_variables/lettings_log_variables.rb @@ -124,6 +124,11 @@ module DerivedVariables::LettingsLogVariables self.nationality_all = nationality_all_group if nationality_uk_or_prefers_not_to_say? + if startdate_changed? && !LocalAuthority.active(startdate).where(code: la).exists? + self.la = nil + self.is_la_inferred = false + end + reset_address_fields! if is_supported_housing? end diff --git a/app/models/derived_variables/sales_log_variables.rb b/app/models/derived_variables/sales_log_variables.rb index a1462454c..0c13d4fdf 100644 --- a/app/models/derived_variables/sales_log_variables.rb +++ b/app/models/derived_variables/sales_log_variables.rb @@ -75,6 +75,11 @@ module DerivedVariables::SalesLogVariables self.nationality_all = nationality_all_group if nationality_uk_or_prefers_not_to_say? self.nationality_all_buyer2 = nationality_all_buyer2_group if nationality2_uk_or_prefers_not_to_say? + if saledate_changed? && !LocalAuthority.active(saledate).where(code: la).exists? + self.la = nil + self.is_la_inferred = false + end + set_encoded_derived_values!(DEPENDENCIES) end diff --git a/spec/models/lettings_log_spec.rb b/spec/models/lettings_log_spec.rb index c47de8cf6..5702e65c8 100644 --- a/spec/models/lettings_log_spec.rb +++ b/spec/models/lettings_log_spec.rb @@ -809,6 +809,21 @@ RSpec.describe LettingsLog do expect { lettings_log.update!(nationality_all_group: nil, declaration: 1) }.not_to change(lettings_log, :nationality_all) end end + + context "when form year changes and LA is no longer active" do + before do + LocalAuthority.find_by(code: "E08000003").update!(end_date: Time.zone.today) + end + + it "removes the LA" do + lettings_log.update!(startdate: Time.zone.yesterday, la: "E08000003") + expect(lettings_log.reload.la).to eq("E08000003") + + lettings_log.update!(startdate: Time.zone.tomorrow) + expect(lettings_log.reload.la).to eq(nil) + expect(lettings_log.reload.is_la_inferred).to eq(false) + end + end end describe "optional fields" do diff --git a/spec/models/sales_log_spec.rb b/spec/models/sales_log_spec.rb index ae9b00d4c..f3dea90f9 100644 --- a/spec/models/sales_log_spec.rb +++ b/spec/models/sales_log_spec.rb @@ -978,5 +978,22 @@ RSpec.describe SalesLog, type: :model do end end end + + context "when form year changes and LA is no longer active" do + let!(:sales_log) { create(:sales_log) } + + before do + LocalAuthority.find_by(code: "E08000003").update!(end_date: Time.zone.today) + end + + it "removes the LA" do + sales_log.update!(saledate: Time.zone.yesterday, la: "E08000003") + expect(sales_log.reload.la).to eq("E08000003") + + sales_log.update!(saledate: Time.zone.tomorrow) + expect(sales_log.reload.la).to eq(nil) + expect(sales_log.reload.is_la_inferred).to eq(false) + end + end end # rubocop:enable RSpec/MessageChain