<?php
/**********************************************************************
***********************************************************************/
$page_security = 'SA_SUPPTRANSVIEW';
$path_to_root = "../..";
include($path_to_root . "/purchasing/includes/po_class.inc");

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

$js = "";
if ($SysPrefs->use_popup_windows)
	$js .= get_js_open_window(900, 500);
page(_($help_context = "View Purchase Order Delivery"), true, false, "", $js);

include($path_to_root . "/purchasing/includes/ui/purchasing_ui.inc");

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


function display_grn_header(&$po)
{
	global $Refs;
    start_table(TABLESTYLE2, "width=90%");

    start_row();
        $supplier_name = get_supplier_formatted_name($po->supplier_id);
	label_cells(_("Supplier"), $supplier_name, "class='tableheader2'");

    if (!is_company_currency($po->curr_code))
    	label_cells(_("Order Currency"), $po->curr_code, "class='tableheader2'");

    label_cells(_("For Purchase Order"), get_trans_view_str(ST_PURCHORDER, $po->order_no),
    	"class='tableheader2'");

	label_cells(_("Ordered On"), $po->orig_order_date, "class='tableheader2'");
	end_row();

   	start_row();
   	label_cells(_("Reference"), $po->reference, "class='tableheader2'");
   	label_cells(_("Deliver Into Location"), get_location_name($po->Location), "class='tableheader2'");
	label_cells(_("Supplier's Reference"), $po->supp_ref, "class='tableheader2'");

    end_row();

   	label_row(_("Delivery Address"), $po->delivery_address, "class='tableheader2'", "colspan=9");

    if ($po->Comments != "")
    	label_row(_("Order Comments"), $po->Comments, "class='tableheader2'", "colspan=9");
    end_table(1);
}


$purchase_order = new purch_order;
read_grn($_GET["trans_no"], $purchase_order);

$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 Delivery") . " #" . $_GET['trans_no']);
echo "<BR>";
display_grn_header($purchase_order);

display_heading2(_("Line Details"));

start_table(TABLESTYLE, "colspan=9 width=90%");
$th = array(_("Id"), _("Item Code"), _("Item Description"), _("Delivery Date"), _("Quantity"),
	_("Unit"), _("Price"),  _("Discount"), _("Line Total"), _("Qty Invcd"));

table_header($th);

$total = 0;
$k = 0;  //row colour counter

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

//	$line_total = $stock_item->qty_received * $stock_item->price;
	$line_total = $stock_item->original_tran_qty * $stock_item->convertion_factor * $stock_item->price;

	alt_table_row_color($k);

	label_cell($stock_item->grn_item_id, "align=center");
	label_cell($stock_item->stock_id);
	label_cell($stock_item->item_description);
	label_cell($stock_item->req_del_date, "nowrap align=right");
	$dec = get_qty_dec($stock_item->stock_id);
//	qty_cell($stock_item->qty_received / $stock_item->convertion_factor, false, $dec);
	qty_cell($stock_item->original_tran_qty, false, $dec);
//	label_cell($stock_item->units);
    label_cell($stock_item->tran_unit);
    
	amount_decimal_cell($stock_item->price * $stock_item->convertion_factor);
//    amount_decimal_cell($stock_item->order_unit_price);
    amount_decimal_cell($stock_item->order_unit_price_discount);
    
	amount_cell($line_total);
	qty_cell($stock_item->qty_inv / $stock_item->convertion_factor, false, $dec);
	end_row();

	$total += $line_total;
}

$display_sub_tot = number_format2($total,user_price_dec());
label_row(_("Sub Total"), $display_sub_tot,
	"align=right colspan=8", "nowrap align=right", 1);

//$taxes = $purchase_order->get_taxes();
$taxes = $purchase_order->get_all_taxes();
$all_taxes_total = display_edit_tax_items($taxes, 8, $purchase_order->tax_included, 1);

$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=8 align='right'","align='right'");
label_cell('');
end_row();


end_table(1);

is_voided_display(ST_SUPPRECEIVE, $_GET['trans_no'], _("This delivery has been voided."));

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

?>
