<?php
//include("../include/session.php");
//include ('../include/db.php');
include ("../include/formvalidations.php");

$delColText="Not defined";
    
    if ($_SESSION['orderNo']=="") {
    $_SESSION['orderNo']= $_GET['orderNo'];
	}
	$sel_query = "SELECT productId FROM orders WHERE orderNo = ".  $_SESSION['orderNo'] ;
	$orderNo = $_SESSION['orderNo'];
	$sel_result = execsql($sel_query);
	$sel_num = mysql_num_rows($sel_result);
	//$checkId = mysql_result($sel_result, 0, "productId");
	
	
	if ($sel_num=="0") {
	  $d3a = "<font size=xsmall color=red>Order no ".$orderNo." not found, search again</font>";
	  $orderNo=0;
	  }
	 else {
		$d3a="";
	 }
	 
	 $num_d=0;
	$delAddressNum=0;
	$delAddressNum=0;
	$commentNum=0;
	$delNum=0;
	
	$displayQuery = "SELECT * FROM orders WHERE (orderNo ='$orderNo') ";		
	$displayQueryResult = mySQL_query($displayQuery) or die ("Error in query:   $Query . * ". mySQL_error());
	$i_d = 0; 
	$num_d = mysql_num_rows($displayQueryResult);
	
	$delAddressQuery = "SELECT * FROM delivery_address WHERE (orderNo ='$orderNo')";		
	$delAddressResult = mySQL_query($delAddressQuery) or die ("Error in query:   $Query . * ". mySQL_error());
	$delAddressNum = mysql_num_rows($delAddressResult);
	
	
	$commentQuery = "SELECT * FROM order_comments WHERE (orderNo ='$orderNo')";		
	$commentResult = mySQL_query($commentQuery) or die ("Error in query:   $Query . * ". mySQL_error());
	$commentNum = mysql_num_rows($commentResult);
	
	$delNoteQuery = "SELECT DISTINCT delNote FROM sales WHERE orderNo = '$orderNo'";
	$delNoteResult = execsql($delNoteQuery);
	$delNum = mysql_num_rows($delNoteResult);
	
	$palletsPriceQuery = "SELECT price  FROM products WHERE productId  = '100292'";
	$palletsPriceResult = execsql($palletsPriceQuery);
	$_SESSION['PalletsPriceText'] = mysql_result($palletsPriceResult, 0, "price");
	
	if ($num_d!=0) {
		$d0 = mysql_result($displayQueryResult, 0, "custNo");
		$d2 = mysql_result($displayQueryResult, 0, "date");
		$d5 = mysql_result($displayQueryResult, 0, "client"); 
		$d3 = mysql_result($displayQueryResult, 0, "orderNo");
		$d10 = mysql_result($displayQueryResult, 0, "delDate");
		$d16 = mysql_result($displayQueryResult, 0, "delCol");
	}
	else {
		$d0 = "";
		$d2 = "";
		$d5 = "";
		$d3 = "";
		$d10 = "";
		$d16 = "";
	}
	
	
	if ($commentNum!=0) {
	   $d15 = mysql_result($commentResult, 0, "comment");
	}
	else {
		$d15="";
	}
	
	if ($delAddressNum!=0) {
	  	$d11 = mysql_result($delAddressResult, 0, "deliverTo");
		$d12 = mysql_result($delAddressResult, 0, "address");
		$d13 = mysql_result($delAddressResult, 0, "place");
		$d14 = mysql_result($delAddressResult, 0, "contact");
		if ($d16=="D") { $delColText = "Deliver to:"; }
	    if ($d16=="C") { $delColText = "Collect by:"; }
	}
	else {
		$d11="";
		$d12="";
		$d13="";
		$d14="";
		$delColText="Not defined";
	}
	
	
	
	
	
	$clientQuery = "SELECT  CompanyName FROM customer WHERE (custNo ='$d0') ";		
	$clientQueryResult = mySQL_query($clientQuery) or die ("Error in query:   $clientQuery . * ". mySQL_error());
	$clientNum = mysql_num_rows($clientQueryResult);
	if ($clientNum!=0) {
	   $d1 = mysql_result($clientQueryResult, 0, "CompanyName");
	}
	else {
		$d1="";
	}
	
	$productId="";
	$quantity="";
	$statusdisplay="";
	
	if ($orderNo!=0) {
	$statusQuery = "SELECT DISTINCT orderNo, SUM(quantity) AS SQuantity, SUM(reserved) AS SReserved, canceled FROM orders WHERE orderNo = $d3 GROUP BY orderNo";
	$statusResult = mySQL_query($statusQuery) or die ("Error in query:   $statusQuery . * ". mySQL_error());
		  
	$s1 = mysql_result($statusResult, 0, "SQuantity");
	$s2 = mysql_result($statusResult, 0, "SReserved");	
	$s3 = mysql_result($statusResult, 0, "canceled");	
	$b4 = $s1-$s2;
	
	if ($s3==1) { $statusdisplay="<b><font color=red>This order is canceled</font></b>";}
	else {	
	if ($s3==2) { $statusdisplay="<b><font color=orange>This order is suspended</font></b>";}
	else {
	if ($b4 == $s1) { $statusdisplay="<b><font color=red>This order is delivered in full</font></b>";}
	else {
	if ($s1-$s2 == 0) { $statusdisplay="<font color=green>This order is not delivered yet</font></b>";} 
	else {
	if ($s1-$s2 != 0) { $statusdisplay="<font color=orange>This order is partly delivered</font></b>";} 
	}
	}
	}
	}
	}
	
	
	$_SESSION['OrderNo'] = $orderNo;
	$_SESSION['OrderNoText'] = $orderNo;
	$_SESSION['CustomerNoText'] = $d0;
	$_SESSION['CustomerNameText'] = $d1;
	//$_SESSION['DeliveryDateText'] = $d2;
	$_SESSION['DeliverText1'] = $d11;
	$_SESSION['DeliverText2'] = $d12;
	$_SESSION['DeliverText3'] = $d13;
	$_SESSION['contactText'] = $d14;
