X-Git-Url: http://wagnertech.de/gitweb/gitweb.cgi/timetracker.git/blobdiff_plain/270ff8df9ea85d9deb349accedfaedc2f8734251..f74704271ae5d37aee3fdf941f3462c8b92ad0fd:/plugins/MonthlyQuota.class.php diff --git a/plugins/MonthlyQuota.class.php b/plugins/MonthlyQuota.class.php index 5961a479..3516fe4a 100644 --- a/plugins/MonthlyQuota.class.php +++ b/plugins/MonthlyQuota.class.php @@ -1,41 +1,70 @@ db = getConnection(); $i18n = $GLOBALS['I18N']; $this->holidays = $i18n->holidays; global $user; - $this->usersTeamId = $user->team_id; + $this->team_id = $user->team_id; } - + + // update - deletes a quota, then inserts a new one. public function update($year, $month, $quota) { - $teamId = $this->usersTeamId; - $deleteSql = "DELETE FROM tt_monthly_quota WHERE year = $year AND month = $month AND team_id = $teamId"; + $teamId = $this->team_id; + $deleteSql = "DELETE FROM tt_monthly_quotas WHERE year = $year AND month = $month AND team_id = $teamId"; $this->db->exec($deleteSql); if ($quota){ - $insertSql = "INSERT INTO tt_monthly_quota (team_id, year, month, quota) values ($teamId, $year, $month, $quota)"; + $insertSql = "INSERT INTO tt_monthly_quotas (team_id, year, month, quota) values ($teamId, $year, $month, $quota)"; $affected = $this->db->exec($insertSql); return (!is_a($affected, 'PEAR_Error')); } return true; } - + + // get - obains either a single month quota or an array of quotas for an entire year. public function get($year, $month) { if (is_null($month)){ return $this->getMany($year); } - return $this->getSingle($year, $month); } - public function getDailyWorkingHours(){ - $teamId = $this->usersTeamId; + // getWorkdayHours - obtains workday_hours value for a team from the database. + public function getWorkdayHours(){ + $teamId = $this->team_id; $sql = "SELECT workday_hours FROM tt_teams where id = $teamId"; $reader = $this->db->query($sql); if (is_a($reader, 'PEAR_Error')) { @@ -43,39 +72,38 @@ class MonthlyQuota { } $row = $reader->fetchRow(); - return $row["workday_hours"]; + return $row['workday_hours']; } - + + // getSingle - obtains a quota for a single month. private function getSingle($year, $month) { - $teamId = $this->usersTeamId; - $sql = "SELECT quota FROM tt_monthly_quota WHERE year = $year AND month = $month AND team_id = $teamId"; + $teamId = $this->team_id; + $sql = "SELECT quota FROM tt_monthly_quotas WHERE year = $year AND month = $month AND team_id = $teamId"; $reader = $this->db->query($sql); if (is_a($reader, 'PEAR_Error')) { return false; } $row = $reader->fetchRow(); + if ($row) + return $row['quota']; - // if we don't find a record, return calculated monthly quota - if (is_null($row)){ - - $holidaysWithYear = array(); - foreach ($this->holidays as $day) { - $parts = explode("/", $day); - $holiday = "$year-$parts[0]-$parts[1]"; - array_push($holidaysWithYear, $holiday); - } - - $daysInMonth = cal_days_in_month(CAL_GREGORIAN, $month, $year); - return $this->getWorkingDays("$year-$month-01", "$year-$month-$daysInMonth", $holidaysWithYear) * $this->getDailyWorkingHours(); + // If we did not find a record, return a calculated monthly quota. + $holidaysWithYear = array(); + foreach ($this->holidays as $day) { + $parts = explode("/", $day); + $holiday = "$year-$parts[0]-$parts[1]"; + array_push($holidaysWithYear, $holiday); } - - return $row["quota"]; - } + $daysInMonth = cal_days_in_month(CAL_GREGORIAN, $month, $year); + return $this->getWorkingDays("$year-$month-01", "$year-$month-$daysInMonth", $holidaysWithYear) * $this->getWorkdayHours(); + } + + // getMany - returns an array of quotas for a given year for team. private function getMany($year){ - $teamId = $this->usersTeamId; - $sql = "SELECT month, quota FROM tt_monthly_quota WHERE year = $year AND team_id = $teamId"; + $teamId = $this->team_id; + $sql = "SELECT month, quota FROM tt_monthly_quotas WHERE year = $year AND team_id = $teamId"; $result = array(); $res = $this->db->query($sql); if (is_a($res, 'PEAR_Error')) { @@ -83,15 +111,17 @@ class MonthlyQuota { } while ($val = $res->fetchRow()) { - $result[$val["month"]] = $val["quota"]; - // $result[] = $val; + $result[$val['month']] = $val['quota']; } return $result; } - //The function returns the no. of business days between two dates and it skips the holidays + // The function returns the number of business days between two dates skipping holidays. private function getWorkingDays($startDate, $endDate, $holidays) { + // TODO: this function needs a fix, as it assumes Sat + Sun weekends, + // and will not work properly for other week types (ex. Arabic weeks). + // do strtotime calculations just once $endDate = strtotime($endDate); $startDate = strtotime($startDate); @@ -157,4 +187,4 @@ class MonthlyQuota { return $workingDays; } -} \ No newline at end of file +}