No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.

429 líneas
20KB

  1. <?php
  2. namespace Lc\ShopBundle\Controller\Backend;
  3. use EasyCorp\Bundle\EasyAdminBundle\Event\EasyAdminEvents;
  4. use Lc\ShopBundle\Context\OrderProductInterface;
  5. use Lc\ShopBundle\Context\UserInterface;
  6. use Lc\ShopBundle\Form\Backend\Order\AddPoductToOrderType;
  7. use Lc\ShopBundle\Form\Backend\Order\OrderDeliveryAddressType;
  8. use Lc\ShopBundle\Form\Backend\Order\OrderInvoiceAddressType;
  9. use Lc\ShopBundle\Form\Backend\Order\OrderProductsType;
  10. use Lc\ShopBundle\Form\Backend\Order\OrderReductionCartType;
  11. use Lc\ShopBundle\Form\Backend\Order\OrderReductionCreditType;
  12. use Lc\ShopBundle\Form\Backend\Order\OrderStatusType;
  13. use Symfony\Bridge\Doctrine\Form\Type\EntityType;
  14. use Symfony\Component\Form\Extension\Core\Type\SubmitType;
  15. use Symfony\Component\HttpFoundation\Response;
  16. class OrderController extends AdminController
  17. {
  18. public function updateEntity($entity)
  19. {
  20. foreach ($entity->getOrderProducts() as $orderProduct) {
  21. //dump($orderProduct);
  22. $orderProduct->setCreatedBy($this->getUser());
  23. $orderProduct->setUpdatedBy($this->getUser());
  24. $orderProduct->setTaxRate($orderProduct->getProduct()->getProductFamily()->getTaxRate());
  25. $orderProduct->setOrderShop($entity);
  26. $this->em->persist($orderProduct);
  27. $entity->addOrderProduct($orderProduct);
  28. }
  29. $this->setUpdated($entity);
  30. parent::updateEntity($entity);
  31. }
  32. public function persistEntity($entity)
  33. {
  34. foreach ($entity->getOrderProducts() as $orderProduct) {
  35. $orderProduct->setUnit($orderProduct->getProduct()->getUnitInherited());
  36. $orderProduct->setTitle($orderProduct->getProduct()->getTitleInherited());
  37. $orderProduct->setPrice($orderProduct->getProduct()->getPriceInherited());
  38. if ($orderProduct->getProduct()->getProductFamily()->getTaxRate()) {
  39. $orderProduct->setTaxRate($orderProduct->getProduct()->getProductFamily()->getTaxRate());
  40. } else {
  41. $orderProduct->setTaxRate($this->security->getUser()->getMerchant()->getTaxRate());
  42. }
  43. $orderProduct->setOrderShop($entity);
  44. $orderProduct->setCreatedBy($this->getUser());
  45. $orderProduct->setUpdatedBy($this->getUser());
  46. $this->em->persist($orderProduct);
  47. $entity->addOrderProduct($orderProduct);
  48. }
  49. parent::persistEntity($entity);
  50. }
  51. public function getUserViaFirstStepForm($entity)
  52. {
  53. $userClass = $this->em->getClassMetadata(UserInterface::class);
  54. $userChoiceForm = $this->createFormBuilder($entity)
  55. ->add('user', EntityType::class, array(
  56. 'class' => $userClass->name
  57. ))
  58. ->add('nextStep', SubmitType::class)
  59. ->getForm();
  60. $userChoiceForm->handleRequest($this->request);
  61. if ($userChoiceForm->isSubmitted() && $userChoiceForm->isValid()) {
  62. return $userChoiceForm->get('user')->getData();
  63. }
  64. $parameters = [
  65. 'form' => $userChoiceForm->createView(),
  66. 'formView' => 'default',
  67. 'entity' => $entity,
  68. ];
  69. return $this->executeDynamicMethod('render<EntityName>Template', ['new', $this->entity['templates']['new'], $parameters]);
  70. }
  71. public function addProductToOrderAction()
  72. {
  73. $id = $this->request->query->get('id');
  74. $easyadmin = $this->request->attributes->get('easyadmin');
  75. $orderShop = $easyadmin['item'];
  76. $orderProductClass = $this->em->getClassMetadata(OrderProductInterface::class);
  77. $formAddProductToOrder = $this->createForm(AddPoductToOrderType::class);
  78. $formAddProductToOrder->handleRequest($this->request);
  79. if ($formAddProductToOrder->get('product')->getData() == null) {
  80. $response['status'] = 'error';
  81. $response['message'] = 'Vous devez choisir un produit dans la liste';
  82. } else if ($formAddProductToOrder->get('quantity')->getData() == null) {
  83. $response['status'] = 'error';
  84. $response['message'] = 'Vous devez entrer une quantité';
  85. } else if ($formAddProductToOrder->isSubmitted() && $formAddProductToOrder->isValid()) {
  86. $orderProduct = new $orderProductClass->name;
  87. $orderProduct->setQuantityOrder($formAddProductToOrder->get('quantity')->getData());
  88. $orderProduct->setProduct($formAddProductToOrder->get('product')->getData());
  89. $this->orderUtils->addOrderProduct($orderShop, $orderProduct);
  90. $response['status'] = 'success';
  91. $response['message'] = 'Le produit a bien été ajouté à la commande';
  92. } else {
  93. $response['status'] = 'error';
  94. $response['message'] = 'Une erreur est survenue';
  95. }
  96. $response['data'] = $this->orderUtils->getOrderAsJsonObject($orderShop);;
  97. return new Response(json_encode($response));
  98. }
  99. public function orderInvoiceAddressAction()
  100. {
  101. $id = $this->request->query->get('id');
  102. $easyadmin = $this->request->attributes->get('easyadmin');
  103. $orderShop = $easyadmin['item'];
  104. $formOrderInvoiceAddress = $this->createForm(OrderInvoiceAddressType::class, $orderShop);
  105. $formOrderInvoiceAddress->handleRequest($this->request);
  106. if ($formOrderInvoiceAddress->isSubmitted() && $formOrderInvoiceAddress->isValid()) {
  107. $this->em->persist($orderShop);
  108. $this->em->flush();
  109. $response['status'] = 'success';
  110. $response['message'] = 'La commande a bien été modifié';
  111. } else {
  112. $response['status'] = 'error';
  113. $response['message'] = 'Une erreur est survenue';
  114. }
  115. $response['data'] = $this->orderUtils->getOrderAsJsonObject($orderShop);;
  116. return new Response(json_encode($response));
  117. }
  118. public function orderStatusAction()
  119. {
  120. $id = $this->request->query->get('id');
  121. $easyadmin = $this->request->attributes->get('easyadmin');
  122. $orderShop = $easyadmin['item'];
  123. $formOrderStatus = $this->createForm(OrderStatusType::class, $orderShop);
  124. $formOrderStatus->handleRequest($this->request);
  125. if ($formOrderStatus->isSubmitted() && $formOrderStatus->isValid()) {
  126. if($orderShop = $this->orderUtils->setOrderStatus($formOrderStatus->get('orderStatus')->getData(), $orderShop)){
  127. $this->persistEntity($orderShop);
  128. $this->em->flush();
  129. $this->addFlash('success', 'La commande a bien été modifié.');
  130. }
  131. }else{
  132. $this->addFlash('error', 'Une erreur s\'est produite');
  133. }
  134. return $this->redirectToRoute('easyadmin', [
  135. 'action' => 'show',
  136. 'entity' => $this->request->query->get('entity'),
  137. 'id' => $id
  138. ]);
  139. }
  140. public function orderProductsAction()
  141. {
  142. $id = $this->request->query->get('id');
  143. $easyadmin = $this->request->attributes->get('easyadmin');
  144. $orderShop = $easyadmin['item'];
  145. $formOrderProducts = $this->createForm(OrderProductsType::class, $orderShop);
  146. $formOrderProducts->handleRequest($this->request);
  147. if ($formOrderProducts->isSubmitted() && $formOrderProducts->isValid()) {
  148. // dump($formOrderProducts->get('orderProducts')->getData());
  149. foreach ($orderShop->getOrderProducts() as $orderProduct) {
  150. if ($orderProduct->getQuantityOrder() <= 0) {
  151. $response['niche'] = $orderProduct->getQuantityOrder();
  152. $this->em->remove($orderProduct);
  153. } else {
  154. $this->em->persist($orderProduct);
  155. }
  156. }
  157. $this->em->flush();
  158. $response['status'] = 'success';
  159. $response['message'] = 'La commande a bien été modifié';
  160. } else {
  161. $response['status'] = 'error';
  162. $response['message'] = 'Une erreur est survenue';
  163. }
  164. $response['data'] = $this->orderUtils->getOrderAsJsonObject($orderShop);;
  165. return new Response(json_encode($response));
  166. }
  167. public function orderReductionCartAction()
  168. {
  169. $id = $this->request->query->get('id');
  170. $easyadmin = $this->request->attributes->get('easyadmin');
  171. $orderShop = $easyadmin['item'];
  172. $formOrderReductionCart = $this->createForm(OrderReductionCartType::class, $orderShop);
  173. $formOrderReductionCart->handleRequest($this->request);
  174. if ($formOrderReductionCart->isSubmitted() && $formOrderReductionCart->isValid()) {
  175. $reductionCart = $formOrderReductionCart->get('reductionCart')->getData();
  176. $orderShop->reductionError = array();
  177. if($this->orderUtils->isReductionCartAllowToBeAddToOrder($orderShop, $reductionCart)){
  178. $orderReductionCart = $this->orderUtils->createOrderReductionCart($orderShop, $reductionCart);
  179. $this->em->persist($orderReductionCart);
  180. $this->em->flush();
  181. $response['status'] = 'success';
  182. $response['message'] = 'La réduction a bien été ajouté';
  183. }else{
  184. $response['status'] = 'error';
  185. $response['message'] = 'Cette réduction ne peut pas être appliqué sur cette commande';
  186. $response['message'] .= '<ul>';
  187. foreach ($orderShop->reductionError as $error) {
  188. $response['message'] .= '<li> <i>'.$this->translator->trans($error, array(), 'lcshop'). '</i></li>';
  189. }
  190. $response['message'] .= '</ul>';
  191. }
  192. } else {
  193. $response['status'] = 'error';
  194. $response['message'] = 'Une erreur est survenue';
  195. }
  196. $response['data'] = $this->orderUtils->getOrderAsJsonObject($orderShop);;
  197. return new Response(json_encode($response));
  198. }
  199. public function renderOrderShopTemplate($actionName, $templatePath, array $parameters = [])
  200. {
  201. if ($actionName == 'show') {
  202. $formAddProductToOrder = $this->createForm(AddPoductToOrderType::class, null, array(
  203. 'action' => $this->generateUrl('easyadmin', [
  204. 'action' => 'addProductToOrder',
  205. 'entity' => $this->entity['name'],
  206. 'id' => $parameters['entity']->getId()
  207. ])
  208. ));
  209. $formOrderProducts = $this->createForm(OrderProductsType::class, null, array(
  210. 'action' => $this->generateUrl('easyadmin', [
  211. 'action' => 'orderProducts',
  212. 'entity' => $this->entity['name'],
  213. 'id' => $parameters['entity']->getId()
  214. ])
  215. ));
  216. $formOrderInvoiceAddress = $this->createForm(OrderInvoiceAddressType::class, null, array(
  217. 'data' => $parameters['entity'],
  218. 'action' => $this->generateUrl('easyadmin', [
  219. 'action' => 'orderInvoiceAddress',
  220. 'entity' => $this->entity['name'],
  221. 'id' => $parameters['entity']->getId()
  222. ])
  223. ));
  224. $formOrderStatus = $this->createForm(OrderStatusType::class, null, array(
  225. 'data' => $parameters['entity'],
  226. 'action' => $this->generateUrl('easyadmin', [
  227. 'action' => 'orderStatus',
  228. 'entity' => $this->entity['name'],
  229. 'id' => $parameters['entity']->getId()
  230. ])
  231. ));
  232. $formOrderReductionCart = $this->createForm(OrderReductionCartType::class, null, array(
  233. 'data' => $parameters['entity'],
  234. 'action' => $this->generateUrl('easyadmin', [
  235. 'action' => 'orderReductionCart',
  236. 'entity' => $this->entity['name'],
  237. 'id' => $parameters['entity']->getId()
  238. ])
  239. ));
  240. $formOrderReductionCredit = $this->createForm(OrderReductionCreditType::class, null, array(
  241. 'data' => $parameters['entity'],
  242. 'action' => $this->generateUrl('easyadmin', [
  243. 'action' => 'orderReductionCredit',
  244. 'entity' => $this->entity['name'],
  245. 'id' => $parameters['entity']->getId()
  246. ])
  247. ));
  248. if(!isset($parameters['form_order_delivery_address'])) {
  249. $formOrderDeliveryAddress = $this->createForm(OrderDeliveryAddressType::class, null, array(
  250. 'data' => $parameters['entity'],
  251. 'action' => $this->generateUrl('easyadmin', [
  252. 'action' => 'orderDeliveryAddress',
  253. 'entity' => $this->entity['name'],
  254. 'id' => $parameters['entity']->getId()
  255. ])
  256. ));
  257. $parameters['form_order_delivery_address'] = $formOrderDeliveryAddress->createView();
  258. }
  259. $parameters['form_order_reduction_credit'] = $formOrderReductionCredit->createView();
  260. $parameters['form_order_reduction_cart'] = $formOrderReductionCart->createView();
  261. $parameters['form_add_product_to_order'] = $formAddProductToOrder->createView();
  262. $parameters['form_order_products'] = $formOrderProducts->createView();
  263. $parameters['form_order_invoice_address'] = $formOrderInvoiceAddress->createView();
  264. $parameters['form_order_status'] = $formOrderStatus->createView();
  265. }
  266. return parent::renderTemplate($actionName, $templatePath, $parameters);
  267. }
  268. protected function newAction()
  269. {
  270. $this->dispatch(EasyAdminEvents::PRE_NEW);
  271. $entity = $this->executeDynamicMethod('createNew<EntityName>Entity');
  272. $easyadmin = $this->request->attributes->get('easyadmin');
  273. $easyadmin['item'] = $entity;
  274. $this->request->attributes->set('easyadmin', $easyadmin);
  275. $fields = $this->entity['new']['fields'];
  276. $newForm = $this->executeDynamicMethod('create<EntityName>NewForm', [$entity, $fields]);
  277. $newForm->handleRequest($this->request);
  278. //ETAPE 1 Choix de l'utilisateur
  279. $user = $newForm->get('user')->getData();
  280. if ($user == null) {
  281. $user = $this->getUserViaFirstStepForm($entity);
  282. }
  283. if (!$user instanceof UserInterface) return $user;
  284. else {
  285. $orderShop = $this->orderUtils->createOrderShop(array(
  286. 'user' => $user,
  287. 'merchant' => $this->merchantUtils->getMerchantUser()
  288. ));
  289. return $this->redirectToRoute('easyadmin', [
  290. 'action' => 'edit',
  291. 'entity' => $this->entity['name'],
  292. 'id' => $orderShop->getId()
  293. ]);
  294. }
  295. }
  296. /**
  297. * The method that is executed when the user performs a 'show' action on an entity.
  298. *
  299. * @return Response
  300. */
  301. public function showAction()
  302. {
  303. $this->dispatch(EasyAdminEvents::PRE_SHOW);
  304. $id = $this->request->query->get('id');
  305. $easyadmin = $this->request->attributes->get('easyadmin');
  306. $entity = $easyadmin['item'];
  307. $fields = $this->entity['show']['fields'];
  308. $deleteForm = $this->createDeleteForm($this->entity['name'], $id);
  309. $this->dispatch(EasyAdminEvents::POST_SHOW, [
  310. 'deleteForm' => $deleteForm,
  311. 'fields' => $fields,
  312. 'entity' => $entity,
  313. ]);
  314. $parameters = [
  315. 'entity' => $entity,
  316. 'fields' => $fields,
  317. 'delete_form' => $deleteForm->createView(),
  318. 'order' => $this->orderUtils->getOrderAsJsonObject($entity)
  319. ];
  320. return $this->executeDynamicMethod('render<EntityName>Template', ['show', $this->entity['templates']['show'], $parameters]);
  321. }
  322. /**
  323. * Réécriture de edit action pr rediriger vers le show */
  324. public function editAction()
  325. {
  326. $id = $this->request->query->get('id');
  327. $entity = $this->request->query->get('entity');
  328. return $this->redirectToRoute('easyadmin', [
  329. 'action' => 'show',
  330. 'entity' => $entity,
  331. 'id' => $id
  332. ]);
  333. }
  334. }