<?php //
/**********************************************************************
***********************************************************************/
function add_project_boq_item_groups($project_id, $code, $description, 
                                     $display_order, $parent_id, $depth)                                          
{
        $id_user = $_SESSION['wa_current_user']->user;
        $datetime = date('Y-m-d H:i:s');
                
        $sql = "INSERT INTO ".TB_PREF."project_boq_item_groups 
            
                       ( project_id, code, 
                       description, display_order, 
                       parent_id, depth,
                       created_by, created_on )                
                  
                VALUES (".db_escape($project_id).", ".db_escape($code).", "
                         .db_escape($description).", ".db_escape($display_order).", "
                         .db_escape($parent_id).", ".db_escape($depth).", "
                         .db_escape($id_user).", " .db_escape($datetime).")";                     

        db_query($sql,"Project BOQ Item Group could not be added");	
        
        return db_insert_id();
}

function update_project_boq_item_groups($selected_id, $code, $description, $display_order, $parent_id, $depth)                     
                                        
{
        $id_user = $_SESSION['wa_current_user']->user;
        $datetime = date('Y-m-d H:i:s');
        $sql = "UPDATE ".TB_PREF."project_boq_item_groups
                SET 
                    code=".db_escape($code).", 
                    description=".db_escape($description).",       
                    display_order=".db_escape($display_order).",
                    parent_id=".db_escape($parent_id).", 
                    depth=".db_escape($depth).", 
                    updated_by=".db_escape($id_user).",
                    updated_on=".db_escape($datetime)."
                        
                WHERE id=".db_escape($selected_id);

        db_query($sql,"Project BOQ item Group could not be updated");		
}

function delete_project_boq_item_groups($selected_id)
{
    return;
	$sql="DELETE FROM ".TB_PREF."project_boq_item_groups WHERE id= ".db_escape($selected_id);
	db_query($sql,"Selected Project BOQ item group could not be deleted");			
}

function get_project_boq_item_groups($project_id, $descending = false)
{
	$sql = "SELECT pbig.display_order,
                       pbig.code,
                       pbig.description item_description,
                       pbig.id,
                       pbigp.parent_id,
                       pbigp.description parent_description,
                       pbigp.position,
                       pbigp.depth,
                       pbig.project_id, 
                       pbig.budgets_total, 
                       pbig.budgets_used_total, 
                       pbig.budgets_allocated_total  
                       
                FROM ".TB_PREF."project_boq_item_groups pbig     
                        LEFT JOIN ".TB_PREF."project_boq_item_groups pbigp ON 
                                pbig.parent_id = pbigp.id                     
                WHERE pbig.project_id=".db_escape($project_id);
        
        if($descending == false){
            $sql .= " ORDER BY pbig.position, pbig.display_order";               
        }
        else{
            $sql .= " ORDER BY pbig.position DESC, pbig.display_order DESC";     
        }
                     
	return db_query($sql,"Project BOQ item group could not be retreived");
}

function get_project_boq_item_group_details($id)
{
	$sql = "SELECT pbig.display_order,
                       pbig.code,
                       pbig.description item_description,
                       pbig.id,
                       pb.parent_id,
                       pb.description parent_description
                       
                FROM ".TB_PREF."project_boq_item_groups pbig,    
                     ".TB_PREF."project_boq_item_groups pb 
                    
                WHERE pbig.id = pb.id AND
                      pbig.id=".db_escape($id)." 
                ORDER BY display_order";               
	
	$result = db_query($sql,"project_boq_item_groups could not be retreived");	
	return db_fetch($result);
}

function get_existing_code_from_project_boq_item_groups($project_id, $code, $selected_id)
{
    $sql = "SELECT code
            FROM ".TB_PREF."project_boq_item_groups
            WHERE project_id = ".db_escape($project_id)."AND 
                    code = ".db_escape($code)." AND    
                    id <> ".db_escape($selected_id);
    
    $result = db_query($sql, "Could not get existing code");
    return db_num_rows($result);    
}

function get_selected_id($id)
{
    $sql = "SELECT id
            FROM ".TB_PREF."project_boq_item_groups
            WHERE id=".db_escape($id);
    
    $result = db_query($sql);
    return db_fetch($result);    
}


function get_project_boq_item_group_record($id)
{
	$sql = "SELECT pbig.*                        
                FROM ".TB_PREF."project_boq_item_groups pbig                     
                WHERE pbig.id=".db_escape($id);         
	
	$result = db_query($sql,"Could not get_project_boq_item_group_record");	
	return db_fetch($result);
}

function get_project_boq_item_group_record_by_code($project_id, $code)
{
	$sql = "SELECT pbig.*                        
                FROM ".TB_PREF."project_boq_item_groups pbig                     
                WHERE pbig.project_id=".db_escape($project_id)." AND 
                        pbig.code=".db_escape($code);         
	
	$result = db_query($sql,"Could not get_project_boq_item_group_record_by_code");	
	return db_fetch($result);
}

function get_top_level_boq_groups($project_id)
{
    $sql = "SELECT p_boqg.*
            FROM ".TB_PREF."project_boq_item_groups p_boqg                      
            WHERE  p_boqg.project_id=".db_escape($project_id)." 
                    AND p_boqg.parent_id = 0 
            ORDER BY p_boqg.display_order";
	
	return db_query($sql,"Could not get_top_level_boq_groups");
}

function get_child_boq_groups($parent_id)
{
    $sql ="SELECT p_boqg.*  
            FROM ".TB_PREF."project_boq_item_groups p_boqg                      
            WHERE p_boqg.parent_id = ".db_escape($parent_id)." 
            ORDER BY p_boqg.display_order";
	
	return db_query($sql,"Could not get_child_boq_groups");
}

function update_project_boq_group_totals($boq_group_id, $budgets_total, $budgets_used_total, $budgets_allocated_total)
{
    $updated_on = date('Y-m-d H:i:s');
    $sql = "UPDATE ".TB_PREF."project_boq_item_groups 
            SET                    
                budgets_total=".db_escape($budgets_total).", 
                budgets_used_total=".db_escape($budgets_used_total).", 
                budgets_allocated_total=".db_escape($budgets_allocated_total)."
            WHERE id=".db_escape($boq_group_id);

    db_query($sql,"Could not update_project_boq_group_totals");    

}


function update_boq_item_group_positions($boq_groups)
{
    foreach ($boq_groups as $boq_group) {
            update_boq_item_group_position($boq_group);
    }
//    $sql = "UPDATE ".TB_PREF."project_boq_item_groups 
//            SET                    
//                position =".db_escape($boq_groups['position'])." 
//            WHERE id=".db_escape($boq_group['id']);
//
//    db_query($sql,"Could not update_boq_item_group_position");  
//    
//    foreach ($boq_groups['children'] as $child_boq_group) {
//        update_boq_item_group_position($child_boq_group);
//    }

}


function update_boq_item_group_position($boq_group)
{

    $sql = "UPDATE ".TB_PREF."project_boq_item_groups 
            SET position =".db_escape($boq_group['position']).",
                depth =".db_escape($boq_group['depth'])."  
            WHERE id=".db_escape($boq_group['id']);

    db_query($sql,"Could not update_boq_item_group_position");  
    
    foreach ($boq_group['children'] as $child_boq_group) {
        update_boq_item_group_position($child_boq_group);
    }
}


function get_project_boq_item_group_records($project_id)
{
	$sql = "SELECT pbig.*     
                FROM ".TB_PREF."project_boq_item_groups pbig           
                WHERE pbig.project_id=".db_escape($project_id)." 
                ORDER BY pbig.position";               
                     
	return db_query($sql,"get_project_boq_item_group_records");
}


function get_child_boq_groups_budgets_summary($boq_group_id, $project_id)
{          
        
        $sql = "SELECT SUM(budgets_total) budgets_total, 
                    SUM(budgets_used_total) budgets_used_total, 
                    SUM(budgets_allocated_total) budgets_allocated_total 
                FROM ".TB_PREF."project_boq_item_groups 
                WHERE project_id = ".db_escape($project_id)." 
                    AND parent_id = ".db_escape($boq_group_id);
                
                     
	$result =  db_query($sql,"get_child_boq_groups_budgets_summary");
        
        return db_fetch($result);
}

function get_project_boq_item_groups_with_parent($project_id, $descending = false)
{
	$sql = "SELECT pbig.*,
                       pbigp.code parent_code,
                       pbigp.description parent_description,
                       pbigp.position parent_position,
                       pbigp.depth parent_depth,
                       pbigp.position parent_position                        
                FROM ".TB_PREF."project_boq_item_groups pbig     
                        LEFT JOIN ".TB_PREF."project_boq_item_groups pbigp ON 
                                pbig.parent_id = pbigp.id                     
                WHERE pbig.project_id=".db_escape($project_id);
        
        if($descending == false){
            $sql .= " ORDER BY pbig.position, pbig.display_order";               
        }
        else{
            $sql .= " ORDER BY pbig.position DESC, pbig.display_order DESC";     
        }
                     
	return db_query($sql,"get_project_boq_item_groups_with_parent");
}



function get_project_boq_item_group_record_by_code_with_parent($project_id, $code)
{
	$sql = "SELECT pbig.*,
                        pbigp.code parent_code,
                        pbigp.description parent_description              
                FROM ".TB_PREF."project_boq_item_groups pbig     
                        LEFT JOIN ".TB_PREF."project_boq_item_groups pbigp ON 
                                pbig.parent_id = pbigp.id                           
                WHERE pbig.project_id=".db_escape($project_id)." AND 
                        pbig.code=".db_escape($code);         
	
	$result = db_query($sql,"Could not get_project_boq_item_group_record_by_code_with_parent");	
	return db_fetch($result);
}

?>