phpgroupware-cvs
[Top][All Lists]
Advanced

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

[Phpgroupware-cvs] property/inc/class.soworkorder.inc.php, 1.2


From: nomail
Subject: [Phpgroupware-cvs] property/inc/class.soworkorder.inc.php, 1.2
Date: Wed, 2 Jun 2004 20:54:28 +0200

Update of /property/inc
Added Files:
        Branch: 
          class.soworkorder.inc.php

date: 2004/06/02 18:54:28;  author: sigurdne;  state: Exp;  lines: +547 -0

Log Message:
no message
=====================================================================
<?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 soworkorder
        {

                function soworkorder()
                {
                        $this->currentapp       = 
$GLOBALS['phpgw_info']['flags']['currentapp'];
                        $this->db                       = $GLOBALS['phpgw']->db;
                        $this->db2                      = $this->db;
                        $this->account          = 
$GLOBALS['phpgw_info']['user']['account_id'];
                        $this->bocommon         = 
CreateObject($this->currentapp.'.bocommon');

                        $this->join                     = $this->bocommon->join;
                        $this->left_join        = $this->bocommon->left_join;
                        $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']:'');
                        }

//_debug_array($data);
                        $sql = 
$this->bocommon->fm_cache('sql_workorder'.!!$search_vendor);

                        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 .= ',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 .='(';

//                              if($search_vendor)
                                {
                                        $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 .='(';
                                }

                                $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,$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,$this->uicols);
                                
$this->bocommon->fm_cache('cols_return_workorder'.!!$search_vendor,$cols_return);
                                
$this->bocommon->fm_cache('type_id_workorder'.!!$search_vendor,$type_id);
//                              
$this->bocommon->fm_cache('cols_extra_workorder'.!!$search_vendor,$this->cols_extra);

                        }
                        else
                        {
                                $this->uicols           = 
$this->bocommon->fm_cache('uicols_workorder'.!!$search_vendor);
                                $cols_return            = 
$this->bocommon->fm_cache('cols_return_workorder'.!!$search_vendor);
                                $type_id                        = 
$this->bocommon->fm_cache('type_id_workorder'.!!$search_vendor);
//                              $this->cols_extra       = 
$this->bocommon->fm_cache('cols_extra_workorder'.!!$search_vendor);
                        }

                        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 ($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)
                        {
                                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 LIKE '%$query%' or fm_workorder.descr LIKE '%$query%' or 
fm_project.address LIKE '%$query%' or fm_project.location_code LIKE '%$query%' 
or fm_workorder.id 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 LIKE '%$search_vendor%'";
                                }
                        }

                        $sql .= " $filtermethod $querymethod 
$querymethod_vendor";
//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__);

                        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']);
//_debug_array($workorder);

                        $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($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->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__);

                }
        }
?>




reply via email to

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