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.

339 line
16KB

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