diff --git a/app/models/derived_variables/lettings_log_variables.rb b/app/models/derived_variables/lettings_log_variables.rb index ac17e0219..ac8776ca0 100644 --- a/app/models/derived_variables/lettings_log_variables.rb +++ b/app/models/derived_variables/lettings_log_variables.rb @@ -91,8 +91,9 @@ module DerivedVariables::LettingsLogVariables self.prevten = 30 if owning_organisation&.provider_type == "LA" end end - if form.start_year_after_2024? && is_bedsit? - self.beds = 1 + if form.start_year_after_2024? + self.beds = 1 if is_bedsit? + self.preg_occ = 2 if preg_occ.nil? && hhmemb.present? && (all_male_household? || no_women_or_non_binary_between_11_and_60?) end child_under_16_constraints! diff --git a/app/models/lettings_log.rb b/app/models/lettings_log.rb index cc8c7d58b..710757340 100644 --- a/app/models/lettings_log.rb +++ b/app/models/lettings_log.rb @@ -664,6 +664,21 @@ class LettingsLog < Log LettingsLog.where.not(duplicate_set_id: nil).where(duplicate_set_id:).where.not(id:) end + def all_male_household? + (1..hhmemb).all? do |n| + public_send("sex#{n}") == "M" + end + end + + def no_women_or_non_binary_between_11_and_60? + (1..hhmemb).none? do |n| + sex = public_send("sex#{n}") + age = public_send("age#{n}") + + %w[F X].include?(sex) && age.present? && age.between?(11, 60) + end + end + private def reset_invalid_unresolved_log_fields! diff --git a/spec/models/lettings_log_spec.rb b/spec/models/lettings_log_spec.rb index 5c55d87cb..ee708be89 100644 --- a/spec/models/lettings_log_spec.rb +++ b/spec/models/lettings_log_spec.rb @@ -2367,6 +2367,57 @@ 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 inferring pregnancy" do + around do |example| + Timecop.freeze(now) do + Singleton.__init__(FormHandler) + example.run + end + end + + context "when before 2024/25" do + let(:now) { Time.zone.local(2024, 1, 1) } + let!(:lettings_log) { create(:lettings_log, :setup_completed, startdate: now, preg_occ: nil, declaration: 1, hhmemb: 3) } + + it "does not infer pregnancy for all male households" do + lettings_log.update!(sex1: "M", sex2: "M", sex3: "M") + expect(lettings_log.preg_occ).to be_nil + end + + it "does not infer pregnancy when no woman or non-binary person in the household is 11 to 60" do + lettings_log.update!(sex1: "F", sex2: "X", sex3: "M", age1: 61, age2: 10, age3: 55) + expect(lettings_log.preg_occ).to be_nil + end + end + + context "when in 2024/25" do + let(:now) { Time.zone.local(2024, 5, 1) } + let!(:lettings_log) { create(:lettings_log, :setup_completed, startdate: now, preg_occ: nil, declaration: 1, hhmemb: 3) } + + it "infers no pregnancy for all male households" do + lettings_log.update!(sex1: "M", sex2: "M", sex3: "M") + expect(lettings_log.preg_occ).to eq(2) + end + + it "infers no pregnancy when no woman or non-binary person in the household is 11 to 60" do + lettings_log.update!(sex1: "F", sex2: "X", sex3: "M", age1: 61, age2: 10, age3: 55) + expect(lettings_log.preg_occ).to eq(2) + end + + it "does not override user entered data when household is all male" do + lettings_log.update!(preg_occ: 1) + lettings_log.update!(sex1: "M", sex2: "M", sex3: "M") + expect(lettings_log.preg_occ).to eq(1) + end + + it "does not override user entered data when no woman or non-binary person in the household is 11 to 60" do + lettings_log.update!(preg_occ: 1) + lettings_log.update!(sex1: "F", sex2: "X", sex3: "M", age1: 61, age2: 10, age3: 55) + expect(lettings_log.preg_occ).to eq(1) + end + end + end end describe "optional fields" do