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

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