/** * 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; }
/** * 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); }
/** * 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); }
/** * 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())); }
/** * Disable entity * * @param CurrencyInterface $currency The currency to disable * * @return array Result * * @Route( * path = "/currency/{iso}/disable", * name = "admin_currency_disable" * ) * @Method({"POST"}) * * @EntityAnnotation( * class = "elcodi.entity.currency.class", * name = "currency", * mapping = { * "iso" = "~iso~" * } * ) * * @JsonResponse() */ public function disableCurrencyAction(CurrencyInterface $currency) { $translator = $this->get('translator'); /** * We cannot disable the default currency */ $masterCurrency = $configManager = $this->get('elcodi.store')->getDefaultCurrency(); if ($currency->getIso() == $masterCurrency) { throw new HttpException('403', $translator->trans('admin.currency.error.disable_master_currency')); } $this->disableEntity($currency); return ['message' => $translator->trans('admin.currency.saved.disabled')]; }
/** * 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); }
/** * 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; }