Browse Source

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"
pull/1195/head v0.2.33
kosiakkatrina 2 years ago committed by GitHub
parent
commit
52abc23c70
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 72
      app/models/derived_variables/sales_log_variables.rb
  2. 10
      db/migrate/20230113091706_add_derived_household_fields.rb
  3. 4
      db/schema.rb
  4. 38
      spec/models/sales_log_spec.rb

72
app/models/derived_variables/sales_log_variables.rb

@ -16,5 +16,77 @@ module DerivedVariables::SalesLogVariables
self.mscharge = 0 self.mscharge = 0
end end
self.pcode1, self.pcode2 = postcode_full.split(" ") if postcode_full.present? 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
end end

10
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

4
db/schema.rb

@ -483,6 +483,10 @@ ActiveRecord::Schema[7.0].define(version: 2023_01_13_125117) do
t.integer "hoday" t.integer "hoday"
t.integer "homonth" t.integer "homonth"
t.integer "hoyear" t.integer "hoyear"
t.integer "hhmemb"
t.integer "totadult"
t.integer "totchild"
t.integer "hhtype"
t.integer "fromprop" t.integer "fromprop"
t.integer "socprevten" t.integer "socprevten"
t.integer "mortlen" t.integer "mortlen"

38
spec/models/sales_log_spec.rb

@ -232,6 +232,44 @@ RSpec.describe SalesLog, type: :model do
end end
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 context "when saving previous address" do
def check_previous_postcode_fields(postcode_field) 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] record_from_db = ActiveRecord::Base.connection.execute("select #{postcode_field} from sales_logs where id=#{address_sales_log.id}").to_a[0]

Loading…
Cancel
Save