<?php
/**********************************************************************
***********************************************************************/
function write_approval_group($selected, $approval_group_code, $approval_group_description)
{
    if($selected > 0 ){
		$sql = "UPDATE ".TB_PREF."approval_group SET
                        approval_group_code = ".db_escape($approval_group_code).",
                        approval_group_description = ".db_escape($approval_group_description)."
                WHERE approval_group_id = ".db_escape($selected);
    }
    else{
		$sql = "INSERT INTO ".TB_PREF."approval_group(
                        approval_group_code,
                        approval_group_description) 
                VALUES( ".db_escape($approval_group_description).","
                        .db_escape($approval_group_description).")";
    }

	db_query($sql,"Could not write_approval_group");
}


function get_all_budget_group_codes_and_boq_item_codes($project_id, $boq_group_id)
{
    $sql = "SELECT DISTINCT code 
            FROM ".TB_PREF."project_task_groups 
            WHERE project_id = ".db_escape($project_id)." AND 
                    boq_group_id = ".db_escape($boq_group_id)."
            UNION 
            SELECT distinct code 
            FROM ".TB_PREF."project_boq_items 
            WHERE project_id = ".db_escape($project_id)." AND 
                    group_id = ".db_escape($boq_group_id)." 
            ORDER BY code";
    
    return db_query($sql,"Could not write_approval_group");
}


function get_project_boq_group_boq_item_components($project_id, $boq_group_id)
{
    $sql = "SELECT c.id component_id,
                    c.detail_type_code,
                    c.cost,
                    c.boq_item_id,
                    i.code boq_item_code,
                    i.description boq_item_description,
                    i.type_id boq_item_type_id,
                    i.long_description boq_item_long_description,
                    dtn.value_name detail_type_name 
            FROM ".TB_PREF."project_boq_item_components c 
                    INNER JOIN ".TB_PREF."project_boq_items i ON 
                            c.boq_item_id = i.id 
                    INNER JOIN ".TB_PREF."list_values dtn ON 
                            c.detail_type_code = dtn.value_code AND 
                            dtn.list_code = 'PROJECT_TASK_DETAIL_TYPES' 
            WHERE i.project_id = ".db_escape($project_id)." AND 
                    i.group_id = ".db_escape($boq_group_id);
    
    return db_query($sql,"Could not get_project_boq_group_boq_item_components");
}


?>