Browse Source

Merge branch 'main' into CLDC-1751-extra-borrowing

# Conflicts:
#	spec/models/form_handler_spec.rb
pull/1165/head
natdeanlewissoftwire 3 years ago
parent
commit
1ac781624a
  1. 8
      app/components/bulk_upload_error_row_component.rb
  2. 72
      app/models/derived_variables/sales_log_variables.rb
  3. 1
      app/models/form/lettings/questions/renewal.rb
  4. 4
      app/models/form/sales/pages/about_deposit_without_discount.rb
  5. 1
      app/models/form/sales/pages/about_price_not_rtb.rb
  6. 3
      app/models/form/sales/pages/mortgage_lender.rb
  7. 7
      app/models/form/sales/pages/mortgage_length.rb
  8. 5
      app/models/form/sales/pages/purchase_price.rb
  9. 5
      app/models/form/sales/questions/deposit_amount.rb
  10. 1
      app/models/form/sales/subsections/discounted_ownership_scheme.rb
  11. 2
      app/models/form/sales/subsections/outright_sale.rb
  12. 4
      app/models/sales_log.rb
  13. 10
      db/migrate/20230113091706_add_derived_household_fields.rb
  14. 4
      db/schema.rb
  15. 16
      spec/components/bulk_upload_error_row_component_spec.rb
  16. 2
      spec/models/form/lettings/questions/renewal_spec.rb
  17. 4
      spec/models/form/sales/pages/about_deposit_without_discount_spec.rb
  18. 1
      spec/models/form/sales/pages/about_price_not_rtb_spec.rb
  19. 4
      spec/models/form/sales/pages/mortgage_lender_spec.rb
  20. 4
      spec/models/form/sales/pages/mortgage_length_spec.rb
  21. 7
      spec/models/form/sales/pages/purchase_price_spec.rb
  22. 4
      spec/models/form/sales/questions/deposit_amount_spec.rb
  23. 1
      spec/models/form/sales/subsections/discounted_ownership_scheme_spec.rb
  24. 2
      spec/models/form/sales/subsections/outright_sale_spec.rb
  25. 4
      spec/models/form_handler_spec.rb
  26. 38
      spec/models/sales_log_spec.rb

8
app/components/bulk_upload_error_row_component.rb

@ -2,7 +2,7 @@ class BulkUploadErrorRowComponent < ViewComponent::Base
attr_reader :bulk_upload_errors attr_reader :bulk_upload_errors
def initialize(bulk_upload_errors:) def initialize(bulk_upload_errors:)
@bulk_upload_errors = bulk_upload_errors @bulk_upload_errors = sorted_errors(bulk_upload_errors)
super super
end end
@ -45,4 +45,10 @@ class BulkUploadErrorRowComponent < ViewComponent::Base
def sales? def sales?
bulk_upload.log_type == "sales" bulk_upload.log_type == "sales"
end end
private
def sorted_errors(errors)
errors.sort_by { |e| e.cell.rjust(3, "0") }
end
end end

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

1
app/models/form/lettings/questions/renewal.rb

