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 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  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\Context\UnitInterface;
  12. use Lc\ShopBundle\Form\ProductFamilyCategoriesType;
  13. use Lc\ShopBundle\Form\ProductType;
  14. use Symfony\Bridge\Doctrine\Form\Type\EntityType;
  15. use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
  16. use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
  17. use Symfony\Component\Form\Extension\Core\Type\CollectionType;
  18. use Symfony\Component\Form\Extension\Core\Type\DateType;
  19. use Symfony\Component\Form\Extension\Core\Type\MoneyType;
  20. use Symfony\Component\Form\Extension\Core\Type\NumberType;
  21. use Symfony\Component\HttpFoundation\Response;
  22. class ProductFamilyController extends AdminController
  23. {
  24. private $taxRateClass;
  25. private $choicesTaxRateParam;
  26. private $choicesSupplierTaxRateParam;
  27. public function createEntityFormBuilder($entity, $view)
  28. {
  29. $formBuilder = parent::createEntityFormBuilder($entity, $view);
  30. $class = $this->em->getClassMetadata(ProductCategoryInterface::class);
  31. $this->taxRateClass = $this->em->getClassMetadata(TaxRateInterface::class);
  32. $formBuilder->add('productCategories', ProductFamilyCategoriesType::class);
  33. /*$formBuilder->add('taxRate', EntityType::class, array(
  34. 'class' => $this->em->getClassMetadata(TaxRateInterface::class)->name,
  35. 'required' =>false,
  36. 'placeholder'=> 'Tva par défaut ('.$this->getUser()->getMerchant()->getTaxRate()->getValue().'%)'
  37. ));*/
  38. $formBuilder->add('warningMessageType', ChoiceType::class, array(
  39. 'choices' => array(
  40. 'field.default.warningMessageTypeOptions.alert' => 'alert',
  41. 'field.default.warningMessageTypeOptions.info' => 'info'
  42. ),
  43. 'translation_domain' => 'lcshop',
  44. 'multiple' => false,
  45. 'expanded' => false,
  46. 'required' => false
  47. ));
  48. $formBuilder->add('behaviorCountStock', ChoiceType::class, array(
  49. 'empty_data' => 'by-product-family',
  50. 'choices' => array(
  51. 'field.ProductFamily.behaviorCountStockOptions.byQuantity' => 'by-quantity',
  52. 'field.ProductFamily.behaviorCountStockOptions.byProductFamily' => 'by-product-family',
  53. 'field.ProductFamily.behaviorCountStockOptions.byProduct' => 'by-product'
  54. ),
  55. 'translation_domain' => 'lcshop',
  56. 'multiple' => false,
  57. 'expanded' => true
  58. ));
  59. $formBuilder->add('propertyOrganicLabel', ChoiceType::class, array(
  60. 'choices' => array(
  61. 'field.ProductFamily.organicLabelOptions.ab' => 'ab',
  62. 'field.ProductFamily.organicLabelOptions.natureProgres' => 'nature-progres',
  63. 'field.ProductFamily.organicLabelOptions.hVE' => 'hve'
  64. ),
  65. 'translation_domain' => 'lcshop',
  66. 'multiple' => false,
  67. 'expanded' => false,
  68. 'required' => false
  69. ));
  70. $formBuilder->add('typeExpirationDate', ChoiceType::class, array(
  71. 'choices' => array(
  72. 'field.default.dlc' => 'dlc',
  73. 'field.default.ddm' => 'ddm',
  74. 'field.default.dluo' => 'dluo'
  75. ),
  76. 'translation_domain' => 'lcshop',
  77. 'multiple' => false,
  78. 'expanded' => true,
  79. 'required'=>false
  80. ));
  81. $formBuilder->add('behaviorExpirationDate', ChoiceType::class, array(
  82. 'choices' => array(
  83. 'field.ProductFamily.behaviorExpirationDateOptions.productFamily' => 'by-product-family',
  84. 'field.ProductFamily.behaviorExpirationDateOptions.product' => 'by-product'
  85. ),
  86. 'translation_domain' => 'lcshop',
  87. 'multiple' => false,
  88. 'expanded' => true,
  89. 'required'=>false
  90. ));
  91. $formBuilder->add('products', CollectionType::class, array(
  92. 'label' => 'Déclinaisons',
  93. 'entry_type' => ProductType::class,
  94. 'entry_options' => ['label' => false],
  95. 'allow_add' => true,
  96. 'allow_delete' => true,
  97. 'required' => true
  98. )
  99. );
  100. return $formBuilder;
  101. }
  102. public function updateEntity($entity)
  103. {
  104. $productFamilyRequest = $this->request->request->get('productfamily');
  105. $taxRateId = intval($productFamilyRequest['taxRate']);
  106. if ($taxRateId > 0) {
  107. $repo = $this->em->getRepository(TaxRateInterface::class);
  108. $entity->setTaxRate($repo->find($taxRateId));
  109. }
  110. dump($productFamilyRequest);
  111. $unitId = intval($productFamilyRequest['unit']);
  112. if ($unitId > 0) {
  113. $repo = $this->em->getRepository(UnitInterface::class);
  114. $entity->setUnit($repo->find($unitId));
  115. }
  116. $this->processCategories($entity);
  117. $this->processProducts($entity);
  118. parent::updateEntity($entity);
  119. }
  120. public function persistEntity($entity)
  121. {
  122. $this->processCategories($entity);
  123. $this->processProducts($entity);
  124. parent::persistEntity($entity);
  125. }
  126. protected function processProducts($entity)
  127. {
  128. //si il existe un et un seul produit pour ce product family n'ajoute rien supprime rien
  129. if (count($entity->getProducts()) == 0) {
  130. $product = new Product();
  131. $product->setCreatedBy($this->getUser());
  132. $product->setUpdatedBy($this->getUser());
  133. $product->setProductFamily($entity);
  134. $this->em->persist($product);
  135. $entity->addProduct($product);
  136. } else {
  137. foreach ($entity->getProducts() as $product) {
  138. $product->setProductFamily($entity);
  139. $product->setCreatedBy($this->getUser());
  140. $product->setUpdatedBy($this->getUser());
  141. $this->em->persist($product);
  142. $entity->addProduct($product);
  143. // die('ncici');
  144. }
  145. }
  146. }
  147. protected function processCategories(ProductFamilyInterface $entity)
  148. {
  149. $productCategoryRepository = $this->getDoctrine()->getRepository(ProductCategoryInterface::class);
  150. $productCategories = $entity->getProductCategories();
  151. $entity->initProductCategories();
  152. foreach ($productCategories as $key => $bool) {
  153. if (is_bool($bool) && $bool) {
  154. if (strpos($key, 'category_children_') !== false) {
  155. $idCategory = (int)str_replace('category_children_', '', $key);
  156. } else {
  157. $idCategory = (int)str_replace('category_', '', $key);
  158. }
  159. $category = $productCategoryRepository->find($idCategory);
  160. $entity->addProductCategory($category);
  161. }
  162. }
  163. }
  164. protected function editAction()
  165. {
  166. $this->dispatch(EasyAdminEvents::PRE_EDIT);
  167. $id = $this->request->query->get('id');
  168. $easyadmin = $this->request->attributes->get('easyadmin');
  169. $entity = $easyadmin['item'];
  170. if ($this->request->isXmlHttpRequest() && $property = $this->request->query->get('property')) {
  171. $newValue = 'true' === mb_strtolower($this->request->query->get('newValue'));
  172. $fieldsMetadata = $this->entity['list']['fields'];
  173. if (!isset($fieldsMetadata[$property]) || 'toggle' !== $fieldsMetadata[$property]['dataType']) {
  174. throw new \RuntimeException(sprintf('The type of the "%s" property is not "toggle".', $property));
  175. }
  176. $this->updateEntityProperty($entity, $property, $newValue);
  177. // cast to integer instead of string to avoid sending empty responses for 'false'
  178. return new Response((int)$newValue);
  179. }
  180. $fields = $this->entity['edit']['fields'];
  181. $editForm = $this->executeDynamicMethod('create<EntityName>EditForm', [$entity, $fields]);
  182. $deleteForm = $this->createDeleteForm($this->entity['name'], $id);
  183. $sortableProductsField = array();
  184. foreach ($editForm->get('products')->getData() as $k => $product) {
  185. $sortableProductsField[$product->getPosition()] = $k;
  186. }
  187. ksort($sortableProductsField);
  188. $editForm->handleRequest($this->request);
  189. if ($editForm->isSubmitted() && $editForm->isValid()) {
  190. $this->processUploadedFiles($editForm);
  191. $this->dispatch(EasyAdminEvents::PRE_UPDATE, ['entity' => $entity]);
  192. $this->executeDynamicMethod('update<EntityName>Entity', [$entity, $editForm]);
  193. $this->dispatch(EasyAdminEvents::POST_UPDATE, ['entity' => $entity]);
  194. return $this->redirectToReferrer();
  195. }
  196. $this->dispatch(EasyAdminEvents::POST_EDIT);
  197. $productCategoryRepository = $this->getDoctrine()->getRepository(ProductCategoryInterface::class);
  198. $categories = $productCategoryRepository->findAllParents();
  199. $parameters = [
  200. 'form' => $editForm->createView(),
  201. 'entity_fields' => $fields,
  202. 'entity' => $entity,
  203. 'delete_form' => $deleteForm->createView(),
  204. 'categories' => $categories,
  205. 'sortableProductsField' => $sortableProductsField
  206. ];
  207. return $this->executeDynamicMethod('render<EntityName>Template', ['edit', $this->entity['templates']['edit'], $parameters]);
  208. }
  209. protected function newAction()
  210. {
  211. $this->dispatch(EasyAdminEvents::PRE_NEW);
  212. $entity = $this->executeDynamicMethod('createNew<EntityName>Entity');
  213. $easyadmin = $this->request->attributes->get('easyadmin');
  214. $easyadmin['item'] = $entity;
  215. $this->request->attributes->set('easyadmin', $easyadmin);
  216. $fields = $this->entity['new']['fields'];
  217. $newForm = $this->executeDynamicMethod('create<EntityName>NewForm', [$entity, $fields]);
  218. $newForm->handleRequest($this->request);
  219. if ($newForm->isSubmitted() && $newForm->isValid()) {
  220. $this->processUploadedFiles($newForm);
  221. $this->dispatch(EasyAdminEvents::PRE_PERSIST, ['entity' => $entity]);
  222. $this->executeDynamicMethod('persist<EntityName>Entity', [$entity, $newForm]);
  223. $this->dispatch(EasyAdminEvents::POST_PERSIST, ['entity' => $entity]);
  224. return $this->redirectToReferrer();
  225. }
  226. $this->dispatch(EasyAdminEvents::POST_NEW, [
  227. 'entity_fields' => $fields,
  228. 'form' => $newForm,
  229. 'entity' => $entity
  230. ]);
  231. $productCategoryRepository = $this->getDoctrine()->getRepository(ProductCategoryInterface::class);
  232. $categories = $productCategoryRepository->findAllParents();
  233. $parameters = [
  234. 'form' => $newForm->createView(),
  235. 'entity_fields' => $fields,
  236. 'entity' => $entity,
  237. 'categories' => $categories,
  238. 'sortableProductsField' => array()
  239. ];
  240. return $this->executeDynamicMethod('render<EntityName>Template', ['new', $this->entity['templates']['new'], $parameters]);
  241. }
  242. }