<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<?php
include('../include/session.php');
?>

<?php
$receive_id=$_GET['receive_id'];

$table_name = $_SESSION['db_prefix']."_receive_store";
$receive_query = "SELECT *  FROM $table_name WHERE  receive_id = '".$receive_id."' ";
$receive_result = mySQL_query($receive_query);
$b0 = mysql_result($receive_result, 0, "receive_date");
$b1 = mysql_result($receive_result, 0, "delivery_note");
$b2 = mysql_result($receive_result, 0, "supplier_id");
$value = mysql_result($receive_result, 0, "value");
$transport = mysql_result($receive_result, 0, "transport");
$b5 = mysql_result($receive_result, 0, "purchase_order");
$company = mysql_result($receive_result, 0, "company");


$table_name = $_SESSION['db_prefix']."_receive_store";
$check_query = "SELECT receive_id FROM $table_name WHERE  delivery_note = '".$b1."' AND supplier_id = '".$b2."' ";
$check_result = mySQL_query($check_query);
$check_num = mysql_num_rows($check_result);
if ($check_num>1) {
	  	$check_message="There are multiple receivals on this delivery note.";
		
		$table_name2 = $_SESSION['db_prefix']."_receive_store";
	  	$entry_query = "SELECT receive_id FROM $table_name2 WHERE  delivery_note = '".$b1."' AND supplier_id = '".$b2."' ";
		$entry_result = mySQL_query($entry_query);
		$entry_num = mysql_num_rows($entry_result);
	  $k=0;
	  $search_text="";
	  while ($k<$entry_num) {
	  $x1 = mysql_result($entry_result, $k, "receive_id");
	  if ($k==$entry_num-1) {
		$search_text = $search_text ."receive_id ='".$x1;
	  }
	  else {
	  $search_text = $search_text ."receive_id ='".$x1."' OR ";
	  }
	  $k++;
	  }
	  
	 $table_name = $_SESSION['db_prefix']."_receive_store_lines";
	 $line_query = "SELECT *  FROM $table_name WHERE  (".$search_text."') AND store_item_id>0";
	 $line_result = mySQL_query($line_query);
	 $line_num = mysql_num_rows($line_result);
	 
	 $table_name = $_SESSION['db_prefix']."_receive_store_lines";
	 $line2_query = "SELECT *  FROM $table_name WHERE  (".$search_text."') AND order_no>0";
	 $line2_result = mySQL_query($line2_query);
	 $line2_num = mysql_num_rows($line2_result);
	 
}
else {
	$check_message="";
	$table_name = $_SESSION['db_prefix']."_receive_store_lines";
	$line_query = "SELECT *  FROM $table_name WHERE  receive_id = '".$receive_id."' AND store_item_id>0";
	$line_result = mySQL_query($line_query);
	$line_num = mysql_num_rows($line_result);
	
	$table_name = $_SESSION['db_prefix']."_receive_store_lines";
	$line2_query = "SELECT *  FROM $table_name WHERE  receive_id = '".$receive_id."' AND order_no>0";
	$line2_result = mySQL_query($line2_query);
	$line2_num = mysql_num_rows($line2_result);
	echo '<br>'.$line2_num;
}


	
$table_name2 = $_SESSION['db_prefix']."_suppliers";
$customer_query = "SELECT * FROM $table_name2 WHERE supplier_id ='".$b2."' ";
$customer_result = mySQL_query($customer_query);
$customer_num = mysql_num_rows($customer_result);	

$a0 = mysql_result($customer_result, 0, "account_name");
$a5 = mysql_result($customer_result, 0, "contact1");
$a6 = mysql_result($customer_result, 0, "credit_cash");
$a7 = mysql_result($customer_result, 0, "post_address");
$a9 = mysql_result($customer_result, 0, "post_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, "email2");
$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, "email1");
$a30 = mysql_result($customer_result, 0, "cell1");
$a32 = mysql_result($customer_result, 0, "cell2");
$a35 = mysql_result($customer_result, 0, "contact2");
$a36 = mysql_result($customer_result, 0, "notes");

