Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

475 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->setOrderStatus($formOrderStatus->get('orderStatus')->getData(), $orderShop)){
  128. $this->persistEntity($orderShop);
  129. $this->em->flush();
  130. $this->addFlash('success', 'La commande a bien été modifié.');
  131. }
  132. }else{
  133. $this->addFlash('error', 'Une erreur s\'est produite');
  134. }
  135. return $this->redirectToRoute('easyadmin', [
  136. 'action' => 'show',
  137. 'entity' => $this->request->query->get('entity'),
  138. 'id' => $id
  139. ]);
  140. }
  141. public function orderProductsAction()
  142. {
  143. $id = $this->request->query->get('id');
  144. $easyadmin = $this->request->attributes->get('easyadmin');
  145. $orderShop = $easyadmin['item'];
  146. $formOrderProducts = $this->createForm(OrderProductsType::class, $orderShop);
  147. $formOrderProducts->handleRequest($this->request);
  148. if ($formOrderProducts->isSubmitted() && $formOrderProducts->isValid()) {
  149. // dump($formOrderProducts->get('orderProducts')->getData());
  150. foreach ($orderShop->getOrderProducts() as $orderProduct) {
  151. if ($orderProduct->getQuantityOrder() <= 0) {
  152. $response['niche'] = $orderProduct->getQuantityOrder();
  153. $this->em->remove($orderProduct);
  154. } else {
  155. $this->em->persist($orderProduct);
  156. }
  157. }
  158. $this->em->flush();
  159. $response['status'] = 'success';
  160. $response['message'] = 'La commande a bien été modifié';
  161. } else {
  162. $response['status'] = 'error';
  163. $response['message'] = 'Une erreur est survenue';
  164. }
  165. $response['data'] = $this->orderUtils->getOrderAsJsonObject($orderShop);;
  166. return new Response(json_encode($response));
  167. }
  168. public function orderReductionCartAction()
  169. {
  170. $id = $this->request->query->get('id');
  171. $easyadmin = $this->request->attributes->get('easyadmin');
  172. $orderShop = $easyadmin['item'];
  173. $formOrderReductionCart = $this->createForm(OrderReductionCartType::class, $orderShop);
  174. $formOrderReductionCart->handleRequest($this->request);
  175. if ($formOrderReductionCart->isSubmitted() && $formOrderReductionCart->isValid()) {
  176. $reductionCart = $formOrderReductionCart->get('reductionCart')->getData();
  177. $orderShop->reductionError = array();
  178. if($this->orderUtils->isReductionCartAllowToBeAddToOrder($orderShop, $reductionCart)){
  179. $orderReductionCart = $this->orderUtils->createOrderReductionCart($orderShop, $reductionCart);
  180. $this->em->persist($orderReductionCart);
  181. $this->em->flush();
  182. $response['status'] = 'success';
  183. $response['message'] = 'La réduction a bien été ajouté';
  184. }else{
  185. $response['status'] = 'error';
  186. $response['message'] = 'Cette réduction ne peut pas être appliqué sur cette commande';
  187. $response['message'] .= '<ul>';
  188. foreach ($orderShop->reductionError as $error) {
  189. $response['message'] .= '<li> <i>'.$this->translator->trans($error, array(), 'lcshop'). '</i></li>';
  190. }
  191. $response['message'] .= '</ul>';
  192. }
  193. } else {
  194. $response['status'] = 'error';
  195. $response['message'] = 'Une erreur est survenue';
  196. }
  197. $response['data'] = $this->orderUtils->getOrderAsJsonObject($orderShop);;
  198. return new Response(json_encode($response));
  199. }
  200. public function orderReductionCreditAction()
  201. {
  202. $id = $this->request->query->get('id');
  203. $easyadmin = $this->request->attributes->get('easyadmin');
  204. $orderShop = $easyadmin['item'];
  205. $formOrderReductionCredit = $this->createForm(OrderReductionCreditType::class, $orderShop);
  206. $formOrderReductionCredit->handleRequest($this->request);
  207. if ($formOrderReductionCredit->isSubmitted() && $formOrderReductionCredit->isValid()) {
  208. $reductionCredit = $formOrderReductionCredit->get('reductionCredit')->getData();
  209. $orderShop->reductionError = array();
  210. if($this->orderUtils->isReductionCreditAllowToBeAddToOrder($orderShop, $reductionCredit)){
  211. $orderReductionCredit = $this->orderUtils->createOrderReductionCredit($orderShop, $reductionCredit);
  212. $this->em->persist($orderReductionCredit);
  213. $this->em->flush();
  214. $response['status'] = 'success';
  215. $response['message'] = 'L\'avoir a bien été ajouté';
  216. }else{
  217. $response['status'] = 'error';
  218. $response['message'] = 'Cet avoir ne peut pas être appliqué sur cette commande';
  219. $response['message'] .= '<ul>';
  220. foreach ($orderShop->reductionError as $error) {
  221. $response['message'] .= '<li> <i>'.$this->translator->trans($error, array(), 'lcshop'). '</i></li>';
  222. }
  223. $response['message'] .= '</ul>';
  224. }
  225. } else {
  226. $response['status'] = 'error';
  227. $response['message'] = 'Une erreur est survenue';
  228. }
  229. $response['data'] = $this->orderUtils->getOrderAsJsonObject($orderShop);;
  230. return new Response(json_encode($response));
  231. }
  232. public function renderOrderShopTemplate($actionName, $templatePath, array $parameters = [])
  233. {
  234. //dump($this->em->getRepository(OrderShop::class)->getValidOrder());
  235. if ($actionName == 'show') {
  236. $formAddProductToOrder = $this->createForm(AddPoductToOrderType::class, null, array(
  237. 'action' => $this->generateUrl('easyadmin', [
  238. 'action' => 'addProductToOrder',
  239. 'entity' => $this->entity['name'],
  240. 'id' => $parameters['entity']->getId()
  241. ])
  242. ));
  243. $formOrderProducts = $this->createForm(OrderProductsType::class, null, array(
  244. 'action' => $this->generateUrl('easyadmin', [
  245. 'action' => 'orderProducts',
  246. 'entity' => $this->entity['name'],
  247. 'id' => $parameters['entity']->getId()
  248. ])
  249. ));
  250. $formOrderInvoiceAddress = $this->createForm(OrderInvoiceAddressType::class, null, array(
  251. 'data' => $parameters['entity'],
  252. 'action' => $this->generateUrl('easyadmin', [
  253. 'action' => 'orderInvoiceAddress',
  254. 'entity' => $this->entity['name'],
  255. 'id' => $parameters['entity']->getId()
  256. ])
  257. ));
  258. $formOrderStatus = $this->createForm(OrderStatusType::class, null, array(
  259. 'data' => $parameters['entity'],
  260. 'action' => $this->generateUrl('easyadmin', [
  261. 'action' => 'orderStatus',
  262. 'entity' => $this->entity['name'],
  263. 'id' => $parameters['entity']->getId()
  264. ])
  265. ));
  266. $formOrderReductionCart = $this->createForm(OrderReductionCartType::class, null, array(
  267. 'data' => $parameters['entity'],
  268. 'action' => $this->generateUrl('easyadmin', [
  269. 'action' => 'orderReductionCart',
  270. 'entity' => $this->entity['name'],
  271. 'id' => $parameters['entity']->getId()
  272. ])
  273. ));
  274. $formOrderReductionCredit = $this->createForm(OrderReductionCreditType::class, null, array(
  275. 'data' => $parameters['entity'],
  276. 'action' => $this->generateUrl('easyadmin', [
  277. 'action' => 'orderReductionCredit',
  278. 'entity' => $this->entity['name'],
  279. 'id' => $parameters['entity']->getId()
  280. ])
  281. ));
  282. if(!isset($parameters['form_order_delivery_address'])) {
  283. $formOrderDeliveryAddress = $this->createForm(OrderDeliveryAddressType::class, null, array(
  284. 'data' => $parameters['entity'],
  285. 'action' => $this->generateUrl('easyadmin', [
  286. 'action' => 'orderDeliveryAddress',
  287. 'entity' => $this->entity['name'],
  288. 'id' => $parameters['entity']->getId()
  289. ])
  290. ));
  291. $parameters['form_order_delivery_address'] = $formOrderDeliveryAddress->createView();
  292. }
  293. $parameters['form_order_reduction_credit'] = $formOrderReductionCredit->createView();
  294. $parameters['form_order_reduction_cart'] = $formOrderReductionCart->createView();
  295. $parameters['form_add_product_to_order'] = $formAddProductToOrder->createView();
  296. $parameters['form_order_products'] = $formOrderProducts->createView();
  297. $parameters['form_order_invoice_address'] = $formOrderInvoiceAddress->createView();
  298. $parameters['form_order_status'] = $formOrderStatus->createView();
  299. }
  300. return parent::renderTemplate($actionName, $templatePath, $parameters);
  301. }
  302. protected function newAction()
  303. {
  304. $this->dispatch(EasyAdminEvents::PRE_NEW);
  305. $entity = $this->executeDynamicMethod('createNew<EntityName>Entity');
  306. $easyadmin = $this->request->attributes->get('easyadmin');
  307. $easyadmin['item'] = $entity;
  308. $this->request->attributes->set('easyadmin', $easyadmin);
  309. $fields = $this->entity['new']['fields'];
  310. $newForm = $this->executeDynamicMethod('create<EntityName>NewForm', [$entity, $fields]);
  311. $newForm->handleRequest($this->request);
  312. //ETAPE 1 Choix de l'utilisateur
  313. $user = $newForm->get('user')->getData();
  314. if ($user == null) {
  315. $user = $this->getUserViaFirstStepForm($entity);
  316. }
  317. if (!$user instanceof UserInterface) return $user;
  318. else {
  319. $orderShop = $this->orderUtils->createOrderShop(array(
  320. 'user' => $user,
  321. 'merchant' => $this->merchantUtils->getMerchantUser()
  322. ));
  323. return $this->redirectToRoute('easyadmin', [
  324. 'action' => 'edit',
  325. 'entity' => $this->entity['name'],
  326. 'id' => $orderShop->getId()
  327. ]);
  328. }
  329. }
  330. /**
  331. * The method that is executed when the user performs a 'show' action on an entity.
  332. *
  333. * @return Response
  334. */
  335. public function showAction()
  336. {
  337. $this->dispatch(EasyAdminEvents::PRE_SHOW);
  338. $id = $this->request->query->get('id');
  339. $easyadmin = $this->request->attributes->get('easyadmin');
  340. $entity = $easyadmin['item'];
  341. $fields = $this->entity['show']['fields'];
  342. $deleteForm = $this->createDeleteForm($this->entity['name'], $id);
  343. $this->dispatch(EasyAdminEvents::POST_SHOW, [
  344. 'deleteForm' => $deleteForm,
  345. 'fields' => $fields,
  346. 'entity' => $entity,
  347. ]);
  348. $parameters = [
  349. 'entity' => $entity,
  350. 'fields' => $fields,
  351. 'delete_form' => $deleteForm->createView(),
  352. 'order' => $this->orderUtils->getOrderAsJsonObject($entity)
  353. ];
  354. return $this->executeDynamicMethod('render<EntityName>Template', ['show', $this->entity['templates']['show'], $parameters]);
  355. }
  356. /**
  357. * Réécriture de edit action pr rediriger vers le show */
  358. public function editAction()
  359. {
  360. $id = $this->request->query->get('id');
  361. $entity = $this->request->query->get('entity');
  362. return $this->redirectToRoute('easyadmin', [
  363. 'action' => 'show',
  364. 'entity' => $entity,
  365. 'id' => $id
  366. ]);
  367. }
  368. }