<?php
/**********************************************************************
***********************************************************************/
$path_to_root = "..";
include_once($path_to_root . "/projects/includes/db/project_boq_item_groups_db.inc");

function build_boq_groups_summary_array(&$items, $project_id)
{
    $loop_count = 1;
    
    $boq_groups_top_q = get_top_level_boq_groups($project_id); 
    
    while ($boq_groups_top = db_fetch($boq_groups_top_q))
    {
        $line = array();
        $line['id'] = $boq_groups_top['id']; 
        $line['code'] = $boq_groups_top['code']; 
        $line['description'] = $boq_groups_top['description']; 
        $line['parent_id'] = $boq_groups_top['parent_id']; 

        $line['budgets_total'] = $boq_groups_top['budgets_total']; 
        $line['budgets_used_total'] = $boq_groups_top['budgets_used_total']; 
        $line['budgets_allocated_total'] = $boq_groups_top['budgets_allocated_total']; 
        
        $line['depth'] = $loop_count;
        
        $child_groups_array = add_child_boq_groups_to_array($boq_groups_top['id'], $loop_count);
        if($child_groups_array){
            foreach ($child_groups_array as $child_group){
                $line['budgets_total'] += $child_group['budgets_total'];
                $line['budgets_used_total'] += $child_group['budgets_used_total'];                
                $line['budgets_allocated_total'] += $child_group['budgets_allocated_total'];
            }      
            $line['children'] = $child_groups_array;
        }
        else{
            $line['children'] = array();
        }       

        $items[] = $line;
        
    }

}
//function get_group_company_employee($company_code, $employee_code)
//{
//    $db_name = get_group_company_db_name($company_code);
//    
//    if(isset($db_name) && strlen($db_name) > 0){
//        $sql = "SELECT * FROM ".$db_name.".".TB_PREF."employee_master WHERE employee_code=".db_escape($employee_code);
//    }
//    else{
//        $sql = "SELECT * FROM ".TB_PREF."employee_master WHERE employee_code=".db_escape($employee_code);
//    }
//    
//	$result = db_query($sql,"an employee could not be retreived");
//
//	return db_fetch($result);
//}

//function get_group_company_db_name($company_code)
//{
//    if(strlen($company_code) > 0){
//        $sql = "SELECT db_name FROM group_companies 
//                WHERE company_code = ".db_escape($company_code);
//    }
//    else{
//        $sql = "SELECT db_name FROM group_companies 
//                WHERE default_company = 1 LIMIT 1";
//    }
//
//	$res = db_query($sql, "Could not get_group_company_db_name");
//    $row = db_fetch($res);
//    
//    $db_name = '';
//	if(isset($row['db_name'])){
//		$db_name = $row['db_name'];
//    }
//
//	return $db_name;
//}

function get_group_company_employee_formatted_name($company_code, $employee_code)
{
    $emp = get_group_company_employee($company_code, $employee_code);
    if(isset($emp['employee_code'])){
        return ("[".$emp['employee_code']."] - ". $emp['initials']." ".$emp['last_name']." (".$emp['first_name'].")");
    }
    else{
        return "[".$employee_code."] - Can not access the database of this employee";
    }

}

function add_child_boq_groups_to_array($parent_id, &$loop_count)
{
    $loop_count++;
    if($loop_count > 20){
        //Stop infinite recursive loop
        return null;
    }
    $boq_groups_array = array();

        
    
    $child_boq_groups_q = get_child_boq_groups($parent_id);   
    
    while ($boq_group = db_fetch($child_boq_groups_q))
    {
        $line = array();
        $line['id'] = $boq_group['id']; 
        $line['code'] = $boq_group['code']; 
        $line['description'] = $boq_group['description']; 
        $line['parent_id'] = $boq_group['parent_id']; 
        
        $line['budgets_total'] = $boq_group['budgets_total']; 
        $line['budgets_used_total'] = $boq_group['budgets_used_total']; 
        $line['budgets_allocated_total'] = $boq_group['budgets_allocated_total'];

        $line['depth'] = $loop_count;

        $child_groups_array = add_child_boq_groups_to_array($boq_group['id'], $loop_count);
        if($child_groups_array){
            foreach ($child_groups_array as $child_group){
                $line['budgets_total'] += $child_group['budgets_total'];
                $line['budgets_used_total'] += $child_group['budgets_used_total'];                
                $line['budgets_allocated_total'] += $child_group['budgets_allocated_total'];
            }      
            $line['children'] = $child_groups_array;
        }
        else{
            $line['children'] = array();
        }       

        $boq_groups_array[] = $line;
    }
    
    $loop_count--;

    return $boq_groups_array;

}
function get_department_name($department_id)
{
    $sql = "SELECT value_name dep 
            FROM ".TB_PREF."list_values
            WHERE  id=".db_escape($department_id);
    
    $result= db_query($sql, "Could not get department details");
    return db_fetch($result);
    
}
function get_employement_record($selected_id)
{
	$sql = "SELECT *    
		FROM "
                    .TB_PREF."employment_details 
                WHERE 
                    employee_code =".db_escape($selected_id)." ORDER BY id DESC LIMIT 1";	
	
	$result = db_query($sql,"Employment history record could not be retreived");
        return db_fetch($result);
}
//function get_project_name($project_id)
// {
//     $sql = "SELECT  
//                    description,project_name
//             FROM    ".TB_PREF."projects_master
//             WHERE project_id=".db_escape($project_id);
//            
//       $result = db_query($sql, "Department could not be retrieved");
//       return db_fetch($result);
//     
// }
 
 function get_list_value_name_by_id($code,$id)
 {
     $sql = "SELECT  
                    value_name
             FROM    ".TB_PREF."list_values
             WHERE id=".db_escape($id)." AND list_code =". db_escape($code);
            
       $result = db_query($sql, "list data could not be retrieved");
       return db_fetch($result);
     
 }


?>