<?php
// Checked for PHP5.6 / 8.x compatibility
ini_set('memory_limit', '2048M');
include("../../operainfo/getq3.php");
// Initialize arrays to prevent warnings
$currency = array();
$pdfcurrency = array();
$stockdesc = array();
$total = 0;
$hostcompanyid=0;
$x=getsecurity();
$orderid=trim(safetext2(isset($_GET['orderid']) ? $_GET['orderid'] : ''));
$sql = "SELECT * FROM viamedquote_orders where id = ?";
$stmt = pdoquery($sql, array($orderid));
$rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
foreach ($rows as $row) {
	$companyid=$row['company_id'];
	$contactid=$row['contact_id'];
	$hostcompanyid=$row['hostcompany'];
	$createdby=$row['createdby'];
	$createdon=$row['createdby'];
	$modifiedon=$row['modifiedon'];
	$modifiedby=$row['modifiedby'];
	$deliverycrmid=$row['delivery_company_id'];
	if ($deliverycrmid==0) {
		$deliverycrmid=$companyid;
	}
	$invoicecrmid=$row['invoice_company_id'];
	if ($invoicecrmid==0) {
		$invoicecrmid=$companyid;
	}
	$quoteproforma=$row['quote_proforma'];
	$wasiquoteorproforma=$row['wasaquoteproforma'];
	$quoteproformatoorderon=$row['wasaquoteproformaon'];
	$quoteproformatoorderby=$row['wasaquoteproformaon'];
	$prepaid=$row['prepaid'];
	$customerpodocid=$row['po_docid'];
	$paperworkmemo=$row['paperworkmemo'];
	$enduserid=$row['enduser_contact_id'];
	$customer_ref=$row['customer_ref'];
	$hostcompanyid=$row['hostcompany'];
	$currentstage=$row['order_stage'];
	$companyid=$row['company_id'];
	$due=$row['due'];
}
//$prefix=$global_company_prefix[$hostcompanyid];

