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

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