<?php
$page_security = 'SA_PROJECT_BOQ_ITEM_GROUPS';
$path_to_root = "../..";

include_once($path_to_root . "/includes/session.inc");
include_once($path_to_root . "/includes/ui/ui_input.inc");

include_once($path_to_root . "/includes/date_functions.inc");
include_once($path_to_root . "/includes/ui.inc");
//include_once($path_to_root . "/includes/data_checks.inc");

include_once($path_to_root . "/estimation/includes/db/est_project_boq_item_groups_db.inc");

include_once($path_to_root . "/estimation/includes/db/est_project_boq_items_db.inc");
include_once($path_to_root . "/estimation/includes/db/est_project_boq_item_detail_edit_db.inc");
//include_once($path_to_root . "/estimation/includes/db/copy_est_project_boq_items_to_tasks_db.inc");
//include_once($path_to_root . "/estimation/includes/db/est_tasks_db.inc");
include_once($path_to_root . "/estimation/includes/db/est_projects_db.inc");

include_once($path_to_root . "/admin/db/list_values_db.inc");

        
$js = '';

if ($SysPrefs->use_popup_windows) {
	$js .= get_js_open_window(900, 500);
}

if (user_use_date_picker()) {
	$js .= get_js_date_picker();
}


simple_page_mode(true);

if (isset($_GET["trans_no"]))
{
	$trans_no = $_GET["trans_no"];
}


$sep_pos = strpos($trans_no, "|");
$est_project_id = substr($trans_no, 0, $sep_pos);
$boq_group_id = substr($trans_no, $sep_pos+1);

$site_id = 0;

page(_($help_context = "BOQ Group Details"), true, false, false, $js);

function display_est_project_boq_group($est_project_id, $boq_group_id)
{
    if(!($boq_group_id > 0)){
        $boq_group_id = 0;
    }
    
    $project = get_est_project($est_project_id);
    
    start_table(TABLESTYLE, "width=90%");
    $th = array(_('Detil Type'),_('BOQ Item'));
//inactive_control_column($th);

    table_header($th);
    $k = 0; //row colour counter

    $result = get_est_boq_group_boq_item_codes($est_project_id, $boq_group_id);
    
    while ($group_code = db_fetch($result)) {
        
        $boq_item = get_est_project_boq_item_record_by_code($est_project_id, $group_code['code']);
        $boq_item_id = $boq_item['id'];
        $boqi_code = $boq_item['code'];
        $boqi_description = $boq_item['description'];
        
//        $task_group = get_est_task_group_details_by_code($est_project_id, $group_code['code']);
//        $tg_code = $task_group['code'];
//        $tg_description = $task_group['description'];
        
        start_row();
        label_cell('');
        if(strlen($boqi_code) > 0){
            $boqi_name = ('<b>'.$boqi_code.' - '.$boqi_description.'</b>');
            $s = get_estimation_trans_view_str(ST_EST_PROJECT_BOQ_ITEM, $boq_item_id, $boqi_name);
            label_cell($s);
        }
        else{
            label_cell('No BOQ item', 'align=center');
        }
        
//        if(strlen($tg_code) > 0){
//            $tg_name = ('<b>'.$tg_code.' - '.$tg_description.'</b>');
//            //$s = get_project_trans_view_str(ST_PROJECT_BUDGET_GROUP, $task_group["id"], $tg_name);
//            label_cell($s);
//        }
//        else{
//
//            label_cell('');
//        }
        
        end_row();
        
        $component_total = $budget_total = 0;
        
        $list_values_result = get_all_list_values('PROJECT_TASK_DETAIL_TYPES');
        while ($detail_type = db_fetch($list_values_result)) {
            $has_boqic = $has_task = false;
            $boqi_component_id = 0;
            $boqi_component = get_est_boq_item_component_record($boq_item['id'], $detail_type['value_code']);
            if(isset($boqi_component['id']) && $boqi_component['id'] > 0){
                $has_boqic = true;
                $boqi_component_id = $boqi_component['id'];
            }
            
//            $task = get_est_task_record_by_detail_type($task_group['id'], $detail_type['value_code']);
//            if(isset($task['task_id']) && $task['task_id'] > 0){
//                $has_task = true;
//            }
//            
            if($has_boqic){
                alt_table_row_color($k);
                label_cell($detail_type['value_name']);
                
                if($has_boqic){
                    amount_decimal_cell($boqi_component['cost']);
                    $component_total += $boqi_component['cost'];
                }
                else{
                    label_cell('');
                }
                        
//                if($has_task){
//                    amount_decimal_cell($task['budget']);
//                    //$budget_total += $task['budget'];
//                }
//                else{
//                    label_cell('No budget', 'align=center');
//                }
                
                end_row();
            }
        
        }
        label_cell('<b>'."Total".'</b>', 'align=right');
        amount_cell($component_total, true);
        //amount_cell($budget_total, true);
    }    
        end_table(1);

}


start_form();

br(1);

$user_id = $_SESSION['wa_current_user']->user;
//$r = can_access_project($est_project_id, $user_id);
//if($r["success"] !== true){
//    display_error($r["message"]);
//    display_footer_exit();
//    exit;
//}


if($boq_group_id > 0){
    $boq_item_group = get_est_project_boq_item_group_record($boq_group_id);
    display_heading("BOQ Item Group : ".$boq_item_group['code']." - ".$boq_item_group['description']);
}
else{
    display_heading("BOQ Items with no BOQ Item Group");
}
 br(1);
 
display_est_project_boq_group($est_project_id, $boq_group_id);

//submit_center_first('Recalculate_Poject_PA', _("Recalculate Payment Application Summaries"), '', 'default');

end_form();


end_page();
