<?php

$trans_id = $_GET['AddedDI'];
$myrow = get_customer_trans($trans_id, ST_SALESINVOICE);
$sales_order = get_sales_order_header($myrow["order_"], ST_SALESORDER);
$result = get_customer_trans_details(ST_SALESINVOICE, $trans_id);
$cash_tendered_row = get_pos_trans($trans_id);
$cash_tendered = $cash_tendered_row['cash_amount'];
$bill_disc = $cash_tendered_row['discount'];
$table_id = $cash_tendered_row['table_id'];

$comp_details = get_company_prefs();
$comp_name = $comp_details["coy_name"];
$comp_adrs = $comp_details["postal_address"];
$comp_phone = $comp_details["phone"];
$comp_logo = $comp_details['coy_logo'];

echo "<div id='receipt_container'>";

$row = db_fetch(get_allocatable_from_cust_transactions(null, $invoice, ST_SALESINVOICE));
if ($row !== false)
	display_note(pos_print_link($trans_id, "<img src='".$path_to_root."/modules/pos/css/printer_gray.svg' width='25'/>"._('Print &Receipt & Go To Next Order'), true, ST_POSRECEIPT, false, 'printlink', 'prtopt', 0));

echo "<div id='pos_receipt'>";
echo "<center><img src='".company_path().'/images/'.$comp_logo."' height='50'></center>";
start_table(TABLESTYLE_NOBORDER);
start_row();
echo "<td>".$comp_name."</td>";
end_row();
start_row();
echo "<td>Address: ".$comp_adrs."</td>";
end_row();
start_row();
echo "<td>Phone: ".$comp_phone."</td>";
end_row();
start_row();
echo "<td>Date:</td><td>".sql2date($myrow["tran_date"])."</td>";
end_row();
start_row();
echo "<td>Reference: </td><td>".$myrow["reference"]."</td>";
end_row();

if($comp_details['pos_restaurant_mode'] == '1') {
	$tables_no = floor($comp_details['pos_table_number']);
	start_row();
	echo "<td><b>Table: </b></td><td><b>".$table_id."</b></td>";
	end_row();
}

start_row();
echo "<td colspan='6' id='receipt_header'>receipt</td>";
end_row();

end_table();

start_table(TABLESTYLE_NOBORDER);
start_row();
echo "<td>"._('Customer:')."</td>";
echo "<td>".$myrow["DebtorName"]."</td>";
end_row();

start_row();
$sales_txt = _('Sales Person:');
if($comp_details['pos_restaurant_mode'] == '1')
	$sales_txt = _('Waiter:');
echo "<td>".$sales_txt."</td>";
echo "<td>".$_SESSION["wa_current_user"]->name."</td>";
end_row();
end_table();

start_table(TABLESTYLE2);
start_row();
echo "<td></td><th>"._('Quantity')."</th>";
echo "<th align='right'>"._('Price')."</th>";
echo "<th align='right'>"._('Discount')."</th>";
echo "<th align='right'>"._('Total')."</th>";
end_row();

$sub_total = 0;
while ($myrow2 = db_fetch($result)) {
	if($myrow2["quantity"]==0 || $myrow2["unit_price"] == 0) continue;
	
	$value = round2(((1 - $myrow2["discount_percent"]) * $myrow2["unit_price"] * $myrow2["quantity"]), user_price_dec());
	$sub_total += $value;

	if ($myrow2["discount_percent"] == 0)
		$display_discount = "";
	else
		$display_discount = percent_format($myrow2["discount_percent"]*100) . "%";
	start_row();
	label_cell("<b>".$myrow2["StockDescription"]."</b>", "colspan='5'");
	end_row();

	start_row();
	qty_cell($myrow2["quantity"], false, get_qty_dec($myrow2["stock_id"]));
	label_cell($myrow2["units"], "align=left");
	amount_cell($myrow2["unit_price"]);
	label_cell($display_discount, "nowrap align=right");
	amount_cell($value);
	end_row();
}

$display_total = $myrow["ov_freight"]+$myrow["ov_gst"]+$myrow["ov_amount"]+$myrow["ov_freight_tax"];

if($cash_tendered === null)
	$cash_tendered = $display_total;
$refund = $cash_tendered - ($display_total - $bill_disc);

label_row(_('Sub-total')."&nbsp;&nbsp;", price_format($sub_total), 'colspan=4 align=right', "nowrap align=right width='15%'");
label_row(_('After Taxes')."&nbsp;&nbsp;", price_format($display_total), 'colspan=4 align=right', "nowrap align=right width='15%'");
label_row(_('Bill Discount')."&nbsp;&nbsp;", price_format($bill_disc), "colspan=4 align=right", "nowrap align=right");
label_row("<b>"._("TOTAL RECEIPT")."&nbsp;&nbsp;"."</b>", "<b>".price_format($display_total - $bill_disc)."</b>", "colspan=4 align=right", "nowrap align=right");
label_row(_('Cash Tendered')."&nbsp;&nbsp;", price_format($cash_tendered), "colspan=4 align=right", "nowrap align=right");
label_row(_('Refund')."&nbsp;&nbsp;", price_format($refund), "colspan=4 align=right", "nowrap align=right");

end_table();

echo "</div>";
echo "</div>";