From 28de125b59c1ddafb2f41d560e71df1d03131a09 Mon Sep 17 00:00:00 2001 From: Rohan Mitchell Date: Fri, 24 Jul 2015 07:00:49 +1000 Subject: [PATCH] Bulk coop allocation report uses unit value rather than variant weight --- lib/open_food_network/bulk_coop_report.rb | 31 +++---------------- .../reports/bulk_coop_report.rb | 18 ++--------- 2 files changed, 8 insertions(+), 41 deletions(-) diff --git a/lib/open_food_network/bulk_coop_report.rb b/lib/open_food_network/bulk_coop_report.rb index 6edc74a4e7..0a88132299 100644 --- a/lib/open_food_network/bulk_coop_report.rb +++ b/lib/open_food_network/bulk_coop_report.rb @@ -1,4 +1,5 @@ require 'open_food_network/reports/bulk_coop_supplier_report' +require 'open_food_network/reports/bulk_coop_allocation_report' module OpenFoodNetwork class BulkCoopReport @@ -8,6 +9,7 @@ module OpenFoodNetwork @user = user @supplier_report = OpenFoodNetwork::Reports::BulkCoopSupplierReport.new + @allocation_report = OpenFoodNetwork::Reports::BulkCoopAllocationReport.new end def header @@ -15,7 +17,7 @@ module OpenFoodNetwork when "bulk_coop_supplier_report" @supplier_report.header when "bulk_coop_allocation" - ["Customer", "Product", "Unit Size", "Variant", "Weight", "Sum Total", "Sum Max Total", "Total Allocated", "Remainder"] + @allocation_report.header when "bulk_coop_packing_sheets" ["Customer", "Product", "Variant", "Sum Total"] when "bulk_coop_customer_payments" @@ -40,23 +42,8 @@ module OpenFoodNetwork case params[:report_type] when "bulk_coop_supplier_report" @supplier_report.rules - when "bulk_coop_allocation" - [ { group_by: proc { |li| li.variant.product }, - sort_by: proc { |product| product.name }, - summary_columns: [ proc { |lis| "TOTAL" }, - proc { |lis| lis.first.variant.product.name }, - proc { |lis| lis.first.variant.product.group_buy ? (lis.first.variant.product.group_buy_unit_size || 0.0) : "" }, - proc { |lis| "" }, - proc { |lis| "" }, - proc { |lis| lis.sum { |li| li.quantity * (li.variant.weight || 0) } }, - proc { |lis| lis.sum { |li| (li.max_quantity || 0) * (li.variant.weight || 0) } }, - proc { |lis| ( (lis.first.variant.product.group_buy_unit_size || 0).zero? ? 0 : ( lis.sum { |li| ( [li.max_quantity || 0, li.quantity || 0].max ) * (li.variant.weight || 0) } / lis.first.variant.product.group_buy_unit_size ) ).floor * (lis.first.variant.product.group_buy_unit_size || 0) }, - proc { |lis| lis.sum { |li| ( [li.max_quantity || 0, li.quantity || 0].max ) * (li.variant.weight || 0) } - ( ( (lis.first.variant.product.group_buy_unit_size || 0).zero? ? 0 : ( lis.sum { |li| ( [li.max_quantity || 0, li.quantity || 0].max ) * (li.variant.weight || 0) } / lis.first.variant.product.group_buy_unit_size ) ).floor * (lis.first.variant.product.group_buy_unit_size || 0) ) } ] }, - { group_by: proc { |li| li.variant }, - sort_by: proc { |variant| variant.full_name } }, - { group_by: proc { |li| li.order }, - sort_by: proc { |order| order.to_s } } ] + @allocation_report.rules when "bulk_coop_packing_sheets" [ { group_by: proc { |li| li.variant.product }, sort_by: proc { |product| product.name } }, @@ -91,15 +78,7 @@ module OpenFoodNetwork when "bulk_coop_supplier_report" @supplier_report.columns when "bulk_coop_allocation" - [ proc { |lis| lis.first.order.bill_address.firstname + " " + lis.first.order.bill_address.lastname }, - proc { |lis| lis.first.variant.product.name }, - proc { |lis| lis.first.variant.product.group_buy ? (lis.first.variant.product.group_buy_unit_size || 0.0) : "" }, - proc { |lis| lis.first.variant.full_name }, - proc { |lis| lis.first.variant.weight || 0 }, - proc { |lis| lis.sum { |li| li.quantity } }, - proc { |lis| lis.sum { |li| li.max_quantity || 0 } }, - proc { |lis| "" }, - proc { |lis| "" } ] + @allocation_report.columns when "bulk_coop_packing_sheets" [ proc { |lis| lis.first.order.bill_address.firstname + " " + lis.first.order.bill_address.lastname }, proc { |lis| lis.first.variant.product.name }, diff --git a/lib/open_food_network/reports/bulk_coop_report.rb b/lib/open_food_network/reports/bulk_coop_report.rb index 5faeb58033..877d5c4280 100644 --- a/lib/open_food_network/reports/bulk_coop_report.rb +++ b/lib/open_food_network/reports/bulk_coop_report.rb @@ -45,24 +45,12 @@ module OpenFoodNetwork::Reports end end - def total_allocated(lis) - # Proposed new factoring: - # units_required * group_buy_unit_size - - num_units = if (lis.first.variant.product.group_buy_unit_size || 0).zero? - 0 - else - max_weight = lis.sum { |li| ( [li.max_quantity || 0, li.quantity || 0].max ) * (li.variant.weight || 0) } - group_buy_unit_size = lis.first.variant.product.group_buy_unit_size - - (max_weight / group_buy_unit_size).floor - end - - num_units * (lis.first.variant.product.group_buy_unit_size || 0) + def total_available(lis) + units_required(lis) * group_buy_unit_size(lis) end def remainder(lis) - (units_required(lis) * group_buy_unit_size(lis)) - max_quantity_amount(lis) + total_available(lis) - max_quantity_amount(lis) end def max_quantity_amount(lis)