Example #1
0
 /**
  * 取消訂單
  * 
  * @Route("/invoice/{id}/cancel", requirements={"id"="\d+"}, name="front_invoice_cancel")
  * @ParamConverter("invoice", class="WoojinOrderBundle:Invoice")
  * @Method("PUT")
  */
 public function cancelAction(Invoice $invoice, Request $request)
 {
     if (!$this->get('security.csrf.token_manager')->isCsrfTokenValid('invoice', $request->request->get('avenue_token'))) {
         throw new AccessDeniedHttpException('Invalid CSRF token.');
     }
     if (Avenue::IV_NOT_GET !== $invoice->getStatus()->getId()) {
         return $this->redirect($this->get('router')->generate('front_profile_orders'));
     }
     $em = $this->getDoctrine()->getManager();
     $this->checkOwnInvoice($invoice, $request, $em);
     $em->getConnection()->beginTransaction();
     try {
         $invoice->setStatus(Avenue::IV_BACK_DONE);
         $em->persist($invoice);
         $orders = $invoice->getOrders();
         $orderCancel = $em->find('WoojinOrderBundle:OrdersStatus', Avenue::OS_CANCEL);
         $productOnsale = $em->find('WoojinGoodsBundle:GoodsStatus', Avenue::GS_ONSALE);
         $iterator = $orders->getIterator();
         while ($iterator->valid()) {
             $order = $iterator->current();
             $order->setStatus($orderCancel);
             $em->persist($order);
             $product = $order->getGoodsPassport();
             $product->setStatus($productOnsale);
             $em->persist($product);
             $iterator->next();
         }
         $em->flush();
         $em->getConnection()->commit();
     } catch (\Exception $e) {
         // Rollback the failed transaction attempt
         $em->getConnection()->rollback();
         throw $e;
     }
     $logger = $this->get('logger.custom');
     $logger->write($invoice->getCustom(), array('entity' => 'invoice', 'id' => $invoice->getId(), 'method' => 'cancel', 'url' => 'front_invoice_cancel'));
     $this->get('session')->getFlashBag()->add('success', '訂單編號: ' . $invoice->getSn() . '已經取消!');
     // 發送訂單取消通知
     $notifier = $this->get('avenue.notifier');
     $notifier->cancelOrder($invoice);
     return $this->redirect($this->get('router')->generate('front_profile_orders'));
 }