Browse Source

CLDC-4184: Consolidate logic for questions that appear in multiple subsections

pull/3221/head
samyou-softwire 3 weeks ago
parent
commit
13d67552c2
  1. 18
      app/models/form/question.rb
  2. 9
      app/models/form/sales/questions/deposit_amount.rb
  3. 11
      app/models/form/sales/questions/equity.rb
  4. 9
      app/models/form/sales/questions/extra_borrowing.rb
  5. 9
      app/models/form/sales/questions/has_leasehold_charges.rb
  6. 17
      app/models/form/sales/questions/has_service_charge.rb
  7. 9
      app/models/form/sales/questions/leasehold_charges.rb
  8. 5
      app/models/form/sales/questions/living_before_purchase.rb
  9. 5
      app/models/form/sales/questions/living_before_purchase_years.rb
  10. 9
      app/models/form/sales/questions/mortgage_amount.rb
  11. 10
      app/models/form/sales/questions/mortgage_length.rb
  12. 6
      app/models/form/sales/questions/mortgage_length_known.rb
  13. 19
      app/models/form/sales/questions/mortgageused.rb
  14. 5
      app/models/form/sales/questions/purchase_price.rb
  15. 17
      app/models/form/sales/questions/service_charge.rb
  16. 11
      app/models/form/sales/questions/value.rb

18
app/models/form/question.rb

