|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144 |
- <?php
-
- namespace Lc\CaracoleBundle\EventSubscriber\Product;
-
- use Doctrine\ORM\EntityManagerInterface;
-
- 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 $em;
- protected $adminUrlGenerator;
-
- public function __construct(EntityManagerInterface $entityManager)
- {
- $this->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);
- }
- }
- }*/
- }
|