Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

507 lines
25KB

  1. <?php
  2. namespace Lc\ShopBundle\Controller\Backend;
  3. use EasyCorp\Bundle\EasyAdminBundle\Event\EasyAdminEvents;
  4. use Lc\ShopBundle\Context\OrderPaymentInterface;
  5. use Lc\ShopBundle\Context\OrderProductInterface;
  6. use Lc\ShopBundle\Context\OrderReductionCartInterface;
  7. use Lc\ShopBundle\Context\OrderReductionCreditInterface;
  8. use Lc\ShopBundle\Context\OrderShopInterface;
  9. use Lc\ShopBundle\Context\UserInterface;
  10. use Lc\ShopBundle\Form\Backend\Order\AddPoductToOrderType;
  11. use Lc\ShopBundle\Form\Backend\Order\DeleteOrderReductionCartType;
  12. use Lc\ShopBundle\Form\Backend\Order\DeleteOrderReductionCreditType;
  13. use Lc\ShopBundle\Form\Backend\Order\OrderDeliveryAddressType;
  14. use Lc\ShopBundle\Form\Backend\Order\OrderInvoiceAddressType;
  15. use Lc\ShopBundle\Form\Backend\Order\OrderPaymentType;
  16. use Lc\ShopBundle\Form\Backend\Order\OrderProductsType;
  17. use Lc\ShopBundle\Form\Backend\Order\AddOrderReductionCartType;
  18. use Lc\ShopBundle\Form\Backend\Order\AddOrderReductionCreditType;
  19. use Lc\ShopBundle\Form\Backend\Order\OrderStatusType;
  20. use Symfony\Bridge\Doctrine\Form\Type\EntityType;
  21. use Symfony\Component\Form\Extension\Core\Type\SubmitType;
  22. use Symfony\Component\HttpFoundation\Response;
  23. class OrderController extends AdminController
  24. {
  25. protected function createListQueryBuilder($entityClass, $sortDirection, $sortField = null, $dqlFilter = null)
  26. {
  27. $filterOrderStatus = false;
  28. if ($dqlFilter['orderStatus']) $filterOrderStatus = $dqlFilter['orderStatus'];
  29. $dqlFilter = $dqlFilter['filter'];
  30. $queryBuilder = parent::createListQueryBuilder($entityClass, $sortDirection, $sortField, $dqlFilter);
  31. $queryBuilder->leftJoin('entity.orderStatus', 'orderStatus');
  32. if ($filterOrderStatus) {
  33. $queryBuilder->andWhere('orderStatus.alias IN (:status)');
  34. $queryBuilder->setParameter('status', $filterOrderStatus);
  35. }
  36. return $queryBuilder;
  37. }
  38. public function updateEntity($entity)
  39. {
  40. foreach ($entity->getOrderProducts() as $orderProduct) {
  41. //dump($orderProduct);
  42. $orderProduct->setCreatedBy($this->getUser());
  43. $orderProduct->setUpdatedBy($this->getUser());
  44. $orderProduct->setTaxRate($orderProduct->getProduct()->getProductFamily()->getTaxRate());
  45. $orderProduct->setOrderShop($entity);
  46. $this->em->persist($orderProduct);
  47. $entity->addOrderProduct($orderProduct);
  48. }
  49. $this->setUpdated($entity);
  50. parent::updateEntity($entity);
  51. }
  52. public function persistEntity($entity)
  53. {
  54. foreach ($entity->getOrderProducts() as $orderProduct) {
  55. $orderProduct->setUnit($orderProduct->getProduct()->getUnitInherited());
  56. $orderProduct->setTitle($orderProduct->getProduct()->getTitleInherited());
  57. $orderProduct->setPrice($orderProduct->getProduct()->getPriceInherited());
  58. if ($orderProduct->getProduct()->getProductFamily()->getTaxRate()) {
  59. $orderProduct->setTaxRate($orderProduct->getProduct()->getProductFamily()->getTaxRate());
  60. } else {
  61. $orderProduct->setTaxRate($this->security->getUser()->getMerchant()->getTaxRate());
  62. }
  63. $orderProduct->setOrderShop($entity);
  64. $orderProduct->setCreatedBy($this->getUser());
  65. $orderProduct->setUpdatedBy($this->getUser());
  66. $this->em->persist($orderProduct);
  67. $entity->addOrderProduct($orderProduct);
  68. }
  69. parent::persistEntity($entity);
  70. }
  71. public function getUserViaFirstStepForm($entity)
  72. {
  73. $userClass = $this->em->getClassMetadata(UserInterface::class);
  74. $userChoiceForm = $this->createFormBuilder($entity)
  75. ->add('user', EntityType::class, array(
  76. 'class' => $userClass->name
  77. ))
  78. ->add('nextStep', SubmitType::class)
  79. ->getForm();
  80. $userChoiceForm->handleRequest($this->request);
  81. if ($userChoiceForm->isSubmitted() && $userChoiceForm->isValid()) {
  82. return $userChoiceForm->get('user')->getData();
  83. }
  84. $parameters = [
  85. 'form' => $userChoiceForm->createView(),
  86. 'formView' => 'default',
  87. 'entity' => $entity,
  88. ];
  89. return $this->executeDynamicMethod('render<EntityName>Template', ['new', $this->entity['templates']['new'], $parameters]);
  90. }
  91. public function addProductToOrderAction()
  92. {
  93. $orderShop = $this->getOrderShopEntity();
  94. $orderProductClass = $this->em->getClassMetadata(OrderProductInterface::class);
  95. $formAddProductToOrder = $this->createForm(AddPoductToOrderType::class);
  96. $formAddProductToOrder->handleRequest($this->request);
  97. if ($formAddProductToOrder->get('product')->getData() == null) {
  98. $response['status'] = 'error';
  99. $response['message'] = 'Vous devez choisir un produit dans la liste';
  100. } else if ($formAddProductToOrder->get('quantity')->getData() == null) {
  101. $response['status'] = 'error';
  102. $response['message'] = 'Vous devez entrer une quantité';
  103. } else if ($formAddProductToOrder->isSubmitted() && $formAddProductToOrder->isValid()) {
  104. $orderProduct = new $orderProductClass->name;
  105. if ($this->orderUtils->isProductAvailable($formAddProductToOrder->get('product')->getData(), $formAddProductToOrder->get('quantity')->getData())) {
  106. $orderProduct->setQuantityOrder($formAddProductToOrder->get('quantity')->getData());
  107. $orderProduct->setProduct($formAddProductToOrder->get('product')->getData());
  108. $this->orderUtils->addOrderProduct($orderShop, $orderProduct);
  109. $response['status'] = 'success';
  110. $response['message'] = 'Le produit a bien été ajouté à la commande';
  111. } else {
  112. $response['status'] = 'error';
  113. $response['message'] = 'Le produit n\'est pas disponible dans cette quantité';
  114. }
  115. } else {
  116. $response['status'] = 'error';
  117. $response['message'] = 'Une erreur est survenue';
  118. }
  119. $response['data'] = $this->orderUtils->getOrderAsJsonObject($orderShop);;
  120. return new Response(json_encode($response));
  121. }
  122. public function orderProductsAction()
  123. {
  124. $orderShop = $this->getOrderShopEntity();
  125. $formOrderProducts = $this->createForm(OrderProductsType::class, $orderShop);
  126. $formOrderProducts->handleRequest($this->request);
  127. if ($formOrderProducts->isSubmitted() && $formOrderProducts->isValid()) {
  128. $error = false;
  129. // dump($formOrderProducts->get('orderProducts')->getData());
  130. foreach ($orderShop->getOrderProducts() as $orderProduct) {
  131. if ($this->orderUtils->isProductAvailable($orderProduct->getProduct(), $orderProduct->getQuantityOrder())) {
  132. if ($orderProduct->getQuantityOrder() <= 0) {
  133. $this->em->remove($orderProduct);
  134. } else {
  135. $this->em->persist($orderProduct);
  136. }
  137. } else {
  138. $error = true;
  139. $response['status'] = 'error';
  140. $response['message'] = 'Le prduit n\'est pas disponible dans cette quantité';
  141. }
  142. }
  143. if (!$error) {
  144. $this->em->flush();
  145. $response['status'] = 'success';
  146. $response['message'] = 'La commande a bien été modifié';
  147. }
  148. } else {
  149. $response['status'] = 'error';
  150. $response['message'] = 'Une erreur est survenue';
  151. }
  152. $response['data'] = $this->orderUtils->getOrderAsJsonObject($orderShop);;
  153. return new Response(json_encode($response));
  154. }
  155. public function orderInvoiceAddressAction()
  156. {
  157. $orderShop = $this->getOrderShopEntity();
  158. $formOrderInvoiceAddress = $this->createForm(OrderInvoiceAddressType::class, $orderShop);
  159. $formOrderInvoiceAddress->handleRequest($this->request);
  160. if ($formOrderInvoiceAddress->isSubmitted() && $formOrderInvoiceAddress->isValid()) {
  161. //TODO si la commande est valide on hydrate le champ invoiceAddresText et on vide invoiceAddres
  162. $this->em->persist($orderShop);
  163. $this->em->flush();
  164. $this->utils->addFlash('success', 'success.order.changeInvoiceAddress');
  165. } else {
  166. $this->utils->addFlash('success', 'error.form.submitted');
  167. }
  168. return $this->createOrderAjaxReponse($orderShop);
  169. }
  170. public function orderStatusAction()
  171. {
  172. $orderShop = $this->getOrderShopEntity();
  173. $formOrderStatus = $this->createForm(OrderStatusType::class, $orderShop);
  174. $formOrderStatus->handleRequest($this->request);
  175. if ($formOrderStatus->isSubmitted() && $formOrderStatus->isValid()) {
  176. if ($orderShop = $this->orderUtils->changeOrderStatus($formOrderStatus->get('orderStatus')->getData(), $orderShop)) {
  177. $this->utils->addFlash('success', 'success.order.changeStatus');
  178. }
  179. } else {
  180. $this->utils->addFlash('success', 'error.form.submitted');
  181. }
  182. return $this->redirectToRoute('easyadmin', [
  183. 'action' => 'show',
  184. 'entity' => $this->request->query->get('entity'),
  185. 'id' => $orderShop->getId()
  186. ]);
  187. }
  188. public function orderPaymentAction()
  189. {
  190. $orderShop = $this->getOrderShopEntity();
  191. $orderPaymentClass = $this->em->getClassMetadata(OrderPaymentInterface::class);
  192. $orderPayment = new $orderPaymentClass->name;
  193. $formOrderPayment = $this->createForm(OrderPaymentType::class, $orderPayment);
  194. $formOrderPayment->handleRequest($this->request);
  195. if ($formOrderPayment->isSubmitted() && $formOrderPayment->isValid()) {
  196. $orderPayment->setOrderShop($orderShop);
  197. $this->em->persist($orderPayment);
  198. $this->em->flush();
  199. $this->utils->addFlash('success', 'success.order.addPayment');
  200. } else {
  201. $this->utils->addFlash('success', 'error.form.submitted');
  202. }
  203. return $this->createOrderAjaxReponse($orderShop);
  204. }
  205. public function addOrderReductionCartAction()
  206. {
  207. $orderShop = $this->getOrderShopEntity();
  208. $formAddOrderReductionCart = $this->createForm(AddOrderReductionCartType::class, $orderShop);
  209. $formAddOrderReductionCart->handleRequest($this->request);
  210. if ($formAddOrderReductionCart->isSubmitted() && $formAddOrderReductionCart->isValid()) {
  211. $reductionCart = $formAddOrderReductionCart->get('reductionCart')->getData();
  212. $orderShop->reductionError = array();
  213. if ($this->orderUtils->isReductionCartAllowAddToOrder($orderShop, $reductionCart)) {
  214. $orderReductionCart = $this->orderUtils->createOrderReductionCart($orderShop, $reductionCart);
  215. $this->em->persist($orderReductionCart);
  216. $this->em->flush();
  217. $this->utils->addFlash('success', 'success.order.addReductionCart');
  218. }
  219. } else {
  220. $this->utils->addFlash('error', $formAddOrderReductionCart->getErrors());
  221. }
  222. return $this->createOrderAjaxReponse($orderShop);
  223. }
  224. public function addOrderReductionCreditAction()
  225. {
  226. $orderShop = $this->getOrderShopEntity();
  227. $formAddOrderReductionCredit = $this->createForm(AddOrderReductionCreditType::class, $orderShop);
  228. $formAddOrderReductionCredit->handleRequest($this->request);
  229. if ($formAddOrderReductionCredit->isSubmitted() && $formAddOrderReductionCredit->isValid()) {
  230. $reductionCredit = $formAddOrderReductionCredit->get('reductionCredit')->getData();
  231. if ($this->orderUtils->isReductionCreditAllowAddToOrder($orderShop, $reductionCredit)) {
  232. $orderReductionCredit = $this->orderUtils->createOrderReductionCredit($orderShop, $reductionCredit);
  233. $this->em->persist($orderReductionCredit);
  234. $this->em->flush();
  235. $this->utils->addFlash('success', 'success.order.addReductionCredit');
  236. }
  237. } else {
  238. $this->utils->addFlash('error', $formAddOrderReductionCredit->getErrors());
  239. }
  240. return $this->createOrderAjaxReponse($orderShop);
  241. }
  242. public function deleteOrderReductionCartAction()
  243. {
  244. $orderShop = $this->getOrderShopEntity();
  245. $formDeleteOrderReductionCart = $this->createForm(DeleteOrderReductionCartType::class);
  246. $formDeleteOrderReductionCart->handleRequest($this->request);
  247. if ($formDeleteOrderReductionCart->isSubmitted() && $formDeleteOrderReductionCart->isValid()) {
  248. $orderReductionCart = $this->em->getRepository(OrderReductionCartInterface::class)->find($formDeleteOrderReductionCart->get('id')->getData());
  249. if($orderReductionCart && $orderShop->getOrderReductionCarts()->contains($orderReductionCart)){
  250. $orderShop->removeOrderReductionCart($orderReductionCart);
  251. $this->em->remove($orderReductionCart);
  252. $this->em->flush();
  253. $this->utils->addFlash('warning', 'success.order.removeReductionCart');
  254. }
  255. } else {
  256. $this->utils->addFlash('error', $formDeleteOrderReductionCart->getErrors());
  257. }
  258. return $this->createOrderAjaxReponse($orderShop);
  259. }
  260. public function deleteOrderReductionCreditAction()
  261. {
  262. $orderShop = $this->getOrderShopEntity();
  263. $formDeleteOrderReductionCredit = $this->createForm(DeleteOrderReductionCreditType::class);
  264. $formDeleteOrderReductionCredit->handleRequest($this->request);
  265. if ($formDeleteOrderReductionCredit->isSubmitted() && $formDeleteOrderReductionCredit->isValid()) {
  266. $orderReductionCredit = $this->em->getRepository(OrderReductionCreditInterface::class)->find($formDeleteOrderReductionCredit->get('id')->getData());
  267. if($formDeleteOrderReductionCredit && $orderShop->getOrderReductionCredits()->contains($orderReductionCredit)){
  268. $orderShop->removeOrderReductionCredit($orderReductionCredit);
  269. $this->em->remove($orderReductionCredit);
  270. $this->em->flush();
  271. $this->utils->addFlash('warning', 'success.order.removeReductionCredit');
  272. }
  273. } else {
  274. $this->utils->addFlash('error', $formDeleteOrderReductionCredit->getErrors());
  275. }
  276. return $this->createOrderAjaxReponse($orderShop);
  277. }
  278. protected function createOrderAjaxReponse(OrderShopInterface $order)
  279. {
  280. $response['flashMessages'] = $this->utils->getFlashMessages();
  281. $response['data'] = $this->orderUtils->getOrderAsJsonObject($order);
  282. return new Response(json_encode($response));
  283. }
  284. public function renderOrderCartTemplate($actionName, $templatePath, array $parameters = [])
  285. {
  286. if ($actionName == 'show') {
  287. if (!isset($parameters['form_order_delivery_address'])) {
  288. $parameters['form_order_delivery_address'] = $this->createOrderForm(OrderDeliveryAddressType::class, 'orderDeliveryAddress', $parameters)->createView();
  289. }
  290. $parameters['form_add_order_reduction_credit'] = $this->createOrderForm(AddOrderReductionCreditType::class, 'addOrderReductionCredit', $parameters)->createView();
  291. $parameters['form_add_order_reduction_cart'] = $this->createOrderForm(AddOrderReductionCartType::class, 'addOrderReductionCart', $parameters)->createView();
  292. $parameters['form_delete_order_reduction_cart'] = $this->createOrderForm(DeleteOrderReductionCartType::class, 'deleteOrderReductionCart', $parameters)->createView();
  293. $parameters['form_delete_order_reduction_credit'] = $this->createOrderForm(DeleteOrderReductionCreditType::class, 'deleteOrderReductionCredit', $parameters)->createView();
  294. $parameters['form_add_product_to_order'] = $this->createOrderForm(AddPoductToOrderType::class, 'addProductToOrder', $parameters)->createView();
  295. $parameters['form_order_products'] = $this->createOrderForm(OrderProductsType::class, 'orderProducts', $parameters)->createView();
  296. $parameters['form_order_invoice_address'] = $this->createOrderForm(OrderInvoiceAddressType::class, 'orderInvoiceAddress', $parameters)->createView();
  297. $parameters['form_order_status'] = $this->createOrderForm(OrderStatusType::class, 'orderStatus', $parameters)->createView();
  298. }
  299. return parent::renderTemplate($actionName, $templatePath, $parameters);
  300. }
  301. public function renderOrderShopTemplate($actionName, $templatePath, array $parameters = [])
  302. {
  303. if ($actionName == 'show') {
  304. if (!isset($parameters['form_order_delivery_address'])) {
  305. $parameters['form_order_delivery_address'] = $this->createOrderForm(OrderDeliveryAddressType::class, 'orderDeliveryAddress', $parameters)->createView();
  306. }
  307. $parameters['form_add_order_reduction_credit'] = $this->createOrderForm(AddOrderReductionCreditType::class, 'addOrderReductionCredit', $parameters)->createView();
  308. $parameters['form_add_order_reduction_cart'] = $this->createOrderForm(AddOrderReductionCartType::class, 'addOrderReductionCart', $parameters)->createView();
  309. $parameters['form_delete_order_reduction_cart'] = $this->createOrderForm(DeleteOrderReductionCartType::class, 'deleteOrderReductionCart', $parameters)->createView();
  310. $parameters['form_delete_order_reduction_credit'] = $this->createOrderForm(DeleteOrderReductionCreditType::class, 'deleteOrderReductionCredit', $parameters)->createView();
  311. $parameters['form_add_product_to_order'] = $this->createOrderForm(AddPoductToOrderType::class, 'addProductToOrder', $parameters)->createView();
  312. $parameters['form_order_products'] = $this->createOrderForm(OrderProductsType::class, 'orderProducts', $parameters)->createView();
  313. $parameters['form_order_invoice_address'] = $this->createOrderForm(OrderInvoiceAddressType::class, 'orderInvoiceAddress', $parameters)->createView();
  314. $parameters['form_order_status'] = $this->createOrderForm(OrderStatusType::class, 'orderStatus', $parameters)->createView();
  315. $parameters['form_order_payment'] = $this->createOrderForm(OrderPaymentType::class, 'orderPayment', $parameters, false)->createView();
  316. }
  317. return parent::renderTemplate($actionName, $templatePath, $parameters);
  318. }
  319. public function createOrderForm($class, $action, $parameters, $data = true)
  320. {
  321. $options = array();
  322. if ($data) $options['data'] = $parameters['entity'];
  323. $options['action'] = $this->generateUrl('easyadmin', array(
  324. 'action' => $action,
  325. 'entity' => $this->entity['name'],
  326. 'id' => $parameters['entity']->getId()
  327. ));
  328. return $this->createForm($class, null, $options);
  329. }
  330. protected function newAction()
  331. {
  332. $this->dispatch(EasyAdminEvents::PRE_NEW);
  333. $entity = $this->executeDynamicMethod('createNew<EntityName>Entity');
  334. $easyadmin = $this->request->attributes->get('easyadmin');
  335. $easyadmin['item'] = $entity;
  336. $this->request->attributes->set('easyadmin', $easyadmin);
  337. $fields = $this->entity['new']['fields'];
  338. $newForm = $this->executeDynamicMethod('create<EntityName>NewForm', [$entity, $fields]);
  339. $newForm->handleRequest($this->request);
  340. //ETAPE 1 Choix de l'utilisateur
  341. $user = $newForm->get('user')->getData();
  342. if ($user == null) {
  343. $user = $this->getUserViaFirstStepForm($entity);
  344. }
  345. if (!$user instanceof UserInterface) return $user;
  346. else {
  347. $orderShop = $this->orderUtils->createOrderShop(array(
  348. 'user' => $user,
  349. 'merchant' => $this->merchantUtils->getMerchantUser()
  350. ));
  351. return $this->redirectToRoute('easyadmin', [
  352. 'action' => 'edit',
  353. 'entity' => $this->entity['name'],
  354. 'id' => $orderShop->getId()
  355. ]);
  356. }
  357. }
  358. /**
  359. * The method that is executed when the user performs a 'show' action on an entity.
  360. *
  361. * @return Response
  362. */
  363. public function showAction()
  364. {
  365. $this->dispatch(EasyAdminEvents::PRE_SHOW);
  366. $id = $this->request->query->get('id');
  367. $easyadmin = $this->request->attributes->get('easyadmin');
  368. $entity = $easyadmin['item'];
  369. $fields = $this->entity['show']['fields'];
  370. $deleteForm = $this->createDeleteForm($this->entity['name'], $id);
  371. $this->dispatch(EasyAdminEvents::POST_SHOW, [
  372. 'deleteForm' => $deleteForm,
  373. 'fields' => $fields,
  374. 'entity' => $entity,
  375. ]);
  376. $parameters = [
  377. 'entity' => $entity,
  378. 'fields' => $fields,
  379. 'delete_form' => $deleteForm->createView(),
  380. 'order' => $this->orderUtils->getOrderAsJsonObject($entity)
  381. ];
  382. return $this->executeDynamicMethod('render<EntityName>Template', ['show', $this->entity['templates']['show'], $parameters]);
  383. }
  384. /**
  385. * Réécriture de edit action pr rediriger vers le show */
  386. public function editAction()
  387. {
  388. $id = $this->request->query->get('id');
  389. $entity = $this->request->query->get('entity');
  390. return $this->redirectToRoute('easyadmin', [
  391. 'action' => 'show',
  392. 'entity' => $entity,
  393. 'id' => $id
  394. ]);
  395. }
  396. protected function getOrderShopEntity()
  397. {
  398. $easyadmin = $this->request->attributes->get('easyadmin');
  399. return $easyadmin['item'];
  400. }
  401. }