<?php
include("../include/session.php");
if( !$session->isAdmin() && !$session->isRadstoneReceptionUser() && !$session->isRadstoneStoreUser() ){
	//header("Location: ../Store/storeMenu.php");?>
<script language="javascript" type="text/javascript">
        <!--
		  window.location="denied.php";
        -->
       </script>
<?php 
}else{
	
	include("../storeHeader.php");
		
	$orderNo = mysql_real_escape_string($_GET['OrderNoText']);
	
	$edit = $_GET['edit'];
	
	if ($edit=="1") {
		$field = $_GET['field'];
		$editText =  mysql_real_escape_string($_GET['date1']);
		$editQuery = "UPDATE nam_orders SET $field = '".  $editText ."' WHERE orderNo = '$orderNo' ";
		$editResult = mySQL_query($editQuery);
	}
	
	if ($edit=="2") {
		$saleId = $_GET['saleId'];
		$quantity = $_GET['QuantityText'];
		$productId = $_GET['ProductSelect'];
		$priceQuery = "SELECT price FROM nam_products WHERE productId='$productId'";
		$priceResult= mySQL_query($priceQuery);
		$price = mysql_result($priceResult, 0, "price");
		
		if ($quantity==0) {
		   $removeQuery = "DELETE FROM nam_orders WHERE saleId = '$saleId' ";
		   $removeResult = mySQL_query($removeQuery);
		}
		else {
		   $changeQuery  = "UPDATE nam_orders SET quantity = '". $quantity . "', productId = '".$productId."', price = '".$price."', reserved = '".$quantity."' WHERE saleId = '$saleId' ";
		   $changeResult = mySQL_query($changeQuery);
		}
		
	}
	
	
	$sel_query = "SELECT productId FROM nam_orders WHERE orderNo =  $orderNo  ";
	$sel_result = execsql($sel_query);
	$sel_num = mysql_num_rows($sel_result);
	//$checkId = mysql_result($sel_result, 0, "productId");
	
	
		
	
	$num_d=0;
	$delAddressNum=0;
	$delAddressNum=0;
	$commentNum=0;
	$delNum=0;
	
	
	
	$displayQuery = "SELECT * FROM nam_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 nam_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 nam_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 nam_sales WHERE orderNo = '$orderNo'";
	$delNoteResult = execsql($delNoteQuery);
	$delNum = mysql_num_rows($delNoteResult);
	
	
	
	
	
		$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");
	
	
	
	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 nam_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 nam_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>";} 
	}
	}
	}
	}
	}
	

	
?>
<script type="text/JavaScript">
<!--
function MM_callJS(jsStr) { //v2.0
  return eval(jsStr)
}
//-->
</script>

<script language="JavaScript" src="gen_validatorv4.js"
    type="text/javascript" xml:space="preserve"></script>

<link rel="stylesheet" href="../css/dhtmlwindow.css" type="text/css" />
<script type="text/javascript" src="../css/dhtmlwindow.js"></script>
<link rel="stylesheet" href="../css/modal.css" type="text/css" />
<script type="text/javascript" src="../css/modal.js"></script>
<script language="javascript" src="../include/calendar.js"></script>

<script type="text/javascript">
<!--
function MM_goToURL() { //v3.0
  var i, args=MM_goToURL.arguments; document.MM_returnValue = false;
  for (i=0; i<(args.length-1); i+=2) eval(args[i]+".location='"+args[i+1]+"'");
}
//-->
</script>
</head>


<table id="tableMain" width="595">
  <!--begin of tableMain-->
  <tr>
    <td ><div class="headerbox2">Edit Order Delivery Date</div>
      <br />
     
      <p class="edit">
        
        
     <label class="std"><span class="main"><font size="3"><b>Order <?php echo $d3 ?></b></font></span>&nbsp;&nbsp;&nbsp;Click on the delivery date to change.</label>  </p>
       <table width="590" id="tablePlain" border="0">
         <tr>
           <td width="110">Order No.: </td>
           <td width="173"><?php echo $d3 ?></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="61">&nbsp;</td>
           <td width="228" rowspan="4" valign="top"><b><?php echo $d11 ?></b><b><br />
           <?php echo $d12 ?><br />
           </b><b><?php echo $d13 ?><br />
           </b><b><?php echo $d14 ?></b></td>
         </tr>
         <tr>
           <td>Customer No:</td>
           <td>
             <?php if ($d0==0) { ?>
             <a href="customerNewTemp.php?<?php echo  "OrderNo=".$d3."&Comp=".$d5 ?> " onmouseover="ddrivetip('<?php echo 'no client number for this order.<br><b>Click to assign an account number</b>' ?>','#FFFFCC', 200)" onmouseout="hideddrivetip()";><?php echo "--"; ?></a>
             <?php  }
   else {
   echo $d0;} ?>
           </td>
           <td>&nbsp;</td>
         </tr>
         <tr>
           <td>Company Name:</td>
           <td>
             <?php if ($d0==0) {
      echo $d5; }
	  else {
	   echo $d1; } ?>
           </td>
           <td>&nbsp;</td>
         </tr>
         <tr>
           <td>Delivery date:</td>
           <td>
             <a href="" onClick="editwindow=dhtmlmodal.open('editBox', 'iframe', 'editOrderDateLine.php?orderNo=<?php echo $d3?>&field=<?php echo 'delDate' ?>&Type=D ', 'Edit delivery date', 'width=330px,height=170px,center=1,resize=0,scrolling=0')
