<?php
/**********************************************************************

	Released under the terms of the GNU General Public License, GPL,
***********************************************************************/
$page_security = 'SA_ASSET_ANALYTIC';
// ----------------------------------------------------------------
// $ Revision:	2.0 $
// Creator:	Joe Hunt
// date_:	2005-05-19
// Title:	Stock Check Sheet
// ----------------------------------------------------------------
$path_to_root="..";

include_once($path_to_root . "/includes/session.inc");
include_once($path_to_root . "/includes/date_functions.inc");
include_once($path_to_root . "/includes/data_checks.inc");
include_once($path_to_root . "/gl/includes/gl_db.inc");
include_once($path_to_root . "/reporting/db/report_db.inc");

//----------------------------------------------------------------------------------------------------

print_asset_master();

function get_asset_sub_group_name($selected_id)
{
	$sql = "SELECT description  FROM ".TB_PREF."asset_subgroups  WHERE id=".db_escape($selected_id);

	$result = db_query($sql, "could not get asset type");

	return db_fetch($result);
}
function get_asset_category_name($selected_id)
{
	$sql = "SELECT description FROM ".TB_PREF."asset_catergories  WHERE id=".db_escape($selected_id);

	$result = db_query($sql, "could not get asset category details");

	return db_fetch($result);
}
function get_assets_for_report($asset_type,$asset_cat,$asset_group,$asset_subgroupp)
{
        $sql = "SELECT am.* ,
                    at.description asset_sub_group_name,
                    ag.description assset_group_name,
                    acd.description asset_catergory_name,
                    ar.description asset_rate_cat
                FROM ".TB_PREF."asset_master am
                LEFT JOIN ".TB_PREF."asset_subgroups at ON
                        at.id = am.subgroup_id   
                LEFT JOIN ".TB_PREF."asset_groups ag ON
                            ag.id = am.group_id       
                LEFT JOIN ".TB_PREF."asset_catergories acd ON
                            acd.id = am.asset_category_id 
                LEFT JOIN ".TB_PREF."tool_rates ar ON
                            ar.id = am.rate_category_id
                WHERE 1=1 ";
        
        if(strlen($asset_type) > 0)
        {
            $sql .= " AND am.type =".db_escape($asset_type);
        }    
        if($asset_group > 0)
        {
            $sql .= " AND am.group_id =".db_escape($asset_group);
        }    
        if($asset_subgroupp > -1)
        {
            $sql .= " AND am.subgroup_id =".db_escape($asset_subgroupp);
        }
        if($asset_cat > -1)
        {
            $sql .= " AND am.asset_category_id =".db_escape($asset_cat);
        }
    return $result = db_query($sql, 'Could not get asset details');
    
}



function get_yes_no($value)
{
    if($value == "1" || $value == "Yes"){
        return 'Yes';
    }
    elseif($value == "0"|| $value == "No"){
        return 'No';
    }
}

