Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

ProductFamilyController.php 15KB

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