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.

322 lines
14KB

  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. }