|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184 |
- <?php
-
- namespace Lc\ShopBundle\Services\Price;
-
- use Lc\ShopBundle\Context\OrderReductionCreditInterface;
- use Lc\ShopBundle\Context\OrderShopInterface;
- use Lc\ShopBundle\Context\OrderShopPriceUtilsInterface;
- use Lc\ShopBundle\Model\ReductionCart;
-
- class OrderShopPriceUtils implements OrderShopPriceUtilsInterface
- {
- use PriceUtilsTrait ;
-
- protected $orderProductPriceUtils ;
-
- public function __construct(OrderProductPriceUtils $orderProductPriceUtils)
- {
- $this->orderProductPriceUtils = $orderProductPriceUtils ;
- }
-
-
- public function getTotalOrderProducts(OrderShopInterface $orderShop):float
- {
-
-
- $total = 0;
- foreach ($orderShop->getOrderProducts() as $orderProduct) {
- $total += $this->orderProductPriceUtils->getTotalWithReduction($orderProduct);
- }
- return $total;
- }
-
- public function getTotalOrderProductsWithTax(OrderShopInterface $orderShop):float
- {
- $total = 0;
- foreach ($orderShop->getOrderProducts() as $orderProduct) {
- $total += $this->orderProductPriceUtils->getTotalWithTaxAndReduction($orderProduct);
- }
- return $total;
-
- }
-
-
- public function getTotalOrderProductsTaxes(OrderShopInterface $orderShop):float {
- }
-
- public function getOrderProductsTaxesAsArray(OrderShopInterface $orderShop):array
- {
- $orderProductsTaxes = [];
- foreach ($orderShop->getOrderProducts() as $orderProduct) {
-
- $idTaxRate = $orderProduct->getTaxRate()->getId() ;
-
- if(!isset($orderProductsTaxes[$idTaxRate])) {
- $orderProductsTaxes[$idTaxRate] = [
- 'label' => $orderProduct->getTaxRate()->getValue() . '%',
- 'totalOrderProducts' => 0,
- 'totalTaxes' => 0,
- ];
- }
-
- $orderProductsTaxes[$idTaxRate]['totalOrderProducts'] += $this->orderProductPriceUtils->getTotalWithReduction($orderProduct) * $this->getReductionsCoef($orderShop);
- $orderProductsTaxes[$idTaxRate]['totalTaxes'] += $this->orderProductPriceUtils->getTotalTaxes($orderProduct) * $this->getReductionsCoef($orderShop) ;
- }
-
- return $orderProductsTaxes ;
- }
-
- private function getReductionsCoef(OrderShopInterface $orderShop) :float
- {
- return $this->getTotalOrderProducts($orderShop) / $this->getTotalOrderProductsWithReductions($orderShop);
- }
-
- private function getTaxRateAverage(OrderShopInterface $order):float
- {
- return $this->getTotalOrderProductsWithTax() / $this->getTotalOrderProducts();
- }
-
- public function getTotalOrderProductsWithReductions(OrderShopInterface $orderShop)
- {
- $total = $this->getTotalOrderProducts($orderShop) ;
-
- $totalReductionAmount = 0 ;
- foreach ($orderShop->getOrderReductionCarts() as $orderReductionCart) {
- $totalReductionAmount += $this->getOrderProductsReductionCartAmountWithoutTax($orderShop,$orderReductionCart);
- }
-
- foreach ($orderShop->getOrderReductionCredits() as $orderReductionCredit) {
- $totalReductionAmount += $this->getOrderProductsReductionCreditAmountWithoutTax($orderShop,$orderReductionCredit);
- }
-
- $total -= $totalReductionAmount ;
-
- return $total ;
- }
-
-
- public function getTotalOrderProductsWithTaxAndReductions(OrderShopInterface $orderShop)
- {
- $total = $this->getTotalOrderProductsWithTax($orderShop) ;
-
- $totalReductionAmount = 0 ;
- foreach ($orderShop->getOrderReductionCarts() as $orderReductionCart) {
- $totalReductionAmount += $this->getOrderProductsReductionCartAmountWithTax($orderShop,$orderReductionCart);
- }
-
- foreach ($orderShop->getOrderReductionCredits() as $orderReductionCredit) {
- $totalReductionAmount += $this->getOrderProductsReductionCreditAmountWithTax($orderShop,$orderReductionCredit);
- }
-
- $total -= $totalReductionAmount ;
-
- return $total ;
- }
-
- public function getOrderProductsReductionCartAmountWithoutTax(OrderShopInterface $order, $orderReductionCart)
- {
- $amount =0;
- if($orderReductionCart->getAppliedTo() === ReductionCart::APPLIED_TO_ORDER_PRODUCTS) {
- if ($orderReductionCart->getUnit() == 'percent') {
- $amount = $this->amountReductionByPercentValue(
- $this->getTotalOrderProducts($order),
- $orderReductionCart->getValue()
- );
- } else if ($orderReductionCart->getUnit() == 'amount') {
- if ($orderReductionCart->getBehaviorTaxRate() == 'tax-excluded') {
- $price = $orderReductionCart->getValue();
- } else if ($orderReductionCart->getBehaviorTaxRate() == 'tax-included') {
- $price = $orderReductionCart->getValue() / $this->getTaxRateAverage();
- }
-
- }
- }
- return $amount ;
-
- }
-
- public function getOrderProductsReductionCartAmountWithTax(OrderShopInterface $order, $orderReductionCart)
- {
-
- $amount =0;
- if($orderReductionCart->getAppliedTo() === ReductionCart::APPLIED_TO_ORDER_PRODUCTS) {
- if ($orderReductionCart->getUnit() == 'percent') {
-
- $amount = $this->amountReductionByPercentValue(
- $this->getTotalOrderProductsWithTax($order),
- $orderReductionCart->getValue()
- );
- } else if ($orderReductionCart->getUnit() == 'amount') {
- if ($orderReductionCart->getBehaviorTaxRate() == 'tax-excluded') {
- $amount = $orderReductionCart->getValue() * $this->getTaxRateAverage();
- } else if ($orderReductionCart->getBehaviorTaxRate() == 'tax-included') {
- $amount = $orderReductionCart->getValue() ;
- }
-
- }
- }
- return $amount ;
- }
-
- public function getOrderProductsReductionCreditAmountWithoutTax(OrderShopInterface $order, OrderReductionCreditInterface $orderReductionCredit)
- {
- $amount =0;
- if ($orderReductionCredit->getBehaviorTaxRate() == 'tax-excluded') {
- $amount = $orderReductionCredit->getValue();
- } else if ($orderReductionCredit->getBehaviorTaxRate() == 'tax-included') {
- $amount = $orderReductionCredit->getValue() / $this->getTaxRateAverage();
- }
-
- return $amount;
- }
-
- public function getOrderProductsReductionCreditAmountWithTax(OrderShopInterface $order, OrderReductionCreditInterface $orderReductionCredit)
- {
- $amount =0;
- if ($orderReductionCredit->getBehaviorTaxRate() == 'tax-excluded') {
- $amountWithTax = $orderReductionCredit->getValue() * $this->getTaxRateAverage();
- } else if ($orderReductionCredit->getBehaviorTaxRate() == 'tax-included') {
- $amountWithTax = $orderReductionCredit->getValue();
- }
- return $amountWithTax;
- }
- }
-
|