|
|
|
|
|
|
|
|
<?php |
|
|
<?php |
|
|
|
|
|
|
|
|
namespace Lc\ShopBundle\Services ; |
|
|
|
|
|
|
|
|
namespace Lc\ShopBundle\Services; |
|
|
|
|
|
|
|
|
use App\Entity\Product; |
|
|
use App\Entity\Product; |
|
|
use Doctrine\ORM\EntityManagerInterface; |
|
|
use Doctrine\ORM\EntityManagerInterface; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class ProductFamilyUtils |
|
|
class ProductFamilyUtils |
|
|
{ |
|
|
{ |
|
|
protected $priceUtils ; |
|
|
|
|
|
protected $em ; |
|
|
|
|
|
|
|
|
protected $priceUtils; |
|
|
|
|
|
protected $em; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public function __construct(PriceUtilsInterface $priceUtils, EntityManagerInterface $em) |
|
|
public function __construct(PriceUtilsInterface $priceUtils, EntityManagerInterface $em) |
|
|
{ |
|
|
{ |
|
|
$this->priceUtils = $priceUtils ; |
|
|
|
|
|
|
|
|
$this->priceUtils = $priceUtils; |
|
|
$this->em = $em; |
|
|
$this->em = $em; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
public function getCheapestProduct($productFamily) |
|
|
public function getCheapestProduct($productFamily) |
|
|
{ |
|
|
{ |
|
|
$priceUtils = $this->priceUtils ; |
|
|
|
|
|
|
|
|
$priceUtils = $this->priceUtils; |
|
|
return $this->getCheapestOrMostExpensiveProduct($productFamily, function ($a, $b) use ($priceUtils) { |
|
|
return $this->getCheapestOrMostExpensiveProduct($productFamily, function ($a, $b) use ($priceUtils) { |
|
|
return $priceUtils->getPriceWithTaxAndReduction($a) > $priceUtils->getPriceWithTaxAndReduction($b) ; |
|
|
|
|
|
|
|
|
return $priceUtils->getPriceWithTaxAndReduction($a) > $priceUtils->getPriceWithTaxAndReduction($b); |
|
|
}, true); |
|
|
}, true); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
public function getCheapestProductByRefUnit($productFamily) |
|
|
public function getCheapestProductByRefUnit($productFamily) |
|
|
{ |
|
|
{ |
|
|
$priceUtils = $this->priceUtils ; |
|
|
|
|
|
|
|
|
$priceUtils = $this->priceUtils; |
|
|
return $this->getCheapestOrMostExpensiveProduct($productFamily, function ($a, $b) use ($priceUtils) { |
|
|
return $this->getCheapestOrMostExpensiveProduct($productFamily, function ($a, $b) use ($priceUtils) { |
|
|
return $priceUtils->getPriceByRefUnitWithTaxAndReduction($a) > $priceUtils->getPriceByRefUnitWithTaxAndReduction($b) ; |
|
|
|
|
|
|
|
|
return $priceUtils->getPriceByRefUnitWithTaxAndReduction($a) > $priceUtils->getPriceByRefUnitWithTaxAndReduction($b); |
|
|
}, false); |
|
|
}, false); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
public function getMostExpensiveProductByRefUnit($productFamily) |
|
|
public function getMostExpensiveProductByRefUnit($productFamily) |
|
|
{ |
|
|
{ |
|
|
$priceUtils = $this->priceUtils ; |
|
|
|
|
|
|
|
|
$priceUtils = $this->priceUtils; |
|
|
return $this->getCheapestOrMostExpensiveProduct($productFamily, function ($a, $b) use ($priceUtils) { |
|
|
return $this->getCheapestOrMostExpensiveProduct($productFamily, function ($a, $b) use ($priceUtils) { |
|
|
return $priceUtils->getPriceByRefUnitWithTaxAndReduction($a) < $priceUtils->getPriceByRefUnitWithTaxAndReduction($b) ; |
|
|
|
|
|
|
|
|
return $priceUtils->getPriceByRefUnitWithTaxAndReduction($a) < $priceUtils->getPriceByRefUnitWithTaxAndReduction($b); |
|
|
}, false); |
|
|
}, false); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
private function getCheapestOrMostExpensiveProduct($productFamily, $comparisonFunction, $returnSelfIfNotActiveProducts) |
|
|
private function getCheapestOrMostExpensiveProduct($productFamily, $comparisonFunction, $returnSelfIfNotActiveProducts) |
|
|
{ |
|
|
{ |
|
|
if($productFamily->getActiveProducts()) { |
|
|
|
|
|
|
|
|
if ($productFamily->getActiveProducts()) { |
|
|
$products = $productFamily->getProductsOnline()->getValues(); |
|
|
$products = $productFamily->getProductsOnline()->getValues(); |
|
|
if (count($products) > 0) { |
|
|
if (count($products) > 0) { |
|
|
usort($products, $comparisonFunction); |
|
|
usort($products, $comparisonFunction); |
|
|
return $products[0]; |
|
|
return $products[0]; |
|
|
} |
|
|
} |
|
|
}else{ |
|
|
|
|
|
|
|
|
} else { |
|
|
return $productFamily->getOriginProduct(); |
|
|
return $productFamily->getOriginProduct(); |
|
|
} |
|
|
} |
|
|
if ($returnSelfIfNotActiveProducts) { |
|
|
if ($returnSelfIfNotActiveProducts) { |
|
|
return $productFamily; |
|
|
return $productFamily; |
|
|
} |
|
|
|
|
|
else { |
|
|
|
|
|
|
|
|
} else { |
|
|
return false; |
|
|
return false; |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public function processBeforePersistProductFamily($productFamily, $editForm=false, $clone =false){ |
|
|
|
|
|
if($editForm){ |
|
|
|
|
|
|
|
|
public function processBeforePersistProductFamily($productFamily, $editForm = false, $clone = false) |
|
|
|
|
|
{ |
|
|
|
|
|
if ($editForm) { |
|
|
$this->processReductionCatalog($productFamily, $editForm); |
|
|
$this->processReductionCatalog($productFamily, $editForm); |
|
|
$this->processCategories($productFamily); |
|
|
$this->processCategories($productFamily); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
protected function processCloneProduct($productFamily) |
|
|
|
|
|
{ |
|
|
|
|
|
foreach ($productFamily->getProducts() as $i => $product) { |
|
|
|
|
|
$newProduct = clone $product; |
|
|
|
|
|
$newProduct->setProductFamily($productFamily); |
|
|
|
|
|
$this->em->persist($newProduct); |
|
|
|
|
|
$productFamily->addProduct($newProduct); |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
protected function processProducts($entity, $clone = false) |
|
|
protected function processProducts($entity, $clone = false) |
|
|
{ |
|
|
{ |
|
|
if($clone) { |
|
|
|
|
|
foreach ($entity->getProducts() as $i => $product) { |
|
|
|
|
|
$newProduct = clone $product; |
|
|
|
|
|
$newProduct->setProductFamily($entity); |
|
|
|
|
|
$this->em->persist($newProduct); |
|
|
|
|
|
$entity->addProduct($newProduct); |
|
|
|
|
|
} |
|
|
|
|
|
}else { |
|
|
|
|
|
|
|
|
if ($clone) { |
|
|
|
|
|
$this->processCloneProduct($entity); |
|
|
|
|
|
} else { |
|
|
//Récupère le product origin |
|
|
//Récupère le product origin |
|
|
$originProducts = $this->em->getRepository(ProductInterface::class)->findBy(array( |
|
|
$originProducts = $this->em->getRepository(ProductInterface::class)->findBy(array( |
|
|
'productFamily' => $entity->getId(), |
|
|
'productFamily' => $entity->getId(), |
|
|
|
|
|
|
|
|
foreach ($entity->getProducts() as $product) { |
|
|
foreach ($entity->getProducts() as $product) { |
|
|
$product->setProductFamily($entity); |
|
|
$product->setProductFamily($entity); |
|
|
|
|
|
|
|
|
if($entity->getProductsQuantityAsTitle() && $product->getStatus()>=1){ |
|
|
|
|
|
$product->setTitle(str_replace('.', ',',$product->getQuantityInherited()).$product->getUnitInherited()->getWording()); |
|
|
|
|
|
|
|
|
if ($entity->getProductsQuantityAsTitle() && $product->getStatus() >= 1) { |
|
|
|
|
|
$product->setTitle(str_replace('.', ',', $product->getQuantityInherited()) . $product->getUnitInherited()->getWording()); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
$this->em->persist($product); |
|
|
$this->em->persist($product); |