/**
  * 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;
 }
Exemplo n.º 2
0
 /**
  * This function search a currency exchange by id currency from and id currency to
  * 
  * @author Erick Guevara Martínez
  * @param int $from
  * @param int $to
  * @return CurrencyExchange
  */
 public function getByFromAndTo($from, $to)
 {
     $currencyExchange = new CurrencyExchange();
     if ($from == $to) {
         CurrencyExchangeFactory::populate($currencyExchange, array(CurrencyExchange::CURRENCY => $to, CurrencyExchange::RATE => 1));
     } else {
         if ($to == CurrencyExchange::$baseCurrency) {
             $currencyExchange = $this->getByPK($from);
         } else {
             if ($from == CurrencyExchange::$baseCurrency) {
                 $toExchange = $this->getByPK($to);
                 CurrencyExchangeFactory::populate($currencyExchange, array(CurrencyExchange::CURRENCY => $to, CurrencyExchange::RATE => $toExchange->invertExchangeRate()));
             } else {
                 if ($from != CurrencyExchange::$baseCurrency && $to != CurrencyExchange::$baseCurrency) {
                     $fromExchange = $this->getByPK($from);
                     $toExchange = $this->getByPK($to);
                     CurrencyExchangeFactory::populate($currencyExchange, array(CurrencyExchange::CURRENCY => $to, CurrencyExchange::RATE => $fromExchange->getRate() / $toExchange->getRate()));
                 }
             }
         }
     }
     return $currencyExchange;
 }
Exemplo n.º 3
0
 /**
  *
  * @return array
  */
 public function updateAction()
 {
     $form = $this->getForm();
     if ($this->getRequest()->isPost()) {
         $params = $this->getRequest()->getParams();
         if (!$form->isValid($params)) {
             $this->view->setTpl("New");
             $this->view->form = $form;
             return;
         }
         $id = $this->getRequest()->getParam('id');
         $currencyExchange = CurrencyExchangeQuery::create()->findByPKOrThrow($id, $this->i18n->_("It does not exist the CurrencyExchange with id {$id}"));
         try {
             $this->getCurrencyExchangeCatalog()->beginTransaction();
             CurrencyExchangeFactory::populate($currencyExchange, $form->getValues());
             $this->getCurrencyExchangeCatalog()->update($currencyExchange);
             $this->getCurrencyExchangeCatalog()->commit();
             $this->setFlash('ok', $this->i18n->_("Se actualizo correctamente el CurrencyExchange"));
         } catch (Exception $e) {
             $this->getCurrencyExchangeCatalog()->rollBack();
             $this->setFlash('error', $this->i18n->_($e->getMessage()));
         }
     }
     $this->_redirect('currency-exchange/list');
 }
Exemplo n.º 4
0
 /**
  *
  * makeBean
  * @param array $resultset
  * @return \Application\Model\Bean\CurrencyExchange
  */
 protected function makeBean($resultset)
 {
     return CurrencyExchangeFactory::createFromArray($resultset);
 }