Source for file cashbook.inc.php
Documentation is available at cashbook.inc.php
* php functions for cash book
* 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: cashbook.inc.php 130 2007-08-17 14:24:44Z m2mtech $
* @link http://www.ea-geier.at/
* html functions for cashbook
require_once('code/cashbook.html.php');
* database functions for cashbook
require_once('code/cashbook.db.php');
require_once('code/numbers.inc.php');
require_once('code/date.inc.php');
* generate data for html output of cash-book
* @param eaInput input data
if (!isset ($in->options)) $in->options = array();
if (!$clientID = $in->getClient($db)) return false; // wrong client
$sort = $in->getSort(array('id', 'txtID', 'txtName', 'month', 'numAmount', 'numAmountDist', 'numAmountTax'), 'cashbook');
if (strpos($sort, 'month') !== false) {
if (strpos($sort, 'asc')) $sort = 'year asc, month asc, day asc';
else $sort = 'year desc, month desc, day desc';
if (!$opt['page'] = $in->getPage('cashbook')) $opt['page'] = 1;
$opt['lines'] = $conf['numLinesPerPage'];
// if javascript is disabled get filter variables from post
if (isset ($_POST) && !empty($_POST)) {
$vars = array('p:txt:txt');
$month = $in->getGet('date', 'txt');
if (!$month) $month = $in->post('txtMonth');
if (!$month && isset ($_SESSION['filter'][$clientID]['cashbookmonth']))
$month = $_SESSION['filter'][$clientID]['cashbookmonth'];
$_SESSION['filter'][$clientID]['cashbookmonth'] = $month;
$in->post['txtMonth'] = $month;
} elseif (isset ($_SESSION['filter'][$clientID]['cashbookmonth']))
unset ($_SESSION['filter'][$clientID]['cashbookmonth']);
$cashbank = $in->getGet('cashbank', 'txt');
if (!$cashbank) $cashbank = $in->post('txtCashBank');
if (!$cashbank && isset ($_SESSION['filter'][$clientID]['cashbookcashbank']))
$cashbank = $_SESSION['filter'][$clientID]['cashbookcashbank'];
if ($cashbank != 'none') {
$where['txtCashBank'] = $cashbank;
$_SESSION['filter'][$clientID]['cashbookcashbank'] = $cashbank;
$in->post['txtCashBank'] = $cashbank;
} elseif (isset ($_SESSION['filter'][$clientID]['cashbookcashbank']))
unset ($_SESSION['filter'][$clientID]['cashbookcashbank']);
$distr = $in->getGet('distr', 'txt');
if (!$distr) $distr = $in->post('txtDistributor');
if (!$distr && isset ($_SESSION['filter'][$clientID]['cashbookdistr']))
$distr = $_SESSION['filter'][$clientID]['cashbookdistr'];
if ($distr) switch ($distr) {
if (isset ($_SESSION['filter'][$clientID]['cashbookdistr']))
unset ($_SESSION['filter'][$clientID]['cashbookdistr']);
$where['abcInOut'] = $distr;
$in->post['txtDistributor'] = $distr;
$_SESSION['filter'][$clientID]['cashbookdistr'] = $distr;
$where['txtDistributor'] = $distr;
$in->post['txtDistributor'] = $distr;
$_SESSION['filter'][$clientID]['cashbookdistr'] = $distr;
$tax = $in->getGet('tax', 'txt');
if (!$tax && ($tax !== '0')) $tax = $in->post('txtTax');
if (!$tax && ($tax !== '0') && isset ($_SESSION['filter'][$clientID]['cashbooktax']))
$tax = $_SESSION['filter'][$clientID]['cashbooktax'];
if ($tax || ($tax === '0')) {
$_SESSION['filter'][$clientID]['cashbooktax'] = $tax;
$in->post['txtTax'] = $tax;
} elseif (isset ($_SESSION['filter'][$clientID]['cashbooktax']))
unset ($_SESSION['filter'][$clientID]['cashbooktax']);
$name = $in->getGet('find', 'txt');
if (!$name) $name = $in->post('txtWhat');
if (isset ($in->error['txtWhat']) && ($in->error['txtWhat'] == 'empty')) {
unset ($in->error['txtWhat']);
if (!$name && isset ($_SESSION['filter'][$clientID]['cashbookname']))
$name = $_SESSION['filter'][$clientID]['cashbookname'];
if ($name && ($name != '=')) {
$where['txtName'] = $name;
$_SESSION['filter'][$clientID]['cashbookname'] = $name;
$in->post['txtWhat'] = $name;
} elseif (isset ($_SESSION['filter'][$clientID]['cashbookname']))
unset ($_SESSION['filter'][$clientID]['cashbookname']);
$opt['cashbook'] = true; // we are in the cash book section
// generate options for heading
// prepare cash/bank accounts & categories
$vars = array('txtCashBankAccounts' => 'cashbankaccounts', 'txtInCategories' => 'incategories', 'txtOutCategories' => 'outcategories');
foreach ($vars as $key => $val) {
$split = explode(',', $conf[$key]);
foreach ($split as $item) {
$opt[$val][$item] = $item;
$split = explode(',', $conf['txtVatCategories']);
foreach ($split as $item) {
$opt['vatcategories'][$item] = $item . ' %';
} else $opt['countitems'] = $opt['itemstotal']['count'];
$opt['pages'] = ceil($opt['countitems'] / $opt['lines']);
if ($opt['page'] > $opt['pages']) $opt['page'] = 1;
if (!$opt['items'] = dbLoadCashBookItems($db, $clientID, $opt['lines'], $opt['page'], $sort, $where))
* generate data for print output of cash-book
* @param eaInput input data
if (!isset ($in->options)) $in->options = array();
if (!$clientID = $in->getClient($db)) return false; // wrong client
$sort = $in->getSort(array('id', 'txtID', 'txtName', 'month', 'numAmount', 'numAmountDist', 'numAmountTax'), 'cashbook');
if (strpos($sort, 'month') !== false) {
if (strpos($sort, 'asc')) $sort = 'year asc, month asc, day asc';
else $sort = 'year desc, month desc, day desc';
if (isset ($_SESSION['filter'][$clientID]['cashbookmonth'])) {
$month = $_SESSION['filter'][$clientID]['cashbookmonth'];
if (isset ($_SESSION['filter'][$clientID]['cashbookcashbank']))
$where['txtCashBank'] = $_SESSION['filter'][$clientID]['cashbookcashbank'];
if (isset ($_SESSION['filter'][$clientID]['cashbookdistr'])) {
$distr = $_SESSION['filter'][$clientID]['cashbookdistr'];
$where['abcInOut'] = $distr;
$where['txtDistributor'] = $distr;
if (isset ($_SESSION['filter'][$clientID]['cashbooktax'])) {
$tax = $_SESSION['filter'][$clientID]['cashbooktax'];
if ($tax || ($tax === '0'))
if ($tax != 'none') $where['txtTax'] = $tax;
if (isset ($_SESSION['filter'][$clientID]['cashbookname'])) {
$name = $_SESSION['filter'][$clientID]['cashbookname'];
if ($name && ($name != '=')) {
$where['txtName'] = $name;
if (isset ($where['month'])) $opt['months'] = array($month => true);
$opt['countitems']['total'] = 0;
$vars = array('numAmount', 'numAmountDist', 'numAmountTax');
foreach ($vars as $var) $opt['itemstotal']['total'][$var] = 0;
foreach ($opt['months'] as $month => $monthNotUsed) {
$opt['where'][$month] = $where;
$opt['countitems'][$month] = $opt['itemstotal'][$month]['count'];
$opt['countitems']['total'] += $opt['countitems'][$month];
$opt['itemstotal']['total'][$var] +=
makeDot($opt['itemstotal'][$month][$var]);
* generate data for html output of cash-book item
* @param eaInput input data
if (!isset ($in->options)) $in->options = array();
if (!$clientID = $in->getClient($db)) return false; // wrong client
$opt['cashbook'] = true; // we are in the cash book section
$opt['dateformat'] = $conf['txtDateFormat'];
// prepare cash/bank accounts & categories
$vars = array('txtCashBankAccounts' => 'cashbankaccounts', 'txtInCategories' => 'incategories', 'txtOutCategories' => 'outcategories');
foreach ($vars as $key => $val) {
$split = explode(',', $conf[$key]);
foreach ($split as $item) {
$opt[$val][$item] = $item;
$split = explode(',', $conf['txtVatCategories']);
foreach ($split as $item) {
$opt['vatcategories'][$item] = $item . ' %';
$opt['decimal'] = $conf['numNumberDecimal'];
switch ($conf['txtNumberSeperator']) {
$opt['numSaveTemplate'][1] = true;
if (isset ($_POST['buttonCashBookTemplate'])) {
$vars = array('p:num:num');
$itemID = $in->post('numTemplate');
$itemID = $in->getGet('template');
// if a category is missing ... add it
if (!isset ($opt['cashbankaccounts'][$post['txtCashBank']]))
$opt['cashbankaccounts'][$post['txtCashBank']] = $post['txtCashBank'];
if ($post['abcInOut'] == 'in') {
if (!isset ($opt['incategories'][$post['txtDistributor']]))
$opt['incategories'][$post['txtDistributor']];
if (!isset ($opt['outcategories'][$post['txtDistributor']]))
$opt['outcategories'][$post['txtDistributor']];
$post['numTemplate'] = $itemID;
if (($itemID = $in->getGet('item'))
$post['numID'] = $post['id'];
if ($post['template'] == 1) $post['numSaveTemplate'] = array(1);
// if a category is missing ... add it
if (!isset ($opt['cashbankaccounts'][$post['txtCashBank']]))
$opt['cashbankaccounts'][$post['txtCashBank']] = $post['txtCashBank'];
if ($post['abcInOut'] == 'in') {
if (!isset ($opt['incategories'][$post['txtDistributor']]))
$opt['incategories'][$post['txtDistributor']];
if (!isset ($opt['outcategories'][$post['txtDistributor']]))
$opt['outcategories'][$post['txtDistributor']];
// actually we do not need the templates
$opt['templates'] = false;
* @param eaInput input data
if (!isset ($_POST['buttonSaveCashBookItem'])) return false;
$in->post['buttonSaveCashBookItem'] = true;
$clientID = $in->options['client'];
if (!$db->isOwner($clientID)) return false; // wrong client
$vars = array('p:txt:txt', 'p:num:num', 'p:abcLower:abc');
$vars = array('g' => 'numAmount', 'n' => 'numAmountDist', 't' => 'numAmountTax');
foreach ($vars as $key => $var)
$p = $in->post('txtTax');
if ($in->_check->num($p)) $p /= 100;
unset ($error['numAmountTax']);
} elseif ($t !== false) { // gNt
unset ($error['numAmountDist']);
} elseif ($p > - 1) { // gNT
$n = makeDot($g / (1 + $p), $opt['decimal']);
unset ($error['numAmountDist']);
unset ($error['numAmountTax']);
} elseif ($n !== false) { // Gn
if ($t !== false) { // Gnt
$t = makeDot($post['numAmountTax'], $opt['decimal']);
unset ($error['numAmount']);
} elseif ($p > - 1) { // GnT
$t = makeDot($n * $p, $opt['decimal']);
unset ($error['numAmount']);
unset ($error['numAmountTax']);
} elseif ($t !== false) { // GNt
$n = makeDot($t / $p, $opt['decimal']);
unset ($error['numAmount']);
unset ($error['numAmountDist']);
foreach ($vars as $key => $var) if ($ $key !== false)
$post[$var] = makeNum($ $key, $opt['seperator'], $opt['decimal']);
if ($error) return false; // wrong input
if (log10(abs($g - $t - $n)) >= - $opt['decimal'] - 0.1)
foreach ($vars as $var) $error[$var] = 'sumwrong';
if (($p > - 1) && (log10(abs($n * $p - $t)) >= - $opt['decimal']))
$error['txtTax'] = 'percentagewrong';
$error['txtDate'] = 'wrongformat';
// txtInOut -> check valid selection
switch ($post['abcInOut']) {
if (!isset ($opt['incategories'][$post['txtDistributor']]))
$error['txtDistributor'] = 'wrongcategory';
if (!isset ($opt['outcategories'][$post['txtDistributor']]))
$error['txtDistributor'] = 'wrongcategory';
$error['abcInOut'] = true;
$vars = array('txtCashBank' => 'cashbankaccounts', 'txtTax' => 'vatcategories');
foreach ($vars as $key => $val)
if (!isset ($opt[$val][$post[$key]])) $error[$key] = 'wrongselection';
// txtID -> check if id exists already
if ($error) return false; // still wrong input
// actually we do not need the templates anymore
$opt['templates'] = false;
|