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

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

function update_est_project_boq_item_groups($selected_id, $code, $description, $display_order, $parent_id )                     
                                        
{
        $id_user = $_SESSION['wa_current_user']->user;
        $datetime = date('Y-m-d H:i:s');
        $sql = "UPDATE ".TB_PREF."est_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).", 
                    updated_by=".db_escape($id_user).",
                    updated_on=".db_escape($datetime)."
                        
                WHERE id=".db_escape($selected_id);

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

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

function get_est_project_boq_item_groups($est_project_id)
{
	$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.est_project_id 
                       
                FROM ".TB_PREF."est_project_boq_item_groups pbig     
                        LEFT JOIN ".TB_PREF."project_boq_item_groups pbigp ON 
                                pbig.parent_id = pbigp.id                     
                WHERE pbig.est_project_id=".db_escape($est_project_id)." 
                ORDER BY pbig.position, pbig.display_order";               
                     
	return db_query($sql,"Estimation Project BOQ item group could not be retreived");
}

function get_est_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."est_project_boq_item_groups pbig,    
                     ".TB_PREF."est_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_est_project_boq_item_groups($est_project_id, $code, $selected_id)
{
    $sql = "SELECT code
            FROM ".TB_PREF."est_project_boq_item_groups
            WHERE est_project_id = ".db_escape($est_project_id)."AND 
                    code = ".db_escape($code)." AND    
                    id <> ".db_escape($selected_id);
    
    $result = db_query($sql, "get_existing_code_from_est_project_boq_item_groups");
    return db_num_rows($result);    
}

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


function get_est_project_boq_item_group_record($id)
{
    $sql = "SELECT epbig.*                        
            FROM ".TB_PREF."est_project_boq_item_groups epbig                     
            WHERE epbig.id=".db_escape($id);         

    $result = db_query($sql,"Could not get_est_project_boq_item_group_record");	
    return db_fetch($result);
}

function get_est_project_boq_item_group_record_by_code($est_project_id, $code)
{
    $sql = "SELECT pbig.*                        
            FROM ".TB_PREF."est_project_boq_item_groups pbig                     
            WHERE pbig.est_project_id = ".db_escape($est_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_est_boq_groups($est_project_id)
{
    $sql = "SELECT ep_boqg.*
            FROM ".TB_PREF."est_project_boq_item_groups ep_boqg                      
            WHERE  ep_boqg.est_project_id=".db_escape($est_project_id)." 
                    AND ep_boqg.parent_id = 0 
            ORDER BY ep_boqg.display_order";
	
	return db_query($sql,"Could not get top level estimation boq_groups");
}

function get_child_est_boq_groups($est_parent_id)
{
    $sql ="SELECT ep_boqg.*  
            FROM ".TB_PREF."est_project_boq_item_groups ep_boqg                      
            WHERE ep_boqg.parent_id = ".db_escape($est_parent_id)." 
            ORDER BY ep_boqg.display_order";
	
	return db_query($sql,"Could not get child estimation boq groups");
}

function update_est_project_boq_group_totals($boq_group_id, $total)
{
    $updated_on = date('Y-m-d H:i:s');
    $sql = "UPDATE ".TB_PREF."est_project_boq_item_groups 
            SET                    
                total=".db_escape($total)." 
            WHERE id=".db_escape($boq_group_id);

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

}


function update_est_boq_item_group_positions($boq_groups)
{
    foreach ($boq_groups as $boq_group) {
            update_est_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_est_boq_item_group_position($boq_group)
{

    $sql = "UPDATE ".TB_PREF."est_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_est_boq_item_group_position($child_boq_group);
    }
}


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

function get_project_and_est_project_boq_item_groups($est_project_id, $project_id)
{
    $sql = "SELECT estg.*,
                    sysg.id sys_boq_group_id,
                    estg.id est_boq_group_id 
            FROM ".TB_PREF."est_project_boq_item_groups estg 
                JOIN ".TB_PREF."project_boq_item_groups sysg 
                    ON estg.code = sysg.code 
            WHERE estg.est_project_id = ".db_escape($est_project_id)." 
                AND sysg.project_id = ".db_escape($project_id)." 
            ORDER BY estg.position";               

    return db_query($sql,"get_project_and_est_project_boq_item_groups");
}


function get_project_and_est_project_boq_item_group($est_project_id, $filtering_est_group_id, $project_id)
{
    $sql = "SELECT estg.*,
                    sysg.id sys_boq_group_id,
                    estg.id est_boq_group_id 
            FROM ".TB_PREF."est_project_boq_item_groups estg 
                JOIN ".TB_PREF."project_boq_item_groups sysg 
                    ON estg.code = sysg.code 
            WHERE estg.est_project_id = ".db_escape($est_project_id)." 
                AND sysg.project_id = ".db_escape($project_id)." 
                AND estg.id = ".db_escape($filtering_est_group_id)." 
            ORDER BY estg.position";               
                     
     $result = db_query($sql,"get_project_and_est_project_boq_item_groups");
     return db_fetch($result);
}


function get_est_project_and_another_est_project_boq_item_groups($from_est_project_id, $to_est_project_id)
{
    $sql = "SELECT festg.*,
                    testg.id to_est_boq_group_id,
                    festg.id from_est_boq_group_id 
            FROM ".TB_PREF."est_project_boq_item_groups festg 
                JOIN ".TB_PREF."est_project_boq_item_groups testg 
                    ON festg.code = testg.code 
            WHERE festg.est_project_id = ".db_escape($from_est_project_id)." 
                AND testg.est_project_id = ".db_escape($to_est_project_id)." 
            ORDER BY festg.position";               

    return db_query($sql,"get_est_project_and_another_est_project_boq_item_groups");
}

function get_est_project_and_another_est_project_boq_item_group($from_est_project_id, $filtering_est_group_id, $to_est_project_id)
{
    $sql = "SELECT festg.*,
                    testg.id to_est_boq_group_id,
                    festg.id from_est_boq_group_id 
            FROM ".TB_PREF."est_project_boq_item_groups festg 
                JOIN ".TB_PREF."est_project_boq_item_groups testg 
                    ON festg.code = testg.code 
            WHERE festg.est_project_id = ".db_escape($from_est_project_id)." 
                AND testg.est_project_id = ".db_escape($to_est_project_id)." 
                AND festg.id = ".db_escape($filtering_est_group_id)." 
            ORDER BY festg.position";               

    $result = db_query($sql,"get_est_project_and_another_est_project_boq_item_groups");
    return db_fetch($result);
}


?>

































































