$supplierTT =
		   "<table id=table_tool_tip width=250 ><tr><td  width=75>Name:</td><td width=175>".$a0."</td></tr><tr><td>Address:</td><td>".$a7."</td></tr><tr><td></td><td>".$a9."</td></tr><tr><td>Contact:</td><td>".$a5."</td></tr><tr><td>Phone:</td><td>".$a24."</td></tr><tr><td>Cell:</td><td>".$a30."</td></tr></table><br><b>Click for details</b>";   

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

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

</head>

<body>
<?php
include ("../navigation/header.php");
$page_name = "Stores-main";
include ("../navigation/access.php");

if ($permission_num==1) {
	include ("../navigation/store_sidebar.php");
?>
<div class="main_frame">
  <div class="page_head_frame">
          <table width="809" border="0" cellspacing="0" cellpadding="0">
            <tr>
              <td><label class="label_page_head">Received from:</label>
                &nbsp;&nbsp;&nbsp;
                <label class="label_page_head_2"> <?php echo $a0 ?></label></td>
            </tr>
          </table>
          </div>
          <div class="page_middle_frame">
        <div class="page_inside_frame">
          <table width="612" id="table_display" >
            <tr>
              <td align="left" valign="top">Receival for:</td>
              <td align="left" valign="top"><?php echo $company?></td>
              <td align="left" valign="top">&nbsp;</td>
              <td align="left" valign="top">&nbsp;</td>
            </tr>
            <tr>
              <td width="114" align="left" valign="top">Received from:</td>
              <td width="155" align="left" valign="top"><a href="displaySupplier.php?supplier_id=<?php echo $b2 ?>" onMouseover="ddrivetip('<?php echo $supplierTT ?>', 200)"; onMouseout="hideddrivetip()"><?php echo $a0?></a></td>
              <td width="146" align="left" valign="top">Date:</td>
              <td width="185" align="left" valign="top"><?php echo date("d-m-Y", strtotime($b0 )) ?></td>
            </tr>
            <tr>
              <td align="left" valign="top">Delivery note:</td>
              <td align="left" valign="top"><?php echo $b1 ?></td>
              <td align="left" valign="top">Purchase order no:</td>
              <td align="left" valign="top"><?php echo $b5 ?></td>
            </tr>
            <tr>
              <td colspan="4" align="left" valign="top"><?php echo $check_message?></td>
            </tr>
          </table>
        </div>
        <div class="page_inside_frame">
          <table width="612" id="table_display" >
            <tr>
              <td width="211" height="26" align="left" valign="top">Item</td>
              <td width="116" align="left" valign="top">Group</td>
              <td width="51" align="left" valign="top">Quantity</td>
              <td width="106" align="left" valign="top">Price (BWP)</td>
              <td width="104" align="left" valign="top">Total</td>
            </tr>
            <?php
				$i=0;
				$sub_tot=0;
				$a6_total=0;
				 while ($i<$line_num){
					 $a0 = mysql_result($line_result, $i, "receive_line_id");
					 $a1 = mysql_result($line_result, $i, "store_item_id");
					 $table_name = $_SESSION['db_prefix']."_store_items";
						$item_query = "SELECT item_name, subCategory, unit 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");
						$a4 = mysql_result($item_result, 0, "subCategory");
					 $a2 = mysql_result($line_result, $i, "quantity");
					 $a3 = mysql_result($line_result, $i, "price");
					$a5=$a2*$a3;
					$a6_total=$a6_total+$a5;
					 
				 ?>
            <tr>
              <td valign="top"><?php echo $a1a ?></td>
              <td valign="top"><?php echo $a4 ?></td>
              <td valign="top"><?php echo $a2 ?></td>
              <td align="right" valign="top"><div align="right"><?php echo $a3." /".$a3a ?></div></td>
              <td align="right" valign="top"><div align="right"><?php echo number_format($a5, 2, '.', ',')?></div></td>
            </tr>
            <?php 
			   // $a6_total=$a6_total+$a6;
				$i++;
				 }
				 ?>
            <tr>
              <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">&nbsp;</td>
              <td align="left" valign="top"></td>
            </tr>
            <tr>
              <td colspan="4" align="left" valign="top"><div align="right">Total:</div></td>
              <td align="left" valign="top"><div align="right"><?php echo number_format($a6_total, 2, '.', ',') ?></div></td>
            </tr>
          </table>
          <br>
          <table width="612" id="table_display" >
            <tr>
              <td width="211" height="26" align="left" valign="top">Item </td>
              <td width="116" align="left" valign="top">Ordered for:</td>
              <td width="51" align="left" valign="top">Quantity</td>
              <td width="106" align="left" valign="top">Price (BWP)</td>
              <td width="112" align="left" valign="top">Total</td>
            </tr>
            <?php
				$i=0;
				$sub_tot=0;
				$b6_total=0;
				 while ($i<$line2_num){
					 $b0 = mysql_result($line2_result, $i, "receive_line_id");
					 $b1 = mysql_result($line2_result, $i, "item_name");
					 $b2 = mysql_result($line2_result, $i, "quantity");
					 $b3 = mysql_result($line2_result, $i, "price");
					 $b4 = mysql_result($line2_result, $i, "order_no");
					$b5=$b2*$b3;
					$b6_total=$b6_total+$b5;
					
					$table_name = $_SESSION['db_prefix']."_compiled_jobs";
				$project_query = "SELECT  project, cust_id, date  FROM $table_name WHERE job_card_no = '".$b4."' ";
				$project_result = mySQL_query($project_query);
				$project = mysql_result($project_result, 0, "project");	
				$cust_id = mysql_result($project_result, 0, "cust_id");	
				$date = mysql_result($project_result, 0, "date");
				
				$table_name2 = $_SESSION['db_prefix']."_customer";
				$customer_query = "SELECT account_type, title, name_2, comp_name 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") { 
					$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>".$b4."</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 valign="top"><?php echo $b1 ?></td>
              <td valign="top"><a href="../orders/job_card.php?job_card_no=<?php echo $b4 ?>" onmouseover="ddrivetip('<?php echo $jobTT ?>', 250)"; onmouseout="hideddrivetip()"><?php echo $b4 ?></a></td>
              <td valign="top"><?php echo $b2 ?></td>
              <td align="right" valign="top"><div align="right"><?php echo $b3 ?></div></td>
              <td align="right" valign="top"><div align="right"><?php echo number_format($b5, 2, '.', ',')?></div></td>
            </tr>
            <?php 
			   // $a6_total=$a6_total+$a6;
				$i++;
				 }
				 ?>
            <tr>
              <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">&nbsp;</td>
              <td align="left" valign="top"></td>
            </tr>
            <tr>
              <td colspan="4" align="left" valign="top"><div align="right">Total:</div></td>
              <td align="left" valign="top"><div align="right"><?php echo number_format($b6_total, 2, '.', ',') ?></div></td>
            </tr>
          </table>
          <br>
          <table width="612" id="table_display" >
            <tr>
              <td align="right">                All total:</td>
              <td align="right"><?php echo number_format($value, 2, '.', ',')?></td>
            </tr>
            <tr>
              <td colspan="2">&nbsp;</td>
            </tr>
            <tr>
              <td width="494" align="right">Transport costs:</td>
              <td width="106" align="right"><?php echo number_format($transport, 2, '.', ',')?>&nbsp;</td>
            </tr>
          </table>
          <br>
        
</div>
<div class="page_inside_frame">
<a href="receive_note_print.php?receive_id=<?php echo $receive_id ?>" target="_blank"><input name="print_button" type="button" class="imagebutton" value="Print receive note " /></a>
</div> 
</div>       
</div>  
</div>
<?php include ('../navigation/side_bar.php'); ?>


<?php } else {
$_SESSION['denied_return'] = "store/store_main.php";	
include ("../navigation/store_sidebar.php");?>
<body onLoad="accessdenied(); return false">
<?php include ('../navigation/side_bar.php'); ?>
</body>
<?php }
?>
</body>
</html>