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.

ProductFamilyController.php 16KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  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. //CHOICE qui permet de sélectionner une taxe
  34. //ça c'est du lourd fais très attention faudra refactorer
  35. $this->getUser()->getMerchant()->getTaxRate();
  36. $choicesTaxRate['Valeur par défaut'] = 0;
  37. $choicesSupplierTaxRate['Hérité de la TVA du produit'] = 0;
  38. foreach ($this->em->getRepository($this->taxRateClass->name)->findAll() as $tax) {
  39. $choicesTaxRate[$tax->getTitle()] = $tax->getId();
  40. $choicesSupplierTaxRate[$tax->getTitle()] = $tax->getId();
  41. $this->choicesTaxRateParam[$tax->getId()] = $tax->getValue();
  42. $this->choicesSupplierTaxRateParam[$tax->getId()] = $tax->getValue();
  43. }
  44. //là mon ami je kiffe symfo !!!!!
  45. $this->choicesTaxRateParam[0] = $this->getUser()->getMerchant()->getTaxRate()->getValue();
  46. // choices unit
  47. $choicesUnits = [] ;
  48. $unitClass = $this->em->getClassMetadata(UnitInterface::class)->getName() ;
  49. foreach ($this->em->getRepository($unitClass)->findAll() as $unit) {
  50. $choicesUnits[$unit->getWording()] = $unit->getId() ;
  51. }
  52. $formBuilder->add('taxRate', ChoiceType::class, array(
  53. 'label' => 'TVA',
  54. 'choices' => $choicesTaxRate,
  55. 'mapped' => false,
  56. 'data'=> 0,
  57. 'choice_attr' => function ($choice, $key, $value) {
  58. return ['data-tax-rate-value' => $this->choicesTaxRateParam[$choice]];
  59. },
  60. ));
  61. $formBuilder->add('differentSupplierTaxRate', CheckboxType::class, array(
  62. 'required'=>false,
  63. 'mapped'=>false
  64. ));
  65. $this->choicesSupplierTaxRateParam[0] = 'inherited';
  66. $formBuilder->add('supplierTaxRate', ChoiceType::class, array(
  67. 'label' => 'TVA du fournisseur',
  68. 'choices' => $choicesSupplierTaxRate,
  69. 'data'=> 0,
  70. 'mapped' => false,
  71. 'choice_attr' => function ($choice, $key, $value) {
  72. return ['data-tax-rate-value' => $this->choicesSupplierTaxRateParam[$choice]];
  73. },
  74. ));
  75. $formBuilder->add('unit', EntityType::class, array(
  76. 'class' => $unitClass,
  77. /*'label' => 'Unité',
  78. 'choices' => $choicesUnits,
  79. 'data'=> 0,*/
  80. ));
  81. $formBuilder->add('quantity', NumberType::class, array(
  82. 'label' => 'Quantité',
  83. 'attr' => [
  84. 'append_html' => 'g'
  85. ]
  86. ));
  87. $formBuilder->add('price', NumberType::class, array(
  88. 'label' => 'Prix',
  89. ));
  90. $formBuilder->add('priceWithTax', NumberType::class, array(
  91. 'label' => 'Prix TTC',
  92. 'required' => false,
  93. 'mapped' => false
  94. ));
  95. $formBuilder->add('buyingPrice', NumberType::class, array(
  96. 'label' => 'Prix d\'achat',
  97. ));
  98. $formBuilder->add('priceByRefUnit', NumberType::class, array(
  99. 'label' => 'Prix de vente en fonction de l\'unité de référence',
  100. 'mapped'=>false,
  101. 'required'=>false
  102. ));
  103. $formBuilder->add('priceByRefUnitWithTax', NumberType::class, array(
  104. 'label' => 'Prix',
  105. 'mapped'=>false,
  106. 'required'=>false
  107. ));
  108. $formBuilder->add('buyingPriceWithTax', NumberType::class, array(
  109. 'label' => 'Prix d\'achat TTC',
  110. 'required' => false,
  111. 'mapped' => false
  112. ));
  113. $formBuilder->add('multiplyingFactor', NumberType::class, array(
  114. 'label' => 'Coefficiant de multiplication',
  115. 'mapped'=>false,
  116. 'required'=>false
  117. ));
  118. $formBuilder->add('behaviorCountStock', ChoiceType::class, array(
  119. 'label' => 'Stock',
  120. 'empty_data' => 'by-product-family',
  121. 'choices' => array(
  122. 'Gèrer le stock par quantité' => 'by-quantity',
  123. 'Gèrer le stock par produit (à la pièce)' => 'by-product-family',
  124. 'Gèrer le stock par déclinaison' => 'by-product'
  125. ),
  126. 'multiple' => false,
  127. 'expanded' => true
  128. ));
  129. $formBuilder->add('organicLabel', ChoiceType::class, array(
  130. 'label' => 'Type de labellisation',
  131. 'choices' => array(
  132. 'Agriculture biologique' => 'bio',
  133. 'Nature & progrès' => 'nature-progres',
  134. 'Haute valeur environnementale' => 'hve'
  135. ),
  136. 'multiple' => false,
  137. 'expanded' => false,
  138. 'required' => false
  139. ));
  140. $formBuilder->add('products', CollectionType::class, array(
  141. 'label' => 'Déclinaisons',
  142. 'entry_type' => ProductType::class,
  143. 'entry_options' => ['label' => false],
  144. 'allow_add' => true,
  145. 'allow_delete' => true,
  146. 'required' => true
  147. )
  148. );
  149. return $formBuilder;
  150. }
  151. public function updateEntity($entity)
  152. {
  153. $prodductFamilyRequest = $this->request->request->get('productfamily');
  154. if ($taxRateId = intval($prodductFamilyRequest['taxRate']) > 0) {
  155. $repo = $this->em->getRepository(TaxRateInterface::class);
  156. $entity->setTaxRate($repo->find($taxRateId));
  157. }
  158. $this->processCategories($entity) ;
  159. $this->processProducts($entity) ;
  160. $this->setUpdated($entity);
  161. parent::updateEntity($entity);
  162. }
  163. public function persistEntity($entity)
  164. {
  165. $this->processCategories($entity) ;
  166. $this->processProducts($entity) ;
  167. parent::persistEntity($entity) ;
  168. }
  169. protected function processProducts($entity)
  170. {
  171. //si il existe un et un seul produit pour ce product family n'ajoute rien supprime rien
  172. if(count($entity->getProducts()) == 0) {
  173. $product = new Product();
  174. $product->setCreatedBy($this->getUser()) ;
  175. $product->setUpdatedBy($this->getUser()) ;
  176. $product->setProductFamily($entity) ;
  177. $this->em->persist($product);
  178. $entity->addProduct($product) ;
  179. }
  180. else {
  181. foreach($entity->getProducts() as $product) {
  182. $product->setProductFamily($entity) ;
  183. $product->setCreatedBy($this->getUser()) ;
  184. $product->setUpdatedBy($this->getUser()) ;
  185. $this->em->persist($product);
  186. $entity->addProduct($product) ;
  187. // die('ncici');
  188. }
  189. }
  190. }
  191. protected function processCategories(ProductFamilyInterface $entity)
  192. {
  193. $productCategoryRepository = $this->getDoctrine()->getRepository(ProductCategoryInterface::class) ;
  194. $productCategories = $entity->getProductCategories() ;
  195. $entity->initProductCategories() ;
  196. foreach($productCategories as $key => $bool) {
  197. if(is_bool($bool) && $bool) {
  198. if(strpos($key, 'category_children_') !== false) {
  199. $idCategory = (int) str_replace('category_children_', '', $key) ;
  200. }
  201. else {
  202. $idCategory = (int) str_replace('category_', '', $key) ;
  203. }
  204. $category = $productCategoryRepository->find($idCategory) ;
  205. $entity->addProductCategory($category) ;
  206. }
  207. }
  208. }
  209. protected function editAction()
  210. {
  211. $this->dispatch(EasyAdminEvents::PRE_EDIT);
  212. $id = $this->request->query->get('id');
  213. $easyadmin = $this->request->attributes->get('easyadmin');
  214. $entity = $easyadmin['item'];
  215. if ($this->request->isXmlHttpRequest() && $property = $this->request->query->get('property')) {
  216. $newValue = 'true' === mb_strtolower($this->request->query->get('newValue'));
  217. $fieldsMetadata = $this->entity['list']['fields'];
  218. if (!isset($fieldsMetadata[$property]) || 'toggle' !== $fieldsMetadata[$property]['dataType']) {
  219. throw new \RuntimeException(sprintf('The type of the "%s" property is not "toggle".', $property));
  220. }
  221. $this->updateEntityProperty($entity, $property, $newValue);
  222. // cast to integer instead of string to avoid sending empty responses for 'false'
  223. return new Response((int) $newValue);
  224. }
  225. $fields = $this->entity['edit']['fields'];
  226. $editForm = $this->executeDynamicMethod('create<EntityName>EditForm', [$entity, $fields]);
  227. $deleteForm = $this->createDeleteForm($this->entity['name'], $id);
  228. $sortableProductsField = array();
  229. foreach($editForm->get('products')->getData() as $k=>$product){
  230. $sortableProductsField[$product->getPosition()] = $k;
  231. }
  232. ksort($sortableProductsField);
  233. $editForm->handleRequest($this->request);
  234. if ($editForm->isSubmitted() && $editForm->isValid()) {
  235. $this->processUploadedFiles($editForm);
  236. $this->dispatch(EasyAdminEvents::PRE_UPDATE, ['entity' => $entity]);
  237. $this->executeDynamicMethod('update<EntityName>Entity', [$entity, $editForm]);
  238. $this->dispatch(EasyAdminEvents::POST_UPDATE, ['entity' => $entity]);
  239. return $this->redirectToReferrer();
  240. }
  241. $this->dispatch(EasyAdminEvents::POST_EDIT);
  242. $productCategoryRepository = $this->getDoctrine()->getRepository(ProductCategoryInterface::class);
  243. $categories = $productCategoryRepository->findAllParents();
  244. $parameters = [
  245. 'form' => $editForm->createView(),
  246. 'entity_fields' => $fields,
  247. 'entity' => $entity,
  248. 'delete_form' => $deleteForm->createView(),
  249. 'categories' => $categories,
  250. 'sortableProductsField' => $sortableProductsField
  251. ];
  252. return $this->executeDynamicMethod('render<EntityName>Template', ['edit', $this->entity['templates']['edit'], $parameters]);
  253. }
  254. protected function newAction()
  255. {
  256. $this->dispatch(EasyAdminEvents::PRE_NEW);
  257. $entity = $this->executeDynamicMethod('createNew<EntityName>Entity');
  258. $easyadmin = $this->request->attributes->get('easyadmin');
  259. $easyadmin['item'] = $entity;
  260. $this->request->attributes->set('easyadmin', $easyadmin);
  261. $fields = $this->entity['new']['fields'];
  262. $newForm = $this->executeDynamicMethod('create<EntityName>NewForm', [$entity, $fields]);
  263. $newForm->handleRequest($this->request);
  264. if ($newForm->isSubmitted() && $newForm->isValid()) {
  265. $this->processUploadedFiles($newForm);
  266. $this->dispatch(EasyAdminEvents::PRE_PERSIST, ['entity' => $entity]);
  267. $this->executeDynamicMethod('persist<EntityName>Entity', [$entity, $newForm]);
  268. $this->dispatch(EasyAdminEvents::POST_PERSIST, ['entity' => $entity]);
  269. return $this->redirectToReferrer();
  270. }
  271. $this->dispatch(EasyAdminEvents::POST_NEW, [
  272. 'entity_fields' => $fields,
  273. 'form' => $newForm,
  274. 'entity' => $entity
  275. ]);
  276. $productCategoryRepository = $this->getDoctrine()->getRepository(ProductCategoryInterface::class);
  277. $categories = $productCategoryRepository->findAllParents();
  278. $parameters = [
  279. 'form' => $newForm->createView(),
  280. 'entity_fields' => $fields,
  281. 'entity' => $entity,
  282. 'categories' => $categories,
  283. 'sortableProductsField' => array()
  284. ];
  285. return $this->executeDynamicMethod('render<EntityName>Template', ['new', $this->entity['templates']['new'], $parameters]);
  286. }
  287. }