/**
  * Calculates order's total values
  *
  * @param OrderInterface $order
  *
  * @return OrderTotal
  */
 protected function calculateOrderTotal(OrderInterface $order)
 {
     $totals = $order->getTotals();
     $targetCurrency = $order->getCurrency();
     $orderTotal = new OrderTotal();
     $grossTotal = 0;
     $netTotal = 0;
     $taxTotal = 0;
     $totals->map(function (OrderTotalDetailInterface $orderTotalDetail) use(&$grossTotal, &$netTotal, &$taxTotal, $targetCurrency) {
         $total = $orderTotalDetail->getOrderTotal();
         $baseCurrency = $total->getCurrency();
         $grossAmount = $this->currencyHelper->convert($total->getGrossAmount(), $baseCurrency, $targetCurrency);
         $netAmount = $this->currencyHelper->convert($total->getNetAmount(), $baseCurrency, $targetCurrency);
         $taxAmount = $this->currencyHelper->convert($total->getTaxAmount(), $baseCurrency, $targetCurrency);
         if ($orderTotalDetail->isSubtraction()) {
             $grossTotal -= $grossAmount;
             $netTotal -= $netAmount;
             $taxTotal -= $taxAmount;
         } else {
             $grossTotal += $grossAmount;
             $netTotal += $netAmount;
             $taxTotal += $taxAmount;
         }
     });
     $orderTotal->setCurrency($targetCurrency);
     $orderTotal->setGrossAmount($grossTotal);
     $orderTotal->setNetAmount($netTotal);
     $orderTotal->setTaxAmount($taxTotal);
     return $orderTotal;
 }
 /**
  * Prepares order product totals
  *
  * @param OrderInterface $order
  *
  * @return OrderTotal
  */
 protected function prepareOrderProductTotal(OrderInterface $order)
 {
     $grossAmountTotal = $this->calculateTotalGrossAmount($order);
     $netAmountTotal = $this->calculateTotalNetAmount($order);
     $taxAmountTotal = $this->calculateTotalTaxAmount($order);
     $productTotal = new OrderTotal();
     $productTotal->setGrossAmount($grossAmountTotal);
     $productTotal->setNetAmount($netAmountTotal);
     $productTotal->setTaxAmount($taxAmountTotal);
     $productTotal->setCurrency($order->getCurrency());
     return $productTotal;
 }
 /**
  * {@inheritdoc}
  */
 public function visitOrder(OrderInterface $order)
 {
     if (null === $order->getCoupon()) {
         $orderTotal = new OrderTotal();
         $discount = $this->getDiscountForClient($order->getClient());
         if ($discount > 0) {
             $productTotal = $order->getProductTotal();
             $orderTotal->setCurrency($order->getCurrency());
             $orderTotal->setGrossAmount($productTotal->getGrossAmount() * $discount);
             $orderTotal->setNetAmount($productTotal->getNetAmount() * $discount);
             $orderTotal->setTaxAmount($productTotal->getTaxAmount() * $discount);
             $orderTotalDetail = $this->initResource();
             $orderTotalDetail->setOrderTotal($orderTotal);
             $orderTotalDetail->setModifierType('%');
             $orderTotalDetail->setModifierValue($discount);
             $orderTotalDetail->setOrder($order);
             $orderTotalDetail->setSubtraction(true);
             $order->addTotal($orderTotalDetail);
         }
     }
 }
 /**
  * {@inheritdoc}
  */
 public function visitOrder(OrderInterface $order)
 {
     if (null !== $order->getCoupon()) {
         $productTotal = $order->getProductTotal();
         $coupon = $order->getCoupon();
         $modifier = $this->calculateModifierValue($coupon, $productTotal->getGrossAmount(), $order->getCurrency());
         if ($modifier > 0) {
             $orderTotal = new OrderTotal();
             $orderTotal->setCurrency($order->getCurrency());
             $orderTotal->setGrossAmount($productTotal->getGrossAmount() * $modifier);
             $orderTotal->setNetAmount($productTotal->getNetAmount() * $modifier);
             $orderTotal->setTaxAmount($productTotal->getTaxAmount() * $modifier);
             $orderTotalDetail = $this->initResource();
             $orderTotalDetail->setOrderTotal($orderTotal);
             $orderTotalDetail->setModifierType($coupon->getModifierType());
             $orderTotalDetail->setModifierValue($modifier);
             $orderTotalDetail->setOrder($order);
             $orderTotalDetail->setSubtraction(true);
             $order->addTotal($orderTotalDetail);
         }
     }
 }