From 9bfab51e00608b6e008aa71f6df74104b28fc662 Mon Sep 17 00:00:00 2001
From: Frederic Guillot <fred@kanboard.net>
Date: Sat, 28 Mar 2015 18:00:18 -0400
Subject: Add currency rates for budget calculation

---
 app/Model/Budget.php   |   2 +-
 app/Model/Currency.php | 104 +++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 105 insertions(+), 1 deletion(-)
 create mode 100644 app/Model/Currency.php

(limited to 'app/Model')

diff --git a/app/Model/Budget.php b/app/Model/Budget.php
index fe8457bd..d74dd870 100644
--- a/app/Model/Budget.php
+++ b/app/Model/Budget.php
@@ -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;
                 }
             }
diff --git a/app/Model/Currency.php b/app/Model/Currency.php
new file mode 100644
index 00000000..bc423337
--- /dev/null
+++ b/app/Model/Currency.php
@@ -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()
+        );
+    }
+}
-- 
cgit v1.2.3