<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>

<?php 
include ("../navigation/header.php");

$page_name = "Production-main";
include ("../navigation/access.php");

if ($permission_num==1) {

include ("../navigation/production_sidebar.php");
include('../include/period_select_date.php');	

$mix_design_name = $_GET['mix_design_name'];
 
$table_name = $_SESSION['db_prefix']."_production_plan";	 	
$display_query = "SELECT production_no, production_date, mix_design_name FROM $table_name WHERE production_date BETWEEN '".$start."' AND '".$end."' AND mix_design_name = '".$mix_design_name."' ";
$display_result = execsql($display_query);
$display_num = mysql_num_rows($display_result);
		
?> 

<!--style sheets-->
<link rel="stylesheet" type="text/css" href="../css/scroll_table.css" />

<link rel="stylesheet" type="text/css" href="../css/scroll_table.css" />
<script type="text/javascript" language="javascript" src="../jscript/jquery.dataTables.js"></script>
<script language="JavaScript" src="../jscript/tablesort.js"  type="text/javascript"></script> 

</head>


<body>

<div class="main_frame">
<div class="page_head_frame">
  <label class="label_page_head">Production <?php echo $mix_design_name ?></label></div>
   
<br />
<br /><br />
<br />
<div class="page_middle_frame">
<?php
$page='production_item_list.php';
$reference_name='mix_design_name';
$reference=$mix_design_name;
include('../include/period_select_block.php');
?>


    
<br />
<div class="page_inside_frame">
<script type="text/javascript" charset="utf-8">
			$(document).ready(function() {
				$('#scroll_table').dataTable( {
					"sScrollY": "500px",
					"bPaginate": false,
					"bScrollCollapse": true,
					"aaSorting": [[ 0, "desc" ]],
					"aoColumns": [
								{"iDataSort": 7},
                              	{"bSortable": true},
                             	{"bSortable": true},
								{"bSortable": true},
								{"bSortable": true},
								{"bSortable": true},
								{"bSortable": true},
								{"bVisible": false}
								 ]
				} );
			} );
		</script>

  <table width="689" id="scroll_table" class="data_display" >
    <thead>
    <tr>
      <th width="63" align="center">Date</th>
      <th width="186" align="center">Product</th>
      <th width="96" align="center">Planned<br />
        skips</th>
      <th width="81" align="center">Skip<br />
        produced</th>
      <th width="74" align="center">Estimated<br />
        products</th>
      <th width="83" align="center">Produced<br />
wet products</th>
      <th width="85" align="center">Produced<br />
        dry products</th>
      <th width="85" align="center">&nbsp;</th>
    </tr>
    </thead>
    <?php
$total=0;
$i=0;
while ($i<$display_num) {
		$production_date = mysql_result($display_result, $i, "production_date");
		$mix_design_name = mysql_result($display_result, $i, "mix_design_name");
		
		
		$table_name = $_SESSION['db_prefix']."_production_plan";
		$name_query = "SELECT production_no, production_date  FROM $table_name WHERE mix_design_name = '".$mix_design_name."'AND production_date = '".$production_date."' ";
		$name_result = mySQL_query($name_query);
		$name_num = mysql_num_rows($name_result);
		$n=0;
		while ($n<$name_num) {
			$production_no = mysql_result($name_result, $n, "production_no");
			$production_date = mysql_result($name_result, $n, "production_date");
		
		
		$table_name = $_SESSION['db_prefix']."_production_plan_lines";
		$skip_query = "SELECT SUM(quantity) AS summed_skip FROM $table_name WHERE production_no = '".$production_no."' AND action = 'skip' GROUP BY action ";
		$skip_result = mySQL_query($skip_query);
		$skip_num = mysql_num_rows($skip_result);
		if ($skip_num!=0) {
		  $skip_quantity = mysql_result($skip_result, 0, "summed_skip");
		}
		else {
			$skip_quantity = 0;
		}
		
		
		$table_name = $_SESSION['db_prefix']."_production_plan_lines";
		$skip_produced_query = "SELECT SUM(quantity) AS summed_skip_produced FROM $table_name WHERE production_no = '".$production_no."' AND action = 'skip_count' GROUP BY action ";
		$skip_produced_result = mySQL_query($skip_produced_query);
		$skip_produced_num = mysql_num_rows($skip_produced_result);
		if ($skip_produced_num!=0) {
		  $skip_produced = mysql_result($skip_produced_result, 0, "summed_skip_produced");
		}
		else {
			$skip_produced = 0;
		}
		
		
		$table_name = $_SESSION['db_prefix']."_production_plan_lines";
		$estimated_query = "SELECT SUM(quantity) AS summed_estimated FROM $table_name WHERE production_no = '".$production_no."' AND action = 'estimated' GROUP BY action ";
		$estimated_result = mySQL_query($estimated_query);
		$estimated_num = mysql_num_rows($estimated_result);
		if ($estimated_num!=0) {
		  $est_quantity = mysql_result($estimated_result, 0, "summed_estimated");
		}
		else {
			$est_quantity = 0;
		}
		
		
		$table_name = $_SESSION['db_prefix']."_production_plan_lines";
		$wet_produced_query = "SELECT SUM(quantity) AS summed_wet_produced FROM $table_name WHERE production_no = '".$production_no."' AND action = 'wet_count' GROUP BY action ";
		$wet_produced_result = mySQL_query($wet_produced_query);
		$wet_produced_num = mysql_num_rows($wet_produced_result);
		if ($wet_produced_num!=0) {
		  $wet_produced = mysql_result($wet_produced_result, 0, "summed_wet_produced");
		}
		else {
			$wet_produced = 0;
		}
		
		
		$table_name = $_SESSION['db_prefix']."_production_plan_lines";
		$dry_produced_query = "SELECT SUM(quantity) AS summed_dry_produced FROM $table_name WHERE production_no = '".$production_no."' AND action = 'dry_count' GROUP BY action ";
		$dry_produced_result = mySQL_query($dry_produced_query);
		$dry_produced_num = mysql_num_rows($dry_produced_result);
		if ($dry_produced_num!=0) {
		  $dry_produced = mysql_result($dry_produced_result, 0, "summed_dry_produced");
		}
		else {
			$dry_produced = 0;
		}
		
		
		$table_name = $_SESSION['db_prefix']."_production_plan_lines";
		$depalleted_query = "SELECT SUM(quantity) AS summed_depalleted FROM $table_name WHERE production_no = '".$production_no."' AND action = 'dep_count' GROUP BY action ";
		$depalleted_result = mySQL_query($depalleted_query);
		$depalleted_num = mysql_num_rows($depalleted_result);
		if ($depalleted_num!=0) {
		  $depalleted = mysql_result($depalleted_result, 0, "summed_depalleted");
		}
		else {
			$depalleted = 0;
		}
			
		
		//$productionTT_body = $productionTT_body.'<tr><td>'.$production_no.'</td><td>'.date("d-m-Y", strtotime($production_date)).'</td></tr>';
		$productionTT_body = '';
		
		$n++;
		}
		$productionTT_header='<b>Production for '.$mix_design_name.'</b><br><table><tr><td width=100>Prdoduction no</td><td width=100>production date</td></tr>';		
		$productionTT_footer='</table><br>Click for details.';
		$productionTT=$productionTT_header.$productionTT_body.$productionTT_footer;
		
		
		
		
		?>
    <tr>
      <td><?php echo date("d-m-Y", strtotime($production_date)) ?></td>
      <td><?php echo $mix_design_name ?></td>
      <td align="right"><?php echo number_format($skip_quantity, 1, '.', ',') ?></td>
      <td align="right"><?php echo number_format($skip_produced, 1, '.', ',') ?></td>
      <td align="right"><?php echo number_format($est_quantity, 0, '.', ',') ?></td>
      <td align="right"><?php echo number_format($wet_produced, 0, '.', ',') ?></td>
      <td align="right"><?php echo number_format($dry_produced, 0, '.', ',') ?></td>
      <td align="right"><?php echo $production_date ?></td>
    </tr>
    <?php
  $i++;
}
?>

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