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

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