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 // Class ttClientHelper is used to help with client related tasks.
 
  30 class ttClientHelper {
 
  32   // The getClient looks up a client by id.
 
  33   static function getClient($client_id, $all_fields = false) {
 
  35     $mdb2 = getConnection();
 
  37     $group_id = $user->getGroup();
 
  38     $org_id = $user->org_id;
 
  46     $sql .= "from tt_clients where group_id = $group_id and org_id = $org_id".
 
  47       " and id = $client_id and (status = 1 or status = 0)";
 
  48     $res = $mdb2->query($sql);
 
  49     if (!is_a($res, 'PEAR_Error')) {
 
  50       $val = $res->fetchRow();
 
  56   // getClients - returns an array of active and inactive clients in a group.
 
  57   static function getClients()
 
  62     $mdb2 = getConnection();
 
  64     $sql = "select id, name from tt_clients where group_id = ".$user->getGroup()." and (status = 0 or status = 1) order by upper(name)";
 
  65     $res = $mdb2->query($sql);
 
  66     if (!is_a($res, 'PEAR_Error')) {
 
  67       while ($val = $res->fetchRow()) {
 
  74   // The getClientByName looks up a client by name.
 
  75   static function getClientByName($client_name) {
 
  77     $mdb2 = getConnection();
 
  80     $sql = "select id from tt_clients where group_id = ".$user->getGroup().
 
  81       " and name = ".$mdb2->quote($client_name)." and (status = 1 or status = 0)";
 
  83     $res = $mdb2->query($sql);
 
  84     if (!is_a($res, 'PEAR_Error')) {
 
  85       $val = $res->fetchRow();
 
  93   // The getDeletedClient looks up a deleted client by id.
 
  94   static function getDeletedClient($client_id) {
 
  96     $mdb2 = getConnection();
 
  99     $sql = "select name, address from tt_clients where group_id = ".$user->getGroup().
 
 100       " and id = $client_id and status is NULL";
 
 101     $res = $mdb2->query($sql);
 
 102     if (!is_a($res, 'PEAR_Error')) {
 
 103       $val = $res->fetchRow();
 
 109   // The delete function marks client as deleded.
 
 110   static function delete($id, $delete_client_entries) {
 
 112     $mdb2 = getConnection();
 
 115     // Handle custom field log records.
 
 116     if ($delete_client_entries) {
 
 117       $sql = "update tt_custom_field_log set status = NULL where log_id in (select id from tt_log where client_id = $id and status = 1)";
 
 118       $affected = $mdb2->exec($sql);
 
 119         if (is_a($affected, 'PEAR_Error'))
 
 123     // Handle time records.
 
 124     $modified_part = ', modified = now(), modified_ip = '.$mdb2->quote($_SERVER['REMOTE_ADDR']).', modified_by = '.$user->id;
 
 125     if ($delete_client_entries) {
 
 126       $sql = 'update tt_log set status = NULL'.$modified_part." where client_id = $id";
 
 127       $affected = $mdb2->exec($sql);
 
 128       if (is_a($affected, 'PEAR_Error'))
 
 132     // Handle expense items.
 
 133     if ($delete_client_entries) {
 
 134       $sql = 'update tt_expense_items set status = NULL'.$modified_part." where client_id = $id";
 
 135       $affected = $mdb2->exec($sql);
 
 136       if (is_a($affected, 'PEAR_Error'))
 
 141     if ($delete_client_entries) {
 
 142       $sql = "update tt_invoices set status = NULL where client_id = $id and group_id = ".$user->getGroup();
 
 143       $affected = $mdb2->exec($sql);
 
 144       if (is_a($affected, 'PEAR_Error'))
 
 148     // Delete project binds to this client.
 
 149     $sql = "delete from tt_client_project_binds where client_id = $id";
 
 150     $affected = $mdb2->exec($sql);
 
 151     if (is_a($affected, 'PEAR_Error'))
 
 154     // Handle users for client.
 
 155     $sql = 'update tt_users set status = NULL'.$modified_part." where client_id = $id and group_id = ".$user->getGroup();
 
 156     $affected = $mdb2->exec($sql);
 
 157     if (is_a($affected, 'PEAR_Error'))
 
 160     // Mark client deleted.
 
 161     $sql = "update tt_clients set status = NULL where id = $id and group_id = ".$user->getGroup();
 
 162     $affected = $mdb2->exec($sql);
 
 163     return (!is_a($affected, 'PEAR_Error'));
 
 166   // The insert function inserts a new client record into the clients table.
 
 167   static function insert($fields)
 
 170     $mdb2 = getConnection();
 
 172     $group_id = (int) $fields['group_id'];
 
 173     $org_id = (int) $fields['org_id'];
 
 174     $name = $fields['name'];
 
 175     $address = $fields['address'];
 
 176     $tax = $fields['tax'];
 
 177     $projects = $fields['projects'];
 
 179       $comma_separated = implode(',', $projects); // This is a comma-separated list of associated projects ids.
 
 180     $status = $fields['status'];
 
 182     $tax = str_replace(',', '.', $tax);
 
 183     if ($tax == '') $tax = 0;
 
 185     $sql = "insert into tt_clients (group_id, org_id, name, address, tax, projects, status)".
 
 186       " values ($group_id, $org_id, ".$mdb2->quote($name).", ".$mdb2->quote($address).", $tax, ".$mdb2->quote($comma_separated).", ".$mdb2->quote($status).")";
 
 188     $affected = $mdb2->exec($sql);
 
 189     if (is_a($affected, 'PEAR_Error'))
 
 193     $sql = "select last_insert_id() as last_insert_id";
 
 194     $res = $mdb2->query($sql);
 
 195     $val = $res->fetchRow();
 
 196     $last_id = $val['last_insert_id'];
 
 198     if (count($projects) > 0)
 
 199       foreach ($projects as $p_id) {
 
 200         $sql = "insert into tt_client_project_binds (client_id, project_id, group_id, org_id) values($last_id, $p_id, $group_id, $org_id)";
 
 201         $affected = $mdb2->exec($sql);
 
 202         if (is_a($affected, 'PEAR_Error'))
 
 209   // The update function updates a client record in tt_clients table.  
 
 210   static function update($fields)
 
 212     $mdb2 = getConnection();
 
 216     $name = $fields['name'];
 
 217     $address = $fields['address'];
 
 218     $tax = $fields['tax'];
 
 219     $status = $fields['status'];
 
 220     $projects = $fields['projects'];
 
 222     $tax = str_replace(',', '.', $tax);
 
 223     if ($tax == '') $tax = 0;
 
 225     // Insert client to project binds into tt_client_project_binds table.
 
 226     $sql = "delete from tt_client_project_binds where client_id = $id";
 
 227     $affected = $mdb2->exec($sql);
 
 228     if (is_a($affected, 'PEAR_Error'))
 
 229       die($affected->getMessage());
 
 230     if (count($projects) > 0)
 
 231       foreach ($projects as $p_id) {
 
 232         $sql = "insert into tt_client_project_binds (client_id, project_id) values($id, $p_id)";
 
 233         $affected = $mdb2->exec($sql);
 
 234         if (is_a($affected, 'PEAR_Error'))
 
 238     // Update client properties in tt_clients table.
 
 239     $comma_separated = implode(",", $projects); // This is a comma-separated list of associated project ids.
 
 240     $sql = "update tt_clients set name = ".$mdb2->quote($name).", address = ".$mdb2->quote($address).
 
 241       ", tax = $tax, projects = ".$mdb2->quote($comma_separated).", status = $status".
 
 242       " where group_id = ".$user->getGroup()." and id = ".$id;
 
 243     $affected = $mdb2->exec($sql);
 
 244     return (!is_a($affected, 'PEAR_Error'));
 
 247   // The setMappedClient function is used during group import to change client_id value for tt_users to a mapped value.
 
 248   static function setMappedClient($group_id, $imported_id, $mapped_id)
 
 250     $mdb2 = getConnection();
 
 251     $sql = "update tt_users set client_id = $mapped_id where client_id = $imported_id and group_id = $group_id ";
 
 252     $affected = $mdb2->exec($sql);
 
 253     if (is_a($affected, 'PEAR_Error'))
 
 259   // The fillBean function fills the ActionForm object with client data.
 
 260   static function fillBean($client_id, &$bean) {
 
 261     $client = ttClientHelper::getClient($client_id, true);
 
 262     $bean->setAttribute('name', $client['name']);
 
 263     $bean->setAttribute('address', $client['address']);
 
 264     $bean->setAttribute('tax', $client['tax']);
 
 267   // getAssignedProjects - returns an array of projects associatied with a client.
 
 268   static function getAssignedProjects($client_id)
 
 273     $mdb2 = getConnection();
 
 275     // Do a query with inner join to get assigned projects.
 
 276     $sql = "select p.id, p.name from tt_projects p".
 
 277       " inner join tt_client_project_binds cpb on (cpb.client_id = $client_id and cpb.project_id = p.id)".
 
 278       " where p.group_id = ".$user->getGroup()." and p.status = 1 order by p.name";
 
 279     $res = $mdb2->query($sql);
 
 280     if (!is_a($res, 'PEAR_Error')) {
 
 281       while ($val = $res->fetchRow()) {
 
 288   // getClientsForUser - returns an array of clients that are relevant to a user via assigned projects. 
 
 289   static function getClientsForUser()
 
 292     $user_id = $user->getUser();
 
 295     $mdb2 = getConnection();
 
 297     $sql = "select distinct c.id, c.name, c.projects from tt_user_project_binds upb
 
 298       inner join tt_client_project_binds cpb on (cpb.project_id = upb.project_id)
 
 299       inner join tt_clients c on (c.id = cpb.client_id and c.status = 1)
 
 300       where upb.user_id = $user_id and upb.status = 1 order by upper(c.name)";
 
 302     $res = $mdb2->query($sql);
 
 303     if (!is_a($res, 'PEAR_Error')) {
 
 304       while ($val = $res->fetchRow()) {