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

Fix unexpected error about not enough stocks while completing the payment in the Admin Panel #15688

Closed
wants to merge 1 commit into from

Conversation

jakubtobiasz
Copy link
Contributor

@jakubtobiasz jakubtobiasz commented Dec 30, 2023

Q A
Branch? 1.12
Bug fix? yes
New feature? no
BC breaks? no
Deprecations? no
Related tickets #16160
License MIT

@jakubtobiasz jakubtobiasz added the Bug Confirmed bugs or bugfixes. label Dec 30, 2023
@jakubtobiasz jakubtobiasz requested a review from a team as a code owner December 30, 2023 18:46
Copy link

Bunnyshell Preview Environment deployed

It will be automatically stopped in 4 hours.

Use the command /bns:start to start it if it's stopped.

Component Endpoints
mailhog https://mailhog-mwdahs.bunnyenv.com/
php https://store-mwdahs.bunnyenv.com/

Available commands:

  • /bns:stop to stop the environment
  • /bns:deploy to redeploy the environment
  • /bns:delete to remove the environment

return false;
}

if ($variant->getOnHand() - $quantity < 0) {
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, but I feel that we should only check onHold. However, what happens if someone orders a product variant and, before completing this order, the administrator activates tracking for this variant?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This logic comes from the \Sylius\Component\Core\Inventory\Operator\OrderInventoryOperator::sell method.

<?php

// ...
Assert::greaterThanEq(
                ($variant->getOnHold() - $orderItem->getQuantity()),
                0,
                sprintf(
                    'Not enough units to decrease on hold quantity from the inventory of a variant "%s".',
                    $variant->getName(),
                ),
            );

            Assert::greaterThanEq(
                ($variant->getOnHand() - $orderItem->getQuantity()),
                0,
                sprintf(
                    'Not enough units to decrease on hand quantity from the inventory of a variant "%s".',
                    $variant->getName(),
                ),
            );
// ...

So both values are checked, same as in the example above. If you hold a variant, the onHand value is not decreased.

1. There're 4 socks in stock (onHand: 4, onHold: 0)
2. You buy 2 socks with using the offline payment method (onHand: 4, onHold 2)
3. As an admin you complete the payment (onHand: 2, onHold: 0)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

However, what happens if someone orders a product variant and, before completing this order, the administrator activates tracking for this variant?

I guess we have to think about it, as currently it'll throw an exception. We'll discuss it on the daily meeting, and create a task if needed.

Copy link
Member

Choose a reason for hiding this comment

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

This onHand/onHold pre-sell check logic feels important enough to varant it's own checker.

For example, why not add new method canBeSold to

public function sell(OrderInterface $order): void
or make brand new OrderInventoryChecker

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 it needs a bit of work.
Because the

or Assertions introduce a bug into Sylius.

Let me explain:

  • On a Variant, stock management is activated
  • A customer places an order with this variant
  • An administrator deactivates stock management on this variant
  • On actions, such as payment, order cancellation, etc., we get a 500 error because the assertion is not correct.

@@ -99,7 +99,6 @@
</service>

<service id="Sylius\Bundle\CoreBundle\EventListener\PaymentPreCompleteListener">
<argument type="service" id="Sylius\Component\Inventory\Checker\AvailabilityCheckerInterface" />
Copy link
Member

Choose a reason for hiding this comment

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

Just in case, can service's argument be removed BC wise?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That's debatable. In theory, it can "break" something in terms of the system might start behaving differently. On the other hand,

  • we never bothered about it, as our hands could be tied too much and make Sylius development harder
  • our documentation doesn't mention it
  • Symfony documentation also doesn't mention anything about it

Copy link
Member

Choose a reason for hiding this comment

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

Something happened with this file,

  • file name is not right
  • feature/background parts are identical to finalizing_order_payment.feature aside from inventory line.

And the "Angel T-Shirt" product's inventory has become tracked with 1 items

Can this line be moved from background to scenario, so original Finalizing order payment and this feature file could be merged?

Otherwise, please rename file and feature accordingly.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Right, I wanted to just reproduce the bug, I forgot to merge/rename that file.

return false;
}

if ($variant->getOnHand() - $quantity < 0) {
Copy link
Member

Choose a reason for hiding this comment

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

This onHand/onHold pre-sell check logic feels important enough to varant it's own checker.

For example, why not add new method canBeSold to

public function sell(OrderInterface $order): void
or make brand new OrderInventoryChecker

NoResponseMate added a commit that referenced this pull request Jun 3, 2024
…ing a payment (GSadee)

This PR was merged into the 1.12 branch.

Discussion
----------

| Q               | A
|-----------------|-----
| Branch?         | 1.12
| Bug fix?        | yes
| New feature?    | no
| BC breaks?      | no
| Deprecations?   | no
| Related tickets | caused by #15280, fixes #16160, replaces #15688, closes #16258
| License         | MIT

<!--
 - Bug fixes must be submitted against the 1.12 or 1.13 branches
 - Features and deprecations must be submitted against the 1.14 branch
 - Features, removing deprecations and BC breaks must be submitted against the 2.0 branch
 - Make sure that the correct base branch is set

 To be sure you are not breaking any Backward Compatibilities, check the documentation:
 https://docs.sylius.com/en/latest/book/organization/backward-compatibility-promise.html
-->


Commits
-------
  Fix unexpected error about not enough stocks while completing the payment in the Admin Panel
  [Behat] Refactor scenarios for finalizing order's payment
  Add spec for PaymentPreCompleteListener with minor improvements
  [Behat] Cover scenarios for finalizing order's payment in API
  [Inventory] Introduce custom exceptions for inventory operations
  [Behat] Adjust scenario and feature names for finalizing payment with item that has become tracked
@Rafikooo
Copy link
Contributor

Rafikooo commented Jun 4, 2024

I'm closing this ticket as the problem was resolved here: #16307

@Rafikooo Rafikooo closed this Jun 4, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Confirmed bugs or bugfixes.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants