<?php 
if ($_SESSION['company']=='') {
?>
<script language="javascript" type="text/javascript">
          needToConfirm = false;
          window.location="store_no_return.php";
</script>
<?php
}
else {
$table_name = $_SESSION['db_prefix']."_purchase_lines_temp";
$line_query = "SELECT *  FROM $table_name WHERE  user_name =  '".$_SESSION['username']."' AND store_item_id>0";
$line_result = mySQL_query($line_query);
$line_num = mysql_num_rows($line_result);

$table_name = $_SESSION['db_prefix']."_purchase_lines_temp";
$line2_query = "SELECT *  FROM $table_name WHERE store_item_id=0  AND user_name =  '".$_SESSION['username']."' ";
$line2_result = mySQL_query($line2_query);
$line2_num = mysql_num_rows($line2_result);
?>
</head>
<?php include('../include/wait.php') ?>

<body onLoad="MM_showHideLayers('wait','','hide')" onFocus="MM_showHideLayers('wait','','hide');"  onBlur="MM_showHideLayers('wait','','hide');">
<div class="wizard_page_middle_frame">
<form id="feedbackform" method="get" action="purchaseWiz6.php" >
  <table width="670" id="table_display" >
          <tr>
            <td align="left" valign="top">Purchase order for:</td>
            <td align="left" valign="top"><?php echo $_SESSION['company'] ?></td>
            <td align="left" valign="top">Ordered for:</td>
            <td align="left" valign="top"><?php echo $_SESSION['order_text'] ?></td>
          </tr>
          <tr>
            <td width="121" align="left" valign="top">To purchase from:</td>
            <td width="181" align="left" valign="top"><?php echo $_SESSION['sup_name'] ?></td>
            <td width="106" align="left" valign="top">Date:</td>
            <td width="242" align="left" valign="top"><?php echo date("d-m-Y", strtotime($_SESSION['purchase_date'])) ?></td>
          </tr>
          <tr>
            <td align="left" valign="top">Ordered by:</td>
            <td align="left" valign="top"><?php echo $_SESSION['ordered_by'] ?></td>
            <td align="left" valign="top">Authorised by:</td>
            <td align="left" valign="top">&nbsp;</td>
          </tr>
          <tr>
            <td colspan="4" align="left" valign="top"></td>
          </tr>
        </table><br>
<br>
Store items
    <table width="665" id="table_display" >
          <tr>
            <td height="26" colspan="2" align="left" valign="top">Item</td>
            <td width="101" align="left" valign="top">Group</td>
            <td width="71" align="left" valign="top">Unit</td>
            <td width="56" align="left" valign="top">quantity</td>
            <td width="71" align="left" valign="top">unit price</td>
            <td width="97" align="left" valign="top">total</td>
            </tr>
          <?php
				$i=0;
				$sub_tot=0;
				$a5_total=0;
				 while ($i<$line_num){
					 $a0 = mysql_result($line_result, $i, "purchase_line_id");
					 $a1 = mysql_result($line_result, $i, "store_item_id");
					 $table_name = $_SESSION['db_prefix']."_store_items";
						$item_query = "SELECT item_name, unit, subCategory FROM $table_name WHERE store_item_id = '".$a1."' ";
						$item_result = mySQL_query($item_query);
						$a1a = mysql_result($item_result, 0, "item_name");
						$a3a = mysql_result($item_result, 0, "unit");
						$a4a = mysql_result($item_result, 0, "subCategory");
					 $a2 = mysql_result($line_result, $i, "quantity");
					 $a3 = mysql_result($line_result, $i, "price");
					 
					$a5=$a2*$a3;
					$a5_total=$a5_total+$a5;
					 
				 ?>
          <tr>
            <td colspan="2" valign="top"><?php echo $a1a ?></td>
            <td valign="top"><?php echo $a4a ?></td>
            <td valign="top"><?php echo $a3a ?></td>
            <td valign="top"><?php echo $a2 ?></td>
            <td align="right" valign="top"><?php echo $a3. "  /" .$a3a ?>&nbsp;</td>
            <td align="right" valign="top"><?php echo number_format($a5, 2, '.', ',')?></td>
            </tr>
          <?php 
			  $i++;
				 }
				 ?>
          <tr>
            <td colspan="2" align="left" valign="top">&nbsp;</td>
            <td align="left" valign="top">&nbsp;</td>
            <td align="left" valign="top">&nbsp;</td>
            <td align="left" valign="top">&nbsp;</td>
            <td align="left" valign="top">Total:</td>
            <td align="right" valign="top"><?php echo number_format($a5_total, 2, '.', ',') ?></td>
            </tr>
          </table>
        <br>
<br>
Non store items
    <table width="665" id="table_display" >
          <tr>
            <td height="26" colspan="2" align="left" valign="top">Item</td>
            <td width="71" align="left" valign="top">Order</td>
            <td width="56" align="left" valign="top">quantity</td>
            <td width="71" align="left" valign="top">unitprice</td>
            <td width="97" align="left" valign="top">total</td>
          </tr>
          <?php
				$i=0;
				$sub_tot=0;
				$a6_total=0;
				 while ($i<$line2_num){
					 $a0 = mysql_result($line2_result, $i, "purchase_line_id");
					 $a1 = mysql_result($line2_result, $i, "item_name");
					 $a2 = mysql_result($line2_result, $i, "quantity");
					 $a3 = mysql_result($line2_result, $i, "price");
					 $a4 = mysql_result($line2_result, $i, "order_no");
					 
					 
					$a6=$a2*$a3;
					$a6_total=$a6_total+$a6;
					
		
		$table_name = $_SESSION['db_prefix']."_compiled_jobs";
		$job_query = "SELECT cust_id, date, project FROM $table_name WHERE job_card_no = '".$a4."' ";
		$job_result = mySQL_query($job_query);
		$job_num = mysql_num_rows($job_result);
		$cust_id = mysql_result($job_result, 0, "cust_id");
		$date = mysql_result($job_result, 0, "date");
		$project = mysql_result($job_result, 0, "project");
		
		$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);	
		$type = mysql_result($customer_result, 0, "account_type");
			if ($type=="corporate") { 
				$cust_name = mysql_result($customer_result, 0, "comp_name"); 
			} 
			else {
				$cust_name = mysql_result($customer_result, 0, "title")." ".mysql_result($customer_result, 0, "name_2")." ".mysql_result($customer_result, 0, "comp_name");
 			}			
					
					
					$jobTT =
		   "<b>Special order:</b><br><table id=table_tool_tip width=250 border=0 ><tr><td>Job card no:</td><td>".$a4."</td></tr><tr><td>Project:</td><td>".$project."</td></tr><tr><td>Ordered for:</td><td>".$cust_name."</td></tr><tr><td>Date:</td><td>".date("d-m-Y", strtotime($date))."</td></tr></table><br><b>Click to view job card.</b>";  
					 
				 ?>
          <tr>
            <td colspan="2" valign="top"><?php echo $a1 ?></td>
            <td valign="top"><a href="../orders/job_card.php?job_card_no=<?php echo $a4 ?>" onMouseOver="ddrivetip('<?php echo $jobTT ?>', 250)"; onMouseOut="hideddrivetip()"><?php echo $a4 ?></a></td>
            <td valign="top"><?php echo $a2 ?></td>
            <td align="right" valign="top"><?php echo $a3  ?>&nbsp;</td>
            <td align="right" valign="top"><?php echo number_format($a6, 2, '.', ',')?></td>
          </tr>
          <?php 
			  $i++;
				 }
				 ?>
          <tr>
            <td colspan="2" align="left" valign="top">&nbsp;</td>
            <td align="left" valign="top">&nbsp;</td>
            <td align="left" valign="top">&nbsp;</td>
            <td align="left" valign="top">Total:</td>
            <td align="right" valign="top"><?php echo number_format($a6_total, 2, '.', ',') ?></td>
          </tr>
        </table>
