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

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