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

4 yıl önce
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  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. 'data' => $entity->getBehaviorAddToCart() ? $entity->getBehaviorAddToCart() : 'simple',
  65. 'choices' => array(
  66. 'field.ProductFamily.behaviorAddToCartOptions.simple' => 'simple',
  67. 'field.ProductFamily.behaviorAddToCartOptions.multiple' => 'multiple'
  68. ),
  69. 'translation_domain' => 'lcshop',
  70. 'multiple' => false,
  71. 'expanded' => true
  72. ));
  73. $formBuilder->add('behaviorPrice', ChoiceType::class, array(
  74. 'empty_data' => 'by-piece',
  75. 'choices' => array(
  76. 'field.ProductFamily.behaviorPriceOptions.byPiece' => 'by-piece',
  77. 'field.ProductFamily.behaviorPriceOptions.byRefUnit' => 'by-reference-unit'
  78. ),
  79. 'translation_domain' => 'lcshop',
  80. 'multiple' => false,
  81. 'expanded' => true
  82. ));
  83. $formBuilder->add('multiplyingFactor', NumberType::class, array(
  84. 'mapped'=> false,
  85. 'data'=> floatval($this->merchantUtils->getMerchantConfig('multiplying-factor')),
  86. ));
  87. $formBuilder->add('propertyOrganicLabel', ChoiceType::class, array(
  88. 'choices' => array(
  89. 'field.ProductFamily.organicLabelOptions.ab' => 'ab',
  90. 'field.ProductFamily.organicLabelOptions.natureProgres' => 'nature-progres',
  91. 'field.ProductFamily.organicLabelOptions.hVE' => 'hve'
  92. ),
  93. 'translation_domain' => 'lcshop',
  94. 'multiple' => false,
  95. 'expanded' => false,
  96. 'required' => false
  97. ));
  98. $formBuilder->add('typeExpirationDate', ChoiceType::class, array(
  99. 'choices' => array(
  100. 'field.default.dlc' => 'dlc',
  101. 'field.default.ddm' => 'ddm',
  102. 'field.default.dluo' => 'dluo'
  103. ),
  104. 'translation_domain' => 'lcshop',
  105. 'multiple' => false,
  106. 'expanded' => true,
  107. 'required'=>false
  108. ));
  109. $formBuilder->add('behaviorExpirationDate', ChoiceType::class, array(
  110. 'choices' => array(
  111. 'field.ProductFamily.behaviorExpirationDateOptions.productFamily' => 'by-product-family',
  112. 'field.ProductFamily.behaviorExpirationDateOptions.product' => 'by-product'
  113. ),
  114. 'translation_domain' => 'lcshop',
  115. 'multiple' => false,
  116. 'expanded' => true,
  117. 'required'=>false
  118. ));
  119. $formBuilder->add('products', CollectionType::class, array(
  120. 'label' => 'Déclinaisons',
  121. 'entry_type' => ProductType::class,
  122. 'entry_options' => ['label' => false],
  123. 'allow_add' => true,
  124. 'allow_delete' => true,
  125. 'required' => true
  126. )
  127. );
  128. return $formBuilder;
  129. }
  130. public function updateEntity($entity)
  131. {
  132. $productFamilyRequest = $this->request->request->get('productfamily');
  133. /*$unitId = intval($productFamilyRequest['unit']);
  134. if ($unitId > 0) {
  135. $repo = $this->em->getRepository(UnitInterface::class);
  136. $entity->setUnit($repo->find($unitId));
  137. }*/
  138. //$reductionCatalogInfo = $productFamilyRequest['reductionCatalog'];
  139. $this->processCategories($entity);
  140. $this->processProducts($entity);
  141. $this->processPrice($entity);
  142. /* dump($reductionCatalog);
  143. dump($productFamilyRequest);*/
  144. parent::updateEntity($entity);
  145. }
  146. public function persistEntity($entity)
  147. {
  148. $this->processCategories($entity);
  149. $this->processProducts($entity);
  150. parent::persistEntity($entity);
  151. }
  152. protected function processPrice($entity){
  153. if($entity->getBehaviorPrice()=='by-piece'){
  154. $entity->setPriceByRefUnit(null);
  155. $entity->setBuyingPriceByRefUnit(null);
  156. }else if($entity->getBehaviorPrice()=='by-reference-unit') {
  157. $entity->setPrice(null);
  158. $entity->setBuyingPrice(null);
  159. }
  160. }
  161. protected function processProducts($entity)
  162. {
  163. //si il existe un et un seul produit pour ce product family n'ajoute rien supprime rien
  164. if (count($entity->getProducts()) == 0) {
  165. $product = new Product();
  166. $product->setProductFamily($entity);
  167. $this->em->persist($product);
  168. $entity->addProduct($product);
  169. } else {
  170. foreach ($entity->getProducts() as $i=>$product) {
  171. $product->setProductFamily($entity);
  172. $this->em->persist($product);
  173. $entity->addProduct($product);
  174. }
  175. }
  176. }
  177. protected function processCategories(ProductFamilyInterface $entity)
  178. {
  179. $productCategoryRepository = $this->getDoctrine()->getRepository(ProductCategoryInterface::class);
  180. $productCategories = $entity->getProductCategories();
  181. $entity->initProductCategories();
  182. foreach ($productCategories as $key => $bool) {
  183. if (is_bool($bool) && $bool) {
  184. if (strpos($key, 'category_children_') !== false) {
  185. $idCategory = (int)str_replace('category_children_', '', $key);
  186. } else {
  187. $idCategory = (int)str_replace('category_', '', $key);
  188. }
  189. $category = $productCategoryRepository->find($idCategory);
  190. $entity->addProductCategory($category);
  191. }
  192. }
  193. }
  194. protected function editAction()
  195. {
  196. $this->dispatch(EasyAdminEvents::PRE_EDIT);
  197. $id = $this->request->query->get('id');
  198. $easyadmin = $this->request->attributes->get('easyadmin');
  199. $entity = $easyadmin['item'];
  200. if ($this->request->isXmlHttpRequest() && $property = $this->request->query->get('property')) {
  201. $newValue = 'true' === mb_strtolower($this->request->query->get('newValue'));
  202. $fieldsMetadata = $this->entity['list']['fields'];
  203. if (!isset($fieldsMetadata[$property]) || 'toggle' !== $fieldsMetadata[$property]['dataType']) {
  204. throw new \RuntimeException(sprintf('The type of the "%s" property is not "toggle".', $property));
  205. }
  206. $this->updateEntityProperty($entity, $property, $newValue);
  207. // cast to integer instead of string to avoid sending empty responses for 'false'
  208. return new Response((int)$newValue);
  209. }
  210. $fields = $this->entity['edit']['fields'];
  211. $editForm = $this->executeDynamicMethod('create<EntityName>EditForm', [$entity, $fields]);
  212. $deleteForm = $this->createDeleteForm($this->entity['name'], $id);
  213. $sortableProductsField = array();
  214. foreach ($editForm->get('products')->getData() as $k => $product) {
  215. $sortableProductsField[$product->getPosition()] = $k;
  216. }
  217. ksort($sortableProductsField);
  218. $editForm->handleRequest($this->request);
  219. if ($editForm->isSubmitted() && $editForm->isValid()) {
  220. $this->processUploadedFiles($editForm);
  221. $this->dispatch(EasyAdminEvents::PRE_UPDATE, ['entity' => $entity]);
  222. $this->executeDynamicMethod('update<EntityName>Entity', [$entity, $editForm]);
  223. $this->dispatch(EasyAdminEvents::POST_UPDATE, ['entity' => $entity]);
  224. return $this->redirectToReferrer();
  225. }
  226. $this->dispatch(EasyAdminEvents::POST_EDIT);
  227. $parameters = [
  228. 'form' => $editForm->createView(),
  229. 'entity_fields' => $fields,
  230. 'entity' => $entity,
  231. 'delete_form' => $deleteForm->createView(),
  232. 'sortableProductsField' => $sortableProductsField
  233. ];
  234. return $this->executeDynamicMethod('render<EntityName>Template', ['edit', $this->entity['templates']['edit'], $parameters]);
  235. }
  236. protected function newAction()
  237. {
  238. $this->dispatch(EasyAdminEvents::PRE_NEW);
  239. $entity = $this->executeDynamicMethod('createNew<EntityName>Entity');
  240. $easyadmin = $this->request->attributes->get('easyadmin');
  241. $easyadmin['item'] = $entity;
  242. $this->request->attributes->set('easyadmin', $easyadmin);
  243. $fields = $this->entity['new']['fields'];
  244. $newForm = $this->executeDynamicMethod('create<EntityName>NewForm', [$entity, $fields]);
  245. $newForm->handleRequest($this->request);
  246. if ($newForm->isSubmitted() && $newForm->isValid()) {
  247. $this->processUploadedFiles($newForm);
  248. $this->dispatch(EasyAdminEvents::PRE_PERSIST, ['entity' => $entity]);
  249. $this->executeDynamicMethod('persist<EntityName>Entity', [$entity, $newForm]);
  250. $this->dispatch(EasyAdminEvents::POST_PERSIST, ['entity' => $entity]);
  251. return $this->redirectToReferrer();
  252. }
  253. $this->dispatch(EasyAdminEvents::POST_NEW, [
  254. 'entity_fields' => $fields,
  255. 'form' => $newForm,
  256. 'entity' => $entity
  257. ]);
  258. $productCategoryRepository = $this->getDoctrine()->getRepository(ProductCategoryInterface::class);
  259. $categories = $productCategoryRepository->findAllParents();
  260. $parameters = [
  261. 'form' => $newForm->createView(),
  262. 'entity_fields' => $fields,
  263. 'entity' => $entity,
  264. 'categories' => $categories,
  265. 'sortableProductsField' => array()
  266. ];
  267. return $this->executeDynamicMethod('render<EntityName>Template', ['new', $this->entity['templates']['new'], $parameters]);
  268. }
  269. }