<!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');	
 
$table_name = $_SESSION['db_prefix']."_production_plan";	 	
$display_query = "SELECT production_no, production_date, mix_design_name, mix_design_cost  FROM $table_name WHERE production_date BETWEEN '".$start."' AND '".$end."' AND depalleted = 'yes'  AND sub_category = 'Accessories' GROUP BY mix_design_name ORDER BY mix_design_name";
$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 summary accessories</label></div>
   
<br />
<br /><br />
<br />
<?php
$page='costing_accessories_list_summary.php';
$reference_name='';
$reference='';
include('../include/period_select_block.php');
?>


    
<br />
<div class="page_middle_frame">
<div align="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>
<!--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": [[ 8, "asc" ]],
					"aoColumns": [
								{"iDataSort": 8},
                              	{"bSortable": true},
								{"bSortable": true},
								{"bSortable": true},
								{"bSortable": true},
								{"bSortable": true},
								{"bSortable": true},
								{"bSortable": true},
								{"bVisible": false}
                         	 ]
				} );
			} );
		</script>    	
<table width="600" id="scroll_table" class="data_display" >
	<thead>
		<tr>
		  <th width="118" align="left">Mix design</th>
		  <th width="46" align="left">Skips</th>
		  <th width="60" align="left">Skips Cost</th>
		  <th width="76" align="left">Total cost production</th>
		  <th width="76" align="left">Depalleted</th>
		  <th width="54" align="left">Cost per tile</th>
		  <th width="69" align="left">Est. cost per tile</th>
		  <th width="38" align="left">Diff %</th>
		  <th width="23" align="left">&nbsp;</th>
        </tr>
	</thead>
	<tbody>
  <?php
  
    $i=0;
    while($i < $display_num){
		 $b2 = mysql_result($display_result, $i, "mix_design_name");
		 $b3 = mysql_result($display_result, $i, "mix_design_cost");
		
		$table_name = $_SESSION['db_prefix']."_production_plan";
		$production_query = "SELECT production_no, production_date FROM $table_name WHERE mix_design_name = '".$b2."' AND production_date >= '".$start."' AND production_date <= '".$end."'  AND depalleted = 'yes' ORDER BY '".$b2."' ";
		$production_result = mySQL_query($production_query);
		$production_num = mysql_num_rows($production_result);
		
		$b4=0;
		$b5=0;
		$productionTT_line = '';
		$p=0;
		while ($p<$production_num) {
		$production_no = mysql_result($production_result, $p, "production_no"); 
		$production_date = mysql_result($production_result, $p, "production_date");
		
		$table_name = $_SESSION['db_prefix']."_production_plan_lines";
		$skip_query = "SELECT quantity  FROM $table_name WHERE production_no = '".$production_no."' AND action = 'skip_count' ";
		$skip_result = mySQL_query($skip_query);
		$skip_num = mysql_num_rows($skip_result);
		if ($skip_num>0) {
		$skip = mysql_result($skip_result, 0, "quantity");
		}
		else {
		$skip=0;
		}
		$b4 = $b4 + $skip;
		
		$table_name = $_SESSION['db_prefix']."_production_plan_lines";
		$depallet_query = "SELECT quantity  FROM $table_name WHERE production_no = '".$production_no."' AND action = 'dep_count' ";
		$depallet_result = mySQL_query($depallet_query);
		$depallet_num = mysql_num_rows($depallet_result);
		if ($depallet_num>0) {
		$depallet = mysql_result($depallet_result, 0, "quantity");
		}
		else {
		$depallet=0;
		}
		$b5 = $b5 + $depallet;
		
		 
		$productionTT_line = $productionTT_line.'<tr><td width=200px;>'.$production_no.'</td><td width=100px;>'.date("d-m-Y", strtotime($production_date)).'</td></tr>';
		$p++;
		}
		
		$productionTT = 'Productions in this summary:<br><table>'.$productionTT_line.'</table><br><b>Click for detailed summary.</b>';
		 
						
		$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 align="left"><a href="costing_accessories_list.php?product=<?php echo $b2 ?>&date=1&date1=<?php echo $start ?>&date2=<?php echo $end ?>" onMouseover="ddrivetip('<?php echo $productionTT ?>', 150)"; onMouseout="hideddrivetip()"><?php echo $b2 ?></a></td>
		  <td align="right"><?php echo $b4 ?></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>
		  <td align="right"><?php echo $b2 ?></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'); ?>