<?php 
if ($_SESSION['order_no']=='') {
?>
<script language="JavaScript" src="../jscript/confirm_exit.js"  type="text/javascript"></script> 
<script language="JavaScript" src="../jscript/go_to_url.js"  type="text/javascript"></script> 
<script language="JavaScript" src="../jscript/show_hide_layers.js"  type="text/javascript"></script> 
<script language="javascript" type="text/javascript">
          needToConfirm = false;
             window.location="store_no_return.php";
</script>
<?php
}
else {
 
    $table_name = $_SESSION['db_prefix']."_orders";
		$quote_query = "SELECT *  FROM $table_name WHERE order_no = '".$_SESSION['order_no']."'";
		$quote_result = $con->query($quote_query);
		$row = mysqli_fetch_assoc($quote_result);
		$order_reference = $row['order_reference'];
		$pallets = $row['pallets'];
		$pallets_price = $row['pallets_price'];
		$del_pallets = $row['del_pallets'];
				
		$available_pallets = $pallets - $del_pallets;
				
		$table_name = $_SESSION['db_prefix']."_order_lines";
		$line_query = "SELECT *  FROM $table_name WHERE order_no = '".$_SESSION['order_no']."'";
		$line_result = $con->query($line_query);
		$line_num = mysqli_num_rows($line_result);
		

?>
<script language="JavaScript" src="../jscript/alert_message.js"  type="text/javascript"></script>  
<script type="text/javascript">
/*function validate_required(input,alerttxt,maxvalue)
{
with (input)
  {
  if (value>maxvalue)
    {
    alertMessage(1, '', 'The delivery amount exceeds the balance, the entry will be cleared. Please enter a valid quantity.');
	input.value = "";
	return false;
	}
  else
    {
    return true;
    }
  }
}*/
</script>

<script type="text/javascript">
function validate_pallets(input,alerttxt,maxvalue)
{
with (input)
  {
  if (value>maxvalue)
    {
    alertMessage(1, 'tile_receive.php', 'You are to supply more <br> pallets as deposit was paid for!');
	return false;
	}
  else
    {
    return true;
    }
  }
}
</script>

<script>
/*function del_all() {
	
	for (var i=0; i < 25; i++) {
		    document.getElementById('quantity_text'+i).value=document.getElementById('balance_text'+i).value;
			document.getElementById('quantity_text'+i).onchange;
	}
}*/
</script>

<script>
function pallets_all() {
	    document.getElementById('pallets_text').value=document.getElementById('balance_pallets').value;
}
</script>

</head>

<?php include('../include/wait.php') ?>

<body onLoad="MM_showHideLayers('wait','','hide');  " onFocus="MM_showHideLayers('wait','','hide');" >

<form id="form1" name="form1" method="get" action="deliver_product_Wizbase.php">
<div class="wizard_page_middle_frame">
<div class="wizard_frame" align="right"></div>
<div class="wizard_frame" >
<table width="650" id="table_display" >
           <tr>
             <td height="26" align="left" valign="top">Item</td>
             <td width="56" align="center" valign="top">Group</td>
             <td width="31" align="center" valign="top">Unit</td>
             <td width="61" align="center" valign="top">Ordered</td>
             <td width="62" align="center" valign="top">&nbsp;Delivered</td>
             <td width="55" align="center" valign="top">Balance</td>
             <td colspan="2" align="center" valign="top">Deliver&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Batch code</td>
           </tr>
           <?php
		   $i=0;
				while($row = $line_result->fetch_assoc()) {
						$a0 = $row["order_line_id"];
						$a1 = $row["product_item_id"];
						$a6 = $row["quantity"];
					 	$a5 = $row["delivered"];
						$price = $row["price"];
						
					 
					 $table_name = $_SESSION['db_prefix']."_product_store";
						$item_query = "SELECT item_name, unit, subCategory FROM $table_name WHERE product_item_id = '".$a1."' ";
						$item_result = $con->query($item_query);
						$row = mysqli_fetch_assoc($item_result);
						$a1a = $row['item_name'];
						$a3a = $row['unit'];
						$a4a = $row['subCategory'];

					 					 
					$table_name = $_SESSION['db_prefix']."_deliver_product_lines_temp";
					$quantity_query = "SELECT quantity, batch_code  FROM $table_name WHERE order_reference = '".$_SESSION['order_reference']."' AND product_item_id = '".$a1."' AND user_name = '".$_SESSION['username']."'"; 
					$quantity_result = $con->query($quantity_query);
					$quantity_num = mysqli_num_rows($quantity_result);
					
					
					
					
				$table_name = $_SESSION['db_prefix']."_product_store_lines";
				$stock_query = "SELECT SUM(quantity_in - quantity_out) AS stock_available FROM $table_name WHERE product_item_id = '".$a1."' AND (quantity_in - quantity_out) > 0 ";
				$stock_result = $con->query($stock_query);
				$row = mysqli_fetch_assoc($stock_result);
				$stock_available = $row['stock_available'];
				
					 
				 ?>
           <tr>
             <td valign="middle"><?php echo $a1a ?>
             <input type="hidden" name="store_item_id<?php echo $i?>" id="store_item_id<?php echo $i?>" value="<?php echo $a1 ?>"></td>
             <td valign="middle"><?php echo $a4a ?></td>
             <td valign="middle"><?php echo $a3a ?></td>
             <td align="right" valign="middle"><?php echo $a6 ?></td>
             <td align="right" valign="middle"><?php echo $a5 ?></td>
             <td align="right" valign="middle"><?php $a7=$a6-$a5;  echo $a7; ?>
             <input name="balance_text<?php echo $i ?>" id="balance_text<?php echo $i?>" type="hidden" value="<?php echo $a7 ?>">
             <input name="price" type="hidden" value="<?php echo $price ?>"></td>
             <td colspan="2" align="right" valign="middle">
             <?php if ($stock_available > 0) { ?>
             <div style="float:left"><a href="" onMouseOver="ddrivetip('<b>Available stock: <?php echo $stock_available ?></b><br>To select qty and batch code to deliver, click here.', 200)"; onMouseOut="hideddrivetip()" onClick="editwindow=dhtmlmodal.open('editBox', 'iframe', '../edit_windows/deliver_products.php?product_item_id=<?php echo $a1 ?>&order_reference=<?php echo $order_reference ?>&order_balance=<?php echo $a7 ?>&price=<?php echo $price ?>', 'Available products for delivery', 'width=400px,height=300px,center=1,resize=0,scrolling=0')
needToConfirm = false;
editwindow.onclose=function(){return window}; return false"><img src="../images/line_edit.png" width="12" height="12" /></a></div>
			<?php } else { ?>
            <div style="float:left">
            <img src="../images/exclamation_mark_red_small.png" width="10" height="16">&nbsp;&nbsp;No stock available.
            </div>
            <?php } ?>
<?php 
while($row = $quantity_result->fetch_assoc()) {
$a8 = $row['quantity'];
$a9 = $row['batch_code'];
?>							
<input name="quantity_text<?php echo $i ?>" type="text" class="text60" id="quantity_text<?php echo $i ?>"  onkeypress="return numbersonly(this, event)" onKeyUp="validate_required(quantity_text<?php echo $i?>, 'The delivery amount exceeds the balance, the entry will be cleared. ' +'\n'+ 'Please enter a valid amount.', <?php echo $a7 ?>);"  value="<?php echo $a8 ?>" style="width:30px; font-weight:bold; border:hidden; text-align:right" readonly  >&nbsp;&nbsp;&nbsp;<input name="batch_code_text<?php echo $i ?>" type="text" style="width:170px; font-weight:bold; border:hidden;" readonly id="batch_code_text<?php echo $i ?>"   autocomplete="off"  onfocus = "this.select();" onKeyUp="ToUpper(this)" maxlength="4" value="<?php echo $a9 ?>" >							
			
<?php } 
?>              </td>
           </tr>
           <?php 
			   // $a6_total=$a6_total+$a6;
				$i++;
				 }
				 ?>
           <?php 
					$table_name = $_SESSION['db_prefix']."_deliver_product_lines_temp";
					$quantity_query = "SELECT pallets  FROM $table_name WHERE order_reference = '".$_SESSION['order_reference']."' AND pallets > 0 AND user_name = '".$_SESSION['username']."'"; 
					$quantity_result = $con->query($quantity_query);
					if ( $quantity_num> 0) {
							$row = mysqli_fetch_assoc($quantity_result);
							$a9 = $row['pallets'];
					}
					else { 
					$a9 = '';
					}
	?>          
           <tr>
             <td colspan="8" align="left" valign="middle">&nbsp;</td>
           </tr>
           <tr>
             <td align="left" valign="middle">
             Pallets</td>
             <td align="left" valign="middle">&nbsp;</td>
             <td align="left" valign="middle">&nbsp;</td>
             <td align="right" valign="middle"><?php echo $pallets ?></td>
             <td align="right" valign="middle"><?php echo $del_pallets ?></td>
             <td align="right" valign="middle"><?php if ($pallets!=0) { $bal_pallets = $pallets - $del_pallets;} else {$bal_pallets=0;} echo $bal_pallets ?>
             <input name="balance_pallets" id="balance_pallets" type="hidden" value="<?php echo $bal_pallets ?>"> <input name="pallets_price" type="hidden" value="<?php echo $pallets_price ?>"></td>
             <td width="61" align="right" valign="middle"><input type="text" name="pallets_text" id="pallets_text"  class="text50" value="<?php echo $a9 ?>" onfocus = "this.select();" onKeyPress="return numbersonly(this, event)" onKeyUp="validate_pallets(pallets_text, '', <?php echo $bal_pallets ?>)" style="width:50px; text-align:right" autocomplete="off"></td>
             <td width="167" align="right" valign="middle">&nbsp;</td>
           </tr>
         </table>
</div>
<div class="wizard_frame">
				<input name="s" type="hidden" id="s" value="2" />
              	<input name="p" type="hidden" id="p" value="2" />
                <input name="line_num"  id="line_num" type="hidden" value="<?php echo $line_num ?>">
      <table width="590" height="50" align="center" id="table_button_bar">
          <tr>
            <td width="117"><input type="button" name="lastprev_button" class="imagebutton" value="Prev" onClick="needToConfirm = false;   MM_showHideLayers('wait','','show'); MM_goToURL('parent','deliver_product_Wizbase.php?s=&p=1');return document.MM_returnValue" /></td>
            <td width="115">&nbsp;</td>
            <td width="99">&nbsp;</td>
            <td width="125"><input name="cancelbutton" type="button" class="imagebutton" id="cancelbutton" onClick="javascript:cancel_confirmation(1, '../store/store_main.php?date=0&product=salt'); needToConfirm = false;" value="Cancel" /></td>
            <td width="110"><input type="submit" name="next_button" id="next_button" class="imagebutton" value="Next" onClick="needToConfirm = false; MM_showHideLayers('wait','','show');" /></td>
          </tr>
    </table>
  </div>

</form>

</body>

<?php 
}
?>