Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

OrderController.php 14KB

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