Provided default value for parameter as per issue #47.
[timetracker.git] / plugins / MonthlyQuota.class.php
1 <?php
2 // +----------------------------------------------------------------------+
3 // | Anuko Time Tracker
4 // +----------------------------------------------------------------------+
5 // | Copyright (c) Anuko International Ltd. (https://www.anuko.com)
6 // +----------------------------------------------------------------------+
7 // | LIBERAL FREEWARE LICENSE: This source code document may be used
8 // | by anyone for any purpose, and freely redistributed alone or in
9 // | combination with other software, provided that the license is obeyed.
10 // |
11 // | There are only two ways to violate the license:
12 // |
13 // | 1. To redistribute this code in source form, with the copyright
14 // |    notice or license removed or altered. (Distributing in compiled
15 // |    forms without embedded copyright notices is permitted).
16 // |
17 // | 2. To redistribute modified versions of this code in *any* form
18 // |    that bears insufficient indications that the modifications are
19 // |    not the work of the original author(s).
20 // |
21 // | This license applies to this document only, not any other software
22 // | that it may be combined with.
23 // |
24 // +----------------------------------------------------------------------+
25 // | Contributors:
26 // | https://www.anuko.com/time_tracker/credits.htm
27 // +----------------------------------------------------------------------+
28
29 // MontlyQuota class implements handling of work hour quotas.
30 class MonthlyQuota {
31
32   var $db;       // Database connection.
33   var $team_id;  // Team id.
34
35   // Old style constructors are DEPRECATED in PHP 7.0, and will be removed in a future version. You should always use __construct() in new code.
36   function __construct() {
37     $this->db = getConnection();
38     global $user;
39     $this->team_id = $user->team_id;
40   }
41
42   // update - deletes a quota, then inserts a new one.
43   public function update($year, $month, $quota) {
44     $teamId = $this->team_id;
45     $deleteSql = "DELETE FROM tt_monthly_quotas WHERE year = $year AND month = $month AND team_id = $teamId";
46     $this->db->exec($deleteSql);
47     if ($quota){
48       $insertSql = "INSERT INTO tt_monthly_quotas (team_id, year, month, quota) values ($teamId, $year, $month, $quota)";
49       $affected = $this->db->exec($insertSql);
50       return (!is_a($affected, 'PEAR_Error'));
51     }
52     return true;
53   }
54
55   // get - obtains either a single month quota or an array of quotas for an entire year.
56   // Month starts with 1 for January, not 0.
57   public function get($year, $month = null) {
58     if (is_null($month)){
59       return $this->getMany($year);
60     }
61     return $this->getSingle($year, $month);
62   }
63
64   // getSingle - obtains a quota for a single month.
65   private function getSingle($year, $month) {
66     $teamId = $this->team_id;
67     $sql = "SELECT quota FROM tt_monthly_quotas WHERE year = $year AND month = $month AND team_id = $teamId";
68     $reader = $this->db->query($sql);
69     if (is_a($reader, 'PEAR_Error')) {
70       return false;
71     }
72
73     $row = $reader->fetchRow();
74     if ($row)
75       return $row['quota'];
76
77     // If we did not find a record, return a calculated monthly quota.
78     $numWorkdays = $this->getNumWorkdays($month, $year);
79     global $user;
80     return $numWorkdays * $user->workday_hours;
81   }
82
83   // getMany - returns an array of quotas for a given year for team.
84   private function getMany($year){
85     $teamId = $this->team_id;
86     $sql = "SELECT month, quota FROM tt_monthly_quotas WHERE year = $year AND team_id = $teamId";
87     $result = array();
88     $res = $this->db->query($sql);
89     if (is_a($res, 'PEAR_Error')) {
90       return false;
91     }
92
93     while ($val = $res->fetchRow()) {
94       $result[$val['month']] = $val['quota'];
95     }
96
97     return $result;
98   }
99
100   // getNumWorkdays returns a number of work days in a given month.
101   private function getNumWorkdays($month, $year) {
102
103     $daysInMonth = cal_days_in_month(CAL_GREGORIAN, $month, $year); // Number of calendar days in month.
104
105     $workdaysInMonth = 0;
106     // Iterate through the entire month.
107     for ($i = 1; $i <= $daysInMonth; $i++) {
108       $date = "$year-$month-$i";
109       if (!ttTimeHelper::isWeekend($date) && !ttTimeHelper::isHoliday($date)) {
110         $workdaysInMonth++;
111       }
112     }
113     return $workdaysInMonth;
114   }
115 }