diff --git a/app/models/derived_variables/sales_log_variables.rb b/app/models/derived_variables/sales_log_variables.rb index a4817a230..668969269 100644 --- a/app/models/derived_variables/sales_log_variables.rb +++ b/app/models/derived_variables/sales_log_variables.rb @@ -36,6 +36,7 @@ module DerivedVariables::SalesLogVariables if saledate && form.start_year_after_2024? self.soctenant = soctenant_from_prevten_values + child_under_16_constraints! end self.uprn_known = 0 if address_answered_without_uprn? @@ -124,6 +125,17 @@ private end end + def child_under_16_constraints! + start_index = joint_purchase? ? 3 : 2 + (start_index..6).each do |idx| + if age_under_16?(idx) + self["ecstat#{idx}"] = 9 + elsif public_send("ecstat#{idx}") == 9 && age_known?(idx) + self["ecstat#{idx}"] = nil + end + end + end + def household_type return unless total_elder && total_adult && totchild diff --git a/app/models/form/lettings/questions/person_working_situation.rb b/app/models/form/lettings/questions/person_working_situation.rb index 8541d096a..38fb820ef 100644 --- a/app/models/form/lettings/questions/person_working_situation.rb +++ b/app/models/form/lettings/questions/person_working_situation.rb @@ -7,31 +7,31 @@ class Form::Lettings::Questions::PersonWorkingSituation < ::Form::Question @type = "radio" @check_answers_card_number = person_index @hint_text = "" - @answer_options = ANSWER_OPTIONS @person_index = person_index @question_number = question_number + @answer_options = answer_options end - ANSWER_OPTIONS = { - "1" => { "value" => "Full-time – 30 hours or more" }, - "2" => { "value" => "Part-time – Less than 30 hours" }, - "7" => { "value" => "Full-time student" }, - "3" => { "value" => "In government training into work, such as New Deal" }, - "4" => { "value" => "Jobseeker" }, - "6" => { "value" => "Not seeking work" }, - "8" => { "value" => "Unable to work because of long term sick or disability" }, - "5" => { "value" => "Retired" }, - "9" => { - "value" => "Child under 16", - "depends_on" => [ - { "age2_known" => 1 }, - { "age2" => { "operator" => "<", "operand" => 16 } }, - ], - }, - "0" => { "value" => "Other" }, - "divider" => { "value" => true }, - "10" => { "value" => "Person prefers not to say" }, - }.freeze + def answer_options + { "1" => { "value" => "Full-time – 30 hours or more" }, + "2" => { "value" => "Part-time – Less than 30 hours" }, + "7" => { "value" => "Full-time student" }, + "3" => { "value" => "In government training into work, such as New Deal" }, + "4" => { "value" => "Jobseeker" }, + "6" => { "value" => "Not seeking work" }, + "8" => { "value" => "Unable to work because of long term sick or disability" }, + "5" => { "value" => "Retired" }, + "9" => { + "value" => "Child under 16", + "depends_on" => [ + { "age#{@person_index}_known" => 1 }, + { "age#{@person_index}" => { "operator" => "<", "operand" => 16 } }, + ], + }, + "0" => { "value" => "Other" }, + "divider" => { "value" => true }, + "10" => { "value" => "Person prefers not to say" } } + end def question_number base_question_number = case form.start_date.year diff --git a/app/models/form/sales/questions/person_working_situation.rb b/app/models/form/sales/questions/person_working_situation.rb index e73f7debd..40adc317d 100644 --- a/app/models/form/sales/questions/person_working_situation.rb +++ b/app/models/form/sales/questions/person_working_situation.rb @@ -4,7 +4,6 @@ class Form::Sales::Questions::PersonWorkingSituation < ::Form::Question @check_answer_label = "Person #{person_index}’s working situation" @header = "Which of these best describes Person #{person_index}’s working situation?" @type = "radio" - @answer_options = ANSWER_OPTIONS @check_answers_card_number = person_index @inferred_check_answers_value = [{ "condition" => { @@ -14,21 +13,31 @@ class Form::Sales::Questions::PersonWorkingSituation < ::Form::Question }] @person_index = person_index @question_number = question_number + @answer_options = answer_options end - ANSWER_OPTIONS = { - "1" => { "value" => "Full-time - 30 hours or more" }, - "2" => { "value" => "Part-time - Less than 30 hours" }, - "3" => { "value" => "In government training into work, such as New Deal" }, - "4" => { "value" => "Jobseeker" }, - "6" => { "value" => "Not seeking work" }, - "8" => { "value" => "Unable to work due to long term sick or disability" }, - "5" => { "value" => "Retired" }, - "0" => { "value" => "Other" }, - "10" => { "value" => "Person prefers not to say" }, - "7" => { "value" => "Full-time student" }, - "9" => { "value" => "Child under 16" }, - }.freeze + def answer_options + { + "1" => { "value" => "Full-time - 30 hours or more" }, + "2" => { "value" => "Part-time - Less than 30 hours" }, + "3" => { "value" => "In government training into work, such as New Deal" }, + "4" => { "value" => "Jobseeker" }, + "6" => { "value" => "Not seeking work" }, + "8" => { "value" => "Unable to work due to long term sick or disability" }, + "5" => { "value" => "Retired" }, + "0" => { "value" => "Other" }, + "10" => { "value" => "Person prefers not to say" }, + "7" => { "value" => "Full-time student" }, + "9" => { + "value" => "Child under 16", + "depends_on" => [ + { "saledate" => { "operator" => "<", "operand" => Time.zone.local(2024, 4, 1) } }, + { "age#{@person_index}_known" => 1 }, + { "age#{@person_index}" => { "operator" => "<", "operand" => 16 } }, + ], + }, + } + end def question_number base_question_number = case form.start_date.year diff --git a/app/models/lettings_log.rb b/app/models/lettings_log.rb index 5d56e53c7..7dcebd226 100644 --- a/app/models/lettings_log.rb +++ b/app/models/lettings_log.rb @@ -556,12 +556,6 @@ class LettingsLog < Log RETIREMENT_AGES[gender] end - def age_known?(person_num) - return false unless person_num.is_a?(Integer) - - !!public_send("age#{person_num}_known")&.zero? - end - def age_unknown?(person_num) return false unless person_num.is_a?(Integer) @@ -752,10 +746,6 @@ private collection_start_year >= 2022 && !is_fixed_term_tenancy? end - def age_under_16?(person_num) - public_send("age#{person_num}") && public_send("age#{person_num}") < 16 - end - def process_postcode_changes! self.postcode_full = upcase_and_remove_whitespace(postcode_full) return if postcode_full.blank? diff --git a/app/models/log.rb b/app/models/log.rb index 8847812d3..0d898827a 100644 --- a/app/models/log.rb +++ b/app/models/log.rb @@ -277,6 +277,16 @@ class Log < ApplicationRecord nationality_all_group&.zero? || nationality_all_group == 826 end + def age_under_16?(person_num) + public_send("age#{person_num}") && public_send("age#{person_num}") < 16 + end + + def age_known?(person_num) + return false unless person_num.is_a?(Integer) + + !!public_send("age#{person_num}_known")&.zero? + end + private # Handle logs that are older than previous collection start date diff --git a/spec/models/form/sales/questions/person_working_situation_spec.rb b/spec/models/form/sales/questions/person_working_situation_spec.rb index 77ec1267b..41b88223e 100644 --- a/spec/models/form/sales/questions/person_working_situation_spec.rb +++ b/spec/models/form/sales/questions/person_working_situation_spec.rb @@ -32,7 +32,11 @@ RSpec.describe Form::Sales::Questions::PersonWorkingSituation, type: :model do "0" => { "value" => "Other" }, "10" => { "value" => "Person prefers not to say" }, "7" => { "value" => "Full-time student" }, - "9" => { "value" => "Child under 16" }, + "9" => { "value" => "Child under 16", + "depends_on" => + [{ "saledate" => { "operator" => "<", "operand" => Time.zone.local(2024, 4, 1) } }, + { "age2_known" => 1 }, + { "age2" => { "operator" => "<", "operand" => 16 } }] }, }) end diff --git a/spec/models/sales_log_spec.rb b/spec/models/sales_log_spec.rb index 18b5dc5e8..d2678ebe5 100644 --- a/spec/models/sales_log_spec.rb +++ b/spec/models/sales_log_spec.rb @@ -816,6 +816,31 @@ RSpec.describe SalesLog, type: :model do expect(record_from_db["ppostcode_full"]).to eq(nil) expect(record_from_db["prevloc"]).to eq(nil) end + + context "when validating household members derived vars" do + let!(:household_sales_log) do + create( + :sales_log, + :completed, + managing_organisation: owning_organisation, + owning_organisation:, + created_by: created_by_user, + age6: 14, + saledate: Time.zone.local(2024, 5, 2), + ) + end + + it "correctly derives economic status for tenants under 16" do + record_from_db = described_class.find(household_sales_log.id) + expect(record_from_db["ecstat6"]).to eq(9) + end + + it "correctly resets economic status when age changes from under 16" do + household_sales_log.update!(age6_known: 0, age6: 17) + record_from_db = described_class.find(household_sales_log.id) + expect(record_from_db["ecstat6"]).to eq(nil) + end + end end it "errors if the property postcode is emptied" do