|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188 |
- <?php
-
- namespace Lc\ShopBundle\Services ;
-
- use Lc\ShopBundle\Context\OrderProductInterface;
- 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() ;
- }
- }
- }
-
- 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->getPriceInherited($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) ;
- }
- return null ;
- }
-
- public function getTotalWithTax($entity)
- {
- if($entity instanceof OrderProductInterface) {
- return $this->applyTax(
- $this->getTotal($entity),
- $entity->getTaxRateInherited()->getValue()
- ) ;
- }
- return null ;
- }
-
- public function getTotalWithTaxAndReduction($entity)
- {
- if($entity instanceof OrderProductInterface) {
- return $this->getPriceWithTaxAndReductionCatalog(
- $entity,
- $this->getTotal($entity),
- $this->getTotalWithTax($entity)
- ) ;
- }
- }
-
- private function getPriceWithTaxAndReductionCatalog($entity, $price, $priceWithTax): ?float
- {
- if($entity instanceof ProductPropertyInterface) {
- $reductionCatalog = $entity->getReductionCatalogInherited() ;
-
- if($reductionCatalog) {
- $reductionCatalogValue = $reductionCatalog->getValue() ;
- $reductionCatalogUnit = $reductionCatalog->getUnit() ;
- $reductionCatalogBehaviorTaxRate = $reductionCatalog->getBehaviorTaxRate() ;
- }
- }
-
- if($entity instanceof OrderProductInterface) {
- // OrderProductReductionCatalog
- $reductionCatalog = null ;
- }
-
- if(isset($reductionCatalogValue) && isset($reductionCatalogUnit) && isset($reductionCatalogBehaviorTaxRate)) {
- if ($reductionCatalogUnit == 'percent') {
- return $this->applyReductionPercent(
- $priceWithTax,
- $reductionCatalogValue
- );
- }
- elseif ($reductionCatalogUnit == 'amount') {
- if($reductionCatalogBehaviorTaxRate == 'tax-excluded') {
- return $this->applyTax(
- $this->applyReductionAmount(
- $price,
- $reductionCatalogValue
- ),
- $this->getTaxRateInherited()->getValue()
- );
- }
- elseif($reductionCatalogBehaviorTaxRate == 'tax-included') {
- return $this->applyReductionAmount(
- $priceWithTax,
- $reductionCatalogValue
- );
- }
- }
- }
-
- return $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);
- }
-
- }
|