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

Source for file assets.inc.php

Documentation is available at assets.inc.php

  1. <?php 
  2. /**
  3.  * php functions for assets 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: assets.inc.php 130 2007-08-17 14:24:44Z m2mtech $
  24.  * @link       http://www.ea-geier.at/
  25.  */
  26.  
  27. /**
  28.  * html functions for assets book
  29.  */
  30. require_once('code/assets.html.php');
  31.  
  32. /**
  33.  * database functions for assets book
  34.  */
  35. require_once('code/assets.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.  * generate data for html output of assets book
  49.  *
  50.  * @param    eaDB    database
  51.  * @param    eaInput input data
  52.  */
  53. function genOptAssets(&$db, &$in) { 
  54.     $conf =& $db->conf;
  55.     $user =& $db->user;
  56.  
  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.     $opt['assets'] = true; // we are in the assets book section
  63.     $opt['assetsurl'] = 'assets';
  64.  
  65.     // prepare number format
  66.     $opt['decimal'] = $conf['numNumberDecimal'];
  67.     switch ($conf['txtNumberSeperator']) {
  68.         case 'komma':
  69.             $opt['seperator'] = ',';
  70.             break;
  71.         case 'dot': default:
  72.             $opt['seperator'] = '.';
  73.             break;
  74.     }
  75.     
  76.     // sorting
  77.     $sort = $in->getSort(array('id', 'txtName', 'purchaseDate', 'numAmount'), 'assets');
  78.  
  79.     // pagenumber
  80.     if (!$opt['page'] = $in->getPage('assets')) $opt['page'] = 1;
  81.     
  82.     // lines per page
  83.     $opt['lines'] = $conf['numLinesPerPage'];
  84.  
  85.     // if javascript is disabled get filter variables from post
  86.     if (isset($_POST) && !empty($_POST)) {
  87.         $vars = array('p:txt:txt');
  88.         $in->check($vars);
  89.     }            
  90.  
  91.     $where = array();
  92.     // filters name
  93.     $name = $in->getGet('find', 'txt');
  94.     if (!$name) $name = $in->post('txtWhat');
  95.     if (isset($in->error['txtWhat']) && ($in->error['txtWhat'] == 'empty')) {
  96.         $name = '=';
  97.         unset ($in->error['txtWhat']);
  98.     }
  99.     if (!$name && isset($_SESSION['filter'][$clientID]['assetsname'])) 
  100.         $name = $_SESSION['filter'][$clientID]['assetsname'];
  101.     if ($name && ($name != '=')) {
  102.         if (strpos($name, '=') !== false) $name = trim(substr($name, 1));
  103.         $where['txtName'] = $name;
  104.         $_SESSION['filter'][$clientID]['assetsname'] = $name;
  105.         $in->post['txtWhat'] = $name;
  106.     } elseif (isset($_SESSION['filter'][$clientID]['assetsname']))
  107.         unset($_SESSION['filter'][$clientID]['assetsname']);
  108.  
  109.     // get year to show values for for depreciation and value
  110.     $year = $in->getGet('depreciation', 'num'); // from depr select
  111.     if (!$year) $year = $in->getGet('value', 'num'); // from value select
  112.     if (!$year) $year = $in->post('numDepreciation'); // from depr post
  113.     if (!$year) $year = $in->post('numValue'); // from value post
  114.     if ($year) $_SESSION['show'][$clientID]['assetsyear'] = $year; // store in session
  115.     if (!$year && isset($_SESSION['show'][$clientID]['assetsyear'])) 
  116.         $year = $_SESSION['show'][$clientID]['assetsyear']; // from session
  117.     if (!$year) $year = date('Y'); // this year
  118.  
  119.     // generate options for heading
  120.     $opt['year'] = dbGetAssetsYears($db, $clientID, $where);
  121.     if (!isset($opt['year'][$year])) $year = key($opt['year']);
  122.  
  123.     // load items
  124.     if (!$opt['itemstotal'] = dbCountAssetsItems($db, $clientID, $where, $year)) {
  125.         if ($where) {
  126.             $opt['countitems'] = 0;
  127.             return true;
  128.         }
  129.         return false;
  130.     } else $opt['countitems'] = $opt['itemstotal']['count'];
  131.  
  132.     $opt['pages'] = ceil($opt['countitems'] / $opt['lines']);
  133.     
  134.     if ($opt['page'] > $opt['pages']) $opt['page'] = 1;
  135.  
  136.     if (!$opt['items'] = dbLoadAssetsItems($db, $clientID, $opt['lines'], $opt['page'], $sort, $where))
  137.         return false;
  138.     
  139.     $in->post['numDepreciation'] = $year;
  140.     $in->post['numValue'] = $year;
  141.  
  142.     foreach ($opt['items'] as $key => $item) {
  143.         $firstYear = reset($item['lines']);
  144.         $lastYear = end($item['lines']);
  145.         $nullValue = makeNum(0, $opt['seperator'], $opt['decimal']);
  146.         if ($firstYear > $year) {
  147.             $opt['items'][$key]['numDepreciation'] = $nullValue;
  148.             $opt['items'][$key]['numValue'] = $nullValue;
  149.             continue;
  150.         }
  151.         if ($lastYear < $year) {
  152.             $opt['items'][$key]['numDepreciation'] = $nullValue;
  153.             $opt['items'][$key]['numValue'] = $item['numValue' . key($item['lines'])];
  154.             continue;
  155.         }
  156.         $years = array_flip($item['lines']);
  157.         $opt['items'][$key]['numDepreciation'] = $item['numDepreciation' . $years[$year]];
  158.         $opt['items'][$key]['numValue'] = $item['numValue' . $years[$year]];
  159.     };
  160.         
  161.     return true;    
  162. }
  163.  
  164. function genOptAssetsPrint(&$db, &$in) { 
  165.     $conf =& $db->conf;
  166.     $user =& $db->user;
  167.  
  168.     if (!isset($in->options)) $in->options = array();
  169.     $opt =& $in->options;
  170.  
  171.     if (!$clientID = $in->getClient($db)) return false; // wrong client
  172.     
  173.     // prepare number format
  174.     $opt['decimal'] = $conf['numNumberDecimal'];
  175.     switch ($conf['txtNumberSeperator']) {
  176.         case 'komma':
  177.             $opt['seperator'] = ',';
  178.             break;
  179.         case 'dot': default:
  180.             $opt['seperator'] = '.';
  181.             break;
  182.     }
  183.     
  184.     // sorting
  185.     $sort = $in->getSort(array('id', 'txtName', 'purchaseDate', 'numAmount'), 'assets');
  186.  
  187.     $where = array();
  188.     // filters name
  189.     // filters name
  190.     if (isset($_SESSION['filter'][$clientID]['assetsname'])) {
  191.         $name = $_SESSION['filter'][$clientID]['assetsname'];
  192.         if ($name && ($name != '=')) {
  193.             if (strpos($name, '=') !== false) $name = trim(substr($name, 1));
  194.             $where['txtName'] = $name;
  195.         }
  196.     }
  197.  
  198.     // get year to show values for for depreciation and value
  199.     $year = date('Y'); // this year
  200.     if (isset($_SESSION['show'][$clientID]['assetsyear'])) 
  201.         $year = $_SESSION['show'][$clientID]['assetsyear']; // from session
  202.     
  203.     $opt['year'] = dbGetAssetsYears($db, $clientID, $where);
  204.     
  205.     $in->post['numDepreciation'] = $year;
  206.     $in->post['numValue'] = $year;
  207.  
  208.     // load items
  209.     if (!$opt['itemstotal'] = dbCountAssetsItems($db, $clientID, $where, $year)) {
  210.         if ($where) {
  211.             $opt['countitems'] = 0;
  212.             return true;
  213.         }
  214.         return false;
  215.     } else $opt['countitems'] = $opt['itemstotal']['count'];
  216.  
  217.     if (!$opt['items'] = dbLoadAssetsItems($db, $clientID, -1, -1, $sort, $where))
  218.         return false;
  219.  
  220.     foreach ($opt['items'] as $key => $item) {
  221.         $firstYear = reset($item['lines']);
  222.         $lastYear = end($item['lines']);
  223.         $nullValue = makeNum(0, $opt['seperator'], $opt['decimal']);
  224.         if ($firstYear > $year) {
  225.             $opt['items'][$key]['numDepreciation'] = $nullValue;
  226.             $opt['items'][$key]['numValue'] = $nullValue;
  227.             continue;
  228.         }
  229.         if ($lastYear < $year) {
  230.             $opt['items'][$key]['numDepreciation'] = $nullValue;
  231.             $opt['items'][$key]['numValue'] = $item['numValue' . key($item['lines'])];
  232.             continue;
  233.         }
  234.         $years = array_flip($item['lines']);
  235.         $opt['items'][$key]['numDepreciation'] = $item['numDepreciation' . $years[$year]];
  236.         $opt['items'][$key]['numValue'] = $item['numValue' . $years[$year]];
  237.     };
  238.         
  239.     return true;    
  240. }
  241.  
  242. /**
  243.  * generate data for html output of assets book item
  244.  *
  245.  * @param    eaDB    database
  246.  * @param    eaInput input data
  247.  */
  248. function genOptAssetsItem(&$db, &$in) { 
  249.     $conf =& $db->conf;
  250.     $user =& $db->user;
  251.  
  252.     if (!isset($in->options)) $in->options = array();
  253.     $opt =& $in->options;
  254.  
  255.     if (!$clientID = $in->getClient($db)) return false; // wrong client
  256.     
  257.     $opt['assets'] = true; // we are in the assets book section
  258.     $opt['assetsurl'] = 'assetsitem';
  259.  
  260.     // prepare date format
  261.     $opt['dateformat'] = $conf['txtDateFormat'];;
  262.  
  263.     // prepare number format
  264.     $opt['decimal'] = $conf['numNumberDecimal'];
  265.     switch ($conf['txtNumberSeperator']) {
  266.         case 'komma':
  267.             $opt['seperator'] = ',';
  268.             break;
  269.         case 'dot': default:
  270.             $opt['seperator'] = '.';
  271.             break;
  272.     }
  273.  
  274.     $post =& $in->post;
  275.     if (!isset($post['numValue1'])) $post['numValue1'] = '';
  276.     $post['lines'] = array(1 => date('Y'));
  277.     
  278.     // load old data
  279.     if (($itemID = $in->getGet('item')) 
  280.         && ($dummy = dbLoadAssetsItem($db, $clientID, $itemID))) {
  281.         $in->post =& $dummy;
  282.         $post =& $in->post;
  283.         $post['numID'] = $post['id'];
  284.  
  285.         // prepare options derived from data 
  286.  
  287.     }
  288.     
  289.     return true;    
  290. }
  291.  
  292. /**
  293.  * save assets book item
  294.  *
  295.  * @param    eaDB    database
  296.  * @param    eaInput input data
  297.  */
  298. function saveAssetsItem(&$db, &$in) { 
  299.     if (!isset($_POST['buttonSaveAssetsItem'])) return false;
  300.     $in->post['buttonSaveAssetsItem'] = true;
  301.  
  302.     $conf =& $db->conf;
  303.     $user =& $db->user;
  304.  
  305.     $clientID = $in->options['client'];
  306.     if (!$db->isOwner($clientID)) return false; // wrong client
  307.  
  308.     // check for errors
  309.     $vars = array('p:txt:txt', 'p:num:num');
  310.     $in->check($vars);
  311.  
  312.     $error =& $in->error;
  313.     $post =& $in->post;
  314.     $opt =& $in->options;
  315.  
  316.     // calculate values if there are base values
  317.     $calc = true;
  318.  
  319.     // txtDate -> check date
  320.     if ($dummy = checkDateFormat($post['txtDate'], $conf['txtDateFormat'])) {
  321.         $post += $dummy;
  322.         $year = $post['year'];
  323.         $month = $post['month'];        
  324.     } else {
  325.         $error['txtDate'] = 'wrongformat';
  326.         $calc = false;
  327.         $year = date('Y');
  328.     }
  329.  
  330.     // check depriciation entries
  331.     
  332.     $amount = abs(makeDot($post['numAmount'], $opt['decimal']));
  333.     if (!$amount) $calc = false;
  334.     $numVars = array('numAmount');
  335.     
  336.     $lifetime = abs($post['numLifeTime']) % 100;
  337.     if (!$lifetime) {
  338.         $calc = false;
  339.         $lifetime = 1;
  340.     }
  341.     
  342.     $method = $in->post('txtDeprMethod');
  343.     switch ($method) {
  344.         case 'lin1': case 'lin6': break;
  345.         case 'custom': break;
  346.         default: 
  347.             $method = 'custom';
  348.             break;        
  349.     }
  350.     
  351.     $depr = makeDot($amount / $lifetime);
  352.  
  353.     for ($i = 1; $i <= $lifetime + 1; $i++) {
  354.         if ($calc) {        
  355.             $thisDepr = $depr; // default
  356.  
  357.             if ($i == 1) { // first year might be different 
  358.                 switch ($method) {
  359.                     case 'lin6':
  360.                         if ($month > 6) $thisDepr = makeDot($amount / $lifetime / 2);
  361.                         break;
  362.                     case 'lin1':
  363.                         if ($month > 1)
  364.                             $thisDepr = makeDot((13 - $post['month']) * $amount / $lifetime / 12);
  365.                         break;
  366.                 }        
  367.             } 
  368.  
  369.             if ($thisDepr + 1 < $amount) $amount -= $thisDepr;
  370.             else {
  371.                 $thisDepr = $amount;
  372.                 $amount = 0;
  373.             }
  374.  
  375.             switch ($method) {
  376.                 case 'lin6': case 'lin1':
  377.                     if (!$thisDepr) break 2;
  378.                     checkSetValue($in, 'numDepreciation' . $i, $thisDepr);
  379.                     checkSetValue($in, 'numValue' . $i, $amount);
  380.                     break;
  381.                 case 'custom';
  382.                     if ($i > $lifetime) break 2;
  383.             }
  384.         } else { if ($i > $lifetime) break; }
  385.                     
  386.         $numVars[] = 'numDepreciation' . $i;
  387.         $numVars[] = 'numValue' . $i;
  388.         $post['lines'][$i] = $year++;
  389.     }
  390.  
  391.     $amount = abs(makeDot($post['numAmount']));
  392.     for ($i = 1; $i <= $lifetime + 1; $i++) {
  393.         if (!isset($post['numDepreciation' . $i])) break;
  394.         if (!isset($post['numValue' . $i])) break;
  395.         
  396.         checkSetValue($in, 'numValue' . $i, $amount - makeDot($post['numDepreciation' . $i]));
  397.         checkSetValue($in, 'numDepreciation' . $i, $amount - makeDot($post['numValue' . $i]));
  398.         $amount = makeDot($post['numValue' . $i]);
  399.     }
  400.  
  401.     foreach ($numVars as $var) if (isset($post[$var]))
  402.         $post[$var] = makeNum($post[$var], $opt['seperator'], $opt['decimal']);
  403.  
  404.     if ($error) return false; // wrong input
  405.     
  406.     // save item    
  407.     return dbSaveAssetsItem($db, $clientID, $post);
  408. }
  409.  
  410. /**
  411.  * check & store post variables
  412.  *
  413.  * @param    eaInput input data
  414.  * @param    string    variable name
  415.  * @param    mixed    variable value
  416.  */
  417. function checkSetValue(&$in, $var, $val) {
  418.     $error =& $in->error;
  419.     $post =& $in->post;
  420.     $opt =& $in->options;
  421.  
  422.     if (!isset($post[$var])) { // !var -> set
  423.         $post[$var] = makeNum($val, $opt['seperator'], $opt['decimal']);
  424.         return true;
  425.     }
  426.     
  427.     if (isset($error[$var]) && ($error[$var] == 'empty')) { // empty var -> set
  428.         $post[$var] = makeNum($val, $opt['seperator'], $opt['decimal']);
  429.         unset($error[$var]);
  430.         return true;
  431.     }
  432.  
  433.     if (isset($error[$var])) // other error -> do nothing
  434.         return false;
  435.  
  436.     if (log10(abs(makeDot($post[$var], $opt['decimal']) - $val)) >= -$opt['decimal'] - 0.1) {
  437.         $error[$var] = 'wrongvalue';
  438.         return false;
  439.     }
  440.     
  441.     return true;    
  442. }
  443.  
  444. ?>

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