custom/plugins/DmitsPaymentCost/src/Storefront/Subscriber/PageSubscriber.php line 129

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Dmits\PaymentCost\Storefront\Subscriber;
  3. use Shopware\Core\Checkout\Cart\Price\Struct\CartPrice;
  4. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  5. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  6. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  7. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsAnyFilter;
  8. use Shopware\Core\Framework\Struct\ArrayStruct;
  9. use Shopware\Core\System\SalesChannel\SalesChannelContext;
  10. use Shopware\Storefront\Page\Checkout\Confirm\CheckoutConfirmPageLoadedEvent;
  11. use Shopware\Core\Checkout\Cart\Cart;
  12. use Shopware\Core\Checkout\Payment\PaymentMethodCollection;
  13. use Shopware\Core\Checkout\Payment\PaymentMethodEntity;
  14. use Shopware\Core\Framework\Context;
  15. use Shopware\Core\Framework\DataAbstractionLayer\EntityCollection;
  16. use Shopware\Storefront\Page\Checkout\Finish\CheckoutFinishPageLoadedEvent;
  17. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  18. use Shopware\Core\Framework\Uuid\Exception\InvalidUuidException;
  19. use Shopware\Core\Framework\Uuid\Uuid;
  20. use Shopware\Storefront\Page\Account\Order\AccountEditOrderPageLoadedEvent;
  21. use Shopware\Storefront\Event\RouteRequest\SetPaymentOrderRouteRequestEvent;
  22. use Dmits\PaymentCost\Core\Content\PaymentCost\PaymentCostCollection;
  23. use Dmits\PaymentCost\Core\Content\PaymentCost\PaymentCostEntity;
  24. use Dmits\PaymentCost\Service\ConfigService;
  25. use Dmits\PaymentCost\Service\PaymentMethodPaymentCostService;
  26. use Shopware\Core\Checkout\Cart\Price\Struct\CalculatedPrice;
  27. use Shopware\Core\Checkout\Cart\Tax\Struct\CalculatedTaxCollection;
  28. use Shopware\Core\Checkout\Cart\Tax\Struct\TaxRuleCollection;
  29. use Shopware\Core\Content\Product\Cart\ProductCartProcessor;
  30. use Shopware\Core\Checkout\Cart\Tax\Struct\CalculatedTax;
  31. use Shopware\Core\Checkout\Order\Aggregate\OrderLineItem\OrderLineItemCollection;
  32. use Shopware\Core\Checkout\Order\Aggregate\OrderLineItem\OrderLineItemEntity;
  33. use Shopware\Core\Checkout\Cart\Price\Struct\QuantityPriceDefinition;
  34. use Shopware\Core\Checkout\Cart\Tax\Struct\TaxRule;
  35. use Shopware\Core\Checkout\Cart\Price\QuantityPriceCalculator;
  36. use Shopware\Core\Checkout\Cart\Price\CashRounding;
  37. use Shopware\Core\Checkout\Cart\Price\GrossPriceCalculator;
  38. use Shopware\Core\Checkout\Cart\Price\NetPriceCalculator;
  39. use Shopware\Core\Checkout\Cart\Tax\TaxCalculator;
  40. use Shopware\Core\Checkout\Cart\LineItem\LineItem;
  41. /**
  42.  * Class PageSubscriber
  43.  *
  44.  * @package Dmits\PaymentCost\Subscriber
  45.  */
  46. class PageSubscriber implements EventSubscriberInterface
  47. {
  48.     /**
  49.      * @var configService
  50.      */
  51.     protected $configService;
  52.     /**
  53.      * @var PaymentMethodPaymentCostService
  54.      */
  55.     protected $paymentcostService;
  56.     
  57.     
  58.     /**
  59.      * @var EntityRepositoryInterface
  60.      */
  61.     private $orderRepository;
  62.     
  63.     /**
  64.      * @var EntityRepositoryInterface
  65.      */
  66.     private $orderLineItemRepository;
  67.     
  68.     /**
  69.      * @var EntityRepositoryInterface
  70.      */
  71.     private EntityRepositoryInterface $productRepository;
  72.     /**
  73.      * PageSubscriber constructor.
  74.      *
  75.      * @param ConfigService                 $configService
  76.      * @param PaymentMethodPaymentCostService $paymentcostService
  77.      * @param EntityRepositoryInterface $orderRepository
  78.      */
  79.     public function __construct(
  80.         ConfigService $configService,
  81.         PaymentMethodPaymentCostService $paymentcostService,
  82.         EntityRepositoryInterface $orderRepository,
  83.         EntityRepositoryInterface $orderLineItemRepository,
  84.         EntityRepositoryInterface $productRepository
  85.     )
  86.     {
  87.         $this->configService $configService;
  88.         $this->paymentcostService $paymentcostService;
  89.         $this->orderRepository $orderRepository;
  90.         $this->orderLineItemRepository $orderLineItemRepository;
  91.         $this->productRepository $productRepository;
  92.     }
  93.     /**
  94.      * @return array
  95.      */
  96.     public static function getSubscribedEvents(): array
  97.     {
  98.         return [
  99.             CheckoutConfirmPageLoadedEvent::class => 'onCheckoutConfirmPageLoaded',
  100.             CheckoutFinishPageLoadedEvent::class => 'onCheckoutFinishedPageLoaded',
  101.             AccountEditOrderPageLoadedEvent::class => 'onAccountEditOrderPageLoaded',
  102.             SetPaymentOrderRouteRequestEvent::class=> 'onAccountChangePayment'
  103.         ];
  104.     }
  105.     public function onAccountChangePayment(SetPaymentOrderRouteRequestEvent $event): void
  106.     {
  107.         $orderId=($event->getStoreApiRequest()->request->get('orderId'));
  108.         $selectedPaymentMethod=$event->getStoreApiRequest()->request->get('paymentMethodId');
  109.         $paymentCost $this->paymentcostService->getPaymentCostForPaymentMethods([$selectedPaymentMethod], $event->getContext());
  110.   
  111.         $selectedPaymentCost=$paymentCost->get($selectedPaymentMethod);
  112.         
  113.         $name $event->getSalesChannelContext()->getPaymentMethod()->getTranslation('name');
  114.         if($selectedPaymentCost){
  115.             if($selectedPaymentCost->getTitle()){
  116.                 $name=$selectedPaymentCost->getTitle();
  117.             }
  118.         }
  119.         $criteria = new Criteria([$orderId]);
  120.         $order $this->orderRepository->search($criteria$event->getContext())->first();
  121.         $criteria = new Criteria();
  122.         $criteria->addFilter(new EqualsFilter('orderId',$orderId));
  123.         $lineItemId="";
  124.         $price="";
  125.         $orderLineItems $this->orderLineItemRepository->search($criteria$event->getContext());
  126.         foreach($orderLineItems->getElements() as $item){
  127.             if($item->getType()=="payment_cost"){
  128.                 $lineItemId=$item->getId();
  129.                 $price=$item->getPrice();
  130.             }
  131.             
  132.         }
  133.         $newPrice=    $this->calculatePrice($orderLineItems->getElements(),$order->getPrice(),$order->getTaxStatus(),$selectedPaymentCost,$event->getSalesChannelContext());
  134.         $calcCost=$newPrice["calcCost"];
  135.         $paymentcost_tax=$newPrice["calcTax"];
  136.         
  137.         $netPrice=$newPrice["netPrice"];
  138.         $totalPrice=$newPrice["totalPrice"];
  139.         $positionPrice=$newPrice["positionPrice"];
  140.         $calculatedTaxes=$newPrice["calculatedTaxes"];
  141.         $taxRules=$newPrice["taxRules"];
  142.         $taxStatus=$newPrice["taxStatus"];      
  143.         
  144.         $tax=$paymentcost_tax;
  145.         $rules = new TaxRuleCollection([]);
  146.         if($tax>0){
  147.             $rules->add(
  148.                 new TaxRule(
  149.                     $tax,
  150.                     100
  151.                     )
  152.                 );
  153.         }
  154.         
  155.         $definition = new QuantityPriceDefinition($calcCost$rules1true);
  156.         
  157.         $calculator = new QuantityPriceCalculator(
  158.             new GrossPriceCalculator(new TaxCalculator(), new CashRounding()),
  159.             new NetPriceCalculator(new TaxCalculator(), new CashRounding())
  160.             );
  161.         $newPrice=$calculator->calculate($definition$event->getSalesChannelContext());
  162.         $definition = new QuantityPriceDefinition($calcCost$rules1true);
  163.         
  164.                
  165.         
  166.         
  167.         
  168.         $update=['id'=>$lineItemId,'label'=>$name,"quantity"=>1,"priceDefinition"=>$definition,
  169.             "price"=>["unitPrice"=>$calcCost,"totalPrice"=>$calcCost,"quantity"=>1,"calculatedTaxes"=>$newPrice->getCalculatedTaxes(),"taxRules"=> $newPrice->getTaxRules()]
  170.         ];
  171.         
  172.         try{
  173.             if(isset($selectedPaymentCost) && $selectedPaymentCost->getProductId()){  
  174.                 if (Uuid::isValid($selectedPaymentCost->getProductId())) {
  175.                 $criteria = new Criteria();
  176.                 $criteria->addFilter(new EqualsFilter('id'$selectedPaymentCost->getProductId()));
  177.                         $products $this->productRepository->search($criteria$event->getContext())->first();
  178.                         if(isset($product) && $products->getId()==$selectedPaymentCost->getProductId()){
  179.                             $update["productId"] =$products->getId();
  180.                             $update["referencedId"] =$products->getId();
  181. //                             $update["product"] =$products;
  182.                         }
  183.                 }
  184.             }
  185.         }catch(InvalidUuidException Exception $e){
  186.           
  187.         }
  188.         if($lineItemId){
  189.             
  190.             if($calcCost==0){
  191.                 $this->orderLineItemRepository->delete([$update],$event->getContext());
  192.             }else{
  193.                 $this->orderLineItemRepository->update([$update],$event->getContext());
  194.             }
  195.             
  196.         }elseif($calcCost!=0){
  197.            
  198.             $update=['identifier'=>$this->getPaymentCostItemKey(),'orderId'=>$orderId,'label'=>$name,"quantity"=>1,"priceDefinition"=>$definition,
  199.                 "type"=>PaymentMethodPaymentCostService::LINE_ITEM_TYPE,     "good"=>false,   "price"=>["unitPrice"=>$calcCost,"totalPrice"=>$calcCost,"quantity"=>1,"calculatedTaxes"=>$newPrice->getCalculatedTaxes(),"taxRules"=> $newPrice->getTaxRules()]
  200.             ];
  201.             try{
  202.                 if(isset($selectedPaymentCost) && $selectedPaymentCost->getProductId()){
  203.                 if (Uuid::isValid($selectedPaymentCost->getProductId())) {
  204.                     $criteria = new Criteria();
  205.                     $criteria->addFilter(new EqualsFilter('id'$selectedPaymentCost->getProductId()));
  206.                     $products $this->productRepository->search($criteria$event->getContext())->first();
  207.                     if(isset($product) && $products->getId()==$selectedPaymentCost->getProductId()){
  208.                         $update["productId"] =$products->getId();
  209.                         $update["referencedId"] =$products->getId();
  210. //                         $update["product"] =$products;
  211.                     }
  212.                 }
  213.             }
  214.         }catch(InvalidUuidException Exception $e){
  215.             
  216.         }
  217.             $this->orderLineItemRepository->create([$update],$event->getContext());
  218.  
  219.         
  220.         }
  221.         $price=  new CartPrice($netPrice$totalPrice$positionPrice$calculatedTaxes$taxRules$taxStatus);
  222.         $order->setPrice($price);
  223.         $updateOrder=['id'=>$orderId,"price"=>$order->getPrice()];
  224.         $this->orderRepository->update([$updateOrder],$event->getContext());
  225.      
  226.     }
  227.     
  228.     /**
  229.      * @return string
  230.      */
  231.     private function getPaymentCostItemKey(): string
  232.     {
  233.         return md5(PaymentMethodPaymentCostService::LINE_ITEM_TYPE);
  234.     }
  235.     
  236.     /**
  237.      * @param CheckoutFinishPageLoadedEvent $event
  238.      */
  239.     public function onAccountEditOrderPageLoaded(AccountEditOrderPageLoadedEvent $event): void
  240.     {        
  241.         $paymentMethods $event->getPage()->getPaymentMethods();
  242.         $paymentCost $this->paymentcostService->getPaymentCostForPaymentMethods(array_values($paymentMethods->getIds()), $event->getContext());
  243.         $selectedPaymentMethod $event->getSalesChannelContext()->getPaymentMethod();
  244.         $selectedPaymentCost=$paymentCost->get($selectedPaymentMethod->getId());
  245.         $newPrice=    $this->calculatePrice($event->getPage()->getOrder()->getLineItems(),$event->getPage()->getOrder()->getPrice(),$event->getPage()->getOrder()->getTaxStatus(),$selectedPaymentCost,$event->getSalesChannelContext());
  246.         
  247.         $calcCost=$newPrice["calcCost"];
  248.         $paymentcost_tax=$newPrice["calcTax"];
  249.         
  250.         $netPrice=$newPrice["netPrice"];
  251.         $totalPrice=$newPrice["totalPrice"];
  252.         $positionPrice=$newPrice["positionPrice"];
  253.         $calculatedTaxes=$newPrice["calculatedTaxes"];
  254.         $taxRules=$newPrice["taxRules"];
  255.         $taxStatus=$newPrice["taxStatus"];
  256.         $price=  new CartPrice($netPrice$totalPrice$positionPrice$calculatedTaxes$taxRules$taxStatus);
  257.             $event->getPage()->getOrder()->setPrice($price);
  258.             $name $event->getSalesChannelContext()->getPaymentMethod()->getTranslation('name');
  259.             if(isset($selectedPaymentCost) && $selectedPaymentCost->getTitle()){
  260.                 $name=$selectedPaymentCost->getTitle();
  261.             }
  262.             $lineitem=   $this->createPaymentCost($name$event->getContext(),$event->getSalesChannelContext(),$calcCost,$paymentcost_tax);
  263.          $newCartItem=array();
  264.          foreach($event->getPage()->getOrder()->getLineItems() as $cartItem){
  265.              if($cartItem->getType()!="payment_cost"){
  266.                 $newCartItem[]=$cartItem;
  267.              }
  268.          }
  269.          if($calcCost!=0){
  270.             $newCartItem[]=$lineitem;
  271.          }
  272.          $event->getPage()->getOrder()->setLineItems(new OrderLineItemCollection($newCartItem));
  273.             $this->paymentcostService->applyPaymentCostToSelectedPaymentMethod(
  274.                 $selectedPaymentMethod,
  275.                 $paymentCost,
  276.                 $calcCost
  277.                 );
  278.         $this->paymentcostService->applyPaymentCostToAvailablePaymentMethods($paymentCost$paymentMethods);
  279.         
  280.     }
  281.     
  282.     /**
  283.      * @param PaymentMethodEntity $paymentMethod
  284.      * @param SalesChannelContext $salesChannelContext
  285.      *
  286.      * @return LineItem
  287.      */
  288.     private function createPaymentCost($name,Context $contextSalesChannelContext $salesChannelContext,$price,$tax): OrderLineItemEntity
  289.     {
  290.         
  291.         $lineItem=$this->buildOrderLineItemEntity(md5(PaymentMethodPaymentCostService::LINE_ITEM_TYPE),$name,$price,$tax,$context,$salesChannelContext);
  292.         return $lineItem;
  293.     }
  294.     
  295.     
  296.     private function buildOrderLineItemEntity($id,$name,$price,$tax,$context,$salesChannelContext){
  297.         
  298.         $rules = new TaxRuleCollection([]);
  299.         if($tax>0){
  300.             $rules->add(
  301.                 new TaxRule(
  302.                     $tax,
  303.                     100
  304.                     )
  305.                 );
  306.         }
  307.         
  308.         $definition = new QuantityPriceDefinition($price$rules1true);
  309.         
  310.         $calculator = new QuantityPriceCalculator(
  311.             new GrossPriceCalculator(new TaxCalculator(), new CashRounding()),
  312.             new NetPriceCalculator(new TaxCalculator(), new CashRounding())
  313.             );
  314.         $newPrice=$calculator->calculate($definition$salesChannelContext);
  315.         $orderLineItemEntity = new OrderLineItemEntity();
  316.         $orderLineItemEntity->setId($id);
  317.         $orderLineItemEntity->setType(PaymentMethodPaymentCostService::LINE_ITEM_TYPE);
  318.         $orderLineItemEntity->setPosition(1);
  319.         $orderLineItemEntity->setIdentifier($id);
  320.         
  321. //         $orderLineItemEntity->setReferencedId($id);     
  322.         
  323.         $orderLineItemEntity->setLabel($name);
  324.         $orderLineItemEntity->setGood(false);
  325.         $orderLineItemEntity->setRemovable(false);
  326.         $orderLineItemEntity->setStackable(true);
  327.         $orderLineItemEntity->setPrice($newPrice);
  328.         $orderLineItemEntity->setQuantity(1);
  329.     
  330.         return $orderLineItemEntity;
  331.     }
  332.     /**
  333.      * @param CheckoutFinishPageLoadedEvent $event
  334.      */
  335.     public function onCheckoutFinishedPageLoaded(CheckoutFinishPageLoadedEvent $event): void
  336.     {
  337.         if (!$this->isActive($event->getSalesChannelContext())) {
  338.             return;
  339.         }
  340.         $paymentCostItem="";
  341.         foreach($event->getPage()->getOrder()->getLineItems() as $item){
  342.             
  343.             if($item->getType()=="payment_cost"){
  344.                 $calcCost=$item->getPrice()->getTotalPrice();
  345.                 $paymentCostItem=$item;
  346.             }
  347.         }
  348.        $selectedPaymentMethod $event->getSalesChannelContext()->getPaymentMethod();
  349.         
  350.        $selectedPaymentCost $this->paymentcostService->getPaymentCostForPaymentMethod($selectedPaymentMethod$event->getContext());
  351.        $name="";
  352.        if($paymentCostItem!=""){
  353.             $name=$paymentCostItem->getLabel();
  354.            if($selectedPaymentCost->getTitle()){
  355.                $name=$selectedPaymentCost->getTitle();
  356.            }
  357.        }
  358.        try{
  359.            if(isset($selectedPaymentCost) && $selectedPaymentCost->getProductId()){
  360.                if (Uuid::isValid($selectedPaymentCost->getProductId())) {
  361.                     $criteria = new Criteria();
  362.                     $criteria->addFilter(new EqualsFilter('id'$selectedPaymentCost->getProductId()));
  363.                     $product $this->productRepository->search($criteria$event->getContext())->first();
  364.                     if(isset($product) && $product->getId()==$selectedPaymentCost->getProductId()){
  365.                         $paymentCostItem->setProductId($product->getId());
  366.                     }
  367.                     $update=['id'=>$paymentCostItem->getId(),'productId'=>$paymentCostItem->getProductId(),'referencedId'=>$paymentCostItem->getProductId(),'label'=>$name,"quantity"=>1,"priceDefinition"=>$paymentCostItem->getPriceDefinition(),
  368.                         "price"=>$paymentCostItem->getPrice()
  369.                     ];
  370.                     $this->orderLineItemRepository->update([$update],$event->getContext());
  371.                }
  372.             }
  373.        }catch(InvalidUuidException Exception $e){
  374.            
  375.        }
  376.         if (!is_null($selectedPaymentCost)) {
  377.             $selectedPaymentMethod->addExtension('DmitsPaymentCost', new ArrayStruct([
  378.                 'paymentcost_amount' => $selectedPaymentCost->getAmount(),
  379.                 'paymentcost_amount_per' => $selectedPaymentCost->getAmountPer(),
  380.             ]));
  381.         }
  382.     }
  383.     
  384.     
  385.     /**
  386.      * @param CheckoutConfirmPageLoadedEvent $event
  387.      */
  388.     public function onCheckoutConfirmPageLoaded(CheckoutConfirmPageLoadedEvent $event): void
  389.     {
  390.         if (!$this->isActive($event->getSalesChannelContext())) {
  391.             return;
  392.         }
  393.         $paymentMethods $event->getPage()->getPaymentMethods();
  394.         $paymentCost $this->paymentcostService->getPaymentCostForPaymentMethods(array_values($paymentMethods->getIds()), $event->getContext());
  395.         $selectedPaymentMethod $event->getSalesChannelContext()->getPaymentMethod();
  396.         $calcCost=0;
  397.         foreach($event->getPage()->getCart()->getLineItems() as $item){
  398.             
  399.             if($item->getType()=="payment_cost"){
  400.                 $calcCost=$item->getPrice()->getTotalPrice();
  401.             }
  402.         }
  403.         
  404.         $this->paymentcostService->applyPaymentCostToSelectedPaymentMethod(
  405.             $selectedPaymentMethod,
  406.             $paymentCost,
  407.             $calcCost
  408.             );
  409.         
  410.         $this->paymentcostService->applyPaymentCostToAvailablePaymentMethods($paymentCost$paymentMethods);
  411.     }
  412.     /**
  413.      * @param SalesChannelContext $salesChannelContext
  414.      *
  415.      * @return bool
  416.      */
  417.     protected function isActive(SalesChannelContext $salesChannelContext): bool
  418.     {
  419.         return $this->configService->getIsActive($salesChannelContext->getSalesChannel());
  420.     }
  421.     
  422.     /**
  423.      * 
  424.      * @param unknown $lineItems
  425.      * @param unknown $orderPrice
  426.      * @param unknown $orderTaxStatus
  427.      * @param unknown $paymentCosts
  428.      * @return number[]|\Shopware\Core\Checkout\Cart\Tax\Struct\CalculatedTaxCollection[]|unknown[]
  429.      */
  430.     protected function calculatePrice($lineItems,$orderPrice,$orderTaxStatus,$paymentCosts,$context){
  431.         
  432.         $rounding=new CashRounding();
  433.         $calcCost=0;
  434.         foreach($lineItems as $item){
  435.             
  436.             if($item->getType()=="payment_cost"){
  437.                 $calcCostObject=$item->getPrice();
  438.                 $calcCost=$item->getPrice()->getTotalPrice();
  439.             }
  440.         }
  441.         $array_payment=array();
  442.         if(!empty($calcCostObject)){
  443.             foreach($calcCostObject->getCalculatedTaxes()->sortByTax()->getElements() as $element){
  444.                 $array_payment[$element->getTaxRate()]=array("tax"=>$element->getTax(),"price"=>$element->getPrice());
  445.                 
  446.             }
  447.         }
  448.         if(empty($array_payment)){
  449.             $array_payment[0]=array("tax"=>0,"price"=>$calcCost);
  450.         }
  451.         
  452.         $netPrice=$orderPrice->getNetPrice();
  453.         $totalPrice=$orderPrice->getTotalPrice();
  454.         $positionPrice=$orderPrice->getPositionPrice();
  455.         
  456.         $calculatedTaxes=$orderPrice->getCalculatedTaxes();
  457.         
  458.         $taxRules=$orderPrice->getTaxRules();
  459.         $taxStatus=$orderPrice->getTaxStatus();
  460.         
  461.         $calculatedTaxes1 = new CalculatedTaxCollection();
  462.         
  463.         $paymentcost_absolut=0;
  464.         $paymentcost_per=0;
  465.         $paymentcost_tax=0;
  466.         $paymentcost_absolut_net=0;
  467.         $taxSum=0;
  468.         
  469.         if($paymentCosts){
  470.             $paymentcost_absolut=$paymentCosts->getAmount();
  471.             $paymentcost_per=$paymentCosts->getAmountPer();
  472.             $paymentcost_tax$paymentCosts->getTax();
  473.             
  474.             if($paymentcost_tax>0){
  475.                 $paymentcost_absolut_net=($paymentcost_absolut/(($paymentcost_tax/100)+1));
  476.             }else{
  477.                 $paymentcost_absolut_net=$paymentcost_absolut;
  478.                 
  479.             }
  480.             
  481.         }
  482.         
  483.         $isPaymentTaxinOrderTax=false;
  484.         foreach($calculatedTaxes->getElements() as  $key=> $element){
  485.             
  486.             if(isset($array_payment[$key])){
  487.                 
  488.                 $newTax =$element->getTax()-   $array_payment[$key]["tax"];
  489.  
  490.                 if($newTax!=0){
  491.                     $calculatedTaxes1->add(new CalculatedTax($newTax$element->getTaxRate(), $newTax));
  492.                 }
  493.                 
  494.                 if($orderTaxStatus=="net"){
  495.               
  496.                     $netPrice=$netPrice-($array_payment[$key]["price"]);
  497.                     $totalPrice=$totalPrice-$array_payment[$key]["price"]-$array_payment[$key]["tax"];
  498.                     $positionPrice=$positionPrice-$array_payment[$key]["price"];
  499.                  
  500.                 }else{
  501.                     $netPrice=$netPrice-($array_payment[$key]["price"]-$array_payment[$key]["tax"]);
  502.                     $totalPrice=$totalPrice-($array_payment[$key]["price"]);
  503.                     $positionPrice=$positionPrice-$array_payment[$key]["price"];
  504.                    
  505.                 }
  506.                 
  507.                 $isPaymentTaxinOrderTax=true;
  508.             }else{
  509.                 
  510.                 $calculatedTaxes1->add(new CalculatedTax($element->getTax(), $element->getTaxRate(), $element->getTax()));
  511.             }
  512.         }
  513.       
  514.         if(!$isPaymentTaxinOrderTax && isset($array_payment[0])){
  515.             if($orderTaxStatus=="net"){
  516.                 $netPrice=$netPrice-$array_payment[0]["price"];
  517.                 $totalPrice=$totalPrice-$array_payment[0]["price"]-$array_payment[0]["tax"];
  518.                 $positionPrice=$positionPrice-$array_payment[0]["price"];
  519.             }else{
  520.                 $netPrice=$netPrice-($array_payment[0]["price"]-$array_payment[0]["tax"]);
  521.              
  522.                 $totalPrice=$totalPrice-$array_payment[0]["price"];
  523.                 $positionPrice=$positionPrice-$array_payment[0]["price"];
  524.             }
  525.         }
  526.    
  527.         $netPrice_add_perc=0;
  528.         $totalPrice_add_perc=0;
  529.         $positionPrice_add_perc=0;
  530.         if($paymentcost_per!=0){
  531.             $netPrice_add_perc=$netPrice/100*$paymentcost_per;
  532.             $totalPrice_add_perc=$totalPrice/100*$paymentcost_per;
  533.             
  534.             
  535.             $positionPrice_add_perc=$positionPrice/100*$paymentcost_per;
  536.         }
  537.       
  538.         foreach($calculatedTaxes1->getElements() as  $key=> $element){
  539.             if($key==$paymentcost_tax)   {
  540.                 if($orderTaxStatus=="net"){
  541.                     $add_tax=($paymentcost_absolut+$netPrice_add_perc)-(($paymentcost_absolut+$netPrice_add_perc)/(($paymentcost_tax/100)+1));
  542.                 }else{
  543.                     $add_tax=($paymentcost_absolut+$totalPrice_add_perc)-(($paymentcost_absolut+$totalPrice_add_perc)/(($paymentcost_tax/100)+1));
  544.                 }
  545.                 
  546.                 
  547.                 $newTax$element->getTax()+$add_tax;
  548.                 $newTax$rounding->mathRound($newTax,$context->getItemRounding());
  549.                 $calculatedTaxes1->add(new CalculatedTax($newTax$element->getTaxRate(), $newTax));
  550.                
  551.             }
  552.         }
  553.      
  554.         if($orderTaxStatus=="net"){
  555.             $calcCost=$paymentcost_absolut_net+$netPrice_add_perc;
  556.         }else{
  557.             $calcCost=$paymentcost_absolut+$totalPrice_add_perc;
  558.         }
  559.     
  560.         $netPrice=$netPrice+$rounding->mathRound($paymentcost_absolut_net+$netPrice_add_perc,$context->getItemRounding());
  561.         foreach($calculatedTaxes1->getElements() as  $key=> $element){
  562.             $taxSum=$taxSum+$element->getTax();
  563.         }
  564.         
  565.      
  566.         if($orderTaxStatus=="net"){
  567.             $positionPrice=$positionPrice+$paymentcost_absolut_net+$positionPrice_add_perc;
  568.             $totalPrice=$totalPrice+$rounding->mathRound($paymentcost_absolut+$totalPrice_add_perc,$context->getItemRounding());
  569.         }else{
  570.             $positionPrice=$positionPrice+$paymentcost_absolut+$positionPrice_add_perc;
  571.             $totalPrice=$totalPrice+$paymentcost_absolut+$totalPrice_add_perc;
  572.         }
  573.         
  574.         $calcCost=   $rounding->mathRound($calcCost,$context->getItemRounding());
  575.         if($orderTaxStatus=="net"){
  576.         $netPrice=$rounding->mathRound($netPrice,$context->getItemRounding());
  577.         $totalPrice=$rounding->mathRound($netPrice+$taxSum,$context->getItemRounding());
  578.         }else{
  579.             $netPrice=$rounding->mathRound($totalPrice-$taxSum,$context->getItemRounding());
  580.             $totalPrice=$rounding->mathRound($totalPrice,$context->getItemRounding());
  581.         }
  582.         $positionPrice=$rounding->mathRound($positionPrice,$context->getItemRounding());
  583.         return array("calcCost"=>$calcCost,
  584.             "calcTax"=>$paymentcost_tax,
  585.             "netPrice"=>$netPrice,
  586.             "totalPrice"=>$totalPrice,
  587.             "positionPrice"=>$positionPrice,
  588.             "calculatedTaxes"=>$calculatedTaxes1,
  589.             "taxRules"=>$taxRules,
  590.             "taxStatus"=>$taxStatus
  591.         );
  592.         
  593.         
  594.     }
  595. }