/**
  * Updates or deletes dispatches when items of an order are cancelled.
  * Removes all cancelled items and updates the dispatch.
  * If all items have been cancelled, the dispatch is deleted.
  * 
  * @param  Event\TransactionalEvent $event Event
  */
 public function updateDispatches(Event\TransactionalEvent $event)
 {
     $order = $event->getOrder();
     $trans = $event->getTransaction();
     $dispatchEdit = $this->get('order.dispatch.edit');
     $dispatchDeleter = $this->get('order.dispatch.delete');
     $dispatchEdit->setTransaction($trans);
     $dispatchDeleter->setTransaction($trans);
     foreach ($order->dispatches as $dispatch) {
         if ($dispatch->authorship->isDeleted()) {
             continue;
         }
         $updateNecessary = false;
         foreach ($dispatch->items as $item) {
             if (Statuses::CANCELLED === $item->status->code) {
                 $dispatch->items->remove($item);
                 $updateNecessary = true;
             }
         }
         if ($updateNecessary) {
             $dispatchEdit->update($dispatch);
             if (0 === $dispatch->items->count()) {
                 $dispatchDeleter->delete($dispatch);
             }
         }
     }
 }
 /**
  * Update the items' statuses to match their parent order's status, where
  * the order status is 'cancelled'.
  *
  * @param  EventTransactionalEvent $event
  */
 public function updateStatus(Event\TransactionalEvent $event)
 {
     $order = $event->getOrder();
     if (Statuses::CANCELLED === $order->status->code) {
         $this->_itemEdit->setTransaction($event->getTransaction());
         $this->_itemEdit->updateStatus($order->items->all(), Statuses::CANCELLED);
     }
 }
 public function adjustStock(Event\TransactionalEvent $event)
 {
     $order = $event->getOrder();
     $trans = $event->getTransaction();
     $stockManager = $this->get('stock.manager');
     $stockManager->setTransaction($trans);
     $stockManager->createWithRawNote(true);
     $stockManager->setReason($this->get('stock.movement.reasons')->get('new_order'));
     $trans->add("SET @STOCK_NOTE = CONCAT('Order #', ?i);", $order->id);
     $stockManager->setNote('@STOCK_NOTE');
     $stockManager->setAutomated(true);
     foreach ($order->getItems() as $item) {
         $stockManager->decrement($item->getUnit(), $item->stockLocation);
     }
 }
 /**
  * Dispatch the event to set the order's overall status. This is fired when
  * an item status changes.
  *
  * If the event results in a status code that is different to the order's
  * existing status code, it is updated.
  *
  * @param  Event\TransactionalEvent $event The event object
  */
 public function dispatchSetOrderStatusEvent(Event\TransactionalEvent $event)
 {
     $statusEvent = $event->getDispatcher()->dispatch(OrderEvents::SET_STATUS, new Event\SetOrderStatusEvent($event->getOrder()));
     $orderStatus = $statusEvent->getStatus();
     $order = $statusEvent->getOrder();
     // Skip if no status was set
     if (is_null($orderStatus)) {
         return;
     }
     // Skip if the status hasn't changed
     if ($orderStatus === $order->status->code) {
         return;
     }
     $edit = $this->get('order.edit');
     $edit->setTransaction($event->getTransaction());
     $edit->updateStatus($order, $orderStatus);
 }
 /**
  * Creates a transaction with records for the order, all items and payments
  *
  * @param  Event\Event $event event carrying information about order
  */
 public function createOrderTransaction(Event\TransactionalEvent $event)
 {
     $order = $event->getOrder();
     if ($order->status->code >= Statuses::AWAITING_DISPATCH || $order->status->code === Statuses::PAYMENT_PENDING) {
         $transaction = new Transaction();
         $transaction->records->add($order);
         foreach ($order->items as $item) {
             $transaction->records->add($item);
         }
         foreach ($order->payments as $payment) {
             $transaction->records->add($payment);
         }
         // $transaction->type =
         // 	($order->status->code === Statuses::PAYMENT_PENDING ? Types::CONTRACT_INITIATION : Types::ORDER);
         $transaction->type = Types::ORDER;
         $this->get('order.transaction.create')->setDbTransaction($event->getTransaction())->create($transaction);
     }
 }