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   // insert function inserts a new timesheet into database.
 
  58   static function insert($fields)
 
  60     // First, we obtain report items.
 
  62     // Obtain session bean with report attributes.
 
  63     $bean = new ActionForm('reportBean', new Form('reportForm'));
 
  64     $options = ttReportHelper::getReportOptions($bean);
 
  65     $report_items = ttReportHelper::getItems($options);
 
  67     // Prepare ids for time and expense items, at the same time checking
 
  68     // if we can proceed with creating a timesheet.
 
  69     $canCreateTimesheet = true;
 
  70     $first_user_id = null;
 
  72     foreach ($report_items as $report_item) {
 
  75         $first_user_id = $report_item['user_id'];
 
  77         if ($report_item['user_id'] != $first_user_id) {
 
  78           // We have items for multiple users.
 
  79           $canCreateTimesheet = false;
 
  83       // Check timesheet id.
 
  84       if ($report_item['timesheet_id']) {
 
  85         // We have an item already assigned to a timesheet.
 
  86         $canCreateTimesheet = false;
 
  89       if ($report_item['type'] == 1)
 
  90         $time_ids[] = $report_item['id'];
 
  91       elseif ($report_item['type'] == 2)
 
  92         $expense_ids[] = $report_item['id'];
 
  94     if (!$canCreateTimesheet) return false;
 
  96     // Make comma-seperated lists of ids for sql.
 
  98       $comma_separated_time_ids = implode(',', $time_ids);
 
 100       $comma_separated_expense_ids = implode(',', $expense_ids);
 
 102     // Create a new timesheet entry.
 
 104     $mdb2 = getConnection();
 
 106     $group_id = $user->getGroup();
 
 107     $org_id = $user->org_id;
 
 109     $user_id = $fields['user_id'];
 
 110     $client_id = $fields['client_id'];
 
 111     $name = $fields['name'];
 
 112     $submitter_comment = $fields['comment'];
 
 114     $sql = "insert into tt_timesheets (user_id, group_id, org_id, client_id, name, submitter_comment)".
 
 115       " values ($user_id, $group_id, $org_id, ".$mdb2->quote($client_id).", ".$mdb2->quote($name).", ".$mdb2->quote($submitter_comment).")";
 
 116     $affected = $mdb2->exec($sql);
 
 117     if (is_a($affected, 'PEAR_Error'))
 
 120     $last_id = $mdb2->lastInsertID('tt_timesheets', 'id');
 
 122     // Associate time items with timesheet.
 
 123     if ($comma_separated_time_ids) {
 
 124       $sql = "update tt_log set timesheet_id = $last_id".
 
 125         " where id in ($comma_separated_time_ids) and group_id = $group_id and org_id = $org_id";
 
 126       $affected = $mdb2->exec($sql);
 
 127       if (is_a($affected, 'PEAR_Error'))
 
 131     // Associate expense items with timesheet.
 
 132     if ($comma_separated_expense_ids) {
 
 133       $sql = "update tt_expense_items set timesheet_id = $last_id".
 
 134         " where id in ($comma_separated_expense_ids) and group_id = $group_id and org_id = $org_id";
 
 135       $affected = $mdb2->exec($sql);
 
 136       if (is_a($affected, 'PEAR_Error'))
 
 143   // The getActiveTimesheets obtains active timesheets for a user.
 
 144   static function getActiveTimesheets($user_id)
 
 147     $mdb2 = getConnection();
 
 149     $group_id = $user->getGroup();
 
 150     $org_id = $user->org_id;
 
 152     // $addPaidStatus = $user->isPluginEnabled('ps');
 
 155     if ($user->isClient())
 
 156       $client_part = "and ts.client_id = $user->client_id";
 
 158     $sql = "select ts.id, ts.name, ts.client_id, c.name as client_name, ts.submit_status, ts.approval_status from tt_timesheets ts".
 
 159       " left join tt_clients c on (c.id = ts.client_id)".
 
 160       " where ts.status = 1 and ts.group_id = $group_id and ts.org_id = $org_id and ts.user_id = $user_id".
 
 161       " $client_part order by ts.name";
 
 162     $res = $mdb2->query($sql);
 
 164     if (!is_a($res, 'PEAR_Error')) {
 
 165       $dt = new DateAndTime(DB_DATEFORMAT);
 
 166       while ($val = $res->fetchRow()) {
 
 167         //if ($addPaidStatus)
 
 168         //  $val['paid'] = ttTimesheetHelper::isPaid($val['id']);
 
 175   // The getInactiveTimesheets obtains inactive timesheets for a user.
 
 176   static function getInactiveTimesheets($user_id)
 
 179     $mdb2 = getConnection();
 
 181     $group_id = $user->getGroup();
 
 182     $org_id = $user->org_id;
 
 184     // $addPaidStatus = $user->isPluginEnabled('ps');
 
 187     if ($user->isClient())
 
 188       $client_part = "and ts.client_id = $user->client_id";
 
 190     $sql = "select ts.id, ts.name, ts.client_id, c.name as client_name, ts.submit_status, ts.approval_status from tt_timesheets ts".
 
 191       " left join tt_clients c on (c.id = ts.client_id)".
 
 192       " where ts.status = 0 and ts.group_id = $group_id and ts.org_id = $org_id and ts.user_id = $user_id".
 
 193       " $client_part order by ts.name";
 
 194     $res = $mdb2->query($sql);
 
 196     if (!is_a($res, 'PEAR_Error')) {
 
 197       $dt = new DateAndTime(DB_DATEFORMAT);
 
 198       while ($val = $res->fetchRow()) {
 
 199         //if ($addPaidStatus)
 
 200         //  $val['paid'] = ttTimesheetHelper::isPaid($val['id']);
 
 207   // getTimesheet - obtains timesheet data from the database.
 
 208   static function getTimesheet($timesheet_id) {
 
 210     $mdb2 = getConnection();
 
 212     $group_id = $user->getGroup();
 
 213     $org_id = $user->org_id;
 
 215     if ($user->isClient()) $client_part = "and ts.client_id = $user->client_id";
 
 217     $sql = "select ts.id, ts.user_id, u.name as user_name, ts.client_id, c.name as client_name,".
 
 218       " ts.name, ts.submitter_comment, ts.submit_status, ts.approval_status, ts.manager_comment from tt_timesheets ts".
 
 219       " left join tt_users u on (u.id = ts.user_id)".
 
 220       " left join tt_clients c on (c.id = ts.client_id)".
 
 221       " where ts.id = $timesheet_id and ts.group_id = $group_id and ts.org_id = $org_id $client_part and ts.status is not null";
 
 222     $res = $mdb2->query($sql);
 
 223     if (!is_a($res, 'PEAR_Error')) {
 
 224       if ($val = $res->fetchRow())
 
 230   // delete - deletes timesheet from the database.
 
 231   static function delete($timesheet_id) {
 
 233     $mdb2 = getConnection();
 
 235     $group_id = $user->getGroup();
 
 236     $org_id = $user->org_id;
 
 238     // Handle time records.
 
 239     $sql = "update tt_log set timesheet_id = null".
 
 240       " where timesheet_id = $timesheet_id and group_id = $group_id and org_id = $org_id";
 
 241     $affected = $mdb2->exec($sql);
 
 242     if (is_a($affected, 'PEAR_Error')) return false;
 
 244     // Handle expense items.
 
 245     $sql = "update tt_expense_items set timesheet_id = null".
 
 246       " where timesheet_id = $timesheet_id and group_id = $group_id and org_id = $org_id";
 
 247     $affected = $mdb2->exec($sql);
 
 248     if (is_a($affected, 'PEAR_Error')) return false;
 
 251     $sql = "update tt_timesheets set status = null".
 
 252       " where id = $timesheet_id and group_id = $group_id and org_id = $org_id";
 
 253     $affected = $mdb2->exec($sql);
 
 254     return (!is_a($affected, 'PEAR_Error'));
 
 257   // update function - updates the timesheet in database.
 
 258   static function update($fields) {
 
 260     $mdb2 = getConnection();
 
 262     $group_id = $user->getGroup();
 
 263     $org_id = $user->org_id;
 
 265     $timesheet_id = $fields['id']; // Timesheet we are updating.
 
 266     $name = $fields['name']; // Timesheet name.
 
 267     $submitter_comment = $fields['submitter_comment'];
 
 268     $status = $fields['status']; // Project status.
 
 270     $sql = "update tt_timesheets set name = ".$mdb2->quote($name).", submitter_comment = ".$mdb2->quote($submitter_comment).
 
 271       ", status = ".$mdb2->quote($status).
 
 272       " where id = $timesheet_id and group_id = $group_id and org_id = $org_id";
 
 273     $affected = $mdb2->exec($sql);
 
 274     return (!is_a($affected, 'PEAR_Error'));
 
 277   // isUserValid function is used during access checks and determines whether user id, passed in post, is valid
 
 278   // in current context.
 
 279   static function isUserValid($user_id) {
 
 280     // We have to cover several situations.
 
 282     // 1) User is a client.
 
 283     // 2) User with view_all_timesheets rights.
 
 284     // 3) User with view_timesheets rights.
 
 289     // A client must have view_client_timesheets and
 
 290     // aser must be assigned to one of client projects.
 
 291     if ($user->isClient()) {
 
 292       if (!$user->can('view_client_timesheets'))
 
 294       $valid_users = ttGroupHelper::getUsersForClient($user->client_id);
 
 301   // getReportOptions prepares $options array to be used with ttReportHelper
 
 302   // to obtain items for timesheet view.
 
 303   static function getReportOptions($timesheet) {
 
 305     $group_by_client = $user->isPluginEnabled('cl') && !$timesheet['client_id'];
 
 306     $trackingMode = $user->getTrackingMode();
 
 307     $group_by_project = MODE_PROJECTS == $trackingMode || MODE_PROJECTS_AND_TASKS == $trackingMode;
 
 309     $options['timesheet_id'] = $timesheet['id'];
 
 310     $options['client_id'] = $timesheet['client_id'];
 
 311     $options['users'] = $timesheet['user_id'];
 
 312     $options['show_durarion'] = 1;
 
 313     $options['show_cost'] = 1; // To include expenses.
 
 314     $options['show_totals_only'] = 1;
 
 315     $options['group_by1'] = 'date';
 
 316     if ($group_by_client || $group_by_project) {
 
 317       $options['group_by2'] = $group_by_client ? 'client' : 'project';
 
 319     if ($options['group_by2'] && $options['group_by2'] != 'project' && $group_by_project) {
 
 320       $options['group_by3'] = 'project';
 
 325   // getApprovers obtains a list of users who can approve a timesheet for a given user
 
 326   // and also have an email to receive a notification about it.
 
 327   static function getApprovers($user_id) {
 
 329     $mdb2 = getConnection();
 
 331     $group_id = $user->getGroup();
 
 332     $org_id = $user->org_id;
 
 334     $approvers = array();
 
 335     $rank = ttUserHelper::getUserRank($user_id);
 
 336     $sql = "select u.id, u.name, u.email".
 
 338       " left join tt_roles r on (r.id = u.role_id)".
 
 339       " where u.status = 1 and u.email is not null".
 
 340       " and (r.rights like '%approve_all_timesheets%' or (r.rank > $rank and r.rights like '%approve_timesheets%'))";
 
 341     $res = $mdb2->query($sql);
 
 342     if (!is_a($res, 'PEAR_Error')) {
 
 343       while ($val = $res->fetchRow()) {
 
 350   // submitTimesheet marks a timesheet as submitted and sends an email to an approver.
 
 351   static function submitTimesheet($fields) {
 
 353     $mdb2 = getConnection();
 
 355     $group_id = $user->getGroup();
 
 356     $org_id = $user->org_id;
 
 358     // First, mark a timesheet as submitted.
 
 359     // Even if mail part below does not work, this will get us a functioning workflow
 
 360     // (without email notifications).
 
 361     $timesheet_id = $fields['timesheet_id'];
 
 362     $sql = "update tt_timesheets set submit_status = 1".
 
 363       " where id = $timesheet_id 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 approver here...
 
 368     // $approver_id = $fields['approver_id'];
 
 373   // approveTimesheet marks a timesheet as approved and sends an email to submitter.
 
 374   static function approveTimesheet($fields) {
 
 376     $mdb2 = getConnection();
 
 378     $group_id = $user->getGroup();
 
 379     $org_id = $user->org_id;
 
 381     // First, mark a timesheet as approved.
 
 382     // Even if mail part below does not work, this will get us a functioning workflow
 
 383     // (without email notifications).
 
 384     $timesheet_id = $fields['timesheet_id'];
 
 385     $manager_comment = $fields['comment'];
 
 387     $sql = "update tt_timesheets set approval_status = 1, manager_comment = ".$mdb2->quote($manager_comment).
 
 388       " where id = $timesheet_id and submit_status = 1 and group_id = $group_id and org_id = $org_id";
 
 389     $affected = $mdb2->exec($sql);
 
 390     if (is_a($affected, 'PEAR_Error')) return false;
 
 392     // TODO: send email to submitter here...
 
 396   // disapproveTimesheet marks a timesheet as approved and sends an email to submitter.
 
 397   static function disapproveTimesheet($fields) {
 
 399     $mdb2 = getConnection();
 
 401     $group_id = $user->getGroup();
 
 402     $org_id = $user->org_id;
 
 404     // First, mark a timesheet as disapproved.
 
 405     // Even if mail part below does not work, this will get us a functioning workflow
 
 406     // (without email notifications).
 
 407     $timesheet_id = $fields['timesheet_id'];
 
 408     $manager_comment = $fields['comment'];
 
 410     $sql = "update tt_timesheets set approval_status = 0, manager_comment = ".$mdb2->quote($manager_comment).
 
 411       " where id = $timesheet_id and submit_status = 1 and group_id = $group_id and org_id = $org_id";
 
 412     $affected = $mdb2->exec($sql);
 
 413     if (is_a($affected, 'PEAR_Error')) return false;
 
 415     // TODO: send email to submitter here...