Merge pull request #3513 from luisramos0/2-0-remove-variant-count-on-hand

[Spree Upgrade] Remove variant.count_on_hand (keep variant.on_hand only)
This commit is contained in:
Maikel
2019-02-28 09:07:41 +11:00
committed by GitHub
40 changed files with 172 additions and 204 deletions

View File

@@ -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')

View File

@@ -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 } }}

View File

@@ -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}}"}

View File

@@ -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,

View File

@@ -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

View File

@@ -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'])

View File

@@ -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

View File

@@ -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

View File

@@ -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

View File

@@ -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

View File

@@ -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

View File

@@ -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

View File

@@ -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

View File

@@ -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

View File

@@ -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

View File

@@ -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

View File

@@ -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' }

View File

@@ -16,7 +16,7 @@
</ul>
<ul class='variant-data'>
<li class='variant-sku'><strong><%= t('admin.sku') %>:</strong> {{variant.sku}}</li>
<li class='variant-on_hand'><strong><%= t('on_hand') %>:</strong> {{variant.count_on_hand}}</li>
<li class='variant-on_hand'><strong><%= t('on_hand') %>:</strong> {{variant.on_hand}}</li>
</ul>
{{#if variant.option_values}}
@@ -41,7 +41,7 @@
</colgroup>
<thead>
<th><%= Spree.t(:location) %></th>
<th><%= Spree.t(:count_on_hand) %></th>
<th><%= Spree.t(:on_hand) %></th>
<th><%= Spree.t(:quantity) %></th>
<th class="actions"></th>
</thead>
@@ -53,7 +53,7 @@
<td>Spree.t(:on_demand)</td>
{{else}}
<td>
{{variant.count_on_hand}}
{{variant.on_hand}}
</td>
{{/if}}
<td>

View File

@@ -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

View File

@@ -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

View File

@@ -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

View File

@@ -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

View File

@@ -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")]

View File

@@ -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) }

View File

@@ -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

View File

@@ -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}

View File

@@ -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

View File

@@ -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", ->

View File

@@ -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

View File

@@ -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

View File

@@ -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

View File

@@ -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

View File

@@ -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)

View File

@@ -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

View File

@@ -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])

View File

@@ -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"

View File

@@ -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,

View File

@@ -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

View File

@@ -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) }

View File

@@ -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