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.

ProductFamilyController.php 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. <?php
  2. namespace Lc\ShopBundle\Controller\Admin;
  3. use App\Entity\Product;
  4. use Doctrine\DBAL\Types\FloatType;
  5. use Doctrine\ORM\EntityRepository;
  6. use EasyCorp\Bundle\EasyAdminBundle\Event\EasyAdminEvents;
  7. use Lc\ShopBundle\Context\ProductCategoryInterface;
  8. use Lc\ShopBundle\Context\ProductFamilyInterface;
  9. use Lc\ShopBundle\Context\ProductInterface;
  10. use Lc\ShopBundle\Context\TaxRateInterface;
  11. use Lc\ShopBundle\Form\ProductFamilyCategoriesType;
  12. use Lc\ShopBundle\Form\ProductType;
  13. use Symfony\Bridge\Doctrine\Form\Type\EntityType;
  14. use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
  15. use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
  16. use Symfony\Component\Form\Extension\Core\Type\CollectionType;
  17. use Symfony\Component\Form\Extension\Core\Type\MoneyType;
  18. use Symfony\Component\Form\Extension\Core\Type\NumberType;
  19. use Symfony\Component\HttpFoundation\Response;
  20. class ProductFamilyController extends AdminController
  21. {
  22. private $taxRateClass;
  23. private $choicesTaxRateParam;
  24. public function createEntityFormBuilder($entity, $view)
  25. {
  26. $formBuilder = parent::createEntityFormBuilder($entity, $view);
  27. $class = $this->em->getClassMetadata(ProductCategoryInterface::class);
  28. $this->taxRateClass = $this->em->getClassMetadata(TaxRateInterface::class);
  29. $formBuilder->add('productCategories', ProductFamilyCategoriesType::class) ;
  30. //CHOICE qui permet de sélectionner une taxe
  31. //ça c'est du lourd fais très attention faudra refactorer
  32. $this->getUser()->getMerchant()->getTaxRate();
  33. $choicesTaxRate['Valeur par défaut'] = 0;
  34. foreach ($this->em->getRepository($this->taxRateClass->name)->findAll() as $tax) {
  35. $choicesTaxRate[$tax->getTitle()] = $tax->getId();
  36. $this->choicesTaxRateParam[$tax->getId()] = $tax->getValue();
  37. }
  38. //là mon ami je kiffe symfo !!!!!
  39. $this->choicesTaxRateParam[0] = $this->getUser()->getMerchant()->getTaxRate()->getValue();
  40. $formBuilder->add('taxRate', ChoiceType::class, array(
  41. 'label' => 'TVA',
  42. 'choices' => $choicesTaxRate,
  43. 'mapped' => false,
  44. 'choice_attr' => function ($choice, $key, $value) {
  45. return ['data-tax-rate-value' => $this->choicesTaxRateParam[$choice]];
  46. },
  47. ));
  48. $formBuilder->add('unit', ChoiceType::class, array(
  49. 'label' => 'Unité',
  50. //''
  51. 'choices' => array(
  52. 'pièce' => 'piece',
  53. 'g' => 'g',
  54. 'kg' => 'kg',
  55. 'ml' => 'ml',
  56. 'L' => 'L'
  57. ),
  58. ));
  59. $formBuilder->add('weight', NumberType::class, array(
  60. 'label' => 'Poids',
  61. 'attr' => [
  62. 'append_html' => 'g'
  63. ]
  64. ));
  65. $formBuilder->add('step', NumberType::class, array(
  66. 'label' => 'Pas',
  67. 'help' => 'Quantité à incrémenter / décrémenter lors des mouvements de quantité',
  68. ));
  69. $formBuilder->add('price', NumberType::class, array(
  70. 'label' => 'Prix',
  71. ));
  72. $formBuilder->add('priceWithTax', NumberType::class, array(
  73. 'label' => 'Prix TTC',
  74. 'required' => false,
  75. 'mapped' => false
  76. ));
  77. $formBuilder->add('behaviorCountStock', ChoiceType::class, array(
  78. 'label' => 'Stock',
  79. 'choices' => array(
  80. 'Gèrer le stock par déclinaison' => 'by-product',
  81. 'Gèrer le stock par produit' => 'by-product-family'
  82. ),
  83. 'multiple' => false,
  84. 'expanded' => true
  85. ));
  86. $formBuilder->add('availableQuantity', NumberType::class, array(
  87. 'label' => 'Quantité disponible',
  88. 'required' => false,
  89. ));
  90. $formBuilder->add('products', CollectionType::class, array(
  91. 'label' => 'Déclinaisons',
  92. 'entry_type' => ProductType::class,
  93. 'entry_options' => ['label' => false],
  94. 'allow_add' => true,
  95. 'allow_delete' => true,
  96. 'required' => true
  97. )
  98. );
  99. return $formBuilder;
  100. }
  101. public function updateEntity($entity)
  102. {
  103. $prodductFamilyRequest = $this->request->request->get('productfamily');
  104. if ($taxRateId = intval($prodductFamilyRequest['taxRate']) > 0) {
  105. $repo = $this->em->getRepository(TaxRateInterface::class);
  106. $entity->setTaxRate($repo->find($taxRateId));
  107. }
  108. $this->processCategories($entity) ;
  109. $this->processProducts($entity) ;
  110. $this->setUpdated($entity);
  111. parent::updateEntity($entity);
  112. }
  113. public function persistEntity($entity)
  114. {
  115. $this->processCategories($entity) ;
  116. $this->processProducts($entity) ;
  117. parent::persistEntity($entity) ;
  118. }
  119. protected function processProducts($entity)
  120. {
  121. if(count($entity->getProducts()) == 0) {
  122. $product = new Product();
  123. $product->setTitle($entity->getTitle()) ;
  124. $product->setCreatedBy($this->getUser()) ;
  125. $product->setUpdatedBy($this->getUser()) ;
  126. $product->setProductFamily($entity) ;
  127. $this->em->persist($product);
  128. $entity->addProduct($product) ;
  129. }
  130. else {
  131. foreach($entity->getProducts() as $product) {
  132. $product->setProductFamily($entity) ;
  133. $product->setCreatedBy($this->getUser()) ;
  134. $product->setUpdatedBy($this->getUser()) ;
  135. }
  136. }
  137. }
  138. protected function processCategories(ProductFamilyInterface $entity)
  139. {
  140. $productCategoryRepository = $this->getDoctrine()->getRepository(ProductCategoryInterface::class) ;
  141. $productCategories = $entity->getProductCategories() ;
  142. $entity->initProductCategories() ;
  143. foreach($productCategories as $key => $bool) {
  144. if(is_bool($bool) && $bool) {
  145. if(strpos($key, 'category_children_') !== false) {
  146. $idCategory = (int) str_replace('category_children_', '', $key) ;
  147. }
  148. else {
  149. $idCategory = (int) str_replace('category_', '', $key) ;
  150. }
  151. $category = $productCategoryRepository->find($idCategory) ;
  152. $entity->addProductCategory($category) ;
  153. }
  154. }
  155. }
  156. protected function editAction()
  157. {
  158. $this->dispatch(EasyAdminEvents::PRE_EDIT);
  159. $id = $this->request->query->get('id');
  160. $easyadmin = $this->request->attributes->get('easyadmin');
  161. $entity = $easyadmin['item'];
  162. if ($this->request->isXmlHttpRequest() && $property = $this->request->query->get('property')) {
  163. $newValue = 'true' === mb_strtolower($this->request->query->get('newValue'));
  164. $fieldsMetadata = $this->entity['list']['fields'];
  165. if (!isset($fieldsMetadata[$property]) || 'toggle' !== $fieldsMetadata[$property]['dataType']) {
  166. throw new \RuntimeException(sprintf('The type of the "%s" property is not "toggle".', $property));
  167. }
  168. $this->updateEntityProperty($entity, $property, $newValue);
  169. // cast to integer instead of string to avoid sending empty responses for 'false'
  170. return new Response((int) $newValue);
  171. }
  172. $fields = $this->entity['edit']['fields'];
  173. $editForm = $this->executeDynamicMethod('create<EntityName>EditForm', [$entity, $fields]);
  174. $deleteForm = $this->createDeleteForm($this->entity['name'], $id);
  175. $editForm->handleRequest($this->request);
  176. if ($editForm->isSubmitted() && $editForm->isValid()) {
  177. $this->processUploadedFiles($editForm);
  178. $this->dispatch(EasyAdminEvents::PRE_UPDATE, ['entity' => $entity]);
  179. $this->executeDynamicMethod('update<EntityName>Entity', [$entity, $editForm]);
  180. $this->dispatch(EasyAdminEvents::POST_UPDATE, ['entity' => $entity]);
  181. return $this->redirectToReferrer();
  182. }
  183. $this->dispatch(EasyAdminEvents::POST_EDIT);
  184. $productCategoryRepository = $this->getDoctrine()->getRepository(ProductCategoryInterface::class);
  185. $categories = $productCategoryRepository->findAllParents();
  186. $parameters = [
  187. 'form' => $editForm->createView(),
  188. 'entity_fields' => $fields,
  189. 'entity' => $entity,
  190. 'delete_form' => $deleteForm->createView(),
  191. 'categories' => $categories
  192. ];
  193. return $this->executeDynamicMethod('render<EntityName>Template', ['edit', $this->entity['templates']['edit'], $parameters]);
  194. }
  195. protected function newAction()
  196. {
  197. $this->dispatch(EasyAdminEvents::PRE_NEW);
  198. $entity = $this->executeDynamicMethod('createNew<EntityName>Entity');
  199. $easyadmin = $this->request->attributes->get('easyadmin');
  200. $easyadmin['item'] = $entity;
  201. $this->request->attributes->set('easyadmin', $easyadmin);
  202. $fields = $this->entity['new']['fields'];
  203. $newForm = $this->executeDynamicMethod('create<EntityName>NewForm', [$entity, $fields]);
  204. $newForm->handleRequest($this->request);
  205. if ($newForm->isSubmitted() && $newForm->isValid()) {
  206. $this->processUploadedFiles($newForm);
  207. $this->dispatch(EasyAdminEvents::PRE_PERSIST, ['entity' => $entity]);
  208. $this->executeDynamicMethod('persist<EntityName>Entity', [$entity, $newForm]);
  209. $this->dispatch(EasyAdminEvents::POST_PERSIST, ['entity' => $entity]);
  210. return $this->redirectToReferrer();
  211. }
  212. $this->dispatch(EasyAdminEvents::POST_NEW, [
  213. 'entity_fields' => $fields,
  214. 'form' => $newForm,
  215. 'entity' => $entity,
  216. ]);
  217. $productCategoryRepository = $this->getDoctrine()->getRepository(ProductCategoryInterface::class);
  218. $categories = $productCategoryRepository->findAllParents();
  219. $parameters = [
  220. 'form' => $newForm->createView(),
  221. 'entity_fields' => $fields,
  222. 'entity' => $entity,
  223. 'categories' => $categories,
  224. ];
  225. return $this->executeDynamicMethod('render<EntityName>Template', ['new', $this->entity['templates']['new'], $parameters]);
  226. }
  227. }