<?php

function add_asset($asset_code, $description, $long_description, $type, $multiple_items,
                   $asset_group_id, $is_hired, $asset_category_id, $asset_subgroup_id, $asset_serial,
                   $make, $model, $po_number, $cost, $current_value, $useful_life,
                   $extended_life, $rate_category_id, $inactive, $owner_project_id, $next_service_days=0, 
                   $status=0)
        
{
        $id_user = $_SESSION['wa_current_user']->user;
        $datetime = date('Y-m-d H:i:s');
        

        $sql = "INSERT INTO ".TB_PREF."asset_master (asset_code, description, long_description, type, multiple_items,
                                            group_id, is_hired, asset_category_id, subgroup_id, asset_serial,  
                                            make, model, po_number, purchase_value, current_value,
                                            useful_life, extended_life, rate_category_id, inactive,
                                            owner_project_id, next_service_days, status, created_by, created_on)                                                                                                  
                                               
                VALUES (".db_escape($asset_code).
                        ",".db_escape($description).
                        ",".db_escape($long_description).
                        ",".db_escape($type).
                        ",".db_escape($multiple_items).
                        ",".db_escape($asset_group_id).
                        ",".db_escape($is_hired).
                        ",".db_escape($asset_category_id).
                        ",".db_escape($asset_subgroup_id).
                        ",".db_escape($asset_serial).                       
                        ",".db_escape($make).
                        ",".db_escape($model).
                        ",".db_escape($po_number).
                        ",".db_escape($cost). 
                        ",".db_escape($current_value). 
                        ",".db_escape($useful_life).
                        ",".db_escape($extended_life).
                        ",".db_escape($rate_category_id).
                        ",".db_escape($inactive).
                        ",".db_escape($owner_project_id).
                        ",".db_escape($next_service_days).
                        ",".db_escape($status).
                        ",".db_escape($id_user).
                        ",".db_escape($datetime).")";                      

	 db_query($sql, "could not add asset.");
         return db_insert_id();
}

function update_asset( $asset_id, $asset_code, $description, $long_description, $multiple_items,
                       $asset_group_id, $is_hired, $asset_category_id, $asset_subgroup_id, $asset_serial,
                       $make, $model, $po_number, $cost, $current_value, $useful_life,
                       $extended_life, $rate_category_id, $inactive, $owner_project_id, $next_service_days=0, $status=0) 
{
    $id_user = $_SESSION['wa_current_user']->user;
    $datetime = date('Y-m-d H:i:s');
	$sql = "UPDATE ".TB_PREF."asset_master SET asset_code=".db_escape($asset_code).
                                    ", description=".db_escape($description).
                                    ", long_description=".db_escape($long_description).
                                    ", multiple_items=".db_escape($multiple_items).
                                    ", group_id=".db_escape($asset_group_id). 
                                    ", is_hired=".db_escape($is_hired). 
                                    ", asset_category_id=".db_escape($asset_category_id).
                                    ", subgroup_id=".db_escape($asset_subgroup_id).
                                    ", asset_serial=".db_escape($asset_serial).                                    
                                    ", make=".db_escape($make).
                                    ", model=".db_escape($model).
                                    ", po_number=".db_escape($po_number).
                                    ", purchase_value=".db_escape($cost).   
                                    ", current_value=".db_escape($current_value).   
                                    ", useful_life=".db_escape($useful_life).
                                    ", extended_life=".db_escape($extended_life).                                  
                                    ", rate_category_id=".db_escape($rate_category_id).
                                    ", inactive=".db_escape($inactive).
                                    ", owner_project_id=".db_escape($owner_project_id).
                                    ", next_service_days=".db_escape($next_service_days).
                                    ", status=".db_escape($status).
                                    ", updated_by=".db_escape($id_user).
                                    ", updated_on=".db_escape($datetime)."                                    
                                    
			WHERE asset_id=".db_escape($asset_id);

	db_query($sql, "could not update asset");
}

function get_all_assets($all=false)
{
	$sql = "SELECT *, ".TB_PREF."get_asset_value(a.asset_id, YEAR(now())) as current_value 
		FROM ".TB_PREF."asset_categories at INNER JOIN ".TB_PREF."assets a ON at.asset_type_id = a.asset_type_id";
	if (!$all) $sql .= " WHERE !a.inactive";

	return db_query($sql, "could not get all assets");
}

function get_one_asset($assetid)
{
	$sql = "SELECT  asset_type_id,
                        asset_name,
                        asset_serial,
                        purchase_date,
                        purchase_value                       
            
		FROM ".TB_PREF."assets                                      

	        WHERE asset_id=".db_escape($assetid);                     
                      
                      
	return db_query($sql, "could not get one assets");
        
}

