選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

520 行
25KB

  1. <?php
  2. namespace Lc\ShopBundle\Controller\Backend;
  3. use Doctrine\ORM\EntityManagerInterface;
  4. use EasyCorp\Bundle\EasyAdminBundle\Event\EasyAdminEvents;
  5. use FOS\UserBundle\Model\UserManagerInterface;
  6. use Lc\ShopBundle\Context\MerchantUtilsInterface;
  7. use Lc\ShopBundle\Context\OrderPaymentInterface;
  8. use Lc\ShopBundle\Context\OrderProductInterface;
  9. use Lc\ShopBundle\Context\OrderReductionCartInterface;
  10. use Lc\ShopBundle\Context\OrderReductionCreditInterface;
  11. use Lc\ShopBundle\Context\OrderShopInterface;
  12. use Lc\ShopBundle\Context\OrderUtilsInterface;
  13. use Lc\ShopBundle\Context\UserInterface;
  14. use Lc\ShopBundle\Form\Backend\Order\AddPoductToOrderType;
  15. use Lc\ShopBundle\Form\Backend\Order\DeleteOrderReductionCartType;
  16. use Lc\ShopBundle\Form\Backend\Order\DeleteOrderReductionCreditType;
  17. use Lc\ShopBundle\Form\Backend\Order\OrderDeliveryAddressType;
  18. use Lc\ShopBundle\Form\Backend\Order\OrderInvoiceAddressType;
  19. use Lc\ShopBundle\Form\Backend\Order\OrderPaymentType;
  20. use Lc\ShopBundle\Form\Backend\Order\OrderProductsType;
  21. use Lc\ShopBundle\Form\Backend\Order\AddOrderReductionCartType;
  22. use Lc\ShopBundle\Form\Backend\Order\AddOrderReductionCreditType;
  23. use Lc\ShopBundle\Form\Backend\Order\OrderStatusType;
  24. use Lc\ShopBundle\Model\CreditHistory;
  25. use Lc\ShopBundle\Model\OrderStatus;
  26. use Lc\ShopBundle\Services\CreditUtils;
  27. use Lc\ShopBundle\Services\Utils;
  28. use Mailjet\MailjetSwiftMailer\SwiftMailer\MailjetTransport;
  29. use Symfony\Bridge\Doctrine\Form\Type\EntityType;
  30. use Symfony\Component\Form\Extension\Core\Type\SubmitType;
  31. use Symfony\Component\HttpFoundation\Response;
  32. use Symfony\Component\Security\Core\Security;
  33. use Symfony\Contracts\Translation\TranslatorInterface;
  34. class OrderController extends AdminController
  35. {
  36. protected $creditUtils;
  37. public function __construct(Security $security, UserManagerInterface $userManager, EntityManagerInterface $em, Utils $utils, MerchantUtilsInterface $merchantUtils, MailjetTransport $mailjetTransport, OrderUtilsInterface $orderUtils, TranslatorInterface $translator, CreditUtils $creditUtils)
  38. {
  39. parent::__construct($security, $userManager, $em, $utils, $merchantUtils, $mailjetTransport, $orderUtils, $translator);
  40. $this->creditUtils = $creditUtils;
  41. }
  42. protected function createListQueryBuilder($entityClass, $sortDirection, $sortField = null, $dqlFilter = null)
  43. {
  44. $filterIsOrderStatus = false;
  45. $filterNotOrderStatus = false;
  46. if (isset($dqlFilter['isOrderStatus'])) $filterIsOrderStatus = $dqlFilter['isOrderStatus'];
  47. if (isset($dqlFilter['notOrderStatus'])) $filterNotOrderStatus = $dqlFilter['notOrderStatus'];
  48. $dqlFilter = $dqlFilter['filter'];
  49. $queryBuilder = parent::createListQueryBuilder($entityClass, $sortDirection, $sortField, $dqlFilter);
  50. $queryBuilder->leftJoin('entity.orderStatus', 'orderStatus');
  51. if ($filterIsOrderStatus) {
  52. $queryBuilder->andWhere('orderStatus.alias IN (:status)');
  53. $queryBuilder->setParameter('status', $filterIsOrderStatus);
  54. }
  55. if ($filterNotOrderStatus) {
  56. $queryBuilder->andWhere('orderStatus.alias NOT IN (:status)');
  57. $queryBuilder->setParameter('status', $filterNotOrderStatus);
  58. }
  59. return $queryBuilder;
  60. }
  61. public function updateEntity($entity)
  62. {
  63. foreach ($entity->getOrderProducts() as $orderProduct) {
  64. //dump($orderProduct);
  65. $orderProduct->setCreatedBy($this->getUser());
  66. $orderProduct->setUpdatedBy($this->getUser());
  67. $orderProduct->setTaxRate($orderProduct->getProduct()->getProductFamily()->getTaxRate());
  68. $orderProduct->setOrderShop($entity);
  69. $this->em->persist($orderProduct);
  70. $entity->addOrderProduct($orderProduct);
  71. }
  72. $this->setUpdated($entity);
  73. parent::updateEntity($entity);
  74. }
  75. public function persistEntity($entity)
  76. {
  77. foreach ($entity->getOrderProducts() as $orderProduct) {
  78. $orderProduct->setUnit($orderProduct->getProduct()->getUnitInherited());
  79. $orderProduct->setTitle($orderProduct->getProduct()->getTitleInherited());
  80. $orderProduct->setPrice($orderProduct->getProduct()->getPriceInherited());
  81. if ($orderProduct->getProduct()->getProductFamily()->getTaxRate()) {
  82. $orderProduct->setTaxRate($orderProduct->getProduct()->getProductFamily()->getTaxRate());
  83. } else {
  84. $orderProduct->setTaxRate($this->security->getUser()->getMerchant()->getTaxRate());
  85. }
  86. $orderProduct->setOrderShop($entity);
  87. $orderProduct->setCreatedBy($this->getUser());
  88. $orderProduct->setUpdatedBy($this->getUser());
  89. $this->em->persist($orderProduct);
  90. $entity->addOrderProduct($orderProduct);
  91. }
  92. parent::persistEntity($entity);
  93. }
  94. public function getUserViaFirstStepForm($entity)
  95. {
  96. $userClass = $this->em->getClassMetadata(UserInterface::class);
  97. $userChoiceForm = $this->createFormBuilder($entity)
  98. ->add('user', EntityType::class, array(
  99. 'class' => $userClass->name
  100. ))
  101. ->add('nextStep', SubmitType::class)
  102. ->getForm();
  103. $userChoiceForm->handleRequest($this->request);
  104. if ($userChoiceForm->isSubmitted() && $userChoiceForm->isValid()) {
  105. return $userChoiceForm->get('user')->getData();
  106. }
  107. $parameters = [
  108. 'form' => $userChoiceForm->createView(),
  109. 'formView' => 'default',
  110. 'entity' => $entity,
  111. ];
  112. return $this->executeDynamicMethod('render<EntityName>Template', ['new', $this->entity['templates']['new'], $parameters]);
  113. }
  114. public function addProductToOrderAction()
  115. {
  116. $orderShop = $this->getOrderShopEntity();
  117. $orderProductClass = $this->em->getClassMetadata(OrderProductInterface::class);
  118. $formAddProductToOrder = $this->createForm(AddPoductToOrderType::class);
  119. $formAddProductToOrder->handleRequest($this->request);
  120. if ($formAddProductToOrder->isSubmitted() && $formAddProductToOrder->isValid()) {
  121. $orderProduct = new $orderProductClass->name;
  122. if ($this->orderUtils->isProductAvailable($formAddProductToOrder->get('product')->getData(), $formAddProductToOrder->get('quantity')->getData())) {
  123. $orderProduct->setQuantityOrder($formAddProductToOrder->get('quantity')->getData());
  124. $orderProduct->setProduct($formAddProductToOrder->get('product')->getData());
  125. $this->orderUtils->addOrderProduct($orderShop, $orderProduct);
  126. $this->utils->addFlash('success', 'success.order.addProduct');
  127. } else {
  128. $this->utils->addFlash('error', 'error.order.productUnavailable');
  129. }
  130. } else {
  131. $this->utils->addFlash('error', 'error.form.submitted');
  132. }
  133. return $this->createOrderAjaxReponse($orderShop);
  134. }
  135. public function orderProductsAction()
  136. {
  137. $orderShop = $this->getOrderShopEntity();
  138. $formOrderProducts = $this->createForm(OrderProductsType::class, $orderShop);
  139. $formOrderProducts->handleRequest($this->request);
  140. if ($formOrderProducts->isSubmitted() && $formOrderProducts->isValid()) {
  141. $error = false;
  142. // dump($formOrderProducts->get('orderProducts')->getData());
  143. foreach ($orderShop->getOrderProducts() as $orderProduct) {
  144. if ($this->orderUtils->isProductAvailable($orderProduct->getProduct(), $orderProduct->getQuantityOrder())) {
  145. if ($orderProduct->getQuantityOrder() <= 0) {
  146. $this->em->remove($orderProduct);
  147. } else {
  148. $this->em->persist($orderProduct);
  149. }
  150. } else {
  151. $error = true;
  152. $this->utils->addFlash('error', 'error.order.productUnavailable');
  153. }
  154. }
  155. if (!$error) {
  156. $this->em->flush();
  157. $this->utils->addFlash('success', 'success.order.editQuantity');
  158. }
  159. } else {
  160. $this->utils->addFlash('error', 'error.form.submitted');
  161. }
  162. return $this->createOrderAjaxReponse($orderShop);
  163. }
  164. public function orderInvoiceAddressAction()
  165. {
  166. $orderShop = $this->getOrderShopEntity();
  167. $formOrderInvoiceAddress = $this->createForm(OrderInvoiceAddressType::class, $orderShop);
  168. $formOrderInvoiceAddress->handleRequest($this->request);
  169. if ($formOrderInvoiceAddress->isSubmitted() && $formOrderInvoiceAddress->isValid()) {
  170. //TODO si la commande est valide on hydrate le champ invoiceAddresText et on vide invoiceAddres
  171. $this->em->persist($orderShop);
  172. $this->em->flush();
  173. $this->utils->addFlash('success', 'success.order.changeInvoiceAddress');
  174. } else {
  175. $this->utils->addFlash('error', 'error.form.submitted');
  176. }
  177. return $this->createOrderAjaxReponse($orderShop);
  178. }
  179. public function orderStatusAction()
  180. {
  181. $orderShop = $this->getOrderShopEntity();
  182. $formOrderStatus = $this->createForm(OrderStatusType::class, $orderShop);
  183. $formOrderStatus->handleRequest($this->request);
  184. if ($formOrderStatus->isSubmitted() && $formOrderStatus->isValid()) {
  185. $oldStatus = $orderShop->getOrderStatus();
  186. $orderShop = $this->orderUtils->changeOrderStatus($formOrderStatus->get('orderStatus')->getData(), $orderShop);
  187. if($oldStatus !== $orderShop->getOrderStatus()){
  188. $this->utils->addFlash('success', 'success.order.changeStatus');
  189. }
  190. } else {
  191. $this->utils->addFlash('success', 'error.form.submitted');
  192. }
  193. return $this->redirectToRoute('easyadmin', [
  194. 'action' => 'show',
  195. 'entity' => $this->request->query->get('entity'),
  196. 'id' => $orderShop->getId()
  197. ]);
  198. }
  199. public function orderPaymentAction()
  200. {
  201. $orderShop = $this->getOrderShopEntity();
  202. $orderPaymentClass = $this->em->getClassMetadata(OrderPaymentInterface::class);
  203. $orderPayment = new $orderPaymentClass->name;
  204. $formOrderPayment = $this->createForm(OrderPaymentType::class, $orderPayment);
  205. $formOrderPayment->handleRequest($this->request);
  206. if ($formOrderPayment->isSubmitted() && $formOrderPayment->isValid()) {
  207. $orderPayment->setOrderShop($orderShop);
  208. if($orderPayment->getMeanPayment('credit')){
  209. $orderPayment->setEditable(false);
  210. $params['amount'] = $orderPayment->getAmount();
  211. $params['orderPayment'] = $orderPayment->getId();
  212. $creditHistory = $this->creditUtils->initCreditHistory(CreditHistory::TYPE_DEBIT, $orderShop->getUser(), $params);
  213. if($this->creditUtils->saveCreditHistory($creditHistory)) {
  214. $this->em->persist($orderPayment);
  215. $this->em->flush();
  216. $this->utils->addFlash('success', 'success.credit.debited');
  217. $this->utils->addFlash('success', 'success.order.addPayment');
  218. }else{
  219. $this->utils->addFlash('success', 'error.credit.debited');
  220. }
  221. }else{
  222. $orderPayment->setEditable(true);
  223. $this->em->persist($orderPayment);
  224. $this->em->flush();
  225. $this->utils->addFlash('success', 'success.order.addPayment');
  226. }
  227. } else {
  228. $this->utils->addFlash('error', $formOrderPayment->getErrors());
  229. }
  230. return $this->createOrderAjaxReponse($orderShop);
  231. }
  232. public function addOrderReductionCartAction()
  233. {
  234. $orderShop = $this->getOrderShopEntity();
  235. $formAddOrderReductionCart = $this->createForm(AddOrderReductionCartType::class, $orderShop);
  236. $formAddOrderReductionCart->handleRequest($this->request);
  237. if ($formAddOrderReductionCart->isSubmitted() && $formAddOrderReductionCart->isValid()) {
  238. $reductionCart = $formAddOrderReductionCart->get('reductionCart')->getData();
  239. $orderShop->reductionError = array();
  240. if ($this->orderUtils->isReductionCartAllowAddToOrder($orderShop, $reductionCart)) {
  241. $orderReductionCart = $this->orderUtils->createOrderReductionCart($orderShop, $reductionCart);
  242. $this->em->persist($orderReductionCart);
  243. $this->em->flush();
  244. $this->utils->addFlash('success', 'success.order.addReductionCart');
  245. }
  246. } else {
  247. $this->utils->addFlash('error', $formAddOrderReductionCart->getErrors());
  248. }
  249. return $this->createOrderAjaxReponse($orderShop);
  250. }
  251. public function addOrderReductionCreditAction()
  252. {
  253. $orderShop = $this->getOrderShopEntity();
  254. $formAddOrderReductionCredit = $this->createForm(AddOrderReductionCreditType::class, $orderShop);
  255. $formAddOrderReductionCredit->handleRequest($this->request);
  256. if ($formAddOrderReductionCredit->isSubmitted() && $formAddOrderReductionCredit->isValid()) {
  257. $reductionCredit = $formAddOrderReductionCredit->get('reductionCredit')->getData();
  258. if ($this->orderUtils->isReductionCreditAllowAddToOrder($orderShop, $reductionCredit)) {
  259. $orderReductionCredit = $this->orderUtils->createOrderReductionCredit($orderShop, $reductionCredit);
  260. $this->em->persist($orderReductionCredit);
  261. $this->em->flush();
  262. $this->utils->addFlash('success', 'success.order.addReductionCredit');
  263. }
  264. } else {
  265. $this->utils->addFlash('error', $formAddOrderReductionCredit->getErrors());
  266. }
  267. return $this->createOrderAjaxReponse($orderShop);
  268. }
  269. public function deleteOrderReductionCartAction()
  270. {
  271. $orderShop = $this->getOrderShopEntity();
  272. $formDeleteOrderReductionCart = $this->createForm(DeleteOrderReductionCartType::class);
  273. $formDeleteOrderReductionCart->handleRequest($this->request);
  274. if ($formDeleteOrderReductionCart->isSubmitted() && $formDeleteOrderReductionCart->isValid()) {
  275. $orderReductionCart = $this->em->getRepository(OrderReductionCartInterface::class)->find($formDeleteOrderReductionCart->get('id')->getData());
  276. if($orderReductionCart && $orderShop->getOrderReductionCarts()->contains($orderReductionCart)){
  277. $orderShop->removeOrderReductionCart($orderReductionCart);
  278. $this->em->remove($orderReductionCart);
  279. $this->em->flush();
  280. $this->utils->addFlash('warning', 'success.order.removeReductionCart');
  281. }
  282. } else {
  283. $this->utils->addFlash('error', $formDeleteOrderReductionCart->getErrors());
  284. }
  285. return $this->createOrderAjaxReponse($orderShop);
  286. }
  287. public function deleteOrderReductionCreditAction()
  288. {
  289. $orderShop = $this->getOrderShopEntity();
  290. $formDeleteOrderReductionCredit = $this->createForm(DeleteOrderReductionCreditType::class);
  291. $formDeleteOrderReductionCredit->handleRequest($this->request);
  292. if ($formDeleteOrderReductionCredit->isSubmitted() && $formDeleteOrderReductionCredit->isValid()) {
  293. $orderReductionCredit = $this->em->getRepository(OrderReductionCreditInterface::class)->find($formDeleteOrderReductionCredit->get('id')->getData());
  294. if($formDeleteOrderReductionCredit && $orderShop->getOrderReductionCredits()->contains($orderReductionCredit)){
  295. $orderShop->removeOrderReductionCredit($orderReductionCredit);
  296. $this->em->remove($orderReductionCredit);
  297. $this->em->flush();
  298. $this->utils->addFlash('warning', 'success.order.removeReductionCredit');
  299. }
  300. } else {
  301. $this->utils->addFlash('error', $formDeleteOrderReductionCredit->getErrors());
  302. }
  303. return $this->createOrderAjaxReponse($orderShop);
  304. }
  305. protected function createOrderAjaxReponse(OrderShopInterface $order)
  306. {
  307. $response['flashMessages'] = $this->utils->getFlashMessages();
  308. $response['data'] = $this->orderUtils->getOrderAsJsonObject($order);
  309. return new Response(json_encode($response));
  310. }
  311. public function renderOrderShopTemplate($actionName, $templatePath, array $parameters = [])
  312. {
  313. if ($actionName == 'show') {
  314. $orderShop = $this->getOrderShopEntity();
  315. switch ($orderShop->getOrderStatus()->getAlias()){
  316. case OrderStatus::ALIAS_CART :
  317. if (!isset($parameters['form_order_delivery_address'])) {
  318. $parameters['form_order_delivery_address'] = $this->createCustomForm(OrderDeliveryAddressType::class, 'orderDeliveryAddress', $parameters)->createView();
  319. }
  320. $parameters['form_add_order_reduction_credit'] = $this->createCustomForm(AddOrderReductionCreditType::class, 'addOrderReductionCredit', $parameters)->createView();
  321. $parameters['form_add_order_reduction_cart'] = $this->createCustomForm(AddOrderReductionCartType::class, 'addOrderReductionCart', $parameters)->createView();
  322. $parameters['form_delete_order_reduction_cart'] = $this->createCustomForm(DeleteOrderReductionCartType::class, 'deleteOrderReductionCart', $parameters)->createView();
  323. $parameters['form_delete_order_reduction_credit'] = $this->createCustomForm(DeleteOrderReductionCreditType::class, 'deleteOrderReductionCredit', $parameters)->createView();
  324. $parameters['form_add_product_to_order'] = $this->createCustomForm(AddPoductToOrderType::class, 'addProductToOrder', $parameters)->createView();
  325. $parameters['form_order_products'] = $this->createCustomForm(OrderProductsType::class, 'orderProducts', $parameters)->createView();
  326. break;
  327. case OrderStatus::ALIAS_WAITING_PAYMENT_CREDIT :
  328. case OrderStatus::ALIAS_WAITING_PAYMENT_ONLINE :
  329. $parameters['form_order_payment'] = $this->createCustomForm(OrderPaymentType::class, 'orderPayment', $parameters, false)->createView();
  330. $parameters['form_order_status'] = $this->createCustomForm(OrderStatusType::class, 'orderStatus', $parameters)->createView();
  331. $parameters['form_order_invoice_address'] = $this->createCustomForm(OrderInvoiceAddressType::class, 'orderInvoiceAddress', $parameters)->createView();
  332. break;
  333. }
  334. }
  335. dump($templatePath);
  336. return parent::renderTemplate($actionName, $templatePath, $parameters);
  337. }
  338. protected function newAction()
  339. {
  340. $this->dispatch(EasyAdminEvents::PRE_NEW);
  341. $entity = $this->executeDynamicMethod('createNew<EntityName>Entity');
  342. $easyadmin = $this->request->attributes->get('easyadmin');
  343. $easyadmin['item'] = $entity;
  344. $this->request->attributes->set('easyadmin', $easyadmin);
  345. $fields = $this->entity['new']['fields'];
  346. $newForm = $this->executeDynamicMethod('create<EntityName>NewForm', [$entity, $fields]);
  347. $newForm->handleRequest($this->request);
  348. //ETAPE 1 Choix de l'utilisateur
  349. $user = $newForm->get('user')->getData();
  350. if ($user == null) {
  351. $user = $this->getUserViaFirstStepForm($entity);
  352. }
  353. if (!$user instanceof UserInterface) return $user;
  354. else {
  355. $orderShop = $this->orderUtils->createOrderShop(array(
  356. 'user' => $user,
  357. 'merchant' => $this->merchantUtils->getMerchantUser()
  358. ));
  359. return $this->redirectToRoute('easyadmin', [
  360. 'action' => 'edit',
  361. 'entity' => $this->entity['name'],
  362. 'id' => $orderShop->getId()
  363. ]);
  364. }
  365. }
  366. /**
  367. * The method that is executed when the user performs a 'show' action on an entity.
  368. *
  369. * @return Response
  370. */
  371. public function showAction()
  372. {
  373. $this->dispatch(EasyAdminEvents::PRE_SHOW);
  374. $id = $this->request->query->get('id');
  375. $easyadmin = $this->request->attributes->get('easyadmin');
  376. $entity = $easyadmin['item'];
  377. $fields = $this->entity['show']['fields'];
  378. $deleteForm = $this->createDeleteForm($this->entity['name'], $id);
  379. $this->dispatch(EasyAdminEvents::POST_SHOW, [
  380. 'deleteForm' => $deleteForm,
  381. 'fields' => $fields,
  382. 'entity' => $entity,
  383. ]);
  384. $parameters = [
  385. 'entity' => $entity,
  386. 'fields' => $fields,
  387. 'delete_form' => $deleteForm->createView(),
  388. 'order' => $this->orderUtils->getOrderAsJsonObject($entity)
  389. ];
  390. return $this->executeDynamicMethod('renderOrderShopTemplate', ['show', $this->entity['templates']['show'], $parameters]);
  391. }
  392. /**
  393. * Réécriture de edit action pr rediriger vers le show */
  394. public function editAction()
  395. {
  396. $id = $this->request->query->get('id');
  397. $entity = $this->request->query->get('entity');
  398. return $this->redirectToRoute('easyadmin', [
  399. 'action' => 'show',
  400. 'entity' => $entity,
  401. 'id' => $id
  402. ]);
  403. }
  404. protected function getOrderShopEntity()
  405. {
  406. $easyadmin = $this->request->attributes->get('easyadmin');
  407. return $easyadmin['item'];
  408. }
  409. }