Question

I see orders that are closed, canceled or completed that were created in the reservation table but never removed and they won't be removed since it's triggered when the order changes the status.

I check the reservation running this command below.

select * from inventory_reservation where sku='822189036724';

Magento 2 MSI inventory reservation compensation fix

I don't have the lasted version of MSI which includes a command to fix it, so how could I fix it in the DB directly?

No correct solution

OTHER TIPS

This query will return the reservation IDs from the reservations from every order that is either closed, complete or canceled, was created more than 2 weeks ago but has its reservation still different than 0 (which affects the salable quantity on the store, once their reservations were not properly finished).

SELECT to_be_deleted.reservation_id FROM (
    SELECT ir3.*, SUBSTRING_INDEX(SUBSTRING_INDEX(ir3.metadata, '_id":"', -1), '"', 1) AS order_id FROM inventory_reservation ir3
) to_be_deleted WHERE to_be_deleted.order_id IN (
    SELECT entity_id FROM sales_order WHERE entity_id IN (
        SELECT msi_info.order_id FROM (
            SELECT ir1.*, SUBSTRING_INDEX(SUBSTRING_INDEX(ir1.metadata, '_id":"', -1), '"', 1) AS order_id FROM inventory_reservation ir1
        ) msi_info WHERE msi_info.order_id IN (
            SELECT msi_info.order_id FROM (
                SELECT ir2.*, SUBSTRING_INDEX(SUBSTRING_INDEX(ir2.metadata, '_id":"', -1), '"', 1) AS order_id FROM inventory_reservation ir2
            ) msi_info GROUP BY msi_info.order_id HAVING sum(msi_info.quantity)<>0
        )
    ) AND created_at < NOW() - INTERVAL 2 WEEK 
);

You might be able to fix it by running this query.

DELETE FROM inventory_reservation WHERE reservation_id IN (
    SELECT to_be_deleted.reservation_id FROM (
        SELECT ir3.*, SUBSTRING_INDEX(SUBSTRING_INDEX(ir3.metadata, '_id":"', -1), '"', 1) AS order_id FROM inventory_reservation ir3
    ) to_be_deleted WHERE to_be_deleted.order_id IN (
        SELECT entity_id FROM sales_order WHERE entity_id IN (
            SELECT msi_info.order_id FROM (
                SELECT ir1.*, SUBSTRING_INDEX(SUBSTRING_INDEX(ir1.metadata, '_id":"', -1), '"', 1) AS order_id FROM inventory_reservation ir1
            ) msi_info WHERE msi_info.order_id IN (
                SELECT msi_info.order_id FROM (
                    SELECT ir2.*, SUBSTRING_INDEX(SUBSTRING_INDEX(ir2.metadata, '_id":"', -1), '"', 1) AS order_id FROM inventory_reservation ir2
                ) msi_info GROUP BY msi_info.order_id HAVING sum(msi_info.quantity)<>0
            )
        ) AND created_at < NOW() - INTERVAL 1 WEEK AND state IN ('closed', 'complete', 'canceled')
    )
);
Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top