Fixed monthly quota class for flexible week start days.
[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 class MonthlyQuota {
30     
31     var $db;       // Database connection.
32     var $holidays; // Array of holidays from localization file.
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         $i18n = $GLOBALS['I18N'];
39         $this->holidays = $i18n->holidays;
40         global $user;
41         $this->team_id = $user->team_id;
42     }
43
44     // update - deletes a quota, then inserts a new one.
45     public function update($year, $month, $quota) {
46         $teamId = $this->team_id;
47         $deleteSql = "DELETE FROM tt_monthly_quotas WHERE year = $year AND month = $month AND team_id = $teamId";
48         $this->db->exec($deleteSql);
49         if ($quota){
50             $insertSql = "INSERT INTO tt_monthly_quotas (team_id, year, month, quota) values ($teamId, $year, $month, $quota)";
51             $affected = $this->db->exec($insertSql);
52             return (!is_a($affected, 'PEAR_Error'));
53         }
54         return true;
55     }
56
57     // get - obains either a single month quota or an array of quotas for an entire year.
58     public function get($year, $month) {
59         if (is_null($month)){
60             return $this->getMany($year);
61         }
62         return $this->getSingle($year, $month);
63     }
64
65     // getWorkdayHours - obtains workday_hours value for a team from the database.
66     public function getWorkdayHours(){
67         $teamId = $this->team_id;
68         $sql = "SELECT workday_hours FROM tt_teams where id = $teamId";
69         $reader = $this->db->query($sql);
70         if (is_a($reader, 'PEAR_Error')) {
71             return false;
72         }
73
74         $row = $reader->fetchRow();
75         return $row['workday_hours'];
76     }
77
78     // getSingle - obtains a quota for a single month.
79     private function getSingle($year, $month) {
80         $teamId = $this->team_id;
81         $sql = "SELECT quota FROM tt_monthly_quotas WHERE year = $year AND month = $month AND team_id = $teamId";
82         $reader = $this->db->query($sql);
83         if (is_a($reader, 'PEAR_Error')) {
84             return false;
85         }
86         
87         $row = $reader->fetchRow();
88         if ($row)
89           return $row['quota'];
90         
91         // If we did not find a record, return a calculated monthly quota.
92         $numWorkdays = $this->getNumWorkdays($month, $year);
93         return $numWorkdays * $this->getWorkdayHours();
94     }
95
96     // getMany - returns an array of quotas for a given year for team.
97     private function getMany($year){
98         $teamId = $this->team_id;
99         $sql = "SELECT month, quota FROM tt_monthly_quotas WHERE year = $year AND team_id = $teamId";
100         $result = array();
101         $res = $this->db->query($sql);
102         if (is_a($res, 'PEAR_Error')) {
103             return false;
104         }
105         
106         while ($val = $res->fetchRow()) {
107             $result[$val['month']] = $val['quota'];
108         }        
109         
110         return $result;
111     }
112     
113     // getNumWorkdays returns a number of work days in a given month.
114     private function getNumWorkdays($month, $year) {
115
116       $daysInMonth = cal_days_in_month(CAL_GREGORIAN, $month, $year); // Number of calendar days in month.
117
118       $workdaysInMonth = 0;
119       // Iterate through all.
120       for ($i = 1; $i <= $daysInMonth; $i++) {
121         $date = "$year-$month-$i";
122         if (!ttTimeHelper::isWeekend($date) && !ttTimeHelper::isHoliday($date)) {
123           $workdaysInMonth++;
124         }
125       }
126       return $workdaysInMonth;
127     }
128 }