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.
 
  11 // | There are only two ways to violate the license:
 
  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).
 
  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).
 
  21 // | This license applies to this document only, not any other software
 
  22 // | that it may be combined with.
 
  24 // +----------------------------------------------------------------------+
 
  26 // | https://www.anuko.com/time_tracker/credits.htm
 
  27 // +----------------------------------------------------------------------+
 
  29 import('ttUserHelper');
 
  31 // Class ttTimesheetHelper is used to help with project related tasks.
 
  32 class ttTimesheetHelper {
 
  34   // The getTimesheetByName looks up a project by name.
 
  35   static function getTimesheetByName($name) {
 
  37     $mdb2 = getConnection();
 
  39     $user_id = $user->getUser();
 
  40     $group_id = $user->getGroup();
 
  41     $org_id = $user->org_id;
 
  43     $sql = "select id from tt_timesheets".
 
  44       " where group_id = $group_id and org_id = $org_id and user_id = $user_id and name = ".$mdb2->quote($name).
 
  45       " and status is not null";
 
  46     $res = $mdb2->query($sql);
 
  47     if (!is_a($res, 'PEAR_Error')) {
 
  48       $val = $res->fetchRow();
 
  49       if ($val && $val['id'])
 
  55   // createTimesheet function creates a new timesheet.
 
  56   static function createTimesheet($fields)
 
  58     // Create a new timesheet entry.
 
  60     $mdb2 = getConnection();
 
  62     $user_id = $user->getUser();
 
  63     $group_id = $user->getGroup();
 
  64     $org_id = $user->org_id;
 
  66     $client_id = $fields['client_id'];
 
  67     $project_id = $fields['project_id'];
 
  68     $name = $fields['name'];
 
  69     $comment = $fields['comment'];
 
  71     $start_date = new DateAndTime($user->date_format, $fields['start_date']);
 
  72     $start = $start_date->toString(DB_DATEFORMAT);
 
  74     $end_date = new DateAndTime($user->date_format, $fields['end_date']);
 
  75     $end = $end_date->toString(DB_DATEFORMAT);
 
  77     $sql = "insert into tt_timesheets (user_id, group_id, org_id, client_id, project_id, name, comment, start_date, end_date)".
 
  78       " values ($user_id, $group_id, $org_id, ".$mdb2->quote($client_id).", ".$mdb2->quote($project_id).", ".$mdb2->quote($name).
 
  79       ", ".$mdb2->quote($comment).", ".$mdb2->quote($start).", ".$mdb2->quote($end).")";
 
  80     $affected = $mdb2->exec($sql);
 
  81     if (is_a($affected, 'PEAR_Error'))
 
  84     $last_id = $mdb2->lastInsertID('tt_timesheets', 'id');
 
  86     // Associate tt_log items with timesheet.
 
  87     if (isset($fields['client'])) $client_id = (int) $fields['client_id'];
 
  88     if (isset($fields['project_id'])) $project_id = (int) $fields['project_id'];
 
  90     if ($client_id) $client_part = " and client_id = $client_id";
 
  91     if ($project_id) $project_part = " and project_id = $project_id";
 
  93     $sql = "update tt_log set timesheet_id = $last_id".
 
  94       " where status = 1 $client_part $project_part and timesheet_id is null".
 
  95       " and date >= ".$mdb2->quote($start)." and date <= ".$mdb2->quote($end).
 
  96       " and user_id = $user_id and group_id = $group_id and org_id = $org_id";
 
  97     $affected = $mdb2->exec($sql);
 
  98     if (is_a($affected, 'PEAR_Error'))
 
 104   // The getActiveTimesheets obtains active timesheets for a user.
 
 105   static function getActiveTimesheets()
 
 108     $mdb2 = getConnection();
 
 110     $user_id = $user->getUser();
 
 111     $group_id = $user->getGroup();
 
 112     $org_id = $user->org_id;
 
 115     $sql = "select ts.id, ts.name, ts.client_id, c.name as client_name,".
 
 116       " ts.submit_status, ts.approve_status from tt_timesheets ts".
 
 117       " left join tt_clients c on (c.id = ts.client_id)".
 
 118       " where ts.status = 1 and ts.group_id = $group_id and ts.org_id = $org_id and ts.user_id = $user_id".
 
 120     $res = $mdb2->query($sql);
 
 122     if (!is_a($res, 'PEAR_Error')) {
 
 123       while ($val = $res->fetchRow()) {
 
 130   // The getInactiveTimesheets obtains inactive timesheets for a user.
 
 131   static function getInactiveTimesheets()
 
 134     $mdb2 = getConnection();
 
 136     $user_id = $user->getUser();
 
 137     $group_id = $user->getGroup();
 
 138     $org_id = $user->org_id;
 
 141     $sql = "select ts.id, ts.name, ts.client_id, c.name as client_name,".
 
 142       " ts.submit_status, ts.approve_status from tt_timesheets ts".
 
 143       " left join tt_clients c on (c.id = ts.client_id)".
 
 144       " where ts.status = 0 and ts.group_id = $group_id and ts.org_id = $org_id and ts.user_id = $user_id".
 
 146     $res = $mdb2->query($sql);
 
 148     if (!is_a($res, 'PEAR_Error')) {
 
 149       while ($val = $res->fetchRow()) {
 
 156   // getTimesheet - obtains timesheet data from the database.
 
 157   static function getTimesheet($timesheet_id) {
 
 159     $mdb2 = getConnection();
 
 161     $user_id = $user->getUser();
 
 162     $group_id = $user->getGroup();
 
 163     $org_id = $user->org_id;
 
 165     $sql = "select ts.*, u.name as user_name, c.name as client_name,".
 
 166       " p.name as project_name from tt_timesheets ts".
 
 167       " left join tt_users u on (ts.user_id = u.id)".
 
 168       " left join tt_clients c on (ts.client_id = c.id)".
 
 169       " left join tt_projects p on (ts.project_id = p.id)".
 
 170       " where ts.id = $timesheet_id and ts.user_id = $user_id and ts.group_id = $group_id and ts.org_id = $org_id and ts.status is not null";
 
 171     $res = $mdb2->query($sql);
 
 172     if (!is_a($res, 'PEAR_Error')) {
 
 173       if ($val = $res->fetchRow())
 
 179   // delete - deletes timesheet from the database.
 
 180   static function delete($timesheet_id) {
 
 182     $mdb2 = getConnection();
 
 184     $user_id = $user->getUser();
 
 185     $group_id = $user->getGroup();
 
 186     $org_id = $user->org_id;
 
 188     // Handle tt_log records.
 
 189     $sql = "update tt_log set timesheet_id = null".
 
 190       " where timesheet_id = $timesheet_id and user_id = $user_id and group_id = $group_id and org_id = $org_id";
 
 191     $affected = $mdb2->exec($sql);
 
 192     if (is_a($affected, 'PEAR_Error')) return false;
 
 195     $sql = "update tt_timesheets set status = null".
 
 196       " where id = $timesheet_id and user_id = $user_id and group_id = $group_id and org_id = $org_id";
 
 197     $affected = $mdb2->exec($sql);
 
 198     return (!is_a($affected, 'PEAR_Error'));
 
 201   // update function - updates the timesheet in database.
 
 202   static function update($fields) {
 
 204     $mdb2 = getConnection();
 
 206     $user_id = $user->getUser();
 
 207     $group_id = $user->getGroup();
 
 208     $org_id = $user->org_id;
 
 210     $timesheet_id = $fields['id']; // Timesheet we are updating.
 
 211     $name = $fields['name']; // Timesheet name.
 
 212     $comment = $fields['comment'];
 
 213     $status = $fields['status']; // Timesheet status.
 
 215     $sql = "update tt_timesheets set name = ".$mdb2->quote($name).", comment = ".$mdb2->quote($comment).
 
 216       ", status = ".$mdb2->quote($status).
 
 217       " where id = $timesheet_id and user_id = $user_id and group_id = $group_id and org_id = $org_id";
 
 218     $affected = $mdb2->exec($sql);
 
 219     return (!is_a($affected, 'PEAR_Error'));
 
 222   // getReportOptions prepares $options array to be used with ttReportHelper
 
 223   // to obtain items for timesheet view.
 
 224   static function getReportOptions($timesheet) {
 
 226     $group_by_client = $user->isPluginEnabled('cl') && !$timesheet['client_id'];
 
 227     $trackingMode = $user->getTrackingMode();
 
 228     $group_by_project = MODE_PROJECTS == $trackingMode || MODE_PROJECTS_AND_TASKS == $trackingMode;
 
 230     $options['timesheet_id'] = $timesheet['id'];
 
 231     $options['group_by1'] = 'date';
 
 232     if ($group_by_client || $group_by_project) {
 
 233       $options['group_by2'] = $group_by_client ? 'client' : 'project';
 
 235     if ($options['group_by2'] && $options['group_by2'] != 'project' && $group_by_project) {
 
 236       $options['group_by3'] = 'project';
 
 241   // getApprovers obtains a list of users who can approve a timesheet for a given user
 
 242   // and also have an email to receive a notification about it.
 
 243   static function getApprovers() {
 
 245     $mdb2 = getConnection();
 
 247     $user_id = $user->getUser();
 
 248     $group_id = $user->getGroup();
 
 249     $org_id = $user->org_id;
 
 251     $approvers = array();
 
 252     $rank = ttUserHelper::getUserRank($user_id);
 
 253     $sql = "select u.id, u.name, u.email".
 
 255       " left join tt_roles r on (r.id = u.role_id)".
 
 256       " where u.status = 1 and u.email is not null and u.group_id = $group_id and u.org_id = $org_id".
 
 257       " and (r.rank > $rank and r.rights like '%approve_timesheets%')";
 
 258     $res = $mdb2->query($sql);
 
 259     if (!is_a($res, 'PEAR_Error')) {
 
 260       while ($val = $res->fetchRow()) {
 
 267   // submitTimesheet marks a timesheet as submitted and also sends an email
 
 268   // to a selected approver.
 
 269   static function submitTimesheet($fields) {
 
 271     $mdb2 = getConnection();
 
 273     $user_id = $user->getUser();
 
 274     $group_id = $user->getGroup();
 
 275     $org_id = $user->org_id;
 
 277     // First, mark timesheet as submitted.
 
 278     // Even if mail part below does not work, this will get us a functioning workflow
 
 279     // without email notification.
 
 280     $timesheet_id = $fields['timesheet_id'];
 
 281     $sql = "update tt_timesheets set submit_status = 1".
 
 282       " where id = $timesheet_id and user_id = $user_id and group_id = $group_id and org_id = $org_id";
 
 283     $affected = $mdb2->exec($sql);
 
 284     if (is_a($affected, 'PEAR_Error')) return false;
 
 286     // TODO: send email to approver here...
 
 287     // $approver_id = $fields['approver_id'];
 
 292   // approveTimesheet marks a timesheet as approved and sends an email to submitter.
 
 293   static function approveTimesheet($fields) {
 
 295     $mdb2 = getConnection();
 
 297     $user_id = $user->getUser();
 
 298     $group_id = $user->getGroup();
 
 299     $org_id = $user->org_id;
 
 301     // First, mark timesheet as approved.
 
 302     // Even if mail part below does not work, this will get us a functioning workflow
 
 303     // without email notification.
 
 304     $timesheet_id = $fields['timesheet_id'];
 
 305     $comment = $fields['comment'];
 
 307     $sql = "update tt_timesheets set approve_status = 1, approve_comment = ".$mdb2->quote($comment).
 
 308       " where id = $timesheet_id and submit_status = 1 and user_id = $user_id and group_id = $group_id and org_id = $org_id";
 
 309     $affected = $mdb2->exec($sql);
 
 310     if (is_a($affected, 'PEAR_Error')) return false;
 
 312     // TODO: send email to submitter here...
 
 316   // disapproveTimesheet marks a timesheet as approved and sends an email to submitter.
 
 317   static function disapproveTimesheet($fields) {
 
 319     $mdb2 = getConnection();
 
 321     $user_id = $user->getUser();
 
 322     $group_id = $user->getGroup();
 
 323     $org_id = $user->org_id;
 
 325     // First, mark timesheet as disapproved.
 
 326     // Even if mail part below does not work, this will get us a functioning workflow
 
 327     // without email notification.
 
 328     $timesheet_id = $fields['timesheet_id'];
 
 329     $comment = $fields['comment'];
 
 331     $sql = "update tt_timesheets set approve_status = 0, approve_comment = ".$mdb2->quote($comment).
 
 332       " where id = $timesheet_id and submit_status = 1 and user_id = $user_id and group_id = $group_id and org_id = $org_id";
 
 333     $affected = $mdb2->exec($sql);
 
 334     if (is_a($affected, 'PEAR_Error')) return false;
 
 336     // TODO: send email to submitter here...
 
 340   // The timesheetItemsExist determines whether tt_log records exist in the specified period
 
 341   // for inclusion in a new timesheet.
 
 342   static function timesheetItemsExist($fields) {
 
 344     $mdb2 = getConnection();
 
 346     $user_id = $user->getUser();
 
 347     $group_id = $user->getGroup();
 
 348     $org_id = $user->org_id;
 
 350     if (isset($fields['client_id'])) $client_id = (int) $fields['client_id'];
 
 351     if (isset($fields['project_id'])) $project_id = (int) $fields['project_id'];
 
 353     $start_date = new DateAndTime($user->date_format, $fields['start_date']);
 
 354     $start = $start_date->toString(DB_DATEFORMAT);
 
 356     $end_date = new DateAndTime($user->date_format, $fields['end_date']);
 
 357     $end = $end_date->toString(DB_DATEFORMAT);
 
 360     if ($client_id) $client_part = " and client_id = $client_id";
 
 361     if ($project_id) $project_part = " and project_id = $project_id";
 
 363     $sql = "select count(*) as num from tt_log".
 
 364       " where status = 1 $client_part $project_part and timesheet_id is null".
 
 365       " and date >= ".$mdb2->quote($start)." and date <= ".$mdb2->quote($end).
 
 366       " and user_id = $user_id and group_id = $group_id and org_id = $org_id";
 
 367     $res = $mdb2->query($sql);
 
 368     if (!is_a($res, 'PEAR_Error')) {
 
 369       $val = $res->fetchRow();
 
 378   // The overlaps function determines if a new timesheet overlaps with
 
 379   // an already existing timesheet.
 
 380   static function overlaps($fields) {
 
 382     $mdb2 = getConnection();
 
 384     $user_id = $user->getUser();
 
 385     $group_id = $user->getGroup();
 
 386     $org_id = $user->org_id;
 
 388     if (isset($fields['client_id'])) $client_id = (int) $fields['client_id'];
 
 389     if (isset($fields['project_id'])) $project_id = (int) $fields['project_id'];
 
 391     $start_date = new DateAndTime($user->date_format, $fields['start_date']);
 
 392     $start = $start_date->toString(DB_DATEFORMAT);
 
 393     $quoted_start = $mdb2->quote($start);
 
 395     $end_date = new DateAndTime($user->date_format, $fields['end_date']);
 
 396     $end = $end_date->toString(DB_DATEFORMAT);
 
 397     $quoted_end = $mdb2->quote($end);
 
 400     if ($client_id) $client_part = " and client_id = $client_id";
 
 401     if ($project_id) $project_part = " and project_id = $project_id";
 
 403     $sql = "select id from tt_timesheets".
 
 404       " where status is not null $client_part $project_part".
 
 405       " and (($quoted_start >= start_date and $quoted_start <= end_date)".
 
 406       "   or ($quoted_end >= start_date and $quoted_end <= end_date))".
 
 407       " and user_id = $user_id and group_id = $group_id and org_id = $org_id";
 
 408     $res = $mdb2->query($sql);
 
 409     if (!is_a($res, 'PEAR_Error')) {
 
 410       $val = $res->fetchRow();
 
 418   // The getMatchingTimesheets function retrieves a timesheet that "matches"
 
 419   // a report for an option to assign report items to it.
 
 421   // Condition: report range is fully enclosed in an existing timesheet with
 
 422   // matching client_id and project_id and null approved_status.
 
 423   static function getMatchingTimesheets($options) {
 
 425     $mdb2 = getConnection();
 
 427     $user_id = $user->getUser();
 
 428     $group_id = $user->getGroup();
 
 429     $org_id = $user->org_id;
 
 432     if (isset($options['users'])) {
 
 433       $comma_separated = $options['users'];
 
 434       $users = explode(',', $comma_separated);
 
 435       if (count($users) > 1 || $users[0] != $user->getUser())
 
 439     // No timesheets for expenses.
 
 440     if ($options['show_cost'] && $user->isPluginEnabled('ex')) return false;
 
 442     // Parts for client and project.
 
 443     if ($options['client_id']) $client_part = ' and (client_id is null or client_id = '.(int)$options['client_id'].')';
 
 444     if ($options['project_id']) $project_part = ' and (project_id is null or project_id = '.(int)$options['project_id'].')';
 
 446     // Determine start and end dates.
 
 447     $dateFormat = $user->getDateFormat();
 
 448     if ($options['period'])
 
 449       $period = new Period($options['period'], new DateAndTime($dateFormat));
 
 451       $period = new Period();
 
 453         new DateAndTime($dateFormat, $options['period_start']),
 
 454         new DateAndTime($dateFormat, $options['period_end']));
 
 456     $start = $period->getStartDate(DB_DATEFORMAT);
 
 457     $end = $period->getEndDate(DB_DATEFORMAT);
 
 460     $sql = "select id, name from tt_timesheets".
 
 461       " where ".$mdb2->quote($start)." >= start_date and ".$mdb2->quote($end)." <= end_date".
 
 462       "$client_part $project_part".
 
 463       " and user_id = $user_id and group_id = $group_id and org_id = $org_id".
 
 464       " and approve_status is null and status is not null";
 
 465     $res = $mdb2->query($sql);
 
 466     if (!is_a($res, 'PEAR_Error')) {
 
 467       while ($val = $res->fetchRow()) {