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

Source for file cashbook.db.php

Documentation is available at cashbook.db.php

  1. <?php 
  2. /**
  3.  * database functions for cash book
  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: cashbook.db.php 139 2007-08-29 20:49:58Z 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.  * save cash book item
  39.  *
  40.  * @param    eaDB    database
  41.  * @param    integer client
  42.  * @param    array    data
  43.  */
  44. function dbSaveCashBookItem(&$db, $client, &$data) { 
  45.     dbChangeToClientDB($db, $client);
  46.  
  47.     // prepare number format
  48.     $opt['decimal'] = $db->conf['numNumberDecimal'];
  49.  
  50.     // prepare varilabels
  51.     $vars = array('txtID', 'txtName', 'abcInOut', 'txtCashBank', 'numAmount', 'txtDistributor', 'numAmountDist', 'txtTax', 'numAmountTax', 'year', 'month', 'day');
  52.     foreach ($vars as $var) if (isset($data[$var])) switch ($var) {
  53.         case 'numAmount': case 'numAmountDist': case 'numAmountTax':
  54.             $what[$var] = makeDot($data[$var], $opt['decimal']);
  55.             break;
  56.         default: $what[$var] = $data[$var];
  57.     }
  58.  
  59.     if (isset($data['numSaveTemplate'])) $what['template'] = 1;
  60.     else $what['template'] = 0;
  61.     
  62.     $what['lastModified'] = $db->now();
  63.     
  64.     $what['who'] = $db->user['mail'];
  65.     
  66.     $table = $db->table('cashbook');
  67.  
  68.     if (isset($data['numID'])) { // old item
  69.         $id = $data['numID'];
  70.         if (!$db->update($what, $table, array('id' => $id))) return false;
  71.     } else {
  72.         if (!$db->insert($what, $table)) return false;
  73.         if (!$id = $db->lastID()) return false;
  74.     }
  75.  
  76.     return $data['numID'] = $id;
  77. }
  78.  
  79. /**
  80.  * load cash book item
  81.  *
  82.  * @param    eaDB    database
  83.  * @param    integer clientID
  84.  * @param    integer itemID
  85.  * @return    array    data
  86.  */
  87. function dbLoadCashBookItem(&$db, $client, $item) { 
  88.     dbChangeToClientDB($db, $client);
  89.  
  90.     $table = $db->table('cashbook');
  91.  
  92.     if (!$data = $db->selectOne('*', $table, array('id' => $item))) return false;
  93.  
  94.     dbPrepareSQLData($db->conf, $data);
  95.     
  96.     return $data;
  97. }
  98.  
  99. /**
  100.  * get number of cashbook items
  101.  *
  102.  * @param    eaDB    database
  103.  * @param    integer clientID
  104.  * @return    integer number of items
  105.  */
  106. function dbCountCashBookItems(&$db, $client, $where = false) { 
  107.     dbChangeToClientDB($db, $client);
  108.  
  109.     $table = $db->table('cashbook');
  110.  
  111.     $what = 'count(id) as c, sum(numAmount) as numAmount, sum(numAmountDist) as numAmountDist, sum(numAmountTax) as numAmountTax, abcInOut';
  112.  
  113.     if (!$where) $where = '';
  114.     if (isset($where['txtName'])) { // we have to prepare it ourselves 
  115.         $filters = $where;
  116.         $where = "txtName like '%" . $db->escape($where['txtName'], true) . "%'";    
  117.         unset($filters['txtName']);
  118.         if ($filters) { // other filters available
  119.             foreach ($filters as $key => $value)
  120.                 $where .= ' and ' . $db->escapeKey($key) . ' = ' . $db->escape($value);
  121.         }        
  122.     }
  123.  
  124.     if (!$dummy = $db->select($what, $table, $where, '', -1, -1, 'abcInOut'))
  125.         return false;
  126.  
  127.     $vars = array('numAmount', 'numAmountDist', 'numAmountTax');
  128.     foreach ($vars as $var) $data[$var] = 0;
  129.     $data['count'] = 0;
  130.     
  131.     foreach ($dummy as $item) {
  132.         switch ($item['abcInOut']) {
  133.             case 'in':
  134.                 foreach ($vars as $var) $data[$var] += $item[$var];
  135.                 break;
  136.             case 'out':
  137.                 foreach ($vars as $var) $data[$var] -= $item[$var];
  138.                 break;
  139.         }
  140.         $data['count'] += $item['c'];
  141.         $data[$item['abcInOut']] = $item;        
  142.     }
  143.  
  144.     dbPrepareSQLData($db->conf, $data, true);
  145.     if (isset($data['in'])) dbPrepareSQLData($db->conf, $data['in'], true);
  146.     if (isset($data['out'])) dbPrepareSQLData($db->conf, $data['out'], true);
  147.     
  148.     return $data;
  149. }
  150.  
  151. /**
  152.  * load multiple cash book item
  153.  *
  154.  * @param    eaDB    database
  155.  * @param    integer clientID
  156.  * @param    integer number of items
  157.  * @param    integer number page
  158.  * @param    string    sorting string
  159.  * @param    array    filtering
  160.  * @return    array    data
  161.  */
  162. function dbLoadCashBookItems(&$db, $client, $num = 10, $page = 1, $sort = false, $where = false) { 
  163.     dbChangeToClientDB($db, $client);
  164.  
  165.     $table = $db->table('cashbook');
  166.  
  167.     if (!$where) $where = '';
  168.     if (isset($where['txtName'])) { // we have to prepare it ourselves 
  169.         $filters = $where;
  170.         $where = "txtName like '%" . $db->escape($where['txtName'], true) . "%'";    
  171.         unset($filters['txtName']);
  172.         if ($filters) { // other filters available
  173.             foreach ($filters as $key => $value)
  174.                 $where .= ' and ' . $db->escapeKey($key) . ' = ' . $db->escape($value);
  175.         }        
  176.     }
  177.     
  178.     if (!$sort) $sort = 'id asc';
  179.  
  180.     if ($page == -1) $offset = -1;
  181.     else $offset = $num * ($page - 1);
  182.  
  183.     if (!$data = $db->select('*', $table, $where, $sort, $num, $offset))
  184.         return false;
  185.         
  186.     foreach ($data as $key => $val) dbPrepareSQLData($db->conf, $data[$key]);    
  187.     return $data;
  188. }
  189.  
  190. /**
  191.  * get templates
  192.  *
  193.  * @param    eaDB    database
  194.  * @param    integer clientID
  195.  * @return    array    templates
  196.  */
  197. function dbGetTemplates(&$db, $client) { 
  198.     dbChangeToClientDB($db, $client);
  199.  
  200.     $table = $db->table('cashbook');
  201.  
  202.     if (!$dummy = $db->select('id, txtName', $table, array('template' => 1))) return false;
  203.  
  204.     foreach($dummy as $val)
  205.         $templates[$val['id']] = $val['txtName'];
  206.     
  207.     return $templates;
  208. }
  209.  
  210. /**
  211.  * get months in database
  212.  *
  213.  * @param    eaDB    database
  214.  * @param    integer clientID
  215.  * @return    array    templates
  216.  */
  217. function dbGetMonths(&$db, $client) { 
  218.     dbChangeToClientDB($db, $client);
  219.  
  220.     $table = $db->table('cashbook');
  221.  
  222.     if (!$dummy = $db->select('distinct month, year', $table, '', 'year desc, month desc'))
  223.         return false;
  224.     
  225.     foreach ($dummy as $val) {
  226.         $comb = $val['year'] . '-' . $val['month'];
  227.         switch ($db->conf['txtDateFormat']) {
  228.             case 'DD.MM.YYYY':
  229.                 $months[$comb] = $val['month'] . '.' . $val['year'];
  230.                 break;
  231.             case 'YYYY-MM-DD':
  232.                 $months[$comb] = $comb;
  233.                 break;
  234.         }
  235.     }
  236.     
  237.     return $months;
  238. }
  239.  
  240. /**
  241.  * prepare the sql data for output
  242.  *
  243.  * @param    array    configuration data
  244.  * @param    array    cashbook item
  245.  */
  246. function dbPrepareSQLData(&$conf, &$data, $numOnly = false) {
  247.     static $decimal = false;
  248.     static $separator = false;
  249.     static $date = false;
  250.     
  251.     if (!$decimal) $decimal = $conf['numNumberDecimal'];
  252.     if (!$separator) switch ($conf['txtNumberSeperator']) {
  253.         case 'komma':
  254.             $separator = ',';
  255.             break;
  256.         case 'dot': default:
  257.             $separator = '.';
  258.             break;
  259.     }
  260.     if (!$date) $date = $conf['txtDateFormat'];
  261.     
  262.     $vars = array('numAmount', 'numAmountDist', 'numAmountTax');
  263.     foreach ($vars as $var) 
  264.         $data[$var] = makeNum($data[$var], $separator, $decimal);
  265.         
  266.     if ($numOnly) return true;
  267.         
  268.     $data['txtDate'] = dateFormat($data, $date);    
  269. }
  270.  
  271. ?>

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