<?php
include("../include/session.php");

$userId = $_SESSION['username'];
date_default_timezone_set('Africa/Gaborone');
$entry_date = date("Y-m-d H:i:s", time());

$table_name = $_SESSION['db_prefix']."_purchase";
$purchase_order_query = "SELECT purchase_order  FROM $table_name ORDER BY purchase_order DESC LIMIT 1  ";
$purchase_order_result = mySQL_query($purchase_order_query);
$purchase_order_num = mysql_num_rows($purchase_order_result);
if ($purchase_order_num!=0) {
$purchase_order = mysql_result($purchase_order_result, 0, "purchase_order");
}
else {
	$purchase_order=0;
}
$purchase_order=$purchase_order+1;

$table_name = $_SESSION['db_prefix']."_purchase";
$purchase_query = "INSERT INTO $table_name (purchase_date, purchase_order, ordered_for,  supplier_id, ordered_by, company, authorised, value ) VALUES ('".$_SESSION['purchase_date']."', '".$purchase_order."', '".$_SESSION['order_text']."', '".$_SESSION['supplier_id']."', '".$_SESSION['ordered_by']."', '".$_SESSION['company']."', 'no', '".$_SESSION['all_total']."'  ) ";
execsql($purchase_query);

$table_name = $_SESSION['db_prefix']."_purchase";
$purchase_id_query = "SELECT purchase_id  FROM $table_name ORDER BY purchase_id DESC LIMIT 1  ";
$purchase_id_result = mySQL_query($purchase_id_query);
$purchase_id = mysql_result($purchase_id_result, 0, "purchase_id");


$table_name3 = $_SESSION['db_prefix']."_purchase_lines_temp";
$items_query = "SELECT * FROM $table_name3 WHERE  user_name =  '".$_SESSION['username']."' ";
$items_result = mySQL_query($items_query);
$items_num = mysql_num_rows($items_result);

$i=0;
while ($i<$items_num) {
	
   $category = mysql_result($items_result, $i, "category");
   $sub_category = mysql_result($items_result, $i, "sub_category");
   $store_item_id = mysql_result($items_result, $i, "store_item_id");
   $item_name = mysql_result($items_result, $i, "item_name");
   $quantity = mysql_result($items_result, $i, "quantity");
   $price = mysql_result($items_result, $i, "price");
   $ordered_for = mysql_result($items_result, $i, "order_no");
   $special_order_id = mysql_result($items_result, $i, "special_order_id");
   
   $table_name4 = $_SESSION['db_prefix']."_purchase_lines";  
   $insert_item_query = "INSERT INTO $table_name4 (purchase_id, category, sub_category, special_order_id, store_item_id, item_name, order_no, quantity, price ) VALUES ('".$purchase_id."', '".$category."', '".$sub_category."', '".$special_order_id."', '".$store_item_id."', '".$item_name."', '".$ordered_for."', '".$quantity."', '".$price."' ) ";
   execsql($insert_item_query);

	$table_name = $_SESSION['db_prefix']."_special_orders";
	$update_query = "UPDATE $table_name SET date_purchased = '".$_SESSION['purchase_date']."' WHERE special_order_id = '".$special_order_id."' ";
	execsql($update_query);
	

$i++;
}


	$table_name = $_SESSION['db_prefix']."_tracking_store";
	$trackingQuery = "INSERT INTO $table_name (entry_date, user_name, action, details_1, details_2, details_3, details_4) VALUES ('".$entry_date."' ,'".$_SESSION['username']."', '85', '".$purchase_id."', '".$_SESSION['supplier_id']."' ,'".$_SESSION['all_total']."', '".$_SESSION['username']."' )";
	$trackingResult = mySQL_query($trackingQuery);
	
include('../include/purchase_order_auth_alert.php');	


$table_name = $_SESSION['db_prefix']."_purchase_lines_temp";
$delete2_query = "DELETE FROM $table_name WHERE  user_name =  '".$_SESSION['username']."' "; 
execsql($delete2_query);

	
if (isset($_GET['new_button']) )
	   {
		 $action='purchaseWiz0.php';
	   }
		 
	if (isset($_GET['finish_button']) )
	  {
		$action='purchase_order.php?purchase_id='.$purchase_id.'&message=0' ;
	  }

	 $_SESSION['supplier_id'] = '';	
	 $_SESSION['del_note'] = '';	
     $_SESSION['purchase_date']  = date('Y-m-d');
	 $_SESSION['purchase_id'] = '';
	 $_SESSION['purchase_order'] = '';
	 $_SESSION['all_total'] = '';
	 
	 $_SESSION['company']= '';
	 $_SESSION['ordered_by'] = '';
	 
	 $_SESSION['store_item_id'] = '';
	 $_SESSION['store_item_name'] = '';
	 
	 $_SESSION['purchase_line_id'] = '';
	 $_SESSION['sup_name'] = '';
	 $_SESSION['total'] = '';
	 
	 $_SESSION['value'] = '';
	
?>

<script language="javascript" type="text/javascript">
        
		 window.location="<?php echo $action?>";
        
       </script>

