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

 function add_est_boq_item_labour($boq_item_id, $detail_type_code,
                                  $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."est_project_boq_item_labour          
                    (boq_item_id, detail_type_code, 
                     rate_category_id, rate, 
                     days, comment, 
                     created_by, created_on)                   
            VALUES (".db_escape($boq_item_id).",  ".db_escape($detail_type_code).", 
                    ".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 estimation boq labour"); 
    
    $result = recalcualte_est_boq_item_total_cost($boq_item_id, $detail_type_code, BOQPT_LABOUR);
    if($result){
        return array("success" => true); 
    }
    else{
        return array("success" => false); 
    } 
    
}
 
 
function update_est_boq_item_labour($id, $boq_item_id, $detail_type_code,
                                $rate_category_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_labour
                    SET detail_type_code=".db_escape($detail_type_code).                       
                        ", rate_category_id=".db_escape($rate_category_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 labour");
    
    $result = recalcualte_est_boq_item_total_cost($boq_item_id, $detail_type_code, BOQPT_LABOUR);
    if($result){
        return array("success" => true); 
    }
    else{
        return array("success" => false); 
    }
}
     

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



function get_est_boq_item_labour($boq_item_id, $detail_type_code)
{
    $sql = "SELECT l.*,
                    r.description rate_category_description,
                    r.rate current_rate 
        
            FROM ".TB_PREF."est_project_boq_item_labour l  
                    LEFT JOIN ".TB_PREF."employee_rates r ON 
                            l.rate_category_id = r.id 
                
            WHERE  l.boq_item_id=".db_escape($boq_item_id)." AND 
                    l.detail_type_code=".db_escape($detail_type_code);
    
    return db_query($sql, "Could not get estimation boq labour.");
}


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



function recalculate_est_boq_item_component_labour($boq_item_id, $detail_type_code, $primary_type)
{
    $primary_cost_total = recalculate_est_boq_item_component_primary_labour_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_labour_sum($boq_item_id, $detail_type_code)
{
    $sql = "SELECT SUM(rate * days) total_cost 
            FROM ".TB_PREF."est_project_boq_item_labour  
            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_labour($boq_item_id, $detail_type_code)
{
    $sql="DELETE FROM ".TB_PREF."est_project_boq_item_labour 
          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_labour");		
}

?>




