function get_asset_group_name($selected_id)
{
	$sql = "SELECT description asset_group FROM ".TB_PREF."asset_groups  WHERE id=".db_escape($selected_id);

	$result = db_query($sql, "could not get asset group");

	return db_fetch($result);
}
function print_asset_master()
{
    global $path_to_root, $pic_height;
        
    $asset_type = $_POST['PARAM_0'];
    $asset_cat = $_POST['PARAM_1'];
    $asset_group = $_POST['PARAM_2'];
    $asset_subgroup = $_POST['PARAM_3'];
    
   
//   $asset_group = 0;
//    if(isset($_POST['PARAM_0']) && strlen($_POST['PARAM_0']) > 0){
//        $asset_group = $_POST['PARAM_1'];
//        
//        $level_2_list_value_record = asset_groups_list($asset_group, $asset_type, $asset_group,null,false);
//
//    }
    
    include_once($path_to_root . "/reporting/includes/excel_report.inc");
//    include_once($path_to_root . "/reporting/includes/pdf_report.inc");
    
    $orientation ='P';
        if(!($asset_type > 0))
        {
            $asset_type_row = get_asset_sub_group_name($asset_type);  
            $aset_type_name = "";
            if(isset($asset_type_row['description'])){
                $aset_type_name = $asset_type_row['description'];
            }
        }else {
             $aset_type_name ="All";
        }
        
        if(!($asset_cat > 0))
        {
            $asset_cat_data= get_asset_category_name($asset_cat);
            $asset_cat_name = "";
            if(isset($asset_cat_data['description'])){
                $asset_cat_name = $asset_cat_data['description'];   
            }
        }else{
            $asset_cat_name ="All";
        }
        
        if(!($asset_group > 0))
        {
            $asset_cat_data= get_asset_group_name($asset_group);
            $asset_group_name = "";
            if(isset($asset_cat_data['description'])){
                $asset_group_name = $asset_cat_data['asset_group'];
            }
        }else{
            $asset_group_name ="All";
        }
        
        if(!($asset_subgroup > 0))
        {
            $asset_subgroup_row = get_asset_sub_group_name($asset_subgroup);  
            $asset_subgroup_name = "";
            if(isset($asset_subgroup_row['description'])){
                $asset_subgroup_name = $asset_subgroup_row['description'];
            }
        }else {
             $asset_subgroup_name ="All";
        }
        
       
        $cols = array($x=0,$x+=100,$x+=30,$x+=100,$x+=100,$x+=100,$x+=100,$x+=100,$x+=100,$x+=100,$x+=100,$x+=100,$x+=100,$x+=100,$x+=100,$x+=100,$x+=100,$x+=100,$x+=100,$x+=100);
        $headers = array('Asset Code','Id','Description','Long Description','Type', 'Multiple', 'Category','Group','Subgroup','Hired','Rate Category','Serial/Plate Number',
            'Make','Model','Useful Life (Years)','Extended Life (Years)','PO Number','Purchase Value','Current Value');
        
        $params =   array( 	0 => $aset_type_name,
                                1 => $asset_cat_name,
                                2 => $asset_group_name,
                                2 => $asset_subgroup_name
                        );
        
        
        $aligns = array('left','left','left','left','left','left','lrft','left','left','left','left','left','left','left','left','left','left','right','right');
        
        $rep = new FrontReport(_('Asset Master'), "AssetMaster", 'A3', 9, $orientation);           
        
        
        $rep->SetHeaderType('form_991450');
        $rep->Font();
        $rep->Info($params, $cols, $headers, $aligns);
        $rep->NewPage();
      
        global $asset_types;
//        $rep->row - 4;
        $res = get_assets_for_report($asset_type,$asset_cat,$asset_group,$asset_subgroup);
    
       	while ($asset_data = db_fetch($res))
	{
            
            if($asset_data['is_hired']==0)
            {
                $hired = "No";
            }
            else
            {
                $hired = "Yes";
            }
                    
                        $rep->TextCol($c=0, $c+=1, $asset_data['asset_code'] );
//                        $rep->TextCol($c, $c+=1, html_entity_decode($asset_data['asset_code2'],ENT_QUOTES)); 
                        $rep->TextCol($c, $c+=1, $asset_data['asset_id']); 
                        $rep->TextCol($c, $c+=1, $asset_data['description']); 
                        $rep->TextCol($c, $c+=1, $asset_data['long_description']); 
                        $rep->TextCol($c, $c+=1, $asset_types[ $asset_data['type']]); 
                        $rep->TextCol($c, $c+=1, get_yes_no_name($asset_data['multiple_items'])); 
                        $rep->TextCol($c, $c+=1, $asset_data['asset_catergory_name']); 
                        $rep->TextCol($c, $c+=1, $asset_data['assset_group_name']);
                        $rep->TextCol($c, $c+=1,  $asset_data['asset_sub_group_name']);  
                        
                        
                        
                        $rep->TextCol($c, $c+=1, $hired);
                        
                        $rep->TextCol($c, $c+=1,$asset_data['asset_rate_cat']);
                        $rep->TextCol($c, $c+=1, $asset_data['asset_serial']); 
                         $rep->TextCol($c, $c+=1,$asset_data['make']); 
                        $rep->TextCol($c, $c+=1, $asset_data['model']); 
                        $rep->TextCol($c, $c+=1, $asset_data['useful_life']); 
                         $rep->TextCol($c, $c+=1,$asset_data['extended_life']); 
                        $rep->TextCol($c, $c+=1, $asset_data['po_number']); 
                        $rep->TextCol($c, $c+=1, number_format($asset_data['purchase_value'],2)); 
                        $rep->TextCol($c, $c+=1, number_format($asset_data['current_value'],2)); 
//                         $rep->TextCol(16, 17,sql2date($asset_data['warranty_from']));  
//                       $rep->TextCol(17, 18,sql2date($asset_data['warranty_to']));  
                        
                        $rep->NewLine();
                         
                }
                 $rep->End();       
	}
        