//function get_asset($assetid)
//{
//	$sql = "SELECT * FROM ".TB_PREF."asset_groups at INNER JOIN ".TB_PREF."assets a ON at.asset_type_id = a.asset_type_id ".
//	"WHERE a.asset_id=".db_escape($assetid);
//
//	$result = db_query($sql, "could not get asset");
//
//	return db_fetch($result);
//}
function get_asset($asset_id)
{
	$sql="SELECT * FROM ".TB_PREF."asset_master WHERE asset_id=".db_escape($asset_id);

	$result = db_query($sql, "could not retrive asset");
        return db_fetch($result);
}


function get_asset_by_code($asset_code)
{
	$sql="SELECT * FROM ".TB_PREF."asset_master WHERE asset_code=".db_escape($asset_code);

	$result = db_query($sql, "could not retrive asset");
        return db_fetch($result);
}

function delete_asset($asset_id)
{
	$sql="DELETE FROM ".TB_PREF."assets WHERE asset_id=".db_escape($asset_id);

	db_query($sql, "could not delete asset");
}

function asset_in_amortisation($selected_id)
{
	$sql="SELECT COUNT(*) FROM ".TB_PREF."amortisation WHERE asset_id=".db_escape($selected_id);

	$result = db_query($sql, "could not query amortisation");
	$myrow = db_fetch_row($result);
	return ($myrow[0] > 0); 
}

function get_existing_asset_code($selected_id, $asset_code)
{
    $sql = "SELECT asset_code
            FROM ".TB_PREF."asset_master  
            WHERE asset_code=".db_escape($asset_code)." AND
                  asset_id <>".db_escape($selected_id);               
    
    $result = db_query($sql, "Could not get_existing_asset_code");
    return db_num_rows($result);    
    
}

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

function add_asset_add_gl_trans($tran_type, $trans_no, $asset_id, $date_, 
                                    $cost, $memo_)
{

	if ($cost > 0)
	{

		$asset_gl_codes = get_asset_gl_codes($asset_id);

		add_gl_trans_std_cost($tran_type, $trans_no, $date_, $asset_gl_codes['asset_account'], 0, 0, $memo_, -$cost);

		add_gl_trans_std_cost($tran_type, $trans_no, $date_, -$asset_gl_codes['accumulated_account'], 0, 0, $memo_, $cost);
	}
}


function get_asset_gl_codes($asset_id)
{
	$sql="SELECT ac.* 
            FROM ".TB_PREF."asset_master am 
                JOIN ".TB_PREF."asset_catergories ac ON 
                    am.asset_category_id = ac.id 
                    WHERE asset_id=".db_escape($asset_id);

	$result = db_query($sql, "could not get_asset_gl_codes");
        return db_fetch($result);
}
//-------------------------------------------------------------------------------------------------------------


function get_tool_rate_category($asset_id)     
{
    $sql = "SELECT ar.id, ar.rate, 
            ar.description rate_description  
        
            FROM ".TB_PREF."asset_master am 
                JOIN ".TB_PREF."tool_rates ar ON 
                    am.rate_category_id = ar.id 
                
            WHERE am.asset_id =".db_escape($asset_id);
    
    $result = db_query($sql);
    return db_fetch($result); 

}


function add_asset_move($trans_type, $trans_no, $line_id, $reference, 
                        $tran_date_, $asset_id, $loc_code, $site_id, 
                        $task_id, $qty, $project_id = 0)
{
        $datetime = date('Y-m-d H:i:s');
        
        $sql = "INSERT INTO ".TB_PREF."asset_moves(
                        trans_type, 
                        trans_no, line_id, 
                        reference, tran_date, 
                        asset_id, loc_code, 
                        site_id, task_id, 
                        qty, project_id, created_on)                                                                                                 
                                               
                VALUES (".db_escape($trans_type).
                        ",".db_escape($trans_no).
                        ",".db_escape($line_id).
                        ",".db_escape($reference).
                        ",".db_escape(date2sql($tran_date_)).
                        ",".db_escape($asset_id).
                        ",".db_escape($loc_code).
                        ",".db_escape($site_id).
                        ",".db_escape($task_id).
                        ",".db_escape($qty).      
                        ",".db_escape($project_id).
                        ",".db_escape($datetime).")";                      

	 db_query($sql, "could not add_asset_move.");
//         return db_insert_id();
}

function update_asset_owner_project($asset_id, $to_project_id)
{
    $sql = "UPDATE ".TB_PREF."asset_master
            SET owner_project_id=".db_escape($to_project_id)." 
            WHERE asset_id=".db_escape($asset_id);
    
    db_query($sql);
    
}


function get_asset_total_qty($asset_id) {
    $exclude_array = array(ST_DAILY_TOOL_ISSUE, ST_DAILY_TOOL_RETURN, ST_TOOL_ISSUE, ST_TOOL_RETURN);
    
    $sql = "SELECT SUM(qty) total_qty 
            FROM ".TB_PREF."asset_moves  
            WHERE asset_id = ".db_escape($asset_id)." 
                AND trans_type NOT IN (". implode(',', $exclude_array).")";
    
        $result = db_query($sql, 'get_asset_total_qty');
        return db_result_value($result, 'total_qty');
}
