magento / inventory

Magento Inventory Project (a.k.a MSI)
Open Software License 3.0
334 stars 246 forks source link

Resaving shipments in same process results in stock deduction #2276

Open Berdir opened 5 years ago

Berdir commented 5 years ago

It seems that saving a shipment with changes can result in repeated stock deduction.

I assume that this should prevent that:

        if ($shipment->getOrigData('entity_id')) {
            return;
        }

I think this might only happen if a shipment is saved again in the same process, because \Magento\Sales\Model\Order\ShipmentRepository::save() puts the saved entity into the repository, but that means that \Magento\Framework\EntityManager\Observer\AfterEntityLoad::execute() doesn't run on it when loading it again, then it has no original data.

Preconditions (*)

  1. Magento 2.3.1

Steps to reproduce (*)

Create a shipment, save it, then load it again from the repository, make a change and save it again.

Expected result (*)

Stock did not change.

Actual result (*)

Stock is deducted again.

Maybe there is a more reliable way to check that an entity wasn't new?

Berdir commented 5 years ago

I did run into this in an integration test for a module I'm creating.

I added a workaround there: https://github.com/swisspost-yellowcube/magento2-yellowcube/commit/78ad1b2795deaaa44ddc27fcf3621861c4c1aafd#diff-fbc227e53b22951fbb542a93bfd927aeR278, without that, the test fails.