Merge pull request #13571 from pacodelaluna/update_sum_calcultation

Update sum calculation in order_cycle_customer_totals spec
This commit is contained in:
Maikel
2025-10-31 16:03:11 +11:00
committed by GitHub
25 changed files with 90 additions and 80 deletions

View File

@@ -133,7 +133,7 @@ Metrics/ClassLength:
- 'lib/reporting/reports/enterprise_fee_summary/scope.rb'
- 'lib/reporting/reports/xero_invoices/base.rb'
# Offense count: 30
# Offense count: 35
# Configuration parameters: AllowedMethods, AllowedPatterns, Max.
Metrics/CyclomaticComplexity:
Exclude:
@@ -156,6 +156,11 @@ Metrics/CyclomaticComplexity:
- 'app/models/spree/tax_rate.rb'
- 'app/models/spree/zone.rb'
- 'lib/open_food_network/enterprise_issue_validator.rb'
- 'lib/reporting/reports/orders_and_fulfillment/order_cycle_customer_totals.rb'
- 'lib/reporting/reports/orders_and_fulfillment/order_cycle_supplier_totals.rb'
- 'lib/reporting/reports/payments/itemised_payment_totals.rb'
- 'lib/reporting/reports/payments/payment_totals.rb'
- 'lib/reporting/reports/sales_tax/sales_tax_totals_by_producer.rb'
- 'lib/reporting/reports/xero_invoices/base.rb'
- 'lib/spree/core/controller_helpers/order.rb'
- 'lib/spree/core/controller_helpers/respond_with.rb'
@@ -403,13 +408,10 @@ Style/HashSlice:
- 'app/services/product_filters.rb'
- 'lib/reporting/report_row_builder.rb'
# Offense count: 4
# Offense count: 1
# This cop supports unsafe autocorrection (--autocorrect-all).
Style/MapToHash:
Exclude:
- 'lib/reporting/report_query_template.rb'
- 'lib/reporting/report_row_builder.rb'
- 'lib/reporting/reports/enterprise_fee_summary/fee_summary.rb'
- 'lib/tasks/sample_data/user_factory.rb'
# Offense count: 38

View File

@@ -15,7 +15,7 @@ module Reporting
# Here the query_result is already the expected result, so we just create
# a fake columns method to copy the sql result into the row result
def columns
report_data.columns.map { |field| [field.to_sym, proc { |data| data[field] }] }.to_h
report_data.columns.to_h{ |field| [field.to_sym, proc { |data| data[field] }] }
end
def search

View File

@@ -13,7 +13,7 @@ module Reporting
# Compute the query result item into a result row
# We use OpenStruct to it's easier to access the properties
# i.e. row.my_field, rows.sum(&:quantity)
# i.e. row.my_field, rows.map(&:quantity).sum(&:to_i)
def build_row(item)
OpenStruct.new(
report.columns.transform_values do |column_constructor|
@@ -30,7 +30,7 @@ module Reporting
result = row.to_h.select { |k, _v| k.in?(report.fields_to_show) }
unless report.unformatted_render?
result = result.map { |k, v| [k, format_cell(v, k)] }.to_h
result = result.to_h { |k, v| [k, format_cell(v, k)] }
end
OpenStruct.new(result)
end

View File

@@ -87,8 +87,8 @@ module Reporting
# fields_used_in_header: [:first_name, :last_name],
# summary_row: proc do |group_key, items, rows|
# {
# quantity: rows.sum(&:quantity),
# price: "#{rows.sum(&:price)} #{currency_symbol}"
# quantity: rows.map(&:quantity).sum(&:to_i),
# price: "#{rows.map(&:price).sum(&:to_f)} #{currency_symbol}"
# }
# end,
# summary_row_class: "", # by default 'text-bold'

View File

@@ -32,7 +32,7 @@ module Reporting
summary_row: proc do |_key, items, rows|
line_items = items.flatten
{
sum_total: rows.sum(&:sum_total),
sum_total: rows.map(&:sum_total).sum(&:to_f),
total_available: total_available(line_items),
unallocated: remainder(line_items),
max_quantity_excess: max_quantity_excess(line_items)

View File

@@ -57,10 +57,10 @@ module Reporting
end
def max_quantity_amount(line_items)
line_items.sum do |line_item|
line_items.map do |line_item|
max_quantity = [line_item.max_quantity || 0, line_item.quantity || 0].max
max_quantity * scaled_unit_value(line_item.variant)
end
end.sum(&:to_i)
end
def scaled_unit_value(variant)
@@ -94,7 +94,7 @@ module Reporting
end
def total_amount(line_items)
line_items.sum { |li| scaled_final_weight_volume(li) }
line_items.map { |li| scaled_final_weight_volume(li) }.sum(&:to_f)
end
def scaled_final_weight_volume(line_item)

View File

@@ -22,7 +22,7 @@ module Reporting
private
def total_quantity(line_items)
line_items.sum(&:quantity)
line_items.map(&:quantity).sum(&:to_i)
end
end
end

View File

@@ -32,7 +32,7 @@ module Reporting
summary_row: proc do |_key, items, rows|
line_items = items.flatten
{
sum_total: rows.sum(&:sum_total),
sum_total: rows.map(&:sum_total).sum(&:to_f),
units_required: units_required(line_items),
unallocated: remainder(line_items),
max_quantity_excess: max_quantity_excess(line_items)

View File

@@ -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.sum(&:total) },
total_incl_tax: proc { |orders| orders.map(&:total).sum(&:to_f) },
last_completed_order_date: proc { |orders| last_completed_order_date(orders) },
}
end

View File

@@ -158,7 +158,7 @@ module Reporting
end
def enterprise_fees_sum(order)
amount = enterprise_fees(order).sum(:amount)
amount = enterprise_fees(order).map(&:amount).sum(&:to_f)
apply_voucher_on_amount(order, amount)
end
@@ -182,7 +182,7 @@ 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)).sum(:amount)
amount = query.where(adjustable: enterprise_fees(order)).map(&:amount).sum(&:to_f)
apply_voucher_on_amount(order, amount)
end

