<!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 = "Production";
include ("../navigation/access.php");
require_once ('../include/tc_calendar.php');

$production_date = $_GET['production_date'];
$show = $_GET['show'];
$e = $_GET['e'];

if ($production_date > $today ) { ?>
<script>
window.location='production.php?show=0&production_date=&e=1';
</script>
<?php	  
}

$batch_warning = 'n';

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

$table_name = $_SESSION['db_prefix']."_mix_design";
$mix_query = "SELECT design_no, mix_design FROM $table_name  ORDER BY mix_design ASC";
$mix_result = $con->query($mix_query);
$mix_num = mysqli_num_rows($mix_result);
	$w = 0;
	$arrId4 = array ();
	$arrName4 = array ();
	while ($w < $mix_num) {
		$row = mysqli_fetch_assoc($mix_result);
		$arrId4[$w] = $row['design_no'];
		$arrName4[$w] = $row['mix_design'];
		$w++;
	}

if ($show==1) {
$design_no = $_GET['design_select'];
$skips = $_GET['skips'];
$recycled = $_GET['recycled_text'];
$recycle_TT_line = '';

$table_name = $_SESSION['db_prefix']."_mix_design";
$product_query = "SELECT mix_design, est_qty FROM $table_name WHERE design_no = '".$design_no."' ";
$product_result = $con->query($product_query);
$row = mysqli_fetch_assoc($product_result);
$product_name = $row['mix_design'];
$est_qty = $row['est_qty'];

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

$table_name = $_SESSION['db_prefix']."_production";
$production_no_query = "SELECT production_no, batch_no, production_date FROM $table_name ORDER BY production_no DESC LIMIT 1";
$production_no_result = $con->query($production_no_query);
$production_no_num = mysqli_num_rows($production_no_result);
if ($production_no_num==0) {
	  $production_no = 1;
	  $batch_no = 1;
}
else {
	$row = mysqli_fetch_assoc($production_no_result);
	$production_no = $row['production_no']+1;
	$batch_no = $row['batch_no']+1;
	$last_production_date = $row['production_date'];
	$last_production_month = date("Ym", strtotime($row['production_date']));
	$curr_month = date("Ym", strtotime($production_date));
	
	if ($batch_no > 99) {
		   $batch_no = 0;
		   $batch_warning = 'y';
	}
	

	
	if ($curr_month > $last_production_month) {
		  $batch_no = 0;
	}
}

$batch_no = str_pad($batch_no, 2, '0', STR_PAD_LEFT);
$bb_date = date("d-m-Y", strtotime(date("d-m-Y", strtotime($production_date)) . " + 2 year"));
$bb_date =date("dmy", strtotime($bb_date));
$bb_string = $bb_date.'     '.$batch_no.'     '.date("my", strtotime($production_date));


}
?>

<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 production_calc() {
	var est_qty = <?php echo $est_qty ?>
	
	 sskips = document.getElementById("no_skips_text").value;
	 	
	var  skip_products = +est_qty * sskips
	
		
	var produced = '0';
	produced = (+est_qty * sskips);
	
	document.getElementById('skip_products_text').value = skip_products;
	document.getElementById('produced_text').value = produced;
		
}
</script>



</head>
<?php
if ($permission_num==1) {
	include ("../navigation/production_sidebar.php");
?>
<body onload="
<?php if ($e==1) {?> alertMessage(1, '', 'You cannot record a production for dates after today.'); <?php } ?>
<?php if ($show==1) {?> document.getElementById('no_skips_text').value = <?php echo $skips ?>; production_calc(); <?php } ?>
<?php if ($batch_warning=='y') {?>alertMessage(1, '', 'The maximum number of batches for this month were made, the number is set to zero.');<?php } ?>
">

<div class="main_frame">
<div class="page_head_frame">
        <label class="label_page_head">Production packing</label>
</div>

<?php 
if ($show==0) {
?>
<div class="page_middle_frame">
<form action="production.php" method="get" name="form1">
<input name="e" type="hidden" value="0" />
<table width="649" border="0">
<tr>
<td width="117">Production date:</td>
<td width="260">
<?php
		$myCalendar = new tc_calendar("production_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" /><input name="skips" type="hidden" value="0" /><input name="recycled_text" type="hidden" value="" /></td>
</tr>
<tr>
  <td>Product:</td>
  <td>
    <select name="design_select" autofocus="autofocus" class="selectbox200" >
      <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("design_select","req","- a product.");
</script>                
</div>
<?php 
}
?>

<?php 
if ($show==1) {
?>
<div class="page_middle_frame">
<form action="production_save.php" method="get" name="form2">
Production for: &nbsp;&nbsp;&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;&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($production_date))  ?></label> 
<input name="production_date_day" id="production_date_day" type="hidden" value="<?php echo date("Y-m-d", strtotime($production_date)) ?>" />
<input type="hidden" name="mix_design_no" id="mix_design_no"  value="<?php echo $design_no ?>"/><input name="batch_no_text" type="hidden" value="<?php echo $batch_no ?>" />
 <br />
<br />
Production no: <?php echo $production_no ?>&nbsp;&nbsp;
<input type="hidden" name="production_no" id="production_no" value="<?php echo $production_no ?>" />
&nbsp;&nbsp;Best before: &nbsp;&nbsp;&nbsp;<?php echo $bb_string; ?> 
<input name="batch_code_string" type="hidden" value="<?php echo $bb_string ?>" /><br />
<br />

<table width="500" border="0" cellpadding="0" >
  <tr>
    <td width="167">No of batches:</td>
    <td width="88"><input type="text" name="no_skips_text" id="no_skips_text" class="text50" autocomplete="off" onfocus = "this.select();"  onkeyup="production_calc();"/></td>
    <td width="237">Estimated products per batch: <?php echo $est_qty ?> <input type="hidden" name="est_qty_text" id="est_qty_text" value="<?php echo $est_qty ?>"/></td>
  </tr>
  <tr>
    <td>Est. no of batch products:</td>
    <td><input type="text" name="skip_products_text" id="skip_products_text" class="text50"  readonly="readonly"/></td>
    <td>&nbsp;</td>
  </tr>
  <tr>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
  </tr>
  <tr>
    <td>No of produced products:</td>
    <td><input type="text" name="produced_text" id="produced_text" class="text50" onfocus = "this.select();" /></td>
    <td>&nbsp;</td>
  </tr>
</table>
<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("produced_text","req","- produced products.");
</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>
