public function getStockUpdateOnOrderStatusChange(GetStockUpdateOperationOnOrderStatusChangeEvent $event, $eventName, EventDispatcherInterface $dispatcher)
    {
        // The order
        $order = $event->getOrder();

        // The new order status
        $newStatus = $event->getNewOrderStatus();

        if ($newStatus->getId() !== $order->getStatusId()) {
            // We have to change the stock in the following cases :
            // 1) The order is currently paid, and will become unpaid (get products back in stock unconditionnaly)
            // 2) The order is currently unpaid, and will become paid (remove products from stock, except if was done at order creation $manageStockOnCreation == false)
            // 3) The order is currently NOT PAID, and will become canceled or the like (get products back in stock if it was done at order creation $manageStockOnCreation == true)

            // We consider the ManageStockOnCreation flag only if the order status as not yet changed.
            // Count distinct order statuses (e.g. NOT_PAID to something else) in the order version table.
            if (OrderVersionQuery::create()->groupByStatusId()->filterById($order->getId())->count() > 1) {
                // A status change occured. Ignore $manageStockOnCreation
                $manageStockOnCreation = false;
            } else {
                // A status has not yet occured. Consider the ManageStockOnCreation flag
                $manageStockOnCreation = $order->isStockManagedOnOrderCreation($dispatcher);
            }

            if (($order->isPaid(false) && $newStatus->isNotPaid(false)) // Case 1
                ||
                ($order->isNotPaid(true) && $newStatus->isNotPaid(false) && $manageStockOnCreation === true) // Case 3
            ) {
                $event->setOperation($event::INCREASE_STOCK);
            }

            if ($order->isNotPaid(false) // Case 2
                &&
                $newStatus->isPaid(false)
                &&
                $manageStockOnCreation === false) {
                $event->setOperation($event::DECREASE_STOCK);
            }

            Tlog::getInstance()->addInfo(
                "Checking stock operation for status change of order : " . $order->getRef()
                . ", version: " . $order->getVersion()
                . ", manageStockOnCreation: " . ($manageStockOnCreation ? 0 : 1)
                . ", paid:" . ($order->isPaid(false) ? 1 : 0)
                . ", is not paid:" . ($order->isNotPaid(false) ? 1 : 0)
                . ", new status paid:" . ($newStatus->isPaid(false) ? 1 : 0)
                . ", new status is not paid:" . ($newStatus->isNotPaid(false) ? 1 : 0)
                . " = operation: " . $event->getOperation()
            );
        }
    }