@@ -2,6 +2,9 @@ | |||
namespace Lc\ShopBundle\Price\Services; | |||
use Lc\ShopBundle\Context\OrderProductInterface; | |||
use Lc\ShopBundle\Context\ProductPropertyInterface; | |||
trait PriceUtilsTrait | |||
{ | |||
public function applyTax($price, $taxRateValue) | |||
@@ -33,4 +36,64 @@ trait PriceUtilsTrait | |||
{ | |||
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); | |||
} | |||
} |
@@ -34,10 +34,6 @@ class ProductPriceUtils | |||
); | |||
} | |||
public function getPriceWithTaxAndReduction(ProductPropertyInterface $product) | |||
{ | |||
} | |||
public function getPriceByRefUnit(ProductPropertyInterface $product) | |||
{ | |||
@@ -56,9 +52,26 @@ class ProductPriceUtils | |||
); | |||
} | |||
public function getPriceWithTaxAndReduction(ProductPropertyInterface $product) | |||
{ | |||
return $this->applyReductionCatalog( | |||
$product, | |||
$this->getTotal($product), | |||
$this->getTotalWithTax($product) | |||
); | |||
} | |||
public function getPriceByRefUnitWithTaxAndReduction(ProductPropertyInterface $product) | |||
{ | |||
return $this->applyReductionCatalog( | |||
$product, | |||
$this->getPriceByRefUnit($product), | |||
$this->getPriceByRefUnitWithTax($product) | |||
); | |||
} | |||
} | |||
@@ -2,36 +2,38 @@ | |||
namespace Lc\ShopBundle\Services ; | |||
use Lc\ShopBundle\Price\Services\ProductPriceUtils; | |||
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) | |||
{ | |||
$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); | |||
} | |||
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); | |||
} | |||
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); | |||
} | |||