From 289b75e1434eef8dd2dc384dda1aafc8cd72ac20 Mon Sep 17 00:00:00 2001 From: Kristina Lim Date: Thu, 10 Oct 2019 19:05:39 +0800 Subject: [PATCH] Move specific logic for Supplier Totals by Distributor report to class --- .../orders_and_fulfillments_report.rb | 51 ++------------- .../supplier_totals_by_distributor_report.rb | 65 +++++++++++++++++++ 2 files changed, 72 insertions(+), 44 deletions(-) create mode 100644 lib/open_food_network/orders_and_fulfillments_report/supplier_totals_by_distributor_report.rb diff --git a/lib/open_food_network/orders_and_fulfillments_report.rb b/lib/open_food_network/orders_and_fulfillments_report.rb index a5e631ae05..5fc803806b 100644 --- a/lib/open_food_network/orders_and_fulfillments_report.rb +++ b/lib/open_food_network/orders_and_fulfillments_report.rb @@ -1,5 +1,6 @@ require "open_food_network/reports/line_items" require "open_food_network/orders_and_fulfillments_report/supplier_totals_report" +require "open_food_network/orders_and_fulfillments_report/supplier_totals_by_distributor_report" require 'open_food_network/orders_and_fulfillments_report/default_report' include Spree::ReportsHelper @@ -19,10 +20,8 @@ module OpenFoodNetwork when SupplierTotalsReport::REPORT_TYPE SupplierTotalsReport.new(self).header - when "order_cycle_supplier_totals_by_distributor" - [I18n.t(:report_header_producer), I18n.t(:report_header_product), I18n.t(:report_header_variant), I18n.t(:report_header_to_hub), - I18n.t(:report_header_amount), I18n.t(:report_header_curr_cost_per_unit), I18n.t(:report_header_total_cost), - I18n.t(:report_header_shipping_method)] + when SupplierTotalsByDistributorReport::REPORT_TYPE + SupplierTotalsByDistributorReport.new(self).header when "order_cycle_distributor_totals_by_supplier" [I18n.t(:report_header_hub), I18n.t(:report_header_producer), I18n.t(:report_header_product), I18n.t(:report_header_variant), I18n.t(:report_header_amount), I18n.t(:report_header_curr_cost_per_unit), I18n.t(:report_header_total_cost), @@ -59,35 +58,8 @@ module OpenFoodNetwork case options[:report_type] when SupplierTotalsReport::REPORT_TYPE SupplierTotalsReport.new(self).rules - when "order_cycle_supplier_totals_by_distributor" - [ - { - group_by: proc { |line_item| line_item.variant.product.supplier }, - sort_by: proc { |supplier| supplier.name } - }, - { - group_by: proc { |line_item| line_item.variant.product }, - sort_by: proc { |product| product.name } - }, - { - group_by: proc { |line_item| line_item.variant.full_name }, - sort_by: proc { |full_name| full_name }, - summary_columns: [ - proc { |_line_items| "" }, - proc { |_line_items| "" }, - proc { |_line_items| "" }, - proc { |_line_items| I18n.t('admin.reports.total') }, - proc { |_line_items| "" }, - proc { |_line_items| "" }, - proc { |line_items| line_items.sum(&:amount) }, - proc { |_line_items| "" } - ] - }, - { - group_by: proc { |line_item| line_item.order.distributor }, - sort_by: proc { |distributor| distributor.name } - } - ] + when SupplierTotalsByDistributorReport::REPORT_TYPE + SupplierTotalsByDistributorReport.new(self).rules when "order_cycle_distributor_totals_by_supplier" [ { @@ -194,17 +166,8 @@ module OpenFoodNetwork case options[:report_type] when SupplierTotalsReport::REPORT_TYPE SupplierTotalsReport.new(self).columns - when "order_cycle_supplier_totals_by_distributor" - [ - supplier_name, - proc { |line_items| line_items.first.variant.product.name }, - proc { |line_items| line_items.first.variant.full_name }, - proc { |line_items| line_items.first.order.distributor.name }, - proc { |line_items| line_items.sum(&:quantity) }, - proc { |line_items| line_items.first.price }, - proc { |line_items| line_items.sum(&:amount) }, - proc { |_line_items| I18n.t(:report_header_shipping_method) } - ] + when SupplierTotalsByDistributorReport::REPORT_TYPE + SupplierTotalsByDistributorReport.new(self).columns when "order_cycle_distributor_totals_by_supplier" [proc { |line_items| line_items.first.order.distributor.name }, proc { |line_items| line_items.first.variant.product.supplier.name }, diff --git a/lib/open_food_network/orders_and_fulfillments_report/supplier_totals_by_distributor_report.rb b/lib/open_food_network/orders_and_fulfillments_report/supplier_totals_by_distributor_report.rb new file mode 100644 index 0000000000..7124479334 --- /dev/null +++ b/lib/open_food_network/orders_and_fulfillments_report/supplier_totals_by_distributor_report.rb @@ -0,0 +1,65 @@ +module OpenFoodNetwork + class OrdersAndFulfillmentsReport + class SupplierTotalsByDistributorReport + REPORT_TYPE = "order_cycle_supplier_totals_by_distributor" + + attr_reader :context + + delegate :supplier_name, to: :context + + def initialize(context) + @context = context + end + + def header + [I18n.t(:report_header_producer), I18n.t(:report_header_product), I18n.t(:report_header_variant), I18n.t(:report_header_to_hub), + I18n.t(:report_header_amount), I18n.t(:report_header_curr_cost_per_unit), I18n.t(:report_header_total_cost), + I18n.t(:report_header_shipping_method)] + end + + def rules + [ + { + group_by: proc { |line_item| line_item.variant.product.supplier }, + sort_by: proc { |supplier| supplier.name } + }, + { + group_by: proc { |line_item| line_item.variant.product }, + sort_by: proc { |product| product.name } + }, + { + group_by: proc { |line_item| line_item.variant.full_name }, + sort_by: proc { |full_name| full_name }, + summary_columns: [ + proc { |_line_items| "" }, + proc { |_line_items| "" }, + proc { |_line_items| "" }, + proc { |_line_items| I18n.t('admin.reports.total') }, + proc { |_line_items| "" }, + proc { |_line_items| "" }, + proc { |line_items| line_items.sum(&:amount) }, + proc { |_line_items| "" } + ] + }, + { + group_by: proc { |line_item| line_item.order.distributor }, + sort_by: proc { |distributor| distributor.name } + } + ] + end + + def columns + [ + supplier_name, + proc { |line_items| line_items.first.variant.product.name }, + proc { |line_items| line_items.first.variant.full_name }, + proc { |line_items| line_items.first.order.distributor.name }, + proc { |line_items| line_items.sum(&:quantity) }, + proc { |line_items| line_items.first.price }, + proc { |line_items| line_items.sum(&:amount) }, + proc { |_line_items| I18n.t(:report_header_shipping_method) } + ] + end + end + end +end