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.

328 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 Symfony\Bridge\Doctrine\Form\Type\EntityType;
  15. use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
  16. use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
  17. use Symfony\Component\Form\Extension\Core\Type\CollectionType;
  18. use Symfony\Component\Form\Extension\Core\Type\DateType;
  19. use Symfony\Component\Form\Extension\Core\Type\MoneyType;
  20. use Symfony\Component\Form\Extension\Core\Type\NumberType;
  21. use Symfony\Component\HttpFoundation\Response;
  22. class ProductFamilyController extends AdminController
  23. {
  24. private $taxRateClass;
  25. private $choicesTaxRateParam;
  26. private $choicesSupplierTaxRateParam;
  27. public function createEntityFormBuilder($entity, $view)
  28. {
  29. $formBuilder = parent::createEntityFormBuilder($entity, $view);
  30. $class = $this->em->getClassMetadata(ProductCategoryInterface::class);
  31. $this->taxRateClass = $this->em->getClassMetadata(TaxRateInterface::class);
  32. $formBuilder->add('productCategories', ProductFamilyCategoriesType::class);
  33. /*$formBuilder->add('taxRate', EntityType::class, array(
  34. 'class' => $this->em->getClassMetadata(TaxRateInterface::class)->name,
  35. 'required' =>false,
  36. 'placeholder'=> 'Tva par défaut ('.$this->getUser()->getMerchant()->getTaxRate()->getValue().'%)'
  37. ));*/
  38. $formBuilder->add('warningMessageType', ChoiceType::class, array(
  39. 'choices' => array(
  40. 'field.default.warningMessageTypeOptions.success' => 'success',
  41. 'field.default.warningMessageTypeOptions.error' => 'error',
  42. 'field.default.warningMessageTypeOptions.warning' => 'warning',
  43. 'field.default.warningMessageTypeOptions.info' => 'info'
  44. ),
  45. 'translation_domain' => 'lcshop',
  46. 'multiple' => false,
  47. 'expanded' => false,
  48. 'required' => false
  49. ));
  50. $formBuilder->add('behaviorCountStock', ChoiceType::class, array(
  51. 'empty_data' => 'by-product-family',
  52. 'choices' => array(
  53. 'field.ProductFamily.behaviorCountStockOptions.byQuantity' => 'by-quantity',
  54. 'field.ProductFamily.behaviorCountStockOptions.byProductFamily' => 'by-product-family',
  55. 'field.ProductFamily.behaviorCountStockOptions.byProduct' => 'by-product'
  56. ),
  57. 'translation_domain' => 'lcshop',
  58. 'multiple' => false,
  59. 'expanded' => true
  60. ));
  61. $formBuilder->add('behaviorAddToCart', ChoiceType::class, array(
  62. 'empty_data' => 'by-product-family',
  63. 'choices' => array(
  64. 'field.ProductFamily.behaviorAddToCartOptions.simple' => 'simple',
  65. 'field.ProductFamily.behaviorAddToCartOptions.multiple' => 'multiple'
  66. ),
  67. 'translation_domain' => 'lcshop',
  68. 'multiple' => false,
  69. 'required'=>false,
  70. 'expanded' => true
  71. ));
  72. $formBuilder->add('behaviorBuyingPrice', ChoiceType::class, array(
  73. 'empty_data' => 'by-piece',
  74. 'choices' => array(
  75. 'field.ProductFamily.behaviorBuyingPriceOptions.byPiece' => 'by-piece',
  76. 'field.ProductFamily.behaviorBuyingPriceOptions.byRefUnit' => 'by-reference-unit'
  77. ),
  78. 'translation_domain' => 'lcshop',
  79. 'multiple' => false,
  80. 'expanded' => true
  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. return $formBuilder;
  124. }
  125. public function updateEntity($entity)
  126. {
  127. $productFamilyRequest = $this->request->request->get('productfamily');
  128. if(isset($productFamilyRequest['taxRate'])) {
  129. $taxRateId = intval($productFamilyRequest['taxRate']);
  130. if ($taxRateId > 0) {
  131. $repo = $this->em->getRepository(TaxRateInterface::class);
  132. $entity->setTaxRate($repo->find($taxRateId));
  133. }
  134. }
  135. $unitId = intval($productFamilyRequest['unit']);
  136. if ($unitId > 0) {
  137. $repo = $this->em->getRepository(UnitInterface::class);
  138. $entity->setUnit($repo->find($unitId));
  139. }
  140. $this->processCategories($entity);
  141. $this->processProducts($entity);
  142. parent::updateEntity($entity);
  143. }
  144. public function persistEntity($entity)
  145. {
  146. $this->processCategories($entity);
  147. $this->processProducts($entity);
  148. parent::persistEntity($entity);
  149. }
  150. protected function processProducts($entity)
  151. {
  152. $repo = $this->em->getRepository(UnitInterface::class);
  153. //si il existe un et un seul produit pour ce product family n'ajoute rien supprime rien
  154. if (count($entity->getProducts()) == 0) {
  155. $product = new Product();
  156. $product->setProductFamily($entity);
  157. $this->em->persist($product);
  158. $entity->addProduct($product);
  159. } else {
  160. foreach ($entity->getProducts() as $i=>$product) {
  161. $product->setProductFamily($entity);
  162. $this->em->persist($product);
  163. $entity->addProduct($product);
  164. // die('ncici');
  165. }
  166. }
  167. }
  168. protected function processCategories(ProductFamilyInterface $entity)
  169. {
  170. $productCategoryRepository = $this->getDoctrine()->getRepository(ProductCategoryInterface::class);
  171. $productCategories = $entity->getProductCategories();
  172. $entity->initProductCategories();
  173. foreach ($productCategories as $key => $bool) {
  174. if (is_bool($bool) && $bool) {
  175. if (strpos($key, 'category_children_') !== false) {
  176. $idCategory = (int)str_replace('category_children_', '', $key);
  177. } else {
  178. $idCategory = (int)str_replace('category_', '', $key);
  179. }
  180. $category = $productCategoryRepository->find($idCategory);
  181. $entity->addProductCategory($category);
  182. }
  183. }
  184. }
  185. protected function editAction()
  186. {
  187. $this->dispatch(EasyAdminEvents::PRE_EDIT);
  188. $id = $this->request->query->get('id');
  189. $easyadmin = $this->request->attributes->get('easyadmin');
  190. $entity = $easyadmin['item'];
  191. if ($this->request->isXmlHttpRequest() && $property = $this->request->query->get('property')) {
  192. $newValue = 'true' === mb_strtolower($this->request->query->get('newValue'));
  193. $fieldsMetadata = $this->entity['list']['fields'];
  194. if (!isset($fieldsMetadata[$property]) || 'toggle' !== $fieldsMetadata[$property]['dataType']) {
  195. throw new \RuntimeException(sprintf('The type of the "%s" property is not "toggle".', $property));
  196. }
  197. $this->updateEntityProperty($entity, $property, $newValue);
  198. // cast to integer instead of string to avoid sending empty responses for 'false'
  199. return new Response((int)$newValue);
  200. }
  201. $fields = $this->entity['edit']['fields'];
  202. $editForm = $this->executeDynamicMethod('create<EntityName>EditForm', [$entity, $fields]);
  203. $deleteForm = $this->createDeleteForm($this->entity['name'], $id);
  204. $sortableProductsField = array();
  205. foreach ($editForm->get('products')->getData() as $k => $product) {
  206. $sortableProductsField[$product->getPosition()] = $k;
  207. }
  208. ksort($sortableProductsField);
  209. $editForm->handleRequest($this->request);
  210. if ($editForm->isSubmitted() && $editForm->isValid()) {
  211. $this->processUploadedFiles($editForm);
  212. $this->dispatch(EasyAdminEvents::PRE_UPDATE, ['entity' => $entity]);
  213. $this->executeDynamicMethod('update<EntityName>Entity', [$entity, $editForm]);
  214. $this->dispatch(EasyAdminEvents::POST_UPDATE, ['entity' => $entity]);
  215. return $this->redirectToReferrer();
  216. }
  217. $this->dispatch(EasyAdminEvents::POST_EDIT);
  218. $parameters = [
  219. 'form' => $editForm->createView(),
  220. 'entity_fields' => $fields,
  221. 'entity' => $entity,
  222. 'delete_form' => $deleteForm->createView(),
  223. 'sortableProductsField' => $sortableProductsField
  224. ];
  225. return $this->executeDynamicMethod('render<EntityName>Template', ['edit', $this->entity['templates']['edit'], $parameters]);
  226. }
  227. protected function newAction()
  228. {
  229. $this->dispatch(EasyAdminEvents::PRE_NEW);
  230. $entity = $this->executeDynamicMethod('createNew<EntityName>Entity');
  231. $easyadmin = $this->request->attributes->get('easyadmin');
  232. $easyadmin['item'] = $entity;
  233. $this->request->attributes->set('easyadmin', $easyadmin);
  234. $fields = $this->entity['new']['fields'];
  235. $newForm = $this->executeDynamicMethod('create<EntityName>NewForm', [$entity, $fields]);
  236. $newForm->handleRequest($this->request);
  237. if ($newForm->isSubmitted() && $newForm->isValid()) {
  238. $this->processUploadedFiles($newForm);
  239. $this->dispatch(EasyAdminEvents::PRE_PERSIST, ['entity' => $entity]);
  240. $this->executeDynamicMethod('persist<EntityName>Entity', [$entity, $newForm]);
  241. $this->dispatch(EasyAdminEvents::POST_PERSIST, ['entity' => $entity]);
  242. return $this->redirectToReferrer();
  243. }
  244. $this->dispatch(EasyAdminEvents::POST_NEW, [
  245. 'entity_fields' => $fields,
  246. 'form' => $newForm,
  247. 'entity' => $entity
  248. ]);
  249. $productCategoryRepository = $this->getDoctrine()->getRepository(ProductCategoryInterface::class);
  250. $categories = $productCategoryRepository->findAllParents();
  251. $parameters = [
  252. 'form' => $newForm->createView(),
  253. 'entity_fields' => $fields,
  254. 'entity' => $entity,
  255. 'categories' => $categories,
  256. 'sortableProductsField' => array()
  257. ];
  258. return $this->executeDynamicMethod('render<EntityName>Template', ['new', $this->entity['templates']['new'], $parameters]);
  259. }
  260. }