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

<link href="../css/main.css" rel="stylesheet" type="text/css" />
<?php
include ("../navigation/header.php");
$page_name = "Sales-main";
include ("../navigation/access.php");

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

$job_no = $_GET['job_no'];
$quote='no';
?>
</head>
<?php

$table_name = $_SESSION['db_prefix']."_captured_jobs";
$job_query = "SELECT * FROM $table_name WHERE job_no = '".$job_no."' ";
$job_result = mySQL_query($job_query);
$job_no = mysql_result($job_result, 0, "job_no");
$cust_id = mysql_result($job_result, 0, "cust_id");
$designer = mysql_result($job_result, 0, "designer");
$roof_cover = mysql_result($job_result, 0, "roof_cover");
$roof_pitch = mysql_result($job_result, 0, "roof_pitch");
$max_overhang = mysql_result($job_result, 0, "max_overhang");
$roof_area = mysql_result($job_result, 0, "roof_area");
$floor_area = mysql_result($job_result, 0, "floor_area");

	
$table_name2 = $_SESSION['db_prefix']."_customer";
$customer_query = "SELECT * FROM $table_name2 WHERE cust_id ='".$cust_id."' ";
$customer_result = mySQL_query($customer_query);
$customer_num = mysql_num_rows($customer_result);	
$a1 = mysql_result($customer_result, 0, "account_type");
if ($a1=="corporate") { 
$a0 = mysql_result($customer_result, 0, "comp_name"); 
} 
else {
$a0 = mysql_result($customer_result, 0, "title")." ".mysql_result($customer_result, 0, "name_2")." ".mysql_result($customer_result, 0, "comp_name");
 }
$a5 = mysql_result($customer_result, 0, "contact_1");
$a6 = mysql_result($customer_result, 0, "status");
$a7 = mysql_result($customer_result, 0, "bag_box");
$a8 = mysql_result($customer_result, 0, "box_no");
$a9 = mysql_result($customer_result, 0, "postal_place");
$a10 = mysql_result($customer_result, 0, "phys_address");
$a11 = mysql_result($customer_result, 0, "phys_place");
$a12 = mysql_result($customer_result, 0, "web_site");
$a13 = mysql_result($customer_result, 0, "vat_no");
$a15 = mysql_result($customer_result, 0, "email_2");
$a23 = mysql_result($customer_result, 0, "fax");
$a24 = mysql_result($customer_result, 0, "phone");
$a26 = mysql_result($customer_result, 0, "country");
$a29 = mysql_result($customer_result, 0, "email_1");
$a30 = mysql_result($customer_result, 0, "cell_1");
$a32 = mysql_result($customer_result, 0, "cell_2");
$a35 = mysql_result($customer_result, 0, "contact_2");
$a36 = mysql_result($customer_result, 0, "notes");

$table_name = $_SESSION['db_prefix']."_cutting_bill";
$cutting_query = "SELECT * FROM $table_name WHERE job_no = '".$job_no."' ";
$cutting_result = mySQL_query($cutting_query);
$cutting_num = mysql_num_rows($cutting_result);
if ($cutting_num!=0) {
		$bill_id = mysql_result($cutting_result, 0, "bill_id");
}
else {
	$bill_id='';
}

$table_name = $_SESSION['db_prefix']."_cutting_bill_lines";
$truss_query = "SELECT * FROM $table_name WHERE type = 'Truss' AND bill_id = '".$bill_id."' ";
$truss_result = mySQL_query($truss_query);
$truss_num = mysql_num_rows($truss_result);

$table_name = $_SESSION['db_prefix']."_captured_timber";
$timber_query = "SELECT * FROM $table_name WHERE cap_timber_group = 'Trusses' AND cap_job = '".$job_no."' AND cap_class = 'S5'  ";
$timber_result = mySQL_query($timber_query);
$timber_num = mysql_num_rows($timber_result);

$table_name = $_SESSION['db_prefix']."_captured_timber";
$timber2_query = "SELECT * FROM $table_name WHERE cap_timber_group = 'Trusses' and cap_job = '".$job_no."' AND cap_class = 'S7' ";
$timber2_result = mySQL_query($timber2_query);
$timber2_num = mysql_num_rows($timber2_result);

$table_name3 = $_SESSION['db_prefix']."_captured_mitek";
$mitek_query = "SELECT * FROM $table_name3 WHERE cap_job = '".$job_no."' AND ( (cap_quantity > 0) OR (auth_quantity > 0) )";
$mitek_result = mySQL_query($mitek_query);
$mitek_num = mysql_num_rows($mitek_result);


$table_name4 = $_SESSION['db_prefix']."_captured_sundries";
$sundries_query = "SELECT * FROM $table_name4 WHERE cap_job = '".$job_no."' AND ( (cap_quantity > 0) OR (auth_quantity > 0) )";
$sundries_result = mySQL_query($sundries_query);
$sundries_num = mysql_num_rows($sundries_result);

