No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.

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