|
|
@@ -1,6 +1,6 @@ |
|
|
|
<?php |
|
|
|
|
|
|
|
namespace Lc\ShopBundle\Services ; |
|
|
|
namespace Lc\ShopBundle\Services; |
|
|
|
|
|
|
|
use App\Entity\Product; |
|
|
|
use Doctrine\ORM\EntityManagerInterface; |
|
|
@@ -13,62 +13,62 @@ use Lc\ShopBundle\Model\ProductFamily; |
|
|
|
|
|
|
|
class ProductFamilyUtils |
|
|
|
{ |
|
|
|
protected $priceUtils ; |
|
|
|
protected $em ; |
|
|
|
protected $priceUtils; |
|
|
|
protected $em; |
|
|
|
|
|
|
|
|
|
|
|
public function __construct(PriceUtilsInterface $priceUtils, EntityManagerInterface $em) |
|
|
|
{ |
|
|
|
$this->priceUtils = $priceUtils ; |
|
|
|
$this->priceUtils = $priceUtils; |
|
|
|
$this->em = $em; |
|
|
|
} |
|
|
|
|
|
|
|
public function getCheapestProduct($productFamily) |
|
|
|
{ |
|
|
|
$priceUtils = $this->priceUtils ; |
|
|
|
$priceUtils = $this->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); |
|
|
|
} |
|
|
|
|
|
|
|
public function getCheapestProductByRefUnit($productFamily) |
|
|
|
{ |
|
|
|
$priceUtils = $this->priceUtils ; |
|
|
|
$priceUtils = $this->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); |
|
|
|
} |
|
|
|
|
|
|
|
public function getMostExpensiveProductByRefUnit($productFamily) |
|
|
|
{ |
|
|
|
$priceUtils = $this->priceUtils ; |
|
|
|
$priceUtils = $this->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); |
|
|
|
} |
|
|
|
|
|
|
|
private function getCheapestOrMostExpensiveProduct($productFamily, $comparisonFunction, $returnSelfIfNotActiveProducts) |
|
|
|
{ |
|
|
|
if($productFamily->getActiveProducts()) { |
|
|
|
if ($productFamily->getActiveProducts()) { |
|
|
|
$products = $productFamily->getProductsOnline()->getValues(); |
|
|
|
if (count($products) > 0) { |
|
|
|
usort($products, $comparisonFunction); |
|
|
|
return $products[0]; |
|
|
|
} |
|
|
|
}else{ |
|
|
|
} else { |
|
|
|
return $productFamily->getOriginProduct(); |
|
|
|
} |
|
|
|
if ($returnSelfIfNotActiveProducts) { |
|
|
|
return $productFamily; |
|
|
|
} |
|
|
|
else { |
|
|
|
} else { |
|
|
|
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->processCategories($productFamily); |
|
|
|
} |
|
|
@@ -102,16 +102,21 @@ class ProductFamilyUtils |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
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) |
|
|
|
{ |
|
|
|
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 |
|
|
|
$originProducts = $this->em->getRepository(ProductInterface::class)->findBy(array( |
|
|
|
'productFamily' => $entity->getId(), |
|
|
@@ -144,8 +149,8 @@ class ProductFamilyUtils |
|
|
|
foreach ($entity->getProducts() as $product) { |
|
|
|
$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); |