Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Upgrade to spree 3.1+, fixes for inventory units, packing and shipments #157

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ Gemfile.lock
coverage
.ruby-gemset
.ruby-version
gemfiles/*.gemfile.lock
34 changes: 25 additions & 9 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,13 +1,29 @@
---
sudo: required
dist: trusty

language: ruby
sudo: false
cache: bundler
before_script:
- sh -e /etc/init.d/xvfb start
- export DISPLAY=:99.0
- bundle exec rake test_app

env:
- DB=postgres
- DB=mysql

gemfile:
- gemfiles/spree_3_1.gemfile
- gemfiles/spree_3_2.gemfile
- gemfiles/spree_master.gemfile

script:
- bundle exec rspec spec
- bundle exec rake test_app
- bundle exec rake spec

rvm:
- 2.2.4
- 2.3.0
- 2.3.1
- 2.2.5

addons:
apt:
packages:
- mysql-server-5.6
- mysql-client-core-5.6
- mysql-client-5.6
13 changes: 13 additions & 0 deletions Appraisals
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
appraise 'spree-3-1' do
gem 'spree', '~> 3.1.0'
end

appraise 'spree-3-2' do
gem 'spree', '~> 3.2.0'
gem 'rails-controller-testing'
end

appraise 'spree-master' do
gem 'spree', github: 'spree/spree', branch: 'master'
gem 'rails-controller-testing'
end
2 changes: 0 additions & 2 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,4 @@ source "https://rubygems.org"

gem 'spree', github: 'spree/spree', branch: 'master'

gem 'pry-rails'

gemspec
14 changes: 14 additions & 0 deletions app/models/spree/order_decorator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
module Spree
Order.class_eval do
after_update :clean_inventory_units

# during checkout each inventory_unit is saved twice
# second unit is not associated to shipment and is unnecessary
def clean_inventory_units
if state == 'delivery'
units = inventory_units.where(shipment: nil)
InventoryUnit.destroy(units.ids) if units.any?
end
end
end
end
27 changes: 27 additions & 0 deletions app/models/spree/order_inventory_assembly.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ def initialize(line_item)
@order = line_item.order
@line_item = line_item
@product = line_item.product
@variant = line_item.variant
end

def verify(shipment = nil)
Expand All @@ -25,6 +26,32 @@ def verify(shipment = nil)

private

def add_to_shipment(shipment, quantity)
units = shipment.inventory_units
if variant.should_track_inventory?
on_hand, back_order = shipment.stock_location.fill_status(variant, quantity)

on_hand.times { units << set_up_invetory_unit('on_hand', variant, order, line_item) }
back_order.times { units << set_up_invetory_unit('backordered', variant, order, line_item) }
else
quantity.times { units << set_up_invetory_unit('on_hand', variant, order, line_item) }
end

shipment.inventory_units = units
shipment.save

# adding to this shipment, and removing from stock_location
if order.completed?
shipment.stock_location.unstock(variant, quantity, shipment)
end

quantity
end

def set_up_invetory_unit(state, variant, order, line_item)
InventoryUnit.new(state: state, variant_id: variant.id, order_id: order.id, line_item_id: line_item.id)
end

def verify_parts(shipment, total_parts, existing_parts)
if existing_parts < total_parts
verifiy_add_to_shipment(shipment, total_parts, existing_parts)
Expand Down
45 changes: 45 additions & 0 deletions app/models/spree/stock/coordinator_decorator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
module Spree
module Stock
Coordinator.class_eval do
attr_reader :allocated_inventory_units

def initialize(order, inventory_units = nil)
@order = order
@inventory_units = inventory_units || InventoryUnitBuilder.new(order).units
@allocated_inventory_units = []
end

def build_packages(packages = Array.new)
stock_locations_with_requested_variants.each do |stock_location|
packer = build_packer(stock_location, unallocated_inventory_units)
packages += packer.packages
@allocated_inventory_units += packer.allocated_inventory_units
end

packages
end

private

def unallocated_inventory_units
if inventory_units.map(&:id).include?(nil)
allocated_inventory_units.each do |allocated|
inventory_units.each_with_index do |unit, index|
if should_delete_item?(allocated, unit)
inventory_units.delete_at(index)
break
end
end
end
inventory_units
else
inventory_units - allocated_inventory_units
end
end

def should_delete_item?(allocated, unit)
allocated.line_item_id == unit.line_item_id && allocated.variant_id == unit.variant_id
end
end
end
end
33 changes: 33 additions & 0 deletions app/models/spree/stock/packer_decorator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
module Spree
module Stock
Packer.class_eval do
attr_reader :allocated_inventory_units

def initialize(stock_location, inventory_units, splitters = [Splitter::Base])
@stock_location = stock_location
@inventory_units = inventory_units
@splitters = splitters
@allocated_inventory_units = []
end

def default_package
package = Package.new(stock_location)

inventory_units.each do |inventory_unit|
variant = inventory_unit.variant
unit = inventory_unit.dup # Can be used by others, do not use directly
if variant.should_track_inventory?
next unless stock_location.stock_item(variant)
on_hand, backordered = stock_location.fill_status(variant, 1)
package.add(unit, :backordered) if backordered > 0
package.add(unit, :on_hand) if on_hand > 0
else
package.add unit
end
allocated_inventory_units << unit
end
package
end
end
end
end
18 changes: 18 additions & 0 deletions app/models/spree/stock/prioritizer_decorator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
module Spree
module Stock
Prioritizer.class_eval do

private

def hash_item(item)
shipment = item.inventory_unit.shipment
inventory_unit = item.inventory_unit
if shipment.present?
inventory_unit.hash ^ shipment.hash
else
inventory_unit.hash
end
end
end
end
end
7 changes: 7 additions & 0 deletions gemfiles/spree_3_1.gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# This file was generated by Appraisal

source "https://rubygems.org"

gem "spree", "~> 3.1.0"

gemspec :path => "../"
8 changes: 8 additions & 0 deletions gemfiles/spree_3_2.gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# This file was generated by Appraisal

source "https://rubygems.org"

gem "spree", "~> 3.2.0"
gem "rails-controller-testing"

gemspec :path => "../"
8 changes: 8 additions & 0 deletions gemfiles/spree_master.gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# This file was generated by Appraisal

source "https://rubygems.org"

gem "spree", :github => "spree/spree", :branch => "master"
gem "rails-controller-testing"

gemspec :path => "../"
2 changes: 1 addition & 1 deletion spec/features/admin/orders_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

background do
bundle.master.parts << [parts]
line_item.update_attributes!(quantity: 3)
line_item.reload.update_attributes!(quantity: 3)
order.reload.create_proposed_shipments
order.finalize!
end
Expand Down
4 changes: 2 additions & 2 deletions spec/models/spree/assign_part_to_bundle_form_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ module Spree
bundle = create(:product)
part = create(:product, can_be_part: true)
assignment = AssembliesPart.create(
assembly_id: bundle.id,
assembly_id: bundle.master.id,
count: 1,
part_id: part.id
)
Expand All @@ -37,7 +37,7 @@ module Spree
bundle = create(:product)
part = create(:product, can_be_part: true)

part_options = { count: 2, part_id: part.id, assembly_id: bundle.id }
part_options = { count: 2, part_id: part.id, assembly_id: bundle.master.id }

command = AssignPartToBundleForm.new(bundle, part_options)

Expand Down
4 changes: 2 additions & 2 deletions spec/models/spree/order_contents_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,10 @@
create(:variant_in_stock, product: shirt, option_values: [blue_option])

assembly_part_keychain = create(:assemblies_part,
assembly_id: assembly.id,
assembly_id: assembly.master.id,
part_id: keychain.master.id)
assembly_part_shirt = create(:assemblies_part,
assembly_id: assembly.id,
assembly_id: assembly.master.id,
part_id: shirt.master.id,
variant_selection_deferred: true)
assembly.reload
Expand Down
38 changes: 26 additions & 12 deletions spec/models/spree/order_inventory_assembly_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -146,30 +146,44 @@ module Spree
line_item_quantity: 2,
parts: [{ count: 1 }, { count: 1 }, { count: 3 }]
)

shipped_shipment = create(:shipment, state: 'shipped')
InventoryUnit.all[0..2].each do |unit|
shipped_variant = variants.first
shipped = InventoryUnit.where(variant: shipped_variant)

shipped.each do |unit|
unit.update_attribute(:shipment_id, shipped_shipment.id)
unit.update_attribute(:state, 'shipped')
end

inventory = OrderInventoryAssembly.new(line_item)
new_quantity = 1

line_item.update_column(:quantity, 1)
inventory.verify
line_item.update_column(:quantity, new_quantity)
line_item.reload

expect(inventory.inventory_units.count).to eq 6
inventory = OrderInventoryAssembly.new(line_item)
inventory.verify

unshipped_units = unshipped_shipment.inventory_units
expect(unshipped_units.count).to eq 3
shipped_units = shipped_shipment.inventory_units

units_last_variant = new_quantity * variants.last.assemblies_variants.first.count
units_second_variant = new_quantity * variants.second.assemblies_variants.first.count
units_first_variant = shipped.count

expected_units_count = units_first_variant + units_second_variant + units_last_variant

expect(inventory.inventory_units.count).to eq expected_units_count

expect(unshipped_units.count).to eq(expected_units_count - shipped.count)
unshipped_units.each do |unit|
expect(unit.variant).to eq variants[2]
expect(unit.variant).not_to eq shipped_variant
end

shipped_units = shipped_shipment.inventory_units
expect(shipped_units.count).to eq 3
shipped_units[0..1].each do |unit|
expect(unit.variant).to eq variants[0]
expect(shipped_units.count).to eq(shipped.count)
shipped_units.each do |unit|
expect(unit.variant).to eq shipped_variant
end
expect(shipped_units[2].variant).to eq variants[1]
end
end
end
Expand Down
Loading