|
- <?php
-
- namespace Lc\ShopBundle\Services ;
-
- use App\Entity\Product;
- use Doctrine\ORM\EntityManagerInterface;
- use Lc\ShopBundle\Context\PriceUtilsInterface;
- use Lc\ShopBundle\Context\ProductCategoryInterface;
- use Lc\ShopBundle\Context\ProductFamilyInterface;
- use Lc\ShopBundle\Context\ReductionCatalogInterface;
- use Lc\ShopBundle\Model\ProductFamily;
-
- class ProductFamilyUtils
- {
- protected $priceUtils ;
- protected $em ;
-
- public function __construct(PriceUtilsInterface $priceUtils, EntityManagerInterface $em)
- {
- $this->priceUtils = $priceUtils ;
- $this->em = $em;
- }
-
- public function getCheapestProduct($productFamily)
- {
- $priceUtils = $this->priceUtils ;
- return $this->getCheapestOrMostExpensiveProduct($productFamily, function ($a, $b) use ($priceUtils) {
- return $priceUtils->getPriceWithTaxAndReduction($a) > $priceUtils->getPriceWithTaxAndReduction($b) ;
- }, true);
- }
-
- public function getCheapestProductByRefUnit($productFamily)
- {
- $priceUtils = $this->priceUtils ;
- return $this->getCheapestOrMostExpensiveProduct($productFamily, function ($a, $b) use ($priceUtils) {
- return $priceUtils->getPriceByRefUnitWithTaxAndReduction($a) > $priceUtils->getPriceByRefUnitWithTaxAndReduction($b) ;
- }, false);
- }
-
- public function getMostExpensiveProductByRefUnit($productFamily)
- {
- $priceUtils = $this->priceUtils ;
- return $this->getCheapestOrMostExpensiveProduct($productFamily, function ($a, $b) use ($priceUtils) {
- return $priceUtils->getPriceByRefUnitWithTaxAndReduction($a) < $priceUtils->getPriceByRefUnitWithTaxAndReduction($b) ;
- }, false);
- }
-
- private function getCheapestOrMostExpensiveProduct($productFamily, $comparisonFunction, $returnSelfIfNotActiveProducts)
- {
- $products = $productFamily->getProductsOnline()->getValues() ;
- if (count($products) > 0) {
- usort($products, $comparisonFunction);
- return $products[0];
- }
- if ($returnSelfIfNotActiveProducts) {
- return $productFamily;
- }
- else {
- return false;
- }
- }
-
-
-
- public function processBeforePersistProductFamily($productFamily, $editForm=false, $clone =false){
- if($editForm){
- $this->processReductionCatalog($productFamily, $editForm);
- $this->processCategories($productFamily);
- }
- $this->processProducts($productFamily, $clone);
- $this->processPrice($productFamily);
-
- return $productFamily;
- }
-
- protected function processReductionCatalog($entity, $editForm)
- {
- $reductionCatalog = $editForm->get('reductionCatalog')->getData();
- if ($reductionCatalog instanceof ReductionCatalogInterface) {
- if ($reductionCatalog->getValue() && $reductionCatalog->getBehaviorTaxRate() && $reductionCatalog->getUnit()) {
- $reductionCatalog->setMerchant($entity->getMerchant());
- $reductionCatalog->setStatus($editForm->get('activeReductionCatalog')->getData());
- $reductionCatalog->setProductFamily($entity);
- $this->em->persist($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, $clone = false)
- {
- //si il existe un et un seul produit pour ce product family n'ajoute rien supprime rien
- if (count($entity->getProducts()) == 0) {
- $product = new Product();
- $product->setProductFamily($entity);
- $this->em->persist($product);
- $entity->addProduct($product);
- } else {
- foreach ($entity->getProducts() as $i => $product) {
- if ($clone) {
- $newProduct = clone $product;
- $newProduct->setProductFamily($entity);
- $this->em->persist($newProduct);
- $entity->addProduct($newProduct);
- } else {
- $product->setProductFamily($entity);
- $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);
- }
- }
- }
-
- }
|