Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

OrderController.php 25KB

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