您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

595 行
30KB

  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égler 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.removeReductionCredit');
  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. $parameters['form_order_payment'] = $this->createCustomForm(OrderPaymentType::class, 'orderPayment', $parameters, false)->createView();
  374. $parameters['form_order_status'] = $this->createCustomForm(OrderStatusType::class, 'orderStatus', $parameters)->createView();
  375. $parameters['form_delete_order_payment'] = $this->createCustomForm(DeleteOrderPaymentType::class, 'deleteOrderPayment', $parameters)->createView();
  376. break;
  377. }
  378. if($orderShop->isValid()){
  379. $templatePath = "@LcShop/backend/order/show-order.html.twig";
  380. }else{
  381. $templatePath = "@LcShop/backend/order/show-cart.html.twig";
  382. }
  383. }
  384. return parent::renderTemplate($actionName, $templatePath, $parameters);
  385. }
  386. protected function newAction()
  387. {
  388. $this->dispatch(EasyAdminEvents::PRE_NEW);
  389. $entity = $this->executeDynamicMethod('createNew<EntityName>Entity');
  390. $easyadmin = $this->request->attributes->get('easyadmin');
  391. $easyadmin['item'] = $entity;
  392. $this->request->attributes->set('easyadmin', $easyadmin);
  393. $fields = $this->entity['new']['fields'];
  394. $newForm = $this->executeDynamicMethod('create<EntityName>NewForm', [$entity, $fields]);
  395. $newForm->handleRequest($this->request);
  396. //ETAPE 1 Choix de l'utilisateur
  397. $user = $newForm->get('user')->getData();
  398. if ($user == null) {
  399. $user = $this->getUserViaFirstStepForm($entity);
  400. }
  401. if (!$user instanceof UserInterface) return $user;
  402. else {
  403. //TODO Utiliser getCartByUserOrCreateIt ds OrderUtils
  404. $orderShopUser = $this->em->getRepository(OrderShopInterface::class)->findCartCurrent(['user' => $user]);
  405. if ($orderShopUser) {
  406. $this->utils->addFlash('info', 'error.order.otherOrderAlreadyExist');
  407. return $this->redirectToRoute('easyadmin', [
  408. 'action' => 'edit',
  409. 'entity' => $this->entity['name'],
  410. 'id' => $orderShopUser->getId()
  411. ]);
  412. } else {
  413. $orderShop = $this->orderUtils->createOrderShop(array(
  414. 'user' => $user,
  415. 'merchant' => $this->merchantUtils->getMerchantUser()
  416. ));
  417. return $this->redirectToRoute('easyadmin', [
  418. 'action' => 'edit',
  419. 'entity' => $this->entity['name'],
  420. 'id' => $orderShop->getId()
  421. ]);
  422. }
  423. }
  424. }
  425. /**
  426. * The method that is executed when the user performs a 'show' action on an entity.
  427. *
  428. * @return Response
  429. */
  430. public function showAction()
  431. {
  432. $this->dispatch(EasyAdminEvents::PRE_SHOW);
  433. $id = $this->request->query->get('id');
  434. $easyadmin = $this->request->attributes->get('easyadmin');
  435. $entity = $easyadmin['item'];
  436. $fields = $this->entity['show']['fields'];
  437. $deleteForm = $this->createDeleteForm($this->entity['name'], $id);
  438. $this->dispatch(EasyAdminEvents::POST_SHOW, [
  439. 'deleteForm' => $deleteForm,
  440. 'fields' => $fields,
  441. 'entity' => $entity,
  442. ]);
  443. $parameters = [
  444. 'entity' => $entity,
  445. 'fields' => $fields,
  446. 'delete_form' => $deleteForm->createView(),
  447. 'order' => $this->orderUtils->getOrderAsJsonObject($entity)
  448. ];
  449. return $this->executeDynamicMethod('renderOrderShopTemplate', ['show', $this->entity['templates']['show'], $parameters]);
  450. }
  451. /**
  452. * Réécriture de edit action pr rediriger vers le show */
  453. public function editAction()
  454. {
  455. $id = $this->request->query->get('id');
  456. $entity = $this->request->query->get('entity');
  457. return $this->redirectToRoute('easyadmin', [
  458. 'action' => 'show',
  459. 'entity' => $entity,
  460. 'id' => $id
  461. ]);
  462. }
  463. protected function getOrderShopEntity()
  464. {
  465. $easyadmin = $this->request->attributes->get('easyadmin');
  466. $this->orderUtils->initOrderShop($easyadmin['item']);
  467. return $easyadmin['item'];
  468. }
  469. }