Open Source Platform for DTC, B2B Commerce, Marketplaces & Omnichannel. REST APIs, TypeScript SDKs, and production-ready Next.js storefront. Self-host it. Own your data. No vendor lock-in. Zero platform fees.
Ruby b2b-commercee-commerceecommerceecommerce-apiecommerce-frameworkecommerce-platformheadlessheadless-commerceheadless-ecommercemarketplacemulti-tenantmulti-vendormulti-vendor-ecommerceopen-sourcespree-commerce
Fixed N+1s and overall performance of SalesTotal report for larger da… (#13358)
Damian Legawiec2f585a3bed5f
diff --git a/core/app/models/spree/line_item.rb b/core/app/models/spree/line_item.rbindex c12829a919..bac7102f1d 100644--- a/core/app/models/spree/line_item.rb+++ b/core/app/models/spree/line_item.rb@@ -163,16 +163,22 @@ module Spree# Skip cancelled shipmentsreturn BigDecimal('0') if shipment.canceled?- # Get all inventory units in this shipment for this line item- line_item_units = shipment.inventory_units.where(line_item_id: id).count+ # Skip shipments with no cost/zero cost+ return BigDecimal('0') if shipment.cost.zero?# Get total inventory units in this shipment- total_units = shipment.inventory_units.count+ total_units = shipment.inventory_units++ # Calculate proportional shipping cost+ return BigDecimal('0') if total_units.empty?++ # Get all inventory units in this shipment for this line item+ line_item_units = shipment.inventory_units.find_all { |unit| unit.line_item_id == id }.count# Calculate proportional shipping cost- return BigDecimal('0') if total_units.zero? || line_item_units.zero? || shipment.cost.zero?+ return BigDecimal('0') if line_item_units.zero?- shipment.cost * (line_item_units.to_d / total_units)+ shipment.cost * (line_item_units.to_d / total_units.count)endenddiff --git a/core/app/models/spree/reports/sales_total.rb b/core/app/models/spree/reports/sales_total.rbindex dfa97824fb..e37a688bf0 100644--- a/core/app/models/spree/reports/sales_total.rb+++ b/core/app/models/spree/reports/sales_total.rb@@ -7,7 +7,7 @@ module Spreecurrency: currency,completed_at: (date_from.to_time.beginning_of_day)..(date_to.to_time.end_of_day))- ).includes(:order, variant: :product)+ ).includes(:order, shipments: :inventory_units, variant: :product)scope = scope.where(vendor_id: vendor.id) if defined?(vendor) && vendor.present?