<!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");

if ($permission_num==1) {
	include ("../navigation/production_sidebar.php");

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


?>
<!--style sheets-->
<link rel="stylesheet" type="text/css" href="../css/tooltip.css" />
<link rel="stylesheet" type="text/css" href="../css/scroll_table.css" />

<!--jscript-->
<script language="JavaScript" src="../jscript/tooltip.js"  type="text/javascript"></script> 
<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": "450px",
					"bPaginate": false,
					"bScrollCollapse": true,
					"aaSorting": [[ 0, "asc" ]],
					"aoColumns": [
								{"iDataSort": 5},
								{"bSortable": true},
								{"bSortable": true},
								{"bSortable": true},
                              	{"bSortable": true},
                             	{"bVisible": false}
								
								 ]
				} );
			} );
		</script>

</head>

<!--jscript-->
<script language="JavaScript" src="../jscript/tooltip.js"  type="text/javascript"></script> 

<body>

<div class="main_frame"  >
  <div class="page_head_frame" >
          <table width="590" border="0" cellspacing="0" cellpadding="0">
            <tr>
              <td width="385"><label class="label_page_head">Packaging designs</label></td>
              <td width="253">&nbsp;</td>
              <td width="171" align="right">&nbsp;</td>
            </tr>
          </table>
       </div></td>
             
        <div class="page_middle_frame" style="height:500px;">
          <table width="618" id="scroll_table" class="data_display"   >
            <thead>
            <tr>
              <th align="center">Packaging design</th>
              <th align="center">Product</th>
              <th align="center">Estimated<br />
                no of products</th>
              <th align="center">Design cost</th>
              <th align="center">Estimated                single<br />
product cost</th>
              <th align="center">&nbsp;</th>
            </tr>
          </thead>
            <?php

while($row = $packaging_result->fetch_assoc()) {
	$a0 = $row["design_no"];
	$a1 = $row["packaging_design"];
	$est_qty = $row["est_qty"];
	$product_item_id = $row["product_item_id"];
	
	$table_name = $_SESSION['db_prefix']."_packaging_design_lines";
	$line_query = "SELECT store_item_id, quantity, used_for FROM $table_name WHERE design_no = '".$a0."' ";
	$line_result = $con->query($line_query);
	$tip_line = '';
	$design_cost = ''; 
	while($row = $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"];
				
	  	$table_name = $_SESSION['db_prefix']."_product_store";
		$product_name_query = "SELECT item_name FROM $table_name WHERE product_item_id = '".$product_item_id."' ";
		$product_name_result = $con->query($product_name_query);
		$row = mysqli_fetch_assoc($product_name_result);
		$product_name = $row['item_name'];
		
	  
	  if ($used_for=='block') {
	  		$cost = $est_qty * $quantity * $price;
			$design_qty = $est_qty * $quantity;
	  }
	  else {
		    $cost = $quantity * $price;
			$design_qty = $quantity;
	  }
		  
	  $design_cost = $design_cost + $cost;
	  
	 		
		$line_total = $est_qty * $price;
		//$total = $total + $line_total;
	  
	  $tip_line = $tip_line.'<tr><td>'.$item_name.'</td><td>'.$design_qty.' '.$unit.'</td></tr>';
	
	}
	
	$tip_head = '<table><tr><td width=300px;>item</td><td width=100px;>quantity</td></tr>';
	$tip_foot = '</table><br>Click for details.';
	$packaging_tool_tip = $tip_head.$tip_line.$tip_foot;
    ?> 
           <tr>
              <td width="163" align="left" valign="top"><a href="packaging_design_display.php?design_no=<?php echo $a0 ?>" onmouseover="ddrivetip('<?php echo $packaging_tool_tip ?>', 300)"; onmouseout="hideddrivetip()"><?php echo $a1 ?></a></td>
              <td width="131" align="left" valign="top"><?php echo $product_name ?></td>
              <td width="91" align="center" valign="top"><?php echo number_format($est_qty, 0, '.', ',') ?><br /></td>
              <td width="81" align="right" valign="top"><?php echo number_format($design_cost, 2, '.', ',') ?></td>
              <td width="97" align="right" valign="top"><?php if ($est_qty!=0) { $single_cost=$design_cost/$est_qty; echo number_format($single_cost, 2, '.', ','); }?></td>
              <td width="27" align="left" valign="top"><?php echo $a1 ?></td>
            </tr>
            <?php
}
?>
          </table>
          
        </div>
  
</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");?>
<body onload="accessdenied(); return false">
</body>
<?php }
?>
</html>