Exemplo n.º 1
0
 /**
  * Convert amount between two currencies
  *
  * If are the same currency, return same amount
  *
  * If is impossible to convert between them, throw Exception
  *
  * @param CurrencyInterface $currencyFrom Currency where to convert from
  * @param CurrencyInterface $currencyTo   Currency where to convert to
  * @param integer           $amount       Amount to convert
  *
  * @return MoneyInterface Money converted
  *
  * @throws CurrencyNotConvertibleException Currencies cannot be converted
  */
 private function convertCurrency(CurrencyInterface $currencyFrom, CurrencyInterface $currencyTo, $amount)
 {
     if ($currencyFrom->getIso() == $currencyTo->getIso()) {
         return Money::create($amount, $currencyFrom);
     }
     $exchangeRate = $this->exchangeRateCalculator->calculateExchangeRate($currencyFrom, $currencyTo);
     return Money::create($amount * $exchangeRate, $currencyTo);
 }
Exemplo n.º 2
0
 /**
  * Calculates the exchange rate.
  *
  * @param CurrencyInterface $currencyFrom The currency we are converting from.
  * @param CurrencyInterface $currencyTo   The currency we are converting to.
  *
  * @return float
  *
  * @throws CurrencyNotConvertibleException
  */
 public function calculateExchangeRate(CurrencyInterface $currencyFrom, CurrencyInterface $currencyTo)
 {
     $currencyFromIso = $currencyFrom->getIso();
     $currencyToIso = $currencyTo->getIso();
     if ($currencyFromIso == $currencyToIso) {
         return 1.0;
     }
     return $this->calculateExchangeRateBetweenIsos($currencyFromIso, $currencyToIso);
 }
Exemplo n.º 3
0
 /**
  * Sets the amount
  *
  * @param integer $amount Amount
  *
  * @return $this self Object
  */
 public function setAmount($amount)
 {
     $amount = intval($amount);
     $this->wrappedMoney = new WrappedMoney($amount, new WrappedCurrency($this->currency->getIso()));
     $this->amount = $amount;
     return $this;
 }
Exemplo n.º 4
0
 /**
  * Simple Money Value Object constructor
  *
  * @param integer           $amount   Amount
  * @param CurrencyInterface $currency Currency
  */
 protected function __construct($amount, CurrencyInterface $currency)
 {
     $this->amount = intval($amount);
     $this->currency = $currency;
     $this->wrappedMoney = new WrappedMoney($this->amount, new WrappedCurrency($currency->getIso()));
 }
Exemplo n.º 5
0
 /**
  * Set the master currency.
  *
  * @param CurrencyInterface $currency
  *
  * @return array
  *
  * @Route(
  *      path = "/{iso}/master",
  *      name = "admin_currency_master"
  * )
  * @Method({"POST"})
  *
  * @EntityAnnotation(
  *      class = {
  *          "factory" = "elcodi.wrapper.store",
  *          "method" = "get",
  *          "static" = false
  *      },
  *      name = "store",
  *      persist = false
  * )
  * @EntityAnnotation(
  *      class = "elcodi.entity.currency.class",
  *      name = "currency",
  *      mapping = {
  *          "iso" = "~iso~"
  *      }
  * )
  *
  * @JsonResponse()
  */
 public function masterCurrencyAction(StoreInterface $store, CurrencyInterface $currency)
 {
     $translator = $this->get('translator');
     if (!$currency->isEnabled()) {
         throw new HttpException('403', $translator->trans('admin.currency.error.setting_disabled_master_currency'));
     }
     $store->setDefaultCurrency($currency);
     $this->get('elcodi.object_manager.store')->flush($store);
     return ['message' => $translator->trans('admin.currency.saved.master')];
 }
Exemplo n.º 6
0
 /**
  * Convert amount between two currencies
  *
  * If are the same currency, return same amount
  *
  * If is impossible to convert between them, throw Exception
  *
  * @param CurrencyInterface $currencyFrom Currency where to convert from
  * @param CurrencyInterface $currencyTo   Currency where to convert to
  * @param integer           $amount       Amount to convert
  *
  * @return MoneyInterface Money converted
  *
  * @throws CurrencyNotConvertibleException Currencies cannot be converted
  */
 private function convertCurrency(CurrencyInterface $currencyFrom, CurrencyInterface $currencyTo, $amount)
 {
     if ($currencyFrom->getIso() == $currencyTo->getIso()) {
         return Money::create($amount, $currencyFrom);
     }
     $convertedAmount = $this->convertBetweenIsos($currencyFrom->getIso(), $currencyTo->getIso(), $amount);
     return Money::create($convertedAmount, $currencyTo);
 }
Exemplo n.º 7
0
 /**
  * Set Currency in session.
  *
  * @param CurrencyInterface $currency Currency
  *
  * @return $this Self object
  */
 public function set(CurrencyInterface $currency)
 {
     $this->session->set($this->sessionFieldName, $currency->getIso());
     return $this;
 }