Skip to content

Commit

Permalink
Calculate columns subtotals
Browse files Browse the repository at this point in the history
  • Loading branch information
DaniBitZesty committed Aug 14, 2023
1 parent 54e2d53 commit 047d846
Showing 1 changed file with 36 additions and 11 deletions.
47 changes: 36 additions & 11 deletions forms/qae_form_builder/matrix_question.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,40 +48,65 @@ def calculate_row_total(question_key, x_headings, y_headings, answers)
answers["#{question_key}_total_system_calculated_#{y_heading.key}"] = row_totals[y_heading.key]
end
end

return row_totals
end

def calculate_col_total(question_key, x_headings, y_headings, answers)
col_totals = {}

x_headings.each do |x_heading|
col_totals[x_heading.key] ||= 0
if y_headings.any? { |heading| heading.key == "calculated_sub_total" }
subtotal = answers["#{question_key}_#{x_heading.key}_calculated_sub_total"].to_i
others = answers["#{question_key}_#{x_heading.key}_others"].to_i
total = subtotal + others
answers["#{question_key}_#{x_heading.key}_calculated_total"] = total
else
y_headings.each do |y_heading|
cell_value = answers["#{question_key}_#{x_heading.key}_#{y_heading.key}"]
unless y_heading.key == "calculated_total" || y_heading.key == "calculated_proportion"
col_totals[x_heading.key] += cell_value.to_i
end
answers["#{question_key}_#{x_heading.key}_calculated_total"] = col_totals[x_heading.key]
end
end
end
end

def calculate_col_subtotal(question_key, x_headings, y_headings, answers)
col_subtotals = {}

x_headings.each do |x_heading|
col_subtotals[x_heading.key] ||= 0
y_headings.each do |y_heading|
cell_value = answers["#{question_key}_#{x_heading.key}_#{y_heading.key}"]
unless y_heading.key == "calculated_total" || y_heading.key == "calculated_proportion"
col_totals[x_heading.key] += cell_value.to_i
unless %w(calculated_sub_total others calculated_total calculated_proportion).include?(y_heading.key)
col_subtotals[x_heading.key] += cell_value.to_i
end
answers["#{question_key}_#{x_heading.key}_calculated_total"] = col_totals[x_heading.key]
answers["#{question_key}_#{x_heading.key}_calculated_sub_total"] = col_subtotals[x_heading.key]
end
end

return col_totals
end

def calculate_proportion(question_key, answers, x_heading, y_heading)
disadvantaged = answers["#{question_key}_#{x_heading}_total_disadvantaged"].to_f
if y_headings.any? { |heading| heading.key == "calculated_sub_total" }
disadvantaged = answers["#{question_key}_#{x_heading}_calculated_sub_total"].to_f
else
disadvantaged = answers["#{question_key}_#{x_heading}_total_disadvantaged"].to_f
end
total = answers["#{question_key}_#{x_heading}_calculated_total"].to_f
proportion = (disadvantaged.to_f / total * 100).round(2)
answers["#{question_key}_#{x_heading}_calculated_proportion"] = proportion
end

def assign_autocalculated_value(question_key, x_headings, y_headings, answers, disabled_input, x_heading, y_heading)
if disabled_input == "auto-totals-col"
case disabled_input
when "auto-totals-col"
calculate_row_total(question_key, x_headings, y_headings, answers)
elsif disabled_input == "auto-totals-row"
when "auto-totals-row"
calculate_col_total(question_key, x_headings, y_headings, answers)
elsif disabled_input == "auto-proportion-row"
when "auto-subtotals-row"
calculate_col_subtotal(question_key, x_headings, y_headings, answers)
when "auto-proportion-row"
calculate_proportion(question_key, answers, x_heading, y_heading)
end
end
Expand Down

0 comments on commit 047d846

Please sign in to comment.