<?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 . "/projects/includes/db/project_boq_item_groups_db.inc");

include_once($path_to_root . "/projects/includes/db/project_boq_items_db.inc");
include_once($path_to_root . "/projects/includes/db/project_boq_item_detail_edit_db.inc");
include_once($path_to_root . "/projects/includes/db/copy_project_boq_items_to_tasks_db.inc");
include_once($path_to_root . "/projects/includes/db/tasks_db.inc");
include_once($path_to_root . "/projects/includes/db/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, "|");
$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_project_boq_group($project_id, $boq_group_id)
{
    if(!($boq_group_id > 0)){
        $boq_group_id = 0;
    }
    
    $project = get_project($project_id);
    
    start_table(TABLESTYLE, "width=90%");
    
//        labelheader_cell("Detil Type");
//        labelheader_cell("Budget Group", "colspan=4");
//        labelheader_cell("BOQ Item"); 
    
    $k = 0; //row colour counter

    $boq_group_component_total = $boq_group_budget_total = $boq_group_budget_used_total = $boq_group_budget_allocated_toal = $boq_group_budget_available_total = 0;
    
    $result = get_all_budget_group_codes_and_boq_item_codes($project_id, $boq_group_id);
    
    while ($group_code = db_fetch($result)) {
        
        start_row();
            labelheader_cell("Detil Type");
            labelheader_cell("Budget Group", "colspan=4");
            labelheader_cell("BOQ Item");
        end_row();

//        $th = array(_(''),"Budget", "Used", "Allocated", "Available",_('BOQ Item'));
//        table_header($th);
        start_row();
//            label_cell(spaces(2),"tableheader");
            labelheader_cell(spaces(5));
            label_cell(text_bold('Budget'), "style=background-color:#008080;color:white;text-align:center");
            label_cell(text_bold('Used'), "style=background-color:#008080;color:white;text-align:center");
            label_cell(text_bold('Allocated'), "style=background-color:#008080;color:white;text-align:center");
            label_cell(text_bold('Available'), "style=background-color:#008080;color:white;text-align:center");
            label_cell(text_bold('Component Cost'), "style=background-color:#732673;color:white;text-align:center");
        end_row();
    
        $boq_item = get_project_boq_item_record_by_code($project_id, $group_code['code'], $boq_group_id);
        $boq_item_id = $boq_item['id'];
        $boqi_code = $boq_item['code'];
        $boqi_description = $boq_item['description'];
        
        $task_group = get_task_group_details_by_code($project_id, $group_code['code'], $boq_group_id);
        $tg_code = $task_group['code'];
        $tg_description = $task_group['description'];
        
        start_row();
        label_cell('');
        
        
        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"], htmlspecialchars_decode($tg_name));
            label_cell($s, "align=center colspan=4");
        }
        else{

            label_cell('No Budget Group for code '.$group_code['code'], "align=center colspan=4");
        }
        
        if(strlen($boqi_code) > 0){
            $boqi_name = ('<b>'.$boqi_code.' - '.$boqi_description.'</b>');
            $s = get_project_trans_view_str(ST_PROJECT_BOQ_ITEM, $boq_item_id, $boqi_name);
            label_cell($s, 'align=center');
        }
        else{
            label_cell('No BOQ item for code '.$group_code['code'], 'align=center');
        }
        
        end_row();
        
        $component_total = $budget_total = $budget_used_total = $budget_allocated_toal = $budget_available_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_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_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 || $has_task){
                alt_table_row_color($k);
                label_cell($detail_type['value_name']);
                
                
                        
                if($has_task){
                    amount_decimal_cell($task['budget']);
                    $budget_total += $task['budget'];
                    
                    $task_cost_link = get_task_cost_details_view_str($task['task_id'], number_format($task['budget_used'],2));
                    label_cell($task_cost_link, "align=right");
                    $budget_used_total += $task['budget_used'];
        
                    $cost_allocation_link = get_task_cost_allocation_details_view_str($task['task_id'], number_format($task['budget_allocated'],2));
                    label_cell($cost_allocation_link, "align=right");
                    $budget_allocated_toal += $task['budget_allocated'];
                    
                    $budget_available = $task['budget'] - ($task['budget_used'] + $task['budget_allocated']);
                    amount_decimal_cell($budget_available);
                    $budget_available_total += $budget_available;
        
                }
                else{
                    label_cell('No budget', 'align=center colspan=4');
                }
                
                if($has_boqic){
                    amount_decimal_cell($boqi_component['cost']);
                    $component_total += $boqi_component['cost'];
                }
                else{
                    label_cell('No component');
                }
                
                end_row();
            }
        
        }
        start_row();
        label_cell('<b>'."Total".'</b>', 'align=right');
        
        amount_cell($budget_total, true);
        amount_cell($budget_used_total, true);
        amount_cell($budget_allocated_toal, true);
        amount_cell($budget_available_total, true);
        amount_cell($component_total, true);
        end_row();
        $boq_group_component_total += $component_total;
        $boq_group_budget_total += $budget_total;
        $boq_group_budget_used_total += $budget_used_total;
        $boq_group_budget_allocated_toal += $budget_allocated_toal;
        $boq_group_budget_available_total += $budget_available_total;
        
        start_row();
        label_cell(spaces(1),"colspan=6");
        end_row();
    }    
    
    
    
    start_row();
        label_cell('<b>'."Total for BOQ Item Group".'</b>', 'align=right');
        
        amount_cell($boq_group_budget_total, true);
        amount_cell($boq_group_budget_used_total, true);
        amount_cell($boq_group_budget_allocated_toal, true);
        amount_cell($boq_group_budget_available_total, true);
        amount_cell($boq_group_component_total, true);
    end_row();
    end_table(1);

}


start_form();

br(1);

$user_id = $_SESSION['wa_current_user']->user;
$r = can_access_project($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_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 and Budget Groups with no BOQ Item Group");
}
 br(1);
 
display_project_boq_group($project_id, $boq_group_id);

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

end_form();


end_page();
