<?php

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


function get_asset_type_id_by_name($asset_type_name)
{
    global $asset_types;
            
    foreach ($asset_types as $key => $value) {
        if($value == $asset_type_name){
            return array("success" => true, "message" => $key);
        }
    }	
	
    return array("success" => false, "message" => "Unknown asset type");
}


function get_asset_group_id_by_name($asset_type_name)
{
    $sql="SELECT id FROM ".TB_PREF."asset_groups    
		WHERE description= ".db_escape($asset_type_name);

	$result = db_query($sql,"Could not get_asset_type_id_by_name");
    if(db_num_rows($result) > 0){
        $row = db_fetch($result);
        return array("success" => true, "message" => $row['id']);
    }
	
	return array("success" => false, "message" => "Unknown asset group.");
}



function get_asset_subgroup_id_by_name($asset_group_id, $asset_subgroup_name)
{
    $sql="SELECT id FROM ".TB_PREF."asset_subgroups   
            WHERE group_id = ".db_escape($asset_group_id)." 
                    AND description= ".db_escape($asset_subgroup_name);

	$result = db_query($sql,"Could not get_asset_subgroup_id_by_name");
    if(db_num_rows($result) > 0){
        $row = db_fetch($result);
        return array("success" => true, "message" => $row['id']);
    }
	
	return array("success" => false, "message" => "Unknown asset subgroup for the asset group.");
}


function get_asset_category_id_by_name($asset_category_name)
{
    $sql="SELECT id FROM ".TB_PREF."asset_catergories   
		WHERE description= ".db_escape($asset_category_name);

	$result = db_query($sql,"Could not get_asset_category_id_by_name");
    if(db_num_rows($result) > 0){
        $row = db_fetch($result);
        return array("success" => true, "message" => $row['id']);
    }
	
	return array("success" => false, "message" => "Unknown asset category");
}



function get_asset_rate_category_id_by_name($asset_rate_category_name)
{
    $sql="SELECT id FROM ".TB_PREF."tool_rates    
		WHERE description= ".db_escape($asset_rate_category_name);

	$result = db_query($sql,"Could not get_asset_category_id_by_name");
    if(db_num_rows($result) > 0){
        $row = db_fetch($result);
        return array("success" => true, "message" => $row['id']);
    }
	
	return array("success" => false, "message" => "Unknown asset rate category");
}


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

function validate_import_asset($myrow, &$data)
{
    if ($myrow['asset_code'] == "" || !isset($myrow['asset_code']))
   	{
      	$message = ( _("Asset code is blank."));
		return array("success" => false, "message" => $message);
   	}

    
    if (strstr($myrow['asset_code'], " ") || strstr($myrow['asset_code'],"'") || 
        strstr($myrow['asset_code'], "+") || strstr($myrow['asset_code'], "\"") || 
        strstr($myrow['asset_code'], "&") || strstr($myrow['asset_code'], "\t")||
        strstr($myrow['asset_code'], "%")){
        $message = ( _('Asset code cannot contain any of the following characters -  & + % OR a space OR quotes'));
        return array("success" => false, "message" => $message);
    }
    else{
        $data['asset_code']=$myrow['asset_code'];
    }
    
    if($myrow['asset_id'] > 0){
        $existing_asset = get_asset($myrow['asset_id']);
        if(!(isset($existing_asset['asset_id']) && $existing_asset['asset_id'] > 0)){
            $message = (_("Asset id ".$myrow['asset_id']." not found."));
            return array("success" => false, "message" => $message);
        }
    }
    
           
    
    $num_rows = get_existing_asset_code($myrow['asset_id'], $myrow['asset_code']); 
    if($num_rows > 0){
        $message = (_("Asset code already exists."));
        return array("success" => false, "message" => $message);
    }
            
    return array("success" => true, "message" => "");
}


