<?php

session_start();
if($HTTP_SESSION_VARS['dbName']==""){
header("location:../index.php");
}


include("config.inc.php");
include("Functions_Purchase.php");

$connection = mysql_connect($config['dbServer'],$config['dbUser'],$config['dbPass']) or die("Could not connect to DB");
mysql_select_db($HTTP_SESSION_VARS['dbName'],$connection) or die("Could not find DB");

							

print <<<HERE
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<link rel="stylesheet" href="default.css" type="text/css" />
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />

</head>
<body>
<form name='additemserials' method='post' action=''>
<input type='hidden' name='ItemSerialNo' id='ItemSerialNo' value='$ItemSerialNo' size='20' maxlength='40'/>
<input type='hidden' name='SerialId' id='SerialId' value='$SerialId' size='10' maxlength='40'/>
<input type='hidden' name='ItemCode' id='ItemCode' value='$ItemCode' size='20' maxlength='40'/>
<input type='hidden' name='PrdId' id='PrdId' value='$PrdId' size='10' maxlength='40'/>
<input type='hidden' name='ItemQty' id='ItemQty' value='$ItemQty' size='10' maxlength='40'/>
<input type='hidden' name='ItemWhCode' id='ItemWhCode' value='$ItemWhCode' size='10' maxlength='40'/>
<input type='hidden' name='PerPage' id='PerPage' value='$PerPage' size='10' maxlength='40'/>
<input type='hidden' name='HideSerialNo' id='HideSerialNo' value='$HideSerialNo' size='10' maxlength='40'/>
<input type='hidden' name='Mode' id='Mode' value='$Mode' size='10' maxlength='40'/>	

HERE;


if ($Mode == 'insert' && $ItemSerialNo != '') {

	$IsAvailable = 1;
	
	if ($ItemWhCode != '' && isset($ItemWhCode)) {
		$query = " SELECT Count(*) As SerialNoCount From ".
				 " ( SELECT SerialNo_Code from temp_purchasesserialno ".
				 "   WHERE SerialNo_Code = '".$ItemSerialNo."' ".
				 "   AND Journal_Type = 'PD' ".
				 //"   AND ifNull(SerialNo_Used, 0) = 0  ".
				 "   UNION ".
				 "   SELECT SerialNo_Code from purchasesserialno ".
				 "   WHERE SerialNo_Code = '".$ItemSerialNo."' ".
				 "   AND Journal_Type = 'PD' ".
				 "   AND Production_ID != '".$PrdId."' ".
				 //"   AND ifNull(SerialNo_Used, 0) = 0  ".
					 " ) Q1 ";
		$result = selectData($connection, $query);
		$SerialNoExists = $result[0];
	
		if ($SerialNoExists == 0) {
			$IsAvailable = 1;	
		}
		else {
			$IsAvailable = 0;
			$message  = "Insert not allowed, Serial No ".$ItemSerialNo." duplicates. "; 
		}	
	
	}
	else {
		$IsAvailable = 0;
		$message  = "Insert not allowed, warehouse is empty. "; 
	}
	
	
	
	if ($IsAvailable == 1)
	{
		fbegin();
		$SerId = get_nextsequence('Serial_Id','',$connection);	
		fcommit();
					
		fbegin();
		
		$query = " INSERT INTO temp_purchasesserialno (Serial_ID, Journal_Type, PurchaseDt_ID, Production_ID, Item_Code, SerialNo_Code, SerialNo_Used, RelatedSerial_Id, SerialNo_Quantity, Warehouse_code, Logged_Code ) ".
				 " VALUES ( ".$SerId.", 'PD', $PrdId, $PrdId, '".$ItemCode."', '".$ItemSerialNo."', 0, Null, 1, '".$ItemWhCode."', '".$HTTP_SESSION_VARS['useraccount']."' ) ";
		$result=mysql_query($query,$connection);
			
		if ($result){
			$message = "Add serial no ".$ItemSerialNo." done successfully.";
			
			$query = " UPDATE Temp_Production SET ".
					 " Production_Quantity =  (ifNull(Production_Quantity,0) + 1) ".
					 " WHERE Production_ID = '".$PrdId."' ";
			//echo $query."<br>";exit;				
			if (!updateData($connection, $query, true))		 
				$message  = mysql_error(); 
				
			fcommit();
		}else{
			$message  = mysql_error(); 
			frollback();
		}
	
	}
	
	
}

if ($Mode == 'delete' && $SerialId != '') {
	
	$query = " SELECT ifNull(SerialNo_Used, 0) As SerialNo_Used, ifNull(PurchaseDt_ID, Production_ID) from temp_purchasesserialno ".
	         " WHERE Serial_ID = ".$SerialId;
			 //" AND Logged_Code = '".$HTTP_SESSION_VARS['useraccount']."' ";
	$result = selectData($connection, $query);
	$SerialNoUsed = $result[0];
	$SerialPrd_ID = $result[1];
	
	if ($SerialNoUsed == 0) {
		fbegin();
		
		$query = " DELETE from temp_purchasesserialno ".
				 " WHERE Serial_ID = ".$SerialId.
				 //" AND Logged_Code = '".$HTTP_SESSION_VARS['useraccount']."' ".
				 " AND ifNull(SerialNo_Used, 0) = 0  ";
		
		$result=mysql_query($query,$connection);
			
		if ($result){
			$message = "Delete serial no ".$ItemSerialNo." done successfully.";
			
			$query = " UPDATE Temp_Production SET ".
					 " Production_Quantity =  (ifNull(Production_Quantity,1) - 1) ".
					 " WHERE Production_ID = '".$SerialPrd_ID."' ";
			//echo $query."<br>";exit;				
			if (!updateData($connection, $query, true))		 
				$message  = mysql_error(); 	
			
			
			
			fcommit();
		}else{
			$message  = mysql_error(); 
			frollback();
		}			
	} 
	else {	
		$message  = "Delete not allowed, Serial No ".$ItemSerialNo." is used. "; 
	}	
}


$message = ereg_replace(' ','%20',$message);
$ItemWhCode = ereg_replace('&','%26',$ItemWhCode);
header ("Location: Form_AssembleSerialNo.php?SRPrdId=".$PrdId."&perpage=".$PerPage."&SRHideSerialNo=".$HideSerialNo."&message=".$message);


print <<<HERE
</form>
</body>

HERE;

?>