?>


<link rel="stylesheet" type="text/css" href="../css/jquery.autocomplete.css" />
<script type="text/javascript" src="../css/jquery.1.4.2.js"></script>
<script type="text/javascript" src="../css/jquery.autocomplete.js"></script>
<link rel="stylesheet" type="text/css" href="../css/wizard.css" />

<script>
function confirmRefresh() {
var okToRefresh = confirm("Do you really want to cancel this entry?");
var url='showAllProducts.php?cat=Tiles and Accessories&productSelect=All&colorSelect=All'
if (okToRefresh)
	{
			window.location=url;
	}
}
</script>
<script language="JavaScript">
  var needToConfirm = true;
  window.onbeforeunload = confirmExit;
  function confirmExit()
  {
    if (needToConfirm)
      return "You did not finish the wizard.\nIf you leave now your data will not be saved.";
  }
function MM_callJS(jsStr) { //v2.0
  return eval(jsStr)
}
</script>
<form id="form1" name="form1" method="get" action="outWizbase.php">
  <table width=595 border="0" align="center" cellpadding="10" cellspacing="0" bordercolor="#0066FF" border-collapse="collapse">
    <tr>
      <td valign="top" >
      <?php if ($_SESSION['error']==1) { ?>
<p class="wizard2"> <img src="../img/icon-error.png" width="30" height="30" />&nbsp;&nbsp;&nbsp;&nbsp;
          <label class="std">Delivery note number "<?php echo $_SESSION['delNoteText'] ?>" allready exists, please use another number.</label>
        </p>
        <?php 
	 	 
	 } ?>
        <p class="wizard">
          <label class="std">enter&nbsp;delivery note no.:&nbsp;</label>
          <input name="delNoteText" type="text" id="delNoteText" class="wizard2"  value="<?php echo $_SESSION['delNoteText'] ?>" onkeypress="return numbersonly(this, event)" />
          <label class="star">*</label>
          <label class="std">select  deliver part or complete: &nbsp;
            <select name="complete_select" class="wizard1" id="complete_select"  >
              <option value="complete">all outstanding</option>
              <option value="partly">partly</option>
              <option value="<?php echo $_SESSION['complete'] ?>" selected="selected"><?php echo $_SESSION['complete'] ?></option>
            </select>
          </label><label class="star">*</label>
          <input name="error" type="hidden" id="error" value="0" />
          <input name="s" type="hidden" id="s" value="1" />
          <input name="p" type="hidden" id="p" value="1" />
        </p>
      </td>
    </tr>
    <tr>
      <td width="525" valign="top" ><table width="588" id="list2">
        <tr>
          <td width="128"> Order No.:</td>
          <td width="179"><?php echo $d3 ?>            <input type="hidden" name="delColText" id="delColText" value="<?php echo $d16 ?>" /></td>
          <td colspan="2"><?php echo $delColText ?></td>
        </tr>
        <tr>
          <td>Date:</td>
          <td><?php if ($d2<"2000-01-01") {echo "t.b.a."; } else {echo date("d-m-Y ", strtotime($d2)); } ?></td>
          <td width="48">&nbsp;</td>
          <td width="213"><?php echo $d11 ?></b></b><strong>
            <input type="hidden" name="DeliverText1" id="DeliverText1" value="<?php echo $d11 ?>" />
          </strong></td>
        </tr>
        <tr>
          <td>Company Name:</td>
          <td><?php echo $d1?><strong>
            <input type="hidden" name="CustomerNoText" id="CustomerNoText" value="<?php echo $d0 ?>" />
            <input type="hidden" name="CustomerNameText" id="CustomerNameText" value="<?php echo $d1 ?>" />
          </strong></td>
          <td>&nbsp;</td>
          <td><b><?php echo $d12 ?><strong>
            <input type="hidden" name="DeliverText2" id="DeliverText2" value="<?php echo $d12 ?>" />
          </strong></b></td>
        </tr>
        <tr>
          <td>Delivery date:</td>
          <td><strong>
            <?php if ($d10<"2000-01-01") {echo "t.b.a."; } else {echo date("d-m-Y ", strtotime($d10)); } ?>
            <input type="hidden" name="DeliveryDate" id="DeliveryDate" value="<?php echo $d10 ?>" />
          </strong></td>
          <td>&nbsp;</td>
          <td><b><?php echo $d13 ?><strong>
            <input type="hidden" name="DeliverText3" id="DeliverText3" value="<?php echo $d13 ?>" />
          </strong></b></td>
        </tr>
        <tr>
          <td>&nbsp;</td>
          <td>&nbsp;</td>
          <td>&nbsp;</td>
          <td><b><?php echo $d14 ?><strong>
            <input type="hidden" name="contactText" id="contactText" value="<?php echo $d14 ?>" />
          </strong></b></td>
        </tr>
        <tr>
          <td><strong>Notes:</strong></td>
          <td colspan="3"><?php echo $d15 ?></td>
        </tr>
      </table>
      </td>