@ -373,8 +373,22 @@ private
# every year currently visible should have an explicit question number specified.
# however, form_handler.rb will still initialise the next form even if its not visible.
# so we have a fallback to the latest year for these future years so all question have a question number.
def get_question_number_from_hash(hash)
hash[form.start_date.year] || hash[hash.keys.max]
#
# some hashes are complex and require a second key to find the correct year.
# 2025 and before the question number used to depend on the ownershipsch and other log attributes
# 2026 and beyond we use the subsection ID for consistency
def get_question_number_from_hash(hash, value_key: nil)
year = if hash[form.start_date.year].present?
form.start_date.year
else
hash.keys.max
end
hash_value = hash[year]
return hash_value if hash_value.is_a?(Integer)
hash_value[value_key]
end
RADIO_YES_VALUE = {

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

@ -1,6 +1,6 @@
class Form::Sales::Questions::DepositAmount < ::Form::Question
def initialize(id, hsh, subsection, ownershipsch:, optional:)
super(id, hsh, subsection)
def initialize(id, hsh, page, ownershipsch:, optional:)
super(id, hsh, page)
@id = "deposit"
@type = "numeric"
@min = 0
@ -9,7 +9,7 @@ class Form::Sales::Questions::DepositAmount < ::Form::Question
@width = 5
@prefix = "£"
@ownershipsch = ownershipsch
@question_number = QUESTION_NUMBER_FROM_YEAR_AND_OWNERSHIP.fetch(form.start_date.year, QUESTION_NUMBER_FROM_YEAR_AND_OWNERSHIP.max_by { |k, _v| k }.last)[ownershipsch]
@question_number = get_question_number_from_hash(QUESTION_NUMBER_FROM_YEAR_AND_SECTION, value_key: form.start_year_2026_or_later? ? subsection.id : ownershipsch)
@optional = optional
@top_guidance_partial = top_guidance_partial
@copy_key = copy_key
@ -20,10 +20,11 @@ class Form::Sales::Questions::DepositAmount < ::Form::Question
log.outright_sale? && !log.mortgage_used?
end
QUESTION_NUMBER_FROM_YEAR_AND_OWNERSHIP = {
QUESTION_NUMBER_FROM_YEAR_AND_SECTION = {
2023 => { 1 => 95, 2 => 108, 3 => 116 },
2024 => { 1 => 96, 2 => 109, 3 => 116 },
2025 => { 1 => 85, 2 => 110 },
2026 => { "shared_ownership_initial_purchase" => 85, "discounted_ownership_scheme" => 110 },
}.freeze
def top_guidance_partial

11
app/models/form/sales/questions/equity.rb

@ -9,11 +9,14 @@ class Form::Sales::Questions::Equity < ::Form::Question
@step = 0.1
@width = 5
@suffix = "%"
@question_number = question_number_from_year[form.start_date.year] || question_number_from_year[question_number_from_year.keys.max]
@question_number = get_question_number_from_hash(QUESTION_NUMBER_FROM_YEAR_AND_SECTION, value_key: subsection.id)
@top_guidance_partial = "financial_calculations_shared_ownership"
end
def question_number_from_year
{ 2023 => 89, 2024 => 90, 2025 => subsection.id == "shared_ownership_staircasing_transaction" ? 98 : 81 }
end
QUESTION_NUMBER_FROM_YEAR_AND_SECTION = {
2023 => 89,
2024 => 90,
2025 => { "shared_ownership_initial_purchase" => 81, "shared_ownership_staircasing_transaction" => 98 },
2026 => { "shared_ownership_initial_purchase" => 81, "shared_ownership_staircasing_transaction" => 98 },
}.freeze
end

9
app/models/form/sales/questions/extra_borrowing.rb

@ -1,12 +1,12 @@
class Form::Sales::Questions::ExtraBorrowing < ::Form::Question
def initialize(id, hsh, subsection, ownershipsch:)
super(id, hsh, subsection)
def initialize(id, hsh, page, ownershipsch:)
super(id, hsh, page)
@id = "extrabor"
@type = "radio"
@answer_options = ANSWER_OPTIONS
@page = page
@ownershipsch = ownershipsch
@question_number = QUESTION_NUMBER_FROM_YEAR_AND_OWNERSHIP.fetch(form.start_date.year, QUESTION_NUMBER_FROM_YEAR_AND_OWNERSHIP.max_by { |k, _v| k }.last)[ownershipsch]
@question_number = get_question_number_from_hash(QUESTION_NUMBER_FROM_YEAR_AND_SECTION, value_key: form.start_year_2026_or_later? ? subsection.id : ownershipsch)
end
ANSWER_OPTIONS = {
@ -16,9 +16,10 @@ class Form::Sales::Questions::ExtraBorrowing < ::Form::Question
"3" => { "value" => "Don’t know" },
}.freeze
QUESTION_NUMBER_FROM_YEAR_AND_OWNERSHIP = {
QUESTION_NUMBER_FROM_YEAR_AND_SECTION = {
2023 => { 1 => 94, 2 => 107, 3 => 115 },
2024 => { 1 => 95, 2 => 108, 3 => 115 },
2025 => { 2 => 109 },
2026 => { "discounted_ownership_scheme" => 109 },
}.freeze
end

9
app/models/form/sales/questions/has_leasehold_charges.rb

@ -1,6 +1,6 @@
class Form::Sales::Questions::HasLeaseholdCharges < ::Form::Question
def initialize(id, hsh, subsection, ownershipsch:)
super(id, hsh, subsection)
def initialize(id, hsh, page, ownershipsch:)
super(id, hsh, page)
@id = "has_mscharge"
@type = "radio"
@answer_options = ANSWER_OPTIONS
@ -16,7 +16,7 @@ class Form::Sales::Questions::HasLeaseholdCharges < ::Form::Question
}
@ownershipsch = ownershipsch
@copy_key = "sales.sale_information.leaseholdcharges.has_mscharge"
@question_number = QUESTION_NUMBER_FROM_YEAR_AND_OWNERSHIP.fetch(form.start_date.year, QUESTION_NUMBER_FROM_YEAR_AND_OWNERSHIP.max_by { |k, _v| k }.last)[ownershipsch]
@question_number = get_question_number_from_hash(QUESTION_NUMBER_FROM_YEAR_AND_SECTION, value_key: form.start_year_2026_or_later? ? subsection.id : ownershipsch)
end
ANSWER_OPTIONS = {
@ -24,8 +24,9 @@ class Form::Sales::Questions::HasLeaseholdCharges < ::Form::Question
"0" => { "value" => "No" },
}.freeze
QUESTION_NUMBER_FROM_YEAR_AND_OWNERSHIP = {
QUESTION_NUMBER_FROM_YEAR_AND_SECTION = {
2024 => { 1 => 99, 2 => 110, 3 => 117 },
2025 => { 2 => 111 },
2026 => 111,
}.freeze
end

17
app/models/form/sales/questions/has_service_charge.rb

@ -1,6 +1,6 @@
class Form::Sales::Questions::HasServiceCharge < ::Form::Question
def initialize(id, hsh, subsection, staircasing:)
super(id, hsh, subsection)
def initialize(id, hsh, page, staircasing:)
super(id, hsh, page)
@id = "has_mscharge"
@type = "radio"
@answer_options = ANSWER_OPTIONS
@ -16,7 +16,7 @@ class Form::Sales::Questions::HasServiceCharge < ::Form::Question
}
@copy_key = "sales.sale_information.servicecharges.has_servicecharge"
@staircasing = staircasing
@question_number = question_number_from_year[form.start_date.year] || question_number_from_year[question_number_from_year.keys.max]
@question_number = get_question_number_from_hash(QUESTION_NUMBER_FROM_YEAR_AND_SECTION, value_key: subsection.id)
end
ANSWER_OPTIONS = {
@ -24,11 +24,8 @@ class Form::Sales::Questions::HasServiceCharge < ::Form::Question
"0" => { "value" => "No" },
}.freeze
def question_number_from_year
if @staircasing
{ 2026 => 0 }.freeze
else
{ 2025 => 88, 2026 => 0 }.freeze
end
end
QUESTION_NUMBER_FROM_YEAR_AND_SECTION = {
2025 => 88,
2026 => { "shared_ownership_initial_purchase" => 88, "shared_ownership_staircasing_transaction" => 88 },
}
end

9
app/models/form/sales/questions/leasehold_charges.rb

@ -1,6 +1,6 @@
class Form::Sales::Questions::LeaseholdCharges < ::Form::Question
def initialize(id, hsh, subsection, ownershipsch:)
super(id, hsh, subsection)
def initialize(id, hsh, page, ownershipsch:)
super(id, hsh, page)
@id = "mscharge"
@type = "numeric"
@min = 1
@ -9,13 +9,14 @@ class Form::Sales::Questions::LeaseholdCharges < ::Form::Question
@prefix = "£"
@copy_key = "sales.sale_information.leaseholdcharges.mscharge"
@ownershipsch = ownershipsch
@question_number = QUESTION_NUMBER_FROM_YEAR_AND_OWNERSHIP.fetch(form.start_date.year, QUESTION_NUMBER_FROM_YEAR_AND_OWNERSHIP.max_by { |k, _v| k }.last)[ownershipsch]
@question_number = get_question_number_from_hash(QUESTION_NUMBER_FROM_YEAR_AND_SECTION, value_key: form.start_year_2026_or_later? ? subsection.id : ownershipsch)
@strip_commas = true
end
QUESTION_NUMBER_FROM_YEAR_AND_OWNERSHIP = {
QUESTION_NUMBER_FROM_YEAR_AND_SECTION = {
2023 => { 1 => 98, 2 => 109, 3 => 117 },
2024 => { 1 => 99, 2 => 110, 3 => 117 },
2025 => { 2 => 111 },
2026 => { "discounted_ownership_scheme" => 111 },
}.freeze
end

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

@ -16,7 +16,7 @@ class Form::Sales::Questions::LivingBeforePurchase < ::Form::Question
],
}
@ownershipsch = ownershipsch
@question_number = QUESTION_NUMBER_FROM_YEAR_AND_OWNERSHIP.fetch(form.start_date.year, QUESTION_NUMBER_FROM_YEAR_AND_OWNERSHIP.max_by { |k, _v| k }.last)[ownershipsch]
@question_number = get_question_number_from_hash(QUESTION_NUMBER_FROM_YEAR_AND_SECTION, value_key: form.start_year_2026_or_later? ? subsection.id : ownershipsch)
end
ANSWER_OPTIONS = {
@ -24,9 +24,10 @@ class Form::Sales::Questions::LivingBeforePurchase < ::Form::Question
"1" => { "value" => "No" },
}.freeze
QUESTION_NUMBER_FROM_YEAR_AND_OWNERSHIP = {
QUESTION_NUMBER_FROM_YEAR_AND_SECTION = {
2023 => { 1 => 75, 2 => 99 },
2024 => { 1 => 77, 2 => 100 },
2025 => { 1 => 75, 2 => 102 },
2026 => { "shared_ownership_initial_purchase" => 75, "discounted_ownership_scheme" => 102 },
}.freeze
end

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

@ -9,16 +9,17 @@ class Form::Sales::Questions::LivingBeforePurchaseYears < ::Form::Question
@step = 1
@width = 5
@ownershipsch = ownershipsch
@question_number = QUESTION_NUMBER_FROM_YEAR_AND_OWNERSHIP.fetch(form.start_date.year, QUESTION_NUMBER_FROM_YEAR_AND_OWNERSHIP.max_by { |k, _v| k }.last)[ownershipsch]
@question_number = get_question_number_from_hash(QUESTION_NUMBER_FROM_YEAR_AND_SECTION, value_key: form.start_year_2026_or_later? ? subsection.id : ownershipsch)
end
def suffix_label(log)
" #{'year'.pluralize(log[id])}"
end
QUESTION_NUMBER_FROM_YEAR_AND_OWNERSHIP = {
QUESTION_NUMBER_FROM_YEAR_AND_SECTION = {
2023 => { 1 => 75, 2 => 99 },
2024 => { 1 => 77, 2 => 100 },
2025 => { 1 => 75, 2 => 102 },
2026 => { "shared_ownership_initial_purchase" => 75, "discounted_ownership_scheme" => 102 },
}.freeze
end

9
app/models/form/sales/questions/mortgage_amount.rb

@ -1,6 +1,6 @@
class Form::Sales::Questions::MortgageAmount < ::Form::Question
def initialize(id, hsh, subsection, ownershipsch:)
super(id, hsh, subsection)
def initialize(id, hsh, page, ownershipsch:)
super(id, hsh, page)
@id = "mortgage"
@type = "numeric"
@min = 1
@ -8,15 +8,16 @@ class Form::Sales::Questions::MortgageAmount < ::Form::Question
@width = 5
@prefix = "£"
@ownershipsch = ownershipsch
@question_number = QUESTION_NUMBER_FROM_YEAR_AND_OWNERSHIP.fetch(form.start_date.year, QUESTION_NUMBER_FROM_YEAR_AND_OWNERSHIP.max_by { |k, _v| k }.last)[ownershipsch]
@question_number = get_question_number_from_hash(QUESTION_NUMBER_FROM_YEAR_AND_SECTION, value_key: form.start_year_2026_or_later? ? subsection.id : ownershipsch)
@top_guidance_partial = top_guidance_partial
@strip_commas = true
end
QUESTION_NUMBER_FROM_YEAR_AND_OWNERSHIP = {
QUESTION_NUMBER_FROM_YEAR_AND_SECTION = {
2023 => { 1 => 91, 2 => 104, 3 => 112 },
2024 => { 1 => 92, 2 => 105, 3 => 113 },
2025 => { 1 => 83, 2 => 107 },
2026 => { "shared_ownership_initial_purchase" => 83, "discounted_ownership_scheme" => 107 },
}.freeze
def derived?(log)

10
app/models/form/sales/questions/mortgage_length.rb

@ -1,6 +1,6 @@
class Form::Sales::Questions::MortgageLength < ::Form::Question
def initialize(id, hsh, subsection, ownershipsch:)
super(id, hsh, subsection)
def initialize(id, hsh, page, ownershipsch:)
super(id, hsh, page)
@id = "mortlen"
@type = "numeric"
@min = 0
@ -8,17 +8,17 @@ class Form::Sales::Questions::MortgageLength < ::Form::Question
@step = 1
@width = 5
@ownershipsch = ownershipsch
@question_number = QUESTION_NUMBER_FROM_YEAR_AND_OWNERSHIP.fetch(form.start_date.year, QUESTION_NUMBER_FROM_YEAR_AND_OWNERSHIP.max_by { |k, _v| k }.last)[ownershipsch]
@question_number = get_question_number_from_hash(QUESTION_NUMBER_FROM_YEAR_AND_SECTION, value_key: form.start_year_2026_or_later? ? subsection.id : ownershipsch)
end
def suffix_label(log)
" #{'year'.pluralize(log[id])}"
end
QUESTION_NUMBER_FROM_YEAR_AND_OWNERSHIP = {
QUESTION_NUMBER_FROM_YEAR_AND_SECTION = {
2023 => { 1 => 93, 2 => 106, 3 => 114 },
2024 => { 1 => 94, 2 => 107, 3 => 114 },
2025 => { 1 => 84, 2 => 108 },
2026 => { 1 => 84, 2 => 108 },
2026 => { "shared_ownership_initial_purchase" => 84, "discounted_ownership_scheme" => 108 },
}.freeze
end

6
app/models/form/sales/questions/mortgage_length_known.rb

@ -10,12 +10,12 @@ class Form::Sales::Questions::MortgageLengthKnown < ::Form::Question
{ "mortlen_known" => 0 },
],
}
@question_number = QUESTION_NUMBER_FROM_YEAR_AND_OWNERSHIP.fetch(form.start_date.year, QUESTION_NUMBER_FROM_YEAR_AND_OWNERSHIP.max_by { |k, _v| k }.last)[ownershipsch]
@question_number = get_question_number_from_hash(QUESTION_NUMBER_FROM_YEAR_AND_SECTION, value_key: form.start_year_2026_or_later? ? subsection.id : ownershipsch)
end
ANSWER_OPTIONS = { "0" => { "value" => "Yes" }, "1" => { "value" => "No" } }.freeze
QUESTION_NUMBER_FROM_YEAR_AND_OWNERSHIP = {
2026 => { 1 => 84, 2 => 108 },
QUESTION_NUMBER_FROM_YEAR_AND_SECTION = {
2026 => { "shared_ownership_initial_purchase" => 84, "discounted_ownership_scheme" => 108 },
}.freeze
end

19
app/models/form/sales/questions/mortgageused.rb

@ -1,11 +1,11 @@
class Form::Sales::Questions::Mortgageused < ::Form::Question
def initialize(id, hsh, subsection, ownershipsch:)
super(id, hsh, subsection)
def initialize(id, hsh, page, ownershipsch:)
super(id, hsh, page)
@id = "mortgageused"
@type = "radio"
@answer_options = ANSWER_OPTIONS
@ownershipsch = ownershipsch
@question_number = question_number_from_year_and_ownership.fetch(form.start_date.year, question_number_from_year_and_ownership.max_by { |k, _v| k }.last)[ownershipsch]
@question_number = get_question_number_from_hash(QUESTION_NUMBER_FROM_YEAR_AND_SECTION, value_key: form.start_year_2025_or_later? ? subsection.id : ownershipsch)
@top_guidance_partial = top_guidance_partial
end
@ -30,13 +30,12 @@ class Form::Sales::Questions::Mortgageused < ::Form::Question
ANSWER_OPTIONS.reject { |key, _v| %w[3 divider].include?(key) }
end
def question_number_from_year_and_ownership
{
2023 => { 1 => 90, 2 => 103, 3 => 111 },
2024 => { 1 => 91, 2 => 104, 3 => 112 },
2025 => { 1 => subsection.id == "shared_ownership_staircasing_transaction" ? 99 : 82, 2 => 106 },
}
end
QUESTION_NUMBER_FROM_YEAR_AND_SECTION = {
2023 => { 1 => 90, 2 => 103, 3 => 111 },
2024 => { 1 => 91, 2 => 104, 3 => 112 },
2025 => { "shared_ownership_initial_purchase" => 82, "shared_ownership_staircasing_transaction" => 99, "discounted_ownership_scheme" => 106 },
2026 => { "shared_ownership_initial_purchase" => 82, "shared_ownership_staircasing_transaction" => 99, "discounted_ownership_scheme" => 106 },
}.freeze
def top_guidance_partial
return "financial_calculations_shared_ownership" if @ownershipsch == 1

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

@ -8,15 +8,16 @@ class Form::Sales::Questions::PurchasePrice < ::Form::Question
@width = 5
@prefix = "£"
@ownership_sch = ownershipsch
@question_number = QUESTION_NUMBER_FROM_YEAR_AND_OWNERSHIP.fetch(form.start_date.year, QUESTION_NUMBER_FROM_YEAR_AND_OWNERSHIP.max_by { |k, _v| k }.last)[ownershipsch]
@question_number = get_question_number_from_hash(QUESTION_NUMBER_FROM_YEAR_AND_SECTION, value_key: form.start_year_2026_or_later? ? subsection.id : ownershipsch)
@top_guidance_partial = top_guidance_partial
@strip_commas = true
end
QUESTION_NUMBER_FROM_YEAR_AND_OWNERSHIP = {
QUESTION_NUMBER_FROM_YEAR_AND_SECTION = {
2023 => { 2 => 100, 3 => 110 },
2024 => { 2 => 101, 3 => 111 },
2025 => { 2 => 103 },
2026 => { "discounted_ownership_scheme" => 103 },
}.freeze
def copy_key

17
app/models/form/sales/questions/service_charge.rb

@ -1,6 +1,6 @@
class Form::Sales::Questions::ServiceCharge < ::Form::Question
def initialize(id, hsh, subsection, staircasing:)
super(id, hsh, subsection)
def initialize(id, hsh, page, staircasing:)
super(id, hsh, page)
@id = "mscharge"
@type = "numeric"
@min = 1
@ -10,15 +10,12 @@ class Form::Sales::Questions::ServiceCharge < ::Form::Question
@prefix = "£"
@copy_key = "sales.sale_information.servicecharges.servicecharge"
@staircasing = staircasing
@question_number = question_number_from_year[form.start_date.year] || question_number_from_year[question_number_from_year.keys.max]
@question_number = get_question_number_from_hash(QUESTION_NUMBER_FROM_YEAR_AND_SECTION, value_key: subsection.id)
@strip_commas = true
end
def question_number_from_year
if @staircasing
{ 2026 => 0 }.freeze
else
{ 2025 => 88, 2026 => 0 }.freeze
end
end
QUESTION_NUMBER_FROM_YEAR_AND_SECTION = {
2025 => 88,
2026 => { "shared_ownership_initial_purchase" => 88, "shared_ownership_staircasing_transaction" => 88 },
}
end

11
app/models/form/sales/questions/value.rb

@ -8,12 +8,15 @@ class Form::Sales::Questions::Value < ::Form::Question
@step = 1
@width = 5
@prefix = "£"
@question_number = question_number_from_year[form.start_date.year] || question_number_from_year[question_number_from_year.keys.max]
@question_number = get_question_number_from_hash(QUESTION_NUMBER_FROM_YEAR_AND_SECTION, value_key: subsection.id)
@top_guidance_partial = "financial_calculations_shared_ownership"
@strip_commas = true
end
def question_number_from_year
{ 2023 => 88, 2024 => 89, 2025 => subsection.id == "shared_ownership_staircasing_transaction" ? 97 : 80 }
end
QUESTION_NUMBER_FROM_YEAR_AND_SECTION = {
2023 => 88,
2024 => 89,
2025 => { "shared_ownership_initial_purchase" => 80, "shared_ownership_staircasing_transaction" => 97 },
2026 => { "shared_ownership_initial_purchase" => 80, "shared_ownership_staircasing_transaction" => 97 },
}.freeze
end

Loading…
Cancel
Save