View File

@@ -232,7 +232,7 @@ module Reporting
line_item.supplier_id == supplier_id
end
tax_for_enterprise_fees = rows.map(&:tax).sum
tax_for_enterprise_fees = rows.map(&:tax).sum(&:to_f)
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.sum(&:included_tax)
cost_of_line_items(line_items) - line_items.map(&:included_tax).sum(&:to_f)
end
def cost_of_line_items(line_items)
line_items.sum(&:amount)
line_items.map(&:amount).sum(&:to_f)
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.sum('amount')
end.sum
line_item.adjustments.eligible.tax.map(&:amount).sum(&:to_f)
end.sum(&:to_f)
end
def included_tax_for_order_ids(order_ids, enterprise_fee_ids)

View File

@@ -46,9 +46,9 @@ module Reporting
# This report calculate data in a different way, so we just encapsulate the result
# in the columns method
def columns
data_attributes.map { |field|
data_attributes.to_h { |field|
[field.to_sym, proc { |data| data.public_send(field) }]
}.to_h
}
end
private

View File

@@ -71,9 +71,9 @@ module Reporting
def total_units(line_items)
return " " if not_all_have_unit?(line_items)
total_units = line_items.sum do |li|
total_units = line_items.map do |li|
li.quantity * li.unit_value / scale_factor(li.variant)
end
end.sum(&:to_f)
total_units.round(3)
end

View File

