Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

510 lines
24KB

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