Skip to content

Commit 8748bd7

Browse files
committed
Introduce a prices_sum helper to factorize the calculation on reports
1 parent 30f702e commit 8748bd7

22 files changed

+77
-65
lines changed

app/helpers/reports_helper.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,4 +61,8 @@ def datepicker_time(datetime)
6161
datetime = Time.zone.parse(datetime) if datetime.is_a? String
6262
datetime.strftime('%Y-%m-%d %H:%M')
6363
end
64+
65+
def prices_sum(prices_list)
66+
prices_list.sum(&:to_f).round(2)
67+
end
6468
end

lib/reporting/report_template.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ def custom_headers
8888
# summary_row: proc do |group_key, items, rows|
8989
# {
9090
# quantity: rows.map(&:quantity).sum(&:to_i),
91-
# price: "#{rows.map(&:price).sum(&:to_f).round(2)} #{currency_symbol}"
91+
# price: "#{prices_sum(rows.map(&:price))} #{currency_symbol}"
9292
# }
9393
# end,
9494
# summary_row_class: "", # by default 'text-bold'

lib/reporting/reports/bulk_coop/allocation.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ def rules
3232
summary_row: proc do |_key, items, rows|
3333
line_items = items.flatten
3434
{
35-
sum_total: rows.map(&:sum_total).sum(&:to_f).round(2),
35+
sum_total: prices_sum(rows.map(&:sum_total)),
3636
total_available: total_available(line_items),
3737
unallocated: remainder(line_items),
3838
max_quantity_excess: max_quantity_excess(line_items)

lib/reporting/reports/bulk_coop/base.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ def remainder(line_items)
9696
def total_amount(line_items)
9797
line_items.map { |li|
9898
scaled_final_weight_volume(li)
99-
}.sum(&:to_f).round(2)
99+
}.sum(&:to_f).round(3)
100100
end
101101

102102
def scaled_final_weight_volume(line_item)

lib/reporting/reports/bulk_coop/customer_payments.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,15 @@ def columns
2121
private
2222

2323
def customer_payments_total_cost(line_items)
24-
unique_orders(line_items).map(&:total).sum(&:to_f).round(2)
24+
prices_sum(unique_orders(line_items).map(&:total))
2525
end
2626

2727
def customer_payments_amount_owed(line_items)
28-
unique_orders(line_items).map(&:new_outstanding_balance).sum(&:to_f).round(2)
28+
prices_sum(unique_orders(line_items).map(&:new_outstanding_balance))
2929
end
3030

3131
def customer_payments_amount_paid(line_items)
32-
unique_orders(line_items).map(&:payment_total).sum(&:to_f).round(2)
32+
prices_sum(unique_orders(line_items).map(&:payment_total))
3333
end
3434

3535
def unique_orders(line_items)

lib/reporting/reports/bulk_coop/supplier_report.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ def rules
3232
summary_row: proc do |_key, items, rows|
3333
line_items = items.flatten
3434
{
35-
sum_total: rows.map(&:sum_total).sum(&:to_f).round(2),
35+
sum_total: prices_sum(rows.map(&:sum_total)),
3636
units_required: units_required(line_items),
3737
unallocated: remainder(line_items),
3838
max_quantity_excess: max_quantity_excess(line_items)

lib/reporting/reports/customers/base.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ def columns
3333
},
3434
shipping_method: proc { |orders| last_completed_order(orders).shipping_method&.name },
3535
total_orders: proc { |orders| orders.count },
36-
total_incl_tax: proc { |orders| orders.map(&:total).sum(&:to_f).round(2) },
36+
total_incl_tax: proc { |orders| prices_sum(orders.map(&:total)) },
3737
last_completed_order_date: proc { |orders| last_completed_order_date(orders) },
3838
}
3939
end

lib/reporting/reports/enterprise_fee_summary/enterprise_fees_with_tax_report_by_order.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ def rules
158158
end
159159

160160
def enterprise_fees_sum(order)
161-
amount = enterprise_fees(order).map(&:amount).sum(&:to_f).round(2)
161+
amount = prices_sum(enterprise_fees(order).map(&:amount))
162162
apply_voucher_on_amount(order, amount)
163163
end
164164

