538 lines
25KB

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