Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

536 rindas
26KB

  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. //TODO si édition despayment existant on vérifie qu'il est lié à nla commabde courante
  203. $orderPaymentPost = $this->request->request->get('order_payment');
  204. if($orderPaymentPost['id']){
  205. $orderPayment = $this->em->getRepository(OrderPaymentInterface::class)->find($orderPaymentPost['id']);
  206. }else{
  207. $orderPaymentClass = $this->em->getClassMetadata(OrderPaymentInterface::class);
  208. $orderPayment = new $orderPaymentClass->name;
  209. }
  210. $formOrderPayment = $this->createForm(OrderPaymentType::class, $orderPayment);
  211. $formOrderPayment->handleRequest($this->request);
  212. if ($formOrderPayment->isSubmitted() && $formOrderPayment->isValid()) {
  213. $orderPayment->setOrderShop($orderShop);
  214. if($orderPayment->getMeanPayment() === Utils::MEAN_PAYMENT_CREDIT){
  215. $orderPayment->setEditable(false);
  216. $params['orderPayment'] = $orderPayment;
  217. $creditHistory = $this->creditUtils->initCreditHistory(CreditHistory::TYPE_DEBIT, $orderShop->getUser(), $params);
  218. if ($this->creditUtils->isCreditSufficientToPay($creditHistory->getUserMerchant(), $creditHistory->getAmountInherited())) {
  219. if($this->creditUtils->saveCreditHistory($creditHistory)) {
  220. $this->em->persist($orderPayment);
  221. $this->em->flush();
  222. $this->utils->addFlash('success', 'success.credit.debited');
  223. $this->utils->addFlash('success', 'success.order.addPayment');
  224. }
  225. }else{
  226. $this->utils->addFlash('error', 'error.credit.notEnoughCredit');
  227. }
  228. }else{
  229. $orderPayment->setEditable(true);
  230. $this->em->persist($orderPayment);
  231. $this->em->flush();
  232. $this->utils->addFlash('success', 'success.order.addPayment');
  233. }
  234. } else {
  235. $this->utils->addFlash('error', $formOrderPayment->getErrors());
  236. }
  237. return $this->createOrderAjaxReponse($orderShop);
  238. }
  239. public function addOrderReductionCartAction()
  240. {
  241. $orderShop = $this->getOrderShopEntity();
  242. $formAddOrderReductionCart = $this->createForm(AddOrderReductionCartType::class, $orderShop);
  243. $formAddOrderReductionCart->handleRequest($this->request);
  244. if ($formAddOrderReductionCart->isSubmitted() && $formAddOrderReductionCart->isValid()) {
  245. $reductionCart = $formAddOrderReductionCart->get('reductionCart')->getData();
  246. $orderShop->reductionError = array();
  247. if ($this->orderUtils->isReductionCartAllowAddToOrder($orderShop, $reductionCart)) {
  248. $orderReductionCart = $this->orderUtils->createOrderReductionCart($orderShop, $reductionCart);
  249. $this->em->persist($orderReductionCart);
  250. $this->em->flush();
  251. $this->utils->addFlash('success', 'success.order.addReductionCart');
  252. }
  253. } else {
  254. $this->utils->addFlash('error', $formAddOrderReductionCart->getErrors());
  255. }
  256. return $this->createOrderAjaxReponse($orderShop);
  257. }
  258. public function addOrderReductionCreditAction()
  259. {
  260. $orderShop = $this->getOrderShopEntity();
  261. $formAddOrderReductionCredit = $this->createForm(AddOrderReductionCreditType::class, $orderShop);
  262. $formAddOrderReductionCredit->handleRequest($this->request);
  263. if ($formAddOrderReductionCredit->isSubmitted() && $formAddOrderReductionCredit->isValid()) {
  264. $reductionCredit = $formAddOrderReductionCredit->get('reductionCredit')->getData();
  265. if ($this->orderUtils->isReductionCreditAllowAddToOrder($orderShop, $reductionCredit)) {
  266. $orderReductionCredit = $this->orderUtils->createOrderReductionCredit($orderShop, $reductionCredit);
  267. $this->em->persist($orderReductionCredit);
  268. $this->em->flush();
  269. $this->utils->addFlash('success', 'success.order.addReductionCredit');
  270. }
  271. } else {
  272. $this->utils->addFlash('error', $formAddOrderReductionCredit->getErrors());
  273. }
  274. return $this->createOrderAjaxReponse($orderShop);
  275. }
  276. public function deleteOrderReductionCartAction()
  277. {
  278. $orderShop = $this->getOrderShopEntity();
  279. $formDeleteOrderReductionCart = $this->createForm(DeleteOrderReductionCartType::class);
  280. $formDeleteOrderReductionCart->handleRequest($this->request);
  281. if ($formDeleteOrderReductionCart->isSubmitted() && $formDeleteOrderReductionCart->isValid()) {
  282. $orderReductionCart = $this->em->getRepository(OrderReductionCartInterface::class)->find($formDeleteOrderReductionCart->get('id')->getData());
  283. if($orderReductionCart && $orderShop->getOrderReductionCarts()->contains($orderReductionCart)){
  284. $orderShop->removeOrderReductionCart($orderReductionCart);
  285. $this->em->remove($orderReductionCart);
  286. $this->em->flush();
  287. $this->utils->addFlash('warning', 'success.order.removeReductionCart');
  288. }
  289. } else {
  290. $this->utils->addFlash('error', $formDeleteOrderReductionCart->getErrors());
  291. }
  292. return $this->createOrderAjaxReponse($orderShop);
  293. }
  294. public function deleteOrderReductionCreditAction()
  295. {
  296. $orderShop = $this->getOrderShopEntity();
  297. $formDeleteOrderReductionCredit = $this->createForm(DeleteOrderReductionCreditType::class);
  298. $formDeleteOrderReductionCredit->handleRequest($this->request);
  299. if ($formDeleteOrderReductionCredit->isSubmitted() && $formDeleteOrderReductionCredit->isValid()) {
  300. $orderReductionCredit = $this->em->getRepository(OrderReductionCreditInterface::class)->find($formDeleteOrderReductionCredit->get('id')->getData());
  301. if($formDeleteOrderReductionCredit && $orderShop->getOrderReductionCredits()->contains($orderReductionCredit)){
  302. $orderShop->removeOrderReductionCredit($orderReductionCredit);
  303. $this->em->remove($orderReductionCredit);
  304. $this->em->flush();
  305. $this->utils->addFlash('warning', 'success.order.removeReductionCredit');
  306. }
  307. } else {
  308. $this->utils->addFlash('error', $formDeleteOrderReductionCredit->getErrors());
  309. }
  310. return $this->createOrderAjaxReponse($orderShop);
  311. }
  312. protected function createOrderAjaxReponse(OrderShopInterface $order)
  313. {
  314. $response['flashMessages'] = $this->utils->getFlashMessages();
  315. $response['data'] = $this->orderUtils->getOrderAsJsonObject($order);
  316. return new Response(json_encode($response));
  317. }
  318. public function renderOrderShopTemplate($actionName, $templatePath, array $parameters = [])
  319. {
  320. if ($actionName == 'show') {
  321. $orderShop = $this->getOrderShopEntity();
  322. switch ($orderShop->getOrderStatus()->getAlias()){
  323. case OrderStatus::ALIAS_CART :
  324. if (!isset($parameters['form_order_delivery_address'])) {
  325. $parameters['form_order_delivery_address'] = $this->createCustomForm(OrderDeliveryAddressType::class, 'orderDeliveryAddress', $parameters)->createView();
  326. }
  327. $parameters['form_add_order_reduction_credit'] = $this->createCustomForm(AddOrderReductionCreditType::class, 'addOrderReductionCredit', $parameters)->createView();
  328. $parameters['form_add_order_reduction_cart'] = $this->createCustomForm(AddOrderReductionCartType::class, 'addOrderReductionCart', $parameters)->createView();
  329. $parameters['form_delete_order_reduction_cart'] = $this->createCustomForm(DeleteOrderReductionCartType::class, 'deleteOrderReductionCart', $parameters)->createView();
  330. $parameters['form_delete_order_reduction_credit'] = $this->createCustomForm(DeleteOrderReductionCreditType::class, 'deleteOrderReductionCredit', $parameters)->createView();
  331. $parameters['form_add_product_to_order'] = $this->createCustomForm(AddPoductToOrderType::class, 'addProductToOrder', $parameters)->createView();
  332. $parameters['form_order_products'] = $this->createCustomForm(OrderProductsType::class, 'orderProducts', $parameters)->createView();
  333. $parameters['form_order_payment'] = $this->createCustomForm(OrderPaymentType::class, 'orderPayment', $parameters, false)->createView();
  334. $parameters['form_order_status'] = $this->createCustomForm(OrderStatusType::class, 'orderStatus', $parameters)->createView();
  335. $parameters['form_order_invoice_address'] = $this->createCustomForm(OrderInvoiceAddressType::class, 'orderInvoiceAddress', $parameters)->createView();
  336. break;
  337. case OrderStatus::ALIAS_WAITING_PAYMENT_CREDIT :
  338. case OrderStatus::ALIAS_WAITING_PAYMENT_ONLINE :
  339. $parameters['form_order_payment'] = $this->createCustomForm(OrderPaymentType::class, 'orderPayment', $parameters, false)->createView();
  340. $parameters['form_order_status'] = $this->createCustomForm(OrderStatusType::class, 'orderStatus', $parameters)->createView();
  341. $parameters['form_order_invoice_address'] = $this->createCustomForm(OrderInvoiceAddressType::class, 'orderInvoiceAddress', $parameters)->createView();
  342. break;
  343. }
  344. }
  345. return parent::renderTemplate($actionName, $templatePath, $parameters);
  346. }
  347. protected function newAction()
  348. {
  349. $this->dispatch(EasyAdminEvents::PRE_NEW);
  350. $entity = $this->executeDynamicMethod('createNew<EntityName>Entity');
  351. $easyadmin = $this->request->attributes->get('easyadmin');
  352. $easyadmin['item'] = $entity;
  353. $this->request->attributes->set('easyadmin', $easyadmin);
  354. $fields = $this->entity['new']['fields'];
  355. $newForm = $this->executeDynamicMethod('create<EntityName>NewForm', [$entity, $fields]);
  356. $newForm->handleRequest($this->request);
  357. //ETAPE 1 Choix de l'utilisateur
  358. $user = $newForm->get('user')->getData();
  359. if ($user == null) {
  360. $user = $this->getUserViaFirstStepForm($entity);
  361. }
  362. if (!$user instanceof UserInterface) return $user;
  363. else {
  364. $orderShop = $this->orderUtils->createOrderShop(array(
  365. 'user' => $user,
  366. 'merchant' => $this->merchantUtils->getMerchantUser()
  367. ));
  368. return $this->redirectToRoute('easyadmin', [
  369. 'action' => 'edit',
  370. 'entity' => $this->entity['name'],
  371. 'id' => $orderShop->getId()
  372. ]);
  373. }
  374. }
  375. /**
  376. * The method that is executed when the user performs a 'show' action on an entity.
  377. *
  378. * @return Response
  379. */
  380. public function showAction()
  381. {
  382. $this->dispatch(EasyAdminEvents::PRE_SHOW);
  383. $id = $this->request->query->get('id');
  384. $easyadmin = $this->request->attributes->get('easyadmin');
  385. $entity = $easyadmin['item'];
  386. $fields = $this->entity['show']['fields'];
  387. $deleteForm = $this->createDeleteForm($this->entity['name'], $id);
  388. $this->dispatch(EasyAdminEvents::POST_SHOW, [
  389. 'deleteForm' => $deleteForm,
  390. 'fields' => $fields,
  391. 'entity' => $entity,
  392. ]);
  393. $parameters = [
  394. 'entity' => $entity,
  395. 'fields' => $fields,
  396. 'delete_form' => $deleteForm->createView(),
  397. 'order' => $this->orderUtils->getOrderAsJsonObject($entity)
  398. ];
  399. return $this->executeDynamicMethod('renderOrderShopTemplate', ['show', $this->entity['templates']['show'], $parameters]);
  400. }
  401. /**
  402. * Réécriture de edit action pr rediriger vers le show */
  403. public function editAction()
  404. {
  405. $id = $this->request->query->get('id');
  406. $entity = $this->request->query->get('entity');
  407. return $this->redirectToRoute('easyadmin', [
  408. 'action' => 'show',
  409. 'entity' => $entity,
  410. 'id' => $id
  411. ]);
  412. }
  413. protected function getOrderShopEntity()
  414. {
  415. $easyadmin = $this->request->attributes->get('easyadmin');
  416. return $easyadmin['item'];
  417. }
  418. }