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

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