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

Source for file balance.inc.php

Documentation is available at balance.inc.php

  1. <?php 
  2. /**
  3.  * php 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.inc.php 143 2007-09-03 22:30:04Z m2mtech $
  24.  * @link       http://www.ea-geier.at/
  25.  */
  26.  
  27. /**
  28.  * html functions for accounting balance
  29.  */
  30. require_once('code/balance.html.php');
  31.  
  32. /**
  33.  * database functions for accounting balance
  34.  */
  35. require_once('code/balance.db.php');
  36.  
  37. /**
  38.  * number functions
  39.  */
  40. require_once('code/numbers.inc.php');
  41.  
  42. /**
  43.  * generate data for html output of accounting balance
  44.  *
  45.  * @param    eaDB    database
  46.  * @param    eaInput input data
  47.  */
  48. function genOptBalance(&$db, &$in) {
  49.     $conf =& $db->conf;
  50.     $user =& $db->user;
  51.  
  52.     if (!isset($in->options)) $in->options = array();
  53.     $opt =& $in->options;
  54.  
  55.     if (!$clientID = $in->getClient($db)) return false; // wrong client
  56.     
  57.     $opt['balance'] = true; // we are in the balance section
  58.     $opt['balanceurl'] = 'balance';
  59.  
  60.     // prepare number format
  61.     $opt['decimal'] = $conf['numNumberDecimal'];
  62.     switch ($conf['txtNumberSeperator']) {
  63.         case 'komma':
  64.             $opt['seperator'] = ',';
  65.             break;
  66.         case 'dot': default:
  67.             $opt['seperator'] = '.';
  68.             break;
  69.     }
  70.     
  71.     $opt['years'] = dbGetBalanceYears($db, $clientID);
  72.     
  73.     
  74.     if (!$year = $in->getGet('year')) {
  75.         if (isset($_POST['numYear']) && !empty($_POST['numYear'])) {
  76.             $vars = array('p:num:numYear');
  77.             $in->check($vars);
  78.             $year = $in->post('numYear');
  79.         }
  80.         if (!$year) $year = date('Y');
  81.     }
  82.     
  83.     $opt['categories'] = dbGetCashBookCategories($db, $clientID, $year);
  84.     
  85.     $in->post['lines']['in'] = array('1' => true);
  86.     $in->post['lines']['out'] = array('1' => true);
  87.     
  88.     $in->post['sum']['in'] = 0;
  89.     $in->post['sum']['out'] = 0;
  90.             
  91.     $opt['travels'] = dbGetTravelCategories($db, $clientID, $year);
  92.  
  93.     $opt['categories']['total']['out'] += $opt['travels']['total'];
  94.     
  95.     $opt['depreciation'] = dbGetAssetCategories($db, $clientID, $year);
  96.  
  97.     $opt['categories']['total']['out'] += $opt['depreciation']['total'];
  98.         
  99.     // load old data
  100.     if (!isset($_POST['numYear']) 
  101.         && ($dummy = dbLoadBalance($db, $clientID, $year))) {
  102.         $in->post =& $dummy;
  103.         $post =& $in->post;
  104.         $post['numID'] = $post['id'];
  105.  
  106.         // prepare options derived from data 
  107.  
  108.     }
  109.  
  110.     $in->post['numYear'] = $year;    
  111.     $opt['total']['in'] = makeNum($opt['categories']['total']['in'] + $in->post['sum']['in'], $opt['seperator'], $opt['decimal']);
  112.     $opt['total']['out'] = makeNum($opt['categories']['total']['out'] + $in->post['sum']['out'], $opt['seperator'], $opt['decimal']);
  113.     $opt['total']['inout'] = makeNum($opt['categories']['total']['in'] + $in->post['sum']['in'] - $opt['categories']['total']['out'] + $in->post['sum']['out'], $opt['seperator'], $opt['decimal']);
  114.  
  115.     
  116.     return true;    
  117. }
  118.  
  119. /**
  120.  * save travel item
  121.  *
  122.  * @param    eaDB    database
  123.  * @param    eaInput input data
  124.  */
  125. function saveBalance(&$db, &$in) { 
  126.     if (!isset($_POST['buttonSaveBalance'])) return false;
  127.     $in->post['buttonSaveBalance'] = true;
  128.  
  129.     $conf =& $db->conf;
  130.     $user =& $db->user;
  131.  
  132.     $clientID = $in->options['client'];
  133.     if (!$db->isOwner($clientID)) return false; // wrong client
  134.  
  135.     // check for errors
  136.     $vars = array('p:txt:txt', 'p:num:num');
  137.     $in->check($vars);
  138.  
  139.     $error =& $in->error;
  140.     $post =& $in->post;
  141.     $opt =& $in->options;
  142.  
  143.     $numVars = array();
  144.     
  145.     $dirs = array('in' => 'In', 'out' => 'Out');    
  146.     foreach ($dirs as $dir => $dirVar) {
  147.         $sum[$dir] = 0;
  148.         $post['lines'][$dir] = array();
  149.         $line = 1;
  150.         for ($i = 1; $i < 100; $i++) { // artificial limit to 100 entries 
  151.             $txtLine = 'txt' . $dirVar . $line;
  152.             $numLine = 'num' . $dirVar . $line;
  153.             $txtCurrent = 'txt' . $dirVar . $i;
  154.             $numCurrent = 'num' . $dirVar . $i;
  155.             
  156.             if (!isset($post[$txtCurrent])) break; // no text variable left
  157.             if (!isset($post[$numCurrent])) break; // no number variable left
  158.     
  159.             $empty = true;
  160.             
  161.             if (!isset($error[$txtCurrent])) $empty = false;
  162.             elseif ($error[$txtCurrent] != 'empty') $empty = false; 
  163.  
  164.             if (isset($error[$numCurrent]) && ($error[$numCurrent] != 'empty')) 
  165.                 $empty = false; 
  166.     
  167.             $num = makeDot($post[$numCurrent], $opt['decimal']) * 1;
  168.  
  169.             if ($num != 0) $empty = false;
  170.             
  171.             if ($empty) {
  172.                 unset($post[$txtCurrent]);
  173.                 unset($post[$numCurrent]);
  174.                 if (isset($error[$txtCurrent])) unset($error[$txtCurrent]);
  175.                 if (isset($error[$numCurrent])) unset($error[$numCurrent]);
  176.                 continue;
  177.             }
  178.             
  179.             $sum[$dir] += $num;
  180.             
  181.             $post['lines'][$dir][$line] = true; 
  182.             
  183.             $numVar[] = $numLine;
  184.             
  185.             if ($line == $i) { $line++; continue; }
  186.             
  187.             // renumbering
  188.             $post[$txtLine] = $post[$txtCurrent]; 
  189.             unset($post[$txtCurrent]);
  190.             $post[$numLine] = $post[$numCurrent]; 
  191.             unset($post[$numCurrent]);
  192.             if (isset($error[$txtCurrent])) {
  193.                 $error[$txtLine] = $error[$txtCurrent]; 
  194.                 unset($error[$txtCurrent]);
  195.             }
  196.             if (isset($error[$numCurrent])) {
  197.                 $error[$numLine] = $error[$numCurrent]; 
  198.                 unset($error[$numCurrent]);
  199.             }
  200.             $line++;
  201.         }
  202.     }
  203.  
  204.     foreach ($numVars as $var) if (isset($post[$var]))
  205.         $post[$var] = makeNum($post[$var], $opt['seperator'], $opt['decimal']);
  206.  
  207.     // prepare options changed by saved data:
  208.     
  209.     $opt['total']['in'] = makeNum($sum['in'] + $opt['categories']['total']['in'], $opt['seperator'], $opt['decimal']);
  210.             
  211.     $opt['total']['out'] = makeNum($sum['out'] + $opt['categories']['total']['out'], $opt['seperator'], $opt['decimal']);
  212.         
  213.     $opt['total']['inout'] = makeNum($sum['in'] + $opt['categories']['total']['in'] - $sum['out'] - $opt['categories']['total']['out'], $opt['seperator'], $opt['decimal']);
  214.     
  215.     // and add a line
  216.     $post['lines']['in'][count($post['lines']['in']) + 1] = true;
  217.     $post['lines']['out'][count($post['lines']['out']) + 1] = true;
  218.  
  219.     if ($error) return false; // wrong input
  220.  
  221.     // save item    
  222.     return dbSaveBalance($db, $clientID, $post);
  223. }
  224.  
  225.  
  226. ?>

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