Skip to content

Commit

Permalink
No longer fill in backorders for incomplete orders (i.e. abandoned ca…
Browse files Browse the repository at this point in the history
…rts)

Fixes solidusio#4056
  • Loading branch information
sohara authored and radar committed Dec 10, 2013
1 parent 101dcfd commit adc8a4f
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 3 deletions.
5 changes: 3 additions & 2 deletions core/app/models/spree/inventory_unit.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,11 @@ class InventoryUnit < ActiveRecord::Base
scope :backordered, -> { where state: 'backordered' }
scope :shipped, -> { where state: 'shipped' }
scope :backordered_per_variant, ->(stock_item) do
includes(:shipment)
includes(:shipment, :order)
.where("spree_shipments.state != 'canceled'").references(:shipment)
.where(variant_id: stock_item.variant_id)
.backordered.order("#{self.table_name}.created_at ASC")
.where('spree_orders.completed_at is not null')
.backordered.order("spree_orders.completed_at ASC")
end

# state machine (see https://github.com/pluginaweek/state_machine/tree/master for details)
Expand Down
41 changes: 40 additions & 1 deletion core/spec/models/spree/inventory_unit_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,12 @@
let(:stock_item) { stock_location.stock_items.order(:id).first }

context "#backordered_for_stock_item" do
let(:order) { create(:order) }
let(:order) do
order = create(:order)
order.state = 'complete'
order.completed_at = Time.now
order.tap(&:save!)
end

let(:shipment) do
shipment = Spree::Shipment.new
Expand All @@ -21,6 +26,7 @@
unit = shipment.inventory_units.build
unit.state = 'backordered'
unit.variant_id = stock_item.variant.id
unit.order_id = order.id
unit.tap(&:save!)
end

Expand Down Expand Up @@ -51,6 +57,39 @@

Spree::InventoryUnit.backordered_for_stock_item(stock_item).should_not include(other_variant_unit)
end

context "other shipments" do
let(:other_order) do
order = create(:order)
order.state = 'payment'
order.completed_at = nil
order.tap(&:save!)
end

let(:other_shipment) do
shipment = Spree::Shipment.new
shipment.stock_location = stock_location
shipment.shipping_methods << create(:shipping_method)
shipment.order = other_order
# We don't care about this in this test
shipment.stub(:ensure_correct_adjustment)
shipment.tap(&:save!)
end

let!(:other_unit) do
unit = other_shipment.inventory_units.build
unit.state = 'backordered'
unit.variant_id = stock_item.variant.id
unit.order_id = other_order.id
unit.tap(&:save!)
end

it "does not find inventory units belonging to incomplete orders" do
Spree::InventoryUnit.backordered_for_stock_item(stock_item).should_not include(other_unit)
end

end

end

context "variants deleted" do
Expand Down

0 comments on commit adc8a4f

Please sign in to comment.