</tr>
    <tr>
      <td valign="top" ><table  id="tableList" >
        <thead>
          <tr>
            <th width="56">Qty</th>
            <th width="261">Item</th>
            <th width="82">Price</th>
            <th width="100">Delivered</th>
            <th width="72">Balance</th>
          </tr>
        </thead>
        <tbody>
          <?php 
	if (($orderNo==0))
	  {  echo "<font color=red><b>This order is empty.</b></font>";
	  }
	  else
	  {
	  while ($i_d < $num_d) {
	    $d4 = mysql_result($displayQueryResult, $i_d, "productId");
		$productNameQuery = "SELECT productId, productName, price FROM products WHERE productId = $d4 ";
		$nameResult = execsql($productNameQuery);
		$d5 = mysql_result($nameResult, 0, "productName");
		$d6 = mysql_result($displayQueryResult, $i_d, "quantity");
		$d7 = mysql_result($displayQueryResult, $i_d, "saleId");
		$d8 = mysql_result($nameResult, 0, "price");
		$d9 = mysql_result($displayQueryResult, $i_d, "reserved");
		$d11 = mysql_result($displayQueryResult, $i_d, "canceled");
	   
	    ?>
          <tr class="">
            <td><?php echo $d6?></td>
            <td><?php echo $d5?></td>
            <td><div align="right">
              <?php echo $d8?>
              </div></td>
            <td align="right"><?php if ($d11==1) {
		      echo "<font color=red> canceled </font>";
		      } 
			  else {
		      if ($d11==2) {echo 
			  $d6-$d9."<br><font color=orange>balance suspended </font>";
			  } 
			  }
			  if ($d11==0) {
			  if ($d9==0) {
			     echo $d6-$d9;
			  }
			  else {
			    echo "<font color=orange>". ($d6-$d9) ."</font> <br />";
              }
			  }
			  
			   ?></td>
            <td align="right"><font color="black"><?php echo $d9 ?></font></td>
          </tr>
          <?php 
	$i_d++;
	}
	}
	?>
        </tbody>
      </table></td>
    </tr>
    <tr>
      <td><table width="590" height="50" align="center" id="tableButtonBar">
          <tr>
            <td width="117">&nbsp;</td>
            <td width="115">&nbsp;</td>
            <td width="99">&nbsp;</td>
            <td width="125" align="left"><input name="cancelbutton" type="button" class="imagebutton" id="cancelbutton" onclick="needToConfirm = false; confirmRefresh();" value="Cancel" /></td>
            <td width="110" align="left"><input type="submit" name="next_button" class="imagebutton" value="Next" onclick="needToConfirm = false; " /></td>
          </tr>
        </table></td>
    </tr>
  </table>
</form>
<!--form validation-->
<script language="JavaScript" type="text/javascript" xml:space="preserve">
  var frmvalidator  = new Validator("form1");
  frmvalidator.EnableMsgsTogether();
  frmvalidator.addValidation("delNoteText","req","Please enter the delivery note number.");
  frmvalidator.addValidation("complete_select","req","Please select part or full delivery.");
  </script>
