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.

341 lines
15KB

  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. 'empty_data' => 'by-product-family',
  65. 'choices' => array(
  66. 'field.ProductFamily.behaviorAddToCartOptions.simple' => 'simple',
  67. 'field.ProductFamily.behaviorAddToCartOptions.multiple' => 'multiple'
  68. ),
  69. 'translation_domain' => 'lcshop',
  70. 'multiple' => false,
  71. 'required'=>false,
  72. 'expanded' => true
  73. ));
  74. $formBuilder->add('behaviorPrice', ChoiceType::class, array(
  75. 'empty_data' => 'by-piece',
  76. 'choices' => array(
  77. 'field.ProductFamily.behaviorPriceOptions.byPiece' => 'by-piece',
  78. 'field.ProductFamily.behaviorPriceOptions.byRefUnit' => 'by-reference-unit'
  79. ),
  80. 'translation_domain' => 'lcshop',
  81. 'multiple' => false,
  82. 'expanded' => true
  83. ));
  84. dump(floatval( $this->merchantUtils->getMerchantConfig('multiplying-factor')));
  85. $formBuilder->add('multiplyingFactor', NumberType::class, array(
  86. 'mapped'=> false,
  87. 'data'=> floatval($this->merchantUtils->getMerchantConfig('multiplying-factor')),
  88. ));
  89. $formBuilder->add('propertyOrganicLabel', ChoiceType::class, array(
  90. 'choices' => array(
  91. 'field.ProductFamily.organicLabelOptions.ab' => 'ab',
  92. 'field.ProductFamily.organicLabelOptions.natureProgres' => 'nature-progres',
  93. 'field.ProductFamily.organicLabelOptions.hVE' => 'hve'
  94. ),
  95. 'translation_domain' => 'lcshop',
  96. 'multiple' => false,
  97. 'expanded' => false,
  98. 'required' => false
  99. ));
  100. $formBuilder->add('typeExpirationDate', ChoiceType::class, array(
  101. 'choices' => array(
  102. 'field.default.dlc' => 'dlc',
  103. 'field.default.ddm' => 'ddm',
  104. 'field.default.dluo' => 'dluo'
  105. ),
  106. 'translation_domain' => 'lcshop',
  107. 'multiple' => false,
  108. 'expanded' => true,
  109. 'required'=>false
  110. ));
  111. $formBuilder->add('behaviorExpirationDate', ChoiceType::class, array(
  112. 'choices' => array(
  113. 'field.ProductFamily.behaviorExpirationDateOptions.productFamily' => 'by-product-family',
  114. 'field.ProductFamily.behaviorExpirationDateOptions.product' => 'by-product'
  115. ),
  116. 'translation_domain' => 'lcshop',
  117. 'multiple' => false,
  118. 'expanded' => true,
  119. 'required'=>false
  120. ));
  121. $formBuilder->add('products', CollectionType::class, array(
  122. 'label' => 'Déclinaisons',
  123. 'entry_type' => ProductType::class,
  124. 'entry_options' => ['label' => false],
  125. 'allow_add' => true,
  126. 'allow_delete' => true,
  127. 'required' => true
  128. )
  129. );
  130. return $formBuilder;
  131. }
  132. public function updateEntity($entity)
  133. {
  134. $productFamilyRequest = $this->request->request->get('productfamily');
  135. if(isset($productFamilyRequest['taxRate'])) {
  136. $taxRateId = intval($productFamilyRequest['taxRate']);
  137. if ($taxRateId > 0) {
  138. $repo = $this->em->getRepository(TaxRateInterface::class);
  139. $entity->setTaxRate($repo->find($taxRateId));
  140. }
  141. }
  142. $unitId = intval($productFamilyRequest['unit']);
  143. if ($unitId > 0) {
  144. $repo = $this->em->getRepository(UnitInterface::class);
  145. $entity->setUnit($repo->find($unitId));
  146. }
  147. //$reductionCatalogInfo = $productFamilyRequest['reductionCatalog'];
  148. $this->processCategories($entity);
  149. $this->processProducts($entity);
  150. /* dump($reductionCatalog);
  151. dump($productFamilyRequest);*/
  152. parent::updateEntity($entity);
  153. }
  154. public function persistEntity($entity)
  155. {
  156. $this->processCategories($entity);
  157. $this->processProducts($entity);
  158. parent::persistEntity($entity);
  159. }
  160. protected function processProducts($entity)
  161. {
  162. $repo = $this->em->getRepository(UnitInterface::class);
  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. // die('ncici');
  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. }