phpgroupware-cvs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Phpgroupware-cvs] property/inc class.acl2.inc.php class.boadmin_e...


From: Sigurd Nes
Subject: [Phpgroupware-cvs] property/inc class.acl2.inc.php class.boadmin_e...
Date: Mon, 30 Jan 2006 22:14:20 +0000

CVSROOT:        /sources/phpgroupware
Module name:    property
Branch:         
Changes by:     Sigurd Nes <address@hidden>     06/01/30 22:14:19

Modified files:
        inc            : class.acl2.inc.php class.boadmin_entity.inc.php 
                         class.boadmin_location.inc.php 
                         class.soactor.inc.php class.soadmin.inc.php 
                         class.soadmin_entity.inc.php 
                         class.soadmin_location.inc.php 
                         class.soagreement.inc.php class.soalarm.inc.php 
                         class.soasync.inc.php class.sob_account.inc.php 
                         class.socategory.inc.php class.socustom.inc.php 
                         class.sodocument.inc.php class.soentity.inc.php 
                         class.soinvestment.inc.php 
                         class.soinvoice.inc.php 
                         class.solocation.inc.php class.solookup.inc.php 
                         class.someter.inc.php class.sop_of_town.inc.php 
                         class.sopricebook.inc.php 
                         class.soproject.inc.php class.sorequest.inc.php 
                         class.sos_agreement.inc.php 
                         class.sostandard_2.inc.php 
                         class.sostandard_3.inc.php 
                         class.sostandard_entity.inc.php 
                         class.sotemplate.inc.php 
                         class.sotenant_claim.inc.php 
                         class.sotts.inc.php class.sotts2.inc.php 
                         class.sowo_hour.inc.php 
                         class.soworkorder.inc.php class.soXport.inc.php 
                         class.uiadmin_entity.inc.php 

Log message:
        no message

CVSWeb URLs:
http://cvs.savannah.gnu.org/viewcvs/phpgroupware/property/inc/class.acl2.inc.php.diff?tr1=1.7&tr2=1.8&r1=text&r2=text
http://cvs.savannah.gnu.org/viewcvs/phpgroupware/property/inc/class.boadmin_entity.inc.php.diff?tr1=1.9&tr2=1.10&r1=text&r2=text
http://cvs.savannah.gnu.org/viewcvs/phpgroupware/property/inc/class.boadmin_location.inc.php.diff?tr1=1.7&tr2=1.8&r1=text&r2=text
http://cvs.savannah.gnu.org/viewcvs/phpgroupware/property/inc/class.soactor.inc.php.diff?tr1=1.10&tr2=1.11&r1=text&r2=text
http://cvs.savannah.gnu.org/viewcvs/phpgroupware/property/inc/class.soadmin.inc.php.diff?tr1=1.6&tr2=1.7&r1=text&r2=text
http://cvs.savannah.gnu.org/viewcvs/phpgroupware/property/inc/class.soadmin_entity.inc.php.diff?tr1=1.11&tr2=1.12&r1=text&r2=text
http://cvs.savannah.gnu.org/viewcvs/phpgroupware/property/inc/class.soadmin_location.inc.php.diff?tr1=1.14&tr2=1.15&r1=text&r2=text
http://cvs.savannah.gnu.org/viewcvs/phpgroupware/property/inc/class.soagreement.inc.php.diff?tr1=1.9&tr2=1.10&r1=text&r2=text
http://cvs.savannah.gnu.org/viewcvs/phpgroupware/property/inc/class.soalarm.inc.php.diff?tr1=1.6&tr2=1.7&r1=text&r2=text
http://cvs.savannah.gnu.org/viewcvs/phpgroupware/property/inc/class.soasync.inc.php.diff?tr1=1.5&tr2=1.6&r1=text&r2=text
http://cvs.savannah.gnu.org/viewcvs/phpgroupware/property/inc/class.sob_account.inc.php.diff?tr1=1.7&tr2=1.8&r1=text&r2=text
http://cvs.savannah.gnu.org/viewcvs/phpgroupware/property/inc/class.socategory.inc.php.diff?tr1=1.12&tr2=1.13&r1=text&r2=text
http://cvs.savannah.gnu.org/viewcvs/phpgroupware/property/inc/class.socustom.inc.php.diff?tr1=1.7&tr2=1.8&r1=text&r2=text
http://cvs.savannah.gnu.org/viewcvs/phpgroupware/property/inc/class.sodocument.inc.php.diff?tr1=1.8&tr2=1.9&r1=text&r2=text
http://cvs.savannah.gnu.org/viewcvs/phpgroupware/property/inc/class.soentity.inc.php.diff?tr1=1.11&tr2=1.12&r1=text&r2=text
http://cvs.savannah.gnu.org/viewcvs/phpgroupware/property/inc/class.soinvestment.inc.php.diff?tr1=1.7&tr2=1.8&r1=text&r2=text
http://cvs.savannah.gnu.org/viewcvs/phpgroupware/property/inc/class.soinvoice.inc.php.diff?tr1=1.18&tr2=1.19&r1=text&r2=text
http://cvs.savannah.gnu.org/viewcvs/phpgroupware/property/inc/class.solocation.inc.php.diff?tr1=1.20&tr2=1.21&r1=text&r2=text
http://cvs.savannah.gnu.org/viewcvs/phpgroupware/property/inc/class.solookup.inc.php.diff?tr1=1.8&tr2=1.9&r1=text&r2=text
http://cvs.savannah.gnu.org/viewcvs/phpgroupware/property/inc/class.someter.inc.php.diff?tr1=1.6&tr2=1.7&r1=text&r2=text
http://cvs.savannah.gnu.org/viewcvs/phpgroupware/property/inc/class.sop_of_town.inc.php.diff?tr1=1.6&tr2=1.7&r1=text&r2=text
http://cvs.savannah.gnu.org/viewcvs/phpgroupware/property/inc/class.sopricebook.inc.php.diff?tr1=1.13&tr2=1.14&r1=text&r2=text
http://cvs.savannah.gnu.org/viewcvs/phpgroupware/property/inc/class.soproject.inc.php.diff?tr1=1.25&tr2=1.26&r1=text&r2=text
http://cvs.savannah.gnu.org/viewcvs/phpgroupware/property/inc/class.sorequest.inc.php.diff?tr1=1.12&tr2=1.13&r1=text&r2=text
http://cvs.savannah.gnu.org/viewcvs/phpgroupware/property/inc/class.sos_agreement.inc.php.diff?tr1=1.15&tr2=1.16&r1=text&r2=text
http://cvs.savannah.gnu.org/viewcvs/phpgroupware/property/inc/class.sostandard_2.inc.php.diff?tr1=1.7&tr2=1.8&r1=text&r2=text
http://cvs.savannah.gnu.org/viewcvs/phpgroupware/property/inc/class.sostandard_3.inc.php.diff?tr1=1.8&tr2=1.9&r1=text&r2=text
http://cvs.savannah.gnu.org/viewcvs/phpgroupware/property/inc/class.sostandard_entity.inc.php.diff?tr1=1.6&tr2=1.7&r1=text&r2=text
http://cvs.savannah.gnu.org/viewcvs/phpgroupware/property/inc/class.sotemplate.inc.php.diff?tr1=1.6&tr2=1.7&r1=text&r2=text
http://cvs.savannah.gnu.org/viewcvs/phpgroupware/property/inc/class.sotenant_claim.inc.php.diff?tr1=1.8&tr2=1.9&r1=text&r2=text
http://cvs.savannah.gnu.org/viewcvs/phpgroupware/property/inc/class.sotts.inc.php.diff?tr1=1.12&tr2=1.13&r1=text&r2=text
http://cvs.savannah.gnu.org/viewcvs/phpgroupware/property/inc/class.sotts2.inc.php.diff?tr1=1.9&tr2=1.10&r1=text&r2=text
http://cvs.savannah.gnu.org/viewcvs/phpgroupware/property/inc/class.sowo_hour.inc.php.diff?tr1=1.10&tr2=1.11&r1=text&r2=text
http://cvs.savannah.gnu.org/viewcvs/phpgroupware/property/inc/class.soworkorder.inc.php.diff?tr1=1.13&tr2=1.14&r1=text&r2=text
http://cvs.savannah.gnu.org/viewcvs/phpgroupware/property/inc/class.soXport.inc.php.diff?tr1=1.20&tr2=1.21&r1=text&r2=text
http://cvs.savannah.gnu.org/viewcvs/phpgroupware/property/inc/class.uiadmin_entity.inc.php.diff?tr1=1.12&tr2=1.13&r1=text&r2=text

Patches:
Index: property/inc/class.acl2.inc.php
diff -u property/inc/class.acl2.inc.php:1.7 property/inc/class.acl2.inc.php:1.8
--- property/inc/class.acl2.inc.php:1.7 Fri Jan 27 17:03:30 2006
+++ property/inc/class.acl2.inc.php     Mon Jan 30 22:14:19 2006
@@ -1,44 +1,44 @@
-<?php
-       /**
-       * phpGroupWare - property: a Facilities Management System.
-       *
-       * @author Sigurd Nes <address@hidden>
-       * @copyright Copyright (C) 2003-2005 Free Software Foundation, Inc. 
http://www.fsf.org/
-       * @license http://www.gnu.org/licenses/gpl.html GNU General Public 
License
-       * @internal Development of this application was funded by 
http://www.bergen.kommune.no/bbb_/ekstern/
-       * @package property
-       * @subpackage core
-       * @version $Id: class.acl2.inc.php,v 1.7 2006/01/27 17:03:30 sigurdne 
Exp $
-       */
-
-       /**
-        * Description
-        * @package property
-        */
-
-       class acl2
-       {
-               var $account_id;
-               var $host_id;
-               var $app_id;
-               var $memberships = Array(0=>1); //group 0 is for all users
-               var $memberships_sql = ',0'; //group 0 is for all users
-               var $rights_cache = Array();
-               var $masks_cache = Array();
-               var $previous_location; // used for inheritance
-               var $db;
-
-               
/*************************************************************************\
-               * These lines load up the templates class and set some default 
values     *
-               
\*************************************************************************/
-               function acl2()
-               {
-                       $expected_args[0] = 
Array('name'=>'account_id','default'=>$GLOBALS['phpgw_info']['user']['account_id'],
 'type'=>'number');
-                       $expected_args[1] = 
Array('name'=>'host_id','default'=>0, 'type'=>'number');
-                       $expected_args[2] = 
Array('name'=>'app_id','default'=>$GLOBALS['phpgw']->applications->data[$GLOBALS['phpgw_info']['flags']['currentapp']]['id'],
 'type'=>'number');
-                       $recieved_args = func_get_args();
-                       $args = safe_args($expected_args, 
$recieved_args,__LINE__,__FILE__);
-               //      $this->db       = $GLOBALS['phpgw']->db;
+<?php
+       /**
+       * phpGroupWare - property: a Facilities Management System.
+       *
+       * @author Sigurd Nes <address@hidden>
+       * @copyright Copyright (C) 2003-2005 Free Software Foundation, Inc. 
http://www.fsf.org/
+       * @license http://www.gnu.org/licenses/gpl.html GNU General Public 
License
+       * @internal Development of this application was funded by 
http://www.bergen.kommune.no/bbb_/ekstern/
+       * @package property
+       * @subpackage core
+       * @version $Id: class.acl2.inc.php,v 1.8 2006/01/30 22:14:19 sigurdne 
Exp $
+       */
+
+       /**
+        * Description
+        * @package property
+        */
+
+       class acl2
+       {
+               var $account_id;
+               var $host_id;
+               var $app_id;
+               var $memberships = Array(0=>1); //group 0 is for all users
+               var $memberships_sql = ',0'; //group 0 is for all users
+               var $rights_cache = Array();
+               var $masks_cache = Array();
+               var $previous_location; // used for inheritance
+               var $db;
+
+               
/*************************************************************************\
+               * These lines load up the templates class and set some default 
values     *
+               
\*************************************************************************/
+               function acl2()
+               {
+                       $expected_args[0] = 
Array('name'=>'account_id','default'=>$GLOBALS['phpgw_info']['user']['account_id'],
 'type'=>'number');
+                       $expected_args[1] = 
Array('name'=>'host_id','default'=>0, 'type'=>'number');
+                       $expected_args[2] = 
Array('name'=>'app_id','default'=>$GLOBALS['phpgw']->applications->data[$GLOBALS['phpgw_info']['flags']['currentapp']]['id'],
 'type'=>'number');
+                       $recieved_args = func_get_args();
+                       $args = safe_args($expected_args, 
$recieved_args,__LINE__,__FILE__);
+               //      $this->db       = $GLOBALS['phpgw']->db;
                        $this->db = CreateObject('phpgwapi.db');
                        $this->db->Host = 
$GLOBALS['phpgw_info']['server']['db_host'];
                        $this->db->Type = 
$GLOBALS['phpgw_info']['server']['db_type'];
@@ -53,632 +53,632 @@
                        $this->db2->User = 
$GLOBALS['phpgw_info']['server']['db_user'];
                        $this->db2->Password = 
$GLOBALS['phpgw_info']['server']['db_pass'];
 
-               //      $this->db2      = $this->db;
-                       $this->account_id = $args['account_id'];
-                       $this->host_id = $args['host_id'];
-                       $this->app_id = $args['app_id'];
-
-                       if 
($GLOBALS['phpgw_info']['server']['db_type']=='pgsql')
-                       {
-                               $this->like = "ILIKE";
-                       }
-                       else
-                       {
-                               $this->like = "LIKE";
-                       }
-
-
-               }
-
-               function get_memberships ()
-               {
-                       $expected_args[0] = 
Array('name'=>'account_id','default'=>$this->account_id, 'type'=>'number');
-                       $recieved_args = func_get_args();
-                       $args = safe_args($expected_args, 
$recieved_args,__LINE__,__FILE__);
-
-                       $sql = "SELECT acl_location,acl_rights FROM fm_acl2
-                                                       WHERE ( 
acl_host='".$this->host_id."' and acl_appid = 0 and acl_account = 
".$args['account_id'].")";
-                       $this->db->query($sql,__LINE__,__FILE__);
-
-                       while ($this->db->next_record())
-                       {
-                               
if(!isset($this->memberships[$this->db->f('acl_location')]))
-                               {
-                                       
$this->memberships[$this->db->f('acl_location')] = $this->db->f('acl_rights');
-                                       $this->memberships_sql .= 
','.$this->db->f('acl_location');
-                                       
$this->get_memberships(Array('account_id'=>$this->db->f('acl_location')));
-                               }
-                       }
-               }
-
-
-               function cache_rights()
-               {
-                       $expected_args[0] = 
Array('name'=>'location','default'=>'##REQUIRED##', 'type'=>'alphanumeric');
-                       $expected_args[1] = 
Array('name'=>'app_id','default'=>$this->app_id, 'type'=>'number');
-                       $expected_args[2] = 
Array('name'=>'account_id','default'=>$this->account_id, 'type'=>'number');
-                       $expected_args[3] = 
Array('name'=>'grantor','default'=>False, 'type'=>'any');
-                       $expected_args[4] = 
Array('name'=>'no_effective','default'=>false, 'type'=>'any');
-
-                       $recieved_args = func_get_args();
-                       $args = safe_args($expected_args, 
$recieved_args,__LINE__,__FILE__);
-
-                       
if(isset($this->rights_cache[$args['app_id']][$args['account_id']][$args['location']]))
-                       {
-                               return;
-                       }
-
-                       $sql = "SELECT 
acl_rights,acl_type,acl_data,acl_location FROM fm_acl2 WHERE (acl_appid = 
'".$args['app_id']."' ";
-                       $sql .= " and (acl_account in 
(".$args['account_id']."".$this->memberships_sql.'))';
-                       $sql .= " and acl_location = '". $args['location'] . 
"'";
-
-                       if($args['grantor']>0)
-                       {
-                               $sql .= " and (grantor=" . $args['grantor'] . 
")";
-                       }
-                       else
-                       {
-                               $sql .= " and (grantor is NULL)";
-                       }
-
-                       $sql .= ') ORDER BY acl_location, acl_type DESC';
-
-                       $this->db->query($sql,__LINE__,__FILE__);
-                       while($this->db->next_record())
-                       {
-                               
if($this->rights_cache[$args['app_id']][$args['account_id']][$args['location']] 
== 0)
-                               {
-                                       
$this->rights_cache[$args['app_id']][$args['account_id']][$this->db->f('acl_location')]
 = 0;
-                               }
-
-                               
if($this->masks_cache[$args['app_id']][$args['account_id']][$args['location']] 
== 0)
-                               {
-                                       
$this->masks_cache[$args['app_id']][$args['account_id']][$this->db->f('acl_location')]
 = 0;
-                               }
-
-                               if((int)$this->db->f('acl_type') == 0)
-                               {
-                                       
$this->rights_cache[$args['app_id']][$args['account_id']][$this->db->f('acl_location')]
 = 
$this->bit_set($this->rights_cache[$args['app_id']][$args['account_id']][$this->db->f('acl_location')],(int)$this->db->f('acl_rights'));
-                               }
-                               else
-                               {
-                                       
$this->masks_cache[$args['app_id']][$args['account_id']][$this->db->f('acl_location')]
 = 
$this->bit_set($this->rights_cache[$args['app_id']][$args['account_id']][$this->db->f('acl_location')],(int)$this->db->f('acl_rights'));
-                               }
-
-                               if(!$this->db->f('acl_rights'))
-                               {
-                                       
$this->rights_cache[$args['app_id']][$args['account_id']][$args['location']]=0;
-                                       
$this->masks_cache[$args['app_id']][$args['account_id']][$args['location']]=0;
-                               }
-
-                               if(!$args['no_effective'])
-                               {
-                                       
$this->rights_cache[$args['app_id']][$args['account_id']][$this->db->f('acl_location')]
 = 
$this->bit_mask($this->rights_cache[$args['app_id']][$args['account_id']][$args['location']],
 $this->masks_cache[$args['app_id']][$args['account_id']][$args['location']]);
-                               }
-
-                       }
-               }
-
-
-               function check_grant()
-               {
-                       $expected_args[0] = 
Array('name'=>'grantor','default'=>$this->account_id, 'type'=>'number');
-                       $expected_args[1] = 
Array('name'=>'location','default'=>'##REQUIRED##', 'type'=>'alphanumeric');
-                       $expected_args[2] = 
Array('name'=>'required','default'=>1, 'type'=>'number');
-                       $expected_args[3] = 
Array('name'=>'account_id','default'=>$this->account_id, 'type'=>'number');
-                       $expected_args[4] = 
Array('name'=>'app_id','default'=>$this->app_id, 'type'=>'number');
-                       $expected_args[5] = 
Array('name'=>'no_membership','default'=>false, 'type'=>'number');
-                       $expected_args[6] = 
Array('name'=>'acl_type','default'=>0, 'type'=>'number');
-                       $expected_args[7] = 
Array('name'=>'no_effective','default'=>false, 'type'=>'any');
-                       $recieved_args = func_get_args();
-                       $args = safe_args($expected_args, 
$recieved_args,__LINE__,__FILE__);
-
-                       if(!$args['no_membership'])
-                       {
-                               $memberships = 
$GLOBALS['phpgw']->accounts->membership($args['account_id']);
-
-                               while (is_array($memberships) && 
list(,$account) = each($memberships))
-                               {
-                                       $member_of[] = $account['account_id'];
-                               }
-
-                               if($member_of)
-                               {
-                                       $this->memberships_sql = ',' . 
@implode(",",$member_of);
-                               }
-                       }
-
-                       
$this->cache_rights($args['location'],$args['app_id'],$args['account_id'],$args['grantor'],$args['no_effective']);
-                       if($args['acl_type']==0)
-                       {
-                               return 
$this->bit_check($this->rights_cache[$args['app_id']][$args['account_id']][$args['location']],$args['required']);
-                       }
-                       else
-                       {
-                               return 
$this->bit_check($this->masks_cache[$args['app_id']][$args['account_id']][$args['location']],$args['required']);
-                       }
-               }
-
-               function check()
-               {
-                       $expected_args[0] = 
Array('name'=>'location','default'=>'##REQUIRED##', 'type'=>'alphanumeric');
-                       $expected_args[1] = 
Array('name'=>'required','default'=>1, 'type'=>'number');
-                       $expected_args[2] = 
Array('name'=>'account_id','default'=>$this->account_id, 'type'=>'number');
-                       $expected_args[3] = 
Array('name'=>'app_id','default'=>$this->app_id, 'type'=>'number');
-                       $expected_args[4] = 
Array('name'=>'no_membership','default'=>false, 'type'=>'number');
-                       $expected_args[5] = 
Array('name'=>'acl_type','default'=>0, 'type'=>'number');
-                       $expected_args[6] = 
Array('name'=>'no_effective','default'=>false, 'type'=>'any');
-                       $recieved_args = func_get_args();
-                       $args = safe_args($expected_args, 
$recieved_args,__LINE__,__FILE__);
-
-                       if(!$args['no_membership'])
-                       {
-                               $memberships = 
$GLOBALS['phpgw']->accounts->membership($args['account_id']);
-
-                               while (is_array($memberships) && 
list(,$account) = each($memberships))
-                               {
-                                       $member_of[] = $account['account_id'];
-                               }
-
-                               if($member_of)
-                               {
-                                       $this->memberships_sql = ',' . 
@implode(",",$member_of);
-                               }
-                       }
-
-                       
$this->cache_rights($args['location'],$args['app_id'],$args['account_id'],False,$args['no_effective']);
-                       if($args['acl_type']==0)
-                       {
-                               return 
$this->bit_check($this->rights_cache[$args['app_id']][$args['account_id']][$args['location']],$args['required']);
-                       }
-                       else
-                       {
-                               return 
$this->bit_check($this->masks_cache[$args['app_id']][$args['account_id']][$args['location']],$args['required']);
-                       }
-               }
-
-               /*!
-               @function add
-               @abstract Adds ACL record to $acl->data
-               @discussion Adds ACL record to $acl->data. <br>
-               Syntax: array add() <br>
-               Example1: acl->add();
-               @param $appname default False derives value from 
$phpgw_info['flags']['currentapp']
-               @param $location location
-               @param $rights rights
-               */
-               function add()
-               {
-                       $expected_args[0] = 
Array('name'=>'location','default'=>'##REQUIRED##', 'type'=>'alphanumeric');
-                       $expected_args[1] = 
Array('name'=>'rights','default'=>1, 'type'=>'number');
-                       $expected_args[2] = 
Array('name'=>'account_id','default'=>$this->account_id, 'type'=>'number');
-                       $expected_args[3] = Array('name'=>'type','default'=>0, 
'type'=>'number');
-                       $expected_args[4] = 
Array('name'=>'app_id','default'=>$this->app_id, 'type'=>'number');
-                       $expected_args[5] = 
Array('name'=>'data','default'=>NULL, 'type'=>'any');
-                       $recieved_args = func_get_args();
-                       $args = safe_args($expected_args, 
$recieved_args,__LINE__,__FILE__);
-
-                       $sql = "SELECT acl_rights FROM fm_acl2 WHERE (acl_appid 
= '".$args['app_id']."' ";
-                       $sql .= " and acl_account = ".$args['account_id'];
-                       $sql .= " and acl_location = '".$args['location']."' 
and acl_type=".$args['type']." and grantor is NULL)";
-                       $this->db->query($sql,__LINE__,__FILE__);
-                       if($this->db->num_rows() != 0)
-                       {
-                               $this->db->next_record();
-                               $newrights = $this->bit_set($args['rights'], 
(int)$this->db->f('acl_rights'));
-                               $sql = "UPDATE fm_acl2 SET acl_rights 
=".$newrights;
-                               $sql .= " WHERE acl_host=".$this->host_id." AND 
acl_appid=".$args['app_id']." AND acl_account=".$args['account_id']." AND 
acl_location='".$args['location']."' AND acl_type=".$args['type'];
-                       }
-                       else
-                       {
-                               $sql = "INSERT INTO fm_acl2 
(acl_host,acl_appid,acl_account,acl_location,acl_rights,acl_type,acl_data) 
VALUES 
(".$this->host_id.",".$args['app_id'].",".$args['account_id'].",'".$args['location']."',".$args['rights'].",".$args['type'].",'".$args['data']."')";
-                       }
-                       $this->db->query($sql,__LINE__,__FILE__);
-                       $this->rights_cache = Array();
-                       $this->masks_cache = Array();
-               }
-
-               function set_grant()  //inheritance is saved to the database
-               {
-                       $expected_args[0] = 
Array('name'=>'location','default'=>'##REQUIRED##', 'type'=>'alphanumeric');
-                       $expected_args[1] = 
Array('name'=>'rights','default'=>1, 'type'=>'number');
-                       $expected_args[2] = 
Array('name'=>'account_id','default'=>$this->account_id, 'type'=>'number');
-                       $expected_args[3] = Array('name'=>'type','default'=>0, 
'type'=>'number');
-                       $expected_args[4] = 
Array('name'=>'grantor','default'=>NULL, 'type'=>'any');
-                       $expected_args[5] = 
Array('name'=>'app_id','default'=>$this->app_id, 'type'=>'number');
-                       $expected_args[6] = 
Array('name'=>'data','default'=>NULL, 'type'=>'any');
-                       $recieved_args = func_get_args();
-                       $args = safe_args($expected_args, 
$recieved_args,__LINE__,__FILE__);
-                       if($args['grantor'])
-                       {
-                               $grantor = $args['grantor'];
-                               $grantor_type = 'g';
-                       }
-                       else
-                       {
-                               $grantor = 
$GLOBALS['phpgw_info']['user']['account_id'];
-                       }
-
-                       $sql = "SELECT acl_rights, acl_location FROM fm_acl2 
WHERE (acl_appid = '".$args['app_id']."' ";
-                       $sql .= " and acl_account = ".$args['account_id'];
-                       $sql .= " and acl_location = '".$args['location']."' 
and acl_type=".$args['type']." and grantor = $grantor)";
-                       $this->db->query($sql,__LINE__,__FILE__);
-                       while($this->db->next_record())
-                       {
-                               if((int)$args['rights'] == 0)
-                               {
-                                       $sql = "DELETE FROM fm_acl2";
-                               }
-                               else
-                               {
-                                       $sql = "UPDATE fm_acl2 SET acl_rights 
=".$args['rights'];
-                               }
-                               $sql .= " WHERE acl_host=".$this->host_id." AND 
acl_appid=".$args['app_id']." AND acl_account=".$args['account_id']." AND 
acl_location='".$this->db->f('acl_location')."' AND acl_type=".$args['type'] . 
" AND grantor = $grantor";
-                               $this->db2->query($sql,__LINE__,__FILE__);
-
-                               
$updated_locations[]=$this->db->f('acl_location');
-                       }
-
-                       $sql = "SELECT id FROM fm_acl_location WHERE (id 
$this->like '%".$args['location']."%') AND allow_grant=1";
-                       $this->db->query($sql,__LINE__,__FILE__);
-                       while($this->db->next_record())
-                       {
-
-                               $add_locations[]=$this->db->f('id');
-                       }
-
-                       if($updated_locations)
-                       {
-                               $add_locations  = array_diff($add_locations, 
$updated_locations);
-                       }
-                       $add_locations = 
explode("-",implode("-",$add_locations));
-                       if(is_array($add_locations) && count($add_locations)>0)
-                       {
-                               for ($i=0;$i<count($add_locations);$i++)
-                               {
-
-                                       for ($j=0;$j<($i+1);$j++)
-                                       {
-                                               $location       
=$add_locations[$j];
-                                       }
-
-                                       
if(strlen($location)>=strlen($args['location']))
-                                       {
-                                               if($args['rights'] != 0)
-                                               {
-                                                       $sql = "SELECT 
acl_rights FROM fm_acl2 WHERE (acl_appid = '".$args['app_id']."' ";
-                                                       $sql .= " and 
acl_account = ".$args['account_id'];
-                                                       $sql .= " and 
acl_location = '$location' and acl_type=".$args['type']. " AND grantor = 
$grantor)";
-                                                       
$this->db->query($sql,__LINE__,__FILE__);
-
-                                                       
if($this->db->num_rows() == 0)
-                                                       {
-                                                               $sql = "INSERT 
INTO fm_acl2 
(acl_host,acl_appid,acl_account,acl_location,acl_rights,acl_type,acl_data,grantor,grantor_type)
 VALUES 
(".$this->host_id.",".$args['app_id'].",".$args['account_id'].",'".$location."',".$args['rights'].",".$args['type'].",'".$args['data']."',$grantor,'$grantor_type')";
-
-                                                               
$this->db2->query($sql,__LINE__,__FILE__);
-                                                       }
-                                                       else
-                                                       {
-                                                               $sql = "UPDATE 
fm_acl2 SET acl_rights =".$args['rights'];
-                                                               $sql .= " WHERE 
acl_host=".$this->host_id." AND acl_appid=".$args['app_id']." AND 
acl_account=".$args['account_id']." AND acl_location='".$location."' AND 
acl_type=".$args['type']  . " AND grantor = $grantor";
-                                                               
$this->db2->query($sql,__LINE__,__FILE__);
-                                                       }
-                                               }
-                                       }
-                                       unset($location);
-                               }
-                       }
-
-                       $this->rights_cache = Array();
-                       $this->masks_cache = Array();
-               }
-
-               function set()  //inheritance is saved to the database
-               {
-                       $expected_args[0] = 
Array('name'=>'location','default'=>'##REQUIRED##', 'type'=>'alphanumeric');
-                       $expected_args[1] = 
Array('name'=>'rights','default'=>1, 'type'=>'number');
-                       $expected_args[2] = 
Array('name'=>'account_id','default'=>$this->account_id, 'type'=>'number');
-                       $expected_args[3] = Array('name'=>'type','default'=>0, 
'type'=>'number');
-                       $expected_args[4] = 
Array('name'=>'app_id','default'=>$this->app_id, 'type'=>'number');
-                       $expected_args[5] = 
Array('name'=>'data','default'=>NULL, 'type'=>'any');
-                       $recieved_args = func_get_args();
-                       $args = safe_args($expected_args, 
$recieved_args,__LINE__,__FILE__);
-                       $sql = "SELECT acl_rights, acl_location FROM fm_acl2 
WHERE (acl_appid = '".$args['app_id']."' ";
-                       $sql .= " and acl_account = ".$args['account_id'];
-                       $sql .= " and acl_location = '".$args['location']."' 
and acl_type=".$args['type']." AND grantor is NULL)";
-                       $this->db->query($sql,__LINE__,__FILE__);
-                       while($this->db->next_record())
-                       {
-                               if((int)$args['rights'] == 0)
-                               {
-                                       $sql = "DELETE FROM fm_acl2";
-                               }
-                               else
-                               {
-                                       $sql = "UPDATE fm_acl2 SET acl_rights 
=".$args['rights'];
-                               }
-                               $sql .= " WHERE acl_host=".$this->host_id." AND 
acl_appid=".$args['app_id']." AND acl_account=".$args['account_id']." AND 
acl_location='".$this->db->f('acl_location')."' AND acl_type=".$args['type'] . 
' AND grantor is NULL';
-                               $this->db2->query($sql,__LINE__,__FILE__);
-
-                               
$updated_locations[]=$this->db->f('acl_location');
-                       }
-
-                       $sql = "SELECT id FROM fm_acl_location WHERE (id 
$this->like '%".$args['location']."%')";
-                       $this->db->query($sql,__LINE__,__FILE__);
-                       while($this->db->next_record())
-                       {
-
-                               $add_locations[]=$this->db->f('id');
-                       }
-
-                       if($updated_locations)
-                       {
-                               $add_locations  = array_diff($add_locations, 
$updated_locations);
-                       }
-                       $add_locations = 
explode("-",implode("-",$add_locations));
-                       if(is_array($add_locations) && count($add_locations)>0)
-                       {
-                               for ($i=0;$i<count($add_locations);$i++)
-                               {
-
-                                       for ($j=0;$j<($i+1);$j++)
-                                       {
-                                               $location       
=$add_locations[$j];
-                                       }
-
-                                       
if(strlen($location)>=strlen($args['location']))
-                                       {
-                                               if($args['rights'] != 0)
-                                               {
-                                                       $sql = "SELECT 
acl_rights FROM fm_acl2 WHERE (acl_appid = '".$args['app_id']."' ";
-                                                       $sql .= " and 
acl_account = ".$args['account_id'];
-                                                       $sql .= " and 
acl_location = '$location' and acl_type=".$args['type']." AND grantor is NULL 
)";
-                                                       
$this->db->query($sql,__LINE__,__FILE__);
-                                                       
if($this->db->num_rows() == 0)
-                                                       {
-                                                               $sql = "INSERT 
INTO fm_acl2 
(acl_host,acl_appid,acl_account,acl_location,acl_rights,acl_type,acl_data) 
VALUES 
(".$this->host_id.",".$args['app_id'].",".$args['account_id'].",'".$location."',".$args['rights'].",".$args['type'].",'".$args['data']."')";
-
-                                                               
$this->db2->query($sql,__LINE__,__FILE__);
-                                                       }
-                                                       else
-                                                       {
-                                                               $sql = "UPDATE 
fm_acl2 SET acl_rights =".$args['rights'];
-                                                               $sql .= " WHERE 
acl_host=".$this->host_id." AND acl_appid=".$args['app_id']." AND 
acl_account=".$args['account_id']." AND acl_location='".$location."' AND 
acl_type=".$args['type'] . ' AND grantor is NULL';
-                                                               
$this->db2->query($sql,__LINE__,__FILE__);
-                                                       }
-                                               }
-                                       }
-                                       unset($location);
-                               }
-                       }
-
-                       $this->rights_cache = Array();
-                       $this->masks_cache = Array();
-               }
-
-               function remove()
-               {
-                       $expected_args[0] = 
Array('name'=>'location','default'=>'##REQUIRED##', 'type'=>'alphanumeric');
-                       $expected_args[1] = 
Array('name'=>'rights','default'=>1, 'type'=>'number');
-                       $expected_args[2] = 
Array('name'=>'account_id','default'=>$this->account_id, 'type'=>'number');
-                       $expected_args[3] = Array('name'=>'type','default'=>0, 
'type'=>'number');
-                       $expected_args[4] = 
Array('name'=>'app_id','default'=>$this->app_id, 'type'=>'number');
-                       $expected_args[5] = 
Array('name'=>'data','default'=>NULL, 'type'=>'any');
-                       $recieved_args = func_get_args();
-                       $args = safe_args($expected_args, 
$recieved_args,__LINE__,__FILE__);
-
-                       $sql = "SELECT acl_rights FROM fm_acl2 WHERE (acl_appid 
= '".$args['app_id']."' ";
-                       $sql .= " and acl_account = ".$args['account_id'];
-                       $sql .= " and acl_location = '".$args['location']."' 
and acl_type=".$args['type']." and grantor is NULL)";
-                       $this->db->query($sql,__LINE__,__FILE__);
-                       if($this->db->num_rows() != 0)
-                       {
-                               $this->db->next_record();
-                               $newrights = 
$this->bit_mask((int)$this->db->f('acl_rights'),$args['rights']);
-                               if ($newrights != 0)
-                               {
-                                       $sql = "UPDATE fm_acl2 SET acl_rights 
=".$newrights;
-                               }
-                               else
-                               {
-                                       $sql = "DELETE FROM fm_acl2";
-                               }
-                               $sql .= " WHERE acl_host=".$this->host_id." AND 
acl_appid=".$args['app_id']." AND acl_account=".$args['account_id']." AND 
acl_location $this->like '%".$args['location']."%' AND acl_type=".$args['type'] 
. ' and grantor is NULL';
-                               $this->db->query($sql,__LINE__,__FILE__);
-                               $this->rights_cache = Array();
-                               $this->masks_cache = Array();
-                       }
-               }
-
-               function remove_grant()
-               {
-                       $expected_args[0] = 
Array('name'=>'location','default'=>'##REQUIRED##', 'type'=>'alphanumeric');
-                       $expected_args[1] = 
Array('name'=>'rights','default'=>1, 'type'=>'number');
-                       $expected_args[2] = 
Array('name'=>'account_id','default'=>$this->account_id, 'type'=>'number');
-                       $expected_args[3] = Array('name'=>'type','default'=>0, 
'type'=>'number');
-                       $expected_args[4] = 
Array('name'=>'grantor','default'=>NULL, 'type'=>'any');
-                       $expected_args[5] = 
Array('name'=>'app_id','default'=>$this->app_id, 'type'=>'number');
-                       $expected_args[6] = 
Array('name'=>'data','default'=>NULL, 'type'=>'any');
-                       $recieved_args = func_get_args();
-                       $args = safe_args($expected_args, 
$recieved_args,__LINE__,__FILE__);
-
-                       if($args['grantor'])
-                       {
-                               $grantor = $args['grantor'];
-                       }
-                       else
-                       {
-                               $grantor = 
$GLOBALS['phpgw_info']['user']['account_id'];
-                       }
-
-                       $sql = "SELECT acl_rights FROM fm_acl2 WHERE (acl_appid 
= '".$args['app_id']."' ";
-                       $sql .= " and acl_account = ".$args['account_id'];
-                       $sql .= " and acl_location = '".$args['location']."' 
and acl_type=".$args['type']." and grantor = $grantor)";
-                       $this->db->query($sql,__LINE__,__FILE__);
-                       if($this->db->num_rows() != 0)
-                       {
-                               $this->db->next_record();
-                               $newrights = 
$this->bit_mask((int)$this->db->f('acl_rights'),$args['rights']);
-                               if ($newrights != 0)
-                               {
-                                       $sql = "UPDATE fm_acl2 SET acl_rights 
=".$newrights;
-                               }
-                               else
-                               {
-                                       $sql = "DELETE FROM fm_acl2";
-                               }
-                               $sql .= " WHERE acl_host=".$this->host_id." AND 
acl_appid=".$args['app_id']." AND acl_account=".$args['account_id']." AND 
acl_location $this->like '%".$args['location']."%' AND acl_type=".$args['type'] 
. " and grantor = $grantor";
-                               $this->db->query($sql,__LINE__,__FILE__);
-                               $this->rights_cache = Array();
-                               $this->masks_cache = Array();
-                       }
-               }
-
-               
/*************************************************************************\
-               * Non-standard functions. Should only be used for ACL 
management needs    *
-               
\*************************************************************************/
-               function check_specific()
-               {
-                       $expected_args[0] = 
Array('name'=>'location','default'=>'##REQUIRED##', 'type'=>'alphanumeric');
-                       $expected_args[1] = 
Array('name'=>'required','default'=>1, 'type'=>'number');
-                       $expected_args[2] = 
Array('name'=>'account_id','default'=>$this->account_id, 'type'=>'number');
-                       $expected_args[3] = 
Array('name'=>'app_id','default'=>$this->app_id, 'type'=>'number');
-                       $recieved_args = func_get_args();
-                       $args = safe_args($expected_args, 
$recieved_args,__LINE__,__FILE__);
-
-                       $sql = "SELECT acl_rights,acl_type,acl_data FROM 
fm_acl2 WHERE (acl_appid = '".$args['app_id']."' ";
-                       $sql .= " and acl_account = ".$args['account_id'];
-                       $sql .= " and acl_location = '".$args['location']."' 
and acl_type=0)";
-                       $this->db->query($sql,__LINE__,__FILE__);
-                       $rights = 0;
-                       while ($this->db->next_record())
-                       {
-                               $rights = 
$this->bit_set($rights,(int)$this->db->f('acl_rights'));
-                       }
-                       return $this->bit_check($rights,$args['required']);
-               }
-
-               /* I dont feel this function will be needed, and plan to remove 
it when certain.
-               function check_location()
-               {
-                       $expected_args[0] = 
Array('name'=>'location','default'=>'##REQUIRED##', 'type'=>'alphanumeric');
-                       $expected_args[1] = 
Array('name'=>'required','default'=>1, 'type'=>'number');
-                       $expected_args[2] = 
Array('name'=>'app_id','default'=>$this->app_id, 'type'=>'number');
-                       $recieved_args = func_get_args();
-                       $args = safe_args($expected_args, 
$recieved_args,__LINE__,__FILE__);
-
-                       $sql = "SELECT acl_rights,acl_type,acl_data FROM 
fm_acl2 WHERE (acl_appid = '".$args['app_id']."' ";
-                       $sql .= " and (acl_account in 
(".$this->account_id.",".$this->memberships_sql.'))';
-                       $sql .= " and acl_location = '".$args['location']."' 
and acl_type=0)";
-                       $this->db->query($sql,__LINE__,__FILE__);
-                       $rights = 0;
-                       while ($this->db->next_record())
-                       {
-                               $rights = 
$this->bit_set($rights,(int)$this->db->f('acl_rights'));
-                       }
-                       return $this->bit_check($rights,$args['required']);
-               }
-               */
-               function get_grants($app='',$location='')
-               {
-                       if ($app=='')
-                       {
-                               $app = 
$GLOBALS['phpgw_info']['flags']['currentapp'];
-                       }
-
-                       $app_id = 
$GLOBALS['phpgw']->applications->data[$app]['id'];
-
-                       $sql = "select grantor,grantor_type, acl_rights from 
fm_acl2  where acl_appid = $app_id AND  acl_type=0  and acl_location = 
'$location' AND grantor is NOT NULL AND"
-                               . " fm_acl2.acl_account in ";
-                       $security = "(". $this->account_id ."";
-                       $myaccounts = CreateObject('phpgwapi.accounts');
-                       $my_memberships = 
$myaccounts->membership($this->account_id);
-
-//                     unset($myaccounts);
-                       @reset($my_memberships);
-                       while($my_memberships && list($key,$group) = 
each($my_memberships))
-                       {
-                               $security .= "," . $group['account_id'];
-                       }
-                       $security .= ')';
-                       $this->db2->query($sql . $security ,__LINE__,__FILE__);
-                       $rights = 0;
-                       $accounts = Array();
-                       if ($this->db2->num_rows() == 0)
-                       {
-                               
$grants[$GLOBALS['phpgw_info']['user']['account_id']] = 31;
-                               return $grants;
-                       }
-                       while ($this->db2->next_record())
-                       {
-                               $grantor = $this->db2->f('grantor');
-                               $rights = $this->db2->f('acl_rights');
-
-                               $grantor_type = $this->db2->f('grantor_type');
-
-                               if($grantor_type =='g')
-                               {
-                                       $grantor_members = 
$myaccounts->member($grantor);
-
-                                       foreach($grantor_members as $grantor)
-                                       {
-                                               
$accounts[$grantor['account_id']] = array($grantor['account_id']);
-
-                                               
if(!isset($grants[$grantor['account_id']]))
-                                               {
-                                                       
$grants[$grantor['account_id']] = 0;
-                                               }
-                                               $grants[$grantor['account_id']] 
|= $rights;
-                                               
reset($accounts[$grantor['account_id']]);
-                                       }
-                               }
-                               else
-                               {
-                                       $accounts[$grantor] = Array($grantor);
-                                       foreach($accounts[$grantor] as 
$grantors)
-                                       {
-                                               if(!isset($grants[$grantors]))
-                                               {
-                                                       $grants[$grantors] = 0;
-                                               }
-                                               $grants[$grantors] |= $rights;
-                                       }
-                                       reset($accounts[$grantor]);
-                               }
-                       }
-
-                       $grants[$GLOBALS['phpgw_info']['user']['account_id']] = 
31;
-                       return $grants;
-               }
-
-               
/*************************************************************************\
-               * Support functions                                             
          *
-               
\*************************************************************************/
-               /*!
-               @function bit_set
-               @abstract add/turn_on new bit to current value
-               */
-               function bit_set($rights, $new)
-               {
-                       return $rights |= $new;
-               }
-
-               /*!
-               @function bit_mask
-               @abstract mask/turn_off new bit from current value
-               */
-               function bit_mask($rights, $mask)
-               {
-                       return $rights &= ~$mask;
-               }
-
-               /*!
-               @function bit_check
-               @abstract check if required bit is set/turned_on in the rights
-               */
-               function bit_check($rights, $required)
-               {
-                       return ($rights & $required);
-               }
-       }
-?>
+               //      $this->db2      = $this->db;
+
+                       $this->account_id = $args['account_id'];
+
+                       $this->host_id = $args['host_id'];
+                       $this->app_id = $args['app_id'];
+
+                       if 
($GLOBALS['phpgw_info']['server']['db_type']=='pgsql')
+                       {
+                               $this->like = "ILIKE";
+                       }
+                       else
+                       {
+                               $this->like = "LIKE";
+                       }
+               }
+
+               function get_memberships ()
+               {
+                       $expected_args[0] = 
Array('name'=>'account_id','default'=>$this->account_id, 'type'=>'number');
+                       $recieved_args = func_get_args();
+                       $args = safe_args($expected_args, 
$recieved_args,__LINE__,__FILE__);
+
+                       $sql = "SELECT acl_location,acl_rights FROM fm_acl2
+                                                       WHERE ( 
acl_host='".$this->host_id."' and acl_appid = 0 and acl_account = 
".$args['account_id'].")";
+                       $this->db->query($sql,__LINE__,__FILE__);
+
+                       while ($this->db->next_record())
+                       {
+                               
if(!isset($this->memberships[$this->db->f('acl_location')]))
+                               {
+                                       
$this->memberships[$this->db->f('acl_location')] = $this->db->f('acl_rights');
+                                       $this->memberships_sql .= 
','.$this->db->f('acl_location');
+                                       
$this->get_memberships(Array('account_id'=>$this->db->f('acl_location')));
+                               }
+                       }
+               }
+
+
+               function cache_rights()
+               {
+                       $expected_args[0] = 
Array('name'=>'location','default'=>'##REQUIRED##', 'type'=>'alphanumeric');
+                       $expected_args[1] = 
Array('name'=>'app_id','default'=>$this->app_id, 'type'=>'number');
+                       $expected_args[2] = 
Array('name'=>'account_id','default'=>$this->account_id, 'type'=>'number');
+                       $expected_args[3] = 
Array('name'=>'grantor','default'=>False, 'type'=>'any');
+                       $expected_args[4] = 
Array('name'=>'no_effective','default'=>false, 'type'=>'any');
+
+                       $recieved_args = func_get_args();
+                       $args = safe_args($expected_args, 
$recieved_args,__LINE__,__FILE__);
+
+                       
if(isset($this->rights_cache[$args['app_id']][$args['account_id']][$args['location']]))
+                       {
+                               return;
+                       }
+
+                       $sql = "SELECT 
acl_rights,acl_type,acl_data,acl_location FROM fm_acl2 WHERE (acl_appid = 
'".$args['app_id']."' ";
+                       $sql .= " and (acl_account in 
(".$args['account_id']."".$this->memberships_sql.'))';
+                       $sql .= " and acl_location = '". $args['location'] . 
"'";
+
+                       if($args['grantor']>0)
+                       {
+                               $sql .= " and (grantor=" . $args['grantor'] . 
")";
+                       }
+                       else
+                       {
+                               $sql .= " and (grantor is NULL)";
+                       }
+
+                       $sql .= ') ORDER BY acl_location, acl_type DESC';
+
+                       $this->db->query($sql,__LINE__,__FILE__);
+                       while($this->db->next_record())
+                       {
+                               
if($this->rights_cache[$args['app_id']][$args['account_id']][$args['location']] 
== 0)
+                               {
+                                       
$this->rights_cache[$args['app_id']][$args['account_id']][$this->db->f('acl_location')]
 = 0;
+                               }
+
+                               
if($this->masks_cache[$args['app_id']][$args['account_id']][$args['location']] 
== 0)
+                               {
+                                       
$this->masks_cache[$args['app_id']][$args['account_id']][$this->db->f('acl_location')]
 = 0;
+                               }
+
+                               if((int)$this->db->f('acl_type') == 0)
+                               {
+                                       
$this->rights_cache[$args['app_id']][$args['account_id']][$this->db->f('acl_location')]
 = 
$this->bit_set($this->rights_cache[$args['app_id']][$args['account_id']][$this->db->f('acl_location')],(int)$this->db->f('acl_rights'));
+                               }
+                               else
+                               {
+                                       
$this->masks_cache[$args['app_id']][$args['account_id']][$this->db->f('acl_location')]
 = 
$this->bit_set($this->rights_cache[$args['app_id']][$args['account_id']][$this->db->f('acl_location')],(int)$this->db->f('acl_rights'));
+                               }
+
+                               if(!$this->db->f('acl_rights'))
+                               {
+                                       
$this->rights_cache[$args['app_id']][$args['account_id']][$args['location']]=0;
+                                       
$this->masks_cache[$args['app_id']][$args['account_id']][$args['location']]=0;
+                               }
+
+                               if(!$args['no_effective'])
+                               {
+                                       
$this->rights_cache[$args['app_id']][$args['account_id']][$this->db->f('acl_location')]
 = 
$this->bit_mask($this->rights_cache[$args['app_id']][$args['account_id']][$args['location']],
 $this->masks_cache[$args['app_id']][$args['account_id']][$args['location']]);
+                               }
+
+                       }
+               }
+
+
+               function check_grant()
+               {
+                       $expected_args[0] = 
Array('name'=>'grantor','default'=>$this->account_id, 'type'=>'number');
+                       $expected_args[1] = 
Array('name'=>'location','default'=>'##REQUIRED##', 'type'=>'alphanumeric');
+                       $expected_args[2] = 
Array('name'=>'required','default'=>1, 'type'=>'number');
+                       $expected_args[3] = 
Array('name'=>'account_id','default'=>$this->account_id, 'type'=>'number');
+                       $expected_args[4] = 
Array('name'=>'app_id','default'=>$this->app_id, 'type'=>'number');
+                       $expected_args[5] = 
Array('name'=>'no_membership','default'=>false, 'type'=>'number');
+                       $expected_args[6] = 
Array('name'=>'acl_type','default'=>0, 'type'=>'number');
+                       $expected_args[7] = 
Array('name'=>'no_effective','default'=>false, 'type'=>'any');
+                       $recieved_args = func_get_args();
+                       $args = safe_args($expected_args, 
$recieved_args,__LINE__,__FILE__);
+
+                       if(!$args['no_membership'])
+                       {
+                               $memberships = 
$GLOBALS['phpgw']->accounts->membership($args['account_id']);
+
+                               while (is_array($memberships) && 
list(,$account) = each($memberships))
+                               {
+                                       $member_of[] = $account['account_id'];
+                               }
+
+                               if($member_of)
+                               {
+                                       $this->memberships_sql = ',' . 
@implode(",",$member_of);
+                               }
+                       }
+
+                       
$this->cache_rights($args['location'],$args['app_id'],$args['account_id'],$args['grantor'],$args['no_effective']);
+                       if($args['acl_type']==0)
+                       {
+                               return 
$this->bit_check($this->rights_cache[$args['app_id']][$args['account_id']][$args['location']],$args['required']);
+                       }
+                       else
+                       {
+                               return 
$this->bit_check($this->masks_cache[$args['app_id']][$args['account_id']][$args['location']],$args['required']);
+                       }
+               }
+
+               function check()
+               {
+                       $expected_args[0] = 
Array('name'=>'location','default'=>'##REQUIRED##', 'type'=>'alphanumeric');
+                       $expected_args[1] = 
Array('name'=>'required','default'=>1, 'type'=>'number');
+                       $expected_args[2] = 
Array('name'=>'account_id','default'=>$this->account_id, 'type'=>'number');
+                       $expected_args[3] = 
Array('name'=>'app_id','default'=>$this->app_id, 'type'=>'number');
+                       $expected_args[4] = 
Array('name'=>'no_membership','default'=>false, 'type'=>'number');
+                       $expected_args[5] = 
Array('name'=>'acl_type','default'=>0, 'type'=>'number');
+                       $expected_args[6] = 
Array('name'=>'no_effective','default'=>false, 'type'=>'any');
+                       $recieved_args = func_get_args();
+                       $args = safe_args($expected_args, 
$recieved_args,__LINE__,__FILE__);
+
+                       if(!$args['no_membership'])
+                       {
+                               $memberships = 
$GLOBALS['phpgw']->accounts->membership($args['account_id']);
+
+                               while (is_array($memberships) && 
list(,$account) = each($memberships))
+                               {
+                                       $member_of[] = $account['account_id'];
+                               }
+
+                               if($member_of)
+                               {
+                                       $this->memberships_sql = ',' . 
@implode(",",$member_of);
+                               }
+                       }
+
+                       
$this->cache_rights($args['location'],$args['app_id'],$args['account_id'],False,$args['no_effective']);
+                       if($args['acl_type']==0)
+                       {
+                               return 
$this->bit_check($this->rights_cache[$args['app_id']][$args['account_id']][$args['location']],$args['required']);
+                       }
+                       else
+                       {
+                               return 
$this->bit_check($this->masks_cache[$args['app_id']][$args['account_id']][$args['location']],$args['required']);
+                       }
+               }
+
+               /*!
+               @function add
+               @abstract Adds ACL record to $acl->data
+               @discussion Adds ACL record to $acl->data. <br>
+               Syntax: array add() <br>
+               Example1: acl->add();
+               @param $appname default False derives value from 
$phpgw_info['flags']['currentapp']
+               @param $location location
+               @param $rights rights
+               */
+               function add()
+               {
+                       $expected_args[0] = 
Array('name'=>'location','default'=>'##REQUIRED##', 'type'=>'alphanumeric');
+                       $expected_args[1] = 
Array('name'=>'rights','default'=>1, 'type'=>'number');
+                       $expected_args[2] = 
Array('name'=>'account_id','default'=>$this->account_id, 'type'=>'number');
+                       $expected_args[3] = Array('name'=>'type','default'=>0, 
'type'=>'number');
+                       $expected_args[4] = 
Array('name'=>'app_id','default'=>$this->app_id, 'type'=>'number');
+                       $expected_args[5] = 
Array('name'=>'data','default'=>NULL, 'type'=>'any');
+                       $recieved_args = func_get_args();
+                       $args = safe_args($expected_args, 
$recieved_args,__LINE__,__FILE__);
+
+                       $sql = "SELECT acl_rights FROM fm_acl2 WHERE (acl_appid 
= '".$args['app_id']."' ";
+                       $sql .= " and acl_account = ".$args['account_id'];
+                       $sql .= " and acl_location = '".$args['location']."' 
and acl_type=".$args['type']." and grantor is NULL)";
+                       $this->db->query($sql,__LINE__,__FILE__);
+                       if($this->db->num_rows() != 0)
+                       {
+                               $this->db->next_record();
+                               $newrights = $this->bit_set($args['rights'], 
(int)$this->db->f('acl_rights'));
+                               $sql = "UPDATE fm_acl2 SET acl_rights 
=".$newrights;
+                               $sql .= " WHERE acl_host=".$this->host_id." AND 
acl_appid=".$args['app_id']." AND acl_account=".$args['account_id']." AND 
acl_location='".$args['location']."' AND acl_type=".$args['type'];
+                       }
+                       else
+                       {
+                               $sql = "INSERT INTO fm_acl2 
(acl_host,acl_appid,acl_account,acl_location,acl_rights,acl_type,acl_data) 
VALUES 
(".$this->host_id.",".$args['app_id'].",".$args['account_id'].",'".$args['location']."',".$args['rights'].",".$args['type'].",'".$args['data']."')";
+                       }
+                       $this->db->query($sql,__LINE__,__FILE__);
+                       $this->rights_cache = Array();
+                       $this->masks_cache = Array();
+               }
+
+               function set_grant()  //inheritance is saved to the database
+               {
+                       $expected_args[0] = 
Array('name'=>'location','default'=>'##REQUIRED##', 'type'=>'alphanumeric');
+                       $expected_args[1] = 
Array('name'=>'rights','default'=>1, 'type'=>'number');
+                       $expected_args[2] = 
Array('name'=>'account_id','default'=>$this->account_id, 'type'=>'number');
+                       $expected_args[3] = Array('name'=>'type','default'=>0, 
'type'=>'number');
+                       $expected_args[4] = 
Array('name'=>'grantor','default'=>NULL, 'type'=>'any');
+                       $expected_args[5] = 
Array('name'=>'app_id','default'=>$this->app_id, 'type'=>'number');
+                       $expected_args[6] = 
Array('name'=>'data','default'=>NULL, 'type'=>'any');
+                       $recieved_args = func_get_args();
+                       $args = safe_args($expected_args, 
$recieved_args,__LINE__,__FILE__);
+                       if($args['grantor'])
+                       {
+                               $grantor = $args['grantor'];
+                               $grantor_type = 'g';
+                       }
+                       else
+                       {
+                               $grantor = 
$GLOBALS['phpgw_info']['user']['account_id'];
+                       }
+
+                       $sql = "SELECT acl_rights, acl_location FROM fm_acl2 
WHERE (acl_appid = '".$args['app_id']."' ";
+                       $sql .= " and acl_account = ".$args['account_id'];
+                       $sql .= " and acl_location = '".$args['location']."' 
and acl_type=".$args['type']." and grantor = $grantor)";
+                       $this->db->query($sql,__LINE__,__FILE__);
+                       while($this->db->next_record())
+                       {
+                               if((int)$args['rights'] == 0)
+                               {
+                                       $sql = "DELETE FROM fm_acl2";
+                               }
+                               else
+                               {
+                                       $sql = "UPDATE fm_acl2 SET acl_rights 
=".$args['rights'];
+                               }
+                               $sql .= " WHERE acl_host=".$this->host_id." AND 
acl_appid=".$args['app_id']." AND acl_account=".$args['account_id']." AND 
acl_location='".$this->db->f('acl_location')."' AND acl_type=".$args['type'] . 
" AND grantor = $grantor";
+                               $this->db2->query($sql,__LINE__,__FILE__);
+
+                               
$updated_locations[]=$this->db->f('acl_location');
+                       }
+
+                       $sql = "SELECT id FROM fm_acl_location WHERE (id 
$this->like '%".$args['location']."%') AND allow_grant=1";
+                       $this->db->query($sql,__LINE__,__FILE__);
+                       while($this->db->next_record())
+                       {
+
+                               $add_locations[]=$this->db->f('id');
+                       }
+
+                       if($updated_locations)
+                       {
+                               $add_locations  = array_diff($add_locations, 
$updated_locations);
+                       }
+                       $add_locations = 
explode("-",implode("-",$add_locations));
+                       if(is_array($add_locations) && count($add_locations)>0)
+                       {
+                               for ($i=0;$i<count($add_locations);$i++)
+                               {
+
+                                       for ($j=0;$j<($i+1);$j++)
+                                       {
+                                               $location       
=$add_locations[$j];
+                                       }
+
+                                       
if(strlen($location)>=strlen($args['location']))
+                                       {
+                                               if($args['rights'] != 0)
+                                               {
+                                                       $sql = "SELECT 
acl_rights FROM fm_acl2 WHERE (acl_appid = '".$args['app_id']."' ";
+                                                       $sql .= " and 
acl_account = ".$args['account_id'];
+                                                       $sql .= " and 
acl_location = '$location' and acl_type=".$args['type']. " AND grantor = 
$grantor)";
+                                                       
$this->db->query($sql,__LINE__,__FILE__);
+
+                                                       
if($this->db->num_rows() == 0)
+                                                       {
+                                                               $sql = "INSERT 
INTO fm_acl2 
(acl_host,acl_appid,acl_account,acl_location,acl_rights,acl_type,acl_data,grantor,grantor_type)
 VALUES 
(".$this->host_id.",".$args['app_id'].",".$args['account_id'].",'".$location."',".$args['rights'].",".$args['type'].",'".$args['data']."',$grantor,'$grantor_type')";
+
+                                                               
$this->db2->query($sql,__LINE__,__FILE__);
+                                                       }
+                                                       else
+                                                       {
+                                                               $sql = "UPDATE 
fm_acl2 SET acl_rights =".$args['rights'];
+                                                               $sql .= " WHERE 
acl_host=".$this->host_id." AND acl_appid=".$args['app_id']." AND 
acl_account=".$args['account_id']." AND acl_location='".$location."' AND 
acl_type=".$args['type']  . " AND grantor = $grantor";
+                                                               
$this->db2->query($sql,__LINE__,__FILE__);
+                                                       }
+                                               }
+                                       }
+                                       unset($location);
+                               }
+                       }
+
+                       $this->rights_cache = Array();
+                       $this->masks_cache = Array();
+               }
+
+               function set()  //inheritance is saved to the database
+               {
+                       $expected_args[0] = 
Array('name'=>'location','default'=>'##REQUIRED##', 'type'=>'alphanumeric');
+                       $expected_args[1] = 
Array('name'=>'rights','default'=>1, 'type'=>'number');
+                       $expected_args[2] = 
Array('name'=>'account_id','default'=>$this->account_id, 'type'=>'number');
+                       $expected_args[3] = Array('name'=>'type','default'=>0, 
'type'=>'number');
+                       $expected_args[4] = 
Array('name'=>'app_id','default'=>$this->app_id, 'type'=>'number');
+                       $expected_args[5] = 
Array('name'=>'data','default'=>NULL, 'type'=>'any');
+                       $recieved_args = func_get_args();
+                       $args = safe_args($expected_args, 
$recieved_args,__LINE__,__FILE__);
+                       $sql = "SELECT acl_rights, acl_location FROM fm_acl2 
WHERE (acl_appid = '".$args['app_id']."' ";
+                       $sql .= " and acl_account = ".$args['account_id'];
+                       $sql .= " and acl_location = '".$args['location']."' 
and acl_type=".$args['type']." AND grantor is NULL)";
+                       $this->db->query($sql,__LINE__,__FILE__);
+                       while($this->db->next_record())
+                       {
+                               if((int)$args['rights'] == 0)
+                               {
+                                       $sql = "DELETE FROM fm_acl2";
+                               }
+                               else
+                               {
+                                       $sql = "UPDATE fm_acl2 SET acl_rights 
=".$args['rights'];
+                               }
+                               $sql .= " WHERE acl_host=".$this->host_id." AND 
acl_appid=".$args['app_id']." AND acl_account=".$args['account_id']." AND 
acl_location='".$this->db->f('acl_location')."' AND acl_type=".$args['type'] . 
' AND grantor is NULL';
+                               $this->db2->query($sql,__LINE__,__FILE__);
+
+                               
$updated_locations[]=$this->db->f('acl_location');
+                       }
+
+                       $sql = "SELECT id FROM fm_acl_location WHERE (id 
$this->like '%".$args['location']."%')";
+                       $this->db->query($sql,__LINE__,__FILE__);
+                       while($this->db->next_record())
+                       {
+
+                               $add_locations[]=$this->db->f('id');
+                       }
+
+                       if($updated_locations)
+                       {
+                               $add_locations  = array_diff($add_locations, 
$updated_locations);
+                       }
+                       $add_locations = 
explode("-",implode("-",$add_locations));
+                       if(is_array($add_locations) && count($add_locations)>0)
+                       {
+                               for ($i=0;$i<count($add_locations);$i++)
+                               {
+
+                                       for ($j=0;$j<($i+1);$j++)
+                                       {
+                                               $location       
=$add_locations[$j];
+                                       }
+
+                                       
if(strlen($location)>=strlen($args['location']))
+                                       {
+                                               if($args['rights'] != 0)
+                                               {
+                                                       $sql = "SELECT 
acl_rights FROM fm_acl2 WHERE (acl_appid = '".$args['app_id']."' ";
+                                                       $sql .= " and 
acl_account = ".$args['account_id'];
+                                                       $sql .= " and 
acl_location = '$location' and acl_type=".$args['type']." AND grantor is NULL 
)";
+                                                       
$this->db->query($sql,__LINE__,__FILE__);
+                                                       
if($this->db->num_rows() == 0)
+                                                       {
+                                                               $sql = "INSERT 
INTO fm_acl2 
(acl_host,acl_appid,acl_account,acl_location,acl_rights,acl_type,acl_data) 
VALUES 
(".$this->host_id.",".$args['app_id'].",".$args['account_id'].",'".$location."',".$args['rights'].",".$args['type'].",'".$args['data']."')";
+
+                                                               
$this->db2->query($sql,__LINE__,__FILE__);
+                                                       }
+                                                       else
+                                                       {
+                                                               $sql = "UPDATE 
fm_acl2 SET acl_rights =".$args['rights'];
+                                                               $sql .= " WHERE 
acl_host=".$this->host_id." AND acl_appid=".$args['app_id']." AND 
acl_account=".$args['account_id']." AND acl_location='".$location."' AND 
acl_type=".$args['type'] . ' AND grantor is NULL';
+                                                               
$this->db2->query($sql,__LINE__,__FILE__);
+                                                       }
+                                               }
+                                       }
+                                       unset($location);
+                               }
+                       }
+
+                       $this->rights_cache = Array();
+                       $this->masks_cache = Array();
+               }
+
+               function remove()
+               {
+                       $expected_args[0] = 
Array('name'=>'location','default'=>'##REQUIRED##', 'type'=>'alphanumeric');
+                       $expected_args[1] = 
Array('name'=>'rights','default'=>1, 'type'=>'number');
+                       $expected_args[2] = 
Array('name'=>'account_id','default'=>$this->account_id, 'type'=>'number');
+                       $expected_args[3] = Array('name'=>'type','default'=>0, 
'type'=>'number');
+                       $expected_args[4] = 
Array('name'=>'app_id','default'=>$this->app_id, 'type'=>'number');
+                       $expected_args[5] = 
Array('name'=>'data','default'=>NULL, 'type'=>'any');
+                       $recieved_args = func_get_args();
+                       $args = safe_args($expected_args, 
$recieved_args,__LINE__,__FILE__);
+
+                       $sql = "SELECT acl_rights FROM fm_acl2 WHERE (acl_appid 
= '".$args['app_id']."' ";
+                       $sql .= " and acl_account = ".$args['account_id'];
+                       $sql .= " and acl_location = '".$args['location']."' 
and acl_type=".$args['type']." and grantor is NULL)";
+                       $this->db->query($sql,__LINE__,__FILE__);
+                       if($this->db->num_rows() != 0)
+                       {
+                               $this->db->next_record();
+                               $newrights = 
$this->bit_mask((int)$this->db->f('acl_rights'),$args['rights']);
+                               if ($newrights != 0)
+                               {
+                                       $sql = "UPDATE fm_acl2 SET acl_rights 
=".$newrights;
+                               }
+                               else
+                               {
+                                       $sql = "DELETE FROM fm_acl2";
+                               }
+                               $sql .= " WHERE acl_host=".$this->host_id." AND 
acl_appid=".$args['app_id']." AND acl_account=".$args['account_id']." AND 
acl_location $this->like '%".$args['location']."%' AND acl_type=".$args['type'] 
. ' and grantor is NULL';
+                               $this->db->query($sql,__LINE__,__FILE__);
+                               $this->rights_cache = Array();
+                               $this->masks_cache = Array();
+                       }
+               }
+
+               function remove_grant()
+               {
+                       $expected_args[0] = 
Array('name'=>'location','default'=>'##REQUIRED##', 'type'=>'alphanumeric');
+                       $expected_args[1] = 
Array('name'=>'rights','default'=>1, 'type'=>'number');
+                       $expected_args[2] = 
Array('name'=>'account_id','default'=>$this->account_id, 'type'=>'number');
+                       $expected_args[3] = Array('name'=>'type','default'=>0, 
'type'=>'number');
+                       $expected_args[4] = 
Array('name'=>'grantor','default'=>NULL, 'type'=>'any');
+                       $expected_args[5] = 
Array('name'=>'app_id','default'=>$this->app_id, 'type'=>'number');
+                       $expected_args[6] = 
Array('name'=>'data','default'=>NULL, 'type'=>'any');
+                       $recieved_args = func_get_args();
+                       $args = safe_args($expected_args, 
$recieved_args,__LINE__,__FILE__);
+
+                       if($args['grantor'])
+                       {
+                               $grantor = $args['grantor'];
+                       }
+                       else
+                       {
+                               $grantor = 
$GLOBALS['phpgw_info']['user']['account_id'];
+                       }
+
+                       $sql = "SELECT acl_rights FROM fm_acl2 WHERE (acl_appid 
= '".$args['app_id']."' ";
+                       $sql .= " and acl_account = ".$args['account_id'];
+                       $sql .= " and acl_location = '".$args['location']."' 
and acl_type=".$args['type']." and grantor = $grantor)";
+                       $this->db->query($sql,__LINE__,__FILE__);
+                       if($this->db->num_rows() != 0)
+                       {
+                               $this->db->next_record();
+                               $newrights = 
$this->bit_mask((int)$this->db->f('acl_rights'),$args['rights']);
+                               if ($newrights != 0)
+                               {
+                                       $sql = "UPDATE fm_acl2 SET acl_rights 
=".$newrights;
+                               }
+                               else
+                               {
+                                       $sql = "DELETE FROM fm_acl2";
+                               }
+                               $sql .= " WHERE acl_host=".$this->host_id." AND 
acl_appid=".$args['app_id']." AND acl_account=".$args['account_id']." AND 
acl_location $this->like '%".$args['location']."%' AND acl_type=".$args['type'] 
. " and grantor = $grantor";
+                               $this->db->query($sql,__LINE__,__FILE__);
+                               $this->rights_cache = Array();
+                               $this->masks_cache = Array();
+                       }
+               }
+
+               
/*************************************************************************\
+               * Non-standard functions. Should only be used for ACL 
management needs    *
+               
\*************************************************************************/
+               function check_specific()
+               {
+                       $expected_args[0] = 
Array('name'=>'location','default'=>'##REQUIRED##', 'type'=>'alphanumeric');
+                       $expected_args[1] = 
Array('name'=>'required','default'=>1, 'type'=>'number');
+                       $expected_args[2] = 
Array('name'=>'account_id','default'=>$this->account_id, 'type'=>'number');
+                       $expected_args[3] = 
Array('name'=>'app_id','default'=>$this->app_id, 'type'=>'number');
+                       $recieved_args = func_get_args();
+                       $args = safe_args($expected_args, 
$recieved_args,__LINE__,__FILE__);
+
+                       $sql = "SELECT acl_rights,acl_type,acl_data FROM 
fm_acl2 WHERE (acl_appid = '".$args['app_id']."' ";
+                       $sql .= " and acl_account = ".$args['account_id'];
+                       $sql .= " and acl_location = '".$args['location']."' 
and acl_type=0)";
+                       $this->db->query($sql,__LINE__,__FILE__);
+                       $rights = 0;
+                       while ($this->db->next_record())
+                       {
+                               $rights = 
$this->bit_set($rights,(int)$this->db->f('acl_rights'));
+                       }
+                       return $this->bit_check($rights,$args['required']);
+               }
+
+               /* I dont feel this function will be needed, and plan to remove 
it when certain.
+               function check_location()
+               {
+                       $expected_args[0] = 
Array('name'=>'location','default'=>'##REQUIRED##', 'type'=>'alphanumeric');
+                       $expected_args[1] = 
Array('name'=>'required','default'=>1, 'type'=>'number');
+                       $expected_args[2] = 
Array('name'=>'app_id','default'=>$this->app_id, 'type'=>'number');
+                       $recieved_args = func_get_args();
+                       $args = safe_args($expected_args, 
$recieved_args,__LINE__,__FILE__);
+
+                       $sql = "SELECT acl_rights,acl_type,acl_data FROM 
fm_acl2 WHERE (acl_appid = '".$args['app_id']."' ";
+                       $sql .= " and (acl_account in 
(".$this->account_id.",".$this->memberships_sql.'))';
+                       $sql .= " and acl_location = '".$args['location']."' 
and acl_type=0)";
+                       $this->db->query($sql,__LINE__,__FILE__);
+                       $rights = 0;
+                       while ($this->db->next_record())
+                       {
+                               $rights = 
$this->bit_set($rights,(int)$this->db->f('acl_rights'));
+                       }
+                       return $this->bit_check($rights,$args['required']);
+               }
+               */
+               function get_grants($app='',$location='')
+               {
+                       if ($app=='')
+                       {
+                               $app = 
$GLOBALS['phpgw_info']['flags']['currentapp'];
+                       }
+
+                       $app_id = 
$GLOBALS['phpgw']->applications->data[$app]['id'];
+
+                       $sql = "select grantor,grantor_type, acl_rights from 
fm_acl2  where acl_appid = $app_id AND  acl_type=0  and acl_location = 
'$location' AND grantor is NOT NULL AND"
+                               . " fm_acl2.acl_account in ";
+                       $security = "(". $this->account_id ."";
+                       $myaccounts = CreateObject('phpgwapi.accounts');
+                       $my_memberships = 
$myaccounts->membership($this->account_id);
+
+//                     unset($myaccounts);
+                       @reset($my_memberships);
+                       while($my_memberships && list($key,$group) = 
each($my_memberships))
+                       {
+                               $security .= "," . $group['account_id'];
+                       }
+                       $security .= ')';
+                       $this->db2->query($sql . $security ,__LINE__,__FILE__);
+                       $rights = 0;
+                       $accounts = Array();
+                       if ($this->db2->num_rows() == 0)
+                       {
+                               
$grants[$GLOBALS['phpgw_info']['user']['account_id']] = 31;
+                               return $grants;
+                       }
+                       while ($this->db2->next_record())
+                       {
+                               $grantor = $this->db2->f('grantor');
+                               $rights = $this->db2->f('acl_rights');
+
+                               $grantor_type = $this->db2->f('grantor_type');
+
+                               if($grantor_type =='g')
+                               {
+                                       $grantor_members = 
$myaccounts->member($grantor);
+
+                                       foreach($grantor_members as $grantor)
+                                       {
+                                               
$accounts[$grantor['account_id']] = array($grantor['account_id']);
+
+                                               
if(!isset($grants[$grantor['account_id']]))
+                                               {
+                                                       
$grants[$grantor['account_id']] = 0;
+                                               }
+                                               $grants[$grantor['account_id']] 
|= $rights;
+                                               
reset($accounts[$grantor['account_id']]);
+                                       }
+                               }
+                               else
+                               {
+                                       $accounts[$grantor] = Array($grantor);
+                                       foreach($accounts[$grantor] as 
$grantors)
+                                       {
+                                               if(!isset($grants[$grantors]))
+                                               {
+                                                       $grants[$grantors] = 0;
+                                               }
+                                               $grants[$grantors] |= $rights;
+                                       }
+                                       reset($accounts[$grantor]);
+                               }
+                       }
+
+                       $grants[$GLOBALS['phpgw_info']['user']['account_id']] = 
31;
+                       return $grants;
+               }
+
+               
/*************************************************************************\
+               * Support functions                                             
          *
+               
\*************************************************************************/
+               /*!
+               @function bit_set
+               @abstract add/turn_on new bit to current value
+               */
+               function bit_set($rights, $new)
+               {
+                       return $rights |= $new;
+               }
+
+               /*!
+               @function bit_mask
+               @abstract mask/turn_off new bit from current value
+               */
+               function bit_mask($rights, $mask)
+               {
+                       return $rights &= ~$mask;
+               }
+
+               /*!
+               @function bit_check
+               @abstract check if required bit is set/turned_on in the rights
+               */
+               function bit_check($rights, $required)
+               {
+                       return ($rights & $required);
+               }
+       }
+?>
Index: property/inc/class.boadmin_entity.inc.php
diff -u property/inc/class.boadmin_entity.inc.php:1.9 
property/inc/class.boadmin_entity.inc.php:1.10
--- property/inc/class.boadmin_entity.inc.php:1.9       Fri Jan 27 14:05:43 2006
+++ property/inc/class.boadmin_entity.inc.php   Mon Jan 30 22:14:19 2006
@@ -1,475 +1,475 @@
-<?php

-       /**

-       * phpGroupWare - property: a Facilities Management System.

-       *

-       * @author Sigurd Nes <address@hidden>

-       * @copyright Copyright (C) 2003-2005 Free Software Foundation, Inc. 
http://www.fsf.org/

-       * @license http://www.gnu.org/licenses/gpl.html GNU General Public 
License

-       * @internal Development of this application was funded by 
http://www.bergen.kommune.no/bbb_/ekstern/

-       * @package property

-       * @subpackage admin

-       * @version $Id: class.boadmin_entity.inc.php,v 1.9 2006/01/27 14:05:43 
sigurdne Exp $

-       */

-

-       /**

-        * Description

-        * @package property

-        */

-

-       class boadmin_entity

-       {

-               var $start;

-               var $query;

-               var $filter;

-               var $sort;

-               var $order;

-               var $cat_id;

-               var $entity_id;

-

-               var $public_functions = array

-               (

-                       'read'                          => True,

-                       'read_single'           => True,

-                       'save'                          => True,

-                       'delete'                        => True,

-                       'check_perms'           => True

-               );

-

-               var $soap_functions = array(

-                       'list' => array(

-                               'in'  => 
array('int','int','struct','string','int'),

-                               'out' => array('array')

-                       ),

-                       'read' => array(

-                               'in'  => array('int','struct'),

-                               'out' => array('array')

-                       ),

-                       'save' => array(

-                               'in'  => array('int','struct'),

-                               'out' => array()

-                       ),

-                       'delete' => array(

-                               'in'  => array('int','struct'),

-                               'out' => array()

-                       )

-               );

-

-               function boadmin_entity($session=False)

-               {

-                       $this->currentapp       = 
$GLOBALS['phpgw_info']['flags']['currentapp'];

-                       $this->so               = 
CreateObject($this->currentapp.'.soadmin_entity');

-                       $this->bocommon = 
CreateObject($this->currentapp.'.bocommon');

-

-                       if ($session)

-                       {

-                               $this->read_sessiondata();

-                               $this->use_session = True;

-                       }

-

-                       $start  = get_var('start',array('POST','GET'));

-                       $query  = get_var('query',array('POST','GET'));

-                       $sort   = get_var('sort',array('POST','GET'));

-                       $order  = get_var('order',array('POST','GET'));

-                       $filter = get_var('filter',array('POST','GET'));

-                       $cat_id = get_var('cat_id',array('POST','GET'));

-                       $allrows        = 
get_var('allrows',array('POST','GET'));

-                       $entity_id      = 
get_var('entity_id',array('POST','GET'));

-

-                       if ($start)

-                       {

-                               $this->start=$start;

-                       }

-                       else

-                       {

-                               $this->start=0;

-                       }

-

-                       if(isset($query))

-                       {

-                               $this->query = $query;

-                       }

-                       if(!empty($filter))

-                       {

-                               $this->filter = $filter;

-                       }

-                       if(isset($sort))

-                       {

-                               $this->sort = $sort;

-                       }

-                       if(isset($order))

-                       {

-                               $this->order = $order;

-                       }

-                       if(isset($cat_id))

-                       {

-                               $this->cat_id = $cat_id;

-                       }

-                       if(isset($entity_id))

-                       {

-                               $this->entity_id = $entity_id;

-                       }

-                       if(isset($allrows))

-                       {

-                               $this->allrows = $allrows;

-                       }

-               }

-

-

-               function save_sessiondata($data)

-               {

-                       if ($this->use_session)

-                       {

-                               
$GLOBALS['phpgw']->session->appsession('session_data','standard_e',$data);

-                       }

-               }

-

-               function read_sessiondata()

-               {

-                       $data = 
$GLOBALS['phpgw']->session->appsession('session_data','standard_e');

-

-

-                       $this->start    = $data['start'];

-                       $this->query    = $data['query'];

-                       $this->filter   = $data['filter'];

-                       $this->sort             = $data['sort'];

-                       $this->order    = $data['order'];

-                       $this->cat_id   = $data['cat_id'];

-                       $this->entity_id        = $data['entity_id'];

-                       $this->allrows  = $data['allrows'];

-               }

-

-               function reset_fm_cache()

-               {

-                       $this->so->reset_fm_cache();

-               }

-

-

-               function get_location_level_list($selected='')

-               {

-

-                       $soadmin_location       = 
CreateObject($this->currentapp.'.soadmin_location');

-                       $location_types         = 
$soadmin_location->select_location_type();

-                       $max_location_type=count($location_types);

-

-                       for ($i=1; $i<=$max_location_type; $i++)

-                       {

-                               $location[$i][id] = $i;

-                               $location[$i][name] = $i . '-' . 
$location_types[($i-1)]['name'];

-                       }

-

-                       return 
$this->bocommon->select_list($selected,$location);

-

-               }

-

-               function get_entity_list($selected='')

-               {

-                       $list = $this->so->read(array('allrows'=>True));

-                       return 
$this->bocommon->select_multi_list($selected,$list);

-               }

-

-               function get_entity_list_2($selected='')

-               {

-                       $list[0]['id']='project';

-                       $list[0]['name']='project';

-                       $list[1]['id']='ticket';

-                       $list[1]['name']='ticket';

-                       $list[2]['id']='document';

-                       $list[2]['name']='document';

-                       $list[3]['id']='request';

-                       $list[3]['name']='request';

-                       $list[4]['id']='investment';

-                       $list[4]['name']='investment';

-                       $list[5]['id']='s_agreement';

-                       $list[5]['name']='service agreement';

-                       return 
$this->bocommon->select_multi_list($selected,$list);

-               }

-

-               function get_entity_list_3($selected='')

-               {

-                       $list[0]['id']='ticket';

-                       $list[0]['name']='ticket';

-                       $list[1]['id']='request';

-                       $list[1]['name']='request';

-                       return 
$this->bocommon->select_multi_list($selected,$list);

-               }

-

-               function read()

-               {

-                       $entity = $this->so->read(array('start' => 
$this->start,'query' => $this->query,'sort' => $this->sort,'order' => 
$this->order,'allrows'=>$this->allrows));

-

-                       $this->total_records = $this->so->total_records;

-                       return $entity;

-               }

-

-               function read_category($entity_id)

-               {

-                       $category = $this->so->read_category(array('start' => 
$this->start,'query' => $this->query,'sort' => $this->sort,

-                               'order' => 
$this->order,'allrows'=>$this->allrows,'entity_id'=>$entity_id));

-

-                       $this->total_records = $this->so->total_records;

-

-                       return $category;

-               }

-

-               function read_status()

-               {

-                       $status = $this->so->read_status(array('start' => 
$this->start,'query' => $this->query,'sort' => $this->sort,

-                               'order' => 
$this->order,'allrows'=>$this->allrows,'entity_id'=>$this->entity_id,'cat_id'=>$this->cat_id));

-

-                       $this->total_records = $this->so->total_records;

-

-                       return $status;

-               }

-

-               function read_config()

-               {

-                       $standard = $this->so->read_config(array('start' => 
$this->start,'query' => $this->query,'sort' => $this->sort,'order' => 
$this->order));

-

-                       $this->total_records = $this->so->total_records;

-

-

-                       return $standard;

-               }

-

-               function read_config_single($column_name)

-               {

-                       return $this->so->read_config_single($column_name);

-               }

-

-               function read_single($id)

-               {

-                       return $this->so->read_single($id);

-               }

-

-               function read_single_category($entity_id,$cat_id)

-               {

-                       return 
$this->so->read_single_category($entity_id,$cat_id);

-               }

-

-               function read_single_status($id)

-               {

-                       return 
$this->so->read_single_status($this->entity_id,$this->cat_id,$id);

-               }

-

-               function save($values,$action='')

-               {

-                       if ($action=='edit')

-                       {

-                               if ($values['id'] != '')

-                               {

-                                       $receipt = 
$this->so->edit_entity($values);

-                               }

-                       }

-                       else

-                       {

-                               $receipt = $this->so->add_entity($values);

-                       }

-                       return $receipt;

-               }

-

-               function save_category($values,$action='')

-               {

-                       if ($action=='edit')

-                       {

-                               if ($values['id'] != '')

-                               {

-                                       $receipt = 
$this->so->edit_category($values);

-                               }

-                       }

-                       else

-                       {

-                               $receipt = $this->so->add_category($values);

-                       }

-                       return $receipt;

-               }

-

-               function save_status($values,$action='')

-               {

-                       if ($action=='edit')

-                       {

-                               if ($values['id'] != '')

-                               {

-                                       $receipt = 
$this->so->edit_status($values,$this->entity_id,$this->cat_id);

-                               }

-                       }

-                       else

-                       {

-                               $receipt = 
$this->so->add_status($values,$this->entity_id,$this->cat_id);

-                       }

-                       return $receipt;

-               }

-

-               function 
delete($cat_id='',$entity_id='',$attrib_id='',$status_id='',$acl_location='',$custom_function_id='')

-               {

-                       if(!$status_id && !$attrib_id && !$cat_id && $entity_id 
&& !$custom_function_id):

-                       {

-                               $this->so->delete_entity($entity_id);

-                       }

-                       elseif(!$status_id && !$attrib_id && $cat_id && 
$entity_id && !$custom_function_id):

-                       {

-                               $this->so->delete_category($cat_id,$entity_id);

-                       }

-                       elseif(!$status_id && $attrib_id && $cat_id && 
$entity_id && !$custom_function_id):

-                       {

-                               
$this->so->delete_attrib($cat_id,$entity_id,$attrib_id);

-                       }

-                       elseif($status_id && !$attrib_id && $cat_id && 
$entity_id && !$custom_function_id):

-                       {

-                               
$this->so->delete_status($cat_id,$entity_id,$status_id);

-                       }

-                       elseif(!$status_id && $custom_function_id && 
$acl_location):

-                       {

-                               
$this->so->delete_custom_function($acl_location,$custom_function_id);

-                       }

-                       endif;

-

-

-               }

-

-               function read_attrib($entity_id='',$cat_id='',$allrows='')

-               {

-                       if($allrows)

-                       {

-                               $this->allrows = $allrows;

-                       }

-

-                       $attrib = $this->so->read_attrib(array('start' => 
$this->start,'query' => $this->query,'sort' => $this->sort,'order' => 
$this->order,

-                                                                               
        'cat_id' => $cat_id,'entity_id' => 
$entity_id,'allrows'=>$this->allrows));

-

-                       for ($i=0; $i<count($attrib); $i++)

-                       {

-                               $attrib[$i]['datatype'] = 
$this->bocommon->translate_datatype($attrib[$i]['datatype']);

-                       }

-

-                       $this->total_records = $this->so->total_records;

-

-                       return $attrib;

-               }

-

-               function read_single_attrib($entity_id,$cat_id,$id)

-               {

-                       return 
$this->so->read_single_attrib($entity_id,$cat_id,$id);

-               }

-

-               function resort_attrib($id,$resort)

-               {

-                       
$this->so->resort_attrib(array('resort'=>$resort,'entity_id' => 
$this->entity_id,'cat_id' => $this->cat_id,'id'=>$id));

-               }

-

-               function save_attrib($attrib,$action='')

-               {

-                       if ($action=='edit')

-                       {

-                               if ($attrib['id'] != '')

-                               {

-

-                                       $receipt = 
$this->so->edit_attrib($attrib);

-                               }

-                       }

-                       else

-                       {

-                               $receipt = $this->so->add_attrib($attrib);

-                       }

-                       return $receipt;

-               }

-

-               function save_config($values='',$column_name='')

-               {

-                               return 
$this->so->save_config($values,$column_name);

-               }

-

-               function 
read_custom_function($entity_id='',$cat_id='',$allrows='', $acl_location='')

-               {

-                       if($allrows)

-                       {

-                               $this->allrows = $allrows;

-                       }

-

-                       if (!$acl_location && $entity_id && $cat_id)

-                       {

-                               $acl_location = '.entity.' . $entity_id . '.' . 
$cat_id;

-                       }

-

-                       $custom_function = 
$this->so->read_custom_function(array('start' => $this->start,'query' => 
$this->query,'sort' => $this->sort,'order' => $this->order,

-                                                                               
        'acl_location' => $acl_location,'allrows'=>$this->allrows));

-

-                       $this->total_records = $this->so->total_records;

-

-                       return $custom_function;

-               }

-

-               function resort_custom_function($id,$resort)

-               {

-                       
$this->so->resort_custom_function(array('resort'=>$resort,'entity_id' => 
$this->entity_id,'cat_id' => $this->cat_id,'id'=>$id));

-               }

-

-               function save_custom_function($custom_function,$action='')

-               {

-                       if ($action=='edit')

-                       {

-                               if ($custom_function['id'] != '')

-                               {

-

-                                       $receipt = 
$this->so->edit_custom_function($custom_function);

-                               }

-                       }

-                       else

-                       {

-                               $receipt = 
$this->so->add_custom_function($custom_function);

-                       }

-                       return $receipt;

-               }

-

-               function select_custom_function($selected='')

-               {

-

-                       $dir_handle = @opendir(PHPGW_APP_INC . SEP . 'custom');

-                       $i=0; $myfilearray = '';

-                       while ($file = readdir($dir_handle))

-                       {

-                               if ((substr($file, 0, 1) != '.') && 
is_file(PHPGW_APP_INC . SEP . 'custom' . SEP . $file) )

-                               {

-                                       $myfilearray[$i] = $file;

-                                       $i++;

-                               }

-                       }

-                       closedir($dir_handle);

-                       sort($myfilearray);

-

-                       for ($i=0;$i<count($myfilearray);$i++)

-                       {

-                               $fname = ereg_replace('_',' ',$myfilearray[$i]);

-                               $sel_file = '';

-                               if ($myfilearray[$i]==$selected)

-                               {

-                                       $sel_file = 'selected';

-                               }

-

-                               $file_list[] = array

-                               (

-                                       'id'            => $myfilearray[$i],

-                                       'name'          => $fname,

-                                       'selected'      => $sel_file

-                               );

-                       }

-

-                       for ($i=0;$i<count($file_list);$i++)

-                       {

-                               if ($file_list[$i]['selected'] != 'selected')

-                               {

-                                       unset($conv_list[$i]['selected']);

-                               }

-                       }

-

-                       return $file_list;

-               }

-               function 
read_single_custom_function($entity_id='',$cat_id='',$id,$acl_location='')

-               {

-                       if (!$acl_location && $entity_id && $cat_id)

-                       {

-                               $acl_location = '.entity.' . $entity_id . '.' . 
$cat_id;

-                       }

-                       return 
$this->so->read_single_custom_function($acl_location,$id);

-               }

-       }

-?>

+<?php
+       /**
+       * phpGroupWare - property: a Facilities Management System.
+       *
+       * @author Sigurd Nes <address@hidden>
+       * @copyright Copyright (C) 2003-2005 Free Software Foundation, Inc. 
http://www.fsf.org/
+       * @license http://www.gnu.org/licenses/gpl.html GNU General Public 
License
+       * @internal Development of this application was funded by 
http://www.bergen.kommune.no/bbb_/ekstern/
+       * @package property
+       * @subpackage admin
+       * @version $Id: class.boadmin_entity.inc.php,v 1.10 2006/01/30 22:14:19 
sigurdne Exp $
+       */
+
+       /**
+        * Description
+        * @package property
+        */
+
+       class boadmin_entity
+       {
+               var $start;
+               var $query;
+               var $filter;
+               var $sort;
+               var $order;
+               var $cat_id;
+               var $entity_id;
+
+               var $public_functions = array
+               (
+                       'read'                          => True,
+                       'read_single'           => True,
+                       'save'                          => True,
+                       'delete'                        => True,
+                       'check_perms'           => True
+               );
+
+               var $soap_functions = array(
+                       'list' => array(
+                               'in'  => 
array('int','int','struct','string','int'),
+                               'out' => array('array')
+                       ),
+                       'read' => array(
+                               'in'  => array('int','struct'),
+                               'out' => array('array')
+                       ),
+                       'save' => array(
+                               'in'  => array('int','struct'),
+                               'out' => array()
+                       ),
+                       'delete' => array(
+                               'in'  => array('int','struct'),
+                               'out' => array()
+                       )
+               );
+
+               function boadmin_entity($session=False)
+               {
+                       $this->currentapp       = 
$GLOBALS['phpgw_info']['flags']['currentapp'];
+                       $this->so               = 
CreateObject($this->currentapp.'.soadmin_entity');
+                       $this->bocommon = 
CreateObject($this->currentapp.'.bocommon');
+
+                       if ($session)
+                       {
+                               $this->read_sessiondata();
+                               $this->use_session = True;
+                       }
+
+                       $start  = get_var('start',array('POST','GET'));
+                       $query  = get_var('query',array('POST','GET'));
+                       $sort   = get_var('sort',array('POST','GET'));
+                       $order  = get_var('order',array('POST','GET'));
+                       $filter = get_var('filter',array('POST','GET'));
+                       $cat_id = get_var('cat_id',array('POST','GET'));
+                       $allrows        = 
get_var('allrows',array('POST','GET'));
+                       $entity_id      = 
get_var('entity_id',array('POST','GET'));
+
+                       if ($start)
+                       {
+                               $this->start=$start;
+                       }
+                       else
+                       {
+                               $this->start=0;
+                       }
+
+                       if(isset($query))
+                       {
+                               $this->query = $query;
+                       }
+                       if(!empty($filter))
+                       {
+                               $this->filter = $filter;
+                       }
+                       if(isset($sort))
+                       {
+                               $this->sort = $sort;
+                       }
+                       if(isset($order))
+                       {
+                               $this->order = $order;
+                       }
+                       if(isset($cat_id))
+                       {
+                               $this->cat_id = $cat_id;
+                       }
+                       if(isset($entity_id))
+                       {
+                               $this->entity_id = $entity_id;
+                       }
+                       if(isset($allrows))
+                       {
+                               $this->allrows = $allrows;
+                       }
+               }
+
+
+               function save_sessiondata($data)
+               {
+                       if ($this->use_session)
+                       {
+                               
$GLOBALS['phpgw']->session->appsession('session_data','standard_e',$data);
+                       }
+               }
+
+               function read_sessiondata()
+               {
+                       $data = 
$GLOBALS['phpgw']->session->appsession('session_data','standard_e');
+
+
+                       $this->start    = $data['start'];
+                       $this->query    = $data['query'];
+                       $this->filter   = $data['filter'];
+                       $this->sort             = $data['sort'];
+                       $this->order    = $data['order'];
+                       $this->cat_id   = $data['cat_id'];
+                       $this->entity_id        = $data['entity_id'];
+                       $this->allrows  = $data['allrows'];
+               }
+
+               function reset_fm_cache()
+               {
+                       $this->so->reset_fm_cache();
+               }
+
+
+               function get_location_level_list($selected='')
+               {
+
+                       $soadmin_location       = 
CreateObject($this->currentapp.'.soadmin_location');
+                       $location_types         = 
$soadmin_location->select_location_type();
+                       $max_location_type=count($location_types);
+
+                       for ($i=1; $i<=$max_location_type; $i++)
+                       {
+                               $location[$i][id] = $i;
+                               $location[$i][name] = $i . '-' . 
$location_types[($i-1)]['name'];
+                       }
+
+                       return 
$this->bocommon->select_list($selected,$location);
+
+               }
+
+               function get_entity_list($selected='')
+               {
+                       $list = $this->so->read(array('allrows'=>True));
+                       return 
$this->bocommon->select_multi_list($selected,$list);
+               }
+
+               function get_entity_list_2($selected='')
+               {
+                       $list[0]['id']='project';
+                       $list[0]['name']='project';
+                       $list[1]['id']='ticket';
+                       $list[1]['name']='ticket';
+                       $list[2]['id']='document';
+                       $list[2]['name']='document';
+                       $list[3]['id']='request';
+                       $list[3]['name']='request';
+                       $list[4]['id']='investment';
+                       $list[4]['name']='investment';
+                       $list[5]['id']='s_agreement';
+                       $list[5]['name']='service agreement';
+                       return 
$this->bocommon->select_multi_list($selected,$list);
+               }
+
+               function get_entity_list_3($selected='')
+               {
+                       $list[0]['id']='ticket';
+                       $list[0]['name']='ticket';
+                       $list[1]['id']='request';
+                       $list[1]['name']='request';
+                       return 
$this->bocommon->select_multi_list($selected,$list);
+               }
+
+               function read()
+               {
+                       $entity = $this->so->read(array('start' => 
$this->start,'query' => $this->query,'sort' => $this->sort,'order' => 
$this->order,'allrows'=>$this->allrows));
+
+                       $this->total_records = $this->so->total_records;
+                       return $entity;
+               }
+
+               function read_category($entity_id)
+               {
+                       $category = $this->so->read_category(array('start' => 
$this->start,'query' => $this->query,'sort' => $this->sort,
+                               'order' => 
$this->order,'allrows'=>$this->allrows,'entity_id'=>$entity_id));
+
+                       $this->total_records = $this->so->total_records;
+
+                       return $category;
+               }
+
+               function read_status()
+               {
+                       $status = $this->so->read_status(array('start' => 
$this->start,'query' => $this->query,'sort' => $this->sort,
+                               'order' => 
$this->order,'allrows'=>$this->allrows,'entity_id'=>$this->entity_id,'cat_id'=>$this->cat_id));
+
+                       $this->total_records = $this->so->total_records;
+
+                       return $status;
+               }
+
+               function read_config()
+               {
+                       $standard = $this->so->read_config(array('start' => 
$this->start,'query' => $this->query,'sort' => $this->sort,'order' => 
$this->order));
+
+                       $this->total_records = $this->so->total_records;
+
+
+                       return $standard;
+               }
+
+               function read_config_single($column_name)
+               {
+                       return $this->so->read_config_single($column_name);
+               }
+
+               function read_single($id)
+               {
+                       return $this->so->read_single($id);
+               }
+
+               function read_single_category($entity_id,$cat_id)
+               {
+                       return 
$this->so->read_single_category($entity_id,$cat_id);
+               }
+
+               function read_single_status($id)
+               {
+                       return 
$this->so->read_single_status($this->entity_id,$this->cat_id,$id);
+               }
+
+               function save($values,$action='')
+               {
+                       if ($action=='edit')
+                       {
+                               if ($values['id'] != '')
+                               {
+                                       $receipt = 
$this->so->edit_entity($values);
+                               }
+                       }
+                       else
+                       {
+                               $receipt = $this->so->add_entity($values);
+                       }
+                       return $receipt;
+               }
+
+               function save_category($values,$action='')
+               {
+                       if ($action=='edit')
+                       {
+                               if ($values['id'] != '')
+                               {
+                                       $receipt = 
$this->so->edit_category($values);
+                               }
+                       }
+                       else
+                       {
+                               $receipt = $this->so->add_category($values);
+                       }
+                       return $receipt;
+               }
+
+               function save_status($values,$action='')
+               {
+                       if ($action=='edit')
+                       {
+                               if ($values['id'] != '')
+                               {
+                                       $receipt = 
$this->so->edit_status($values,$this->entity_id,$this->cat_id);
+                               }
+                       }
+                       else
+                       {
+                               $receipt = 
$this->so->add_status($values,$this->entity_id,$this->cat_id);
+                       }
+                       return $receipt;
+               }
+
+               function 
delete($cat_id='',$entity_id='',$attrib_id='',$status_id='',$acl_location='',$custom_function_id='')
+               {
+                       if(!$status_id && !$attrib_id && !$cat_id && $entity_id 
&& !$custom_function_id):
+                       {
+                               $this->so->delete_entity($entity_id);
+                       }
+                       elseif(!$status_id && !$attrib_id && $cat_id && 
$entity_id && !$custom_function_id):
+                       {
+                               $this->so->delete_category($cat_id,$entity_id);
+                       }
+                       elseif(!$status_id && $attrib_id && $cat_id && 
$entity_id && !$custom_function_id):
+                       {
+                               
$this->so->delete_attrib($cat_id,$entity_id,$attrib_id);
+                       }
+                       elseif($status_id && !$attrib_id && $cat_id && 
$entity_id && !$custom_function_id):
+                       {
+                               
$this->so->delete_status($cat_id,$entity_id,$status_id);
+                       }
+                       elseif(!$status_id && $custom_function_id && 
$acl_location):
+                       {
+                               
$this->so->delete_custom_function($acl_location,$custom_function_id);
+                       }
+                       endif;
+
+
+               }
+
+               function read_attrib($entity_id='',$cat_id='',$allrows='')
+               {
+                       if($allrows)
+                       {
+                               $this->allrows = $allrows;
+                       }
+
+                       $attrib = $this->so->read_attrib(array('start' => 
$this->start,'query' => $this->query,'sort' => $this->sort,'order' => 
$this->order,
+                                                                               
        'cat_id' => $cat_id,'entity_id' => 
$entity_id,'allrows'=>$this->allrows));
+
+                       for ($i=0; $i<count($attrib); $i++)
+                       {
+                               $attrib[$i]['datatype'] = 
$this->bocommon->translate_datatype($attrib[$i]['datatype']);
+                       }
+
+                       $this->total_records = $this->so->total_records;
+
+                       return $attrib;
+               }
+
+               function read_single_attrib($entity_id,$cat_id,$id)
+               {
+                       return 
$this->so->read_single_attrib($entity_id,$cat_id,$id);
+               }
+
+               function resort_attrib($id,$resort)
+               {
+                       
$this->so->resort_attrib(array('resort'=>$resort,'entity_id' => 
$this->entity_id,'cat_id' => $this->cat_id,'id'=>$id));
+               }
+
+               function save_attrib($attrib,$action='')
+               {
+                       if ($action=='edit')
+                       {
+                               if ($attrib['id'] != '')
+                               {
+
+                                       $receipt = 
$this->so->edit_attrib($attrib);
+                               }
+                       }
+                       else
+                       {
+                               $receipt = $this->so->add_attrib($attrib);
+                       }
+                       return $receipt;
+               }
+
+               function save_config($values='',$column_name='')
+               {
+                               return 
$this->so->save_config($values,$column_name);
+               }
+
+               function 
read_custom_function($entity_id='',$cat_id='',$allrows='', $acl_location='')
+               {
+                       if($allrows)
+                       {
+                               $this->allrows = $allrows;
+                       }
+
+                       if (!$acl_location && $entity_id && $cat_id)
+                       {
+                               $acl_location = '.entity.' . $entity_id . '.' . 
$cat_id;
+                       }
+
+                       $custom_function = 
$this->so->read_custom_function(array('start' => $this->start,'query' => 
$this->query,'sort' => $this->sort,'order' => $this->order,
+                                                                               
        'acl_location' => $acl_location,'allrows'=>$this->allrows));
+
+                       $this->total_records = $this->so->total_records;
+
+                       return $custom_function;
+               }
+
+               function resort_custom_function($id,$resort)
+               {
+                       
$this->so->resort_custom_function(array('resort'=>$resort,'entity_id' => 
$this->entity_id,'cat_id' => $this->cat_id,'id'=>$id));
+               }
+
+               function save_custom_function($custom_function,$action='')
+               {
+                       if ($action=='edit')
+                       {
+                               if ($custom_function['id'] != '')
+                               {
+
+                                       $receipt = 
$this->so->edit_custom_function($custom_function);
+                               }
+                       }
+                       else
+                       {
+                               $receipt = 
$this->so->add_custom_function($custom_function);
+                       }
+                       return $receipt;
+               }
+
+               function select_custom_function($selected='')
+               {
+
+                       $dir_handle = @opendir(PHPGW_APP_INC . SEP . 'custom');
+                       $i=0; $myfilearray = '';
+                       while ($file = readdir($dir_handle))
+                       {
+                               if ((substr($file, 0, 1) != '.') && 
is_file(PHPGW_APP_INC . SEP . 'custom' . SEP . $file) )
+                               {
+                                       $myfilearray[$i] = $file;
+                                       $i++;
+                               }
+                       }
+                       closedir($dir_handle);
+                       sort($myfilearray);
+
+                       for ($i=0;$i<count($myfilearray);$i++)
+                       {
+                               $fname = ereg_replace('_',' ',$myfilearray[$i]);
+                               $sel_file = '';
+                               if ($myfilearray[$i]==$selected)
+                               {
+                                       $sel_file = 'selected';
+                               }
+
+                               $file_list[] = array
+                               (
+                                       'id'            => $myfilearray[$i],
+                                       'name'          => $fname,
+                                       'selected'      => $sel_file
+                               );
+                       }
+
+                       for ($i=0;$i<count($file_list);$i++)
+                       {
+                               if ($file_list[$i]['selected'] != 'selected')
+                               {
+                                       unset($conv_list[$i]['selected']);
+                               }
+                       }
+
+                       return $file_list;
+               }
+               function 
read_single_custom_function($entity_id='',$cat_id='',$id,$acl_location='')
+               {
+                       if (!$acl_location && $entity_id && $cat_id)
+                       {
+                               $acl_location = '.entity.' . $entity_id . '.' . 
$cat_id;
+                       }
+                       return 
$this->so->read_single_custom_function($acl_location,$id);
+               }
+       }
+?>
Index: property/inc/class.boadmin_location.inc.php
diff -u property/inc/class.boadmin_location.inc.php:1.7 
property/inc/class.boadmin_location.inc.php:1.8
--- property/inc/class.boadmin_location.inc.php:1.7     Fri Jan 27 14:05:43 2006
+++ property/inc/class.boadmin_location.inc.php Mon Jan 30 22:14:19 2006
@@ -8,7 +8,7 @@
        * @internal Development of this application was funded by 
http://www.bergen.kommune.no/bbb_/ekstern/
        * @package property
        * @subpackage admin
-       * @version $Id: class.boadmin_location.inc.php,v 1.7 2006/01/27 
14:05:43 sigurdne Exp $
+       * @version $Id: class.boadmin_location.inc.php,v 1.8 2006/01/30 
22:14:19 sigurdne Exp $
        */
 
        /**
@@ -300,7 +300,5 @@
                        }
                        return $nullable_list;
                }
-
-
        }
 ?>
Index: property/inc/class.soXport.inc.php
diff -u property/inc/class.soXport.inc.php:1.20 
property/inc/class.soXport.inc.php:1.21
--- property/inc/class.soXport.inc.php:1.20     Fri Jan 27 14:05:43 2006
+++ property/inc/class.soXport.inc.php  Mon Jan 30 22:14:19 2006
@@ -1,548 +1 @@
-<?php

-       /**

-       * phpGroupWare - property: a Facilities Management System.

-       *

-       * @author Sigurd Nes <address@hidden>

-       * @copyright Copyright (C) 2003-2005 Free Software Foundation, Inc. 
http://www.fsf.org/

-       * @license http://www.gnu.org/licenses/gpl.html GNU General Public 
License

-       * @internal Development of this application was funded by 
http://www.bergen.kommune.no/bbb_/ekstern/

-       * @package property

-       * @subpackage admin

-       * @version $Id: class.soXport.inc.php,v 1.20 2006/01/27 14:05:43 
sigurdne Exp $

-       */

-

-       /**

-        * Description

-        * @package property

-        */

-

-       class soXport

-       {

-               var $db = '';

-               var $account_id = 0;

-               var $total_records = 0;

-

-               function soXport($useacl=True)

-               {

-

-                       $GLOBALS['phpgw_info']['flags']['currentapp']   =       
'property';

-                       $this->currentapp               = 
$GLOBALS['phpgw_info']['flags']['currentapp'];

-                       $this->bocommon                 = 
CreateObject($this->currentapp.'.bocommon');
                        $this->db               = $this->bocommon->new_db();
-                       $this->db2              = $this->bocommon->new_db();

-                       $this->join                             = 
$this->bocommon->join;

-                       $this->left_join                        = 
$this->bocommon->left_join;

-                       $this->datetimeformat   = 
$this->bocommon->datetimeformat;

-                       $this->like                             = 
$this->bocommon->like;

-                       $this->account_id               = 
$GLOBALS['phpgw_info']['user']['account_id'];

-               }

-

-

-               function auto_tax($dima='')

-               {

-                       if(!$dima)

-                       {

-                               return;

-                       }

-                       $sql = "select mva as tax_code from fm_location1 where 
loc1=" . substr($dima,0,4);

-                       $this->db->query($sql);

-                       $this->db->next_record();

-

-                       return $this->db->f('tax_code');

-               }

-

-               function tax_b_account_override($mvakode='',$b_account_id='')

-               {

-                       if(!$b_account_id)

-                       {

-                               return $mvakode;

-                       }

-                       $sql = "select mva as tax_code from fm_b_account where 
id='$b_account_id'";

-                       $this->db->query($sql);

-                       $this->db->next_record();

-

-                       if($this->db->f('tax_code'))

-                       {

-                               return $this->db->f('tax_code');

-                       }

-                       else

-                       {

-                               return $mvakode;

-                       }

-

-               }

-

-               function tax_vendor_override($mvakode='',$vendor_id='')

-               {

-                       if(!$vendor_id)

-                       {

-                               return $mvakode;

-                       }

-                       $sql = "select mva as tax_code from fm_vendor where 
id='$vendor_id'";

-                       $this->db->query($sql);

-                       $this->db->next_record();

-

-                       if($this->db->f('tax_code'))

-                       {

-                               return $this->db->f('tax_code');

-                       }

-                       else

-                       {

-                               return $mvakode;

-                       }

-

-               }

-

-               function get_kostra_id($dima='')

-               {

-                       if(!$dima)

-                       {

-                               return;

-                       }

-                       $sql = "select kostra_id from fm_location1 where loc1=" 
. substr($dima,0,4);

-                       $this->db->query($sql);

-                       $this->db->next_record();

-

-                       return $this->db->f('kostra_id');

-               }

-

-               function anleggsnr_to_objekt($anleggsnr,$meter_table)

-               {

-                       $this->db->query("select 
$meter_table.ext_meter_id,$meter_table.loc1,$meter_table.loc2,$meter_table.loc3,fm_part_of_town.district_id
 "

-                       . " from $meter_table $this->join fm_location1 ON 
$meter_table.loc1 = fm_location1.loc1 $this->join "

-            . " fm_part_of_town ON fm_location1.part_of_town_id = 
fm_part_of_town.part_of_town_id where $meter_table.ext_system_id='$anleggsnr'");

-

-                       $this->db->next_record();

-

-//                     $location       = split("-", 
$this->db->f('location_code'));

-

-                       $loc1 = $this->db->f('loc1');

-                       $loc2 = $this->db->f('loc2');

-                       $loc3 = $this->db->f('loc3');

-                       $dima=$loc1.$loc2.$loc3;

-

-                       $maalerinfo['loc1']=$loc1;

-                       $maalerinfo['dima']=$dima;

-                       $maalerinfo['maalernr']=$this->db->f('ext_meter_id');

-                       $maalerinfo['district']=$this->db->f('district_id');

-                       return $maalerinfo;

-

-               }

-

-               function gabnr_to_objekt($Gnr,$Bnr,$sekjonnr)

-               {

-               //Finn dima fra Boei

-                       $sql = "select fm_gab_location.loc1, 
fm_gab_location.loc2, fm_gab_location.loc3 from fm_gab_location, fm_location1, 
fm_owner "

-                       . "where substring(fm_gab_location.gab_id,5,5)='$Gnr' 
and "

-                       . "      substring(fm_gab_location.gab_id,10,4)='$Bnr' 
and "

-                       . "      
substring(fm_gab_location.gab_id,18,3)='$sekjonnr' and "

-                       . "      fm_gab_location.loc1=fm_location1.loc1 and "

-                       . "      fm_location1.owner_id=fm_owner.id ";

-               //      . "      and (fm_owner.category=0 or 
fm_owner.category=2)";

-

-                       $GLOBALS['phpgw']->db->query($sql,__LINE__,__FILE__);

-                       $GLOBALS['phpgw']->db->next_record();

-

-                       $gabinfo['loc1']=$GLOBALS['phpgw']->db->f('loc1');

-                       
$gabinfo['dima']=$GLOBALS['phpgw']->db->f('loc1').$GLOBALS['phpgw']->db->f('loc2').$GLOBALS['phpgw']->db->f('loc3');

-

-                       return $gabinfo;

-               }

-

-

-               function dima_to_address($dima)

-               {

-                       $loc1=substr($dima,0,4);

-                       $loc2=substr($dima,4,2);

-                       $loc3=substr($dima,6,2);

-                       $sql = "select loc3_name from fm_location3 where loc1 = 
'$loc1' and loc2= '$loc2' and loc3 = '$loc3' ";

-                       $GLOBALS['phpgw']->db->query($sql,__LINE__,__FILE__);

-                       $GLOBALS['phpgw']->db->next_record();

-                       $address=$GLOBALS['phpgw']->db->f('loc3_name');

-                       return $address;

-

-               }

-

-               function check_order($id)

-               {

-                       $this->db->query("select id,type from fm_orders where 
id='$id'");

-                       $this->db->next_record();

-                       return $this->db->f('type');

-               }

-

-               function get_project($id)

-               {

-                       $this->db->query("select project_id from fm_workorder 
where id='$id'");

-                       $this->db->next_record();

-                       return $this->db->f('project_id');

-               }

-

-               function check_spbudact_code($id)

-               {

-

-                       $this->db->query("select count(*) from fm_b_account 
where id='$id'");

-                       $this->db->next_record();

-                       return $this->db->f(0);

-               }

-

-               function add($buffer)

-               {

-                       $this->db->transaction_begin();

-

-                       $num=0;

-                       foreach ($buffer as $fields)

-                       {

-                               if(abs($fields['belop'])>0)

-                               {

-                                       $values= array(

-                                               $fields['project_id'],

-                                               $fields['kostra_id'],

-                                               $fields['pmwrkord_code'],

-                                               $fields['bilagsnr'],

-                                               $fields['splitt'],

-                                               $fields['kildeid'],

-                                               $fields['kidnr'],

-                                               $fields['typeid'],

-                                               $fields['fakturadato'],

-                                               $fields['forfallsdato'],

-                                               $fields['regtid'],

-                                               $fields['artid'],

-                                               $fields['spvend_code'],

-                                               $fields['dimb'],

-                                               $fields['oppsynsmannid'],

-                                               $fields['saksbehandlerid'],

-                                               $fields['budsjettansvarligid'],

-                                               $fields['fakturanr'],

-                                               $fields['spbudact_code'],

-                                               $fields['loc1'],

-                                               $fields['dima'],

-                                               $fields['dimd'],

-                                               $fields['mvakode'],

-                                               $fields['periode'],

-                                               
$this->db->db_addslashes($fields['merknad']),

-                                               False,

-                                               False,

-                                               False,

-                                               False,

-                                               $fields['item_type'],

-                                               $fields['item_id']

-                                               );

-

-                                       $bilagsnr       = $fields['bilagsnr'];

-

-                                       $values = 
$this->bocommon->validate_db_insert($values);

-

-                                       $sql= "INSERT INTO fm_ecobilag 
(project_id,kostra_id,pmwrkord_code,bilagsnr,splitt,kildeid,kidnr,typeid,fakturadato,"

-                                       . " 
forfallsdato,regtid,artid,spvend_code,dimb,oppsynsmannid,saksbehandlerid,budsjettansvarligid,"

-                                       . " 
fakturanr,spbudact_code,loc1,dima,dimd,mvakode,periode,merknad,oppsynsigndato,saksigndato,"

-                                       . " 
budsjettsigndato,utbetalingsigndato,item_type,item_id,belop,godkjentbelop)"

-                                       . " VALUES ($values," . 
$this->bocommon->moneyformat($fields['belop']) . "," . 
$this->bocommon->moneyformat($fields['godkjentbelop']) . ")";

-

-                                       
$this->db->query($sql,__LINE__,__FILE__);

-

-                                       $num++;

-                               }

-                       }

-

-                       $this->db->query("UPDATE fm_idgenerator set value 
=$bilagsnr WHERE name = 'Bilagsnummer'");

-

-                       $this->db->transaction_commit();

-

-                       return $num;

-               }

-

-               function add_OverfBilag ($data)

-               {

-                       $values= array(

-                               $data['id'],

-                               $data['bilagsnr'],

-                               $data['kidnr'],

-                               $data['typeid'],

-                               $data['kildeid'],

-                               $data['project_id'],

-                               $data['kostra_id'],

-                               $data['pmwrkord_code'],

-                               $data['fakturadato'],

-                               $data['periode'],

-                               $data['forfallsdato'],

-                               $data['fakturanr'],

-                               $data['spbudact_code'],

-                               $data['regtid'],

-                               $data['artid'],

-                               $data['spvend_code'],

-                               $data['dima'],

-                               $data['loc1'],

-                               $data['dimb'],

-                               $data['mvakode'],

-                               $data['dimd'],

-                               $data['oppsynsmannid'],

-                               $data['saksbehandlerid'],

-                               $data['budsjettansvarligid'],

-                               $data['oppsynsigndato'],

-                               $data['saksigndato'],

-                               $data['budsjettsigndato'],

-                               $this->db->db_addslashes($data['merknad']),

-                               $data['splitt'],

-                               $data['utbetalingid'],

-                               $data['utbetalingsigndato'],

-                               $data['filnavn'],

-                               date("Y-m-d G:i:s"),

-                               $data['item_type'],

-                               $data['item_id'],

-                               );

-

-                       $values = $this->bocommon->validate_db_insert($values);

-

-                       $sql="INSERT INTO fm_ecobilagoverf 
(id,bilagsnr,kidnr,typeid,kildeid,project_id,kostra_id,pmwrkord_code,fakturadato,"

-                               . " 
periode,forfallsdato,fakturanr,spbudact_code,regtid,artid,spvend_code,dima,loc1,"

-                               . " 
dimb,mvakode,dimd,oppsynsmannid,saksbehandlerid,budsjettansvarligid,oppsynsigndato,saksigndato,"

-                               . " 
budsjettsigndato,merknad,splitt,utbetalingid,utbetalingsigndato,filnavn,overftid,item_type,item_id,"

-                               . " belop,godkjentbelop,ordrebelop)"

-                               . "values ($values, "

-                               . $this->bocommon->moneyformat($data['belop']) 
. ","

-                               . 
$this->bocommon->moneyformat($data['godkjentbelop']) . ","

-                               . 
$this->bocommon->moneyformat($data['ordrebelop']) . ")";

-

-                       $this->db->query($sql,__LINE__,__FILE__);

-//echo 'sql ' . $sql.'<br>';

-               }

-

-               function delete_from_fm_ecobilag($id)

-               {

-                       $sql="delete from fm_ecobilag where id=$id";

-                       $this->db->query($sql,__LINE__,__FILE__);

-               }

-

-               // Velg ut alle hoved bilag som skal overføres

-       function hoved_bilag ($periode,$pre_transfer='')

-       {

-                       if($pre_transfer)

-                       {

-                               $sql= "SELECT sum(belop) as belop, bilagsnr 
from fm_ecobilag WHERE periode='$periode' AND utbetalingsigndato IS NULL GROUP 
BY bilagsnr ";

-                       }

-                       else

-                       {

-                       $sql="select sum(belop) as belop, bilagsnr from 
fm_ecobilag where periode='$periode'  AND budsjettsigndato is not NULL  AND ( 
saksigndato is not NULL  OR oppsynsigndato is not NULL  ) AND 
utbetalingsigndato is not NULL group by bilagsnr";

-               }

-

-                       $this->db->query($sql,__LINE__,__FILE__);

-                       $i = 0;

-                       while ($this->db->next_record())

-                       {

-                               $hoved_bilag_temp[$i]['belop']          = 
$this->db->f('belop');

-                               $hoved_bilag_temp[$i]['bilagsnr']       = 
$this->db->f('bilagsnr');

-                               $i++;

-                       }

-

-//_debug_array($hoved_bilag_temp);

-

-                       if ($hoved_bilag_temp)

-                       {

-                               $i = 0;

-                               while(each($hoved_bilag_temp))

-                               {

-                                       $bilagsnr = 
$hoved_bilag_temp[$i]['bilagsnr'];

-

-                                       $sql= "select 
fm_ecobilag.*,fm_ecouser.initials as saksbehandler from fm_ecobilag $this->join 
fm_ecouser on fm_ecobilag.budsjettansvarligid=fm_ecouser.lid where 
bilagsnr=$bilagsnr ";

-                                       
$this->db->query($sql,__LINE__,__FILE__);

-

-                                       $this->db->next_record();

-

-                                       $hoved_bilag[$i]['id']                  
        = $this->db->f('id');

-                                       $hoved_bilag[$i]['bilagsnr']            
= $bilagsnr;

-                                       $hoved_bilag[$i]['kidnr']               
        = $this->db->f('kidnr');

-                                       $hoved_bilag[$i]['typeid']              
        = $this->db->f('typeid');

-                                       $hoved_bilag[$i]['kildeid']             
        = $this->db->f('kildeid');

-                                       $hoved_bilag[$i]['pmwrkord_code']       
= $this->db->f('pmwrkord_code');

-                                       $hoved_bilag[$i]['belop']               
        = $hoved_bilag_temp[$i]['belop'];

-                                       $hoved_bilag[$i]['fakturadato']         
= $this->db->f('fakturadato');

-                                       $hoved_bilag[$i]['periode']             
        = $this->db->f('periode');

-                                       $hoved_bilag[$i]['forfallsdato']        
= $this->db->f('forfallsdato');

-                                       $hoved_bilag[$i]['fakturanr']           
= $this->db->f('fakturanr');

-                                       $hoved_bilag[$i]['spbudact_code']       
= $this->db->f('spbudact_code');

-                                       $hoved_bilag[$i]['regtid']              
        = $this->db->f('regtid');

-                                       $hoved_bilag[$i]['artid']               
        = $this->db->f('artid');

-                                       $hoved_bilag[$i]['godkjentbelop']       
= $hoved_bilag_temp[$i]['belop'];

-                                       $hoved_bilag[$i]['spvend_code']         
= $this->db->f('spvend_code');

-                                       $hoved_bilag[$i]['dima']                
        = $this->db->f('dima');

-                                       $hoved_bilag[$i]['dimb']                
        = $this->db->f('dimb');

-                                       $hoved_bilag[$i]['mvakode']             
        = $this->db->f('mvakode');

-                                       $hoved_bilag[$i]['dimd']                
        = $this->db->f('dimd');

-                                       if($this->db->f('oppsynsmannid'))

-                                       {

-                                               
$hoved_bilag[$i]['oppsynsmannid']       = $this->db->f('oppsynsmannid');

-                                       }

-                                       if($this->db->f('saksbehandlerid'))

-                                       {

-                                               
$hoved_bilag[$i]['saksbehandlerid']     = $this->db->f('saksbehandlerid');

-                                       }

-

-                                       $hoved_bilag[$i]['budsjettansvarligid'] 
= $this->db->f('budsjettansvarligid');

-

-                                       if($this->db->f('oppsynsigndato'))

-                                       {

-                                               
$hoved_bilag[$i]['oppsynsigndato']      = $this->db->f('oppsynsigndato');

-                                       }

-                                       if($this->db->f('saksigndato'))

-                                       {

-                                               $hoved_bilag[$i]['saksigndato'] 
= $this->db->f('saksigndato');

-                                       }

-

-                                       $hoved_bilag[$i]['budsjettsigndato']    
= $this->db->f('budsjettsigndato');

-                                       $hoved_bilag[$i]['merknad']             
                = $this->db->f('merknad');

-                                       $hoved_bilag[$i]['splitt']              
                = $this->db->f('splitt');

-                                       $hoved_bilag[$i]['utbetalingid']        
        = $this->db->f('utbetalingid');

-                                       $hoved_bilag[$i]['utbetalingsigndato']  
= $this->db->f('utbetalingsigndato');

-                                       $hoved_bilag[$i]['saksbehandler']       
        = $this->db->f('saksbehandler');

-                                       $i++;

-                               }

-                       }

-//_debug_array($hoved_bilag);

-

-                       return $hoved_bilag;

-       }

-

-               //Velg ut alle underbilag

-

-               function select_underbilag ($bilagsnr)

-               {

-                       $sql= "SELECT fm_ecobilag.* 
,fm_part_of_town.district_id FROM (fm_location1 $this->join fm_part_of_town ON 
fm_location1.part_of_town_id = fm_part_of_town.part_of_town_id) $this->join 
fm_ecobilag ON fm_location1.loc1 = fm_ecobilag.loc1 WHERE bilagsnr='$bilagsnr'";

-

-                       $this->db->query($sql,__LINE__,__FILE__);

-                       $i = 0;

-                       while ($this->db->next_record())

-                       {

-                               $underbilag[$i]['id']   = $this->db->f('id');

-                               $underbilag[$i]['bilagsnr']     = 
$this->db->f('bilagsnr');

-                               $underbilag[$i]['kidnr']        = 
$this->db->f('kidnr');

-                               $underbilag[$i]['typeid']       = 
$this->db->f('typeid');

-                               $underbilag[$i]['kildeid']      = 
$this->db->f('kildeid');

-                               $underbilag[$i]['pmwrkord_code']        = 
$this->db->f('pmwrkord_code');

-                               $underbilag[$i]['belop']        = 
$this->db->f('belop');

-                               $underbilag[$i]['fakturadato']  = 
$this->db->f('fakturadato');

-                               $underbilag[$i]['periode']      = 
$this->db->f('periode');

-                               $underbilag[$i]['forfallsdato'] = 
$this->db->f('forfallsdato');

-                               $underbilag[$i]['fakturanr']    = 
$this->db->f('fakturanr');

-                               $underbilag[$i]['spbudact_code']        = 
$this->db->f('spbudact_code');

-                               $underbilag[$i]['regtid']       = 
$this->db->f('regtid');

-                               $underbilag[$i]['artid']        = 
$this->db->f('artid');

-                               $underbilag[$i]['godkjentbelop']        = 
$this->db->f('godkjentbelop');

-                               $underbilag[$i]['spvend_code']  = 
$this->db->f('spvend_code');

-                               $underbilag[$i]['dima'] = $this->db->f('dima');

-                               $underbilag[$i]['loc1'] = $this->db->f('loc1');

-                               $underbilag[$i]['dimb'] = $this->db->f('dimb');

-                               $underbilag[$i]['mvakode']      = 
$this->db->f('mvakode');

-                               $underbilag[$i]['dimd'] = $this->db->f('dimd');

-                               $underbilag[$i]['project_id']   = 
$this->db->f('project_id');

-                               $underbilag[$i]['kostra_id']    = 
$this->db->f('kostra_id');

-                               if($this->db->f('oppsynsmannid'))

-                               {

-                                       $underbilag[$i]['oppsynsmannid']        
= $this->db->f('oppsynsmannid');

-                               }

-                               if($this->db->f('saksbehandlerid'))

-                               {

-                                       $underbilag[$i]['saksbehandlerid']      
= $this->db->f('saksbehandlerid');

-                               }

-

-                               $underbilag[$i]['budsjettansvarligid']  = 
$this->db->f('budsjettansvarligid');

-

-                               if($this->db->f('oppsynsigndato'))

-                               {

-                                       $underbilag[$i]['oppsynsigndato']       
= $this->db->f('oppsynsigndato');

-                               }

-                               if($this->db->f('saksigndato'))

-                               {

-                                       $underbilag[$i]['saksigndato']  = 
$this->db->f('saksigndato');

-                               }

-

-                               $underbilag[$i]['budsjettsigndato']     = 
$this->db->f('budsjettsigndato');

-                               $underbilag[$i]['merknad']      = 
$this->db->f('merknad');

-                               $underbilag[$i]['splitt']       = 
$this->db->f('splitt');

-                               $underbilag[$i]['utbetalingid'] = 
$this->db->f('utbetalingid');

-                               $underbilag[$i]['utbetalingsigndato']   = 
$this->db->f('utbetalingsigndato');

-                               $underbilag[$i]['district_id']  = 
$this->db->f('district_id');

-                               $underbilag[$i]['item_type']    = 
$this->db->f('item_type');

-                               $underbilag[$i]['item_id']      = 
$this->db->f('item_id');

-                               $i++;

-

-                       }

-

-                       return $underbilag;

-               }

-

-/*             function update_avvik($avvik)

-               {

-               }

-

-*/

-               function log_to_deviation_table($oRsBilag)

-               {

-                       $bilagsnr=$oRsBilag['bilagsnr'];

-                       $fakturadato=$oRsBilag['fakturadato'];

-                       $forfallsdato=$oRsBilag['forfallsdato'];

-                       $oppsynsmannid=$oRsBilag['oppsynsmannid'];

-                       $oppsynsigndato=$oRsBilag['oppsynsigndato'];

-                       $saksbehandlerid=$oRsBilag['saksbehandlerid'];

-                       $saksigndato=$oRsBilag['saksigndato'];

-                       $budsjettansvarligid=$oRsBilag['budsjettansvarligid'];

-                       $budsjettsigndato=$oRsBilag['budsjettsigndato'];

-                       $artid=$oRsBilag['artid'];

-                       $spvend_code=$oRsBilag['spvend_code'];

-                       $belop=$oRsBilag['belop'];

-                       $godkjentbelop=$oRsBilag['godkjentbelop'];

-

-                       $sql="INSERT INTO fm_ecoavvik 
(bilagsnr,fakturadato,forfallsdato,oppsynsmannid,oppsynsigndato,saksbehandlerid,saksigndato,budsjettansvarligid,budsjettsigndato,artid,spvend_code,belop,godkjentbelop)
  values "

-                       . 
"($bilagsnr','$fakturadato','$forfallsdato','$oppsynsmannid','$oppsynsigndato','$saksbehandlerid','$saksigndato','$budsjettansvarligid','$budsjettsigndato','$artid','$spvend_code','$belop','$godkjentbelop')";

-                       $this->db->query($sql,__LINE__,__FILE__);

-               }

-

-               function delete_avvik($bilagsnr)

-               {

-                       $sql="delete from fm_ecoavvik where 
bilagsnr='$bilagsnr'";

-                       $this->db->query($sql,__LINE__,__FILE__);

-               }

-

-               function delete_invoice($bilagsnr)

-               {

-                       $sql="delete from fm_ecobilagoverf where 
bilagsnr='$bilagsnr'";

-                       $this->db->query($sql,__LINE__,__FILE__);

-               }

-

-               //Logg transaksjon

-               function log_transaction($batchid,$bilagid,$message='')

-               {

-                       $tid=date($this->datetimeformat);

-                       $sql= "insert into fm_ecologg 
(batchid,ecobilagid,melding,tid) values ('$batchid','$bilagid' 
,'$message','$tid')";

-                       $this->db->query($sql,__LINE__,__FILE__);

-               }

-

-               function increment_batchid()

-               {

-                       $this->db->query("update fm_idgenerator  set value = 
value + 1 where name = 'Ecobatchid'");

-                       $this->db->query("select value from fm_idgenerator  
where name = 'Ecobatchid'");

-                       $this->db->next_record();

-                       $bilagsnr = $this->db->f('value');

-                       return $bilagsnr;

-

-               }

-

-               function next_batchid()

-               {

-

-                       $this->db->query("select value from fm_idgenerator  
where name = 'Ecobatchid'");

-                       $this->db->next_record();

-                       $batchid = $this->db->f('value')+1;

-

-                       return $batchid;

-               }

-

-               function get_responsible($b_abbount_id)

-               {

-                       $this->db->query("select account_lid from fm_b_account 
$this->join phpgw_accounts on fm_b_account.responsible = 
phpgw_accounts.account_id where fm_b_account.id = '$b_abbount_id'");

-                       $this->db->next_record();

-                       $responsible = $this->db->f('account_lid');

-                       return $responsible;

-               }

-

-       }

-

-?>

+<?php
        /**
        * phpGroupWare - property: a Facilities Management System.
        *
        * @author Sigurd Nes <address@hidden>
        * @copyright Copyright (C) 2003-2005 Free Software Foundation, Inc. 
http://www.fsf.org/
        * @license http://www.gnu.org/licenses/gpl.html GNU General Public 
License
        * @internal Development of this application was funded by 
http://www.bergen.kommune.no/bbb_/ekstern/
        * @package property
        * @subpackage admin
        * @version $Id: class.soXport.inc.php,v 1.21 2006/01/30 22:14:19 
sigurdne Exp $
        */

        /**
         * Description
         * @package property
         */

        class soXport
        {
                var $db = '';
                var $account_id = 0;
                var $total_records = 0;

                function soXport($useacl=True)
                {

                        $GLOBALS['phpgw_info']['flags']['currentapp']   =       
'property';
                        $this->currentapp               = 
$GLOBALS['phpgw_info']['flags']['currentapp'];
                        $this->bocommon                 = 
CreateObject($this->currentapp.'.bocommon');
        $this->db               = $this->bocommon->new_db();
                $this->db2              = $this->bocommon->new_db();
                        $this->join                             = 
$this->bocommon->join;
                        $this->left_join                        = 
$this->bocommon->left_join;
                        $this->datetimeformat   = 
$this->bocommon->datetimeformat;
                        $this->like                             = 
$this->bocommon->like;
                        $this->account_id               = 
$GLOBALS['phpgw_info']['user']['account_id'];
                }


                function auto_tax($dima='')
                {
                        if(!$dima)
                        {
                                return;
                        }
                        $sql = "select mva as tax_code from fm_location1 where 
loc1=" . substr($dima,0,4);
                        $this->db->query($sql);
                        $this->db->next_record();

                        return $this->db->f('tax_code');
                }

                function tax_b_account_override($mvakode='',$b_account_id='')
                {
                        if(!$b_account_id)
                        {
                                return $mvakode;
                        }
                        $sql = "select mva as tax_code from fm_b_account where 
id='$b_account_id'";
                        $this->db->query($sql);
                        $this->db->next_record();

                        if($this->db->f('tax_code'))
                        {
                                return $this->db->f('tax_code');
                        }
                        else
                        {
                                return $mvakode;
                        }

                }

                function tax_vendor_override($mvakode='',$vendor_id='')
                {
                        if(!$vendor_id)
                        {
                                return $mvakode;
                        }
                        $sql = "select mva as tax_code from fm_vendor where 
id='$vendor_id'";
                        $this->db->query($sql);
                        $this->db->next_record();

                        if($this->db->f('tax_code'))
                        {
                                return $this->db->f('tax_code');
                        }
                        else
                        {
                                return $mvakode;
                        }

                }

                function get_kostra_id($dima='')
                {
                        if(!$dima)
                        {
                                return;
                        }
                        $sql = "select kostra_id from fm_location1 where loc1=" 
. substr($dima,0,4);
                        $this->db->query($sql);
                        $this->db->next_record();

                        return $this->db->f('kostra_id');
                }

                function anleggsnr_to_objekt($anleggsnr,$meter_table)
                {
                        $this->db->query("select 
$meter_table.ext_meter_id,$meter_table.loc1,$meter_table.loc2,$meter_table.loc3,fm_part_of_town.district_id
 "
                        . " from $meter_table $this->join fm_location1 ON 
$meter_table.loc1 = fm_location1.loc1 $this->join "
            . " fm_part_of_town ON fm_location1.part_of_town_id = 
fm_part_of_town.part_of_town_id where $meter_table.ext_system_id='$anleggsnr'");

                        $this->db->next_record();

//                      $location       = split("-", 
$this->db->f('location_code'));

                        $loc1 = $this->db->f('loc1');
                        $loc2 = $this->db->f('loc2');
                        $loc3 = $this->db->f('loc3');
                        $dima=$loc1.$loc2.$loc3;

                        $maalerinfo['loc1']=$loc1;
                        $maalerinfo['dima']=$dima;
                        $maalerinfo['maalernr']=$this->db->f('ext_meter_id');
                        $maalerinfo['district']=$this->db->f('district_id');
                        return $maalerinfo;

                }

                function gabnr_to_objekt($Gnr,$Bnr,$sekjonnr)
                {
                //Finn dima fra Boei
                        $sql = "select fm_gab_location.loc1, 
fm_gab_location.loc2, fm_gab_location.loc3 from fm_gab_location, fm_location1, 
fm_owner "
                        . "where substring(fm_gab_location.gab_id,5,5)='$Gnr' 
and "
                        . "      substring(fm_gab_location.gab_id,10,4)='$Bnr' 
and "
                        . "      
substring(fm_gab_location.gab_id,18,3)='$sekjonnr' and "
                        . "      fm_gab_location.loc1=fm_location1.loc1 and "
                        . "      fm_location1.owner_id=fm_owner.id ";
                //      . "      and (fm_owner.category=0 or 
fm_owner.category=2)";

                        $GLOBALS['phpgw']->db->query($sql,__LINE__,__FILE__);
                        $GLOBALS['phpgw']->db->next_record();

                        $gabinfo['loc1']=$GLOBALS['phpgw']->db->f('loc1');
                        
$gabinfo['dima']=$GLOBALS['phpgw']->db->f('loc1').$GLOBALS['phpgw']->db->f('loc2').$GLOBALS['phpgw']->db->f('loc3');

                        return $gabinfo;
                }


                function dima_to_address($dima)
                {
                        $loc1=substr($dima,0,4);
                        $loc2=substr($dima,4,2);
                        $loc3=substr($dima,6,2);
                        $sql = "select loc3_name from fm_location3 where loc1 = 
'$loc1' and loc2= '$loc2' and loc3 = '$loc3' ";
                        $GLOBALS['phpgw']->db->query($sql,__LINE__,__FILE__);
                        $GLOBALS['phpgw']->db->next_record();
                        $address=$GLOBALS['phpgw']->db->f('loc3_name');
                        return $address;

                }

                function check_order($id)
                {
                        $this->db->query("select id,type from fm_orders where 
id='$id'");
                        $this->db->next_record();
                        return $this->db->f('type');
                }

                function get_project($id)
                {
                        $this->db->query("select project_id from fm_workorder 
where id='$id'");
                        $this->db->next_record();
                        return $this->db->f('project_id');
                }

                function check_spbudact_code($id)
                {

                        $this->db->query("select count(*) from fm_b_account 
where id='$id'");
                        $this->db->next_record();
                        return $this->db->f(0);
                }

                function add($buffer)
                {
                        $this->db->transaction_begin();

                        $num=0;
                        foreach ($buffer as $fields)
                        {
                                if(abs($fields['belop'])>0)
                                {
                                        $values= array(
                                                $fields['project_id'],
                                                $fields['kostra_id'],
                                                $fields['pmwrkord_code'],
                                                $fields['bilagsnr'],
                                                $fields['splitt'],
                                                $fields['kildeid'],
                                                $fields['kidnr'],
                                                $fields['typeid'],
                                                $fields['fakturadato'],
                                                $fields['forfallsdato'],
                                                $fields['regtid'],
                                                $fields['artid'],
                                                $fields['spvend_code'],
                                                $fields['dimb'],
                                                $fields['oppsynsmannid'],
                                                $fields['saksbehandlerid'],
                                                $fields['budsjettansvarligid'],
                                                $fields['fakturanr'],
                                                $fields['spbudact_code'],
                                                $fields['loc1'],
                                                $fields['dima'],
                                                $fields['dimd'],
                                                $fields['mvakode'],
                                                $fields['periode'],
                                                
$this->db->db_addslashes($fields['merknad']),
                                                False,
                                                False,
                                                False,
                                                False,
                                                $fields['item_type'],
                                                $fields['item_id']
                                                );

                                        $bilagsnr       = $fields['bilagsnr'];

                                        $values = 
$this->bocommon->validate_db_insert($values);

                                        $sql= "INSERT INTO fm_ecobilag 
(project_id,kostra_id,pmwrkord_code,bilagsnr,splitt,kildeid,kidnr,typeid,fakturadato,"
                                        . " 
forfallsdato,regtid,artid,spvend_code,dimb,oppsynsmannid,saksbehandlerid,budsjettansvarligid,"
                                        . " 
fakturanr,spbudact_code,loc1,dima,dimd,mvakode,periode,merknad,oppsynsigndato,saksigndato,"
                                        . " 
budsjettsigndato,utbetalingsigndato,item_type,item_id,belop,godkjentbelop)"
                                        . " VALUES ($values," . 
$this->bocommon->moneyformat($fields['belop']) . "," . 
$this->bocommon->moneyformat($fields['godkjentbelop']) . ")";

                                        
$this->db->query($sql,__LINE__,__FILE__);

                                        $num++;
                                }
                        }

                        $this->db->query("UPDATE fm_idgenerator set value 
=$bilagsnr WHERE name = 'Bilagsnummer'");

                        $this->db->transaction_commit();

                        return $num;
                }

                function add_OverfBilag ($data)
                {
                        $values= array(
                                $data['id'],
                                $data['bilagsnr'],
                                $data['kidnr'],
                                $data['typeid'],
                                $data['kildeid'],
                                $data['project_id'],
                                $data['kostra_id'],
                                $data['pmwrkord_code'],
                                $data['fakturadato'],
                                $data['periode'],
                                $data['forfallsdato'],
                                $data['fakturanr'],
                                $data['spbudact_code'],
                                $data['regtid'],
                                $data['artid'],
                                $data['spvend_code'],
                                $data['dima'],
                                $data['loc1'],
                                $data['dimb'],
                                $data['mvakode'],
                                $data['dimd'],
                                $data['oppsynsmannid'],
                                $data['saksbehandlerid'],
                                $data['budsjettansvarligid'],
                                $data['oppsynsigndato'],
                                $data['saksigndato'],
                                $data['budsjettsigndato'],
                                $this->db->db_addslashes($data['merknad']),
                                $data['splitt'],
                                $data['utbetalingid'],
                                $data['utbetalingsigndato'],
                                $data['filnavn'],
                                date("Y-m-d G:i:s"),
                                $data['item_type'],
                                $data['item_id'],
                                );

                        $values = $this->bocommon->validate_db_insert($values);

                        $sql="INSERT INTO fm_ecobilagoverf 
(id,bilagsnr,kidnr,typeid,kildeid,project_id,kostra_id,pmwrkord_code,fakturadato,"
                                . " 
periode,forfallsdato,fakturanr,spbudact_code,regtid,artid,spvend_code,dima,loc1,"
                                . " 
dimb,mvakode,dimd,oppsynsmannid,saksbehandlerid,budsjettansvarligid,oppsynsigndato,saksigndato,"
                                . " 
budsjettsigndato,merknad,splitt,utbetalingid,utbetalingsigndato,filnavn,overftid,item_type,item_id,"
                                . " belop,godkjentbelop,ordrebelop)"
                                . "values ($values, "
                                . $this->bocommon->moneyformat($data['belop']) 
. ","
                                . 
$this->bocommon->moneyformat($data['godkjentbelop']) . ","
                                . 
$this->bocommon->moneyformat($data['ordrebelop']) . ")";

                        $this->db->query($sql,__LINE__,__FILE__);
//echo 'sql ' . $sql.'<br>';
                }

                function delete_from_fm_ecobilag($id)
                {
                        $sql="delete from fm_ecobilag where id=$id";
                        $this->db->query($sql,__LINE__,__FILE__);
                }

                // Velg ut alle hoved bilag som skal overføres
        function hoved_bilag ($periode,$pre_transfer='')
        {
                        if($pre_transfer)
                        {
                                $sql= "SELECT sum(belop) as belop, bilagsnr 
from fm_ecobilag WHERE periode='$periode' AND utbetalingsigndato IS NULL GROUP 
BY bilagsnr ";
                        }
                        else
                        {
                        $sql="select sum(belop) as belop, bilagsnr from 
fm_ecobilag where periode='$periode'  AND budsjettsigndato is not NULL  AND ( 
saksigndato is not NULL  OR oppsynsigndato is not NULL  ) AND 
utbetalingsigndato is not NULL group by bilagsnr";
                }

                        $this->db->query($sql,__LINE__,__FILE__);
                        $i = 0;
                        while ($this->db->next_record())
                        {
                                $hoved_bilag_temp[$i]['belop']          = 
$this->db->f('belop');
                                $hoved_bilag_temp[$i]['bilagsnr']       = 
$this->db->f('bilagsnr');
                                $i++;
                        }

//_debug_array($hoved_bilag_temp);

                        if ($hoved_bilag_temp)
                        {
                                $i = 0;
                                while(each($hoved_bilag_temp))
                                {
                                        $bilagsnr = 
$hoved_bilag_temp[$i]['bilagsnr'];

                                        $sql= "select 
fm_ecobilag.*,fm_ecouser.initials as saksbehandler from fm_ecobilag $this->join 
fm_ecouser on fm_ecobilag.budsjettansvarligid=fm_ecouser.lid where 
bilagsnr=$bilagsnr ";
                                        
$this->db->query($sql,__LINE__,__FILE__);

                                        $this->db->next_record();

                                        $hoved_bilag[$i]['id']                  
        = $this->db->f('id');
                                        $hoved_bilag[$i]['bilagsnr']            
= $bilagsnr;
                                        $hoved_bilag[$i]['kidnr']               
        = $this->db->f('kidnr');
                                        $hoved_bilag[$i]['typeid']              
        = $this->db->f('typeid');
                                        $hoved_bilag[$i]['kildeid']             
        = $this->db->f('kildeid');
                                        $hoved_bilag[$i]['pmwrkord_code']       
= $this->db->f('pmwrkord_code');
                                        $hoved_bilag[$i]['belop']               
        = $hoved_bilag_temp[$i]['belop'];
                                        $hoved_bilag[$i]['fakturadato']         
= $this->db->f('fakturadato');
                                        $hoved_bilag[$i]['periode']             
        = $this->db->f('periode');
                                        $hoved_bilag[$i]['forfallsdato']        
= $this->db->f('forfallsdato');
                                        $hoved_bilag[$i]['fakturanr']           
= $this->db->f('fakturanr');
                                        $hoved_bilag[$i]['spbudact_code']       
= $this->db->f('spbudact_code');
                                        $hoved_bilag[$i]['regtid']              
        = $this->db->f('regtid');
                                        $hoved_bilag[$i]['artid']               
        = $this->db->f('artid');
                                        $hoved_bilag[$i]['godkjentbelop']       
= $hoved_bilag_temp[$i]['belop'];
                                        $hoved_bilag[$i]['spvend_code']         
= $this->db->f('spvend_code');
                                        $hoved_bilag[$i]['dima']                
        = $this->db->f('dima');
                                        $hoved_bilag[$i]['dimb']                
        = $this->db->f('dimb');
                                        $hoved_bilag[$i]['mvakode']             
        = $this->db->f('mvakode');
                                        $hoved_bilag[$i]['dimd']                
        = $this->db->f('dimd');
                                        if($this->db->f('oppsynsmannid'))
                                        {
                                                
$hoved_bilag[$i]['oppsynsmannid']       = $this->db->f('oppsynsmannid');
                                        }
                                        if($this->db->f('saksbehandlerid'))
                                        {
                                                
$hoved_bilag[$i]['saksbehandlerid']     = $this->db->f('saksbehandlerid');
                                        }

                                        $hoved_bilag[$i]['budsjettansvarligid'] 
= $this->db->f('budsjettansvarligid');

                                        if($this->db->f('oppsynsigndato'))
                                        {
                                                
$hoved_bilag[$i]['oppsynsigndato']      = $this->db->f('oppsynsigndato');
                                        }
                                        if($this->db->f('saksigndato'))
                                        {
                                                $hoved_bilag[$i]['saksigndato'] 
= $this->db->f('saksigndato');
                                        }

                                        $hoved_bilag[$i]['budsjettsigndato']    
= $this->db->f('budsjettsigndato');
                                        $hoved_bilag[$i]['merknad']             
                = $this->db->f('merknad');
                                        $hoved_bilag[$i]['splitt']              
                = $this->db->f('splitt');
                                        $hoved_bilag[$i]['utbetalingid']        
        = $this->db->f('utbetalingid');
                                        $hoved_bilag[$i]['utbetalingsigndato']  
= $this->db->f('utbetalingsigndato');
                                        $hoved_bilag[$i]['saksbehandler']       
        = $this->db->f('saksbehandler');
                                        $i++;
                                }
                        }
//_debug_array($hoved_bilag);

                        return $hoved_bilag;
        }

                //Velg ut alle underbilag

                function select_underbilag ($bilagsnr)
                {
                        $sql= "SELECT fm_ecobilag.* 
,fm_part_of_town.district_id FROM (fm_location1 $this->join fm_part_of_town ON 
fm_location1.part_of_town_id = fm_part_of_town.part_of_town_id) $this->join 
fm_ecobilag ON fm_location1.loc1 = fm_ecobilag.loc1 WHERE bilagsnr='$bilagsnr'";

                        $this->db->query($sql,__LINE__,__FILE__);
                        $i = 0;
                        while ($this->db->next_record())
                        {
                                $underbilag[$i]['id']   = $this->db->f('id');
                                $underbilag[$i]['bilagsnr']     = 
$this->db->f('bilagsnr');
                                $underbilag[$i]['kidnr']        = 
$this->db->f('kidnr');
                                $underbilag[$i]['typeid']       = 
$this->db->f('typeid');
                                $underbilag[$i]['kildeid']      = 
$this->db->f('kildeid');
                                $underbilag[$i]['pmwrkord_code']        = 
$this->db->f('pmwrkord_code');
                                $underbilag[$i]['belop']        = 
$this->db->f('belop');
                                $underbilag[$i]['fakturadato']  = 
$this->db->f('fakturadato');
                                $underbilag[$i]['periode']      = 
$this->db->f('periode');
                                $underbilag[$i]['forfallsdato'] = 
$this->db->f('forfallsdato');
                                $underbilag[$i]['fakturanr']    = 
$this->db->f('fakturanr');
                                $underbilag[$i]['spbudact_code']        = 
$this->db->f('spbudact_code');
                                $underbilag[$i]['regtid']       = 
$this->db->f('regtid');
                                $underbilag[$i]['artid']        = 
$this->db->f('artid');
                                $underbilag[$i]['godkjentbelop']        = 
$this->db->f('godkjentbelop');
                                $underbilag[$i]['spvend_code']  = 
$this->db->f('spvend_code');
                                $underbilag[$i]['dima'] = $this->db->f('dima');
                                $underbilag[$i]['loc1'] = $this->db->f('loc1');
                                $underbilag[$i]['dimb'] = $this->db->f('dimb');
                                $underbilag[$i]['mvakode']      = 
$this->db->f('mvakode');
                                $underbilag[$i]['dimd'] = $this->db->f('dimd');
                                $underbilag[$i]['project_id']   = 
$this->db->f('project_id');
                                $underbilag[$i]['kostra_id']    = 
$this->db->f('kostra_id');
                                if($this->db->f('oppsynsmannid'))
                                {
                                        $underbilag[$i]['oppsynsmannid']        
= $this->db->f('oppsynsmannid');
                                }
                                if($this->db->f('saksbehandlerid'))
                                {
                                        $underbilag[$i]['saksbehandlerid']      
= $this->db->f('saksbehandlerid');
                                }

                                $underbilag[$i]['budsjettansvarligid']  = 
$this->db->f('budsjettansvarligid');

                                if($this->db->f('oppsynsigndato'))
                                {
                                        $underbilag[$i]['oppsynsigndato']       
= $this->db->f('oppsynsigndato');
                                }
                                if($this->db->f('saksigndato'))
                                {
                                        $underbilag[$i]['saksigndato']  = 
$this->db->f('saksigndato');
                                }

                                $underbilag[$i]['budsjettsigndato']     = 
$this->db->f('budsjettsigndato');
                                $underbilag[$i]['merknad']      = 
$this->db->f('merknad');
                                $underbilag[$i]['splitt']       = 
$this->db->f('splitt');
                                $underbilag[$i]['utbetalingid'] = 
$this->db->f('utbetalingid');
                                $underbilag[$i]['utbetalingsigndato']   = 
$this->db->f('utbetalingsigndato');
                                $underbilag[$i]['district_id']  = 
$this->db->f('district_id');
                                $underbilag[$i]['item_type']    = 
$this->db->f('item_type');
                                $underbilag[$i]['item_id']      = 
$this->db->f('item_id');
                                $i++;
                        }

                        return $underbilag;
                }

/*              function update_avvik($avvik)
                {
                }

*/
                function log_to_deviation_table($oRsBilag)
                {
                        $bilagsnr=$oRsBilag['bilagsnr'];
                        $fakturadato=$oRsBilag['fakturadato'];
                        $forfallsdato=$oRsBilag['forfallsdato'];
                        $oppsynsmannid=$oRsBilag['oppsynsmannid'];
                        $oppsynsigndato=$oRsBilag['oppsynsigndato'];
                        $saksbehandlerid=$oRsBilag['saksbehandlerid'];
                        $saksigndato=$oRsBilag['saksigndato'];
                        $budsjettansvarligid=$oRsBilag['budsjettansvarligid'];
                        $budsjettsigndato=$oRsBilag['budsjettsigndato'];
                        $artid=$oRsBilag['artid'];
                        $spvend_code=$oRsBilag['spvend_code'];
                        $belop=$oRsBilag['belop'];
                        $godkjentbelop=$oRsBilag['godkjentbelop'];

                        $sql="INSERT INTO fm_ecoavvik 
(bilagsnr,fakturadato,forfallsdato,oppsynsmannid,oppsynsigndato,saksbehandlerid,saksigndato,budsjettansvarligid,budsjettsigndato,artid,spvend_code,belop,godkjentbelop)
  values "
                        . 
"($bilagsnr','$fakturadato','$forfallsdato','$oppsynsmannid','$oppsynsigndato','$saksbehandlerid','$saksigndato','$budsjettansvarligid','$budsjettsigndato','$artid','$spvend_code','$belop','$godkjentbelop')";
                        $this->db->query($sql,__LINE__,__FILE__);
                }

                function delete_avvik($bilagsnr)
                {
                        $sql="delete from fm_ecoavvik where 
bilagsnr='$bilagsnr'";
                        $this->db->query($sql,__LINE__,__FILE__);
                }

                function delete_invoice($bilagsnr)
                {
                        $sql="delete from fm_ecobilagoverf where 
bilagsnr='$bilagsnr'";
                        $this->db->query($sql,__LINE__,__FILE__);
                }

                //Logg transaksjon
                function log_transaction($batchid,$bilagid,$message='')
                {
                        $tid=date($this->datetimeformat);
                        $sql= "insert into fm_ecologg 
(batchid,ecobilagid,melding,tid) values ('$batchid','$bilagid' 
,'$message','$tid')";
                        $this->db->query($sql,__LINE__,__FILE__);
                }

                function increment_batchid()
                {
                        $this->db->query("update fm_idgenerator  set value = 
value + 1 where name = 'Ecobatchid'");
                        $this->db->query("select value from fm_idgenerator  
where name = 'Ecobatchid'");
                        $this->db->next_record();
                        $bilagsnr = $this->db->f('value');
                        return $bilagsnr;
                }

                function next_batchid()
                {

                        $this->db->query("select value from fm_idgenerator  
where name = 'Ecobatchid'");
                        $this->db->next_record();
                        $batchid = $this->db->f('value')+1;

                        return $batchid;
                }

                function get_responsible($b_abbount_id)
                {
                        $this->db->query("select account_lid from fm_b_account 
$this->join phpgw_accounts on fm_b_account.responsible = 
phpgw_accounts.account_id where fm_b_account.id = '$b_abbount_id'");
                        $this->db->next_record();
                        $responsible = $this->db->f('account_lid');
                        return $responsible;
                }
        }
?>
\ No newline at end of file
Index: property/inc/class.soactor.inc.php
diff -u property/inc/class.soactor.inc.php:1.10 
property/inc/class.soactor.inc.php:1.11
--- property/inc/class.soactor.inc.php:1.10     Fri Jan 27 14:05:43 2006
+++ property/inc/class.soactor.inc.php  Mon Jan 30 22:14:19 2006
@@ -1,912 +1,913 @@
-<?php
-       /**
-       * phpGroupWare - property: a Facilities Management System.
-       *
-       * @author Sigurd Nes <address@hidden>
-       * @copyright Copyright (C) 2003-2005 Free Software Foundation, Inc. 
http://www.fsf.org/
-       * @license http://www.gnu.org/licenses/gpl.html GNU General Public 
License
-       * @internal Development of this application was funded by 
http://www.bergen.kommune.no/bbb_/ekstern/
-       * @package property
-       * @subpackage admin
-       * @version $Id: class.soactor.inc.php,v 1.10 2006/01/27 14:05:43 
sigurdne Exp $
-       */
-
-       /**
-        * Description
-        * @package property
-        */
-
-       class soactor
-       {
-               var $role;
-
-               function soactor()
-               {
-                       $this->currentapp       = 
$GLOBALS['phpgw_info']['flags']['currentapp'];
-                       $this->account  = 
$GLOBALS['phpgw_info']['user']['account_id'];
-                       $this->bocommon         = 
CreateObject($this->currentapp.'.bocommon');
+<?php
+       /**
+       * phpGroupWare - property: a Facilities Management System.
+       *
+       * @author Sigurd Nes <address@hidden>
+       * @copyright Copyright (C) 2003-2005 Free Software Foundation, Inc. 
http://www.fsf.org/
+       * @license http://www.gnu.org/licenses/gpl.html GNU General Public 
License
+       * @internal Development of this application was funded by 
http://www.bergen.kommune.no/bbb_/ekstern/
+       * @package property
+       * @subpackage admin
+       * @version $Id: class.soactor.inc.php,v 1.11 2006/01/30 22:14:19 
sigurdne Exp $
+       */
+
+       /**
+        * Description
+        * @package property
+        */
+
+       class soactor
+       {
+               var $role;
+
+               function soactor()
+               {
+                       $this->currentapp       = 
$GLOBALS['phpgw_info']['flags']['currentapp'];
+                       $this->account  = 
$GLOBALS['phpgw_info']['user']['account_id'];
+                       $this->bocommon         = 
CreateObject($this->currentapp.'.bocommon');
                        $this->db               = $this->bocommon->new_db();
-                       $this->db2              = $this->bocommon->new_db();
-                       $this->join                     = $this->bocommon->join;
-                       $this->left_join        = $this->bocommon->left_join;
-                       $this->like                     = $this->bocommon->like;
-               }
-
-               function select_category_list()
-               {
-                       $table = 'fm_' . $this->role . '_category';
-                       $this->db->query("SELECT id,descr FROM $table  ORDER BY 
descr ");
-
-                       $i = 0;
-                       while ($this->db->next_record())
-                       {
-                               $categories[$i]['id']                           
= $this->db->f('id');
-                               $categories[$i]['name']                         
= stripslashes($this->db->f('descr'));
-                               $i++;
-                       }
-                       return $categories;
-               }
-
-               function read($data)
-               {
-                       if(is_array($data))
-                       {
-                               $start  = 
(isset($data['start'])?$data['start']:0);
-                               $filter = 
(isset($data['filter'])?$data['filter']:'none');
-                               $query = 
(isset($data['query'])?$data['query']:'');
-                               $sort = 
(isset($data['sort'])?$data['sort']:'DESC');
-                               $order = 
(isset($data['order'])?$data['order']:'');
-                               $cat_id = 
(isset($data['cat_id'])?$data['cat_id']:'');
-                               $allrows        = 
(isset($data['allrows'])?$data['allrows']:'');
-                               $member_id      = 
(isset($data['member_id'])?$data['member_id']:0);
-                       }
-
-                       $sql = $this->bocommon->fm_cache('sql_actor_' . 
$this->role . $lookup);
-
-                       $entity_table = 'fm_' . $this->role;
-                       $category_table = 'fm_' . $this->role . '_category';
-                       $choice_table = 'fm_' . $this->role . '_choice';
-                       $attribute_table = 'fm_' . $this->role . '_attribute';
-                       if(!$sql)
-                       {
-                               $cols = $entity_table . 
".*,$category_table.descr as category";
-
-                               $cols_return[]                          = 'id';
-                               $uicols['input_type'][]         = 'text';
-                               $uicols['name'][]                       = 'id';
-                               $uicols['descr'][]                      = 
lang('ID');
-                               $uicols['statustext'][]         = lang('ID');
-
-                               $cols_return[]                          = 'id';
-                               $uicols['input_type'][]         = 'hidden';
-                               $uicols['name'][]                       = 'id';
-                               $uicols['descr'][]                      = false;
-                               $uicols['statustext'][]         = false;
-
-                               $cols_return[]                          = 
'category';
-                               $uicols['input_type'][]         = 'text';
-                               $uicols['name'][]                       = 
'category';
-                               $uicols['descr'][]                      = 
lang('category');
-                               $uicols['statustext'][]         = 
lang('category');
-
-
-                               $paranthesis .='(';
-
-                               $joinmethod .= " $this->join  " . $entity_table 
. "_category ON ( $entity_table" . ".category =" .$entity_table . 
"_category.id))";
-
-                               $from .= " FROM $paranthesis $entity_table ";
-
-                               $sql = "SELECT $cols $from $joinmethod";
-
-                               $this->bocommon->fm_cache('sql_actor_' . 
$this->role . $lookup,$sql);
-                               $this->bocommon->fm_cache('uicols_actor_' . 
$this->role . $lookup,$uicols);
-                               $this->bocommon->fm_cache('cols_return_actor_' 
. $this->role . $lookup,$cols_return);
-
-                       }
-                       else
-                       {
-                               $uicols                                         
        = $this->bocommon->fm_cache('uicols_actor_'. $this->role . $lookup);
-                               $cols_return                                    
= $this->bocommon->fm_cache('cols_return_actor_' . $this->role . $lookup);
-                               if($lookup)
-                               {
-                                       $admin_entity   = 
CreateObject($this->currentapp.'_soadmin_entity');
-                                       $category = 
$admin_entity->read_single_category($entity_id,$cat_id);
-                               }
-                       }
-
-                       $i      = count($uicols['name']);
-
-                       $this->db->query("SELECT * FROM $attribute_table WHERE 
list=1");
-                       while ($this->db->next_record())
-                       {
-                               $uicols['input_type'][]         = 'text';
-                               $uicols['name'][]                       = 
$this->db->f('column_name');
-                               $uicols['descr'][]                      = 
$this->db->f('input_text');
-                               $uicols['statustext'][]         = 
$this->db->f('statustext');
-                               $uicols['datatype'][$i]         = 
$this->db->f('datatype');
-                               $cols_return_extra[]= array(
-                                       'name'  => $this->db->f('column_name'),
-                                       'datatype'      => 
$this->db->f('datatype'),
-                                       'attrib_id'     => $this->db->f('id')
-                               );
-
-                               $i++;
-                       }
-
-                       
$user_columns=$GLOBALS['phpgw_info']['user']['preferences'][$this->currentapp]['actor_columns_'
 . $this->role];
-
-//_debug_array($user_columns);
-
-                       if (isset($user_columns) AND is_array($user_columns) 
AND $user_columns[0])
-                       {
-                               foreach($user_columns as $column_id)
-                               {
-                                       $this->db->query("SELECT * FROM 
$attribute_table WHERE id= $column_id");
-
-                                       $this->db->next_record();
-                                       $uicols['input_type'][]         = 
'text';
-                                       $uicols['name'][]                       
= $this->db->f('column_name');
-                                       $uicols['descr'][]                      
= $this->db->f('input_text');
-                                       $uicols['statustext'][]         = 
$this->db->f('statustext');
-                                       $uicols['datatype'][$i]         = 
$this->db->f('datatype');
-                                       $cols_return_extra[]= array(
-                                               'name'  => 
$this->db->f('column_name'),
-                                               'datatype'      => 
$this->db->f('datatype'),
-                                               'attrib_id'     => 
$this->db->f('id')
-                                       );
-                                       $i++;
-                               }
-                       }
-
-                       $this->uicols   = $uicols;
-
-//_debug_array($cols_return_extra);
-                       if ($order)
-                       {
-                               $ordermethod = " order by $entity_table.$order 
$sort";
-                       }
-                       else
-                       {
-                               $ordermethod = " order by $entity_table.id 
DESC";
-                       }
-
-                       $where= 'WHERE';
-
-/*                     if ($filter=='all')
-                       {
-                               if (is_array($grants))
-                               {
-                                       while (list($user) = each($grants))
-                                       {
-                                               $public_user_list[] = $user;
-                                       }
-                                       reset($public_user_list);
-                                       $filtermethod .= " $where ( 
$entity_table.user_id IN(" . implode(',',$public_user_list) . "))";
-
-                                       $where= 'AND';
-                               }
-
-                       }
-                       else
-                       {
-                               $filtermethod = " $where 
$entity_table.user_id=$filter ";
-                               $where= 'AND';
-                       }
-*/
-
-                       if ($cat_id)
-                       {
-                               $filtermethod .= " $where 
$entity_table.category='$cat_id' ";
-                               $where= 'AND';
-                       }
-
-                       if ($member_id>0)
-                       {
-                               $filtermethod .= " $where 
$entity_table.member_of $this->like '%,$member_id,%' ";
-                               $where= 'AND';
-                       }
-
-                       if ($status)
-                       {
-                               $filtermethod .= " $where 
$entity_table.status='$status' ";
-                               $where= 'AND';
-                       }
-
-
-                       if($query)
-                       {
-                               $query = ereg_replace("'",'',$query);
-                               $query = ereg_replace('"','',$query);
-
-                               $filtermethod .= " $where $entity_table.id ='" 
. (int)$query . "'";
-                               $where= 'OR';
-
-                               $this->db->query("SELECT * FROM 
$attribute_table where search='1'");
-
-                               while ($this->db->next_record())
-                               {
-                                       if($this->db->f('datatype')=='V' || 
$this->db->f('datatype')=='email' || $this->db->f('datatype')=='CH')
-                                       {
-                                               $querymethod[]= 
"$entity_table." . $this->db->f('column_name') . " $this->like '%$query%'";
-                                       }
-                                       else
-                                       {
-                                               $querymethod[]= 
"$entity_table." . $this->db->f('column_name') . " = '$query'";
-                                       }
-                               }
-
-                               if (isset($querymethod) AND 
is_array($querymethod))
-                               {
-                                       $querymethod = " $where (" . implode (' 
OR ',$querymethod) . ')';
-                                       $where = 'AND';
-                               }
-                       }
-
-                       $sql .= " $filtermethod $querymethod";
-//echo $sql;
-
-                       $this->db2->query($sql,__LINE__,__FILE__);
-                       $this->total_records = $this->db2->num_rows();
-                       if(!$allrows)
-                       {
-                               $this->db->limit_query($sql . 
$ordermethod,$start,__LINE__,__FILE__);
-                       }
-                       else
-                       {
-                               $this->db->query($sql . 
$ordermethod,__LINE__,__FILE__);
-                       }
-
-                       $j=0;
-                       $n=count($cols_return);
-//_debug_array($cols_return);
-                       $contacts                       = 
CreateObject('phpgwapi.contacts');
-
-                       while ($this->db->next_record())
-                       {
-                               for ($i=0;$i<$n;$i++)
-                               {
-                                       $actor_list[$j][$cols_return[$i]] = 
$this->db->f($cols_return[$i]);
-                                       $actor_list[$j]['grants'] = 
(int)$grants[$this->db->f('user_id')];
-                               }
-
-                               for ($i=0;$i<count($cols_return_extra);$i++)
-                               {
-                                       $value='';
-                                       
$value=$this->db->f($cols_return_extra[$i]['name']);
-
-                                       
if(($cols_return_extra[$i]['datatype']=='R' || 
$cols_return_extra[$i]['datatype']=='LB') && $value):
-                                       {
-                                               $sql="SELECT value FROM 
$choice_table where attrib_id=" .$cols_return_extra[$i]['attrib_id']. "  AND 
id=" . $value;
-                                               $this->db2->query($sql);
-                                               $this->db2->next_record();
-                                               
$actor_list[$j][$cols_return_extra[$i]['name']] = $this->db2->f('value');
-                                       }
-                                       
elseif($cols_return_extra[$i]['datatype']=='AB' && $value):
-                                       {
-                                               $contact_data   = 
$contacts->read_single_entry($value,array('n_given'=>'n_given','n_family'=>'n_family','email'=>'email'));
-                                               
$actor_list[$j][$cols_return_extra[$i]['name']] = $contact_data[0]['n_family'] 
. ', ' . $contact_data[0]['n_given'];
-
-/*                                             $sql="SELECT org_name FROM 
phpgw_addressbook where id=$value";
-                                               $this->db2->query($sql);
-                                               $this->db2->next_record();
-                                               
$actor_list[$j][$cols_return_extra[$i]['name']] = $this->db2->f('org_name');
-*/
-                                       }
-                                       
elseif($cols_return_extra[$i]['datatype']=='VENDOR' && $value):
-                                       {
-                                               $sql="SELECT org_name FROM 
fm_vendor where id=$value";
-                                               $this->db2->query($sql);
-                                               $this->db2->next_record();
-                                               
$actor_list[$j][$cols_return_extra[$i]['name']] = $this->db2->f('org_name');
-                                       }
-                                       
elseif($cols_return_extra[$i]['datatype']=='CH' && $value):
-                                       {
-                                               $ch= unserialize($value);
-
-                                               if (isset($ch) AND 
is_array($ch))
-                                               {
-                                                       for 
($k=0;$k<count($ch);$k++)
-                                                       {
-                                                               $sql="SELECT 
value FROM $choice_table where attrib_id=" 
.$cols_return_extra[$i]['attrib_id']. "  AND id=" . $ch[$k];
-                                                               
$this->db2->query($sql);
-                                                               while 
($this->db2->next_record())
-                                                               {
-                                                                       
$ch_value[]=$this->db2->f('value');
-                                                               }
-                                                       }
-                                                       
$actor_list[$j][$cols_return_extra[$i]['name']] = @implode(",", $ch_value);
-                                                       unset($ch_value);
-                                               }
-                                       }
-                                       
elseif($cols_return_extra[$i]['datatype']=='D' && $value):
-                                       {
-//html_print_r($value);
-
-                                               
$actor_list[$j][$cols_return_extra[$i]['name']]=date($GLOBALS['phpgw_info']['user']['preferences']['common']['dateformat'],strtotime($value));
-                                       }
-                                       else:
-                                       {
-                                               
$actor_list[$j][$cols_return_extra[$i]['name']]=$value;
-                                       }
-                                       endif;
-                               }
-
-                               $j++;
-                       }
-//html_print_r($actor_list);
-                       return $actor_list;
-               }
-
-
-               function read_single($data)
-               {
-                       $attribute_table = 'fm_' . $this->role . '_attribute';
-                       $table = 'fm_' . $this->role;
-
-                       $actor_id =$data['actor_id'];
-
-                       $this->db->query("SELECT * FROM $attribute_table ORDER 
BY attrib_sort");
-
-                       while ($this->db->next_record())
-                       {
-                               $actor['attributes'][] = array
-                               (
-                                       'attrib_id'             => 
$this->db->f('id'),
-                                       'name'                  => 
$this->db->f('column_name'),
-                                       'input_text'    => 
stripslashes($this->db->f('input_text')),
-                                       'statustext'    => 
stripslashes($this->db->f('statustext')),
-                                       'datatype'              => 
$this->db->f('datatype')
-                               );
-                       }
-
-                       if($actor_id)
-                       {
-                               $this->db->query("SELECT * from $table where 
id='$actor_id'");
-
-                               if($this->db->next_record())
-                               {
-                                       $actor['id']                    = 
(int)$this->db->f('id');
-                                       $actor['entry_date']            = 
$this->db->f('entry_date');
-                                       $actor['cat_id']                        
= $this->db->f('category');
-                                       $actor['member_of']                     
= explode(',',$this->db->f('member_of'));
-
-                                       for 
($i=0;$i<count($actor['attributes']);$i++)
-                                       {
-                                               
$actor['attributes'][$i]['value']       = 
$this->db->f($actor['attributes'][$i]['name']);
-                                               
$actor['attributes'][$i]['datatype_text']       = 
$this->bocommon->translate_datatype($actor['attributes'][$i]['datatype']);
-                                       }
-
-                               }
-                       }
-                       return $actor;
-               }
-
-               function add($actor,$values_attribute='')
-               {
-                       $table = 'fm_' . $this->role;
-
-                       if($actor['member_of'])
-                       {
-                               $actor['member_of']=',' . 
implode(',',$actor['member_of']) . ',';
-                       }
-
-                       while (is_array($actor['extra']) && 
list($input_name,$value) = each($actor['extra']))
-                       {
-                               if($value)
-                               {
-                                       $cols[] = $input_name;
-                                       $vals[] = $value;
-                               }
-                       }
-
-                       if (isset($values_attribute) AND 
is_array($values_attribute))
-                       {
-                               foreach($values_attribute as $entry)
-                               {
-                                       if($entry['value'])
-                                       {
-                                               $cols[] = $entry['name'];
-                                               $vals[] = $entry['value'];
-                                       }
-                               }
-                       }
-
-                       if($this->role == 'vendor')
-                       {
-                               $cols[] = 'member_of';
-                               $vals[] = $actor['member_of'];
-                       }
-
-                       if($cols)
-                       {
-                               $cols   = "," . implode(",", $cols);
-                               $vals   = "," . 
$this->bocommon->validate_db_insert($vals);
-                       }
-
-                       $this->db->transaction_begin();
-                       if($actor['new_actor_id'])
-                       {
-                               $id = $actor['new_actor_id'];
-                       }
-                       else
-                       {
-                               $id = $this->bocommon->next_id($table);
-                       }
-
-                       $this->db->query("INSERT INTO $table 
(id,entry_date,category $cols) "
-                               . "VALUES ($id,'" . time() . "','" . 
$actor['cat_id'] . "' $vals)");
-
-                       $receipt['actor_id']= 
$id;//$this->db->get_last_insert_id($table,'id');
-
-                       $receipt['message'][] = array('msg'=>lang('actor %1 has 
been saved',$receipt['actor_id']));
-
-                       $this->db->transaction_commit();
-                       return $receipt;
-               }
-
-               function edit($actor,$values_attribute='')
-               {
-//_debug_array($actor);
-//_debug_array($values_attribute);
-                       $table = 'fm_' . $this->role;
-
-                       if($actor['member_of'])
-                       {
-                               $actor['member_of']=',' . 
implode(',',$actor['member_of']) . ',';
-                       }
-
-                       while (is_array($actor['extra']) && 
list($column,$value) = each($actor['extra']))
-                       {
-                               $value_set[$column]     = $value;
-                       }
-
-                       if (isset($values_attribute) AND 
is_array($values_attribute))
-                       {
-                               foreach($values_attribute as $entry)
-                               {
-                                       if($entry['datatype']!='AB' && 
$entry['datatype']!='VENDOR')
-                                       {
-                                               $value_set[$entry['name']]      
= $entry['value'];
-                                       }
-                               }
-                       }
-
-                       $value_set['entry_date']        = time();
-                       $value_set['category']  = $actor['cat_id'];
-
-                       if($this->role == 'vendor')
-                       {
-                               $value_set['member_of'] = $actor['member_of'];
-                       }
-
-                       if($value_set)
-                       {
-                               $value_set      = 
$this->bocommon->validate_db_update($value_set);
-                       }
-
-                       $this->db->query("UPDATE $table set $value_set WHERE 
id=" . intval($actor['actor_id']));
-
-                       $receipt['actor_id']= $actor['actor_id'];
-                       $receipt['message'][] = array('msg'=>lang('actor %1 has 
been edited',$actor['actor_id']));
-                       return $receipt;
-               }
-
-               function delete($actor_id)
-               {
-                       $table = 'fm_' . $this->role;
-                       $this->db->query('DELETE FROM $table WHERE id=' . 
intval($actor_id));
-               }
-
-               function read_attrib($data)
-               {
-                       $attribute_table = 'fm_' . $this->role . '_attribute';
-//html_print_r($data);
-                       if(is_array($data))
-                       {
-                               if ($data['start'])
-                               {
-                                       $start=$data['start'];
-                               }
-                               else
-                               {
-                                       $start=0;
-                               }
-                               $query = 
(isset($data['query'])?$data['query']:'');
-                               $sort = 
(isset($data['sort'])?$data['sort']:'DESC');
-                               $order = 
(isset($data['order'])?$data['order']:'');
-                               $allrows = 
(isset($data['allrows'])?$data['allrows']:'');
-                               $column_list = 
(isset($data['column_list'])?$data['column_list']:'');
-                       }
-
-                       $where = 'WHERE';
-                       if ($column_list)
-                       {
-                               $filtermethod = " $where list !=1 or list is 
null";
-                               $where = 'AND';
-
-                       }
-                       if ($order)
-                       {
-                               $ordermethod = " order by $order $sort";
-
-                       }
-                       else
-                       {
-                               $ordermethod = ' order by attrib_sort asc';
-                       }
-
-                       if($query)
-                       {
-                               $query = ereg_replace("'",'',$query);
-                               $query = ereg_replace('"','',$query);
-
-                               $querymethod = " $where 
($attribute_table.input_text $this->like '%$query%' or 
$attribute_table.column_name $this->like '%$query%')";
-                       }
-
-                       $sql = "SELECT * FROM $attribute_table $filtermethod 
$querymethod";
-
-//echo $sql;
-                       $this->db2->query($sql,__LINE__,__FILE__);
-                       $this->total_records = $this->db2->num_rows();
-                       if(!$allrows)
-                       {
-                               $this->db->limit_query($sql . 
$ordermethod,$start,__LINE__,__FILE__);
-                       }
-                       else
-                       {
-                               $this->db->query($sql . 
$ordermethod,__LINE__,__FILE__);
-                       }
-
-                       while ($this->db->next_record())
-                       {
-                               $attrib[] = array
-                               (
-                                       'id'                    => 
$this->db->f('id'),
-                                       'attrib_sort'   => 
$this->db->f('attrib_sort'),
-                                       'list'                  => 
$this->db->f('list'),
-                                       'lookup_form'   => 
$this->db->f('lookup_form'),
-                                       'column_name'   => 
$this->db->f('column_name'),
-                                       'name'                  => 
$this->db->f('input_text'),
-                                       'size'                  => 
$this->db->f('size'),
-                                       'statustext'    => 
$this->db->f('statustext'),
-                                       'input_text'    => 
$this->db->f('input_text'),
-                                       'type_name'             => 
$this->db->f('type'),
-                                       'datatype'              => 
$this->db->f('datatype'),
-                                       'search'                => 
$this->db->f('search')
-                               );
-                       }
-                       return $attrib;
-               }
-
-               function read_single_attrib($id)
-               {
-                       $attribute_table = 'fm_' . $this->role . '_attribute';
-
-                       $sql = "SELECT * FROM $attribute_table where id=$id";
-
-                       $this->db->query($sql);
-
-                       if($this->db->next_record())
-                       {
-                               $attrib['id']                                   
        = $this->db->f('id');
-                               $attrib['column_name']                          
= $this->db->f('column_name');
-                               $attrib['input_text']                           
= $this->db->f('input_text');
-                               $attrib['statustext']                           
= $this->db->f('statustext');
-                               $attrib['column_info']['precision']     = 
$this->db->f('precision_');
-                               $attrib['column_info']['scale']         = 
$this->db->f('scale');
-                               $attrib['column_info']['default']       = 
$this->db->f('default_value');
-                               $attrib['column_info']['nullable']      = 
$this->db->f('nullable');
-                               $attrib['column_info']['type']          = 
$this->db->f('datatype');
-                               $attrib['type_name']                            
= $this->db->f('type_name');
-                               $attrib['lookup_form']                          
= $this->db->f('lookup_form');
-                               $attrib['list']                                 
        = $this->db->f('list');
-                               $attrib['search']                               
        = $this->db->f('search');
-                               if($this->db->f('datatype')=='R' || 
$this->db->f('datatype')=='CH' || $this->db->f('datatype')=='LB')
-                               {
-                                       $attrib['choice'] = 
$this->read_attrib_choice($id);
-                               }
-
-                               return $attrib;
-                       }
-               }
-
-               function read_attrib_choice($attrib_id)
-               {
-                       $choice_table = 'fm_' . $this->role . '_choice';
-                       $sql = "SELECT * FROM $choice_table WHERE 
attrib_id=$attrib_id";
-                       $this->db->query($sql);
-
-                       while ($this->db->next_record())
-                       {
-                               $choice[] = array
-                               (
-                                       'id'    => $this->db->f('id'),
-                                       'value' => $this->db->f('value')
-                               );
-
-                       }
-                       return $choice;
-               }
-
-               function add_attrib($attrib)
-               {
-                       $table = 'fm_' . $this->role;
-                       $attribute_table = 'fm_' . $this->role . '_attribute';
-                       $attrib['column_name'] = 
$this->db->db_addslashes($attrib['column_name']);
-                       $attrib['input_text'] = 
$this->db->db_addslashes($attrib['input_text']);
-                       $attrib['statustext'] = 
$this->db->db_addslashes($attrib['statustext']);
-                       $attrib['default'] = 
$this->db->db_addslashes($attrib['default']);
-                       $attrib['id'] = 
$this->bocommon->next_id($attribute_table);
-
-                       $sql = "SELECT max(attrib_sort) as max_sort FROM 
$attribute_table";
-                       $this->db->query($sql);
-                       $this->db->next_record();
-                       $attrib_sort    = $this->db->f('max_sort')+1;
-
-                       $values= array(
-                               $attrib['id'],
-                               $attrib['column_name'],
-                               $attrib['input_text'],
-                               $attrib['statustext'],
-                               $attrib['lookup_form'],
-                               $attrib['search'],
-                               $attrib['list'],
-                               $attrib_sort,
-                               $attrib['column_info']['type'],
-                               $attrib['column_info']['precision'],
-                               $attrib['column_info']['scale'],
-                               $attrib['column_info']['default'],
-                               $attrib['column_info']['nullable']
-                               );
-
-                       $values = $this->bocommon->validate_db_insert($values);
-
-                       $this->db->transaction_begin();
-
-                       $this->db->query("INSERT INTO $attribute_table 
(id,column_name, input_text, 
statustext,lookup_form,search,list,attrib_sort,datatype,precision_,scale,default_value,nullable)
 "
-                               . "VALUES ($values)");
-
-                       $receipt['id']= $attrib['id'];
-
-                       if($attrib['column_info']['type']=='email' && 
!$attrib['column_info']['precision'])
-                       {
-                               $attrib['column_info']['precision']=64;
-                       }
-
-                       $attrib['column_info']['type']  = 
$this->bocommon->translate_datatype_insert($attrib['column_info']['type']);
-
-                       if($attrib['column_info']['type']=='int' && 
!$attrib['column_info']['precision'])
-                       {
-                               $attrib['column_info']['precision']=4;
-                       }
-
-                       if(!$attrib['column_info']['default'])
-                       {
-                               unset($attrib['column_info']['default']);
-                       }
-
-                       $this->init_process();
-
-                       
if($this->oProc->AddColumn($table,$attrib['column_name'], 
$attrib['column_info']))
-                       {
-                               $receipt['message'][] = array('msg'     => 
lang('Attribute has been saved')     );
-                               $this->db->transaction_commit();
-
-                       }
-                       else
-                       {
-                               $receipt['error'][] = array('msg'       => 
lang('column could not be added')    );
-                               if($this->db->Transaction)
-                               {
-                                       $GLOBALS['phpgw']->db->rollbacktrans();
-                               }
-                               else
-                               {
-                                       $GLOBALS['phpgw']->db->Execute("DELETE 
FROM $attribute_table WHERE id='" . $receipt['id'] . "'");
-                                       unset($receipt['id']);
-
-                               }
-                       }
-
-                       return $receipt;
-               }
-
-               function init_process()
-               {
-                       $this->oProc                                            
= 
CreateObject('phpgwapi.schema_proc',$GLOBALS['phpgw_info']['server']['db_type']);
-                       $this->oProc->m_odb                                     
= $this->db;
-                       $this->oProc->m_odb->Halt_On_Error      = 'report';
-               }
-
-               function edit_attrib($attrib)
-               {
-                       $attribute_table = 'fm_' . $this->role . '_attribute';
-                       $table = 'fm_' . $this->role;
-
-                       $attrib['column_name'] = 
$this->db->db_addslashes($attrib['column_name']);
-                       $attrib['input_text'] = 
$this->db->db_addslashes($attrib['input_text']);
-                       $attrib['statustext'] = 
$this->db->db_addslashes($attrib['statustext']);
-                       $attrib['default'] = 
$this->db->db_addslashes($attrib['default']);
-
-                       $choice_table = 'fm_' . $this->role . '_choice';
-
-                       $this->db->query("SELECT column_name FROM 
$attribute_table WHERE id='" . $attrib['id']. "'");
-                       $this->db->next_record();
-                       $OldColumnName          = $this->db->f('column_name');
-
-                       $this->db->transaction_begin();
-
-                       $value_set=array(
-                               'input_text'            => 
$attrib['input_text'],
-                               'statustext'            => 
$attrib['statustext'],
-                               'lookup_form'           => 
$attrib['lookup_form'],
-                               'search'                        => 
$attrib['search'],
-                               'list'                          => 
$attrib['list'],
-                               );
-
-                       $value_set      = 
$this->bocommon->validate_db_update($value_set);
-
-                       $this->db->query("UPDATE $attribute_table set 
$value_set WHERE id=" . $attrib['id']);
-
-                       $attrib_type=$attrib['column_info']['type'];
-                       if(($OldColumnName !=$attrib['column_name']) || 
($attrib['column_info']['type'] != $this->db->f('datatype')))
-                       {
-                               $value_set=array(
-                                       'column_name'           => 
$attrib['column_name'],
-                                       'datatype'                      => 
$attrib['column_info']['type'],
-                                       'precision_'            => 
$attrib['column_info']['precision'],
-                                       'scale'                         => 
$attrib['column_info']['scale'],
-                                       'default_value'         => 
$attrib['column_info']['default'],
-                                       'nullable'                      => 
$attrib['column_info']['nullable']
-                                       );
-
-                               $value_set      = 
$this->bocommon->validate_db_update($value_set);
-
-                               $this->db->query("UPDATE $attribute_table set 
$value_set WHERE id=" . $attrib['id']);
-
-                               if($attrib['column_info']['type']=='email' && 
!$attrib['column_info']['precision'])
-                               {
-                                       $attrib['column_info']['precision']=64;
-                               }
-
-                               $attrib['column_info']['type']  = 
$this->bocommon->translate_datatype_insert($attrib['column_info']['type']);
-
-                               if($attrib['column_info']['type']=='int' && 
!$attrib['column_info']['precision'])
-                               {
-                                       $attrib['column_info']['precision']=4;
-                               }
-
-                               if(!$attrib['column_info']['default'])
-                               {
-                                       
unset($attrib['column_info']['default']);
-                               }
-
-                               $this->init_process();
-
-                               
$this->oProc->AlterColumn($table,$OldColumnName,$attrib['column_name'],$attrib['column_info']);
-
-                       }
-
-
-                       if($attrib['new_choice'])
-                       {
-                               $choice_id = 
$this->bocommon->next_id($choice_table ,array('attrib_id'=>$attrib['id']));
-
-                               $values= array(
-                                       $attrib['id'],
-                                       $choice_id,
-                                       $attrib['new_choice']
-                                       );
-
-                               $values = 
$this->bocommon->validate_db_insert($values);
-
-                               $this->db->query("INSERT INTO $choice_table 
(attrib_id,id,value) "
-                               . "VALUES ($values)");
-                       }
-
-
-                       if($attrib['delete_choice'])
-                       {
-                               for 
($i=0;$i<count($attrib['delete_choice']);$i++)
-                               {
-                                       $this->db->query("DELETE FROM 
$choice_table WHERE  attrib_id=" . $attrib['id']  ." AND id=" . 
$attrib['delete_choice'][$i]);
-                               }
-                       }
-
-//html_print_r($attrib_type);
-                       if($attrib_type!='R' && $attrib_type!='CH' && 
$attrib_type!='LB')
-                       {
-                               $this->db->query("DELETE FROM $choice_table 
WHERE  attrib_id=" . $attrib['id']);
-                       }
-
-
-                       $this->db->transaction_commit();
-
-                       $receipt['message'][] = array('msg'     => 
lang('Attribute has been edited'));
-
-
-                       return $receipt;
-
-               }
-               function resort_attrib($data)
-               {
-//html_print_r($data);
-                       $attribute_table = 'fm_' . $this->role . '_attribute';
-                       if(is_array($data))
-                       {
-                               $resort = 
(isset($data['resort'])?$data['resort']:'up');
-                               $id = (isset($data['id'])?$data['id']:'');
-                       }
-
-                       $sql = "SELECT attrib_sort FROM $attribute_table where 
id=$id";
-                       $this->db->query($sql);
-                       $this->db->next_record();
-                       $attrib_sort    = $this->db->f('attrib_sort');
-                       $sql = "SELECT max(attrib_sort) as max_sort FROM 
$attribute_table";
-                       $this->db->query($sql);
-                       $this->db->next_record();
-                       $max_sort       = $this->db->f('max_sort');
-                       switch($resort)
-                       {
-                               case 'up':
-                                       if($attrib_sort>1)
-                                       {
-                                               $sql = "UPDATE $attribute_table 
set attrib_sort=$attrib_sort WHERE attrib_sort =" . ($attrib_sort-1);
-                                               $this->db->query($sql);
-                                               $sql = "UPDATE $attribute_table 
set attrib_sort=" . ($attrib_sort-1) ." WHERE id=$id";
-                                               $this->db->query($sql);
-                                       }
-                                       break;
-                               case 'down':
-                                       if($max_sort > $attrib_sort)
-                                       {
-                                               $sql = "UPDATE $attribute_table 
set attrib_sort=$attrib_sort WHERE attrib_sort =" . ($attrib_sort+1);
-                                               $this->db->query($sql);
-                                               $sql = "UPDATE $attribute_table 
set attrib_sort=" . ($attrib_sort+1) ." WHERE id=$id";
-                                               $this->db->query($sql);
-                                       }
-                                       break;
-                               default:
-                                       return;
-                                       break;
-                       }
-               }
-               function delete_attrib($attrib_id)
-               {
-                       $table = 'fm_' . $this->role;
-                       $attribute_table = 'fm_' . $this->role . '_attribute';
-                       $this->init_process();
-
-                       $sql = "SELECT * FROM $attribute_table WHERE 
id=$attrib_id";
-
-                       $this->db->query($sql);
-                       $this->db->next_record();
-                       $ColumnName             = $this->db->f('column_name');
-
-                       if($this->oProc->DropColumn($table,'', $ColumnName))
-                       {
-                               $sql = "SELECT attrib_sort FROM 
$attribute_table where id=$attrib_id";
-                               $this->db->query($sql);
-                               $this->db->next_record();
-                               $attrib_sort    = $this->db->f('attrib_sort');
-                               $sql2 = "SELECT max(attrib_sort) as max_sort 
FROM $attribute_table";
-                               $this->db->query($sql2);
-                               $this->db->next_record();
-                               $max_sort       = $this->db->f('max_sort');
-                               if($max_sort>$attrib_sort)
-                               {
-                                       $sql = "UPDATE $attribute_table set 
attrib_sort=attrib_sort-1 WHERE attrib_sort > $attrib_sort";
-                                       $this->db->query($sql);
-                               }
-
-                               $this->db->query("DELETE FROM $attribute_table 
WHERE id=$attrib_id");
-                       }
-                       else
-                       {
-                               $receipt['error'][] = array('msg'       => 
lang('Attribute has NOT been deleted'));
-                       }
-
-                       return $receipt;
-               }
-       }
-?>
+                       $this->db2              = $this->bocommon->new_db();
+
+                       $this->join                     = $this->bocommon->join;
+                       $this->left_join        = $this->bocommon->left_join;
+                       $this->like                     = $this->bocommon->like;
+               }
+
+               function select_category_list()
+               {
+                       $table = 'fm_' . $this->role . '_category';
+                       $this->db->query("SELECT id,descr FROM $table  ORDER BY 
descr ");
+
+                       $i = 0;
+                       while ($this->db->next_record())
+                       {
+                               $categories[$i]['id']                           
= $this->db->f('id');
+                               $categories[$i]['name']                         
= stripslashes($this->db->f('descr'));
+                               $i++;
+                       }
+                       return $categories;
+               }
+
+               function read($data)
+               {
+                       if(is_array($data))
+                       {
+                               $start  = 
(isset($data['start'])?$data['start']:0);
+                               $filter = 
(isset($data['filter'])?$data['filter']:'none');
+                               $query = 
(isset($data['query'])?$data['query']:'');
+                               $sort = 
(isset($data['sort'])?$data['sort']:'DESC');
+                               $order = 
(isset($data['order'])?$data['order']:'');
+                               $cat_id = 
(isset($data['cat_id'])?$data['cat_id']:'');
+                               $allrows        = 
(isset($data['allrows'])?$data['allrows']:'');
+                               $member_id      = 
(isset($data['member_id'])?$data['member_id']:0);
+                       }
+
+                       $sql = $this->bocommon->fm_cache('sql_actor_' . 
$this->role . $lookup);
+
+                       $entity_table = 'fm_' . $this->role;
+                       $category_table = 'fm_' . $this->role . '_category';
+                       $choice_table = 'fm_' . $this->role . '_choice';
+                       $attribute_table = 'fm_' . $this->role . '_attribute';
+                       if(!$sql)
+                       {
+                               $cols = $entity_table . 
".*,$category_table.descr as category";
+
+                               $cols_return[]                          = 'id';
+                               $uicols['input_type'][]         = 'text';
+                               $uicols['name'][]                       = 'id';
+                               $uicols['descr'][]                      = 
lang('ID');
+                               $uicols['statustext'][]         = lang('ID');
+
+                               $cols_return[]                          = 'id';
+                               $uicols['input_type'][]         = 'hidden';
+                               $uicols['name'][]                       = 'id';
+                               $uicols['descr'][]                      = false;
+                               $uicols['statustext'][]         = false;
+
+                               $cols_return[]                          = 
'category';
+                               $uicols['input_type'][]         = 'text';
+                               $uicols['name'][]                       = 
'category';
+                               $uicols['descr'][]                      = 
lang('category');
+                               $uicols['statustext'][]         = 
lang('category');
+
+
+                               $paranthesis .='(';
+
+                               $joinmethod .= " $this->join  " . $entity_table 
. "_category ON ( $entity_table" . ".category =" .$entity_table . 
"_category.id))";
+
+                               $from .= " FROM $paranthesis $entity_table ";
+
+                               $sql = "SELECT $cols $from $joinmethod";
+
+                               $this->bocommon->fm_cache('sql_actor_' . 
$this->role . $lookup,$sql);
+                               $this->bocommon->fm_cache('uicols_actor_' . 
$this->role . $lookup,$uicols);
+                               $this->bocommon->fm_cache('cols_return_actor_' 
. $this->role . $lookup,$cols_return);
+
+                       }
+                       else
+                       {
+                               $uicols                                         
        = $this->bocommon->fm_cache('uicols_actor_'. $this->role . $lookup);
+                               $cols_return                                    
= $this->bocommon->fm_cache('cols_return_actor_' . $this->role . $lookup);
+                               if($lookup)
+                               {
+                                       $admin_entity   = 
CreateObject($this->currentapp.'_soadmin_entity');
+                                       $category = 
$admin_entity->read_single_category($entity_id,$cat_id);
+                               }
+                       }
+
+                       $i      = count($uicols['name']);
+
+                       $this->db->query("SELECT * FROM $attribute_table WHERE 
list=1");
+                       while ($this->db->next_record())
+                       {
+                               $uicols['input_type'][]         = 'text';
+                               $uicols['name'][]                       = 
$this->db->f('column_name');
+                               $uicols['descr'][]                      = 
$this->db->f('input_text');
+                               $uicols['statustext'][]         = 
$this->db->f('statustext');
+                               $uicols['datatype'][$i]         = 
$this->db->f('datatype');
+                               $cols_return_extra[]= array(
+                                       'name'  => $this->db->f('column_name'),
+                                       'datatype'      => 
$this->db->f('datatype'),
+                                       'attrib_id'     => $this->db->f('id')
+                               );
+
+                               $i++;
+                       }
+
+                       
$user_columns=$GLOBALS['phpgw_info']['user']['preferences'][$this->currentapp]['actor_columns_'
 . $this->role];
+
+//_debug_array($user_columns);
+
+                       if (isset($user_columns) AND is_array($user_columns) 
AND $user_columns[0])
+                       {
+                               foreach($user_columns as $column_id)
+                               {
+                                       $this->db->query("SELECT * FROM 
$attribute_table WHERE id= $column_id");
+
+                                       $this->db->next_record();
+                                       $uicols['input_type'][]         = 
'text';
+                                       $uicols['name'][]                       
= $this->db->f('column_name');
+                                       $uicols['descr'][]                      
= $this->db->f('input_text');
+                                       $uicols['statustext'][]         = 
$this->db->f('statustext');
+                                       $uicols['datatype'][$i]         = 
$this->db->f('datatype');
+                                       $cols_return_extra[]= array(
+                                               'name'  => 
$this->db->f('column_name'),
+                                               'datatype'      => 
$this->db->f('datatype'),
+                                               'attrib_id'     => 
$this->db->f('id')
+                                       );
+                                       $i++;
+                               }
+                       }
+
+                       $this->uicols   = $uicols;
+
+//_debug_array($cols_return_extra);
+                       if ($order)
+                       {
+                               $ordermethod = " order by $entity_table.$order 
$sort";
+                       }
+                       else
+                       {
+                               $ordermethod = " order by $entity_table.id 
DESC";
+                       }
+
+                       $where= 'WHERE';
+
+/*                     if ($filter=='all')
+                       {
+                               if (is_array($grants))
+                               {
+                                       while (list($user) = each($grants))
+                                       {
+                                               $public_user_list[] = $user;
+                                       }
+                                       reset($public_user_list);
+                                       $filtermethod .= " $where ( 
$entity_table.user_id IN(" . implode(',',$public_user_list) . "))";
+
+                                       $where= 'AND';
+                               }
+
+                       }
+                       else
+                       {
+                               $filtermethod = " $where 
$entity_table.user_id=$filter ";
+                               $where= 'AND';
+                       }
+*/
+
+                       if ($cat_id)
+                       {
+                               $filtermethod .= " $where 
$entity_table.category='$cat_id' ";
+                               $where= 'AND';
+                       }
+
+                       if ($member_id>0)
+                       {
+                               $filtermethod .= " $where 
$entity_table.member_of $this->like '%,$member_id,%' ";
+                               $where= 'AND';
+                       }
+
+                       if ($status)
+                       {
+                               $filtermethod .= " $where 
$entity_table.status='$status' ";
+                               $where= 'AND';
+                       }
+
+
+                       if($query)
+                       {
+                               $query = ereg_replace("'",'',$query);
+                               $query = ereg_replace('"','',$query);
+
+                               $filtermethod .= " $where $entity_table.id ='" 
. (int)$query . "'";
+                               $where= 'OR';
+
+                               $this->db->query("SELECT * FROM 
$attribute_table where search='1'");
+
+                               while ($this->db->next_record())
+                               {
+                                       if($this->db->f('datatype')=='V' || 
$this->db->f('datatype')=='email' || $this->db->f('datatype')=='CH')
+                                       {
+                                               $querymethod[]= 
"$entity_table." . $this->db->f('column_name') . " $this->like '%$query%'";
+                                       }
+                                       else
+                                       {
+                                               $querymethod[]= 
"$entity_table." . $this->db->f('column_name') . " = '$query'";
+                                       }
+                               }
+
+                               if (isset($querymethod) AND 
is_array($querymethod))
+                               {
+                                       $querymethod = " $where (" . implode (' 
OR ',$querymethod) . ')';
+                                       $where = 'AND';
+                               }
+                       }
+
+                       $sql .= " $filtermethod $querymethod";
+//echo $sql;
+
+                       $this->db2->query($sql,__LINE__,__FILE__);
+                       $this->total_records = $this->db2->num_rows();
+                       if(!$allrows)
+                       {
+                               $this->db->limit_query($sql . 
$ordermethod,$start,__LINE__,__FILE__);
+                       }
+                       else
+                       {
+                               $this->db->query($sql . 
$ordermethod,__LINE__,__FILE__);
+                       }
+
+                       $j=0;
+                       $n=count($cols_return);
+//_debug_array($cols_return);
+                       $contacts                       = 
CreateObject('phpgwapi.contacts');
+
+                       while ($this->db->next_record())
+                       {
+                               for ($i=0;$i<$n;$i++)
+                               {
+                                       $actor_list[$j][$cols_return[$i]] = 
$this->db->f($cols_return[$i]);
+                                       $actor_list[$j]['grants'] = 
(int)$grants[$this->db->f('user_id')];
+                               }
+
+                               for ($i=0;$i<count($cols_return_extra);$i++)
+                               {
+                                       $value='';
+                                       
$value=$this->db->f($cols_return_extra[$i]['name']);
+
+                                       
if(($cols_return_extra[$i]['datatype']=='R' || 
$cols_return_extra[$i]['datatype']=='LB') && $value):
+                                       {
+                                               $sql="SELECT value FROM 
$choice_table where attrib_id=" .$cols_return_extra[$i]['attrib_id']. "  AND 
id=" . $value;
+                                               $this->db2->query($sql);
+                                               $this->db2->next_record();
+                                               
$actor_list[$j][$cols_return_extra[$i]['name']] = $this->db2->f('value');
+                                       }
+                                       
elseif($cols_return_extra[$i]['datatype']=='AB' && $value):
+                                       {
+                                               $contact_data   = 
$contacts->read_single_entry($value,array('n_given'=>'n_given','n_family'=>'n_family','email'=>'email'));
+                                               
$actor_list[$j][$cols_return_extra[$i]['name']] = $contact_data[0]['n_family'] 
. ', ' . $contact_data[0]['n_given'];
+
+/*                                             $sql="SELECT org_name FROM 
phpgw_addressbook where id=$value";
+                                               $this->db2->query($sql);
+                                               $this->db2->next_record();
+                                               
$actor_list[$j][$cols_return_extra[$i]['name']] = $this->db2->f('org_name');
+*/
+                                       }
+                                       
elseif($cols_return_extra[$i]['datatype']=='VENDOR' && $value):
+                                       {
+                                               $sql="SELECT org_name FROM 
fm_vendor where id=$value";
+                                               $this->db2->query($sql);
+                                               $this->db2->next_record();
+                                               
$actor_list[$j][$cols_return_extra[$i]['name']] = $this->db2->f('org_name');
+                                       }
+                                       
elseif($cols_return_extra[$i]['datatype']=='CH' && $value):
+                                       {
+                                               $ch= unserialize($value);
+
+                                               if (isset($ch) AND 
is_array($ch))
+                                               {
+                                                       for 
($k=0;$k<count($ch);$k++)
+                                                       {
+                                                               $sql="SELECT 
value FROM $choice_table where attrib_id=" 
.$cols_return_extra[$i]['attrib_id']. "  AND id=" . $ch[$k];
+                                                               
$this->db2->query($sql);
+                                                               while 
($this->db2->next_record())
+                                                               {
+                                                                       
$ch_value[]=$this->db2->f('value');
+                                                               }
+                                                       }
+                                                       
$actor_list[$j][$cols_return_extra[$i]['name']] = @implode(",", $ch_value);
+                                                       unset($ch_value);
+                                               }
+                                       }
+                                       
elseif($cols_return_extra[$i]['datatype']=='D' && $value):
+                                       {
+//html_print_r($value);
+
+                                               
$actor_list[$j][$cols_return_extra[$i]['name']]=date($GLOBALS['phpgw_info']['user']['preferences']['common']['dateformat'],strtotime($value));
+                                       }
+                                       else:
+                                       {
+                                               
$actor_list[$j][$cols_return_extra[$i]['name']]=$value;
+                                       }
+                                       endif;
+                               }
+
+                               $j++;
+                       }
+//html_print_r($actor_list);
+                       return $actor_list;
+               }
+
+
+               function read_single($data)
+               {
+                       $attribute_table = 'fm_' . $this->role . '_attribute';
+                       $table = 'fm_' . $this->role;
+
+                       $actor_id =$data['actor_id'];
+
+                       $this->db->query("SELECT * FROM $attribute_table ORDER 
BY attrib_sort");
+
+                       while ($this->db->next_record())
+                       {
+                               $actor['attributes'][] = array
+                               (
+                                       'attrib_id'             => 
$this->db->f('id'),
+                                       'name'                  => 
$this->db->f('column_name'),
+                                       'input_text'    => 
stripslashes($this->db->f('input_text')),
+                                       'statustext'    => 
stripslashes($this->db->f('statustext')),
+                                       'datatype'              => 
$this->db->f('datatype')
+                               );
+                       }
+
+                       if($actor_id)
+                       {
+                               $this->db->query("SELECT * from $table where 
id='$actor_id'");
+
+                               if($this->db->next_record())
+                               {
+                                       $actor['id']                    = 
(int)$this->db->f('id');
+                                       $actor['entry_date']            = 
$this->db->f('entry_date');
+                                       $actor['cat_id']                        
= $this->db->f('category');
+                                       $actor['member_of']                     
= explode(',',$this->db->f('member_of'));
+
+                                       for 
($i=0;$i<count($actor['attributes']);$i++)
+                                       {
+                                               
$actor['attributes'][$i]['value']       = 
$this->db->f($actor['attributes'][$i]['name']);
+                                               
$actor['attributes'][$i]['datatype_text']       = 
$this->bocommon->translate_datatype($actor['attributes'][$i]['datatype']);
+                                       }
+
+                               }
+                       }
+                       return $actor;
+               }
+
+               function add($actor,$values_attribute='')
+               {
+                       $table = 'fm_' . $this->role;
+
+                       if($actor['member_of'])
+                       {
+                               $actor['member_of']=',' . 
implode(',',$actor['member_of']) . ',';
+                       }
+
+                       while (is_array($actor['extra']) && 
list($input_name,$value) = each($actor['extra']))
+                       {
+                               if($value)
+                               {
+                                       $cols[] = $input_name;
+                                       $vals[] = $value;
+                               }
+                       }
+
+                       if (isset($values_attribute) AND 
is_array($values_attribute))
+                       {
+                               foreach($values_attribute as $entry)
+                               {
+                                       if($entry['value'])
+                                       {
+                                               $cols[] = $entry['name'];
+                                               $vals[] = $entry['value'];
+                                       }
+                               }
+                       }
+
+                       if($this->role == 'vendor')
+                       {
+                               $cols[] = 'member_of';
+                               $vals[] = $actor['member_of'];
+                       }
+
+                       if($cols)
+                       {
+                               $cols   = "," . implode(",", $cols);
+                               $vals   = "," . 
$this->bocommon->validate_db_insert($vals);
+                       }
+
+                       $this->db->transaction_begin();
+                       if($actor['new_actor_id'])
+                       {
+                               $id = $actor['new_actor_id'];
+                       }
+                       else
+                       {
+                               $id = $this->bocommon->next_id($table);
+                       }
+
+                       $this->db->query("INSERT INTO $table 
(id,entry_date,category $cols) "
+                               . "VALUES ($id,'" . time() . "','" . 
$actor['cat_id'] . "' $vals)");
+
+                       $receipt['actor_id']= 
$id;//$this->db->get_last_insert_id($table,'id');
+
+                       $receipt['message'][] = array('msg'=>lang('actor %1 has 
been saved',$receipt['actor_id']));
+
+                       $this->db->transaction_commit();
+                       return $receipt;
+               }
+
+               function edit($actor,$values_attribute='')
+               {
+//_debug_array($actor);
+//_debug_array($values_attribute);
+                       $table = 'fm_' . $this->role;
+
+                       if($actor['member_of'])
+                       {
+                               $actor['member_of']=',' . 
implode(',',$actor['member_of']) . ',';
+                       }
+
+                       while (is_array($actor['extra']) && 
list($column,$value) = each($actor['extra']))
+                       {
+                               $value_set[$column]     = $value;
+                       }
+
+                       if (isset($values_attribute) AND 
is_array($values_attribute))
+                       {
+                               foreach($values_attribute as $entry)
+                               {
+                                       if($entry['datatype']!='AB' && 
$entry['datatype']!='VENDOR')
+                                       {
+                                               $value_set[$entry['name']]      
= $entry['value'];
+                                       }
+                               }
+                       }
+
+                       $value_set['entry_date']        = time();
+                       $value_set['category']  = $actor['cat_id'];
+
+                       if($this->role == 'vendor')
+                       {
+                               $value_set['member_of'] = $actor['member_of'];
+                       }
+
+                       if($value_set)
+                       {
+                               $value_set      = 
$this->bocommon->validate_db_update($value_set);
+                       }
+
+                       $this->db->query("UPDATE $table set $value_set WHERE 
id=" . intval($actor['actor_id']));
+
+                       $receipt['actor_id']= $actor['actor_id'];
+                       $receipt['message'][] = array('msg'=>lang('actor %1 has 
been edited',$actor['actor_id']));
+                       return $receipt;
+               }
+
+               function delete($actor_id)
+               {
+                       $table = 'fm_' . $this->role;
+                       $this->db->query('DELETE FROM $table WHERE id=' . 
intval($actor_id));
+               }
+
+               function read_attrib($data)
+               {
+                       $attribute_table = 'fm_' . $this->role . '_attribute';
+//html_print_r($data);
+                       if(is_array($data))
+                       {
+                               if ($data['start'])
+                               {
+                                       $start=$data['start'];
+                               }
+                               else
+                               {
+                                       $start=0;
+                               }
+                               $query = 
(isset($data['query'])?$data['query']:'');
+                               $sort = 
(isset($data['sort'])?$data['sort']:'DESC');
+                               $order = 
(isset($data['order'])?$data['order']:'');
+                               $allrows = 
(isset($data['allrows'])?$data['allrows']:'');
+                               $column_list = 
(isset($data['column_list'])?$data['column_list']:'');
+                       }
+
+                       $where = 'WHERE';
+                       if ($column_list)
+                       {
+                               $filtermethod = " $where list !=1 or list is 
null";
+                               $where = 'AND';
+
+                       }
+                       if ($order)
+                       {
+                               $ordermethod = " order by $order $sort";
+
+                       }
+                       else
+                       {
+                               $ordermethod = ' order by attrib_sort asc';
+                       }
+
+                       if($query)
+                       {
+                               $query = ereg_replace("'",'',$query);
+                               $query = ereg_replace('"','',$query);
+
+                               $querymethod = " $where 
($attribute_table.input_text $this->like '%$query%' or 
$attribute_table.column_name $this->like '%$query%')";
+                       }
+
+                       $sql = "SELECT * FROM $attribute_table $filtermethod 
$querymethod";
+
+//echo $sql;
+                       $this->db2->query($sql,__LINE__,__FILE__);
+                       $this->total_records = $this->db2->num_rows();
+                       if(!$allrows)
+                       {
+                               $this->db->limit_query($sql . 
$ordermethod,$start,__LINE__,__FILE__);
+                       }
+                       else
+                       {
+                               $this->db->query($sql . 
$ordermethod,__LINE__,__FILE__);
+                       }
+
+                       while ($this->db->next_record())
+                       {
+                               $attrib[] = array
+                               (
+                                       'id'                    => 
$this->db->f('id'),
+                                       'attrib_sort'   => 
$this->db->f('attrib_sort'),
+                                       'list'                  => 
$this->db->f('list'),
+                                       'lookup_form'   => 
$this->db->f('lookup_form'),
+                                       'column_name'   => 
$this->db->f('column_name'),
+                                       'name'                  => 
$this->db->f('input_text'),
+                                       'size'                  => 
$this->db->f('size'),
+                                       'statustext'    => 
$this->db->f('statustext'),
+                                       'input_text'    => 
$this->db->f('input_text'),
+                                       'type_name'             => 
$this->db->f('type'),
+                                       'datatype'              => 
$this->db->f('datatype'),
+                                       'search'                => 
$this->db->f('search')
+                               );
+                       }
+                       return $attrib;
+               }
+
+               function read_single_attrib($id)
+               {
+                       $attribute_table = 'fm_' . $this->role . '_attribute';
+
+                       $sql = "SELECT * FROM $attribute_table where id=$id";
+
+                       $this->db->query($sql);
+
+                       if($this->db->next_record())
+                       {
+                               $attrib['id']                                   
        = $this->db->f('id');
+                               $attrib['column_name']                          
= $this->db->f('column_name');
+                               $attrib['input_text']                           
= $this->db->f('input_text');
+                               $attrib['statustext']                           
= $this->db->f('statustext');
+                               $attrib['column_info']['precision']     = 
$this->db->f('precision_');
+                               $attrib['column_info']['scale']         = 
$this->db->f('scale');
+                               $attrib['column_info']['default']       = 
$this->db->f('default_value');
+                               $attrib['column_info']['nullable']      = 
$this->db->f('nullable');
+                               $attrib['column_info']['type']          = 
$this->db->f('datatype');
+                               $attrib['type_name']                            
= $this->db->f('type_name');
+                               $attrib['lookup_form']                          
= $this->db->f('lookup_form');
+                               $attrib['list']                                 
        = $this->db->f('list');
+                               $attrib['search']                               
        = $this->db->f('search');
+                               if($this->db->f('datatype')=='R' || 
$this->db->f('datatype')=='CH' || $this->db->f('datatype')=='LB')
+                               {
+                                       $attrib['choice'] = 
$this->read_attrib_choice($id);
+                               }
+
+                               return $attrib;
+                       }
+               }
+
+               function read_attrib_choice($attrib_id)
+               {
+                       $choice_table = 'fm_' . $this->role . '_choice';
+                       $sql = "SELECT * FROM $choice_table WHERE 
attrib_id=$attrib_id";
+                       $this->db->query($sql);
+
+                       while ($this->db->next_record())
+                       {
+                               $choice[] = array
+                               (
+                                       'id'    => $this->db->f('id'),
+                                       'value' => $this->db->f('value')
+                               );
+
+                       }
+                       return $choice;
+               }
+
+               function add_attrib($attrib)
+               {
+                       $table = 'fm_' . $this->role;
+                       $attribute_table = 'fm_' . $this->role . '_attribute';
+                       $attrib['column_name'] = 
$this->db->db_addslashes($attrib['column_name']);
+                       $attrib['input_text'] = 
$this->db->db_addslashes($attrib['input_text']);
+                       $attrib['statustext'] = 
$this->db->db_addslashes($attrib['statustext']);
+                       $attrib['default'] = 
$this->db->db_addslashes($attrib['default']);
+                       $attrib['id'] = 
$this->bocommon->next_id($attribute_table);
+
+                       $sql = "SELECT max(attrib_sort) as max_sort FROM 
$attribute_table";
+                       $this->db->query($sql);
+                       $this->db->next_record();
+                       $attrib_sort    = $this->db->f('max_sort')+1;
+
+                       $values= array(
+                               $attrib['id'],
+                               $attrib['column_name'],
+                               $attrib['input_text'],
+                               $attrib['statustext'],
+                               $attrib['lookup_form'],
+                               $attrib['search'],
+                               $attrib['list'],
+                               $attrib_sort,
+                               $attrib['column_info']['type'],
+                               $attrib['column_info']['precision'],
+                               $attrib['column_info']['scale'],
+                               $attrib['column_info']['default'],
+                               $attrib['column_info']['nullable']
+                               );
+
+                       $values = $this->bocommon->validate_db_insert($values);
+
+                       $this->db->transaction_begin();
+
+                       $this->db->query("INSERT INTO $attribute_table 
(id,column_name, input_text, 
statustext,lookup_form,search,list,attrib_sort,datatype,precision_,scale,default_value,nullable)
 "
+                               . "VALUES ($values)");
+
+                       $receipt['id']= $attrib['id'];
+
+                       if($attrib['column_info']['type']=='email' && 
!$attrib['column_info']['precision'])
+                       {
+                               $attrib['column_info']['precision']=64;
+                       }
+
+                       $attrib['column_info']['type']  = 
$this->bocommon->translate_datatype_insert($attrib['column_info']['type']);
+
+                       if($attrib['column_info']['type']=='int' && 
!$attrib['column_info']['precision'])
+                       {
+                               $attrib['column_info']['precision']=4;
+                       }
+
+                       if(!$attrib['column_info']['default'])
+                       {
+                               unset($attrib['column_info']['default']);
+                       }
+
+                       $this->init_process();
+
+                       
if($this->oProc->AddColumn($table,$attrib['column_name'], 
$attrib['column_info']))
+                       {
+                               $receipt['message'][] = array('msg'     => 
lang('Attribute has been saved')     );
+                               $this->db->transaction_commit();
+
+                       }
+                       else
+                       {
+                               $receipt['error'][] = array('msg'       => 
lang('column could not be added')    );
+                               if($this->db->Transaction)
+                               {
+                                       $GLOBALS['phpgw']->db->rollbacktrans();
+                               }
+                               else
+                               {
+                                       $GLOBALS['phpgw']->db->Execute("DELETE 
FROM $attribute_table WHERE id='" . $receipt['id'] . "'");
+                                       unset($receipt['id']);
+
+                               }
+                       }
+
+                       return $receipt;
+               }
+
+               function init_process()
+               {
+                       $this->oProc                                            
= 
CreateObject('phpgwapi.schema_proc',$GLOBALS['phpgw_info']['server']['db_type']);
+                       $this->oProc->m_odb                                     
= $this->db;
+                       $this->oProc->m_odb->Halt_On_Error      = 'report';
+               }
+
+               function edit_attrib($attrib)
+               {
+                       $attribute_table = 'fm_' . $this->role . '_attribute';
+                       $table = 'fm_' . $this->role;
+
+                       $attrib['column_name'] = 
$this->db->db_addslashes($attrib['column_name']);
+                       $attrib['input_text'] = 
$this->db->db_addslashes($attrib['input_text']);
+                       $attrib['statustext'] = 
$this->db->db_addslashes($attrib['statustext']);
+                       $attrib['default'] = 
$this->db->db_addslashes($attrib['default']);
+
+                       $choice_table = 'fm_' . $this->role . '_choice';
+
+                       $this->db->query("SELECT column_name FROM 
$attribute_table WHERE id='" . $attrib['id']. "'");
+                       $this->db->next_record();
+                       $OldColumnName          = $this->db->f('column_name');
+
+                       $this->db->transaction_begin();
+
+                       $value_set=array(
+                               'input_text'            => 
$attrib['input_text'],
+                               'statustext'            => 
$attrib['statustext'],
+                               'lookup_form'           => 
$attrib['lookup_form'],
+                               'search'                        => 
$attrib['search'],
+                               'list'                          => 
$attrib['list'],
+                               );
+
+                       $value_set      = 
$this->bocommon->validate_db_update($value_set);
+
+                       $this->db->query("UPDATE $attribute_table set 
$value_set WHERE id=" . $attrib['id']);
+
+                       $attrib_type=$attrib['column_info']['type'];
+                       if(($OldColumnName !=$attrib['column_name']) || 
($attrib['column_info']['type'] != $this->db->f('datatype')))
+                       {
+                               $value_set=array(
+                                       'column_name'           => 
$attrib['column_name'],
+                                       'datatype'                      => 
$attrib['column_info']['type'],
+                                       'precision_'            => 
$attrib['column_info']['precision'],
+                                       'scale'                         => 
$attrib['column_info']['scale'],
+                                       'default_value'         => 
$attrib['column_info']['default'],
+                                       'nullable'                      => 
$attrib['column_info']['nullable']
+                                       );
+
+                               $value_set      = 
$this->bocommon->validate_db_update($value_set);
+
+                               $this->db->query("UPDATE $attribute_table set 
$value_set WHERE id=" . $attrib['id']);
+
+                               if($attrib['column_info']['type']=='email' && 
!$attrib['column_info']['precision'])
+                               {
+                                       $attrib['column_info']['precision']=64;
+                               }
+
+                               $attrib['column_info']['type']  = 
$this->bocommon->translate_datatype_insert($attrib['column_info']['type']);
+
+                               if($attrib['column_info']['type']=='int' && 
!$attrib['column_info']['precision'])
+                               {
+                                       $attrib['column_info']['precision']=4;
+                               }
+
+                               if(!$attrib['column_info']['default'])
+                               {
+                                       
unset($attrib['column_info']['default']);
+                               }
+
+                               $this->init_process();
+
+                               
$this->oProc->AlterColumn($table,$OldColumnName,$attrib['column_name'],$attrib['column_info']);
+
+                       }
+
+
+                       if($attrib['new_choice'])
+                       {
+                               $choice_id = 
$this->bocommon->next_id($choice_table ,array('attrib_id'=>$attrib['id']));
+
+                               $values= array(
+                                       $attrib['id'],
+                                       $choice_id,
+                                       $attrib['new_choice']
+                                       );
+
+                               $values = 
$this->bocommon->validate_db_insert($values);
+
+                               $this->db->query("INSERT INTO $choice_table 
(attrib_id,id,value) "
+                               . "VALUES ($values)");
+                       }
+
+
+                       if($attrib['delete_choice'])
+                       {
+                               for 
($i=0;$i<count($attrib['delete_choice']);$i++)
+                               {
+                                       $this->db->query("DELETE FROM 
$choice_table WHERE  attrib_id=" . $attrib['id']  ." AND id=" . 
$attrib['delete_choice'][$i]);
+                               }
+                       }
+
+//html_print_r($attrib_type);
+                       if($attrib_type!='R' && $attrib_type!='CH' && 
$attrib_type!='LB')
+                       {
+                               $this->db->query("DELETE FROM $choice_table 
WHERE  attrib_id=" . $attrib['id']);
+                       }
+
+
+                       $this->db->transaction_commit();
+
+                       $receipt['message'][] = array('msg'     => 
lang('Attribute has been edited'));
+
+
+                       return $receipt;
+
+               }
+               function resort_attrib($data)
+               {
+//html_print_r($data);
+                       $attribute_table = 'fm_' . $this->role . '_attribute';
+                       if(is_array($data))
+                       {
+                               $resort = 
(isset($data['resort'])?$data['resort']:'up');
+                               $id = (isset($data['id'])?$data['id']:'');
+                       }
+
+                       $sql = "SELECT attrib_sort FROM $attribute_table where 
id=$id";
+                       $this->db->query($sql);
+                       $this->db->next_record();
+                       $attrib_sort    = $this->db->f('attrib_sort');
+                       $sql = "SELECT max(attrib_sort) as max_sort FROM 
$attribute_table";
+                       $this->db->query($sql);
+                       $this->db->next_record();
+                       $max_sort       = $this->db->f('max_sort');
+                       switch($resort)
+                       {
+                               case 'up':
+                                       if($attrib_sort>1)
+                                       {
+                                               $sql = "UPDATE $attribute_table 
set attrib_sort=$attrib_sort WHERE attrib_sort =" . ($attrib_sort-1);
+                                               $this->db->query($sql);
+                                               $sql = "UPDATE $attribute_table 
set attrib_sort=" . ($attrib_sort-1) ." WHERE id=$id";
+                                               $this->db->query($sql);
+                                       }
+                                       break;
+                               case 'down':
+                                       if($max_sort > $attrib_sort)
+                                       {
+                                               $sql = "UPDATE $attribute_table 
set attrib_sort=$attrib_sort WHERE attrib_sort =" . ($attrib_sort+1);
+                                               $this->db->query($sql);
+                                               $sql = "UPDATE $attribute_table 
set attrib_sort=" . ($attrib_sort+1) ." WHERE id=$id";
+                                               $this->db->query($sql);
+                                       }
+                                       break;
+                               default:
+                                       return;
+                                       break;
+                       }
+               }
+               function delete_attrib($attrib_id)
+               {
+                       $table = 'fm_' . $this->role;
+                       $attribute_table = 'fm_' . $this->role . '_attribute';
+                       $this->init_process();
+
+                       $sql = "SELECT * FROM $attribute_table WHERE 
id=$attrib_id";
+
+                       $this->db->query($sql);
+                       $this->db->next_record();
+                       $ColumnName             = $this->db->f('column_name');
+
+                       if($this->oProc->DropColumn($table,'', $ColumnName))
+                       {
+                               $sql = "SELECT attrib_sort FROM 
$attribute_table where id=$attrib_id";
+                               $this->db->query($sql);
+                               $this->db->next_record();
+                               $attrib_sort    = $this->db->f('attrib_sort');
+                               $sql2 = "SELECT max(attrib_sort) as max_sort 
FROM $attribute_table";
+                               $this->db->query($sql2);
+                               $this->db->next_record();
+                               $max_sort       = $this->db->f('max_sort');
+                               if($max_sort>$attrib_sort)
+                               {
+                                       $sql = "UPDATE $attribute_table set 
attrib_sort=attrib_sort-1 WHERE attrib_sort > $attrib_sort";
+                                       $this->db->query($sql);
+                               }
+
+                               $this->db->query("DELETE FROM $attribute_table 
WHERE id=$attrib_id");
+                       }
+                       else
+                       {
+                               $receipt['error'][] = array('msg'       => 
lang('Attribute has NOT been deleted'));
+                       }
+
+                       return $receipt;
+               }
+       }
+?>
Index: property/inc/class.soadmin.inc.php
diff -u property/inc/class.soadmin.inc.php:1.6 
property/inc/class.soadmin.inc.php:1.7
--- property/inc/class.soadmin.inc.php:1.6      Fri Jan 27 14:05:43 2006
+++ property/inc/class.soadmin.inc.php  Mon Jan 30 22:14:19 2006
@@ -1,114 +1,114 @@
-<?php
-       /**
-       * phpGroupWare - property: a Facilities Management System.
-       *
-       * @author Sigurd Nes <address@hidden>
-       * @copyright Copyright (C) 2003-2005 Free Software Foundation, Inc. 
http://www.fsf.org/
-       * @license http://www.gnu.org/licenses/gpl.html GNU General Public 
License
-       * @internal Development of this application was funded by 
http://www.bergen.kommune.no/bbb_/ekstern/
-       * @package property
-       * @subpackage admin
-       * @version $Id: class.soadmin.inc.php,v 1.6 2006/01/27 14:05:43 
sigurdne Exp $
-       */
-
-       /**
-        * Description
-        * @package property
-        */
-
-       class soadmin
-       {
-
-               function soadmin()
-               {
-                       $this->currentapp       = 
$GLOBALS['phpgw_info']['flags']['currentapp'];
-                       $this->account  = 
$GLOBALS['phpgw_info']['user']['account_id'];
-                       $this->bocommon         = 
CreateObject($this->currentapp.'.bocommon');
+<?php
+       /**
+       * phpGroupWare - property: a Facilities Management System.
+       *
+       * @author Sigurd Nes <address@hidden>
+       * @copyright Copyright (C) 2003-2005 Free Software Foundation, Inc. 
http://www.fsf.org/
+       * @license http://www.gnu.org/licenses/gpl.html GNU General Public 
License
+       * @internal Development of this application was funded by 
http://www.bergen.kommune.no/bbb_/ekstern/
+       * @package property
+       * @subpackage admin
+       * @version $Id: class.soadmin.inc.php,v 1.7 2006/01/30 22:14:19 
sigurdne Exp $
+       */
+
+       /**
+        * Description
+        * @package property
+        */
+
+       class soadmin
+       {
+
+               function soadmin()
+               {
+                       $this->currentapp       = 
$GLOBALS['phpgw_info']['flags']['currentapp'];
+                       $this->account  = 
$GLOBALS['phpgw_info']['user']['account_id'];
+                       $this->bocommon         = 
CreateObject($this->currentapp.'.bocommon');
                        $this->db               = $this->bocommon->new_db();
-                       $this->db2              = $this->bocommon->new_db();
-                       $this->join                     = $this->bocommon->join;
-                       $this->like                     = $this->bocommon->like;
-               }
-
-
-               function get_initials($id)
-               {
-                       $this->db->query("SELECT initials FROM fm_ecouser WHERE 
id=$id ");
-                       $this->db->next_record();
-                       return $this->db->f('initials');
-               }
-
-               function set_initials($initials)
-               {
-                       while (is_array($initials) && list($account_id,$value) 
= each($initials))
-                       {
-                               $this->db->query("UPDATE fm_ecouser set 
initials= '$value' WHERE id=$account_id ",__LINE__,__FILE__);
-                               if($value)
-                               {
-                                       if(!$this->get_initials($account_id))
-                                       {
-                                               $account_lid    = 
strtoupper($GLOBALS['phpgw']->accounts->id2name($account_id));
-                                               $this->db->query("INSERT INTO 
fm_ecouser (id,lid,initials) VALUES ($account_id,'$account_lid','$value' 
)",__LINE__,__FILE__);
-                                       }
-                               }
-                       }
-               }
-
-               function select_location($grant='')
-               {
-                       if($grant)
-                       {
-                               $filter = ' WHERE allow_grant=1';
-                       }
-                       $this->db->query("SELECT * FROM fm_acl_location $filter 
ORDER BY id ");
-
-                               $i = 0;
-                               while ($this->db->next_record())
-                               {
-                                               $location[$i]['id']             
        = $this->db->f('id');
-                                               $location[$i]['descr']          
= stripslashes($this->db->f('descr'));
-                               $i++;
-                               }
-
-                               return $location;
-               }
-
-
-               function read_fm_id()
-               {
-                       $sql = "select * from fm_idgenerator ";
-                       $this->db->query($sql,__LINE__,__FILE__);
-
-                       while ($this->db->next_record())
-                       {
-                               $fm_ids[] = array(
-                                                       'descr'         => 
$this->db->f('name'),
-                                                       'value'         => 
$this->db->f('value'),
-                                                       'remark'        => 
$this->db->f('remark')
-                               );
-
-                       }
-
-                       return $fm_ids;
-               }
-
-               function edit_id($values='')
-               {
-                       $field=$values['field'];
-                       $select=$values['select'];
-
-                       while($entry=each($select))
-                               {
-                                       $n=$entry[0];
-
-
-                                       $sql = "update  fm_idgenerator set 
value='$values[$n]' where name='$field[$n]'";
-                                       
$this->db->query($sql,__LINE__,__FILE__);
-
-                               }
-
-                       $receipt['message'][] = array('msg' => lang('ID is 
updated'));
-                       return $receipt;
-               }
-       }
-?>
+                       $this->db2              = $this->bocommon->new_db();
+
+                       $this->join                     = $this->bocommon->join;
+                       $this->like                     = $this->bocommon->like;
+               }
+
+               function get_initials($id)
+               {
+                       $this->db->query("SELECT initials FROM fm_ecouser WHERE 
id=$id ");
+                       $this->db->next_record();
+                       return $this->db->f('initials');
+               }
+
+               function set_initials($initials)
+               {
+                       while (is_array($initials) && list($account_id,$value) 
= each($initials))
+                       {
+                               $this->db->query("UPDATE fm_ecouser set 
initials= '$value' WHERE id=$account_id ",__LINE__,__FILE__);
+                               if($value)
+                               {
+                                       if(!$this->get_initials($account_id))
+                                       {
+                                               $account_lid    = 
strtoupper($GLOBALS['phpgw']->accounts->id2name($account_id));
+                                               $this->db->query("INSERT INTO 
fm_ecouser (id,lid,initials) VALUES ($account_id,'$account_lid','$value' 
)",__LINE__,__FILE__);
+                                       }
+                               }
+                       }
+               }
+
+               function select_location($grant='')
+               {
+                       if($grant)
+                       {
+                               $filter = ' WHERE allow_grant=1';
+                       }
+                       $this->db->query("SELECT * FROM fm_acl_location $filter 
ORDER BY id ");
+
+                               $i = 0;
+                               while ($this->db->next_record())
+                               {
+                                               $location[$i]['id']             
        = $this->db->f('id');
+                                               $location[$i]['descr']          
= stripslashes($this->db->f('descr'));
+                               $i++;
+                               }
+
+                               return $location;
+               }
+
+
+               function read_fm_id()
+               {
+                       $sql = "select * from fm_idgenerator ";
+                       $this->db->query($sql,__LINE__,__FILE__);
+
+                       while ($this->db->next_record())
+                       {
+                               $fm_ids[] = array(
+                                                       'descr'         => 
$this->db->f('name'),
+                                                       'value'         => 
$this->db->f('value'),
+                                                       'remark'        => 
$this->db->f('remark')
+                               );
+
+                       }
+
+                       return $fm_ids;
+               }
+
+               function edit_id($values='')
+               {
+                       $field=$values['field'];
+                       $select=$values['select'];
+
+                       while($entry=each($select))
+                               {
+                                       $n=$entry[0];
+
+
+                                       $sql = "update  fm_idgenerator set 
value='$values[$n]' where name='$field[$n]'";
+                                       
$this->db->query($sql,__LINE__,__FILE__);
+
+                               }
+
+                       $receipt['message'][] = array('msg' => lang('ID is 
updated'));
+                       return $receipt;
+               }
+       }
+?>
Index: property/inc/class.soadmin_entity.inc.php
diff -u property/inc/class.soadmin_entity.inc.php:1.11 
property/inc/class.soadmin_entity.inc.php:1.12
--- property/inc/class.soadmin_entity.inc.php:1.11      Fri Jan 27 14:05:43 2006
+++ property/inc/class.soadmin_entity.inc.php   Mon Jan 30 22:14:19 2006
@@ -1,1354 +1,1353 @@
-<?php
-       /**
-       * phpGroupWare - property: a Facilities Management System.
-       *
-       * @author Sigurd Nes <address@hidden>
-       * @copyright Copyright (C) 2003-2005 Free Software Foundation, Inc. 
http://www.fsf.org/
-       * @license http://www.gnu.org/licenses/gpl.html GNU General Public 
License
-       * @internal Development of this application was funded by 
http://www.bergen.kommune.no/bbb_/ekstern/
-       * @package property
-       * @subpackage admin
-       * @version $Id: class.soadmin_entity.inc.php,v 1.11 2006/01/27 14:05:43 
sigurdne Exp $
-       */
-
-       /**
-        * Description
-        * @package property
-        */
-
-       class soadmin_entity
-       {
-               var $grants;
-
-               function soadmin_entity($entity_id='',$cat_id='')
-               {
-                       $this->currentapp       = 
$GLOBALS['phpgw_info']['flags']['currentapp'];
-                       $this->account          = 
$GLOBALS['phpgw_info']['user']['account_id'];
-                       $this->bocommon         = 
CreateObject($this->currentapp.'.bocommon');
+<?php
+       /**
+       * phpGroupWare - property: a Facilities Management System.
+       *
+       * @author Sigurd Nes <address@hidden>
+       * @copyright Copyright (C) 2003-2005 Free Software Foundation, Inc. 
http://www.fsf.org/
+       * @license http://www.gnu.org/licenses/gpl.html GNU General Public 
License
+       * @internal Development of this application was funded by 
http://www.bergen.kommune.no/bbb_/ekstern/
+       * @package property
+       * @subpackage admin
+       * @version $Id: class.soadmin_entity.inc.php,v 1.12 2006/01/30 22:14:19 
sigurdne Exp $
+       */
+
+       /**
+        * Description
+        * @package property
+        */
+
+       class soadmin_entity
+       {
+               var $grants;
+
+               function soadmin_entity($entity_id='',$cat_id='')
+               {
+                       $this->currentapp       = 
$GLOBALS['phpgw_info']['flags']['currentapp'];
+                       $this->account          = 
$GLOBALS['phpgw_info']['user']['account_id'];
+                       $this->bocommon         = 
CreateObject($this->currentapp.'.bocommon');
                        $this->db               = $this->bocommon->new_db();
                        $this->db2              = $this->bocommon->new_db();
-
-                       $this->join                     = $this->bocommon->join;
-                       $this->like                     = $this->bocommon->like;
-
-                       if($entity_id && $cat_id)
-                       {
-                               $this->category_name    = 
$this->read_category_name($entity_id,$cat_id);
-                       }
-               }
-
-               function reset_fm_cache()
-               {
-                       $this->db->query("DELETE FROM fm_cache 
",__LINE__,__FILE__);
-               }
-
-               function read($data)
-               {
-                       if(is_array($data))
-                       {
-                               if ($data['start'])
-                               {
-                                       $start=$data['start'];
-                               }
-                               else
-                               {
-                                       $start=0;
-                               }
-                               $query = 
(isset($data['query'])?$data['query']:'');
-                               $sort = 
(isset($data['sort'])?$data['sort']:'DESC');
-                               $order = 
(isset($data['order'])?$data['order']:'');
-                               $allrows = 
(isset($data['allrows'])?$data['allrows']:'');
-                       }
-
-                       if ($order)
-                       {
-                               $ordermethod = " order by $order $sort";
-
-                       }
-                       else
-                       {
-                               $ordermethod = ' order by id asc';
-                       }
-
-                       $table = 'fm_entity';
-
-                       if($query)
-                       {
-                               $query = ereg_replace("'",'',$query);
-                               $query = ereg_replace('"','',$query);
-
-                               $querymethod = " where name $this->like 
'%$query%' or descr $this->like '%$query%'";
-                       }
-
-                       $sql = "SELECT * FROM $table $querymethod";
-
-                       $this->db2->query($sql,__LINE__,__FILE__);
-                       $this->total_records = $this->db2->num_rows();
-
-                       if(!$allrows)
-                       {
-                               $this->db->limit_query($sql . 
$ordermethod,$start,__LINE__,__FILE__);
-                       }
-                       else
-                       {
-                               $this->db->query($sql . 
$ordermethod,__LINE__,__FILE__);
-                       }
-
-                       while ($this->db->next_record())
-                       {
-                               $entity[] = array
-                               (
-                                       'id'    => $this->db->f('id'),
-                                       'name'  => $this->db->f('name'),
-                                       'descr' => $this->db->f('descr'),
-                                       'documentation' => 
$this->db->f('documentation')
-                               );
-                       }
-                       return $entity;
-               }
-
-               function read_category($data)
-               {
-                       if(is_array($data))
-                       {
-                               if ($data['start'])
-                               {
-                                       $start=$data['start'];
-                               }
-                               else
-                               {
-                                       $start=0;
-                               }
-                               $query = 
(isset($data['query'])?$data['query']:'');
-                               $sort = 
(isset($data['sort'])?$data['sort']:'DESC');
-                               $order = 
(isset($data['order'])?$data['order']:'');
-                               $allrows = 
(isset($data['allrows'])?$data['allrows']:'');
-                               $entity_id = 
(isset($data['entity_id'])?$data['entity_id']:'');
-                       }
-
-                       if ($order)
-                       {
-                               $ordermethod = " order by $order $sort";
-
-                       }
-                       else
-                       {
-                               $ordermethod = ' order by id asc';
-                       }
-
-                       $table = 'fm_entity_category';
-
-                       if($query)
-                       {
-                               $query = ereg_replace("'",'',$query);
-                               $query = ereg_replace('"','',$query);
-
-                               $querymethod = " AND name $this->like 
'%$query%' or descr $this->like '%$query%'";
-                       }
-
-                       $sql = "SELECT * FROM $table WHERE entity_id=$entity_id 
$querymethod";
-
-                       $this->db2->query($sql,__LINE__,__FILE__);
-                       $this->total_records = $this->db2->num_rows();
-
-                       if(!$allrows)
-                       {
-                               $this->db->limit_query($sql . 
$ordermethod,$start,__LINE__,__FILE__);
-                       }
-                       else
-                       {
-                               $this->db->query($sql . 
$ordermethod,__LINE__,__FILE__);
-                       }
-
-                       while ($this->db->next_record())
-                       {
-                               $standard[] = array
-                               (
-                                       'id'    => $this->db->f('id'),
-                                       'name'  => $this->db->f('name'),
-                                       'prefix'=> $this->db->f('prefix'),
-                                       'descr' => $this->db->f('descr')
-                               );
-                       }
-                       return $standard;
-               }
-
-               function read_status($data)
-               {
-                       if(is_array($data))
-                       {
-                               if ($data['start'])
-                               {
-                                       $start=$data['start'];
-                               }
-                               else
-                               {
-                                       $start=0;
-                               }
-                               $query = 
(isset($data['query'])?$data['query']:'');
-                               $sort = 
(isset($data['sort'])?$data['sort']:'DESC');
-                               $order = 
(isset($data['order'])?$data['order']:'');
-                               $allrows = 
(isset($data['allrows'])?$data['allrows']:'');
-                               $entity_id = 
(isset($data['entity_id'])?$data['entity_id']:'');
-                               $cat_id = 
(isset($data['cat_id'])?$data['cat_id']:'');
-                       }
-
-                       if ($order)
-                       {
-                               $ordermethod = " order by $order $sort";
-
-                       }
-                       else
-                       {
-                               $ordermethod = ' order by id asc';
-                       }
-
-                       $table  = 'fm_entity_'. $entity_id .'_'.$cat_id . 
'_status';
-
-                       if($query)
-                       {
-                               $query = ereg_replace("'",'',$query);
-                               $query = ereg_replace('"','',$query);
-
-                               $querymethod = " AND name $this->like 
'%$query%' or descr $this->like '%$query%'";
-                       }
-
-                       $sql = "SELECT * FROM $table $querymethod";
-
-                       $this->db2->query($sql,__LINE__,__FILE__);
-                       $this->total_records = $this->db2->num_rows();
-
-                       if(!$allrows)
-                       {
-                               $this->db->limit_query($sql . 
$ordermethod,$start,__LINE__,__FILE__);
-                       }
-                       else
-                       {
-                               $this->db->query($sql . 
$ordermethod,__LINE__,__FILE__);
-                       }
-
-                       while ($this->db->next_record())
-                       {
-                               $status[] = array
-                               (
-                                       'id'    => $this->db->f('id'),
-                                       'descr' => $this->db->f('descr')
-                               );
-                       }
-                       return $status;
-               }
-
-
-               function read_single($id)
-               {
-
-                       $sql = "SELECT * FROM fm_entity  where id='$id'";
-
-                       $this->db->query($sql,__LINE__,__FILE__);
-
-                       if ($this->db->next_record())
-                       {
-                               $entity['id']                           = 
$this->db->f('id');
-                               $entity['name']                         = 
$this->db->f('name');
-                               $entity['descr']                        = 
$this->db->f('descr');
-                               $entity['location_form']        = 
$this->db->f('location_form');
-                               $entity['lookup_entity']        = 
unserialize($this->db->f('lookup_entity'));
-                               $entity['documentation']        = 
$this->db->f('documentation');
-                       }
-
-                       $sql = "SELECT location FROM fm_entity_lookup where 
entity_id=$id AND type='lookup'";
-
-                       $this->db->query($sql,__LINE__,__FILE__);
-
-                       while ($this->db->next_record())
-                       {
-                               $entity['include_entity_for'][] = 
$this->db->f('location');
-                       }
-
-                       $sql = "SELECT location FROM fm_entity_lookup where 
entity_id=$id AND type='start'";
-
-                       $this->db->query($sql,__LINE__,__FILE__);
-
-                       while ($this->db->next_record())
-                       {
-                               $entity['start_entity_from'][] = 
$this->db->f('location');
-                       }
-
-                       return $entity;
-               }
-
-               function read_single_category($entity_id,$cat_id)
-               {
-                       $sql = "SELECT * FROM fm_entity_category where 
entity_id=$entity_id AND id=$cat_id";
-
-                       $this->db->query($sql,__LINE__,__FILE__);
-
-                       if ($this->db->next_record())
-                       {
-                               $category['id']                 = 
$this->db->f('id');
-                               $category['name']               = 
$this->db->f('name');
-                               $category['descr']              = 
$this->db->f('descr');
-                               $category['prefix']             = 
$this->db->f('prefix');
-                               $category['lookup_tenant']      = 
$this->db->f('lookup_tenant');
-                               $category['tracking']   = 
$this->db->f('tracking');
-                               $category['location_level']     = 
$this->db->f('location_level');
-                               $category['fileupload'] = 
$this->db->f('fileupload');
-                               $category['loc_link']   = 
$this->db->f('loc_link');
-                               $category['start_project']      = 
$this->db->f('start_project');
-                               return $category;
-                       }
-               }
-
-               function read_category_name($entity_id,$cat_id)
-               {
-                       $sql = "SELECT * FROM fm_entity_category where 
entity_id=$entity_id AND id=$cat_id";
-                       $this->db->query($sql,__LINE__,__FILE__);
-                       $this->db->next_record();
-                       return $this->db->f('name');
-               }
-
-               function read_single_status($entity_id,$cat_id,$id)
-               {
-
-                       $table  = 'fm_entity_'. $entity_id .'_'.$cat_id . 
'_status';
-                       $sql = "SELECT * FROM $table  where id='$id'";
-
-                       $this->db->query($sql,__LINE__,__FILE__);
-
-                       if ($this->db->next_record())
-                       {
-                               $status['id']                           = 
$this->db->f('id');
-                               $status['descr']                        = 
$this->db->f('descr');
-
-                               return $status;
-                       }
-               }
-
-               function add_entity($entity)
-               {
-                       $entity['name'] = 
$this->db->db_addslashes($entity['name']);
-                       $entity['descr'] = 
$this->db->db_addslashes($entity['descr']);
-
-                       $entity['id'] = $this->bocommon->next_id('fm_entity');
-
-                       $values= array(
-                               $entity['id'],
-                               $entity['name'],
-                               $entity['descr'],
-                               $entity['location_form'],
-                               $entity['documentation']
-                               );
-
-                       $values = $this->bocommon->validate_db_insert($values);
-
-                       $this->db->query("INSERT INTO fm_entity (id,name, 
descr,location_form,documentation) "
-                               . "VALUES ($values)",__LINE__,__FILE__);
-
-
-                       $values_acl_location= array(
-                               '.entity.' . $entity['id'],
-                               $entity['name'],
-                               1
-                               );
-
-                       $values_acl_location    = 
$this->bocommon->validate_db_insert($values_acl_location);
-
-                       $this->db->query("INSERT INTO fm_acl_location 
(id,descr,allow_grant) "
-                               . "VALUES 
($values_acl_location)",__LINE__,__FILE__);
-
-                       $receipt['id']= $entity['id'];
-
-                       $receipt['message'][] = array('msg'=> lang('entity has 
been added'));
-                       return $receipt;
-               }
-
-
-               function add_status($values,$entity_id,$cat_id)
-               {
-                       $values['id'] = $this->db->db_addslashes($values['id']);
-                       $values['descr'] = 
$this->db->db_addslashes($values['descr']);
-
-                       $values_insert= array(
-                               $values['id'],
-                               $values['descr'],
-                               );
-
-                       $values_insert  = 
$this->bocommon->validate_db_insert($values_insert);
-
-                       $table  = 'fm_entity_'. $entity_id .'_'.$cat_id . 
'_status';
-
-                       $this->db->query("INSERT INTO $table (id,descr) VALUES 
($values_insert)",__LINE__,__FILE__);
-
-                       $receipt['id']= $values['id'];
-
-                       $receipt['message'][] = array('msg'=> lang('status has 
been added'));
-                       return $receipt;
-               }
-
-
-               function add_category($values)
-               {
-                       $values['name'] = 
$this->db->db_addslashes($values['name']);
-                       $values['descr'] = 
$this->db->db_addslashes($values['descr']);
-
-                       $values['id'] = 
$this->bocommon->next_id('fm_entity_category',array('entity_id'=>$values['entity_id']));
-
-                       $location_type = 
$this->bocommon->next_id('fm_location_type');
-
-                       $values_insert= array(
-                               $values['entity_id'],
-                               $values['id'],
-                               $values['name'],
-                               $values['descr'],
-                               $values['prefix'],
-                               $values['lookup_tenant'],
-                               $values['tracking'],
-                               $values['location_level'],
-                               $values['fileupload'],
-                               $values['loc_link'],
-                               $values['start_project']
-                               );
-
-                       $values_insert  = 
$this->bocommon->validate_db_insert($values_insert);
-
-
-                       $this->db->transaction_begin();
-
-                       $this->db->query("INSERT INTO fm_entity_category 
(entity_id,id,name, 
descr,prefix,lookup_tenant,tracking,location_level,fileupload,loc_link,start_project)
 "
-                               . "VALUES ($values_insert)",__LINE__,__FILE__);
-
-                       $values_acl_location= array(
-                               '.entity.' . $values['entity_id'] . '.' . 
$values['id'],
-                               $values['name'],
-                               1
-                               );
-
-                       $values_acl_location    = 
$this->bocommon->validate_db_insert($values_acl_location);
-
-                       $this->db->query("INSERT INTO fm_acl_location 
(id,descr,allow_grant) "
-                               . "VALUES 
($values_acl_location)",__LINE__,__FILE__);
-
-                       $receipt['id']= $values['id'];
-
-                       $this->init_process();
-
-                       $fd=array();
-                       $fd['id'] = array('type' => 'int', 'precision' => 4, 
'nullable' => False);
-                       $fd['num'] = array('type' => 'varchar', 'precision' => 
16, 'nullable' => False);
-                       $fd['p_num'] = array('type' => 'varchar', 'precision' 
=> 16, 'nullable' => True);
-                       $fd['p_entity_id'] = array('type' => 'int', 'precision' 
=> 4, 'nullable' => True);
-                       $fd['p_cat_id'] = array('type' => 'int', 'precision' => 
4, 'nullable' => True);
-                       $fd['location_code'] = array('type' => 'varchar', 
'precision' => 25, 'nullable' => True);
-
-                       for ($i=1; $i<$location_type; $i++)
-                       {
-                               $fd['loc' . $i] = array('type' => 'varchar', 
'precision' => 4, 'nullable' => True);
-                       }
-
-                       $fd['address'] = array('type' => 'varchar', 'precision' 
=> 150, 'nullable' => True);
-                       $fd['tenant_id'] = array('type' => 'int', 'precision' 
=> 4, 'nullable' => True);
-                       $fd['contact_phone'] = array('type' => 'varchar', 
'precision' => 30, 'nullable' => True);
-                       $fd['status'] = array('type' => 'int', 'precision' => 
4, 'nullable' => True);
-                       $fd['entry_date'] = array('type' => 'int', 'precision' 
=> 4, 'nullable' => True);
-                       $fd['user_id'] = array('type' => 'int', 'precision' => 
4, 'nullable' => True);
-
-                       $pk[]= 'id';
-                       $table                  = 'fm_entity_'. 
$values['entity_id'] .'_'.$values['id'];
-/*
-                       $fd_status['id'] = array('type' => 'varchar', 
'precision' => 20, 'nullable' => False);
-                       $fd_status['descr'] = array('type' => 'varchar', 
'precision' => 255, 'nullable' => False);
-                       $pk_status[]= 'id';
-
-                       $statustable    = $table . '_' .'status';
-*/
-                       if(($this->oProc->CreateTable($table,array('fd' => 
$fd,'pk' => $pk,'fk' => $fk,'ix' => array('location_code'),'uc' => array()))))
-//                             && 
($this->oProc->CreateTable($statustable,array('fd' => $fd_status,'pk' => 
$pk_status,'fk' => $fk_status,'ix' => False,'uc' => array()))))
-                       {
-
-                               $values_insert= array(
-                                       $values['entity_id'],
-                                       $values['id'],
-                                       1,
-                                       'status',
-                                       'Status',
-                                       'Status',
-                                       'LB',
-                                       1,
-                                       'True'
-                                       );
-
-                               $values_insert  = 
$this->bocommon->validate_db_insert($values_insert);
-
-                               $this->db->query("INSERT INTO 
fm_entity_attribute 
(entity_id,cat_id,id,column_name,input_text,statustext,datatype,attrib_sort,nullable)
 "
-                                       . "VALUES 
($values_insert)",__LINE__,__FILE__);
-
-                               $receipt['message'][] = array('msg'     => 
lang('table %1 has been saved',$table));
-                               $this->db->transaction_commit();
-                       }
-                       else
-                       {
-                               $receipt['error'][] = array('msg'       => 
lang('table could not be added')     );
-                               if($this->db->Transaction)
-                               {
-                                       $this->db->transaction_abort();
-                               }
-                               else
-                               {
-                                       $this->db->query("DELETE FROM 
fm_entity_category WHERE id=" . $values['id'] . " AND entity_id=" . 
$values['entity_id'],__LINE__,__FILE__);
-                                       unset($receipt['id']);
-
-                               }
-                       }
-
-                       return $receipt;
-               }
-
-               function edit_status($values,$entity_id,$cat_id)
-               {
-                       $table  = 'fm_entity_'. $entity_id .'_'.$cat_id . 
'_status';
-
-                       $values['descr'] = 
$this->db->db_addslashes($values['descr']);
-
-                       $value_set=array(
-                               'descr'                 => $values['descr'],
-                               );
-
-                       $value_set      = 
$this->bocommon->validate_db_update($value_set);
-
-                       $this->db->query("UPDATE $table set $value_set WHERE 
id='" . $values['id'] . "'",__LINE__,__FILE__);
-
-                       $receipt['message'][] = array('msg'=> lang('Status has 
been edited'));
-
-                       return $receipt;
-               }
-
-               function edit_entity($entity)
-               {
-                       if (!$entity['name'])
-                       {
-                               $receipt['error'][] = array('msg'=>lang('Name 
not entered!'));
-                       }
-
-                       if (!$receipt['error'])
-                       {
-                               $table = 'fm_entity';
-
-                               $entity['name'] = 
$this->db->db_addslashes($entity['name']);
-                               $entity['descr'] = 
$this->db->db_addslashes($entity['descr']);
-
-                               if(!$entity['location_form'])
-                               {
-                                       unset($entity['lookup_entity']);
-                               }
-
-                               $value_set=array(
-                               'descr'                 => $entity['descr'],
-                               'name'                  => $entity['name'],
-                               'location_form' => $entity['location_form'],
-                               'lookup_entity' => 
serialize($entity['lookup_entity']),
-                               'documentation' => $entity['documentation']
-                               );
-
-                               $value_set      = 
$this->bocommon->validate_db_update($value_set);
-
-                               $this->db->transaction_begin();
-
-                               $this->db->query("UPDATE $table set $value_set 
WHERE id=" . $entity['id'],__LINE__,__FILE__);
-
-                               $value_set_acl=array(
-                                       'descr'                 => 
$entity['name']
-                                       );
-
-                               $value_set_acl  = 
$this->bocommon->validate_db_update($value_set_acl);
-
-                               $this->db->query("UPDATE fm_acl_location set 
$value_set_acl WHERE id='.entity." . $entity['id']. "'",__LINE__,__FILE__);
-
-                               $this->db->query("DELETE FROM fm_entity_lookup 
WHERE type='lookup' AND entity_id=" . $entity['id'],__LINE__,__FILE__);
-                               if (isset($entity['include_entity_for']) AND 
is_array($entity['include_entity_for']))
-                               {
-                                       foreach($entity['include_entity_for'] 
as $location)
-                                       {
-                                               $this->db->query("INSERT INTO 
fm_entity_lookup (entity_id,location,type)"
-                                               . "VALUES (" .$entity['id'] . 
",'$location','lookup' )",__LINE__,__FILE__);
-                                       }
-                               }
-
-                               $this->db->query("DELETE FROM fm_entity_lookup 
WHERE type='start' AND entity_id=" . $entity['id'],__LINE__,__FILE__);
-
-                               if (isset($entity['start_entity_from']) AND 
is_array($entity['start_entity_from']))
-                               {
-                                       foreach($entity['start_entity_from'] as 
$location)
-                                       {
-                                               $this->db->query("INSERT INTO 
fm_entity_lookup (entity_id,location,type)"
-                                               . "VALUES (" .$entity['id'] . 
",'$location','start' )",__LINE__,__FILE__);
-                                       }
-                               }
-
-                               $this->db->transaction_commit();
-
-                               $receipt['message'][] = array('msg'=> 
lang('entity has been edited'));
-                       }
-                       else
-                       {
-                               $receipt['error'][] = array('msg'       => 
lang('entity has NOT been edited'));
-                       }
-
-                       return $receipt;
-               }
-
-               function edit_category($entity)
-               {
-
-                       if (!$entity['name'])
-                       {
-                               $receipt['error'][] = array('msg'=>lang('Name 
not entered!'));
-                       }
-
-                       if (!$receipt['error'])
-                       {
-                               $table = 'fm_entity_category';
-
-                               $entity['name'] = 
$this->db->db_addslashes($entity['name']);
-                               $entity['descr'] = 
$this->db->db_addslashes($entity['descr']);
-
-                               $value_set=array(
-                                       'descr'                 => 
$entity['descr'],
-                                       'name'                  => 
$entity['name'],
-                                       'prefix'                => 
$entity['prefix'],
-                                       'lookup_tenant' => 
$entity['lookup_tenant'],
-                                       'tracking'              => 
$entity['tracking'],
-                                       'location_level'=> 
$entity['location_level'],
-                                       'fileupload'    => 
$entity['fileupload'],
-                                       'loc_link'              => 
$entity['loc_link'],
-                                       'start_project' => 
$entity['start_project']
-                                       );
-
-                               $value_set      = 
$this->bocommon->validate_db_update($value_set);
-
-                               $this->db->query("UPDATE $table set $value_set 
WHERE entity_id=" . $entity['entity_id']. " AND id=" . 
$entity['id'],__LINE__,__FILE__);
-
-                               $value_set_acl=array(
-                                       'descr'                 => 
$entity['name']
-                                       );
-
-                               $value_set_acl  = 
$this->bocommon->validate_db_update($value_set_acl);
-
-                               $this->db->query("UPDATE fm_acl_location set 
$value_set_acl WHERE id='.entity." . $entity['entity_id']. "." . $entity['id']. 
"'",__LINE__,__FILE__);
-
-
-                               $receipt['message'][] = array('msg'=> 
lang('entity has been edited'));
-                       }
-                       else
-                       {
-                               $receipt['error'][] = array('msg'       => 
lang('entity has NOT been edited'));
-                       }
-
-                       return $receipt;
-               }
-
-               function delete_entity($id)
-               {
-                       
$category_list=$this->read_category(array('entity_id'=>$id));
-                       $this->db->query("DELETE FROM fm_entity WHERE 
id=$id",__LINE__,__FILE__);
-                       $this->db->query("DELETE FROM fm_entity_category WHERE 
entity_id=$id",__LINE__,__FILE__);
-                       $this->db->query("DELETE FROM fm_entity_attribute WHERE 
entity_id=$id",__LINE__,__FILE__);
-                       $this->db->query("DELETE FROM fm_acl_location WHERE id 
$this->like '.entity." . $id ."%'",__LINE__,__FILE__);
-                       $this->db->query("DELETE FROM fm_acl2 WHERE 
acl_location $this->like '.entity." . $id ."%'",__LINE__,__FILE__);
-                       if (isset($category_list) AND is_array($category_list))
-                       {
-                               $this->init_process();
-
-                               foreach($category_list as $entry)
-                               {
-                                       $this->oProc->DropTable('fm_entity_' . 
$id . '_' . $entry['id']);
-                               }
-                       }
-
-               }
-
-               function delete_category($id,$entity_id)
-               {
-                       $this->init_process();
-                       $this->oProc->DropTable('fm_entity_' . $entity_id . '_' 
. $id);
-//                     $this->oProc->DropTable('fm_entity_' . $entity_id . '_' 
. $id . '_' . 'status');
-                       $this->db->query("DELETE FROM fm_entity_category WHERE 
entity_id= $entity_id AND id= $id",__LINE__,__FILE__);
-                       $this->db->query("DELETE FROM fm_entity_attribute WHERE 
entity_id= $entity_id AND cat_id= $id",__LINE__,__FILE__);
-                       $this->db->query("DELETE FROM fm_acl_location WHERE 
id='.entity." . $entity_id . "." . $id ."'",__LINE__,__FILE__);
-                       $this->db->query("DELETE FROM fm_acl2 WHERE 
acl_location='.entity." . $entity_id . "." . $id ."'",__LINE__,__FILE__);
-               }
-
-               function delete_attrib($cat_id,$entity_id,$attrib_id)
-               {
-                       $this->init_process();
-
-                       $sql = "SELECT * FROM fm_entity_attribute WHERE 
entity_id=$entity_id AND cat_id=$cat_id AND id=$attrib_id";
-
-                       $this->db->query($sql,__LINE__,__FILE__);
-                       $this->db->next_record();
-                       $ColumnName             = $this->db->f('column_name');
-
-                       $this->oProc->DropColumn('fm_entity_' .$entity_id.'_'. 
$cat_id,'', $ColumnName);
-
-                       $sql = "SELECT attrib_sort FROM fm_entity_attribute 
where entity_id=$entity_id AND cat_id=$cat_id AND id=$attrib_id";
-                       $this->db->query($sql,__LINE__,__FILE__);
-                       $this->db->next_record();
-                       $attrib_sort    = $this->db->f('attrib_sort');
-                       $sql2 = "SELECT max(attrib_sort) as max_sort FROM 
fm_entity_attribute where entity_id=$entity_id AND cat_id=$cat_id";
-                       $this->db->query($sql2,__LINE__,__FILE__);
-                       $this->db->next_record();
-                       $max_sort       = $this->db->f('max_sort');
-                       if($max_sort>$attrib_sort)
-                       {
-                               $sql = "UPDATE fm_entity_attribute set 
attrib_sort=attrib_sort-1 WHERE entity_id=$entity_id AND cat_id=$cat_id AND 
attrib_sort > $attrib_sort";
-                               $this->db->query($sql,__LINE__,__FILE__);
-                       }
-
-                       $this->db->query("DELETE FROM fm_entity_attribute WHERE 
entity_id=$entity_id AND cat_id=$cat_id AND id=$attrib_id",__LINE__,__FILE__);
-               }
-
-               function delete_status($cat_id,$entity_id,$status_id)
-               {
-                       $table  = 'fm_entity_'. $entity_id .'_'.$cat_id . 
'_status';
-
-                       $this->db->query("DELETE FROM $table WHERE 
id='$status_id'",__LINE__,__FILE__);
-               }
-
-               function read_attrib($data)
-               {
-
-//_debug_array($data);
-                       if(is_array($data))
-                       {
-                               if ($data['start'])
-                               {
-                                       $start=$data['start'];
-                               }
-                               else
-                               {
-                                       $start=0;
-                               }
-                               $query = 
(isset($data['query'])?$data['query']:'');
-                               $sort = 
(isset($data['sort'])?$data['sort']:'DESC');
-                               $order = 
(isset($data['order'])?$data['order']:'');
-                               $allrows = 
(isset($data['allrows'])?$data['allrows']:'');
-                               $entity_id = 
(isset($data['entity_id'])?$data['entity_id']:0);
-                               $cat_id = 
(isset($data['cat_id'])?$data['cat_id']:0);
-                       }
-
-                       if ($order)
-                       {
-                               $ordermethod = " order by $order $sort";
-
-                       }
-                       else
-                       {
-                               $ordermethod = ' order by attrib_sort asc';
-                       }
-
-                       if($query)
-                       {
-                               $query = ereg_replace("'",'',$query);
-                               $query = ereg_replace('"','',$query);
-
-                               $querymethod = " AND (fm_entity_attribute.name 
$this->like '%$query%' or fm_entity_attribute.descr $this->like '%$query%')";
-                       }
-
-                       $sql = "SELECT * FROM fm_entity_attribute WHERE 
entity_id=$entity_id AND cat_id = $cat_id $filtermethod $querymethod";
-
-                       $this->db2->query($sql,__LINE__,__FILE__);
-                       $this->total_records = $this->db2->num_rows();
-                       if(!$allrows)
-                       {
-                               $this->db->limit_query($sql . 
$ordermethod,$start,__LINE__,__FILE__);
-                       }
-                       else
-                       {
-                               $this->db->query($sql . 
$ordermethod,__LINE__,__FILE__);
-                       }
-
-
-                       while ($this->db->next_record())
-                       {
-                               $attrib[] = array
-                               (
-                                       'id'                    => 
$this->db->f('id'),
-                                       'entity_type'   => 
$this->db->f('type_id'),
-                                       'attrib_sort'   => 
$this->db->f('attrib_sort'),
-                                       'list'                  => 
$this->db->f('list'),
-                                       'lookup_form'   => 
$this->db->f('lookup_form'),
-                                       'entity_form'   => 
$this->db->f('entity_form'),
-                                       'column_name'   => 
$this->db->f('column_name'),
-                                       'name'                  => 
$this->db->f('input_text'),
-                                       'size'                  => 
$this->db->f('size'),
-                                       'statustext'    => 
$this->db->f('statustext'),
-                                       'input_text'    => 
$this->db->f('input_text'),
-                                       'type_name'             => 
$this->db->f('type'),
-                                       'datatype'              => 
$this->db->f('datatype'),
-                                       'search'                => 
$this->db->f('search')
-                       );
-                       }
-                       return $attrib;
-               }
-
-               function read_single_attrib($entity_id,$cat_id,$id)
-               {
-
-                       $sql = "SELECT * FROM fm_entity_attribute where 
entity_id=$entity_id AND cat_id=$cat_id AND id=$id";
-
-                       $this->db->query($sql,__LINE__,__FILE__);
-
-                       if ($this->db->next_record())
-                       {
-                               $attrib['id']                                   
        = $this->db->f('id');
-                               $attrib['column_name']                          
= $this->db->f('column_name');
-                               $attrib['input_text']                           
= $this->db->f('input_text');
-                               $attrib['statustext']                           
= $this->db->f('statustext');
-                               $attrib['column_info']['precision']     = 
$this->db->f('precision_');
-                               $attrib['column_info']['scale']         = 
$this->db->f('scale');
-                               $attrib['column_info']['default']       = 
$this->db->f('default_value');
-                               $attrib['column_info']['nullable']      = 
$this->db->f('nullable');
-                               $attrib['column_info']['type']          = 
$this->db->f('datatype');
-                               $attrib['type_id']                              
        = $this->db->f('type_id');
-                               $attrib['type_name']                            
= $this->db->f('type_name');
-                               $attrib['lookup_form']                          
= $this->db->f('lookup_form');
-                               $attrib['list']                                 
        = $this->db->f('list');
-                               $attrib['search']                               
        = $this->db->f('search');
-                               if($this->db->f('datatype')=='R' || 
$this->db->f('datatype')=='CH' || $this->db->f('datatype')=='LB')
-                               {
-                                       $attrib['choice'] = 
$this->read_attrib_choice($entity_id,$cat_id,$id);
-                               }
-
-                               return $attrib;
-                       }
-               }
-
-               function read_attrib_choice($entity_id,$cat_id,$attrib_id)
-               {
-                       $choice_table = 'fm_entity_choice';
-                       $sql = "SELECT * FROM $choice_table WHERE 
entity_id=$entity_id AND cat_id=$cat_id AND attrib_id=$attrib_id";
-                       $this->db->query($sql,__LINE__,__FILE__);
-
-                       while ($this->db->next_record())
-                       {
-                               $choice[] = array
-                               (
-                                       'id'    => $this->db->f('id'),
-                                       'value' => $this->db->f('value')
-                               );
-                       }
-                       return $choice;
-               }
-
-               function add_attrib($attrib)
-               {
-
-                       $attrib['column_name'] = 
$this->db->db_addslashes($attrib['column_name']);
-                       $attrib['input_text'] = 
$this->db->db_addslashes($attrib['input_text']);
-                       $attrib['statustext'] = 
$this->db->db_addslashes($attrib['statustext']);
-                       $attrib['default'] = 
$this->db->db_addslashes($attrib['default']);
-                       $attrib['id'] = 
$this->bocommon->next_id('fm_entity_attribute',array('entity_id'=>$attrib['entity_id'],'cat_id'=>$attrib['cat_id']));
-
-                       $sql = "SELECT max(attrib_sort) as max_sort FROM 
fm_entity_attribute where entity_id=" . $attrib['entity_id'] . " AND cat_id=" . 
$attrib['cat_id'];
-                       $this->db->query($sql,__LINE__,__FILE__);
-                       $this->db->next_record();
-                       $attrib_sort    = $this->db->f('max_sort')+1;
-
-                       $values= array(
-                               $attrib['entity_id'],
-                               $attrib['cat_id'],
-                               $attrib['id'],
-                               $attrib['column_name'],
-                               $attrib['input_text'],
-                               $attrib['statustext'],
-                               $attrib['search'],
-                               $attrib['list'],
-                               $attrib_sort,
-                               $attrib['column_info']['type'],
-                               $attrib['column_info']['precision'],
-                               $attrib['column_info']['scale'],
-                               $attrib['column_info']['default'],
-                               $attrib['column_info']['nullable']
-                               );
-
-                       $values = $this->bocommon->validate_db_insert($values);
-
-                       $this->db->transaction_begin();
-
-                       $this->db->query("INSERT INTO fm_entity_attribute 
(entity_id,cat_id,id,column_name, input_text, 
statustext,search,list,attrib_sort, 
datatype,precision_,scale,default_value,nullable) "
-                               . "VALUES ($values)",__LINE__,__FILE__);
-
-                       $receipt['id']= $attrib['id'];
-
-                       if($attrib['column_info']['type']=='email' && 
!$attrib['column_info']['precision'])
-                       {
-                               $attrib['column_info']['precision']=64;
-                       }
-
-                       $attrib['column_info']['type']  = 
$this->bocommon->translate_datatype_insert($attrib['column_info']['type']);
-
-                       if($attrib['column_info']['type']=='int' && 
!$attrib['column_info']['precision'])
-                       {
-                               $attrib['column_info']['precision']=4;
-                       }
-
-                       if(!$attrib['column_info']['default'])
-                       {
-                               unset($attrib['column_info']['default']);
-                       }
-
-                       $this->init_process();
-
-                       
if($this->oProc->AddColumn('fm_entity_'.$attrib['entity_id'] . '_' . 
$attrib['cat_id'],$attrib['column_name'], $attrib['column_info']))
-                       {
-                               $receipt['message'][] = array('msg'     => 
lang('Attribute has been saved')     );
-                               $this->db->transaction_commit();
-
-                       }
-                       else
-                       {
-                               $receipt['error'][] = array('msg'       => 
lang('column could not be added')    );
-                               if($this->db->Transaction)
-                               {
-                                       $this->db->transaction_abort();
-                               }
-                               else
-                               {
-                                       $this->db->query("DELETE FROM 
fm_entity_attribute WHERE entity_id=" . $attrib['entity_id']. " AND cat_id=" . 
$attrib['id']. " AND id='" . $receipt['id'] . "'",__LINE__,__FILE__);
-                                       unset($receipt['id']);
-
-                               }
-                       }
-
-                       return $receipt;
-               }
-
-               function init_process()
-               {
-                       $this->oProc                                            
= 
CreateObject('phpgwapi.schema_proc',$GLOBALS['phpgw_info']['server']['db_type']);
-                       $this->oProc->m_odb                                     
= $this->db;
-                       $this->oProc->m_odb->Halt_On_Error      = 'report';
-               }
-
-               function edit_attrib($attrib)
-               {
-
-                       $choice_table = 'fm_entity_choice';
-
-                       $attrib['column_name'] = 
$this->db->db_addslashes($attrib['column_name']);
-                       $attrib['input_text'] = 
$this->db->db_addslashes($attrib['input_text']);
-                       $attrib['statustext'] = 
$this->db->db_addslashes($attrib['statustext']);
-                       $attrib['default'] = 
$this->db->db_addslashes($attrib['default']);
-
-                       $this->db->query("SELECT column_name FROM 
fm_entity_attribute WHERE entity_id=" . $attrib['entity_id']. " AND cat_id=" . 
$attrib['cat_id']. " AND id='" . $attrib['id']. "'",__LINE__,__FILE__);
-                       $this->db->next_record();
-                       $OldColumnName          = $this->db->f('column_name');
-
-                       $this->db->transaction_begin();
-
-                               $value_set=array(
-                                       'input_text'    => 
$attrib['input_text'],
-                                       'statustext'    => 
$attrib['statustext'],
-                                       'search'                => 
$attrib['search'],
-                                       'list'                  => 
$attrib['list'],
-                                       );
-
-                               $value_set      = 
$this->bocommon->validate_db_update($value_set);
-
-                               $this->db->query("UPDATE fm_entity_attribute 
set $value_set WHERE entity_id=" . $attrib['entity_id']. " AND cat_id=" . 
$attrib['cat_id']. " AND id=" . $attrib['id'],__LINE__,__FILE__);
-
-                       if($OldColumnName !=$attrib['column_name'])
-                       {
-
-                               if($attrib['column_info']['type']!='R' && 
$attrib['column_info']['type']!='CH' && $attrib['column_info']['type']!='LB')
-                               {
-                                       $this->db->query("DELETE FROM 
$choice_table WHERE entity_id=" . $attrib['entity_id']. " AND cat_id=" . 
$attrib['cat_id']. " AND attrib_id=" . $attrib['id'],__LINE__,__FILE__);
-                               }
-
-                               if($attrib['column_info']['type']=='email' && 
!$attrib['column_info']['precision'])
-                               {
-                                       $attrib['column_info']['precision']=64;
-                               }
-
-                               if($precision = 
$this->bocommon->translate_datatype_precision($attrib['column_info']['type']))
-                               {
-                                       
$attrib['column_info']['precision']=$precision;
-                               }
-
-                               if(!$attrib['column_info']['default'])
-                               {
-                                       
unset($attrib['column_info']['default']);
-                               }
-
-                               $value_set=array(
-                                       'column_name'   => 
$attrib['column_name'],
-                                       'datatype'              => 
$attrib['column_info']['type'],
-                                       'precision_'    => 
$attrib['column_info']['precision'],
-                                       'scale'                 => 
$attrib['column_info']['scale'],
-                                       'default_value' => 
$attrib['column_info']['default'],
-                                       'nullable'              => 
$attrib['column_info']['nullable']
-                                       );
-
-                               $value_set      = 
$this->bocommon->validate_db_update($value_set);
-
-                               $this->db->query("UPDATE fm_entity_attribute 
set $value_set WHERE entity_id=" . $attrib['entity_id']. " AND cat_id=" . 
$attrib['cat_id']. " AND id=" . $attrib['id'],__LINE__,__FILE__);
-
-                               $attrib['column_info']['type']  = 
$this->bocommon->translate_datatype_insert($attrib['column_info']['type']);
-
-                               $this->init_process();
-
-                               $mini_table_def = array(
-                                       'fm_entity_'.$attrib['entity_id'] . '_' 
. $attrib['cat_id'] =>  array(
-                                               'fd' => array(
-                                                       $OldColumnName  
=>$attrib['column_info']),
-                                               'pk' => array(),
-                                               'fk' => array(),
-                                               'ix' => array(),
-                                               'uc' => array()
-                                               )
-                                       );
-
-                               $this->oProc->GenerateScripts($mini_table_def);
-                               
$this->oProc->RenameColumn('fm_entity_'.$attrib['entity_id'] . '_' . 
$attrib['cat_id'], $OldColumnName, $attrib['column_name']);
-                               
$this->oProc->AlterColumn('fm_entity_'.$attrib['entity_id'] . '_' . 
$attrib['cat_id'],$attrib['column_name'],$attrib['column_info']);
-                       }
-
-                       $choice_table ='fm_entity_choice';
-
-                       if($attrib['new_choice'])
-                       {
-                               $choice_id = 
$this->bocommon->next_id($choice_table 
,array('entity_id'=>$attrib['entity_id'],'cat_id'=>$attrib['cat_id'],'attrib_id'=>$attrib['id']));
-
-                               $values= array(
-                                       $attrib['entity_id'],
-                                       $attrib['cat_id'],
-                                       $attrib['id'],
-                                       $choice_id,
-                                       $attrib['new_choice']
-                                       );
-
-                               $values = 
$this->bocommon->validate_db_insert($values);
-
-                               $this->db->query("INSERT INTO $choice_table 
(entity_id,cat_id,attrib_id,id,value) "
-                               . "VALUES ($values)",__LINE__,__FILE__);
-                       }
-
-                       if($attrib['delete_choice'])
-                       {
-                               for 
($i=0;$i<count($attrib['delete_choice']);$i++)
-                               {
-                                       $this->db->query("DELETE FROM 
$choice_table WHERE entity_id=" . $attrib['entity_id']. " AND cat_id=" . 
$attrib['cat_id']. " AND attrib_id=" . $attrib['id']  ." AND id=" . 
$attrib['delete_choice'][$i],__LINE__,__FILE__);
-                               }
-                       }
-
-                       $this->db->transaction_commit();
-
-                       $receipt['message'][] = array('msg'     => 
lang('Attribute has been edited'));
-
-                       return $receipt;
-               }
-
-               function resort_attrib($data)
-               {
-                       if(is_array($data))
-                       {
-                               $resort = 
(isset($data['resort'])?$data['resort']:'up');
-                               $entity_id = 
(isset($data['entity_id'])?$data['entity_id']:0);
-                               $cat_id = 
(isset($data['cat_id'])?$data['cat_id']:0);
-                               $id = (isset($data['id'])?$data['id']:'');
-                       }
-
-                       $sql = "SELECT attrib_sort FROM fm_entity_attribute 
where entity_id=$entity_id AND cat_id=$cat_id AND id=$id";
-                       $this->db->query($sql,__LINE__,__FILE__);
-                       $this->db->next_record();
-                       $attrib_sort    = $this->db->f('attrib_sort');
-                       $sql2 = "SELECT max(attrib_sort) as max_sort FROM 
fm_entity_attribute where entity_id=$entity_id AND cat_id=$cat_id";
-                       $this->db->query($sql2,__LINE__,__FILE__);
-                       $this->db->next_record();
-                       $max_sort       = $this->db->f('max_sort');
-
-                       switch($resort)
-                       {
-                               case 'up':
-                                       if($attrib_sort>1)
-                                       {
-                                               $sql = "UPDATE 
fm_entity_attribute set attrib_sort=$attrib_sort WHERE entity_id=$entity_id AND 
cat_id=$cat_id AND attrib_sort =" . ($attrib_sort-1);
-                                               
$this->db->query($sql,__LINE__,__FILE__);
-                                               $sql = "UPDATE 
fm_entity_attribute set attrib_sort=" . ($attrib_sort-1) ." WHERE 
entity_id=$entity_id AND cat_id=$cat_id AND id=$id";
-                                               
$this->db->query($sql,__LINE__,__FILE__);
-                                       }
-                                       break;
-                               case 'down':
-                                       if($max_sort > $attrib_sort)
-                                       {
-                                               $sql = "UPDATE 
fm_entity_attribute set attrib_sort=$attrib_sort WHERE entity_id=$entity_id AND 
cat_id=$cat_id AND attrib_sort =" . ($attrib_sort+1);
-                                               
$this->db->query($sql,__LINE__,__FILE__);
-                                               $sql = "UPDATE 
fm_entity_attribute set attrib_sort=" . ($attrib_sort+1) ." WHERE 
entity_id=$entity_id AND cat_id=$cat_id AND id=$id";
-                                               
$this->db->query($sql,__LINE__,__FILE__);
-                                       }
-                                       break;
-                               default:
-                                       return;
-                                       break;
-                       }
-               }
-
-               function read_custom_function($data)
-               {
-                       if(is_array($data))
-                       {
-                               if ($data['start'])
-                               {
-                                       $start=$data['start'];
-                               }
-                               else
-                               {
-                                       $start=0;
-                               }
-                               $query = 
(isset($data['query'])?$data['query']:'');
-                               $sort = 
(isset($data['sort'])?$data['sort']:'DESC');
-                               $order = 
(isset($data['order'])?$data['order']:'');
-                               $allrows = 
(isset($data['allrows'])?$data['allrows']:'');
-                               $acl_location = 
(isset($data['acl_location'])?$data['acl_location']:'');
-                       }
-
-                       if(!$acl_location)
-                       {
-                               return;
-                       }
-
-                       if ($order)
-                       {
-                               $ordermethod = " order by $order $sort";
-
-                       }
-                       else
-                       {
-                               $ordermethod = ' order by custom_sort asc';
-                       }
-
-                       $table = 'fm_custom_function';
-
-                       if($query)
-                       {
-                               $query = ereg_replace("'",'',$query);
-                               $query = ereg_replace('"','',$query);
-
-                               $querymethod = " AND name $this->like 
'%$query%' or descr $this->like '%$query%'";
-                       }
-
-                       $sql = "SELECT * FROM $table WHERE 
acl_location='$acl_location' $querymethod";
-
-                       $this->db2->query($sql,__LINE__,__FILE__);
-                       $this->total_records = $this->db2->num_rows();
-
-                       if(!$allrows)
-                       {
-                               $this->db->limit_query($sql . 
$ordermethod,$start,__LINE__,__FILE__);
-                       }
-                       else
-                       {
-                               $this->db->query($sql . 
$ordermethod,__LINE__,__FILE__);
-                       }
-
-                       while ($this->db->next_record())
-                       {
-                               $custom_function[] = array
-                               (
-                                       'id'    => $this->db->f('id'),
-                                       'file_name'     => 
$this->db->f('file_name'),
-                                       'sorting'       => 
$this->db->f('custom_sort'),
-                                       'descr'         => 
$this->db->f('descr'),
-                                       'active'        => 
$this->db->f('active')
-                               );
-                       }
-                       return $custom_function;
-               }
-
-
-               function read_single_custom_function($acl_location,$id)
-               {
-
-                       $sql = "SELECT * FROM fm_custom_function where 
acl_location='$acl_location' AND id=$id";
-
-                       $this->db->query($sql,__LINE__,__FILE__);
-
-                       if ($this->db->next_record())
-                       {
-                               $custom_function['id']                  = 
$this->db->f('id');
-                               $custom_function['descr']               = 
$this->db->f('descr');
-                               $custom_function['custom_function_file']= 
$this->db->f('file_name');
-                               $custom_function['active']              = 
$this->db->f('active');
-
-                               return $custom_function;
-                       }
-
-               }
-
-               function add_custom_function($custom_function)
-               {
-                       if(!$custom_function['acl_location'] && 
$custom_function['entity_id'] && $custom_function['cat_id'])
-                       {
-                               $acl_location = '.entity.' . 
$custom_function['entity_id'] . '.' . $custom_function['cat_id'];
-                       }
-                       else
-                       {
-                               $acl_location = 
$custom_function['acl_location'];
-                       }
-
-                       if(!$acl_location)
-                       {
-                               return  $receipt['error'][] = array('msg' => 
lang('acl_locastion is missing'));
-                       }
-
-                       $custom_function['descr'] = 
$this->db->db_addslashes($custom_function['descr']);
-
-
-                       $this->db->query("SELECT max(id) as maximum FROM 
fm_custom_function WHERE acl_location='$acl_location'",__LINE__,__FILE__);
-                       $this->db->next_record();
-                       $custom_function['id'] = $this->db->f('maximum')+1;
-
-                       $sql = "SELECT max(custom_sort) as max_sort FROM 
fm_custom_function where acl_location='$acl_location'";
-                       $this->db->query($sql,__LINE__,__FILE__);
-                       $this->db->next_record();
-                       $custom_sort    = $this->db->f('max_sort')+1;
-
-                       $values= array(
-                               $acl_location,
-                               $custom_function['id'],
-                               $custom_function['custom_function_file'],
-                               $custom_function['descr'],
-                               $custom_function['active'],
-                               $custom_sort
-                               );
-
-                       $values = $this->bocommon->validate_db_insert($values);
-
-//_debug_array($custom_function);
-                       $this->db->transaction_begin();
-
-                       $this->db->query("INSERT INTO fm_custom_function 
(acl_location, id, file_name, descr, active, custom_sort) "
-                               . "VALUES ($values)",__LINE__,__FILE__);
-
-                       $receipt['id']= $custom_function['id'];
-
-                       $this->db->transaction_commit();
-
-                       return $receipt;
-               }
-
-               function edit_custom_function($custom_function)
-               {
-                       if(!$custom_function['acl_location'] && 
$custom_function['entity_id'] && $custom_function['cat_id'])
-                       {
-                               $acl_location = '.entity.' . 
$custom_function['entity_id'] . '.' . $custom_function['cat_id'];
-                       }
-                       else
-                       {
-                               $acl_location = 
$custom_function['acl_location'];
-                       }
-
-                       if(!$acl_location)
-                       {
-                               return  $receipt['error'][] = array('msg' => 
lang('acl_locastion is missing'));
-                       }
-
-                       $custom_function['descr'] = 
$this->db->db_addslashes($custom_function['descr']);
-
-                       $this->db->transaction_begin();
-
-                               $value_set=array(
-                                       'descr'         => 
$custom_function['descr'],
-                                       'file_name'     => 
$custom_function['custom_function_file'],
-                                       'active'        => 
$custom_function['active']
-                                       );
-
-                               $value_set      = 
$this->bocommon->validate_db_update($value_set);
-
-                               $this->db->query("UPDATE fm_custom_function set 
$value_set WHERE acl_location='" . $acl_location . "' AND id=" . 
$custom_function['id'],__LINE__,__FILE__);
-
-                       $this->db->transaction_commit();
-
-                       $receipt['message'][] = array('msg'     => lang('Custom 
function has been edited'));
-
-                       return $receipt;
-               }
-
-               function resort_custom_function($data)
-               {
-                       if(is_array($data))
-                       {
-                               $resort = 
(isset($data['resort'])?$data['resort']:'up');
-                               $entity_id = 
(isset($data['entity_id'])?$data['entity_id']:'');
-                               $cat_id = 
(isset($data['cat_id'])?$data['cat_id']:'');
-                               $acl_location = 
(isset($data['acl_location'])?$data['acl_location']:'');
-                               $id = (isset($data['id'])?$data['id']:'');
-                       }
-
-                       if(!$acl_location && $entity_id && $cat_id)
-                       {
-                               $acl_location = '.entity.' . $entity_id . '.' . 
$cat_id;
-                       }
-                       else
-                       {
-                               $acl_location = $acl_location;
-                       }
-
-                       if(!$acl_location)
-                       {
-                               return  $receipt['error'][] = array('msg' => 
lang('acl_locastion is missing'));
-                       }
-
-                       $sql = "SELECT custom_sort FROM fm_custom_function 
where acl_location='$acl_location' AND id=$id";
-                       $this->db->query($sql,__LINE__,__FILE__);
-                       $this->db->next_record();
-                       $custom_sort    = $this->db->f('custom_sort');
-                       $sql2 = "SELECT max(custom_sort) as max_sort FROM 
fm_custom_function where acl_location='$acl_location'";
-                       $this->db->query($sql2,__LINE__,__FILE__);
-                       $this->db->next_record();
-                       $max_sort       = $this->db->f('max_sort');
-
-                       switch($resort)
-                       {
-                               case 'up':
-                                       if($custom_sort>1)
-                                       {
-                                               $sql = "UPDATE 
fm_custom_function set custom_sort=$custom_sort WHERE 
acl_location='$acl_location' AND custom_sort =" . ($custom_sort-1);
-                                               
$this->db->query($sql,__LINE__,__FILE__);
-                                               $sql = "UPDATE 
fm_custom_function set custom_sort=" . ($custom_sort-1) ." WHERE 
acl_location='$acl_location' AND id=$id";
-                                               
$this->db->query($sql,__LINE__,__FILE__);
-                                       }
-                                       break;
-                               case 'down':
-                                       if($max_sort > $custom_sort)
-                                       {
-                                               $sql = "UPDATE 
fm_custom_function set custom_sort=$custom_sort WHERE 
acl_location='$acl_location' AND custom_sort =" . ($custom_sort+1);
-                                               
$this->db->query($sql,__LINE__,__FILE__);
-                                               $sql = "UPDATE 
fm_custom_function set custom_sort=" . ($custom_sort+1) ." WHERE 
acl_location='$acl_location' AND id=$id";
-                                               
$this->db->query($sql,__LINE__,__FILE__);
-                                       }
-                                       break;
-                               default:
-                                       return;
-                                       break;
-                       }
-               }
-
-               function 
delete_custom_function($acl_location,$custom_function_id)
-               {
-                       $sql = "SELECT custom_sort FROM fm_custom_function 
where acl_location='$acl_location' AND id=$custom_function_id";
-                       $this->db->query($sql,__LINE__,__FILE__);
-                       $this->db->next_record();
-                       $custom_sort    = $this->db->f('custom_sort');
-                       $sql2 = "SELECT max(custom_sort) as max_sort FROM 
fm_custom_function where acl_location='$acl_location'";
-                       $this->db->query($sql2,__LINE__,__FILE__);
-                       $this->db->next_record();
-                       $max_sort       = $this->db->f('max_sort');
-                       if($max_sort>$custom_sort)
-                       {
-                               $sql = "UPDATE fm_custom_function set 
custom_sort=custom_sort-1 WHERE acl_location='$acl_location' AND custom_sort > 
$custom_sort";
-                               $this->db->query($sql,__LINE__,__FILE__);
-                       }
-                       $this->db->query("DELETE FROM fm_custom_function WHERE 
acl_location='$acl_location' AND id=$custom_function_id",__LINE__,__FILE__);
-               }
-       }
-?>
+                       $this->join                     = $this->bocommon->join;
+                       $this->like                     = $this->bocommon->like;
+
+                       if($entity_id && $cat_id)
+                       {
+                               $this->category_name    = 
$this->read_category_name($entity_id,$cat_id);
+                       }
+               }
+
+               function reset_fm_cache()
+               {
+                       $this->db->query("DELETE FROM fm_cache 
",__LINE__,__FILE__);
+               }
+
+               function read($data)
+               {
+                       if(is_array($data))
+                       {
+                               if ($data['start'])
+                               {
+                                       $start=$data['start'];
+                               }
+                               else
+                               {
+                                       $start=0;
+                               }
+                               $query = 
(isset($data['query'])?$data['query']:'');
+                               $sort = 
(isset($data['sort'])?$data['sort']:'DESC');
+                               $order = 
(isset($data['order'])?$data['order']:'');
+                               $allrows = 
(isset($data['allrows'])?$data['allrows']:'');
+                       }
+
+                       if ($order)
+                       {
+                               $ordermethod = " order by $order $sort";
+
+                       }
+                       else
+                       {
+                               $ordermethod = ' order by id asc';
+                       }
+
+                       $table = 'fm_entity';
+
+                       if($query)
+                       {
+                               $query = ereg_replace("'",'',$query);
+                               $query = ereg_replace('"','',$query);
+
+                               $querymethod = " where name $this->like 
'%$query%' or descr $this->like '%$query%'";
+                       }
+
+                       $sql = "SELECT * FROM $table $querymethod";
+
+                       $this->db2->query($sql,__LINE__,__FILE__);
+                       $this->total_records = $this->db2->num_rows();
+
+                       if(!$allrows)
+                       {
+                               $this->db->limit_query($sql . 
$ordermethod,$start,__LINE__,__FILE__);
+                       }
+                       else
+                       {
+                               $this->db->query($sql . 
$ordermethod,__LINE__,__FILE__);
+                       }
+
+                       while ($this->db->next_record())
+                       {
+                               $entity[] = array
+                               (
+                                       'id'    => $this->db->f('id'),
+                                       'name'  => $this->db->f('name'),
+                                       'descr' => $this->db->f('descr'),
+                                       'documentation' => 
$this->db->f('documentation')
+                               );
+                       }
+                       return $entity;
+               }
+
+               function read_category($data)
+               {
+                       if(is_array($data))
+                       {
+                               if ($data['start'])
+                               {
+                                       $start=$data['start'];
+                               }
+                               else
+                               {
+                                       $start=0;
+                               }
+                               $query = 
(isset($data['query'])?$data['query']:'');
+                               $sort = 
(isset($data['sort'])?$data['sort']:'DESC');
+                               $order = 
(isset($data['order'])?$data['order']:'');
+                               $allrows = 
(isset($data['allrows'])?$data['allrows']:'');
+                               $entity_id = 
(isset($data['entity_id'])?$data['entity_id']:'');
+                       }
+
+                       if ($order)
+                       {
+                               $ordermethod = " order by $order $sort";
+
+                       }
+                       else
+                       {
+                               $ordermethod = ' order by id asc';
+                       }
+
+                       $table = 'fm_entity_category';
+
+                       if($query)
+                       {
+                               $query = ereg_replace("'",'',$query);
+                               $query = ereg_replace('"','',$query);
+
+                               $querymethod = " AND name $this->like 
'%$query%' or descr $this->like '%$query%'";
+                       }
+
+                       $sql = "SELECT * FROM $table WHERE entity_id=$entity_id 
$querymethod";
+
+                       $this->db2->query($sql,__LINE__,__FILE__);
+                       $this->total_records = $this->db2->num_rows();
+
+                       if(!$allrows)
+                       {
+                               $this->db->limit_query($sql . 
$ordermethod,$start,__LINE__,__FILE__);
+                       }
+                       else
+                       {
+                               $this->db->query($sql . 
$ordermethod,__LINE__,__FILE__);
+                       }
+
+                       while ($this->db->next_record())
+                       {
+                               $standard[] = array
+                               (
+                                       'id'    => $this->db->f('id'),
+                                       'name'  => $this->db->f('name'),
+                                       'prefix'=> $this->db->f('prefix'),
+                                       'descr' => $this->db->f('descr')
+                               );
+                       }
+                       return $standard;
+               }
+
+               function read_status($data)
+               {
+                       if(is_array($data))
+                       {
+                               if ($data['start'])
+                               {
+                                       $start=$data['start'];
+                               }
+                               else
+                               {
+                                       $start=0;
+                               }
+                               $query = 
(isset($data['query'])?$data['query']:'');
+                               $sort = 
(isset($data['sort'])?$data['sort']:'DESC');
+                               $order = 
(isset($data['order'])?$data['order']:'');
+                               $allrows = 
(isset($data['allrows'])?$data['allrows']:'');
+                               $entity_id = 
(isset($data['entity_id'])?$data['entity_id']:'');
+                               $cat_id = 
(isset($data['cat_id'])?$data['cat_id']:'');
+                       }
+
+                       if ($order)
+                       {
+                               $ordermethod = " order by $order $sort";
+
+                       }
+                       else
+                       {
+                               $ordermethod = ' order by id asc';
+                       }
+
+                       $table  = 'fm_entity_'. $entity_id .'_'.$cat_id . 
'_status';
+
+                       if($query)
+                       {
+                               $query = ereg_replace("'",'',$query);
+                               $query = ereg_replace('"','',$query);
+
+                               $querymethod = " AND name $this->like 
'%$query%' or descr $this->like '%$query%'";
+                       }
+
+                       $sql = "SELECT * FROM $table $querymethod";
+
+                       $this->db2->query($sql,__LINE__,__FILE__);
+                       $this->total_records = $this->db2->num_rows();
+
+                       if(!$allrows)
+                       {
+                               $this->db->limit_query($sql . 
$ordermethod,$start,__LINE__,__FILE__);
+                       }
+                       else
+                       {
+                               $this->db->query($sql . 
$ordermethod,__LINE__,__FILE__);
+                       }
+
+                       while ($this->db->next_record())
+                       {
+                               $status[] = array
+                               (
+                                       'id'    => $this->db->f('id'),
+                                       'descr' => $this->db->f('descr')
+                               );
+                       }
+                       return $status;
+               }
+
+
+               function read_single($id)
+               {
+
+                       $sql = "SELECT * FROM fm_entity  where id='$id'";
+
+                       $this->db->query($sql,__LINE__,__FILE__);
+
+                       if ($this->db->next_record())
+                       {
+                               $entity['id']                           = 
$this->db->f('id');
+                               $entity['name']                         = 
$this->db->f('name');
+                               $entity['descr']                        = 
$this->db->f('descr');
+                               $entity['location_form']        = 
$this->db->f('location_form');
+                               $entity['lookup_entity']        = 
unserialize($this->db->f('lookup_entity'));
+                               $entity['documentation']        = 
$this->db->f('documentation');
+                       }
+
+                       $sql = "SELECT location FROM fm_entity_lookup where 
entity_id=$id AND type='lookup'";
+
+                       $this->db->query($sql,__LINE__,__FILE__);
+
+                       while ($this->db->next_record())
+                       {
+                               $entity['include_entity_for'][] = 
$this->db->f('location');
+                       }
+
+                       $sql = "SELECT location FROM fm_entity_lookup where 
entity_id=$id AND type='start'";
+
+                       $this->db->query($sql,__LINE__,__FILE__);
+
+                       while ($this->db->next_record())
+                       {
+                               $entity['start_entity_from'][] = 
$this->db->f('location');
+                       }
+
+                       return $entity;
+               }
+
+               function read_single_category($entity_id,$cat_id)
+               {
+                       $sql = "SELECT * FROM fm_entity_category where 
entity_id=$entity_id AND id=$cat_id";
+
+                       $this->db->query($sql,__LINE__,__FILE__);
+
+                       if ($this->db->next_record())
+                       {
+                               $category['id']                 = 
$this->db->f('id');
+                               $category['name']               = 
$this->db->f('name');
+                               $category['descr']              = 
$this->db->f('descr');
+                               $category['prefix']             = 
$this->db->f('prefix');
+                               $category['lookup_tenant']      = 
$this->db->f('lookup_tenant');
+                               $category['tracking']   = 
$this->db->f('tracking');
+                               $category['location_level']     = 
$this->db->f('location_level');
+                               $category['fileupload'] = 
$this->db->f('fileupload');
+                               $category['loc_link']   = 
$this->db->f('loc_link');
+                               $category['start_project']      = 
$this->db->f('start_project');
+                               return $category;
+                       }
+               }
+
+               function read_category_name($entity_id,$cat_id)
+               {
+                       $sql = "SELECT * FROM fm_entity_category where 
entity_id=$entity_id AND id=$cat_id";
+                       $this->db->query($sql,__LINE__,__FILE__);
+                       $this->db->next_record();
+                       return $this->db->f('name');
+               }
+
+               function read_single_status($entity_id,$cat_id,$id)
+               {
+
+                       $table  = 'fm_entity_'. $entity_id .'_'.$cat_id . 
'_status';
+                       $sql = "SELECT * FROM $table  where id='$id'";
+
+                       $this->db->query($sql,__LINE__,__FILE__);
+
+                       if ($this->db->next_record())
+                       {
+                               $status['id']                           = 
$this->db->f('id');
+                               $status['descr']                        = 
$this->db->f('descr');
+
+                               return $status;
+                       }
+               }
+
+               function add_entity($entity)
+               {
+                       $entity['name'] = 
$this->db->db_addslashes($entity['name']);
+                       $entity['descr'] = 
$this->db->db_addslashes($entity['descr']);
+
+                       $entity['id'] = $this->bocommon->next_id('fm_entity');
+
+                       $values= array(
+                               $entity['id'],
+                               $entity['name'],
+                               $entity['descr'],
+                               $entity['location_form'],
+                               $entity['documentation']
+                               );
+
+                       $values = $this->bocommon->validate_db_insert($values);
+
+                       $this->db->query("INSERT INTO fm_entity (id,name, 
descr,location_form,documentation) "
+                               . "VALUES ($values)",__LINE__,__FILE__);
+
+
+                       $values_acl_location= array(
+                               '.entity.' . $entity['id'],
+                               $entity['name'],
+                               1
+                               );
+
+                       $values_acl_location    = 
$this->bocommon->validate_db_insert($values_acl_location);
+
+                       $this->db->query("INSERT INTO fm_acl_location 
(id,descr,allow_grant) "
+                               . "VALUES 
($values_acl_location)",__LINE__,__FILE__);
+
+                       $receipt['id']= $entity['id'];
+
+                       $receipt['message'][] = array('msg'=> lang('entity has 
been added'));
+                       return $receipt;
+               }
+
+
+               function add_status($values,$entity_id,$cat_id)
+               {
+                       $values['id'] = $this->db->db_addslashes($values['id']);
+                       $values['descr'] = 
$this->db->db_addslashes($values['descr']);
+
+                       $values_insert= array(
+                               $values['id'],
+                               $values['descr'],
+                               );
+
+                       $values_insert  = 
$this->bocommon->validate_db_insert($values_insert);
+
+                       $table  = 'fm_entity_'. $entity_id .'_'.$cat_id . 
'_status';
+
+                       $this->db->query("INSERT INTO $table (id,descr) VALUES 
($values_insert)",__LINE__,__FILE__);
+
+                       $receipt['id']= $values['id'];
+
+                       $receipt['message'][] = array('msg'=> lang('status has 
been added'));
+                       return $receipt;
+               }
+
+
+               function add_category($values)
+               {
+                       $values['name'] = 
$this->db->db_addslashes($values['name']);
+                       $values['descr'] = 
$this->db->db_addslashes($values['descr']);
+
+                       $values['id'] = 
$this->bocommon->next_id('fm_entity_category',array('entity_id'=>$values['entity_id']));
+
+                       $location_type = 
$this->bocommon->next_id('fm_location_type');
+
+                       $values_insert= array(
+                               $values['entity_id'],
+                               $values['id'],
+                               $values['name'],
+                               $values['descr'],
+                               $values['prefix'],
+                               $values['lookup_tenant'],
+                               $values['tracking'],
+                               $values['location_level'],
+                               $values['fileupload'],
+                               $values['loc_link'],
+                               $values['start_project']
+                               );
+
+                       $values_insert  = 
$this->bocommon->validate_db_insert($values_insert);
+
+
+                       $this->db->transaction_begin();
+
+                       $this->db->query("INSERT INTO fm_entity_category 
(entity_id,id,name, 
descr,prefix,lookup_tenant,tracking,location_level,fileupload,loc_link,start_project)
 "
+                               . "VALUES ($values_insert)",__LINE__,__FILE__);
+
+                       $values_acl_location= array(
+                               '.entity.' . $values['entity_id'] . '.' . 
$values['id'],
+                               $values['name'],
+                               1
+                               );
+
+                       $values_acl_location    = 
$this->bocommon->validate_db_insert($values_acl_location);
+
+                       $this->db->query("INSERT INTO fm_acl_location 
(id,descr,allow_grant) "
+                               . "VALUES 
($values_acl_location)",__LINE__,__FILE__);
+
+                       $receipt['id']= $values['id'];
+
+                       $this->init_process();
+
+                       $fd=array();
+                       $fd['id'] = array('type' => 'int', 'precision' => 4, 
'nullable' => False);
+                       $fd['num'] = array('type' => 'varchar', 'precision' => 
16, 'nullable' => False);
+                       $fd['p_num'] = array('type' => 'varchar', 'precision' 
=> 16, 'nullable' => True);
+                       $fd['p_entity_id'] = array('type' => 'int', 'precision' 
=> 4, 'nullable' => True);
+                       $fd['p_cat_id'] = array('type' => 'int', 'precision' => 
4, 'nullable' => True);
+                       $fd['location_code'] = array('type' => 'varchar', 
'precision' => 25, 'nullable' => True);
+
+                       for ($i=1; $i<$location_type; $i++)
+                       {
+                               $fd['loc' . $i] = array('type' => 'varchar', 
'precision' => 4, 'nullable' => True);
+                       }
+
+                       $fd['address'] = array('type' => 'varchar', 'precision' 
=> 150, 'nullable' => True);
+                       $fd['tenant_id'] = array('type' => 'int', 'precision' 
=> 4, 'nullable' => True);
+                       $fd['contact_phone'] = array('type' => 'varchar', 
'precision' => 30, 'nullable' => True);
+                       $fd['status'] = array('type' => 'int', 'precision' => 
4, 'nullable' => True);
+                       $fd['entry_date'] = array('type' => 'int', 'precision' 
=> 4, 'nullable' => True);
+                       $fd['user_id'] = array('type' => 'int', 'precision' => 
4, 'nullable' => True);
+
+                       $pk[]= 'id';
+                       $table                  = 'fm_entity_'. 
$values['entity_id'] .'_'.$values['id'];
+/*
+                       $fd_status['id'] = array('type' => 'varchar', 
'precision' => 20, 'nullable' => False);
+                       $fd_status['descr'] = array('type' => 'varchar', 
'precision' => 255, 'nullable' => False);
+                       $pk_status[]= 'id';
+
+                       $statustable    = $table . '_' .'status';
+*/
+                       if(($this->oProc->CreateTable($table,array('fd' => 
$fd,'pk' => $pk,'fk' => $fk,'ix' => array('location_code'),'uc' => array()))))
+//                             && 
($this->oProc->CreateTable($statustable,array('fd' => $fd_status,'pk' => 
$pk_status,'fk' => $fk_status,'ix' => False,'uc' => array()))))
+                       {
+
+                               $values_insert= array(
+                                       $values['entity_id'],
+                                       $values['id'],
+                                       1,
+                                       'status',
+                                       'Status',
+                                       'Status',
+                                       'LB',
+                                       1,
+                                       'True'
+                                       );
+
+                               $values_insert  = 
$this->bocommon->validate_db_insert($values_insert);
+
+                               $this->db->query("INSERT INTO 
fm_entity_attribute 
(entity_id,cat_id,id,column_name,input_text,statustext,datatype,attrib_sort,nullable)
 "
+                                       . "VALUES 
($values_insert)",__LINE__,__FILE__);
+
+                               $receipt['message'][] = array('msg'     => 
lang('table %1 has been saved',$table));
+                               $this->db->transaction_commit();
+                       }
+                       else
+                       {
+                               $receipt['error'][] = array('msg'       => 
lang('table could not be added')     );
+                               if($this->db->Transaction)
+                               {
+                                       $this->db->transaction_abort();
+                               }
+                               else
+                               {
+                                       $this->db->query("DELETE FROM 
fm_entity_category WHERE id=" . $values['id'] . " AND entity_id=" . 
$values['entity_id'],__LINE__,__FILE__);
+                                       unset($receipt['id']);
+
+                               }
+                       }
+
+                       return $receipt;
+               }
+
+               function edit_status($values,$entity_id,$cat_id)
+               {
+                       $table  = 'fm_entity_'. $entity_id .'_'.$cat_id . 
'_status';
+
+                       $values['descr'] = 
$this->db->db_addslashes($values['descr']);
+
+                       $value_set=array(
+                               'descr'                 => $values['descr'],
+                               );
+
+                       $value_set      = 
$this->bocommon->validate_db_update($value_set);
+
+                       $this->db->query("UPDATE $table set $value_set WHERE 
id='" . $values['id'] . "'",__LINE__,__FILE__);
+
+                       $receipt['message'][] = array('msg'=> lang('Status has 
been edited'));
+
+                       return $receipt;
+               }
+
+               function edit_entity($entity)
+               {
+                       if (!$entity['name'])
+                       {
+                               $receipt['error'][] = array('msg'=>lang('Name 
not entered!'));
+                       }
+
+                       if (!$receipt['error'])
+                       {
+                               $table = 'fm_entity';
+
+                               $entity['name'] = 
$this->db->db_addslashes($entity['name']);
+                               $entity['descr'] = 
$this->db->db_addslashes($entity['descr']);
+
+                               if(!$entity['location_form'])
+                               {
+                                       unset($entity['lookup_entity']);
+                               }
+
+                               $value_set=array(
+                               'descr'                 => $entity['descr'],
+                               'name'                  => $entity['name'],
+                               'location_form' => $entity['location_form'],
+                               'lookup_entity' => 
serialize($entity['lookup_entity']),
+                               'documentation' => $entity['documentation']
+                               );
+
+                               $value_set      = 
$this->bocommon->validate_db_update($value_set);
+
+                               $this->db->transaction_begin();
+
+                               $this->db->query("UPDATE $table set $value_set 
WHERE id=" . $entity['id'],__LINE__,__FILE__);
+
+                               $value_set_acl=array(
+                                       'descr'                 => 
$entity['name']
+                                       );
+
+                               $value_set_acl  = 
$this->bocommon->validate_db_update($value_set_acl);
+
+                               $this->db->query("UPDATE fm_acl_location set 
$value_set_acl WHERE id='.entity." . $entity['id']. "'",__LINE__,__FILE__);
+
+                               $this->db->query("DELETE FROM fm_entity_lookup 
WHERE type='lookup' AND entity_id=" . $entity['id'],__LINE__,__FILE__);
+                               if (isset($entity['include_entity_for']) AND 
is_array($entity['include_entity_for']))
+                               {
+                                       foreach($entity['include_entity_for'] 
as $location)
+                                       {
+                                               $this->db->query("INSERT INTO 
fm_entity_lookup (entity_id,location,type)"
+                                               . "VALUES (" .$entity['id'] . 
",'$location','lookup' )",__LINE__,__FILE__);
+                                       }
+                               }
+
+                               $this->db->query("DELETE FROM fm_entity_lookup 
WHERE type='start' AND entity_id=" . $entity['id'],__LINE__,__FILE__);
+
+                               if (isset($entity['start_entity_from']) AND 
is_array($entity['start_entity_from']))
+                               {
+                                       foreach($entity['start_entity_from'] as 
$location)
+                                       {
+                                               $this->db->query("INSERT INTO 
fm_entity_lookup (entity_id,location,type)"
+                                               . "VALUES (" .$entity['id'] . 
",'$location','start' )",__LINE__,__FILE__);
+                                       }
+                               }
+
+                               $this->db->transaction_commit();
+
+                               $receipt['message'][] = array('msg'=> 
lang('entity has been edited'));
+                       }
+                       else
+                       {
+                               $receipt['error'][] = array('msg'       => 
lang('entity has NOT been edited'));
+                       }
+
+                       return $receipt;
+               }
+
+               function edit_category($entity)
+               {
+
+                       if (!$entity['name'])
+                       {
+                               $receipt['error'][] = array('msg'=>lang('Name 
not entered!'));
+                       }
+
+                       if (!$receipt['error'])
+                       {
+                               $table = 'fm_entity_category';
+
+                               $entity['name'] = 
$this->db->db_addslashes($entity['name']);
+                               $entity['descr'] = 
$this->db->db_addslashes($entity['descr']);
+
+                               $value_set=array(
+                                       'descr'                 => 
$entity['descr'],
+                                       'name'                  => 
$entity['name'],
+                                       'prefix'                => 
$entity['prefix'],
+                                       'lookup_tenant' => 
$entity['lookup_tenant'],
+                                       'tracking'              => 
$entity['tracking'],
+                                       'location_level'=> 
$entity['location_level'],
+                                       'fileupload'    => 
$entity['fileupload'],
+                                       'loc_link'              => 
$entity['loc_link'],
+                                       'start_project' => 
$entity['start_project']
+                                       );
+
+                               $value_set      = 
$this->bocommon->validate_db_update($value_set);
+
+                               $this->db->query("UPDATE $table set $value_set 
WHERE entity_id=" . $entity['entity_id']. " AND id=" . 
$entity['id'],__LINE__,__FILE__);
+
+                               $value_set_acl=array(
+                                       'descr'                 => 
$entity['name']
+                                       );
+
+                               $value_set_acl  = 
$this->bocommon->validate_db_update($value_set_acl);
+
+                               $this->db->query("UPDATE fm_acl_location set 
$value_set_acl WHERE id='.entity." . $entity['entity_id']. "." . $entity['id']. 
"'",__LINE__,__FILE__);
+
+
+                               $receipt['message'][] = array('msg'=> 
lang('entity has been edited'));
+                       }
+                       else
+                       {
+                               $receipt['error'][] = array('msg'       => 
lang('entity has NOT been edited'));
+                       }
+
+                       return $receipt;
+               }
+
+               function delete_entity($id)
+               {
+                       
$category_list=$this->read_category(array('entity_id'=>$id));
+                       $this->db->query("DELETE FROM fm_entity WHERE 
id=$id",__LINE__,__FILE__);
+                       $this->db->query("DELETE FROM fm_entity_category WHERE 
entity_id=$id",__LINE__,__FILE__);
+                       $this->db->query("DELETE FROM fm_entity_attribute WHERE 
entity_id=$id",__LINE__,__FILE__);
+                       $this->db->query("DELETE FROM fm_acl_location WHERE id 
$this->like '.entity." . $id ."%'",__LINE__,__FILE__);
+                       $this->db->query("DELETE FROM fm_acl2 WHERE 
acl_location $this->like '.entity." . $id ."%'",__LINE__,__FILE__);
+                       if (isset($category_list) AND is_array($category_list))
+                       {
+                               $this->init_process();
+
+                               foreach($category_list as $entry)
+                               {
+                                       $this->oProc->DropTable('fm_entity_' . 
$id . '_' . $entry['id']);
+                               }
+                       }
+
+               }
+
+               function delete_category($id,$entity_id)
+               {
+                       $this->init_process();
+                       $this->oProc->DropTable('fm_entity_' . $entity_id . '_' 
. $id);
+//                     $this->oProc->DropTable('fm_entity_' . $entity_id . '_' 
. $id . '_' . 'status');
+                       $this->db->query("DELETE FROM fm_entity_category WHERE 
entity_id= $entity_id AND id= $id",__LINE__,__FILE__);
+                       $this->db->query("DELETE FROM fm_entity_attribute WHERE 
entity_id= $entity_id AND cat_id= $id",__LINE__,__FILE__);
+                       $this->db->query("DELETE FROM fm_acl_location WHERE 
id='.entity." . $entity_id . "." . $id ."'",__LINE__,__FILE__);
+                       $this->db->query("DELETE FROM fm_acl2 WHERE 
acl_location='.entity." . $entity_id . "." . $id ."'",__LINE__,__FILE__);
+               }
+
+               function delete_attrib($cat_id,$entity_id,$attrib_id)
+               {
+                       $this->init_process();
+
+                       $sql = "SELECT * FROM fm_entity_attribute WHERE 
entity_id=$entity_id AND cat_id=$cat_id AND id=$attrib_id";
+
+                       $this->db->query($sql,__LINE__,__FILE__);
+                       $this->db->next_record();
+                       $ColumnName             = $this->db->f('column_name');
+
+                       $this->oProc->DropColumn('fm_entity_' .$entity_id.'_'. 
$cat_id,'', $ColumnName);
+
+                       $sql = "SELECT attrib_sort FROM fm_entity_attribute 
where entity_id=$entity_id AND cat_id=$cat_id AND id=$attrib_id";
+                       $this->db->query($sql,__LINE__,__FILE__);
+                       $this->db->next_record();
+                       $attrib_sort    = $this->db->f('attrib_sort');
+                       $sql2 = "SELECT max(attrib_sort) as max_sort FROM 
fm_entity_attribute where entity_id=$entity_id AND cat_id=$cat_id";
+                       $this->db->query($sql2,__LINE__,__FILE__);
+                       $this->db->next_record();
+                       $max_sort       = $this->db->f('max_sort');
+                       if($max_sort>$attrib_sort)
+                       {
+                               $sql = "UPDATE fm_entity_attribute set 
attrib_sort=attrib_sort-1 WHERE entity_id=$entity_id AND cat_id=$cat_id AND 
attrib_sort > $attrib_sort";
+                               $this->db->query($sql,__LINE__,__FILE__);
+                       }
+
+                       $this->db->query("DELETE FROM fm_entity_attribute WHERE 
entity_id=$entity_id AND cat_id=$cat_id AND id=$attrib_id",__LINE__,__FILE__);
+               }
+
+               function delete_status($cat_id,$entity_id,$status_id)
+               {
+                       $table  = 'fm_entity_'. $entity_id .'_'.$cat_id . 
'_status';
+
+                       $this->db->query("DELETE FROM $table WHERE 
id='$status_id'",__LINE__,__FILE__);
+               }
+
+               function read_attrib($data)
+               {
+
+//_debug_array($data);
+                       if(is_array($data))
+                       {
+                               if ($data['start'])
+                               {
+                                       $start=$data['start'];
+                               }
+                               else
+                               {
+                                       $start=0;
+                               }
+                               $query = 
(isset($data['query'])?$data['query']:'');
+                               $sort = 
(isset($data['sort'])?$data['sort']:'DESC');
+                               $order = 
(isset($data['order'])?$data['order']:'');
+                               $allrows = 
(isset($data['allrows'])?$data['allrows']:'');
+                               $entity_id = 
(isset($data['entity_id'])?$data['entity_id']:0);
+                               $cat_id = 
(isset($data['cat_id'])?$data['cat_id']:0);
+                       }
+
+                       if ($order)
+                       {
+                               $ordermethod = " order by $order $sort";
+
+                       }
+                       else
+                       {
+                               $ordermethod = ' order by attrib_sort asc';
+                       }
+
+                       if($query)
+                       {
+                               $query = ereg_replace("'",'',$query);
+                               $query = ereg_replace('"','',$query);
+
+                               $querymethod = " AND (fm_entity_attribute.name 
$this->like '%$query%' or fm_entity_attribute.descr $this->like '%$query%')";
+                       }
+
+                       $sql = "SELECT * FROM fm_entity_attribute WHERE 
entity_id=$entity_id AND cat_id = $cat_id $filtermethod $querymethod";
+
+                       $this->db2->query($sql,__LINE__,__FILE__);
+                       $this->total_records = $this->db2->num_rows();
+                       if(!$allrows)
+                       {
+                               $this->db->limit_query($sql . 
$ordermethod,$start,__LINE__,__FILE__);
+                       }
+                       else
+                       {
+                               $this->db->query($sql . 
$ordermethod,__LINE__,__FILE__);
+                       }
+
+
+                       while ($this->db->next_record())
+                       {
+                               $attrib[] = array
+                               (
+                                       'id'                    => 
$this->db->f('id'),
+                                       'entity_type'   => 
$this->db->f('type_id'),
+                                       'attrib_sort'   => 
$this->db->f('attrib_sort'),
+                                       'list'                  => 
$this->db->f('list'),
+                                       'lookup_form'   => 
$this->db->f('lookup_form'),
+                                       'entity_form'   => 
$this->db->f('entity_form'),
+                                       'column_name'   => 
$this->db->f('column_name'),
+                                       'name'                  => 
$this->db->f('input_text'),
+                                       'size'                  => 
$this->db->f('size'),
+                                       'statustext'    => 
$this->db->f('statustext'),
+                                       'input_text'    => 
$this->db->f('input_text'),
+                                       'type_name'             => 
$this->db->f('type'),
+                                       'datatype'              => 
$this->db->f('datatype'),
+                                       'search'                => 
$this->db->f('search')
+                       );
+                       }
+                       return $attrib;
+               }
+
+               function read_single_attrib($entity_id,$cat_id,$id)
+               {
+
+                       $sql = "SELECT * FROM fm_entity_attribute where 
entity_id=$entity_id AND cat_id=$cat_id AND id=$id";
+
+                       $this->db->query($sql,__LINE__,__FILE__);
+
+                       if ($this->db->next_record())
+                       {
+                               $attrib['id']                                   
        = $this->db->f('id');
+                               $attrib['column_name']                          
= $this->db->f('column_name');
+                               $attrib['input_text']                           
= $this->db->f('input_text');
+                               $attrib['statustext']                           
= $this->db->f('statustext');
+                               $attrib['column_info']['precision']     = 
$this->db->f('precision_');
+                               $attrib['column_info']['scale']         = 
$this->db->f('scale');
+                               $attrib['column_info']['default']       = 
$this->db->f('default_value');
+                               $attrib['column_info']['nullable']      = 
$this->db->f('nullable');
+                               $attrib['column_info']['type']          = 
$this->db->f('datatype');
+                               $attrib['type_id']                              
        = $this->db->f('type_id');
+                               $attrib['type_name']                            
= $this->db->f('type_name');
+                               $attrib['lookup_form']                          
= $this->db->f('lookup_form');
+                               $attrib['list']                                 
        = $this->db->f('list');
+                               $attrib['search']                               
        = $this->db->f('search');
+                               if($this->db->f('datatype')=='R' || 
$this->db->f('datatype')=='CH' || $this->db->f('datatype')=='LB')
+                               {
+                                       $attrib['choice'] = 
$this->read_attrib_choice($entity_id,$cat_id,$id);
+                               }
+
+                               return $attrib;
+                       }
+               }
+
+               function read_attrib_choice($entity_id,$cat_id,$attrib_id)
+               {
+                       $choice_table = 'fm_entity_choice';
+                       $sql = "SELECT * FROM $choice_table WHERE 
entity_id=$entity_id AND cat_id=$cat_id AND attrib_id=$attrib_id";
+                       $this->db->query($sql,__LINE__,__FILE__);
+
+                       while ($this->db->next_record())
+                       {
+                               $choice[] = array
+                               (
+                                       'id'    => $this->db->f('id'),
+                                       'value' => $this->db->f('value')
+                               );
+                       }
+                       return $choice;
+               }
+
+               function add_attrib($attrib)
+               {
+
+                       $attrib['column_name'] = 
$this->db->db_addslashes($attrib['column_name']);
+                       $attrib['input_text'] = 
$this->db->db_addslashes($attrib['input_text']);
+                       $attrib['statustext'] = 
$this->db->db_addslashes($attrib['statustext']);
+                       $attrib['default'] = 
$this->db->db_addslashes($attrib['default']);
+                       $attrib['id'] = 
$this->bocommon->next_id('fm_entity_attribute',array('entity_id'=>$attrib['entity_id'],'cat_id'=>$attrib['cat_id']));
+
+                       $sql = "SELECT max(attrib_sort) as max_sort FROM 
fm_entity_attribute where entity_id=" . $attrib['entity_id'] . " AND cat_id=" . 
$attrib['cat_id'];
+                       $this->db->query($sql,__LINE__,__FILE__);
+                       $this->db->next_record();
+                       $attrib_sort    = $this->db->f('max_sort')+1;
+
+                       $values= array(
+                               $attrib['entity_id'],
+                               $attrib['cat_id'],
+                               $attrib['id'],
+                               $attrib['column_name'],
+                               $attrib['input_text'],
+                               $attrib['statustext'],
+                               $attrib['search'],
+                               $attrib['list'],
+                               $attrib_sort,
+                               $attrib['column_info']['type'],
+                               $attrib['column_info']['precision'],
+                               $attrib['column_info']['scale'],
+                               $attrib['column_info']['default'],
+                               $attrib['column_info']['nullable']
+                               );
+
+                       $values = $this->bocommon->validate_db_insert($values);
+
+                       $this->db->transaction_begin();
+
+                       $this->db->query("INSERT INTO fm_entity_attribute 
(entity_id,cat_id,id,column_name, input_text, 
statustext,search,list,attrib_sort, 
datatype,precision_,scale,default_value,nullable) "
+                               . "VALUES ($values)",__LINE__,__FILE__);
+
+                       $receipt['id']= $attrib['id'];
+
+                       if($attrib['column_info']['type']=='email' && 
!$attrib['column_info']['precision'])
+                       {
+                               $attrib['column_info']['precision']=64;
+                       }
+
+                       $attrib['column_info']['type']  = 
$this->bocommon->translate_datatype_insert($attrib['column_info']['type']);
+
+                       if($attrib['column_info']['type']=='int' && 
!$attrib['column_info']['precision'])
+                       {
+                               $attrib['column_info']['precision']=4;
+                       }
+
+                       if(!$attrib['column_info']['default'])
+                       {
+                               unset($attrib['column_info']['default']);
+                       }
+
+                       $this->init_process();
+
+                       
if($this->oProc->AddColumn('fm_entity_'.$attrib['entity_id'] . '_' . 
$attrib['cat_id'],$attrib['column_name'], $attrib['column_info']))
+                       {
+                               $receipt['message'][] = array('msg'     => 
lang('Attribute has been saved')     );
+                               $this->db->transaction_commit();
+
+                       }
+                       else
+                       {
+                               $receipt['error'][] = array('msg'       => 
lang('column could not be added')    );
+                               if($this->db->Transaction)
+                               {
+                                       $this->db->transaction_abort();
+                               }
+                               else
+                               {
+                                       $this->db->query("DELETE FROM 
fm_entity_attribute WHERE entity_id=" . $attrib['entity_id']. " AND cat_id=" . 
$attrib['id']. " AND id='" . $receipt['id'] . "'",__LINE__,__FILE__);
+                                       unset($receipt['id']);
+
+                               }
+                       }
+
+                       return $receipt;
+               }
+
+               function init_process()
+               {
+                       $this->oProc                                            
= 
CreateObject('phpgwapi.schema_proc',$GLOBALS['phpgw_info']['server']['db_type']);
+                       $this->oProc->m_odb                                     
= $this->db;
+                       $this->oProc->m_odb->Halt_On_Error      = 'report';
+               }
+
+               function edit_attrib($attrib)
+               {
+
+                       $choice_table = 'fm_entity_choice';
+
+                       $attrib['column_name'] = 
$this->db->db_addslashes($attrib['column_name']);
+                       $attrib['input_text'] = 
$this->db->db_addslashes($attrib['input_text']);
+                       $attrib['statustext'] = 
$this->db->db_addslashes($attrib['statustext']);
+                       $attrib['default'] = 
$this->db->db_addslashes($attrib['default']);
+
+                       $this->db->query("SELECT column_name FROM 
fm_entity_attribute WHERE entity_id=" . $attrib['entity_id']. " AND cat_id=" . 
$attrib['cat_id']. " AND id='" . $attrib['id']. "'",__LINE__,__FILE__);
+                       $this->db->next_record();
+                       $OldColumnName          = $this->db->f('column_name');
+
+                       $this->db->transaction_begin();
+
+                               $value_set=array(
+                                       'input_text'    => 
$attrib['input_text'],
+                                       'statustext'    => 
$attrib['statustext'],
+                                       'search'                => 
$attrib['search'],
+                                       'list'                  => 
$attrib['list'],
+                                       );
+
+                               $value_set      = 
$this->bocommon->validate_db_update($value_set);
+
+                               $this->db->query("UPDATE fm_entity_attribute 
set $value_set WHERE entity_id=" . $attrib['entity_id']. " AND cat_id=" . 
$attrib['cat_id']. " AND id=" . $attrib['id'],__LINE__,__FILE__);
+
+                       if($OldColumnName !=$attrib['column_name'])
+                       {
+
+                               if($attrib['column_info']['type']!='R' && 
$attrib['column_info']['type']!='CH' && $attrib['column_info']['type']!='LB')
+                               {
+                                       $this->db->query("DELETE FROM 
$choice_table WHERE entity_id=" . $attrib['entity_id']. " AND cat_id=" . 
$attrib['cat_id']. " AND attrib_id=" . $attrib['id'],__LINE__,__FILE__);
+                               }
+
+                               if($attrib['column_info']['type']=='email' && 
!$attrib['column_info']['precision'])
+                               {
+                                       $attrib['column_info']['precision']=64;
+                               }
+
+                               if($precision = 
$this->bocommon->translate_datatype_precision($attrib['column_info']['type']))
+                               {
+                                       
$attrib['column_info']['precision']=$precision;
+                               }
+
+                               if(!$attrib['column_info']['default'])
+                               {
+                                       
unset($attrib['column_info']['default']);
+                               }
+
+                               $value_set=array(
+                                       'column_name'   => 
$attrib['column_name'],
+                                       'datatype'              => 
$attrib['column_info']['type'],
+                                       'precision_'    => 
$attrib['column_info']['precision'],
+                                       'scale'                 => 
$attrib['column_info']['scale'],
+                                       'default_value' => 
$attrib['column_info']['default'],
+                                       'nullable'              => 
$attrib['column_info']['nullable']
+                                       );
+
+                               $value_set      = 
$this->bocommon->validate_db_update($value_set);
+
+                               $this->db->query("UPDATE fm_entity_attribute 
set $value_set WHERE entity_id=" . $attrib['entity_id']. " AND cat_id=" . 
$attrib['cat_id']. " AND id=" . $attrib['id'],__LINE__,__FILE__);
+
+                               $attrib['column_info']['type']  = 
$this->bocommon->translate_datatype_insert($attrib['column_info']['type']);
+
+                               $this->init_process();
+
+                               $mini_table_def = array(
+                                       'fm_entity_'.$attrib['entity_id'] . '_' 
. $attrib['cat_id'] =>  array(
+                                               'fd' => array(
+                                                       $OldColumnName  
=>$attrib['column_info']),
+                                               'pk' => array(),
+                                               'fk' => array(),
+                                               'ix' => array(),
+                                               'uc' => array()
+                                               )
+                                       );
+
+                               $this->oProc->GenerateScripts($mini_table_def);
+                               
$this->oProc->RenameColumn('fm_entity_'.$attrib['entity_id'] . '_' . 
$attrib['cat_id'], $OldColumnName, $attrib['column_name']);
+                               
$this->oProc->AlterColumn('fm_entity_'.$attrib['entity_id'] . '_' . 
$attrib['cat_id'],$attrib['column_name'],$attrib['column_info']);
+                       }
+
+                       $choice_table ='fm_entity_choice';
+
+                       if($attrib['new_choice'])
+                       {
+                               $choice_id = 
$this->bocommon->next_id($choice_table 
,array('entity_id'=>$attrib['entity_id'],'cat_id'=>$attrib['cat_id'],'attrib_id'=>$attrib['id']));
+
+                               $values= array(
+                                       $attrib['entity_id'],
+                                       $attrib['cat_id'],
+                                       $attrib['id'],
+                                       $choice_id,
+                                       $attrib['new_choice']
+                                       );
+
+                               $values = 
$this->bocommon->validate_db_insert($values);
+
+                               $this->db->query("INSERT INTO $choice_table 
(entity_id,cat_id,attrib_id,id,value) "
+                               . "VALUES ($values)",__LINE__,__FILE__);
+                       }
+
+                       if($attrib['delete_choice'])
+                       {
+                               for 
($i=0;$i<count($attrib['delete_choice']);$i++)
+                               {
+                                       $this->db->query("DELETE FROM 
$choice_table WHERE entity_id=" . $attrib['entity_id']. " AND cat_id=" . 
$attrib['cat_id']. " AND attrib_id=" . $attrib['id']  ." AND id=" . 
$attrib['delete_choice'][$i],__LINE__,__FILE__);
+                               }
+                       }
+
+                       $this->db->transaction_commit();
+
+                       $receipt['message'][] = array('msg'     => 
lang('Attribute has been edited'));
+
+                       return $receipt;
+               }
+
+               function resort_attrib($data)
+               {
+                       if(is_array($data))
+                       {
+                               $resort = 
(isset($data['resort'])?$data['resort']:'up');
+                               $entity_id = 
(isset($data['entity_id'])?$data['entity_id']:0);
+                               $cat_id = 
(isset($data['cat_id'])?$data['cat_id']:0);
+                               $id = (isset($data['id'])?$data['id']:'');
+                       }
+
+                       $sql = "SELECT attrib_sort FROM fm_entity_attribute 
where entity_id=$entity_id AND cat_id=$cat_id AND id=$id";
+                       $this->db->query($sql,__LINE__,__FILE__);
+                       $this->db->next_record();
+                       $attrib_sort    = $this->db->f('attrib_sort');
+                       $sql2 = "SELECT max(attrib_sort) as max_sort FROM 
fm_entity_attribute where entity_id=$entity_id AND cat_id=$cat_id";
+                       $this->db->query($sql2,__LINE__,__FILE__);
+                       $this->db->next_record();
+                       $max_sort       = $this->db->f('max_sort');
+
+                       switch($resort)
+                       {
+                               case 'up':
+                                       if($attrib_sort>1)
+                                       {
+                                               $sql = "UPDATE 
fm_entity_attribute set attrib_sort=$attrib_sort WHERE entity_id=$entity_id AND 
cat_id=$cat_id AND attrib_sort =" . ($attrib_sort-1);
+                                               
$this->db->query($sql,__LINE__,__FILE__);
+                                               $sql = "UPDATE 
fm_entity_attribute set attrib_sort=" . ($attrib_sort-1) ." WHERE 
entity_id=$entity_id AND cat_id=$cat_id AND id=$id";
+                                               
$this->db->query($sql,__LINE__,__FILE__);
+                                       }
+                                       break;
+                               case 'down':
+                                       if($max_sort > $attrib_sort)
+                                       {
+                                               $sql = "UPDATE 
fm_entity_attribute set attrib_sort=$attrib_sort WHERE entity_id=$entity_id AND 
cat_id=$cat_id AND attrib_sort =" . ($attrib_sort+1);
+                                               
$this->db->query($sql,__LINE__,__FILE__);
+                                               $sql = "UPDATE 
fm_entity_attribute set attrib_sort=" . ($attrib_sort+1) ." WHERE 
entity_id=$entity_id AND cat_id=$cat_id AND id=$id";
+                                               
$this->db->query($sql,__LINE__,__FILE__);
+                                       }
+                                       break;
+                               default:
+                                       return;
+                                       break;
+                       }
+               }
+
+               function read_custom_function($data)
+               {
+                       if(is_array($data))
+                       {
+                               if ($data['start'])
+                               {
+                                       $start=$data['start'];
+                               }
+                               else
+                               {
+                                       $start=0;
+                               }
+                               $query = 
(isset($data['query'])?$data['query']:'');
+                               $sort = 
(isset($data['sort'])?$data['sort']:'DESC');
+                               $order = 
(isset($data['order'])?$data['order']:'');
+                               $allrows = 
(isset($data['allrows'])?$data['allrows']:'');
+                               $acl_location = 
(isset($data['acl_location'])?$data['acl_location']:'');
+                       }
+
+                       if(!$acl_location)
+                       {
+                               return;
+                       }
+
+                       if ($order)
+                       {
+                               $ordermethod = " order by $order $sort";
+
+                       }
+                       else
+                       {
+                               $ordermethod = ' order by custom_sort asc';
+                       }
+
+                       $table = 'fm_custom_function';
+
+                       if($query)
+                       {
+                               $query = ereg_replace("'",'',$query);
+                               $query = ereg_replace('"','',$query);
+
+                               $querymethod = " AND name $this->like 
'%$query%' or descr $this->like '%$query%'";
+                       }
+
+                       $sql = "SELECT * FROM $table WHERE 
acl_location='$acl_location' $querymethod";
+
+                       $this->db2->query($sql,__LINE__,__FILE__);
+                       $this->total_records = $this->db2->num_rows();
+
+                       if(!$allrows)
+                       {
+                               $this->db->limit_query($sql . 
$ordermethod,$start,__LINE__,__FILE__);
+                       }
+                       else
+                       {
+                               $this->db->query($sql . 
$ordermethod,__LINE__,__FILE__);
+                       }
+
+                       while ($this->db->next_record())
+                       {
+                               $custom_function[] = array
+                               (
+                                       'id'    => $this->db->f('id'),
+                                       'file_name'     => 
$this->db->f('file_name'),
+                                       'sorting'       => 
$this->db->f('custom_sort'),
+                                       'descr'         => 
$this->db->f('descr'),
+                                       'active'        => 
$this->db->f('active')
+                               );
+                       }
+                       return $custom_function;
+               }
+
+
+               function read_single_custom_function($acl_location,$id)
+               {
+
+                       $sql = "SELECT * FROM fm_custom_function where 
acl_location='$acl_location' AND id=$id";
+
+                       $this->db->query($sql,__LINE__,__FILE__);
+
+                       if ($this->db->next_record())
+                       {
+                               $custom_function['id']                  = 
$this->db->f('id');
+                               $custom_function['descr']               = 
$this->db->f('descr');
+                               $custom_function['custom_function_file']= 
$this->db->f('file_name');
+                               $custom_function['active']              = 
$this->db->f('active');
+
+                               return $custom_function;
+                       }
+
+               }
+
+               function add_custom_function($custom_function)
+               {
+                       if(!$custom_function['acl_location'] && 
$custom_function['entity_id'] && $custom_function['cat_id'])
+                       {
+                               $acl_location = '.entity.' . 
$custom_function['entity_id'] . '.' . $custom_function['cat_id'];
+                       }
+                       else
+                       {
+                               $acl_location = 
$custom_function['acl_location'];
+                       }
+
+                       if(!$acl_location)
+                       {
+                               return  $receipt['error'][] = array('msg' => 
lang('acl_locastion is missing'));
+                       }
+
+                       $custom_function['descr'] = 
$this->db->db_addslashes($custom_function['descr']);
+
+
+                       $this->db->query("SELECT max(id) as maximum FROM 
fm_custom_function WHERE acl_location='$acl_location'",__LINE__,__FILE__);
+                       $this->db->next_record();
+                       $custom_function['id'] = $this->db->f('maximum')+1;
+
+                       $sql = "SELECT max(custom_sort) as max_sort FROM 
fm_custom_function where acl_location='$acl_location'";
+                       $this->db->query($sql,__LINE__,__FILE__);
+                       $this->db->next_record();
+                       $custom_sort    = $this->db->f('max_sort')+1;
+
+                       $values= array(
+                               $acl_location,
+                               $custom_function['id'],
+                               $custom_function['custom_function_file'],
+                               $custom_function['descr'],
+                               $custom_function['active'],
+                               $custom_sort
+                               );
+
+                       $values = $this->bocommon->validate_db_insert($values);
+
+//_debug_array($custom_function);
+                       $this->db->transaction_begin();
+
+                       $this->db->query("INSERT INTO fm_custom_function 
(acl_location, id, file_name, descr, active, custom_sort) "
+                               . "VALUES ($values)",__LINE__,__FILE__);
+
+                       $receipt['id']= $custom_function['id'];
+
+                       $this->db->transaction_commit();
+
+                       return $receipt;
+               }
+
+               function edit_custom_function($custom_function)
+               {
+                       if(!$custom_function['acl_location'] && 
$custom_function['entity_id'] && $custom_function['cat_id'])
+                       {
+                               $acl_location = '.entity.' . 
$custom_function['entity_id'] . '.' . $custom_function['cat_id'];
+                       }
+                       else
+                       {
+                               $acl_location = 
$custom_function['acl_location'];
+                       }
+
+                       if(!$acl_location)
+                       {
+                               return  $receipt['error'][] = array('msg' => 
lang('acl_locastion is missing'));
+                       }
+
+                       $custom_function['descr'] = 
$this->db->db_addslashes($custom_function['descr']);
+
+                       $this->db->transaction_begin();
+
+                               $value_set=array(
+                                       'descr'         => 
$custom_function['descr'],
+                                       'file_name'     => 
$custom_function['custom_function_file'],
+                                       'active'        => 
$custom_function['active']
+                                       );
+
+                               $value_set      = 
$this->bocommon->validate_db_update($value_set);
+
+                               $this->db->query("UPDATE fm_custom_function set 
$value_set WHERE acl_location='" . $acl_location . "' AND id=" . 
$custom_function['id'],__LINE__,__FILE__);
+
+                       $this->db->transaction_commit();
+
+                       $receipt['message'][] = array('msg'     => lang('Custom 
function has been edited'));
+
+                       return $receipt;
+               }
+
+               function resort_custom_function($data)
+               {
+                       if(is_array($data))
+                       {
+                               $resort = 
(isset($data['resort'])?$data['resort']:'up');
+                               $entity_id = 
(isset($data['entity_id'])?$data['entity_id']:'');
+                               $cat_id = 
(isset($data['cat_id'])?$data['cat_id']:'');
+                               $acl_location = 
(isset($data['acl_location'])?$data['acl_location']:'');
+                               $id = (isset($data['id'])?$data['id']:'');
+                       }
+
+                       if(!$acl_location && $entity_id && $cat_id)
+                       {
+                               $acl_location = '.entity.' . $entity_id . '.' . 
$cat_id;
+                       }
+                       else
+                       {
+                               $acl_location = $acl_location;
+                       }
+
+                       if(!$acl_location)
+                       {
+                               return  $receipt['error'][] = array('msg' => 
lang('acl_locastion is missing'));
+                       }
+
+                       $sql = "SELECT custom_sort FROM fm_custom_function 
where acl_location='$acl_location' AND id=$id";
+                       $this->db->query($sql,__LINE__,__FILE__);
+                       $this->db->next_record();
+                       $custom_sort    = $this->db->f('custom_sort');
+                       $sql2 = "SELECT max(custom_sort) as max_sort FROM 
fm_custom_function where acl_location='$acl_location'";
+                       $this->db->query($sql2,__LINE__,__FILE__);
+                       $this->db->next_record();
+                       $max_sort       = $this->db->f('max_sort');
+
+                       switch($resort)
+                       {
+                               case 'up':
+                                       if($custom_sort>1)
+                                       {
+                                               $sql = "UPDATE 
fm_custom_function set custom_sort=$custom_sort WHERE 
acl_location='$acl_location' AND custom_sort =" . ($custom_sort-1);
+                                               
$this->db->query($sql,__LINE__,__FILE__);
+                                               $sql = "UPDATE 
fm_custom_function set custom_sort=" . ($custom_sort-1) ." WHERE 
acl_location='$acl_location' AND id=$id";
+                                               
$this->db->query($sql,__LINE__,__FILE__);
+                                       }
+                                       break;
+                               case 'down':
+                                       if($max_sort > $custom_sort)
+                                       {
+                                               $sql = "UPDATE 
fm_custom_function set custom_sort=$custom_sort WHERE 
acl_location='$acl_location' AND custom_sort =" . ($custom_sort+1);
+                                               
$this->db->query($sql,__LINE__,__FILE__);
+                                               $sql = "UPDATE 
fm_custom_function set custom_sort=" . ($custom_sort+1) ." WHERE 
acl_location='$acl_location' AND id=$id";
+                                               
$this->db->query($sql,__LINE__,__FILE__);
+                                       }
+                                       break;
+                               default:
+                                       return;
+                                       break;
+                       }
+               }
+
+               function 
delete_custom_function($acl_location,$custom_function_id)
+               {
+                       $sql = "SELECT custom_sort FROM fm_custom_function 
where acl_location='$acl_location' AND id=$custom_function_id";
+                       $this->db->query($sql,__LINE__,__FILE__);
+                       $this->db->next_record();
+                       $custom_sort    = $this->db->f('custom_sort');
+                       $sql2 = "SELECT max(custom_sort) as max_sort FROM 
fm_custom_function where acl_location='$acl_location'";
+                       $this->db->query($sql2,__LINE__,__FILE__);
+                       $this->db->next_record();
+                       $max_sort       = $this->db->f('max_sort');
+                       if($max_sort>$custom_sort)
+                       {
+                               $sql = "UPDATE fm_custom_function set 
custom_sort=custom_sort-1 WHERE acl_location='$acl_location' AND custom_sort > 
$custom_sort";
+                               $this->db->query($sql,__LINE__,__FILE__);
+                       }
+                       $this->db->query("DELETE FROM fm_custom_function WHERE 
acl_location='$acl_location' AND id=$custom_function_id",__LINE__,__FILE__);
+               }
+       }
+?>
Index: property/inc/class.soadmin_location.inc.php
diff -u property/inc/class.soadmin_location.inc.php:1.14 
property/inc/class.soadmin_location.inc.php:1.15
--- property/inc/class.soadmin_location.inc.php:1.14    Fri Jan 27 14:05:43 2006
+++ property/inc/class.soadmin_location.inc.php Mon Jan 30 22:14:19 2006
@@ -1,1002 +1,1002 @@
-<?php
-       /**
-       * phpGroupWare - property: a Facilities Management System.
-       *
-       * @author Sigurd Nes <address@hidden>
-       * @copyright Copyright (C) 2003-2005 Free Software Foundation, Inc. 
http://www.fsf.org/
-       * @license http://www.gnu.org/licenses/gpl.html GNU General Public 
License
-       * @internal Development of this application was funded by 
http://www.bergen.kommune.no/bbb_/ekstern/
-       * @package property
-       * @subpackage admin
-       * @version $Id: class.soadmin_location.inc.php,v 1.14 2006/01/27 
14:05:43 sigurdne Exp $
-       */
-
-       /**
-        * Description
-        * @package property
-        */
-
-       class soadmin_location
-       {
-               var $grants;
-
-               function soadmin_location()
-               {
-                       $this->currentapp       = 
$GLOBALS['phpgw_info']['flags']['currentapp'];
-                       $this->account          = 
$GLOBALS['phpgw_info']['user']['account_id'];
-                       $this->bocommon         = 
CreateObject($this->currentapp.'.bocommon');
+<?php
+       /**
+       * phpGroupWare - property: a Facilities Management System.
+       *
+       * @author Sigurd Nes <address@hidden>
+       * @copyright Copyright (C) 2003-2005 Free Software Foundation, Inc. 
http://www.fsf.org/
+       * @license http://www.gnu.org/licenses/gpl.html GNU General Public 
License
+       * @internal Development of this application was funded by 
http://www.bergen.kommune.no/bbb_/ekstern/
+       * @package property
+       * @subpackage admin
+       * @version $Id: class.soadmin_location.inc.php,v 1.15 2006/01/30 
22:14:19 sigurdne Exp $
+       */
+
+       /**
+        * Description
+        * @package property
+        */
+
+       class soadmin_location
+       {
+               var $grants;
+
+               function soadmin_location()
+               {
+                       $this->currentapp       = 
$GLOBALS['phpgw_info']['flags']['currentapp'];
+                       $this->account          = 
$GLOBALS['phpgw_info']['user']['account_id'];
+                       $this->bocommon         = 
CreateObject($this->currentapp.'.bocommon');
                        $this->db               = $this->bocommon->new_db();
-                       $this->db2              = $this->bocommon->new_db();
-                       $this->join                     = $this->bocommon->join;
-                       $this->like                     = $this->bocommon->like;
-               }
-
-
-               function reset_fm_cache()
-               {
-                       $this->db->query("DELETE FROM fm_cache 
",__LINE__,__FILE__);
-               }
-
-               function read($data)
-               {
-                       if(is_array($data))
-                       {
-                               if ($data['start'])
-                               {
-                                       $start=$data['start'];
-                               }
-                               else
-                               {
-                                       $start=0;
-                               }
-                               $query = 
(isset($data['query'])?$data['query']:'');
-                               $sort = 
(isset($data['sort'])?$data['sort']:'DESC');
-                               $order = 
(isset($data['order'])?$data['order']:'');
-                       }
-
-                       if ($order)
-                       {
-                               $ordermethod = " order by $order $sort";
-
-                       }
-                       else
-                       {
-                               $ordermethod = ' order by id asc';
-                       }
-
-                       $table = 'fm_location_type';
-
-                       if($query)
-                       {
-                               $query = ereg_replace("'",'',$query);
-                               $query = ereg_replace('"','',$query);
-
-                               $querymethod = " where name $this->like 
'%$query%' or descr $this->like '%$query%'";
-                       }
-
-                       $sql = "SELECT * FROM $table $querymethod";
-
-                       $this->db2->query($sql,__LINE__,__FILE__);
-                       $this->total_records = $this->db2->num_rows();
-                       $this->db->limit_query($sql . 
$ordermethod,$start,__LINE__,__FILE__);
-
-                       while ($this->db->next_record())
-                       {
-                               $standard[] = array
-                               (
-                                       'id'    => $this->db->f('id'),
-                                       'name'  => $this->db->f('name'),
-                                       'prefix'=> $this->db->f('prefix'),
-                                       'descr' => $this->db->f('descr')
-                               );
-                       }
-                       return $standard;
-               }
-
-               function read_config($data=0)
-               {
-                       if ($data['start'])
-                       {
-                               $start=$data['start'];
-                       }
-                       else
-                       {
-                               $start=0;
-                       }
-
-                       if(is_array($data))
-                       {
-                               $query = 
(isset($data['query'])?$data['query']:'');
-                               $sort = 
(isset($data['sort'])?$data['sort']:'DESC');
-                               $order = 
(isset($data['order'])?$data['order']:'');
-                       }
-
-                       if ($order)
-                       {
-                               $ordermethod = " order by $order $sort";
-
-                       }
-                       else
-                       {
-                               $ordermethod = ' order by column_name asc';
-                       }
-
-                       if($query)
-                       {
-                               $query = ereg_replace("'",'',$query);
-                               $query = ereg_replace('"','',$query);
-
-                               $querymethod = " where name $this->like 
'%$query%' or column_name $this->like '%$query%'";
-                       }
-
-                       $sql = "SELECT fm_location_config.* 
,fm_location_type.name as name FROM fm_location_config  $this->join 
fm_location_type on fm_location_config.location_type=fm_location_type.id 
$querymethod";
-
-                       $this->db2->query($sql,__LINE__,__FILE__);
-                       $this->total_records = $this->db2->num_rows();
-                       $this->db->limit_query($sql . 
$ordermethod,$start,__LINE__,__FILE__);
-
-                       while ($this->db->next_record())
-                       {
-                               $config[] = array
-                               (
-                                       'column_name'           => 
$this->db->f('column_name'),
-                                       'input_text'            => 
$this->db->f('input_text'),
-                                       'f_key'                         => 
$this->db->f('f_key'),
-                                       'lookup_form'           => 
$this->db->f('lookup_form'),
-                                       'ref_to_category'       => 
$this->db->f('ref_to_category'),
-                                       'query_value'           => 
$this->db->f('query_value'),
-                                       'reference_table'       => 
$this->db->f('reference_table'),
-                                       'reference_id'          => 
$this->db->f('reference_id'),
-                                       'location_name'         => 
$this->db->f('name'),
-                                       'location_type'         => 
$this->db->f('location_type')
-                               );
-                       }
-                       return $config;
-               }
-
-               function read_config_single($column_name='')
-               {
-                       $this->db->query("SELECT location_type FROM 
fm_location_config where column_name='$column_name'",__LINE__,__FILE__);
-                       $this->db->next_record();
-                       return $this->db->f('location_type');
-               }
-
-
-               function read_single($id)
-               {
-
-                       $table = 'fm_location_type';
-
-                       $sql = "SELECT * FROM $table  where id='$id'";
-
-                       $this->db->query($sql,__LINE__,__FILE__);
-
-                       if ($this->db->next_record())
-                       {
-                               $standard['id']                 = 
$this->db->f('id');
-                               $standard['name']               = 
$this->db->f('name');
-                               $standard['descr']              = 
$this->db->f('descr');
-                               $standard['prefix']             = 
$this->db->f('prefix');
-
-                               return $standard;
-                       }
-               }
-
-               function add($standard)
-               {
-
-                       $standard['name'] = 
$this->db->db_addslashes($standard['name']);
-                       $standard['descr'] = 
$this->db->db_addslashes($standard['descr']);
-
-                       $standard['id'] = 
$this->bocommon->next_id('fm_location_type');
-
-                       $this->db->transaction_begin();
-
-
-                       $receipt['id']= $standard['id'];
-
-                       $this->init_process();
-
-                       $default_attrib['id'][]= 1;
-                       $default_attrib['column_name'][]= 'location_code';
-                       $default_attrib['type'][]='V';
-                       $default_attrib['precision'][] =4*$standard['id'];
-                       $default_attrib['nullable'][] ='False';
-                       $default_attrib['input_text'][] ='dummy';
-                       $default_attrib['statustext'][] ='dummy';
-                       $default_attrib['attrib_sort'][] ='NULL';
-                       $default_attrib['custom'][] ='NULL';
-
-                       $default_attrib['id'][]= 2;
-                       $default_attrib['column_name'][]= 'loc' . 
$standard['id'] . '_name';
-                       $default_attrib['type'][]='V';
-                       $default_attrib['precision'][] =50;
-                       $default_attrib['nullable'][] ='True';
-                       $default_attrib['input_text'][] ='dummy';
-                       $default_attrib['statustext'][] ='dummy';
-                       $default_attrib['attrib_sort'][] ='NULL';
-                       $default_attrib['custom'][] ='NULL';
-
-                       $default_attrib['id'][]= 3;
-                       $default_attrib['column_name'][]= 'entry_date';
-                       $default_attrib['type'][]='I';
-                       $default_attrib['precision'][] =4;
-                       $default_attrib['nullable'][] ='True';
-                       $default_attrib['input_text'][] ='dummy';
-                       $default_attrib['statustext'][] ='dummy';
-                       $default_attrib['attrib_sort'][] ='NULL';
-                       $default_attrib['custom'][] ='NULL';
-
-                       $default_attrib['id'][]= 4;
-                       $default_attrib['column_name'][]= 'category';
-                       $default_attrib['type'][]='I';
-                       $default_attrib['precision'][] =4;
-                       $default_attrib['nullable'][] ='False';
-                       $default_attrib['input_text'][] ='dummy';
-                       $default_attrib['statustext'][] ='dummy';
-                       $default_attrib['attrib_sort'][] ='NULL';
-                       $default_attrib['custom'][] ='NULL';
-
-                       $default_attrib['id'][]= 5;
-                       $default_attrib['column_name'][]= 'user_id';
-                       $default_attrib['type'][]='I';
-                       $default_attrib['precision'][] =4;
-                       $default_attrib['nullable'][] ='False';
-                       $default_attrib['input_text'][] ='dummy';
-                       $default_attrib['statustext'][] ='dummy';
-                       $default_attrib['attrib_sort'][] ='NULL';
-                       $default_attrib['custom'][] ='NULL';
-
-                       $default_attrib['id'][]= 6;
-                       $default_attrib['column_name'][]= 'status';
-                       $default_attrib['type'][]='LB';
-                       $default_attrib['precision'][] = False;
-                       $default_attrib['nullable'][] ='True';
-                       $default_attrib['input_text'][] ='Status';
-                       $default_attrib['statustext'][] ='Status';
-                       $default_attrib['attrib_sort'][] =1;
-                       $default_attrib['custom'][] =1;
-
-                       $default_attrib['id'][]= 7;
-                       $default_attrib['column_name'][]= 'remark';
-                       $default_attrib['type'][]='T';
-                       $default_attrib['precision'][] = False;
-                       $default_attrib['nullable'][] ='False';
-                       $default_attrib['input_text'][] ='Remark';
-                       $default_attrib['statustext'][] ='Remark';
-                       $default_attrib['attrib_sort'][] =2;
-                       $default_attrib['custom'][] =1;
-
-                       $fd=array();
-                       $fd['location_code'] = array('type' => 'varchar', 
'precision' => 25, 'nullable' => False);
-
-                       for ($i=1; $i<$standard['id']+1; $i++)
-                       {
-                               $fd['loc' . $i] = array('type' => 'varchar', 
'precision' => 4, 'nullable' => False);
-                               $pk[$i-1]= 'loc' . $i;
-
-                               $default_attrib['id'][]= $i+7;
-                               $default_attrib['column_name'][]= 'loc' . $i;
-                               $default_attrib['type'][]='V';
-                               $default_attrib['precision'][] =4;
-                               $default_attrib['nullable'][] ='False';
-                               $default_attrib['input_text'][] ='dummy';
-                               $default_attrib['statustext'][] ='dummy';
-                               $default_attrib['attrib_sort'][] ='NULL';
-                               $default_attrib['custom'][] ='NULL';
-                       }
-
-                       $fk_table='fm_location'. ($standard['id']-1);
-
-                       for ($i=1; $i<$standard['id']; $i++)
-                       {
-                               $fk['loc' . $i] = $fk_table . '.loc' . $i;
-                       }
-
-                       if($standard['id']==1)
-                       {
-                               $fd['part_of_town_id'] = array('type' => 'int', 
'precision' => 2, 'nullable' => True);
-                       }
-
-                       $fd['loc' .$standard['id'] . '_name'] = array('type' => 
'varchar', 'precision' => 25, 'nullable' => True);
-                       $fd['entry_date'] = array('type' => 'int', 'precision' 
=> 4, 'nullable' => True);
-                       $fd['category'] = array('type' => 'int', 'precision' => 
4, 'nullable' => True);
-                       $fd['user_id'] = array('type' => 'int', 'precision' => 
4, 'nullable' => True);
-                       $fd['remark'] = array('type' => 'text', 'nullable' => 
True);
-                       $fd['status'] = array('type' => 'int', 'precision' => 
4, 'nullable' => True);
-
-                       $ix = array('location_code');
-                       $uc = array();
-
-                       $fd_history = $fd;
-                       $fd_history['exp_date'] = array('type' => 
'timestamp','nullable' => True,'default' => 'current_timestamp');
-//                     $fd_history['change_type'] = array('type' => 
'int','precision' => '4','nullable' => True);
-
-                       
$add_columns_in_tables=array('fm_project','fm_tts_tickets','fm_request','fm_document','fm_investment');
-
-                       if($this->oProc->CreateTable('fm_location'. 
$standard['id'],array('fd' => $fd,'pk' => $pk,'fk' => $fk,'ix' => $ix,'uc' => 
$uc))
-                               && $this->oProc->CreateTable('fm_location'. 
$standard['id'] . '_history',array('fd' => $fd_history)))
-                       {
-
-                               $this->oProc->CreateTable('fm_location'. 
$standard['id'] . '_category', array(
-                               'fd' => array(
-                                       'id' => array('type' => 
'int','precision' => '4','nullable' => False),
-                                       'descr' => array('type' => 
'varchar','precision' => '50','nullable' => True)
-                               ),
-                               'pk' => array('id'),
-                               'fk' => array(),
-                               'ix' => array(),
-                               'uc' => array()));
-
-                               for ($i=0;$i<count($add_columns_in_tables);$i++)
-                               {
-                                       
$this->oProc->AddColumn($add_columns_in_tables[$i],'loc'. $standard['id'], 
array('type' => 'varchar', 'precision' => 4, 'nullable' => True));
-                               }
-
-                               $values_insert= array(
-                                       $standard['id'],
-                                       $standard['name'],
-                                       $standard['descr'],
-                                   $this->db->db_addslashes(implode(',',$pk)),
-                                   $this->db->db_addslashes(implode(',',$ix)),
-                                   $this->db->db_addslashes(implode(',',$uc)),
-                                       );
-
-                               $values_insert  = 
$this->bocommon->validate_db_insert($values_insert);
-
-                               $this->db->query("INSERT INTO fm_location_type 
(id,name, descr,pk,ix,uc) "
-                                       . "VALUES 
($values_insert)",__LINE__,__FILE__);
-
-                               for ($i=0;$i<count($default_attrib['id']);$i++)
-                               {
-                                       $values_insert= array(
-                                               $standard['id'],
-                                               $default_attrib['id'][$i],
-                                               
$default_attrib['column_name'][$i],
-                                               $default_attrib['type'][$i],
-                                               
$default_attrib['precision'][$i],
-                                               
$default_attrib['input_text'][$i],
-                                               
$default_attrib['statustext'][$i],
-                                               
$default_attrib['attrib_sort'][$i],
-                                               $default_attrib['custom'][$i],
-                                               $default_attrib['nullable'][$i]
-                                               );
-
-
-                                       $values_insert  = 
$this->bocommon->validate_db_insert($values_insert);
-
-                                       $this->db->query("INSERT INTO 
fm_location_attrib 
(type_id,id,column_name,datatype,precision_,input_text,statustext,attrib_sort,custom,nullable)
 "
-                                               . "VALUES 
($values_insert)",__LINE__,__FILE__);
-                               }
-
-                               $type_id=$standard['id'];
-
-                               $this->db->query("INSERT INTO 
fm_location_choice (type_id,attrib_id,id,value) "
-                                       . "VALUES 
($type_id,1,1,'ok')",__LINE__,__FILE__);
-                               $this->db->query("INSERT INTO 
fm_location_choice (type_id,attrib_id,id,value) "
-                                       . "VALUES ($type_id,1,2,'Not 
Active')",__LINE__,__FILE__);
-
-
-                               $receipt['message'][] = array('msg'     => 
lang('table %1 has been saved','fm_location'. $receipt['id'])        );
-                               $this->db->transaction_commit();
-                       }
-                       else
-                       {
-                               $receipt['error'][] = array('msg'       => 
lang('table could not be added')     );
-                               if($this->db->Transaction)
-                               {
-                                       $this->db->transaction_abort();
-                               }
-                               else
-                               {
-                                       $this->db->query("DELETE FROM 
fm_location_type WHERE id='" . $standard['id'] . "'",__LINE__,__FILE__);
-                                       unset($receipt['id']);
-
-                               }
-                       }
-
-                       return $receipt;
-               }
-
-               function edit($standard)
-               {
-
-                       if (!$standard['name'])
-                       {
-                               $receipt['error'][] = array('msg'=>lang('Name 
not entered!'));
-                       }
-
-                       if (!$receipt['error'])
-                       {
-                               $table = 'fm_location_type';
-
-                               $standard['name'] = 
$this->db->db_addslashes($standard['name']);
-                               $standard['descr'] = 
$this->db->db_addslashes($standard['descr']);
-
-                               $this->db->query("UPDATE $table set descr='" . 
$standard['descr'] . "', name='". $standard['name']
-                                       . "' WHERE id='" . $standard['id']. 
"'",__LINE__,__FILE__);
-
-
-                               $receipt['message'][] = array('msg'=> 
lang('Standard has been edited'));
-                       }
-                       else
-                       {
-                               $receipt['error'][] = array('msg'       => 
lang('Standard has NOT been edited'));
-                       }
-
-                       return $receipt;
-               }
-
-               function delete($type_id,$id,$attrib)
-               {
-                       $this->init_process();
-
-                       if($attrib)
-                       {
-                               $table = 'fm_location_attrib';
-
-                               $this->db->query("SELECT column_name,type_id 
FROM fm_location_attrib WHERE id='" . $id . "'",__LINE__,__FILE__);
-                               $this->db->next_record();
-                               $ColumnName             = 
$this->db->f('column_name');
-
-                               $this->oProc->DropColumn('fm_location' 
.$type_id ,'', $ColumnName);
-                               $this->oProc->DropColumn('fm_location' 
.$type_id . '_history','', $ColumnName);
-                       }
-                       else
-                       {
-                               $this->oProc->DropTable('fm_location' . $id);
-                               $this->oProc->DropTable('fm_location' . $id . 
'_category');
-                               $this->oProc->DropTable('fm_location' . $id . 
'_history');
-
-                               $table                  = 'fm_location_type';
-                               $attrib_table   = 'fm_location_attrib';
-                               $this->db->query("DELETE FROM $attrib_table 
WHERE type_id='" . $id . "'",__LINE__,__FILE__);
-                               $this->db->query("DELETE FROM 
fm_location_choice WHERE type_id='" . $id . "'",__LINE__,__FILE__);
-                       }
-                       $this->db->query("DELETE FROM $table WHERE id='" . $id 
. "'",__LINE__,__FILE__);
-               }
-
-               function read_attrib($data)
-               {
-
-//_debug_array($data);
-                       if(is_array($data))
-                       {
-                               if ($data['start'])
-                               {
-                                       $start=$data['start'];
-                               }
-                               else
-                               {
-                                       $start=0;
-                               }
-                               $query = 
(isset($data['query'])?$data['query']:'');
-                               $sort = 
(isset($data['sort'])?$data['sort']:'DESC');
-                               $order = 
(isset($data['order'])?$data['order']:'');
-                               $type_id = 
(isset($data['type_id'])?$data['type_id']:0);
-                               $lookup_type = 
(isset($data['lookup_type'])?$data['lookup_type']:'');
-                               $allrows = 
(isset($data['allrows'])?$data['allrows']:'');
-                       }
-
-                       if ($order)
-                       {
-                               $ordermethod = " order by $order $sort";
-
-                       }
-                       else
-                       {
-                               $ordermethod = ' order by 
fm_location_attrib.attrib_sort asc';
-                       }
-
-                       $table          = 'fm_location_attrib';
-                       $type_table = 'fm_location_type';
-
-                       if ($lookup_type)
-                       {
-                               $filtermethod = " OR (type_id < $lookup_type 
AND lookup_form=1) ";
-
-                       }
-
-                       if($query)
-                       {
-                               $query = ereg_replace("'",'',$query);
-                               $query = ereg_replace('"','',$query);
-
-                               $querymethod = " and ($table.name $this->like 
'%$query%' or $table.descr $this->like '%$query%')";
-                       }
-
-                       $sql = "SELECT 
$table.id,$table.type_id,$table.list,$table.attrib_sort,$table.location_form,lookup_form,$table.column_name,$table.size
 ,statustext,$table.input_text,"
-                               . " $table.datatype ,$type_table.name as type 
FROM $type_table $this->join $table on $table.type_id = $type_table.id "
-                               . " WHERE $table.type_id= '$type_id' AND custom 
= 1 $filtermethod $querymethod";
-
-                       $this->db2->query($sql,__LINE__,__FILE__);
-                       $this->total_records = $this->db2->num_rows();
-                       if(!$allrows)
-                       {
-                               $this->db->limit_query($sql . 
$ordermethod,$start,__LINE__,__FILE__);
-                       }
-                       else
-                       {
-                               $this->db->query($sql . 
$ordermethod,__LINE__,__FILE__);
-                       }
-
-                       while ($this->db->next_record())
-                       {
-                               $attrib[] = array
-                               (
-                                       'id'                    => 
$this->db->f('id'),
-                                       'location_type' => 
$this->db->f('type_id'),
-                                       'attrib_sort'   => 
$this->db->f('attrib_sort'),
-                                       'list'                  => 
$this->db->f('list'),
-                                       'lookup_form'   => 
$this->db->f('lookup_form'),
-                                       'location_form' => 
$this->db->f('location_form'),
-                                       'column_name'   => 
$this->db->f('column_name'),
-                                       'size'                  => 
$this->db->f('size'),
-                                       'statustext'    => 
$this->db->f('statustext'),
-                                       'input_text'    => 
$this->db->f('input_text'),
-                                       'type_name'             => 
$this->db->f('type'),
-                                       'datatype'              => 
$this->db->f('datatype')
-                               );
-                       }
-                       return $attrib;
-               }
-
-               function read_single_attrib($type_id,$id)
-               {
-
-                       $table          = 'fm_location_attrib';
-                       $type_table = 'fm_location_type';
-
-                       $sql = "SELECT $table.* ,$type_table.name as type_name 
FROM $type_table $this->join $table on $table.type_id = $type_id where 
$table.id= '$id'";
-
-                       $this->db->query($sql,__LINE__,__FILE__);
-
-                       if ($this->db->next_record())
-                       {
-                               $attrib['id']                                   
        = $this->db->f('id');
-                               $attrib['column_name']                          
= $this->db->f('column_name');
-                               $attrib['input_text']                           
= $this->db->f('input_text');
-                               $attrib['statustext']                           
= $this->db->f('statustext');
-                               $attrib['column_info']['precision']     = 
$this->db->f('precision_');
-                               $attrib['column_info']['scale']         = 
$this->db->f('scale');
-                               $attrib['column_info']['default']       = 
$this->db->f('default_value');
-                               $attrib['column_info']['nullable']      = 
$this->db->f('nullable');
-                               $attrib['column_info']['type']          = 
$this->db->f('datatype');
-                               $attrib['type_id']                              
        = $type_id;
-                               $attrib['type_name']                            
= $this->db->f('type_name');
-                               $attrib['lookup_form']                          
= $this->db->f('lookup_form');
-                               $attrib['list']                                 
        = $this->db->f('list');
-                               if($this->db->f('datatype')=='R' || 
$this->db->f('datatype')=='CH' || $this->db->f('datatype')=='LB')
-                               {
-                                       $attrib['choice'] = 
$this->read_attrib_choice($type_id,$id);
-                               }
-
-                               return $attrib;
-                       }
-               }
-
-               function read_attrib_choice($type_id,$attrib_id)
-               {
-                       $choice_table = 'fm_location_choice';
-                       $sql = "SELECT * FROM $choice_table WHERE 
type_id=$type_id AND attrib_id=$attrib_id";
-                       $this->db->query($sql,__LINE__,__FILE__);
-
-                       while ($this->db->next_record())
-                       {
-                               $choice[] = array
-                               (
-                                       'id'    => $this->db->f('id'),
-                                       'value' => $this->db->f('value')
-                               );
-                       }
-                       return $choice;
-               }
-
-               function add_attrib($attrib)
-               {
-                       $attrib['column_name'] = 
$this->db->db_addslashes($attrib['column_name']);
-                       $attrib['input_text'] = 
$this->db->db_addslashes($attrib['input_text']);
-                       $attrib['statustext'] = 
$this->db->db_addslashes($attrib['statustext']);
-                       $attrib['default'] = 
$this->db->db_addslashes($attrib['default']);
-                       $attrib['id'] = 
$this->bocommon->next_id('fm_location_attrib',array('type_id'=>$attrib['type_id']));
-
-                       $sql = "SELECT max(attrib_sort) as max_sort FROM 
fm_location_attrib where type_id=" . $attrib['type_id'];
-                       $this->db->query($sql,__LINE__,__FILE__);
-                       $this->db->next_record();
-                       $attrib_sort    = $this->db->f('max_sort')+1;
-
-                       if($precision = 
$this->bocommon->translate_datatype_precision($attrib['column_info']['type']))
-                       {
-                               $attrib['column_info']['precision']=$precision;
-                       }
-
-                       $values= array(
-                               $attrib['id'],
-                               $attrib['column_name'],
-                               $attrib['input_text'],
-                               $attrib['statustext'],
-                               $attrib['type_id'],
-                               $attrib['lookup_form'],
-                               $attrib['list'],
-                               $attrib_sort,
-                               $attrib['column_info']['type'],
-                               $attrib['column_info']['precision'],
-                               $attrib['column_info']['scale'],
-                               $attrib['column_info']['default'],
-                               $attrib['column_info']['nullable'],
-                               1
-                               );
-
-                       $values = $this->bocommon->validate_db_insert($values);
-
-                       $this->db->transaction_begin();
-
-                       $this->db->query("INSERT INTO fm_location_attrib 
(id,column_name, input_text, statustext, 
type_id,lookup_form,list,attrib_sort,datatype,precision_,scale,default_value,nullable,custom)
 "
-                               . "VALUES ($values)",__LINE__,__FILE__);
-
-                       $receipt['id']= $attrib['id'];
-
-                       $attrib['column_info']['type']  = 
$this->bocommon->translate_datatype_insert($attrib['column_info']['type']);
-
-                       if(!$attrib['column_info']['default'])
-                       {
-                               unset($attrib['column_info']['default']);
-                       }
-
-                       $this->init_process();
-
-                       
if($this->oProc->AddColumn('fm_location'.$attrib['type_id'],$attrib['column_name'],
 $attrib['column_info'])
-                               && 
$this->oProc->AddColumn('fm_location'.$attrib['type_id'] 
.'_history',$attrib['column_name'], $attrib['column_info']))
-                       {
-                               $receipt['message'][] = array('msg'     => 
lang('Attribute has been saved')     );
-                               $this->db->transaction_commit();
-
-                       }
-                       else
-                       {
-                               $receipt['error'][] = array('msg'       => 
lang('column could not be added')    );
-                               if($this->db->Transaction)
-                               {
-                                       $this->db->transaction_abort();
-                               }
-                               else
-                               {
-                                       $this->db->query("DELETE FROM 
fm_location_attrib WHERE id=" . $receipt['id'] . " AND type_id=" . 
$attrib['type_id'],__LINE__,__FILE__);
-                                       unset($receipt['id']);
-
-                               }
-                       }
-
-                       return $receipt;
-               }
-
-               function init_process()
-               {
-                       $this->oProc                                            
= 
CreateObject('phpgwapi.schema_proc',$GLOBALS['phpgw_info']['server']['db_type']);
-                       $this->oProc->m_odb                                     
= $this->db;
-                       $this->oProc->m_odb->Halt_On_Error      = 'report';
-               }
-
-
-               function save_config($values='',$column_name='')
-               {
-
-                       $this->db->query("SELECT * FROM fm_location_config  
WHERE column_name='$column_name' ",__LINE__,__FILE__);
-                       $this->db->next_record();
-
-                       $column_info['type']            = 
$this->db->f('datatype');
-                       $column_info['precision']       = 
$this->db->f('precision_');
-                       $column_info['scale']           = $this->db->f('scale');
-                       $column_info['default']         = 
$this->db->f('default_value');
-                       $column_info['nullable']        = 
$this->db->f('nullable');
-                       $location_type                          = 
$this->db->f('location_type');
-
-                       if(!($location_type==$values[$column_name]))
-                       {
-                               $this->init_process();
-
-                               
if($this->oProc->AddColumn('fm_location'.$values[$column_name],$column_name, 
$column_info))
-                               {
-                                       if($column_name=='street_id')
-                                       {
-                                               
$this->oProc->AddColumn('fm_location'.$values[$column_name],'street_number', 
array('type'=>'varchar','precision'=>10));
-                                               
$this->oProc->DropColumn('fm_location' .$location_type ,'','street_number');
-                                       }
-
-                                       $this->oProc->DropColumn('fm_location' 
.$location_type ,'', $column_name);
-                                       $this->oProc->DropColumn('fm_location' 
.$location_type . ' _history','', $column_name);
-
-
-                                       $this->db->query("UPDATE 
fm_location_config set
-                                               location_type           = '". 
$values[$column_name]     . "' WHERE column_name='" . $column_name . 
"'",__LINE__,__FILE__);
-
-                                       $receipt['message'][] = array('msg'     
=> lang('column %1 has been moved',$column_name));
-                               }
-                               else
-                               {
-                                       $receipt['message'][] = array('msg'     
=> lang('column %1 could not be moved',$column_name));
-
-                               }
-                       }
-
-                       return $receipt;
-               }
-
-
-               function edit_attrib($attrib)
-               {
-                       $attrib['column_name'] = 
$this->db->db_addslashes($attrib['column_name']);
-                       $attrib['input_text'] = 
$this->db->db_addslashes($attrib['input_text']);
-                       $attrib['statustext'] = 
$this->db->db_addslashes($attrib['statustext']);
-                       $attrib['default'] = 
$this->db->db_addslashes($attrib['default']);
-
-                       $this->db->query("SELECT * FROM fm_location_attrib 
WHERE type_id = " . $attrib['type_id'] ." AND id=" . 
$attrib['id'],__LINE__,__FILE__);
-                       $this->db->next_record();
-                       $OldColumnName          = $this->db->f('column_name');
-                       $OldColumnType          = $this->db->f('datatype');
-                       $OldColumnPrecision     = $this->db->f('precision_');
-
-                       $table_def = $this->get_table_def($attrib['type_id']);
-                       $history_table_def['fm_location'.$attrib['type_id'] . 
'_history'] = $table_def['fm_location'.$attrib['type_id']];
-                       
$history_table_def['fm_location'.$attrib['type_id']]['fd']['exp_date']=array('type'
 => 'timestamp','nullable' => False,'default' => 'current_timestamp');
-//                     
$history_table_def['fm_location'.$attrib['type_id']]['fd']['change_type'] = 
array('type' => 'int','precision' => '4','nullable' => True);
-
-                       
unset($history_table_def['fm_location'.$attrib['type_id']]['pk']);
-                       
unset($history_table_def['fm_location'.$attrib['type_id']]['fk']);
-                       
unset($history_table_def['fm_location'.$attrib['type_id']]['ix']);
-                       
unset($history_table_def['fm_location'.$attrib['type_id']]['uc']);
-
-                       if($this->receipt['error'])
-                       {
-                               return $this->receipt;
-                       }
-
-                       $this->db->transaction_begin();
-
-                       $value_set=array(
-                               'input_text'    => $attrib['input_text'],
-                               'statustext'    => $attrib['statustext'],
-                               'list'                  => $attrib['list'],
-                               'lookup_form'   => $attrib['lookup_form'],
-                               );
-
-                       $value_set      = 
$this->bocommon->validate_db_update($value_set);
-
-                       $this->db->query("UPDATE fm_location_attrib set 
$value_set WHERE type_id = " . $attrib['type_id'] ." AND id=" . 
$attrib['id'],__LINE__,__FILE__);
-
-                       $this->init_process();
-
-                       if($OldColumnName !=$attrib['column_name'])
-                       {
-                               $value_set=array(
-                                       'column_name'           => 
$attrib['column_name']
-                                       );
-
-                               $value_set      = 
$this->bocommon->validate_db_update($value_set);
-
-                               $this->db->query("UPDATE fm_location_attrib set 
$value_set WHERE type_id = " . $attrib['type_id'] ." AND id=" . 
$attrib['id'],__LINE__,__FILE__);
-
-                               $this->oProc->m_aTables = $table_def;
-                               
$this->oProc->RenameColumn('fm_location'.$attrib['type_id'], $OldColumnName, 
$attrib['column_name']);
-                               $this->oProc->m_aTables = $history_table_def;
-                               
$this->oProc->RenameColumn('fm_location'.$attrib['type_id'] . '_history', 
$OldColumnName, $attrib['column_name']);
-                       }
-
-                       if($OldColumnType !=$attrib['column_info']['type'])
-                       {
-                               if($precision = 
$this->bocommon->translate_datatype_precision($attrib['column_info']['type']))
-                               {
-                                       
$attrib['column_info']['precision']=$precision;
-                               }
-
-                               if(!$attrib['column_info']['default'])
-                               {
-                                       
unset($attrib['column_info']['default']);
-                               }
-
-
-                               $value_set=array(
-                                       'type_id'                       => 
$attrib['type_id'],
-                                       'datatype'                      => 
$attrib['column_info']['type'],
-                                       'precision_'            => 
$attrib['column_info']['precision'],
-                                       'scale'                         => 
$attrib['column_info']['scale'],
-                                       'default_value'         => 
$attrib['column_info']['default'],
-                                       'nullable'                      => 
$attrib['column_info']['nullable']
-                                       );
-
-                               $value_set      = 
$this->bocommon->validate_db_update($value_set);
-
-                               $this->db->query("UPDATE fm_location_attrib set 
$value_set WHERE type_id = " . $attrib['type_id'] ." AND id=" . 
$attrib['id'],__LINE__,__FILE__);
-
-                               $attrib['column_info']['type']  = 
$this->bocommon->translate_datatype_insert($attrib['column_info']['type']);
-
-                               $this->oProc->m_aTables = $table_def;
-                               
$this->oProc->AlterColumn('fm_location'.$attrib['type_id'],$attrib['column_name'],$attrib['column_info']);
-                               $this->oProc->m_aTables = $history_table_def;
-                               
$this->oProc->AlterColumn('fm_location'.$attrib['type_id'] . 
'_history',$attrib['column_name'],$attrib['column_info']);
-                       }
-
-                       $choice_table ='fm_location_choice';
-
-                       if($attrib['new_choice'])
-                       {
-                               $choice_id = 
$this->bocommon->next_id($choice_table 
,array('type_id'=>$attrib['type_id'],'attrib_id'=>$attrib['id']));
-
-                               $values= array(
-                                       $attrib['type_id'],
-                                       $attrib['id'],
-                                       $choice_id,
-                                       $attrib['new_choice']
-                                       );
-
-                               $values = 
$this->bocommon->validate_db_insert($values);
-
-                               $this->db->query("INSERT INTO $choice_table 
(type_id,attrib_id,id,value) "
-                               . "VALUES ($values)",__LINE__,__FILE__);
-                       }
-
-                       if($attrib['delete_choice'])
-                       {
-                               for 
($i=0;$i<count($attrib['delete_choice']);$i++)
-                               {
-                                       $this->db->query("DELETE FROM 
$choice_table WHERE type_id=" . $attrib['type_id']. " AND attrib_id=" . 
$attrib['id']  ." AND id=" . $attrib['delete_choice'][$i],__LINE__,__FILE__);
-                               }
-                       }
-
-                       if($attrib['column_info']['type']!='R' && 
$attrib['column_info']['type']!='CH' && $attrib['column_info']['type']!='LB')
-                       {
-                               $this->db->query("DELETE FROM $choice_table 
WHERE type_id=" . $attrib['type_id']. " AND attrib_id=" . 
$attrib['id'],__LINE__,__FILE__);
-                       }
-
-                       $this->db->transaction_commit();
-
-                       $receipt['message'][] = array('msg'     => 
lang('Attribute has been edited'));
-
-                       return $receipt;
-
-               }
-
-               function get_table_def($id='')
-               {
-
-                       $metadata = $this->db->metadata('fm_location'.$id);
-//     _debug_array($metadata);
-
-                       for ($i=0; $i<count($metadata); $i++)
-                       {
-                               $sql = "SELECT * FROM fm_location_attrib WHERE 
type_id=$id AND column_name = '" . $metadata[$i]['name'] . "'";
-//     _debug_array($sql);
-
-                               $this->db->query($sql,__LINE__,__FILE__);
-                               if($this->db->next_record())
-                               {
-
-                                       if(!$precision = 
$this->db->f('precision_'))
-                                       {
-                                               $precision = 
$this->bocommon->translate_datatype_precision($this->db->f('datatype'));
-                                       }
-
-                                       $fd[$metadata[$i]['name']] = array(
-                                                       'type' => 
$this->bocommon->translate_datatype_insert(stripslashes($this->db->f('datatype'))),
-                                                       'precision' => 
$precision,
-                                                       'nullable' => 
stripslashes($this->db->f('nullable')),
-                                                       'default' => 
stripslashes($this->db->f('default_value')),
-                                                       'scale' => 
$this->db->f('scale')
-                                                       );
-                                       unset($precision);
-                               }
-                               else
-                               {
-                                       $this->receipt['error'][] = array('msg' 
=> lang('Column %1 is missing from metadata',$metadata[$i]['name']));
-                               }
-                       }
-
-                       $this->db->query("SELECT * FROM fm_location_type WHERE 
id=$id");
-                       $this->db->next_record();
-
-
-                       if($id>1)
-                       {
-                               $fk_table='fm_location'. ($id-1);
-                               for ($i=1; $i<$id; $i++)
-                               {
-                                       $fk['loc' . $i] = $fk_table . '.loc' . 
$i;
-                               }
-                       }
-
-                       $table_def = array(
-                               'fm_location'.$id =>    array(
-                                       'fd' => $fd
-                                       )
-                               );
-
-                       if($this->db->f('pk'))
-                       {
-                               $table_def['fm_location'.$id]['pk'] = 
explode(',',$this->db->f('pk'));
-                       }
-                       if($fk)
-                       {
-                               $table_def['fm_location'.$id]['fk'] = $fk;
-                       }
-                       if($this->db->f('ix'))
-                       {
-                               $table_def['fm_location'.$id]['ix'] = 
explode(',',$this->db->f('ix'));
-                       }
-                       if($this->db->f('uc'))
-                       {
-                               $table_def['fm_location'.$id]['uc'] = 
explode(',',$this->db->f('uc'));
-                       }
-
-
-
-                       return $table_def;
-               }
-
-
-               function select_location_type()
-               {
-                       $this->db->query("SELECT * FROM fm_location_type ORDER 
BY id ");
-
-                       $i = 0;
-                       while ($this->db->next_record())
-                       {
-                               $location_type[$i]['id']                = 
$this->db->f('id');
-                               $location_type[$i]['name']              = 
stripslashes($this->db->f('name'));
-                               $location_type[$i]['descr']     = 
stripslashes($this->db->f('descr'));
-                               $i++;
-                       }
-//_debug_array($location_type);
-
-                       return $location_type;
-               }
-
-               function resort_attrib($data)
-               {
-                       if(is_array($data))
-                       {
-                               $resort = 
(isset($data['resort'])?$data['resort']:'up');
-                               $type_id = 
(isset($data['type_id'])?$data['type_id']:'');
-                               $id = (isset($data['id'])?$data['id']:'');
-                       }
-
-                       if(!$type_id)
-                       {
-                               return;
-                       }
-
-                       $sql = "SELECT attrib_sort FROM fm_location_attrib 
where type_id=$type_id AND id=$id";
-                       $this->db->query($sql,__LINE__,__FILE__);
-                       $this->db->next_record();
-                       $attrib_sort    = $this->db->f('attrib_sort');
-                       $sql2 = "SELECT max(attrib_sort) as max_sort FROM 
fm_location_attrib where type_id=$type_id";
-                       $this->db->query($sql2,__LINE__,__FILE__);
-                       $this->db->next_record();
-                       $max_sort       = $this->db->f('max_sort');
-
-                       switch($resort)
-                       {
-                               case 'up':
-                                       if($attrib_sort>1)
-                                       {
-                                               $sql = "UPDATE 
fm_location_attrib set attrib_sort=$attrib_sort WHERE type_id=$type_id AND 
attrib_sort =" . ($attrib_sort-1);
-                                               
$this->db->query($sql,__LINE__,__FILE__);
-                                               $sql = "UPDATE 
fm_location_attrib set attrib_sort=" . ($attrib_sort-1) ." WHERE 
type_id=$type_id AND id=$id";
-                                               
$this->db->query($sql,__LINE__,__FILE__);
-                                       }
-                                       break;
-                               case 'down':
-                                       if($max_sort > $attrib_sort)
-                                       {
-                                               $sql = "UPDATE 
fm_location_attrib set attrib_sort=$attrib_sort WHERE type_id=$type_id AND 
attrib_sort =" . ($attrib_sort+1);
-                                               
$this->db->query($sql,__LINE__,__FILE__);
-                                               $sql = "UPDATE 
fm_location_attrib set attrib_sort=" . ($attrib_sort+1) ." WHERE 
type_id=$type_id AND id=$id";
-                                               
$this->db->query($sql,__LINE__,__FILE__);
-                                       }
-                                       break;
-                               default:
-                                       return;
-                                       break;
-                       }
-               }
-       }
-?>
+                       $this->db2              = $this->bocommon->new_db();
+
+                       $this->join                     = $this->bocommon->join;
+                       $this->like                     = $this->bocommon->like;
+               }
+
+               function reset_fm_cache()
+               {
+                       $this->db->query("DELETE FROM fm_cache 
",__LINE__,__FILE__);
+               }
+
+               function read($data)
+               {
+                       if(is_array($data))
+                       {
+                               if ($data['start'])
+                               {
+                                       $start=$data['start'];
+                               }
+                               else
+                               {
+                                       $start=0;
+                               }
+                               $query = 
(isset($data['query'])?$data['query']:'');
+                               $sort = 
(isset($data['sort'])?$data['sort']:'DESC');
+                               $order = 
(isset($data['order'])?$data['order']:'');
+                       }
+
+                       if ($order)
+                       {
+                               $ordermethod = " order by $order $sort";
+
+                       }
+                       else
+                       {
+                               $ordermethod = ' order by id asc';
+                       }
+
+                       $table = 'fm_location_type';
+
+                       if($query)
+                       {
+                               $query = ereg_replace("'",'',$query);
+                               $query = ereg_replace('"','',$query);
+
+                               $querymethod = " where name $this->like 
'%$query%' or descr $this->like '%$query%'";
+                       }
+
+                       $sql = "SELECT * FROM $table $querymethod";
+
+                       $this->db2->query($sql,__LINE__,__FILE__);
+                       $this->total_records = $this->db2->num_rows();
+                       $this->db->limit_query($sql . 
$ordermethod,$start,__LINE__,__FILE__);
+
+                       while ($this->db->next_record())
+                       {
+                               $standard[] = array
+                               (
+                                       'id'    => $this->db->f('id'),
+                                       'name'  => $this->db->f('name'),
+                                       'prefix'=> $this->db->f('prefix'),
+                                       'descr' => $this->db->f('descr')
+                               );
+                       }
+                       return $standard;
+               }
+
+               function read_config($data=0)
+               {
+                       if ($data['start'])
+                       {
+                               $start=$data['start'];
+                       }
+                       else
+                       {
+                               $start=0;
+                       }
+
+                       if(is_array($data))
+                       {
+                               $query = 
(isset($data['query'])?$data['query']:'');
+                               $sort = 
(isset($data['sort'])?$data['sort']:'DESC');
+                               $order = 
(isset($data['order'])?$data['order']:'');
+                       }
+
+                       if ($order)
+                       {
+                               $ordermethod = " order by $order $sort";
+
+                       }
+                       else
+                       {
+                               $ordermethod = ' order by column_name asc';
+                       }
+
+                       if($query)
+                       {
+                               $query = ereg_replace("'",'',$query);
+                               $query = ereg_replace('"','',$query);
+
+                               $querymethod = " where name $this->like 
'%$query%' or column_name $this->like '%$query%'";
+                       }
+
+                       $sql = "SELECT fm_location_config.* 
,fm_location_type.name as name FROM fm_location_config  $this->join 
fm_location_type on fm_location_config.location_type=fm_location_type.id 
$querymethod";
+
+                       $this->db2->query($sql,__LINE__,__FILE__);
+                       $this->total_records = $this->db2->num_rows();
+                       $this->db->limit_query($sql . 
$ordermethod,$start,__LINE__,__FILE__);
+
+                       while ($this->db->next_record())
+                       {
+                               $config[] = array
+                               (
+                                       'column_name'           => 
$this->db->f('column_name'),
+                                       'input_text'            => 
$this->db->f('input_text'),
+                                       'f_key'                         => 
$this->db->f('f_key'),
+                                       'lookup_form'           => 
$this->db->f('lookup_form'),
+                                       'ref_to_category'       => 
$this->db->f('ref_to_category'),
+                                       'query_value'           => 
$this->db->f('query_value'),
+                                       'reference_table'       => 
$this->db->f('reference_table'),
+                                       'reference_id'          => 
$this->db->f('reference_id'),
+                                       'location_name'         => 
$this->db->f('name'),
+                                       'location_type'         => 
$this->db->f('location_type')
+                               );
+                       }
+                       return $config;
+               }
+
+               function read_config_single($column_name='')
+               {
+                       $this->db->query("SELECT location_type FROM 
fm_location_config where column_name='$column_name'",__LINE__,__FILE__);
+                       $this->db->next_record();
+                       return $this->db->f('location_type');
+               }
+
+
+               function read_single($id)
+               {
+
+                       $table = 'fm_location_type';
+
+                       $sql = "SELECT * FROM $table  where id='$id'";
+
+                       $this->db->query($sql,__LINE__,__FILE__);
+
+                       if ($this->db->next_record())
+                       {
+                               $standard['id']                 = 
$this->db->f('id');
+                               $standard['name']               = 
$this->db->f('name');
+                               $standard['descr']              = 
$this->db->f('descr');
+                               $standard['prefix']             = 
$this->db->f('prefix');
+
+                               return $standard;
+                       }
+               }
+
+               function add($standard)
+               {
+
+                       $standard['name'] = 
$this->db->db_addslashes($standard['name']);
+                       $standard['descr'] = 
$this->db->db_addslashes($standard['descr']);
+
+                       $standard['id'] = 
$this->bocommon->next_id('fm_location_type');
+
+                       $this->db->transaction_begin();
+
+
+                       $receipt['id']= $standard['id'];
+
+                       $this->init_process();
+
+                       $default_attrib['id'][]= 1;
+                       $default_attrib['column_name'][]= 'location_code';
+                       $default_attrib['type'][]='V';
+                       $default_attrib['precision'][] =4*$standard['id'];
+                       $default_attrib['nullable'][] ='False';
+                       $default_attrib['input_text'][] ='dummy';
+                       $default_attrib['statustext'][] ='dummy';
+                       $default_attrib['attrib_sort'][] ='NULL';
+                       $default_attrib['custom'][] ='NULL';
+
+                       $default_attrib['id'][]= 2;
+                       $default_attrib['column_name'][]= 'loc' . 
$standard['id'] . '_name';
+                       $default_attrib['type'][]='V';
+                       $default_attrib['precision'][] =50;
+                       $default_attrib['nullable'][] ='True';
+                       $default_attrib['input_text'][] ='dummy';
+                       $default_attrib['statustext'][] ='dummy';
+                       $default_attrib['attrib_sort'][] ='NULL';
+                       $default_attrib['custom'][] ='NULL';
+
+                       $default_attrib['id'][]= 3;
+                       $default_attrib['column_name'][]= 'entry_date';
+                       $default_attrib['type'][]='I';
+                       $default_attrib['precision'][] =4;
+                       $default_attrib['nullable'][] ='True';
+                       $default_attrib['input_text'][] ='dummy';
+                       $default_attrib['statustext'][] ='dummy';
+                       $default_attrib['attrib_sort'][] ='NULL';
+                       $default_attrib['custom'][] ='NULL';
+
+                       $default_attrib['id'][]= 4;
+                       $default_attrib['column_name'][]= 'category';
+                       $default_attrib['type'][]='I';
+                       $default_attrib['precision'][] =4;
+                       $default_attrib['nullable'][] ='False';
+                       $default_attrib['input_text'][] ='dummy';
+                       $default_attrib['statustext'][] ='dummy';
+                       $default_attrib['attrib_sort'][] ='NULL';
+                       $default_attrib['custom'][] ='NULL';
+
+                       $default_attrib['id'][]= 5;
+                       $default_attrib['column_name'][]= 'user_id';
+                       $default_attrib['type'][]='I';
+                       $default_attrib['precision'][] =4;
+                       $default_attrib['nullable'][] ='False';
+                       $default_attrib['input_text'][] ='dummy';
+                       $default_attrib['statustext'][] ='dummy';
+                       $default_attrib['attrib_sort'][] ='NULL';
+                       $default_attrib['custom'][] ='NULL';
+
+                       $default_attrib['id'][]= 6;
+                       $default_attrib['column_name'][]= 'status';
+                       $default_attrib['type'][]='LB';
+                       $default_attrib['precision'][] = False;
+                       $default_attrib['nullable'][] ='True';
+                       $default_attrib['input_text'][] ='Status';
+                       $default_attrib['statustext'][] ='Status';
+                       $default_attrib['attrib_sort'][] =1;
+                       $default_attrib['custom'][] =1;
+
+                       $default_attrib['id'][]= 7;
+                       $default_attrib['column_name'][]= 'remark';
+                       $default_attrib['type'][]='T';
+                       $default_attrib['precision'][] = False;
+                       $default_attrib['nullable'][] ='False';
+                       $default_attrib['input_text'][] ='Remark';
+                       $default_attrib['statustext'][] ='Remark';
+                       $default_attrib['attrib_sort'][] =2;
+                       $default_attrib['custom'][] =1;
+
+                       $fd=array();
+                       $fd['location_code'] = array('type' => 'varchar', 
'precision' => 25, 'nullable' => False);
+
+                       for ($i=1; $i<$standard['id']+1; $i++)
+                       {
+                               $fd['loc' . $i] = array('type' => 'varchar', 
'precision' => 4, 'nullable' => False);
+                               $pk[$i-1]= 'loc' . $i;
+
+                               $default_attrib['id'][]= $i+7;
+                               $default_attrib['column_name'][]= 'loc' . $i;
+                               $default_attrib['type'][]='V';
+                               $default_attrib['precision'][] =4;
+                               $default_attrib['nullable'][] ='False';
+                               $default_attrib['input_text'][] ='dummy';
+                               $default_attrib['statustext'][] ='dummy';
+                               $default_attrib['attrib_sort'][] ='NULL';
+                               $default_attrib['custom'][] ='NULL';
+                       }
+
+                       $fk_table='fm_location'. ($standard['id']-1);
+
+                       for ($i=1; $i<$standard['id']; $i++)
+                       {
+                               $fk['loc' . $i] = $fk_table . '.loc' . $i;
+                       }
+
+                       if($standard['id']==1)
+                       {
+                               $fd['part_of_town_id'] = array('type' => 'int', 
'precision' => 2, 'nullable' => True);
+                       }
+
+                       $fd['loc' .$standard['id'] . '_name'] = array('type' => 
'varchar', 'precision' => 25, 'nullable' => True);
+                       $fd['entry_date'] = array('type' => 'int', 'precision' 
=> 4, 'nullable' => True);
+                       $fd['category'] = array('type' => 'int', 'precision' => 
4, 'nullable' => True);
+                       $fd['user_id'] = array('type' => 'int', 'precision' => 
4, 'nullable' => True);
+                       $fd['remark'] = array('type' => 'text', 'nullable' => 
True);
+                       $fd['status'] = array('type' => 'int', 'precision' => 
4, 'nullable' => True);
+
+                       $ix = array('location_code');
+                       $uc = array();
+
+                       $fd_history = $fd;
+                       $fd_history['exp_date'] = array('type' => 
'timestamp','nullable' => True,'default' => 'current_timestamp');
+//                     $fd_history['change_type'] = array('type' => 
'int','precision' => '4','nullable' => True);
+
+                       
$add_columns_in_tables=array('fm_project','fm_tts_tickets','fm_request','fm_document','fm_investment');
+
+                       if($this->oProc->CreateTable('fm_location'. 
$standard['id'],array('fd' => $fd,'pk' => $pk,'fk' => $fk,'ix' => $ix,'uc' => 
$uc))
+                               && $this->oProc->CreateTable('fm_location'. 
$standard['id'] . '_history',array('fd' => $fd_history)))
+                       {
+
+                               $this->oProc->CreateTable('fm_location'. 
$standard['id'] . '_category', array(
+                               'fd' => array(
+                                       'id' => array('type' => 
'int','precision' => '4','nullable' => False),
+                                       'descr' => array('type' => 
'varchar','precision' => '50','nullable' => True)
+                               ),
+                               'pk' => array('id'),
+                               'fk' => array(),
+                               'ix' => array(),
+                               'uc' => array()));
+
+                               for ($i=0;$i<count($add_columns_in_tables);$i++)
+                               {
+                                       
$this->oProc->AddColumn($add_columns_in_tables[$i],'loc'. $standard['id'], 
array('type' => 'varchar', 'precision' => 4, 'nullable' => True));
+                               }
+
+                               $values_insert= array(
+                                       $standard['id'],
+                                       $standard['name'],
+                                       $standard['descr'],
+                                   $this->db->db_addslashes(implode(',',$pk)),
+                                   $this->db->db_addslashes(implode(',',$ix)),
+                                   $this->db->db_addslashes(implode(',',$uc)),
+                                       );
+
+                               $values_insert  = 
$this->bocommon->validate_db_insert($values_insert);
+
+                               $this->db->query("INSERT INTO fm_location_type 
(id,name, descr,pk,ix,uc) "
+                                       . "VALUES 
($values_insert)",__LINE__,__FILE__);
+
+                               for ($i=0;$i<count($default_attrib['id']);$i++)
+                               {
+                                       $values_insert= array(
+                                               $standard['id'],
+                                               $default_attrib['id'][$i],
+                                               
$default_attrib['column_name'][$i],
+                                               $default_attrib['type'][$i],
+                                               
$default_attrib['precision'][$i],
+                                               
$default_attrib['input_text'][$i],
+                                               
$default_attrib['statustext'][$i],
+                                               
$default_attrib['attrib_sort'][$i],
+                                               $default_attrib['custom'][$i],
+                                               $default_attrib['nullable'][$i]
+                                               );
+
+
+                                       $values_insert  = 
$this->bocommon->validate_db_insert($values_insert);
+
+                                       $this->db->query("INSERT INTO 
fm_location_attrib 
(type_id,id,column_name,datatype,precision_,input_text,statustext,attrib_sort,custom,nullable)
 "
+                                               . "VALUES 
($values_insert)",__LINE__,__FILE__);
+                               }
+
+                               $type_id=$standard['id'];
+
+                               $this->db->query("INSERT INTO 
fm_location_choice (type_id,attrib_id,id,value) "
+                                       . "VALUES 
($type_id,1,1,'ok')",__LINE__,__FILE__);
+                               $this->db->query("INSERT INTO 
fm_location_choice (type_id,attrib_id,id,value) "
+                                       . "VALUES ($type_id,1,2,'Not 
Active')",__LINE__,__FILE__);
+
+
+                               $receipt['message'][] = array('msg'     => 
lang('table %1 has been saved','fm_location'. $receipt['id'])        );
+                               $this->db->transaction_commit();
+                       }
+                       else
+                       {
+                               $receipt['error'][] = array('msg'       => 
lang('table could not be added')     );
+                               if($this->db->Transaction)
+                               {
+                                       $this->db->transaction_abort();
+                               }
+                               else
+                               {
+                                       $this->db->query("DELETE FROM 
fm_location_type WHERE id='" . $standard['id'] . "'",__LINE__,__FILE__);
+                                       unset($receipt['id']);
+
+                               }
+                       }
+
+                       return $receipt;
+               }
+
+               function edit($standard)
+               {
+
+                       if (!$standard['name'])
+                       {
+                               $receipt['error'][] = array('msg'=>lang('Name 
not entered!'));
+                       }
+
+                       if (!$receipt['error'])
+                       {
+                               $table = 'fm_location_type';
+
+                               $standard['name'] = 
$this->db->db_addslashes($standard['name']);
+                               $standard['descr'] = 
$this->db->db_addslashes($standard['descr']);
+
+                               $this->db->query("UPDATE $table set descr='" . 
$standard['descr'] . "', name='". $standard['name']
+                                       . "' WHERE id='" . $standard['id']. 
"'",__LINE__,__FILE__);
+
+
+                               $receipt['message'][] = array('msg'=> 
lang('Standard has been edited'));
+                       }
+                       else
+                       {
+                               $receipt['error'][] = array('msg'       => 
lang('Standard has NOT been edited'));
+                       }
+
+                       return $receipt;
+               }
+
+               function delete($type_id,$id,$attrib)
+               {
+                       $this->init_process();
+
+                       if($attrib)
+                       {
+                               $table = 'fm_location_attrib';
+
+                               $this->db->query("SELECT column_name,type_id 
FROM fm_location_attrib WHERE id='" . $id . "'",__LINE__,__FILE__);
+                               $this->db->next_record();
+                               $ColumnName             = 
$this->db->f('column_name');
+
+                               $this->oProc->DropColumn('fm_location' 
.$type_id ,'', $ColumnName);
+                               $this->oProc->DropColumn('fm_location' 
.$type_id . '_history','', $ColumnName);
+                       }
+                       else
+                       {
+                               $this->oProc->DropTable('fm_location' . $id);
+                               $this->oProc->DropTable('fm_location' . $id . 
'_category');
+                               $this->oProc->DropTable('fm_location' . $id . 
'_history');
+
+                               $table                  = 'fm_location_type';
+                               $attrib_table   = 'fm_location_attrib';
+                               $this->db->query("DELETE FROM $attrib_table 
WHERE type_id='" . $id . "'",__LINE__,__FILE__);
+                               $this->db->query("DELETE FROM 
fm_location_choice WHERE type_id='" . $id . "'",__LINE__,__FILE__);
+                       }
+                       $this->db->query("DELETE FROM $table WHERE id='" . $id 
. "'",__LINE__,__FILE__);
+               }
+
+               function read_attrib($data)
+               {
+
+//_debug_array($data);
+                       if(is_array($data))
+                       {
+                               if ($data['start'])
+                               {
+                                       $start=$data['start'];
+                               }
+                               else
+                               {
+                                       $start=0;
+                               }
+                               $query = 
(isset($data['query'])?$data['query']:'');
+                               $sort = 
(isset($data['sort'])?$data['sort']:'DESC');
+                               $order = 
(isset($data['order'])?$data['order']:'');
+                               $type_id = 
(isset($data['type_id'])?$data['type_id']:0);
+                               $lookup_type = 
(isset($data['lookup_type'])?$data['lookup_type']:'');
+                               $allrows = 
(isset($data['allrows'])?$data['allrows']:'');
+                       }
+
+                       if ($order)
+                       {
+                               $ordermethod = " order by $order $sort";
+
+                       }
+                       else
+                       {
+                               $ordermethod = ' order by 
fm_location_attrib.attrib_sort asc';
+                       }
+
+                       $table          = 'fm_location_attrib';
+                       $type_table = 'fm_location_type';
+
+                       if ($lookup_type)
+                       {
+                               $filtermethod = " OR (type_id < $lookup_type 
AND lookup_form=1) ";
+
+                       }
+
+                       if($query)
+                       {
+                               $query = ereg_replace("'",'',$query);
+                               $query = ereg_replace('"','',$query);
+
+                               $querymethod = " and ($table.name $this->like 
'%$query%' or $table.descr $this->like '%$query%')";
+                       }
+
+                       $sql = "SELECT 
$table.id,$table.type_id,$table.list,$table.attrib_sort,$table.location_form,lookup_form,$table.column_name,$table.size
 ,statustext,$table.input_text,"
+                               . " $table.datatype ,$type_table.name as type 
FROM $type_table $this->join $table on $table.type_id = $type_table.id "
+                               . " WHERE $table.type_id= '$type_id' AND custom 
= 1 $filtermethod $querymethod";
+
+                       $this->db2->query($sql,__LINE__,__FILE__);
+                       $this->total_records = $this->db2->num_rows();
+                       if(!$allrows)
+                       {
+                               $this->db->limit_query($sql . 
$ordermethod,$start,__LINE__,__FILE__);
+                       }
+                       else
+                       {
+                               $this->db->query($sql . 
$ordermethod,__LINE__,__FILE__);
+                       }
+
+                       while ($this->db->next_record())
+                       {
+                               $attrib[] = array
+                               (
+                                       'id'                    => 
$this->db->f('id'),
+                                       'location_type' => 
$this->db->f('type_id'),
+                                       'attrib_sort'   => 
$this->db->f('attrib_sort'),
+                                       'list'                  => 
$this->db->f('list'),
+                                       'lookup_form'   => 
$this->db->f('lookup_form'),
+                                       'location_form' => 
$this->db->f('location_form'),
+                                       'column_name'   => 
$this->db->f('column_name'),
+                                       'size'                  => 
$this->db->f('size'),
+                                       'statustext'    => 
$this->db->f('statustext'),
+                                       'input_text'    => 
$this->db->f('input_text'),
+                                       'type_name'             => 
$this->db->f('type'),
+                                       'datatype'              => 
$this->db->f('datatype')
+                               );
+                       }
+                       return $attrib;
+               }
+
+               function read_single_attrib($type_id,$id)
+               {
+
+                       $table          = 'fm_location_attrib';
+                       $type_table = 'fm_location_type';
+
+                       $sql = "SELECT $table.* ,$type_table.name as type_name 
FROM $type_table $this->join $table on $table.type_id = $type_id where 
$table.id= '$id'";
+
+                       $this->db->query($sql,__LINE__,__FILE__);
+
+                       if ($this->db->next_record())
+                       {
+                               $attrib['id']                                   
        = $this->db->f('id');
+                               $attrib['column_name']                          
= $this->db->f('column_name');
+                               $attrib['input_text']                           
= $this->db->f('input_text');
+                               $attrib['statustext']                           
= $this->db->f('statustext');
+                               $attrib['column_info']['precision']     = 
$this->db->f('precision_');
+                               $attrib['column_info']['scale']         = 
$this->db->f('scale');
+                               $attrib['column_info']['default']       = 
$this->db->f('default_value');
+                               $attrib['column_info']['nullable']      = 
$this->db->f('nullable');
+                               $attrib['column_info']['type']          = 
$this->db->f('datatype');
+                               $attrib['type_id']                              
        = $type_id;
+                               $attrib['type_name']                            
= $this->db->f('type_name');
+                               $attrib['lookup_form']                          
= $this->db->f('lookup_form');
+                               $attrib['list']                                 
        = $this->db->f('list');
+                               if($this->db->f('datatype')=='R' || 
$this->db->f('datatype')=='CH' || $this->db->f('datatype')=='LB')
+                               {
+                                       $attrib['choice'] = 
$this->read_attrib_choice($type_id,$id);
+                               }
+
+                               return $attrib;
+                       }
+               }
+
+               function read_attrib_choice($type_id,$attrib_id)
+               {
+                       $choice_table = 'fm_location_choice';
+                       $sql = "SELECT * FROM $choice_table WHERE 
type_id=$type_id AND attrib_id=$attrib_id";
+                       $this->db->query($sql,__LINE__,__FILE__);
+
+                       while ($this->db->next_record())
+                       {
+                               $choice[] = array
+                               (
+                                       'id'    => $this->db->f('id'),
+                                       'value' => $this->db->f('value')
+                               );
+                       }
+                       return $choice;
+               }
+
+               function add_attrib($attrib)
+               {
+                       $attrib['column_name'] = 
$this->db->db_addslashes($attrib['column_name']);
+                       $attrib['input_text'] = 
$this->db->db_addslashes($attrib['input_text']);
+                       $attrib['statustext'] = 
$this->db->db_addslashes($attrib['statustext']);
+                       $attrib['default'] = 
$this->db->db_addslashes($attrib['default']);
+                       $attrib['id'] = 
$this->bocommon->next_id('fm_location_attrib',array('type_id'=>$attrib['type_id']));
+
+                       $sql = "SELECT max(attrib_sort) as max_sort FROM 
fm_location_attrib where type_id=" . $attrib['type_id'];
+                       $this->db->query($sql,__LINE__,__FILE__);
+                       $this->db->next_record();
+                       $attrib_sort    = $this->db->f('max_sort')+1;
+
+                       if($precision = 
$this->bocommon->translate_datatype_precision($attrib['column_info']['type']))
+                       {
+                               $attrib['column_info']['precision']=$precision;
+                       }
+
+                       $values= array(
+                               $attrib['id'],
+                               $attrib['column_name'],
+                               $attrib['input_text'],
+                               $attrib['statustext'],
+                               $attrib['type_id'],
+                               $attrib['lookup_form'],
+                               $attrib['list'],
+                               $attrib_sort,
+                               $attrib['column_info']['type'],
+                               $attrib['column_info']['precision'],
+                               $attrib['column_info']['scale'],
+                               $attrib['column_info']['default'],
+                               $attrib['column_info']['nullable'],
+                               1
+                               );
+
+                       $values = $this->bocommon->validate_db_insert($values);
+
+                       $this->db->transaction_begin();
+
+                       $this->db->query("INSERT INTO fm_location_attrib 
(id,column_name, input_text, statustext, 
type_id,lookup_form,list,attrib_sort,datatype,precision_,scale,default_value,nullable,custom)
 "
+                               . "VALUES ($values)",__LINE__,__FILE__);
+
+                       $receipt['id']= $attrib['id'];
+
+                       $attrib['column_info']['type']  = 
$this->bocommon->translate_datatype_insert($attrib['column_info']['type']);
+
+                       if(!$attrib['column_info']['default'])
+                       {
+                               unset($attrib['column_info']['default']);
+                       }
+
+                       $this->init_process();
+
+                       
if($this->oProc->AddColumn('fm_location'.$attrib['type_id'],$attrib['column_name'],
 $attrib['column_info'])
+                               && 
$this->oProc->AddColumn('fm_location'.$attrib['type_id'] 
.'_history',$attrib['column_name'], $attrib['column_info']))
+                       {
+                               $receipt['message'][] = array('msg'     => 
lang('Attribute has been saved')     );
+                               $this->db->transaction_commit();
+
+                       }
+                       else
+                       {
+                               $receipt['error'][] = array('msg'       => 
lang('column could not be added')    );
+                               if($this->db->Transaction)
+                               {
+                                       $this->db->transaction_abort();
+                               }
+                               else
+                               {
+                                       $this->db->query("DELETE FROM 
fm_location_attrib WHERE id=" . $receipt['id'] . " AND type_id=" . 
$attrib['type_id'],__LINE__,__FILE__);
+                                       unset($receipt['id']);
+
+                               }
+                       }
+
+                       return $receipt;
+               }
+
+               function init_process()
+               {
+                       $this->oProc                                            
= 
CreateObject('phpgwapi.schema_proc',$GLOBALS['phpgw_info']['server']['db_type']);
+                       $this->oProc->m_odb                                     
= $this->db;
+                       $this->oProc->m_odb->Halt_On_Error      = 'report';
+               }
+
+
+               function save_config($values='',$column_name='')
+               {
+
+                       $this->db->query("SELECT * FROM fm_location_config  
WHERE column_name='$column_name' ",__LINE__,__FILE__);
+                       $this->db->next_record();
+
+                       $column_info['type']            = 
$this->db->f('datatype');
+                       $column_info['precision']       = 
$this->db->f('precision_');
+                       $column_info['scale']           = $this->db->f('scale');
+                       $column_info['default']         = 
$this->db->f('default_value');
+                       $column_info['nullable']        = 
$this->db->f('nullable');
+                       $location_type                          = 
$this->db->f('location_type');
+
+                       if(!($location_type==$values[$column_name]))
+                       {
+                               $this->init_process();
+
+                               
if($this->oProc->AddColumn('fm_location'.$values[$column_name],$column_name, 
$column_info))
+                               {
+                                       if($column_name=='street_id')
+                                       {
+                                               
$this->oProc->AddColumn('fm_location'.$values[$column_name],'street_number', 
array('type'=>'varchar','precision'=>10));
+                                               
$this->oProc->DropColumn('fm_location' .$location_type ,'','street_number');
+                                       }
+
+                                       $this->oProc->DropColumn('fm_location' 
.$location_type ,'', $column_name);
+                                       $this->oProc->DropColumn('fm_location' 
.$location_type . ' _history','', $column_name);
+
+
+                                       $this->db->query("UPDATE 
fm_location_config set
+                                               location_type           = '". 
$values[$column_name]     . "' WHERE column_name='" . $column_name . 
"'",__LINE__,__FILE__);
+
+                                       $receipt['message'][] = array('msg'     
=> lang('column %1 has been moved',$column_name));
+                               }
+                               else
+                               {
+                                       $receipt['message'][] = array('msg'     
=> lang('column %1 could not be moved',$column_name));
+
+                               }
+                       }
+
+                       return $receipt;
+               }
+
+
+               function edit_attrib($attrib)
+               {
+                       $attrib['column_name'] = 
$this->db->db_addslashes($attrib['column_name']);
+                       $attrib['input_text'] = 
$this->db->db_addslashes($attrib['input_text']);
+                       $attrib['statustext'] = 
$this->db->db_addslashes($attrib['statustext']);
+                       $attrib['default'] = 
$this->db->db_addslashes($attrib['default']);
+
+                       $this->db->query("SELECT * FROM fm_location_attrib 
WHERE type_id = " . $attrib['type_id'] ." AND id=" . 
$attrib['id'],__LINE__,__FILE__);
+                       $this->db->next_record();
+                       $OldColumnName          = $this->db->f('column_name');
+                       $OldColumnType          = $this->db->f('datatype');
+                       $OldColumnPrecision     = $this->db->f('precision_');
+
+                       $table_def = $this->get_table_def($attrib['type_id']);
+                       $history_table_def['fm_location'.$attrib['type_id'] . 
'_history'] = $table_def['fm_location'.$attrib['type_id']];
+                       
$history_table_def['fm_location'.$attrib['type_id']]['fd']['exp_date']=array('type'
 => 'timestamp','nullable' => False,'default' => 'current_timestamp');
+//                     
$history_table_def['fm_location'.$attrib['type_id']]['fd']['change_type'] = 
array('type' => 'int','precision' => '4','nullable' => True);
+
+                       
unset($history_table_def['fm_location'.$attrib['type_id']]['pk']);
+                       
unset($history_table_def['fm_location'.$attrib['type_id']]['fk']);
+                       
unset($history_table_def['fm_location'.$attrib['type_id']]['ix']);
+                       
unset($history_table_def['fm_location'.$attrib['type_id']]['uc']);
+
+                       if($this->receipt['error'])
+                       {
+                               return $this->receipt;
+                       }
+
+                       $this->db->transaction_begin();
+
+                       $value_set=array(
+                               'input_text'    => $attrib['input_text'],
+                               'statustext'    => $attrib['statustext'],
+                               'list'                  => $attrib['list'],
+                               'lookup_form'   => $attrib['lookup_form'],
+                               );
+
+                       $value_set      = 
$this->bocommon->validate_db_update($value_set);
+
+                       $this->db->query("UPDATE fm_location_attrib set 
$value_set WHERE type_id = " . $attrib['type_id'] ." AND id=" . 
$attrib['id'],__LINE__,__FILE__);
+
+                       $this->init_process();
+
+                       if($OldColumnName !=$attrib['column_name'])
+                       {
+                               $value_set=array(
+                                       'column_name'           => 
$attrib['column_name']
+                                       );
+
+                               $value_set      = 
$this->bocommon->validate_db_update($value_set);
+
+                               $this->db->query("UPDATE fm_location_attrib set 
$value_set WHERE type_id = " . $attrib['type_id'] ." AND id=" . 
$attrib['id'],__LINE__,__FILE__);
+
+                               $this->oProc->m_aTables = $table_def;
+                               
$this->oProc->RenameColumn('fm_location'.$attrib['type_id'], $OldColumnName, 
$attrib['column_name']);
+                               $this->oProc->m_aTables = $history_table_def;
+                               
$this->oProc->RenameColumn('fm_location'.$attrib['type_id'] . '_history', 
$OldColumnName, $attrib['column_name']);
+                       }
+
+                       if($OldColumnType !=$attrib['column_info']['type'])
+                       {
+                               if($precision = 
$this->bocommon->translate_datatype_precision($attrib['column_info']['type']))
+                               {
+                                       
$attrib['column_info']['precision']=$precision;
+                               }
+
+                               if(!$attrib['column_info']['default'])
+                               {
+                                       
unset($attrib['column_info']['default']);
+                               }
+
+
+                               $value_set=array(
+                                       'type_id'                       => 
$attrib['type_id'],
+                                       'datatype'                      => 
$attrib['column_info']['type'],
+                                       'precision_'            => 
$attrib['column_info']['precision'],
+                                       'scale'                         => 
$attrib['column_info']['scale'],
+                                       'default_value'         => 
$attrib['column_info']['default'],
+                                       'nullable'                      => 
$attrib['column_info']['nullable']
+                                       );
+
+                               $value_set      = 
$this->bocommon->validate_db_update($value_set);
+
+                               $this->db->query("UPDATE fm_location_attrib set 
$value_set WHERE type_id = " . $attrib['type_id'] ." AND id=" . 
$attrib['id'],__LINE__,__FILE__);
+
+                               $attrib['column_info']['type']  = 
$this->bocommon->translate_datatype_insert($attrib['column_info']['type']);
+
+                               $this->oProc->m_aTables = $table_def;
+                               
$this->oProc->AlterColumn('fm_location'.$attrib['type_id'],$attrib['column_name'],$attrib['column_info']);
+                               $this->oProc->m_aTables = $history_table_def;
+                               
$this->oProc->AlterColumn('fm_location'.$attrib['type_id'] . 
'_history',$attrib['column_name'],$attrib['column_info']);
+                       }
+
+                       $choice_table ='fm_location_choice';
+
+                       if($attrib['new_choice'])
+                       {
+                               $choice_id = 
$this->bocommon->next_id($choice_table 
,array('type_id'=>$attrib['type_id'],'attrib_id'=>$attrib['id']));
+
+                               $values= array(
+                                       $attrib['type_id'],
+                                       $attrib['id'],
+                                       $choice_id,
+                                       $attrib['new_choice']
+                                       );
+
+                               $values = 
$this->bocommon->validate_db_insert($values);
+
+                               $this->db->query("INSERT INTO $choice_table 
(type_id,attrib_id,id,value) "
+                               . "VALUES ($values)",__LINE__,__FILE__);
+                       }
+
+                       if($attrib['delete_choice'])
+                       {
+                               for 
($i=0;$i<count($attrib['delete_choice']);$i++)
+                               {
+                                       $this->db->query("DELETE FROM 
$choice_table WHERE type_id=" . $attrib['type_id']. " AND attrib_id=" . 
$attrib['id']  ." AND id=" . $attrib['delete_choice'][$i],__LINE__,__FILE__);
+                               }
+                       }
+
+                       if($attrib['column_info']['type']!='R' && 
$attrib['column_info']['type']!='CH' && $attrib['column_info']['type']!='LB')
+                       {
+                               $this->db->query("DELETE FROM $choice_table 
WHERE type_id=" . $attrib['type_id']. " AND attrib_id=" . 
$attrib['id'],__LINE__,__FILE__);
+                       }
+
+                       $this->db->transaction_commit();
+
+                       $receipt['message'][] = array('msg'     => 
lang('Attribute has been edited'));
+
+                       return $receipt;
+
+               }
+
+               function get_table_def($id='')
+               {
+
+                       $metadata = $this->db->metadata('fm_location'.$id);
+//     _debug_array($metadata);
+
+                       for ($i=0; $i<count($metadata); $i++)
+                       {
+                               $sql = "SELECT * FROM fm_location_attrib WHERE 
type_id=$id AND column_name = '" . $metadata[$i]['name'] . "'";
+//     _debug_array($sql);
+
+                               $this->db->query($sql,__LINE__,__FILE__);
+                               if($this->db->next_record())
+                               {
+
+                                       if(!$precision = 
$this->db->f('precision_'))
+                                       {
+                                               $precision = 
$this->bocommon->translate_datatype_precision($this->db->f('datatype'));
+                                       }
+
+                                       $fd[$metadata[$i]['name']] = array(
+                                                       'type' => 
$this->bocommon->translate_datatype_insert(stripslashes($this->db->f('datatype'))),
+                                                       'precision' => 
$precision,
+                                                       'nullable' => 
stripslashes($this->db->f('nullable')),
+                                                       'default' => 
stripslashes($this->db->f('default_value')),
+                                                       'scale' => 
$this->db->f('scale')
+                                                       );
+                                       unset($precision);
+                               }
+                               else
+                               {
+                                       $this->receipt['error'][] = array('msg' 
=> lang('Column %1 is missing from metadata',$metadata[$i]['name']));
+                               }
+                       }
+
+                       $this->db->query("SELECT * FROM fm_location_type WHERE 
id=$id");
+                       $this->db->next_record();
+
+
+                       if($id>1)
+                       {
+                               $fk_table='fm_location'. ($id-1);
+                               for ($i=1; $i<$id; $i++)
+                               {
+                                       $fk['loc' . $i] = $fk_table . '.loc' . 
$i;
+                               }
+                       }
+
+                       $table_def = array(
+                               'fm_location'.$id =>    array(
+                                       'fd' => $fd
+                                       )
+                               );
+
+                       if($this->db->f('pk'))
+                       {
+                               $table_def['fm_location'.$id]['pk'] = 
explode(',',$this->db->f('pk'));
+                       }
+                       if($fk)
+                       {
+                               $table_def['fm_location'.$id]['fk'] = $fk;
+                       }
+                       if($this->db->f('ix'))
+                       {
+                               $table_def['fm_location'.$id]['ix'] = 
explode(',',$this->db->f('ix'));
+                       }
+                       if($this->db->f('uc'))
+                       {
+                               $table_def['fm_location'.$id]['uc'] = 
explode(',',$this->db->f('uc'));
+                       }
+
+
+
+                       return $table_def;
+               }
+
+
+               function select_location_type()
+               {
+                       $this->db->query("SELECT * FROM fm_location_type ORDER 
BY id ");
+
+                       $i = 0;
+                       while ($this->db->next_record())
+                       {
+                               $location_type[$i]['id']                = 
$this->db->f('id');
+                               $location_type[$i]['name']              = 
stripslashes($this->db->f('name'));
+                               $location_type[$i]['descr']     = 
stripslashes($this->db->f('descr'));
+                               $i++;
+                       }
+//_debug_array($location_type);
+
+                       return $location_type;
+               }
+
+               function resort_attrib($data)
+               {
+                       if(is_array($data))
+                       {
+                               $resort = 
(isset($data['resort'])?$data['resort']:'up');
+                               $type_id = 
(isset($data['type_id'])?$data['type_id']:'');
+                               $id = (isset($data['id'])?$data['id']:'');
+                       }
+
+                       if(!$type_id)
+                       {
+                               return;
+                       }
+
+                       $sql = "SELECT attrib_sort FROM fm_location_attrib 
where type_id=$type_id AND id=$id";
+                       $this->db->query($sql,__LINE__,__FILE__);
+                       $this->db->next_record();
+                       $attrib_sort    = $this->db->f('attrib_sort');
+                       $sql2 = "SELECT max(attrib_sort) as max_sort FROM 
fm_location_attrib where type_id=$type_id";
+                       $this->db->query($sql2,__LINE__,__FILE__);
+                       $this->db->next_record();
+                       $max_sort       = $this->db->f('max_sort');
+
+                       switch($resort)
+                       {
+                               case 'up':
+                                       if($attrib_sort>1)
+                                       {
+                                               $sql = "UPDATE 
fm_location_attrib set attrib_sort=$attrib_sort WHERE type_id=$type_id AND 
attrib_sort =" . ($attrib_sort-1);
+                                               
$this->db->query($sql,__LINE__,__FILE__);
+                                               $sql = "UPDATE 
fm_location_attrib set attrib_sort=" . ($attrib_sort-1) ." WHERE 
type_id=$type_id AND id=$id";
+                                               
$this->db->query($sql,__LINE__,__FILE__);
+                                       }
+                                       break;
+                               case 'down':
+                                       if($max_sort > $attrib_sort)
+                                       {
+                                               $sql = "UPDATE 
fm_location_attrib set attrib_sort=$attrib_sort WHERE type_id=$type_id AND 
attrib_sort =" . ($attrib_sort+1);
+                                               
$this->db->query($sql,__LINE__,__FILE__);
+                                               $sql = "UPDATE 
fm_location_attrib set attrib_sort=" . ($attrib_sort+1) ." WHERE 
type_id=$type_id AND id=$id";
+                                               
$this->db->query($sql,__LINE__,__FILE__);
+                                       }
+                                       break;
+                               default:
+                                       return;
+                                       break;
+                       }
+               }
+       }
+?>
Index: property/inc/class.soagreement.inc.php
diff -u property/inc/class.soagreement.inc.php:1.9 
property/inc/class.soagreement.inc.php:1.10
--- property/inc/class.soagreement.inc.php:1.9  Fri Jan 27 14:05:43 2006
+++ property/inc/class.soagreement.inc.php      Mon Jan 30 22:14:19 2006
@@ -1,1569 +1,1570 @@
-<?php
-       /**
-       * phpGroupWare - property: a Facilities Management System.
-       *
-       * @author Sigurd Nes <address@hidden>
-       * @copyright Copyright (C) 2003-2005 Free Software Foundation, Inc. 
http://www.fsf.org/
-       * @license http://www.gnu.org/licenses/gpl.html GNU General Public 
License
-       * @internal Development of this application was funded by 
http://www.bergen.kommune.no/bbb_/ekstern/
-       * @package property
-       * @subpackage agreement
-       * @version $Id: class.soagreement.inc.php,v 1.9 2006/01/27 14:05:43 
sigurdne Exp $
-       */
-
-       /**
-        * Description
-        * @package property
-        */
-
-       class soagreement
-       {
-               var $role;
-
-               function soagreement()
-               {
-                       $this->currentapp       = 
$GLOBALS['phpgw_info']['flags']['currentapp'];
-                       $this->account  = 
$GLOBALS['phpgw_info']['user']['account_id'];
-                       $this->bocommon         = 
CreateObject($this->currentapp.'.bocommon');
+<?php
+       /**
+       * phpGroupWare - property: a Facilities Management System.
+       *
+       * @author Sigurd Nes <address@hidden>
+       * @copyright Copyright (C) 2003-2005 Free Software Foundation, Inc. 
http://www.fsf.org/
+       * @license http://www.gnu.org/licenses/gpl.html GNU General Public 
License
+       * @internal Development of this application was funded by 
http://www.bergen.kommune.no/bbb_/ekstern/
+       * @package property
+       * @subpackage agreement
+       * @version $Id: class.soagreement.inc.php,v 1.10 2006/01/30 22:14:19 
sigurdne Exp $
+       */
+
+       /**
+        * Description
+        * @package property
+        */
+
+       class soagreement
+       {
+               var $role;
+
+               function soagreement()
+               {
+                       $this->currentapp       = 
$GLOBALS['phpgw_info']['flags']['currentapp'];
+                       $this->account  = 
$GLOBALS['phpgw_info']['user']['account_id'];
+                       $this->bocommon         = 
CreateObject($this->currentapp.'.bocommon');
                        $this->db               = $this->bocommon->new_db();
-                       $this->db2              = $this->bocommon->new_db();
-                       $this->join                     = $this->bocommon->join;
-                       $this->left_join        = $this->bocommon->left_join;
-                       $this->like                     = $this->bocommon->like;
-//                     $this->role             = 'agreement';
-               }
-
-               function select_category_list()
-               {
-                       $table = 'fm_branch';
-                       $this->db->query("SELECT id,descr FROM $table  ORDER BY 
descr ");
-
-                       $i = 0;
-                       while ($this->db->next_record())
-                       {
-                               $categories[$i]['id']                           
= $this->db->f('id');
-                               $categories[$i]['name']                         
= stripslashes($this->db->f('descr'));
-                               $i++;
-                       }
-                       return $categories;
-               }
-
-               function select_vendor_list()
-               {
-                       $table = 'fm_agreement';
-                       $this->db->query("SELECT vendor_id,org_name FROM $table 
$this->join fm_vendor on fm_agreement.vendor_id=fm_vendor.id GROUP BY 
org_name,vendor_id ");
-
-                       $i = 0;
-                       while ($this->db->next_record())
-                       {
-                               $vendor[$i]['id']                               
= $this->db->f('vendor_id');
-                               $vendor[$i]['name']                             
= stripslashes($this->db->f('org_name'));
-                               $i++;
-                       }
-                       return $vendor;
-               }
-
-               function read($data)
-               {
-                       if(is_array($data))
-                       {
-                               $start                  = 
(isset($data['start'])?$data['start']:0);
-                               $filter                 = 
(isset($data['filter'])?$data['filter']:'none');
-                               $query                  = 
(isset($data['query'])?$data['query']:'');
-                               $sort                   = 
(isset($data['sort'])?$data['sort']:'DESC');
-                               $order                  = 
(isset($data['order'])?$data['order']:'');
-                               $cat_id                 = 
(isset($data['cat_id'])?$data['cat_id']:'');
-                               $vendor_id              = 
(isset($data['vendor_id'])?$data['vendor_id']:'');
-                               $allrows                = 
(isset($data['allrows'])?$data['allrows']:'');
-                               $member_id              = 
(isset($data['member_id'])?$data['member_id']:0);
-                               $agreement_id   = 
(isset($data['agreement_id'])?$data['agreement_id']:'');
-                       }
-
-                       $choice_table = 'fm_agreement_choice';
-                       $attribute_table = 'fm_agreement_attribute';
-
-                       $entity_table = 'fm_agreement';
-                       $category_table = 'fm_branch';
-                       $attribute_filter = " AND attrib_detail = 1";
-                       $paranthesis .='(';
-                       $joinmethod .= " $this->join $category_table ON ( 
$entity_table.category =$category_table.id)";
-                       $joinmethod .= " $this->join  fm_vendor ON ( 
$entity_table.vendor_id =fm_vendor.id ))";
-
-                       $cols = $entity_table . ".*,$category_table.descr as 
category, org_name";
-
-                       $cols_return[]                          = 'id';
-                       $uicols['input_type'][]         = 'text';
-                       $uicols['name'][]                       = 'id';
-                       $uicols['descr'][]                      = lang('ID');
-                       $uicols['statustext'][]         = lang('ID');
-
-
-                       $cols_return[]                          = 'name';
-                       $uicols['input_type'][]         = 'text';
-                       $uicols['name'][]                       = 'name';
-                       $uicols['descr'][]                      = lang('name');
-                       $uicols['statustext'][]         = lang('name');
-
-                       $cols_return[]                          = 'org_name';
-                       $uicols['input_type'][]         = 'text';
-                       $uicols['name'][]                       = 'org_name';
-                       $uicols['descr'][]                      = 
lang('Vendor');
-                       $uicols['statustext'][]         = lang('Vendor');
-
-                       $cols_return[]                          = 'category';
-                       $uicols['input_type'][]         = 'text';
-                       $uicols['name'][]                       = 'category';
-                       $uicols['descr'][]                      = 
lang('category');
-                       $uicols['statustext'][]         = lang('category');
-
-                       $cols_return[]                          = 'start_date';
-                       $uicols['input_type'][]         = 'text';
-                       $uicols['name'][]                       = 'start_date';
-                       $uicols['descr'][]                      = lang('start');
-                       $uicols['statustext'][]         = lang('start date');
-
-                       $cols_return[]                          = 'end_date';
-                       $uicols['input_type'][]         = 'text';
-                       $uicols['name'][]                       = 'end_date';
-                       $uicols['descr'][]                      = lang('end');
-                       $uicols['statustext'][]         = lang('end date');
-
-                       $cols_return[]                          = 'status';
-                       $uicols['input_type'][]         = 'text';
-                       $uicols['name'][]                       = 'status';
-                       $uicols['descr'][]                      = 
lang('status');
-                       $uicols['statustext'][]         = lang('status');
-
-                       if ($order)
-                       {
-                               if ($order=='id')
-                               {
-                                       $ordermethod = " order by 
$entity_table.$order $sort";
-                               }
-                               else
-                               {
-                                       $ordermethod = " order by $order $sort";
-                               }
-                       }
-                       else
-                       {
-                               $ordermethod = " order by $entity_table.id 
DESC";
-                       }
-
-
-                       $from .= " FROM $paranthesis $entity_table ";
-
-                       $sql = "SELECT $cols $from $joinmethod";
-
-                       $i      = count($uicols['name']);
-
-                       $this->db->query("SELECT * FROM $attribute_table WHERE 
list=1 $attribute_filter ");
-                       while ($this->db->next_record())
-                       {
-                               $uicols['input_type'][]         = 'text';
-                               $uicols['name'][]                       = 
$this->db->f('column_name');
-                               $uicols['descr'][]                      = 
$this->db->f('input_text');
-                               $uicols['statustext'][]         = 
$this->db->f('statustext');
-                               $uicols['datatype'][$i]         = 
$this->db->f('datatype');
-                               $cols_return_extra[]= array(
-                                       'name'  => $this->db->f('column_name'),
-                                       'datatype'      => 
$this->db->f('datatype'),
-                                       'attrib_id'     => $this->db->f('id')
-                               );
-
-                               $i++;
-                       }
-
-                       
$user_columns=$GLOBALS['phpgw_info']['user']['preferences'][$this->currentapp]['agreement_columns'
 . !!$agreement_id];
-
-//_debug_array($user_columns);
-
-                       if (isset($user_columns) AND is_array($user_columns) 
AND $user_columns[0])
-                       {
-                               foreach($user_columns as $column_id)
-                               {
-                                       $this->db->query("SELECT * FROM 
$attribute_table WHERE id= $column_id");
-
-                                       $this->db->next_record();
-                                       $uicols['input_type'][]         = 
'text';
-                                       $uicols['name'][]                       
= $this->db->f('column_name');
-                                       $uicols['descr'][]                      
= $this->db->f('input_text');
-                                       $uicols['statustext'][]         = 
$this->db->f('statustext');
-                                       $uicols['datatype'][$i]         = 
$this->db->f('datatype');
-                                       $cols_return_extra[]= array(
-                                               'name'  => 
$this->db->f('column_name'),
-                                               'datatype'      => 
$this->db->f('datatype'),
-                                               'attrib_id'     => 
$this->db->f('id')
-                                       );
-                                       $i++;
-                               }
-                       }
-
-                       $this->uicols   = $uicols;
-
-//_debug_array($cols_return_extra);
-
-                       $where= 'WHERE';
-
-/*                     if ($filter=='all')
-                       {
-                               if (is_array($grants))
-                               {
-                                       while (list($user) = each($grants))
-                                       {
-                                               $public_user_list[] = $user;
-                                       }
-                                       reset($public_user_list);
-                                       $filtermethod .= " $where ( 
$entity_table.user_id IN(" . implode(',',$public_user_list) . "))";
-
-                                       $where= 'AND';
-                               }
-
-                       }
-                       else
-                       {
-                               $filtermethod = " $where 
$entity_table.user_id=$filter ";
-                               $where= 'AND';
-                       }
-*/
-
-                       if ($agreement_id)
-                       {
-                               $filtermethod .= " $where 
$entity_table.agreement_id=$agreement_id";
-                               $filtermethod .= " AND current_index = 1";
-                               $where= 'AND';
-                       }
-
-                       if ($cat_id)
-                       {
-                               $filtermethod .= " $where 
$entity_table.category='$cat_id' ";
-                               $where= 'AND';
-                       }
-
-                       if ($vendor_id)
-                       {
-                               $filtermethod .= " $where 
$entity_table.vendor_id='$vendor_id' ";
-                               $where= 'AND';
-                       }
-
-                       if ($member_id>0)
-                       {
-                               $filtermethod .= " $where fm_vendor.member_of 
$this->like '%,$member_id,%' ";
-                               $where= 'AND';
-                       }
-
-                       if ($status)
-                       {
-                               $filtermethod .= " $where 
$entity_table.status='$status' ";
-                               $where= 'AND';
-                       }
-
-
-                       if($query)
-                       {
-                               $query = ereg_replace("'",'',$query);
-                               $query = ereg_replace('"','',$query);
-
-                               $this->db->query("SELECT * FROM 
$attribute_table where search='1'");
-
-                               while ($this->db->next_record())
-                               {
-                                       if($this->db->f('datatype')=='V' || 
$this->db->f('datatype')=='email' || $this->db->f('datatype')=='CH')
-                                       {
-                                               $querymethod[]= 
"$entity_table." . $this->db->f('column_name') . " $this->like '%$query%'";
-                                       }
-                                       else
-                                       {
-                                               $querymethod[]= 
"$entity_table." . $this->db->f('column_name') . " = '$query'";
-                                       }
-                               }
-
-                               if (isset($querymethod) AND 
is_array($querymethod))
-                               {
-                                       $querymethod = " $where (" . implode (' 
OR ',$querymethod) . ')';
-                                       $where = 'AND';
-                               }
-                       }
-
-                       $sql .= " $filtermethod $querymethod";
-//echo $sql;
-
-                       $this->db2->query($sql,__LINE__,__FILE__);
-                       $this->total_records = $this->db2->num_rows();
-                       if(!$allrows)
-                       {
-                               $this->db->limit_query($sql . 
$ordermethod,$start,__LINE__,__FILE__);
-                       }
-                       else
-                       {
-                               $this->db->query($sql . 
$ordermethod,__LINE__,__FILE__);
-                       }
-
-                       $j=0;
-                       $n=count($cols_return);
-//_debug_array($cols_return);
-
-                       $contacts                       = 
CreateObject('phpgwapi.contacts');
-
-                       while ($this->db->next_record())
-                       {
-                               for ($i=0;$i<$n;$i++)
-                               {
-                                       $agreement_list[$j][$cols_return[$i]] = 
$this->db->f($cols_return[$i]);
-                                       $agreement_list[$j]['grants'] = 
(int)$grants[$this->db->f('user_id')];
-                               }
-
-                               for ($i=0;$i<count($cols_return_extra);$i++)
-                               {
-                                       $value='';
-                                       
$value=$this->db->f($cols_return_extra[$i]['name']);
-
-                                       
if(($cols_return_extra[$i]['datatype']=='R' || 
$cols_return_extra[$i]['datatype']=='LB') && $value):
-                                       {
-                                               $sql="SELECT value FROM 
$choice_table where attrib_id=" .$cols_return_extra[$i]['attrib_id']. "  AND 
id=" . $value . $attribute_filter;
-                                               $this->db2->query($sql);
-                                               $this->db2->next_record();
-                                               
$agreement_list[$j][$cols_return_extra[$i]['name']] = $this->db2->f('value');
-                                       }
-                                       
elseif($cols_return_extra[$i]['datatype']=='AB' && $value):
-                                       {
-                                               $contact_data   = 
$contacts->read_single_entry($value,array('n_given'=>'n_given','n_family'=>'n_family','email'=>'email'));
-                                               
$agreement_list[$j][$cols_return_extra[$i]['name']]     = 
$contact_data[0]['n_family'] . ', ' . $contact_data[0]['n_given'];
-                                       }
-                                       
elseif($cols_return_extra[$i]['datatype']=='VENDOR' && $value):
-                                       {
-                                               $sql="SELECT org_name FROM 
fm_vendor where id=$value";
-                                               $this->db2->query($sql);
-                                               $this->db2->next_record();
-                                               
$agreement_list[$j][$cols_return_extra[$i]['name']] = $this->db2->f('org_name');
-
-                                       }
-                                       
elseif($cols_return_extra[$i]['datatype']=='CH' && $value):
-                                       {
-                                               $ch= unserialize($value);
-
-                                               if (isset($ch) AND 
is_array($ch))
-                                               {
-                                                       for 
($k=0;$k<count($ch);$k++)
-                                                       {
-                                                               $sql="SELECT 
value FROM $choice_table where attrib_id=" 
.$cols_return_extra[$i]['attrib_id']. "  AND id=" . $ch[$k] . $attribute_filter;
-                                                               
$this->db2->query($sql);
-                                                               while 
($this->db2->next_record())
-                                                               {
-                                                                       
$ch_value[]=$this->db2->f('value');
-                                                               }
-                                                       }
-                                                       
$agreement_list[$j][$cols_return_extra[$i]['name']] = @implode(",", $ch_value);
-                                                       unset($ch_value);
-                                               }
-                                       }
-                                       
elseif($cols_return_extra[$i]['datatype']=='D' && $value):
-                                       {
-//_debug_array($value);
-
-                                               
$agreement_list[$j][$cols_return_extra[$i]['name']]=date($GLOBALS['phpgw_info']['user']['preferences']['common']['dateformat'],strtotime($value));
-                                       }
-                                       else:
-                                       {
-                                               
$agreement_list[$j][$cols_return_extra[$i]['name']]=$value;
-                                       }
-                                       endif;
-                               }
-
-
-                               $j++;
-                       }
-//_debug_array($agreement_list);
-                       return $agreement_list;
-               }
-
-               function read_details($data)
-               {
-                       if(is_array($data))
-                       {
-                               $start                  = 
(isset($data['start'])?$data['start']:0);
-                               $filter                 = 
(isset($data['filter'])?$data['filter']:'none');
-                               $query                  = 
(isset($data['query'])?$data['query']:'');
-                               $sort                   = 
(isset($data['sort'])?$data['sort']:'DESC');
-                               $order                  = 
(isset($data['order'])?$data['order']:'');
-                               $cat_id                 = 
(isset($data['cat_id'])?$data['cat_id']:'');
-                               $allrows                = 
(isset($data['allrows'])?$data['allrows']:'');
-                               $agreement_id   = 
(isset($data['agreement_id'])?$data['agreement_id']:'');
-                       }
-
-                       $entity_table = 'fm_activity_price_index';
-
-                       $paranthesis .='(';
-                       $joinmethod .= " $this->join fm_activities ON ( 
fm_activities.id = $entity_table.activity_id))";
-
-                       $cols = "fm_activities.*, 
$entity_table.m_cost,$entity_table.w_cost,"
-                               . " 
$entity_table.total_cost,$entity_table.index_count,"
-                               . " 
$entity_table.index_date,$entity_table.activity_id,"
-                               . " 
$entity_table.this_index,$entity_table.agreement_id";
-
-
-                       $uicols['name'][]                       = 'activity_id';
-                       $uicols['descr'][]                      = lang('ID');
-
-                       $uicols['name'][]                       = 'num';
-                       $uicols['descr'][]                      = lang('Code');
-
-                       $uicols['name'][]                       = 'descr';
-                       $uicols['descr'][]                      = lang('descr');
-
-                       $uicols['name'][]                       = 'unit';
-                       $uicols['descr'][]                      = lang('unit');
-
-                       $uicols['name'][]                       = 'm_cost';
-                       $uicols['descr'][]                      = 
lang('Material cost');
-
-                       $uicols['name'][]                       = 'w_cost';
-                       $uicols['descr'][]                      = lang('Labour 
cost');
-
-                       $uicols['name'][]                       = 'total_cost';
-                       $uicols['descr'][]                      = lang('Total 
cost');
-
-                       $uicols['name'][]                       = 'this_index';
-                       $uicols['descr'][]                      = lang('index');
-
-                       $uicols['name'][]                       = 'index_count';
-                       $uicols['descr'][]                      = 
lang('index_count');
-
-                       $uicols['name'][]                       = 'index_date';
-                       $uicols['descr'][]                      = lang('Date');
-
-                       if ($order)
-                       {
-                               $ordermethod = " order by $entity_table.$order 
$sort";
-                       }
-                       else
-                       {
-                               $ordermethod = " order by 
$entity_table.activity_id DESC";
-                       }
-
-
-                       $from .= " FROM $paranthesis $entity_table ";
-
-                       $sql = "SELECT $cols $from $joinmethod";
-
-                       $this->uicols   = $uicols;
-
-                       $where= 'WHERE';
-
-                       if ($agreement_id)
-                       {
-                               $filtermethod .= " $where 
$entity_table.agreement_id=$agreement_id";
-                               $filtermethod .= " AND current_index = 1";
-                               $where= 'AND';
-                       }
-
-                       if ($cat_id)
-                       {
-                               $filtermethod .= " $where 
$entity_table.category='$cat_id' ";
-                               $where= 'AND';
-                       }
-
-                       if ($vendor_id)
-                       {
-                               $filtermethod .= " $where 
$entity_table.vendor_id='$vendor_id' ";
-                               $where= 'AND';
-                       }
-
-                       if ($member_id>0)
-                       {
-                               $filtermethod .= " $where fm_vendor.member_of 
$this->like '%,$member_id,%' ";
-                               $where= 'AND';
-                       }
-
-                       if ($status)
-                       {
-                               $filtermethod .= " $where 
$entity_table.status='$status' ";
-                               $where= 'AND';
-                       }
-
-
-                       $sql .= " $filtermethod";
-//echo $sql;
-
-                       $this->db2->query($sql,__LINE__,__FILE__);
-                       $this->total_records = $this->db2->num_rows();
-                       if(!$allrows)
-                       {
-                               $this->db->limit_query($sql . 
$ordermethod,$start,__LINE__,__FILE__);
-                       }
-                       else
-                       {
-                               $this->db->query($sql . 
$ordermethod,__LINE__,__FILE__);
-                       }
-
-                       $dateformat = 
$GLOBALS['phpgw_info']['user']['preferences']['common']['dateformat'];
-
-                       while ($this->db->next_record())
-                       {
-                               $details[] = array
-                               (
-                                       'agreement_id'          => 
$this->db->f('agreement_id'),
-                                       'activity_id'           => 
$this->db->f('activity_id'),
-                                       'id'                            => 
$this->db->f('id'),
-                                       'num'                           => 
$this->db->f('num'),
-                                       'descr'                         => 
$this->db->f('descr'),
-                                       'unit'                          => 
$this->db->f('unit'),
-                                       'm_cost'                        => 
$this->db->f('m_cost'),
-                                       'w_cost'                        => 
$this->db->f('w_cost'),
-                                       'total_cost'            => 
$this->db->f('total_cost'),
-                                       'this_index'            => 
$this->db->f('this_index'),
-                                       'index_count'           => 
$this->db->f('index_count'),
-                                       'index_date'            => 
$GLOBALS['phpgw']->common->show_date($this->db->f('index_date'),$dateformat)
-                               );
-                       }
-//html_print_r($details);
-                       return $details;
-               }
-
-               function read_prizing($data)
-               {
-                       if(is_array($data))
-                       {
-                               $agreement_id   = 
(isset($data['agreement_id'])?$data['agreement_id']:0);
-                               $activity_id    = 
(isset($data['activity_id'])?$data['activity_id']:0);
-                       }
-
-                       $entity_table = 'fm_activity_price_index';
-
-                       $cols = 
"fm_activity_price_index.m_cost,fm_activity_price_index.w_cost,fm_activity_price_index.total_cost,"
-                               . " 
fm_activity_price_index.index_count,fm_activity_price_index.index_date,fm_activity_price_index.activity_id,fm_activity_price_index.this_index";
-
-                       $cols_return[]                  = 'activity_id';
-                       $uicols['input_type'][]         = 'text';
-                       $uicols['name'][]                       = 'activity_id';
-                       $uicols['descr'][]                      = 
lang('activity ID');
-                       $uicols['statustext'][]         = lang('activity ID');
-
-                       $cols_return[]                  = 'id';
-                       $uicols['input_type'][]         = 'hidden';
-                       $uicols['name'][]                       = 'id';
-                       $uicols['descr'][]                      = False;
-                       $uicols['statustext'][]         = False;
-
-                       $cols_return[]                  = 'm_cost';
-                       $uicols['input_type'][]         = 'text';
-                       $uicols['name'][]                       = 'm_cost';
-                       $uicols['descr'][]                      = 
lang('m_cost');
-                       $uicols['statustext'][]         = lang('m_cost');
-                       $cols_return[]                  = 'w_cost';
-                       $uicols['input_type'][]         = 'text';
-                       $uicols['name'][]                       = 'w_cost';
-                       $uicols['descr'][]                      = 
lang('w_cost');
-                       $uicols['statustext'][]         = lang('w_cost');
-                       $cols_return[]                  = 'total_cost';
-                       $uicols['input_type'][]         = 'text';
-                       $uicols['name'][]                       = 'total_cost';
-                       $uicols['descr'][]                      = 
lang('total_cost');
-                       $uicols['statustext'][]         = lang('total_cost');
-
-                       $cols_return[]                  = 'this_index';
-                       $uicols['input_type'][]         = 'text';
-                       $uicols['name'][]                       = 'this_index';
-                       $uicols['descr'][]                      = lang('index');
-                       $uicols['statustext'][]         = lang('index');
-
-                       $cols_return[]                  = 'index_count';
-                       $uicols['input_type'][]         = 'text';
-                       $uicols['name'][]                       = 'index_count';
-                       $uicols['descr'][]                      = 
lang('index_count');
-                       $uicols['statustext'][]         = lang('index_count');
-
-                       $cols_return[]                  = 'index_date';
-                       $uicols['input_type'][]         = 'text';
-                       $uicols['name'][]                       = 'index_date';
-                       $uicols['descr'][]                      = lang('date');
-                       $uicols['statustext'][]         = lang('date');
-
-                       $from .= " FROM $entity_table ";
-
-                       $sql = "SELECT $cols $from $joinmethod";
-
-
-                       $this->uicols   = $uicols;
-
-                       $ordermethod = " order by $entity_table.index_count 
ASC";
-
-                       $where= 'WHERE';
-
-
-                       if ($agreement_id)
-                       {
-                               $filtermethod .= " $where 
$entity_table.agreement_id=$agreement_id AND activity_id=$activity_id";
-                               $where= 'AND';
-                       }
-
-
-                       $sql .= " $filtermethod";
-//echo $sql;
-
-                       $this->db2->query($sql,__LINE__,__FILE__);
-                       $this->total_records = $this->db2->num_rows();
-                       if(!$allrows)
-                       {
-                               $this->db->limit_query($sql . 
$ordermethod,$start,__LINE__,__FILE__);
-                       }
-                       else
-                       {
-                               $this->db->query($sql . 
$ordermethod,__LINE__,__FILE__);
-                       }
-
-                       $j=0;
-                       $n=count($cols_return);
-//_debug_array($cols_return);
-                       while ($this->db->next_record())
-                       {
-                               for ($i=0;$i<$n;$i++)
-                               {
-                                       $agreement_list[$j][$cols_return[$i]] = 
$this->db->f($cols_return[$i]);
-                                       $agreement_list[$j]['agreement_id'] = 
$agreement_id;
-                               }
-                               $j++;
-                       }
-//_debug_array($agreement_list);
-                       return $agreement_list;
-               }
-
-
-               function read_single($data)
-               {
-                       $attribute_table = 'fm_agreement_attribute';
-                       $table = 'fm_agreement';
-
-                       $agreement_id =$data['agreement_id'];
-
-                       $this->db->query("SELECT * FROM $attribute_table WHERE 
attrib_detail = 1 ORDER BY attrib_sort");
-
-                       while ($this->db->next_record())
-                       {
-                               $agreement['attributes'][] = array
-                               (
-                                       'attrib_id'             => 
$this->db->f('id'),
-                                       'name'                  => 
$this->db->f('column_name'),
-                                       'input_text'    => 
stripslashes($this->db->f('input_text')),
-                                       'statustext'    => 
stripslashes($this->db->f('statustext')),
-                                       'datatype'              => 
$this->db->f('datatype')
-                               );
-                       }
-
-                       if($agreement_id)
-                       {
-                               $this->db->query("SELECT 
$table.*,fm_vendor.member_of FROM $table $this->join fm_vendor ON 
$table.vendor_id = fm_vendor.id where $table.id='$agreement_id'");
-
-                               if($this->db->next_record())
-                               {
-                                       $agreement['id']                        
= (int)$this->db->f('id');
-                                       $agreement['entry_date']        = 
$this->db->f('entry_date');
-                                       $agreement['cat_id']            = 
$this->db->f('category');
-                                       $agreement['start_date']        = 
$this->db->f('start_date');
-                                       $agreement['end_date']          = 
$this->db->f('end_date');
-                                       $agreement['termination_date']= 
$this->db->f('termination_date');
-                                       $agreement['vendor_id']         = 
$this->db->f('vendor_id');
-                                       $agreement['b_account_id']      = 
$this->db->f('account_id');
-                                       $agreement['name']                      
= stripslashes($this->db->f('name'));
-                                       $agreement['descr']                     
= stripslashes($this->db->f('descr'));
-                                       $agreement['user_id']           = 
$this->db->f('user_id');
-                                       $agreement['group_id']          = 
$this->db->f('group_id');
-                                       $agreement['status']            = 
$this->db->f('status');
-                                       $agreement['member_of']         = 
explode(',',$this->db->f('member_of'));
-
-                                       for 
($i=0;$i<count($agreement['attributes']);$i++)
-                                       {
-                                               
$agreement['attributes'][$i]['value']   = 
$this->db->f($agreement['attributes'][$i]['name']);
-                                               
$agreement['attributes'][$i]['datatype_text']   = 
$this->bocommon->translate_datatype($agreement['attributes'][$i]['datatype']);
-                                       }
-
-                               }
-                       }
-//_debug_array($agreement);
-                       return $agreement;
-               }
-
-               function read_single_item($data)
-               {
-                       $attribute_table = 'fm_agreement_attribute';
-                       $table = 'fm_activities';
-
-                       $agreement_id =$data['agreement_id'];
-                       $id =$data['id'];
-
-                       $this->db->query("SELECT * FROM $attribute_table WHERE 
attrib_detail = 2 ORDER BY attrib_sort");
-
-                       while ($this->db->next_record())
-                       {
-                               $item['attributes'][] = array
-                               (
-                                       'attrib_id'             => 
$this->db->f('id'),
-                                       'name'                  => 
$this->db->f('column_name'),
-                                       'input_text'    => 
stripslashes($this->db->f('input_text')),
-                                       'statustext'    => 
stripslashes($this->db->f('statustext')),
-                                       'datatype'              => 
$this->db->f('datatype')
-                               );
-                       }
-
-                       if($id && $agreement_id)
-                       {
-                               $this->db->query("SELECT * from $table 
$this->join fm_activity_price_index on $table.id = 
fm_activity_price_index.activity_id where $table.id=$id AND 
agreement_id=$agreement_id and index_count = 1");
-
-                               if($this->db->next_record())
-                               {
-                                       $item['agreement_id']   = 
(int)$this->db->f('agreement_id');
-                                       $item['id']                             
= (int)$this->db->f('id');
-                                       $item['num']                    = 
$this->db->f('num');
-                                       $item['entry_date']             = 
$this->db->f('entry_date');
-                                       $item['m_cost']                 = 
$this->db->f('m_cost');
-                                       $item['w_cost']                 = 
$this->db->f('w_cost');
-                                       $item['total_cost']             = 
$this->db->f('total_cost');
-
-                                       for 
($i=0;$i<count($item['attributes']);$i++)
-                                       {
-                                               
$item['attributes'][$i]['value']        = 
$this->db->f($item['attributes'][$i]['name']);
-                                               
$item['attributes'][$i]['datatype_text']        = 
$this->bocommon->translate_datatype($item['attributes'][$i]['datatype']);
-                                       }
-
-                               }
-                       }
-                       return $item;
-               }
-
-               function add($agreement,$values_attribute='')
-               {
-//_debug_array($agreement);
-                       $table = 'fm_agreement';
-                       $agreement['name'] = 
$this->db->db_addslashes($agreement['name']);
-                       $agreement['descr'] = 
$this->db->db_addslashes($agreement['descr']);
-
-                       if($agreement['member_of'])
-                       {
-                               $agreement['member_of']=',' . 
implode(',',$agreement['member_of']) . ',';
-                       }
-
-                       $this->db->transaction_begin();
-                       $id = $this->bocommon->next_id($table);
-                       $vals[] = $id;
-                       $vals[] = $agreement['name'];
-                       $vals[] = $agreement['descr'];
-                       $vals[] = time();
-                       $vals[] = $agreement['cat_id'];
-                       $vals[] = $agreement['start_date'];
-                       $vals[] = $agreement['end_date'];
-                       $vals[] = $agreement['termination_date'];
-                       $vals[] = $agreement['vendor_id'];
-                       $vals[] = $this->account;
-
-
-                       while (is_array($agreement['extra']) && 
list($input_name,$value) = each($agreement['extra']))
-                       {
-                               if($value)
-                               {
-                                       $cols[] = $input_name;
-                                       $vals[] = $value;
-                               }
-                       }
-
-                       if (isset($values_attribute) AND 
is_array($values_attribute))
-                       {
-                               foreach($values_attribute as $entry)
-                               {
-                                       if($entry['value'])
-                                       {
-                                               $cols[] = $entry['name'];
-                                               $vals[] = $entry['value'];
-                                       }
-                               }
-                       }
-
-                       $cols[] = 'group_id';
-                       $vals[] = $agreement['group_id'];
-                       $cols[] = 'status';
-                       $vals[] = $agreement['status'];
-
-                       if($cols)
-                       {
-                               $cols   = "," . implode(",", $cols);
-                               $vals   = 
$this->bocommon->validate_db_insert($vals);
-                       }
-
-                       $this->db->query("INSERT INTO $table 
(id,name,descr,entry_date,category,start_date,end_date,termination_date,vendor_id,user_id
 $cols) "
-                               . "VALUES ($vals)",__LINE__,__FILE__);
-
-                       $receipt['agreement_id']= 
$id;//$this->db->get_last_insert_id($table,'id');
-
-                       $receipt['message'][] = array('msg'=>lang('agreement %1 
has been saved',$receipt['agreement_id']));
-
-                       $this->db->transaction_commit();
-                       return $receipt;
-               }
-
-               function add_item($values,$values_attribute='')
-               {
-//_debug_array($values);
-
-                       $this->db->transaction_begin();
-
-                       $this->db->query("SELECT start_date FROM fm_agreement 
WHERE id=" . $values['agreement_id']);
-                       $this->db->next_record();
-                       $start_date     = $this->db->f('start_date');
-
-                       $agreement_id = $values['agreement_id'];
-                       $activity_id = $values['id'];
-                       $m_cost = $this->floatval($values['m_cost']);
-                       $w_cost = $this->floatval($values['w_cost']);
-                       $total_cost = $this->floatval($values['total_cost']);
-                       $entry_date = time();
-
-
-                       $sql = "UPDATE fm_activity_price_index SET "
-                               . " index_count = 1,current_index = 
1,this_index = 1,"
-                               . " m_cost = $m_cost,w_cost = $w_cost 
,total_cost = $total_cost ,index_date = $start_date,"
-                               . " entry_date = $entry_date ,user_id =" . 
$this->account . " WHERE agreement_id = $agreement_id AND activity_id = 
$activity_id AND index_count = -1";
-
-                       $this->db->query($sql);
-
-                       $receipt['agreement_id']= $values['agreement_id'];
-                       $receipt['id']= $values['id'];
-
-                       $receipt['message'][] = array('msg'=>lang('activity %1 
has been saved',$receipt['id']));
-
-                       $this->db->transaction_commit();
-                       return $receipt;
-               }
-
-               function edit($values,$values_attribute='')
-               {
-//_debug_array($values);
-//_debug_array($values_attribute);
-                       $table = 'fm_agreement';
-
-                       $values['name'] = 
$this->db->db_addslashes($values['name']);
-
-                       if($values['member_of'])
-                       {
-                               $values['member_of']=',' . 
implode(',',$values['member_of']) . ',';
-                       }
-
-                       while (is_array($values['extra']) && 
list($column,$value) = each($values['extra']))
-                       {
-                               $value_set[$column]     = $value;
-                       }
-
-                       if (isset($values_attribute) AND 
is_array($values_attribute))
-                       {
-                               foreach($values_attribute as $entry)
-                               {
-                                       if($entry['datatype']!='AB' && 
$entry['datatype']!='VENDOR')
-                                       {
-                                               $value_set[$entry['name']]      
= $entry['value'];
-                                       }
-                               }
-                       }
-
-                       $value_set['name']      = $values['name'];
-                       $value_set['descr']     = $values['descr'];
-                       $value_set['group_id']  = $values['group_id'];
-                       $value_set['status']    = $values['status'];
-                       if($value_set)
-                       {
-                               $value_set      = ',' . 
$this->bocommon->validate_db_update($value_set);
-                       }
-
-                       $this->db->query("UPDATE $table set entry_date='" . 
time() . "', category='"
-                                                       . $values['cat_id'] . 
"', start_date=" . intval($values['start_date']) . ", end_date=" . 
intval($values['end_date']) . ", termination_date=" . 
intval($values['termination_date']) . "$value_set WHERE id=" . 
intval($values['agreement_id']));
-
-                       $this->db->query("UPDATE fm_activity_price_index set 
index_date=" . intval($values['start_date']) . " WHERE index_count=1 AND 
agreement_id= " . intval($values['agreement_id']));
-
-                       $receipt['agreement_id']= $values['agreement_id'];
-                       $receipt['message'][] = array('msg'=>lang('agreement %1 
has been edited',$values['agreement_id']));
-                       return $receipt;
-               }
-
-               function edit_item($values)
-               {
-//_debug_array($values);
-
-                       $value_set['m_cost']            = $values['m_cost'];
-                       $value_set['w_cost']            = $values['w_cost'];
-                       $value_set['total_cost']        = $values['total_cost'];
-
-                       if($value_set)
-                       {
-                               $value_set      = ',' . 
$this->bocommon->validate_db_update($value_set);
-                       }
-
-                       $this->db->query("UPDATE fm_activity_price_index set 
entry_date=" . time() . "$value_set WHERE agreement_id=" . 
intval($values['agreement_id']) . ' AND activity_id=' . intval($values['id']));
-
-                       $this->db->query("UPDATE fm_activity_price_index  set 
m_cost = this_index *" . $this->floatval($values['m_cost']) . ",w_cost = 
this_index *" . $this->floatval($values['w_cost']) . ",total_cost = this_index 
*" . $this->floatval($values['total_cost']) . "  WHERE agreement_id=" . 
intval($values['agreement_id']) . ' AND activity_id=' . intval($values['id']));
-
-                       $receipt['agreement_id']= $values['agreement_id'];
-                       $receipt['id']= $values['id'];
-                       $receipt['message'][] = array('msg'=>lang('Activity %1 
has been edited',$values['id']));
-                       return $receipt;
-               }
-
-               function update($values)
-               {
-//_debug_array($values);
-                       
$values['new_index']=$this->floatval($values['new_index']);
-                       $this->db->transaction_begin();
-
-                       while (is_array($values['select']) && 
list(,$activity_id) = each($values['select']))
-                       {
-
-                               if($values['id'][$activity_id]>0)
-                               {
-                                       $this->db->query("UPDATE 
fm_activity_price_index set current_index = NULL WHERE agreement_id=" . 
intval($values['agreement_id']) . ' AND activity_id=' . intval($activity_id));
-
-                                       $this->db->query("INSERT INTO 
fm_activity_price_index 
(agreement_id,activity_id,index_count,current_index,this_index,m_cost,w_cost,total_cost,index_date,entry_date,user_id)"
-                                               . "VALUES (" . 
$values['agreement_id'] . "," . $activity_id ."," . 
($values['id'][$activity_id]+1) .",1,'" . $values['new_index'] . "','" . 
($values['m_cost'][$activity_id] * $values['new_index']) . "','" . 
($values['w_cost'][$activity_id] * $values['new_index']) . "','" . 
($values['total_cost'][$activity_id] * $values['new_index'])  . "'," . 
(int)$values['date'] . "," . time()
-                                               . "," . $this->account . ")");
-
-                                       $receipt['message'][] = 
array('msg'=>lang('Activity %1 has been updated for index',$activity_id));
-                               }
-
-                       }
-
-                       $this->db->transaction_commit();
-
-                       return $receipt;
-               }
-
-               function floatval($strValue)
-               {
-                       $floatValue = 
ereg_replace("(^[0-9]*)(\\.|,)([0-9]*)(.*)", "\\1.\\3", $strValue);
-                       if(!is_numeric($floatValue))
-                       {
-                               $floatValue = ereg_replace("(^[0-9]*)(.*)", 
"\\1", $strValue);
-                       }
-                       if(!is_numeric($floatValue))
-                       {
-                               $floatValue = 0;
-                       }
-                       return $floatValue;
-               }
-
-
-               function delete_last_index($agreement_id,$activity_id)
-               {
-                       $this->db->transaction_begin();
-                       $this->db->query("SELECT max(index_count) as 
index_count FROM fm_activity_price_index WHERE agreement_id=$agreement_id AND 
activity_id=$activity_id");
-                       $this->db->next_record();
-                       $index_count    = $this->db->f('index_count');
-                       if($index_count>1)
-                       {
-                               $this->db->query("DELETE FROM 
fm_activity_price_index WHERE agreement_id=$agreement_id AND 
activity_id=$activity_id AND index_count=$index_count");
-                               $this->db->query("UPDATE 
fm_activity_price_index set current_index = 1 WHERE agreement_id=$agreement_id 
AND activity_id=$activity_id AND index_count =" . ($index_count-1));
-                       }
-                       else
-                       {
-                               $sql = "UPDATE fm_activity_price_index SET "
-                                       . " index_count = -1,current_index = 
1,this_index = 1,"
-                                       . " m_cost = NULL,w_cost = NULL 
,total_cost = NULL ,index_date = NULL,"
-                                       . " entry_date = NULL ,user_id =" . 
$this->account . " WHERE agreement_id = $agreement_id AND activity_id = 
$activity_id";
-
-                               $this->db->query($sql);
-
-                       }
-                       $this->db->transaction_commit();
-               }
-
-               function delete_item($agreement_id,$activity_id)
-               {
-                       $this->db->transaction_begin();
-                       $this->db->query("DELETE FROM fm_activity_price_index 
WHERE agreement_id=$agreement_id AND activity_id=$activity_id");
-                       $this->db->transaction_commit();
-               }
-
-               function delete($agreement_id)
-               {
-                       $this->db->transaction_begin();
-                       $this->db->query("DELETE FROM fm_agreement WHERE id=" . 
intval($agreement_id));
-                       $this->db->query("DELETE FROM fm_activity_price_index 
WHERE agreement_id=" . intval($agreement_id));
-                       $this->db->transaction_commit();
-               }
-
-               function read_attrib($data)
-               {
-                       $attribute_table = 'fm_agreement_attribute';
-//html_print_r($data);
-                       if(is_array($data))
-                       {
-                               if ($data['start'])
-                               {
-                                       $start=$data['start'];
-                               }
-                               else
-                               {
-                                       $start=0;
-                               }
-                               $query = 
(isset($data['query'])?$data['query']:'');
-                               $sort = 
(isset($data['sort'])?$data['sort']:'DESC');
-                               $order = 
(isset($data['order'])?$data['order']:'');
-                               $allrows = 
(isset($data['allrows'])?$data['allrows']:'');
-                               $column_list = 
(isset($data['column_list'])?$data['column_list']:'');
-                       }
-
-                       $where = 'WHERE';
-                       if ($column_list)
-                       {
-                               $filtermethod = " $where list !=1 or list is 
null";
-                               $where = 'AND';
-                       }
-                       if ($this->role=='detail')
-                       {
-                               $filtermethod .= " $where attrib_detail=2 ";
-                       }
-                       else
-                       {
-                               $filtermethod .= " $where attrib_detail=1 ";
-                       }
-
-                       $where = 'AND';
-
-                       if ($order)
-                       {
-                               $ordermethod = " order by $order $sort";
-
-                       }
-                       else
-                       {
-                               $ordermethod = ' order by attrib_sort asc';
-                       }
-
-                       if($query)
-                       {
-                               $query = ereg_replace("'",'',$query);
-                               $query = ereg_replace('"','',$query);
-
-                               $querymethod = " $where 
($attribute_table.input_text $this->like '%$query%' or 
$attribute_table.column_name $this->like '%$query%')";
-                       }
-
-                       $sql = "SELECT * FROM $attribute_table $filtermethod 
$querymethod";
-
-//echo $sql;
-                       $this->db2->query($sql,__LINE__,__FILE__);
-                       $this->total_records = $this->db2->num_rows();
-                       if(!$allrows)
-                       {
-                               $this->db->limit_query($sql . 
$ordermethod,$start,__LINE__,__FILE__);
-                       }
-                       else
-                       {
-                               $this->db->query($sql . 
$ordermethod,__LINE__,__FILE__);
-                       }
-
-                       while ($this->db->next_record())
-                       {
-                               $attrib[] = array
-                               (
-                                       'id'                    => 
$this->db->f('id'),
-                                       'attrib_sort'   => 
$this->db->f('attrib_sort'),
-                                       'list'                  => 
$this->db->f('list'),
-                                       'lookup_form'   => 
$this->db->f('lookup_form'),
-                                       'column_name'   => 
$this->db->f('column_name'),
-                                       'name'                  => 
$this->db->f('input_text'),
-                                       'size'                  => 
$this->db->f('size'),
-                                       'statustext'    => 
$this->db->f('statustext'),
-                                       'input_text'    => 
$this->db->f('input_text'),
-                                       'type_name'             => 
$this->db->f('type'),
-                                       'datatype'              => 
$this->db->f('datatype'),
-                                       'search'                => 
$this->db->f('search')
-                               );
-                       }
-                       return $attrib;
-               }
-
-               function read_single_attrib($id)
-               {
-                       $attribute_table = 'fm_agreement_attribute';
-
-                       if ($this->role=='detail')
-                       {
-                               $filtermethod = " AND attrib_detail=2 ";
-                       }
-                       else
-                       {
-                               $filtermethod = " AND attrib_detail=1 ";
-                       }
-
-                       $sql = "SELECT * FROM $attribute_table where id=$id 
$filtermethod";
-
-                       $this->db->query($sql);
-
-                       if($this->db->next_record())
-                       {
-                               $attrib['id']                                   
        = $this->db->f('id');
-                               $attrib['column_name']                          
= $this->db->f('column_name');
-                               $attrib['input_text']                           
= $this->db->f('input_text');
-                               $attrib['statustext']                           
= $this->db->f('statustext');
-                               $attrib['column_info']['precision']     = 
$this->db->f('precision_');
-                               $attrib['column_info']['scale']         = 
$this->db->f('scale');
-                               $attrib['column_info']['default']       = 
$this->db->f('default_value');
-                               $attrib['column_info']['nullable']      = 
$this->db->f('nullable');
-                               $attrib['column_info']['type']          = 
$this->db->f('datatype');
-                               $attrib['type_name']                            
= $this->db->f('type_name');
-                               $attrib['lookup_form']                          
= $this->db->f('lookup_form');
-                               $attrib['list']                                 
        = $this->db->f('list');
-                               $attrib['search']                               
        = $this->db->f('search');
-                               if($this->db->f('datatype')=='R' || 
$this->db->f('datatype')=='CH' || $this->db->f('datatype')=='LB')
-                               {
-                                       $attrib['choice'] = 
$this->read_attrib_choice($id);
-                               }
-
-                               return $attrib;
-                       }
-               }
-
-               function read_attrib_choice($attrib_id)
-               {
-                       $choice_table = 'fm_agreement_choice';
-
-                       if ($this->role=='detail')
-                       {
-                               $filtermethod = " AND attrib_detail=2 ";
-                       }
-                       else
-                       {
-                               $filtermethod = " AND attrib_detail=1 ";
-                       }
-
-                       $sql = "SELECT * FROM $choice_table WHERE 
attrib_id=$attrib_id $filtermethod";
-                       $this->db->query($sql);
-
-                       while ($this->db->next_record())
-                       {
-                               $choice[] = array
-                               (
-                                       'id'    => $this->db->f('id'),
-                                       'value' => $this->db->f('value')
-                               );
-
-                       }
-                       return $choice;
-               }
-
-               function add_attrib($attrib)
-               {
-                       $attribute_table = 'fm_agreement_attribute';
-                       $attrib['column_name'] = 
$this->db->db_addslashes($attrib['column_name']);
-                       $attrib['input_text'] = 
$this->db->db_addslashes($attrib['input_text']);
-                       $attrib['statustext'] = 
$this->db->db_addslashes($attrib['statustext']);
-                       $attrib['default'] = 
$this->db->db_addslashes($attrib['default']);
-                       $attrib['id'] = 
$this->bocommon->next_id($attribute_table, array('attrib_detail'=>!!$this->role 
+1));
-
-                       if($this->role=='detail')
-                       {
-                               $filtermethod= 'WHERE attrib_detail=2';
-                               $table = 'fm_agreement_detail';
-                       }
-                       else
-                       {
-                               $filtermethod= 'WHERE attrib_detail=1';
-                               $table = 'fm_agreement';
-                       }
-                       $sql = "SELECT max(attrib_sort) as max_sort FROM 
$attribute_table $filtermethod";
-                       $this->db->query($sql);
-                       $this->db->next_record();
-                       $attrib_sort    = $this->db->f('max_sort')+1;
-
-                       $values= array(
-                               !!$this->role +1,
-                               $attrib['id'],
-                               $attrib['column_name'],
-                               $attrib['input_text'],
-                               $attrib['statustext'],
-                               $attrib['lookup_form'],
-                               $attrib['search'],
-                               $attrib['list'],
-                               $attrib_sort,
-                               $attrib['column_info']['type'],
-                               $attrib['column_info']['precision'],
-                               $attrib['column_info']['scale'],
-                               $attrib['column_info']['default'],
-                               $attrib['column_info']['nullable']
-                               );
-
-                       $values = $this->bocommon->validate_db_insert($values);
-
-                       $this->db->transaction_begin();
-
-                       $this->db->query("INSERT INTO $attribute_table 
(attrib_detail,id,column_name, input_text, 
statustext,lookup_form,search,list,attrib_sort,datatype,precision_,scale,default_value,nullable)
 "
-                               . "VALUES ($values)");
-
-                       $receipt['id']= $attrib['id'];
-
-                       if($attrib['column_info']['type']=='email' && 
!$attrib['column_info']['precision'])
-                       {
-                               $attrib['column_info']['precision']=64;
-                       }
-
-                       $attrib['column_info']['type']  = 
$this->bocommon->translate_datatype_insert($attrib['column_info']['type']);
-
-                       if($attrib['column_info']['type']=='int' && 
!$attrib['column_info']['precision'])
-                       {
-                               $attrib['column_info']['precision']=4;
-                       }
-
-                       if(!$attrib['column_info']['default'])
-                       {
-                               unset($attrib['column_info']['default']);
-                       }
-
-                       $this->init_process();
-
-                       
if($this->oProc->AddColumn($table,$attrib['column_name'], 
$attrib['column_info']))
-                       {
-                               $receipt['message'][] = array('msg'     => 
lang('Attribute has been saved')     );
-                               $this->db->transaction_commit();
-
-                       }
-                       else
-                       {
-                               $receipt['error'][] = array('msg'       => 
lang('column could not be added')    );
-                               if($this->db->Transaction)
-                               {
-                                       $GLOBALS['phpgw']->db->rollbacktrans();
-                               }
-                               else
-                               {
-                                       $GLOBALS['phpgw']->db->Execute("DELETE 
FROM $attribute_table WHERE id='" . $receipt['id'] . "'");
-                                       unset($receipt['id']);
-
-                               }
-                       }
-
-                       return $receipt;
-               }
-
-               function init_process()
-               {
-                       $this->oProc                                            
= 
CreateObject('phpgwapi.schema_proc',$GLOBALS['phpgw_info']['server']['db_type']);
-                       $this->oProc->m_odb                                     
= $this->db;
-                       $this->oProc->m_odb->Halt_On_Error      = 'report';
-               }
-
-               function edit_attrib($attrib)
-               {
-                       $attribute_table = 'fm_agreement_attribute';
-                       $table = 'fm_agreement';
-
-                       $attrib['column_name'] = 
$this->db->db_addslashes($attrib['column_name']);
-                       $attrib['input_text'] = 
$this->db->db_addslashes($attrib['input_text']);
-                       $attrib['statustext'] = 
$this->db->db_addslashes($attrib['statustext']);
-                       $attrib['default'] = 
$this->db->db_addslashes($attrib['default']);
-
-                       $choice_table = 'fm_agreement_choice';
-
-                       if($this->role=='detail')
-                       {
-                               $filtermethod= ' AND attrib_detail=2';
-                               $table = 'fm_agreement_detail';
-                       }
-                       else
-                       {
-                               $filtermethod= ' AND attrib_detail=1';
-                               $table = 'fm_agreement';
-                       }
-
-                       $this->db->query("SELECT column_name FROM 
$attribute_table WHERE id='" . $attrib['id']. "' $filtermethod");
-                       $this->db->next_record();
-                       $OldColumnName          = $this->db->f('column_name');
-
-                       $this->db->transaction_begin();
-
-                       $attrib_type=$attrib['column_info']['type'];
-                       if(($OldColumnName !=$attrib['column_name']) || 
($attrib['column_info']['type'] != $this->db->f('datatype')))
-                       {
-                               $value_set=array(
-                                       'column_name'           => 
$attrib['column_name'],
-                                       'input_text'            => 
$attrib['input_text'],
-                                       'statustext'            => 
$attrib['statustext'],
-                                       'lookup_form'           => 
$attrib['lookup_form'],
-                                       'search'                        => 
$attrib['search'],
-                                       'list'                          => 
$attrib['list'],
-                                       'datatype'                      => 
$attrib['column_info']['type'],
-                                       'precision_'            => 
$attrib['column_info']['precision'],
-                                       'scale'                         => 
$attrib['column_info']['scale'],
-                                       'default_value'         => 
$attrib['column_info']['default'],
-                                       'nullable'                      => 
$attrib['column_info']['nullable']
-                                       );
-
-                               $value_set      = 
$this->bocommon->validate_db_update($value_set);
-
-                               $this->db->query("UPDATE $attribute_table set 
$value_set WHERE id=" . $attrib['id'] . $filtermethod);
-
-                               if($attrib['column_info']['type']=='email' && 
!$attrib['column_info']['precision'])
-                               {
-                                       $attrib['column_info']['precision']=64;
-                               }
-
-                               $attrib['column_info']['type']  = 
$this->bocommon->translate_datatype_insert($attrib['column_info']['type']);
-
-                               if($attrib['column_info']['type']=='int' && 
!$attrib['column_info']['precision'])
-                               {
-                                       $attrib['column_info']['precision']=4;
-                               }
-
-                               if(!$attrib['column_info']['default'])
-                               {
-                                       
unset($attrib['column_info']['default']);
-                               }
-
-                               $this->init_process();
-
-                               
$this->oProc->AlterColumn($table,$OldColumnName,$attrib['column_name'],$attrib['column_info']);
-
-                       }
-
-
-                       if($attrib['new_choice'])
-                       {
-                               $this->db->query("SELECT max(id) as id FROM 
$choice_table WHERE attrib_id='" . $attrib['id']. "' $filtermethod");
-                               $this->db->next_record();
-                               $choice_id              = $this->db->f('id')+1;
-
-       //                      $choice_id = 
$this->bocommon->next_id($choice_table 
,array('attrib_detail'=>2,'attrib_id'=>$attrib['id']));
-
-                               $values= array(
-                                       $attrib['id'],
-                                       $choice_id,
-                                       !!$this->role +1,
-                                       $attrib['new_choice']
-                                       );
-
-                               $values = 
$this->bocommon->validate_db_insert($values);
-
-                               $this->db->query("INSERT INTO $choice_table 
(attrib_id,id,attrib_detail,value) "
-                               . "VALUES ($values)");
-                       }
-
-
-                       if($attrib['delete_choice'])
-                       {
-                               for 
($i=0;$i<count($attrib['delete_choice']);$i++)
-                               {
-                                       $this->db->query("DELETE FROM 
$choice_table WHERE  attrib_id=" . $attrib['id']  ." AND id=" . 
$attrib['delete_choice'][$i] . $filtermethod);
-                               }
-                       }
-
-//html_print_r($attrib_type);
-                       if($attrib_type!='R' && $attrib_type!='CH' && 
$attrib_type!='LB')
-                       {
-                               $this->db->query("DELETE FROM $choice_table 
WHERE  attrib_id=" . $attrib['id'] . $filtermethod);
-                       }
-
-
-                       $this->db->transaction_commit();
-
-                       $receipt['message'][] = array('msg'     => 
lang('Attribute has been edited'));
-
-
-                       return $receipt;
-
-               }
-               function resort_attrib($data)
-               {
-//html_print_r($data);
-                       $attribute_table = 'fm_agreement_attribute';
-                       if(is_array($data))
-                       {
-                               $resort = 
(isset($data['resort'])?$data['resort']:'up');
-                               $id = (isset($data['id'])?$data['id']:'');
-                       }
-
-                       $sql = "SELECT attrib_sort FROM $attribute_table where 
id=$id";
-                       $this->db->query($sql);
-                       $this->db->next_record();
-                       $attrib_sort    = $this->db->f('attrib_sort');
-                       $sql = "SELECT max(attrib_sort) as max_sort FROM 
$attribute_table";
-                       $this->db->query($sql);
-                       $this->db->next_record();
-                       $max_sort       = $this->db->f('max_sort');
-                       switch($resort)
-                       {
-                               case 'up':
-                                       if($attrib_sort>1)
-                                       {
-                                               $sql = "UPDATE $attribute_table 
set attrib_sort=$attrib_sort WHERE attrib_sort =" . ($attrib_sort-1);
-                                               $this->db->query($sql);
-                                               $sql = "UPDATE $attribute_table 
set attrib_sort=" . ($attrib_sort-1) ." WHERE id=$id";
-                                               $this->db->query($sql);
-                                       }
-                                       break;
-                               case 'down':
-                                       if($max_sort > $attrib_sort)
-                                       {
-                                               $sql = "UPDATE $attribute_table 
set attrib_sort=$attrib_sort WHERE attrib_sort =" . ($attrib_sort+1);
-                                               $this->db->query($sql);
-                                               $sql = "UPDATE $attribute_table 
set attrib_sort=" . ($attrib_sort+1) ." WHERE id=$id";
-                                               $this->db->query($sql);
-                                       }
-                                       break;
-                               default:
-                                       return;
-                                       break;
-                       }
-               }
-               function delete_attrib($attrib_id)
-               {
-                       $table = 'fm_agreement';
-                       $attribute_table = 'fm_agreement_attribute';
-                       $this->init_process();
-
-                       $sql = "SELECT * FROM $attribute_table WHERE 
id=$attrib_id";
-
-                       $this->db->query($sql);
-                       $this->db->next_record();
-                       $ColumnName             = $this->db->f('column_name');
-
-                       if($this->oProc->DropColumn($table,'', $ColumnName))
-                       {
-                               $sql = "SELECT attrib_sort FROM 
$attribute_table where id=$attrib_id";
-                               $this->db->query($sql);
-                               $this->db->next_record();
-                               $attrib_sort    = $this->db->f('attrib_sort');
-                               $sql2 = "SELECT max(attrib_sort) as max_sort 
FROM $attribute_table";
-                               $this->db->query($sql2);
-                               $this->db->next_record();
-                               $max_sort       = $this->db->f('max_sort');
-                               if($max_sort>$attrib_sort)
-                               {
-                                       $sql = "UPDATE $attribute_table set 
attrib_sort=attrib_sort-1 WHERE attrib_sort > $attrib_sort";
-                                       $this->db->query($sql);
-                               }
-
-                               $this->db->query("DELETE FROM $attribute_table 
WHERE id=$attrib_id");
-                       }
-                       else
-                       {
-                               $receipt['error'][] = array('msg'       => 
lang('Attribute has NOT been deleted'));
-                       }
-
-                       return $receipt;
-               }
-
-               function request_next_id()
-               {
-                       $this->db->query("SELECT max(id) as id FROM 
fm_agreement");
-                       $this->db->next_record();
-                       $next_id= $this->db->f('id')+1;
-                       return $next_id;
-               }
-
-               function get_agreement_group_list()
-               {
-                       $this->db->query("SELECT * FROM fm_agreement_group 
ORDER BY descr asc");
-                       while ($this->db->next_record())
-                       {
-                               $agreement_group_list[]=array
-                               (
-                                       'id'    => $this->db->f('id'),
-                                       'name'  => 
$GLOBALS['phpgw']->strip_html($this->db->f('descr')).' [ '. 
$GLOBALS['phpgw']->strip_html($this->db->f('status')).' ] '
-                               );
-                       }
-                       return $agreement_group_list;
-               }
-
-               function read_group_activity($group_id='',$agreement_id='')
-               {
-                       $uicols['name'][]                       = 'id';
-                       $uicols['descr'][]                      = lang('ID');
-                       $uicols['name'][]                       = 'num';
-                       $uicols['descr'][]                      = lang('Num');
-                       $uicols['name'][]                       = 'base_descr';
-                       $uicols['descr'][]                      = lang('Base');
-                       $uicols['name'][]                       = 'descr';
-                       $uicols['descr'][]                      = lang('Descr');
-                       $uicols['name'][]                       = 'unit';
-                       $uicols['descr'][]                      = lang('Unit');
-                       $uicols['name'][]                       = 'ns3420';
-                       $uicols['descr'][]                      = 
lang('ns3420');
-
-                       $this->uicols   = $uicols;
-
-                       $sql="SELECT fm_activities.* FROM fm_activities WHERE 
agreement_group_id = $group_id";
-                       $this->db->query($sql);
-
-                       while ($this->db->next_record())
-                       {
-                               $activity_list[$this->db->f('id')]=array
-                               (
-                                       'id'            => $this->db->f('id'),
-                                       'num'           => $this->db->f('num'),
-                                       'base_descr'    => 
$this->db->f('base_descr'),
-                                       'descr'         => 
$this->db->f('descr'),
-                                       'unit'          => $this->db->f('unit'),
-                                       'ns3420'        => 
$this->db->f('ns3420'),
-                               );
-                       }
-
-                       $sql="SELECT activity_id FROM fm_activity_price_index 
WHERE agreement_id = $agreement_id";
-
-                       $this->db->query($sql);
-
-                       while ($this->db->next_record())
-                       {
-                               
unset($activity_list[$this->db->f('activity_id')]);
-                       }
-
-                       foreach($activity_list as $entry)
-                       {
-                               $activity_list_result[] = $entry;
-                       }
-
-                       return $activity_list_result;
-               }
-
-               function add_activity($values='',$agreement_id='')
-               {
-                       if (isset($values['select']) AND 
is_array($values['select']))
-                       {
-                               $this->db->transaction_begin();
-
-                               $this->db->query("SELECT start_date FROM 
fm_agreement WHERE id=" . $values['agreement_id']);
-                               $this->db->next_record();
-                               $date   = $this->db->f('start_date');
-
-                               foreach($values['select'] as $activity_id)
-                               {
-                                       $this->db->query("INSERT INTO 
fm_activity_price_index ( agreement_id, 
activity_id,index_count,current_index,index_date,entry_date,user_id) "
-                                       . "VALUES 
($agreement_id,$activity_id,-1,1,$date," . time() . "," . $this->account . ")");
-                               }
-
-                               $this->db->transaction_commit();
-                       }
-
-
-                       $receipt['agreement_id']= 
$id;//$this->db->get_last_insert_id($table,'id');
-
-                       $receipt['message'][] = array('msg'=>lang('agreement %1 
has been saved',$receipt['agreement_id']));
-
-                       return $receipt;
-               }
-
-               function select_status_list()
-               {
-                       $this->db->query("SELECT id, descr FROM 
fm_agreement_status ORDER BY id ");
-
-                       $i = 0;
-                       while ($this->db->next_record())
-                       {
-                               $status_entries[$i]['id']                       
        = $this->db->f('id');
-                               $status_entries[$i]['name']                     
        = stripslashes($this->db->f('descr'));
-                               $i++;
-                       }
-                       return $status_entries;
-               }
-               
-               function get_activity_descr($id)
-               {
-                       $this->db->query("SELECT descr FROM fm_activities WHERE 
id = $id",__LINE__,__FILE__);
-                       $this->db->next_record();
-                       return stripslashes($this->db->f('descr'));
-               }
-
-       }
-?>
+                       $this->db2              = $this->bocommon->new_db();
+
+                       $this->join                     = $this->bocommon->join;
+                       $this->left_join        = $this->bocommon->left_join;
+                       $this->like                     = $this->bocommon->like;
+//                     $this->role             = 'agreement';
+               }
+
+               function select_category_list()
+               {
+                       $table = 'fm_branch';
+                       $this->db->query("SELECT id,descr FROM $table  ORDER BY 
descr ");
+
+                       $i = 0;
+                       while ($this->db->next_record())
+                       {
+                               $categories[$i]['id']                           
= $this->db->f('id');
+                               $categories[$i]['name']                         
= stripslashes($this->db->f('descr'));
+                               $i++;
+                       }
+                       return $categories;
+               }
+
+               function select_vendor_list()
+               {
+                       $table = 'fm_agreement';
+                       $this->db->query("SELECT vendor_id,org_name FROM $table 
$this->join fm_vendor on fm_agreement.vendor_id=fm_vendor.id GROUP BY 
org_name,vendor_id ");
+
+                       $i = 0;
+                       while ($this->db->next_record())
+                       {
+                               $vendor[$i]['id']                               
= $this->db->f('vendor_id');
+                               $vendor[$i]['name']                             
= stripslashes($this->db->f('org_name'));
+                               $i++;
+                       }
+                       return $vendor;
+               }
+
+               function read($data)
+               {
+                       if(is_array($data))
+                       {
+                               $start                  = 
(isset($data['start'])?$data['start']:0);
+                               $filter                 = 
(isset($data['filter'])?$data['filter']:'none');
+                               $query                  = 
(isset($data['query'])?$data['query']:'');
+                               $sort                   = 
(isset($data['sort'])?$data['sort']:'DESC');
+                               $order                  = 
(isset($data['order'])?$data['order']:'');
+                               $cat_id                 = 
(isset($data['cat_id'])?$data['cat_id']:'');
+                               $vendor_id              = 
(isset($data['vendor_id'])?$data['vendor_id']:'');
+                               $allrows                = 
(isset($data['allrows'])?$data['allrows']:'');
+                               $member_id              = 
(isset($data['member_id'])?$data['member_id']:0);
+                               $agreement_id   = 
(isset($data['agreement_id'])?$data['agreement_id']:'');
+                       }
+
+                       $choice_table = 'fm_agreement_choice';
+                       $attribute_table = 'fm_agreement_attribute';
+
+                       $entity_table = 'fm_agreement';
+                       $category_table = 'fm_branch';
+                       $attribute_filter = " AND attrib_detail = 1";
+                       $paranthesis .='(';
+                       $joinmethod .= " $this->join $category_table ON ( 
$entity_table.category =$category_table.id)";
+                       $joinmethod .= " $this->join  fm_vendor ON ( 
$entity_table.vendor_id =fm_vendor.id ))";
+
+                       $cols = $entity_table . ".*,$category_table.descr as 
category, org_name";
+
+                       $cols_return[]                          = 'id';
+                       $uicols['input_type'][]         = 'text';
+                       $uicols['name'][]                       = 'id';
+                       $uicols['descr'][]                      = lang('ID');
+                       $uicols['statustext'][]         = lang('ID');
+
+
+                       $cols_return[]                          = 'name';
+                       $uicols['input_type'][]         = 'text';
+                       $uicols['name'][]                       = 'name';
+                       $uicols['descr'][]                      = lang('name');
+                       $uicols['statustext'][]         = lang('name');
+
+                       $cols_return[]                          = 'org_name';
+                       $uicols['input_type'][]         = 'text';
+                       $uicols['name'][]                       = 'org_name';
+                       $uicols['descr'][]                      = 
lang('Vendor');
+                       $uicols['statustext'][]         = lang('Vendor');
+
+                       $cols_return[]                          = 'category';
+                       $uicols['input_type'][]         = 'text';
+                       $uicols['name'][]                       = 'category';
+                       $uicols['descr'][]                      = 
lang('category');
+                       $uicols['statustext'][]         = lang('category');
+
+                       $cols_return[]                          = 'start_date';
+                       $uicols['input_type'][]         = 'text';
+                       $uicols['name'][]                       = 'start_date';
+                       $uicols['descr'][]                      = lang('start');
+                       $uicols['statustext'][]         = lang('start date');
+
+                       $cols_return[]                          = 'end_date';
+                       $uicols['input_type'][]         = 'text';
+                       $uicols['name'][]                       = 'end_date';
+                       $uicols['descr'][]                      = lang('end');
+                       $uicols['statustext'][]         = lang('end date');
+
+                       $cols_return[]                          = 'status';
+                       $uicols['input_type'][]         = 'text';
+                       $uicols['name'][]                       = 'status';
+                       $uicols['descr'][]                      = 
lang('status');
+                       $uicols['statustext'][]         = lang('status');
+
+                       if ($order)
+                       {
+                               if ($order=='id')
+                               {
+                                       $ordermethod = " order by 
$entity_table.$order $sort";
+                               }
+                               else
+                               {
+                                       $ordermethod = " order by $order $sort";
+                               }
+                       }
+                       else
+                       {
+                               $ordermethod = " order by $entity_table.id 
DESC";
+                       }
+
+
+                       $from .= " FROM $paranthesis $entity_table ";
+
+                       $sql = "SELECT $cols $from $joinmethod";
+
+                       $i      = count($uicols['name']);
+
+                       $this->db->query("SELECT * FROM $attribute_table WHERE 
list=1 $attribute_filter ");
+                       while ($this->db->next_record())
+                       {
+                               $uicols['input_type'][]         = 'text';
+                               $uicols['name'][]                       = 
$this->db->f('column_name');
+                               $uicols['descr'][]                      = 
$this->db->f('input_text');
+                               $uicols['statustext'][]         = 
$this->db->f('statustext');
+                               $uicols['datatype'][$i]         = 
$this->db->f('datatype');
+                               $cols_return_extra[]= array(
+                                       'name'  => $this->db->f('column_name'),
+                                       'datatype'      => 
$this->db->f('datatype'),
+                                       'attrib_id'     => $this->db->f('id')
+                               );
+
+                               $i++;
+                       }
+
+                       
$user_columns=$GLOBALS['phpgw_info']['user']['preferences'][$this->currentapp]['agreement_columns'
 . !!$agreement_id];
+
+//_debug_array($user_columns);
+
+                       if (isset($user_columns) AND is_array($user_columns) 
AND $user_columns[0])
+                       {
+                               foreach($user_columns as $column_id)
+                               {
+                                       $this->db->query("SELECT * FROM 
$attribute_table WHERE id= $column_id");
+
+                                       $this->db->next_record();
+                                       $uicols['input_type'][]         = 
'text';
+                                       $uicols['name'][]                       
= $this->db->f('column_name');
+                                       $uicols['descr'][]                      
= $this->db->f('input_text');
+                                       $uicols['statustext'][]         = 
$this->db->f('statustext');
+                                       $uicols['datatype'][$i]         = 
$this->db->f('datatype');
+                                       $cols_return_extra[]= array(
+                                               'name'  => 
$this->db->f('column_name'),
+                                               'datatype'      => 
$this->db->f('datatype'),
+                                               'attrib_id'     => 
$this->db->f('id')
+                                       );
+                                       $i++;
+                               }
+                       }
+
+                       $this->uicols   = $uicols;
+
+//_debug_array($cols_return_extra);
+
+                       $where= 'WHERE';
+
+/*                     if ($filter=='all')
+                       {
+                               if (is_array($grants))
+                               {
+                                       while (list($user) = each($grants))
+                                       {
+                                               $public_user_list[] = $user;
+                                       }
+                                       reset($public_user_list);
+                                       $filtermethod .= " $where ( 
$entity_table.user_id IN(" . implode(',',$public_user_list) . "))";
+
+                                       $where= 'AND';
+                               }
+
+                       }
+                       else
+                       {
+                               $filtermethod = " $where 
$entity_table.user_id=$filter ";
+                               $where= 'AND';
+                       }
+*/
+
+                       if ($agreement_id)
+                       {
+                               $filtermethod .= " $where 
$entity_table.agreement_id=$agreement_id";
+                               $filtermethod .= " AND current_index = 1";
+                               $where= 'AND';
+                       }
+
+                       if ($cat_id)
+                       {
+                               $filtermethod .= " $where 
$entity_table.category='$cat_id' ";
+                               $where= 'AND';
+                       }
+
+                       if ($vendor_id)
+                       {
+                               $filtermethod .= " $where 
$entity_table.vendor_id='$vendor_id' ";
+                               $where= 'AND';
+                       }
+
+                       if ($member_id>0)
+                       {
+                               $filtermethod .= " $where fm_vendor.member_of 
$this->like '%,$member_id,%' ";
+                               $where= 'AND';
+                       }
+
+                       if ($status)
+                       {
+                               $filtermethod .= " $where 
$entity_table.status='$status' ";
+                               $where= 'AND';
+                       }
+
+
+                       if($query)
+                       {
+                               $query = ereg_replace("'",'',$query);
+                               $query = ereg_replace('"','',$query);
+
+                               $this->db->query("SELECT * FROM 
$attribute_table where search='1'");
+
+                               while ($this->db->next_record())
+                               {
+                                       if($this->db->f('datatype')=='V' || 
$this->db->f('datatype')=='email' || $this->db->f('datatype')=='CH')
+                                       {
+                                               $querymethod[]= 
"$entity_table." . $this->db->f('column_name') . " $this->like '%$query%'";
+                                       }
+                                       else
+                                       {
+                                               $querymethod[]= 
"$entity_table." . $this->db->f('column_name') . " = '$query'";
+                                       }
+                               }
+
+                               if (isset($querymethod) AND 
is_array($querymethod))
+                               {
+                                       $querymethod = " $where (" . implode (' 
OR ',$querymethod) . ')';
+                                       $where = 'AND';
+                               }
+                       }
+
+                       $sql .= " $filtermethod $querymethod";
+//echo $sql;
+
+                       $this->db2->query($sql,__LINE__,__FILE__);
+                       $this->total_records = $this->db2->num_rows();
+                       if(!$allrows)
+                       {
+                               $this->db->limit_query($sql . 
$ordermethod,$start,__LINE__,__FILE__);
+                       }
+                       else
+                       {
+                               $this->db->query($sql . 
$ordermethod,__LINE__,__FILE__);
+                       }
+
+                       $j=0;
+                       $n=count($cols_return);
+//_debug_array($cols_return);
+
+                       $contacts                       = 
CreateObject('phpgwapi.contacts');
+
+                       while ($this->db->next_record())
+                       {
+                               for ($i=0;$i<$n;$i++)
+                               {
+                                       $agreement_list[$j][$cols_return[$i]] = 
$this->db->f($cols_return[$i]);
+                                       $agreement_list[$j]['grants'] = 
(int)$grants[$this->db->f('user_id')];
+                               }
+
+                               for ($i=0;$i<count($cols_return_extra);$i++)
+                               {
+                                       $value='';
+                                       
$value=$this->db->f($cols_return_extra[$i]['name']);
+
+                                       
if(($cols_return_extra[$i]['datatype']=='R' || 
$cols_return_extra[$i]['datatype']=='LB') && $value):
+                                       {
+                                               $sql="SELECT value FROM 
$choice_table where attrib_id=" .$cols_return_extra[$i]['attrib_id']. "  AND 
id=" . $value . $attribute_filter;
+                                               $this->db2->query($sql);
+                                               $this->db2->next_record();
+                                               
$agreement_list[$j][$cols_return_extra[$i]['name']] = $this->db2->f('value');
+                                       }
+                                       
elseif($cols_return_extra[$i]['datatype']=='AB' && $value):
+                                       {
+                                               $contact_data   = 
$contacts->read_single_entry($value,array('n_given'=>'n_given','n_family'=>'n_family','email'=>'email'));
+                                               
$agreement_list[$j][$cols_return_extra[$i]['name']]     = 
$contact_data[0]['n_family'] . ', ' . $contact_data[0]['n_given'];
+                                       }
+                                       
elseif($cols_return_extra[$i]['datatype']=='VENDOR' && $value):
+                                       {
+                                               $sql="SELECT org_name FROM 
fm_vendor where id=$value";
+                                               $this->db2->query($sql);
+                                               $this->db2->next_record();
+                                               
$agreement_list[$j][$cols_return_extra[$i]['name']] = $this->db2->f('org_name');
+
+                                       }
+                                       
elseif($cols_return_extra[$i]['datatype']=='CH' && $value):
+                                       {
+                                               $ch= unserialize($value);
+
+                                               if (isset($ch) AND 
is_array($ch))
+                                               {
+                                                       for 
($k=0;$k<count($ch);$k++)
+                                                       {
+                                                               $sql="SELECT 
value FROM $choice_table where attrib_id=" 
.$cols_return_extra[$i]['attrib_id']. "  AND id=" . $ch[$k] . $attribute_filter;
+                                                               
$this->db2->query($sql);
+                                                               while 
($this->db2->next_record())
+                                                               {
+                                                                       
$ch_value[]=$this->db2->f('value');
+                                                               }
+                                                       }
+                                                       
$agreement_list[$j][$cols_return_extra[$i]['name']] = @implode(",", $ch_value);
+                                                       unset($ch_value);
+                                               }
+                                       }
+                                       
elseif($cols_return_extra[$i]['datatype']=='D' && $value):
+                                       {
+//_debug_array($value);
+
+                                               
$agreement_list[$j][$cols_return_extra[$i]['name']]=date($GLOBALS['phpgw_info']['user']['preferences']['common']['dateformat'],strtotime($value));
+                                       }
+                                       else:
+                                       {
+                                               
$agreement_list[$j][$cols_return_extra[$i]['name']]=$value;
+                                       }
+                                       endif;
+                               }
+
+
+                               $j++;
+                       }
+//_debug_array($agreement_list);
+                       return $agreement_list;
+               }
+
+               function read_details($data)
+               {
+                       if(is_array($data))
+                       {
+                               $start                  = 
(isset($data['start'])?$data['start']:0);
+                               $filter                 = 
(isset($data['filter'])?$data['filter']:'none');
+                               $query                  = 
(isset($data['query'])?$data['query']:'');
+                               $sort                   = 
(isset($data['sort'])?$data['sort']:'DESC');
+                               $order                  = 
(isset($data['order'])?$data['order']:'');
+                               $cat_id                 = 
(isset($data['cat_id'])?$data['cat_id']:'');
+                               $allrows                = 
(isset($data['allrows'])?$data['allrows']:'');
+                               $agreement_id   = 
(isset($data['agreement_id'])?$data['agreement_id']:'');
+                       }
+
+                       $entity_table = 'fm_activity_price_index';
+
+                       $paranthesis .='(';
+                       $joinmethod .= " $this->join fm_activities ON ( 
fm_activities.id = $entity_table.activity_id))";
+
+                       $cols = "fm_activities.*, 
$entity_table.m_cost,$entity_table.w_cost,"
+                               . " 
$entity_table.total_cost,$entity_table.index_count,"
+                               . " 
$entity_table.index_date,$entity_table.activity_id,"
+                               . " 
$entity_table.this_index,$entity_table.agreement_id";
+
+
+                       $uicols['name'][]                       = 'activity_id';
+                       $uicols['descr'][]                      = lang('ID');
+
+                       $uicols['name'][]                       = 'num';
+                       $uicols['descr'][]                      = lang('Code');
+
+                       $uicols['name'][]                       = 'descr';
+                       $uicols['descr'][]                      = lang('descr');
+
+                       $uicols['name'][]                       = 'unit';
+                       $uicols['descr'][]                      = lang('unit');
+
+                       $uicols['name'][]                       = 'm_cost';
+                       $uicols['descr'][]                      = 
lang('Material cost');
+
+                       $uicols['name'][]                       = 'w_cost';
+                       $uicols['descr'][]                      = lang('Labour 
cost');
+
+                       $uicols['name'][]                       = 'total_cost';
+                       $uicols['descr'][]                      = lang('Total 
cost');
+
+                       $uicols['name'][]                       = 'this_index';
+                       $uicols['descr'][]                      = lang('index');
+
+                       $uicols['name'][]                       = 'index_count';
+                       $uicols['descr'][]                      = 
lang('index_count');
+
+                       $uicols['name'][]                       = 'index_date';
+                       $uicols['descr'][]                      = lang('Date');
+
+                       if ($order)
+                       {
+                               $ordermethod = " order by $entity_table.$order 
$sort";
+                       }
+                       else
+                       {
+                               $ordermethod = " order by 
$entity_table.activity_id DESC";
+                       }
+
+
+                       $from .= " FROM $paranthesis $entity_table ";
+
+                       $sql = "SELECT $cols $from $joinmethod";
+
+                       $this->uicols   = $uicols;
+
+                       $where= 'WHERE';
+
+                       if ($agreement_id)
+                       {
+                               $filtermethod .= " $where 
$entity_table.agreement_id=$agreement_id";
+                               $filtermethod .= " AND current_index = 1";
+                               $where= 'AND';
+                       }
+
+                       if ($cat_id)
+                       {
+                               $filtermethod .= " $where 
$entity_table.category='$cat_id' ";
+                               $where= 'AND';
+                       }
+
+                       if ($vendor_id)
+                       {
+                               $filtermethod .= " $where 
$entity_table.vendor_id='$vendor_id' ";
+                               $where= 'AND';
+                       }
+
+                       if ($member_id>0)
+                       {
+                               $filtermethod .= " $where fm_vendor.member_of 
$this->like '%,$member_id,%' ";
+                               $where= 'AND';
+                       }
+
+                       if ($status)
+                       {
+                               $filtermethod .= " $where 
$entity_table.status='$status' ";
+                               $where= 'AND';
+                       }
+
+
+                       $sql .= " $filtermethod";
+//echo $sql;
+
+                       $this->db2->query($sql,__LINE__,__FILE__);
+                       $this->total_records = $this->db2->num_rows();
+                       if(!$allrows)
+                       {
+                               $this->db->limit_query($sql . 
$ordermethod,$start,__LINE__,__FILE__);
+                       }
+                       else
+                       {
+                               $this->db->query($sql . 
$ordermethod,__LINE__,__FILE__);
+                       }
+
+                       $dateformat = 
$GLOBALS['phpgw_info']['user']['preferences']['common']['dateformat'];
+
+                       while ($this->db->next_record())
+                       {
+                               $details[] = array
+                               (
+                                       'agreement_id'          => 
$this->db->f('agreement_id'),
+                                       'activity_id'           => 
$this->db->f('activity_id'),
+                                       'id'                            => 
$this->db->f('id'),
+                                       'num'                           => 
$this->db->f('num'),
+                                       'descr'                         => 
$this->db->f('descr'),
+                                       'unit'                          => 
$this->db->f('unit'),
+                                       'm_cost'                        => 
$this->db->f('m_cost'),
+                                       'w_cost'                        => 
$this->db->f('w_cost'),
+                                       'total_cost'            => 
$this->db->f('total_cost'),
+                                       'this_index'            => 
$this->db->f('this_index'),
+                                       'index_count'           => 
$this->db->f('index_count'),
+                                       'index_date'            => 
$GLOBALS['phpgw']->common->show_date($this->db->f('index_date'),$dateformat)
+                               );
+                       }
+//html_print_r($details);
+                       return $details;
+               }
+
+               function read_prizing($data)
+               {
+                       if(is_array($data))
+                       {
+                               $agreement_id   = 
(isset($data['agreement_id'])?$data['agreement_id']:0);
+                               $activity_id    = 
(isset($data['activity_id'])?$data['activity_id']:0);
+                       }
+
+                       $entity_table = 'fm_activity_price_index';
+
+                       $cols = 
"fm_activity_price_index.m_cost,fm_activity_price_index.w_cost,fm_activity_price_index.total_cost,"
+                               . " 
fm_activity_price_index.index_count,fm_activity_price_index.index_date,fm_activity_price_index.activity_id,fm_activity_price_index.this_index";
+
+                       $cols_return[]                  = 'activity_id';
+                       $uicols['input_type'][]         = 'text';
+                       $uicols['name'][]                       = 'activity_id';
+                       $uicols['descr'][]                      = 
lang('activity ID');
+                       $uicols['statustext'][]         = lang('activity ID');
+
+                       $cols_return[]                  = 'id';
+                       $uicols['input_type'][]         = 'hidden';
+                       $uicols['name'][]                       = 'id';
+                       $uicols['descr'][]                      = False;
+                       $uicols['statustext'][]         = False;
+
+                       $cols_return[]                  = 'm_cost';
+                       $uicols['input_type'][]         = 'text';
+                       $uicols['name'][]                       = 'm_cost';
+                       $uicols['descr'][]                      = 
lang('m_cost');
+                       $uicols['statustext'][]         = lang('m_cost');
+                       $cols_return[]                  = 'w_cost';
+                       $uicols['input_type'][]         = 'text';
+                       $uicols['name'][]                       = 'w_cost';
+                       $uicols['descr'][]                      = 
lang('w_cost');
+                       $uicols['statustext'][]         = lang('w_cost');
+                       $cols_return[]                  = 'total_cost';
+                       $uicols['input_type'][]         = 'text';
+                       $uicols['name'][]                       = 'total_cost';
+                       $uicols['descr'][]                      = 
lang('total_cost');
+                       $uicols['statustext'][]         = lang('total_cost');
+
+                       $cols_return[]                  = 'this_index';
+                       $uicols['input_type'][]         = 'text';
+                       $uicols['name'][]                       = 'this_index';
+                       $uicols['descr'][]                      = lang('index');
+                       $uicols['statustext'][]         = lang('index');
+
+                       $cols_return[]                  = 'index_count';
+                       $uicols['input_type'][]         = 'text';
+                       $uicols['name'][]                       = 'index_count';
+                       $uicols['descr'][]                      = 
lang('index_count');
+                       $uicols['statustext'][]         = lang('index_count');
+
+                       $cols_return[]                  = 'index_date';
+                       $uicols['input_type'][]         = 'text';
+                       $uicols['name'][]                       = 'index_date';
+                       $uicols['descr'][]                      = lang('date');
+                       $uicols['statustext'][]         = lang('date');
+
+                       $from .= " FROM $entity_table ";
+
+                       $sql = "SELECT $cols $from $joinmethod";
+
+
+                       $this->uicols   = $uicols;
+
+                       $ordermethod = " order by $entity_table.index_count 
ASC";
+
+                       $where= 'WHERE';
+
+
+                       if ($agreement_id)
+                       {
+                               $filtermethod .= " $where 
$entity_table.agreement_id=$agreement_id AND activity_id=$activity_id";
+                               $where= 'AND';
+                       }
+
+
+                       $sql .= " $filtermethod";
+//echo $sql;
+
+                       $this->db2->query($sql,__LINE__,__FILE__);
+                       $this->total_records = $this->db2->num_rows();
+                       if(!$allrows)
+                       {
+                               $this->db->limit_query($sql . 
$ordermethod,$start,__LINE__,__FILE__);
+                       }
+                       else
+                       {
+                               $this->db->query($sql . 
$ordermethod,__LINE__,__FILE__);
+                       }
+
+                       $j=0;
+                       $n=count($cols_return);
+//_debug_array($cols_return);
+                       while ($this->db->next_record())
+                       {
+                               for ($i=0;$i<$n;$i++)
+                               {
+                                       $agreement_list[$j][$cols_return[$i]] = 
$this->db->f($cols_return[$i]);
+                                       $agreement_list[$j]['agreement_id'] = 
$agreement_id;
+                               }
+                               $j++;
+                       }
+//_debug_array($agreement_list);
+                       return $agreement_list;
+               }
+
+
+               function read_single($data)
+               {
+                       $attribute_table = 'fm_agreement_attribute';
+                       $table = 'fm_agreement';
+
+                       $agreement_id =$data['agreement_id'];
+
+                       $this->db->query("SELECT * FROM $attribute_table WHERE 
attrib_detail = 1 ORDER BY attrib_sort");
+
+                       while ($this->db->next_record())
+                       {
+                               $agreement['attributes'][] = array
+                               (
+                                       'attrib_id'             => 
$this->db->f('id'),
+                                       'name'                  => 
$this->db->f('column_name'),
+                                       'input_text'    => 
stripslashes($this->db->f('input_text')),
+                                       'statustext'    => 
stripslashes($this->db->f('statustext')),
+                                       'datatype'              => 
$this->db->f('datatype')
+                               );
+                       }
+
+                       if($agreement_id)
+                       {
+                               $this->db->query("SELECT 
$table.*,fm_vendor.member_of FROM $table $this->join fm_vendor ON 
$table.vendor_id = fm_vendor.id where $table.id='$agreement_id'");
+
+                               if($this->db->next_record())
+                               {
+                                       $agreement['id']                        
= (int)$this->db->f('id');
+                                       $agreement['entry_date']        = 
$this->db->f('entry_date');
+                                       $agreement['cat_id']            = 
$this->db->f('category');
+                                       $agreement['start_date']        = 
$this->db->f('start_date');
+                                       $agreement['end_date']          = 
$this->db->f('end_date');
+                                       $agreement['termination_date']= 
$this->db->f('termination_date');
+                                       $agreement['vendor_id']         = 
$this->db->f('vendor_id');
+                                       $agreement['b_account_id']      = 
$this->db->f('account_id');
+                                       $agreement['name']                      
= stripslashes($this->db->f('name'));
+                                       $agreement['descr']                     
= stripslashes($this->db->f('descr'));
+                                       $agreement['user_id']           = 
$this->db->f('user_id');
+                                       $agreement['group_id']          = 
$this->db->f('group_id');
+                                       $agreement['status']            = 
$this->db->f('status');
+                                       $agreement['member_of']         = 
explode(',',$this->db->f('member_of'));
+
+                                       for 
($i=0;$i<count($agreement['attributes']);$i++)
+                                       {
+                                               
$agreement['attributes'][$i]['value']   = 
$this->db->f($agreement['attributes'][$i]['name']);
+                                               
$agreement['attributes'][$i]['datatype_text']   = 
$this->bocommon->translate_datatype($agreement['attributes'][$i]['datatype']);
+                                       }
+
+                               }
+                       }
+//_debug_array($agreement);
+                       return $agreement;
+               }
+
+               function read_single_item($data)
+               {
+                       $attribute_table = 'fm_agreement_attribute';
+                       $table = 'fm_activities';
+
+                       $agreement_id =$data['agreement_id'];
+                       $id =$data['id'];
+
+                       $this->db->query("SELECT * FROM $attribute_table WHERE 
attrib_detail = 2 ORDER BY attrib_sort");
+
+                       while ($this->db->next_record())
+                       {
+                               $item['attributes'][] = array
+                               (
+                                       'attrib_id'             => 
$this->db->f('id'),
+                                       'name'                  => 
$this->db->f('column_name'),
+                                       'input_text'    => 
stripslashes($this->db->f('input_text')),
+                                       'statustext'    => 
stripslashes($this->db->f('statustext')),
+                                       'datatype'              => 
$this->db->f('datatype')
+                               );
+                       }
+
+                       if($id && $agreement_id)
+                       {
+                               $this->db->query("SELECT * from $table 
$this->join fm_activity_price_index on $table.id = 
fm_activity_price_index.activity_id where $table.id=$id AND 
agreement_id=$agreement_id and index_count = 1");
+
+                               if($this->db->next_record())
+                               {
+                                       $item['agreement_id']   = 
(int)$this->db->f('agreement_id');
+                                       $item['id']                             
= (int)$this->db->f('id');
+                                       $item['num']                    = 
$this->db->f('num');
+                                       $item['entry_date']             = 
$this->db->f('entry_date');
+                                       $item['m_cost']                 = 
$this->db->f('m_cost');
+                                       $item['w_cost']                 = 
$this->db->f('w_cost');
+                                       $item['total_cost']             = 
$this->db->f('total_cost');
+
+                                       for 
($i=0;$i<count($item['attributes']);$i++)
+                                       {
+                                               
$item['attributes'][$i]['value']        = 
$this->db->f($item['attributes'][$i]['name']);
+                                               
$item['attributes'][$i]['datatype_text']        = 
$this->bocommon->translate_datatype($item['attributes'][$i]['datatype']);
+                                       }
+
+                               }
+                       }
+                       return $item;
+               }
+
+               function add($agreement,$values_attribute='')
+               {
+//_debug_array($agreement);
+                       $table = 'fm_agreement';
+                       $agreement['name'] = 
$this->db->db_addslashes($agreement['name']);
+                       $agreement['descr'] = 
$this->db->db_addslashes($agreement['descr']);
+
+                       if($agreement['member_of'])
+                       {
+                               $agreement['member_of']=',' . 
implode(',',$agreement['member_of']) . ',';
+                       }
+
+                       $this->db->transaction_begin();
+                       $id = $this->bocommon->next_id($table);
+                       $vals[] = $id;
+                       $vals[] = $agreement['name'];
+                       $vals[] = $agreement['descr'];
+                       $vals[] = time();
+                       $vals[] = $agreement['cat_id'];
+                       $vals[] = $agreement['start_date'];
+                       $vals[] = $agreement['end_date'];
+                       $vals[] = $agreement['termination_date'];
+                       $vals[] = $agreement['vendor_id'];
+                       $vals[] = $this->account;
+
+
+                       while (is_array($agreement['extra']) && 
list($input_name,$value) = each($agreement['extra']))
+                       {
+                               if($value)
+                               {
+                                       $cols[] = $input_name;
+                                       $vals[] = $value;
+                               }
+                       }
+
+                       if (isset($values_attribute) AND 
is_array($values_attribute))
+                       {
+                               foreach($values_attribute as $entry)
+                               {
+                                       if($entry['value'])
+                                       {
+                                               $cols[] = $entry['name'];
+                                               $vals[] = $entry['value'];
+                                       }
+                               }
+                       }
+
+                       $cols[] = 'group_id';
+                       $vals[] = $agreement['group_id'];
+                       $cols[] = 'status';
+                       $vals[] = $agreement['status'];
+
+                       if($cols)
+                       {
+                               $cols   = "," . implode(",", $cols);
+                               $vals   = 
$this->bocommon->validate_db_insert($vals);
+                       }
+
+                       $this->db->query("INSERT INTO $table 
(id,name,descr,entry_date,category,start_date,end_date,termination_date,vendor_id,user_id
 $cols) "
+                               . "VALUES ($vals)",__LINE__,__FILE__);
+
+                       $receipt['agreement_id']= 
$id;//$this->db->get_last_insert_id($table,'id');
+
+                       $receipt['message'][] = array('msg'=>lang('agreement %1 
has been saved',$receipt['agreement_id']));
+
+                       $this->db->transaction_commit();
+                       return $receipt;
+               }
+
+               function add_item($values,$values_attribute='')
+               {
+//_debug_array($values);
+
+                       $this->db->transaction_begin();
+
+                       $this->db->query("SELECT start_date FROM fm_agreement 
WHERE id=" . $values['agreement_id']);
+                       $this->db->next_record();
+                       $start_date     = $this->db->f('start_date');
+
+                       $agreement_id = $values['agreement_id'];
+                       $activity_id = $values['id'];
+                       $m_cost = $this->floatval($values['m_cost']);
+                       $w_cost = $this->floatval($values['w_cost']);
+                       $total_cost = $this->floatval($values['total_cost']);
+                       $entry_date = time();
+
+
+                       $sql = "UPDATE fm_activity_price_index SET "
+                               . " index_count = 1,current_index = 
1,this_index = 1,"
+                               . " m_cost = $m_cost,w_cost = $w_cost 
,total_cost = $total_cost ,index_date = $start_date,"
+                               . " entry_date = $entry_date ,user_id =" . 
$this->account . " WHERE agreement_id = $agreement_id AND activity_id = 
$activity_id AND index_count = -1";
+
+                       $this->db->query($sql);
+
+                       $receipt['agreement_id']= $values['agreement_id'];
+                       $receipt['id']= $values['id'];
+
+                       $receipt['message'][] = array('msg'=>lang('activity %1 
has been saved',$receipt['id']));
+
+                       $this->db->transaction_commit();
+                       return $receipt;
+               }
+
+               function edit($values,$values_attribute='')
+               {
+//_debug_array($values);
+//_debug_array($values_attribute);
+                       $table = 'fm_agreement';
+
+                       $values['name'] = 
$this->db->db_addslashes($values['name']);
+
+                       if($values['member_of'])
+                       {
+                               $values['member_of']=',' . 
implode(',',$values['member_of']) . ',';
+                       }
+
+                       while (is_array($values['extra']) && 
list($column,$value) = each($values['extra']))
+                       {
+                               $value_set[$column]     = $value;
+                       }
+
+                       if (isset($values_attribute) AND 
is_array($values_attribute))
+                       {
+                               foreach($values_attribute as $entry)
+                               {
+                                       if($entry['datatype']!='AB' && 
$entry['datatype']!='VENDOR')
+                                       {
+                                               $value_set[$entry['name']]      
= $entry['value'];
+                                       }
+                               }
+                       }
+
+                       $value_set['name']      = $values['name'];
+                       $value_set['descr']     = $values['descr'];
+                       $value_set['group_id']  = $values['group_id'];
+                       $value_set['status']    = $values['status'];
+                       if($value_set)
+                       {
+                               $value_set      = ',' . 
$this->bocommon->validate_db_update($value_set);
+                       }
+
+                       $this->db->query("UPDATE $table set entry_date='" . 
time() . "', category='"
+                                                       . $values['cat_id'] . 
"', start_date=" . intval($values['start_date']) . ", end_date=" . 
intval($values['end_date']) . ", termination_date=" . 
intval($values['termination_date']) . "$value_set WHERE id=" . 
intval($values['agreement_id']));
+
+                       $this->db->query("UPDATE fm_activity_price_index set 
index_date=" . intval($values['start_date']) . " WHERE index_count=1 AND 
agreement_id= " . intval($values['agreement_id']));
+
+                       $receipt['agreement_id']= $values['agreement_id'];
+                       $receipt['message'][] = array('msg'=>lang('agreement %1 
has been edited',$values['agreement_id']));
+                       return $receipt;
+               }
+
+               function edit_item($values)
+               {
+//_debug_array($values);
+
+                       $value_set['m_cost']            = $values['m_cost'];
+                       $value_set['w_cost']            = $values['w_cost'];
+                       $value_set['total_cost']        = $values['total_cost'];
+
+                       if($value_set)
+                       {
+                               $value_set      = ',' . 
$this->bocommon->validate_db_update($value_set);
+                       }
+
+                       $this->db->query("UPDATE fm_activity_price_index set 
entry_date=" . time() . "$value_set WHERE agreement_id=" . 
intval($values['agreement_id']) . ' AND activity_id=' . intval($values['id']));
+
+                       $this->db->query("UPDATE fm_activity_price_index  set 
m_cost = this_index *" . $this->floatval($values['m_cost']) . ",w_cost = 
this_index *" . $this->floatval($values['w_cost']) . ",total_cost = this_index 
*" . $this->floatval($values['total_cost']) . "  WHERE agreement_id=" . 
intval($values['agreement_id']) . ' AND activity_id=' . intval($values['id']));
+
+                       $receipt['agreement_id']= $values['agreement_id'];
+                       $receipt['id']= $values['id'];
+                       $receipt['message'][] = array('msg'=>lang('Activity %1 
has been edited',$values['id']));
+                       return $receipt;
+               }
+
+               function update($values)
+               {
+//_debug_array($values);
+                       
$values['new_index']=$this->floatval($values['new_index']);
+                       $this->db->transaction_begin();
+
+                       while (is_array($values['select']) && 
list(,$activity_id) = each($values['select']))
+                       {
+
+                               if($values['id'][$activity_id]>0)
+                               {
+                                       $this->db->query("UPDATE 
fm_activity_price_index set current_index = NULL WHERE agreement_id=" . 
intval($values['agreement_id']) . ' AND activity_id=' . intval($activity_id));
+
+                                       $this->db->query("INSERT INTO 
fm_activity_price_index 
(agreement_id,activity_id,index_count,current_index,this_index,m_cost,w_cost,total_cost,index_date,entry_date,user_id)"
+                                               . "VALUES (" . 
$values['agreement_id'] . "," . $activity_id ."," . 
($values['id'][$activity_id]+1) .",1,'" . $values['new_index'] . "','" . 
($values['m_cost'][$activity_id] * $values['new_index']) . "','" . 
($values['w_cost'][$activity_id] * $values['new_index']) . "','" . 
($values['total_cost'][$activity_id] * $values['new_index'])  . "'," . 
(int)$values['date'] . "," . time()
+                                               . "," . $this->account . ")");
+
+                                       $receipt['message'][] = 
array('msg'=>lang('Activity %1 has been updated for index',$activity_id));
+                               }
+
+                       }
+
+                       $this->db->transaction_commit();
+
+                       return $receipt;
+               }
+
+               function floatval($strValue)
+               {
+                       $floatValue = 
ereg_replace("(^[0-9]*)(\\.|,)([0-9]*)(.*)", "\\1.\\3", $strValue);
+                       if(!is_numeric($floatValue))
+                       {
+                               $floatValue = ereg_replace("(^[0-9]*)(.*)", 
"\\1", $strValue);
+                       }
+                       if(!is_numeric($floatValue))
+                       {
+                               $floatValue = 0;
+                       }
+                       return $floatValue;
+               }
+
+
+               function delete_last_index($agreement_id,$activity_id)
+               {
+                       $this->db->transaction_begin();
+                       $this->db->query("SELECT max(index_count) as 
index_count FROM fm_activity_price_index WHERE agreement_id=$agreement_id AND 
activity_id=$activity_id");
+                       $this->db->next_record();
+                       $index_count    = $this->db->f('index_count');
+                       if($index_count>1)
+                       {
+                               $this->db->query("DELETE FROM 
fm_activity_price_index WHERE agreement_id=$agreement_id AND 
activity_id=$activity_id AND index_count=$index_count");
+                               $this->db->query("UPDATE 
fm_activity_price_index set current_index = 1 WHERE agreement_id=$agreement_id 
AND activity_id=$activity_id AND index_count =" . ($index_count-1));
+                       }
+                       else
+                       {
+                               $sql = "UPDATE fm_activity_price_index SET "
+                                       . " index_count = -1,current_index = 
1,this_index = 1,"
+                                       . " m_cost = NULL,w_cost = NULL 
,total_cost = NULL ,index_date = NULL,"
+                                       . " entry_date = NULL ,user_id =" . 
$this->account . " WHERE agreement_id = $agreement_id AND activity_id = 
$activity_id";
+
+                               $this->db->query($sql);
+
+                       }
+                       $this->db->transaction_commit();
+               }
+
+               function delete_item($agreement_id,$activity_id)
+               {
+                       $this->db->transaction_begin();
+                       $this->db->query("DELETE FROM fm_activity_price_index 
WHERE agreement_id=$agreement_id AND activity_id=$activity_id");
+                       $this->db->transaction_commit();
+               }
+
+               function delete($agreement_id)
+               {
+                       $this->db->transaction_begin();
+                       $this->db->query("DELETE FROM fm_agreement WHERE id=" . 
intval($agreement_id));
+                       $this->db->query("DELETE FROM fm_activity_price_index 
WHERE agreement_id=" . intval($agreement_id));
+                       $this->db->transaction_commit();
+               }
+
+               function read_attrib($data)
+               {
+                       $attribute_table = 'fm_agreement_attribute';
+//html_print_r($data);
+                       if(is_array($data))
+                       {
+                               if ($data['start'])
+                               {
+                                       $start=$data['start'];
+                               }
+                               else
+                               {
+                                       $start=0;
+                               }
+                               $query = 
(isset($data['query'])?$data['query']:'');
+                               $sort = 
(isset($data['sort'])?$data['sort']:'DESC');
+                               $order = 
(isset($data['order'])?$data['order']:'');
+                               $allrows = 
(isset($data['allrows'])?$data['allrows']:'');
+                               $column_list = 
(isset($data['column_list'])?$data['column_list']:'');
+                       }
+
+                       $where = 'WHERE';
+                       if ($column_list)
+                       {
+                               $filtermethod = " $where list !=1 or list is 
null";
+                               $where = 'AND';
+                       }
+                       if ($this->role=='detail')
+                       {
+                               $filtermethod .= " $where attrib_detail=2 ";
+                       }
+                       else
+                       {
+                               $filtermethod .= " $where attrib_detail=1 ";
+                       }
+
+                       $where = 'AND';
+
+                       if ($order)
+                       {
+                               $ordermethod = " order by $order $sort";
+
+                       }
+                       else
+                       {
+                               $ordermethod = ' order by attrib_sort asc';
+                       }
+
+                       if($query)
+                       {
+                               $query = ereg_replace("'",'',$query);
+                               $query = ereg_replace('"','',$query);
+
+                               $querymethod = " $where 
($attribute_table.input_text $this->like '%$query%' or 
$attribute_table.column_name $this->like '%$query%')";
+                       }
+
+                       $sql = "SELECT * FROM $attribute_table $filtermethod 
$querymethod";
+
+//echo $sql;
+                       $this->db2->query($sql,__LINE__,__FILE__);
+                       $this->total_records = $this->db2->num_rows();
+                       if(!$allrows)
+                       {
+                               $this->db->limit_query($sql . 
$ordermethod,$start,__LINE__,__FILE__);
+                       }
+                       else
+                       {
+                               $this->db->query($sql . 
$ordermethod,__LINE__,__FILE__);
+                       }
+
+                       while ($this->db->next_record())
+                       {
+                               $attrib[] = array
+                               (
+                                       'id'                    => 
$this->db->f('id'),
+                                       'attrib_sort'   => 
$this->db->f('attrib_sort'),
+                                       'list'                  => 
$this->db->f('list'),
+                                       'lookup_form'   => 
$this->db->f('lookup_form'),
+                                       'column_name'   => 
$this->db->f('column_name'),
+                                       'name'                  => 
$this->db->f('input_text'),
+                                       'size'                  => 
$this->db->f('size'),
+                                       'statustext'    => 
$this->db->f('statustext'),
+                                       'input_text'    => 
$this->db->f('input_text'),
+                                       'type_name'             => 
$this->db->f('type'),
+                                       'datatype'              => 
$this->db->f('datatype'),
+                                       'search'                => 
$this->db->f('search')
+                               );
+                       }
+                       return $attrib;
+               }
+
+               function read_single_attrib($id)
+               {
+                       $attribute_table = 'fm_agreement_attribute';
+
+                       if ($this->role=='detail')
+                       {
+                               $filtermethod = " AND attrib_detail=2 ";
+                       }
+                       else
+                       {
+                               $filtermethod = " AND attrib_detail=1 ";
+                       }
+
+                       $sql = "SELECT * FROM $attribute_table where id=$id 
$filtermethod";
+
+                       $this->db->query($sql);
+
+                       if($this->db->next_record())
+                       {
+                               $attrib['id']                                   
        = $this->db->f('id');
+                               $attrib['column_name']                          
= $this->db->f('column_name');
+                               $attrib['input_text']                           
= $this->db->f('input_text');
+                               $attrib['statustext']                           
= $this->db->f('statustext');
+                               $attrib['column_info']['precision']     = 
$this->db->f('precision_');
+                               $attrib['column_info']['scale']         = 
$this->db->f('scale');
+                               $attrib['column_info']['default']       = 
$this->db->f('default_value');
+                               $attrib['column_info']['nullable']      = 
$this->db->f('nullable');
+                               $attrib['column_info']['type']          = 
$this->db->f('datatype');
+                               $attrib['type_name']                            
= $this->db->f('type_name');
+                               $attrib['lookup_form']                          
= $this->db->f('lookup_form');
+                               $attrib['list']                                 
        = $this->db->f('list');
+                               $attrib['search']                               
        = $this->db->f('search');
+                               if($this->db->f('datatype')=='R' || 
$this->db->f('datatype')=='CH' || $this->db->f('datatype')=='LB')
+                               {
+                                       $attrib['choice'] = 
$this->read_attrib_choice($id);
+                               }
+
+                               return $attrib;
+                       }
+               }
+
+               function read_attrib_choice($attrib_id)
+               {
+                       $choice_table = 'fm_agreement_choice';
+
+                       if ($this->role=='detail')
+                       {
+                               $filtermethod = " AND attrib_detail=2 ";
+                       }
+                       else
+                       {
+                               $filtermethod = " AND attrib_detail=1 ";
+                       }
+
+                       $sql = "SELECT * FROM $choice_table WHERE 
attrib_id=$attrib_id $filtermethod";
+                       $this->db->query($sql);
+
+                       while ($this->db->next_record())
+                       {
+                               $choice[] = array
+                               (
+                                       'id'    => $this->db->f('id'),
+                                       'value' => $this->db->f('value')
+                               );
+
+                       }
+                       return $choice;
+               }
+
+               function add_attrib($attrib)
+               {
+                       $attribute_table = 'fm_agreement_attribute';
+                       $attrib['column_name'] = 
$this->db->db_addslashes($attrib['column_name']);
+                       $attrib['input_text'] = 
$this->db->db_addslashes($attrib['input_text']);
+                       $attrib['statustext'] = 
$this->db->db_addslashes($attrib['statustext']);
+                       $attrib['default'] = 
$this->db->db_addslashes($attrib['default']);
+                       $attrib['id'] = 
$this->bocommon->next_id($attribute_table, array('attrib_detail'=>!!$this->role 
+1));
+
+                       if($this->role=='detail')
+                       {
+                               $filtermethod= 'WHERE attrib_detail=2';
+                               $table = 'fm_agreement_detail';
+                       }
+                       else
+                       {
+                               $filtermethod= 'WHERE attrib_detail=1';
+                               $table = 'fm_agreement';
+                       }
+                       $sql = "SELECT max(attrib_sort) as max_sort FROM 
$attribute_table $filtermethod";
+                       $this->db->query($sql);
+                       $this->db->next_record();
+                       $attrib_sort    = $this->db->f('max_sort')+1;
+
+                       $values= array(
+                               !!$this->role +1,
+                               $attrib['id'],
+                               $attrib['column_name'],
+                               $attrib['input_text'],
+                               $attrib['statustext'],
+                               $attrib['lookup_form'],
+                               $attrib['search'],
+                               $attrib['list'],
+                               $attrib_sort,
+                               $attrib['column_info']['type'],
+                               $attrib['column_info']['precision'],
+                               $attrib['column_info']['scale'],
+                               $attrib['column_info']['default'],
+                               $attrib['column_info']['nullable']
+                               );
+
+                       $values = $this->bocommon->validate_db_insert($values);
+
+                       $this->db->transaction_begin();
+
+                       $this->db->query("INSERT INTO $attribute_table 
(attrib_detail,id,column_name, input_text, 
statustext,lookup_form,search,list,attrib_sort,datatype,precision_,scale,default_value,nullable)
 "
+                               . "VALUES ($values)");
+
+                       $receipt['id']= $attrib['id'];
+
+                       if($attrib['column_info']['type']=='email' && 
!$attrib['column_info']['precision'])
+                       {
+                               $attrib['column_info']['precision']=64;
+                       }
+
+                       $attrib['column_info']['type']  = 
$this->bocommon->translate_datatype_insert($attrib['column_info']['type']);
+
+                       if($attrib['column_info']['type']=='int' && 
!$attrib['column_info']['precision'])
+                       {
+                               $attrib['column_info']['precision']=4;
+                       }
+
+                       if(!$attrib['column_info']['default'])
+                       {
+                               unset($attrib['column_info']['default']);
+                       }
+
+                       $this->init_process();
+
+                       
if($this->oProc->AddColumn($table,$attrib['column_name'], 
$attrib['column_info']))
+                       {
+                               $receipt['message'][] = array('msg'     => 
lang('Attribute has been saved')     );
+                               $this->db->transaction_commit();
+
+                       }
+                       else
+                       {
+                               $receipt['error'][] = array('msg'       => 
lang('column could not be added')    );
+                               if($this->db->Transaction)
+                               {
+                                       $GLOBALS['phpgw']->db->rollbacktrans();
+                               }
+                               else
+                               {
+                                       $GLOBALS['phpgw']->db->Execute("DELETE 
FROM $attribute_table WHERE id='" . $receipt['id'] . "'");
+                                       unset($receipt['id']);
+
+                               }
+                       }
+
+                       return $receipt;
+               }
+
+               function init_process()
+               {
+                       $this->oProc                                            
= 
CreateObject('phpgwapi.schema_proc',$GLOBALS['phpgw_info']['server']['db_type']);
+                       $this->oProc->m_odb                                     
= $this->db;
+                       $this->oProc->m_odb->Halt_On_Error      = 'report';
+               }
+
+               function edit_attrib($attrib)
+               {
+                       $attribute_table = 'fm_agreement_attribute';
+                       $table = 'fm_agreement';
+
+                       $attrib['column_name'] = 
$this->db->db_addslashes($attrib['column_name']);
+                       $attrib['input_text'] = 
$this->db->db_addslashes($attrib['input_text']);
+                       $attrib['statustext'] = 
$this->db->db_addslashes($attrib['statustext']);
+                       $attrib['default'] = 
$this->db->db_addslashes($attrib['default']);
+
+                       $choice_table = 'fm_agreement_choice';
+
+                       if($this->role=='detail')
+                       {
+                               $filtermethod= ' AND attrib_detail=2';
+                               $table = 'fm_agreement_detail';
+                       }
+                       else
+                       {
+                               $filtermethod= ' AND attrib_detail=1';
+                               $table = 'fm_agreement';
+                       }
+
+                       $this->db->query("SELECT column_name FROM 
$attribute_table WHERE id='" . $attrib['id']. "' $filtermethod");
+                       $this->db->next_record();
+                       $OldColumnName          = $this->db->f('column_name');
+
+                       $this->db->transaction_begin();
+
+                       $attrib_type=$attrib['column_info']['type'];
+                       if(($OldColumnName !=$attrib['column_name']) || 
($attrib['column_info']['type'] != $this->db->f('datatype')))
+                       {
+                               $value_set=array(
+                                       'column_name'           => 
$attrib['column_name'],
+                                       'input_text'            => 
$attrib['input_text'],
+                                       'statustext'            => 
$attrib['statustext'],
+                                       'lookup_form'           => 
$attrib['lookup_form'],
+                                       'search'                        => 
$attrib['search'],
+                                       'list'                          => 
$attrib['list'],
+                                       'datatype'                      => 
$attrib['column_info']['type'],
+                                       'precision_'            => 
$attrib['column_info']['precision'],
+                                       'scale'                         => 
$attrib['column_info']['scale'],
+                                       'default_value'         => 
$attrib['column_info']['default'],
+                                       'nullable'                      => 
$attrib['column_info']['nullable']
+                                       );
+
+                               $value_set      = 
$this->bocommon->validate_db_update($value_set);
+
+                               $this->db->query("UPDATE $attribute_table set 
$value_set WHERE id=" . $attrib['id'] . $filtermethod);
+
+                               if($attrib['column_info']['type']=='email' && 
!$attrib['column_info']['precision'])
+                               {
+                                       $attrib['column_info']['precision']=64;
+                               }
+
+                               $attrib['column_info']['type']  = 
$this->bocommon->translate_datatype_insert($attrib['column_info']['type']);
+
+                               if($attrib['column_info']['type']=='int' && 
!$attrib['column_info']['precision'])
+                               {
+                                       $attrib['column_info']['precision']=4;
+                               }
+
+                               if(!$attrib['column_info']['default'])
+                               {
+                                       
unset($attrib['column_info']['default']);
+                               }
+
+                               $this->init_process();
+
+                               
$this->oProc->AlterColumn($table,$OldColumnName,$attrib['column_name'],$attrib['column_info']);
+
+                       }
+
+
+                       if($attrib['new_choice'])
+                       {
+                               $this->db->query("SELECT max(id) as id FROM 
$choice_table WHERE attrib_id='" . $attrib['id']. "' $filtermethod");
+                               $this->db->next_record();
+                               $choice_id              = $this->db->f('id')+1;
+
+       //                      $choice_id = 
$this->bocommon->next_id($choice_table 
,array('attrib_detail'=>2,'attrib_id'=>$attrib['id']));
+
+                               $values= array(
+                                       $attrib['id'],
+                                       $choice_id,
+                                       !!$this->role +1,
+                                       $attrib['new_choice']
+                                       );
+
+                               $values = 
$this->bocommon->validate_db_insert($values);
+
+                               $this->db->query("INSERT INTO $choice_table 
(attrib_id,id,attrib_detail,value) "
+                               . "VALUES ($values)");
+                       }
+
+
+                       if($attrib['delete_choice'])
+                       {
+                               for 
($i=0;$i<count($attrib['delete_choice']);$i++)
+                               {
+                                       $this->db->query("DELETE FROM 
$choice_table WHERE  attrib_id=" . $attrib['id']  ." AND id=" . 
$attrib['delete_choice'][$i] . $filtermethod);
+                               }
+                       }
+
+//html_print_r($attrib_type);
+                       if($attrib_type!='R' && $attrib_type!='CH' && 
$attrib_type!='LB')
+                       {
+                               $this->db->query("DELETE FROM $choice_table 
WHERE  attrib_id=" . $attrib['id'] . $filtermethod);
+                       }
+
+
+                       $this->db->transaction_commit();
+
+                       $receipt['message'][] = array('msg'     => 
lang('Attribute has been edited'));
+
+
+                       return $receipt;
+
+               }
+               function resort_attrib($data)
+               {
+//html_print_r($data);
+                       $attribute_table = 'fm_agreement_attribute';
+                       if(is_array($data))
+                       {
+                               $resort = 
(isset($data['resort'])?$data['resort']:'up');
+                               $id = (isset($data['id'])?$data['id']:'');
+                       }
+
+                       $sql = "SELECT attrib_sort FROM $attribute_table where 
id=$id";
+                       $this->db->query($sql);
+                       $this->db->next_record();
+                       $attrib_sort    = $this->db->f('attrib_sort');
+                       $sql = "SELECT max(attrib_sort) as max_sort FROM 
$attribute_table";
+                       $this->db->query($sql);
+                       $this->db->next_record();
+                       $max_sort       = $this->db->f('max_sort');
+                       switch($resort)
+                       {
+                               case 'up':
+                                       if($attrib_sort>1)
+                                       {
+                                               $sql = "UPDATE $attribute_table 
set attrib_sort=$attrib_sort WHERE attrib_sort =" . ($attrib_sort-1);
+                                               $this->db->query($sql);
+                                               $sql = "UPDATE $attribute_table 
set attrib_sort=" . ($attrib_sort-1) ." WHERE id=$id";
+                                               $this->db->query($sql);
+                                       }
+                                       break;
+                               case 'down':
+                                       if($max_sort > $attrib_sort)
+                                       {
+                                               $sql = "UPDATE $attribute_table 
set attrib_sort=$attrib_sort WHERE attrib_sort =" . ($attrib_sort+1);
+                                               $this->db->query($sql);
+                                               $sql = "UPDATE $attribute_table 
set attrib_sort=" . ($attrib_sort+1) ." WHERE id=$id";
+                                               $this->db->query($sql);
+                                       }
+                                       break;
+                               default:
+                                       return;
+                                       break;
+                       }
+               }
+               function delete_attrib($attrib_id)
+               {
+                       $table = 'fm_agreement';
+                       $attribute_table = 'fm_agreement_attribute';
+                       $this->init_process();
+
+                       $sql = "SELECT * FROM $attribute_table WHERE 
id=$attrib_id";
+
+                       $this->db->query($sql);
+                       $this->db->next_record();
+                       $ColumnName             = $this->db->f('column_name');
+
+                       if($this->oProc->DropColumn($table,'', $ColumnName))
+                       {
+                               $sql = "SELECT attrib_sort FROM 
$attribute_table where id=$attrib_id";
+                               $this->db->query($sql);
+                               $this->db->next_record();
+                               $attrib_sort    = $this->db->f('attrib_sort');
+                               $sql2 = "SELECT max(attrib_sort) as max_sort 
FROM $attribute_table";
+                               $this->db->query($sql2);
+                               $this->db->next_record();
+                               $max_sort       = $this->db->f('max_sort');
+                               if($max_sort>$attrib_sort)
+                               {
+                                       $sql = "UPDATE $attribute_table set 
attrib_sort=attrib_sort-1 WHERE attrib_sort > $attrib_sort";
+                                       $this->db->query($sql);
+                               }
+
+                               $this->db->query("DELETE FROM $attribute_table 
WHERE id=$attrib_id");
+                       }
+                       else
+                       {
+                               $receipt['error'][] = array('msg'       => 
lang('Attribute has NOT been deleted'));
+                       }
+
+                       return $receipt;
+               }
+
+               function request_next_id()
+               {
+                       $this->db->query("SELECT max(id) as id FROM 
fm_agreement");
+                       $this->db->next_record();
+                       $next_id= $this->db->f('id')+1;
+                       return $next_id;
+               }
+
+               function get_agreement_group_list()
+               {
+                       $this->db->query("SELECT * FROM fm_agreement_group 
ORDER BY descr asc");
+                       while ($this->db->next_record())
+                       {
+                               $agreement_group_list[]=array
+                               (
+                                       'id'    => $this->db->f('id'),
+                                       'name'  => 
$GLOBALS['phpgw']->strip_html($this->db->f('descr')).' [ '. 
$GLOBALS['phpgw']->strip_html($this->db->f('status')).' ] '
+                               );
+                       }
+                       return $agreement_group_list;
+               }
+
+               function read_group_activity($group_id='',$agreement_id='')
+               {
+                       $uicols['name'][]                       = 'id';
+                       $uicols['descr'][]                      = lang('ID');
+                       $uicols['name'][]                       = 'num';
+                       $uicols['descr'][]                      = lang('Num');
+                       $uicols['name'][]                       = 'base_descr';
+                       $uicols['descr'][]                      = lang('Base');
+                       $uicols['name'][]                       = 'descr';
+                       $uicols['descr'][]                      = lang('Descr');
+                       $uicols['name'][]                       = 'unit';
+                       $uicols['descr'][]                      = lang('Unit');
+                       $uicols['name'][]                       = 'ns3420';
+                       $uicols['descr'][]                      = 
lang('ns3420');
+
+                       $this->uicols   = $uicols;
+
+                       $sql="SELECT fm_activities.* FROM fm_activities WHERE 
agreement_group_id = $group_id";
+                       $this->db->query($sql);
+
+                       while ($this->db->next_record())
+                       {
+                               $activity_list[$this->db->f('id')]=array
+                               (
+                                       'id'            => $this->db->f('id'),
+                                       'num'           => $this->db->f('num'),
+                                       'base_descr'    => 
$this->db->f('base_descr'),
+                                       'descr'         => 
$this->db->f('descr'),
+                                       'unit'          => $this->db->f('unit'),
+                                       'ns3420'        => 
$this->db->f('ns3420'),
+                               );
+                       }
+
+                       $sql="SELECT activity_id FROM fm_activity_price_index 
WHERE agreement_id = $agreement_id";
+
+                       $this->db->query($sql);
+
+                       while ($this->db->next_record())
+                       {
+                               
unset($activity_list[$this->db->f('activity_id')]);
+                       }
+
+                       foreach($activity_list as $entry)
+                       {
+                               $activity_list_result[] = $entry;
+                       }
+
+                       return $activity_list_result;
+               }
+
+               function add_activity($values='',$agreement_id='')
+               {
+                       if (isset($values['select']) AND 
is_array($values['select']))
+                       {
+                               $this->db->transaction_begin();
+
+                               $this->db->query("SELECT start_date FROM 
fm_agreement WHERE id=" . $values['agreement_id']);
+                               $this->db->next_record();
+                               $date   = $this->db->f('start_date');
+
+                               foreach($values['select'] as $activity_id)
+                               {
+                                       $this->db->query("INSERT INTO 
fm_activity_price_index ( agreement_id, 
activity_id,index_count,current_index,index_date,entry_date,user_id) "
+                                       . "VALUES 
($agreement_id,$activity_id,-1,1,$date," . time() . "," . $this->account . ")");
+                               }
+
+                               $this->db->transaction_commit();
+                       }
+
+
+                       $receipt['agreement_id']= 
$id;//$this->db->get_last_insert_id($table,'id');
+
+                       $receipt['message'][] = array('msg'=>lang('agreement %1 
has been saved',$receipt['agreement_id']));
+
+                       return $receipt;
+               }
+
+               function select_status_list()
+               {
+                       $this->db->query("SELECT id, descr FROM 
fm_agreement_status ORDER BY id ");
+
+                       $i = 0;
+                       while ($this->db->next_record())
+                       {
+                               $status_entries[$i]['id']                       
        = $this->db->f('id');
+                               $status_entries[$i]['name']                     
        = stripslashes($this->db->f('descr'));
+                               $i++;
+                       }
+                       return $status_entries;
+               }
+
+               function get_activity_descr($id)
+               {
+                       $this->db->query("SELECT descr FROM fm_activities WHERE 
id = $id",__LINE__,__FILE__);
+                       $this->db->next_record();
+                       return stripslashes($this->db->f('descr'));
+               }
+
+       }
+?>
Index: property/inc/class.soalarm.inc.php
diff -u property/inc/class.soalarm.inc.php:1.6 
property/inc/class.soalarm.inc.php:1.7
--- property/inc/class.soalarm.inc.php:1.6      Fri Jan 27 14:05:43 2006
+++ property/inc/class.soalarm.inc.php  Mon Jan 30 22:14:19 2006
@@ -1,242 +1,242 @@
-<?php
-       /**
-       * phpGroupWare - property: a Facilities Management System.
-       *
-       * @author Sigurd Nes <address@hidden>
-       * @copyright Copyright (C) 2003-2005 Free Software Foundation, Inc. 
http://www.fsf.org/
-       * @license http://www.gnu.org/licenses/gpl.html GNU General Public 
License
-       * @internal Development of this application was funded by 
http://www.bergen.kommune.no/bbb_/ekstern/
-       * @package property
-       * @subpackage admin
-       * @version $Id: class.soalarm.inc.php,v 1.6 2006/01/27 14:05:43 
sigurdne Exp $
-       */
-
-       /**
-        * Description
-        * @package property
-        */
-
-       class soalarm
-       {
-
-               function soalarm()
-               {
-                       $this->currentapp       = 
$GLOBALS['phpgw_info']['flags']['currentapp'];
-                       $this->account  = 
$GLOBALS['phpgw_info']['user']['account_id'];
-                       $this->bocommon         = 
CreateObject($this->currentapp.'.bocommon');
+<?php
+       /**
+       * phpGroupWare - property: a Facilities Management System.
+       *
+       * @author Sigurd Nes <address@hidden>
+       * @copyright Copyright (C) 2003-2005 Free Software Foundation, Inc. 
http://www.fsf.org/
+       * @license http://www.gnu.org/licenses/gpl.html GNU General Public 
License
+       * @internal Development of this application was funded by 
http://www.bergen.kommune.no/bbb_/ekstern/
+       * @package property
+       * @subpackage admin
+       * @version $Id: class.soalarm.inc.php,v 1.7 2006/01/30 22:14:19 
sigurdne Exp $
+       */
+
+       /**
+        * Description
+        * @package property
+        */
+
+       class soalarm
+       {
+               function soalarm()
+               {
+                       $this->currentapp       = 
$GLOBALS['phpgw_info']['flags']['currentapp'];
+                       $this->account  = 
$GLOBALS['phpgw_info']['user']['account_id'];
+                       $this->bocommon         = 
CreateObject($this->currentapp.'.bocommon');
                        $this->db               = $this->bocommon->new_db();
-                       $this->db2              = $this->bocommon->new_db();
-                       $this->join                     = $this->bocommon->join;
-                       $this->like                     = $this->bocommon->like;
-               }
-
-               function select_method_list()
-               {
-                       $this->db->query("SELECT id,name,data FROM 
fm_async_method ORDER BY name ");
-
-                       $i = 0;
-                       while ($this->db->next_record())
-                       {
-                               if($this->db->f('data'))
-                               {
-                                       $method_data=array();
-                                       $data_set = 
unserialize($this->db->f('data'));
-                                       while (is_array($data_set) && 
list($key,$value) = each($data_set))
-                                       {
-                                               $method_data[] = $key . '=' . 
$value;
-                                       }
-
-                                       $method_data= @implode 
(',',$method_data);
-                               }
-
-                               $categories[$i]['id']                           
= $this->db->f('id');
-                               $categories[$i]['name']                         
= stripslashes($this->db->f('name')) . '(' . $method_data . ')';
-                               $i++;
-                       }
-                       return $categories;
-               }
-
-               function read_single_method($id)
-               {
-                       $this->db->query("SELECT name FROM fm_async_method  
where id='$id'");
-                       $this->db->next_record();
-                       return $this->db->f('name');
-               }
-
-               function read($data)
-               {
-                       if(is_array($data))
-                       {
-                               $id     = (isset($data['id'])?$data['id']:0);
-                               $start  = 
(isset($data['start'])?$data['start']:0);
-                               $filter = 
(isset($data['filter'])?$data['filter']:'');
-                               $query = 
(isset($data['query'])?$data['query']:'');
-                               $sort = 
(isset($data['sort'])?$data['sort']:'DESC');
-                               $order = 
(isset($data['order'])?$data['order']:'');
-                               $allrows                = 
(isset($data['allrows'])?$data['allrows']:'');
-                       }
-
-                       if ($order)
-                       {
-                               $ordermethod = " order by $order $sort";
-                       }
-                       else
-                       {
-                               $ordermethod = ' order by id DESC';
-                       }
-
-                       $where = 'WHERE';
-                       if ($filter > 0)
-                       {
-                               $filtermethod .= " $where owner='$filter' ";
-                               $where = 'AND';
-
-                       }
-
-                       $id = $this->db->db_addslashes($id);
-                       if (strpos($id,'%') !== False || strpos($id,'_') !== 
False)
-                       {
-                               $filtermethod = "$where id $this->like '%$id%' 
AND id!='##last-check-run##'";
-                       }
-                       elseif (!$id)
-                       {
-                               $filtermethod = $where . ' next<='.time()." AND 
id!='##last-check-run##'";
-                       }
-                       else
-                       {
-                               $filtermethod = "$where id='$id'";
-                       }
-
-                       if($query)
-                       {
-                               $query = ereg_replace("'",'',$query);
-                               $query = ereg_replace('"','',$query);
-
-                               $querymethod = " AND (account_lid $this->like 
'%$query%' OR method $this->like '%$query%' OR id $this->like '%$query%')";
-                       }
-
-                       $sql = "SELECT 
phpgw_async.id,phpgw_async.next,phpgw_async.times,phpgw_async.method,phpgw_async.data,account_lid
 FROM phpgw_async $this->join phpgw_accounts on 
phpgw_async.account_id=phpgw_accounts.account_id $filtermethod $querymethod";
-
-                       $this->db2->query($sql,__LINE__,__FILE__);
-                       $this->total_records = $this->db2->num_rows();
-
-                       if(!$allrows)
-                       {
-                               $this->db->limit_query($sql . 
$ordermethod,$start,__LINE__,__FILE__);
-                       }
-                       else
-                       {
-                               $this->db->query($sql . 
$ordermethod,__LINE__,__FILE__);
-                       }
-
-                       $jobs = array();
-                       while ($this->db->next_record())
-                       {
-                               $id = $this->db->f('id');
-                               $data   = unserialize($this->db->f('data'));
-
-                               $jobs[$id] = array(
-                                       'id'     => $id,
-                                       'next'   => $this->db->f('next'),
-                                       'times'  => 
unserialize($this->db->f('times')),
-                                       'method' => $this->db->f('method'),
-                                       'data'   => $data,
-                                       'enabled'   => (int)$data['enabled'],
-                                       'user'   => $this->db->f('account_lid')
-                               );
-                       }
-                       if (!count($jobs))
-                       {
-                               return False;
-                       }
-                       return $jobs;
-               }
-
-               function read_org($id=0)
-               {
-                       $id = $this->db->db_addslashes($id);
-                       if (strpos($id,'%') !== False || strpos($id,'_') !== 
False)
-                       {
-                               $where = "id $this->like '%$id%' AND 
id!='##last-check-run##'";
-                       }
-                       elseif (!$id)
-                       {
-                               $where = 'next<='.time()." AND 
id!='##last-check-run##'";
-                       }
-                       else
-                       {
-                               $where = "id='$id'";
-                       }
-                       $this->db->query($sql="SELECT * FROM $this->db_table 
WHERE $where",__LINE__,__FILE__);
-
-                       $jobs = array();
-                       while ($this->db->next_record())
-                       {
-                               $id = $this->db->f('id');
-
-                               $jobs[$id] = array(
-                                       'id'     => $id,
-                                       'next'   => $this->db->f('next'),
-                                       'times'  => 
unserialize($this->db->f('times')),
-                                       'method' => $this->db->f('method'),
-                                       'data'   => 
unserialize($this->db->f('data')),
-                                       'account_id'   => 
$this->db->f('account_id')
-                               );
-                               //echo "job id='$id'<pre>"; 
print_r($jobs[$id]); echo "</pre>\n";
-                       }
-                       if (!count($jobs))
-                       {
-                               return False;
-                       }
-                       return $jobs;
-               }
-
-
-               function read_single($owner_id)
-               {
-                       $this->db->query("select * from fm_owner where 
owner_id='$owner_id'",__LINE__,__FILE__);
-
-                       if ($this->db->next_record())
-                       {
-                               $owner['id']                    = 
(int)$this->db->f('owner_id');
-                               $owner['abid']                  = 
$this->db->f('abid');
-                               $owner['contact_name']          = 
stripslashes($this->db->f('contact_name'));
-                               $owner['remark']                = 
stripslashes($this->db->f('remark'));
-                               $owner['entry_date']            = 
$this->db->f('entry_date');
-                               $owner['cat_id']                        = 
(int)$this->db->f('category');
-
-                               return $owner;
-                       }
-               }
-
-               function add($owner)
-               {
-                       $owner['name'] = 
$this->db->db_addslashes($owner['name']);
-
-                       $this->db->query("INSERT INTO fm_owner 
(entry_date,remark,abid,contact_name,category) "
-                               . "VALUES ('" . time() . "','" . 
$owner['remark'] . "','" . $owner['abid'] . "','" . $owner['contact_name']
-                               . "','" . $owner['cat_id'] . 
"')",__LINE__,__FILE__);
-
-                       $receipt['owner_id']= 
$this->db->get_last_insert_id('fm_owner','owner_id');
-                       $receipt['message'][] = array('msg'=>lang('owner %1 has 
been saved',$receipt['owner_id']));
-                       return $receipt;
-               }
-
-               function edit($owner)
-               {
-                       $owner['name'] = 
$this->db->db_addslashes($owner['name']);
-
-                       $this->db->query("UPDATE fm_owner set remark='" . 
$owner['remark'] . "', entry_date='" . time() . "', abid='" . $owner['abid'] . 
"', contact_name='" . $owner['contact_name'] . "', category='"
-                                                       . $owner['cat_id'] . "' 
WHERE owner_id=" . intval($owner['owner_id']),__LINE__,__FILE__);
-
-                       $receipt['owner_id']= $owner['owner_id'];
-                       $receipt['message'][] = array('msg'=>lang('owner %1 has 
been edited',$owner['owner_id']));
-                       return $receipt;
-               }
-
-               function delete($owner_id)
-               {
-                       $this->db->query('DELETE FROM fm_owner WHERE owner_id=' 
. intval($owner_id),__LINE__,__FILE__);
-               }
-       }
-?>
+                       $this->db2              = $this->bocommon->new_db();
+
+                       $this->join                     = $this->bocommon->join;
+                       $this->like                     = $this->bocommon->like;
+               }
+
+               function select_method_list()
+               {
+                       $this->db->query("SELECT id,name,data FROM 
fm_async_method ORDER BY name ");
+
+                       $i = 0;
+                       while ($this->db->next_record())
+                       {
+                               if($this->db->f('data'))
+                               {
+                                       $method_data=array();
+                                       $data_set = 
unserialize($this->db->f('data'));
+                                       while (is_array($data_set) && 
list($key,$value) = each($data_set))
+                                       {
+                                               $method_data[] = $key . '=' . 
$value;
+                                       }
+
+                                       $method_data= @implode 
(',',$method_data);
+                               }
+
+                               $categories[$i]['id']                           
= $this->db->f('id');
+                               $categories[$i]['name']                         
= stripslashes($this->db->f('name')) . '(' . $method_data . ')';
+                               $i++;
+                       }
+                       return $categories;
+               }
+
+               function read_single_method($id)
+               {
+                       $this->db->query("SELECT name FROM fm_async_method  
where id='$id'");
+                       $this->db->next_record();
+                       return $this->db->f('name');
+               }
+
+               function read($data)
+               {
+                       if(is_array($data))
+                       {
+                               $id     = (isset($data['id'])?$data['id']:0);
+                               $start  = 
(isset($data['start'])?$data['start']:0);
+                               $filter = 
(isset($data['filter'])?$data['filter']:'');
+                               $query = 
(isset($data['query'])?$data['query']:'');
+                               $sort = 
(isset($data['sort'])?$data['sort']:'DESC');
+                               $order = 
(isset($data['order'])?$data['order']:'');
+                               $allrows                = 
(isset($data['allrows'])?$data['allrows']:'');
+                       }
+
+                       if ($order)
+                       {
+                               $ordermethod = " order by $order $sort";
+                       }
+                       else
+                       {
+                               $ordermethod = ' order by id DESC';
+                       }
+
+                       $where = 'WHERE';
+                       if ($filter > 0)
+                       {
+                               $filtermethod .= " $where owner='$filter' ";
+                               $where = 'AND';
+
+                       }
+
+                       $id = $this->db->db_addslashes($id);
+                       if (strpos($id,'%') !== False || strpos($id,'_') !== 
False)
+                       {
+                               $filtermethod = "$where id $this->like '%$id%' 
AND id!='##last-check-run##'";
+                       }
+                       elseif (!$id)
+                       {
+                               $filtermethod = $where . ' next<='.time()." AND 
id!='##last-check-run##'";
+                       }
+                       else
+                       {
+                               $filtermethod = "$where id='$id'";
+                       }
+
+                       if($query)
+                       {
+                               $query = ereg_replace("'",'',$query);
+                               $query = ereg_replace('"','',$query);
+
+                               $querymethod = " AND (account_lid $this->like 
'%$query%' OR method $this->like '%$query%' OR id $this->like '%$query%')";
+                       }
+
+                       $sql = "SELECT 
phpgw_async.id,phpgw_async.next,phpgw_async.times,phpgw_async.method,phpgw_async.data,account_lid
 FROM phpgw_async $this->join phpgw_accounts on 
phpgw_async.account_id=phpgw_accounts.account_id $filtermethod $querymethod";
+
+                       $this->db2->query($sql,__LINE__,__FILE__);
+                       $this->total_records = $this->db2->num_rows();
+
+                       if(!$allrows)
+                       {
+                               $this->db->limit_query($sql . 
$ordermethod,$start,__LINE__,__FILE__);
+                       }
+                       else
+                       {
+                               $this->db->query($sql . 
$ordermethod,__LINE__,__FILE__);
+                       }
+
+                       $jobs = array();
+                       while ($this->db->next_record())
+                       {
+                               $id = $this->db->f('id');
+                               $data   = unserialize($this->db->f('data'));
+
+                               $jobs[$id] = array(
+                                       'id'     => $id,
+                                       'next'   => $this->db->f('next'),
+                                       'times'  => 
unserialize($this->db->f('times')),
+                                       'method' => $this->db->f('method'),
+                                       'data'   => $data,
+                                       'enabled'   => (int)$data['enabled'],
+                                       'user'   => $this->db->f('account_lid')
+                               );
+                       }
+                       if (!count($jobs))
+                       {
+                               return False;
+                       }
+                       return $jobs;
+               }
+
+               function read_org($id=0)
+               {
+                       $id = $this->db->db_addslashes($id);
+                       if (strpos($id,'%') !== False || strpos($id,'_') !== 
False)
+                       {
+                               $where = "id $this->like '%$id%' AND 
id!='##last-check-run##'";
+                       }
+                       elseif (!$id)
+                       {
+                               $where = 'next<='.time()." AND 
id!='##last-check-run##'";
+                       }
+                       else
+                       {
+                               $where = "id='$id'";
+                       }
+                       $this->db->query($sql="SELECT * FROM $this->db_table 
WHERE $where",__LINE__,__FILE__);
+
+                       $jobs = array();
+                       while ($this->db->next_record())
+                       {
+                               $id = $this->db->f('id');
+
+                               $jobs[$id] = array(
+                                       'id'     => $id,
+                                       'next'   => $this->db->f('next'),
+                                       'times'  => 
unserialize($this->db->f('times')),
+                                       'method' => $this->db->f('method'),
+                                       'data'   => 
unserialize($this->db->f('data')),
+                                       'account_id'   => 
$this->db->f('account_id')
+                               );
+                               //echo "job id='$id'<pre>"; 
print_r($jobs[$id]); echo "</pre>\n";
+                       }
+                       if (!count($jobs))
+                       {
+                               return False;
+                       }
+                       return $jobs;
+               }
+
+
+               function read_single($owner_id)
+               {
+                       $this->db->query("select * from fm_owner where 
owner_id='$owner_id'",__LINE__,__FILE__);
+
+                       if ($this->db->next_record())
+                       {
+                               $owner['id']                    = 
(int)$this->db->f('owner_id');
+                               $owner['abid']                  = 
$this->db->f('abid');
+                               $owner['contact_name']          = 
stripslashes($this->db->f('contact_name'));
+                               $owner['remark']                = 
stripslashes($this->db->f('remark'));
+                               $owner['entry_date']            = 
$this->db->f('entry_date');
+                               $owner['cat_id']                        = 
(int)$this->db->f('category');
+
+                               return $owner;
+                       }
+               }
+
+               function add($owner)
+               {
+                       $owner['name'] = 
$this->db->db_addslashes($owner['name']);
+
+                       $this->db->query("INSERT INTO fm_owner 
(entry_date,remark,abid,contact_name,category) "
+                               . "VALUES ('" . time() . "','" . 
$owner['remark'] . "','" . $owner['abid'] . "','" . $owner['contact_name']
+                               . "','" . $owner['cat_id'] . 
"')",__LINE__,__FILE__);
+
+                       $receipt['owner_id']= 
$this->db->get_last_insert_id('fm_owner','owner_id');
+                       $receipt['message'][] = array('msg'=>lang('owner %1 has 
been saved',$receipt['owner_id']));
+                       return $receipt;
+               }
+
+               function edit($owner)
+               {
+                       $owner['name'] = 
$this->db->db_addslashes($owner['name']);
+
+                       $this->db->query("UPDATE fm_owner set remark='" . 
$owner['remark'] . "', entry_date='" . time() . "', abid='" . $owner['abid'] . 
"', contact_name='" . $owner['contact_name'] . "', category='"
+                                                       . $owner['cat_id'] . "' 
WHERE owner_id=" . intval($owner['owner_id']),__LINE__,__FILE__);
+
+                       $receipt['owner_id']= $owner['owner_id'];
+                       $receipt['message'][] = array('msg'=>lang('owner %1 has 
been edited',$owner['owner_id']));
+                       return $receipt;
+               }
+
+               function delete($owner_id)
+               {
+                       $this->db->query('DELETE FROM fm_owner WHERE owner_id=' 
. intval($owner_id),__LINE__,__FILE__);
+               }
+       }
+?>
Index: property/inc/class.soasync.inc.php
diff -u property/inc/class.soasync.inc.php:1.5 
property/inc/class.soasync.inc.php:1.6
--- property/inc/class.soasync.inc.php:1.5      Fri Jan 27 14:05:43 2006
+++ property/inc/class.soasync.inc.php  Mon Jan 30 22:14:19 2006
@@ -1,149 +1,148 @@
-<?php
-       /**
-       * phpGroupWare - property: a Facilities Management System.
-       *
-       * @author Sigurd Nes <address@hidden>
-       * @copyright Copyright (C) 2003-2005 Free Software Foundation, Inc. 
http://www.fsf.org/
-       * @license http://www.gnu.org/licenses/gpl.html GNU General Public 
License
-       * @internal Development of this application was funded by 
http://www.bergen.kommune.no/bbb_/ekstern/
-       * @package property
-       * @subpackage admin
-       * @version $Id: class.soasync.inc.php,v 1.5 2006/01/27 14:05:43 
sigurdne Exp $
-       */
-
-       /**
-        * Description
-        * @package property
-        */
-
-       class soasync
-       {
-
-               function soasync()
-               {
-                       $this->currentapp       = 
$GLOBALS['phpgw_info']['flags']['currentapp'];
-                       $this->account          = 
$GLOBALS['phpgw_info']['user']['account_id'];
-                       $this->bocommon         = 
CreateObject($this->currentapp.'.bocommon');
+<?php
+       /**
+       * phpGroupWare - property: a Facilities Management System.
+       *
+       * @author Sigurd Nes <address@hidden>
+       * @copyright Copyright (C) 2003-2005 Free Software Foundation, Inc. 
http://www.fsf.org/
+       * @license http://www.gnu.org/licenses/gpl.html GNU General Public 
License
+       * @internal Development of this application was funded by 
http://www.bergen.kommune.no/bbb_/ekstern/
+       * @package property
+       * @subpackage admin
+       * @version $Id: class.soasync.inc.php,v 1.6 2006/01/30 22:14:19 
sigurdne Exp $
+       */
+
+       /**
+        * Description
+        * @package property
+        */
+
+       class soasync
+       {
+               function soasync()
+               {
+                       $this->currentapp       = 
$GLOBALS['phpgw_info']['flags']['currentapp'];
+                       $this->account          = 
$GLOBALS['phpgw_info']['user']['account_id'];
+                       $this->bocommon         = 
CreateObject($this->currentapp.'.bocommon');
                        $this->db               = $this->bocommon->new_db();
-                       $this->db2              = $this->bocommon->new_db();
-                       $this->join                     = $this->bocommon->join;
-                       $this->like                     = $this->bocommon->like;
-               }
-
-               function read($data)
-               {
-                       if(is_array($data))
-                       {
-                               if ($data['start'])
-                               {
-                                       $start=$data['start'];
-                               }
-                               else
-                               {
-                                       $start=0;
-                               }
-                               $query = 
(isset($data['query'])?$data['query']:'');
-                               $sort = 
(isset($data['sort'])?$data['sort']:'DESC');
-                               $order = 
(isset($data['order'])?$data['order']:'');
-                       }
-
-                       if ($order)
-                       {
-                               $ordermethod = " order by $order $sort";
-
-                       }
-                       else
-                       {
-                               $ordermethod = ' order by id asc';
-                       }
-
-                       $table='fm_async_method';
-
-                       if($query)
-                       {
-                               $query = ereg_replace("'",'',$query);
-                               $query = ereg_replace('"','',$query);
-
-                               $querymethod = " where id $this->like 
'%$query%' or descr $this->like '%$query%'";
-                       }
-
-                       $sql = "SELECT * FROM $table $querymethod";
-
-                       $this->db2->query($sql,__LINE__,__FILE__);
-                       $this->total_records = $this->db2->num_rows();
-                       $this->db->limit_query($sql . 
$ordermethod,$start,__LINE__,__FILE__);
-
-                       while ($this->db->next_record())
-                       {
-                               $method[] = array
-                               (
-                                       'id'    => $this->db->f('id'),
-                                       'name'  => $this->db->f('name'),
-                                       'data'  => $this->db->f('data'),
-                                       'descr' => $this->db->f('descr')
-                               );
-                       }
-                       return $method;
-               }
-
-
-               function read_single($id)
-               {
-                       $table='fm_async_method';
-
-                       $sql = "SELECT * FROM $table  where id='$id'";
-
-                       $this->db->query($sql,__LINE__,__FILE__);
-
-                       if ($this->db->next_record())
-                       {
-                               $method['id']           = $this->db->f('id');
-                               $method['name']         = $this->db->f('name');
-                               $method['data']         = $this->db->f('data');
-                               $method['descr']        = $this->db->f('descr');
-
-                               return $method;
-                       }
-               }
-
-               function add($method)
-               {
-                       $table='fm_async_method';
-
-                       $method['id'] = $this->socommon->next_id($table);
-                       $method['name'] = 
$this->db->db_addslashes($method['name']);
-                       $method['descr'] = 
$this->db->db_addslashes($method['descr']);
-
-                       $this->db->query("INSERT INTO $table (id, name,data, 
descr) "
-                               . "VALUES ('" . $method['id'] . "','" . 
$method['name'] . "','" . $method['data'] . "','" . $method['descr']. 
"')",__LINE__,__FILE__);
-
-                       $receipt['id'] = $method['id'];
-                       $receipt['message'][] = array('msg' => lang('async 
method has been saved'));
-
-                       return $receipt;
-               }
-
-               function edit($method)
-               {
-
-                       $table='fm_async_method';
-
-                       $method['name'] = 
$this->db->db_addslashes($method['name']);
-                       $method['descr'] = 
$this->db->db_addslashes($method['descr']);
-
-                       $this->db->query("UPDATE $table set descr='" . 
$method['descr'] . "', name='". $method['name'] . "', data='". $method['data']
-                                                       . "' WHERE id='" . 
$method['id']. "'",__LINE__,__FILE__);
-
-                       $receipt['id'] = $method['id'];
-                       $receipt['message'][] = array('msg' =>lang('method has 
been edited'));
-                       return $receipt;
-               }
-
-               function delete($id)
-               {
-                       $table='fm_async_method';
-
-                       $this->db->query("DELETE FROM $table WHERE id='" . $id 
. "'",__LINE__,__FILE__);
-               }
-       }
-?>
+                       $this->db2              = $this->bocommon->new_db();
+
+                       $this->join                     = $this->bocommon->join;
+                       $this->like                     = $this->bocommon->like;
+               }
+
+               function read($data)
+               {
+                       if(is_array($data))
+                       {
+                               if ($data['start'])
+                               {
+                                       $start=$data['start'];
+                               }
+                               else
+                               {
+                                       $start=0;
+                               }
+                               $query = 
(isset($data['query'])?$data['query']:'');
+                               $sort = 
(isset($data['sort'])?$data['sort']:'DESC');
+                               $order = 
(isset($data['order'])?$data['order']:'');
+                       }
+
+                       if ($order)
+                       {
+                               $ordermethod = " order by $order $sort";
+
+                       }
+                       else
+                       {
+                               $ordermethod = ' order by id asc';
+                       }
+
+                       $table='fm_async_method';
+
+                       if($query)
+                       {
+                               $query = ereg_replace("'",'',$query);
+                               $query = ereg_replace('"','',$query);
+
+                               $querymethod = " where id $this->like 
'%$query%' or descr $this->like '%$query%'";
+                       }
+
+                       $sql = "SELECT * FROM $table $querymethod";
+
+                       $this->db2->query($sql,__LINE__,__FILE__);
+                       $this->total_records = $this->db2->num_rows();
+                       $this->db->limit_query($sql . 
$ordermethod,$start,__LINE__,__FILE__);
+
+                       while ($this->db->next_record())
+                       {
+                               $method[] = array
+                               (
+                                       'id'    => $this->db->f('id'),
+                                       'name'  => $this->db->f('name'),
+                                       'data'  => $this->db->f('data'),
+                                       'descr' => $this->db->f('descr')
+                               );
+                       }
+                       return $method;
+               }
+
+
+               function read_single($id)
+               {
+                       $table='fm_async_method';
+
+                       $sql = "SELECT * FROM $table  where id='$id'";
+
+                       $this->db->query($sql,__LINE__,__FILE__);
+
+                       if ($this->db->next_record())
+                       {
+                               $method['id']           = $this->db->f('id');
+                               $method['name']         = $this->db->f('name');
+                               $method['data']         = $this->db->f('data');
+                               $method['descr']        = $this->db->f('descr');
+
+                               return $method;
+                       }
+               }
+
+               function add($method)
+               {
+                       $table='fm_async_method';
+
+                       $method['id'] = $this->socommon->next_id($table);
+                       $method['name'] = 
$this->db->db_addslashes($method['name']);
+                       $method['descr'] = 
$this->db->db_addslashes($method['descr']);
+
+                       $this->db->query("INSERT INTO $table (id, name,data, 
descr) "
+                               . "VALUES ('" . $method['id'] . "','" . 
$method['name'] . "','" . $method['data'] . "','" . $method['descr']. 
"')",__LINE__,__FILE__);
+
+                       $receipt['id'] = $method['id'];
+                       $receipt['message'][] = array('msg' => lang('async 
method has been saved'));
+
+                       return $receipt;
+               }
+
+               function edit($method)
+               {
+                       $table='fm_async_method';
+
+                       $method['name'] = 
$this->db->db_addslashes($method['name']);
+                       $method['descr'] = 
$this->db->db_addslashes($method['descr']);
+
+                       $this->db->query("UPDATE $table set descr='" . 
$method['descr'] . "', name='". $method['name'] . "', data='". $method['data']
+                                                       . "' WHERE id='" . 
$method['id']. "'",__LINE__,__FILE__);
+
+                       $receipt['id'] = $method['id'];
+                       $receipt['message'][] = array('msg' =>lang('method has 
been edited'));
+                       return $receipt;
+               }
+
+               function delete($id)
+               {
+                       $table='fm_async_method';
+
+                       $this->db->query("DELETE FROM $table WHERE id='" . $id 
. "'",__LINE__,__FILE__);
+               }
+       }
+?>
Index: property/inc/class.sob_account.inc.php
diff -u property/inc/class.sob_account.inc.php:1.7 
property/inc/class.sob_account.inc.php:1.8
--- property/inc/class.sob_account.inc.php:1.7  Fri Jan 27 14:05:43 2006
+++ property/inc/class.sob_account.inc.php      Mon Jan 30 22:14:19 2006
@@ -1,150 +1,149 @@
-<?php
-       /**
-       * phpGroupWare - property: a Facilities Management System.
-       *
-       * @author Sigurd Nes <address@hidden>
-       * @copyright Copyright (C) 2003-2005 Free Software Foundation, Inc. 
http://www.fsf.org/
-       * @license http://www.gnu.org/licenses/gpl.html GNU General Public 
License
-       * @internal Development of this application was funded by 
http://www.bergen.kommune.no/bbb_/ekstern/
-       * @package property
-       * @subpackage admin
-       * @version $Id: class.sob_account.inc.php,v 1.7 2006/01/27 14:05:43 
sigurdne Exp $
-       */
-
-       /**
-        * Description
-        * @package property
-        */
-
-       class sob_account
-       {
-
-               function sob_account()
-               {
-                       $this->currentapp       = 
$GLOBALS['phpgw_info']['flags']['currentapp'];
-                       $this->account  = 
$GLOBALS['phpgw_info']['user']['account_id'];
-                       $this->bocommon         = 
CreateObject($this->currentapp.'.bocommon');
+<?php
+       /**
+       * phpGroupWare - property: a Facilities Management System.
+       *
+       * @author Sigurd Nes <address@hidden>
+       * @copyright Copyright (C) 2003-2005 Free Software Foundation, Inc. 
http://www.fsf.org/
+       * @license http://www.gnu.org/licenses/gpl.html GNU General Public 
License
+       * @internal Development of this application was funded by 
http://www.bergen.kommune.no/bbb_/ekstern/
+       * @package property
+       * @subpackage admin
+       * @version $Id: class.sob_account.inc.php,v 1.8 2006/01/30 22:14:19 
sigurdne Exp $
+       */
+
+       /**
+        * Description
+        * @package property
+        */
+
+       class sob_account
+       {
+               function sob_account()
+               {
+                       $this->currentapp       = 
$GLOBALS['phpgw_info']['flags']['currentapp'];
+                       $this->account  = 
$GLOBALS['phpgw_info']['user']['account_id'];
+                       $this->bocommon         = 
CreateObject($this->currentapp.'.bocommon');
                        $this->db               = $this->bocommon->new_db();
-                       $this->db2              = $this->bocommon->new_db();    
                
-                       $this->like                     = $this->bocommon->like;
-               }
-
-               function read($data)
-               {
-                       if(is_array($data))
-                       {
-                               if ($data['start'])
-                               {
-                                       $start=$data['start'];
-                               }
-                               else
-                               {
-                                       $start=0;
-                               }
-                               $query          = 
(isset($data['query'])?$data['query']:'');
-                               $sort           = 
(isset($data['sort'])?$data['sort']:'DESC');
-                               $order          = 
(isset($data['order'])?$data['order']:'');
-                               $allrows        = 
(isset($data['allrows'])?$data['allrows']:'');
-                       }
-
-                       if ($order)
-                       {
-                               $ordermethod = " order by $order $sort";
-
-                       }
-                       else
-                       {
-                               $ordermethod = ' order by id asc';
-                       }
-
-                       $table = 'fm_b_account';
-
-                       if($query)
-                       {
-                               $query = ereg_replace("'",'',$query);
-                               $query = ereg_replace('"','',$query);
-
-                               $querymethod = " where id $this->like 
'%$query%' or descr $this->like '%$query%'";
-                       }
-
-                       $sql = "SELECT * FROM $table $querymethod";
-
-                       $this->db2->query($sql,__LINE__,__FILE__);
-                       $this->total_records = $this->db2->num_rows();
-
-                       if(!$allrows)
-                       {
-                               $this->db->limit_query($sql . 
$ordermethod,$start,__LINE__,__FILE__);
-                       }
-                       else
-                       {
-                               $this->db->query($sql . 
$ordermethod,__LINE__,__FILE__);
-                       }
-
-                       while ($this->db->next_record())
-                       {
-                               $b_account[] = array
-                               (
-                                       'id'    => $this->db->f('id'),
-                                       'descr'                 => 
$this->db->f('descr')
-                               );
-                       }
-                       return $b_account;
-               }
-
-               function read_single($id)
-               {
-
-                       $table = 'fm_b_account';
-
-                       $sql = "SELECT * FROM $table  where id='$id'";
-
-                       $this->db->query($sql,__LINE__,__FILE__);
-
-                       if ($this->db->next_record())
-                       {
-                               $b_account['id']                        = 
$this->db->f('id');
-                               $b_account['descr']                     = 
$this->db->f('descr');
-                               $b_account['group']                     = 
$this->db->f('grouping');
-                               $b_account['responsible']       = 
$this->db->f('responsible');
-
-                               return $b_account;
-                       }
-               }
-
-               function add($b_account)
-               {
-                       $table = 'fm_b_account';
-
-                       $b_account['descr'] = 
$this->db->db_addslashes($b_account['descr']);
-
-                       $this->db->query("INSERT INTO $table (id, 
descr,grouping,responsible) "
-                               . "VALUES ('" . $b_account['id'] . "','" . 
$b_account['descr']. "','" . substr($b_account['id'],0,2) . "','" . 
$b_account['responsible'] . "')",__LINE__,__FILE__);
-
-                       $receipt['message'][]=array('msg'=>lang('budget account 
%1 has been saved',$b_account['id']));
-                       return $receipt;
-               }
-
-               function edit($b_account)
-               {
-
-                       $table = 'fm_b_account';
-
-                       $b_account['descr'] = 
$this->db->db_addslashes($b_account['descr']);
-
-                       $this->db->query("UPDATE $table set descr='" . 
$b_account['descr'] . "',responsible='" . $b_account['responsible']
-                                                       . "' WHERE id='" . 
$b_account['id']. "'",__LINE__,__FILE__);
-
-
-                       $receipt['message'][]=array('msg'=>lang('budget account 
%1 has been edited',$b_account['id']));
-                       return $receipt;
-               }
-
-               function delete($id)
-               {
-                       $table = 'fm_b_account';
-
-                       $this->db->query("DELETE FROM $table WHERE id='" . $id 
. "'",__LINE__,__FILE__);
-               }
-       }
-?>
+                       $this->db2              = $this->bocommon->new_db();
+                       $this->like                     = $this->bocommon->like;
+               }
+
+               function read($data)
+               {
+                       if(is_array($data))
+                       {
+                               if ($data['start'])
+                               {
+                                       $start=$data['start'];
+                               }
+                               else
+                               {
+                                       $start=0;
+                               }
+                               $query          = 
(isset($data['query'])?$data['query']:'');
+                               $sort           = 
(isset($data['sort'])?$data['sort']:'DESC');
+                               $order          = 
(isset($data['order'])?$data['order']:'');
+                               $allrows        = 
(isset($data['allrows'])?$data['allrows']:'');
+                       }
+
+                       if ($order)
+                       {
+                               $ordermethod = " order by $order $sort";
+
+                       }
+                       else
+                       {
+                               $ordermethod = ' order by id asc';
+                       }
+
+                       $table = 'fm_b_account';
+
+                       if($query)
+                       {
+                               $query = ereg_replace("'",'',$query);
+                               $query = ereg_replace('"','',$query);
+
+                               $querymethod = " where id $this->like 
'%$query%' or descr $this->like '%$query%'";
+                       }
+
+                       $sql = "SELECT * FROM $table $querymethod";
+
+                       $this->db2->query($sql,__LINE__,__FILE__);
+                       $this->total_records = $this->db2->num_rows();
+
+                       if(!$allrows)
+                       {
+                               $this->db->limit_query($sql . 
$ordermethod,$start,__LINE__,__FILE__);
+                       }
+                       else
+                       {
+                               $this->db->query($sql . 
$ordermethod,__LINE__,__FILE__);
+                       }
+
+                       while ($this->db->next_record())
+                       {
+                               $b_account[] = array
+                               (
+                                       'id'    => $this->db->f('id'),
+                                       'descr'                 => 
$this->db->f('descr')
+                               );
+                       }
+                       return $b_account;
+               }
+
+               function read_single($id)
+               {
+
+                       $table = 'fm_b_account';
+
+                       $sql = "SELECT * FROM $table  where id='$id'";
+
+                       $this->db->query($sql,__LINE__,__FILE__);
+
+                       if ($this->db->next_record())
+                       {
+                               $b_account['id']                        = 
$this->db->f('id');
+                               $b_account['descr']                     = 
$this->db->f('descr');
+                               $b_account['group']                     = 
$this->db->f('grouping');
+                               $b_account['responsible']       = 
$this->db->f('responsible');
+
+                               return $b_account;
+                       }
+               }
+
+               function add($b_account)
+               {
+                       $table = 'fm_b_account';
+
+                       $b_account['descr'] = 
$this->db->db_addslashes($b_account['descr']);
+
+                       $this->db->query("INSERT INTO $table (id, 
descr,grouping,responsible) "
+                               . "VALUES ('" . $b_account['id'] . "','" . 
$b_account['descr']. "','" . substr($b_account['id'],0,2) . "','" . 
$b_account['responsible'] . "')",__LINE__,__FILE__);
+
+                       $receipt['message'][]=array('msg'=>lang('budget account 
%1 has been saved',$b_account['id']));
+                       return $receipt;
+               }
+
+               function edit($b_account)
+               {
+
+                       $table = 'fm_b_account';
+
+                       $b_account['descr'] = 
$this->db->db_addslashes($b_account['descr']);
+
+                       $this->db->query("UPDATE $table set descr='" . 
$b_account['descr'] . "',responsible='" . $b_account['responsible']
+                                                       . "' WHERE id='" . 
$b_account['id']. "'",__LINE__,__FILE__);
+
+
+                       $receipt['message'][]=array('msg'=>lang('budget account 
%1 has been edited',$b_account['id']));
+                       return $receipt;
+               }
+
+               function delete($id)
+               {
+                       $table = 'fm_b_account';
+
+                       $this->db->query("DELETE FROM $table WHERE id='" . $id 
. "'",__LINE__,__FILE__);
+               }
+       }
+?>
Index: property/inc/class.socategory.inc.php
diff -u property/inc/class.socategory.inc.php:1.12 
property/inc/class.socategory.inc.php:1.13
--- property/inc/class.socategory.inc.php:1.12  Fri Jan 27 14:05:43 2006
+++ property/inc/class.socategory.inc.php       Mon Jan 30 22:14:19 2006
@@ -1,219 +1,218 @@
-<?php
-       /**
-       * phpGroupWare - property: a Facilities Management System.
-       *
-       * @author Sigurd Nes <address@hidden>
-       * @copyright Copyright (C) 2003-2005 Free Software Foundation, Inc. 
http://www.fsf.org/
-       * @license http://www.gnu.org/licenses/gpl.html GNU General Public 
License
-       * @internal Development of this application was funded by 
http://www.bergen.kommune.no/bbb_/ekstern/
-       * @package property
-       * @subpackage admin
-       * @version $Id: class.socategory.inc.php,v 1.12 2006/01/27 14:05:43 
sigurdne Exp $
-       */
-
-       /**
-        * Description
-        * @package property
-        */
-
-       class socategory
-       {
-
-               function socategory()
-               {
-                       $this->currentapp       = 
$GLOBALS['phpgw_info']['flags']['currentapp'];
-                       $this->account  = 
$GLOBALS['phpgw_info']['user']['account_id'];
-                       $this->bocommon         = 
CreateObject($this->currentapp.'.bocommon');
+<?php
+       /**
+       * phpGroupWare - property: a Facilities Management System.
+       *
+       * @author Sigurd Nes <address@hidden>
+       * @copyright Copyright (C) 2003-2005 Free Software Foundation, Inc. 
http://www.fsf.org/
+       * @license http://www.gnu.org/licenses/gpl.html GNU General Public 
License
+       * @internal Development of this application was funded by 
http://www.bergen.kommune.no/bbb_/ekstern/
+       * @package property
+       * @subpackage admin
+       * @version $Id: class.socategory.inc.php,v 1.13 2006/01/30 22:14:19 
sigurdne Exp $
+       */
+
+       /**
+        * Description
+        * @package property
+        */
+
+       class socategory
+       {
+               function socategory()
+               {
+                       $this->currentapp       = 
$GLOBALS['phpgw_info']['flags']['currentapp'];
+                       $this->account  = 
$GLOBALS['phpgw_info']['user']['account_id'];
+                       $this->bocommon         = 
CreateObject($this->currentapp.'.bocommon');
                        $this->db               = $this->bocommon->new_db();
-                       $this->db2              = $this->bocommon->new_db();
-                       $this->join                     = $this->bocommon->join;
-                       $this->like                     = $this->bocommon->like;
-
-               }
-
-               function read($data)
-               {
-                       if(is_array($data))
-                       {
-                               if ($data['start'])
-                               {
-                                       $start=$data['start'];
-                               }
-                               else
-                               {
-                                       $start=0;
-                               }
-                               $query          = 
(isset($data['query'])?$data['query']:'');
-                               $sort           = 
(isset($data['sort'])?$data['sort']:'DESC');
-                               $order          = 
(isset($data['order'])?$data['order']:'');
-                               $type           = 
(isset($data['type'])?$data['type']:'');
-                               $type_id                = 
(isset($data['type_id'])?$data['type_id']:'');
-                               $allrows        = 
(isset($data['allrows'])?$data['allrows']:'');
-                       }
-
-                       if(!$type)
-                       {
-                               return;
-                       }
-                       if ($order)
-                       {
-                               $ordermethod = " order by $order $sort";
-
-                       }
-                       else
-                       {
-                               $ordermethod = ' order by id asc';
-                       }
-
-                       $table = $this->select_table($type,$type_id);
-
-                       if($query)
-                       {
-                               $query = ereg_replace("'",'',$query);
-                               $query = ereg_replace('"','',$query);
-
-                               $querymethod = " where id $this->like 
'%$query%' or descr $this->like '%$query%'";
-                       }
-
-                       $sql = "SELECT * FROM $table $querymethod";
-
-                       $this->db2->query($sql,__LINE__,__FILE__);
-                       $this->total_records = $this->db2->num_rows();
-
-                       if(!$allrows)
-                       {
-                               $this->db->limit_query($sql . 
$ordermethod,$start,__LINE__,__FILE__);
-                       }
-                       else
-                       {
-                               $this->db->query($sql . 
$ordermethod,__LINE__,__FILE__);
-                       }
-
-                       while ($this->db->next_record())
-                       {
-                               $category[] = array
-                               (
-                                       'id'    => $this->db->f('id'),
-                                       'descr' => 
stripslashes($this->db->f('descr'))
-                               );
-                       }
-                       return $category;
-               }
-
-
-               function select_table($type,$type_id)
-               {
-
-                       switch($type)
-                       {
-                               case 'dim_d':
-                                       $table='fm_dim_d';
-                                       break;
-                               case 'tender_chapter':
-                                       $table='fm_chapter';
-                                       break;
-                               case 'ticket':
-                                       $table='fm_tts_category';
-                                       break;
-                               case 'request':
-                                       $table='fm_request_category';
-                                       break;
-                               case 'wo':
-                                       $table='fm_workorder_category';
-                                       break;
-                               case 'location':
-                                       $table='fm_location' . $type_id . 
'_category';
-                                       break;
-                               case 'meter':
-                                       $table='fm_meter_category';
-                                       break;
-                               case 'document':
-                                       $table='fm_document_category';
-                                       break;
-                               case 'owner':
-                                       $table='fm_owner_category';
-                                       break;
-                               case 'tenant':
-                                       $table='fm_tenant_category';
-                                       break;
-                               case 'vendor':
-                                       $table='fm_vendor_category';
-                                       break;
-                               case 'district':
-                                       $table='fm_district';
-                                       break;
-                               case 'street':
-                                       $table='fm_streetaddress';
-                                       break;
-                               case 's_agreement':
-                                       $table='fm_s_agreement_category';
-                                       break;
-                               case 'tenant_claim':
-                                       $table='fm_tenant_claim_category';
-                                       break;
-                               case 'wo_hours':
-                                       $table='fm_wo_hours_category';
-                                       break;
-                               case 'r_condition_type':
-                                       $table='fm_request_condition_type';
-                                       break;
-                       }
-
-                       return $table;
-               }
-
-
-               function read_single($id,$type,$type_id)
-               {
-
-                       $table = $this->select_table($type,$type_id);
-
-                       $sql = "SELECT * FROM $table  where id='$id'";
-
-                       $this->db->query($sql,__LINE__,__FILE__);
-
-                       if ($this->db->next_record())
-                       {
-                               $category['id']         = $this->db->f('id');
-                               $category['descr']      = 
stripslashes($this->db->f('descr'));
-
-                               return $category;
-                       }
-               }
-
-               function add($category,$type,$type_id)
-               {
-                       $table = $this->select_table($type,$type_id);
-
-                       $category['descr'] = 
$this->db->db_addslashes($category['descr']);
-
-                       $this->db->query("INSERT INTO $table (id, descr) "
-                               . "VALUES ('" . $category['id'] . "','" . 
$category['descr']. "')",__LINE__,__FILE__);
-
-                       $receipt['message'][]=array('msg'=>lang('category has 
been saved'));
-                       return $receipt;
-               }
-
-               function edit($category,$type,$type_id)
-               {
-
-                       $table = $this->select_table($type,$type_id);
-
-                       $category['descr'] = 
$this->db->db_addslashes($category['descr']);
-
-                       $this->db->query("UPDATE $table set descr='" . 
$category['descr']
-                                                       . "' WHERE id='" . 
$category['id']. "'",__LINE__,__FILE__);
-
-
-                       $receipt['message'][]=array('msg'=>lang('category has 
been edited'));
-                       return $receipt;
-               }
-
-               function delete($id,$type,$type_id)
-               {
-                       $table = $this->select_table($type,$type_id);
-
-                       $this->db->query("DELETE FROM $table WHERE id='" . $id 
. "'",__LINE__,__FILE__);
-               }
-       }
-?>
+                       $this->db2              = $this->bocommon->new_db();
+
+                       $this->join                     = $this->bocommon->join;
+                       $this->like                     = $this->bocommon->like;
+               }
+
+               function read($data)
+               {
+                       if(is_array($data))
+                       {
+                               if ($data['start'])
+                               {
+                                       $start=$data['start'];
+                               }
+                               else
+                               {
+                                       $start=0;
+                               }
+                               $query          = 
(isset($data['query'])?$data['query']:'');
+                               $sort           = 
(isset($data['sort'])?$data['sort']:'DESC');
+                               $order          = 
(isset($data['order'])?$data['order']:'');
+                               $type           = 
(isset($data['type'])?$data['type']:'');
+                               $type_id                = 
(isset($data['type_id'])?$data['type_id']:'');
+                               $allrows        = 
(isset($data['allrows'])?$data['allrows']:'');
+                       }
+
+                       if(!$type)
+                       {
+                               return;
+                       }
+                       if ($order)
+                       {
+                               $ordermethod = " order by $order $sort";
+
+                       }
+                       else
+                       {
+                               $ordermethod = ' order by id asc';
+                       }
+
+                       $table = $this->select_table($type,$type_id);
+
+                       if($query)
+                       {
+                               $query = ereg_replace("'",'',$query);
+                               $query = ereg_replace('"','',$query);
+
+                               $querymethod = " where id $this->like 
'%$query%' or descr $this->like '%$query%'";
+                       }
+
+                       $sql = "SELECT * FROM $table $querymethod";
+
+                       $this->db2->query($sql,__LINE__,__FILE__);
+                       $this->total_records = $this->db2->num_rows();
+
+                       if(!$allrows)
+                       {
+                               $this->db->limit_query($sql . 
$ordermethod,$start,__LINE__,__FILE__);
+                       }
+                       else
+                       {
+                               $this->db->query($sql . 
$ordermethod,__LINE__,__FILE__);
+                       }
+
+                       while ($this->db->next_record())
+                       {
+                               $category[] = array
+                               (
+                                       'id'    => $this->db->f('id'),
+                                       'descr' => 
stripslashes($this->db->f('descr'))
+                               );
+                       }
+                       return $category;
+               }
+
+
+               function select_table($type,$type_id)
+               {
+
+                       switch($type)
+                       {
+                               case 'dim_d':
+                                       $table='fm_dim_d';
+                                       break;
+                               case 'tender_chapter':
+                                       $table='fm_chapter';
+                                       break;
+                               case 'ticket':
+                                       $table='fm_tts_category';
+                                       break;
+                               case 'request':
+                                       $table='fm_request_category';
+                                       break;
+                               case 'wo':
+                                       $table='fm_workorder_category';
+                                       break;
+                               case 'location':
+                                       $table='fm_location' . $type_id . 
'_category';
+                                       break;
+                               case 'meter':
+                                       $table='fm_meter_category';
+                                       break;
+                               case 'document':
+                                       $table='fm_document_category';
+                                       break;
+                               case 'owner':
+                                       $table='fm_owner_category';
+                                       break;
+                               case 'tenant':
+                                       $table='fm_tenant_category';
+                                       break;
+                               case 'vendor':
+                                       $table='fm_vendor_category';
+                                       break;
+                               case 'district':
+                                       $table='fm_district';
+                                       break;
+                               case 'street':
+                                       $table='fm_streetaddress';
+                                       break;
+                               case 's_agreement':
+                                       $table='fm_s_agreement_category';
+                                       break;
+                               case 'tenant_claim':
+                                       $table='fm_tenant_claim_category';
+                                       break;
+                               case 'wo_hours':
+                                       $table='fm_wo_hours_category';
+                                       break;
+                               case 'r_condition_type':
+                                       $table='fm_request_condition_type';
+                                       break;
+                       }
+
+                       return $table;
+               }
+
+
+               function read_single($id,$type,$type_id)
+               {
+
+                       $table = $this->select_table($type,$type_id);
+
+                       $sql = "SELECT * FROM $table  where id='$id'";
+
+                       $this->db->query($sql,__LINE__,__FILE__);
+
+                       if ($this->db->next_record())
+                       {
+                               $category['id']         = $this->db->f('id');
+                               $category['descr']      = 
stripslashes($this->db->f('descr'));
+
+                               return $category;
+                       }
+               }
+
+               function add($category,$type,$type_id)
+               {
+                       $table = $this->select_table($type,$type_id);
+
+                       $category['descr'] = 
$this->db->db_addslashes($category['descr']);
+
+                       $this->db->query("INSERT INTO $table (id, descr) "
+                               . "VALUES ('" . $category['id'] . "','" . 
$category['descr']. "')",__LINE__,__FILE__);
+
+                       $receipt['message'][]=array('msg'=>lang('category has 
been saved'));
+                       return $receipt;
+               }
+
+               function edit($category,$type,$type_id)
+               {
+
+                       $table = $this->select_table($type,$type_id);
+
+                       $category['descr'] = 
$this->db->db_addslashes($category['descr']);
+
+                       $this->db->query("UPDATE $table set descr='" . 
$category['descr']
+                                                       . "' WHERE id='" . 
$category['id']. "'",__LINE__,__FILE__);
+
+
+                       $receipt['message'][]=array('msg'=>lang('category has 
been edited'));
+                       return $receipt;
+               }
+
+               function delete($id,$type,$type_id)
+               {
+                       $table = $this->select_table($type,$type_id);
+
+                       $this->db->query("DELETE FROM $table WHERE id='" . $id 
. "'",__LINE__,__FILE__);
+               }
+       }
+?>
Index: property/inc/class.socustom.inc.php
diff -u property/inc/class.socustom.inc.php:1.7 
property/inc/class.socustom.inc.php:1.8
--- property/inc/class.socustom.inc.php:1.7     Fri Jan 27 14:05:43 2006
+++ property/inc/class.socustom.inc.php Mon Jan 30 22:14:19 2006
@@ -1,333 +1,329 @@
-<?php
-       /**
-       * phpGroupWare - property: a Facilities Management System.
-       *
-       * @author Sigurd Nes <address@hidden>
-       * @copyright Copyright (C) 2003-2005 Free Software Foundation, Inc. 
http://www.fsf.org/
-       * @license http://www.gnu.org/licenses/gpl.html GNU General Public 
License
-       * @internal Development of this application was funded by 
http://www.bergen.kommune.no/bbb_/ekstern/
-       * @package property
-       * @subpackage custom
-       * @version $Id: class.socustom.inc.php,v 1.7 2006/01/27 14:05:43 
sigurdne Exp $
-       */
-
-       /**
-        * Description
-        * @package property
-        */
-
-       class socustom
-       {
-
-               function socustom()
-               {
-                       $this->currentapp       = 
$GLOBALS['phpgw_info']['flags']['currentapp'];
-                       $this->account  = 
$GLOBALS['phpgw_info']['user']['account_id'];
-                       $this->bocommon         = 
CreateObject($this->currentapp.'.bocommon');
+<?php
+       /**
+       * phpGroupWare - property: a Facilities Management System.
+       *
+       * @author Sigurd Nes <address@hidden>
+       * @copyright Copyright (C) 2003-2005 Free Software Foundation, Inc. 
http://www.fsf.org/
+       * @license http://www.gnu.org/licenses/gpl.html GNU General Public 
License
+       * @internal Development of this application was funded by 
http://www.bergen.kommune.no/bbb_/ekstern/
+       * @package property
+       * @subpackage custom
+       * @version $Id: class.socustom.inc.php,v 1.8 2006/01/30 22:14:19 
sigurdne Exp $
+       */
+
+       /**
+        * Description
+        * @package property
+        */
+
+       class socustom
+       {
+               function socustom()
+               {
+                       $this->currentapp       = 
$GLOBALS['phpgw_info']['flags']['currentapp'];
+                       $this->account  = 
$GLOBALS['phpgw_info']['user']['account_id'];
+                       $this->bocommon         = 
CreateObject($this->currentapp.'.bocommon');
                        $this->db               = $this->bocommon->new_db();
                        $this->db2              = $this->bocommon->new_db();
-                       $this->account          = 
$GLOBALS['phpgw_info']['user']['account_id'];
-                       $this->join                     = $this->bocommon->join;
-                       $this->like                     = $this->bocommon->like;
-               }
-
-
-               function read($data)
-               {
-                       if(is_array($data))
-                       {
-                               $start  = 
(isset($data['start'])?$data['start']:0);
-                               $filter = 
(isset($data['filter'])?$data['filter']:'none');
-                               $query = 
(isset($data['query'])?$data['query']:'');
-                               $sort = 
(isset($data['sort'])?$data['sort']:'DESC');
-                               $order = 
(isset($data['order'])?$data['order']:'');
-                               $cat_id = 
(isset($data['cat_id'])?$data['cat_id']:0);
-                               $allrows                = 
(isset($data['allrows'])?$data['allrows']:'');
-                       }
-
-                       if ($order)
-                       {
-                               $ordermethod = " order by $order $sort";
-                       }
-                       else
-                       {
-                               $ordermethod = ' order by id DESC';
-                       }
-
-
-                       $where = 'WHERE';
-                       if ($cat_id > 0)
-                       {
-                               $filtermethod .= " $where category='$cat_id' ";
-                               $where = 'AND';
-
-                       }
-
-                       if($query)
-                       {
-                               $query = ereg_replace("'",'',$query);
-                               $query = ereg_replace('"','',$query);
-
-                               $querymethod = " $where ( abid = '$query' or 
org_name $this->like '%$query%')";
-                       }
-
-                       $sql = "SELECT * FROM fm_custom $filtermethod 
$querymethod";
-
-                       $this->db2->query($sql,__LINE__,__FILE__);
-                       $this->total_records = $this->db2->num_rows();
-
-                       if(!$allrows)
-                       {
-                               $this->db->limit_query($sql . 
$ordermethod,$start,__LINE__,__FILE__);
-                       }
-                       else
-                       {
-                               $this->db->query($sql . 
$ordermethod,__LINE__,__FILE__);
-                       }
-
-                       while ($this->db->next_record())
-                       {
-                               $customs[] = array
-                               (
-                                       'custom_id'             => 
$this->db->f('id'),
-                                       'name'                  => 
stripslashes($this->db->f('name')),
-                                       'entry_date'    => 
$this->db->f('entry_date'),
-                                       'user'                  => 
$GLOBALS['phpgw']->accounts->id2name($this->db->f('user_id'))
-                               );
-                       }
-                       return $customs;
-               }
-
-               function read_single($custom_id)
-               {
-                       $this->db->query("select * from fm_custom where 
id='$custom_id'",__LINE__,__FILE__);
-
-                       if ($this->db->next_record())
-                       {
-                               $custom['id']                   = 
(int)$this->db->f('id');
-                               $custom['name']                 = 
stripslashes($this->db->f('name'));
-                               $custom['sql_text']                     = 
stripslashes($this->db->f('sql_text'));
-                               $custom['entry_date']   = 
$this->db->f('entry_date');
-                               $custom['cols']                 = 
$this->read_cols($custom_id);
-                       }
-
-                       return $custom;
-               }
-
-               function read_cols($custom_id)
-               {
-                       $sql = "SELECT * FROM fm_custom_cols WHERE 
custom_id=$custom_id ORDER by sorting";
-                       $this->db->query($sql);
-
-                       while ($this->db->next_record())
-                       {
-                               $choice[] = array
-                               (
-                                       'id'    => $this->db->f('id'),
-                                       'name'  => $this->db->f('name'),
-                                       'descr' => $this->db->f('descr'),
-                                       'sorting'=> $this->db->f('sorting')
-                               );
-
-                       }
-                       return $choice;
-               }
-
-               function read_custom_name($custom_id)
-               {
-                       $this->db->query("select name from fm_custom where 
id='$custom_id'",__LINE__,__FILE__);
-
-                       if ($this->db->next_record())
-                       {
-                               $name                   = 
stripslashes($this->db->f('name'));
-                       }
-
-                       return $name;
-               }
-
-               function add($custom)
-               {
-                       $custom['name'] = 
$this->db->db_addslashes($custom['name']);
-                       $custom['sql_text'] = 
$this->db->db_addslashes($custom['sql_text']);
-
-                       $this->db->transaction_begin();
-
-                       $id = $this->bocommon->next_id('fm_custom');
-
-                       $this->db->query("INSERT INTO fm_custom 
(id,entry_date,sql_text,name,user_id) "
-                               . "VALUES ($id,'" . time() . "','" . 
$custom['sql_text'] . "','" . $custom['name'] . "'," . $this->account . 
")",__LINE__,__FILE__);
-
-                       $receipt['custom_id']= $id;
-
-
-                       $this->db->transaction_commit();
-
-                       $receipt['message'][] = array('msg'=>lang('custom %1 
has been saved',$receipt['custom_id']));
-                       return $receipt;
-               }
-
-               function edit($custom)
-               {
-                       $custom['name'] = 
$this->db->db_addslashes($custom['name']);
-                       $custom['sql_text'] = 
$this->db->db_addslashes($custom['sql_text']);
-
-                       $this->db->query("UPDATE fm_custom set sql_text='" . 
$custom['sql_text'] . "', entry_date='" . time() . "', name='" . 
$custom['name'] . "' WHERE id=" . 
intval($custom['custom_id']),__LINE__,__FILE__);
-
-                       if($custom['new_name'])
-                       {
-                               $column_id = 
$this->bocommon->next_id('fm_custom_cols' 
,array('custom_id'=>$custom['custom_id']));
-
-                               $sql = "SELECT max(sorting) as max_sort FROM 
fm_custom_cols WHERE custom_id=" . $custom['custom_id'];
-                               $this->db->query($sql);
-                               $this->db->next_record();
-                               $sorting        = $this->db->f('max_sort')+1;
-
-
-                               $values= array(
-                                       $custom['custom_id'],
-                                       $column_id,
-                                       $custom['new_name'],
-                                       
$this->db->db_addslashes($custom['new_descr']),
-                                       $sorting
-                                       );
-
-                               $values = 
$this->bocommon->validate_db_insert($values);
-
-                               $this->db->query("INSERT INTO fm_custom_cols 
(custom_id,id,name,descr,sorting) "
-                               . "VALUES ($values)");
-                       }
-
-
-                       if($custom['delete_cols'])
-                       {
-                               for ($i=0;$i<count($custom['delete_cols']);$i++)
-                               {
-
-                                       $sql = "SELECT sorting FROM 
fm_custom_cols where custom_id=" . $custom['custom_id'] . " AND id=" . 
$custom['delete_cols'][$i];
-                                       $this->db->query($sql);
-                                       $this->db->next_record();
-                                       $sorting        = 
$this->db->f('sorting');
-                                       $sql2 = "SELECT max(sorting) as 
max_sort FROM fm_custom_cols";
-                                       $this->db->query($sql2);
-                                       $this->db->next_record();
-                                       $max_sort       = 
$this->db->f('max_sort');
-
-                                       if($max_sort>$sorting)
-                                       {
-                                               $sql = "UPDATE fm_custom_cols 
set sorting=sorting-1 WHERE sorting > $sorting AND custom_id=" . 
$custom['custom_id'];
-                                               $this->db->query($sql);
-                                       }
-
-
-                                       $this->db->query("DELETE FROM 
fm_custom_cols WHERE  custom_id=" . $custom['custom_id']  ." AND id=" . 
$custom['delete_cols'][$i]);
-                               }
-                       }
-
-                       $receipt['custom_id']= $custom['custom_id'];
-                       $receipt['message'][] = array('msg'=>lang('custom %1 
has been edited',$custom['custom_id']));
-                       return $receipt;
-               }
-
-               function resort($data)
-               {
-//html_print_r($data);
-                       if(is_array($data))
-                       {
-                               $resort = 
(isset($data['resort'])?$data['resort']:'up');
-                               $custom_id = 
(isset($data['id'])?$data['custom_id']:'');
-                               $id = (isset($data['id'])?$data['id']:'');
-                       }
-
-                       $sql = "SELECT sorting FROM fm_custom_cols WHERE 
custom_id = $custom_id AND id=$id";
-                       $this->db->query($sql);
-                       $this->db->next_record();
-                       $sorting        = $this->db->f('sorting');
-                       $sql = "SELECT max(sorting) as max_sort FROM 
fm_custom_cols WHERE custom_id = $custom_id";
-                       $this->db->query($sql);
-                       $this->db->next_record();
-                       $max_sort       = $this->db->f('max_sort');
-                       switch($resort)
-                       {
-                               case 'up':
-                                       if($sorting>1)
-                                       {
-                                               $sql = "UPDATE fm_custom_cols 
set sorting=$sorting WHERE custom_id = $custom_id AND sorting =" . ($sorting-1);
-                                               $this->db->query($sql);
-                                               $sql = "UPDATE fm_custom_cols 
set sorting=" . ($sorting-1) ." WHERE custom_id = $custom_id AND id=$id";
-                                               $this->db->query($sql);
-                                       }
-                                       break;
-                               case 'down':
-                                       if($max_sort > $sorting)
-                                       {
-                                               $sql = "UPDATE fm_custom_cols 
set sorting=$sorting WHERE custom_id = $custom_id AND sorting =" . ($sorting+1);
-                                               $this->db->query($sql);
-                                               $sql = "UPDATE fm_custom_cols 
set sorting=" . ($sorting+1) ." WHERE custom_id = $custom_id AND id=$id";
-                                               $this->db->query($sql);
-                                       }
-                                       break;
-                               default:
-                                       return;
-                                       break;
-                       }
-               }
-
-               function read_custom($data)
-               {
-                       if(is_array($data))
-                       {
-                               $start  = 
(isset($data['start'])?$data['start']:0);
-                               $filter = 
(isset($data['filter'])?$data['filter']:'none');
-                               $query = 
(isset($data['query'])?$data['query']:'');
-                               $sort = 
(isset($data['sort'])?$data['sort']:'DESC');
-                               $order = 
(isset($data['order'])?$data['order']:'');
-                               $cat_id = 
(isset($data['cat_id'])?$data['cat_id']:0);
-                               $allrows                = 
(isset($data['allrows'])?$data['allrows']:'');
-                               $custom_id              = 
(isset($data['custom_id'])?$data['custom_id']:'');
-                       }
-
-
-                       $this->db->query("select sql_text from fm_custom where 
id='$custom_id'",__LINE__,__FILE__);
-                       $this->db->next_record();
-                       $sql = stripslashes($this->db->f('sql_text'));
-
-                       $uicols = $this->read_cols($custom_id);
-                       $this->uicols = $uicols;
-
-                       $this->db2->query($sql,__LINE__,__FILE__);
-                       $this->total_records = $this->db2->num_rows();
-
-                       if(!$allrows)
-                       {
-                               $this->db->limit_query($sql . 
$ordermethod,$start,__LINE__,__FILE__);
-                       }
-                       else
-                       {
-                               $this->db->query($sql . 
$ordermethod,__LINE__,__FILE__);
-                       }
-
-                       $n=count($uicols);
-                       $j=0;
-                       while ($this->db->next_record())
-                       {
-                               for ($i=0;$i<$n;$i++)
-                               {
-                                       $custom[$j][$uicols[$i]['name']] = 
$this->db->f($uicols[$i]['name']);
-                                       $custom[$j]['grants'] = 
(int)$grants[$this->db->f('user_id')];
-                               }
-                       $j++;
-                       }
-
-//_debug_array($custom);
-                       return $custom;
-               }
-
-               function delete($custom_id)
-               {
-                       $this->db->query('DELETE FROM fm_custom WHERE id=' . 
intval($custom_id),__LINE__,__FILE__);
-                       $this->db->query('DELETE FROM fm_custom_cols WHERE 
custom_id=' . intval($custom_id),__LINE__,__FILE__);
-               }
-
-
-       }
-?>
+                       $this->account          = 
$GLOBALS['phpgw_info']['user']['account_id'];
+
+                       $this->join                     = $this->bocommon->join;
+                       $this->like                     = $this->bocommon->like;
+               }
+
+               function read($data)
+               {
+                       if(is_array($data))
+                       {
+                               $start  = 
(isset($data['start'])?$data['start']:0);
+                               $filter = 
(isset($data['filter'])?$data['filter']:'none');
+                               $query = 
(isset($data['query'])?$data['query']:'');
+                               $sort = 
(isset($data['sort'])?$data['sort']:'DESC');
+                               $order = 
(isset($data['order'])?$data['order']:'');
+                               $cat_id = 
(isset($data['cat_id'])?$data['cat_id']:0);
+                               $allrows                = 
(isset($data['allrows'])?$data['allrows']:'');
+                       }
+
+                       if ($order)
+                       {
+                               $ordermethod = " order by $order $sort";
+                       }
+                       else
+                       {
+                               $ordermethod = ' order by id DESC';
+                       }
+
+
+                       $where = 'WHERE';
+                       if ($cat_id > 0)
+                       {
+                               $filtermethod .= " $where category='$cat_id' ";
+                               $where = 'AND';
+
+                       }
+
+                       if($query)
+                       {
+                               $query = ereg_replace("'",'',$query);
+                               $query = ereg_replace('"','',$query);
+
+                               $querymethod = " $where ( abid = '$query' or 
org_name $this->like '%$query%')";
+                       }
+
+                       $sql = "SELECT * FROM fm_custom $filtermethod 
$querymethod";
+
+                       $this->db2->query($sql,__LINE__,__FILE__);
+                       $this->total_records = $this->db2->num_rows();
+
+                       if(!$allrows)
+                       {
+                               $this->db->limit_query($sql . 
$ordermethod,$start,__LINE__,__FILE__);
+                       }
+                       else
+                       {
+                               $this->db->query($sql . 
$ordermethod,__LINE__,__FILE__);
+                       }
+
+                       while ($this->db->next_record())
+                       {
+                               $customs[] = array
+                               (
+                                       'custom_id'             => 
$this->db->f('id'),
+                                       'name'                  => 
stripslashes($this->db->f('name')),
+                                       'entry_date'    => 
$this->db->f('entry_date'),
+                                       'user'                  => 
$GLOBALS['phpgw']->accounts->id2name($this->db->f('user_id'))
+                               );
+                       }
+                       return $customs;
+               }
+
+               function read_single($custom_id)
+               {
+                       $this->db->query("select * from fm_custom where 
id='$custom_id'",__LINE__,__FILE__);
+
+                       if ($this->db->next_record())
+                       {
+                               $custom['id']                   = 
(int)$this->db->f('id');
+                               $custom['name']                 = 
stripslashes($this->db->f('name'));
+                               $custom['sql_text']                     = 
stripslashes($this->db->f('sql_text'));
+                               $custom['entry_date']   = 
$this->db->f('entry_date');
+                               $custom['cols']                 = 
$this->read_cols($custom_id);
+                       }
+
+                       return $custom;
+               }
+
+               function read_cols($custom_id)
+               {
+                       $sql = "SELECT * FROM fm_custom_cols WHERE 
custom_id=$custom_id ORDER by sorting";
+                       $this->db->query($sql);
+
+                       while ($this->db->next_record())
+                       {
+                               $choice[] = array
+                               (
+                                       'id'    => $this->db->f('id'),
+                                       'name'  => $this->db->f('name'),
+                                       'descr' => $this->db->f('descr'),
+                                       'sorting'=> $this->db->f('sorting')
+                               );
+
+                       }
+                       return $choice;
+               }
+
+               function read_custom_name($custom_id)
+               {
+                       $this->db->query("select name from fm_custom where 
id='$custom_id'",__LINE__,__FILE__);
+
+                       if ($this->db->next_record())
+                       {
+                               $name                   = 
stripslashes($this->db->f('name'));
+                       }
+
+                       return $name;
+               }
+
+               function add($custom)
+               {
+                       $custom['name'] = 
$this->db->db_addslashes($custom['name']);
+                       $custom['sql_text'] = 
$this->db->db_addslashes($custom['sql_text']);
+
+                       $this->db->transaction_begin();
+
+                       $id = $this->bocommon->next_id('fm_custom');
+
+                       $this->db->query("INSERT INTO fm_custom 
(id,entry_date,sql_text,name,user_id) "
+                               . "VALUES ($id,'" . time() . "','" . 
$custom['sql_text'] . "','" . $custom['name'] . "'," . $this->account . 
")",__LINE__,__FILE__);
+
+                       $receipt['custom_id']= $id;
+
+
+                       $this->db->transaction_commit();
+
+                       $receipt['message'][] = array('msg'=>lang('custom %1 
has been saved',$receipt['custom_id']));
+                       return $receipt;
+               }
+
+               function edit($custom)
+               {
+                       $custom['name'] = 
$this->db->db_addslashes($custom['name']);
+                       $custom['sql_text'] = 
$this->db->db_addslashes($custom['sql_text']);
+
+                       $this->db->query("UPDATE fm_custom set sql_text='" . 
$custom['sql_text'] . "', entry_date='" . time() . "', name='" . 
$custom['name'] . "' WHERE id=" . 
intval($custom['custom_id']),__LINE__,__FILE__);
+
+                       if($custom['new_name'])
+                       {
+                               $column_id = 
$this->bocommon->next_id('fm_custom_cols' 
,array('custom_id'=>$custom['custom_id']));
+
+                               $sql = "SELECT max(sorting) as max_sort FROM 
fm_custom_cols WHERE custom_id=" . $custom['custom_id'];
+                               $this->db->query($sql);
+                               $this->db->next_record();
+                               $sorting        = $this->db->f('max_sort')+1;
+
+
+                               $values= array(
+                                       $custom['custom_id'],
+                                       $column_id,
+                                       $custom['new_name'],
+                                       
$this->db->db_addslashes($custom['new_descr']),
+                                       $sorting
+                                       );
+
+                               $values = 
$this->bocommon->validate_db_insert($values);
+
+                               $this->db->query("INSERT INTO fm_custom_cols 
(custom_id,id,name,descr,sorting) "
+                               . "VALUES ($values)");
+                       }
+
+
+                       if($custom['delete_cols'])
+                       {
+                               for ($i=0;$i<count($custom['delete_cols']);$i++)
+                               {
+
+                                       $sql = "SELECT sorting FROM 
fm_custom_cols where custom_id=" . $custom['custom_id'] . " AND id=" . 
$custom['delete_cols'][$i];
+                                       $this->db->query($sql);
+                                       $this->db->next_record();
+                                       $sorting        = 
$this->db->f('sorting');
+                                       $sql2 = "SELECT max(sorting) as 
max_sort FROM fm_custom_cols";
+                                       $this->db->query($sql2);
+                                       $this->db->next_record();
+                                       $max_sort       = 
$this->db->f('max_sort');
+
+                                       if($max_sort>$sorting)
+                                       {
+                                               $sql = "UPDATE fm_custom_cols 
set sorting=sorting-1 WHERE sorting > $sorting AND custom_id=" . 
$custom['custom_id'];
+                                               $this->db->query($sql);
+                                       }
+
+
+                                       $this->db->query("DELETE FROM 
fm_custom_cols WHERE  custom_id=" . $custom['custom_id']  ." AND id=" . 
$custom['delete_cols'][$i]);
+                               }
+                       }
+
+                       $receipt['custom_id']= $custom['custom_id'];
+                       $receipt['message'][] = array('msg'=>lang('custom %1 
has been edited',$custom['custom_id']));
+                       return $receipt;
+               }
+
+               function resort($data)
+               {
+//html_print_r($data);
+                       if(is_array($data))
+                       {
+                               $resort = 
(isset($data['resort'])?$data['resort']:'up');
+                               $custom_id = 
(isset($data['id'])?$data['custom_id']:'');
+                               $id = (isset($data['id'])?$data['id']:'');
+                       }
+
+                       $sql = "SELECT sorting FROM fm_custom_cols WHERE 
custom_id = $custom_id AND id=$id";
+                       $this->db->query($sql);
+                       $this->db->next_record();
+                       $sorting        = $this->db->f('sorting');
+                       $sql = "SELECT max(sorting) as max_sort FROM 
fm_custom_cols WHERE custom_id = $custom_id";
+                       $this->db->query($sql);
+                       $this->db->next_record();
+                       $max_sort       = $this->db->f('max_sort');
+                       switch($resort)
+                       {
+                               case 'up':
+                                       if($sorting>1)
+                                       {
+                                               $sql = "UPDATE fm_custom_cols 
set sorting=$sorting WHERE custom_id = $custom_id AND sorting =" . ($sorting-1);
+                                               $this->db->query($sql);
+                                               $sql = "UPDATE fm_custom_cols 
set sorting=" . ($sorting-1) ." WHERE custom_id = $custom_id AND id=$id";
+                                               $this->db->query($sql);
+                                       }
+                                       break;
+                               case 'down':
+                                       if($max_sort > $sorting)
+                                       {
+                                               $sql = "UPDATE fm_custom_cols 
set sorting=$sorting WHERE custom_id = $custom_id AND sorting =" . ($sorting+1);
+                                               $this->db->query($sql);
+                                               $sql = "UPDATE fm_custom_cols 
set sorting=" . ($sorting+1) ." WHERE custom_id = $custom_id AND id=$id";
+                                               $this->db->query($sql);
+                                       }
+                                       break;
+                               default:
+                                       return;
+                                       break;
+                       }
+               }
+
+               function read_custom($data)
+               {
+                       if(is_array($data))
+                       {
+                               $start  = 
(isset($data['start'])?$data['start']:0);
+                               $filter = 
(isset($data['filter'])?$data['filter']:'none');
+                               $query = 
(isset($data['query'])?$data['query']:'');
+                               $sort = 
(isset($data['sort'])?$data['sort']:'DESC');
+                               $order = 
(isset($data['order'])?$data['order']:'');
+                               $cat_id = 
(isset($data['cat_id'])?$data['cat_id']:0);
+                               $allrows                = 
(isset($data['allrows'])?$data['allrows']:'');
+                               $custom_id              = 
(isset($data['custom_id'])?$data['custom_id']:'');
+                       }
+
+                       $this->db->query("select sql_text from fm_custom where 
id='$custom_id'",__LINE__,__FILE__);
+                       $this->db->next_record();
+                       $sql = stripslashes($this->db->f('sql_text'));
+
+                       $uicols = $this->read_cols($custom_id);
+                       $this->uicols = $uicols;
+
+                       $this->db2->query($sql,__LINE__,__FILE__);
+                       $this->total_records = $this->db2->num_rows();
+
+                       if(!$allrows)
+                       {
+                               $this->db->limit_query($sql . 
$ordermethod,$start,__LINE__,__FILE__);
+                       }
+                       else
+                       {
+                               $this->db->query($sql . 
$ordermethod,__LINE__,__FILE__);
+                       }
+
+                       $n=count($uicols);
+                       $j=0;
+                       while ($this->db->next_record())
+                       {
+                               for ($i=0;$i<$n;$i++)
+                               {
+                                       $custom[$j][$uicols[$i]['name']] = 
$this->db->f($uicols[$i]['name']);
+                                       $custom[$j]['grants'] = 
(int)$grants[$this->db->f('user_id')];
+                               }
+                       $j++;
+                       }
+
+//_debug_array($custom);
+                       return $custom;
+               }
+
+               function delete($custom_id)
+               {
+                       $this->db->query('DELETE FROM fm_custom WHERE id=' . 
intval($custom_id),__LINE__,__FILE__);
+                       $this->db->query('DELETE FROM fm_custom_cols WHERE 
custom_id=' . intval($custom_id),__LINE__,__FILE__);
+               }
+       }
+?>
Index: property/inc/class.sodocument.inc.php
diff -u property/inc/class.sodocument.inc.php:1.8 
property/inc/class.sodocument.inc.php:1.9
--- property/inc/class.sodocument.inc.php:1.8   Fri Jan 27 14:05:43 2006
+++ property/inc/class.sodocument.inc.php       Mon Jan 30 22:14:19 2006
@@ -1,643 +1,642 @@
-<?php
-       /**
-       * phpGroupWare - property: a Facilities Management System.
-       *
-       * @author Sigurd Nes <address@hidden>
-       * @copyright Copyright (C) 2003-2005 Free Software Foundation, Inc. 
http://www.fsf.org/
-       * @license http://www.gnu.org/licenses/gpl.html GNU General Public 
License
-       * @internal Development of this application was funded by 
http://www.bergen.kommune.no/bbb_/ekstern/
-       * @package property
-       * @subpackage document
-       * @version $Id: class.sodocument.inc.php,v 1.8 2006/01/27 14:05:43 
sigurdne Exp $
-       */
-
-       /**
-        * Description
-        * @package property
-        */
-
-       class sodocument
-       {
-
-               function sodocument()
-               {
-                       $this->currentapp       = 
$GLOBALS['phpgw_info']['flags']['currentapp'];
-                       $this->account          = 
$GLOBALS['phpgw_info']['user']['account_id'];
-                       $this->bocommon         = 
CreateObject($this->currentapp.'.bocommon',True);
+<?php
+       /**
+       * phpGroupWare - property: a Facilities Management System.
+       *
+       * @author Sigurd Nes <address@hidden>
+       * @copyright Copyright (C) 2003-2005 Free Software Foundation, Inc. 
http://www.fsf.org/
+       * @license http://www.gnu.org/licenses/gpl.html GNU General Public 
License
+       * @internal Development of this application was funded by 
http://www.bergen.kommune.no/bbb_/ekstern/
+       * @package property
+       * @subpackage document
+       * @version $Id: class.sodocument.inc.php,v 1.9 2006/01/30 22:14:19 
sigurdne Exp $
+       */
+
+       /**
+        * Description
+        * @package property
+        */
+
+       class sodocument
+       {
+
+               function sodocument()
+               {
+                       $this->currentapp       = 
$GLOBALS['phpgw_info']['flags']['currentapp'];
+                       $this->account          = 
$GLOBALS['phpgw_info']['user']['account_id'];
+                       $this->bocommon         = 
CreateObject($this->currentapp.'.bocommon',True);
                        $this->db               = $this->bocommon->new_db();
                        $this->db2              = $this->bocommon->new_db();
-                       $this->historylog       = 
CreateObject($this->currentapp.'.historylog','document');
-                       $this->vfs                      = 
CreateObject('phpgwapi.vfs');
-                       $this->rootdir          = $this->vfs->basedir;
-                       $this->fakebase         = $this->vfs->fakebase;
-
-                       $this->socommon         = 
CreateObject($this->currentapp.'.socommon');
-
-                       $this->join                     = $this->socommon->join;
-                       $this->like                     = $this->socommon->like;
-               }
-
-               function select_doc_type_list()
-               {
-                       $this->db->query("SELECT id, descr FROM 
fm_document_category  ORDER BY descr ");
-
-                       $i = 0;
-                       while ($this->db->next_record())
-                       {
-                               $categories[$i]['id']                           
= $this->db->f('id');
-                               $categories[$i]['name']                         
= stripslashes($this->db->f('descr'));
-                               $i++;
-                       }
-                       return $categories;
-               }
-
-               function read_single_category($id='')
-               {
-                       $this->db->query("SELECT descr FROM 
fm_document_category where id='$id' ");
-                       $this->db->next_record();
-                       return $this->db->f('descr');
-               }
-
-               function select_status_list()
-               {
-                       $this->db->query("SELECT id, descr FROM 
fm_document_status ORDER BY id ");
-
-                       $i = 0;
-                       while ($this->db->next_record())
-                       {
-                               $status_entries[$i]['id']                       
        = $this->db->f('id');
-                               $status_entries[$i]['name']                     
        = stripslashes($this->db->f('descr'));
-                               $i++;
-                       }
-                       return $status_entries;
-               }
-
-               function select_branch_list()
-               {
-                       $this->db->query("SELECT id, descr FROM fm_branch ORDER 
BY id ");
-
-                       $i = 0;
-                       while ($this->db->next_record())
-                       {
-                               $branch_entries[$i]['id']                       
        = $this->db->f('id');
-                               $branch_entries[$i]['name']                     
        = stripslashes($this->db->f('descr'));
-                               $i++;
-                       }
-                       return $branch_entries;
-               }
-
-               function read($data)
-               {
-                       if(is_array($data))
-                       {
-                               if ($data['start'])
-                               {
-                                       $start=$data['start'];
-                               }
-                               else
-                               {
-                                       $start=0;
-                               }
-                               $filter = 
(isset($data['filter'])?$data['filter']:'');
-                               $query = 
(isset($data['query'])?$data['query']:'');
-                               $sort = 
(isset($data['sort'])?$data['sort']:'DESC');
-                               $order = 
(isset($data['order'])?$data['order']:'');
-                               $cat_id = 
(isset($data['cat_id'])?$data['cat_id']:0);
-                               $entity_id = 
(isset($data['entity_id'])?$data['entity_id']:'');
-                               $doc_type = 
(isset($data['doc_type'])?$data['doc_type']:0);
-                       }
-
-                       $sql = $this->bocommon->fm_cache('sql_document_' . 
$entity_id);
-
-                       if(!$sql)
-                       {
-
-                               $document_table = 'fm_document';
-
-                               $cols .= $document_table . '.location_code';
-                               $cols_return[] = 'location_code';
-
-//                             $cols .= ",$document_table.id as document_id";
-//                             $cols_return[]                          = 
'document_id';
-
-                               if ($entity_id)
-                               {
-                                       $cols .= ",$document_table.p_num as 
p_num";
-                                       $cols_return[]                          
= 'p_num';
-                                       $uicols['input_type'][]         = 
'text';
-                                       $uicols['name'][]                       
= 'p_num';
-                                       $uicols['descr'][]                      
= lang('ID');
-                                       $uicols['statustext'][]         = 
lang('ID');
-
-                                       $cols .= ',fm_entity_category.name as 
category';
-                                       $cols_return[]                          
= 'category';
-                                       $uicols['input_type'][]         = 
'text';
-                                       $uicols['name'][]                       
= 'category';
-                                       $uicols['descr'][]                      
= lang('Type');
-                                       $uicols['statustext'][]         = 
lang('Type');
-
-                                       $cols .= ",$document_table.p_entity_id";
-                                       $cols_return[]                          
= 'p_entity_id';
-                                       $cols .= ",$document_table.p_cat_id";
-                                       $cols_return[]                          
= 'p_cat_id';
-
-                                       $joinmethod .= " $this->join  
fm_entity_category ON (fm_entity_category.entity_id 
=$document_table.p_entity_id AND fm_entity_category.id = 
$document_table.p_cat_id))";
-                                       $paranthesis .='(';
-
-                               }
-
-
-                               $sql    = 
$this->bocommon->generate_sql(array('entity_table'=>$document_table,'cols'=>$cols,'cols_return'=>$cols_return,
-                                                                               
                                
'uicols'=>$uicols,'joinmethod'=>$joinmethod,'paranthesis'=>$paranthesis,'query'=>$query));
-
-                               $this->bocommon->fm_cache('sql_document_' . 
$entity_id,$sql);
-
-                               $this->uicols           = 
$this->bocommon->uicols;
-                               $cols_return            = 
$this->bocommon->cols_return;
-                               $type_id                        = 
$this->bocommon->type_id;
-                               $this->cols_extra       = 
$this->bocommon->cols_extra;
-
-                               $this->bocommon->fm_cache('uicols_document_' . 
$entity_id,$this->uicols);
-                               
$this->bocommon->fm_cache('cols_return_document_' . $entity_id,$cols_return);
-                               $this->bocommon->fm_cache('type_id_document_' . 
$entity_id,$type_id);
-                               
$this->bocommon->fm_cache('cols_extra_document_' . 
$entity_id,$this->cols_extra);
-
-                       }
-                       else
-                       {
-                               $this->uicols           = 
$this->bocommon->fm_cache('uicols_document_' . $entity_id);
-                               $cols_return            = 
$this->bocommon->fm_cache('cols_return_document_' . $entity_id);
-                               $type_id                        = 
$this->bocommon->fm_cache('type_id_document_' . $entity_id);
-                               $this->cols_extra       = 
$this->bocommon->fm_cache('cols_extra_document_' . $entity_id);
-                       }
-
-                       $groupmethod= " GROUP BY 
fm_document.location_code,fm_document.address  ";
-
-                       if ($entity_id)
-                       {
-
-                               $groupmethod.= " 
,fm_document.p_entity_id,fm_entity_category.name,fm_document.p_num,fm_document.p_cat_id";
-                       }
-
-                       if ($order)
-                       {
-                               $ordermethod = " order by fm_document.$order 
$sort";
-                       }
-                       else
-                       {
-                               $ordermethod = ' order by 
fm_document.location_code ASC';
-                       }
-
-                       $where= 'WHERE';
-
-                       if(!$entity_id)
-                       {
-                               $filtermethod .= " $where ( fm_document.p_num 
is NULL OR fm_document.p_num='') ";
-                               $where= 'AND';
-                       }
-
-                       if ($cat_id)
-                       {
-                               $filtermethod .= " $where 
fm_document.p_cat_id=$cat_id ";
-                               $where= 'AND';
-                       }
-
-                       if ($doc_type > 0)
-                       {
-                               $filtermethod .= " $where 
fm_document.category='$doc_type' ";
-                               $where= 'AND';
-                       }
-
-                       if ($filter!='all' && $filter)
-                       {
-                               $filtermethod .= " $where 
fm_document.user_id='$filter' ";
-                       }
-
-                       if($query)
-                       {
-                               $query = ereg_replace("'",'',$query);
-                               $query = ereg_replace('"','',$query);
-
-                               $querymethod = " $where (fm_document.address 
$this->like '%$query%' or fm_document.location_code $this->like '%$query%')";
-                       }
-
-
-                       $sql .= " $filtermethod $querymethod $groupmethod";
-
-//echo $sql;
-
-                       $this->db2->query($sql,__LINE__,__FILE__);
-                       $this->total_records = $this->db2->num_rows();
-                       $this->db->limit_query($sql . 
$ordermethod,$start,__LINE__,__FILE__);
-
-                       $j=0;
-                       while ($this->db->next_record())
-                       {
-                               for ($i=0;$i<count($cols_return);$i++)
-                               {
-                                       $document_list[$j][$cols_return[$i]] = 
stripslashes($this->db->f($cols_return[$i]));
-                               }
-
-                               $location_code= $this->db->f('location_code');
-                               $location = split('-',$location_code);
-                               for ($m=0;$m<count($location);$m++)
-                               {
-                                       $document_list[$j]['loc' . ($m+1)] = 
$location[$m];
-                                       
$document_list[$j]['query_location']['loc' . ($m+1)]=implode("-", 
array_slice($location, 0, ($m+1)));
-                               }
-
-                               $j++;
-                       }
-
-
-//_debug_array($document_list);
-                       return $document_list;
-               }
-
-               function read_at_location($data)
-               {
-                       if(is_array($data))
-                       {
-                               if ($data['start'])
-                               {
-                                       $start=$data['start'];
-                               }
-                               else
-                               {
-                                       $start=0;
-                               }
-                               $filter = 
(isset($data['filter'])?$data['filter']:'');
-                               $query = 
(isset($data['query'])?$data['query']:'');
-                               $sort = 
(isset($data['sort'])?$data['sort']:'DESC');
-                               $order = 
(isset($data['order'])?$data['order']:'');
-                               $entity_id = 
(isset($data['entity_id'])?$data['entity_id']:0);
-                               $cat_id = 
(isset($data['cat_id'])?$data['cat_id']:0);
-                               $doc_type = 
(isset($data['doc_type'])?$data['doc_type']:0);
-                               $location_code = 
(isset($data['location_code'])?$data['location_code']:'');
-                       }
-
-                       if ($order)
-                       {
-                               $ordermethod = " order by $order $sort";
-                       }
-                       else
-                       {
-                               $ordermethod = ' order by location_code ASC';
-                       }
-
-                       $filtermethod = " fm_document.location_code $this->like 
'%$location_code%'";
-
-                       if ($doc_type > 0)
-                       {
-                               $filtermethod .= " AND 
fm_document.category='$doc_type' ";
-                       }
-                       if ($cat_id > 0)
-                       {
-                               $filtermethod .= " AND 
fm_document.p_cat_id=$cat_id AND fm_document.p_entity_id=$entity_id ";
-                       }
-
-
-                       if ($filter)
-                       {
-                               $filtermethod .= " AND 
fm_document.user_id='$filter' ";
-                       }
-
-                       if($query)
-                       {
-                               $query = ereg_replace("'",'',$query);
-                               $query = ereg_replace('"','',$query);
-
-                               $querymethod = " AND fm_document.title 
$this->like '%$query%' or fm_document.document_name $this->like '%$query%'";
-                       }
-
-                       $sql = "SELECT fm_document.*, 
fm_document_category.descr as category FROM fm_document $this->join 
fm_document_category on fm_document.category = fm_document_category.id WHERE  
$filtermethod $querymethod ";
-
-                       $this->db2->query($sql,__LINE__,__FILE__);
-                       $this->total_records = $this->db2->num_rows();
-                       $this->db->limit_query($sql . 
$ordermethod,$start,__LINE__,__FILE__);
-
-                       while ($this->db->next_record())
-                       {
-                               $document_list[] = array
-                               (
-                                       'document_id'           => 
$this->db->f('id'),
-                                       'document_name'         => 
$this->db->f('document_name'),
-                                       'link'                          => 
$this->db->f('link'),
-                                       'title'                         => 
$this->db->f('title'),
-                                       'doc_type'                      => 
$this->db->f('category'),
-                                       'user_id'                       => 
$this->db->f('user_id')
-                                       );
-                       }
-//_debug_array($document_list);
-                       return $document_list;
-               }
-
-               function read_single($document_id)
-               {
-                       $sql = "SELECT * from fm_document where 
id='$document_id'";
-
-                       $this->db->query($sql,__LINE__,__FILE__);
-
-                       if ($this->db->next_record())
-                       {
-                               $document['document_id']                = 
$this->db->f('id');
-                               $document['title']                              
= $this->db->f('title');
-                               $document['document_name']              = 
$this->db->f('document_name');
-                               $document['link']                               
= $this->db->f('link');
-                               $document['location_code']              = 
$this->db->f('location_code');
-                               $document['branch_id']                  = 
$this->db->f('branch_id');
-                               $document['version']                    = 
$this->db->f('version');
-                               $document['vendor_id']                  = 
$this->db->f('vendor_id');
-                               $document['floor_id']                   = 
$this->db->f('floor_id');
-                               $document['descr']                              
= $this->db->f('descr');
-                               $document['status']                             
= $this->db->f('status');
-                               $document['user_id']                    = 
$this->db->f('user_id');
-                               $document['coordinator']                = 
$this->db->f('coordinator');
-                               $document['access']                             
= $this->db->f('access');
-                               $document['document_date']              = 
$this->db->f('document_date');
-                               $document['doc_type']                   = 
$this->db->f('category');
-                               $document['p_num']                              
= $this->db->f('p_num');
-                               $document['p_entity_id']                = 
$this->db->f('p_entity_id');
-                               $document['p_cat_id']                   = 
$this->db->f('p_cat_id');
-                       }
-
-//_debug_array($document);
-                               return $document;
-               }
-
-               function add($document)
-               {
-                       while (is_array($document['location']) && 
list($input_name,$value) = each($document['location']))
-                       {
-                               if($value)
-                               {
-                                       $cols[] = $input_name;
-                                       $vals[] = $value;
-                               }
-                       }
-
-                       while (is_array($document['extra']) && 
list($input_name,$value) = each($document['extra']))
-                       {
-                               if($value)
-                               {
-                                       $cols[] = $input_name;
-                                       $vals[] = $value;
-                               }
-                       }
-
-                       if($cols)
-                       {
-                               $cols   = "," . implode(",", $cols);
-                               $vals   = ",'" . implode("','", $vals) . "'";
-                       }
-
-                       if($document['street_name'])
-                       {
-                               $address[]= $document['street_name'];
-                               $address[]= $document['street_number'];
-                               $address        = 
$this->db->db_addslashes(implode(" ", $address));
-                       }
-
-                       if(!$address)
-                       {
-                               $address = 
$this->db->db_addslashes($document['location_name']);
-                       }
-
-                       $document['descr'] = 
$this->db->db_addslashes($document['descr']);
-                       $document['name'] = 
$this->db->db_addslashes($document['name']);
-                       $document['title'] = 
$this->db->db_addslashes($document['title']);
-//_debug_array($document);
-
-                       $values= array(
-                               $document['document_name'],
-                               $document['link'],
-                               $document['title'],
-                               'public',
-                               $document['doc_type'],
-                               time(),
-                               $document['document_date'],
-                               $document['version'],
-                               $document['coordinator'],
-                               $document['status'],
-                               $document['descr'],
-                               $document['location_code'],
-                               $address,
-                               $document['branch_id'],
-                               $document['vendor_id'],
-                               $this->account);
-
-                       $values = $this->bocommon->validate_db_insert($values);
-
-                       $this->db->query("INSERT INTO fm_document 
(document_name,link,title,access,category,entry_date,document_date,version,coordinator,status,"
-                               . 
"descr,location_code,address,branch_id,vendor_id,user_id $cols) "
-                               . "VALUES ($values $vals )",__LINE__,__FILE__);
-
-                       $receipt['document_id'] = 
$this->db->get_last_insert_id('fm_document','id');
-
-                       
$this->historylog->add('SO',$receipt['document_id'],$document['status']);
-                       
$this->historylog->add('TO',$receipt['document_id'],$document['doc_type']);
-                       if($document['coordinator'])
-                       {
-                               
$this->historylog->add('CO',$receipt['document_id'],$document['coordinator']);
-                       }
-                       if($document['document_name'])
-                       {
-                               
$this->historylog->add('FO',$receipt['document_id'],$document['coodocument_name']);
-                       }
-                       if($document['link'])
-                       {
-                               
$this->historylog->add('LO',$receipt['document_id'],$document['link']);
-                       }
-
-                       $receipt['message'][] = array('msg'=>lang('document %1 
has been saved',"'".$document['document_name']."'"));
-                       return $receipt;
-               }
-
-               function edit($document)
-               {
-                       while (is_array($document['location']) && 
list($input_name,$value) = each($document['location']))
-                       {
-                               $vals[] = "$input_name = '$value'";
-                       }
-
-                       while (is_array($document['extra']) && 
list($input_name,$value) = each($document['extra']))
-                       {
-                               $vals[] = "$input_name = '$value'";
-                       }
-
-                       if($vals)
-                       {
-                               $vals   = "," . implode(",",$vals);
-                       }
-
-                       if($document['street_name'])
-                       {
-                               $address[]= $document['street_name'];
-                               $address[]= $document['street_number'];
-                               $address        = 
$this->db->db_addslashes(implode(" ", $address));
-                       }
-
-                       if(!$address)
-                       {
-                               $address = 
$this->db->db_addslashes($document['location_name']);
-                       }
-
-                       $this->db->query("SELECT 
status,category,coordinator,document_name,loc1,link,p_num FROM fm_document 
where id='" .$document['document_id']."'",__LINE__,__FILE__);
-                       $this->db->next_record();
-
-                       $old_status = $this->db->f('status');
-                       $old_doc_type = $this->db->f('category');
-                       $old_coordinator = $this->db->f('coordinator');
-                       $old_document_name = $this->db->f('document_name');
-                       $old_loc1 = $this->db->f('loc1');
-                       $old_link = $this->db->f('link');
-                       $old_p_num = $this->db->f('p_num');
-
-                       if ($old_status != $document['status'])
-                       {
-                               
$this->historylog->add('S',$document['document_id'],$document['status']);
-                       }
-                       if ($old_doc_type != $document['doc_type'])
-                       {
-                               
$this->historylog->add('T',$document['document_id'],$document['doc_type']);
-                       }
-                       if ((int)$old_coordinator != 
(int)$document['coordinator'])
-                       {
-                               
$this->historylog->add('C',$document['document_id'],$document['coordinator']);
-                       }
-
-                       if($document['document_name_orig'] && 
!$document['document_name'] )
-                       {
-                               $document['document_name'] = 
$document['document_name_orig'];
-                       }
-
-                       if($old_link !=$document['link'] )
-                       {
-                                       
$this->historylog->add('L',$document['document_id'],$document['link']);
-                                       $alter_link=True;
-                       }
-
-//_debug_array($document);
-                       if ($old_document_name && ($old_document_name != 
$document['document_name']))
-                       {
-                               if($document['link'] && !$alter_link)
-                               {
-                                       
$this->historylog->add('L',$document['document_id'],$document['link']);
-                               }
-                               else
-                               {
-                                       
$this->historylog->add('F',$document['document_id'],$document['document_name']);
-                               }
-
-                               if($old_p_num)
-                               {
-                                       $file = $this->fakebase. SEP . 
'document' . SEP . $old_loc1 . SEP . $document['entity_name'] . SEP . 
$document['category_name'] . SEP . $p_num . SEP . $old_document_name;
-                               }
-                               else
-                               {
-                                       $file = $this->fakebase. SEP . 
'document' . SEP . $old_loc1 . SEP . $old_document_name;
-                               }
-
-                               $receipt= $this->delete_file($file);
-                       }
-
-                       if($document['link'])
-                       {
-                               unset($document['document_name']);
-                       }
-
-                       $document['descr'] = 
$this->db->db_addslashes($document['descr']);
-                       $document['name'] = 
$this->db->db_addslashes($document['name']);
-                       $document['title'] = 
$this->db->db_addslashes($document['title']);
-
-                       $value_set=array(
-                               'document_name' =>$document['document_name'],
-                               'link'                  =>$document['link'],
-                               'title'                 =>$document['title'],
-                               'branch_id'             
=>$document['branch_id'],
-                               'status'                =>$document['status'],
-                               'category'              =>$document['doc_type'],
-                               'document_date' =>$document['document_date'],
-                               'coordinator'   =>$document['coordinator'],
-                               'descr'                 =>$document['descr'],
-                               'version'               =>$document['version'],
-                               'location_code' =>$document['location_code'],
-                               'vendor_id'             
=>$document['vendor_id'],
-                               'address'               =>$address
-                               );
-
-                       $value_set      = 
$this->bocommon->validate_db_update($value_set);
-
-                       $this->db->query("UPDATE fm_document set $value_set 
$vals WHERE id= '" . $document['document_id'] ."'",__LINE__,__FILE__);
-
-                       $receipt['document_id'] = $document['document_id'];
-                       $receipt['message'][] = array('msg'=>lang('document %1 
has been edited',"'".$document['title']."'"));
-                       return $receipt;
-
-               }
-
-               function delete_file($file)
-               {
-                       if($this->vfs->file_exists(array(
-                                       'string' => $file,
-                                       'relatives' => Array(RELATIVE_NONE)
-                               )))
-                       {
-                               $this->vfs->override_acl = 1;
-
-                               if(!$this->vfs->rm (array(
-                                       'string' => $file,
-                                    'relatives' => array(
-                                         RELATIVE_NONE
-                                    )
-                               )))
-                               {
-                                       
$receipt['error'][]=array('msg'=>lang('failed to delete file') . ' :'. 
$this->fakebase. SEP . 'document'. SEP . $document_name);
-                               }
-                               else
-                               {
-                                       
$receipt['message'][]=array('msg'=>lang('file deleted') . ' :'. 
$this->fakebase. SEP . 'document'. SEP . $document_name);
-                               }
-                               $this->vfs->override_acl = 0;
-                       }
-
-               }
-
-               function delete($document_id )
-               {
-                       $this->db->query("SELECT 
document_name,location_code,p_num,p_entity_id,p_cat_id FROM fm_document where 
id='$document_id'",__LINE__,__FILE__);
-                       $this->db->next_record();
-                       $document_name  = $this->db->f('document_name');
-                       $location_code  = $this->db->f('location_code');
-                       $p_num                  = $this->db->f('p_num');
-                       $p_entity_id    = $this->db->f('p_entity_id');
-                       $p_cat_id               = $this->db->f('p_cat_id');
-
-                       $location               = split("-", $location_code);
-                       $loc1   = $location[0];
-                       if($p_cat_id)
-                       {
-                               $boadmin_entity         = 
CreateObject($this->currentapp.'.boadmin_entity');
-                               $entity = 
$boadmin_entity->read_single($p_entity_id,false);
-                               $category = 
$boadmin_entity->read_single_category($p_entity_id,$p_cat_id);
-                       }
-
-                       if($document_name)
-                       {
-                               if($p_num)
-                               {
-                                       $file = $this->fakebase. SEP . 
'document' . SEP . $loc1 . SEP . $entity['name'] . SEP . $category['name'] . 
SEP . $p_num . SEP . $document_name;
-                               }
-                               else
-                               {
-                                       $file = $this->fakebase. SEP . 
'document' . SEP . $loc1 . SEP . $document_name;
-                               }
-
-                               $receipt= $this->delete_file($file);
-                       }
-
-                       $this->db->query("DELETE FROM fm_document WHERE 
id='$document_id'",__LINE__,__FILE__);
-                       $this->db->query("DELETE FROM fm_document_history  
WHERE  history_record_id='$document_id'",__LINE__,__FILE__);
-               }
-       }
-?>
+                       $this->historylog       = 
CreateObject($this->currentapp.'.historylog','document');
+                       $this->vfs                      = 
CreateObject('phpgwapi.vfs');
+                       $this->rootdir          = $this->vfs->basedir;
+                       $this->fakebase         = $this->vfs->fakebase;
+                       $this->socommon         = 
CreateObject($this->currentapp.'.socommon');
+
+                       $this->join                     = $this->socommon->join;
+                       $this->like                     = $this->socommon->like;
+               }
+
+               function select_doc_type_list()
+               {
+                       $this->db->query("SELECT id, descr FROM 
fm_document_category  ORDER BY descr ");
+
+                       $i = 0;
+                       while ($this->db->next_record())
+                       {
+                               $categories[$i]['id']                           
= $this->db->f('id');
+                               $categories[$i]['name']                         
= stripslashes($this->db->f('descr'));
+                               $i++;
+                       }
+                       return $categories;
+               }
+
+               function read_single_category($id='')
+               {
+                       $this->db->query("SELECT descr FROM 
fm_document_category where id='$id' ");
+                       $this->db->next_record();
+                       return $this->db->f('descr');
+               }
+
+               function select_status_list()
+               {
+                       $this->db->query("SELECT id, descr FROM 
fm_document_status ORDER BY id ");
+
+                       $i = 0;
+                       while ($this->db->next_record())
+                       {
+                               $status_entries[$i]['id']                       
        = $this->db->f('id');
+                               $status_entries[$i]['name']                     
        = stripslashes($this->db->f('descr'));
+                               $i++;
+                       }
+                       return $status_entries;
+               }
+
+               function select_branch_list()
+               {
+                       $this->db->query("SELECT id, descr FROM fm_branch ORDER 
BY id ");
+
+                       $i = 0;
+                       while ($this->db->next_record())
+                       {
+                               $branch_entries[$i]['id']                       
        = $this->db->f('id');
+                               $branch_entries[$i]['name']                     
        = stripslashes($this->db->f('descr'));
+                               $i++;
+                       }
+                       return $branch_entries;
+               }
+
+               function read($data)
+               {
+                       if(is_array($data))
+                       {
+                               if ($data['start'])
+                               {
+                                       $start=$data['start'];
+                               }
+                               else
+                               {
+                                       $start=0;
+                               }
+                               $filter = 
(isset($data['filter'])?$data['filter']:'');
+                               $query = 
(isset($data['query'])?$data['query']:'');
+                               $sort = 
(isset($data['sort'])?$data['sort']:'DESC');
+                               $order = 
(isset($data['order'])?$data['order']:'');
+                               $cat_id = 
(isset($data['cat_id'])?$data['cat_id']:0);
+                               $entity_id = 
(isset($data['entity_id'])?$data['entity_id']:'');
+                               $doc_type = 
(isset($data['doc_type'])?$data['doc_type']:0);
+                       }
+
+                       $sql = $this->bocommon->fm_cache('sql_document_' . 
$entity_id);
+
+                       if(!$sql)
+                       {
+
+                               $document_table = 'fm_document';
+
+                               $cols .= $document_table . '.location_code';
+                               $cols_return[] = 'location_code';
+
+//                             $cols .= ",$document_table.id as document_id";
+//                             $cols_return[]                          = 
'document_id';
+
+                               if ($entity_id)
+                               {
+                                       $cols .= ",$document_table.p_num as 
p_num";
+                                       $cols_return[]                          
= 'p_num';
+                                       $uicols['input_type'][]         = 
'text';
+                                       $uicols['name'][]                       
= 'p_num';
+                                       $uicols['descr'][]                      
= lang('ID');
+                                       $uicols['statustext'][]         = 
lang('ID');
+
+                                       $cols .= ',fm_entity_category.name as 
category';
+                                       $cols_return[]                          
= 'category';
+                                       $uicols['input_type'][]         = 
'text';
+                                       $uicols['name'][]                       
= 'category';
+                                       $uicols['descr'][]                      
= lang('Type');
+                                       $uicols['statustext'][]         = 
lang('Type');
+
+                                       $cols .= ",$document_table.p_entity_id";
+                                       $cols_return[]                          
= 'p_entity_id';
+                                       $cols .= ",$document_table.p_cat_id";
+                                       $cols_return[]                          
= 'p_cat_id';
+
+                                       $joinmethod .= " $this->join  
fm_entity_category ON (fm_entity_category.entity_id 
=$document_table.p_entity_id AND fm_entity_category.id = 
$document_table.p_cat_id))";
+                                       $paranthesis .='(';
+
+                               }
+
+
+                               $sql    = 
$this->bocommon->generate_sql(array('entity_table'=>$document_table,'cols'=>$cols,'cols_return'=>$cols_return,
+                                                                               
                                
'uicols'=>$uicols,'joinmethod'=>$joinmethod,'paranthesis'=>$paranthesis,'query'=>$query));
+
+                               $this->bocommon->fm_cache('sql_document_' . 
$entity_id,$sql);
+
+                               $this->uicols           = 
$this->bocommon->uicols;
+                               $cols_return            = 
$this->bocommon->cols_return;
+                               $type_id                        = 
$this->bocommon->type_id;
+                               $this->cols_extra       = 
$this->bocommon->cols_extra;
+
+                               $this->bocommon->fm_cache('uicols_document_' . 
$entity_id,$this->uicols);
+                               
$this->bocommon->fm_cache('cols_return_document_' . $entity_id,$cols_return);
+                               $this->bocommon->fm_cache('type_id_document_' . 
$entity_id,$type_id);
+                               
$this->bocommon->fm_cache('cols_extra_document_' . 
$entity_id,$this->cols_extra);
+
+                       }
+                       else
+                       {
+                               $this->uicols           = 
$this->bocommon->fm_cache('uicols_document_' . $entity_id);
+                               $cols_return            = 
$this->bocommon->fm_cache('cols_return_document_' . $entity_id);
+                               $type_id                        = 
$this->bocommon->fm_cache('type_id_document_' . $entity_id);
+                               $this->cols_extra       = 
$this->bocommon->fm_cache('cols_extra_document_' . $entity_id);
+                       }
+
+                       $groupmethod= " GROUP BY 
fm_document.location_code,fm_document.address  ";
+
+                       if ($entity_id)
+                       {
+
+                               $groupmethod.= " 
,fm_document.p_entity_id,fm_entity_category.name,fm_document.p_num,fm_document.p_cat_id";
+                       }
+
+                       if ($order)
+                       {
+                               $ordermethod = " order by fm_document.$order 
$sort";
+                       }
+                       else
+                       {
+                               $ordermethod = ' order by 
fm_document.location_code ASC';
+                       }
+
+                       $where= 'WHERE';
+
+                       if(!$entity_id)
+                       {
+                               $filtermethod .= " $where ( fm_document.p_num 
is NULL OR fm_document.p_num='') ";
+                               $where= 'AND';
+                       }
+
+                       if ($cat_id)
+                       {
+                               $filtermethod .= " $where 
fm_document.p_cat_id=$cat_id ";
+                               $where= 'AND';
+                       }
+
+                       if ($doc_type > 0)
+                       {
+                               $filtermethod .= " $where 
fm_document.category='$doc_type' ";
+                               $where= 'AND';
+                       }
+
+                       if ($filter!='all' && $filter)
+                       {
+                               $filtermethod .= " $where 
fm_document.user_id='$filter' ";
+                       }
+
+                       if($query)
+                       {
+                               $query = ereg_replace("'",'',$query);
+                               $query = ereg_replace('"','',$query);
+
+                               $querymethod = " $where (fm_document.address 
$this->like '%$query%' or fm_document.location_code $this->like '%$query%')";
+                       }
+
+
+                       $sql .= " $filtermethod $querymethod $groupmethod";
+
+//echo $sql;
+
+                       $this->db2->query($sql,__LINE__,__FILE__);
+                       $this->total_records = $this->db2->num_rows();
+                       $this->db->limit_query($sql . 
$ordermethod,$start,__LINE__,__FILE__);
+
+                       $j=0;
+                       while ($this->db->next_record())
+                       {
+                               for ($i=0;$i<count($cols_return);$i++)
+                               {
+                                       $document_list[$j][$cols_return[$i]] = 
stripslashes($this->db->f($cols_return[$i]));
+                               }
+
+                               $location_code= $this->db->f('location_code');
+                               $location = split('-',$location_code);
+                               for ($m=0;$m<count($location);$m++)
+                               {
+                                       $document_list[$j]['loc' . ($m+1)] = 
$location[$m];
+                                       
$document_list[$j]['query_location']['loc' . ($m+1)]=implode("-", 
array_slice($location, 0, ($m+1)));
+                               }
+
+                               $j++;
+                       }
+
+
+//_debug_array($document_list);
+                       return $document_list;
+               }
+
+               function read_at_location($data)
+               {
+                       if(is_array($data))
+                       {
+                               if ($data['start'])
+                               {
+                                       $start=$data['start'];
+                               }
+                               else
+                               {
+                                       $start=0;
+                               }
+                               $filter = 
(isset($data['filter'])?$data['filter']:'');
+                               $query = 
(isset($data['query'])?$data['query']:'');
+                               $sort = 
(isset($data['sort'])?$data['sort']:'DESC');
+                               $order = 
(isset($data['order'])?$data['order']:'');
+                               $entity_id = 
(isset($data['entity_id'])?$data['entity_id']:0);
+                               $cat_id = 
(isset($data['cat_id'])?$data['cat_id']:0);
+                               $doc_type = 
(isset($data['doc_type'])?$data['doc_type']:0);
+                               $location_code = 
(isset($data['location_code'])?$data['location_code']:'');
+                       }
+
+                       if ($order)
+                       {
+                               $ordermethod = " order by $order $sort";
+                       }
+                       else
+                       {
+                               $ordermethod = ' order by location_code ASC';
+                       }
+
+                       $filtermethod = " fm_document.location_code $this->like 
'%$location_code%'";
+
+                       if ($doc_type > 0)
+                       {
+                               $filtermethod .= " AND 
fm_document.category='$doc_type' ";
+                       }
+                       if ($cat_id > 0)
+                       {
+                               $filtermethod .= " AND 
fm_document.p_cat_id=$cat_id AND fm_document.p_entity_id=$entity_id ";
+                       }
+
+
+                       if ($filter)
+                       {
+                               $filtermethod .= " AND 
fm_document.user_id='$filter' ";
+                       }
+
+                       if($query)
+                       {
+                               $query = ereg_replace("'",'',$query);
+                               $query = ereg_replace('"','',$query);
+
+                               $querymethod = " AND fm_document.title 
$this->like '%$query%' or fm_document.document_name $this->like '%$query%'";
+                       }
+
+                       $sql = "SELECT fm_document.*, 
fm_document_category.descr as category FROM fm_document $this->join 
fm_document_category on fm_document.category = fm_document_category.id WHERE  
$filtermethod $querymethod ";
+
+                       $this->db2->query($sql,__LINE__,__FILE__);
+                       $this->total_records = $this->db2->num_rows();
+                       $this->db->limit_query($sql . 
$ordermethod,$start,__LINE__,__FILE__);
+
+                       while ($this->db->next_record())
+                       {
+                               $document_list[] = array
+                               (
+                                       'document_id'           => 
$this->db->f('id'),
+                                       'document_name'         => 
$this->db->f('document_name'),
+                                       'link'                          => 
$this->db->f('link'),
+                                       'title'                         => 
$this->db->f('title'),
+                                       'doc_type'                      => 
$this->db->f('category'),
+                                       'user_id'                       => 
$this->db->f('user_id')
+                                       );
+                       }
+//_debug_array($document_list);
+                       return $document_list;
+               }
+
+               function read_single($document_id)
+               {
+                       $sql = "SELECT * from fm_document where 
id='$document_id'";
+
+                       $this->db->query($sql,__LINE__,__FILE__);
+
+                       if ($this->db->next_record())
+                       {
+                               $document['document_id']                = 
$this->db->f('id');
+                               $document['title']                              
= $this->db->f('title');
+                               $document['document_name']              = 
$this->db->f('document_name');
+                               $document['link']                               
= $this->db->f('link');
+                               $document['location_code']              = 
$this->db->f('location_code');
+                               $document['branch_id']                  = 
$this->db->f('branch_id');
+                               $document['version']                    = 
$this->db->f('version');
+                               $document['vendor_id']                  = 
$this->db->f('vendor_id');
+                               $document['floor_id']                   = 
$this->db->f('floor_id');
+                               $document['descr']                              
= $this->db->f('descr');
+                               $document['status']                             
= $this->db->f('status');
+                               $document['user_id']                    = 
$this->db->f('user_id');
+                               $document['coordinator']                = 
$this->db->f('coordinator');
+                               $document['access']                             
= $this->db->f('access');
+                               $document['document_date']              = 
$this->db->f('document_date');
+                               $document['doc_type']                   = 
$this->db->f('category');
+                               $document['p_num']                              
= $this->db->f('p_num');
+                               $document['p_entity_id']                = 
$this->db->f('p_entity_id');
+                               $document['p_cat_id']                   = 
$this->db->f('p_cat_id');
+                       }
+
+//_debug_array($document);
+                               return $document;
+               }
+
+               function add($document)
+               {
+                       while (is_array($document['location']) && 
list($input_name,$value) = each($document['location']))
+                       {
+                               if($value)
+                               {
+                                       $cols[] = $input_name;
+                                       $vals[] = $value;
+                               }
+                       }
+
+                       while (is_array($document['extra']) && 
list($input_name,$value) = each($document['extra']))
+                       {
+                               if($value)
+                               {
+                                       $cols[] = $input_name;
+                                       $vals[] = $value;
+                               }
+                       }
+
+                       if($cols)
+                       {
+                               $cols   = "," . implode(",", $cols);
+                               $vals   = ",'" . implode("','", $vals) . "'";
+                       }
+
+                       if($document['street_name'])
+                       {
+                               $address[]= $document['street_name'];
+                               $address[]= $document['street_number'];
+                               $address        = 
$this->db->db_addslashes(implode(" ", $address));
+                       }
+
+                       if(!$address)
+                       {
+                               $address = 
$this->db->db_addslashes($document['location_name']);
+                       }
+
+                       $document['descr'] = 
$this->db->db_addslashes($document['descr']);
+                       $document['name'] = 
$this->db->db_addslashes($document['name']);
+                       $document['title'] = 
$this->db->db_addslashes($document['title']);
+//_debug_array($document);
+
+                       $values= array(
+                               $document['document_name'],
+                               $document['link'],
+                               $document['title'],
+                               'public',
+                               $document['doc_type'],
+                               time(),
+                               $document['document_date'],
+                               $document['version'],
+                               $document['coordinator'],
+                               $document['status'],
+                               $document['descr'],
+                               $document['location_code'],
+                               $address,
+                               $document['branch_id'],
+                               $document['vendor_id'],
+                               $this->account);
+
+                       $values = $this->bocommon->validate_db_insert($values);
+
+                       $this->db->query("INSERT INTO fm_document 
(document_name,link,title,access,category,entry_date,document_date,version,coordinator,status,"
+                               . 
"descr,location_code,address,branch_id,vendor_id,user_id $cols) "
+                               . "VALUES ($values $vals )",__LINE__,__FILE__);
+
+                       $receipt['document_id'] = 
$this->db->get_last_insert_id('fm_document','id');
+
+                       
$this->historylog->add('SO',$receipt['document_id'],$document['status']);
+                       
$this->historylog->add('TO',$receipt['document_id'],$document['doc_type']);
+                       if($document['coordinator'])
+                       {
+                               
$this->historylog->add('CO',$receipt['document_id'],$document['coordinator']);
+                       }
+                       if($document['document_name'])
+                       {
+                               
$this->historylog->add('FO',$receipt['document_id'],$document['coodocument_name']);
+                       }
+                       if($document['link'])
+                       {
+                               
$this->historylog->add('LO',$receipt['document_id'],$document['link']);
+                       }
+
+                       $receipt['message'][] = array('msg'=>lang('document %1 
has been saved',"'".$document['document_name']."'"));
+                       return $receipt;
+               }
+
+               function edit($document)
+               {
+                       while (is_array($document['location']) && 
list($input_name,$value) = each($document['location']))
+                       {
+                               $vals[] = "$input_name = '$value'";
+                       }
+
+                       while (is_array($document['extra']) && 
list($input_name,$value) = each($document['extra']))
+                       {
+                               $vals[] = "$input_name = '$value'";
+                       }
+
+                       if($vals)
+                       {
+                               $vals   = "," . implode(",",$vals);
+                       }
+
+                       if($document['street_name'])
+                       {
+                               $address[]= $document['street_name'];
+                               $address[]= $document['street_number'];
+                               $address        = 
$this->db->db_addslashes(implode(" ", $address));
+                       }
+
+                       if(!$address)
+                       {
+                               $address = 
$this->db->db_addslashes($document['location_name']);
+                       }
+
+                       $this->db->query("SELECT 
status,category,coordinator,document_name,loc1,link,p_num FROM fm_document 
where id='" .$document['document_id']."'",__LINE__,__FILE__);
+                       $this->db->next_record();
+
+                       $old_status = $this->db->f('status');
+                       $old_doc_type = $this->db->f('category');
+                       $old_coordinator = $this->db->f('coordinator');
+                       $old_document_name = $this->db->f('document_name');
+                       $old_loc1 = $this->db->f('loc1');
+                       $old_link = $this->db->f('link');
+                       $old_p_num = $this->db->f('p_num');
+
+                       if ($old_status != $document['status'])
+                       {
+                               
$this->historylog->add('S',$document['document_id'],$document['status']);
+                       }
+                       if ($old_doc_type != $document['doc_type'])
+                       {
+                               
$this->historylog->add('T',$document['document_id'],$document['doc_type']);
+                       }
+                       if ((int)$old_coordinator != 
(int)$document['coordinator'])
+                       {
+                               
$this->historylog->add('C',$document['document_id'],$document['coordinator']);
+                       }
+
+                       if($document['document_name_orig'] && 
!$document['document_name'] )
+                       {
+                               $document['document_name'] = 
$document['document_name_orig'];
+                       }
+
+                       if($old_link !=$document['link'] )
+                       {
+                                       
$this->historylog->add('L',$document['document_id'],$document['link']);
+                                       $alter_link=True;
+                       }
+
+//_debug_array($document);
+                       if ($old_document_name && ($old_document_name != 
$document['document_name']))
+                       {
+                               if($document['link'] && !$alter_link)
+                               {
+                                       
$this->historylog->add('L',$document['document_id'],$document['link']);
+                               }
+                               else
+                               {
+                                       
$this->historylog->add('F',$document['document_id'],$document['document_name']);
+                               }
+
+                               if($old_p_num)
+                               {
+                                       $file = $this->fakebase. SEP . 
'document' . SEP . $old_loc1 . SEP . $document['entity_name'] . SEP . 
$document['category_name'] . SEP . $p_num . SEP . $old_document_name;
+                               }
+                               else
+                               {
+                                       $file = $this->fakebase. SEP . 
'document' . SEP . $old_loc1 . SEP . $old_document_name;
+                               }
+
+                               $receipt= $this->delete_file($file);
+                       }
+
+                       if($document['link'])
+                       {
+                               unset($document['document_name']);
+                       }
+
+                       $document['descr'] = 
$this->db->db_addslashes($document['descr']);
+                       $document['name'] = 
$this->db->db_addslashes($document['name']);
+                       $document['title'] = 
$this->db->db_addslashes($document['title']);
+
+                       $value_set=array(
+                               'document_name' =>$document['document_name'],
+                               'link'                  =>$document['link'],
+                               'title'                 =>$document['title'],
+                               'branch_id'             
=>$document['branch_id'],
+                               'status'                =>$document['status'],
+                               'category'              =>$document['doc_type'],
+                               'document_date' =>$document['document_date'],
+                               'coordinator'   =>$document['coordinator'],
+                               'descr'                 =>$document['descr'],
+                               'version'               =>$document['version'],
+                               'location_code' =>$document['location_code'],
+                               'vendor_id'             
=>$document['vendor_id'],
+                               'address'               =>$address
+                               );
+
+                       $value_set      = 
$this->bocommon->validate_db_update($value_set);
+
+                       $this->db->query("UPDATE fm_document set $value_set 
$vals WHERE id= '" . $document['document_id'] ."'",__LINE__,__FILE__);
+
+                       $receipt['document_id'] = $document['document_id'];
+                       $receipt['message'][] = array('msg'=>lang('document %1 
has been edited',"'".$document['title']."'"));
+                       return $receipt;
+
+               }
+
+               function delete_file($file)
+               {
+                       if($this->vfs->file_exists(array(
+                                       'string' => $file,
+                                       'relatives' => Array(RELATIVE_NONE)
+                               )))
+                       {
+                               $this->vfs->override_acl = 1;
+
+                               if(!$this->vfs->rm (array(
+                                       'string' => $file,
+                                    'relatives' => array(
+                                         RELATIVE_NONE
+                                    )
+                               )))
+                               {
+                                       
$receipt['error'][]=array('msg'=>lang('failed to delete file') . ' :'. 
$this->fakebase. SEP . 'document'. SEP . $document_name);
+                               }
+                               else
+                               {
+                                       
$receipt['message'][]=array('msg'=>lang('file deleted') . ' :'. 
$this->fakebase. SEP . 'document'. SEP . $document_name);
+                               }
+                               $this->vfs->override_acl = 0;
+                       }
+
+               }
+
+               function delete($document_id )
+               {
+                       $this->db->query("SELECT 
document_name,location_code,p_num,p_entity_id,p_cat_id FROM fm_document where 
id='$document_id'",__LINE__,__FILE__);
+                       $this->db->next_record();
+                       $document_name  = $this->db->f('document_name');
+                       $location_code  = $this->db->f('location_code');
+                       $p_num                  = $this->db->f('p_num');
+                       $p_entity_id    = $this->db->f('p_entity_id');
+                       $p_cat_id               = $this->db->f('p_cat_id');
+
+                       $location               = split("-", $location_code);
+                       $loc1   = $location[0];
+                       if($p_cat_id)
+                       {
+                               $boadmin_entity         = 
CreateObject($this->currentapp.'.boadmin_entity');
+                               $entity = 
$boadmin_entity->read_single($p_entity_id,false);
+                               $category = 
$boadmin_entity->read_single_category($p_entity_id,$p_cat_id);
+                       }
+
+                       if($document_name)
+                       {
+                               if($p_num)
+                               {
+                                       $file = $this->fakebase. SEP . 
'document' . SEP . $loc1 . SEP . $entity['name'] . SEP . $category['name'] . 
SEP . $p_num . SEP . $document_name;
+                               }
+                               else
+                               {
+                                       $file = $this->fakebase. SEP . 
'document' . SEP . $loc1 . SEP . $document_name;
+                               }
+
+                               $receipt= $this->delete_file($file);
+                       }
+
+                       $this->db->query("DELETE FROM fm_document WHERE 
id='$document_id'",__LINE__,__FILE__);
+                       $this->db->query("DELETE FROM fm_document_history  
WHERE  history_record_id='$document_id'",__LINE__,__FILE__);
+               }
+       }
+?>
Index: property/inc/class.soentity.inc.php
diff -u property/inc/class.soentity.inc.php:1.11 
property/inc/class.soentity.inc.php:1.12
--- property/inc/class.soentity.inc.php:1.11    Fri Jan 27 14:05:43 2006
+++ property/inc/class.soentity.inc.php Mon Jan 30 22:14:19 2006
@@ -1,647 +1,648 @@
-<?php
-       /**
-       * phpGroupWare - property: a Facilities Management System.
-       *
-       * @author Sigurd Nes <address@hidden>
-       * @copyright Copyright (C) 2003-2005 Free Software Foundation, Inc. 
http://www.fsf.org/
-       * @license http://www.gnu.org/licenses/gpl.html GNU General Public 
License
-       * @internal Development of this application was funded by 
http://www.bergen.kommune.no/bbb_/ekstern/
-       * @package property
-       * @subpackage entity
-       * @version $Id: class.soentity.inc.php,v 1.11 2006/01/27 14:05:43 
sigurdne Exp $
-       */
-
-       /**
-        * Description
-        * @package property
-        */
-
-       class soentity
-       {
-               function soentity()
-               {
-                       $this->currentapp       = 
$GLOBALS['phpgw_info']['flags']['currentapp'];
-                       $this->account          = 
$GLOBALS['phpgw_info']['user']['account_id'];
-                       $this->bocommon         = 
CreateObject($this->currentapp.'.bocommon');
+<?php
+       /**
+       * phpGroupWare - property: a Facilities Management System.
+       *
+       * @author Sigurd Nes <address@hidden>
+       * @copyright Copyright (C) 2003-2005 Free Software Foundation, Inc. 
http://www.fsf.org/
+       * @license http://www.gnu.org/licenses/gpl.html GNU General Public 
License
+       * @internal Development of this application was funded by 
http://www.bergen.kommune.no/bbb_/ekstern/
+       * @package property
+       * @subpackage entity
+       * @version $Id: class.soentity.inc.php,v 1.12 2006/01/30 22:14:19 
sigurdne Exp $
+       */
+
+       /**
+        * Description
+        * @package property
+        */
+
+       class soentity
+       {
+               function soentity()
+               {
+                       $this->currentapp       = 
$GLOBALS['phpgw_info']['flags']['currentapp'];
+                       $this->account          = 
$GLOBALS['phpgw_info']['user']['account_id'];
+                       $this->bocommon         = 
CreateObject($this->currentapp.'.bocommon');
                        $this->db               = $this->bocommon->new_db();
-                       $this->db2              = $this->bocommon->new_db();
-                       $this->join                     = $this->bocommon->join;
-                       $this->left_join        = $this->bocommon->left_join;
-                       $this->like                     = $this->bocommon->like;
-               }
-
-               function select_status_list($entity_id,$cat_id)
-               {
-                       if(!$entity_id || !$cat_id)
-                       {
-                               return;
-                       }
-
-                       $sql= "SELECT fm_entity_choice.id, 
fm_entity_choice.value FROM fm_entity_attribute $this->join fm_entity_choice ON 
"
-                       . " fm_entity_attribute.entity_id= 
fm_entity_choice.entity_id AND "
-                       . " fm_entity_attribute.cat_id= fm_entity_choice.cat_id 
AND "
-                       . " fm_entity_attribute.id= fm_entity_choice.attrib_id "
-                       . " WHERE fm_entity_attribute.column_name='status' "
-                       . " AND fm_entity_choice.entity_id=$entity_id "
-                       . " AND fm_entity_choice.cat_id=$cat_id ORDER BY 
fm_entity_choice.id";
-
-
-                       $this->db->query($sql,__LINE__,__FILE__);
-
-                       $i = 0;
-                       while ($this->db->next_record())
-                       {
-                               $status[$i]['id']                               
= $this->db->f('id');
-                               $status[$i]['name']                             
= stripslashes($this->db->f('value'));
-                               $i++;
-                       }
-                       return $status;
-               }
-
-
-               function read($data)
-               {
-                       if(is_array($data))
-                       {
-                               if ($data['start'])
-                               {
-                                       $start=$data['start'];
-                               }
-                               else
-                               {
-                                       $start=0;
-                               }
-                               $filter = $data['filter']?$data['filter']:'all';
-                               $query = 
(isset($data['query'])?$data['query']:'');
-                               $sort = 
(isset($data['sort'])?$data['sort']:'DESC');
-                               $order = 
(isset($data['order'])?$data['order']:'');
-                               $cat_id = 
(isset($data['cat_id'])?$data['cat_id']:0);
-                               $district_id = 
(isset($data['district_id'])?$data['district_id']:0);
-                               $lookup = 
(isset($data['lookup'])?$data['lookup']:'');
-                               $allrows = 
(isset($data['allrows'])?$data['allrows']:'');
-                               $entity_id = 
(isset($data['entity_id'])?$data['entity_id']:'');
-                               $cat_id = 
(isset($data['cat_id'])?$data['cat_id']:'');
-                               $status = 
(isset($data['status'])?$data['status']:'');
-                               $start_date = 
(isset($data['start_date'])?$data['start_date']:'');
-                               $end_date = 
(isset($data['end_date'])?$data['end_date']:'');
-                       }
-
-                       if(!$entity_id || !$cat_id)
-                       {
-                               return;
-                       }
-
-                       $grants         = 
$GLOBALS['phpgw']->session->appsession('grants_entity_'.$entity_id.'_'.$cat_id,$this->currentapp);
-
-                       if(!$grants)
-                       {
-                               $this->acl2             = 
CreateObject($this->currentapp.'.acl2','##DEFAULT##','##DEFAULT##');
-                               $grants         = 
$this->acl2->get_grants($this->currentapp,'.entity.' . $entity_id . '.' . 
$cat_id);
-                               
$GLOBALS['phpgw']->session->appsession('grants_entity_'.$entity_id.'_'.$cat_id,$this->currentapp,$grants);
-                       }
-
-
-                       $sql = $this->bocommon->fm_cache('sql_entity_' . 
$entity_id . '_' . $cat_id . '_' . $lookup);
-
-                       $entity_table = 'fm_entity_' . $entity_id . '_' . 
$cat_id;
-                       if(!$sql)
-                       {
-                               $cols = $entity_table . '.*';
-                               $cols_return[] = 'location_code';
-
-                               $cols_return[]                          = 'num';
-                               $uicols['input_type'][]         = 'text';
-                               $uicols['name'][]                       = 'num';
-                               $uicols['descr'][]                      = 
lang('ID');
-                               $uicols['statustext'][]         = lang('ID');
-
-                               $cols_return[]                          = 'id';
-                               $uicols['input_type'][]         = 'hidden';
-                               $uicols['name'][]                       = 'id';
-                               $uicols['descr'][]                      = false;
-                               $uicols['statustext'][]         = false;
-                               if($lookup)
-                               {
-                                       $cols .= ',num as entity_num_' . 
$entity_id;
-                                       $cols_return[] = 'entity_num_' . 
$entity_id;
-                               }
-
-
-                               $admin_entity   = 
CreateObject($this->currentapp.'.soadmin_entity');
-                               $category = 
$admin_entity->read_single_category($entity_id,$cat_id);
-
-                               $sql = 
$this->bocommon->generate_sql(array('entity_table'=>$entity_table,'cols_return'=>$cols_return,'cols'=>$cols,
-                                                               
'uicols'=>$uicols,'joinmethod'=>$joinmethod,'paranthesis'=>$paranthesis,'query'=>$query,'lookup'=>$lookup,'location_level'=>$category['location_level']));
-
-                               $this->bocommon->fm_cache('sql_entity_' . 
$entity_id . '_' . $cat_id . '_' . $lookup,$sql);
-                               $this->bocommon->fm_cache('uicols_entityt_' . 
$entity_id . '_' . $cat_id . '_' . $lookup,$this->bocommon->uicols);
-                               
$this->bocommon->fm_cache('cols_return_entityt_' . $entity_id . '_' . $cat_id . 
'_' . $lookup,$this->bocommon->cols_return);
-                               
$this->bocommon->fm_cache('cols_return_lookup_entityt_' . $entity_id . '_' . 
$cat_id . '_' . $lookup,$this->bocommon->cols_return_lookup);
-                               $this->bocommon->fm_cache('cols_extra_entityt_' 
. $entity_id . '_' . $cat_id . '_' . $lookup,$this->bocommon->cols_extra);
-
-                               $uicols                                         
= $this->bocommon->uicols;
-                               $cols_return                            = 
$this->bocommon->cols_return;
-                               $this->cols_return_lookup       = 
$this->bocommon->cols_return_lookup;
-                               $this->cols_extra                       = 
$this->bocommon->cols_extra;
-                       }
-                       else
-                       {
-                               $uicols                                         
        = $this->bocommon->fm_cache('uicols_entityt_' . $entity_id . '_' . 
$cat_id . '_' . $lookup);
-                               $cols_return                                    
= $this->bocommon->fm_cache('cols_return_entityt_' . $entity_id . '_' . $cat_id 
. '_' . $lookup);
-                               $this->cols_return_lookup               = 
$this->bocommon->fm_cache('cols_return_lookup_entityt_' . $entity_id . '_' . 
$cat_id . '_' . $lookup);
-                               $this->cols_extra                               
= $this->bocommon->fm_cache('cols_extra_entityt_' . $entity_id . '_' . $cat_id 
. '_' . $lookup);
-                               if($lookup)
-                               {
-                                       $admin_entity   = 
CreateObject($this->currentapp.'.soadmin_entity');
-                                       $category = 
$admin_entity->read_single_category($entity_id,$cat_id);
-                               }
-
-                       }
-
-                       if ($cat_id > 0)
-                       {
-                               
$user_columns=$GLOBALS['phpgw_info']['user']['preferences'][$this->currentapp]['entity_columns_'.$entity_id.'_'.$cat_id];
-//_debug_array($user_columns);
-
-                               if (isset($user_columns) AND 
is_array($user_columns) AND $user_columns[0])
-                               {
-                                       $i      = count($uicols['name']);
-                                       foreach($user_columns as $column_id)
-                                       {
-                                               $this->db->query("SELECT * FROM 
fm_entity_attribute WHERE entity_id= $entity_id AND cat_id= $cat_id AND id= 
$column_id");
-
-                                               $this->db->next_record();
-                                               $uicols['input_type'][]         
= 'text';
-                                               $uicols['name'][]               
        = $this->db->f('column_name');
-                                               $uicols['descr'][]              
        = $this->db->f('input_text');
-                                               $uicols['statustext'][]         
= $this->db->f('statustext');
-                                               $uicols['datatype'][$i]         
= $this->db->f('datatype');
-                                               $cols_return_extra[]= array(
-                                                       'name'  => 
$this->db->f('column_name'),
-                                                       'datatype'      => 
$this->db->f('datatype'),
-                                                       'attrib_id'     => 
$this->db->f('id')
-                                               );
-                                               $i++;
-                                       }
-                               }
-                       }
-                       else
-                       {
-                               return;
-                       }
-
-                       $this->uicols   = $uicols;
-
-//_debug_array($cols_return_extra);
-                       if ($order)
-                       {
-                               $ordermethod = " order by $entity_table.$order 
$sort";
-                       }
-                       else
-                       {
-                               $ordermethod = " order by $entity_table.id 
DESC";
-                       }
-
-                       $where= 'WHERE';
-
-                       if ($filter=='all')
-                       {
-                               if (is_array($grants))
-                               {
-                                       while (list($user) = each($grants))
-                                       {
-                                               $public_user_list[] = $user;
-                                       }
-                                       reset($public_user_list);
-                                       $filtermethod .= " $where ( 
$entity_table.user_id IN(" . implode(',',$public_user_list) . "))";
-
-                                       $where= 'AND';
-                               }
-
-                       }
-                       else
-                       {
-                               $filtermethod = " $where 
$entity_table.user_id=$filter ";
-                               $where= 'AND';
-                       }
-
-
-                       if ($status)
-                       {
-                               $filtermethod .= " $where 
$entity_table.status='$status' ";
-                               $where= 'AND';
-                       }
-
-                       if ($district_id > 0)
-                       {
-                               $filtermethod .= " $where  
district_id='$district_id' ";
-                               $where = 'AND';
-                       }
-
-                       if ($start_date)
-                       {
-                               $filtermethod .= " $where 
$entity_table.entry_date >= $start_date AND $entity_table.entry_date <= 
$end_date ";
-                               $where= 'AND';
-                       }
-
-                       if($query)
-                       {
-                               $query = ereg_replace("'",'',$query);
-                               $query = ereg_replace('"','',$query);
-
-                               $filtermethod .= " $where ( 
$entity_table.location_code $this->like '%$query%' OR $entity_table.num 
$this->like '%$query%'  OR loc1_name $this->like '%$query%' OR address 
$this->like '%$query%')";
-                               $where= 'OR';
-
-                               $this->db->query("SELECT * FROM 
fm_entity_attribute where search='1' AND entity_id= $entity_id AND cat_id = 
$cat_id");
-
-                               while ($this->db->next_record())
-                               {
-                                       if($this->db->f('datatype')=='V' || 
$this->db->f('datatype')=='email' || $this->db->f('datatype')=='CH')
-                                       {
-                                               $querymethod[]= 
"$entity_table." . $this->db->f('column_name') . " $this->like '%$query%'";
-                                       }
-                                       else
-                                       {
-                                               $querymethod[]= 
"$entity_table." . $this->db->f('column_name') . " = '$query'";
-                                       }
-                               }
-
-                               if (isset($querymethod) AND 
is_array($querymethod))
-                               {
-                                       $querymethod = " $where (" . implode (' 
OR ',$querymethod) . ')';
-                                       $where = 'AND';
-                               }
-
-                       }
-
-                       $sql .= " $filtermethod $querymethod";
-
-//echo $sql;
-
-                       $this->db2->query($sql,__LINE__,__FILE__);
-                       $this->total_records = $this->db2->num_rows();
-                       if(!$allrows)
-                       {
-                               $this->db->limit_query($sql . 
$ordermethod,$start,__LINE__,__FILE__);
-                       }
-                       else
-                       {
-                               $this->db->query($sql . 
$ordermethod,__LINE__,__FILE__);
-                       }
-
-                       $j=0;
-                       $n=count($cols_return);
-//_debug_array($cols_return);
-                       $contacts                       = 
CreateObject('phpgwapi.contacts');
-
-                       while ($this->db->next_record())
-                       {
-                               for ($i=0;$i<$n;$i++)
-                               {
-                                       $entity_list[$j][$cols_return[$i]] = 
$this->db->f($cols_return[$i]);
-                                       $entity_list[$j]['grants'] = 
(int)$grants[$this->db->f('user_id')];
-                                       if($lookup)
-                                       {
-                                               
$entity_list[$j]['entity_cat_name_' . $entity_id] = $category['name'];
-                                               $entity_list[$j]['entity_id_' . 
$entity_id] = $entity_id;
-                                               $entity_list[$j]['cat_id_' . 
$entity_id] = $cat_id;
-                                       }
-                               }
-
-                               for ($i=0;$i<count($cols_return_extra);$i++)
-                               {
-                                       $value='';
-                                       
$value=$this->db->f($cols_return_extra[$i]['name']);
-
-                                       
if(($cols_return_extra[$i]['datatype']=='R' || 
$cols_return_extra[$i]['datatype']=='LB') && $value):
-                                       {
-                                               $sql="SELECT value FROM 
fm_entity_choice where entity_id=$entity_id AND cat_id=$cat_id AND attrib_id=" 
.$cols_return_extra[$i]['attrib_id']. "  AND id=" . $value;
-                                               $this->db2->query($sql);
-                                               $this->db2->next_record();
-                                               
$entity_list[$j][$cols_return_extra[$i]['name']] = $this->db2->f('value');
-                                       }
-                                       
elseif($cols_return_extra[$i]['datatype']=='AB' && $value):
-                                       {
-                                               $contact_data   = 
$contacts->read_single_entry($value,array('n_given'=>'n_given','n_family'=>'n_family','email'=>'email'));
-                                               
$entity_list[$j][$cols_return_extra[$i]['name']]        = 
$contact_data[0]['n_family'] . ', ' . $contact_data[0]['n_given'];
-                                       }
-                                       
elseif($cols_return_extra[$i]['datatype']=='VENDOR' && $value):
-                                       {
-                                               $sql="SELECT org_name FROM 
fm_vendor where id=$value";
-                                               $this->db2->query($sql);
-                                               $this->db2->next_record();
-                                               
$entity_list[$j][$cols_return_extra[$i]['name']] = $this->db2->f('org_name');
-                                       }
-                                       
elseif($cols_return_extra[$i]['datatype']=='CH' && $value):
-                                       {
-                                               $ch= unserialize($value);
-
-                                               if (isset($ch) AND 
is_array($ch))
-                                               {
-                                                       for 
($k=0;$k<count($ch);$k++)
-                                                       {
-                                                               $sql="SELECT 
value FROM fm_entity_choice where entity_id=$entity_id AND cat_id=$cat_id AND 
attrib_id=" .$cols_return_extra[$i]['attrib_id']. "  AND id=" . $ch[$k];
-                                                               
$this->db2->query($sql);
-                                                               while 
($this->db2->next_record())
-                                                               {
-                                                                       
$ch_value[]=$this->db2->f('value');
-                                                               }
-                                                       }
-                                                       
$entity_list[$j][$cols_return_extra[$i]['name']] = @implode(",", $ch_value);
-                                                       unset($ch_value);
-                                               }
-                                       }
-                                       
elseif($cols_return_extra[$i]['datatype']=='D' && $value):
-                                       {
-//_debug_array($value);
-
-                                               
$entity_list[$j][$cols_return_extra[$i]['name']]=date($GLOBALS['phpgw_info']['user']['preferences']['common']['dateformat'],strtotime($value));
-                                       }
-                                       else:
-                                       {
-                                               
$entity_list[$j][$cols_return_extra[$i]['name']]=$value;
-                                       }
-                                       endif;
-                               }
-
-                               $location_code= $this->db->f('location_code');
-                               $location = split('-',$location_code);
-                               for ($m=0;$m<count($location);$m++)
-                               {
-                                       $entity_list[$j]['loc' . ($m+1)] = 
$location[$m];
-                                       
$entity_list[$j]['query_location']['loc' . ($m+1)]=implode("-", 
array_slice($location, 0, ($m+1)));
-                               }
-
-                               $j++;
-                       }
-//_debug_array($entity_list);
-                       return $entity_list;
-               }
-
-               function read_single($data)
-               {
-                       $entity_id =$data['entity_id'];
-                       $cat_id =$data['cat_id'];
-                       $id =$data['id'];
-
-                       $this->db->query("SELECT * FROM fm_entity_attribute 
WHERE entity_id =$entity_id AND cat_id =$cat_id ORDER BY attrib_sort");
-
-                       while ($this->db->next_record())
-                       {
-                               $entity['attributes'][] = array
-                               (
-                                       'attrib_id'             => 
$this->db->f('id'),
-                                       'name'                  => 
$this->db->f('column_name'),
-                                       'input_text'    => 
stripslashes($this->db->f('input_text')),
-                                       'statustext'    => 
stripslashes($this->db->f('statustext')),
-//                                     'statustext'    => str_replace("\n"," 
",stripslashes($this->db->f('statustext'))),
-                                       'datatype'              => 
$this->db->f('datatype')
-                               );
-                       }
-//_debug_array($entity);
-
-                       if($id)
-                       {
-                               $table='fm_entity_' . $entity_id .'_' . $cat_id;
-
-                               $this->db->query("SELECT * FROM $table WHERE id 
=$id");
-
-                               if($this->db->next_record())
-                               {
-                                       $entity['id']                           
= $id;
-                                       $entity['num']                          
= $this->db->f('num');
-                                       $entity['p_num']                        
= $this->db->f('p_num');
-                                       $entity['p_entity_id']          = 
$this->db->f('p_entity_id');
-                                       $entity['p_cat_id']                     
= $this->db->f('p_cat_id');
-                                       $entity['location_code']        = 
$this->db->f('location_code');
-                                       $entity['tenant_id']            = 
$this->db->f('tenant_id');
-                                       $entity['contact_phone']        = 
$this->db->f('contact_phone');
-                                       $entity['status']                       
= $this->db->f('status');
-
-                                       for 
($i=0;$i<count($entity['attributes']);$i++)
-                                       {
-                                               
$entity['attributes'][$i]['value']      = 
$this->db->f($entity['attributes'][$i]['name']);
-                                               
$entity['attributes'][$i]['datatype_text']      = 
$this->bocommon->translate_datatype($entity['attributes'][$i]['datatype']);
-                                       }
-                               }
-
-                               $sql = "SELECT * FROM fm_origin WHERE 
destination ='entity_" . $entity_id . '_' . $cat_id . "' AND destination_id = 
$id";
-                               $this->db->query($sql,__LINE__,__FILE__);
-
-                               $i=-1;
-                               while ($this->db->next_record())
-                               {
-                                       if($last_type != $this->db->f('origin'))
-                                       {
-                                               $i++;
-                                       }
-                                       $entity['origin'][$i]['type'] = 
$this->db->f('origin');
-                                       $entity['origin'][$i]['link'] = 
$this->bocommon->get_origin_link($this->db->f('origin'));
-                                       $entity['origin'][$i]['data'][]= array(
-                                               'id'=> 
$this->db->f('origin_id'),
-                                               'type'=> $this->db->f('origin')
-                                               );
-
-                                       $last_type=$this->db->f('origin');
-                               }
-                       }
-
-                       return  $entity;
-               }
-
-
-               function check_entity($entity_id,$cat_id,$num)
-               {
-                       $table='fm_entity_' . $entity_id .'_' . $cat_id;
-                       $this->db->query("SELECT count(*) FROM $table where 
num='$num'");
-
-                       $this->db->next_record();
-
-                       if ( $this->db->f(0))
-                       {
-                               return True;
-                       }
-               }
-
-               function generate_id($data)
-               {
-                       $table='fm_entity_' . $data['entity_id'] .'_' . 
$data['cat_id'];
-                       $this->db->query("select max(id) as id from $table");
-                       $this->db->next_record();
-                       $id = $this->db->f('id')+1;
-
-                       return $id;
-               }
-
-               function generate_num($entity_id,$cat_id,$id)
-               {
-                       $this->db->query("select prefix from fm_entity_category 
WHERE entity_id=$entity_id AND id=$cat_id ");
-                       $this->db->next_record();
-                       $prefix = $this->db->f('prefix');
-
-                       if (strlen($id) == 4)
-                               $return = $id;
-                       if (strlen($id) == 3)
-                               $return = "0$id";
-                       if (strlen($id) == 2)
-                               $return = "00$id";
-                       if (strlen($id) == 1)
-                               $return = "000$id";
-                       if (strlen($id) == 0)
-                               $return = "0001";
-
-                       return $prefix . strtoupper($return);
-               }
-
-
-               function add($values,$values_attribute,$entity_id,$cat_id)
-               {
-                       if($values['street_name'])
-                       {
-                               $address[]= $values['street_name'];
-                               $address[]= $values['street_number'];
-                               $address = $this->db->db_addslashes(implode(" 
", $address));
-                       }
-
-                       if(!$address)
-                       {
-                               $address = 
$this->db->db_addslashes($values['location_name']);
-                       }
-
-                       $values['descr'] = 
$this->db->db_addslashes($values['descr']);
-
-                       while (is_array($values['location']) && 
list($input_name,$value) = each($values['location']))
-                       {
-                               if($value)
-                               {
-                                       $cols[] = $input_name;
-                                       $vals[] = $value;
-                               }
-                       }
-//_debug_array($values);
-                       while (is_array($values['extra']) && 
list($input_name,$value) = each($values['extra']))
-                       {
-                               if($value)
-                               {
-                                       $cols[] = $input_name;
-                                       $vals[] = $value;
-                               }
-                       }
-
-                       if (isset($values_attribute) AND 
is_array($values_attribute))
-                       {
-                               foreach($values_attribute as $entry)
-                               {
-                                       if($entry['value'])
-                                       {
-                                               $cols[] = $entry['name'];
-                                               $vals[] = $entry['value'];
-                                       }
-                               }
-                       }
-
-                       if($cols)
-                       {
-                               $cols   = "," . implode(",", $cols);
-                               $vals   = "," . 
$this->bocommon->validate_db_insert($vals);
-                       }
-
-                       $table='fm_entity_' . $entity_id .'_' . $cat_id;
-                       
$num=$this->generate_num($entity_id,$cat_id,$values['id']);
-                       $this->db->transaction_begin();
-
-                       $this->db->query("INSERT INTO $table 
(id,num,address,location_code,entry_date,user_id $cols) "
-                               . "VALUES ("
-                               . $values['id']. ",'"
-                               . $num . "','"
-                               . $address. "','"
-                               . $values['location_code']. "',"
-                               . time() . ","
-                               . $this->account. " $vals)",__LINE__,__FILE__);
-
-                       if($values['origin'][0]['data'][0]['id'])
-                       {
-                               $this->db->query("INSERT INTO fm_origin 
(origin,origin_id,destination,destination_id,user_id,entry_date) "
-                                       . "VALUES ('"
-                                       . $values['origin'][0]['type']. "',"
-                                       . 
$values['origin'][0]['data'][0]['id']. ","
-                                       . "'entity_" . $entity_id .'_' . 
$cat_id . "',"
-                                       . $values['id']. ","
-                                       . $this->account. ","
-                                       . time() . ")",__LINE__,__FILE__);
-                       }
-
-                       $this->db->transaction_commit();
-
-                       $receipt['message'][] = array('msg'=>lang('Entity %1 
has been saved',$values['id']));
-                       return $receipt;
-               }
-
-               function edit($values,$values_attribute,$entity_id,$cat_id)
-               {
-
-                       if($values['street_name'])
-                       {
-                               $address[]= $values['street_name'];
-                               $address[]= $values['street_number'];
-                               $address        = implode(" ", $address);
-                       }
-
-                       if(!$address)
-                       {
-                               $address = $values['location_name'];
-                       }
-
-                       $values['descr'] = 
$this->db->db_addslashes($values['descr']);
-
-                       $value_set=array(
-                               'location_code' => $values['location_code'],
-                               'address'       => 
$this->db->db_addslashes($address)
-                               );
-
-                       while (is_array($values['location']) && 
list($column,$value) = each($values['location']))
-                       {
-                               $value_set[$column]     = $value;
-                       }
-
-                       while (is_array($values['extra']) && 
list($column,$value) = each($values['extra']))
-                       {
-                               $value_set[$column]     = $value;
-                       }
-
-                       if (isset($values_attribute) AND 
is_array($values_attribute))
-                       {
-                               foreach($values_attribute as $entry)
-                               {
-                                       if($entry['datatype']!='AB' && 
$entry['datatype']!='VENDOR')
-                                       {
-                                               $value_set[$entry['name']]      
= $entry['value'];
-                                       }
-                               }
-                       }
-
-
-                       $value_set      = 
$this->bocommon->validate_db_update($value_set);
-
-                       $table='fm_entity_' . $entity_id .'_' . $cat_id;
-
-                       $this->db->transaction_begin();
-
-                       $this->db->query("UPDATE $table set $value_set WHERE 
id=" . $values['id'],__LINE__,__FILE__);
-
-                       $this->db->transaction_commit();
-
-                       $receipt['message'][] = array('msg'=>lang('entity %1 
has been edited',$values['num']));
-                       return $receipt;
-               }
-
-               function delete($entity_id,$cat_id,$id )
-               {
-                       $table='fm_entity_' . $entity_id .'_' . $cat_id;
-                       $this->db->transaction_begin();
-                       $this->db->query("DELETE FROM $table WHERE 
id=$id",__LINE__,__FILE__);
-                       $this->db->query("DELETE FROM fm_origin WHERE 
destination ='entity_" . $entity_id . '_' . $cat_id . "' AND 
destination_id=$id",__LINE__,__FILE__);
-                       $this->db->transaction_commit();
-               }
-       }
-?>
+                       $this->db2              = $this->bocommon->new_db();
+
+                       $this->join                     = $this->bocommon->join;
+                       $this->left_join        = $this->bocommon->left_join;
+                       $this->like                     = $this->bocommon->like;
+               }
+
+               function select_status_list($entity_id,$cat_id)
+               {
+                       if(!$entity_id || !$cat_id)
+                       {
+                               return;
+                       }
+
+                       $sql= "SELECT fm_entity_choice.id, 
fm_entity_choice.value FROM fm_entity_attribute $this->join fm_entity_choice ON 
"
+                       . " fm_entity_attribute.entity_id= 
fm_entity_choice.entity_id AND "
+                       . " fm_entity_attribute.cat_id= fm_entity_choice.cat_id 
AND "
+                       . " fm_entity_attribute.id= fm_entity_choice.attrib_id "
+                       . " WHERE fm_entity_attribute.column_name='status' "
+                       . " AND fm_entity_choice.entity_id=$entity_id "
+                       . " AND fm_entity_choice.cat_id=$cat_id ORDER BY 
fm_entity_choice.id";
+
+
+                       $this->db->query($sql,__LINE__,__FILE__);
+
+                       $i = 0;
+                       while ($this->db->next_record())
+                       {
+                               $status[$i]['id']                               
= $this->db->f('id');
+                               $status[$i]['name']                             
= stripslashes($this->db->f('value'));
+                               $i++;
+                       }
+                       return $status;
+               }
+
+
+               function read($data)
+               {
+                       if(is_array($data))
+                       {
+                               if ($data['start'])
+                               {
+                                       $start=$data['start'];
+                               }
+                               else
+                               {
+                                       $start=0;
+                               }
+                               $filter = $data['filter']?$data['filter']:'all';
+                               $query = 
(isset($data['query'])?$data['query']:'');
+                               $sort = 
(isset($data['sort'])?$data['sort']:'DESC');
+                               $order = 
(isset($data['order'])?$data['order']:'');
+                               $cat_id = 
(isset($data['cat_id'])?$data['cat_id']:0);
+                               $district_id = 
(isset($data['district_id'])?$data['district_id']:0);
+                               $lookup = 
(isset($data['lookup'])?$data['lookup']:'');
+                               $allrows = 
(isset($data['allrows'])?$data['allrows']:'');
+                               $entity_id = 
(isset($data['entity_id'])?$data['entity_id']:'');
+                               $cat_id = 
(isset($data['cat_id'])?$data['cat_id']:'');
+                               $status = 
(isset($data['status'])?$data['status']:'');
+                               $start_date = 
(isset($data['start_date'])?$data['start_date']:'');
+                               $end_date = 
(isset($data['end_date'])?$data['end_date']:'');
+                       }
+
+                       if(!$entity_id || !$cat_id)
+                       {
+                               return;
+                       }
+
+                       $grants         = 
$GLOBALS['phpgw']->session->appsession('grants_entity_'.$entity_id.'_'.$cat_id,$this->currentapp);
+
+                       if(!$grants)
+                       {
+                               $this->acl2             = 
CreateObject($this->currentapp.'.acl2','##DEFAULT##','##DEFAULT##');
+                               $grants         = 
$this->acl2->get_grants($this->currentapp,'.entity.' . $entity_id . '.' . 
$cat_id);
+                               
$GLOBALS['phpgw']->session->appsession('grants_entity_'.$entity_id.'_'.$cat_id,$this->currentapp,$grants);
+                       }
+
+
+                       $sql = $this->bocommon->fm_cache('sql_entity_' . 
$entity_id . '_' . $cat_id . '_' . $lookup);
+
+                       $entity_table = 'fm_entity_' . $entity_id . '_' . 
$cat_id;
+                       if(!$sql)
+                       {
+                               $cols = $entity_table . '.*';
+                               $cols_return[] = 'location_code';
+
+                               $cols_return[]                          = 'num';
+                               $uicols['input_type'][]         = 'text';
+                               $uicols['name'][]                       = 'num';
+                               $uicols['descr'][]                      = 
lang('ID');
+                               $uicols['statustext'][]         = lang('ID');
+
+                               $cols_return[]                          = 'id';
+                               $uicols['input_type'][]         = 'hidden';
+                               $uicols['name'][]                       = 'id';
+                               $uicols['descr'][]                      = false;
+                               $uicols['statustext'][]         = false;
+                               if($lookup)
+                               {
+                                       $cols .= ',num as entity_num_' . 
$entity_id;
+                                       $cols_return[] = 'entity_num_' . 
$entity_id;
+                               }
+
+
+                               $admin_entity   = 
CreateObject($this->currentapp.'.soadmin_entity');
+                               $category = 
$admin_entity->read_single_category($entity_id,$cat_id);
+
+                               $sql = 
$this->bocommon->generate_sql(array('entity_table'=>$entity_table,'cols_return'=>$cols_return,'cols'=>$cols,
+                                                               
'uicols'=>$uicols,'joinmethod'=>$joinmethod,'paranthesis'=>$paranthesis,'query'=>$query,'lookup'=>$lookup,'location_level'=>$category['location_level']));
+
+                               $this->bocommon->fm_cache('sql_entity_' . 
$entity_id . '_' . $cat_id . '_' . $lookup,$sql);
+                               $this->bocommon->fm_cache('uicols_entityt_' . 
$entity_id . '_' . $cat_id . '_' . $lookup,$this->bocommon->uicols);
+                               
$this->bocommon->fm_cache('cols_return_entityt_' . $entity_id . '_' . $cat_id . 
'_' . $lookup,$this->bocommon->cols_return);
+                               
$this->bocommon->fm_cache('cols_return_lookup_entityt_' . $entity_id . '_' . 
$cat_id . '_' . $lookup,$this->bocommon->cols_return_lookup);
+                               $this->bocommon->fm_cache('cols_extra_entityt_' 
. $entity_id . '_' . $cat_id . '_' . $lookup,$this->bocommon->cols_extra);
+
+                               $uicols                                         
= $this->bocommon->uicols;
+                               $cols_return                            = 
$this->bocommon->cols_return;
+                               $this->cols_return_lookup       = 
$this->bocommon->cols_return_lookup;
+                               $this->cols_extra                       = 
$this->bocommon->cols_extra;
+                       }
+                       else
+                       {
+                               $uicols                                         
        = $this->bocommon->fm_cache('uicols_entityt_' . $entity_id . '_' . 
$cat_id . '_' . $lookup);
+                               $cols_return                                    
= $this->bocommon->fm_cache('cols_return_entityt_' . $entity_id . '_' . $cat_id 
. '_' . $lookup);
+                               $this->cols_return_lookup               = 
$this->bocommon->fm_cache('cols_return_lookup_entityt_' . $entity_id . '_' . 
$cat_id . '_' . $lookup);
+                               $this->cols_extra                               
= $this->bocommon->fm_cache('cols_extra_entityt_' . $entity_id . '_' . $cat_id 
. '_' . $lookup);
+                               if($lookup)
+                               {
+                                       $admin_entity   = 
CreateObject($this->currentapp.'.soadmin_entity');
+                                       $category = 
$admin_entity->read_single_category($entity_id,$cat_id);
+                               }
+
+                       }
+
+                       if ($cat_id > 0)
+                       {
+                               
$user_columns=$GLOBALS['phpgw_info']['user']['preferences'][$this->currentapp]['entity_columns_'.$entity_id.'_'.$cat_id];
+//_debug_array($user_columns);
+
+                               if (isset($user_columns) AND 
is_array($user_columns) AND $user_columns[0])
+                               {
+                                       $i      = count($uicols['name']);
+                                       foreach($user_columns as $column_id)
+                                       {
+                                               $this->db->query("SELECT * FROM 
fm_entity_attribute WHERE entity_id= $entity_id AND cat_id= $cat_id AND id= 
$column_id");
+
+                                               $this->db->next_record();
+                                               $uicols['input_type'][]         
= 'text';
+                                               $uicols['name'][]               
        = $this->db->f('column_name');
+                                               $uicols['descr'][]              
        = $this->db->f('input_text');
+                                               $uicols['statustext'][]         
= $this->db->f('statustext');
+                                               $uicols['datatype'][$i]         
= $this->db->f('datatype');
+                                               $cols_return_extra[]= array(
+                                                       'name'  => 
$this->db->f('column_name'),
+                                                       'datatype'      => 
$this->db->f('datatype'),
+                                                       'attrib_id'     => 
$this->db->f('id')
+                                               );
+                                               $i++;
+                                       }
+                               }
+                       }
+                       else
+                       {
+                               return;
+                       }
+
+                       $this->uicols   = $uicols;
+
+//_debug_array($cols_return_extra);
+                       if ($order)
+                       {
+                               $ordermethod = " order by $entity_table.$order 
$sort";
+                       }
+                       else
+                       {
+                               $ordermethod = " order by $entity_table.id 
DESC";
+                       }
+
+                       $where= 'WHERE';
+
+                       if ($filter=='all')
+                       {
+                               if (is_array($grants))
+                               {
+                                       while (list($user) = each($grants))
+                                       {
+                                               $public_user_list[] = $user;
+                                       }
+                                       reset($public_user_list);
+                                       $filtermethod .= " $where ( 
$entity_table.user_id IN(" . implode(',',$public_user_list) . "))";
+
+                                       $where= 'AND';
+                               }
+
+                       }
+                       else
+                       {
+                               $filtermethod = " $where 
$entity_table.user_id=$filter ";
+                               $where= 'AND';
+                       }
+
+
+                       if ($status)
+                       {
+                               $filtermethod .= " $where 
$entity_table.status='$status' ";
+                               $where= 'AND';
+                       }
+
+                       if ($district_id > 0)
+                       {
+                               $filtermethod .= " $where  
district_id='$district_id' ";
+                               $where = 'AND';
+                       }
+
+                       if ($start_date)
+                       {
+                               $filtermethod .= " $where 
$entity_table.entry_date >= $start_date AND $entity_table.entry_date <= 
$end_date ";
+                               $where= 'AND';
+                       }
+
+                       if($query)
+                       {
+                               $query = ereg_replace("'",'',$query);
+                               $query = ereg_replace('"','',$query);
+
+                               $filtermethod .= " $where ( 
$entity_table.location_code $this->like '%$query%' OR $entity_table.num 
$this->like '%$query%'  OR loc1_name $this->like '%$query%' OR address 
$this->like '%$query%')";
+                               $where= 'OR';
+
+                               $this->db->query("SELECT * FROM 
fm_entity_attribute where search='1' AND entity_id= $entity_id AND cat_id = 
$cat_id");
+
+                               while ($this->db->next_record())
+                               {
+                                       if($this->db->f('datatype')=='V' || 
$this->db->f('datatype')=='email' || $this->db->f('datatype')=='CH')
+                                       {
+                                               $querymethod[]= 
"$entity_table." . $this->db->f('column_name') . " $this->like '%$query%'";
+                                       }
+                                       else
+                                       {
+                                               $querymethod[]= 
"$entity_table." . $this->db->f('column_name') . " = '$query'";
+                                       }
+                               }
+
+                               if (isset($querymethod) AND 
is_array($querymethod))
+                               {
+                                       $querymethod = " $where (" . implode (' 
OR ',$querymethod) . ')';
+                                       $where = 'AND';
+                               }
+
+                       }
+
+                       $sql .= " $filtermethod $querymethod";
+
+//echo $sql;
+
+                       $this->db2->query($sql,__LINE__,__FILE__);
+                       $this->total_records = $this->db2->num_rows();
+                       if(!$allrows)
+                       {
+                               $this->db->limit_query($sql . 
$ordermethod,$start,__LINE__,__FILE__);
+                       }
+                       else
+                       {
+                               $this->db->query($sql . 
$ordermethod,__LINE__,__FILE__);
+                       }
+
+                       $j=0;
+                       $n=count($cols_return);
+//_debug_array($cols_return);
+                       $contacts                       = 
CreateObject('phpgwapi.contacts');
+
+                       while ($this->db->next_record())
+                       {
+                               for ($i=0;$i<$n;$i++)
+                               {
+                                       $entity_list[$j][$cols_return[$i]] = 
$this->db->f($cols_return[$i]);
+                                       $entity_list[$j]['grants'] = 
(int)$grants[$this->db->f('user_id')];
+                                       if($lookup)
+                                       {
+                                               
$entity_list[$j]['entity_cat_name_' . $entity_id] = $category['name'];
+                                               $entity_list[$j]['entity_id_' . 
$entity_id] = $entity_id;
+                                               $entity_list[$j]['cat_id_' . 
$entity_id] = $cat_id;
+                                       }
+                               }
+
+                               for ($i=0;$i<count($cols_return_extra);$i++)
+                               {
+                                       $value='';
+                                       
$value=$this->db->f($cols_return_extra[$i]['name']);
+
+                                       
if(($cols_return_extra[$i]['datatype']=='R' || 
$cols_return_extra[$i]['datatype']=='LB') && $value):
+                                       {
+                                               $sql="SELECT value FROM 
fm_entity_choice where entity_id=$entity_id AND cat_id=$cat_id AND attrib_id=" 
.$cols_return_extra[$i]['attrib_id']. "  AND id=" . $value;
+                                               $this->db2->query($sql);
+                                               $this->db2->next_record();
+                                               
$entity_list[$j][$cols_return_extra[$i]['name']] = $this->db2->f('value');
+                                       }
+                                       
elseif($cols_return_extra[$i]['datatype']=='AB' && $value):
+                                       {
+                                               $contact_data   = 
$contacts->read_single_entry($value,array('n_given'=>'n_given','n_family'=>'n_family','email'=>'email'));
+                                               
$entity_list[$j][$cols_return_extra[$i]['name']]        = 
$contact_data[0]['n_family'] . ', ' . $contact_data[0]['n_given'];
+                                       }
+                                       
elseif($cols_return_extra[$i]['datatype']=='VENDOR' && $value):
+                                       {
+                                               $sql="SELECT org_name FROM 
fm_vendor where id=$value";
+                                               $this->db2->query($sql);
+                                               $this->db2->next_record();
+                                               
$entity_list[$j][$cols_return_extra[$i]['name']] = $this->db2->f('org_name');
+                                       }
+                                       
elseif($cols_return_extra[$i]['datatype']=='CH' && $value):
+                                       {
+                                               $ch= unserialize($value);
+
+                                               if (isset($ch) AND 
is_array($ch))
+                                               {
+                                                       for 
($k=0;$k<count($ch);$k++)
+                                                       {
+                                                               $sql="SELECT 
value FROM fm_entity_choice where entity_id=$entity_id AND cat_id=$cat_id AND 
attrib_id=" .$cols_return_extra[$i]['attrib_id']. "  AND id=" . $ch[$k];
+                                                               
$this->db2->query($sql);
+                                                               while 
($this->db2->next_record())
+                                                               {
+                                                                       
$ch_value[]=$this->db2->f('value');
+                                                               }
+                                                       }
+                                                       
$entity_list[$j][$cols_return_extra[$i]['name']] = @implode(",", $ch_value);
+                                                       unset($ch_value);
+                                               }
+                                       }
+                                       
elseif($cols_return_extra[$i]['datatype']=='D' && $value):
+                                       {
+//_debug_array($value);
+
+                                               
$entity_list[$j][$cols_return_extra[$i]['name']]=date($GLOBALS['phpgw_info']['user']['preferences']['common']['dateformat'],strtotime($value));
+                                       }
+                                       else:
+                                       {
+                                               
$entity_list[$j][$cols_return_extra[$i]['name']]=$value;
+                                       }
+                                       endif;
+                               }
+
+                               $location_code= $this->db->f('location_code');
+                               $location = split('-',$location_code);
+                               for ($m=0;$m<count($location);$m++)
+                               {
+                                       $entity_list[$j]['loc' . ($m+1)] = 
$location[$m];
+                                       
$entity_list[$j]['query_location']['loc' . ($m+1)]=implode("-", 
array_slice($location, 0, ($m+1)));
+                               }
+
+                               $j++;
+                       }
+//_debug_array($entity_list);
+                       return $entity_list;
+               }
+
+               function read_single($data)
+               {
+                       $entity_id =$data['entity_id'];
+                       $cat_id =$data['cat_id'];
+                       $id =$data['id'];
+
+                       $this->db->query("SELECT * FROM fm_entity_attribute 
WHERE entity_id =$entity_id AND cat_id =$cat_id ORDER BY attrib_sort");
+
+                       while ($this->db->next_record())
+                       {
+                               $entity['attributes'][] = array
+                               (
+                                       'attrib_id'             => 
$this->db->f('id'),
+                                       'name'                  => 
$this->db->f('column_name'),
+                                       'input_text'    => 
stripslashes($this->db->f('input_text')),
+                                       'statustext'    => 
stripslashes($this->db->f('statustext')),
+//                                     'statustext'    => str_replace("\n"," 
",stripslashes($this->db->f('statustext'))),
+                                       'datatype'              => 
$this->db->f('datatype')
+                               );
+                       }
+//_debug_array($entity);
+
+                       if($id)
+                       {
+                               $table='fm_entity_' . $entity_id .'_' . $cat_id;
+
+                               $this->db->query("SELECT * FROM $table WHERE id 
=$id");
+
+                               if($this->db->next_record())
+                               {
+                                       $entity['id']                           
= $id;
+                                       $entity['num']                          
= $this->db->f('num');
+                                       $entity['p_num']                        
= $this->db->f('p_num');
+                                       $entity['p_entity_id']          = 
$this->db->f('p_entity_id');
+                                       $entity['p_cat_id']                     
= $this->db->f('p_cat_id');
+                                       $entity['location_code']        = 
$this->db->f('location_code');
+                                       $entity['tenant_id']            = 
$this->db->f('tenant_id');
+                                       $entity['contact_phone']        = 
$this->db->f('contact_phone');
+                                       $entity['status']                       
= $this->db->f('status');
+
+                                       for 
($i=0;$i<count($entity['attributes']);$i++)
+                                       {
+                                               
$entity['attributes'][$i]['value']      = 
$this->db->f($entity['attributes'][$i]['name']);
+                                               
$entity['attributes'][$i]['datatype_text']      = 
$this->bocommon->translate_datatype($entity['attributes'][$i]['datatype']);
+                                       }
+                               }
+
+                               $sql = "SELECT * FROM fm_origin WHERE 
destination ='entity_" . $entity_id . '_' . $cat_id . "' AND destination_id = 
$id";
+                               $this->db->query($sql,__LINE__,__FILE__);
+
+                               $i=-1;
+                               while ($this->db->next_record())
+                               {
+                                       if($last_type != $this->db->f('origin'))
+                                       {
+                                               $i++;
+                                       }
+                                       $entity['origin'][$i]['type'] = 
$this->db->f('origin');
+                                       $entity['origin'][$i]['link'] = 
$this->bocommon->get_origin_link($this->db->f('origin'));
+                                       $entity['origin'][$i]['data'][]= array(
+                                               'id'=> 
$this->db->f('origin_id'),
+                                               'type'=> $this->db->f('origin')
+                                               );
+
+                                       $last_type=$this->db->f('origin');
+                               }
+                       }
+
+                       return  $entity;
+               }
+
+
+               function check_entity($entity_id,$cat_id,$num)
+               {
+                       $table='fm_entity_' . $entity_id .'_' . $cat_id;
+                       $this->db->query("SELECT count(*) FROM $table where 
num='$num'");
+
+                       $this->db->next_record();
+
+                       if ( $this->db->f(0))
+                       {
+                               return True;
+                       }
+               }
+
+               function generate_id($data)
+               {
+                       $table='fm_entity_' . $data['entity_id'] .'_' . 
$data['cat_id'];
+                       $this->db->query("select max(id) as id from $table");
+                       $this->db->next_record();
+                       $id = $this->db->f('id')+1;
+
+                       return $id;
+               }
+
+               function generate_num($entity_id,$cat_id,$id)
+               {
+                       $this->db->query("select prefix from fm_entity_category 
WHERE entity_id=$entity_id AND id=$cat_id ");
+                       $this->db->next_record();
+                       $prefix = $this->db->f('prefix');
+
+                       if (strlen($id) == 4)
+                               $return = $id;
+                       if (strlen($id) == 3)
+                               $return = "0$id";
+                       if (strlen($id) == 2)
+                               $return = "00$id";
+                       if (strlen($id) == 1)
+                               $return = "000$id";
+                       if (strlen($id) == 0)
+                               $return = "0001";
+
+                       return $prefix . strtoupper($return);
+               }
+
+
+               function add($values,$values_attribute,$entity_id,$cat_id)
+               {
+                       if($values['street_name'])
+                       {
+                               $address[]= $values['street_name'];
+                               $address[]= $values['street_number'];
+                               $address = $this->db->db_addslashes(implode(" 
", $address));
+                       }
+
+                       if(!$address)
+                       {
+                               $address = 
$this->db->db_addslashes($values['location_name']);
+                       }
+
+                       $values['descr'] = 
$this->db->db_addslashes($values['descr']);
+
+                       while (is_array($values['location']) && 
list($input_name,$value) = each($values['location']))
+                       {
+                               if($value)
+                               {
+                                       $cols[] = $input_name;
+                                       $vals[] = $value;
+                               }
+                       }
+//_debug_array($values);
+                       while (is_array($values['extra']) && 
list($input_name,$value) = each($values['extra']))
+                       {
+                               if($value)
+                               {
+                                       $cols[] = $input_name;
+                                       $vals[] = $value;
+                               }
+                       }
+
+                       if (isset($values_attribute) AND 
is_array($values_attribute))
+                       {
+                               foreach($values_attribute as $entry)
+                               {
+                                       if($entry['value'])
+                                       {
+                                               $cols[] = $entry['name'];
+                                               $vals[] = $entry['value'];
+                                       }
+                               }
+                       }
+
+                       if($cols)
+                       {
+                               $cols   = "," . implode(",", $cols);
+                               $vals   = "," . 
$this->bocommon->validate_db_insert($vals);
+                       }
+
+                       $table='fm_entity_' . $entity_id .'_' . $cat_id;
+                       
$num=$this->generate_num($entity_id,$cat_id,$values['id']);
+                       $this->db->transaction_begin();
+
+                       $this->db->query("INSERT INTO $table 
(id,num,address,location_code,entry_date,user_id $cols) "
+                               . "VALUES ("
+                               . $values['id']. ",'"
+                               . $num . "','"
+                               . $address. "','"
+                               . $values['location_code']. "',"
+                               . time() . ","
+                               . $this->account. " $vals)",__LINE__,__FILE__);
+
+                       if($values['origin'][0]['data'][0]['id'])
+                       {
+                               $this->db->query("INSERT INTO fm_origin 
(origin,origin_id,destination,destination_id,user_id,entry_date) "
+                                       . "VALUES ('"
+                                       . $values['origin'][0]['type']. "',"
+                                       . 
$values['origin'][0]['data'][0]['id']. ","
+                                       . "'entity_" . $entity_id .'_' . 
$cat_id . "',"
+                                       . $values['id']. ","
+                                       . $this->account. ","
+                                       . time() . ")",__LINE__,__FILE__);
+                       }
+
+                       $this->db->transaction_commit();
+
+                       $receipt['message'][] = array('msg'=>lang('Entity %1 
has been saved',$values['id']));
+                       return $receipt;
+               }
+
+               function edit($values,$values_attribute,$entity_id,$cat_id)
+               {
+
+                       if($values['street_name'])
+                       {
+                               $address[]= $values['street_name'];
+                               $address[]= $values['street_number'];
+                               $address        = implode(" ", $address);
+                       }
+
+                       if(!$address)
+                       {
+                               $address = $values['location_name'];
+                       }
+
+                       $values['descr'] = 
$this->db->db_addslashes($values['descr']);
+
+                       $value_set=array(
+                               'location_code' => $values['location_code'],
+                               'address'       => 
$this->db->db_addslashes($address)
+                               );
+
+                       while (is_array($values['location']) && 
list($column,$value) = each($values['location']))
+                       {
+                               $value_set[$column]     = $value;
+                       }
+
+                       while (is_array($values['extra']) && 
list($column,$value) = each($values['extra']))
+                       {
+                               $value_set[$column]     = $value;
+                       }
+
+                       if (isset($values_attribute) AND 
is_array($values_attribute))
+                       {
+                               foreach($values_attribute as $entry)
+                               {
+                                       if($entry['datatype']!='AB' && 
$entry['datatype']!='VENDOR')
+                                       {
+                                               $value_set[$entry['name']]      
= $entry['value'];
+                                       }
+                               }
+                       }
+
+
+                       $value_set      = 
$this->bocommon->validate_db_update($value_set);
+
+                       $table='fm_entity_' . $entity_id .'_' . $cat_id;
+
+                       $this->db->transaction_begin();
+
+                       $this->db->query("UPDATE $table set $value_set WHERE 
id=" . $values['id'],__LINE__,__FILE__);
+
+                       $this->db->transaction_commit();
+
+                       $receipt['message'][] = array('msg'=>lang('entity %1 
has been edited',$values['num']));
+                       return $receipt;
+               }
+
+               function delete($entity_id,$cat_id,$id )
+               {
+                       $table='fm_entity_' . $entity_id .'_' . $cat_id;
+                       $this->db->transaction_begin();
+                       $this->db->query("DELETE FROM $table WHERE 
id=$id",__LINE__,__FILE__);
+                       $this->db->query("DELETE FROM fm_origin WHERE 
destination ='entity_" . $entity_id . '_' . $cat_id . "' AND 
destination_id=$id",__LINE__,__FILE__);
+                       $this->db->transaction_commit();
+               }
+       }
+?>
Index: property/inc/class.soinvestment.inc.php
diff -u property/inc/class.soinvestment.inc.php:1.7 
property/inc/class.soinvestment.inc.php:1.8
--- property/inc/class.soinvestment.inc.php:1.7 Fri Jan 27 14:05:43 2006
+++ property/inc/class.soinvestment.inc.php     Mon Jan 30 22:14:19 2006
@@ -1,360 +1,359 @@
-<?php
-       /**
-       * phpGroupWare - property: a Facilities Management System.
-       *
-       * @author Sigurd Nes <address@hidden>
-       * @copyright Copyright (C) 2003-2005 Free Software Foundation, Inc. 
http://www.fsf.org/
-       * @license http://www.gnu.org/licenses/gpl.html GNU General Public 
License
-       * @internal Development of this application was funded by 
http://www.bergen.kommune.no/bbb_/ekstern/
-       * @package property
-       * @subpackage eco
-       * @version $Id: class.soinvestment.inc.php,v 1.7 2006/01/27 14:05:43 
sigurdne Exp $
-       */
-
-       /**
-        * Description
-        * @package property
-        */
-
-       class soinvestment
-       {
-
-               function soinvestment()
-               {
-                       $this->currentapp       = 
$GLOBALS['phpgw_info']['flags']['currentapp'];
-                       $this->account  = 
$GLOBALS['phpgw_info']['user']['account_id'];
-                       $this->bocommon         = 
CreateObject($this->currentapp.'.bocommon');
-                       $this->db               = $this->bocommon->new_db();
-                       $this->db2              = $this->bocommon->new_db();
-                       $this->join                     = $this->bocommon->join;
-                       $this->like                     = $this->bocommon->like;
-               }
-
-               function get_type_list()
-               {
-                       $this->db->query("SELECT entity_type FROM fm_investment 
GROUP BY entity_type ");
-                       while ($this->db->next_record())
-                       {
-                               $type_list[] = Array(
-                                       'id'        => 
$this->db->f('entity_type'),
-                                       'num'       => 
lang($this->db->f('entity_type'))
-                               );
-                       }
-                       return $type_list;
-               }
-
-
-               function read($data)
-               {
-                       if(is_array($data))
-                       {
-                               if ($data['start'])
-                               {
-                                       $start=$data['start'];
-                               }
-                               else
-                               {
-                                       $start=0;
-                               }
-                               $filter = 
(isset($data['filter'])?$data['filter']:'');
-                               $query = 
(isset($data['query'])?$data['query']:'');
-                               $sort = 
(isset($data['sort'])?$data['sort']:'DESC');
-                               $order = 
(isset($data['order'])?$data['order']:'');
-                               $cat_id = 
(isset($data['cat_id'])?$data['cat_id']:'');
-                               $part_of_town_id = 
(isset($data['part_of_town_id'])?$data['part_of_town_id']:'');
-                               $allrows                = 
(isset($data['allrows'])?$data['allrows']:'');
-
-                       }
-                       if (!$cat_id)
-                       {
-                               return;
-                       }
-
-                       if ($order)
-                       {
-                               $ordermethod = " order by $order $sort";
-                       }
-                       else
-                       {
-                               if ($cat_id=='property')
-                               {
-                                       $ordermethod = ' order by 
fm_part_of_town.name ,fm_location1.loc1 DESC';
-                               }
-                               else
-                               {
-                                       $ordermethod = ' order by 
fm_investment.entity_id  DESC';
-                               }
-                       }
-
-                       if ($part_of_town_id)
-                       {
-                               $filtermethod = "and 
fm_part_of_town.part_of_town_id ='$part_of_town_id'";
-                       }
-
-                       if ($filter=='investment')
-                       {
-                               $filtermethod .= "and initial_value > 0";
-                       }
-                       if ($filter=='funding')
-                       {
-                               $filtermethod .= "and initial_value < 0";
-                       }
-
-                       if ($cat_id=='property')
-                       {
-                               $sql = "SELECT fm_investment.entity_id as 
entity_id, fm_investment.descr as descr, 
fm_investment_value.invest_id,initial_value, fm_location1.loc1_name as name, 
fm_part_of_town.district_id, fm_part_of_town.name as part_of_town,"
-                               . " fm_investment_value.value, 
fm_investment_value.index_date, fm_investment_value.this_index, "
-                               . " fm_investment_value.index_count"
-                               . " FROM (((fm_investment $this->join 
fm_investment_value ON ( fm_investment.entity_id = 
fm_investment_value.entity_id) AND "
-                               . " (fm_investment.invest_id = 
fm_investment_value.invest_id )) "
-                               . " $this->join fm_location1 ON 
(fm_investment.loc1 = fm_location1.loc1)) "
-                               . " $this->join fm_part_of_town ON 
(fm_location1.part_of_town_id = fm_part_of_town.part_of_town_id)) "
-                               . " WHERE ( current_index = '1'  or (this_index 
= NULL and index_count= '1'))  AND entity_type ='$cat_id' $filtermethod ";
-
-                       }
-                       else
-                       {
-                               $sql = "SELECT 
fm_investment.location_code,fm_investment.entity_id, fm_investment.descr as 
descr, fm_investment_value.invest_id,initial_value, 
fm_part_of_town.district_id, fm_part_of_town.name as part_of_town,"
-                               . " fm_investment_value.value, 
fm_investment_value.index_date, 
fm_investment_value.this_index,fm_entity_category.name as entity_name, "
-                               . " fm_investment_value.index_count "
-                               . " FROM ((((fm_investment $this->join "
-                               . " fm_entity_category ON 
(fm_investment.p_entity_id = fm_entity_category.entity_id AND 
fm_investment.p_cat_id = fm_entity_category.id)) $this->join "
-                               . " fm_investment_value ON 
(fm_investment_value.entity_id = fm_investment.entity_id) AND "
-                               . " (fm_investment_value.invest_id = 
fm_investment.invest_id)) "
-                               . " $this->join fm_location1 ON 
(fm_investment.loc1 = fm_location1.loc1)) "
-                               . " $this->join fm_part_of_town ON 
(fm_location1.part_of_town_id = fm_part_of_town.part_of_town_id)) "
-                               . " WHERE ( current_index = '1'  or (this_index 
= NULL and index_count= '1'))  AND entity_type ='$cat_id' $filtermethod ";
-                       }
-                       if($sql)
-                       {
-                               $this->db2->query($sql,__LINE__,__FILE__);
-                               $this->total_records = $this->db2->num_rows();
-
-                               if(!$allrows)
-                               {
-                                       $this->db->limit_query($sql . 
$ordermethod,$start,__LINE__,__FILE__);
-                               }
-                               else
-                               {
-                                       $this->db->query($sql . 
$ordermethod,__LINE__,__FILE__);
-                               }
-                       }
-
-
-                       $i=0;
-                       while ($this->db->next_record())
-                       {
-                               $investment[$i]['counter']                      
= $i;
-                               $investment[$i]['location_code']        = 
$this->db->f('location_code');
-                               $investment[$i]['entity_id']            = 
$this->db->f('entity_id');
-                               $investment[$i]['investment_id']        = 
$this->db->f('invest_id');
-                               $investment[$i]['district_id']          = 
$this->db->f('district_id');
-                               $investment[$i]['part_of_town']         = 
$this->db->f('part_of_town');
-                               $investment[$i]['descr']                        
= $this->db->f('descr');
-                               $investment[$i]['initial_value']        = 
$this->db->f('initial_value');
-                               $investment[$i]['value']                        
= $this->db->f('value');
-                               $investment[$i]['this_index']           = 
$this->db->f('this_index');
-                               $investment[$i]['index_count']          = 
$this->db->f('index_count');
-                               $investment[$i]['date']                         
= $this->db->f('index_date');
-                               if ($cat_id=='property')
-                               {
-                                       $investment[$i]['entity_name']  = 
$this->db->f('name');
-                               }
-                               else
-                               {
-                                       $investment[$i]['entity_name']  = 
$this->db->f('entity_name');
-                               }
-
-                               $investment[$i]['this_write_off']       = 
round(($this->db->f('this_index') * $this->db->f('initial_value')),2);
-                               $i++;
-                       }
-
-//_debug_array($investment);
-                       return $investment;
-               }
-
-               function save_investment($values)
-               {
-//_debug_array($values);
-
-                       while (is_array($values['location']) && 
list($input_name,$value) = each($values['location']))
-                       {
-                               if($value)
-                               {
-                                       $cols[] = $input_name;
-                                       $vals[] = $value;
-                               }
-                       }
-
-                       while (is_array($values['extra']) && 
list($input_name,$value) = each($values['extra']))
-                       {
-                               if($value)
-                               {
-                                       $cols[] = $input_name;
-                                       $vals[] = $value;
-                               }
-                       }
-
-                       if($cols)
-                       {
-                               $cols   = "," . implode(",", $cols);
-                               $vals   = ",'" . implode("','", $vals) . "'";
-                       }
-
-                       if($values['street_name'])
-                       {
-                               $address[]= $values['street_name'];
-                               $address[]= $values['street_number'];
-                               $address        = 
$this->db->db_addslashes(implode(" ", $address));
-                       }
-
-                       if(!$address)
-                       {
-                               $address = 
$this->db->db_addslashes($values['location_name']);
-                       }
-
-
-                       $period         = $values['period'];
-                       $type           = $values['funding'];
-                       $initial_value  = $values['initial_value'];
-                       $descr          = 
$this->db->db_addslashes($values['descr']);
-                       $date           = $values['date'];
-                       $location_code  = $values['location_code'];
-                       $entity_id      = $values['entity_id'];
-                       $entity_type    = $values['entity_type'];
-
-
-                       $this->db->query("select max(invest_id) as 
max_invest_id from fm_investment Where entity_id= '$entity_id'");
-                       $this->db->next_record();
-                       $next_invest_id  = $this->db->f('max_invest_id')+1;
-
-                       $this->db->transaction_begin();
-
-                       $this->db->query("insert into fm_investment (entity_id, 
invest_id,entity_type,location_code,writeoff_year, descr $cols) "
-                       . " values ('$entity_id', 
'$next_invest_id','$entity_type','$location_code','$period','$descr' $vals )");
-
-                       $this->db->query("insert into fm_investment_value 
(entity_id, invest_id, index_count, this_index, current_index, 
value,initial_value, index_date) "
-                       . " values ('$entity_id', '$next_invest_id','1', '0', 
'1','$initial_value','$initial_value','$date')");
-
-
-                       if($this->db->transaction_commit())
-                       {
-                               $receipt['message'][]=array('msg' => 
lang('Investment added !'));
-                               $receipt['message'][]=array('msg' => 
lang('Entity ID'). ' ' . $entity_id);
-                               $receipt['message'][]=array('msg' => 
lang('Investment ID:'). ' ' . $next_invest_id);
-                       }
-                       return $receipt;
-               }
-
-               function update_investment($values)
-               {
-                       if (isSet($values) AND is_array($values))
-                       {
-                               $this->db->transaction_begin();
-                               foreach($values as $entry)
-                               {
-                                       $this->db->query("select 
max(index_count) as max_index_count from fm_investment_value Where entity_id='" 
.$entry['entity_id'] . "' and invest_id=" .$entry['invest_id']);
-                                       $this->db->next_record();
-                                       $next_index_count  = 
$this->db->f('max_index_count')+1;
-
-                                       $this->db->query("update 
fm_investment_value set current_index = Null"
-                                       . " WHERE entity_id='" . 
$entry['entity_id'] . "' and invest_id=" . $entry['invest_id']);
-
-                                       $insert= array(
-                                               $entry['entity_id'],
-                                               $entry['invest_id'],
-                                               $next_index_count,
-                                               $entry['new_index'],
-                                               $entry['new_value'],
-                                               $entry['initial_value'],
-                                               $entry['date'],
-                                               1
-                                               );
-
-                                       $insert = 
$this->bocommon->validate_db_insert($insert);
-
-
-                                       $this->db->query("insert into 
fm_investment_value (entity_id, invest_id, index_count, this_index, 
value,initial_value, index_date,current_index) "
-                                       . " values ($insert)");
-                               }
-                               $this->db->transaction_commit();
-                       }
-               }
-
-               function read_single($entity_id,$investment_id,$start,$allrows)
-               {
-                       if (!$start)
-                       {
-                               $start=0;
-                       }
-
-
-                       $sql = "SELECT index_count, 
this_index,current_index,value, initial_value, index_date  "
-                       . " FROM fm_investment_value Where entity_id= 
'$entity_id' and invest_id= '$investment_id' order by index_count";
-
-                       $this->db2->query($sql,__LINE__,__FILE__);
-                       $this->total_records = $this->db2->num_rows();
-
-                       if(!$allrows)
-                       {
-                               
$this->db->limit_query($sql,$start,__LINE__,__FILE__);
-                       }
-                       else
-                       {
-                               $this->db->query($sql,__LINE__,__FILE__);
-                       }
-
-                       $i=0;
-                       while ($this->db->next_record())
-                       {
-                               $investment[$i]['descr']                        
= $this->db->f('descr');
-                               $investment[$i]['initial_value']        = 
$this->db->f('initial_value');
-                               $investment[$i]['value']                        
= $this->db->f('value');
-                               $investment[$i]['this_index']           = 
$this->db->f('this_index');
-                               $investment[$i]['current_index']        = 
$this->db->f('current_index');
-                               $investment[$i]['index_count']          = 
$this->db->f('index_count');
-                               $investment[$i]['date']                         
= $this->db->f('index_date');
-                               $investment[$i]['this_write_off']       = 
round(($this->db->f('this_index') * $this->db->f('initial_value')),2);
-                               $i++;
-                       }
-
-                       return $investment;
-               }
-
-
-               function write_off_period_list()
-               {
-                       $this->db->query("SELECT writeoff_year FROM 
fm_investment GROUP BY writeoff_year ",__LINE__,__FILE__);
-
-                       while ($this->db->next_record())
-                       {
-                               $period_list[] = Array(
-                                       'period'        => 
$this->db->f('writeoff_year')
-                               );
-                       }
-
-                       return $period_list;
-
-               }
-
-               function delete($entity_id,$invest_id,$index_count)
-               {
-                       $this->db->transaction_begin();
-                       if ($index_count==1)
-                       {
-                               $this->db->query("update fm_investment_value 
set current_index = '0', this_index=Null, 
value=Null,initial_value=Null,index_date=Null  where entity_id='$entity_id' and 
invest_id= '$invest_id' and index_count= '1'");
-                       }
-                       else
-                       {
-                               $this->db->query("delete from 
fm_investment_value where entity_id='$entity_id' and invest_id= '$invest_id' 
and index_count= '$index_count'");
-
-                               $new_current= $index_count -1;
-
-                               $this->db->query("update fm_investment_value 
set current_index = '1' where entity_id='$entity_id' and invest_id= 
'$invest_id' and index_count= '$new_current'");
-                       }
-
-       //              $this->db->query("DELETE FROM fm_investment_value WHERE 
entity_id= '$entity_id'  and invest_id='$invest_id' and 
index_count='$index_count'",__LINE__,__FILE__);
-                       $this->db->transaction_commit();
-               }
-       }
-?>
+<?php
+       /**
+       * phpGroupWare - property: a Facilities Management System.
+       *
+       * @author Sigurd Nes <address@hidden>
+       * @copyright Copyright (C) 2003-2005 Free Software Foundation, Inc. 
http://www.fsf.org/
+       * @license http://www.gnu.org/licenses/gpl.html GNU General Public 
License
+       * @internal Development of this application was funded by 
http://www.bergen.kommune.no/bbb_/ekstern/
+       * @package property
+       * @subpackage eco
+       * @version $Id: class.soinvestment.inc.php,v 1.8 2006/01/30 22:14:19 
sigurdne Exp $
+       */
+
+       /**
+        * Description
+        * @package property
+        */
+
+       class soinvestment
+       {
+               function soinvestment()
+       {
+               $this->currentapp       = 
$GLOBALS['phpgw_info']['flags']['currentapp'];
+               $this->account  = $GLOBALS['phpgw_info']['user']['account_id'];
+               $this->bocommon         = 
CreateObject($this->currentapp.'.bocommon');
+               $this->db               = $this->bocommon->new_db();
+               $this->db2              = $this->bocommon->new_db();
+                       $this->join                     = $this->bocommon->join;
+                       $this->like                     = $this->bocommon->like;
+               }
+
+               function get_type_list()
+               {
+                       $this->db->query("SELECT entity_type FROM fm_investment 
GROUP BY entity_type ");
+                       while ($this->db->next_record())
+                       {
+                               $type_list[] = Array(
+                                       'id'        => 
$this->db->f('entity_type'),
+                                       'num'       => 
lang($this->db->f('entity_type'))
+                               );
+                       }
+                       return $type_list;
+               }
+
+
+               function read($data)
+               {
+                       if(is_array($data))
+                       {
+                               if ($data['start'])
+                               {
+                                       $start=$data['start'];
+                               }
+                               else
+                               {
+                                       $start=0;
+                               }
+                               $filter = 
(isset($data['filter'])?$data['filter']:'');
+                               $query = 
(isset($data['query'])?$data['query']:'');
+                               $sort = 
(isset($data['sort'])?$data['sort']:'DESC');
+                               $order = 
(isset($data['order'])?$data['order']:'');
+                               $cat_id = 
(isset($data['cat_id'])?$data['cat_id']:'');
+                               $part_of_town_id = 
(isset($data['part_of_town_id'])?$data['part_of_town_id']:'');
+                               $allrows                = 
(isset($data['allrows'])?$data['allrows']:'');
+
+                       }
+                       if (!$cat_id)
+                       {
+                               return;
+                       }
+
+                       if ($order)
+                       {
+                               $ordermethod = " order by $order $sort";
+                       }
+                       else
+                       {
+                               if ($cat_id=='property')
+                               {
+                                       $ordermethod = ' order by 
fm_part_of_town.name ,fm_location1.loc1 DESC';
+                               }
+                               else
+                               {
+                                       $ordermethod = ' order by 
fm_investment.entity_id  DESC';
+                               }
+                       }
+
+                       if ($part_of_town_id)
+                       {
+                               $filtermethod = "and 
fm_part_of_town.part_of_town_id ='$part_of_town_id'";
+                       }
+
+                       if ($filter=='investment')
+                       {
+                               $filtermethod .= "and initial_value > 0";
+                       }
+                       if ($filter=='funding')
+                       {
+                               $filtermethod .= "and initial_value < 0";
+                       }
+
+                       if ($cat_id=='property')
+                       {
+                               $sql = "SELECT fm_investment.entity_id as 
entity_id, fm_investment.descr as descr, 
fm_investment_value.invest_id,initial_value, fm_location1.loc1_name as name, 
fm_part_of_town.district_id, fm_part_of_town.name as part_of_town,"
+                               . " fm_investment_value.value, 
fm_investment_value.index_date, fm_investment_value.this_index, "
+                               . " fm_investment_value.index_count"
+                               . " FROM (((fm_investment $this->join 
fm_investment_value ON ( fm_investment.entity_id = 
fm_investment_value.entity_id) AND "
+                               . " (fm_investment.invest_id = 
fm_investment_value.invest_id )) "
+                               . " $this->join fm_location1 ON 
(fm_investment.loc1 = fm_location1.loc1)) "
+                               . " $this->join fm_part_of_town ON 
(fm_location1.part_of_town_id = fm_part_of_town.part_of_town_id)) "
+                               . " WHERE ( current_index = '1'  or (this_index 
= NULL and index_count= '1'))  AND entity_type ='$cat_id' $filtermethod ";
+
+                       }
+                       else
+                       {
+                               $sql = "SELECT 
fm_investment.location_code,fm_investment.entity_id, fm_investment.descr as 
descr, fm_investment_value.invest_id,initial_value, 
fm_part_of_town.district_id, fm_part_of_town.name as part_of_town,"
+                               . " fm_investment_value.value, 
fm_investment_value.index_date, 
fm_investment_value.this_index,fm_entity_category.name as entity_name, "
+                               . " fm_investment_value.index_count "
+                               . " FROM ((((fm_investment $this->join "
+                               . " fm_entity_category ON 
(fm_investment.p_entity_id = fm_entity_category.entity_id AND 
fm_investment.p_cat_id = fm_entity_category.id)) $this->join "
+                               . " fm_investment_value ON 
(fm_investment_value.entity_id = fm_investment.entity_id) AND "
+                               . " (fm_investment_value.invest_id = 
fm_investment.invest_id)) "
+                               . " $this->join fm_location1 ON 
(fm_investment.loc1 = fm_location1.loc1)) "
+                               . " $this->join fm_part_of_town ON 
(fm_location1.part_of_town_id = fm_part_of_town.part_of_town_id)) "
+                               . " WHERE ( current_index = '1'  or (this_index 
= NULL and index_count= '1'))  AND entity_type ='$cat_id' $filtermethod ";
+                       }
+                       if($sql)
+                       {
+                               $this->db2->query($sql,__LINE__,__FILE__);
+                               $this->total_records = $this->db2->num_rows();
+
+                               if(!$allrows)
+                               {
+                                       $this->db->limit_query($sql . 
$ordermethod,$start,__LINE__,__FILE__);
+                               }
+                               else
+                               {
+                                       $this->db->query($sql . 
$ordermethod,__LINE__,__FILE__);
+                               }
+                       }
+
+
+                       $i=0;
+                       while ($this->db->next_record())
+                       {
+                               $investment[$i]['counter']                      
= $i;
+                               $investment[$i]['location_code']        = 
$this->db->f('location_code');
+                               $investment[$i]['entity_id']            = 
$this->db->f('entity_id');
+                               $investment[$i]['investment_id']        = 
$this->db->f('invest_id');
+                               $investment[$i]['district_id']          = 
$this->db->f('district_id');
+                               $investment[$i]['part_of_town']         = 
$this->db->f('part_of_town');
+                               $investment[$i]['descr']                        
= $this->db->f('descr');
+                               $investment[$i]['initial_value']        = 
$this->db->f('initial_value');
+                               $investment[$i]['value']                        
= $this->db->f('value');
+                               $investment[$i]['this_index']           = 
$this->db->f('this_index');
+                               $investment[$i]['index_count']          = 
$this->db->f('index_count');
+                               $investment[$i]['date']                         
= $this->db->f('index_date');
+                               if ($cat_id=='property')
+                               {
+                                       $investment[$i]['entity_name']  = 
$this->db->f('name');
+                               }
+                               else
+                               {
+                                       $investment[$i]['entity_name']  = 
$this->db->f('entity_name');
+                               }
+
+                               $investment[$i]['this_write_off']       = 
round(($this->db->f('this_index') * $this->db->f('initial_value')),2);
+                               $i++;
+                       }
+
+//_debug_array($investment);
+                       return $investment;
+               }
+
+               function save_investment($values)
+               {
+//_debug_array($values);
+
+                       while (is_array($values['location']) && 
list($input_name,$value) = each($values['location']))
+                       {
+                               if($value)
+                               {
+                                       $cols[] = $input_name;
+                                       $vals[] = $value;
+                               }
+                       }
+
+                       while (is_array($values['extra']) && 
list($input_name,$value) = each($values['extra']))
+                       {
+                               if($value)
+                               {
+                                       $cols[] = $input_name;
+                                       $vals[] = $value;
+                               }
+                       }
+
+                       if($cols)
+                       {
+                               $cols   = "," . implode(",", $cols);
+                               $vals   = ",'" . implode("','", $vals) . "'";
+                       }
+
+                       if($values['street_name'])
+                       {
+                               $address[]= $values['street_name'];
+                               $address[]= $values['street_number'];
+                               $address        = 
$this->db->db_addslashes(implode(" ", $address));
+                       }
+
+                       if(!$address)
+                       {
+                               $address = 
$this->db->db_addslashes($values['location_name']);
+                       }
+
+
+                       $period         = $values['period'];
+                       $type           = $values['funding'];
+                       $initial_value  = $values['initial_value'];
+                       $descr          = 
$this->db->db_addslashes($values['descr']);
+                       $date           = $values['date'];
+                       $location_code  = $values['location_code'];
+                       $entity_id      = $values['entity_id'];
+                       $entity_type    = $values['entity_type'];
+
+
+                       $this->db->query("select max(invest_id) as 
max_invest_id from fm_investment Where entity_id= '$entity_id'");
+                       $this->db->next_record();
+                       $next_invest_id  = $this->db->f('max_invest_id')+1;
+
+                       $this->db->transaction_begin();
+
+                       $this->db->query("insert into fm_investment (entity_id, 
invest_id,entity_type,location_code,writeoff_year, descr $cols) "
+                       . " values ('$entity_id', 
'$next_invest_id','$entity_type','$location_code','$period','$descr' $vals )");
+
+                       $this->db->query("insert into fm_investment_value 
(entity_id, invest_id, index_count, this_index, current_index, 
value,initial_value, index_date) "
+                       . " values ('$entity_id', '$next_invest_id','1', '0', 
'1','$initial_value','$initial_value','$date')");
+
+
+                       if($this->db->transaction_commit())
+                       {
+                               $receipt['message'][]=array('msg' => 
lang('Investment added !'));
+                               $receipt['message'][]=array('msg' => 
lang('Entity ID'). ' ' . $entity_id);
+                               $receipt['message'][]=array('msg' => 
lang('Investment ID:'). ' ' . $next_invest_id);
+                       }
+                       return $receipt;
+               }
+
+               function update_investment($values)
+               {
+                       if (isSet($values) AND is_array($values))
+                       {
+                               $this->db->transaction_begin();
+                               foreach($values as $entry)
+                               {
+                                       $this->db->query("select 
max(index_count) as max_index_count from fm_investment_value Where entity_id='" 
.$entry['entity_id'] . "' and invest_id=" .$entry['invest_id']);
+                                       $this->db->next_record();
+                                       $next_index_count  = 
$this->db->f('max_index_count')+1;
+
+                                       $this->db->query("update 
fm_investment_value set current_index = Null"
+                                       . " WHERE entity_id='" . 
$entry['entity_id'] . "' and invest_id=" . $entry['invest_id']);
+
+                                       $insert= array(
+                                               $entry['entity_id'],
+                                               $entry['invest_id'],
+                                               $next_index_count,
+                                               $entry['new_index'],
+                                               $entry['new_value'],
+                                               $entry['initial_value'],
+                                               $entry['date'],
+                                               1
+                                               );
+
+                                       $insert = 
$this->bocommon->validate_db_insert($insert);
+
+
+                                       $this->db->query("insert into 
fm_investment_value (entity_id, invest_id, index_count, this_index, 
value,initial_value, index_date,current_index) "
+                                       . " values ($insert)");
+                               }
+                               $this->db->transaction_commit();
+                       }
+               }
+
+               function read_single($entity_id,$investment_id,$start,$allrows)
+               {
+                       if (!$start)
+                       {
+                               $start=0;
+                       }
+
+
+                       $sql = "SELECT index_count, 
this_index,current_index,value, initial_value, index_date  "
+                       . " FROM fm_investment_value Where entity_id= 
'$entity_id' and invest_id= '$investment_id' order by index_count";
+
+                       $this->db2->query($sql,__LINE__,__FILE__);
+                       $this->total_records = $this->db2->num_rows();
+
+                       if(!$allrows)
+                       {
+                               
$this->db->limit_query($sql,$start,__LINE__,__FILE__);
+                       }
+                       else
+                       {
+                               $this->db->query($sql,__LINE__,__FILE__);
+                       }
+
+                       $i=0;
+                       while ($this->db->next_record())
+                       {
+                               $investment[$i]['descr']                        
= $this->db->f('descr');
+                               $investment[$i]['initial_value']        = 
$this->db->f('initial_value');
+                               $investment[$i]['value']                        
= $this->db->f('value');
+                               $investment[$i]['this_index']           = 
$this->db->f('this_index');
+                               $investment[$i]['current_index']        = 
$this->db->f('current_index');
+                               $investment[$i]['index_count']          = 
$this->db->f('index_count');
+                               $investment[$i]['date']                         
= $this->db->f('index_date');
+                               $investment[$i]['this_write_off']       = 
round(($this->db->f('this_index') * $this->db->f('initial_value')),2);
+                               $i++;
+                       }
+
+                       return $investment;
+               }
+
+
+               function write_off_period_list()
+               {
+                       $this->db->query("SELECT writeoff_year FROM 
fm_investment GROUP BY writeoff_year ",__LINE__,__FILE__);
+
+                       while ($this->db->next_record())
+                       {
+                               $period_list[] = Array(
+                                       'period'        => 
$this->db->f('writeoff_year')
+                               );
+                       }
+
+                       return $period_list;
+
+               }
+
+               function delete($entity_id,$invest_id,$index_count)
+               {
+                       $this->db->transaction_begin();
+                       if ($index_count==1)
+                       {
+                               $this->db->query("update fm_investment_value 
set current_index = '0', this_index=Null, 
value=Null,initial_value=Null,index_date=Null  where entity_id='$entity_id' and 
invest_id= '$invest_id' and index_count= '1'");
+                       }
+                       else
+                       {
+                               $this->db->query("delete from 
fm_investment_value where entity_id='$entity_id' and invest_id= '$invest_id' 
and index_count= '$index_count'");
+
+                               $new_current= $index_count -1;
+
+                               $this->db->query("update fm_investment_value 
set current_index = '1' where entity_id='$entity_id' and invest_id= 
'$invest_id' and index_count= '$new_current'");
+                       }
+
+       //              $this->db->query("DELETE FROM fm_investment_value WHERE 
entity_id= '$entity_id'  and invest_id='$invest_id' and 
index_count='$index_count'",__LINE__,__FILE__);
+                       $this->db->transaction_commit();
+               }
+       }
+?>
Index: property/inc/class.soinvoice.inc.php
diff -u property/inc/class.soinvoice.inc.php:1.18 
property/inc/class.soinvoice.inc.php:1.19
--- property/inc/class.soinvoice.inc.php:1.18   Fri Jan 27 14:05:43 2006
+++ property/inc/class.soinvoice.inc.php        Mon Jan 30 22:14:19 2006
@@ -1,1146 +1,1146 @@
-<?php
-       /**
-       * phpGroupWare - property: a Facilities Management System.
-       *
-       * @author Sigurd Nes <address@hidden>
-       * @copyright Copyright (C) 2003-2005 Free Software Foundation, Inc. 
http://www.fsf.org/
-       * @license http://www.gnu.org/licenses/gpl.html GNU General Public 
License
-       * @internal Development of this application was funded by 
http://www.bergen.kommune.no/bbb_/ekstern/
-       * @package property
-       * @subpackage eco
-       * @version $Id: class.soinvoice.inc.php,v 1.18 2006/01/27 14:05:43 
sigurdne Exp $
-       */
-
-       /**
-        * Description
-        * @package property
-        */
-
-       class soinvoice
-       {
-
-               function soinvoice()
-               {
-                       $this->currentapp       = 
$GLOBALS['phpgw_info']['flags']['currentapp'];
-                       $this->bocommon         = 
CreateObject($this->currentapp.'.bocommon');
+<?php
+       /**
+       * phpGroupWare - property: a Facilities Management System.
+       *
+       * @author Sigurd Nes <address@hidden>
+       * @copyright Copyright (C) 2003-2005 Free Software Foundation, Inc. 
http://www.fsf.org/
+       * @license http://www.gnu.org/licenses/gpl.html GNU General Public 
License
+       * @internal Development of this application was funded by 
http://www.bergen.kommune.no/bbb_/ekstern/
+       * @package property
+       * @subpackage eco
+       * @version $Id: class.soinvoice.inc.php,v 1.19 2006/01/30 22:14:19 
sigurdne Exp $
+       */
+
+       /**
+        * Description
+        * @package property
+        */
+
+       class soinvoice
+       {
+
+               function soinvoice()
+               {
+                       $this->currentapp       = 
$GLOBALS['phpgw_info']['flags']['currentapp'];
+                       $this->bocommon         = 
CreateObject($this->currentapp.'.bocommon');
                        $this->db               = $this->bocommon->new_db();
                        $this->db2              = $this->bocommon->new_db();
-                       $this->account_id       = 
$GLOBALS['phpgw_info']['user']['account_id'];
-                       $this->acl2             = 
CreateObject($this->currentapp.'.acl2','##DEFAULT##','##DEFAULT##');
-
-                       $this->join                     = $this->bocommon->join;
-                       $this->left_join        = $this->bocommon->left_join;
-                       $this->like                     = $this->bocommon->like;
-               }
-
-
-               function read_invoice($data)
-               {
-                       if(is_array($data))
-                       {
-                               if ($data['start'])
-                               {
-                                       $start=$data['start'];
-                               }
-                               else
-                               {
-                                       $start=0;
-                               }
-                               $query                  = 
(isset($data['query'])?$data['query']:'');
-                               $sort                   = 
(isset($data['sort'])?$data['sort']:'DESC');
-                               $order                  = 
(isset($data['order'])?$data['order']:'');
-                               $cat_id                 = 
(isset($data['cat_id'])?$data['cat_id']:0);
-                               $user_lid               = 
(isset($data['user_lid'])?$data['user_lid']:'none');
-                               $paid                   = 
(isset($data['paid'])?$data['paid']:'');
-                               $start_date     = 
(isset($data['start_date'])?$data['start_date']:'');
-                               $end_date               = 
(isset($data['end_date'])?$data['end_date']:'');
-                               $vendor_id              = 
(isset($data['vendor_id'])?$data['vendor_id']:'');
-                               $loc1                   = 
(isset($data['loc1'])?$data['loc1']:'');
-                               $workorder_id   = 
(isset($data['workorder_id'])?$data['workorder_id']:'');
-                               $allrows                = 
(isset($data['allrows'])?$data['allrows']:'');
-                               $voucher_id             = 
(isset($data['voucher_id'])?$data['voucher_id']:'');
-                       }
-
-//_debug_array($data);
-
-                       if ($order)
-                       {
-                               $ordermethod = " order by $order $sort";
-                       }
-                       else
-                       {
-                               $ordermethod = ' order by bilagsnr DESC';
-                       }
-
-                       $where= 'WHERE';
-
-                       if ($user_lid=='none' || !$user_lid):
-                       {
-                               return;
-                       }
-                       elseif ($user_lid!='all'):
-                       {
-                               $filtermethod = " WHERE ( oppsynsmannid= 
'$user_lid' or saksbehandlerid= '$user_lid' or budsjettansvarligid= 
'$user_lid')";
-                               $where= 'AND';
-                       }
-                       endif;
-
-                       if ($cat_id > 0)
-                       {
-                               $filtermethod .= " $where typeid='$cat_id' ";
-                               $where= 'AND';
-                       }
-
-                       if ($district_id > 0)
-                       {
-                               $filtermethod .= " $where  
district_id='$district_id' ";
-                               $where= 'AND';
-                       }
-
-                       if ($vendor_id)
-                       {
-                               $filtermethod .= " $where  spvend_code 
='$vendor_id' ";
-                               $where= 'AND';
-                       }
-                       if ($loc1)
-                       {
-                               $filtermethod .= " $where  dima $this->like 
'%$loc1%' ";
-                               $where= 'AND';
-                       }
-                       if ($workorder_id)
-                       {
-                               $filtermethod .= " $where  pmwrkord_code 
='$workorder_id' ";
-                               $where= 'AND';
-                       }
-                       if ($voucher_id)
-                       {
-                               $filtermethod .= " $where  bilagsnr $this->like 
'%$voucher_id%' ";
-                               $where= 'AND';
-                       }
-
-
-                       if ($paid)
-                       {
-                               $table = 'fm_ecobilagoverf';
-                               $dateformat = 
strtolower($GLOBALS['phpgw_info']['user']['preferences']['common']['dateformat']);
-                               $dateformat = str_replace(".","",$dateformat);
-                               $dateformat = str_replace("-","",$dateformat);
-                               $dateformat = str_replace("/","",$dateformat);
-                               $y=strpos($dateformat,'y');
-                               $d=strpos($dateformat,'d');
-                               $m=strpos($dateformat,'m');
-
-                               $dateparts = explode('/', $start_date);
-                               $sday = $dateparts[$d];
-                               $smonth = $dateparts[$m];
-                               $syear = $dateparts[$y];
-
-                               $dateparts = explode('/', $end_date);
-                               $eday = $dateparts[$d];
-                               $emonth = $dateparts[$m];
-                               $eyear = $dateparts[$y];
-
-                               $start_date = 
date($this->bocommon->dateformat,mktime(2,0,0,$smonth,$sday,$syear));
-                               $end_date = 
date($this->bocommon->dateformat,mktime(2,0,0,$emonth,$eday,$eyear));
-
-                               if (!$workorder_id && !$voucher_id)
-                               {
-                                       $filtermethod .= " $where  overftid 
>'$start_date' and overftid < '$end_date'";
-                               }
-                       }
-                       else
-                       {
-                               $table ='fm_ecobilag';
-                       }
-
-
-                       if($query)
-                       {
-                               $query = ereg_replace("'",'',$query);
-                               $query = ereg_replace('"','',$query);
-
-                               $querymethod = " $where ( spvend_code 
$this->like '%$query%' OR bilagsnr $this->like '%$query%' )";
-                       }
-
-
-                       $sql = "SELECT DISTINCT bilagsnr, count(bilagsnr) as 
invoice_count, sum(belop) as belop,spvend_code,fakturadato FROM  $table  
$filtermethod $querymethod group by bilagsnr,spvend_code,fakturadato ";
-                       $sql2 = "SELECT DISTINCT bilagsnr FROM  $table  
$filtermethod $querymethod";
-
-//echo $sql;
-                       $this->db2->query($sql2,__LINE__,__FILE__);
-                       $this->total_records = $this->db2->num_rows();
-
-                       if(!$allrows)
-                       {
-                               $this->db->limit_query($sql . 
$ordermethod,$start,__LINE__,__FILE__);
-                       }
-                       else
-                       {
-                               $this->db->query($sql . 
$ordermethod,__LINE__,__FILE__);
-                       }
-
-                       while ($this->db->next_record())
-                       {
-                               $temp[] = array
-                               (
-                                       'voucher_id'            => 
$this->db->f('bilagsnr'),
-                                       'invoice_count'         => 
$this->db->f('invoice_count'),
-                                       'amount'                        => 
$this->db->f('belop')
-                                       );
-                       }
-
-                       if ($temp)
-                       {
-                               $role= $this->check_role();
-                               $i = 0;
-                               foreach($temp as $invoice_temp)
-                               {
-                                       $voucher_id=$invoice_temp['voucher_id'];
-
-                                       $sql = "SELECT 
spvend_code,oppsynsmannid,saksbehandlerid,budsjettansvarligid,"
-                                       . " 
utbetalingid,oppsynsigndato,saksigndato,budsjettsigndato,utbetalingsigndato,fakturadato,org_name,"
-                                       . " 
forfallsdato,periode,artid,kidnr,kreditnota "
-                                       . " from $table $this->join fm_vendor 
ON fm_vendor.id = $table.spvend_code WHERE bilagsnr = $voucher_id "
-                                       . " group by 
bilagsnr,spvend_code,oppsynsmannid,saksbehandlerid,budsjettansvarligid,"
-                                       . " 
utbetalingid,oppsynsigndato,saksigndato,budsjettsigndato,utbetalingsigndato,fakturadato,org_name,"
-                                       . " 
forfallsdato,periode,artid,kidnr,kreditnota ";
-
-                                       
$this->db->query($sql,__LINE__,__FILE__);
-
-                                       $this->db->next_record();
-
-                                       $timestamp_voucher_date= 
mktime(0,0,0,date(m,strtotime($this->db->f('fakturadato'))),date(d,strtotime($this->db->f('fakturadato'))),date(y,strtotime($this->db->f('fakturadato'))));
-                                       $timestamp_payment_date= 
mktime(0,0,0,date(m,strtotime($this->db->f('forfallsdato'))),date(d,strtotime($this->db->f('forfallsdato'))),date(y,strtotime($this->db->f('forfallsdato'))));
-
-                                       if($this->db->f('oppsynsmannid') && 
$this->db->f('oppsynsigndato'))
-                                       {
-//                                             $timestamp_jan_date             
        = 
mktime(0,0,0,date(m,strtotime($this->db->f('oppsynsigndato'))),date(d,strtotime($this->db->f('oppsynsigndato'))),date(y,strtotime($this->db->f('oppsynsigndato'))));
-//                                             $invoice[$i]['jan_date']        
= 
$GLOBALS['phpgw']->common->show_date($timestamp_jan_date,$GLOBALS['phpgw_info']['user']['preferences']['common']['dateformat']);
-                                               
$invoice[$i]['jan_date']=date($GLOBALS['phpgw_info']['user']['preferences']['common']['dateformat'],strtotime($this->db->f('oppsynsigndato')));
-                                       }
-                                       else
-                                       {
-                                               $invoice[$i]['jan_date']        
='';
-                                       }
-                                       if($this->db->f('saksbehandlerid') && 
$this->db->f('saksigndato'))
-                                       {
-//                                             $timestamp_super_date           
= 
mktime(0,0,0,date(m,strtotime($this->db->f('saksigndato'))),date(d,strtotime($this->db->f('saksigndato'))),date(y,strtotime($this->db->f('saksigndato'))));
-//                                             $invoice[$i]['super_date']      
= 
$GLOBALS['phpgw']->common->show_date($timestamp_super_date,$GLOBALS['phpgw_info']['user']['preferences']['common']['dateformat']);
-                                               
$invoice[$i]['super_date']=date($GLOBALS['phpgw_info']['user']['preferences']['common']['dateformat'],strtotime($this->db->f('saksigndato')));
-                                       }
-                                       else
-                                       {
-                                               $invoice[$i]['super_date']      
='';
-                                       }
-
-                                       if($this->db->f('budsjettansvarligid') 
&& $this->db->f('budsjettsigndato'))
-                                       {
-//                                                     $timestamp_budget_date  
        = 
mktime(0,0,0,date(m,strtotime($this->db->f('budsjettsigndato'))),date(d,strtotime($this->db->f('budsjettsigndato'))),date(y,strtotime($this->db->f('budsjettsigndato'))));
-//                                                     
$invoice[$i]['budget_date']     = 
$GLOBALS['phpgw']->common->show_date($timestamp_budget_date,$GLOBALS['phpgw_info']['user']['preferences']['common']['dateformat']);
-                                                       
$invoice[$i]['budget_date']=date($GLOBALS['phpgw_info']['user']['preferences']['common']['dateformat'],strtotime($this->db->f('budsjettsigndato')));
-                                       }
-                                       else
-                                       {
-                                               $invoice[$i]['budget_date']     
='';
-                                       }
-
-                                       if($this->db->f('utbetalingid') && 
$this->db->f('utbetalingsigndato'))
-                                       {
-//                                             $timestamp_transfer_date= 
mktime(0,0,0,date(m,strtotime($this->db->f('utbetalingsigndato'))),date(d,strtotime($this->db->f('utbetalingsigndato'))),date(y,strtotime($this->db->f('utbetalingsigndato'))));
-//                                             $invoice[$i]['transfer_date']   
                = 
$GLOBALS['phpgw']->common->show_date($timestamp_transfer_date,$GLOBALS['phpgw_info']['user']['preferences']['common']['dateformat']);
-                                               
$invoice[$i]['transfer_date']=date($GLOBALS['phpgw_info']['user']['preferences']['common']['dateformat'],strtotime($this->db->f('utbetalingsigndato')));
-                                       }
-                                       else
-                                       {
-                                               $invoice[$i]['transfer_date']   
='';
-                                       }
-
-                                       $invoice[$i]['counter']                 
                = $i;
-                                       $invoice[$i]['current_user']            
        = $GLOBALS['phpgw_info']['user']['account_lid'];
-                                       $invoice[$i]['voucher_id']              
                = $voucher_id;
-                                       $invoice[$i]['invoice_count']           
        = $invoice_temp['invoice_count'];
-                                       $invoice[$i]['vendor_id']               
                = $this->db->f('spvend_code');
-                                       $invoice[$i]['vendor']                  
                = $this->db->f('org_name');
-                                       $invoice[$i]['is_janitor']              
                = $role['is_janitor'];
-                                       $invoice[$i]['is_supervisor']           
        = $role['is_supervisor'];
-                                       $invoice[$i]['is_budget_responsible']   
= $role['is_budget_responsible'];
-                                       $invoice[$i]['is_janitor']              
                = $role['is_janitor'];
-                                       $invoice[$i]['is_transfer']             
                = $role['is_transfer'];
-                                       $invoice[$i]['janitor']                 
                = $this->db->f('oppsynsmannid');
-                                       $invoice[$i]['supervisor']              
                = $this->db->f('saksbehandlerid');
-                                       $invoice[$i]['budget_responsible']      
        = $this->db->f('budsjettansvarligid');
-                                       $invoice[$i]['transfer_id']             
                = $this->db->f('utbetalingid');
-                                       $invoice[$i]['voucher_date']            
        = 
$GLOBALS['phpgw']->common->show_date($timestamp_voucher_date,$GLOBALS['phpgw_info']['user']['preferences']['common']['dateformat']);
-                                       $invoice[$i]['payment_date']            
        = 
$GLOBALS['phpgw']->common->show_date($timestamp_payment_date,$GLOBALS['phpgw_info']['user']['preferences']['common']['dateformat']);
-                                       $invoice[$i]['period']                  
                = $this->db->f('periode');
-                                       $invoice[$i]['type']                    
                = $this->db->f('artid');
-                                       $invoice[$i]['kidnr']                   
                = $this->db->f('kidnr');
-                                       $invoice[$i]['kreditnota']              
                = $this->db->f('kreditnota');
-                                       $invoice[$i]['amount']                  
                = $invoice_temp['amount'];
-                                       $invoice[$i]['num_days']                
                = 
intval(($timestamp_payment_date-$timestamp_voucher_date)/(24*3600));
-                                       $invoice[$i]['timestamp_voucher_date']  
= $timestamp_voucher_date;
-
-                                       
if($invoice[$i]['current_user']==$invoice[$i]['janitor'] && 
$invoice[$i]['jan_date']):
-                                       {
-                                               
$invoice[$i]['sign_orig']='sign_janitor';
-                                       }
-                                       
elseif($invoice[$i]['current_user']==$invoice[$i]['supervisor'] && 
$invoice[$i]['super_date']):
-                                       {
-                                               
$invoice[$i]['sign_orig']='sign_supervisor';
-                                       }
-                                       
elseif($invoice[$i]['current_user']==$invoice[$i]['budget_responsible'] && 
$invoice[$i]['budget_date']):
-                                       {
-                                               
$invoice[$i]['sign_orig']='sign_budget_responsible';
-                                       }
-                                       endif;
-
-                                       $i++;
-
-                               }
-                       }
-//                     $end_time = explode(' ',microtime());
-//                     $this->end_time = $end_time;
-
-//_debug_array($invoice);
-//_debug_array($invoice_temp);
-
-                       return $invoice;
-
-               }
-
-
-               function read_invoice_sub($data)
-               {
-                       if(is_array($data))
-                       {
-                               if ($data['start'])
-                               {
-                                       $start=$data['start'];
-                               }
-                               else
-                               {
-                                       $start=0;
-                               }
-                               $filter = 
(isset($data['filter'])?$data['filter']:'none');
-                               $sort = 
(isset($data['sort'])?$data['sort']:'DESC');
-                               $order = 
(isset($data['order'])?$data['order']:'');
-                               $voucher_id = 
(isset($data['voucher_id'])?$data['voucher_id']:0);
-                               $paid = (isset($data['paid'])?$data['paid']:'');
-                       }
-
-                       if ($paid)
-                       {
-                               $table = 'fm_ecobilagoverf';
-                       }
-                       else
-                       {
-                               $table ='fm_ecobilag';
-                       }
-
-
-                       if ($order)
-                       {
-                               $ordermethod = " order by $order $sort";
-                       }
-                       else
-                       {
-                               $ordermethod = ' order by bilagsnr DESC';
-                       }
-
-                       if ($voucher_id)
-                       {
-                               $filtermethod = " WHERE ( bilagsnr= 
'$voucher_id')";
-                       }
-
-                       $sql = "SELECT 
$table.*,fm_workorder.status,fm_workorder.charge_tenant,org_name,fm_workorder.claim_issued
 FROM $table "
-                       . " $this->left_join fm_workorder on fm_workorder.id = 
$table.pmwrkord_code  "
-                       . " $this->join fm_vendor  on $table.spvend_code = 
fm_vendor.id  $filtermethod ";
-
-                       $this->db2->query($sql,__LINE__,__FILE__);
-                       $this->total_records = $this->db2->num_rows();
-                       $this->db->query($sql . 
$ordermethod,$start,__LINE__,__FILE__);
-
-                       $i = 0;
-
-                       while ($this->db->next_record())
-                       {
-                               $invoice[$i]['counter']                         
        = $i;
-                               $invoice[$i]['claim_issued']                    
= $this->db->f('claim_issued');
-                               $invoice[$i]['project_id']                      
        = $this->db->f('project_id');
-                               $invoice[$i]['workorder_id']                    
= $this->db->f('pmwrkord_code');
-                               $invoice[$i]['status']                          
        = $this->db->f('status');
-                               if ($this->db->f('status')=='closed')
-                               {
-                                       $invoice[$i]['closed']                  
        = True;
-                               }
-                               $invoice[$i]['voucher_id']                      
        = $voucher_id;
-                               $invoice[$i]['id']                              
                = $this->db->f('id');
-                               $invoice[$i]['invoice_id']                      
        = $this->db->f('fakturanr');
-                               $invoice[$i]['budget_account']                  
= $this->db->f('spbudact_code');
-                               $invoice[$i]['dima']                            
        = $this->db->f('dima');
-                               $invoice[$i]['dimb']                            
        = $this->db->f('dimb');
-                               $invoice[$i]['dimd']                            
        = $this->db->f('dimd');
-                               if ($this->db->f('merknad'))
-                               {
-                                       $invoice[$i]['remark']                  
        = True;
-                               }
-                               $invoice[$i]['tax_code']                        
        = $this->db->f('mvakode');
-                               $invoice[$i]['amount']                          
        = $this->db->f('belop');
-                               $invoice[$i]['charge_tenant']                   
= $this->db->f('charge_tenant');
-                               $invoice[$i]['vendor']                          
        = $this->db->f('org_name');
-                               $i++;
-
-                       }
-
-                       return $invoice;
-
-               }
-
-
-               function read_consume($data)
-               {
-                       if(is_array($data))
-                       {
-                               if ($data['start'])
-                               {
-                                       $start=$data['start'];
-                               }
-                               else
-                               {
-                                       $start=0;
-                               }
-                               $filter                 = 
(isset($data['filter'])?$data['filter']:'none');
-                               $query                  = 
(isset($data['query'])?$data['query']:'');
-                               $sort                   = 
(isset($data['sort'])?$data['sort']:'DESC');
-                               $order                  = 
(isset($data['order'])?$data['order']:'');
-                               $cat_id                 = 
(isset($data['cat_id'])?$data['cat_id']:0);
-                               $start_date     = 
(isset($data['start_date'])?$data['start_date']:'');
-                               $end_date               = 
(isset($data['end_date'])?$data['end_date']:'');
-                               $vendor_id              = 
(isset($data['vendor_id'])?$data['vendor_id']:'');
-                               $loc1   = 
(isset($data['loc1'])?$data['loc1']:'');
-                               $district_id    = 
(isset($data['district_id'])?$data['district_id']:'');
-                               $workorder_id   = 
(isset($data['workorder_id'])?$data['workorder_id']:0);
-                               $b_account_class = 
(isset($data['b_account_class'])?$data['b_account_class']:'');
-                       }
-//_debug_array($data);
-
-                       $dateformat = 
strtolower($GLOBALS['phpgw_info']['user']['preferences']['common']['dateformat']);
-                       $dateformat = str_replace(".","",$dateformat);
-                       $dateformat = str_replace("-","",$dateformat);
-                       $dateformat = str_replace("/","",$dateformat);
-                       $y=strpos($dateformat,'y');
-                       $d=strpos($dateformat,'d');
-                       $m=strpos($dateformat,'m');
-                       $dateparts = explode('/', $start_date);
-                       $sday = $dateparts[$d];
-                       $smonth = $dateparts[$m];
-                       $syear = $dateparts[$y];
-
-                       $dateparts = explode('/', $end_date);
-                       $eday = $dateparts[$d];
-                       $emonth = $dateparts[$m];
-                       $eyear = $dateparts[$y];
-
-                       $start_date = 
date($this->bocommon->dateformat,mktime(2,0,0,$smonth,$sday,$syear));
-                       $end_date = 
date($this->bocommon->dateformat,mktime(2,0,0,$emonth,$eday,$eyear));
-
-
-                       $where = 'WHERE';
-
-                       if($b_account_class)
-                       {
-                               $filtermethod= " $where 
fm_b_account.grouping='$b_account_class'";
-                               $where= 'AND';
-                       }
-                       else
-                       {
-                               $select_account_class=',fm_b_account.grouping 
as b_account_class';
-                               $group_account_class=', 
spbudact_code,fm_b_account.grouping';
-                       }
-
-                       if ($vendor_id)
-                       {
-                               $filtermethod .= " $where (spvend_code = 
$vendor_id)";
-                               $where= 'AND';
-                       }
-
-                       if($loc1)
-                       {
-                               $filtermethod .=" $where (dima $this->like 
'%$loc1%')";
-                               $where= 'AND';
-                       }
-
-
-                       if ($district_id)
-                       {
-                               $filtermethod.= " $where district_id= 
$district_id ";
-                               $where= 'AND';
-                       }
-
-                       if ($workorder_id)
-                       {
-                               $filtermethod.= " $where pmwrkord_code = 
$workorder_id";
-                               $where= 'AND';
-                       }
-
-
-                       if ($cat_id>0)
-                       {
-                               $filtermethod .= " $where typeid = $cat_id";
-                               $where= 'AND';
-                       }
-
-                       $filtermethod .= " $where (fakturadato >'$start_date') 
AND (fakturadato < '$end_date')";
-
-                       $sql = "SELECT district_id,periode,sum(godkjentbelop) 
as consume $select_account_class "
-                               . " FROM  fm_ecobilagoverf $this->join 
fm_location1 ON (fm_ecobilagoverf.loc1 = fm_location1.loc1) "
-                               . " $this->join fm_part_of_town ON 
(fm_location1.part_of_town_id = fm_part_of_town.part_of_town_id) "
-                               . " $this->join fm_b_account ON 
(fm_ecobilagoverf.spbudact_code = fm_b_account.id) "
-                       . " $filtermethod group by district_id,periode 
$group_account_class";
-//echo $sql;
-
-                       $this->db->query($sql,__LINE__,__FILE__);
-                       $this->total_records = $this->db->num_rows();
-                       $this->db->query($sql . 
$ordermethod,$start,__LINE__,__FILE__);
-
-                       $i = 0;
-
-                       while ($this->db->next_record())
-                       {
-                               $consume[$i]['consume']                         
        = $this->db->f('consume');
-                               $consume[$i]['period']                          
        = $this->db->f('periode');
-                               $consume[$i]['district_id']                     
        = $this->db->f('district_id');
-                               if(!$b_account_class)
-                               {
-                                       $consume[$i]['account_class']           
        = $this->db->f('b_account_class');
-                               }
-                               else
-                               {
-                                       $consume[$i]['account_class']           
        = $b_account_class;
-                               }
-
-                               $i++;
-                       }
-//_debug_array($consume);
-                       return $consume;
-               }
-
-               function check_for_updates($values)
-               {
-                       $update=False;
-
-                       if($values['sign_orig']!=$values['sign'])
-                       {
-                               $update=True;
-                               return $update;
-                       }
-
-                       $sql = "SELECT * FROM fm_ecobilag WHERE bilagsnr=" . 
$values['voucher_id'];
-                       $this->db->limit_query($sql,0,__LINE__,__FILE__,1);
-
-                       $this->db->next_record();
-
-                       if( ($this->db->f('utbetalingsigndato') && 
!$values['transfer']) || (!$this->db->f('utbetalingsigndato') && 
$values['transfer']))
-                       {
-                               $update=True;
-                               return $update;
-                       }
-
-                       if( ($this->db->f('kreditnota') && 
!$values['kreditnota']) || (!$this->db->f('kreditnota') && 
$values['kreditnota']) )
-                       {
-                               $update=True;
-                               return $update;
-                       }
-
-                       $timestamp_voucher_date= 
mktime(0,0,0,date(m,strtotime($this->db->f('fakturadato'))),date(d,strtotime($this->db->f('fakturadato'))),date(y,strtotime($this->db->f('fakturadato'))));
-                       $timestamp_payment_date= 
mktime(0,0,0,date(m,strtotime($this->db->f('forfallsdato'))),date(d,strtotime($this->db->f('forfallsdato'))),date(y,strtotime($this->db->f('forfallsdato'))));
-
-                       if( 
((intval(($timestamp_payment_date-$timestamp_voucher_date)/(24*3600)))!=$values['num_days'])
 )
-                       {
-                               $update=True;
-                               return $update;
-                       }
-               }
-
-               function update_invoice_sub($values)
-               {
-
-                       $GLOBALS['phpgw']->db->transaction_begin();
-
-                       while($entry=each($values['counter']))
-                               {
-                                       $local_error='';
-
-                                       $n=$entry[0];
-
-
-//_debug_array($entry);
-
-
-                               if ($values['budget_account'][$n])
-                               {
-                                       
$budget_account=$values['budget_account'][$n];
-
-                                       $GLOBALS['phpgw']->db->query("select 
count(*) from fm_b_account  where id =$budget_account");
-                                       $GLOBALS['phpgw']->db->next_record();
-                                       if ($GLOBALS['phpgw']->db->f(0) == 0)
-                                       {
-                                               $receipt['error'][] = 
array('msg'=> lang('This account is not valid:'). " ".$budget_account);
-                                               $local_error= true;
-                                       }
-                               }
-                               else
-                               {
-                                               $receipt['error'][] = 
array('msg'=>lang('Budget account is missing:'));
-                                               $local_error= true;
-                               }
-
-                               if(!$values['dimd'][$n])
-                               {
-                                       $dimd_field="dimd=NULL";
-                               }
-                               else
-                               {
-                                       $dimd=$values['dimd'][$n];
-                                       $GLOBALS['phpgw']->db->query("select 
count(*) from  fm_dim_d where id =$dimd");
-                                       $GLOBALS['phpgw']->db->next_record();
-                                       if ($GLOBALS['phpgw']->db->f(0) == 0)
-                                       {
-                                               $receipt['error'][] = 
array('msg'=>lang('This Dim D is not valid:'). " ".$dimd);
-                                               $local_error= true;
-                                       }
-
-                                       $dimd_field="dimd="."'" . $dimd . "'";
-                               }
-
-                               if (!$values['dima'][$n])
-                               {
-                                       $dima_field="dima=NULL";
-                                       $receipt['error'][] = 
array('msg'=>lang('Dim A is missing'));
-                                       $local_error= true;
-                               }
-                               else
-                               {
-                                       
$dima_check=substr($values['dima'][$n],0,4);
-                                       $GLOBALS['phpgw']->db->query("select 
loc1, kostra_id from fm_location1 where loc1 = '$dima_check' ");
-                                       $GLOBALS['phpgw']->db->next_record();
-                                       if (!$GLOBALS['phpgw']->db->f('loc1'))
-                                       {
-                                               $receipt['error'][] = 
array('msg'=>lang('This Dim A is not valid:'). " ".$values['dima'][$n]);
-                                               $local_error= true;
-                                       }
-
-                                       if 
(!$GLOBALS['phpgw']->db->f('kostra_id') || 
$GLOBALS['phpgw']->db->f('kostra_id') == 0)
-                                       {
-                                               $receipt['error'][] = 
array('msg'=>'objektet mangler tjeneste - utgått? '. " ".$values['dima'][$n]);
-                                               $local_error= true;
-                                       }
-
-                               //      $dima_field="dima="."'" . 
$values['dima'][$n] . "'";
-                                       $dima_field="dima="."'" . 
$values['dima'][$n] . "',loc1=" . "'" . substr($values['dima'][$n],0,4) . "'";
-
-                                       $kostra_field="kostra_id="."'" . 
$GLOBALS['phpgw']->db->f('kostra_id') . "'";
-
-                               }
-
-                               if (! $local_error)
-                               {
-                                       $id                     = 
$values['id'][$n];
-                                       $tax_code       = 
$values['tax_code'][$n];
-                                       
$workorder_id=$values['workorder_id'][$n];
-                                       if($values['close_order'][$n] && 
!$values['close_order_orig'][$n])
-                                       {
-                                               
$update_status[$workorder_id]='X';
-                                       }
-
-                                       if(!$values['close_order'][$n] && 
$values['close_order_orig'][$n])
-                                       {
-                                               
$update_status[$workorder_id]='R';
-                                       }
-
-                                       $GLOBALS['phpgw']->db->query("UPDATE 
fm_ecobilag set $dima_field ,$kostra_field,$dimd_field, mvakode = 
'$tax_code',spbudact_code = '$budget_account' where id='$id'");
-
-                                       $receipt['message'][] = 
array('msg'=>lang('Voucher is updated '));
-                               }
-
-                       }
-
-                       if (isset($update_status) AND is_array($update_status))
-                       {
-                               
$status_code=array('X'=>'closed','R'=>'re_opened');
-
-                               $historylog_workorder   = 
CreateObject($this->currentapp.'.historylog','workorder');
-
-                               while (list($id,$entry) = each($update_status))
-                               {
-                                       
$historylog_workorder->add($entry,$id,$status_code[$entry]);
-                                       $GLOBALS['phpgw']->db->query("UPDATE 
fm_workorder set status=" . "'$status_code[$entry]'" . "where id=$id");
-                                       $receipt['message'][] = 
array('msg'=>lang('Workorder %1 is %2',$id, $status_code[$entry]));
-                               }
-                       }
-
-                       $GLOBALS['phpgw']->db->transaction_commit();
-
-                       return $receipt;
-               }
-
-               function read_remark($id='',$paid='')
-               {
-                       if ($paid)
-                       {
-                               $table = 'fm_ecobilagoverf';
-                       }
-                       else
-                       {
-                               $table ='fm_ecobilag';
-                       }
-
-                       $this->db->query(" SELECT merknad from $table  where 
id= '$id'");
-                       $this->db->next_record();
-
-                       return $this->db->f('merknad');
-               }
-
-               function check_role()
-               {
-                       if(!$this->role)
-                       {
-                               $this->role=array(
-                                       'is_janitor'                            
=> $this->acl2->check('.invoice',32),
-                                       'is_supervisor'                         
=> $this->acl2->check('.invoice',64),
-                                       'is_budget_responsible'         => 
$this->acl2->check('.invoice',128),
-                                       'is_transfer'                           
=> $this->acl2->check('.invoice',16)
-                                       );
-                       }
-                       return $this->role;
-               }
-
-               function check_count($voucher_id)
-               {
-
-                       $this->db->query("select count(dima) as dima_count , 
count(spbudact_code) as spbudact_code_count from fm_ecobilag where bilagsnr 
='$voucher_id'");
-                       $this->db->next_record();
-
-                       $check_count=array(
-                               'dima_count'                            => 
$this->db->f('dima_count'),
-                               'spbudact_code_count'           => 
$this->db->f('spbudact_code_count')
-                               );
-
-                       $this->db->query("select count(kostra_id) as 
kostra_count  from fm_ecobilag where bilagsnr ='$voucher_id' and kostra_id > 
0");
-                       $this->db->next_record();
-                       $check_count['kostra_count'] = 
$this->db->f('kostra_count');
-
-                       return $check_count;
-               }
-
-
-               function update_period($voucher_id='',$period='')
-               {
-                       $this->db->transaction_begin();
-
-                       $this->db->query("UPDATE fm_ecobilag set 
periode='$period' where bilagsnr='$voucher_id'");
-
-                       $this->db->transaction_commit();
-
-                       $receipt['message'][] = array('msg'=>lang('voucher 
period is updated'));
-                       return $receipt;
-               }
-
-
-               function increment_bilagsnr()
-               {
-
-                       $this->db->query("UPDATE fm_idgenerator set value = 
value + 1 where name = 'Bilagsnummer'");
-                       $this->db->query("select value from fm_idgenerator 
where name = 'Bilagsnummer'");
-                       $this->db->next_record();
-                       $bilagsnr = $this->db->f('value');
-                       return $bilagsnr;
-
-               }
-
-               function next_bilagsnr()
-               {
-
-                       $this->db->query("select value from fm_idgenerator 
where name = 'Bilagsnummer'");
-                       $this->db->next_record();
-                       $bilagsnr = $this->db->f('value')+1;
-
-                       return $bilagsnr;
-               }
-
-               function check_vendor($vendor_id)
-               {
-
-                       $this->db->query("select count(*) from fm_vendor where 
id='$vendor_id'");
-                       $this->db->next_record();
-                       return $this->db->f(0);
-               }
-
-
-               function tax_code_list($selected='')
-               {
-                       $this->db->query("SELECT * FROM fm_ecomva order by id 
asc ");
-                       while ($this->db->next_record())
-                       {
-                               $tax_code_list[] = Array(
-                                       'id'        => $this->db->f('id'),
-                               );
-                       }
-
-                       return $tax_code_list;
-               }
-
-
-               function get_lisfm_ecoart()
-               {
-                       $this->db->query("SELECT * FROM fm_ecoart order by id 
asc ");
-                       while ($this->db->next_record())
-                       {
-                               $art_list[] = Array(
-                                       'id'        => $this->db->f('id'),
-                                       'num'       => $this->db->f('name')
-                               );
-                       }
-
-                       return $art_list;
-               }
-
-       //----------
-
-               function get_type_list()
-               {
-                       $this->db->query("SELECT * FROM fm_ecobilagtype order 
by id asc ");
-                       while ($this->db->next_record())
-                       {
-                               $art_list[] = Array(
-                                       'id'        => $this->db->f('id'),
-                                       'num'       => $this->db->f('name')
-                               );
-                       }
-                       return $art_list;
-               }
-
-       //----------
-               function select_dimb_list()
-               {
-                       $this->db->query("SELECT * FROM fm_ecodimb order by id 
asc ");
-                       while ($this->db->next_record())
-                       {
-                               $dimb_list[] = Array(
-                                       'id'        => $this->db->f('id'),
-                                       'num'       => $this->db->f('name')
-                               );
-                       }
-                       return $dimb_list;
-               }
-
-       //-------------------
-               function select_dimd_list()
-               {
-                       $this->db->query("SELECT * FROM fm_ecodimd order by id 
asc ");
-                       while ($this->db->next_record())
-                       {
-                               $dimd_list[] = Array(
-                                       'id'        => $this->db->f('id'),
-                                       'num'       => $this->db->f('name')
-                               );
-                       }
-                       return $dimd_list;
-               }
-       //---------------------
-
-               function select_tax_code_list()
-               {
-                       $this->db->query("SELECT * FROM fm_ecomva order by id 
asc ");
-                       while ($this->db->next_record())
-                       {
-                               $tax_code_list[] = Array(
-                                       'id'        => $this->db->f('id'),
-                                       'num'       => $this->db->f('name')
-                               );
-                       }
-                       return $tax_code_list;
-               }
-
-               function get_invoice_user_list()
-               {
-
-                       $sql = "SELECT * FROM fm_acl2 $this->join 
phpgw_accounts on phpgw_accounts.account_id=fm_acl2.acl_account where 
acl_location ='.invoice' and account_type='u' order by account_lastname";
-                       $this->db->query($sql,__LINE__,__FILE__);
-
-                       $employees[] = Array(
-                               'account_lid'       => 'all',
-                               'account_lastname'  => lang('All')
-                       );
-
-                       while ($this->db->next_record())
-                       {
-                               $employees[] = Array(
-                                       'account_id'        => 
$this->db->f('account_id'),
-                                       'account_lid'       => 
$this->db->f('account_lid'),
-                                       'account_type'      => 
$this->db->f('account_type'),
-                                       'account_firstname' => 
$this->db->f('account_firstname'),
-                                       'account_lastname'  => 
$this->db->f('account_lastname'),
-                                       'account_status'    => 
$this->db->f('account_status'),
-                                       'account_expires'   => 
$this->db->f('account_expires')
-                               );
-                       }
-//_debug_array($employees);
-                       return $employees;
-               }
-
-               function select_account_class()
-               {
-                       $sql = "SELECT DISTINCT grouping as id from 
fm_b_account ";
-                       $this->db->query($sql,__LINE__,__FILE__);
-
-                       while ($this->db->next_record())
-                       {
-                               $class[] = Array(
-                                       'id'        => $this->db->f('id')
-                               );
-                       }
-                       return $class;
-               }
-               function delete($bilagsnr)
-               {
-                       $this->db->query("DELETE FROM fm_ecobilag WHERE 
bilagsnr ='" . $bilagsnr  ."'",__LINE__,__FILE__);
-               }
-
-
-               function update_invoice($values)
-               {
-
-//_debug_array($values);
-                       foreach($values['counter'] as $n)
-                       {
-                               $local_error='';
-
-                               if($values['voucher_id'][$n])
-                               {
-                                       $voucher_id=$values['voucher_id'][$n];
-
-                                       
$check_value=array('voucher_id'=>$voucher_id,
-                                                       'sign_orig'             
=> $values['sign_orig'][$n],
-                                                       'sign'                  
=> $values['sign'][$n],
-                                                       'transfer'              
=> $values['transfer'][$n],
-                                                       'kreditnota'    => 
$values['kreditnota'][$n],
-                                                       'num_days'              
=> $values['num_days'][$n]);
-
-                                       
if($this->check_for_updates($check_value))
-                                       {
-
-                                               $check_count = 
$this->check_count($voucher_id);
-
-                                               if 
(!($check_count['dima_count'] == $values['invoice_count'][$n]))
-                                               {
-                                                       $receipt['error'][] = 
array('msg'=>lang('Dima is missing from sub invoice in:'). " 
".$values['voucher_id'][$n]);
-                                                       $local_error= true;
-                                               }
-
-                                               if 
(!($check_count['spbudact_code_count'] == $values['invoice_count'][$n]))
-                                               {
-                                                       $receipt['error'][] = 
array('msg'=>lang('Budget code is missing from sub invoice in :'). " 
".$values['voucher_id'][$n]);
-                                                       $local_error= true;
-                                               }
-
-                                               if 
(!($check_count['kostra_count'] == $values['invoice_count'][$n]))
-                                               {
-                                                       $receipt['error'][] = 
array('msg'=>'Tjenestekode mangler for undebilag: ' . " 
".$values['voucher_id'][$n]);
-                                                       $local_error= true;
-                                               }
-
-                                               if 
($this->check_claim($voucher_id))
-                                               {
-                                                       $receipt['error'][] = 
array('msg'=>lang('Tenant claim is not issued for project in voucher 
%1',$voucher_id));
-                                                       $local_error= true;
-                                               }
-
-                                               $blank_date = '';
-                                               $sign_field='';
-                                               $sign_id='';
-                                               $sign_date_field='';
-                                               $sign_date='';
-                                               $kommma='';
-                                               $wait_for_kreditnota='';
-                                               $user_lid       
=$GLOBALS['phpgw_info']['user']['account_lid'];
-
-                                               if 
(($values['sign'][$n]=='sign_none') && 
($values['sign_orig'][$n]=='sign_janitor')):
-                                                       $blank_date = 
'oppsynsigndato= NULL';
-                                                       $sign_field='';
-                                                       $sign_id='';
-                                                       $sign_date_field='';
-                                                       $sign_date='';
-                                                       $kommma='';
-                                               elseif 
(($values['sign'][$n]=='sign_none') && 
($values['sign_orig'][$n]=='sign_supervisor')):
-                                                       $blank_date = 
'saksigndato= NULL';
-                                                       $sign_field='';
-                                                       $sign_id='';
-                                                       $sign_date_field='';
-                                                       $sign_date='';
-                                                       $kommma='';
-                                               elseif 
(($values['sign'][$n]=='sign_none') && 
($values['sign_orig'][$n]=='sign_budget_responsible')):
-                                                       $blank_date = 
'budsjettsigndato= NULL';
-                                                       $sign_field='';
-                                                       $sign_id='';
-                                                       $sign_date_field='';
-                                                       $sign_date='';
-                                                       $kommma='';
-                                               elseif 
($values['sign'][$n]=='sign_janitor' && !$values['sign_orig'][$n]):
-                                                       $blank_date = '';
-                                                       $sign_field = 
'oppsynsmannid=';
-                                                       $sign_id = 
"'$user_lid'";
-                                                       $sign_date_field = 
'oppsynsigndato=';
-                                                       $sign_date="'" . 
date($this->bocommon->datetimeformat) . "'";
-                                                       $kommma=",";
-                                               elseif 
($values['sign'][$n]=='sign_janitor' && 
$values['sign_orig'][$n]=='sign_supervisor'):
-                                                       $blank_date = 
'saksigndato= NULL';
-                                                       $sign_field = 
'oppsynsmannid=';
-                                                       $sign_id = 
"'$user_lid'";
-                                                       $sign_date_field = 
'oppsynsigndato=';
-                                                       $sign_date="'" . 
date($this->bocommon->datetimeformat) . "'";
-                                                       $kommma=",";
-                                               elseif 
($values['sign'][$n]=='sign_janitor' && 
$values['sign_orig'][$n]=='sign_budget_responsible'):
-                                                       $blank_date = 
'budsjettsigndato= NULL';
-                                                       $sign_field = 
'oppsynsmannid=';
-                                                       $sign_id = 
"'$user_lid'";
-                                                       $sign_date_field = 
'oppsynsigndato=';
-                                                       $sign_date="'" . 
date($this->bocommon->datetimeformat) . "'";
-                                                       $kommma=",";
-                                               elseif 
($values['sign'][$n]=='sign_supervisor' && !$values['sign_orig'][$n]):
-                                                       $blank_date = '';
-                                                       $sign_field = 
'saksbehandlerid=';
-                                                       $sign_id = 
"'$user_lid'";
-                                                       $sign_date_field = 
'saksigndato=';
-                                                       $sign_date="'" . 
date($this->bocommon->datetimeformat) . "'";
-                                                       $kommma=",";
-                                               elseif 
($values['sign'][$n]=='sign_supervisor' && 
$values['sign_orig'][$n]=='sign_janitor'):
-                                                       $blank_date = 
'oppsynsigndato= NULL';
-                                                       $sign_field = 
'saksbehandlerid=';
-                                                       $sign_id = 
"'$user_lid'";
-                                                       $sign_date_field = 
'saksigndato=';
-                                                       $sign_date="'" . 
date($this->bocommon->datetimeformat) . "'";
-                                                       $kommma=",";
-                                               elseif 
($values['sign'][$n]=='sign_supervisor' && 
$values['sign_orig'][$n]=='sign_budget_responsible'):
-                                                       $blank_date = 
'budsjettsigndato= NULL';
-                                                       $sign_field = 
'saksbehandlerid=';
-                                                       $sign_id = 
"'$user_lid'";
-                                                       $sign_date_field = 
'saksigndato=';
-                                                       $sign_date="'" . 
date($this->bocommon->datetimeformat) . "'";
-                                                       $kommma=",";
-                                               elseif 
($values['sign'][$n]=='sign_budget_responsible' && 
$values['sign_orig'][$n]=='sign_janitor'):
-                                                       $blank_date = 
'oppsynsigndato= NULL';
-                                                       $sign_field = 
'budsjettansvarligid=';
-                                                       $sign_id = 
"'$user_lid'";
-                                                       $sign_date_field = 
'budsjettsigndato=';
-                                                       $sign_date="'" . 
date($this->bocommon->datetimeformat) . "'";
-                                                       $kommma=",";
-                                               elseif 
($values['sign'][$n]=='sign_budget_responsible' && 
$values['sign_orig'][$n]=='sign_supervisor'):
-                                                       $blank_date = 
'saksigndato= NULL';
-                                                       $sign_field = 
'budsjettansvarligid=';
-                                                       $sign_id = 
"'$user_lid'";
-                                                       $sign_date_field = 
'budsjettsigndato=';
-                                                       $sign_date="'" . 
date($this->bocommon->datetimeformat) . "'";
-                                                       $kommma=",";
-                                               elseif 
($values['sign'][$n]=='sign_budget_responsible' && !$values['sign_orig'][$n]):
-                                                       $blank_date = '';
-                                                       $sign_field = 
'budsjettansvarligid=';
-                                                       $sign_id = 
"'$user_lid'";
-                                                       $sign_date_field = 
'budsjettsigndato=';
-                                                       $sign_date="'" . 
date($this->bocommon->datetimeformat) . "'";
-                                                       $kommma=",";
-                                               endif;
-
-
-                                               if($blank_date )
-                                               {
-                                                       $kommma_blank=",";
-                                               }
-                                               else
-                                               {
-                                                       $kommma_blank='';
-                                               }
-
-                                               
$transfer_sign_field='utbetalingid=';
-                                               
$transfer_date_field='utbetalingsigndato=';
-
-                                               if 
(!($values['num_days_orig'][$n]==$values['num_days'][$n]))
-                                               {
-                                                       $payment_date = 
date($this->bocommon->dateformat,$values['timestamp_voucher_date'][$n]+(24*3600*$values['num_days'][$n]));
-                                                       
$GLOBALS['phpgw']->db->query("UPDATE fm_ecobilag set forfallsdato= 
'$payment_date' where bilagsnr='$voucher_id'");
-                                               }
-
-                                               $transfer_id="Null".",";
-                                               $transfer_date="Null";
-
-                                               if ($values['transfer'][$n])
-                                               {
-                                                       if 
($this->check_for_transfer($voucher_id))
-                                                       {
-                                                               
$transfer_id="'$user_lid',";
-                                                               
$transfer_date="'" . date($this->bocommon->datetimeformat) . "'";
-                                                       }
-                                                       else
-                                                       {
-                                                               
$receipt['error'][] = array('msg'=>'Dette bilaget er ikkje godkjent: ' . " 
".$voucher_id);
-                                                               $local_error= 
true;
-                                                       }
-
-                                               }
-
-                                               if ($values['kreditnota'][$n])
-                                               {
-                                                       $wait_for_kreditnota=1;
-                                                       $transfer_date="Null";
-                                               }
-                                               else
-                                               {
-                                                       
$wait_for_kreditnota='NULL';
-                                               }
-
-                                               if (! $local_error)
-                                               {
-                                                       $sql= "UPDATE 
fm_ecobilag set $blank_date $kommma_blank $sign_field $sign_id $kommma 
$sign_date_field $sign_date $kommma $transfer_sign_field $transfer_id 
$transfer_date_field $transfer_date ,kreditnota=$wait_for_kreditnota  where 
bilagsnr='$voucher_id'";
-                                                       
$GLOBALS['phpgw']->db->transaction_begin();
-                                                       
$GLOBALS['phpgw']->db->query($sql);
-                                                       
$GLOBALS['phpgw']->db->transaction_commit();
-
-                                                       $receipt['message'][] = 
array('msg'=> lang('voucher is updated: ') . $voucher_id);
-                                               }
-                                       }
-                               }
-                       }
-
-                       $GLOBALS['phpgw']->db->query("UPDATE fm_ecobilag set 
utbetalingid = NULL, utbetalingsigndato = NULL WHERE budsjettsigndato IS NULL");
-                       $GLOBALS['phpgw']->db->query("UPDATE fm_ecobilag set 
utbetalingid = NULL, utbetalingsigndato = NULL WHERE oppsynsigndato IS NULL AND 
saksigndato IS NULL");
-
-                       return $receipt;
-               }
-
-               function check_for_transfer($voucher_id='')
-               {
-                       $allow_transfer=False;
-
-                       $sql = "SELECT * FROM fm_ecobilag WHERE 
bilagsnr='$voucher_id'";
-                       $this->db->limit_query($sql,0,__LINE__,__FILE__,1);
-
-                       $this->db->next_record();
-
-                       if ($this->db->f('budsjettsigndato') && 
($this->db->f('oppsynsigndato') || $this->db->f('saksigndato')))
-                       {
-                               $allow_transfer=True;
-                       }
-
-                       return $allow_transfer;
-               }
-
-               function check_claim($voucher_id='')
-               {
-                       $sql = "SELECT count(*) FROM fm_ecobilag 
$this->left_join fm_workorder on fm_ecobilag.pmwrkord_code = fm_workorder.id "
-                       . " WHERE bilagsnr='$voucher_id' AND 
fm_workorder.charge_tenant=1 AND fm_workorder.claim_issued IS NULL";
-                       $this->db->query($sql,__LINE__,__FILE__);
-                       $this->db->next_record();
-                       return $this->db->f(0);
-               }
-       }
-?>
+                       $this->account_id       = 
$GLOBALS['phpgw_info']['user']['account_id'];
+
+                       $this->acl2             = 
CreateObject($this->currentapp.'.acl2','##DEFAULT##','##DEFAULT##');
+
+                       $this->join                     = $this->bocommon->join;
+                       $this->left_join        = $this->bocommon->left_join;
+                       $this->like                     = $this->bocommon->like;
+               }
+
+               function read_invoice($data)
+               {
+                       if(is_array($data))
+                       {
+                               if ($data['start'])
+                               {
+                                       $start=$data['start'];
+                               }
+                               else
+                               {
+                                       $start=0;
+                               }
+                               $query                  = 
(isset($data['query'])?$data['query']:'');
+                               $sort                   = 
(isset($data['sort'])?$data['sort']:'DESC');
+                               $order                  = 
(isset($data['order'])?$data['order']:'');
+                               $cat_id                 = 
(isset($data['cat_id'])?$data['cat_id']:0);
+                               $user_lid               = 
(isset($data['user_lid'])?$data['user_lid']:'none');
+                               $paid                   = 
(isset($data['paid'])?$data['paid']:'');
+                               $start_date     = 
(isset($data['start_date'])?$data['start_date']:'');
+                               $end_date               = 
(isset($data['end_date'])?$data['end_date']:'');
+                               $vendor_id              = 
(isset($data['vendor_id'])?$data['vendor_id']:'');
+                               $loc1                   = 
(isset($data['loc1'])?$data['loc1']:'');
+                               $workorder_id   = 
(isset($data['workorder_id'])?$data['workorder_id']:'');
+                               $allrows                = 
(isset($data['allrows'])?$data['allrows']:'');
+                               $voucher_id             = 
(isset($data['voucher_id'])?$data['voucher_id']:'');
+                       }
+
+//_debug_array($data);
+
+                       if ($order)
+                       {
+                               $ordermethod = " order by $order $sort";
+                       }
+                       else
+                       {
+                               $ordermethod = ' order by bilagsnr DESC';
+                       }
+
+                       $where= 'WHERE';
+
+                       if ($user_lid=='none' || !$user_lid):
+                       {
+                               return;
+                       }
+                       elseif ($user_lid!='all'):
+                       {
+                               $filtermethod = " WHERE ( oppsynsmannid= 
'$user_lid' or saksbehandlerid= '$user_lid' or budsjettansvarligid= 
'$user_lid')";
+                               $where= 'AND';
+                       }
+                       endif;
+
+                       if ($cat_id > 0)
+                       {
+                               $filtermethod .= " $where typeid='$cat_id' ";
+                               $where= 'AND';
+                       }
+
+                       if ($district_id > 0)
+                       {
+                               $filtermethod .= " $where  
district_id='$district_id' ";
+                               $where= 'AND';
+                       }
+
+                       if ($vendor_id)
+                       {
+                               $filtermethod .= " $where  spvend_code 
='$vendor_id' ";
+                               $where= 'AND';
+                       }
+                       if ($loc1)
+                       {
+                               $filtermethod .= " $where  dima $this->like 
'%$loc1%' ";
+                               $where= 'AND';
+                       }
+                       if ($workorder_id)
+                       {
+                               $filtermethod .= " $where  pmwrkord_code 
='$workorder_id' ";
+                               $where= 'AND';
+                       }
+                       if ($voucher_id)
+                       {
+                               $filtermethod .= " $where  bilagsnr $this->like 
'%$voucher_id%' ";
+                               $where= 'AND';
+                       }
+
+
+                       if ($paid)
+                       {
+                               $table = 'fm_ecobilagoverf';
+                               $dateformat = 
strtolower($GLOBALS['phpgw_info']['user']['preferences']['common']['dateformat']);
+                               $dateformat = str_replace(".","",$dateformat);
+                               $dateformat = str_replace("-","",$dateformat);
+                               $dateformat = str_replace("/","",$dateformat);
+                               $y=strpos($dateformat,'y');
+                               $d=strpos($dateformat,'d');
+                               $m=strpos($dateformat,'m');
+
+                               $dateparts = explode('/', $start_date);
+                               $sday = $dateparts[$d];
+                               $smonth = $dateparts[$m];
+                               $syear = $dateparts[$y];
+
+                               $dateparts = explode('/', $end_date);
+                               $eday = $dateparts[$d];
+                               $emonth = $dateparts[$m];
+                               $eyear = $dateparts[$y];
+
+                               $start_date = 
date($this->bocommon->dateformat,mktime(2,0,0,$smonth,$sday,$syear));
+                               $end_date = 
date($this->bocommon->dateformat,mktime(2,0,0,$emonth,$eday,$eyear));
+
+                               if (!$workorder_id && !$voucher_id)
+                               {
+                                       $filtermethod .= " $where  overftid 
>'$start_date' and overftid < '$end_date'";
+                               }
+                       }
+                       else
+                       {
+                               $table ='fm_ecobilag';
+                       }
+
+
+                       if($query)
+                       {
+                               $query = ereg_replace("'",'',$query);
+                               $query = ereg_replace('"','',$query);
+
+                               $querymethod = " $where ( spvend_code 
$this->like '%$query%' OR bilagsnr $this->like '%$query%' )";
+                       }
+
+
+                       $sql = "SELECT DISTINCT bilagsnr, count(bilagsnr) as 
invoice_count, sum(belop) as belop,spvend_code,fakturadato FROM  $table  
$filtermethod $querymethod group by bilagsnr,spvend_code,fakturadato ";
+                       $sql2 = "SELECT DISTINCT bilagsnr FROM  $table  
$filtermethod $querymethod";
+
+//echo $sql;
+                       $this->db2->query($sql2,__LINE__,__FILE__);
+                       $this->total_records = $this->db2->num_rows();
+
+                       if(!$allrows)
+                       {
+                               $this->db->limit_query($sql . 
$ordermethod,$start,__LINE__,__FILE__);
+                       }
+                       else
+                       {
+                               $this->db->query($sql . 
$ordermethod,__LINE__,__FILE__);
+                       }
+
+                       while ($this->db->next_record())
+                       {
+                               $temp[] = array
+                               (
+                                       'voucher_id'            => 
$this->db->f('bilagsnr'),
+                                       'invoice_count'         => 
$this->db->f('invoice_count'),
+                                       'amount'                        => 
$this->db->f('belop')
+                                       );
+                       }
+
+                       if ($temp)
+                       {
+                               $role= $this->check_role();
+                               $i = 0;
+                               foreach($temp as $invoice_temp)
+                               {
+                                       $voucher_id=$invoice_temp['voucher_id'];
+
+                                       $sql = "SELECT 
spvend_code,oppsynsmannid,saksbehandlerid,budsjettansvarligid,"
+                                       . " 
utbetalingid,oppsynsigndato,saksigndato,budsjettsigndato,utbetalingsigndato,fakturadato,org_name,"
+                                       . " 
forfallsdato,periode,artid,kidnr,kreditnota "
+                                       . " from $table $this->join fm_vendor 
ON fm_vendor.id = $table.spvend_code WHERE bilagsnr = $voucher_id "
+                                       . " group by 
bilagsnr,spvend_code,oppsynsmannid,saksbehandlerid,budsjettansvarligid,"
+                                       . " 
utbetalingid,oppsynsigndato,saksigndato,budsjettsigndato,utbetalingsigndato,fakturadato,org_name,"
+                                       . " 
forfallsdato,periode,artid,kidnr,kreditnota ";
+
+                                       
$this->db->query($sql,__LINE__,__FILE__);
+
+                                       $this->db->next_record();
+
+                                       $timestamp_voucher_date= 
mktime(0,0,0,date(m,strtotime($this->db->f('fakturadato'))),date(d,strtotime($this->db->f('fakturadato'))),date(y,strtotime($this->db->f('fakturadato'))));
+                                       $timestamp_payment_date= 
mktime(0,0,0,date(m,strtotime($this->db->f('forfallsdato'))),date(d,strtotime($this->db->f('forfallsdato'))),date(y,strtotime($this->db->f('forfallsdato'))));
+
+                                       if($this->db->f('oppsynsmannid') && 
$this->db->f('oppsynsigndato'))
+                                       {
+//                                             $timestamp_jan_date             
        = 
mktime(0,0,0,date(m,strtotime($this->db->f('oppsynsigndato'))),date(d,strtotime($this->db->f('oppsynsigndato'))),date(y,strtotime($this->db->f('oppsynsigndato'))));
+//                                             $invoice[$i]['jan_date']        
= 
$GLOBALS['phpgw']->common->show_date($timestamp_jan_date,$GLOBALS['phpgw_info']['user']['preferences']['common']['dateformat']);
+                                               
$invoice[$i]['jan_date']=date($GLOBALS['phpgw_info']['user']['preferences']['common']['dateformat'],strtotime($this->db->f('oppsynsigndato')));
+                                       }
+                                       else
+                                       {
+                                               $invoice[$i]['jan_date']        
='';
+                                       }
+                                       if($this->db->f('saksbehandlerid') && 
$this->db->f('saksigndato'))
+                                       {
+//                                             $timestamp_super_date           
= 
mktime(0,0,0,date(m,strtotime($this->db->f('saksigndato'))),date(d,strtotime($this->db->f('saksigndato'))),date(y,strtotime($this->db->f('saksigndato'))));
+//                                             $invoice[$i]['super_date']      
= 
$GLOBALS['phpgw']->common->show_date($timestamp_super_date,$GLOBALS['phpgw_info']['user']['preferences']['common']['dateformat']);
+                                               
$invoice[$i]['super_date']=date($GLOBALS['phpgw_info']['user']['preferences']['common']['dateformat'],strtotime($this->db->f('saksigndato')));
+                                       }
+                                       else
+                                       {
+                                               $invoice[$i]['super_date']      
='';
+                                       }
+
+                                       if($this->db->f('budsjettansvarligid') 
&& $this->db->f('budsjettsigndato'))
+                                       {
+//                                                     $timestamp_budget_date  
        = 
mktime(0,0,0,date(m,strtotime($this->db->f('budsjettsigndato'))),date(d,strtotime($this->db->f('budsjettsigndato'))),date(y,strtotime($this->db->f('budsjettsigndato'))));
+//                                                     
$invoice[$i]['budget_date']     = 
$GLOBALS['phpgw']->common->show_date($timestamp_budget_date,$GLOBALS['phpgw_info']['user']['preferences']['common']['dateformat']);
+                                                       
$invoice[$i]['budget_date']=date($GLOBALS['phpgw_info']['user']['preferences']['common']['dateformat'],strtotime($this->db->f('budsjettsigndato')));
+                                       }
+                                       else
+                                       {
+                                               $invoice[$i]['budget_date']     
='';
+                                       }
+
+                                       if($this->db->f('utbetalingid') && 
$this->db->f('utbetalingsigndato'))
+                                       {
+//                                             $timestamp_transfer_date= 
mktime(0,0,0,date(m,strtotime($this->db->f('utbetalingsigndato'))),date(d,strtotime($this->db->f('utbetalingsigndato'))),date(y,strtotime($this->db->f('utbetalingsigndato'))));
+//                                             $invoice[$i]['transfer_date']   
                = 
$GLOBALS['phpgw']->common->show_date($timestamp_transfer_date,$GLOBALS['phpgw_info']['user']['preferences']['common']['dateformat']);
+                                               
$invoice[$i]['transfer_date']=date($GLOBALS['phpgw_info']['user']['preferences']['common']['dateformat'],strtotime($this->db->f('utbetalingsigndato')));
+                                       }
+                                       else
+                                       {
+                                               $invoice[$i]['transfer_date']   
='';
+                                       }
+
+                                       $invoice[$i]['counter']                 
                = $i;
+                                       $invoice[$i]['current_user']            
        = $GLOBALS['phpgw_info']['user']['account_lid'];
+                                       $invoice[$i]['voucher_id']              
                = $voucher_id;
+                                       $invoice[$i]['invoice_count']           
        = $invoice_temp['invoice_count'];
+                                       $invoice[$i]['vendor_id']               
                = $this->db->f('spvend_code');
+                                       $invoice[$i]['vendor']                  
                = $this->db->f('org_name');
+                                       $invoice[$i]['is_janitor']              
                = $role['is_janitor'];
+                                       $invoice[$i]['is_supervisor']           
        = $role['is_supervisor'];
+                                       $invoice[$i]['is_budget_responsible']   
= $role['is_budget_responsible'];
+                                       $invoice[$i]['is_janitor']              
                = $role['is_janitor'];
+                                       $invoice[$i]['is_transfer']             
                = $role['is_transfer'];
+                                       $invoice[$i]['janitor']                 
                = $this->db->f('oppsynsmannid');
+                                       $invoice[$i]['supervisor']              
                = $this->db->f('saksbehandlerid');
+                                       $invoice[$i]['budget_responsible']      
        = $this->db->f('budsjettansvarligid');
+                                       $invoice[$i]['transfer_id']             
                = $this->db->f('utbetalingid');
+                                       $invoice[$i]['voucher_date']            
        = 
$GLOBALS['phpgw']->common->show_date($timestamp_voucher_date,$GLOBALS['phpgw_info']['user']['preferences']['common']['dateformat']);
+                                       $invoice[$i]['payment_date']            
        = 
$GLOBALS['phpgw']->common->show_date($timestamp_payment_date,$GLOBALS['phpgw_info']['user']['preferences']['common']['dateformat']);
+                                       $invoice[$i]['period']                  
                = $this->db->f('periode');
+                                       $invoice[$i]['type']                    
                = $this->db->f('artid');
+                                       $invoice[$i]['kidnr']                   
                = $this->db->f('kidnr');
+                                       $invoice[$i]['kreditnota']              
                = $this->db->f('kreditnota');
+                                       $invoice[$i]['amount']                  
                = $invoice_temp['amount'];
+                                       $invoice[$i]['num_days']                
                = 
intval(($timestamp_payment_date-$timestamp_voucher_date)/(24*3600));
+                                       $invoice[$i]['timestamp_voucher_date']  
= $timestamp_voucher_date;
+
+                                       
if($invoice[$i]['current_user']==$invoice[$i]['janitor'] && 
$invoice[$i]['jan_date']):
+                                       {
+                                               
$invoice[$i]['sign_orig']='sign_janitor';
+                                       }
+                                       
elseif($invoice[$i]['current_user']==$invoice[$i]['supervisor'] && 
$invoice[$i]['super_date']):
+                                       {
+                                               
$invoice[$i]['sign_orig']='sign_supervisor';
+                                       }
+                                       
elseif($invoice[$i]['current_user']==$invoice[$i]['budget_responsible'] && 
$invoice[$i]['budget_date']):
+                                       {
+                                               
$invoice[$i]['sign_orig']='sign_budget_responsible';
+                                       }
+                                       endif;
+
+                                       $i++;
+
+                               }
+                       }
+//                     $end_time = explode(' ',microtime());
+//                     $this->end_time = $end_time;
+
+//_debug_array($invoice);
+//_debug_array($invoice_temp);
+
+                       return $invoice;
+
+               }
+
+
+               function read_invoice_sub($data)
+               {
+                       if(is_array($data))
+                       {
+                               if ($data['start'])
+                               {
+                                       $start=$data['start'];
+                               }
+                               else
+                               {
+                                       $start=0;
+                               }
+                               $filter = 
(isset($data['filter'])?$data['filter']:'none');
+                               $sort = 
(isset($data['sort'])?$data['sort']:'DESC');
+                               $order = 
(isset($data['order'])?$data['order']:'');
+                               $voucher_id = 
(isset($data['voucher_id'])?$data['voucher_id']:0);
+                               $paid = (isset($data['paid'])?$data['paid']:'');
+                       }
+
+                       if ($paid)
+                       {
+                               $table = 'fm_ecobilagoverf';
+                       }
+                       else
+                       {
+                               $table ='fm_ecobilag';
+                       }
+
+
+                       if ($order)
+                       {
+                               $ordermethod = " order by $order $sort";
+                       }
+                       else
+                       {
+                               $ordermethod = ' order by bilagsnr DESC';
+                       }
+
+                       if ($voucher_id)
+                       {
+                               $filtermethod = " WHERE ( bilagsnr= 
'$voucher_id')";
+                       }
+
+                       $sql = "SELECT 
$table.*,fm_workorder.status,fm_workorder.charge_tenant,org_name,fm_workorder.claim_issued
 FROM $table "
+                       . " $this->left_join fm_workorder on fm_workorder.id = 
$table.pmwrkord_code  "
+                       . " $this->join fm_vendor  on $table.spvend_code = 
fm_vendor.id  $filtermethod ";
+
+                       $this->db2->query($sql,__LINE__,__FILE__);
+                       $this->total_records = $this->db2->num_rows();
+                       $this->db->query($sql . 
$ordermethod,$start,__LINE__,__FILE__);
+
+                       $i = 0;
+
+                       while ($this->db->next_record())
+                       {
+                               $invoice[$i]['counter']                         
        = $i;
+                               $invoice[$i]['claim_issued']                    
= $this->db->f('claim_issued');
+                               $invoice[$i]['project_id']                      
        = $this->db->f('project_id');
+                               $invoice[$i]['workorder_id']                    
= $this->db->f('pmwrkord_code');
+                               $invoice[$i]['status']                          
        = $this->db->f('status');
+                               if ($this->db->f('status')=='closed')
+                               {
+                                       $invoice[$i]['closed']                  
        = True;
+                               }
+                               $invoice[$i]['voucher_id']                      
        = $voucher_id;
+                               $invoice[$i]['id']                              
                = $this->db->f('id');
+                               $invoice[$i]['invoice_id']                      
        = $this->db->f('fakturanr');
+                               $invoice[$i]['budget_account']                  
= $this->db->f('spbudact_code');
+                               $invoice[$i]['dima']                            
        = $this->db->f('dima');
+                               $invoice[$i]['dimb']                            
        = $this->db->f('dimb');
+                               $invoice[$i]['dimd']                            
        = $this->db->f('dimd');
+                               if ($this->db->f('merknad'))
+                               {
+                                       $invoice[$i]['remark']                  
        = True;
+                               }
+                               $invoice[$i]['tax_code']                        
        = $this->db->f('mvakode');
+                               $invoice[$i]['amount']                          
        = $this->db->f('belop');
+                               $invoice[$i]['charge_tenant']                   
= $this->db->f('charge_tenant');
+                               $invoice[$i]['vendor']                          
        = $this->db->f('org_name');
+                               $i++;
+
+                       }
+
+                       return $invoice;
+
+               }
+
+
+               function read_consume($data)
+               {
+                       if(is_array($data))
+                       {
+                               if ($data['start'])
+                               {
+                                       $start=$data['start'];
+                               }
+                               else
+                               {
+                                       $start=0;
+                               }
+                               $filter                 = 
(isset($data['filter'])?$data['filter']:'none');
+                               $query                  = 
(isset($data['query'])?$data['query']:'');
+                               $sort                   = 
(isset($data['sort'])?$data['sort']:'DESC');
+                               $order                  = 
(isset($data['order'])?$data['order']:'');
+                               $cat_id                 = 
(isset($data['cat_id'])?$data['cat_id']:0);
+                               $start_date     = 
(isset($data['start_date'])?$data['start_date']:'');
+                               $end_date               = 
(isset($data['end_date'])?$data['end_date']:'');
+                               $vendor_id              = 
(isset($data['vendor_id'])?$data['vendor_id']:'');
+                               $loc1   = 
(isset($data['loc1'])?$data['loc1']:'');
+                               $district_id    = 
(isset($data['district_id'])?$data['district_id']:'');
+                               $workorder_id   = 
(isset($data['workorder_id'])?$data['workorder_id']:0);
+                               $b_account_class = 
(isset($data['b_account_class'])?$data['b_account_class']:'');
+                       }
+//_debug_array($data);
+
+                       $dateformat = 
strtolower($GLOBALS['phpgw_info']['user']['preferences']['common']['dateformat']);
+                       $dateformat = str_replace(".","",$dateformat);
+                       $dateformat = str_replace("-","",$dateformat);
+                       $dateformat = str_replace("/","",$dateformat);
+                       $y=strpos($dateformat,'y');
+                       $d=strpos($dateformat,'d');
+                       $m=strpos($dateformat,'m');
+                       $dateparts = explode('/', $start_date);
+                       $sday = $dateparts[$d];
+                       $smonth = $dateparts[$m];
+                       $syear = $dateparts[$y];
+
+                       $dateparts = explode('/', $end_date);
+                       $eday = $dateparts[$d];
+                       $emonth = $dateparts[$m];
+                       $eyear = $dateparts[$y];
+
+                       $start_date = 
date($this->bocommon->dateformat,mktime(2,0,0,$smonth,$sday,$syear));
+                       $end_date = 
date($this->bocommon->dateformat,mktime(2,0,0,$emonth,$eday,$eyear));
+
+
+                       $where = 'WHERE';
+
+                       if($b_account_class)
+                       {
+                               $filtermethod= " $where 
fm_b_account.grouping='$b_account_class'";
+                               $where= 'AND';
+                       }
+                       else
+                       {
+                               $select_account_class=',fm_b_account.grouping 
as b_account_class';
+                               $group_account_class=', 
spbudact_code,fm_b_account.grouping';
+                       }
+
+                       if ($vendor_id)
+                       {
+                               $filtermethod .= " $where (spvend_code = 
$vendor_id)";
+                               $where= 'AND';
+                       }
+
+                       if($loc1)
+                       {
+                               $filtermethod .=" $where (dima $this->like 
'%$loc1%')";
+                               $where= 'AND';
+                       }
+
+
+                       if ($district_id)
+                       {
+                               $filtermethod.= " $where district_id= 
$district_id ";
+                               $where= 'AND';
+                       }
+
+                       if ($workorder_id)
+                       {
+                               $filtermethod.= " $where pmwrkord_code = 
$workorder_id";
+                               $where= 'AND';
+                       }
+
+
+                       if ($cat_id>0)
+                       {
+                               $filtermethod .= " $where typeid = $cat_id";
+                               $where= 'AND';
+                       }
+
+                       $filtermethod .= " $where (fakturadato >'$start_date') 
AND (fakturadato < '$end_date')";
+
+                       $sql = "SELECT district_id,periode,sum(godkjentbelop) 
as consume $select_account_class "
+                               . " FROM  fm_ecobilagoverf $this->join 
fm_location1 ON (fm_ecobilagoverf.loc1 = fm_location1.loc1) "
+                               . " $this->join fm_part_of_town ON 
(fm_location1.part_of_town_id = fm_part_of_town.part_of_town_id) "
+                               . " $this->join fm_b_account ON 
(fm_ecobilagoverf.spbudact_code = fm_b_account.id) "
+                       . " $filtermethod group by district_id,periode 
$group_account_class";
+//echo $sql;
+
+                       $this->db->query($sql,__LINE__,__FILE__);
+                       $this->total_records = $this->db->num_rows();
+                       $this->db->query($sql . 
$ordermethod,$start,__LINE__,__FILE__);
+
+                       $i = 0;
+
+                       while ($this->db->next_record())
+                       {
+                               $consume[$i]['consume']                         
        = $this->db->f('consume');
+                               $consume[$i]['period']                          
        = $this->db->f('periode');
+                               $consume[$i]['district_id']                     
        = $this->db->f('district_id');
+                               if(!$b_account_class)
+                               {
+                                       $consume[$i]['account_class']           
        = $this->db->f('b_account_class');
+                               }
+                               else
+                               {
+                                       $consume[$i]['account_class']           
        = $b_account_class;
+                               }
+
+                               $i++;
+                       }
+//_debug_array($consume);
+                       return $consume;
+               }
+
+               function check_for_updates($values)
+               {
+                       $update=False;
+
+                       if($values['sign_orig']!=$values['sign'])
+                       {
+                               $update=True;
+                               return $update;
+                       }
+
+                       $sql = "SELECT * FROM fm_ecobilag WHERE bilagsnr=" . 
$values['voucher_id'];
+                       $this->db->limit_query($sql,0,__LINE__,__FILE__,1);
+
+                       $this->db->next_record();
+
+                       if( ($this->db->f('utbetalingsigndato') && 
!$values['transfer']) || (!$this->db->f('utbetalingsigndato') && 
$values['transfer']))
+                       {
+                               $update=True;
+                               return $update;
+                       }
+
+                       if( ($this->db->f('kreditnota') && 
!$values['kreditnota']) || (!$this->db->f('kreditnota') && 
$values['kreditnota']) )
+                       {
+                               $update=True;
+                               return $update;
+                       }
+
+                       $timestamp_voucher_date= 
mktime(0,0,0,date(m,strtotime($this->db->f('fakturadato'))),date(d,strtotime($this->db->f('fakturadato'))),date(y,strtotime($this->db->f('fakturadato'))));
+                       $timestamp_payment_date= 
mktime(0,0,0,date(m,strtotime($this->db->f('forfallsdato'))),date(d,strtotime($this->db->f('forfallsdato'))),date(y,strtotime($this->db->f('forfallsdato'))));
+
+                       if( 
((intval(($timestamp_payment_date-$timestamp_voucher_date)/(24*3600)))!=$values['num_days'])
 )
+                       {
+                               $update=True;
+                               return $update;
+                       }
+               }
+
+               function update_invoice_sub($values)
+               {
+
+                       $GLOBALS['phpgw']->db->transaction_begin();
+
+                       while($entry=each($values['counter']))
+                               {
+                                       $local_error='';
+
+                                       $n=$entry[0];
+
+
+//_debug_array($entry);
+
+
+                               if ($values['budget_account'][$n])
+                               {
+                                       
$budget_account=$values['budget_account'][$n];
+
+                                       $GLOBALS['phpgw']->db->query("select 
count(*) from fm_b_account  where id =$budget_account");
+                                       $GLOBALS['phpgw']->db->next_record();
+                                       if ($GLOBALS['phpgw']->db->f(0) == 0)
+                                       {
+                                               $receipt['error'][] = 
array('msg'=> lang('This account is not valid:'). " ".$budget_account);
+                                               $local_error= true;
+                                       }
+                               }
+                               else
+                               {
+                                               $receipt['error'][] = 
array('msg'=>lang('Budget account is missing:'));
+                                               $local_error= true;
+                               }
+
+                               if(!$values['dimd'][$n])
+                               {
+                                       $dimd_field="dimd=NULL";
+                               }
+                               else
+                               {
+                                       $dimd=$values['dimd'][$n];
+                                       $GLOBALS['phpgw']->db->query("select 
count(*) from  fm_dim_d where id =$dimd");
+                                       $GLOBALS['phpgw']->db->next_record();
+                                       if ($GLOBALS['phpgw']->db->f(0) == 0)
+                                       {
+                                               $receipt['error'][] = 
array('msg'=>lang('This Dim D is not valid:'). " ".$dimd);
+                                               $local_error= true;
+                                       }
+
+                                       $dimd_field="dimd="."'" . $dimd . "'";
+                               }
+
+                               if (!$values['dima'][$n])
+                               {
+                                       $dima_field="dima=NULL";
+                                       $receipt['error'][] = 
array('msg'=>lang('Dim A is missing'));
+                                       $local_error= true;
+                               }
+                               else
+                               {
+                                       
$dima_check=substr($values['dima'][$n],0,4);
+                                       $GLOBALS['phpgw']->db->query("select 
loc1, kostra_id from fm_location1 where loc1 = '$dima_check' ");
+                                       $GLOBALS['phpgw']->db->next_record();
+                                       if (!$GLOBALS['phpgw']->db->f('loc1'))
+                                       {
+                                               $receipt['error'][] = 
array('msg'=>lang('This Dim A is not valid:'). " ".$values['dima'][$n]);
+                                               $local_error= true;
+                                       }
+
+                                       if 
(!$GLOBALS['phpgw']->db->f('kostra_id') || 
$GLOBALS['phpgw']->db->f('kostra_id') == 0)
+                                       {
+                                               $receipt['error'][] = 
array('msg'=>'objektet mangler tjeneste - utgått? '. " ".$values['dima'][$n]);
+                                               $local_error= true;
+                                       }
+
+                               //      $dima_field="dima="."'" . 
$values['dima'][$n] . "'";
+                                       $dima_field="dima="."'" . 
$values['dima'][$n] . "',loc1=" . "'" . substr($values['dima'][$n],0,4) . "'";
+
+                                       $kostra_field="kostra_id="."'" . 
$GLOBALS['phpgw']->db->f('kostra_id') . "'";
+
+                               }
+
+                               if (! $local_error)
+                               {
+                                       $id                     = 
$values['id'][$n];
+                                       $tax_code       = 
$values['tax_code'][$n];
+                                       
$workorder_id=$values['workorder_id'][$n];
+                                       if($values['close_order'][$n] && 
!$values['close_order_orig'][$n])
+                                       {
+                                               
$update_status[$workorder_id]='X';
+                                       }
+
+                                       if(!$values['close_order'][$n] && 
$values['close_order_orig'][$n])
+                                       {
+                                               
$update_status[$workorder_id]='R';
+                                       }
+
+                                       $GLOBALS['phpgw']->db->query("UPDATE 
fm_ecobilag set $dima_field ,$kostra_field,$dimd_field, mvakode = 
'$tax_code',spbudact_code = '$budget_account' where id='$id'");
+
+                                       $receipt['message'][] = 
array('msg'=>lang('Voucher is updated '));
+                               }
+
+                       }
+
+                       if (isset($update_status) AND is_array($update_status))
+                       {
+                               
$status_code=array('X'=>'closed','R'=>'re_opened');
+
+                               $historylog_workorder   = 
CreateObject($this->currentapp.'.historylog','workorder');
+
+                               while (list($id,$entry) = each($update_status))
+                               {
+                                       
$historylog_workorder->add($entry,$id,$status_code[$entry]);
+                                       $GLOBALS['phpgw']->db->query("UPDATE 
fm_workorder set status=" . "'$status_code[$entry]'" . "where id=$id");
+                                       $receipt['message'][] = 
array('msg'=>lang('Workorder %1 is %2',$id, $status_code[$entry]));
+                               }
+                       }
+
+                       $GLOBALS['phpgw']->db->transaction_commit();
+
+                       return $receipt;
+               }
+
+               function read_remark($id='',$paid='')
+               {
+                       if ($paid)
+                       {
+                               $table = 'fm_ecobilagoverf';
+                       }
+                       else
+                       {
+                               $table ='fm_ecobilag';
+                       }
+
+                       $this->db->query(" SELECT merknad from $table  where 
id= '$id'");
+                       $this->db->next_record();
+
+                       return $this->db->f('merknad');
+               }
+
+               function check_role()
+               {
+                       if(!$this->role)
+                       {
+                               $this->role=array(
+                                       'is_janitor'                            
=> $this->acl2->check('.invoice',32),
+                                       'is_supervisor'                         
=> $this->acl2->check('.invoice',64),
+                                       'is_budget_responsible'         => 
$this->acl2->check('.invoice',128),
+                                       'is_transfer'                           
=> $this->acl2->check('.invoice',16)
+                                       );
+                       }
+                       return $this->role;
+               }
+
+               function check_count($voucher_id)
+               {
+
+                       $this->db->query("select count(dima) as dima_count , 
count(spbudact_code) as spbudact_code_count from fm_ecobilag where bilagsnr 
='$voucher_id'");
+                       $this->db->next_record();
+
+                       $check_count=array(
+                               'dima_count'                            => 
$this->db->f('dima_count'),
+                               'spbudact_code_count'           => 
$this->db->f('spbudact_code_count')
+                               );
+
+                       $this->db->query("select count(kostra_id) as 
kostra_count  from fm_ecobilag where bilagsnr ='$voucher_id' and kostra_id > 
0");
+                       $this->db->next_record();
+                       $check_count['kostra_count'] = 
$this->db->f('kostra_count');
+
+                       return $check_count;
+               }
+
+
+               function update_period($voucher_id='',$period='')
+               {
+                       $this->db->transaction_begin();
+
+                       $this->db->query("UPDATE fm_ecobilag set 
periode='$period' where bilagsnr='$voucher_id'");
+
+                       $this->db->transaction_commit();
+
+                       $receipt['message'][] = array('msg'=>lang('voucher 
period is updated'));
+                       return $receipt;
+               }
+
+
+               function increment_bilagsnr()
+               {
+
+                       $this->db->query("UPDATE fm_idgenerator set value = 
value + 1 where name = 'Bilagsnummer'");
+                       $this->db->query("select value from fm_idgenerator 
where name = 'Bilagsnummer'");
+                       $this->db->next_record();
+                       $bilagsnr = $this->db->f('value');
+                       return $bilagsnr;
+
+               }
+
+               function next_bilagsnr()
+               {
+
+                       $this->db->query("select value from fm_idgenerator 
where name = 'Bilagsnummer'");
+                       $this->db->next_record();
+                       $bilagsnr = $this->db->f('value')+1;
+
+                       return $bilagsnr;
+               }
+
+               function check_vendor($vendor_id)
+               {
+
+                       $this->db->query("select count(*) from fm_vendor where 
id='$vendor_id'");
+                       $this->db->next_record();
+                       return $this->db->f(0);
+               }
+
+
+               function tax_code_list($selected='')
+               {
+                       $this->db->query("SELECT * FROM fm_ecomva order by id 
asc ");
+                       while ($this->db->next_record())
+                       {
+                               $tax_code_list[] = Array(
+                                       'id'        => $this->db->f('id'),
+                               );
+                       }
+
+                       return $tax_code_list;
+               }
+
+
+               function get_lisfm_ecoart()
+               {
+                       $this->db->query("SELECT * FROM fm_ecoart order by id 
asc ");
+                       while ($this->db->next_record())
+                       {
+                               $art_list[] = Array(
+                                       'id'        => $this->db->f('id'),
+                                       'num'       => $this->db->f('name')
+                               );
+                       }
+
+                       return $art_list;
+               }
+
+       //----------
+
+               function get_type_list()
+               {
+                       $this->db->query("SELECT * FROM fm_ecobilagtype order 
by id asc ");
+                       while ($this->db->next_record())
+                       {
+                               $art_list[] = Array(
+                                       'id'        => $this->db->f('id'),
+                                       'num'       => $this->db->f('name')
+                               );
+                       }
+                       return $art_list;
+               }
+
+       //----------
+               function select_dimb_list()
+               {
+                       $this->db->query("SELECT * FROM fm_ecodimb order by id 
asc ");
+                       while ($this->db->next_record())
+                       {
+                               $dimb_list[] = Array(
+                                       'id'        => $this->db->f('id'),
+                                       'num'       => $this->db->f('name')
+                               );
+                       }
+                       return $dimb_list;
+               }
+
+       //-------------------
+               function select_dimd_list()
+               {
+                       $this->db->query("SELECT * FROM fm_ecodimd order by id 
asc ");
+                       while ($this->db->next_record())
+                       {
+                               $dimd_list[] = Array(
+                                       'id'        => $this->db->f('id'),
+                                       'num'       => $this->db->f('name')
+                               );
+                       }
+                       return $dimd_list;
+               }
+       //---------------------
+
+               function select_tax_code_list()
+               {
+                       $this->db->query("SELECT * FROM fm_ecomva order by id 
asc ");
+                       while ($this->db->next_record())
+                       {
+                               $tax_code_list[] = Array(
+                                       'id'        => $this->db->f('id'),
+                                       'num'       => $this->db->f('name')
+                               );
+                       }
+                       return $tax_code_list;
+               }
+
+               function get_invoice_user_list()
+               {
+
+                       $sql = "SELECT * FROM fm_acl2 $this->join 
phpgw_accounts on phpgw_accounts.account_id=fm_acl2.acl_account where 
acl_location ='.invoice' and account_type='u' order by account_lastname";
+                       $this->db->query($sql,__LINE__,__FILE__);
+
+                       $employees[] = Array(
+                               'account_lid'       => 'all',
+                               'account_lastname'  => lang('All')
+                       );
+
+                       while ($this->db->next_record())
+                       {
+                               $employees[] = Array(
+                                       'account_id'        => 
$this->db->f('account_id'),
+                                       'account_lid'       => 
$this->db->f('account_lid'),
+                                       'account_type'      => 
$this->db->f('account_type'),
+                                       'account_firstname' => 
$this->db->f('account_firstname'),
+                                       'account_lastname'  => 
$this->db->f('account_lastname'),
+                                       'account_status'    => 
$this->db->f('account_status'),
+                                       'account_expires'   => 
$this->db->f('account_expires')
+                               );
+                       }
+//_debug_array($employees);
+                       return $employees;
+               }
+
+               function select_account_class()
+               {
+                       $sql = "SELECT DISTINCT grouping as id from 
fm_b_account ";
+                       $this->db->query($sql,__LINE__,__FILE__);
+
+                       while ($this->db->next_record())
+                       {
+                               $class[] = Array(
+                                       'id'        => $this->db->f('id')
+                               );
+                       }
+                       return $class;
+               }
+               function delete($bilagsnr)
+               {
+                       $this->db->query("DELETE FROM fm_ecobilag WHERE 
bilagsnr ='" . $bilagsnr  ."'",__LINE__,__FILE__);
+               }
+
+
+               function update_invoice($values)
+               {
+
+//_debug_array($values);
+                       foreach($values['counter'] as $n)
+                       {
+                               $local_error='';
+
+                               if($values['voucher_id'][$n])
+                               {
+                                       $voucher_id=$values['voucher_id'][$n];
+
+                                       
$check_value=array('voucher_id'=>$voucher_id,
+                                                       'sign_orig'             
=> $values['sign_orig'][$n],
+                                                       'sign'                  
=> $values['sign'][$n],
+                                                       'transfer'              
=> $values['transfer'][$n],
+                                                       'kreditnota'    => 
$values['kreditnota'][$n],
+                                                       'num_days'              
=> $values['num_days'][$n]);
+
+                                       
if($this->check_for_updates($check_value))
+                                       {
+
+                                               $check_count = 
$this->check_count($voucher_id);
+
+                                               if 
(!($check_count['dima_count'] == $values['invoice_count'][$n]))
+                                               {
+                                                       $receipt['error'][] = 
array('msg'=>lang('Dima is missing from sub invoice in:'). " 
".$values['voucher_id'][$n]);
+                                                       $local_error= true;
+                                               }
+
+                                               if 
(!($check_count['spbudact_code_count'] == $values['invoice_count'][$n]))
+                                               {
+                                                       $receipt['error'][] = 
array('msg'=>lang('Budget code is missing from sub invoice in :'). " 
".$values['voucher_id'][$n]);
+                                                       $local_error= true;
+                                               }
+
+                                               if 
(!($check_count['kostra_count'] == $values['invoice_count'][$n]))
+                                               {
+                                                       $receipt['error'][] = 
array('msg'=>'Tjenestekode mangler for undebilag: ' . " 
".$values['voucher_id'][$n]);
+                                                       $local_error= true;
+                                               }
+
+                                               if 
($this->check_claim($voucher_id))
+                                               {
+                                                       $receipt['error'][] = 
array('msg'=>lang('Tenant claim is not issued for project in voucher 
%1',$voucher_id));
+                                                       $local_error= true;
+                                               }
+
+                                               $blank_date = '';
+                                               $sign_field='';
+                                               $sign_id='';
+                                               $sign_date_field='';
+                                               $sign_date='';
+                                               $kommma='';
+                                               $wait_for_kreditnota='';
+                                               $user_lid       
=$GLOBALS['phpgw_info']['user']['account_lid'];
+
+                                               if 
(($values['sign'][$n]=='sign_none') && 
($values['sign_orig'][$n]=='sign_janitor')):
+                                                       $blank_date = 
'oppsynsigndato= NULL';
+                                                       $sign_field='';
+                                                       $sign_id='';
+                                                       $sign_date_field='';
+                                                       $sign_date='';
+                                                       $kommma='';
+                                               elseif 
(($values['sign'][$n]=='sign_none') && 
($values['sign_orig'][$n]=='sign_supervisor')):
+                                                       $blank_date = 
'saksigndato= NULL';
+                                                       $sign_field='';
+                                                       $sign_id='';
+                                                       $sign_date_field='';
+                                                       $sign_date='';
+                                                       $kommma='';
+                                               elseif 
(($values['sign'][$n]=='sign_none') && 
($values['sign_orig'][$n]=='sign_budget_responsible')):
+                                                       $blank_date = 
'budsjettsigndato= NULL';
+                                                       $sign_field='';
+                                                       $sign_id='';
+                                                       $sign_date_field='';
+                                                       $sign_date='';
+                                                       $kommma='';
+                                               elseif 
($values['sign'][$n]=='sign_janitor' && !$values['sign_orig'][$n]):
+                                                       $blank_date = '';
+                                                       $sign_field = 
'oppsynsmannid=';
+                                                       $sign_id = 
"'$user_lid'";
+                                                       $sign_date_field = 
'oppsynsigndato=';
+                                                       $sign_date="'" . 
date($this->bocommon->datetimeformat) . "'";
+                                                       $kommma=",";
+                                               elseif 
($values['sign'][$n]=='sign_janitor' && 
$values['sign_orig'][$n]=='sign_supervisor'):
+                                                       $blank_date = 
'saksigndato= NULL';
+                                                       $sign_field = 
'oppsynsmannid=';
+                                                       $sign_id = 
"'$user_lid'";
+                                                       $sign_date_field = 
'oppsynsigndato=';
+                                                       $sign_date="'" . 
date($this->bocommon->datetimeformat) . "'";
+                                                       $kommma=",";
+                                               elseif 
($values['sign'][$n]=='sign_janitor' && 
$values['sign_orig'][$n]=='sign_budget_responsible'):
+                                                       $blank_date = 
'budsjettsigndato= NULL';
+                                                       $sign_field = 
'oppsynsmannid=';
+                                                       $sign_id = 
"'$user_lid'";
+                                                       $sign_date_field = 
'oppsynsigndato=';
+                                                       $sign_date="'" . 
date($this->bocommon->datetimeformat) . "'";
+                                                       $kommma=",";
+                                               elseif 
($values['sign'][$n]=='sign_supervisor' && !$values['sign_orig'][$n]):
+                                                       $blank_date = '';
+                                                       $sign_field = 
'saksbehandlerid=';
+                                                       $sign_id = 
"'$user_lid'";
+                                                       $sign_date_field = 
'saksigndato=';
+                                                       $sign_date="'" . 
date($this->bocommon->datetimeformat) . "'";
+                                                       $kommma=",";
+                                               elseif 
($values['sign'][$n]=='sign_supervisor' && 
$values['sign_orig'][$n]=='sign_janitor'):
+                                                       $blank_date = 
'oppsynsigndato= NULL';
+                                                       $sign_field = 
'saksbehandlerid=';
+                                                       $sign_id = 
"'$user_lid'";
+                                                       $sign_date_field = 
'saksigndato=';
+                                                       $sign_date="'" . 
date($this->bocommon->datetimeformat) . "'";
+                                                       $kommma=",";
+                                               elseif 
($values['sign'][$n]=='sign_supervisor' && 
$values['sign_orig'][$n]=='sign_budget_responsible'):
+                                                       $blank_date = 
'budsjettsigndato= NULL';
+                                                       $sign_field = 
'saksbehandlerid=';
+                                                       $sign_id = 
"'$user_lid'";
+                                                       $sign_date_field = 
'saksigndato=';
+                                                       $sign_date="'" . 
date($this->bocommon->datetimeformat) . "'";
+                                                       $kommma=",";
+                                               elseif 
($values['sign'][$n]=='sign_budget_responsible' && 
$values['sign_orig'][$n]=='sign_janitor'):
+                                                       $blank_date = 
'oppsynsigndato= NULL';
+                                                       $sign_field = 
'budsjettansvarligid=';
+                                                       $sign_id = 
"'$user_lid'";
+                                                       $sign_date_field = 
'budsjettsigndato=';
+                                                       $sign_date="'" . 
date($this->bocommon->datetimeformat) . "'";
+                                                       $kommma=",";
+                                               elseif 
($values['sign'][$n]=='sign_budget_responsible' && 
$values['sign_orig'][$n]=='sign_supervisor'):
+                                                       $blank_date = 
'saksigndato= NULL';
+                                                       $sign_field = 
'budsjettansvarligid=';
+                                                       $sign_id = 
"'$user_lid'";
+                                                       $sign_date_field = 
'budsjettsigndato=';
+                                                       $sign_date="'" . 
date($this->bocommon->datetimeformat) . "'";
+                                                       $kommma=",";
+                                               elseif 
($values['sign'][$n]=='sign_budget_responsible' && !$values['sign_orig'][$n]):
+                                                       $blank_date = '';
+                                                       $sign_field = 
'budsjettansvarligid=';
+                                                       $sign_id = 
"'$user_lid'";
+                                                       $sign_date_field = 
'budsjettsigndato=';
+                                                       $sign_date="'" . 
date($this->bocommon->datetimeformat) . "'";
+                                                       $kommma=",";
+                                               endif;
+
+
+                                               if($blank_date )
+                                               {
+                                                       $kommma_blank=",";
+                                               }
+                                               else
+                                               {
+                                                       $kommma_blank='';
+                                               }
+
+                                               
$transfer_sign_field='utbetalingid=';
+                                               
$transfer_date_field='utbetalingsigndato=';
+
+                                               if 
(!($values['num_days_orig'][$n]==$values['num_days'][$n]))
+                                               {
+                                                       $payment_date = 
date($this->bocommon->dateformat,$values['timestamp_voucher_date'][$n]+(24*3600*$values['num_days'][$n]));
+                                                       
$GLOBALS['phpgw']->db->query("UPDATE fm_ecobilag set forfallsdato= 
'$payment_date' where bilagsnr='$voucher_id'");
+                                               }
+
+                                               $transfer_id="Null".",";
+                                               $transfer_date="Null";
+
+                                               if ($values['transfer'][$n])
+                                               {
+                                                       if 
($this->check_for_transfer($voucher_id))
+                                                       {
+                                                               
$transfer_id="'$user_lid',";
+                                                               
$transfer_date="'" . date($this->bocommon->datetimeformat) . "'";
+                                                       }
+                                                       else
+                                                       {
+                                                               
$receipt['error'][] = array('msg'=>'Dette bilaget er ikkje godkjent: ' . " 
".$voucher_id);
+                                                               $local_error= 
true;
+                                                       }
+
+                                               }
+
+                                               if ($values['kreditnota'][$n])
+                                               {
+                                                       $wait_for_kreditnota=1;
+                                                       $transfer_date="Null";
+                                               }
+                                               else
+                                               {
+                                                       
$wait_for_kreditnota='NULL';
+                                               }
+
+                                               if (! $local_error)
+                                               {
+                                                       $sql= "UPDATE 
fm_ecobilag set $blank_date $kommma_blank $sign_field $sign_id $kommma 
$sign_date_field $sign_date $kommma $transfer_sign_field $transfer_id 
$transfer_date_field $transfer_date ,kreditnota=$wait_for_kreditnota  where 
bilagsnr='$voucher_id'";
+                                                       
$GLOBALS['phpgw']->db->transaction_begin();
+                                                       
$GLOBALS['phpgw']->db->query($sql);
+                                                       
$GLOBALS['phpgw']->db->transaction_commit();
+
+                                                       $receipt['message'][] = 
array('msg'=> lang('voucher is updated: ') . $voucher_id);
+                                               }
+                                       }
+                               }
+                       }
+
+                       $GLOBALS['phpgw']->db->query("UPDATE fm_ecobilag set 
utbetalingid = NULL, utbetalingsigndato = NULL WHERE budsjettsigndato IS NULL");
+                       $GLOBALS['phpgw']->db->query("UPDATE fm_ecobilag set 
utbetalingid = NULL, utbetalingsigndato = NULL WHERE oppsynsigndato IS NULL AND 
saksigndato IS NULL");
+
+                       return $receipt;
+               }
+
+               function check_for_transfer($voucher_id='')
+               {
+                       $allow_transfer=False;
+
+                       $sql = "SELECT * FROM fm_ecobilag WHERE 
bilagsnr='$voucher_id'";
+                       $this->db->limit_query($sql,0,__LINE__,__FILE__,1);
+
+                       $this->db->next_record();
+
+                       if ($this->db->f('budsjettsigndato') && 
($this->db->f('oppsynsigndato') || $this->db->f('saksigndato')))
+                       {
+                               $allow_transfer=True;
+                       }
+
+                       return $allow_transfer;
+               }
+
+               function check_claim($voucher_id='')
+               {
+                       $sql = "SELECT count(*) FROM fm_ecobilag 
$this->left_join fm_workorder on fm_ecobilag.pmwrkord_code = fm_workorder.id "
+                       . " WHERE bilagsnr='$voucher_id' AND 
fm_workorder.charge_tenant=1 AND fm_workorder.claim_issued IS NULL";
+                       $this->db->query($sql,__LINE__,__FILE__);
+                       $this->db->next_record();
+                       return $this->db->f(0);
+               }
+       }
+?>
Index: property/inc/class.solocation.inc.php
diff -u property/inc/class.solocation.inc.php:1.20 
property/inc/class.solocation.inc.php:1.21
--- property/inc/class.solocation.inc.php:1.20  Fri Jan 27 14:05:43 2006
+++ property/inc/class.solocation.inc.php       Mon Jan 30 22:14:19 2006
@@ -1,1255 +1,1255 @@
-<?php
-       /**
-       * phpGroupWare - property: a Facilities Management System.
-       *
-       * @author Sigurd Nes <address@hidden>
-       * @copyright Copyright (C) 2003-2005 Free Software Foundation, Inc. 
http://www.fsf.org/
-       * @license http://www.gnu.org/licenses/gpl.html GNU General Public 
License
-       * @internal Development of this application was funded by 
http://www.bergen.kommune.no/bbb_/ekstern/
-       * @package property
-       * @subpackage location
-       * @version $Id: class.solocation.inc.php,v 1.20 2006/01/27 14:05:43 
sigurdne Exp $
-       */
-
-       /**
-        * Description
-        * @package property
-        */
-
-       class solocation
-       {
-
-               function solocation()
-               {
-                       $this->currentapp       = 
$GLOBALS['phpgw_info']['flags']['currentapp'];
-
-                       $this->account  = 
$GLOBALS['phpgw_info']['user']['account_id'];
-                       $this->soadmin_location         = 
CreateObject($this->currentapp.'.soadmin_location');
-                       $this->bocommon         = 
CreateObject($this->currentapp.'.bocommon');
+<?php
+       /**
+       * phpGroupWare - property: a Facilities Management System.
+       *
+       * @author Sigurd Nes <address@hidden>
+       * @copyright Copyright (C) 2003-2005 Free Software Foundation, Inc. 
http://www.fsf.org/
+       * @license http://www.gnu.org/licenses/gpl.html GNU General Public 
License
+       * @internal Development of this application was funded by 
http://www.bergen.kommune.no/bbb_/ekstern/
+       * @package property
+       * @subpackage location
+       * @version $Id: class.solocation.inc.php,v 1.21 2006/01/30 22:14:19 
sigurdne Exp $
+       */
+
+       /**
+        * Description
+        * @package property
+        */
+
+       class solocation
+       {
+
+               function solocation()
+               {
+                       $this->currentapp       = 
$GLOBALS['phpgw_info']['flags']['currentapp'];
+
+                       $this->account  = 
$GLOBALS['phpgw_info']['user']['account_id'];
+                       $this->soadmin_location         = 
CreateObject($this->currentapp.'.soadmin_location');
+                       $this->bocommon         = 
CreateObject($this->currentapp.'.bocommon');
                        $this->db               = $this->bocommon->new_db();
                        $this->db2              = $this->bocommon->new_db();
-                       $this->socommon         = 
CreateObject($this->currentapp.'.socommon');
-
-                       $this->join             = $this->bocommon->join;
-                       $this->left_join        = $this->bocommon->left_join;
-                       $this->like             = $this->bocommon->like;
-               }
-
-               function read_entity_to_link()
-               {
-                       $sql = "SELECT * FROM fm_entity_category where 
loc_link=1";
-
-                       $this->db->query($sql,__LINE__,__FILE__);
-
-                       while ($this->db->next_record())
-                       {
-                               $entity[] = array
-                               (
-                                       'entity_id'     => 
$this->db->f('entity_id'),
-                                       'cat_id'        => $this->db->f('id'),
-                                       'name'          => $this->db->f('name'),
-                                       'descr'         => $this->db->f('descr')
-                               );
-                       }
-
-                       return $entity;
-               }
-
-               function select_status_list($type_id)
-               {
-                       if(!$type_id)
-                       {
-                               return;
-                       }
-
-                       $sql= "SELECT fm_location_choice.id, 
fm_location_choice.value FROM fm_location_attrib $this->join fm_location_choice 
ON "
-                       . " fm_location_attrib.type_id= 
fm_location_choice.type_id AND "
-                       . " fm_location_attrib.id= fm_location_choice.attrib_id 
"
-                       . " WHERE fm_location_attrib.column_name='status' "
-                       . " AND fm_location_choice.type_id=$type_id ORDER BY 
fm_location_choice.id";
-
-
-                       $this->db->query($sql,__LINE__,__FILE__);
-
-                       $i = 0;
-                       while ($this->db->next_record())
-                       {
-                               $status[$i]['id']                               
= $this->db->f('id');
-                               $status[$i]['name']                             
= stripslashes($this->db->f('value'));
-                               $i++;
-                       }
-                       return $status;
-               }
-
-
-               function get_owner_type_list()
-               {
-                       $this->db->query("SELECT id,descr FROM 
fm_owner_category  ORDER BY descr ");
-
-                       $i = 0;
-                       while ($this->db->next_record())
-                       {
-                               $owner_type[$i]['id']                   = 
$this->db->f('id');
-                               $owner_type[$i]['name']         = 
stripslashes($this->db->f('descr'));
-                               $i++;
-                       }
-                       return $owner_type;
-               }
-
-               function get_owner_list()
-               {
-                       $this->db->query("SELECT fm_owner.* 
,fm_owner_category.descr as category FROM fm_owner $this->join 
fm_owner_category on fm_owner.category=fm_owner_category.id  ORDER BY descr ");
-
-                       $i = 0;
-                       while ($this->db->next_record())
-                       {
-                               $owners[$i]['id']                       = 
$this->db->f('id');
-                               $owners[$i]['name']             = 
stripslashes($this->db->f('org_name')) . ' ['. $this->db->f('category') . ']';
-                               $i++;
-                       }
-                       return $owners;
-               }
-
-               function check_location($location_code='',$type_id='')
-               {
-                       $this->db->query("SELECT count(*) FROM 
fm_location$type_id where location_code='$location_code'");
-
-                       $this->db->next_record();
-
-                       if ( $this->db->f(0))
-                       {
-                               return True;
-                       }
-               }
-
-
-               function select_category_list($type_id='')
-               {
-                       if (!$type_id)
-                       {
-                               return;
-                       }
-
-                       $table= 'fm_location'. $type_id . '_category';
-                       $this->db->query("SELECT id, descr FROM $table where id 
>'0' ORDER BY id asc ");
-
-                       $i = 0;
-                       while ($this->db->next_record())
-                       {
-                               $categories[$i]['id']                           
= $this->db->f('id');
-                               $categories[$i]['name']                         
= stripslashes($this->db->f('descr')) . ' [' . $this->db->f('id') . ']';
-                               $i++;
-                       }
-                       return $categories;
-               }
-
-
-               function read($data)
-               {
-                       if(is_array($data))
-                       {
-                               if ($data['start'])
-                               {
-                                       $start=$data['start'];
-                               }
-                               else
-                               {
-                                       $start=0;
-                               }
-                               $filter = 
(isset($data['filter'])?$data['filter']:0);
-                               $query = 
(isset($data['query'])?$data['query']:'');
-                               $sort = 
(isset($data['sort'])?$data['sort']:'DESC');
-                               $order = 
(isset($data['order'])?$data['order']:'');
-                               $cat_id = 
(isset($data['cat_id'])?$data['cat_id']:0);
-                               $type_id = 
(isset($data['type_id'])?$data['type_id']:'');
-                               $lookup_tenant = 
(isset($data['lookup_tenant'])?$data['lookup_tenant']:'');
-                               $district_id = 
(isset($data['district_id'])?$data['district_id']:'');
-                               $allrows = 
(isset($data['allrows'])?$data['allrows']:'');
-                               $lookup = 
(isset($data['lookup'])?$data['lookup']:'');
-                               $status = 
(isset($data['status'])?$data['status']:'');
-                               $part_of_town_id = 
(isset($data['part_of_town_id'])?$data['part_of_town_id']:'');
-                       }
-
-                       if (!$type_id)
-                       {
-                               return;
-                       }
-
-                       $sql = $this->socommon->fm_cache('sql_'. $type_id . '_' 
. $lookup_tenant . '_' . $lookup);
-
-                       if(!$sql)
-                       {
-                               $location_types = 
$this->soadmin_location->select_location_type();
-
-                               $cols = "fm_location" . ($type_id) 
.".location_code";
-                               $cols_return[] = 'location_code';
-                               $uicols['input_type'][]         = 'hidden';
-                               $uicols['name'][]               = 
'location_code';
-                               $uicols['descr'][]              = 'dummy';
-                               $uicols['statustext'][]         = 'dummy';
-                               $uicols['exchange'][]           = false;
-
-                               for ($i=0; $i<($type_id); $i++)
-                               {
-                                       $uicols['input_type'][]         = 
'text';
-                                       $uicols['name'][]               = 'loc' 
. $location_types[$i]['id'];
-                                       $uicols['descr'][]              = 
$location_types[$i]['name'];
-                                       $uicols['statustext'][]         = 
$location_types[$i]['descr'];
-                                       $uicols['exchange'][]           = True;
-                                       $cols .= ",fm_location" . ($type_id) 
.".loc" . $location_types[$i]['id'];
-                                       $cols_return[] = 'loc' . 
$location_types[$i]['id'];
-                               }
-
-
-                               $street_level = 
$this->soadmin_location->read_config_single('street_id');
-                               if ($street_level > $type_id)
-                               {
-                                       $cols.= ',fm_location1.loc1_name as 
loc1_name';
-                                       $cols_return[]                          
= 'loc1_name';
-                                       $uicols['input_type'][]         = 
'text';
-                                       $uicols['name'][]                       
= 'loc1_name';
-                                       $uicols['descr'][]                      
= lang('Property Name');
-                                       $uicols['statustext'][]         = 
lang('Property Name');
-                                       $uicols['exchange'][]           = True;
-
-                                       if($type_id>1)
-                                       {
-                                               $cols.= ',fm_location' . 
$type_id . '.loc' . $type_id . '_name';
-                                               $cols_return[]                  
        = 'loc' . $type_id . '_name';
-                                               $uicols['input_type'][]         
= 'text';
-                                               $uicols['name'][]               
        = 'loc' . $type_id . '_name';
-                                               $uicols['descr'][]              
        = lang('Name');
-                                               $uicols['statustext'][]         
= lang('Name');
-                                               $uicols['exchange'][]           
= True;
-                                       }
-                               }
-
-/*                             for ($i=2;$i<($type_id+1);$i++)
-                               {
-                                       $cols.= ',fm_location' . $i . '.loc' . 
$i . '_name';
-                                       $cols_return[]                          
= 'loc' . $i . '_name';
-                                       $uicols['input_type'][]         = 
'text';
-                                       $uicols['name'][]                       
= 'loc' . $i . '_name';
-                                       $uicols['descr'][]                      
= '';
-                                       $uicols['statustext'][]         = '';
-                               }
-
-*/
-
-                               for ($j=($type_id-1); $j>0; $j--)
-                               {
-                                       $joinmethod .= " $this->join 
fm_location". ($j);
-
-                                       $paranthesis .='(';
-
-                                       $on = 'ON';
-                                       for ($i=($j); $i>0; $i--)
-                                       {
-                                               $joinmethod .= " $on 
(fm_location" . ($j+1) .".loc" . ($i). " = fm_location" . ($j) . ".loc" . ($i) 
. ")";
-                                               $on = 'AND';
-                                               if($i==1)
-                                               {
-                                                       $joinmethod .= ")";
-                                               }
-                                       }
-                               }
-
-                               $config = 
$this->soadmin_location->read_config('');
-
-//_debug_array($config);
-
-                               if($lookup_tenant)
-                               {
-                                       $cols.= ',fm_tenant.id as tenant_id';
-                                       $cols_return[]                          
= 'tenant_id';
-                                       $uicols['input_type'][]         = 
'hidden';
-                                       $uicols['name'][]                       
= 'tenant_id';
-                                       $uicols['descr'][]                      
= 'dummy';
-                                       $uicols['statustext'][]         = 
'dummy';
-                                       $uicols['exchange'][]           = True;
-
-                                       $cols.= ',fm_tenant.last_name';
-                                       $cols_return[]                          
= 'last_name';
-                                       $uicols['input_type'][]         = 
'text';
-                                       $uicols['name'][]                       
= 'last_name';
-                                       $uicols['descr'][]                      
= lang('last name');
-                                       $uicols['statustext'][]         = 
lang('last name');
-                                       $uicols['exchange'][]           = True;
-
-                                       $cols.= ',fm_tenant.first_name';
-                                       $cols_return[]                          
= 'first_name';
-                                       $uicols['input_type'][]         = 
'text';
-                                       $uicols['name'][]                       
= 'first_name';
-                                       $uicols['descr'][]                      
= lang('first name');
-                                       $uicols['statustext'][]         = 
lang('first name');
-                                       $uicols['exchange'][]           = True;
-
-                                       $cols.= ',fm_tenant.contact_phone';
-                                       $cols_return[]                          
= 'contact_phone';
-                                       $uicols['input_type'][]         = 
'text';
-                                       $uicols['name'][]                       
= 'contact_phone';
-                                       $uicols['descr'][]                      
= lang('contact phone');
-                                       $uicols['statustext'][]         = 
lang('contact phone');
-                                       $uicols['exchange'][]           = True;
-
-                                       $sub_query_tenant=1;
-                                       
$this->socommon->fm_cache('sub_query_tenant_'. $type_id  . '_' . $lookup_tenant 
. '_' . $lookup,$sub_query_tenant);
-                               }
-
-                               $config_count   = count($config);
-                               for ($i=0;$i<$config_count;$i++)
-                               {
-                                       if (($config[$i]['location_type'] <= 
$type_id) && ($config[$i]['f_key'] ==1))
-                                       {
-                                               if(!$lookup_tenant && 
$config[$i]['column_name']=='tenant_id')
-                                               {
-                                               }
-                                               else
-                                               {
-                                                       $joinmethod .= " 
$this->join  " . $config[$i]['reference_table'] . " ON ( fm_location" . 
$config[$i]['location_type'] . "." . $config[$i]['column_name'] . "=" . 
$config[$i]['reference_table'] . ".".$config[$i]['reference_id']."))";
-                                                       $paranthesis .='(';
-                                               }
-                                       }
-
-                                       if (($config[$i]['location_type'] <= 
$type_id)  && ($config[$i]['query_value'] ==1))
-                                       {
-
-                                               
if($config[$i]['column_name']=='street_id')
-                                               {
-
-                                                       $sub_query_street=1;
-                                                       
$this->socommon->fm_cache('sub_query_street_'. $type_id  . '_' . $lookup_tenant 
. '_' . $lookup,$sub_query_street);
-
-                                                       $cols.= 
',fm_streetaddress.descr as street_name';
-                                                       $cols_return[]          
                = 'street_name';
-                                                       $uicols['input_type'][] 
        = 'text';
-                                                       $uicols['name'][]       
                = 'street_name';
-                                                       $uicols['descr'][]      
                = lang('street name');
-                                                       $uicols['statustext'][] 
        = lang('street name');
-                                                       $uicols['exchange'][]   
        = True;
-
-                                                       $cols.= 
',street_number';
-                                                       $cols_return[]          
                = 'street_number';
-                                                       $uicols['input_type'][] 
        = 'text';
-                                                       $uicols['name'][]       
                = 'street_number';
-                                                       $uicols['descr'][]      
                = lang('street number');
-                                                       $uicols['statustext'][] 
        = lang('street number');
-                                                       $uicols['exchange'][]   
        = True;
-
-                                                       $cols.= ',fm_location' 
. $config[$i]['location_type'] . '.' . $config[$i]['column_name'];
-                                                       $cols_return[]          
                = $config[$i]['column_name'];
-                                                       $uicols['input_type'][] 
        = 'hidden';
-                                                       $uicols['name'][]       
                = $config[$i]['column_name'];
-                                                       $uicols['descr'][]      
                = lang($config[$i]['input_text']);
-                                                       $uicols['statustext'][] 
        = lang($config[$i]['input_text']);
-                                                       $uicols['exchange'][]   
        = True;
-                                               }
-                                               else
-                                               {
-                                                       $cols.= ',fm_location' 
. $config[$i]['location_type'] . '.' . $config[$i]['column_name'];
-                                                       $cols_return[]          
                = $config[$i]['column_name'];
-                                                       $uicols['input_type'][] 
        = 'hidden';
-                                                       $uicols['name'][]       
                = $config[$i]['column_name'];
-                                                       $uicols['descr'][]      
                = $config[$i]['input_text'];
-                                                       $uicols['statustext'][] 
        = $config[$i]['input_text'];
-                                                       $uicols['exchange'][]   
        = True;
-                                               }
-                                       }
-                               }
-
-                               $fm_location_cols = 
$this->soadmin_location->read_attrib(array('type_id'=>$type_id,'lookup_type'=>$type_id));
-
-//_debug_array($fm_location_cols);
-                               $k      = count($uicols['name']);
-                               $location_cols_count    = 
count($fm_location_cols);
-
-                               for ($i=0;$i<$location_cols_count;$i++)
-                               {
-                                       if($fm_location_cols[$i]['list']==1)
-                                       {
-                                               $cols .= ",fm_location" . 
$fm_location_cols[$i]['location_type'] 
.".".$fm_location_cols[$i]['column_name'];
-                                               $cols_return[]                  
        = $fm_location_cols[$i]['column_name'];
-                                               $uicols['input_type'][]         
= 'text';
-                                               $uicols['name'][]               
        = $fm_location_cols[$i]['column_name'];
-                                               $uicols['descr'][]              
        = $fm_location_cols[$i]['input_text'];
-                                               $uicols['statustext'][]         
= $fm_location_cols[$i]['statustext'];
-
-                                               
$uicols['cols_return_extra'][$k]= array(
-                                                       'id'    => $k,
-                                                       'name'  => 
$fm_location_cols[$i]['column_name'],
-                                                       'datatype'      => 
$fm_location_cols[$i]['datatype'],
-                                                       'attrib_id'     => 
$fm_location_cols[$i]['id']
-                                               );
-                                               $k++;
-
-                                               
if($fm_location_cols[$i]['lookup_form']==1)
-                                               {
-                                                       $uicols['exchange'][]   
        = True;
-                                               }
-                                               else
-                                               {
-                                                       $uicols['exchange'][]   
        = False;
-                                               }
-                                       }
-                               }
-
-
-                               $from .= " FROM $paranthesis 
fm_location$type_id ";
-
-                               $sql = "SELECT $cols $from $joinmethod";
-
-                               $this->socommon->fm_cache('sql_'. $type_id . 
'_' . $lookup_tenant . '_' . $lookup ,$sql);
-                               $this->socommon->fm_cache('uicols_'. $type_id  
. '_' . $lookup_tenant . '_' . $lookup,$uicols);
-                               $this->socommon->fm_cache('cols_return_'. 
$type_id  . '_' . $lookup_tenant . '_' . $lookup,$cols_return);
-
-                       }
-                       else
-                       {
-                               $uicols = $this->socommon->fm_cache('uicols_'. 
$type_id  . '_' . $lookup_tenant . '_' . $lookup);
-                               $cols_return = 
$this->socommon->fm_cache('cols_return_'. $type_id  . '_' . $lookup_tenant . 
'_' . $lookup);
-
-                               $sub_query_tenant       = 
$this->socommon->fm_cache('sub_query_tenant_'. $type_id  . '_' . $lookup_tenant 
. '_' . $lookup);
-                               $sub_query_street       = 
$this->socommon->fm_cache('sub_query_street_'. $type_id  . '_' . $lookup_tenant 
. '_' . $lookup);
-                       }
-
-                       $this->uicols = $uicols;
-
-                       $where= 'WHERE';
-
-                       if ($order)
-                       {
-                               $ordermethod = " order by $order $sort";
-                       }
-                       else
-                       {
-                               $ordermethod = ' order by fm_location' . 
($type_id) .'.location_code ASC';
-                       }
-
-                       if ($cat_id > 0)
-                       {
-                               $filtermethod .= " $where fm_location" . 
($type_id). ".category=$cat_id ";
-                               $where= 'AND';
-                       }
-                       else
-                       {
-                               $filtermethod .= " $where  (fm_location" . 
($type_id). ".category !=99 OR fm_location" . ($type_id). ".category IS NULL)";
-                               $where= 'AND';
-                       }
-
-                       if ($filter > 0)
-                       {
-                               $filtermethod .= " $where 
fm_owner.category='$filter' ";
-                               $where= 'AND';
-                       }
-
-
-                       if ($status > 0)
-                       {
-                               $filtermethod .= " $where fm_location" . 
($type_id). ".status=$status ";
-                               $where= 'AND';
-                       }
-                       else
-                       {
-//                             $filtermethod .= " $where fm_location" . 
($type_id). ".status IS NULL ";
-//                             $filtermethod .= " $where fm_location" . 
($type_id). ".status !=2 ";
-//                             $where= 'AND';
-                       }
-
-
-                       if ($district_id > 0)
-                       {
-                               $filtermethod .= " $where 
fm_part_of_town.district_id='$district_id' ";
-                               $where= 'AND';
-                       }
-
-                       if ($part_of_town_id > 0)
-                       {
-                               $filtermethod .= " $where 
fm_part_of_town.part_of_town_id='$part_of_town_id' ";
-                               $where= 'AND';
-                       }
-
-                       if($query)
-                       {
-                               $query = str_replace(",",'.',$query);
-                               if(stristr($query, '.'))
-                               {
-                                       $query=explode(".",$query);
-                                       $querymethod = " $where (fm_location" . 
($type_id).".loc1='" . $query[0] . "' AND fm_location" . $type_id .".loc" . 
($type_id)."='" . $query[1] . "')";
-                               }
-                               else
-                               {
-                                       $query = ereg_replace("'",'',$query);
-                                       $query = ereg_replace('"','',$query);
-
-                                       if($sub_query_tenant)
-                                       {
-                                               $sub_query = "OR 
fm_tenant.last_name $this->like '%$query%' OR fm_tenant.first_name $this->like 
'%$query%' OR fm_tenant.contact_phone $this->like '%$query%'";
-                                       }
-
-                                       if($sub_query_street)
-                                       {
-                                               $sub_query .= "OR 
fm_streetaddress.descr $this->like '%$query%'";
-                                       }
-
-                                       $querymethod = " $where (fm_location" . 
($type_id).".loc1 $this->like '%$query%' $sub_query OR fm_location" . 
($type_id).".location_code $this->like '%$query%' OR loc" . ($type_id)."_name 
$this->like '%$query%')";
-                               }
-                               $where= 'AND';
-                       }
-
-                       $sql .= "$filtermethod $querymethod";
-
-//echo $sql;
-                       $this->db2->query($sql,__LINE__,__FILE__);
-
-                       $this->total_records = $this->db2->num_rows();
-
-                       if(!$allrows)
-                       {
-                               $this->db->limit_query($sql . 
$ordermethod,$start,__LINE__,__FILE__);
-                       }
-                       else
-                       {
-                               $this->db->query($sql . 
$ordermethod,__LINE__,__FILE__);
-                       }
-
-                       $j=0;
-                       $cols_return_count      = count($cols_return);
-                       $location_count = $type_id-1;
-
-                       while ($this->db->next_record())
-                       {
-                               for ($i=0;$i<$cols_return_count;$i++)
-                               {
-                                       $location_list[$j][$cols_return[$i]] = 
stripslashes($this->db->f($cols_return[$i]));
-
-                                       $value = $this->db->f($cols_return[$i]);
-                                       if($uicols['cols_return_extra'][$i])
-                                       {
-                                               
if(($uicols['cols_return_extra'][$i]['datatype']=='R' || 
$uicols['cols_return_extra'][$i]['datatype']=='LB') && $value):
-                                               {
-                                                       $sql="SELECT value FROM 
fm_location_choice where type_id=$type_id AND attrib_id=" 
.$uicols['cols_return_extra'][$i]['attrib_id']. "  AND id=" . $value;
-                                                       $this->db2->query($sql);
-                                                       
$this->db2->next_record();
-                                                       
$location_list[$j][$cols_return[$i]] = $this->db2->f('value');
-                                               }
-                                               
elseif($uicols['cols_return_extra'][$i]['datatype']=='AB' && $value):
-                                               {
-                                                       $contact_data   = 
$contacts->read_single_entry($value,array('n_given'=>'n_given','n_family'=>'n_family','email'=>'email'));
-                                                       
$location_list[$j][$cols_return[$i]]    = $contact_data[0]['n_family'] . ', ' . 
$contact_data[0]['n_given'];
-                                               }
-                                               
elseif($uicols['cols_return_extra'][$i]['datatype']=='VENDOR' && $value):
-                                               {
-                                                       $sql="SELECT org_name 
FROM fm_vendor where id=$value";
-                                                       $this->db2->query($sql);
-                                                       
$this->db2->next_record();
-                                                       
$location_list[$j][$cols_return[$i]] = $this->db2->f('org_name');
-                                               }
-                                               
elseif($uicols['cols_return_extra'][$i]['datatype']=='CH' && $value):
-                                               {
-                                                       $ch= 
unserialize($value);
-
-                                                       if (isset($ch) AND 
is_array($ch))
-                                                       {
-                                                               for 
($k=0;$k<count($ch);$k++)
-                                                               {
-                                                                       
$sql="SELECT value FROM fm_location_choice where type_id=$type_id AND 
attrib_id=" .$uicols['cols_return_extra'][$i]['attrib_id']. "  AND id=" . 
$ch[$k];
-                                                                       
$this->db2->query($sql);
-                                                                       while 
($this->db2->next_record())
-                                                                       {
-                                                                               
$ch_value[]=$this->db2->f('value');
-                                                                       }
-                                                               }
-                                                               
$location_list[$j][$cols_return[$i]] = @implode(",", $ch_value);
-                                                               
unset($ch_value);
-                                                       }
-                                               }
-                                               
elseif($uicols['cols_return_extra'][$i]['datatype']=='D' && $value):
-                                               {
-                                                       
$location_list[$j][$cols_return[$i]]=date($GLOBALS['phpgw_info']['user']['preferences']['common']['dateformat'],strtotime($value));
-                                               }
-                                               endif;
-                                       }
-                                       unset($value);
-                               }
-
-                               $location_code= $this->db->f('location_code');
-                               $location = split('-',$location_code);
-                               for ($m=0;$m<$location_count;$m++)
-                               {
-                                       $location_list[$j]['loc' . ($m+1)] = 
$location[$m];
-                                       
$location_list[$j]['query_location']['loc' . ($m+1)]=implode("-", 
array_slice($location, 0, ($m+1)));
-                               }
-
-                               $j++;
-                       }
-
-                       return $location_list;
-               }
-
-
-               function 
generate_sql($type_id='',$cols='',$cols_return='',$uicols='',$read_single='')
-               {
-
-                       $joinmethod .= " fm_location" . ($type_id);
-
-                       $location_types = 
$this->soadmin_location->select_location_type();
-
-                       $cols .= "fm_location" . ($type_id) .".location_code";
-                       $cols_return[] = 'location_code';
-                       for ($i=0; $i<($type_id); $i++)
-                       {
-                               $uicols['input_type'][]         = 'text';
-                               $uicols['name'][]                       = 'loc' 
. $location_types[$i]['id'];
-                               $uicols['descr'][]                      = 
$location_types[$i]['name'];
-                               $uicols['statustext'][]         = 
$location_types[$i]['descr'];
-                               $cols .= ",fm_location" . ($type_id) .".loc" . 
$location_types[$i]['id'];
-                               $cols_return[] = 'loc' . 
$location_types[$i]['id'];
-                       }
-
-
-                       if($type!=1)
-                       {
-//                             $cols.= ',fm_location1.loc1_name as loc1_name';
-//                             $cols_return[]                          = 
'loc1_name';
-                               $uicols['input_type'][]         = 'text';
-                               $uicols['name'][]                       = 
'loc1_name';
-                               $uicols['descr'][]                      = 
lang('Property Name');
-                               $uicols['statustext'][]         = 
lang('Property Name');
-                       }
-
-                       for ($j=($type_id-1); $j>0; $j--)
-                       {
-                               $joinmethod .= " $this->join fm_location". ($j);
-
-                               $paranthesis .='(';
-
-
-                               $on = 'ON';
-                               for ($i=($j); $i>0; $i--)
-                               {
-                                       $joinmethod .= " $on (fm_location" . 
($j+1) .".loc" . ($i). " = fm_location" . ($j) . ".loc" . ($i) . ")";
-                                       $on = 'AND';
-                                       if($i==1)
-                                       {
-                                               $joinmethod .= ")";
-                                       }
-                               }
-                       }
-
-                       $config = $this->soadmin_location->read_config('');
-
-                       $config_count   = count($config);
-                       for ($i=0;$i<$config_count;$i++)
-                       {
-                               if (($config[$i]['location_type'] <= $type_id) 
&& ($config[$i]['f_key'] ==1))
-                               {
-
-                                       
if($config[$i]['column_name']=='tenant_id')
-                                       {
-                                               $join=$this->left_join;
-                                       }
-                                       else
-                                       {
-                                               $join =$this->join;
-                                       }
-
-                                       $joinmethod .= " $join  " . 
$config[$i]['reference_table'] . " ON ( fm_location" . 
$config[$i]['location_type'] . "." . $config[$i]['column_name'] . "=" . 
$config[$i]['reference_table'] . ".".$config[$i]['reference_id']."))";
-
-                                       $paranthesis .='(';
-                               }
-
-                               if ($config[$i]['location_type'] <= $type_id)
-                               {
-
-                                       
if($config[$i]['column_name']=='street_id'):
-                                       {
-                                               $cols.= 
',fm_streetaddress.descr as street_name';
-                                               $cols_return[]                  
        = 'street_name';
-                                               $uicols['input_type'][]         
= 'text';
-                                               $uicols['name'][]               
        = 'street_name';
-                                               $uicols['descr'][]              
        = lang('street name');
-                                               $uicols['statustext'][]         
= lang('street name');
-
-                                               $cols.= ',street_number';
-                                               $cols_return[]                  
        = 'street_number';
-                                               $uicols['input_type'][]         
= 'text';
-                                               $uicols['name'][]               
        = 'street_number';
-                                               $uicols['descr'][]              
        = lang('street number');
-                                               $uicols['statustext'][]         
= lang('street number');
-
-                                               $cols.= ',fm_location' . 
$config[$i]['location_type'] . '.' . $config[$i]['column_name'];
-                                               $cols_return[]                  
        = $config[$i]['column_name'];
-                                               $uicols['input_type'][]         
= 'hidden';
-                                               $uicols['name'][]               
        = $config[$i]['column_name'];
-                                               $uicols['descr'][]              
        = lang($config[$i]['input_text']);
-                                               $uicols['statustext'][]         
= lang($config[$i]['input_text']);
-
-                                       }
-                                       
elseif($config[$i]['column_name']=='tenant_id'):
-                                       {
-                                               $cols.= ',fm_tenant.id as 
tenant_id';
-                                               $cols_return[]                  
        = 'tenant_id';
-                                               $uicols['input_type'][]         
        = 'hidden';
-                                               $uicols['name'][]               
        = 'tenant_id';
-                                               $uicols['descr'][]              
        = 'dummy';
-                                               $uicols['statustext'][]         
= 'dummy';
-
-                                               $cols.= ',fm_tenant.last_name 
as last_name';
-                                               $cols_return[]                  
        = 'last_name';
-                                               $uicols['input_type'][]         
= 'text';
-                                               $uicols['name'][]               
        = 'last_name';
-                                               $uicols['descr'][]              
        = lang('last name');
-                                               $uicols['statustext'][]         
= lang('last name');
-
-                                               $cols.= ',fm_tenant.first_name 
as first_name';
-                                               $cols_return[]                  
        = 'first_name';
-                                               $uicols['input_type'][]         
= 'text';
-                                               $uicols['name'][]               
        = 'first_name';
-                                               $uicols['descr'][]              
        = lang('first name');
-                                               $uicols['statustext'][]         
= lang('first name');
-
-                                               $cols.= ',contact_phone';
-                                               $cols_return[]                  
        = 'contact_phone';
-                                               $uicols['input_type'][]         
= 'text';
-                                               $uicols['name'][]               
        = 'contact_phone';
-                                               $uicols['descr'][]              
        = lang('contact phone');
-                                               $uicols['statustext'][]         
= lang('contact phone');
-
-                                       }
-                                       else:
-                                       {
-                                               $cols.= ',fm_location' . 
$config[$i]['location_type'] . '.' . $config[$i]['column_name'];
-                                               $cols_return[]                  
        = $config[$i]['column_name'];
-                                               $uicols['input_type'][]         
= 'text';
-                                               $uicols['name'][]               
        = $config[$i]['column_name'];
-                                               $uicols['descr'][]              
        = $config[$i]['input_text'];
-                                               $uicols['statustext'][]         
= $config[$i]['input_text'];
-                                       }
-                                       endif;
-                               }
-                       }
-
-                       $fm_location_cols = 
$this->soadmin_location->read_attrib(array('type_id'=>$type_id,'lookup_type'=>$type_id,'allrows'=>True));
-//_debug_array($fm_location_cols);
-
-                       $location_cols_count    = count($fm_location_cols);
-
-                       for ($i=0;$i<$location_cols_count;$i++)
-                       {
-
-                               if($read_single)
-                               {
-                                       $cols .= ",fm_location" . 
$fm_location_cols[$i]['location_type'] 
.".".$fm_location_cols[$i]['column_name'];
-                                       $cols_return[]                          
= $fm_location_cols[$i]['column_name'];
-                                       $uicols['input_type'][]         = 
'text';
-                                       $uicols['name'][]                       
= $fm_location_cols[$i]['column_name'];
-                                       $uicols['descr'][]                      
= $fm_location_cols[$i]['input_text'];
-                                       $uicols['statustext'][]         = 
$fm_location_cols[$i]['statustext'];
-
-                               }
-                               else
-                               {
-                                       if($fm_location_cols[$i]['list']==1)
-                                       {
-                                               $cols .= ",fm_location" . 
($type_id) .".".$fm_location_cols[$i]['column_name'];
-                                               $cols_return[]                  
        = $fm_location_cols[$i]['column_name'];
-                                               $uicols['input_type'][]         
= 'text';
-                                               $uicols['name'][]               
        = $fm_location_cols[$i]['column_name'];
-                                               $uicols['descr'][]              
        = $fm_location_cols[$i]['input_text'];
-                                               $uicols['statustext'][]         
= $fm_location_cols[$i]['statustext'];
-                                       }
-                               }
-                       }
-
-
-                       $cols.= ',district_id';
-                       $cols_return[]  = 'district_id';
-
-                       $this->uicols           = $uicols;
-                       $this->cols_return      = $cols_return;
-
-                       $from .= " FROM $paranthesis $entity_table ";
-
-                       $sql = "SELECT $cols $from $joinmethod";
-
-                       $this->socommon->fm_cache('sql_single_'. $type_id,$sql);
-                       $this->socommon->fm_cache('uicols_single_'. 
$type_id,$uicols);
-                       $this->socommon->fm_cache('cols_return_single_'. 
$type_id,$cols_return);
-
-
-//                     $GLOBALS['phpgw']->session->appsession('sql_single_'. 
$type_id ,$this->currentapp,$sql);
-//                     
$GLOBALS['phpgw']->session->appsession('uicols_single_'. $type_id 
,$this->currentapp,$uicols);
-//                     
$GLOBALS['phpgw']->session->appsession('cols_return_single_'. $type_id 
,$this->currentapp,$cols_return);
-
-                       return $sql;
-               }
-
-               function read_single($location_code='')
-               {
-                       $location_array = split('-',$location_code);
-                       $type_id= count($location_array);
-
-                       if (!$type_id)
-                       {
-                               return;
-                       }
-
-                       $cols.= 'fm_location' . $type_id .'.category as 
cat_id,';
-                       $cols_return[]                          = 'cat_id';
-
-                       for ($i=1;$i<($type_id+1);$i++)
-                       {
-                               $cols.= 'fm_location' . $i .'.loc' . $i 
.'_name,';
-                               $cols_return[]                          = 'loc' 
. $i .'_name';
-                       }
-
-
-                       $cols.= 'fm_location' . $type_id . '.change_type,';
-                       $cols_return[]                          = 'change_type';
-                       $uicols['input_type'][]         = 'text';
-                       $uicols['name'][]                       = 'loc' . 
$type_id .'_name';
-                       $uicols['descr'][]                      = lang('name');
-                       $uicols['statustext'][]         = lang('name');
-
-                       $cols.= 'fm_location' . $type_id .'.remark as remark,';
-                       $cols_return[]                          = 'remark';
-                       $uicols['input_type'][]         = 'text';
-                       $uicols['name'][]                       = 'descr';
-                       $uicols['descr'][]                      = 
lang('remark');
-                       $uicols['statustext'][]         = lang('remark');
-
-                       $sql = $this->socommon->fm_cache('sql_single_'. 
$type_id);
-
-                       if(!$sql)
-                       {
-                               $sql    = 
$this->generate_sql($type_id,$cols,$cols_return,$uicols,True);
-                       }
-                       else
-                       {
-
-                               $this->uicols   =       
$this->socommon->fm_cache('uicols_single_'. $type_id);
-                               $this->cols_return      =       
$this->socommon->fm_cache('cols_return_single_'. $type_id);
-                       }
-
-                       $sql .= " WHERE 
fm_location$type_id.location_code='$location_code' ";
-
-                       $this->db->query($sql,__LINE__,__FILE__);
-
-//echo $sql;
-                       $cols_return    = $this->cols_return;
-
-                       $cols_return_count      = count($cols_return);
-
-                       $this->db->next_record();
-                       for ($i=0;$i<$cols_return_count;$i++)
-                       {
-                               $location[$cols_return[$i]] = 
stripslashes($this->db->f($cols_return[$i]));
-                       }
-
-//_debug_array($cols_return);
-//_debug_array($location);
-                       return $location;
-               }
-
-               function add($location='',$values_attribute='',$type_id='')
-               {
-                       $location['remark'] = 
$this->db->db_addslashes($location['remark']);
-
-                       while (is_array($location) && list($input_name,$value) 
= each($location))
-                       {
-                               if($value)
-                               {
-                                       if($input_name=='cat_id')
-                                       {
-                                               $input_name='category';
-                                       }
-                                       $cols[] = $input_name;
-                                       $vals[] = $value;
-                               }
-                       }
-
-                       if (isset($values_attribute) AND 
is_array($values_attribute))
-                       {
-                               foreach($values_attribute as $entry)
-                               {
-                                       if($entry['value'])
-                                       {
-                                               $cols[] = $entry['name'];
-                                               $vals[] = $entry['value'];
-                                       }
-                               }
-                       }
-
-                       $cols   =implode(",", $cols) . ",entry_date,user_id";
-                       $vals   ="'" . implode("','", $vals) . "'," . "'" . 
time() . "','" . $this->account . "'";
-
-
-                       $sql    = "INSERT INTO fm_location$type_id ($cols) 
VALUES ($vals)";
-
-//echo $sql;
-                       $this->db->query($sql,__LINE__,__FILE__);
-
-                       $receipt['message'][] = array('msg'=>lang('Location %1 
has been saved',$location['location_code']));
-                       return $receipt;
-               }
-
-               function edit($location='',$values_attribute='',$type_id='')
-               {
-//_debug_array($values_attribute);
-                       $location['remark'] = 
$this->db->db_addslashes($location['remark']);
-
-                       while (is_array($location) && list($input_name,$value) 
= each($location))
-                       {
-                               if($value)
-                               {
-                                       if($input_name=='cat_id')
-                                       {
-                                               $input_name='category';
-                                       }
-                                       $value_set[$input_name] = $value;
-                               }
-                       }
-
-                       if (isset($values_attribute) AND 
is_array($values_attribute))
-                       {
-                               foreach($values_attribute as $entry)
-                               {
-                                       $value_set[$entry['name']]      = 
$entry['value'];
-                               }
-                       }
-
-                       $value_set['entry_date'] = time();
-
-                       $value_set      = 
$this->bocommon->validate_db_update($value_set);
-
-                       $sql = "SELECT * from fm_location$type_id where 
location_code ='" . $location['location_code'] . "'";
-                       $this->db->query($sql,__LINE__,__FILE__);
-                       $this->db->next_record();
-
-                       $metadata = $this->db->metadata('fm_location'.$type_id);
-
-                       for ($i=0; $i<count($metadata); $i++)
-                       {
-                               $cols[] = $metadata[$i]['name'];
-                               $vals[] = $this->db->f($metadata[$i]['name']);
-                       }
-
-                       $cols[] = 'exp_date';
-                       $vals[] = date($this->bocommon->datetimeformat,time());
-
-                       $cols   =implode(",", $cols);
-                       $vals = $this->bocommon->validate_db_insert($vals);
-
-                       $sql = "INSERT INTO fm_location" . $type_id ."_history 
($cols) VALUES ($vals)";
-                       $this->db->query($sql,__LINE__,__FILE__);
-
-                       $sql = "UPDATE fm_location$type_id SET $value_set WHERE 
location_code='" . $location['location_code'] . "'";
-
-                       $this->db->query($sql,__LINE__,__FILE__);
-
-                       $receipt['message'][] = array('msg'=>lang('Location %1 
has been edited',$location['location_code']));
-                       return $receipt;
-               }
-
-               function delete($location_code )
-               {
-                       $location_array = split('-',$location_code);
-                       $type_id= count($location_array);
-
-                       $this->db->query("DELETE FROM fm_location$type_id WHERE 
location_code='" . $location_code ."'",__LINE__,__FILE__);
-//                     $this->db->query("DELETE FROM fm_location WHERE 
location_code='" . $location_code ."'",__LINE__,__FILE__);
-               }
-
-               function update_cat()
-               {
-                       $location_types = 
$this->soadmin_location->select_location_type();
-
-                       $m= count($location_types);
-
-                       $this->db->transaction_begin();
-
-                       $this->db->query("UPDATE fm_location" . $m. " set       
status= 2  WHERE category=99",__LINE__,__FILE__);
-
-                       for ($type_id=$m; $type_id>1; $type_id--)
-                       {
-                               $parent_table = 'fm_location' . ($type_id-1);
-
-                               $joinmethod .= " $this->join $parent_table";
-
-                               $paranthesis .='(';
-
-                               $on = 'ON';
-                               for ($i=($type_id-1); $i>0; $i--)
-                               {
-                                       $joinmethod .= " $on (fm_location" . 
($type_id) .".loc" . ($i). ' = '.$parent_table . ".loc" . ($i) . ")";
-                                       $on = 'AND';
-                                       if($i==1)
-                                       {
-                                               $joinmethod .= ")";
-                                       }
-                               }
-
-                               $sql = "SELECT $parent_table.location_code 
,count(*) as count_99  FROM $paranthesis fm_location$type_id $joinmethod where 
fm_location$type_id.status=2 group by $parent_table.location_code ";
-                               $this->db->query($sql,__LINE__,__FILE__);
-
-                               while ($this->db->next_record())
-                               {
-                                       
$outdated[$this->db->f('location_code')]['count_99']=$this->db->f('count_99');
-                               }
-
-                               $sql = "SELECT $parent_table.location_code 
,count(*) as count_all  FROM $paranthesis fm_location$type_id $joinmethod group 
by $parent_table.location_code ";
-                               $this->db->query($sql,__LINE__,__FILE__);
-                               while ($this->db->next_record())
-                               {
-                                       if( 
$outdated[$this->db->f('location_code')]['count_99']==$this->db->f('count_all'))
-                                       {
-                                               $update[]=array('location_code' 
=> $this->db->f('location_code'));
-                                       }
-                               }
-
-                               $j=0;
-                               for ($i=0; $i<count($update); $i++)
-                               {
-
-                                       $sql = "SELECT status  FROM 
$parent_table WHERE location_code= '" . $update[$i]['location_code'] ."'";
-
-                                       
$this->db->query($sql,__LINE__,__FILE__);
-                                       $this->db->next_record();
-
-                                       if($this->db->f('status')!=2)
-                                       {
-                                               $j++;
-                                               $this->db->query("UPDATE 
fm_location" . ($type_id-1). " set     status= 2  WHERE location_code= '" . 
$update[$i]['location_code'] ."'",__LINE__,__FILE__);
-                                       }
-                               }
-
-                               $receipt['message'][]=array('msg'=>lang('%1 
location %2 has been updated to not active of %3 already not 
active',$j,$location_types[($type_id-2)]['descr'],count($update)));
-
-                               unset($outdated);
-                               unset($update);
-                               unset($joinmethod);
-                               unset($paranthesis);
-                       }
-
-                       $this->db->transaction_commit();
-
-                       return $receipt;
-               }
-
-               function read_summary($data='')
-               {
-                       if(is_array($data))
-                       {
-                               $filter = 
(isset($data['filter'])?$data['filter']:0);
-                               $type_id = 
(isset($data['type_id'])?$data['type_id']:'');
-                               $district_id = 
(isset($data['district_id'])?$data['district_id']:'');
-                               $part_of_town_id = 
(isset($data['part_of_town_id'])?$data['part_of_town_id']:'');
-                       }
-
-                       if(!$type_id)
-                       {
-                               $type_id=4;
-                       }
-
-                       $entity_table = 'fm_location' . $type_id ;
-
-                       $cols= "count(*) as number, $entity_table.category, 
$entity_table"."_category.descr as type";
-
-                       $groupmethod = " GROUP by $entity_table.category , 
$entity_table"."_category.descr";
-
-                       $uicols['name'][]       = 'type';
-                       $uicols['descr'][]      = lang('type');
-
-
-                       $where = 'WHERE';
-                       if($district_id>0)
-                       {
-                               $uicols['name'][]       = 'district_id';
-                               $uicols['descr'][]      = lang('district_id');
-                               $cols.=", fm_part_of_town.district_id as 
district_id";
-                               $groupmethod .= " ,fm_part_of_town.district_id";
-                               $filtermethod = " $where 
fm_part_of_town.district_id=$district_id";
-                               $where = 'AND';
-                       }
-
-                       if($part_of_town_id>0)
-                       {
-                               $uicols['name'][]       = 'part_of_town';
-                               $uicols['descr'][]      = lang('part of town');
-                               $groupmethod .= " ,fm_part_of_town.name";
-                               $cols.=", fm_part_of_town.name as part_of_town";
-                               $filtermethod .= " $where 
fm_part_of_town.part_of_town_id=$part_of_town_id";
-                               $where = 'AND';
-                       }
-
-                       if($filter>0)
-                       {
-                               $filtermethod .= " $where fm_owner.id=$filter";
-                               $where = 'AND';
-                       }
-
-
-                       $uicols['name'][]       = 'number';
-                       $uicols['descr'][]      = lang('number');
-
-                       $joinmethod= "$this->join $entity_table"."_category on 
$entity_table.category=$entity_table"."_category.id";
-
-                       $sql = 
$this->bocommon->generate_sql(array('entity_table'=>$entity_table,'cols_return'=>$cols_return,'cols'=>$cols,
-                                                               
'uicols'=>$uicols,'joinmethod'=>$joinmethod,'paranthesis'=>$paranthesis,'query'=>$query,'no_address'=>True,'location_level'=>$type_id));
-
-                       $this->db->query($sql . $filtermethod . $groupmethod . 
" ORDER BY $entity_table.category",__LINE__,__FILE__);
-
-                       while ($this->db->next_record())
-                       {
-                               $summary[]=array(
-                                       'number'                => 
$this->db->f('number'),
-                                       'type'                  => '[' . 
$this->db->f('category') . '] ' .$this->db->f('type'),
-                                       'part_of_town'  => 
$this->db->f('part_of_town'),
-                                       'district_id'   => 
$this->db->f('district_id')
-                                       );
-                       }
-
-
-                       $this->uicols           = $uicols;
-                       return $summary;
-               }
-
-               function check_history($location_code='')
-               {
-                       $location_array = split('-',$location_code);
-                       $type_id= count($location_array);
-
-                       if (!$type_id)
-                       {
-                               return false;
-                       }
-
-                       $table = 'fm_location' . $type_id . '_history';
-
-                       $sql = "SELECT count(*) FROM $table WHERE 
location_code='$location_code'";
-
-                       $this->db->query($sql,__LINE__,__FILE__);
-
-                       $this->db->next_record();
-
-                       if($this->db->f('0')>0)
-                       {
-                               return True;
-                       }
-                       else
-                       {
-                               return;
-                       }
-               }
-
-               function get_history($location_code='')
-               {
-                       $this->uicols = array();
-                       $location_array = split('-',$location_code);
-                       $type_id= count($location_array);
-
-                       if (!$type_id)
-                       {
-                               return;
-                       }
-
-                       $table = 'fm_location' . $type_id . '_history';
-
-                       $table_category = 'fm_location' . $type_id . 
'_category';
-
-                       $sql = "SELECT column_name,datatype,input_text,id as 
attrib_id FROM fm_location_attrib WHERE type_id = $type_id";
-
-                       $this->db->query($sql,__LINE__,__FILE__);
-                       while ($this->db->next_record())
-                       {
-                               $attrib[] = array(
-                                       'column_name' => 
$this->db->f('column_name'),
-                                       'input_text' => 
$this->db->f('input_text'),
-                                       'datatype' => $this->db->f('datatype'),
-                                       'attrib_id' => $this->db->f('attrib_id')
-                               );
-
-                               $this->uicols['input_type'][] = 'text';
-                               $this->uicols['name'][] = 
$this->db->f('column_name');
-                               $this->uicols['descr'][] = 
$this->db->f('input_text');
-                       }
-
-                       $this->uicols['input_type'][] = 'text';
-                       $this->uicols['name'][] = 'exp_date';
-                       $this->uicols['descr'][] = lang('exp date');
-
-
-                       $attrib[] = array(
-                               'column_name' => 'exp_date',
-                               'input_text' => 'exp date',
-                               'datatype' => 'D'
-                       );
-
-                       $sql = "SELECT $table.*, $table_category.descr as 
category FROM $table $this->left_join $table_category ON $table.category 
=$table_category.id WHERE location_code='$location_code' ORDER BY exp_date 
DESC";
-                       $this->db->query($sql,__LINE__,__FILE__);
-
-                       $j=0;
-                       while ($this->db->next_record())
-                       {
-                               for ($i=0; $i<count($attrib); $i++)
-                               {
-                                       
$location[$j][$attrib[$i]['column_name']]=$this->db->f($attrib[$i]['column_name']);
-
-                                       $value = 
$this->db->f($attrib[$i]['column_name']);
-                                       if(($attrib[$i]['datatype']=='R' || 
$attrib[$i]['datatype']=='LB') && $value):
-                                       {
-                                               $sql="SELECT value FROM 
fm_location_choice where type_id=$type_id AND attrib_id=" 
.$attrib[$i]['attrib_id']. "  AND id=" . $value;
-                                               $this->db2->query($sql);
-                                               $this->db2->next_record();
-                                               
$location[$j][$attrib[$i]['column_name']] = $this->db2->f('value');
-                                       }
-                                       elseif($attrib[$i]['datatype']=='AB' && 
$value):
-                                       {
-                                               $contact_data   = 
$contacts->read_single_entry($value,array('n_given'=>'n_given','n_family'=>'n_family','email'=>'email'));
-                                               
$location[$j][$attrib[$i]['column_name']]       = $contact_data[0]['n_family'] 
. ', ' . $contact_data[0]['n_given'];
-                                       }
-                                       
elseif($attrib[$i]['datatype']=='VENDOR' && $value):
-                                       {
-                                               $sql="SELECT org_name FROM 
fm_vendor where id=$value";
-                                               $this->db2->query($sql);
-                                               $this->db2->next_record();
-                                               
$location[$j][$attrib[$i]['column_name']] = $this->db2->f('org_name');
-                                       }
-                                       elseif($attrib[$i]['datatype']=='CH' && 
$value):
-                                       {
-                                               $ch= unserialize($value);
-                                               if (isset($ch) AND 
is_array($ch))
-                                               {
-                                                       for 
($k=0;$k<count($ch);$k++)
-                                                       {
-                                                               $sql="SELECT 
value FROM fm_location_choice where type_id=$type_id AND attrib_id=" 
.$attrib[$i]['attrib_id']. "  AND id=" . $ch[$k];
-                                                               
$this->db2->query($sql);
-                                                               while 
($this->db2->next_record())
-                                                               {
-                                                                       
$ch_value[]=$this->db2->f('value');
-                                                               }
-                                                       }
-                                                       
$location[$j][$attrib[$i]['column_name']] = @implode(",", $ch_value);
-                                                       unset($ch_value);
-                                               }
-                                       }
-                                       elseif($attrib[$i]['datatype']=='D' && 
$value):
-                                       {
-                                               
$location[$j][$attrib[$i]['column_name']]=date($GLOBALS['phpgw_info']['user']['preferences']['common']['dateformat'],strtotime($value));
-                                       }
-                                       
elseif($attrib[$i]['column_name']=='entry_date' && $value):
-                                       {
-                                               
$location[$j][$attrib[$i]['column_name']]=date($GLOBALS['phpgw_info']['user']['preferences']['common']['dateformat'],$value);
-                                       }
-                                       endif;
-
-                                       unset($value);
-
-                               }
-                               $j++;
-                       }
-
-                       return $location;
-               }
-       }
-?>
+                       $this->socommon         = 
CreateObject($this->currentapp.'.socommon');
+
+                       $this->join             = $this->bocommon->join;
+                       $this->left_join        = $this->bocommon->left_join;
+                       $this->like             = $this->bocommon->like;
+               }
+
+               function read_entity_to_link()
+               {
+                       $sql = "SELECT * FROM fm_entity_category where 
loc_link=1";
+
+                       $this->db->query($sql,__LINE__,__FILE__);
+
+                       while ($this->db->next_record())
+                       {
+                               $entity[] = array
+                               (
+                                       'entity_id'     => 
$this->db->f('entity_id'),
+                                       'cat_id'        => $this->db->f('id'),
+                                       'name'          => $this->db->f('name'),
+                                       'descr'         => $this->db->f('descr')
+                               );
+                       }
+
+                       return $entity;
+               }
+
+               function select_status_list($type_id)
+               {
+                       if(!$type_id)
+                       {
+                               return;
+                       }
+
+                       $sql= "SELECT fm_location_choice.id, 
fm_location_choice.value FROM fm_location_attrib $this->join fm_location_choice 
ON "
+                       . " fm_location_attrib.type_id= 
fm_location_choice.type_id AND "
+                       . " fm_location_attrib.id= fm_location_choice.attrib_id 
"
+                       . " WHERE fm_location_attrib.column_name='status' "
+                       . " AND fm_location_choice.type_id=$type_id ORDER BY 
fm_location_choice.id";
+
+
+                       $this->db->query($sql,__LINE__,__FILE__);
+
+                       $i = 0;
+                       while ($this->db->next_record())
+                       {
+                               $status[$i]['id']                               
= $this->db->f('id');
+                               $status[$i]['name']                             
= stripslashes($this->db->f('value'));
+                               $i++;
+                       }
+                       return $status;
+               }
+
+
+               function get_owner_type_list()
+               {
+                       $this->db->query("SELECT id,descr FROM 
fm_owner_category  ORDER BY descr ");
+
+                       $i = 0;
+                       while ($this->db->next_record())
+                       {
+                               $owner_type[$i]['id']                   = 
$this->db->f('id');
+                               $owner_type[$i]['name']         = 
stripslashes($this->db->f('descr'));
+                               $i++;
+                       }
+                       return $owner_type;
+               }
+
+               function get_owner_list()
+               {
+                       $this->db->query("SELECT fm_owner.* 
,fm_owner_category.descr as category FROM fm_owner $this->join 
fm_owner_category on fm_owner.category=fm_owner_category.id  ORDER BY descr ");
+
+                       $i = 0;
+                       while ($this->db->next_record())
+                       {
+                               $owners[$i]['id']                       = 
$this->db->f('id');
+                               $owners[$i]['name']             = 
stripslashes($this->db->f('org_name')) . ' ['. $this->db->f('category') . ']';
+                               $i++;
+                       }
+                       return $owners;
+               }
+
+               function check_location($location_code='',$type_id='')
+               {
+                       $this->db->query("SELECT count(*) FROM 
fm_location$type_id where location_code='$location_code'");
+
+                       $this->db->next_record();
+
+                       if ( $this->db->f(0))
+                       {
+                               return True;
+                       }
+               }
+
+
+               function select_category_list($type_id='')
+               {
+                       if (!$type_id)
+                       {
+                               return;
+                       }
+
+                       $table= 'fm_location'. $type_id . '_category';
+                       $this->db->query("SELECT id, descr FROM $table where id 
>'0' ORDER BY id asc ");
+
+                       $i = 0;
+                       while ($this->db->next_record())
+                       {
+                               $categories[$i]['id']                           
= $this->db->f('id');
+                               $categories[$i]['name']                         
= stripslashes($this->db->f('descr')) . ' [' . $this->db->f('id') . ']';
+                               $i++;
+                       }
+                       return $categories;
+               }
+
+
+               function read($data)
+               {
+                       if(is_array($data))
+                       {
+                               if ($data['start'])
+                               {
+                                       $start=$data['start'];
+                               }
+                               else
+                               {
+                                       $start=0;
+                               }
+                               $filter = 
(isset($data['filter'])?$data['filter']:0);
+                               $query = 
(isset($data['query'])?$data['query']:'');
+                               $sort = 
(isset($data['sort'])?$data['sort']:'DESC');
+                               $order = 
(isset($data['order'])?$data['order']:'');
+                               $cat_id = 
(isset($data['cat_id'])?$data['cat_id']:0);
+                               $type_id = 
(isset($data['type_id'])?$data['type_id']:'');
+                               $lookup_tenant = 
(isset($data['lookup_tenant'])?$data['lookup_tenant']:'');
+                               $district_id = 
(isset($data['district_id'])?$data['district_id']:'');
+                               $allrows = 
(isset($data['allrows'])?$data['allrows']:'');
+                               $lookup = 
(isset($data['lookup'])?$data['lookup']:'');
+                               $status = 
(isset($data['status'])?$data['status']:'');
+                               $part_of_town_id = 
(isset($data['part_of_town_id'])?$data['part_of_town_id']:'');
+                       }
+
+                       if (!$type_id)
+                       {
+                               return;
+                       }
+
+                       $sql = $this->socommon->fm_cache('sql_'. $type_id . '_' 
. $lookup_tenant . '_' . $lookup);
+
+                       if(!$sql)
+                       {
+                               $location_types = 
$this->soadmin_location->select_location_type();
+
+                               $cols = "fm_location" . ($type_id) 
.".location_code";
+                               $cols_return[] = 'location_code';
+                               $uicols['input_type'][]         = 'hidden';
+                               $uicols['name'][]               = 
'location_code';
+                               $uicols['descr'][]              = 'dummy';
+                               $uicols['statustext'][]         = 'dummy';
+                               $uicols['exchange'][]           = false;
+
+                               for ($i=0; $i<($type_id); $i++)
+                               {
+                                       $uicols['input_type'][]         = 
'text';
+                                       $uicols['name'][]               = 'loc' 
. $location_types[$i]['id'];
+                                       $uicols['descr'][]              = 
$location_types[$i]['name'];
+                                       $uicols['statustext'][]         = 
$location_types[$i]['descr'];
+                                       $uicols['exchange'][]           = True;
+                                       $cols .= ",fm_location" . ($type_id) 
.".loc" . $location_types[$i]['id'];
+                                       $cols_return[] = 'loc' . 
$location_types[$i]['id'];
+                               }
+
+
+                               $street_level = 
$this->soadmin_location->read_config_single('street_id');
+                               if ($street_level > $type_id)
+                               {
+                                       $cols.= ',fm_location1.loc1_name as 
loc1_name';
+                                       $cols_return[]                          
= 'loc1_name';
+                                       $uicols['input_type'][]         = 
'text';
+                                       $uicols['name'][]                       
= 'loc1_name';
+                                       $uicols['descr'][]                      
= lang('Property Name');
+                                       $uicols['statustext'][]         = 
lang('Property Name');
+                                       $uicols['exchange'][]           = True;
+
+                                       if($type_id>1)
+                                       {
+                                               $cols.= ',fm_location' . 
$type_id . '.loc' . $type_id . '_name';
+                                               $cols_return[]                  
        = 'loc' . $type_id . '_name';
+                                               $uicols['input_type'][]         
= 'text';
+                                               $uicols['name'][]               
        = 'loc' . $type_id . '_name';
+                                               $uicols['descr'][]              
        = lang('Name');
+                                               $uicols['statustext'][]         
= lang('Name');
+                                               $uicols['exchange'][]           
= True;
+                                       }
+                               }
+
+/*                             for ($i=2;$i<($type_id+1);$i++)
+                               {
+                                       $cols.= ',fm_location' . $i . '.loc' . 
$i . '_name';
+                                       $cols_return[]                          
= 'loc' . $i . '_name';
+                                       $uicols['input_type'][]         = 
'text';
+                                       $uicols['name'][]                       
= 'loc' . $i . '_name';
+                                       $uicols['descr'][]                      
= '';
+                                       $uicols['statustext'][]         = '';
+                               }
+
+*/
+
+                               for ($j=($type_id-1); $j>0; $j--)
+                               {
+                                       $joinmethod .= " $this->join 
fm_location". ($j);
+
+                                       $paranthesis .='(';
+
+                                       $on = 'ON';
+                                       for ($i=($j); $i>0; $i--)
+                                       {
+                                               $joinmethod .= " $on 
(fm_location" . ($j+1) .".loc" . ($i). " = fm_location" . ($j) . ".loc" . ($i) 
. ")";
+                                               $on = 'AND';
+                                               if($i==1)
+                                               {
+                                                       $joinmethod .= ")";
+                                               }
+                                       }
+                               }
+
+                               $config = 
$this->soadmin_location->read_config('');
+
+//_debug_array($config);
+
+                               if($lookup_tenant)
+                               {
+                                       $cols.= ',fm_tenant.id as tenant_id';
+                                       $cols_return[]                          
= 'tenant_id';
+                                       $uicols['input_type'][]         = 
'hidden';
+                                       $uicols['name'][]                       
= 'tenant_id';
+                                       $uicols['descr'][]                      
= 'dummy';
+                                       $uicols['statustext'][]         = 
'dummy';
+                                       $uicols['exchange'][]           = True;
+
+                                       $cols.= ',fm_tenant.last_name';
+                                       $cols_return[]                          
= 'last_name';
+                                       $uicols['input_type'][]         = 
'text';
+                                       $uicols['name'][]                       
= 'last_name';
+                                       $uicols['descr'][]                      
= lang('last name');
+                                       $uicols['statustext'][]         = 
lang('last name');
+                                       $uicols['exchange'][]           = True;
+
+                                       $cols.= ',fm_tenant.first_name';
+                                       $cols_return[]                          
= 'first_name';
+                                       $uicols['input_type'][]         = 
'text';
+                                       $uicols['name'][]                       
= 'first_name';
+                                       $uicols['descr'][]                      
= lang('first name');
+                                       $uicols['statustext'][]         = 
lang('first name');
+                                       $uicols['exchange'][]           = True;
+
+                                       $cols.= ',fm_tenant.contact_phone';
+                                       $cols_return[]                          
= 'contact_phone';
+                                       $uicols['input_type'][]         = 
'text';
+                                       $uicols['name'][]                       
= 'contact_phone';
+                                       $uicols['descr'][]                      
= lang('contact phone');
+                                       $uicols['statustext'][]         = 
lang('contact phone');
+                                       $uicols['exchange'][]           = True;
+
+                                       $sub_query_tenant=1;
+                                       
$this->socommon->fm_cache('sub_query_tenant_'. $type_id  . '_' . $lookup_tenant 
. '_' . $lookup,$sub_query_tenant);
+                               }
+
+                               $config_count   = count($config);
+                               for ($i=0;$i<$config_count;$i++)
+                               {
+                                       if (($config[$i]['location_type'] <= 
$type_id) && ($config[$i]['f_key'] ==1))
+                                       {
+                                               if(!$lookup_tenant && 
$config[$i]['column_name']=='tenant_id')
+                                               {
+                                               }
+                                               else
+                                               {
+                                                       $joinmethod .= " 
$this->join  " . $config[$i]['reference_table'] . " ON ( fm_location" . 
$config[$i]['location_type'] . "." . $config[$i]['column_name'] . "=" . 
$config[$i]['reference_table'] . ".".$config[$i]['reference_id']."))";
+                                                       $paranthesis .='(';
+                                               }
+                                       }
+
+                                       if (($config[$i]['location_type'] <= 
$type_id)  && ($config[$i]['query_value'] ==1))
+                                       {
+
+                                               
if($config[$i]['column_name']=='street_id')
+                                               {
+
+                                                       $sub_query_street=1;
+                                                       
$this->socommon->fm_cache('sub_query_street_'. $type_id  . '_' . $lookup_tenant 
. '_' . $lookup,$sub_query_street);
+
+                                                       $cols.= 
',fm_streetaddress.descr as street_name';
+                                                       $cols_return[]          
                = 'street_name';
+                                                       $uicols['input_type'][] 
        = 'text';
+                                                       $uicols['name'][]       
                = 'street_name';
+                                                       $uicols['descr'][]      
                = lang('street name');
+                                                       $uicols['statustext'][] 
        = lang('street name');
+                                                       $uicols['exchange'][]   
        = True;
+
+                                                       $cols.= 
',street_number';
+                                                       $cols_return[]          
                = 'street_number';
+                                                       $uicols['input_type'][] 
        = 'text';
+                                                       $uicols['name'][]       
                = 'street_number';
+                                                       $uicols['descr'][]      
                = lang('street number');
+                                                       $uicols['statustext'][] 
        = lang('street number');
+                                                       $uicols['exchange'][]   
        = True;
+
+                                                       $cols.= ',fm_location' 
. $config[$i]['location_type'] . '.' . $config[$i]['column_name'];
+                                                       $cols_return[]          
                = $config[$i]['column_name'];
+                                                       $uicols['input_type'][] 
        = 'hidden';
+                                                       $uicols['name'][]       
                = $config[$i]['column_name'];
+                                                       $uicols['descr'][]      
                = lang($config[$i]['input_text']);
+                                                       $uicols['statustext'][] 
        = lang($config[$i]['input_text']);
+                                                       $uicols['exchange'][]   
        = True;
+                                               }
+                                               else
+                                               {
+                                                       $cols.= ',fm_location' 
. $config[$i]['location_type'] . '.' . $config[$i]['column_name'];
+                                                       $cols_return[]          
                = $config[$i]['column_name'];
+                                                       $uicols['input_type'][] 
        = 'hidden';
+                                                       $uicols['name'][]       
                = $config[$i]['column_name'];
+                                                       $uicols['descr'][]      
                = $config[$i]['input_text'];
+                                                       $uicols['statustext'][] 
        = $config[$i]['input_text'];
+                                                       $uicols['exchange'][]   
        = True;
+                                               }
+                                       }
+                               }
+
+                               $fm_location_cols = 
$this->soadmin_location->read_attrib(array('type_id'=>$type_id,'lookup_type'=>$type_id));
+
+//_debug_array($fm_location_cols);
+                               $k      = count($uicols['name']);
+                               $location_cols_count    = 
count($fm_location_cols);
+
+                               for ($i=0;$i<$location_cols_count;$i++)
+                               {
+                                       if($fm_location_cols[$i]['list']==1)
+                                       {
+                                               $cols .= ",fm_location" . 
$fm_location_cols[$i]['location_type'] 
.".".$fm_location_cols[$i]['column_name'];
+                                               $cols_return[]                  
        = $fm_location_cols[$i]['column_name'];
+                                               $uicols['input_type'][]         
= 'text';
+                                               $uicols['name'][]               
        = $fm_location_cols[$i]['column_name'];
+                                               $uicols['descr'][]              
        = $fm_location_cols[$i]['input_text'];
+                                               $uicols['statustext'][]         
= $fm_location_cols[$i]['statustext'];
+
+                                               
$uicols['cols_return_extra'][$k]= array(
+                                                       'id'    => $k,
+                                                       'name'  => 
$fm_location_cols[$i]['column_name'],
+                                                       'datatype'      => 
$fm_location_cols[$i]['datatype'],
+                                                       'attrib_id'     => 
$fm_location_cols[$i]['id']
+                                               );
+                                               $k++;
+
+                                               
if($fm_location_cols[$i]['lookup_form']==1)
+                                               {
+                                                       $uicols['exchange'][]   
        = True;
+                                               }
+                                               else
+                                               {
+                                                       $uicols['exchange'][]   
        = False;
+                                               }
+                                       }
+                               }
+
+
+                               $from .= " FROM $paranthesis 
fm_location$type_id ";
+
+                               $sql = "SELECT $cols $from $joinmethod";
+
+                               $this->socommon->fm_cache('sql_'. $type_id . 
'_' . $lookup_tenant . '_' . $lookup ,$sql);
+                               $this->socommon->fm_cache('uicols_'. $type_id  
. '_' . $lookup_tenant . '_' . $lookup,$uicols);
+                               $this->socommon->fm_cache('cols_return_'. 
$type_id  . '_' . $lookup_tenant . '_' . $lookup,$cols_return);
+
+                       }
+                       else
+                       {
+                               $uicols = $this->socommon->fm_cache('uicols_'. 
$type_id  . '_' . $lookup_tenant . '_' . $lookup);
+                               $cols_return = 
$this->socommon->fm_cache('cols_return_'. $type_id  . '_' . $lookup_tenant . 
'_' . $lookup);
+
+                               $sub_query_tenant       = 
$this->socommon->fm_cache('sub_query_tenant_'. $type_id  . '_' . $lookup_tenant 
. '_' . $lookup);
+                               $sub_query_street       = 
$this->socommon->fm_cache('sub_query_street_'. $type_id  . '_' . $lookup_tenant 
. '_' . $lookup);
+                       }
+
+                       $this->uicols = $uicols;
+
+                       $where= 'WHERE';
+
+                       if ($order)
+                       {
+                               $ordermethod = " order by $order $sort";
+                       }
+                       else
+                       {
+                               $ordermethod = ' order by fm_location' . 
($type_id) .'.location_code ASC';
+                       }
+
+                       if ($cat_id > 0)
+                       {
+                               $filtermethod .= " $where fm_location" . 
($type_id). ".category=$cat_id ";
+                               $where= 'AND';
+                       }
+                       else
+                       {
+                               $filtermethod .= " $where  (fm_location" . 
($type_id). ".category !=99 OR fm_location" . ($type_id). ".category IS NULL)";
+                               $where= 'AND';
+                       }
+
+                       if ($filter > 0)
+                       {
+                               $filtermethod .= " $where 
fm_owner.category='$filter' ";
+                               $where= 'AND';
+                       }
+
+
+                       if ($status > 0)
+                       {
+                               $filtermethod .= " $where fm_location" . 
($type_id). ".status=$status ";
+                               $where= 'AND';
+                       }
+                       else
+                       {
+//                             $filtermethod .= " $where fm_location" . 
($type_id). ".status IS NULL ";
+//                             $filtermethod .= " $where fm_location" . 
($type_id). ".status !=2 ";
+//                             $where= 'AND';
+                       }
+
+
+                       if ($district_id > 0)
+                       {
+                               $filtermethod .= " $where 
fm_part_of_town.district_id='$district_id' ";
+                               $where= 'AND';
+                       }
+
+                       if ($part_of_town_id > 0)
+                       {
+                               $filtermethod .= " $where 
fm_part_of_town.part_of_town_id='$part_of_town_id' ";
+                               $where= 'AND';
+                       }
+
+                       if($query)
+                       {
+                               $query = str_replace(",",'.',$query);
+                               if(stristr($query, '.'))
+                               {
+                                       $query=explode(".",$query);
+                                       $querymethod = " $where (fm_location" . 
($type_id).".loc1='" . $query[0] . "' AND fm_location" . $type_id .".loc" . 
($type_id)."='" . $query[1] . "')";
+                               }
+                               else
+                               {
+                                       $query = ereg_replace("'",'',$query);
+                                       $query = ereg_replace('"','',$query);
+
+                                       if($sub_query_tenant)
+                                       {
+                                               $sub_query = "OR 
fm_tenant.last_name $this->like '%$query%' OR fm_tenant.first_name $this->like 
'%$query%' OR fm_tenant.contact_phone $this->like '%$query%'";
+                                       }
+
+                                       if($sub_query_street)
+                                       {
+                                               $sub_query .= "OR 
fm_streetaddress.descr $this->like '%$query%'";
+                                       }
+
+                                       $querymethod = " $where (fm_location" . 
($type_id).".loc1 $this->like '%$query%' $sub_query OR fm_location" . 
($type_id).".location_code $this->like '%$query%' OR loc" . ($type_id)."_name 
$this->like '%$query%')";
+                               }
+                               $where= 'AND';
+                       }
+
+                       $sql .= "$filtermethod $querymethod";
+
+//echo $sql;
+                       $this->db2->query($sql,__LINE__,__FILE__);
+
+                       $this->total_records = $this->db2->num_rows();
+
+                       if(!$allrows)
+                       {
+                               $this->db->limit_query($sql . 
$ordermethod,$start,__LINE__,__FILE__);
+                       }
+                       else
+                       {
+                               $this->db->query($sql . 
$ordermethod,__LINE__,__FILE__);
+                       }
+
+                       $j=0;
+                       $cols_return_count      = count($cols_return);
+                       $location_count = $type_id-1;
+
+                       while ($this->db->next_record())
+                       {
+                               for ($i=0;$i<$cols_return_count;$i++)
+                               {
+                                       $location_list[$j][$cols_return[$i]] = 
stripslashes($this->db->f($cols_return[$i]));
+
+                                       $value = $this->db->f($cols_return[$i]);
+                                       if($uicols['cols_return_extra'][$i])
+                                       {
+                                               
if(($uicols['cols_return_extra'][$i]['datatype']=='R' || 
$uicols['cols_return_extra'][$i]['datatype']=='LB') && $value):
+                                               {
+                                                       $sql="SELECT value FROM 
fm_location_choice where type_id=$type_id AND attrib_id=" 
.$uicols['cols_return_extra'][$i]['attrib_id']. "  AND id=" . $value;
+                                                       $this->db2->query($sql);
+                                                       
$this->db2->next_record();
+                                                       
$location_list[$j][$cols_return[$i]] = $this->db2->f('value');
+                                               }
+                                               
elseif($uicols['cols_return_extra'][$i]['datatype']=='AB' && $value):
+                                               {
+                                                       $contact_data   = 
$contacts->read_single_entry($value,array('n_given'=>'n_given','n_family'=>'n_family','email'=>'email'));
+                                                       
$location_list[$j][$cols_return[$i]]    = $contact_data[0]['n_family'] . ', ' . 
$contact_data[0]['n_given'];
+                                               }
+                                               
elseif($uicols['cols_return_extra'][$i]['datatype']=='VENDOR' && $value):
+                                               {
+                                                       $sql="SELECT org_name 
FROM fm_vendor where id=$value";
+                                                       $this->db2->query($sql);
+                                                       
$this->db2->next_record();
+                                                       
$location_list[$j][$cols_return[$i]] = $this->db2->f('org_name');
+                                               }
+                                               
elseif($uicols['cols_return_extra'][$i]['datatype']=='CH' && $value):
+                                               {
+                                                       $ch= 
unserialize($value);
+
+                                                       if (isset($ch) AND 
is_array($ch))
+                                                       {
+                                                               for 
($k=0;$k<count($ch);$k++)
+                                                               {
+                                                                       
$sql="SELECT value FROM fm_location_choice where type_id=$type_id AND 
attrib_id=" .$uicols['cols_return_extra'][$i]['attrib_id']. "  AND id=" . 
$ch[$k];
+                                                                       
$this->db2->query($sql);
+                                                                       while 
($this->db2->next_record())
+                                                                       {
+                                                                               
$ch_value[]=$this->db2->f('value');
+                                                                       }
+                                                               }
+                                                               
$location_list[$j][$cols_return[$i]] = @implode(",", $ch_value);
+                                                               
unset($ch_value);
+                                                       }
+                                               }
+                                               
elseif($uicols['cols_return_extra'][$i]['datatype']=='D' && $value):
+                                               {
+                                                       
$location_list[$j][$cols_return[$i]]=date($GLOBALS['phpgw_info']['user']['preferences']['common']['dateformat'],strtotime($value));
+                                               }
+                                               endif;
+                                       }
+                                       unset($value);
+                               }
+
+                               $location_code= $this->db->f('location_code');
+                               $location = split('-',$location_code);
+                               for ($m=0;$m<$location_count;$m++)
+                               {
+                                       $location_list[$j]['loc' . ($m+1)] = 
$location[$m];
+                                       
$location_list[$j]['query_location']['loc' . ($m+1)]=implode("-", 
array_slice($location, 0, ($m+1)));
+                               }
+
+                               $j++;
+                       }
+
+                       return $location_list;
+               }
+
+
+               function 
generate_sql($type_id='',$cols='',$cols_return='',$uicols='',$read_single='')
+               {
+
+                       $joinmethod .= " fm_location" . ($type_id);
+
+                       $location_types = 
$this->soadmin_location->select_location_type();
+
+                       $cols .= "fm_location" . ($type_id) .".location_code";
+                       $cols_return[] = 'location_code';
+                       for ($i=0; $i<($type_id); $i++)
+                       {
+                               $uicols['input_type'][]         = 'text';
+                               $uicols['name'][]                       = 'loc' 
. $location_types[$i]['id'];
+                               $uicols['descr'][]                      = 
$location_types[$i]['name'];
+                               $uicols['statustext'][]         = 
$location_types[$i]['descr'];
+                               $cols .= ",fm_location" . ($type_id) .".loc" . 
$location_types[$i]['id'];
+                               $cols_return[] = 'loc' . 
$location_types[$i]['id'];
+                       }
+
+
+                       if($type!=1)
+                       {
+//                             $cols.= ',fm_location1.loc1_name as loc1_name';
+//                             $cols_return[]                          = 
'loc1_name';
+                               $uicols['input_type'][]         = 'text';
+                               $uicols['name'][]                       = 
'loc1_name';
+                               $uicols['descr'][]                      = 
lang('Property Name');
+                               $uicols['statustext'][]         = 
lang('Property Name');
+                       }
+
+                       for ($j=($type_id-1); $j>0; $j--)
+                       {
+                               $joinmethod .= " $this->join fm_location". ($j);
+
+                               $paranthesis .='(';
+
+
+                               $on = 'ON';
+                               for ($i=($j); $i>0; $i--)
+                               {
+                                       $joinmethod .= " $on (fm_location" . 
($j+1) .".loc" . ($i). " = fm_location" . ($j) . ".loc" . ($i) . ")";
+                                       $on = 'AND';
+                                       if($i==1)
+                                       {
+                                               $joinmethod .= ")";
+                                       }
+                               }
+                       }
+
+                       $config = $this->soadmin_location->read_config('');
+
+                       $config_count   = count($config);
+                       for ($i=0;$i<$config_count;$i++)
+                       {
+                               if (($config[$i]['location_type'] <= $type_id) 
&& ($config[$i]['f_key'] ==1))
+                               {
+
+                                       
if($config[$i]['column_name']=='tenant_id')
+                                       {
+                                               $join=$this->left_join;
+                                       }
+                                       else
+                                       {
+                                               $join =$this->join;
+                                       }
+
+                                       $joinmethod .= " $join  " . 
$config[$i]['reference_table'] . " ON ( fm_location" . 
$config[$i]['location_type'] . "." . $config[$i]['column_name'] . "=" . 
$config[$i]['reference_table'] . ".".$config[$i]['reference_id']."))";
+
+                                       $paranthesis .='(';
+                               }
+
+                               if ($config[$i]['location_type'] <= $type_id)
+                               {
+
+                                       
if($config[$i]['column_name']=='street_id'):
+                                       {
+                                               $cols.= 
',fm_streetaddress.descr as street_name';
+                                               $cols_return[]                  
        = 'street_name';
+                                               $uicols['input_type'][]         
= 'text';
+                                               $uicols['name'][]               
        = 'street_name';
+                                               $uicols['descr'][]              
        = lang('street name');
+                                               $uicols['statustext'][]         
= lang('street name');
+
+                                               $cols.= ',street_number';
+                                               $cols_return[]                  
        = 'street_number';
+                                               $uicols['input_type'][]         
= 'text';
+                                               $uicols['name'][]               
        = 'street_number';
+                                               $uicols['descr'][]              
        = lang('street number');
+                                               $uicols['statustext'][]         
= lang('street number');
+
+                                               $cols.= ',fm_location' . 
$config[$i]['location_type'] . '.' . $config[$i]['column_name'];
+                                               $cols_return[]                  
        = $config[$i]['column_name'];
+                                               $uicols['input_type'][]         
= 'hidden';
+                                               $uicols['name'][]               
        = $config[$i]['column_name'];
+                                               $uicols['descr'][]              
        = lang($config[$i]['input_text']);
+                                               $uicols['statustext'][]         
= lang($config[$i]['input_text']);
+
+                                       }
+                                       
elseif($config[$i]['column_name']=='tenant_id'):
+                                       {
+                                               $cols.= ',fm_tenant.id as 
tenant_id';
+                                               $cols_return[]                  
        = 'tenant_id';
+                                               $uicols['input_type'][]         
        = 'hidden';
+                                               $uicols['name'][]               
        = 'tenant_id';
+                                               $uicols['descr'][]              
        = 'dummy';
+                                               $uicols['statustext'][]         
= 'dummy';
+
+                                               $cols.= ',fm_tenant.last_name 
as last_name';
+                                               $cols_return[]                  
        = 'last_name';
+                                               $uicols['input_type'][]         
= 'text';
+                                               $uicols['name'][]               
        = 'last_name';
+                                               $uicols['descr'][]              
        = lang('last name');
+                                               $uicols['statustext'][]         
= lang('last name');
+
+                                               $cols.= ',fm_tenant.first_name 
as first_name';
+                                               $cols_return[]                  
        = 'first_name';
+                                               $uicols['input_type'][]         
= 'text';
+                                               $uicols['name'][]               
        = 'first_name';
+                                               $uicols['descr'][]              
        = lang('first name');
+                                               $uicols['statustext'][]         
= lang('first name');
+
+                                               $cols.= ',contact_phone';
+                                               $cols_return[]                  
        = 'contact_phone';
+                                               $uicols['input_type'][]         
= 'text';
+                                               $uicols['name'][]               
        = 'contact_phone';
+                                               $uicols['descr'][]              
        = lang('contact phone');
+                                               $uicols['statustext'][]         
= lang('contact phone');
+
+                                       }
+                                       else:
+                                       {
+                                               $cols.= ',fm_location' . 
$config[$i]['location_type'] . '.' . $config[$i]['column_name'];
+                                               $cols_return[]                  
        = $config[$i]['column_name'];
+                                               $uicols['input_type'][]         
= 'text';
+                                               $uicols['name'][]               
        = $config[$i]['column_name'];
+                                               $uicols['descr'][]              
        = $config[$i]['input_text'];
+                                               $uicols['statustext'][]         
= $config[$i]['input_text'];
+                                       }
+                                       endif;
+                               }
+                       }
+
+                       $fm_location_cols = 
$this->soadmin_location->read_attrib(array('type_id'=>$type_id,'lookup_type'=>$type_id,'allrows'=>True));
+//_debug_array($fm_location_cols);
+
+                       $location_cols_count    = count($fm_location_cols);
+
+                       for ($i=0;$i<$location_cols_count;$i++)
+                       {
+
+                               if($read_single)
+                               {
+                                       $cols .= ",fm_location" . 
$fm_location_cols[$i]['location_type'] 
.".".$fm_location_cols[$i]['column_name'];
+                                       $cols_return[]                          
= $fm_location_cols[$i]['column_name'];
+                                       $uicols['input_type'][]         = 
'text';
+                                       $uicols['name'][]                       
= $fm_location_cols[$i]['column_name'];
+                                       $uicols['descr'][]                      
= $fm_location_cols[$i]['input_text'];
+                                       $uicols['statustext'][]         = 
$fm_location_cols[$i]['statustext'];
+
+                               }
+                               else
+                               {
+                                       if($fm_location_cols[$i]['list']==1)
+                                       {
+                                               $cols .= ",fm_location" . 
($type_id) .".".$fm_location_cols[$i]['column_name'];
+                                               $cols_return[]                  
        = $fm_location_cols[$i]['column_name'];
+                                               $uicols['input_type'][]         
= 'text';
+                                               $uicols['name'][]               
        = $fm_location_cols[$i]['column_name'];
+                                               $uicols['descr'][]              
        = $fm_location_cols[$i]['input_text'];
+                                               $uicols['statustext'][]         
= $fm_location_cols[$i]['statustext'];
+                                       }
+                               }
+                       }
+
+
+                       $cols.= ',district_id';
+                       $cols_return[]  = 'district_id';
+
+                       $this->uicols           = $uicols;
+                       $this->cols_return      = $cols_return;
+
+                       $from .= " FROM $paranthesis $entity_table ";
+
+                       $sql = "SELECT $cols $from $joinmethod";
+
+                       $this->socommon->fm_cache('sql_single_'. $type_id,$sql);
+                       $this->socommon->fm_cache('uicols_single_'. 
$type_id,$uicols);
+                       $this->socommon->fm_cache('cols_return_single_'. 
$type_id,$cols_return);
+
+
+//                     $GLOBALS['phpgw']->session->appsession('sql_single_'. 
$type_id ,$this->currentapp,$sql);
+//                     
$GLOBALS['phpgw']->session->appsession('uicols_single_'. $type_id 
,$this->currentapp,$uicols);
+//                     
$GLOBALS['phpgw']->session->appsession('cols_return_single_'. $type_id 
,$this->currentapp,$cols_return);
+
+                       return $sql;
+               }
+
+               function read_single($location_code='')
+               {
+                       $location_array = split('-',$location_code);
+                       $type_id= count($location_array);
+
+                       if (!$type_id)
+                       {
+                               return;
+                       }
+
+                       $cols.= 'fm_location' . $type_id .'.category as 
cat_id,';
+                       $cols_return[]                          = 'cat_id';
+
+                       for ($i=1;$i<($type_id+1);$i++)
+                       {
+                               $cols.= 'fm_location' . $i .'.loc' . $i 
.'_name,';
+                               $cols_return[]                          = 'loc' 
. $i .'_name';
+                       }
+
+
+                       $cols.= 'fm_location' . $type_id . '.change_type,';
+                       $cols_return[]                          = 'change_type';
+                       $uicols['input_type'][]         = 'text';
+                       $uicols['name'][]                       = 'loc' . 
$type_id .'_name';
+                       $uicols['descr'][]                      = lang('name');
+                       $uicols['statustext'][]         = lang('name');
+
+                       $cols.= 'fm_location' . $type_id .'.remark as remark,';
+                       $cols_return[]                          = 'remark';
+                       $uicols['input_type'][]         = 'text';
+                       $uicols['name'][]                       = 'descr';
+                       $uicols['descr'][]                      = 
lang('remark');
+                       $uicols['statustext'][]         = lang('remark');
+
+                       $sql = $this->socommon->fm_cache('sql_single_'. 
$type_id);
+
+                       if(!$sql)
+                       {
+                               $sql    = 
$this->generate_sql($type_id,$cols,$cols_return,$uicols,True);
+                       }
+                       else
+                       {
+
+                               $this->uicols   =       
$this->socommon->fm_cache('uicols_single_'. $type_id);
+                               $this->cols_return      =       
$this->socommon->fm_cache('cols_return_single_'. $type_id);
+                       }
+
+                       $sql .= " WHERE 
fm_location$type_id.location_code='$location_code' ";
+
+                       $this->db->query($sql,__LINE__,__FILE__);
+
+//echo $sql;
+                       $cols_return    = $this->cols_return;
+
+                       $cols_return_count      = count($cols_return);
+
+                       $this->db->next_record();
+                       for ($i=0;$i<$cols_return_count;$i++)
+                       {
+                               $location[$cols_return[$i]] = 
stripslashes($this->db->f($cols_return[$i]));
+                       }
+
+//_debug_array($cols_return);
+//_debug_array($location);
+                       return $location;
+               }
+
+               function add($location='',$values_attribute='',$type_id='')
+               {
+                       $location['remark'] = 
$this->db->db_addslashes($location['remark']);
+
+                       while (is_array($location) && list($input_name,$value) 
= each($location))
+                       {
+                               if($value)
+                               {
+                                       if($input_name=='cat_id')
+                                       {
+                                               $input_name='category';
+                                       }
+                                       $cols[] = $input_name;
+                                       $vals[] = $value;
+                               }
+                       }
+
+                       if (isset($values_attribute) AND 
is_array($values_attribute))
+                       {
+                               foreach($values_attribute as $entry)
+                               {
+                                       if($entry['value'])
+                                       {
+                                               $cols[] = $entry['name'];
+                                               $vals[] = $entry['value'];
+                                       }
+                               }
+                       }
+
+                       $cols   =implode(",", $cols) . ",entry_date,user_id";
+                       $vals   ="'" . implode("','", $vals) . "'," . "'" . 
time() . "','" . $this->account . "'";
+
+
+                       $sql    = "INSERT INTO fm_location$type_id ($cols) 
VALUES ($vals)";
+
+//echo $sql;
+                       $this->db->query($sql,__LINE__,__FILE__);
+
+                       $receipt['message'][] = array('msg'=>lang('Location %1 
has been saved',$location['location_code']));
+                       return $receipt;
+               }
+
+               function edit($location='',$values_attribute='',$type_id='')
+               {
+//_debug_array($values_attribute);
+                       $location['remark'] = 
$this->db->db_addslashes($location['remark']);
+
+                       while (is_array($location) && list($input_name,$value) 
= each($location))
+                       {
+                               if($value)
+                               {
+                                       if($input_name=='cat_id')
+                                       {
+                                               $input_name='category';
+                                       }
+                                       $value_set[$input_name] = $value;
+                               }
+                       }
+
+                       if (isset($values_attribute) AND 
is_array($values_attribute))
+                       {
+                               foreach($values_attribute as $entry)
+                               {
+                                       $value_set[$entry['name']]      = 
$entry['value'];
+                               }
+                       }
+
+                       $value_set['entry_date'] = time();
+
+                       $value_set      = 
$this->bocommon->validate_db_update($value_set);
+
+                       $sql = "SELECT * from fm_location$type_id where 
location_code ='" . $location['location_code'] . "'";
+                       $this->db->query($sql,__LINE__,__FILE__);
+                       $this->db->next_record();
+
+                       $metadata = $this->db->metadata('fm_location'.$type_id);
+
+                       for ($i=0; $i<count($metadata); $i++)
+                       {
+                               $cols[] = $metadata[$i]['name'];
+                               $vals[] = $this->db->f($metadata[$i]['name']);
+                       }
+
+                       $cols[] = 'exp_date';
+                       $vals[] = date($this->bocommon->datetimeformat,time());
+
+                       $cols   =implode(",", $cols);
+                       $vals = $this->bocommon->validate_db_insert($vals);
+
+                       $sql = "INSERT INTO fm_location" . $type_id ."_history 
($cols) VALUES ($vals)";
+                       $this->db->query($sql,__LINE__,__FILE__);
+
+                       $sql = "UPDATE fm_location$type_id SET $value_set WHERE 
location_code='" . $location['location_code'] . "'";
+
+                       $this->db->query($sql,__LINE__,__FILE__);
+
+                       $receipt['message'][] = array('msg'=>lang('Location %1 
has been edited',$location['location_code']));
+                       return $receipt;
+               }
+
+               function delete($location_code )
+               {
+                       $location_array = split('-',$location_code);
+                       $type_id= count($location_array);
+
+                       $this->db->query("DELETE FROM fm_location$type_id WHERE 
location_code='" . $location_code ."'",__LINE__,__FILE__);
+//                     $this->db->query("DELETE FROM fm_location WHERE 
location_code='" . $location_code ."'",__LINE__,__FILE__);
+               }
+
+               function update_cat()
+               {
+                       $location_types = 
$this->soadmin_location->select_location_type();
+
+                       $m= count($location_types);
+
+                       $this->db->transaction_begin();
+
+                       $this->db->query("UPDATE fm_location" . $m. " set       
status= 2  WHERE category=99",__LINE__,__FILE__);
+
+                       for ($type_id=$m; $type_id>1; $type_id--)
+                       {
+                               $parent_table = 'fm_location' . ($type_id-1);
+
+                               $joinmethod .= " $this->join $parent_table";
+
+                               $paranthesis .='(';
+
+                               $on = 'ON';
+                               for ($i=($type_id-1); $i>0; $i--)
+                               {
+                                       $joinmethod .= " $on (fm_location" . 
($type_id) .".loc" . ($i). ' = '.$parent_table . ".loc" . ($i) . ")";
+                                       $on = 'AND';
+                                       if($i==1)
+                                       {
+                                               $joinmethod .= ")";
+                                       }
+                               }
+
+                               $sql = "SELECT $parent_table.location_code 
,count(*) as count_99  FROM $paranthesis fm_location$type_id $joinmethod where 
fm_location$type_id.status=2 group by $parent_table.location_code ";
+                               $this->db->query($sql,__LINE__,__FILE__);
+
+                               while ($this->db->next_record())
+                               {
+                                       
$outdated[$this->db->f('location_code')]['count_99']=$this->db->f('count_99');
+                               }
+
+                               $sql = "SELECT $parent_table.location_code 
,count(*) as count_all  FROM $paranthesis fm_location$type_id $joinmethod group 
by $parent_table.location_code ";
+                               $this->db->query($sql,__LINE__,__FILE__);
+                               while ($this->db->next_record())
+                               {
+                                       if( 
$outdated[$this->db->f('location_code')]['count_99']==$this->db->f('count_all'))
+                                       {
+                                               $update[]=array('location_code' 
=> $this->db->f('location_code'));
+                                       }
+                               }
+
+                               $j=0;
+                               for ($i=0; $i<count($update); $i++)
+                               {
+
+                                       $sql = "SELECT status  FROM 
$parent_table WHERE location_code= '" . $update[$i]['location_code'] ."'";
+
+                                       
$this->db->query($sql,__LINE__,__FILE__);
+                                       $this->db->next_record();
+
+                                       if($this->db->f('status')!=2)
+                                       {
+                                               $j++;
+                                               $this->db->query("UPDATE 
fm_location" . ($type_id-1). " set     status= 2  WHERE location_code= '" . 
$update[$i]['location_code'] ."'",__LINE__,__FILE__);
+                                       }
+                               }
+
+                               $receipt['message'][]=array('msg'=>lang('%1 
location %2 has been updated to not active of %3 already not 
active',$j,$location_types[($type_id-2)]['descr'],count($update)));
+
+                               unset($outdated);
+                               unset($update);
+                               unset($joinmethod);
+                               unset($paranthesis);
+                       }
+
+                       $this->db->transaction_commit();
+
+                       return $receipt;
+               }
+
+               function read_summary($data='')
+               {
+                       if(is_array($data))
+                       {
+                               $filter = 
(isset($data['filter'])?$data['filter']:0);
+                               $type_id = 
(isset($data['type_id'])?$data['type_id']:'');
+                               $district_id = 
(isset($data['district_id'])?$data['district_id']:'');
+                               $part_of_town_id = 
(isset($data['part_of_town_id'])?$data['part_of_town_id']:'');
+                       }
+
+                       if(!$type_id)
+                       {
+                               $type_id=4;
+                       }
+
+                       $entity_table = 'fm_location' . $type_id ;
+
+                       $cols= "count(*) as number, $entity_table.category, 
$entity_table"."_category.descr as type";
+
+                       $groupmethod = " GROUP by $entity_table.category , 
$entity_table"."_category.descr";
+
+                       $uicols['name'][]       = 'type';
+                       $uicols['descr'][]      = lang('type');
+
+
+                       $where = 'WHERE';
+                       if($district_id>0)
+                       {
+                               $uicols['name'][]       = 'district_id';
+                               $uicols['descr'][]      = lang('district_id');
+                               $cols.=", fm_part_of_town.district_id as 
district_id";
+                               $groupmethod .= " ,fm_part_of_town.district_id";
+                               $filtermethod = " $where 
fm_part_of_town.district_id=$district_id";
+                               $where = 'AND';
+                       }
+
+                       if($part_of_town_id>0)
+                       {
+                               $uicols['name'][]       = 'part_of_town';
+                               $uicols['descr'][]      = lang('part of town');
+                               $groupmethod .= " ,fm_part_of_town.name";
+                               $cols.=", fm_part_of_town.name as part_of_town";
+                               $filtermethod .= " $where 
fm_part_of_town.part_of_town_id=$part_of_town_id";
+                               $where = 'AND';
+                       }
+
+                       if($filter>0)
+                       {
+                               $filtermethod .= " $where fm_owner.id=$filter";
+                               $where = 'AND';
+                       }
+
+
+                       $uicols['name'][]       = 'number';
+                       $uicols['descr'][]      = lang('number');
+
+                       $joinmethod= "$this->join $entity_table"."_category on 
$entity_table.category=$entity_table"."_category.id";
+
+                       $sql = 
$this->bocommon->generate_sql(array('entity_table'=>$entity_table,'cols_return'=>$cols_return,'cols'=>$cols,
+                                                               
'uicols'=>$uicols,'joinmethod'=>$joinmethod,'paranthesis'=>$paranthesis,'query'=>$query,'no_address'=>True,'location_level'=>$type_id));
+
+                       $this->db->query($sql . $filtermethod . $groupmethod . 
" ORDER BY $entity_table.category",__LINE__,__FILE__);
+
+                       while ($this->db->next_record())
+                       {
+                               $summary[]=array(
+                                       'number'                => 
$this->db->f('number'),
+                                       'type'                  => '[' . 
$this->db->f('category') . '] ' .$this->db->f('type'),
+                                       'part_of_town'  => 
$this->db->f('part_of_town'),
+                                       'district_id'   => 
$this->db->f('district_id')
+                                       );
+                       }
+
+
+                       $this->uicols           = $uicols;
+                       return $summary;
+               }
+
+               function check_history($location_code='')
+               {
+                       $location_array = split('-',$location_code);
+                       $type_id= count($location_array);
+
+                       if (!$type_id)
+                       {
+                               return false;
+                       }
+
+                       $table = 'fm_location' . $type_id . '_history';
+
+                       $sql = "SELECT count(*) FROM $table WHERE 
location_code='$location_code'";
+
+                       $this->db->query($sql,__LINE__,__FILE__);
+
+                       $this->db->next_record();
+
+                       if($this->db->f('0')>0)
+                       {
+                               return True;
+                       }
+                       else
+                       {
+                               return;
+                       }
+               }
+
+               function get_history($location_code='')
+               {
+                       $this->uicols = array();
+                       $location_array = split('-',$location_code);
+                       $type_id= count($location_array);
+
+                       if (!$type_id)
+                       {
+                               return;
+                       }
+
+                       $table = 'fm_location' . $type_id . '_history';
+
+                       $table_category = 'fm_location' . $type_id . 
'_category';
+
+                       $sql = "SELECT column_name,datatype,input_text,id as 
attrib_id FROM fm_location_attrib WHERE type_id = $type_id";
+
+                       $this->db->query($sql,__LINE__,__FILE__);
+                       while ($this->db->next_record())
+                       {
+                               $attrib[] = array(
+                                       'column_name' => 
$this->db->f('column_name'),
+                                       'input_text' => 
$this->db->f('input_text'),
+                                       'datatype' => $this->db->f('datatype'),
+                                       'attrib_id' => $this->db->f('attrib_id')
+                               );
+
+                               $this->uicols['input_type'][] = 'text';
+                               $this->uicols['name'][] = 
$this->db->f('column_name');
+                               $this->uicols['descr'][] = 
$this->db->f('input_text');
+                       }
+
+                       $this->uicols['input_type'][] = 'text';
+                       $this->uicols['name'][] = 'exp_date';
+                       $this->uicols['descr'][] = lang('exp date');
+
+
+                       $attrib[] = array(
+                               'column_name' => 'exp_date',
+                               'input_text' => 'exp date',
+                               'datatype' => 'D'
+                       );
+
+                       $sql = "SELECT $table.*, $table_category.descr as 
category FROM $table $this->left_join $table_category ON $table.category 
=$table_category.id WHERE location_code='$location_code' ORDER BY exp_date 
DESC";
+                       $this->db->query($sql,__LINE__,__FILE__);
+
+                       $j=0;
+                       while ($this->db->next_record())
+                       {
+                               for ($i=0; $i<count($attrib); $i++)
+                               {
+                                       
$location[$j][$attrib[$i]['column_name']]=$this->db->f($attrib[$i]['column_name']);
+
+                                       $value = 
$this->db->f($attrib[$i]['column_name']);
+                                       if(($attrib[$i]['datatype']=='R' || 
$attrib[$i]['datatype']=='LB') && $value):
+                                       {
+                                               $sql="SELECT value FROM 
fm_location_choice where type_id=$type_id AND attrib_id=" 
.$attrib[$i]['attrib_id']. "  AND id=" . $value;
+                                               $this->db2->query($sql);
+                                               $this->db2->next_record();
+                                               
$location[$j][$attrib[$i]['column_name']] = $this->db2->f('value');
+                                       }
+                                       elseif($attrib[$i]['datatype']=='AB' && 
$value):
+                                       {
+                                               $contact_data   = 
$contacts->read_single_entry($value,array('n_given'=>'n_given','n_family'=>'n_family','email'=>'email'));
+                                               
$location[$j][$attrib[$i]['column_name']]       = $contact_data[0]['n_family'] 
. ', ' . $contact_data[0]['n_given'];
+                                       }
+                                       
elseif($attrib[$i]['datatype']=='VENDOR' && $value):
+                                       {
+                                               $sql="SELECT org_name FROM 
fm_vendor where id=$value";
+                                               $this->db2->query($sql);
+                                               $this->db2->next_record();
+                                               
$location[$j][$attrib[$i]['column_name']] = $this->db2->f('org_name');
+                                       }
+                                       elseif($attrib[$i]['datatype']=='CH' && 
$value):
+                                       {
+                                               $ch= unserialize($value);
+                                               if (isset($ch) AND 
is_array($ch))
+                                               {
+                                                       for 
($k=0;$k<count($ch);$k++)
+                                                       {
+                                                               $sql="SELECT 
value FROM fm_location_choice where type_id=$type_id AND attrib_id=" 
.$attrib[$i]['attrib_id']. "  AND id=" . $ch[$k];
+                                                               
$this->db2->query($sql);
+                                                               while 
($this->db2->next_record())
+                                                               {
+                                                                       
$ch_value[]=$this->db2->f('value');
+                                                               }
+                                                       }
+                                                       
$location[$j][$attrib[$i]['column_name']] = @implode(",", $ch_value);
+                                                       unset($ch_value);
+                                               }
+                                       }
+                                       elseif($attrib[$i]['datatype']=='D' && 
$value):
+                                       {
+                                               
$location[$j][$attrib[$i]['column_name']]=date($GLOBALS['phpgw_info']['user']['preferences']['common']['dateformat'],strtotime($value));
+                                       }
+                                       
elseif($attrib[$i]['column_name']=='entry_date' && $value):
+                                       {
+                                               
$location[$j][$attrib[$i]['column_name']]=date($GLOBALS['phpgw_info']['user']['preferences']['common']['dateformat'],$value);
+                                       }
+                                       endif;
+
+                                       unset($value);
+
+                               }
+                               $j++;
+                       }
+
+                       return $location;
+               }
+       }
+?>
Index: property/inc/class.solookup.inc.php
diff -u property/inc/class.solookup.inc.php:1.8 
property/inc/class.solookup.inc.php:1.9
--- property/inc/class.solookup.inc.php:1.8     Fri Jan 27 14:05:43 2006
+++ property/inc/class.solookup.inc.php Mon Jan 30 22:14:19 2006
@@ -1,384 +1,384 @@
-<?php
-       /**
-       * phpGroupWare - property: a Facilities Management System.
-       *
-       * @author Sigurd Nes <address@hidden>
-       * @copyright Copyright (C) 2003-2005 Free Software Foundation, Inc. 
http://www.fsf.org/
-       * @license http://www.gnu.org/licenses/gpl.html GNU General Public 
License
-       * @internal Development of this application was funded by 
http://www.bergen.kommune.no/bbb_/ekstern/
-       * @package property
-       * @subpackage core
-       * @version $Id: class.solookup.inc.php,v 1.8 2006/01/27 14:05:43 
sigurdne Exp $
-       */
-
-       /**
-        * Description
-        * @package property
-        */
-
-       class solookup
-       {
-               var $grants;
-
-               function solookup()
-               {
-                       $this->currentapp       = 
$GLOBALS['phpgw_info']['flags']['currentapp'];
-                       $this->account  = 
$GLOBALS['phpgw_info']['user']['account_id'];
-                       $this->bocommon         = 
CreateObject($this->currentapp.'.bocommon');
+<?php
+       /**
+       * phpGroupWare - property: a Facilities Management System.
+       *
+       * @author Sigurd Nes <address@hidden>
+       * @copyright Copyright (C) 2003-2005 Free Software Foundation, Inc. 
http://www.fsf.org/
+       * @license http://www.gnu.org/licenses/gpl.html GNU General Public 
License
+       * @internal Development of this application was funded by 
http://www.bergen.kommune.no/bbb_/ekstern/
+       * @package property
+       * @subpackage core
+       * @version $Id: class.solookup.inc.php,v 1.9 2006/01/30 22:14:19 
sigurdne Exp $
+       */
+
+       /**
+        * Description
+        * @package property
+        */
+
+       class solookup
+       {
+               var $grants;
+
+               function solookup()
+               {
+                       $this->currentapp       = 
$GLOBALS['phpgw_info']['flags']['currentapp'];
+                       $this->account  = 
$GLOBALS['phpgw_info']['user']['account_id'];
+                       $this->bocommon         = 
CreateObject($this->currentapp.'.bocommon');
                        $this->db               = $this->bocommon->new_db();
-                       $this->db2              = $this->bocommon->new_db();
-                       $this->join                     = $this->bocommon->join;
-                       $this->like                     = $this->bocommon->like;
-
-               }
-
-               function read_addressbook($data)
-               {
-                       if(is_array($data))
-                       {
-                               if ($data['start'])
-                               {
-                                       $start=$data['start'];
-                               }
-                               else
-                               {
-                                       $start=0;
-                               }
-                               $filter = 
(isset($data['filter'])?$data['filter']:'none');
-                               $query = 
(isset($data['query'])?$data['query']:'');
-                               $sort = 
(isset($data['sort'])?$data['sort']:'DESC');
-                               $order = 
(isset($data['order'])?$data['order']:'');
-                               $cat_id = 
(isset($data['cat_id'])?$data['cat_id']:0);
-                       }
-
-
-                       if ($order)
-                       {
-                               $ordermethod = " order by $order $sort";
-                       }
-                       else
-                       {
-                               $ordermethod = ' order by last_name DESC';
-                       }
-
-
-                       $where= 'WHERE';
-
-                       if ($cat_id > 0)
-                       {
-                               $filtermethod .= " $where cat_id $this->like 
'%,$cat_id,%' ";
-                               $where= 'AND';
-                       }
-
-                       if($query)
-                       {
-                               $query = ereg_replace("'",'',$query);
-                               $query = ereg_replace('"','',$query);
-
-                               $querymethod = " $where (id $this->like 
'%$query%' or org_name $this->like '%$query%')";
-                       }
-
-                       $sql = "SELECT person_id,first_name,last_name FROM 
phpgw_contact_person $filtermethod $querymethod";
-
-                       $this->db2->query($sql,__LINE__,__FILE__);
-                       $this->total_records = $this->db2->num_rows();
-                       $this->db->limit_query($sql . 
$ordermethod,$start,__LINE__,__FILE__);
-
-                       while ($this->db->next_record())
-                       {
-                               $contact[] = array
-                               (
-                                       'id'                    => 
$this->db->f('person_id'),
-                                       'contact_name'  => 
$this->db->f('last_name') . ', ' . $this->db->f('first_name'),
-                                       );
-                       }
-//_debug_array($vendor);
-
-                       return $contact;
-               }
-
-               function read_vendor($data)
-               {
-                       if(is_array($data))
-                       {
-                               if ($data['start'])
-                               {
-                                       $start=$data['start'];
-                               }
-                               else
-                               {
-                                       $start=0;
-                               }
-                               $filter = 
(isset($data['filter'])?$data['filter']:'none');
-                               $query = 
(isset($data['query'])?$data['query']:'');
-                               $sort = 
(isset($data['sort'])?$data['sort']:'DESC');
-                               $order = 
(isset($data['order'])?$data['order']:'');
-                               $cat_id = 
(isset($data['cat_id'])?$data['cat_id']:0);
-                       }
-
-
-                       if ($order)
-                       {
-                               $ordermethod = " order by $order $sort";
-                       }
-                       else
-                       {
-                               $ordermethod = ' order by org_name DESC';
-                       }
-
-
-                       $where= 'WHERE';
-
-                       if ($cat_id > 0)
-                       {
-                               $filtermethod .= " $where member_of $this->like 
'%,$cat_id,%' ";
-                               $where= 'AND';
-                       }
-
-                       if($query)
-                       {
-                               $query = ereg_replace("'",'',$query);
-                               $query = ereg_replace('"','',$query);
-
-                               $querymethod = " $where (id $this->like 
'%$query%' or org_name $this->like '%$query%')";
-                       }
-
-                       $sql = "SELECT id,org_name FROM fm_vendor $filtermethod 
$querymethod";
-
-                       $this->db2->query($sql,__LINE__,__FILE__);
-                       $this->total_records = $this->db2->num_rows();
-                       $this->db->limit_query($sql . 
$ordermethod,$start,__LINE__,__FILE__);
-
-                       while ($this->db->next_record())
-                       {
-                               $vendor[] = array
-                               (
-                                       'id'    => $this->db->f('id'),
-                                       'org_name'      => 
$this->db->f('org_name'),
-                                       );
-                       }
-//_debug_array($vendor);
-
-                       return $vendor;
-               }
-
-
-               function read_b_account($data)
-               {
-                       if(is_array($data))
-                       {
-                               if ($data['start'])
-                               {
-                                       $start=$data['start'];
-                               }
-                               else
-                               {
-                                       $start=0;
-                               }
-                               $filter = 
(isset($data['filter'])?$data['filter']:'none');
-                               $query = 
(isset($data['query'])?$data['query']:'');
-                               $sort = 
(isset($data['sort'])?$data['sort']:'DESC');
-                               $order = 
(isset($data['order'])?$data['order']:'');
-                               $cat_id = 
(isset($data['cat_id'])?$data['cat_id']:0);
-                       }
-
-                       if ($order)
-                       {
-                               $ordermethod = " order by $order $sort";
-                       }
-                       else
-                       {
-                               $ordermethod = ' order by id DESC';
-                       }
-
-                       if($query)
-                       {
-                               $query = ereg_replace("'",'',$query);
-                               $query = ereg_replace('"','',$query);
-
-                               $querymethod = " where (id $this->like 
'%$query%' or descr $this->like '%$query%')";
-                       }
-
-                       $sql = "SELECT * FROM fm_b_account $querymethod  ";
-
-                       $this->db2->query($sql,__LINE__,__FILE__);
-                       $this->total_records = $this->db2->num_rows();
-                       $this->db->limit_query($sql . 
$ordermethod,$start,__LINE__,__FILE__);
-
-                       while ($this->db->next_record())
-                       {
-                               $b_account[] = array
-                               (
-                                       'id'                    => 
$this->db->f('id'),
-                                       'descr' => $this->db->f('descr')
-                                       );
-                       }
-
-                       return $b_account;
-               }
-
-
-               function read_street($data)
-               {
-                       if(is_array($data))
-                       {
-                               if ($data['start'])
-                               {
-                                       $start=$data['start'];
-                               }
-                               else
-                               {
-                                       $start=0;
-                               }
-                               $filter = 
(isset($data['filter'])?$data['filter']:'none');
-                               $query = 
(isset($data['query'])?$data['query']:'');
-                               $sort = 
(isset($data['sort'])?$data['sort']:'DESC');
-                               $order = 
(isset($data['order'])?$data['order']:'');
-                               $cat_id = 
(isset($data['cat_id'])?$data['cat_id']:0);
-                       }
-
-                       if ($order)
-                       {
-                               $ordermethod = " order by $order $sort";
-                       }
-                       else
-                       {
-                               $ordermethod = ' order by 
fm_streetaddress.descr DESC';
-                       }
-
-                       if($query)
-                       {
-                               $query = ereg_replace("'",'',$query);
-                               $query = ereg_replace('"','',$query);
-
-                               $querymethod = " where ( descr $this->like 
'%$query%')";
-                       }
-
-                       $sql = "SELECT * FROM fm_streetaddress $querymethod  ";
-
-                       $this->db2->query($sql,__LINE__,__FILE__);
-                       $this->total_records = $this->db2->num_rows();
-                       $this->db->limit_query($sql . 
$ordermethod,$start,__LINE__,__FILE__);
-
-                       while ($this->db->next_record())
-                       {
-                               $street[] = array
-                               (
-                                       'id'            => $this->db->f('id'),
-                                       'street_name'   => 
stripslashes($this->db->f('descr'))
-                                       );
-                       }
-
-                       return $street;
-               }
-
-               function read_tenant($data)
-               {
-                       if(is_array($data))
-                       {
-                               if ($data['start'])
-                               {
-                                       $start=$data['start'];
-                               }
-                               else
-                               {
-                                       $start=0;
-                               }
-                               $filter = 
(isset($data['filter'])?$data['filter']:'none');
-                               $query = 
(isset($data['query'])?$data['query']:'');
-                               $sort = 
(isset($data['sort'])?$data['sort']:'DESC');
-                               $order = 
(isset($data['order'])?$data['order']:'');
-                               $cat_id = 
(isset($data['cat_id'])?$data['cat_id']:0);
-                       }
-
-                       if ($order)
-                       {
-                               $ordermethod = " order by $order $sort";
-                       }
-                       else
-                       {
-                               $ordermethod = ' order by last_name DESC';
-                       }
-
-                       if($query)
-                       {
-                               $query = ereg_replace("'",'',$query);
-                               $query = ereg_replace('"','',$query);
-
-                               $querymethod = " where ( last_name $this->like 
'%$query%' or first_name $this->like '%$query%')";
-                       }
-
-                       $sql = "SELECT * FROM fm_tenant $querymethod  ";
-
-                       $this->db2->query($sql,__LINE__,__FILE__);
-                       $this->total_records = $this->db2->num_rows();
-                       $this->db->limit_query($sql . 
$ordermethod,$start,__LINE__,__FILE__);
-
-                       while ($this->db->next_record())
-                       {
-                               $tenant[] = array
-                               (
-                                       'id'                    => 
$this->db->f('id'),
-                                       'last_name'     => 
$this->db->f('last_name'),
-                                       'first_name'    => 
$this->db->f('first_name')
-                                       );
-                       }
-
-                       return $tenant;
-               }
-
-               function read_ns3420($data)
-               {
-                       if(is_array($data))
-                       {
-                               if ($data['start'])
-                               {
-                                       $start=$data['start'];
-                               }
-                               else
-                               {
-                                       $start=0;
-                               }
-                               $filter = 
(isset($data['filter'])?$data['filter']:'none');
-                               $query = 
(isset($data['query'])?$data['query']:'');
-                               $sort = 
(isset($data['sort'])?$data['sort']:'DESC');
-                               $order = 
(isset($data['order'])?$data['order']:'');
-                               $cat_id = 
(isset($data['cat_id'])?$data['cat_id']:0);
-                       }
-
-                       if ($order)
-                       {
-                               $ordermethod = " order by $order $sort";
-                       }
-                       else
-                       {
-                               $ordermethod = ' order by tekst1 DESC';
-                       }
-
-                       if($query)
-                       {
-                               $query = ereg_replace("'",'',$query);
-                               $query = ereg_replace('"','',$query);
-
-                               $querymethod = " where ( tekst1 $this->like 
'%$query%' or tekst2 $this->like '%$query%' or tekst3 $this->like '%$query%' or 
tekst4 $this->like '%$query%' or tekst5 $this->like '%$query%' or tekst6 
$this->like '%$query%')";
-                       }
-
-                       $sql = "SELECT * FROM fm_ns3420  $querymethod  ";
-
-                       $this->db2->query($sql,__LINE__,__FILE__);
-                       $this->total_records = $this->db2->num_rows();
-                       $this->db->limit_query($sql . 
$ordermethod,$start,__LINE__,__FILE__);
-
-                       while ($this->db->next_record())
-                       {
-                               $ns3420[] = array
-                               (
-                                       'id'                    => 
$this->db->f('id'),
-                                       'ns3420_descr'  => 
$this->db->f('tekst1') . ' ' .$this->db->f('tekst2') . ' ' 
.$this->db->f('tekst3') . ' ' .$this->db->f('tekst4') . ' ' 
.$this->db->f('tekst5') . ' ' .$this->db->f('tekst6')
-                                       );
-                       }
-
-                       return $ns3420;
-               }
-       }
-?>
+                       $this->db2              = $this->bocommon->new_db();
+
+                       $this->join                     = $this->bocommon->join;
+                       $this->like                     = $this->bocommon->like;
+               }
+
+               function read_addressbook($data)
+               {
+                       if(is_array($data))
+                       {
+                               if ($data['start'])
+                               {
+                                       $start=$data['start'];
+                               }
+                               else
+                               {
+                                       $start=0;
+                               }
+                               $filter = 
(isset($data['filter'])?$data['filter']:'none');
+                               $query = 
(isset($data['query'])?$data['query']:'');
+                               $sort = 
(isset($data['sort'])?$data['sort']:'DESC');
+                               $order = 
(isset($data['order'])?$data['order']:'');
+                               $cat_id = 
(isset($data['cat_id'])?$data['cat_id']:0);
+                       }
+
+
+                       if ($order)
+                       {
+                               $ordermethod = " order by $order $sort";
+                       }
+                       else
+                       {
+                               $ordermethod = ' order by last_name DESC';
+                       }
+
+
+                       $where= 'WHERE';
+
+                       if ($cat_id > 0)
+                       {
+                               $filtermethod .= " $where cat_id $this->like 
'%,$cat_id,%' ";
+                               $where= 'AND';
+                       }
+
+                       if($query)
+                       {
+                               $query = ereg_replace("'",'',$query);
+                               $query = ereg_replace('"','',$query);
+
+                               $querymethod = " $where (id $this->like 
'%$query%' or org_name $this->like '%$query%')";
+                       }
+
+                       $sql = "SELECT person_id,first_name,last_name FROM 
phpgw_contact_person $filtermethod $querymethod";
+
+                       $this->db2->query($sql,__LINE__,__FILE__);
+                       $this->total_records = $this->db2->num_rows();
+                       $this->db->limit_query($sql . 
$ordermethod,$start,__LINE__,__FILE__);
+
+                       while ($this->db->next_record())
+                       {
+                               $contact[] = array
+                               (
+                                       'id'                    => 
$this->db->f('person_id'),
+                                       'contact_name'  => 
$this->db->f('last_name') . ', ' . $this->db->f('first_name'),
+                                       );
+                       }
+//_debug_array($vendor);
+
+                       return $contact;
+               }
+
+               function read_vendor($data)
+               {
+                       if(is_array($data))
+                       {
+                               if ($data['start'])
+                               {
+                                       $start=$data['start'];
+                               }
+                               else
+                               {
+                                       $start=0;
+                               }
+                               $filter = 
(isset($data['filter'])?$data['filter']:'none');
+                               $query = 
(isset($data['query'])?$data['query']:'');
+                               $sort = 
(isset($data['sort'])?$data['sort']:'DESC');
+                               $order = 
(isset($data['order'])?$data['order']:'');
+                               $cat_id = 
(isset($data['cat_id'])?$data['cat_id']:0);
+                       }
+
+
+                       if ($order)
+                       {
+                               $ordermethod = " order by $order $sort";
+                       }
+                       else
+                       {
+                               $ordermethod = ' order by org_name DESC';
+                       }
+
+
+                       $where= 'WHERE';
+
+                       if ($cat_id > 0)
+                       {
+                               $filtermethod .= " $where member_of $this->like 
'%,$cat_id,%' ";
+                               $where= 'AND';
+                       }
+
+                       if($query)
+                       {
+                               $query = ereg_replace("'",'',$query);
+                               $query = ereg_replace('"','',$query);
+
+                               $querymethod = " $where (id $this->like 
'%$query%' or org_name $this->like '%$query%')";
+                       }
+
+                       $sql = "SELECT id,org_name FROM fm_vendor $filtermethod 
$querymethod";
+
+                       $this->db2->query($sql,__LINE__,__FILE__);
+                       $this->total_records = $this->db2->num_rows();
+                       $this->db->limit_query($sql . 
$ordermethod,$start,__LINE__,__FILE__);
+
+                       while ($this->db->next_record())
+                       {
+                               $vendor[] = array
+                               (
+                                       'id'    => $this->db->f('id'),
+                                       'org_name'      => 
$this->db->f('org_name'),
+                                       );
+                       }
+//_debug_array($vendor);
+
+                       return $vendor;
+               }
+
+
+               function read_b_account($data)
+               {
+                       if(is_array($data))
+                       {
+                               if ($data['start'])
+                               {
+                                       $start=$data['start'];
+                               }
+                               else
+                               {
+                                       $start=0;
+                               }
+                               $filter = 
(isset($data['filter'])?$data['filter']:'none');
+                               $query = 
(isset($data['query'])?$data['query']:'');
+                               $sort = 
(isset($data['sort'])?$data['sort']:'DESC');
+                               $order = 
(isset($data['order'])?$data['order']:'');
+                               $cat_id = 
(isset($data['cat_id'])?$data['cat_id']:0);
+                       }
+
+                       if ($order)
+                       {
+                               $ordermethod = " order by $order $sort";
+                       }
+                       else
+                       {
+                               $ordermethod = ' order by id DESC';
+                       }
+
+                       if($query)
+                       {
+                               $query = ereg_replace("'",'',$query);
+                               $query = ereg_replace('"','',$query);
+
+                               $querymethod = " where (id $this->like 
'%$query%' or descr $this->like '%$query%')";
+                       }
+
+                       $sql = "SELECT * FROM fm_b_account $querymethod  ";
+
+                       $this->db2->query($sql,__LINE__,__FILE__);
+                       $this->total_records = $this->db2->num_rows();
+                       $this->db->limit_query($sql . 
$ordermethod,$start,__LINE__,__FILE__);
+
+                       while ($this->db->next_record())
+                       {
+                               $b_account[] = array
+                               (
+                                       'id'                    => 
$this->db->f('id'),
+                                       'descr' => $this->db->f('descr')
+                                       );
+                       }
+
+                       return $b_account;
+               }
+
+
+               function read_street($data)
+               {
+                       if(is_array($data))
+                       {
+                               if ($data['start'])
+                               {
+                                       $start=$data['start'];
+                               }
+                               else
+                               {
+                                       $start=0;
+                               }
+                               $filter = 
(isset($data['filter'])?$data['filter']:'none');
+                               $query = 
(isset($data['query'])?$data['query']:'');
+                               $sort = 
(isset($data['sort'])?$data['sort']:'DESC');
+                               $order = 
(isset($data['order'])?$data['order']:'');
+                               $cat_id = 
(isset($data['cat_id'])?$data['cat_id']:0);
+                       }
+
+                       if ($order)
+                       {
+                               $ordermethod = " order by $order $sort";
+                       }
+                       else
+                       {
+                               $ordermethod = ' order by 
fm_streetaddress.descr DESC';
+                       }
+
+                       if($query)
+                       {
+                               $query = ereg_replace("'",'',$query);
+                               $query = ereg_replace('"','',$query);
+
+                               $querymethod = " where ( descr $this->like 
'%$query%')";
+                       }
+
+                       $sql = "SELECT * FROM fm_streetaddress $querymethod  ";
+
+                       $this->db2->query($sql,__LINE__,__FILE__);
+                       $this->total_records = $this->db2->num_rows();
+                       $this->db->limit_query($sql . 
$ordermethod,$start,__LINE__,__FILE__);
+
+                       while ($this->db->next_record())
+                       {
+                               $street[] = array
+                               (
+                                       'id'            => $this->db->f('id'),
+                                       'street_name'   => 
stripslashes($this->db->f('descr'))
+                                       );
+                       }
+
+                       return $street;
+               }
+
+               function read_tenant($data)
+               {
+                       if(is_array($data))
+                       {
+                               if ($data['start'])
+                               {
+                                       $start=$data['start'];
+                               }
+                               else
+                               {
+                                       $start=0;
+                               }
+                               $filter = 
(isset($data['filter'])?$data['filter']:'none');
+                               $query = 
(isset($data['query'])?$data['query']:'');
+                               $sort = 
(isset($data['sort'])?$data['sort']:'DESC');
+                               $order = 
(isset($data['order'])?$data['order']:'');
+                               $cat_id = 
(isset($data['cat_id'])?$data['cat_id']:0);
+                       }
+
+                       if ($order)
+                       {
+                               $ordermethod = " order by $order $sort";
+                       }
+                       else
+                       {
+                               $ordermethod = ' order by last_name DESC';
+                       }
+
+                       if($query)
+                       {
+                               $query = ereg_replace("'",'',$query);
+                               $query = ereg_replace('"','',$query);
+
+                               $querymethod = " where ( last_name $this->like 
'%$query%' or first_name $this->like '%$query%')";
+                       }
+
+                       $sql = "SELECT * FROM fm_tenant $querymethod  ";
+
+                       $this->db2->query($sql,__LINE__,__FILE__);
+                       $this->total_records = $this->db2->num_rows();
+                       $this->db->limit_query($sql . 
$ordermethod,$start,__LINE__,__FILE__);
+
+                       while ($this->db->next_record())
+                       {
+                               $tenant[] = array
+                               (
+                                       'id'                    => 
$this->db->f('id'),
+                                       'last_name'     => 
$this->db->f('last_name'),
+                                       'first_name'    => 
$this->db->f('first_name')
+                                       );
+                       }
+
+                       return $tenant;
+               }
+
+               function read_ns3420($data)
+               {
+                       if(is_array($data))
+                       {
+                               if ($data['start'])
+                               {
+                                       $start=$data['start'];
+                               }
+                               else
+                               {
+                                       $start=0;
+                               }
+                               $filter = 
(isset($data['filter'])?$data['filter']:'none');
+                               $query = 
(isset($data['query'])?$data['query']:'');
+                               $sort = 
(isset($data['sort'])?$data['sort']:'DESC');
+                               $order = 
(isset($data['order'])?$data['order']:'');
+                               $cat_id = 
(isset($data['cat_id'])?$data['cat_id']:0);
+                       }
+
+                       if ($order)
+                       {
+                               $ordermethod = " order by $order $sort";
+                       }
+                       else
+                       {
+                               $ordermethod = ' order by tekst1 DESC';
+                       }
+
+                       if($query)
+                       {
+                               $query = ereg_replace("'",'',$query);
+                               $query = ereg_replace('"','',$query);
+
+                               $querymethod = " where ( tekst1 $this->like 
'%$query%' or tekst2 $this->like '%$query%' or tekst3 $this->like '%$query%' or 
tekst4 $this->like '%$query%' or tekst5 $this->like '%$query%' or tekst6 
$this->like '%$query%')";
+                       }
+
+                       $sql = "SELECT * FROM fm_ns3420  $querymethod  ";
+
+                       $this->db2->query($sql,__LINE__,__FILE__);
+                       $this->total_records = $this->db2->num_rows();
+                       $this->db->limit_query($sql . 
$ordermethod,$start,__LINE__,__FILE__);
+
+                       while ($this->db->next_record())
+                       {
+                               $ns3420[] = array
+                               (
+                                       'id'                    => 
$this->db->f('id'),
+                                       'ns3420_descr'  => 
$this->db->f('tekst1') . ' ' .$this->db->f('tekst2') . ' ' 
.$this->db->f('tekst3') . ' ' .$this->db->f('tekst4') . ' ' 
.$this->db->f('tekst5') . ' ' .$this->db->f('tekst6')
+                                       );
+                       }
+
+                       return $ns3420;
+               }
+       }
+?>
Index: property/inc/class.someter.inc.php
diff -u property/inc/class.someter.inc.php:1.6 
property/inc/class.someter.inc.php:1.7
--- property/inc/class.someter.inc.php:1.6      Fri Jan 27 14:05:43 2006
+++ property/inc/class.someter.inc.php  Mon Jan 30 22:14:19 2006
@@ -1,284 +1,283 @@
-<?php
-       /**
-       * phpGroupWare - property: a Facilities Management System.
-       *
-       * @author Sigurd Nes <address@hidden>
-       * @copyright Copyright (C) 2003-2005 Free Software Foundation, Inc. 
http://www.fsf.org/
-       * @license http://www.gnu.org/licenses/gpl.html GNU General Public 
License
-       * @internal Development of this application was funded by 
http://www.bergen.kommune.no/bbb_/ekstern/
-       * @package property
-       * @subpackage entity
-       * @version $Id: class.someter.inc.php,v 1.6 2006/01/27 14:05:43 
sigurdne Exp $
-       */
-
-       /**
-        * Description
-        * @package property
-        */
-
-       class someter
-       {
-
-               function someter()
-               {
-                       $this->currentapp       = 
$GLOBALS['phpgw_info']['flags']['currentapp'];
-                       $this->account  = 
$GLOBALS['phpgw_info']['user']['account_id'];
-                       $this->bocommon         = 
CreateObject($this->currentapp.'.bocommon');
+<?php
+       /**
+       * phpGroupWare - property: a Facilities Management System.
+       *
+       * @author Sigurd Nes <address@hidden>
+       * @copyright Copyright (C) 2003-2005 Free Software Foundation, Inc. 
http://www.fsf.org/
+       * @license http://www.gnu.org/licenses/gpl.html GNU General Public 
License
+       * @internal Development of this application was funded by 
http://www.bergen.kommune.no/bbb_/ekstern/
+       * @package property
+       * @subpackage entity
+       * @version $Id: class.someter.inc.php,v 1.7 2006/01/30 22:14:19 
sigurdne Exp $
+       */
+
+       /**
+        * Description
+        * @package property
+        */
+
+       class someter
+       {
+
+               function someter()
+               {
+                       $this->currentapp       = 
$GLOBALS['phpgw_info']['flags']['currentapp'];
+                       $this->account  = 
$GLOBALS['phpgw_info']['user']['account_id'];
+                       $this->bocommon         = 
CreateObject($this->currentapp.'.bocommon');
                        $this->db               = $this->bocommon->new_db();
                        $this->db2              = $this->bocommon->new_db();
-                       $this->join                     = $this->bocommon->join;
-                       $this->like                     = $this->bocommon->like;
-               }
-
-               function get_category_list()
-               {
-
-                       $this->db->query("SELECT id, descr FROM 
fm_meter_category ORDER BY descr ");
-
-                       $i = 0;
-                       while ($this->db->next_record())
-                       {
-                               $categories[$i]['id']                           
= $this->db->f('id');
-                               $categories[$i]['name']                         
= stripslashes($this->db->f('descr'));
-                               $i++;
-                       }
-                       return $categories;
-               }
-
-               function check_meter_num($num='')
-               {
-                       $this->db->query("SELECT count(*) FROM fm_meter where 
num='$num'");
-
-                       $this->db->next_record();
-
-                       if ( $this->db->f(0))
-                       {
-                               return True;
-                       }
-               }
-
-               function read($data)
-               {
-                       if(is_array($data))
-                       {
-                               if ($data['start'])
-                               {
-                                       $start=$data['start'];
-                               }
-                               else
-                               {
-                                       $start=0;
-                               }
-                               $filter = 
(isset($data['filter'])?$data['filter']:'none');
-                               $query = 
(isset($data['query'])?$data['query']:'');
-                               $sort = 
(isset($data['sort'])?$data['sort']:'DESC');
-                               $order = 
(isset($data['order'])?$data['order']:'');
-                               $cat_id = 
(isset($data['cat_id'])?$data['cat_id']:0);
-                       }
-
-
-                       $entity_table = 'fm_meter';
-
-                       $cols .= $entity_table . '.location_code';
-                       $cols_return[] = 'location_code';
-
-                       $cols .= ",$entity_table.id as meter_id";
-                       $cols_return[] = 'meter_id';
-
-                       $cols .= ",$entity_table.num as num";
-                       $cols_return[]                          = 'num';
-                       $uicols['input_type'][]         = 'text';
-                       $uicols['name'][]                       = 'num';
-                       $uicols['descr'][]                      = lang('Meter 
ID');
-                       $uicols['statustext'][]         = lang('Meter ID');
-
-                       $cols .= ",$entity_table.name ";
-                       $cols_return[]                          = 'name';
-                       $uicols['input_type'][]         = 'text';
-                       $uicols['name'][]                       = 'name';
-                       $uicols['descr'][]                      = lang('Name');
-                       $uicols['statustext'][]         = lang('Name');
-
-                       $cols .= ",fm_meter_category.descr as meter_type";
-                       $cols_return[]                          = 'meter_type';
-                       $uicols['input_type'][]         = 'text';
-                       $uicols['name'][]                       = 'meter_type';
-                       $uicols['descr'][]                      = lang('Meter 
Type');
-                       $uicols['statustext'][]         = lang('Meter Type');
-
-
-                       $joinmethod .= " $this->join  fm_meter_category ON 
($entity_table.category = fm_meter_category.id))";
-                       $paranthesis .='(';
-
-
-                       $sql    = 
$this->bocommon->generate_sql(array('entity_table'=>$entity_table,'cols'=>$cols,'cols_return'=>$cols_return,
-                                                                               
                                        
'uicols'=>$uicols,'joinmethod'=>$joinmethod,'paranthesis'=>$paranthesis,'query'=>$query));
-
-
-                       if ($order)
-                       {
-                               $ordermethod = " order by $entity_table.$order 
$sort";
-                       }
-                       else
-                       {
-                               $ordermethod = " order by 
$entity_table.location_code asc";
-                       }
-
-
-                       $where = 'WHERE';
-                       if ($cat_id)
-                       {
-                               $filtermethod .= " $where 
fm_meter.category='$cat_id' ";
-                               $where = 'AND';
-                       }
-
-                       if($query)
-                       {
-                               $query = ereg_replace("'",'',$query);
-                               $query = ereg_replace('"','',$query);
-
-                               $querymethod = " $where (fm_meter.name 
$this->like '%$query%' OR fm_meter.num $this->like '%$query%' OR 
fm_meter.location_code $this->like '%$query%' OR fm_meter.address $this->like 
'%$query%')";
-                       }
-
-                       $sql .= " $filtermethod $querymethod";
-//echo $sql;
-                       $this->uicols           = $this->bocommon->uicols;
-                       $cols_return            = $this->bocommon->cols_return;
-                       $type_id                        = 
$this->bocommon->type_id;
-                       $this->cols_extra       = $this->bocommon->cols_extra;
-
-                       $this->db2->query($sql,__LINE__,__FILE__);
-                       $this->total_records = $this->db2->num_rows();
-                       $this->db->limit_query($sql . 
$ordermethod,$start,__LINE__,__FILE__);
-
-
-                       $j=0;
-                       while ($this->db->next_record())
-                       {
-                               for ($i=0;$i<count($cols_return);$i++)
-                               {
-                                       $meter[$j][$cols_return[$i]] = 
$this->db->f($cols_return[$i]);
-                               }
-
-                               $location_code= $this->db->f('location_code');
-                               $location = split('-',$location_code);
-                               for ($m=0;$m<count($location);$m++)
-                               {
-                                       $meter[$j]['loc' . ($m+1)] = 
$location[$m];
-                                       $meter[$j]['query_location']['loc' . 
($m+1)]=implode("-", array_slice($location, 0, ($m+1)));
-                               }
-
-                               $j++;
-                       }
-
-                       return $meter;
-               }
-
-               function read_single($meter_id)
-               {
-                       $sql = "SELECT * FROM fm_meter where id='$meter_id'";
-
-                       $this->db->query($sql,__LINE__,__FILE__);
-
-                       if ($this->db->next_record())
-                       {
-                               $meter['meter_id']                      = 
$this->db->f('id');
-                               $meter['num']                           = 
$this->db->f('num');
-                               $meter['name']                          = 
stripslashes($this->db->f('name'));
-                               $meter['remark']                        = 
stripslashes($this->db->f('remark'));
-                               $meter['cat_id']                        = 
$this->db->f('category');
-                               $meter['location_code']         = 
$this->db->f('location_code');
-                               $meter['p_num']                         = 
$this->db->f('p_num');
-                               $meter['p_entity_id']           = 
$this->db->f('p_entity_id');
-                               $meter['p_cat_id']                      = 
$this->db->f('p_cat_id');
-                       }
-                       return $meter;
-               }
-
-               function add($meter)
-               {
-
-                       while (is_array($meter['location']) && 
list($input_name,$value) = each($meter['location']))
-                       {
-                               if($value)
-                               {
-                                       $cols[] = $input_name;
-                                       $vals[] = $value;
-                               }
-                       }
-
-                       if($cols)
-                       {
-                               $cols   = "," . implode(",", $cols);
-                               $vals   = ",'" . implode("','", $vals) . "'";
-                       }
-
-                       if($meter['street_name'])
-                       {
-                               $address[]= $meter['street_name'];
-                               $address[]= $meter['street_number'];
-                               $address        = implode(" ", $address);
-                       }
-
-                       if(!$address)
-                       {
-                               $address = $meter['location_name'];
-                       }
-
-                       $meter['remark'] = 
$this->db->db_addslashes($meter['remark']);
-
-                       $this->db->query("INSERT INTO fm_meter (location_code, 
num,user_id,entry_date,name,address,category,remark $cols ) "
-                               . "VALUES ('" . $meter['location_code'] . "','" 
. $meter['num'] . "','" . $this->account . "','" . time() . "','" . 
$meter['name']
-                               . "','" . $address ."','" . $meter['cat_id'] 
."','" . $meter['remark']. "' $vals )",__LINE__,__FILE__);
-
-
-                       $meter_id = 
$this->db->get_last_insert_id('fm_meter','id');
-
-                       $receipt['meter_id'] = $meter_id;
-                       $receipt['message'][]=array('msg'=>lang('Meter %1 has 
been saved',$meter['num']));
-                       return $receipt;
-               }
-
-               function edit($meter)
-               {
-
-                       while (is_array($meter['location']) && 
list($input_name,$value) = each($meter['location']))
-                       {
-                               $vals[] = "$input_name = '$value'";
-                       }
-
-                       if($vals)
-                       {
-                               $vals   = "," . implode(",",$vals);
-                       }
-
-                       if($meter['street_name'])
-                       {
-                               $address[]= $meter['street_name'];
-                               $address[]= $meter['street_number'];
-                               $address        = implode(" ", $address);
-                       }
-
-                       if(!$address)
-                       {
-                               $address = $meter['location_name'];
-                       }
-
-                       $meter['remark'] = 
$this->db->db_addslashes($meter['remark']);
-
-
-                       $this->db->query("UPDATE fm_meter set name='" . 
$meter['name'] . "', entry_date='" . time() . "', category='"
-                                                       . $meter['cat_id'] . 
"', location_code='" . $meter['location_code'] . "', address='" . $address . 
"', remark='" . $meter['remark']
-                                                       . "' $vals WHERE id='" 
. $meter['meter_id'] . "'",__LINE__,__FILE__);
-
-                       $receipt['message'][]=array('msg'=>lang('Meter %1 has 
been edited',$meter['num']));
-                       return $receipt;
-               }
-
-               function delete($meter_id)
-               {
-                       $this->db->query("DELETE FROM fm_meter WHERE id='" . 
$meter_id ."'",__LINE__,__FILE__);
-               }
-
-       }
-?>
+                       $this->join                     = $this->bocommon->join;
+                       $this->like                     = $this->bocommon->like;
+               }
+
+               function get_category_list()
+               {
+
+                       $this->db->query("SELECT id, descr FROM 
fm_meter_category ORDER BY descr ");
+
+                       $i = 0;
+                       while ($this->db->next_record())
+                       {
+                               $categories[$i]['id']                           
= $this->db->f('id');
+                               $categories[$i]['name']                         
= stripslashes($this->db->f('descr'));
+                               $i++;
+                       }
+                       return $categories;
+               }
+
+               function check_meter_num($num='')
+               {
+                       $this->db->query("SELECT count(*) FROM fm_meter where 
num='$num'");
+
+                       $this->db->next_record();
+
+                       if ( $this->db->f(0))
+                       {
+                               return True;
+                       }
+               }
+
+               function read($data)
+               {
+                       if(is_array($data))
+                       {
+                               if ($data['start'])
+                               {
+                                       $start=$data['start'];
+                               }
+                               else
+                               {
+                                       $start=0;
+                               }
+                               $filter = 
(isset($data['filter'])?$data['filter']:'none');
+                               $query = 
(isset($data['query'])?$data['query']:'');
+                               $sort = 
(isset($data['sort'])?$data['sort']:'DESC');
+                               $order = 
(isset($data['order'])?$data['order']:'');
+                               $cat_id = 
(isset($data['cat_id'])?$data['cat_id']:0);
+                       }
+
+
+                       $entity_table = 'fm_meter';
+
+                       $cols .= $entity_table . '.location_code';
+                       $cols_return[] = 'location_code';
+
+                       $cols .= ",$entity_table.id as meter_id";
+                       $cols_return[] = 'meter_id';
+
+                       $cols .= ",$entity_table.num as num";
+                       $cols_return[]                          = 'num';
+                       $uicols['input_type'][]         = 'text';
+                       $uicols['name'][]                       = 'num';
+                       $uicols['descr'][]                      = lang('Meter 
ID');
+                       $uicols['statustext'][]         = lang('Meter ID');
+
+                       $cols .= ",$entity_table.name ";
+                       $cols_return[]                          = 'name';
+                       $uicols['input_type'][]         = 'text';
+                       $uicols['name'][]                       = 'name';
+                       $uicols['descr'][]                      = lang('Name');
+                       $uicols['statustext'][]         = lang('Name');
+
+                       $cols .= ",fm_meter_category.descr as meter_type";
+                       $cols_return[]                          = 'meter_type';
+                       $uicols['input_type'][]         = 'text';
+                       $uicols['name'][]                       = 'meter_type';
+                       $uicols['descr'][]                      = lang('Meter 
Type');
+                       $uicols['statustext'][]         = lang('Meter Type');
+
+
+                       $joinmethod .= " $this->join  fm_meter_category ON 
($entity_table.category = fm_meter_category.id))";
+                       $paranthesis .='(';
+
+
+                       $sql    = 
$this->bocommon->generate_sql(array('entity_table'=>$entity_table,'cols'=>$cols,'cols_return'=>$cols_return,
+                                                                               
                                        
'uicols'=>$uicols,'joinmethod'=>$joinmethod,'paranthesis'=>$paranthesis,'query'=>$query));
+
+
+                       if ($order)
+                       {
+                               $ordermethod = " order by $entity_table.$order 
$sort";
+                       }
+                       else
+                       {
+                               $ordermethod = " order by 
$entity_table.location_code asc";
+                       }
+
+
+                       $where = 'WHERE';
+                       if ($cat_id)
+                       {
+                               $filtermethod .= " $where 
fm_meter.category='$cat_id' ";
+                               $where = 'AND';
+                       }
+
+                       if($query)
+                       {
+                               $query = ereg_replace("'",'',$query);
+                               $query = ereg_replace('"','',$query);
+
+                               $querymethod = " $where (fm_meter.name 
$this->like '%$query%' OR fm_meter.num $this->like '%$query%' OR 
fm_meter.location_code $this->like '%$query%' OR fm_meter.address $this->like 
'%$query%')";
+                       }
+
+                       $sql .= " $filtermethod $querymethod";
+//echo $sql;
+                       $this->uicols           = $this->bocommon->uicols;
+                       $cols_return            = $this->bocommon->cols_return;
+                       $type_id                        = 
$this->bocommon->type_id;
+                       $this->cols_extra       = $this->bocommon->cols_extra;
+
+                       $this->db2->query($sql,__LINE__,__FILE__);
+                       $this->total_records = $this->db2->num_rows();
+                       $this->db->limit_query($sql . 
$ordermethod,$start,__LINE__,__FILE__);
+
+
+                       $j=0;
+                       while ($this->db->next_record())
+                       {
+                               for ($i=0;$i<count($cols_return);$i++)
+                               {
+                                       $meter[$j][$cols_return[$i]] = 
$this->db->f($cols_return[$i]);
+                               }
+
+                               $location_code= $this->db->f('location_code');
+                               $location = split('-',$location_code);
+                               for ($m=0;$m<count($location);$m++)
+                               {
+                                       $meter[$j]['loc' . ($m+1)] = 
$location[$m];
+                                       $meter[$j]['query_location']['loc' . 
($m+1)]=implode("-", array_slice($location, 0, ($m+1)));
+                               }
+
+                               $j++;
+                       }
+
+                       return $meter;
+               }
+
+               function read_single($meter_id)
+               {
+                       $sql = "SELECT * FROM fm_meter where id='$meter_id'";
+
+                       $this->db->query($sql,__LINE__,__FILE__);
+
+                       if ($this->db->next_record())
+                       {
+                               $meter['meter_id']                      = 
$this->db->f('id');
+                               $meter['num']                           = 
$this->db->f('num');
+                               $meter['name']                          = 
stripslashes($this->db->f('name'));
+                               $meter['remark']                        = 
stripslashes($this->db->f('remark'));
+                               $meter['cat_id']                        = 
$this->db->f('category');
+                               $meter['location_code']         = 
$this->db->f('location_code');
+                               $meter['p_num']                         = 
$this->db->f('p_num');
+                               $meter['p_entity_id']           = 
$this->db->f('p_entity_id');
+                               $meter['p_cat_id']                      = 
$this->db->f('p_cat_id');
+                       }
+                       return $meter;
+               }
+
+               function add($meter)
+               {
+
+                       while (is_array($meter['location']) && 
list($input_name,$value) = each($meter['location']))
+                       {
+                               if($value)
+                               {
+                                       $cols[] = $input_name;
+                                       $vals[] = $value;
+                               }
+                       }
+
+                       if($cols)
+                       {
+                               $cols   = "," . implode(",", $cols);
+                               $vals   = ",'" . implode("','", $vals) . "'";
+                       }
+
+                       if($meter['street_name'])
+                       {
+                               $address[]= $meter['street_name'];
+                               $address[]= $meter['street_number'];
+                               $address        = implode(" ", $address);
+                       }
+
+                       if(!$address)
+                       {
+                               $address = $meter['location_name'];
+                       }
+
+                       $meter['remark'] = 
$this->db->db_addslashes($meter['remark']);
+
+                       $this->db->query("INSERT INTO fm_meter (location_code, 
num,user_id,entry_date,name,address,category,remark $cols ) "
+                               . "VALUES ('" . $meter['location_code'] . "','" 
. $meter['num'] . "','" . $this->account . "','" . time() . "','" . 
$meter['name']
+                               . "','" . $address ."','" . $meter['cat_id'] 
."','" . $meter['remark']. "' $vals )",__LINE__,__FILE__);
+
+
+                       $meter_id = 
$this->db->get_last_insert_id('fm_meter','id');
+
+                       $receipt['meter_id'] = $meter_id;
+                       $receipt['message'][]=array('msg'=>lang('Meter %1 has 
been saved',$meter['num']));
+                       return $receipt;
+               }
+
+               function edit($meter)
+               {
+                       while (is_array($meter['location']) && 
list($input_name,$value) = each($meter['location']))
+                       {
+                               $vals[] = "$input_name = '$value'";
+                       }
+
+                       if($vals)
+                       {
+                               $vals   = "," . implode(",",$vals);
+                       }
+
+                       if($meter['street_name'])
+                       {
+                               $address[]= $meter['street_name'];
+                               $address[]= $meter['street_number'];
+                               $address        = implode(" ", $address);
+                       }
+
+                       if(!$address)
+                       {
+                               $address = $meter['location_name'];
+                       }
+
+                       $meter['remark'] = 
$this->db->db_addslashes($meter['remark']);
+
+
+                       $this->db->query("UPDATE fm_meter set name='" . 
$meter['name'] . "', entry_date='" . time() . "', category='"
+                                                       . $meter['cat_id'] . 
"', location_code='" . $meter['location_code'] . "', address='" . $address . 
"', remark='" . $meter['remark']
+                                                       . "' $vals WHERE id='" 
. $meter['meter_id'] . "'",__LINE__,__FILE__);
+
+                       $receipt['message'][]=array('msg'=>lang('Meter %1 has 
been edited',$meter['num']));
+                       return $receipt;
+               }
+
+               function delete($meter_id)
+               {
+                       $this->db->query("DELETE FROM fm_meter WHERE id='" . 
$meter_id ."'",__LINE__,__FILE__);
+               }
+
+       }
+?>
Index: property/inc/class.sop_of_town.inc.php
diff -u property/inc/class.sop_of_town.inc.php:1.6 
property/inc/class.sop_of_town.inc.php:1.7
--- property/inc/class.sop_of_town.inc.php:1.6  Fri Jan 27 14:05:43 2006
+++ property/inc/class.sop_of_town.inc.php      Mon Jan 30 22:14:19 2006
@@ -1,151 +1,151 @@
-<?php
-       /**
-       * phpGroupWare - property: a Facilities Management System.
-       *
-       * @author Sigurd Nes <address@hidden>
-       * @copyright Copyright (C) 2003-2005 Free Software Foundation, Inc. 
http://www.fsf.org/
-       * @license http://www.gnu.org/licenses/gpl.html GNU General Public 
License
-       * @internal Development of this application was funded by 
http://www.bergen.kommune.no/bbb_/ekstern/
-       * @package property
-       * @subpackage admin
-       * @version $Id: class.sop_of_town.inc.php,v 1.6 2006/01/27 14:05:43 
sigurdne Exp $
-       */
-
-       /**
-        * Description
-        * @package property
-        */
-
-       class sop_of_town
-       {
-
-               function sop_of_town()
-               {
-                       $this->currentapp       = 
$GLOBALS['phpgw_info']['flags']['currentapp'];
-                       $this->account  = 
$GLOBALS['phpgw_info']['user']['account_id'];
-                       $this->bocommon         = 
CreateObject($this->currentapp.'.bocommon');
+<?php
+       /**
+       * phpGroupWare - property: a Facilities Management System.
+       *
+       * @author Sigurd Nes <address@hidden>
+       * @copyright Copyright (C) 2003-2005 Free Software Foundation, Inc. 
http://www.fsf.org/
+       * @license http://www.gnu.org/licenses/gpl.html GNU General Public 
License
+       * @internal Development of this application was funded by 
http://www.bergen.kommune.no/bbb_/ekstern/
+       * @package property
+       * @subpackage admin
+       * @version $Id: class.sop_of_town.inc.php,v 1.7 2006/01/30 22:14:19 
sigurdne Exp $
+       */
+
+       /**
+        * Description
+        * @package property
+        */
+
+       class sop_of_town
+       {
+
+               function sop_of_town()
+               {
+                       $this->currentapp       = 
$GLOBALS['phpgw_info']['flags']['currentapp'];
+                       $this->account  = 
$GLOBALS['phpgw_info']['user']['account_id'];
+                       $this->bocommon         = 
CreateObject($this->currentapp.'.bocommon');
                        $this->db               = $this->bocommon->new_db();
-                       $this->db2              = $this->bocommon->new_db();
-                       $this->join                     = $this->bocommon->join;
-                       $this->like                     = $this->bocommon->like;
-               }
-
-
-               function read_district_name($id)
-               {
-                       $this->db->query("SELECT descr FROM fm_district  where 
id='$id'");
-                       $this->db->next_record();
-                       return $this->db->f('descr');
-               }
-
-               function read($data)
-               {
-                       if(is_array($data))
-                       {
-                               $start  = 
(isset($data['start'])?$data['start']:0);
-                               $filter = 
(isset($data['filter'])?$data['filter']:'none');
-                               $query = 
(isset($data['query'])?$data['query']:'');
-                               $sort = 
(isset($data['sort'])?$data['sort']:'DESC');
-                               $order = 
(isset($data['order'])?$data['order']:'');
-                               $district_id = 
(isset($data['district_id'])?$data['district_id']:0);
-                               $allrows                = 
(isset($data['allrows'])?$data['allrows']:'');
-                       }
-
-                       if ($order)
-                       {
-                               $ordermethod = " order by $order $sort";
-                       }
-                       else
-                       {
-                               $ordermethod = ' order by part_of_town_id ASC';
-                       }
-
-
-                       $where = 'WHERE';
-                       if ($district_id > 0)
-                       {
-                               $filtermethod .= " $where 
district_id='$district_id' ";
-                               $where = 'AND';
-
-                       }
-
-                       if($query)
-                       {
-                               $query = ereg_replace("'",'',$query);
-                               $query = ereg_replace('"','',$query);
-
-                               $querymethod = " $where ( name $this->like 
'%$query%')";
-                       }
-
-                       $sql = "SELECT fm_part_of_town.*, descr as category 
FROM fm_part_of_town $this->join fm_district on 
fm_part_of_town.district_id=fm_district.id $filtermethod $querymethod";
-
-                       $this->db2->query($sql,__LINE__,__FILE__);
-                       $this->total_records = $this->db2->num_rows();
-
-                       if(!$allrows)
-                       {
-                               $this->db->limit_query($sql . 
$ordermethod,$start,__LINE__,__FILE__);
-                       }
-                       else
-                       {
-                               $this->db->query($sql . 
$ordermethod,__LINE__,__FILE__);
-                       }
-
-                       while ($this->db->next_record())
-                       {
-                               $p_of_towns[] = array
-                               (
-                                       'part_of_town_id'       => 
$this->db->f('part_of_town_id'),
-                                       'name'                          => 
stripslashes($this->db->f('name')),
-                                       'category'                      => 
stripslashes($this->db->f('category')),
-                                       'district_id'           => 
$this->db->f('district_id')
-                               );
-                       }
-                       return $p_of_towns;
-               }
-
-               function read_single($part_of_town_id)
-               {
-                       $this->db->query("select * from fm_part_of_town where 
part_of_town_id='$part_of_town_id'",__LINE__,__FILE__);
-
-                       if ($this->db->next_record())
-                       {
-                               $p_of_town['id']                        = 
(int)$this->db->f('part_of_town_id');
-                               $p_of_town['name']                      = 
stripslashes($this->db->f('name'));
-                               $p_of_town['district_id']       = 
(int)$this->db->f('district_id');
-
-                               return $p_of_town;
-                       }
-               }
-
-               function add($p_of_town)
-               {
-                       $p_of_town['name'] = 
$this->db->db_addslashes($p_of_town['name']);
-
-                       $this->db->query("INSERT INTO fm_part_of_town 
(name,district_id) "
-                               . "VALUES ('" . $p_of_town['name']
-                               . "','" . $p_of_town['district_id'] . 
"')",__LINE__,__FILE__);
-
-                       $receipt['part_of_town_id']= 
$this->db->get_last_insert_id('fm_part_of_town','part_of_town_id');
-                       $receipt['message'][] = array('msg'=>lang('Part of town 
%1 has been saved',$receipt['part_of_town_id']));
-                       return $receipt;
-               }
-
-               function edit($p_of_town)
-               {
-                       $p_of_town['name'] = 
$this->db->db_addslashes($p_of_town['name']);
-
-                       $this->db->query("UPDATE fm_part_of_town set name='" . 
$p_of_town['name'] . "', district_id='"
-                                                       . 
$p_of_town['district_id'] . "' WHERE part_of_town_id=" . 
intval($p_of_town['part_of_town_id']),__LINE__,__FILE__);
-
-                       $receipt['part_of_town_id']= 
$p_of_town['part_of_town_id'];
-                       $receipt['message'][] = array('msg'=>lang('Part of town 
%1 has been edited',$p_of_town['part_of_town_id']));
-                       return $receipt;
-               }
-
-               function delete($part_of_town_id)
-               {
-                       $this->db->query('DELETE FROM fm_part_of_town WHERE 
part_of_town_id=' . intval($part_of_town_id),__LINE__,__FILE__);
-               }
-       }
-?>
+                       $this->db2              = $this->bocommon->new_db();
+
+                       $this->join                     = $this->bocommon->join;
+                       $this->like                     = $this->bocommon->like;
+               }
+
+               function read_district_name($id)
+               {
+                       $this->db->query("SELECT descr FROM fm_district  where 
id='$id'");
+                       $this->db->next_record();
+                       return $this->db->f('descr');
+               }
+
+               function read($data)
+               {
+                       if(is_array($data))
+                       {
+                               $start  = 
(isset($data['start'])?$data['start']:0);
+                               $filter = 
(isset($data['filter'])?$data['filter']:'none');
+                               $query = 
(isset($data['query'])?$data['query']:'');
+                               $sort = 
(isset($data['sort'])?$data['sort']:'DESC');
+                               $order = 
(isset($data['order'])?$data['order']:'');
+                               $district_id = 
(isset($data['district_id'])?$data['district_id']:0);
+                               $allrows                = 
(isset($data['allrows'])?$data['allrows']:'');
+                       }
+
+                       if ($order)
+                       {
+                               $ordermethod = " order by $order $sort";
+                       }
+                       else
+                       {
+                               $ordermethod = ' order by part_of_town_id ASC';
+                       }
+
+
+                       $where = 'WHERE';
+                       if ($district_id > 0)
+                       {
+                               $filtermethod .= " $where 
district_id='$district_id' ";
+                               $where = 'AND';
+
+                       }
+
+                       if($query)
+                       {
+                               $query = ereg_replace("'",'',$query);
+                               $query = ereg_replace('"','',$query);
+
+                               $querymethod = " $where ( name $this->like 
'%$query%')";
+                       }
+
+                       $sql = "SELECT fm_part_of_town.*, descr as category 
FROM fm_part_of_town $this->join fm_district on 
fm_part_of_town.district_id=fm_district.id $filtermethod $querymethod";
+
+                       $this->db2->query($sql,__LINE__,__FILE__);
+                       $this->total_records = $this->db2->num_rows();
+
+                       if(!$allrows)
+                       {
+                               $this->db->limit_query($sql . 
$ordermethod,$start,__LINE__,__FILE__);
+                       }
+                       else
+                       {
+                               $this->db->query($sql . 
$ordermethod,__LINE__,__FILE__);
+                       }
+
+                       while ($this->db->next_record())
+                       {
+                               $p_of_towns[] = array
+                               (
+                                       'part_of_town_id'       => 
$this->db->f('part_of_town_id'),
+                                       'name'                          => 
stripslashes($this->db->f('name')),
+                                       'category'                      => 
stripslashes($this->db->f('category')),
+                                       'district_id'           => 
$this->db->f('district_id')
+                               );
+                       }
+                       return $p_of_towns;
+               }
+
+               function read_single($part_of_town_id)
+               {
+                       $this->db->query("select * from fm_part_of_town where 
part_of_town_id='$part_of_town_id'",__LINE__,__FILE__);
+
+                       if ($this->db->next_record())
+                       {
+                               $p_of_town['id']                        = 
(int)$this->db->f('part_of_town_id');
+                               $p_of_town['name']                      = 
stripslashes($this->db->f('name'));
+                               $p_of_town['district_id']       = 
(int)$this->db->f('district_id');
+
+                               return $p_of_town;
+                       }
+               }
+
+               function add($p_of_town)
+               {
+                       $p_of_town['name'] = 
$this->db->db_addslashes($p_of_town['name']);
+
+                       $this->db->query("INSERT INTO fm_part_of_town 
(name,district_id) "
+                               . "VALUES ('" . $p_of_town['name']
+                               . "','" . $p_of_town['district_id'] . 
"')",__LINE__,__FILE__);
+
+                       $receipt['part_of_town_id']= 
$this->db->get_last_insert_id('fm_part_of_town','part_of_town_id');
+                       $receipt['message'][] = array('msg'=>lang('Part of town 
%1 has been saved',$receipt['part_of_town_id']));
+                       return $receipt;
+               }
+
+               function edit($p_of_town)
+               {
+                       $p_of_town['name'] = 
$this->db->db_addslashes($p_of_town['name']);
+
+                       $this->db->query("UPDATE fm_part_of_town set name='" . 
$p_of_town['name'] . "', district_id='"
+                                                       . 
$p_of_town['district_id'] . "' WHERE part_of_town_id=" . 
intval($p_of_town['part_of_town_id']),__LINE__,__FILE__);
+
+                       $receipt['part_of_town_id']= 
$p_of_town['part_of_town_id'];
+                       $receipt['message'][] = array('msg'=>lang('Part of town 
%1 has been edited',$p_of_town['part_of_town_id']));
+                       return $receipt;
+               }
+
+               function delete($part_of_town_id)
+               {
+                       $this->db->query('DELETE FROM fm_part_of_town WHERE 
part_of_town_id=' . intval($part_of_town_id),__LINE__,__FILE__);
+               }
+       }
+?>
Index: property/inc/class.sopricebook.inc.php
diff -u property/inc/class.sopricebook.inc.php:1.13 
property/inc/class.sopricebook.inc.php:1.14
--- property/inc/class.sopricebook.inc.php:1.13 Fri Jan 27 14:05:43 2006
+++ property/inc/class.sopricebook.inc.php      Mon Jan 30 22:14:19 2006
@@ -1,794 +1,795 @@
-<?php
-       /**
-       * phpGroupWare - property: a Facilities Management System.
-       *
-       * @author Sigurd Nes <address@hidden>
-       * @copyright Copyright (C) 2003-2005 Free Software Foundation, Inc. 
http://www.fsf.org/
-       * @license http://www.gnu.org/licenses/gpl.html GNU General Public 
License
-       * @internal Development of this application was funded by 
http://www.bergen.kommune.no/bbb_/ekstern/
-       * @package property
-       * @subpackage agreement
-       * @version $Id: class.sopricebook.inc.php,v 1.13 2006/01/27 14:05:43 
sigurdne Exp $
-       */
-
-       /**
-        * Description
-        * @package property
-        */
-
-       class sopricebook
-       {
-
-               function sopricebook()
-               {
-                       $this->currentapp       = 
$GLOBALS['phpgw_info']['flags']['currentapp'];
-                       $this->account  = 
$GLOBALS['phpgw_info']['user']['account_id'];
-                       $this->bocommon         = 
CreateObject($this->currentapp.'.bocommon');
+<?php
+       /**
+       * phpGroupWare - property: a Facilities Management System.
+       *
+       * @author Sigurd Nes <address@hidden>
+       * @copyright Copyright (C) 2003-2005 Free Software Foundation, Inc. 
http://www.fsf.org/
+       * @license http://www.gnu.org/licenses/gpl.html GNU General Public 
License
+       * @internal Development of this application was funded by 
http://www.bergen.kommune.no/bbb_/ekstern/
+       * @package property
+       * @subpackage agreement
+       * @version $Id: class.sopricebook.inc.php,v 1.14 2006/01/30 22:14:19 
sigurdne Exp $
+       */
+
+       /**
+        * Description
+        * @package property
+        */
+
+       class sopricebook
+       {
+
+               function sopricebook()
+               {
+                       $this->currentapp       = 
$GLOBALS['phpgw_info']['flags']['currentapp'];
+                       $this->account  = 
$GLOBALS['phpgw_info']['user']['account_id'];
+                       $this->bocommon         = 
CreateObject($this->currentapp.'.bocommon');
                        $this->db               = $this->bocommon->new_db();
-                       $this->db2              = $this->bocommon->new_db();
-                       $this->join                     = $this->bocommon->join;
-                       $this->like                     = $this->bocommon->like;
-               }
-
-               function 
add_activity_first_prize($m_cost,$w_cost,$total_cost,$activity_id,$vendor_id,$date)
-               {
-                       $this->db->query("update fm_activity_price_index  set 
index_count='1',this_index='1', 
m_cost='$m_cost',w_cost='$w_cost',total_cost='$total_cost',index_date='$date',current_index='1'
 where activity_id='$activity_id' and vendor_id= '$vendor_id' and index_count= 
'1'",__LINE__,__FILE__);
-
-                       $receipt['message'][] = array('msg'=>lang('First entry 
is added!'));
-
-                       return $receipt;
-               }
-
-               function update_pricebook($update)
-               {
-                       for ($i=0; $i<count($update); $i++)
-                       {
-                               $this->db->query("select max(index_count) as 
max_index_count from fm_activity_price_index Where activity_id='". 
$update[$i]['activity_id'] . "' and vendor_id='".$update[$i]['vendor_id'] . 
"'",__LINE__,__FILE__);
-                               $this->db->next_record();
-                               $next_index_count  = 
$this->db->f('max_index_count')+1;
-
-                               $this->db->query("update 
fm_activity_price_index set current_index = Null"
-                               . " WHERE activity_id='" . 
$update[$i]['activity_id'] . "' and vendor_id='" . $update[$i]['vendor_id'] . 
"'",__LINE__,__FILE__);
-
-                               $this->db->query("insert into 
fm_activity_price_index (activity_id, vendor_id, index_count, this_index, 
m_cost, w_cost, total_cost, index_date,current_index) "
-                               . " values ('" .
-                                       $update[$i]['activity_id'] . "','" .
-                                       $update[$i]['vendor_id'] . "','" .
-                                       $next_index_count . "','" .
-                                       $update[$i]['new_index'] . "','" .
-                                       $update[$i]['new_m_cost'] . "','" .
-                                       $update[$i]['new_w_cost'] . "','" .
-                                       $update[$i]['new_total_cost'] . "','" .
-                                       $update[$i]['new_date']. "', 
'1')",__LINE__,__FILE__);
-                       }
-
-                       $receipt['message'][] = array('msg'=>lang('%1 entries 
is updated!',$i));
-
-                       return $receipt;
-
-               }
-
-               function get_vendor_list()
-               {
-                       $this->db->query("SELECT fm_vendor.org_name ,vendor_id "
-                               . " FROM fm_agreement $this->join fm_vendor ON 
fm_agreement.vendor_id = fm_vendor.id "
-                               . " WHERE fm_agreement.status='active'"
-                               . " GROUP by fm_vendor.org_name ,vendor_id "
-                               . " ORDER BY fm_vendor.org_name 
",__LINE__,__FILE__);
-                               while ($this->db->next_record())
-                               {
-//--------->fix this------->
-                                       if($this->db->f('vendor_id'))
-                                       {
-                                               $vendor_list[]=array
-                                               (
-                                                       'id'    => 
$this->db->f('vendor_id'),
-                                                       'name'  => 
$this->db->f('org_name')
-                                               );
-                                       }
-                               }
-                       return $vendor_list;
-               }
-
-               function get_agreement_group_list()
-               {
-                       $this->db->query("SELECT * FROM fm_agreement_group 
ORDER BY descr asc");
-                       while ($this->db->next_record())
-                       {
-                               $agreement_group_list[]=array
-                               (
-                                       'id'    => $this->db->f('id'),
-                                       'name'  => 
$GLOBALS['phpgw']->strip_html($this->db->f('descr')).' [ '. 
$GLOBALS['phpgw']->strip_html($this->db->f('status')).' ] '
-                               );
-                       }
-                       return $agreement_group_list;
-               }
-
-               function get_dim_d_list()
-               {
-                       $this->db->query("SELECT * FROM fm_dim_d ORDER BY descr 
asc");
-                       while ($this->db->next_record())
-                       {
-                               $dim_d_list[]=array
-                               (
-                                       'id'    => $this->db->f('id'),
-                                       'name'  => $this->db->f('id')
-                               );
-                       }
-                       return $dim_d_list;
-               }
-
-               function get_unit_list()
-               {
-                       $this->db->query("SELECT * FROM fm_standard_unit ORDER 
BY descr asc");
-                       while ($this->db->next_record())
-                       {
-                               $unit_list[]=array
-                               (
-                                       'id'    => $this->db->f('id'),
-                                       'name'  => 
$GLOBALS['phpgw']->strip_html($this->db->f('descr'))
-                               );
-                       }
-                       return $unit_list;
-               }
-
-               function get_branch_list()
-               {
-                       $this->db->query("SELECT * FROM fm_branch ORDER BY 
descr asc");
-                       while ($this->db->next_record())
-                       {
-                               $branch_list[]=array
-                               (
-                                       'id'    => $this->db->f('id'),
-                                       'name'  => 
$GLOBALS['phpgw']->strip_html($this->db->f('descr'))
-                               );
-                       }
-                       return $branch_list;
-               }
-
-               function check_activity_num($num='',$agreement_group_id='')
-               {
-                       $this->db->query("SELECT count(*) FROM fm_activities 
where num='$num' and agreement_group_id ='$agreement_group_id'");
-
-                       $this->db->next_record();
-
-                       if ( $this->db->f(0))
-                       {
-                               return True;
-                       }
-               }
-
-               function check_agreement_group_num($num='')
-               {
-                       $this->db->query("SELECT count(*) FROM 
fm_agreement_group where num='$num'");
-
-                       $this->db->next_record();
-
-                       if ( $this->db->f(0))
-                       {
-                               return True;
-                       }
-               }
-
-               function read($data)
-               {
-                       if(is_array($data))
-                       {
-                               if ($data['start'])
-                               {
-                                       $start=$data['start'];
-                               }
-                               else
-                               {
-                                       $start=0;
-                               }
-                               $filter = 
(isset($data['filter'])?$data['filter']:'none');
-                               $query = 
(isset($data['query'])?$data['query']:'');
-                               $sort = 
(isset($data['sort'])?$data['sort']:'DESC');
-                               $order = 
(isset($data['order'])?$data['order']:'');
-                               $cat_id = 
(isset($data['cat_id'])?$data['cat_id']:0);
-                               $allrows                = 
(isset($data['allrows'])?$data['allrows']:'');
-                       }
-
-//_debug_array($data);
-                       if ($order)
-                       {
-                               $ordermethod = " order by $order $sort";
-                       }
-                       else
-                       {
-                               $ordermethod = ' order by activity_id';
-                       }
-
-                       if ($cat_id > 0)
-                       {
-                               $filtermethod .= " AND cat_id='$cat_id' ";
-                               $vendor_condition= "= $cat_id";
-                       }
-                       else
-                       {
-                               $vendor_condition= " IS NULL";
-                       }
-
-                       if($query)
-                       {
-                               $query = ereg_replace("'",'',$query);
-                               $query = ereg_replace('"','',$query);
-
-                               $querymethod = " AND (fm_activities.descr 
$this->like '%$query%' or fm_activities.num $this->like '%$query%')";
-                       }
-
-
-                       $sql = "SELECT fm_activities.num, fm_activities.unit, 
fm_activities.dim_d, fm_activities.ns3420, fm_activities.descr AS descr,"
-                       . " fm_activities.base_descr, 
fm_activity_price_index.activity_id, fm_branch.descr AS branch,"
-                       . " fm_agreement.vendor_id, 
fm_activity_price_index.total_cost, fm_activity_price_index.m_cost,"
-                       . " fm_activity_price_index.w_cost, 
fm_activity_price_index.index_count, fm_activity_price_index.this_index, 
fm_agreement.id"
-                       . " FROM  fm_activities "
-                       . " $this->join fm_activity_price_index ON 
fm_activities.id = fm_activity_price_index.activity_id " 
-                       . " $this->join fm_branch ON fm_activities.branch_id = 
fm_branch.id "
-                       . " $this->join fm_agreement ON 
fm_activity_price_index.agreement_id = fm_agreement.id "
-                       . " WHERE fm_agreement.status='active' AND 
(fm_agreement.vendor_id $vendor_condition and current_index is not null "
-                       . " OR (fm_agreement.vendor_id $vendor_condition) AND 
(fm_activity_price_index.this_index IS NULL)) $querymethod";
-
-
-//echo $sql;
-
-
-                       $this->db2->query($sql,__LINE__,__FILE__);
-                       $this->total_records = $this->db2->num_rows();
-
-                       if(!$allrows)
-                       {
-                               $this->db->limit_query($sql . 
$ordermethod,$start,__LINE__,__FILE__);
-                       }
-                       else
-                       {
-                               $this->db->query($sql . 
$ordermethod,__LINE__,__FILE__);
-                       }
-
-                       while ($this->db->next_record())
-                       {
-                               $pricebook[] = array
-                               (
-                                       'activity_id'           => 
$this->db->f('activity_id'),
-                                       'num'                   => 
$this->db->f('num'),
-                                       'branch'                => 
$this->db->f('branch'),
-                                       'vendor_id'             => 
$this->db->f('vendor_id'),
-                                       'm_cost'                => 
$this->db->f('m_cost'),
-                                       'w_cost'                => 
$this->db->f('w_cost'),
-                                       'total_cost'            => 
$this->db->f('total_cost'),
-                                       'this_index'            => 
$this->db->f('this_index'),
-                                       'unit'                  => 
$this->db->f('unit'),
-                                       'dim_d'                 => 
$this->db->f('dim_d'),
-                                       'ns3420_id'             => 
$this->db->f('ns3420'),
-                                       'descr'                 => 
stripslashes($this->db->f('descr')),
-                                       'base_descr'            => 
stripslashes($this->db->f('base_descr')),
-                                       'index_count'           => 
$this->db->f('index_count')
-                               );
-                       }
-//             _debug_array($pricebook);
-                       return $pricebook;
-               }
-
-               function read_agreement_group($data)
-               {
-                       if(is_array($data))
-                       {
-                               if ($data['start'])
-                               {
-                                       $start=$data['start'];
-                               }
-                               else
-                               {
-                                       $start=0;
-                               }
-                               $filter = 
(isset($data['filter'])?$data['filter']:'none');
-                               $query = 
(isset($data['query'])?$data['query']:'');
-                               $sort = 
(isset($data['sort'])?$data['sort']:'DESC');
-                               $order = 
(isset($data['order'])?$data['order']:'');
-                               $cat_id = 
(isset($data['cat_id'])?$data['cat_id']:0);
-                               $allrows                = 
(isset($data['allrows'])?$data['allrows']:'');
-                       }
-
-                       if ($order)
-                       {
-                               $ordermethod = " order by $order $sort";
-                       }
-                       else
-                       {
-                               $ordermethod = ' order by id asc';
-                       }
-
-                       $where = 'WHERE';
-
-                       if ($cat_id)
-                       {
-                               $filtermethod .= " $where status='$cat_id' ";
-                               $where = 'AND';
-                       }
-
-                       if($query)
-                       {
-                               $query = ereg_replace("'",'',$query);
-                               $query = ereg_replace('"','',$query);
-
-                               $querymethod = " $where (descr $this->like 
'%$query%' or num $this->like '%$query%')";
-                       }
-
-                       $sql = "SELECT * FROM  fm_agreement_group $filtermethod 
$querymethod";
-
-                       $this->db2->query($sql,__LINE__,__FILE__);
-                       $this->total_records = $this->db2->num_rows();
-
-                       if(!$allrows)
-                       {
-                               $this->db->limit_query($sql . 
$ordermethod,$start,__LINE__,__FILE__);
-                       }
-                       else
-                       {
-                               $this->db->query($sql . 
$ordermethod,__LINE__,__FILE__);
-                       }
-
-                       while ($this->db->next_record())
-                       {
-                               $agreement_group[] = array
-                               (
-                                       'agreement_group_id'            => 
$this->db->f('id'),
-                                       'num'                           => 
$this->db->f('num'),
-                                       'status'                        => 
$this->db->f('status'),
-                                       'descr'                         => 
stripslashes($this->db->f('descr'))
-                               );
-                       }
-//             _debug_array($agreement_group);
-                       return $agreement_group;
-               }
-
-               function select_status_list()
-               {
-                       $this->db->query("SELECT id, descr FROM 
fm_agreement_status ORDER BY id ");
-
-                       $i = 0;
-                       while ($this->db->next_record())
-                       {
-                               $status_entries[$i]['id']                       
        = $this->db->f('id');
-                               $status_entries[$i]['name']                     
        = stripslashes($this->db->f('descr'));
-                               $i++;
-                       }
-                       return $status_entries;
-               }
-
-
-               function read_activity_prize($data)
-               {
-                       if(is_array($data))
-                       {
-                               if ($data['start'])
-                               {
-                                       $start=$data['start'];
-                               }
-                               else
-                               {
-                                       $start=0;
-                               }
-                               $filter = 
(isset($data['filter'])?$data['filter']:'none');
-                               $query = 
(isset($data['query'])?$data['query']:'');
-                               $sort = 
(isset($data['sort'])?$data['sort']:'DESC');
-                               $order = 
(isset($data['order'])?$data['order']:'');
-                               $cat_id = 
(isset($data['cat_id'])?$data['cat_id']:0);
-                               $activity_id = 
(isset($data['activity_id'])?$data['activity_id']:0);
-                               $vendor_id = 
(isset($data['vendor_id'])?$data['vendor_id']:0);
-                               $allrows                = 
(isset($data['allrows'])?$data['allrows']:'');
-                       }
-
-                       if ($order)
-                       {
-                               $ordermethod = " order by $order $sort";
-                       }
-                       else
-                       {
-                               $ordermethod = ' order by index_count';
-                       }
-
-                       if ($cat_id > 0)
-                       {
-                               $filtermethod .= " AND cat_id='$cat_id' ";
-                       }
-
-                       if($query)
-                       {
-                               $query = ereg_replace("'",'',$query);
-                               $query = ereg_replace('"','',$query);
-
-                               $querymethod = " AND (fm_activities.descr 
$this->like '%$query%' or fm_activities.num $this->like '%$query%')";
-                       }
-
-                       $sql = "SELECT 
index_count,this_index,current_index,m_cost,w_cost,total_cost,index_date"
-                               . " FROM fm_activity_price_index $this->join 
fm_agreement on fm_activity_price_index.agreement_id = fm_agreement.id "
-                               . " Where activity_id= '$activity_id' and 
vendor_id= '$vendor_id'";
-
-
-                       $this->db2->query($sql,__LINE__,__FILE__);
-                       $this->total_records = $this->db2->num_rows();
-
-                       if(!$allrows)
-                       {
-                               $this->db->limit_query($sql . 
$ordermethod,$start,__LINE__,__FILE__);
-                       }
-                       else
-                       {
-                               $this->db->query($sql . 
$ordermethod,__LINE__,__FILE__);
-                       }
-
-                       while ($this->db->next_record())
-                       {
-                               $pricebook[] = array
-                               (
-                                       'index_count'           => 
$this->db->f('index_count'),
-                                       'this_index'            => 
$this->db->f('this_index'),
-                                       'current_index'                 => 
$this->db->f('current_index'),
-                                       'm_cost'                        => 
$this->db->f('m_cost'),
-                                       'w_cost'                        => 
$this->db->f('w_cost'),
-                                       'total_cost'            => 
$this->db->f('total_cost'),
-                                       'date'                          => 
$this->db->f('index_date')
-                               );
-                       }
-//             _debug_array($pricebook);
-                       return $pricebook;
-               }
-
-               function read_activities_pr_agreement_group($data)
-               {
-                       if(is_array($data))
-                       {
-                               if ($data['start'])
-                               {
-                                       $start=$data['start'];
-                               }
-                               else
-                               {
-                                       $start=0;
-                               }
-                               $filter = 
(isset($data['filter'])?$data['filter']:'none');
-                               $query = 
(isset($data['query'])?$data['query']:'');
-                               $sort = 
(isset($data['sort'])?$data['sort']:'DESC');
-                               $order = 
(isset($data['order'])?$data['order']:'');
-                               $cat_id = 
(isset($data['cat_id'])?$data['cat_id']:0);
-                               $allrows                = 
(isset($data['allrows'])?$data['allrows']:'');
-                       }
-
-                       if ($order)
-                       {
-                               $ordermethod = " order by $order $sort";
-                       }
-                       else
-                       {
-                               $ordermethod = " order by activity_id asc";
-                       }
-
-                       if ($cat_id > 0)
-                       {
-                               $filtermethod .= " Where 
agreement_group_id='$cat_id' ";
-                       }
-                       else
-                       {
-                               $filtermethod = " Where agreement_group_id IS 
NULL";
-                       }
-                       if($query)
-                       {
-                               $query = ereg_replace("'",'',$query);
-                               $query = ereg_replace('"','',$query);
-
-//                             $querymethod = " AND (fm_activities.descr 
$this->like '%$query%' or fm_activities.num $this->like '%$query%')";
-                               $querymethod = " and (fm_activities.descr 
$this->like '%$query%' or fm_activities.base_descr $this->like '%$query%' or 
fm_activities.num $this->like '%$query%') ";
-                       }
-
-                       $sql = "SELECT fm_activities.id AS activity_id, 
fm_activities.num, fm_activities.base_descr, fm_activities.unit, 
fm_activities.dim_d, fm_branch.descr as branch, fm_activities.descr, ns3420 "
-                               . " FROM  fm_activities $this->join fm_branch 
on fm_activities.branch_id=fm_branch.id"
-                               . " $filtermethod $querymethod ";
-
-
-                       $this->db2->query($sql,__LINE__,__FILE__);
-                       $this->total_records = $this->db2->num_rows();
-
-                       if(!$allrows)
-                       {
-                               $this->db->limit_query($sql . 
$ordermethod,$start,__LINE__,__FILE__);
-                       }
-                       else
-                       {
-                               $this->db->query($sql . 
$ordermethod,__LINE__,__FILE__);
-                       }
-
-                       while ($this->db->next_record())
-                       {
-                               $pricebook[] = array
-                               (
-                                       'activity_id'   => 
$this->db->f('activity_id'),
-                                       'num'                   => 
$this->db->f('num'),
-                                       'base_descr'    => 
stripslashes($this->db->f('base_descr')),
-                                       'branch'                => 
$this->db->f('branch'),
-                                       'dim_d'                 => 
$this->db->f('dim_d'),
-                                       'ns3420'                => 
$this->db->f('ns3420'),
-                                       'unit'                  => 
$this->db->f('unit'),
-                                       'descr'                 => 
stripslashes($this->db->f('descr'))
-                               );
-                       }
-//             _debug_array($pricebook);
-                       return $pricebook;
-               }
-
-               function read_vendor_pr_activity($data)
-               {
-                       if(is_array($data))
-                       {
-                               if ($data['start'])
-                               {
-                                       $start=$data['start'];
-                               }
-                               else
-                               {
-                                       $start=0;
-                               }
-                               $filter                 = 
(isset($data['filter'])?$data['filter']:'none');
-                               $query                  = 
(isset($data['query'])?$data['query']:'');
-                               $sort                   = 
(isset($data['sort'])?$data['sort']:'DESC');
-                               $order                  = 
(isset($data['order'])?$data['order']:'');
-                               $cat_id                 = 
(isset($data['cat_id'])?$data['cat_id']:0);
-                               $allrows                = 
(isset($data['allrows'])?$data['allrows']:'');
-                               $activity_id    = 
(isset($data['activity_id'])?$data['activity_id']:'');
-                       }
-
-                       if ($order)
-                       {
-                               $ordermethod = " order by $order $sort";
-                       }
-                       else
-                       {
-                               $ordermethod = " order by fm_vendor.org_name 
asc";
-                       }
-
-                       if ($cat_id > 0)
-                       {
-                               $filtermethod .= " Where 
agreement_group_id='$cat_id' ";
-                       }
-                       else
-                       {
-                               $filtermethod = " Where agreement_group_id = 
''";
-                       }
-                       if($query)
-                       {
-                               $query = ereg_replace("'",'',$query);
-                               $query = ereg_replace('"','',$query);
-
-                               $querymethod = " AND (fm_vendor.org_name 
$this->like '%$query%' or vendor_id $this->like '%$query%')";
-                       }
-
-                       $sql = "SELECT fm_activities.id as 
activity_id,fm_activities.num, fm_vendor.org_name,fm_branch.descr as branch 
,fm_activity_price_index.vendor_id "
-                               . " FROM (fm_activities  $this->join 
fm_activity_price_index ON fm_activities.id = 
fm_activity_price_index.activity_id) "
-                               . " $this->join fm_agreement ON 
fm_activity_price_index.agreement_id = fm_agreement.id "
-                               . " $this->join fm_vendor ON 
fm_agreement.vendor_id = fm_vendor.id "
-                               . " $this->join fm_branch on fm_branch.id = 
fm_activities.branch_id "
-                               . " Where fm_activity_price_index.activity_id= 
'$activity_id' $querymethod group by fm_activities.id,fm_activities.num,"
-                               . " fm_branch.descr,org_name 
,fm_activity_price_index.vendor_id ";
-
-                       $this->db2->query($sql,__LINE__,__FILE__);
-                       $this->total_records = $this->db2->num_rows();
-
-                       if(!$allrows)
-                       {
-                               $this->db->limit_query($sql . 
$ordermethod,$start,__LINE__,__FILE__);
-                       }
-                       else
-                       {
-                               $this->db->query($sql . 
$ordermethod,__LINE__,__FILE__);
-                       }
-
-                       while ($this->db->next_record())
-                       {
-                               $pricebook[] = array
-                               (
-                                       'activity_id'   => 
$this->db->f('activity_id'),
-                                       'num'                   => 
$this->db->f('num'),
-                                       'branch'                => 
$this->db->f('branch'),
-                                       'vendor_name'   => 
$this->db->f('org_name'),
-                                       'vendor_id'             => 
$this->db->f('vendor_id')
-                               );
-                       }
-                       return $pricebook;
-               }
-
-               function read_single_activity($id)
-               {
-                       $sql = "SELECT * FROM fm_activities where id='$id'";
-
-                       $this->db->query($sql,__LINE__,__FILE__);
-
-                       if ($this->db->next_record())
-                       {
-                               $activity['activity_id']= $id;
-                               $activity['num']                = 
$this->db->f('num');
-                               $activity['unit']               = 
$this->db->f('unit');
-                               $activity['cat_id']             = 
$this->db->f('agreement_group_id');
-                               $activity['ns3420_id']  = 
$this->db->f('ns3420');
-                               $activity['descr']              = 
stripslashes($this->db->f('descr'));
-                               $activity['base_descr'] = 
stripslashes($this->db->f('base_descr'));
-                               $activity['dim_d']              = 
$this->db->f('dim_d');
-                               $activity['branch_id']  = 
$this->db->f('branch_id');
-
-                               return $activity;
-                       }
-               }
-
-               function read_single_agreement_group($id)
-               {
-                       $sql = "SELECT * FROM fm_agreement_group where 
id='$id'";
-
-                       $this->db->query($sql,__LINE__,__FILE__);
-
-                       if ($this->db->next_record())
-                       {
-                               $agreement_group['agreement_group_id']  = $id;
-                               $agreement_group['num']                 = 
$this->db->f('num');
-                               $agreement_group['status']              = 
$this->db->f('status');
-                               $agreement_group['descr']                       
= stripslashes($this->db->f('descr'));
-
-                               return $agreement_group;
-                       }
-               }
-
-               function add_activity($values)
-               {
-                       $values['descr'] = 
$this->db->db_addslashes($values['descr']);
-                       $values['base_descr'] = 
$this->db->db_addslashes($values['base_descr']);
-
-                       $vals= array(
-                               $values['activity_id'],
-                               $values['num'] ,
-                               $values['unit'] ,
-                               $values['cat_id'] , // agreement_group
-                               $values['ns3420_id'] ,
-                               $values['dim_d'],
-                               $values['branch_id'],
-                               $values['descr'],
-                               $values['base_descr']
-                               );
-
-                       $vals   = $this->bocommon->validate_db_insert($vals);
-
-                       $this->db->transaction_begin();
-                       $this->db->query("INSERT INTO fm_activities (id, 
num,unit,agreement_group_id,ns3420,dim_d,branch_id,descr,base_descr) "
-                               . "VALUES ($vals)",__LINE__,__FILE__);
-                       $this->db->transaction_commit();
-
-                       $receipt['message'][] = array('msg'=>lang('Activity has 
been saved'));
-                       $receipt['activity_id']= $values['activity_id'];
-                       return $receipt;
-               }
-
-               function edit_activity($values)
-               {
-                       $values['descr'] = 
$this->db->db_addslashes($values['descr']);
-                       $values['base_descr'] = 
$this->db->db_addslashes($values['base_descr']);
-
-                       $value_set=array(
-                               'num'                                   => 
$values['num'],
-                               'unit'                                  => 
$values['unit'],
-                               'agreement_group_id'    => $values['cat_id'],
-                               'ns3420'                                => 
$values['ns3420_id'],
-                               'dim_d'                                 => 
$values['dim_d'],
-                               'branch_id'                             => 
$values['branch_id'],
-                               'descr'                                 => 
$values['descr'],
-                               'base_descr'                    => 
$values['base_descr']
-                               );
-
-                       $value_set      = 
$this->bocommon->validate_db_update($value_set);
-                       $this->db->transaction_begin();
-                       $this->db->query("UPDATE fm_activities set $value_set 
WHERE id= '" . $values['activity_id'] . "'",__LINE__,__FILE__);
-                       $this->db->transaction_commit();
-
-                       $receipt['activity_id']= $values['activity_id'];
-                       $receipt['message'][] = array('msg'=>lang('Activity has 
been edited'));
-                       return $receipt;
-               }
-
-               function add_agreement_group($values)
-               {
-                       $values['descr'] = 
$this->db->db_addslashes($values['descr']);
-
-                       $vals= array(
-                               $values['agreement_group_id'],
-                               $values['num'],
-                               $values['status'],
-                               $values['descr']
-                               );
-
-                       $vals   = $this->bocommon->validate_db_insert($vals);
-
-                       $this->db->query("INSERT INTO fm_agreement_group 
(id,num,status,descr) "
-                               . "VALUES ($vals)",__LINE__,__FILE__);
-
-                       $receipt['message'][] = array('msg'=>lang('Agreement 
group has been saved'));
-                       $receipt['agreement_group_id'] = 
$values['agreement_group_id'];
-                       return $receipt;
-               }
-
-               function edit_agreement_group($values)
-               {
-                       $values['descr'] = 
$this->db->db_addslashes($values['descr']);
-
-                       $value_set=array(
-                               'num'   => $values['num'],
-                               'status'=> $values['status'],
-                               'descr' => $values['descr']
-                               );
-
-                       $value_set      = 
$this->bocommon->validate_db_update($value_set);
-                       $this->db->transaction_begin();
-
-                       $this->db->query("UPDATE fm_agreement_group set 
$value_set WHERE id= '" . $values['agreement_group_id'] . 
"'",__LINE__,__FILE__);
-
-                       $this->db->transaction_commit();
-
-                       $receipt['message'][] = 
array('msg'=>lang('Agreement_group has been edited'));
-                       return $receipt;
-               }
-
-               function delete_activity_vendor($activity_id,$vendor_id)
-               {
-                       $this->db->query("DELETE FROM fm_activity_price_index 
WHERE activity_id='$activity_id' and vendor_id='$vendor_id'",__LINE__,__FILE__);
-               }
-
-               function delete_activity($activity_id)
-               {
-                       $this->db->query("DELETE FROM fm_activities WHERE 
id='$activity_id'",__LINE__,__FILE__);
-                       $this->db->query("DELETE FROM fm_activity_price_index 
WHERE activity_id='$activity_id'",__LINE__,__FILE__);
-               }
-
-               function 
delete_prize_index($activity_id,$vendor_id,$index_count)
-               {
-                       if ($index_count==1)
-                       {
-                               $this->db->query("update 
fm_activity_price_index set index_count = '1', current_index = '0', 
this_index=Null, m_cost=Null,w_cost=Null,total_cost=Null,index_date=Null  where 
activity_id='$activity_id' and vendor_id= '$vendor_id' and index_count= 
'1'",__LINE__,__FILE__);
-                       }
-                       else
-                       {
-                               $this->db->query("delete from 
fm_activity_price_index where activity_id='$activity_id' and vendor_id= 
'$vendor_id' and index_count= '$index_count'",__LINE__,__FILE__);
-
-                               $new_index_count= $index_count -1;
-
-                               $this->db->query("update 
fm_activity_price_index set current_index = '1' where 
activity_id='$activity_id' and vendor_id= '$vendor_id' and index_count= 
'$new_index_count'",__LINE__,__FILE__);
-                       }
-
-               }
-
-               function delete_agreement_group($agreement_group_id)
-               {
-                       $this->db->query("DELETE FROM fm_agreement_group WHERE 
id='$agreement_group_id'",__LINE__,__FILE__);
-//how to handle the activities and vendors ...?
-               }
-
-
-               function add_activity_vendor($values)
-               {
-                       $this->db->query("SELECT count(*) FROM 
fm_activity_price_index WHERE activity_id='" . $values['activity_id'] . "' and 
vendor_id='" . $values['vendor_id'] . "'",__LINE__,__FILE__);
-
-                       $this->db->next_record();
-
-                       if ( $this->db->f(0))
-                       {
-                               $receipt['error'][] = array('msg'=>lang('This 
Vendor is already registered for this activity'));
-                       }
-                       else
-                       {
-                               $this->db->query("insert into 
fm_activity_price_index (activity_id, vendor_id, 
index_count,current_index,m_cost,w_cost,total_cost) "
-                                       . " values ('" .
-                                       $values['activity_id']. "','" .
-                                       $values['vendor_id']. 
"','1','0',NULL,NULL,NULL)",__LINE__,__FILE__);
-
-                               $receipt['message'][] = 
array('msg'=>lang('Vendor has been added'));
-
-                       }
-                       return $receipt;
-               }
-       }
-?>
+                       $this->db2              = $this->bocommon->new_db();
+
+                       $this->join                     = $this->bocommon->join;
+                       $this->like                     = $this->bocommon->like;
+               }
+
+               function 
add_activity_first_prize($m_cost,$w_cost,$total_cost,$activity_id,$vendor_id,$date)
+               {
+                       $this->db->query("update fm_activity_price_index  set 
index_count='1',this_index='1', 
m_cost='$m_cost',w_cost='$w_cost',total_cost='$total_cost',index_date='$date',current_index='1'
 where activity_id='$activity_id' and vendor_id= '$vendor_id' and index_count= 
'1'",__LINE__,__FILE__);
+
+                       $receipt['message'][] = array('msg'=>lang('First entry 
is added!'));
+
+                       return $receipt;
+               }
+
+               function update_pricebook($update)
+               {
+                       for ($i=0; $i<count($update); $i++)
+                       {
+                               $this->db->query("select max(index_count) as 
max_index_count from fm_activity_price_index Where activity_id='". 
$update[$i]['activity_id'] . "' and vendor_id='".$update[$i]['vendor_id'] . 
"'",__LINE__,__FILE__);
+                               $this->db->next_record();
+                               $next_index_count  = 
$this->db->f('max_index_count')+1;
+
+                               $this->db->query("update 
fm_activity_price_index set current_index = Null"
+                               . " WHERE activity_id='" . 
$update[$i]['activity_id'] . "' and vendor_id='" . $update[$i]['vendor_id'] . 
"'",__LINE__,__FILE__);
+
+                               $this->db->query("insert into 
fm_activity_price_index (activity_id, vendor_id, index_count, this_index, 
m_cost, w_cost, total_cost, index_date,current_index) "
+                               . " values ('" .
+                                       $update[$i]['activity_id'] . "','" .
+                                       $update[$i]['vendor_id'] . "','" .
+                                       $next_index_count . "','" .
+                                       $update[$i]['new_index'] . "','" .
+                                       $update[$i]['new_m_cost'] . "','" .
+                                       $update[$i]['new_w_cost'] . "','" .
+                                       $update[$i]['new_total_cost'] . "','" .
+                                       $update[$i]['new_date']. "', 
'1')",__LINE__,__FILE__);
+                       }
+
+                       $receipt['message'][] = array('msg'=>lang('%1 entries 
is updated!',$i));
+
+                       return $receipt;
+
+               }
+
+               function get_vendor_list()
+               {
+                       $this->db->query("SELECT fm_vendor.org_name ,vendor_id "
+                               . " FROM fm_agreement $this->join fm_vendor ON 
fm_agreement.vendor_id = fm_vendor.id "
+                               . " WHERE fm_agreement.status='active'"
+                               . " GROUP by fm_vendor.org_name ,vendor_id "
+                               . " ORDER BY fm_vendor.org_name 
",__LINE__,__FILE__);
+                               while ($this->db->next_record())
+                               {
+//--------->fix this------->
+                                       if($this->db->f('vendor_id'))
+                                       {
+                                               $vendor_list[]=array
+                                               (
+                                                       'id'    => 
$this->db->f('vendor_id'),
+                                                       'name'  => 
$this->db->f('org_name')
+                                               );
+                                       }
+                               }
+                       return $vendor_list;
+               }
+
+               function get_agreement_group_list()
+               {
+                       $this->db->query("SELECT * FROM fm_agreement_group 
ORDER BY descr asc");
+                       while ($this->db->next_record())
+                       {
+                               $agreement_group_list[]=array
+                               (
+                                       'id'    => $this->db->f('id'),
+                                       'name'  => 
$GLOBALS['phpgw']->strip_html($this->db->f('descr')).' [ '. 
$GLOBALS['phpgw']->strip_html($this->db->f('status')).' ] '
+                               );
+                       }
+                       return $agreement_group_list;
+               }
+
+               function get_dim_d_list()
+               {
+                       $this->db->query("SELECT * FROM fm_dim_d ORDER BY descr 
asc");
+                       while ($this->db->next_record())
+                       {
+                               $dim_d_list[]=array
+                               (
+                                       'id'    => $this->db->f('id'),
+                                       'name'  => $this->db->f('id')
+                               );
+                       }
+                       return $dim_d_list;
+               }
+
+               function get_unit_list()
+               {
+                       $this->db->query("SELECT * FROM fm_standard_unit ORDER 
BY descr asc");
+                       while ($this->db->next_record())
+                       {
+                               $unit_list[]=array
+                               (
+                                       'id'    => $this->db->f('id'),
+                                       'name'  => 
$GLOBALS['phpgw']->strip_html($this->db->f('descr'))
+                               );
+                       }
+                       return $unit_list;
+               }
+
+               function get_branch_list()
+               {
+                       $this->db->query("SELECT * FROM fm_branch ORDER BY 
descr asc");
+                       while ($this->db->next_record())
+                       {
+                               $branch_list[]=array
+                               (
+                                       'id'    => $this->db->f('id'),
+                                       'name'  => 
$GLOBALS['phpgw']->strip_html($this->db->f('descr'))
+                               );
+                       }
+                       return $branch_list;
+               }
+
+               function check_activity_num($num='',$agreement_group_id='')
+               {
+                       $this->db->query("SELECT count(*) FROM fm_activities 
where num='$num' and agreement_group_id ='$agreement_group_id'");
+
+                       $this->db->next_record();
+
+                       if ( $this->db->f(0))
+                       {
+                               return True;
+                       }
+               }
+
+               function check_agreement_group_num($num='')
+               {
+                       $this->db->query("SELECT count(*) FROM 
fm_agreement_group where num='$num'");
+
+                       $this->db->next_record();
+
+                       if ( $this->db->f(0))
+                       {
+                               return True;
+                       }
+               }
+
+               function read($data)
+               {
+                       if(is_array($data))
+                       {
+                               if ($data['start'])
+                               {
+                                       $start=$data['start'];
+                               }
+                               else
+                               {
+                                       $start=0;
+                               }
+                               $filter = 
(isset($data['filter'])?$data['filter']:'none');
+                               $query = 
(isset($data['query'])?$data['query']:'');
+                               $sort = 
(isset($data['sort'])?$data['sort']:'DESC');
+                               $order = 
(isset($data['order'])?$data['order']:'');
+                               $cat_id = 
(isset($data['cat_id'])?$data['cat_id']:0);
+                               $allrows                = 
(isset($data['allrows'])?$data['allrows']:'');
+                       }
+
+//_debug_array($data);
+                       if ($order)
+                       {
+                               $ordermethod = " order by $order $sort";
+                       }
+                       else
+                       {
+                               $ordermethod = ' order by activity_id';
+                       }
+
+                       if ($cat_id > 0)
+                       {
+                               $filtermethod .= " AND cat_id='$cat_id' ";
+                               $vendor_condition= "= $cat_id";
+                       }
+                       else
+                       {
+                               $vendor_condition= " IS NULL";
+                       }
+
+                       if($query)
+                       {
+                               $query = ereg_replace("'",'',$query);
+                               $query = ereg_replace('"','',$query);
+
+                               $querymethod = " AND (fm_activities.descr 
$this->like '%$query%' or fm_activities.num $this->like '%$query%')";
+                       }
+
+
+                       $sql = "SELECT fm_activities.num, fm_activities.unit, 
fm_activities.dim_d, fm_activities.ns3420, fm_activities.descr AS descr,"
+                       . " fm_activities.base_descr, 
fm_activity_price_index.activity_id, fm_branch.descr AS branch,"
+                       . " fm_agreement.vendor_id, 
fm_activity_price_index.total_cost, fm_activity_price_index.m_cost,"
+                       . " fm_activity_price_index.w_cost, 
fm_activity_price_index.index_count, fm_activity_price_index.this_index, 
fm_agreement.id"
+                       . " FROM  fm_activities "
+                       . " $this->join fm_activity_price_index ON 
fm_activities.id = fm_activity_price_index.activity_id "
+                       . " $this->join fm_branch ON fm_activities.branch_id = 
fm_branch.id "
+                       . " $this->join fm_agreement ON 
fm_activity_price_index.agreement_id = fm_agreement.id "
+                       . " WHERE fm_agreement.status='active' AND 
(fm_agreement.vendor_id $vendor_condition and current_index is not null "
+                       . " OR (fm_agreement.vendor_id $vendor_condition) AND 
(fm_activity_price_index.this_index IS NULL)) $querymethod";
+
+
+//echo $sql;
+
+
+                       $this->db2->query($sql,__LINE__,__FILE__);
+                       $this->total_records = $this->db2->num_rows();
+
+                       if(!$allrows)
+                       {
+                               $this->db->limit_query($sql . 
$ordermethod,$start,__LINE__,__FILE__);
+                       }
+                       else
+                       {
+                               $this->db->query($sql . 
$ordermethod,__LINE__,__FILE__);
+                       }
+
+                       while ($this->db->next_record())
+                       {
+                               $pricebook[] = array
+                               (
+                                       'activity_id'           => 
$this->db->f('activity_id'),
+                                       'num'                   => 
$this->db->f('num'),
+                                       'branch'                => 
$this->db->f('branch'),
+                                       'vendor_id'             => 
$this->db->f('vendor_id'),
+                                       'm_cost'                => 
$this->db->f('m_cost'),
+                                       'w_cost'                => 
$this->db->f('w_cost'),
+                                       'total_cost'            => 
$this->db->f('total_cost'),
+                                       'this_index'            => 
$this->db->f('this_index'),
+                                       'unit'                  => 
$this->db->f('unit'),
+                                       'dim_d'                 => 
$this->db->f('dim_d'),
+                                       'ns3420_id'             => 
$this->db->f('ns3420'),
+                                       'descr'                 => 
stripslashes($this->db->f('descr')),
+                                       'base_descr'            => 
stripslashes($this->db->f('base_descr')),
+                                       'index_count'           => 
$this->db->f('index_count')
+                               );
+                       }
+//             _debug_array($pricebook);
+                       return $pricebook;
+               }
+
+               function read_agreement_group($data)
+               {
+                       if(is_array($data))
+                       {
+                               if ($data['start'])
+                               {
+                                       $start=$data['start'];
+                               }
+                               else
+                               {
+                                       $start=0;
+                               }
+                               $filter = 
(isset($data['filter'])?$data['filter']:'none');
+                               $query = 
(isset($data['query'])?$data['query']:'');
+                               $sort = 
(isset($data['sort'])?$data['sort']:'DESC');
+                               $order = 
(isset($data['order'])?$data['order']:'');
+                               $cat_id = 
(isset($data['cat_id'])?$data['cat_id']:0);
+                               $allrows                = 
(isset($data['allrows'])?$data['allrows']:'');
+                       }
+
+                       if ($order)
+                       {
+                               $ordermethod = " order by $order $sort";
+                       }
+                       else
+                       {
+                               $ordermethod = ' order by id asc';
+                       }
+
+                       $where = 'WHERE';
+
+                       if ($cat_id)
+                       {
+                               $filtermethod .= " $where status='$cat_id' ";
+                               $where = 'AND';
+                       }
+
+                       if($query)
+                       {
+                               $query = ereg_replace("'",'',$query);
+                               $query = ereg_replace('"','',$query);
+
+                               $querymethod = " $where (descr $this->like 
'%$query%' or num $this->like '%$query%')";
+                       }
+
+                       $sql = "SELECT * FROM  fm_agreement_group $filtermethod 
$querymethod";
+
+                       $this->db2->query($sql,__LINE__,__FILE__);
+                       $this->total_records = $this->db2->num_rows();
+
+                       if(!$allrows)
+                       {
+                               $this->db->limit_query($sql . 
$ordermethod,$start,__LINE__,__FILE__);
+                       }
+                       else
+                       {
+                               $this->db->query($sql . 
$ordermethod,__LINE__,__FILE__);
+                       }
+
+                       while ($this->db->next_record())
+                       {
+                               $agreement_group[] = array
+                               (
+                                       'agreement_group_id'            => 
$this->db->f('id'),
+                                       'num'                           => 
$this->db->f('num'),
+                                       'status'                        => 
$this->db->f('status'),
+                                       'descr'                         => 
stripslashes($this->db->f('descr'))
+                               );
+                       }
+//             _debug_array($agreement_group);
+                       return $agreement_group;
+               }
+
+               function select_status_list()
+               {
+                       $this->db->query("SELECT id, descr FROM 
fm_agreement_status ORDER BY id ");
+
+                       $i = 0;
+                       while ($this->db->next_record())
+                       {
+                               $status_entries[$i]['id']                       
        = $this->db->f('id');
+                               $status_entries[$i]['name']                     
        = stripslashes($this->db->f('descr'));
+                               $i++;
+                       }
+                       return $status_entries;
+               }
+
+
+               function read_activity_prize($data)
+               {
+                       if(is_array($data))
+                       {
+                               if ($data['start'])
+                               {
+                                       $start=$data['start'];
+                               }
+                               else
+                               {
+                                       $start=0;
+                               }
+                               $filter = 
(isset($data['filter'])?$data['filter']:'none');
+                               $query = 
(isset($data['query'])?$data['query']:'');
+                               $sort = 
(isset($data['sort'])?$data['sort']:'DESC');
+                               $order = 
(isset($data['order'])?$data['order']:'');
+                               $cat_id = 
(isset($data['cat_id'])?$data['cat_id']:0);
+                               $activity_id = 
(isset($data['activity_id'])?$data['activity_id']:0);
+                               $vendor_id = 
(isset($data['vendor_id'])?$data['vendor_id']:0);
+                               $allrows                = 
(isset($data['allrows'])?$data['allrows']:'');
+                       }
+
+                       if ($order)
+                       {
+                               $ordermethod = " order by $order $sort";
+                       }
+                       else
+                       {
+                               $ordermethod = ' order by index_count';
+                       }
+
+                       if ($cat_id > 0)
+                       {
+                               $filtermethod .= " AND cat_id='$cat_id' ";
+                       }
+
+                       if($query)
+                       {
+                               $query = ereg_replace("'",'',$query);
+                               $query = ereg_replace('"','',$query);
+
+                               $querymethod = " AND (fm_activities.descr 
$this->like '%$query%' or fm_activities.num $this->like '%$query%')";
+                       }
+
+                       $sql = "SELECT 
index_count,this_index,current_index,m_cost,w_cost,total_cost,index_date"
+                               . " FROM fm_activity_price_index $this->join 
fm_agreement on fm_activity_price_index.agreement_id = fm_agreement.id "
+                               . " Where activity_id= '$activity_id' and 
vendor_id= '$vendor_id'";
+
+
+                       $this->db2->query($sql,__LINE__,__FILE__);
+                       $this->total_records = $this->db2->num_rows();
+
+                       if(!$allrows)
+                       {
+                               $this->db->limit_query($sql . 
$ordermethod,$start,__LINE__,__FILE__);
+                       }
+                       else
+                       {
+                               $this->db->query($sql . 
$ordermethod,__LINE__,__FILE__);
+                       }
+
+                       while ($this->db->next_record())
+                       {
+                               $pricebook[] = array
+                               (
+                                       'index_count'           => 
$this->db->f('index_count'),
+                                       'this_index'            => 
$this->db->f('this_index'),
+                                       'current_index'                 => 
$this->db->f('current_index'),
+                                       'm_cost'                        => 
$this->db->f('m_cost'),
+                                       'w_cost'                        => 
$this->db->f('w_cost'),
+                                       'total_cost'            => 
$this->db->f('total_cost'),
+                                       'date'                          => 
$this->db->f('index_date')
+                               );
+                       }
+//             _debug_array($pricebook);
+                       return $pricebook;
+               }
+
+               function read_activities_pr_agreement_group($data)
+               {
+                       if(is_array($data))
+                       {
+                               if ($data['start'])
+                               {
+                                       $start=$data['start'];
+                               }
+                               else
+                               {
+                                       $start=0;
+                               }
+                               $filter = 
(isset($data['filter'])?$data['filter']:'none');
+                               $query = 
(isset($data['query'])?$data['query']:'');
+                               $sort = 
(isset($data['sort'])?$data['sort']:'DESC');
+                               $order = 
(isset($data['order'])?$data['order']:'');
+                               $cat_id = 
(isset($data['cat_id'])?$data['cat_id']:0);
+                               $allrows                = 
(isset($data['allrows'])?$data['allrows']:'');
+                       }
+
+                       if ($order)
+                       {
+                               $ordermethod = " order by $order $sort";
+                       }
+                       else
+                       {
+                               $ordermethod = " order by activity_id asc";
+                       }
+
+                       if ($cat_id > 0)
+                       {
+                               $filtermethod .= " Where 
agreement_group_id='$cat_id' ";
+                       }
+                       else
+                       {
+                               $filtermethod = " Where agreement_group_id IS 
NULL";
+                       }
+                       if($query)
+                       {
+                               $query = ereg_replace("'",'',$query);
+                               $query = ereg_replace('"','',$query);
+
+//                             $querymethod = " AND (fm_activities.descr 
$this->like '%$query%' or fm_activities.num $this->like '%$query%')";
+                               $querymethod = " and (fm_activities.descr 
$this->like '%$query%' or fm_activities.base_descr $this->like '%$query%' or 
fm_activities.num $this->like '%$query%') ";
+                       }
+
+                       $sql = "SELECT fm_activities.id AS activity_id, 
fm_activities.num, fm_activities.base_descr, fm_activities.unit, 
fm_activities.dim_d, fm_branch.descr as branch, fm_activities.descr, ns3420 "
+                               . " FROM  fm_activities $this->join fm_branch 
on fm_activities.branch_id=fm_branch.id"
+                               . " $filtermethod $querymethod ";
+
+
+                       $this->db2->query($sql,__LINE__,__FILE__);
+                       $this->total_records = $this->db2->num_rows();
+
+                       if(!$allrows)
+                       {
+                               $this->db->limit_query($sql . 
$ordermethod,$start,__LINE__,__FILE__);
+                       }
+                       else
+                       {
+                               $this->db->query($sql . 
$ordermethod,__LINE__,__FILE__);
+                       }
+
+                       while ($this->db->next_record())
+                       {
+                               $pricebook[] = array
+                               (
+                                       'activity_id'   => 
$this->db->f('activity_id'),
+                                       'num'                   => 
$this->db->f('num'),
+                                       'base_descr'    => 
stripslashes($this->db->f('base_descr')),
+                                       'branch'                => 
$this->db->f('branch'),
+                                       'dim_d'                 => 
$this->db->f('dim_d'),
+                                       'ns3420'                => 
$this->db->f('ns3420'),
+                                       'unit'                  => 
$this->db->f('unit'),
+                                       'descr'                 => 
stripslashes($this->db->f('descr'))
+                               );
+                       }
+//             _debug_array($pricebook);
+                       return $pricebook;
+               }
+
+               function read_vendor_pr_activity($data)
+               {
+                       if(is_array($data))
+                       {
+                               if ($data['start'])
+                               {
+                                       $start=$data['start'];
+                               }
+                               else
+                               {
+                                       $start=0;
+                               }
+                               $filter                 = 
(isset($data['filter'])?$data['filter']:'none');
+                               $query                  = 
(isset($data['query'])?$data['query']:'');
+                               $sort                   = 
(isset($data['sort'])?$data['sort']:'DESC');
+                               $order                  = 
(isset($data['order'])?$data['order']:'');
+                               $cat_id                 = 
(isset($data['cat_id'])?$data['cat_id']:0);
+                               $allrows                = 
(isset($data['allrows'])?$data['allrows']:'');
+                               $activity_id    = 
(isset($data['activity_id'])?$data['activity_id']:'');
+                       }
+
+                       if ($order)
+                       {
+                               $ordermethod = " order by $order $sort";
+                       }
+                       else
+                       {
+                               $ordermethod = " order by fm_vendor.org_name 
asc";
+                       }
+
+                       if ($cat_id > 0)
+                       {
+                               $filtermethod .= " Where 
agreement_group_id='$cat_id' ";
+                       }
+                       else
+                       {
+                               $filtermethod = " Where agreement_group_id = 
''";
+                       }
+                       if($query)
+                       {
+                               $query = ereg_replace("'",'',$query);
+                               $query = ereg_replace('"','',$query);
+
+                               $querymethod = " AND (fm_vendor.org_name 
$this->like '%$query%' or vendor_id $this->like '%$query%')";
+                       }
+
+                       $sql = "SELECT fm_activities.id as 
activity_id,fm_activities.num, fm_vendor.org_name,fm_branch.descr as branch 
,fm_activity_price_index.vendor_id "
+                               . " FROM (fm_activities  $this->join 
fm_activity_price_index ON fm_activities.id = 
fm_activity_price_index.activity_id) "
+                               . " $this->join fm_agreement ON 
fm_activity_price_index.agreement_id = fm_agreement.id "
+                               . " $this->join fm_vendor ON 
fm_agreement.vendor_id = fm_vendor.id "
+                               . " $this->join fm_branch on fm_branch.id = 
fm_activities.branch_id "
+                               . " Where fm_activity_price_index.activity_id= 
'$activity_id' $querymethod group by fm_activities.id,fm_activities.num,"
+                               . " fm_branch.descr,org_name 
,fm_activity_price_index.vendor_id ";
+
+                       $this->db2->query($sql,__LINE__,__FILE__);
+                       $this->total_records = $this->db2->num_rows();
+
+                       if(!$allrows)
+                       {
+                               $this->db->limit_query($sql . 
$ordermethod,$start,__LINE__,__FILE__);
+                       }
+                       else
+                       {
+                               $this->db->query($sql . 
$ordermethod,__LINE__,__FILE__);
+                       }
+
+                       while ($this->db->next_record())
+                       {
+                               $pricebook[] = array
+                               (
+                                       'activity_id'   => 
$this->db->f('activity_id'),
+                                       'num'                   => 
$this->db->f('num'),
+                                       'branch'                => 
$this->db->f('branch'),
+                                       'vendor_name'   => 
$this->db->f('org_name'),
+                                       'vendor_id'             => 
$this->db->f('vendor_id')
+                               );
+                       }
+                       return $pricebook;
+               }
+
+               function read_single_activity($id)
+               {
+                       $sql = "SELECT * FROM fm_activities where id='$id'";
+
+                       $this->db->query($sql,__LINE__,__FILE__);
+
+                       if ($this->db->next_record())
+                       {
+                               $activity['activity_id']= $id;
+                               $activity['num']                = 
$this->db->f('num');
+                               $activity['unit']               = 
$this->db->f('unit');
+                               $activity['cat_id']             = 
$this->db->f('agreement_group_id');
+                               $activity['ns3420_id']  = 
$this->db->f('ns3420');
+                               $activity['descr']              = 
stripslashes($this->db->f('descr'));
+                               $activity['base_descr'] = 
stripslashes($this->db->f('base_descr'));
+                               $activity['dim_d']              = 
$this->db->f('dim_d');
+                               $activity['branch_id']  = 
$this->db->f('branch_id');
+
+                               return $activity;
+                       }
+               }
+
+               function read_single_agreement_group($id)
+               {
+                       $sql = "SELECT * FROM fm_agreement_group where 
id='$id'";
+
+                       $this->db->query($sql,__LINE__,__FILE__);
+
+                       if ($this->db->next_record())
+                       {
+                               $agreement_group['agreement_group_id']  = $id;
+                               $agreement_group['num']                 = 
$this->db->f('num');
+                               $agreement_group['status']              = 
$this->db->f('status');
+                               $agreement_group['descr']                       
= stripslashes($this->db->f('descr'));
+
+                               return $agreement_group;
+                       }
+               }
+
+               function add_activity($values)
+               {
+                       $values['descr'] = 
$this->db->db_addslashes($values['descr']);
+                       $values['base_descr'] = 
$this->db->db_addslashes($values['base_descr']);
+
+                       $vals= array(
+                               $values['activity_id'],
+                               $values['num'] ,
+                               $values['unit'] ,
+                               $values['cat_id'] , // agreement_group
+                               $values['ns3420_id'] ,
+                               $values['dim_d'],
+                               $values['branch_id'],
+                               $values['descr'],
+                               $values['base_descr']
+                               );
+
+                       $vals   = $this->bocommon->validate_db_insert($vals);
+
+                       $this->db->transaction_begin();
+                       $this->db->query("INSERT INTO fm_activities (id, 
num,unit,agreement_group_id,ns3420,dim_d,branch_id,descr,base_descr) "
+                               . "VALUES ($vals)",__LINE__,__FILE__);
+                       $this->db->transaction_commit();
+
+                       $receipt['message'][] = array('msg'=>lang('Activity has 
been saved'));
+                       $receipt['activity_id']= $values['activity_id'];
+                       return $receipt;
+               }
+
+               function edit_activity($values)
+               {
+                       $values['descr'] = 
$this->db->db_addslashes($values['descr']);
+                       $values['base_descr'] = 
$this->db->db_addslashes($values['base_descr']);
+
+                       $value_set=array(
+                               'num'                                   => 
$values['num'],
+                               'unit'                                  => 
$values['unit'],
+                               'agreement_group_id'    => $values['cat_id'],
+                               'ns3420'                                => 
$values['ns3420_id'],
+                               'dim_d'                                 => 
$values['dim_d'],
+                               'branch_id'                             => 
$values['branch_id'],
+                               'descr'                                 => 
$values['descr'],
+                               'base_descr'                    => 
$values['base_descr']
+                               );
+
+                       $value_set      = 
$this->bocommon->validate_db_update($value_set);
+                       $this->db->transaction_begin();
+                       $this->db->query("UPDATE fm_activities set $value_set 
WHERE id= '" . $values['activity_id'] . "'",__LINE__,__FILE__);
+                       $this->db->transaction_commit();
+
+                       $receipt['activity_id']= $values['activity_id'];
+                       $receipt['message'][] = array('msg'=>lang('Activity has 
been edited'));
+                       return $receipt;
+               }
+
+               function add_agreement_group($values)
+               {
+                       $values['descr'] = 
$this->db->db_addslashes($values['descr']);
+
+                       $vals= array(
+                               $values['agreement_group_id'],
+                               $values['num'],
+                               $values['status'],
+                               $values['descr']
+                               );
+
+                       $vals   = $this->bocommon->validate_db_insert($vals);
+
+                       $this->db->query("INSERT INTO fm_agreement_group 
(id,num,status,descr) "
+                               . "VALUES ($vals)",__LINE__,__FILE__);
+
+                       $receipt['message'][] = array('msg'=>lang('Agreement 
group has been saved'));
+                       $receipt['agreement_group_id'] = 
$values['agreement_group_id'];
+                       return $receipt;
+               }
+
+               function edit_agreement_group($values)
+               {
+                       $values['descr'] = 
$this->db->db_addslashes($values['descr']);
+
+                       $value_set=array(
+                               'num'   => $values['num'],
+                               'status'=> $values['status'],
+                               'descr' => $values['descr']
+                               );
+
+                       $value_set      = 
$this->bocommon->validate_db_update($value_set);
+                       $this->db->transaction_begin();
+
+                       $this->db->query("UPDATE fm_agreement_group set 
$value_set WHERE id= '" . $values['agreement_group_id'] . 
"'",__LINE__,__FILE__);
+
+                       $this->db->transaction_commit();
+
+                       $receipt['message'][] = 
array('msg'=>lang('Agreement_group has been edited'));
+                       return $receipt;
+               }
+
+               function delete_activity_vendor($activity_id,$vendor_id)
+               {
+                       $this->db->query("DELETE FROM fm_activity_price_index 
WHERE activity_id='$activity_id' and vendor_id='$vendor_id'",__LINE__,__FILE__);
+               }
+
+               function delete_activity($activity_id)
+               {
+                       $this->db->query("DELETE FROM fm_activities WHERE 
id='$activity_id'",__LINE__,__FILE__);
+                       $this->db->query("DELETE FROM fm_activity_price_index 
WHERE activity_id='$activity_id'",__LINE__,__FILE__);
+               }
+
+               function 
delete_prize_index($activity_id,$vendor_id,$index_count)
+               {
+                       if ($index_count==1)
+                       {
+                               $this->db->query("update 
fm_activity_price_index set index_count = '1', current_index = '0', 
this_index=Null, m_cost=Null,w_cost=Null,total_cost=Null,index_date=Null  where 
activity_id='$activity_id' and vendor_id= '$vendor_id' and index_count= 
'1'",__LINE__,__FILE__);
+                       }
+                       else
+                       {
+                               $this->db->query("delete from 
fm_activity_price_index where activity_id='$activity_id' and vendor_id= 
'$vendor_id' and index_count= '$index_count'",__LINE__,__FILE__);
+
+                               $new_index_count= $index_count -1;
+
+                               $this->db->query("update 
fm_activity_price_index set current_index = '1' where 
activity_id='$activity_id' and vendor_id= '$vendor_id' and index_count= 
'$new_index_count'",__LINE__,__FILE__);
+                       }
+
+               }
+
+               function delete_agreement_group($agreement_group_id)
+               {
+                       $this->db->query("DELETE FROM fm_agreement_group WHERE 
id='$agreement_group_id'",__LINE__,__FILE__);
+//how to handle the activities and vendors ...?
+               }
+
+
+               function add_activity_vendor($values)
+               {
+                       $this->db->query("SELECT count(*) FROM 
fm_activity_price_index WHERE activity_id='" . $values['activity_id'] . "' and 
vendor_id='" . $values['vendor_id'] . "'",__LINE__,__FILE__);
+
+                       $this->db->next_record();
+
+                       if ( $this->db->f(0))
+                       {
+                               $receipt['error'][] = array('msg'=>lang('This 
Vendor is already registered for this activity'));
+                       }
+                       else
+                       {
+                               $this->db->query("insert into 
fm_activity_price_index (activity_id, vendor_id, 
index_count,current_index,m_cost,w_cost,total_cost) "
+                                       . " values ('" .
+                                       $values['activity_id']. "','" .
+                                       $values['vendor_id']. 
"','1','0',NULL,NULL,NULL)",__LINE__,__FILE__);
+
+                               $receipt['message'][] = 
array('msg'=>lang('Vendor has been added'));
+
+                       }
+                       return $receipt;
+               }
+       }
+?>
Index: property/inc/class.soproject.inc.php
diff -u property/inc/class.soproject.inc.php:1.25 
property/inc/class.soproject.inc.php:1.26
--- property/inc/class.soproject.inc.php:1.25   Fri Jan 27 14:05:43 2006
+++ property/inc/class.soproject.inc.php        Mon Jan 30 22:14:19 2006
@@ -1,930 +1,930 @@
-<?php
-       /**
-       * phpGroupWare - property: a Facilities Management System.
-       *
-       * @author Sigurd Nes <address@hidden>
-       * @copyright Copyright (C) 2003-2005 Free Software Foundation, Inc. 
http://www.fsf.org/
-       * @license http://www.gnu.org/licenses/gpl.html GNU General Public 
License
-       * @internal Development of this application was funded by 
http://www.bergen.kommune.no/bbb_/ekstern/
-       * @package property
-       * @subpackage project
-       * @version $Id: class.soproject.inc.php,v 1.25 2006/01/27 14:05:43 
sigurdne Exp $
-       */
-
-       /**
-        * Description
-        * @package property
-        */
-
-       class soproject
-       {
-
-               function soproject()
-               {
-                       $this->currentapp       = 
$GLOBALS['phpgw_info']['flags']['currentapp'];
-                       $this->account          = 
$GLOBALS['phpgw_info']['user']['account_id'];
-                       $this->bocommon         = 
CreateObject($this->currentapp.'.bocommon');
+<?php
+       /**
+       * phpGroupWare - property: a Facilities Management System.
+       *
+       * @author Sigurd Nes <address@hidden>
+       * @copyright Copyright (C) 2003-2005 Free Software Foundation, Inc. 
http://www.fsf.org/
+       * @license http://www.gnu.org/licenses/gpl.html GNU General Public 
License
+       * @internal Development of this application was funded by 
http://www.bergen.kommune.no/bbb_/ekstern/
+       * @package property
+       * @subpackage project
+       * @version $Id: class.soproject.inc.php,v 1.26 2006/01/30 22:14:19 
sigurdne Exp $
+       */
+
+       /**
+        * Description
+        * @package property
+        */
+
+       class soproject
+       {
+
+               function soproject()
+               {
+                       $this->currentapp       = 
$GLOBALS['phpgw_info']['flags']['currentapp'];
+                       $this->account          = 
$GLOBALS['phpgw_info']['user']['account_id'];
+                       $this->bocommon         = 
CreateObject($this->currentapp.'.bocommon');
                        $this->db               = $this->bocommon->new_db();
-                       $this->db2              = $this->bocommon->new_db();
-                       $this->join                     = $this->bocommon->join;
-                       $this->left_join        = $this->bocommon->left_join;
-                       $this->like                     = $this->bocommon->like;
-
-                       $this->grants   = 
$GLOBALS['phpgw']->session->appsession('grants_project',$this->currentapp);
-
-                       if(!$this->grants)
-                       {
-                               $this->acl2             = 
CreateObject($this->currentapp.'.acl2','##DEFAULT##','##DEFAULT##');
-                               $this->grants           = 
$this->acl2->get_grants($this->currentapp,'.project');
-                               
$GLOBALS['phpgw']->session->appsession('grants_project',$this->currentapp,$this->grants);
-                       }
-               }
-
-
-               function select_category_workorder_list()
-               {
-
-                       $this->db->query("SELECT id, descr FROM 
fm_workorder_category  ORDER BY descr ");
-
-                       $i = 0;
-                       while ($this->db->next_record())
-                       {
-                               $categories[$i]['id']                           
= $this->db->f('id');
-                               $categories[$i]['name']                         
= stripslashes($this->db->f('descr'));
-                               $i++;
-                       }
-                       return $categories;
-               }
-
-               function read_single_project_category($id='')
-               {
-                       $this->db->query("SELECT descr FROM 
fm_workorder_category where id='$id' ");
-                       $this->db->next_record();
-                       return $this->db->f('descr');
-               }
-
-               function select_status_list()
-               {
-                       $this->db->query("SELECT id, descr FROM 
fm_workorder_status ORDER BY id ");
-
-                       $i = 0;
-                       while ($this->db->next_record())
-                       {
-                               $status_entries[$i]['id']                       
        = $this->db->f('id');
-                               $status_entries[$i]['name']                     
        = stripslashes($this->db->f('descr'));
-                               $i++;
-                       }
-                       return $status_entries;
-               }
-
-               function select_branch_list()
-               {
-                       $this->db->query("SELECT id, descr FROM fm_branch ORDER 
BY id ");
-
-                       $i = 0;
-                       while ($this->db->next_record())
-                       {
-                               $branch_entries[$i]['id']                       
        = $this->db->f('id');
-                               $branch_entries[$i]['name']                     
        = stripslashes($this->db->f('descr'));
-                               $i++;
-                       }
-                       return $branch_entries;
-               }
-
-               function select_key_location_list()
-               {
-                       $this->db->query("SELECT id, descr FROM fm_key_loc 
ORDER BY descr ");
-
-                       $i = 0;
-                       while ($this->db->next_record())
-                       {
-                               $key_location_entries[$i]['id']                 
        = $this->db->f('id');
-                               $key_location_entries[$i]['name']               
        = stripslashes($this->db->f('descr'));
-                               $i++;
-                       }
-                       return $key_location_entries;
-               }
-
-               function read($data)
-               {
-                       if(is_array($data))
-                       {
-                               if ($data['start'])
-                               {
-                                       $start=$data['start'];
-                               }
-                               else
-                               {
-                                       $start=0;
-                               }
-                               $filter = 
$data['filter']?$data['filter']:$this->account;
-                               $query = 
(isset($data['query'])?$data['query']:'');
-                               $sort = 
(isset($data['sort'])?$data['sort']:'DESC');
-                               $order = 
(isset($data['order'])?$data['order']:'');
-                               $cat_id = 
(isset($data['cat_id'])?$data['cat_id']:0);
-                               $status_id = 
(isset($data['status_id'])?$data['status_id']:'');
-                               $start_date = 
(isset($data['start_date'])?$data['start_date']:'');
-                               $end_date = 
(isset($data['end_date'])?$data['end_date']:'');
-                               $allrows = 
(isset($data['allrows'])?$data['allrows']:'');
-                               $wo_hour_cat_id = 
(isset($data['wo_hour_cat_id'])?$data['wo_hour_cat_id']:'');
-                       }
-
-                       $sql = $this->bocommon->fm_cache('sql_project_' . 
!!$wo_hour_cat_id);
-
-                       if(!$sql)
-                       {
-                               $entity_table = 'fm_project';
-
-                               $cols .= $entity_table . '.location_code';
-                               $cols_return[] = 'location_code';
-
-                               $cols .= ",$entity_table.id as project_id";
-                               $cols_return[]                          = 
'project_id';
-                               $uicols['input_type'][]         = 'text';
-                               $uicols['name'][]                       = 
'project_id';
-                               $uicols['descr'][]                      = 
lang('Project');
-                               $uicols['statustext'][]         = lang('Project 
ID');
-
-                               $cols.= ",$entity_table.start_date";
-                               $cols_return[]                          = 
'start_date';
-                               $uicols['input_type'][]         = 'text';
-                               $uicols['name'][]                       = 
'start_date';
-                               $uicols['descr'][]                      = 
lang('start date');
-                               $uicols['statustext'][]         = lang('Project 
start date');
-
-                               $cols.= ",$entity_table.name as name";
-                               $cols_return[]                          = 
'name';
-                               $uicols['input_type'][]         = 'text';
-                               $uicols['name'][]                       = 
'name';
-                               $uicols['descr'][]                      = 
lang('name');
-                               $uicols['statustext'][]         = lang('Project 
name');
-
-                               $cols.= ",account_lid as coordinator";
-                               $cols_return[]                          = 
'coordinator';
-                               $uicols['input_type'][]         = 'text';
-                               $uicols['name'][]                       = 
'coordinator';
-                               $uicols['descr'][]                      = 
lang('Coordinator');
-                               $uicols['statustext'][]         = lang('Project 
coordinator');
-
-                               $cols.= ",$entity_table.user_id";
-
-                               $joinmethod .= " $this->join  phpgw_accounts ON 
($entity_table.coordinator = phpgw_accounts.account_id))";
-                               $paranthesis .='(';
-
-                               //----- wo_hour_status
-
-                               if($wo_hour_cat_id)
-                               {
-                                       $joinmethod .= " $this->join 
fm_workorder ON ($entity_table.id = fm_workorder.project_id))";
-                                       $paranthesis .='(';
-
-                                       $joinmethod .= " $this->join 
fm_wo_hours ON (fm_workorder.id = fm_wo_hours.workorder_id))";
-                                       $paranthesis .='(';
-
-                                       $joinmethod .= " $this->join 
fm_wo_hours_category ON (fm_wo_hours.category = fm_wo_hours_category.id))";
-                                       $paranthesis .='(';
-                               }
-
-                               //----- wo_hour_status
-
-                               $sql    = 
$this->bocommon->generate_sql(array('entity_table'=>$entity_table,'cols'=>$cols,'cols_return'=>$cols_return,
-                                                                               
'uicols'=>$uicols,'joinmethod'=>$joinmethod,'paranthesis'=>$paranthesis,'query'=>$query));
-
-                               $this->bocommon->fm_cache('sql_project_' . 
!!$wo_hour_cat_id,$sql);
-
-                               $this->uicols           = 
$this->bocommon->uicols;
-                               $cols_return            = 
$this->bocommon->cols_return;
-                               $type_id                = 
$this->bocommon->type_id;
-                               $this->cols_extra       = 
$this->bocommon->cols_extra;
-
-                               $this->bocommon->fm_cache('uicols_project_' . 
!!$wo_hour_cat_id,$this->uicols);
-                               
$this->bocommon->fm_cache('cols_return_project_' . 
!!$wo_hour_cat_id,$cols_return);
-                               $this->bocommon->fm_cache('type_id_project_' . 
!!$wo_hour_cat_id,$type_id);
-                               $this->bocommon->fm_cache('cols_extra_project_' 
. !!$wo_hour_cat_id,$this->cols_extra);
-
-                       }
-                       else
-                       {
-                               $this->uicols           = 
$this->bocommon->fm_cache('uicols_project_' . !!$wo_hour_cat_id);
-                               $cols_return            = 
$this->bocommon->fm_cache('cols_return_project_' . !!$wo_hour_cat_id);
-                               $type_id                = 
$this->bocommon->fm_cache('type_id_project_' . !!$wo_hour_cat_id);
-                               $this->cols_extra       = 
$this->bocommon->fm_cache('cols_extra_project_' . !!$wo_hour_cat_id);
-                       }
-
-                       if ($order)
-                       {
-                               $ordermethod = " order by $order $sort";
-                       }
-                       else
-                       {
-                               $ordermethod = ' order by fm_project.id DESC';
-                       }
-
-                       $where= 'WHERE';
-
-                       if ($cat_id > 0)
-                       {
-                               $filtermethod .= " $where 
fm_project.category=$cat_id ";
-                               $where= 'AND';
-                       }
-
-                       if ($status_id)
-                       {
-                               $filtermethod .= " $where 
fm_project.status='$status_id' ";
-                               $where= 'AND';
-                       }
-
-                       if($wo_hour_cat_id)
-                       {
-                               $filtermethod .= " $where 
fm_wo_hours_category.id=$wo_hour_cat_id ";
-                               $where= 'AND';
-                               $group_method = " group by 
fm_project.location_code,fm_project.id 
,fm_project.start_date,fm_project.name,account_lid 
,fm_project.user_id,fm_project.address";
-                       }
-
-                       if ($filter=='all')
-                       {
-                               if (is_array($this->grants))
-                               {
-                                       $grants = $this->grants;
-                                       while (list($user) = each($grants))
-                                       {
-                                               $public_user_list[] = $user;
-                                       }
-                                       reset($public_user_list);
-                                       $filtermethod .= " $where 
(fm_project.coordinator IN(" . implode(',',$public_user_list) . "))";
-
-                                       $where= 'AND';
-                               }
-                       }
-                       else
-                       {
-                               $filtermethod .= " $where 
fm_project.coordinator=$filter ";
-                               $where= 'AND';
-                       }
-
-                       if ($start_date)
-                       {
-                               $filtermethod .= " $where fm_project.start_date 
>= $start_date AND fm_project.start_date <= $end_date ";
-                               $where= 'AND';
-                       }
-
-
-                       if($query)
-                       {
-                               $query = str_replace(",",'.',$query);
-                               if(stristr($query, '.'))
-                               {
-                                       $query=explode(".",$query);
-                                       $querymethod = " $where 
(fm_project.loc1='" . $query[0] . "' AND fm_project.loc".$type_id."='" . 
$query[1] . "')";
-                               }
-                               else
-                               {
-                                       $query = ereg_replace("'",'',$query);
-                                       $query = ereg_replace('"','',$query);
-                                       $querymethod = " $where 
(fm_project.name $this->like '%$query%' or fm_project.address $this->like 
'%$query%' or fm_project.location_code $this->like '%$query%' or fm_project.id 
$this->like '%$query%')";
-                               }
-                       }
-
-                       $sql .= " $filtermethod $querymethod $group_method";
-//echo $sql;
-                       $this->db2->query($sql,__LINE__,__FILE__);
-                       $this->total_records = $this->db2->num_rows();
-                       if(!$allrows)
-                       {
-                               $this->db->limit_query($sql . 
$ordermethod,$start,__LINE__,__FILE__);
-                       }
-                       else
-                       {
-                               $this->db->query($sql . 
$ordermethod,__LINE__,__FILE__);
-                       }
-
-                       $j=0;
-                       $k=count($cols_return);
-                       while ($this->db->next_record())
-                       {
-                               for ($i=0;$i<$k;$i++)
-                               {
-                                       $project_list[$j][$cols_return[$i]] = 
stripslashes($this->db->f($cols_return[$i]));
-                                       $project_list[$j]['grants'] = 
(int)$this->grants[$this->db->f('user_id')];
-                               }
-
-                               $location_code= $this->db->f('location_code');
-                               $location = split('-',$location_code);
-                               $n=count($location);
-                               for ($m=0;$m<$n;$m++)
-                               {
-                                       $project_list[$j]['loc' . ($m+1)] = 
$location[$m];
-                                       
$project_list[$j]['query_location']['loc' . ($m+1)]=implode("-", 
array_slice($location, 0, ($m+1)));
-                               }
-
-                               $j++;
-                       }
-
-//_debug_array($project_list);
-                       return $project_list;
-               }
-
-               function read_single($project_id)
-               {
-                       $config = CreateObject('phpgwapi.config','property');
-                       $config->read_repository();
-                       $meter_table = $config->config_data['meter_table'];
-
-                       $sql = "SELECT * from fm_project where 
id='$project_id'";
-
-                       $this->db->query($sql,__LINE__,__FILE__);
-
-                       if ($this->db->next_record())
-                       {
-                               $project['project_id']                  = 
$this->db->f('id');
-                               $project['title']                       = 
$this->db->f('title');
-                               $project['name']                        = 
$this->db->f('name');
-                               $project['location_code']               = 
$this->db->f('location_code');
-                               $project['key_fetch']                   = 
$this->db->f('key_fetch');
-                               $project['key_deliver']                 = 
$this->db->f('key_deliver');
-                               $project['other_branch']                = 
$this->db->f('other_branch');
-                               $project['key_responsible']             = 
$this->db->f('key_responsible');
-                               $project['descr']                       = 
$this->db->f('descr');
-                               $project['status']                      = 
$this->db->f('status');
-                               $project['budget']                      = 
(int)$this->db->f('budget');
-                               $project['reserve']                     = 
(int)$this->db->f('reserve');
-                               $project['tenant_id']                   = 
$this->db->f('tenant_id');
-                               $project['user_id']                     = 
$this->db->f('user_id');
-                               $project['coordinator']                 = 
$this->db->f('coordinator');
-                               $project['access']                      = 
$this->db->f('access');
-                               $project['start_date']                  = 
$this->db->f('start_date');
-                               $project['end_date']                    = 
$this->db->f('end_date');
-                               $project['cat_id']                      = 
$this->db->f('category');
-                               $project['grants']                      = 
(int)$this->grants[$this->db->f('user_id')];
-                               $project['p_num']                       = 
$this->db->f('p_num');
-                               $project['p_entity_id']                 = 
$this->db->f('p_entity_id');
-                               $project['p_cat_id']                    = 
$this->db->f('p_cat_id');
-                               $project['contact_phone']               = 
$this->db->f('contact_phone');
-
-
-                               if($meter_table)
-                               {
-                                       $project['power_meter'] = 
$this->get_power_meter($this->db->f('location_code'),$meter_table);
-                               }
-                       }
-
-                       $sql = "SELECT * FROM fm_origin WHERE destination = 
'project' AND destination_id='$project_id' ORDER by origin DESC  ";
-
-                       $this->db->query($sql,__LINE__,__FILE__);
-
-                       $i=-1;
-                       while ($this->db->next_record())
-                       {
-                               if($last_type != $this->db->f('origin'))
-                               {
-                                       $i++;
-                               }
-                               $project['origin'][$i]['type'] = 
$this->db->f('origin');
-                               $project['origin'][$i]['link'] = 
$this->bocommon->get_origin_link($this->db->f('origin'));
-                               $project['origin'][$i]['data'][]= array(
-                                       'id'=> $this->db->f('origin_id'),
-                                       'type'=> $this->db->f('origin')
-                                       );
-
-                               $last_type=$this->db->f('origin');
-                       }
-
-//_debug_array($project);
-                               return $project;
-               }
-
-               function get_ticket($project_id = '')
-               {
-                       $sql = "SELECT * FROM fm_origin WHERE origin ='tts' AND 
destination = 'project' AND destination_id='$project_id' ORDER by origin DESC  
";
-                       $this->db->query($sql,__LINE__,__FILE__);
-                       $this->db->next_record();
-                       return $this->db->f('origin_id');
-               }
-
-               function get_power_meter($location_code = '',$meter_table='')
-               {
-                       if(!$meter_table)
-                       {
-                               $config = 
CreateObject('phpgwapi.config','property');
-                               $config->read_repository();
-                               $meter_table = 
$config->config_data['meter_table'];
-                       }
-
-                       $this->db2->query("SELECT ext_meter_id as power_meter 
FROM $meter_table where location_code='$location_code' and 
category='1'",__LINE__,__FILE__);
-
-                       $this->db2->next_record();
-
-                       return $this->db2->f('power_meter');
-               }
-
-               function project_workorder_data($project_id = '')
-               {
-                       $this->db->query("SELECT act_mtrl_cost, 
act_vendor_cost, budget, id as workorder_id, vendor_id, 
calculation,rig_addition,addition,deviation,charge_tenant from fm_workorder 
where project_id='$project_id'");
-                       while ($this->db->next_record())
-                       {
-                               $budget[] = array(
-                                       'workorder_id'  => 
$this->db->f('workorder_id'),
-                                       'budget'                => 
$this->db->f('budget'),
-                                       'deviation'             => 
$this->db->f('deviation'),
-                                       'calculation'   => 
($this->db->f('calculation')*(1+$this->db->f('addition')/100))+$this->db->f('rig_addition'),
-                                       'vendor_id'             => 
$this->db->f('vendor_id'),
-                                       'act_mtrl_cost'         => 
$this->db->f('act_mtrl_cost'),
-                                       'act_vendor_cost'       => 
$this->db->f('act_vendor_cost'),
-                                       'charge_tenant'         => 
$this->db->f('charge_tenant')
-                                       );
-                       }
-                       return $budget;
-               }
-
-               function branch_p_list($project_id = '')
-               {
-
-                       $this->db2->query("SELECT branch_id from 
fm_projectbranch WHERE project_id=" .  (int)$project_id ,__LINE__,__FILE__);
-                       while ($this->db2->next_record())
-                       {
-                               $selected[] = array('branch_id' => 
$this->db2->f('branch_id'));
-                       }
-
-                       return $selected;
-               }
-
-               function increment_project_id()
-               {
-                       $this->db->query("update fm_idgenerator set value = 
value + 1 where name = 'project'");
-               }
-
-               function next_project_id()
-               {
-                       $this->db->query("select value from fm_idgenerator 
where name = 'project'");
-                       $this->db->next_record();
-                       $project_id = $this->db->f('value')+1;
-                       return $project_id;
-               }
-
-               function add($project)
-               {
-                       $historylog     = 
CreateObject($this->currentapp.'.historylog','project');
-
-                       while (is_array($project['location']) && 
list($input_name,$value) = each($project['location']))
-                       {
-                               if($value)
-                               {
-                                       $cols[] = $input_name;
-                                       $vals[] = $value;
-                               }
-                       }
-
-                       while (is_array($project['extra']) && 
list($input_name,$value) = each($project['extra']))
-                       {
-                               if($value)
-                               {
-                                       $cols[] = $input_name;
-                                       $vals[] = $value;
-                               }
-                       }
-
-                       if($cols)
-                       {
-                               $cols   = "," . implode(",", $cols);
-                               $vals   = ",'" . implode("','", $vals) . "'";
-                       }
-
-                       if($project['street_name'])
-                       {
-                               $address[]= $project['street_name'];
-                               $address[]= $project['street_number'];
-                               $address = $this->db->db_addslashes(implode(" 
", $address));
-                       }
-
-                       if(!$address)
-                       {
-                               $address = 
$this->db->db_addslashes($project['location_name']);
-                       }
-
-                       $project['descr'] = 
$this->db->db_addslashes($project['descr']);
-                       $project['name'] = 
$this->db->db_addslashes($project['name']);
-
-                       $values= array(
-                               $project['project_id'],
-                               $project['name'],
-                               'public',
-                               $project['cat_id'],
-                               time(),
-                               $project['start_date'],
-                               $project['end_date'],
-                               $project['coordinator'],
-                               $project['status'],
-                               $project['descr'],
-                               $project['budget'],
-                               $project['reserve'],
-                               $project['location_code'],
-                               $address,
-                               $project['key_deliver'],
-                               $project['key_fetch'],
-                               $project['other_branch'],
-                               $project['key_responsible'],
-                               $this->account);
-
-                       $values = $this->bocommon->validate_db_insert($values);
-
-                       $this->db->transaction_begin();
-
-                       $this->db->query("INSERT INTO fm_project 
(id,name,access,category,entry_date,start_date,end_date,coordinator,status,"
-                               . 
"descr,budget,reserve,location_code,address,key_deliver,key_fetch,other_branch,key_responsible,user_id
 $cols) "
-                               . "VALUES ($values $vals )",__LINE__,__FILE__);
-
-                       if($project['extra']['contact_phone'] && 
$project['extra']['tenant_id'])
-                       {
-                               $this->db->query("update fm_tenant set 
contact_phone='". $project['extra']['contact_phone']. "' where id='". 
$project['extra']['tenant_id']. "'",__LINE__,__FILE__);
-                       }
-
-                       $config = CreateObject('phpgwapi.config','property');
-                       $config->read_repository();
-                       $meter_table = $config->config_data['meter_table'];
-
-                       if ($project['power_meter'] && $meter_table)
-                       {
-                               
$this->update_power_meter($project['power_meter'],$project['location_code'],$address,$meter_table);
-                       }
-
-                       if (count($project['branch']) != 0)
-                       {
-                               while($branch=each($project['branch']))
-                               {
-                                       $this->db->query("insert into 
fm_projectbranch (project_id,branch_id) values ('" . $project['project_id']. 
"','$branch[1]')",__LINE__,__FILE__);
-                               }
-                       }
-
-                       if($project['origin'][0]['data'][0]['id'])
-                       {
-                               $this->db->query("INSERT INTO  fm_origin 
(origin,origin_id,destination,destination_id,user_id,entry_date) "
-                                       . "VALUES ('"
-                                       . $project['origin'][0]['type']. "','"
-                                       . 
$project['origin'][0]['data'][0]['id']. "',"
-                                       . "'project',"
-                                       . $project['project_id']. ","
-                                       . $this->account . ","
-                                       . time() . ")",__LINE__,__FILE__);
-                       }
-
-
-                       if($this->db->transaction_commit())
-                       {
-                               $this->increment_project_id();
-                               
$historylog->add('SO',$project['project_id'],$project['status']);
-                               
$historylog->add('TO',$project['project_id'],$project['cat_id']);
-                               
$historylog->add('CO',$project['project_id'],$project['coordinator']);
-
-                               $receipt['message'][] = 
array('msg'=>lang('project %1 has been saved',$project['project_id']));
-                       }
-                       else
-                       {
-                               $receipt['error'][] = array('msg'=>lang('the 
project has not been saved'));
-                       }
-                       return $receipt;
-               }
-
-               function 
update_power_meter($power_meter,$location_code,$address,$meter_table)
-               {
-
-                       $location=explode('-',$location_code);
-
-                       $i=1;
-                       if (isset($location) AND is_array($location))
-                       {
-                               foreach($location as $location_entry)
-                               {
-
-                                       $cols[] = 'loc' . $i;
-                                       $vals[] = $location_entry;
-
-                                       $i++;
-                               }
-
-                       }
-
-                       if($cols)
-                       {
-                               $cols   = "," . implode(",", $cols);
-                               $vals   = ",'" . implode("','", $vals) . "'";
-                       }
-
-
-                       $this->db->query("SELECT count(*) FROM $meter_table 
where location_code='$location_code' and category=1",__LINE__,__FILE__);
-
-                       $this->db->next_record();
-
-                       if ( $this->db->f(0))
-                       {
-                               $this->db->query("update $meter_table set 
ext_meter_id='$power_meter',address='$address' where 
location_code='$location_code' and category='1'",__LINE__,__FILE__);
-                       }
-                       else
-                       {
-                               $id = $this->bocommon->next_id($meter_table);
-
-                               $meter_id       = 
$this->generate_meter_id($meter_table);
-                               $this->db->query("insert into $meter_table 
(id,num,ext_meter_id,category,location_code,entry_date,user_id,address $cols) "
-                                       . "VALUES ('"
-                                       . $id. "','"
-                                       . $meter_id. "','"
-                                       . $power_meter. "',"
-                                       . "1,'"
-                                       . $location_code. "',"
-                                       . time() . ",$this->account, '$address' 
$vals)",__LINE__,__FILE__);
-                       }
-               }
-
-               function generate_meter_id($meter_table)
-               {
-                       $prefix = 'meter';
-                       $pos    = strlen($prefix);
-                       $this->db->query("select max(num) from $meter_table 
where num $this->like ('$prefix%')");
-                       $this->db->next_record();
-
-                       $max = 
$this->bocommon->add_leading_zero(substr($this->db->f(0),$pos));
-
-                       $meter_id= $prefix . $max;
-                       return $meter_id;
-               }
-
-               function edit($project)
-               {
-                       $historylog     = 
CreateObject($this->currentapp.'.historylog','project');
-
-                       while (is_array($project['location']) && 
list($input_name,$value) = each($project['location']))
-                       {
-                               $vals[] = "$input_name = '$value'";
-                       }
-
-                       while (is_array($project['extra']) && 
list($input_name,$value) = each($project['extra']))
-                       {
-                               $vals[] = "$input_name = '$value'";
-                       }
-
-                       if($vals)
-                       {
-                               $vals   = "," . implode(",",$vals);
-                       }
-
-                       if($project['street_name'])
-                       {
-                               $address[]= $project['street_name'];
-                               $address[]= $project['street_number'];
-                               $address = $this->db->db_addslashes(implode(" 
", $address));
-                       }
-
-                       if(!$address)
-                       {
-                               $address = 
$this->db->db_addslashes($project['location_name']);
-                       }
-
-                       $project['descr'] = 
$this->db->db_addslashes($project['descr']);
-                       $project['name'] = 
$this->db->db_addslashes($project['name']);
-
-                       $value_set=array(
-                               'name'                  => $project['name'],
-                               'status'                => $project['status'],
-                               'category'              => $project['cat_id'],
-                               'start_date'            => 
$project['start_date'],
-                               'end_date'              => $project['end_date'],
-                               'coordinator'           => 
$project['coordinator'],
-                               'descr'                 => $project['descr'],
-                               'budget'                => 
(int)$project['budget'],
-                               'reserve'               => 
(int)$project['reserve'],
-                               'key_deliver'           => 
$project['key_deliver'],
-                               'key_fetch'             => 
$project['key_fetch'],
-                               'other_branch'          => 
$project['other_branch'],
-                               'key_responsible'       => 
$project['key_responsible'],
-                               'location_code'         => 
$project['location_code'],
-                               'address'               => $address
-                               );
-
-                       $value_set      = 
$this->bocommon->validate_db_update($value_set);
-
-                       $this->db->transaction_begin();
-
-                       $this->db->query("SELECT status,category,coordinator 
FROM fm_project where id='" .$project['project_id']."'",__LINE__,__FILE__);
-                       $this->db->next_record();
-                       $old_status = $this->db->f('status');
-                       $old_categroy = (int)$this->db->f('category');
-                       $old_coordinator = (int)$this->db->f('coordinator');
-
-                       $this->db->query("UPDATE fm_project set $value_set 
$vals WHERE id= '" . $project['project_id'] ."'",__LINE__,__FILE__);
-
-                       if($project['extra']['contact_phone'] && 
$project['extra']['tenant_id'])
-                       {
-                               $this->db->query("update fm_tenant set 
contact_phone='". $project['extra']['contact_phone']. "' where id='". 
$project['extra']['tenant_id']. "'",__LINE__,__FILE__);
-                       }
-
-                       $config = CreateObject('phpgwapi.config','property');
-                       $config->read_repository();
-                       $meter_table = $config->config_data['meter_table'];
-
-                       if ($project['power_meter'] && $meter_table)
-                       {
-                               
$this->update_power_meter($project['power_meter'],$project['location_code'],$address,$meter_table);
-                       }
-       // -----------------which branch is represented
-                       $this->db->query("delete from fm_projectbranch where 
project_id='" . $project['project_id'] ."'",__LINE__,__FILE__);
-
-                       if (count($project['branch']) != 0)
-                       {
-                               while($branch=each($project['branch']))
-                               {
-                                       $this->db->query("insert into 
fm_projectbranch (project_id,branch_id) values ('" . $project['project_id']. 
"','$branch[1]')",__LINE__,__FILE__);
-                               }
-                       }
-
-                       if($project['delete_request'])
-                       {
-                               $receipt = 
$this->delete_request_from_project($project['delete_request'],$project['project_id']);
-
-                       }
-
-                       
$this->update_request_status($project['project_id'],$project['status'],$project['cat_id'],$project['coordinator']);
-
-                       if ($old_status != $project['status'])
-                       {
-                               
$historylog->add('S',$project['project_id'],$project['status']);
-
-                               $this->db->query("UPDATE fm_workorder set 
status='". $project['status'] . "' WHERE project_id= '" . 
$project['project_id'] ."'",__LINE__,__FILE__);
-
-                               $this->db2->query("SELECT id from fm_workorder 
WHERE project_id=" .  (int)$project['project_id'] ,__LINE__,__FILE__);
-                               while ($this->db2->next_record())
-                               {
-                                       $workorder[] = $this->db2->f('id');
-                               }
-
-                               if (isset($workorder) AND is_array($workorder))
-                               {
-                                       $historylog_workorder   = 
CreateObject($this->currentapp.'.historylog','workorder');
-                                       foreach($workorder as $workorder_id)
-                                       {
-                                               
$historylog_workorder->add('S',$workorder_id,$project['status']);
-                                       }
-                               }
-
-                               $receipt['notice_owner'][]=lang('Status 
changed') . ': ' . $project['status'];
-                       }
-                       if ($old_categroy != $project['cat_id'])
-                       {
-                               
$historylog->add('T',$project['project_id'],$project['cat_id']);
-                       }
-                       if ($old_coordinator != $project['coordinator'])
-                       {
-                               
$historylog->add('C',$project['project_id'],$project['coordinator']);
-                               $receipt['notice_owner'][]=lang('Coordinator 
changed') . ': ' . 
$GLOBALS['phpgw']->accounts->id2name($project['coordinator']);
-                       }
-
-                       $receipt['message'][] = array('msg'=>lang('project %1 
has been edited',$project['project_id']));
-
-                       $this->db->transaction_commit();
-
-                       return $receipt;
-
-               }
-
-
-               function delete_request_from_project($request_id,$project_id)
-               {
-                       for ($i=0;$i<count($request_id);$i++)
-                       {
-                               $this->db2->query("update fm_request set 
project_id = NULL where id='". $request_id[$i] . "'",__LINE__,__FILE__);
-                               $this->db->query("DELETE FROM fm_origin WHERE 
destination ='project' AND origin_id='" . $request_id[$i] . "' AND 
origin='request'",__LINE__,__FILE__);
-                               $receipt['message'][] = 
array('msg'=>lang('Request %1 has been deleted from project 
%2',$request_id[$i],$project_id));
-                       }
-                       return $receipt;
-               }
-
-
-               function 
update_request_status($project_id='',$status='',$category=0,$coordinator=0)
-               {
-                       $historylog_r   = 
CreateObject($this->currentapp.'.historylog','request');
-
-                       $sql = "SELECT origin_id FROM fm_origin WHERE 
destination ='project' AND destination_id='$project_id' and origin ='request'";
-//                     $sql = "SELECT origin_id FROM fm_project_origin WHERE 
project_id='$project_id' and origin ='request'";
-                       $this->db->query($sql,__LINE__,__FILE__);
-
-                       while ($this->db->next_record())
-                       {
-                               $request_id[]   = $this->db->f('origin_id');
-                       }
-
-                       for ($i=0;$i<count($request_id);$i++)
-                       {
-                               $this->db->query("SELECT 
status,category,coordinator FROM fm_request where id='" .$request_id[$i] 
."'",__LINE__,__FILE__);
-
-                               $this->db->next_record();
-
-                               $old_status = $this->db->f('status');
-                               $old_category = (int)$this->db->f('category');
-                               $old_coordinator = 
(int)$this->db->f('coordinator');
-
-                               if ($old_status != $status)
-                               {
-                                       
$historylog_r->add('S',$request_id[$i],$status);
-                               }
-
-                               if ((int)$old_category != (int)$category)
-                               {
-                                       
$historylog_r->add('T',$request_id[$i],$category);
-                               }
-
-                               if ((int)$old_coordinator != (int)$coordinator)
-                               {
-                                       
$historylog_r->add('C',$request_id[$i],$coordinator);
-                               }
-
-                               $this->db2->query("update fm_request set 
status='$status',coordinator='$coordinator' where id='". $request_id[$i] . 
"'",__LINE__,__FILE__);
-                       }
-               }
-
-
-               function check_request($request_id)
-               {
-                       $sql = "SELECT destination_id FROM fm_origin WHERE 
destination ='project' AND origin_id='$request_id' and origin ='request'";
-                       $this->db->query($sql,__LINE__,__FILE__);
-
-                       $this->db->next_record();
-
-                       if ( $this->db->f(0))
-                       {
-                               return $this->db->f('destination_id');
-                       }
-               }
-
-               function add_request($add_request,$id)
-               {
-
-                       for ($i=0;$i<count($add_request['request_id']);$i++)
-                       {
-                               
$project_id=$this->check_request($add_request['request_id'][$i]);
-
-                               if(!$project_id)
-                               {
-                                       $this->db->query("INSERT INTO  
fm_origin (origin,origin_id,destination,destination_id,user_id,entry_date) "
-                                               . "VALUES ('request','"
-                                               . 
$add_request['request_id'][$i]. "','"
-                                               . "project',"
-                                               . $id . ","
-                                               . $this->account . ","
-                                               . time() . 
")",__LINE__,__FILE__);
-
-                                       $this->db2->query("update fm_request 
set project_id='$id' where id='". $add_request['request_id'][$i] . 
"'",__LINE__,__FILE__);
-
-                                       $receipt['message'][] = 
array('msg'=>lang('request %1 has been added',$add_request['request_id'][$i]));
-                               }
-                               else
-                               {
-                                       $receipt['error'][] = 
array('msg'=>lang('request %1 has already been added to project 
%2',$add_request['request_id'][$i],$project_id));
-                               }
-
-                       }
-
-                       return $receipt;
-               }
-
-               function delete($project_id )
-               {
-                       $sql = "SELECT origin_id FROM fm_origin WHERE 
destination ='project' AND destination_id='$project_id' and origin ='request'";
-                       $this->db->query($sql,__LINE__,__FILE__);
-
-                       while ($this->db->next_record())
-                       {
-                               $request_id[]   = $this->db->f('origin_id');
-                       }
-
-
-                       $sql = "SELECT id as workorder_id FROM fm_workorder 
WHERE project_id='$project_id'";
-                       $this->db->query($sql,__LINE__,__FILE__);
-
-                       while ($this->db->next_record())
-                       {
-                               $workorder_id[] = $this->db->f('workorder_id');
-                       }
-
-                       $this->db->transaction_begin();
-
-                       for ($i=0;$i<count($request_id);$i++)
-                       {
-
-                               $this->db2->query("update fm_request set 
project_id = NULL where id='". $request_id[$i] . "'",__LINE__,__FILE__);
-                       }
-
-                       $this->db->query("DELETE FROM fm_project WHERE id='" . 
$project_id . "'",__LINE__,__FILE__);
-                       $this->db->query("DELETE FROM fm_project_history  WHERE 
 history_record_id='" . $project_id   . "'",__LINE__,__FILE__);
-                       $this->db->query("DELETE FROM fm_projectbranch  WHERE  
project_id='" . $project_id   . "'",__LINE__,__FILE__);
-                       $this->db->query("DELETE FROM fm_origin WHERE 
destination ='project' AND destination_id ='" . $project_id . 
"'",__LINE__,__FILE__);
-                       $this->db->query("DELETE FROM fm_workorder WHERE 
project_id='" . $project_id . "'",__LINE__,__FILE__);
-
-                       for ($i=0;$i<count($workorder_id);$i++)
-                       {
-                               $this->db->query("DELETE FROM fm_wo_hours WHERE 
workorder_id='" . $workorder_id[$i] . "'",__LINE__,__FILE__);
-                               $this->db->query("DELETE FROM 
fm_workorder_history  WHERE  history_record_id='" . $workorder_id[$i]   . 
"'",__LINE__,__FILE__);
-                       }
-
-                       $this->db->transaction_commit();
-
-               }
-       }
-?>
+                       $this->db2              = $this->bocommon->new_db();
+
+                       $this->join                     = $this->bocommon->join;
+                       $this->left_join        = $this->bocommon->left_join;
+                       $this->like                     = $this->bocommon->like;
+
+                       $this->grants   = 
$GLOBALS['phpgw']->session->appsession('grants_project',$this->currentapp);
+
+                       if(!$this->grants)
+                       {
+                               $this->acl2             = 
CreateObject($this->currentapp.'.acl2','##DEFAULT##','##DEFAULT##');
+                               $this->grants           = 
$this->acl2->get_grants($this->currentapp,'.project');
+                               
$GLOBALS['phpgw']->session->appsession('grants_project',$this->currentapp,$this->grants);
+                       }
+               }
+
+
+               function select_category_workorder_list()
+               {
+
+                       $this->db->query("SELECT id, descr FROM 
fm_workorder_category  ORDER BY descr ");
+
+                       $i = 0;
+                       while ($this->db->next_record())
+                       {
+                               $categories[$i]['id']                           
= $this->db->f('id');
+                               $categories[$i]['name']                         
= stripslashes($this->db->f('descr'));
+                               $i++;
+                       }
+                       return $categories;
+               }
+
+               function read_single_project_category($id='')
+               {
+                       $this->db->query("SELECT descr FROM 
fm_workorder_category where id='$id' ");
+                       $this->db->next_record();
+                       return $this->db->f('descr');
+               }
+
+               function select_status_list()
+               {
+                       $this->db->query("SELECT id, descr FROM 
fm_workorder_status ORDER BY id ");
+
+                       $i = 0;
+                       while ($this->db->next_record())
+                       {
+                               $status_entries[$i]['id']                       
        = $this->db->f('id');
+                               $status_entries[$i]['name']                     
        = stripslashes($this->db->f('descr'));
+                               $i++;
+                       }
+                       return $status_entries;
+               }
+
+               function select_branch_list()
+               {
+                       $this->db->query("SELECT id, descr FROM fm_branch ORDER 
BY id ");
+
+                       $i = 0;
+                       while ($this->db->next_record())
+                       {
+                               $branch_entries[$i]['id']                       
        = $this->db->f('id');
+                               $branch_entries[$i]['name']                     
        = stripslashes($this->db->f('descr'));
+                               $i++;
+                       }
+                       return $branch_entries;
+               }
+
+               function select_key_location_list()
+               {
+                       $this->db->query("SELECT id, descr FROM fm_key_loc 
ORDER BY descr ");
+
+                       $i = 0;
+                       while ($this->db->next_record())
+                       {
+                               $key_location_entries[$i]['id']                 
        = $this->db->f('id');
+                               $key_location_entries[$i]['name']               
        = stripslashes($this->db->f('descr'));
+                               $i++;
+                       }
+                       return $key_location_entries;
+               }
+
+               function read($data)
+               {
+                       if(is_array($data))
+                       {
+                               if ($data['start'])
+                               {
+                                       $start=$data['start'];
+                               }
+                               else
+                               {
+                                       $start=0;
+                               }
+                               $filter = 
$data['filter']?$data['filter']:$this->account;
+                               $query = 
(isset($data['query'])?$data['query']:'');
+                               $sort = 
(isset($data['sort'])?$data['sort']:'DESC');
+                               $order = 
(isset($data['order'])?$data['order']:'');
+                               $cat_id = 
(isset($data['cat_id'])?$data['cat_id']:0);
+                               $status_id = 
(isset($data['status_id'])?$data['status_id']:'');
+                               $start_date = 
(isset($data['start_date'])?$data['start_date']:'');
+                               $end_date = 
(isset($data['end_date'])?$data['end_date']:'');
+                               $allrows = 
(isset($data['allrows'])?$data['allrows']:'');
+                               $wo_hour_cat_id = 
(isset($data['wo_hour_cat_id'])?$data['wo_hour_cat_id']:'');
+                       }
+
+                       $sql = $this->bocommon->fm_cache('sql_project_' . 
!!$wo_hour_cat_id);
+
+                       if(!$sql)
+                       {
+                               $entity_table = 'fm_project';
+
+                               $cols .= $entity_table . '.location_code';
+                               $cols_return[] = 'location_code';
+
+                               $cols .= ",$entity_table.id as project_id";
+                               $cols_return[]                          = 
'project_id';
+                               $uicols['input_type'][]         = 'text';
+                               $uicols['name'][]                       = 
'project_id';
+                               $uicols['descr'][]                      = 
lang('Project');
+                               $uicols['statustext'][]         = lang('Project 
ID');
+
+                               $cols.= ",$entity_table.start_date";
+                               $cols_return[]                          = 
'start_date';
+                               $uicols['input_type'][]         = 'text';
+                               $uicols['name'][]                       = 
'start_date';
+                               $uicols['descr'][]                      = 
lang('start date');
+                               $uicols['statustext'][]         = lang('Project 
start date');
+
+                               $cols.= ",$entity_table.name as name";
+                               $cols_return[]                          = 
'name';
+                               $uicols['input_type'][]         = 'text';
+                               $uicols['name'][]                       = 
'name';
+                               $uicols['descr'][]                      = 
lang('name');
+                               $uicols['statustext'][]         = lang('Project 
name');
+
+                               $cols.= ",account_lid as coordinator";
+                               $cols_return[]                          = 
'coordinator';
+                               $uicols['input_type'][]         = 'text';
+                               $uicols['name'][]                       = 
'coordinator';
+                               $uicols['descr'][]                      = 
lang('Coordinator');
+                               $uicols['statustext'][]         = lang('Project 
coordinator');
+
+                               $cols.= ",$entity_table.user_id";
+
+                               $joinmethod .= " $this->join  phpgw_accounts ON 
($entity_table.coordinator = phpgw_accounts.account_id))";
+                               $paranthesis .='(';
+
+                               //----- wo_hour_status
+
+                               if($wo_hour_cat_id)
+                               {
+                                       $joinmethod .= " $this->join 
fm_workorder ON ($entity_table.id = fm_workorder.project_id))";
+                                       $paranthesis .='(';
+
+                                       $joinmethod .= " $this->join 
fm_wo_hours ON (fm_workorder.id = fm_wo_hours.workorder_id))";
+                                       $paranthesis .='(';
+
+                                       $joinmethod .= " $this->join 
fm_wo_hours_category ON (fm_wo_hours.category = fm_wo_hours_category.id))";
+                                       $paranthesis .='(';
+                               }
+
+                               //----- wo_hour_status
+
+                               $sql    = 
$this->bocommon->generate_sql(array('entity_table'=>$entity_table,'cols'=>$cols,'cols_return'=>$cols_return,
+                                                                               
'uicols'=>$uicols,'joinmethod'=>$joinmethod,'paranthesis'=>$paranthesis,'query'=>$query));
+
+                               $this->bocommon->fm_cache('sql_project_' . 
!!$wo_hour_cat_id,$sql);
+
+                               $this->uicols           = 
$this->bocommon->uicols;
+                               $cols_return            = 
$this->bocommon->cols_return;
+                               $type_id                = 
$this->bocommon->type_id;
+                               $this->cols_extra       = 
$this->bocommon->cols_extra;
+
+                               $this->bocommon->fm_cache('uicols_project_' . 
!!$wo_hour_cat_id,$this->uicols);
+                               
$this->bocommon->fm_cache('cols_return_project_' . 
!!$wo_hour_cat_id,$cols_return);
+                               $this->bocommon->fm_cache('type_id_project_' . 
!!$wo_hour_cat_id,$type_id);
+                               $this->bocommon->fm_cache('cols_extra_project_' 
. !!$wo_hour_cat_id,$this->cols_extra);
+
+                       }
+                       else
+                       {
+                               $this->uicols           = 
$this->bocommon->fm_cache('uicols_project_' . !!$wo_hour_cat_id);
+                               $cols_return            = 
$this->bocommon->fm_cache('cols_return_project_' . !!$wo_hour_cat_id);
+                               $type_id                = 
$this->bocommon->fm_cache('type_id_project_' . !!$wo_hour_cat_id);
+                               $this->cols_extra       = 
$this->bocommon->fm_cache('cols_extra_project_' . !!$wo_hour_cat_id);
+                       }
+
+                       if ($order)
+                       {
+                               $ordermethod = " order by $order $sort";
+                       }
+                       else
+                       {
+                               $ordermethod = ' order by fm_project.id DESC';
+                       }
+
+                       $where= 'WHERE';
+
+                       if ($cat_id > 0)
+                       {
+                               $filtermethod .= " $where 
fm_project.category=$cat_id ";
+                               $where= 'AND';
+                       }
+
+                       if ($status_id)
+                       {
+                               $filtermethod .= " $where 
fm_project.status='$status_id' ";
+                               $where= 'AND';
+                       }
+
+                       if($wo_hour_cat_id)
+                       {
+                               $filtermethod .= " $where 
fm_wo_hours_category.id=$wo_hour_cat_id ";
+                               $where= 'AND';
+                               $group_method = " group by 
fm_project.location_code,fm_project.id 
,fm_project.start_date,fm_project.name,account_lid 
,fm_project.user_id,fm_project.address";
+                       }
+
+                       if ($filter=='all')
+                       {
+                               if (is_array($this->grants))
+                               {
+                                       $grants = $this->grants;
+                                       while (list($user) = each($grants))
+                                       {
+                                               $public_user_list[] = $user;
+                                       }
+                                       reset($public_user_list);
+                                       $filtermethod .= " $where 
(fm_project.coordinator IN(" . implode(',',$public_user_list) . "))";
+
+                                       $where= 'AND';
+                               }
+                       }
+                       else
+                       {
+                               $filtermethod .= " $where 
fm_project.coordinator=$filter ";
+                               $where= 'AND';
+                       }
+
+                       if ($start_date)
+                       {
+                               $filtermethod .= " $where fm_project.start_date 
>= $start_date AND fm_project.start_date <= $end_date ";
+                               $where= 'AND';
+                       }
+
+
+                       if($query)
+                       {
+                               $query = str_replace(",",'.',$query);
+                               if(stristr($query, '.'))
+                               {
+                                       $query=explode(".",$query);
+                                       $querymethod = " $where 
(fm_project.loc1='" . $query[0] . "' AND fm_project.loc".$type_id."='" . 
$query[1] . "')";
+                               }
+                               else
+                               {
+                                       $query = ereg_replace("'",'',$query);
+                                       $query = ereg_replace('"','',$query);
+                                       $querymethod = " $where 
(fm_project.name $this->like '%$query%' or fm_project.address $this->like 
'%$query%' or fm_project.location_code $this->like '%$query%' or fm_project.id 
$this->like '%$query%')";
+                               }
+                       }
+
+                       $sql .= " $filtermethod $querymethod $group_method";
+//echo $sql;
+                       $this->db2->query($sql,__LINE__,__FILE__);
+                       $this->total_records = $this->db2->num_rows();
+                       if(!$allrows)
+                       {
+                               $this->db->limit_query($sql . 
$ordermethod,$start,__LINE__,__FILE__);
+                       }
+                       else
+                       {
+                               $this->db->query($sql . 
$ordermethod,__LINE__,__FILE__);
+                       }
+
+                       $j=0;
+                       $k=count($cols_return);
+                       while ($this->db->next_record())
+                       {
+                               for ($i=0;$i<$k;$i++)
+                               {
+                                       $project_list[$j][$cols_return[$i]] = 
stripslashes($this->db->f($cols_return[$i]));
+                                       $project_list[$j]['grants'] = 
(int)$this->grants[$this->db->f('user_id')];
+                               }
+
+                               $location_code= $this->db->f('location_code');
+                               $location = split('-',$location_code);
+                               $n=count($location);
+                               for ($m=0;$m<$n;$m++)
+                               {
+                                       $project_list[$j]['loc' . ($m+1)] = 
$location[$m];
+                                       
$project_list[$j]['query_location']['loc' . ($m+1)]=implode("-", 
array_slice($location, 0, ($m+1)));
+                               }
+
+                               $j++;
+                       }
+
+//_debug_array($project_list);
+                       return $project_list;
+               }
+
+               function read_single($project_id)
+               {
+                       $config = CreateObject('phpgwapi.config','property');
+                       $config->read_repository();
+                       $meter_table = $config->config_data['meter_table'];
+
+                       $sql = "SELECT * from fm_project where 
id='$project_id'";
+
+                       $this->db->query($sql,__LINE__,__FILE__);
+
+                       if ($this->db->next_record())
+                       {
+                               $project['project_id']                  = 
$this->db->f('id');
+                               $project['title']                       = 
$this->db->f('title');
+                               $project['name']                        = 
$this->db->f('name');
+                               $project['location_code']               = 
$this->db->f('location_code');
+                               $project['key_fetch']                   = 
$this->db->f('key_fetch');
+                               $project['key_deliver']                 = 
$this->db->f('key_deliver');
+                               $project['other_branch']                = 
$this->db->f('other_branch');
+                               $project['key_responsible']             = 
$this->db->f('key_responsible');
+                               $project['descr']                       = 
$this->db->f('descr');
+                               $project['status']                      = 
$this->db->f('status');
+                               $project['budget']                      = 
(int)$this->db->f('budget');
+                               $project['reserve']                     = 
(int)$this->db->f('reserve');
+                               $project['tenant_id']                   = 
$this->db->f('tenant_id');
+                               $project['user_id']                     = 
$this->db->f('user_id');
+                               $project['coordinator']                 = 
$this->db->f('coordinator');
+                               $project['access']                      = 
$this->db->f('access');
+                               $project['start_date']                  = 
$this->db->f('start_date');
+                               $project['end_date']                    = 
$this->db->f('end_date');
+                               $project['cat_id']                      = 
$this->db->f('category');
+                               $project['grants']                      = 
(int)$this->grants[$this->db->f('user_id')];
+                               $project['p_num']                       = 
$this->db->f('p_num');
+                               $project['p_entity_id']                 = 
$this->db->f('p_entity_id');
+                               $project['p_cat_id']                    = 
$this->db->f('p_cat_id');
+                               $project['contact_phone']               = 
$this->db->f('contact_phone');
+
+
+                               if($meter_table)
+                               {
+                                       $project['power_meter'] = 
$this->get_power_meter($this->db->f('location_code'),$meter_table);
+                               }
+                       }
+
+                       $sql = "SELECT * FROM fm_origin WHERE destination = 
'project' AND destination_id='$project_id' ORDER by origin DESC  ";
+
+                       $this->db->query($sql,__LINE__,__FILE__);
+
+                       $i=-1;
+                       while ($this->db->next_record())
+                       {
+                               if($last_type != $this->db->f('origin'))
+                               {
+                                       $i++;
+                               }
+                               $project['origin'][$i]['type'] = 
$this->db->f('origin');
+                               $project['origin'][$i]['link'] = 
$this->bocommon->get_origin_link($this->db->f('origin'));
+                               $project['origin'][$i]['data'][]= array(
+                                       'id'=> $this->db->f('origin_id'),
+                                       'type'=> $this->db->f('origin')
+                                       );
+
+                               $last_type=$this->db->f('origin');
+                       }
+
+//_debug_array($project);
+                               return $project;
+               }
+
+               function get_ticket($project_id = '')
+               {
+                       $sql = "SELECT * FROM fm_origin WHERE origin ='tts' AND 
destination = 'project' AND destination_id='$project_id' ORDER by origin DESC  
";
+                       $this->db->query($sql,__LINE__,__FILE__);
+                       $this->db->next_record();
+                       return $this->db->f('origin_id');
+               }
+
+               function get_power_meter($location_code = '',$meter_table='')
+               {
+                       if(!$meter_table)
+                       {
+                               $config = 
CreateObject('phpgwapi.config','property');
+                               $config->read_repository();
+                               $meter_table = 
$config->config_data['meter_table'];
+                       }
+
+                       $this->db2->query("SELECT ext_meter_id as power_meter 
FROM $meter_table where location_code='$location_code' and 
category='1'",__LINE__,__FILE__);
+
+                       $this->db2->next_record();
+
+                       return $this->db2->f('power_meter');
+               }
+
+               function project_workorder_data($project_id = '')
+               {
+                       $this->db->query("SELECT act_mtrl_cost, 
act_vendor_cost, budget, id as workorder_id, vendor_id, 
calculation,rig_addition,addition,deviation,charge_tenant from fm_workorder 
where project_id='$project_id'");
+                       while ($this->db->next_record())
+                       {
+                               $budget[] = array(
+                                       'workorder_id'  => 
$this->db->f('workorder_id'),
+                                       'budget'                => 
$this->db->f('budget'),
+                                       'deviation'             => 
$this->db->f('deviation'),
+                                       'calculation'   => 
($this->db->f('calculation')*(1+$this->db->f('addition')/100))+$this->db->f('rig_addition'),
+                                       'vendor_id'             => 
$this->db->f('vendor_id'),
+                                       'act_mtrl_cost'         => 
$this->db->f('act_mtrl_cost'),
+                                       'act_vendor_cost'       => 
$this->db->f('act_vendor_cost'),
+                                       'charge_tenant'         => 
$this->db->f('charge_tenant')
+                                       );
+                       }
+                       return $budget;
+               }
+
+               function branch_p_list($project_id = '')
+               {
+
+                       $this->db2->query("SELECT branch_id from 
fm_projectbranch WHERE project_id=" .  (int)$project_id ,__LINE__,__FILE__);
+                       while ($this->db2->next_record())
+                       {
+                               $selected[] = array('branch_id' => 
$this->db2->f('branch_id'));
+                       }
+
+                       return $selected;
+               }
+
+               function increment_project_id()
+               {
+                       $this->db->query("update fm_idgenerator set value = 
value + 1 where name = 'project'");
+               }
+
+               function next_project_id()
+               {
+                       $this->db->query("select value from fm_idgenerator 
where name = 'project'");
+                       $this->db->next_record();
+                       $project_id = $this->db->f('value')+1;
+                       return $project_id;
+               }
+
+               function add($project)
+               {
+                       $historylog     = 
CreateObject($this->currentapp.'.historylog','project');
+
+                       while (is_array($project['location']) && 
list($input_name,$value) = each($project['location']))
+                       {
+                               if($value)
+                               {
+                                       $cols[] = $input_name;
+                                       $vals[] = $value;
+                               }
+                       }
+
+                       while (is_array($project['extra']) && 
list($input_name,$value) = each($project['extra']))
+                       {
+                               if($value)
+                               {
+                                       $cols[] = $input_name;
+                                       $vals[] = $value;
+                               }
+                       }
+
+                       if($cols)
+                       {
+                               $cols   = "," . implode(",", $cols);
+                               $vals   = ",'" . implode("','", $vals) . "'";
+                       }
+
+                       if($project['street_name'])
+                       {
+                               $address[]= $project['street_name'];
+                               $address[]= $project['street_number'];
+                               $address = $this->db->db_addslashes(implode(" 
", $address));
+                       }
+
+                       if(!$address)
+                       {
+                               $address = 
$this->db->db_addslashes($project['location_name']);
+                       }
+
+                       $project['descr'] = 
$this->db->db_addslashes($project['descr']);
+                       $project['name'] = 
$this->db->db_addslashes($project['name']);
+
+                       $values= array(
+                               $project['project_id'],
+                               $project['name'],
+                               'public',
+                               $project['cat_id'],
+                               time(),
+                               $project['start_date'],
+                               $project['end_date'],
+                               $project['coordinator'],
+                               $project['status'],
+                               $project['descr'],
+                               $project['budget'],
+                               $project['reserve'],
+                               $project['location_code'],
+                               $address,
+                               $project['key_deliver'],
+                               $project['key_fetch'],
+                               $project['other_branch'],
+                               $project['key_responsible'],
+                               $this->account);
+
+                       $values = $this->bocommon->validate_db_insert($values);
+
+                       $this->db->transaction_begin();
+
+                       $this->db->query("INSERT INTO fm_project 
(id,name,access,category,entry_date,start_date,end_date,coordinator,status,"
+                               . 
"descr,budget,reserve,location_code,address,key_deliver,key_fetch,other_branch,key_responsible,user_id
 $cols) "
+                               . "VALUES ($values $vals )",__LINE__,__FILE__);
+
+                       if($project['extra']['contact_phone'] && 
$project['extra']['tenant_id'])
+                       {
+                               $this->db->query("update fm_tenant set 
contact_phone='". $project['extra']['contact_phone']. "' where id='". 
$project['extra']['tenant_id']. "'",__LINE__,__FILE__);
+                       }
+
+                       $config = CreateObject('phpgwapi.config','property');
+                       $config->read_repository();
+                       $meter_table = $config->config_data['meter_table'];
+
+                       if ($project['power_meter'] && $meter_table)
+                       {
+                               
$this->update_power_meter($project['power_meter'],$project['location_code'],$address,$meter_table);
+                       }
+
+                       if (count($project['branch']) != 0)
+                       {
+                               while($branch=each($project['branch']))
+                               {
+                                       $this->db->query("insert into 
fm_projectbranch (project_id,branch_id) values ('" . $project['project_id']. 
"','$branch[1]')",__LINE__,__FILE__);
+                               }
+                       }
+
+                       if($project['origin'][0]['data'][0]['id'])
+                       {
+                               $this->db->query("INSERT INTO  fm_origin 
(origin,origin_id,destination,destination_id,user_id,entry_date) "
+                                       . "VALUES ('"
+                                       . $project['origin'][0]['type']. "','"
+                                       . 
$project['origin'][0]['data'][0]['id']. "',"
+                                       . "'project',"
+                                       . $project['project_id']. ","
+                                       . $this->account . ","
+                                       . time() . ")",__LINE__,__FILE__);
+                       }
+
+
+                       if($this->db->transaction_commit())
+                       {
+                               $this->increment_project_id();
+                               
$historylog->add('SO',$project['project_id'],$project['status']);
+                               
$historylog->add('TO',$project['project_id'],$project['cat_id']);
+                               
$historylog->add('CO',$project['project_id'],$project['coordinator']);
+
+                               $receipt['message'][] = 
array('msg'=>lang('project %1 has been saved',$project['project_id']));
+                       }
+                       else
+                       {
+                               $receipt['error'][] = array('msg'=>lang('the 
project has not been saved'));
+                       }
+                       return $receipt;
+               }
+
+               function 
update_power_meter($power_meter,$location_code,$address,$meter_table)
+               {
+
+                       $location=explode('-',$location_code);
+
+                       $i=1;
+                       if (isset($location) AND is_array($location))
+                       {
+                               foreach($location as $location_entry)
+                               {
+
+                                       $cols[] = 'loc' . $i;
+                                       $vals[] = $location_entry;
+
+                                       $i++;
+                               }
+
+                       }
+
+                       if($cols)
+                       {
+                               $cols   = "," . implode(",", $cols);
+                               $vals   = ",'" . implode("','", $vals) . "'";
+                       }
+
+
+                       $this->db->query("SELECT count(*) FROM $meter_table 
where location_code='$location_code' and category=1",__LINE__,__FILE__);
+
+                       $this->db->next_record();
+
+                       if ( $this->db->f(0))
+                       {
+                               $this->db->query("update $meter_table set 
ext_meter_id='$power_meter',address='$address' where 
location_code='$location_code' and category='1'",__LINE__,__FILE__);
+                       }
+                       else
+                       {
+                               $id = $this->bocommon->next_id($meter_table);
+
+                               $meter_id       = 
$this->generate_meter_id($meter_table);
+                               $this->db->query("insert into $meter_table 
(id,num,ext_meter_id,category,location_code,entry_date,user_id,address $cols) "
+                                       . "VALUES ('"
+                                       . $id. "','"
+                                       . $meter_id. "','"
+                                       . $power_meter. "',"
+                                       . "1,'"
+                                       . $location_code. "',"
+                                       . time() . ",$this->account, '$address' 
$vals)",__LINE__,__FILE__);
+                       }
+               }
+
+               function generate_meter_id($meter_table)
+               {
+                       $prefix = 'meter';
+                       $pos    = strlen($prefix);
+                       $this->db->query("select max(num) from $meter_table 
where num $this->like ('$prefix%')");
+                       $this->db->next_record();
+
+                       $max = 
$this->bocommon->add_leading_zero(substr($this->db->f(0),$pos));
+
+                       $meter_id= $prefix . $max;
+                       return $meter_id;
+               }
+
+               function edit($project)
+               {
+                       $historylog     = 
CreateObject($this->currentapp.'.historylog','project');
+
+                       while (is_array($project['location']) && 
list($input_name,$value) = each($project['location']))
+                       {
+                               $vals[] = "$input_name = '$value'";
+                       }
+
+                       while (is_array($project['extra']) && 
list($input_name,$value) = each($project['extra']))
+                       {
+                               $vals[] = "$input_name = '$value'";
+                       }
+
+                       if($vals)
+                       {
+                               $vals   = "," . implode(",",$vals);
+                       }
+
+                       if($project['street_name'])
+                       {
+                               $address[]= $project['street_name'];
+                               $address[]= $project['street_number'];
+                               $address = $this->db->db_addslashes(implode(" 
", $address));
+                       }
+
+                       if(!$address)
+                       {
+                               $address = 
$this->db->db_addslashes($project['location_name']);
+                       }
+
+                       $project['descr'] = 
$this->db->db_addslashes($project['descr']);
+                       $project['name'] = 
$this->db->db_addslashes($project['name']);
+
+                       $value_set=array(
+                               'name'                  => $project['name'],
+                               'status'                => $project['status'],
+                               'category'              => $project['cat_id'],
+                               'start_date'            => 
$project['start_date'],
+                               'end_date'              => $project['end_date'],
+                               'coordinator'           => 
$project['coordinator'],
+                               'descr'                 => $project['descr'],
+                               'budget'                => 
(int)$project['budget'],
+                               'reserve'               => 
(int)$project['reserve'],
+                               'key_deliver'           => 
$project['key_deliver'],
+                               'key_fetch'             => 
$project['key_fetch'],
+                               'other_branch'          => 
$project['other_branch'],
+                               'key_responsible'       => 
$project['key_responsible'],
+                               'location_code'         => 
$project['location_code'],
+                               'address'               => $address
+                               );
+
+                       $value_set      = 
$this->bocommon->validate_db_update($value_set);
+
+                       $this->db->transaction_begin();
+
+                       $this->db->query("SELECT status,category,coordinator 
FROM fm_project where id='" .$project['project_id']."'",__LINE__,__FILE__);
+                       $this->db->next_record();
+                       $old_status = $this->db->f('status');
+                       $old_categroy = (int)$this->db->f('category');
+                       $old_coordinator = (int)$this->db->f('coordinator');
+
+                       $this->db->query("UPDATE fm_project set $value_set 
$vals WHERE id= '" . $project['project_id'] ."'",__LINE__,__FILE__);
+
+                       if($project['extra']['contact_phone'] && 
$project['extra']['tenant_id'])
+                       {
+                               $this->db->query("update fm_tenant set 
contact_phone='". $project['extra']['contact_phone']. "' where id='". 
$project['extra']['tenant_id']. "'",__LINE__,__FILE__);
+                       }
+
+                       $config = CreateObject('phpgwapi.config','property');
+                       $config->read_repository();
+                       $meter_table = $config->config_data['meter_table'];
+
+                       if ($project['power_meter'] && $meter_table)
+                       {
+                               
$this->update_power_meter($project['power_meter'],$project['location_code'],$address,$meter_table);
+                       }
+       // -----------------which branch is represented
+                       $this->db->query("delete from fm_projectbranch where 
project_id='" . $project['project_id'] ."'",__LINE__,__FILE__);
+
+                       if (count($project['branch']) != 0)
+                       {
+                               while($branch=each($project['branch']))
+                               {
+                                       $this->db->query("insert into 
fm_projectbranch (project_id,branch_id) values ('" . $project['project_id']. 
"','$branch[1]')",__LINE__,__FILE__);
+                               }
+                       }
+
+                       if($project['delete_request'])
+                       {
+                               $receipt = 
$this->delete_request_from_project($project['delete_request'],$project['project_id']);
+
+                       }
+
+                       
$this->update_request_status($project['project_id'],$project['status'],$project['cat_id'],$project['coordinator']);
+
+                       if ($old_status != $project['status'])
+                       {
+                               
$historylog->add('S',$project['project_id'],$project['status']);
+
+                               $this->db->query("UPDATE fm_workorder set 
status='". $project['status'] . "' WHERE project_id= '" . 
$project['project_id'] ."'",__LINE__,__FILE__);
+
+                               $this->db2->query("SELECT id from fm_workorder 
WHERE project_id=" .  (int)$project['project_id'] ,__LINE__,__FILE__);
+                               while ($this->db2->next_record())
+                               {
+                                       $workorder[] = $this->db2->f('id');
+                               }
+
+                               if (isset($workorder) AND is_array($workorder))
+                               {
+                                       $historylog_workorder   = 
CreateObject($this->currentapp.'.historylog','workorder');
+                                       foreach($workorder as $workorder_id)
+                                       {
+                                               
$historylog_workorder->add('S',$workorder_id,$project['status']);
+                                       }
+                               }
+
+                               $receipt['notice_owner'][]=lang('Status 
changed') . ': ' . $project['status'];
+                       }
+                       if ($old_categroy != $project['cat_id'])
+                       {
+                               
$historylog->add('T',$project['project_id'],$project['cat_id']);
+                       }
+                       if ($old_coordinator != $project['coordinator'])
+                       {
+                               
$historylog->add('C',$project['project_id'],$project['coordinator']);
+                               $receipt['notice_owner'][]=lang('Coordinator 
changed') . ': ' . 
$GLOBALS['phpgw']->accounts->id2name($project['coordinator']);
+                       }
+
+                       $receipt['message'][] = array('msg'=>lang('project %1 
has been edited',$project['project_id']));
+
+                       $this->db->transaction_commit();
+
+                       return $receipt;
+
+               }
+
+
+               function delete_request_from_project($request_id,$project_id)
+               {
+                       for ($i=0;$i<count($request_id);$i++)
+                       {
+                               $this->db2->query("update fm_request set 
project_id = NULL where id='". $request_id[$i] . "'",__LINE__,__FILE__);
+                               $this->db->query("DELETE FROM fm_origin WHERE 
destination ='project' AND origin_id='" . $request_id[$i] . "' AND 
origin='request'",__LINE__,__FILE__);
+                               $receipt['message'][] = 
array('msg'=>lang('Request %1 has been deleted from project 
%2',$request_id[$i],$project_id));
+                       }
+                       return $receipt;
+               }
+
+
+               function 
update_request_status($project_id='',$status='',$category=0,$coordinator=0)
+               {
+                       $historylog_r   = 
CreateObject($this->currentapp.'.historylog','request');
+
+                       $sql = "SELECT origin_id FROM fm_origin WHERE 
destination ='project' AND destination_id='$project_id' and origin ='request'";
+//                     $sql = "SELECT origin_id FROM fm_project_origin WHERE 
project_id='$project_id' and origin ='request'";
+                       $this->db->query($sql,__LINE__,__FILE__);
+
+                       while ($this->db->next_record())
+                       {
+                               $request_id[]   = $this->db->f('origin_id');
+                       }
+
+                       for ($i=0;$i<count($request_id);$i++)
+                       {
+                               $this->db->query("SELECT 
status,category,coordinator FROM fm_request where id='" .$request_id[$i] 
."'",__LINE__,__FILE__);
+
+                               $this->db->next_record();
+
+                               $old_status = $this->db->f('status');
+                               $old_category = (int)$this->db->f('category');
+                               $old_coordinator = 
(int)$this->db->f('coordinator');
+
+                               if ($old_status != $status)
+                               {
+                                       
$historylog_r->add('S',$request_id[$i],$status);
+                               }
+
+                               if ((int)$old_category != (int)$category)
+                               {
+                                       
$historylog_r->add('T',$request_id[$i],$category);
+                               }
+
+                               if ((int)$old_coordinator != (int)$coordinator)
+                               {
+                                       
$historylog_r->add('C',$request_id[$i],$coordinator);
+                               }
+
+                               $this->db2->query("update fm_request set 
status='$status',coordinator='$coordinator' where id='". $request_id[$i] . 
"'",__LINE__,__FILE__);
+                       }
+               }
+
+
+               function check_request($request_id)
+               {
+                       $sql = "SELECT destination_id FROM fm_origin WHERE 
destination ='project' AND origin_id='$request_id' and origin ='request'";
+                       $this->db->query($sql,__LINE__,__FILE__);
+
+                       $this->db->next_record();
+
+                       if ( $this->db->f(0))
+                       {
+                               return $this->db->f('destination_id');
+                       }
+               }
+
+               function add_request($add_request,$id)
+               {
+
+                       for ($i=0;$i<count($add_request['request_id']);$i++)
+                       {
+                               
$project_id=$this->check_request($add_request['request_id'][$i]);
+
+                               if(!$project_id)
+                               {
+                                       $this->db->query("INSERT INTO  
fm_origin (origin,origin_id,destination,destination_id,user_id,entry_date) "
+                                               . "VALUES ('request','"
+                                               . 
$add_request['request_id'][$i]. "','"
+                                               . "project',"
+                                               . $id . ","
+                                               . $this->account . ","
+                                               . time() . 
")",__LINE__,__FILE__);
+
+                                       $this->db2->query("update fm_request 
set project_id='$id' where id='". $add_request['request_id'][$i] . 
"'",__LINE__,__FILE__);
+
+                                       $receipt['message'][] = 
array('msg'=>lang('request %1 has been added',$add_request['request_id'][$i]));
+                               }
+                               else
+                               {
+                                       $receipt['error'][] = 
array('msg'=>lang('request %1 has already been added to project 
%2',$add_request['request_id'][$i],$project_id));
+                               }
+
+                       }
+
+                       return $receipt;
+               }
+
+               function delete($project_id )
+               {
+                       $sql = "SELECT origin_id FROM fm_origin WHERE 
destination ='project' AND destination_id='$project_id' and origin ='request'";
+                       $this->db->query($sql,__LINE__,__FILE__);
+
+                       while ($this->db->next_record())
+                       {
+                               $request_id[]   = $this->db->f('origin_id');
+                       }
+
+                       $sql = "SELECT id as workorder_id FROM fm_workorder 
WHERE project_id='$project_id'";
+                       $this->db->query($sql,__LINE__,__FILE__);
+
+                       while ($this->db->next_record())
+                       {
+                               $workorder_id[] = $this->db->f('workorder_id');
+                       }
+
+                       $this->db->transaction_begin();
+
+                       for ($i=0;$i<count($request_id);$i++)
+                       {
+
+                               $this->db2->query("update fm_request set 
project_id = NULL where id='". $request_id[$i] . "'",__LINE__,__FILE__);
+                       }
+
+                       $this->db->query("DELETE FROM fm_project WHERE id='" . 
$project_id . "'",__LINE__,__FILE__);
+                       $this->db->query("DELETE FROM fm_project_history  WHERE 
 history_record_id='" . $project_id   . "'",__LINE__,__FILE__);
+                       $this->db->query("DELETE FROM fm_projectbranch  WHERE  
project_id='" . $project_id   . "'",__LINE__,__FILE__);
+                       $this->db->query("DELETE FROM fm_origin WHERE 
destination ='project' AND destination_id ='" . $project_id . 
"'",__LINE__,__FILE__);
+                       $this->db->query("DELETE FROM fm_workorder WHERE 
project_id='" . $project_id . "'",__LINE__,__FILE__);
+
+                       for ($i=0;$i<count($workorder_id);$i++)
+                       {
+                               $this->db->query("DELETE FROM fm_wo_hours WHERE 
workorder_id='" . $workorder_id[$i] . "'",__LINE__,__FILE__);
+                               $this->db->query("DELETE FROM 
fm_workorder_history  WHERE  history_record_id='" . $workorder_id[$i]   . 
"'",__LINE__,__FILE__);
+                       }
+
+                       $this->db->transaction_commit();
+
+               }
+       }
+?>
Index: property/inc/class.sorequest.inc.php
diff -u property/inc/class.sorequest.inc.php:1.12 
property/inc/class.sorequest.inc.php:1.13
--- property/inc/class.sorequest.inc.php:1.12   Fri Jan 27 14:05:43 2006
+++ property/inc/class.sorequest.inc.php        Mon Jan 30 22:14:19 2006
@@ -1,642 +1,639 @@
-<?php
-       /**
-       * phpGroupWare - property: a Facilities Management System.
-       *
-       * @author Sigurd Nes <address@hidden>
-       * @copyright Copyright (C) 2003-2005 Free Software Foundation, Inc. 
http://www.fsf.org/
-       * @license http://www.gnu.org/licenses/gpl.html GNU General Public 
License
-       * @internal Development of this application was funded by 
http://www.bergen.kommune.no/bbb_/ekstern/
-       * @package property
-       * @subpackage project
-       * @version $Id: class.sorequest.inc.php,v 1.12 2006/01/27 14:05:43 
sigurdne Exp $
-       */
-
-       /**
-        * Description
-        * @package property
-        */
-
-       class sorequest
-       {
-               function sorequest()
-               {
-                       $this->currentapp       = 
$GLOBALS['phpgw_info']['flags']['currentapp'];
-                       $this->account          = 
$GLOBALS['phpgw_info']['user']['account_id'];
-                       $this->soproject        = 
CreateObject($this->currentapp.'.soproject');
-                       $this->historylog       = 
CreateObject($this->currentapp.'.historylog','request');
-                       $this->bocommon         = 
CreateObject($this->currentapp.'.bocommon');
-                       $this->db               = $this->bocommon->new_db();
-                       $this->db2              = $this->bocommon->new_db();
-                       $this->join                     = $this->bocommon->join;
-                       $this->like                     = $this->bocommon->like;
-               }
-
-               function select_category_workorder_list()
-               {
-
-                       $this->db->query("SELECT id, descr FROM 
fm_workorder_category  ORDER BY descr ");
-
-                       $i = 0;
-                       while ($this->db->next_record())
-                       {
-                               $categories[$i]['id']                           
= $this->db->f('id');
-                               $categories[$i]['name']                         
= stripslashes($this->db->f('descr'));
-                               $i++;
-                       }
-                       return $categories;
-               }
-
-               function read_priority_key()
-               {
-                       $this->db->query("SELECT * FROM 
fm_request_condition_type",__LINE__,__FILE__);
-
-                       while ($this->db->next_record())
-                       {
-                               $priority_key[] = array(
-                                       'id'            => $this->db->f('id'),
-                                       'descr'         => 
$this->db->f('descr'),
-                                       'priority_key'  => 
$this->db->f('priority_key')
-                                       );      
-                       }
-
-                       return $priority_key;
-               }
-
-               function update_priority_key($values)
-               {
-
-                       while (is_array($values['priority_key']) && 
list($id,$priority_key) = each($values['priority_key']))
-                       {
-                               $this->db->query("UPDATE 
fm_request_condition_type SET priority_key = $priority_key WHERE id = 
$id",__LINE__,__FILE__);
-                       }
-
-                       $this->update_score();
-
-                       $receipt['message'][] = array('msg'=> lang('priority 
keys has been updated'));
-                       return $receipt;
-
-               }
-
-
-               function update_score($request_id='')
-               {
-                       if($request_id)
-                       {
-                               $request[] = $request_id;
-                       }
-                       else
-                       {
-                               $this->db->query("SELECT id FROM 
fm_request",__LINE__,__FILE__);
-       
-                               while ($this->db->next_record())
-                               {
-                                       $request[] = $this->db->f('id');
-                               }
-                       }
-
-                       while (is_array($request) && list(,$id) = 
each($request))
-                       {
-
-                               
if($GLOBALS['phpgw_info']['server']['db_type']=='pgsql')
-                               {
-                                       $sql = "UPDATE fm_request SET score = 
(SELECT sum(priority_key * ( degree * probability * ( consequence +1 )))  FROM 
fm_request_condition"
-                                        . " $this->join  
fm_request_condition_type ON (fm_request_condition.condition_type = 
fm_request_condition_type.id) WHERE request_id = $id) WHERE fm_request.id = 
$id";
-       
-                                       
$this->db->query($sql,__LINE__,__FILE__);
-                               }
-                               else
-                               {
-                                       $sql = "SELECT sum(priority_key * ( 
degree * probability * ( consequence +1 ))) AS score FROM fm_request_condition"
-                                        . " $this->join  
fm_request_condition_type ON (fm_request_condition.condition_type = 
fm_request_condition_type.id) WHERE request_id = $id";
-       
-                                       
$this->db->query($sql,__LINE__,__FILE__);
-
-                                       $this->db->next_record();
-                                       $score = $this->db->f('score');
-                                       $this->db->query("UPDATE fm_request SET 
score = $score WHERE id = $id",__LINE__,__FILE__);
-                               }
-                       }
-               }
-
-
-               function read_single_request_category($id='')
-               {
-                       $this->db->query("SELECT descr FROM 
fm_workorder_category where id='$id' ");
-                       $this->db->next_record();
-                       return $this->db->f('descr');
-               }
-
-               function select_status_list()
-               {
-                       $this->db->query("SELECT id, descr FROM 
fm_request_status ORDER BY id ");
-
-                       $i = 0;
-                       while ($this->db->next_record())
-                       {
-                               $status_entries[$i]['id']                       
        = $this->db->f('id');
-                               $status_entries[$i]['name']                     
        = stripslashes($this->db->f('descr'));
-                               $i++;
-                       }
-                       return $status_entries;
-               }
-
-               function select_condition_type_list()
-               {
-                       $this->db->query("SELECT id, descr FROM 
fm_request_condition_type ORDER BY id ");
-
-                       $i = 0;
-                       while ($this->db->next_record())
-                       {
-                               $condition_type_list[$i]['id']          = 
$this->db->f('id');
-                               $condition_type_list[$i]['name']        = 
stripslashes($this->db->f('descr'));
-                               $i++;
-                       }
-                       return $condition_type_list;
-               }
-
-               function 
select_conditions($request_id='',$condition_type_list='')
-               {
-                       for ($i=0;$i<count($condition_type_list);$i++)
-                       {
-                               $this->db->query("SELECT 
degree,probability,consequence FROM fm_request_condition WHERE 
request_id=$request_id AND condition_type =" . $condition_type_list[$i]['id']);
-                               $this->db->next_record();
-                               $conditions[$i]['request_id']           = 
$request_id;
-                               $conditions[$i]['degree']               = 
$this->db->f('degree');
-                               $conditions[$i]['probability']          = 
$this->db->f('probability');
-                               $conditions[$i]['consequence']          = 
$this->db->f('consequence');
-                       }
-
-                       return $conditions;
-               }
-
-
-               function read($data)
-               {
-                       if(is_array($data))
-                       {
-                               if ($data['start'])
-                               {
-                                       $start=$data['start'];
-                               }
-                               else
-                               {
-                                       $start=0;
-                               }
-                               $filter = 
(isset($data['filter'])?$data['filter']:'');
-                               $query = 
(isset($data['query'])?$data['query']:'');
-                               $sort = 
(isset($data['sort'])?$data['sort']:'DESC');
-                               $order = 
(isset($data['order'])?$data['order']:'');
-                               $cat_id = 
(isset($data['cat_id'])?$data['cat_id']:0);
-                               $status_id = 
(isset($data['status_id'])?$data['status_id']:0);
-                               $project_id = 
(isset($data['project_id'])?$data['project_id']:'');
-                       }
-
-                       $entity_table = 'fm_request';
-
-                       $cols .= $entity_table . '.location_code';
-                       $cols_return[] = 'location_code';
-
-                       $cols .= ",$entity_table.id as request_id";
-                       $cols_return[]                          = 'request_id';
-                       $uicols['input_type'][]         = 'text';
-                       $uicols['name'][]                       = 'request_id';
-                       $uicols['descr'][]                      = 
lang('Request');
-                       $uicols['statustext'][]         = lang('Request ID');
-
-                       $cols.= ",$entity_table.entry_date";
-                       $cols_return[]                          = 'entry_date';
-                       $uicols['input_type'][]         = 'text';
-                       $uicols['name'][]                       = 'entry_date';
-                       $uicols['descr'][]                      = lang('entry 
date');
-                       $uicols['statustext'][]         = lang('Request entry 
date');
-
-                       $cols.= ",$entity_table.title as title";
-                       $cols_return[]                          = 'title';
-                       $uicols['input_type'][]         = 'text';
-                       $uicols['name'][]                       = 'title';
-                       $uicols['descr'][]                      = lang('title');
-                       $uicols['statustext'][]         = lang('Request title');
-
-                       $cols.= ",$entity_table.coordinator";
-                       $cols_return[]                          = 'coordinator';
-                       $uicols['input_type'][]         = 'text';
-                       $uicols['name'][]                       = 'coordinator';
-                       $uicols['descr'][]                      = 
lang('Coordinator');
-                       $uicols['statustext'][]         = lang('Project 
coordinator');
-
-                       $cols.= ",$entity_table.score";
-                       $cols_return[]                          = 'score';
-                       $uicols['input_type'][]         = 'text';
-                       $uicols['name'][]                       = 'score';
-                       $uicols['descr'][]                      = lang('score');
-                       $uicols['statustext'][]         = lang('score');
-
-                       $sql    = 
$this->bocommon->generate_sql(array('entity_table'=>$entity_table,'cols'=>$cols,'cols_return'=>$cols_return,
-                                                                               
                                        
'uicols'=>$uicols,'joinmethod'=>$joinmethod,'paranthesis'=>$paranthesis,'query'=>$query));
-
-                       if ($order)
-                       {
-                               $ordermethod = " order by $order $sort";
-                       }
-                       else
-                       {
-                               $ordermethod = ' order by fm_request.id ASC';
-                       }
-
-                       $where = 'WHERE';
-                       if ($cat_id > 0)
-                       {
-                               $filtermethod .= " $where 
fm_request.category='$cat_id' ";
-                               $where = 'AND';
-                       }
-
-                       if ($status_id)
-                       {
-                               $filtermethod .= " $where  
fm_request.status='$status_id' ";
-                               $where = 'AND';
-                       }
-
-                       if ($filter)
-                       {
-                               $filtermethod .= " $where 
fm_request.coordinator='$filter' ";
-                               $where = 'AND';
-                       }
-
-                       if ($project_id)// lookup requests not already 
allocated to projects
-                       {
-                               $filtermethod .= " $where project_id is NULL ";
-                               $where = 'AND';
-                       }
-
-                       if($query)
-                       {
-                               $query = ereg_replace("'",'',$query);
-                               $query = ereg_replace('"','',$query);
-
-                               $querymethod = " $where (fm_request.title 
$this->like '%$query%' or fm_request.address $this->like '%$query%' or 
fm_request.location_code $this->like '%$query%')";
-                       }
-
-                       $sql .= " $filtermethod $querymethod";
-
-                       $this->uicols           = $this->bocommon->uicols;
-                       $cols_return            = $this->bocommon->cols_return;
-                       $type_id                        = 
$this->bocommon->type_id;
-                       $this->cols_extra       = $this->bocommon->cols_extra;
-
-                       $this->db2->query($sql,__LINE__,__FILE__);
-                       $this->total_records = $this->db2->num_rows();
-                       $this->db->limit_query($sql . 
$ordermethod,$start,__LINE__,__FILE__);
-
-
-                       $j=0;
-                       while ($this->db->next_record())
-                       {
-                               for ($i=0;$i<count($cols_return);$i++)
-                               {
-                                       $request_list[$j][$cols_return[$i]] = 
stripslashes($this->db->f($cols_return[$i]));
-                               }
-
-                               $location_code= $this->db->f('location_code');
-                               $location = split('-',$location_code);
-                               for ($m=0;$m<count($location);$m++)
-                               {
-                                       $request_list[$j]['loc' . ($m+1)] = 
$location[$m];
-                                       
$request_list[$j]['query_location']['loc' . ($m+1)]=implode("-", 
array_slice($location, 0, ($m+1)));
-                               }
-
-                               $j++;
-                       }
-//_debug_array($request_list);
-                       return $request_list;
-               }
-
-               function read_single($request_id)
-               {
-                       $sql = "SELECT * from fm_request where 
id='$request_id'";
-
-                       $this->db->query($sql,__LINE__,__FILE__);
-
-                       if ($this->db->next_record())
-                       {
-                               $request['request_id']                          
        = $this->db->f('id');
-                               $request['title']                               
                = $this->db->f('title');
-                               $request['location_code']                       
        = $this->db->f('location_code');
-                               $request['descr']                               
                = $this->db->f('descr');
-                               $request['status']                              
                = $this->db->f('status');
-                               $request['budget']                              
                = (int)$this->db->f('budget');
-                               $request['tenant_id']                           
        = $this->db->f('tenant_id');
-                               $request['owner']                               
                = $this->db->f('owner');
-                               $request['coordinator']                         
        = $this->db->f('coordinator');
-                               $request['access']                              
                = $this->db->f('access');
-                               $request['start_date']                          
        = $this->db->f('start_date');
-                               $request['end_date']                            
        = $this->db->f('end_date');
-                               $request['cat_id']                              
                = $this->db->f('category');
-
-                               $request['branch_id']                           
        = $this->db->f('branch_id');
-                               $request['authorities_demands']                 
= $this->db->f('authorities_demands');
-                               $request['score']                               
                = $this->db->f('score');
-                               $request['p_num']                               
                = $this->db->f('p_num');
-                               $request['p_entity_id']                         
        = $this->db->f('p_entity_id');
-                               $request['p_cat_id']                            
        = $this->db->f('p_cat_id');
-                               $request['contact_phone']                       
        = $this->db->f('contact_phone');
-
-                               $request['power_meter'] = 
$this->soproject->get_power_meter($this->db->f('location_code'));
-                       }
-
-                       $sql = "SELECT * FROM fm_origin WHERE destination = 
'request' AND destination_id='$request_id' ORDER by origin DESC  ";
-
-                       $this->db->query($sql,__LINE__,__FILE__);
-
-                       $i=-1;
-                       while ($this->db->next_record())
-                       {
-                               if($last_type != $this->db->f('origin'))
-                               {
-                                       $i++;
-                               }
-                               $request['origin'][$i]['type'] = 
$this->db->f('origin');
-                               $request['origin'][$i]['link'] = 
$this->bocommon->get_origin_link($this->db->f('origin'));
-                               $request['origin'][$i]['data'][]= array(
-                                       'id'=> $this->db->f('origin_id'),
-                                       'type'=> $this->db->f('origin')
-                                       );
-
-                               $last_type=$this->db->f('origin');
-                       }
-
-                       $sql = "SELECT * FROM fm_origin WHERE origin = 
'request' AND origin_id='$request_id' ORDER by destination DESC  ";
-
-                       $this->db->query($sql,__LINE__,__FILE__);
-
-                       while ($this->db->next_record())
-                       {
-                               if($last_type != $this->db->f('destination'))
-                               {
-                                       $i++;
-                               }
-                               $request['origin'][$i]['type'] = 
$this->db->f('destination');
-                               $request['origin'][$i]['link'] = 
$this->bocommon->get_origin_link($this->db->f('destination'));
-                               $request['origin'][$i]['data'][]= array(
-                                       'id'=> $this->db->f('destination_id'),
-                                       'type'=> $this->db->f('destination')
-                                       );
-
-                               $last_type=$this->db->f('destination');
-                       }
-
-                       return $request;
-               }
-
-               function request_workorder_data($request_id = '')
-               {
-                       $this->db->query("select budget, id as workorder_id, 
vendor_id from fm_workorder where request_id='$request_id'");
-                       while ($this->db->next_record())
-                       {
-                               $budget[] = array(
-                                       'workorder_id'  => 
$this->db->f('workorder_id'),
-                                       'budget'                => 
sprintf("%01.2f",$this->db->f('budget')),
-                                       'vendor_id'     => 
$this->db->f('vendor_id')
-                                       );
-                       }
-                       return $budget;
-               }
-
-
-               function increment_request_id()
-               {
-                       $this->db->query("update fm_idgenerator set value = 
value + 1 where name = 'request'");
-               }
-
-               function next_id()
-               {
-                       $this->db->query("select value from fm_idgenerator 
where name = 'request'");
-                       $this->db->next_record();
-                       $id = $this->db->f('value')+1;
-                       return $id;
-               }
-
-               function add($request)
-               {
-//_debug_array($request);
-                       while (is_array($request['location']) && 
list($input_name,$value) = each($request['location']))
-                       {
-                               if($value)
-                               {
-                                       $cols[] = $input_name;
-                                       $vals[] = $value;
-                               }
-                       }
-
-                       while (is_array($request['extra']) && 
list($input_name,$value) = each($request['extra']))
-                       {
-                               if($value)
-                               {
-                                       $cols[] = $input_name;
-                                       $vals[] = $value;
-                               }
-                       }
-
-                       if($cols)
-                       {
-                               $cols   = "," . implode(",", $cols);
-                               $vals   = ",'" . implode("','", $vals) . "'";
-                       }
-
-                       if($request['street_name'])
-                       {
-                               $address[]= $request['street_name'];
-                               $address[]= $request['street_number'];
-                               $address        = 
$this->db->db_addslashes(implode(" ", $address));
-                       }
-
-                       if(!$address)
-                       {
-                               $address = 
$this->db->db_addslashes($request['location_name']);
-                       }
-
-                       $request['descr'] = 
$this->db->db_addslashes($request['descr']);
-                       $request['name'] = 
$this->db->db_addslashes($request['name']);
-                       $request['title'] = 
$this->db->db_addslashes($request['title']);
-
-                       $values= array(
-                               $request['request_id'],
-                               $request['title'],
-                               $this->account,
-                               $request['cat_id'],
-                               $request['descr'],
-                               $request['location_code'],
-                               $address,
-                               time(),
-                               $request['budget'],
-                               $request['status'],
-                               $request['branch_id'],
-                               $request['coordinator'],
-                               $request['authorities_demands']);
-
-                       $values = $this->bocommon->validate_db_insert($values);
-
-                       $this->db->transaction_begin();
-
-                       $this->db->query("insert into fm_request 
(id,title,owner,category,descr,location_code,"
-                               . 
"address,entry_date,budget,status,branch_id,coordinator,"
-                               . "authorities_demands  $cols) "
-                               . "VALUES ($values $vals )",__LINE__,__FILE__);
-
-                       while (is_array($request['condition']) && 
list($condition_type,$value_type) = each($request['condition']))
-                       {
-                               $this->db->query("INSERT INTO 
fm_request_condition 
(request_id,condition_type,degree,probability,consequence,user_id,entry_date) "
-                                       . "VALUES ('"
-                                       . $request['request_id']. "','"
-                                       . $condition_type . "',"
-                                       . $value_type['degree']. ","
-                                       . $value_type['probability']. ","
-                                       . $value_type['consequence']. ","
-                                       . $this->account . ","
-                                       . time() . ")",__LINE__,__FILE__);
-                       }
-
-                       $this->update_score($request['request_id']);
-
-
-                       if($request['extra']['contact_phone'] && 
$request['extra']['tenant_id'])
-                       {
-                               $this->db->query("update fm_tenant set 
contact_phone='". $request['extra']['contact_phone']. "' where id='". 
$request['extra']['tenant_id']. "'",__LINE__,__FILE__);
-                       }
-
-                       if ($request['power_meter'] )
-                       {
-                               
$this->soproject->update_power_meter($request['power_meter'],$request['location_code'],$address);
-                       }
-
-                       if($request['origin'][0]['data'][0]['id'])
-                       {
-                               $this->db->query("INSERT INTO fm_origin 
(origin,origin_id,destination,destination_id,user_id,entry_date) "
-                                       . "VALUES ('"
-                                       . $request['origin'][0]['type']. "','"
-                                       . 
$request['origin'][0]['data'][0]['id']. "',"
-                                       . "'request',"
-                                       . $request['request_id']. ","
-                                       . $this->account . ","
-                                       . time() . ")",__LINE__,__FILE__);
-                       }
-
-
-                       if($this->db->transaction_commit())
-                       {
-                               $this->increment_request_id();
-                               
$this->historylog->add('SO',$request['request_id'],$request['status']);
-                               
$this->historylog->add('TO',$request['request_id'],$request['cat_id']);
-                               
$this->historylog->add('CO',$request['request_id'],$request['coordinator']);
-                               $receipt['message'][] = 
array('msg'=>lang('request %1 has been saved',$request['request_id']));
-                       }
-                       else
-                       {
-                               $receipt['error'][] = 
array('msg'=>lang('request %1 has not been saved',$request['request_id']));
-                       }
-                       return $receipt;
-               }
-
-               function edit($request)
-               {
-                       while (is_array($request['location']) && 
list($input_name,$value) = each($request['location']))
-                       {
-                               $vals[] = "$input_name = '$value'";
-                       }
-
-                       while (is_array($request['extra']) && 
list($input_name,$value) = each($request['extra']))
-                       {
-                               $vals[] = "$input_name = '$value'";
-                       }
-
-                       if($vals)
-                       {
-                               $vals   = "," . implode(",",$vals);
-                       }
-
-                       if($request['street_name'])
-                       {
-                               $address[]= $request['street_name'];
-                               $address[]= $request['street_number'];
-                               $address = $this->db->db_addslashes(implode(" 
", $address));
-                       }
-
-                       if(!$address)
-                       {
-                               $address = 
$this->db->db_addslashes($request['location_name']);
-                       }
-
-
-                       $request['descr'] = 
$this->db->db_addslashes($request['descr']);
-                       $request['name'] = 
$this->db->db_addslashes($request['name']);
-                       $request['title'] = 
$this->db->db_addslashes($request['title']);
-//_debug_array($request);
-
-                       $this->db->transaction_begin();
-
-                       $this->db->query("DELETE FROM fm_request_condition 
WHERE request_id='" . $request['request_id'] . "'",__LINE__,__FILE__);
-                       while (is_array($request['condition']) && 
list($condition_type,$value_type) = each($request['condition']))
-                       {
-                               $this->db->query("INSERT INTO 
fm_request_condition 
(request_id,condition_type,degree,probability,consequence,user_id,entry_date) "
-                                       . "VALUES ('"
-                                       . $request['request_id']. "','"
-                                       . $condition_type . "',"
-                                       . $value_type['degree']. ","
-                                       . $value_type['probability']. ","
-                                       . $value_type['consequence']. ","
-                                       . $this->account . ","
-                                       . time() . ")",__LINE__,__FILE__);
-                       }
-
-                       $this->update_score($request['request_id']);
-
-                       if($request['extra']['contact_phone'] && 
$request['extra']['tenant_id'])
-                       {
-                               $this->db->query("update fm_tenant set 
contact_phone='". $request['extra']['contact_phone']. "' where id='". 
$request['extra']['tenant_id']. "'",__LINE__,__FILE__);
-                       }
-
-                       if ($request['power_meter'] )
-                       {
-                               
$this->soproject->update_power_meter($request['power_meter'],$request['location_code'],$address);
-                       }
-
-                       if($this->db->transaction_commit())
-                       {
-                               $this->db->query("SELECT 
status,category,coordinator FROM fm_request where id='" 
.$request['request_id']."'",__LINE__,__FILE__);
-                               $this->db->next_record();
-
-                               $old_status = $this->db->f('status');
-                               $old_categroy = $this->db->f('category');
-                               $old_coordinator = $this->db->f('coordinator');
-                               if ($old_status != $request['status'])
-                               {
-                                       
$this->historylog->add('S',$request['request_id'],$request['status']);
-                               }
-                               if ($old_categroy != $request['cat_id'])
-                               {
-                                       
$this->historylog->add('T',$request['request_id'],$request['cat_id']);
-                               }
-                               if ($old_coordinator != $request['coordinator'])
-                               {
-                                       
$this->historylog->add('C',$request['request_id'],$request['coordinator']);
-                               }
-
-                               $receipt['message'][] = 
array('msg'=>lang('request %1 has been edited',$request['request_id']));
-                       }
-                       else
-                       {
-                               $receipt['message'][] = 
array('msg'=>lang('request %1 has not been edited',$request['request_id']));
-                       }
-                       return $receipt;
-
-               }
-
-               function delete($request_id )
-               {
-                       $this->db->query("DELETE FROM fm_request WHERE id='" . 
$request_id . "'",__LINE__,__FILE__);
-                       $this->db->query("DELETE FROM fm_request_condition 
WHERE request_id='" . $request_id . "'",__LINE__,__FILE__);
-                       $this->db->query("DELETE FROM fm_request_history  WHERE 
 history_record_id='" . $request_id   . "'",__LINE__,__FILE__);
-                       $this->db->query("DELETE FROM fm_origin WHERE 
destination = 'request' AND destination_id='" . $request_id . 
"'",__LINE__,__FILE__);
-
-               }
-       }
-?>
+<?php
+       /**
+       * phpGroupWare - property: a Facilities Management System.
+       *
+       * @author Sigurd Nes <address@hidden>
+       * @copyright Copyright (C) 2003-2005 Free Software Foundation, Inc. 
http://www.fsf.org/
+       * @license http://www.gnu.org/licenses/gpl.html GNU General Public 
License
+       * @internal Development of this application was funded by 
http://www.bergen.kommune.no/bbb_/ekstern/
+       * @package property
+       * @subpackage project
+       * @version $Id: class.sorequest.inc.php,v 1.13 2006/01/30 22:14:19 
sigurdne Exp $
+       */
+
+       /**
+        * Description
+        * @package property
+        */
+
+       class sorequest
+       {
+               function sorequest()
+               {
+                       $this->currentapp       = 
$GLOBALS['phpgw_info']['flags']['currentapp'];
+                       $this->account          = 
$GLOBALS['phpgw_info']['user']['account_id'];
+                       $this->soproject        = 
CreateObject($this->currentapp.'.soproject');
+                       $this->historylog       = 
CreateObject($this->currentapp.'.historylog','request');
+                       $this->bocommon         = 
CreateObject($this->currentapp.'.bocommon');
+                       $this->db               = $this->bocommon->new_db();
+                       $this->db2              = $this->bocommon->new_db();
+                       $this->join                     = $this->bocommon->join;
+                       $this->like                     = $this->bocommon->like;
+               }
+
+               function select_category_workorder_list()
+               {
+                       $this->db->query("SELECT id, descr FROM 
fm_workorder_category  ORDER BY descr ");
+                       $i = 0;
+                       while ($this->db->next_record())
+                       {
+                               $categories[$i]['id']                           
= $this->db->f('id');
+                               $categories[$i]['name']                         
= stripslashes($this->db->f('descr'));
+                               $i++;
+                       }
+                       return $categories;
+               }
+
+               function read_priority_key()
+               {
+                       $this->db->query("SELECT * FROM 
fm_request_condition_type",__LINE__,__FILE__);
+
+                       while ($this->db->next_record())
+                       {
+                               $priority_key[] = array(
+                                       'id'            => $this->db->f('id'),
+                                       'descr'         => 
$this->db->f('descr'),
+                                       'priority_key'  => 
$this->db->f('priority_key')
+                                       );
+                       }
+
+                       return $priority_key;
+               }
+
+               function update_priority_key($values)
+               {
+
+                       while (is_array($values['priority_key']) && 
list($id,$priority_key) = each($values['priority_key']))
+                       {
+                               $this->db->query("UPDATE 
fm_request_condition_type SET priority_key = $priority_key WHERE id = 
$id",__LINE__,__FILE__);
+                       }
+
+                       $this->update_score();
+
+                       $receipt['message'][] = array('msg'=> lang('priority 
keys has been updated'));
+                       return $receipt;
+               }
+
+
+               function update_score($request_id='')
+               {
+                       if($request_id)
+                       {
+                               $request[] = $request_id;
+                       }
+                       else
+                       {
+                               $this->db->query("SELECT id FROM 
fm_request",__LINE__,__FILE__);
+
+                               while ($this->db->next_record())
+                               {
+                                       $request[] = $this->db->f('id');
+                               }
+                       }
+
+                       while (is_array($request) && list(,$id) = 
each($request))
+                       {
+
+                               
if($GLOBALS['phpgw_info']['server']['db_type']=='pgsql')
+                               {
+                                       $sql = "UPDATE fm_request SET score = 
(SELECT sum(priority_key * ( degree * probability * ( consequence +1 )))  FROM 
fm_request_condition"
+                                        . " $this->join  
fm_request_condition_type ON (fm_request_condition.condition_type = 
fm_request_condition_type.id) WHERE request_id = $id) WHERE fm_request.id = 
$id";
+
+                                       
$this->db->query($sql,__LINE__,__FILE__);
+                               }
+                               else
+                               {
+                                       $sql = "SELECT sum(priority_key * ( 
degree * probability * ( consequence +1 ))) AS score FROM fm_request_condition"
+                                        . " $this->join  
fm_request_condition_type ON (fm_request_condition.condition_type = 
fm_request_condition_type.id) WHERE request_id = $id";
+
+                                       
$this->db->query($sql,__LINE__,__FILE__);
+
+                                       $this->db->next_record();
+                                       $score = $this->db->f('score');
+                                       $this->db->query("UPDATE fm_request SET 
score = $score WHERE id = $id",__LINE__,__FILE__);
+                               }
+                       }
+               }
+
+
+               function read_single_request_category($id='')
+               {
+                       $this->db->query("SELECT descr FROM 
fm_workorder_category where id='$id' ");
+                       $this->db->next_record();
+                       return $this->db->f('descr');
+               }
+
+               function select_status_list()
+               {
+                       $this->db->query("SELECT id, descr FROM 
fm_request_status ORDER BY id ");
+
+                       $i = 0;
+                       while ($this->db->next_record())
+                       {
+                               $status_entries[$i]['id']                       
        = $this->db->f('id');
+                               $status_entries[$i]['name']                     
        = stripslashes($this->db->f('descr'));
+                               $i++;
+                       }
+                       return $status_entries;
+               }
+
+               function select_condition_type_list()
+               {
+                       $this->db->query("SELECT id, descr FROM 
fm_request_condition_type ORDER BY id ");
+
+                       $i = 0;
+                       while ($this->db->next_record())
+                       {
+                               $condition_type_list[$i]['id']          = 
$this->db->f('id');
+                               $condition_type_list[$i]['name']        = 
stripslashes($this->db->f('descr'));
+                               $i++;
+                       }
+                       return $condition_type_list;
+               }
+
+               function 
select_conditions($request_id='',$condition_type_list='')
+               {
+                       for ($i=0;$i<count($condition_type_list);$i++)
+                       {
+                               $this->db->query("SELECT 
degree,probability,consequence FROM fm_request_condition WHERE 
request_id=$request_id AND condition_type =" . $condition_type_list[$i]['id']);
+                               $this->db->next_record();
+                               $conditions[$i]['request_id']           = 
$request_id;
+                               $conditions[$i]['degree']               = 
$this->db->f('degree');
+                               $conditions[$i]['probability']          = 
$this->db->f('probability');
+                               $conditions[$i]['consequence']          = 
$this->db->f('consequence');
+                       }
+
+                       return $conditions;
+               }
+
+
+               function read($data)
+               {
+                       if(is_array($data))
+                       {
+                               if ($data['start'])
+                               {
+                                       $start=$data['start'];
+                               }
+                               else
+                               {
+                                       $start=0;
+                               }
+                               $filter = 
(isset($data['filter'])?$data['filter']:'');
+                               $query = 
(isset($data['query'])?$data['query']:'');
+                               $sort = 
(isset($data['sort'])?$data['sort']:'DESC');
+                               $order = 
(isset($data['order'])?$data['order']:'');
+                               $cat_id = 
(isset($data['cat_id'])?$data['cat_id']:0);
+                               $status_id = 
(isset($data['status_id'])?$data['status_id']:0);
+                               $project_id = 
(isset($data['project_id'])?$data['project_id']:'');
+                       }
+
+                       $entity_table = 'fm_request';
+
+                       $cols .= $entity_table . '.location_code';
+                       $cols_return[] = 'location_code';
+
+                       $cols .= ",$entity_table.id as request_id";
+                       $cols_return[]                          = 'request_id';
+                       $uicols['input_type'][]         = 'text';
+                       $uicols['name'][]                       = 'request_id';
+                       $uicols['descr'][]                      = 
lang('Request');
+                       $uicols['statustext'][]         = lang('Request ID');
+
+                       $cols.= ",$entity_table.entry_date";
+                       $cols_return[]                          = 'entry_date';
+                       $uicols['input_type'][]         = 'text';
+                       $uicols['name'][]                       = 'entry_date';
+                       $uicols['descr'][]                      = lang('entry 
date');
+                       $uicols['statustext'][]         = lang('Request entry 
date');
+
+                       $cols.= ",$entity_table.title as title";
+                       $cols_return[]                          = 'title';
+                       $uicols['input_type'][]         = 'text';
+                       $uicols['name'][]                       = 'title';
+                       $uicols['descr'][]                      = lang('title');
+                       $uicols['statustext'][]         = lang('Request title');
+
+                       $cols.= ",$entity_table.coordinator";
+                       $cols_return[]                          = 'coordinator';
+                       $uicols['input_type'][]         = 'text';
+                       $uicols['name'][]                       = 'coordinator';
+                       $uicols['descr'][]                      = 
lang('Coordinator');
+                       $uicols['statustext'][]         = lang('Project 
coordinator');
+
+                       $cols.= ",$entity_table.score";
+                       $cols_return[]                          = 'score';
+                       $uicols['input_type'][]         = 'text';
+                       $uicols['name'][]                       = 'score';
+                       $uicols['descr'][]                      = lang('score');
+                       $uicols['statustext'][]         = lang('score');
+
+                       $sql    = 
$this->bocommon->generate_sql(array('entity_table'=>$entity_table,'cols'=>$cols,'cols_return'=>$cols_return,
+                                                                               
                                        
'uicols'=>$uicols,'joinmethod'=>$joinmethod,'paranthesis'=>$paranthesis,'query'=>$query));
+
+                       if ($order)
+                       {
+                               $ordermethod = " order by $order $sort";
+                       }
+                       else
+                       {
+                               $ordermethod = ' order by fm_request.id ASC';
+                       }
+
+                       $where = 'WHERE';
+                       if ($cat_id > 0)
+                       {
+                               $filtermethod .= " $where 
fm_request.category='$cat_id' ";
+                               $where = 'AND';
+                       }
+
+                       if ($status_id)
+                       {
+                               $filtermethod .= " $where  
fm_request.status='$status_id' ";
+                               $where = 'AND';
+                       }
+
+                       if ($filter)
+                       {
+                               $filtermethod .= " $where 
fm_request.coordinator='$filter' ";
+                               $where = 'AND';
+                       }
+
+                       if ($project_id)// lookup requests not already 
allocated to projects
+                       {
+                               $filtermethod .= " $where project_id is NULL ";
+                               $where = 'AND';
+                       }
+
+                       if($query)
+                       {
+                               $query = ereg_replace("'",'',$query);
+                               $query = ereg_replace('"','',$query);
+
+                               $querymethod = " $where (fm_request.title 
$this->like '%$query%' or fm_request.address $this->like '%$query%' or 
fm_request.location_code $this->like '%$query%')";
+                       }
+
+                       $sql .= " $filtermethod $querymethod";
+
+                       $this->uicols           = $this->bocommon->uicols;
+                       $cols_return            = $this->bocommon->cols_return;
+                       $type_id                        = 
$this->bocommon->type_id;
+                       $this->cols_extra       = $this->bocommon->cols_extra;
+
+                       $this->db2->query($sql,__LINE__,__FILE__);
+                       $this->total_records = $this->db2->num_rows();
+                       $this->db->limit_query($sql . 
$ordermethod,$start,__LINE__,__FILE__);
+
+
+                       $j=0;
+                       while ($this->db->next_record())
+                       {
+                               for ($i=0;$i<count($cols_return);$i++)
+                               {
+                                       $request_list[$j][$cols_return[$i]] = 
stripslashes($this->db->f($cols_return[$i]));
+                               }
+
+                               $location_code= $this->db->f('location_code');
+                               $location = split('-',$location_code);
+                               for ($m=0;$m<count($location);$m++)
+                               {
+                                       $request_list[$j]['loc' . ($m+1)] = 
$location[$m];
+                                       
$request_list[$j]['query_location']['loc' . ($m+1)]=implode("-", 
array_slice($location, 0, ($m+1)));
+                               }
+
+                               $j++;
+                       }
+//_debug_array($request_list);
+                       return $request_list;
+               }
+
+               function read_single($request_id)
+               {
+                       $sql = "SELECT * from fm_request where 
id='$request_id'";
+
+                       $this->db->query($sql,__LINE__,__FILE__);
+
+                       if ($this->db->next_record())
+                       {
+                               $request['request_id']                          
        = $this->db->f('id');
+                               $request['title']                               
                = $this->db->f('title');
+                               $request['location_code']                       
        = $this->db->f('location_code');
+                               $request['descr']                               
                = $this->db->f('descr');
+                               $request['status']                              
                = $this->db->f('status');
+                               $request['budget']                              
                = (int)$this->db->f('budget');
+                               $request['tenant_id']                           
        = $this->db->f('tenant_id');
+                               $request['owner']                               
                = $this->db->f('owner');
+                               $request['coordinator']                         
        = $this->db->f('coordinator');
+                               $request['access']                              
                = $this->db->f('access');
+                               $request['start_date']                          
        = $this->db->f('start_date');
+                               $request['end_date']                            
        = $this->db->f('end_date');
+                               $request['cat_id']                              
                = $this->db->f('category');
+
+                               $request['branch_id']                           
        = $this->db->f('branch_id');
+                               $request['authorities_demands']                 
= $this->db->f('authorities_demands');
+                               $request['score']                               
                = $this->db->f('score');
+                               $request['p_num']                               
                = $this->db->f('p_num');
+                               $request['p_entity_id']                         
        = $this->db->f('p_entity_id');
+                               $request['p_cat_id']                            
        = $this->db->f('p_cat_id');
+                               $request['contact_phone']                       
        = $this->db->f('contact_phone');
+
+                               $request['power_meter'] = 
$this->soproject->get_power_meter($this->db->f('location_code'));
+                       }
+
+                       $sql = "SELECT * FROM fm_origin WHERE destination = 
'request' AND destination_id='$request_id' ORDER by origin DESC  ";
+
+                       $this->db->query($sql,__LINE__,__FILE__);
+
+                       $i=-1;
+                       while ($this->db->next_record())
+                       {
+                               if($last_type != $this->db->f('origin'))
+                               {
+                                       $i++;
+                               }
+                               $request['origin'][$i]['type'] = 
$this->db->f('origin');
+                               $request['origin'][$i]['link'] = 
$this->bocommon->get_origin_link($this->db->f('origin'));
+                               $request['origin'][$i]['data'][]= array(
+                                       'id'=> $this->db->f('origin_id'),
+                                       'type'=> $this->db->f('origin')
+                                       );
+
+                               $last_type=$this->db->f('origin');
+                       }
+
+                       $sql = "SELECT * FROM fm_origin WHERE origin = 
'request' AND origin_id='$request_id' ORDER by destination DESC  ";
+
+                       $this->db->query($sql,__LINE__,__FILE__);
+
+                       while ($this->db->next_record())
+                       {
+                               if($last_type != $this->db->f('destination'))
+                               {
+                                       $i++;
+                               }
+                               $request['origin'][$i]['type'] = 
$this->db->f('destination');
+                               $request['origin'][$i]['link'] = 
$this->bocommon->get_origin_link($this->db->f('destination'));
+                               $request['origin'][$i]['data'][]= array(
+                                       'id'=> $this->db->f('destination_id'),
+                                       'type'=> $this->db->f('destination')
+                                       );
+
+                               $last_type=$this->db->f('destination');
+                       }
+
+                       return $request;
+               }
+
+               function request_workorder_data($request_id = '')
+               {
+                       $this->db->query("select budget, id as workorder_id, 
vendor_id from fm_workorder where request_id='$request_id'");
+                       while ($this->db->next_record())
+                       {
+                               $budget[] = array(
+                                       'workorder_id'  => 
$this->db->f('workorder_id'),
+                                       'budget'                => 
sprintf("%01.2f",$this->db->f('budget')),
+                                       'vendor_id'     => 
$this->db->f('vendor_id')
+                                       );
+                       }
+                       return $budget;
+               }
+
+
+               function increment_request_id()
+               {
+                       $this->db->query("update fm_idgenerator set value = 
value + 1 where name = 'request'");
+               }
+
+               function next_id()
+               {
+                       $this->db->query("select value from fm_idgenerator 
where name = 'request'");
+                       $this->db->next_record();
+                       $id = $this->db->f('value')+1;
+                       return $id;
+               }
+
+               function add($request)
+               {
+//_debug_array($request);
+                       while (is_array($request['location']) && 
list($input_name,$value) = each($request['location']))
+                       {
+                               if($value)
+                               {
+                                       $cols[] = $input_name;
+                                       $vals[] = $value;
+                               }
+                       }
+
+                       while (is_array($request['extra']) && 
list($input_name,$value) = each($request['extra']))
+                       {
+                               if($value)
+                               {
+                                       $cols[] = $input_name;
+                                       $vals[] = $value;
+                               }
+                       }
+
+                       if($cols)
+                       {
+                               $cols   = "," . implode(",", $cols);
+                               $vals   = ",'" . implode("','", $vals) . "'";
+                       }
+
+                       if($request['street_name'])
+                       {
+                               $address[]= $request['street_name'];
+                               $address[]= $request['street_number'];
+                               $address        = 
$this->db->db_addslashes(implode(" ", $address));
+                       }
+
+                       if(!$address)
+                       {
+                               $address = 
$this->db->db_addslashes($request['location_name']);
+                       }
+
+                       $request['descr'] = 
$this->db->db_addslashes($request['descr']);
+                       $request['name'] = 
$this->db->db_addslashes($request['name']);
+                       $request['title'] = 
$this->db->db_addslashes($request['title']);
+
+                       $values= array(
+                               $request['request_id'],
+                               $request['title'],
+                               $this->account,
+                               $request['cat_id'],
+                               $request['descr'],
+                               $request['location_code'],
+                               $address,
+                               time(),
+                               $request['budget'],
+                               $request['status'],
+                               $request['branch_id'],
+                               $request['coordinator'],
+                               $request['authorities_demands']);
+
+                       $values = $this->bocommon->validate_db_insert($values);
+
+                       $this->db->transaction_begin();
+
+                       $this->db->query("insert into fm_request 
(id,title,owner,category,descr,location_code,"
+                               . 
"address,entry_date,budget,status,branch_id,coordinator,"
+                               . "authorities_demands  $cols) "
+                               . "VALUES ($values $vals )",__LINE__,__FILE__);
+
+                       while (is_array($request['condition']) && 
list($condition_type,$value_type) = each($request['condition']))
+                       {
+                               $this->db->query("INSERT INTO 
fm_request_condition 
(request_id,condition_type,degree,probability,consequence,user_id,entry_date) "
+                                       . "VALUES ('"
+                                       . $request['request_id']. "','"
+                                       . $condition_type . "',"
+                                       . $value_type['degree']. ","
+                                       . $value_type['probability']. ","
+                                       . $value_type['consequence']. ","
+                                       . $this->account . ","
+                                       . time() . ")",__LINE__,__FILE__);
+                       }
+
+                       $this->update_score($request['request_id']);
+
+
+                       if($request['extra']['contact_phone'] && 
$request['extra']['tenant_id'])
+                       {
+                               $this->db->query("update fm_tenant set 
contact_phone='". $request['extra']['contact_phone']. "' where id='". 
$request['extra']['tenant_id']. "'",__LINE__,__FILE__);
+                       }
+
+                       if ($request['power_meter'] )
+                       {
+                               
$this->soproject->update_power_meter($request['power_meter'],$request['location_code'],$address);
+                       }
+
+                       if($request['origin'][0]['data'][0]['id'])
+                       {
+                               $this->db->query("INSERT INTO fm_origin 
(origin,origin_id,destination,destination_id,user_id,entry_date) "
+                                       . "VALUES ('"
+                                       . $request['origin'][0]['type']. "','"
+                                       . 
$request['origin'][0]['data'][0]['id']. "',"
+                                       . "'request',"
+                                       . $request['request_id']. ","
+                                       . $this->account . ","
+                                       . time() . ")",__LINE__,__FILE__);
+                       }
+
+
+                       if($this->db->transaction_commit())
+                       {
+                               $this->increment_request_id();
+                               
$this->historylog->add('SO',$request['request_id'],$request['status']);
+                               
$this->historylog->add('TO',$request['request_id'],$request['cat_id']);
+                               
$this->historylog->add('CO',$request['request_id'],$request['coordinator']);
+                               $receipt['message'][] = 
array('msg'=>lang('request %1 has been saved',$request['request_id']));
+                       }
+                       else
+                       {
+                               $receipt['error'][] = 
array('msg'=>lang('request %1 has not been saved',$request['request_id']));
+                       }
+                       return $receipt;
+               }
+
+               function edit($request)
+               {
+                       while (is_array($request['location']) && 
list($input_name,$value) = each($request['location']))
+                       {
+                               $vals[] = "$input_name = '$value'";
+                       }
+
+                       while (is_array($request['extra']) && 
list($input_name,$value) = each($request['extra']))
+                       {
+                               $vals[] = "$input_name = '$value'";
+                       }
+
+                       if($vals)
+                       {
+                               $vals   = "," . implode(",",$vals);
+                       }
+
+                       if($request['street_name'])
+                       {
+                               $address[]= $request['street_name'];
+                               $address[]= $request['street_number'];
+                               $address = $this->db->db_addslashes(implode(" 
", $address));
+                       }
+
+                       if(!$address)
+                       {
+                               $address = 
$this->db->db_addslashes($request['location_name']);
+                       }
+
+
+                       $request['descr'] = 
$this->db->db_addslashes($request['descr']);
+                       $request['name'] = 
$this->db->db_addslashes($request['name']);
+                       $request['title'] = 
$this->db->db_addslashes($request['title']);
+//_debug_array($request);
+
+                       $this->db->transaction_begin();
+
+                       $this->db->query("DELETE FROM fm_request_condition 
WHERE request_id='" . $request['request_id'] . "'",__LINE__,__FILE__);
+                       while (is_array($request['condition']) && 
list($condition_type,$value_type) = each($request['condition']))
+                       {
+                               $this->db->query("INSERT INTO 
fm_request_condition 
(request_id,condition_type,degree,probability,consequence,user_id,entry_date) "
+                                       . "VALUES ('"
+                                       . $request['request_id']. "','"
+                                       . $condition_type . "',"
+                                       . $value_type['degree']. ","
+                                       . $value_type['probability']. ","
+                                       . $value_type['consequence']. ","
+                                       . $this->account . ","
+                                       . time() . ")",__LINE__,__FILE__);
+                       }
+
+                       $this->update_score($request['request_id']);
+
+                       if($request['extra']['contact_phone'] && 
$request['extra']['tenant_id'])
+                       {
+                               $this->db->query("update fm_tenant set 
contact_phone='". $request['extra']['contact_phone']. "' where id='". 
$request['extra']['tenant_id']. "'",__LINE__,__FILE__);
+                       }
+
+                       if ($request['power_meter'] )
+                       {
+                               
$this->soproject->update_power_meter($request['power_meter'],$request['location_code'],$address);
+                       }
+
+                       if($this->db->transaction_commit())
+                       {
+                               $this->db->query("SELECT 
status,category,coordinator FROM fm_request where id='" 
.$request['request_id']."'",__LINE__,__FILE__);
+                               $this->db->next_record();
+
+                               $old_status = $this->db->f('status');
+                               $old_categroy = $this->db->f('category');
+                               $old_coordinator = $this->db->f('coordinator');
+                               if ($old_status != $request['status'])
+                               {
+                                       
$this->historylog->add('S',$request['request_id'],$request['status']);
+                               }
+                               if ($old_categroy != $request['cat_id'])
+                               {
+                                       
$this->historylog->add('T',$request['request_id'],$request['cat_id']);
+                               }
+                               if ($old_coordinator != $request['coordinator'])
+                               {
+                                       
$this->historylog->add('C',$request['request_id'],$request['coordinator']);
+                               }
+
+                               $receipt['message'][] = 
array('msg'=>lang('request %1 has been edited',$request['request_id']));
+                       }
+                       else
+                       {
+                               $receipt['message'][] = 
array('msg'=>lang('request %1 has not been edited',$request['request_id']));
+                       }
+                       return $receipt;
+
+               }
+
+               function delete($request_id )
+               {
+                       $this->db->query("DELETE FROM fm_request WHERE id='" . 
$request_id . "'",__LINE__,__FILE__);
+                       $this->db->query("DELETE FROM fm_request_condition 
WHERE request_id='" . $request_id . "'",__LINE__,__FILE__);
+                       $this->db->query("DELETE FROM fm_request_history  WHERE 
 history_record_id='" . $request_id   . "'",__LINE__,__FILE__);
+                       $this->db->query("DELETE FROM fm_origin WHERE 
destination = 'request' AND destination_id='" . $request_id . 
"'",__LINE__,__FILE__);
+
+               }
+       }
+?>
Index: property/inc/class.sos_agreement.inc.php
diff -u property/inc/class.sos_agreement.inc.php:1.15 
property/inc/class.sos_agreement.inc.php:1.16
--- property/inc/class.sos_agreement.inc.php:1.15       Fri Jan 27 14:05:43 2006
+++ property/inc/class.sos_agreement.inc.php    Mon Jan 30 22:14:19 2006
@@ -1,1443 +1,1442 @@
-<?php
-       /**
-       * phpGroupWare - property: a Facilities Management System.
-       *
-       * @author Sigurd Nes <address@hidden>
-       * @copyright Copyright (C) 2003-2005 Free Software Foundation, Inc. 
http://www.fsf.org/
-       * @license http://www.gnu.org/licenses/gpl.html GNU General Public 
License
-       * @internal Development of this application was funded by 
http://www.bergen.kommune.no/bbb_/ekstern/
-       * @package property
-       * @subpackage agreement
-       * @version $Id: class.sos_agreement.inc.php,v 1.15 2006/01/27 14:05:43 
sigurdne Exp $
-       */
-
-       /**
-        * Description
-        * @package property
-        */
-
-       class sos_agreement
-       {
-               var $role;
-
-               function sos_agreement()
-               {
-                       $this->currentapp       = 
$GLOBALS['phpgw_info']['flags']['currentapp'];
-                       $this->account  = 
$GLOBALS['phpgw_info']['user']['account_id'];
-                       $this->bocommon         = 
CreateObject($this->currentapp.'.bocommon');
+<?php
+       /**
+       * phpGroupWare - property: a Facilities Management System.
+       *
+       * @author Sigurd Nes <address@hidden>
+       * @copyright Copyright (C) 2003-2005 Free Software Foundation, Inc. 
http://www.fsf.org/
+       * @license http://www.gnu.org/licenses/gpl.html GNU General Public 
License
+       * @internal Development of this application was funded by 
http://www.bergen.kommune.no/bbb_/ekstern/
+       * @package property
+       * @subpackage agreement
+       * @version $Id: class.sos_agreement.inc.php,v 1.16 2006/01/30 22:14:19 
sigurdne Exp $
+       */
+
+       /**
+        * Description
+        * @package property
+        */
+
+       class sos_agreement
+       {
+               var $role;
+
+               function sos_agreement()
+               {
+                       $this->currentapp       = 
$GLOBALS['phpgw_info']['flags']['currentapp'];
+                       $this->account  = 
$GLOBALS['phpgw_info']['user']['account_id'];
+                       $this->bocommon         = 
CreateObject($this->currentapp.'.bocommon');
                        $this->db               = $this->bocommon->new_db();
-                       $this->db2              = $this->bocommon->new_db();
-                       $this->join                     = $this->bocommon->join;
-                       $this->left_join        = $this->bocommon->left_join;
-                       $this->like                     = $this->bocommon->like;
-               }
-
-               function select_category_list()
-               {
-                       $table = 'fm_s_agreement' . '_category';
-                       $this->db->query("SELECT id,descr FROM $table  ORDER BY 
descr ");
-
-                       $i = 0;
-                       while ($this->db->next_record())
-                       {
-                               $categories[$i]['id']                           
= $this->db->f('id');
-                               $categories[$i]['name']                         
= stripslashes($this->db->f('descr'));
-                               $i++;
-                       }
-                       return $categories;
-               }
-
-               function select_vendor_list()
-               {
-                       $table = 'fm_s_agreement';
-                       $this->db->query("SELECT vendor_id,org_name FROM $table 
$this->join fm_vendor on fm_s_agreement.vendor_id=fm_vendor.id GROUP BY 
org_name,vendor_id ");
-
-                       $i = 0;
-                       while ($this->db->next_record())
-                       {
-                               $vendor[$i]['id']                               
= $this->db->f('vendor_id');
-                               $vendor[$i]['name']                             
= stripslashes($this->db->f('org_name'));
-                               $i++;
-                       }
-                       return $vendor;
-               }
-
-               function read($data)
-               {
-                       if(is_array($data))
-                       {
-                               $start                  = 
(isset($data['start'])?$data['start']:0);
-                               $filter                 = 
(isset($data['filter'])?$data['filter']:'none');
-                               $query                  = 
(isset($data['query'])?$data['query']:'');
-                               $sort                   = 
(isset($data['sort'])?$data['sort']:'DESC');
-                               $order                  = 
(isset($data['order'])?$data['order']:'');
-                               $cat_id                 = 
(isset($data['cat_id'])?$data['cat_id']:'');
-                               $vendor_id              = 
(isset($data['vendor_id'])?$data['vendor_id']:'');
-                               $allrows                = 
(isset($data['allrows'])?$data['allrows']:'');
-                               $member_id              = 
(isset($data['member_id'])?$data['member_id']:0);
-                               $s_agreement_id = 
(isset($data['s_agreement_id'])?$data['s_agreement_id']:'');
-                               $detail                 = 
(isset($data['detail'])?$data['detail']:'');
-                       }
-
-                       $choice_table = 'fm_s_agreement_choice';
-                       $attribute_table = 'fm_s_agreement_attribute';
-
-                       if(!$detail)
-                       {
-                               $entity_table = 'fm_s_agreement';
-                               $category_table = 'fm_s_agreement_category';
-                               $attribute_filter = " AND attrib_detail = 1";
-
-                               $paranthesis .='(';
-                               $joinmethod .= " $this->join $category_table ON 
( $entity_table.category =$category_table.id))";
-                               $paranthesis .='(';
-                               $joinmethod .= " $this->join fm_vendor ON ( 
$entity_table.vendor_id =fm_vendor.id))";
-
-                               $cols = $entity_table . 
".*,$category_table.descr as category, org_name";
-
-                               $cols_return[]                          = 'id';
-                               $uicols['input_type'][]         = 'text';
-                               $uicols['name'][]                       = 'id';
-                               $uicols['descr'][]                      = 
lang('ID');
-                               $uicols['statustext'][]         = lang('ID');
-
-                               $cols_return[]                          = 
'name';
-                               $uicols['input_type'][]         = 'text';
-                               $uicols['name'][]                       = 
'name';
-                               $uicols['descr'][]                      = 
lang('name');
-                               $uicols['statustext'][]         = lang('name');
-
-                               $cols_return[]                          = 
'org_name';
-                               $uicols['input_type'][]         = 'text';
-                               $uicols['name'][]                       = 
'org_name';
-                               $uicols['descr'][]                      = 
lang('vendor');
-                               $uicols['statustext'][]         = 
lang('vendor');
-
-                               $cols_return[]                          = 
'category';
-                               $uicols['input_type'][]         = 'text';
-                               $uicols['name'][]                       = 
'category';
-                               $uicols['descr'][]                      = 
lang('category');
-                               $uicols['statustext'][]         = 
lang('category');
-
-                               $cols_return[]                          = 
'start_date';
-                               $uicols['input_type'][]         = 'text';
-                               $uicols['name'][]                       = 
'start_date';
-                               $uicols['descr'][]                      = 
lang('start');
-                               $uicols['statustext'][]         = lang('start 
date');
-
-                               $cols_return[]                          = 
'end_date';
-                               $uicols['input_type'][]         = 'text';
-                               $uicols['name'][]                       = 
'end_date';
-                               $uicols['descr'][]                      = 
lang('end');
-                               $uicols['statustext'][]         = lang('end 
date');
-
-                       }
-                       else
-                       {
-                               $allrows=True;
-                               $entity_table = 'fm_s_agreement_detail';
-                               $attribute_filter = " AND attrib_detail = 2";
-
-                               $paranthesis .='(';
-                               $joinmethod .= " $this->join  
fm_s_agreement_pricing ON ( $entity_table.agreement_id 
=fm_s_agreement_pricing.agreement_id AND $entity_table.id 
=fm_s_agreement_pricing.item_id))";
-
-
-
-                               $cols = "$entity_table.*, 
fm_s_agreement_pricing.cost,fm_s_agreement_pricing.id as 
index_count,fm_s_agreement_pricing.index_date,fm_s_agreement_pricing.item_id,fm_s_agreement_pricing.this_index";
-
-                               $cols_return[]                          = 
'agreement_id';
-                               $uicols['input_type'][]         = 'hidden';
-                               $uicols['name'][]                       = 
'agreement_id';
-                               $uicols['descr'][]                      = 
lang('agreement_id');
-                               $uicols['statustext'][]         = 
lang('agreement_id');
-
-                               $cols_return[]                          = 
'item_id';
-                               $uicols['input_type'][]         = 'text';
-                               $uicols['name'][]                       = 
'item_id';
-                               $uicols['descr'][]                      = 
lang('ID');
-                               $uicols['statustext'][]         = lang('ID');
-
-                               $cols_return[]                          = 'id';
-                               $uicols['input_type'][]         = 'hidden';
-                               $uicols['name'][]                       = 'id';
-                               $uicols['descr'][]                      = False;
-                               $uicols['statustext'][]         = False;
-
-                               $cols_return[]                          = 
'location_code';
-                               $uicols['input_type'][]         = 'text';
-                               $uicols['name'][]                       = 
'location_code';
-                               $uicols['descr'][]                      = 
lang('location');
-                               $uicols['statustext'][]         = 
lang('location');
-
-                               $cols_return[]                          = 
'address';
-                               $uicols['input_type'][]         = 'text';
-                               $uicols['name'][]                       = 
'address';
-                               $uicols['descr'][]                      = 
lang('address');
-                               $uicols['statustext'][]         = 
lang('address');
-
-                               $cols_return[]                          = 
'p_num';
-                               $uicols['input_type'][]         = 'text';
-                               $uicols['name'][]                       = 
'p_num';
-                               $uicols['descr'][]                      = 
lang('entity num');
-                               $uicols['statustext'][]         = lang('entity 
num');
-
-                               $cols_return[]                          = 
'cost';
-                               $uicols['input_type'][]         = 'text';
-                               $uicols['name'][]                       = 
'cost';
-                               $uicols['descr'][]                      = 
lang('cost');
-                               $uicols['statustext'][]         = lang('cost');
-
-                               $cols_return[]                          = 
'this_index';
-                               $uicols['input_type'][]         = 'text';
-                               $uicols['name'][]                       = 
'this_index';
-                               $uicols['descr'][]                      = 
lang('index');
-                               $uicols['statustext'][]         = lang('index');
-
-                               $cols_return[]                          = 
'index_count';
-                               $uicols['input_type'][]         = 'text';
-                               $uicols['name'][]                       = 
'index_count';
-                               $uicols['descr'][]                      = 
lang('index_count');
-                               $uicols['statustext'][]         = 
lang('index_count');
-
-                               $cols_return[]                          = 
'index_date';
-                               $uicols['input_type'][]         = 'text';
-                               $uicols['name'][]                       = 
'index_date';
-                               $uicols['descr'][]                      = 
lang('date');
-                               $uicols['statustext'][]         = lang('date');
-                       }
-
-                       $from .= " FROM $paranthesis $entity_table ";
-
-                       $sql = "SELECT $cols $from $joinmethod";
-
-                       $i      = count($uicols['name']);
-
-                       $this->db->query("SELECT * FROM $attribute_table WHERE 
list=1 $attribute_filter ");
-                       while ($this->db->next_record())
-                       {
-                               $uicols['input_type'][]         = 'text';
-                               $uicols['name'][]                       = 
$this->db->f('column_name');
-                               $uicols['descr'][]                      = 
$this->db->f('input_text');
-                               $uicols['statustext'][]         = 
$this->db->f('statustext');
-                               $uicols['datatype'][$i]         = 
$this->db->f('datatype');
-                               $cols_return_extra[]= array(
-                                       'name'  => $this->db->f('column_name'),
-                                       'datatype'      => 
$this->db->f('datatype'),
-                                       'attrib_id'     => $this->db->f('id')
-                               );
-
-                               $i++;
-                       }
-
-                       
$user_columns=$GLOBALS['phpgw_info']['user']['preferences'][$this->currentapp]['s_agreement_columns'
 . !!$s_agreement_id];
-
-//_debug_array($user_columns);
-
-                       if (isset($user_columns) AND is_array($user_columns) 
AND $user_columns[0])
-                       {
-                               foreach($user_columns as $column_id)
-                               {
-                                       $this->db->query("SELECT * FROM 
$attribute_table WHERE id= $column_id");
-
-                                       $this->db->next_record();
-                                       $uicols['input_type'][]         = 
'text';
-                                       $uicols['name'][]                       
= $this->db->f('column_name');
-                                       $uicols['descr'][]                      
= $this->db->f('input_text');
-                                       $uicols['statustext'][]         = 
$this->db->f('statustext');
-                                       $uicols['datatype'][$i]         = 
$this->db->f('datatype');
-                                       $cols_return_extra[]= array(
-                                               'name'  => 
$this->db->f('column_name'),
-                                               'datatype'      => 
$this->db->f('datatype'),
-                                               'attrib_id'     => 
$this->db->f('id')
-                                       );
-                                       $i++;
-                               }
-                       }
-
-                       $this->uicols   = $uicols;
-
-//_debug_array($cols_return_extra);
-                       if ($order)
-                       {
-                               if ($order=='id')
-                               {
-                                       $ordermethod = " order by 
$entity_table.$order $sort";
-                               }
-                               else
-                               {
-                                       $ordermethod = " order by $order $sort";
-                               }
-                       }
-                       else
-                       {
-                               $ordermethod = " order by $entity_table.id 
DESC";
-                       }
-
-                       $where= 'WHERE';
-
-/*                     if ($filter=='all')
-                       {
-                               if (is_array($grants))
-                               {
-                                       while (list($user) = each($grants))
-                                       {
-                                               $public_user_list[] = $user;
-                                       }
-                                       reset($public_user_list);
-                                       $filtermethod .= " $where ( 
$entity_table.user_id IN(" . implode(',',$public_user_list) . "))";
-
-                                       $where= 'AND';
-                               }
-
-                       }
-                       else
-                       {
-                               $filtermethod = " $where 
$entity_table.user_id=$filter ";
-                               $where= 'AND';
-                       }
-*/
-
-                       if ($s_agreement_id)
-                       {
-                               $filtermethod .= " $where 
$entity_table.agreement_id=$s_agreement_id AND current_index = 1";
-                               $where= 'AND';
-                       }
-
-                       if ($cat_id)
-                       {
-                               $filtermethod .= " $where 
$entity_table.category='$cat_id' ";
-                               $where= 'AND';
-                       }
-
-                       if ($vendor_id)
-                       {
-                               $filtermethod .= " $where 
$entity_table.vendor_id='$vendor_id' ";
-                               $where= 'AND';
-                       }
-
-                       if ($member_id>0)
-                       {
-                               $filtermethod .= " $where 
$entity_table.member_of $this->like '%,$member_id,%' ";
-                               $where= 'AND';
-                       }
-
-                       if ($status)
-                       {
-                               $filtermethod .= " $where 
$entity_table.status='$status' ";
-                               $where= 'AND';
-                       }
-
-
-                       if($query)
-                       {
-                               $query = ereg_replace("'",'',$query);
-                               $query = ereg_replace('"','',$query);
-
-                               $this->db->query("SELECT * FROM 
$attribute_table where search='1'");
-
-                               while ($this->db->next_record())
-                               {
-                                       if($this->db->f('datatype')=='V' || 
$this->db->f('datatype')=='email' || $this->db->f('datatype')=='CH')
-                                       {
-                                               $querymethod[]= 
"$entity_table." . $this->db->f('column_name') . " $this->like '%$query%'";
-                                       }
-                                       else
-                                       {
-                                               $querymethod[]= 
"$entity_table." . $this->db->f('column_name') . " = '$query'";
-                                       }
-                               }
-
-                               if (isset($querymethod) AND 
is_array($querymethod))
-                               {
-                                       $querymethod = " $where (" . implode (' 
OR ',$querymethod) . ')';
-                                       $where = 'AND';
-                               }
-                       }
-
-                       $sql .= " $filtermethod $querymethod";
-//echo $sql;
-
-                       $this->db2->query($sql,__LINE__,__FILE__);
-                       $this->total_records = $this->db2->num_rows();
-                       if(!$allrows)
-                       {
-                               $this->db->limit_query($sql . 
$ordermethod,$start,__LINE__,__FILE__);
-                       }
-                       else
-                       {
-                               $this->db->query($sql . 
$ordermethod,__LINE__,__FILE__);
-                       }
-
-                       $j=0;
-                       $n=count($cols_return);
-//_debug_array($cols_return);
-
-                       $contacts                       = 
CreateObject('phpgwapi.contacts');
-
-                       while ($this->db->next_record())
-                       {
-                               for ($i=0;$i<$n;$i++)
-                               {
-                                       $s_agreement_list[$j][$cols_return[$i]] 
= stripslashes($this->db->f($cols_return[$i]));
-                                       $s_agreement_list[$j]['grants'] = 
(int)$grants[$this->db->f('user_id')];
-                               }
-
-                               for ($i=0;$i<count($cols_return_extra);$i++)
-                               {
-                                       $value='';
-                                       
$value=$this->db->f($cols_return_extra[$i]['name']);
-
-                                       
if(($cols_return_extra[$i]['datatype']=='R' || 
$cols_return_extra[$i]['datatype']=='LB') && $value):
-                                       {
-                                               $sql="SELECT value FROM 
$choice_table where attrib_id=" .$cols_return_extra[$i]['attrib_id']. "  AND 
id=" . $value . $attribute_filter;
-                                               $this->db2->query($sql);
-                                               $this->db2->next_record();
-                                               
$s_agreement_list[$j][$cols_return_extra[$i]['name']] = $this->db2->f('value');
-                                       }
-                                       
elseif($cols_return_extra[$i]['datatype']=='AB' && $value):
-                                       {
-                                               $contact_data   = 
$contacts->read_single_entry($value,array('n_given'=>'n_given','n_family'=>'n_family','email'=>'email'));
-                                               
$s_agreement_list[$j][$cols_return_extra[$i]['name']]   = 
$contact_data[0]['n_family'] . ', ' . $contact_data[0]['n_given'];
-
-                                       }
-                                       
elseif($cols_return_extra[$i]['datatype']=='VENDOR' && $value):
-                                       {
-                                               $sql="SELECT org_name FROM 
fm_vendor where id=$value";
-                                               $this->db2->query($sql);
-                                               $this->db2->next_record();
-                                               
$s_agreement_list[$j][$cols_return_extra[$i]['name']] = 
$this->db2->f('org_name');
-                                       }
-                                       
elseif($cols_return_extra[$i]['datatype']=='CH' && $value):
-                                       {
-                                               $ch= unserialize($value);
-
-                                               if (isset($ch) AND 
is_array($ch))
-                                               {
-                                                       for 
($k=0;$k<count($ch);$k++)
-                                                       {
-                                                               $sql="SELECT 
value FROM $choice_table where attrib_id=" 
.$cols_return_extra[$i]['attrib_id']. "  AND id=" . $ch[$k] . $attribute_filter;
-                                                               
$this->db2->query($sql);
-                                                               while 
($this->db2->next_record())
-                                                               {
-                                                                       
$ch_value[]=$this->db2->f('value');
-                                                               }
-                                                       }
-                                                       
$s_agreement_list[$j][$cols_return_extra[$i]['name']] = @implode(",", 
$ch_value);
-                                                       unset($ch_value);
-                                               }
-                                       }
-                                       
elseif($cols_return_extra[$i]['datatype']=='D' && $value):
-                                       {
-//html_print_r($value);
-
-                                               
$s_agreement_list[$j][$cols_return_extra[$i]['name']]=date($GLOBALS['phpgw_info']['user']['preferences']['common']['dateformat'],strtotime($value));
-                                       }
-                                       else:
-                                       {
-                                               
$s_agreement_list[$j][$cols_return_extra[$i]['name']]=$value;
-                                       }
-                                       endif;
-                               }
-
-                               $j++;
-                       }
-//html_print_r($s_agreement_list);
-                       return $s_agreement_list;
-               }
-
-               function read_prizing($data)
-               {
-                       if(is_array($data))
-                       {
-                               $s_agreement_id = 
(isset($data['s_agreement_id'])?$data['s_agreement_id']:0);
-                               $item_id        = 
(isset($data['item_id'])?$data['item_id']:0);
-                       }
-
-                       $entity_table = 'fm_s_agreement_pricing';
-
-                       $cols = 
"fm_s_agreement_pricing.cost,fm_s_agreement_pricing.id as 
index_count,fm_s_agreement_pricing.index_date,fm_s_agreement_pricing.item_id,fm_s_agreement_pricing.this_index";
-
-                       $cols_return[]                  = 'item_id';
-                       $uicols['input_type'][]         = 'text';
-                       $uicols['name'][]                       = 'item_id';
-                       $uicols['descr'][]                      = lang('ID');
-                       $uicols['statustext'][]         = lang('ID');
-
-                       $cols_return[]                  = 'id';
-                       $uicols['input_type'][]         = 'hidden';
-                       $uicols['name'][]                       = 'id';
-                       $uicols['descr'][]                      = False;
-                       $uicols['statustext'][]         = False;
-
-                       $cols_return[]                  = 'cost';
-                       $uicols['input_type'][]         = 'text';
-                       $uicols['name'][]                       = 'cost';
-                       $uicols['descr'][]                      = lang('cost');
-                       $uicols['statustext'][]         = lang('cost');
-
-                       $cols_return[]                  = 'this_index';
-                       $uicols['input_type'][]         = 'text';
-                       $uicols['name'][]                       = 'this_index';
-                       $uicols['descr'][]                      = lang('index');
-                       $uicols['statustext'][]         = lang('index');
-
-                       $cols_return[]                  = 'index_count';
-                       $uicols['input_type'][]         = 'text';
-                       $uicols['name'][]                       = 'index_count';
-                       $uicols['descr'][]                      = 
lang('index_count');
-                       $uicols['statustext'][]         = lang('index_count');
-
-                       $cols_return[]                  = 'index_date';
-                       $uicols['input_type'][]         = 'text';
-                       $uicols['name'][]                       = 'index_date';
-                       $uicols['descr'][]                      = lang('date');
-                       $uicols['statustext'][]         = lang('date');
-
-                       $from .= " FROM $entity_table ";
-
-                       $sql = "SELECT $cols $from $joinmethod";
-
-
-                       $this->uicols   = $uicols;
-
-                       $ordermethod = " order by $entity_table.id ASC";
-
-                       $where= 'WHERE';
-
-
-                       if ($s_agreement_id)
-                       {
-                               $filtermethod .= " $where 
$entity_table.agreement_id=$s_agreement_id AND item_id=$item_id";
-                               $where= 'AND';
-                       }
-
-
-                       $sql .= " $filtermethod";
-//echo $sql;
-
-                       $this->db2->query($sql,__LINE__,__FILE__);
-                       $this->total_records = $this->db2->num_rows();
-                       if(!$allrows)
-                       {
-                               $this->db->limit_query($sql . 
$ordermethod,$start,__LINE__,__FILE__);
-                       }
-                       else
-                       {
-                               $this->db->query($sql . 
$ordermethod,__LINE__,__FILE__);
-                       }
-
-                       $j=0;
-                       $n=count($cols_return);
-//_debug_array($cols_return);
-                       while ($this->db->next_record())
-                       {
-                               for ($i=0;$i<$n;$i++)
-                               {
-                                       $s_agreement_list[$j][$cols_return[$i]] 
= $this->db->f($cols_return[$i]);
-                                       $s_agreement_list[$j]['agreement_id'] = 
$s_agreement_id;
-                               }
-                               $j++;
-                       }
-//_debug_array($s_agreement_list);
-                       return $s_agreement_list;
-               }
-
-
-               function read_single($data)
-               {
-                       $attribute_table = 'fm_s_agreement_attribute';
-                       $table = 'fm_s_agreement';
-
-                       $s_agreement_id =$data['s_agreement_id'];
-
-                       $this->db->query("SELECT * FROM $attribute_table WHERE 
attrib_detail = 1 ORDER BY attrib_sort");
-
-                       while ($this->db->next_record())
-                       {
-                               $s_agreement['attributes'][] = array
-                               (
-                                       'attrib_id'             => 
$this->db->f('id'),
-                                       'name'                  => 
$this->db->f('column_name'),
-                                       'input_text'    => 
stripslashes($this->db->f('input_text')),
-                                       'statustext'    => 
stripslashes($this->db->f('statustext')),
-                                       'datatype'              => 
$this->db->f('datatype')
-                               );
-                       }
-
-                       if($s_agreement_id)
-                       {
-                               $this->db->query("SELECT * from $table where 
id='$s_agreement_id'");
-
-                               if($this->db->next_record())
-                               {
-                                       $s_agreement['id']                      
= (int)$this->db->f('id');
-                                       $s_agreement['entry_date']              
= $this->db->f('entry_date');
-                                       $s_agreement['cat_id']                  
= $this->db->f('category');
-                                       $s_agreement['member_of']               
= explode(',',$this->db->f('member_of'));
-                                       $s_agreement['cat_id']                  
= $this->db->f('category');
-                                       $s_agreement['start_date']              
= $this->db->f('start_date');
-                                       $s_agreement['end_date']                
= $this->db->f('end_date');
-                                       $s_agreement['termination_date']= 
$this->db->f('termination_date');
-                                       $s_agreement['vendor_id']               
= $this->db->f('vendor_id');
-                                       $s_agreement['b_account_id']    = 
$this->db->f('account_id');
-                                       $s_agreement['name']                    
= stripslashes($this->db->f('name'));
-                                       $s_agreement['descr']                   
= stripslashes($this->db->f('descr'));
-                                       $s_agreement['user_id']                 
= $this->db->f('user_id');
-
-                                       for 
($i=0;$i<count($s_agreement['attributes']);$i++)
-                                       {
-                                               
$s_agreement['attributes'][$i]['value']         = 
$this->db->f($s_agreement['attributes'][$i]['name']);
-                                               
$s_agreement['attributes'][$i]['datatype_text']         = 
$this->bocommon->translate_datatype($s_agreement['attributes'][$i]['datatype']);
-                                       }
-
-                               }
-                       }
-                       return $s_agreement;
-               }
-
-               function read_single_item($data)
-               {
-                       $attribute_table = 'fm_s_agreement_attribute';
-                       $table = 'fm_s_agreement_detail';
-
-                       $s_agreement_id =$data['s_agreement_id'];
-                       $id =$data['id'];
-
-                       $this->db->query("SELECT * FROM $attribute_table WHERE 
attrib_detail = 2 ORDER BY attrib_sort");
-
-                       while ($this->db->next_record())
-                       {
-                               $item['attributes'][] = array
-                               (
-                                       'attrib_id'             => 
$this->db->f('id'),
-                                       'name'                  => 
$this->db->f('column_name'),
-                                       'input_text'    => 
stripslashes($this->db->f('input_text')),
-                                       'statustext'    => 
stripslashes($this->db->f('statustext')),
-                                       'datatype'              => 
$this->db->f('datatype')
-                               );
-                       }
-
-                       if($id && $s_agreement_id)
-                       {
-                               $this->db->query("SELECT * from $table where 
agreement_id=$s_agreement_id AND id=$id");
-
-                               if($this->db->next_record())
-                               {
-                                       $item['agreement_id']   = 
(int)$this->db->f('agreement_id');
-                                       $item['id']                             
= (int)$this->db->f('id');
-                                       $item['entry_date']             = 
$this->db->f('entry_date');
-                                       $item['location_code']  = 
$this->db->f('location_code');
-                                       $item['p_num']                  = 
$this->db->f('p_num');
-                                       $item['p_entity_id']    = 
$this->db->f('p_entity_id');
-                                       $item['p_cat_id']               = 
$this->db->f('p_cat_id');
-                                       $item['cost']                   = 
$this->db->f('cost');
-
-                                       for 
($i=0;$i<count($item['attributes']);$i++)
-                                       {
-                                               
$item['attributes'][$i]['value']        = 
$this->db->f($item['attributes'][$i]['name']);
-                                               
$item['attributes'][$i]['datatype_text']        = 
$this->bocommon->translate_datatype($item['attributes'][$i]['datatype']);
-                                       }
-
-                               }
-                       }
-                       return $item;
-               }
-
-               function add($s_agreement,$values_attribute='')
-               {
-//_debug_array($s_agreement);
-                       $table = 'fm_s_agreement';
-                       $s_agreement['name'] = 
$this->db->db_addslashes($s_agreement['name']);
-                       $s_agreement['descr'] = 
$this->db->db_addslashes($s_agreement['descr']);
-
-                       if($s_agreement['member_of'])
-                       {
-                               $s_agreement['member_of']=',' . 
implode(',',$s_agreement['member_of']) . ',';
-                       }
-
-
-                       $this->db->transaction_begin();
-                       $id = $this->bocommon->increment_id('workorder');
-
-                       $vals[] = $id;
-                       $vals[] = $s_agreement['name'];
-                       $vals[] = $s_agreement['descr'];
-                       $vals[] = time();
-                       $vals[] = $s_agreement['cat_id'];
-                       $vals[] = $s_agreement['member_of'];
-                       $vals[] = $s_agreement['start_date'];
-                       $vals[] = $s_agreement['end_date'];
-                       $vals[] = $s_agreement['termination_date'];
-                       $vals[] = $s_agreement['vendor_id'];
-                       $vals[] = $s_agreement['b_account_id'];
-                       $vals[] = $this->account;
-
-                       while (is_array($s_agreement['extra']) && 
list($input_name,$value) = each($s_agreement['extra']))
-                       {
-                               if($value)
-                               {
-                                       $cols[] = $input_name;
-                                       $vals[] = $value;
-                               }
-                       }
-
-                       if (isset($values_attribute) AND 
is_array($values_attribute))
-                       {
-                               foreach($values_attribute as $entry)
-                               {
-                                       if($entry['value'])
-                                       {
-                                               $cols[] = $entry['name'];
-                                               $vals[] = $entry['value'];
-                                       }
-                               }
-                       }
-
-                       if($cols)
-                       {
-                               $cols   = "," . implode(",", $cols);
-                               $vals   = 
$this->bocommon->validate_db_insert($vals);
-                       }
-
-                       $this->db->query("INSERT INTO $table 
(id,name,descr,entry_date,category,member_of,start_date,end_date,termination_date,vendor_id,account_id,user_id
 $cols) "
-                               . "VALUES ($vals)",__LINE__,__FILE__);
-
-                       $this->db->query("INSERT INTO fm_orders (id,type) 
VALUES ($id,'s_agreement')");
-
-                       $receipt['s_agreement_id']= 
$id;//$this->db->get_last_insert_id($table,'id');
-
-                       $receipt['message'][] = array('msg'=>lang('s_agreement 
%1 has been saved',$receipt['s_agreement_id']));
-
-                       $this->db->transaction_commit();
-                       return $receipt;
-               }
-
-               function add_item($values,$values_attribute='')
-               {
-//_debug_array($values);
-                       $table = 'fm_s_agreement_detail';
-
-                       $cols[] = 'location_code';
-                       $vals[] = $values['location_code'];
-
-/*                     while (is_array($values['location']) && 
list($input_name,$value) = each($values['location']))
-                       {
-                               if($value)
-                               {
-                                       $cols[] = $input_name;
-                                       $vals[] = $value;
-                               }
-                       }
-*/
-                       while (is_array($values['extra']) && 
list($input_name,$value) = each($values['extra']))
-                       {
-                               if($value)
-                               {
-                                       $cols[] = $input_name;
-                                       $vals[] = $value;
-                               }
-                       }
-
-                       if (isset($values_attribute) AND 
is_array($values_attribute))
-                       {
-                               foreach($values_attribute as $entry)
-                               {
-                                       if($entry['value'])
-                                       {
-                                               $cols[] = $entry['name'];
-                                               $vals[] = $entry['value'];
-                                       }
-                               }
-                       }
-
-                       if($values['street_name'])
-                       {
-                               $address[]= $values['street_name'];
-                               $address[]= $values['street_number'];
-                               $address = $this->db->db_addslashes(implode(" 
", $address));
-                       }
-
-                       if(!$address)
-                       {
-                               $address = 
$this->db->db_addslashes($values['location_name']);
-                       }
-
-                       $cols[] = 'address';
-                       $vals[] = $address;
-                       $cols[] = 'cost';
-                       $vals[] = $this->floatval($values['cost']);
-
-                       if($cols)
-                       {
-                               $cols   = "," . implode(",", $cols);
-                               $vals   = "," . 
$this->bocommon->validate_db_insert($vals);
-                       }
-
-                       $this->db->transaction_begin();
-                       $id = 
$this->bocommon->next_id($table,array('agreement_id'=>$values['s_agreement_id']));
-
-                       $this->db->query("INSERT INTO $table 
(id,agreement_id,entry_date,user_id $cols) "
-                               . "VALUES ($id," . $values['s_agreement_id'] 
."," . time()
-                               . "," . $this->account . " $vals)");
-
-
-                       $this->db->query("SELECT start_date FROM fm_s_agreement 
WHERE id=" . $values['s_agreement_id']);
-                       $this->db->next_record();
-                       $start_date     = $this->db->f('start_date');
-
-
-                       $this->db->query("INSERT INTO fm_s_agreement_pricing 
(agreement_id,item_id,id,current_index,this_index,cost,index_date,entry_date,user_id)
 "
-                               . "VALUES (" . $values['s_agreement_id'] . "," 
. $id .",1,1,1," . $this->floatval($values['cost']) . "," . (int)$start_date . 
"," . time()
-                               . "," . $this->account . ")");
-
-                       $receipt['s_agreement_id']= $values['s_agreement_id'];
-                       $receipt['id']= $id;
-
-                       $receipt['message'][] = array('msg'=>lang('s_agreement 
%1 has been saved',$receipt['s_agreement_id']));
-
-                       $this->db->transaction_commit();
-                       return $receipt;
-               }
-
-               function edit($values,$values_attribute='')
-               {
-//_debug_array($values);
-//_debug_array($values_attribute);
-                       $table = 'fm_s_agreement';
-
-                       $values['name'] = 
$this->db->db_addslashes($values['name']);
-
-                       if($values['member_of'])
-                       {
-                               $values['member_of']=',' . 
implode(',',$values['member_of']) . ',';
-                       }
-
-                       while (is_array($values['extra']) && 
list($column,$value) = each($values['extra']))
-                       {
-                               $value_set[$column]     = $value;
-                       }
-
-                       if (isset($values_attribute) AND 
is_array($values_attribute))
-                       {
-                               foreach($values_attribute as $entry)
-                               {
-                                       if($entry['datatype']!='AB' && 
$entry['datatype']!='VENDOR')
-                                       {
-                                               $value_set[$entry['name']]      
= $entry['value'];
-                                       }
-                               }
-                       }
-
-                       $value_set['name']      = $values['name'];
-                       $value_set['descr']     = $values['descr'];
-                       if($value_set)
-                       {
-                               $value_set      = ',' . 
$this->bocommon->validate_db_update($value_set);
-                       }
-
-                       $this->db->query("UPDATE $table set entry_date='" . 
time() . "', category='"
-                                                       . $values['cat_id'] . 
"', member_of='" . $values['member_of'] . "', start_date=" . 
intval($values['start_date']) . ", end_date=" . intval($values['end_date']) . 
", termination_date=" . intval($values['termination_date']) . ", account_id=" . 
intval($values['b_account_id']) . "$value_set WHERE id=" . 
intval($values['s_agreement_id']));
-
-                       $this->db->query("UPDATE fm_s_agreement_pricing set 
index_date=" . intval($values['start_date']) . " WHERE id=1 AND agreement_id= " 
. intval($values['s_agreement_id']));
-
-                       $receipt['s_agreement_id']= $values['s_agreement_id'];
-                       $receipt['message'][] = array('msg'=>lang('s_agreement 
%1 has been edited',$values['s_agreement_id']));
-                       return $receipt;
-               }
-
-               function edit_item($values,$values_attribute='')
-               {
-//_debug_array($values);
-//_debug_array($values_attribute);
-                       $table = 'fm_s_agreement_detail';
-
-                       while (is_array($values['extra']) && 
list($column,$value) = each($values['extra']))
-                       {
-                               $value_set[$column]     = $value;
-                       }
-
-                       if (isset($values_attribute) AND 
is_array($values_attribute))
-                       {
-                               foreach($values_attribute as $entry)
-                               {
-                                       if($entry['datatype']!='AB' && 
$entry['datatype']!='VENDOR')
-                                       {
-                                               $value_set[$entry['name']]      
= $entry['value'];
-                                       }
-                               }
-                       }
-
-                       if($values['street_name'])
-                       {
-                               $address[]= $values['street_name'];
-                               $address[]= $values['street_number'];
-                               $address        = 
$this->db->db_addslashes(implode(" ", $address));
-                       }
-
-                       if(!$address)
-                       {
-                               $address = 
$this->db->db_addslashes($values['location_name']);
-                       }
-
-                       $value_set['location_code']     = 
$values['location_code'];
-                       $value_set['cost']      = $values['cost'];
-                       $value_set['address']   = $address;
-
-                       if($value_set)
-                       {
-                               $value_set      = ',' . 
$this->bocommon->validate_db_update($value_set);
-                       }
-
-                       $this->db->query("UPDATE $table set entry_date=" . 
time() . "$value_set WHERE agreement_id=" . intval($values['s_agreement_id']) . 
' AND id=' . intval($values['id']));
-
-                       $this->db->query("UPDATE fm_s_agreement_pricing set 
cost = this_index *" . $this->floatval($values['cost']) . " WHERE 
agreement_id=" . intval($values['s_agreement_id']) . ' AND item_id=' . 
intval($values['id']));
-
-                       $receipt['s_agreement_id']= $values['s_agreement_id'];
-                       $receipt['id']= $values['id'];
-                       $receipt['message'][] = array('msg'=>lang('s_agreement 
%1 has been edited',$values['s_agreement_id']));
-                       return $receipt;
-               }
-
-               function update($values)
-               {
-//_debug_array($values);
-                       
$values['new_index']=$this->floatval($values['new_index']);
-                       $this->db->transaction_begin();
-
-                       while (is_array($values['select']) && 
list($item_id,$value) = each($values['select']))
-                       {
-
-                               $this->db->query("UPDATE fm_s_agreement_pricing 
set current_index = NULL WHERE agreement_id=" . intval($values['agreement_id']) 
. ' AND item_id=' . intval($item_id));
-
-                               $this->db->query("INSERT INTO 
fm_s_agreement_pricing 
(agreement_id,item_id,id,current_index,this_index,cost,index_date,entry_date,user_id)"
-                                       . "VALUES (" . $values['agreement_id'] 
. "," . $item_id ."," . ($values['id'][$item_id]+1) .",1,'" . 
$values['new_index'] . "','" . ($value * $values['new_index'])  . "'," . 
(int)$values['date'] . "," . time()
-                                       . "," . $this->account . ")");
-
-                       }
-
-                       $this->db->transaction_commit();
-                       $receipt['message'][] = array('msg'=>lang('s_agreement 
%1 has been updated for index',$values['agreement_id']));
-
-                       return $receipt;
-               }
-
-               function floatval($strValue)
-               {
-                       $floatValue = 
ereg_replace("(^[0-9]*)(\\.|,)([0-9]*)(.*)", "\\1.\\3", $strValue);
-                       if(!is_numeric($floatValue))
-                       {
-                               $floatValue = ereg_replace("(^[0-9]*)(.*)", 
"\\1", $strValue);
-                       }
-                       if(!is_numeric($floatValue))
-                       {
-                               $floatValue = 0;
-                       }
-                       return $floatValue;
-               }
-
-
-               function delete_last_index($s_agreement_id,$item_id)
-               {
-                       $this->db->transaction_begin();
-                       $this->db->query("SELECT max(id) as index_count FROM 
fm_s_agreement_pricing WHERE agreement_id=$s_agreement_id AND 
item_id=$item_id");
-                       $this->db->next_record();
-                       $index_count    = $this->db->f('index_count');
-                       if($index_count>1)
-                       {
-                               $this->db->query("DELETE FROM 
fm_s_agreement_pricing WHERE agreement_id=$s_agreement_id AND item_id=$item_id 
AND id=$index_count");
-                               $this->db->query("UPDATE fm_s_agreement_pricing 
set current_index = 1 WHERE agreement_id=$s_agreement_id AND item_id=$item_id 
AND id =" . ($index_count-1));
-                       }
-                       $this->db->transaction_commit();
-               }
-
-               function delete_item($s_agreement_id,$item_id)
-               {
-                       $this->db->transaction_begin();
-                       $this->db->query("DELETE FROM fm_s_agreement_detail 
WHERE agreement_id=$s_agreement_id AND id=$item_id");
-                       $this->db->query("DELETE FROM fm_s_agreement_pricing 
WHERE agreement_id=$s_agreement_id AND item_id=$item_id");
-                       $this->db->transaction_commit();
-               }
-
-
-               function delete($s_agreement_id)
-               {
-                       $table = 'fm_s_agreement';
-                       $this->db->transaction_begin();
-                       $this->db->query("DELETE FROM $table WHERE id=" . 
intval($s_agreement_id));
-                       $this->db->query("DELETE FROM fm_s_agreement_detail 
WHERE agreement_id=" . intval($s_agreement_id));
-                       $this->db->query("DELETE FROM fm_s_agreement_pricing 
WHERE agreement_id=" . intval($s_agreement_id));
-                       $this->db->query("DELETE FROM fm_orders WHERE id=" . 
intval($s_agreement_id));
-                       $this->db->transaction_commit();
-               }
-
-               function read_attrib($data)
-               {
-                       $attribute_table = 'fm_s_agreement_attribute';
-//html_print_r($data);
-                       if(is_array($data))
-                       {
-                               if ($data['start'])
-                               {
-                                       $start=$data['start'];
-                               }
-                               else
-                               {
-                                       $start=0;
-                               }
-                               $query = 
(isset($data['query'])?$data['query']:'');
-                               $sort = 
(isset($data['sort'])?$data['sort']:'DESC');
-                               $order = 
(isset($data['order'])?$data['order']:'');
-                               $allrows = 
(isset($data['allrows'])?$data['allrows']:'');
-                               $column_list = 
(isset($data['column_list'])?$data['column_list']:'');
-                       }
-
-                       $where = 'WHERE';
-                       if ($column_list)
-                       {
-                               $filtermethod = " $where list !=1 or list is 
null";
-                               $where = 'AND';
-                       }
-                       if ($this->role=='detail')
-                       {
-                               $filtermethod .= " $where attrib_detail=2 ";
-                       }
-                       else
-                       {
-                               $filtermethod .= " $where attrib_detail=1 ";
-                       }
-
-                       $where = 'AND';
-
-                       if ($order)
-                       {
-                               $ordermethod = " order by $order $sort";
-
-                       }
-                       else
-                       {
-                               $ordermethod = ' order by attrib_sort asc';
-                       }
-
-                       if($query)
-                       {
-                               $query = ereg_replace("'",'',$query);
-                               $query = ereg_replace('"','',$query);
-
-                               $querymethod = " $where 
($attribute_table.input_text $this->like '%$query%' or 
$attribute_table.column_name $this->like '%$query%')";
-                       }
-
-                       $sql = "SELECT * FROM $attribute_table $filtermethod 
$querymethod";
-
-//echo $sql;
-                       $this->db2->query($sql,__LINE__,__FILE__);
-                       $this->total_records = $this->db2->num_rows();
-                       if(!$allrows)
-                       {
-                               $this->db->limit_query($sql . 
$ordermethod,$start,__LINE__,__FILE__);
-                       }
-                       else
-                       {
-                               $this->db->query($sql . 
$ordermethod,__LINE__,__FILE__);
-                       }
-
-                       while ($this->db->next_record())
-                       {
-                               $attrib[] = array
-                               (
-                                       'id'                    => 
$this->db->f('id'),
-                                       'attrib_sort'   => 
$this->db->f('attrib_sort'),
-                                       'list'                  => 
$this->db->f('list'),
-                                       'lookup_form'   => 
$this->db->f('lookup_form'),
-                                       'column_name'   => 
$this->db->f('column_name'),
-                                       'name'                  => 
$this->db->f('input_text'),
-                                       'size'                  => 
$this->db->f('size'),
-                                       'statustext'    => 
$this->db->f('statustext'),
-                                       'input_text'    => 
$this->db->f('input_text'),
-                                       'type_name'             => 
$this->db->f('type'),
-                                       'datatype'              => 
$this->db->f('datatype'),
-                                       'search'                => 
$this->db->f('search')
-                               );
-                       }
-                       return $attrib;
-               }
-
-               function read_single_attrib($id)
-               {
-                       $attribute_table = 'fm_s_agreement_attribute';
-
-                       if ($this->role=='detail')
-                       {
-                               $filtermethod = " AND attrib_detail=2 ";
-                       }
-                       else
-                       {
-                               $filtermethod = " AND attrib_detail=1 ";
-                       }
-
-                       $sql = "SELECT * FROM $attribute_table where id=$id 
$filtermethod";
-
-                       $this->db->query($sql);
-
-                       if($this->db->next_record())
-                       {
-                               $attrib['id']                                   
        = $this->db->f('id');
-                               $attrib['column_name']                          
= $this->db->f('column_name');
-                               $attrib['input_text']                           
= $this->db->f('input_text');
-                               $attrib['statustext']                           
= $this->db->f('statustext');
-                               $attrib['column_info']['precision']     = 
$this->db->f('precision_');
-                               $attrib['column_info']['scale']         = 
$this->db->f('scale');
-                               $attrib['column_info']['default']       = 
$this->db->f('default_value');
-                               $attrib['column_info']['nullable']      = 
$this->db->f('nullable');
-                               $attrib['column_info']['type']          = 
$this->db->f('datatype');
-                               $attrib['type_name']                            
= $this->db->f('type_name');
-                               $attrib['lookup_form']                          
= $this->db->f('lookup_form');
-                               $attrib['list']                                 
        = $this->db->f('list');
-                               $attrib['search']                               
        = $this->db->f('search');
-                               if($this->db->f('datatype')=='R' || 
$this->db->f('datatype')=='CH' || $this->db->f('datatype')=='LB')
-                               {
-                                       $attrib['choice'] = 
$this->read_attrib_choice($id);
-                               }
-
-                               return $attrib;
-                       }
-               }
-
-               function read_attrib_choice($attrib_id)
-               {
-                       $choice_table = 'fm_s_agreement_choice';
-
-                       if ($this->role=='detail')
-                       {
-                               $filtermethod = " AND attrib_detail=2 ";
-                       }
-                       else
-                       {
-                               $filtermethod = " AND attrib_detail=1 ";
-                       }
-
-                       $sql = "SELECT * FROM $choice_table WHERE 
attrib_id=$attrib_id $filtermethod";
-                       $this->db->query($sql);
-
-                       while ($this->db->next_record())
-                       {
-                               $choice[] = array
-                               (
-                                       'id'    => $this->db->f('id'),
-                                       'value' => $this->db->f('value')
-                               );
-
-                       }
-                       return $choice;
-               }
-
-               function add_attrib($attrib)
-               {
-                       $attribute_table = 'fm_s_agreement_attribute';
-                       $attrib['column_name'] = 
$this->db->db_addslashes($attrib['column_name']);
-                       $attrib['input_text'] = 
$this->db->db_addslashes($attrib['input_text']);
-                       $attrib['statustext'] = 
$this->db->db_addslashes($attrib['statustext']);
-                       $attrib['default'] = 
$this->db->db_addslashes($attrib['default']);
-                       $attrib['id'] = 
$this->bocommon->next_id($attribute_table, array('attrib_detail'=>!!$this->role 
+1));
-
-                       if($this->role=='detail')
-                       {
-                               $filtermethod= 'WHERE attrib_detail=2';
-                               $table = 'fm_s_agreement_detail';
-                       }
-                       else
-                       {
-                               $filtermethod= 'WHERE attrib_detail=1';
-                               $table = 'fm_s_agreement';
-                       }
-                       $sql = "SELECT max(attrib_sort) as max_sort FROM 
$attribute_table $filtermethod";
-                       $this->db->query($sql);
-                       $this->db->next_record();
-                       $attrib_sort    = $this->db->f('max_sort')+1;
-
-                       $values= array(
-                               !!$this->role +1,
-                               $attrib['id'],
-                               $attrib['column_name'],
-                               $attrib['input_text'],
-                               $attrib['statustext'],
-                               $attrib['lookup_form'],
-                               $attrib['search'],
-                               $attrib['list'],
-                               $attrib_sort,
-                               $attrib['column_info']['type'],
-                               $attrib['column_info']['precision'],
-                               $attrib['column_info']['scale'],
-                               $attrib['column_info']['default'],
-                               $attrib['column_info']['nullable']
-                               );
-
-                       $values = $this->bocommon->validate_db_insert($values);
-
-                       $this->db->transaction_begin();
-
-                       $this->db->query("INSERT INTO $attribute_table 
(attrib_detail,id,column_name, input_text, 
statustext,lookup_form,search,list,attrib_sort,datatype,precision_,scale,default_value,nullable)
 "
-                               . "VALUES ($values)");
-
-                       $receipt['id']= $attrib['id'];
-
-                       if($attrib['column_info']['type']=='email' && 
!$attrib['column_info']['precision'])
-                       {
-                               $attrib['column_info']['precision']=64;
-                       }
-
-                       $attrib['column_info']['type']  = 
$this->bocommon->translate_datatype_insert($attrib['column_info']['type']);
-
-                       if($attrib['column_info']['type']=='int' && 
!$attrib['column_info']['precision'])
-                       {
-                               $attrib['column_info']['precision']=4;
-                       }
-
-                       if(!$attrib['column_info']['default'])
-                       {
-                               unset($attrib['column_info']['default']);
-                       }
-
-                       $this->init_process();
-
-                       
if($this->oProc->AddColumn($table,$attrib['column_name'], 
$attrib['column_info']))
-                       {
-                               $receipt['message'][] = array('msg'     => 
lang('Attribute has been saved')     );
-                               $this->db->transaction_commit();
-
-                       }
-                       else
-                       {
-                               $receipt['error'][] = array('msg'       => 
lang('column could not be added')    );
-                               if($this->db->Transaction)
-                               {
-                                       $GLOBALS['phpgw']->db->rollbacktrans();
-                               }
-                               else
-                               {
-                                       $GLOBALS['phpgw']->db->Execute("DELETE 
FROM $attribute_table WHERE id='" . $receipt['id'] . "'");
-                                       unset($receipt['id']);
-
-                               }
-                       }
-
-                       return $receipt;
-               }
-
-               function init_process()
-               {
-                       $this->oProc                                            
= 
CreateObject('phpgwapi.schema_proc',$GLOBALS['phpgw_info']['server']['db_type']);
-                       $this->oProc->m_odb                                     
= $this->db;
-                       $this->oProc->m_odb->Halt_On_Error      = 'report';
-               }
-
-               function edit_attrib($attrib)
-               {
-                       $attribute_table = 'fm_s_agreement_attribute';
-                       $table = 'fm_s_agreement';
-
-                       $attrib['column_name'] = 
$this->db->db_addslashes($attrib['column_name']);
-                       $attrib['input_text'] = 
$this->db->db_addslashes($attrib['input_text']);
-                       $attrib['statustext'] = 
$this->db->db_addslashes($attrib['statustext']);
-                       $attrib['default'] = 
$this->db->db_addslashes($attrib['default']);
-
-                       $choice_table = 'fm_s_agreement_choice';
-
-                       if($this->role=='detail')
-                       {
-                               $filtermethod= ' AND attrib_detail=2';
-                               $table = 'fm_s_agreement_detail';
-                       }
-                       else
-                       {
-                               $filtermethod= ' AND attrib_detail=1';
-                               $table = 'fm_s_agreement';
-                       }
-
-                       $this->db->query("SELECT column_name FROM 
$attribute_table WHERE id='" . $attrib['id']. "' $filtermethod");
-                       $this->db->next_record();
-                       $OldColumnName          = $this->db->f('column_name');
-
-                       $this->db->transaction_begin();
-
-                       $attrib_type=$attrib['column_info']['type'];
-                       if(($OldColumnName !=$attrib['column_name']) || 
($attrib['column_info']['type'] != $this->db->f('datatype')))
-                       {
-                               $value_set=array(
-                                       'column_name'           => 
$attrib['column_name'],
-                                       'input_text'            => 
$attrib['input_text'],
-                                       'statustext'            => 
$attrib['statustext'],
-                                       'lookup_form'           => 
$attrib['lookup_form'],
-                                       'search'                        => 
$attrib['search'],
-                                       'list'                          => 
$attrib['list'],
-                                       'datatype'                      => 
$attrib['column_info']['type'],
-                                       'precision_'            => 
$attrib['column_info']['precision'],
-                                       'scale'                         => 
$attrib['column_info']['scale'],
-                                       'default_value'         => 
$attrib['column_info']['default'],
-                                       'nullable'                      => 
$attrib['column_info']['nullable']
-                                       );
-
-                               $value_set      = 
$this->bocommon->validate_db_update($value_set);
-
-                               $this->db->query("UPDATE $attribute_table set 
$value_set WHERE id=" . $attrib['id'] . $filtermethod);
-
-                               if($attrib['column_info']['type']=='email' && 
!$attrib['column_info']['precision'])
-                               {
-                                       $attrib['column_info']['precision']=64;
-                               }
-
-                               $attrib['column_info']['type']  = 
$this->bocommon->translate_datatype_insert($attrib['column_info']['type']);
-
-                               if($attrib['column_info']['type']=='int' && 
!$attrib['column_info']['precision'])
-                               {
-                                       $attrib['column_info']['precision']=4;
-                               }
-
-                               if(!$attrib['column_info']['default'])
-                               {
-                                       
unset($attrib['column_info']['default']);
-                               }
-
-                               $this->init_process();
-
-                               
$this->oProc->AlterColumn($table,$OldColumnName,$attrib['column_name'],$attrib['column_info']);
-
-                       }
-
-
-                       if($attrib['new_choice'])
-                       {
-                               $this->db->query("SELECT max(id) as id FROM 
$choice_table WHERE attrib_id='" . $attrib['id']. "' $filtermethod");
-                               $this->db->next_record();
-                               $choice_id              = $this->db->f('id')+1;
-
-       //                      $choice_id = 
$this->bocommon->next_id($choice_table 
,array('attrib_detail'=>2,'attrib_id'=>$attrib['id']));
-
-                               $values= array(
-                                       $attrib['id'],
-                                       $choice_id,
-                                       !!$this->role +1,
-                                       $attrib['new_choice']
-                                       );
-
-                               $values = 
$this->bocommon->validate_db_insert($values);
-
-                               $this->db->query("INSERT INTO $choice_table 
(attrib_id,id,attrib_detail,value) "
-                               . "VALUES ($values)");
-                       }
-
-
-                       if($attrib['delete_choice'])
-                       {
-                               for 
($i=0;$i<count($attrib['delete_choice']);$i++)
-                               {
-                                       $this->db->query("DELETE FROM 
$choice_table WHERE  attrib_id=" . $attrib['id']  ." AND id=" . 
$attrib['delete_choice'][$i] . $filtermethod);
-                               }
-                       }
-
-//html_print_r($attrib_type);
-                       if($attrib_type!='R' && $attrib_type!='CH' && 
$attrib_type!='LB')
-                       {
-                               $this->db->query("DELETE FROM $choice_table 
WHERE  attrib_id=" . $attrib['id'] . $filtermethod);
-                       }
-
-
-                       $this->db->transaction_commit();
-
-                       $receipt['message'][] = array('msg'     => 
lang('Attribute has been edited'));
-
-
-                       return $receipt;
-
-               }
-               function resort_attrib($data)
-               {
-//html_print_r($data);
-                       $attribute_table = 'fm_s_agreement_attribute';
-                       if(is_array($data))
-                       {
-                               $resort = 
(isset($data['resort'])?$data['resort']:'up');
-                               $id = (isset($data['id'])?$data['id']:'');
-                       }
-
-                       $sql = "SELECT attrib_sort FROM $attribute_table where 
id=$id";
-                       $this->db->query($sql);
-                       $this->db->next_record();
-                       $attrib_sort    = $this->db->f('attrib_sort');
-                       $sql = "SELECT max(attrib_sort) as max_sort FROM 
$attribute_table";
-                       $this->db->query($sql);
-                       $this->db->next_record();
-                       $max_sort       = $this->db->f('max_sort');
-                       switch($resort)
-                       {
-                               case 'up':
-                                       if($attrib_sort>1)
-                                       {
-                                               $sql = "UPDATE $attribute_table 
set attrib_sort=$attrib_sort WHERE attrib_sort =" . ($attrib_sort-1);
-                                               $this->db->query($sql);
-                                               $sql = "UPDATE $attribute_table 
set attrib_sort=" . ($attrib_sort-1) ." WHERE id=$id";
-                                               $this->db->query($sql);
-                                       }
-                                       break;
-                               case 'down':
-                                       if($max_sort > $attrib_sort)
-                                       {
-                                               $sql = "UPDATE $attribute_table 
set attrib_sort=$attrib_sort WHERE attrib_sort =" . ($attrib_sort+1);
-                                               $this->db->query($sql);
-                                               $sql = "UPDATE $attribute_table 
set attrib_sort=" . ($attrib_sort+1) ." WHERE id=$id";
-                                               $this->db->query($sql);
-                                       }
-                                       break;
-                               default:
-                                       return;
-                                       break;
-                       }
-               }
-               function delete_attrib($attrib_id)
-               {
-                       $table = 'fm_s_agreement';
-                       $attribute_table = 'fm_s_agreement_attribute';
-                       $this->init_process();
-
-                       $sql = "SELECT * FROM $attribute_table WHERE 
id=$attrib_id";
-
-                       $this->db->query($sql);
-                       $this->db->next_record();
-                       $ColumnName             = $this->db->f('column_name');
-
-                       if($this->oProc->DropColumn($table,'', $ColumnName))
-                       {
-                               $sql = "SELECT attrib_sort FROM 
$attribute_table where id=$attrib_id";
-                               $this->db->query($sql);
-                               $this->db->next_record();
-                               $attrib_sort    = $this->db->f('attrib_sort');
-                               $sql2 = "SELECT max(attrib_sort) as max_sort 
FROM $attribute_table";
-                               $this->db->query($sql2);
-                               $this->db->next_record();
-                               $max_sort       = $this->db->f('max_sort');
-                               if($max_sort>$attrib_sort)
-                               {
-                                       $sql = "UPDATE $attribute_table set 
attrib_sort=attrib_sort-1 WHERE attrib_sort > $attrib_sort";
-                                       $this->db->query($sql);
-                               }
-
-                               $this->db->query("DELETE FROM $attribute_table 
WHERE id=$attrib_id");
-                       }
-                       else
-                       {
-                               $receipt['error'][] = array('msg'       => 
lang('Attribute has NOT been deleted'));
-                       }
-
-                       return $receipt;
-               }
-
-               function request_next_id()
-               {
-                       $this->db->query("SELECT max(id) as id FROM 
fm_s_agreement");
-                       $this->db->next_record();
-                       $next_id= $this->db->f('id')+1;
-                       return $next_id;
-               }
-
-
-       }
-?>
+                       $this->db2              = $this->bocommon->new_db();
+
+                       $this->join                     = $this->bocommon->join;
+                       $this->left_join        = $this->bocommon->left_join;
+                       $this->like                     = $this->bocommon->like;
+               }
+
+               function select_category_list()
+               {
+                       $table = 'fm_s_agreement' . '_category';
+                       $this->db->query("SELECT id,descr FROM $table  ORDER BY 
descr ");
+
+                       $i = 0;
+                       while ($this->db->next_record())
+                       {
+                               $categories[$i]['id']                           
= $this->db->f('id');
+                               $categories[$i]['name']                         
= stripslashes($this->db->f('descr'));
+                               $i++;
+                       }
+                       return $categories;
+               }
+
+               function select_vendor_list()
+               {
+                       $table = 'fm_s_agreement';
+                       $this->db->query("SELECT vendor_id,org_name FROM $table 
$this->join fm_vendor on fm_s_agreement.vendor_id=fm_vendor.id GROUP BY 
org_name,vendor_id ");
+
+                       $i = 0;
+                       while ($this->db->next_record())
+                       {
+                               $vendor[$i]['id']                               
= $this->db->f('vendor_id');
+                               $vendor[$i]['name']                             
= stripslashes($this->db->f('org_name'));
+                               $i++;
+                       }
+                       return $vendor;
+               }
+
+               function read($data)
+               {
+                       if(is_array($data))
+                       {
+                               $start                  = 
(isset($data['start'])?$data['start']:0);
+                               $filter                 = 
(isset($data['filter'])?$data['filter']:'none');
+                               $query                  = 
(isset($data['query'])?$data['query']:'');
+                               $sort                   = 
(isset($data['sort'])?$data['sort']:'DESC');
+                               $order                  = 
(isset($data['order'])?$data['order']:'');
+                               $cat_id                 = 
(isset($data['cat_id'])?$data['cat_id']:'');
+                               $vendor_id              = 
(isset($data['vendor_id'])?$data['vendor_id']:'');
+                               $allrows                = 
(isset($data['allrows'])?$data['allrows']:'');
+                               $member_id              = 
(isset($data['member_id'])?$data['member_id']:0);
+                               $s_agreement_id = 
(isset($data['s_agreement_id'])?$data['s_agreement_id']:'');
+                               $detail                 = 
(isset($data['detail'])?$data['detail']:'');
+                       }
+
+                       $choice_table = 'fm_s_agreement_choice';
+                       $attribute_table = 'fm_s_agreement_attribute';
+
+                       if(!$detail)
+                       {
+                               $entity_table = 'fm_s_agreement';
+                               $category_table = 'fm_s_agreement_category';
+                               $attribute_filter = " AND attrib_detail = 1";
+
+                               $paranthesis .='(';
+                               $joinmethod .= " $this->join $category_table ON 
( $entity_table.category =$category_table.id))";
+                               $paranthesis .='(';
+                               $joinmethod .= " $this->join fm_vendor ON ( 
$entity_table.vendor_id =fm_vendor.id))";
+
+                               $cols = $entity_table . 
".*,$category_table.descr as category, org_name";
+
+                               $cols_return[]                          = 'id';
+                               $uicols['input_type'][]         = 'text';
+                               $uicols['name'][]                       = 'id';
+                               $uicols['descr'][]                      = 
lang('ID');
+                               $uicols['statustext'][]         = lang('ID');
+
+                               $cols_return[]                          = 
'name';
+                               $uicols['input_type'][]         = 'text';
+                               $uicols['name'][]                       = 
'name';
+                               $uicols['descr'][]                      = 
lang('name');
+                               $uicols['statustext'][]         = lang('name');
+
+                               $cols_return[]                          = 
'org_name';
+                               $uicols['input_type'][]         = 'text';
+                               $uicols['name'][]                       = 
'org_name';
+                               $uicols['descr'][]                      = 
lang('vendor');
+                               $uicols['statustext'][]         = 
lang('vendor');
+
+                               $cols_return[]                          = 
'category';
+                               $uicols['input_type'][]         = 'text';
+                               $uicols['name'][]                       = 
'category';
+                               $uicols['descr'][]                      = 
lang('category');
+                               $uicols['statustext'][]         = 
lang('category');
+
+                               $cols_return[]                          = 
'start_date';
+                               $uicols['input_type'][]         = 'text';
+                               $uicols['name'][]                       = 
'start_date';
+                               $uicols['descr'][]                      = 
lang('start');
+                               $uicols['statustext'][]         = lang('start 
date');
+
+                               $cols_return[]                          = 
'end_date';
+                               $uicols['input_type'][]         = 'text';
+                               $uicols['name'][]                       = 
'end_date';
+                               $uicols['descr'][]                      = 
lang('end');
+                               $uicols['statustext'][]         = lang('end 
date');
+
+                       }
+                       else
+                       {
+                               $allrows=True;
+                               $entity_table = 'fm_s_agreement_detail';
+                               $attribute_filter = " AND attrib_detail = 2";
+
+                               $paranthesis .='(';
+                               $joinmethod .= " $this->join  
fm_s_agreement_pricing ON ( $entity_table.agreement_id 
=fm_s_agreement_pricing.agreement_id AND $entity_table.id 
=fm_s_agreement_pricing.item_id))";
+
+
+
+                               $cols = "$entity_table.*, 
fm_s_agreement_pricing.cost,fm_s_agreement_pricing.id as 
index_count,fm_s_agreement_pricing.index_date,fm_s_agreement_pricing.item_id,fm_s_agreement_pricing.this_index";
+
+                               $cols_return[]                          = 
'agreement_id';
+                               $uicols['input_type'][]         = 'hidden';
+                               $uicols['name'][]                       = 
'agreement_id';
+                               $uicols['descr'][]                      = 
lang('agreement_id');
+                               $uicols['statustext'][]         = 
lang('agreement_id');
+
+                               $cols_return[]                          = 
'item_id';
+                               $uicols['input_type'][]         = 'text';
+                               $uicols['name'][]                       = 
'item_id';
+                               $uicols['descr'][]                      = 
lang('ID');
+                               $uicols['statustext'][]         = lang('ID');
+
+                               $cols_return[]                          = 'id';
+                               $uicols['input_type'][]         = 'hidden';
+                               $uicols['name'][]                       = 'id';
+                               $uicols['descr'][]                      = False;
+                               $uicols['statustext'][]         = False;
+
+                               $cols_return[]                          = 
'location_code';
+                               $uicols['input_type'][]         = 'text';
+                               $uicols['name'][]                       = 
'location_code';
+                               $uicols['descr'][]                      = 
lang('location');
+                               $uicols['statustext'][]         = 
lang('location');
+
+                               $cols_return[]                          = 
'address';
+                               $uicols['input_type'][]         = 'text';
+                               $uicols['name'][]                       = 
'address';
+                               $uicols['descr'][]                      = 
lang('address');
+                               $uicols['statustext'][]         = 
lang('address');
+
+                               $cols_return[]                          = 
'p_num';
+                               $uicols['input_type'][]         = 'text';
+                               $uicols['name'][]                       = 
'p_num';
+                               $uicols['descr'][]                      = 
lang('entity num');
+                               $uicols['statustext'][]         = lang('entity 
num');
+
+                               $cols_return[]                          = 
'cost';
+                               $uicols['input_type'][]         = 'text';
+                               $uicols['name'][]                       = 
'cost';
+                               $uicols['descr'][]                      = 
lang('cost');
+                               $uicols['statustext'][]         = lang('cost');
+
+                               $cols_return[]                          = 
'this_index';
+                               $uicols['input_type'][]         = 'text';
+                               $uicols['name'][]                       = 
'this_index';
+                               $uicols['descr'][]                      = 
lang('index');
+                               $uicols['statustext'][]         = lang('index');
+
+                               $cols_return[]                          = 
'index_count';
+                               $uicols['input_type'][]         = 'text';
+                               $uicols['name'][]                       = 
'index_count';
+                               $uicols['descr'][]                      = 
lang('index_count');
+                               $uicols['statustext'][]         = 
lang('index_count');
+
+                               $cols_return[]                          = 
'index_date';
+                               $uicols['input_type'][]         = 'text';
+                               $uicols['name'][]                       = 
'index_date';
+                               $uicols['descr'][]                      = 
lang('date');
+                               $uicols['statustext'][]         = lang('date');
+                       }
+
+                       $from .= " FROM $paranthesis $entity_table ";
+
+                       $sql = "SELECT $cols $from $joinmethod";
+
+                       $i      = count($uicols['name']);
+
+                       $this->db->query("SELECT * FROM $attribute_table WHERE 
list=1 $attribute_filter ");
+                       while ($this->db->next_record())
+                       {
+                               $uicols['input_type'][]         = 'text';
+                               $uicols['name'][]                       = 
$this->db->f('column_name');
+                               $uicols['descr'][]                      = 
$this->db->f('input_text');
+                               $uicols['statustext'][]         = 
$this->db->f('statustext');
+                               $uicols['datatype'][$i]         = 
$this->db->f('datatype');
+                               $cols_return_extra[]= array(
+                                       'name'  => $this->db->f('column_name'),
+                                       'datatype'      => 
$this->db->f('datatype'),
+                                       'attrib_id'     => $this->db->f('id')
+                               );
+
+                               $i++;
+                       }
+
+                       
$user_columns=$GLOBALS['phpgw_info']['user']['preferences'][$this->currentapp]['s_agreement_columns'
 . !!$s_agreement_id];
+
+//_debug_array($user_columns);
+
+                       if (isset($user_columns) AND is_array($user_columns) 
AND $user_columns[0])
+                       {
+                               foreach($user_columns as $column_id)
+                               {
+                                       $this->db->query("SELECT * FROM 
$attribute_table WHERE id= $column_id");
+
+                                       $this->db->next_record();
+                                       $uicols['input_type'][]         = 
'text';
+                                       $uicols['name'][]                       
= $this->db->f('column_name');
+                                       $uicols['descr'][]                      
= $this->db->f('input_text');
+                                       $uicols['statustext'][]         = 
$this->db->f('statustext');
+                                       $uicols['datatype'][$i]         = 
$this->db->f('datatype');
+                                       $cols_return_extra[]= array(
+                                               'name'  => 
$this->db->f('column_name'),
+                                               'datatype'      => 
$this->db->f('datatype'),
+                                               'attrib_id'     => 
$this->db->f('id')
+                                       );
+                                       $i++;
+                               }
+                       }
+
+                       $this->uicols   = $uicols;
+
+//_debug_array($cols_return_extra);
+                       if ($order)
+                       {
+                               if ($order=='id')
+                               {
+                                       $ordermethod = " order by 
$entity_table.$order $sort";
+                               }
+                               else
+                               {
+                                       $ordermethod = " order by $order $sort";
+                               }
+                       }
+                       else
+                       {
+                               $ordermethod = " order by $entity_table.id 
DESC";
+                       }
+
+                       $where= 'WHERE';
+
+/*                     if ($filter=='all')
+                       {
+                               if (is_array($grants))
+                               {
+                                       while (list($user) = each($grants))
+                                       {
+                                               $public_user_list[] = $user;
+                                       }
+                                       reset($public_user_list);
+                                       $filtermethod .= " $where ( 
$entity_table.user_id IN(" . implode(',',$public_user_list) . "))";
+
+                                       $where= 'AND';
+                               }
+
+                       }
+                       else
+                       {
+                               $filtermethod = " $where 
$entity_table.user_id=$filter ";
+                               $where= 'AND';
+                       }
+*/
+
+                       if ($s_agreement_id)
+                       {
+                               $filtermethod .= " $where 
$entity_table.agreement_id=$s_agreement_id AND current_index = 1";
+                               $where= 'AND';
+                       }
+
+                       if ($cat_id)
+                       {
+                               $filtermethod .= " $where 
$entity_table.category='$cat_id' ";
+                               $where= 'AND';
+                       }
+
+                       if ($vendor_id)
+                       {
+                               $filtermethod .= " $where 
$entity_table.vendor_id='$vendor_id' ";
+                               $where= 'AND';
+                       }
+
+                       if ($member_id>0)
+                       {
+                               $filtermethod .= " $where 
$entity_table.member_of $this->like '%,$member_id,%' ";
+                               $where= 'AND';
+                       }
+
+                       if ($status)
+                       {
+                               $filtermethod .= " $where 
$entity_table.status='$status' ";
+                               $where= 'AND';
+                       }
+
+
+                       if($query)
+                       {
+                               $query = ereg_replace("'",'',$query);
+                               $query = ereg_replace('"','',$query);
+
+                               $this->db->query("SELECT * FROM 
$attribute_table where search='1'");
+
+                               while ($this->db->next_record())
+                               {
+                                       if($this->db->f('datatype')=='V' || 
$this->db->f('datatype')=='email' || $this->db->f('datatype')=='CH')
+                                       {
+                                               $querymethod[]= 
"$entity_table." . $this->db->f('column_name') . " $this->like '%$query%'";
+                                       }
+                                       else
+                                       {
+                                               $querymethod[]= 
"$entity_table." . $this->db->f('column_name') . " = '$query'";
+                                       }
+                               }
+
+                               if (isset($querymethod) AND 
is_array($querymethod))
+                               {
+                                       $querymethod = " $where (" . implode (' 
OR ',$querymethod) . ')';
+                                       $where = 'AND';
+                               }
+                       }
+
+                       $sql .= " $filtermethod $querymethod";
+//echo $sql;
+
+                       $this->db2->query($sql,__LINE__,__FILE__);
+                       $this->total_records = $this->db2->num_rows();
+                       if(!$allrows)
+                       {
+                               $this->db->limit_query($sql . 
$ordermethod,$start,__LINE__,__FILE__);
+                       }
+                       else
+                       {
+                               $this->db->query($sql . 
$ordermethod,__LINE__,__FILE__);
+                       }
+
+                       $j=0;
+                       $n=count($cols_return);
+//_debug_array($cols_return);
+
+                       $contacts                       = 
CreateObject('phpgwapi.contacts');
+
+                       while ($this->db->next_record())
+                       {
+                               for ($i=0;$i<$n;$i++)
+                               {
+                                       $s_agreement_list[$j][$cols_return[$i]] 
= stripslashes($this->db->f($cols_return[$i]));
+                                       $s_agreement_list[$j]['grants'] = 
(int)$grants[$this->db->f('user_id')];
+                               }
+
+                               for ($i=0;$i<count($cols_return_extra);$i++)
+                               {
+                                       $value='';
+                                       
$value=$this->db->f($cols_return_extra[$i]['name']);
+
+                                       
if(($cols_return_extra[$i]['datatype']=='R' || 
$cols_return_extra[$i]['datatype']=='LB') && $value):
+                                       {
+                                               $sql="SELECT value FROM 
$choice_table where attrib_id=" .$cols_return_extra[$i]['attrib_id']. "  AND 
id=" . $value . $attribute_filter;
+                                               $this->db2->query($sql);
+                                               $this->db2->next_record();
+                                               
$s_agreement_list[$j][$cols_return_extra[$i]['name']] = $this->db2->f('value');
+                                       }
+                                       
elseif($cols_return_extra[$i]['datatype']=='AB' && $value):
+                                       {
+                                               $contact_data   = 
$contacts->read_single_entry($value,array('n_given'=>'n_given','n_family'=>'n_family','email'=>'email'));
+                                               
$s_agreement_list[$j][$cols_return_extra[$i]['name']]   = 
$contact_data[0]['n_family'] . ', ' . $contact_data[0]['n_given'];
+
+                                       }
+                                       
elseif($cols_return_extra[$i]['datatype']=='VENDOR' && $value):
+                                       {
+                                               $sql="SELECT org_name FROM 
fm_vendor where id=$value";
+                                               $this->db2->query($sql);
+                                               $this->db2->next_record();
+                                               
$s_agreement_list[$j][$cols_return_extra[$i]['name']] = 
$this->db2->f('org_name');
+                                       }
+                                       
elseif($cols_return_extra[$i]['datatype']=='CH' && $value):
+                                       {
+                                               $ch= unserialize($value);
+
+                                               if (isset($ch) AND 
is_array($ch))
+                                               {
+                                                       for 
($k=0;$k<count($ch);$k++)
+                                                       {
+                                                               $sql="SELECT 
value FROM $choice_table where attrib_id=" 
.$cols_return_extra[$i]['attrib_id']. "  AND id=" . $ch[$k] . $attribute_filter;
+                                                               
$this->db2->query($sql);
+                                                               while 
($this->db2->next_record())
+                                                               {
+                                                                       
$ch_value[]=$this->db2->f('value');
+                                                               }
+                                                       }
+                                                       
$s_agreement_list[$j][$cols_return_extra[$i]['name']] = @implode(",", 
$ch_value);
+                                                       unset($ch_value);
+                                               }
+                                       }
+                                       
elseif($cols_return_extra[$i]['datatype']=='D' && $value):
+                                       {
+//html_print_r($value);
+
+                                               
$s_agreement_list[$j][$cols_return_extra[$i]['name']]=date($GLOBALS['phpgw_info']['user']['preferences']['common']['dateformat'],strtotime($value));
+                                       }
+                                       else:
+                                       {
+                                               
$s_agreement_list[$j][$cols_return_extra[$i]['name']]=$value;
+                                       }
+                                       endif;
+                               }
+
+                               $j++;
+                       }
+//html_print_r($s_agreement_list);
+                       return $s_agreement_list;
+               }
+
+               function read_prizing($data)
+               {
+                       if(is_array($data))
+                       {
+                               $s_agreement_id = 
(isset($data['s_agreement_id'])?$data['s_agreement_id']:0);
+                               $item_id        = 
(isset($data['item_id'])?$data['item_id']:0);
+                       }
+
+                       $entity_table = 'fm_s_agreement_pricing';
+
+                       $cols = 
"fm_s_agreement_pricing.cost,fm_s_agreement_pricing.id as 
index_count,fm_s_agreement_pricing.index_date,fm_s_agreement_pricing.item_id,fm_s_agreement_pricing.this_index";
+
+                       $cols_return[]                  = 'item_id';
+                       $uicols['input_type'][]         = 'text';
+                       $uicols['name'][]                       = 'item_id';
+                       $uicols['descr'][]                      = lang('ID');
+                       $uicols['statustext'][]         = lang('ID');
+
+                       $cols_return[]                  = 'id';
+                       $uicols['input_type'][]         = 'hidden';
+                       $uicols['name'][]                       = 'id';
+                       $uicols['descr'][]                      = False;
+                       $uicols['statustext'][]         = False;
+
+                       $cols_return[]                  = 'cost';
+                       $uicols['input_type'][]         = 'text';
+                       $uicols['name'][]                       = 'cost';
+                       $uicols['descr'][]                      = lang('cost');
+                       $uicols['statustext'][]         = lang('cost');
+
+                       $cols_return[]                  = 'this_index';
+                       $uicols['input_type'][]         = 'text';
+                       $uicols['name'][]                       = 'this_index';
+                       $uicols['descr'][]                      = lang('index');
+                       $uicols['statustext'][]         = lang('index');
+
+                       $cols_return[]                  = 'index_count';
+                       $uicols['input_type'][]         = 'text';
+                       $uicols['name'][]                       = 'index_count';
+                       $uicols['descr'][]                      = 
lang('index_count');
+                       $uicols['statustext'][]         = lang('index_count');
+
+                       $cols_return[]                  = 'index_date';
+                       $uicols['input_type'][]         = 'text';
+                       $uicols['name'][]                       = 'index_date';
+                       $uicols['descr'][]                      = lang('date');
+                       $uicols['statustext'][]         = lang('date');
+
+                       $from .= " FROM $entity_table ";
+
+                       $sql = "SELECT $cols $from $joinmethod";
+
+
+                       $this->uicols   = $uicols;
+
+                       $ordermethod = " order by $entity_table.id ASC";
+
+                       $where= 'WHERE';
+
+
+                       if ($s_agreement_id)
+                       {
+                               $filtermethod .= " $where 
$entity_table.agreement_id=$s_agreement_id AND item_id=$item_id";
+                               $where= 'AND';
+                       }
+
+
+                       $sql .= " $filtermethod";
+//echo $sql;
+
+                       $this->db2->query($sql,__LINE__,__FILE__);
+                       $this->total_records = $this->db2->num_rows();
+                       if(!$allrows)
+                       {
+                               $this->db->limit_query($sql . 
$ordermethod,$start,__LINE__,__FILE__);
+                       }
+                       else
+                       {
+                               $this->db->query($sql . 
$ordermethod,__LINE__,__FILE__);
+                       }
+
+                       $j=0;
+                       $n=count($cols_return);
+//_debug_array($cols_return);
+                       while ($this->db->next_record())
+                       {
+                               for ($i=0;$i<$n;$i++)
+                               {
+                                       $s_agreement_list[$j][$cols_return[$i]] 
= $this->db->f($cols_return[$i]);
+                                       $s_agreement_list[$j]['agreement_id'] = 
$s_agreement_id;
+                               }
+                               $j++;
+                       }
+//_debug_array($s_agreement_list);
+                       return $s_agreement_list;
+               }
+
+
+               function read_single($data)
+               {
+                       $attribute_table = 'fm_s_agreement_attribute';
+                       $table = 'fm_s_agreement';
+
+                       $s_agreement_id =$data['s_agreement_id'];
+
+                       $this->db->query("SELECT * FROM $attribute_table WHERE 
attrib_detail = 1 ORDER BY attrib_sort");
+
+                       while ($this->db->next_record())
+                       {
+                               $s_agreement['attributes'][] = array
+                               (
+                                       'attrib_id'             => 
$this->db->f('id'),
+                                       'name'                  => 
$this->db->f('column_name'),
+                                       'input_text'    => 
stripslashes($this->db->f('input_text')),
+                                       'statustext'    => 
stripslashes($this->db->f('statustext')),
+                                       'datatype'              => 
$this->db->f('datatype')
+                               );
+                       }
+
+                       if($s_agreement_id)
+                       {
+                               $this->db->query("SELECT * from $table where 
id='$s_agreement_id'");
+
+                               if($this->db->next_record())
+                               {
+                                       $s_agreement['id']                      
= (int)$this->db->f('id');
+                                       $s_agreement['entry_date']              
= $this->db->f('entry_date');
+                                       $s_agreement['cat_id']                  
= $this->db->f('category');
+                                       $s_agreement['member_of']               
= explode(',',$this->db->f('member_of'));
+                                       $s_agreement['cat_id']                  
= $this->db->f('category');
+                                       $s_agreement['start_date']              
= $this->db->f('start_date');
+                                       $s_agreement['end_date']                
= $this->db->f('end_date');
+                                       $s_agreement['termination_date']= 
$this->db->f('termination_date');
+                                       $s_agreement['vendor_id']               
= $this->db->f('vendor_id');
+                                       $s_agreement['b_account_id']    = 
$this->db->f('account_id');
+                                       $s_agreement['name']                    
= stripslashes($this->db->f('name'));
+                                       $s_agreement['descr']                   
= stripslashes($this->db->f('descr'));
+                                       $s_agreement['user_id']                 
= $this->db->f('user_id');
+
+                                       for 
($i=0;$i<count($s_agreement['attributes']);$i++)
+                                       {
+                                               
$s_agreement['attributes'][$i]['value']         = 
$this->db->f($s_agreement['attributes'][$i]['name']);
+                                               
$s_agreement['attributes'][$i]['datatype_text']         = 
$this->bocommon->translate_datatype($s_agreement['attributes'][$i]['datatype']);
+                                       }
+
+                               }
+                       }
+                       return $s_agreement;
+               }
+
+               function read_single_item($data)
+               {
+                       $attribute_table = 'fm_s_agreement_attribute';
+                       $table = 'fm_s_agreement_detail';
+
+                       $s_agreement_id =$data['s_agreement_id'];
+                       $id =$data['id'];
+
+                       $this->db->query("SELECT * FROM $attribute_table WHERE 
attrib_detail = 2 ORDER BY attrib_sort");
+
+                       while ($this->db->next_record())
+                       {
+                               $item['attributes'][] = array
+                               (
+                                       'attrib_id'             => 
$this->db->f('id'),
+                                       'name'                  => 
$this->db->f('column_name'),
+                                       'input_text'    => 
stripslashes($this->db->f('input_text')),
+                                       'statustext'    => 
stripslashes($this->db->f('statustext')),
+                                       'datatype'              => 
$this->db->f('datatype')
+                               );
+                       }
+
+                       if($id && $s_agreement_id)
+                       {
+                               $this->db->query("SELECT * from $table where 
agreement_id=$s_agreement_id AND id=$id");
+
+                               if($this->db->next_record())
+                               {
+                                       $item['agreement_id']   = 
(int)$this->db->f('agreement_id');
+                                       $item['id']                             
= (int)$this->db->f('id');
+                                       $item['entry_date']             = 
$this->db->f('entry_date');
+                                       $item['location_code']  = 
$this->db->f('location_code');
+                                       $item['p_num']                  = 
$this->db->f('p_num');
+                                       $item['p_entity_id']    = 
$this->db->f('p_entity_id');
+                                       $item['p_cat_id']               = 
$this->db->f('p_cat_id');
+                                       $item['cost']                   = 
$this->db->f('cost');
+
+                                       for 
($i=0;$i<count($item['attributes']);$i++)
+                                       {
+                                               
$item['attributes'][$i]['value']        = 
$this->db->f($item['attributes'][$i]['name']);
+                                               
$item['attributes'][$i]['datatype_text']        = 
$this->bocommon->translate_datatype($item['attributes'][$i]['datatype']);
+                                       }
+
+                               }
+                       }
+                       return $item;
+               }
+
+               function add($s_agreement,$values_attribute='')
+               {
+//_debug_array($s_agreement);
+                       $table = 'fm_s_agreement';
+                       $s_agreement['name'] = 
$this->db->db_addslashes($s_agreement['name']);
+                       $s_agreement['descr'] = 
$this->db->db_addslashes($s_agreement['descr']);
+
+                       if($s_agreement['member_of'])
+                       {
+                               $s_agreement['member_of']=',' . 
implode(',',$s_agreement['member_of']) . ',';
+                       }
+
+
+                       $this->db->transaction_begin();
+                       $id = $this->bocommon->increment_id('workorder');
+
+                       $vals[] = $id;
+                       $vals[] = $s_agreement['name'];
+                       $vals[] = $s_agreement['descr'];
+                       $vals[] = time();
+                       $vals[] = $s_agreement['cat_id'];
+                       $vals[] = $s_agreement['member_of'];
+                       $vals[] = $s_agreement['start_date'];
+                       $vals[] = $s_agreement['end_date'];
+                       $vals[] = $s_agreement['termination_date'];
+                       $vals[] = $s_agreement['vendor_id'];
+                       $vals[] = $s_agreement['b_account_id'];
+                       $vals[] = $this->account;
+
+                       while (is_array($s_agreement['extra']) && 
list($input_name,$value) = each($s_agreement['extra']))
+                       {
+                               if($value)
+                               {
+                                       $cols[] = $input_name;
+                                       $vals[] = $value;
+                               }
+                       }
+
+                       if (isset($values_attribute) AND 
is_array($values_attribute))
+                       {
+                               foreach($values_attribute as $entry)
+                               {
+                                       if($entry['value'])
+                                       {
+                                               $cols[] = $entry['name'];
+                                               $vals[] = $entry['value'];
+                                       }
+                               }
+                       }
+
+                       if($cols)
+                       {
+                               $cols   = "," . implode(",", $cols);
+                               $vals   = 
$this->bocommon->validate_db_insert($vals);
+                       }
+
+                       $this->db->query("INSERT INTO $table 
(id,name,descr,entry_date,category,member_of,start_date,end_date,termination_date,vendor_id,account_id,user_id
 $cols) "
+                               . "VALUES ($vals)",__LINE__,__FILE__);
+
+                       $this->db->query("INSERT INTO fm_orders (id,type) 
VALUES ($id,'s_agreement')");
+
+                       $receipt['s_agreement_id']= 
$id;//$this->db->get_last_insert_id($table,'id');
+
+                       $receipt['message'][] = array('msg'=>lang('s_agreement 
%1 has been saved',$receipt['s_agreement_id']));
+
+                       $this->db->transaction_commit();
+                       return $receipt;
+               }
+
+               function add_item($values,$values_attribute='')
+               {
+//_debug_array($values);
+                       $table = 'fm_s_agreement_detail';
+
+                       $cols[] = 'location_code';
+                       $vals[] = $values['location_code'];
+
+/*                     while (is_array($values['location']) && 
list($input_name,$value) = each($values['location']))
+                       {
+                               if($value)
+                               {
+                                       $cols[] = $input_name;
+                                       $vals[] = $value;
+                               }
+                       }
+*/
+                       while (is_array($values['extra']) && 
list($input_name,$value) = each($values['extra']))
+                       {
+                               if($value)
+                               {
+                                       $cols[] = $input_name;
+                                       $vals[] = $value;
+                               }
+                       }
+
+                       if (isset($values_attribute) AND 
is_array($values_attribute))
+                       {
+                               foreach($values_attribute as $entry)
+                               {
+                                       if($entry['value'])
+                                       {
+                                               $cols[] = $entry['name'];
+                                               $vals[] = $entry['value'];
+                                       }
+                               }
+                       }
+
+                       if($values['street_name'])
+                       {
+                               $address[]= $values['street_name'];
+                               $address[]= $values['street_number'];
+                               $address = $this->db->db_addslashes(implode(" 
", $address));
+                       }
+
+                       if(!$address)
+                       {
+                               $address = 
$this->db->db_addslashes($values['location_name']);
+                       }
+
+                       $cols[] = 'address';
+                       $vals[] = $address;
+                       $cols[] = 'cost';
+                       $vals[] = $this->floatval($values['cost']);
+
+                       if($cols)
+                       {
+                               $cols   = "," . implode(",", $cols);
+                               $vals   = "," . 
$this->bocommon->validate_db_insert($vals);
+                       }
+
+                       $this->db->transaction_begin();
+                       $id = 
$this->bocommon->next_id($table,array('agreement_id'=>$values['s_agreement_id']));
+
+                       $this->db->query("INSERT INTO $table 
(id,agreement_id,entry_date,user_id $cols) "
+                               . "VALUES ($id," . $values['s_agreement_id'] 
."," . time()
+                               . "," . $this->account . " $vals)");
+
+
+                       $this->db->query("SELECT start_date FROM fm_s_agreement 
WHERE id=" . $values['s_agreement_id']);
+                       $this->db->next_record();
+                       $start_date     = $this->db->f('start_date');
+
+
+                       $this->db->query("INSERT INTO fm_s_agreement_pricing 
(agreement_id,item_id,id,current_index,this_index,cost,index_date,entry_date,user_id)
 "
+                               . "VALUES (" . $values['s_agreement_id'] . "," 
. $id .",1,1,1," . $this->floatval($values['cost']) . "," . (int)$start_date . 
"," . time()
+                               . "," . $this->account . ")");
+
+                       $receipt['s_agreement_id']= $values['s_agreement_id'];
+                       $receipt['id']= $id;
+
+                       $receipt['message'][] = array('msg'=>lang('s_agreement 
%1 has been saved',$receipt['s_agreement_id']));
+
+                       $this->db->transaction_commit();
+                       return $receipt;
+               }
+
+               function edit($values,$values_attribute='')
+               {
+//_debug_array($values);
+//_debug_array($values_attribute);
+                       $table = 'fm_s_agreement';
+
+                       $values['name'] = 
$this->db->db_addslashes($values['name']);
+
+                       if($values['member_of'])
+                       {
+                               $values['member_of']=',' . 
implode(',',$values['member_of']) . ',';
+                       }
+
+                       while (is_array($values['extra']) && 
list($column,$value) = each($values['extra']))
+                       {
+                               $value_set[$column]     = $value;
+                       }
+
+                       if (isset($values_attribute) AND 
is_array($values_attribute))
+                       {
+                               foreach($values_attribute as $entry)
+                               {
+                                       if($entry['datatype']!='AB' && 
$entry['datatype']!='VENDOR')
+                                       {
+                                               $value_set[$entry['name']]      
= $entry['value'];
+                                       }
+                               }
+                       }
+
+                       $value_set['name']      = $values['name'];
+                       $value_set['descr']     = $values['descr'];
+                       if($value_set)
+                       {
+                               $value_set      = ',' . 
$this->bocommon->validate_db_update($value_set);
+                       }
+
+                       $this->db->query("UPDATE $table set entry_date='" . 
time() . "', category='"
+                                                       . $values['cat_id'] . 
"', member_of='" . $values['member_of'] . "', start_date=" . 
intval($values['start_date']) . ", end_date=" . intval($values['end_date']) . 
", termination_date=" . intval($values['termination_date']) . ", account_id=" . 
intval($values['b_account_id']) . "$value_set WHERE id=" . 
intval($values['s_agreement_id']));
+
+                       $this->db->query("UPDATE fm_s_agreement_pricing set 
index_date=" . intval($values['start_date']) . " WHERE id=1 AND agreement_id= " 
. intval($values['s_agreement_id']));
+
+                       $receipt['s_agreement_id']= $values['s_agreement_id'];
+                       $receipt['message'][] = array('msg'=>lang('s_agreement 
%1 has been edited',$values['s_agreement_id']));
+                       return $receipt;
+               }
+
+               function edit_item($values,$values_attribute='')
+               {
+//_debug_array($values);
+//_debug_array($values_attribute);
+                       $table = 'fm_s_agreement_detail';
+
+                       while (is_array($values['extra']) && 
list($column,$value) = each($values['extra']))
+                       {
+                               $value_set[$column]     = $value;
+                       }
+
+                       if (isset($values_attribute) AND 
is_array($values_attribute))
+                       {
+                               foreach($values_attribute as $entry)
+                               {
+                                       if($entry['datatype']!='AB' && 
$entry['datatype']!='VENDOR')
+                                       {
+                                               $value_set[$entry['name']]      
= $entry['value'];
+                                       }
+                               }
+                       }
+
+                       if($values['street_name'])
+                       {
+                               $address[]= $values['street_name'];
+                               $address[]= $values['street_number'];
+                               $address        = 
$this->db->db_addslashes(implode(" ", $address));
+                       }
+
+                       if(!$address)
+                       {
+                               $address = 
$this->db->db_addslashes($values['location_name']);
+                       }
+
+                       $value_set['location_code']     = 
$values['location_code'];
+                       $value_set['cost']      = $values['cost'];
+                       $value_set['address']   = $address;
+
+                       if($value_set)
+                       {
+                               $value_set      = ',' . 
$this->bocommon->validate_db_update($value_set);
+                       }
+
+                       $this->db->query("UPDATE $table set entry_date=" . 
time() . "$value_set WHERE agreement_id=" . intval($values['s_agreement_id']) . 
' AND id=' . intval($values['id']));
+
+                       $this->db->query("UPDATE fm_s_agreement_pricing set 
cost = this_index *" . $this->floatval($values['cost']) . " WHERE 
agreement_id=" . intval($values['s_agreement_id']) . ' AND item_id=' . 
intval($values['id']));
+
+                       $receipt['s_agreement_id']= $values['s_agreement_id'];
+                       $receipt['id']= $values['id'];
+                       $receipt['message'][] = array('msg'=>lang('s_agreement 
%1 has been edited',$values['s_agreement_id']));
+                       return $receipt;
+               }
+
+               function update($values)
+               {
+//_debug_array($values);
+                       
$values['new_index']=$this->floatval($values['new_index']);
+                       $this->db->transaction_begin();
+
+                       while (is_array($values['select']) && 
list($item_id,$value) = each($values['select']))
+                       {
+
+                               $this->db->query("UPDATE fm_s_agreement_pricing 
set current_index = NULL WHERE agreement_id=" . intval($values['agreement_id']) 
. ' AND item_id=' . intval($item_id));
+
+                               $this->db->query("INSERT INTO 
fm_s_agreement_pricing 
(agreement_id,item_id,id,current_index,this_index,cost,index_date,entry_date,user_id)"
+                                       . "VALUES (" . $values['agreement_id'] 
. "," . $item_id ."," . ($values['id'][$item_id]+1) .",1,'" . 
$values['new_index'] . "','" . ($value * $values['new_index'])  . "'," . 
(int)$values['date'] . "," . time()
+                                       . "," . $this->account . ")");
+
+                       }
+
+                       $this->db->transaction_commit();
+                       $receipt['message'][] = array('msg'=>lang('s_agreement 
%1 has been updated for index',$values['agreement_id']));
+
+                       return $receipt;
+               }
+
+               function floatval($strValue)
+               {
+                       $floatValue = 
ereg_replace("(^[0-9]*)(\\.|,)([0-9]*)(.*)", "\\1.\\3", $strValue);
+                       if(!is_numeric($floatValue))
+                       {
+                               $floatValue = ereg_replace("(^[0-9]*)(.*)", 
"\\1", $strValue);
+                       }
+                       if(!is_numeric($floatValue))
+                       {
+                               $floatValue = 0;
+                       }
+                       return $floatValue;
+               }
+
+
+               function delete_last_index($s_agreement_id,$item_id)
+               {
+                       $this->db->transaction_begin();
+                       $this->db->query("SELECT max(id) as index_count FROM 
fm_s_agreement_pricing WHERE agreement_id=$s_agreement_id AND 
item_id=$item_id");
+                       $this->db->next_record();
+                       $index_count    = $this->db->f('index_count');
+                       if($index_count>1)
+                       {
+                               $this->db->query("DELETE FROM 
fm_s_agreement_pricing WHERE agreement_id=$s_agreement_id AND item_id=$item_id 
AND id=$index_count");
+                               $this->db->query("UPDATE fm_s_agreement_pricing 
set current_index = 1 WHERE agreement_id=$s_agreement_id AND item_id=$item_id 
AND id =" . ($index_count-1));
+                       }
+                       $this->db->transaction_commit();
+               }
+
+               function delete_item($s_agreement_id,$item_id)
+               {
+                       $this->db->transaction_begin();
+                       $this->db->query("DELETE FROM fm_s_agreement_detail 
WHERE agreement_id=$s_agreement_id AND id=$item_id");
+                       $this->db->query("DELETE FROM fm_s_agreement_pricing 
WHERE agreement_id=$s_agreement_id AND item_id=$item_id");
+                       $this->db->transaction_commit();
+               }
+
+
+               function delete($s_agreement_id)
+               {
+                       $table = 'fm_s_agreement';
+                       $this->db->transaction_begin();
+                       $this->db->query("DELETE FROM $table WHERE id=" . 
intval($s_agreement_id));
+                       $this->db->query("DELETE FROM fm_s_agreement_detail 
WHERE agreement_id=" . intval($s_agreement_id));
+                       $this->db->query("DELETE FROM fm_s_agreement_pricing 
WHERE agreement_id=" . intval($s_agreement_id));
+                       $this->db->query("DELETE FROM fm_orders WHERE id=" . 
intval($s_agreement_id));
+                       $this->db->transaction_commit();
+               }
+
+               function read_attrib($data)
+               {
+                       $attribute_table = 'fm_s_agreement_attribute';
+//html_print_r($data);
+                       if(is_array($data))
+                       {
+                               if ($data['start'])
+                               {
+                                       $start=$data['start'];
+                               }
+                               else
+                               {
+                                       $start=0;
+                               }
+                               $query = 
(isset($data['query'])?$data['query']:'');
+                               $sort = 
(isset($data['sort'])?$data['sort']:'DESC');
+                               $order = 
(isset($data['order'])?$data['order']:'');
+                               $allrows = 
(isset($data['allrows'])?$data['allrows']:'');
+                               $column_list = 
(isset($data['column_list'])?$data['column_list']:'');
+                       }
+
+                       $where = 'WHERE';
+                       if ($column_list)
+                       {
+                               $filtermethod = " $where list !=1 or list is 
null";
+                               $where = 'AND';
+                       }
+                       if ($this->role=='detail')
+                       {
+                               $filtermethod .= " $where attrib_detail=2 ";
+                       }
+                       else
+                       {
+                               $filtermethod .= " $where attrib_detail=1 ";
+                       }
+
+                       $where = 'AND';
+
+                       if ($order)
+                       {
+                               $ordermethod = " order by $order $sort";
+
+                       }
+                       else
+                       {
+                               $ordermethod = ' order by attrib_sort asc';
+                       }
+
+                       if($query)
+                       {
+                               $query = ereg_replace("'",'',$query);
+                               $query = ereg_replace('"','',$query);
+
+                               $querymethod = " $where 
($attribute_table.input_text $this->like '%$query%' or 
$attribute_table.column_name $this->like '%$query%')";
+                       }
+
+                       $sql = "SELECT * FROM $attribute_table $filtermethod 
$querymethod";
+
+//echo $sql;
+                       $this->db2->query($sql,__LINE__,__FILE__);
+                       $this->total_records = $this->db2->num_rows();
+                       if(!$allrows)
+                       {
+                               $this->db->limit_query($sql . 
$ordermethod,$start,__LINE__,__FILE__);
+                       }
+                       else
+                       {
+                               $this->db->query($sql . 
$ordermethod,__LINE__,__FILE__);
+                       }
+
+                       while ($this->db->next_record())
+                       {
+                               $attrib[] = array
+                               (
+                                       'id'                    => 
$this->db->f('id'),
+                                       'attrib_sort'   => 
$this->db->f('attrib_sort'),
+                                       'list'                  => 
$this->db->f('list'),
+                                       'lookup_form'   => 
$this->db->f('lookup_form'),
+                                       'column_name'   => 
$this->db->f('column_name'),
+                                       'name'                  => 
$this->db->f('input_text'),
+                                       'size'                  => 
$this->db->f('size'),
+                                       'statustext'    => 
$this->db->f('statustext'),
+                                       'input_text'    => 
$this->db->f('input_text'),
+                                       'type_name'             => 
$this->db->f('type'),
+                                       'datatype'              => 
$this->db->f('datatype'),
+                                       'search'                => 
$this->db->f('search')
+                               );
+                       }
+                       return $attrib;
+               }
+
+               function read_single_attrib($id)
+               {
+                       $attribute_table = 'fm_s_agreement_attribute';
+
+                       if ($this->role=='detail')
+                       {
+                               $filtermethod = " AND attrib_detail=2 ";
+                       }
+                       else
+                       {
+                               $filtermethod = " AND attrib_detail=1 ";
+                       }
+
+                       $sql = "SELECT * FROM $attribute_table where id=$id 
$filtermethod";
+
+                       $this->db->query($sql);
+
+                       if($this->db->next_record())
+                       {
+                               $attrib['id']                                   
        = $this->db->f('id');
+                               $attrib['column_name']                          
= $this->db->f('column_name');
+                               $attrib['input_text']                           
= $this->db->f('input_text');
+                               $attrib['statustext']                           
= $this->db->f('statustext');
+                               $attrib['column_info']['precision']     = 
$this->db->f('precision_');
+                               $attrib['column_info']['scale']         = 
$this->db->f('scale');
+                               $attrib['column_info']['default']       = 
$this->db->f('default_value');
+                               $attrib['column_info']['nullable']      = 
$this->db->f('nullable');
+                               $attrib['column_info']['type']          = 
$this->db->f('datatype');
+                               $attrib['type_name']                            
= $this->db->f('type_name');
+                               $attrib['lookup_form']                          
= $this->db->f('lookup_form');
+                               $attrib['list']                                 
        = $this->db->f('list');
+                               $attrib['search']                               
        = $this->db->f('search');
+                               if($this->db->f('datatype')=='R' || 
$this->db->f('datatype')=='CH' || $this->db->f('datatype')=='LB')
+                               {
+                                       $attrib['choice'] = 
$this->read_attrib_choice($id);
+                               }
+
+                               return $attrib;
+                       }
+               }
+
+               function read_attrib_choice($attrib_id)
+               {
+                       $choice_table = 'fm_s_agreement_choice';
+
+                       if ($this->role=='detail')
+                       {
+                               $filtermethod = " AND attrib_detail=2 ";
+                       }
+                       else
+                       {
+                               $filtermethod = " AND attrib_detail=1 ";
+                       }
+
+                       $sql = "SELECT * FROM $choice_table WHERE 
attrib_id=$attrib_id $filtermethod";
+                       $this->db->query($sql);
+
+                       while ($this->db->next_record())
+                       {
+                               $choice[] = array
+                               (
+                                       'id'    => $this->db->f('id'),
+                                       'value' => $this->db->f('value')
+                               );
+
+                       }
+                       return $choice;
+               }
+
+               function add_attrib($attrib)
+               {
+                       $attribute_table = 'fm_s_agreement_attribute';
+                       $attrib['column_name'] = 
$this->db->db_addslashes($attrib['column_name']);
+                       $attrib['input_text'] = 
$this->db->db_addslashes($attrib['input_text']);
+                       $attrib['statustext'] = 
$this->db->db_addslashes($attrib['statustext']);
+                       $attrib['default'] = 
$this->db->db_addslashes($attrib['default']);
+                       $attrib['id'] = 
$this->bocommon->next_id($attribute_table, array('attrib_detail'=>!!$this->role 
+1));
+
+                       if($this->role=='detail')
+                       {
+                               $filtermethod= 'WHERE attrib_detail=2';
+                               $table = 'fm_s_agreement_detail';
+                       }
+                       else
+                       {
+                               $filtermethod= 'WHERE attrib_detail=1';
+                               $table = 'fm_s_agreement';
+                       }
+                       $sql = "SELECT max(attrib_sort) as max_sort FROM 
$attribute_table $filtermethod";
+                       $this->db->query($sql);
+                       $this->db->next_record();
+                       $attrib_sort    = $this->db->f('max_sort')+1;
+
+                       $values= array(
+                               !!$this->role +1,
+                               $attrib['id'],
+                               $attrib['column_name'],
+                               $attrib['input_text'],
+                               $attrib['statustext'],
+                               $attrib['lookup_form'],
+                               $attrib['search'],
+                               $attrib['list'],
+                               $attrib_sort,
+                               $attrib['column_info']['type'],
+                               $attrib['column_info']['precision'],
+                               $attrib['column_info']['scale'],
+                               $attrib['column_info']['default'],
+                               $attrib['column_info']['nullable']
+                               );
+
+                       $values = $this->bocommon->validate_db_insert($values);
+
+                       $this->db->transaction_begin();
+
+                       $this->db->query("INSERT INTO $attribute_table 
(attrib_detail,id,column_name, input_text, 
statustext,lookup_form,search,list,attrib_sort,datatype,precision_,scale,default_value,nullable)
 "
+                               . "VALUES ($values)");
+
+                       $receipt['id']= $attrib['id'];
+
+                       if($attrib['column_info']['type']=='email' && 
!$attrib['column_info']['precision'])
+                       {
+                               $attrib['column_info']['precision']=64;
+                       }
+
+                       $attrib['column_info']['type']  = 
$this->bocommon->translate_datatype_insert($attrib['column_info']['type']);
+
+                       if($attrib['column_info']['type']=='int' && 
!$attrib['column_info']['precision'])
+                       {
+                               $attrib['column_info']['precision']=4;
+                       }
+
+                       if(!$attrib['column_info']['default'])
+                       {
+                               unset($attrib['column_info']['default']);
+                       }
+
+                       $this->init_process();
+
+                       
if($this->oProc->AddColumn($table,$attrib['column_name'], 
$attrib['column_info']))
+                       {
+                               $receipt['message'][] = array('msg'     => 
lang('Attribute has been saved')     );
+                               $this->db->transaction_commit();
+
+                       }
+                       else
+                       {
+                               $receipt['error'][] = array('msg'       => 
lang('column could not be added')    );
+                               if($this->db->Transaction)
+                               {
+                                       $GLOBALS['phpgw']->db->rollbacktrans();
+                               }
+                               else
+                               {
+                                       $GLOBALS['phpgw']->db->Execute("DELETE 
FROM $attribute_table WHERE id='" . $receipt['id'] . "'");
+                                       unset($receipt['id']);
+
+                               }
+                       }
+
+                       return $receipt;
+               }
+
+               function init_process()
+               {
+                       $this->oProc                                            
= 
CreateObject('phpgwapi.schema_proc',$GLOBALS['phpgw_info']['server']['db_type']);
+                       $this->oProc->m_odb                                     
= $this->db;
+                       $this->oProc->m_odb->Halt_On_Error      = 'report';
+               }
+
+               function edit_attrib($attrib)
+               {
+                       $attribute_table = 'fm_s_agreement_attribute';
+                       $table = 'fm_s_agreement';
+
+                       $attrib['column_name'] = 
$this->db->db_addslashes($attrib['column_name']);
+                       $attrib['input_text'] = 
$this->db->db_addslashes($attrib['input_text']);
+                       $attrib['statustext'] = 
$this->db->db_addslashes($attrib['statustext']);
+                       $attrib['default'] = 
$this->db->db_addslashes($attrib['default']);
+
+                       $choice_table = 'fm_s_agreement_choice';
+
+                       if($this->role=='detail')
+                       {
+                               $filtermethod= ' AND attrib_detail=2';
+                               $table = 'fm_s_agreement_detail';
+                       }
+                       else
+                       {
+                               $filtermethod= ' AND attrib_detail=1';
+                               $table = 'fm_s_agreement';
+                       }
+
+                       $this->db->query("SELECT column_name FROM 
$attribute_table WHERE id='" . $attrib['id']. "' $filtermethod");
+                       $this->db->next_record();
+                       $OldColumnName          = $this->db->f('column_name');
+
+                       $this->db->transaction_begin();
+
+                       $attrib_type=$attrib['column_info']['type'];
+                       if(($OldColumnName !=$attrib['column_name']) || 
($attrib['column_info']['type'] != $this->db->f('datatype')))
+                       {
+                               $value_set=array(
+                                       'column_name'           => 
$attrib['column_name'],
+                                       'input_text'            => 
$attrib['input_text'],
+                                       'statustext'            => 
$attrib['statustext'],
+                                       'lookup_form'           => 
$attrib['lookup_form'],
+                                       'search'                        => 
$attrib['search'],
+                                       'list'                          => 
$attrib['list'],
+                                       'datatype'                      => 
$attrib['column_info']['type'],
+                                       'precision_'            => 
$attrib['column_info']['precision'],
+                                       'scale'                         => 
$attrib['column_info']['scale'],
+                                       'default_value'         => 
$attrib['column_info']['default'],
+                                       'nullable'                      => 
$attrib['column_info']['nullable']
+                                       );
+
+                               $value_set      = 
$this->bocommon->validate_db_update($value_set);
+
+                               $this->db->query("UPDATE $attribute_table set 
$value_set WHERE id=" . $attrib['id'] . $filtermethod);
+
+                               if($attrib['column_info']['type']=='email' && 
!$attrib['column_info']['precision'])
+                               {
+                                       $attrib['column_info']['precision']=64;
+                               }
+
+                               $attrib['column_info']['type']  = 
$this->bocommon->translate_datatype_insert($attrib['column_info']['type']);
+
+                               if($attrib['column_info']['type']=='int' && 
!$attrib['column_info']['precision'])
+                               {
+                                       $attrib['column_info']['precision']=4;
+                               }
+
+                               if(!$attrib['column_info']['default'])
+                               {
+                                       
unset($attrib['column_info']['default']);
+                               }
+
+                               $this->init_process();
+
+                               
$this->oProc->AlterColumn($table,$OldColumnName,$attrib['column_name'],$attrib['column_info']);
+
+                       }
+
+
+                       if($attrib['new_choice'])
+                       {
+                               $this->db->query("SELECT max(id) as id FROM 
$choice_table WHERE attrib_id='" . $attrib['id']. "' $filtermethod");
+                               $this->db->next_record();
+                               $choice_id              = $this->db->f('id')+1;
+
+       //                      $choice_id = 
$this->bocommon->next_id($choice_table 
,array('attrib_detail'=>2,'attrib_id'=>$attrib['id']));
+
+                               $values= array(
+                                       $attrib['id'],
+                                       $choice_id,
+                                       !!$this->role +1,
+                                       $attrib['new_choice']
+                                       );
+
+                               $values = 
$this->bocommon->validate_db_insert($values);
+
+                               $this->db->query("INSERT INTO $choice_table 
(attrib_id,id,attrib_detail,value) "
+                               . "VALUES ($values)");
+                       }
+
+
+                       if($attrib['delete_choice'])
+                       {
+                               for 
($i=0;$i<count($attrib['delete_choice']);$i++)
+                               {
+                                       $this->db->query("DELETE FROM 
$choice_table WHERE  attrib_id=" . $attrib['id']  ." AND id=" . 
$attrib['delete_choice'][$i] . $filtermethod);
+                               }
+                       }
+
+//html_print_r($attrib_type);
+                       if($attrib_type!='R' && $attrib_type!='CH' && 
$attrib_type!='LB')
+                       {
+                               $this->db->query("DELETE FROM $choice_table 
WHERE  attrib_id=" . $attrib['id'] . $filtermethod);
+                       }
+
+
+                       $this->db->transaction_commit();
+
+                       $receipt['message'][] = array('msg'     => 
lang('Attribute has been edited'));
+
+
+                       return $receipt;
+
+               }
+               function resort_attrib($data)
+               {
+//html_print_r($data);
+                       $attribute_table = 'fm_s_agreement_attribute';
+                       if(is_array($data))
+                       {
+                               $resort = 
(isset($data['resort'])?$data['resort']:'up');
+                               $id = (isset($data['id'])?$data['id']:'');
+                       }
+
+                       $sql = "SELECT attrib_sort FROM $attribute_table where 
id=$id";
+                       $this->db->query($sql);
+                       $this->db->next_record();
+                       $attrib_sort    = $this->db->f('attrib_sort');
+                       $sql = "SELECT max(attrib_sort) as max_sort FROM 
$attribute_table";
+                       $this->db->query($sql);
+                       $this->db->next_record();
+                       $max_sort       = $this->db->f('max_sort');
+                       switch($resort)
+                       {
+                               case 'up':
+                                       if($attrib_sort>1)
+                                       {
+                                               $sql = "UPDATE $attribute_table 
set attrib_sort=$attrib_sort WHERE attrib_sort =" . ($attrib_sort-1);
+                                               $this->db->query($sql);
+                                               $sql = "UPDATE $attribute_table 
set attrib_sort=" . ($attrib_sort-1) ." WHERE id=$id";
+                                               $this->db->query($sql);
+                                       }
+                                       break;
+                               case 'down':
+                                       if($max_sort > $attrib_sort)
+                                       {
+                                               $sql = "UPDATE $attribute_table 
set attrib_sort=$attrib_sort WHERE attrib_sort =" . ($attrib_sort+1);
+                                               $this->db->query($sql);
+                                               $sql = "UPDATE $attribute_table 
set attrib_sort=" . ($attrib_sort+1) ." WHERE id=$id";
+                                               $this->db->query($sql);
+                                       }
+                                       break;
+                               default:
+                                       return;
+                                       break;
+                       }
+               }
+               function delete_attrib($attrib_id)
+               {
+                       $table = 'fm_s_agreement';
+                       $attribute_table = 'fm_s_agreement_attribute';
+                       $this->init_process();
+
+                       $sql = "SELECT * FROM $attribute_table WHERE 
id=$attrib_id";
+
+                       $this->db->query($sql);
+                       $this->db->next_record();
+                       $ColumnName             = $this->db->f('column_name');
+
+                       if($this->oProc->DropColumn($table,'', $ColumnName))
+                       {
+                               $sql = "SELECT attrib_sort FROM 
$attribute_table where id=$attrib_id";
+                               $this->db->query($sql);
+                               $this->db->next_record();
+                               $attrib_sort    = $this->db->f('attrib_sort');
+                               $sql2 = "SELECT max(attrib_sort) as max_sort 
FROM $attribute_table";
+                               $this->db->query($sql2);
+                               $this->db->next_record();
+                               $max_sort       = $this->db->f('max_sort');
+                               if($max_sort>$attrib_sort)
+                               {
+                                       $sql = "UPDATE $attribute_table set 
attrib_sort=attrib_sort-1 WHERE attrib_sort > $attrib_sort";
+                                       $this->db->query($sql);
+                               }
+
+                               $this->db->query("DELETE FROM $attribute_table 
WHERE id=$attrib_id");
+                       }
+                       else
+                       {
+                               $receipt['error'][] = array('msg'       => 
lang('Attribute has NOT been deleted'));
+                       }
+
+                       return $receipt;
+               }
+
+               function request_next_id()
+               {
+                       $this->db->query("SELECT max(id) as id FROM 
fm_s_agreement");
+                       $this->db->next_record();
+                       $next_id= $this->db->f('id')+1;
+                       return $next_id;
+               }
+       }
+?>
Index: property/inc/class.sostandard_2.inc.php
diff -u property/inc/class.sostandard_2.inc.php:1.7 
property/inc/class.sostandard_2.inc.php:1.8
--- property/inc/class.sostandard_2.inc.php:1.7 Fri Jan 27 14:05:43 2006
+++ property/inc/class.sostandard_2.inc.php     Mon Jan 30 22:14:19 2006
@@ -1,179 +1,180 @@
-<?php
-       /**
-       * phpGroupWare - property: a Facilities Management System.
-       *
-       * @author Sigurd Nes <address@hidden>
-       * @copyright Copyright (C) 2003-2005 Free Software Foundation, Inc. 
http://www.fsf.org/
-       * @license http://www.gnu.org/licenses/gpl.html GNU General Public 
License
-       * @internal Development of this application was funded by 
http://www.bergen.kommune.no/bbb_/ekstern/
-       * @package property
-       * @subpackage admin
-       * @version $Id: class.sostandard_2.inc.php,v 1.7 2006/01/27 14:05:43 
sigurdne Exp $
-       */
-
-       /**
-        * Description
-        * @package property
-        */
-
-       class sostandard_2
-       {
-
-               function sostandard_2()
-               {
-                       $this->currentapp       = 
$GLOBALS['phpgw_info']['flags']['currentapp'];
-                       $this->account  = 
$GLOBALS['phpgw_info']['user']['account_id'];
-                       $this->bocommon         = 
CreateObject($this->currentapp.'.bocommon');
+<?php
+       /**
+       * phpGroupWare - property: a Facilities Management System.
+       *
+       * @author Sigurd Nes <address@hidden>
+       * @copyright Copyright (C) 2003-2005 Free Software Foundation, Inc. 
http://www.fsf.org/
+       * @license http://www.gnu.org/licenses/gpl.html GNU General Public 
License
+       * @internal Development of this application was funded by 
http://www.bergen.kommune.no/bbb_/ekstern/
+       * @package property
+       * @subpackage admin
+       * @version $Id: class.sostandard_2.inc.php,v 1.8 2006/01/30 22:14:19 
sigurdne Exp $
+       */
+
+       /**
+        * Description
+        * @package property
+        */
+
+       class sostandard_2
+       {
+
+               function sostandard_2()
+               {
+                       $this->currentapp       = 
$GLOBALS['phpgw_info']['flags']['currentapp'];
+                       $this->account  = 
$GLOBALS['phpgw_info']['user']['account_id'];
+                       $this->bocommon         = 
CreateObject($this->currentapp.'.bocommon');
                        $this->db               = $this->bocommon->new_db();
-                       $this->db2              = $this->bocommon->new_db();
-                       $this->join                     = $this->bocommon->join;
-                       $this->like                     = $this->bocommon->like;
-               }
-
-               function read($data)
-               {
-                       if(is_array($data))
-                       {
-                               if ($data['start'])
-                               {
-                                       $start=$data['start'];
-                               }
-                               else
-                               {
-                                       $start=0;
-                               }
-                               $query          = 
(isset($data['query'])?$data['query']:'');
-                               $sort           = 
(isset($data['sort'])?$data['sort']:'DESC');
-                               $order          = 
(isset($data['order'])?$data['order']:'');
-                               $type           = 
(isset($data['type'])?$data['type']:0);
-                               $allrows        = 
(isset($data['allrows'])?$data['allrows']:'');
-                       }
-
-                       if ($order)
-                       {
-                               $ordermethod = " order by $order $sort";
-
-                       }
-                       else
-                       {
-                               $ordermethod = ' order by id asc';
-                       }
-
-                       $table = $this->select_table($type);
-
-                       if($query)
-                       {
-                               $query = ereg_replace("'",'',$query);
-                               $query = ereg_replace('"','',$query);
-
-                               $querymethod = " where id $this->like 
'%$query%' or descr $this->like '%$query%'";
-                       }
-
-                       $sql = "SELECT * FROM $table $querymethod";
-
-                       $this->db2->query($sql,__LINE__,__FILE__);
-                       $this->total_records = $this->db2->num_rows();
-
-                       if(!$allrows)
-                       {
-                               $this->db->limit_query($sql . 
$ordermethod,$start,__LINE__,__FILE__);
-                       }
-                       else
-                       {
-                               $this->db->query($sql . 
$ordermethod,__LINE__,__FILE__);
-                       }
-
-                       while ($this->db->next_record())
-                       {
-                               $standard[] = array
-                               (
-                                       'id'    => $this->db->f('id'),
-                                       'descr' => $this->db->f('descr')
-                               );
-                       }
-                       return $standard;
-               }
-
-
-               function select_table($type)
-               {
-
-                       switch($type)
-                       {
-                               case 'workorder_status':
-                                       $table='fm_workorder_status';
-                                       break;
-                               case 'request_status':
-                                       $table='fm_request_status';
-                                       break;
-                               case 'agreement_status':
-                                       $table='fm_agreement_status';
-                                       break;
-                               case 'building_part':
-                                       $table='fm_building_part';
-                                       break;
-                               case 'document_status':
-                                       $table='fm_document_status';
-                                       break;
-                               case 'unit':
-                                       $table='fm_standard_unit';
-                                       break;
-                       }
-
-                       return $table;
-               }
-
-
-               function read_single($id,$type)
-               {
-
-                       $table = $this->select_table($type);
-
-                       $sql = "SELECT * FROM $table  where id='$id'";
-
-                       $this->db->query($sql,__LINE__,__FILE__);
-
-                       if ($this->db->next_record())
-                       {
-                               $standard['id']                 = 
$this->db->f('id');
-                               $standard['descr']                              
= $this->db->f('descr');
-
-                               return $standard;
-                       }
-               }
-
-               function add($standard,$type)
-               {
-                       $table = $this->select_table($type);
-
-                       $standard['descr'] = 
$this->db->db_addslashes($standard['descr']);
-
-                       $this->db->query("INSERT INTO $table (id, descr) "
-                               . "VALUES ('" . $standard['id'] . "','" . 
$standard['descr']. "')",__LINE__,__FILE__);
-
-                       $receipt['message'][] = array('msg' => lang('standard 
has been saved'));
-                       return $receipt;
-               }
-
-               function edit($standard,$type)
-               {
-
-                       $table = $this->select_table($type);
-
-                       $standard['descr'] = 
$this->db->db_addslashes($standard['descr']);
-
-                       $this->db->query("UPDATE $table set descr='" . 
$standard['descr']
-                                                       . "' WHERE id='" . 
$standard['id']. "'",__LINE__,__FILE__);
-
-                       $receipt['message'][] = array('msg' => lang('standard 
has been edited'));
-                       return $receipt;
-               }
-
-               function delete($id,$type)
-               {
-                       $table = $this->select_table($type);
-
-                       $this->db->query("DELETE FROM $table WHERE id='" . $id 
. "'",__LINE__,__FILE__);
-               }
-       }
-?>
+                       $this->db2              = $this->bocommon->new_db();
+
+                       $this->join                     = $this->bocommon->join;
+                       $this->like                     = $this->bocommon->like;
+               }
+
+               function read($data)
+               {
+                       if(is_array($data))
+                       {
+                               if ($data['start'])
+                               {
+                                       $start=$data['start'];
+                               }
+                               else
+                               {
+                                       $start=0;
+                               }
+                               $query          = 
(isset($data['query'])?$data['query']:'');
+                               $sort           = 
(isset($data['sort'])?$data['sort']:'DESC');
+                               $order          = 
(isset($data['order'])?$data['order']:'');
+                               $type           = 
(isset($data['type'])?$data['type']:0);
+                               $allrows        = 
(isset($data['allrows'])?$data['allrows']:'');
+                       }
+
+                       if ($order)
+                       {
+                               $ordermethod = " order by $order $sort";
+
+                       }
+                       else
+                       {
+                               $ordermethod = ' order by id asc';
+                       }
+
+                       $table = $this->select_table($type);
+
+                       if($query)
+                       {
+                               $query = ereg_replace("'",'',$query);
+                               $query = ereg_replace('"','',$query);
+
+                               $querymethod = " where id $this->like 
'%$query%' or descr $this->like '%$query%'";
+                       }
+
+                       $sql = "SELECT * FROM $table $querymethod";
+
+                       $this->db2->query($sql,__LINE__,__FILE__);
+                       $this->total_records = $this->db2->num_rows();
+
+                       if(!$allrows)
+                       {
+                               $this->db->limit_query($sql . 
$ordermethod,$start,__LINE__,__FILE__);
+                       }
+                       else
+                       {
+                               $this->db->query($sql . 
$ordermethod,__LINE__,__FILE__);
+                       }
+
+                       while ($this->db->next_record())
+                       {
+                               $standard[] = array
+                               (
+                                       'id'    => $this->db->f('id'),
+                                       'descr' => $this->db->f('descr')
+                               );
+                       }
+                       return $standard;
+               }
+
+
+               function select_table($type)
+               {
+
+                       switch($type)
+                       {
+                               case 'workorder_status':
+                                       $table='fm_workorder_status';
+                                       break;
+                               case 'request_status':
+                                       $table='fm_request_status';
+                                       break;
+                               case 'agreement_status':
+                                       $table='fm_agreement_status';
+                                       break;
+                               case 'building_part':
+                                       $table='fm_building_part';
+                                       break;
+                               case 'document_status':
+                                       $table='fm_document_status';
+                                       break;
+                               case 'unit':
+                                       $table='fm_standard_unit';
+                                       break;
+                       }
+
+                       return $table;
+               }
+
+
+               function read_single($id,$type)
+               {
+
+                       $table = $this->select_table($type);
+
+                       $sql = "SELECT * FROM $table  where id='$id'";
+
+                       $this->db->query($sql,__LINE__,__FILE__);
+
+                       if ($this->db->next_record())
+                       {
+                               $standard['id']                 = 
$this->db->f('id');
+                               $standard['descr']                              
= $this->db->f('descr');
+
+                               return $standard;
+                       }
+               }
+
+               function add($standard,$type)
+               {
+                       $table = $this->select_table($type);
+
+                       $standard['descr'] = 
$this->db->db_addslashes($standard['descr']);
+
+                       $this->db->query("INSERT INTO $table (id, descr) "
+                               . "VALUES ('" . $standard['id'] . "','" . 
$standard['descr']. "')",__LINE__,__FILE__);
+
+                       $receipt['message'][] = array('msg' => lang('standard 
has been saved'));
+                       return $receipt;
+               }
+
+               function edit($standard,$type)
+               {
+
+                       $table = $this->select_table($type);
+
+                       $standard['descr'] = 
$this->db->db_addslashes($standard['descr']);
+
+                       $this->db->query("UPDATE $table set descr='" . 
$standard['descr']
+                                                       . "' WHERE id='" . 
$standard['id']. "'",__LINE__,__FILE__);
+
+                       $receipt['message'][] = array('msg' => lang('standard 
has been edited'));
+                       return $receipt;
+               }
+
+               function delete($id,$type)
+               {
+                       $table = $this->select_table($type);
+
+                       $this->db->query("DELETE FROM $table WHERE id='" . $id 
. "'",__LINE__,__FILE__);
+               }
+       }
+?>
Index: property/inc/class.sostandard_3.inc.php
diff -u property/inc/class.sostandard_3.inc.php:1.8 
property/inc/class.sostandard_3.inc.php:1.9
--- property/inc/class.sostandard_3.inc.php:1.8 Fri Jan 27 14:05:43 2006
+++ property/inc/class.sostandard_3.inc.php     Mon Jan 30 22:14:19 2006
@@ -1,169 +1,163 @@
-<?php
-       /**
-       * phpGroupWare - property: a Facilities Management System.
-       *
-       * @author Sigurd Nes <address@hidden>
-       * @copyright Copyright (C) 2003-2005 Free Software Foundation, Inc. 
http://www.fsf.org/
-       * @license http://www.gnu.org/licenses/gpl.html GNU General Public 
License
-       * @internal Development of this application was funded by 
http://www.bergen.kommune.no/bbb_/ekstern/
-       * @package property
-       * @subpackage admin
-       * @version $Id: class.sostandard_3.inc.php,v 1.8 2006/01/27 14:05:43 
sigurdne Exp $
-       */
-
-       /**
-        * Description
-        * @package property
-        */
-
-       class sostandard_3
-       {
-
-               function sostandard_3()
-               {
-                       $this->currentapp       = 
$GLOBALS['phpgw_info']['flags']['currentapp'];
-                       $this->account          = 
$GLOBALS['phpgw_info']['user']['account_id'];
-                       $this->bocommon         = 
CreateObject($this->currentapp.'.bocommon');
+<?php
+       /**
+       * phpGroupWare - property: a Facilities Management System.
+       *
+       * @author Sigurd Nes <address@hidden>
+       * @copyright Copyright (C) 2003-2005 Free Software Foundation, Inc. 
http://www.fsf.org/
+       * @license http://www.gnu.org/licenses/gpl.html GNU General Public 
License
+       * @internal Development of this application was funded by 
http://www.bergen.kommune.no/bbb_/ekstern/
+       * @package property
+       * @subpackage admin
+       * @version $Id: class.sostandard_3.inc.php,v 1.9 2006/01/30 22:14:19 
sigurdne Exp $
+       */
+
+       /**
+        * Description
+        * @package property
+        */
+
+       class sostandard_3
+       {
+
+               function sostandard_3()
+               {
+                       $this->currentapp       = 
$GLOBALS['phpgw_info']['flags']['currentapp'];
+                       $this->account          = 
$GLOBALS['phpgw_info']['user']['account_id'];
+                       $this->bocommon         = 
CreateObject($this->currentapp.'.bocommon');
                        $this->db               = $this->bocommon->new_db();
                        $this->db2              = $this->bocommon->new_db();
-                       $this->socommon         = 
CreateObject($this->currentapp.'.socommon');
-                       $this->join                     = $this->bocommon->join;
-                       $this->like                     = $this->bocommon->like;
-               }
-
-               function read($data)
-               {
-                       if(is_array($data))
-                       {
-                               if ($data['start'])
-                               {
-                                       $start=$data['start'];
-                               }
-                               else
-                               {
-                                       $start=0;
-                               }
-                               $query = 
(isset($data['query'])?$data['query']:'');
-                               $sort = 
(isset($data['sort'])?$data['sort']:'DESC');
-                               $order = 
(isset($data['order'])?$data['order']:'');
-                               $type = (isset($data['type'])?$data['type']:0);
-                       }
-
-                       if ($order)
-                       {
-                               $ordermethod = " order by $order $sort";
-
-                       }
-                       else
-                       {
-                               $ordermethod = ' order by id asc';
-                       }
-
-                       $table = $this->select_table($type);
-
-                       if($query)
-                       {
-                               $query = ereg_replace("'",'',$query);
-                               $query = ereg_replace('"','',$query);
-
-                               $querymethod = " where id $this->like 
'%$query%' or descr $this->like '%$query%'";
-                       }
-
-                       $sql = "SELECT * FROM $table $querymethod";
-
-                       $this->db2->query($sql,__LINE__,__FILE__);
-                       $this->total_records = $this->db2->num_rows();
-                       $this->db->limit_query($sql . 
$ordermethod,$start,__LINE__,__FILE__);
-
-                       while ($this->db->next_record())
-                       {
-                               $standard[] = array
-                               (
-                                       'id'    => $this->db->f('id'),
-                                       'num'   => $this->db->f('num'),
-                                       'descr' => $this->db->f('descr')
-                               );
-                       }
-                       return $standard;
-               }
-
-
-               function select_table($type)
-               {
-
-                       switch($type)
-                       {
-                               case 'branch':
-                                       $table='fm_branch';
-                                       break;
-                               case 'key_location':
-                                       $table='fm_key_loc';
-                                       break;
-                               case 'async':
-                                       $table='fm_async_method';
-                                       break;
-                       }
-
-
-                       return $table;
-               }
-
-
-               function read_single($id,$type)
-               {
-
-                       $table = $this->select_table($type);
-
-                       $sql = "SELECT * FROM $table  where id='$id'";
-
-                       $this->db->query($sql,__LINE__,__FILE__);
-
-                       if ($this->db->next_record())
-                       {
-                               $standard['id']                 = 
$this->db->f('id');
-                               $standard['num']                = 
$this->db->f('num');
-                               $standard['descr']              = 
$this->db->f('descr');
-
-                               return $standard;
-                       }
-               }
-
-               function add($standard,$type)
-               {
-                       $table = $this->select_table($type);
-
-                       $standard['id'] = $this->socommon->next_id($table);
-                       $standard['num'] = 
$this->db->db_addslashes($standard['descr']);
-                       $standard['descr'] = 
$this->db->db_addslashes($standard['descr']);
-
-                       $this->db->query("INSERT INTO $table (id, num, descr) "
-                               . "VALUES ('" . $standard['id'] . "','" . 
$standard['num'] . "','" . $standard['descr']. "')",__LINE__,__FILE__);
-
-                       $receipt['message'][] = array('msg' => lang('standard 
has been saved'));
-
-                       return $receipt;
-               }
-
-               function edit($standard,$type)
-               {
-
-                       $table = $this->select_table($type);
-
-                       $standard['num'] = 
$this->db->db_addslashes($standard['num']);
-                       $standard['descr'] = 
$this->db->db_addslashes($standard['descr']);
-
-                       $this->db->query("UPDATE $table set descr='" . 
$standard['descr'] . "', num='". $standard['num']
-                                                       . "' WHERE id='" . 
$standard['id']. "'",__LINE__,__FILE__);
-
-                       $receipt['message'][] = array('msg' =>lang('standard 
has been edited'));
-                       return $receipt;
-               }
-
-               function delete($id,$type)
-               {
-                       $table = $this->select_table($type);
-
-                       $this->db->query("DELETE FROM $table WHERE id='" . $id 
. "'",__LINE__,__FILE__);
-               }
-       }
-?>
+                       $this->socommon         = 
CreateObject($this->currentapp.'.socommon');
+
+                       $this->join                     = $this->bocommon->join;
+                       $this->like                     = $this->bocommon->like;
+               }
+
+               function read($data)
+               {
+                       if(is_array($data))
+                       {
+                               if ($data['start'])
+                               {
+                                       $start=$data['start'];
+                               }
+                               else
+                               {
+                                       $start=0;
+                               }
+                               $query = 
(isset($data['query'])?$data['query']:'');
+                               $sort = 
(isset($data['sort'])?$data['sort']:'DESC');
+                               $order = 
(isset($data['order'])?$data['order']:'');
+                               $type = (isset($data['type'])?$data['type']:0);
+                       }
+
+                       if ($order)
+                       {
+                               $ordermethod = " order by $order $sort";
+
+                       }
+                       else
+                       {
+                               $ordermethod = ' order by id asc';
+                       }
+
+                       $table = $this->select_table($type);
+
+                       if($query)
+                       {
+                               $query = ereg_replace("'",'',$query);
+                               $query = ereg_replace('"','',$query);
+
+                               $querymethod = " where id $this->like 
'%$query%' or descr $this->like '%$query%'";
+                       }
+
+                       $sql = "SELECT * FROM $table $querymethod";
+
+                       $this->db2->query($sql,__LINE__,__FILE__);
+                       $this->total_records = $this->db2->num_rows();
+                       $this->db->limit_query($sql . 
$ordermethod,$start,__LINE__,__FILE__);
+
+                       while ($this->db->next_record())
+                       {
+                               $standard[] = array
+                               (
+                                       'id'    => $this->db->f('id'),
+                                       'num'   => $this->db->f('num'),
+                                       'descr' => $this->db->f('descr')
+                               );
+                       }
+                       return $standard;
+               }
+
+               function select_table($type)
+               {
+                       switch($type)
+                       {
+                               case 'branch':
+                                       $table='fm_branch';
+                                       break;
+                               case 'key_location':
+                                       $table='fm_key_loc';
+                                       break;
+                               case 'async':
+                                       $table='fm_async_method';
+                                       break;
+                       }
+                       return $table;
+               }
+
+               function read_single($id,$type)
+               {
+
+                       $table = $this->select_table($type);
+
+                       $sql = "SELECT * FROM $table  where id='$id'";
+
+                       $this->db->query($sql,__LINE__,__FILE__);
+
+                       if ($this->db->next_record())
+                       {
+                               $standard['id']                 = 
$this->db->f('id');
+                               $standard['num']                = 
$this->db->f('num');
+                               $standard['descr']              = 
$this->db->f('descr');
+
+                               return $standard;
+                       }
+               }
+
+               function add($standard,$type)
+               {
+                       $table = $this->select_table($type);
+
+                       $standard['id'] = $this->socommon->next_id($table);
+                       $standard['num'] = 
$this->db->db_addslashes($standard['descr']);
+                       $standard['descr'] = 
$this->db->db_addslashes($standard['descr']);
+
+                       $this->db->query("INSERT INTO $table (id, num, descr) "
+                               . "VALUES ('" . $standard['id'] . "','" . 
$standard['num'] . "','" . $standard['descr']. "')",__LINE__,__FILE__);
+
+                       $receipt['message'][] = array('msg' => lang('standard 
has been saved'));
+
+                       return $receipt;
+               }
+
+               function edit($standard,$type)
+               {
+                       $table = $this->select_table($type);
+
+                       $standard['num'] = 
$this->db->db_addslashes($standard['num']);
+                       $standard['descr'] = 
$this->db->db_addslashes($standard['descr']);
+
+                       $this->db->query("UPDATE $table set descr='" . 
$standard['descr'] . "', num='". $standard['num']
+                                                       . "' WHERE id='" . 
$standard['id']. "'",__LINE__,__FILE__);
+
+                       $receipt['message'][] = array('msg' =>lang('standard 
has been edited'));
+                       return $receipt;
+               }
+
+               function delete($id,$type)
+               {
+                       $table = $this->select_table($type);
+                       $this->db->query("DELETE FROM $table WHERE id='" . $id 
. "'",__LINE__,__FILE__);
+               }
+       }
+?>
Index: property/inc/class.sostandard_entity.inc.php
diff -u property/inc/class.sostandard_entity.inc.php:1.6 
property/inc/class.sostandard_entity.inc.php:1.7
--- property/inc/class.sostandard_entity.inc.php:1.6    Fri Jan 27 14:05:43 2006
+++ property/inc/class.sostandard_entity.inc.php        Mon Jan 30 22:14:19 2006
@@ -1,503 +1,504 @@
-<?php
-       /**
-       * phpGroupWare - property: a Facilities Management System.
-       *
-       * @author Sigurd Nes <address@hidden>
-       * @copyright Copyright (C) 2003-2005 Free Software Foundation, Inc. 
http://www.fsf.org/
-       * @license http://www.gnu.org/licenses/gpl.html GNU General Public 
License
-       * @internal Development of this application was funded by 
http://www.bergen.kommune.no/bbb_/ekstern/
-       * @package property
-       * @subpackage admin
-       * @version $Id: class.sostandard_entity.inc.php,v 1.6 2006/01/27 
14:05:43 sigurdne Exp $
-       */
-
-       /**
-        * Description
-        * @package property
-        */
-
-       class sostandard_entity
-       {
-
-               function sostandard_entity()
-               {
-                       $this->currentapp       = 
$GLOBALS['phpgw_info']['flags']['currentapp'];
-                       $this->account          = 
$GLOBALS['phpgw_info']['user']['account_id'];
-                       $this->bocommon         = 
CreateObject($this->currentapp.'.bocommon');
+<?php
+       /**
+       * phpGroupWare - property: a Facilities Management System.
+       *
+       * @author Sigurd Nes <address@hidden>
+       * @copyright Copyright (C) 2003-2005 Free Software Foundation, Inc. 
http://www.fsf.org/
+       * @license http://www.gnu.org/licenses/gpl.html GNU General Public 
License
+       * @internal Development of this application was funded by 
http://www.bergen.kommune.no/bbb_/ekstern/
+       * @package property
+       * @subpackage admin
+       * @version $Id: class.sostandard_entity.inc.php,v 1.7 2006/01/30 
22:14:19 sigurdne Exp $
+       */
+
+       /**
+        * Description
+        * @package property
+        */
+
+       class sostandard_entity
+       {
+
+               function sostandard_entity()
+               {
+                       $this->currentapp       = 
$GLOBALS['phpgw_info']['flags']['currentapp'];
+                       $this->account          = 
$GLOBALS['phpgw_info']['user']['account_id'];
+                       $this->bocommon         = 
CreateObject($this->currentapp.'.bocommon');
                        $this->db               = $this->bocommon->new_db();
-                       $this->db2              = $this->bocommon->new_db();
-                       $this->join                     = $this->bocommon->join;
-                       $this->like                     = $this->bocommon->like;
-               }
-
-               function read($data)
-               {
-                       if(is_array($data))
-                       {
-                               if ($data['start'])
-                               {
-                                       $start=$data['start'];
-                               }
-                               else
-                               {
-                                       $start=0;
-                               }
-                               $query = 
(isset($data['query'])?$data['query']:'');
-                               $sort = 
(isset($data['sort'])?$data['sort']:'DESC');
-                               $order = 
(isset($data['order'])?$data['order']:'');
-                               $type = (isset($data['type'])?$data['type']:0);
-                       }
-
-                       if ($order)
-                       {
-                               $ordermethod = " order by $order $sort";
-
-                       }
-                       else
-                       {
-                               $ordermethod = ' order by id asc';
-                       }
-
-                       $table = $this->select_table($type);
-
-                       if($query)
-                       {
-                               $query = ereg_replace("'",'',$query);
-                               $query = ereg_replace('"','',$query);
-
-                               $querymethod = " where name $this->like 
'%$query%' or descr $this->like '%$query%'";
-                       }
-
-                       $sql = "SELECT * FROM $table $querymethod";
-
-                       $this->db2->query($sql,__LINE__,__FILE__);
-                       $this->total_records = $this->db2->num_rows();
-                       $this->db->limit_query($sql . 
$ordermethod,$start,__LINE__,__FILE__);
-
-                       while ($this->db->next_record())
-                       {
-                               $standard[] = array
-                               (
-                                       'id'    => $this->db->f('id'),
-                                       'name'  => $this->db->f('name'),
-                                       'prefix'=> $this->db->f('prefix'),
-                                       'descr' => $this->db->f('descr')
-                               );
-                       }
-                       return $standard;
-               }
-
-
-               function select_table($type)
-               {
-
-                       switch($type)
-                       {
-                               case 'report':
-                                       $table='fm_report_type';
-                                       break;
-                       }
-
-                       return $table;
-               }
-
-               function select_table_attrib($type)
-               {
-
-                       switch($type)
-                       {
-                               case 'report':
-                                       $table='fm_report_type_attrib';
-                                       break;
-                       }
-
-                       return $table;
-               }
-
-               function select_table_choice($type)
-               {
-
-                       switch($type)
-                       {
-                               case 'report':
-                                       $table='fm_report_type_choice';
-                                       break;
-                       }
-
-                       return $table;
-               }
-
-
-               function read_single($id,$type)
-               {
-
-                       $table = $this->select_table($type);
-
-                       $sql = "SELECT * FROM $table  where id='$id'";
-
-                       $this->db->query($sql,__LINE__,__FILE__);
-
-                       if ($this->db->next_record())
-                       {
-                               $standard['id']                 = 
$this->db->f('id');
-                               $standard['name']               = 
$this->db->f('name');
-                               $standard['descr']              = 
$this->db->f('descr');
-                               $standard['prefix']             = 
$this->db->f('prefix');
-
-                               return $standard;
-                       }
-               }
-
-               function add($standard,$type)
-               {
-
-                       if (!$standard['name'])
-                       {
-                               $receipt['error'][] = array('msg'=>lang('Name 
not entered!'));
-                       }
-
-                       if (!$receipt['error'])
-                       {
-                               $table = $this->select_table($type);
-
-                               $standard['id'] = 
$this->bocommon->next_id($table);
-                               $standard['name'] = 
$this->db->db_addslashes($standard['name']);
-                               $standard['descr'] = 
$this->db->db_addslashes($standard['descr']);
-
-                       $values= array(
-                               $standard['id'],
-                               $standard['name'],
-                               $standard['descr'],
-                               $standard['prefix']
-                               );
-
-                       $values = $this->bocommon->validate_db_insert($values);
-
-
-                               $this->db->query("INSERT INTO $table (id, name, 
descr, prefix) VALUES ($values)",__LINE__,__FILE__);
-
-                               $receipt['id']=$standard['id'];
-                               $receipt['message'][] = array('msg'     => 
lang('standard has been saved'));
-                       }
-                       else
-                       {
-                               $receipt['error'][] = array('msg'=> 
lang('Attribute has NOT been saved'));
-                       }
-
-//_debug_array($receipt);
-                       return $receipt;
-               }
-
-               function edit($standard,$type)
-               {
-
-                       if (!$standard['name'])
-                       {
-                               $receipt['error'][] = array('msg'=>lang('Name 
not entered!'));
-                       }
-
-                       if (!$receipt['error'])
-                       {
-                               $table = $this->select_table($type);
-
-                               $standard['name'] = 
$this->db->db_addslashes($standard['name']);
-                               $standard['descr'] = 
$this->db->db_addslashes($standard['descr']);
-
-                               $value_set=array(
-                                       'descr'                 => 
$standard['descr'],
-                                       'name'          => $standard['name'],
-                                       'prefix'        => $standard['prefix']
-                                       );
-
-                               $value_set      = 
$this->bocommon->validate_db_update($value_set);
-
-                               $this->db->query("UPDATE $table set $value_set 
WHERE id=" . $standard['id'],__LINE__,__FILE__);
-
-                               $receipt['message'][] = array('msg'=> 
lang('Standard has been edited'));
-                       }
-                       else
-                       {
-                               $receipt['error'][] = array('msg'       => 
lang('Standard has NOT been edited'));
-                       }
-
-                       return $receipt;
-               }
-
-               function delete($id,$type,$attrib)
-               {
-                       if($attrib)
-                       {
-                               $table = $this->select_table_attrib($type);
-                       }
-                       else
-                       {
-
-                               $table                  = 
$this->select_table($type);
-                               $attrib_table   = 
$this->select_table_attrib($type);
-                               $this->db->query("DELETE_ FROM $attrib_table 
WHERE type_id='" . $id . "'",__LINE__,__FILE__);
-                       }
-                       $this->db->query("DELETE_ FROM $table WHERE id='" . $id 
. "'",__LINE__,__FILE__);
-               }
-
-               function read_attrib($data)
-               {
-
-//_debug_array($data);
-                       if(is_array($data))
-                       {
-                               if ($data['start'])
-                               {
-                                       $start=$data['start'];
-                               }
-                               else
-                               {
-                                       $start=0;
-                               }
-                               $query = 
(isset($data['query'])?$data['query']:'');
-                               $sort = 
(isset($data['sort'])?$data['sort']:'DESC');
-                               $order = 
(isset($data['order'])?$data['order']:'');
-                               $type = (isset($data['type'])?$data['type']:0);
-                               $type_id = 
(isset($data['type_id'])?$data['type_id']:0);
-                               $allrows = 
(isset($data['allrows'])?$data['allrows']:'');
-                       }
-
-                       $table          = $this->select_table_attrib($type);
-                       $type_table = $this->select_table($type);
-
-                       if ($order)
-                       {
-                               $ordermethod = " order by $order $sort";
-
-                       }
-                       else
-                       {
-                               $ordermethod = " order by $table.id asc";
-                       }
-
-
-                       if($query)
-                       {
-                               $query = ereg_replace("'",'',$query);
-                               $query = ereg_replace('"','',$query);
-
-                               $querymethod = " and ($table.name $this->like 
'%$query%' or $table.descr $this->like '%$query%')";
-                       }
-
-                       $sql = "SELECT $table.id ,$table.name ,$table.descr, 
$table.datatype ,$type_table.name as type FROM $type_table $this->join $table 
on $table.type_id = $type_table.id where $table.type_id= '$type_id' 
$querymethod";
-
-                       $this->db2->query($sql,__LINE__,__FILE__);
-                       $this->total_records = $this->db2->num_rows();
-
-                       if(!$allrows)
-                       {
-                               $this->db->limit_query($sql . 
$ordermethod,$start,__LINE__,__FILE__);
-                       }
-                       else
-                       {
-                               $this->db->query($sql . 
$ordermethod,__LINE__,__FILE__);
-                       }
-
-
-                       while ($this->db->next_record())
-                       {
-                               $attrib[] = array
-                               (
-                                       'id'    => $this->db->f('id'),
-                                       'name'  => $this->db->f('name'),
-                                       'descr' => $this->db->f('descr'),
-                                       'type_name'     => $this->db->f('type'),
-                                       'datatype'      => 
$this->db->f('datatype')
-                               );
-                       }
-                       return $attrib;
-               }
-
-               function read_single_attrib($id,$type)
-               {
-
-                       $table          = $this->select_table_attrib($type);
-                       $type_table = $this->select_table($type);
-
-                       $sql = "SELECT $table.id ,$table.name ,$table.descr, 
$table.datatype,$type_table.id as type_id ,$type_table.name as type_name FROM 
$type_table $this->join $table on $table.type_id = $type_table.id where 
$table.id= '$id'  $querymethod";
-
-                       $this->db->query($sql,__LINE__,__FILE__);
-
-                       if ($this->db->next_record())
-                       {
-                               $attrib['id']           = $this->db->f('id');
-                               $attrib['name']         = $this->db->f('name');
-                               $attrib['descr']        = $this->db->f('descr');
-                               $attrib['type_id']      = 
$this->db->f('type_id');
-                               $attrib['type_name']    = 
$this->db->f('type_name');
-                               $attrib['datatype']     = 
$this->db->f('datatype');
-//                             $attrib['list'] = $this->db->f('list');
-                               if($this->db->f('datatype')=='R' || 
$this->db->f('datatype')=='CH')
-                               {
-                                       $attrib['choice'] = 
$this->read_attrib_choice($attrib['id'],$type);
-                               }
-
-//_debug_array($attrib);
-                       }
-                       return $attrib;
-               }
-
-               function read_attrib_choice($attrib_id='',$type='')
-               {
-                       $choice_table = $this->select_table_choice($type);
-                       $sql = "SELECT * FROM $choice_table WHERE 
attrib_id=$attrib_id";
-                       $this->db->query($sql,__LINE__,__FILE__);
-
-                       while ($this->db->next_record())
-                       {
-                               $choice[] = array
-                               (
-                                       'id'    => $this->db->f('id'),
-                                       'value' => $this->db->f('value')
-                               );
-                       }
-                       return $choice;
-               }
-
-               function add_attrib($attrib,$type)
-               {
-
-                       if (!$attrib['type_id'])
-                       {
-                               $receipt['error'][] = array('msg'=>lang('Entity 
type not choosen!'));
-                       }
-
-                       if (!$attrib['datatype'])
-                       {
-                               $receipt['error'][] = 
array('msg'=>lang('Datatype type not choosen!'));
-                       }
-
-                       if (!$attrib['name'])
-                       {
-                               $receipt['error'][] = array('msg'=>lang('Name 
not entered!'));
-                       }
-
-                       if (!$receipt['error'])
-                       {
-
-                               $table = $this->select_table_attrib($type);
-
-                               $attrib['id'] = 
$this->bocommon->next_id($table);
-                               $attrib['name'] = 
$this->db->db_addslashes($attrib['name']);
-                               $attrib['descr'] = 
$this->db->db_addslashes($attrib['descr']);
-
-
-                       $values= array(
-                               $attrib['id'],
-                               $attrib['type_id'],
-                               $attrib['name'],
-                               $attrib['descr'],
-                               $attrib['datatype']
-//                             $attrib['list']
-                               );
-
-                       $values = $this->bocommon->validate_db_insert($values);
-
-                               $this->db->query("INSERT INTO $table (id, 
type_id, name, descr, datatype) "
-                                       . "VALUES ($values)",__LINE__,__FILE__);
-
-                               $receipt['id']=$attrib['id'];
-                               $receipt['message'][] = array('msg'     => 
lang('Attribute has been saved')     );
-
-                       }
-                       else
-                       {
-                               $receipt['error'][] = array('msg'       => 
lang('Attribute has NOT been saved'));
-                       }
-                       return $receipt;
-               }
-
-               function edit_attrib($attrib,$type)
-               {
-
-                       if (!$attrib['type_id'])
-                       {
-                               $receipt['error'][] = array('msg'=>lang('Entity 
type not choosen!'));
-                       }
-
-                       if (!$attrib['datatype'])
-                       {
-                               $receipt['error'][] = 
array('msg'=>lang('Datatype type not choosen!'));
-                       }
-
-                       if (!$attrib['name'])
-                       {
-                               $receipt['error'][] = array('msg'=>lang('Name 
not entered!'));
-                       }
-
-                       if (!$receipt['error'])
-                       {
-                               $table = $this->select_table_attrib($type);
-                               $choice_table = 
$this->select_table_choice($type);
-
-                               $attrib['name'] = 
$this->db->db_addslashes($attrib['name']);
-                               $attrib['descr'] = 
$this->db->db_addslashes($attrib['descr']);
-
-                               $value_set=array(
-                                       'descr'         => $attrib['descr'],
-                                       'name'          => $attrib['name'],
-                                       'type_id'       => $attrib['type_id'],
-                                       'datatype'      => $attrib['datatype']
-//                                     'list'          => $attrib['list']
-                                       );
-
-                               $value_set      = 
$this->bocommon->validate_db_update($value_set);
-
-                               $this->db->query("UPDATE $table set $value_set 
WHERE id=" . $attrib['id'],__LINE__,__FILE__);
-
-                               if($attrib['new_choice'])
-                               {
-                                       $this->db->query("INSERT INTO 
$choice_table (attrib_id, value) "
-                                       . "VALUES ('" . $attrib['id'] . "','" . 
$attrib['new_choice']. "')",__LINE__,__FILE__);
-
-                               }
-
-                               if($attrib['delete_choice'])
-                               {
-                                       for 
($i=0;$i<count($attrib['delete_choice']);$i++)
-                                       {
-
-                                               $this->db->query("DELETE FROM 
$choice_table WHERE id='" . $attrib['delete_choice'][$i] . 
"'",__LINE__,__FILE__);
-                                       }
-                               }
-
-                               if($attrib['datatype']!='R' && 
$attrib['datatype']!='CH')
-                               {
-                                       $this->db->query("DELETE FROM 
$choice_table WHERE attrib_id='" . $attrib['id'] . "'",__LINE__,__FILE__);
-                               }
-
-                               $receipt['message'][] = array('msg'     => 
lang('Attribute has been edited'));
-                       }
-                       else
-                       {
-                               $receipt['error'][] = array('msg'       => 
lang('Attribute has NOT been edited'));
-                       }
-                       return $receipt;
-               }
-
-
-               function select_entity_type($type='')
-               {
-                       $table = $this->select_table($type);
-
-                       $this->db->query("SELECT * FROM $table ORDER BY name ");
-
-                       $i = 0;
-                       while ($this->db->next_record())
-                       {
-                               $entity_type[$i]['id']          = 
$this->db->f('id');
-                               $entity_type[$i]['name']                = 
stripslashes($this->db->f('name'));
-                               $entity_type[$i]['descr']       = 
stripslashes($this->db->f('descr'));
-                               $i++;
-                       }
-//_debug_array($entity_type);
-
-                       return $entity_type;
-               }
-
-       }
-?>
+                       $this->db2              = $this->bocommon->new_db();
+
+                       $this->join                     = $this->bocommon->join;
+                       $this->like                     = $this->bocommon->like;
+               }
+
+               function read($data)
+               {
+                       if(is_array($data))
+                       {
+                               if ($data['start'])
+                               {
+                                       $start=$data['start'];
+                               }
+                               else
+                               {
+                                       $start=0;
+                               }
+                               $query = 
(isset($data['query'])?$data['query']:'');
+                               $sort = 
(isset($data['sort'])?$data['sort']:'DESC');
+                               $order = 
(isset($data['order'])?$data['order']:'');
+                               $type = (isset($data['type'])?$data['type']:0);
+                       }
+
+                       if ($order)
+                       {
+                               $ordermethod = " order by $order $sort";
+
+                       }
+                       else
+                       {
+                               $ordermethod = ' order by id asc';
+                       }
+
+                       $table = $this->select_table($type);
+
+                       if($query)
+                       {
+                               $query = ereg_replace("'",'',$query);
+                               $query = ereg_replace('"','',$query);
+
+                               $querymethod = " where name $this->like 
'%$query%' or descr $this->like '%$query%'";
+                       }
+
+                       $sql = "SELECT * FROM $table $querymethod";
+
+                       $this->db2->query($sql,__LINE__,__FILE__);
+                       $this->total_records = $this->db2->num_rows();
+                       $this->db->limit_query($sql . 
$ordermethod,$start,__LINE__,__FILE__);
+
+                       while ($this->db->next_record())
+                       {
+                               $standard[] = array
+                               (
+                                       'id'    => $this->db->f('id'),
+                                       'name'  => $this->db->f('name'),
+                                       'prefix'=> $this->db->f('prefix'),
+                                       'descr' => $this->db->f('descr')
+                               );
+                       }
+                       return $standard;
+               }
+
+
+               function select_table($type)
+               {
+
+                       switch($type)
+                       {
+                               case 'report':
+                                       $table='fm_report_type';
+                                       break;
+                       }
+
+                       return $table;
+               }
+
+               function select_table_attrib($type)
+               {
+
+                       switch($type)
+                       {
+                               case 'report':
+                                       $table='fm_report_type_attrib';
+                                       break;
+                       }
+
+                       return $table;
+               }
+
+               function select_table_choice($type)
+               {
+
+                       switch($type)
+                       {
+                               case 'report':
+                                       $table='fm_report_type_choice';
+                                       break;
+                       }
+
+                       return $table;
+               }
+
+
+               function read_single($id,$type)
+               {
+
+                       $table = $this->select_table($type);
+
+                       $sql = "SELECT * FROM $table  where id='$id'";
+
+                       $this->db->query($sql,__LINE__,__FILE__);
+
+                       if ($this->db->next_record())
+                       {
+                               $standard['id']                 = 
$this->db->f('id');
+                               $standard['name']               = 
$this->db->f('name');
+                               $standard['descr']              = 
$this->db->f('descr');
+                               $standard['prefix']             = 
$this->db->f('prefix');
+
+                               return $standard;
+                       }
+               }
+
+               function add($standard,$type)
+               {
+
+                       if (!$standard['name'])
+                       {
+                               $receipt['error'][] = array('msg'=>lang('Name 
not entered!'));
+                       }
+
+                       if (!$receipt['error'])
+                       {
+                               $table = $this->select_table($type);
+
+                               $standard['id'] = 
$this->bocommon->next_id($table);
+                               $standard['name'] = 
$this->db->db_addslashes($standard['name']);
+                               $standard['descr'] = 
$this->db->db_addslashes($standard['descr']);
+
+                       $values= array(
+                               $standard['id'],
+                               $standard['name'],
+                               $standard['descr'],
+                               $standard['prefix']
+                               );
+
+                       $values = $this->bocommon->validate_db_insert($values);
+
+
+                               $this->db->query("INSERT INTO $table (id, name, 
descr, prefix) VALUES ($values)",__LINE__,__FILE__);
+
+                               $receipt['id']=$standard['id'];
+                               $receipt['message'][] = array('msg'     => 
lang('standard has been saved'));
+                       }
+                       else
+                       {
+                               $receipt['error'][] = array('msg'=> 
lang('Attribute has NOT been saved'));
+                       }
+
+//_debug_array($receipt);
+                       return $receipt;
+               }
+
+               function edit($standard,$type)
+               {
+
+                       if (!$standard['name'])
+                       {
+                               $receipt['error'][] = array('msg'=>lang('Name 
not entered!'));
+                       }
+
+                       if (!$receipt['error'])
+                       {
+                               $table = $this->select_table($type);
+
+                               $standard['name'] = 
$this->db->db_addslashes($standard['name']);
+                               $standard['descr'] = 
$this->db->db_addslashes($standard['descr']);
+
+                               $value_set=array(
+                                       'descr'                 => 
$standard['descr'],
+                                       'name'          => $standard['name'],
+                                       'prefix'        => $standard['prefix']
+                                       );
+
+                               $value_set      = 
$this->bocommon->validate_db_update($value_set);
+
+                               $this->db->query("UPDATE $table set $value_set 
WHERE id=" . $standard['id'],__LINE__,__FILE__);
+
+                               $receipt['message'][] = array('msg'=> 
lang('Standard has been edited'));
+                       }
+                       else
+                       {
+                               $receipt['error'][] = array('msg'       => 
lang('Standard has NOT been edited'));
+                       }
+
+                       return $receipt;
+               }
+
+               function delete($id,$type,$attrib)
+               {
+                       if($attrib)
+                       {
+                               $table = $this->select_table_attrib($type);
+                       }
+                       else
+                       {
+
+                               $table                  = 
$this->select_table($type);
+                               $attrib_table   = 
$this->select_table_attrib($type);
+                               $this->db->query("DELETE_ FROM $attrib_table 
WHERE type_id='" . $id . "'",__LINE__,__FILE__);
+                       }
+                       $this->db->query("DELETE_ FROM $table WHERE id='" . $id 
. "'",__LINE__,__FILE__);
+               }
+
+               function read_attrib($data)
+               {
+
+//_debug_array($data);
+                       if(is_array($data))
+                       {
+                               if ($data['start'])
+                               {
+                                       $start=$data['start'];
+                               }
+                               else
+                               {
+                                       $start=0;
+                               }
+                               $query = 
(isset($data['query'])?$data['query']:'');
+                               $sort = 
(isset($data['sort'])?$data['sort']:'DESC');
+                               $order = 
(isset($data['order'])?$data['order']:'');
+                               $type = (isset($data['type'])?$data['type']:0);
+                               $type_id = 
(isset($data['type_id'])?$data['type_id']:0);
+                               $allrows = 
(isset($data['allrows'])?$data['allrows']:'');
+                       }
+
+                       $table          = $this->select_table_attrib($type);
+                       $type_table = $this->select_table($type);
+
+                       if ($order)
+                       {
+                               $ordermethod = " order by $order $sort";
+
+                       }
+                       else
+                       {
+                               $ordermethod = " order by $table.id asc";
+                       }
+
+
+                       if($query)
+                       {
+                               $query = ereg_replace("'",'',$query);
+                               $query = ereg_replace('"','',$query);
+
+                               $querymethod = " and ($table.name $this->like 
'%$query%' or $table.descr $this->like '%$query%')";
+                       }
+
+                       $sql = "SELECT $table.id ,$table.name ,$table.descr, 
$table.datatype ,$type_table.name as type FROM $type_table $this->join $table 
on $table.type_id = $type_table.id where $table.type_id= '$type_id' 
$querymethod";
+
+                       $this->db2->query($sql,__LINE__,__FILE__);
+                       $this->total_records = $this->db2->num_rows();
+
+                       if(!$allrows)
+                       {
+                               $this->db->limit_query($sql . 
$ordermethod,$start,__LINE__,__FILE__);
+                       }
+                       else
+                       {
+                               $this->db->query($sql . 
$ordermethod,__LINE__,__FILE__);
+                       }
+
+
+                       while ($this->db->next_record())
+                       {
+                               $attrib[] = array
+                               (
+                                       'id'    => $this->db->f('id'),
+                                       'name'  => $this->db->f('name'),
+                                       'descr' => $this->db->f('descr'),
+                                       'type_name'     => $this->db->f('type'),
+                                       'datatype'      => 
$this->db->f('datatype')
+                               );
+                       }
+                       return $attrib;
+               }
+
+               function read_single_attrib($id,$type)
+               {
+
+                       $table          = $this->select_table_attrib($type);
+                       $type_table = $this->select_table($type);
+
+                       $sql = "SELECT $table.id ,$table.name ,$table.descr, 
$table.datatype,$type_table.id as type_id ,$type_table.name as type_name FROM 
$type_table $this->join $table on $table.type_id = $type_table.id where 
$table.id= '$id'  $querymethod";
+
+                       $this->db->query($sql,__LINE__,__FILE__);
+
+                       if ($this->db->next_record())
+                       {
+                               $attrib['id']           = $this->db->f('id');
+                               $attrib['name']         = $this->db->f('name');
+                               $attrib['descr']        = $this->db->f('descr');
+                               $attrib['type_id']      = 
$this->db->f('type_id');
+                               $attrib['type_name']    = 
$this->db->f('type_name');
+                               $attrib['datatype']     = 
$this->db->f('datatype');
+//                             $attrib['list'] = $this->db->f('list');
+                               if($this->db->f('datatype')=='R' || 
$this->db->f('datatype')=='CH')
+                               {
+                                       $attrib['choice'] = 
$this->read_attrib_choice($attrib['id'],$type);
+                               }
+
+//_debug_array($attrib);
+                       }
+                       return $attrib;
+               }
+
+               function read_attrib_choice($attrib_id='',$type='')
+               {
+                       $choice_table = $this->select_table_choice($type);
+                       $sql = "SELECT * FROM $choice_table WHERE 
attrib_id=$attrib_id";
+                       $this->db->query($sql,__LINE__,__FILE__);
+
+                       while ($this->db->next_record())
+                       {
+                               $choice[] = array
+                               (
+                                       'id'    => $this->db->f('id'),
+                                       'value' => $this->db->f('value')
+                               );
+                       }
+                       return $choice;
+               }
+
+               function add_attrib($attrib,$type)
+               {
+
+                       if (!$attrib['type_id'])
+                       {
+                               $receipt['error'][] = array('msg'=>lang('Entity 
type not choosen!'));
+                       }
+
+                       if (!$attrib['datatype'])
+                       {
+                               $receipt['error'][] = 
array('msg'=>lang('Datatype type not choosen!'));
+                       }
+
+                       if (!$attrib['name'])
+                       {
+                               $receipt['error'][] = array('msg'=>lang('Name 
not entered!'));
+                       }
+
+                       if (!$receipt['error'])
+                       {
+
+                               $table = $this->select_table_attrib($type);
+
+                               $attrib['id'] = 
$this->bocommon->next_id($table);
+                               $attrib['name'] = 
$this->db->db_addslashes($attrib['name']);
+                               $attrib['descr'] = 
$this->db->db_addslashes($attrib['descr']);
+
+
+                       $values= array(
+                               $attrib['id'],
+                               $attrib['type_id'],
+                               $attrib['name'],
+                               $attrib['descr'],
+                               $attrib['datatype']
+//                             $attrib['list']
+                               );
+
+                       $values = $this->bocommon->validate_db_insert($values);
+
+                               $this->db->query("INSERT INTO $table (id, 
type_id, name, descr, datatype) "
+                                       . "VALUES ($values)",__LINE__,__FILE__);
+
+                               $receipt['id']=$attrib['id'];
+                               $receipt['message'][] = array('msg'     => 
lang('Attribute has been saved')     );
+
+                       }
+                       else
+                       {
+                               $receipt['error'][] = array('msg'       => 
lang('Attribute has NOT been saved'));
+                       }
+                       return $receipt;
+               }
+
+               function edit_attrib($attrib,$type)
+               {
+
+                       if (!$attrib['type_id'])
+                       {
+                               $receipt['error'][] = array('msg'=>lang('Entity 
type not choosen!'));
+                       }
+
+                       if (!$attrib['datatype'])
+                       {
+                               $receipt['error'][] = 
array('msg'=>lang('Datatype type not choosen!'));
+                       }
+
+                       if (!$attrib['name'])
+                       {
+                               $receipt['error'][] = array('msg'=>lang('Name 
not entered!'));
+                       }
+
+                       if (!$receipt['error'])
+                       {
+                               $table = $this->select_table_attrib($type);
+                               $choice_table = 
$this->select_table_choice($type);
+
+                               $attrib['name'] = 
$this->db->db_addslashes($attrib['name']);
+                               $attrib['descr'] = 
$this->db->db_addslashes($attrib['descr']);
+
+                               $value_set=array(
+                                       'descr'         => $attrib['descr'],
+                                       'name'          => $attrib['name'],
+                                       'type_id'       => $attrib['type_id'],
+                                       'datatype'      => $attrib['datatype']
+//                                     'list'          => $attrib['list']
+                                       );
+
+                               $value_set      = 
$this->bocommon->validate_db_update($value_set);
+
+                               $this->db->query("UPDATE $table set $value_set 
WHERE id=" . $attrib['id'],__LINE__,__FILE__);
+
+                               if($attrib['new_choice'])
+                               {
+                                       $this->db->query("INSERT INTO 
$choice_table (attrib_id, value) "
+                                       . "VALUES ('" . $attrib['id'] . "','" . 
$attrib['new_choice']. "')",__LINE__,__FILE__);
+
+                               }
+
+                               if($attrib['delete_choice'])
+                               {
+                                       for 
($i=0;$i<count($attrib['delete_choice']);$i++)
+                                       {
+
+                                               $this->db->query("DELETE FROM 
$choice_table WHERE id='" . $attrib['delete_choice'][$i] . 
"'",__LINE__,__FILE__);
+                                       }
+                               }
+
+                               if($attrib['datatype']!='R' && 
$attrib['datatype']!='CH')
+                               {
+                                       $this->db->query("DELETE FROM 
$choice_table WHERE attrib_id='" . $attrib['id'] . "'",__LINE__,__FILE__);
+                               }
+
+                               $receipt['message'][] = array('msg'     => 
lang('Attribute has been edited'));
+                       }
+                       else
+                       {
+                               $receipt['error'][] = array('msg'       => 
lang('Attribute has NOT been edited'));
+                       }
+                       return $receipt;
+               }
+
+
+               function select_entity_type($type='')
+               {
+                       $table = $this->select_table($type);
+
+                       $this->db->query("SELECT * FROM $table ORDER BY name ");
+
+                       $i = 0;
+                       while ($this->db->next_record())
+                       {
+                               $entity_type[$i]['id']          = 
$this->db->f('id');
+                               $entity_type[$i]['name']                = 
stripslashes($this->db->f('name'));
+                               $entity_type[$i]['descr']       = 
stripslashes($this->db->f('descr'));
+                               $i++;
+                       }
+//_debug_array($entity_type);
+
+                       return $entity_type;
+               }
+
+       }
+?>
Index: property/inc/class.sotemplate.inc.php
diff -u property/inc/class.sotemplate.inc.php:1.6 
property/inc/class.sotemplate.inc.php:1.7
--- property/inc/class.sotemplate.inc.php:1.6   Fri Jan 27 14:05:43 2006
+++ property/inc/class.sotemplate.inc.php       Mon Jan 30 22:14:19 2006
@@ -1,473 +1,472 @@
-<?php
-       /**
-       * phpGroupWare - property: a Facilities Management System.
-       *
-       * @author Sigurd Nes <address@hidden>
-       * @copyright Copyright (C) 2003-2005 Free Software Foundation, Inc. 
http://www.fsf.org/
-       * @license http://www.gnu.org/licenses/gpl.html GNU General Public 
License
-       * @internal Development of this application was funded by 
http://www.bergen.kommune.no/bbb_/ekstern/
-       * @package property
-       * @subpackage project
-       * @version $Id: class.sotemplate.inc.php,v 1.6 2006/01/27 14:05:43 
sigurdne Exp $
-       */
-
-       /**
-        * Description
-        * @package property
-        */
-
-       class sotemplate
-       {
-
-               function sotemplate()
-               {
-                       $this->currentapp       = 
$GLOBALS['phpgw_info']['flags']['currentapp'];
-                       $this->account          = 
$GLOBALS['phpgw_info']['user']['account_id'];
-                       $this->bocommon         = 
CreateObject($this->currentapp.'.bocommon');
+<?php
+       /**
+       * phpGroupWare - property: a Facilities Management System.
+       *
+       * @author Sigurd Nes <address@hidden>
+       * @copyright Copyright (C) 2003-2005 Free Software Foundation, Inc. 
http://www.fsf.org/
+       * @license http://www.gnu.org/licenses/gpl.html GNU General Public 
License
+       * @internal Development of this application was funded by 
http://www.bergen.kommune.no/bbb_/ekstern/
+       * @package property
+       * @subpackage project
+       * @version $Id: class.sotemplate.inc.php,v 1.7 2006/01/30 22:14:19 
sigurdne Exp $
+       */
+
+       /**
+        * Description
+        * @package property
+        */
+
+       class sotemplate
+       {
+               function sotemplate()
+               {
+                       $this->currentapp       = 
$GLOBALS['phpgw_info']['flags']['currentapp'];
+                       $this->account          = 
$GLOBALS['phpgw_info']['user']['account_id'];
+                       $this->bocommon         = 
CreateObject($this->currentapp.'.bocommon');
                        $this->db               = $this->bocommon->new_db();
-                       $this->db2              = $this->bocommon->new_db();
-                       $this->join                     = $this->bocommon->join;
-                       $this->left_join        = $this->bocommon->left_join;
-                       $this->like                     = $this->bocommon->like;
-               }
-
-               function read($data)
-               {
-
-                       if(is_array($data))
-                       {
-                               if ($data['start'])
-                               {
-                                       $start=$data['start'];
-                               }
-                               else
-                               {
-                                       $start=0;
-                               }
-                               $filter                 = 
(isset($data['filter'])?$data['filter']:'');
-                               $query                  = 
(isset($data['query'])?$data['query']:'');
-                               $sort                   = 
(isset($data['sort'])?$data['sort']:'DESC');
-                               $order                  = 
(isset($data['order'])?$data['order']:'');
-                               $chapter_id     = 
(isset($data['chapter_id'])?$data['chapter_id']:0);
-                               $allrows                = 
(isset($data['allrows'])?$data['allrows']:'');
-                               $template_id    = 
(isset($data['template_id'])?$data['template_id']:0);
-                       }
-
-                       if ($order)
-                       {
-                               $ordermethod = " order by $order $sort";
-                       }
-                       else
-                       {
-                               $ordermethod = ' order by fm_template.id desc';
-                       }
-
-                       $where= 'WHERE';
-
-                       if ($chapter_id > 0)
-                       {
-                               $filtermethod .= " $where 
chapter_id='$chapter_id' ";
-                               $where= 'AND';
-                       }
-
-                       if ($filter)
-                       {
-                               $filtermethod .= " $where 
fm_template.owner='$filter' ";
-                               $where= 'AND';
-                       }
-
-
-                       if($query)
-                       {
-                               $query = ereg_replace("'",'',$query);
-                               $query = ereg_replace('"','',$query);
-
-                               $querymethod = " $where (fm_template.descr 
$this->like '%$query%')";
-                       }
-
-                       $sql = "SELECT 
fm_template.id,fm_template.descr,fm_template.name,fm_template.owner,fm_template.entry_date,"
-                        . " fm_chapter.descr as chapter FROM fm_template 
$this->left_join fm_chapter  on fm_template.chapter_id=fm_chapter.id"
-                        . " $filtermethod $querymethod";
-
-                       $this->db2->query($sql,__LINE__,__FILE__);
-                       $this->total_records = $this->db2->num_rows();
-
-                       if(!$allrows)
-                       {
-                               $this->db->limit_query($sql . 
$ordermethod,$start,__LINE__,__FILE__);
-                       }
-                       else
-                       {
-                               $this->db->query($sql . 
$ordermethod,__LINE__,__FILE__);
-                       }
-
-                       while ($this->db->next_record())
-                       {
-                               $template_list[] = array
-                               (
-                                       'template_id'           => 
$this->db->f('id'),
-                                       'name'                          => 
stripslashes($this->db->f('name')),
-                                       'descr'                         => 
stripslashes($this->db->f('descr')),
-                                       'owner'                         => 
$this->db->f('owner'),
-                                       'entry_date'            => 
$this->db->f('entry_date'),
-                                       'chapter'                       => 
$this->db->f('chapter')
-                                       );
-                       }
-                       return $template_list;
-               }
-
-               function read_template_hour($data)
-               {
-
-                       if(is_array($data))
-                       {
-                               if ($data['start'])
-                               {
-                                       $start=$data['start'];
-                               }
-                               else
-                               {
-                                       $start=0;
-                               }
-                               $query                  = 
(isset($data['query'])?$data['query']:'');
-                               $sort                   = 
(isset($data['sort'])?$data['sort']:'DESC');
-                               $order                  = 
(isset($data['order'])?$data['order']:'');
-                               $chapter_id     = 
(isset($data['chapter_id'])?$data['chapter_id']:0);
-                               $allrows                = 
(isset($data['allrows'])?$data['allrows']:'');
-                               $template_id    = 
(isset($data['template_id'])?$data['template_id']:0);
-                       }
-
-                       if ($order)
-                       {
-                               $ordermethod = " order by $order $sort";
-                       }
-                       else
-                       {
-                               $ordermethod = ' order by record ';
-                       }
-
-
-                       $filtermethod .= " where template_id='$template_id' ";
-
-                       if($query)
-                       {
-                               $query = ereg_replace("'",'',$query);
-                               $query = ereg_replace('"','',$query);
-
-                               $querymethod = " AND (hours_descr $this->like 
'%$query%' or fm_template_hours.remark $this->like '%$query%' or ns3420_id 
$this->like '%$query%')";
-                       }
-
-                       $sql = "SELECT fm_template_hours.*, chapter_id from 
fm_template_hours  $this->join fm_template on 
fm_template.id=fm_template_hours.template_id $filtermethod $querymethod";
-
-                       $this->db2->query($sql,__LINE__,__FILE__);
-                       $this->total_records = $this->db2->num_rows();
-
-                       if(!$allrows)
-                       {
-                               $this->db->limit_query($sql . 
$ordermethod,$start,__LINE__,__FILE__);
-                       }
-                       else
-                       {
-                               $this->db->query($sql . 
$ordermethod,__LINE__,__FILE__);
-                       }
-
-                       while ($this->db->next_record())
-                       {
-                               $hour_list[] = array
-                               (
-                                       'hour_id'                       => 
$this->db->f('id'),
-                                       'chapter_id'            => 
$this->db->f('chapter_id'),
-                                       'activity_num'          => 
$this->db->f('activity_num'),
-                                       'hours_descr'           => 
stripslashes($this->db->f('hours_descr')),
-                                       'remark'                        => 
stripslashes($this->db->f('remark')),
-                                       'grouping_id'           => 
$this->db->f('grouping_id'),
-                                       'grouping_descr'        => 
$this->db->f('grouping_descr'),
-                                       'ns3420_id'                     => 
$this->db->f('ns3420_id'),
-                                       'tolerance'                     => 
$this->db->f('tolerance'),
-                                       'activity_id'           => 
$this->db->f('activity_id'),
-                                       'unit'                          => 
$this->db->f('unit'),
-                                       'record'                        => 
$this->db->f('record'),
-                                       'cost'                          => 
$this->db->f('cost'),
-                                       'billperae'                     => 
$this->db->f('billperae'),
-                                       'building_part'         => 
$this->db->f('building_part'),
-                                       'dim_d'                         => 
$this->db->f('dim_d')
-                                       );
-                       }
-                       return $hour_list;
-               }
-
-               function read_single_template($template_id)
-               {
-                       $sql = "SELECT * FROM fm_template where 
id='$template_id'";
-
-                       $this->db->query($sql,__LINE__,__FILE__);
-
-                       if ($this->db->next_record())
-                       {
-                               $template['template_id']                = 
$this->db->f('id');
-                               $template['name']                               
= stripslashes($this->db->f('name'));
-                               $template['descr']                              
= stripslashes($this->db->f('descr'));
-                               $template['chapter_id']                 = 
(int)$this->db->f('chapter_id');
-                       }
-                       return $template;
-               }
-
-               function read_single_hour($hour_id)
-               {
-                       $sql = "SELECT * from fm_template_hours where 
id='$hour_id'";
-
-                       $this->db->query($sql,__LINE__,__FILE__);
-
-                       if ($this->db->next_record())
-                       {
-                               $hour['hour_id']                                
= $this->db->f('id');
-                               $hour['record']                                 
= $this->db->f('record');
-                               $hour['activity_id']                    = 
$this->db->f('activity_id');
-                               $hour['activity_num']                   = 
$this->db->f('activity_num');
-                               $hour['grouping_id']                    = 
$this->db->f('grouping_id');
-                               $hour['hours_descr']                    = 
$this->db->f('hours_descr');
-                               $hour['remark']                                 
= $this->db->f('remark');
-                               $hour['billperae']                              
= $this->db->f('billperae');
-                               $hour['unit']                                   
= $this->db->f('unit');
-                               $hour['ns3420_id']                              
= $this->db->f('ns3420_id');
-                               $hour['tolerance_id']                   = 
(int)$this->db->f('tolerance');
-                               $hour['building_part_id']               = 
(int)$this->db->f('building_part');
-                               $hour['quantity']                               
= $this->db->f('quantity');
-                               $hour['cost']                                   
= $this->db->f('cost');
-                               $hour['dim_d']                                  
= $this->db->f('dim_d');
-                       }
-
-                               return $hour;
-               }
-
-               function next_record($template_id)
-               {
-
-                       $this->db->query("SELECT  max(record) as record FROM 
fm_template_hours where template_id='$template_id'",__LINE__,__FILE__);
-                       $this->db->next_record();
-                       $record = $this->db->f('record')+1;
-                       return $record;
-
-               }
-
-               function add_custom_hour($hour,$template_id)
-               {
-
-                       $hour['record'] = $this->next_record($template_id);
-
-                       $this->db->query("UPDATE fm_template set
-                               chapter_id      ='" . $hour['chapter_id'] . "' 
WHERE id= '$template_id'",__LINE__,__FILE__);
-
-                       if($hour['grouping_id'])
-                       {
-                               $this->db->query("SELECT grouping_descr , 
max(record) as record FROM fm_template_hours where grouping_id='" 
.$hour['grouping_id'] . "' and template_id= '$template_id' GROUP by 
grouping_descr",__LINE__,__FILE__);
-                               $this->db->next_record();
-                               $hour['grouping_descr'] = 
$this->db->f('grouping_descr');
-                       }
-
-                       if($hour['new_grouping'])
-                       {
-                               $this->db->query("SELECT grouping_id FROM 
fm_template_hours where grouping_descr ='" .$hour['new_grouping'] . "' and 
template_id= '$template_id'",__LINE__,__FILE__);
-                               $this->db->next_record();
-                               if ( $this->db->f('grouping_id'))
-                               {
-                                       $hour['grouping_id']    = 
$this->db->f('grouping_id');
-                               }
-                               else
-                               {
-                                       $this->db->query("SELECT 
max(grouping_id) as grouping_id FROM fm_template_hours where template_id= 
'$template_id'",__LINE__,__FILE__);
-                                       $this->db->next_record();
-                                       $hour['grouping_id']    = 
$this->db->f('grouping_id')+1;
-                               }
-
-                               $hour['grouping_descr'] = $hour['new_grouping'];
-                       }
-
-                       $this->db->query("insert into fm_template_hours 
(owner,hours_descr,unit,cost,quantity,billperae,ns3420_id,dim_d,"
-                               . " 
grouping_id,grouping_descr,record,building_part,tolerance,remark,entry_date,template_id)
 "
-                               . " values ('" .
-                                       $this->account . "','" .
-                                       $hour['descr'] . "','" .
-                                       $hour['unit'] . "','" .
-                                       $hour['cost'] . "','" .
-                                       $hour['quantity'] . "','" .
-                                       $hour['billperae'] . "','" .
-                                       $hour['ns3420_id'] . "','" .
-                                       $hour['dim_d'] . "','" .
-                                       $hour['grouping_id'] . "','" .
-                                       $hour['grouping_descr'] . "','" .
-                                       $hour['record'] . "','" .
-                                       $hour['building_part_id'] . "','" .
-                                       $hour['tolerance_id'] . "','" .
-                                       $hour['remark'] . "','" .
-                                       time() . "','" .
-                                       $template_id . "')",__LINE__,__FILE__);
-
-                       $receipt['hour_id'] = 
$this->db->get_last_insert_id('fm_template_hours','id');
-
-                       $receipt['message'][] = array('msg'=>lang('hour %1 is 
added!',$hour['record']));
-
-                       return $receipt;
-               }
-
-
-               function edit_hour($hour,$template_id)
-               {
-
-                       $hour['descr'] = 
$this->db->db_addslashes($hour['descr']);
-                       $hour['remark'] = 
$this->db->db_addslashes($hour['remark']);
-
-                       $this->db->query("UPDATE fm_template set
-                               chapter_id      ='" . $hour['chapter_id'] . "' 
WHERE id= '$template_id'",__LINE__,__FILE__);
-
-                       if($hour['new_grouping'])
-                       {
-                               $this->db->query("SELECT grouping_id FROM 
fm_template_hours where grouping_descr ='" .$hour['new_grouping'] . "' and 
template_id= '$template_id'",__LINE__,__FILE__);
-                               $this->db->next_record();
-                               if ( $this->db->f('grouping_id'))
-                               {
-                                       $hour['grouping_id']    = 
$this->db->f('grouping_id');
-                               }
-                               else
-                               {
-
-                                       $this->db->query("UPDATE 
fm_template_hours set grouping_id = NULL WHERE id ='" .$hour['hour_id'] . 
"'",__LINE__,__FILE__);
-                                       $this->db->query("SELECT 
count(grouping_id) as num_grouping FROM fm_template_hours where template_id= 
'$template_id' and grouping_id >0 ",__LINE__,__FILE__);
-                                       $this->db->next_record();
-                                       if ($this->db->f('num_grouping')==1)
-                                       {
-                                               $hour['grouping_id']=1;
-                                       }
-                                       else
-                                       {
-                                               $this->db->query("SELECT 
max(grouping_id) as grouping_id FROM fm_template_hours where template_id= 
'$template_id'",__LINE__,__FILE__);
-                                               $this->db->next_record();
-                                               $hour['grouping_id']    = 
$this->db->f('grouping_id')+1;
-                                       }
-                               }
-                               $hour['grouping_descr'] = $hour['new_grouping'];
-                       }
-                       else
-                       {
-                               $this->db->query("SELECT 
grouping_id,grouping_descr FROM fm_template_hours where id ='" 
.$hour['hour_id'] . "'",__LINE__,__FILE__);
-                               $this->db->next_record();
-                               $old_grouping_id        = 
$this->db->f('grouping_id');
-
-                               if ( $old_grouping_id == $hour['grouping_id'])
-                               {
-
-                                       $hour['grouping_descr'] = 
$this->db->f('grouping_descr');
-                               }
-                               else
-                               {
-                                       $this->db->query("SELECT grouping_descr 
, max(record) as record FROM fm_template_hours where grouping_id='" 
.$hour['grouping_id'] . "' and template_id= '$template_id' GROUP by 
grouping_descr",__LINE__,__FILE__);
-                                       $this->db->next_record();
-                                       if($this->db->f('grouping_descr'))
-                                       {
-                                               $hour['grouping_descr'] = 
$this->db->f('grouping_descr');
-                                       }
-                                       else
-                                       {
-                                               $hour['grouping_id']='';
-                                               $hour['grouping_descr']='';
-                                       }
-
-                               }
-                       }
-
-                       $this->db->query("SELECT record FROM fm_template_hours 
where id ='" .$hour['hour_id'] . "'",__LINE__,__FILE__);
-                       $this->db->next_record();
-                       $hour['record'] = $this->db->f('record');
-
-                       $this->db->query("UPDATE fm_template_hours set
-                               hours_descr                     ='" . 
$hour['descr'] . "',
-                               remark                          ='" . 
$hour['remark'] . "',
-                               billperae                       ='" . 
$hour['billperae'] . "',
-                               unit                            ='" . 
$hour['unit'] . "',
-                               quantity                        ='" . 
$hour['quantity'] . "',
-                               cost                            ='" . 
$hour['cost'] . "',
-                               ns3420_id                       ='" . 
$hour['ns3420_id'] . "',
-                               tolerance                       ='" . 
$hour['tolerance_id'] . "',
-                               building_part           ='" . 
$hour['building_part_id'] . "',
-                               dim_d                           ='" . 
$hour['dim_d'] . "',
-                               grouping_id                     ='" . 
$hour['grouping_id'] . "',
-                               grouping_descr          ='" . 
$hour['grouping_descr']
-                               . "' WHERE id= '" . $hour['hour_id'] 
."'",__LINE__,__FILE__);
-
-                       $receipt['hour_id'] = $hour['hour_id'];
-                       $receipt['message'][] = array('msg'=>lang('hour %1 has 
been edited',$hour['record']));
-                       return $receipt;
-
-               }
-
-               function get_grouping_list($template_id='')
-               {
-                       $this->db->query("SELECT grouping_id, grouping_descr 
FROM fm_template_hours where template_id='$template_id' and grouping_id >0 
group by grouping_id, grouping_descr");
-
-                       $i = 0;
-                       while ($this->db->next_record())
-                       {
-                               $grouping_entries[$i]['id']                     
        = $this->db->f('grouping_id');
-                               $grouping_entries[$i]['name']                   
        = stripslashes($this->db->f('grouping_descr'));
-                               $i++;
-                       }
-                       return $grouping_entries;
-               }
-
-               function add_template($values)
-               {
-
-                       $values['name'] = 
$this->db->db_addslashes($values['name']);
-
-                       $this->db->query("insert into fm_template 
(owner,name,descr,chapter_id,entry_date) "
-                               . " values ('" .
-                                       $this->account . "','" .
-                                       $values['name'] . "','" .
-                                       $values['descr'] . "','" .
-                                       $values['chapter_id'] . "','" .
-                                       time() . "')",__LINE__,__FILE__);
-
-                       $template_id = 
$this->db->get_last_insert_id('fm_template','id');
-
-                       $receipt['template_id'] = $template_id;
-                       $receipt['message'][] = array('msg'=>lang('template %1 
is added',$values['name']));
-                       return $receipt;
-               }
-
-               function edit_template($values)
-               {
-                       $values['name'] = 
$this->db->db_addslashes($values['name']);
-                       $values['descr'] = 
$this->db->db_addslashes($values['descr']);
-
-                       $this->db->query("UPDATE fm_template set name='" . 
$values['name'] . "', descr='" . $values['descr'] . "', chapter_id='"
-                                                       . 
(int)$values['chapter_id']
-                                                       . "' WHERE id='" . 
$values['template_id'] . "'",__LINE__,__FILE__);
-
-                       $receipt['message'][]=array('msg'=>lang('Property has 
been edited'));
-                       return $receipt;
-               }
-
-               function delete($id)
-               {
-                       $this->db->query("DELETE FROM fm_template WHERE 
id='$id'",__LINE__,__FILE__);
-                       $this->db->query("DELETE FROM fm_template_hours  WHERE 
template_id='$id'",__LINE__,__FILE__);
-               }
-
-               function delete_hour($hour_id,$template_id )
-               {
-                       $this->db->query("SELECT record FROM fm_template_hours 
where id ='$hour_id'",__LINE__,__FILE__);
-                       $this->db->next_record();
-                       $old_record     = $this->db->f('record');
-
-                       $this->db->query("DELETE FROM fm_template_hours WHERE 
id='" . $hour_id . "'",__LINE__,__FILE__);
-                       if($old_record)
-                       {
-                               $this->db->query("UPDATE fm_template_hours set 
record   = record - 1 where  template_id= '$template_id' and record > 
$old_record ",__LINE__,__FILE__);
-                       }
-
-                       $receipt['message'][] = array('msg'=>lang('hour %1 has 
been deleted',$hour_id));
-                       return $receipt;
-
-               }
-       }
-?>
+                       $this->db2              = $this->bocommon->new_db();
+
+                       $this->join                     = $this->bocommon->join;
+                       $this->left_join        = $this->bocommon->left_join;
+                       $this->like                     = $this->bocommon->like;
+               }
+
+               function read($data)
+               {
+                       if(is_array($data))
+                       {
+                               if ($data['start'])
+                               {
+                                       $start=$data['start'];
+                               }
+                               else
+                               {
+                                       $start=0;
+                               }
+                               $filter                 = 
(isset($data['filter'])?$data['filter']:'');
+                               $query                  = 
(isset($data['query'])?$data['query']:'');
+                               $sort                   = 
(isset($data['sort'])?$data['sort']:'DESC');
+                               $order                  = 
(isset($data['order'])?$data['order']:'');
+                               $chapter_id     = 
(isset($data['chapter_id'])?$data['chapter_id']:0);
+                               $allrows                = 
(isset($data['allrows'])?$data['allrows']:'');
+                               $template_id    = 
(isset($data['template_id'])?$data['template_id']:0);
+                       }
+
+                       if ($order)
+                       {
+                               $ordermethod = " order by $order $sort";
+                       }
+                       else
+                       {
+                               $ordermethod = ' order by fm_template.id desc';
+                       }
+
+                       $where= 'WHERE';
+
+                       if ($chapter_id > 0)
+                       {
+                               $filtermethod .= " $where 
chapter_id='$chapter_id' ";
+                               $where= 'AND';
+                       }
+
+                       if ($filter)
+                       {
+                               $filtermethod .= " $where 
fm_template.owner='$filter' ";
+                               $where= 'AND';
+                       }
+
+
+                       if($query)
+                       {
+                               $query = ereg_replace("'",'',$query);
+                               $query = ereg_replace('"','',$query);
+
+                               $querymethod = " $where (fm_template.descr 
$this->like '%$query%')";
+                       }
+
+                       $sql = "SELECT 
fm_template.id,fm_template.descr,fm_template.name,fm_template.owner,fm_template.entry_date,"
+                        . " fm_chapter.descr as chapter FROM fm_template 
$this->left_join fm_chapter  on fm_template.chapter_id=fm_chapter.id"
+                        . " $filtermethod $querymethod";
+
+                       $this->db2->query($sql,__LINE__,__FILE__);
+                       $this->total_records = $this->db2->num_rows();
+
+                       if(!$allrows)
+                       {
+                               $this->db->limit_query($sql . 
$ordermethod,$start,__LINE__,__FILE__);
+                       }
+                       else
+                       {
+                               $this->db->query($sql . 
$ordermethod,__LINE__,__FILE__);
+                       }
+
+                       while ($this->db->next_record())
+                       {
+                               $template_list[] = array
+                               (
+                                       'template_id'           => 
$this->db->f('id'),
+                                       'name'                          => 
stripslashes($this->db->f('name')),
+                                       'descr'                         => 
stripslashes($this->db->f('descr')),
+                                       'owner'                         => 
$this->db->f('owner'),
+                                       'entry_date'            => 
$this->db->f('entry_date'),
+                                       'chapter'                       => 
$this->db->f('chapter')
+                                       );
+                       }
+                       return $template_list;
+               }
+
+               function read_template_hour($data)
+               {
+
+                       if(is_array($data))
+                       {
+                               if ($data['start'])
+                               {
+                                       $start=$data['start'];
+                               }
+                               else
+                               {
+                                       $start=0;
+                               }
+                               $query                  = 
(isset($data['query'])?$data['query']:'');
+                               $sort                   = 
(isset($data['sort'])?$data['sort']:'DESC');
+                               $order                  = 
(isset($data['order'])?$data['order']:'');
+                               $chapter_id     = 
(isset($data['chapter_id'])?$data['chapter_id']:0);
+                               $allrows                = 
(isset($data['allrows'])?$data['allrows']:'');
+                               $template_id    = 
(isset($data['template_id'])?$data['template_id']:0);
+                       }
+
+                       if ($order)
+                       {
+                               $ordermethod = " order by $order $sort";
+                       }
+                       else
+                       {
+                               $ordermethod = ' order by record ';
+                       }
+
+
+                       $filtermethod .= " where template_id='$template_id' ";
+
+                       if($query)
+                       {
+                               $query = ereg_replace("'",'',$query);
+                               $query = ereg_replace('"','',$query);
+
+                               $querymethod = " AND (hours_descr $this->like 
'%$query%' or fm_template_hours.remark $this->like '%$query%' or ns3420_id 
$this->like '%$query%')";
+                       }
+
+                       $sql = "SELECT fm_template_hours.*, chapter_id from 
fm_template_hours  $this->join fm_template on 
fm_template.id=fm_template_hours.template_id $filtermethod $querymethod";
+
+                       $this->db2->query($sql,__LINE__,__FILE__);
+                       $this->total_records = $this->db2->num_rows();
+
+                       if(!$allrows)
+                       {
+                               $this->db->limit_query($sql . 
$ordermethod,$start,__LINE__,__FILE__);
+                       }
+                       else
+                       {
+                               $this->db->query($sql . 
$ordermethod,__LINE__,__FILE__);
+                       }
+
+                       while ($this->db->next_record())
+                       {
+                               $hour_list[] = array
+                               (
+                                       'hour_id'                       => 
$this->db->f('id'),
+                                       'chapter_id'            => 
$this->db->f('chapter_id'),
+                                       'activity_num'          => 
$this->db->f('activity_num'),
+                                       'hours_descr'           => 
stripslashes($this->db->f('hours_descr')),
+                                       'remark'                        => 
stripslashes($this->db->f('remark')),
+                                       'grouping_id'           => 
$this->db->f('grouping_id'),
+                                       'grouping_descr'        => 
$this->db->f('grouping_descr'),
+                                       'ns3420_id'                     => 
$this->db->f('ns3420_id'),
+                                       'tolerance'                     => 
$this->db->f('tolerance'),
+                                       'activity_id'           => 
$this->db->f('activity_id'),
+                                       'unit'                          => 
$this->db->f('unit'),
+                                       'record'                        => 
$this->db->f('record'),
+                                       'cost'                          => 
$this->db->f('cost'),
+                                       'billperae'                     => 
$this->db->f('billperae'),
+                                       'building_part'         => 
$this->db->f('building_part'),
+                                       'dim_d'                         => 
$this->db->f('dim_d')
+                                       );
+                       }
+                       return $hour_list;
+               }
+
+               function read_single_template($template_id)
+               {
+                       $sql = "SELECT * FROM fm_template where 
id='$template_id'";
+
+                       $this->db->query($sql,__LINE__,__FILE__);
+
+                       if ($this->db->next_record())
+                       {
+                               $template['template_id']                = 
$this->db->f('id');
+                               $template['name']                               
= stripslashes($this->db->f('name'));
+                               $template['descr']                              
= stripslashes($this->db->f('descr'));
+                               $template['chapter_id']                 = 
(int)$this->db->f('chapter_id');
+                       }
+                       return $template;
+               }
+
+               function read_single_hour($hour_id)
+               {
+                       $sql = "SELECT * from fm_template_hours where 
id='$hour_id'";
+
+                       $this->db->query($sql,__LINE__,__FILE__);
+
+                       if ($this->db->next_record())
+                       {
+                               $hour['hour_id']                                
= $this->db->f('id');
+                               $hour['record']                                 
= $this->db->f('record');
+                               $hour['activity_id']                    = 
$this->db->f('activity_id');
+                               $hour['activity_num']                   = 
$this->db->f('activity_num');
+                               $hour['grouping_id']                    = 
$this->db->f('grouping_id');
+                               $hour['hours_descr']                    = 
$this->db->f('hours_descr');
+                               $hour['remark']                                 
= $this->db->f('remark');
+                               $hour['billperae']                              
= $this->db->f('billperae');
+                               $hour['unit']                                   
= $this->db->f('unit');
+                               $hour['ns3420_id']                              
= $this->db->f('ns3420_id');
+                               $hour['tolerance_id']                   = 
(int)$this->db->f('tolerance');
+                               $hour['building_part_id']               = 
(int)$this->db->f('building_part');
+                               $hour['quantity']                               
= $this->db->f('quantity');
+                               $hour['cost']                                   
= $this->db->f('cost');
+                               $hour['dim_d']                                  
= $this->db->f('dim_d');
+                       }
+
+                               return $hour;
+               }
+
+               function next_record($template_id)
+               {
+
+                       $this->db->query("SELECT  max(record) as record FROM 
fm_template_hours where template_id='$template_id'",__LINE__,__FILE__);
+                       $this->db->next_record();
+                       $record = $this->db->f('record')+1;
+                       return $record;
+
+               }
+
+               function add_custom_hour($hour,$template_id)
+               {
+
+                       $hour['record'] = $this->next_record($template_id);
+
+                       $this->db->query("UPDATE fm_template set
+                               chapter_id      ='" . $hour['chapter_id'] . "' 
WHERE id= '$template_id'",__LINE__,__FILE__);
+
+                       if($hour['grouping_id'])
+                       {
+                               $this->db->query("SELECT grouping_descr , 
max(record) as record FROM fm_template_hours where grouping_id='" 
.$hour['grouping_id'] . "' and template_id= '$template_id' GROUP by 
grouping_descr",__LINE__,__FILE__);
+                               $this->db->next_record();
+                               $hour['grouping_descr'] = 
$this->db->f('grouping_descr');
+                       }
+
+                       if($hour['new_grouping'])
+                       {
+                               $this->db->query("SELECT grouping_id FROM 
fm_template_hours where grouping_descr ='" .$hour['new_grouping'] . "' and 
template_id= '$template_id'",__LINE__,__FILE__);
+                               $this->db->next_record();
+                               if ( $this->db->f('grouping_id'))
+                               {
+                                       $hour['grouping_id']    = 
$this->db->f('grouping_id');
+                               }
+                               else
+                               {
+                                       $this->db->query("SELECT 
max(grouping_id) as grouping_id FROM fm_template_hours where template_id= 
'$template_id'",__LINE__,__FILE__);
+                                       $this->db->next_record();
+                                       $hour['grouping_id']    = 
$this->db->f('grouping_id')+1;
+                               }
+
+                               $hour['grouping_descr'] = $hour['new_grouping'];
+                       }
+
+                       $this->db->query("insert into fm_template_hours 
(owner,hours_descr,unit,cost,quantity,billperae,ns3420_id,dim_d,"
+                               . " 
grouping_id,grouping_descr,record,building_part,tolerance,remark,entry_date,template_id)
 "
+                               . " values ('" .
+                                       $this->account . "','" .
+                                       $hour['descr'] . "','" .
+                                       $hour['unit'] . "','" .
+                                       $hour['cost'] . "','" .
+                                       $hour['quantity'] . "','" .
+                                       $hour['billperae'] . "','" .
+                                       $hour['ns3420_id'] . "','" .
+                                       $hour['dim_d'] . "','" .
+                                       $hour['grouping_id'] . "','" .
+                                       $hour['grouping_descr'] . "','" .
+                                       $hour['record'] . "','" .
+                                       $hour['building_part_id'] . "','" .
+                                       $hour['tolerance_id'] . "','" .
+                                       $hour['remark'] . "','" .
+                                       time() . "','" .
+                                       $template_id . "')",__LINE__,__FILE__);
+
+                       $receipt['hour_id'] = 
$this->db->get_last_insert_id('fm_template_hours','id');
+
+                       $receipt['message'][] = array('msg'=>lang('hour %1 is 
added!',$hour['record']));
+
+                       return $receipt;
+               }
+
+
+               function edit_hour($hour,$template_id)
+               {
+
+                       $hour['descr'] = 
$this->db->db_addslashes($hour['descr']);
+                       $hour['remark'] = 
$this->db->db_addslashes($hour['remark']);
+
+                       $this->db->query("UPDATE fm_template set
+                               chapter_id      ='" . $hour['chapter_id'] . "' 
WHERE id= '$template_id'",__LINE__,__FILE__);
+
+                       if($hour['new_grouping'])
+                       {
+                               $this->db->query("SELECT grouping_id FROM 
fm_template_hours where grouping_descr ='" .$hour['new_grouping'] . "' and 
template_id= '$template_id'",__LINE__,__FILE__);
+                               $this->db->next_record();
+                               if ( $this->db->f('grouping_id'))
+                               {
+                                       $hour['grouping_id']    = 
$this->db->f('grouping_id');
+                               }
+                               else
+                               {
+
+                                       $this->db->query("UPDATE 
fm_template_hours set grouping_id = NULL WHERE id ='" .$hour['hour_id'] . 
"'",__LINE__,__FILE__);
+                                       $this->db->query("SELECT 
count(grouping_id) as num_grouping FROM fm_template_hours where template_id= 
'$template_id' and grouping_id >0 ",__LINE__,__FILE__);
+                                       $this->db->next_record();
+                                       if ($this->db->f('num_grouping')==1)
+                                       {
+                                               $hour['grouping_id']=1;
+                                       }
+                                       else
+                                       {
+                                               $this->db->query("SELECT 
max(grouping_id) as grouping_id FROM fm_template_hours where template_id= 
'$template_id'",__LINE__,__FILE__);
+                                               $this->db->next_record();
+                                               $hour['grouping_id']    = 
$this->db->f('grouping_id')+1;
+                                       }
+                               }
+                               $hour['grouping_descr'] = $hour['new_grouping'];
+                       }
+                       else
+                       {
+                               $this->db->query("SELECT 
grouping_id,grouping_descr FROM fm_template_hours where id ='" 
.$hour['hour_id'] . "'",__LINE__,__FILE__);
+                               $this->db->next_record();
+                               $old_grouping_id        = 
$this->db->f('grouping_id');
+
+                               if ( $old_grouping_id == $hour['grouping_id'])
+                               {
+
+                                       $hour['grouping_descr'] = 
$this->db->f('grouping_descr');
+                               }
+                               else
+                               {
+                                       $this->db->query("SELECT grouping_descr 
, max(record) as record FROM fm_template_hours where grouping_id='" 
.$hour['grouping_id'] . "' and template_id= '$template_id' GROUP by 
grouping_descr",__LINE__,__FILE__);
+                                       $this->db->next_record();
+                                       if($this->db->f('grouping_descr'))
+                                       {
+                                               $hour['grouping_descr'] = 
$this->db->f('grouping_descr');
+                                       }
+                                       else
+                                       {
+                                               $hour['grouping_id']='';
+                                               $hour['grouping_descr']='';
+                                       }
+
+                               }
+                       }
+
+                       $this->db->query("SELECT record FROM fm_template_hours 
where id ='" .$hour['hour_id'] . "'",__LINE__,__FILE__);
+                       $this->db->next_record();
+                       $hour['record'] = $this->db->f('record');
+
+                       $this->db->query("UPDATE fm_template_hours set
+                               hours_descr                     ='" . 
$hour['descr'] . "',
+                               remark                          ='" . 
$hour['remark'] . "',
+                               billperae                       ='" . 
$hour['billperae'] . "',
+                               unit                            ='" . 
$hour['unit'] . "',
+                               quantity                        ='" . 
$hour['quantity'] . "',
+                               cost                            ='" . 
$hour['cost'] . "',
+                               ns3420_id                       ='" . 
$hour['ns3420_id'] . "',
+                               tolerance                       ='" . 
$hour['tolerance_id'] . "',
+                               building_part           ='" . 
$hour['building_part_id'] . "',
+                               dim_d                           ='" . 
$hour['dim_d'] . "',
+                               grouping_id                     ='" . 
$hour['grouping_id'] . "',
+                               grouping_descr          ='" . 
$hour['grouping_descr']
+                               . "' WHERE id= '" . $hour['hour_id'] 
."'",__LINE__,__FILE__);
+
+                       $receipt['hour_id'] = $hour['hour_id'];
+                       $receipt['message'][] = array('msg'=>lang('hour %1 has 
been edited',$hour['record']));
+                       return $receipt;
+
+               }
+
+               function get_grouping_list($template_id='')
+               {
+                       $this->db->query("SELECT grouping_id, grouping_descr 
FROM fm_template_hours where template_id='$template_id' and grouping_id >0 
group by grouping_id, grouping_descr");
+
+                       $i = 0;
+                       while ($this->db->next_record())
+                       {
+                               $grouping_entries[$i]['id']                     
        = $this->db->f('grouping_id');
+                               $grouping_entries[$i]['name']                   
        = stripslashes($this->db->f('grouping_descr'));
+                               $i++;
+                       }
+                       return $grouping_entries;
+               }
+
+               function add_template($values)
+               {
+
+                       $values['name'] = 
$this->db->db_addslashes($values['name']);
+
+                       $this->db->query("insert into fm_template 
(owner,name,descr,chapter_id,entry_date) "
+                               . " values ('" .
+                                       $this->account . "','" .
+                                       $values['name'] . "','" .
+                                       $values['descr'] . "','" .
+                                       $values['chapter_id'] . "','" .
+                                       time() . "')",__LINE__,__FILE__);
+
+                       $template_id = 
$this->db->get_last_insert_id('fm_template','id');
+
+                       $receipt['template_id'] = $template_id;
+                       $receipt['message'][] = array('msg'=>lang('template %1 
is added',$values['name']));
+                       return $receipt;
+               }
+
+               function edit_template($values)
+               {
+                       $values['name'] = 
$this->db->db_addslashes($values['name']);
+                       $values['descr'] = 
$this->db->db_addslashes($values['descr']);
+
+                       $this->db->query("UPDATE fm_template set name='" . 
$values['name'] . "', descr='" . $values['descr'] . "', chapter_id='"
+                                                       . 
(int)$values['chapter_id']
+                                                       . "' WHERE id='" . 
$values['template_id'] . "'",__LINE__,__FILE__);
+
+                       $receipt['message'][]=array('msg'=>lang('Property has 
been edited'));
+                       return $receipt;
+               }
+
+               function delete($id)
+               {
+                       $this->db->query("DELETE FROM fm_template WHERE 
id='$id'",__LINE__,__FILE__);
+                       $this->db->query("DELETE FROM fm_template_hours  WHERE 
template_id='$id'",__LINE__,__FILE__);
+               }
+
+               function delete_hour($hour_id,$template_id )
+               {
+                       $this->db->query("SELECT record FROM fm_template_hours 
where id ='$hour_id'",__LINE__,__FILE__);
+                       $this->db->next_record();
+                       $old_record     = $this->db->f('record');
+
+                       $this->db->query("DELETE FROM fm_template_hours WHERE 
id='" . $hour_id . "'",__LINE__,__FILE__);
+                       if($old_record)
+                       {
+                               $this->db->query("UPDATE fm_template_hours set 
record   = record - 1 where  template_id= '$template_id' and record > 
$old_record ",__LINE__,__FILE__);
+                       }
+
+                       $receipt['message'][] = array('msg'=>lang('hour %1 has 
been deleted',$hour_id));
+                       return $receipt;
+
+               }
+       }
+?>
Index: property/inc/class.sotenant_claim.inc.php
diff -u property/inc/class.sotenant_claim.inc.php:1.8 
property/inc/class.sotenant_claim.inc.php:1.9
--- property/inc/class.sotenant_claim.inc.php:1.8       Fri Jan 27 14:05:43 2006
+++ property/inc/class.sotenant_claim.inc.php   Mon Jan 30 22:14:19 2006
@@ -1,306 +1,305 @@
-<?php
-       /**
-       * phpGroupWare - property: a Facilities Management System.
-       *
-       * @author Sigurd Nes <address@hidden>
-       * @copyright Copyright (C) 2003-2005 Free Software Foundation, Inc. 
http://www.fsf.org/
-       * @license http://www.gnu.org/licenses/gpl.html GNU General Public 
License
-       * @internal Development of this application was funded by 
http://www.bergen.kommune.no/bbb_/ekstern/
-       * @package property
-       * @subpackage project
-       * @version $Id: class.sotenant_claim.inc.php,v 1.8 2006/01/27 14:05:43 
sigurdne Exp $
-       */
-
-       /**
-        * Description
-        * @package property
-        */
-
-
-       class sotenant_claim
-       {
-
-               function sotenant_claim()
-               {
-                       $this->currentapp       = 
$GLOBALS['phpgw_info']['flags']['currentapp'];
-                       $this->account  = 
$GLOBALS['phpgw_info']['user']['account_id'];
-                       $this->bocommon         = 
CreateObject($this->currentapp.'.bocommon');
+<?php
+       /**
+       * phpGroupWare - property: a Facilities Management System.
+       *
+       * @author Sigurd Nes <address@hidden>
+       * @copyright Copyright (C) 2003-2005 Free Software Foundation, Inc. 
http://www.fsf.org/
+       * @license http://www.gnu.org/licenses/gpl.html GNU General Public 
License
+       * @internal Development of this application was funded by 
http://www.bergen.kommune.no/bbb_/ekstern/
+       * @package property
+       * @subpackage project
+       * @version $Id: class.sotenant_claim.inc.php,v 1.9 2006/01/30 22:14:19 
sigurdne Exp $
+       */
+
+       /**
+        * Description
+        * @package property
+        */
+
+       class sotenant_claim
+       {
+               function sotenant_claim()
+               {
+                       $this->currentapp       = 
$GLOBALS['phpgw_info']['flags']['currentapp'];
+                       $this->account  = 
$GLOBALS['phpgw_info']['user']['account_id'];
+                       $this->bocommon         = 
CreateObject($this->currentapp.'.bocommon');
                        $this->db               = $this->bocommon->new_db();
-                       $this->db2              = $this->bocommon->new_db();
-                       $this->join                     = $this->bocommon->join;
-               }
-
-               function select_category_list()
-               {
-                       $this->db->query("SELECT id,descr FROM 
fm_tenant_claim_category  ORDER BY descr ",__LINE__,__FILE__);
-
-                       $i = 0;
-                       while ($this->db->next_record())
-                       {
-                               $categories[$i]['id']           = 
$this->db->f('id');
-                               $categories[$i]['name']         = 
stripslashes($this->db->f('descr'));
-                               $i++;
-                       }
-                       return $categories;
-               }
-
-               function read($data)
-               {
-                       if(is_array($data))
-                       {
-                               $start  = 
(isset($data['start'])?$data['start']:0);
-                               $filter = 
(isset($data['filter'])?$data['filter']:'none');
-                               $status = 
(isset($data['status'])?$data['status']:'open');
-                               $query = 
(isset($data['query'])?$data['query']:'');
-                               $sort = 
(isset($data['sort'])?$data['sort']:'DESC');
-                               $order = 
(isset($data['order'])?$data['order']:'');
-                               $cat_id = 
(isset($data['cat_id'])?$data['cat_id']:0);
-                               $allrows        = 
(isset($data['allrows'])?$data['allrows']:'');
-                               $project_id     = 
(isset($data['project_id'])?$data['project_id']:'');
-
-                       }
-
-                       if ($order)
-                       {
-                               $ordermethod = " order by $order $sort";
-                       }
-                       else
-                       {
-                               $ordermethod = ' order by id DESC';
-                       }
-
-
-                       $where = 'WHERE';
-                       if ($cat_id > 0)
-                       {
-                               $filtermethod .= " $where category='$cat_id' ";
-                               $where = 'AND';
-                       }
-
-                       if ($project_id > 0)
-                       {
-                               $filtermethod .= " $where 
project_id='$project_id' ";
-                               $where = 'AND';
-                       }
-
-                       if ($status == 'closed'):
-                       {
-                               $filtermethod .= " $where 
fm_tenant_claim.status='closed'";
-                               $where = 'AND';
-                       }
-                       elseif($status == ''):
-                       {
-                               $filtermethod .= " $where 
fm_tenant_claim.status='open'";
-                               $where = 'AND';
-                       }
-                       endif;
-
-                       if($query)
-                       {
-                               $query = ereg_replace("'",'',$query);
-                               $query = ereg_replace('"','',$query);
-
-                               $querymethod = " $where ( abid = '$query' or 
org_name LIKE '%$query%')";
-                       }
-
-                       $sql = "SELECT fm_tenant_claim.*, descr as category 
FROM fm_tenant_claim $this->join fm_tenant_claim_category on 
fm_tenant_claim.category=fm_tenant_claim_category.id $filtermethod 
$querymethod";
-
-                       $this->db2->query($sql,__LINE__,__FILE__);
-                       $this->total_records = $this->db2->num_rows();
-
-                       if(!$allrows)
-                       {
-                               $this->db->limit_query($sql . 
$ordermethod,$start,__LINE__,__FILE__);
-                       }
-                       else
-                       {
-                               $this->db->query($sql . 
$ordermethod,__LINE__,__FILE__);
-                       }
-
-                       while ($this->db->next_record())
-                       {
-                               $claims[] = array
-                               (
-                                       'claim_id'              => 
$this->db->f('id'),
-                                       'project_id'    => 
$this->db->f('project_id'),
-                                       'tenant_id'             => 
$this->db->f('tenant_id'),
-                                       'remark'                => 
stripslashes($this->db->f('remark')),
-                                       'entry_date'    => 
$this->db->f('entry_date'),
-                                       'category'              => 
$this->db->f('category'),
-                                       'status'                => 
$this->db->f('status')
-                               );
-                       }
-                       return $claims;
-               }
-
-               function check_claim_project($project_id)
-               {
-                       $sql = "SELECT fm_tenant_claim.*, descr as category 
FROM fm_tenant_claim"
-                        . " $this->join fm_tenant_claim_category on 
fm_tenant_claim.category=fm_tenant_claim_category.id"
-                        . " WHERE project_id = $project_id";
-
-                       $this->db2->query($sql,__LINE__,__FILE__);
-                       $this->total_records = $this->db2->num_rows();
-
-                       $this->db->query($sql . $ordermethod,__LINE__,__FILE__);
-
-                       while ($this->db->next_record())
-                       {
-                               $claims[] = array
-                               (
-                                       'claim_id'              => 
$this->db->f('id'),
-                                       'project_id'    => 
$this->db->f('project_id'),
-                                       'tenant_id'             => 
$this->db->f('tenant_id'),
-                                       'entry_date'    => 
$this->db->f('entry_date'),
-                                       'category'              => 
$this->db->f('category')
-                               );
-                       }
-                       return $claims;
-               }
-
-               function check_claim_workorder($workorder_id)
-               {
-                       $this->db->query("select * from fm_origin WHERE 
destination ='tenant_claim' AND origin_id='$workorder_id'",__LINE__,__FILE__);
-
-                       while ($this->db->next_record())
-                       {
-                               $claim[] = $this->db->f('destination_id');
-                       }
-
-                       return @implode(",", $claim);
-               }
-
-               function read_single($id)
-               {
-                       $this->db->query("select * from fm_tenant_claim where 
id='$id'",__LINE__,__FILE__);
-
-                       if ($this->db->next_record())
-                       {
-                               $claim['id']                    = $id;
-                               $claim['project_id']    = 
$this->db->f('project_id');
-                               $claim['tenant_id']             = 
$this->db->f('tenant_id');
-                               $claim['remark']                = 
stripslashes($this->db->f('remark'));
-                               $claim['entry_date']    = 
$this->db->f('entry_date');
-                               $claim['cat_id']                = 
(int)$this->db->f('category');
-                               $claim['amount']                = 
$this->db->f('amount');
-                               $claim['b_account_id']  = 
$this->db->f('b_account_id');
-                               $claim['cat_id']                = 
(int)$this->db->f('category');
-                               $claim['status']                = 
$this->db->f('status');
-
-                       }
-
-                       $this->db->query("select * from fm_origin WHERE 
destination ='tenant_claim' AND destination_id='$id'",__LINE__,__FILE__);
-
-                       while ($this->db->next_record())
-                       {
-                               $claim['workorder'][] = 
$this->db->f('origin_id');
-                       }
-
-                       return $claim;
-               }
-
-               function add($claim)
-               {
-                       $this->db->transaction_begin();
-
-                       $claim['name'] = 
$this->db->db_addslashes($claim['name']);
-                       $claim['amount'] =  
str_replace(",",".",$claim['amount']);
-
-                       $values_insert= array(
-                               $claim['project_id'],
-                               $claim['tenant_id'],
-                               $claim['amount'],
-                               $claim['b_account_id'],
-                               $claim['cat_id'],
-                               $claim['remark'],
-                               $this->account,
-                               time(),
-                               $claim['status']
-                               );
-
-                       $values_insert  = 
$this->bocommon->validate_db_insert($values_insert);
-
-
-                       $this->db->query("INSERT INTO fm_tenant_claim 
(project_id,tenant_id,amount,b_account_id,category,remark,user_id,entry_date,status)
 "
-                               . "VALUES ($values_insert)",__LINE__,__FILE__);
-
-                       $claim_id = 
$this->db->get_last_insert_id('fm_tenant_claim','id');
-                       $receipt['claim_id'] = $claim_id;
-
-                       foreach ($claim['workorder'] as $workorder_id)
-                       {
-                               $this->db->query("INSERT INTO fm_origin 
(origin,origin_id,destination,destination_id,entry_date,user_id) "
-                               . "VALUES ('workorder',"
-                               . $workorder_id .","
-                               . "'tenant_claim',"
-                               . $claim_id . ","
-                               . time().","
-                               . $this->account .")",__LINE__,__FILE__);
-
-                               $this->db->query("UPDATE fm_workorder set 
claim_issued = 1 WHERE id=" . $workorder_id ,__LINE__,__FILE__);
-                       }
-
-                       $this->db->transaction_commit();
-
-                       $receipt['message'][] = array('msg'=>lang('claim %1 has 
been saved',$claim_id));
-                       return $receipt;
-               }
-
-               function edit($claim)
-               {
-                       $this->db->transaction_begin();
-
-                       $claim['name'] = 
$this->db->db_addslashes($claim['name']);
-                       $claim['amount'] =  
str_replace(",",".",$claim['amount']);
-
-                       $value_set=array(
-                               'amount'                        => 
$claim['amount'],
-                               'tenant_id'                     => 
$claim['tenant_id'],
-                               'b_account_id'          => 
$claim['b_account_id'],
-                               'amount'                        => 
$claim['amount'],
-                               'category'                      => 
$claim['cat_id'],
-                               'status'                        => 
$claim['status'],
-                               'user_id'                       => 
$this->account,
-                               'remark'                        => 
$this->db->db_addslashes($claim['remark'])
-                               );
-
-                       $value_set      = 
$this->bocommon->validate_db_update($value_set);
-
-                       $this->db->query("UPDATE fm_tenant_claim set $value_set 
 WHERE id=" . intval($claim['claim_id']),__LINE__,__FILE__);
-
-                       $claim_id = $claim['claim_id'];
-
-                       $this->db->query("DELETE FROM fm_origin WHERE 
destination ='tenant_claim' AND destination_id=$claim_id",__LINE__,__FILE__);
-
-                       $this->db->query("UPDATE fm_workorder set claim_issued 
= NULL WHERE id=" . $claim['project_id'] ,__LINE__,__FILE__);
-
-                       foreach ($claim['workorder'] as $workorder_id)
-                       {
-                               $this->db->query("INSERT INTO fm_origin 
(origin,origin_id,destination,destination_id,entry_date,user_id) "
-                               . "VALUES ('workorder',"
-                               . $workorder_id .","
-                               . "'tenant_claim',"
-                               . $claim_id . ","
-                               . time().","
-                               . $this->account .")",__LINE__,__FILE__);
-
-                               $this->db->query("UPDATE fm_workorder set 
claim_issued = 1 WHERE id=" . $workorder_id ,__LINE__,__FILE__);
-                       }
-
-                       $this->db->transaction_commit();
-
-                       $receipt['claim_id']= $claim['claim_id'];
-                       $receipt['message'][] = array('msg'=>lang('claim %1 has 
been edited',$claim['claim_id']));
-                       return $receipt;
-               }
-
-               function delete($id)
-               {
-                       $this->db->transaction_begin();
-                       $this->db->query('DELETE FROM fm_tenant_claim WHERE 
id=' . intval($id),__LINE__,__FILE__);
-                       $this->db->query("DELETE FROM fm_origin WHERE 
destination ='tenant_claim' AND destination_id=$id",__LINE__,__FILE__);
-                       $this->db->transaction_commit();
-
-               }
-       }
-?>
+                       $this->db2              = $this->bocommon->new_db();
+
+                       $this->join                     = $this->bocommon->join;
+               }
+
+               function select_category_list()
+               {
+                       $this->db->query("SELECT id,descr FROM 
fm_tenant_claim_category  ORDER BY descr ",__LINE__,__FILE__);
+
+                       $i = 0;
+                       while ($this->db->next_record())
+                       {
+                               $categories[$i]['id']           = 
$this->db->f('id');
+                               $categories[$i]['name']         = 
stripslashes($this->db->f('descr'));
+                               $i++;
+                       }
+                       return $categories;
+               }
+
+               function read($data)
+               {
+                       if(is_array($data))
+                       {
+                               $start  = 
(isset($data['start'])?$data['start']:0);
+                               $filter = 
(isset($data['filter'])?$data['filter']:'none');
+                               $status = 
(isset($data['status'])?$data['status']:'open');
+                               $query = 
(isset($data['query'])?$data['query']:'');
+                               $sort = 
(isset($data['sort'])?$data['sort']:'DESC');
+                               $order = 
(isset($data['order'])?$data['order']:'');
+                               $cat_id = 
(isset($data['cat_id'])?$data['cat_id']:0);
+                               $allrows        = 
(isset($data['allrows'])?$data['allrows']:'');
+                               $project_id     = 
(isset($data['project_id'])?$data['project_id']:'');
+
+                       }
+
+                       if ($order)
+                       {
+                               $ordermethod = " order by $order $sort";
+                       }
+                       else
+                       {
+                               $ordermethod = ' order by id DESC';
+                       }
+
+
+                       $where = 'WHERE';
+                       if ($cat_id > 0)
+                       {
+                               $filtermethod .= " $where category='$cat_id' ";
+                               $where = 'AND';
+                       }
+
+                       if ($project_id > 0)
+                       {
+                               $filtermethod .= " $where 
project_id='$project_id' ";
+                               $where = 'AND';
+                       }
+
+                       if ($status == 'closed'):
+                       {
+                               $filtermethod .= " $where 
fm_tenant_claim.status='closed'";
+                               $where = 'AND';
+                       }
+                       elseif($status == ''):
+                       {
+                               $filtermethod .= " $where 
fm_tenant_claim.status='open'";
+                               $where = 'AND';
+                       }
+                       endif;
+
+                       if($query)
+                       {
+                               $query = ereg_replace("'",'',$query);
+                               $query = ereg_replace('"','',$query);
+
+                               $querymethod = " $where ( abid = '$query' or 
org_name LIKE '%$query%')";
+                       }
+
+                       $sql = "SELECT fm_tenant_claim.*, descr as category 
FROM fm_tenant_claim $this->join fm_tenant_claim_category on 
fm_tenant_claim.category=fm_tenant_claim_category.id $filtermethod 
$querymethod";
+
+                       $this->db2->query($sql,__LINE__,__FILE__);
+                       $this->total_records = $this->db2->num_rows();
+
+                       if(!$allrows)
+                       {
+                               $this->db->limit_query($sql . 
$ordermethod,$start,__LINE__,__FILE__);
+                       }
+                       else
+                       {
+                               $this->db->query($sql . 
$ordermethod,__LINE__,__FILE__);
+                       }
+
+                       while ($this->db->next_record())
+                       {
+                               $claims[] = array
+                               (
+                                       'claim_id'              => 
$this->db->f('id'),
+                                       'project_id'    => 
$this->db->f('project_id'),
+                                       'tenant_id'             => 
$this->db->f('tenant_id'),
+                                       'remark'                => 
stripslashes($this->db->f('remark')),
+                                       'entry_date'    => 
$this->db->f('entry_date'),
+                                       'category'              => 
$this->db->f('category'),
+                                       'status'                => 
$this->db->f('status')
+                               );
+                       }
+                       return $claims;
+               }
+
+               function check_claim_project($project_id)
+               {
+                       $sql = "SELECT fm_tenant_claim.*, descr as category 
FROM fm_tenant_claim"
+                        . " $this->join fm_tenant_claim_category on 
fm_tenant_claim.category=fm_tenant_claim_category.id"
+                        . " WHERE project_id = $project_id";
+
+                       $this->db2->query($sql,__LINE__,__FILE__);
+                       $this->total_records = $this->db2->num_rows();
+
+                       $this->db->query($sql . $ordermethod,__LINE__,__FILE__);
+
+                       while ($this->db->next_record())
+                       {
+                               $claims[] = array
+                               (
+                                       'claim_id'              => 
$this->db->f('id'),
+                                       'project_id'    => 
$this->db->f('project_id'),
+                                       'tenant_id'             => 
$this->db->f('tenant_id'),
+                                       'entry_date'    => 
$this->db->f('entry_date'),
+                                       'category'              => 
$this->db->f('category')
+                               );
+                       }
+                       return $claims;
+               }
+
+               function check_claim_workorder($workorder_id)
+               {
+                       $this->db->query("select * from fm_origin WHERE 
destination ='tenant_claim' AND origin_id='$workorder_id'",__LINE__,__FILE__);
+
+                       while ($this->db->next_record())
+                       {
+                               $claim[] = $this->db->f('destination_id');
+                       }
+
+                       return @implode(",", $claim);
+               }
+
+               function read_single($id)
+               {
+                       $this->db->query("select * from fm_tenant_claim where 
id='$id'",__LINE__,__FILE__);
+
+                       if ($this->db->next_record())
+                       {
+                               $claim['id']                    = $id;
+                               $claim['project_id']    = 
$this->db->f('project_id');
+                               $claim['tenant_id']             = 
$this->db->f('tenant_id');
+                               $claim['remark']                = 
stripslashes($this->db->f('remark'));
+                               $claim['entry_date']    = 
$this->db->f('entry_date');
+                               $claim['cat_id']                = 
(int)$this->db->f('category');
+                               $claim['amount']                = 
$this->db->f('amount');
+                               $claim['b_account_id']  = 
$this->db->f('b_account_id');
+                               $claim['cat_id']                = 
(int)$this->db->f('category');
+                               $claim['status']                = 
$this->db->f('status');
+
+                       }
+
+                       $this->db->query("select * from fm_origin WHERE 
destination ='tenant_claim' AND destination_id='$id'",__LINE__,__FILE__);
+
+                       while ($this->db->next_record())
+                       {
+                               $claim['workorder'][] = 
$this->db->f('origin_id');
+                       }
+
+                       return $claim;
+               }
+
+               function add($claim)
+               {
+                       $this->db->transaction_begin();
+
+                       $claim['name'] = 
$this->db->db_addslashes($claim['name']);
+                       $claim['amount'] =  
str_replace(",",".",$claim['amount']);
+
+                       $values_insert= array(
+                               $claim['project_id'],
+                               $claim['tenant_id'],
+                               $claim['amount'],
+                               $claim['b_account_id'],
+                               $claim['cat_id'],
+                               $claim['remark'],
+                               $this->account,
+                               time(),
+                               $claim['status']
+                               );
+
+                       $values_insert  = 
$this->bocommon->validate_db_insert($values_insert);
+
+
+                       $this->db->query("INSERT INTO fm_tenant_claim 
(project_id,tenant_id,amount,b_account_id,category,remark,user_id,entry_date,status)
 "
+                               . "VALUES ($values_insert)",__LINE__,__FILE__);
+
+                       $claim_id = 
$this->db->get_last_insert_id('fm_tenant_claim','id');
+                       $receipt['claim_id'] = $claim_id;
+
+                       foreach ($claim['workorder'] as $workorder_id)
+                       {
+                               $this->db->query("INSERT INTO fm_origin 
(origin,origin_id,destination,destination_id,entry_date,user_id) "
+                               . "VALUES ('workorder',"
+                               . $workorder_id .","
+                               . "'tenant_claim',"
+                               . $claim_id . ","
+                               . time().","
+                               . $this->account .")",__LINE__,__FILE__);
+
+                               $this->db->query("UPDATE fm_workorder set 
claim_issued = 1 WHERE id=" . $workorder_id ,__LINE__,__FILE__);
+                       }
+
+                       $this->db->transaction_commit();
+
+                       $receipt['message'][] = array('msg'=>lang('claim %1 has 
been saved',$claim_id));
+                       return $receipt;
+               }
+
+               function edit($claim)
+               {
+                       $this->db->transaction_begin();
+
+                       $claim['name'] = 
$this->db->db_addslashes($claim['name']);
+                       $claim['amount'] =  
str_replace(",",".",$claim['amount']);
+
+                       $value_set=array(
+                               'amount'                        => 
$claim['amount'],
+                               'tenant_id'                     => 
$claim['tenant_id'],
+                               'b_account_id'          => 
$claim['b_account_id'],
+                               'amount'                        => 
$claim['amount'],
+                               'category'                      => 
$claim['cat_id'],
+                               'status'                        => 
$claim['status'],
+                               'user_id'                       => 
$this->account,
+                               'remark'                        => 
$this->db->db_addslashes($claim['remark'])
+                               );
+
+                       $value_set      = 
$this->bocommon->validate_db_update($value_set);
+
+                       $this->db->query("UPDATE fm_tenant_claim set $value_set 
 WHERE id=" . intval($claim['claim_id']),__LINE__,__FILE__);
+
+                       $claim_id = $claim['claim_id'];
+
+                       $this->db->query("DELETE FROM fm_origin WHERE 
destination ='tenant_claim' AND destination_id=$claim_id",__LINE__,__FILE__);
+
+                       $this->db->query("UPDATE fm_workorder set claim_issued 
= NULL WHERE id=" . $claim['project_id'] ,__LINE__,__FILE__);
+
+                       foreach ($claim['workorder'] as $workorder_id)
+                       {
+                               $this->db->query("INSERT INTO fm_origin 
(origin,origin_id,destination,destination_id,entry_date,user_id) "
+                               . "VALUES ('workorder',"
+                               . $workorder_id .","
+                               . "'tenant_claim',"
+                               . $claim_id . ","
+                               . time().","
+                               . $this->account .")",__LINE__,__FILE__);
+
+                               $this->db->query("UPDATE fm_workorder set 
claim_issued = 1 WHERE id=" . $workorder_id ,__LINE__,__FILE__);
+                       }
+
+                       $this->db->transaction_commit();
+
+                       $receipt['claim_id']= $claim['claim_id'];
+                       $receipt['message'][] = array('msg'=>lang('claim %1 has 
been edited',$claim['claim_id']));
+                       return $receipt;
+               }
+
+               function delete($id)
+               {
+                       $this->db->transaction_begin();
+                       $this->db->query('DELETE FROM fm_tenant_claim WHERE 
id=' . intval($id),__LINE__,__FILE__);
+                       $this->db->query("DELETE FROM fm_origin WHERE 
destination ='tenant_claim' AND destination_id=$id",__LINE__,__FILE__);
+                       $this->db->transaction_commit();
+
+               }
+       }
+?>
Index: property/inc/class.sotts.inc.php
diff -u property/inc/class.sotts.inc.php:1.12 
property/inc/class.sotts.inc.php:1.13
--- property/inc/class.sotts.inc.php:1.12       Fri Jan 27 14:05:43 2006
+++ property/inc/class.sotts.inc.php    Mon Jan 30 22:14:19 2006
@@ -1,454 +1,451 @@
-<?php
-       /**
-       * phpGroupWare - property: a Facilities Management System.
-       *
-       * @author Sigurd Nes <address@hidden>
-       * @copyright Copyright (C) 2003-2005 Free Software Foundation, Inc. 
http://www.fsf.org/
-       * @license http://www.gnu.org/licenses/gpl.html GNU General Public 
License
-       * @internal Development of this application was funded by 
http://www.bergen.kommune.no/bbb_/ekstern/
-       * @package property
-       * @subpackage helpdesk
-       * @version $Id: class.sotts.inc.php,v 1.12 2006/01/27 14:05:43 sigurdne 
Exp $
-       */
-
-       /**
-        * Description
-        * @package property
-        */
-
-       class sotts
-       {
-
-               function sotts()
-               {
+<?php
+       /**
+       * phpGroupWare - property: a Facilities Management System.
+       *
+       * @author Sigurd Nes <address@hidden>
+       * @copyright Copyright (C) 2003-2005 Free Software Foundation, Inc. 
http://www.fsf.org/
+       * @license http://www.gnu.org/licenses/gpl.html GNU General Public 
License
+       * @internal Development of this application was funded by 
http://www.bergen.kommune.no/bbb_/ekstern/
+       * @package property
+       * @subpackage helpdesk
+       * @version $Id: class.sotts.inc.php,v 1.13 2006/01/30 22:14:19 sigurdne 
Exp $
+       */
+
+       /**
+        * Description
+        * @package property
+        */
+
+       class sotts
+       {
+
+               function sotts()
+               {
                        $this->currentapp       = 
$GLOBALS['phpgw_info']['flags']['currentapp'];
-                       $this->account          = 
$GLOBALS['phpgw_info']['user']['account_id'];
-                       $this->historylog       = 
CreateObject($this->currentapp.'.historylog','tts');
-                       $this->config           = 
CreateObject('phpgwapi.config');
-                       $this->bocommon         = 
CreateObject($this->currentapp.'.bocommon');
+                       $this->account          = 
$GLOBALS['phpgw_info']['user']['account_id'];
+                       $this->historylog       = 
CreateObject($this->currentapp.'.historylog','tts');
+                       $this->config           = 
CreateObject('phpgwapi.config');
+                       $this->bocommon         = 
CreateObject($this->currentapp.'.bocommon');
                        $this->db               = $this->bocommon->new_db();
                        $this->db2              = $this->bocommon->new_db();
-                       $this->join                     = $this->bocommon->join;
-                       $this->like                     = $this->bocommon->like;
-                       $this->soadmin_entity   = 
CreateObject($this->currentapp.'.soadmin_entity');
-                       $this->dateformat = 
$GLOBALS['phpgw_info']['user']['preferences']['common']['dateformat'];
-
-               }
-
-               function get_category_name($cat_id)
-               {
-                       $this->db->query("SELECT descr FROM fm_tts_category  
WHERE id='$cat_id' ");
-
-                       $this->db->next_record();
-
-                       return stripslashes($this->db->f('descr'));
-               }
-
-               function select_category_list()
-               {
-                       $this->db->query("SELECT id, descr FROM fm_tts_category 
 ORDER BY descr ");
-
-                       $i = 0;
-                       while ($this->db->next_record())
-                       {
-                               $categories[$i]['id']                           
= $this->db->f('id');
-                               $categories[$i]['name']                         
= stripslashes($this->db->f('descr'));
-                               $i++;
-                       }
-                       return $categories;
-               }
-
-
-               function read($data)
-               {
-
-                       if(is_array($data))
-                       {
-                               if ($data['start'])
-                               {
-                                       $start=$data['start'];
-                               }
-                               else
-                               {
-                                       $start=0;
-                               }
-                               if ($data['filter'])
-                               {
-                                       $filter=$data['filter'];
-                               }
-                               else
-                               {
-                                       $filter='open';
-                               }
-               //              $filter = 
(isset($data['filter'])?$data['filter']:'open');
-                               $user_filter= 
(isset($data['user_filter'])?$data['user_filter']:'');
-                               $query = 
(isset($data['query'])?$data['query']:'');
-                               $sort = 
(isset($data['sort'])?$data['sort']:'DESC');
-                               $order = 
(isset($data['order'])?$data['order']:'');
-                               $cat_id = 
(isset($data['cat_id'])?$data['cat_id']:0);
-                               $district_id = 
(isset($data['district_id'])?$data['district_id']:0);
-                               $allrows = 
(isset($data['allrows'])?$data['allrows']:'');
-                               $start_date = 
(isset($data['start_date'])?$data['start_date']:'');
-                               $end_date = 
(isset($data['end_date'])?$data['end_date']:'');
-                       }
-
-
-/*                     $this->grants   = 
$GLOBALS['phpgw']->session->appsession('grants_ticket',$this->currentapp);
-
-                       if(!$this->grants)
-                       {
-                               $this->acl2             = 
CreateObject($this->currentapp.'.acl2','##DEFAULT##','##DEFAULT##');
-                               $this->grants           = 
$this->acl2->get_grants($this->currentapp,'.ticket');
-                               
$GLOBALS['phpgw']->session->appsession('grants_ticket',$this->currentapp,$this->grants);
-                       }
-*/
-                       if ($order)
-                       {
-                               $ordermethod = " order by $order $sort";
-                       }
-                       else
-                       {
-                               $ordermethod = ' order by fm_tts_tickets.id 
DESC';
-                       }
-
-                       $where= 'WHERE';
-
-/*                     $filtermethod = $where . ' ( fm_tts_tickets.user_id=' . 
$this->account;
-                       if (is_array($this->grants))
-                       {
-                               $grants = $this->grants;
-                               while (list($user) = each($grants))
-                               {
-                                       $public_user_list[] = $user;
-                               }
-                               reset($public_user_list);
-                               $filtermethod = " $where ( 
fm_tts_tickets.user_id IN(" . implode(',',$public_user_list) . "))";
-                       }
-
-                       $where= 'AND';
-
-*/
-                       if ($filter == 'closed'):
-                       {
-                               $filtermethod .= " $where 
fm_tts_tickets.status='X'";
-                               $where = 'AND';
-                       }
-                       elseif($filter == 'open'):
-                       {
-                               $filtermethod .= " $where 
fm_tts_tickets.status='O'";
-                               $where = 'AND';
-                       }
-                       endif;
-
-                       if ($cat_id > 0)
-                       {
-                               $filtermethod .= " $where cat_id='$cat_id' ";
-                               $where = 'AND';
-                       }
-
-                       if ($user_filter > 0)
-                       {
-                               $filtermethod .= " $where 
assignedto=$user_filter";
-                               $where = 'AND';
-                       }
-
-                       if ($district_id > 0)
-                       {
-                               $filtermethod .= " $where  
district_id='$district_id' ";
-                               $where = 'AND';
-                       }
-
-                       if ($start_date)
-                       {
-                               $filtermethod .= " $where 
fm_tts_tickets.entry_date >= $start_date AND fm_tts_tickets.entry_date <= 
$end_date ";
-                               $where= 'AND';
-                       }
-
-                       if($query)
-                       {
-                               $query = str_replace(",",'.',$query);
-                               if(stristr($query, '.'))
-                               {
-                                       $query=explode(".",$query);
-                                       $querymethod = " $where 
(fm_tts_tickets.loc1='" . $query[0] . "' AND fm_tts_tickets.loc4='" . $query[1] 
. "')";
-                               }
-                               else
-                               {
-                                       $query = ereg_replace("'",'',$query);
-                                       $query = ereg_replace('"','',$query);
-                                       $querymethod = " $where (subject 
$this->like '%$query%' or address $this->like '%$query%' or 
fm_tts_tickets.location_code $this->like '%$query%')";
-                               }
-                       }
-
-                       $sql = "SELECT fm_tts_tickets.*, fm_tts_category.descr 
as category,phpgw_accounts.account_lid as user_lid FROM fm_tts_tickets 
$this->join fm_tts_category on fm_tts_tickets.cat_id=fm_tts_category.id "
-                       . " $this->join phpgw_accounts on 
fm_tts_tickets.user_id=phpgw_accounts.account_id "
-                       . " $this->join fm_location1 on 
fm_tts_tickets.loc1=fm_location1.loc1 "
-                       . " $this->join fm_part_of_town on 
fm_location1.part_of_town_id=fm_part_of_town.part_of_town_id $filtermethod 
$querymethod";
-
-//echo $sql;
-                       $this->db2->query($sql,__LINE__,__FILE__);
-                       $this->total_records = $this->db2->num_rows();
-
-                       if(!$allrows)
-                       {
-                               $this->db->limit_query($sql . 
$ordermethod,$start,__LINE__,__FILE__);
-                       }
-                       else
-                       {
-                               $this->db->query($sql . 
$ordermethod,__LINE__,__FILE__);
-                       }
-
-                       $i = 0;
-
-                       while ($this->db->next_record())
-                       {
-
-                               $tickets[$i]['id']                              
= $this->db->f('id');
-                               $tickets[$i]['subject']                 = 
$this->db->f('subject');
-                               $tickets[$i]['location_code']   = 
$this->db->f('location_code');
-                               $tickets[$i]['user']                    = 
$this->db->f('user_lid');
-                               $tickets[$i]['address']                 = 
stripslashes($this->db->f('address'));
-                               $tickets[$i]['assignedto']              = 
$this->db->f('assignedto');
-                               $tickets[$i]['status']                  = 
$this->db->f('status');
-                               $tickets[$i]['priority']                = 
$this->db->f('priority');
-                               $tickets[$i]['category']                = 
$this->db->f('category');
-                               $tickets[$i]['subject']                 = 
$this->db->f('subject');
-                               $tickets[$i]['group_id']                = 
$this->db->f('group_id');
-                               $tickets[$i]['entry_date']              = 
$this->db->f('entry_date');
-                               $tickets[$i]['finnish_date']    = 
$this->db->f('finnish_date');
-                               $tickets[$i]['finnish_date2']   = 
$this->db->f('finnish_date2');
-
-                               $this->db2->query("select count(*) from 
fm_tts_views where id='" . $this->db->f('id')
-                                       . "' and account_id='" . 
$GLOBALS['phpgw_info']['user']['account_id'] . "'",__LINE__,__FILE__);
-                               $this->db2->next_record();
-
-                               if (!$this->db2->f(0))
-                               {
-                                       $tickets[$i]['new_ticket'] = True;
-                               }
-
-                               $i++;
-
-                       }
-                       return $tickets;
-               }
-
-               function get_origin_entity_type()
-               {
-                       $sql = "SELECT entity_id, id as cat_id,name"
-                       . " FROM fm_entity_category "
-                       . " WHERE tracking=1 ORDER by entity_id,cat_id";
-
-
-                       $this->db->query($sql,__LINE__,__FILE__);
-
-                       $i=0;
-                       while ($this->db->next_record())
-                       {
-                               
$entity[$i]['entity_id']=$this->db->f('entity_id');
-                               $entity[$i]['cat_id']=$this->db->f('cat_id');
-                               $entity[$i]['type']='entity';
-                               $uicols[]       = $this->db->f('name');
-                               $i++;
-                       }
-
-                       $entity[$i]['type']='project';
-                       $uicols[]       = lang('project');
-
-                       $this->uicols   = $uicols;
-                       return $entity;
-               }
-
-               function get_child_date($id,$type,$entity_id='',$cat_id='')
-               {
-                       $table= 'fm_origin';
-                       if($cat_id)
-                       {
-                               $and = " AND destination = 'entity_" . 
$entity_id . '_' . $cat_id . "'";
-                       }
-                       else
-                       {
-                               $and = " AND destination = '$type'";
-                       }
-
-                       $sql = "SELECT * FROM $table WHERE origin_id='$id' and 
origin ='tts' $and";
-
-                       $this->db->query($sql,__LINE__,__FILE__);
-
-                       $i=0;
-                       while ($this->db->next_record())
-                       {
-                               $date_info['date_info'][$i]['entry_date']= 
$GLOBALS['phpgw']->common->show_date($this->db->f('entry_date'),$this->dateformat);
-                               if($cat_id)
-                               {
-                                       
$date_info['date_info'][$i]['link']=$GLOBALS['phpgw']->link('/index.php','menuaction='.$this->currentapp.'.uientity.view&entity_id='.$entity_id.'&cat_id='
 . $cat_id . '&id=' . $this->db->f('destination_id'));
-                                       
$date_info['date_info'][$i]['descr']=$this->soadmin_entity->read_category_name($entity_id,$cat_id);
-                               }
-                               else
-                               {
-                                       
$date_info['date_info'][$i]['link']=$GLOBALS['phpgw']->link('/index.php','menuaction='.$this->currentapp.'.ui'
 . $type . '.view&id=' . $this->db->f('destination_id'));
-                                       
$date_info['date_info'][$i]['descr']=lang($type);
-                               }
-
-                               $i++;
-                       }
-//_debug_array($date_info);
-                       return $date_info;
-               }
-
-               function read_single($id)
-               {
-                       $sql = "SELECT * FROM fm_tts_tickets WHERE id=$id";
-
-                       $this->db->query($sql,__LINE__,__FILE__);
-
-                       if ($this->db->next_record())
-                       {
-                               $ticket['assignedto']           = 
$this->db->f('assignedto');
-                               $ticket['user_id']                      = 
$this->db->f('user_id');
-                               $ticket['group_id']                     = 
$this->db->f('group_id');
-                               $ticket['status']                       = 
$this->db->f('status');
-                               $ticket['cat_id']                       = 
$this->db->f('cat_id');
-                               $ticket['subject']                      = 
stripslashes($this->db->f('subject'));
-                               $ticket['priority']                     = 
$this->db->f('priority');
-                               $ticket['details']                      = 
stripslashes($this->db->f('details'));
-                               $ticket['location_code']        = 
$this->db->f('location_code');
-                               $ticket['contact_phone']        = 
$this->db->f('contact_phone');
-                               $ticket['address']                      = 
stripslashes($this->db->f('address'));
-                               $ticket['tenant_id']            = 
$this->db->f('tenant_id');
-                               $ticket['p_num']                        = 
$this->db->f('p_num');
-                               $ticket['p_entity_id']          = 
$this->db->f('p_entity_id');
-                               $ticket['p_cat_id']                     = 
$this->db->f('p_cat_id');
-                               $ticket['finnish_date']         = 
$this->db->f('finnish_date');
-                               $ticket['finnish_date2']        = 
$this->db->f('finnish_date2');
-
-                               $user_id=(int)$this->db->f('user_id');
-                               $this->db->query("SELECT 
account_firstname,account_lastname FROM phpgw_accounts WHERE 
account_id='$user_id' ");
-                               $this->db->next_record();
-
-                               $ticket['user_name']    = 
$this->db->f('account_firstname') . " " .$this->db->f('account_lastname') ;
-
-                       }
-
-
-                       $sql = "SELECT * FROM fm_origin WHERE origin = 'tts' 
AND origin_id='$id' ORDER by destination DESC  ";
-
-                       $this->db->query($sql,__LINE__,__FILE__);
-
-                       $i=-1;
-                       while ($this->db->next_record())
-                       {
-                               if($last_type != $this->db->f('destination'))
-                               {
-                                       $i++;
-                               }
-                               $ticket['origin'][$i]['type'] = 
$this->db->f('destination');
-                               $ticket['origin'][$i]['link'] = 
$this->bocommon->get_origin_link($this->db->f('destination'));
-                               $ticket['origin'][$i]['data'][]= array(
-                                       'id'=> $this->db->f('destination_id'),
-                                       'type'=> $this->db->f('destination')
-                                       );
-
-                               $last_type=$this->db->f('destination');
-                       }
-
-                       return $ticket;
-               }
-
-               function update_view($id='')
-               {
-                       // Have they viewed this ticket before ?
-                       $this->db->query("select count(*) from fm_tts_views 
where id='$id' "
-                                       . "and account_id='" . 
$GLOBALS['phpgw_info']['user']['account_id'] . "'",__LINE__,__FILE__);
-                       $this->db->next_record();
-
-                       if (! $this->db->f(0))
-                       {
-                               $this->db->query("insert into fm_tts_views 
(id,account_id,time) values ('$id','"
-                                       . 
$GLOBALS['phpgw_info']['user']['account_id'] . "','" . time() . 
"')",__LINE__,__FILE__);
-                       }
-               }
-
-               function add($ticket)
-               {
-                       while (is_array($ticket['location']) && 
list($input_name,$value) = each($ticket['location']))
-                       {
-                               if($value)
-                               {
-                                       $cols[] = $input_name;
-                                       $vals[] = $value;
-                               }
-                       }
-
-
-                       while (is_array($ticket['extra']) && 
list($input_name,$value) = each($ticket['extra']))
-                       {
-                               if($value)
-                               {
-                                       $cols[] = $input_name;
-                                       $vals[] = $value;
-                               }
-                       }
-
-                       if($cols)
-                       {
-                               $cols   = "," . implode(",", $cols);
-                               $vals   = ",'" . implode("','", $vals) . "'";
-                       }
-
-                       if($ticket['street_name'])
-                       {
-                               $address[]= $ticket['street_name'];
-                               $address[]= $ticket['street_number'];
-                               $address        = 
$this->db->db_addslashes(implode(" ", $address));
-                       }
-
-                       if(!$address)
-                       {
-                               $address = 
$this->db->db_addslashes($ticket['location_name']);
-                       }
-
-                       $values= array(
-                               $ticket['priority'],
-                               $GLOBALS['phpgw_info']['user']['account_id'],
-                               $ticket['assignedto'],
-                               $ticket['group_id'],
-                               $this->db->db_addslashes($ticket['subject']),
-                               $ticket['cat_id'],
-                               'O',
-                               $this->db->db_addslashes($ticket['details']),
-                               $ticket['location_code'],
-                               $address,
-                               time(),
-                               $ticket['finnish_date']);
-
-                       $values = $this->bocommon->validate_db_insert($values);
-                       $this->db->transaction_begin();
-
-                       $this->db->query("insert into fm_tts_tickets 
(priority,user_id,"
-                               . 
"assignedto,group_id,subject,cat_id,status,details,location_code,"
-                               . "address,entry_date,finnish_date $cols)"
-                               . "VALUES ($values $vals )",__LINE__,__FILE__);
-
-                       $id = 
$this->db->get_last_insert_id('fm_tts_tickets','id');
-                       if($ticket['extra']['contact_phone'] && 
$ticket['extra']['tenant_id'])
-                       {
-                               $this->db->query("update fm_tenant set 
contact_phone='". $ticket['extra']['contact_phone']. "' where id='". 
$ticket['extra']['tenant_id']. "'",__LINE__,__FILE__);
-                       }
-
-                       if($this->db->transaction_commit())
-                       {
-                               $this->historylog->add('O',$id,mktime(),'');
-                               if($ticket['finnish_date'])
-                               {
-                                       
$this->historylog->add('IF',$id,$ticket['finnish_date'],'');
-                               }
-                       }
-
-                       $receipt['message'][0]=array('msg'=>lang('Ticket %1 has 
been saved',$id));
-                       $receipt['id']  = $id;
-                       return $receipt;
-               }
-
-       }
-?>
+                       $this->join                     = $this->bocommon->join;
+                       $this->like                     = $this->bocommon->like;
+                       $this->soadmin_entity   = 
CreateObject($this->currentapp.'.soadmin_entity');
+                       $this->dateformat = 
$GLOBALS['phpgw_info']['user']['preferences']['common']['dateformat'];
+               }
+
+               function get_category_name($cat_id)
+               {
+                       $this->db->query("SELECT descr FROM fm_tts_category  
WHERE id='$cat_id' ");
+                       $this->db->next_record();
+                       return stripslashes($this->db->f('descr'));
+               }
+
+               function select_category_list()
+               {
+                       $this->db->query("SELECT id, descr FROM fm_tts_category 
 ORDER BY descr ");
+
+                       $i = 0;
+                       while ($this->db->next_record())
+                       {
+                               $categories[$i]['id']                           
= $this->db->f('id');
+                               $categories[$i]['name']                         
= stripslashes($this->db->f('descr'));
+                               $i++;
+                       }
+                       return $categories;
+               }
+
+
+               function read($data)
+               {
+
+                       if(is_array($data))
+                       {
+                               if ($data['start'])
+                               {
+                                       $start=$data['start'];
+                               }
+                               else
+                               {
+                                       $start=0;
+                               }
+                               if ($data['filter'])
+                               {
+                                       $filter=$data['filter'];
+                               }
+                               else
+                               {
+                                       $filter='open';
+                               }
+               //              $filter = 
(isset($data['filter'])?$data['filter']:'open');
+                               $user_filter= 
(isset($data['user_filter'])?$data['user_filter']:'');
+                               $query = 
(isset($data['query'])?$data['query']:'');
+                               $sort = 
(isset($data['sort'])?$data['sort']:'DESC');
+                               $order = 
(isset($data['order'])?$data['order']:'');
+                               $cat_id = 
(isset($data['cat_id'])?$data['cat_id']:0);
+                               $district_id = 
(isset($data['district_id'])?$data['district_id']:0);
+                               $allrows = 
(isset($data['allrows'])?$data['allrows']:'');
+                               $start_date = 
(isset($data['start_date'])?$data['start_date']:'');
+                               $end_date = 
(isset($data['end_date'])?$data['end_date']:'');
+                       }
+
+
+/*                     $this->grants   = 
$GLOBALS['phpgw']->session->appsession('grants_ticket',$this->currentapp);
+
+                       if(!$this->grants)
+                       {
+                               $this->acl2             = 
CreateObject($this->currentapp.'.acl2','##DEFAULT##','##DEFAULT##');
+                               $this->grants           = 
$this->acl2->get_grants($this->currentapp,'.ticket');
+                               
$GLOBALS['phpgw']->session->appsession('grants_ticket',$this->currentapp,$this->grants);
+                       }
+*/
+                       if ($order)
+                       {
+                               $ordermethod = " order by $order $sort";
+                       }
+                       else
+                       {
+                               $ordermethod = ' order by fm_tts_tickets.id 
DESC';
+                       }
+
+                       $where= 'WHERE';
+
+/*                     $filtermethod = $where . ' ( fm_tts_tickets.user_id=' . 
$this->account;
+                       if (is_array($this->grants))
+                       {
+                               $grants = $this->grants;
+                               while (list($user) = each($grants))
+                               {
+                                       $public_user_list[] = $user;
+                               }
+                               reset($public_user_list);
+                               $filtermethod = " $where ( 
fm_tts_tickets.user_id IN(" . implode(',',$public_user_list) . "))";
+                       }
+
+                       $where= 'AND';
+
+*/
+                       if ($filter == 'closed'):
+                       {
+                               $filtermethod .= " $where 
fm_tts_tickets.status='X'";
+                               $where = 'AND';
+                       }
+                       elseif($filter == 'open'):
+                       {
+                               $filtermethod .= " $where 
fm_tts_tickets.status='O'";
+                               $where = 'AND';
+                       }
+                       endif;
+
+                       if ($cat_id > 0)
+                       {
+                               $filtermethod .= " $where cat_id='$cat_id' ";
+                               $where = 'AND';
+                       }
+
+                       if ($user_filter > 0)
+                       {
+                               $filtermethod .= " $where 
assignedto=$user_filter";
+                               $where = 'AND';
+                       }
+
+                       if ($district_id > 0)
+                       {
+                               $filtermethod .= " $where  
district_id='$district_id' ";
+                               $where = 'AND';
+                       }
+
+                       if ($start_date)
+                       {
+                               $filtermethod .= " $where 
fm_tts_tickets.entry_date >= $start_date AND fm_tts_tickets.entry_date <= 
$end_date ";
+                               $where= 'AND';
+                       }
+
+                       if($query)
+                       {
+                               $query = str_replace(",",'.',$query);
+                               if(stristr($query, '.'))
+                               {
+                                       $query=explode(".",$query);
+                                       $querymethod = " $where 
(fm_tts_tickets.loc1='" . $query[0] . "' AND fm_tts_tickets.loc4='" . $query[1] 
. "')";
+                               }
+                               else
+                               {
+                                       $query = ereg_replace("'",'',$query);
+                                       $query = ereg_replace('"','',$query);
+                                       $querymethod = " $where (subject 
$this->like '%$query%' or address $this->like '%$query%' or 
fm_tts_tickets.location_code $this->like '%$query%')";
+                               }
+                       }
+
+                       $sql = "SELECT fm_tts_tickets.*, fm_tts_category.descr 
as category,phpgw_accounts.account_lid as user_lid FROM fm_tts_tickets 
$this->join fm_tts_category on fm_tts_tickets.cat_id=fm_tts_category.id "
+                       . " $this->join phpgw_accounts on 
fm_tts_tickets.user_id=phpgw_accounts.account_id "
+                       . " $this->join fm_location1 on 
fm_tts_tickets.loc1=fm_location1.loc1 "
+                       . " $this->join fm_part_of_town on 
fm_location1.part_of_town_id=fm_part_of_town.part_of_town_id $filtermethod 
$querymethod";
+
+//echo $sql;
+                       $this->db2->query($sql,__LINE__,__FILE__);
+                       $this->total_records = $this->db2->num_rows();
+
+                       if(!$allrows)
+                       {
+                               $this->db->limit_query($sql . 
$ordermethod,$start,__LINE__,__FILE__);
+                       }
+                       else
+                       {
+                               $this->db->query($sql . 
$ordermethod,__LINE__,__FILE__);
+                       }
+
+                       $i = 0;
+
+                       while ($this->db->next_record())
+                       {
+
+                               $tickets[$i]['id']                              
= $this->db->f('id');
+                               $tickets[$i]['subject']                 = 
$this->db->f('subject');
+                               $tickets[$i]['location_code']   = 
$this->db->f('location_code');
+                               $tickets[$i]['user']                    = 
$this->db->f('user_lid');
+                               $tickets[$i]['address']                 = 
stripslashes($this->db->f('address'));
+                               $tickets[$i]['assignedto']              = 
$this->db->f('assignedto');
+                               $tickets[$i]['status']                  = 
$this->db->f('status');
+                               $tickets[$i]['priority']                = 
$this->db->f('priority');
+                               $tickets[$i]['category']                = 
$this->db->f('category');
+                               $tickets[$i]['subject']                 = 
$this->db->f('subject');
+                               $tickets[$i]['group_id']                = 
$this->db->f('group_id');
+                               $tickets[$i]['entry_date']              = 
$this->db->f('entry_date');
+                               $tickets[$i]['finnish_date']    = 
$this->db->f('finnish_date');
+                               $tickets[$i]['finnish_date2']   = 
$this->db->f('finnish_date2');
+
+                               $this->db2->query("select count(*) from 
fm_tts_views where id='" . $this->db->f('id')
+                                       . "' and account_id='" . 
$GLOBALS['phpgw_info']['user']['account_id'] . "'",__LINE__,__FILE__);
+                               $this->db2->next_record();
+
+                               if (!$this->db2->f(0))
+                               {
+                                       $tickets[$i]['new_ticket'] = True;
+                               }
+
+                               $i++;
+
+                       }
+               return $tickets;
+               }
+
+               function get_origin_entity_type()
+               {
+                       $sql = "SELECT entity_id, id as cat_id,name"
+                       . " FROM fm_entity_category "
+                       . " WHERE tracking=1 ORDER by entity_id,cat_id";
+
+
+                       $this->db->query($sql,__LINE__,__FILE__);
+
+                       $i=0;
+                       while ($this->db->next_record())
+                       {
+                               
$entity[$i]['entity_id']=$this->db->f('entity_id');
+                               $entity[$i]['cat_id']=$this->db->f('cat_id');
+                               $entity[$i]['type']='entity';
+                               $uicols[]       = $this->db->f('name');
+                               $i++;
+                       }
+
+                       $entity[$i]['type']='project';
+                       $uicols[]       = lang('project');
+
+                       $this->uicols   = $uicols;
+                       return $entity;
+               }
+
+               function get_child_date($id,$type,$entity_id='',$cat_id='')
+               {
+                       $table= 'fm_origin';
+                       if($cat_id)
+                       {
+                               $and = " AND destination = 'entity_" . 
$entity_id . '_' . $cat_id . "'";
+                       }
+                       else
+                       {
+                               $and = " AND destination = '$type'";
+                       }
+
+                       $sql = "SELECT * FROM $table WHERE origin_id='$id' and 
origin ='tts' $and";
+
+                       $this->db->query($sql,__LINE__,__FILE__);
+
+                       $i=0;
+                       while ($this->db->next_record())
+                       {
+                               $date_info['date_info'][$i]['entry_date']= 
$GLOBALS['phpgw']->common->show_date($this->db->f('entry_date'),$this->dateformat);
+                               if($cat_id)
+                               {
+                                       
$date_info['date_info'][$i]['link']=$GLOBALS['phpgw']->link('/index.php','menuaction='.$this->currentapp.'.uientity.view&entity_id='.$entity_id.'&cat_id='
 . $cat_id . '&id=' . $this->db->f('destination_id'));
+                                       
$date_info['date_info'][$i]['descr']=$this->soadmin_entity->read_category_name($entity_id,$cat_id);
+                               }
+                               else
+                               {
+                                       
$date_info['date_info'][$i]['link']=$GLOBALS['phpgw']->link('/index.php','menuaction='.$this->currentapp.'.ui'
 . $type . '.view&id=' . $this->db->f('destination_id'));
+                                       
$date_info['date_info'][$i]['descr']=lang($type);
+                               }
+
+                               $i++;
+                       }
+//_debug_array($date_info);
+                       return $date_info;
+               }
+
+               function read_single($id)
+               {
+                       $sql = "SELECT * FROM fm_tts_tickets WHERE id=$id";
+
+                       $this->db->query($sql,__LINE__,__FILE__);
+
+                       if ($this->db->next_record())
+                       {
+                               $ticket['assignedto']           = 
$this->db->f('assignedto');
+                               $ticket['user_id']                      = 
$this->db->f('user_id');
+                               $ticket['group_id']                     = 
$this->db->f('group_id');
+                               $ticket['status']                       = 
$this->db->f('status');
+                               $ticket['cat_id']                       = 
$this->db->f('cat_id');
+                               $ticket['subject']                      = 
stripslashes($this->db->f('subject'));
+                               $ticket['priority']                     = 
$this->db->f('priority');
+                               $ticket['details']                      = 
stripslashes($this->db->f('details'));
+                               $ticket['location_code']        = 
$this->db->f('location_code');
+                               $ticket['contact_phone']        = 
$this->db->f('contact_phone');
+                               $ticket['address']                      = 
stripslashes($this->db->f('address'));
+                               $ticket['tenant_id']            = 
$this->db->f('tenant_id');
+                               $ticket['p_num']                        = 
$this->db->f('p_num');
+                               $ticket['p_entity_id']          = 
$this->db->f('p_entity_id');
+                               $ticket['p_cat_id']                     = 
$this->db->f('p_cat_id');
+                               $ticket['finnish_date']         = 
$this->db->f('finnish_date');
+                               $ticket['finnish_date2']        = 
$this->db->f('finnish_date2');
+
+                               $user_id=(int)$this->db->f('user_id');
+                               $this->db->query("SELECT 
account_firstname,account_lastname FROM phpgw_accounts WHERE 
account_id='$user_id' ");
+                               $this->db->next_record();
+
+                               $ticket['user_name']    = 
$this->db->f('account_firstname') . " " .$this->db->f('account_lastname') ;
+
+                       }
+
+
+                       $sql = "SELECT * FROM fm_origin WHERE origin = 'tts' 
AND origin_id='$id' ORDER by destination DESC  ";
+
+                       $this->db->query($sql,__LINE__,__FILE__);
+
+                       $i=-1;
+                       while ($this->db->next_record())
+                       {
+                               if($last_type != $this->db->f('destination'))
+                               {
+                                       $i++;
+                               }
+                               $ticket['origin'][$i]['type'] = 
$this->db->f('destination');
+                               $ticket['origin'][$i]['link'] = 
$this->bocommon->get_origin_link($this->db->f('destination'));
+                               $ticket['origin'][$i]['data'][]= array(
+                                       'id'=> $this->db->f('destination_id'),
+                                       'type'=> $this->db->f('destination')
+                                       );
+
+                               $last_type=$this->db->f('destination');
+                       }
+
+                       return $ticket;
+               }
+
+               function update_view($id='')
+               {
+                       // Have they viewed this ticket before ?
+                       $this->db->query("select count(*) from fm_tts_views 
where id='$id' "
+                                       . "and account_id='" . 
$GLOBALS['phpgw_info']['user']['account_id'] . "'",__LINE__,__FILE__);
+                       $this->db->next_record();
+
+                       if (! $this->db->f(0))
+                       {
+                               $this->db->query("insert into fm_tts_views 
(id,account_id,time) values ('$id','"
+                                       . 
$GLOBALS['phpgw_info']['user']['account_id'] . "','" . time() . 
"')",__LINE__,__FILE__);
+                       }
+               }
+
+               function add($ticket)
+               {
+                       while (is_array($ticket['location']) && 
list($input_name,$value) = each($ticket['location']))
+                       {
+                               if($value)
+                               {
+                                       $cols[] = $input_name;
+                                       $vals[] = $value;
+                               }
+                       }
+
+
+                       while (is_array($ticket['extra']) && 
list($input_name,$value) = each($ticket['extra']))
+                       {
+                               if($value)
+                               {
+                                       $cols[] = $input_name;
+                                       $vals[] = $value;
+                               }
+                       }
+
+                       if($cols)
+                       {
+                               $cols   = "," . implode(",", $cols);
+                               $vals   = ",'" . implode("','", $vals) . "'";
+                       }
+
+                       if($ticket['street_name'])
+                       {
+                               $address[]= $ticket['street_name'];
+                               $address[]= $ticket['street_number'];
+                               $address        = 
$this->db->db_addslashes(implode(" ", $address));
+                       }
+
+                       if(!$address)
+                       {
+                               $address = 
$this->db->db_addslashes($ticket['location_name']);
+                       }
+
+                       $values= array(
+                               $ticket['priority'],
+                               $GLOBALS['phpgw_info']['user']['account_id'],
+                               $ticket['assignedto'],
+                               $ticket['group_id'],
+                               $this->db->db_addslashes($ticket['subject']),
+                               $ticket['cat_id'],
+                               'O',
+                               $this->db->db_addslashes($ticket['details']),
+                               $ticket['location_code'],
+                               $address,
+                               time(),
+                               $ticket['finnish_date']);
+
+                       $values = $this->bocommon->validate_db_insert($values);
+                       $this->db->transaction_begin();
+
+                       $this->db->query("insert into fm_tts_tickets 
(priority,user_id,"
+                               . 
"assignedto,group_id,subject,cat_id,status,details,location_code,"
+                               . "address,entry_date,finnish_date $cols)"
+                               . "VALUES ($values $vals )",__LINE__,__FILE__);
+
+                       $id = 
$this->db->get_last_insert_id('fm_tts_tickets','id');
+                       if($ticket['extra']['contact_phone'] && 
$ticket['extra']['tenant_id'])
+                       {
+                               $this->db->query("update fm_tenant set 
contact_phone='". $ticket['extra']['contact_phone']. "' where id='". 
$ticket['extra']['tenant_id']. "'",__LINE__,__FILE__);
+                       }
+
+                       if($this->db->transaction_commit())
+                       {
+                               $this->historylog->add('O',$id,mktime(),'');
+                               if($ticket['finnish_date'])
+                               {
+                                       
$this->historylog->add('IF',$id,$ticket['finnish_date'],'');
+                               }
+                       }
+
+                       $receipt['message'][0]=array('msg'=>lang('Ticket %1 has 
been saved',$id));
+                       $receipt['id']  = $id;
+                       return $receipt;
+               }
+
+       }
+?>
Index: property/inc/class.sotts2.inc.php
diff -u property/inc/class.sotts2.inc.php:1.9 
property/inc/class.sotts2.inc.php:1.10
--- property/inc/class.sotts2.inc.php:1.9       Fri Jan 27 14:05:43 2006
+++ property/inc/class.sotts2.inc.php   Mon Jan 30 22:14:19 2006
@@ -1,279 +1,280 @@
-<?php
-       /**
-       * phpGroupWare - property: a Facilities Management System.
-       *
-       * @author Sigurd Nes <address@hidden>
-       * @copyright Copyright (C) 2003-2005 Free Software Foundation, Inc. 
http://www.fsf.org/
-       * @license http://www.gnu.org/licenses/gpl.html GNU General Public 
License
-       * @internal Development of this application was funded by 
http://www.bergen.kommune.no/bbb_/ekstern/
-       * @package property
-       * @subpackage helpdesk
-       * @version $Id: class.sotts2.inc.php,v 1.9 2006/01/27 14:05:43 sigurdne 
Exp $
-       */
-
-       /**
-        * Description
-        * @package property
-        */
-
-       class sotts2
-       {
-               function sotts2()
-               {
-                       $this->currentapp       = 
$GLOBALS['phpgw_info']['flags']['currentapp'];
-                       $this->bo               = 
CreateObject($this->currentapp.'.botts');
-                       $this->historylog       = 
CreateObject($this->currentapp.'.historylog','tts');
-                       $this->config           = 
CreateObject('phpgwapi.config');
-                       $this->bocommon         = 
CreateObject($this->currentapp.'.bocommon');
+<?php
+       /**
+       * phpGroupWare - property: a Facilities Management System.
+       *
+       * @author Sigurd Nes <address@hidden>
+       * @copyright Copyright (C) 2003-2005 Free Software Foundation, Inc. 
http://www.fsf.org/
+       * @license http://www.gnu.org/licenses/gpl.html GNU General Public 
License
+       * @internal Development of this application was funded by 
http://www.bergen.kommune.no/bbb_/ekstern/
+       * @package property
+       * @subpackage helpdesk
+       * @version $Id: class.sotts2.inc.php,v 1.10 2006/01/30 22:14:19 
sigurdne Exp $
+       */
+
+       /**
+        * Description
+        * @package property
+        */
+
+       class sotts2
+       {
+               function sotts2()
+               {
+                       $this->currentapp       = 
$GLOBALS['phpgw_info']['flags']['currentapp'];
+                       $this->bo               = 
CreateObject($this->currentapp.'.botts');
+                       $this->historylog       = 
CreateObject($this->currentapp.'.historylog','tts');
+                       $this->config           = 
CreateObject('phpgwapi.config');
+                       $this->bocommon         = 
CreateObject($this->currentapp.'.bocommon');
                        $this->db               = $this->bocommon->new_db();
-                       $this->db2              = $this->bocommon->new_db();    
                
-                       $this->join                     = $this->bocommon->join;
-                       $this->like                     = $this->bocommon->like;
-               }
-
-               function update_status($ticket,$id='')
-               {
-                       // DB Content is fresher than http posted value.
-                       $this->db->query("select * from fm_tts_tickets where 
id='$id'",__LINE__,__FILE__);
-                       $this->db->next_record();
-                       $old_status             = $this->db->f('status');
-
-                       $this->db->transaction_begin();
-
-                       /*
-                       ** phpgw_fm_tts_append.append_type - Defs
-                       ** R - Reopen ticket
-                       ** X - Ticket closed
-                       ** O - Ticket opened
-                       ** C - Comment appended
-                       ** A - Ticket assignment
-                       ** G - Ticket group assignment
-                       ** P - Priority change
-                       ** T - Category change
-                       ** S - Subject change
-                       ** B - Billing rate
-                       ** H - Billing hours
-                       ** F - finnish date
-                       */
-
-                       if ($old_status != $ticket['status'])
-                       {
-                               $fields_updated = True;
-                               if($old_status=='X')
-                               {
-                                       
$this->historylog->add('R',$id,$ticket['status'],$old_status);
-
-                                       $this->db->query("update fm_tts_tickets 
set status='O' where id='$id'",__LINE__,__FILE__);
-                               }
-                               else
-                               {
-                                       
$this->historylog->add($ticket['status'],$id,$ticket['status'],$old_status);
-
-                                       $this->db->query("update fm_tts_tickets 
set status='"
-                                       . $ticket['status'] . "' where 
id='$id'",__LINE__,__FILE__);
-                               }
-                       }
-
-                       $this->db->transaction_commit();
-
-                       if ($fields_updated)
-                       {
-                               $this->config->read_repository();
-
-                               if 
($this->config->config_data['mailnotification'])
-                               {
-                                       
$receipt=$this->bo->mail_ticket($id,$fields_updated,'',$location_code);
-
-                               }
-
-                               $receipt['message'][]= array('msg' => 
lang('Ticket %1 has been updated',$id));
-                       }
-
-                       return $receipt;
-
-               }
-
-               function update_ticket($ticket,$id='')
-               {
-                       // DB Content is fresher than http posted value.
-                       $this->db->query("select * from fm_tts_tickets where 
id='$id'",__LINE__,__FILE__);
-                       $this->db->next_record();
-
-
-                       $location_code  = $this->db->f('location_code');
-                       $oldfinnish_date        = $this->db->f('finnish_date');
-                       $oldfinnish_date2       = $this->db->f('finnish_date2');
-                       $oldassigned            = $this->db->f('assignedto');
-                       $oldgroup_id            = $this->db->f('group_id');
-                       $oldpriority            = $this->db->f('priority');
-                       $oldcat_id                      = 
$this->db->f('cat_id');
-                       $old_status             = $this->db->f('status');
-                       $old_billable_hours     = 
$this->db->f('billable_hours');
-                       $old_billable_rate      = $this->db->f('billable_rate');
-                       $old_subject            = $this->db->f('subject');
-                       if($oldcat_id ==0){$oldcat_id ='';}
-                       if($oldassigned ==0){$oldassigned ='';}
-                       if($oldgroup_id ==0){$oldgroup_id ='';}
-
-                       // Figure out and last note
-
-                       $history_values = 
$this->historylog->return_array(array(),array('C'),'history_timestamp','DESC',$id);
-                       $old_note = $history_values[0]['new_value'];
-
-                       if(!$old_note)
-                       {
-                               $old_note = $this->db->f('details');
-                       }
-
-
-                       $this->db->transaction_begin();
-
-                       /*
-                       ** phpgw_fm_tts_append.append_type - Defs
-                       ** R - Reopen ticket
-                       ** X - Ticket closed
-                       ** O - Ticket opened
-                       ** C - Comment appended
-                       ** A - Ticket assignment
-                       ** G - Ticket group assignment
-                       ** P - Priority change
-                       ** T - Category change
-                       ** S - Subject change
-                       ** B - Billing rate
-                       ** H - Billing hours
-                       ** F - finnish date
-                       */
-
-                       $finnish_date   = 
$this->bocommon->date_to_timestamp($ticket['finnish_date']);
-
-                       if ($oldfinnish_date && $ticket['finnish_date']):
-                       {
-                               $this->db->query("update fm_tts_tickets set 
finnish_date2='" . $finnish_date
-                                       . "' where id='$id'",__LINE__,__FILE__);
-                       }
-                       elseif(!$oldfinnish_date && $ticket['finnish_date'] ):
-                       {
-                               $this->db->query("update fm_tts_tickets set 
finnish_date='" . $finnish_date
-                                       . "' where id='$id'",__LINE__,__FILE__);
-                       }
-                       endif;
-
-                       if($oldfinnish_date2>0)
-                       {
-                               $oldfinnish_date = $oldfinnish_date2;
-                       }
-                       if($ticket['finnish_date'])
-                       {
-                               if ($oldfinnish_date != $finnish_date)
-                               {
-                                       $fields_updated = True;
-                                       
$this->historylog->add('F',$id,$finnish_date,$oldfinnish_date);
-                               }
-                       }
-
-                       if ($old_status != $ticket['status'])
-                       {
-                               $fields_updated = True;
-                               if($old_status=='X')
-                               {
-                                       
$this->historylog->add('R',$id,$ticket['status'],$old_status);
-
-                                       $this->db->query("update fm_tts_tickets 
set status='O' where id='$id'",__LINE__,__FILE__);
-                               }
-                               else
-                               {
-                                       
$this->historylog->add($ticket['status'],$id,$ticket['status'],$old_status);
-
-                                       $this->db->query("update fm_tts_tickets 
set status='"
-                                       . $ticket['status'] . "' where 
id='$id'",__LINE__,__FILE__);
-                               }
-                       }
-
-                       if ($oldassigned != $ticket['assignedto'])
-                       {
-                               $fields_updated = True;
-
-                               $value_set=array('assignedto'   => 
$ticket['assignedto']);
-                               $value_set      = 
$this->bocommon->validate_db_update($value_set);
-
-                               $this->db->query("update fm_tts_tickets set 
$value_set where id='$id'",__LINE__,__FILE__);
-                               
$this->historylog->add('A',$id,$ticket['assignedto'],$oldassigned);
-                       }
-
-                       if ($oldgroup_id != $ticket['group_id'])
-                       {
-                               $fields_updated = True;
-
-                               $value_set=array('group_id'     => 
$ticket['group_id']);
-                               $value_set      = 
$this->bocommon->validate_db_update($value_set);
-
-                               $this->db->query("update fm_tts_tickets set 
$value_set where id='$id'",__LINE__,__FILE__);
-                               
$this->historylog->add('G',$id,$ticket['group_id'],$oldgroup_id);
-                       }
-
-                       if ($oldpriority != $ticket['priority'])
-                       {
-                               $fields_updated = True;
-                               $this->db->query("update fm_tts_tickets set 
priority='" . $ticket['priority']
-                                       . "' where id='$id'",__LINE__,__FILE__);
-                               
$this->historylog->add('P',$id,$ticket['priority'],$oldpriority);
-                       }
-
-                       if ($oldcat_id != $ticket['cat_id'])
-                       {
-                               $fields_updated = True;
-                               $this->db->query("update fm_tts_tickets set 
cat_id='" . $ticket['cat_id']
-                                       . "' where id='$id'",__LINE__,__FILE__);
-                               
$this->historylog->add('T',$id,$ticket['cat_id'],$oldcat_id);
-                       }
-
-                       if ($old_billable_hours != $ticket['billable_hours'])
-                       {
-                               $fields_updated = True;
-                               $this->db->query("update fm_tts_tickets set 
billable_hours='" . $ticket['billable_hours']
-                                       . "' where id='$id'",__LINE__,__FILE__);
-                               
$this->historylog->add('H',$id,$ticket['billable_hours'],$old_billable_hours);
-                       }
-
-                       if ($old_billable_rate != $ticket['billable_rate'])
-                       {
-                               $fields_updated = True;
-                               $this->db->query("update fm_tts_tickets set 
billable_rate='" . $ticket['billable_rate']
-                                       . "' where id='$id'",__LINE__,__FILE__);
-                               
$this->historylog->add('B',$id,$ticket['billable_rate'],$old_billable_rate);
-                       }
-
-                       if ($old_subject != $ticket['subject'])
-                       {
-                               $this->db->query("update fm_tts_tickets set 
subject='" . $ticket['subject']
-                                       . "' where id='$id'",__LINE__,__FILE__);
-                               
$this->historylog->add('S',$id,$ticket['subject'],$old_subject);
-                               $receipt['message'][]= array('msg' => 
lang('Subject has been updated'));
-                       }
-
-                       if (($old_note != $ticket['note']) && $ticket['note'])
-                       {
-                               $fields_updated = True;
-                               
$this->historylog->add('C',$id,$this->db->db_addslashes($ticket['note']),$old_note);
-                       }
-
-                       $this->db->transaction_commit();
-
-                       if ($fields_updated)
-                       {
-                               $this->config->read_repository();
-
-                               if 
($this->config->config_data['mailnotification'] && $ticket['send_mail'])
-                               {
-                                       
$receipt=$this->bo->mail_ticket($id,$fields_updated,'',$location_code);
-
-                               }
-
-                               $receipt['message'][]= array('msg' => 
lang('Ticket has been updated'));
-                       }
-
-                       return $receipt;
-               }
-
-       }
-?>
+                       $this->db2              = $this->bocommon->new_db();
+
+                       $this->join                     = $this->bocommon->join;
+                       $this->like                     = $this->bocommon->like;
+               }
+
+               function update_status($ticket,$id='')
+               {
+                       // DB Content is fresher than http posted value.
+                       $this->db->query("select * from fm_tts_tickets where 
id='$id'",__LINE__,__FILE__);
+                       $this->db->next_record();
+                       $old_status             = $this->db->f('status');
+
+                       $this->db->transaction_begin();
+
+                       /*
+                       ** phpgw_fm_tts_append.append_type - Defs
+                       ** R - Reopen ticket
+                       ** X - Ticket closed
+                       ** O - Ticket opened
+                       ** C - Comment appended
+                       ** A - Ticket assignment
+                       ** G - Ticket group assignment
+                       ** P - Priority change
+                       ** T - Category change
+                       ** S - Subject change
+                       ** B - Billing rate
+                       ** H - Billing hours
+                       ** F - finnish date
+                       */
+
+                       if ($old_status != $ticket['status'])
+                       {
+                               $fields_updated = True;
+                               if($old_status=='X')
+                               {
+                                       
$this->historylog->add('R',$id,$ticket['status'],$old_status);
+
+                                       $this->db->query("update fm_tts_tickets 
set status='O' where id='$id'",__LINE__,__FILE__);
+                               }
+                               else
+                               {
+                                       
$this->historylog->add($ticket['status'],$id,$ticket['status'],$old_status);
+
+                                       $this->db->query("update fm_tts_tickets 
set status='"
+                                       . $ticket['status'] . "' where 
id='$id'",__LINE__,__FILE__);
+                               }
+                       }
+
+                       $this->db->transaction_commit();
+
+                       if ($fields_updated)
+                       {
+                               $this->config->read_repository();
+
+                               if 
($this->config->config_data['mailnotification'])
+                               {
+                                       
$receipt=$this->bo->mail_ticket($id,$fields_updated,'',$location_code);
+
+                               }
+
+                               $receipt['message'][]= array('msg' => 
lang('Ticket %1 has been updated',$id));
+                       }
+
+                       return $receipt;
+
+               }
+
+               function update_ticket($ticket,$id='')
+               {
+                       // DB Content is fresher than http posted value.
+                       $this->db->query("select * from fm_tts_tickets where 
id='$id'",__LINE__,__FILE__);
+                       $this->db->next_record();
+
+
+                       $location_code  = $this->db->f('location_code');
+                       $oldfinnish_date        = $this->db->f('finnish_date');
+                       $oldfinnish_date2       = $this->db->f('finnish_date2');
+                       $oldassigned            = $this->db->f('assignedto');
+                       $oldgroup_id            = $this->db->f('group_id');
+                       $oldpriority            = $this->db->f('priority');
+                       $oldcat_id                      = 
$this->db->f('cat_id');
+                       $old_status             = $this->db->f('status');
+                       $old_billable_hours     = 
$this->db->f('billable_hours');
+                       $old_billable_rate      = $this->db->f('billable_rate');
+                       $old_subject            = $this->db->f('subject');
+                       if($oldcat_id ==0){$oldcat_id ='';}
+                       if($oldassigned ==0){$oldassigned ='';}
+                       if($oldgroup_id ==0){$oldgroup_id ='';}
+
+                       // Figure out and last note
+
+                       $history_values = 
$this->historylog->return_array(array(),array('C'),'history_timestamp','DESC',$id);
+                       $old_note = $history_values[0]['new_value'];
+
+                       if(!$old_note)
+                       {
+                               $old_note = $this->db->f('details');
+                       }
+
+
+                       $this->db->transaction_begin();
+
+                       /*
+                       ** phpgw_fm_tts_append.append_type - Defs
+                       ** R - Reopen ticket
+                       ** X - Ticket closed
+                       ** O - Ticket opened
+                       ** C - Comment appended
+                       ** A - Ticket assignment
+                       ** G - Ticket group assignment
+                       ** P - Priority change
+                       ** T - Category change
+                       ** S - Subject change
+                       ** B - Billing rate
+                       ** H - Billing hours
+                       ** F - finnish date
+                       */
+
+                       $finnish_date   = 
$this->bocommon->date_to_timestamp($ticket['finnish_date']);
+
+                       if ($oldfinnish_date && $ticket['finnish_date']):
+                       {
+                               $this->db->query("update fm_tts_tickets set 
finnish_date2='" . $finnish_date
+                                       . "' where id='$id'",__LINE__,__FILE__);
+                       }
+                       elseif(!$oldfinnish_date && $ticket['finnish_date'] ):
+                       {
+                               $this->db->query("update fm_tts_tickets set 
finnish_date='" . $finnish_date
+                                       . "' where id='$id'",__LINE__,__FILE__);
+                       }
+                       endif;
+
+                       if($oldfinnish_date2>0)
+                       {
+                               $oldfinnish_date = $oldfinnish_date2;
+                       }
+                       if($ticket['finnish_date'])
+                       {
+                               if ($oldfinnish_date != $finnish_date)
+                               {
+                                       $fields_updated = True;
+                                       
$this->historylog->add('F',$id,$finnish_date,$oldfinnish_date);
+                               }
+                       }
+
+                       if ($old_status != $ticket['status'])
+                       {
+                               $fields_updated = True;
+                               if($old_status=='X')
+                               {
+                                       
$this->historylog->add('R',$id,$ticket['status'],$old_status);
+
+                                       $this->db->query("update fm_tts_tickets 
set status='O' where id='$id'",__LINE__,__FILE__);
+                               }
+                               else
+                               {
+                                       
$this->historylog->add($ticket['status'],$id,$ticket['status'],$old_status);
+
+                                       $this->db->query("update fm_tts_tickets 
set status='"
+                                       . $ticket['status'] . "' where 
id='$id'",__LINE__,__FILE__);
+                               }
+                       }
+
+                       if ($oldassigned != $ticket['assignedto'])
+                       {
+                               $fields_updated = True;
+
+                               $value_set=array('assignedto'   => 
$ticket['assignedto']);
+                               $value_set      = 
$this->bocommon->validate_db_update($value_set);
+
+                               $this->db->query("update fm_tts_tickets set 
$value_set where id='$id'",__LINE__,__FILE__);
+                               
$this->historylog->add('A',$id,$ticket['assignedto'],$oldassigned);
+                       }
+
+                       if ($oldgroup_id != $ticket['group_id'])
+                       {
+                               $fields_updated = True;
+
+                               $value_set=array('group_id'     => 
$ticket['group_id']);
+                               $value_set      = 
$this->bocommon->validate_db_update($value_set);
+
+                               $this->db->query("update fm_tts_tickets set 
$value_set where id='$id'",__LINE__,__FILE__);
+                               
$this->historylog->add('G',$id,$ticket['group_id'],$oldgroup_id);
+                       }
+
+                       if ($oldpriority != $ticket['priority'])
+                       {
+                               $fields_updated = True;
+                               $this->db->query("update fm_tts_tickets set 
priority='" . $ticket['priority']
+                                       . "' where id='$id'",__LINE__,__FILE__);
+                               
$this->historylog->add('P',$id,$ticket['priority'],$oldpriority);
+                       }
+
+                       if ($oldcat_id != $ticket['cat_id'])
+                       {
+                               $fields_updated = True;
+                               $this->db->query("update fm_tts_tickets set 
cat_id='" . $ticket['cat_id']
+                                       . "' where id='$id'",__LINE__,__FILE__);
+                               
$this->historylog->add('T',$id,$ticket['cat_id'],$oldcat_id);
+                       }
+
+                       if ($old_billable_hours != $ticket['billable_hours'])
+                       {
+                               $fields_updated = True;
+                               $this->db->query("update fm_tts_tickets set 
billable_hours='" . $ticket['billable_hours']
+                                       . "' where id='$id'",__LINE__,__FILE__);
+                               
$this->historylog->add('H',$id,$ticket['billable_hours'],$old_billable_hours);
+                       }
+
+                       if ($old_billable_rate != $ticket['billable_rate'])
+                       {
+                               $fields_updated = True;
+                               $this->db->query("update fm_tts_tickets set 
billable_rate='" . $ticket['billable_rate']
+                                       . "' where id='$id'",__LINE__,__FILE__);
+                               
$this->historylog->add('B',$id,$ticket['billable_rate'],$old_billable_rate);
+                       }
+
+                       if ($old_subject != $ticket['subject'])
+                       {
+                               $this->db->query("update fm_tts_tickets set 
subject='" . $ticket['subject']
+                                       . "' where id='$id'",__LINE__,__FILE__);
+                               
$this->historylog->add('S',$id,$ticket['subject'],$old_subject);
+                               $receipt['message'][]= array('msg' => 
lang('Subject has been updated'));
+                       }
+
+                       if (($old_note != $ticket['note']) && $ticket['note'])
+                       {
+                               $fields_updated = True;
+                               
$this->historylog->add('C',$id,$this->db->db_addslashes($ticket['note']),$old_note);
+                       }
+
+                       $this->db->transaction_commit();
+
+                       if ($fields_updated)
+                       {
+                               $this->config->read_repository();
+
+                               if 
($this->config->config_data['mailnotification'] && $ticket['send_mail'])
+                               {
+                                       
$receipt=$this->bo->mail_ticket($id,$fields_updated,'',$location_code);
+
+                               }
+
+                               $receipt['message'][]= array('msg' => 
lang('Ticket has been updated'));
+                       }
+
+                       return $receipt;
+               }
+
+       }
+?>
Index: property/inc/class.sowo_hour.inc.php
diff -u property/inc/class.sowo_hour.inc.php:1.10 
property/inc/class.sowo_hour.inc.php:1.11
--- property/inc/class.sowo_hour.inc.php:1.10   Fri Jan 27 14:05:43 2006
+++ property/inc/class.sowo_hour.inc.php        Mon Jan 30 22:14:19 2006
@@ -1,711 +1,711 @@
-<?php
-       /**
-       * phpGroupWare - property: a Facilities Management System.
-       *
-       * @author Sigurd Nes <address@hidden>
-       * @copyright Copyright (C) 2003-2005 Free Software Foundation, Inc. 
http://www.fsf.org/
-       * @license http://www.gnu.org/licenses/gpl.html GNU General Public 
License
-       * @internal Development of this application was funded by 
http://www.bergen.kommune.no/bbb_/ekstern/
-       * @package property
-       * @subpackage project
-       * @version $Id: class.sowo_hour.inc.php,v 1.10 2006/01/27 14:05:43 
sigurdne Exp $
-       */
-
-       /**
-        * Description
-        * @package property
-        */
-
-       class sowo_hour
-       {
-
-               function sowo_hour()
-               {
-                       $this->currentapp               = 
$GLOBALS['phpgw_info']['flags']['currentapp'];
-                       $this->account                  = 
$GLOBALS['phpgw_info']['user']['account_id'];
-                       $this->bocommon         = 
CreateObject($this->currentapp.'.bocommon');
+<?php
+       /**
+       * phpGroupWare - property: a Facilities Management System.
+       *
+       * @author Sigurd Nes <address@hidden>
+       * @copyright Copyright (C) 2003-2005 Free Software Foundation, Inc. 
http://www.fsf.org/
+       * @license http://www.gnu.org/licenses/gpl.html GNU General Public 
License
+       * @internal Development of this application was funded by 
http://www.bergen.kommune.no/bbb_/ekstern/
+       * @package property
+       * @subpackage project
+       * @version $Id: class.sowo_hour.inc.php,v 1.11 2006/01/30 22:14:19 
sigurdne Exp $
+       */
+
+       /**
+        * Description
+        * @package property
+        */
+
+       class sowo_hour
+       {
+               function sowo_hour()
+               {
+                       $this->currentapp               = 
$GLOBALS['phpgw_info']['flags']['currentapp'];
+                       $this->account                  = 
$GLOBALS['phpgw_info']['user']['account_id'];
+                       $this->bocommon         = 
CreateObject($this->currentapp.'.bocommon');
                        $this->db               = $this->bocommon->new_db();
-                       $this->db2              = $this->bocommon->new_db();
-                       $this->join                     = $this->bocommon->join;
-                       $this->left_join                = 
$this->bocommon->left_join;
-                       $this->like                     = $this->bocommon->like;
-               }
-
-               function get_chapter_list()
-               {
-                       $this->db->query("SELECT id, descr FROM fm_chapter 
ORDER BY id ");
-
-                       $i = 0;
-                       while ($this->db->next_record())
-                       {
-                               $chapter_entries[$i]['id']                      
        = $this->db->f('id');
-                               $chapter_entries[$i]['name']                    
        = stripslashes($this->db->f('descr'));
-                               $i++;
-                       }
-                       return $chapter_entries;
-               }
-
-               function get_grouping_list($workorder_id='')
-               {
-                       $this->db->query("SELECT grouping_id, grouping_descr 
FROM fm_wo_hours where workorder_id='$workorder_id' and grouping_id >0 group by 
grouping_id, grouping_descr");
-
-                       $i = 0;
-                       while ($this->db->next_record())
-                       {
-                               $grouping_entries[$i]['id']                     
        = $this->db->f('grouping_id');
-                               $grouping_entries[$i]['name']                   
        = stripslashes($this->db->f('grouping_descr'));
-                               $i++;
-                       }
-                       return $grouping_entries;
-               }
-
-               function get_building_part_list()
-               {
-                       $this->db->query("SELECT id, descr FROM 
fm_building_part ORDER BY id ");
-
-                       $i = 0;
-                       while ($this->db->next_record())
-                       {
-                               $building_part_entries[$i]['id']                
                = $this->db->f('id');
-                               $building_part_entries[$i]['name']              
                = stripslashes($this->db->f('descr'));
-                               $i++;
-                       }
-                       return $building_part_entries;
-               }
-
-
-               function select_branch_list()
-               {
-                       $this->db->query("SELECT id, descr FROM fm_branch ORDER 
BY id ");
-
-                       $i = 0;
-                       while ($this->db->next_record())
-                       {
-                               $branch_entries[$i]['id']                       
        = $this->db->f('id');
-                               $branch_entries[$i]['name']                     
        = stripslashes($this->db->f('descr'));
-                               $i++;
-                       }
-                       return $branch_entries;
-               }
-
-               function read($data)
-               {
-                       if(is_array($data))
-                       {
-                               $workorder_id = 
(isset($data['workorder_id'])?$data['workorder_id']:0);
-                       }
-
-
-                       $ordermethod = ' order by grouping_id, record , id asc 
';
-
-                       $sql = "SELECT fm_wo_hours.*, 
fm_wo_hours_category.descr as wo_hour_category"
-                               . " FROM fm_wo_hours $this->left_join 
fm_wo_hours_category on fm_wo_hours.category = fm_wo_hours_category.id WHERE 
workorder_id='$workorder_id' ";
-
-                       $this->db->query($sql . $ordermethod,__LINE__,__FILE__);
-                       $this->total_records = $this->db->num_rows();
-
-                       while ($this->db->next_record())
-                       {
-                               $hour_list[] = array
-                               (
-                                       'hour_id'                       => 
$this->db->f('id'),
-                                       'activity_num'          => 
$this->db->f('activity_num'),
-                                       'hours_descr'           => 
stripslashes($this->db->f('hours_descr')),
-                                       'owner'                         => 
$this->db->f('owner'),
-                                       'quantity'                      => 
$this->db->f('quantity'),
-                                       'grouping_id'           => 
$this->db->f('grouping_id'),
-                                       'grouping_descr'        => 
$this->db->f('grouping_descr'),
-                                       'ns3420_id'                     => 
$this->db->f('ns3420_id'),
-                                       'tolerance'                     => 
$this->db->f('tolerance'),
-                                       'activity_id'           => 
$this->db->f('activity_id'),
-                                       'unit'                          => 
$this->db->f('unit'),
-                                       'record'                        => 
$this->db->f('record'),
-                                       'cost'                          => 
$this->db->f('cost'),
-                                       'billperae'                     => 
$this->db->f('billperae'),
-                                       'remark'                        => 
$this->db->f('remark'),
-                                       'building_part'         => 
$this->db->f('building_part'),
-                                       'dim_d'                         => 
$this->db->f('dim_d'),
-                                       'wo_hour_category'      => 
$this->db->f('wo_hour_category')
-                                       );
-                       }
-
-                       for ($i=0; $i<count($hour_list); $i++)
-                       {
-                               $sql = "SELECT sum(amount) as deviation, 
count(amount) as count_deviation FROM fm_wo_h_deviation WHERE 
workorder_id=$workorder_id and hour_id=". $hour_list[$i]['hour_id'];
-                               $this->db->query($sql,__LINE__,__FILE__);
-                               $this->db->next_record();
-                               
$hour_list[$i]['deviation']=$this->db->f('deviation');
-                               
$hour_list[$i]['count_deviation']=$this->db->f('count_deviation');
-                       }
-
-                       return $hour_list;
-               }
-
-
-               function read_deviation($data)
-               {
-                       if(is_array($data))
-                       {
-                               $workorder_id = 
(isset($data['workorder_id'])?$data['workorder_id']:0);
-                               $hour_id = 
(isset($data['hour_id'])?$data['hour_id']:0);
-                       }
-
-                       $ordermethod = ' order by id asc';
-
-                       $sql = "SELECT *  FROM fm_wo_h_deviation WHERE 
workorder_id=$workorder_id AND hour_id=$hour_id ";
-
-
-                       $this->db->query($sql . $ordermethod,__LINE__,__FILE__);
-                       $this->total_records = $this->db->num_rows();
-
-                       while ($this->db->next_record())
-                       {
-                               $deviation[] = array
-                               (
-                                       'entry_date'            => 
$this->db->f('entry_date'),
-                                       'workorder_id'          => 
$workorder_id,
-                                       'hour_id'                       => 
$hour_id,
-                                       'id'                            => 
$this->db->f('id'),
-                                       'amount'                        => 
$this->db->f('amount'),
-                                       'descr'                         => 
stripslashes($this->db->f('descr'))
-                                       );
-                       }
-//_debug_array($deviation);
-                       return $deviation;
-               }
-
-
-               function read_single_deviation($data)
-               {
-                       if(is_array($data))
-                       {
-                               $workorder_id = 
(isset($data['workorder_id'])?$data['workorder_id']:0);
-                               $hour_id = 
(isset($data['hour_id'])?$data['hour_id']:0);
-                               $id = (isset($data['id'])?$data['id']:0);
-                       }
-
-                       $sql = "SELECT *  FROM fm_wo_h_deviation WHERE 
workorder_id=$workorder_id AND hour_id=$hour_id AND id = $id";
-                       $this->db->query($sql,__LINE__,__FILE__);
-                       $this->db->next_record();
-                       $deviation = array
-                       (
-                               'entry_date'            => 
$this->db->f('entry_date'),
-                               'workorder_id'          => $workorder_id,
-                               'hour_id'                       => $hour_id,
-                               'id'                            => 
$this->db->f('id'),
-                               'amount'                        => 
$this->db->f('amount'),
-                               'descr'                         => 
stripslashes($this->db->f('descr'))
-                       );
-                       return $deviation;
-               }
-
-               function add_deviation($values)
-               {
-                       $sql = "SELECT max(id) as current_id FROM 
fm_wo_h_deviation WHERE  workorder_id=" . $values['workorder_id'] . " AND 
hour_id=" . $values['hour_id'];
-                       $this->db->query($sql,__LINE__,__FILE__);
-                       $this->db->next_record();
-                       $id=$this->db->f('current_id')+1;
-
-                       $values['descr'] = 
$this->db->db_addslashes($values['descr']);
-
-                       $values_insert= array(
-                               $values['workorder_id'],
-                               $values['hour_id'],
-                               $id,
-                               $values['amount'],
-                               $values['descr'],
-                               time()
-                               );
-
-                       $values_insert  = 
$this->bocommon->validate_db_insert($values_insert);
-
-                       $this->db->query("INSERT INTO fm_wo_h_deviation 
(workorder_id,hour_id,id,amount,descr,entry_date) VALUES 
($values_insert)",__LINE__,__FILE__);
-
-                       $receipt['id']= $id;
-
-                       $receipt['message'][] = array('msg'=> lang('deviation 
has been added'));
-                       return $receipt;
-               }
-
-               function edit_deviation($values)
-               {
-                       $values['descr'] = 
$this->db->db_addslashes($values['descr']);
-
-                       $value_set=array(
-                               'amount'                        => 
$values['amount'],
-                               'descr'                 => 
$this->db->db_addslashes($values['descr'])
-                               );
-
-                       $value_set      = 
$this->bocommon->validate_db_update($value_set);
-
-                       $this->db->query("UPDATE fm_wo_h_deviation set 
$value_set WHERE workorder_id=" . $values['workorder_id'] . " AND hour_id=" . 
$values['hour_id'] . " AND id=" . $values['id'],__LINE__,__FILE__);
-
-                       $receipt['message'][] = array('msg'=> lang('deviation 
has been edited'));
-
-                       return $receipt;
-               }
-
-               function update_deviation($data)
-               {
-                       if(is_array($data))
-                       {
-                               $id = 
(isset($data['workorder_id'])?$data['workorder_id']:0);
-                               $deviation = 
(isset($data['sum_deviation'])?$data['sum_deviation']:0);
-                       }
-                       $this->db->query("UPDATE fm_workorder set deviation = 
$deviation WHERE id=$id",__LINE__,__FILE__);
-                       return $receipt;
-               }
-
-
-               function next_record($workorder_id)
-               {
-                       $this->db->query("SELECT  max(record) as record FROM 
fm_wo_hours where workorder_id='$workorder_id'",__LINE__,__FILE__);
-                       $this->db->next_record();
-                       $record = $this->db->f('record')+1;
-                       return $record;
-               }
-
-               function add_template($values,$workorder_id)
-               {
-
-                       $values['name'] = 
$this->db->db_addslashes($values['name']);
-                       $soworkorder    = 
CreateObject($this->currentapp.'.soworkorder');
-                       $workorder              = 
$soworkorder->read_single($workorder_id);
-
-                       $this->db->transaction_begin();
-
-                       $values_insert= array(
-                               $this->account,
-                               $values['name'],
-                               $values['descr'],
-                               $workorder['chapter_id'],
-                               time()
-                                );
-
-                       $values_insert  = 
$this->bocommon->validate_db_insert($values_insert);
-
-                       $this->db->query("insert into fm_template 
(owner,name,descr,chapter_id,entry_date) "
-                               . " values ($values_insert)",__LINE__,__FILE__);
-
-                       unset ($values_insert);
-
-                       $template_id = 
$this->db->get_last_insert_id('fm_template','id');
-
-                       $hour = $this->read(array('workorder_id' => 
$workorder_id));
-
-                       $record = $this->next_record($workorder_id);
-
-                       for ($i=0; $i<count($hour); $i++)
-                       {
-                               $values_insert= array(
-                                       $hour[$i]['activity_id'],
-                                       $hour[$i]['activity_num'],
-                                       $this->account,
-                                       $hour[$i]['hours_descr'],
-                                       $hour[$i]['unit'],
-                                       $hour[$i]['cost'],
-                                       $hour[$i]['quantity'],
-                                       $hour[$i]['billperae'],
-                                       $hour[$i]['ns3420_id'],
-                                       $hour[$i]['dim_d'],
-                                       $hour[$i]['grouping_id'],
-                                       $hour[$i]['grouping_descr'],
-                                       $hour[$i]['remark'],
-                                       $hour[$i]['tolerance'],
-                                       $hour[$i]['building_part'],
-                                       $record,
-                                       $template_id );
-
-                               $values_insert  = 
$this->bocommon->validate_db_insert($values_insert);
-
-                               $this->db->query("insert into fm_template_hours 
(activity_id,activity_num,owner,hours_descr,unit,"
-                               . 
"cost,quantity,billperae,ns3420_id,dim_d,grouping_id,grouping_descr,remark,tolerance,building_part,record,template_id)
 "
-                               . " values ($values_insert)",__LINE__,__FILE__);
-
-                               $record++;
-                       }
-
-                       $this->db->transaction_commit();
-
-                       $receipt['message'][] = array('msg'=>lang('template %1 
is added',$values['name']));
-                       return $receipt;
-               }
-
-
-               function add_hour($hour)
-               {
-                       $record = $this->next_record($hour[0]['workorder_id']);
-
-                       for ($i=0; $i<count($hour); $i++)
-                       {
-                               $values= array(
-                                       $hour[$i]['activity_id'],
-                                       $hour[$i]['activity_num'],
-                                       $this->account,
-                                       $hour[$i]['hours_descr'],
-                                       $hour[$i]['unit'],
-                                       $hour[$i]['cost'],
-                                       $hour[$i]['quantity'],
-                                       $hour[$i]['billperae'],
-                                       $hour[$i]['ns3420_id'],
-                                       $hour[$i]['dim_d'],
-                                       $record,
-                                       time(),
-                                       $hour[$i]['workorder_id'],
-                                       $hour[$i]['wo_hour_cat']);
-
-                               $values = 
$this->bocommon->validate_db_insert($values);
-
-                               $this->db->query("insert into fm_wo_hours 
(activity_id,activity_num,owner,hours_descr,unit,cost,quantity,billperae,ns3420_id,dim_d,record,entry_date,workorder_id,category)
 "
-                               . " values ($values)",__LINE__,__FILE__);
-
-                               $record++;
-                       }
-
-                       $receipt['message'][] = array('msg'=>lang('%1 entries 
is added!',count($hour)));
-
-                       $receipt['hour_id'] = 
$this->db->get_last_insert_id('fm_wo_hours','id');
-
-                       return $receipt;
-               }
-
-               function add_hour_from_template($hour,$workorder_id)
-               {
-
-                       $record = $this->next_record($workorder_id);
-
-                       if($hour[0]['chapter_id'])
-                       {
-                               $this->db->query("UPDATE fm_workorder set
-                                       chapter_id      ='" . 
$hour[0]['chapter_id'] . "' WHERE id= '$workorder_id'",__LINE__,__FILE__);
-                       }
-
-                       for ($i=0; $i<count($hour); $i++)
-                       {
-
-                               if($hour[$i]['new_grouping'])
-                               {
-                                       $this->db->query("SELECT grouping_id 
FROM fm_wo_hours where grouping_descr ='" .$hour[$i]['new_grouping'] . "' and 
workorder_id= '$workorder_id'",__LINE__,__FILE__);
-                                       $this->db->next_record();
-                                       if ( $this->db->f('grouping_id'))
-                                       {
-                                               $grouping_id    = 
$this->db->f('grouping_id');
-                                       }
-                                       else
-                                       {
-                                               $this->db->query("SELECT 
max(grouping_id) as grouping_id FROM fm_wo_hours where workorder_id= 
'$workorder_id'",__LINE__,__FILE__);
-                                               $this->db->next_record();
-                                               $grouping_id    = 
$this->db->f('grouping_id')+1;
-                                       }
-
-                                       $grouping_descr = 
$hour[$i]['new_grouping'];
-                               }
-
-                               $values= array(
-                                       $this->account,
-                                       $hour[$i]['activity_id'],
-                                       $hour[$i]['activity_num'],
-                                       $hour[$i]['hours_descr'],
-                                       $hour[$i]['unit'],
-                                       $hour[$i]['cost'],
-                                       $hour[$i]['quantity'],
-                                       $hour[$i]['billperae'],
-                                       $hour[$i]['ns3420_id'],
-                                       $hour[$i]['dim_d'],
-                                       $grouping_id,
-                                       $grouping_descr,
-                                       $record,
-                                       $hour[$i]['building_part_id'],
-                                       $hour[$i]['tolerance_id'],
-                                       $hour[$i]['remark'],
-                                       time(),
-                                       $workorder_id );
-
-                               $values = 
$this->bocommon->validate_db_insert($values);
-
-                               $this->db->query("insert into fm_wo_hours 
(owner,activity_id,activity_num,hours_descr,unit,cost,quantity,billperae,ns3420_id,dim_d,"
-                               . " 
grouping_id,grouping_descr,record,building_part,tolerance,remark,entry_date,workorder_id)
 "
-                               . " values ($values     )",__LINE__,__FILE__);
-
-                               $record++;
-                       }
-
-                       $receipt['message'][] = array('msg'=>lang('the number 
of %1 hour is added!',$i));
-
-                       return $receipt;
-               }
-
-               function add_custom_hour($hour,$workorder_id)
-               {
-
-                       $hour['record'] = $this->next_record($workorder_id);
-
-                       if($hour['chapter_id'])
-                       {
-                               $this->db->query("UPDATE fm_workorder set
-                               chapter_id      ='" . $hour['chapter_id'] . "' 
WHERE id= '$workorder_id'",__LINE__,__FILE__);
-                       }
-
-                       if($hour['grouping_id'])
-                       {
-                               $this->db->query("SELECT grouping_descr , 
max(record) as record FROM fm_wo_hours where grouping_id='" 
.$hour['grouping_id'] . "' and workorder_id= '$workorder_id' GROUP by 
grouping_descr",__LINE__,__FILE__);
-                               $this->db->next_record();
-                               $hour['grouping_descr'] = 
$this->db->f('grouping_descr');
-//                             $hour['record'] = $this->db->f('record')+1;
-                       }
-
-                       if($hour['new_grouping'])
-                       {
-                               $this->db->query("SELECT grouping_id FROM 
fm_wo_hours where grouping_descr ='" .$hour['new_grouping'] . "' and 
workorder_id= '$workorder_id'",__LINE__,__FILE__);
-                               $this->db->next_record();
-                               if ( $this->db->f('grouping_id'))
-                               {
-                                       $hour['grouping_id']    = 
$this->db->f('grouping_id');
-                               }
-                               else
-                               {
-                                       $this->db->query("SELECT 
max(grouping_id) as grouping_id FROM fm_wo_hours where workorder_id= 
'$workorder_id'",__LINE__,__FILE__);
-                                       $this->db->next_record();
-                                       $hour['grouping_id']    = 
$this->db->f('grouping_id')+1;
-//                                     $hour['record'] = 1;
-                               }
-
-                               $hour['grouping_descr'] = $hour['new_grouping'];
-                       }
-
-                       $values= array(
-                                       $this->account,
-                                       $hour['descr'],
-                                       $hour['unit'],
-                                       $hour['cost'],
-                                       $hour['quantity'],
-                                       $hour['billperae'],
-                                       $hour['ns3420_id'],
-                                       $hour['dim_d'],
-                                       $hour['grouping_id'],
-                                       $hour['grouping_descr'],
-                                       $hour['record'],
-                                       $hour['building_part_id'],
-                                       $hour['tolerance_id'],
-                                       $hour['remark'],
-                                       time(),
-                                       $workorder_id,
-                                       $hour['wo_hour_cat']
-                                       );
-
-                       $values = $this->bocommon->validate_db_insert($values);
-
-                       $this->db->query("insert into fm_wo_hours 
(owner,hours_descr,unit,cost,quantity,billperae,ns3420_id,dim_d,"
-                               . " 
grouping_id,grouping_descr,record,building_part,tolerance,remark,entry_date,workorder_id,category)
 "
-                               . "VALUES ( $values )",__LINE__,__FILE__);
-
-                       $receipt['hour_id'] = 
$this->db->get_last_insert_id('fm_wo_hours','id');
-
-                       $receipt['message'][] = array('msg'=>lang('hour %1 is 
added!',$receipt['hour_id']));
-
-                       return $receipt;
-
-               }
-
-               function read_single_hour($hour_id)
-               {
-                       $sql = "SELECT * from fm_wo_hours where id='$hour_id'";
-
-                       $this->db->query($sql,__LINE__,__FILE__);
-
-                       if ($this->db->next_record())
-                       {
-                               $hour['hour_id']                                
= $this->db->f('id');
-                               $hour['record']                                 
= $this->db->f('record');
-                               $hour['activity_id']                    = 
$this->db->f('activity_id');
-                               $hour['activity_num']                   = 
$this->db->f('activity_num');
-                               $hour['grouping_id']                    = 
$this->db->f('grouping_id');
-                               $hour['hours_descr']                    = 
$this->db->f('hours_descr');
-                               $hour['remark']                                 
= $this->db->f('remark');
-                               $hour['billperae']                              
= $this->db->f('billperae');
-                               $hour['unit']                                   
= $this->db->f('unit');
-                               $hour['ns3420_id']                              
= $this->db->f('ns3420_id');
-                               $hour['tolerance_id']                   = 
(int)$this->db->f('tolerance');
-                               $hour['building_part_id']               = 
(int)$this->db->f('building_part');
-                               $hour['quantity']                               
= $this->db->f('quantity');
-                               $hour['cost']                                   
= $this->db->f('cost');
-                               $hour['dim_d']                                  
= $this->db->f('dim_d');
-                               $hour['wo_hour_cat']                            
= $this->db->f('category');
-                       }
-
-                       return $hour;
-               }
-
-               function edit($hour,$workorder_id)
-               {
-
-                       $hour['descr'] = 
$this->db->db_addslashes($hour['descr']);
-                       $hour['remark'] = 
$this->db->db_addslashes($hour['remark']);
-//_debug_array($hour);
-
-
-                       if($hour['chapter_id'])
-                       {
-                               $this->db->query("UPDATE fm_workorder set
-                               chapter_id      ='" . $hour['chapter_id'] . "' 
WHERE id= '$workorder_id'",__LINE__,__FILE__);
-                       }
-
-                       if($hour['new_grouping'])
-                       {
-                               $this->db->query("SELECT grouping_id FROM 
fm_wo_hours where grouping_descr ='" .$hour['new_grouping'] . "' and 
workorder_id= '$workorder_id'",__LINE__,__FILE__);
-                               $this->db->next_record();
-                               if ( $this->db->f('grouping_id'))
-                               {
-                                       $hour['grouping_id']    = 
$this->db->f('grouping_id');
-                               }
-                               else
-                               {
-
-                                       $this->db->query("UPDATE fm_wo_hours 
set grouping_id = NULL WHERE id ='" .$hour['hour_id'] . "'",__LINE__,__FILE__);
-                                       $this->db->query("SELECT 
count(grouping_id) as num_grouping FROM fm_wo_hours where workorder_id= 
'$workorder_id' and grouping_id >0 ",__LINE__,__FILE__);
-                                       $this->db->next_record();
-                                       if ($this->db->f('num_grouping')==1)
-                                       {
-                                               $hour['grouping_id']=1;
-                                       }
-                                       else
-                                       {
-                                               $this->db->query("SELECT 
max(grouping_id) as grouping_id FROM fm_wo_hours where workorder_id= 
'$workorder_id'",__LINE__,__FILE__);
-                                               $this->db->next_record();
-                                               $hour['grouping_id']    = 
$this->db->f('grouping_id')+1;
-                                       }
-                               }
-                               $hour['grouping_descr'] = $hour['new_grouping'];
-                       }
-                       else
-                       {
-                               $this->db->query("SELECT 
grouping_id,grouping_descr FROM fm_wo_hours where id ='" .$hour['hour_id'] . 
"'",__LINE__,__FILE__);
-                               $this->db->next_record();
-                               $old_grouping_id        = 
$this->db->f('grouping_id');
-
-                               if ( $old_grouping_id == $hour['grouping_id'])
-                               {
-
-                                       $hour['grouping_descr'] = 
$this->db->f('grouping_descr');
-                               }
-                               else
-                               {
-                                       $this->db->query("SELECT grouping_descr 
, max(record) as record FROM fm_wo_hours where grouping_id='" 
.$hour['grouping_id'] . "' and workorder_id= '$workorder_id' GROUP by 
grouping_descr",__LINE__,__FILE__);
-                                       $this->db->next_record();
-                                       if($this->db->f('grouping_descr'))
-                                       {
-                                               $hour['grouping_descr'] = 
$this->db->f('grouping_descr');
-                                       }
-                                       else
-                                       {
-                                               $hour['grouping_id']='';
-                                               $hour['grouping_descr']='';
-                                       }
-
-/*                                     if($old_record>1)
-                                       {
-                                               $this->db->query("UPDATE 
fm_wo_hours set
-                                                       record  = record - 1 
where grouping_id='" .$hour['grouping_id'] . "' and workorder_id= 
'$workorder_id' and record > $old_record ",__LINE__,__FILE__);
-
-                                       }
-
-*/
-                               }
-                       }
-
-                       $value_set=array(
-                               'hours_descr'           => $hour['descr'],
-                               'remark'                        => 
$hour['remark'],
-                               'billperae'                     => 
$hour['billperae'],
-                               'unit'                          => 
$hour['unit'],
-                               'quantity'                      => 
$hour['quantity'],
-                               'cost'                          => 
$hour['cost'],
-                               'ns3420_id'                     => 
$hour['ns3420_id'],
-                               'tolerance'                     => 
$hour['tolerance_id'],
-                               'building_part'         => 
$hour['building_part_id'],
-                               'dim_d'                         => 
$hour['dim_d'],
-                               'grouping_id'           => $hour['grouping_id'],
-                               'grouping_descr'        => 
$hour['grouping_descr'],
-                               'category'              => $hour['wo_hour_cat']
-                               );
-
-                       $value_set      = 
$this->bocommon->validate_db_update($value_set);
-
-                       $this->db->query("UPDATE fm_wo_hours set $value_set 
WHERE id= '" . $hour['hour_id'] ."'",__LINE__,__FILE__);
-
-                       $receipt['hour_id'] = $hour['hour_id'];
-                       $receipt['message'][] = array('msg'=>lang('hour %1 has 
been edited',$hour['hour_id']));
-                       return $receipt;
-
-               }
-
-               function update_email($to_email,$workorder_id)
-               {
-                       $this->db->query("SELECT vendor_id FROM fm_workorder 
where id ='$workorder_id'",__LINE__,__FILE__);
-                       $this->db->next_record();
-                       $vendor_id      = $this->db->f('vendor_id');
-
-                       $this->db->query("UPDATE fm_vendor set email 
='$to_email' where  id= '$vendor_id'",__LINE__,__FILE__);
-               }
-
-               function get_email($vendor_id)
-               {
-                       $vendor_id=(int)$vendor_id;
-
-                       $this->db->query("select column_name from 
fm_vendor_attribute WHERE datatype='email'",__LINE__,__FILE__);
-                       while ($this->db->next_record())
-                       {
-                               $this->db2->query("select " . 
$this->db->f('column_name') . " from fm_vendor where 
id=$vendor_id",__LINE__,__FILE__);
-                               while ($this->db2->next_record())
-                               {
-                                       
if($this->db2->f($this->db->f('column_name')))
-                                       {
-                                               $email_list[] = array('email' 
=> $this->db2->f($this->db->f('column_name')));
-                                       }
-                               }
-                       }
-
-                       return  $email_list;
-               }
-
-               function delete($hour_id,$workorder_id )
-               {
-                       $this->db->query("SELECT record FROM fm_wo_hours where 
id ='$hour_id'",__LINE__,__FILE__);
-                       $this->db->next_record();
-                       $old_record     = $this->db->f('record');
-
-                       $this->db->transaction_begin();
-
-                       $this->db->query("DELETE FROM fm_wo_hours WHERE id='" . 
$hour_id . "'",__LINE__,__FILE__);
-                       $this->db->query("DELETE FROM fm_wo_h_deviation WHERE 
workorder_id=$workorder_id AND hour_id=$hour_id",__LINE__,__FILE__);
-                       if($old_record)
-                       {
-                               $this->db->query("UPDATE fm_wo_hours set record 
= record - 1 where  workorder_id= '$workorder_id' and record > $old_record 
",__LINE__,__FILE__);
-                       }
-
-                       $this->db->transaction_commit();
-
-                       $receipt['message'][] = array('msg'=>lang('hour %1 has 
been deleted',$hour_id));
-                       return $receipt;
-
-               }
-
-               function delete_deviation($workorder_id,$hour_id,$id )
-               {
-                       $this->db->query("DELETE FROM fm_wo_h_deviation WHERE 
workorder_id=$workorder_id AND hour_id=$hour_id AND id=$id",__LINE__,__FILE__);
-                       $receipt['message'][] = array('msg'=>lang('deviation %1 
has been deleted',$id));
-                       return $receipt;
-               }
-
-               function update_calculation($data)
-               {
-                       if(is_array($data))
-                       {
-                               $id = 
(isset($data['workorder_id'])?$data['workorder_id']:0);
-                               $calculation = 
(isset($data['calculation'])?$data['calculation']:0);
-                       }
-                       $this->db->query("UPDATE fm_workorder set calculation = 
$calculation WHERE id=$id",__LINE__,__FILE__);
-                       return $receipt;
-               }
-       }
-?>
+                       $this->db2              = $this->bocommon->new_db();
+
+                       $this->join                     = $this->bocommon->join;
+                       $this->left_join                = 
$this->bocommon->left_join;
+                       $this->like                     = $this->bocommon->like;
+               }
+
+               function get_chapter_list()
+               {
+                       $this->db->query("SELECT id, descr FROM fm_chapter 
ORDER BY id ");
+
+                       $i = 0;
+                       while ($this->db->next_record())
+                       {
+                               $chapter_entries[$i]['id']                      
        = $this->db->f('id');
+                               $chapter_entries[$i]['name']                    
        = stripslashes($this->db->f('descr'));
+                               $i++;
+                       }
+                       return $chapter_entries;
+               }
+
+               function get_grouping_list($workorder_id='')
+               {
+                       $this->db->query("SELECT grouping_id, grouping_descr 
FROM fm_wo_hours where workorder_id='$workorder_id' and grouping_id >0 group by 
grouping_id, grouping_descr");
+
+                       $i = 0;
+                       while ($this->db->next_record())
+                       {
+                               $grouping_entries[$i]['id']                     
        = $this->db->f('grouping_id');
+                               $grouping_entries[$i]['name']                   
        = stripslashes($this->db->f('grouping_descr'));
+                               $i++;
+                       }
+                       return $grouping_entries;
+               }
+
+               function get_building_part_list()
+               {
+                       $this->db->query("SELECT id, descr FROM 
fm_building_part ORDER BY id ");
+
+                       $i = 0;
+                       while ($this->db->next_record())
+                       {
+                               $building_part_entries[$i]['id']                
                = $this->db->f('id');
+                               $building_part_entries[$i]['name']              
                = stripslashes($this->db->f('descr'));
+                               $i++;
+                       }
+                       return $building_part_entries;
+               }
+
+
+               function select_branch_list()
+               {
+                       $this->db->query("SELECT id, descr FROM fm_branch ORDER 
BY id ");
+
+                       $i = 0;
+                       while ($this->db->next_record())
+                       {
+                               $branch_entries[$i]['id']                       
        = $this->db->f('id');
+                               $branch_entries[$i]['name']                     
        = stripslashes($this->db->f('descr'));
+                               $i++;
+                       }
+                       return $branch_entries;
+               }
+
+               function read($data)
+               {
+                       if(is_array($data))
+                       {
+                               $workorder_id = 
(isset($data['workorder_id'])?$data['workorder_id']:0);
+                       }
+
+
+                       $ordermethod = ' order by grouping_id, record , id asc 
';
+
+                       $sql = "SELECT fm_wo_hours.*, 
fm_wo_hours_category.descr as wo_hour_category"
+                               . " FROM fm_wo_hours $this->left_join 
fm_wo_hours_category on fm_wo_hours.category = fm_wo_hours_category.id WHERE 
workorder_id='$workorder_id' ";
+
+                       $this->db->query($sql . $ordermethod,__LINE__,__FILE__);
+                       $this->total_records = $this->db->num_rows();
+
+                       while ($this->db->next_record())
+                       {
+                               $hour_list[] = array
+                               (
+                                       'hour_id'                       => 
$this->db->f('id'),
+                                       'activity_num'          => 
$this->db->f('activity_num'),
+                                       'hours_descr'           => 
stripslashes($this->db->f('hours_descr')),
+                                       'owner'                         => 
$this->db->f('owner'),
+                                       'quantity'                      => 
$this->db->f('quantity'),
+                                       'grouping_id'           => 
$this->db->f('grouping_id'),
+                                       'grouping_descr'        => 
$this->db->f('grouping_descr'),
+                                       'ns3420_id'                     => 
$this->db->f('ns3420_id'),
+                                       'tolerance'                     => 
$this->db->f('tolerance'),
+                                       'activity_id'           => 
$this->db->f('activity_id'),
+                                       'unit'                          => 
$this->db->f('unit'),
+                                       'record'                        => 
$this->db->f('record'),
+                                       'cost'                          => 
$this->db->f('cost'),
+                                       'billperae'                     => 
$this->db->f('billperae'),
+                                       'remark'                        => 
$this->db->f('remark'),
+                                       'building_part'         => 
$this->db->f('building_part'),
+                                       'dim_d'                         => 
$this->db->f('dim_d'),
+                                       'wo_hour_category'      => 
$this->db->f('wo_hour_category')
+                                       );
+                       }
+
+                       for ($i=0; $i<count($hour_list); $i++)
+                       {
+                               $sql = "SELECT sum(amount) as deviation, 
count(amount) as count_deviation FROM fm_wo_h_deviation WHERE 
workorder_id=$workorder_id and hour_id=". $hour_list[$i]['hour_id'];
+                               $this->db->query($sql,__LINE__,__FILE__);
+                               $this->db->next_record();
+                               
$hour_list[$i]['deviation']=$this->db->f('deviation');
+                               
$hour_list[$i]['count_deviation']=$this->db->f('count_deviation');
+                       }
+
+                       return $hour_list;
+               }
+
+
+               function read_deviation($data)
+               {
+                       if(is_array($data))
+                       {
+                               $workorder_id = 
(isset($data['workorder_id'])?$data['workorder_id']:0);
+                               $hour_id = 
(isset($data['hour_id'])?$data['hour_id']:0);
+                       }
+
+                       $ordermethod = ' order by id asc';
+
+                       $sql = "SELECT *  FROM fm_wo_h_deviation WHERE 
workorder_id=$workorder_id AND hour_id=$hour_id ";
+
+
+                       $this->db->query($sql . $ordermethod,__LINE__,__FILE__);
+                       $this->total_records = $this->db->num_rows();
+
+                       while ($this->db->next_record())
+                       {
+                               $deviation[] = array
+                               (
+                                       'entry_date'            => 
$this->db->f('entry_date'),
+                                       'workorder_id'          => 
$workorder_id,
+                                       'hour_id'                       => 
$hour_id,
+                                       'id'                            => 
$this->db->f('id'),
+                                       'amount'                        => 
$this->db->f('amount'),
+                                       'descr'                         => 
stripslashes($this->db->f('descr'))
+                                       );
+                       }
+//_debug_array($deviation);
+                       return $deviation;
+               }
+
+
+               function read_single_deviation($data)
+               {
+                       if(is_array($data))
+                       {
+                               $workorder_id = 
(isset($data['workorder_id'])?$data['workorder_id']:0);
+                               $hour_id = 
(isset($data['hour_id'])?$data['hour_id']:0);
+                               $id = (isset($data['id'])?$data['id']:0);
+                       }
+
+                       $sql = "SELECT *  FROM fm_wo_h_deviation WHERE 
workorder_id=$workorder_id AND hour_id=$hour_id AND id = $id";
+                       $this->db->query($sql,__LINE__,__FILE__);
+                       $this->db->next_record();
+                       $deviation = array
+                       (
+                               'entry_date'            => 
$this->db->f('entry_date'),
+                               'workorder_id'          => $workorder_id,
+                               'hour_id'                       => $hour_id,
+                               'id'                            => 
$this->db->f('id'),
+                               'amount'                        => 
$this->db->f('amount'),
+                               'descr'                         => 
stripslashes($this->db->f('descr'))
+                       );
+                       return $deviation;
+               }
+
+               function add_deviation($values)
+               {
+                       $sql = "SELECT max(id) as current_id FROM 
fm_wo_h_deviation WHERE  workorder_id=" . $values['workorder_id'] . " AND 
hour_id=" . $values['hour_id'];
+                       $this->db->query($sql,__LINE__,__FILE__);
+                       $this->db->next_record();
+                       $id=$this->db->f('current_id')+1;
+
+                       $values['descr'] = 
$this->db->db_addslashes($values['descr']);
+
+                       $values_insert= array(
+                               $values['workorder_id'],
+                               $values['hour_id'],
+                               $id,
+                               $values['amount'],
+                               $values['descr'],
+                               time()
+                               );
+
+                       $values_insert  = 
$this->bocommon->validate_db_insert($values_insert);
+
+                       $this->db->query("INSERT INTO fm_wo_h_deviation 
(workorder_id,hour_id,id,amount,descr,entry_date) VALUES 
($values_insert)",__LINE__,__FILE__);
+
+                       $receipt['id']= $id;
+
+                       $receipt['message'][] = array('msg'=> lang('deviation 
has been added'));
+                       return $receipt;
+               }
+
+               function edit_deviation($values)
+               {
+                       $values['descr'] = 
$this->db->db_addslashes($values['descr']);
+
+                       $value_set=array(
+                               'amount'                        => 
$values['amount'],
+                               'descr'                 => 
$this->db->db_addslashes($values['descr'])
+                               );
+
+                       $value_set      = 
$this->bocommon->validate_db_update($value_set);
+
+                       $this->db->query("UPDATE fm_wo_h_deviation set 
$value_set WHERE workorder_id=" . $values['workorder_id'] . " AND hour_id=" . 
$values['hour_id'] . " AND id=" . $values['id'],__LINE__,__FILE__);
+
+                       $receipt['message'][] = array('msg'=> lang('deviation 
has been edited'));
+
+                       return $receipt;
+               }
+
+               function update_deviation($data)
+               {
+                       if(is_array($data))
+                       {
+                               $id = 
(isset($data['workorder_id'])?$data['workorder_id']:0);
+                               $deviation = 
(isset($data['sum_deviation'])?$data['sum_deviation']:0);
+                       }
+                       $this->db->query("UPDATE fm_workorder set deviation = 
$deviation WHERE id=$id",__LINE__,__FILE__);
+                       return $receipt;
+               }
+
+
+               function next_record($workorder_id)
+               {
+                       $this->db->query("SELECT  max(record) as record FROM 
fm_wo_hours where workorder_id='$workorder_id'",__LINE__,__FILE__);
+                       $this->db->next_record();
+                       $record = $this->db->f('record')+1;
+                       return $record;
+               }
+
+               function add_template($values,$workorder_id)
+               {
+
+                       $values['name'] = 
$this->db->db_addslashes($values['name']);
+                       $soworkorder    = 
CreateObject($this->currentapp.'.soworkorder');
+                       $workorder              = 
$soworkorder->read_single($workorder_id);
+
+                       $this->db->transaction_begin();
+
+                       $values_insert= array(
+                               $this->account,
+                               $values['name'],
+                               $values['descr'],
+                               $workorder['chapter_id'],
+                               time()
+                                );
+
+                       $values_insert  = 
$this->bocommon->validate_db_insert($values_insert);
+
+                       $this->db->query("insert into fm_template 
(owner,name,descr,chapter_id,entry_date) "
+                               . " values ($values_insert)",__LINE__,__FILE__);
+
+                       unset ($values_insert);
+
+                       $template_id = 
$this->db->get_last_insert_id('fm_template','id');
+
+                       $hour = $this->read(array('workorder_id' => 
$workorder_id));
+
+                       $record = $this->next_record($workorder_id);
+
+                       for ($i=0; $i<count($hour); $i++)
+                       {
+                               $values_insert= array(
+                                       $hour[$i]['activity_id'],
+                                       $hour[$i]['activity_num'],
+                                       $this->account,
+                                       $hour[$i]['hours_descr'],
+                                       $hour[$i]['unit'],
+                                       $hour[$i]['cost'],
+                                       $hour[$i]['quantity'],
+                                       $hour[$i]['billperae'],
+                                       $hour[$i]['ns3420_id'],
+                                       $hour[$i]['dim_d'],
+                                       $hour[$i]['grouping_id'],
+                                       $hour[$i]['grouping_descr'],
+                                       $hour[$i]['remark'],
+                                       $hour[$i]['tolerance'],
+                                       $hour[$i]['building_part'],
+                                       $record,
+                                       $template_id );
+
+                               $values_insert  = 
$this->bocommon->validate_db_insert($values_insert);
+
+                               $this->db->query("insert into fm_template_hours 
(activity_id,activity_num,owner,hours_descr,unit,"
+                               . 
"cost,quantity,billperae,ns3420_id,dim_d,grouping_id,grouping_descr,remark,tolerance,building_part,record,template_id)
 "
+                               . " values ($values_insert)",__LINE__,__FILE__);
+
+                               $record++;
+                       }
+
+                       $this->db->transaction_commit();
+
+                       $receipt['message'][] = array('msg'=>lang('template %1 
is added',$values['name']));
+                       return $receipt;
+               }
+
+
+               function add_hour($hour)
+               {
+                       $record = $this->next_record($hour[0]['workorder_id']);
+
+                       for ($i=0; $i<count($hour); $i++)
+                       {
+                               $values= array(
+                                       $hour[$i]['activity_id'],
+                                       $hour[$i]['activity_num'],
+                                       $this->account,
+                                       $hour[$i]['hours_descr'],
+                                       $hour[$i]['unit'],
+                                       $hour[$i]['cost'],
+                                       $hour[$i]['quantity'],
+                                       $hour[$i]['billperae'],
+                                       $hour[$i]['ns3420_id'],
+                                       $hour[$i]['dim_d'],
+                                       $record,
+                                       time(),
+                                       $hour[$i]['workorder_id'],
+                                       $hour[$i]['wo_hour_cat']);
+
+                               $values = 
$this->bocommon->validate_db_insert($values);
+
+                               $this->db->query("insert into fm_wo_hours 
(activity_id,activity_num,owner,hours_descr,unit,cost,quantity,billperae,ns3420_id,dim_d,record,entry_date,workorder_id,category)
 "
+                               . " values ($values)",__LINE__,__FILE__);
+
+                               $record++;
+                       }
+
+                       $receipt['message'][] = array('msg'=>lang('%1 entries 
is added!',count($hour)));
+
+                       $receipt['hour_id'] = 
$this->db->get_last_insert_id('fm_wo_hours','id');
+
+                       return $receipt;
+               }
+
+               function add_hour_from_template($hour,$workorder_id)
+               {
+
+                       $record = $this->next_record($workorder_id);
+
+                       if($hour[0]['chapter_id'])
+                       {
+                               $this->db->query("UPDATE fm_workorder set
+                                       chapter_id      ='" . 
$hour[0]['chapter_id'] . "' WHERE id= '$workorder_id'",__LINE__,__FILE__);
+                       }
+
+                       for ($i=0; $i<count($hour); $i++)
+                       {
+
+                               if($hour[$i]['new_grouping'])
+                               {
+                                       $this->db->query("SELECT grouping_id 
FROM fm_wo_hours where grouping_descr ='" .$hour[$i]['new_grouping'] . "' and 
workorder_id= '$workorder_id'",__LINE__,__FILE__);
+                                       $this->db->next_record();
+                                       if ( $this->db->f('grouping_id'))
+                                       {
+                                               $grouping_id    = 
$this->db->f('grouping_id');
+                                       }
+                                       else
+                                       {
+                                               $this->db->query("SELECT 
max(grouping_id) as grouping_id FROM fm_wo_hours where workorder_id= 
'$workorder_id'",__LINE__,__FILE__);
+                                               $this->db->next_record();
+                                               $grouping_id    = 
$this->db->f('grouping_id')+1;
+                                       }
+
+                                       $grouping_descr = 
$hour[$i]['new_grouping'];
+                               }
+
+                               $values= array(
+                                       $this->account,
+                                       $hour[$i]['activity_id'],
+                                       $hour[$i]['activity_num'],
+                                       $hour[$i]['hours_descr'],
+                                       $hour[$i]['unit'],
+                                       $hour[$i]['cost'],
+                                       $hour[$i]['quantity'],
+                                       $hour[$i]['billperae'],
+                                       $hour[$i]['ns3420_id'],
+                                       $hour[$i]['dim_d'],
+                                       $grouping_id,
+                                       $grouping_descr,
+                                       $record,
+                                       $hour[$i]['building_part_id'],
+                                       $hour[$i]['tolerance_id'],
+                                       $hour[$i]['remark'],
+                                       time(),
+                                       $workorder_id );
+
+                               $values = 
$this->bocommon->validate_db_insert($values);
+
+                               $this->db->query("insert into fm_wo_hours 
(owner,activity_id,activity_num,hours_descr,unit,cost,quantity,billperae,ns3420_id,dim_d,"
+                               . " 
grouping_id,grouping_descr,record,building_part,tolerance,remark,entry_date,workorder_id)
 "
+                               . " values ($values     )",__LINE__,__FILE__);
+
+                               $record++;
+                       }
+
+                       $receipt['message'][] = array('msg'=>lang('the number 
of %1 hour is added!',$i));
+
+                       return $receipt;
+               }
+
+               function add_custom_hour($hour,$workorder_id)
+               {
+
+                       $hour['record'] = $this->next_record($workorder_id);
+
+                       if($hour['chapter_id'])
+                       {
+                               $this->db->query("UPDATE fm_workorder set
+                               chapter_id      ='" . $hour['chapter_id'] . "' 
WHERE id= '$workorder_id'",__LINE__,__FILE__);
+                       }
+
+                       if($hour['grouping_id'])
+                       {
+                               $this->db->query("SELECT grouping_descr , 
max(record) as record FROM fm_wo_hours where grouping_id='" 
.$hour['grouping_id'] . "' and workorder_id= '$workorder_id' GROUP by 
grouping_descr",__LINE__,__FILE__);
+                               $this->db->next_record();
+                               $hour['grouping_descr'] = 
$this->db->f('grouping_descr');
+//                             $hour['record'] = $this->db->f('record')+1;
+                       }
+
+                       if($hour['new_grouping'])
+                       {
+                               $this->db->query("SELECT grouping_id FROM 
fm_wo_hours where grouping_descr ='" .$hour['new_grouping'] . "' and 
workorder_id= '$workorder_id'",__LINE__,__FILE__);
+                               $this->db->next_record();
+                               if ( $this->db->f('grouping_id'))
+                               {
+                                       $hour['grouping_id']    = 
$this->db->f('grouping_id');
+                               }
+                               else
+                               {
+                                       $this->db->query("SELECT 
max(grouping_id) as grouping_id FROM fm_wo_hours where workorder_id= 
'$workorder_id'",__LINE__,__FILE__);
+                                       $this->db->next_record();
+                                       $hour['grouping_id']    = 
$this->db->f('grouping_id')+1;
+//                                     $hour['record'] = 1;
+                               }
+
+                               $hour['grouping_descr'] = $hour['new_grouping'];
+                       }
+
+                       $values= array(
+                                       $this->account,
+                                       $hour['descr'],
+                                       $hour['unit'],
+                                       $hour['cost'],
+                                       $hour['quantity'],
+                                       $hour['billperae'],
+                                       $hour['ns3420_id'],
+                                       $hour['dim_d'],
+                                       $hour['grouping_id'],
+                                       $hour['grouping_descr'],
+                                       $hour['record'],
+                                       $hour['building_part_id'],
+                                       $hour['tolerance_id'],
+                                       $hour['remark'],
+                                       time(),
+                                       $workorder_id,
+                                       $hour['wo_hour_cat']
+                                       );
+
+                       $values = $this->bocommon->validate_db_insert($values);
+
+                       $this->db->query("insert into fm_wo_hours 
(owner,hours_descr,unit,cost,quantity,billperae,ns3420_id,dim_d,"
+                               . " 
grouping_id,grouping_descr,record,building_part,tolerance,remark,entry_date,workorder_id,category)
 "
+                               . "VALUES ( $values )",__LINE__,__FILE__);
+
+                       $receipt['hour_id'] = 
$this->db->get_last_insert_id('fm_wo_hours','id');
+
+                       $receipt['message'][] = array('msg'=>lang('hour %1 is 
added!',$receipt['hour_id']));
+
+                       return $receipt;
+
+               }
+
+               function read_single_hour($hour_id)
+               {
+                       $sql = "SELECT * from fm_wo_hours where id='$hour_id'";
+
+                       $this->db->query($sql,__LINE__,__FILE__);
+
+                       if ($this->db->next_record())
+                       {
+                               $hour['hour_id']                                
= $this->db->f('id');
+                               $hour['record']                                 
= $this->db->f('record');
+                               $hour['activity_id']                    = 
$this->db->f('activity_id');
+                               $hour['activity_num']                   = 
$this->db->f('activity_num');
+                               $hour['grouping_id']                    = 
$this->db->f('grouping_id');
+                               $hour['hours_descr']                    = 
$this->db->f('hours_descr');
+                               $hour['remark']                                 
= $this->db->f('remark');
+                               $hour['billperae']                              
= $this->db->f('billperae');
+                               $hour['unit']                                   
= $this->db->f('unit');
+                               $hour['ns3420_id']                              
= $this->db->f('ns3420_id');
+                               $hour['tolerance_id']                   = 
(int)$this->db->f('tolerance');
+                               $hour['building_part_id']               = 
(int)$this->db->f('building_part');
+                               $hour['quantity']                               
= $this->db->f('quantity');
+                               $hour['cost']                                   
= $this->db->f('cost');
+                               $hour['dim_d']                                  
= $this->db->f('dim_d');
+                               $hour['wo_hour_cat']                            
= $this->db->f('category');
+                       }
+
+                       return $hour;
+               }
+
+               function edit($hour,$workorder_id)
+               {
+
+                       $hour['descr'] = 
$this->db->db_addslashes($hour['descr']);
+                       $hour['remark'] = 
$this->db->db_addslashes($hour['remark']);
+//_debug_array($hour);
+
+
+                       if($hour['chapter_id'])
+                       {
+                               $this->db->query("UPDATE fm_workorder set
+                               chapter_id      ='" . $hour['chapter_id'] . "' 
WHERE id= '$workorder_id'",__LINE__,__FILE__);
+                       }
+
+                       if($hour['new_grouping'])
+                       {
+                               $this->db->query("SELECT grouping_id FROM 
fm_wo_hours where grouping_descr ='" .$hour['new_grouping'] . "' and 
workorder_id= '$workorder_id'",__LINE__,__FILE__);
+                               $this->db->next_record();
+                               if ( $this->db->f('grouping_id'))
+                               {
+                                       $hour['grouping_id']    = 
$this->db->f('grouping_id');
+                               }
+                               else
+                               {
+
+                                       $this->db->query("UPDATE fm_wo_hours 
set grouping_id = NULL WHERE id ='" .$hour['hour_id'] . "'",__LINE__,__FILE__);
+                                       $this->db->query("SELECT 
count(grouping_id) as num_grouping FROM fm_wo_hours where workorder_id= 
'$workorder_id' and grouping_id >0 ",__LINE__,__FILE__);
+                                       $this->db->next_record();
+                                       if ($this->db->f('num_grouping')==1)
+                                       {
+                                               $hour['grouping_id']=1;
+                                       }
+                                       else
+                                       {
+                                               $this->db->query("SELECT 
max(grouping_id) as grouping_id FROM fm_wo_hours where workorder_id= 
'$workorder_id'",__LINE__,__FILE__);
+                                               $this->db->next_record();
+                                               $hour['grouping_id']    = 
$this->db->f('grouping_id')+1;
+                                       }
+                               }
+                               $hour['grouping_descr'] = $hour['new_grouping'];
+                       }
+                       else
+                       {
+                               $this->db->query("SELECT 
grouping_id,grouping_descr FROM fm_wo_hours where id ='" .$hour['hour_id'] . 
"'",__LINE__,__FILE__);
+                               $this->db->next_record();
+                               $old_grouping_id        = 
$this->db->f('grouping_id');
+
+                               if ( $old_grouping_id == $hour['grouping_id'])
+                               {
+
+                                       $hour['grouping_descr'] = 
$this->db->f('grouping_descr');
+                               }
+                               else
+                               {
+                                       $this->db->query("SELECT grouping_descr 
, max(record) as record FROM fm_wo_hours where grouping_id='" 
.$hour['grouping_id'] . "' and workorder_id= '$workorder_id' GROUP by 
grouping_descr",__LINE__,__FILE__);
+                                       $this->db->next_record();
+                                       if($this->db->f('grouping_descr'))
+                                       {
+                                               $hour['grouping_descr'] = 
$this->db->f('grouping_descr');
+                                       }
+                                       else
+                                       {
+                                               $hour['grouping_id']='';
+                                               $hour['grouping_descr']='';
+                                       }
+
+/*                                     if($old_record>1)
+                                       {
+                                               $this->db->query("UPDATE 
fm_wo_hours set
+                                                       record  = record - 1 
where grouping_id='" .$hour['grouping_id'] . "' and workorder_id= 
'$workorder_id' and record > $old_record ",__LINE__,__FILE__);
+
+                                       }
+
+*/
+                               }
+                       }
+
+                       $value_set=array(
+                               'hours_descr'           => $hour['descr'],
+                               'remark'                        => 
$hour['remark'],
+                               'billperae'                     => 
$hour['billperae'],
+                               'unit'                          => 
$hour['unit'],
+                               'quantity'                      => 
$hour['quantity'],
+                               'cost'                          => 
$hour['cost'],
+                               'ns3420_id'                     => 
$hour['ns3420_id'],
+                               'tolerance'                     => 
$hour['tolerance_id'],
+                               'building_part'         => 
$hour['building_part_id'],
+                               'dim_d'                         => 
$hour['dim_d'],
+                               'grouping_id'           => $hour['grouping_id'],
+                               'grouping_descr'        => 
$hour['grouping_descr'],
+                               'category'              => $hour['wo_hour_cat']
+                               );
+
+                       $value_set      = 
$this->bocommon->validate_db_update($value_set);
+
+                       $this->db->query("UPDATE fm_wo_hours set $value_set 
WHERE id= '" . $hour['hour_id'] ."'",__LINE__,__FILE__);
+
+                       $receipt['hour_id'] = $hour['hour_id'];
+                       $receipt['message'][] = array('msg'=>lang('hour %1 has 
been edited',$hour['hour_id']));
+                       return $receipt;
+
+               }
+
+               function update_email($to_email,$workorder_id)
+               {
+                       $this->db->query("SELECT vendor_id FROM fm_workorder 
where id ='$workorder_id'",__LINE__,__FILE__);
+                       $this->db->next_record();
+                       $vendor_id      = $this->db->f('vendor_id');
+
+                       $this->db->query("UPDATE fm_vendor set email 
='$to_email' where  id= '$vendor_id'",__LINE__,__FILE__);
+               }
+
+               function get_email($vendor_id)
+               {
+                       $vendor_id=(int)$vendor_id;
+
+                       $this->db->query("select column_name from 
fm_vendor_attribute WHERE datatype='email'",__LINE__,__FILE__);
+                       while ($this->db->next_record())
+                       {
+                               $this->db2->query("select " . 
$this->db->f('column_name') . " from fm_vendor where 
id=$vendor_id",__LINE__,__FILE__);
+                               while ($this->db2->next_record())
+                               {
+                                       
if($this->db2->f($this->db->f('column_name')))
+                                       {
+                                               $email_list[] = array('email' 
=> $this->db2->f($this->db->f('column_name')));
+                                       }
+                               }
+                       }
+
+                       return  $email_list;
+               }
+
+               function delete($hour_id,$workorder_id )
+               {
+                       $this->db->query("SELECT record FROM fm_wo_hours where 
id ='$hour_id'",__LINE__,__FILE__);
+                       $this->db->next_record();
+                       $old_record     = $this->db->f('record');
+
+                       $this->db->transaction_begin();
+
+                       $this->db->query("DELETE FROM fm_wo_hours WHERE id='" . 
$hour_id . "'",__LINE__,__FILE__);
+                       $this->db->query("DELETE FROM fm_wo_h_deviation WHERE 
workorder_id=$workorder_id AND hour_id=$hour_id",__LINE__,__FILE__);
+                       if($old_record)
+                       {
+                               $this->db->query("UPDATE fm_wo_hours set record 
= record - 1 where  workorder_id= '$workorder_id' and record > $old_record 
",__LINE__,__FILE__);
+                       }
+
+                       $this->db->transaction_commit();
+
+                       $receipt['message'][] = array('msg'=>lang('hour %1 has 
been deleted',$hour_id));
+                       return $receipt;
+
+               }
+
+               function delete_deviation($workorder_id,$hour_id,$id )
+               {
+                       $this->db->query("DELETE FROM fm_wo_h_deviation WHERE 
workorder_id=$workorder_id AND hour_id=$hour_id AND id=$id",__LINE__,__FILE__);
+                       $receipt['message'][] = array('msg'=>lang('deviation %1 
has been deleted',$id));
+                       return $receipt;
+               }
+
+               function update_calculation($data)
+               {
+                       if(is_array($data))
+                       {
+                               $id = 
(isset($data['workorder_id'])?$data['workorder_id']:0);
+                               $calculation = 
(isset($data['calculation'])?$data['calculation']:0);
+                       }
+                       $this->db->query("UPDATE fm_workorder set calculation = 
$calculation WHERE id=$id",__LINE__,__FILE__);
+                       return $receipt;
+               }
+       }
+?>
Index: property/inc/class.soworkorder.inc.php
diff -u property/inc/class.soworkorder.inc.php:1.13 
property/inc/class.soworkorder.inc.php:1.14
--- property/inc/class.soworkorder.inc.php:1.13 Fri Jan 27 14:05:43 2006
+++ property/inc/class.soworkorder.inc.php      Mon Jan 30 22:14:19 2006
@@ -1,582 +1,587 @@
-<?php
-       /**
-       * phpGroupWare - property: a Facilities Management System.
-       *
-       * @author Sigurd Nes <address@hidden>
-       * @copyright Copyright (C) 2003-2005 Free Software Foundation, Inc. 
http://www.fsf.org/
-       * @license http://www.gnu.org/licenses/gpl.html GNU General Public 
License
-       * @internal Development of this application was funded by 
http://www.bergen.kommune.no/bbb_/ekstern/
-       * @package property
-       * @subpackage project
-       * @version $Id: class.soworkorder.inc.php,v 1.13 2006/01/27 14:05:43 
sigurdne Exp $
-       */
-
-       /**
-        * Description
-        * @package property
-        */
-
-       class soworkorder
-       {
-
-               function soworkorder()
-               {
-                       $this->currentapp       = 
$GLOBALS['phpgw_info']['flags']['currentapp'];
-                       $this->account          = 
$GLOBALS['phpgw_info']['user']['account_id'];
-                       $this->bocommon         = 
CreateObject($this->currentapp.'.bocommon');
-                       $this->db               = $this->bocommon->new_db();
+<?php
+       /**
+       * phpGroupWare - property: a Facilities Management System.
+       *
+       * @author Sigurd Nes <address@hidden>
+       * @copyright Copyright (C) 2003-2005 Free Software Foundation, Inc. 
http://www.fsf.org/
+       * @license http://www.gnu.org/licenses/gpl.html GNU General Public 
License
+       * @internal Development of this application was funded by 
http://www.bergen.kommune.no/bbb_/ekstern/
+       * @package property
+       * @subpackage project
+       * @version $Id: class.soworkorder.inc.php,v 1.14 2006/01/30 22:14:19 
sigurdne Exp $
+       */
+
+       /**
+        * Description
+        * @package property
+        */
+
+       class soworkorder
+       {
+
+               function soworkorder()
+               {
+                       $this->currentapp       = 
$GLOBALS['phpgw_info']['flags']['currentapp'];
+                       $this->account          = 
$GLOBALS['phpgw_info']['user']['account_id'];
+                       $this->bocommon         = 
CreateObject($this->currentapp.'.bocommon');
+                       $this->db               = $this->bocommon->new_db();
                        $this->db2              = $this->bocommon->new_db();
-                       $this->join                     = $this->bocommon->join;
-                       $this->left_join        = $this->bocommon->left_join;
-                       $this->like                     = $this->bocommon->like;
-                       $this->grants           = 
$GLOBALS['phpgw']->session->appsession('grants_project',$this->currentapp);
-
-                       if(!$this->grants)
-                       {
-                               $this->acl2             = 
CreateObject($this->currentapp.'.acl2','##DEFAULT##','##DEFAULT##');
-                               $this->grants           = 
$this->acl2->get_grants($this->currentapp,'.project');
-                               
$GLOBALS['phpgw']->session->appsession('grants_project',$this->currentapp,$this->grants);
-                       }
-               }
-
-               function next_id()
-               {
-                       $this->db->query("select value from fm_idgenerator 
where name = 'workorder'");
-                       $this->db->next_record();
-                       $id = $this->db->f('value')+1;
-                       return $id;
-               }
-
-               function select_category_workorder_list()
-               {
-
-                       $this->db->query("SELECT id, descr FROM 
fm_workorder_category  ORDER BY descr ");
-
-                       $i = 0;
-                       while ($this->db->next_record())
-                       {
-                               $categories[$i]['id']                           
= $this->db->f('id');
-                               $categories[$i]['name']                         
= stripslashes($this->db->f('descr'));
-                               $i++;
-                       }
-                       return $categories;
-               }
-
-               function read_single_project_category($id='')
-               {
-                       $this->db->query("SELECT descr FROM 
fm_workorder_category where id='$id' ");
-                       $this->db->next_record();
-                       return $this->db->f('descr');
-               }
-
-               function get_b_account_name($id='')
-               {
-                       $this->db->query("SELECT descr FROM fm_b_account where 
id='$id' ");
-                       $this->db->next_record();
-                       return $this->db->f('descr');
-               }
-
-               function select_status_list()
-               {
-                       $this->db->query("SELECT id, descr FROM 
fm_workorder_status ORDER BY id ");
-
-                       $i = 0;
-                       while ($this->db->next_record())
-                       {
-                               $status_entries[$i]['id']                       
        = $this->db->f('id');
-                               $status_entries[$i]['name']                     
        = stripslashes($this->db->f('descr'));
-                               $i++;
-                       }
-                       return $status_entries;
-               }
-
-               function select_branch_list()
-               {
-                       $this->db->query("SELECT id, descr FROM fm_branch ORDER 
BY id ");
-
-                       $i = 0;
-                       while ($this->db->next_record())
-                       {
-                               $branch_entries[$i]['id']                       
        = $this->db->f('id');
-                               $branch_entries[$i]['name']                     
        = stripslashes($this->db->f('descr'));
-                               $i++;
-                       }
-                       return $branch_entries;
-               }
-
-               function select_key_location_list()
-               {
-                       $this->db->query("SELECT id, descr FROM fm_key_loc 
ORDER BY descr ");
-
-                       $i = 0;
-                       while ($this->db->next_record())
-                       {
-                               $key_location_entries[$i]['id']                 
        = $this->db->f('id');
-                               $key_location_entries[$i]['name']               
        = stripslashes($this->db->f('descr'));
-                               $i++;
-                       }
-                       return $key_location_entries;
-               }
-
-               function read($data)
-               {
-
-                       if(is_array($data))
-                       {
-                               if ($data['start'])
-                               {
-                                       $start=$data['start'];
-                               }
-                               else
-                               {
-                                       $start=0;
-                               }
-                               $filter = 
$data['filter']?$data['filter']:$this->account;
-                               $query = 
(isset($data['query'])?$data['query']:'');
-                               $sort = 
(isset($data['sort'])?$data['sort']:'DESC');
-                               $order = 
(isset($data['order'])?$data['order']:'');
-                               $cat_id = 
(isset($data['cat_id'])?$data['cat_id']:0);
-                               $status_id = 
(isset($data['status_id'])?$data['status_id']:0);
-                               $search_vendor = 
(isset($data['search_vendor'])?$data['search_vendor']:'');
-                               $start_date = 
(isset($data['start_date'])?$data['start_date']:'');
-                               $end_date = 
(isset($data['end_date'])?$data['end_date']:'');
-                               $allrows = 
(isset($data['allrows'])?$data['allrows']:'');
-                               $wo_hour_cat_id = 
(isset($data['wo_hour_cat_id'])?$data['wo_hour_cat_id']:'');
-                       }
-
-//_debug_array($data);
-                       $sql = 
$this->bocommon->fm_cache('sql_workorder'.!!$search_vendor . '_' . 
!!$wo_hour_cat_id);
-
-                       if(!$sql)
-                       {
-                               $entity_table = 'fm_project';
-
-                               $cols .= $entity_table . '.location_code';
-                               $cols_return[] = 'location_code';
-
-                               $cols .= ",$entity_table.id as project_id";
-                               $cols_return[]                          = 
'project_id';
-                               $uicols['input_type'][]         = 'text';
-                               $uicols['name'][]                       = 
'project_id';
-                               $uicols['descr'][]                      = 
lang('Project');
-                               $uicols['statustext'][]         = lang('Project 
ID');
-
-                               $cols .= ",fm_workorder.id as workorder_id";
-                               $cols_return[]                          = 
'workorder_id';
-                               $uicols['input_type'][]         = 'text';
-                               $uicols['name'][]                       = 
'workorder_id';
-                               $uicols['descr'][]                      = 
lang('Workorder');
-                               $uicols['statustext'][]         = 
lang('Workorder ID');
-
-                               $cols .= ",fm_workorder.title as title";
-                               $cols_return[]                          = 
'title';
-                               $uicols['input_type'][]         = 'text';
-                               $uicols['name'][]                       = 
'title';
-                               $uicols['descr'][]                      = 
lang('Title');
-                               $uicols['statustext'][]         = 
lang('Workorder title');
-
-                               $cols .= ",fm_workorder.status as status";
-                               $cols_return[]                          = 
'status';
-                               $uicols['input_type'][]         = 'text';
-                               $uicols['name'][]                       = 
'status';
-                               $uicols['descr'][]                      = 
lang('Status');
-                               $uicols['statustext'][]         = 
lang('Workorder status');
-
-                               $cols .= ",fm_workorder.entry_date as 
entry_date";
-                               $cols_return[]                          = 
'entry_date';
-                               $uicols['input_type'][]         = 'text';
-                               $uicols['name'][]                       = 
'entry_date';
-                               $uicols['descr'][]                      = 
lang('Entry date');
-                               $uicols['statustext'][]         = 
lang('Workorder entry date');
-
-                               $cols .= ",phpgw_accounts.account_lid as 
user_lid";
-                               $cols_return[]                          = 
'user_lid';
-                               $uicols['input_type'][]         = 'text';
-                               $uicols['name'][]                       = 
'user_lid';
-                               $uicols['descr'][]                      = 
lang('User');
-                               $uicols['statustext'][]         = 
lang('Workorder User');
-
-                               $cols .= ',fm_workorder.vendor_id';
-                               $cols_return[] = 'vendor_id';
-                               $uicols['input_type'][]         = 'text';
-                               $uicols['name'][]                       = 
'vendor_id';
-                               $uicols['descr'][]                      = 
lang('Vendor ID');
-
-                               $cols .= ",fm_project.user_id as project_owner";
-
-                               $joinmethod .= " $this->join  fm_workorder ON 
($entity_table.id = fm_workorder.project_id) $this->join  phpgw_accounts ON 
(fm_workorder.user_id = phpgw_accounts.account_id))";
-                               $paranthesis .='(';
-
-                               $cols .= ',fm_vendor.org_name';
-                               $cols_return[] = 'org_name';
-                               $uicols['input_type'][]         = 'hidden';
-                               $uicols['name'][]                       = 
'org_name';
-                               $uicols['descr'][]                      = 
lang('Vendor name');
-
-                               $joinmethod .= " $this->left_join  fm_vendor ON 
(fm_workorder.vendor_id = fm_vendor.id))";
-                               $paranthesis .='(';
-
-                               //----- wo_hour_status
-
-                               if($wo_hour_cat_id)
-                               {
-                                       $joinmethod .= " $this->join 
fm_wo_hours ON (fm_workorder.id = fm_wo_hours.workorder_id))";
-                                       $paranthesis .='(';
-
-                                       $joinmethod .= " $this->join 
fm_wo_hours_category ON (fm_wo_hours.category = fm_wo_hours_category.id))";
-                                       $paranthesis .='(';
-                               }
-
-                               //----- wo_hour_status
-
-                               $sql    = 
$this->bocommon->generate_sql(array('entity_table'=>$entity_table,'cols'=>$cols,'cols_return'=>$cols_return,
-                                                                       
'uicols'=>$uicols,'joinmethod'=>$joinmethod,'paranthesis'=>$paranthesis,'query'=>$query));
-
-
-                               
$this->bocommon->fm_cache('sql_workorder'.!!$search_vendor . '_' . 
!!$wo_hour_cat_id,$sql);
-
-                               $this->uicols           = 
$this->bocommon->uicols;
-                               $cols_return            = 
$this->bocommon->cols_return;
-                               $type_id                        = 
$this->bocommon->type_id;
-//                             $this->cols_extra       = 
$this->bocommon->cols_extra;
-
-                               
$this->bocommon->fm_cache('uicols_workorder'.!!$search_vendor . '_' . 
!!$wo_hour_cat_id,$this->uicols);
-                               
$this->bocommon->fm_cache('cols_return_workorder'.!!$search_vendor . '_' . 
!!$wo_hour_cat_id,$cols_return);
-                               
$this->bocommon->fm_cache('type_id_workorder'.!!$search_vendor . '_' . 
!!$wo_hour_cat_id,$type_id);
-//                             
$this->bocommon->fm_cache('cols_extra_workorder'.!!$search_vendor . '_' . 
!!$wo_hour_cat_id,$this->cols_extra);
-
-                       }
-                       else
-                       {
-                               $this->uicols           = 
$this->bocommon->fm_cache('uicols_workorder'.!!$search_vendor . '_' . 
!!$wo_hour_cat_id);
-                               $cols_return            = 
$this->bocommon->fm_cache('cols_return_workorder'.!!$search_vendor . '_' . 
!!$wo_hour_cat_id);
-                               $type_id                        = 
$this->bocommon->fm_cache('type_id_workorder'.!!$search_vendor . '_' . 
!!$wo_hour_cat_id);
-//                             $this->cols_extra       = 
$this->bocommon->fm_cache('cols_extra_workorder'.!!$search_vendor . '_' . 
!!$wo_hour_cat_id);
-                       }
-
-                       if ($order)
-                       {
-                               $ordermethod = " order by $order $sort";
-                       }
-                       else
-                       {
-                               $ordermethod = ' order by fm_workorder.id DESC';
-                       }
-
-                       $where= 'WHERE';
-
-                       if ($cat_id > 0)
-                       {
-                               $filtermethod .= " $where 
fm_project.category=$cat_id ";
-                               $where= 'AND';
-                       }
-
-                       if ($status_id)
-                       {
-                               $filtermethod .= " $where 
fm_workorder.status='$status_id' ";
-                               $where= 'AND';
-                       }
-
-                       if($wo_hour_cat_id)
-                       {
-                               $filtermethod .= " $where 
fm_wo_hours_category.id=$wo_hour_cat_id ";
-                               $where= 'AND';
-                               $group_method = " group by 
fm_project.id,fm_project.location_code,fm_workorder.id,workorder_id,title,fm_workorder.status,fm_workorder.entry_date,user_lid,fm_workorder.vendor_id,project_owner,fm_project.address,fm_vendor.org_name";
-                       }
-
-                       if ($filter=='all')
-                       {
-                               if (is_array($this->grants))
-                               {
-                                       $grants = $this->grants;
-                                       while (list($user) = each($grants))
-                                       {
-                                               $public_user_list[] = $user;
-                                       }
-                                       reset($public_user_list);
-                                       $filtermethod .= " $where 
(fm_project.access='public' AND fm_project.user_id IN(" . 
implode(',',$public_user_list) . "))";
-                                       $where= 'AND';
-                               }
-                       }
-                       else
-                       {
-                               $filtermethod .= " $where 
fm_workorder.user_id=$filter ";
-                               $where= 'AND';
-                       }
-
-                       if ($start_date)
-                       {
-                               $filtermethod .= " $where 
fm_workorder.start_date >= $start_date AND fm_workorder.start_date <= $end_date 
";
-                               $where= 'AND';
-                       }
-
-                       if($query)
-                       {
-                               $query = str_replace(",",'.',$query);
-                               if(stristr($query, '.'))
-                               {
-                                       $query=explode(".",$query);
-                                       $querymethod = " $where 
(fm_project.loc1='" . $query[0] . "' AND fm_project.loc".$type_id."='" . 
$query[1] . "')";
-                               }
-                               else
-                               {
-                                       $query = ereg_replace("'",'',$query);
-                                       $query = ereg_replace('"','',$query);
-
-                                       $querymethod = " $where 
(fm_workorder.title $this->like '%$query%' or fm_workorder.descr $this->like 
'%$query%' or fm_project.address $this->like '%$query%' or 
fm_project.location_code $this->like '%$query%' or fm_workorder.id $this->like 
'%$query%')";
-                               }
-                               $where= 'AND';
-
-                       }
-
-                       if($search_vendor)
-                       {
-                               if((int)$search_vendor>0)
-                               {
-                                       $querymethod_vendor = " $where 
fm_workorder.vendor_id=" .(int)$search_vendor ;
-                               }
-                               else
-                               {
-                                       $querymethod_vendor = " $where  
fm_vendor.org_name $this->like '%$search_vendor%'";
-                               }
-                       }
-
-                       $sql .= " $filtermethod $querymethod 
$querymethod_vendor $group_method";
-//echo $sql;
-                       $this->db2->query($sql,__LINE__,__FILE__);
-                       $this->total_records = $this->db2->num_rows();
-                       if(!$allrows)
-                       {
-                               $this->db->limit_query($sql . 
$ordermethod,$start,__LINE__,__FILE__);
-                       }
-                       else
-                       {
-                               $this->db->query($sql . 
$ordermethod,__LINE__,__FILE__);
-                       }
-
-                       $count_cols_return=count($cols_return);
-                       $j=0;
-                       while ($this->db->next_record())
-                       {
-                               for ($i=0;$i<$count_cols_return;$i++)
-                               {
-                                       $workorder_list[$j][$cols_return[$i]] = 
$this->db->f($cols_return[$i]);
-                                       $workorder_list[$j]['grants'] = 
(int)$this->grants[$this->db->f('project_owner')];
-                               }
-
-                               $location_code= $this->db->f('location_code');
-                               $location = split('-',$location_code);
-                               $count_location =count($location);
-                               for ($m=0;$m<$count_location;$m++)
-                               {
-                                       $workorder_list[$j]['loc' . ($m+1)] = 
$location[$m];
-                                       
$workorder_list[$j]['query_location']['loc' . ($m+1)]=implode("-", 
array_slice($location, 0, ($m+1)));
-                               }
-
-                               $j++;
-                       }
-
-                       return $workorder_list;
-               }
-
-               function read_single($workorder_id)
-               {
-                       $sql = "SELECT fm_workorder.*, fm_chapter.descr as 
chapter ,fm_project.user_id from fm_workorder $this->join fm_project on 
fm_workorder.project_id=fm_project.id  $this->left_join fm_chapter on "
-                               . " fm_workorder.chapter_id = fm_chapter.id 
where fm_workorder.id=$workorder_id";
-
-                       $this->db->query($sql,__LINE__,__FILE__);
-
-                       if ($this->db->next_record())
-                       {
-                               $workorder['workorder_id']              = 
$this->db->f('id');
-                               $workorder['project_id']                = 
$this->db->f('project_id');
-                               $workorder['title']                             
= $this->db->f('title');
-                               $workorder['name']                              
= $this->db->f('name');
-                               $workorder['key_fetch']                 = 
$this->db->f('key_fetch');
-                               $workorder['key_deliver']               = 
$this->db->f('key_deliver');
-                               $workorder['key_responsible']   = 
$this->db->f('key_responsible');
-                               $workorder['charge_tenant']             = 
$this->db->f('charge_tenant');
-                               $workorder['descr']                             
= $this->db->f('descr');
-                               $workorder['status']                    = 
$this->db->f('status');
-                               $workorder['budget']                    = 
(int)$this->db->f('budget');
-                               $workorder['calculation']                       
= 
($this->db->f('calculation')*(1+$this->db->f('addition')/100))+$this->db->f('rig_addition');
-                               $workorder['b_account_id']                      
= (int)$this->db->f('account_id');
-                               $workorder['addition_percentage']       = 
(int)$this->db->f('addition');
-                               $workorder['addition_rs']                       
= (int)$this->db->f('rig_addition');
-                               $workorder['act_mtrl_cost']                     
= $this->db->f('act_mtrl_cost');
-                               $workorder['act_vendor_cost']           = 
$this->db->f('act_vendor_cost');
-                               $workorder['user_id']                           
= $this->db->f('user_id');
-                               $workorder['vendor_id']                 = 
$this->db->f('vendor_id');
-                               $workorder['coordinator']               = 
$this->db->f('coordinator');
-                               $workorder['access']                    = 
$this->db->f('access');
-                               $workorder['start_date']                = 
$this->db->f('start_date');
-                               $workorder['end_date']                  = 
$this->db->f('end_date');
-                               $workorder['cat_id']                    = 
$this->db->f('category');
-                               $workorder['chapter_id']                = 
$this->db->f('chapter_id');
-                               $workorder['chapter']                   = 
$this->db->f('chapter');
-                               $workorder['deviation']                 = 
$this->db->f('deviation');
-                               $workorder['grants']                            
= (int)$this->grants[$this->db->f('user_id')];
-                       }
-
-//_debug_array($workorder);
-                               return $workorder;
-               }
-
-
-               function project_budget_from_workorder($project_id = '')
-               {
-                       $this->db->query("select budget, id as workorder_id 
from fm_workorder where project_id='$project_id'");
-                       while ($this->db->next_record())
-                       {
-                               $budget[] = array(
-                                       'workorder_id'  => 
$this->db->f('workorder_id'),
-                                       'budget'                => 
sprintf("%01.2f",$this->db->f('budget'))
-                                       );
-                       }
-                       return $budget;
-               }
-
-               function branch_p_list($project_id = '')
-               {
-
-                       $this->db2->query("SELECT branch_id from 
fm_projectbranch WHERE project_id='$project_id' ",__LINE__,__FILE__);
-                       while ($this->db2->next_record())
-                       {
-                               $selected[] = array('branch_id' => 
$this->db2->f('branch_id'));
-                       }
-
-                       return $selected;
-               }
-
-               function increment_workorder_id()
-               {
-                       $this->db->query("update fm_idgenerator set value = 
value + 1 where name = 'workorder'");
-               }
-
-               function add($workorder)
-               {
-                       $historylog     = 
CreateObject($this->currentapp.'.historylog','workorder');
-                       $workorder['descr'] = 
$this->db->db_addslashes($workorder['descr']);
-                       $workorder['title'] = 
$this->db->db_addslashes($workorder['title']);
-
-                       $values= array(
-                               $workorder['workorder_id'],
-                               $workorder['project_id'],
-                               $workorder['title'],
-                               'public',
-                               time(),
-                               $workorder['start_date'],
-                               $workorder['end_date'],
-                               $workorder['status'],
-                               $workorder['descr'],
-                               $workorder['budget'],
-                               $workorder['b_account_id'],
-                               $workorder['addition_rs'],
-                               $workorder['addition_percentage'],
-                               $workorder['key_deliver'],
-                               $workorder['key_fetch'],
-                               $workorder['vendor_id'],
-                               $workorder['charge_tenant'],
-                               $this->account);
-
-                       $values = $this->bocommon->validate_db_insert($values);
-
-                       $this->db->transaction_begin();
-
-                       $this->db->query("INSERT INTO fm_workorder 
(id,project_id,title,access,entry_date,start_date,end_date,status,"
-                               . 
"descr,budget,account_id,rig_addition,addition,key_deliver,key_fetch,vendor_id,charge_tenant,user_id)
 "
-                               . "VALUES ( $values )",__LINE__,__FILE__);
-
-                       $this->db->query("INSERT INTO fm_orders (id,type) 
VALUES (" . $workorder['workorder_id'] . ",'workorder')");
-
-/*
-                       if($workorder['charge_tenant'])
-                       {
-                               $this->db->query("UPDATE fm_project set 
charge_tenant = 1 WHERE id =" . $workorder['project_id']);
-                       }
-*/
-                       if($this->db->transaction_commit())
-                       {
-                               $this->increment_workorder_id();
-                               
$historylog->add('SO',$workorder['workorder_id'],$workorder['status']);
-                               $receipt['message'][] = 
array('msg'=>lang('workorder %1 has been saved',$workorder['workorder_id']));
-                       }
-                       else
-                       {
-                               $receipt['error'][] = array('msg'=>lang('the 
workorder has not been saved'));
-                       }
-                       return $receipt;
-               }
-
-               function edit($workorder)
-               {
-                       $historylog     = 
CreateObject($this->currentapp.'.historylog','workorder');
-                       $workorder['descr'] = 
$this->db->db_addslashes($workorder['descr']);
-                       $workorder['title'] = 
$this->db->db_addslashes($workorder['title']);
-
-                       $value_set=array(
-                               'title'                 => $workorder['title'],
-                               'status'                => $workorder['status'],
-                               'start_date'    => $workorder['start_date'],
-                               'end_date'              => 
$workorder['end_date'],
-                               'descr'                 => $workorder['descr'],
-                               'budget'                => 
(int)$workorder['budget'],
-                               'key_deliver'   => $workorder['key_deliver'],
-                               'key_fetch'             => 
$workorder['key_fetch'],
-                               'account_id'    => $workorder['b_account_id'],
-                               'rig_addition'  => $workorder['addition_rs'],
-                               'addition'              => 
$workorder['addition_percentage'],
-                               'charge_tenant' => $workorder['charge_tenant'],
-                               'vendor_id'             => 
$workorder['vendor_id']
-                               );
-
-                       $value_set      = 
$this->bocommon->validate_db_update($value_set);
-
-                       $this->db->query("SELECT status,budget FROM 
fm_workorder where id='" .$workorder['workorder_id']."'",__LINE__,__FILE__);
-                       $this->db->next_record();
-
-                       $old_status = $this->db->f('status');
-                       $old_budget = $this->db->f('budget');
-
-                       $this->db->transaction_begin();
-
-                       $this->db->query("UPDATE fm_workorder set $value_set 
WHERE id=" . $workorder['workorder_id'] ,__LINE__,__FILE__);
-
-/*                     if($workorder['charge_tenant'])
-                       {
-                               $this->db->query("UPDATE fm_project set 
charge_tenant = 1 WHERE id =" . $workorder['project_id']);
-                       }
-*/
-                       if($this->db->transaction_commit())
-                       {
-                               if ($old_status != $workorder['status'])
-                               {
-                                       
$historylog->add('S',$workorder['workorder_id'],$workorder['status']);
-                               }
-                               if ($old_budget != $workorder['budget'])
-                               {
-                                       
$historylog->add('B',$workorder['workorder_id'],$workorder['budget']);
-                               }
-
-                               $receipt['message'][] = 
array('msg'=>lang('workorder %1 has been edited',$workorder['workorder_id']));
-                       }
-                       else
-                       {
-                               $receipt['error'][] = 
array('msg'=>lang('workorder %1 has not been 
edited',$workorder['workorder_id']));
-                       }
-
-                       return $receipt;
-               }
-
-               function delete($workorder_id )
-               {
-                       $this->db->transaction_begin();
-                       $this->db->query("DELETE FROM fm_workorder WHERE id='" 
. $workorder_id . "'",__LINE__,__FILE__);
-                       $this->db->query("DELETE FROM fm_workorder_history  
WHERE  history_record_id='" . $workorder_id   . "'",__LINE__,__FILE__);
-                       $this->db->query("DELETE FROM fm_wo_hours WHERE 
workorder_id='" . $workorder_id   . "'",__LINE__,__FILE__);
-                       $this->db->query("DELETE FROM fm_orders WHERE id='" . 
$workorder_id . "'",__LINE__,__FILE__);
-                       $this->db->transaction_commit();
-
-               }
-       }
-?>
+                       $this->join                     = $this->bocommon->join;
+
+                       $this->left_join        = $this->bocommon->left_join;
+
+                       $this->like                     = $this->bocommon->like;
+
+                       $this->grants           = 
$GLOBALS['phpgw']->session->appsession('grants_project',$this->currentapp);
+
+
+
+                       if(!$this->grants)
+                       {
+                               $this->acl2             = 
CreateObject($this->currentapp.'.acl2','##DEFAULT##','##DEFAULT##');
+                               $this->grants           = 
$this->acl2->get_grants($this->currentapp,'.project');
+                               
$GLOBALS['phpgw']->session->appsession('grants_project',$this->currentapp,$this->grants);
+                       }
+               }
+
+               function next_id()
+               {
+                       $this->db->query("select value from fm_idgenerator 
where name = 'workorder'");
+                       $this->db->next_record();
+                       $id = $this->db->f('value')+1;
+                       return $id;
+               }
+
+               function select_category_workorder_list()
+               {
+
+                       $this->db->query("SELECT id, descr FROM 
fm_workorder_category  ORDER BY descr ");
+
+                       $i = 0;
+                       while ($this->db->next_record())
+                       {
+                               $categories[$i]['id']                           
= $this->db->f('id');
+                               $categories[$i]['name']                         
= stripslashes($this->db->f('descr'));
+                               $i++;
+                       }
+                       return $categories;
+               }
+
+               function read_single_project_category($id='')
+               {
+                       $this->db->query("SELECT descr FROM 
fm_workorder_category where id='$id' ");
+                       $this->db->next_record();
+                       return $this->db->f('descr');
+               }
+
+               function get_b_account_name($id='')
+               {
+                       $this->db->query("SELECT descr FROM fm_b_account where 
id='$id' ");
+                       $this->db->next_record();
+                       return $this->db->f('descr');
+               }
+
+               function select_status_list()
+               {
+                       $this->db->query("SELECT id, descr FROM 
fm_workorder_status ORDER BY id ");
+
+                       $i = 0;
+                       while ($this->db->next_record())
+                       {
+                               $status_entries[$i]['id']                       
        = $this->db->f('id');
+                               $status_entries[$i]['name']                     
        = stripslashes($this->db->f('descr'));
+                               $i++;
+                       }
+                       return $status_entries;
+               }
+
+               function select_branch_list()
+               {
+                       $this->db->query("SELECT id, descr FROM fm_branch ORDER 
BY id ");
+
+                       $i = 0;
+                       while ($this->db->next_record())
+                       {
+                               $branch_entries[$i]['id']                       
        = $this->db->f('id');
+                               $branch_entries[$i]['name']                     
        = stripslashes($this->db->f('descr'));
+                               $i++;
+                       }
+                       return $branch_entries;
+               }
+
+               function select_key_location_list()
+               {
+                       $this->db->query("SELECT id, descr FROM fm_key_loc 
ORDER BY descr ");
+
+                       $i = 0;
+                       while ($this->db->next_record())
+                       {
+                               $key_location_entries[$i]['id']                 
        = $this->db->f('id');
+                               $key_location_entries[$i]['name']               
        = stripslashes($this->db->f('descr'));
+                               $i++;
+                       }
+                       return $key_location_entries;
+               }
+
+               function read($data)
+               {
+
+                       if(is_array($data))
+                       {
+                               if ($data['start'])
+                               {
+                                       $start=$data['start'];
+                               }
+                               else
+                               {
+                                       $start=0;
+                               }
+                               $filter = 
$data['filter']?$data['filter']:$this->account;
+                               $query = 
(isset($data['query'])?$data['query']:'');
+                               $sort = 
(isset($data['sort'])?$data['sort']:'DESC');
+                               $order = 
(isset($data['order'])?$data['order']:'');
+                               $cat_id = 
(isset($data['cat_id'])?$data['cat_id']:0);
+                               $status_id = 
(isset($data['status_id'])?$data['status_id']:0);
+                               $search_vendor = 
(isset($data['search_vendor'])?$data['search_vendor']:'');
+                               $start_date = 
(isset($data['start_date'])?$data['start_date']:'');
+                               $end_date = 
(isset($data['end_date'])?$data['end_date']:'');
+                               $allrows = 
(isset($data['allrows'])?$data['allrows']:'');
+                               $wo_hour_cat_id = 
(isset($data['wo_hour_cat_id'])?$data['wo_hour_cat_id']:'');
+                       }
+
+//_debug_array($data);
+                       $sql = 
$this->bocommon->fm_cache('sql_workorder'.!!$search_vendor . '_' . 
!!$wo_hour_cat_id);
+
+                       if(!$sql)
+                       {
+                               $entity_table = 'fm_project';
+
+                               $cols .= $entity_table . '.location_code';
+                               $cols_return[] = 'location_code';
+
+                               $cols .= ",$entity_table.id as project_id";
+                               $cols_return[]                          = 
'project_id';
+                               $uicols['input_type'][]         = 'text';
+                               $uicols['name'][]                       = 
'project_id';
+                               $uicols['descr'][]                      = 
lang('Project');
+                               $uicols['statustext'][]         = lang('Project 
ID');
+
+                               $cols .= ",fm_workorder.id as workorder_id";
+                               $cols_return[]                          = 
'workorder_id';
+                               $uicols['input_type'][]         = 'text';
+                               $uicols['name'][]                       = 
'workorder_id';
+                               $uicols['descr'][]                      = 
lang('Workorder');
+                               $uicols['statustext'][]         = 
lang('Workorder ID');
+
+                               $cols .= ",fm_workorder.title as title";
+                               $cols_return[]                          = 
'title';
+                               $uicols['input_type'][]         = 'text';
+                               $uicols['name'][]                       = 
'title';
+                               $uicols['descr'][]                      = 
lang('Title');
+                               $uicols['statustext'][]         = 
lang('Workorder title');
+
+                               $cols .= ",fm_workorder.status as status";
+                               $cols_return[]                          = 
'status';
+                               $uicols['input_type'][]         = 'text';
+                               $uicols['name'][]                       = 
'status';
+                               $uicols['descr'][]                      = 
lang('Status');
+                               $uicols['statustext'][]         = 
lang('Workorder status');
+
+                               $cols .= ",fm_workorder.entry_date as 
entry_date";
+                               $cols_return[]                          = 
'entry_date';
+                               $uicols['input_type'][]         = 'text';
+                               $uicols['name'][]                       = 
'entry_date';
+                               $uicols['descr'][]                      = 
lang('Entry date');
+                               $uicols['statustext'][]         = 
lang('Workorder entry date');
+
+                               $cols .= ",phpgw_accounts.account_lid as 
user_lid";
+                               $cols_return[]                          = 
'user_lid';
+                               $uicols['input_type'][]         = 'text';
+                               $uicols['name'][]                       = 
'user_lid';
+                               $uicols['descr'][]                      = 
lang('User');
+                               $uicols['statustext'][]         = 
lang('Workorder User');
+
+                               $cols .= ',fm_workorder.vendor_id';
+                               $cols_return[] = 'vendor_id';
+                               $uicols['input_type'][]         = 'text';
+                               $uicols['name'][]                       = 
'vendor_id';
+                               $uicols['descr'][]                      = 
lang('Vendor ID');
+
+                               $cols .= ",fm_project.user_id as project_owner";
+
+                               $joinmethod .= " $this->join  fm_workorder ON 
($entity_table.id = fm_workorder.project_id) $this->join  phpgw_accounts ON 
(fm_workorder.user_id = phpgw_accounts.account_id))";
+                               $paranthesis .='(';
+
+                               $cols .= ',fm_vendor.org_name';
+                               $cols_return[] = 'org_name';
+                               $uicols['input_type'][]         = 'hidden';
+                               $uicols['name'][]                       = 
'org_name';
+                               $uicols['descr'][]                      = 
lang('Vendor name');
+
+                               $joinmethod .= " $this->left_join  fm_vendor ON 
(fm_workorder.vendor_id = fm_vendor.id))";
+                               $paranthesis .='(';
+
+                               //----- wo_hour_status
+
+                               if($wo_hour_cat_id)
+                               {
+                                       $joinmethod .= " $this->join 
fm_wo_hours ON (fm_workorder.id = fm_wo_hours.workorder_id))";
+                                       $paranthesis .='(';
+
+                                       $joinmethod .= " $this->join 
fm_wo_hours_category ON (fm_wo_hours.category = fm_wo_hours_category.id))";
+                                       $paranthesis .='(';
+                               }
+
+                               //----- wo_hour_status
+
+                               $sql    = 
$this->bocommon->generate_sql(array('entity_table'=>$entity_table,'cols'=>$cols,'cols_return'=>$cols_return,
+                                                                       
'uicols'=>$uicols,'joinmethod'=>$joinmethod,'paranthesis'=>$paranthesis,'query'=>$query));
+
+
+                               
$this->bocommon->fm_cache('sql_workorder'.!!$search_vendor . '_' . 
!!$wo_hour_cat_id,$sql);
+
+                               $this->uicols           = 
$this->bocommon->uicols;
+                               $cols_return            = 
$this->bocommon->cols_return;
+                               $type_id                        = 
$this->bocommon->type_id;
+//                             $this->cols_extra       = 
$this->bocommon->cols_extra;
+
+                               
$this->bocommon->fm_cache('uicols_workorder'.!!$search_vendor . '_' . 
!!$wo_hour_cat_id,$this->uicols);
+                               
$this->bocommon->fm_cache('cols_return_workorder'.!!$search_vendor . '_' . 
!!$wo_hour_cat_id,$cols_return);
+                               
$this->bocommon->fm_cache('type_id_workorder'.!!$search_vendor . '_' . 
!!$wo_hour_cat_id,$type_id);
+//                             
$this->bocommon->fm_cache('cols_extra_workorder'.!!$search_vendor . '_' . 
!!$wo_hour_cat_id,$this->cols_extra);
+
+                       }
+                       else
+                       {
+                               $this->uicols           = 
$this->bocommon->fm_cache('uicols_workorder'.!!$search_vendor . '_' . 
!!$wo_hour_cat_id);
+                               $cols_return            = 
$this->bocommon->fm_cache('cols_return_workorder'.!!$search_vendor . '_' . 
!!$wo_hour_cat_id);
+                               $type_id                        = 
$this->bocommon->fm_cache('type_id_workorder'.!!$search_vendor . '_' . 
!!$wo_hour_cat_id);
+//                             $this->cols_extra       = 
$this->bocommon->fm_cache('cols_extra_workorder'.!!$search_vendor . '_' . 
!!$wo_hour_cat_id);
+                       }
+
+                       if ($order)
+                       {
+                               $ordermethod = " order by $order $sort";
+                       }
+                       else
+                       {
+                               $ordermethod = ' order by fm_workorder.id DESC';
+                       }
+
+                       $where= 'WHERE';
+
+                       if ($cat_id > 0)
+                       {
+                               $filtermethod .= " $where 
fm_project.category=$cat_id ";
+                               $where= 'AND';
+                       }
+
+                       if ($status_id)
+                       {
+                               $filtermethod .= " $where 
fm_workorder.status='$status_id' ";
+                               $where= 'AND';
+                       }
+
+                       if($wo_hour_cat_id)
+                       {
+                               $filtermethod .= " $where 
fm_wo_hours_category.id=$wo_hour_cat_id ";
+                               $where= 'AND';
+                               $group_method = " group by 
fm_project.id,fm_project.location_code,fm_workorder.id,workorder_id,title,fm_workorder.status,fm_workorder.entry_date,user_lid,fm_workorder.vendor_id,project_owner,fm_project.address,fm_vendor.org_name";
+                       }
+
+                       if ($filter=='all')
+                       {
+                               if (is_array($this->grants))
+                               {
+                                       $grants = $this->grants;
+                                       while (list($user) = each($grants))
+                                       {
+                                               $public_user_list[] = $user;
+                                       }
+                                       reset($public_user_list);
+                                       $filtermethod .= " $where 
(fm_project.access='public' AND fm_project.user_id IN(" . 
implode(',',$public_user_list) . "))";
+                                       $where= 'AND';
+                               }
+                       }
+                       else
+                       {
+                               $filtermethod .= " $where 
fm_workorder.user_id=$filter ";
+                               $where= 'AND';
+                       }
+
+                       if ($start_date)
+                       {
+                               $filtermethod .= " $where 
fm_workorder.start_date >= $start_date AND fm_workorder.start_date <= $end_date 
";
+                               $where= 'AND';
+                       }
+
+                       if($query)
+                       {
+                               $query = str_replace(",",'.',$query);
+                               if(stristr($query, '.'))
+                               {
+                                       $query=explode(".",$query);
+                                       $querymethod = " $where 
(fm_project.loc1='" . $query[0] . "' AND fm_project.loc".$type_id."='" . 
$query[1] . "')";
+                               }
+                               else
+                               {
+                                       $query = ereg_replace("'",'',$query);
+                                       $query = ereg_replace('"','',$query);
+
+                                       $querymethod = " $where 
(fm_workorder.title $this->like '%$query%' or fm_workorder.descr $this->like 
'%$query%' or fm_project.address $this->like '%$query%' or 
fm_project.location_code $this->like '%$query%' or fm_workorder.id $this->like 
'%$query%')";
+                               }
+                               $where= 'AND';
+
+                       }
+
+                       if($search_vendor)
+                       {
+                               if((int)$search_vendor>0)
+                               {
+                                       $querymethod_vendor = " $where 
fm_workorder.vendor_id=" .(int)$search_vendor ;
+                               }
+                               else
+                               {
+                                       $querymethod_vendor = " $where  
fm_vendor.org_name $this->like '%$search_vendor%'";
+                               }
+                       }
+
+                       $sql .= " $filtermethod $querymethod 
$querymethod_vendor $group_method";
+//echo $sql;
+                       $this->db2->query($sql,__LINE__,__FILE__);
+                       $this->total_records = $this->db2->num_rows();
+                       if(!$allrows)
+                       {
+                               $this->db->limit_query($sql . 
$ordermethod,$start,__LINE__,__FILE__);
+                       }
+                       else
+                       {
+                               $this->db->query($sql . 
$ordermethod,__LINE__,__FILE__);
+                       }
+
+                       $count_cols_return=count($cols_return);
+                       $j=0;
+                       while ($this->db->next_record())
+                       {
+                               for ($i=0;$i<$count_cols_return;$i++)
+                               {
+                                       $workorder_list[$j][$cols_return[$i]] = 
$this->db->f($cols_return[$i]);
+                                       $workorder_list[$j]['grants'] = 
(int)$this->grants[$this->db->f('project_owner')];
+                               }
+
+                               $location_code= $this->db->f('location_code');
+                               $location = split('-',$location_code);
+                               $count_location =count($location);
+                               for ($m=0;$m<$count_location;$m++)
+                               {
+                                       $workorder_list[$j]['loc' . ($m+1)] = 
$location[$m];
+                                       
$workorder_list[$j]['query_location']['loc' . ($m+1)]=implode("-", 
array_slice($location, 0, ($m+1)));
+                               }
+
+                               $j++;
+                       }
+
+                       return $workorder_list;
+               }
+
+               function read_single($workorder_id)
+               {
+                       $sql = "SELECT fm_workorder.*, fm_chapter.descr as 
chapter ,fm_project.user_id from fm_workorder $this->join fm_project on 
fm_workorder.project_id=fm_project.id  $this->left_join fm_chapter on "
+                               . " fm_workorder.chapter_id = fm_chapter.id 
where fm_workorder.id=$workorder_id";
+
+                       $this->db->query($sql,__LINE__,__FILE__);
+
+                       if ($this->db->next_record())
+                       {
+                               $workorder['workorder_id']              = 
$this->db->f('id');
+                               $workorder['project_id']                = 
$this->db->f('project_id');
+                               $workorder['title']                             
= $this->db->f('title');
+                               $workorder['name']                              
= $this->db->f('name');
+                               $workorder['key_fetch']                 = 
$this->db->f('key_fetch');
+                               $workorder['key_deliver']               = 
$this->db->f('key_deliver');
+                               $workorder['key_responsible']   = 
$this->db->f('key_responsible');
+                               $workorder['charge_tenant']             = 
$this->db->f('charge_tenant');
+                               $workorder['descr']                             
= $this->db->f('descr');
+                               $workorder['status']                    = 
$this->db->f('status');
+                               $workorder['budget']                    = 
(int)$this->db->f('budget');
+                               $workorder['calculation']                       
= 
($this->db->f('calculation')*(1+$this->db->f('addition')/100))+$this->db->f('rig_addition');
+                               $workorder['b_account_id']                      
= (int)$this->db->f('account_id');
+                               $workorder['addition_percentage']       = 
(int)$this->db->f('addition');
+                               $workorder['addition_rs']                       
= (int)$this->db->f('rig_addition');
+                               $workorder['act_mtrl_cost']                     
= $this->db->f('act_mtrl_cost');
+                               $workorder['act_vendor_cost']           = 
$this->db->f('act_vendor_cost');
+                               $workorder['user_id']                           
= $this->db->f('user_id');
+                               $workorder['vendor_id']                 = 
$this->db->f('vendor_id');
+                               $workorder['coordinator']               = 
$this->db->f('coordinator');
+                               $workorder['access']                    = 
$this->db->f('access');
+                               $workorder['start_date']                = 
$this->db->f('start_date');
+                               $workorder['end_date']                  = 
$this->db->f('end_date');
+                               $workorder['cat_id']                    = 
$this->db->f('category');
+                               $workorder['chapter_id']                = 
$this->db->f('chapter_id');
+                               $workorder['chapter']                   = 
$this->db->f('chapter');
+                               $workorder['deviation']                 = 
$this->db->f('deviation');
+                               $workorder['grants']                            
= (int)$this->grants[$this->db->f('user_id')];
+                       }
+
+//_debug_array($workorder);
+                               return $workorder;
+               }
+
+
+               function project_budget_from_workorder($project_id = '')
+               {
+                       $this->db->query("select budget, id as workorder_id 
from fm_workorder where project_id='$project_id'");
+                       while ($this->db->next_record())
+                       {
+                               $budget[] = array(
+                                       'workorder_id'  => 
$this->db->f('workorder_id'),
+                                       'budget'                => 
sprintf("%01.2f",$this->db->f('budget'))
+                                       );
+                       }
+                       return $budget;
+               }
+
+               function branch_p_list($project_id = '')
+               {
+
+                       $this->db2->query("SELECT branch_id from 
fm_projectbranch WHERE project_id='$project_id' ",__LINE__,__FILE__);
+                       while ($this->db2->next_record())
+                       {
+                               $selected[] = array('branch_id' => 
$this->db2->f('branch_id'));
+                       }
+
+                       return $selected;
+               }
+
+               function increment_workorder_id()
+               {
+                       $this->db->query("update fm_idgenerator set value = 
value + 1 where name = 'workorder'");
+               }
+
+               function add($workorder)
+               {
+                       $historylog     = 
CreateObject($this->currentapp.'.historylog','workorder');
+                       $workorder['descr'] = 
$this->db->db_addslashes($workorder['descr']);
+                       $workorder['title'] = 
$this->db->db_addslashes($workorder['title']);
+
+                       $values= array(
+                               $workorder['workorder_id'],
+                               $workorder['project_id'],
+                               $workorder['title'],
+                               'public',
+                               time(),
+                               $workorder['start_date'],
+                               $workorder['end_date'],
+                               $workorder['status'],
+                               $workorder['descr'],
+                               $workorder['budget'],
+                               $workorder['b_account_id'],
+                               $workorder['addition_rs'],
+                               $workorder['addition_percentage'],
+                               $workorder['key_deliver'],
+                               $workorder['key_fetch'],
+                               $workorder['vendor_id'],
+                               $workorder['charge_tenant'],
+                               $this->account);
+
+                       $values = $this->bocommon->validate_db_insert($values);
+
+                       $this->db->transaction_begin();
+
+                       $this->db->query("INSERT INTO fm_workorder 
(id,project_id,title,access,entry_date,start_date,end_date,status,"
+                               . 
"descr,budget,account_id,rig_addition,addition,key_deliver,key_fetch,vendor_id,charge_tenant,user_id)
 "
+                               . "VALUES ( $values )",__LINE__,__FILE__);
+
+                       $this->db->query("INSERT INTO fm_orders (id,type) 
VALUES (" . $workorder['workorder_id'] . ",'workorder')");
+
+/*
+                       if($workorder['charge_tenant'])
+                       {
+                               $this->db->query("UPDATE fm_project set 
charge_tenant = 1 WHERE id =" . $workorder['project_id']);
+                       }
+*/
+                       if($this->db->transaction_commit())
+                       {
+                               $this->increment_workorder_id();
+                               
$historylog->add('SO',$workorder['workorder_id'],$workorder['status']);
+                               $receipt['message'][] = 
array('msg'=>lang('workorder %1 has been saved',$workorder['workorder_id']));
+                       }
+                       else
+                       {
+                               $receipt['error'][] = array('msg'=>lang('the 
workorder has not been saved'));
+                       }
+                       return $receipt;
+               }
+
+               function edit($workorder)
+               {
+                       $historylog     = 
CreateObject($this->currentapp.'.historylog','workorder');
+                       $workorder['descr'] = 
$this->db->db_addslashes($workorder['descr']);
+                       $workorder['title'] = 
$this->db->db_addslashes($workorder['title']);
+
+                       $value_set=array(
+                               'title'                 => $workorder['title'],
+                               'status'                => $workorder['status'],
+                               'start_date'    => $workorder['start_date'],
+                               'end_date'              => 
$workorder['end_date'],
+                               'descr'                 => $workorder['descr'],
+                               'budget'                => 
(int)$workorder['budget'],
+                               'key_deliver'   => $workorder['key_deliver'],
+                               'key_fetch'             => 
$workorder['key_fetch'],
+                               'account_id'    => $workorder['b_account_id'],
+                               'rig_addition'  => $workorder['addition_rs'],
+                               'addition'              => 
$workorder['addition_percentage'],
+                               'charge_tenant' => $workorder['charge_tenant'],
+                               'vendor_id'             => 
$workorder['vendor_id']
+                               );
+
+                       $value_set      = 
$this->bocommon->validate_db_update($value_set);
+
+                       $this->db->query("SELECT status,budget FROM 
fm_workorder where id='" .$workorder['workorder_id']."'",__LINE__,__FILE__);
+                       $this->db->next_record();
+
+                       $old_status = $this->db->f('status');
+                       $old_budget = $this->db->f('budget');
+
+                       $this->db->transaction_begin();
+
+                       $this->db->query("UPDATE fm_workorder set $value_set 
WHERE id=" . $workorder['workorder_id'] ,__LINE__,__FILE__);
+
+/*                     if($workorder['charge_tenant'])
+                       {
+                               $this->db->query("UPDATE fm_project set 
charge_tenant = 1 WHERE id =" . $workorder['project_id']);
+                       }
+*/
+                       if($this->db->transaction_commit())
+                       {
+                               if ($old_status != $workorder['status'])
+                               {
+                                       
$historylog->add('S',$workorder['workorder_id'],$workorder['status']);
+                               }
+                               if ($old_budget != $workorder['budget'])
+                               {
+                                       
$historylog->add('B',$workorder['workorder_id'],$workorder['budget']);
+                               }
+
+                               $receipt['message'][] = 
array('msg'=>lang('workorder %1 has been edited',$workorder['workorder_id']));
+                       }
+                       else
+                       {
+                               $receipt['error'][] = 
array('msg'=>lang('workorder %1 has not been 
edited',$workorder['workorder_id']));
+                       }
+
+                       return $receipt;
+               }
+
+               function delete($workorder_id )
+               {
+                       $this->db->transaction_begin();
+                       $this->db->query("DELETE FROM fm_workorder WHERE id='" 
. $workorder_id . "'",__LINE__,__FILE__);
+                       $this->db->query("DELETE FROM fm_workorder_history  
WHERE  history_record_id='" . $workorder_id   . "'",__LINE__,__FILE__);
+                       $this->db->query("DELETE FROM fm_wo_hours WHERE 
workorder_id='" . $workorder_id   . "'",__LINE__,__FILE__);
+                       $this->db->query("DELETE FROM fm_orders WHERE id='" . 
$workorder_id . "'",__LINE__,__FILE__);
+                       $this->db->transaction_commit();
+
+               }
+       }
+?>
Index: property/inc/class.uiadmin_entity.inc.php
diff -u property/inc/class.uiadmin_entity.inc.php:1.12 
property/inc/class.uiadmin_entity.inc.php:1.13
--- property/inc/class.uiadmin_entity.inc.php:1.12      Fri Jan 27 14:05:43 2006
+++ property/inc/class.uiadmin_entity.inc.php   Mon Jan 30 22:14:19 2006
@@ -1,1495 +1,1497 @@
-<?php

-       /**

-       * phpGroupWare - property: a Facilities Management System.

-       *

-       * @author Sigurd Nes <address@hidden>

-       * @copyright Copyright (C) 2003-2005 Free Software Foundation, Inc. 
http://www.fsf.org/

-       * @license http://www.gnu.org/licenses/gpl.html GNU General Public 
License

-       * @internal Development of this application was funded by 
http://www.bergen.kommune.no/bbb_/ekstern/

-       * @package property

-       * @subpackage admin

-       * @version $Id: class.uiadmin_entity.inc.php,v 1.12 2006/01/27 14:05:43 
sigurdne Exp $

-       */

-

-       /**

-        * Description

-        * @package property

-        */

-

-       class uiadmin_entity

-       {

-               var $grants;

-               var $start;

-               var $query;

-               var $sort;

-               var $order;

-               var $sub;

-               var $currentapp;

-

-               var $public_functions = array

-               (

-                       'index'                         => True,

-                       'list_status'                   => True,

-                       'category'                      => True,

-                       'edit'                          => True,

-                       'edit_status'           => True,

-                       'edit_category'         => True,

-                       'view'                          => True,

-                       'delete'                        => True,

-                       'list_attribute'        => True,

-                       'edit_attrib'           => True,

-                       'list_custom_function'=>True,

-                       'edit_custom_function'  => True

-               );

-

-               function uiadmin_entity()

-               {

-                       $GLOBALS['phpgw_info']['flags']['xslt_app'] = True;

-                       $this->currentapp                       = 
$GLOBALS['phpgw_info']['flags']['currentapp'];

-                       $this->nextmatchs                       = 
CreateObject('phpgwapi.nextmatchs');

-                       $this->account                          = 
$GLOBALS['phpgw_info']['user']['account_id'];

-                       $this->bo                                       = 
CreateObject($this->currentapp.'.boadmin_entity',True);

-                       $this->bocommon                         = 
CreateObject($this->currentapp.'.bocommon');

-                       $this->menu                                     = 
CreateObject($this->currentapp.'.menu');

-

-                       $this->start                            = 
$this->bo->start;

-                       $this->query                            = 
$this->bo->query;

-                       $this->sort                                     = 
$this->bo->sort;

-                       $this->order                            = 
$this->bo->order;

-                       $this->entity_id                        = 
$this->bo->entity_id;

-                       $this->cat_id                           = 
$this->bo->cat_id;

-                       $this->allrows                          = 
$this->bo->allrows;

-

-                       $this->menu->sub                        ='admin_entity';

-               }

-

-               function save_sessiondata()

-               {

-                       $data = array

-                       (

-                               'start'         => $this->start,

-                               'query'         => $this->query,

-                               'sort'          => $this->sort,

-                               'order'         => $this->order,

-                               'allrows'       => $this->allrows,

-                               'entity_id'     => $this->entity_id,

-                               'cat_id'        => $this->cat_id

-                       );

-                       $this->bo->save_sessiondata($data);

-               }

-

-               function index()

-               {

-                       $this->bo->reset_fm_cache();

-                       $GLOBALS['phpgw']->xslttpl->add_file(array(

-                                                                               
'admin_entity',

-                                                                               
'nextmatchs',

-                                                                               
'menu',

-                                                                               
'search_field'));

-                       $links = $this->menu->links();

-

-                       $entity_list = $this->bo->read();

-

-                       if (isSet($entity_list) AND is_array($entity_list))

-                       {

-                               foreach($entity_list as $entry)

-                               {

-                                       $content[] = array

-                                       (

-                                               'id'                            
                => $entry['id'],

-                                               'name'                          
                => $entry['name'],

-                                               'descr'                         
                => $entry['descr'],

-                                               'link_categories'               
        => 
$GLOBALS['phpgw']->link('/index.php','menuaction='.$this->currentapp.'.uiadmin_entity.category&entity_id='
 . $entry['id']),

-                                               'link_edit'                     
                => 
$GLOBALS['phpgw']->link('/index.php','menuaction='.$this->currentapp.'.uiadmin_entity.edit&id='
 . $entry['id']),

-                                               'link_delete'                   
        => 
$GLOBALS['phpgw']->link('/index.php','menuaction='.$this->currentapp.'.uiadmin_entity.delete&entity_id='
 . $entry['id']),

-                                               'lang_view_standardtext'        
=> lang('view the standard'),

-                                               'lang_category_text'            
=> lang('categories for the entity type'),

-                                               'lang_edit_standardtext'        
=> lang('edit the entity'),

-                                               'lang_delete_standardtext'      
=> lang('delete the entity'),

-                                               'text_categories'               
        => lang('Categories'),

-                                               'text_edit'                     
                => lang('edit'),

-                                               'text_delete'                   
        => lang('delete')

-                                       );

-                               }

-                       }

-

-//_debug_array($content);

-

-                       $table_header[] = array

-                       (

-

-                               'lang_descr'            => lang('Descr'),

-                               'lang_categories'       => lang('Categories'),

-                               'lang_edit'                     => lang('edit'),

-                               'lang_delete'           => lang('delete'),

-                               'sort_id'       => 
$this->nextmatchs->show_sort_order(array

-                                                                               
(

-                                                                               
        'sort'  => $this->sort,

-                                                                               
        'var'   =>      'id',

-                                                                               
        'order' =>      $this->order,

-                                                                               
        'extra'         => array('menuaction'   => 
$this->currentapp.'.uiadmin_entity.index',

-                                                                               
                                                'allrows'=>$this->allrows)

-                                                                               
)),

-                               'lang_id'       => lang('entity id'),

-                               'sort_name'     => 
$this->nextmatchs->show_sort_order(array

-                                                                               
(

-                                                                               
        'sort'  => $this->sort,

-                                                                               
        'var'   =>      'name',

-                                                                               
        'order' =>      $this->order,

-                                                                               
        'extra'         => array('menuaction'   => 
$this->currentapp.'.uiadmin_entity.index',

-                                                                               
                                                'allrows'=>$this->allrows)

-                                                                               
)),

-                               'lang_name'     => lang('Name'),

-                       );

-

-                       $table_add[] = array

-                       (

-                               'lang_add'                              => 
lang('add'),

-                               'lang_add_standardtext' => lang('add a 
standard'),

-                               'add_action'                    => 
$GLOBALS['phpgw']->link('/index.php','menuaction='.$this->currentapp.'.uiadmin_entity.edit'),

-                               'lang_done'                             => 
lang('done'),

-                               'lang_done_standardtext'        => lang('back 
to admin'),

-                               'done_action'                   => 
$GLOBALS['phpgw']->link('/admin/index.php')

-                       );

-

-                       if(!$this->allrows)

-                       {

-                               $record_limit   = 
$GLOBALS['phpgw_info']['user']['preferences']['common']['maxmatchs'];

-                       }

-                       else

-                       {

-                               $record_limit   = $this->bo->total_records;

-                       }

-

-                       $link_data = array

-                       (

-                               'menuaction'    => 
$this->currentapp.'.uiadmin_entity.index',

-                                               'sort'                  
=>$this->sort,

-                                               'order'                 
=>$this->order,

-                                               'query'                 
=>$this->query

-                       );

-

-                       $data = array

-                       (

-                               'links'                                         
        => $links,

-                               'allow_allrows'                                 
=> True,

-                               'allrows'                                       
        => $this->allrows,

-                               'start_record'                                  
=> $this->start,

-                               'record_limit'                                  
=> $record_limit,

-                               'num_records'                                   
=> count($entity_list),

-                               'all_records'                                   
=> $this->bo->total_records,

-                               'link_url'                                      
        => $GLOBALS['phpgw']->link('/index.php',$link_data),

-                               'img_path'                                      
        => $GLOBALS['phpgw']->common->get_image_path('phpgwapi','default'),

-                               'lang_searchfield_standardtext' => lang('Enter 
the search string. To show all entries, empty this field and press the SUBMIT 
button again'),

-                               'lang_searchbutton_standardtext'        => 
lang('Submit the search string'),

-                               'query'                                         
        => $this->query,

-                               'lang_search'                                   
=> lang('search'),

-                               'table_header'                                  
=> $table_header,

-                               'values'                                        
        => $content,

-                               'table_add'                                     
        => $table_add

-                       );

-

-                       $appname                                                
= lang('entity');

-                       $function_msg                                   = 
lang('list entity type');

-

-                       $GLOBALS['phpgw_info']['flags']['app_header'] = 
lang($this->currentapp) . ' - ' . $appname . ': ' . $function_msg;

-                       
$GLOBALS['phpgw']->xslttpl->set_var('phpgw',array('list' => $data));

-               //      $GLOBALS['phpgw']->xslttpl->pp();

-                       $this->save_sessiondata();

-               }

-

-               function list_status()

-               {

-                       $this->bo->reset_fm_cache();

-                       $GLOBALS['phpgw']->xslttpl->add_file(array(

-                                                                               
'admin_entity',

-                                                                               
'nextmatchs',

-                                                                               
'menu',

-                                                                               
'search_field'));

-                       $links = $this->menu->links();

-

-                       $list = $this->bo->read_status();

-

-                       if (isSet($list) AND is_array($list))

-                       {

-                               foreach($list as $entry)

-                               {

-                                       $content[] = array

-                                       (

-                                               'id'                            
                => $entry['id'],

-                                               'descr'                         
                => $entry['descr'],

-                                               'link_edit'                     
                => 
$GLOBALS['phpgw']->link('/index.php','menuaction='.$this->currentapp.'.uiadmin_entity.edit_status&entity_id='
 . $this->entity_id . '&cat_id=' . $this->cat_id . '&id=' . $entry['id']),

-                                               'link_delete'                   
        => 
$GLOBALS['phpgw']->link('/index.php','menuaction='.$this->currentapp.'.uiadmin_entity.delete&entity_id='
 . $this->entity_id . '&cat_id=' . $this->cat_id . '&status_id=' . 
$entry['id']),

-                                               'lang_edit_standardtext'        
=> lang('edit the entity'),

-                                               'lang_delete_standardtext'      
=> lang('delete the entity'),

-                                               'text_edit'                     
                => lang('edit'),

-                                               'text_delete'                   
        => lang('delete')

-                                       );

-                               }

-                       }

-

-//_debug_array($content);

-

-                       $table_header[] = array

-                       (

-

-                               'lang_descr'            => lang('Descr'),

-                               'lang_edit'                     => lang('edit'),

-                               'lang_delete'           => lang('delete'),

-                               'sort_id'       => 
$this->nextmatchs->show_sort_order(array

-                                                                               
(

-                                                                               
        'sort'  => $this->sort,

-                                                                               
        'var'   =>      'id',

-                                                                               
        'order' =>      $this->order,

-                                                                               
        'extra'         => array('menuaction'   => 
$this->currentapp.'.uiadmin_entity.list_status',

-                                                                               
                                                        'entity_id'     => 
$this->entity_id,

-                                                                               
                                                        'cat_id'        => 
$this->cat_id

-                                                                               
                                                        )

-                                                                               
)),

-                               'lang_id'       => lang('status'),

-                       );

-

-                       $table_add[] = array

-                       (

-                               'lang_add'                              => 
lang('add'),

-                               'lang_add_text' => lang('add a standard'),

-                               'add_action'                    => 
$GLOBALS['phpgw']->link('/index.php','menuaction='.$this->currentapp.'.uiadmin_entity.edit_status&entity_id='
 . $this->entity_id . '&cat_id=' . $this->cat_id),

-                               'lang_done'                             => 
lang('done'),

-                               'lang_done_text'        => lang('back to 
admin'),

-                               'done_action'                   => 
$GLOBALS['phpgw']->link('/index.php','menuaction='.$this->currentapp.'.uiadmin_entity.category&entity_id='
 . $this->entity_id . '&cat_id=' . $this->cat_id),

-                       );

-

-                       if(!$this->allrows)

-                       {

-                               $record_limit   = 
$GLOBALS['phpgw_info']['user']['preferences']['common']['maxmatchs'];

-                       }

-                       else

-                       {

-                               $record_limit   = $this->bo->total_records;

-                       }

-

-                       $link_data = array

-                       (

-                               'menuaction'    => 
$this->currentapp.'.uiadmin_entity.list_status',

-                                               'sort'                  
=>$this->sort,

-                                               'order'                 
=>$this->order,

-                                               'query'                 
=>$this->query,

-                                               'entity_id'             
=>$this->entity_id,

-                                               'cat_id'                
=>$this->cat_id

-                       );

-

-                       $entity = 
$this->bo->read_single($this->entity_id,false);

-                       $category = 
$this->bo->read_single_category($this->entity_id,$this->cat_id);

-

-                       $msgbox_data = $this->bocommon->msgbox_data($receipt);

-

-                       $data = array

-                       (

-                               'lang_entity'                                   
=> lang('entity'),

-                               'entity_name'                                   
=> $entity['name'],

-                               'lang_category'                                 
=> lang('category'),

-                               'category_name'                                 
=> $category['name'],

-                               'links'                                         
        => $links,

-                               'allow_allrows'                                 
=> True,

-                               'allrows'                                       
        => $this->allrows,

-                               'start_record'                                  
=> $this->start,

-                               'record_limit'                                  
=> $record_limit,

-                               'num_records'                                   
=> count($entity_list),

-                               'all_records'                                   
=> $this->bo->total_records,

-                               'link_url'                                      
        => $GLOBALS['phpgw']->link('/index.php',$link_data),

-                               'img_path'                                      
        => $GLOBALS['phpgw']->common->get_image_path('phpgwapi','default'),

-                               'lang_searchfield_standardtext' => lang('Enter 
the search string. To show all entries, empty this field and press the SUBMIT 
button again'),

-                               'lang_searchbutton_standardtext'        => 
lang('Submit the search string'),

-                               'query'                                         
        => $this->query,

-                               'lang_search'                                   
=> lang('search'),

-                               'table_header_status'                   => 
$table_header,

-                               'values_status'                                 
=> $content,

-                               'table_add'                                     
        => $table_add

-                       );

-

-                       $appname                                                
= lang('entity');

-                       $function_msg                                   = 
lang('list status');

-

-                       $GLOBALS['phpgw_info']['flags']['app_header'] = 
lang($this->currentapp) . ' - ' . $appname . ': ' . $function_msg;

-                       
$GLOBALS['phpgw']->xslttpl->set_var('phpgw',array('list_status' => $data));

-               //      $GLOBALS['phpgw']->xslttpl->pp();

-                       $this->save_sessiondata();

-               }

-

-               function category()

-               {

-

-                       $entity_id      = 
get_var('entity_id',array('POST','GET'));

-

-                       $GLOBALS['phpgw']->xslttpl->add_file(array(

-                                                                               
'admin_entity',

-                                                                               
'nextmatchs',

-                                                                               
'menu',

-                                                                               
'search_field'));

-                       $links = $this->menu->links();

-

-                       $category_list = $this->bo->read_category($entity_id);

-

-                       if (isSet($category_list) AND is_array($category_list))

-                       {

-                               foreach($category_list as $entry)

-                               {

-                                       $content[] = array

-                                       (

-                                               'id'                            
                => $entry['id'],

-                                               'name'                          
                => $entry['name'],

-                                               'prefix'                        
                => $entry['prefix'],

-                                               'descr'                         
                => $entry['descr'],

-//                                             'link_status'                   
        => 
$GLOBALS['phpgw']->link('/index.php','menuaction='.$this->currentapp.'.uiadmin_entity.list_status&cat_id='
 . $entry['id'] . '&entity_id=' . $entity_id),

-                                               'link_custom_function'          
        => 
$GLOBALS['phpgw']->link('/index.php','menuaction='.$this->currentapp.'.uiadmin_entity.list_custom_function&cat_id='
 . $entry['id'] . '&entity_id=' . $entity_id),

-                                               'link_attribute'                
        => 
$GLOBALS['phpgw']->link('/index.php','menuaction='.$this->currentapp.'.uiadmin_entity.list_attribute&cat_id='
 . $entry['id'] . '&entity_id=' . $entity_id),

-                                               'link_edit'                     
                => 
$GLOBALS['phpgw']->link('/index.php','menuaction='.$this->currentapp.'.uiadmin_entity.edit_category&id='
 . $entry['id'] . '&entity_id=' . $entity_id),

-                                               'link_delete'                   
        => 
$GLOBALS['phpgw']->link('/index.php','menuaction='.$this->currentapp.'.uiadmin_entity.delete&cat_id='
 . $entry['id'] . '&entity_id=' . $entity_id),

-                                               'lang_view_standardtext'        
=> lang('view the category'),

-                                               'lang_status_standardtext'      
=> lang('Status for the entity category'),

-                                               'lang_attribute_standardtext'   
=> lang('attributes for the entity category'),

-                                               
'lang_custom_function_standardtext'     => lang('custom functions for the 
entity category'),

-                                               'lang_edit_standardtext'        
=> lang('edit the standard'),

-                                               'lang_delete_standardtext'      
=> lang('delete the standard'),

-                                               'text_status'                   
=> lang('Status'),

-                                               'text_attribute'                
        => lang('Attributes'),

-                                               'text_custom_function'          
        => lang('Custom functions'),

-                                               'text_edit'                     
                => lang('edit'),

-                                               'text_delete'                   
        => lang('delete')

-                                       );

-                               }

-                       }

-

-//_debug_array($content);

-

-                       $table_header[] = array

-                       (

-

-                               'lang_descr'            => lang('Descr'),

-                               'lang_prefix'           => lang('prefix'),

-//                             'lang_status'           => lang('Status'),

-                               'lang_attribute'        => lang('Attributes'),

-                               'lang_custom_function'  => lang('custom 
functions'),

-                               'lang_edit'                     => lang('edit'),

-                               'lang_delete'           => lang('delete'),

-                               'sort_id'       => 
$this->nextmatchs->show_sort_order(array

-                                                                               
(

-                                                                               
        'sort'  => $this->sort,

-                                                                               
        'var'   =>      'id',

-                                                                               
        'order' =>      $this->order,

-                                                                               
        'extra'         => array('menuaction'   => 
$this->currentapp.'.uiadmin_entity.category',

-                                                                               
                                                'entity_id' =>$entity_id,

-                                                                               
                                                'allrows'=>$this->allrows)

-                                                                               
)),

-                               'lang_id'       => lang('category id'),

-                               'sort_name'     => 
$this->nextmatchs->show_sort_order(array

-                                                                               
(

-                                                                               
        'sort'  => $this->sort,

-                                                                               
        'var'   =>      'name',

-                                                                               
        'order' =>      $this->order,

-                                                                               
        'extra'         => array('menuaction'   => 
$this->currentapp.'.uiadmin_entity.category',

-                                                                               
                                                'entity_id' =>$entity_id,

-                                                                               
                                                'allrows'=>$this->allrows)

-                                                                               
)),

-                               'lang_name'     => lang('Name'),

-                       );

-

-                       $table_add[] = array

-                       (

-                               'lang_add'                              => 
lang('add'),

-                               'lang_add_standardtext' => lang('add a 
category'),

-                               'add_action'                    => 
$GLOBALS['phpgw']->link('/index.php','menuaction='.$this->currentapp.'.uiadmin_entity.edit_category&entity_id='
 . $entity_id),

-                               'lang_done'                             => 
lang('done'),

-                               'lang_done_standardtext'        => lang('back 
to entity'),

-                               'done_action'                   => 
$GLOBALS['phpgw']->link('/index.php','menuaction='.$this->currentapp.'.uiadmin_entity.index')

-                       );

-

-                       $entity = $this->bo->read_single($entity_id,false);

-

-                       if(!$this->allrows)

-                       {

-                               $record_limit   = 
$GLOBALS['phpgw_info']['user']['preferences']['common']['maxmatchs'];

-                       }

-                       else

-                       {

-                               $record_limit   = $this->bo->total_records;

-                       }

-

-                       $link_data = array

-                       (

-                               'menuaction'    => 
$this->currentapp.'.uiadmin_entity.category',

-                                               'sort'                  
=>$this->sort,

-                                               'order'                 
=>$this->order,

-                                               'query'                 
=>$this->query,

-                                               'entity_id'             
=>$entity_id

-                       );

-

-                       $data = array

-                       (

-                               'lang_entity'                                   
=> lang('entity'),

-                               'entity_name'                                   
=> $entity['name'],

-                               'links'                                         
        => $links,

-                               'allow_allrows'                                 
=> True,

-                               'allrows'                                       
        => $this->allrows,

-                               'start_record'                                  
=> $this->start,

-                               'record_limit'                                  
=> $record_limit,

-                               'num_records'                                   
=> count($category_list),

-                               'all_records'                                   
=> $this->bo->total_records,

-                               'link_url'                                      
        => $GLOBALS['phpgw']->link('/index.php',$link_data),

-                               'img_path'                                      
        => $GLOBALS['phpgw']->common->get_image_path('phpgwapi','default'),

-                               'lang_searchfield_standardtext' => lang('Enter 
the search string. To show all entries, empty this field and press the SUBMIT 
button again'),

-                               'lang_searchbutton_standardtext'        => 
lang('Submit the search string'),

-                               'query'                                         
        => $this->query,

-                               'lang_search'                                   
=> lang('search'),

-                               'table_header_category'                 => 
$table_header,

-                               'values_category'                               
=> $content,

-                               'table_add'                                     
        => $table_add

-                       );

-

-                       $appname                                                
= lang('entity');

-                       $function_msg                                   = 
lang('list entity type');

-

-                       $GLOBALS['phpgw_info']['flags']['app_header'] = 
lang($this->currentapp) . ' - ' . $appname . ': ' . $function_msg;

-                       
$GLOBALS['phpgw']->xslttpl->set_var('phpgw',array('list_category' => $data));

-               //      $GLOBALS['phpgw']->xslttpl->pp();

-                       $this->save_sessiondata();

-               }

-

-               function edit()

-               {

-                       $id     = get_var('id',array('POST','GET'));

-                       $values                 = 
get_var('values',array('POST'));

-                       $config = 
CreateObject('phpgwapi.config',$this->currentapp);

-

-                       
$GLOBALS['phpgw']->xslttpl->add_file(array('admin_entity'));

-

-                       if ($values['save'])

-                       {

-                               if (!$values['name'])

-                               {

-                                       $receipt['error'][] = 
array('msg'=>lang('Name not entered!'));

-                               }

-

-                               if($id)

-                               {

-                                       $values['id']=$id;

-                                       $action='edit';

-                               }

-

-                               if (!$receipt['error'])

-                               {

-

-                                       $receipt = 
$this->bo->save($values,$action);

-                                       if(!$id)

-                                       {

-                                               $id=$receipt['id'];

-                                       }

-                                       $config->read_repository();

-

-                                       
if(!is_array($config->config_data['location_form']))

-                                       {

-                                               
$config->config_data['location_form'] = array();

-                                       }

-

-                                       if($values['location_form'])

-                                       {

-

-                                               
$config->config_data['location_form']['entity_' . $id] = 'entity_' . $id;

-

-                                       }

-                                       else

-                                       {

-                                               
unset($config->config_data['location_form']['entity_' . $id]);

-                                       }

-

-                                       $config->save_repository();

-                               }

-                               else

-                               {

-                                       $receipt['error'][] = array('msg'=> 
lang('Entity has NOT been saved'));

-                               }

-

-                       }

-

-

-                       if ($id)

-                       {

-                               $values = $this->bo->read_single($id);

-                               $function_msg = lang('edit standard');

-                               $action='edit';

-                       }

-                       else

-                       {

-                               $function_msg = lang('add entity');

-                               $action='add';

-                       }

-

-                       $include_list   = 
$this->bo->get_entity_list($values['lookup_entity']);

-                       $include_list_2 = 
$this->bo->get_entity_list_2($values['include_entity_for']);

-                       $include_list_3 = 
$this->bo->get_entity_list_3($values['start_entity_from']);

-

-                       $link_data = array

-                       (

-                               'menuaction'    => 
$this->currentapp.'.uiadmin_entity.edit',

-                               'id'    => $id

-                       );

-//_debug_array($include_list);

-

-                       $msgbox_data = $this->bocommon->msgbox_data($receipt);

-

-                       $data = array

-                       (

-                               'msgbox_data'                                   
=> $GLOBALS['phpgw']->common->msgbox($msgbox_data),

-                               'lang_name_standardtext'                => 
lang('Enter a name of the standard'),

-                               'form_action'                                   
=> $GLOBALS['phpgw']->link('/index.php',$link_data),

-                               'done_action'                                   
=> 
$GLOBALS['phpgw']->link('/index.php','menuaction='.$this->currentapp.'.uiadmin_entity.index'),

-                               'lang_id'                                       
        => lang('standard ID'),

-                               'lang_name'                                     
        => lang('Name'),

-                               'lang_descr'                                    
=> lang('Descr'),

-                               'lang_save'                                     
        => lang('save'),

-                               'lang_done'                                     
        => lang('done'),

-                               'value_id'                                      
        => $id,

-                               'value_name'                                    
=> $values['name'],

-                               'lang_id_standardtext'                  => 
lang('Enter the standard ID'),

-                               'lang_descr_standardtext'               => 
lang('Enter a description of the standard'),

-                               'lang_done_standardtext'                => 
lang('Back to the list'),

-                               'lang_save_standardtext'                => 
lang('Save the standard'),

-                               'type_id'                                       
        => $values['type_id'],

-                               'value_descr'                                   
=> $values['descr'],

-                               'lang_location_form'                    => 
lang('location form'),

-                               'value_location_form'                   => 
$values['location_form'],

-                               'lang_location_form_statustext' => lang('If 
this entity type is to be linked to a location'),

-                               'lang_include_in_location_form' => 
lang('include in location form'),

-                               'include_list'                                  
=> $include_list,

-                               'lang_include_statustext'               => 
lang('Which entity type is to show up in location forms'),

-                               'lang_include_this_entity'              => 
lang('include this entity'),

-                               'include_list_2'                                
=> $include_list_2,

-                               'lang_include_2_statustext'             => 
lang('Let this entity show up in location form'),

-                               'lang_start_this_entity'                => 
lang('start this entity'),

-                               'include_list_3'                                
=> $include_list_3,

-                               'lang_include_3_statustext'             => 
lang('Start this entity from'),

-                               'lang_select'                                   
=> lang('select'),

-                               'lang_documentation'                    => 
lang('documentation'),

-                               'value_documentation'                   => 
$values['documentation'],

-                               'lang_documentation_statustext' => lang('If 
this entity type is to be linked to documents'),

-                       );

-

-                       $appname                                                
= lang('entity');

-

-                       $GLOBALS['phpgw_info']['flags']['app_header'] = 
lang($this->currentapp) . ' - ' . $appname . ': ' . $function_msg;

-                       
$GLOBALS['phpgw']->xslttpl->set_var('phpgw',array('edit' => $data));

-               //      $GLOBALS['phpgw']->xslttpl->pp();

-               }

-

-               function edit_status()

-               {

-                       $id     = get_var('id',array('POST','GET'));

-                       $values                 = 
get_var('values',array('POST'));

-

-                       
$GLOBALS['phpgw']->xslttpl->add_file(array('admin_entity'));

-

-                       if ($values['save'])

-                       {

-                               if (!$values['id'])

-                               {

-                                       $receipt['error'][] = 
array('msg'=>lang('ID not entered!'));

-                               }

-

-                               if($id)

-                               {

-                                       $values['id']=$id;

-                                       $action='edit';

-                               }

-

-                               if (!$receipt['error'])

-                               {

-                                       $receipt = 
$this->bo->save_status($values,$action);

-                                       if(!$id)

-                                       {

-                                               $id=$receipt['id'];

-                                       }

-                               }

-                               else

-                               {

-                                       $receipt['error'][] = array('msg'=> 
lang('Status has NOT been saved'));

-                               }

-

-                       }

-

-                       if ($id)

-                       {

-                               $values = $this->bo->read_single_status($id);

-                               $function_msg = lang('edit status');

-                               $action='edit';

-                       }

-                       else

-                       {

-                               $function_msg = lang('add status');

-                               $action='add';

-                       }

-

-

-                       $link_data = array

-                       (

-                               'menuaction'    => 
$this->currentapp.'.uiadmin_entity.edit_status',

-                               'entity_id'             => $this->entity_id,

-                               'cat_id'                => $this->cat_id,

-                               'id'                    => $id

-                       );

-//_debug_array($link_data);

-

-                       $entity = 
$this->bo->read_single($this->entity_id,false);

-                       $category = 
$this->bo->read_single_category($this->entity_id,$this->cat_id);

-

-                       $msgbox_data = $this->bocommon->msgbox_data($receipt);

-

-                       $data = array

-                       (

-                               'lang_entity'                                   
=> lang('entity'),

-                               'entity_name'                                   
=> $entity['name'],

-                               'lang_category'                                 
=> lang('category'),

-                               'category_name'                                 
=> $category['name'],

-                               'msgbox_data'                                   
=> $GLOBALS['phpgw']->common->msgbox($msgbox_data),

-                               'lang_name_standardtext'                => 
lang('Enter a name of the standard'),

-                               'form_action'                                   
=> $GLOBALS['phpgw']->link('/index.php',$link_data),

-                               'done_action'                                   
=> 
$GLOBALS['phpgw']->link('/index.php','menuaction='.$this->currentapp.'.uiadmin_entity.list_status&entity_id='
 . $this->entity_id . '&cat_id=' . $this->cat_id),

-                               'lang_id'                                       
        => lang('status ID'),

-                               'lang_descr'                                    
=> lang('Descr'),

-                               'lang_save'                                     
        => lang('save'),

-                               'lang_done'                                     
        => lang('done'),

-                               'value_id'                                      
        => $id,

-                               'lang_id_standardtext'                  => 
lang('Enter the status ID'),

-                               'lang_descr_standardtext'               => 
lang('Enter a description of the status'),

-                               'lang_done_standardtext'                => 
lang('Back to the list'),

-                               'lang_save_standardtext'                => 
lang('Save the status'),

-                               'value_descr'                                   
=> $values['descr']

-                       );

-

-                       $appname                                                
= lang('entity');

-

-                       $GLOBALS['phpgw_info']['flags']['app_header'] = 
lang($this->currentapp) . ' - ' . $appname . ': ' . $function_msg;

-                       
$GLOBALS['phpgw']->xslttpl->set_var('phpgw',array('edit_status' => $data));

-               //      $GLOBALS['phpgw']->xslttpl->pp();

-               }

-

-               function edit_category()

-               {

-                       $entity_id      = 
get_var('entity_id',array('POST','GET'));

-                       $id     = get_var('id',array('POST','GET'));

-                       $values                 = 
get_var('values',array('POST'));

-

-                       
$GLOBALS['phpgw']->xslttpl->add_file(array('admin_entity'));

-

-                       if ($values['save'])

-                       {

-                               $values['entity_id']    = $entity_id;

-

-                               if (!$values['name'])

-                               {

-                                       $receipt['error'][] = 
array('msg'=>lang('Name not entered!'));

-                               }

-                               if (!$values['entity_id'])

-                               {

-                                       $receipt['error'][] = 
array('msg'=>lang('Entity not choosen'));

-                               }

-

-                               if($id)

-                               {

-                                       $values['id']=$id;

-                                       $action='edit';

-                               }

-

-                               if (!$receipt['error'])

-                               {

-                                       $receipt = 
$this->bo->save_category($values,$action);

-                                       if(!$id)

-                                       {

-                                               $id=$receipt['id'];

-                                       }

-                               }

-                               else

-                               {

-                                       $receipt['error'][] = array('msg'=> 
lang('Category has NOT been saved'));

-                               }

-

-                       }

-

-                       if ($id)

-                       {

-                               $values = 
$this->bo->read_single_category($entity_id,$id);

-                               $function_msg = lang('edit category');

-                               $action='edit';

-                       }

-                       else

-                       {

-                               $function_msg = lang('add category');

-                               $action='add';

-                       }

-

-

-                       $link_data = array

-                       (

-                               'menuaction'    => 
$this->currentapp.'.uiadmin_entity.edit_category',

-                               'entity_id' =>$entity_id,

-                               'id'    => $id

-                       );

-//_debug_array($link_data);

-

-                       $entity = $this->bo->read_single($entity_id,false);

-

-                       $msgbox_data = $this->bocommon->msgbox_data($receipt);

-

-                       $data = array

-                       (

-                               'lang_entity'                                   
=> lang('entity'),

-                               'entity_name'                                   
=> $entity['name'],

-                               'msgbox_data'                                   
=> $GLOBALS['phpgw']->common->msgbox($msgbox_data),

-                               'lang_prefix_standardtext'              => 
lang('Enter a standard prefix for the id'),

-                               'lang_name_standardtext'                => 
lang('Enter a name of the standard'),

-

-                               'form_action'                                   
=> $GLOBALS['phpgw']->link('/index.php',$link_data),

-                               'done_action'                                   
=> 
$GLOBALS['phpgw']->link('/index.php','menuaction='.$this->currentapp.'.uiadmin_entity.category&entity_id='
 . $entity_id),

-                               'lang_id'                                       
        => lang('Category'),

-                               'lang_name'                                     
        => lang('Name'),

-                               'lang_descr'                                    
=> lang('Descr'),

-                               'lang_prefix'                                   
=> lang('Prefix'),

-                               'lang_save'                                     
        => lang('save'),

-                               'lang_done'                                     
        => lang('done'),

-                               'value_id'                                      
        => $id,

-                               'value_name'                                    
=> $values['name'],

-                               'value_prefix'                                  
=> $values['prefix'],

-                               'lang_id_standardtext'                  => 
lang('Enter the standard ID'),

-                               'lang_descr_standardtext'               => 
lang('Enter a description of the standard'),

-                               'lang_done_standardtext'                => 
lang('Back to the list'),

-                               'lang_save_standardtext'                => 
lang('Save the standard'),

-                               'type_id'                                       
        => $values['type_id'],

-                               'value_descr'                                   
=> $values['descr'],

-                               'lang_lookup_tenant'                    => 
lang('lookup tenant'),

-                               'value_lookup_tenant'                   => 
$values['lookup_tenant'],

-                               'lang_lookup_tenant_statustext' => lang('If 
this entity type is to look up tenants'),

-                               'lang_location_level'                   => 
lang('location level'),

-                               'location_level_list'                   => 
$this->bo->get_location_level_list($values['location_level']),

-                               'lang_location_level_statustext'        => 
lang('select location level'),

-                               'lang_no_location_level'                => 
lang('None'),

-                               'lang_tracking'                                 
=> lang('tracking'),

-                               'value_tracking'                                
=> $values['tracking'],

-                               'lang_tracking_statustext'              => 
lang('If this entity type is to be tracket in ticket list'),

-                               'lang_fileupload'                               
=> lang('Enable file upload'),

-                               'value_fileupload'                              
=> $values['fileupload'],

-                               'lang_fileupload_statustext'    => lang('If 
files can be uploaded for this category'),

-                               'lang_loc_link'                                 
=> lang('Link from location'),

-                               'value_loc_link'                                
=> $values['loc_link'],

-                               'lang_loc_link_statustext'              => 
lang('Enable link from location detail'),

-                               'lang_start_project'                    => 
lang('Start project'),

-                               'value_start_project'                   => 
$values['start_project'],

-                               'lang_start_project_statustext' => lang('Enable 
start project from this category')

-                       );

-

-                       $appname                                                
= lang('entity');

-

-                       $GLOBALS['phpgw_info']['flags']['app_header'] = 
lang($this->currentapp) . ' - ' . $appname . ': ' . $function_msg;

-                       
$GLOBALS['phpgw']->xslttpl->set_var('phpgw',array('edit' => $data));

-               //      $GLOBALS['phpgw']->xslttpl->pp();

-               }

-

-               function delete()

-               {

-                       $entity_id              = 
get_var('entity_id',array('POST','GET'));

-                       $cat_id         = get_var('cat_id',array('POST','GET'));

-                       $attrib_id              = 
get_var('attrib_id',array('POST','GET'));

-                       $status_id              = 
get_var('status_id',array('POST','GET'));

-                       $acl_location           = 
get_var('acl_location',array('POST','GET'));

-                       $custom_function_id     = 
get_var('custom_function_id',array('POST','GET'));

-                       $confirm        = get_var('confirm',array('POST'));

-

-                       if($attrib_id):

-                       {

-                               $function='list_attribute';

-                       }

-                       elseif($custom_function_id):

-                       {

-                               $function='list_custom_function';

-                       }

-                       endif;

-

-                       if (!$acl_location && $entity_id && $cat_id)

-                       {

-                               $acl_location = '.entity.' . $entity_id . '.' . 
$cat_id;

-                       }

-                       

-                       if(!$function)

-                       {

-                               if($cat_id)

-                               {

-                                       if($status_id)

-                                       {

-                                               $function='list_status';

-                                       }

-                                       else

-                                       {

-                                               $function='category';

-                                       }

-                               }

-                               else

-                               {

-                                       $function='index';

-                               }

-                       }

-

-

-                       $link_data = array

-                       (

-                               'menuaction' => 
$this->currentapp.'.uiadmin_entity.'.$function,

-                               'cat_id' => $cat_id,

-                               'entity_id' => $entity_id,

-                               'attrib_id' => $attrib_id,

-                               'status_id' => $status_id

-                       );

-

-                       $delete_data = array

-                       (

-                               'menuaction' => 
$this->currentapp.'.uiadmin_entity.delete',

-                               'cat_id' => $cat_id,

-                               'entity_id' => $entity_id,

-                               'attrib_id' => $attrib_id,

-                               'status_id' => $status_id,

-                               'acl_location' => $acl_location,

-                               'custom_function_id' => $custom_function_id

-                       );

-

-                       if (get_var('confirm',array('POST')))

-                       {

-                               
$this->bo->delete($cat_id,$entity_id,$attrib_id,$status_id,$acl_location,$custom_function_id);

-                               Header('Location: ' . 
$GLOBALS['phpgw']->link('/index.php',$link_data));

-                       }

-

-                       
$GLOBALS['phpgw']->xslttpl->add_file(array('app_delete'));

-

-                       $data = array

-                       (

-                               'done_action'                   => 
$GLOBALS['phpgw']->link('/index.php',$link_data),

-                               'delete_action'                 => 
$GLOBALS['phpgw']->link('/index.php',$delete_data),

-                               'lang_confirm_msg'              => lang('do you 
really want to delete this entry'),

-                               'lang_yes'                              => 
lang('yes'),

-                               'lang_yes_standardtext' => lang('Delete the 
entry'),

-                               'lang_no_standardtext'  => lang('Back to the 
list'),

-                               'lang_no'                               => 
lang('no')

-                       );

-

-                       $appname                                                
= lang('entity');

-                       $function_msg                                   = 
lang('delete entity type');

-

-                       $GLOBALS['phpgw_info']['flags']['app_header'] = 
lang($this->currentapp) . ' - ' . $appname . ': ' . $function_msg;

-                       
$GLOBALS['phpgw']->xslttpl->set_var('phpgw',array('delete' => $data));

-               //      $GLOBALS['phpgw']->xslttpl->pp();

-               }

-

-

-               function list_attribute()

-               {

-                       $entity_id      = $this->entity_id;

-                       $cat_id = $this->cat_id;

-                       $id     = get_var('id',array('POST','GET'));

-                       $resort = get_var('resort',array('POST','GET'));

-

-                       $GLOBALS['phpgw']->xslttpl->add_file(array(

-                                                                               
'admin_entity',

-                                                                               
'nextmatchs',

-                                                                               
'search_field'));

-

-                       if($resort)

-                       {

-                               $this->bo->resort_attrib($id,$resort);

-                       }

-                       $attrib_list = 
$this->bo->read_attrib($entity_id,$cat_id);

-

-                       if (isset($attrib_list) AND is_array($attrib_list))

-                       {

-                               foreach($attrib_list as $entry)

-                               {

-

-                                       $content[] = array

-                                       (

-                                               'name'                          
                => $entry['name'],

-                                               'datatype'                      
                => $entry['datatype'],

-                                               'column_name'                   
        => $entry['column_name'],

-                                               'input_text'                    
        => $entry['input_text'],

-                                               'sorting'                       
                => $entry['attrib_sort'],

-                                               'search'                        
                => $entry['search'],

-                                               'link_up'                       
                => 
$GLOBALS['phpgw']->link('/index.php','menuaction='.$this->currentapp.'.uiadmin_entity.list_attribute&resort=up&entity_id='
 . $entity_id . '&cat_id=' . $cat_id . '&id=' . $entry['id'] . '&allrows=' . 
$this->allrows),

-                                               'link_down'                     
                => 
$GLOBALS['phpgw']->link('/index.php','menuaction='.$this->currentapp.'.uiadmin_entity.list_attribute&resort=down&entity_id='
 . $entity_id . '&cat_id=' . $cat_id . '&id=' . $entry['id'] . '&allrows=' . 
$this->allrows),

-                                               'link_edit'                     
                => 
$GLOBALS['phpgw']->link('/index.php','menuaction='.$this->currentapp.'.uiadmin_entity.edit_attrib&entity_id='
 . $entity_id . '&cat_id=' . $cat_id . '&id=' . $entry['id']),

-                                               'link_delete'                   
        => 
$GLOBALS['phpgw']->link('/index.php','menuaction='.$this->currentapp.'.uiadmin_entity.delete&entity_id='
 . $entity_id . '&cat_id=' . $cat_id . '&attrib_id=' . $entry['id']),

-                                               'lang_up_text'                  
        => lang('shift up'),

-                                               'lang_down_text'                
        => lang('shift down'),

-                                               'lang_edit_text'                
        => lang('edit the attrib'),

-                                               'lang_delete_text'              
        => lang('delete the attrib'),

-                                               'text_attribute'                
        => lang('Attributes'),

-                                               'text_up'                       
                => lang('up'),

-                                               'text_down'                     
                => lang('down'),

-                                               'text_edit'                     
                => lang('edit'),

-                                               'text_delete'                   
        => lang('delete')

-                                       );

-                               }

-                       }

-

-//_debug_array($content);

-

-                       $table_header[] = array

-                       (

-                               'lang_descr'            => lang('Descr'),

-                               'lang_datatype'         => lang('Datatype'),

-                               'lang_sorting'          => lang('sorting'),

-                               'lang_search'           => lang('search'),

-                               'lang_edit'                     => lang('edit'),

-                               'lang_delete'           => lang('delete'),

-                               'sort_name'     => 
$this->nextmatchs->show_sort_order(array

-                                                                               
(

-                                                                               
        'sort'  => $this->sort,

-                                                                               
        'var'   =>      'column_name',

-                                                                               
        'order' =>      $this->order,

-                                                                               
        'extra'         => array('menuaction'   => 
$this->currentapp.'.uiadmin_entity.list_attribute',

-                                                                               
                                                        'entity_id'     
=>$entity_id,

-                                                                               
                                                        'cat_id'        
=>$cat_id,

-                                                                               
                                                'allrows'=>$this->allrows)

-                                                                               
)),

-                               'sort_sorting'  => 
$this->nextmatchs->show_sort_order(array

-                                                                               
(

-                                                                               
        'sort'  => $this->sort,

-                                                                               
        'var'   =>      'attrib_sort',

-                                                                               
        'order' =>      $this->order,

-                                                                               
        'extra'         => array('menuaction'   => 
$this->currentapp.'.uiadmin_entity.list_attribute',

-                                                                               
                                                        'entity_id'     
=>$entity_id,

-                                                                               
                                                        'cat_id'        
=>$cat_id,

-                                                                               
                                                'allrows'=>$this->allrows)

-                                                                               
)),

-                               'lang_name'     => lang('Name'),

-                       );

-

-                       $table_add[] = array

-                       (

-                               'lang_add'                              => 
lang('add'),

-                               'lang_add_attribtext'   => lang('add a attrib'),

-                               'add_action'                    => 
$GLOBALS['phpgw']->link('/index.php','menuaction='.$this->currentapp.'.uiadmin_entity.edit_attrib&entity_id='.$entity_id
 . '&cat_id=' . $cat_id),

-                               'lang_done'                             => 
lang('done'),

-                               'lang_done_attribtext'  => lang('back to 
admin'),

-                               'done_action'                   => 
$GLOBALS['phpgw']->link('/index.php','menuaction='.$this->currentapp.'.uiadmin_entity.category&entity_id='.$entity_id),

-                       );

-

-                       if(!$this->allrows)

-                       {

-                               $record_limit   = 
$GLOBALS['phpgw_info']['user']['preferences']['common']['maxmatchs'];

-                       }

-                       else

-                       {

-                               $record_limit   = $this->bo->total_records;

-                       }

-

-                       $link_data = array

-                       (

-                               'menuaction'    => 
$this->currentapp.'.uiadmin_entity.list_attribute',

-                                               'sort'                  
=>$this->sort,

-                                               'order'                 
=>$this->order,

-                                               'query'                 
=>$this->query,

-                                               'entity_id'             
=>$entity_id,

-                                               'cat_id'                
=>$cat_id

-                       );

-

-                       $entity = $this->bo->read_single($entity_id,false);

-                       $category = 
$this->bo->read_single_category($entity_id,$cat_id);

-

-                       $data = array

-                       (

-                               'lang_entity'                                   
=> lang('entity'),

-                               'entity_name'                                   
=> $entity['name'],

-                               'lang_category'                                 
=> lang('category'),

-                               'category_name'                                 
=> $category['name'],

-                               'allow_allrows'                                 
=> True,

-                               'allrows'                                       
        => $this->allrows,

-                               'start_record'                                  
=> $this->start,

-                               'record_limit'                                  
=> $record_limit,

-                               'start_record'                                  
=> $this->start,

-                               'num_records'                                   
=> count($attrib_list),

-                               'all_records'                                   
=> $this->bo->total_records,

-                               'link_url'                                      
        => $GLOBALS['phpgw']->link('/index.php',$link_data),

-                               'img_path'                                      
        => $GLOBALS['phpgw']->common->get_image_path('phpgwapi','default'),

-                               'lang_searchfield_attribtext'   => lang('Enter 
the search string. To show all entries, empty this field and press the SUBMIT 
button again'),

-                               'lang_searchbutton_attribtext'  => lang('Submit 
the search string'),

-                               'query'                                         
        => $this->query,

-                               'lang_search'                                   
=> lang('search'),

-                               'table_header_attrib'                   => 
$table_header,

-                               'values_attrib'                                 
=> $content,

-                               'table_add'                                     
        => $table_add

-                       );

-

-                       $appname                                                
= lang('attribute');

-                       $function_msg                                   = 
lang('list entity attribute');

-

-                       $GLOBALS['phpgw_info']['flags']['app_header'] = 
lang($this->currentapp) . ' - ' . $appname . ': ' . $function_msg;

-                       
$GLOBALS['phpgw']->xslttpl->set_var('phpgw',array('list_attribute' => $data));

-               //      $GLOBALS['phpgw']->xslttpl->pp();

-                       $this->save_sessiondata();

-               }

-

-               function edit_attrib()

-               {

-                       $entity_id      = 
get_var('entity_id',array('POST','GET'));

-                       $cat_id         = get_var('cat_id',array('POST','GET'));

-                       $id                     = 
get_var('id',array('POST','GET'));

-                       $values         = get_var('values',array('POST'));

-

-                       
$GLOBALS['phpgw']->xslttpl->add_file(array('admin_entity'));

-

-                       if ($values['save'])

-                       {

-                               if($id)

-                               {

-                                       $values['id']=$id;

-                                       $action='edit';

-                               }

-

-                               $values['entity_id']=$entity_id;

-                               $values['cat_id']=$cat_id;

-

-                               if (!$values['column_name'])

-                               {

-                                       $receipt['error'][] = 
array('msg'=>lang('Column name not entered!'));

-                               }

-

-                               if (!$values['input_text'])

-                               {

-                                       $receipt['error'][] = 
array('msg'=>lang('Input text not entered!'));

-                               }

-                               if (!$values['statustext'])

-                               {

-                                       $receipt['error'][] = 
array('msg'=>lang('Statustext not entered!'));

-                               }

-

-                               if (!$values['entity_id'])

-                               {

-                                       $receipt['error'][] = 
array('msg'=>lang('entity type not choosen!'));

-                               }

-

-                               if (!$values['column_info']['type'])

-                               {

-                                       $receipt['error'][] = 
array('msg'=>lang('Datatype type not choosen!'));

-                               }

-

-                               
if(!ctype_digit($values['column_info']['precision']))

-                               {

-                                       
$receipt['error'][]=array('msg'=>lang('Please enter precision as integer !'));

-                                       
unset($values['column_info']['precision']);

-                               }

-

-                               if($values['column_info']['scale'] && 
!ctype_digit($values['column_info']['scale']))

-                               {

-                                       
$receipt['error'][]=array('msg'=>lang('Please enter scale as integer !'));

-                                       unset($values['column_info']['scale']);

-                               }

-

-                               if (!$values['column_info']['nullable'])

-                               {

-                                       $receipt['error'][] = 
array('msg'=>lang('Nullable not choosen!'));

-                               }

-

-

-                               if (!$receipt['error'])

-                               {

-

-                                       $receipt = 
$this->bo->save_attrib($values,$action);

-

-                                       if(!$id)

-                                       {

-                                               $id=$receipt['id'];

-                                       }

-                               }

-                               else

-                               {

-                                       $receipt['error'][] = array('msg'       
=> lang('Attribute has NOT been saved'));

-                               }

-

-                       }

-

-                       if ($id)

-                       {

-                               $values = 
$this->bo->read_single_attrib($entity_id,$cat_id,$id);

-                               $type_name=$values['type_name'];

-                               $function_msg = lang('edit attribute'). ' ' . 
lang($type_name);

-                               $action='edit';

-                       }

-                       else

-                       {

-                               $function_msg = lang('add attribute');

-                               $action='add';

-                       }

-

-                       $link_data = array

-                       (

-                               'menuaction'    => 
$this->currentapp.'.uiadmin_entity.edit_attrib',

-                               'entity_id'     => $entity_id,

-                               'cat_id'        => $cat_id,

-                               'id'    => $id

-                       );

-

-                       if($values['column_info']['type']=='R' || 
$values['column_info']['type']=='CH' || $values['column_info']['type']=='LB')

-                       {

-                               $multiple_choice= True;

-                       }

-

-//_debug_array($values);

-

-                       $entity = $this->bo->read_single($entity_id,false);

-                       $category = 
$this->bo->read_single_category($entity_id,$cat_id);

-

-                       $msgbox_data = $this->bocommon->msgbox_data($receipt);

-

-                       $data = array

-                       (

-                               'lang_entity'                                   
=> lang('entity'),

-                               'entity_name'                                   
=> $entity['name'],

-                               'lang_category'                                 
=> lang('category'),

-                               'category_name'                                 
=> $category['name'],

-

-                               'lang_choice'                           => 
lang('Choice'),

-                               'lang_new_value'                        => 
lang('New value'),

-                               'lang_new_value_statustext'     => lang('New 
value for mulitple choice'),

-                               'multiple_choice'                       => 
$multiple_choice,

-                               'value_choice'                          => 
$values['choice'],

-                               'lang_delete_value'                     => 
lang('Delete value'),

-                               'lang_value'                            => 
lang('value'),

-                               'lang_delete_choice_statustext'=> lang('Delete 
this value from the list of multiple choice'),

-

-                               'msgbox_data'                           => 
$GLOBALS['phpgw']->common->msgbox($msgbox_data),

-                               'form_action'                           => 
$GLOBALS['phpgw']->link('/index.php',$link_data),

-                               'done_action'                           => 
$GLOBALS['phpgw']->link('/index.php','menuaction='.$this->currentapp.'.uiadmin_entity.list_attribute&entity_id='.$entity_id
 . '&cat_id='.$cat_id),

-                               'lang_id'                                       
=> lang('Attribute ID'),

-                               'lang_entity_type'                      => 
lang('Entity type'),

-                               'lang_no_entity_type'           => lang('No 
entity type'),

-                               'lang_save'                                     
=> lang('save'),

-                               'lang_done'                                     
=> lang('done'),

-                               'value_id'                                      
=> $id,

-

-                               'lang_column_name'                              
=> lang('Column name'),

-                               'value_column_name'                             
=> $values['column_name'],

-                               'lang_column_name_statustext'   => lang('enter 
the name for the column'),

-

-                               'lang_input_text'                               
=> lang('input text'),

-                               'value_input_text'                              
=> $values['input_text'],

-                               'lang_input_name_statustext'    => lang('enter 
the input text for records'),

-

-                               'lang_id_attribtext'            => lang('Enter 
the attribute ID'),

-                               'lang_entity_statustext'        => lang('Select 
a entity type'),

-

-                               'lang_statustext'                       => 
lang('Statustext'),

-                               'lang_statustext_attribtext'=> lang('Enter a 
statustext for the inputfield in forms'),

-                               'value_statustext'                      => 
$values['statustext'],

-

-                               'lang_done_attribtext'          => lang('Back 
to the list'),

-                               'lang_save_attribtext'          => lang('Save 
the attribute'),

-

-                               'lang_datatype'                         => 
lang('Datatype'),

-                               'lang_datatype_statustext'      => lang('Select 
a datatype'),

-                               'lang_no_datatype'                      => 
lang('No datatype'),

-                               'datatype_list'                         => 
$this->bocommon->select_datatype($values['column_info']['type']),

-

-                               'lang_precision'                        => 
lang('Precision'),

-                               'lang_precision_statustext'     => lang('enter 
the record length'),

-                               'value_precision'                       => 
$values['column_info']['precision'],

-

-                               'lang_scale'                            => 
lang('scale'),

-                               'lang_scale_statustext'         => lang('enter 
the scale if type is decimal'),

-                               'value_scale'                           => 
$values['column_info']['scale'],

-

-                               'lang_default'                          => 
lang('default'),

-                               'lang_default_statustext'       => lang('enter 
the default value'),

-                               'value_default'                         => 
$values['column_info']['default'],

-

-                               'lang_nullable'                         => 
lang('Nullable'),

-                               'lang_nullable_statustext'      => lang('Chose 
if this column is nullable'),

-                               'lang_select_nullable'          => lang('Select 
nullable'),

-                               'nullable_list'                         => 
$this->bocommon->select_nullable($values['column_info']['nullable']),

-                               'value_lookup_form'                     => 
$values['lookup_form'],

-                               'lang_lookup_form'                              
=> lang('show in lookup forms'),

-                               'lang_lookup_form_statustext'                   
=> lang('check to show this attribue in lookup forms'),

-                               'value_list'                    => 
$values['list'],

-                               'lang_list'                             => 
lang('show in list'),

-                               'lang_list_statustext'                  => 
lang('check to show this attribute in entity list'),

-                               'value_search'                          => 
$values['search'],

-                               'lang_include_search'           => 
lang('Include in search'),

-                               'lang_include_search_statustext'=> lang('check 
to show this attribute in location list'),

-

-                       );

-//_debug_array($values);

-

-                       $appname                                                
= lang('entity');

-

-                       $GLOBALS['phpgw_info']['flags']['app_header'] = 
lang($this->currentapp) . ' - ' . $appname . ': ' . $function_msg;

-                       
$GLOBALS['phpgw']->xslttpl->set_var('phpgw',array('edit_attrib' => $data));

-               //      $GLOBALS['phpgw']->xslttpl->pp();

-               }

-

-               function list_custom_function()

-               {

-                       $entity_id      = $this->entity_id;

-                       $cat_id = $this->cat_id;

-                       $id     = get_var('id',array('POST','GET'));

-                       $resort = get_var('resort',array('POST','GET'));

-

-                       $GLOBALS['phpgw']->xslttpl->add_file(array(

-                                                                               
'admin_entity',

-                                                                               
'nextmatchs',

-                                                                               
'search_field'));

-

-                       if($resort)

-                       {

-                               $this->bo->resort_custom_function($id,$resort);

-                       }

-                       $custom_function_list = 
$this->bo->read_custom_function($entity_id,$cat_id);

-

-                       if (isset($custom_function_list) AND 
is_array($custom_function_list))

-                       {

-                               foreach($custom_function_list as $entry)

-                               {

-

-                                       $content[] = array

-                                       (

-                                               'file_name'                     
                        => $entry['file_name'],

-                                               'descr'                         
        => $entry['descr'],

-                                               'sorting'                       
                => $entry['sorting'],

-                                               'active'                        
                => $entry['active']?'X':'',

-                                               'link_up'                       
                => 
$GLOBALS['phpgw']->link('/index.php','menuaction='.$this->currentapp.'.uiadmin_entity.list_custom_function&resort=up&entity_id='
 . $entity_id . '&cat_id=' . $cat_id . '&id=' . $entry['id'] . '&allrows=' . 
$this->allrows),

-                                               'link_down'                     
                => 
$GLOBALS['phpgw']->link('/index.php','menuaction='.$this->currentapp.'.uiadmin_entity.list_custom_function&resort=down&entity_id='
 . $entity_id . '&cat_id=' . $cat_id . '&id=' . $entry['id'] . '&allrows=' . 
$this->allrows),

-                                               'link_edit'                     
                => 
$GLOBALS['phpgw']->link('/index.php','menuaction='.$this->currentapp.'.uiadmin_entity.edit_custom_function&entity_id='
 . $entity_id . '&cat_id=' . $cat_id . '&id=' . $entry['id']),

-                                               'link_delete'                   
        => 
$GLOBALS['phpgw']->link('/index.php','menuaction='.$this->currentapp.'.uiadmin_entity.delete&entity_id='
 . $entity_id . '&cat_id=' . $cat_id . '&custom_function_id=' . $entry['id']),

-                                               'lang_up_text'                  
        => lang('shift up'),

-                                               'lang_down_text'                
        => lang('shift down'),

-                                               'lang_edit_text'                
        => lang('edit the custom_function'),

-                                               'lang_delete_text'              
        => lang('delete the custom_function'),

-                                               'text_custom_function'          
        => lang('custom_functions'),

-                                               'text_up'                       
                => lang('up'),

-                                               'text_down'                     
                => lang('down'),

-                                               'text_edit'                     
                => lang('edit'),

-                                               'text_delete'                   
        => lang('delete')

-                                       );

-                               }

-                       }

-

-                       $table_header[] = array

-                       (

-                               'lang_descr'            => lang('Descr'),

-                               'lang_active'           => lang('Active'),

-                               'lang_sorting'          => lang('sorting'),

-                               'lang_search'           => lang('search'),

-                               'lang_edit'                     => lang('edit'),

-                               'lang_delete'           => lang('delete'),

-                               'sort_name'     => 
$this->nextmatchs->show_sort_order(array

-                                                                               
(

-                                                                               
        'sort'  => $this->sort,

-                                                                               
        'var'   =>      'column_name',

-                                                                               
        'order' =>      $this->order,

-                                                                               
        'extra'         => array('menuaction'   => 
$this->currentapp.'.uiadmin_entity.list_custom_function',

-                                                                               
                                                        'entity_id'     
=>$entity_id,

-                                                                               
                                                        'cat_id'        
=>$cat_id,

-                                                                               
                                                'allrows'=>$this->allrows)

-                                                                               
)),

-                               'sort_sorting'  => 
$this->nextmatchs->show_sort_order(array

-                                                                               
(

-                                                                               
        'sort'  => $this->sort,

-                                                                               
        'var'   =>      'custom_function_sort',

-                                                                               
        'order' =>      $this->order,

-                                                                               
        'extra'         => array('menuaction'   => 
$this->currentapp.'.uiadmin_entity.list_custom_function',

-                                                                               
                                                        'entity_id'     
=>$entity_id,

-                                                                               
                                                        'cat_id'        
=>$cat_id,

-                                                                               
                                                'allrows'=>$this->allrows)

-                                                                               
)),

-                               'lang_name'     => lang('Name'),

-                       );

-

-                       $table_add[] = array

-                       (

-                               'lang_add'                              => 
lang('add'),

-                               'lang_add_custom_functiontext'  => lang('add a 
custom_function'),

-                               'add_action'                    => 
$GLOBALS['phpgw']->link('/index.php','menuaction='.$this->currentapp.'.uiadmin_entity.edit_custom_function&entity_id='.$entity_id
 . '&cat_id=' . $cat_id),

-                               'lang_done'                             => 
lang('done'),

-                               'lang_done_custom_functiontext' => lang('back 
to admin'),

-                               'done_action'                   => 
$GLOBALS['phpgw']->link('/index.php','menuaction='.$this->currentapp.'.uiadmin_entity.category&entity_id='.$entity_id),

-                       );

-

-                       if(!$this->allrows)

-                       {

-                               $record_limit   = 
$GLOBALS['phpgw_info']['user']['preferences']['common']['maxmatchs'];

-                       }

-                       else

-                       {

-                               $record_limit   = $this->bo->total_records;

-                       }

-

-                       $link_data = array

-                       (

-                               'menuaction'    => 
$this->currentapp.'.uiadmin_entity.list_custom_function',

-                                               'sort'                  
=>$this->sort,

-                                               'order'                 
=>$this->order,

-                                               'query'                 
=>$this->query,

-                                               'entity_id'             
=>$entity_id,

-                                               'cat_id'                
=>$cat_id

-                       );

-

-                       $entity = $this->bo->read_single($entity_id,false);

-                       $category = 
$this->bo->read_single_category($entity_id,$cat_id);

-

-                       $data = array

-                       (

-                               'lang_entity'                                   
=> lang('entity'),

-                               'entity_name'                                   
=> $entity['name'],

-                               'lang_category'                                 
=> lang('category'),

-                               'category_name'                                 
=> $category['name'],

-                               'allow_allrows'                                 
=> True,

-                               'allrows'                                       
        => $this->allrows,

-                               'start_record'                                  
=> $this->start,

-                               'record_limit'                                  
=> $record_limit,

-                               'start_record'                                  
=> $this->start,

-                               'num_records'                                   
=> count($custom_function_list),

-                               'all_records'                                   
=> $this->bo->total_records,

-                               'link_url'                                      
        => $GLOBALS['phpgw']->link('/index.php',$link_data),

-                               'img_path'                                      
        => $GLOBALS['phpgw']->common->get_image_path('phpgwapi','default'),

-                               'lang_searchfield_custom_functiontext'  => 
lang('Enter the search string. To show all entries, empty this field and press 
the SUBMIT button again'),

-                               'lang_searchbutton_custom_functiontext' => 
lang('Submit the search string'),

-                               'query'                                         
        => $this->query,

-                               'lang_search'                                   
=> lang('search'),

-                               'table_header_custom_function'                  
=> $table_header,

-                               'values_custom_function'                        
                => $content,

-                               'table_add'                                     
        => $table_add

-                       );

-

-                       $appname                                                
= lang('custom function');

-                       $function_msg                                   = 
lang('list entity custom function');

-

-                       $GLOBALS['phpgw_info']['flags']['app_header'] = 
lang($this->currentapp) . ' - ' . $appname . ': ' . $function_msg;

-                       
$GLOBALS['phpgw']->xslttpl->set_var('phpgw',array('list_custom_function' => 
$data));

-               //      $GLOBALS['phpgw']->xslttpl->pp();

-                       $this->save_sessiondata();

-               }

-

-               function edit_custom_function()

-               {

-                       $entity_id      = 
get_var('entity_id',array('POST','GET'));

-                       $cat_id         = get_var('cat_id',array('POST','GET'));

-                       $id                     = 
get_var('id',array('POST','GET'));

-                       $values         = get_var('values',array('POST'));

-

-                       
$GLOBALS['phpgw']->xslttpl->add_file(array('admin_entity'));

-

-                       if ($values['save'])

-                       {

-                               if($id)

-                               {

-                                       $values['id']=$id;

-                                       $action='edit';

-                               }

-

-                               $values['entity_id']=$entity_id;

-                               $values['cat_id']=$cat_id;

-

-

-                               if (!$values['entity_id'])

-                               {

-                                       $receipt['error'][] = 
array('msg'=>lang('entity type not choosen!'));

-                               }

-

-                               if (!$values['custom_function_file'])

-                               {

-                                       $receipt['error'][] = 
array('msg'=>lang('custom function file not choosen!'));

-                               }

-

-

-                               if (!$receipt['error'])

-                               {

-

-                                       $receipt = 
$this->bo->save_custom_function($values,$action);

-

-                                       if(!$id)

-                                       {

-                                               $id=$receipt['id'];

-                                       }

-                               }

-                               else

-                               {

-                                       $receipt['error'][] = array('msg'       
=> lang('Custom function has NOT been saved'));

-                               }

-

-                       }

-

-                       if ($id)

-                       {

-                               $values = 
$this->bo->read_single_custom_function($entity_id,$cat_id,$id);

-                               $type_name=$values['type_name'];

-                               $function_msg = lang('edit custom function'). ' 
' . lang($type_name);

-                               $action='edit';

-                       }

-                       else

-                       {

-                               $function_msg = lang('add custom function');

-                               $action='add';

-                       }

-

-                       $link_data = array

-                       (

-                               'menuaction'    => 
$this->currentapp.'.uiadmin_entity.edit_custom_function',

-                               'entity_id'     => $entity_id,

-                               'cat_id'        => $cat_id,

-                               'id'    => $id

-                       );

-

-

-//_debug_array($values);

-

-                       $entity = $this->bo->read_single($entity_id,false);

-                       $category = 
$this->bo->read_single_category($entity_id,$cat_id);

-

-                       $msgbox_data = $this->bocommon->msgbox_data($receipt);

-

-                       $data = array

-                       (

-                               'lang_entity'                                   
=> lang('entity'),

-                               'entity_name'                                   
=> $entity['name'],

-                               'lang_category'                                 
=> lang('category'),

-                               'category_name'                                 
=> $category['name'],

-

-                               'msgbox_data'                           => 
$GLOBALS['phpgw']->common->msgbox($msgbox_data),

-                               'form_action'                           => 
$GLOBALS['phpgw']->link('/index.php',$link_data),

-                               'done_action'                           => 
$GLOBALS['phpgw']->link('/index.php','menuaction='.$this->currentapp.'.uiadmin_entity.list_custom_function&entity_id='.$entity_id
 . '&cat_id='.$cat_id),

-                               'lang_id'                                       
=> lang('Custom function ID'),

-                               'lang_entity_type'                      => 
lang('Entity type'),

-                               'lang_no_entity_type'           => lang('No 
entity type'),

-                               'lang_save'                                     
=> lang('save'),

-                               'lang_done'                                     
=> lang('done'),

-                               'value_id'                                      
=> $id,

-

-                               'lang_descr'                    => 
lang('descr'),

-                               'lang_descr_custom_functiontext'=> lang('Enter 
a descr for the custom function'),

-                               'value_descr'                   => 
$values['descr'],

-

-                               'lang_done_custom_functiontext'         => 
lang('Back to the list'),

-                               'lang_save_custom_functiontext'         => 
lang('Save the custom function'),

-

-                               'lang_custom_function'                          
=> lang('custom function'),

-                               'lang_custom_function_statustext'       => 
lang('Select a custom_function'),

-                               'lang_no_custom_function'                       
=> lang('No custom function'),

-                               'custom_function_list'                          
=> $this->bo->select_custom_function($values['custom_function_file']),

-

-                               'value_active'                  => 
$values['active'],

-                               'lang_active'                           => 
lang('Active'),

-                               'lang_active_statustext'        => lang('check 
acivate custom function'),

-                               'value_search'                          => 
$values['search'],

-                               'lang_include_search'           => 
lang('Include in search'),

-                               'lang_include_search_statustext'=> lang('check 
to show this custom function in location list')

-                       );

-

-                       $appname                                                
= lang('entity');

-

-                       $GLOBALS['phpgw_info']['flags']['app_header'] = 
lang($this->currentapp) . ' - ' . $appname . ': ' . $function_msg;

-                       
$GLOBALS['phpgw']->xslttpl->set_var('phpgw',array('edit_custom_function' => 
$data));

-               }

-

-       }

-?>

+<?php
+       /**
+
+
+       * phpGroupWare - property: a Facilities Management System.
+       *
+       * @author Sigurd Nes <address@hidden>
+       * @copyright Copyright (C) 2003-2005 Free Software Foundation, Inc. 
http://www.fsf.org/
+       * @license http://www.gnu.org/licenses/gpl.html GNU General Public 
License
+       * @internal Development of this application was funded by 
http://www.bergen.kommune.no/bbb_/ekstern/
+       * @package property
+       * @subpackage admin
+       * @version $Id: class.uiadmin_entity.inc.php,v 1.13 2006/01/30 22:14:19 
sigurdne Exp $
+       */
+
+       /**
+        * Description
+        * @package property
+        */
+
+       class uiadmin_entity
+       {
+               var $grants;
+               var $start;
+               var $query;
+               var $sort;
+               var $order;
+               var $sub;
+               var $currentapp;
+
+               var $public_functions = array
+               (
+                       'index'                         => True,
+                       'list_status'                   => True,
+                       'category'                      => True,
+                       'edit'                          => True,
+                       'edit_status'           => True,
+                       'edit_category'         => True,
+                       'view'                          => True,
+                       'delete'                        => True,
+                       'list_attribute'        => True,
+                       'edit_attrib'           => True,
+                       'list_custom_function'=>True,
+                       'edit_custom_function'  => True
+               );
+
+               function uiadmin_entity()
+               {
+                       $GLOBALS['phpgw_info']['flags']['xslt_app'] = True;
+                       $this->currentapp                       = 
$GLOBALS['phpgw_info']['flags']['currentapp'];
+                       $this->nextmatchs                       = 
CreateObject('phpgwapi.nextmatchs');
+                       $this->account                          = 
$GLOBALS['phpgw_info']['user']['account_id'];
+                       $this->bo                                       = 
CreateObject($this->currentapp.'.boadmin_entity',True);
+                       $this->bocommon                         = 
CreateObject($this->currentapp.'.bocommon');
+                       $this->menu                                     = 
CreateObject($this->currentapp.'.menu');
+
+                       $this->start                            = 
$this->bo->start;
+                       $this->query                            = 
$this->bo->query;
+                       $this->sort                                     = 
$this->bo->sort;
+                       $this->order                            = 
$this->bo->order;
+                       $this->entity_id                        = 
$this->bo->entity_id;
+                       $this->cat_id                           = 
$this->bo->cat_id;
+                       $this->allrows                          = 
$this->bo->allrows;
+
+                       $this->menu->sub                        ='admin_entity';
+               }
+
+               function save_sessiondata()
+               {
+                       $data = array
+                       (
+                               'start'         => $this->start,
+                               'query'         => $this->query,
+                               'sort'          => $this->sort,
+                               'order'         => $this->order,
+                               'allrows'       => $this->allrows,
+                               'entity_id'     => $this->entity_id,
+                               'cat_id'        => $this->cat_id
+                       );
+                       $this->bo->save_sessiondata($data);
+               }
+
+               function index()
+               {
+                       $this->bo->reset_fm_cache();
+                       $GLOBALS['phpgw']->xslttpl->add_file(array(
+                                                                               
'admin_entity',
+                                                                               
'nextmatchs',
+                                                                               
'menu',
+                                                                               
'search_field'));
+                       $links = $this->menu->links();
+
+                       $entity_list = $this->bo->read();
+
+                       if (isSet($entity_list) AND is_array($entity_list))
+                       {
+                               foreach($entity_list as $entry)
+                               {
+                                       $content[] = array
+                                       (
+                                               'id'                            
                => $entry['id'],
+                                               'name'                          
                => $entry['name'],
+                                               'descr'                         
                => $entry['descr'],
+                                               'link_categories'               
        => 
$GLOBALS['phpgw']->link('/index.php','menuaction='.$this->currentapp.'.uiadmin_entity.category&entity_id='
 . $entry['id']),
+                                               'link_edit'                     
                => 
$GLOBALS['phpgw']->link('/index.php','menuaction='.$this->currentapp.'.uiadmin_entity.edit&id='
 . $entry['id']),
+                                               'link_delete'                   
        => 
$GLOBALS['phpgw']->link('/index.php','menuaction='.$this->currentapp.'.uiadmin_entity.delete&entity_id='
 . $entry['id']),
+                                               'lang_view_standardtext'        
=> lang('view the standard'),
+                                               'lang_category_text'            
=> lang('categories for the entity type'),
+                                               'lang_edit_standardtext'        
=> lang('edit the entity'),
+                                               'lang_delete_standardtext'      
=> lang('delete the entity'),
+                                               'text_categories'               
        => lang('Categories'),
+                                               'text_edit'                     
                => lang('edit'),
+                                               'text_delete'                   
        => lang('delete')
+                                       );
+                               }
+                       }
+
+//_debug_array($content);
+
+                       $table_header[] = array
+                       (
+
+                               'lang_descr'            => lang('Descr'),
+                               'lang_categories'       => lang('Categories'),
+                               'lang_edit'                     => lang('edit'),
+                               'lang_delete'           => lang('delete'),
+                               'sort_id'       => 
$this->nextmatchs->show_sort_order(array
+                                                                               
(
+                                                                               
        'sort'  => $this->sort,
+                                                                               
        'var'   =>      'id',
+                                                                               
        'order' =>      $this->order,
+                                                                               
        'extra'         => array('menuaction'   => 
$this->currentapp.'.uiadmin_entity.index',
+                                                                               
                                                'allrows'=>$this->allrows)
+                                                                               
)),
+                               'lang_id'       => lang('entity id'),
+                               'sort_name'     => 
$this->nextmatchs->show_sort_order(array
+                                                                               
(
+                                                                               
        'sort'  => $this->sort,
+                                                                               
        'var'   =>      'name',
+                                                                               
        'order' =>      $this->order,
+                                                                               
        'extra'         => array('menuaction'   => 
$this->currentapp.'.uiadmin_entity.index',
+                                                                               
                                                'allrows'=>$this->allrows)
+                                                                               
)),
+                               'lang_name'     => lang('Name'),
+                       );
+
+                       $table_add[] = array
+                       (
+                               'lang_add'                              => 
lang('add'),
+                               'lang_add_standardtext' => lang('add a 
standard'),
+                               'add_action'                    => 
$GLOBALS['phpgw']->link('/index.php','menuaction='.$this->currentapp.'.uiadmin_entity.edit'),
+                               'lang_done'                             => 
lang('done'),
+                               'lang_done_standardtext'        => lang('back 
to admin'),
+                               'done_action'                   => 
$GLOBALS['phpgw']->link('/admin/index.php')
+                       );
+
+                       if(!$this->allrows)
+                       {
+                               $record_limit   = 
$GLOBALS['phpgw_info']['user']['preferences']['common']['maxmatchs'];
+                       }
+                       else
+                       {
+                               $record_limit   = $this->bo->total_records;
+                       }
+
+                       $link_data = array
+                       (
+                               'menuaction'    => 
$this->currentapp.'.uiadmin_entity.index',
+                                               'sort'                  
=>$this->sort,
+                                               'order'                 
=>$this->order,
+                                               'query'                 
=>$this->query
+                       );
+
+                       $data = array
+                       (
+                               'links'                                         
        => $links,
+                               'allow_allrows'                                 
=> True,
+                               'allrows'                                       
        => $this->allrows,
+                               'start_record'                                  
=> $this->start,
+                               'record_limit'                                  
=> $record_limit,
+                               'num_records'                                   
=> count($entity_list),
+                               'all_records'                                   
=> $this->bo->total_records,
+                               'link_url'                                      
        => $GLOBALS['phpgw']->link('/index.php',$link_data),
+                               'img_path'                                      
        => $GLOBALS['phpgw']->common->get_image_path('phpgwapi','default'),
+                               'lang_searchfield_standardtext' => lang('Enter 
the search string. To show all entries, empty this field and press the SUBMIT 
button again'),
+                               'lang_searchbutton_standardtext'        => 
lang('Submit the search string'),
+                               'query'                                         
        => $this->query,
+                               'lang_search'                                   
=> lang('search'),
+                               'table_header'                                  
=> $table_header,
+                               'values'                                        
        => $content,
+                               'table_add'                                     
        => $table_add
+                       );
+
+                       $appname                                                
= lang('entity');
+                       $function_msg                                   = 
lang('list entity type');
+
+                       $GLOBALS['phpgw_info']['flags']['app_header'] = 
lang($this->currentapp) . ' - ' . $appname . ': ' . $function_msg;
+                       
$GLOBALS['phpgw']->xslttpl->set_var('phpgw',array('list' => $data));
+               //      $GLOBALS['phpgw']->xslttpl->pp();
+                       $this->save_sessiondata();
+               }
+
+               function list_status()
+               {
+                       $this->bo->reset_fm_cache();
+                       $GLOBALS['phpgw']->xslttpl->add_file(array(
+                                                                               
'admin_entity',
+                                                                               
'nextmatchs',
+                                                                               
'menu',
+                                                                               
'search_field'));
+                       $links = $this->menu->links();
+
+                       $list = $this->bo->read_status();
+
+                       if (isSet($list) AND is_array($list))
+                       {
+                               foreach($list as $entry)
+                               {
+                                       $content[] = array
+                                       (
+                                               'id'                            
                => $entry['id'],
+                                               'descr'                         
                => $entry['descr'],
+                                               'link_edit'                     
                => 
$GLOBALS['phpgw']->link('/index.php','menuaction='.$this->currentapp.'.uiadmin_entity.edit_status&entity_id='
 . $this->entity_id . '&cat_id=' . $this->cat_id . '&id=' . $entry['id']),
+                                               'link_delete'                   
        => 
$GLOBALS['phpgw']->link('/index.php','menuaction='.$this->currentapp.'.uiadmin_entity.delete&entity_id='
 . $this->entity_id . '&cat_id=' . $this->cat_id . '&status_id=' . 
$entry['id']),
+                                               'lang_edit_standardtext'        
=> lang('edit the entity'),
+                                               'lang_delete_standardtext'      
=> lang('delete the entity'),
+                                               'text_edit'                     
                => lang('edit'),
+                                               'text_delete'                   
        => lang('delete')
+                                       );
+                               }
+                       }
+
+//_debug_array($content);
+
+                       $table_header[] = array
+                       (
+
+                               'lang_descr'            => lang('Descr'),
+                               'lang_edit'                     => lang('edit'),
+                               'lang_delete'           => lang('delete'),
+                               'sort_id'       => 
$this->nextmatchs->show_sort_order(array
+                                                                               
(
+                                                                               
        'sort'  => $this->sort,
+                                                                               
        'var'   =>      'id',
+                                                                               
        'order' =>      $this->order,
+                                                                               
        'extra'         => array('menuaction'   => 
$this->currentapp.'.uiadmin_entity.list_status',
+                                                                               
                                                        'entity_id'     => 
$this->entity_id,
+                                                                               
                                                        'cat_id'        => 
$this->cat_id
+                                                                               
                                                        )
+                                                                               
)),
+                               'lang_id'       => lang('status'),
+                       );
+
+                       $table_add[] = array
+                       (
+                               'lang_add'                              => 
lang('add'),
+                               'lang_add_text' => lang('add a standard'),
+                               'add_action'                    => 
$GLOBALS['phpgw']->link('/index.php','menuaction='.$this->currentapp.'.uiadmin_entity.edit_status&entity_id='
 . $this->entity_id . '&cat_id=' . $this->cat_id),
+                               'lang_done'                             => 
lang('done'),
+                               'lang_done_text'        => lang('back to 
admin'),
+                               'done_action'                   => 
$GLOBALS['phpgw']->link('/index.php','menuaction='.$this->currentapp.'.uiadmin_entity.category&entity_id='
 . $this->entity_id . '&cat_id=' . $this->cat_id),
+                       );
+
+                       if(!$this->allrows)
+                       {
+                               $record_limit   = 
$GLOBALS['phpgw_info']['user']['preferences']['common']['maxmatchs'];
+                       }
+                       else
+                       {
+                               $record_limit   = $this->bo->total_records;
+                       }
+
+                       $link_data = array
+                       (
+                               'menuaction'    => 
$this->currentapp.'.uiadmin_entity.list_status',
+                                               'sort'                  
=>$this->sort,
+                                               'order'                 
=>$this->order,
+                                               'query'                 
=>$this->query,
+                                               'entity_id'             
=>$this->entity_id,
+                                               'cat_id'                
=>$this->cat_id
+                       );
+
+                       $entity = 
$this->bo->read_single($this->entity_id,false);
+                       $category = 
$this->bo->read_single_category($this->entity_id,$this->cat_id);
+
+                       $msgbox_data = $this->bocommon->msgbox_data($receipt);
+
+                       $data = array
+                       (
+                               'lang_entity'                                   
=> lang('entity'),
+                               'entity_name'                                   
=> $entity['name'],
+                               'lang_category'                                 
=> lang('category'),
+                               'category_name'                                 
=> $category['name'],
+                               'links'                                         
        => $links,
+                               'allow_allrows'                                 
=> True,
+                               'allrows'                                       
        => $this->allrows,
+                               'start_record'                                  
=> $this->start,
+                               'record_limit'                                  
=> $record_limit,
+                               'num_records'                                   
=> count($entity_list),
+                               'all_records'                                   
=> $this->bo->total_records,
+                               'link_url'                                      
        => $GLOBALS['phpgw']->link('/index.php',$link_data),
+                               'img_path'                                      
        => $GLOBALS['phpgw']->common->get_image_path('phpgwapi','default'),
+                               'lang_searchfield_standardtext' => lang('Enter 
the search string. To show all entries, empty this field and press the SUBMIT 
button again'),
+                               'lang_searchbutton_standardtext'        => 
lang('Submit the search string'),
+                               'query'                                         
        => $this->query,
+                               'lang_search'                                   
=> lang('search'),
+                               'table_header_status'                   => 
$table_header,
+                               'values_status'                                 
=> $content,
+                               'table_add'                                     
        => $table_add
+                       );
+
+                       $appname                                                
= lang('entity');
+                       $function_msg                                   = 
lang('list status');
+
+                       $GLOBALS['phpgw_info']['flags']['app_header'] = 
lang($this->currentapp) . ' - ' . $appname . ': ' . $function_msg;
+                       
$GLOBALS['phpgw']->xslttpl->set_var('phpgw',array('list_status' => $data));
+               //      $GLOBALS['phpgw']->xslttpl->pp();
+                       $this->save_sessiondata();
+               }
+
+               function category()
+               {
+
+                       $entity_id      = 
get_var('entity_id',array('POST','GET'));
+
+                       $GLOBALS['phpgw']->xslttpl->add_file(array(
+                                                                               
'admin_entity',
+                                                                               
'nextmatchs',
+                                                                               
'menu',
+                                                                               
'search_field'));
+                       $links = $this->menu->links();
+
+                       $category_list = $this->bo->read_category($entity_id);
+
+                       if (isSet($category_list) AND is_array($category_list))
+                       {
+                               foreach($category_list as $entry)
+                               {
+                                       $content[] = array
+                                       (
+                                               'id'                            
                => $entry['id'],
+                                               'name'                          
                => $entry['name'],
+                                               'prefix'                        
                => $entry['prefix'],
+                                               'descr'                         
                => $entry['descr'],
+//                                             'link_status'                   
        => 
$GLOBALS['phpgw']->link('/index.php','menuaction='.$this->currentapp.'.uiadmin_entity.list_status&cat_id='
 . $entry['id'] . '&entity_id=' . $entity_id),
+                                               'link_custom_function'          
        => 
$GLOBALS['phpgw']->link('/index.php','menuaction='.$this->currentapp.'.uiadmin_entity.list_custom_function&cat_id='
 . $entry['id'] . '&entity_id=' . $entity_id),
+                                               'link_attribute'                
        => 
$GLOBALS['phpgw']->link('/index.php','menuaction='.$this->currentapp.'.uiadmin_entity.list_attribute&cat_id='
 . $entry['id'] . '&entity_id=' . $entity_id),
+                                               'link_edit'                     
                => 
$GLOBALS['phpgw']->link('/index.php','menuaction='.$this->currentapp.'.uiadmin_entity.edit_category&id='
 . $entry['id'] . '&entity_id=' . $entity_id),
+                                               'link_delete'                   
        => 
$GLOBALS['phpgw']->link('/index.php','menuaction='.$this->currentapp.'.uiadmin_entity.delete&cat_id='
 . $entry['id'] . '&entity_id=' . $entity_id),
+                                               'lang_view_standardtext'        
=> lang('view the category'),
+                                               'lang_status_standardtext'      
=> lang('Status for the entity category'),
+                                               'lang_attribute_standardtext'   
=> lang('attributes for the entity category'),
+                                               
'lang_custom_function_standardtext'     => lang('custom functions for the 
entity category'),
+                                               'lang_edit_standardtext'        
=> lang('edit the standard'),
+                                               'lang_delete_standardtext'      
=> lang('delete the standard'),
+                                               'text_status'                   
=> lang('Status'),
+                                               'text_attribute'                
        => lang('Attributes'),
+                                               'text_custom_function'          
        => lang('Custom functions'),
+                                               'text_edit'                     
                => lang('edit'),
+                                               'text_delete'                   
        => lang('delete')
+                                       );
+                               }
+                       }
+
+//_debug_array($content);
+
+                       $table_header[] = array
+                       (
+
+                               'lang_descr'            => lang('Descr'),
+                               'lang_prefix'           => lang('prefix'),
+//                             'lang_status'           => lang('Status'),
+                               'lang_attribute'        => lang('Attributes'),
+                               'lang_custom_function'  => lang('custom 
functions'),
+                               'lang_edit'                     => lang('edit'),
+                               'lang_delete'           => lang('delete'),
+                               'sort_id'       => 
$this->nextmatchs->show_sort_order(array
+                                                                               
(
+                                                                               
        'sort'  => $this->sort,
+                                                                               
        'var'   =>      'id',
+                                                                               
        'order' =>      $this->order,
+                                                                               
        'extra'         => array('menuaction'   => 
$this->currentapp.'.uiadmin_entity.category',
+                                                                               
                                                'entity_id' =>$entity_id,
+                                                                               
                                                'allrows'=>$this->allrows)
+                                                                               
)),
+                               'lang_id'       => lang('category id'),
+                               'sort_name'     => 
$this->nextmatchs->show_sort_order(array
+                                                                               
(
+                                                                               
        'sort'  => $this->sort,
+                                                                               
        'var'   =>      'name',
+                                                                               
        'order' =>      $this->order,
+                                                                               
        'extra'         => array('menuaction'   => 
$this->currentapp.'.uiadmin_entity.category',
+                                                                               
                                                'entity_id' =>$entity_id,
+                                                                               
                                                'allrows'=>$this->allrows)
+                                                                               
)),
+                               'lang_name'     => lang('Name'),
+                       );
+
+                       $table_add[] = array
+                       (
+                               'lang_add'                              => 
lang('add'),
+                               'lang_add_standardtext' => lang('add a 
category'),
+                               'add_action'                    => 
$GLOBALS['phpgw']->link('/index.php','menuaction='.$this->currentapp.'.uiadmin_entity.edit_category&entity_id='
 . $entity_id),
+                               'lang_done'                             => 
lang('done'),
+                               'lang_done_standardtext'        => lang('back 
to entity'),
+                               'done_action'                   => 
$GLOBALS['phpgw']->link('/index.php','menuaction='.$this->currentapp.'.uiadmin_entity.index')
+                       );
+
+                       $entity = $this->bo->read_single($entity_id,false);
+
+                       if(!$this->allrows)
+                       {
+                               $record_limit   = 
$GLOBALS['phpgw_info']['user']['preferences']['common']['maxmatchs'];
+                       }
+                       else
+                       {
+                               $record_limit   = $this->bo->total_records;
+                       }
+
+                       $link_data = array
+                       (
+                               'menuaction'    => 
$this->currentapp.'.uiadmin_entity.category',
+                                               'sort'                  
=>$this->sort,
+                                               'order'                 
=>$this->order,
+                                               'query'                 
=>$this->query,
+                                               'entity_id'             
=>$entity_id
+                       );
+
+                       $data = array
+                       (
+                               'lang_entity'                                   
=> lang('entity'),
+                               'entity_name'                                   
=> $entity['name'],
+                               'links'                                         
        => $links,
+                               'allow_allrows'                                 
=> True,
+                               'allrows'                                       
        => $this->allrows,
+                               'start_record'                                  
=> $this->start,
+                               'record_limit'                                  
=> $record_limit,
+                               'num_records'                                   
=> count($category_list),
+                               'all_records'                                   
=> $this->bo->total_records,
+                               'link_url'                                      
        => $GLOBALS['phpgw']->link('/index.php',$link_data),
+                               'img_path'                                      
        => $GLOBALS['phpgw']->common->get_image_path('phpgwapi','default'),
+                               'lang_searchfield_standardtext' => lang('Enter 
the search string. To show all entries, empty this field and press the SUBMIT 
button again'),
+                               'lang_searchbutton_standardtext'        => 
lang('Submit the search string'),
+                               'query'                                         
        => $this->query,
+                               'lang_search'                                   
=> lang('search'),
+                               'table_header_category'                 => 
$table_header,
+                               'values_category'                               
=> $content,
+                               'table_add'                                     
        => $table_add
+                       );
+
+                       $appname                                                
= lang('entity');
+                       $function_msg                                   = 
lang('list entity type');
+
+                       $GLOBALS['phpgw_info']['flags']['app_header'] = 
lang($this->currentapp) . ' - ' . $appname . ': ' . $function_msg;
+                       
$GLOBALS['phpgw']->xslttpl->set_var('phpgw',array('list_category' => $data));
+               //      $GLOBALS['phpgw']->xslttpl->pp();
+                       $this->save_sessiondata();
+               }
+
+               function edit()
+               {
+                       $id     = get_var('id',array('POST','GET'));
+                       $values                 = 
get_var('values',array('POST'));
+                       $config = 
CreateObject('phpgwapi.config',$this->currentapp);
+
+                       
$GLOBALS['phpgw']->xslttpl->add_file(array('admin_entity'));
+
+                       if ($values['save'])
+                       {
+                               if (!$values['name'])
+                               {
+                                       $receipt['error'][] = 
array('msg'=>lang('Name not entered!'));
+                               }
+
+                               if($id)
+                               {
+                                       $values['id']=$id;
+                                       $action='edit';
+                               }
+
+                               if (!$receipt['error'])
+                               {
+
+                                       $receipt = 
$this->bo->save($values,$action);
+                                       if(!$id)
+                                       {
+                                               $id=$receipt['id'];
+                                       }
+                                       $config->read_repository();
+
+                                       
if(!is_array($config->config_data['location_form']))
+                                       {
+                                               
$config->config_data['location_form'] = array();
+                                       }
+
+                                       if($values['location_form'])
+                                       {
+
+                                               
$config->config_data['location_form']['entity_' . $id] = 'entity_' . $id;
+
+                                       }
+                                       else
+                                       {
+                                               
unset($config->config_data['location_form']['entity_' . $id]);
+                                       }
+
+                                       $config->save_repository();
+                               }
+                               else
+                               {
+                                       $receipt['error'][] = array('msg'=> 
lang('Entity has NOT been saved'));
+                               }
+
+                       }
+
+
+                       if ($id)
+                       {
+                               $values = $this->bo->read_single($id);
+                               $function_msg = lang('edit standard');
+                               $action='edit';
+                       }
+                       else
+                       {
+                               $function_msg = lang('add entity');
+                               $action='add';
+                       }
+
+                       $include_list   = 
$this->bo->get_entity_list($values['lookup_entity']);
+                       $include_list_2 = 
$this->bo->get_entity_list_2($values['include_entity_for']);
+                       $include_list_3 = 
$this->bo->get_entity_list_3($values['start_entity_from']);
+
+                       $link_data = array
+                       (
+                               'menuaction'    => 
$this->currentapp.'.uiadmin_entity.edit',
+                               'id'    => $id
+                       );
+//_debug_array($include_list);
+
+                       $msgbox_data = $this->bocommon->msgbox_data($receipt);
+
+                       $data = array
+                       (
+                               'msgbox_data'                                   
=> $GLOBALS['phpgw']->common->msgbox($msgbox_data),
+                               'lang_name_standardtext'                => 
lang('Enter a name of the standard'),
+                               'form_action'                                   
=> $GLOBALS['phpgw']->link('/index.php',$link_data),
+                               'done_action'                                   
=> 
$GLOBALS['phpgw']->link('/index.php','menuaction='.$this->currentapp.'.uiadmin_entity.index'),
+                               'lang_id'                                       
        => lang('standard ID'),
+                               'lang_name'                                     
        => lang('Name'),
+                               'lang_descr'                                    
=> lang('Descr'),
+                               'lang_save'                                     
        => lang('save'),
+                               'lang_done'                                     
        => lang('done'),
+                               'value_id'                                      
        => $id,
+                               'value_name'                                    
=> $values['name'],
+                               'lang_id_standardtext'                  => 
lang('Enter the standard ID'),
+                               'lang_descr_standardtext'               => 
lang('Enter a description of the standard'),
+                               'lang_done_standardtext'                => 
lang('Back to the list'),
+                               'lang_save_standardtext'                => 
lang('Save the standard'),
+                               'type_id'                                       
        => $values['type_id'],
+                               'value_descr'                                   
=> $values['descr'],
+                               'lang_location_form'                    => 
lang('location form'),
+                               'value_location_form'                   => 
$values['location_form'],
+                               'lang_location_form_statustext' => lang('If 
this entity type is to be linked to a location'),
+                               'lang_include_in_location_form' => 
lang('include in location form'),
+                               'include_list'                                  
=> $include_list,
+                               'lang_include_statustext'               => 
lang('Which entity type is to show up in location forms'),
+                               'lang_include_this_entity'              => 
lang('include this entity'),
+                               'include_list_2'                                
=> $include_list_2,
+                               'lang_include_2_statustext'             => 
lang('Let this entity show up in location form'),
+                               'lang_start_this_entity'                => 
lang('start this entity'),
+                               'include_list_3'                                
=> $include_list_3,
+                               'lang_include_3_statustext'             => 
lang('Start this entity from'),
+                               'lang_select'                                   
=> lang('select'),
+                               'lang_documentation'                    => 
lang('documentation'),
+                               'value_documentation'                   => 
$values['documentation'],
+                               'lang_documentation_statustext' => lang('If 
this entity type is to be linked to documents'),
+                       );
+
+                       $appname                                                
= lang('entity');
+
+                       $GLOBALS['phpgw_info']['flags']['app_header'] = 
lang($this->currentapp) . ' - ' . $appname . ': ' . $function_msg;
+                       
$GLOBALS['phpgw']->xslttpl->set_var('phpgw',array('edit' => $data));
+               //      $GLOBALS['phpgw']->xslttpl->pp();
+               }
+
+               function edit_status()
+               {
+                       $id     = get_var('id',array('POST','GET'));
+                       $values                 = 
get_var('values',array('POST'));
+
+                       
$GLOBALS['phpgw']->xslttpl->add_file(array('admin_entity'));
+
+                       if ($values['save'])
+                       {
+                               if (!$values['id'])
+                               {
+                                       $receipt['error'][] = 
array('msg'=>lang('ID not entered!'));
+                               }
+
+                               if($id)
+                               {
+                                       $values['id']=$id;
+                                       $action='edit';
+                               }
+
+                               if (!$receipt['error'])
+                               {
+                                       $receipt = 
$this->bo->save_status($values,$action);
+                                       if(!$id)
+                                       {
+                                               $id=$receipt['id'];
+                                       }
+                               }
+                               else
+                               {
+                                       $receipt['error'][] = array('msg'=> 
lang('Status has NOT been saved'));
+                               }
+
+                       }
+
+                       if ($id)
+                       {
+                               $values = $this->bo->read_single_status($id);
+                               $function_msg = lang('edit status');
+                               $action='edit';
+                       }
+                       else
+                       {
+                               $function_msg = lang('add status');
+                               $action='add';
+                       }
+
+
+                       $link_data = array
+                       (
+                               'menuaction'    => 
$this->currentapp.'.uiadmin_entity.edit_status',
+                               'entity_id'             => $this->entity_id,
+                               'cat_id'                => $this->cat_id,
+                               'id'                    => $id
+                       );
+//_debug_array($link_data);
+
+                       $entity = 
$this->bo->read_single($this->entity_id,false);
+                       $category = 
$this->bo->read_single_category($this->entity_id,$this->cat_id);
+
+                       $msgbox_data = $this->bocommon->msgbox_data($receipt);
+
+                       $data = array
+                       (
+                               'lang_entity'                                   
=> lang('entity'),
+                               'entity_name'                                   
=> $entity['name'],
+                               'lang_category'                                 
=> lang('category'),
+                               'category_name'                                 
=> $category['name'],
+                               'msgbox_data'                                   
=> $GLOBALS['phpgw']->common->msgbox($msgbox_data),
+                               'lang_name_standardtext'                => 
lang('Enter a name of the standard'),
+                               'form_action'                                   
=> $GLOBALS['phpgw']->link('/index.php',$link_data),
+                               'done_action'                                   
=> 
$GLOBALS['phpgw']->link('/index.php','menuaction='.$this->currentapp.'.uiadmin_entity.list_status&entity_id='
 . $this->entity_id . '&cat_id=' . $this->cat_id),
+                               'lang_id'                                       
        => lang('status ID'),
+                               'lang_descr'                                    
=> lang('Descr'),
+                               'lang_save'                                     
        => lang('save'),
+                               'lang_done'                                     
        => lang('done'),
+                               'value_id'                                      
        => $id,
+                               'lang_id_standardtext'                  => 
lang('Enter the status ID'),
+                               'lang_descr_standardtext'               => 
lang('Enter a description of the status'),
+                               'lang_done_standardtext'                => 
lang('Back to the list'),
+                               'lang_save_standardtext'                => 
lang('Save the status'),
+                               'value_descr'                                   
=> $values['descr']
+                       );
+
+                       $appname                                                
= lang('entity');
+
+                       $GLOBALS['phpgw_info']['flags']['app_header'] = 
lang($this->currentapp) . ' - ' . $appname . ': ' . $function_msg;
+                       
$GLOBALS['phpgw']->xslttpl->set_var('phpgw',array('edit_status' => $data));
+               //      $GLOBALS['phpgw']->xslttpl->pp();
+               }
+
+               function edit_category()
+               {
+                       $entity_id      = 
get_var('entity_id',array('POST','GET'));
+                       $id     = get_var('id',array('POST','GET'));
+                       $values                 = 
get_var('values',array('POST'));
+
+                       
$GLOBALS['phpgw']->xslttpl->add_file(array('admin_entity'));
+
+                       if ($values['save'])
+                       {
+                               $values['entity_id']    = $entity_id;
+
+                               if (!$values['name'])
+                               {
+                                       $receipt['error'][] = 
array('msg'=>lang('Name not entered!'));
+                               }
+                               if (!$values['entity_id'])
+                               {
+                                       $receipt['error'][] = 
array('msg'=>lang('Entity not choosen'));
+                               }
+
+                               if($id)
+                               {
+                                       $values['id']=$id;
+                                       $action='edit';
+                               }
+
+                               if (!$receipt['error'])
+                               {
+                                       $receipt = 
$this->bo->save_category($values,$action);
+                                       if(!$id)
+                                       {
+                                               $id=$receipt['id'];
+                                       }
+                               }
+                               else
+                               {
+                                       $receipt['error'][] = array('msg'=> 
lang('Category has NOT been saved'));
+                               }
+
+                       }
+
+                       if ($id)
+                       {
+                               $values = 
$this->bo->read_single_category($entity_id,$id);
+                               $function_msg = lang('edit category');
+                               $action='edit';
+                       }
+                       else
+                       {
+                               $function_msg = lang('add category');
+                               $action='add';
+                       }
+
+
+                       $link_data = array
+                       (
+                               'menuaction'    => 
$this->currentapp.'.uiadmin_entity.edit_category',
+                               'entity_id' =>$entity_id,
+                               'id'    => $id
+                       );
+//_debug_array($link_data);
+
+                       $entity = $this->bo->read_single($entity_id,false);
+
+                       $msgbox_data = $this->bocommon->msgbox_data($receipt);
+
+                       $data = array
+                       (
+                               'lang_entity'                                   
=> lang('entity'),
+                               'entity_name'                                   
=> $entity['name'],
+                               'msgbox_data'                                   
=> $GLOBALS['phpgw']->common->msgbox($msgbox_data),
+                               'lang_prefix_standardtext'              => 
lang('Enter a standard prefix for the id'),
+                               'lang_name_standardtext'                => 
lang('Enter a name of the standard'),
+
+                               'form_action'                                   
=> $GLOBALS['phpgw']->link('/index.php',$link_data),
+                               'done_action'                                   
=> 
$GLOBALS['phpgw']->link('/index.php','menuaction='.$this->currentapp.'.uiadmin_entity.category&entity_id='
 . $entity_id),
+                               'lang_id'                                       
        => lang('Category'),
+                               'lang_name'                                     
        => lang('Name'),
+                               'lang_descr'                                    
=> lang('Descr'),
+                               'lang_prefix'                                   
=> lang('Prefix'),
+                               'lang_save'                                     
        => lang('save'),
+                               'lang_done'                                     
        => lang('done'),
+                               'value_id'                                      
        => $id,
+                               'value_name'                                    
=> $values['name'],
+                               'value_prefix'                                  
=> $values['prefix'],
+                               'lang_id_standardtext'                  => 
lang('Enter the standard ID'),
+                               'lang_descr_standardtext'               => 
lang('Enter a description of the standard'),
+                               'lang_done_standardtext'                => 
lang('Back to the list'),
+                               'lang_save_standardtext'                => 
lang('Save the standard'),
+                               'type_id'                                       
        => $values['type_id'],
+                               'value_descr'                                   
=> $values['descr'],
+                               'lang_lookup_tenant'                    => 
lang('lookup tenant'),
+                               'value_lookup_tenant'                   => 
$values['lookup_tenant'],
+                               'lang_lookup_tenant_statustext' => lang('If 
this entity type is to look up tenants'),
+                               'lang_location_level'                   => 
lang('location level'),
+                               'location_level_list'                   => 
$this->bo->get_location_level_list($values['location_level']),
+                               'lang_location_level_statustext'        => 
lang('select location level'),
+                               'lang_no_location_level'                => 
lang('None'),
+                               'lang_tracking'                                 
=> lang('tracking'),
+                               'value_tracking'                                
=> $values['tracking'],
+                               'lang_tracking_statustext'              => 
lang('If this entity type is to be tracket in ticket list'),
+                               'lang_fileupload'                               
=> lang('Enable file upload'),
+                               'value_fileupload'                              
=> $values['fileupload'],
+                               'lang_fileupload_statustext'    => lang('If 
files can be uploaded for this category'),
+                               'lang_loc_link'                                 
=> lang('Link from location'),
+                               'value_loc_link'                                
=> $values['loc_link'],
+                               'lang_loc_link_statustext'              => 
lang('Enable link from location detail'),
+                               'lang_start_project'                    => 
lang('Start project'),
+                               'value_start_project'                   => 
$values['start_project'],
+                               'lang_start_project_statustext' => lang('Enable 
start project from this category')
+                       );
+
+                       $appname                                                
= lang('entity');
+
+                       $GLOBALS['phpgw_info']['flags']['app_header'] = 
lang($this->currentapp) . ' - ' . $appname . ': ' . $function_msg;
+                       
$GLOBALS['phpgw']->xslttpl->set_var('phpgw',array('edit' => $data));
+               //      $GLOBALS['phpgw']->xslttpl->pp();
+               }
+
+               function delete()
+               {
+                       $entity_id              = 
get_var('entity_id',array('POST','GET'));
+                       $cat_id         = get_var('cat_id',array('POST','GET'));
+                       $attrib_id              = 
get_var('attrib_id',array('POST','GET'));
+                       $status_id              = 
get_var('status_id',array('POST','GET'));
+                       $acl_location           = 
get_var('acl_location',array('POST','GET'));
+                       $custom_function_id     = 
get_var('custom_function_id',array('POST','GET'));
+                       $confirm        = get_var('confirm',array('POST'));
+
+                       if($attrib_id):
+                       {
+                               $function='list_attribute';
+                       }
+                       elseif($custom_function_id):
+                       {
+                               $function='list_custom_function';
+                       }
+                       endif;
+
+                       if (!$acl_location && $entity_id && $cat_id)
+                       {
+                               $acl_location = '.entity.' . $entity_id . '.' . 
$cat_id;
+                       }
+
+                       if(!$function)
+                       {
+                               if($cat_id)
+                               {
+                                       if($status_id)
+                                       {
+                                               $function='list_status';
+                                       }
+                                       else
+                                       {
+                                               $function='category';
+                                       }
+                               }
+                               else
+                               {
+                                       $function='index';
+                               }
+                       }
+
+
+                       $link_data = array
+                       (
+                               'menuaction' => 
$this->currentapp.'.uiadmin_entity.'.$function,
+                               'cat_id' => $cat_id,
+                               'entity_id' => $entity_id,
+                               'attrib_id' => $attrib_id,
+                               'status_id' => $status_id
+                       );
+
+                       $delete_data = array
+                       (
+                               'menuaction' => 
$this->currentapp.'.uiadmin_entity.delete',
+                               'cat_id' => $cat_id,
+                               'entity_id' => $entity_id,
+                               'attrib_id' => $attrib_id,
+                               'status_id' => $status_id,
+                               'acl_location' => $acl_location,
+                               'custom_function_id' => $custom_function_id
+                       );
+
+                       if (get_var('confirm',array('POST')))
+                       {
+                               
$this->bo->delete($cat_id,$entity_id,$attrib_id,$status_id,$acl_location,$custom_function_id);
+                               Header('Location: ' . 
$GLOBALS['phpgw']->link('/index.php',$link_data));
+                       }
+
+                       
$GLOBALS['phpgw']->xslttpl->add_file(array('app_delete'));
+
+                       $data = array
+                       (
+                               'done_action'                   => 
$GLOBALS['phpgw']->link('/index.php',$link_data),
+                               'delete_action'                 => 
$GLOBALS['phpgw']->link('/index.php',$delete_data),
+                               'lang_confirm_msg'              => lang('do you 
really want to delete this entry'),
+                               'lang_yes'                              => 
lang('yes'),
+                               'lang_yes_standardtext' => lang('Delete the 
entry'),
+                               'lang_no_standardtext'  => lang('Back to the 
list'),
+                               'lang_no'                               => 
lang('no')
+                       );
+
+                       $appname                                                
= lang('entity');
+                       $function_msg                                   = 
lang('delete entity type');
+
+                       $GLOBALS['phpgw_info']['flags']['app_header'] = 
lang($this->currentapp) . ' - ' . $appname . ': ' . $function_msg;
+                       
$GLOBALS['phpgw']->xslttpl->set_var('phpgw',array('delete' => $data));
+               //      $GLOBALS['phpgw']->xslttpl->pp();
+               }
+
+
+               function list_attribute()
+               {
+                       $entity_id      = $this->entity_id;
+                       $cat_id = $this->cat_id;
+                       $id     = get_var('id',array('POST','GET'));
+                       $resort = get_var('resort',array('POST','GET'));
+
+                       $GLOBALS['phpgw']->xslttpl->add_file(array(
+                                                                               
'admin_entity',
+                                                                               
'nextmatchs',
+                                                                               
'search_field'));
+
+                       if($resort)
+                       {
+                               $this->bo->resort_attrib($id,$resort);
+                       }
+                       $attrib_list = 
$this->bo->read_attrib($entity_id,$cat_id);
+
+                       if (isset($attrib_list) AND is_array($attrib_list))
+                       {
+                               foreach($attrib_list as $entry)
+                               {
+
+                                       $content[] = array
+                                       (
+                                               'name'                          
                => $entry['name'],
+                                               'datatype'                      
                => $entry['datatype'],
+                                               'column_name'                   
        => $entry['column_name'],
+                                               'input_text'                    
        => $entry['input_text'],
+                                               'sorting'                       
                => $entry['attrib_sort'],
+                                               'search'                        
                => $entry['search'],
+                                               'link_up'                       
                => 
$GLOBALS['phpgw']->link('/index.php','menuaction='.$this->currentapp.'.uiadmin_entity.list_attribute&resort=up&entity_id='
 . $entity_id . '&cat_id=' . $cat_id . '&id=' . $entry['id'] . '&allrows=' . 
$this->allrows),
+                                               'link_down'                     
                => 
$GLOBALS['phpgw']->link('/index.php','menuaction='.$this->currentapp.'.uiadmin_entity.list_attribute&resort=down&entity_id='
 . $entity_id . '&cat_id=' . $cat_id . '&id=' . $entry['id'] . '&allrows=' . 
$this->allrows),
+                                               'link_edit'                     
                => 
$GLOBALS['phpgw']->link('/index.php','menuaction='.$this->currentapp.'.uiadmin_entity.edit_attrib&entity_id='
 . $entity_id . '&cat_id=' . $cat_id . '&id=' . $entry['id']),
+                                               'link_delete'                   
        => 
$GLOBALS['phpgw']->link('/index.php','menuaction='.$this->currentapp.'.uiadmin_entity.delete&entity_id='
 . $entity_id . '&cat_id=' . $cat_id . '&attrib_id=' . $entry['id']),
+                                               'lang_up_text'                  
        => lang('shift up'),
+                                               'lang_down_text'                
        => lang('shift down'),
+                                               'lang_edit_text'                
        => lang('edit the attrib'),
+                                               'lang_delete_text'              
        => lang('delete the attrib'),
+                                               'text_attribute'                
        => lang('Attributes'),
+                                               'text_up'                       
                => lang('up'),
+                                               'text_down'                     
                => lang('down'),
+                                               'text_edit'                     
                => lang('edit'),
+                                               'text_delete'                   
        => lang('delete')
+                                       );
+                               }
+                       }
+
+//_debug_array($content);
+
+                       $table_header[] = array
+                       (
+                               'lang_descr'            => lang('Descr'),
+                               'lang_datatype'         => lang('Datatype'),
+                               'lang_sorting'          => lang('sorting'),
+                               'lang_search'           => lang('search'),
+                               'lang_edit'                     => lang('edit'),
+                               'lang_delete'           => lang('delete'),
+                               'sort_name'     => 
$this->nextmatchs->show_sort_order(array
+                                                                               
(
+                                                                               
        'sort'  => $this->sort,
+                                                                               
        'var'   =>      'column_name',
+                                                                               
        'order' =>      $this->order,
+                                                                               
        'extra'         => array('menuaction'   => 
$this->currentapp.'.uiadmin_entity.list_attribute',
+                                                                               
                                                        'entity_id'     
=>$entity_id,
+                                                                               
                                                        'cat_id'        
=>$cat_id,
+                                                                               
                                                'allrows'=>$this->allrows)
+                                                                               
)),
+                               'sort_sorting'  => 
$this->nextmatchs->show_sort_order(array
+                                                                               
(
+                                                                               
        'sort'  => $this->sort,
+                                                                               
        'var'   =>      'attrib_sort',
+                                                                               
        'order' =>      $this->order,
+                                                                               
        'extra'         => array('menuaction'   => 
$this->currentapp.'.uiadmin_entity.list_attribute',
+                                                                               
                                                        'entity_id'     
=>$entity_id,
+                                                                               
                                                        'cat_id'        
=>$cat_id,
+                                                                               
                                                'allrows'=>$this->allrows)
+                                                                               
)),
+                               'lang_name'     => lang('Name'),
+                       );
+
+                       $table_add[] = array
+                       (
+                               'lang_add'                              => 
lang('add'),
+                               'lang_add_attribtext'   => lang('add a attrib'),
+                               'add_action'                    => 
$GLOBALS['phpgw']->link('/index.php','menuaction='.$this->currentapp.'.uiadmin_entity.edit_attrib&entity_id='.$entity_id
 . '&cat_id=' . $cat_id),
+                               'lang_done'                             => 
lang('done'),
+                               'lang_done_attribtext'  => lang('back to 
admin'),
+                               'done_action'                   => 
$GLOBALS['phpgw']->link('/index.php','menuaction='.$this->currentapp.'.uiadmin_entity.category&entity_id='.$entity_id),
+                       );
+
+                       if(!$this->allrows)
+                       {
+                               $record_limit   = 
$GLOBALS['phpgw_info']['user']['preferences']['common']['maxmatchs'];
+                       }
+                       else
+                       {
+                               $record_limit   = $this->bo->total_records;
+                       }
+
+                       $link_data = array
+                       (
+                               'menuaction'    => 
$this->currentapp.'.uiadmin_entity.list_attribute',
+                                               'sort'                  
=>$this->sort,
+                                               'order'                 
=>$this->order,
+                                               'query'                 
=>$this->query,
+                                               'entity_id'             
=>$entity_id,
+                                               'cat_id'                
=>$cat_id
+                       );
+
+                       $entity = $this->bo->read_single($entity_id,false);
+                       $category = 
$this->bo->read_single_category($entity_id,$cat_id);
+
+                       $data = array
+                       (
+                               'lang_entity'                                   
=> lang('entity'),
+                               'entity_name'                                   
=> $entity['name'],
+                               'lang_category'                                 
=> lang('category'),
+                               'category_name'                                 
=> $category['name'],
+                               'allow_allrows'                                 
=> True,
+                               'allrows'                                       
        => $this->allrows,
+                               'start_record'                                  
=> $this->start,
+                               'record_limit'                                  
=> $record_limit,
+                               'start_record'                                  
=> $this->start,
+                               'num_records'                                   
=> count($attrib_list),
+                               'all_records'                                   
=> $this->bo->total_records,
+                               'link_url'                                      
        => $GLOBALS['phpgw']->link('/index.php',$link_data),
+                               'img_path'                                      
        => $GLOBALS['phpgw']->common->get_image_path('phpgwapi','default'),
+                               'lang_searchfield_attribtext'   => lang('Enter 
the search string. To show all entries, empty this field and press the SUBMIT 
button again'),
+                               'lang_searchbutton_attribtext'  => lang('Submit 
the search string'),
+                               'query'                                         
        => $this->query,
+                               'lang_search'                                   
=> lang('search'),
+                               'table_header_attrib'                   => 
$table_header,
+                               'values_attrib'                                 
=> $content,
+                               'table_add'                                     
        => $table_add
+                       );
+
+                       $appname                                                
= lang('attribute');
+                       $function_msg                                   = 
lang('list entity attribute');
+
+                       $GLOBALS['phpgw_info']['flags']['app_header'] = 
lang($this->currentapp) . ' - ' . $appname . ': ' . $function_msg;
+                       
$GLOBALS['phpgw']->xslttpl->set_var('phpgw',array('list_attribute' => $data));
+               //      $GLOBALS['phpgw']->xslttpl->pp();
+                       $this->save_sessiondata();
+               }
+
+               function edit_attrib()
+               {
+                       $entity_id      = 
get_var('entity_id',array('POST','GET'));
+                       $cat_id         = get_var('cat_id',array('POST','GET'));
+                       $id                     = 
get_var('id',array('POST','GET'));
+                       $values         = get_var('values',array('POST'));
+
+                       
$GLOBALS['phpgw']->xslttpl->add_file(array('admin_entity'));
+
+                       if ($values['save'])
+                       {
+                               if($id)
+                               {
+                                       $values['id']=$id;
+                                       $action='edit';
+                               }
+
+                               $values['entity_id']=$entity_id;
+                               $values['cat_id']=$cat_id;
+
+                               if (!$values['column_name'])
+                               {
+                                       $receipt['error'][] = 
array('msg'=>lang('Column name not entered!'));
+                               }
+
+                               if (!$values['input_text'])
+                               {
+                                       $receipt['error'][] = 
array('msg'=>lang('Input text not entered!'));
+                               }
+                               if (!$values['statustext'])
+                               {
+                                       $receipt['error'][] = 
array('msg'=>lang('Statustext not entered!'));
+                               }
+
+                               if (!$values['entity_id'])
+                               {
+                                       $receipt['error'][] = 
array('msg'=>lang('entity type not choosen!'));
+                               }
+
+                               if (!$values['column_info']['type'])
+                               {
+                                       $receipt['error'][] = 
array('msg'=>lang('Datatype type not choosen!'));
+                               }
+
+                               
if(!ctype_digit($values['column_info']['precision']))
+                               {
+                                       
$receipt['error'][]=array('msg'=>lang('Please enter precision as integer !'));
+                                       
unset($values['column_info']['precision']);
+                               }
+
+                               if($values['column_info']['scale'] && 
!ctype_digit($values['column_info']['scale']))
+                               {
+                                       
$receipt['error'][]=array('msg'=>lang('Please enter scale as integer !'));
+                                       unset($values['column_info']['scale']);
+                               }
+
+                               if (!$values['column_info']['nullable'])
+                               {
+                                       $receipt['error'][] = 
array('msg'=>lang('Nullable not choosen!'));
+                               }
+
+
+                               if (!$receipt['error'])
+                               {
+
+                                       $receipt = 
$this->bo->save_attrib($values,$action);
+
+                                       if(!$id)
+                                       {
+                                               $id=$receipt['id'];
+                                       }
+                               }
+                               else
+                               {
+                                       $receipt['error'][] = array('msg'       
=> lang('Attribute has NOT been saved'));
+                               }
+
+                       }
+
+                       if ($id)
+                       {
+                               $values = 
$this->bo->read_single_attrib($entity_id,$cat_id,$id);
+                               $type_name=$values['type_name'];
+                               $function_msg = lang('edit attribute'). ' ' . 
lang($type_name);
+                               $action='edit';
+                       }
+                       else
+                       {
+                               $function_msg = lang('add attribute');
+                               $action='add';
+                       }
+
+                       $link_data = array
+                       (
+                               'menuaction'    => 
$this->currentapp.'.uiadmin_entity.edit_attrib',
+                               'entity_id'     => $entity_id,
+                               'cat_id'        => $cat_id,
+                               'id'    => $id
+                       );
+
+                       if($values['column_info']['type']=='R' || 
$values['column_info']['type']=='CH' || $values['column_info']['type']=='LB')
+                       {
+                               $multiple_choice= True;
+                       }
+
+//_debug_array($values);
+
+                       $entity = $this->bo->read_single($entity_id,false);
+                       $category = 
$this->bo->read_single_category($entity_id,$cat_id);
+
+                       $msgbox_data = $this->bocommon->msgbox_data($receipt);
+
+                       $data = array
+                       (
+                               'lang_entity'                                   
=> lang('entity'),
+                               'entity_name'                                   
=> $entity['name'],
+                               'lang_category'                                 
=> lang('category'),
+                               'category_name'                                 
=> $category['name'],
+
+                               'lang_choice'                           => 
lang('Choice'),
+                               'lang_new_value'                        => 
lang('New value'),
+                               'lang_new_value_statustext'     => lang('New 
value for mulitple choice'),
+                               'multiple_choice'                       => 
$multiple_choice,
+                               'value_choice'                          => 
$values['choice'],
+                               'lang_delete_value'                     => 
lang('Delete value'),
+                               'lang_value'                            => 
lang('value'),
+                               'lang_delete_choice_statustext'=> lang('Delete 
this value from the list of multiple choice'),
+
+                               'msgbox_data'                           => 
$GLOBALS['phpgw']->common->msgbox($msgbox_data),
+                               'form_action'                           => 
$GLOBALS['phpgw']->link('/index.php',$link_data),
+                               'done_action'                           => 
$GLOBALS['phpgw']->link('/index.php','menuaction='.$this->currentapp.'.uiadmin_entity.list_attribute&entity_id='.$entity_id
 . '&cat_id='.$cat_id),
+                               'lang_id'                                       
=> lang('Attribute ID'),
+                               'lang_entity_type'                      => 
lang('Entity type'),
+                               'lang_no_entity_type'           => lang('No 
entity type'),
+                               'lang_save'                                     
=> lang('save'),
+                               'lang_done'                                     
=> lang('done'),
+                               'value_id'                                      
=> $id,
+
+                               'lang_column_name'                              
=> lang('Column name'),
+                               'value_column_name'                             
=> $values['column_name'],
+                               'lang_column_name_statustext'   => lang('enter 
the name for the column'),
+
+                               'lang_input_text'                               
=> lang('input text'),
+                               'value_input_text'                              
=> $values['input_text'],
+                               'lang_input_name_statustext'    => lang('enter 
the input text for records'),
+
+                               'lang_id_attribtext'            => lang('Enter 
the attribute ID'),
+                               'lang_entity_statustext'        => lang('Select 
a entity type'),
+
+                               'lang_statustext'                       => 
lang('Statustext'),
+                               'lang_statustext_attribtext'=> lang('Enter a 
statustext for the inputfield in forms'),
+                               'value_statustext'                      => 
$values['statustext'],
+
+                               'lang_done_attribtext'          => lang('Back 
to the list'),
+                               'lang_save_attribtext'          => lang('Save 
the attribute'),
+
+                               'lang_datatype'                         => 
lang('Datatype'),
+                               'lang_datatype_statustext'      => lang('Select 
a datatype'),
+                               'lang_no_datatype'                      => 
lang('No datatype'),
+                               'datatype_list'                         => 
$this->bocommon->select_datatype($values['column_info']['type']),
+
+                               'lang_precision'                        => 
lang('Precision'),
+                               'lang_precision_statustext'     => lang('enter 
the record length'),
+                               'value_precision'                       => 
$values['column_info']['precision'],
+
+                               'lang_scale'                            => 
lang('scale'),
+                               'lang_scale_statustext'         => lang('enter 
the scale if type is decimal'),
+                               'value_scale'                           => 
$values['column_info']['scale'],
+
+                               'lang_default'                          => 
lang('default'),
+                               'lang_default_statustext'       => lang('enter 
the default value'),
+                               'value_default'                         => 
$values['column_info']['default'],
+
+                               'lang_nullable'                         => 
lang('Nullable'),
+                               'lang_nullable_statustext'      => lang('Chose 
if this column is nullable'),
+                               'lang_select_nullable'          => lang('Select 
nullable'),
+                               'nullable_list'                         => 
$this->bocommon->select_nullable($values['column_info']['nullable']),
+                               'value_lookup_form'                     => 
$values['lookup_form'],
+                               'lang_lookup_form'                              
=> lang('show in lookup forms'),
+                               'lang_lookup_form_statustext'                   
=> lang('check to show this attribue in lookup forms'),
+                               'value_list'                    => 
$values['list'],
+                               'lang_list'                             => 
lang('show in list'),
+                               'lang_list_statustext'                  => 
lang('check to show this attribute in entity list'),
+                               'value_search'                          => 
$values['search'],
+                               'lang_include_search'           => 
lang('Include in search'),
+                               'lang_include_search_statustext'=> lang('check 
to show this attribute in location list'),
+
+                       );
+//_debug_array($values);
+
+                       $appname                                                
= lang('entity');
+
+                       $GLOBALS['phpgw_info']['flags']['app_header'] = 
lang($this->currentapp) . ' - ' . $appname . ': ' . $function_msg;
+                       
$GLOBALS['phpgw']->xslttpl->set_var('phpgw',array('edit_attrib' => $data));
+               //      $GLOBALS['phpgw']->xslttpl->pp();
+               }
+
+               function list_custom_function()
+               {
+                       $entity_id      = $this->entity_id;
+                       $cat_id = $this->cat_id;
+                       $id     = get_var('id',array('POST','GET'));
+                       $resort = get_var('resort',array('POST','GET'));
+
+                       $GLOBALS['phpgw']->xslttpl->add_file(array(
+                                                                               
'admin_entity',
+                                                                               
'nextmatchs',
+                                                                               
'search_field'));
+
+                       if($resort)
+                       {
+                               $this->bo->resort_custom_function($id,$resort);
+                       }
+                       $custom_function_list = 
$this->bo->read_custom_function($entity_id,$cat_id);
+
+                       if (isset($custom_function_list) AND 
is_array($custom_function_list))
+                       {
+                               foreach($custom_function_list as $entry)
+                               {
+
+                                       $content[] = array
+                                       (
+                                               'file_name'                     
                        => $entry['file_name'],
+                                               'descr'                         
        => $entry['descr'],
+                                               'sorting'                       
                => $entry['sorting'],
+                                               'active'                        
                => $entry['active']?'X':'',
+                                               'link_up'                       
                => 
$GLOBALS['phpgw']->link('/index.php','menuaction='.$this->currentapp.'.uiadmin_entity.list_custom_function&resort=up&entity_id='
 . $entity_id . '&cat_id=' . $cat_id . '&id=' . $entry['id'] . '&allrows=' . 
$this->allrows),
+                                               'link_down'                     
                => 
$GLOBALS['phpgw']->link('/index.php','menuaction='.$this->currentapp.'.uiadmin_entity.list_custom_function&resort=down&entity_id='
 . $entity_id . '&cat_id=' . $cat_id . '&id=' . $entry['id'] . '&allrows=' . 
$this->allrows),
+                                               'link_edit'                     
                => 
$GLOBALS['phpgw']->link('/index.php','menuaction='.$this->currentapp.'.uiadmin_entity.edit_custom_function&entity_id='
 . $entity_id . '&cat_id=' . $cat_id . '&id=' . $entry['id']),
+                                               'link_delete'                   
        => 
$GLOBALS['phpgw']->link('/index.php','menuaction='.$this->currentapp.'.uiadmin_entity.delete&entity_id='
 . $entity_id . '&cat_id=' . $cat_id . '&custom_function_id=' . $entry['id']),
+                                               'lang_up_text'                  
        => lang('shift up'),
+                                               'lang_down_text'                
        => lang('shift down'),
+                                               'lang_edit_text'                
        => lang('edit the custom_function'),
+                                               'lang_delete_text'              
        => lang('delete the custom_function'),
+                                               'text_custom_function'          
        => lang('custom_functions'),
+                                               'text_up'                       
                => lang('up'),
+                                               'text_down'                     
                => lang('down'),
+                                               'text_edit'                     
                => lang('edit'),
+                                               'text_delete'                   
        => lang('delete')
+                                       );
+                               }
+                       }
+
+                       $table_header[] = array
+                       (
+                               'lang_descr'            => lang('Descr'),
+                               'lang_active'           => lang('Active'),
+                               'lang_sorting'          => lang('sorting'),
+                               'lang_search'           => lang('search'),
+                               'lang_edit'                     => lang('edit'),
+                               'lang_delete'           => lang('delete'),
+                               'sort_name'     => 
$this->nextmatchs->show_sort_order(array
+                                                                               
(
+                                                                               
        'sort'  => $this->sort,
+                                                                               
        'var'   =>      'column_name',
+                                                                               
        'order' =>      $this->order,
+                                                                               
        'extra'         => array('menuaction'   => 
$this->currentapp.'.uiadmin_entity.list_custom_function',
+                                                                               
                                                        'entity_id'     
=>$entity_id,
+                                                                               
                                                        'cat_id'        
=>$cat_id,
+                                                                               
                                                'allrows'=>$this->allrows)
+                                                                               
)),
+                               'sort_sorting'  => 
$this->nextmatchs->show_sort_order(array
+                                                                               
(
+                                                                               
        'sort'  => $this->sort,
+                                                                               
        'var'   =>      'custom_function_sort',
+                                                                               
        'order' =>      $this->order,
+                                                                               
        'extra'         => array('menuaction'   => 
$this->currentapp.'.uiadmin_entity.list_custom_function',
+                                                                               
                                                        'entity_id'     
=>$entity_id,
+                                                                               
                                                        'cat_id'        
=>$cat_id,
+                                                                               
                                                'allrows'=>$this->allrows)
+                                                                               
)),
+                               'lang_name'     => lang('Name'),
+                       );
+
+                       $table_add[] = array
+                       (
+                               'lang_add'                              => 
lang('add'),
+                               'lang_add_custom_functiontext'  => lang('add a 
custom_function'),
+                               'add_action'                    => 
$GLOBALS['phpgw']->link('/index.php','menuaction='.$this->currentapp.'.uiadmin_entity.edit_custom_function&entity_id='.$entity_id
 . '&cat_id=' . $cat_id),
+                               'lang_done'                             => 
lang('done'),
+                               'lang_done_custom_functiontext' => lang('back 
to admin'),
+                               'done_action'                   => 
$GLOBALS['phpgw']->link('/index.php','menuaction='.$this->currentapp.'.uiadmin_entity.category&entity_id='.$entity_id),
+                       );
+
+                       if(!$this->allrows)
+                       {
+                               $record_limit   = 
$GLOBALS['phpgw_info']['user']['preferences']['common']['maxmatchs'];
+                       }
+                       else
+                       {
+                               $record_limit   = $this->bo->total_records;
+                       }
+
+                       $link_data = array
+                       (
+                               'menuaction'    => 
$this->currentapp.'.uiadmin_entity.list_custom_function',
+                                               'sort'                  
=>$this->sort,
+                                               'order'                 
=>$this->order,
+                                               'query'                 
=>$this->query,
+                                               'entity_id'             
=>$entity_id,
+                                               'cat_id'                
=>$cat_id
+                       );
+
+                       $entity = $this->bo->read_single($entity_id,false);
+                       $category = 
$this->bo->read_single_category($entity_id,$cat_id);
+
+                       $data = array
+                       (
+                               'lang_entity'                                   
=> lang('entity'),
+                               'entity_name'                                   
=> $entity['name'],
+                               'lang_category'                                 
=> lang('category'),
+                               'category_name'                                 
=> $category['name'],
+                               'allow_allrows'                                 
=> True,
+                               'allrows'                                       
        => $this->allrows,
+                               'start_record'                                  
=> $this->start,
+                               'record_limit'                                  
=> $record_limit,
+                               'start_record'                                  
=> $this->start,
+                               'num_records'                                   
=> count($custom_function_list),
+                               'all_records'                                   
=> $this->bo->total_records,
+                               'link_url'                                      
        => $GLOBALS['phpgw']->link('/index.php',$link_data),
+                               'img_path'                                      
        => $GLOBALS['phpgw']->common->get_image_path('phpgwapi','default'),
+                               'lang_searchfield_custom_functiontext'  => 
lang('Enter the search string. To show all entries, empty this field and press 
the SUBMIT button again'),
+                               'lang_searchbutton_custom_functiontext' => 
lang('Submit the search string'),
+                               'query'                                         
        => $this->query,
+                               'lang_search'                                   
=> lang('search'),
+                               'table_header_custom_function'                  
=> $table_header,
+                               'values_custom_function'                        
                => $content,
+                               'table_add'                                     
        => $table_add
+                       );
+
+                       $appname                                                
= lang('custom function');
+                       $function_msg                                   = 
lang('list entity custom function');
+
+                       $GLOBALS['phpgw_info']['flags']['app_header'] = 
lang($this->currentapp) . ' - ' . $appname . ': ' . $function_msg;
+                       
$GLOBALS['phpgw']->xslttpl->set_var('phpgw',array('list_custom_function' => 
$data));
+               //      $GLOBALS['phpgw']->xslttpl->pp();
+                       $this->save_sessiondata();
+               }
+
+               function edit_custom_function()
+               {
+                       $entity_id      = 
get_var('entity_id',array('POST','GET'));
+                       $cat_id         = get_var('cat_id',array('POST','GET'));
+                       $id                     = 
get_var('id',array('POST','GET'));
+                       $values         = get_var('values',array('POST'));
+
+                       
$GLOBALS['phpgw']->xslttpl->add_file(array('admin_entity'));
+
+                       if ($values['save'])
+                       {
+                               if($id)
+                               {
+                                       $values['id']=$id;
+                                       $action='edit';
+                               }
+
+                               $values['entity_id']=$entity_id;
+                               $values['cat_id']=$cat_id;
+
+
+                               if (!$values['entity_id'])
+                               {
+                                       $receipt['error'][] = 
array('msg'=>lang('entity type not choosen!'));
+                               }
+
+                               if (!$values['custom_function_file'])
+                               {
+                                       $receipt['error'][] = 
array('msg'=>lang('custom function file not choosen!'));
+                               }
+
+
+                               if (!$receipt['error'])
+                               {
+
+                                       $receipt = 
$this->bo->save_custom_function($values,$action);
+
+                                       if(!$id)
+                                       {
+                                               $id=$receipt['id'];
+                                       }
+                               }
+                               else
+                               {
+                                       $receipt['error'][] = array('msg'       
=> lang('Custom function has NOT been saved'));
+                               }
+
+                       }
+
+                       if ($id)
+                       {
+                               $values = 
$this->bo->read_single_custom_function($entity_id,$cat_id,$id);
+                               $type_name=$values['type_name'];
+                               $function_msg = lang('edit custom function'). ' 
' . lang($type_name);
+                               $action='edit';
+                       }
+                       else
+                       {
+                               $function_msg = lang('add custom function');
+                               $action='add';
+                       }
+
+                       $link_data = array
+                       (
+                               'menuaction'    => 
$this->currentapp.'.uiadmin_entity.edit_custom_function',
+                               'entity_id'     => $entity_id,
+                               'cat_id'        => $cat_id,
+                               'id'    => $id
+                       );
+
+
+//_debug_array($values);
+
+                       $entity = $this->bo->read_single($entity_id,false);
+                       $category = 
$this->bo->read_single_category($entity_id,$cat_id);
+
+                       $msgbox_data = $this->bocommon->msgbox_data($receipt);
+
+                       $data = array
+                       (
+                               'lang_entity'                                   
=> lang('entity'),
+                               'entity_name'                                   
=> $entity['name'],
+                               'lang_category'                                 
=> lang('category'),
+                               'category_name'                                 
=> $category['name'],
+
+                               'msgbox_data'                           => 
$GLOBALS['phpgw']->common->msgbox($msgbox_data),
+                               'form_action'                           => 
$GLOBALS['phpgw']->link('/index.php',$link_data),
+                               'done_action'                           => 
$GLOBALS['phpgw']->link('/index.php','menuaction='.$this->currentapp.'.uiadmin_entity.list_custom_function&entity_id='.$entity_id
 . '&cat_id='.$cat_id),
+                               'lang_id'                                       
=> lang('Custom function ID'),
+                               'lang_entity_type'                      => 
lang('Entity type'),
+                               'lang_no_entity_type'           => lang('No 
entity type'),
+                               'lang_save'                                     
=> lang('save'),
+                               'lang_done'                                     
=> lang('done'),
+                               'value_id'                                      
=> $id,
+
+                               'lang_descr'                    => 
lang('descr'),
+                               'lang_descr_custom_functiontext'=> lang('Enter 
a descr for the custom function'),
+                               'value_descr'                   => 
$values['descr'],
+
+                               'lang_done_custom_functiontext'         => 
lang('Back to the list'),
+                               'lang_save_custom_functiontext'         => 
lang('Save the custom function'),
+
+                               'lang_custom_function'                          
=> lang('custom function'),
+                               'lang_custom_function_statustext'       => 
lang('Select a custom_function'),
+                               'lang_no_custom_function'                       
=> lang('No custom function'),
+                               'custom_function_list'                          
=> $this->bo->select_custom_function($values['custom_function_file']),
+
+                               'value_active'                  => 
$values['active'],
+                               'lang_active'                           => 
lang('Active'),
+                               'lang_active_statustext'        => lang('check 
acivate custom function'),
+                               'value_search'                          => 
$values['search'],
+                               'lang_include_search'           => 
lang('Include in search'),
+                               'lang_include_search_statustext'=> lang('check 
to show this custom function in location list')
+                       );
+
+                       $appname                                                
= lang('entity');
+
+                       $GLOBALS['phpgw_info']['flags']['app_header'] = 
lang($this->currentapp) . ' - ' . $appname . ': ' . $function_msg;
+                       
$GLOBALS['phpgw']->xslttpl->set_var('phpgw',array('edit_custom_function' => 
$data));
+               }
+
+       }
+?>




reply via email to

[Prev in Thread] Current Thread [Next in Thread]