<br>
<?php 
$all_total = $a5_total + $a6_total;
$_SESSION['all_total'] = $all_total;
?>
        
        <table width="665" id="table_display" >
  <tr>
    <td width="556">Total value on this purchase order:</td>
    <td width="97" align="right"><?php echo number_format($all_total, 2, '.', ',') ?></td>
  </tr>
</table>

        
        <div class="wizard_frame">
          <input name="s" type="hidden" id="s" value="4" />
          <input name="p" type="hidden" id="p" value="4" />
          <label class="label_std">If you want to make changes use the 'Prev' button to go back. <br />
            Click 'New' to make a new entry or to finish the wizard click 'Finish'.</label>
        </div>
        <div class="wizard_frame">
      <table width="590" height="50" align="center" id="table_button_bar">
          <tr>
            <td width="117"><input type="button" name="lastprev_button" class="imagebutton" value="Prev" onClick="needToConfirm = false;   MM_showHideLayers('wait','','show'); MM_goToURL('parent','purchaseWizbase.php?s=&p=2');return document.MM_returnValue" /></td>
            <td width="115">&nbsp;</td>
            <td width="99"><input name="cancelbutton" type="button" class="imagebutton" id="cancelbutton" onClick="javascript:cancel_confirmation(1, '../store/store_main.php'); needToConfirm = false;" value="Cancel" /></td>
            <td width="125"><input type="submit" name="new_button" class="imagebutton" id="new_button" value="New" onClick="needToConfirm = false; MM_showHideLayers('wait','','show');" /></td>
            <td width="110"><input  name="finish_button" type="submit" class="imagebutton" id="finish_button" value="Finish" onClick="needToConfirm = false; MM_showHideLayers('wait','','show');"  /></td>
          </tr>
        </table>
        </div>  
</form>
</div>

<?php
}
?>