$table_name = $_SESSION['db_prefix']."_captured_timber";
$timber3_query = "SELECT * FROM $table_name WHERE cap_timber_group = 'Bracing' and cap_job = '".$job_no."' AND ( (cap_timber_length > 0) OR (auth_timber_length > 0) )";
$timber3_result = mySQL_query($timber3_query);
$timber3_num = mysql_num_rows($timber3_result);

$table_name = $_SESSION['db_prefix']."_captured_timber";
$timber4_query = "SELECT * FROM $table_name WHERE cap_timber_group = 'Sundries' and cap_job = '".$job_no."' AND ( (cap_timber_length > 0) OR (auth_timber_length > 0) )";
$timber4_result = mySQL_query($timber4_query);
$timber4_num = mysql_num_rows($timber4_result);

$table_name6 = $_SESSION['db_prefix']."_captured_extra";
$extra_query = "SELECT * FROM $table_name6 WHERE job_no = '".$job_no."' ";
$extra_result = mySQL_query($extra_query);
$extra_num = mysql_num_rows($extra_result);



?>
<script type="text/javascript" src="../jscript/tablesort.js"></script>

<link rel="stylesheet" type="text/css" href="../css/tooltip.css" />
<body>
<?php include ('../include/tooltip.php'); ?>

<div class="main_frame" style="height:auto;">
  <table id="tableMain" > <!--begin of tableMain-->
    <tr>
      <td width="830" height="62" valign="top" ><div class="page_head_frame">
        
        <table width="809" border="0" cellspacing="0" cellpadding="0">
          <tr>
            <td width="472"><label class="label_page_head">Job progress:</label>
              &nbsp;&nbsp;&nbsp;<label class="label_page_head_2">&nbsp;&nbsp;&nbsp; <?php echo $a0 ?></label></td>
            <td width="208"><label style="font-size:24px; color:#FB6808;"><?php echo "J".$job_no ?></label></td>
            </tr>
        </table>
      </div>
        <br />

<br /></td> <!--center of tableMain-->
</tr>
<tr>
<td height="93"> <!--center of tableMain-->
<div class="page_middle_frame">
<div align="left">Company details</div>
  <table width="680" id="table_details_display">
    <tr>
      <td width="143" align="left" valign="top">Customer:</td>
      <td colspan="3" align="left" valign="top"><?php echo $a0 ?></td>
    </tr>
    <tr>
      <td align="left" valign="top">Job  no:</td>
      <td width="155" align="left" valign="top"><?php echo "J".$job_no ?></td>
      <td width="187" align="left" valign="top">Designer:</td>
      <td width="175" align="left" valign="top"><?php echo $designer ?>&nbsp;</td>
    </tr>
    </table>
    </div>
    <div class="page_middle_frame">
    Design details
    <table width="680" id="table_details_display">
    <tr>
      <td width="143" align="left" valign="top">Roof cover:</td>
      <td colspan="3" align="left" valign="top"><?php echo $roof_cover ?>&nbsp;</td>
    </tr>
    <tr>
      <td align="left" valign="top">Roof pitch:</td>
      <td width="155" align="left" valign="top"><?php echo $roof_pitch ?>&nbsp;deg</td>
      <td width="187" align="left" valign="top">Max overhang:</td>
      <td width="175" align="left" valign="top"><?php echo $max_overhang ?>&nbsp;mm</td>
    </tr>
    <tr>
      <td align="left" valign="top">Roof area:</td>
      <td align="left" valign="top"><?php echo $roof_area ?>&nbsp;m<sup>2</sup></td>
      <td align="left" valign="top">Floor area:</td>
      <td align="left" valign="top"><?php echo $floor_area ?>&nbsp;m<sup>2</sup></td>
    </tr>
  </table>
</div>
</td>
</tr>
<tr>
<td>
<div class="page_middle_frame">
<div align="left">
  <label class="label_std">Trusses:</label>
