-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
[RFC] Add FulfillmentChanger #2070
Merged
jhawthorn
merged 6 commits into
solidusio:master
from
mamhoff:introduce-fulfilment-changer
Jul 28, 2017
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
19c1804
Fix Order Details spec
mamhoff 7de80e4
Add FulfillmentChanger
mamhoff e044c78
Use FulfilmentChanger for transfer_to_* endpoints
mamhoff 40b6767
Deprecate Spree::Shipment#transfer_to_*
mamhoff aabfaf5
Add Changelog entry for changes to shipment fulfilment
mamhoff b7e73a4
Merge branch 'master' into introduce-fulfilment-changer
jhawthorn File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
module Spree | ||
# Service class to change fulfilment of inventory units of a particular variant | ||
# to another shipment. The other shipment would typically have a different | ||
# shipping method, stock location or delivery date, such that we actually change | ||
# the planned fulfilment for the items in question. | ||
# | ||
# Can be used to merge shipments by moving all items to another shipment, because | ||
# this class will delete any empty original shipment. | ||
# | ||
# @attr [Spree::Shipment] current_shipment The shipment we transfer units from | ||
# @attr [Spree::Shipment] desired_shipment The shipment we want to move units onto | ||
# @attr [Spree::StockLocation] current_stock_location The stock location of the current shipment | ||
# @attr [Spree::StockLocation] desired_stock_location The stock location of the desired shipment | ||
# @attr [Spree::Variant] variant We only move units that represent this variant | ||
# @attr [Integer] quantity How many units we want to move | ||
# | ||
class FulfilmentChanger | ||
include ActiveModel::Validations | ||
|
||
attr_accessor :current_shipment, :desired_shipment | ||
attr_reader :variant, :quantity, :current_stock_location, :desired_stock_location | ||
|
||
def initialize(current_shipment:, desired_shipment:, variant:, quantity:) | ||
@current_shipment = current_shipment | ||
@desired_shipment = desired_shipment | ||
@current_stock_location = current_shipment.stock_location | ||
@desired_stock_location = desired_shipment.stock_location | ||
@variant = variant | ||
@quantity = quantity | ||
end | ||
|
||
validates :quantity, numericality: { greater_than: 0 } | ||
validate :current_shipment_not_already_shipped | ||
validate :desired_shipment_different_from_current | ||
validates :desired_stock_location, presence: true | ||
validate :enough_stock_at_desired_location, if: :handle_stock_counts? | ||
|
||
# Performs the change of fulfilment | ||
# @return [true, false] Whether the requested fulfilment change was successful | ||
def run! | ||
# Validations here are intended to catch all necessary prerequisites. | ||
# We return early so all checks have happened already. | ||
return false if invalid? | ||
desired_shipment.save! if desired_shipment.new_record? | ||
|
||
new_on_hand_quantity = [desired_shipment.stock_location.count_on_hand(variant), quantity].min | ||
|
||
ActiveRecord::Base.transaction do | ||
if handle_stock_counts? | ||
# We only run this query if we need it. | ||
current_on_hand_quantity = [current_shipment.inventory_units.on_hand.size, quantity].min | ||
|
||
# Restock things we will not fulfil from the current shipment anymore | ||
current_stock_location.restock(variant, current_on_hand_quantity, current_shipment) | ||
# Unstock what we will fulfil with the new shipment | ||
desired_stock_location.unstock(variant, new_on_hand_quantity, desired_shipment) | ||
end | ||
|
||
# These two statements are the heart of this class. We change the number | ||
# of inventory units requested from one shipment to the other. | ||
# We order by state, because `'backordered' < 'on_hand'`. | ||
current_shipment. | ||
inventory_units. | ||
where(variant: variant). | ||
order(state: :asc). | ||
limit(new_on_hand_quantity). | ||
update_all(shipment_id: desired_shipment.id, state: :on_hand) | ||
|
||
current_shipment. | ||
inventory_units. | ||
where(variant: variant). | ||
order(state: :asc). | ||
limit(quantity - new_on_hand_quantity). | ||
update_all(shipment_id: desired_shipment.id, state: :backordered) | ||
end | ||
|
||
# We modified the inventory units at the database level for speed reasons. | ||
# The downside of that is that we need to reload the associations. | ||
current_shipment.inventory_units.reload | ||
desired_shipment.inventory_units.reload | ||
|
||
# If the current shipment now has no inventory units left, we won't need it any longer. | ||
if current_shipment.inventory_units.length.zero? | ||
current_shipment.destroy! | ||
else | ||
# The current shipment has changed, so we need to make sure that shipping rates | ||
# have the correct amount. | ||
current_shipment.refresh_rates | ||
end | ||
|
||
# The desired shipment has also change, so we need to make sure shipping rates | ||
# are up-to-date, too. | ||
desired_shipment.refresh_rates | ||
|
||
true | ||
end | ||
|
||
private | ||
|
||
# We don't need to handle stock counts for incomplete orders. Also, if | ||
# the new shipment and the desired shipment will ship from the same stock location, | ||
# unstocking and restocking will not be necessary. | ||
def handle_stock_counts? | ||
current_shipment.order.completed? && current_stock_location != desired_stock_location | ||
end | ||
|
||
def current_shipment_not_already_shipped | ||
if current_shipment.shipped? | ||
errors.add(:current_shipment, :has_already_been_shipped) | ||
end | ||
end | ||
|
||
def enough_stock_at_desired_location | ||
unless Spree::Stock::Quantifier.new(variant, desired_stock_location).can_supply?(quantity) | ||
errors.add(:desired_shipment, :not_enough_stock_at_desired_location) | ||
end | ||
end | ||
|
||
def desired_shipment_different_from_current | ||
if desired_shipment.id == current_shipment.id | ||
errors.add(:desired_shipment, :can_not_transfer_within_same_shipment) | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could we use
show_flash
instead of analert
here?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Using
show_flash
resulted in quite a lot of testing trouble for some reason: Locked Sqlite databases, hung phantomjs, weird things. Can we move that to a separate PR?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure