From 8a57977b448937b81a2f058546183e8d269d707a Mon Sep 17 00:00:00 2001 From: Matt-Yorkley <9029026+Matt-Yorkley@users.noreply.github.com> Date: Mon, 4 Nov 2019 00:29:09 +0000 Subject: [PATCH] Increase eager-loading and adjustment calculations --- .../customer_totals_report.rb | 33 ++++++++++++++++--- 1 file changed, 28 insertions(+), 5 deletions(-) diff --git a/lib/open_food_network/orders_and_fulfillments_report/customer_totals_report.rb b/lib/open_food_network/orders_and_fulfillments_report/customer_totals_report.rb index 97e1bbca05..2c323c1eb2 100644 --- a/lib/open_food_network/orders_and_fulfillments_report/customer_totals_report.rb +++ b/lib/open_food_network/orders_and_fulfillments_report/customer_totals_report.rb @@ -61,7 +61,7 @@ module OpenFoodNetwork proc { |_line_items| "" }, proc { |line_items| line_items.sum(&:amount) }, - proc { |line_items| line_items.sum(&:amount_with_adjustments) }, + proc { |line_items| amount_with_adjustments(line_items) }, proc { |line_items| admin_and_handling_total(line_items.first.order) }, proc { |line_items| ship_total(line_items.first.order) }, proc { |line_items| payment_fee(line_items.first.order) }, @@ -115,7 +115,7 @@ module OpenFoodNetwork # rubocop:disable Metrics/MethodLength # rubocop:disable Metrics/PerceivedComplexity def columns - rsa = proc { |line_items| line_items.first.order.shipping_method.andand.delivery? } + rsa = proc { |line_items| shipping_method(line_items).andand.delivery? } [ proc { |line_items| line_items.first.order.distributor.name }, proc { |line_items| @@ -130,7 +130,7 @@ module OpenFoodNetwork proc { |line_items| line_items.sum(&:quantity) }, proc { |line_items| line_items.sum(&:amount) }, - proc { |line_items| line_items.sum(&:amount_with_adjustments) }, + proc { |line_items| amount_with_adjustments(line_items) }, proc { |_line_items| "" }, proc { |_line_items| "" }, proc { |_line_items| "" }, @@ -139,7 +139,7 @@ module OpenFoodNetwork line_items.all? { |li| li.order.paid? } ? I18n.t(:yes) : I18n.t(:no) }, - proc { |line_items| line_items.first.order.shipping_method.andand.name }, + proc { |line_items| shipping_method(line_items).andand.name }, proc { |line_items| rsa.call(line_items) ? I18n.t(:yes) : I18n.t(:no) }, proc { |line_items| @@ -190,7 +190,9 @@ module OpenFoodNetwork # rubocop:enable Metrics/PerceivedComplexity def line_item_includes - [{ variant: :product, order: [:bill_address, :shipments, :order_cycle, :adjustments] }] + [{ variant: { product: :supplier }, + order: [:bill_address, :ship_address, :order_cycle, :adjustments, :payments, + :user, :distributor, shipments: { shipping_rates: :shipping_method }] }] end private @@ -210,6 +212,27 @@ module OpenFoodNetwork order.adjustments.select{ |a| a.originator_type == 'Spree::PaymentMethod' }. map(&:amount).sum.to_f end + + def amount_with_adjustments(line_items) + line_items.map do |line_item| + price_with_adjustments(line_item) * line_item.quantity + end.sum + end + + def price_with_adjustments(line_item) + return 0 if line_item.quantity == 0 + (line_item.price + line_item_adjustments(line_item).map(&:amount).sum / line_item.quantity). + round(2) + end + + def line_item_adjustments(line_item) + line_item.order.adjustments.select{ |a| a.source_id == line_item.id } + end + + def shipping_method(line_items) + line_items.first.order.shipments.first. + andand.shipping_rates.andand.first.andand.shipping_method + end end end end