Author: Adam Piotrowski (adam@wellcommerce.org)
 protected function extractVariantData(VariantInterface $variant, array &$variants)
 {
     $sellPrice = $variant->getSellPrice();
     $baseCurrency = $sellPrice->getCurrency();
     $key = $this->getVariantOptionsKey($variant);
     $variants[$key] = ['id' => $variant->getId(), 'finalPriceGross' => $this->currencyHelper->convertAndFormat($sellPrice->getFinalGrossAmount(), $baseCurrency), 'sellPriceGross' => $this->currencyHelper->convertAndFormat($sellPrice->getGrossAmount(), $baseCurrency), 'discountPriceGross' => $this->currencyHelper->convertAndFormat($sellPrice->getDiscountedGrossAmount(), $baseCurrency), 'stock' => $variant->getStock(), 'symbol' => $variant->getSymbol(), 'options' => $this->getVariantOptions($variant)];
 }
 private function setOrderNumber(OrderInterface $order)
 {
     if (null === $order->getNumber()) {
         $orderNumber = $this->orderNumberGenerator->generateOrderNumber($order);
         $order->setNumber($orderNumber);
     }
 }
 public function visitOrder(OrderInterface $order)
 {
     $productTotal = $order->getProductTotal();
     $modifiers = $order->getModifiers();
     $targetCurrency = $order->getCurrency();
     /** @var OrderSummaryInterface $summary */
     $summary = $this->factory->create();
     $summary->setGrossAmount($productTotal->getGrossPrice());
     $summary->setNetAmount($productTotal->getNetPrice());
     $summary->setTaxAmount($productTotal->getTaxAmount());
     $modifiers->map(function (OrderModifierInterface $modifier) use($summary, $targetCurrency) {
         $baseCurrency = $modifier->getCurrency();
         $grossAmount = $this->helper->convert($modifier->getGrossAmount(), $baseCurrency, $targetCurrency);
         $netAmount = $this->helper->convert($modifier->getNetAmount(), $baseCurrency, $targetCurrency);
         $taxAmount = $this->helper->convert($modifier->getTaxAmount(), $baseCurrency, $targetCurrency);
         if ($modifier->isSubtraction()) {
             $summary->setGrossAmount($summary->getGrossAmount() - $grossAmount);
             $summary->setNetAmount($summary->getNetAmount() - $netAmount);
             $summary->setTaxAmount($summary->getTaxAmount() - $taxAmount);
         } else {
             $summary->setGrossAmount($summary->getGrossAmount() + $grossAmount);
             $summary->setNetAmount($summary->getNetAmount() + $netAmount);
             $summary->setTaxAmount($summary->getTaxAmount() + $taxAmount);
         }
     });
     $order->setSummary($summary);
 }
 protected function collectShippingCost(ShippingMethodCostInterface $shippingMethodCost = null)
 {
     if (null !== $shippingMethodCost) {
         $cost = $shippingMethodCost->getCost();
         $baseCurrency = $cost->getCurrency();
         $grossAmount = $cost->getGrossAmount();
         return $this->helper->convert($grossAmount, $baseCurrency);
     }
     return 0;
 }
 private function refreshOrderProductBuyPrice(OrderProductInterface $orderProduct)
 {
     $baseBuyPrice = $orderProduct->getProduct()->getBuyPrice();
     $baseCurrency = $baseBuyPrice->getCurrency();
     $targetCurrency = $orderProduct->getOrder()->getCurrency();
     $grossAmount = $this->currencyHelper->convert($baseBuyPrice->getGrossAmount(), $baseCurrency, $targetCurrency);
     $netAmount = $this->currencyHelper->convert($baseBuyPrice->getNetAmount(), $baseCurrency, $targetCurrency);
     $taxAmount = $this->currencyHelper->convert($baseBuyPrice->getTaxAmount(), $baseCurrency, $targetCurrency);
     $buyPrice = $this->priceFactory->create();
     $buyPrice->setCurrency($targetCurrency);
     $buyPrice->setGrossAmount($grossAmount);
     $buyPrice->setNetAmount($netAmount);
     $buyPrice->setTaxAmount($taxAmount);
     $buyPrice->setTaxRate($baseBuyPrice->getTaxRate());
     $orderProduct->setBuyPrice($buyPrice);
 }
 private function calculateCouponModifier(CouponInterface $coupon, OrderInterface $order) : float
 {
     $modifierType = $coupon->getModifierType();
     $modifierValue = $coupon->getModifierValue();
     $baseCurrency = $coupon->getCurrency();
     $targetCurrency = $order->getCurrency();
     $totalGrossPrice = $order->getProductTotal()->getGrossPrice();
     if ('%' === $modifierType) {
         return $modifierValue / 100;
     }
     if ('-' === $modifierType) {
         $modifierValue = $this->currencyHelper->convert($modifierValue, $baseCurrency, $targetCurrency);
         return $modifierValue >= $totalGrossPrice ? 1 : $modifierValue / $totalGrossPrice;
     }
     return 1;
 }
 /**
  * {@inheritdoc}
  */
 public function collectTotalTaxAmount(CartInterface $cart, $targetCurrency = null)
 {
     $amount = 0;
     $cart->getProducts()->map(function (CartProductInterface $cartProduct) use(&$amount, &$targetCurrency) {
         $sellPrice = $cartProduct->getSellPrice();
         $baseCurrency = $sellPrice->getCurrency();
         $taxAmount = $sellPrice->getFinalTaxAmount();
         $amount += $this->helper->convert($taxAmount, $baseCurrency, $targetCurrency, $cartProduct->getQuantity());
     });
     return $amount;
 }
 private function calculateTaxAmount(OrderInterface $order) : float
 {
     $targetCurrency = $order->getCurrency();
     $tax = 0;
     $order->getProducts()->map(function (OrderProductInterface $orderProduct) use(&$tax, $targetCurrency) {
         $sellPrice = $orderProduct->getSellPrice();
         $baseCurrency = $sellPrice->getCurrency();
         $taxAmount = $sellPrice->getTaxAmount();
         $tax += $this->helper->convert($taxAmount, $baseCurrency, $targetCurrency, $orderProduct->getQuantity());
     });
     return $tax;
 }
 /**
  * Returns the attribute's data
  *
  * @param ProductAttributeInterface $productAttribute
  *
  * @return array
  */
 protected function getAttributeData(ProductAttributeInterface $productAttribute)
 {
     $sellPrice = $productAttribute->getSellPrice();
     $baseCurrency = $sellPrice->getCurrency();
     return ['id' => $productAttribute->getId(), 'finalPriceGross' => $this->currencyHelper->convertAndFormat($sellPrice->getFinalGrossAmount(), $baseCurrency), 'sellPriceGross' => $this->currencyHelper->convertAndFormat($sellPrice->getGrossAmount(), $baseCurrency), 'discountPriceGross' => $this->currencyHelper->convertAndFormat($sellPrice->getDiscountedGrossAmount(), $baseCurrency), 'stock' => $productAttribute->getStock(), 'symbol' => $productAttribute->getSymbol()];
 }
 /**
  * Converts the amount
  *
  * @param int|float   $price
  * @param null|string $baseCurrency
  * @param null|string $targetCurrency
  *
  * @return string
  */
 public function convertPrice($price, $baseCurrency = null, $targetCurrency = null, $quantity = 1)
 {
     return $this->helper->convert($price, $baseCurrency, $targetCurrency, $quantity);
 }