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.

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