Add currency rates for budget calculation
This commit is contained in:
@@ -147,7 +147,7 @@ class Budget extends Base
|
||||
foreach ($rates as $rate) {
|
||||
|
||||
if ($rate['user_id'] == $record['user_id'] && date('Y-m-d', $rate['date_effective']) <= date('Y-m-d', $record['start'])) {
|
||||
$hourly_price = $rate['rate'];
|
||||
$hourly_price = $this->currency->getPrice($rate['currency'], $rate['rate']);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
104
app/Model/Currency.php
Normal file
104
app/Model/Currency.php
Normal file
@@ -0,0 +1,104 @@
|
||||
<?php
|
||||
|
||||
namespace Model;
|
||||
|
||||
use SimpleValidator\Validator;
|
||||
use SimpleValidator\Validators;
|
||||
|
||||
/**
|
||||
* Currency
|
||||
*
|
||||
* @package model
|
||||
* @author Frederic Guillot
|
||||
*/
|
||||
class Currency extends Base
|
||||
{
|
||||
/**
|
||||
* SQL table name
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
const TABLE = 'currencies';
|
||||
|
||||
/**
|
||||
* Get all currency rates
|
||||
*
|
||||
* @access public
|
||||
* @return array
|
||||
*/
|
||||
public function getAll()
|
||||
{
|
||||
return $this->db->table(self::TABLE)->findAll();
|
||||
}
|
||||
|
||||
/**
|
||||
* Calculate the price for the reference currency
|
||||
*
|
||||
* @access public
|
||||
* @return array
|
||||
*/
|
||||
public function getPrice($currency, $price)
|
||||
{
|
||||
static $rates = null;
|
||||
$reference = $this->config->get('application_currency', 'USD');
|
||||
|
||||
if ($reference !== $currency) {
|
||||
$rates = $rates === null ? $this->db->hashtable(self::TABLE)->getAll('currency', 'rate') : array();
|
||||
$rate = isset($rates[$currency]) ? $rates[$currency] : 1;
|
||||
|
||||
return $rate * $price;
|
||||
}
|
||||
|
||||
return $price;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a new currency rate
|
||||
*
|
||||
* @access public
|
||||
* @param string $currency
|
||||
* @param float $rate
|
||||
* @return boolean|integer
|
||||
*/
|
||||
public function create($currency, $rate)
|
||||
{
|
||||
if ($this->db->table(self::TABLE)->eq('currency', $currency)->count() === 1) {
|
||||
return $this->update($currency, $rate);
|
||||
}
|
||||
|
||||
return $this->persist(self::TABLE, compact('currency', 'rate'));
|
||||
}
|
||||
|
||||
/**
|
||||
* Update a currency rate
|
||||
*
|
||||
* @access public
|
||||
* @param string $currency
|
||||
* @param float $rate
|
||||
* @return boolean
|
||||
*/
|
||||
public function update($currency, $rate)
|
||||
{
|
||||
return $this->db->table(self::TABLE)->eq('currency', $currency)->update(array('rate' => $rate));
|
||||
}
|
||||
|
||||
/**
|
||||
* Validate
|
||||
*
|
||||
* @access public
|
||||
* @param array $values Form values
|
||||
* @return array $valid, $errors [0] = Success or not, [1] = List of errors
|
||||
*/
|
||||
public function validate(array $values)
|
||||
{
|
||||
$v = new Validator($values, array(
|
||||
new Validators\Required('currency', t('Field required')),
|
||||
new Validators\Required('rate', t('Field required')),
|
||||
));
|
||||
|
||||
return array(
|
||||
$v->execute(),
|
||||
$v->getErrors()
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user