Skip to content

Commit

Permalink
add variants with status of backordered or on_hand to packages
Browse files Browse the repository at this point in the history
  • Loading branch information
cmar committed Feb 14, 2013
1 parent cb6a772 commit 9a1f29a
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 16 deletions.
20 changes: 15 additions & 5 deletions core/app/models/spree/StockPackage.rb
Original file line number Diff line number Diff line change
@@ -1,23 +1,33 @@
module Spree
class StockPackage
ContentItem = Struct.new(:variant, :quantity, :status)

attr_accessor :stock_location, :contents

def initialize(stock_location)
@stock_location = stock_location
@contents = Array.new
end

def add(stock_item)
contents << stock_item
def add(variant, quantity, status=:on_hand)
contents << ContentItem.new(variant, quantity, status)
end

def weight
contents.sum &:weight
contents.sum { |item| item.variant.weight }
end

def on_hand
contents.select { |item| item.status == :on_hand }
end

def backordered
contents.select { |item| item.status == :backordered }
end

def inspect
contents.map do |stock_item|
"#{stock_item.variant.name}"
contents.map do |content_item|
"#{content_item.variant.name} #{content_item.quantity} #{content_item.status}"
end.join(',')
end
end
Expand Down
18 changes: 12 additions & 6 deletions core/app/models/spree/stock_location.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ def packages(order)
def default_package(order)
package = StockPackage.new(self)
order.line_items.each do |line_item|
line_item.quantity.times do
package.add stock_item_for_variant(line_item.variant)
end
on_hand, backordered = stock_status(line_item.variant, line_item.quantity)
package.add line_item.variant, on_hand, :on_hand if on_hand > 0
package.add line_item.variant, backordered, :backordered if backordered > 0
end
package
end
Expand All @@ -28,11 +28,17 @@ def splitter(order)

private
def stock_item_for_variant(variant)
stock_items.first
stock_items.where(variant: variant).first
end

def count_on_hand(variant_id)
stock_item = stock_items.where(variant_id: variant_id).first
def stock_status(variant, quantity)
on_hand = count_on_hand(variant)
#TODO fancy math for backorder counts
[on_hand, 0]
end

def count_on_hand(variant)
stock_item = stock_items.where(variant_id: variant).first
stock_item.try(:count_on_hand)
end
end
Expand Down
28 changes: 23 additions & 5 deletions core/spec/models/spree/stock_location_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,28 @@ module Spree
let(:order) { create(:order_with_line_items, line_items_count: 5) }
subject { create(:stock_location) }

it 'builds a default package of all the items' do
order.reload #temp fix for bug where line_items weren't loaded
package = subject.default_package(order)
package.contents.size.should eq 5
package.weight.should > 0
before :all do
order.reload
order.line_items.each do |line_item|
create(:stock_item,
variant: line_item.variant,
stock_location: subject)
end
end

context 'default_package' do
it 'contains all the items' do
package = subject.default_package(order)
package.contents.size.should eq 5
package.weight.should > 0
end

it 'variants are added as backordered without enough on_hand' do
subject.should_receive(:stock_status).exactly(5).times.and_return([2,3])
package = subject.default_package(order)
package.on_hand.size.should eq 5
package.backordered.size.should eq 5
end
end

it 'builds an array of packages' do
Expand All @@ -18,5 +35,6 @@ module Spree
packages.size.should eq 1
packages.first.contents.size.should eq 5
end

end
end

0 comments on commit 9a1f29a

Please sign in to comment.