|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107 |
- <?php
-
- namespace Lc\ShopBundle\Price\Services;
-
- use Lc\ShopBundle\Context\OrderProductInterface;
- use Lc\ShopBundle\Context\OrderShopInterface;
- use Lc\ShopBundle\Context\ProductPropertyInterface;
-
- trait PriceUtilsTrait
- {
- 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 applyPercentNegative($price, $percentage)
- {
- return $price / ($percentage / 100 + 1);
- }
-
- public function round($price)
- {
- return round((($price * 100)) / 100, 2);
- }
- public function amountReductionByPercentValue($price, $percent){
- return round((($price * $percent)) / 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);
- }
-
-
-
-
- }
|