<?php

include("../../include/session.php");
if(!$session->isAdmin() && !$session->isRadstoneReceptionUser()){
	header("Location: ../../main.php");
}else{

	include("../../include/db.php");
	$date = isset($_REQUEST["date1"]) ? $_REQUEST["date1"] : "";
	//		$date = date("Y-m-d");
	$skip = mysql_real_escape_string($_POST['skip']);
	$recipieId = mysql_real_escape_string($_POST['recipieId']);
	$q = "SELECT * FROM nam_recipie WHERE recipieId = '$recipieId'";
	$resultR = execsql($q);
//	$recipie2 = mysql_real_escape_string($_POST['recipieId2']);
	$wet = mysql_real_escape_string($_POST['wet']);
	$hours = mysql_real_escape_string($_POST['hours']);
	$diesel = mysql_real_escape_string($_POST['dieselText']);
//	$date2 = isset($_REQUEST["date2"]) ? $_REQUEST["date2"] : "";
	$row = mysql_fetch_assoc($resultR);
	$name = mysql_result($resultR, 0, "recipieName");
	
	//updating diesel used and entering in the history - added to system 17-09-2011
	//$dieselQuery = "UPDATE nam_products SET quantity = quantity-$diesel WHERE productId = '100127'";
	//execsql($dieselQuery);
	
	//counting the new stock level
	//	$totalQuery= "SELECT quantity FROM nam_products WHERE productId = ". '100127' ;
	//	$totalResult = execsql($totalQuery);
	//	$total = mysql_result($totalResult, 0, "quantity");
		
	//updating item History
	//	$historyQuery = "INSERT INTO nam_itemhistory (productId, details, delNote, quantity, total, date) VALUES ('100127','boiler usage ','"."' '"."','".$diesel."', '".$total."','".$date."')";
	// 	execsql($historyQuery);
		
		
	//gets the products required in the recipie and stores them in 
	//material (to make up the query to find prices on each material)
	//matArray (quantity of each material)
	//recipieName is being added as a key and messing up the $matResult
		$totalCost = 0.00;
		$reference="";
		$entry="60";
		$userId = $_SESSION['username'];
	    
		date_default_timezone_set('Africa/Khartoum');
	    $entryDate = date("Y-m-d H:i:s",time()-3600);
		
	foreach($row as $key => $value){
		if($key != "recipieId" && $value != 0 && $key != "recipieId2" && $key != "wet" && $key != "hours")
		if($key != "recipieName")
		{
			$prodName = str_replace("_"," ", $key); #the tables added to the recipie use '_'s for spaces
			$q1 = "SELECT $key FROM nam_recipie WHERE recipieId = '$recipieId'";
			$r = execsql($q1);
			$query = "SELECT productId, quantity FROM nam_products WHERE productName = '$prodName'";
			$query1 = "SELECT $key FROM nam_perproductprice WHERE recipieName = '$name'";
			$resultP = execsql($query);
			$resultP1 = execsql($query1);
			$bleh = mysql_result($resultP1, 0, "$key");
			$totalQuantity = mysql_result($resultP,0,"quantity"); 
			$productId = mysql_result($resultP, 0, "productId");
			$perSkip = mysql_result($r, 0, $key);
			$cost = $bleh * $perSkip;

			$skipUsed = $perSkip * $skip;
			$totalQuantity = $totalQuantity - $skipUsed;
			$total = $cost * $skip;
			$totalCost += $total;
			$q2 = "UPDATE nam_products SET quantity = $totalQuantity WHERE productName = '$prodName'";
			execsql($q2);
//			echo $q2 ."<br /><br />";
//			echo $totalCost;
			$q3 = "INSERT INTO nam_itemhistory (productId, entry, reference, quantity, total, date, entryDate, userId) VALUES ('".$productId."','".$entry."','".$reference."', '".$skipUsed."', '".$totalQuantity."','".$date."','".$entryDate."','".$userId."')";
			execsql($q3);
//			echo $q3;
//			echo "$totalCost $prodName <br />";
//			echo "$totalQuantity $prodName <br />";
//			echo $skip ." ";
//			echo "$q3 <br />";
		}
	}
//	echo $cost;
	$return = mysql_real_escape_string($_POST['submitok']);
	$q4 = "INSERT INTO nam_production (recipieId, date, skips, cost)
		VALUES ('".$recipieId."','".$date."','".$skip."','".$totalCost."')";
//echo $totalCost;
	execsql($q4);
			header("Location: wetDaysProduction.php?recipieId=$recipieId&wet=$wet&hours=$hours&date=$date&return=$return"); 
}
?>