diff --git a/app/assets/javascripts/darkswarm/services/cart.js.coffee b/app/assets/javascripts/darkswarm/services/cart.js.coffee
index 849af562b8..bdd0aa3676 100644
--- a/app/assets/javascripts/darkswarm/services/cart.js.coffee
+++ b/app/assets/javascripts/darkswarm/services/cart.js.coffee
@@ -57,18 +57,20 @@ Darkswarm.factory 'Cart', (CurrentOrder, Variants, $timeout, $http, $modal, $roo
scope = $rootScope.$new(true)
scope.variants = []
- # TODO: These changes to quantity/max_quantity trigger another cart update, which
- # is unnecessary.
-
+ # TODO: These changes to quantity/max_quantity trigger another cart update, which is unnecessary.
for li in @line_items when li.quantity > 0
- if stockLevels[li.variant.id]?
- li.variant.count_on_hand = stockLevels[li.variant.id].on_hand
- if li.quantity > li.variant.count_on_hand
- li.quantity = li.variant.count_on_hand
- scope.variants.push li.variant
- if li.variant.count_on_hand == 0 && li.max_quantity > li.variant.count_on_hand
- li.max_quantity = li.variant.count_on_hand
- scope.variants.push(li.variant) unless li.variant in scope.variants
+ continue unless stockLevels[li.variant.id]?
+
+ li.variant.on_hand = stockLevels[li.variant.id].on_hand
+ li.variant.on_demand = stockLevels[li.variant.id].on_demand
+ continue if li.variant.on_demand
+
+ if li.quantity > li.variant.on_hand
+ li.quantity = li.variant.on_hand
+ scope.variants.push li.variant
+ if li.variant.on_hand == 0 && li.max_quantity > li.variant.on_hand
+ li.max_quantity = li.variant.on_hand
+ scope.variants.push(li.variant) unless li.variant in scope.variants
if scope.variants.length > 0
$modal.open(templateUrl: "out_of_stock.html", scope: scope, windowClass: 'out-of-stock-modal')
diff --git a/app/assets/javascripts/templates/out_of_stock.html.haml b/app/assets/javascripts/templates/out_of_stock.html.haml
index a92c721c25..4ee27e7aa1 100644
--- a/app/assets/javascripts/templates/out_of_stock.html.haml
+++ b/app/assets/javascripts/templates/out_of_stock.html.haml
@@ -9,7 +9,7 @@
%p{'ng-repeat' => "v in variants"}
%em {{ v.name_to_display }} - {{ v.unit_to_display }}
- %span{'ng-if' => "v.count_on_hand == 0"}
+ %span{'ng-if' => "v.on_hand == 0"}
{{ 'js.out_of_stock.now_out_of_stock' | t }}
- %span{'ng-if' => "v.count_on_hand > 0"}
- {{ 'js.out_of_stock.only_n_remainging' | t:{ num: v.count_on_hand } }}
+ %span{'ng-if' => "v.on_hand > 0"}
+ {{ 'js.out_of_stock.only_n_remainging' | t:{ num: v.on_hand } }}
diff --git a/app/assets/javascripts/templates/partials/shop_variant_no_group_buy.html.haml b/app/assets/javascripts/templates/partials/shop_variant_no_group_buy.html.haml
index 0441156c2c..2e7e49a0ca 100644
--- a/app/assets/javascripts/templates/partials/shop_variant_no_group_buy.html.haml
+++ b/app/assets/javascripts/templates/partials/shop_variant_no_group_buy.html.haml
@@ -7,6 +7,6 @@
placeholder: "0",
"ofn-disable-scroll" => true,
"ng-model" => "variant.line_item.quantity",
- "ofn-on-hand" => "{{variant.on_demand && 9999 || variant.count_on_hand }}",
- "ng-disabled" => "!variant.on_demand && variant.count_on_hand == 0",
+ "ofn-on-hand" => "{{variant.on_demand && 9999 || variant.on_hand }}",
+ "ng-disabled" => "!variant.on_demand && variant.on_hand == 0",
name: "variants[{{::variant.id}}]", id: "variants_{{::variant.id}}"}
diff --git a/app/assets/javascripts/templates/partials/shop_variant_with_group_buy.html.haml b/app/assets/javascripts/templates/partials/shop_variant_with_group_buy.html.haml
index 05bdf468b4..169b33391c 100644
--- a/app/assets/javascripts/templates/partials/shop_variant_with_group_buy.html.haml
+++ b/app/assets/javascripts/templates/partials/shop_variant_with_group_buy.html.haml
@@ -9,7 +9,7 @@
"ng-model" => "variant.line_item.quantity",
placeholder: "{{::'shop_variant_quantity_min' | t}}",
"ofn-disable-scroll" => true,
- "ofn-on-hand" => "{{variant.on_demand && 9999 || variant.count_on_hand }}",
+ "ofn-on-hand" => "{{variant.on_demand && 9999 || variant.on_hand }}",
name: "variants[{{::variant.id}}]", id: "variants_{{::variant.id}}"}
%span.bulk-input
%input.bulk.second{type: :number,
diff --git a/app/models/concerns/variant_stock.rb b/app/models/concerns/variant_stock.rb
index f7553b107f..8eac3eb32e 100644
--- a/app/models/concerns/variant_stock.rb
+++ b/app/models/concerns/variant_stock.rb
@@ -20,61 +20,30 @@ module VariantStock
after_update :save_stock
end
- # Returns the number of items of the variant available in the stock. When
- # allowing on demand, it returns infinite.
- #
- # Spree computes it as the sum of the count_on_hand of all its stock_items.
+ # Returns the number of items of the variant available.
+ # Spree computes total_on_hand as the sum of the count_on_hand of all its stock_items.
#
# @return [Float|Integer]
def on_hand
warn_deprecation(__method__, '#total_on_hand')
- if on_demand
- Float::INFINITY
- else
- total_on_hand
- end
- end
-
- # Returns the number of items available in the stock for this variant
- #
- # @return [Float|Integer]
- def count_on_hand
- warn_deprecation(__method__, '#total_on_hand')
total_on_hand
end
- # Sets the stock level when `track_inventory_levels` config is
- # set. It raises otherwise.
+ # Sets the stock level of the variant.
+ # This will only work if `track_inventory_levels` config is set
+ # and if there is a stock item for the variant.
#
- # @raise [StandardError] when the track_inventory_levels config
- # key is not set.
+ # @raise [StandardError] when the track_inventory_levels config key is not set
+ # and when the variant has no stock item
def on_hand=(new_level)
warn_deprecation(__method__, '#total_on_hand')
error = 'Cannot set on_hand value when Spree::Config[:track_inventory_levels] is false'
raise error unless Spree::Config.track_inventory_levels
- self.count_on_hand = new_level
- end
-
- # Sets the stock level. As opposed to #on_hand= it does not check
- # `track_inventory_levels`'s value as it was previously an ActiveModel
- # setter of the database column of the `spree_variants` table. That is why
- # #on_hand= is more widely used in Spree's codebase using #count_on_hand=
- # underneath.
- #
- # So, if #count_on_hand= is used, `track_inventory_levels` won't be taken
- # into account thus dismissing instance's configuration.
- #
- # It does ensure there's a stock item for the variant however. See
- # #raise_error_if_no_stock_item_available for details.
- #
- # @raise [StandardError] when the variant has no stock item yet
- def count_on_hand=(new_level)
- warn_deprecation(__method__, '#total_on_hand')
-
raise_error_if_no_stock_item_available
+
overwrite_stock_levels(new_level)
end
@@ -131,7 +100,7 @@ module VariantStock
# Here we depend only on variant.total_on_hand and variant.on_demand.
# This way, variant_overrides only need to override variant.total_on_hand and variant.on_demand.
def fill_status(quantity)
- if count_on_hand >= quantity
+ if on_hand >= quantity
on_hand = quantity
backordered = 0
else
diff --git a/app/models/product_import/entry_processor.rb b/app/models/product_import/entry_processor.rb
index e6c004b7ca..eab1ed4cc3 100644
--- a/app/models/product_import/entry_processor.rb
+++ b/app/models/product_import/entry_processor.rb
@@ -218,7 +218,7 @@ module ProductImport
end
when 'overwrite_empty'
if object.public_send(attribute).blank? ||
- ((attribute == 'on_hand' || attribute == 'count_on_hand') &&
+ ((attribute == 'on_hand') &&
entry.on_hand_nil)
object.assign_attributes(attribute => setting['value'])
diff --git a/app/models/product_import/entry_validator.rb b/app/models/product_import/entry_validator.rb
index 090c55e01d..39e3d761f9 100644
--- a/app/models/product_import/entry_validator.rb
+++ b/app/models/product_import/entry_validator.rb
@@ -372,7 +372,6 @@ module ProductImport
return if entry.on_hand.present?
object.on_hand = 0 if object.respond_to?(:on_hand)
- object.count_on_hand = 0 if object.respond_to?(:count_on_hand)
entry.on_hand_nil = true
end
diff --git a/app/models/product_import/products_reset_strategy.rb b/app/models/product_import/products_reset_strategy.rb
index 0dc005af82..989f5a275c 100644
--- a/app/models/product_import/products_reset_strategy.rb
+++ b/app/models/product_import/products_reset_strategy.rb
@@ -9,7 +9,7 @@ module ProductImport
return 0 if enterprise_ids.blank?
- reset_variants_count_on_hand(enterprise_variants_relation)
+ reset_variants_on_hand(enterprise_variants_relation)
end
private
@@ -29,17 +29,17 @@ module ProductImport
relation.where('spree_variants.id NOT IN (?)', excluded_items_ids)
end
- def reset_variants_count_on_hand(variants)
+ def reset_variants_on_hand(variants)
updated_records_count = 0
variants.each do |variant|
- updated_records_count += 1 if reset_variant_count_on_hand(variant)
+ updated_records_count += 1 if reset_variant_on_hand(variant)
end
updated_records_count
end
- def reset_variant_count_on_hand(variant)
- variant.count_on_hand = 0
- variant.count_on_hand.zero?
+ def reset_variant_on_hand(variant)
+ variant.on_hand = 0
+ variant.on_hand.zero?
end
end
end
diff --git a/app/models/product_import/spreadsheet_entry.rb b/app/models/product_import/spreadsheet_entry.rb
index c87f46ec0e..990124e24b 100644
--- a/app/models/product_import/spreadsheet_entry.rb
+++ b/app/models/product_import/spreadsheet_entry.rb
@@ -16,7 +16,7 @@ module ProductImport
:distributor_id, :name, :display_name, :sku, :unit_value,
:unit_description, :variant_unit, :variant_unit_scale,
:variant_unit_name, :display_as, :category, :primary_taxon_id,
- :price, :on_hand, :count_on_hand, :on_demand,
+ :price, :on_hand, :on_demand,
:tax_category_id, :shipping_category_id, :description,
:import_date, :enterprise, :enterprise_id
diff --git a/app/models/spree/line_item_decorator.rb b/app/models/spree/line_item_decorator.rb
index 31b4945c51..d64c02155d 100644
--- a/app/models/spree/line_item_decorator.rb
+++ b/app/models/spree/line_item_decorator.rb
@@ -64,6 +64,7 @@ Spree::LineItem.class_eval do
def cap_quantity_at_stock!
scoper.scope(variant)
+ return if variant.on_demand
update_attributes!(quantity: variant.on_hand) if quantity > variant.on_hand
end
diff --git a/app/serializers/api/admin/product_serializer.rb b/app/serializers/api/admin/product_serializer.rb
index e84588128f..5ef3a27d4e 100644
--- a/app/serializers/api/admin/product_serializer.rb
+++ b/app/serializers/api/admin/product_serializer.rb
@@ -25,7 +25,8 @@ class Api::Admin::ProductSerializer < ActiveModel::Serializer
end
def on_hand
- object.on_hand.nil? ? 0 : object.on_hand.to_f.finite? ? object.on_hand : I18n.t(:on_demand)
+ return 0 if object.on_hand.nil?
+ object.on_hand
end
def price
diff --git a/app/serializers/api/admin/variant_serializer.rb b/app/serializers/api/admin/variant_serializer.rb
index 3fc22b98d4..9be1b3662b 100644
--- a/app/serializers/api/admin/variant_serializer.rb
+++ b/app/serializers/api/admin/variant_serializer.rb
@@ -4,7 +4,8 @@ class Api::Admin::VariantSerializer < ActiveModel::Serializer
has_many :variant_overrides
def on_hand
- object.on_hand.nil? ? 0 : ( object.on_hand.to_f.finite? ? object.on_hand : I18n.t(:on_demand) )
+ return 0 if object.on_hand.nil?
+ object.on_hand
end
def price
diff --git a/app/serializers/api/variant_serializer.rb b/app/serializers/api/variant_serializer.rb
index 59600b6ab3..f4845dc6af 100644
--- a/app/serializers/api/variant_serializer.rb
+++ b/app/serializers/api/variant_serializer.rb
@@ -1,5 +1,5 @@
class Api::VariantSerializer < ActiveModel::Serializer
- attributes :id, :is_master, :count_on_hand, :name_to_display, :unit_to_display, :unit_value
+ attributes :id, :is_master, :on_hand, :name_to_display, :unit_to_display, :unit_value
attributes :options_text, :on_demand, :price, :fees, :price_with_fees, :product_name
attributes :tag_list
diff --git a/app/services/order_factory.rb b/app/services/order_factory.rb
index e920719384..85c887411a 100644
--- a/app/services/order_factory.rb
+++ b/app/services/order_factory.rb
@@ -45,7 +45,7 @@ class OrderFactory
attrs[:line_items].each do |li|
next unless variant = Spree::Variant.find_by_id(li[:variant_id])
scoper.scope(variant)
- li[:quantity] = stock_limited_quantity(variant.count_on_hand, li[:quantity])
+ li[:quantity] = stock_limited_quantity(variant.on_hand, li[:quantity])
li[:price] = variant.price
build_item_from(li)
end
diff --git a/app/services/variants_stock_levels.rb b/app/services/variants_stock_levels.rb
index 43d9190800..69dbc49dee 100644
--- a/app/services/variants_stock_levels.rb
+++ b/app/services/variants_stock_levels.rb
@@ -10,8 +10,8 @@ class VariantsStockLevels
order_variant_ids = variant_stock_levels.keys
missing_variant_ids = requested_variant_ids - order_variant_ids
missing_variant_ids.each do |variant_id|
- variant_on_hand = Spree::Variant.find(variant_id).on_hand
- variant_stock_levels[variant_id] = { quantity: 0, max_quantity: 0, on_hand: variant_on_hand }
+ variant = Spree::Variant.find(variant_id)
+ variant_stock_levels[variant_id] = { quantity: 0, max_quantity: 0, on_hand: variant.on_hand, on_demand: variant.on_demand }
end
# The code above is most probably dead code, this bugsnag notification will confirm it
@@ -37,14 +37,9 @@ class VariantsStockLevels
[line_item.variant.id,
{ quantity: line_item.quantity,
max_quantity: line_item.max_quantity,
- on_hand: wrap_json_infinity(line_item.variant.on_hand) }]
+ on_hand: line_item.variant.on_hand,
+ on_demand: line_item.variant.on_demand }]
end
]
end
-
- # Rails to_json encodes Float::INFINITY as Infinity, which is not valid JSON
- # Return it as a large integer (max 32 bit signed int)
- def wrap_json_infinity(number)
- number == Float::INFINITY ? 2_147_483_647 : number
- end
end
diff --git a/app/views/shop/products/_form.html.haml b/app/views/shop/products/_form.html.haml
index 8bf8f8dcbb..98fb49501d 100644
--- a/app/views/shop/products/_form.html.haml
+++ b/app/views/shop/products/_form.html.haml
@@ -31,7 +31,7 @@
%div.pad-top{ "infinite-scroll" => "incrementLimit()", "infinite-scroll-distance" => "1", "infinite-scroll-disabled" => 'filteredProducts.length <= limit' }
%product.animate-repeat{"ng-controller" => "ProductNodeCtrl", "ng-repeat" => "product in visibleProducts track by product.id", "id" => "product-{{ product.id }}"}
= render "shop/products/summary"
- %shop-variant{variant: 'variant', "ng-repeat" => "variant in product.variants | orderBy: ['name_to_display','unit_value'] track by variant.id", "id" => "variant-{{ variant.id }}", "ng-class" => "{'out-of-stock': !variant.on_demand && variant.count_on_hand == 0}"}
+ %shop-variant{variant: 'variant', "ng-repeat" => "variant in product.variants | orderBy: ['name_to_display','unit_value'] track by variant.id", "id" => "variant-{{ variant.id }}", "ng-class" => "{'out-of-stock': !variant.on_demand && variant.on_hand == 0}"}
%product{"ng-show" => "Products.loading"}
.row.summary
diff --git a/app/views/spree/admin/products/index/_products_variant.html.haml b/app/views/spree/admin/products/index/_products_variant.html.haml
index c4c0412eef..584e9f3610 100644
--- a/app/views/spree/admin/products/index/_products_variant.html.haml
+++ b/app/views/spree/admin/products/index/_products_variant.html.haml
@@ -16,7 +16,8 @@
%input{ 'ng-model' => 'variant.price', 'ofn-decimal' => :true, :name => 'variant_price', 'ofn-track-variant' => 'price', :type => 'text' }
%td{ 'ng-show' => 'columns.on_hand.visible' }
%input.field{ 'ng-model' => 'variant.on_hand', 'ng-change' => 'updateOnHand(product)', :name => 'variant_on_hand', 'ng-if' => '!variant.on_demand', 'ofn-track-variant' => 'on_hand', :type => 'number' }
- %span{ 'ng-bind' => 'variant.on_hand', :name => 'variant_on_hand', 'ng-if' => 'variant.on_demand' }
+ %span{ :name => 'variant_on_hand', 'ng-if' => 'variant.on_demand' }
+ = t(:on_demand)
%td{ 'ng-show' => 'columns.on_demand.visible' }
%input.field{ 'ng-model' => 'variant.on_demand', :name => 'variant_on_demand', 'ofn-track-variant' => 'on_demand', :type => 'checkbox' }
%td{ 'ng-show' => 'columns.category.visible' }
diff --git a/app/views/spree/admin/variants/_autocomplete.js.erb b/app/views/spree/admin/variants/_autocomplete.js.erb
index 4cecb1f61d..c4c6d101bf 100644
--- a/app/views/spree/admin/variants/_autocomplete.js.erb
+++ b/app/views/spree/admin/variants/_autocomplete.js.erb
@@ -16,7 +16,7 @@
- <%= t('admin.sku') %>: {{variant.sku}}
- - <%= t('on_hand') %>: {{variant.count_on_hand}}
+ - <%= t('on_hand') %>: {{variant.on_hand}}
{{#if variant.option_values}}
@@ -41,7 +41,7 @@
<%= Spree.t(:location) %> |
- <%= Spree.t(:count_on_hand) %> |
+ <%= Spree.t(:on_hand) %> |
<%= Spree.t(:quantity) %> |
|
@@ -53,7 +53,7 @@
Spree.t(:on_demand) |
{{else}}
- {{variant.count_on_hand}}
+ {{variant.on_hand}}
|
{{/if}}
diff --git a/app/views/spree/admin/variants/search.rabl b/app/views/spree/admin/variants/search.rabl
index 8206932c5f..b65ef4d768 100644
--- a/app/views/spree/admin/variants/search.rabl
+++ b/app/views/spree/admin/variants/search.rabl
@@ -2,7 +2,7 @@
# overriding spree/core/app/views/spree/admin/variants/search.rabl
#
collection @variants
-attributes :sku, :options_text, :in_stock?, :on_demand, :count_on_hand, :id, :cost_price
+attributes :sku, :options_text, :in_stock?, :on_demand, :on_hand, :id, :cost_price
node(:name) do |v|
# TODO: when products must have a unit, full_name will always be present
diff --git a/spec/controllers/cart_controller_spec.rb b/spec/controllers/cart_controller_spec.rb
index 8a7ad16dd8..5630429d6d 100644
--- a/spec/controllers/cart_controller_spec.rb
+++ b/spec/controllers/cart_controller_spec.rb
@@ -63,8 +63,8 @@ describe CartController, type: :controller do
let!(:line_item) { create(:line_item, order: order, variant: variant_in_the_order, quantity: 2, max_quantity: 3) }
before do
- variant_in_the_order.count_on_hand = 4
- variant_not_in_the_order.count_on_hand = 2
+ variant_in_the_order.on_hand = 4
+ variant_not_in_the_order.on_hand = 2
order_cycle.exchanges.outgoing.first.variants = [variant_in_the_order, variant_not_in_the_order]
order.order_cycle = order_cycle
order.distributor = hub
diff --git a/spec/controllers/spree/admin/variants_controller_spec.rb b/spec/controllers/spree/admin/variants_controller_spec.rb
index 36fd0463a2..d8178fa3a1 100644
--- a/spec/controllers/spree/admin/variants_controller_spec.rb
+++ b/spec/controllers/spree/admin/variants_controller_spec.rb
@@ -22,7 +22,7 @@ module Spree
it "applies variant overrides" do
spree_get :search, q: 'Prod', distributor_id: d.id.to_s
assigns(:variants).should == [v1]
- assigns(:variants).first.count_on_hand.should == 44
+ assigns(:variants).first.on_hand.should == 44
end
it "filters by order cycle" do
diff --git a/spec/factories.rb b/spec/factories.rb
index cbdbdab3a8..f3100100f3 100644
--- a/spec/factories.rb
+++ b/spec/factories.rb
@@ -604,7 +604,7 @@ FactoryBot.modify do
after(:create) do |variant, evaluator|
variant.on_demand = evaluator.on_demand
- variant.count_on_hand = evaluator.on_hand
+ variant.on_hand = evaluator.on_hand
variant.save
end
end
diff --git a/spec/features/admin/reports_spec.rb b/spec/features/admin/reports_spec.rb
index f34d2ef28e..08e01a4159 100644
--- a/spec/features/admin/reports_spec.rb
+++ b/spec/features/admin/reports_spec.rb
@@ -308,11 +308,11 @@ xfeature %q{
product2.set_property 'Organic', 'NASAA 12345'
product1.taxons = [taxon]
product2.taxons = [taxon]
- variant1.count_on_hand = 10
+ variant1.on_hand = 10
variant1.update_column(:sku, "sku1")
- variant2.count_on_hand = 20
+ variant2.on_hand = 20
variant2.update_column(:sku, "sku2")
- variant3.count_on_hand = 9
+ variant3.on_hand = 9
variant3.update_column(:sku, "")
variant1.option_values = [create(:option_value, :presentation => "Test")]
variant2.option_values = [create(:option_value, :presentation => "Something")]
diff --git a/spec/features/consumer/shopping/shopping_spec.rb b/spec/features/consumer/shopping/shopping_spec.rb
index 4b18a62434..abf1cc7d51 100644
--- a/spec/features/consumer/shopping/shopping_spec.rb
+++ b/spec/features/consumer/shopping/shopping_spec.rb
@@ -302,6 +302,15 @@ feature "As a consumer I want to shop with a distributor", js: true do
Spree::LineItem.where(id: li).should be_empty
end
+ it "lets us add a quantity greater than on_hand value if product is on_demand" do
+ variant.update_attributes on_hand: 5, on_demand: true
+ visit shop_path
+
+ fill_in "variants[#{variant.id}]", with: '10'
+
+ page.should have_field "variants[#{variant.id}]", with: '10'
+ end
+
it "alerts us when we enter a quantity greater than the stock available" do
variant.update_attributes on_hand: 5
visit shop_path
@@ -341,6 +350,18 @@ feature "As a consumer I want to shop with a distributor", js: true do
page.should have_selector "#variants_#{variant.id}[disabled='disabled']"
end
+ it 'does not show out of stock modal if product is on_demand' do
+ expect(page).to have_content "Product"
+
+ variant.update_attributes! on_hand: 0, on_demand: true
+
+ expect(page).to have_input "variants[#{variant.id}]"
+ fill_in "variants[#{variant.id}]", with: '1'
+ wait_until { !cart_dirty }
+
+ expect(page).to_not have_selector '.out-of-stock-modal'
+ end
+
context "group buy products" do
let(:product) { create(:simple_product, group_buy: true) }
diff --git a/spec/features/consumer/shopping/variant_overrides_spec.rb b/spec/features/consumer/shopping/variant_overrides_spec.rb
index 8638616872..5aaa0d1ac9 100644
--- a/spec/features/consumer/shopping/variant_overrides_spec.rb
+++ b/spec/features/consumer/shopping/variant_overrides_spec.rb
@@ -123,7 +123,7 @@ feature "shopping with variant overrides defined", js: true, retry: 3 do
expect do
expect do
complete_checkout
- end.to change { product1_variant3.reload.count_on_hand }.by(0)
+ end.to change { product1_variant3.reload.on_hand }.by(0)
end.to change { product1_variant3_override.reload.count_on_hand }.by(-2)
end
@@ -134,7 +134,7 @@ feature "shopping with variant overrides defined", js: true, retry: 3 do
expect do
expect do
complete_checkout
- end.to change { product3_variant2.reload.count_on_hand }.by(0)
+ end.to change { product3_variant2.reload.on_hand }.by(0)
end.to change { product3_variant2_override.reload.count_on_hand }.by(-2)
end
@@ -143,12 +143,12 @@ feature "shopping with variant overrides defined", js: true, retry: 3 do
click_checkout
expect do
complete_checkout
- end.to change { product1_variant1.reload.count_on_hand }.by(-2)
+ end.to change { product1_variant1.reload.on_hand }.by(-2)
product1_variant1_override.reload.count_on_hand.should be_nil
end
it "does not show out of stock flags on order confirmation page" do
- product1_variant3.count_on_hand = 0
+ product1_variant3.on_hand = 0
fill_in "variants[#{product1_variant3.id}]", with: "2"
click_checkout
diff --git a/spec/javascripts/unit/admin/controllers/variant_overrides_controller_spec.js.coffee b/spec/javascripts/unit/admin/controllers/variant_overrides_controller_spec.js.coffee
index 3ff397eb18..5724fe1353 100644
--- a/spec/javascripts/unit/admin/controllers/variant_overrides_controller_spec.js.coffee
+++ b/spec/javascripts/unit/admin/controllers/variant_overrides_controller_spec.js.coffee
@@ -98,7 +98,7 @@ describe "VariantOverridesCtrl", ->
beforeEach ->
# Ideally, count_on_hand is blank when the variant is on demand. However, this rule is not
# enforced.
- variant = {id: 2, on_demand: true, count_on_hand: 20, on_hand: "On demand"}
+ variant = {id: 2, on_demand: true, on_hand: 20, on_hand: "On demand"}
it "clears count_on_hand when variant override uses producer stock settings", ->
scope.variantOverrides[123][2] = {on_demand: null, count_on_hand: 1}
@@ -162,7 +162,7 @@ describe "VariantOverridesCtrl", ->
beforeEach ->
# Ideally, count_on_hand is blank when the variant is on demand. However, this rule is not
# enforced.
- variant = {id: 2, on_demand: true, count_on_hand: 20, on_hand: t("on_demand")}
+ variant = {id: 2, on_demand: true, on_hand: 20, on_hand: t("on_demand")}
it "is 'On demand' when variant override uses producer stock settings", ->
scope.variantOverrides[123][2] = {on_demand: null, count_on_hand: 1}
@@ -183,7 +183,7 @@ describe "VariantOverridesCtrl", ->
variant = null
beforeEach ->
- variant = {id: 2, on_demand: false, count_on_hand: 20, on_hand: 20}
+ variant = {id: 2, on_demand: false, on_hand: 20, on_hand: 20}
it "is variant count on hand when variant override uses producer stock settings", ->
scope.variantOverrides[123][2] = {on_demand: null, count_on_hand: 1}
diff --git a/spec/javascripts/unit/bulk_product_update_spec.js.coffee b/spec/javascripts/unit/bulk_product_update_spec.js.coffee
index c1c34cbe6b..466253885f 100644
--- a/spec/javascripts/unit/bulk_product_update_spec.js.coffee
+++ b/spec/javascripts/unit/bulk_product_update_spec.js.coffee
@@ -183,7 +183,7 @@ describe "filtering products for submission to database", ->
shipping_category_id: null
created_at: null
updated_at: null
- count_on_hand: 0
+ on_hand: 0
producer_id: 5
group_buy: null
diff --git a/spec/javascripts/unit/darkswarm/services/cart_spec.js.coffee b/spec/javascripts/unit/darkswarm/services/cart_spec.js.coffee
index 131880ddc7..f8d124c110 100644
--- a/spec/javascripts/unit/darkswarm/services/cart_spec.js.coffee
+++ b/spec/javascripts/unit/darkswarm/services/cart_spec.js.coffee
@@ -145,11 +145,11 @@ describe 'Cart service', ->
expect(li.max_quantity).toEqual 0
it "resets the count on hand available", ->
- li = {variant: {id: 1, count_on_hand: 10}, quantity: 5}
+ li = {variant: {id: 1, on_hand: 10}, quantity: 5}
Cart.line_items = [li]
stockLevels = {1: {quantity: 0, max_quantity: 0, on_hand: 0}}
Cart.compareAndNotifyStockLevels stockLevels
- expect(li.variant.count_on_hand).toEqual 0
+ expect(li.variant.on_hand).toEqual 0
describe "when the quantity available is less than that requested", ->
it "reduces the quantity in the cart", ->
@@ -172,7 +172,7 @@ describe 'Cart service', ->
Cart.line_items = [li]
stockLevels = {1: {quantity: 5, on_hand: 6}}
Cart.compareAndNotifyStockLevels stockLevels
- expect(li.variant.count_on_hand).toEqual 6
+ expect(li.variant.on_hand).toEqual 6
describe "when the client-side quantity has been increased during the request", ->
it "does not reset the quantity", ->
diff --git a/spec/jobs/subscription_placement_job_spec.rb b/spec/jobs/subscription_placement_job_spec.rb
index 685d6f4ded..a55bce0d00 100644
--- a/spec/jobs/subscription_placement_job_spec.rb
+++ b/spec/jobs/subscription_placement_job_spec.rb
@@ -76,9 +76,9 @@ describe SubscriptionPlacementJob do
context "and insufficient stock exists to fulfil the order for some items" do
before do
- variant1.update_attribute(:count_on_hand, 5)
- variant2.update_attribute(:count_on_hand, 2)
- variant3.update_attribute(:count_on_hand, 0)
+ variant1.update_attribute(:on_hand, 5)
+ variant2.update_attribute(:on_hand, 2)
+ variant3.update_attribute(:on_hand, 0)
end
xit "caps quantity at the stock level for stock-limited items, and reports the change" do
@@ -98,9 +98,9 @@ describe SubscriptionPlacementJob do
context "and insufficient stock exists to fulfil the order for some items" do
before do
- variant1.update_attribute(:count_on_hand, 5)
- variant2.update_attribute(:count_on_hand, 2)
- variant3.update_attribute(:count_on_hand, 0)
+ variant1.update_attribute(:on_hand, 5)
+ variant2.update_attribute(:on_hand, 2)
+ variant3.update_attribute(:on_hand, 0)
end
xit "sets quantity to 0 for unavailable items, and reports the change" do
diff --git a/spec/lib/open_food_network/lettuce_share_report_spec.rb b/spec/lib/open_food_network/lettuce_share_report_spec.rb
index eb912f58c2..0ca330251c 100644
--- a/spec/lib/open_food_network/lettuce_share_report_spec.rb
+++ b/spec/lib/open_food_network/lettuce_share_report_spec.rb
@@ -50,7 +50,7 @@ module OpenFoodNetwork
end
it "only available items" do
- variant.count_on_hand = 0
+ variant.on_hand = 0
report.stub(:child_variants) { Spree::Variant.where(id: [variant, variant2, variant3, variant4]) }
report.table.count.should eq 3
end
diff --git a/spec/lib/open_food_network/products_renderer_spec.rb b/spec/lib/open_food_network/products_renderer_spec.rb
index a845dfecf5..330ad57062 100644
--- a/spec/lib/open_food_network/products_renderer_spec.rb
+++ b/spec/lib/open_food_network/products_renderer_spec.rb
@@ -50,7 +50,7 @@ module OpenFoodNetwork
it "doesn't return products not in stock" do
variant.update_attribute(:on_demand, false)
- variant.update_attribute(:count_on_hand, 0)
+ variant.update_attribute(:on_hand, 0)
pr.products_json.should_not include product.name
end
diff --git a/spec/lib/open_food_network/scope_variant_to_hub_spec.rb b/spec/lib/open_food_network/scope_variant_to_hub_spec.rb
index 11bf218a1d..4daaa05c6c 100644
--- a/spec/lib/open_food_network/scope_variant_to_hub_spec.rb
+++ b/spec/lib/open_food_network/scope_variant_to_hub_spec.rb
@@ -39,12 +39,12 @@ module OpenFoodNetwork
it "returns the overridden stock level when one is present" do
vo
scoper.scope v
- v.count_on_hand.should == 2
+ v.on_hand.should == 2
end
it "returns the variant's stock level otherwise" do
scoper.scope v
- v.count_on_hand.should == 1
+ v.on_hand.should == 1
end
describe "overriding stock on an on_demand variant" do
@@ -131,7 +131,7 @@ module OpenFoodNetwork
end
context "when variant out of stock" do
- before { v.count_on_hand = 0 }
+ before { v.on_hand = 0 }
it "returns true if VO in stock" do
scoper.scope v
@@ -153,7 +153,7 @@ module OpenFoodNetwork
end
it "returns false if variant out of stock" do
- v.count_on_hand = 0
+ v.on_hand = 0
scoper.scope v
expect(v.in_stock?).to eq(false)
end
diff --git a/spec/models/concerns/variant_stock_spec.rb b/spec/models/concerns/variant_stock_spec.rb
index 50c89671e4..0c521ac3e6 100644
--- a/spec/models/concerns/variant_stock_spec.rb
+++ b/spec/models/concerns/variant_stock_spec.rb
@@ -18,15 +18,15 @@ describe VariantStock do
end
describe '#on_hand' do
- context 'when the variant is ordered on demand' do
+ context 'when the variant is on demand' do
before do
variant.stock_items.first.update_attribute(
:backorderable, true
)
end
- it 'returns infinite' do
- expect(variant.on_hand).to eq(Float::INFINITY)
+ it 'returns the total items in stock anyway' do
+ expect(variant.on_hand).to eq(variant.stock_items.sum(&:count_on_hand))
end
end
@@ -44,28 +44,27 @@ describe VariantStock do
end
end
- describe '#count_on_hand' do
- before { allow(variant).to receive(:total_on_hand) }
-
- it 'delegates to #total_on_hand' do
- variant.count_on_hand
- expect(variant).to have_received(:total_on_hand)
- end
- end
-
describe '#on_hand=' do
context 'when track_inventory_levels is set' do
before do
- allow(variant).to receive(:count_on_hand=)
allow(Spree::Config)
.to receive(:track_inventory_levels) { true }
end
- it 'delegates to #count_on_hand=' do
+ it 'sets the new level as the stock item\'s count_on_hand' do
variant.on_hand = 3
- expect(variant)
- .to have_received(:count_on_hand=).with(3)
+ unique_stock_item = variant.stock_items.first
+ expect(unique_stock_item.count_on_hand).to eq(3)
end
+
+ context 'when the variant has no stock item' do
+ let(:variant) { build(:variant) }
+
+ it 'raises' do
+ expect { variant.on_hand = 3 }
+ .to raise_error(StandardError)
+ end
+ end
end
context 'when track_inventory_levels is not set' do
@@ -81,27 +80,6 @@ describe VariantStock do
end
end
- describe '#count_on_hand=' do
- context 'when the variant has a stock item' do
- let(:variant) { create(:variant) }
-
- it 'sets the new level as the stock item\'s count_on_hand' do
- variant.count_on_hand = 3
- unique_stock_item = variant.stock_items.first
- expect(unique_stock_item.count_on_hand).to eq(3)
- end
- end
-
- context 'when the variant has no stock item' do
- let(:variant) { build(:variant) }
-
- it 'raises' do
- expect { variant.count_on_hand = 3 }
- .to raise_error(StandardError)
- end
- end
- end
-
describe '#on_demand' do
context 'when the variant has a stock item' do
let(:variant) { create(:variant) }
@@ -187,7 +165,7 @@ describe VariantStock do
end
context 'when variant out of stock' do
- before { variant.count_on_hand = 0 }
+ before { variant.on_hand = 0 }
it "returns true for zero" do
expect(variant.can_supply?(0)).to eq(true)
diff --git a/spec/models/product_import/products_reset_strategy_spec.rb b/spec/models/product_import/products_reset_strategy_spec.rb
index c3aa64a00b..e44249e8a3 100644
--- a/spec/models/product_import/products_reset_strategy_spec.rb
+++ b/spec/models/product_import/products_reset_strategy_spec.rb
@@ -9,7 +9,7 @@ describe ProductImport::ProductsResetStrategy do
let(:enterprise) { product.supplier }
let(:variant) { product.variants.first }
- before { variant.count_on_hand = 2 }
+ before { variant.on_hand = 2 }
context 'when there are excluded_items_ids' do
let(:excluded_items_ids) { [variant.id] }
@@ -17,35 +17,35 @@ describe ProductImport::ProductsResetStrategy do
context 'and supplier_ids is []' do
let(:supplier_ids) { [] }
- it 'does not reset the variant.count_on_hand' do
+ it 'does not reset the variant.on_hand' do
products_reset.reset(supplier_ids)
- expect(variant.reload.count_on_hand).to eq(2)
+ expect(variant.reload.on_hand).to eq(2)
end
end
context 'and supplier_ids is nil' do
let(:supplier_ids) { nil }
- it 'does not reset the variant.count_on_hand' do
+ it 'does not reset the variant.on_hand' do
products_reset.reset(supplier_ids)
- expect(variant.reload.count_on_hand).to eq(2)
+ expect(variant.reload.on_hand).to eq(2)
end
end
context 'and supplier_ids is set' do
- it 'does not update the count_on_hand of the excluded items' do
+ it 'does not update the on_hand of the excluded items' do
products_reset.reset(supplier_ids)
- expect(variant.reload.count_on_hand).to eq(2)
+ expect(variant.reload.on_hand).to eq(2)
end
- it 'updates the count_on_hand of the non-excluded items' do
+ it 'updates the on_hand of the non-excluded items' do
non_excluded_variant = create(
:variant,
product: variant.product
)
- non_excluded_variant.count_on_hand = 3
+ non_excluded_variant.on_hand = 3
products_reset.reset(supplier_ids)
- expect(non_excluded_variant.reload.count_on_hand).to eq(0)
+ expect(non_excluded_variant.reload.on_hand).to eq(0)
end
end
end
@@ -56,31 +56,31 @@ describe ProductImport::ProductsResetStrategy do
context 'and supplier_ids is []' do
let(:supplier_ids) { [] }
- it 'does not reset the variant.count_on_hand' do
+ it 'does not reset the variant.on_hand' do
products_reset.reset(supplier_ids)
- expect(variant.reload.count_on_hand).to eq(2)
+ expect(variant.reload.on_hand).to eq(2)
end
end
context 'and supplier_ids is nil' do
let(:supplier_ids) { nil }
- it 'does not reset the variant.count_on_hand' do
+ it 'does not reset the variant.on_hand' do
products_reset.reset(supplier_ids)
- expect(variant.reload.count_on_hand).to eq(2)
+ expect(variant.reload.on_hand).to eq(2)
end
end
context 'and supplier_ids is not nil' do
- it 'sets all count_on_hand to 0' do
+ it 'sets all on_hand to 0' do
updated_records_count = products_reset.reset(supplier_ids)
- expect(variant.reload.count_on_hand).to eq(0)
+ expect(variant.reload.on_hand).to eq(0)
expect(updated_records_count).to eq(1)
end
context 'and there is an unresetable variant' do
before do
- variant.stock_items = [] # this makes variant.count_on_hand raise an error
+ variant.stock_items = [] # this makes variant.on_hand raise an error
end
it 'returns correct number of resetted variants' do
@@ -96,24 +96,24 @@ describe ProductImport::ProductsResetStrategy do
context 'and supplier_ids is []' do
let(:supplier_ids) { [] }
- it 'does not reset the variant.count_on_hand' do
+ it 'does not reset the variant.on_hand' do
products_reset.reset(supplier_ids)
- expect(variant.reload.count_on_hand).to eq(2)
+ expect(variant.reload.on_hand).to eq(2)
end
end
context 'and supplier_ids is nil' do
let(:supplier_ids) { nil }
- it 'does not reset the variant.count_on_hand' do
+ it 'does not reset the variant.on_hand' do
products_reset.reset(supplier_ids)
- expect(variant.reload.count_on_hand).to eq(2)
+ expect(variant.reload.on_hand).to eq(2)
end
end
context 'and supplier_ids is nil' do
- it 'sets all count_on_hand to 0' do
+ it 'sets all on_hand to 0' do
products_reset.reset(supplier_ids)
- expect(variant.reload.count_on_hand).to eq(0)
+ expect(variant.reload.on_hand).to eq(0)
end
end
end
diff --git a/spec/models/spree/product_spec.rb b/spec/models/spree/product_spec.rb
index a060e34a09..d3678cda68 100644
--- a/spec/models/spree/product_spec.rb
+++ b/spec/models/spree/product_spec.rb
@@ -629,14 +629,14 @@ module Spree
describe "stock filtering" do
it "considers products that are on_demand as being in stock" do
product = create(:simple_product, on_demand: true)
- product.master.update_attribute(:count_on_hand, 0)
+ product.master.update_attribute(:on_hand, 0)
product.has_stock?.should == true
end
describe "finding products in stock for a particular distribution" do
it "returns on-demand products" do
p = create(:simple_product, on_demand: true)
- p.variants.first.update_attributes!(count_on_hand: 0, on_demand: true)
+ p.variants.first.update_attributes!(on_hand: 0, on_demand: true)
d = create(:distributor_enterprise)
oc = create(:simple_order_cycle, distributors: [d])
oc.exchanges.outgoing.first.variants << p.variants.first
@@ -647,7 +647,7 @@ module Spree
it "returns products with in-stock variants" do
p = create(:simple_product)
v = create(:variant, product: p)
- v.update_attribute(:count_on_hand, 1)
+ v.update_attribute(:on_hand, 1)
d = create(:distributor_enterprise)
oc = create(:simple_order_cycle, distributors: [d])
oc.exchanges.outgoing.first.variants << v
@@ -658,7 +658,7 @@ module Spree
it "returns products with on-demand variants" do
p = create(:simple_product)
v = create(:variant, product: p, on_demand: true)
- v.update_attribute(:count_on_hand, 0)
+ v.update_attribute(:on_hand, 0)
d = create(:distributor_enterprise)
oc = create(:simple_order_cycle, distributors: [d])
oc.exchanges.outgoing.first.variants << v
@@ -668,7 +668,7 @@ module Spree
it "does not return products that have stock not in the distribution" do
p = create(:simple_product)
- p.master.update_attribute(:count_on_hand, 1)
+ p.master.update_attribute(:on_hand, 1)
d = create(:distributor_enterprise)
oc = create(:simple_order_cycle, distributors: [d])
diff --git a/spec/requests/shop_spec.rb b/spec/requests/shop_spec.rb
index 359549fa41..c73e8bd099 100644
--- a/spec/requests/shop_spec.rb
+++ b/spec/requests/shop_spec.rb
@@ -23,15 +23,15 @@ describe "Shop API", type: :request do
before do
set_order order
- v61.update_attribute(:count_on_hand, 1)
+ v61.update_attribute(:on_hand, 1)
p6.destroy
- v71.update_attribute(:count_on_hand, 1)
- v41.update_attribute(:count_on_hand, 1)
- v42.update_attribute(:count_on_hand, 0)
- v43.update_attribute(:count_on_hand, 0)
- v51.update_attribute(:count_on_hand, 1)
- v52.update_attribute(:count_on_hand, 0)
- v71.update_attribute(:count_on_hand, 1)
+ v71.update_attribute(:on_hand, 1)
+ v41.update_attribute(:on_hand, 1)
+ v42.update_attribute(:on_hand, 0)
+ v43.update_attribute(:on_hand, 0)
+ v51.update_attribute(:on_hand, 1)
+ v52.update_attribute(:on_hand, 0)
+ v71.update_attribute(:on_hand, 1)
v71.update_attribute(:deleted_at, Time.zone.now)
exchange = Exchange.find(oc1.exchanges.to_enterprises(distributor).outgoing.first.id)
exchange.update_attribute :pickup_time, "frogs"
diff --git a/spec/serializers/variant_serializer_spec.rb b/spec/serializers/variant_serializer_spec.rb
index 3c6aebf6a9..583412ff56 100644
--- a/spec/serializers/variant_serializer_spec.rb
+++ b/spec/serializers/variant_serializer_spec.rb
@@ -10,7 +10,7 @@ describe Api::VariantSerializer do
:id,
:name_to_display,
:is_master,
- :count_on_hand,
+ :on_hand,
:name_to_display,
:unit_to_display,
:unit_value,
diff --git a/spec/services/order_factory_spec.rb b/spec/services/order_factory_spec.rb
index 3755f69e8e..672ac0a780 100644
--- a/spec/services/order_factory_spec.rb
+++ b/spec/services/order_factory_spec.rb
@@ -55,7 +55,7 @@ describe OrderFactory do
context "when requested quantity is greater than available stock" do
context "when no override is present" do
before do
- variant1.update_attribute(:count_on_hand, 2)
+ variant1.update_attribute(:on_hand, 2)
attrs[:line_items].first[:quantity] = 5
end
diff --git a/spec/services/order_syncer_spec.rb b/spec/services/order_syncer_spec.rb
index e1ee400fc8..8645eb674e 100644
--- a/spec/services/order_syncer_spec.rb
+++ b/spec/services/order_syncer_spec.rb
@@ -292,7 +292,7 @@ describe OrderSyncer do
let(:sli) { subscription.subscription_line_items.first }
let(:variant) { sli.variant }
- before { variant.update_attribute(:count_on_hand, 2) }
+ before { variant.update_attribute(:on_hand, 2) }
context "when quantity is within available stock" do
let(:params) { { subscription_line_items_attributes: [{ id: sli.id, quantity: 2}] } }
@@ -327,7 +327,7 @@ describe OrderSyncer do
let(:syncer) { OrderSyncer.new(subscription) }
let(:changed_line_item) { order.line_items.find_by_variant_id(sli.variant_id) }
- before { variant.update_attribute(:count_on_hand, 3) }
+ before { variant.update_attribute(:on_hand, 3) }
context "when the changed line_item quantity matches the new quantity on the subscription line item" do
before { changed_line_item.update_attributes(quantity: 3) }
diff --git a/spec/services/variants_stock_levels_spec.rb b/spec/services/variants_stock_levels_spec.rb
index a7e788d657..f4a77d7018 100644
--- a/spec/services/variants_stock_levels_spec.rb
+++ b/spec/services/variants_stock_levels_spec.rb
@@ -12,39 +12,39 @@ describe VariantsStockLevels do
let(:variant_stock_levels) { VariantsStockLevels.new }
before do
- variant_in_the_order.count_on_hand = 4
- variant_not_in_the_order.count_on_hand = 2
+ variant_in_the_order.on_hand = 4
+ variant_not_in_the_order.on_hand = 2
order.reload
end
- it "returns a hash with variant id, quantity, max_quantity and stock on hand" do
+ it "returns a hash with variant id, quantity, max_quantity, on hand and on demand" do
expect(variant_stock_levels.call(order, [variant_in_the_order.id])).to eq(
- variant_in_the_order.id => { quantity: 2, max_quantity: 3, on_hand: 4 }
+ variant_in_the_order.id => { quantity: 2, max_quantity: 3, on_hand: 4, on_demand: false }
)
end
it "includes all line items, even when the variant_id is not specified" do
expect(variant_stock_levels.call(order, [])).to eq(
- variant_in_the_order.id => { quantity: 2, max_quantity: 3, on_hand: 4 }
+ variant_in_the_order.id => { quantity: 2, max_quantity: 3, on_hand: 4, on_demand: false }
)
end
it "includes an empty quantity entry for variants that aren't in the order" do
variant_ids = [variant_in_the_order.id, variant_not_in_the_order.id]
expect(variant_stock_levels.call(order, variant_ids)).to eq(
- variant_in_the_order.id => { quantity: 2, max_quantity: 3, on_hand: 4 },
- variant_not_in_the_order.id => { quantity: 0, max_quantity: 0, on_hand: 2 }
+ variant_in_the_order.id => { quantity: 2, max_quantity: 3, on_hand: 4, on_demand: false },
+ variant_not_in_the_order.id => { quantity: 0, max_quantity: 0, on_hand: 2, on_demand: false }
)
end
- describe "encoding Infinity" do
+ describe "when variant is on_demand" do
let!(:variant_in_the_order) { create(:variant, on_demand: true) }
- before { variant_in_the_order.count_on_hand = 0 }
+ before { variant_in_the_order.on_hand = 0 }
- it "encodes Infinity as a large, finite integer" do
+ it "includes the actual on_hand value and on_demand: true" do
expect(variant_stock_levels.call(order, [variant_in_the_order.id])).to eq(
- variant_in_the_order.id => { quantity: 2, max_quantity: 3, on_hand: 2147483647 }
+ variant_in_the_order.id => { quantity: 2, max_quantity: 3, on_hand: 0, on_demand: true }
)
end
end
|