<?php
/* $Revision: 1.25 $ */
$PageSecurity = 2;
/* Session started in header.inc for password checking and authorisation level check  OpenDeliveryDetails*/
include('includes/session.inc');
$OrderNumber=$_GET['OrderNumber'];
if (isset($_GET['OrderNumber'])) {
	$OrderNumber = strtoupper($_GET['OrderNumber']);
	$title = _('Reviewing Sales Order Number') . ' ' . $_GET['OrderNumber'];
} else if (isset($_POST['OrderNumber'])){
	$OrderNumber = strtoupper($_POST['OrderNumber']);
	$title = _('Reviewing Sales Order Number') . ' ' . $_POST['OrderNumber'];
}
/*if (isset($_GET['OrderNumber'])) {
	$title = _('Reviewing Sales Order Number') . ' ' . $_GET['OrderNumber'];
} 
*/
if (!isset($OrderNumber)) {
	include('includes/header.inc');
	echo '<br><br><br>';
	prnMsg(_('This page must be called with a sales order number to review') . '.<br>' . _('i.e.') . ' http://????/OrderDetails.php?OrderNumber=<i>xyz</i><br>' . _('Click on back') . '.','error');
	include('includes/footer.inc');
	exit;
}
if (isset($Errors)) {
	unset($Errors);
}

	//initialise no input errors assumed initially before we test
if (isset($_POST['submit'])) {
$OrderNumber=$_REQUEST['OrderNumber'];
	/* actions to take once the user has clicked the submit button
	ie the page has called itself with some user input */
	//first off validate inputs sensible
	
	$debtorSQL = 'SELECT
			*
		FROM
			open_salesorders
		WHERE
		 open_salesorders.orderno ="'.$OrderNumber.'"';

$ErrMsg =  _('The order cannot be retrieved because');
$DbgMsg = _('The SQL that failed to get the order header was');
$Getdebtor= DB_query($debtorSQL,$db, $ErrMsg, $DbgMsg);
$mydebtor=DB_fetch_array($Getdebtor);
		
			$stkcode=$_POST['stkcode'] ;
			$quantity= $_POST['quantity']; 
			$Discount =($_POST['Discount'])/100; 
			$usage=$_POST['usage'];
			$deliverytime=$_POST['deliverytime'];
			$location=$_POST['location'];
			$SQL_actualdispatchdate = FormatDateForSQL($_POST['QtyDelivery']);
			$sqlselect="select  units,description FROM stockmaster WHERE stockid='".$stkcode."'";	
			$resultselect = DB_query($sqlselect,$db);
			$myrow = DB_fetch_array($resultselect);
			
			
			$sqladd="INSERT  into deliveredschedule_item set 
			locusage='".$usage."',
			deliverytime='".$deliverytime."',
			location='".$location."',
			debtorno='".$mydebtor['debtorno']."',
			orderno='".$OrderNumber."',
			stkcode='',
			description='".$stkcode."', 
			units='".$myrow['units']."', 
			quantity='".$quantity."', 
			discountpercent='".$Discount."',
			actualdispatchdate='".$SQL_actualdispatchdate."',
			unitprice='NULL'";
			$ErrMsg = _('This Item could not be added because');
			$result = DB_query($sqladd,$db,$ErrMsg);

echo "<meta http-equiv='Refresh' content='0; url=" . $rootpath ."/OrderDetails.php?" . SID . "&OrderNumber=" . $_POST['OrderNumber'] . "'>";
}
include('includes/header.inc');

$OrderHeaderSQL = 'SELECT
			open_salesorders.debtorno,
			debtorsmaster.name,
			open_salesorders.branchcode,
			open_salesorders.customerref,
			open_salesorders.comments,
			open_salesorders.orddate,
			open_salesorders.ordertype,
			open_salesorders.confirmeddate,
			debtorsmaster.currcode,
			open_salesorders.fromstkloc
		FROM
			open_salesorders,
			debtorsmaster
		WHERE
			open_salesorders.debtorno = debtorsmaster.debtorno
		AND open_salesorders.orderno = ' . $_GET['OrderNumber'];

$ErrMsg =  _('The order cannot be retrieved because');
$DbgMsg = _('The SQL that failed to get the order header was');
$GetOrdHdrResult = DB_query($OrderHeaderSQL,$db, $ErrMsg, $DbgMsg);

