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





function get_boq_item_group_boq_item_component_details($project_id, $boq_group_id)
{
 
        $sql = "SELECT v.*, 
                        dtn.value_name detail_type_name  
                FROM ".TB_PREF."vw_project_boq_item_component_details v    
                    JOIN  ".TB_PREF."list_values dtn  
                        ON dtn.value_code = v.detail_type_code 
                        AND dtn.list_code = 'PROJECT_TASK_DETAIL_TYPES'
                WHERE v.project_id =". db_escape($project_id)." AND 
                        v.group_id=".  db_escape($boq_group_id)." 
                ORDER BY v.description, detail_type_code, primary_type";
        
	return db_query($sql,"get_boq_item_group_boq_item_component_details");
    
}


//function get_project_boq_item_component_details($project_id)
function get_project_boq_groups_and_item_component_details($project_id)
{
 
//        $sql1 = "SELECT g.id boq_group_id, 
//                        g.code boq_group_code, 
//                        g.description boq_group_description, 
//                        g.depth, 
//                        g.position,
//                        dtn.value_name detail_type_name,
//                        
//                        v.id boq_item_id,
//                        v.code boq_item_code,
//                        v.description boq_item_description,
//                        v.group_id boq_item_group_id,
//                        v.detail_type_code,
//                        v.primary_type primary_type_id,
//                        v.detail_id,
//                        v.item_type item_type_id,
//                        v.item_id,
//                        v.item_code,
//                        v.unit,
//                        v.item_description,
//                        v.item_qty,
//                        v.rate_id,
//                        v.item_tran_unit_cost,
//                        v.item_comment 
//                FROM ".TB_PREF."project_boq_item_groups g 
//                    LEFT JOIN  ".TB_PREF."vw_project_boq_item_component_details v 
//                        ON v.group_id = g.id  
//                    LEFT JOIN  ".TB_PREF."list_values dtn  
//                        ON dtn.value_code = v.detail_type_code 
//                        AND dtn.list_code = 'PROJECT_TASK_DETAIL_TYPES'
//                WHERE g.project_id = ". db_escape($project_id);
//        
//        
//        $sql2 = "SELECT g.id boq_group_id, 
//                        g.code boq_group_code, 
//                        g.description boq_group_description, 
//                        g.depth, 
//                        g.position,
//                        dtn.value_name detail_type_name,
//                        
//                        tg.id boq_item_id,
//                        tg.code boq_item_code,
//                        tg.description boq_item_description,
//                        0 boq_item_group_id,
//                        '' detail_type_code,
//                        0 primary_type_id,
//                        0 detail_id,
//                        0 item_type_id,
//                        0 item_id,
//                        '' item_code,
//                        '' unit,
//                        '' item_description,
//                        0 item_qty,
//                        0 rate_id,
//                        0 item_tran_unit_cost,
//                        '' item_comment 
//                FROM ".TB_PREF."project_boq_item_groups g 
//                    JOIN  ".TB_PREF."project_task_groups tg  
//                        ON tg.boq_group_id = g.id  
//                    JOIN  ".TB_PREF."project_tasks pt   
//                        ON pt.task_group_id = tg.id  
//                    JOIN  ".TB_PREF."list_values dtn  
//                        ON dtn.value_code = pt.detail_type_code 
//                        AND dtn.list_code = 'PROJECT_TASK_DETAIL_TYPES'
//                WHERE g.project_id = ". db_escape($project_id)." 
//                ORDER BY position, boq_item_code, detail_type_code ";
//        
//        $sql = $sql1." UNION ".$sql2;
        
        
        
        $sql = "SELECT g.id boq_group_id, 
                        g.code boq_group_code, 
                        g.description boq_group_description, 
                        g.depth, 
                        g.position,
                        dtn.value_name detail_type_name,
                        
                        tg.id boq_item_id,
                        tg.code boq_item_code,
                        tg.description boq_item_description,
                        
                        pt.task_id,
                        pt.task_code,
                        pt.description task_description,
                        pt.detail_type_code,
                        
                        v.group_id boq_item_group_id,
                        v.primary_type primary_type_id,
                        v.detail_id,
                        v.item_type item_type_id,
                        v.item_id,
                        v.item_code,
                        v.unit,
                        v.item_description,
                        v.item_qty,
                        v.rate_id,
                        v.item_tran_unit_cost,
                        v.item_comment 
                FROM ".TB_PREF."project_boq_item_groups g  
                    JOIN  ".TB_PREF."project_task_groups tg  
                        ON tg.boq_group_id = g.id  
                    JOIN  ".TB_PREF."project_tasks pt   
                        ON pt.task_group_id = tg.id  
                    JOIN  ".TB_PREF."list_values dtn  
                        ON dtn.value_code = pt.detail_type_code 
                        AND dtn.list_code = 'PROJECT_TASK_DETAIL_TYPES'
                    LEFT JOIN  ".TB_PREF."vw_project_boq_item_component_details v 
                        ON v.group_id = g.id  
                        AND v.id = tg.id  
                        AND pt.detail_type_code = v.detail_type_code  
                WHERE g.project_id = ". db_escape($project_id);
        
	return db_query($sql,"get_project_boq_item_component_details");
    
}

