Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

UpdateProductfamilyEventSubscriber.php 5.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. <?php
  2. namespace Lc\CaracoleBundle\EventSubscriber\Product;
  3. use Doctrine\ORM\EntityManagerInterface;
  4. use Lc\CaracoleBundle\Container\Product\ProductContainer;
  5. use Lc\CaracoleBundle\Container\Product\ProductFamilyContainer;
  6. use Lc\CaracoleBundle\Model\Product\ProductFamilyInterface;
  7. use Lc\CaracoleBundle\Model\Product\ProductInterface;
  8. use Lc\CaracoleBundle\Model\Reduction\ReductionCatalogInterface;
  9. use Lc\SovBundle\Event\EntityManager\EntityManagerEvent;
  10. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  11. class UpdateProductfamilyEventSubscriber implements EventSubscriberInterface
  12. {
  13. protected EntityManagerInterface $em;
  14. protected ProductFamilyContainer $productFamilyContainer;
  15. protected ProductContainer $productContainer;
  16. public function __construct(EntityManagerInterface $entityManager, ProductFamilyContainer $productFamilyContainer, ProductContainer $productContainer)
  17. {
  18. $this->em = $entityManager;
  19. $this->productFamilyContainer = $productFamilyContainer;
  20. $this->productContainer = $productContainer;
  21. }
  22. public static function getSubscribedEvents()
  23. {
  24. return [
  25. EntityManagerEvent::PRE_UPDATE_EVENT => ['processBeforePersistProductFamily'],
  26. EntityManagerEvent::PRE_CREATE_EVENT => ['processBeforePersistProductFamily'],
  27. ];
  28. }
  29. public function processBeforePersistProductFamily(EntityManagerEvent $event)
  30. {
  31. $entity = $event->getEntity();
  32. if ($entity instanceof ProductFamilyInterface) {
  33. $this->processProducts($entity);
  34. $this->processPrice($entity);
  35. $this->processReductionCatalog($entity);
  36. }
  37. //TODO updatePriceByProductFamily
  38. }
  39. protected function processReductionCatalog($entity)
  40. {
  41. $reductionCatalog = $entity->getReductionCatalog();
  42. if ($reductionCatalog instanceof ReductionCatalogInterface) {
  43. if ($reductionCatalog->getValue() && $reductionCatalog->getBehaviorTaxRate() && $reductionCatalog->getUnit()) {
  44. //$reductionCatalog->setSection($entity->getSection());
  45. $reductionCatalog->setProductFamily($entity);
  46. if (is_null($reductionCatalog->getId())) {
  47. $this->em->create($reductionCatalog);
  48. } else {
  49. $this->em->update($reductionCatalog);
  50. }
  51. }
  52. }
  53. }
  54. protected function processPrice($entity)
  55. {
  56. if ($entity->getBehaviorPrice() == 'by-piece') {
  57. $entity->setPriceByRefUnit(null);
  58. $entity->setBuyingPriceByRefUnit(null);
  59. } else {
  60. if ($entity->getBehaviorPrice() == 'by-reference-unit') {
  61. $entity->setPrice(null);
  62. $entity->setBuyingPrice(null);
  63. }
  64. }
  65. }
  66. protected function processProducts($entity)
  67. {
  68. //Récupère le product origin
  69. $originProducts = $this->em->getRepository(ProductInterface::class)
  70. ->findBy(
  71. array(
  72. 'productFamily' => $entity->getId(),
  73. 'originProduct' => true,
  74. )
  75. );
  76. if (count($originProducts) > 1) {
  77. throw new \ErrorException('Plusieurs OriginProduct pour un même produit... Contacter fab');
  78. // Case Nouveau product family
  79. } else {
  80. if (count($originProducts) == 0) {
  81. $entityClassName = $this->em->getEntityName(ProductInterface::class);
  82. $originProduct = new $entityClassName();
  83. $originProduct->setProductFamily($entity);
  84. $originProduct->setOriginProduct(true);
  85. $entity->addProduct($originProduct);
  86. } else {
  87. $originProduct = $originProducts[0];
  88. }
  89. }
  90. if ($entity->getActiveProducts()) {
  91. $originProduct->setStatus(-1);
  92. } else {
  93. $originProduct->setStatus(1);
  94. }
  95. //Enregistrement
  96. $entity->addProduct($originProduct);
  97. foreach ($entity->getProducts() as $product) {
  98. $product->setProductFamily($entity);
  99. if ($entity->getProductsQuantityAsTitle() && $product->getStatus() >= 1) {
  100. $product->setTitle(
  101. str_replace('.', ',', $this->productContainer->getSolver()->getQuantityInherited($product)) . $this->productContainer->getSolver()->getUnitInherited($product)->getWording()
  102. );
  103. }
  104. $this->em->persist($product);
  105. $entity->addProduct($product);
  106. }
  107. }
  108. /* protected function processCategories(ProductFamilyInterface $entity)
  109. {
  110. $productCategoryRepository = $this->em->getRepository(ProductCategoryInterface::class);
  111. $productCategories = $entity->getProductCategories();
  112. $entity->initProductCategories();
  113. foreach ($productCategories as $key => $bool) {
  114. if (is_bool($bool) && $bool) {
  115. if (strpos($key, 'category_children_') !== false) {
  116. $idCategory = (int)str_replace('category_children_', '', $key);
  117. } else {
  118. $idCategory = (int)str_replace('category_', '', $key);
  119. }
  120. $category = $productCategoryRepository->find($idCategory);
  121. $entity->addProductCategory($category);
  122. }
  123. }
  124. }*/
  125. }