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.

147 lines
5.5KB

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