em = $entityManager; $this->productFamilyContainer = $productFamilyContainer; $this->productContainer = $productContainer; $this->orderShopBuilder = $orderShopBuilder; } public static function getSubscribedEvents() { return [ EntityManagerEvent::PRE_UPDATE_EVENT => ['processBeforePersistProductFamily'], EntityManagerEvent::PRE_CREATE_EVENT => ['processBeforePersistProductFamily'], ]; } public function processBeforePersistProductFamily(EntityManagerEvent $event) { $entity = $event->getEntity(); if ($entity instanceof ProductFamilyInterface) { $this->processProducts($entity); $this->processPrice($entity); $this->processReductionCatalog($entity); } } protected function processReductionCatalog($entity) { $reductionCatalog = $entity->getReductionCatalog(); if ($reductionCatalog instanceof ReductionCatalogInterface) { if ($reductionCatalog->getValue() && $reductionCatalog->getBehaviorTaxRate() && $reductionCatalog->getUnit()) { //$reductionCatalog->setSection($entity->getSection()); $reductionCatalog->setProductFamily($entity); if (is_null($reductionCatalog->getId())) { $this->em->create($reductionCatalog); } else { $this->em->update($reductionCatalog); } } } } protected function processPrice($entity) { if ($entity->getBehaviorPrice() == 'by-piece') { $entity->setPriceByRefUnit(null); $entity->setBuyingPriceByRefUnit(null); } else { if ($entity->getBehaviorPrice() == 'by-reference-unit') { $entity->setPrice(null); $entity->setBuyingPrice(null); } } } protected function processProducts($entity) { if ($entity->getId()) { //Récupère le product origin $originProducts = $this->productContainer->getStore()->getOriginByProductFamily($entity); } else { //CAse de création d'un produit $originProducts = array(); } if (count($originProducts) > 1) { throw new \ErrorException('Plusieurs OriginProduct pour un même produit... Contacter fab'); // Case Nouveau product family } else { if (count($originProducts) == 0) { $entityClassName = $this->em->getEntityName(ProductInterface::class); $originProduct = new $entityClassName(); $originProduct->setProductFamily($entity); $originProduct->setOriginProduct(true); $entity->addProduct($originProduct); } else { $originProduct = $originProducts[0]; } } if ($entity->getActiveProducts()) { $originProduct->setStatus(-1); } else { $originProduct->setStatus(1); } //Enregistrement $entity->addProduct($originProduct); foreach ($entity->getProducts() as $product) { $product->setProductFamily($entity); if ($entity->getProductsQuantityAsTitle() && $product->getStatus() >= 1) { $product->setTitle( str_replace('.', ',', $this->productContainer->getSolver()->getQuantityInherited($product)) . $this->productContainer->getSolver()->getUnitInherited($product)->getWording() ); } $this->em->persist($product); $entity->addProduct($product); } } }