Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

616 rindas
31KB

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