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

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