AmpersandHQ / magento2-disable-stock-reservation

This module disables the inventory reservation logic introduced as part of MSI in Magento 2.3.3
GNU Lesser General Public License v3.0
211 stars 60 forks source link

Update SourceRepository.php to return stock to relevant stock source #113

Closed peekarboo closed 1 year ago

peekarboo commented 1 year ago

In case of multiple msi sources, stock is not returned to the relevant stock source. This PR

  1. Updates SourcesConverter to return all sources associated with order (#102)
  2. Returns stock to relevant stock source when there are different sources

Assign multiple sources to a stock

Screenshot 2023-05-15 at 08 08 19

Stock A (BE) = 2 Stock B (NL) = 2

mysql> select * from inventory_source_item where sku ='64411d69-0361-49f7-bb85-4a30bb75bbeb';
+----------------+-------------+--------------------------------------+----------+--------+
| source_item_id | source_code | sku                                  | quantity | status |
+----------------+-------------+--------------------------------------+----------+--------+
|       10241017 | BE          | 64411d69-0361-49f7-bb85-4a30bb75bbeb |   2.0000 |      1 |
|       10201427 | default     | 64411d69-0361-49f7-bb85-4a30bb75bbeb |   2.0000 |      1 |
|       10232247 | NL          | 64411d69-0361-49f7-bb85-4a30bb75bbeb |   2.0000 |      1 |
+----------------+-------------+--------------------------------------+----------+--------+

Create a pending order with qty 5

Stock A (BE) = 0 Stock B (NL) = 0

mysql> mysql> select * from inventory_source_item where sku ='64411d69-0361-49f7-bb85-4a30bb75bbeb';
+----------------+-------------+--------------------------------------+----------+--------+
| source_item_id | source_code | sku                                  | quantity | status |
+----------------+-------------+--------------------------------------+----------+--------+
|       10241017 | BE          | 64411d69-0361-49f7-bb85-4a30bb75bbeb |   0.0000 |      0 |
|       10201427 | default     | 64411d69-0361-49f7-bb85-4a30bb75bbeb |   2.0000 |      1 |
|       10232247 | NL          | 64411d69-0361-49f7-bb85-4a30bb75bbeb |   0.0000 |      0 |
+----------------+-------------+--------------------------------------+----------+--------+

Cancel the order

Stock A (BE) = 2 Stock B (NL) = 2

mysql> mysql> select * from inventory_source_item where sku ='64411d69-0361-49f7-bb85-4a30bb75bbeb';
+----------------+-------------+--------------------------------------+----------+--------+
| source_item_id | source_code | sku                                  | quantity | status |
+----------------+-------------+--------------------------------------+----------+--------+
|       10241017 | BE          | 64411d69-0361-49f7-bb85-4a30bb75bbeb |   2.0000 |      1 |
|       10201427 | default     | 64411d69-0361-49f7-bb85-4a30bb75bbeb |   2.0000 |      1 |
|       10232247 | NL          | 64411d69-0361-49f7-bb85-4a30bb75bbeb |   2.0000 |      1 |
+----------------+-------------+--------------------------------------+----------+--------+

Checklist

convenient commented 1 year ago

@peekarboo this includes the changes in https://github.com/AmpersandHQ/magento2-disable-stock-reservation/pull/102 ye?

peekarboo commented 1 year ago

@convenient Yes it does

barryvdh commented 1 year ago

At first sight this seems to fix my issue indeed. The stock is returned to the original source.

barryvdh commented 1 year ago

Wait I think this doesn't go well with multiple products. It seems to add the stock to all sources

Product 1 Stock A: 3 Stock B: 2

Product 2: Stock A: 5 Stock B: 5

Order 5x1 and 10x 2

Product 1 Stock A: 0 Stock B: 1

Product 2 Stock A: 0 Stock B: 0

Cancel order

Product 1 Stock A: 8 Stock B: 7 -> 15 instead of 5, so 10 from product 2 are added also.

Product 2 Stock A: 8 Stock B: 6 -> 15 instead of 10, so 5 from Product 1 are added here also

barryvdh commented 1 year ago

This looks good, but don't know why some tests are failing. Is there anything I can do to help this along?

convenient commented 1 year ago

@barryvdh tests going green now because of this https://github.com/AmpersandHQ/magento2-disable-stock-reservation/blob/4f35d8d2e4c7aa82c6083c989fd790ff07cb5e2f/dev/MagentoTests/Integration/MultipleSourceInventoryTest.php#L206-L233

Seems we were having odd / bad values cached on an object property during the fixture booting that needed cleared before running the tests.

@peekarboo would be good to get a few additional test cases in please

convenient commented 1 year ago

Hey @barryvdh

I believe this is all sorted now, would you agree? Before I merge and tag just wanted to check.

Thanks, Luke

barryvdh commented 1 year ago

I think so. I tested this patch on my local shop and it seems to work correctly for the cases I encountered before. Thanks!