@@ -8,7 +8,6 @@ module Reporting
# rubocop:disable Metrics/AbcSize
# rubocop:disable Metrics/MethodLength
# rubocop:disable Metrics/PerceivedComplexity
# rubocop:disable Metrics/CyclomaticComplexity
# rubocop:disable Naming/VariableNumber
def columns
{
@@ -23,9 +22,11 @@ module Reporting
product: product_name,
variant: variant_name,
quantity: proc { |line_items| line_items.to_a.sum(&:quantity) },
item_price: proc { |line_items| line_items.sum(&:amount) },
item_fees_price: proc { |line_items| line_items.sum(&:amount_with_adjustments) },
quantity: proc { |line_items| line_items.map(&:quantity).sum(&:to_i) },
item_price: proc { |line_items| line_items.map(&:amount).sum(&:to_f) },
item_fees_price: proc { |line_items|
line_items.map(&:amount_with_adjustments).sum(&:to_f)
},
admin_handling_fees: proc { |_line_items| "" },
ship_price: proc { |_line_items| "" },
pay_fee_price: proc { |_line_items| "" },
@@ -66,14 +67,15 @@ 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.sum(&:final_weight_volume) },
final_weight_volume: proc { |line_items|
line_items.map(&:final_weight_volume).sum(&:to_f)
},
shipment_state: proc { |line_items| line_items.first.order.shipment_state },
}
end
# rubocop:enable Metrics/AbcSize
# rubocop:enable Metrics/MethodLength
# rubocop:enable Metrics/PerceivedComplexity
# rubocop:enable Metrics/CyclomaticComplexity
# rubocop:enable Naming/VariableNumber
def rules
@@ -127,8 +129,8 @@ module Reporting
{
hub: rows.last.hub,
customer: rows.last.customer,
item_price: rows.sum(&:item_price),
item_fees_price: rows.sum(&:item_fees_price),
item_price: rows.map(&:item_price).sum(&:to_f),
item_fees_price: rows.map(&:item_fees_price).sum(&:to_f),
admin_handling_fees: order.admin_and_handling_total,
ship_price: order.ship_total,
pay_fee_price: order.payment_fee,

View File

@@ -10,9 +10,9 @@ module Reporting
producer: supplier_name,
product: product_name,
variant: variant_name,
quantity: proc { |line_items| line_items.to_a.sum(&:quantity) },
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.sum(&:amount) },
total_cost: proc { |line_items| line_items.map(&:amount).sum(&:to_f) },
total_shipping_cost: proc { |_line_items| "" },
shipping_method: proc { |line_items| line_items.first.order.shipping_method&.name }
}
@@ -25,8 +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.sum(&:total_cost),
total_shipping_cost: line_items.map(&:first).map(&:order).uniq.sum(&:ship_total)
total_cost: rows.map(&:total_cost).sum(&:to_f),
total_shipping_cost:
line_items.map(&:first).map(&:order).uniq.map(&:ship_total).sum(&:to_f)
}
end
}

View File

@@ -9,10 +9,10 @@ module Reporting
producer: supplier_name,
product: product_name,
variant: variant_name,
quantity: proc { |line_items| line_items.sum(&:quantity) },
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.sum(&:amount) },
total_cost: proc { |line_items| line_items.map(&:amount).sum(&:to_f) },
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.sum(&:total_units)
rows.map(&:total_units).sum(&:to_f)
else
" "
end
{
quantity: rows.sum(&:quantity),
quantity: rows.map(&:quantity).sum(&:to_i),
total_units: summary_total_units,
total_cost: rows.sum(&:total_cost)
total_cost: rows.map(&:total_cost).sum(&:to_f)
}
end
}

View File

@@ -10,9 +10,9 @@ module Reporting
product: product_name,
variant: variant_name,
hub: hub_name,
quantity: proc { |line_items| line_items.to_a.sum(&:quantity) },
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.sum(&:amount) },
total_cost: proc { |line_items| line_items.map(&:amount).sum(&:to_f) },
shipping_method: proc { |line_items| line_items.first.order.shipping_method&.name }
}
end
@@ -31,8 +31,8 @@ module Reporting
group_by: :hub,
summary_row: proc do |_key, _items, rows|
{
quantity: rows.sum(&:quantity),
total_cost: rows.sum(&:total_cost)
quantity: rows.map(&:quantity).sum(&:to_i),
total_cost: rows.map(&:total_cost).sum(&:to_f)
}
end,
}

View File

@@ -72,8 +72,8 @@ module Reporting
def summary_row
proc do |_key, _items, rows|
{
quantity: rows.sum(&:quantity),
price: rows.sum(&:price)
quantity: rows.map(&:quantity).sum(&:to_i),
price: rows.map(&:price).sum(&:to_f)
}
end
end

View File

@@ -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.to_a.sum(&:item_total) },
shipping_total_price: proc { |orders| orders.sum(&:ship_total) },
product_total_price: proc { |orders| orders.map(&:item_total).sum(&:to_f) },
shipping_total_price: proc { |orders| orders.map(&:ship_total).sum(&:to_f) },
outstanding_balance_price: proc do |orders|
orders.sum { |order| order.outstanding_balance.to_f }
orders.map(&:outstanding_balance).sum(&:to_f)
end,
total_price: proc { |orders| orders.map(&:total).sum }
total_price: proc { |orders| orders.map(&:total).sum(&:to_f) }
}
end
end

View File

@@ -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.to_a.sum(&:item_total) },
shipping_total_price: proc { |orders| orders.sum(&:ship_total) },
total_price: proc { |orders| orders.map(&:total).sum },
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) },
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.sum{ |order| order.outstanding_balance.to_f }
orders.map(&:outstanding_balance).sum(&:to_f)
}
}
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)
}.sum(&:amount)
}.map(&:amount).sum(&:to_f)
end
end
end

