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 18KB

пре 4 година
пре 4 година
пре 4 година
пре 4 година
пре 4 година
пре 4 година
пре 4 година
пре 4 година
пре 4 година
пре 4 година
пре 4 година
пре 4 година
пре 4 година
пре 4 година
пре 4 година
пре 4 година
пре 4 година
пре 4 година
пре 4 година
пре 4 година
пре 4 година
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  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\ProductCategoryInterface;
  6. use Lc\ShopBundle\Context\ProductFamilyInterface;
  7. use Lc\ShopBundle\Context\ReductionCatalogInterface;
  8. use Lc\ShopBundle\Context\TaxRateInterface;
  9. use Lc\ShopBundle\Form\Backend\Common\ReductionCatalogType;
  10. use Lc\ShopBundle\Form\Backend\ProductFamily\ProductType;
  11. use Lc\ShopBundle\Model\ProductFamily;
  12. use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
  13. use Symfony\Component\Form\Extension\Core\Type\CollectionType;
  14. use Symfony\Component\Form\Extension\Core\Type\NumberType;
  15. use Symfony\Component\HttpFoundation\Response;
  16. class ProductFamilyController extends AdminController
  17. {
  18. private $taxRateClass;
  19. private $choicesTaxRateParam;
  20. private $choicesSupplierTaxRateParam;
  21. public function createEntityFormBuilder($entity, $view, $override = true)
  22. {
  23. $formBuilder = parent::createEntityFormBuilder($entity, $view, false);
  24. $class = $this->em->getClassMetadata(ProductCategoryInterface::class);
  25. $this->taxRateClass = $this->em->getClassMetadata(TaxRateInterface::class);
  26. //$formBuilder->add('productCategories', ProductFamilyCategoriesType::class);
  27. $reductionCatalogRepo = $this->em->getRepository(ReductionCatalogInterface::class);
  28. $reductionCatalogClass = $this->em->getClassMetadata(ReductionCatalogInterface::class);
  29. $reductionCatalog = $reductionCatalogRepo->findOneBy(array('status'=>false, 'productFamily'=>$entity));
  30. if($reductionCatalog == null)$reductionCatalog = new $reductionCatalogClass->name;
  31. $formBuilder->add('reductionCatalog', ReductionCatalogType::class,array(
  32. 'mapped'=>false,
  33. 'data'=> $reductionCatalog
  34. ));
  35. $formBuilder->add('warningMessageType', ChoiceType::class, array(
  36. 'choices' => array(
  37. 'field.default.warningMessageTypeOptions.'.ProductFamily::WARNING_MESSAGE_TYPE_SUCCESS => ProductFamily::WARNING_MESSAGE_TYPE_SUCCESS,
  38. 'field.default.warningMessageTypeOptions.'.ProductFamily::WARNING_MESSAGE_TYPE_ERROR => ProductFamily::WARNING_MESSAGE_TYPE_ERROR,
  39. 'field.default.warningMessageTypeOptions.'.ProductFamily::WARNING_MESSAGE_TYPE_WARNING => ProductFamily::WARNING_MESSAGE_TYPE_WARNING,
  40. 'field.default.warningMessageTypeOptions.'.ProductFamily::WARNING_MESSAGE_TYPE_INFO => ProductFamily::WARNING_MESSAGE_TYPE_INFO
  41. ),
  42. 'translation_domain' => 'lcshop',
  43. 'multiple' => false,
  44. 'expanded' => false,
  45. 'required' => false
  46. ));
  47. $formBuilder->add('behaviorCountStock', ChoiceType::class, array(
  48. 'empty_data' => ProductFamily::BEHAVIOR_COUNT_STOCK_BY_PRODUCT_FAMILY,
  49. 'choices' => array(
  50. 'field.ProductFamily.behaviorCountStockOptions.' . ProductFamily::BEHAVIOR_COUNT_STOCK_BY_MEASURE => ProductFamily::BEHAVIOR_COUNT_STOCK_BY_MEASURE,
  51. 'field.ProductFamily.behaviorCountStockOptions.' . ProductFamily::BEHAVIOR_COUNT_STOCK_BY_PRODUCT_FAMILY => ProductFamily::BEHAVIOR_COUNT_STOCK_BY_PRODUCT_FAMILY,
  52. 'field.ProductFamily.behaviorCountStockOptions.' . ProductFamily::BEHAVIOR_COUNT_STOCK_BY_PRODUCT => ProductFamily::BEHAVIOR_COUNT_STOCK_BY_PRODUCT,
  53. 'field.ProductFamily.behaviorCountStockOptions.' . ProductFamily::BEHAVIOR_COUNT_STOCK_UNLIMITED => ProductFamily::BEHAVIOR_COUNT_STOCK_UNLIMITED
  54. ),
  55. 'translation_domain' => 'lcshop',
  56. 'multiple' => false,
  57. 'expanded' => true
  58. ));
  59. $formBuilder->add('behaviorAddToCart', ChoiceType::class, array(
  60. 'data' => $entity->getBehaviorAddToCart() ? $entity->getBehaviorAddToCart() : 'simple',
  61. 'choices' => array(
  62. 'field.ProductFamily.behaviorAddToCartOptions.'.ProductFamily::BEHAVIOR_ADD_TO_CART_SIMPLE => ProductFamily::BEHAVIOR_ADD_TO_CART_SIMPLE,
  63. 'field.ProductFamily.behaviorAddToCartOptions.'.ProductFamily::BEHAVIOR_ADD_TO_CART_MULTIPLE => ProductFamily::BEHAVIOR_ADD_TO_CART_MULTIPLE
  64. ),
  65. 'translation_domain' => 'lcshop',
  66. 'multiple' => false,
  67. 'expanded' => true
  68. ));
  69. $formBuilder->add('behaviorPrice', ChoiceType::class, array(
  70. 'empty_data' => 'by-piece',
  71. 'choices' => array(
  72. 'field.ProductFamily.behaviorPriceOptions.'.ProductFamily::BEHAVIOR_PRICE_BY_PIECE => ProductFamily::BEHAVIOR_PRICE_BY_PIECE,
  73. 'field.ProductFamily.behaviorPriceOptions.'.ProductFamily::BEHAVIOR_PRICE_BY_REFERENCE_UNIT => ProductFamily::BEHAVIOR_PRICE_BY_REFERENCE_UNIT
  74. ),
  75. 'translation_domain' => 'lcshop',
  76. 'multiple' => false,
  77. 'expanded' => true
  78. ));
  79. $formBuilder->add('multiplyingFactor', NumberType::class, array(
  80. 'mapped'=> false,
  81. 'data'=> floatval($this->merchantUtils->getMerchantConfig('multiplying-factor')),
  82. ));
  83. $formBuilder->add('propertyOrganicLabel', ChoiceType::class, array(
  84. 'choices' => array(
  85. 'field.ProductFamily.organicLabelOptions.'.ProductFamily::PROPERTY_ORGANIC_LABEL_AB => ProductFamily::PROPERTY_ORGANIC_LABEL_AB,
  86. 'field.ProductFamily.organicLabelOptions.'.ProductFamily::PROPERTY_ORGANIC_LABEL_NP => ProductFamily::PROPERTY_ORGANIC_LABEL_NP,
  87. 'field.ProductFamily.organicLabelOptions.'.ProductFamily::PROPERTY_ORGANIC_LABEL_HVE => ProductFamily::PROPERTY_ORGANIC_LABEL_HVE
  88. ),
  89. 'translation_domain' => 'lcshop',
  90. 'multiple' => false,
  91. 'expanded' => false,
  92. 'required' => false
  93. ));
  94. $formBuilder->add('typeExpirationDate', ChoiceType::class, array(
  95. 'choices' => array(
  96. 'field.default.'.ProductFamily::TYPE_EXPIRATION_DATE_DLC => ProductFamily::TYPE_EXPIRATION_DATE_DLC,
  97. 'field.default.'.ProductFamily::TYPE_EXPIRATION_DATE_DDM => ProductFamily::TYPE_EXPIRATION_DATE_DDM,
  98. 'field.default.'.ProductFamily::TYPE_EXPIRATION_DATE_DLUO => ProductFamily::TYPE_EXPIRATION_DATE_DLUO
  99. ),
  100. 'translation_domain' => 'lcshop',
  101. 'multiple' => false,
  102. 'expanded' => true,
  103. 'required'=>false
  104. ));
  105. $formBuilder->add('behaviorStockWeek', ChoiceType::class, array(
  106. 'choices' => array(
  107. 'field.ProductFamily.behaviorStockWeekOptions.'.ProductFamily::BEHAVIOR_STOCK_WEEK_RENEWABLE => ProductFamily::BEHAVIOR_STOCK_WEEK_RENEWABLE,
  108. 'field.ProductFamily.behaviorStockWeekOptions.'.ProductFamily::BEHAVIOR_STOCK_WEEK_RENEWABLE_VALIDATION => ProductFamily::BEHAVIOR_STOCK_WEEK_RENEWABLE_VALIDATION,
  109. 'field.ProductFamily.behaviorStockWeekOptions.'.ProductFamily::BEHAVIOR_STOCK_WEEK_NON_RENEWABLE => ProductFamily::BEHAVIOR_STOCK_WEEK_NON_RENEWABLE
  110. ),
  111. 'translation_domain' => 'lcshop',
  112. 'multiple' => false,
  113. 'expanded' => true,
  114. 'required'=>true
  115. ));
  116. $formBuilder->add('behaviorExpirationDate', ChoiceType::class, array(
  117. 'choices' => array(
  118. 'field.ProductFamily.behaviorExpirationDateOptions.'.ProductFamily::BEHAVIOR_EXPIRATION_DATE_BY_PRODUCT_FAMILY => ProductFamily::BEHAVIOR_EXPIRATION_DATE_BY_PRODUCT_FAMILY,
  119. 'field.ProductFamily.behaviorExpirationDateOptions.'.ProductFamily::BEHAVIOR_EXPIRATION_DATE_BY_PRODUCT => ProductFamily::BEHAVIOR_EXPIRATION_DATE_BY_PRODUCT
  120. ),
  121. 'translation_domain' => 'lcshop',
  122. 'multiple' => false,
  123. 'expanded' => true,
  124. 'required'=>false
  125. ));
  126. $formBuilder->add('products', CollectionType::class, array(
  127. 'label' => 'Déclinaisons',
  128. 'entry_type' => ProductType::class,
  129. 'entry_options' => ['label' => false],
  130. 'allow_add' => true,
  131. 'allow_delete' => true,
  132. 'required' => true
  133. )
  134. );
  135. $formBuilder = $this->overrideFormBuilder($formBuilder, $entity, $view);
  136. return $formBuilder;
  137. }
  138. public function updateProductFamilyEntity($entity, $editForm)
  139. {
  140. $this->processReductionCatalog($entity, $editForm);
  141. $this->processCategories($entity);
  142. $this->processProducts($entity);
  143. $this->processPrice($entity);
  144. parent::updateEntity($entity);
  145. }
  146. public function persistProductFamilyEntity($entity, $newForm)
  147. {
  148. $this->processReductionCatalog($entity, $newForm);
  149. $this->processCategories($entity);
  150. $this->processProducts($entity);
  151. $this->processPrice($entity);
  152. $this->em->persist($entity);
  153. $this->em->flush();
  154. }
  155. protected function processReductionCatalog($entity, $editForm){
  156. $reductionCatalog = $editForm->get('reductionCatalog')->getData();
  157. if($reductionCatalog instanceof ReductionCatalogInterface ) {
  158. if($reductionCatalog->getValue() && $reductionCatalog->getBehaviorTaxRate() && $reductionCatalog->getUnit()){
  159. $reductionCatalog->setMerchant($entity->getMerchant());
  160. $reductionCatalog->setStatus($editForm->get('activeReductionCatalog')->getData());
  161. $reductionCatalog->setProductFamily($entity);
  162. $this->em->persist($reductionCatalog);
  163. }
  164. }
  165. }
  166. protected function processPrice($entity){
  167. if($entity->getBehaviorPrice()=='by-piece'){
  168. $entity->setPriceByRefUnit(null);
  169. $entity->setBuyingPriceByRefUnit(null);
  170. }else if($entity->getBehaviorPrice()=='by-reference-unit') {
  171. $entity->setPrice(null);
  172. $entity->setBuyingPrice(null);
  173. }
  174. }
  175. protected function processProducts($entity)
  176. {
  177. //si il existe un et un seul produit pour ce product family n'ajoute rien supprime rien
  178. if (count($entity->getProducts()) == 0) {
  179. $product = new Product();
  180. $product->setProductFamily($entity);
  181. $this->em->persist($product);
  182. $entity->addProduct($product);
  183. } else {
  184. foreach ($entity->getProducts() as $i=>$product) {
  185. $product->setProductFamily($entity);
  186. $this->em->persist($product);
  187. $entity->addProduct($product);
  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. } else {
  201. $idCategory = (int)str_replace('category_', '', $key);
  202. }
  203. $category = $productCategoryRepository->find($idCategory);
  204. $entity->addProductCategory($category);
  205. }
  206. }
  207. }
  208. protected function editAction()
  209. {
  210. $this->dispatch(EasyAdminEvents::PRE_EDIT);
  211. $id = $this->request->query->get('id');
  212. $easyadmin = $this->request->attributes->get('easyadmin');
  213. $entity = $easyadmin['item'];
  214. if ($this->request->isXmlHttpRequest() && $property = $this->request->query->get('property')) {
  215. $newValue = 'true' === mb_strtolower($this->request->query->get('newValue'));
  216. $fieldsMetadata = $this->entity['list']['fields'];
  217. if (!isset($fieldsMetadata[$property]) || 'toggle' !== $fieldsMetadata[$property]['dataType']) {
  218. throw new \RuntimeException(sprintf('The type of the "%s" property is not "toggle".', $property));
  219. }
  220. $this->updateEntityProperty($entity, $property, $newValue);
  221. // cast to integer instead of string to avoid sending empty responses for 'false'
  222. return new Response((int)$newValue);
  223. }
  224. $fields = $this->entity['edit']['fields'];
  225. $editForm = $this->executeDynamicMethod('create<EntityName>EditForm', [$entity, $fields]);
  226. $deleteForm = $this->createDeleteForm($this->entity['name'], $id);
  227. $sortableProductsField = array();
  228. foreach ($editForm->get('products')->getData() as $k => $product) {
  229. $sortableProductsField[$product->getPosition()] = $k;
  230. }
  231. ksort($sortableProductsField);
  232. $editForm->handleRequest($this->request);
  233. if ($editForm->isSubmitted() && $editForm->isValid()) {
  234. $this->processUploadedFiles($editForm);
  235. $this->dispatch(EasyAdminEvents::PRE_UPDATE, ['entity' => $entity]);
  236. $this->executeDynamicMethod('update<EntityName>Entity', [$entity, $editForm]);
  237. $this->dispatch(EasyAdminEvents::POST_UPDATE, ['entity' => $entity]);
  238. return $this->redirectToReferrer();
  239. }
  240. $this->dispatch(EasyAdminEvents::POST_EDIT);
  241. $parameters = [
  242. 'form' => $editForm->createView(),
  243. 'entity_fields' => $fields,
  244. 'entity' => $entity,
  245. 'delete_form' => $deleteForm->createView(),
  246. 'sortableProductsField' => $sortableProductsField
  247. ];
  248. return $this->executeDynamicMethod('render<EntityName>Template', ['edit', $this->entity['templates']['edit'], $parameters]);
  249. }
  250. protected function newAction()
  251. {
  252. $this->dispatch(EasyAdminEvents::PRE_NEW);
  253. $entity = $this->executeDynamicMethod('createNew<EntityName>Entity');
  254. $easyadmin = $this->request->attributes->get('easyadmin');
  255. $easyadmin['item'] = $entity;
  256. $this->request->attributes->set('easyadmin', $easyadmin);
  257. $fields = $this->entity['new']['fields'];
  258. $newForm = $this->executeDynamicMethod('create<EntityName>NewForm', [$entity, $fields]);
  259. $newForm->handleRequest($this->request);
  260. if ($newForm->isSubmitted() && $newForm->isValid()) {
  261. $this->processUploadedFiles($newForm);
  262. $this->dispatch(EasyAdminEvents::PRE_PERSIST, ['entity' => $entity]);
  263. $this->executeDynamicMethod('persist<EntityName>Entity', [$entity, $newForm]);
  264. $this->dispatch(EasyAdminEvents::POST_PERSIST, ['entity' => $entity]);
  265. return $this->redirectToReferrer();
  266. }
  267. $this->dispatch(EasyAdminEvents::POST_NEW, [
  268. 'entity_fields' => $fields,
  269. 'form' => $newForm,
  270. 'entity' => $entity
  271. ]);
  272. $productCategoryRepository = $this->getDoctrine()->getRepository(ProductCategoryInterface::class);
  273. $categories = $productCategoryRepository->findAllParents();
  274. $parameters = [
  275. 'form' => $newForm->createView(),
  276. 'entity_fields' => $fields,
  277. 'entity' => $entity,
  278. 'categories' => $categories,
  279. 'sortableProductsField' => array()
  280. ];
  281. return $this->executeDynamicMethod('render<EntityName>Template', ['new', $this->entity['templates']['new'], $parameters]);
  282. }
  283. }