<?php
/**********************************************************************
***********************************************************************/
$page_security = 'SA_SUPPTRANSVIEW';
$path_to_root = "../..";
include($path_to_root . "/purchasing/includes/po_class.inc");
include($path_to_root . "/includes/session.inc");
include($path_to_root . "/purchasing/includes/ui/purchasing_ui.inc");
include($path_to_root . "/purchasing/includes/db/purchase_requisitions_new_db.inc");
include_once($path_to_root . "/projects/includes/db/projects_db.inc");

$js = "";
if($use_popup_windows)
    $js .= get_js_open_window(900, 500);

page(_($help_context = "View Purchase Order All Payments"), true, false, "", $js);

if(!isset($_GET['trans_no'])){
    die ("<br>" . _("This page must be called with a purchase order number to review."));
}

global $systypes_array;

$purchase_order = new purch_order;
read_po($_GET['trans_no'], $purchase_order);

$purchase_order->po_no = $_GET['trans_no'];
$project_id = $purchase_order->project_id;
$user_id = $_SESSION['wa_current_user']->user;
$cap = can_access_project($project_id, $user_id);
if($cap['success'] !== true){
    display_error($cap["message"]);
    set_focus('project_id');
    display_footer_exit();
    exit;
}

display_heading(_("All Payments of Purchase Order") . " #" . $_GET['trans_no']); 

        $total = $k = $discounts_total = 0;
        $overdue_items = false;

        foreach($purchase_order->line_items as $stock_item){

                $line_total = $stock_item->tran_qty * ($stock_item->order_unit_price * (1 - $stock_item->order_unit_price_discount /100));
                $line_discounts_total = $stock_item->tran_qty * ($stock_item->order_unit_price * ($stock_item->order_unit_price_discount /100));

                $total += $line_total;
                $discounts_total += $line_discounts_total;        
        }

        $display_discounts_total = number_format2($discounts_total,user_price_dec());
        
        $display_sub_tot = number_format2($total,user_price_dec());
        $taxes = $purchase_order->get_all_taxes();
        $staxes = $purchase_order->get_staxes_total();

    end_table();



//----------------------------------------------------------------------------------------------------

br();
$k = 0;

$transactions = array();

$invoice_result = get_po_invoices_credits($_GET['trans_no']);

$invoice_count = db_num_rows($invoice_result);

while ($myrow = db_fetch($invoice_result)){ 
    if($myrow["type"] == ST_SUPPINVOICE){
        display_heading2($systypes_array[ST_SUPPINVOICE]." #".$myrow["trans_no"]);
        display_allocations_to(PT_SUPPLIER, $purchase_order->supplier_id, ST_SUPPINVOICE, $myrow["trans_no"], $myrow["Total"]);
    }
    elseif($myrow["type"] == ST_SUPPCREDIT){
        display_heading2($systypes_array[ST_SUPPCREDIT]." #".$myrow["trans_no"]);
        display_allocations_from(PT_SUPPLIER, $purchase_order->supplier_id, ST_SUPPCREDIT, $myrow["trans_no"], -$myrow["Total"]);
    }else{
        
    }
    
    $a = array("type"=>$myrow["type"], "trans_no"=>$myrow["trans_no"], "date"=>$myrow["tran_date"], "total"=>$myrow["Total"]);
    $transactions[] = $a;
}

    $k = 0;

    if ($invoice_count > 0){
        $trans_total = $trans_alloc = $trans_bal = 0;
        
        display_heading2(_("Payment Summary"));
        start_table(TABLESTYLE, "width=50%");
            $th = array(_("Type"), _("#"), _("Date"), _("Total"), _("Allocated"), _("Balance"));
            table_header($th);
            foreach ($transactions as $tran) {
                    alt_table_row_color($k);
                        label_cell($systypes_array[$tran["type"]]);
                        label_cell(get_trans_view_str($tran["type"],$tran["trans_no"]));
                        label_cell(sql2date($tran["date"]));
                        amount_cell($tran["total"]);
                        $trans_total += $tran["total"];
                        
                        
                        $supp_trans = get_supp_trans_record($tran["trans_no"], $tran["type"]);
                        $alloc = $supp_trans['alloc'];
                        if($tran["type"] == ST_SUPPCREDIT){
                            $alloc *= -1;
                        }
                        amount_cell($alloc);
                        $trans_alloc += $alloc;
                        $bal = $tran["total"] - $alloc;
                        amount_cell(($bal));
                        $trans_bal = $trans_total - $bal;
                    end_row();
                }
                start_row();
                    label_cell("Summary", "colspan=3 align=right");
                    amount_cell($trans_total);
                    amount_cell($trans_alloc);
                    amount_cell($trans_total - $trans_alloc);
                end_row();
                
                $po = get_po_edit_info($purchase_order->po_no);
                $po_alloc = $po['alloc'];
                start_row();
                    label_cell("Purchase Order Advance Payments", "colspan=3 align=right");
                    label_cell("");
                    label_cell(""); 
                    amount_cell(-$po_alloc);
                end_row();
                
                start_row();
                    label_cell("Balance", "colspan=3 align=right");
                    label_cell("");
                    label_cell(""); 
                    amount_cell($trans_total - ($trans_alloc + $po_alloc));
                end_row();
                
        end_table();
    }


br(2);

display_allocations_to(PT_SUPPLIER, $purchase_order->supplier_id, ST_PURCHORDER, $purchase_order->po_no, 
		$po['alloc'] + $po['taxes']);


//----------------------------------------------------------------------------------------------------

end_page(true, false, false, ST_PURCHORDER, $_GET['trans_no']);

