ea-Geiercode
[ class tree: ea-Geier ] [ index: ea-Geier ] [ all elements ]

Source for file travel.db.php

Documentation is available at travel.db.php

  1. <?php 
  2. /**
  3.  * database functions for travelling expenses
  4.  *
  5.  * LICENSE:
  6.  * This program is free software; you can redistribute it and/or modify
  7.  * it under the terms of the GNU General Public License as published by
  8.  * the Free Software Foundation; either version 2 of the License, or
  9.  * (at your option) any later version.
  10.  * This program is distributed in the hope that it will be useful,
  11.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.     See the
  13.  * GNU General Public License for more details.
  14.  * You should have received a copy of the GNU General Public License along
  15.  * with this program; if not, write to the Free Software Foundation, Inc.,
  16.  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  17.  *
  18.  * @package       ea-Geier
  19.  * @subpackage code
  20.  * @author       m2mtech <tech@m2m.at>
  21.  * @copyright  2007 m2m server software gmbh
  22.  * @license       http://www.gnu.org/licenses/gpl.html GNU General Public License Version 2
  23.  * @version       $Id: travel.db.php 134 2007-08-24 17:30:36Z m2mtech $
  24.  * @link       http://www.ea-geier.at/
  25.  */
  26.  
  27. /**
  28.  * standard client db functions
  29.  */
  30. require_once('code/clients.db.php');
  31.  
  32. /**
  33.  * number functions
  34.  */
  35. require_once('code/numbers.inc.php');
  36.  
  37. /**
  38.  * date functions
  39.  */
  40. require_once('code/date.inc.php');
  41.  
  42. /**
  43.  * save travel item
  44.  *
  45.  * @param    eaDB    database
  46.  * @param    integer client
  47.  * @param    array    data
  48.  */
  49. function dbSaveTravelItem(&$db, $client, &$data) { 
  50.     dbChangeToClientDB($db, $client);
  51.  
  52.     // prepare number format
  53.     $opt['decimal'] = $db->conf['numNumberDecimal'];
  54.  
  55.     // prepare variables
  56.     $vars = array('txtName', 'txtPeriod', 'txtVouchers', 'numDay', 'numNight', 'txtTax', 'numTax', 'numAmount', 'numDistance', 'numDistRate', 'numDistAmount', 'year', 'month', 'day');
  57.     foreach ($vars as $var) if (isset($data[$var])) switch ($var) {
  58.         case 'numDay': case 'numNight': case 'numTax': case 'numAmount':  
  59.         case 'numDistAmount':
  60.             $what[$var] = makeDot($data[$var], $opt['decimal']);
  61.             break;
  62.         case 'numDistance': case 'numDistRate':
  63.             $what[$var] = makeDotOnly($data[$var], $opt['decimal']);
  64.             break;
  65.         default: $what[$var] = $data[$var];
  66.     }
  67.     
  68.     $what['lastModified'] = $db->now();
  69.     
  70.     $what['who'] = $db->user['mail'];
  71.  
  72.     $table = $db->table('travels');
  73.  
  74.     if (isset($data['numID'])) { // old item
  75.         $id = $data['numID'];
  76.         if (!$db->update($what, $table, array('id' => $id))) return false;
  77.     } else {
  78.         if (!$db->insert($what, $table)) return false;
  79.         if (!$id = $db->lastID()) return false;
  80.     }
  81.  
  82.     return $data['numID'] = $id;
  83. }
  84.  
  85. /**
  86.  * load travel item
  87.  *
  88.  * @param    eaDB    database
  89.  * @param    integer clientID
  90.  * @param    integer itemID
  91.  * @return    array    data
  92.  */
  93. function dbLoadTravelItem(&$db, $client, $item) {
  94.     dbChangeToClientDB($db, $client);
  95.  
  96.     $table = $db->table('travels');
  97.  
  98.     if (!$data = $db->selectOne('*', $table, array('id' => $item))) return false;
  99.  
  100.     dbPrepareTravelSQLData($db->conf, $data);
  101.     
  102.     return $data;
  103. }
  104.  
  105. /**
  106.  * get number of travel items
  107.  *
  108.  * @param    eaDB    database
  109.  * @param    integer clientID
  110.  * @param    array    filter
  111.  * @return    integer number of items
  112.  */
  113. function dbCountTravelItems(&$db, $client, $where = false) { 
  114.     dbChangeToClientDB($db, $client);
  115.  
  116.     $table = $db->table('travels');
  117.  
  118.     $what = 'count(id) as c, sum(numAmount) as numAmount, sum(numTax) as numTax, sum(numDistAmount) as numDistAmount';
  119.  
  120.     if (!$where) $where = '';
  121.     if (isset($where['txtName'])) { // we have to prepare it ourselves 
  122.         $filters = $where;
  123.         $where = "txtName like '%" . $db->escape($where['txtName'], true) . "%'";    
  124.         unset($filters['txtName']);
  125.         if ($filters) { // other filters available
  126.             foreach ($filters as $key => $value)
  127.                 $where .= ' and ' . $db->escapeKey($key) . ' = ' . $db->escape($value);
  128.         }        
  129.     }
  130.  
  131.     if (!$dummy = $db->selectOne($what, $table, $where)) return false;
  132.     
  133.     if (!$dummy['c']) return false;
  134.     
  135.     $data['count'] = $dummy['c'];
  136.     $data['numAmount'] = $dummy['numAmount'];
  137.     $data['numTax'] = $dummy['numTax'];
  138.     $data['numDistAmount'] = $dummy['numDistAmount'];
  139.  
  140.     dbPrepareTravelSQLData($db->conf, $data);
  141.     
  142.     return $data;
  143.  
  144.  
  145. }
  146.  
  147. /**
  148.  * load multiple travel items
  149.  *
  150.  * @param    eaDB    database
  151.  * @param    integer clientID
  152.  * @param    integer number of items
  153.  * @param    integer number page
  154.  * @param    string    sorting string
  155.  * @param    array    filtering
  156.  * @return    array    data
  157.  */
  158. function dbLoadTravelItems(&$db, $client, $num = 10, $page = 1, $sort = false, $where = false) { 
  159.     dbChangeToClientDB($db, $client);
  160.  
  161.     $table = $db->table('travels');
  162.  
  163.     if (!$where) $where = '';
  164.     if (isset($where['txtName'])) { // we have to prepare it ourselves 
  165.         $filters = $where;
  166.         $where = "txtName like '%" . $db->escape($where['txtName'], true) . "%'";    
  167.         unset($filters['txtName']);
  168.         if ($filters) { // other filters available
  169.             foreach ($filters as $key => $value)
  170.                 $where .= ' and ' . $db->escapeKey($key) . ' = ' . $db->escape($value);
  171.         }        
  172.     }
  173.     
  174.     if (!$sort) $sort = 'id asc';
  175.  
  176.     if ($page == -1) $offset = -1;
  177.     else $offset = $num * ($page - 1);
  178.  
  179.     if (!$data = $db->select('*', $table, $where, $sort, $num, $offset))
  180.         return false;
  181.         
  182.     foreach ($data as $key => $val) dbPrepareTravelSQLData($db->conf, $data[$key]);    
  183.     return $data;
  184. }
  185.  
  186. /**
  187.  * prepare the sql data for output
  188.  *
  189.  * @param    array    configuration data
  190.  * @param    array    travel items
  191.  */
  192. function dbPrepareTravelSQLData(&$conf, &$data) {
  193.     static $decimal = false;
  194.     static $separator = false;
  195.     static $date = false;
  196.  
  197.     if (!$decimal) $decimal = $conf['numNumberDecimal'];
  198.     if (!$separator) switch ($conf['txtNumberSeperator']) {
  199.         case 'komma':
  200.             $separator = ',';
  201.             break;
  202.         case 'dot': default:
  203.             $separator = '.';
  204.             break;
  205.     }
  206.     if (!$date) $date = $conf['txtDateFormat'];
  207.     
  208.     // prepare numbers
  209.  
  210.     
  211.     $numVars = array('numDay', 'numNight', 'numTax', 'numAmount', 'numDistAmount');
  212.     foreach ($numVars as $var) if (isset($data[$var]))
  213.         $data[$var] = makeNum($data[$var], $separator, $decimal);
  214.  
  215.     $numVars = array('numDistance', 'numDistRate');
  216.     foreach ($numVars as $var) if (isset($data[$var]))
  217.         $data[$var] = makeNumOnly($data[$var], $separator);
  218.  
  219.     // prepare date
  220.         $data['txtDate'] = dateFormat($data, $date);    
  221. }
  222.  
  223. /**
  224.  * get months in database
  225.  *
  226.  * @param    eaDB    database
  227.  * @param    integer clientID
  228.  * @return    array    templates
  229.  */
  230. function dbGetTravelMonths(&$db, $client) { 
  231.     dbChangeToClientDB($db, $client);
  232.  
  233.     $table = $db->table('travels');
  234.  
  235.     if (!$dummy = $db->select('distinct month, year', $table, '', 'year desc, month desc'))
  236.         return false;
  237.     
  238.     foreach ($dummy as $val) {
  239.         $comb = $val['year'] . '-' . $val['month'];
  240.         switch ($db->conf['txtDateFormat']) {
  241.             case 'DD.MM.YYYY':
  242.                 $months[$comb] = $val['month'] . '.' . $val['year'];
  243.                 break;
  244.             case 'YYYY-MM-DD':
  245.                 $months[$comb] = $comb;
  246.                 break;
  247.         }
  248.     }
  249.  
  250.     return $months;
  251. }
  252.  
  253. ?>

Documentation generated on Sun, 09 Sep 2007 17:09:37 +0200 by phpDocumentor 1.3.1