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

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

include_once($path_to_root . "/includes/date_functions.inc");
include_once($path_to_root . "/includes/inventory.inc");
include_once($path_to_root . "/includes/data_checks.inc");
include_once($path_to_root . "/includes/ui/ui_input.inc");
include_once($path_to_root . "/includes/ui/ui_lists_custom.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_inquiry_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(800, 500);
page(_($help_context = "View Work Order"), true, false, "", $js);

//-------------------------------------------------------------------------------------------------
$pa_no = 0;
if (isset($_GET['trans_no']) && $_GET['trans_no'] != "")
{
	$_POST['trans_no'] = $_GET['trans_no'];
}

if(isset($_POST['trans_no']))
{
	$pa_no = $_POST['trans_no'];
}

global $approval_statuses;

display_heading("Project Payment Application #$pa_no");


function display_ppa_header($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'];
    
}

function display_ppa_details($project_id, $pa_no, $boq_group_id)
{
    global $Ajax, $project_billing_types; 
      
    if(!($boq_group_id>0)){
        $boq_group_id = 0;
    }
    $result_a = view_project_payment_application_boq_details($project_id, $pa_no, $boq_group_id);
    
//  $quantity = $result_a['quantity'];
//  $date = $result_a['date'];    

    div_start('project_payment_application_details');
        
    start_table(TABLESTYLE_NOBORDER);
        
    $th = array(_("Code"), _("Description"), "Type",
                _("Unit"), "rate", _("Max Qty"), "Max Amount",_("Work Done"), _("Pre. Qty"), _("Curr. Qty"), 
                _("Accum. Qty"), "");               
   
    table_header($th);

    $j = 1;
    $k=0; //row colour counter

    $work_done_total = $previous_total = $current_total = $completed_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;
        
//        $total_work_done = get_total_work_done_upto_pa_date($pa_row['id'], $site_id, $ord_date);
        
//        if($total_work_done > 0){
//            label_cell($total_work_done, "align=right");  
//            $work_done_total += $total_work_done * $rate;
//        }
//        else{
//            label_cell('');
//        }
//
//        $payment_detail = get_project_payment_application_detail($pa_no, $pa_row["id"]);
//        if($payment_detail){           
//            $_POST[$pa_row["id"]."_previous_qty"] = $payment_detail["previous_qty"]; 
//            $_POST[$pa_row["id"]."_quantity"] = $payment_detail["quantity"];  
//           // $_POST[$pa_row["id"]."_total_work_done"] = $pa_row["total_work_done"];                
//        }
//        else{
//            if(!isset($_POST[$pa_row["id"]."_previous_qty"])){
//                $_POST[$pa_row["id"]."_previous_qty"] = 0.00; 
//            }
//            if(!isset($_POST[$pa_row["id"]."_quantity"])){
//                $_POST[$pa_row["id"]."_quantity"] = 0.00;     
//            }
//        }
        
        amount_decimal_cell($pa_row["total_work_done"]);     
        
        $previous_total += $pa_row["previous_qty"] * $rate;
        $current_total += $pa_row["quantity"] * $rate;
        
        amount_decimal_cell($pa_row["previous_qty"]);           
        amount_decimal_cell($pa_row["quantity"]); 
        
        $acc_total = $pa_row["previous_qty"] + $pa_row["quantity"];
        if($acc_total > 0){
            amount_decimal_cell($acc_total);   
            $completed_total += $acc_total * $rate;
        }
        else{
            label_cell('');
        }
        
//        hidden($pa_row["id"]."_"."boq_item_id", $pa_row["id"]);

//        check_cells(null, $pa_row["id"]."_check_item");       
        
        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($previous_total, true);
    amount_cell($current_total, true);
    amount_cell($completed_total, true);
    
    end_row();
    
    hidden('PAId', $pa_no);
    end_table(1);
    div_end();
}

start_form();

display_ppa_header($pa_no);   

    start_table(TABLESTYLE_NOBORDER);
    start_row();
        project_pa_boq_item_groups_list_cells("BOQ Item Group", "boq_item_group_id", get_post('project_id'), $pa_no, null, "Select a BOQ Item Group", true);
    end_row();
    end_table(1);
    
    hidden('trans_no', $pa_no);
//br();

display_ppa_details(get_post('project_id'),$pa_no, get_post('boq_item_group_id'));

end_form();

end_page(true, false, false, ST_WORKORDER, $pa_no);

?>
