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');
 
  32 // Class ttTimesheetHelper is used to help with project related tasks.
 
  33 class ttTimesheetHelper {
 
  35   // The getTimesheetByName looks up a project by name.
 
  36   static function getTimesheetByName($name, $user_id) {
 
  38     $mdb2 = getConnection();
 
  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 = 1 or status = 0)";
 
  46     $res = $mdb2->query($sql);
 
  47     if (!is_a($res, 'PEAR_Error')) {
 
  48       $val = $res->fetchRow();
 
  49       if ($val && $val['id'])
 
  55   // insert function inserts a new timesheet into database.
 
  56   static function insert($fields)
 
  59     $mdb2 = getConnection();
 
  61     $group_id = $user->getGroup();
 
  62     $org_id = $user->org_id;
 
  64     $user_id = $fields['user_id'];
 
  65     $client_id = $fields['client_id'];
 
  66     $name = $fields['name'];
 
  67     $submitter_comment = $fields['comment'];
 
  69     $sql = "insert into tt_timesheets (user_id, group_id, org_id, client_id, name, submitter_comment)".
 
  70       " values ($user_id, $group_id, $org_id, ".$mdb2->quote($client_id).", ".$mdb2->quote($name).", ".$mdb2->quote($submitter_comment).")";
 
  71     $affected = $mdb2->exec($sql);
 
  72     if (is_a($affected, 'PEAR_Error'))
 
  75     $last_id = $mdb2->lastInsertID('tt_timesheets', 'id');
 
  77     // TODO: Associate report items with new timesheet.
 
  82   // The getActiveTimesheets obtains active timesheets for a user.
 
  83   static function getActiveTimesheets($user_id)
 
  86     $mdb2 = getConnection();
 
  88     $group_id = $user->getGroup();
 
  89     $org_id = $user->org_id;
 
  91     // $addPaidStatus = $user->isPluginEnabled('ps');
 
  94     if ($user->isClient())
 
  95       $client_part = "and ts.client_id = $user->client_id";
 
  97     $sql = "select ts.id, ts.name, ts.client_id, c.name as client_name, ts.submit_status, ts.approval_status from tt_timesheets ts".
 
  98       " left join tt_clients c on (c.id = ts.client_id)".
 
  99       " where ts.status = 1 and ts.group_id = $group_id and ts.org_id = $org_id and ts.user_id = $user_id".
 
 100       " $client_part order by ts.name";
 
 101     $res = $mdb2->query($sql);
 
 103     if (!is_a($res, 'PEAR_Error')) {
 
 104       $dt = new DateAndTime(DB_DATEFORMAT);
 
 105       while ($val = $res->fetchRow()) {
 
 106         //if ($addPaidStatus)
 
 107         //  $val['paid'] = ttTimesheetHelper::isPaid($val['id']);
 
 114   // The getInactiveTimesheets obtains inactive timesheets for a user.
 
 115   static function getInactiveTimesheets($user_id)
 
 118     $mdb2 = getConnection();
 
 120     $group_id = $user->getGroup();
 
 121     $org_id = $user->org_id;
 
 123     // $addPaidStatus = $user->isPluginEnabled('ps');
 
 126     if ($user->isClient())
 
 127       $client_part = "and ts.client_id = $user->client_id";
 
 129     $sql = "select ts.id, ts.name, ts.client_id, c.name as client_name, ts.submit_status, ts.approval_status from tt_timesheets ts".
 
 130       " left join tt_clients c on (c.id = ts.client_id)".
 
 131       " where ts.status = 0 and ts.group_id = $group_id and ts.org_id = $org_id and ts.user_id = $user_id".
 
 132       " $client_part order by ts.name";
 
 133     $res = $mdb2->query($sql);
 
 135     if (!is_a($res, 'PEAR_Error')) {
 
 136       $dt = new DateAndTime(DB_DATEFORMAT);
 
 137       while ($val = $res->fetchRow()) {
 
 138         //if ($addPaidStatus)
 
 139         //  $val['paid'] = ttTimesheetHelper::isPaid($val['id']);
 
 146   // getTimesheet - obtains timesheet data from the database.
 
 147   static function getTimesheet($timesheet_id) {
 
 149     $mdb2 = getConnection();
 
 151     $group_id = $user->getGroup();
 
 152     $org_id = $user->org_id;
 
 154     if ($user->isClient()) $client_part = "and client_id = $user->client_id";
 
 156     $sql = "select * from tt_timesheets".
 
 157       " where id = $timesheet_id and group_id = $group_id and org_id = $org_id $client_part and status = 1";
 
 158     $res = $mdb2->query($sql);
 
 159     if (!is_a($res, 'PEAR_Error')) {
 
 160       if ($val = $res->fetchRow())
 
 166   // delete - deletes timesheet from the database.
 
 167   static function delete($timesheet_id) {
 
 169     $mdb2 = getConnection();
 
 171     $group_id = $user->getGroup();
 
 172     $org_id = $user->org_id;
 
 174     // Handle time records.
 
 175     $sql = "update tt_log set timesheet_id = null".
 
 176       " where timesheet_id = $timesheet_id and group_id = $group_id and org_id = $org_id";
 
 177     $affected = $mdb2->exec($sql);
 
 178     if (is_a($affected, 'PEAR_Error')) return false;
 
 180     // Handle expense items.
 
 181     $sql = "update tt_expense_items set timesheet_id = null".
 
 182       " where timesheet_id = $timesheet_id and group_id = $group_id and org_id = $org_id";
 
 183     $affected = $mdb2->exec($sql);
 
 184     if (is_a($affected, 'PEAR_Error')) return false;
 
 187     $sql = "update tt_timesheets set status = null".
 
 188       " where id = $timesheet_id and group_id = $group_id and org_id = $org_id";
 
 189     $affected = $mdb2->exec($sql);
 
 190     return (!is_a($affected, 'PEAR_Error'));