LettuceShare report: display only if count_on_hand > 0

Use the inventory of a distributor if selected.
This commit is contained in:
Maikel Linke
2015-12-10 17:39:35 +11:00
parent 15ea64b409
commit 1d46c2febd
4 changed files with 19 additions and 19 deletions

View File

@@ -26,18 +26,15 @@ Spree::Variant.class_eval do
after_save :update_units
scope :with_order_cycles_inner, joins(exchanges: :order_cycle)
scope :with_order_cycles_outer, joins('LEFT OUTER JOIN exchange_variants AS o_exchange_variants ON (o_exchange_variants.variant_id = spree_variants.id)').
joins('LEFT OUTER JOIN exchanges AS o_exchanges ON (o_exchanges.id = o_exchange_variants.exchange_id)').
joins('LEFT OUTER JOIN order_cycles AS o_order_cycles ON (o_order_cycles.id = o_exchanges.order_cycle_id)')
scope :not_deleted, where(deleted_at: nil)
scope :in_stock, where('spree_variants.count_on_hand > 0 OR spree_variants.on_demand=?', true)
scope :in_distributor, lambda { |distributor|
with_order_cycles_outer.
where('o_exchanges.incoming = ? AND o_exchanges.receiver_id = ?', false, distributor).
select('DISTINCT spree_variants.*')
where(id: ExchangeVariant.select(:variant_id).
joins(:exchange).
where('exchanges.incoming = ? AND exchanges.receiver_id = ?', false, distributor)
)
}
scope :in_order_cycle, lambda { |order_cycle|
with_order_cycles_inner.
merge(Exchange.outgoing).

View File

@@ -19,7 +19,13 @@ module OpenFoodNetwork
end
def table
variants.map do |variant|
if params[:distributor_id].to_i > 0
distributor = Enterprise.find(params[:distributor_id])
scoper = OpenFoodNetwork::ScopeVariantToHub.new(distributor)
variants.each { |v| scoper.scope(v) }
end
variants.select { |v| v.count_on_hand > 0 }
.map do |variant|
[
variant.product.name,
variant.full_name,

View File

@@ -28,8 +28,6 @@ module OpenFoodNetwork
end
def filter(variants)
# NOTE: Ordering matters.
# filter_to_order_cycle and filter_to_distributor return arrays not relations
filter_to_distributor filter_to_order_cycle filter_on_hand filter_to_supplier filter_not_deleted variants
end
@@ -56,9 +54,7 @@ module OpenFoodNetwork
def filter_to_distributor(variants)
if params[:distributor_id].to_i > 0
distributor = Enterprise.find params[:distributor_id]
variants.select do |v|
Enterprise.distributing_product(v.product_id).include? distributor
end
variants.in_distributor(distributor)
else
variants
end
@@ -67,7 +63,7 @@ module OpenFoodNetwork
def filter_to_order_cycle(variants)
if params[:order_cycle_id].to_i > 0
order_cycle = OrderCycle.find params[:order_cycle_id]
variants.select { |v| order_cycle.variants.include? v }
variants.where(id: order_cycle.variants)
else
variants
end

View File

@@ -124,15 +124,16 @@ module OpenFoodNetwork
it "filters to a specific distributor" do
distributor = create(:distributor_enterprise)
product1 = create(:simple_product, supplier: supplier)
product2 = create(:simple_product, supplier: supplier, distributors: [distributor])
product2 = create(:simple_product, supplier: supplier)
order_cycle = create(:simple_order_cycle, suppliers: [supplier], distributors: [distributor], variants: [product2.variants.first])
subject.stub(:params).and_return(distributor_id: distributor.id)
subject.filter(variants).should == [product2.variants.first]
end
it "filters to a specific order cycle" do
distributor = create(:distributor_enterprise)
product1 = create(:simple_product, supplier: supplier, distributors: [distributor])
product2 = create(:simple_product, supplier: supplier, distributors: [distributor])
product1 = create(:simple_product, supplier: supplier)
product2 = create(:simple_product, supplier: supplier)
order_cycle = create(:simple_order_cycle, suppliers: [supplier], distributors: [distributor], variants: [product1.variants.first])
subject.stub(:params).and_return(order_cycle_id: order_cycle.id)
@@ -141,8 +142,8 @@ module OpenFoodNetwork
it "should do all the filters at once" do
distributor = create(:distributor_enterprise)
product1 = create(:simple_product, supplier: supplier, distributors: [distributor])
product2 = create(:simple_product, supplier: supplier, distributors: [distributor])
product1 = create(:simple_product, supplier: supplier)
product2 = create(:simple_product, supplier: supplier)
order_cycle = create(:simple_order_cycle, suppliers: [supplier], distributors: [distributor], variants: [product1.variants.first])
subject.stub(:params).and_return(