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 // +----------------------------------------------------------------------+
 
  30 // Class ttPredefinedExpenseHelper is used to help with predefined expense related tasks.
 
  31 class ttPredefinedExpenseHelper {
 
  33   // get - gets predefined expense details.
 
  34   static function get($id)
 
  37     $replaceDecimalMark = ('.' != $user->decimal_mark);
 
  39     $mdb2 = getConnection();
 
  41     $sql = "select id, name, cost from tt_predefined_expenses
 
  42       where id = $id and group_id = ".$user->getActiveGroup();
 
  43     $res = $mdb2->query($sql);
 
  44     if (!is_a($res, 'PEAR_Error')) {
 
  45       $val = $res->fetchRow();
 
  46       if ($val && $val['id']) {
 
  47         if ($replaceDecimalMark)
 
  48           $val['cost'] = str_replace('.', $user->decimal_mark, $val['cost']);
 
  55   // delete - deletes a predefined expense from tt_predefined_expenses table.
 
  56   static function delete($id) {
 
  59     $mdb2 = getConnection();
 
  61     $sql = "delete from tt_predefined_expenses where id = $id and group_id = ".$user->getActiveGroup();
 
  62     $affected = $mdb2->exec($sql);
 
  63     if (is_a($affected, 'PEAR_Error'))
 
  69   // insert function inserts a new predefined expense into database.
 
  70   static function insert($fields)
 
  74     $mdb2 = getConnection();
 
  76     $group_id = $user->getActiveGroup();
 
  77     $org_id = $user->org_id;
 
  78     $name = $fields['name'];
 
  79     $cost = $fields['cost'];
 
  80     if ('.' != $user->decimal_mark)
 
  81       $cost = str_replace($user->decimal_mark, '.', $cost);
 
  83     $sql = "insert into tt_predefined_expenses (group_id, org_id, name, cost)".
 
  84       " values ($group_id, $org_id, ".$mdb2->quote($name).", ".$mdb2->quote($cost).")";
 
  85     $affected = $mdb2->exec($sql);
 
  86     if (is_a($affected, 'PEAR_Error'))
 
  92   // update function - updates a predefined expense in database.
 
  93   static function update($fields)
 
  97     $mdb2 = getConnection();
 
  99     $group_id = $user->getActiveGroup();
 
 100     $org_id = $user->org_id;
 
 101     $predefined_expense_id = (int) $fields['id'];
 
 102     $name = $fields['name'];
 
 103     $cost = $fields['cost'];
 
 104     if ('.' != $user->decimal_mark)
 
 105       $cost = str_replace($user->decimal_mark, '.', $cost);
 
 107     $sql = "update tt_predefined_expenses set name = ".$mdb2->quote($name).", cost = ".$mdb2->quote($cost).
 
 108       " where id = $predefined_expense_id and group_id = $group_id and org_id = $org_id";
 
 109     $affected = $mdb2->exec($sql);
 
 110     return (!is_a($affected, 'PEAR_Error'));