</div>
  <table width="676" id="table_details_display">
   <tr>
     <th width="313">Truss type	</th>
     <th width="86">Quantity</th>
     <th width="86">Produced</th>
     <th width="86">Delivered</th>
     <th width="81">Balance</th>
    <?php 
	if ($truss_num!=0) {
	$i=0;
	while ($i<$truss_num) {
		$a1 = mysql_result($truss_result, $i, "truss_type");
		$a2 = mysql_result($truss_result, $i, "quantity");
				
		$table_name = $_SESSION['db_prefix']."_production_bill_lines";
		$produce_query = "SELECT SUM(quantity) AS summed_quantity, SUM(delivered) AS summed_delivered FROM $table_name WHERE job_no = '".$job_no."' AND truss_type = '".$a1."' GROUP BY job_no ";
		$produce_result = mySQL_query($produce_query);
		$produce_num = mysql_num_rows($produce_result);
		if ($produce_num != 0) {
				$a3 = mysql_result($produce_result, 0, "summed_quantity");
				$a4 = mysql_result($produce_result, 0, "summed_delivered");
				$a5 = $a2 - $a4;
		}
		else {
			$a3='';
			$a4='';
			$a5='';
		}
	?>
     <tr>
      <td align="left" valign="top"><?php echo $a1 ?></td>
      <td align="right" valign="top"><?php echo $a2 ?></td>
      <td align="right" valign="top"><?php echo $a3 ?></td>
      <td align="right" valign="top"><?php echo $a4 ?></td>
      <td align="right" valign="top"><?php echo $a5 ?></td>
    </tr>
    <?php
	$i++;
	}
	
    }
    else {
    ?>
    <tr>
    <td colspan="3">
      <label class="label_std">Cutting bill not yet made.</label>
        <label class="label_std">&nbsp;</label>
      <label class="label_std">&nbsp;</label>
    </td>
    <td>
      <label class="label_std">&nbsp;</label>
    </td>
    <td>
    <label class="label_std">&nbsp;</label>
    </td>
    </tr>
    <?php
	}
	?>
  </table>
</div></td>
</tr>
<tr>
<td height="220">
<div class="page_middle_frame">
<table width="676"  border='0'>
  <tr>
      <td width="269">&nbsp;</td>
      <td width="26" align="center">&nbsp;</td>
      <td width="86" align="center">&nbsp;</td>
      <td width="86" align="center">&nbsp;</td>
      <td width="86" align="center">&nbsp;</td>
      <td width="97" align="center">&nbsp;</td>
    </tr>
    </table>
<div align="left"></div>
  <table width="676" id="table_details_display">
  <tr>
      <th><label class="label_std">item</label></th>
      <th align="left"><label class="label_std">unit</label></th>
      <th align="left"><label class="label_std">compiled</label></th>
      <th align="left"><label class="label_std">authorised</label></th>
      <th align="left"><label class="label_std">delivered</label></th>
      <th align="left"><label class="label_std">balance</label></th>
    </tr>
    <tr  style="background-color:#F0F0F0;">
      <th height="22" align="left">eCo products</th>
      <th align="left">&nbsp;</th>
      <th align="left">&nbsp;</th>
      <th align="left">&nbsp;</th>
      <th align="left">&nbsp;</th>
      <th align="left">&nbsp;</th>
    </tr>
    <?php 
	$i=0;
	while ($i<$mitek_num) {
		$cap_item_id = mysql_result($mitek_result, $i, "cap_item_id");
			$table_name4 = $_SESSION['db_prefix']."_store_items";
			$item_query = "SELECT item_name, unit FROM $table_name4 WHERE store_item_id = '".$cap_item_id."' ";
			$item_result = mySQL_query($item_query);
			$item_name = mysql_result($item_result, 0, "item_name");
			$unit = mysql_result($item_result, 0, "unit");
		$cap_quantity = mysql_result($mitek_result, $i, "cap_quantity");
		$auth_quantity = mysql_result($mitek_result, $i, "auth_quantity");
		$delivered = mysql_result($mitek_result, $i, "delivered");
		$balance = $auth_quantity - $delivered;
	?>
	
    
    
    <tr>
      <td width="268"><?php echo $item_name ?>
      </td>
      <td width="41" align="left"><?php echo $unit ?></td>
      <td width="86" align="right"><?php echo $cap_quantity ?></td>
      <td width="86" align="right"><?php echo $auth_quantity ?></td>
      <td width="86" align="right"><?php echo $delivered ?></td>
      <td width="81" align="right"><?php echo $balance ?></td>
      </tr>
    <?php
	$i++;
	}
	?>
    </table>
    </div>
    <div class="page_middle_frame">
	<div align="left">
	  <label class="label_std">Bracing</label>
	</div>
 	 <table width="676" id="table_details_display">
            
             <?php 
			 $j=0;
			 while ($j<$timber3_num) {
				 $bracing_name = mysql_result($timber3_result, $j, "cap_timber_name");
				 $bracing_length = mysql_result($timber3_result, $j, "cap_timber_length");
				 $auth_bracing_length = mysql_result($timber3_result, $j, "auth_timber_length");
				 $delivered = mysql_result($timber3_result, $j, "delivered");
				 $balance = $auth_bracing_length - $delivered;
			 ?>
             <tr>
               <td width="266"><?php echo $bracing_name ?>
               </td>
               <td width="43">&nbsp;&nbsp;m </td>
               <td width="86" align="right" ><?php echo $bracing_length ?></td>
               <td width="86" align="right" ><?php echo $auth_bracing_length ?></td>
               <td width="86" align="right" ><?php echo $delivered ?></td>
               <td width="81" align="right" ><?php echo number_format($balance, 2, '.', ',') ?></td>
             </tr>
             <?php
			 $j++;
			 }
			 ?>
    </table>
    </div>
    <div class="page_middle_frame">
	<div align="left">
	  <label class="label_std">Sundry timbers</label>
	</div>
 	 <table width="676" id="table_details_display">
             <?php 
			 $k=0;
			 while ($k<$timber4_num) {
				 $sundries_name = mysql_result($timber4_result, $k, "cap_timber_name");
				 $sundries_length = mysql_result($timber4_result, $k, "cap_timber_length");
				 $auth_sundries_length = mysql_result($timber4_result, $k, "auth_timber_length");
				 $delivered = mysql_result($timber4_result, $k, "delivered");
				 $balance = $auth_sundries_length - $delivered;
			 ?>
             <tr>
               <td width="268"><?php echo $sundries_name ?>
               </td>
               <td width="41">&nbsp;m</td>
               <td width="86" align="right" ><?php echo $sundries_length ?></td>
               <td width="86" align="right" ><?php echo $auth_sundries_length ?></td>
               <td width="86" align="right" ><?php echo $delivered ?></td>
               <td width="81" align="right" ><?php echo number_format($balance, 2, '.', ',') ?></td>
             </tr>
             <?php
				
			 $k++;
			 }
			 ?>
  </table>
