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

Source for file cashbook.inc.php

Documentation is available at cashbook.inc.php

  1. <?php 
  2. /**
  3.  * php 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.inc.php 130 2007-08-17 14:24:44Z m2mtech $
  24.  * @link       http://www.ea-geier.at/
  25.  */
  26.  
  27. /**
  28.  * html functions for cashbook
  29.  */
  30. require_once('code/cashbook.html.php');
  31.  
  32. /**
  33.  * database functions for cashbook
  34.  */
  35. require_once('code/cashbook.db.php');
  36.  
  37. /**
  38.  * number functions
  39.  */
  40. require_once('code/numbers.inc.php');
  41.  
  42. /**
  43.  * date functions
  44.  */
  45. require_once('code/date.inc.php');
  46.  
  47.  
  48. /**
  49.  * generate data for html output of cash-book
  50.  *
  51.  * @param    eaDB    database
  52.  * @param    eaInput input data
  53.  */
  54. function genOptCashBook(&$db, &$in) { 
  55.     $conf =& $db->conf;
  56.     $user =& $db->user;
  57.     if (!isset($in->options)) $in->options = array();
  58.     $opt =& $in->options;
  59.  
  60.     if (!$clientID = $in->getClient($db)) return false; // wrong client
  61.     
  62.     // sorting
  63.     $sort = $in->getSort(array('id', 'txtID', 'txtName', 'month', 'numAmount', 'numAmountDist', 'numAmountTax'), 'cashbook');
  64.     
  65.     if (strpos($sort, 'month') !== false) {
  66.         if (strpos($sort, 'asc')) $sort = 'year asc, month asc, day asc';
  67.         else $sort = 'year desc, month desc, day desc';
  68.     }
  69.     
  70.     // pagenumber
  71.     if (!$opt['page'] = $in->getPage('cashbook')) $opt['page'] = 1;
  72.     
  73.     // lines per page
  74.     $opt['lines'] = $conf['numLinesPerPage'];
  75.     
  76.  
  77.     // if javascript is disabled get filter variables from post
  78.     if (isset($_POST) && !empty($_POST)) {
  79.         $vars = array('p:txt:txt');
  80.         $in->check($vars);
  81.     }            
  82.  
  83.     $where = array();
  84.     // filter months
  85.     $month = $in->getGet('date', 'txt');
  86.     if (!$month) $month = $in->post('txtMonth');
  87.     if (!$month && isset($_SESSION['filter'][$clientID]['cashbookmonth'])) 
  88.         $month = $_SESSION['filter'][$clientID]['cashbookmonth'];
  89.     if ($month) {
  90.         if ($dummy = splitMonthYear($month)) {
  91.             $where += $dummy;
  92.             $_SESSION['filter'][$clientID]['cashbookmonth'] = $month;
  93.             $in->post['txtMonth'] = $month;
  94.         } elseif (isset($_SESSION['filter'][$clientID]['cashbookmonth'])) 
  95.             unset($_SESSION['filter'][$clientID]['cashbookmonth']);
  96.     }
  97.     
  98.     // filter cash / bank
  99.     $cashbank = $in->getGet('cashbank', 'txt');
  100.     if (!$cashbank) $cashbank = $in->post('txtCashBank');
  101.     if (!$cashbank && isset($_SESSION['filter'][$clientID]['cashbookcashbank'])) 
  102.         $cashbank = $_SESSION['filter'][$clientID]['cashbookcashbank'];
  103.     if ($cashbank) {
  104.         if ($cashbank != 'none') {
  105.             $where['txtCashBank'] = $cashbank;
  106.             $_SESSION['filter'][$clientID]['cashbookcashbank'] = $cashbank;
  107.             $in->post['txtCashBank'] = $cashbank;
  108.         } elseif (isset($_SESSION['filter'][$clientID]['cashbookcashbank'])) 
  109.             unset($_SESSION['filter'][$clientID]['cashbookcashbank']);
  110.     }
  111.     
  112.     // filter distributors
  113.     $distr = $in->getGet('distr', 'txt');
  114.     if (!$distr) $distr = $in->post('txtDistributor');
  115.     if (!$distr && isset($_SESSION['filter'][$clientID]['cashbookdistr'])) 
  116.         $distr = $_SESSION['filter'][$clientID]['cashbookdistr'];
  117.     if ($distr) switch ($distr) {
  118.         case 'none':
  119.             if (isset($_SESSION['filter'][$clientID]['cashbookdistr']))
  120.                 unset($_SESSION['filter'][$clientID]['cashbookdistr']);
  121.             break;
  122.         case 'in': case 'out':
  123.             $where['abcInOut'] = $distr;
  124.             $in->post['txtDistributor'] = $distr;
  125.             $_SESSION['filter'][$clientID]['cashbookdistr'] = $distr;
  126.             break;
  127.         default:
  128.             $where['txtDistributor'] = $distr;
  129.             $in->post['txtDistributor'] = $distr;
  130.             $_SESSION['filter'][$clientID]['cashbookdistr'] = $distr;            
  131.     }
  132.  
  133.     // filter tax
  134.     $tax = $in->getGet('tax', 'txt');
  135.     if (!$tax && ($tax !== '0')) $tax = $in->post('txtTax');
  136.     if (!$tax && ($tax !== '0') && isset($_SESSION['filter'][$clientID]['cashbooktax'])) 
  137.         $tax = $_SESSION['filter'][$clientID]['cashbooktax'];
  138.     if ($tax || ($tax === '0')) {
  139.         if ($tax != 'none') {
  140.             $where['txtTax'] = $tax;
  141.             $_SESSION['filter'][$clientID]['cashbooktax'] = $tax;
  142.             $in->post['txtTax'] = $tax;
  143.         } elseif (isset($_SESSION['filter'][$clientID]['cashbooktax'])) 
  144.             unset($_SESSION['filter'][$clientID]['cashbooktax']);
  145.     }
  146.     
  147.     // filters name
  148.     $name = $in->getGet('find', 'txt');
  149.     if (!$name) $name = $in->post('txtWhat');
  150.     if (isset($in->error['txtWhat']) && ($in->error['txtWhat'] == 'empty')) {
  151.         $name = '=';
  152.         unset ($in->error['txtWhat']);
  153.     }
  154.     if (!$name && isset($_SESSION['filter'][$clientID]['cashbookname'])) 
  155.         $name = $_SESSION['filter'][$clientID]['cashbookname'];
  156.     if ($name && ($name != '=')) {
  157.         if (strpos($name, '=') !== false) $name = trim(substr($name, 1));
  158.         $where['txtName'] = $name;
  159.         $_SESSION['filter'][$clientID]['cashbookname'] = $name;
  160.         $in->post['txtWhat'] = $name;
  161.     } elseif (isset($_SESSION['filter'][$clientID]['cashbookname']))
  162.         unset($_SESSION['filter'][$clientID]['cashbookname']);
  163.  
  164.     // generate options
  165.  
  166.     $opt['cashbook'] = true; // we are in the cash book section
  167.     
  168.     // generate options for heading
  169.     
  170.     $opt['months'] = dbGetMonths($db, $clientID);
  171.  
  172.     // prepare cash/bank accounts & categories
  173.     $vars = array('txtCashBankAccounts' => 'cashbankaccounts', 'txtInCategories' => 'incategories', 'txtOutCategories' => 'outcategories');
  174.     foreach ($vars as $key => $val) {
  175.         $split = explode(',', $conf[$key]);
  176.         foreach ($split as $item) {
  177.             $item = trim($item);
  178.             $opt[$val][$item] = $item; 
  179.         }
  180.     }
  181.     
  182.     // prepare vat values
  183.     $split = explode(',', $conf['txtVatCategories']);
  184.     foreach ($split as $item) {
  185.         $item = trim($item);
  186.         $opt['vatcategories'][$item] = $item . ' %'; 
  187.     }
  188.  
  189.     // load items
  190.     if (!$opt['itemstotal'] = dbCountCashBookItems($db, $clientID, $where)) {
  191.         if ($where) {
  192.             $opt['countitems'] = 0;
  193.             return true;
  194.         }
  195.         return false;
  196.     } else $opt['countitems'] = $opt['itemstotal']['count'];
  197.  
  198.     $opt['pages'] = ceil($opt['countitems'] / $opt['lines']);
  199.     
  200.     if ($opt['page'] > $opt['pages']) $opt['page'] = 1;
  201.  
  202.     if (!$opt['items'] = dbLoadCashBookItems($db, $clientID, $opt['lines'], $opt['page'], $sort, $where)) 
  203.         return false;    
  204.         
  205.     return true;    
  206. }
  207.  
  208. /**
  209.  * generate data for print output of cash-book
  210.  *
  211.  * @param    eaDB    database
  212.  * @param    eaInput input data
  213.  */
  214. function genOptCashBookPrint(&$db, &$in) { 
  215.     $conf =& $db->conf;
  216.     $user =& $db->user;
  217.     if (!isset($in->options)) $in->options = array();
  218.     $opt =& $in->options;
  219.  
  220.     if (!$clientID = $in->getClient($db)) return false; // wrong client
  221.     
  222.     // sorting
  223.     $sort = $in->getSort(array('id', 'txtID', 'txtName', 'month', 'numAmount', 'numAmountDist', 'numAmountTax'), 'cashbook');
  224.     
  225.     if (strpos($sort, 'month') !== false) {
  226.         if (strpos($sort, 'asc')) $sort = 'year asc, month asc, day asc';
  227.         else $sort = 'year desc, month desc, day desc';
  228.     }
  229.     
  230.     $where = array();
  231.     // filter months
  232.     if (isset($_SESSION['filter'][$clientID]['cashbookmonth'])) {
  233.         $month = $_SESSION['filter'][$clientID]['cashbookmonth'];
  234.         if ($dummy = splitMonthYear($month)) $where += $dummy;
  235.     }
  236.     
  237.     // filter cash / bank
  238.     if (isset($_SESSION['filter'][$clientID]['cashbookcashbank'])) 
  239.         $where['txtCashBank'] = $_SESSION['filter'][$clientID]['cashbookcashbank'];
  240.     
  241.     // filter distributors
  242.     if (isset($_SESSION['filter'][$clientID]['cashbookdistr'])) {
  243.         $distr = $_SESSION['filter'][$clientID]['cashbookdistr'];
  244.         switch ($distr) {
  245.             case 'none': break;
  246.             case 'in': case 'out':
  247.                 $where['abcInOut'] = $distr;
  248.                 break;
  249.             default:
  250.                 $where['txtDistributor'] = $distr;
  251.         }
  252.     }
  253.  
  254.     // filter tax
  255.     if (isset($_SESSION['filter'][$clientID]['cashbooktax'])) {
  256.         $tax = $_SESSION['filter'][$clientID]['cashbooktax'];
  257.         if ($tax || ($tax === '0')) 
  258.             if ($tax != 'none') $where['txtTax'] = $tax;
  259.     }
  260.     
  261.     // filters name
  262.     if (isset($_SESSION['filter'][$clientID]['cashbookname'])) {
  263.         $name = $_SESSION['filter'][$clientID]['cashbookname'];
  264.         if ($name && ($name != '=')) {
  265.             if (strpos($name, '=') !== false) $name = trim(substr($name, 1));
  266.             $where['txtName'] = $name;
  267.         }
  268.     }
  269.  
  270.     $opt['months'] = dbGetMonths($db, $clientID);
  271.  
  272.     // show only one month?
  273.     if (isset($where['month'])) $opt['months'] = array($month => true);
  274.  
  275.     $opt['countitems']['total'] = 0;
  276.     $vars = array('numAmount', 'numAmountDist', 'numAmountTax');
  277.     foreach ($vars as $var) $opt['itemstotal']['total'][$var] = 0;        
  278.     foreach ($opt['months'] as $month => $monthNotUsed) {
  279.         if ($dummy = splitMonthYear($month)) $where = $dummy + $where;
  280.         $opt['where'][$month] = $where;
  281.  
  282.         // load items
  283.         if ($opt['itemstotal'][$month] = dbCountCashBookItems($db, $clientID, $where)) {
  284.             $opt['countitems'][$month] = $opt['itemstotal'][$month]['count'];
  285.             $opt['countitems']['total'] += $opt['countitems'][$month];
  286.             foreach ($vars as $var)
  287.                 $opt['itemstotal']['total'][$var] +=
  288.                     makeDot($opt['itemstotal'][$month][$var]);
  289.         } else continue;
  290.         
  291.         if (!$opt['items'][$month] = dbLoadCashBookItems($db, $clientID, -1, -1, $sort, $where)) 
  292.         continue;        
  293.     }
  294.     
  295.     dbPrepareSQLData($conf, $opt['itemstotal']['total'], true);    
  296.     
  297.     return true;    
  298. }
  299.  
  300. /**
  301.  * generate data for html output of cash-book item
  302.  *
  303.  * @param    eaDB    database
  304.  * @param    eaInput input data
  305.  */
  306. function genOptCashBookItem(&$db, &$in) { 
  307.     $conf =& $db->conf;
  308.     $user =& $db->user;
  309.     if (!isset($in->options)) $in->options = array();
  310.     $opt =& $in->options;
  311.  
  312.     if (!$clientID = $in->getClient($db)) return false; // wrong client
  313.     
  314.     // generate options
  315.  
  316.     $opt['cashbook'] = true; // we are in the cash book section
  317.     
  318.     // prepare date
  319.     $opt['dateformat'] = $conf['txtDateFormat'];
  320.  
  321.     // prepare cash/bank accounts & categories
  322.     $vars = array('txtCashBankAccounts' => 'cashbankaccounts', 'txtInCategories' => 'incategories', 'txtOutCategories' => 'outcategories');
  323.     foreach ($vars as $key => $val) {
  324.         $split = explode(',', $conf[$key]);
  325.         foreach ($split as $item) {
  326.             $item = trim($item);
  327.             $opt[$val][$item] = $item; 
  328.         }
  329.     }
  330.     
  331.     // prepare vat values
  332.     $split = explode(',', $conf['txtVatCategories']);
  333.     foreach ($split as $item) {
  334.         $item = trim($item);
  335.         $opt['vatcategories'][$item] = $item . ' %'; 
  336.     }
  337.     
  338.     // prepare number format
  339.     $opt['decimal'] = $conf['numNumberDecimal'];
  340.     switch ($conf['txtNumberSeperator']) {
  341.         case 'komma':
  342.             $opt['seperator'] = ',';
  343.             break;
  344.         case 'dot': default:
  345.             $opt['seperator'] = '.';
  346.             break;
  347.     }
  348.     
  349.     // template
  350.     $opt['numSaveTemplate'][1] = true;
  351.     if ($opt['templates'] = dbGetTemplates($db, $clientID)) {            
  352.         if (isset($_POST['buttonCashBookTemplate'])) {
  353.             $vars = array('p:num:num');
  354.             $in->check($vars);
  355.             $itemID = $in->post('numTemplate');
  356.             $in->error = array();
  357.         } else {
  358.             $itemID = $in->getGet('template');
  359.         }
  360.         if ($itemID && ($in->post = dbLoadCashBookItem($db, $clientID, $itemID))) {
  361.             $post =& $in->post;
  362.  
  363.             // if a category is missing ... add it
  364.             if (!isset($opt['cashbankaccounts'][$post['txtCashBank']]))
  365.                 $opt['cashbankaccounts'][$post['txtCashBank']] = $post['txtCashBank'];
  366.         
  367.             if ($post['abcInOut'] == 'in') {
  368.                 if (!isset($opt['incategories'][$post['txtDistributor']]))
  369.                     $opt['incategories'][$post['txtDistributor']];
  370.             } else {
  371.                 if (!isset($opt['outcategories'][$post['txtDistributor']]))
  372.                     $opt['outcategories'][$post['txtDistributor']];
  373.             }
  374.             
  375.             $post['numTemplate'] = $itemID;
  376.         }            
  377.     }
  378.  
  379.     // load old data
  380.     if (($itemID = $in->getGet('item')) 
  381.         && ($in->post = dbLoadCashBookItem($db, $clientID, $itemID))) {
  382.         $post =& $in->post;
  383.  
  384.         $post['numID'] = $post['id'];
  385.         if ($post['template'] == 1) $post['numSaveTemplate'] = array(1);
  386.  
  387.         // if a category is missing ... add it
  388.         if (!isset($opt['cashbankaccounts'][$post['txtCashBank']]))
  389.             $opt['cashbankaccounts'][$post['txtCashBank']] = $post['txtCashBank'];
  390.         
  391.         if ($post['abcInOut'] == 'in') {
  392.             if (!isset($opt['incategories'][$post['txtDistributor']]))
  393.                 $opt['incategories'][$post['txtDistributor']];
  394.         } else {
  395.             if (!isset($opt['outcategories'][$post['txtDistributor']]))
  396.                 $opt['outcategories'][$post['txtDistributor']];
  397.         }
  398.  
  399.         // actually we do not need the templates
  400.         $opt['templates'] = false;        
  401.     }
  402.     
  403.     return true;    
  404. }
  405.  
  406. /**
  407.  * save cash book item
  408.  *
  409.  * @param    eaDB    database
  410.  * @param    eaInput input data
  411.  */
  412. function saveCashBookItem(&$db, &$in) { 
  413.     if (!isset($_POST['buttonSaveCashBookItem'])) return false;
  414.     $in->post['buttonSaveCashBookItem'] = true;
  415.  
  416.     $conf =& $db->conf;
  417.     $user =& $db->user;
  418.  
  419.     $clientID = $in->options['client'];
  420.     if (!$db->isOwner($clientID)) return false; // wrong client
  421.     
  422.     // check for errors
  423.     $vars = array('p:txt:txt', 'p:num:num', 'p:abcLower:abc');
  424.     $in->check($vars);
  425.  
  426.     $error =& $in->error;
  427.     $post =& $in->post;
  428.     $opt =& $in->options;
  429.  
  430.     // check amounts
  431.     $vars = array('g' => 'numAmount', 'n' => 'numAmountDist', 't' => 'numAmountTax');
  432.     foreach ($vars as $key => $var)
  433.         $$key = $in->post($var);
  434.     $p = $in->post('txtTax');
  435.     if ($p !== false) {
  436.         if ($in->_check->num($p)) $p /= 100;
  437.         else $p = -1;
  438.     } else $p = -1;
  439.     if ($g !== false) { //g
  440.         $g = makeDot($g, $opt['decimal']);
  441.         if ($n !== false) { //gn
  442.             $n = makeDot($n, $opt['decimal']);
  443.             if ($t !== false) //gnt
  444.                 $t = makeDot($t, $opt['decimal']);
  445.             else { //gnT
  446.                 $t = $g - $n;
  447.                 unset($error['numAmountTax']);
  448.             }
  449.         } elseif ($t !== false) { // gNt
  450.             $t = makeDot($t, $opt['decimal']);
  451.             $n = $g - $t;
  452.             unset($error['numAmountDist']);
  453.         } elseif ($p > -1) { // gNT
  454.             $n = makeDot($g / (1 + $p), $opt['decimal']);
  455.             $t = $g - $n;            
  456.             unset($error['numAmountDist']);
  457.             unset($error['numAmountTax']);
  458.         }
  459.     } elseif ($n !== false) { // Gn
  460.         $n = makeDot($n, $opt['decimal']);
  461.         if ($t !== false) { // Gnt
  462.             $t = makeDot($post['numAmountTax'], $opt['decimal']);
  463.             $g = $n + $t;
  464.             unset($error['numAmount']);
  465.         } elseif ($p > -1) { // GnT
  466.             $t = makeDot($n * $p, $opt['decimal']);
  467.             $g = $n + $t;            
  468.             unset($error['numAmount']);
  469.             unset($error['numAmountTax']);
  470.         }        
  471.     } elseif ($t !== false) { // GNt
  472.         if ($p > 0) {
  473.             $n = makeDot($t / $p, $opt['decimal']);
  474.             $g = $n + $t;            
  475.             unset($error['numAmount']);
  476.             unset($error['numAmountDist']);
  477.         }        
  478.     }
  479.     foreach ($vars as $key => $var) if ($$key !== false)
  480.         $post[$var] = makeNum($$key, $opt['seperator'], $opt['decimal']);
  481.  
  482.     if ($error) return false; // wrong input
  483.     
  484.     // g = t + n    
  485.     if (log10(abs($g - $t - $n)) >= -$opt['decimal'] - 0.1) 
  486.         foreach ($vars as $var) $error[$var] = 'sumwrong';
  487.  
  488.     // n * p = t    
  489.     if (($p > -1) && (log10(abs($n * $p - $t)) >= -$opt['decimal'])) 
  490.         $error['txtTax'] = 'percentagewrong';
  491.  
  492.     // txtDate -> check date
  493.     if ($dummy = checkDateFormat($post['txtDate'], $conf['txtDateFormat'])) {
  494.         $post += $dummy;
  495.     } else {
  496.         $error['txtDate'] = 'wrongformat';
  497.     }
  498.     
  499.     // txtInOut -> check valid selection
  500.     switch ($post['abcInOut']) {
  501.         case 'in':
  502.             if (!isset($opt['incategories'][$post['txtDistributor']]))
  503.                 $error['txtDistributor'] = 'wrongcategory';
  504.             break;
  505.         case 'out':
  506.             if (!isset($opt['outcategories'][$post['txtDistributor']]))
  507.                 $error['txtDistributor'] = 'wrongcategory';
  508.             break;
  509.         default:
  510.             $error['abcInOut'] = true;
  511.     }
  512.     
  513.     // check valid selection
  514.     $vars = array('txtCashBank' => 'cashbankaccounts', 'txtTax' => 'vatcategories');
  515.     foreach ($vars as $key => $val) 
  516.         if (!isset($opt[$val][$post[$key]])) $error[$key] = 'wrongselection';
  517.  
  518.     // txtID -> check if id exists already    
  519.  
  520.     if ($error) return false; // still wrong input
  521.  
  522.     // actually we do not need the templates anymore
  523.     $opt['templates'] = false;        
  524.  
  525.     // save item    
  526.     return dbSaveCashBookItem($db, $clientID, $post);
  527. }
  528.  
  529. ?>

Documentation generated on Sun, 09 Sep 2007 17:08:04 +0200 by phpDocumentor 1.3.1