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.

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