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.

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