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

Allow order with multiple line items to be marked as "Returned" #3199

Conversation

spaghetticode
Copy link
Member

Description

This PR fixes issue #3198

When a customer return is created for an order with multiple line items and the
return state of all the items is Received then the order state should change
to Returned just like it happens with orders with only one line item.

Checklist:

  • I have followed Pull Request guidelines
  • I have added a detailed description into each commit message
  • I have added tests to cover this change (if needed)

@spaghetticode spaghetticode force-pushed the spaghetticode/fix-3198-order-state-after-return branch from 6b5082a to 3be97d7 Compare May 3, 2019 16:11
Copy link
Member

@kennyadsl kennyadsl left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @spaghetticode, I don't get why this change is necessary, even if the issue and the solution are pretty clear. I just need some more info about it, thanks!

@@ -286,7 +286,7 @@ def allow_cancel?
end

def all_inventory_units_returned?
inventory_units.all?(&:returned?)
inventory_units.returned.count == inventory_units.count
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure to understand why the former code is not working. 🤔 Did you get that? Can you please explain it a little bit?

Copy link
Member Author

@spaghetticode spaghetticode May 6, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This code is executed for each return item, for a better understanding of the process you can start from ReturnItem#process_inventory_unit!. The first inventory unit is marked as returned, and eventually order.inventory.units is called.

The query is executed only for the first return item, and as we know there's no issue as long as the customer return is for one single item. But when there's more than one, from the second time on, order.inventory.units refers to stale data, and must be updated. Using count ensures a new (lightweight) query is performed.

Basically, it happens something similar to this:

o = create :shipped_order, line_items_count: 2
o.inventory_units.all? &:shipped?
=> true

u = Spree::InventoryUnit.find(o.inventory_units.first.id)
u.return!
u.reload.returned?
=> true

o.inventory_units.all? &:shipped?
=> true

@spaghetticode spaghetticode force-pushed the spaghetticode/fix-3198-order-state-after-return branch 5 times, most recently from d644b96 to 7a94c47 Compare May 10, 2019 07:56
if customer_return
customer_return.stock_location.restock(inventory_unit.variant, 1, customer_return) if should_restock?
customer_return.process_return!
unless skip_customer_return_processing
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should also test that a deprecation warning is raised in some circumstances.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@@ -38,7 +38,10 @@
let(:order) { customer_return.order }
let(:return_item) { customer_return.return_items.first }
let(:payment) { order.payments.first }
before { return_item.receive! }
before do
return_item.skip_customer_return_processing = true
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you think it could be a good idea to add this as default in the return item factory?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I totally agree 👍
This also reduced the changes I had to make to spec files

@spaghetticode spaghetticode force-pushed the spaghetticode/fix-3198-order-state-after-return branch from 7a94c47 to 0b43aed Compare May 15, 2019 10:13
…! callback

Processing order returns in `ReturnItem#process_inventory_unit!` does not work
reliably when there are multiple return items processed at the same time (see
issue solidusio#3198).

Also, decoupling sensible interactions with other models in ActiveRecord
callbacks simplifies the understandability of code and its flow.

Order processing in `#process_inventory_unit!` is now only deprecated, but will
be completely removed in the following release of Solidus.
…m specs

Deprecations make test fails on circleCI. The easiest way to remove them is
by updating the `return_item` factory.
…eturn!

The condition that verifies if an order can change state to `returned` is now
completely delegated to the order state machine in order to avoid leaking of
context and knowledge.
@spaghetticode spaghetticode force-pushed the spaghetticode/fix-3198-order-state-after-return branch from 0b43aed to 4870e08 Compare May 15, 2019 10:20
Copy link
Member

@kennyadsl kennyadsl left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

Copy link
Contributor

@jacobherrington jacobherrington left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks good to me. Thanks Andrea!!

@kennyadsl kennyadsl merged commit 897dc04 into solidusio:master May 28, 2019
@kennyadsl kennyadsl deleted the spaghetticode/fix-3198-order-state-after-return branch May 28, 2019 10:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants