<?php
$page_security = 'SA_PROJECT_PAY_APP_INQ';
$path_to_root = "../..";
include_once($path_to_root . "/includes/db_pager.inc");
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/ui/project_payment_application_ui.inc");
include_once($path_to_root . "/projects/includes/db/project_payment_application_db.inc");
include_once($path_to_root . "/projects/includes/db/project_payment_application_details_db.inc");
include_once($path_to_root . "/projects/includes/db/project_boq_item_groups_db.inc");
include_once($path_to_root . "/projects/includes/db/project_payment_application_inquiry_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"]))
{
	$_POST["trans_no"] = $_GET["trans_no"];
}

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

page(_($help_context = "Project Payment Application BOQ Group Summaries"), true, false, false, $js);

function display_ppa_boq_item_groups($payment_application_id)
{
    global $Ajax;
    
    if(!($payment_application_id > 0)){
        return;
    }
    
    
    display_heading("BOQ Group Summary");
    
//    br();
    
    $boq_groups = array();
    
    $Ajax->activate('iou_details');

    $pa = get_project_payment_application_header($payment_application_id);
    $project_id = $pa['project_id'];
    
    build_pa_boq_groups_array($boq_groups, $project_id, $payment_application_id);
    
//    $result = get_project_boq_item_groups($project_id);
    div_start('iou_details');

    start_table(TABLESTYLE, "width=80%");

    $th = array(_("Group"), _("Group Total"), _("Group Taxes Total"), "");
    table_header($th);

    $k=0; //row colour counter

//    while ($myrow = db_fetch($result))
    foreach ($boq_groups as $myrow) {
            display_ppa_boq_item_group($myrow, $payment_application_id);
    }
    
    hidden('trans_no', $payment_application_id);
   
//    start_row();
//    display_ppa_no_boq_item_group($payment_application_id);
//    end_row();   
    
    display_pa_total($payment_application_id, $boq_groups);
    
    end_table(1);
    div_end();
}

function display_ppa_no_boq_item_group($pa_id)
{

    $boq_groups = get_ppa_no_boq_item_group($pa_id);
    label_cell("Budgets with no BOQ Item Group");
//    amount_decimal_cell($boq_groups["child_groups_total"]); 
//    amount_decimal_cell($boq_groups["child_groups_taxes_total"]); 
    amount_decimal_cell($boq_groups["boq_items_total"]); 
    amount_decimal_cell($boq_groups["boq_items_taxes_total"]); 

    label_cell("");
    label_cell("");
}

function display_ppa_header($pa_no)
{
    display_heading("Payment Application ".$pa_no);
    
    $myrow = view_project_payment_application_header($pa_no);
    start_table(TABLESTYLE2, "width=60%");
        start_row();
        label_cells(_("Reference"), $myrow['reference'], "class='tableheader2'");
        end_row();
        start_row();
        label_cells(_("Project Name"), $myrow['project_description'], "class='tableheader2'");
        end_row();
        start_row();
        label_cells(_("Job"), $myrow['project_site_description'], "class='tableheader2'"); //project_site_id
        end_row();
        start_row();
        label_cells(_("Customer"), $myrow['customer_name'], "class='tableheader2'");
        end_row();
        start_row();
        label_cells(_("Branch"), $myrow['cust_branch_name'], "class='tableheader2'");
        end_row();
        start_row();
        label_cells(_("Project Manager"), $myrow['project_manager_name'], "class='tableheader2'");
        end_row();
        start_row();
        label_cells(_("Date"), sql2date($myrow['ord_date']), "class='tableheader2'");
        end_row();
    end_table(1);

    $_POST['project_id'] = $myrow['project_id'];
    
}



global $selected_id, $Ajax;





start_form();

display_ppa_header($payment_application_id);

display_ppa_boq_item_groups($payment_application_id);





end_form();


end_page();
