From cee6906c06c42a0e9fb313db3b2fc27c16133e99 Mon Sep 17 00:00:00 2001 From: Kat Date: Fri, 13 Jan 2023 09:41:06 +0000 Subject: [PATCH] Derive household variables for sales logs --- .../derived_variables/sales_log_variables.rb | 69 +++++++++++++++++++ spec/models/sales_log_spec.rb | 38 ++++++++++ 2 files changed, 107 insertions(+) diff --git a/app/models/derived_variables/sales_log_variables.rb b/app/models/derived_variables/sales_log_variables.rb index a527ed3f0..67b4fc6ac 100644 --- a/app/models/derived_variables/sales_log_variables.rb +++ b/app/models/derived_variables/sales_log_variables.rb @@ -16,5 +16,74 @@ module DerivedVariables::SalesLogVariables self.mscharge = 0 end self.pcode1, self.pcode2 = postcode_full.split(" ") if postcode_full.present? + self.totchild = total_child + self.totadult = total_adult + total_elder + self.hhmemb = totchild + totadult + self.hhtype = household_type + end + +private + + def total_elder + ages = [age1, age2, age3, age4, age5, age6] + ages.count { |x| !x.nil? && x >= 60 } + end + + def total_child + relationships = [relat2, relat3, relat4, relat5, relat6] + relationships.count("C") + end + + def total_adult + total = !age1.nil? && age1 >= 16 && age1 < 60 ? 1 : 0 + total + (2..6).count do |i| + age = public_send("age#{i}") + relat = public_send("relat#{i}") + !age.nil? && ((age >= 16 && age < 18 && %w[P X].include?(relat)) || age >= 18 && age < 60) + end + end + + def household_type + return unless total_elder && total_adult && totchild + + if only_one_elder? + 1 + elsif two_adults_including_elders? + 2 + elsif only_one_adult? + 3 + elsif only_two_adults? + 4 + elsif one_adult_with_at_least_one_child? + 5 + elsif two_adults_with_at_least_one_child? + 6 + else + 9 + end + end + + def two_adults_with_at_least_one_child? + total_elder.zero? && total_adult >= 2 && totchild >= 1 + end + + def one_adult_with_at_least_one_child? + total_elder.zero? && total_adult == 1 && totchild >= 1 + end + + def only_two_adults? + total_elder.zero? && total_adult == 2 && totchild.zero? + end + + def only_one_adult? + total_elder.zero? && total_adult == 1 && totchild.zero? + end + + def two_adults_including_elders? + (total_elder + total_adult) == 2 && total_elder >= 1 + end + + def only_one_elder? + total_elder == 1 && total_adult.zero? && totchild.zero? end end diff --git a/spec/models/sales_log_spec.rb b/spec/models/sales_log_spec.rb index bad72db3b..44444811d 100644 --- a/spec/models/sales_log_spec.rb +++ b/spec/models/sales_log_spec.rb @@ -232,6 +232,44 @@ RSpec.describe SalesLog, type: :model do end end + context "when deriving household variables" do + let!(:household_lettings_log) do + described_class.create!({ + jointpur: 1, + hholdcount: 3, + relat2: "X", + relat3: "C", + relat4: "X", + relat5: "C", + age1: 22, + age2: 60, + age3: 15, + age4: 88, + age5: 14, + }) + end + + it "correctly derives and saves hhmemb" do + record_from_db = ActiveRecord::Base.connection.execute("select hhmemb from sales_logs where id=#{household_lettings_log.id}").to_a[0] + expect(record_from_db["hhmemb"]).to eq(5) + end + + it "correctly derives and saves totchild" do + record_from_db = ActiveRecord::Base.connection.execute("select totchild from sales_logs where id=#{household_lettings_log.id}").to_a[0] + expect(record_from_db["totchild"]).to eq(2) + end + + it "correctly derives and saves totadult" do + record_from_db = ActiveRecord::Base.connection.execute("select totadult from sales_logs where id=#{household_lettings_log.id}").to_a[0] + expect(record_from_db["totadult"]).to eq(3) + end + + it "correctly derives and saves hhtype" do + record_from_db = ActiveRecord::Base.connection.execute("select hhtype from sales_logs where id=#{household_lettings_log.id}").to_a[0] + expect(record_from_db["hhtype"]).to eq(9) + end + end + context "when saving previous address" do def check_previous_postcode_fields(postcode_field) record_from_db = ActiveRecord::Base.connection.execute("select #{postcode_field} from sales_logs where id=#{address_sales_log.id}").to_a[0]