Variant overrides can override stock levels

This commit is contained in:
Rohan Mitchell
2014-12-18 10:40:56 +11:00
parent f5ee9ba2f3
commit 1d3800696e
4 changed files with 48 additions and 11 deletions

View File

@@ -1,3 +1,5 @@
require 'open_food_network/scope_product_to_hub'
class ShopController < BaseController
layout "darkswarm"
before_filter :require_distributor_chosen
@@ -10,10 +12,13 @@ class ShopController < BaseController
def products
# Can we make this query less slow?
#
if @products = current_order_cycle.andand
.valid_products_distributed_by(current_distributor).andand
.select { |p| !p.deleted? && p.has_stock_for_distribution?(current_order_cycle, current_distributor) }.andand
.sort_by {|p| p.name }
if current_order_cycle
@products = current_order_cycle.
valid_products_distributed_by(current_distributor).
each { |p| p.scope_to_hub current_distributor }.
select { |p| !p.deleted? && p.has_stock_for_distribution?(current_order_cycle, current_distributor) }.
sort_by(&:name)
render status: 200,
json: ActiveModel::ArraySerializer.new(@products, each_serializer: Api::ProductSerializer,
current_order_cycle: current_order_cycle, current_distributor: current_distributor).to_json

View File

@@ -137,7 +137,7 @@ Spree::Product.class_eval do
# This product has stock for a distribution if it is available on-demand
# or if one of its variants in the distribution is in stock
(!has_variants? && on_demand) ||
variants_distributed_by(order_cycle, distributor).any? { |v| v.in_stock? }
variants_distributed_by(order_cycle, distributor).any?(&:in_stock?)
end
def variants_distributed_by(order_cycle, distributor)

View File

@@ -0,0 +1,16 @@
require 'open_food_network/scope_variant_to_hub'
module OpenFoodNetwork
module ScopeProductToHub
def variants_distributed_by(order_cycle, distributor)
super.each { |v| v.scope_to_hub @hub }
end
end
end
Spree::Product.class_eval do
def scope_to_hub(hub)
extend OpenFoodNetwork::ScopeProductToHub
@hub = hub
end
end

View File

@@ -13,26 +13,42 @@ feature "shopping with variant overrides defined", js: true do
let(:producer) { create(:supplier_enterprise) }
let(:oc) { create(:simple_order_cycle, suppliers: [producer], coordinator: hub, distributors: [hub]) }
let(:outgoing_exchange) { oc.exchanges.outgoing.first }
let(:product) { create(:simple_product, supplier: producer) }
let(:variant) { create(:variant, product: product, price: 11.11) }
let!(:vo) { create(:variant_override, hub: hub, variant: variant, price: 22.22) }
let(:p1) { create(:simple_product, supplier: producer) }
let(:p2) { create(:simple_product, supplier: producer) }
let(:v1) { create(:variant, product: p1, price: 11.11, unit_value: 1) }
let(:v2) { create(:variant, product: p1, price: 22.22, unit_value: 2) }
let(:v3) { create(:variant, product: p2, price: 33.33, unit_value: 3) }
let!(:vo1) { create(:variant_override, hub: hub, variant: v1, price: 99.99) }
let!(:vo2) { create(:variant_override, hub: hub, variant: v2, count_on_hand: 0) }
let!(:vo3) { create(:variant_override, hub: hub, variant: v3, count_on_hand: 0) }
before do
outgoing_exchange.variants << variant
outgoing_exchange.variants << v1
outgoing_exchange.variants << v2
outgoing_exchange.variants << v3
visit shop_path
click_link hub.name
end
it "shows the overridden price" do
page.should_not have_price "$11.11"
page.should have_price "$22.22"
page.should have_price "$99.99"
end
it "looks up stock from the override" do
# Product should appear but one of the variants is out of stock
page.should_not have_content v2.options_text
# Entire product should not appear - no stock
page.should_not have_content p2.name
page.should_not have_content v3.options_text
end
it "takes stock from the override"
it "calculates fees correctly"
it "shows the overridden price with fees in the quick cart"
it "shows the correct prices in the shopping cart"
it "shows the correct prices in the checkout"
it "creates the order with the correct prices"
it "subtracts stock from the override"
end
end