<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

<?php
include ("../navigation/header.php");
$page_name = "Packing";
include ("../navigation/access.php");
require_once ('../include/tc_calendar.php');

$packing_date = $_GET['packing_date'];
$show = $_GET['show'];

if ($packing_date=='') {
$packing_date = strtotime(date("Y-m-d"));
$d = date("d", $packing_date );
$m = date("m", $packing_date );
$y = date("Y", $packing_date );
}

$table_name = $_SESSION['db_prefix']."_production";
$packable_query = "SELECT production_no, production_date, product_item_id, produced_no, packed_no, (produced_no - SUM(packed_no+damaged_no)) AS outstanding, batch_code FROM $table_name GROUP BY production_no HAVING (produced_no - SUM(packed_no+damaged_no)) > 0 ORDER BY production_no ASC";
$packable_result = $con->query($packable_query);
$packable_num = mysqli_num_rows($packable_result);
	$w = 0;
	$arrId4 = array ();
	$arrName4 = array ();
	while($row = $packable_result->fetch_assoc()) {while ($w < $packable_num) {
		$production_no = $row['production_no'];
		$production_date = $row['production_date'];
		$produced_no = $row['produced_no'];
		$packed_no = $row['packed_no'];
		$quantity = $row['outstanding'];
		$batch_code = $row['batch_code'];
		$product_item_id = $row['product_item_id'];
			$table_name = $_SESSION['db_prefix']."_product_store";
			$item_name_query = "SELECT item_name FROM $table_name WHERE product_item_id = '".$product_item_id."' ";
			$item_name_result = $con->query($item_name_query);
			$row = mysqli_fetch_assoc($item_name_result);
			$item_name = $row['item_name'];
			
		$row = mysqli_fetch_assoc($packable_result);
		$arrId4[$w] = $production_no;
		$arrName4[$w] = $production_no.'&nbsp;&nbsp;|&nbsp;&nbsp;'.date("d-m-Y", strtotime($production_date)).'&nbsp;&nbsp;|&nbsp;&nbsp;'.$item_name.'&nbsp;&nbsp;|&nbsp;&nbsp;'.$batch_code.'&nbsp;&nbsp;|&nbsp;qty:'.$quantity;
		$w++;
	}
	}

if ($show==1) {
$production_no = $_GET['production_no_select'];


$table_name = $_SESSION['db_prefix']."_production";
$production_no_query = "SELECT production_date, product_item_id, mix_design_no, batch_code, produced_no, packed_no, (produced_no - SUM(packed_no+damaged_no)) AS outstanding FROM $table_name WHERE production_no = '".$production_no."' GROUP BY production_no ORDER BY production_id DESC ";
$production_no_result = $con->query($production_no_query);
$row = mysqli_fetch_assoc($production_no_result);
$production_date = $row['production_date'];
$product_item_id = $row['product_item_id'];
$bb_string = $row['batch_code'];
$produced_no = $row['produced_no'];
$packed_no = $row['packed_no'];
$pack_qty = $row['outstanding'];

//$pack_qty = $produced_no - $packed_no;
$mix_design_no = $row['mix_design_no'];



$table_name = $_SESSION['db_prefix']."_product_store";
$item_id_query = "SELECT item_name  FROM $table_name WHERE product_item_id = '".$product_item_id."' ";
$item_id_result = $con->query($item_id_query);
$row = mysqli_fetch_assoc($item_id_result);
$product_name = $row['item_name'];

$table_name = $_SESSION['db_prefix']."_mix_design";
$pack_design_query = "SELECT packaging_design_no, packaging_design FROM $table_name WHERE design_no = '".$mix_design_no."' ";
$pack_design_result = $con->query($pack_design_query);
$row = mysqli_fetch_assoc($pack_design_result);
$packaging_design_no = $row['packaging_design_no'];
$packaging_design_name = $row['packaging_design'];


$table_name = $_SESSION['db_prefix']."_packaging_design";
$pack_design_query = "SELECT design_no, packaging_design, est_qty, pack_qty FROM $table_name ORDER BY packaging_design ASC";
$pack_design_result = $con->query($pack_design_query);
$pack_design_num = mysqli_num_rows($pack_design_result);
	$w = 0;
	$arrId2 = array ();
	$arrName2 = array ();
	while ($w < $pack_design_num) {
		$row = mysqli_fetch_assoc($pack_design_result);
		$arrId2[$w] = $row['design_no'];
		$arrName2[$w] = $row['packaging_design'].'  ('.$row['est_qty'].') --'.$row['pack_qty'].'';
		$w++;
	}
}
?>

