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

Bugfix InventoryHandler to remove units of items that were removed from cart #842

Merged
merged 4 commits into from
Jan 16, 2014

Conversation

Richtermeister
Copy link
Contributor

Currently the inventory handler only adjusts the units of items that are still in the cart,
but when an item is removed from the cart, the inventory unit remains. This PR fixes this.

…ed from the cart.

Currently the inventory handler only adjusts the units of items that are still in the cart,
but when an item is removed from the cart, the inventory unit remains. This PR fixes this.
@winzou
Copy link
Contributor

winzou commented Jan 15, 2014

Partially solves #567
It will fully solve it if you take into account that a same variant can appear in several OrderItems =)

@Richtermeister
Copy link
Contributor Author

@winzou Sorry, I forgot all about your ticket.. see if the new implementation solves things better.

@@ -113,9 +124,15 @@ protected function addInventoryUnits(OrderInterface $order, VariantInterface $va
protected function removeInventoryUnits(OrderInterface $order, VariantInterface $variant, $quantity)
{
$units = $order->getInventoryUnitsByVariant($variant);
$counter = 0;

foreach ($units as $unit) {
Copy link
Contributor

Choose a reason for hiding this comment

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

You should use $units->slice(0, $quantity) to stay elegant here.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I was wondering that too.. can I always count on collections though? The interface only hints at an array..

Copy link
Contributor

Choose a reason for hiding this comment

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

@Richtermeister I guess you can, the Collection implements \Countable interface, so it's supported.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@stloyd \Countable is not what's needed. The slice() method is on the ArrayCollection class.. but I guess we should use collections properly when we have them.. I'll make that change.

Copy link
Contributor

Choose a reason for hiding this comment

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

@Richtermeister Your question was:

can I always count on collections though?

So my answer was correct =) But additionally, all Collections must implement Collection interface, which requires: slice() to be implemented.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@stloyd Ah, sorry, what that is meant to say is "Can I always rely on Collections being returned".. as in "I'm counting on you!" :)

When you look at the specs, for example, they work with arrays, so we'll have to switch those to Collections, if we want to go that route..

Copy link
Contributor

Choose a reason for hiding this comment

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

@Richtermeister Why still not using slice? Any real reason, or you just forgot =)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@stloyd Did not forget, but it requires updating all the specs to use ArrayCollection instead of array, and currently it's making the specs fail for me when I do that.. and, I'd love @pjedrzejewski to chime in, because he wrote the original specs with arrays..

Copy link
Contributor

Choose a reason for hiding this comment

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

Ok, let's skip it, I will create PR about this & fix specs then.

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 trying to recall why I have used the arrays instead of the ArrayCollection. I'd prefer to use the Collection interface of course, but I think there was a reason why I did not in the first place...

@Richtermeister
Copy link
Contributor Author

Just ran into the "hidden feature" where all the units passed to InventoryOperator::decrease() must belong to the same variant.. @pjedrzejewski do you think I can clean that up a little to accept a mixed collection? When you look at my previous commits here you see how easy it is to use this method incorrectly..

@Richtermeister
Copy link
Contributor Author

@stloyd How about them apples? :)

@Richtermeister
Copy link
Contributor Author

@pjedrzejewski Just for my better understanding, what is the purpose of creating inventory units before an order is completed?

*/
private function updateVariantUnits(OrderInterface $order, VariantInterface $variant, $quantity)
{
$units = $order->getInventoryUnitsByVariant($variant);
Copy link
Contributor

Choose a reason for hiding this comment

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

As you get always Collection object, you can just call:

$quantityDifference = $quantity - $order->getInventoryUnitsByVariant($variant)->count();

@winzou
Copy link
Contributor

winzou commented Jan 16, 2014

Big 👍 on this PR, thanks @Richtermeister

Regarding our interfaces, I think yes it will be much easier if we can rely on a getId method! If one day a model actually uses another identifier, it will just have to implement the getId method to just return something unique. I see no issue here.

@Richtermeister
Copy link
Contributor Author

@pjedrzejewski If there are no technical issues here, could we get this merged? The bug is otherwise affecting shipping calculations.. By the way, that is the reason for the early creation of inventory units, right? Being able to calculate shipping?

Also, can you please kick off the subtree split update again (ideally after merging this :).. I'm trying to deploy an app that has some dependencies.. Thanks!

pjedrzejewski pushed a commit that referenced this pull request Jan 16, 2014
Bugfix InventoryHandler to remove units of items that were removed from cart
@pjedrzejewski pjedrzejewski merged commit ab5693b into Sylius:master Jan 16, 2014
@pjedrzejewski
Copy link
Member

I'm fine with this solution, but we should somehow resolve the getId() method issue in another PR. And now @stloyd reminded me why we had the issue in the first place. There will be conflicts if 2 interfaces implement the same method. We could bump the requirement... but I'd prefer not to if there is any other solution.

@Richtermeister Thanks for the patch and I just started the sync. My script is crashing for some reason and I did not have time to figure it out. (I run it manually on my local)

@Richtermeister
Copy link
Contributor Author

Thanks man! 😘

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.

None yet

4 participants