if (DB_num_rows($GetOrdHdrResult)==1) {
	$myrow = DB_fetch_array($GetOrdHdrResult);
	$SQL="SELECT * from debtorsmaster where debtorno='".$myrow['customerref']."'";
	$RSQL=DB_query($SQL,$db);
	$myrowname = DB_fetch_array($RSQL);
	echo '<br><br><table bgcolor="#CCCCCC">';
	echo '<tr>
		<th>' . _('Customer Code') . ':</th>
		<td class="EvenTableRows"><font color=BLUE><b><a href="' . $rootpath . '/SelectCustomer.php?Select=' . $myrow['debtorno'] . '">' . $myrow['debtorno'] . '</a></b></td>
		<th>' . _('Customer Name') . ':</th><td bgcolor="#CCCCCC"><font color=BLUE><b>' . $myrow['name'] . '</b></td>
	</tr>';
	echo '<tr>
		<th>' . _('Customer Reference') . ':</th>
		<td class="EvenTableRows"><font color=BLUE><b>' . $myrowname['name'] . '</font></b></td>
		
		<th>' . _('Ordered On') . ':</th>
		<td class="EvenTableRows"><font color=BLUE><b>' . ConvertSQLDate($myrow['orddate']) . '</font></b></td>
	</tr>';
	
	echo '<tr>
		<th>' . _('Requested Delivery') . ':</th>
		<td class="EvenTableRows"><font color=BLUE><b>' . ConvertSQLDate($myrow['confirmeddate']) . '</font></b></td>
		<th>' . _('Order Currency') . ':</th>
		<td class="EvenTableRows"><font color=BLUE><b>' . $myrow['currcode'] . '</font></b></td>
	</tr>';
	
	/*echo '<tr>
		<th>' . _('Deliver From Location') . ':</th>
		<td class="EvenTableRows"><font color=BLUE><b>' . $myrow['fromstkloc'] . '</font></b></td>
		
	</tr>';
	*/
	
	
	echo '</table>';
	/*<div class="centre">';
	echo _('Comments'). ': ' . $myrow['comments'] . '<br></div>*/
}

/*Now get the line items */

	$LineItemsSQL = 'SELECT
				stkcode,
				stockmaster.description,
				stockmaster.volume,
				stockmaster.kgs,
				stockmaster.decimalplaces,
				stockmaster.mbflag,
				stockmaster.units,
				stockmaster.discountcategory,
				stockmaster.controlled,
				stockmaster.serialised,
				unitprice,
				quantity,
				discountpercent,
				actualdispatchdate,
				qtyinvoiced
			FROM salesorderdetails, stockmaster
			WHERE salesorderdetails.stkcode = stockmaster.stockid AND orderno =' . $_GET['OrderNumber'];

	$ErrMsg =  _('The line items of the order cannot be retrieved because');
	$DbgMsg =  _('The SQL used to retrieve the line items, that failed was');
	$LineItemsResult = db_query($LineItemsSQL,$db, $ErrMsg, $DbgMsg);
                                                                                                              																																
	if (db_num_rows($LineItemsResult)>0) 
	{
		
					$OrderTotal = 0;
					$OrderTotalVolume = 0;
					$OrderTotalWeight = 0;
			
					echo '<br><div class="centre"><b>' . _('Line Details') . '</div></b>
						<table cellpadding=2 colspan=9 border=1>
						<tr>
						<th>' . _('Item Code') . '</th>
						<th>' . _('Item Description') . '</th>
						<th>' . _('Unit') . '</th>
						<th>' . _('Price') . '</th>
						<th colspan=2>' . _('Total') . '</th>
						</tr>';
					$k=0;
		while ($myrow=db_fetch_array($LineItemsResult)) 
		{
			
						if ($k==1){
							echo '<tr class="EvenTableRows">';
							$k=0;
						} else {
							echo '<tr class="OddTableRows">';
							$k=1;
						}

						/*if ($myrow['qtyinvoiced']>0)
						{
							$DisplayActualDeliveryDate = ConvertSQLDate($myrow['confirmeddate']);
						} else {
							$DisplayActualDeliveryDate = _('N/A');
						}*/

								echo 	'<td>' . $myrow['stkcode'] . '</td>
									<td>' . $myrow['description'] . '</td>
									
									<td>' . $myrow['units'] . '</td>
									<td align=right>' . number_format($myrow['unitprice'],2) . '</td>
									<td align=right colspan=2>' . number_format($myrow['unitprice']) . '</td>
									
								</tr>';
							
							$OrderTotal = $OrderTotal + $myrow['unitprice'];
							$OrderTotalVolume = $OrderTotalVolume + $myrow['quantity'] * $myrow['volume'];
							$OrderTotalWeight = $OrderTotalWeight + $myrow['quantity'] * $myrow['kgs'];
			
		}
				$DisplayTotal = number_format($OrderTotal,2);
				$DisplayVolume = number_format($OrderTotalVolume,2);
				$DisplayWeight = number_format($OrderTotalWeight,2);
				
				echo '<tr>
					<td colspan=4 align=right><b>' . _('TOTAL Excl Tax/Freight') . '</b></td>
					<td  align=right>' . $DisplayTotal . '</td>
					<td  align=right><a href="#" onclick="show()";><b>' . _('Add Schedule') .'</b></a></td>
					</tr>
				</table>';
				
			/*	echo '<table border=1>
					<tr>
						<td>' . _('Total Weight') . ':</td>
						<td>' . $DisplayWeight . '</td>
						<td>' . _('Total Volume') . ':</td>
						<td>' . $DisplayVolume . '</td>
					</tr>
				</table>';*/
	}
	echo '<br>';
	?>