<link href="../css/main.css" rel="stylesheet" type="text/css" />
<script language="javascript" src="../jscript/calendar.js"></script>
<script language="JavaScript" src="../jscript/cancel_confirmation.js"  type="text/javascript"></script> 
<script language="JavaScript" src="../jscript/alert_message.js"  type="text/javascript"></script>
<script language="JavaScript" src="../jscript/show_hide_layers.js"  type="text/javascript"></script> 
<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/gen_validatorv4.js"  type="text/javascript" xml:space="preserve"></script>  

<script>
function packed_calc() {
	var produced = document.getElementById('produced_text').value;
	var e = document.getElementById("damaged_select");
	var damaged = e.options[e.selectedIndex].value;
	var packed = 0;
	
	packed = produced - damaged;
	
	document.getElementById('packed_no_text').value = packed; 
		
}
</script>

<script>
function check_qty() {
	var produced = document.getElementById('produced_text').value;
	var e = document.getElementById("damaged_select");
	var damaged = e.options[e.selectedIndex].value;
	var packed_no = document.getElementById('packed_no_text').value;
	var packed = 0;
	
	packed = produced - damaged;
	
	if (packed_no > packed) {
	alertMessage(1, '', 'You are trying to pack more then available.<br>The packing quantity is reset.');
	document.getElementById('packed_no_text').value = packed;
	}
}
</script>

<script>
function design_count() {
	
	var e = document.getElementById('pack_design_select');
	var val = e.options[e.selectedIndex].text; 
	
	
	var pack_qty = val.split('(').pop().split(')')[0];
	var pack_unit = val.split('--').pop().split('--')[0];
	var to_pack_qty = document.getElementById('packed_no_text').value;	
	
	
		
	if (pack_qty != '') {
	design_no = (to_pack_qty / pack_qty / pack_unit);
	design_no = Math.ceil(design_no);
	alertMessage(1, '', 'To pack '+to_pack_qty+' products requires '+design_no+' design batches.');
	}
	
	var packed_qty = Math.floor(to_pack_qty / pack_unit);	
	
	document.getElementById('unit_packed_no_text').value = packed_qty;
	document.getElementById('unit_no_text').value = pack_unit;
	document.getElementById('batch_text').value = design_no;
}
</script>
	