print "<hr><h1>Order Details $hostcompanyid</h1>";
$sql = "SELECT * FROM pw_companys where vui = ? and company_id = ?";
$stmt = pdoquery($sql, array($vui, $hostcompanyid));
$access = $stmt->rowCount();
if ($access>0) {


$prefix=$global_company_prefix[$hostcompanyid];
$sql = "SELECT * FROM global_companys_bankaccounts where companyid = ?";
$stmt = pdoquery($sql, array($hostcompanyid));
$rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
foreach ($rows as $row) {
    $pricelist=$row['pricelist'];
    $currency[$pricelist]=$row['currency'];
    $pdfcurrency[$pricelist]=$row['bankcurrsymbol'];
	}

if ($companyid<>'') {
	$sql = "SELECT * FROM viamedquoteaddress where ID = ?";
	$stmt = pdoquery($sql, array($companyid));
	$rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
	foreach ($rows as $row) {
		$Opera_Pricelist=$row['Opera_Pricelist'];
		}
}
if ($Opera_Pricelist<>'') {
	if ($hostcompanyid==4)
	{
		$usecur="GBP";
	} else {

	}
	$sql = "SELECT * FROM specialprices where Pricelist = ?";
	$stmt = pdoquery($sql, array($Opera_Pricelist));
	$rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
	foreach ($rows as $row) {
		$usecur=$row['Currency'];
		}



}
function freestock($stkref)
	{
	$sql = "SELECT Serialnumbers.Stock_ref FROM Serialnumbers,serialnumberlocation where Serialnumbers.Stock_ref = ? and Serialnumbers.Invoicenumber='' and Serialnumbers.Ordernumber='' and Serialnumbers.stocklocation2<>'sent' AND
	Serialnumbers.stocklocation2=serialnumberlocation.locationid AND
	serialnumberlocation.sellable='1'";
	$stmt = pdoquery($sql, array($stkref));
	$num = $stmt->rowCount();

	if (substr($stkref,0,5)=='PPUPS')
		{
		$num='';
		}

	return ($num);
	}
	
	

print "<table width=75% class=CSSTable_n2><tr><Td>Stock Reference</td><td>Description</td><td>Qty On the Order</td><td>Unit Price $usecur</td><td>Total</td><td>Qty Shipped</td><td>Outstanding</td><td>VAT E</td><td>Due</td><td>Customer Informed</td><td></td></tr>";


$sql = "SELECT * FROM stocklist_tarriffs where hostcompany_id = ? ";
$stmt = pdoquery($sql,array($hostcompanyid));
$rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
foreach ($rows as $row) {
	$stkref=$row['stock_ref'];
	$stockdesc[$stkref]=$row['paperworkdescription'];
	if ($stockdesc[$stkref]=='') {
		$stockdesc[$stkref]=$row['pricedesc'];
	}
	if ($stockdesc[$stkref]=='') {
		$stockdesc[$stkref]=$row['label_description'];
	}



	}


$sql = "SELECT
viamedquote_orders_details.stockref,
viamedquote_orders_details.id,
viamedquote_orders_details.qty,
viamedquote_orders_details.qty_shipped,
viamedquote_orders_details.unitprice,
viamedquote_orders_details.vat_exempt,
viamedquote_orders_details.desctriptiveitem,
viamedquote_orders_details.memo,
viamedquote_orders_details.duedate,
viamedquote_orders_details.pricelist,
viamedquote_orders_details.current_del_date
FROM
  viamedquote_orders_details
  where
  viamedquote_orders_details.orderid = ?

  order by viamedquote_orders_details.paperworkorder,viamedquote_orders_details.id";
$stmt = pdoquery($sql, array($orderid));
$rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
foreach ($rows as $row) {
	   $pricelist=$row['pricelist'];
	   $mem = $row['memo'];
	   $mem=str_replace("<br>","
",$mem);
	$stkref=$row['stockref'];
	if ($row['desctriptiveitem']=='')
		{
		$usedesc=$stockdesc["$stkref"];
		} else
		{
		$usedesc=$row['desctriptiveitem'];
		}

	print "<tr>";

	print "<td><input type=hidden id='stk{$row['id']}' value='{$row['stockref']}'>{$row['stockref']}  </td><td>$usedesc";
	print "<br>Memo:$mem";
	print "</td><td>";




	if ($currentstage==0)
		{
	print "<input type=number id='{$row['id']}' value='{$row['qty']}' onchange=\"changeqty('{$row['id']}')\">";
		} else
		{
		print "{$row['qty']}";
		}
	print "</td>";
		print "<td>$currency[$pricelist] $pdfcurrency[$pricelist]";
	print "{$row['unitprice']}";
	print "</td>";
	print "<td>$currency[$pricelist] $pdfcurrency[$pricelist]";
	print $row['qty']*$row['unitprice'];
	$total=$total + $row['qty']*$row['unitprice'];
	print "</td>";
	print "<td>{$row['qty_shipped']}</td><td>";
	$left=$row['qty']-$row['qty_shipped']+0;
	print "$left</td>";
	if ($row['current_del_date'] > 0 ) {
	$prndeldate=date("d M Y",$row['current_del_date']);
	} else {
		$prndeldate="";
		}

	print "<td>";
	if ($row['vat_exempt']==0)
		{
		print "";
		}else
		{
		print "<img src=\"/intranet/images/tick.png\" width=12 height=12>";
		}
	print "</td>";

	print "<td>";
	if ($row['duedate'] >0) {
	$duedate=date("d M Y",$row['duedate']);
	} else {
	$duedate=$due;
	}
	print "$duedate</td>";
	print "<td> $prndeldate</td>";
	print "</tr>";

	}
print "<tr><td colspan=3></td><td>Total</td><td>$currency[$pricelist] $pdfcurrency[$pricelist] $total</td><td></td>";
print "</table>";


}
?>