474 lines
22KB

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