|
- <?php
-
- namespace Lc\CaracoleBundle\EventSubscriber\Product;
-
- use Doctrine\ORM\EntityManagerInterface;
-
- use Lc\CaracoleBundle\Builder\Order\OrderShopBuilder;
- use Lc\CaracoleBundle\Container\Product\ProductContainer;
- use Lc\CaracoleBundle\Container\Product\ProductFamilyContainer;
- use Lc\CaracoleBundle\Model\Product\ProductFamilyInterface;
- use Lc\CaracoleBundle\Model\Product\ProductInterface;
- use Lc\CaracoleBundle\Model\Reduction\ReductionCatalogInterface;
- use Lc\SovBundle\Event\EntityManager\EntityManagerEvent;
- use Symfony\Component\EventDispatcher\EventSubscriberInterface;
-
- class UpdateProductfamilyEventSubscriber implements EventSubscriberInterface
- {
- protected EntityManagerInterface $em;
- protected ProductFamilyContainer $productFamilyContainer;
- protected ProductContainer $productContainer;
- protected OrderShopBuilder $orderShopBuilder;
-
- public function __construct(EntityManagerInterface $entityManager, ProductFamilyContainer $productFamilyContainer, ProductContainer $productContainer, OrderShopBuilder $orderShopBuilder)
- {
- $this->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);
-
- 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);
- }
- }
- }
-
- /* 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);
- }
- }
- }*/
- }
|