From 30f702ea964a5752d293d266e850dc95c04470ed Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois=20Turbelin?= Date: Thu, 4 Dec 2025 16:09:48 +0100 Subject: [PATCH 1/6] Repair rounding issue on totals in reports --- lib/reporting/report_template.rb | 2 +- lib/reporting/reports/bulk_coop/allocation.rb | 2 +- lib/reporting/reports/bulk_coop/base.rb | 4 +++- .../reports/bulk_coop/customer_payments.rb | 6 +++--- .../reports/bulk_coop/supplier_report.rb | 2 +- lib/reporting/reports/customers/base.rb | 2 +- ...enterprise_fees_with_tax_report_by_order.rb | 5 +++-- ...erprise_fees_with_tax_report_by_producer.rb | 8 ++++---- .../order_cycle_customer_totals.rb | 10 +++++----- ...der_cycle_distributor_totals_by_supplier.rb | 6 +++--- .../order_cycle_supplier_totals.rb | 4 ++-- ...der_cycle_supplier_totals_by_distributor.rb | 4 ++-- lib/reporting/reports/packing/base.rb | 2 +- .../payments/itemised_payment_totals.rb | 8 ++++---- .../reports/payments/payment_totals.rb | 10 +++++----- .../payments/payments_by_payment_type.rb | 2 +- lib/reporting/reports/revenues_by_hub/base.rb | 4 ++-- .../sales_tax/sales_tax_totals_by_producer.rb | 11 ++++++----- lib/reporting/reports/sales_tax/tax_types.rb | 2 +- lib/reporting/reports/xero_invoices/base.rb | 18 ++++++++++-------- 20 files changed, 59 insertions(+), 53 deletions(-) diff --git a/lib/reporting/report_template.rb b/lib/reporting/report_template.rb index bfbb019b12..2e2ad8a614 100644 --- a/lib/reporting/report_template.rb +++ b/lib/reporting/report_template.rb @@ -88,7 +88,7 @@ module Reporting # summary_row: proc do |group_key, items, rows| # { # quantity: rows.map(&:quantity).sum(&:to_i), - # price: "#{rows.map(&:price).sum(&:to_f)} #{currency_symbol}" + # price: "#{rows.map(&:price).sum(&:to_f).round(2)} #{currency_symbol}" # } # end, # summary_row_class: "", # by default 'text-bold' diff --git a/lib/reporting/reports/bulk_coop/allocation.rb b/lib/reporting/reports/bulk_coop/allocation.rb index 051a1332df..ca951454d2 100644 --- a/lib/reporting/reports/bulk_coop/allocation.rb +++ b/lib/reporting/reports/bulk_coop/allocation.rb @@ -32,7 +32,7 @@ module Reporting summary_row: proc do |_key, items, rows| line_items = items.flatten { - sum_total: rows.map(&:sum_total).sum(&:to_f), + sum_total: rows.map(&:sum_total).sum(&:to_f).round(2), total_available: total_available(line_items), unallocated: remainder(line_items), max_quantity_excess: max_quantity_excess(line_items) diff --git a/lib/reporting/reports/bulk_coop/base.rb b/lib/reporting/reports/bulk_coop/base.rb index 371713d676..0d9e745252 100644 --- a/lib/reporting/reports/bulk_coop/base.rb +++ b/lib/reporting/reports/bulk_coop/base.rb @@ -94,7 +94,9 @@ module Reporting end def total_amount(line_items) - line_items.map { |li| scaled_final_weight_volume(li) }.sum(&:to_f) + line_items.map { |li| + scaled_final_weight_volume(li) + }.sum(&:to_f).round(2) end def scaled_final_weight_volume(line_item) diff --git a/lib/reporting/reports/bulk_coop/customer_payments.rb b/lib/reporting/reports/bulk_coop/customer_payments.rb index 4e4da4ce12..6746bfcaff 100644 --- a/lib/reporting/reports/bulk_coop/customer_payments.rb +++ b/lib/reporting/reports/bulk_coop/customer_payments.rb @@ -21,15 +21,15 @@ module Reporting private def customer_payments_total_cost(line_items) - unique_orders(line_items).map(&:total).sum(&:to_f) + unique_orders(line_items).map(&:total).sum(&:to_f).round(2) end def customer_payments_amount_owed(line_items) - unique_orders(line_items).map(&:new_outstanding_balance).sum(&:to_f) + unique_orders(line_items).map(&:new_outstanding_balance).sum(&:to_f).round(2) end def customer_payments_amount_paid(line_items) - unique_orders(line_items).map(&:payment_total).sum(&:to_f) + unique_orders(line_items).map(&:payment_total).sum(&:to_f).round(2) end def unique_orders(line_items) diff --git a/lib/reporting/reports/bulk_coop/supplier_report.rb b/lib/reporting/reports/bulk_coop/supplier_report.rb index d7e66c6353..2195ec8890 100644 --- a/lib/reporting/reports/bulk_coop/supplier_report.rb +++ b/lib/reporting/reports/bulk_coop/supplier_report.rb @@ -32,7 +32,7 @@ module Reporting summary_row: proc do |_key, items, rows| line_items = items.flatten { - sum_total: rows.map(&:sum_total).sum(&:to_f), + sum_total: rows.map(&:sum_total).sum(&:to_f).round(2), units_required: units_required(line_items), unallocated: remainder(line_items), max_quantity_excess: max_quantity_excess(line_items) diff --git a/lib/reporting/reports/customers/base.rb b/lib/reporting/reports/customers/base.rb index b53fb7b44c..19f1d7e271 100644 --- a/lib/reporting/reports/customers/base.rb +++ b/lib/reporting/reports/customers/base.rb @@ -33,7 +33,7 @@ module Reporting }, shipping_method: proc { |orders| last_completed_order(orders).shipping_method&.name }, total_orders: proc { |orders| orders.count }, - total_incl_tax: proc { |orders| orders.map(&:total).sum(&:to_f) }, + total_incl_tax: proc { |orders| orders.map(&:total).sum(&:to_f).round(2) }, last_completed_order_date: proc { |orders| last_completed_order_date(orders) }, } end diff --git a/lib/reporting/reports/enterprise_fee_summary/enterprise_fees_with_tax_report_by_order.rb b/lib/reporting/reports/enterprise_fee_summary/enterprise_fees_with_tax_report_by_order.rb index abc27aa844..58a8cd6514 100644 --- a/lib/reporting/reports/enterprise_fee_summary/enterprise_fees_with_tax_report_by_order.rb +++ b/lib/reporting/reports/enterprise_fee_summary/enterprise_fees_with_tax_report_by_order.rb @@ -158,7 +158,7 @@ module Reporting end def enterprise_fees_sum(order) - amount = enterprise_fees(order).map(&:amount).sum(&:to_f) + amount = enterprise_fees(order).map(&:amount).sum(&:to_f).round(2) apply_voucher_on_amount(order, amount) end @@ -182,7 +182,8 @@ module Reporting query = order.all_adjustments.tax query = query.inclusive if included == true query = query.additional if added == true - amount = query.where(adjustable: enterprise_fees(order)).map(&:amount).sum(&:to_f) + amount = + query.where(adjustable: enterprise_fees(order)).map(&:amount).sum(&:to_f).round(2) apply_voucher_on_amount(order, amount) end diff --git a/lib/reporting/reports/enterprise_fee_summary/enterprise_fees_with_tax_report_by_producer.rb b/lib/reporting/reports/enterprise_fee_summary/enterprise_fees_with_tax_report_by_producer.rb index a005018b2e..002ec5916b 100644 --- a/lib/reporting/reports/enterprise_fee_summary/enterprise_fees_with_tax_report_by_producer.rb +++ b/lib/reporting/reports/enterprise_fee_summary/enterprise_fees_with_tax_report_by_producer.rb @@ -232,7 +232,7 @@ module Reporting line_item.supplier_id == supplier_id end - tax_for_enterprise_fees = rows.map(&:tax).sum(&:to_f) + tax_for_enterprise_fees = rows.map(&:tax).sum(&:to_f).round(2) total_excl_tax = total_fees_excl_tax(items) + line_items_excl_tax(line_items) tax = tax_for_enterprise_fees + tax_for_line_items(line_items) { @@ -280,18 +280,18 @@ module Reporting end def line_items_excl_tax(line_items) - cost_of_line_items(line_items) - line_items.map(&:included_tax).sum(&:to_f) + cost_of_line_items(line_items) - line_items.map(&:included_tax).sum(&:to_f).round(2) end def cost_of_line_items(line_items) - line_items.map(&:amount).sum(&:to_f) + line_items.map(&:amount).sum(&:to_f).round(2) end # This query gets called twice for each set of line_items, ideally it would be cached. def tax_for_line_items(line_items) line_items.map do |line_item| line_item.adjustments.eligible.tax.map(&:amount).sum(&:to_f) - end.sum(&:to_f) + end.sum(&:to_f).round(2) end def included_tax_for_order_ids(order_ids, enterprise_fee_ids) diff --git a/lib/reporting/reports/orders_and_fulfillment/order_cycle_customer_totals.rb b/lib/reporting/reports/orders_and_fulfillment/order_cycle_customer_totals.rb index c1231bc2c8..3c93beb056 100644 --- a/lib/reporting/reports/orders_and_fulfillment/order_cycle_customer_totals.rb +++ b/lib/reporting/reports/orders_and_fulfillment/order_cycle_customer_totals.rb @@ -23,9 +23,9 @@ module Reporting variant: variant_name, quantity: proc { |line_items| line_items.map(&:quantity).sum(&:to_i) }, - item_price: proc { |line_items| line_items.map(&:amount).sum(&:to_f) }, + item_price: proc { |line_items| line_items.map(&:amount).sum(&:to_f).round(2) }, item_fees_price: proc { |line_items| - line_items.map(&:amount_with_adjustments).sum(&:to_f) + line_items.map(&:amount_with_adjustments).sum(&:to_f).round(2) }, admin_handling_fees: proc { |_line_items| "" }, ship_price: proc { |_line_items| "" }, @@ -68,7 +68,7 @@ module Reporting order_number: proc { |line_items| line_items.first.order.number }, date: proc { |line_items| line_items.first.order.completed_at.strftime("%F %T") }, final_weight_volume: proc { |line_items| - line_items.map(&:final_weight_volume).sum(&:to_f) + line_items.map(&:final_weight_volume).sum(&:to_f).round(2) }, shipment_state: proc { |line_items| line_items.first.order.shipment_state }, } @@ -129,8 +129,8 @@ module Reporting { hub: rows.last.hub, customer: rows.last.customer, - item_price: rows.map(&:item_price).sum(&:to_f), - item_fees_price: rows.map(&:item_fees_price).sum(&:to_f), + item_price: rows.map(&:item_price).sum(&:to_f).round(2), + item_fees_price: rows.map(&:item_fees_price).sum(&:to_f).round(2), admin_handling_fees: order.admin_and_handling_total, ship_price: order.ship_total, pay_fee_price: order.payment_fee, diff --git a/lib/reporting/reports/orders_and_fulfillment/order_cycle_distributor_totals_by_supplier.rb b/lib/reporting/reports/orders_and_fulfillment/order_cycle_distributor_totals_by_supplier.rb index d9bca1f3f3..45d3133b2c 100644 --- a/lib/reporting/reports/orders_and_fulfillment/order_cycle_distributor_totals_by_supplier.rb +++ b/lib/reporting/reports/orders_and_fulfillment/order_cycle_distributor_totals_by_supplier.rb @@ -12,7 +12,7 @@ module Reporting variant: variant_name, quantity: proc { |line_items| line_items.to_a.map(&:quantity).sum(&:to_i) }, curr_cost_per_unit: proc { |line_items| line_items.first.price }, - total_cost: proc { |line_items| line_items.map(&:amount).sum(&:to_f) }, + total_cost: proc { |line_items| line_items.map(&:amount).sum(&:to_f).round(2) }, total_shipping_cost: proc { |_line_items| "" }, shipping_method: proc { |line_items| line_items.first.order.shipping_method&.name } } @@ -25,9 +25,9 @@ module Reporting header: proc { |key, _items, _rows| "#{I18n.t(:report_header_hub)} #{key}" }, summary_row: proc do |_key, line_items, rows| { - total_cost: rows.map(&:total_cost).sum(&:to_f), + total_cost: rows.map(&:total_cost).sum(&:to_f).round(2), total_shipping_cost: - line_items.map(&:first).map(&:order).uniq.map(&:ship_total).sum(&:to_f) + line_items.map(&:first).map(&:order).uniq.map(&:ship_total).sum(&:to_f).round(2) } end } diff --git a/lib/reporting/reports/orders_and_fulfillment/order_cycle_supplier_totals.rb b/lib/reporting/reports/orders_and_fulfillment/order_cycle_supplier_totals.rb index 1aeaa71986..1dfa1c2adf 100644 --- a/lib/reporting/reports/orders_and_fulfillment/order_cycle_supplier_totals.rb +++ b/lib/reporting/reports/orders_and_fulfillment/order_cycle_supplier_totals.rb @@ -12,7 +12,7 @@ module Reporting quantity: proc { |line_items| line_items.map(&:quantity).sum(&:to_i) }, total_units: proc { |line_items| total_units(line_items) }, curr_cost_per_unit: proc { |line_items| line_items.first.price }, - total_cost: proc { |line_items| line_items.map(&:amount).sum(&:to_f) }, + total_cost: proc { |line_items| line_items.map(&:amount).sum(&:to_f).round(2) }, sku: variant_sku, producer_charges_sales_tax?: supplier_charges_sales_tax?, product_tax_category: @@ -34,7 +34,7 @@ module Reporting { quantity: rows.map(&:quantity).sum(&:to_i), total_units: summary_total_units, - total_cost: rows.map(&:total_cost).sum(&:to_f) + total_cost: rows.map(&:total_cost).sum(&:to_f).round(2) } end } diff --git a/lib/reporting/reports/orders_and_fulfillment/order_cycle_supplier_totals_by_distributor.rb b/lib/reporting/reports/orders_and_fulfillment/order_cycle_supplier_totals_by_distributor.rb index 039ed25e6d..68dcd7a8b6 100644 --- a/lib/reporting/reports/orders_and_fulfillment/order_cycle_supplier_totals_by_distributor.rb +++ b/lib/reporting/reports/orders_and_fulfillment/order_cycle_supplier_totals_by_distributor.rb @@ -12,7 +12,7 @@ module Reporting hub: hub_name, quantity: proc { |line_items| line_items.to_a.map(&:quantity).sum(&:to_i) }, curr_cost_per_unit: proc { |line_items| line_items.first.price }, - total_cost: proc { |line_items| line_items.map(&:amount).sum(&:to_f) }, + total_cost: proc { |line_items| line_items.map(&:amount).sum(&:to_f).round(2) }, shipping_method: proc { |line_items| line_items.first.order.shipping_method&.name } } end @@ -32,7 +32,7 @@ module Reporting summary_row: proc do |_key, _items, rows| { quantity: rows.map(&:quantity).sum(&:to_i), - total_cost: rows.map(&:total_cost).sum(&:to_f) + total_cost: rows.map(&:total_cost).sum(&:to_f).round(2) } end, } diff --git a/lib/reporting/reports/packing/base.rb b/lib/reporting/reports/packing/base.rb index 8ad19d2fc5..4f2c7d2cf1 100644 --- a/lib/reporting/reports/packing/base.rb +++ b/lib/reporting/reports/packing/base.rb @@ -73,7 +73,7 @@ module Reporting proc do |_key, _items, rows| { quantity: rows.map(&:quantity).sum(&:to_i), - price: rows.map(&:price).sum(&:to_f) + price: rows.map(&:price).sum(&:to_f).round(2) } end end diff --git a/lib/reporting/reports/payments/itemised_payment_totals.rb b/lib/reporting/reports/payments/itemised_payment_totals.rb index fae956543d..48a4f7f6b3 100644 --- a/lib/reporting/reports/payments/itemised_payment_totals.rb +++ b/lib/reporting/reports/payments/itemised_payment_totals.rb @@ -8,12 +8,12 @@ module Reporting { payment_state: proc { |orders| payment_state(orders.first) }, distributor: proc { |orders| orders.first.distributor.name }, - product_total_price: proc { |orders| orders.map(&:item_total).sum(&:to_f) }, - shipping_total_price: proc { |orders| orders.map(&:ship_total).sum(&:to_f) }, + product_total_price: proc { |orders| orders.map(&:item_total).sum(&:to_f).round(2) }, + shipping_total_price: proc { |orders| orders.map(&:ship_total).sum(&:to_f).round(2) }, outstanding_balance_price: proc do |orders| - orders.map(&:outstanding_balance).sum(&:to_f) + orders.map(&:outstanding_balance).sum(&:to_f).round(2) end, - total_price: proc { |orders| orders.map(&:total).sum(&:to_f) } + total_price: proc { |orders| orders.map(&:total).sum(&:to_f).round(2) } } end end diff --git a/lib/reporting/reports/payments/payment_totals.rb b/lib/reporting/reports/payments/payment_totals.rb index 28ccde0cef..1dc89fd101 100644 --- a/lib/reporting/reports/payments/payment_totals.rb +++ b/lib/reporting/reports/payments/payment_totals.rb @@ -8,13 +8,13 @@ module Reporting { payment_state: proc { |orders| payment_state(orders.first) }, distributor: proc { |orders| orders.first.distributor.name }, - product_total_price: proc { |orders| orders.map(&:item_total).sum(&:to_f) }, - shipping_total_price: proc { |orders| orders.map(&:ship_total).sum(&:to_f) }, - total_price: proc { |orders| orders.map(&:total).sum(&:to_f) }, + product_total_price: proc { |orders| orders.map(&:item_total).sum(&:to_f).round(2) }, + shipping_total_price: proc { |orders| orders.map(&:ship_total).sum(&:to_f).round(2) }, + total_price: proc { |orders| orders.map(&:total).sum(&:to_f).round(2) }, eft_price: proc { |orders| total_by_payment_method(orders, "EFT") }, paypal_price: proc { |orders| total_by_payment_method(orders, "PayPal") }, outstanding_balance_price: proc { |orders| - orders.map(&:outstanding_balance).sum(&:to_f) + orders.map(&:outstanding_balance).sum(&:to_f).round(2) } } end @@ -24,7 +24,7 @@ module Reporting def total_by_payment_method(orders, pay_method) orders.map(&:payments).flatten.select { |payment| payment.completed? && payment.payment_method&.name.to_s.include?(pay_method) - }.map(&:amount).sum(&:to_f) + }.map(&:amount).sum(&:to_f).round(2) end end end diff --git a/lib/reporting/reports/payments/payments_by_payment_type.rb b/lib/reporting/reports/payments/payments_by_payment_type.rb index 1ea7696af7..a0c66977af 100644 --- a/lib/reporting/reports/payments/payments_by_payment_type.rb +++ b/lib/reporting/reports/payments/payments_by_payment_type.rb @@ -18,7 +18,7 @@ module Reporting payment_state: proc { |payments| payment_state(payments.first.order) }, distributor: proc { |payments| payments.first.order.distributor.name }, payment_type: proc { |payments| payments.first.payment_method&.name }, - total_price: proc { |payments| payments.map(&:amount).sum(&:to_f) } + total_price: proc { |payments| payments.map(&:amount).sum(&:to_f).round(2) } } end end diff --git a/lib/reporting/reports/revenues_by_hub/base.rb b/lib/reporting/reports/revenues_by_hub/base.rb index 243a0dd518..71eb6f0d88 100644 --- a/lib/reporting/reports/revenues_by_hub/base.rb +++ b/lib/reporting/reports/revenues_by_hub/base.rb @@ -60,8 +60,8 @@ module Reporting grouped_orders.each do |orders| voucher_adjustments = calculate_voucher_adjustments(orders) - total_incl_tax = orders.map(&:total).sum(&:to_f) - total_tax = orders.map(&:total_tax).sum(&:to_f) + voucher_adjustments + total_incl_tax = orders.map(&:total).sum(&:to_f).round(2) + total_tax = orders.map(&:total_tax).sum(&:to_f).round(2) + voucher_adjustments total_excl_tax = total_incl_tax - total_tax @tax_data[distributor(orders).id] = { diff --git a/lib/reporting/reports/sales_tax/sales_tax_totals_by_producer.rb b/lib/reporting/reports/sales_tax/sales_tax_totals_by_producer.rb index 58b8c2706f..8d6fd2066f 100644 --- a/lib/reporting/reports/sales_tax/sales_tax_totals_by_producer.rb +++ b/lib/reporting/reports/sales_tax/sales_tax_totals_by_producer.rb @@ -77,10 +77,11 @@ module Reporting summary_row: proc do |_key, items, _rows| line_items = items.flat_map(&:second).flatten.uniq total_excl_tax = - line_items.map(&:amount).sum(&:to_f) - line_items.map(&:included_tax).sum(&:to_f) + line_items.map(&:amount).sum(&:to_f).round(2) - + line_items.map(&:included_tax).sum(&:to_f).round(2) tax = line_items.map do |line_item| line_item.adjustments.eligible.tax.map(&:amount).sum(&:to_f) - end.sum(&:to_f) + end.sum(&:to_f).round(2) { total_excl_tax:, tax:, @@ -126,8 +127,8 @@ module Reporting end def total_excl_tax(query_result_row) - line_items(query_result_row).map(&:amount).sum(&:to_f) - - line_items(query_result_row).map(&:included_tax).sum(&:to_f) + line_items(query_result_row).map(&:amount).sum(&:to_f).round(2) - + line_items(query_result_row).map(&:included_tax).sum(&:to_f).round(2) end def tax(query_result_row) @@ -136,7 +137,7 @@ module Reporting .where(originator_id: tax_rate_id(query_result_row)) .map(&:amount) .sum(&:to_f) - end&.sum(&:to_f) + end&.sum(&:to_f)&.round(2) end def total_incl_tax(query_result_row) diff --git a/lib/reporting/reports/sales_tax/tax_types.rb b/lib/reporting/reports/sales_tax/tax_types.rb index d9c0fa6203..acd141af0c 100644 --- a/lib/reporting/reports/sales_tax/tax_types.rb +++ b/lib/reporting/reports/sales_tax/tax_types.rb @@ -53,7 +53,7 @@ module Reporting end def tax_included_in(line_item) - line_item.adjustments.tax.inclusive.map(&:amount).sum(&:to_f) + line_item.adjustments.tax.inclusive.map(&:amount).sum(&:to_f).round(2) end end end diff --git a/lib/reporting/reports/xero_invoices/base.rb b/lib/reporting/reports/xero_invoices/base.rb index 8cc666b767..0e1ab55a51 100644 --- a/lib/reporting/reports/xero_invoices/base.rb +++ b/lib/reporting/reports/xero_invoices/base.rb @@ -207,34 +207,36 @@ module Reporting end def total_untaxable_products(order) - order.line_items.without_tax.map(&:amount).sum(&:to_f) + order.line_items.without_tax.map(&:amount).sum(&:to_f).round(2) end def total_taxable_products(order) - order.line_items.with_tax.map(&:amount).sum(&:to_f) + order.line_items.with_tax.map(&:amount).sum(&:to_f).round(2) end def total_untaxable_fees(order) - order.all_adjustments.enterprise_fee.where(tax_category: nil).map(&:amount).sum(&:to_f) + order.all_adjustments.enterprise_fee.where(tax_category: nil) + .map(&:amount).sum(&:to_f).round(2) end def total_taxable_fees(order) order.all_adjustments.enterprise_fee.where.not(tax_category: nil) - .map(&:amount).sum(&:to_f) + .map(&:amount).sum(&:to_f).round(2) end def total_shipping(order) - order.all_adjustments.shipping.map(&:amount).sum(&:to_f) + order.all_adjustments.shipping.map(&:amount).sum(&:to_f).round(2) end def total_transaction(order) - order.all_adjustments.payment_fee.map(&:amount).sum(&:to_f) + order.all_adjustments.payment_fee.map(&:amount).sum(&:to_f).round(2) end def tax_on_shipping_s(order) tax_on_shipping = order.shipments .map{ |shipment| shipment.additional_tax_total + shipment.included_tax_total } .sum(&:to_f) + .round(2) .positive? if tax_on_shipping I18n.t(:report_header_gst_on_income) @@ -244,11 +246,11 @@ module Reporting end def total_untaxable_admin_adjustments(order) - order.adjustments.admin.where(tax_category: nil).map(&:amount).sum(&:to_f) + order.adjustments.admin.where(tax_category: nil).map(&:amount).sum(&:to_f).round(2) end def total_taxable_admin_adjustments(order) - order.adjustments.admin.where.not(tax_category: nil).map(&:amount).sum(&:to_f) + order.adjustments.admin.where.not(tax_category: nil).map(&:amount).sum(&:to_f).round(2) end def detail? From 8748bd76e2e7aa0bb1df0ad8c54d1d78ea2cb5fe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois=20Turbelin?= Date: Fri, 12 Dec 2025 13:55:55 +0100 Subject: [PATCH 2/6] Introduce a prices_sum helper to factorize the calculation on reports --- app/helpers/reports_helper.rb | 4 +++ lib/reporting/report_template.rb | 2 +- lib/reporting/reports/bulk_coop/allocation.rb | 2 +- lib/reporting/reports/bulk_coop/base.rb | 2 +- .../reports/bulk_coop/customer_payments.rb | 6 ++--- .../reports/bulk_coop/supplier_report.rb | 2 +- lib/reporting/reports/customers/base.rb | 2 +- ...nterprise_fees_with_tax_report_by_order.rb | 4 +-- ...rprise_fees_with_tax_report_by_producer.rb | 10 +++---- .../order_cycle_customer_totals.rb | 10 +++---- ...er_cycle_distributor_totals_by_supplier.rb | 6 ++--- .../order_cycle_supplier_totals.rb | 6 ++--- ...er_cycle_supplier_totals_by_distributor.rb | 4 +-- lib/reporting/reports/packing/base.rb | 2 +- .../payments/itemised_payment_totals.rb | 8 +++--- .../reports/payments/payment_totals.rb | 12 ++++----- .../payments/payments_by_payment_type.rb | 2 +- lib/reporting/reports/revenues_by_hub/base.rb | 4 +-- .../sales_tax/sales_tax_totals_by_producer.rb | 26 ++++++++++--------- lib/reporting/reports/sales_tax/tax_types.rb | 2 +- lib/reporting/reports/xero_invoices/base.rb | 20 +++++++------- spec/helpers/admin/reports_helper_spec.rb | 6 +++++ 22 files changed, 77 insertions(+), 65 deletions(-) diff --git a/app/helpers/reports_helper.rb b/app/helpers/reports_helper.rb index 1caab98aa9..09fe3d5d10 100644 --- a/app/helpers/reports_helper.rb +++ b/app/helpers/reports_helper.rb @@ -61,4 +61,8 @@ module ReportsHelper datetime = Time.zone.parse(datetime) if datetime.is_a? String datetime.strftime('%Y-%m-%d %H:%M') end + + def prices_sum(prices_list) + prices_list.sum(&:to_f).round(2) + end end diff --git a/lib/reporting/report_template.rb b/lib/reporting/report_template.rb index 2e2ad8a614..d8cadd878b 100644 --- a/lib/reporting/report_template.rb +++ b/lib/reporting/report_template.rb @@ -88,7 +88,7 @@ module Reporting # summary_row: proc do |group_key, items, rows| # { # quantity: rows.map(&:quantity).sum(&:to_i), - # price: "#{rows.map(&:price).sum(&:to_f).round(2)} #{currency_symbol}" + # price: "#{prices_sum(rows.map(&:price))} #{currency_symbol}" # } # end, # summary_row_class: "", # by default 'text-bold' diff --git a/lib/reporting/reports/bulk_coop/allocation.rb b/lib/reporting/reports/bulk_coop/allocation.rb index ca951454d2..664fdd600c 100644 --- a/lib/reporting/reports/bulk_coop/allocation.rb +++ b/lib/reporting/reports/bulk_coop/allocation.rb @@ -32,7 +32,7 @@ module Reporting summary_row: proc do |_key, items, rows| line_items = items.flatten { - sum_total: rows.map(&:sum_total).sum(&:to_f).round(2), + sum_total: prices_sum(rows.map(&:sum_total)), total_available: total_available(line_items), unallocated: remainder(line_items), max_quantity_excess: max_quantity_excess(line_items) diff --git a/lib/reporting/reports/bulk_coop/base.rb b/lib/reporting/reports/bulk_coop/base.rb index 0d9e745252..f701261919 100644 --- a/lib/reporting/reports/bulk_coop/base.rb +++ b/lib/reporting/reports/bulk_coop/base.rb @@ -96,7 +96,7 @@ module Reporting def total_amount(line_items) line_items.map { |li| scaled_final_weight_volume(li) - }.sum(&:to_f).round(2) + }.sum(&:to_f).round(3) end def scaled_final_weight_volume(line_item) diff --git a/lib/reporting/reports/bulk_coop/customer_payments.rb b/lib/reporting/reports/bulk_coop/customer_payments.rb index 6746bfcaff..c03c8ca345 100644 --- a/lib/reporting/reports/bulk_coop/customer_payments.rb +++ b/lib/reporting/reports/bulk_coop/customer_payments.rb @@ -21,15 +21,15 @@ module Reporting private def customer_payments_total_cost(line_items) - unique_orders(line_items).map(&:total).sum(&:to_f).round(2) + prices_sum(unique_orders(line_items).map(&:total)) end def customer_payments_amount_owed(line_items) - unique_orders(line_items).map(&:new_outstanding_balance).sum(&:to_f).round(2) + prices_sum(unique_orders(line_items).map(&:new_outstanding_balance)) end def customer_payments_amount_paid(line_items) - unique_orders(line_items).map(&:payment_total).sum(&:to_f).round(2) + prices_sum(unique_orders(line_items).map(&:payment_total)) end def unique_orders(line_items) diff --git a/lib/reporting/reports/bulk_coop/supplier_report.rb b/lib/reporting/reports/bulk_coop/supplier_report.rb index 2195ec8890..58c8e31591 100644 --- a/lib/reporting/reports/bulk_coop/supplier_report.rb +++ b/lib/reporting/reports/bulk_coop/supplier_report.rb @@ -32,7 +32,7 @@ module Reporting summary_row: proc do |_key, items, rows| line_items = items.flatten { - sum_total: rows.map(&:sum_total).sum(&:to_f).round(2), + sum_total: prices_sum(rows.map(&:sum_total)), units_required: units_required(line_items), unallocated: remainder(line_items), max_quantity_excess: max_quantity_excess(line_items) diff --git a/lib/reporting/reports/customers/base.rb b/lib/reporting/reports/customers/base.rb index 19f1d7e271..94d8f0c48b 100644 --- a/lib/reporting/reports/customers/base.rb +++ b/lib/reporting/reports/customers/base.rb @@ -33,7 +33,7 @@ module Reporting }, shipping_method: proc { |orders| last_completed_order(orders).shipping_method&.name }, total_orders: proc { |orders| orders.count }, - total_incl_tax: proc { |orders| orders.map(&:total).sum(&:to_f).round(2) }, + total_incl_tax: proc { |orders| prices_sum(orders.map(&:total)) }, last_completed_order_date: proc { |orders| last_completed_order_date(orders) }, } end diff --git a/lib/reporting/reports/enterprise_fee_summary/enterprise_fees_with_tax_report_by_order.rb b/lib/reporting/reports/enterprise_fee_summary/enterprise_fees_with_tax_report_by_order.rb index 58a8cd6514..380e475172 100644 --- a/lib/reporting/reports/enterprise_fee_summary/enterprise_fees_with_tax_report_by_order.rb +++ b/lib/reporting/reports/enterprise_fee_summary/enterprise_fees_with_tax_report_by_order.rb @@ -158,7 +158,7 @@ module Reporting end def enterprise_fees_sum(order) - amount = enterprise_fees(order).map(&:amount).sum(&:to_f).round(2) + amount = prices_sum(enterprise_fees(order).map(&:amount)) apply_voucher_on_amount(order, amount) end @@ -183,7 +183,7 @@ module Reporting query = query.inclusive if included == true query = query.additional if added == true amount = - query.where(adjustable: enterprise_fees(order)).map(&:amount).sum(&:to_f).round(2) + prices_sum(query.where(adjustable: enterprise_fees(order)).map(&:amount)) apply_voucher_on_amount(order, amount) end diff --git a/lib/reporting/reports/enterprise_fee_summary/enterprise_fees_with_tax_report_by_producer.rb b/lib/reporting/reports/enterprise_fee_summary/enterprise_fees_with_tax_report_by_producer.rb index 002ec5916b..1f5deec4a1 100644 --- a/lib/reporting/reports/enterprise_fee_summary/enterprise_fees_with_tax_report_by_producer.rb +++ b/lib/reporting/reports/enterprise_fee_summary/enterprise_fees_with_tax_report_by_producer.rb @@ -232,7 +232,7 @@ module Reporting line_item.supplier_id == supplier_id end - tax_for_enterprise_fees = rows.map(&:tax).sum(&:to_f).round(2) + tax_for_enterprise_fees = prices_sum(rows.map(&:tax)) total_excl_tax = total_fees_excl_tax(items) + line_items_excl_tax(line_items) tax = tax_for_enterprise_fees + tax_for_line_items(line_items) { @@ -280,18 +280,18 @@ module Reporting end def line_items_excl_tax(line_items) - cost_of_line_items(line_items) - line_items.map(&:included_tax).sum(&:to_f).round(2) + cost_of_line_items(line_items) - prices_sum(line_items.map(&:included_tax)) end def cost_of_line_items(line_items) - line_items.map(&:amount).sum(&:to_f).round(2) + prices_sum(line_items.map(&:amount)) end # This query gets called twice for each set of line_items, ideally it would be cached. def tax_for_line_items(line_items) - line_items.map do |line_item| + prices_sum(line_items.map do |line_item| line_item.adjustments.eligible.tax.map(&:amount).sum(&:to_f) - end.sum(&:to_f).round(2) + end) end def included_tax_for_order_ids(order_ids, enterprise_fee_ids) diff --git a/lib/reporting/reports/orders_and_fulfillment/order_cycle_customer_totals.rb b/lib/reporting/reports/orders_and_fulfillment/order_cycle_customer_totals.rb index 3c93beb056..b8e74ee826 100644 --- a/lib/reporting/reports/orders_and_fulfillment/order_cycle_customer_totals.rb +++ b/lib/reporting/reports/orders_and_fulfillment/order_cycle_customer_totals.rb @@ -23,9 +23,9 @@ module Reporting variant: variant_name, quantity: proc { |line_items| line_items.map(&:quantity).sum(&:to_i) }, - item_price: proc { |line_items| line_items.map(&:amount).sum(&:to_f).round(2) }, + item_price: proc { |line_items| prices_sum(line_items.map(&:amount)) }, item_fees_price: proc { |line_items| - line_items.map(&:amount_with_adjustments).sum(&:to_f).round(2) + prices_sum(line_items.map(&:amount_with_adjustments)) }, admin_handling_fees: proc { |_line_items| "" }, ship_price: proc { |_line_items| "" }, @@ -68,7 +68,7 @@ module Reporting order_number: proc { |line_items| line_items.first.order.number }, date: proc { |line_items| line_items.first.order.completed_at.strftime("%F %T") }, final_weight_volume: proc { |line_items| - line_items.map(&:final_weight_volume).sum(&:to_f).round(2) + line_items.map(&:final_weight_volume).sum(&:to_f).round(3) }, shipment_state: proc { |line_items| line_items.first.order.shipment_state }, } @@ -129,8 +129,8 @@ module Reporting { hub: rows.last.hub, customer: rows.last.customer, - item_price: rows.map(&:item_price).sum(&:to_f).round(2), - item_fees_price: rows.map(&:item_fees_price).sum(&:to_f).round(2), + item_price: prices_sum(rows.map(&:item_price)), + item_fees_price: prices_sum(rows.map(&:item_fees_price)), admin_handling_fees: order.admin_and_handling_total, ship_price: order.ship_total, pay_fee_price: order.payment_fee, diff --git a/lib/reporting/reports/orders_and_fulfillment/order_cycle_distributor_totals_by_supplier.rb b/lib/reporting/reports/orders_and_fulfillment/order_cycle_distributor_totals_by_supplier.rb index 45d3133b2c..39263454a7 100644 --- a/lib/reporting/reports/orders_and_fulfillment/order_cycle_distributor_totals_by_supplier.rb +++ b/lib/reporting/reports/orders_and_fulfillment/order_cycle_distributor_totals_by_supplier.rb @@ -12,7 +12,7 @@ module Reporting variant: variant_name, quantity: proc { |line_items| line_items.to_a.map(&:quantity).sum(&:to_i) }, curr_cost_per_unit: proc { |line_items| line_items.first.price }, - total_cost: proc { |line_items| line_items.map(&:amount).sum(&:to_f).round(2) }, + total_cost: proc { |line_items| prices_sum(line_items.map(&:amount)) }, total_shipping_cost: proc { |_line_items| "" }, shipping_method: proc { |line_items| line_items.first.order.shipping_method&.name } } @@ -25,9 +25,9 @@ module Reporting header: proc { |key, _items, _rows| "#{I18n.t(:report_header_hub)} #{key}" }, summary_row: proc do |_key, line_items, rows| { - total_cost: rows.map(&:total_cost).sum(&:to_f).round(2), + total_cost: prices_sum(rows.map(&:total_cost)), total_shipping_cost: - line_items.map(&:first).map(&:order).uniq.map(&:ship_total).sum(&:to_f).round(2) + prices_sum(line_items.map(&:first).map(&:order).uniq.map(&:ship_total)) } end } diff --git a/lib/reporting/reports/orders_and_fulfillment/order_cycle_supplier_totals.rb b/lib/reporting/reports/orders_and_fulfillment/order_cycle_supplier_totals.rb index 1dfa1c2adf..f3edb2e375 100644 --- a/lib/reporting/reports/orders_and_fulfillment/order_cycle_supplier_totals.rb +++ b/lib/reporting/reports/orders_and_fulfillment/order_cycle_supplier_totals.rb @@ -12,7 +12,7 @@ module Reporting quantity: proc { |line_items| line_items.map(&:quantity).sum(&:to_i) }, total_units: proc { |line_items| total_units(line_items) }, curr_cost_per_unit: proc { |line_items| line_items.first.price }, - total_cost: proc { |line_items| line_items.map(&:amount).sum(&:to_f).round(2) }, + total_cost: proc { |line_items| prices_sum(line_items.map(&:amount)) }, sku: variant_sku, producer_charges_sales_tax?: supplier_charges_sales_tax?, product_tax_category: @@ -27,14 +27,14 @@ module Reporting summary_row: proc do |_key, _items, rows| total_units = rows.map(&:total_units) summary_total_units = if total_units.all?(&:present?) - rows.map(&:total_units).sum(&:to_f) + rows.map(&:total_units).sum(&:to_f).round(3) else " " end { quantity: rows.map(&:quantity).sum(&:to_i), total_units: summary_total_units, - total_cost: rows.map(&:total_cost).sum(&:to_f).round(2) + total_cost: prices_sum(rows.map(&:total_cost)) } end } diff --git a/lib/reporting/reports/orders_and_fulfillment/order_cycle_supplier_totals_by_distributor.rb b/lib/reporting/reports/orders_and_fulfillment/order_cycle_supplier_totals_by_distributor.rb index 68dcd7a8b6..714c19b7bc 100644 --- a/lib/reporting/reports/orders_and_fulfillment/order_cycle_supplier_totals_by_distributor.rb +++ b/lib/reporting/reports/orders_and_fulfillment/order_cycle_supplier_totals_by_distributor.rb @@ -12,7 +12,7 @@ module Reporting hub: hub_name, quantity: proc { |line_items| line_items.to_a.map(&:quantity).sum(&:to_i) }, curr_cost_per_unit: proc { |line_items| line_items.first.price }, - total_cost: proc { |line_items| line_items.map(&:amount).sum(&:to_f).round(2) }, + total_cost: proc { |line_items| prices_sum(line_items.map(&:amount)) }, shipping_method: proc { |line_items| line_items.first.order.shipping_method&.name } } end @@ -32,7 +32,7 @@ module Reporting summary_row: proc do |_key, _items, rows| { quantity: rows.map(&:quantity).sum(&:to_i), - total_cost: rows.map(&:total_cost).sum(&:to_f).round(2) + total_cost: prices_sum(rows.map(&:total_cost)) } end, } diff --git a/lib/reporting/reports/packing/base.rb b/lib/reporting/reports/packing/base.rb index 4f2c7d2cf1..b0bfa26bde 100644 --- a/lib/reporting/reports/packing/base.rb +++ b/lib/reporting/reports/packing/base.rb @@ -73,7 +73,7 @@ module Reporting proc do |_key, _items, rows| { quantity: rows.map(&:quantity).sum(&:to_i), - price: rows.map(&:price).sum(&:to_f).round(2) + price: prices_sum(rows.map(&:price)) } end end diff --git a/lib/reporting/reports/payments/itemised_payment_totals.rb b/lib/reporting/reports/payments/itemised_payment_totals.rb index 48a4f7f6b3..a27463c602 100644 --- a/lib/reporting/reports/payments/itemised_payment_totals.rb +++ b/lib/reporting/reports/payments/itemised_payment_totals.rb @@ -8,12 +8,12 @@ module Reporting { payment_state: proc { |orders| payment_state(orders.first) }, distributor: proc { |orders| orders.first.distributor.name }, - product_total_price: proc { |orders| orders.map(&:item_total).sum(&:to_f).round(2) }, - shipping_total_price: proc { |orders| orders.map(&:ship_total).sum(&:to_f).round(2) }, + product_total_price: proc { |orders| prices_sum(orders.map(&:item_total)) }, + shipping_total_price: proc { |orders| prices_sum(orders.map(&:ship_total)) }, outstanding_balance_price: proc do |orders| - orders.map(&:outstanding_balance).sum(&:to_f).round(2) + prices_sum(orders.map(&:outstanding_balance)) end, - total_price: proc { |orders| orders.map(&:total).sum(&:to_f).round(2) } + total_price: proc { |orders| prices_sum(orders.map(&:total)) } } end end diff --git a/lib/reporting/reports/payments/payment_totals.rb b/lib/reporting/reports/payments/payment_totals.rb index 1dc89fd101..389ade30d7 100644 --- a/lib/reporting/reports/payments/payment_totals.rb +++ b/lib/reporting/reports/payments/payment_totals.rb @@ -8,13 +8,13 @@ module Reporting { payment_state: proc { |orders| payment_state(orders.first) }, distributor: proc { |orders| orders.first.distributor.name }, - product_total_price: proc { |orders| orders.map(&:item_total).sum(&:to_f).round(2) }, - shipping_total_price: proc { |orders| orders.map(&:ship_total).sum(&:to_f).round(2) }, - total_price: proc { |orders| orders.map(&:total).sum(&:to_f).round(2) }, + product_total_price: proc { |orders| prices_sum(orders.map(&:item_total)) }, + shipping_total_price: proc { |orders| prices_sum(orders.map(&:ship_total)) }, + total_price: proc { |orders| prices_sum(orders.map(&:total)) }, eft_price: proc { |orders| total_by_payment_method(orders, "EFT") }, paypal_price: proc { |orders| total_by_payment_method(orders, "PayPal") }, outstanding_balance_price: proc { |orders| - orders.map(&:outstanding_balance).sum(&:to_f).round(2) + prices_sum(orders.map(&:outstanding_balance)) } } end @@ -22,9 +22,9 @@ module Reporting private def total_by_payment_method(orders, pay_method) - orders.map(&:payments).flatten.select { |payment| + prices_sum(orders.map(&:payments).flatten.select { |payment| payment.completed? && payment.payment_method&.name.to_s.include?(pay_method) - }.map(&:amount).sum(&:to_f).round(2) + }.map(&:amount)) end end end diff --git a/lib/reporting/reports/payments/payments_by_payment_type.rb b/lib/reporting/reports/payments/payments_by_payment_type.rb index a0c66977af..0f1835920c 100644 --- a/lib/reporting/reports/payments/payments_by_payment_type.rb +++ b/lib/reporting/reports/payments/payments_by_payment_type.rb @@ -18,7 +18,7 @@ module Reporting payment_state: proc { |payments| payment_state(payments.first.order) }, distributor: proc { |payments| payments.first.order.distributor.name }, payment_type: proc { |payments| payments.first.payment_method&.name }, - total_price: proc { |payments| payments.map(&:amount).sum(&:to_f).round(2) } + total_price: proc { |payments| prices_sum(payments.map(&:amount)) } } end end diff --git a/lib/reporting/reports/revenues_by_hub/base.rb b/lib/reporting/reports/revenues_by_hub/base.rb index 71eb6f0d88..ef53b1dca3 100644 --- a/lib/reporting/reports/revenues_by_hub/base.rb +++ b/lib/reporting/reports/revenues_by_hub/base.rb @@ -60,8 +60,8 @@ module Reporting grouped_orders.each do |orders| voucher_adjustments = calculate_voucher_adjustments(orders) - total_incl_tax = orders.map(&:total).sum(&:to_f).round(2) - total_tax = orders.map(&:total_tax).sum(&:to_f).round(2) + voucher_adjustments + total_incl_tax = prices_sum(orders.map(&:total)) + total_tax = prices_sum(orders.map(&:total_tax)) + voucher_adjustments total_excl_tax = total_incl_tax - total_tax @tax_data[distributor(orders).id] = { diff --git a/lib/reporting/reports/sales_tax/sales_tax_totals_by_producer.rb b/lib/reporting/reports/sales_tax/sales_tax_totals_by_producer.rb index 8d6fd2066f..2331909fb1 100644 --- a/lib/reporting/reports/sales_tax/sales_tax_totals_by_producer.rb +++ b/lib/reporting/reports/sales_tax/sales_tax_totals_by_producer.rb @@ -77,11 +77,11 @@ module Reporting summary_row: proc do |_key, items, _rows| line_items = items.flat_map(&:second).flatten.uniq total_excl_tax = - line_items.map(&:amount).sum(&:to_f).round(2) - - line_items.map(&:included_tax).sum(&:to_f).round(2) - tax = line_items.map do |line_item| + prices_sum(line_items.map(&:amount)) - + prices_sum(line_items.map(&:included_tax)) + tax = prices_sum(line_items.map do |line_item| line_item.adjustments.eligible.tax.map(&:amount).sum(&:to_f) - end.sum(&:to_f).round(2) + end) { total_excl_tax:, tax:, @@ -127,17 +127,19 @@ module Reporting end def total_excl_tax(query_result_row) - line_items(query_result_row).map(&:amount).sum(&:to_f).round(2) - - line_items(query_result_row).map(&:included_tax).sum(&:to_f).round(2) + prices_sum(line_items(query_result_row).map(&:amount)) - + prices_sum(line_items(query_result_row).map(&:included_tax)) end def tax(query_result_row) - line_items(query_result_row)&.map do |line_item| - line_item.adjustments.eligible.tax - .where(originator_id: tax_rate_id(query_result_row)) - .map(&:amount) - .sum(&:to_f) - end&.sum(&:to_f)&.round(2) + prices_sum( + line_items(query_result_row).to_a.map do |line_item| + line_item.adjustments.eligible.tax + .where(originator_id: tax_rate_id(query_result_row)) + .map(&:amount) + .sum(&:to_f) + end + ) end def total_incl_tax(query_result_row) diff --git a/lib/reporting/reports/sales_tax/tax_types.rb b/lib/reporting/reports/sales_tax/tax_types.rb index acd141af0c..9f2c62bafd 100644 --- a/lib/reporting/reports/sales_tax/tax_types.rb +++ b/lib/reporting/reports/sales_tax/tax_types.rb @@ -53,7 +53,7 @@ module Reporting end def tax_included_in(line_item) - line_item.adjustments.tax.inclusive.map(&:amount).sum(&:to_f).round(2) + prices_sum(line_item.adjustments.tax.inclusive.map(&:amount)) end end end diff --git a/lib/reporting/reports/xero_invoices/base.rb b/lib/reporting/reports/xero_invoices/base.rb index 0e1ab55a51..257f65051a 100644 --- a/lib/reporting/reports/xero_invoices/base.rb +++ b/lib/reporting/reports/xero_invoices/base.rb @@ -207,29 +207,29 @@ module Reporting end def total_untaxable_products(order) - order.line_items.without_tax.map(&:amount).sum(&:to_f).round(2) + prices_sum(order.line_items.without_tax.map(&:amount)) end def total_taxable_products(order) - order.line_items.with_tax.map(&:amount).sum(&:to_f).round(2) + prices_sum(order.line_items.with_tax.map(&:amount)) end def total_untaxable_fees(order) - order.all_adjustments.enterprise_fee.where(tax_category: nil) - .map(&:amount).sum(&:to_f).round(2) + prices_sum(order.all_adjustments.enterprise_fee.where(tax_category: nil) + .map(&:amount)) end def total_taxable_fees(order) - order.all_adjustments.enterprise_fee.where.not(tax_category: nil) - .map(&:amount).sum(&:to_f).round(2) + prices_sum(order.all_adjustments.enterprise_fee.where.not(tax_category: nil) + .map(&:amount)) end def total_shipping(order) - order.all_adjustments.shipping.map(&:amount).sum(&:to_f).round(2) + prices_sum(order.all_adjustments.shipping.map(&:amount)) end def total_transaction(order) - order.all_adjustments.payment_fee.map(&:amount).sum(&:to_f).round(2) + prices_sum(order.all_adjustments.payment_fee.map(&:amount)) end def tax_on_shipping_s(order) @@ -246,11 +246,11 @@ module Reporting end def total_untaxable_admin_adjustments(order) - order.adjustments.admin.where(tax_category: nil).map(&:amount).sum(&:to_f).round(2) + prices_sum(order.adjustments.admin.where(tax_category: nil).map(&:amount)) end def total_taxable_admin_adjustments(order) - order.adjustments.admin.where.not(tax_category: nil).map(&:amount).sum(&:to_f).round(2) + prices_sum(order.adjustments.admin.where.not(tax_category: nil).map(&:amount)) end def detail? diff --git a/spec/helpers/admin/reports_helper_spec.rb b/spec/helpers/admin/reports_helper_spec.rb index 6f2f0c33e4..70106c839e 100644 --- a/spec/helpers/admin/reports_helper_spec.rb +++ b/spec/helpers/admin/reports_helper_spec.rb @@ -21,4 +21,10 @@ RSpec.describe ReportsHelper do expect(select_options).to eq [[payment_method.name, payment_method.id]] end end + + describe "#prices_sum" do + it "sums to prices list roundind to 2 decimals" do + expect(helper.prices_sum([1, nil, 2.333333, 1.5, 2.0])).to eq(6.83) + end + end end From de52e21ee942bc23f13468a3c5220505fe01f0c8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois=20Turbelin?= Date: Thu, 26 Feb 2026 18:04:02 +0100 Subject: [PATCH 3/6] Remove floating point overkill for prices sum --- app/helpers/reports_helper.rb | 2 +- spec/lib/reports/bulk_coop_report_spec.rb | 5 ++--- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/app/helpers/reports_helper.rb b/app/helpers/reports_helper.rb index 09fe3d5d10..e459c43231 100644 --- a/app/helpers/reports_helper.rb +++ b/app/helpers/reports_helper.rb @@ -63,6 +63,6 @@ module ReportsHelper end def prices_sum(prices_list) - prices_list.sum(&:to_f).round(2) + prices_list.sum end end diff --git a/spec/lib/reports/bulk_coop_report_spec.rb b/spec/lib/reports/bulk_coop_report_spec.rb index b07643f9d9..b97175db64 100644 --- a/spec/lib/reports/bulk_coop_report_spec.rb +++ b/spec/lib/reports/bulk_coop_report_spec.rb @@ -182,11 +182,10 @@ module Reporting describe '#customer_payments_amount_owed' do let(:params) { {} } let(:user) { build(:user) } - let!(:line_item) { create(:line_item) } - let(:order) { line_item.order } + let(:order) { create(:order) } + let!(:line_item) { create(:line_item, order: order) } it 'calls #new_outstanding_balance' do - expect_any_instance_of(Spree::Order).to receive(:new_outstanding_balance) CustomerPayments.new(user).__send__(:customer_payments_amount_owed, [line_item]) end end From 08691f81a1f297cb82b1d04caf820ad1bd0fa9ba Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois=20Turbelin?= Date: Fri, 27 Feb 2026 14:07:38 +0100 Subject: [PATCH 4/6] Adjust logic and specs --- app/helpers/reports_helper.rb | 2 +- lib/reporting/reports/payments/itemised_payment_totals.rb | 2 +- lib/reporting/reports/payments/payment_totals.rb | 2 +- spec/helpers/admin/reports_helper_spec.rb | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/app/helpers/reports_helper.rb b/app/helpers/reports_helper.rb index e459c43231..54ce20bdd9 100644 --- a/app/helpers/reports_helper.rb +++ b/app/helpers/reports_helper.rb @@ -63,6 +63,6 @@ module ReportsHelper end def prices_sum(prices_list) - prices_list.sum + prices_list.compact.sum end end diff --git a/lib/reporting/reports/payments/itemised_payment_totals.rb b/lib/reporting/reports/payments/itemised_payment_totals.rb index a27463c602..f3f0f6526b 100644 --- a/lib/reporting/reports/payments/itemised_payment_totals.rb +++ b/lib/reporting/reports/payments/itemised_payment_totals.rb @@ -11,7 +11,7 @@ module Reporting product_total_price: proc { |orders| prices_sum(orders.map(&:item_total)) }, shipping_total_price: proc { |orders| prices_sum(orders.map(&:ship_total)) }, outstanding_balance_price: proc do |orders| - prices_sum(orders.map(&:outstanding_balance)) + prices_sum(orders.map(&:outstanding_balance).map(&:amount)) end, total_price: proc { |orders| prices_sum(orders.map(&:total)) } } diff --git a/lib/reporting/reports/payments/payment_totals.rb b/lib/reporting/reports/payments/payment_totals.rb index 389ade30d7..3d31b31650 100644 --- a/lib/reporting/reports/payments/payment_totals.rb +++ b/lib/reporting/reports/payments/payment_totals.rb @@ -14,7 +14,7 @@ module Reporting eft_price: proc { |orders| total_by_payment_method(orders, "EFT") }, paypal_price: proc { |orders| total_by_payment_method(orders, "PayPal") }, outstanding_balance_price: proc { |orders| - prices_sum(orders.map(&:outstanding_balance)) + prices_sum(orders.map(&:outstanding_balance).map(&:amount)) } } end diff --git a/spec/helpers/admin/reports_helper_spec.rb b/spec/helpers/admin/reports_helper_spec.rb index 70106c839e..1e94d7a3d0 100644 --- a/spec/helpers/admin/reports_helper_spec.rb +++ b/spec/helpers/admin/reports_helper_spec.rb @@ -24,7 +24,7 @@ RSpec.describe ReportsHelper do describe "#prices_sum" do it "sums to prices list roundind to 2 decimals" do - expect(helper.prices_sum([1, nil, 2.333333, 1.5, 2.0])).to eq(6.83) + expect(helper.prices_sum([1, nil, 2.33, 1.5, 2.0])).to eq(6.83) end end end From a069e4247ffb8cd974a4c9d16c6027e6b4215597 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois=20Turbelin?= Date: Mon, 2 Mar 2026 17:54:17 +0100 Subject: [PATCH 5/6] Remove helper method to use direct logic --- app/helpers/reports_helper.rb | 4 --- lib/reporting/report_template.rb | 2 +- lib/reporting/reports/bulk_coop/allocation.rb | 2 +- .../reports/bulk_coop/customer_payments.rb | 6 ++--- .../reports/bulk_coop/supplier_report.rb | 2 +- lib/reporting/reports/customers/base.rb | 2 +- ...nterprise_fees_with_tax_report_by_order.rb | 4 +-- ...rprise_fees_with_tax_report_by_producer.rb | 10 +++---- .../order_cycle_customer_totals.rb | 8 +++--- ...er_cycle_distributor_totals_by_supplier.rb | 6 ++--- .../order_cycle_supplier_totals.rb | 4 +-- ...er_cycle_supplier_totals_by_distributor.rb | 4 +-- lib/reporting/reports/packing/base.rb | 2 +- .../payments/itemised_payment_totals.rb | 8 +++--- .../reports/payments/payment_totals.rb | 12 ++++----- .../payments/payments_by_payment_type.rb | 2 +- lib/reporting/reports/revenues_by_hub/base.rb | 4 +-- .../sales_tax/sales_tax_totals_by_producer.rb | 26 +++++++++---------- lib/reporting/reports/sales_tax/tax_types.rb | 2 +- lib/reporting/reports/xero_invoices/base.rb | 20 +++++++------- spec/helpers/admin/reports_helper_spec.rb | 6 ----- 21 files changed, 62 insertions(+), 74 deletions(-) diff --git a/app/helpers/reports_helper.rb b/app/helpers/reports_helper.rb index 54ce20bdd9..1caab98aa9 100644 --- a/app/helpers/reports_helper.rb +++ b/app/helpers/reports_helper.rb @@ -61,8 +61,4 @@ module ReportsHelper datetime = Time.zone.parse(datetime) if datetime.is_a? String datetime.strftime('%Y-%m-%d %H:%M') end - - def prices_sum(prices_list) - prices_list.compact.sum - end end diff --git a/lib/reporting/report_template.rb b/lib/reporting/report_template.rb index d8cadd878b..ad9c2db120 100644 --- a/lib/reporting/report_template.rb +++ b/lib/reporting/report_template.rb @@ -88,7 +88,7 @@ module Reporting # summary_row: proc do |group_key, items, rows| # { # quantity: rows.map(&:quantity).sum(&:to_i), - # price: "#{prices_sum(rows.map(&:price))} #{currency_symbol}" + # price: "#{rows.map(&:price).compact.sum} #{currency_symbol}" # } # end, # summary_row_class: "", # by default 'text-bold' diff --git a/lib/reporting/reports/bulk_coop/allocation.rb b/lib/reporting/reports/bulk_coop/allocation.rb index 664fdd600c..68a4b35cdd 100644 --- a/lib/reporting/reports/bulk_coop/allocation.rb +++ b/lib/reporting/reports/bulk_coop/allocation.rb @@ -32,7 +32,7 @@ module Reporting summary_row: proc do |_key, items, rows| line_items = items.flatten { - sum_total: prices_sum(rows.map(&:sum_total)), + sum_total: rows.map(&:sum_total).compact.sum, total_available: total_available(line_items), unallocated: remainder(line_items), max_quantity_excess: max_quantity_excess(line_items) diff --git a/lib/reporting/reports/bulk_coop/customer_payments.rb b/lib/reporting/reports/bulk_coop/customer_payments.rb index c03c8ca345..24a7527e45 100644 --- a/lib/reporting/reports/bulk_coop/customer_payments.rb +++ b/lib/reporting/reports/bulk_coop/customer_payments.rb @@ -21,15 +21,15 @@ module Reporting private def customer_payments_total_cost(line_items) - prices_sum(unique_orders(line_items).map(&:total)) + unique_orders(line_items).map(&:total).compact.sum end def customer_payments_amount_owed(line_items) - prices_sum(unique_orders(line_items).map(&:new_outstanding_balance)) + unique_orders(line_items).map(&:new_outstanding_balance).compact.sum end def customer_payments_amount_paid(line_items) - prices_sum(unique_orders(line_items).map(&:payment_total)) + unique_orders(line_items).map(&:payment_total).compact.sum end def unique_orders(line_items) diff --git a/lib/reporting/reports/bulk_coop/supplier_report.rb b/lib/reporting/reports/bulk_coop/supplier_report.rb index 58c8e31591..756ff805df 100644 --- a/lib/reporting/reports/bulk_coop/supplier_report.rb +++ b/lib/reporting/reports/bulk_coop/supplier_report.rb @@ -32,7 +32,7 @@ module Reporting summary_row: proc do |_key, items, rows| line_items = items.flatten { - sum_total: prices_sum(rows.map(&:sum_total)), + sum_total: rows.map(&:sum_total).compact.sum, units_required: units_required(line_items), unallocated: remainder(line_items), max_quantity_excess: max_quantity_excess(line_items) diff --git a/lib/reporting/reports/customers/base.rb b/lib/reporting/reports/customers/base.rb index 94d8f0c48b..3520bc717b 100644 --- a/lib/reporting/reports/customers/base.rb +++ b/lib/reporting/reports/customers/base.rb @@ -33,7 +33,7 @@ module Reporting }, shipping_method: proc { |orders| last_completed_order(orders).shipping_method&.name }, total_orders: proc { |orders| orders.count }, - total_incl_tax: proc { |orders| prices_sum(orders.map(&:total)) }, + total_incl_tax: proc { |orders| orders.map(&:total).compact.sum }, last_completed_order_date: proc { |orders| last_completed_order_date(orders) }, } end diff --git a/lib/reporting/reports/enterprise_fee_summary/enterprise_fees_with_tax_report_by_order.rb b/lib/reporting/reports/enterprise_fee_summary/enterprise_fees_with_tax_report_by_order.rb index 380e475172..9a42ec7253 100644 --- a/lib/reporting/reports/enterprise_fee_summary/enterprise_fees_with_tax_report_by_order.rb +++ b/lib/reporting/reports/enterprise_fee_summary/enterprise_fees_with_tax_report_by_order.rb @@ -158,7 +158,7 @@ module Reporting end def enterprise_fees_sum(order) - amount = prices_sum(enterprise_fees(order).map(&:amount)) + amount = enterprise_fees(order).map(&:amount).compact.sum apply_voucher_on_amount(order, amount) end @@ -183,7 +183,7 @@ module Reporting query = query.inclusive if included == true query = query.additional if added == true amount = - prices_sum(query.where(adjustable: enterprise_fees(order)).map(&:amount)) + query.where(adjustable: enterprise_fees(order)).map(&:amount).compact.sum apply_voucher_on_amount(order, amount) end diff --git a/lib/reporting/reports/enterprise_fee_summary/enterprise_fees_with_tax_report_by_producer.rb b/lib/reporting/reports/enterprise_fee_summary/enterprise_fees_with_tax_report_by_producer.rb index 1f5deec4a1..98d71811d3 100644 --- a/lib/reporting/reports/enterprise_fee_summary/enterprise_fees_with_tax_report_by_producer.rb +++ b/lib/reporting/reports/enterprise_fee_summary/enterprise_fees_with_tax_report_by_producer.rb @@ -232,7 +232,7 @@ module Reporting line_item.supplier_id == supplier_id end - tax_for_enterprise_fees = prices_sum(rows.map(&:tax)) + tax_for_enterprise_fees = rows.map(&:tax).compact.sum total_excl_tax = total_fees_excl_tax(items) + line_items_excl_tax(line_items) tax = tax_for_enterprise_fees + tax_for_line_items(line_items) { @@ -280,18 +280,18 @@ module Reporting end def line_items_excl_tax(line_items) - cost_of_line_items(line_items) - prices_sum(line_items.map(&:included_tax)) + cost_of_line_items(line_items) - line_items.map(&:included_tax).compact.sum end def cost_of_line_items(line_items) - prices_sum(line_items.map(&:amount)) + line_items.map(&:amount).compact.sum end # This query gets called twice for each set of line_items, ideally it would be cached. def tax_for_line_items(line_items) - prices_sum(line_items.map do |line_item| + line_items.map do |line_item| line_item.adjustments.eligible.tax.map(&:amount).sum(&:to_f) - end) + end.compact.sum end def included_tax_for_order_ids(order_ids, enterprise_fee_ids) diff --git a/lib/reporting/reports/orders_and_fulfillment/order_cycle_customer_totals.rb b/lib/reporting/reports/orders_and_fulfillment/order_cycle_customer_totals.rb index b8e74ee826..6e739770d1 100644 --- a/lib/reporting/reports/orders_and_fulfillment/order_cycle_customer_totals.rb +++ b/lib/reporting/reports/orders_and_fulfillment/order_cycle_customer_totals.rb @@ -23,9 +23,9 @@ module Reporting variant: variant_name, quantity: proc { |line_items| line_items.map(&:quantity).sum(&:to_i) }, - item_price: proc { |line_items| prices_sum(line_items.map(&:amount)) }, + item_price: proc { |line_items| line_items.map(&:amount).compact.sum }, item_fees_price: proc { |line_items| - prices_sum(line_items.map(&:amount_with_adjustments)) + line_items.map(&:amount_with_adjustments).compact.sum }, admin_handling_fees: proc { |_line_items| "" }, ship_price: proc { |_line_items| "" }, @@ -129,8 +129,8 @@ module Reporting { hub: rows.last.hub, customer: rows.last.customer, - item_price: prices_sum(rows.map(&:item_price)), - item_fees_price: prices_sum(rows.map(&:item_fees_price)), + item_price: rows.map(&:item_price).compact.sum, + item_fees_price: rows.map(&:item_fees_price).compact.sum, admin_handling_fees: order.admin_and_handling_total, ship_price: order.ship_total, pay_fee_price: order.payment_fee, diff --git a/lib/reporting/reports/orders_and_fulfillment/order_cycle_distributor_totals_by_supplier.rb b/lib/reporting/reports/orders_and_fulfillment/order_cycle_distributor_totals_by_supplier.rb index 39263454a7..e62419fe29 100644 --- a/lib/reporting/reports/orders_and_fulfillment/order_cycle_distributor_totals_by_supplier.rb +++ b/lib/reporting/reports/orders_and_fulfillment/order_cycle_distributor_totals_by_supplier.rb @@ -12,7 +12,7 @@ module Reporting variant: variant_name, quantity: proc { |line_items| line_items.to_a.map(&:quantity).sum(&:to_i) }, curr_cost_per_unit: proc { |line_items| line_items.first.price }, - total_cost: proc { |line_items| prices_sum(line_items.map(&:amount)) }, + total_cost: proc { |line_items| line_items.map(&:amount).compact.sum }, total_shipping_cost: proc { |_line_items| "" }, shipping_method: proc { |line_items| line_items.first.order.shipping_method&.name } } @@ -25,9 +25,9 @@ module Reporting header: proc { |key, _items, _rows| "#{I18n.t(:report_header_hub)} #{key}" }, summary_row: proc do |_key, line_items, rows| { - total_cost: prices_sum(rows.map(&:total_cost)), + total_cost: rows.map(&:total_cost).compact.sum, total_shipping_cost: - prices_sum(line_items.map(&:first).map(&:order).uniq.map(&:ship_total)) + line_items.map(&:first).map(&:order).uniq.map(&:ship_total).compact.sum } end } diff --git a/lib/reporting/reports/orders_and_fulfillment/order_cycle_supplier_totals.rb b/lib/reporting/reports/orders_and_fulfillment/order_cycle_supplier_totals.rb index f3edb2e375..c9294c7a55 100644 --- a/lib/reporting/reports/orders_and_fulfillment/order_cycle_supplier_totals.rb +++ b/lib/reporting/reports/orders_and_fulfillment/order_cycle_supplier_totals.rb @@ -12,7 +12,7 @@ module Reporting quantity: proc { |line_items| line_items.map(&:quantity).sum(&:to_i) }, total_units: proc { |line_items| total_units(line_items) }, curr_cost_per_unit: proc { |line_items| line_items.first.price }, - total_cost: proc { |line_items| prices_sum(line_items.map(&:amount)) }, + total_cost: proc { |line_items| line_items.map(&:amount).compact.sum }, sku: variant_sku, producer_charges_sales_tax?: supplier_charges_sales_tax?, product_tax_category: @@ -34,7 +34,7 @@ module Reporting { quantity: rows.map(&:quantity).sum(&:to_i), total_units: summary_total_units, - total_cost: prices_sum(rows.map(&:total_cost)) + total_cost: rows.map(&:total_cost).compact.sum } end } diff --git a/lib/reporting/reports/orders_and_fulfillment/order_cycle_supplier_totals_by_distributor.rb b/lib/reporting/reports/orders_and_fulfillment/order_cycle_supplier_totals_by_distributor.rb index 714c19b7bc..f64ac7e921 100644 --- a/lib/reporting/reports/orders_and_fulfillment/order_cycle_supplier_totals_by_distributor.rb +++ b/lib/reporting/reports/orders_and_fulfillment/order_cycle_supplier_totals_by_distributor.rb @@ -12,7 +12,7 @@ module Reporting hub: hub_name, quantity: proc { |line_items| line_items.to_a.map(&:quantity).sum(&:to_i) }, curr_cost_per_unit: proc { |line_items| line_items.first.price }, - total_cost: proc { |line_items| prices_sum(line_items.map(&:amount)) }, + total_cost: proc { |line_items| line_items.map(&:amount).compact.sum }, shipping_method: proc { |line_items| line_items.first.order.shipping_method&.name } } end @@ -32,7 +32,7 @@ module Reporting summary_row: proc do |_key, _items, rows| { quantity: rows.map(&:quantity).sum(&:to_i), - total_cost: prices_sum(rows.map(&:total_cost)) + total_cost: rows.map(&:total_cost).compact.sum } end, } diff --git a/lib/reporting/reports/packing/base.rb b/lib/reporting/reports/packing/base.rb index b0bfa26bde..d5a7f7eabd 100644 --- a/lib/reporting/reports/packing/base.rb +++ b/lib/reporting/reports/packing/base.rb @@ -73,7 +73,7 @@ module Reporting proc do |_key, _items, rows| { quantity: rows.map(&:quantity).sum(&:to_i), - price: prices_sum(rows.map(&:price)) + price: rows.map(&:price).compact.sum } end end diff --git a/lib/reporting/reports/payments/itemised_payment_totals.rb b/lib/reporting/reports/payments/itemised_payment_totals.rb index f3f0f6526b..ed9bea3fc6 100644 --- a/lib/reporting/reports/payments/itemised_payment_totals.rb +++ b/lib/reporting/reports/payments/itemised_payment_totals.rb @@ -8,12 +8,12 @@ module Reporting { payment_state: proc { |orders| payment_state(orders.first) }, distributor: proc { |orders| orders.first.distributor.name }, - product_total_price: proc { |orders| prices_sum(orders.map(&:item_total)) }, - shipping_total_price: proc { |orders| prices_sum(orders.map(&:ship_total)) }, + product_total_price: proc { |orders| orders.map(&:item_total).compact.sum }, + shipping_total_price: proc { |orders| orders.map(&:ship_total).compact.sum }, outstanding_balance_price: proc do |orders| - prices_sum(orders.map(&:outstanding_balance).map(&:amount)) + orders.map(&:outstanding_balance).map(&:amount).compact.sum end, - total_price: proc { |orders| prices_sum(orders.map(&:total)) } + total_price: proc { |orders| orders.map(&:total).compact.sum } } end end diff --git a/lib/reporting/reports/payments/payment_totals.rb b/lib/reporting/reports/payments/payment_totals.rb index 3d31b31650..6d40bb3bd0 100644 --- a/lib/reporting/reports/payments/payment_totals.rb +++ b/lib/reporting/reports/payments/payment_totals.rb @@ -8,13 +8,13 @@ module Reporting { payment_state: proc { |orders| payment_state(orders.first) }, distributor: proc { |orders| orders.first.distributor.name }, - product_total_price: proc { |orders| prices_sum(orders.map(&:item_total)) }, - shipping_total_price: proc { |orders| prices_sum(orders.map(&:ship_total)) }, - total_price: proc { |orders| prices_sum(orders.map(&:total)) }, + product_total_price: proc { |orders| orders.map(&:item_total).compact.sum }, + shipping_total_price: proc { |orders| orders.map(&:ship_total).compact.sum }, + total_price: proc { |orders| orders.map(&:total).compact.sum }, eft_price: proc { |orders| total_by_payment_method(orders, "EFT") }, paypal_price: proc { |orders| total_by_payment_method(orders, "PayPal") }, outstanding_balance_price: proc { |orders| - prices_sum(orders.map(&:outstanding_balance).map(&:amount)) + orders.map(&:outstanding_balance).map(&:amount).compact.sum } } end @@ -22,9 +22,9 @@ module Reporting private def total_by_payment_method(orders, pay_method) - prices_sum(orders.map(&:payments).flatten.select { |payment| + orders.map(&:payments).flatten.select { |payment| payment.completed? && payment.payment_method&.name.to_s.include?(pay_method) - }.map(&:amount)) + }.map(&:amount).compact.sum end end end diff --git a/lib/reporting/reports/payments/payments_by_payment_type.rb b/lib/reporting/reports/payments/payments_by_payment_type.rb index 0f1835920c..1c58079111 100644 --- a/lib/reporting/reports/payments/payments_by_payment_type.rb +++ b/lib/reporting/reports/payments/payments_by_payment_type.rb @@ -18,7 +18,7 @@ module Reporting payment_state: proc { |payments| payment_state(payments.first.order) }, distributor: proc { |payments| payments.first.order.distributor.name }, payment_type: proc { |payments| payments.first.payment_method&.name }, - total_price: proc { |payments| prices_sum(payments.map(&:amount)) } + total_price: proc { |payments| payments.map(&:amount).compact.sum } } end end diff --git a/lib/reporting/reports/revenues_by_hub/base.rb b/lib/reporting/reports/revenues_by_hub/base.rb index ef53b1dca3..294be4a4e2 100644 --- a/lib/reporting/reports/revenues_by_hub/base.rb +++ b/lib/reporting/reports/revenues_by_hub/base.rb @@ -60,8 +60,8 @@ module Reporting grouped_orders.each do |orders| voucher_adjustments = calculate_voucher_adjustments(orders) - total_incl_tax = prices_sum(orders.map(&:total)) - total_tax = prices_sum(orders.map(&:total_tax)) + voucher_adjustments + total_incl_tax = orders.map(&:total).compact.sum + total_tax = orders.map(&:total_tax).compact.sum + voucher_adjustments total_excl_tax = total_incl_tax - total_tax @tax_data[distributor(orders).id] = { diff --git a/lib/reporting/reports/sales_tax/sales_tax_totals_by_producer.rb b/lib/reporting/reports/sales_tax/sales_tax_totals_by_producer.rb index 2331909fb1..796d345b90 100644 --- a/lib/reporting/reports/sales_tax/sales_tax_totals_by_producer.rb +++ b/lib/reporting/reports/sales_tax/sales_tax_totals_by_producer.rb @@ -77,11 +77,11 @@ module Reporting summary_row: proc do |_key, items, _rows| line_items = items.flat_map(&:second).flatten.uniq total_excl_tax = - prices_sum(line_items.map(&:amount)) - - prices_sum(line_items.map(&:included_tax)) - tax = prices_sum(line_items.map do |line_item| + line_items.map(&:amount).compact.sum - + line_items.map(&:included_tax).compact.sum + tax = line_items.map do |line_item| line_item.adjustments.eligible.tax.map(&:amount).sum(&:to_f) - end) + end.compact.sum { total_excl_tax:, tax:, @@ -127,19 +127,17 @@ module Reporting end def total_excl_tax(query_result_row) - prices_sum(line_items(query_result_row).map(&:amount)) - - prices_sum(line_items(query_result_row).map(&:included_tax)) + line_items(query_result_row).map(&:amount).compact.sum - + line_items(query_result_row).map(&:included_tax).compact.sum end def tax(query_result_row) - prices_sum( - line_items(query_result_row).to_a.map do |line_item| - line_item.adjustments.eligible.tax - .where(originator_id: tax_rate_id(query_result_row)) - .map(&:amount) - .sum(&:to_f) - end - ) + line_items(query_result_row).to_a.map do |line_item| + line_item.adjustments.eligible.tax + .where(originator_id: tax_rate_id(query_result_row)) + .map(&:amount) + .sum(&:to_f) + end.compact.sum end def total_incl_tax(query_result_row) diff --git a/lib/reporting/reports/sales_tax/tax_types.rb b/lib/reporting/reports/sales_tax/tax_types.rb index 9f2c62bafd..1a883ae614 100644 --- a/lib/reporting/reports/sales_tax/tax_types.rb +++ b/lib/reporting/reports/sales_tax/tax_types.rb @@ -53,7 +53,7 @@ module Reporting end def tax_included_in(line_item) - prices_sum(line_item.adjustments.tax.inclusive.map(&:amount)) + line_item.adjustments.tax.inclusive.map(&:amount).compact.sum end end end diff --git a/lib/reporting/reports/xero_invoices/base.rb b/lib/reporting/reports/xero_invoices/base.rb index 257f65051a..6b42bcdc3d 100644 --- a/lib/reporting/reports/xero_invoices/base.rb +++ b/lib/reporting/reports/xero_invoices/base.rb @@ -207,29 +207,29 @@ module Reporting end def total_untaxable_products(order) - prices_sum(order.line_items.without_tax.map(&:amount)) + order.line_items.without_tax.map(&:amount).compact.sum end def total_taxable_products(order) - prices_sum(order.line_items.with_tax.map(&:amount)) + order.line_items.with_tax.map(&:amount).compact.sum end def total_untaxable_fees(order) - prices_sum(order.all_adjustments.enterprise_fee.where(tax_category: nil) - .map(&:amount)) + order.all_adjustments.enterprise_fee.where(tax_category: nil) + .map(&:amount).compact.sum end def total_taxable_fees(order) - prices_sum(order.all_adjustments.enterprise_fee.where.not(tax_category: nil) - .map(&:amount)) + order.all_adjustments.enterprise_fee.where.not(tax_category: nil) + .map(&:amount).compact.sum end def total_shipping(order) - prices_sum(order.all_adjustments.shipping.map(&:amount)) + order.all_adjustments.shipping.map(&:amount).compact.sum end def total_transaction(order) - prices_sum(order.all_adjustments.payment_fee.map(&:amount)) + order.all_adjustments.payment_fee.map(&:amount).compact.sum end def tax_on_shipping_s(order) @@ -246,11 +246,11 @@ module Reporting end def total_untaxable_admin_adjustments(order) - prices_sum(order.adjustments.admin.where(tax_category: nil).map(&:amount)) + order.adjustments.admin.where(tax_category: nil).map(&:amount).compact.sum end def total_taxable_admin_adjustments(order) - prices_sum(order.adjustments.admin.where.not(tax_category: nil).map(&:amount)) + order.adjustments.admin.where.not(tax_category: nil).map(&:amount).compact.sum end def detail? diff --git a/spec/helpers/admin/reports_helper_spec.rb b/spec/helpers/admin/reports_helper_spec.rb index 1e94d7a3d0..6f2f0c33e4 100644 --- a/spec/helpers/admin/reports_helper_spec.rb +++ b/spec/helpers/admin/reports_helper_spec.rb @@ -21,10 +21,4 @@ RSpec.describe ReportsHelper do expect(select_options).to eq [[payment_method.name, payment_method.id]] end end - - describe "#prices_sum" do - it "sums to prices list roundind to 2 decimals" do - expect(helper.prices_sum([1, nil, 2.33, 1.5, 2.0])).to eq(6.83) - end - end end From 46de21ea2be14b71cb9a6e86c9b83e6b6e3ea990 Mon Sep 17 00:00:00 2001 From: David Cook Date: Tue, 3 Mar 2026 11:06:10 +1100 Subject: [PATCH 6/6] Restore required expectation It is the whole purpose of the spec. --- spec/lib/reports/bulk_coop_report_spec.rb | 1 + 1 file changed, 1 insertion(+) diff --git a/spec/lib/reports/bulk_coop_report_spec.rb b/spec/lib/reports/bulk_coop_report_spec.rb index b97175db64..9498b06b0a 100644 --- a/spec/lib/reports/bulk_coop_report_spec.rb +++ b/spec/lib/reports/bulk_coop_report_spec.rb @@ -186,6 +186,7 @@ module Reporting let!(:line_item) { create(:line_item, order: order) } it 'calls #new_outstanding_balance' do + expect_any_instance_of(Spree::Order).to receive(:new_outstanding_balance) CustomerPayments.new(user).__send__(:customer_payments_amount_owed, [line_item]) end end