</head>
<?php
if ($permission_num==1) {
	include ("../navigation/production_sidebar.php");
?>
<body onload="<?php if ($show==1) { ?> packed_calc(); <?php } ?>">

<div class="main_frame">
<div class="page_head_frame">
        <label class="label_page_head">Packing </label>
</div>

<?php 
if ($show==0) {
?>
<div class="page_middle_frame">
<form action="packing.php" method="get" name="form1">
<table width="649" border="0">
<tr>
<td width="117">Packing date:</td>
<td width="260">
<?php
		$myCalendar = new tc_calendar("packing_date", true);
		$myCalendar->setIcon("../images/iconCalendar.gif");
		$myCalendar->setDate($d, $m, $y);
		$myCalendar->setPath("./");
		$myCalendar->setYearInterval(2018, 2025);
		$myCalendar->writeScript();
?>
</td>
<td width="260"><input name="show" type="hidden" value="1" /></td>
</tr>
<tr>
  <td>Production no:</td>
  <td>
    <select name="production_no_select" autofocus="autofocus" class="selectbox300" style="width:450px;" id="production_no_select" >
      <option value=" " selected="selected" >&nbsp;</option>
      <?php
    	for ($j = 0; $j < count($arrId4); $j++) {
			echo "<option value=".$arrId4[$j].">".$arrName4[$j]."</option>";	
		}
     ?>
    </select>
    </td>
  <td><input type="submit" name="date_select_button" id="date_select_button" value="Select" class="imagebutton" onclick="needToConfirm = false;"/></td>
</tr>
</table>
</form>
<script language="JavaScript" type="text/javascript" xml:space="preserve">
  var frmvalidator  = new Validator("form1");
  frmvalidator.EnableMsgsTogether();
  frmvalidator.addValidation("production_no_select","req","- a production.");
</script>                
</div>
<?php 
}
?>

<?php 
if ($show==1) {
?>
<div class="page_middle_frame">
<form action="packing_save.php" method="get" name="form2">
Packing for:&nbsp;&nbsp;
<label style="font-weight:bold; font-size:18px;"><?php echo $product_name ?></label><br />
<br />
on: &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<label style="font-weight:bold; font-size:18px;"><?php echo date("l, d-m-Y", strtotime($packing_date))  ?></label>:
<input name="packing_date_day" id="packing_date_day" type="hidden" value="<?php echo date("Y-m-d", strtotime($packing_date)) ?>" />

 <br />
<br />
Production no:&nbsp;&nbsp;<?php echo $production_no ?>&nbsp;&nbsp;&nbsp;&nbsp;Production date:&nbsp;&nbsp;<?php echo date("l, d-m-Y", strtotime($production_date)) ?>
<input type="hidden" name="production_no" id="production_no" value="<?php echo $production_no ?>" />
<br />
<br />
Best before:&nbsp;&nbsp;<?php echo $bb_string; ?> 
<input name="batch_code_string" type="hidden" value="<?php echo $bb_string ?>" /><br />
<br />

<table width="538" height="179" border="0" cellpadding="0">
  <tr>
    <td width="202">No of products:</td>
    <td colspan="3"><label for="skips_select"></label>
      <input type="text" name="produced_text" id="produced_text" class="text70" onfocus = "this.select(); "onkeypress="return numbersonly(this, event)" value="<?php echo $pack_qty ?>" onchange="packed_calc();" readonly="readonly" style="border:hidden;"/> </td>
    </tr>
  <tr>
    <td>No of broken products:</td>
    <td colspan="3"><select name="damaged_select" id="damaged_select"  class="select80" onchange="packed_calc();">
      <option value="0" selected="selected">0</option>
      <option value="1">1</option>
      <option value="2">2</option>
      <option value="3">3</option>
      <option value="4">4</option>
      <option value="5">5</option>
      <option value="6">6</option>
      <option value="7">7</option>
      <option value="8">8</option>
      <option value="9">9</option>
      <option value="10">10</option>
      
    </select></td>
    </tr>
  <tr>
    <td>&nbsp;</td>
    <td colspan="3">&nbsp;</td>
    </tr>
  <tr>
    <td>No of packed products:</td>
    <td colspan="3"><input type="text" name="packed_no_text" id="packed_no_text"  onfocus="packed_calc(); this.select();" onkeyup="check_qty();" onchange="design_count();" autocomplete="off;"  />
      &nbsp;</td>
    </tr>
  <tr>
    <td>&nbsp;</td>
    <td colspan="3">&nbsp;</td>
    </tr>
  <tr>
    <td height="26">Packing design used:</td>
    <td colspan="3"><select name="pack_design_select" autofocus="autofocus" class="selectbox300" id="pack_design_select" onchange="design_count();">
      <option value="<?php echo $packaging_design_no ?>"><?php echo $packaging_design_name  ?></option>
      <?php
    	for ($j = 0; $j < count($arrId2); $j++) {
			echo "<option value=".$arrId2[$j].">".$arrName2[$j]."</option>";	
		}
     ?>
    </select></td>
    </tr>
  <tr>
    <td height="19">No of packed products:</td>
    <td width="68"><input type="text" name="unit_packed_no_text" id="unit_packed_no_text"  class="text_readonly"  readonly="readonly" /></td>
    <td width="168">Units per single pack:</td>
    <td width="90"><input type="text" name="unit_no_text" id="unit_no_text"  class="text_readonly"  readonly="readonly" /></td>
  </tr>
  <tr>
    <td>No of packing batches to be used:</td>
    <td colspan="3"><input type="text" name="batch_text" id="batch_text" class="text70" onfocus = "this.select(); "onkeypress="return numbersonly(this, event)" value="" readonly="readonly" style="border:hidden;"/></td>
    </tr>
</table>
<br />
<br />
<table width="600" border="0">
  <tr>
      <td width="20"></td>
      <td width="615" align="right"><input type="hidden" name="product_item_id" id="product_item_id" value="<?php echo $product_item_id ?>" />
        <input name="close_button" type="button" class="imagebutton" id="close_button" onClick="cancel_confirmation(3, '../production/production_main.php');" value="Close"/>
        <input name="save_close_button"  id="save_close_button" type="submit" class="imagebutton" value="Save &amp; Close" onClick="needToConfirm = false; confirmRefresh();" /></td>
    </tr>
</table>
</form>
<script language="JavaScript" type="text/javascript" xml:space="preserve">
  var frmvalidator  = new Validator("form2");
  frmvalidator.EnableMsgsTogether();
  frmvalidator.addValidation("packed_no_text","req","- packed products.");
  frmvalidator.addValidation("pack_design_select","req","- packing design.");
</script>
</div>
<?php
} ?>


</div>

<?php include ('../navigation/side_bar.php'); ?>

</body>
<?php } else { ?>
<script> needToConfirm = false; </script>
<?php
    $_SESSION['denied_return'] = "production/production_main.php?date=&branch=";	
    include ("../navigation/production_sidebar.php");?>
    <?php include ('../navigation/side_bar.php'); ?>
	<body onLoad="accessdenied(); return false">	
	</body>
<?php }
?>
</html>
