<!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) {
	
$product = $_GET['product'];


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

$table_name = $_SESSION['db_prefix']."_mix_design";
$mix_query = "SELECT  mix_design FROM $table_name WHERE product_type = 'Salt product' ORDER BY mix_design ASC";
$mix_result = $con->query($mix_query);;
	$w = 0;
	$arrId4 = array ();
	$arrName4 = array ();
	while($row = $mix_result->fetch_assoc()) {
		$arrId4[$w] = $row["mix_design"];
		$arrName4[$w] = $row["mix_design"];
		$w++;
	}
 
$table_name = $_SESSION['db_prefix']."_production";
$table_name2 = $_SESSION['db_prefix']."_mix_design";
if ($product=='All') {	 	
$display_query = "SELECT production_no, production_date, mix_design_no, skips_no, produced_no, production_cost  FROM $table_name WHERE production_date >= '".$start."' AND production_date <= '".$end."' GROUP BY production_no " ;
}
else {
$display_query = "SELECT production_no, production_date, mix_design_no, skips_no, produced_no, production_cost  FROM $table_name, $table_name2  WHERE production_date >= '".$start."' AND production_date <= '".$end."'  and design_no = mix_design_no AND mix_design = '".$product."' GROUP BY production_no" ;
}
$display_result = $con->query($display_query);
$i = 0;


$table_name = $_SESSION['db_prefix']."_presets";
$production_margin_query = "SELECT preset_value FROM $table_name WHERE preset_name = 'production_margin'  ";
$production_margin_result = $con->query($production_margin_query);
$row = mysqli_fetch_assoc($production_margin_result);
$production_margin = $row["preset_value"];

		
?> 

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

</head>


<body>

<div class="main_frame">
<div class="page_head_frame">
  <label class="label_page_head">Production costing </label></div>
   
<br />
<br /><br />
<br />
<?php
$page='costing_product_list.php';
$reference_name='product';
$reference=$product;
$reference_name_2='';
$reference_2='';
include('../include/period_select_block.php');
?>


    
<br />
<div class="page_middle_frame">
<div style="float:left;">
Select single product:
<form action="costing_product_list.php" method="get" name="form_2">
<select name="product_select" autofocus class="selectbox300" id="product_select" onchange="window.location.href= this.form.product_select.options[this.form.product_select.selectedIndex].value">
  <option value="<?php echo $product ?>" selected="selected" ><?php echo $product ?></option>
  <option value="costing_product_list.php?product=All&date=1&date1=<?php echo $start ?>&date2=<?php echo $end ?>">All</option>
  <?php
    	for ($j = 0; $j < count($arrId4); $j++) {
 ?>			
  <option value="costing_product_list.php?product=<?php echo $arrId4[$j] ?>&date=1&date1=<?php echo $start ?>&date2=<?php echo $end ?>"><?php echo $arrName4[$j] ?></option>	
 <?php }
     ?>
</select>
</form>
</div>

<!--jscript-->
<script type="text/javascript" language="javascript" src="../jscript/jquery.dataTables.js"></script>
<script type="text/javascript" charset="utf-8">
			$(document).ready(function() {
				$('#scroll_table').dataTable( {
					"sScrollY": "500px",
					"bPaginate": false,
					"bScrollCollapse": true,
					"aaSorting": [[ 0, "desc" ]],
					"aoColumns": [
								{"iDataSort": 1},
								{"bVisible": false},
								{"iDataSort": 3},
								{"bVisible": false},
                              	{"bSortable": true},
								{"bSortable": true},
								{"bSortable": true},
								{"bSortable": true},
								{"bSortable": true},
								{"bSortable": true},
								{"bSortable": true},
								{"bSortable": true}
                         	 ]
				} );
			} );
		</script>    	
<table width="796" id="scroll_table" class="data_display" >
	<thead>
		<tr>
		  <th width="41" align="left">Prod<br>no</th>
		  <th width="23" align="left">&nbsp;</th>
		  <th width="79" align="left">Date</th>
		  <th width="23" align="left">&nbsp;</th>
		  <th width="144" align="left">Mix design</th>
		  <th width="48" align="left">Skips</th>
		  <th width="79" align="left">Skips Cost</th>
		  <th width="91" align="left">Products<br>produced</th>
		  <th width="81" align="left">Cost per unit produced</th>
		  <th width="19" align="left">&nbsp;</th>
		  <th width="71" align="left">Est. cost per unit</th>
		  <th width="45" align="left">Diff %</th>
        </tr>
	</thead>
	<tbody>
  <?php
  
    $i=0;
    while($row = $display_result->fetch_assoc()) {
			$b0 = $row["production_no"];
			$b1 = $row["production_date"];
		 	$b2 = $row["mix_design_no"];
		 	$b3 = $row["production_cost"];
			$b4 = $row["skips_no"];
			$b5 = $row["produced_no"];
		 
	$table_name = $_SESSION['db_prefix']."_mix_design";
	$product_name_query = "SELECT mix_design, est_qty FROM $table_name WHERE design_id = '".$b2."' ";
	$product_name_result = $con->query($product_name_query);
	$row = mysqli_fetch_assoc($product_name_result);
	$b2a = $row['mix_design'];
	$b7 = $row['est_qty'];
		
			
		if ($b5!=0) {
		$b6 = $b3/$b5;
		$b8 = $b3/$b4/$b7;
		$b11 = (($b6 / $b8) * 100) - 100;
		} 
		else {
			$b6 = 0;
			$b8 = 0;
			$b11 = 0;
		}
	
				

		?>
		<tr >
		  <td ><a href="production_slip.php?production_no=<?php echo $b0 ?>" onMouseover="ddrivetip('Click to view production report', 150)"; onMouseout="hideddrivetip()"><?php echo $b0 ?></a></td>
		  <td ><?php echo $b0 ?></td>
		  <td ><?php echo date("d-m-Y", strtotime($b1 ))?></td>
		  <td ><?php echo $b1 ?>&nbsp;</td>
		  <td align="left"><?php echo $b2a ?></td>
		  <td align="right"><?php echo $b4 ?></td>
		  <td align="right"><?php echo number_format($b3, 2, '.', ',') ?></td>
		  <td align="right"><?php echo $b5 ?></td>
		  <td align="right"><?php echo number_format($b6, 2, '.', ',') ?></td>
		  <td align="right">&nbsp;</td>
		  <td align="right"><?php echo number_format($b8, 2, '.', ',') ?></td>
		  <td align="right"><?php if ($b11>$production_margin) {echo '<font color=#FF0000>'.number_format($b11, 2, '.', ',').'</font>';} else { echo number_format($b11, 2, '.', ','); } ?></td>
	    </tr>
		     
		<?php
		$i++; 
	}
	?>
</tbody>   
</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'); ?>