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

include($path_to_root . "/includes/session.inc");
include_once($path_to_root . "/includes/date_functions.inc");
include_once($path_to_root . "/includes/ui.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_details_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();
}

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

//$boq_group_id = -1;

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


$sep_pos = strpos($trans_no, "|");
$pa_id = substr($trans_no, 0, $sep_pos);
$boq_group_id = substr($trans_no, $sep_pos+1);
        
function display_project_payment_application_items($pa_id, $boq_group_id)
{
    if(!($boq_group_id >= 0)){
        return;
    }
    
    $boq_item_group = get_project_boq_item_group_record($boq_group_id);
    
    display_heading2("Payment Application No : ".$pa_id);
    br(1);
    if($boq_group_id > 0){
        display_heading2("BOQ Items of group : [".$boq_item_group['code']."] ".$boq_item_group['description']);
    }else{
        display_heading2("BOQ Items with no BOQ Item Group");
    }

    br();
    
    global  $project_billing_types; 
    
    div_start('project_payment_application_details');
//  display_heading('Expenses');    
        
    start_table(TABLESTYLE, "width=95%");
    $result_a = view_project_payment_application_boq_items($pa_id, $boq_group_id);   

    $th = array(_("Code"), _("Description"), "Type",
                _("Unit"), "rate", _("Max Qty"), "Max Amount",_("Work Done"), 
                _("Cuml. Qty"),_("Pre. Qty"), _("Curr. Qty"),  _("Cert. Cuml. Qty"));               
   
    table_header($th);

    $j = 1;
    $k=0; //row colour counter
    
    $work_done_total = $previous_total = $current_total = $cuml_total = $cert_cuml_total = $max_amount_total = 0;
    while($pa_row = db_fetch($result_a))
    {
        alt_table_row_color($k);
     
        label_cell($pa_row["code"]);
        label_cell($pa_row["description"]);       
        label_cell($project_billing_types[$pa_row["billing_type"]]);  
        label_cell($pa_row["unit"]); 
        
        $rate = $pa_row["rate"];
        amount_decimal_cell($rate); 
        label_cell($pa_row["max_quantity"], "align=right");  
        $max_amount = $pa_row["rate"] * $pa_row["max_quantity"];
        amount_decimal_cell($max_amount); 
        $max_amount_total += $max_amount;
        
        if($pa_row["total_work_done"] > 0){
            label_cell($pa_row["total_work_done"], "align=right");  
            $work_done_total += $pa_row["total_work_done"] * $rate;
        }
        else{
            label_cell('');
        }
        
        

        $previous_total += $pa_row["previous_qty"] * $rate;
        $current_total += $pa_row["quantity"] * $rate;

        $cuml_total += $pa_row["cumulative_qty"] * $rate;
//        if($acc_total > 0){
//            amount_decimal_cell($acc_total);   
//            $completed_total += $acc_total * $rate;
//        }
//        else{
//            label_cell('');
//        }
        
        $cert_cuml_total += $pa_row["certi_cuml_qty"] * $rate;
        
        amount_decimal_cell($pa_row["cumulative_qty"]); 
        amount_decimal_cell($pa_row["previous_qty"]); 
        amount_decimal_cell($pa_row["quantity"]); 
        amount_decimal_cell($pa_row["certi_cuml_qty"]); 
        
        end_row();
    }
    
    start_row();
    label_cell('Totals: ',"colspan=6 align=right");
    amount_cell($max_amount_total, true);
    amount_cell($work_done_total, true);
    
    amount_cell($cuml_total, true);
    amount_cell($previous_total, true);
    amount_cell($current_total, true);
    amount_cell($cert_cuml_total, true);
    
    end_row();
    
    hidden('PAId', $pa_id);
    end_table(1);
    div_end();

}

display_project_payment_application_items($pa_id, $boq_group_id);

end_page(true, false, false, ST_MATERIAL_REQ, $trans_no);
?>