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.

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