summaryrefslogtreecommitdiff
path: root/app/Model/Budget.php
diff options
context:
space:
mode:
authorFrederic Guillot <fred@kanboard.net>2015-03-22 18:44:45 -0400
committerFrederic Guillot <fred@kanboard.net>2015-03-22 18:44:45 -0400
commit72de621d77dc1e0fc21a2409d310d6dcdc3eee9e (patch)
treeccfe5d7c21961a60c60938c41dd30c16a6ecfd5d /app/Model/Budget.php
parent373537efe2f893f91f11c04436073bbb2c2732dd (diff)
Add budget graph
Diffstat (limited to 'app/Model/Budget.php')
-rw-r--r--app/Model/Budget.php51
1 files changed, 50 insertions, 1 deletions
diff --git a/app/Model/Budget.php b/app/Model/Budget.php
index 827182a3..84cadf6e 100644
--- a/app/Model/Budget.php
+++ b/app/Model/Budget.php
@@ -2,6 +2,8 @@
namespace Model;
+use DateInterval;
+use DateTime;
use SimpleValidator\Validator;
use SimpleValidator\Validators;
@@ -52,7 +54,7 @@ class Budget extends Base
* @param integer $project_id
* @return \PicoDb\Table
*/
- public function getBreakdown($project_id)
+ public function getSubtaskBreakdown($project_id)
{
return $this->db
->table(SubtaskTimeTracking::TABLE)
@@ -77,6 +79,53 @@ class Budget extends Base
}
/**
+ * Gather necessary information to display the budget graph
+ *
+ * @access public
+ * @param integer $project_id
+ * @return array
+ */
+ public function getDailyBudgetBreakdown($project_id)
+ {
+ $out = array();
+ $in = $this->db->hashtable(self::TABLE)->eq('project_id', $project_id)->gt('amount', 0)->asc('date')->getAll('date', 'amount');
+ $time_slots = $this->getSubtaskBreakdown($project_id)->findAll();
+
+ foreach ($time_slots as $slot) {
+ $date = date('Y-m-d', $slot['start']);
+
+ if (! isset($out[$date])) {
+ $out[$date] = 0;
+ }
+
+ $out[$date] += $slot['cost'];
+ }
+
+ $start = key($in) ?: key($out);
+ $end = new DateTime;
+ $left = 0;
+ $serie = array();
+
+ for ($today = new DateTime($start); $today <= $end; $today->add(new DateInterval('P1D'))) {
+
+ $date = $today->format('Y-m-d');
+ $today_in = isset($in[$date]) ? (int) $in[$date] : 0;
+ $today_out = isset($out[$date]) ? (int) $out[$date] : 0;
+ $left += $today_in;
+ $left -= $today_out;
+
+ $serie[] = array(
+ 'date' => $date,
+ 'in' => $today_in,
+ 'out' => -$today_out,
+ 'left' => $left,
+ );
+ }
+
+ return $serie;
+ }
+
+ /**
* Filter callback to apply the rate according to the effective date
*
* @access public