View File

@@ -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.sum(&:amount) }
total_price: proc { |payments| payments.map(&:amount).sum(&:to_f) }
}
end
end

View File

@@ -60,8 +60,8 @@ module Reporting
grouped_orders.each do |orders|
voucher_adjustments = calculate_voucher_adjustments(orders)
total_incl_tax = orders.sum(&:total)
total_tax = orders.sum(&:total_tax) + voucher_adjustments
total_incl_tax = orders.map(&:total).sum(&:to_f)
total_tax = orders.map(&:total_tax).sum(&:to_f) + voucher_adjustments
total_excl_tax = total_incl_tax - total_tax
@tax_data[distributor(orders).id] = {

View File

@@ -76,10 +76,11 @@ module Reporting
group_by: :order_cycle,
summary_row: proc do |_key, items, _rows|
line_items = items.flat_map(&:second).flatten.uniq
total_excl_tax = line_items.sum(&:amount) - line_items.sum(&:included_tax)
total_excl_tax =
line_items.map(&:amount).sum(&:to_f) - line_items.map(&:included_tax).sum(&:to_f)
tax = line_items.map do |line_item|
line_item.adjustments.eligible.tax.sum(&:amount)
end.sum
line_item.adjustments.eligible.tax.map(&:amount).sum(&:to_f)
end.sum(&:to_f)
{
total_excl_tax:,
tax:,
@@ -125,16 +126,17 @@ module Reporting
end
def total_excl_tax(query_result_row)
line_items(query_result_row).sum(&:amount) -
line_items(query_result_row).sum(&:included_tax)
line_items(query_result_row).map(&:amount).sum(&:to_f) -
line_items(query_result_row).map(&:included_tax).sum(&:to_f)
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))
.sum(&:amount)
end&.sum
.map(&:amount)
.sum(&:to_f)
end&.sum(&:to_f)
end
def total_incl_tax(query_result_row)

View File

@@ -53,7 +53,7 @@ module Reporting
end
def tax_included_in(line_item)
line_item.adjustments.tax.inclusive.sum(:amount)
line_item.adjustments.tax.inclusive.map(&:amount).sum(&:to_f)
end
end
end

View File

@@ -207,32 +207,35 @@ module Reporting
end
def total_untaxable_products(order)
order.line_items.without_tax.to_a.sum(&:amount)
order.line_items.without_tax.map(&:amount).sum(&:to_f)
end
def total_taxable_products(order)
order.line_items.with_tax.to_a.sum(&:amount)
order.line_items.with_tax.map(&:amount).sum(&:to_f)
end
def total_untaxable_fees(order)
order.all_adjustments.enterprise_fee.where(tax_category: nil).sum(:amount)
order.all_adjustments.enterprise_fee.where(tax_category: nil).map(&:amount).sum(&:to_f)
end
def total_taxable_fees(order)
order.all_adjustments.enterprise_fee.where.not(tax_category: nil).sum(:amount)
order.all_adjustments.enterprise_fee.where.not(tax_category: nil)
.map(&:amount).sum(&:to_f)
end
def total_shipping(order)
order.all_adjustments.shipping.sum(:amount)
order.all_adjustments.shipping.map(&:amount).sum(&:to_f)
end
def total_transaction(order)
order.all_adjustments.payment_fee.sum(:amount)
order.all_adjustments.payment_fee.map(&:amount).sum(&:to_f)
end
def tax_on_shipping_s(order)
tax_on_shipping = order.shipments
.sum("additional_tax_total + included_tax_total").positive?
.map{ |shipment| shipment.additional_tax_total + shipment.included_tax_total }
.sum(&:to_f)
.positive?
if tax_on_shipping
I18n.t(:report_header_gst_on_income)
else
@@ -241,11 +244,11 @@ module Reporting
end
def total_untaxable_admin_adjustments(order)
order.adjustments.admin.where(tax_category: nil).sum(:amount)
order.adjustments.admin.where(tax_category: nil).map(&:amount).sum(&:to_f)
end
def total_taxable_admin_adjustments(order)
order.adjustments.admin.where.not(tax_category: nil).sum(:amount)
order.adjustments.admin.where.not(tax_category: nil).map(&:amount).sum(&:to_f)
end
def detail?