<?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"), true, false, "", $js);

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

//function get_pr_allocation_ids_string($po_line_id){
//    $pr_ids_info = get_pr_allocation_ids_for_po_line($po_line_id);
//    $pr_ids_string = "";
//    
//    while($pr_ids=db_fetch($pr_ids_info)){
//        $pr_ids = array_unique($pr_ids);
//        foreach($pr_ids as $pr_id){
//            $pr_ids_string .= get_supplier_trans_view_str(ST_PR_DETAIL_ALLOCATIONS,$pr_id)." ";
//        }
//    }
//    $pr_ids_string = trim($pr_ids_string);
//    return $pr_ids_string;
//}

function display_po_all_transactions_payments_link($trans_no)
{

    start_table(TABLESTYLE_NOBORDER, "width=40%");
    start_row();
//$th = array(_("Approval Status"), _("Approver"), _("Approved On"), _("Display Status"),_("Comment"),_('Next Approval Group'));
//    $s = get_project_trans_view_str(ST_DOCUMENT_BUDGET_STATUS,$trans_no."&source_type_id=".$source_type_id, "View Document Budget Summary");
    $s = get_po_all_transactions_payments_view_str(ST_PURCHORDER, $trans_no);
//    $s .= "kjsbdvjbJSDNVOJnsdv";
    label_cell($s, "align = center");
    end_row();
    end_table(1);
    
}

$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(_("Purchase Order") . " #" . $_GET['trans_no']);
br();
display_po_summary($purchase_order, true);

display_heading2(_("Line Details"));
start_table(TABLESTYLE, "colspan=9 width=98%");

    $th = array(_("Id"),_("PRs"),_("Allo."), _("Item Code"), _("Item Description"), _("Quantity"), _("Unit"), _("Price"), "Discount", _("Del. Date"), _("Line Total"), 
                _("Qty Rcvd"), _("Qty Invcd"));

    table_header($th);
        $total = $k = $discounts_total = 0;
        $overdue_items = false;

        foreach($purchase_order->line_items as $stock_item){
//            $ids ='';
//            $ids_pr =  get_pr_allocation_ids_for_po_line($stock_item -> row_id);
//
//                while($ids_data= db_fetch($ids_pr)){
//                    $id =  $ids_data['allocation_id'];
//                    if($id > 0){
//                        $ids .= get_supplier_trans_view_str(ST_PR_PO_DETAILS,$id, $id).' ' ;
//                    }
//                }

                $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));

                // if overdue and outstanding quantities, then highlight as so
                if(($stock_item->quantity - $stock_item->qty_received > 0) && date1_greater_date2(Today(), $stock_item->req_del_date)){
                    start_row("class='overduebg'");
                    $overdue_items = true;
                }else{
                    alt_table_row_color($k);
                }

                label_cell($stock_item->row_id, "align=center");
                
                $prs = get_pr_line_ids_string($stock_item->row_id);
                label_cell($prs, "align=center");
                
                $pr_allocs = get_pr_allocation_ids_string($stock_item->row_id);
                label_cell($pr_allocs, "align=center");
                
//                label_cell($ids);
                label_cell($stock_item->stock_id);
                label_cell($stock_item->item_description);
                $dec = get_qty_dec($stock_item->stock_id);
                qty_cell($stock_item->tran_qty, false, $dec);
                $alternate_unit = get_unit_name($stock_item->tran_unit);
                label_cell($alternate_unit['name']);
                amount_decimal_cell($stock_item->order_unit_price);
                amount_decimal_cell($stock_item->order_unit_price_discount);
                label_cell($stock_item->req_del_date);
                amount_cell($line_total);
                qty_cell($stock_item->qty_received / $stock_item->convertion_factor, false, $dec);
                qty_cell($stock_item->qty_inv / $stock_item->convertion_factor, false, $dec);
                end_row();

                $total += $line_total;
                $discounts_total += $line_discounts_total;                
                
                if(strlen($stock_item->line_comment) > 0){
                    $k = $k == 0 ? 1 : 0;
                    alt_table_row_color($k);
                    label_cell("", 'colspan=4');
                    label_cell(nl2br($stock_item->line_comment), 'colspan=6');
                    label_cell("", 'colspan=3');
                    end_row();
                }
        }

        start_row();
        $display_discounts_total = number_format2($discounts_total,user_price_dec());
        label_cells(_("Discounts Total"), $display_discounts_total, "align=right colspan=8", "nowrap align=right",2);
        label_cell("","colspan=2");
        label_cell("","colspan=2");
        end_row();
        
        $display_sub_tot = number_format2($total,user_price_dec());
        label_row(_("Sub Total"), $display_sub_tot, "align=right colspan=10", "nowrap align=right",2);
        $taxes = $purchase_order->get_all_taxes();
        $all_taxes_total = display_edit_tax_items($taxes, 10, $purchase_order->tax_included,2);
        $staxes = $purchase_order->get_staxes_total();

        $tax_total = $all_taxes_total - $staxes;
        $display_total = price_format(($total + $tax_total));

        start_row();
            label_cells(_("Amount Total"), $display_total, "colspan=10 align='right'","align='right'");
            label_cell('', "colspan=2");
        end_row();

    end_table();

if($overdue_items)
    display_note(_("Marked items are overdue."), 0, 0, "class='overduefg'");

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

br(2);
$k = 0;

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

$grns_count = db_num_rows($grns_result);
$invoice_count = db_num_rows($invoice_result);

if($grns_count > 0 || $invoice_count > 0){
    start_outer_table(TABLESTYLE, "width=60%");
    
    if($grns_count > 0){

        display_heading2(_("Deliveries"));
        start_table(TABLESTYLE);
            $th = array(_("#"), _("Reference"), _("Delivered On"));
            table_header($th);
                while($myrow = db_fetch($grns_result)){
                    alt_table_row_color($k);
                        label_cell(get_trans_view_str(ST_SUPPRECEIVE,$myrow["id"]));
                        label_cell($myrow["reference"]);
                        label_cell(sql2date($myrow["delivery_date"]));
                    end_row();
                }
        end_table();
    }



    $k = 0;

    if ($invoice_count > 0){
        echo "</td><td valign=top>"; // outer table

        display_heading2(_("Invoices/Credits"));
        start_table(TABLESTYLE);
            $th = array(_("#"), _("Date"), _("Total"));
            table_header($th);
                while ($myrow = db_fetch($invoice_result)){
                    alt_table_row_color($k);
                        label_cell(get_trans_view_str($myrow["type"],$myrow["trans_no"]));
                        label_cell(sql2date($myrow["tran_date"]));
                        amount_cell($myrow["Total"]);
                    end_row();
                }
        end_table();
    }

    end_outer_table();
}

br(1);


display_allocations_to(PT_SUPPLIER, $purchase_order->supplier_id, ST_PURCHORDER, $purchase_order->po_no, 
		$total + $tax_total);

br(1);

if ($invoice_count > 0){
    display_po_all_transactions_payments_link($purchase_order->po_no);
}
//----------------------------------------------------------------------------------------------------

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

?>