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.

UpdateProductfamilyEventSubscriber.php 4.7KB

3 vuotta sitten
3 vuotta sitten
3 vuotta sitten
3 vuotta sitten
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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. } else {
  74. //CAse de création d'un produit
  75. $originProducts = array();
  76. }
  77. if (count($originProducts) > 1) {
  78. throw new \ErrorException('Plusieurs OriginProduct pour un même produit... Contacter fab');
  79. // Case Nouveau product family
  80. } else {
  81. if (count($originProducts) == 0) {
  82. $entityClassName = $this->em->getEntityName(ProductInterface::class);
  83. $originProduct = new $entityClassName();
  84. $originProduct->setProductFamily($entity);
  85. $originProduct->setOriginProduct(true);
  86. $entity->addProduct($originProduct);
  87. } else {
  88. $originProduct = $originProducts[0];
  89. }
  90. }
  91. if ($entity->getActiveProducts()) {
  92. $originProduct->setStatus(-1);
  93. } else {
  94. $originProduct->setStatus(1);
  95. }
  96. //Enregistrement
  97. $entity->addProduct($originProduct);
  98. foreach ($entity->getProducts() as $product) {
  99. $product->setProductFamily($entity);
  100. if ($entity->getProductsQuantityAsTitle() && $product->getStatus() >= 1) {
  101. $product->setTitle(
  102. str_replace('.', ',', $this->productContainer->getSolver()->getQuantityInherited($product)) . $this->productContainer->getSolver()->getUnitInherited($product)->getWording()
  103. );
  104. }
  105. $this->em->persist($product);
  106. $entity->addProduct($product);
  107. }
  108. }
  109. }