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



 
 function add_boq_item_asset($boq_item_id, $detail_type_code,
                        $asset_id, $rate_category_id, $rate, 
                        $days, $comment)
                       
 {
    $created_by = $_SESSION['wa_current_user']->user;
    $created_on = date('Y-m-d H:i:s');
 
    $sql = "INSERT INTO ".TB_PREF."project_boq_item_assets          
                    (boq_item_id, detail_type_code, 
                    asset_id, rate_category_id,
                    rate, 
                    days, comment, 
                    created_by, created_on)                   
            VALUES (".db_escape($boq_item_id).",  ".db_escape($detail_type_code).", 
                    ".db_escape($asset_id).", ".db_escape($rate_category_id).",
                    ".db_escape($rate).",
                    ".db_escape($days).", ".db_escape($comment).",  
                    ".db_escape($created_by).", ".db_escape($created_on).")";                     
                                
    db_query($sql, "Could not add boq asset"); 
    
    $result = recalcualte_boq_item_total_cost($boq_item_id, $detail_type_code, BOQPT_ASSET);
    if($result){
        return array("success" => true); 
    }
    else{
        return array("success" => false); 
    } 
    
}
 
 
function update_boq_item_asset($id, $boq_item_id, $detail_type_code,
                                $asset_group_id, $rate, 
                                $days, $comment)
{
    $id_user = $_SESSION['wa_current_user']->user;
    $datetime = date('Y-m-d H:i:s');

        $sql = "UPDATE ".TB_PREF."project_boq_item_assets
                    SET detail_type_code=".db_escape($detail_type_code).                       
//                        ", asset_group_id=".db_escape($asset_group_id). 
                        ", rate=".db_escape($rate). 
                        ", days=".db_escape($days). 
                        ", comment=".db_escape($comment).                                  
                        ", updated_by=".db_escape($id_user). 
                        ", updated_on=".db_escape($datetime)."  
                                    
			WHERE id=".db_escape($id);

	db_query($sql, "Could not update_boq_item_asset");
    
    $result = recalcualte_boq_item_total_cost($boq_item_id, $detail_type_code, BOQPT_ASSET);
    if($result){
        return array("success" => true); 
    }
    else{
        return array("success" => false); 
    }
}
     

function get_boq_item_asset($selected_id)
{
    $sql = "SELECT ba.*,
                    am.asset_code,
                    am.description asset_description,
                    am.rate_category_id asset_master_rate_cetegory_id 
            FROM ".TB_PREF."project_boq_item_assets  ba 
                    JOIN ".TB_PREF."asset_master am ON 
                            ba.asset_id = am.asset_id 
                
            WHERE id=".db_escape($selected_id);
    
        $result = db_query($sql, "Could not get_boq_item_asset");
        return db_fetch($result);
}



function get_boq_item_assets($boq_item_id, $detail_type_code)
{
    $sql = "SELECT ba.*,
                    am.asset_code,
                    am.description asset_description,
                    ar.description rate_category_description,
                    ar.rate current_rate 
        
            FROM ".TB_PREF."project_boq_item_assets ba 
                    JOIN ".TB_PREF."asset_master am ON 
                            ba.asset_id = am.asset_id 
                    LEFT JOIN ".TB_PREF."tool_rates ar ON 
                            ba.rate_category_id = ar.id 
                
            WHERE  ba.boq_item_id=".db_escape($boq_item_id)." AND 
                    ba.detail_type_code=".db_escape($detail_type_code);
            
    return db_query($sql, "Could not get boq assets.");
}


function delete_boq_item_asset($id, $boq_item_id, $detail_type_code)
{
    $sql="DELETE FROM ".TB_PREF."project_boq_item_assets WHERE id= ".db_escape($id);
	db_query($sql,"Selected boq assets could not be deleted");		
    
    $result = recalcualte_boq_item_total_cost($boq_item_id, $detail_type_code, BOQPT_ASSET);
    if($result){
        return array("success" => true); 
    }
    else{
        return array("success" => false); 
    }
    
}



function recalculate_boq_item_component_assets($boq_item_id, $detail_type_code, $primary_type=0)
{
    $primary_cost_total = recalculate_boq_item_component_primary_assets_sum($boq_item_id, $detail_type_code);
    return $primary_cost_total;
//    return update_boq_item_component_total_cost($boq_item_id, $detail_type_code, $primary_type, $primary_cost_total);
}

function recalculate_boq_item_component_primary_assets_sum($boq_item_id, $detail_type_code)
{
    $sql = "SELECT SUM(rate * days) total_cost 
            FROM ".TB_PREF."project_boq_item_assets  
            WHERE boq_item_id=".db_escape($boq_item_id)." AND 
                    detail_type_code=".db_escape($detail_type_code);
    
    $result = db_query($sql);
    $total_cost = db_fetch($result); 
    if(isset($total_cost['total_cost']) && $total_cost['total_cost'] > 0){
        return $total_cost['total_cost'];
    }
    else{
        return 0;
    }
    
}


function delete_boq_item_component_primary_assets($boq_item_id, $detail_type_code)
{
    $sql="DELETE FROM ".TB_PREF."project_boq_item_assets  
            WHERE boq_item_id = ".db_escape($boq_item_id)." AND 
                    detail_type_code = ".db_escape($detail_type_code);
	db_query($sql,"Could not delete_boq_item_component_primary_assets");		
}

?>