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');
 
  30 import('ttGroupHelper');
 
  31 import('form.ActionForm');
 
  32 import('ttReportHelper');
 
  34 // Class ttTimesheetHelper is used to help with project related tasks.
 
  35 class ttTimesheetHelper {
 
  37   // The getTimesheetByName looks up a project by name.
 
  38   static function getTimesheetByName($name, $user_id) {
 
  40     $mdb2 = getConnection();
 
  42     $group_id = $user->getGroup();
 
  43     $org_id = $user->org_id;
 
  45     $sql = "select id from tt_timesheets".
 
  46       " where group_id = $group_id and org_id = $org_id and user_id = $user_id and name = ".$mdb2->quote($name).
 
  47       " and (status = 1 or status = 0)";
 
  48     $res = $mdb2->query($sql);
 
  49     if (!is_a($res, 'PEAR_Error')) {
 
  50       $val = $res->fetchRow();
 
  51       if ($val && $val['id'])
 
  57   // createTimesheet function creates a new timesheet.
 
  58   static function createTimesheet($fields)
 
  60     // Create a new timesheet entry.
 
  62     $mdb2 = getConnection();
 
  64     $user_id = $user->getUser();
 
  65     $group_id = $user->getGroup();
 
  66     $org_id = $user->org_id;
 
  68     $client_id = $fields['client_id'];
 
  69     $project_id = $fields['project_id'];
 
  70     $name = $fields['name'];
 
  71     $comment = $fields['comment'];
 
  73     $start_date = new DateAndTime($user->date_format, $fields['start_date']);
 
  74     $start = $start_date->toString(DB_DATEFORMAT);
 
  76     $end_date = new DateAndTime($user->date_format, $fields['end_date']);
 
  77     $end = $end_date->toString(DB_DATEFORMAT);
 
  79     $sql = "insert into tt_timesheets (user_id, group_id, org_id, client_id, project_id, name, comment, start_date, end_date)".
 
  80       " values ($user_id, $group_id, $org_id, ".$mdb2->quote($client_id).", ".$mdb2->quote($project_id).", ".$mdb2->quote($name).
 
  81       ", ".$mdb2->quote($comment).", ".$mdb2->quote($start).", ".$mdb2->quote($end).")";
 
  82     $affected = $mdb2->exec($sql);
 
  83     if (is_a($affected, 'PEAR_Error'))
 
  86     $last_id = $mdb2->lastInsertID('tt_timesheets', 'id');
 
  88     // Associate time items with timesheet.
 
  89     if (isset($fields['client'])) $client_id = (int) $fields['client_id'];
 
  90     if (isset($fields['project_id'])) $project_id = (int) $fields['project_id'];
 
  92     if ($client_id) $client_part = " and client_id = $client_id";
 
  93     if ($project_id) $project_part = " and project_id = $project_id";
 
  95     $sql = "update tt_log set timesheet_id = $last_id".
 
  96       " where status = 1 $client_part $project_part and timesheet_id is null".
 
  97       " and date >= ".$mdb2->quote($start)." and date <= ".$mdb2->quote($end).
 
  98       " and user_id = $user_id and group_id = $group_id and org_id = $org_id";
 
  99     $affected = $mdb2->exec($sql);
 
 100     if (is_a($affected, 'PEAR_Error'))
 
 106   // The getActiveTimesheets obtains active timesheets for a user.
 
 107   static function getActiveTimesheets($user_id)
 
 110     $mdb2 = getConnection();
 
 112     $group_id = $user->getGroup();
 
 113     $org_id = $user->org_id;
 
 115     // $addPaidStatus = $user->isPluginEnabled('ps');
 
 118     if ($user->isClient())
 
 119       $client_part = "and ts.client_id = $user->client_id";
 
 121     $sql = "select ts.id, ts.name, ts.client_id, c.name as client_name, ts.submit_status, ts.approve_status from tt_timesheets ts".
 
 122       " left join tt_clients c on (c.id = ts.client_id)".
 
 123       " where ts.status = 1 and ts.group_id = $group_id and ts.org_id = $org_id and ts.user_id = $user_id".
 
 124       " $client_part order by ts.name";
 
 125     $res = $mdb2->query($sql);
 
 127     if (!is_a($res, 'PEAR_Error')) {
 
 128       $dt = new DateAndTime(DB_DATEFORMAT);
 
 129       while ($val = $res->fetchRow()) {
 
 130         //if ($addPaidStatus)
 
 131         //  $val['paid'] = ttTimesheetHelper::isPaid($val['id']);
 
 138   // The getInactiveTimesheets obtains inactive timesheets for a user.
 
 139   static function getInactiveTimesheets($user_id)
 
 142     $mdb2 = getConnection();
 
 144     $group_id = $user->getGroup();
 
 145     $org_id = $user->org_id;
 
 147     // $addPaidStatus = $user->isPluginEnabled('ps');
 
 150     if ($user->isClient())
 
 151       $client_part = "and ts.client_id = $user->client_id";
 
 153     $sql = "select ts.id, ts.name, ts.client_id, c.name as client_name, ts.submit_status, ts.approval_status from tt_timesheets ts".
 
 154       " left join tt_clients c on (c.id = ts.client_id)".
 
 155       " where ts.status = 0 and ts.group_id = $group_id and ts.org_id = $org_id and ts.user_id = $user_id".
 
 156       " $client_part order by ts.name";
 
 157     $res = $mdb2->query($sql);
 
 159     if (!is_a($res, 'PEAR_Error')) {
 
 160       $dt = new DateAndTime(DB_DATEFORMAT);
 
 161       while ($val = $res->fetchRow()) {
 
 162         //if ($addPaidStatus)
 
 163         //  $val['paid'] = ttTimesheetHelper::isPaid($val['id']);
 
 170   // getTimesheet - obtains timesheet data from the database.
 
 171   static function getTimesheet($timesheet_id) {
 
 173     $mdb2 = getConnection();
 
 175     $user_id = $user->getUser();
 
 176     $group_id = $user->getGroup();
 
 177     $org_id = $user->org_id;
 
 179     $sql = "select ts.*, u.name as user_name, c.name as client_name,".
 
 180       " p.name as project_name from tt_timesheets ts".
 
 181       " left join tt_users u on (ts.user_id = u.id)".
 
 182       " left join tt_clients c on (ts.client_id = c.id)".
 
 183       " left join tt_projects p on (ts.project_id = p.id)".
 
 184       " 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";
 
 185     $res = $mdb2->query($sql);
 
 186     if (!is_a($res, 'PEAR_Error')) {
 
 187       if ($val = $res->fetchRow())
 
 193   // delete - deletes timesheet from the database.
 
 194   static function delete($timesheet_id) {
 
 196     $mdb2 = getConnection();
 
 198     $group_id = $user->getGroup();
 
 199     $org_id = $user->org_id;
 
 201     // Handle time records.
 
 202     $sql = "update tt_log set timesheet_id = null".
 
 203       " where timesheet_id = $timesheet_id and group_id = $group_id and org_id = $org_id";
 
 204     $affected = $mdb2->exec($sql);
 
 205     if (is_a($affected, 'PEAR_Error')) return false;
 
 207     // Handle expense items.
 
 208     $sql = "update tt_expense_items set timesheet_id = null".
 
 209       " where timesheet_id = $timesheet_id and group_id = $group_id and org_id = $org_id";
 
 210     $affected = $mdb2->exec($sql);
 
 211     if (is_a($affected, 'PEAR_Error')) return false;
 
 214     $sql = "update tt_timesheets set status = null".
 
 215       " where id = $timesheet_id and group_id = $group_id and org_id = $org_id";
 
 216     $affected = $mdb2->exec($sql);
 
 217     return (!is_a($affected, 'PEAR_Error'));
 
 220   // update function - updates the timesheet in database.
 
 221   static function update($fields) {
 
 223     $mdb2 = getConnection();
 
 225     $group_id = $user->getGroup();
 
 226     $org_id = $user->org_id;
 
 228     $timesheet_id = $fields['id']; // Timesheet we are updating.
 
 229     $name = $fields['name']; // Timesheet name.
 
 230     $submitter_comment = $fields['submitter_comment'];
 
 231     $status = $fields['status']; // Project status.
 
 233     $sql = "update tt_timesheets set name = ".$mdb2->quote($name).", submitter_comment = ".$mdb2->quote($submitter_comment).
 
 234       ", status = ".$mdb2->quote($status).
 
 235       " where id = $timesheet_id and group_id = $group_id and org_id = $org_id";
 
 236     $affected = $mdb2->exec($sql);
 
 237     return (!is_a($affected, 'PEAR_Error'));
 
 240   // isUserValid function is used during access checks and determines whether user id, passed in post, is valid
 
 241   // in current context.
 
 242   static function isUserValid($user_id) {
 
 243     // We have to cover several situations.
 
 247     // TODO: we are currently re-designing timesheets.
 
 248     // Clients are not supposed to view them at all.
 
 249     // And the post will change on_behalf user, to keep things consistent.
 
 253   // getReportOptions prepares $options array to be used with ttReportHelper
 
 254   // to obtain items for timesheet view.
 
 255   static function getReportOptions($timesheet) {
 
 257     $group_by_client = $user->isPluginEnabled('cl') && !$timesheet['client_id'];
 
 258     $trackingMode = $user->getTrackingMode();
 
 259     $group_by_project = MODE_PROJECTS == $trackingMode || MODE_PROJECTS_AND_TASKS == $trackingMode;
 
 261     $options['timesheet_id'] = $timesheet['id'];
 
 262     $options['client_id'] = $timesheet['client_id'];
 
 263     $options['users'] = $timesheet['user_id'];
 
 264     $options['show_durarion'] = 1;
 
 265     $options['show_cost'] = 1; // To include expenses.
 
 266     $options['show_totals_only'] = 1;
 
 267     $options['group_by1'] = 'date';
 
 268     if ($group_by_client || $group_by_project) {
 
 269       $options['group_by2'] = $group_by_client ? 'client' : 'project';
 
 271     if ($options['group_by2'] && $options['group_by2'] != 'project' && $group_by_project) {
 
 272       $options['group_by3'] = 'project';
 
 277   // getApprovers obtains a list of users who can approve a timesheet for a given user
 
 278   // and also have an email to receive a notification about it.
 
 279   static function getApprovers($user_id) {
 
 281     $mdb2 = getConnection();
 
 283     $group_id = $user->getGroup();
 
 284     $org_id = $user->org_id;
 
 286     $approvers = array();
 
 287     $rank = ttUserHelper::getUserRank($user_id);
 
 288     $sql = "select u.id, u.name, u.email".
 
 290       " left join tt_roles r on (r.id = u.role_id)".
 
 291       " where u.status = 1 and u.email is not null and u.group_id = $group_id and u.org_id = $org_id".
 
 292       " and (r.rank > $rank and r.rights like '%approve_timesheets%')";
 
 293     $res = $mdb2->query($sql);
 
 294     if (!is_a($res, 'PEAR_Error')) {
 
 295       while ($val = $res->fetchRow()) {
 
 302   // submitTimesheet marks a timesheet as submitted and sends an email to an approver.
 
 303   static function submitTimesheet($fields) {
 
 305     $mdb2 = getConnection();
 
 307     $group_id = $user->getGroup();
 
 308     $org_id = $user->org_id;
 
 310     // First, mark a timesheet as submitted.
 
 311     // Even if mail part below does not work, this will get us a functioning workflow
 
 312     // (without email notifications).
 
 313     $timesheet_id = $fields['timesheet_id'];
 
 314     $sql = "update tt_timesheets set submit_status = 1".
 
 315       " where id = $timesheet_id and group_id = $group_id and org_id = $org_id";
 
 316     $affected = $mdb2->exec($sql);
 
 317     if (is_a($affected, 'PEAR_Error')) return false;
 
 319     // TODO: send email to approver here...
 
 320     // $approver_id = $fields['approver_id'];
 
 325   // approveTimesheet marks a timesheet as approved and sends an email to submitter.
 
 326   static function approveTimesheet($fields) {
 
 328     $mdb2 = getConnection();
 
 330     $group_id = $user->getGroup();
 
 331     $org_id = $user->org_id;
 
 333     // First, mark a timesheet as approved.
 
 334     // Even if mail part below does not work, this will get us a functioning workflow
 
 335     // (without email notifications).
 
 336     $timesheet_id = $fields['timesheet_id'];
 
 337     $comment = $fields['comment'];
 
 339     $sql = "update tt_timesheets set approve_status = 1, approve_comment = ".$mdb2->quote($comment).
 
 340       " where id = $timesheet_id and submit_status = 1 and group_id = $group_id and org_id = $org_id";
 
 341     $affected = $mdb2->exec($sql);
 
 342     if (is_a($affected, 'PEAR_Error')) return false;
 
 344     // TODO: send email to submitter here...
 
 348   // disapproveTimesheet marks a timesheet as approved and sends an email to submitter.
 
 349   static function disapproveTimesheet($fields) {
 
 351     $mdb2 = getConnection();
 
 353     $group_id = $user->getGroup();
 
 354     $org_id = $user->org_id;
 
 356     // First, mark a timesheet as disapproved.
 
 357     // Even if mail part below does not work, this will get us a functioning workflow
 
 358     // (without email notifications).
 
 359     $timesheet_id = $fields['timesheet_id'];
 
 360     $comment = $fields['comment'];
 
 362     $sql = "update tt_timesheets set approve_status = 0, approve_comment = ".$mdb2->quote($comment).
 
 363       " where id = $timesheet_id and submit_status = 1 and group_id = $group_id and org_id = $org_id";
 
 364     $affected = $mdb2->exec($sql);
 
 365     if (is_a($affected, 'PEAR_Error')) return false;
 
 367     // TODO: send email to submitter here...
 
 371   // The timesheetItemsExist determines whether tt_log records exist in the specified period
 
 372   // for inclusion in a new timesheet.
 
 373   static function timesheetItemsExist($fields) {
 
 375     $mdb2 = getConnection();
 
 377     $user_id = $user->getUser();
 
 378     $group_id = $user->getGroup();
 
 379     $org_id = $user->org_id;
 
 381     if (isset($fields['client_id'])) $client_id = (int) $fields['client_id'];
 
 382     if (isset($fields['project_id'])) $project_id = (int) $fields['project_id'];
 
 384     $start_date = new DateAndTime($user->date_format, $fields['start_date']);
 
 385     $start = $start_date->toString(DB_DATEFORMAT);
 
 387     $end_date = new DateAndTime($user->date_format, $fields['end_date']);
 
 388     $end = $end_date->toString(DB_DATEFORMAT);
 
 391     if ($client_id) $client_part = " and client_id = $client_id";
 
 392     if ($project_id) $project_part = " and project_id = $project_id";
 
 394     $sql = "select count(*) as num from tt_log".
 
 395       " where status = 1 $client_part $project_part and timesheet_id is null".
 
 396       " and date >= ".$mdb2->quote($start)." and date <= ".$mdb2->quote($end).
 
 397       " and user_id = $user_id and group_id = $group_id and org_id = $org_id";
 
 398     $res = $mdb2->query($sql);
 
 399     if (!is_a($res, 'PEAR_Error')) {
 
 400       $val = $res->fetchRow();