From 52abc23c70b98d724a78ec3cc241f013d57495ed Mon Sep 17 00:00:00 2001 From: kosiakkatrina <54268893+kosiakkatrina@users.noreply.github.com> Date: Tue, 17 Jan 2023 11:52:59 +0000 Subject: [PATCH] Cldc 808 derived sales variables (#1184) * Add derived household fields * Derive household variables for sales logs * Refactor and fix wrong test names * Count everyone under 20 as children if relationship is "C" --- .../derived_variables/sales_log_variables.rb | 72 +++++++++++++++++++ ...0113091706_add_derived_household_fields.rb | 10 +++ db/schema.rb | 4 ++ spec/models/sales_log_spec.rb | 38 ++++++++++ 4 files changed, 124 insertions(+) create mode 100644 db/migrate/20230113091706_add_derived_household_fields.rb diff --git a/app/models/derived_variables/sales_log_variables.rb b/app/models/derived_variables/sales_log_variables.rb index a527ed3f0..b00a16b63 100644 --- a/app/models/derived_variables/sales_log_variables.rb +++ b/app/models/derived_variables/sales_log_variables.rb @@ -16,5 +16,77 @@ 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 { |age| age.present? && age >= 60 } + end + + def total_child + (2..6).count do |i| + age = public_send("age#{i}") + relat = public_send("relat#{i}") + age.present? && ((age < 20 && %w[C].include?(relat))) + end + end + + def total_adult + total = age1.present? && age1.between?(16, 59) ? 1 : 0 + total + (2..6).count do |i| + age = public_send("age#{i}") + relat = public_send("relat#{i}") + age.present? && ((age.between?(20, 59) || age.between?(18, 19) && relat != "C")) + end + end + + def household_type + return unless total_elder && total_adult && totchild + + if only_one_elder? + 1 + elsif only_two_elders? + 2 + elsif only_one_adult? + 3 + elsif only_two_adults? + 4 + elsif one_adult_with_at_least_one_child? + 5 + elsif at_least_two_adults_with_at_least_one_child? + 6 + else + 9 + end + end + + def at_least_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 only_two_elders? + total_elder == 2 && total_adult.zero? && totchild.zero? + end + + def only_one_elder? + total_elder == 1 && total_adult.zero? && totchild.zero? end end diff --git a/db/migrate/20230113091706_add_derived_household_fields.rb b/db/migrate/20230113091706_add_derived_household_fields.rb new file mode 100644 index 000000000..d364b60a8 --- /dev/null +++ b/db/migrate/20230113091706_add_derived_household_fields.rb @@ -0,0 +1,10 @@ +class AddDerivedHouseholdFields < ActiveRecord::Migration[7.0] + def change + change_table :sales_logs, bulk: true do |t| + t.column :hhmemb, :integer + t.column :totadult, :integer + t.column :totchild, :integer + t.column :hhtype, :integer + end + end +end diff --git a/db/schema.rb b/db/schema.rb index 87f656bc7..85c088b66 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -483,6 +483,10 @@ ActiveRecord::Schema[7.0].define(version: 2023_01_13_125117) do t.integer "hoday" t.integer "homonth" t.integer "hoyear" + t.integer "hhmemb" + t.integer "totadult" + t.integer "totchild" + t.integer "hhtype" t.integer "fromprop" t.integer "socprevten" t.integer "mortlen" diff --git a/spec/models/sales_log_spec.rb b/spec/models/sales_log_spec.rb index bad72db3b..a0efcf313 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: "C", + relat3: "C", + relat4: "X", + relat5: "C", + age1: 22, + age2: 40, + age3: 19, + 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]