<!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-edit";
include ("../navigation/access.php");
require_once ('../include/tc_calendar.php');

$production_no = $_GET['production_no'];

$table_name = $_SESSION['db_prefix']."_production";
$production_query = "SELECT *  FROM $table_name WHERE production_no = '".$production_no ."' ";
$production_result = $con->query($production_query);
$row = mysqli_fetch_assoc($production_result);
$production_date = $row['production_date'];
$product_item_id = $row['product_item_id'];
$skips_no = $row['skips_no'];
$skip_products_no = $row['skip_products_no'];
$produced_no = $row['produced_no'];
$batch_code = $row['batch_code'];
$production_cost = $row['production_cost'];
$edit_date = $row['edit_date'];
$packing_design_no = $row['packing_design_no'];

$actual_no = $produced_no;

$table_name = $_SESSION['db_prefix']."_product_store";
$item_name_query = "SELECT item_name, mix_design_id   FROM $table_name WHERE product_item_id = '".$product_item_id."' ";
$item_name_result = $con->query($item_name_query);
$row = mysqli_fetch_assoc($item_name_result);
$item_name = $row['item_name'];
$mix_design = $row['mix_design_id'];

$table_name = $_SESSION['db_prefix']."_mix_design_lines";
$mix_query = "SELECT store_item_id, quantity FROM $table_name WHERE design_no = '".$mix_design."' ";
$mix_result = $con->query($mix_query);

$table_name = $_SESSION['db_prefix']."_product_store_lines";
$line_id_query = "SELECT product_store_line_id FROM $table_name WHERE batch_code = '".$batch_code."' AND product_item_id = '".$product_item_id."' "; 
$line_id_result = $con->query($line_id_query);
$row = mysqli_fetch_assoc($line_id_result);
$product_store_line_id = $row['product_store_line_id'];

$table_name = $_SESSION['db_prefix']."_product_store_lines";
$packing_no_query = "SELECT packing_no FROM $table_name WHERE production_no = '".$production_no."' AND packing_no <> '0' ";
$packing_no_result = $con->query($packing_no_query);


?>

<link href="../css/main.css" rel="stylesheet" type="text/css" />
<script language="javascript" src="../jscript/calendar.js"></script>
<script language="JavaScript" src="../jscript/cancel_confirmation.js"  type="text/javascript"></script> 
<script language="JavaScript" src="../jscript/alert_message.js"  type="text/javascript"></script>
<script language="JavaScript" src="../jscript/show_hide_layers.js"  type="text/javascript"></script> 
<script language="JavaScript" src="../jscript/go_to_url.js"  type="text/javascript"></script> 
<script language="JavaScript" src="../jscript/gen_validatorv4.js"  type="text/javascript" xml:space="preserve"></script>  



</head>
<?php
if ($permission_num==1) {
	include ("../navigation/production_sidebar.php");
?>
<body>

<div class="main_frame">
<div class="page_head_frame">
        <label class="label_page_head">Production no <?php echo $production_no ?></label>
</div>

<div class="page_inside_frame">

<table width="600" id="table_display">
  <tr>
    <td>Product:</td>
    <td><?php echo $item_name ?></td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
  </tr>
  <tr>
    <td width="126">Production no:</td>
    <td width="169"><?php echo $production_no ?></td>
    <td width="116">Batch no:</td>
    <td width="169"><?php echo $batch_code ?></td>
  </tr>
  <tr>
    <td>Production date:</td>
    <td><?php echo date("d-m-Y", strtotime($production_date)) ?></td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
  </tr>
  <tr>
    <td>No of batches:</td>
    <td><?php echo $skips_no ?></td>
    <td>Est. no packed products:</td>
    <td><?php echo $skip_products_no ?></td>
  </tr>
  <tr>
    <td>Actual no packed products:</td>
    <td><?php echo $actual_no ?></td>
    <td>Production cost:</td>
    <td><?php echo number_format($production_cost, 2, '.', ',') ?></td>
  </tr>
  <tr>
    <td>Total packed products:</td>
    <td><?php echo $produced_no ?></td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
  </tr>
</table>
<br />
<?php if ($edit_date>'2018-01-01') { ?>
<img src="../images/exclamation_mark_red_small.png" width="10" height="16" /><font color="#FF0000">&nbsp;&nbsp;This production was edited on <?php echo date("d-m-Y, H:i", strtotime($edit_date)); ?>.</font>
<?php } ?>
<?php if ($packing_design_no>0) { ?>
<img src="../images/info_blue.png" width="16" height="16" /> This production has been packed on the following packing no's:
<?php
while($row = $packing_no_result->fetch_assoc()) {
$packing_no = $row["packing_no"];
?>
<a href="packing_slip.php?packing_no=<?php echo $packing_no ?>"><?php echo $packing_no ?></a>&nbsp;&nbsp;&nbsp;
<?php
}
 } ?>
<br />
<br />
<br />
Production packing breakdown:<br />
<table width="600" id="table_display">
  <tr>
    <td>Item</td>
    <td>Usage</td>
    <td>Cost</td>
    <td>Usage for <?php echo $skips_no ?> batch</td>
    <td>Costs for <?php echo $skips_no ?> batch</td>
  </tr>
  <?php
  $total_cost = 0;
  while($row = $mix_result->fetch_assoc()) {
			$mix_store_item_id = $row["store_item_id"];
			$quantity = $row["quantity"];
			
			$table_name = $_SESSION['db_prefix']."_store_items";
			$item_query = "SELECT item_name, unit, price FROM $table_name WHERE store_item_id = '".$mix_store_item_id."' ";
			$item_result = $con->query($item_query);
			$row = mysqli_fetch_assoc($item_result);
			$item_name = $row['item_name'];
			$unit = $row['unit'];
			
			$table_name = $_SESSION['db_prefix']."_production_mix_lines";
			$mix_line_query = "SELECT item_quantity, item_price FROM $table_name WHERE production_no = '".$production_no."' AND store_item_id = '".$mix_store_item_id."' ";
			$mix_line_result = $con->query($mix_line_query);
			$row = mysqli_fetch_assoc($mix_line_result);
			$item_quantity = $row['item_quantity'];
			$item_price = $row['item_price'];
			
			
			$skip_usage = $item_quantity * $skips_no;
			$skip_cost = $skip_usage * $item_price;
			
			$total_cost = $total_cost + $skip_cost;

?>
  <tr>
    <td width="84"><?php echo $item_name ?></td>
    <td width="116"><?php echo $item_quantity.' '.$unit ?> /batch</td>
    <td width="99"><?php echo $item_price.' / '.$unit ?></td>
    <td width="123" align="right"><?php echo number_format($skip_usage, 2, '.', ',') ?></td>
    <td width="154" align="right"><?php echo number_format($skip_cost, 2, '.', ',') ?></td>
  </tr>
<?php
} 
?>
  <tr>
    <td colspan="4" align="right">Total production cost:</td>
    <td align="right"><?php echo number_format($total_cost, 2, '.', ',') ?></td>
  </tr>
  <tr>
    <td colspan="5" align="left">&nbsp;</td>
  </tr>
  <tr>
    <td colspan="5" align="left"><?php $unit_cost=$total_cost/$actual_no ?> Production cost per produced product: <?php echo number_format($unit_cost, 2, '.', ','); ?>&nbsp;&nbsp;&nbsp; (<?php echo $actual_no ?> products)</td>
    </tr>
</table>
<br />
<br />
<br />

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

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