@@ -183,7 +183,7 @@ def enterprise_fee_tax(order, included: false, added: false)
183183
query = query.inclusive if included == true
184184
query = query.additional if added == true
185185
amount =
186-
query.where(adjustable: enterprise_fees(order)).map(&:amount).sum(&:to_f).round(2)
186+
prices_sum(query.where(adjustable: enterprise_fees(order)).map(&:amount))
187187
apply_voucher_on_amount(order, amount)
188188
end
189189

lib/reporting/reports/enterprise_fee_summary/enterprise_fees_with_tax_report_by_producer.rb

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,7 @@ def order_cycle_totals_row
232232
line_item.supplier_id == supplier_id
233233
end
234234

235-
tax_for_enterprise_fees = rows.map(&:tax).sum(&:to_f).round(2)
235+
tax_for_enterprise_fees = prices_sum(rows.map(&:tax))
236236
total_excl_tax = total_fees_excl_tax(items) + line_items_excl_tax(line_items)
237237
tax = tax_for_enterprise_fees + tax_for_line_items(line_items)
238238
{
@@ -280,18 +280,18 @@ def total_fees_excl_tax(items)
280280
end
281281

282282
def line_items_excl_tax(line_items)
283-
cost_of_line_items(line_items) - line_items.map(&:included_tax).sum(&:to_f).round(2)
283+
cost_of_line_items(line_items) - prices_sum(line_items.map(&:included_tax))
284284
end
285285

286286
def cost_of_line_items(line_items)
287-
line_items.map(&:amount).sum(&:to_f).round(2)
287+
prices_sum(line_items.map(&:amount))
288288
end
289289

290290
# This query gets called twice for each set of line_items, ideally it would be cached.
291291
def tax_for_line_items(line_items)
292-
line_items.map do |line_item|
292+
prices_sum(line_items.map do |line_item|
293293
line_item.adjustments.eligible.tax.map(&:amount).sum(&:to_f)
294-
end.sum(&:to_f).round(2)
294+
end)
295295
end
296296

297297
def included_tax_for_order_ids(order_ids, enterprise_fee_ids)

lib/reporting/reports/orders_and_fulfillment/order_cycle_customer_totals.rb

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,9 @@ def columns
2323
variant: variant_name,
2424

2525
quantity: proc { |line_items| line_items.map(&:quantity).sum(&:to_i) },
26-
item_price: proc { |line_items| line_items.map(&:amount).sum(&:to_f).round(2) },
26+
item_price: proc { |line_items| prices_sum(line_items.map(&:amount)) },
2727
item_fees_price: proc { |line_items|
28-
line_items.map(&:amount_with_adjustments).sum(&:to_f).round(2)
28+
prices_sum(line_items.map(&:amount_with_adjustments))
2929
},
3030
admin_handling_fees: proc { |_line_items| "" },
3131
ship_price: proc { |_line_items| "" },
@@ -68,7 +68,7 @@ def columns
6868
order_number: proc { |line_items| line_items.first.order.number },
6969
date: proc { |line_items| line_items.first.order.completed_at.strftime("%F %T") },
7070
final_weight_volume: proc { |line_items|
71-
line_items.map(&:final_weight_volume).sum(&:to_f).round(2)
71+
line_items.map(&:final_weight_volume).sum(&:to_f).round(3)
7272
},
7373
shipment_state: proc { |line_items| line_items.first.order.shipment_state },
7474
}
@@ -129,8 +129,8 @@ def summary_row(order, rows)
129129
{
130130
hub: rows.last.hub,
131131
customer: rows.last.customer,
132-
item_price: rows.map(&:item_price).sum(&:to_f).round(2),
133-
item_fees_price: rows.map(&:item_fees_price).sum(&:to_f).round(2),
132+
item_price: prices_sum(rows.map(&:item_price)),
133+
item_fees_price: prices_sum(rows.map(&:item_fees_price)),
134134
admin_handling_fees: order.admin_and_handling_total,
135135
ship_price: order.ship_total,
136136
pay_fee_price: order.payment_fee,

0 commit comments

Comments
 (0)