Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

419 Zeilen
22KB

  1. <?php
  2. namespace Lc\ShopBundle\Controller\Backend;
  3. use App\Entity\Product;
  4. use EasyCorp\Bundle\EasyAdminBundle\Event\EasyAdminEvents;
  5. use Lc\ShopBundle\Context\OrderShopInterface;
  6. use Lc\ShopBundle\Context\ProductCategoryInterface;
  7. use Lc\ShopBundle\Context\ProductFamilyInterface;
  8. use Lc\ShopBundle\Context\ReductionCatalogInterface;
  9. use Lc\ShopBundle\Context\TaxRateInterface;
  10. use Lc\ShopBundle\Form\Backend\Common\ReductionCatalogType;
  11. use Lc\ShopBundle\Form\Backend\ProductFamily\ProductType;
  12. use Lc\ShopBundle\Model\ProductFamily;
  13. use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
  14. use Symfony\Component\Form\Extension\Core\Type\CollectionType;
  15. use Symfony\Component\Form\Extension\Core\Type\HiddenType;
  16. use Symfony\Component\Form\Extension\Core\Type\NumberType;
  17. use Symfony\Component\Form\FormError;
  18. use Symfony\Component\HttpFoundation\Response;
  19. class ProductFamilyController extends AdminController
  20. {
  21. private $taxRateClass;
  22. private $choicesTaxRateParam;
  23. private $choicesSupplierTaxRateParam;
  24. public function createEntityFormBuilder($entity, $view, $override = true)
  25. {
  26. $formBuilder = parent::createEntityFormBuilder($entity, $view, false);
  27. $class = $this->em->getClassMetadata(ProductCategoryInterface::class);
  28. $this->taxRateClass = $this->em->getClassMetadata(TaxRateInterface::class);
  29. //$formBuilder->add('productCategories', ProductFamilyCategoriesType::class);
  30. $reductionCatalogRepo = $this->em->getRepository(ReductionCatalogInterface::class);
  31. $reductionCatalogClass = $this->em->getClassMetadata(ReductionCatalogInterface::class);
  32. $reductionCatalog = $reductionCatalogRepo->findOneBy(array('status' => false, 'productFamily' => $entity));
  33. if ($reductionCatalog == null) $reductionCatalog = new $reductionCatalogClass->name;
  34. $formBuilder->add('reductionCatalog', ReductionCatalogType::class, array(
  35. 'mapped' => false,
  36. 'data' => $reductionCatalog
  37. ));
  38. $formBuilder->add('stayOnPage', HiddenType::class, array(
  39. 'required' => false,
  40. 'mapped' => false,
  41. ));
  42. $formBuilder->add('warningMessageType', ChoiceType::class, array(
  43. 'choices' => array(
  44. 'field.default.warningMessageTypeOptions.' . ProductFamily::WARNING_MESSAGE_TYPE_SUCCESS => ProductFamily::WARNING_MESSAGE_TYPE_SUCCESS,
  45. 'field.default.warningMessageTypeOptions.' . ProductFamily::WARNING_MESSAGE_TYPE_ERROR => ProductFamily::WARNING_MESSAGE_TYPE_ERROR,
  46. 'field.default.warningMessageTypeOptions.' . ProductFamily::WARNING_MESSAGE_TYPE_WARNING => ProductFamily::WARNING_MESSAGE_TYPE_WARNING,
  47. 'field.default.warningMessageTypeOptions.' . ProductFamily::WARNING_MESSAGE_TYPE_INFO => ProductFamily::WARNING_MESSAGE_TYPE_INFO
  48. ),
  49. 'translation_domain' => 'lcshop',
  50. 'multiple' => false,
  51. 'expanded' => false,
  52. 'required' => false
  53. ));
  54. $formBuilder->add('behaviorCountStock', ChoiceType::class, array(
  55. 'empty_data' => ProductFamily::BEHAVIOR_COUNT_STOCK_BY_PRODUCT_FAMILY,
  56. 'choices' => array(
  57. 'field.ProductFamily.behaviorCountStockOptions.' . ProductFamily::BEHAVIOR_COUNT_STOCK_BY_MEASURE => ProductFamily::BEHAVIOR_COUNT_STOCK_BY_MEASURE,
  58. 'field.ProductFamily.behaviorCountStockOptions.' . ProductFamily::BEHAVIOR_COUNT_STOCK_BY_PRODUCT_FAMILY => ProductFamily::BEHAVIOR_COUNT_STOCK_BY_PRODUCT_FAMILY,
  59. 'field.ProductFamily.behaviorCountStockOptions.' . ProductFamily::BEHAVIOR_COUNT_STOCK_BY_PRODUCT => ProductFamily::BEHAVIOR_COUNT_STOCK_BY_PRODUCT,
  60. 'field.ProductFamily.behaviorCountStockOptions.' . ProductFamily::BEHAVIOR_COUNT_STOCK_UNLIMITED => ProductFamily::BEHAVIOR_COUNT_STOCK_UNLIMITED
  61. ),
  62. 'translation_domain' => 'lcshop',
  63. 'multiple' => false,
  64. 'expanded' => true
  65. ));
  66. $formBuilder->add('behaviorAddToCart', ChoiceType::class, array(
  67. 'data' => $entity->getBehaviorAddToCart() ? $entity->getBehaviorAddToCart() : 'simple',
  68. 'choices' => array(
  69. 'field.ProductFamily.behaviorAddToCartOptions.' . ProductFamily::BEHAVIOR_ADD_TO_CART_SIMPLE => ProductFamily::BEHAVIOR_ADD_TO_CART_SIMPLE,
  70. 'field.ProductFamily.behaviorAddToCartOptions.' . ProductFamily::BEHAVIOR_ADD_TO_CART_MULTIPLE => ProductFamily::BEHAVIOR_ADD_TO_CART_MULTIPLE
  71. ),
  72. 'translation_domain' => 'lcshop',
  73. 'multiple' => false,
  74. 'expanded' => true
  75. ));
  76. $formBuilder->add('behaviorPrice', ChoiceType::class, array(
  77. 'empty_data' => 'by-piece',
  78. 'choices' => array(
  79. 'field.ProductFamily.behaviorPriceOptions.' . ProductFamily::BEHAVIOR_PRICE_BY_PIECE => ProductFamily::BEHAVIOR_PRICE_BY_PIECE,
  80. 'field.ProductFamily.behaviorPriceOptions.' . ProductFamily::BEHAVIOR_PRICE_BY_REFERENCE_UNIT => ProductFamily::BEHAVIOR_PRICE_BY_REFERENCE_UNIT
  81. ),
  82. 'translation_domain' => 'lcshop',
  83. 'multiple' => false,
  84. 'expanded' => true
  85. ));
  86. $formBuilder->add('multiplyingFactor', NumberType::class, array(
  87. 'mapped' => false,
  88. 'data' => floatval($this->merchantUtils->getMerchantConfig('multiplying-factor')),
  89. ));
  90. $formBuilder->add('propertyOrganicLabel', ChoiceType::class, array(
  91. 'choices' => array(
  92. 'field.ProductFamily.organicLabelOptions.' . ProductFamily::PROPERTY_ORGANIC_LABEL_AB => ProductFamily::PROPERTY_ORGANIC_LABEL_AB,
  93. 'field.ProductFamily.organicLabelOptions.' . ProductFamily::PROPERTY_ORGANIC_LABEL_NP => ProductFamily::PROPERTY_ORGANIC_LABEL_NP,
  94. 'field.ProductFamily.organicLabelOptions.' . ProductFamily::PROPERTY_ORGANIC_LABEL_HVE => ProductFamily::PROPERTY_ORGANIC_LABEL_HVE
  95. ),
  96. 'translation_domain' => 'lcshop',
  97. 'multiple' => false,
  98. 'expanded' => false,
  99. 'required' => false
  100. ));
  101. $formBuilder->add('typeExpirationDate', ChoiceType::class, array(
  102. 'choices' => array(
  103. 'field.default.' . ProductFamily::TYPE_EXPIRATION_DATE_DLC => ProductFamily::TYPE_EXPIRATION_DATE_DLC,
  104. 'field.default.' . ProductFamily::TYPE_EXPIRATION_DATE_DDM => ProductFamily::TYPE_EXPIRATION_DATE_DDM,
  105. 'field.default.' . ProductFamily::TYPE_EXPIRATION_DATE_DLUO => ProductFamily::TYPE_EXPIRATION_DATE_DLUO
  106. ),
  107. 'translation_domain' => 'lcshop',
  108. 'multiple' => false,
  109. 'expanded' => true,
  110. 'required' => false
  111. ));
  112. $formBuilder->add('behaviorStockWeek', ChoiceType::class, array(
  113. 'choices' => array(
  114. 'field.ProductFamily.behaviorStockWeekOptions.' . ProductFamily::BEHAVIOR_STOCK_WEEK_RENEWABLE => ProductFamily::BEHAVIOR_STOCK_WEEK_RENEWABLE,
  115. 'field.ProductFamily.behaviorStockWeekOptions.' . ProductFamily::BEHAVIOR_STOCK_WEEK_RENEWABLE_VALIDATION => ProductFamily::BEHAVIOR_STOCK_WEEK_RENEWABLE_VALIDATION,
  116. 'field.ProductFamily.behaviorStockWeekOptions.' . ProductFamily::BEHAVIOR_STOCK_WEEK_NON_RENEWABLE => ProductFamily::BEHAVIOR_STOCK_WEEK_NON_RENEWABLE
  117. ),
  118. 'translation_domain' => 'lcshop',
  119. 'multiple' => false,
  120. 'expanded' => true,
  121. 'required' => true
  122. ));
  123. $formBuilder->add('behaviorExpirationDate', ChoiceType::class, array(
  124. 'choices' => array(
  125. 'field.ProductFamily.behaviorExpirationDateOptions.' . ProductFamily::BEHAVIOR_EXPIRATION_DATE_BY_PRODUCT_FAMILY => ProductFamily::BEHAVIOR_EXPIRATION_DATE_BY_PRODUCT_FAMILY,
  126. 'field.ProductFamily.behaviorExpirationDateOptions.' . ProductFamily::BEHAVIOR_EXPIRATION_DATE_BY_PRODUCT => ProductFamily::BEHAVIOR_EXPIRATION_DATE_BY_PRODUCT
  127. ),
  128. 'translation_domain' => 'lcshop',
  129. 'multiple' => false,
  130. 'expanded' => true,
  131. 'required' => false
  132. ));
  133. $formBuilder->add('products', CollectionType::class, array(
  134. 'label' => 'Déclinaisons',
  135. 'entry_type' => ProductType::class,
  136. 'entry_options' => ['label' => false],
  137. 'allow_add' => true,
  138. 'allow_delete' => true,
  139. 'required' => true
  140. )
  141. );
  142. $formBuilder = $this->overrideFormBuilder($formBuilder, $entity, $view);
  143. return $formBuilder;
  144. }
  145. public function updateProductFamilyEntity($entity, $editForm = false)
  146. {
  147. if ($editForm) {
  148. $this->processReductionCatalog($entity, $editForm);
  149. $this->processCategories($entity);
  150. $this->processProducts($entity);
  151. $this->processPrice($entity);
  152. }
  153. parent::updateEntity($entity);
  154. $this->orderUtils->updatePriceByProductFamily($entity);
  155. }
  156. public function persistProductFamilyEntity($entity, $newForm)
  157. {
  158. $this->processReductionCatalog($entity, $newForm);
  159. $this->processCategories($entity);
  160. $this->processProducts($entity);
  161. $this->processPrice($entity);
  162. $this->em->persist($entity);
  163. $this->em->flush();
  164. }
  165. protected function processReductionCatalog($entity, $editForm)
  166. {
  167. $reductionCatalog = $editForm->get('reductionCatalog')->getData();
  168. if ($reductionCatalog instanceof ReductionCatalogInterface) {
  169. if ($reductionCatalog->getValue() && $reductionCatalog->getBehaviorTaxRate() && $reductionCatalog->getUnit()) {
  170. $reductionCatalog->setMerchant($entity->getMerchant());
  171. $reductionCatalog->setStatus($editForm->get('activeReductionCatalog')->getData());
  172. $reductionCatalog->setProductFamily($entity);
  173. $this->em->persist($reductionCatalog);
  174. }
  175. }
  176. }
  177. protected function processPrice($entity)
  178. {
  179. if ($entity->getBehaviorPrice() == 'by-piece') {
  180. $entity->setPriceByRefUnit(null);
  181. $entity->setBuyingPriceByRefUnit(null);
  182. } else if ($entity->getBehaviorPrice() == 'by-reference-unit') {
  183. $entity->setPrice(null);
  184. $entity->setBuyingPrice(null);
  185. }
  186. }
  187. protected function processProducts($entity, $clone = false)
  188. {
  189. //si il existe un et un seul produit pour ce product family n'ajoute rien supprime rien
  190. if (count($entity->getProducts()) == 0) {
  191. $product = new Product();
  192. $product->setProductFamily($entity);
  193. $this->em->persist($product);
  194. $entity->addProduct($product);
  195. } else {
  196. foreach ($entity->getProducts() as $i => $product) {
  197. if ($clone) {
  198. $newProduct = clone $product;
  199. $newProduct->setProductFamily($entity);
  200. $this->em->persist($newProduct);
  201. $entity->addProduct($newProduct);
  202. } else {
  203. $product->setProductFamily($entity);
  204. $this->em->persist($product);
  205. $entity->addProduct($product);
  206. }
  207. }
  208. }
  209. }
  210. protected function processCategories(ProductFamilyInterface $entity)
  211. {
  212. $productCategoryRepository = $this->getDoctrine()->getRepository(ProductCategoryInterface::class);
  213. $productCategories = $entity->getProductCategories();
  214. $entity->initProductCategories();
  215. foreach ($productCategories as $key => $bool) {
  216. if (is_bool($bool) && $bool) {
  217. if (strpos($key, 'category_children_') !== false) {
  218. $idCategory = (int)str_replace('category_children_', '', $key);
  219. } else {
  220. $idCategory = (int)str_replace('category_', '', $key);
  221. }
  222. $category = $productCategoryRepository->find($idCategory);
  223. $entity->addProductCategory($category);
  224. }
  225. }
  226. }
  227. protected function editAction()
  228. {
  229. $this->dispatch(EasyAdminEvents::PRE_EDIT);
  230. $id = $this->request->query->get('id');
  231. $easyadmin = $this->request->attributes->get('easyadmin');
  232. $entity = $easyadmin['item'];
  233. if ($this->request->isXmlHttpRequest() && $property = $this->request->query->get('property')) {
  234. $newValue = 'true' === mb_strtolower($this->request->query->get('newValue'));
  235. $fieldsMetadata = $this->entity['list']['fields'];
  236. if (!isset($fieldsMetadata[$property]) || 'toggle' !== $fieldsMetadata[$property]['dataType']) {
  237. throw new \RuntimeException(sprintf('The type of the "%s" property is not "toggle".', $property));
  238. }
  239. $this->updateEntityProperty($entity, $property, $newValue);
  240. $this->utils->addFlash('success', 'success.common.fieldChange');
  241. $response['flashMessages'] = $this->utils->getFlashMessages();
  242. return new Response(json_encode($response));
  243. }
  244. $fields = $this->entity['edit']['fields'];
  245. $editForm = $this->executeDynamicMethod('create<EntityName>EditForm', [$entity, $fields]);
  246. $deleteForm = $this->createDeleteForm($this->entity['name'], $id);
  247. $sortableProductsField = array();
  248. foreach ($editForm->get('products')->getData() as $k => $product) {
  249. $sortableProductsField[$product->getPosition()] = $k;
  250. }
  251. ksort($sortableProductsField);
  252. $editForm->handleRequest($this->request);
  253. if ($editForm->isSubmitted() && count($entity->getProductCategories()) == 0) {
  254. $editForm->get('productCategories')->addError(new FormError('Vous devez choisir au moins une catégorie'));
  255. }
  256. if ($editForm->isSubmitted() && $editForm->isValid() && count($entity->getProductCategories()) > 0) {
  257. $this->processUploadedFiles($editForm);
  258. $this->dispatch(EasyAdminEvents::PRE_UPDATE, ['entity' => $entity]);
  259. $this->executeDynamicMethod('update<EntityName>Entity', [$entity, $editForm]);
  260. $this->dispatch(EasyAdminEvents::POST_UPDATE, ['entity' => $entity]);
  261. $this->utils->addFlash('success', 'Produit sauvegardé');
  262. if ($editForm->get('stayOnPage')->getData() != "false" || $this->request->get('submitAndStay') !== null) {
  263. $refererUrl = $this->request->query->get('referer', '');
  264. return $this->redirectToRoute('easyadmin', ['entity' => 'ProductFamily', 'action' => 'edit', 'id' => $id, 'referer' => $refererUrl]);
  265. } else {
  266. return $this->redirectToReferrer();
  267. }
  268. }
  269. $this->dispatch(EasyAdminEvents::POST_EDIT);
  270. $parameters = [
  271. 'form' => $editForm->createView(),
  272. 'entity_fields' => $fields,
  273. 'entity' => $entity,
  274. 'delete_form' => $deleteForm->createView(),
  275. 'sortableProductsField' => $sortableProductsField,
  276. 'totalProductOrdered' => $this->orderUtils->getTotalProductOrderedLastWeeks($entity)
  277. ];
  278. return $this->executeDynamicMethod('render<EntityName>Template', ['edit', $this->entity['templates']['edit'], $parameters]);
  279. }
  280. protected function newAction()
  281. {
  282. $this->dispatch(EasyAdminEvents::PRE_NEW);
  283. $entity = $this->executeDynamicMethod('createNew<EntityName>Entity');
  284. $easyadmin = $this->request->attributes->get('easyadmin');
  285. $easyadmin['item'] = $entity;
  286. $this->request->attributes->set('easyadmin', $easyadmin);
  287. $fields = $this->entity['new']['fields'];
  288. $newForm = $this->executeDynamicMethod('create<EntityName>NewForm', [$entity, $fields]);
  289. $newForm->handleRequest($this->request);
  290. if ($newForm->isSubmitted() && array_search(true, $newForm->get('productCategories')->getData()->toArray()) === false) {
  291. $newForm->get('productCategories')->addError(new FormError('Vous devez choisir au moins une catégorie'));
  292. }
  293. if ($newForm->isSubmitted() && $newForm->isValid() && array_search(true, $newForm->get('productCategories')->getData()->toArray()) !== false) {
  294. $this->processUploadedFiles($newForm);
  295. $this->dispatch(EasyAdminEvents::PRE_PERSIST, ['entity' => $entity]);
  296. $this->executeDynamicMethod('persist<EntityName>Entity', [$entity, $newForm]);
  297. $this->dispatch(EasyAdminEvents::POST_PERSIST, ['entity' => $entity]);
  298. if ($newForm->get('stayOnPage')->getData() != "false" || $this->request->get('submitAndStay') !== null) {
  299. return $this->redirectToRoute('easyadmin', ['entity' => 'ProductFamily', 'action' => 'edit', 'id' => $entity->getId()]);
  300. } else {
  301. return $this->redirectToReferrer();
  302. }
  303. }
  304. $this->dispatch(EasyAdminEvents::POST_NEW, [
  305. 'entity_fields' => $fields,
  306. 'form' => $newForm,
  307. 'entity' => $entity
  308. ]);
  309. $productCategoryRepository = $this->getDoctrine()->getRepository(ProductCategoryInterface::class);
  310. $categories = $productCategoryRepository->findAllParents();
  311. $parameters = [
  312. 'form' => $newForm->createView(),
  313. 'entity_fields' => $fields,
  314. 'entity' => $entity,
  315. 'categories' => $categories,
  316. 'sortableProductsField' => array(),
  317. 'totalProductOrdered' => array()
  318. ];
  319. return $this->executeDynamicMethod('render<EntityName>Template', ['new', $this->entity['templates']['new'], $parameters]);
  320. }
  321. public function duplicateAction()
  322. {
  323. $id = $this->request->query->get('id');
  324. $refererUrl = $this->request->query->get('referer', '');
  325. $easyadmin = $this->request->attributes->get('easyadmin');
  326. $entity = $this->em->getRepository($easyadmin['entity']['class'])->find($id);
  327. $newProductFamily = clone $entity;
  328. if ($easyadmin['entity']['name'] == "ProductFamily") {
  329. $this->processProducts($newProductFamily, true);
  330. }
  331. $this->em->persist($newProductFamily);
  332. $this->em->flush();
  333. return $this->redirectToRoute('easyadmin', ['entity' => $easyadmin['entity']['name'], 'action' => 'edit', 'id' => $newProductFamily->getId(), 'referer' => $refererUrl]);
  334. }
  335. }