<?php

session_start();
if($_SESSION['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($_SESSION['dbName'],$connection) or die("Could not find DB");

							
$PurId    		= $_POST['PurId']; if ($PurId == ""){$PurId = $_GET["PurId"];}
$PurDtId    	= $_POST['PurDtId']; if ($PurDtId == ""){$PurDtId = $_GET["PurDtId"];}
$Mode  			= $_POST['Mode']; if ($Mode == ""){$Mode = $_GET["Mode"];}
$ItemQty    	= $_POST['ItemQty']; if ($ItemQty == ""){$ItemQty = $_GET["ItemQty"];}
$ItemCode    	= $_POST['ItemCode']; if ($ItemCode == ""){$ItemCode = $_GET["ItemCode"];}
$ItemWhCode 	= $_POST['ItemWhCode']; if ($ItemWhCode == ""){$ItemWhCode = $_GET["ItemWhCode"];}
$SerialId  		= $_POST['SerialId']; if ($SerialId == ""){$SerialId = $_GET["SerialId"];}
$ItemSerialNo	= $_POST['ItemSerialNo']; if ($ItemSerialNo == ""){$ItemSerialNo = $_GET["ItemSerialNo"];}
$HideSerialNo	= $_POST['HideSerialNo']; if ($HideSerialNo == ""){$HideSerialNo = $_GET["HideSerialNo"];}
$PPurType		= $_POST['PPurType']; if ($PPurType == ""){$PPurType = $_GET["PPurType"];}
$PerPage		= $_POST['PerPage']; if ($PerPage == ""){$PerPage = $_GET["PerPage"];}


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='PurDtId' id='PurDtId' value='$PurDtId' size='10' maxlength='40'/>
<input type='hidden' name='PurId' id='PurId' value='$PurId' 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'/>		
<input type='hidden' name='PPurType' value='$PPurType'>    
HERE;

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

	$query = " SELECT Count(*) As SerialNoCount From ".
          	 " ( SELECT SerialNo_Code from temp_purchasesserialno ".
	         "   WHERE SerialNo_Code = '".$ItemSerialNo."' ".
			 "   AND Journal_Type = 'PI' ".
			 "   UNION ".
			 "   SELECT SerialNo_Code from purchasesserialno ".
	         "   WHERE SerialNo_Code = '".$ItemSerialNo."' ".
			 "   AND Journal_Type = 'PI' ".
			 "   AND ifNull(SerialNo_Used,0) = 0 ".
			 "   AND GrnDt_ID != ".$PurDtId.
			 " ) Q1 ";
	$result = selectData($connection, $query);
	$SerialNoExists = $result[0];
	
	if ($SerialNoExists == 0) {
		if ($ItemWhCode != '' && isset($ItemWhCode)) {
			fbegin();
			$SerId = get_nextsequence('Serial_Id','',$connection);	
			fcommit();
			
			fbegin();
			$query = " INSERT INTO temp_purchasesserialno (Serial_ID, Journal_Type, PurchaseDt_ID, GrnDt_ID, Item_Code, SerialNo_Code, SerialNo_Used, RelatedSerial_Id, SerialNo_Quantity, Warehouse_code, Logged_Code ) ".
					 " VALUES ( ".$SerId.", 'PI', $PurDtId, $PurDtId, '".$ItemCode."', '".$ItemSerialNo."', 0, Null, 1, '".$ItemWhCode."', '".$_SESSION['useraccount']."' ) ";
							 
			$result=mysql_query($query,$connection);
				
			if ($result){
				$message = "Add serial no ".$ItemSerialNo." done successfully.";
				fcommit();
			}else{
				$message  = mysql_error(); 
				frollback();
			}	
		}
		else {
			$message  = "Insert not allowed, warehouse is empty. "; 
		}	
	}
	else {
		$message  = "Insert not allowed, Serial No ".$ItemSerialNo." exists. "; 
	}
}

if ($Mode == 'delete' && $SerialId != '') {
	
	
	$query = " SELECT ifNull(SerialNo_Used, 0) As SerialNo_Used from temp_purchasesserialno ".
	         " WHERE Serial_ID = ".$SerialId;
			 //" AND Logged_Code = '".$_SESSION['useraccount']."' ";
	$result = selectData($connection, $query);
	$SerialNoUsed = $result[0];
	
	if ($SerialNoUsed == 0) {
		fbegin();
		
		$query = " DELETE from temp_purchasesserialno ".
				 " WHERE Serial_ID = ".$SerialId.
				 //" AND Logged_Code = '".$_SESSION['useraccount']."' ".
				 " AND ifNull(SerialNo_Used, 0) = 0  ";
		
		$result=mysql_query($query,$connection);
			
		if ($result){
			$message = "Delete serial no ".$ItemSerialNo." done successfully.";
			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_GoodRecNoteSerialNo.php?PPurType=".$PPurType."&SRItemCode=".$ItemCode."&SRItemQty=".$ItemQty."&SRWhCode=".$ItemWhCode."&SRPurDtId=".$PurDtId."&SRPurId=".$PurId."&SRRemainQty=".$SRRemainQty."&perpage=".$PerPage."&SRHideSerialNo=".$HideSerialNo."&message=".$message);


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

HERE;

?>