<!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");
	
$design_no = $_GET['design_no'];	

$table_name = $_SESSION['db_prefix']."_packaging_design";
$packaging_query = "SELECT packaging_design, pack_qty, est_qty, product_item_id FROM $table_name WHERE design_no = '".$design_no."' ";
$packaging_result = $con->query($packaging_query);
$row = mysqli_fetch_assoc($packaging_result);
$packaging_design = $row['packaging_design'];
$pack_qty = $row['pack_qty'];
$est_qty = $row['est_qty'];
$product_item_id = $row['product_item_id'];


$table_name = $_SESSION['db_prefix']."_packaging_design_lines";
$mix_line_query = "SELECT store_item_id, quantity, used_for FROM $table_name WHERE design_no = '".$design_no."' ";
$mix_line_result = $con->query($mix_line_query);
$mix_line_num = mysqli_num_rows($mix_line_result);

$table_name = $_SESSION['db_prefix']."_product_store";
$mix_design_query = "SELECT mix_design_id FROM $table_name WHERE product_item_id = '".$product_item_id."' ";
$mix_design_result = $con->query($mix_design_query);
$row = mysqli_fetch_assoc($mix_design_result);
$mix_design_id = $row['mix_design_id'];

$table_name = $_SESSION['db_prefix']."_product_store";
$product_name_query = "SELECT item_name FROM $table_name WHERE mix_design_id = '".$mix_design_id."' AND subCategory = 'Production' ";
$product_name_result = $con->query($product_name_query);
$row = mysqli_fetch_assoc($product_name_result);
$product_name = $row['item_name'];



?>
<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">Packaging design <?php echo $packaging_design ?></label></td>
            </tr>
        </table>
      </div>
       

<div class="page_middle_frame">
  <table id="table_details_display">
    <tr>
      <td width="269">Ingredient</td>
      <td width="61">Used for</td>
      <td width="71">Quantity</td>
      <td width="61">Design Qty</td>
      <td width="52">Unit</td>
      <td width="65">Unit cost</td>
      <td width="89">Packaging cost</td>
    </tr>
    <?php
$total=0;
while($row = $mix_line_result->fetch_assoc()) {
		$store_item_id = $row["store_item_id"];
		$quantity = $row["quantity"];
		$used_for = $row["used_for"];

		$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 = $con->query($name_query);
		$row = mysqli_fetch_assoc($name_result);
		$item_name = $row["item_name"];
		$unit = $row["unit"];
		$price = $row["price"];
		
		if ($used_for=='block') {
		$design_qty = $quantity * $est_qty;
		}
		else {
		$design_qty = $quantity;
		}
		
		$line_total = $design_qty * $price;
		$total = $total + $line_total;
		?>
    <tr>
      <td><?php echo $item_name ?></td>
      <td><?php echo $used_for ?></td>
      <td align="right"><?php echo $quantity ?></td>
      <td align="right"><?php echo $design_qty ?></td>
      <td><?php echo $unit ?></td>
      <td align="right"><?php echo number_format($price, 2, '.', ',') ?></td>
      <td align="right"><?php echo number_format($line_total, 2, '.', ',') ?></td>
    </tr>
    <?php
}
?>
    <tr>
      <td colspan="6" align="right">Total packaging design cost:&nbsp;&nbsp;</td>
      <td align="right"><?php echo number_format($total, 2, '.', ',') ?></td>
    </tr>
  </table>
  <br />
    Product to be packed with this packing design:&nbsp;&nbsp; <?php echo $product_name ?>
  <br /><br />
  Units to be packed with this packing design:&nbsp;&nbsp;&nbsp;<?php echo $pack_qty ?>
    <br /><br />
    Estimated number of products to be packed with this packaging design: <?php echo $est_qty ?>
    <br />
    <br />
    
    Estimated packing cost per product:&nbsp;&nbsp;&nbsp; <?php $single_cost=$total/$est_qty; echo number_format($single_cost, 2, '.', ','); ?>
    <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>
