protected function updateQuantity(ModelOrder $order, $newStatus, EventDispatcherInterface $dispatcher)
    {
        if ($newStatus !== $order->getStatusId()) {
            if (null !== $newStatusModel = OrderStatusQuery::create()->findPk($newStatus)) {
                $operationEvent = new GetStockUpdateOperationOnOrderStatusChangeEvent($order, $newStatusModel);

                $dispatcher->dispatch(
                    TheliaEvents::ORDER_GET_STOCK_UPDATE_OPERATION_ON_ORDER_STATUS_CHANGE,
                    $operationEvent
                );

                if ($operationEvent->getOperation() !== $operationEvent::DO_NOTHING) {
                    $orderProductList = $order->getOrderProducts();

                    /** @var OrderProduct $orderProduct */
                    foreach ($orderProductList as $orderProduct) {
                        $productSaleElementsId = $orderProduct->getProductSaleElementsId();

                        /** @var ProductSaleElements $productSaleElements */
                        if (null !== $productSaleElements = ProductSaleElementsQuery::create()->findPk($productSaleElementsId)) {
                            $offset = 0;

                            if ($operationEvent->getOperation() == $operationEvent::INCREASE_STOCK) {
                                $offset = $orderProduct->getQuantity();
                            } elseif ($operationEvent->getOperation() == $operationEvent::DECREASE_STOCK) {
                                /* Check if we have enough stock */
                                if ($orderProduct->getQuantity() > $productSaleElements->getQuantity() && true === ConfigQuery::checkAvailableStock()) {
                                    throw new TheliaProcessException($productSaleElements->getRef() . " : Not enough stock 2");
                                }

                                $offset = -$orderProduct->getQuantity();
                            }

                            Tlog::getInstance()->addError("Product stock: " . $productSaleElements->getQuantity() . " -> " . ($productSaleElements->getQuantity() + $offset));

                            $productSaleElements
                                ->setQuantity($productSaleElements->getQuantity() + $offset)
                                ->save();
                        }
                    }
                }
            }
        }
    }
protected function updateQuantity(ModelOrder $order, $newStatus, EventDispatcherInterface $dispatcher)
    {
        if ($newStatus !== $order->getStatusId()) {
            if (null !== $newStatusModel = OrderStatusQuery::create()->findPk($newStatus)) {
                $operationEvent = new GetStockUpdateOperationOnOrderStatusChangeEvent($order, $newStatusModel);

                $dispatcher->dispatch(
                    TheliaEvents::ORDER_GET_STOCK_UPDATE_OPERATION_ON_ORDER_STATUS_CHANGE,
                    $operationEvent
                );

                if ($operationEvent->getOperation() !== $operationEvent::DO_NOTHING) {
                    $orderProductList = $order->getOrderProducts();

                    /** @var OrderProduct $orderProduct */
                    foreach ($orderProductList as $orderProduct) {
                        $productSaleElementsId = $orderProduct->getProductSaleElementsId();

                        /** @var ProductSaleElements $productSaleElements */
                        if (null !== $productSaleElements = ProductSaleElementsQuery::create()->findPk($productSaleElementsId)) {
                            $offset = 0;

                            if ($operationEvent->getOperation() == $operationEvent::INCREASE_STOCK) {
                                $offset = $orderProduct->getQuantity();
                            } elseif ($operationEvent->getOperation() == $operationEvent::DECREASE_STOCK) {
                                /* Check if we have enough stock */
                                if ($orderProduct->getQuantity() > $productSaleElements->getQuantity() && true === ConfigQuery::checkAvailableStock()) {
                                    throw new TheliaProcessException($productSaleElements->getRef() . " : Not enough stock 2");
                                }

                                $offset = -$orderProduct->getQuantity();
                            }

                            Tlog::getInstance()->addError("Product stock: " . $productSaleElements->getQuantity() . " -> " . ($productSaleElements->getQuantity() + $offset));

                            $productSaleElements
                                ->setQuantity($productSaleElements->getQuantity() + $offset)
                                ->save();
                        }
                    }
                }
            }
        }
    }
protected function updateQuantity ( ModelOrder $ order , $ newStatus , EventDispatcherInterface $ dispatcher ) { if ( $ newStatus !== $ order -> getStatusId ( ) ) { if ( null !== $ newStatusModel = OrderStatusQuery :: create ( ) -> findPk ( $ newStatus ) ) { $ operationEvent = new GetStockUpdateOperationOnOrderStatusChangeEvent ( $ order , $ newStatusModel ) ; $ dispatcher -> dispatch ( TheliaEvents :: ORDER_GET_STOCK_UPDATE_OPERATION_ON_ORDER_STATUS_CHANGE , $ operationEvent ) ; if ( $ operationEvent -> getOperation ( ) !== $ operationEvent :: DO_NOTHING ) { $ orderProductList = $ order -> getOrderProducts ( ) ; /** @var OrderProduct $orderProduct */ foreach ( $ orderProductList as $ orderProduct ) { $ productSaleElementsId = $ orderProduct -> getProductSaleElementsId ( ) ; /** @var ProductSaleElements $productSaleElements */ if ( null !== $ productSaleElements = ProductSaleElementsQuery :: create ( ) -> findPk ( $ productSaleElementsId ) ) { $ offset = 0 ; if ( $ operationEvent -> getOperation ( ) == $ operationEvent :: INCREASE_STOCK ) { $ offset = $ orderProduct -> getQuantity ( ) ; } elseif ( $ operationEvent -> getOperation ( ) == $ operationEvent :: DECREASE_STOCK ) { /* Check if we have enough stock */ if ( $ orderProduct -> getQuantity ( ) > $ productSaleElements -> getQuantity ( ) && true === ConfigQuery :: checkAvailableStock ( ) ) { throw new TheliaProcessException ( $ productSaleElements -> getRef ( ) . " : Not enough stock 2" ) ; } $ offset = - $ orderProduct -> getQuantity ( ) ; } Tlog :: getInstance ( ) -> addError ( "Product stock: " . $ productSaleElements -> getQuantity ( ) . " -> " . ( $ productSaleElements -> getQuantity ( ) + $ offset ) ) ; $ productSaleElements -> setQuantity ( $ productSaleElements -> getQuantity ( ) + $ offset ) -> save ( ) ; } } } } } }
Update order products stock after an order status change

@param OrderModel $order
@param int $newStatus the new status ID
@throws \Exception
@throws \Propel\Runtime\Exception\PropelException
Update order products stock after an order status change