Some refactoring of labels in translation files.
[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 import('ttTimeHelper');
30
31 // MontlyQuota class implements handling of work hour quotas.
32 class MonthlyQuota {
33
34   var $db;       // Database connection.
35   var $team_id;  // Team id.
36
37   // 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.
38   function __construct() {
39     $this->db = getConnection();
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     $team_id = $this->team_id;
47     $deleteSql = "DELETE FROM tt_monthly_quotas WHERE year = $year AND month = $month AND team_id = $team_id";
48     $this->db->exec($deleteSql);
49     if ($quota){
50       $float_quota = $this->quotaToFloat($quota);
51       $insertSql = "INSERT INTO tt_monthly_quotas (team_id, year, month, quota) values ($team_id, $year, $month, $float_quota)";
52       $affected = $this->db->exec($insertSql);
53       return (!is_a($affected, 'PEAR_Error'));
54     }
55     return true;
56   }
57
58   // get - obtains either a single month quota or an array of quotas for an entire year.
59   // Month starts with 1 for January, not 0.
60   public function get($year, $month = null) {
61     if (is_null($month)){
62       return $this->getMany($year);
63     }
64     return $this->getSingle($year, $month);
65   }
66
67   // getSingle - obtains a quota for a single month.
68   private function getSingle($year, $month) {
69     $team_id = $this->team_id;
70     $sql = "SELECT quota FROM tt_monthly_quotas WHERE year = $year AND month = $month AND team_id = $team_id";
71     $reader = $this->db->query($sql);
72     if (is_a($reader, 'PEAR_Error')) {
73       return false;
74     }
75
76     $row = $reader->fetchRow();
77     if ($row)
78       return $row['quota'];
79
80     // If we did not find a record, return a calculated monthly quota.
81     $numWorkdays = $this->getNumWorkdays($month, $year);
82     global $user;
83     return $numWorkdays * $user->workday_hours; // TODO: fix a rounding issue for small values like 0:01
84                                                 // Possibly with a database field type change (minutes?).
85   }
86
87   // getMany - returns an array of quotas for a given year for team.
88   private function getMany($year){
89     $team_id = $this->team_id;
90     $sql = "SELECT month, quota FROM tt_monthly_quotas WHERE year = $year AND team_id = $team_id";
91     $result = array();
92     $res = $this->db->query($sql);
93     if (is_a($res, 'PEAR_Error')) {
94       return false;
95     }
96
97     while ($val = $res->fetchRow()) {
98       $result[$val['month']] = $val['quota'];
99     }
100
101     return $result;
102   }
103
104   // getNumWorkdays returns a number of work days in a given month.
105   private function getNumWorkdays($month, $year) {
106
107     $daysInMonth = cal_days_in_month(CAL_GREGORIAN, $month, $year); // Number of calendar days in month.
108
109     $workdaysInMonth = 0;
110     // Iterate through the entire month.
111     for ($i = 1; $i <= $daysInMonth; $i++) {
112       $date = "$year-$month-$i";
113       if (!ttTimeHelper::isWeekend($date) && !ttTimeHelper::isHoliday($date)) {
114         $workdaysInMonth++;
115       }
116     }
117     return $workdaysInMonth;
118   }
119
120   // isValidQuota validates a localized value as an hours quota string (in hours and minutes).
121   public function isValidQuota($value) {
122
123     if (strlen($value) == 0 || !isset($value)) return true;
124
125     if (preg_match('/^[0-9]{1,3}h?$/', $value )) { // 000 - 999
126       return true;
127     }
128
129     if (preg_match('/^[0-9]{1,3}:[0-5][0-9]$/', $value )) { // 000:00 - 999:59
130       return true;
131     }
132
133     global $user;
134     $localizedPattern = '/^([0-9]{1,3})?['.$user->decimal_mark.'][0-9]{1,4}h?$/';
135     if (preg_match($localizedPattern, $value )) { // decimal values like 000.5, 999.25h, ... .. 999.9999h (or with comma)
136       return true;
137     }
138
139     return false;
140   }
141
142   // quotaToFloat converts a valid quota value to a float.
143   public function quotaToFloat($value) {
144
145     if (preg_match('/^[0-9]{1,3}h?$/', $value )) { // 000 - 999
146       return (float) $value;
147     }
148
149     if (preg_match('/^[0-9]{1,3}:[0-5][0-9]$/', $value )) { // 000:00 - 999:59
150       $minutes = ttTimeHelper::toMinutes($value);
151       return ($minutes / 60.0);
152     }
153
154     global $user;
155     $localizedPattern = '/^([0-9]{1,3})?['.$user->decimal_mark.'][0-9]{1,4}h?$/';
156     if (preg_match($localizedPattern, $value )) { // decimal values like 000.5, 999.25h, ... .. 999.9999h (or with comma)
157       // Strip optional h in the end.
158       $value = trim($value, 'h');
159       if ($user->decimal_mark == ',')
160         $value = str_replace(',', '.', $value);
161       return (float) $value;
162     }
163
164     return null;
165   }
166 }