|
- <?php
-
- namespace Lc\ShopBundle\Services ;
-
- use Doctrine\Common\Collections\Collection;
- use Lc\ShopBundle\Context\OrderProductInterface;
- use Lc\ShopBundle\Context\OrderShopInterface;
- use Lc\ShopBundle\Context\ProductFamilyInterface;
- use Lc\ShopBundle\Context\ProductInterface;
- use Lc\ShopBundle\Context\ProductPropertyInterface;
- use Lc\ShopBundle\Context\ReductionCatalogInterface;
-
- class PriceUtils
- {
-
- public function getPrice($entity)
- {
- if($entity instanceof ProductPropertyInterface) {
- if($entity->getBehaviorPriceInherited() == 'by-piece') {
- return $entity->getPriceInherited() ;
- }
- elseif($entity->getBehaviorPriceInherited() == 'by-reference-unit') {
- if($entity->getQuantityInherited() > 0) {
- return $entity->getPriceByRefUnitInherited() * ($entity->getQuantityInherited() / $entity->getUnitInherited()->getCoefficient()) ;
- }
- }
- }
-
- if($entity instanceof OrderProductInterface) {
- return $entity->getPrice() ;
- }
-
- return null ;
- }
-
- public function getPriceWithTax($entity)
- {
- return $this->applyTax(
- $this->getPrice($entity),
- $entity->getTaxRateInherited()->getValue()
- ) ;
- }
-
- public function getPriceWithTaxAndReduction($entity)
- {
- return $this->getPriceWithTaxAndReductionCatalog(
- $entity,
- $this->getPrice($entity),
- $this->getPriceWithTax($entity)
- );
- }
-
- public function getPriceByRefUnit($entity)
- {
- if($entity instanceof ProductPropertyInterface) {
- if($entity->getBehaviorPriceInherited() == 'by-piece') {
- return ($this->getPrice($entity) * $entity->getUnitInherited()->getCoefficient()) / $entity->getQuantityInherited() ;
- }
- elseif($entity->getBehaviorPriceInherited() == 'by-reference-unit') {
- return $entity->getPriceByRefUnitInherited() ;
- }
- }
-
- if($entity instanceof OrderProductInterface) {
- return ($this->getPrice($entity) * $entity->getUnitInherited()->getCoefficient()) / $entity->getQuantityProduct() ;
- }
-
- return null ;
- }
-
- public function getPriceByRefUnitWithTax($entity)
- {
- return $this->applyTax(
- $this->getPriceByRefUnit($entity),
- $entity->getTaxRateInherited()->getValue()
- ) ;
- }
-
- public function getPriceByRefUnitWithTaxAndReduction($entity)
- {
- return $this->getPriceWithTaxAndReductionCatalog(
- $entity,
- $this->getPriceByRefUnit($entity),
- $this->getPriceByRefUnitWithTax($entity)
- );
- }
-
- public function getTotal($entity)
- {
- if($entity instanceof OrderProductInterface) {
- return $entity->getQuantityOrder() * $this->getPrice($entity) ;
- }
- if($entity instanceof OrderShopInterface) {
- $total = 0 ;
- foreach($entity->getOrderProducts() as $orderProduct) {
- $total += $this->getTotal($orderProduct) ;
- }
- return $total ;
- }
- return null ;
- }
-
- public function getTotalWithTax($entity)
- {
- if($entity instanceof OrderProductInterface) {
- return $this->applyTax(
- $this->getTotal($entity),
- $entity->getTaxRateInherited()->getValue()
- ) ;
- }
- if($entity instanceof OrderShopInterface) {
- $total = 0 ;
- foreach($entity->getOrderProducts() as $orderProduct) {
- $total += $this->getTotalWithTax($orderProduct) ;
- }
- return $total ;
- }
-
- //C'est bizzare ce truc là
- //if($entity instanceof OrderShopInterface) {
- // return $this->getTotalOrderProducts($entity->getOrderProducts(), true) ;
- //}
- return null ;
- }
-
- public function getTotalWithTaxAndReduction($entity)
- {
- if($entity instanceof OrderProductInterface) {
- return $this->getPriceWithTaxAndReductionCatalog(
- $entity,
- $this->getTotal($entity),
- $this->getTotalWithTax($entity)
- ) ;
- }
-
- if($entity instanceof OrderShopInterface) {
- return $this->getTotalOrderProductsWithTaxAndReduction($entity->getOrderProducts(), true, true) ;
- }
- }
-
- public function getTotalOrderProducts($entity)
- {
- return $this->getSumOrderProductsDispatch($entity) ;
- }
-
- public function getTotalOrderProductsWithTax($entity)
- {
- return $this->getSumOrderProductsDispatch($entity, true) ;
- }
-
- public function getTotalOrderProductsWithTaxAndReduction($entity)
- {
- return $this->getSumOrderProductsDispatch($entity, true, true) ;
- }
-
- public function getSumOrderProductsDispatch($entity, $withTax = false, $withReduction = false)
- {
- if($entity instanceof OrderShopInterface) {
- return $this->getSumOrderProducts($entity->getOrderProducts(), $withTax, $withReduction) ;
- }
- if($entity instanceof Collection || is_array($entity)) {
- return $this->getSumOrderProducts($entity, $withTax, $withReduction) ;
- }
- }
-
- public function getSumOrderProducts($orderProducts, $withTax = false, $withReduction = false)
- {
- $total = 0 ;
- foreach($orderProducts as $orderProduct) {
- if($withTax && $withReduction) {
- $total += $this->getTotalWithTaxAndReduction($orderProduct) ;
- }
- elseif($withTax) {
- $total += $this->getTotalWithTax($orderProduct) ;
- }
- else {
- $total += $this->getTotal($orderProduct) ;
- }
- }
- return $total ;
- }
-
- public function getPriceWithTaxAndReductionCatalog($entity, $price, $priceWithTax, $reductionCatalog = null): ?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
- );
- }
- }
- }
-
- return $this->round($priceWithTax) ;
- }
-
- public function applyTax($price, $taxRateValue)
- {
- return $this->round($this->applyPercent($price, $taxRateValue)) ;
- }
-
- public function applyReductionPercent($price, $percentage)
- {
- return $this->applyPercent($price, -$percentage) ;
- }
-
- public function applyReductionAmount($price, $amount)
- {
- return $price - $amount ;
- }
-
- public function applyPercent($price, $percentage)
- {
- return $price * ($percentage / 100 + 1) ;
- }
-
- public function round($price)
- {
- return round((($price * 100)) / 100, 2);
- }
-
- }
|