</div>
<div class="page_middle_frame">
<div align="left">
  <label class="label_std">Additional sundries</label>
</div>
  <table width="676" id="table_details_display">
   
             <?php 
				  $m=0;
				   while ($m<$sundries_num) { 
					    $_SESSION['additional_item_id'.$m] = mysql_result($sundries_result, $m, "cap_item_id");
						$table_name = $_SESSION['db_prefix']."_store_items";
							$name_query = "SELECT item_name, unit FROM $table_name WHERE store_item_id = '".$_SESSION['additional_item_id'.$m]."' ";
							$name_result = mySQL_query($name_query);
							$name_num = mysql_num_rows($name_result);
							$item_name = mysql_result($name_result, 0, "item_name");
							$unit = mysql_result($name_result, 0, "unit");  
						$additional_quantity = mysql_result($sundries_result, $m, "cap_quantity");
						$auth_additional_quantity = mysql_result($sundries_result, $m, "auth_quantity");
						$delivered = mysql_result($sundries_result, $m, "delivered");
						$balance = $auth_additional_quantity - $delivered;
				 ?>
             <tr>
               <td width="268"><?php echo $item_name ?>
               <input type="hidden" name="additional_item_id_<?php echo $m?>" id="additional_item_id_<?php echo $m?>" value="<?php echo $_SESSION['additional_item_id'.$m] ?>"></td>
               <td width="41"><?php echo $unit ?></td>
               <td width="86" align="right" ><?php echo $additional_quantity ?></td>
               <td width="86" align="right" ><?php echo $auth_additional_quantity ?></td>
               <td width="86" align="right" ><?php echo $delivered ?></td>
               <td width="81" align="right" ><?php echo $balance ?></td>
             </tr>
              <?php
			  $m++;
             }
             ?>
  </table>
</div>
<div class="page_middle_frame">
<div align="left">
  <label class="label_std">Extra supplies </label>
</div>
  <table width="676" id="table_details_display">
   
             <?php 
				  $p=0;
				   while ($p<$extra_num) { 
					    $store_item_id = mysql_result($extra_result, $p, "store_item_id");
						$table_name = $_SESSION['db_prefix']."_store_items";
							$name_query = "SELECT item_name, unit FROM $table_name WHERE store_item_id = '".$store_item_id."' ";
							$name_result = mySQL_query($name_query);
							$name_num = mysql_num_rows($name_result);
							$item_name = mysql_result($name_result, 0, "item_name");
							$unit = mysql_result($name_result, 0, "unit");
						$auth_quantity = mysql_result($extra_result, $p, "auth_quantity");  
						$extra_quantity = mysql_result($extra_result, $p, "delivered");
				 ?>
             <tr>
               <td width="268"><?php echo $item_name ?></td>
               <td width="41"><?php echo $unit ?></td>
               <td width="86" align="right">&nbsp;</td>
               <td width="86" align="right" >&nbsp;</td>
               <td width="86" align="right" ><?php echo $extra_quantity ?></td>
               <td width="81" align="right" >&nbsp;</td>
             </tr>
              <?php
			  $p++;
             }
             ?>
  </table>
</div>
</td>
</tr>
</table> 
 <!--end of tableMain-->
  
</div>

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

</body>
<?php } else {
$_SESSION['denied_return'] = "sales/sales_main.php";	
include ("../navigation/sales_sidebar.php");?>
	<body onload="accessdenied(); return false">
	</body>
<?php }
?>
</html>