function can_process_import_asset($myrow, &$data, $Mode)
{  
    if(isset($myrow['asset_code2']) && $myrow['asset_code2'] != "")
   	{
        if (strstr($myrow['asset_code2'], " ") || strstr($myrow['asset_code2'],"'") || 
            strstr($myrow['asset_code2'], "+") || strstr($myrow['asset_code2'], "\"") || 
            strstr($myrow['asset_code2'], "&") || strstr($myrow['asset_code2'], "\t")||
            strstr($myrow['asset_code2'], "%")){
            $message = ( _('Asset code2 cannot contain any of the following characters -  & + % OR a space OR quotes'));
            return array("success" => false, "message" => $message);
        }
        else{
            $data['asset_code2']=$myrow['asset_code2'];
        }
    }
    else{
        $data['asset_code2'] = "";
    }
        
    
    $msg = get_asset_type_id_by_name($myrow['asset_type_name']);
    if($msg['success'] !== true){
        return $msg;
    }
    else{
        $data['asset_type_id'] = $msg['message'];
    }
    
    if(strlen($myrow['owner_project_name']) > 0){
        if(is_numeric($myrow['owner_project_name'])){
            $msg = get_project_id_by_id($myrow['owner_project_name']);
            if($msg['success'] !== true){
                return $msg;
            }
            else{
                $data['project_id'] = $msg['message'];
            }
        }
        else{
            $msg = get_project_id_by_name($myrow['owner_project_name']);
            if($msg['success'] !== true){
                return $msg;
            }
            else{
                $data['project_id'] = $msg['message'];
            }            
        }
    }else{
        $data['project_id'] = 0;
    }
    
    
    $msg = get_asset_group_id_by_name($myrow['asset_group_name']);
    if($msg['success'] !== true){
        return $msg;
    }
    else{
        $data['asset_group_id'] = $msg['message'];
    }
    
    $data['asset_subgroup_id'] = 0;
    if(strlen($myrow['asset_subgroup_name']) > 0){
        $msg = get_asset_subgroup_id_by_name($data['asset_group_id'], $myrow['asset_subgroup_name']);
        if($msg['success'] !== true){
            return $msg;
        }
        else{
            $data['asset_subgroup_id'] = $msg['message'];
        }
    }
    
    $msg = get_asset_category_id_by_name($myrow['asset_category_name']);
    if($msg['success'] !== true){
        return $msg;
    }
    else{
        $data['asset_category_id'] = $msg['message'];
    }
    
    $msg = get_asset_rate_category_id_by_name($myrow['rate_category_name']);
    if($msg['success'] !== true){
        return $msg;
    }
    else{
        $data['rate_category_id'] = $msg['message'];
    }
        
    $data['is_hired']=$myrow['hired'];
    
    if(!(strlen($myrow['description']) > 0)){
        return array("success" => false, "message" => "Description can not be empty");
    }
    else{
        $data['description'] = $myrow['description'];
    }
    
    $data['long_description'] = $myrow['long_description'];
    $data['asset_serial'] = $myrow['asset_serial'];
    $data['make'] = $myrow['make'];
    $data['model']=$myrow['model'];
    
    $data['po_number'] = $myrow['po_number'];
    
    if(!(check_num2($myrow['purchase_value'],0))){
        return array("success" => false, "message" => "Purchase value can not be negative");
    }
    else{
        $data['purchase_value'] = $myrow['purchase_value'];
    }
    
    if(!(check_num2($myrow['current_value'],0))){
        return array("success" => false, "message" => "Current value can not be negative");
    }
    else{
        $data['current_value'] = $myrow['current_value'];
    }
    
    if(!(check_num2($myrow['useful_life'],0))){
        return array("success" => false, "message" => "Userful life can not be negative");
    }
    else{
        $data['useful_life'] = $myrow['useful_life'];
    }
    
    if(!(check_num2($myrow['extended_life'],0))){
        return array("success" => false, "message" => "Exended life can not be negative");
    }
    else{
        $data['extended_life'] = $myrow['extended_life'];
    }
    
    if($myrow['inactive'] == "No" || $myrow['inactive'] == "0"){
        $data['inactive'] = 0;
    }else{
        $data['inactive'] = 1;
    }
    
    if(strtoupper($myrow['multiple_items']) == "YES" || $myrow['multiple_items'] == "1"){
        $data['multiple_items'] = 1;
    }else{
        $data['multiple_items'] = 0;
    }
    
	
	return array("success" => true, "message" => "");
}

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


function save_import_assets($import=false)
{
    $sql="SELECT * FROM import_assets";
    $result = db_query($sql,"Import asset records could not be retrieved");
    while ($myrow = db_fetch($result))
    {
        $data = "";
        $data['id'] = $myrow['id'];
        $data['asset_id'] = $myrow['asset_id'];
        $data['owner_project_name'] = $myrow['owner_project_name'];
        
        $clvd = validate_import_asset($myrow, $data);
        if($clvd['success'] !==  true){
            update_import_asset_record($myrow['id'], $clvd['message']);  
            continue;
        }
            
//        $exisiting_asset = get_asset_by_code($data['asset_code']);
//        $exisiting_asset = get_asset($data['asset_id']);
        if($data['asset_id'] > 0){
            $Mode = "UPDATE_ITEM";
            $exisiting_asset_id = $data['asset_id'];
        }
        else{
            $Mode = "ADD_ITEM";
            $exisiting_asset_id = 0;
        }
        
        $cplv = can_process_import_asset($myrow, $data, $Mode);
        if($cplv['success'] !== true){
            update_import_asset_record($myrow['id'], $cplv['message']);   
            continue;   
        }
        
        if(!$import){
            update_import_asset_record($myrow['id'], "Can import");   
            continue;   
        }
        else{
            save_import_asset($data, $Mode);
        }
        
        //break;
    }
}
//----------------------------------------------------------------------------------


function save_import_asset($data, $Mode)
{
    if ($Mode == 'ADD_ITEM'){
         
        add_asset($data['asset_code'], $data['description'], $data['long_description'], $data['asset_type_id'], $data['multiple_items'],
                        $data['asset_group_id'], $data['is_hired'], $data['asset_category_id'], $data['asset_subgroup_id'], $data['asset_serial'], 
                        $data['make'], $data['model'], $data['po_number'], $data['purchase_value'], $data['current_value'], 
                        $data['useful_life'], $data['extended_life'], $data['rate_category_id'], $data['inactive'], $data['project_id']); 
                
        update_import_asset_record($data['id'],"Record Added");
    } 
    else{
               
        update_asset($data['asset_id'], $data['asset_code'], $data['description'], $data['long_description'], $data['multiple_items'],
                        $data['asset_group_id'], $data['is_hired'], $data['asset_category_id'], $data['asset_subgroup_id'], $data['asset_serial'], 
                        $data['make'], $data['model'], $data['po_number'], $data['purchase_value'], $data['current_value'], 
                        $data['useful_life'], $data['extended_life'], $data['rate_category_id'], $data['inactive'], $data['project_id']); 

        update_import_asset_record($data['id'],"Record Updated");
    }
    
}

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


function update_import_asset_record($id, $message)
{
    $sql = "UPDATE import_assets SET message = ".db_escape($message)." WHERE id = ".$id;
    db_query($sql);
}


?>