namespace Lc\ShopBundle\Price\Services; | namespace Lc\ShopBundle\Price\Services; | ||||
use Lc\ShopBundle\Context\OrderProductInterface; | |||||
use Lc\ShopBundle\Context\ProductPropertyInterface; | |||||
trait PriceUtilsTrait | trait PriceUtilsTrait | ||||
{ | { | ||||
public function applyTax($price, $taxRateValue) | public function applyTax($price, $taxRateValue) | ||||
{ | { | ||||
return round((($price * 100)) / 100, 2); | return round((($price * 100)) / 100, 2); | ||||
} | } | ||||
} | |||||
public function applyReductionCatalog($entity, $price, $priceWithTax, $reductionCatalog = null, $withTax = true): ?float | |||||
{ | |||||
if ($reductionCatalog) { | |||||
$reductionCatalogValue = $reductionCatalog->getValue(); | |||||
$reductionCatalogUnit = $reductionCatalog->getUnit(); | |||||
$reductionCatalogBehaviorTaxRate = $reductionCatalog->getBehaviorTaxRate(); | |||||
} else { | |||||
if ($entity instanceof ProductPropertyInterface) { | |||||
$reductionCatalog = $entity->getReductionCatalogInherited(); | |||||
if ($reductionCatalog) { | |||||
$reductionCatalogValue = $reductionCatalog->getValue(); | |||||
$reductionCatalogUnit = $reductionCatalog->getUnit(); | |||||
$reductionCatalogBehaviorTaxRate = $reductionCatalog->getBehaviorTaxRate(); | |||||
} | |||||
} | |||||
if ($entity instanceof OrderProductInterface) { | |||||
$orderProductReductionCatalog = $entity->getOrderProductReductionCatalog(); | |||||
if ($orderProductReductionCatalog) { | |||||
$reductionCatalogValue = $orderProductReductionCatalog->getValue(); | |||||
$reductionCatalogUnit = $orderProductReductionCatalog->getUnit(); | |||||
$reductionCatalogBehaviorTaxRate = $orderProductReductionCatalog->getBehaviorTaxRate(); | |||||
} | |||||
} | |||||
} | |||||
if (isset($reductionCatalogValue) && isset($reductionCatalogUnit) && isset($reductionCatalogBehaviorTaxRate)) { | |||||
if ($reductionCatalogUnit == 'percent') { | |||||
$priceWithTax = $this->applyReductionPercent( | |||||
$priceWithTax, | |||||
$reductionCatalogValue | |||||
); | |||||
} elseif ($reductionCatalogUnit == 'amount') { | |||||
if ($reductionCatalogBehaviorTaxRate == 'tax-excluded') { | |||||
$priceWithTax = $this->applyTax( | |||||
$this->applyReductionAmount( | |||||
$price, | |||||
$reductionCatalogValue | |||||
), | |||||
$entity->getTaxRateInherited()->getValue() | |||||
); | |||||
} elseif ($reductionCatalogBehaviorTaxRate == 'tax-included') { | |||||
$priceWithTax = $this->applyReductionAmount( | |||||
$priceWithTax, | |||||
$reductionCatalogValue | |||||
); | |||||
} | |||||
} | |||||
} | |||||
if ($withTax) { | |||||
$priceReturn = $priceWithTax; | |||||
} else { | |||||
$priceReturn = $this->applyPercentNegative($priceWithTax, $entity->getTaxRateInherited()->getValue()); | |||||
} | |||||
return $this->round($priceReturn); | |||||
} | |||||
} |
); | ); | ||||
} | } | ||||
public function getPriceWithTaxAndReduction(ProductPropertyInterface $product) | |||||
{ | |||||
} | |||||
public function getPriceByRefUnit(ProductPropertyInterface $product) | public function getPriceByRefUnit(ProductPropertyInterface $product) | ||||
{ | { | ||||
); | ); | ||||
} | } | ||||
public function getPriceWithTaxAndReduction(ProductPropertyInterface $product) | |||||
{ | |||||
return $this->applyReductionCatalog( | |||||
$product, | |||||
$this->getTotal($product), | |||||
$this->getTotalWithTax($product) | |||||
); | |||||
} | |||||
public function getPriceByRefUnitWithTaxAndReduction(ProductPropertyInterface $product) | public function getPriceByRefUnitWithTaxAndReduction(ProductPropertyInterface $product) | ||||
{ | { | ||||
return $this->applyReductionCatalog( | |||||
$product, | |||||
$this->getPriceByRefUnit($product), | |||||
$this->getPriceByRefUnitWithTax($product) | |||||
); | |||||
} | } | ||||
} | } | ||||
namespace Lc\ShopBundle\Services ; | namespace Lc\ShopBundle\Services ; | ||||
use Lc\ShopBundle\Price\Services\ProductPriceUtils; | |||||
class ProductFamilyUtils | class ProductFamilyUtils | ||||
{ | { | ||||
protected $priceUtils ; | |||||
protected $productPriceUtils ; | |||||
public function __construct(PriceUtils $priceUtils) | |||||
public function __construct(ProductPriceUtils $productPriceUtils) | |||||
{ | { | ||||
$this->priceUtils = $priceUtils ; | |||||
$this->productPriceUtils = $productPriceUtils ; | |||||
} | } | ||||
public function getCheapestProduct($productFamily) | public function getCheapestProduct($productFamily) | ||||
{ | { | ||||
$priceUtils = $this->priceUtils ; | |||||
return $this->getCheapestOrMostExpensiveProduct($productFamily, function ($a, $b) use ($priceUtils) { | |||||
return $priceUtils->getPriceWithTaxAndReduction($a) > $priceUtils->getPriceWithTaxAndReduction($b) ; | |||||
$productPriceUtils = $this->productPriceUtils ; | |||||
return $this->getCheapestOrMostExpensiveProduct($productFamily, function ($a, $b) use ($productPriceUtils) { | |||||
return $productPriceUtils->getPriceWithTaxAndReduction($a) > $productPriceUtils->getPriceWithTaxAndReduction($b) ; | |||||
}, true); | }, true); | ||||
} | } | ||||
public function getCheapestProductByRefUnit($productFamily) | public function getCheapestProductByRefUnit($productFamily) | ||||
{ | { | ||||
$priceUtils = $this->priceUtils ; | |||||
return $this->getCheapestOrMostExpensiveProduct($productFamily, function ($a, $b) use ($priceUtils) { | |||||
return $priceUtils->getPriceByRefUnitWithTaxAndReduction($a) > $priceUtils->getPriceByRefUnitWithTaxAndReduction($b) ; | |||||
$productPriceUtils = $this->productPriceUtils ; | |||||
return $this->getCheapestOrMostExpensiveProduct($productFamily, function ($a, $b) use ($productPriceUtils) { | |||||
return $productPriceUtils->getPriceByRefUnitWithTaxAndReduction($a) > $productPriceUtils->getPriceByRefUnitWithTaxAndReduction($b) ; | |||||
}, false); | }, false); | ||||
} | } | ||||
public function getMostExpensiveProductByRefUnit($productFamily) | public function getMostExpensiveProductByRefUnit($productFamily) | ||||
{ | { | ||||
$priceUtils = $this->priceUtils ; | |||||
return $this->getCheapestOrMostExpensiveProduct($productFamily, function ($a, $b) use ($priceUtils) { | |||||
return $priceUtils->getPriceByRefUnitWithTaxAndReduction($a) < $priceUtils->getPriceByRefUnitWithTaxAndReduction($b) ; | |||||
$productPriceUtils = $this->productPriceUtils ; | |||||
return $this->getCheapestOrMostExpensiveProduct($productFamily, function ($a, $b) use ($productPriceUtils) { | |||||
return $productPriceUtils->getPriceByRefUnitWithTaxAndReduction($a) < $productPriceUtils->getPriceByRefUnitWithTaxAndReduction($b) ; | |||||
}, false); | }, false); | ||||
} | } | ||||