diff --git a/app/models/derived_variables/lettings_log_variables.rb b/app/models/derived_variables/lettings_log_variables.rb index aae49422d..861b99300 100644 --- a/app/models/derived_variables/lettings_log_variables.rb +++ b/app/models/derived_variables/lettings_log_variables.rb @@ -1,4 +1,6 @@ module DerivedVariables::LettingsLogVariables + include DerivedVariables::SharedLogic + # renttype and unitletas values are different for intermediate rent (3 for renttype and 4 for unitletas) RENT_TYPE_MAPPING = { 0 => 1, # "Social Rent" => "Social Rent" @@ -35,10 +37,12 @@ module DerivedVariables::LettingsLogVariables end def set_derived_fields! + clear_inapplicable_derived_values + set_encoded_derived_values!(DEPENDENCIES) + if rsnvac.present? self.newprop = has_first_let_vacancy_reason? ? 1 : 2 end - self.incref = 1 if net_income_refused? self.renttype = RENT_TYPE_MAPPING[rent_type] self.lettype = get_lettype self.totchild = get_totchild @@ -69,15 +73,9 @@ module DerivedVariables::LettingsLogVariables self.nocharge = household_charge&.zero? ? 1 : 0 if is_renewal? self.underoccupation_benefitcap = 2 if collection_start_year == 2021 - self.referral = 1 - self.waityear = 2 - self.offered = 0 self.voiddate = startdate - self.first_time_property_let_as_social_housing = 0 - self.rsnvac = 14 self.unitletas = form.start_date.year >= 2023 ? UNITLETAS_MAPPING_23_24[rent_type] : UNITLETAS_MAPPING[rent_type] if is_general_needs? - # fixed term self.prevten = 32 if managing_organisation&.provider_type == "PRP" self.prevten = 30 if managing_organisation&.provider_type == "LA" end @@ -109,6 +107,49 @@ module DerivedVariables::LettingsLogVariables private + DEPENDENCIES = [ + { + conditions: { + renewal: 1, + }, + derived_values: { + referral: 1, + waityear: 2, + offered: 0, + rsnvac: 14, + first_time_property_let_as_social_housing: 0, + }, + }, + { + conditions: { + net_income_known: 2, + }, + derived_values: { + incref: 1, + }, + }, + ].freeze + + def clear_inapplicable_derived_values + reset_invalidated_derived_values!(DEPENDENCIES) + if (startdate_changed? || renewal_changed?) && (renewal_was == 1 && startdate_was&.between?(Time.zone.local(2021, 4, 1), Time.zone.local(2022, 3, 31))) + self.underoccupation_benefitcap = nil + end + if renewal_changed? && renewal_was == 1 + self.voiddate = nil + self.unitletas = nil + end + if %w[PRP LA].include?(managing_organisation&.provider_type) && + (needstype_changed? || renewal_changed?) && + needstype_was == 1 && renewal_was == 1 + self.prevten = nil + end + if needstype_changed? && needstype_was == 2 + self.wchair = nil + self.location_id = nil + end + end + def get_totelder ages = [age1, age2, age3, age4, age5, age6, age7, age8] ages.count { |x| !x.nil? && x >= 60 } diff --git a/app/models/derived_variables/shared_logic.rb b/app/models/derived_variables/shared_logic.rb index ce265c276..c326cb8c9 100644 --- a/app/models/derived_variables/shared_logic.rb +++ b/app/models/derived_variables/shared_logic.rb @@ -1,5 +1,5 @@ module DerivedVariables::SharedLogic - private +private def reset_invalidated_derived_values!(dependencies) dependencies.each do |dependency| @@ -24,4 +24,4 @@ module DerivedVariables::SharedLogic end end end -end \ No newline at end of file +end diff --git a/app/models/lettings_log.rb b/app/models/lettings_log.rb index 2d91e2ad9..6fce43f42 100644 --- a/app/models/lettings_log.rb +++ b/app/models/lettings_log.rb @@ -531,24 +531,6 @@ class LettingsLog < Log private - def reset_derived_questions - dependent_questions = { waityear: [{ key: :renewal, value: 0 }], - referral: [{ key: :renewal, value: 0 }], - rsnvac: [{ key: :renewal, value: 0 }], - underoccupation_benefitcap: [{ key: :renewal, value: 0 }], - wchair: [{ key: :needstype, value: 1 }], - location_id: [{ key: :needstype, value: 1 }] } - - dependent_questions.each do |dependent, conditions| - condition_key = conditions.first[:key] - condition_value = conditions.first[:value] - if public_send("#{condition_key}_changed?") && condition_value == public_send(condition_key) && !public_send("#{dependent}_changed?") - Rails.logger.debug("Cleared derived #{dependent} value") - self[dependent] = nil - end - end - end - def reset_invalid_unresolved_log_fields! return unless unresolved? @@ -581,7 +563,6 @@ private reset_invalid_unresolved_log_fields! reset_scheme - reset_derived_questions end def dynamically_not_required diff --git a/spec/models/lettings_log_spec.rb b/spec/models/lettings_log_spec.rb index 7d0799240..22585ca30 100644 --- a/spec/models/lettings_log_spec.rb +++ b/spec/models/lettings_log_spec.rb @@ -1578,7 +1578,8 @@ RSpec.describe LettingsLog do it "clears underoccupation_benefitcap if log is no longer in 2021/22" do lettings_log.update!(renewal: 1) expect(lettings_log.underoccupation_benefitcap).to be 2 - lettings_log.update!(startdate: Time.zone.now) + Timecop.return + lettings_log.update!(startdate: Time.zone.local(2023, 1, 1)) expect(lettings_log.underoccupation_benefitcap).to be nil end @@ -1591,7 +1592,6 @@ RSpec.describe LettingsLog do it "correctly derives prevten" do expect(lettings_log.prevten).to be 32 - end it "clears prevten if the log is marked as supported housing" do @@ -1613,7 +1613,6 @@ RSpec.describe LettingsLog do it "correctly derives prevten" do expect(lettings_log.prevten).to be 30 - end it "clears prevten if the log is marked as supported housing" do