您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

145 行
5.4KB

  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. if($entity->getId()) {
  69. //Récupère le product origin
  70. $originProducts = $this->productContainer->getStore()->getOriginByProductFamily($entity);
  71. if (count($originProducts) > 1) {
  72. throw new \ErrorException('Plusieurs OriginProduct pour un même produit... Contacter fab');
  73. // Case Nouveau product family
  74. } else {
  75. if (count($originProducts) == 0) {
  76. $entityClassName = $this->em->getEntityName(ProductInterface::class);
  77. $originProduct = new $entityClassName();
  78. $originProduct->setProductFamily($entity);
  79. $originProduct->setOriginProduct(true);
  80. $entity->addProduct($originProduct);
  81. } else {
  82. $originProduct = $originProducts[0];
  83. }
  84. }
  85. if ($entity->getActiveProducts()) {
  86. $originProduct->setStatus(-1);
  87. } else {
  88. $originProduct->setStatus(1);
  89. }
  90. //Enregistrement
  91. $entity->addProduct($originProduct);
  92. foreach ($entity->getProducts() as $product) {
  93. $product->setProductFamily($entity);
  94. if ($entity->getProductsQuantityAsTitle() && $product->getStatus() >= 1) {
  95. $product->setTitle(
  96. str_replace('.', ',', $this->productContainer->getSolver()->getQuantityInherited($product)) . $this->productContainer->getSolver()->getUnitInherited($product)->getWording()
  97. );
  98. }
  99. $this->em->persist($product);
  100. $entity->addProduct($product);
  101. }
  102. }
  103. }
  104. /* protected function processCategories(ProductFamilyInterface $entity)
  105. {
  106. $productCategoryRepository = $this->em->getRepository(ProductCategoryInterface::class);
  107. $productCategories = $entity->getProductCategories();
  108. $entity->initProductCategories();
  109. foreach ($productCategories as $key => $bool) {
  110. if (is_bool($bool) && $bool) {
  111. if (strpos($key, 'category_children_') !== false) {
  112. $idCategory = (int)str_replace('category_children_', '', $key);
  113. } else {
  114. $idCategory = (int)str_replace('category_', '', $key);
  115. }
  116. $category = $productCategoryRepository->find($idCategory);
  117. $entity->addProductCategory($category);
  118. }
  119. }
  120. }*/
  121. }