em = $entityManager; } 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); } //TODO updatePriceByProductFamily } 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) { //Récupère le product origin $originProducts = $this->em->getRepository(ProductInterface::class) ->findBy( array( 'productFamily' => $entity->getId(), 'originProduct' => true, ) ); 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('.', ',', $product->getQuantityInherited()).$product->getUnitInherited( )->getWording() ); } $this->em->persist($product); $entity->addProduct($product); } } /* protected function processCategories(ProductFamilyInterface $entity) { $productCategoryRepository = $this->em->getRepository(ProductCategoryInterface::class); $productCategories = $entity->getProductCategories(); $entity->initProductCategories(); foreach ($productCategories as $key => $bool) { if (is_bool($bool) && $bool) { if (strpos($key, 'category_children_') !== false) { $idCategory = (int)str_replace('category_children_', '', $key); } else { $idCategory = (int)str_replace('category_', '', $key); } $category = $productCategoryRepository->find($idCategory); $entity->addProductCategory($category); } } }*/ }