phpgroupware-cvs
[Top][All Lists]
Advanced

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

[Phpgroupware-cvs] property/class.soproject.php, 1.1.1.3


From: nomail
Subject: [Phpgroupware-cvs] property/class.soproject.php, 1.1.1.3
Date: Fri, 21 May 2004 16:03:48 -0000

Update of /property
Modified Files:
        Branch: 
          class.soproject.php

date: 2004/04/23 21:26:33;  author: sigurdne;  state: Exp;  lines: +841 -841

Log Message:
no message
=====================================================================
Index: property/class.soproject.php
diff -u property/class.soproject.php:1.1.1.2 
property/class.soproject.php:1.1.1.3
--- property/class.soproject.php:1.1.1.2        Fri Apr 23 20:25:57 2004
+++ property/class.soproject.php        Fri Apr 23 21:26:33 2004
@@ -1,841 +1,841 @@
-<?php
-       
/**************************************************************************\
-       * phpGroupWare - property                                               
   *
-       * http://www.phpgroupware.org                                           
   *
-       *                                                                       
   *
-       * Facilities Management                                                 
   *
-       * Written by Sigurd Nes [sigurdne at online.no]                         
   *
-       * 
------------------------------------------------------------------------ *
-       * Copyright 2000 - 2003 Free Software Foundation, Inc                   
   *
-       * This program is part of the GNU project, see http://www.gnu.org/      
   *
-       * 
------------------------------------------------------------------------ *
-       * This program is free software; you can redistribute it and/or modify 
it  *
-       * under the terms of the GNU General Public License as published by the 
   *
-       * Free Software Foundation; either version 2 of the License, or (at 
your   *
-       * option) any later version.                                            
   *
-       
\**************************************************************************/
-
-       class property_soproject
-       {
-
-               function property_soproject()
-               {
-                       $this->currentapp       = 'property'; 
//$GLOBALS['phpgw_info']['flags']['currentapp'];
-                       $this->db                       = $GLOBALS['phpgw']->db;
-                       $this->db2                      = $this->db;
-                       $this->account          = 
$GLOBALS['phpgw_data']['user']['id'];
-                       $this->bocommon         = 
CreateObject($this->currentapp.'_bocommon');
-                       $this->fm_session       = 
CreateObject($this->currentapp.'_session');
-
-                       $this->join                     = $this->bocommon->join;
-
-                       $this->grants   = 
$this->fm_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');
-                               
$this->fm_session->appsession('grants_project',$this->currentapp,$this->grants);
-                       }
-               }
-
-
-               function select_category_workorder_list()
-               {
-
-                       $dbresult = $GLOBALS['phpgw']->db->Execute("SELECT id, 
descr FROM fm_workorder_category  ORDER BY descr ");
-
-                       $i = 0;
-                       while (!$dbresult->EOF)
-                       {
-                               $categories[$i]['id']                           
= $dbresult->fields['id'];
-                               $categories[$i]['name']                         
= stripslashes($dbresult->fields['descr']);
-                               $i++;
-                       }
-                       return $categories;
-               }
-
-               function read_single_project_category($id='')
-               {
-                       $dbresult = $GLOBALS['phpgw']->db->Execute("SELECT 
descr FROM fm_workorder_category where id='$id' ");
-
-                       return $dbresult->fields['descr'];
-               }
-
-               function select_status_list()
-               {
-                       $dbresult = $GLOBALS['phpgw']->db->Execute("SELECT id, 
descr FROM fm_workorder_status ORDER BY id ");
-
-                       $i = 0;
-                       while (!$dbresult->EOF)
-                       {
-                               $status_entries[$i]['id']                       
        = $dbresult->fields['id'];
-                               $status_entries[$i]['name']                     
        = stripslashes($dbresult->fields['descr']);
-                               $i++;
-                       }
-                       return $status_entries;
-               }
-
-               function select_branch_list()
-               {
-                       $dbresult = $GLOBALS['phpgw']->db->Execute("SELECT id, 
descr FROM fm_branch ORDER BY id ");
-
-                       $i = 0;
-                       while (!$dbresult->EOF)
-                       {
-                               $branch_entries[$i]['id']                       
        = $dbresult->fields['id'];
-                               $branch_entries[$i]['name']                     
        = stripslashes($dbresult->fields['descr']);
-                               $i++;
-                       }
-                       return $branch_entries;
-               }
-
-               function select_key_location_list()
-               {
-                       $dbresult = $GLOBALS['phpgw']->db->Execute("SELECT id, 
descr FROM fm_key_loc ORDER BY descr ");
-
-                       $i = 0;
-                       while (!$dbresult->EOF)
-                       {
-                               $key_location_entries[$i]['id']                 
        = $dbresult->fields['id'];
-                               $key_location_entries[$i]['name']               
        = stripslashes($dbresult->fields['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']:'');
-                       }
-
-                       $sql = $this->bocommon->fm_cache('sql_project');
-
-                       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 .='(';
-
-                               $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',$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',$this->uicols);
-                               
$this->bocommon->fm_cache('cols_return_project',$cols_return);
-                               
$this->bocommon->fm_cache('type_id_project',$type_id);
-                               
$this->bocommon->fm_cache('cols_extra_project',$this->cols_extra);
-
-                       }
-                       else
-                       {
-                               $this->uicols           = 
$this->bocommon->fm_cache('uicols_project');
-                               $cols_return            = 
$this->bocommon->fm_cache('cols_return_project');
-                               $type_id                        = 
$this->bocommon->fm_cache('type_id_project');
-                               $this->cols_extra       = 
$this->bocommon->fm_cache('cols_extra_project');
-                       }
-
-                       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 ($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)
-                       {
-                               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 LIKE '%$query%' or fm_project.address LIKE '%$query%' or 
fm_project.location_code LIKE '%$query%' or fm_project.id LIKE '%$query%')";
-                               }
-                       }
-
-                       $sql .= " $filtermethod $querymethod";
-//echo $sql;
-                       $dbresult2 = $GLOBALS['phpgw']->db->Execute($sql);
-                       $this->total_records = $dbresult2->_numOfRows;
-
-                       $maxmatchs = 15;
-
-                       if(!$allrows)
-                       {
-                               $dbresult = 
$GLOBALS['phpgw']->db->SelectLimit($sql . $ordermethod,$maxmatchs,$start);
-
-                       }
-                       else
-                       {
-                               $dbresult = $GLOBALS['phpgw']->db->Execute($sql 
. $ordermethod);
-                       }
-
-
-                       $j=0;
-                       $k=count($cols_return);
-                       while (!$dbresult->EOF)
-                       {
-                               for ($i=0;$i<$k;$i++)
-                               {
-                                       $project_list[$j][$cols_return[$i]] = 
$dbresult->fields[$cols_return[$i]];
-                                       $project_list[$j]['grants'] = 
(int)$this->grants[$dbresult->fields['user_id']];
-
-                               }
-
-                               $location_code= 
$dbresult->fields['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++;
-                       }
-
-//html_print_r($project_list);
-                       return $project_list;
-               }
-
-               function read_single($project_id)
-               {
-
-                       $sql = "SELECT * from fm_project where 
id='$project_id'";
-
-                       $dbresult = $GLOBALS['phpgw']->db->Execute($sql);
-
-                       if (!$dbresult->EOF)
-                       {
-                               $project['project_id']                  = 
$dbresult->fields['id'];
-                               $project['title']                               
= $dbresult->fields['title'];
-                               $project['name']                                
= $dbresult->fields['name'];
-                               $project['location_code']               = 
$dbresult->fields['location_code'];
-                               $project['key_fetch']                   = 
$dbresult->fields['key_fetch'];
-                               $project['key_deliver']                 = 
$dbresult->fields['key_deliver'];
-                               $project['other_branch']                = 
$dbresult->fields['other_branch'];
-                               $project['key_responsible']             = 
$dbresult->fields['key_responsible'];
-                               $project['charge_tenant']               = 
$dbresult->fields['charge_tenant'];
-                               $project['descr']                               
= $dbresult->fields['descr'];
-                               $project['status']                              
= $dbresult->fields['status'];
-                               $project['budget']                              
= (int)$dbresult->fields['budget'];
-                               $project['reserve']                             
= (int)$dbresult->fields['reserve'];
-                               $project['tenant_id']                   = 
$dbresult->fields['tenant_id'];
-                               $project['user_id']                             
= $dbresult->fields['user_id'];
-                               $project['coordinator']                 = 
$dbresult->fields['coordinator'];
-                               $project['access']                              
= $dbresult->fields['access'];
-                               $project['start_date']                  = 
$dbresult->fields['start_date'];
-                               $project['end_date']                    = 
$dbresult->fields['end_date'];
-                               $project['cat_id']                              
= $dbresult->fields['category'];
-                               $project['grants']                              
= (int)$this->grants[$dbresult->fields['user_id']];
-                               $project['p_num']                               
= $dbresult->fields['p_num'];
-                               $project['p_entity_id']                 = 
$dbresult->fields['p_entity_id'];
-                               $project['p_cat_id']                    = 
$dbresult->fields['p_cat_id'];
-
-                               $project['power_meter'] = 
$this->get_power_meter($dbresult->fields['location_code']);
-                       }
-
-                       $sql = "SELECT * FROM fm_project_origin WHERE 
project_id='$project_id' and origin ='request'";
-
-                       $dbresult = $GLOBALS['phpgw']->db->Execute($sql);
-
-                       $i=0;
-                       while (!$dbresult->EOF)
-                       {
-                               $project['request_id'][$i]['id']        = 
$dbresult->fields['origin_id'];
-                               $i++;
-                       }
-
-                       $sql = "SELECT * FROM fm_project_origin WHERE 
project_id='$project_id' and origin ='tts'";
-
-                       $dbresult = $GLOBALS['phpgw']->db->Execute($sql);
-
-                       while (!$dbresult->EOF)
-                       {
-                               $project['ticket_id'][$i]['id'] = 
$dbresult->fields['origin_id'];
-                               $i++;
-                       }
-
-
-//html_print_r($project);
-                               return $project;
-               }
-
-
-               function get_ticket($project_id = '')
-               {
-                       $sql = "SELECT * FROM fm_project_origin WHERE 
project_id='$project_id' and origin ='tts'";
-                       $dbresult = $GLOBALS['phpgw']->db->Execute($sql);
-
-                       return $dbresult->fields['origin_id'];
-               }
-
-               function get_power_meter($location_code = '')
-               {
-                       $dbresult = $GLOBALS['phpgw']->db->Execute("SELECT name 
as power_meter FROM fm_meter where location_code='$location_code' and 
category='1'");
-
-                       return $dbresult->fields['power_meter'];
-               }
-
-               function project_workorder_data($project_id = '')
-               {
-                       $dbresult = $GLOBALS['phpgw']->db->Execute("SELECT 
budget, id as workorder_id, vendor_id, 
calculation,rig_addition,addition,deviation from fm_workorder where 
project_id='$project_id'");
-                       while (!$dbresult->EOF)
-                       {
-                               $budget[] = array(
-                                       'workorder_id'  => 
$dbresult->fields['workorder_id'],
-                                       'budget'                => 
$dbresult->fields['budget'],
-                                       'deviation'             => 
$dbresult->fields['deviation'],
-                                       'calculation'   => 
($dbresult->fields['calculation']*(1+$dbresult->fields['addition']/100))+$dbresult->fields['rig_addition'],
-                                       'vendor_id'     => 
$dbresult->fields['vendor_id']
-                                       );
-                       }
-                       return $budget;
-               }
-
-               function branch_p_list($project_id = '')
-               {
-
-                       $dbresult = $GLOBALS['phpgw']->db->Execute("SELECT 
branch_id from fm_projectbranch WHERE project_id=" .  (int)$project_id );
-                       while (!$dbresult->EOF)
-                       {
-                               $selected[] = array('branch_id' => 
$dbresult->fields['branch_id']);
-                       }
-
-                       return $selected;
-               }
-
-               function increment_project_id()
-               {
-                       $GLOBALS['phpgw']->db->Execute("UPDATE fm_idgenerator 
set value = value + 1 where name = 'project'");
-               }
-
-               function next_project_id()
-               {
-                       $dbresult = $GLOBALS['phpgw']->db->Execute("SELECT 
value from fm_idgenerator where name = 'project'");
-
-                       $project_id = $dbresult->fields['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        = implode(" ", $address);
-                       }
-
-                       if(!$address)
-                       {
-                               $address = $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'],
-                               $project['charge_tenant'],
-                               $this->account);
-
-                       $values = $this->bocommon->validate_db_insert($values);
-
-                       $GLOBALS['phpgw']->db->begintrans();
-
-                       $GLOBALS['phpgw']->db->Execute("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,charge_tenant,user_id
 $cols) "
-                               . "VALUES ($values $vals )");
-
-                       if($project['extra']['contact_phone'])
-                       {
-                               $GLOBALS['phpgw']->db->Execute("UPDATE 
fm_tenant set contact_phone='". $project['extra']['contact_phone']. "' where 
tenant_id='". $project['extra']['tenant_id']. "'");
-                       }
-
-                       if ($project['power_meter'] )
-                       {
-                               
$this->update_power_meter($project['power_meter'],$project['location_code'],$address);
-                       }
-
-                       if (count($project['branch']) != 0)
-                       {
-                               while($branch=each($project['branch']))
-                               {
-                                       $GLOBALS['phpgw']->db->Execute("INSERT 
into fm_projectbranch (project_id,branch_id) values ('" . 
$project['project_id']. "','$branch[1]')");
-                               }
-                       }
-
-                       if($project['origin'] && $project['origin_id'])
-                       {
-                               $GLOBALS['phpgw']->db->Execute("INSERT INTO  
fm_project_origin (origin,origin_id,project_id,entry_date) "
-                                       . "VALUES ('"
-                                       . $project['origin']. "','"
-                                       . $project['origin_id']. "',"
-                                       . $project['project_id']. ","
-                                       . time() . ")");
-                       }
-
-
-                       if($GLOBALS['phpgw']->db->committrans())
-                       {
-                               $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)
-               {
-
-                       $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) . "'";
-                       }
-
-
-                       $dbresult = $GLOBALS['phpgw']->db->Execute("SELECT 
count(*) FROM fm_meter where location_code='$location_code' and category=1");
-
-
-
-                       if ( $dbresult->fields[0])
-                       {
-                               $GLOBALS['phpgw']->db->Execute("UPDATE fm_meter 
set name='$power_meter',address='$address' where location_code='$location_code' 
and category='1'");
-                       }
-                       else
-                       {
-                               $meter_id       = $this->generate_meter_id();
-                               $GLOBALS['phpgw']->db->Execute("INSERT into 
fm_meter (num,name,category,location_code,entry_date,user_id,address $cols) "
-                                       . "VALUES ('"
-                                       . $meter_id. "','"
-                                       . $power_meter. "',"
-                                       . "1,'"
-                                       . $location_code. "',"
-                                       . time() . ",$this->account, '$address' 
$vals)");
-                       }
-               }
-
-               function generate_meter_id()
-               {
-                       $prefix = 'meter';
-                       $pos    = strlen($prefix);
-                       $dbresult = $GLOBALS['phpgw']->db->Execute("SELECT 
max(num) from fm_meter where num like ('$prefix%')");
-
-
-                       $max = 
$this->bocommon->add_leading_zero(substr($dbresult->fields[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        = implode(" ", $address);
-                       }
-
-                       if(!$address)
-                       {
-                               $address = $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'],
-                               'charge_tenant'         => 
$project['charge_tenant'],
-                               'location_code'         => 
$project['location_code'],
-                               'address'                       => $address
-                               );
-
-                       $value_set      = 
$this->bocommon->validate_db_update($value_set);
-
-                       $GLOBALS['phpgw']->db->begintrans();
-
-                       $dbresult = $GLOBALS['phpgw']->db->Execute("SELECT 
status,category,coordinator FROM fm_project where id='" 
.$project['project_id']."'");
-
-                       $old_status = $dbresult->fields['status'];
-                       $old_categroy = (int)$dbresult->fields['category'];
-                       $old_coordinator = 
(int)$dbresult->fields['coordinator'];
-
-                       $GLOBALS['phpgw']->db->Execute("UPDATE fm_project set 
$value_set $vals WHERE id= '" . $project['project_id'] ."'");
-
-                       if($project['extra']['contact_phone'])
-                       {
-                               $GLOBALS['phpgw']->db->Execute("UPDATE 
fm_tenant set contact_phone='". $project['extra']['contact_phone']. "' where 
tenant_id='". $project['extra']['tenant_id']. "'");
-                       }
-
-                       if ($project['power_meter'] )
-                       {
-                               
$this->update_power_meter($project['power_meter'],$project['location_code'],$address);
-                       }
-       // -----------------which branch is represented
-                       $GLOBALS['phpgw']->db->Execute("DELETE from 
fm_projectbranch where project_id='" . $project['project_id'] ."'");
-
-                       if (count($project['branch']) != 0)
-                       {
-                               while($branch=each($project['branch']))
-                               {
-                                       $GLOBALS['phpgw']->db->Execute("INSERT 
into fm_projectbranch (project_id,branch_id) values ('" . 
$project['project_id']. "','$branch[1]')");
-                               }
-                       }
-
-                       if($project['delete_request'])
-                       {
-                               $receipt = 
$this->delete_request_from_project($project['delete_request'],$project['project_id']);
-
-                       }
-
-                       if($GLOBALS['phpgw']->db->committrans())
-                       {
-                               
$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']);
-                               }
-                               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['message'][] = 
array('msg'=>lang('project %1 has been edited',$project['project_id']));
-                       }
-                       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] . "'");
-                               $GLOBALS['phpgw']->db->Execute("DELETE FROM 
fm_project_origin WHERE origin_id='" . $request_id[$i] . "' AND 
origin='request'");
-                               $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_project_origin WHERE 
project_id='$project_id' and origin ='request'";
-                       $dbresult = $GLOBALS['phpgw']->db->Execute($sql);
-
-                       while (!$dbresult->EOF)
-                       {
-                               $request_id[]   = 
$dbresult->fields['origin_id'];
-                       }
-
-                       for ($i=0;$i<count($request_id);$i++)
-                       {
-                               $dbresult = 
$GLOBALS['phpgw']->db->Execute("SELECT status,category,coordinator FROM 
fm_request where id='" .$request_id[$i] ."'");
-
-
-
-                               $old_status = $dbresult->fields['status'];
-                               $old_category = 
(int)$dbresult->fields['category'];
-                               $old_coordinator = 
(int)$dbresult->fields['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] . "'");
-                       }
-               }
-
-
-               function check_request($request_id)
-               {
-                       $sql = "SELECT project_id FROM fm_project_origin WHERE 
origin_id='$request_id' and origin ='request'";
-                       $dbresult = $GLOBALS['phpgw']->db->Execute($sql);
-
-
-
-                       if ( $dbresult->fields[0])
-                       {
-                               return $dbresult->fields['project_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)
-                               {
-                                       $GLOBALS['phpgw']->db->Execute("INSERT 
INTO  fm_project_origin (origin,origin_id,project_id) "
-                                               . "VALUES ('request','"
-                                               . 
$add_request['request_id'][$i]. "','"
-                                               . $id. "')");
-
-                                       $this->db2->query("update fm_request 
set project_id='$id' where id='". $add_request['request_id'][$i] . "'");
-
-                                       $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_project_origin WHERE 
project_id='$project_id' and origin ='request'";
-                       $dbresult = $GLOBALS['phpgw']->db->Execute($sql);
-
-                       while (!$dbresult->EOF)
-                       {
-                               $request_id[]   = 
$dbresult->fields['origin_id'];
-                       }
-
-                       for ($i=0;$i<count($request_id);$i++)
-                       {
-
-                               $this->db2->query("update fm_request set 
project_id = NULL where id='". $request_id[$i] . "'");
-                       }
-
-                       $GLOBALS['phpgw']->db->Execute("DELETE FROM fm_project 
WHERE id='" . $project_id . "'");
-                       $GLOBALS['phpgw']->db->Execute("DELETE FROM 
fm_project_history  WHERE  history_record_id='" . $project_id   . "'");
-                       $GLOBALS['phpgw']->db->Execute("DELETE FROM 
fm_projectbranch  WHERE  project_id='" . $project_id   . "'");
-                       $GLOBALS['phpgw']->db->Execute("DELETE FROM 
fm_project_origin WHERE project_id='" . $project_id . "'");
-
-               }
-       }
-?>
+<?php
+       
/**************************************************************************\
+       * phpGroupWare - property                                               
   *
+       * http://www.phpgroupware.org                                           
   *
+       *                                                                       
   *
+       * Facilities Management                                                 
   *
+       * Written by Sigurd Nes [sigurdne at online.no]                         
   *
+       * 
------------------------------------------------------------------------ *
+       * Copyright 2000 - 2003 Free Software Foundation, Inc                   
   *
+       * This program is part of the GNU project, see http://www.gnu.org/      
   *
+       * 
------------------------------------------------------------------------ *
+       * This program is free software; you can redistribute it and/or modify 
it  *
+       * under the terms of the GNU General Public License as published by the 
   *
+       * Free Software Foundation; either version 2 of the License, or (at 
your   *
+       * option) any later version.                                            
   *
+       
\**************************************************************************/
+
+       class property_soproject
+       {
+
+               function property_soproject()
+               {
+                       $this->currentapp       = 'property'; 
//$GLOBALS['phpgw_info']['flags']['currentapp'];
+                       $this->db                       = $GLOBALS['phpgw']->db;
+                       $this->db2                      = $this->db;
+                       $this->account          = 
$GLOBALS['phpgw_data']['user']['id'];
+                       $this->bocommon         = 
CreateObject($this->currentapp.'_bocommon');
+                       $this->fm_session       = 
CreateObject($this->currentapp.'_session');
+
+                       $this->join                     = $this->bocommon->join;
+
+                       $this->grants   = 
$this->fm_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');
+                               
$this->fm_session->appsession('grants_project',$this->currentapp,$this->grants);
+                       }
+               }
+
+
+               function select_category_workorder_list()
+               {
+
+                       $dbresult = $GLOBALS['phpgw']->db->Execute("SELECT id, 
descr FROM fm_workorder_category  ORDER BY descr ");
+
+                       $i = 0;
+                       while (!$dbresult->EOF)
+                       {
+                               $categories[$i]['id']                           
= $dbresult->fields['id'];
+                               $categories[$i]['name']                         
= stripslashes($dbresult->fields['descr']);
+                               $i++;
+                       }
+                       return $categories;
+               }
+
+               function read_single_project_category($id='')
+               {
+                       $dbresult = $GLOBALS['phpgw']->db->Execute("SELECT 
descr FROM fm_workorder_category where id='$id' ");
+
+                       return $dbresult->fields['descr'];
+               }
+
+               function select_status_list()
+               {
+                       $dbresult = $GLOBALS['phpgw']->db->Execute("SELECT id, 
descr FROM fm_workorder_status ORDER BY id ");
+
+                       $i = 0;
+                       while (!$dbresult->EOF)
+                       {
+                               $status_entries[$i]['id']                       
        = $dbresult->fields['id'];
+                               $status_entries[$i]['name']                     
        = stripslashes($dbresult->fields['descr']);
+                               $i++;
+                       }
+                       return $status_entries;
+               }
+
+               function select_branch_list()
+               {
+                       $dbresult = $GLOBALS['phpgw']->db->Execute("SELECT id, 
descr FROM fm_branch ORDER BY id ");
+
+                       $i = 0;
+                       while (!$dbresult->EOF)
+                       {
+                               $branch_entries[$i]['id']                       
        = $dbresult->fields['id'];
+                               $branch_entries[$i]['name']                     
        = stripslashes($dbresult->fields['descr']);
+                               $i++;
+                       }
+                       return $branch_entries;
+               }
+
+               function select_key_location_list()
+               {
+                       $dbresult = $GLOBALS['phpgw']->db->Execute("SELECT id, 
descr FROM fm_key_loc ORDER BY descr ");
+
+                       $i = 0;
+                       while (!$dbresult->EOF)
+                       {
+                               $key_location_entries[$i]['id']                 
        = $dbresult->fields['id'];
+                               $key_location_entries[$i]['name']               
        = stripslashes($dbresult->fields['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']:'');
+                       }
+
+                       $sql = $this->bocommon->fm_cache('sql_project');
+
+                       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 .='(';
+
+                               $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',$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',$this->uicols);
+                               
$this->bocommon->fm_cache('cols_return_project',$cols_return);
+                               
$this->bocommon->fm_cache('type_id_project',$type_id);
+                               
$this->bocommon->fm_cache('cols_extra_project',$this->cols_extra);
+
+                       }
+                       else
+                       {
+                               $this->uicols           = 
$this->bocommon->fm_cache('uicols_project');
+                               $cols_return            = 
$this->bocommon->fm_cache('cols_return_project');
+                               $type_id                        = 
$this->bocommon->fm_cache('type_id_project');
+                               $this->cols_extra       = 
$this->bocommon->fm_cache('cols_extra_project');
+                       }
+
+                       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 ($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)
+                       {
+                               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 LIKE '%$query%' or fm_project.address LIKE '%$query%' or 
fm_project.location_code LIKE '%$query%' or fm_project.id LIKE '%$query%')";
+                               }
+                       }
+
+                       $sql .= " $filtermethod $querymethod";
+//echo $sql;
+                       $dbresult2 = $GLOBALS['phpgw']->db->Execute($sql);
+                       $this->total_records = $dbresult2->_numOfRows;
+
+                       $maxmatchs = 15;
+
+                       if(!$allrows)
+                       {
+                               $dbresult = 
$GLOBALS['phpgw']->db->SelectLimit($sql . $ordermethod,$maxmatchs,$start);
+
+                       }
+                       else
+                       {
+                               $dbresult = $GLOBALS['phpgw']->db->Execute($sql 
. $ordermethod);
+                       }
+
+
+                       $j=0;
+                       $k=count($cols_return);
+                       while (!$dbresult->EOF)
+                       {
+                               for ($i=0;$i<$k;$i++)
+                               {
+                                       $project_list[$j][$cols_return[$i]] = 
$dbresult->fields[$cols_return[$i]];
+                                       $project_list[$j]['grants'] = 
(int)$this->grants[$dbresult->fields['user_id']];
+
+                               }
+
+                               $location_code= 
$dbresult->fields['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++;
+                       }
+
+//html_print_r($project_list);
+                       return $project_list;
+               }
+
+               function read_single($project_id)
+               {
+
+                       $sql = "SELECT * from fm_project where 
id='$project_id'";
+
+                       $dbresult = $GLOBALS['phpgw']->db->Execute($sql);
+
+                       if (!$dbresult->EOF)
+                       {
+                               $project['project_id']                  = 
$dbresult->fields['id'];
+                               $project['title']                               
= $dbresult->fields['title'];
+                               $project['name']                                
= $dbresult->fields['name'];
+                               $project['location_code']               = 
$dbresult->fields['location_code'];
+                               $project['key_fetch']                   = 
$dbresult->fields['key_fetch'];
+                               $project['key_deliver']                 = 
$dbresult->fields['key_deliver'];
+                               $project['other_branch']                = 
$dbresult->fields['other_branch'];
+                               $project['key_responsible']             = 
$dbresult->fields['key_responsible'];
+                               $project['charge_tenant']               = 
$dbresult->fields['charge_tenant'];
+                               $project['descr']                               
= $dbresult->fields['descr'];
+                               $project['status']                              
= $dbresult->fields['status'];
+                               $project['budget']                              
= (int)$dbresult->fields['budget'];
+                               $project['reserve']                             
= (int)$dbresult->fields['reserve'];
+                               $project['tenant_id']                   = 
$dbresult->fields['tenant_id'];
+                               $project['user_id']                             
= $dbresult->fields['user_id'];
+                               $project['coordinator']                 = 
$dbresult->fields['coordinator'];
+                               $project['access']                              
= $dbresult->fields['access'];
+                               $project['start_date']                  = 
$dbresult->fields['start_date'];
+                               $project['end_date']                    = 
$dbresult->fields['end_date'];
+                               $project['cat_id']                              
= $dbresult->fields['category'];
+                               $project['grants']                              
= (int)$this->grants[$dbresult->fields['user_id']];
+                               $project['p_num']                               
= $dbresult->fields['p_num'];
+                               $project['p_entity_id']                 = 
$dbresult->fields['p_entity_id'];
+                               $project['p_cat_id']                    = 
$dbresult->fields['p_cat_id'];
+
+                               $project['power_meter'] = 
$this->get_power_meter($dbresult->fields['location_code']);
+                       }
+
+                       $sql = "SELECT * FROM fm_project_origin WHERE 
project_id='$project_id' and origin ='request'";
+
+                       $dbresult = $GLOBALS['phpgw']->db->Execute($sql);
+
+                       $i=0;
+                       while (!$dbresult->EOF)
+                       {
+                               $project['request_id'][$i]['id']        = 
$dbresult->fields['origin_id'];
+                               $i++;
+                       }
+
+                       $sql = "SELECT * FROM fm_project_origin WHERE 
project_id='$project_id' and origin ='tts'";
+
+                       $dbresult = $GLOBALS['phpgw']->db->Execute($sql);
+
+                       while (!$dbresult->EOF)
+                       {
+                               $project['ticket_id'][$i]['id'] = 
$dbresult->fields['origin_id'];
+                               $i++;
+                       }
+
+
+//html_print_r($project);
+                               return $project;
+               }
+
+
+               function get_ticket($project_id = '')
+               {
+                       $sql = "SELECT * FROM fm_project_origin WHERE 
project_id='$project_id' and origin ='tts'";
+                       $dbresult = $GLOBALS['phpgw']->db->Execute($sql);
+
+                       return $dbresult->fields['origin_id'];
+               }
+
+               function get_power_meter($location_code = '')
+               {
+                       $dbresult = $GLOBALS['phpgw']->db->Execute("SELECT name 
as power_meter FROM fm_meter where location_code='$location_code' and 
category='1'");
+
+                       return $dbresult->fields['power_meter'];
+               }
+
+               function project_workorder_data($project_id = '')
+               {
+                       $dbresult = $GLOBALS['phpgw']->db->Execute("SELECT 
budget, id as workorder_id, vendor_id, 
calculation,rig_addition,addition,deviation from fm_workorder where 
project_id='$project_id'");
+                       while (!$dbresult->EOF)
+                       {
+                               $budget[] = array(
+                                       'workorder_id'  => 
$dbresult->fields['workorder_id'],
+                                       'budget'                => 
$dbresult->fields['budget'],
+                                       'deviation'             => 
$dbresult->fields['deviation'],
+                                       'calculation'   => 
($dbresult->fields['calculation']*(1+$dbresult->fields['addition']/100))+$dbresult->fields['rig_addition'],
+                                       'vendor_id'     => 
$dbresult->fields['vendor_id']
+                                       );
+                       }
+                       return $budget;
+               }
+
+               function branch_p_list($project_id = '')
+               {
+
+                       $dbresult = $GLOBALS['phpgw']->db->Execute("SELECT 
branch_id from fm_projectbranch WHERE project_id=" .  (int)$project_id );
+                       while (!$dbresult->EOF)
+                       {
+                               $selected[] = array('branch_id' => 
$dbresult->fields['branch_id']);
+                       }
+
+                       return $selected;
+               }
+
+               function increment_project_id()
+               {
+                       $GLOBALS['phpgw']->db->Execute("UPDATE fm_idgenerator 
set value = value + 1 where name = 'project'");
+               }
+
+               function next_project_id()
+               {
+                       $dbresult = $GLOBALS['phpgw']->db->Execute("SELECT 
value from fm_idgenerator where name = 'project'");
+
+                       $project_id = $dbresult->fields['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        = implode(" ", $address);
+                       }
+
+                       if(!$address)
+                       {
+                               $address = $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'],
+                               $project['charge_tenant'],
+                               $this->account);
+
+                       $values = $this->bocommon->validate_db_insert($values);
+
+                       $GLOBALS['phpgw']->db->begintrans();
+
+                       $GLOBALS['phpgw']->db->Execute("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,charge_tenant,user_id
 $cols) "
+                               . "VALUES ($values $vals )");
+
+                       if($project['extra']['contact_phone'])
+                       {
+                               $GLOBALS['phpgw']->db->Execute("UPDATE 
fm_tenant set contact_phone='". $project['extra']['contact_phone']. "' where 
tenant_id='". $project['extra']['tenant_id']. "'");
+                       }
+
+                       if ($project['power_meter'] )
+                       {
+                               
$this->update_power_meter($project['power_meter'],$project['location_code'],$address);
+                       }
+
+                       if (count($project['branch']) != 0)
+                       {
+                               while($branch=each($project['branch']))
+                               {
+                                       $GLOBALS['phpgw']->db->Execute("INSERT 
into fm_projectbranch (project_id,branch_id) values ('" . 
$project['project_id']. "','$branch[1]')");
+                               }
+                       }
+
+                       if($project['origin'] && $project['origin_id'])
+                       {
+                               $GLOBALS['phpgw']->db->Execute("INSERT INTO  
fm_project_origin (origin,origin_id,project_id,entry_date) "
+                                       . "VALUES ('"
+                                       . $project['origin']. "','"
+                                       . $project['origin_id']. "',"
+                                       . $project['project_id']. ","
+                                       . time() . ")");
+                       }
+
+
+                       if($GLOBALS['phpgw']->db->committrans())
+                       {
+                               $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)
+               {
+
+                       $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) . "'";
+                       }
+
+
+                       $dbresult = $GLOBALS['phpgw']->db->Execute("SELECT 
count(*) FROM fm_meter where location_code='$location_code' and category=1");
+
+
+
+                       if ( $dbresult->fields[0])
+                       {
+                               $GLOBALS['phpgw']->db->Execute("UPDATE fm_meter 
set name='$power_meter',address='$address' where location_code='$location_code' 
and category='1'");
+                       }
+                       else
+                       {
+                               $meter_id       = $this->generate_meter_id();
+                               $GLOBALS['phpgw']->db->Execute("INSERT into 
fm_meter (num,name,category,location_code,entry_date,user_id,address $cols) "
+                                       . "VALUES ('"
+                                       . $meter_id. "','"
+                                       . $power_meter. "',"
+                                       . "1,'"
+                                       . $location_code. "',"
+                                       . time() . ",$this->account, '$address' 
$vals)");
+                       }
+               }
+
+               function generate_meter_id()
+               {
+                       $prefix = 'meter';
+                       $pos    = strlen($prefix);
+                       $dbresult = $GLOBALS['phpgw']->db->Execute("SELECT 
max(num) from fm_meter where num like ('$prefix%')");
+
+
+                       $max = 
$this->bocommon->add_leading_zero(substr($dbresult->fields[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        = implode(" ", $address);
+                       }
+
+                       if(!$address)
+                       {
+                               $address = $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'],
+                               'charge_tenant'         => 
$project['charge_tenant'],
+                               'location_code'         => 
$project['location_code'],
+                               'address'                       => $address
+                               );
+
+                       $value_set      = 
$this->bocommon->validate_db_update($value_set);
+
+                       $GLOBALS['phpgw']->db->begintrans();
+
+                       $dbresult = $GLOBALS['phpgw']->db->Execute("SELECT 
status,category,coordinator FROM fm_project where id='" 
.$project['project_id']."'");
+
+                       $old_status = $dbresult->fields['status'];
+                       $old_categroy = (int)$dbresult->fields['category'];
+                       $old_coordinator = 
(int)$dbresult->fields['coordinator'];
+
+                       $GLOBALS['phpgw']->db->Execute("UPDATE fm_project set 
$value_set $vals WHERE id= '" . $project['project_id'] ."'");
+
+                       if($project['extra']['contact_phone'])
+                       {
+                               $GLOBALS['phpgw']->db->Execute("UPDATE 
fm_tenant set contact_phone='". $project['extra']['contact_phone']. "' where 
tenant_id='". $project['extra']['tenant_id']. "'");
+                       }
+
+                       if ($project['power_meter'] )
+                       {
+                               
$this->update_power_meter($project['power_meter'],$project['location_code'],$address);
+                       }
+       // -----------------which branch is represented
+                       $GLOBALS['phpgw']->db->Execute("DELETE from 
fm_projectbranch where project_id='" . $project['project_id'] ."'");
+
+                       if (count($project['branch']) != 0)
+                       {
+                               while($branch=each($project['branch']))
+                               {
+                                       $GLOBALS['phpgw']->db->Execute("INSERT 
into fm_projectbranch (project_id,branch_id) values ('" . 
$project['project_id']. "','$branch[1]')");
+                               }
+                       }
+
+                       if($project['delete_request'])
+                       {
+                               $receipt = 
$this->delete_request_from_project($project['delete_request'],$project['project_id']);
+
+                       }
+
+                       if($GLOBALS['phpgw']->db->committrans())
+                       {
+                               
$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']);
+                               }
+                               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['message'][] = 
array('msg'=>lang('project %1 has been edited',$project['project_id']));
+                       }
+                       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] . "'");
+                               $GLOBALS['phpgw']->db->Execute("DELETE FROM 
fm_project_origin WHERE origin_id='" . $request_id[$i] . "' AND 
origin='request'");
+                               $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_project_origin WHERE 
project_id='$project_id' and origin ='request'";
+                       $dbresult = $GLOBALS['phpgw']->db->Execute($sql);
+
+                       while (!$dbresult->EOF)
+                       {
+                               $request_id[]   = 
$dbresult->fields['origin_id'];
+                       }
+
+                       for ($i=0;$i<count($request_id);$i++)
+                       {
+                               $dbresult = 
$GLOBALS['phpgw']->db->Execute("SELECT status,category,coordinator FROM 
fm_request where id='" .$request_id[$i] ."'");
+
+
+
+                               $old_status = $dbresult->fields['status'];
+                               $old_category = 
(int)$dbresult->fields['category'];
+                               $old_coordinator = 
(int)$dbresult->fields['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] . "'");
+                       }
+               }
+
+
+               function check_request($request_id)
+               {
+                       $sql = "SELECT project_id FROM fm_project_origin WHERE 
origin_id='$request_id' and origin ='request'";
+                       $dbresult = $GLOBALS['phpgw']->db->Execute($sql);
+
+
+
+                       if ( $dbresult->fields[0])
+                       {
+                               return $dbresult->fields['project_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)
+                               {
+                                       $GLOBALS['phpgw']->db->Execute("INSERT 
INTO  fm_project_origin (origin,origin_id,project_id) "
+                                               . "VALUES ('request','"
+                                               . 
$add_request['request_id'][$i]. "','"
+                                               . $id. "')");
+
+                                       $this->db2->query("update fm_request 
set project_id='$id' where id='". $add_request['request_id'][$i] . "'");
+
+                                       $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_project_origin WHERE 
project_id='$project_id' and origin ='request'";
+                       $dbresult = $GLOBALS['phpgw']->db->Execute($sql);
+
+                       while (!$dbresult->EOF)
+                       {
+                               $request_id[]   = 
$dbresult->fields['origin_id'];
+                       }
+
+                       for ($i=0;$i<count($request_id);$i++)
+                       {
+
+                               $this->db2->query("update fm_request set 
project_id = NULL where id='". $request_id[$i] . "'");
+                       }
+
+                       $GLOBALS['phpgw']->db->Execute("DELETE FROM fm_project 
WHERE id='" . $project_id . "'");
+                       $GLOBALS['phpgw']->db->Execute("DELETE FROM 
fm_project_history  WHERE  history_record_id='" . $project_id   . "'");
+                       $GLOBALS['phpgw']->db->Execute("DELETE FROM 
fm_projectbranch  WHERE  project_id='" . $project_id   . "'");
+                       $GLOBALS['phpgw']->db->Execute("DELETE FROM 
fm_project_origin WHERE project_id='" . $project_id . "'");
+
+               }
+       }
+?>




reply via email to

[Prev in Thread] Current Thread [Next in Thread]