OrderController.php 29KB

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