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

Source for file balance.db.php

Documentation is available at balance.db.php

  1. <?php 
  2. /**
  3.  * database functions for accounting balance
  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: balance.db.php 143 2007-09-03 22:30:04Z 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 balance
  39.  *
  40.  * @param    eaDB    database
  41.  * @param    integer client
  42.  * @param    array    data
  43.  */
  44. function dbSaveBalance(&$db, $client, &$data) { 
  45.     dbChangeToClientDB($db, $client);
  46.  
  47.     // prepare number format
  48.     $opt['decimal'] = $db->conf['numNumberDecimal'];
  49.  
  50.     // prepare variables
  51.     $what['year'] = $data['numYear'];
  52.  
  53.     $lines = array();
  54.     $dirs = array('in' => 'In', 'out' => 'Out');    
  55.     foreach ($dirs as $dir => $var) {
  56.         if (isset($data['lines'][$dir])) foreach ($data['lines'][$dir] as $key => $dummy) {
  57.             if (!isset($data['txt' . $var . $key])) break;
  58.             $lines[$dir][$key] = array('t' => $data['txt' . $var . $key], 'n' => makeDot($data['num' . $var . $key], $opt['decimal']));
  59.         }
  60.     }
  61.     $what['data'] = serialize($lines);
  62.     
  63.     $what['lastModified'] = $db->now();
  64.     
  65.     $what['who'] = $db->user['mail'];
  66.  
  67.     $table = $db->table('balance');
  68.  
  69.     if (isset($data['numID'])) { // old item
  70.         $id = $data['numID'];
  71.         if (!$db->update($what, $table, array('id' => $id))) return false;
  72.     } else {
  73.         if (!$db->insert($what, $table)) return false;
  74.         if (!$id = $db->lastID()) return false;
  75.     }
  76.  
  77.     return $data['numID'] = $id;
  78. }
  79.  
  80. /**
  81.  * load balance
  82.  *
  83.  * @param    eaDB    database
  84.  * @param    integer clientID
  85.  * @param    integer year
  86.  * @return    array    data
  87.  */
  88. function dbLoadBalance(&$db, $client, $year) {
  89.     dbChangeToClientDB($db, $client);
  90.  
  91.     $table = $db->table('balance');
  92.  
  93.     if (!$data = $db->selectOne('*', $table, array('year' => $year))) return false;
  94.  
  95.     if (isset($data['data'])) {
  96.         $conf =& $db->conf;
  97.         $decimal = $conf['numNumberDecimal'];
  98.         switch ($conf['txtNumberSeperator']) {
  99.             case 'komma':
  100.                 $separator = ',';
  101.                 break;
  102.             case 'dot': default:
  103.                 $separator = '.';
  104.                 break;
  105.         }
  106.         $lines = unserialize($data['data']);
  107.  
  108.         $dirs = array('in' => 'In', 'out' => 'Out');    
  109.         foreach ($dirs as $dir => $var) {
  110.             $data['sum'][$dir] = 0;
  111.             if (!isset($lines[$dir])) continue;
  112.  
  113.             foreach ($lines[$dir] as $key => $line) {
  114.                 $data['sum'][$dir] += $line['n'];
  115.                 $data['lines'][$dir][$key] = true;
  116.                 $data['txt' . $var . $key] = $line['t'];
  117.                 $data['num' . $var . $key] = makeNum($line['n'], $separator, $decimal);
  118.             }
  119.         }
  120.     }
  121.     
  122.     if (!isset($data['lines']['in'])) $data['lines']['in'][1] = true;
  123.     else $data['lines']['in'][count($data['lines']['in']) + 1] = true;            
  124.     if (!isset($data['lines']['out'])) $data['lines']['out'][1] = true;
  125.     else $data['lines']['out'][count($data['lines']['out']) + 1] = true;            
  126.  
  127.     return $data;
  128. }
  129.  
  130. /**
  131.  * get categories & sums from cashbook
  132.  *
  133.  * @param    eaDB    database
  134.  * @param    integer clientID
  135.  * @param    integer year
  136.  * @return    array    categories
  137.  */
  138. function dbGetCashBookCategories(&$db, $client, $year) { 
  139.     dbChangeToClientDB($db, $client);
  140.  
  141.     $what = 'abcInOut, txtDistributor, sum(numAmountDist) as total';
  142.     $table = $db->table('cashbook');
  143.     $where = array('year' => $year);
  144.     $group = 'txtDistributor';
  145.  
  146.     $data['total'] = array('in' => 0, 'out' => 0);
  147.     
  148.     if (!$dummy = $db->select($what, $table, $where, '', -1, -1, $group)) 
  149.         return $data;
  150.  
  151.     $conf =& $db->conf;
  152.     
  153.     $decimal = $conf['numNumberDecimal'];
  154.     switch ($conf['txtNumberSeperator']) {
  155.         case 'komma':
  156.             $separator = ',';
  157.             break;
  158.         case 'dot': default:
  159.             $separator = '.';
  160.             break;
  161.     }
  162.         
  163.     $vars = array('in' => 'txtInCategories', 'out' => 'txtOutCategories');
  164.     foreach ($vars as $key => $var) {
  165.         if (!$cats = explode(',', $conf[$var])) continue;
  166.         foreach ($cats as $cat) $data[$key][$cat] = makeNum(0, $separator, $decimal);
  167.     }
  168.  
  169.     foreach ($dummy as $item) {
  170.         $data[$item['abcInOut']][$item['txtDistributor']] = makeNum($item['total'], $separator, $decimal);
  171.         $data['total'][$item['abcInOut']] += $item['total'];
  172.     }
  173.     
  174.     return $data;
  175. }
  176.  
  177. /**
  178.  * get categories & sums from travel expenses
  179.  *
  180.  * @param    eaDB    database
  181.  * @param    integer clientID
  182.  * @param    integer year
  183.  * @return    array    categories
  184.  */
  185. function dbGetTravelCategories(&$db, $client, $year) { 
  186.     dbChangeToClientDB($db, $client);
  187.  
  188.     $what = 'sum(numAmount) + sum(numDistAmount) as total';
  189.     $table = $db->table('travels');
  190.     $where = array('year' => $year);
  191.  
  192.     if (!$data = $db->selectOne($what, $table, $where)) return false;
  193.  
  194.     $conf =& $db->conf;
  195.     
  196.     $decimal = $conf['numNumberDecimal'];
  197.     switch ($conf['txtNumberSeperator']) {
  198.         case 'komma':
  199.             $separator = ',';
  200.             break;
  201.         case 'dot': default:
  202.             $separator = '.';
  203.             break;
  204.     }
  205.  
  206.     $data['costs'] = makeNum($data['total'], $separator, $decimal);
  207.  
  208.     return $data;
  209. }
  210.  
  211. /**
  212.  * get categories & sums from assets
  213.  *
  214.  * @param    eaDB    database
  215.  * @param    integer clientID
  216.  * @param    integer year
  217.  * @return    array    categories
  218.  */
  219. function dbGetAssetCategories(&$db, $client, $year) { 
  220.     dbChangeToClientDB($db, $client);
  221.  
  222.     $what = 'data';
  223.     $table = $db->table('assets');
  224.     $wYear = $db->escape($year);
  225.     $where = 'firstYear <= ' . $wYear . ' and lastYear >= ' . $wYear;
  226.     if (!$dummy = $db->select($what, $table, $where)) return false;
  227.  
  228.     $data['total'] = 0;
  229.     foreach ($dummy as $item) {
  230.         $depr = unserialize($item['data']);
  231.         foreach ($depr as $yearKey => $var) {
  232.             if ($year != $yearKey) continue;
  233.             $data['total'] += $var['d'];
  234.         }
  235.     }        
  236.  
  237.     $conf =& $db->conf;
  238.     
  239.     $decimal = $conf['numNumberDecimal'];
  240.     switch ($conf['txtNumberSeperator']) {
  241.         case 'komma':
  242.             $separator = ',';
  243.             break;
  244.         case 'dot': default:
  245.             $separator = '.';
  246.             break;
  247.     }
  248.  
  249.     $data['costs'] = makeNum($data['total'], $separator, $decimal);
  250.  
  251.     return $data;
  252. }
  253.  
  254. /**
  255.  * get years in database
  256.  *
  257.  * @param    eaDB    database
  258.  * @param    integer clientID
  259.  * @return    array    years
  260.  */
  261. function dbGetBalanceYears(&$db, $client) { 
  262.     dbChangeToClientDB($db, $client);
  263.  
  264.     $years = array();
  265.     $tables = array('cashbook', 'travels', 'assets');
  266.     foreach ($tables as $table) switch ($table) {
  267.         case 'assets': 
  268.             if (!$dummy = $db->select('distinct firstYear, lastYear', $db->table($table)))
  269.                 break;
  270.             foreach ($dummy as $item) 
  271.                 for ($i = $item['firstYear']; $i <= $item['lastYear']; $i++)
  272.                     $years[$i] = $i;
  273.             break;
  274.         default:
  275.             if (!$dummy = $db->select('distinct year', $db->table($table))) break;
  276.             foreach ($dummy as $item) $years[$item['year']] = $item['year'];
  277.     }
  278.     
  279.     if (!$years) return array(date('Y') => date('Y'));
  280.     
  281.     krsort($years);    
  282.  
  283.     return $years;
  284. }
  285.  
  286.  
  287. ?>

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