Move specific logic for Supplier Totals report to class

This commit is contained in:
Kristina Lim
2019-10-10 16:09:39 +08:00
parent 4e0ecdd44c
commit cbfce69a6d
2 changed files with 54 additions and 28 deletions

View File

@@ -1,4 +1,5 @@
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/default_report'
include Spree::ReportsHelper
@@ -17,9 +18,7 @@ module OpenFoodNetwork
case options[:report_type]
when "order_cycle_supplier_totals"
[I18n.t(:report_header_producer), I18n.t(:report_header_product), I18n.t(:report_header_variant), I18n.t(:report_header_amount),
I18n.t(:report_header_total_units), I18n.t(:report_header_curr_cost_per_unit), I18n.t(:report_header_total_cost),
I18n.t(:report_header_status), I18n.t(:report_header_incoming_transport)]
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),
@@ -59,20 +58,7 @@ module OpenFoodNetwork
def rules
case options[:report_type]
when "order_cycle_supplier_totals"
[
{
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 }
}
]
SupplierTotalsReport.new(self).rules
when "order_cycle_supplier_totals_by_distributor"
[
{
@@ -207,17 +193,7 @@ module OpenFoodNetwork
def columns
case options[:report_type]
when "order_cycle_supplier_totals"
[
supplier_name,
product_name,
line_items_name,
proc { |line_items| line_items.sum(&:quantity) },
proc { |line_items| total_units(line_items) },
proc { |line_items| line_items.first.price },
proc { |line_items| line_items.sum(&:amount) },
proc { |_line_items| "" },
proc { |_line_items| I18n.t(:report_header_incoming_transport) }
]
SupplierTotalsReport.new(self).columns
when "order_cycle_supplier_totals_by_distributor"
[
supplier_name,

View File

@@ -0,0 +1,50 @@
module OpenFoodNetwork
class OrdersAndFulfillmentsReport
class SupplierTotalsReport
attr_reader :context
delegate :supplier_name, :product_name, :line_items_name, :total_units, 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_amount),
I18n.t(:report_header_total_units), I18n.t(:report_header_curr_cost_per_unit), I18n.t(:report_header_total_cost),
I18n.t(:report_header_status), I18n.t(:report_header_incoming_transport)]
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 }
}
]
end
def columns
[
supplier_name,
product_name,
line_items_name,
proc { |line_items| line_items.sum(&:quantity) },
proc { |line_items| total_units(line_items) },
proc { |line_items| line_items.first.price },
proc { |line_items| line_items.sum(&:amount) },
proc { |_line_items| "" },
proc { |_line_items| I18n.t(:report_header_incoming_transport) }
]
end
end
end
end