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.

302 lines
13KB

  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\Form\ProductFamilyCategoriesType;
  12. use Lc\ShopBundle\Form\ProductType;
  13. use Symfony\Bridge\Doctrine\Form\Type\EntityType;
  14. use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
  15. use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
  16. use Symfony\Component\Form\Extension\Core\Type\CollectionType;
  17. use Symfony\Component\Form\Extension\Core\Type\MoneyType;
  18. use Symfony\Component\Form\Extension\Core\Type\NumberType;
  19. use Symfony\Component\HttpFoundation\Response;
  20. class ProductFamilyController extends AdminController
  21. {
  22. private $taxRateClass;
  23. private $choicesTaxRateParam;
  24. public function createEntityFormBuilder($entity, $view)
  25. {
  26. $formBuilder = parent::createEntityFormBuilder($entity, $view);
  27. $class = $this->em->getClassMetadata(ProductCategoryInterface::class);
  28. $this->taxRateClass = $this->em->getClassMetadata(TaxRateInterface::class);
  29. $formBuilder->add('productCategories', ProductFamilyCategoriesType::class) ;
  30. //CHOICE qui permet de sélectionner une taxe
  31. //ça c'est du lourd fais très attention faudra refactorer
  32. $this->getUser()->getMerchant()->getTaxRate();
  33. $choicesTaxRate['Valeur par défaut'] = 0;
  34. foreach ($this->em->getRepository($this->taxRateClass->name)->findAll() as $tax) {
  35. $choicesTaxRate[$tax->getTitle()] = $tax->getId();
  36. $this->choicesTaxRateParam[$tax->getId()] = $tax->getValue();
  37. }
  38. //là mon ami je kiffe symfo !!!!!
  39. $this->choicesTaxRateParam[0] = $this->getUser()->getMerchant()->getTaxRate()->getValue();
  40. $formBuilder->add('taxRate', ChoiceType::class, array(
  41. 'label' => 'TVA',
  42. 'choices' => $choicesTaxRate,
  43. 'mapped' => false,
  44. 'data'=> 0,
  45. 'choice_attr' => function ($choice, $key, $value) {
  46. return ['data-tax-rate-value' => $this->choicesTaxRateParam[$choice]];
  47. },
  48. ));
  49. $formBuilder->add('unit', ChoiceType::class, array(
  50. 'label' => 'Unité',
  51. //''
  52. 'choices' => array(
  53. 'pièce' => 'piece',
  54. 'g' => 'g',
  55. 'kg' => 'kg',
  56. 'ml' => 'ml',
  57. 'L' => 'L'
  58. ),
  59. ));
  60. $formBuilder->add('weight', NumberType::class, array(
  61. 'label' => 'Poids',
  62. 'attr' => [
  63. 'append_html' => 'g'
  64. ]
  65. ));
  66. $formBuilder->add('step', NumberType::class, array(
  67. 'label' => 'Pas',
  68. 'help' => 'Quantité à incrémenter / décrémenter lors des mouvements de quantité',
  69. ));
  70. $formBuilder->add('price', NumberType::class, array(
  71. 'label' => 'Prix',
  72. ));
  73. $formBuilder->add('priceWithTax', NumberType::class, array(
  74. 'label' => 'Prix TTC',
  75. 'required' => false,
  76. 'mapped' => false
  77. ));
  78. $formBuilder->add('behaviorCountStock', ChoiceType::class, array(
  79. 'label' => 'Stock',
  80. 'empty_data' => 'by-product-family',
  81. 'choices' => array(
  82. 'Gèrer le stock par produit' => 'by-product-family',
  83. 'Gèrer le stock par déclinaison' => 'by-product'
  84. ),
  85. 'multiple' => false,
  86. 'expanded' => true
  87. ));
  88. $formBuilder->add('availableQuantity', NumberType::class, array(
  89. 'label' => 'Quantité disponible',
  90. 'required' => false,
  91. ));
  92. $formBuilder->add('products', CollectionType::class, array(
  93. 'label' => 'Déclinaisons',
  94. 'entry_type' => ProductType::class,
  95. 'entry_options' => ['label' => false],
  96. 'allow_add' => true,
  97. 'allow_delete' => true,
  98. 'required' => true
  99. )
  100. );
  101. return $formBuilder;
  102. }
  103. public function updateEntity($entity)
  104. {
  105. $prodductFamilyRequest = $this->request->request->get('productfamily');
  106. if ($taxRateId = intval($prodductFamilyRequest['taxRate']) > 0) {
  107. $repo = $this->em->getRepository(TaxRateInterface::class);
  108. $entity->setTaxRate($repo->find($taxRateId));
  109. }
  110. $this->processCategories($entity) ;
  111. $this->processProducts($entity) ;
  112. $this->setUpdated($entity);
  113. parent::updateEntity($entity);
  114. }
  115. public function persistEntity($entity)
  116. {
  117. $this->processCategories($entity) ;
  118. $this->processProducts($entity) ;
  119. parent::persistEntity($entity) ;
  120. }
  121. protected function processProducts($entity)
  122. {
  123. //si il existe un et un seul produit pour ce product family n'ajoute rien supprime rien
  124. if(count($entity->getProducts()) == 0) {
  125. $product = new Product();
  126. $product->setCreatedBy($this->getUser()) ;
  127. $product->setUpdatedBy($this->getUser()) ;
  128. $product->setProductFamily($entity) ;
  129. $this->em->persist($product);
  130. $entity->addProduct($product) ;
  131. }
  132. else {
  133. foreach($entity->getProducts() as $product) {
  134. $product->setProductFamily($entity) ;
  135. $product->setCreatedBy($this->getUser()) ;
  136. $product->setUpdatedBy($this->getUser()) ;
  137. $this->em->persist($product);
  138. $entity->addProduct($product) ;
  139. // die('ncici');
  140. }
  141. }
  142. }
  143. protected function processCategories(ProductFamilyInterface $entity)
  144. {
  145. $productCategoryRepository = $this->getDoctrine()->getRepository(ProductCategoryInterface::class) ;
  146. $productCategories = $entity->getProductCategories() ;
  147. $entity->initProductCategories() ;
  148. foreach($productCategories as $key => $bool) {
  149. if(is_bool($bool) && $bool) {
  150. if(strpos($key, 'category_children_') !== false) {
  151. $idCategory = (int) str_replace('category_children_', '', $key) ;
  152. }
  153. else {
  154. $idCategory = (int) str_replace('category_', '', $key) ;
  155. }
  156. $category = $productCategoryRepository->find($idCategory) ;
  157. $entity->addProductCategory($category) ;
  158. }
  159. }
  160. }
  161. protected function editAction()
  162. {
  163. $this->dispatch(EasyAdminEvents::PRE_EDIT);
  164. $id = $this->request->query->get('id');
  165. $easyadmin = $this->request->attributes->get('easyadmin');
  166. $entity = $easyadmin['item'];
  167. if ($this->request->isXmlHttpRequest() && $property = $this->request->query->get('property')) {
  168. $newValue = 'true' === mb_strtolower($this->request->query->get('newValue'));
  169. $fieldsMetadata = $this->entity['list']['fields'];
  170. if (!isset($fieldsMetadata[$property]) || 'toggle' !== $fieldsMetadata[$property]['dataType']) {
  171. throw new \RuntimeException(sprintf('The type of the "%s" property is not "toggle".', $property));
  172. }
  173. $this->updateEntityProperty($entity, $property, $newValue);
  174. // cast to integer instead of string to avoid sending empty responses for 'false'
  175. return new Response((int) $newValue);
  176. }
  177. $fields = $this->entity['edit']['fields'];
  178. $editForm = $this->executeDynamicMethod('create<EntityName>EditForm', [$entity, $fields]);
  179. $deleteForm = $this->createDeleteForm($this->entity['name'], $id);
  180. $sortableProductsField = array();
  181. foreach($editForm->get('products')->getData() as $k=>$product){
  182. $sortableProductsField[$product->getPosition()] = $k;
  183. }
  184. ksort($sortableProductsField);
  185. $editForm->handleRequest($this->request);
  186. if ($editForm->isSubmitted() && $editForm->isValid()) {
  187. $this->processUploadedFiles($editForm);
  188. $this->dispatch(EasyAdminEvents::PRE_UPDATE, ['entity' => $entity]);
  189. $this->executeDynamicMethod('update<EntityName>Entity', [$entity, $editForm]);
  190. $this->dispatch(EasyAdminEvents::POST_UPDATE, ['entity' => $entity]);
  191. return $this->redirectToReferrer();
  192. }
  193. $this->dispatch(EasyAdminEvents::POST_EDIT);
  194. $productCategoryRepository = $this->getDoctrine()->getRepository(ProductCategoryInterface::class);
  195. $categories = $productCategoryRepository->findAllParents();
  196. $parameters = [
  197. 'form' => $editForm->createView(),
  198. 'entity_fields' => $fields,
  199. 'entity' => $entity,
  200. 'delete_form' => $deleteForm->createView(),
  201. 'categories' => $categories,
  202. 'sortableProductsField' => $sortableProductsField
  203. ];
  204. return $this->executeDynamicMethod('render<EntityName>Template', ['edit', $this->entity['templates']['edit'], $parameters]);
  205. }
  206. protected function newAction()
  207. {
  208. $this->dispatch(EasyAdminEvents::PRE_NEW);
  209. $entity = $this->executeDynamicMethod('createNew<EntityName>Entity');
  210. $easyadmin = $this->request->attributes->get('easyadmin');
  211. $easyadmin['item'] = $entity;
  212. $this->request->attributes->set('easyadmin', $easyadmin);
  213. $fields = $this->entity['new']['fields'];
  214. $newForm = $this->executeDynamicMethod('create<EntityName>NewForm', [$entity, $fields]);
  215. $newForm->handleRequest($this->request);
  216. if ($newForm->isSubmitted() && $newForm->isValid()) {
  217. $this->processUploadedFiles($newForm);
  218. $this->dispatch(EasyAdminEvents::PRE_PERSIST, ['entity' => $entity]);
  219. $this->executeDynamicMethod('persist<EntityName>Entity', [$entity, $newForm]);
  220. $this->dispatch(EasyAdminEvents::POST_PERSIST, ['entity' => $entity]);
  221. return $this->redirectToReferrer();
  222. }
  223. $this->dispatch(EasyAdminEvents::POST_NEW, [
  224. 'entity_fields' => $fields,
  225. 'form' => $newForm,
  226. 'entity' => $entity
  227. ]);
  228. $productCategoryRepository = $this->getDoctrine()->getRepository(ProductCategoryInterface::class);
  229. $categories = $productCategoryRepository->findAllParents();
  230. $parameters = [
  231. 'form' => $newForm->createView(),
  232. 'entity_fields' => $fields,
  233. 'entity' => $entity,
  234. 'categories' => $categories,
  235. 'sortableProductsField' => array()
  236. ];
  237. return $this->executeDynamicMethod('render<EntityName>Template', ['new', $this->entity['templates']['new'], $parameters]);
  238. }
  239. }