Started to replace canManageTeam calls with right checks.
[timetracker.git] / WEB-INF / lib / ttExpenseHelper.class.php
index b73208a..7d63ba4 100644 (file)
@@ -41,21 +41,23 @@ class ttExpenseHelper {
     $cost = str_replace(',', '.', $fields['cost']);
     $invoice_id = $fields['invoice_id'];
     $status = $fields['status'];
-       
-    $sql = "insert into tt_expense_items (date, user_id, client_id, project_id, name, cost, invoice_id, status) ".
+    $paid = (int) $fields['paid'];
+
+    $sql = "insert into tt_expense_items (date, user_id, client_id, project_id, name, cost, invoice_id, paid, status) ".
       "values (".$mdb2->quote($date).", $user_id, ".$mdb2->quote($client_id).", ".$mdb2->quote($project_id).
-      ", ".$mdb2->quote($name).", ".$mdb2->quote($cost).", ".$mdb2->quote($invoice_id).", ".$mdb2->quote($status).")";
+      ", ".$mdb2->quote($name).", ".$mdb2->quote($cost).", ".$mdb2->quote($invoice_id).", $paid, ".$mdb2->quote($status).")";
     $affected = $mdb2->exec($sql);
     if (is_a($affected, 'PEAR_Error'))
       return false;
-    
+
     $id = $mdb2->lastInsertID('tt_expense_items', 'id');
     return $id;
   }
-  
+
   // update - updates a record in tt_expense_items table.
   static function update($fields)
   {
+    global $user;
     $mdb2 = getConnection();
 
     $id = (int) $fields['id'];
@@ -66,19 +68,24 @@ class ttExpenseHelper {
     $name = $fields['name'];
     $cost = str_replace(',', '.', $fields['cost']);
     $invoice_id = $fields['invoice_id'];
-    
+
+    $paid_part = '';
+    if ($user->can('manage_invoices') && $user->isPluginEnabled('ps')) {
+      $paid_part = $fields['paid'] ? ', paid = 1' : ', paid = 0';
+    }
+
     $sql = "UPDATE tt_expense_items set date = ".$mdb2->quote($date).", user_id = $user_id, client_id = ".$mdb2->quote($client_id).
       ", project_id = ".$mdb2->quote($project_id).", name = ".$mdb2->quote($name).
-      ", cost = ".$mdb2->quote($cost).", invoice_id = ".$mdb2->quote($invoice_id).
+      ", cost = ".$mdb2->quote($cost)."$paid_part, invoice_id = ".$mdb2->quote($invoice_id).
       " WHERE id = $id";
-    
+
     $affected = $mdb2->exec($sql);
     if (is_a($affected, 'PEAR_Error'))
       return false;
-   
+
     return true;
   }
-  
+
   // markDeleted - marks an item as deleted in tt_expense_items table.
   static function markDeleted($id, $user_id) {
     $mdb2 = getConnection();
@@ -87,14 +94,14 @@ class ttExpenseHelper {
     $affected = $mdb2->exec($sql);
     if (is_a($affected, 'PEAR_Error'))
       return false;
-      
+
     return true;
   }
-  
+
   // getTotalForDay - gets total expenses for a user for a specific date.
   static function getTotalForDay($user_id, $date) {
-       global $user;
-       
+    global $user;
+
     $mdb2 = getConnection();
 
     $sql = "select sum(cost) as sm from tt_expense_items where user_id = $user_id and date = ".$mdb2->quote($date)." and status = 1";
@@ -106,23 +113,23 @@ class ttExpenseHelper {
     }
     return false;
   }
-  
+
   // getItem - retrieves an entry from tt_expense_items table.
   static function getItem($id, $user_id) {
-       global $user;
-       
+    global $user;
+
     $mdb2 = getConnection();
-    
+
     $client_field = null;
-    if (in_array('cl', explode(',', $user->plugins)))
+    if ($user->isPluginEnabled('cl'))
       $client_field = ", c.name as client_name";
-      
+
     $left_joins = "";
     $left_joins = " left join tt_projects p on (ei.project_id = p.id)";
-    if (in_array('cl', explode(',', $user->plugins)))
+    if ($user->isPluginEnabled('cl'))
       $left_joins .= " left join tt_clients c on (ei.client_id = c.id)";
-      
-    $sql = "select ei.id, ei.date, ei.client_id, ei.project_id, ei.name, ei.cost, ei.invoice_id $client_field, p.name as project_name
+
+    $sql = "select ei.id, ei.date, ei.client_id, ei.project_id, ei.name, ei.cost, ei.invoice_id, ei.paid $client_field, p.name as project_name
       from tt_expense_items ei
       $left_joins
       where ei.id = $id and ei.user_id = $user_id and ei.status = 1";
@@ -132,45 +139,27 @@ class ttExpenseHelper {
         return false;
       }
       if ($val = $res->fetchRow()) {
-       $val['cost'] = str_replace('.', $user->decimal_mark, $val['cost']);
+        $val['cost'] = str_replace('.', $user->decimal_mark, $val['cost']);
         return $val;
       }
     }
     return false;
   }
-  
-  /*
-  // getAllItems - returns all expense items for a certain user.
-  static function getAllItems($user_id) {
-    $result = array();
-
-    $mdb2 = getConnection();
-
-    $sql = "select * from tt_expense_items where user_id = $user_id order by id";
-    $res = $mdb2->query($sql);
-    if (!is_a($res, 'PEAR_Error')) {
-      while ($val = $res->fetchRow()) {
-        $result[] = $val;
-      }
-    } else return false;
-
-    return $result;
-  }*/
 
   // getItems - returns expense items for a user for a given date.
   static function getItems($user_id, $date) {
-       global $user;
-               
+    global $user;
+
     $result = array();
     $mdb2 = getConnection();
 
     $client_field = null;
-    if (in_array('cl', explode(',', $user->plugins)))
+    if ($user->isPluginEnabled('cl'))
       $client_field = ", c.name as client";
-    
+
     $left_joins = "";
     $left_joins = " left join tt_projects p on (ei.project_id = p.id)";
-    if (in_array('cl', explode(',', $user->plugins)))
+    if ($user->isPluginEnabled('cl'))
       $left_joins .= " left join tt_clients c on (ei.client_id = c.id)";
 
     $sql = "select ei.id as id $client_field, p.name as project, ei.name as item, ei.cost as cost,
@@ -178,7 +167,7 @@ class ttExpenseHelper {
       $left_joins
       where ei.date = ".$mdb2->quote($date)." and ei.user_id = $user_id and ei.status = 1
       order by ei.id";
-      
+
     $res = $mdb2->query($sql);
     if (!is_a($res, 'PEAR_Error')) {
       while ($val = $res->fetchRow()) {
@@ -190,4 +179,3 @@ class ttExpenseHelper {
     return $result;
   }
 }
-?>
\ No newline at end of file