/**
  * This function calculates the exchange rate of a value
  * 
  * @param string $from
  * @param string $to
  * @param float $value
  * @return $convertedValue
  */
 public function converter($from, $to, $value)
 {
     $convertedValue = $value;
     if ($from != $to) {
         if ($from == CurrencyExchange::$baseCurrency) {
             $exchangeRate = CurrencyExchangeQuery::create()->whereAdd(CurrencyExchange::CURRENCY, $to)->addAscendingOrderBy(CurrencyExchange::RATEDATE)->findOneOrThrow("The ExchangeRate from {$from} to {$to} can't be calculate.");
             $exchangeRate->setRate($exchangeRate->invertExchangeRate());
         } else {
             if ($to == CurrencyExchange::$baseCurrency) {
                 $exchangeRate = CurrencyExchangeQuery::create()->whereAdd(CurrencyExchange::CURRENCY, $from)->addAscendingOrderBy(CurrencyExchange::RATEDATE)->findOneOrThrow("The ExchangeRate from {$from} to {$to} can't be calculate.");
             } else {
                 $rateFrom = CurrencyExchangeQuery::create()->whereAdd(CurrencyExchange::CURRENCY, $from)->addDescendingOrderBy(CurrencyExchange::RATEDATE)->findOneOrThrow("The ExchangeRate from {$from} to {$to} can't be calculate.");
                 $rateTo = CurrencyExchangeQuery::create()->whereAdd(CurrencyExchange::CURRENCY, $to)->addDescendingOrderBy(CurrencyExchange::RATEDATE)->findOneOrThrow("The ExchangeRate from {$from} to {$to} can't be calculate.");
                 $exchangeRate = CurrencyExchangeFactory::createFromArray(array(CurrencyExchange::CURRENCY => $to, CurrencyExchange::RATE => $rateFrom->getRate() / $rateTo->getRate()));
             }
         }
         $convertedValue = $value * $exchangeRate->getRate();
     }
     return $convertedValue;
 }
 /**
  *
  * @return array
  */
 public function createAction()
 {
     $form = $this->getForm();
     if ($this->getRequest()->isPost()) {
         $params = $this->getRequest()->getParams();
         if (!$form->isValid($params)) {
             $this->view->setTpl("New");
             $this->view->form = $form;
             return;
         }
         try {
             $this->getCurrencyExchangeCatalog()->beginTransaction();
             $currencyExchange = CurrencyExchangeFactory::createFromArray($form->getValues());
             $this->getCurrencyExchangeCatalog()->create($currencyExchange);
             $this->getCurrencyExchangeCatalog()->commit();
             $this->setFlash('ok', $this->i18n->_("Se ha guardado correctamente el "));
         } catch (Exception $e) {
             $this->getCurrencyExchangeCatalog()->rollBack();
             $this->setFlash('error', $this->i18n->_($e->getMessage()));
         }
     }
     $this->_redirect('currency-exchange/list');
 }
Esempio n. 3
0
 /**
  *
  * makeBean
  * @param array $resultset
  * @return \Application\Model\Bean\CurrencyExchange
  */
 protected function makeBean($resultset)
 {
     return CurrencyExchangeFactory::createFromArray($resultset);
 }