You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

OrderController.php 30KB

4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595
  1. <?php
  2. namespace Lc\ShopBundle\Controller\Backend;
  3. use Doctrine\ORM\EntityManagerInterface;
  4. use EasyCorp\Bundle\EasyAdminBundle\Event\EasyAdminEvents;
  5. use FOS\UserBundle\Model\UserManagerInterface;
  6. use Lc\ShopBundle\Context\OrderPaymentInterface;
  7. use Lc\ShopBundle\Context\OrderProductInterface;
  8. use Lc\ShopBundle\Context\OrderReductionCartInterface;
  9. use Lc\ShopBundle\Context\OrderReductionCreditInterface;
  10. use Lc\ShopBundle\Context\OrderShopInterface;
  11. use Lc\ShopBundle\Context\UserInterface;
  12. use Lc\ShopBundle\Form\Backend\Order\AddPoductToOrderType;
  13. use Lc\ShopBundle\Form\Backend\Order\AddRedeliveryOrderProductType;
  14. use Lc\ShopBundle\Form\Backend\Order\DeleteOrderPaymentType;
  15. use Lc\ShopBundle\Form\Backend\Order\DeleteOrderReductionCartType;
  16. use Lc\ShopBundle\Form\Backend\Order\DeleteOrderReductionCreditType;
  17. use Lc\ShopBundle\Form\Backend\Order\OrderDeliveryAddressType;
  18. use Lc\ShopBundle\Form\Backend\Order\OrderInvoiceAddressType;
  19. use Lc\ShopBundle\Form\Backend\Order\OrderPaymentType;
  20. use Lc\ShopBundle\Form\Backend\Order\OrderProductsType;
  21. use Lc\ShopBundle\Form\Backend\Order\AddOrderReductionCartType;
  22. use Lc\ShopBundle\Form\Backend\Order\AddOrderReductionCreditType;
  23. use Lc\ShopBundle\Form\Backend\Order\OrderSendPaymentLink;
  24. use Lc\ShopBundle\Form\Backend\Order\OrderStatusType;
  25. use Lc\ShopBundle\Model\CreditHistory;
  26. use Lc\ShopBundle\Model\OrderStatus;
  27. use Lc\ShopBundle\Services\MailUtils;
  28. use Lc\ShopBundle\Services\Utils;
  29. use Lc\ShopBundle\Services\UtilsManager;
  30. use Mailjet\MailjetSwiftMailer\SwiftMailer\MailjetTransport;
  31. use Symfony\Bridge\Doctrine\Form\Type\EntityType;
  32. use Symfony\Component\Form\Extension\Core\Type\SubmitType;
  33. use Symfony\Component\HttpFoundation\Response;
  34. use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
  35. use Symfony\Component\Security\Core\Security;
  36. use Symfony\Contracts\Translation\TranslatorInterface;
  37. class OrderController extends AdminController
  38. {
  39. protected $creditUtils;
  40. protected $mailUtils;
  41. public function __construct(Security $security, UserManagerInterface $userManager, EntityManagerInterface $em, MailjetTransport $mailjetTransport, UtilsManager $utilsManager, TranslatorInterface $translator)
  42. {
  43. $this->creditUtils = $utilsManager->getCreditUtils();
  44. $this->mailUtils = $utilsManager->getMailUtils();
  45. parent::__construct($security, $userManager, $em, $mailjetTransport, $utilsManager, $translator);
  46. }
  47. protected function createListQueryBuilder($entityClass, $sortDirection, $sortField = null, $dqlFilter = null)
  48. {
  49. $filterIsOrderStatus = false;
  50. $filterNotOrderStatus = false;
  51. if (isset($dqlFilter['isOrderStatus'])) $filterIsOrderStatus = $dqlFilter['isOrderStatus'];
  52. if (isset($dqlFilter['notOrderStatus'])) $filterNotOrderStatus = $dqlFilter['notOrderStatus'];
  53. $dqlFilter = $dqlFilter['filter'];
  54. $queryBuilder = parent::createListQueryBuilder($entityClass, $sortDirection, $sortField, $dqlFilter);
  55. $queryBuilder->leftJoin('entity.orderStatus', 'orderStatus');
  56. if ($filterIsOrderStatus) {
  57. $queryBuilder->andWhere('orderStatus.alias IN (:status)');
  58. $queryBuilder->setParameter('status', $filterIsOrderStatus);
  59. }
  60. if ($filterNotOrderStatus) {
  61. $queryBuilder->andWhere('orderStatus.alias NOT IN (:status)');
  62. $queryBuilder->setParameter('status', $filterNotOrderStatus);
  63. }
  64. return $queryBuilder;
  65. }
  66. public function updateEntity($entity)
  67. {
  68. foreach ($entity->getOrderProducts() as $orderProduct) {
  69. //dump($orderProduct);
  70. $orderProduct->setCreatedBy($this->getUser());
  71. $orderProduct->setUpdatedBy($this->getUser());
  72. $orderProduct->setTaxRate($orderProduct->getProduct()->getProductFamily()->getTaxRate());
  73. $orderProduct->setOrderShop($entity);
  74. $this->em->persist($orderProduct);
  75. $entity->addOrderProduct($orderProduct);
  76. }
  77. $this->setUpdated($entity);
  78. parent::updateEntity($entity);
  79. }
  80. public function persistEntity($entity)
  81. {
  82. foreach ($entity->getOrderProducts() as $orderProduct) {
  83. $orderProduct->setUnit($orderProduct->getProduct()->getUnitInherited());
  84. $orderProduct->setTitle($orderProduct->getProduct()->getTitleInherited());
  85. $orderProduct->setPrice($orderProduct->getProduct()->getPriceInherited());
  86. if ($orderProduct->getProduct()->getProductFamily()->getTaxRate()) {
  87. $orderProduct->setTaxRate($orderProduct->getProduct()->getProductFamily()->getTaxRate());
  88. } else {
  89. $orderProduct->setTaxRate($this->security->getUser()->getMerchant()->getTaxRate());
  90. }
  91. $orderProduct->setOrderShop($entity);
  92. $orderProduct->setCreatedBy($this->getUser());
  93. $orderProduct->setUpdatedBy($this->getUser());
  94. $this->em->persist($orderProduct);
  95. $entity->addOrderProduct($orderProduct);
  96. }
  97. parent::persistEntity($entity);
  98. }
  99. public function getUserViaFirstStepForm($entity)
  100. {
  101. $userClass = $this->em->getClassMetadata(UserInterface::class);
  102. $userChoiceForm = $this->createFormBuilder($entity)
  103. ->add('user', EntityType::class, array(
  104. 'class' => $userClass->name
  105. ))
  106. ->add('nextStep', SubmitType::class)
  107. ->getForm();
  108. $userChoiceForm->handleRequest($this->request);
  109. if ($userChoiceForm->isSubmitted() && $userChoiceForm->isValid()) {
  110. return $userChoiceForm->get('user')->getData();
  111. }
  112. $parameters = [
  113. 'form' => $userChoiceForm->createView(),
  114. 'formView' => 'default',
  115. 'entity' => $entity,
  116. ];
  117. return $this->executeDynamicMethod('render<EntityName>Template', ['new', $this->entity['templates']['new'], $parameters]);
  118. }
  119. public function addProductToOrderAction()
  120. {
  121. $orderShop = $this->getOrderShopEntity();
  122. $orderProductClass = $this->em->getClassMetadata(OrderProductInterface::class);
  123. $formAddProductToOrder = $this->createForm(AddPoductToOrderType::class);
  124. $formAddProductToOrder->handleRequest($this->request);
  125. if ($formAddProductToOrder->isSubmitted() && $formAddProductToOrder->isValid()) {
  126. $orderProduct = new $orderProductClass->name;
  127. if ($this->orderUtils->isProductAvailable($formAddProductToOrder->get('product')->getData(), $formAddProductToOrder->get('quantity')->getData())) {
  128. $orderProduct->setQuantityOrder($formAddProductToOrder->get('quantity')->getData());
  129. $orderProduct->setProduct($formAddProductToOrder->get('product')->getData());
  130. $this->orderUtils->addOrderProduct($orderShop, $orderProduct);
  131. $this->utils->addFlash('success', 'success.order.addProduct');
  132. } else {
  133. $this->utils->addFlash('error', 'error.order.productUnavailable');
  134. }
  135. } else {
  136. $this->utils->addFlash('error', 'error.form.submitted');
  137. }
  138. return $this->createOrderAjaxReponse($orderShop);
  139. }
  140. public function orderInvoiceAddressAction()
  141. {
  142. $orderShop = $this->getOrderShopEntity();
  143. $formOrderInvoiceAddress = $this->createForm(OrderInvoiceAddressType::class, $orderShop);
  144. $formOrderInvoiceAddress->handleRequest($this->request);
  145. if ($formOrderInvoiceAddress->isSubmitted() && $formOrderInvoiceAddress->isValid()) {
  146. //TODO si la commande est valide on hydrate le champ invoiceAddresText et on vide invoiceAddres
  147. $this->em->persist($orderShop);
  148. $this->em->flush();
  149. $this->utils->addFlash('success', 'success.order.changeInvoiceAddress');
  150. } else {
  151. $this->utils->addFlash('error', 'error.form.submitted');
  152. }
  153. return $this->createOrderAjaxReponse($orderShop);
  154. }
  155. public function orderStatusAction()
  156. {
  157. $orderShop = $this->getOrderShopEntity();
  158. $formOrderStatus = $this->createForm(OrderStatusType::class, $orderShop);
  159. $formOrderStatus->handleRequest($this->request);
  160. if ($formOrderStatus->isSubmitted() && $formOrderStatus->isValid()) {
  161. $oldStatus = $orderShop->getOrderStatus();
  162. $orderShop = $this->orderUtils->changeOrderStatus($formOrderStatus->get('orderStatus')->getData(), $orderShop, true);
  163. if ($oldStatus !== $orderShop->getOrderStatus()) {
  164. $this->utils->addFlash('success', 'success.order.changeStatus');
  165. }
  166. } else {
  167. $this->utils->addFlash('success', 'error.form.submitted');
  168. }
  169. return $this->redirectToRoute('easyadmin', [
  170. 'action' => 'show',
  171. 'entity' => $this->request->query->get('entity'),
  172. 'id' => $orderShop->getId()
  173. ]);
  174. }
  175. public function orderPaymentAction()
  176. {
  177. $orderShop = $this->getOrderShopEntity();
  178. //TODO si édition despayment existant on vérifie qu'il est lié à nla commabde courante
  179. $orderPaymentPost = $this->request->request->get('order_payment');
  180. if ($orderPaymentPost['id']) {
  181. $orderPayment = $this->em->getRepository(OrderPaymentInterface::class)->find($orderPaymentPost['id']);
  182. } else {
  183. $orderPaymentClass = $this->em->getClassMetadata(OrderPaymentInterface::class);
  184. $orderPayment = new $orderPaymentClass->name;
  185. }
  186. $formOrderPayment = $this->createForm(OrderPaymentType::class, $orderPayment);
  187. $formOrderPayment->handleRequest($this->request);
  188. if ($formOrderPayment->isSubmitted() && $formOrderPayment->isValid()) {
  189. $orderPayment->setOrderShop($orderShop);
  190. if ($orderPayment->getMeanPayment() === Utils::MEAN_PAYMENT_CREDIT) {
  191. $orderPayment->setEditable(false);
  192. $params['orderPayment'] = $orderPayment;
  193. $creditHistory = $this->creditUtils->initCreditHistory(CreditHistory::TYPE_DEBIT, $orderShop->getUser(), $params);
  194. if ($this->creditUtils->isCreditSufficientToPay($creditHistory->getUserMerchant(), $creditHistory->getAmountInherited())) {
  195. if ($this->creditUtils->saveCreditHistory($creditHistory)) {
  196. $this->em->persist($orderPayment);
  197. $this->em->flush();
  198. $this->utils->addFlash('success', 'success.credit.debited');
  199. $this->utils->addFlash('success', 'success.order.addPayment');
  200. }
  201. } else {
  202. $this->utils->addFlash('error', 'error.credit.notEnoughCredit');
  203. }
  204. } else {
  205. $orderPayment->setEditable(true);
  206. $this->em->persist($orderPayment);
  207. $this->em->flush();
  208. $this->utils->addFlash('success', 'success.order.addPayment');
  209. }
  210. } else {
  211. $this->utils->addFlash('error', $formOrderPayment->getErrors());
  212. }
  213. return $this->createOrderAjaxReponse($orderShop);
  214. }
  215. public function deleteOrderPaymentAction()
  216. {
  217. $orderShop = $this->getOrderShopEntity();
  218. $formDeleteOrderPayment = $this->createForm(DeleteOrderPaymentType::class);
  219. $formDeleteOrderPayment->handleRequest($this->request);
  220. if ($formDeleteOrderPayment->isSubmitted() && $formDeleteOrderPayment->isValid()) {
  221. $orderPayment = $this->em->getRepository(OrderPaymentInterface::class)->find($formDeleteOrderPayment->get('id')->getData());
  222. if ($orderPayment && $orderShop->getOrderPayments()->contains($orderPayment) && $orderPayment->getEditable()) {
  223. $orderShop->removeOrderPayment($orderPayment);
  224. $this->em->remove($orderPayment);
  225. $this->em->flush();
  226. $this->utils->addFlash('success', 'success.order.removeOrderPayement');
  227. }
  228. } else {
  229. $this->utils->addFlash('error', $formDeleteOrderPayment->getErrors());
  230. }
  231. return $this->createOrderAjaxReponse($orderShop);
  232. }
  233. public function addOrderReductionCartAction()
  234. {
  235. $orderShop = $this->getOrderShopEntity();
  236. $formAddOrderReductionCart = $this->createForm(AddOrderReductionCartType::class, $orderShop);
  237. $formAddOrderReductionCart->handleRequest($this->request);
  238. if ($formAddOrderReductionCart->isSubmitted() && $formAddOrderReductionCart->isValid()) {
  239. $reductionCart = $formAddOrderReductionCart->get('reductionCart')->getData();
  240. $orderShop->reductionError = array();
  241. if ($this->orderUtils->isReductionCartAllowAddToOrder($orderShop, $reductionCart)) {
  242. $orderReductionCart = $this->orderUtils->createOrderReductionCart($orderShop, $reductionCart);
  243. $this->em->persist($orderReductionCart);
  244. $this->em->flush();
  245. $this->utils->addFlash('success', 'success.order.addReductionCart');
  246. }
  247. } else {
  248. $this->utils->addFlash('error', $formAddOrderReductionCart->getErrors());
  249. }
  250. return $this->createOrderAjaxReponse($orderShop);
  251. }
  252. public function addOrderReductionCreditAction()
  253. {
  254. $orderShop = $this->getOrderShopEntity();
  255. $formAddOrderReductionCredit = $this->createForm(AddOrderReductionCreditType::class, $orderShop);
  256. $formAddOrderReductionCredit->handleRequest($this->request);
  257. if ($formAddOrderReductionCredit->isSubmitted() && $formAddOrderReductionCredit->isValid()) {
  258. $reductionCredit = $formAddOrderReductionCredit->get('reductionCredit')->getData();
  259. if ($this->orderUtils->isReductionCreditAllowAddToOrder($orderShop, $reductionCredit)) {
  260. $orderReductionCredit = $this->orderUtils->createOrderReductionCredit($orderShop, $reductionCredit);
  261. $this->em->persist($orderReductionCredit);
  262. $this->em->flush();
  263. $this->utils->addFlash('success', 'success.order.addReductionCredit');
  264. }
  265. } else {
  266. $this->utils->addFlash('error', $formAddOrderReductionCredit->getErrors());
  267. }
  268. return $this->createOrderAjaxReponse($orderShop);
  269. }
  270. public function addRedeliveryOrderProductAction()
  271. {
  272. $orderShop = $this->getOrderShopEntity();
  273. $formAddOrderReductionCredit = $this->createForm(AddRedeliveryOrderProductType::class, $orderShop);
  274. //TODO 0 écrire
  275. }
  276. public function deleteOrderReductionCartAction()
  277. {
  278. $orderShop = $this->getOrderShopEntity();
  279. $formDeleteOrderReductionCart = $this->createForm(DeleteOrderReductionCartType::class);
  280. $formDeleteOrderReductionCart->handleRequest($this->request);
  281. if ($formDeleteOrderReductionCart->isSubmitted() && $formDeleteOrderReductionCart->isValid()) {
  282. $orderReductionCart = $this->em->getRepository(OrderReductionCartInterface::class)->find($formDeleteOrderReductionCart->get('id')->getData());
  283. if ($orderReductionCart && $orderShop->getOrderReductionCarts()->contains($orderReductionCart)) {
  284. $orderShop->removeOrderReductionCart($orderReductionCart);
  285. $this->em->remove($orderReductionCart);
  286. $this->em->flush();
  287. $this->utils->addFlash('warning', 'success.order.removeReductionCart');
  288. }
  289. } else {
  290. $this->utils->addFlash('error', $formDeleteOrderReductionCart->getErrors());
  291. }
  292. return $this->createOrderAjaxReponse($orderShop);
  293. }
  294. public function deleteOrderReductionCreditAction()
  295. {
  296. $orderShop = $this->getOrderShopEntity();
  297. $formDeleteOrderReductionCredit = $this->createForm(DeleteOrderReductionCreditType::class);
  298. $formDeleteOrderReductionCredit->handleRequest($this->request);
  299. if ($formDeleteOrderReductionCredit->isSubmitted() && $formDeleteOrderReductionCredit->isValid()) {
  300. $orderReductionCredit = $this->em->getRepository(OrderReductionCreditInterface::class)->find($formDeleteOrderReductionCredit->get('id')->getData());
  301. if ($formDeleteOrderReductionCredit && $orderShop->getOrderReductionCredits()->contains($orderReductionCredit)) {
  302. $orderShop->removeOrderReductionCredit($orderReductionCredit);
  303. $this->em->remove($orderReductionCredit);
  304. $this->em->flush();
  305. $this->utils->addFlash('warning', 'success.order.removeReductionCredit');
  306. }
  307. } else {
  308. $this->utils->addFlash('error', $formDeleteOrderReductionCredit->getErrors());
  309. }
  310. return $this->createOrderAjaxReponse($orderShop);
  311. }
  312. public function orderSendPaymentLinkAction(){
  313. $orderShop = $this->getOrderShopEntity();
  314. $formOrderSendPaymentLink = $this->createForm(OrderSendPaymentLink::class);
  315. $formOrderSendPaymentLink->handleRequest($this->request);
  316. if ($formOrderSendPaymentLink->isSubmitted() && $formOrderSendPaymentLink->isValid()) {
  317. $orderShop->setOrderAllowByAdmin(true);
  318. $this->em->persist($orderShop);
  319. $this->em->flush();
  320. $this->mailUtils->send([
  321. MailUtils::SUBJECT => 'Réglement de votre commande',
  322. MailUtils::TO_EMAIL => $orderShop->getUser()->getEmail(),
  323. MailUtils::TO_NAME => $orderShop->getUser()->getName(),
  324. MailUtils::CONTENT_TEMPLATE => 'mail/order-payment-link',
  325. MailUtils::CONTENT_DATA => [
  326. 'order_shop' => $orderShop,
  327. 'link' => $this->generateUrl('frontend_order_payment',array(),UrlGeneratorInterface::ABSOLUTE_URL)
  328. ],
  329. ]);
  330. $this->utils->addFlash('success', 'success.order.sendPaymentLink');
  331. } else {
  332. $this->utils->addFlash('error', $formOrderSendPaymentLink->getErrors());
  333. }
  334. return $this->createOrderAjaxReponse($orderShop);
  335. }
  336. protected function createOrderAjaxReponse(OrderShopInterface $order)
  337. {
  338. $response['flashMessages'] = $this->utils->getFlashMessages();
  339. $response['data'] = $this->orderUtils->getOrderAsJsonObject($order);
  340. return new Response(json_encode($response));
  341. }
  342. public function renderOrderShopTemplate($actionName, $templatePath, array $parameters = [])
  343. {
  344. if ($actionName == 'show') {
  345. $orderShop = $this->getOrderShopEntity();
  346. switch ($orderShop->getOrderStatus()->getAlias()) {
  347. case OrderStatus::ALIAS_CART :
  348. if (!isset($parameters['form_order_delivery_address'])) {
  349. $parameters['form_order_delivery_address'] = $this->createCustomForm(OrderDeliveryAddressType::class, 'orderDeliveryAddress', $parameters)->createView();
  350. }
  351. $parameters['form_add_order_reduction_credit'] = $this->createCustomForm(AddOrderReductionCreditType::class, 'addOrderReductionCredit', $parameters)->createView();
  352. $parameters['form_add_order_reduction_cart'] = $this->createCustomForm(AddOrderReductionCartType::class, 'addOrderReductionCart', $parameters)->createView();
  353. $parameters['form_delete_order_reduction_cart'] = $this->createCustomForm(DeleteOrderReductionCartType::class, 'deleteOrderReductionCart', $parameters)->createView();
  354. $parameters['form_delete_order_reduction_credit'] = $this->createCustomForm(DeleteOrderReductionCreditType::class, 'deleteOrderReductionCredit', $parameters)->createView();
  355. $parameters['form_add_product_to_order'] = $this->createCustomForm(AddPoductToOrderType::class, 'addProductToOrder', $parameters)->createView();
  356. $parameters['form_order_products'] = $this->createCustomForm(OrderProductsType::class, 'orderProducts', $parameters)->createView();
  357. $parameters['form_order_status'] = $this->createCustomForm(OrderStatusType::class, 'orderStatus', $parameters)->createView();
  358. $parameters['form_order_invoice_address'] = $this->createCustomForm(OrderInvoiceAddressType::class, 'orderInvoiceAddress', $parameters)->createView();
  359. $parameters['form_order_send_payment_link'] = $this->createCustomForm(OrderSendPaymentLink::class, 'orderSendPaymentLink', $parameters)->createView();
  360. break;
  361. case OrderStatus::ALIAS_ERROR_PAYMENT_ONLINE :
  362. case OrderStatus::ALIAS_WAITING_PAYMENT_CREDIT :
  363. case OrderStatus::ALIAS_WAITING_PAYMENT_ONLINE :
  364. $parameters['form_order_send_payment_link'] = $this->createCustomForm(OrderSendPaymentLink::class, 'orderSendPaymentLink', $parameters)->createView();
  365. $parameters['form_order_payment'] = $this->createCustomForm(OrderPaymentType::class, 'orderPayment', $parameters, false)->createView();
  366. $parameters['form_delete_order_payment'] = $this->createCustomForm(DeleteOrderPaymentType::class, 'deleteOrderPayment', $parameters)->createView();
  367. $parameters['form_order_status'] = $this->createCustomForm(OrderStatusType::class, 'orderStatus', $parameters)->createView();
  368. $parameters['form_order_invoice_address'] = $this->createCustomForm(OrderInvoiceAddressType::class, 'orderInvoiceAddress', $parameters)->createView();
  369. break;
  370. case OrderStatus::ALIAS_PAID_BY_CREDIT :
  371. case OrderStatus::ALIAS_PAID_ONLINE :
  372. case OrderStatus::ALIAS_WAITING_DELIVERY :
  373. case OrderStatus::ALIAS_WAITING_BANK_RETURN :
  374. $parameters['form_order_payment'] = $this->createCustomForm(OrderPaymentType::class, 'orderPayment', $parameters, false)->createView();
  375. $parameters['form_order_status'] = $this->createCustomForm(OrderStatusType::class, 'orderStatus', $parameters)->createView();
  376. $parameters['form_delete_order_payment'] = $this->createCustomForm(DeleteOrderPaymentType::class, 'deleteOrderPayment', $parameters)->createView();
  377. break;
  378. }
  379. if($orderShop->isValid()){
  380. $templatePath = "@LcShop/backend/order/show-order.html.twig";
  381. }else{
  382. $templatePath = "@LcShop/backend/order/show-cart.html.twig";
  383. }
  384. }
  385. return parent::renderTemplate($actionName, $templatePath, $parameters);
  386. }
  387. protected function newAction()
  388. {
  389. $this->dispatch(EasyAdminEvents::PRE_NEW);
  390. $entity = $this->executeDynamicMethod('createNew<EntityName>Entity');
  391. $easyadmin = $this->request->attributes->get('easyadmin');
  392. $easyadmin['item'] = $entity;
  393. $this->request->attributes->set('easyadmin', $easyadmin);
  394. $fields = $this->entity['new']['fields'];
  395. $newForm = $this->executeDynamicMethod('create<EntityName>NewForm', [$entity, $fields]);
  396. $newForm->handleRequest($this->request);
  397. //ETAPE 1 Choix de l'utilisateur
  398. $user = $newForm->get('user')->getData();
  399. if ($user == null) {
  400. $user = $this->getUserViaFirstStepForm($entity);
  401. }
  402. if (!$user instanceof UserInterface) return $user;
  403. else {
  404. //TODO Utiliser getCartByUserOrCreateIt ds OrderUtils
  405. $orderShopUser = $this->em->getRepository(OrderShopInterface::class)->findCartCurrent(['user' => $user]);
  406. if ($orderShopUser) {
  407. $this->utils->addFlash('info', 'error.order.otherOrderAlreadyExist');
  408. return $this->redirectToRoute('easyadmin', [
  409. 'action' => 'edit',
  410. 'entity' => $this->entity['name'],
  411. 'id' => $orderShopUser->getId()
  412. ]);
  413. } else {
  414. $orderShop = $this->orderUtils->createOrderShop(array(
  415. 'user' => $user,
  416. 'merchant' => $this->merchantUtils->getMerchantUser()
  417. ));
  418. return $this->redirectToRoute('easyadmin', [
  419. 'action' => 'edit',
  420. 'entity' => $this->entity['name'],
  421. 'id' => $orderShop->getId()
  422. ]);
  423. }
  424. }
  425. }
  426. /**
  427. * The method that is executed when the user performs a 'show' action on an entity.
  428. *
  429. * @return Response
  430. */
  431. public function showAction()
  432. {
  433. $this->dispatch(EasyAdminEvents::PRE_SHOW);
  434. $id = $this->request->query->get('id');
  435. $easyadmin = $this->request->attributes->get('easyadmin');
  436. $entity = $easyadmin['item'];
  437. $fields = $this->entity['show']['fields'];
  438. $deleteForm = $this->createDeleteForm($this->entity['name'], $id);
  439. $this->dispatch(EasyAdminEvents::POST_SHOW, [
  440. 'deleteForm' => $deleteForm,
  441. 'fields' => $fields,
  442. 'entity' => $entity,
  443. ]);
  444. $parameters = [
  445. 'entity' => $entity,
  446. 'fields' => $fields,
  447. 'delete_form' => $deleteForm->createView(),
  448. 'order' => $this->orderUtils->getOrderAsJsonObject($entity)
  449. ];
  450. return $this->executeDynamicMethod('renderOrderShopTemplate', ['show', $this->entity['templates']['show'], $parameters]);
  451. }
  452. /**
  453. * Réécriture de edit action pr rediriger vers le show */
  454. public function editAction()
  455. {
  456. $id = $this->request->query->get('id');
  457. $entity = $this->request->query->get('entity');
  458. return $this->redirectToRoute('easyadmin', [
  459. 'action' => 'show',
  460. 'entity' => $entity,
  461. 'id' => $id
  462. ]);
  463. }
  464. protected function getOrderShopEntity()
  465. {
  466. $easyadmin = $this->request->attributes->get('easyadmin');
  467. $this->orderUtils->initOrderShop($easyadmin['item']);
  468. return $easyadmin['item'];
  469. }
  470. }