<?php
include("../include/session.php");
include("../include/db.php");
	
		
	$cat = $_SESSION['store'];
	$subcat= $_SESSION['sub_cat'];
	$productId = $_SESSION['productId'];
	$productName =$_SESSION['productName'];
	$supplierName = $_SESSION['supplierName'];
	$delNote = $_SESSION['deliveryNote'];
	$quantity  = $_SESSION['quantity'];
	$price = $_SESSION['price'];
	$date = $_SESSION['receiveDate'];
	
	
	if ($cat=="Tiles and Accessories") {
		$entry="0";
		$reference="Radstone"; }
	else {
		$entry="20";
	    $reference=$_SESSION['accountNo'];
	}
	
	if (isset($_GET['new_button']) )
	   {
		 $action='deliveryWiz0.php';
	   }
		 
	if (isset($_GET['finish_button']) )
	  {
		$action='stockReceived.php?date=0&cat='.$cat.'&productIdSelect=' ;
	  }
	
	
	$d = date("d");
	$m = date("m");
	$y = date("Y");
	$modDate=date('Y-m-d');
	$active="1";
	
	
	
			
	$userId = $_SESSION['username'];
	date_default_timezone_set('Africa/Khartoum');
	$entryDate = date("Y-m-d H:i:s",time()-3600); 
	
	
	$deliveryQuery = "INSERT INTO delivery (productId, supplierName, deliveryNote, quantity, pricePerUnit, date) VALUES ('".$productId."', '".$supplierName."', '".$delNote."', '".$quantity."', '".$price."', '".$date."' )";
	$deliveryResult = mySQL_query($deliveryQuery);
	
	
	
	$quantityQuery = "SELECT quantity, price FROM products WHERE productId = '$productId'";
	$quantityResult = execsql($quantityQuery);
	$totalQuantity = mysql_result($quantityResult, 0, "quantity") + $quantity;
	
	
	$stockQuery = "UPDATE products SET suppliersName = '".$supplierName."', quantity = '".$totalQuantity."' , lastModDate = '".$date."', price = '".$price."'  WHERE productId = '".$productId."'";
	$stockResult = mySQL_query($stockQuery);
	
			
	$historyQuery = "INSERT INTO itemhistory (productId,  entry, reference, quantity, total, date, entryDate, userId) VALUES ('".$productId."' ,'".$entry."','".$reference."','".$quantity."', '".$totalQuantity."','".$date."','".$entryDate."','".$userId."')";
	$historyResult = mySQL_query($historyQuery);	
	

	
	$actionQuery = "INSERT INTO action_tracking (entry_date, user, action, details) VALUES ('".$entryDate."' ,'".$userId."','New received' ,'".$productName."' )";
	$actionResult = mySQL_query($actionQuery);
	
	 $_SESSION['store'] ="";
	 $_SESSION['sub_cat'] ="";
     $_SESSION['productId'] ="";
	 $_SESSION['productName'] ="";
	 $_SESSION['accountNo'] ="";
     $_SESSION['supplierName'] = "";
	 $_SESSION['deliveryNote'] = "";
	 $_SESSION['quantity'] = "";
	 $_SESSION['price'] = "";
	 $_SESSION['receiveDate']  = date('Y-m-d');
	 $_SESSION['p'] = 1;
     $_SESSION['error']=0;
	 $_SESSION['changed']="";
	 $_SESSION['changed2']="";
	
?>

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

