Updating reports to use naming information from line_items directly, rather than from the original variant

This commit is contained in:
Rob Harrington
2015-09-25 18:53:03 +10:00
parent 795830d94d
commit 40972cc6ec
11 changed files with 129 additions and 117 deletions

View File

@@ -94,6 +94,6 @@ Spree::Variant.class_eval do
private
def update_weight_from_unit_value
self.weight = unit_value / 1000 if self.product.variant_unit == 'weight' && unit_value.present?
self.weight = weight_from_unit_value
end
end

View File

@@ -43,31 +43,31 @@ module OpenFoodNetwork
when "bulk_coop_allocation"
@allocation_report.rules
when "bulk_coop_packing_sheets"
[ { group_by: proc { |li| li.variant.product },
[ { group_by: proc { |li| li.product },
sort_by: proc { |product| product.name } },
{ group_by: proc { |li| li.variant },
sort_by: proc { |variant| variant.full_name } },
{ group_by: proc { |li| li.full_name },
sort_by: proc { |full_name| full_name } },
{ group_by: proc { |li| li.order },
sort_by: proc { |order| order.to_s } } ]
when "bulk_coop_customer_payments"
[ { group_by: proc { |li| li.order },
sort_by: proc { |order| order.completed_at } } ]
else
[ { group_by: proc { |li| li.variant.product.supplier },
[ { group_by: proc { |li| li.product.supplier },
sort_by: proc { |supplier| supplier.name } },
{ group_by: proc { |li| li.variant.product },
{ group_by: proc { |li| li.product },
sort_by: proc { |product| product.name },
summary_columns: [ proc { |lis| lis.first.variant.product.supplier.name },
proc { |lis| lis.first.variant.product.name },
proc { |lis| lis.first.variant.product.group_buy_unit_size || 0.0 },
summary_columns: [ proc { |lis| lis.first.product.supplier.name },
proc { |lis| lis.first.product.name },
proc { |lis| lis.first.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 },
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 } } ]
proc { |lis| lis.sum { |li| li.quantity * (li.weight_from_unit_value || 0) } },
proc { |lis| lis.sum { |li| (li.max_quantity || 0) * (li.weight_from_unit_value || 0) } },
proc { |lis| ( (lis.first.product.group_buy_unit_size || 0).zero? ? 0 : ( lis.sum { |li| ( [li.max_quantity || 0, li.quantity || 0].max ) * (li.weight_from_unit_value || 0) } / lis.first.product.group_buy_unit_size ) ).floor },
proc { |lis| lis.sum { |li| ( [li.max_quantity || 0, li.quantity || 0].max ) * (li.weight_from_unit_value || 0) } - ( ( (lis.first.product.group_buy_unit_size || 0).zero? ? 0 : ( lis.sum { |li| ( [li.max_quantity || 0, li.quantity || 0].max ) * (li.weight_from_unit_value || 0) } / lis.first.product.group_buy_unit_size ) ).floor * (lis.first.product.group_buy_unit_size || 0) ) } ] },
{ group_by: proc { |li| li.full_name },
sort_by: proc { |full_name| full_name } } ]
end
end
@@ -79,8 +79,8 @@ module OpenFoodNetwork
@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 },
proc { |lis| lis.first.variant.full_name },
proc { |lis| lis.first.product.name },
proc { |lis| lis.first.full_name },
proc { |lis| lis.sum { |li| li.quantity } } ]
when "bulk_coop_customer_payments"
[ proc { |lis| lis.first.order.bill_address.firstname + " " + lis.first.order.bill_address.lastname },
@@ -89,12 +89,12 @@ module OpenFoodNetwork
proc { |lis| lis.map { |li| li.order }.uniq.sum { |o| o.outstanding_balance } },
proc { |lis| lis.map { |li| li.order }.uniq.sum { |o| o.payment_total } } ]
else
[ proc { |lis| lis.first.variant.product.supplier.name },
proc { |lis| lis.first.variant.product.name },
proc { |lis| 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.first.product.supplier.name },
proc { |lis| lis.first.product.name },
proc { |lis| lis.first.product.group_buy_unit_size || 0.0 },
proc { |lis| lis.first.full_name },
proc { |lis| lis.first.weight_from_unit_value || 0 },
proc { |lis| lis.sum { |li| li.quantity } },
proc { |lis| lis.sum { |li| li.max_quantity || 0 } },
proc { |lis| "" },
proc { |lis| "" } ]

View File

@@ -20,7 +20,7 @@ module OpenFoodNetwork
order.line_items.each do |line_item|
order_and_distributor_details << [order.created_at, order.id,
order.bill_address.full_name, order.email, order.bill_address.phone, order.bill_address.city,
line_item.product.sku, line_item.product.name, line_item.variant.unit_text, line_item.quantity, line_item.max_quantity, line_item.price * line_item.quantity, line_item.distribution_fee,
line_item.product.sku, line_item.product.name, line_item.unit_text, line_item.quantity, line_item.max_quantity, line_item.price * line_item.quantity, line_item.distribution_fee,
order.payments.first.andand.payment_method.andand.name,
order.distributor.andand.name, order.distributor.address.address1, order.distributor.address.city, order.distributor.address.zipcode, order.special_instructions ]
end

View File

@@ -21,7 +21,7 @@ module OpenFoodNetwork
orders.map { |o| payment_method_row o }
else
orders.map { |o| delivery_row o }
end
end
end
def orders
@@ -33,7 +33,7 @@ module OpenFoodNetwork
end
private
private
def payment_method_row(order)
ba = order.billing_address

View File

@@ -56,19 +56,19 @@ module OpenFoodNetwork
def rules
case params[:report_type]
when "order_cycle_supplier_totals"
[ { group_by: proc { |line_item| line_item.variant.product.supplier },
[ { group_by: proc { |line_item| line_item.product.supplier },
sort_by: proc { |supplier| supplier.name } },
{ group_by: proc { |line_item| line_item.variant.product },
{ group_by: proc { |line_item| line_item.product },
sort_by: proc { |product| product.name } },
{ group_by: proc { |line_item| line_item.variant },
sort_by: proc { |variant| variant.full_name } } ]
{ group_by: proc { |line_item| line_item.full_name },
sort_by: proc { |full_name| full_name } } ]
when "order_cycle_supplier_totals_by_distributor"
[ { group_by: proc { |line_item| line_item.variant.product.supplier },
[ { group_by: proc { |line_item| line_item.product.supplier },
sort_by: proc { |supplier| supplier.name } },
{ group_by: proc { |line_item| line_item.variant.product },
{ group_by: proc { |line_item| line_item.product },
sort_by: proc { |product| product.name } },
{ group_by: proc { |line_item| line_item.variant },
sort_by: proc { |variant| variant.full_name },
{ group_by: proc { |line_item| line_item.full_name },
sort_by: proc { |full_name| full_name },
summary_columns: [ proc { |line_items| "" },
proc { |line_items| "" },
proc { |line_items| "" },
@@ -91,12 +91,12 @@ module OpenFoodNetwork
proc { |line_items| line_items.sum { |li| li.amount } },
proc { |line_items| line_items.map { |li| li.order }.uniq.sum { |o| o.ship_total } },
proc { |line_items| "" } ] },
{ group_by: proc { |line_item| line_item.variant.product.supplier },
{ group_by: proc { |line_item| line_item.product.supplier },
sort_by: proc { |supplier| supplier.name } },
{ group_by: proc { |line_item| line_item.variant.product },
{ group_by: proc { |line_item| line_item.product },
sort_by: proc { |product| product.name } },
{ group_by: proc { |line_item| line_item.variant },
sort_by: proc { |variant| variant.full_name } } ]
{ group_by: proc { |line_item| line_item.full_name },
sort_by: proc { |full_name| full_name } } ]
when "order_cycle_customer_totals"
[ { group_by: proc { |line_item| line_item.order.distributor },
sort_by: proc { |distributor| distributor.name } },
@@ -143,26 +143,26 @@ module OpenFoodNetwork
proc { |line_items| "" }
] },
{ group_by: proc { |line_item| line_item.variant.product },
{ group_by: proc { |line_item| line_item.product },
sort_by: proc { |product| product.name } },
{ group_by: proc { |line_item| line_item.variant },
sort_by: proc { |variant| variant.full_name } } ]
{ group_by: proc { |line_item| line_item.full_name },
sort_by: proc { |full_name| full_name } } ]
else
[ { group_by: proc { |line_item| line_item.variant.product.supplier },
[ { group_by: proc { |line_item| line_item.product.supplier },
sort_by: proc { |supplier| supplier.name } },
{ group_by: proc { |line_item| line_item.variant.product },
{ group_by: proc { |line_item| line_item.product },
sort_by: proc { |product| product.name } },
{ group_by: proc { |line_item| line_item.variant },
sort_by: proc { |variant| variant.full_name } } ]
{ group_by: proc { |line_item| line_item.full_name },
sort_by: proc { |full_name| full_name } } ]
end
end
def columns
case params[:report_type]
when "order_cycle_supplier_totals"
[ proc { |line_items| line_items.first.variant.product.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.product.supplier.name },
proc { |line_items| line_items.first.product.name },
proc { |line_items| line_items.first.full_name },
proc { |line_items| line_items.sum { |li| li.quantity } },
proc { |line_items| total_units(line_items) },
proc { |line_items| line_items.first.price },
@@ -170,9 +170,9 @@ module OpenFoodNetwork
proc { |line_items| "" },
proc { |line_items| "incoming transport" } ]
when "order_cycle_supplier_totals_by_distributor"
[ proc { |line_items| line_items.first.variant.product.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.product.supplier.name },
proc { |line_items| line_items.first.product.name },
proc { |line_items| line_items.first.full_name },
proc { |line_items| line_items.first.order.distributor.name },
proc { |line_items| line_items.sum { |li| li.quantity } },
proc { |line_items| line_items.first.price },
@@ -180,9 +180,9 @@ module OpenFoodNetwork
proc { |line_items| "shipping method" } ]
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 },
proc { |line_items| line_items.first.variant.product.name },
proc { |line_items| line_items.first.variant.full_name },
proc { |line_items| line_items.first.product.supplier.name },
proc { |line_items| line_items.first.product.name },
proc { |line_items| line_items.first.full_name },
proc { |line_items| line_items.sum { |li| li.quantity } },
proc { |line_items| line_items.first.price },
proc { |line_items| line_items.sum { |li| li.amount } },
@@ -195,9 +195,9 @@ module OpenFoodNetwork
proc { |line_items| line_items.first.order.bill_address.firstname + " " + line_items.first.order.bill_address.lastname },
proc { |line_items| line_items.first.order.email },
proc { |line_items| line_items.first.order.bill_address.phone },
proc { |line_items| line_items.first.variant.product.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.product.supplier.name },
proc { |line_items| line_items.first.product.name },
proc { |line_items| line_items.first.full_name },
proc { |line_items| line_items.sum { |li| li.quantity } },
proc { |line_items| line_items.sum { |li| li.amount } },
@@ -217,7 +217,7 @@ module OpenFoodNetwork
proc { |line_items| line_items.first.order.ship_address.andand.state if rsa.call(line_items) },
proc { |line_items| "" },
proc { |line_items| line_items.first.variant.product.sku },
proc { |line_items| line_items.first.product.sku },
proc { |line_items| line_items.first.order.order_cycle.andand.name },
proc { |line_items| line_items.first.order.payments.first.andand.payment_method.andand.name },
@@ -230,9 +230,9 @@ module OpenFoodNetwork
proc { |line_items| line_items.first.order.bill_address.andand.zipcode },
proc { |line_items| line_items.first.order.bill_address.andand.state } ]
else
[ proc { |line_items| line_items.first.variant.product.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.product.supplier.name },
proc { |line_items| line_items.first.product.name },
proc { |line_items| line_items.first.full_name },
proc { |line_items| line_items.sum { |li| li.quantity } },
proc { |line_items| line_items.first.price },
proc { |line_items| line_items.sum { |li| li.quantity * li.price } },
@@ -244,10 +244,10 @@ module OpenFoodNetwork
private
def total_units(line_items)
return " " if line_items.map{ |li| li.variant.unit_value.nil? }.any?
return " " if line_items.map{ |li| li.unit_value.nil? }.any?
total_units = line_items.sum do |li|
scale_factor = ( li.product.variant_unit == 'weight' ? 1000 : 1 )
li.quantity * li.variant.unit_value / scale_factor
li.quantity * li.unit_value / scale_factor
end
total_units.round(3)
end

View File

@@ -49,17 +49,19 @@ module OpenFoodNetwork
proc { |line_items| "" },
proc { |line_items| line_items.sum { |li| li.quantity } },
proc { |line_items| "" } ] },
{ group_by: proc { |line_item| line_item.variant.product.supplier },
{ group_by: proc { |line_item| line_item.product.supplier },
sort_by: proc { |supplier| supplier.name } },
{ group_by: proc { |line_item| line_item.variant },
sort_by: proc { |variant| variant.product.name } } ]
{ group_by: proc { |line_item| line_item.product },
sort_by: proc { |product| product.name } },
{ group_by: proc { |line_item| line_item.full_name },
sort_by: proc { |full_name| full_name } } ]
else
# supplier_rows orders
# table_items = supplier_rows orders
#
[ { group_by: proc { |line_item| line_item.order.distributor },
sort_by: proc { |distributor| distributor.name } },
{ group_by: proc { |line_item| line_item.variant.product.supplier },
{ group_by: proc { |line_item| line_item.product.supplier },
sort_by: proc { |supplier| supplier.name },
summary_columns: [ proc { |line_items| "" },
proc { |line_items| "" },
@@ -70,8 +72,10 @@ module OpenFoodNetwork
proc { |line_items| "" },
proc { |line_items| line_items.sum { |li| li.quantity } },
proc { |line_items| "" } ] },
{ group_by: proc { |line_item| line_item.variant },
sort_by: proc { |variant| variant.product.name } } ]
{ group_by: proc { |line_item| line_item.product },
sort_by: proc { |product| product.name } },
{ group_by: proc { |line_item| line_item.full_name },
sort_by: proc { |full_name| full_name } } ]
end
end
@@ -81,21 +85,21 @@ module OpenFoodNetwork
proc { |line_items| customer_code(line_items.first.order.email) },
proc { |line_items| line_items.first.order.bill_address.firstname },
proc { |line_items| line_items.first.order.bill_address.lastname },
proc { |line_items| line_items.first.variant.product.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.product.supplier.name },
proc { |line_items| line_items.first.product.name },
proc { |line_items| line_items.first.full_name },
proc { |line_items| line_items.sum { |li| li.quantity } },
proc { |line_items| is_temperature_controlled?(line_items.first) }
]
else
[
proc { |line_items| line_items.first.order.distributor.name },
proc { |line_items| line_items.first.variant.product.supplier.name },
proc { |line_items| line_items.first.product.supplier.name },
proc { |line_items| customer_code(line_items.first.order.email) },
proc { |line_items| line_items.first.order.bill_address.firstname },
proc { |line_items| line_items.first.order.bill_address.lastname },
proc { |line_items| line_items.first.variant.product.name },
proc { |line_items| line_items.first.variant.full_name },
proc { |line_items| line_items.first.product.name },
proc { |line_items| line_items.first.full_name },
proc { |line_items| line_items.sum { |li| li.quantity } },
proc { |line_items| is_temperature_controlled?(line_items.first) }
]

View File

@@ -5,42 +5,42 @@ module OpenFoodNetwork::Reports
header "Customer", "Product", "Bulk Unit Size", "Variant", "Variant value", "Variant unit", "Weight", "Sum Total", "Total Available", "Unallocated", "Max quantity excess"
organise do
group { |li| li.variant.product }
sort &:name
group { |li| li.product }
sort &:name
summary_row do
column { |lis| "TOTAL" }
column { |lis| product_name(lis) }
column { |lis| group_buy_unit_size_f(lis) }
column { |lis| "" }
column { |lis| "" }
column { |lis| "" }
column { |lis| "" }
column { |lis| total_amount(lis) }
column { |lis| total_available(lis) }
column { |lis| remainder(lis) }
column { |lis| max_quantity_excess(lis) }
end
summary_row do
column { |lis| "TOTAL" }
column { |lis| product_name(lis) }
column { |lis| group_buy_unit_size_f(lis) }
column { |lis| "" }
column { |lis| "" }
column { |lis| "" }
column { |lis| "" }
column { |lis| total_amount(lis) }
column { |lis| total_available(lis) }
column { |lis| remainder(lis) }
column { |lis| max_quantity_excess(lis) }
end
organise do
group { |li| li.full_name }
sort { |full_name| full_name }
organise do
group { |li| li.variant }
sort &:full_name
organise do
group { |li| li.order }
sort { |order| order.to_s }
end
group { |li| li.order }
sort { |order| order.to_s }
end
end
end
columns do
column { |lis| lis.first.order.bill_address.firstname + " " + lis.first.order.bill_address.lastname }
column { |lis| lis.first.variant.product.name }
column { |lis| lis.first.variant.product.group_buy_unit_size || 0.0 }
column { |lis| lis.first.variant.full_name }
column { |lis| OpenFoodNetwork::OptionValueNamer.new(lis.first.variant).value }
column { |lis| OpenFoodNetwork::OptionValueNamer.new(lis.first.variant).unit }
column { |lis| lis.first.variant.weight || 0 }
column { |lis| lis.first.product.name }
column { |lis| lis.first.product.group_buy_unit_size || 0.0 }
column { |lis| lis.first.full_name }
column { |lis| OpenFoodNetwork::OptionValueNamer.new(lis.first).value }
column { |lis| OpenFoodNetwork::OptionValueNamer.new(lis.first).unit }
column { |lis| lis.first.weight_from_unit_value || 0 }
column { |lis| total_amount(lis) }
column { |lis| "" }
column { |lis| "" }

View File

@@ -26,7 +26,7 @@ module OpenFoodNetwork::Reports
end
def total_amount(lis)
lis.sum { |li| (li.quantity || 0) * scaled_amount(li) }
lis.sum { |li| scaled_final_weight_volume(li) }
end
def units_required(lis)
@@ -53,14 +53,17 @@ module OpenFoodNetwork::Reports
def max_quantity_amount(lis)
lis.sum do |li|
max_quantity = [li.max_quantity || 0, li.quantity || 0].max
max_quantity * scaled_amount(li)
max_quantity * scaled_unit_value(li.variant)
end
end
def scaled_amount(li)
(li.variant.unit_value || 0) / (li.product.variant_unit_scale || 1)
def scaled_final_weight_volume(li)
(li.final_weight_volume || 0) / (li.product.variant_unit_scale || 1)
end
def scaled_unit_value(v)
(v.unit_value || 0) / (v.product.variant_unit_scale || 1)
end
end
end
end

View File

@@ -5,11 +5,11 @@ module OpenFoodNetwork::Reports
header "Supplier", "Product", "Bulk Unit Size", "Variant", "Variant value", "Variant unit", "Weight", "Sum Total", "Units Required", "Unallocated", "Max quantity excess"
organise do
group { |li| li.variant.product.supplier }
group { |li| li.product.supplier }
sort &:name
organise do
group { |li| li.variant.product }
group { |li| li.product }
sort &:name
summary_row do
@@ -27,8 +27,8 @@ module OpenFoodNetwork::Reports
end
organise do
group { |li| li.variant }
sort &:full_name
group { |li| li.full_name }
sort { |full_name| full_name }
end
end
end
@@ -37,10 +37,10 @@ module OpenFoodNetwork::Reports
column { |lis| supplier_name(lis) }
column { |lis| product_name(lis) }
column { |lis| group_buy_unit_size_f(lis) }
column { |lis| lis.first.variant.full_name }
column { |lis| OpenFoodNetwork::OptionValueNamer.new(lis.first.variant).value }
column { |lis| OpenFoodNetwork::OptionValueNamer.new(lis.first.variant).unit }
column { |lis| lis.first.variant.weight || 0 }
column { |lis| lis.first.full_name }
column { |lis| OpenFoodNetwork::OptionValueNamer.new(lis.first).value }
column { |lis| OpenFoodNetwork::OptionValueNamer.new(lis.first).unit }
column { |lis| lis.first.weight_from_unit_value || 0 }
column { |lis| total_amount(lis) }
column { |lis| '' }
column { |lis| '' }

View File

@@ -56,6 +56,10 @@ module OpenFoodNetwork
self.option_values.destroy ovs
end
def weight_from_unit_value
unit_value / 1000 if self.product.variant_unit == 'weight' && unit_value.present?
end
private
def option_value_name

View File

@@ -282,7 +282,7 @@ feature %q{
end
end
describe "products and inventory report" do
describe "products and inventory report", js: true do
let(:supplier) { create(:supplier_enterprise, name: 'Supplier Name') }
let(:taxon) { create(:taxon, name: 'Taxon Name') }
let(:product1) { create(:simple_product, name: "Product Name", price: 100, supplier: supplier, primary_taxon: taxon) }
@@ -312,7 +312,8 @@ feature %q{
click_link 'Products & Inventory'
page.should have_content "Supplier"
page.should have_table_row ["Supplier", "Producer Suburb", "Product", "Product Properties", "Taxons", "Variant Value", "Price", "Group Buy Unit Quantity", "Amount"]
save_screenshot '/Users/rob/Desktop/ss.png'
page.should have_table_row ["Supplier", "Producer Suburb", "Product", "Product Properties", "Taxons", "Variant Value", "Price", "Group Buy Unit Quantity", "Amount"].map(&:upcase)
page.should have_table_row [product1.supplier.name, product1.supplier.address.city, "Product Name", product1.properties.map(&:presentation).join(", "), product1.primary_taxon.name, "Test", "100.0", product1.group_buy_unit_size.to_s, ""]
page.should have_table_row [product1.supplier.name, product1.supplier.address.city, "Product Name", product1.properties.map(&:presentation).join(", "), product1.primary_taxon.name, "Something", "80.0", product1.group_buy_unit_size.to_s, ""]
page.should have_table_row [product2.supplier.name, product1.supplier.address.city, "Product 2", product1.properties.map(&:presentation).join(", "), product2.primary_taxon.name, "100g", "99.0", product1.group_buy_unit_size.to_s, ""]