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

 function add_est_boq_item_transport($boq_item_id, $detail_type_code,
                        $vehicle_type_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."est_project_boq_item_transport          
                    (boq_item_id, detail_type_code, 
                     vehicle_type_id, rate, 
                     days, comment, 
                     created_by, created_on)                   
            VALUES (".db_escape($boq_item_id).",  ".db_escape($detail_type_code).", 
                    ".db_escape($vehicle_type_id).", ".db_escape($rate).",  
                    ".db_escape($days).", ".db_escape($comment).",  
                    ".db_escape($created_by).", ".db_escape($created_on).")";                     
                                
    db_query($sql, "Could not add estimation boq transport"); 
    
    $result = recalcualte_est_boq_item_total_cost($boq_item_id, $detail_type_code, BOQPT_TRANSPORT);
    if($result){
        return array("success" => true); 
    }
    else{
        return array("success" => false); 
    } 
    
}
 
 
function update_est_boq_item_transport($id, $boq_item_id, $detail_type_code,
                                $vehicle_type_id, $rate, 
                                $days, $comment)
{
    $id_user = $_SESSION['wa_current_user']->user;
    $datetime = date('Y-m-d H:i:s');

        $sql = "UPDATE ".TB_PREF."est_project_boq_item_transport
                    SET detail_type_code=".db_escape($detail_type_code).                       
                        ", vehicle_type_id=".db_escape($vehicle_type_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 estimation boq item transport");
    
    $result = recalcualte_est_boq_item_total_cost($boq_item_id, $detail_type_code, BOQPT_TRANSPORT);
    if($result){
        return array("success" => true); 
    }
    else{
        return array("success" => false); 
    }
}
     

function get_est_boq_item_transport_record($selected_id)
{
    $sql = "SELECT * 
            FROM ".TB_PREF."est_project_boq_item_transport 
            WHERE id=".db_escape($selected_id);
    
        $result = db_query($sql, "Could not estimation get boq item transport");
        return db_fetch($result);
}



function get_est_boq_item_transport($boq_item_id, $detail_type_code)
{
    $sql = "SELECT t.*,
                    vt.value_name vehicle_type_name 
        
            FROM ".TB_PREF."est_project_boq_item_transport t   
                    LEFT JOIN ".TB_PREF."list_values vt 
                    ON t.vehicle_type_id = vt.id 
                
            WHERE t.boq_item_id=".db_escape($boq_item_id)." 
                AND t.detail_type_code=".db_escape($detail_type_code);
    
    return db_query($sql, "Could not get estimation boq transport.");
}


function delete_est_boq_item_transport($id, $boq_item_id, $detail_type_code)
{
    $sql="DELETE FROM ".TB_PREF."est_project_boq_item_transport WHERE id= ".db_escape($id);
	db_query($sql,"Selected estimation boq transport could not be deleted");		
    
    $result = recalcualte_est_boq_item_total_cost($boq_item_id, $detail_type_code, BOQPT_TRANSPORT);
    if($result){
        return array("success" => true); 
    }
    else{
        return array("success" => false); 
    }
    
}



function recalculate_est_boq_item_component_transport($boq_item_id, $detail_type_code, $primary_type)
{
    $primary_cost_total = recalculate_est_boq_item_component_primary_transport_sum($boq_item_id, $detail_type_code);
    return update_est_boq_item_component_total_cost($boq_item_id, $detail_type_code, $primary_type, $primary_cost_total);
}

function recalculate_est_boq_item_component_primary_transport_sum($boq_item_id, $detail_type_code)
{
    $sql = "SELECT SUM(rate * days) total_cost 
            FROM ".TB_PREF."est_project_boq_item_transport  
            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_est_boq_item_component_primary_transport($boq_item_id, $detail_type_code)
{
    $sql="DELETE FROM ".TB_PREF."est_project_boq_item_transport 
          WHERE boq_item_id = ".db_escape($boq_item_id)." 
             AND detail_type_code = ".db_escape($detail_type_code);
    
    db_query($sql,"Could not delete_est_boq_item_component_primary_transport");		
}

?>





























