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.

преди 4 години
преди 4 години
преди 4 години
преди 4 години
преди 4 години
преди 4 години
преди 4 години
преди 4 години
преди 4 години
преди 4 години
преди 4 години
преди 4 години
преди 4 години
преди 4 години
преди 4 години
преди 4 години
преди 4 години
преди 4 години
преди 4 години
преди 4 години
преди 4 години
преди 4 години
преди 4 години
преди 4 години
преди 4 години
преди 4 години
преди 4 години
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. <?php
  2. namespace Lc\ShopBundle\Controller\Backend;
  3. use Doctrine\ORM\EntityRepository;
  4. use EasyCorp\Bundle\EasyAdminBundle\Event\EasyAdminEvents;
  5. use Lc\ShopBundle\Context\AddressInterface;
  6. use Lc\ShopBundle\Context\OrderProductInterface;
  7. use Lc\ShopBundle\Context\UserInterface;
  8. use Lc\ShopBundle\Form\Order\AddPoductToOrderType;
  9. use Lc\ShopBundle\Form\Order\OrderAddressType;
  10. use Lc\ShopBundle\Form\Order\OrderDeliveryAddressType;
  11. use Lc\ShopBundle\Form\Order\OrderInvoiceAddressType;
  12. use Lc\ShopBundle\Form\Order\OrderProductsType;
  13. use Lc\ShopBundle\Form\OrderProductType;
  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. /*protected $orderUtils;
  20. public function __construct(Security $security, UserManagerInterface $userManager, EntityManagerInterface $em, Utils $utils, MerchantUtilsInterface $merchantUtils, MailjetTransport $mailjetTransport, OrderUtilsInterface $orderUtils)
  21. {
  22. $this->orderUtils = $orderUtils;
  23. parent::__construct($security, $userManager, $em, $utils, $merchantUtils, $mailjetTransport);
  24. }*/
  25. public function updateEntity($entity)
  26. {
  27. foreach ($entity->getOrderProducts() as $orderProduct) {
  28. //dump($orderProduct);
  29. $orderProduct->setCreatedBy($this->getUser());
  30. $orderProduct->setUpdatedBy($this->getUser());
  31. $orderProduct->setTaxRate($orderProduct->getProduct()->getProductFamily()->getTaxRate());
  32. $orderProduct->setOrderShop($entity);
  33. $this->em->persist($orderProduct);
  34. $entity->addOrderProduct($orderProduct);
  35. }
  36. $this->setUpdated($entity);
  37. parent::updateEntity($entity);
  38. }
  39. public function persistEntity($entity)
  40. {
  41. foreach ($entity->getOrderProducts() as $orderProduct) {
  42. $orderProduct->setUnit($orderProduct->getProduct()->getUnitInherited());
  43. $orderProduct->setTitle($orderProduct->getProduct()->getTitleInherited());
  44. $orderProduct->setPrice($orderProduct->getProduct()->getPriceInherited());
  45. if ($orderProduct->getProduct()->getProductFamily()->getTaxRate()) {
  46. $orderProduct->setTaxRate($orderProduct->getProduct()->getProductFamily()->getTaxRate());
  47. } else {
  48. $orderProduct->setTaxRate($this->security->getUser()->getMerchant()->getTaxRate());
  49. }
  50. $orderProduct->setOrderShop($entity);
  51. $orderProduct->setCreatedBy($this->getUser());
  52. $orderProduct->setUpdatedBy($this->getUser());
  53. $this->em->persist($orderProduct);
  54. $entity->addOrderProduct($orderProduct);
  55. }
  56. parent::persistEntity($entity);
  57. }
  58. public function getUserViaFirstStepForm($entity)
  59. {
  60. $userClass = $this->em->getClassMetadata(UserInterface::class);
  61. $userChoiceForm = $this->createFormBuilder($entity)
  62. ->add('user', EntityType::class, array(
  63. 'class' => $userClass->name
  64. ))
  65. ->add('nextStep', SubmitType::class)
  66. ->getForm();
  67. $userChoiceForm->handleRequest($this->request);
  68. if ($userChoiceForm->isSubmitted() && $userChoiceForm->isValid()) {
  69. return $userChoiceForm->get('user')->getData();
  70. }
  71. $parameters = [
  72. 'form' => $userChoiceForm->createView(),
  73. 'formView' => 'default',
  74. 'entity' => $entity,
  75. ];
  76. return $this->executeDynamicMethod('render<EntityName>Template', ['new', $this->entity['templates']['new'], $parameters]);
  77. }
  78. public function addProductToOrderAction()
  79. {
  80. $id = $this->request->query->get('id');
  81. $easyadmin = $this->request->attributes->get('easyadmin');
  82. $orderShop = $easyadmin['item'];
  83. $orderProductClass = $this->em->getClassMetadata(OrderProductInterface::class);
  84. $formAddProductToOrder = $this->createForm(AddPoductToOrderType::class);
  85. $formAddProductToOrder->handleRequest($this->request);
  86. if ($formAddProductToOrder->get('product')->getData() == null) {
  87. $response['status'] = 'error';
  88. $response['message'] = 'Vous devez choisir un produit dans la liste';
  89. } else if ($formAddProductToOrder->get('quantity')->getData() == null) {
  90. $response['status'] = 'error';
  91. $response['message'] = 'Vous devez entrer une quantité';
  92. } else if ($formAddProductToOrder->isSubmitted() && $formAddProductToOrder->isValid()) {
  93. $orderProduct = new $orderProductClass->name;
  94. $orderProduct->setQuantityOrder($formAddProductToOrder->get('quantity')->getData());
  95. $orderProduct->setProduct($formAddProductToOrder->get('product')->getData());
  96. $this->orderUtils->addOrderProduct($orderShop, $orderProduct);
  97. $response['status'] = 'success';
  98. $response['message'] = 'Le produit a bien été ajouté à la commande';
  99. } else {
  100. $response['status'] = 'error';
  101. $response['message'] = 'Une erreur est survenue';
  102. }
  103. $response['data'] = $this->orderUtils->getOrderAsJsonObject($orderShop);;
  104. return new Response(json_encode($response));
  105. }
  106. public function orderInvoiceAddressAction()
  107. {
  108. $id = $this->request->query->get('id');
  109. $easyadmin = $this->request->attributes->get('easyadmin');
  110. $orderShop = $easyadmin['item'];
  111. $formOrderInvoiceAddress = $this->createForm(OrderInvoiceAddressType::class, $orderShop);
  112. $formOrderInvoiceAddress->handleRequest($this->request);
  113. if ($formOrderInvoiceAddress->isSubmitted() && $formOrderInvoiceAddress->isValid()) {
  114. $this->em->persist($orderShop);
  115. $this->em->flush();
  116. $response['status'] = 'success';
  117. $response['message'] = 'La commande a bien été modifié';
  118. } else {
  119. $response['status'] = 'error';
  120. $response['message'] = 'Une erreur est survenue';
  121. }
  122. $response['data'] = $this->orderUtils->getOrderAsJsonObject($orderShop);;
  123. return new Response(json_encode($response));
  124. }
  125. public function orderProductsAction()
  126. {
  127. $id = $this->request->query->get('id');
  128. $easyadmin = $this->request->attributes->get('easyadmin');
  129. $orderShop = $easyadmin['item'];
  130. $formOrderProducts = $this->createForm(OrderProductsType::class, $orderShop);
  131. $formOrderProducts->handleRequest($this->request);
  132. if ($formOrderProducts->isSubmitted() && $formOrderProducts->isValid()) {
  133. // dump($formOrderProducts->get('orderProducts')->getData());
  134. foreach ($orderShop->getOrderProducts() as $orderProduct) {
  135. if ($orderProduct->getQuantityOrder() <= 0) {
  136. $response['niche'] = $orderProduct->getQuantityOrder();
  137. $this->em->remove($orderProduct);
  138. } else {
  139. $this->em->persist($orderProduct);
  140. }
  141. }
  142. $this->em->flush();
  143. $response['status'] = 'success';
  144. $response['message'] = 'La commande a bien été modifié';
  145. } else {
  146. $response['status'] = 'error';
  147. $response['message'] = 'Une erreur est survenue';
  148. }
  149. $response['data'] = $this->orderUtils->getOrderAsJsonObject($orderShop);;
  150. return new Response(json_encode($response));
  151. }
  152. public function renderOrderShopTemplate($actionName, $templatePath, array $parameters = [])
  153. {
  154. if ($actionName == 'show') {
  155. $formAddProductToOrder = $this->createForm(AddPoductToOrderType::class, null, array(
  156. 'action' => $this->generateUrl('easyadmin', [
  157. 'action' => 'addProductToOrder',
  158. 'entity' => $this->entity['name'],
  159. 'id' => $parameters['entity']->getId()
  160. ])
  161. ));
  162. $formOrderProducts = $this->createForm(OrderProductsType::class, null, array(
  163. 'action' => $this->generateUrl('easyadmin', [
  164. 'action' => 'orderProducts',
  165. 'entity' => $this->entity['name'],
  166. 'id' => $parameters['entity']->getId()
  167. ])
  168. ));
  169. $formOrderInvoiceAddress = $this->createForm(OrderInvoiceAddressType::class, null, array(
  170. 'data' => $parameters['entity'],
  171. 'action' => $this->generateUrl('easyadmin', [
  172. 'action' => 'orderInvoiceAddress',
  173. 'entity' => $this->entity['name'],
  174. 'id' => $parameters['entity']->getId()
  175. ])
  176. ));
  177. if(!isset($parameters['form_order_delivery_address'])) {
  178. $formOrderDeliveryAddress = $this->createForm(OrderDeliveryAddressType::class, null, array(
  179. 'data' => $parameters['entity'],
  180. 'action' => $this->generateUrl('easyadmin', [
  181. 'action' => 'orderDeliveryAddress',
  182. 'entity' => $this->entity['name'],
  183. 'id' => $parameters['entity']->getId()
  184. ])
  185. ));
  186. $parameters['form_order_delivery_address'] = $formOrderDeliveryAddress->createView();
  187. }
  188. $parameters['form_add_product_to_order'] = $formAddProductToOrder->createView();
  189. $parameters['form_order_products'] = $formOrderProducts->createView();
  190. $parameters['form_order_invoice_address'] = $formOrderInvoiceAddress->createView();
  191. }
  192. return parent::renderTemplate($actionName, $templatePath, $parameters);
  193. }
  194. protected function newAction()
  195. {
  196. $this->dispatch(EasyAdminEvents::PRE_NEW);
  197. $entity = $this->executeDynamicMethod('createNew<EntityName>Entity');
  198. $easyadmin = $this->request->attributes->get('easyadmin');
  199. $easyadmin['item'] = $entity;
  200. $this->request->attributes->set('easyadmin', $easyadmin);
  201. $fields = $this->entity['new']['fields'];
  202. $newForm = $this->executeDynamicMethod('create<EntityName>NewForm', [$entity, $fields]);
  203. $newForm->handleRequest($this->request);
  204. //ETAPE 1 Choix de l'utilisateur
  205. $user = $newForm->get('user')->getData();
  206. if ($user == null) {
  207. $user = $this->getUserViaFirstStepForm($entity);
  208. }
  209. if (!$user instanceof UserInterface) return $user;
  210. else {
  211. $orderShop = $this->orderUtils->createOrderShop(array(
  212. 'user' => $user,
  213. 'merchant' => $this->merchantUtils->getMerchantUser()
  214. ));
  215. return $this->redirectToRoute('easyadmin', [
  216. 'action' => 'edit',
  217. 'entity' => $this->entity['name'],
  218. 'id' => $orderShop->getId()
  219. ]);
  220. }
  221. }
  222. /**
  223. * The method that is executed when the user performs a 'show' action on an entity.
  224. *
  225. * @return Response
  226. */
  227. public function showAction()
  228. {
  229. $this->dispatch(EasyAdminEvents::PRE_SHOW);
  230. $id = $this->request->query->get('id');
  231. $easyadmin = $this->request->attributes->get('easyadmin');
  232. $entity = $easyadmin['item'];
  233. $fields = $this->entity['show']['fields'];
  234. $deleteForm = $this->createDeleteForm($this->entity['name'], $id);
  235. $this->dispatch(EasyAdminEvents::POST_SHOW, [
  236. 'deleteForm' => $deleteForm,
  237. 'fields' => $fields,
  238. 'entity' => $entity,
  239. ]);
  240. $parameters = [
  241. 'entity' => $entity,
  242. 'fields' => $fields,
  243. 'delete_form' => $deleteForm->createView(),
  244. 'order' => $this->orderUtils->getOrderAsJsonObject($entity)
  245. ];
  246. return $this->executeDynamicMethod('render<EntityName>Template', ['show', $this->entity['templates']['show'], $parameters]);
  247. }
  248. /**
  249. * Réécriture de edit action pr rediriger vers le show */
  250. public function editAction()
  251. {
  252. $id = $this->request->query->get('id');
  253. $entity = $this->request->query->get('entity');
  254. return $this->redirectToRoute('easyadmin', [
  255. 'action' => 'show',
  256. 'entity' => $entity,
  257. 'id' => $id
  258. ]);
  259. }
  260. }