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.

516 lines
24KB

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