/** * Return functions. * * @return ExpressionFunction[] An array of Function instances */ public function getFunctions() { return [new ExpressionFunction('money', function () { throw new RuntimeException('Function "money" can\'t be compiled.'); }, function (array $context, $amount, $currencyIso = null) { if ($currencyIso === null) { $currency = $this->defaultCurrencyWrapper->get(); } else { /** * @var CurrencyInterface $currency */ $currency = $this->currencyRepository->findOneBy(['iso' => $currencyIso]); } return Money::create($amount * 100, $currency); })]; }
/** * Calculates all the amounts for a given a Cart * * @param CartInterface $cart Cart * * @return CartInterface Cart */ private function calculateCartPrices(CartInterface $cart) { $currency = $this->currencyWrapper->get(); $productAmount = Money::create(0, $currency); /** * Calculate Amount and ProductAmount */ foreach ($cart->getCartLines() as $cartLine) { /** * @var CartLineInterface $cartLine */ $cartLine = $this->loadCartLinePrices($cartLine); /** * @var MoneyInterface $productAmount * @var MoneyInterface $totalAmount */ $convertedProductAmount = $this->currencyConverter->convertMoney($cartLine->getProductAmount(), $currency); $productAmount = $productAmount->add($convertedProductAmount->multiply($cartLine->getQuantity())); } $cart->setProductAmount($productAmount)->setAmount($productAmount); }
/** * Load cart total price. * * @param CartInterface $cart Cart */ public function loadCartTotalAmount(CartInterface $cart) { $currency = $this->currencyWrapper->get(); $finalAmount = clone $cart->getPurchasableAmount(); /** * Calculates the shipping amount. */ $shippingAmount = $cart->getShippingAmount(); if ($shippingAmount instanceof MoneyInterface) { $convertedShippingAmount = $this->currencyConverter->convertMoney($shippingAmount, $currency); $finalAmount = $finalAmount->add($convertedShippingAmount); } /** * Calculates the coupon amount. */ $couponAmount = $cart->getCouponAmount(); if ($couponAmount instanceof MoneyInterface) { $convertedCouponAmount = $this->currencyConverter->convertMoney($couponAmount, $currency); $finalAmount = $finalAmount->subtract($convertedCouponAmount); } $cart->setAmount($finalAmount); }
/** * Configures the options for this type. * * @param OptionsResolver $resolver The resolver for the options. */ public function configureOptions(OptionsResolver $resolver) { $resolver->setDefaults(['data_class' => 'Elcodi\\Component\\Currency\\Entity\\Money', 'empty_data' => Money::create(0, $this->defaultCurrencyWrapper->get())]); }
/** * Returns a zero-initialized Money object * to be assigned to product prices * * @return MoneyInterface */ protected function createZeroAmountMoney() { return Money::create(0, $this->defaultCurrencyWrapper->get()); }