Source for file balance.db.php
Documentation is available at balance.db.php
* database functions for accounting balance
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
* @author m2mtech <tech@m2m.at>
* @copyright 2007 m2m server software gmbh
* @license http://www.gnu.org/licenses/gpl.html GNU General Public License Version 2
* @version $Id: balance.db.php 143 2007-09-03 22:30:04Z m2mtech $
* @link http://www.ea-geier.at/
* standard client db functions
require_once('code/clients.db.php');
require_once('code/numbers.inc.php');
$opt['decimal'] = $db->conf['numNumberDecimal'];
$what['year'] = $data['numYear'];
$dirs = array('in' => 'In', 'out' => 'Out');
foreach ($dirs as $dir => $var) {
if (isset ($data['lines'][$dir])) foreach ($data['lines'][$dir] as $key => $dummy) {
if (!isset ($data['txt' . $var . $key])) break;
$lines[$dir][$key] = array('t' => $data['txt' . $var . $key], 'n' => makeDot($data['num' . $var . $key], $opt['decimal']));
$what['lastModified'] = $db->now();
$what['who'] = $db->user['mail'];
$table = $db->table('balance');
if (isset ($data['numID'])) { // old item
if (!$db->update($what, $table, array('id' => $id))) return false;
if (!$db->insert($what, $table)) return false;
if (!$id = $db->lastID()) return false;
return $data['numID'] = $id;
* @param integer clientID
$table = $db->table('balance');
if (!$data = $db->selectOne('*', $table, array('year' => $year))) return false;
if (isset ($data['data'])) {
$decimal = $conf['numNumberDecimal'];
switch ($conf['txtNumberSeperator']) {
$dirs = array('in' => 'In', 'out' => 'Out');
foreach ($dirs as $dir => $var) {
if (!isset ($lines[$dir])) continue;
foreach ($lines[$dir] as $key => $line) {
$data['sum'][$dir] += $line['n'];
$data['lines'][$dir][$key] = true;
$data['txt' . $var . $key] = $line['t'];
$data['num' . $var . $key] = makeNum($line['n'], $separator, $decimal);
if (!isset ($data['lines']['in'])) $data['lines']['in'][1] = true;
else $data['lines']['in'][count($data['lines']['in']) + 1] = true;
if (!isset ($data['lines']['out'])) $data['lines']['out'][1] = true;
else $data['lines']['out'][count($data['lines']['out']) + 1] = true;
* get categories & sums from cashbook
* @param integer clientID
* @return array categories
$what = 'abcInOut, txtDistributor, sum(numAmountDist) as total';
$table = $db->table('cashbook');
$where = array('year' => $year);
$group = 'txtDistributor';
$data['total'] = array('in' => 0, 'out' => 0);
if (!$dummy = $db->select($what, $table, $where, '', - 1, - 1, $group))
$decimal = $conf['numNumberDecimal'];
switch ($conf['txtNumberSeperator']) {
$vars = array('in' => 'txtInCategories', 'out' => 'txtOutCategories');
foreach ($vars as $key => $var) {
if (!$cats = explode(',', $conf[$var])) continue;
foreach ($cats as $cat) $data[$key][$cat] = makeNum(0, $separator, $decimal);
foreach ($dummy as $item) {
$data[$item['abcInOut']][$item['txtDistributor']] = makeNum($item['total'], $separator, $decimal);
$data['total'][$item['abcInOut']] += $item['total'];
* get categories & sums from travel expenses
* @param integer clientID
* @return array categories
$what = 'sum(numAmount) + sum(numDistAmount) as total';
$table = $db->table('travels');
$where = array('year' => $year);
if (!$data = $db->selectOne($what, $table, $where)) return false;
$decimal = $conf['numNumberDecimal'];
switch ($conf['txtNumberSeperator']) {
$data['costs'] = makeNum($data['total'], $separator, $decimal);
* get categories & sums from assets
* @param integer clientID
* @return array categories
$table = $db->table('assets');
$wYear = $db->escape($year);
$where = 'firstYear <= ' . $wYear . ' and lastYear >= ' . $wYear;
if (!$dummy = $db->select($what, $table, $where)) return false;
foreach ($dummy as $item) {
foreach ($depr as $yearKey => $var) {
if ($year != $yearKey) continue;
$data['total'] += $var['d'];
$decimal = $conf['numNumberDecimal'];
switch ($conf['txtNumberSeperator']) {
$data['costs'] = makeNum($data['total'], $separator, $decimal);
* @param integer clientID
$tables = array('cashbook', 'travels', 'assets');
foreach ($tables as $table) switch ($table) {
if (!$dummy = $db->select('distinct firstYear, lastYear', $db->table($table)))
foreach ($dummy as $item)
for ($i = $item['firstYear']; $i <= $item['lastYear']; $i++ )
if (!$dummy = $db->select('distinct year', $db->table($table))) break;
foreach ($dummy as $item) $years[$item['year']] = $item['year'];
if (!$years) return array(date('Y') => date('Y'));
|