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

if(!$session->isRadstoneStoreUser() && !$session->isAdmin() && !$session->isRadstoneReceptionUser()){
	header("Location: ../storeMenu.php");
}else{
	include("../../include/db.php");
	$productId = mysql_real_escape_string($_GET['productId']);
	$deliveryNote = mysql_real_escape_string($_GET['delnote']);
	$cat = mysql_real_escape_string($_GET['cat']);
	$date = isset($_REQUEST["date1"]) ? $_REQUEST["date1"] : "";
	//$date = mysql_real_escape_string($_GET['date1']);
	$supplierName = mysql_real_escape_string($_GET['supplierName']);
	$ppu = mysql_real_escape_string($_GET['ppu']);
	$quantity = mysql_real_escape_string($_GET['quantity']);
	$q4= "SELECT quantity FROM nam_products WHERE productId = '$productId'";
	$result5 = execsql($q4);
	$total = mysql_result($result5, 0, "quantity") + $quantity;
	$userId = $_SESSION['username'];
	date_default_timezone_set('Africa/Khartoum');
	$entryDate = date("Y-m-d H:i:s",time()-3600); 
	
	$q = "INSERT INTO nam_delivery (productId, supplierName, deliveryNote, quantity, pricePerUnit, date) VALUES('".$productId."','".$supplierName."','".$deliveryNote."','".$quantity."','".$ppu."','".$date."')";
	$q1 = "UPDATE nam_products SET suppliersName ='$supplierName', price = '$ppu', quantity = quantity + '$quantity', lastModDate = '$date' WHERE productId = '$productId' ";
				    
	$result1 = execsql($q);
	$result2 = execsql($q1);
	
	$qry = "SELECT deliveryId FROM nam_delivery ORDER BY deliveryId DESC LIMIT 1";
	$result3 = execsql($qry);
	$deliveryId = mysql_result($result3, 0, "deliveryId");
	$q3 = "INSERT INTO nam_itemhistory (productId, details, quantity, total, date, entryDate, userId) VALUES ('".$productId."','New Stock: <a href=\"../Store/showDelivery.php?id=".$deliveryId."\">".$deliveryId."</a>','".$quantity."', '".$total."','".$date."','".$entryDate."','".$userId."')";
	$result4 = execsql($q3);
	
			
	header("Location: ../delivery.php?cat=$cat&productId=0&date=$date");
	
	
	
}
?>