function get_project_no_boq_group_boq_item_component_details($project_id)
{
 
        $sql = "SELECT 
                        dtn.value_name detail_type_name,
                        
                        tg.id boq_item_id,
                        tg.code boq_item_code,
                        tg.description boq_item_description,
                        
                        pt.task_id,
                        pt.task_code,
                        pt.description task_description,
                        pt.detail_type_code,
                        
                        v.group_id boq_item_group_id,
                        v.primary_type primary_type_id,
                        v.detail_id,
                        v.item_type item_type_id,
                        v.item_id,
                        v.item_code,
                        v.unit,
                        v.item_description,
                        v.item_qty,
                        v.rate_id,
                        v.item_tran_unit_cost,
                        v.item_comment 
                FROM ".TB_PREF."project_task_groups tg   
                    JOIN  ".TB_PREF."project_tasks pt   
                        ON pt.task_group_id = tg.id  
                        AND tg.boq_group_id = 0 
                    JOIN  ".TB_PREF."list_values dtn  
                        ON dtn.value_code = pt.detail_type_code 
                        AND dtn.list_code = 'PROJECT_TASK_DETAIL_TYPES'
                    LEFT JOIN  ".TB_PREF."vw_project_boq_item_component_details v 
                        ON v.group_id = 0   
                        AND v.id = tg.id  
                        AND pt.detail_type_code = v.detail_type_code  
                WHERE tg.project_id = ". db_escape($project_id);
                
         
        
	return db_query($sql,"get_project_no_boq_group_boq_item_component_details");
    
}

function get_project_boq_item_component_details($project_id)
{
 
        $sql = "SELECT g.id boq_group_id, 
                        g.code boq_group_code, 
                        g.description boq_group_description, 
                        g.depth, 
                        dtn.value_name detail_type_name,
                        v.* 
                FROM ".TB_PREF."project_boq_item_groups g 
                    LEFT JOIN  ".TB_PREF."vw_project_boq_item_component_details v 
                        ON v.group_id = g.id  
                        AND v.project_id = g.project_id 
                    LEFT JOIN  ".TB_PREF."list_values dtn  
                        ON dtn.value_code = v.detail_type_code 
                        AND dtn.list_code = 'PROJECT_TASK_DETAIL_TYPES'
                WHERE g.project_id = ". db_escape($project_id)." 
                ORDER BY g.position, v.code, v.detail_type_code, v.primary_type ";
        
	return db_query($sql,"get_project_boq_item_component_details");
    
}

function get_boq_groups_and_item_details($project_id) {
    $sql = "SELECT g.id boq_group_id, g.code boq_group_code, g.description boq_group_description, g.depth, g.position,
                tg.id boq_item_id, tg.code boq_item_code, tg.description boq_item_description, tg.start_date, tg.end_date,
                bi.id boq_item_id, bi.code, bi.description, bi.max_quantity, bi.completed_qty, bi.started, bi.actual_start_date, bi.completed, bi.actual_end_date 
            FROM ".TB_PREF."project_boq_item_groups g  
                JOIN ".TB_PREF."project_task_groups tg  
                    ON tg.boq_group_id = g.id  
                JOIN ".TB_PREF."project_boq_items bi   
                    ON tg.id = bi.id  
            WHERE g.project_id = ". db_escape($project_id);
        
	return db_query($sql, "get_boq_groups_and_item_details");
}

function get_no_boq_groups_and_item_details($project_id) {
    $sql = "SELECT tg.id boq_item_id, tg.code boq_item_code, tg.description boq_item_description, tg.start_date, tg.end_date,
                bi.id boq_item_id, bi.code, bi.description, bi.max_quantity, bi.completed_qty, bi.started, bi.actual_start_date, bi.completed, bi.actual_end_date 
            FROM ".TB_PREF."project_task_groups tg  
                JOIN ".TB_PREF."project_boq_items bi   
                    ON tg.id = bi.id  
            WHERE tg.boq_group_id = 0 
                AND tg.project_id = ". db_escape($project_id);
        
	return db_query($sql, "get_no_boq_groups_and_item_details");
}