<script language="javascript">
function show()
{
document.getElementById("showtable").style.display='';
}

function validation()
{
var  numeric_value="1234567890\.-/"

if(document.getElementById("quantity").value=="")
{
alert("Error:Enter Quantity");
document.getElementById("quantity").focus();
return false;
}

var s=document.getElementById("quantity").value;
var i;
for(i=0;i < s.length;i++)
{
var currchar = s.charAt(i);
if(numeric_value.indexOf(currchar) == -1)
{
alert("Error:Quantity Must be Numeric");
document.getElementById("quantity").select();
return false;
}
}


if(document.getElementById("Discount").value=="")
{
//alert("Error:Enter Discount");
//document.getElementById("Discount").focus();
return true;
}

var s=document.getElementById("Discount").value;
var i;
for(i=0;i < s.length;i++)
{
var currchar = s.charAt(i);
if(numeric_value.indexOf(currchar) == -1)
{
alert("Error:Discount Must be Numeric");
document.getElementById("Discount").select();
return false;
}
}
}

		/*var xmlHttp
		function getDescription(str)
		{ 
		xmlHttp=GetXmlHttpObject();
		if (xmlHttp==null)
		{
		alert ("Your browser does not support AJAX!");
		return;
		} 
		var url="description.php";
		url=url+"?a="+str;
		url=url+"&orderno="+<? //=$OrderNumber?>;
		//alert(url);
		//url=url+"&sid="+Math.random();
		xmlHttp.onreadystatechange=DescriptionChanged;
		xmlHttp.open("GET",url,true);
		xmlHttp.send(null);
		}
		
		function DescriptionChanged() 
		{ 
		if (xmlHttp.readyState==4)
		{ 
		document.getElementById("Description").innerHTML="";
		document.getElementById("Description").innerHTML=xmlHttp.responseText;
		}
		}
				
			
		function GetXmlHttpObject()
		{
		var xmlHttp=null;
		try
		{
		// Firefox, Opera 8.0+, Safari
		xmlHttp=new XMLHttpRequest();
		}
		catch (e)
		{
		// Internet Explorer
		try
		{
		xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch (e)
		{
		xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
		}
		}
		return xmlHttp;
		}*/
		</script>
	<?PHP	
	/*NOW GET DELIVERED SCHEDULE ITEMS*/	
		
		
	$DELIVEREDItemsSQL = "SELECT * FROM deliveredschedule_item
			WHERE  orderno ='".$_GET['OrderNumber']."'";

	$ErrMsg =  _('The line items of the order cannot be retrieved because');
	$DbgMsg =  _('The SQL used to retrieve the line items, that failed was');
	$DELIVEREDItemsResult = db_query($DELIVEREDItemsSQL,$db, $ErrMsg, $DbgMsg);
                                                                                                              																																
	if (db_num_rows($DELIVEREDItemsResult)>0) 
	{
		$today=date('d/m/y');
		$OrderTotalQuantity=0;
					echo '<table width=80% border=1><tr><td><b>' . _('Schedule Details') . '</b></td></tr></table>
						<table cellpadding=2 colspan=9 border=1 style="margin-top:40px;">';
						echo'<tr>
						<th>' . _('Item Code') . '</th>
						<th>' . _('Item Description') . '</th>
						<th>' . _('Supply Date') . '</th>
						<th>' . _('Supply Time') . '</th>
						<th>' . _('Unloading Loc') . '</th>
						<th>' . _('Usages Loc') . '</th>
						<th>' . _('Quantity') . '</th>
						<th>' . _('Customer Code') . '</th>
						</tr>';
					$k=0;
		while ($myrow=db_fetch_array($DELIVEREDItemsResult)) 
		{
 
						if ($k==1){
							echo '<tr class="EvenTableRows">';
							$k=0;
						} else {
							echo '<tr class="OddTableRows">';
							$k=1;
						}

						if ($myrow['actualdispatchdate']!="")
						{
							$DisplayActualDeliveryDate = ConvertSQLDate($myrow['actualdispatchdate']);
						} else {
							$DisplayActualDeliveryDate = _('N/A');
						}

								echo 	'<td>' . $myrow['stkcode'] . '</td>
									<td>' . $myrow['description'] . '</td>
									<td>' . $DisplayActualDeliveryDate . '</td>
									<td align=right>' . $myrow['deliverytime'] .'</td>
									<td>' . $myrow['location'] . '</td>
									<td>' .  $myrow['locusage'] . '</td>
									<td>' .  $myrow['quantity'] . '</td>
									<td>' .  $myrow['debtorno'] . '</td>
									
								</tr>';
								$OrderTotalQuantity = $OrderTotalQuantity + $myrow['quantity'];
		}	
		echo '<tr>
					<td colspan=6 align=right><b>' . _('TOTAL Quantity') . '</b></td>
					<td align=right>'.$OrderTotalQuantity.'</td>
					</tr>';
		echo '</table>';
		}
		echo '<br>';
	?>	

	<?php			
				$DateString = Date($_SESSION['DefaultDateFormat']); 
				$Datetime=date('h:i:s');
				echo "<form method='post' onsubmit='return validation();' action='" . $_SERVER['PHP_SELF'] . '?' . "' >";
				echo "<input type='Hidden' name='OrderNumber' value='".$OrderNumber."'>";
				echo '<table id="showtable"  style="display:none" border=1  width=50%><tr><td>' . _('Item Code') . ':</td>
				<td><select  name="stkcode">';
				$SQL=("select * FROM salesorderdetails, stockmaster
			WHERE salesorderdetails.stkcode = stockmaster.stockid AND orderno='".$OrderNumber."'");
				$result = db_query($SQL,$db);
				while($myrow=DB_fetch_array($result)){
				echo "<option VALUE='"  . $myrow['description'] . "'>" . $myrow['description'];
				}
				echo '</select></td></tr>';
				echo '<tr><td>' . _('Quantity') . ':</td>
				<td><input type=text name=quantity  id=quantity></td></tr>
				<tr><td>'. _('Discount Percent') .':</td>
				<td><input type=text  name="Discount" id=Discount></td></tr>
				<tr><td>'. _('Unloading Loc') .':</td>
				<td><input type=text  name="location" id=location></td></tr>
				<tr><td>'. _('Usage Loc') .':</td>
				<td><input type=text  name="usage" id=usage></td></tr>
				<tr><td>' . _('Qty Delivery Date') . '(' . $_SESSION['DefaultDateFormat'] . '):</td>
				<td><input type=text class="date" alt="'.$_SESSION['DefaultDateFormat'].'" name="QtyDelivery" value="' . $DateString . '"></td></tr>
				<tr><td>' . _('Delivery Time') .':</td>
				<td><input type=text class="date" alt="'.date('h:i:s').'" name="deliverytime" value="' . $Datetime . '"></td></tr>';
				echo "<tr><td colspan='2'><div align='center'><input tabindex=20 type='Submit' name='submit' value='" . _('Add New Schedule') . "'>
				</div></td></tr></table></form>";
include('includes/footer.inc');
?>
