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

Source for file assets.db.php

Documentation is available at assets.db.php

  1. <?php 
  2. /**
  3.  * database 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.db.php 130 2007-08-17 14:24:44Z m2mtech $
  24.  * @link       http://www.ea-geier.at/
  25.  */
  26.  
  27. /**
  28.  * standard client db functions
  29.  */
  30. require_once('code/clients.db.php');
  31.  
  32. /**
  33.  * number functions
  34.  */
  35. require_once('code/numbers.inc.php');
  36.  
  37. /**
  38.  * date functions
  39.  */
  40. require_once('code/date.inc.php');
  41.  
  42. /**
  43.  * save assets-book item
  44.  *
  45.  * @param    eaDB    database
  46.  * @param    integer client
  47.  * @param    array    data
  48.  */
  49. function dbSaveAssetsItem(&$db, $client, &$data) { 
  50.     dbChangeToClientDB($db, $client);
  51.  
  52.     // prepare number format
  53.     $opt['decimal'] = $db->conf['numNumberDecimal'];
  54.  
  55.     // prepare variables
  56.     $vars = array('txtName', 'txtVendor', 'numAmount', 'numLifeTime', 'txtDeprMethod');
  57.     foreach ($vars as $var) if (isset($data[$var])) switch ($var) {
  58.         case 'numAmount':
  59.             $what[$var] = makeDot($data[$var], $opt['decimal']);
  60.             break;
  61.         default: $what[$var] = $data[$var];
  62.     }
  63.     
  64.     $what['firstYear'] = $data['year'];
  65.     foreach ($data['lines'] as $key => $year) {
  66.         $depr[$year] = array('d' => makeDot($data['numDepreciation' . $key]), 'v' => makeDot($data['numValue' . $key]));
  67.         $lastYear = $year;    
  68.     }
  69.     $what['data'] = serialize($depr);
  70.     
  71.     $what['lastYear'] = $lastYear;
  72.  
  73.     $what['purchaseDate'] = dateFormat($data, 'YYYY-MM-DD');
  74.  
  75.     $what['lastModified'] = $db->now();
  76.     
  77.     $what['who'] = $db->user['mail'];
  78.  
  79.     $table = $db->table('assets');
  80.  
  81.     if (isset($data['numID'])) { // old item
  82.         $id = $data['numID'];
  83.         if (!$db->update($what, $table, array('id' => $id))) return false;
  84.     } else {
  85.         if (!$db->insert($what, $table)) return false;
  86.         if (!$id = $db->lastID()) return false;
  87.     }
  88.  
  89.     return $data['numID'] = $id;
  90. }
  91.  
  92. /**
  93.  * load assets-book item
  94.  *
  95.  * @param    eaDB    database
  96.  * @param    integer clientID
  97.  * @param    integer itemID
  98.  * @return    array    data
  99.  */
  100. function dbLoadAssetsItem(&$db, $client, $item) {
  101.     dbChangeToClientDB($db, $client);
  102.  
  103.     $table = $db->table('assets');
  104.  
  105.     if (!$data = $db->selectOne('*', $table, array('id' => $item))) return false;
  106.  
  107.     dbPrepareAssetsSQLData($db->conf, $data);
  108.     
  109.     return $data;
  110. }
  111.  
  112. /**
  113.  * get number of assets items
  114.  *
  115.  * @param    eaDB    database
  116.  * @param    integer clientID
  117.  * @param    array    filter
  118.  * @param    integer    year
  119.  * @return    integer number of items
  120.  */
  121. function dbCountAssetsItems(&$db, $client, $filters = false, $year = false) { 
  122.     dbChangeToClientDB($db, $client);
  123.  
  124.     $table = $db->table('assets');
  125.  
  126.     $what = 'count(id) as c, sum(numAmount) as numAmount';
  127.  
  128.     $where = ''; // prepare where clause manually
  129.     $next = '';
  130.     if (isset($filters['txtName'])) { 
  131.         $where = "txtName like '%" . $db->escape($filters['txtName'], true) . "%'";    
  132.         unset($filters['txtName']);
  133.         $next = ' and ';
  134.     }
  135.     if ($filters) // other filters available
  136.         foreach ($filters as $key => $value) {
  137.             $where .= $next . $db->escapeKey($key) . ' = ' . $db->escape($value);
  138.             $next = ' and ';            
  139.         }        
  140.  
  141.     if (!$dummy = $db->selectOne($what, $table, $where)) return false;
  142.  
  143.     if (!$dummy['c']) return false;
  144.     
  145.     $data['count'] = $dummy['c'];
  146.     $data['numAmount'] = $dummy['numAmount'];
  147.     $data['numDepreciation'] = 0;
  148.     $data['numValue'] = 0;
  149.  
  150.     if ($year) {
  151.         $what = 'data';
  152.         $wYear = $db->escape($year);
  153.         $where .= $next . 'firstYear <= ' . $wYear . ' and lastYear >= ' . $wYear;
  154.         if ($dummy = $db->select($what, $table, $where)) 
  155.             foreach ($dummy as $item) {
  156.                 $depr = unserialize($item['data']);
  157.                 foreach ($depr as $yearKey => $var) {
  158.                     if ($year != $yearKey) continue;
  159.                     $data['numDepreciation'] += $var['d'];
  160.                     $data['numValue'] += $var['v'];
  161.                 }
  162.             }        
  163.     }
  164.     
  165.     dbPrepareAssetsSQLData($db->conf, $data);
  166.     
  167.     return $data;
  168. }
  169.  
  170. /**
  171.  * load multiple assets book item
  172.  *
  173.  * @param    eaDB    database
  174.  * @param    integer clientID
  175.  * @param    integer number of items
  176.  * @param    integer number page
  177.  * @param    string    sorting string
  178.  * @param    array    filtering
  179.  * @return    array    data
  180.  */
  181. function dbLoadAssetsItems(&$db, $client, $num = 10, $page = 1, $sort = false, $where = false) { 
  182.     dbChangeToClientDB($db, $client);
  183.  
  184.     $table = $db->table('assets');
  185.  
  186.     if (!$where) $where = '';
  187.     if (isset($where['txtName'])) { // we have to prepare it ourselves 
  188.         $filters = $where;
  189.         $where = "txtName like '%" . $db->escape($where['txtName'], true) . "%'";    
  190.         unset($filters['txtName']);
  191.         if ($filters) { // other filters available
  192.             foreach ($filters as $key => $value)
  193.                 $where .= ' and ' . $db->escapeKey($key) . ' = ' . $db->escape($value);
  194.         }        
  195.     }
  196.     
  197.     if (!$sort) $sort = 'id asc';
  198.  
  199.     if ($page == -1) $offset = -1;
  200.     else $offset = $num * ($page - 1);
  201.  
  202.     if (!$data = $db->select('*', $table, $where, $sort, $num, $offset))
  203.         return false;
  204.  
  205.     foreach ($data as $key => $val) dbPrepareAssetsSQLData($db->conf, $data[$key]);    
  206.     return $data;
  207. }
  208.  
  209. /**
  210.  * prepare the sql data for output
  211.  *
  212.  * @param    array    configuration data
  213.  * @param    array    assets item
  214.  */
  215. function dbPrepareAssetsSQLData(&$conf, &$data) {
  216.     static $decimal = false;
  217.     static $separator = false;
  218.     static $date = false;
  219.  
  220.     if (!$decimal) $decimal = $conf['numNumberDecimal'];
  221.     if (!$separator) switch ($conf['txtNumberSeperator']) {
  222.         case 'komma':
  223.             $separator = ',';
  224.             break;
  225.         case 'dot': default:
  226.             $separator = '.';
  227.             break;
  228.     }
  229.     if (!$date) $date = $conf['txtDateFormat'];
  230.     
  231.     // prepare depreciation data
  232.         if (isset($data['data'])) {
  233.         $depr = unserialize($data['data']);
  234.         $i = 1;
  235.         foreach ($depr as $year => $var) {
  236.             $data['lines'][$i] = $year;
  237.             $data['numDepreciation' . $i] = $var['d'];
  238.             $data['numValue' . $i] = $var['v'];
  239.             $numVars[] = 'numDepreciation' . $i;
  240.             $numVars[] = 'numValue' . $i;
  241.             $i++;
  242.         }
  243.     }
  244.  
  245.     // prepare numbers
  246.         $numVars[] = 'numAmount';
  247.     if (isset($data['numDepreciation'])) $numVars[] = 'numDepreciation';
  248.     if (isset($data['numValue'])) $numVars[] = 'numValue';
  249.     foreach ($numVars as $var) 
  250.         $data[$var] = makeNum($data[$var], $separator, $decimal);
  251.  
  252.     // prepare date
  253.         if ($dummy = checkDateFormat($data['purchaseDate'], 'YYYY-MM-DD'))
  254.         $data += $dummy;
  255.     $data['txtDate'] = dateFormat($data, $date);    
  256. }
  257.  
  258. /**
  259.  * get years in database
  260.  *
  261.  * @param    eaDB    database
  262.  * @param    integer clientID
  263.  * @param    array    filter
  264.  * @return    array    templates
  265.  */
  266. function dbGetAssetsYears(&$db, $client, $where = false) { 
  267.     dbChangeToClientDB($db, $client);
  268.  
  269.     $table = $db->table('assets');
  270.  
  271.     if (!$where) $where = '';
  272.     if (isset($where['txtName'])) { // we have to prepare it ourselves 
  273.         $filters = $where;
  274.         $where = "txtName like '%" . $db->escape($where['txtName'], true) . "%'";    
  275.         unset($filters['txtName']);
  276.         if ($filters) { // other filters available
  277.             foreach ($filters as $key => $value)
  278.                 $where .= ' and ' . $db->escapeKey($key) . ' = ' . $db->escape($value);
  279.         }        
  280.     }
  281.  
  282.     if (!$dummy = $db->selectOne('min(firstYear) as first, max(lastYear) as last', $table, $where))
  283.         return false;
  284.     
  285.     $first = (integer) $dummy['first'];
  286.     $last = (integer) $dummy['last'];
  287.  
  288.     for ($i = $first; $i <= $last; $i++) {
  289.         $date = array('day' => '31', 'month' => '12', 'year' => $i);
  290.         $years[$i] = dateFormat($date, $db->conf['txtDateFormat']);
  291.     }
  292.     
  293.     return $years;
  294. }
  295.  
  296. ?>

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