needToConfirm = false;
editwindow.onclose=function(){return window}; return false"><?php if ($d10<"2000-01-01") {echo "t.b.a."; } else {echo date("d-m-Y ", strtotime($d10)); } ?></a>
          </td>
           <td>&nbsp;</td>
         </tr>
       </table>
      
<br />

<div class="selectionbox">
  <strong>Notes:</strong><br /> 
  <?php echo $d15 ?></div>
    </td> <!--center of tableMain-->
</tr>
<tr>
<td> <!--center of tableMain-->
     
      <?php echo $statusdisplay ?>
      <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 
	
	  while ($i_d < $num_d) {
		$saleId = mysql_result($displayQueryResult, $i_d, "saleId");
		$d4 = mysql_result($displayQueryResult, $i_d, "productId");
		$productNameQuery = "SELECT productId, productName, price FROM nam_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($displayQueryResult, $i_d, "price");
		$d9 = mysql_result($displayQueryResult, $i_d, "reserved");
		$d11 = mysql_result($displayQueryResult, $i_d, "canceled");
	?>
          <tr >
            <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>
      <strong><br />
      deliveries made on delivery notes:</strong>
      <table id="tableList">
        <tr>
          <?php
		$m = 0;
		while($m < $delNum){
		    echo "<td width=30><font size=2>";
			$del = mysql_result($delNoteResult, $m, "delNote");
			
			
			$deliveryQuery = "SELECT productId, quantity, date FROM nam_sales WHERE delNote = '$del' ";		
	        $deliveryQueryResult = mySQL_query($deliveryQuery) or die ("Error in query:   $deliveryQuery . * ". mySQL_error());
			$i_z = 0;
			$num_z = mysql_num_rows($deliveryQueryResult);
			$z0 = mysql_result($deliveryQueryResult, 0, "date");
			$list="";
			while ($i_z < $num_z) {
			   $z2 = mysql_result($deliveryQueryResult, $i_z, "productId");
			   $z3 = mysql_result($deliveryQueryResult, $i_z, "quantity"); 
		       $delProductQuery = "SELECT productName FROM nam_products WHERE productId=$z2 ";
			   $delProductResult = mySQL_query($delProductQuery) or die ("Error in query:   $delProductQuery . * ". mySQL_error());               
			   $z1 = mysql_result($delProductResult, 0, "productName");
			   $list = $list."<br>".$z3."  ".$z1;
			   $i_z++;
			   }
		
			
			?>
          <a href="viewDelivery.php?DelNoText=<?php echo $del ?>" onmouseover="ddrivetip('delivery date: <?php echo date("d-m-Y ", strtotime($z0)) ?> <br><b>delivered:</b><?php echo $list ?> <br><b> click to view delivery note no <?php echo $del ?></b>','#FFFFCC', 200)" onmouseout="hideddrivetip()";><?php echo $del ?></a>
          </font>
          <?php 
	if ($m==9) {echo "</tr> <tr>";}
	if ($m==19) {echo "</tr> <tr>";}
	if ($m==29) {echo "</tr> <tr>";}
	if ($m==39) {echo "</tr> <tr>";}
	if ($m==49) {echo "</tr> <tr>";}
	if ($m==59) {echo "</tr> <tr>";}
	if ($m==69) {echo "</tr> <tr>";}
	if ($m==79) {echo "</tr> <tr>";}
	if ($m==89) {echo "</tr> <tr>";}
	if ($m==99) {echo "</tr> <tr>";}
	if ($m==109) {echo "</tr> <tr>";}
	if ($m==119) {echo "</tr> <tr>";}
	if ($m==129) {echo "</tr> <tr>";}
	if ($m==139) {echo "</tr> <tr>";}
	if ($m==149) {echo "</tr> <tr>";}
	if ($m==159) {echo "</tr> <tr>";}
	if ($m==169) {echo "</tr> <tr>";}
	if ($m==179) {echo "</tr> <tr>";}
	if ($m==189) {echo "</tr> <tr>";}
	if ($m==199) {echo "</tr> <tr>";}
	if ($m==209) {echo "</tr> <tr>";}
	if ($m==219) {echo "</tr> <tr>";}
	if ($m==229) {echo "</tr> <tr>";}
	if ($m==239) {echo "</tr> <tr>";}
	if ($m==249) {echo "</tr> <tr>";}
	 $m++;
		//if ($m>10) {echo "</tr> <tr	>"; } 
		}
		
		?>
          <td></td>
        </tr>
      </table>
      <br />
    </td>
  </tr>
  <tr>
  <td height="37">
    <p class="main">
      
      <input name="button" type="button" class="imagebutton" id="button" onclick="MM_goToURL('parent','showAllProducts.php?cat=Tiles and Accessories&amp;productSelect=All&amp;colorSelect=All');return document.MM_returnValue" value="Done" />
  </p>
  </td>
  </tr>
</table>
<!--end of tableMain-->


<?php
		include("../footer.php");

		}
?>

