<!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";
$mixQuery = "SELECT  mix_design FROM $table_name WHERE product_type = 'Accessories' ORDER BY mix_design ASC";
$mixResult = execsql($mixQuery);
$mixNum = mysql_num_rows($mixResult);
	$w = 0;
	$arrId4 = array ();
	$arrName4 = array ();
	while ($w < $mixNum) {
		$arrId4[$w] = mysql_result($mixResult, $w, 'mix_design');
		$arrName4[$w] = mysql_result($mixResult, $w, 'mix_design');
		$w++;
	}
 
$table_name = $_SESSION['db_prefix']."_production_plan";
if ($product=='All') {	 	
$display_query = "SELECT production_no, production_date, mix_design_name, mix_design_cost  FROM $table_name WHERE production_date >= '".$start."' AND production_date <= '".$end."' AND depalleted = 'yes'  AND sub_category = 'Accessories'" ;
}
else {
$display_query = "SELECT production_no, production_date, mix_design_name, mix_design_cost  FROM $table_name WHERE mix_design_name = '".$product."' AND production_date >= '".$start."' AND production_date <= '".$end."' AND depalleted = 'yes'  AND sub_category = 'Accessories'" ;
}
$display_result = execsql($display_query);
$i = 0;
$display_num = mysql_num_rows($display_result);

$table_name = $_SESSION['db_prefix']."_presets";
$production_margin_query = "SELECT preset_value FROM $table_name WHERE preset_name = 'production_margin'  ";
$production_margin_result = mySQL_query($production_margin_query);
$production_margin = mysql_result($production_margin_result, 0, "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 accessories</label></div>
   
<br />
<br /><br />
<br />
<?php
$page='costing_accessories_list.php';
$reference_name='product';
$reference=$product;
include('../include/period_select_block.php');
?>


    
<br />
<div class="page_middle_frame">
<div style="float:left;">
Select single product:
<form action="costing_tiles_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_accessories_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_accessories_list.php?product=<?php echo $arrId4[$j] ?>&date=1&date1=<?php echo $start ?>&date2=<?php echo $end ?>"><?php echo $arrName4[$j] ?></option>	
 <?php }
     ?>
</select>
</div>
<div style="float:left;">
<img src="../images/info_blue.png" width="16" height="16">
<label class="label_info">List only shows production cycles which are completely depalleted.</label>          
</div>
</form>
<!--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": [[ 1, "desc" ]],
					"aoColumns": [
								{"bSortable": true},
								{"iDataSort": 2},
								{"bVisible": false},
                              	{"bSortable": true},
								{"bSortable": true},
								{"bSortable": true},
								{"bSortable": true},
								{"bSortable": true},
								{"bSortable": true},
								{"bSortable": true},
								{"bSortable": true}
                         	 ]
				} );
			} );
		</script>    	
<table width="722" id="scroll_table" class="data_display" >
	<thead>
		<tr>
		  <th width="86" align="left">Production no</th>
		  <th width="44" align="left">Date</th>
		  <th width="23" align="left">&nbsp;</th>
		  <th width="115" align="left">Mix design</th>
		  <th width="41" align="left">Skips</th>
		  <th width="45" align="left">Skips Cost</th>
		  <th width="76" align="left">Total cost production</th>
		  <th width="76" align="left">Depalleted</th>
		  <th width="60" align="left">Cost per product</th>
		  <th width="60" align="left">Est. cost per product</th>
		  <th width="48" align="left">Diff %</th>
        </tr>
	</thead>
	<tbody>
  <?php
  
    $i=0;
    while($i < $display_num){
		 $b0 = mysql_result($display_result, $i, "production_no");
		 $b1 = mysql_result($display_result, $i, "production_date");
		 $b2 = mysql_result($display_result, $i, "mix_design_name");
		 $b3 = mysql_result($display_result, $i, "mix_design_cost");
		 
		$b4=0;
		$table_name = $_SESSION['db_prefix']."_production_plan_lines";
		$skip_query = "SELECT quantity FROM $table_name WHERE production_no = '".$b0."' AND  action = 'skip_count' ";
		$skip_result = mySQL_query($skip_query);
		$skip_num = mysql_num_rows($skip_result);
		$k=0;
		while ($k<$skip_num) {
		   $b4a = mysql_result($skip_result, $k, "quantity");
		   $b4 = $b4 +$b4a;
		   $k++;
		}
		
		$b5=0;
		$table_name = $_SESSION['db_prefix']."_production_plan_lines";
		$dep_count_query = "SELECT quantity FROM $table_name WHERE production_no = '".$b0."' AND  action = 'dep_count' ";
		$dep_count_result = mySQL_query($dep_count_query);
		$dep_count_num = mysql_num_rows($dep_count_result);
		$k=0;
		while ($k<$dep_count_num) {
		   $b5a = mysql_result($dep_count_result, $k, "quantity");
		   $b5 = $b5 +$b5a;
		   $k++;
		}
		
		$b6 = $b3 * $b4;
		
		if ($b5!=0) {
		$b7 = $b6/$b5;
		} 
		else {
			$b7=0;
		}
		
		$table_name = $_SESSION['db_prefix']."_mix_design_lines";
		$line_query = "SELECT store_item_id, quantity FROM $table_name WHERE mix_design = '".$b2."' ";
		$line_result = mySQL_query($line_query);
		$line_num = mysql_num_rows($line_result);
		$tip_line = '';
		$design_cost = ''; 
		$k=0;
		while ($k<$line_num) {
	  	$store_item_id = mysql_result($line_result, $k, "store_item_id");
	  			$table_name = $_SESSION['db_prefix']."_store_items";
				$name_query = "SELECT item_name, unit, price FROM $table_name WHERE store_item_id = '".$store_item_id."'  ";
				$name_result = mySQL_query($name_query);
				$item_name = mysql_result($name_result, 0, "item_name");
				$unit = mysql_result($name_result, 0, "unit");
				$price = mysql_result($name_result, 0, "price");
	  	$quantity = mysql_result($line_result, $k, "quantity");
	  	$cost = $quantity * $price;
	  	$design_cost = $design_cost + $cost;
		$k++;
		}
		
		$table_name = $_SESSION['db_prefix']."_mix_design";
		$est_qty_query = "SELECT est_qty FROM $table_name WHERE mix_design = '".$b2."' ";
		$est_qty_result = mySQL_query($est_qty_query);
		$est_qty = mysql_result($est_qty_result, 0, "est_qty");
		$b10 = $design_cost / $est_qty;
		
		$b11 = (($b7/$b10)*100)-100;

		?>
		<tr >
		  <td ><a href="production_plan_report.php?production_no=<?php echo $b0 ?>" onMouseover="ddrivetip('Click to view production report', 150)"; onMouseout="hideddrivetip()"><?php echo $b0 ?></a></td>
		  <td ><?php echo date("d-m-Y", strtotime($b1 ))?></td>
		  <td ><?php echo $b1 ?>&nbsp;</td>
		  <td align="left"><?php echo $b2 ?></td>
		  <td align="right"><?php echo number_format($b4, 1, '.', ',') ?></td>
		  <td align="right"><?php echo number_format($b3, 2, '.', ',') ?></td>
		  <td align="right"><?php echo number_format($b6, 2, '.', ',') ?></td>
		  <td align="right"><?php echo $b5 ?></td>
		  <td align="right"><?php echo number_format($b7, 4, '.', ',') ?></td>
		  <td align="right"><?php echo number_format($b10, 4, '.', ',') ?></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++; 
	}
	?>
   
</table>
		
	
</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'); ?>