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

$packing_no = $_GET['packing_no'];

$table_name = $_SESSION['db_prefix']."_product_store_lines";
$pack_query_query = "SELECT production_no, quantity_in  FROM $table_name WHERE packing_no = '".$packing_no."' ";
$pack_query_result = $con->query($pack_query_query);
$row = mysqli_fetch_assoc($pack_query_result);
$production_no = $row['production_no'];
$packed_no = $row['quantity_in'];


$table_name = $_SESSION['db_prefix']."_production";
$production_query = "SELECT *  FROM $table_name WHERE production_no = '".$production_no ."' AND packing_no = '".$packing_no."' ";
$production_result = $con->query($production_query);
$row = mysqli_fetch_assoc($production_result);
$production_date = $row['production_date'];
$packing_design_no = $row['packing_design_no'];
$packing_date = $row['packing_date'];

$product_item_id = $row['product_item_id'];
$skips_no = $row['skips_no'];
$damaged_no = $row['damaged_no'];
$produced_no = $row['produced_no'];
$batch_code = $row['batch_code'];
$packing_cost = $row['packing_cost'];
$packed_batches = $row['packed_batches'];
$edit_date = $row['packing_edit_date'];

$table_name = $_SESSION['db_prefix']."_packaging_design";
$packaging_design_query = "SELECT packaging_design, est_qty FROM $table_name WHERE design_no = '".$packing_design_no."' ";
$packaging_design_result = $con->query($packaging_design_query);
$row = mysqli_fetch_assoc($packaging_design_result);
$packaging_design = $row['packaging_design'];
$est_qty = $row['est_qty']; 

$table_name = $_SESSION['db_prefix']."_product_store";
$sales_item_name_query = "SELECT product_item_id, item_name  FROM $table_name WHERE packing_design_no = '".$packing_design_no."' ";
$sales_item_name_result = $con->query($sales_item_name_query);
$row = mysqli_fetch_assoc($sales_item_name_result);
$sales_item_name = $row['item_name'];
$sales_product_item_id = $row['product_item_id'];


$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']."_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']."_packaging_design_lines";
$packing_query = "SELECT store_item_id, quantity FROM $table_name WHERE design_no = '".$packing_design_no."' ";
$packing_result = $con->query($packing_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 = '".$sales_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'];

?>

<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">Packing no <?php echo $packing_no ?></label>
</div>

<div class="page_inside_frame">

<table width="673" id="table_display">
  <tr>
    <td>Sales product:</td>
    <td><?php echo $sales_item_name ?></td>
    <td>Packaging:</td>
    <td><?php echo $packaging_design ?> </td>
  </tr>
  <tr>
    <td>Production product:</td>
    <td><?php echo $item_name ?></td>
    <td>Packing cost:</td>
    <td><?php echo number_format($packing_cost, 2, '.', ',') ?></td>
  </tr>
  <tr>
    <td width="144">Production no:</td>
    <td width="180"><?php echo $production_no ?></td>
    <td width="122">Batch no:</td>
    <td width="207"><?php echo $batch_code ?></td>
  </tr>
  <tr>
    <td>Packing date:</td>
    <td><?php echo date("d-m-Y", strtotime($packing_date)) ?></td>
    <td>Production date:</td>
    <td><?php echo date("d-m-Y", strtotime($production_date)) ?></td>
  </tr>
  <tr>
    <td>Packed products: </td>
    <td><?php echo $packed_no ?></td>
    <td>Packings:</td>
    <td><?php echo $packed_batches ?></td>
  </tr>
  <tr>
    <td>Recycled products:</td>
    <td><?php echo $damaged_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 packing was edited on <?php echo date("d-m-Y, H:i", strtotime($edit_date)); ?>.</font><br />
<?php } ?>
<img src="../images/info_blue.png" width="24" height="24" /> Click <a href="production_slip.php?production_no=<?php echo $production_no ?>"><b>here</b></a> for production details.
<br />
<br />
<br />
<?php echo $packaging_design ?> packing breakdown:<br />
<table width="673" id="table_display">
  <tr>
    <td>Item</td>
    <td>Usage</td>
    <td>Cost</td>
    <td align="center">Usage for <?php echo $packed_no ?> products<br />in <?php echo $packed_batches ?> packings</td>
    <td align="center">Costs for <?php echo $packed_no ?> products<br />
      in <?php echo $packed_batches ?> packings</td>
  </tr>
  <?php
  $total_cost = 0;
  while($row = $packing_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_packaging_lines";
			$mix_line_query = "SELECT item_quantity, item_price, used_for FROM $table_name WHERE production_no = '".$production_no."' AND packing_no = '".$packing_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'];
			
			$pack_usage = $item_quantity;
			$pack_cost = $item_quantity * $item_price;
			$total_cost = $total_cost + $pack_cost;

?>
  <tr>
    <td width="124"><?php echo $item_name ?></td>
    <td width="111"><?php echo number_format($item_quantity, 4, '.', ',').' '.$unit ?></td>
    <td width="61"><?php echo $item_price.' / '.$unit ?></td>
    <td width="146" align="right"><?php echo number_format($pack_usage, 4, '.', ',') ?></td>
    <td width="134" align="right"><?php echo number_format($pack_cost, 2, '.', ',') ?></td>
  </tr>
<?php
} 
?>
  <tr>
    <td colspan="4" align="right">Total packing 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 $actual_unit_cost=$total_cost/$packed_no ?> Packing cost per packed product: <?php echo number_format($actual_unit_cost, 4, '.', ','); ?> </td>
    </tr>
</table>
<br />
<br />
<br />
<div style="margin-top:10px; margin-left:25px;">
<form action="../store/stock_batch_no_print.php" method="get" target="_blank" >
<input name="product_store_line_id_text" type="hidden" value="<?php echo $product_store_line_id ?>" />
<input name="print_button" type="submit" class="imagebutton" value="Print store label" />
<input type="checkbox" name="stock_quantity_checkbox" id="stock_quantity_checkbox" />
print current stock quantity
</form>
</div>


</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>