@ -6,6 +6,7 @@ class Form::Lettings::Questions::Renewal < ::Form::Question
@header = "Is this letting a renewal?" @header = "Is this letting a renewal?"
@type = "radio" @type = "radio"
@answer_options = ANSWER_OPTIONS @answer_options = ANSWER_OPTIONS
@hint_text = "A renewal is a letting to the same tenant in the same property"
end end
ANSWER_OPTIONS = { ANSWER_OPTIONS = {

4
app/models/form/sales/pages/about_deposit_without_discount.rb

@ -2,7 +2,9 @@ class Form::Sales::Pages::AboutDepositWithoutDiscount < ::Form::Page
def initialize(id, hsh, subsection) def initialize(id, hsh, subsection)
super super
@header = "About the deposit" @header = "About the deposit"
@depends_on = [{ "is_type_discount?" => false }] @depends_on = [{ "is_type_discount?" => false, "ownershipsch" => 1 },
{ "ownershipsch" => 2 },
{ "ownershipsch" => 3, "mortgageused" => 1 }]
end end
def questions def questions

1
app/models/form/sales/pages/about_price_not_rtb.rb

@ -5,6 +5,7 @@ class Form::Sales::Pages::AboutPriceNotRtb < ::Form::Page
@header = "About the price of the property" @header = "About the price of the property"
@depends_on = [{ @depends_on = [{
"right_to_buy?" => false, "right_to_buy?" => false,
"rent_to_buy_full_ownership?" => false,
}] }]
end end

3
app/models/form/sales/pages/mortgage_lender.rb

@ -4,6 +4,9 @@ class Form::Sales::Pages::MortgageLender < ::Form::Page
@header = "" @header = ""
@description = "" @description = ""
@subsection = subsection @subsection = subsection
@depends_on = [{
"mortgageused" => 1,
}]
end end
def questions def questions

7
app/models/form/sales/pages/mortgage_length.rb

@ -1,4 +1,11 @@
class Form::Sales::Pages::MortgageLength < ::Form::Page class Form::Sales::Pages::MortgageLength < ::Form::Page
def initialize(id, hsh, subsection)
super
@depends_on = [{
"mortgageused" => 1,
}]
end
def questions def questions
@questions ||= [ @questions ||= [
Form::Sales::Questions::MortgageLength.new(nil, nil, self), Form::Sales::Questions::MortgageLength.new(nil, nil, self),

5
app/models/form/sales/pages/purchase_price.rb

@ -1,7 +1,10 @@
class Form::Sales::Pages::PurchasePrice < ::Form::Page class Form::Sales::Pages::PurchasePrice < ::Form::Page
def initialize(id, hsh, subsection) def initialize(id, hsh, subsection)
super super
@id = "purchase_price" @depends_on = [
{ "ownershipsch" => 3 },
{ "rent_to_buy_full_ownership?" => true },
]
end end
def questions def questions

5
app/models/form/sales/questions/deposit_amount.rb

@ -9,5 +9,10 @@ class Form::Sales::Questions::DepositAmount < ::Form::Question
@width = 5 @width = 5
@prefix = "£" @prefix = "£"
@hint_text = "Enter the total cash sum paid by the buyer towards the property that was not funded by the mortgage" @hint_text = "Enter the total cash sum paid by the buyer towards the property that was not funded by the mortgage"
@derived = true
end
def selected_answer_option_is_derived?(_log)
true
end end
end end

1
app/models/form/sales/subsections/discounted_ownership_scheme.rb

@ -11,6 +11,7 @@ class Form::Sales::Subsections::DiscountedOwnershipScheme < ::Form::Subsection
Form::Sales::Pages::LivingBeforePurchase.new("living_before_purchase_discounted_ownership", nil, self), Form::Sales::Pages::LivingBeforePurchase.new("living_before_purchase_discounted_ownership", nil, self),
Form::Sales::Pages::AboutPriceRtb.new(nil, nil, self), Form::Sales::Pages::AboutPriceRtb.new(nil, nil, self),
Form::Sales::Pages::AboutPriceNotRtb.new(nil, nil, self), Form::Sales::Pages::AboutPriceNotRtb.new(nil, nil, self),
Form::Sales::Pages::PurchasePrice.new("purchase_price_discounted_ownership", nil, self),
Form::Sales::Pages::Mortgageused.new("mortgage_used_discounted_ownership", nil, self), Form::Sales::Pages::Mortgageused.new("mortgage_used_discounted_ownership", nil, self),
Form::Sales::Pages::MortgageAmount.new("mortgage_amount_discounted_ownership", nil, self), Form::Sales::Pages::MortgageAmount.new("mortgage_amount_discounted_ownership", nil, self),
Form::Sales::Pages::MortgageLender.new("mortgage_lender_discounted_ownership", nil, self), Form::Sales::Pages::MortgageLender.new("mortgage_lender_discounted_ownership", nil, self),

2
app/models/form/sales/subsections/outright_sale.rb

@ -8,7 +8,7 @@ class Form::Sales::Subsections::OutrightSale < ::Form::Subsection
def pages def pages
@pages ||= [ @pages ||= [
Form::Sales::Pages::PurchasePrice.new(nil, nil, self), Form::Sales::Pages::PurchasePrice.new("purchase_price_outright_sale", nil, self),
Form::Sales::Pages::Mortgageused.new("mortgage_used_outright_sale", nil, self), Form::Sales::Pages::Mortgageused.new("mortgage_used_outright_sale", nil, self),
Form::Sales::Pages::MortgageAmount.new("mortgage_amount_outright_sale", nil, self), Form::Sales::Pages::MortgageAmount.new("mortgage_amount_outright_sale", nil, self),
Form::Sales::Pages::MortgageLender.new("mortgage_lender_outright_sale", nil, self), Form::Sales::Pages::MortgageLender.new("mortgage_lender_outright_sale", nil, self),

4
app/models/sales_log.rb

@ -121,6 +121,10 @@ class SalesLog < Log
[9, 14, 27].include?(type) [9, 14, 27].include?(type)
end end
def rent_to_buy_full_ownership?
type == 29
end
def is_type_discount? def is_type_discount?
type == 18 type == 18
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 "mortgagelender" t.integer "mortgagelender"

16
spec/components/bulk_upload_error_row_component_spec.rb

@ -49,6 +49,22 @@ RSpec.describe BulkUploadErrorRowComponent, type: :component do
expect(result).to have_content(expected) expect(result).to have_content(expected)
end end
context "when multiple errors for a row" do
subject(:component) { described_class.new(bulk_upload_errors:) }
let(:bulk_upload_errors) do
[
build(:bulk_upload_error, cell: "Z1"),
build(:bulk_upload_error, cell: "AB1"),
build(:bulk_upload_error, cell: "A1"),
]
end
it "is sorted by cell" do
expect(component.bulk_upload_errors.map(&:cell)).to eql(%w[A1 Z1 AB1])
end
end
context "when a sales bulk upload" do context "when a sales bulk upload" do
let(:bulk_upload) { create(:bulk_upload, :sales) } let(:bulk_upload) { create(:bulk_upload, :sales) }
let(:field) { :field_87 } let(:field) { :field_87 }

2
spec/models/form/lettings/questions/renewal_spec.rb

@ -28,7 +28,7 @@ RSpec.describe Form::Lettings::Questions::Renewal, type: :model do
end end
it "has the correct hint_text" do it "has the correct hint_text" do
expect(question.hint_text).to be_nil expect(question.hint_text).to eq("A renewal is a letting to the same tenant in the same property")
end end
it "has the correct answer_options" do it "has the correct answer_options" do

4
spec/models/form/sales/pages/about_deposit_without_discount_spec.rb

@ -29,7 +29,9 @@ RSpec.describe Form::Sales::Pages::AboutDepositWithoutDiscount, type: :model do
it "has correct depends_on" do it "has correct depends_on" do
expect(page.depends_on).to eq( expect(page.depends_on).to eq(
[{ "is_type_discount?" => false }], [{ "is_type_discount?" => false, "ownershipsch" => 1 },
{ "ownershipsch" => 2 },
{ "ownershipsch" => 3, "mortgageused" => 1 }],
) )
end end
end end

1
spec/models/form/sales/pages/about_price_not_rtb_spec.rb

@ -30,6 +30,7 @@ RSpec.describe Form::Sales::Pages::AboutPriceNotRtb, type: :model do
it "has correct depends_on" do it "has correct depends_on" do
expect(page.depends_on).to eq([{ expect(page.depends_on).to eq([{
"right_to_buy?" => false, "right_to_buy?" => false,
"rent_to_buy_full_ownership?" => false,
}]) }])
end end
end end

4
spec/models/form/sales/pages/mortgage_lender_spec.rb

@ -28,6 +28,8 @@ RSpec.describe Form::Sales::Pages::MortgageLender, type: :model do
end end
it "has correct depends_on" do it "has correct depends_on" do
expect(page.depends_on).to be_nil expect(page.depends_on).to eq([{
"mortgageused" => 1,
}])
end end
end end

4
spec/models/form/sales/pages/mortgage_length_spec.rb

@ -28,6 +28,8 @@ RSpec.describe Form::Sales::Pages::MortgageLength, type: :model do
end end
it "has correct depends_on" do it "has correct depends_on" do
expect(page.depends_on).to be_nil expect(page.depends_on).to eq([{
"mortgageused" => 1,
}])
end end
end end

7
spec/models/form/sales/pages/purchase_price_spec.rb

@ -3,7 +3,7 @@ require "rails_helper"
RSpec.describe Form::Sales::Pages::PurchasePrice, type: :model do RSpec.describe Form::Sales::Pages::PurchasePrice, type: :model do
subject(:page) { described_class.new(page_id, page_definition, subsection) } subject(:page) { described_class.new(page_id, page_definition, subsection) }
let(:page_id) { nil } let(:page_id) { "purchase_price" }
let(:page_definition) { nil } let(:page_definition) { nil }
let(:subsection) { instance_double(Form::Subsection) } let(:subsection) { instance_double(Form::Subsection) }
@ -28,6 +28,9 @@ RSpec.describe Form::Sales::Pages::PurchasePrice, type: :model do
end end
it "has correct depends_on" do it "has correct depends_on" do
expect(page.depends_on).to be_nil expect(page.depends_on).to eq([
{ "ownershipsch" => 3 },
{ "rent_to_buy_full_ownership?" => true },
])
end end
end end

4
spec/models/form/sales/questions/deposit_amount_spec.rb

@ -27,8 +27,8 @@ RSpec.describe Form::Sales::Questions::DepositAmount, type: :model do
expect(question.type).to eq("numeric") expect(question.type).to eq("numeric")
end end
it "is not marked as derived" do it "is marked as derived" do
expect(question.derived?).to be false expect(question.derived?).to be true
end end
it "has the correct hint" do it "has the correct hint" do

1
spec/models/form/sales/subsections/discounted_ownership_scheme_spec.rb

@ -17,6 +17,7 @@ RSpec.describe Form::Sales::Subsections::DiscountedOwnershipScheme, type: :model
living_before_purchase_discounted_ownership living_before_purchase_discounted_ownership
about_price_rtb about_price_rtb
about_price_not_rtb about_price_not_rtb
purchase_price_discounted_ownership
mortgage_used_discounted_ownership mortgage_used_discounted_ownership
mortgage_amount_discounted_ownership mortgage_amount_discounted_ownership
mortgage_lender_discounted_ownership mortgage_lender_discounted_ownership

2
spec/models/form/sales/subsections/outright_sale_spec.rb

@ -14,7 +14,7 @@ RSpec.describe Form::Sales::Subsections::OutrightSale, type: :model do
it "has correct pages" do it "has correct pages" do
expect(outright_sale.pages.map(&:id)).to eq( expect(outright_sale.pages.map(&:id)).to eq(
%w[ %w[
purchase_price purchase_price_outright_sale
mortgage_used_outright_sale mortgage_used_outright_sale
mortgage_amount_outright_sale mortgage_amount_outright_sale
mortgage_lender_outright_sale mortgage_lender_outright_sale

4
spec/models/form_handler_spec.rb

@ -52,14 +52,14 @@ RSpec.describe FormHandler do
it "is able to load a current sales form" do it "is able to load a current sales form" do
form = form_handler.get_form("current_sales") form = form_handler.get_form("current_sales")
expect(form).to be_a(Form) expect(form).to be_a(Form)
expect(form.pages.count).to eq(147) expect(form.pages.count).to eq(148)
expect(form.name).to eq("2022_2023_sales") expect(form.name).to eq("2022_2023_sales")
end end
it "is able to load a previous sales form" do it "is able to load a previous sales form" do
form = form_handler.get_form("previous_sales") form = form_handler.get_form("previous_sales")
expect(form).to be_a(Form) expect(form).to be_a(Form)
expect(form.pages.count).to eq(147) expect(form.pages.count).to eq(148)
expect(form.name).to eq("2021_2022_sales") expect(form.name).to eq("2021_2022_sales")
end end
end end

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