<!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-main";
include ("../navigation/access.php");
?>

<link href="../css/main.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="../jscript/jquery.1.4.2.js"></script>
</head>
<?php
if ($permission_num==1) {
	include ("../navigation/production_sidebar.php");
	
$production_no = $_GET['production_no'];	

$table_name = $_SESSION['db_prefix']."_production_plan";
$plan_query = "SELECT mix_design_name, depalleted, mix_design_cost FROM $table_name WHERE production_no = '".$production_no."'  ";
$plan_result = mySQL_query($plan_query);
$mix_design_name = mysql_result($plan_result, 0, "mix_design_name");
$depalleted = mysql_result($plan_result, 0, "depalleted");
$mix_design_cost = mysql_result($plan_result, 0, "mix_design_cost");


$table_name = $_SESSION['db_prefix']."_mix_design";
$design_qty_query = "SELECT est_qty FROM $table_name WHERE mix_design = '".$mix_design_name."' ";
$design_qty_result = mySQL_query($design_qty_query);
$design_qty = mysql_result($design_qty_result, 0, "est_qty");


$table_name = $_SESSION['db_prefix']."_production_plan_lines";
$estimated_query = "SELECT quantity, action_date FROM $table_name WHERE production_no = '".$production_no."' AND action = 'estimated' ";
$estimated_result = mySQL_query($estimated_query);
$estimated = mysql_result($estimated_result, 0, "quantity");
$action_date = mysql_result($estimated_result, 0, "action_date");

$table_name = $_SESSION['db_prefix']."_production_plan_lines";
$skip_query = "SELECT quantity, action_date FROM $table_name WHERE production_no = '".$production_no."' AND action = 'skip' ";
$skip_result = mySQL_query($skip_query);
$skips = mysql_result($skip_result, 0, "quantity");
$planned_cost = $mix_design_cost * $skips;

$table_name = $_SESSION['db_prefix']."_production_plan_lines";
$skip_count_query = "SELECT SUM(quantity) AS quantity, action_date FROM $table_name WHERE production_no = '".$production_no."' AND action = 'skip_count' ";
$skip_count_result = mySQL_query($skip_count_query);
$skip_count_num = mysql_num_rows($skip_count_result);


$table_name = $_SESSION['db_prefix']."_production_plan_lines";
$dep_count_query = "SELECT quantity, action_date FROM $table_name WHERE production_no = '".$production_no."' AND action = 'dep_count' ";
$dep_count_result = mySQL_query($dep_count_query);
$dep_count_num = mysql_num_rows($dep_count_result);

$table_name = $_SESSION['db_prefix']."_production_plan_lines";
$wet_count_query = "SELECT SUM(quantity) AS quantity, action_date FROM $table_name WHERE production_no = '".$production_no."' AND action = 'wet_count' ";
$wet_count_result = mySQL_query($wet_count_query);
$wet_count_num = mysql_num_rows($wet_count_result);

$table_name = $_SESSION['db_prefix']."_presets";
$margin_query = "SELECT preset_value FROM $table_name WHERE preset_name = 'production_margin'  ";
$margin_result = mySQL_query($margin_query);
$production_margin = mysql_result($margin_result, 0, "preset_value");
?>
<body>

<div class="main_frame">
<table width="630" border="0" cellspacing="0" cellpadding="0">
    <tr>
      <td height="52" colspan="4" align="left" valign="top"><div class="page_head_frame">
        <table width="630" border="0" cellspacing="0" cellpadding="0">
          <tr>
            <td><label class="label_page_head">Production report <?php echo $mix_design_name ?></label></td>
            </tr>
        </table>
      </div>
       

<div class="page_middle_frame">
Production plan: <?php echo $production_no ?>&nbsp;&nbsp;<?php echo $mix_design_name ?><br />
<br />
<img src="../images/info_blue.png" width="16" height="16" /><?php if ($depalleted=='yes') { echo 'This production is completed'; } else { echo 'This production is not completed'; } ?>
<table width="631" id="dataTable">
  <tr>
      <td width="122" align="center">&nbsp;</td>
      <td width="66" align="center">Date</td>
      <td width="46" align="center">Skips</td>
      <td width="76" align="center">Estimated</td>
      <td width="96" align="center">Quantity</td>
      <td width="101" align="center">Cost</td>
      <td width="92" align="center">Cost/Unit</td>
      </tr>
    <tr>
      <td>Planned</td>
      <td><?php echo date("d-m-Y", strtotime($action_date)) ?></td>
      <td align="right"><?php echo number_format($skips, 1, '.', ',') ?></td>
      <td align="right"><?php echo number_format(($skips*$design_qty), 0, '.', ',') ?></td>
      <td align="right"><?php echo number_format($estimated, 0, '.', ',') ?></td>
      <td align="right"><?php echo number_format($planned_cost, 2, '.', ',') ?></td>
      <td align="right"><?php $planned_cost_unit = $planned_cost / $estimated; echo number_format($planned_cost_unit, 4, '.', ',') ?></td>
      </tr>
    <?php
	$i=0;
	while ($i<$wet_count_num) {
		$wet_count = mysql_result($wet_count_result, $i, "quantity");
		$wet_action_date = mysql_result($wet_count_result, $i, "action_date");
		$skip_count = mysql_result($skip_count_result, $i, "quantity");
	?>
    <tr>
      <td>Production</td>
      <td><?php echo date("d-m-Y", strtotime($wet_action_date)) ?></td>
      <td align="right"><?php echo number_format($skip_count, 1, '.', ',') ?></td>
      <td align="right"><?php echo number_format(($skip_count*$design_qty), 0, '.', ',') ?></td>
      <td align="right"><?php echo number_format($wet_count, 0, '.', ',') ?></td>
      <td align="right"><?php echo number_format(($skip_count*$mix_design_cost), 2, '.', ',') ?></td>
      <td align="right"><?php $production_cost_unit = ($skip_count*$mix_design_cost) / $wet_count; echo number_format($production_cost_unit, 4, '.', ',') ?></td>
      </tr>
      <tr>
      <td colspan="7">&nbsp;</td>
      </tr>
    <?php
	$i++;
	}
	?>
    <?php
	$tot_dep_count = 0;
	$i=0;
	while ($i<$dep_count_num) {
		$dep_count = mysql_result($dep_count_result, $i, "quantity");
		$dep_action_date = mysql_result($dep_count_result, $i, "action_date");
		
	?>
    <tr>
      <td>Depalleted</td>
      <td><?php echo date("d-m-Y", strtotime($dep_action_date)) ?></td>
      <td align="right">&nbsp;</td>
      <td align="right">&nbsp;</td>
      <td align="right"><?php $tot_dep_count = $tot_dep_count + $dep_count; echo number_format($dep_count, 0, '.', ',') ?></td>
      <td align="right">&nbsp;</td>
      <td align="right">&nbsp;</td>
      </tr>
    <?php
	$i++;
	}
	?>
    <tr>
      <td>Total Depalleted</td>
      <td>&nbsp;</td>
      <td align="right">&nbsp;</td>
      <td align="right">&nbsp;</td>
      <td align="right"><?php echo number_format($tot_dep_count, 0, '.', ',') ?></td>
      <td align="right"><?php echo number_format(($skip_count*$mix_design_cost), 2, '.', ',') ?></td>
      <td align="right"><?php if ($depalleted=='yes') {$dep_cost_unit = ($skip_count*$mix_design_cost) / $tot_dep_count; echo number_format($dep_cost_unit, 4, '.', ','); } ?></td>
    </tr>
    </table>
    <br />
    <?php
	if ($depalleted=='yes') {
	$margin = (($dep_cost_unit/$planned_cost_unit)*100)-100;
	if ($margin>$production_margin) {
	?>
    <img src="../images/exclamation_mark_blue.png" width="32" height="32" /> The production margin is <?php echo number_format($margin, 2, '.', ',') ?>% and exceeds the set margin of <?php echo number_format($production_margin, 2, '.', ',') ?>%.
    <?php
	}
	else {
	?>
    <img src="../images/check_mark_blue.png" width="32" height="32" /> The production margin falls with <?php echo number_format($margin, 2, '.', ',') ?>%  within the set margin of <?php echo number_format($production_margin, 2, '.', ',') ?>%.
    <?php
	}
	}
	?>

  <br />
  
  <br />
  <br />
  <br />
<br />
<br />
<br />

</div>
    </td>
    </tr>
    </table>
</div>

<?php include ('../navigation/side_bar.php'); ?>

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