<?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");

							
$querysetup="select base1, base2, grid_color1, grid_color2 from setup";
$resultsetup=mysql_query($querysetup,$connection) or die(mysql_error());
while($rowsetup=mysql_fetch_array($resultsetup)){
extract($rowsetup);
}


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" />
<title>Item Serial No</title>
<script type="text/javascript" language="javascript">

function ClearForm()
{
  x=document.itemserials;
  x.SRItemSerialNo.value = '';
  x.action='Form_ProductionSerialNo.php';
  x.submit();

}

function HideShowSerialNo()
{
	x=document.itemserials;
	
	if (x.ChkQuery.checked)
		x.SRHideSerialNo.value = 1;
	else
		x.SRHideSerialNo.value = '';
		
	x.submit();
}

function ConfirmForm()
{
	x=document.itemserials;
	
	var vItemType  = x.SRItemType.value;
	var vPrdId     = x.SRPrdId.value;
	var vPrdDtId   = x.SRPrdDtId.value;
	var vItemQty   = x.SRCount.value;
	
	if (confirm ( "Are you sure you want to Adjust Quantity from Serial No ?") )
	{
		x.action="Form_Production_Actions.php?mode=editqty&SItemType="+vItemType+"&SPrdDtId="+vPrdDtId+"&SPrdId="+vPrdId+"&QItemQty="+vItemQty;
		x.submit();
			
		//opener.document.production.txtPrdId.value = vPrdId;
		//opener.document.production.action='Form_Production.php';
		//opener.document.production.submit();
		//self.close();
	}	
	
	document.getElementById('SRItemSerialNo').focus();
}

function LoadForm() 
{
	x=document.itemserials;
	//x.SRItemSerialNo.focus();
	x.SRItemSerialNo.disabled = false;
	document.getElementById('SRItemSerialNo').focus();
	
	ChangeLocation();
}


function ChangeLocation()
{
	x=document.itemserials;
	
    if (x.PMode.value == 1) {
		opener.document.production.action='Form_Production.php';
		opener.document.production.submit();
		
		if (x.message.value == '')
			self.close();
		
	}	
	
}

function SubmitForm() {
	x=document.itemserials;
	
	var vItemType  = x.SRItemType.value;
	var vPrdId     = x.SRPrdId.value;
	var vPrdDtId   = x.SRPrdDtId.value;
	
	var vSrQty         = x.SRItemQty.value;
	var vSrItemCode    = x.SRItemCode.value;
	var vSrTotal       = x.SRCount.value;
	var vSrSerial      = x.SRItemSerialNo.value;
	var vSrWhCode      = x.SRWhCode.value;
	
	var vSrPerPage = x.perpage.value;
	var vSrHide    = x.SRHideSerialNo.value;
	
	vSrQty   = parseInt(vSrQty);
	vSrTotal = parseInt(vSrTotal);
	
	vSrWhCode = vSrWhCode.replace('&','%26');
	
	
	if (vSrWhCode == '') {
		alert("Can't add Serial No "+vSrSerial+" warehouse is empty.");
	}	
	else if (vSrSerial == '') {
		alert('Please add a serial no.');
		x.SRItemSerialNo.focus();
	}
	else
	{
		x.SRItemSerialNo.disabled = true;
		
		x.action='Form_ProductionSerialNo_Actions.php?Mode=insert&ItemSerialNo='+vSrSerial+'&ItemType='+vItemType+'&PrdDtId='+vPrdDtId+'&PrdId='+vPrdId+'&PerPage='+vSrPerPage+'&HideSerialNo='+vSrHide;
		x.submit();
	}	
	
	//x.SRItemSerialNo.focus();
	document.getElementById('SRItemSerialNo').focus();
	
}

function DeleteSerialNo(SerialId, SerialNo) {
	x=document.itemserials;
	var vItemType  = x.SRItemType.value;
	var vPrdId     = x.SRPrdId.value;
	var vPrdDtId   = x.SRPrdDtId.value;
	var vPerPage   = x.perpage.value;
	var vHide      = x.SRHideSerialNo.value;
	
	
	if (confirm ( "Are you sure you want to Delete this record ?") )
	{
		x.action='Form_ProductionSerialNo_Actions.php?Mode=delete&ItemSerialNo='+SerialNo+'&SerialId='+SerialId+'&ItemType='+vItemType+'&PrdDtId='+vPrdDtId+'&PrdId='+vPrdId+'&PerPage='+vPerPage+'&HideSerialNo='+vHide;
		x.submit();
	}
	
	//x.SRItemSerialNo.focus();
	document.getElementById('SRItemSerialNo').focus();
}


function SetNextPage(startrange, perpage){
	x=document.itemserials;
	
	var vItemType   = x.SRItemType.value;
	var vPrdId      = x.SRPrdId.value;
	var vPrdDtId    = x.SRPrdDtId.value;
	
	x.action='Form_ProductionSerialNo.php?SRItemType='+vItemType+'&SRPrdDtId='+vPrdDtId+'&SRPrdId='+vPrdId+'&startrange='+startrange+'&perpage='+perpage;
	x.submit();
}

function SetPrvPage(startrange, perpage){
	x=document.itemserials;
	
	var vItemType   = x.SRItemType.value;
	var vPrdId      = x.SRPrdId.value;
	var vPrdDtId    = x.SRPrdDtId.value;
	
	x.action='Form_ProductionSerialNo.php?SRItemType='+vItemType+'&SRPrdDtId='+vPrdDtId+'&SRPrdId='+vPrdId+'&startrange='+startrange+'&perpage='+perpage;
	x.submit();
}

function kd(e)
{
	x=document.itemserials;
    var intKey = (window.Event) ? e.which : e.keyCode;
	
    if (intKey == 13) { //enter key
        x.btnSubmit.click();
		return false;
    }
    return true;
}


</script>

<style type="text/css">

body,td,th {
	font-family: Verdana, Arial, Helvetica, sans-serif;
	font-size: 11px;
}
body {
	margin-top: 0px;
	margin-left: 1px;
}
a:link {
	text-decoration: none;
	color: #0000FF;
}
a:visited {
	text-decoration: none;
	color: #0000FF;
}
a:hover {
	text-decoration: none;
	color: #0000FF;
}
a:active {
	text-decoration: none;
	color: #0000FF;
}
.style11 {color: #999999; font-weight: bold; }
.style12 {
	font-size: 9px;
	color: #999999;
	font-weight: bold;
}
.style15 {
   font-size: 11px;
   color: #000000;
   font-family: Verdana, Arial, Helvetica, sans-serif;   }
   
.style16 {
	font-size: 16px;
	color: #FF0000;
	font-weight: bold;
	font-family: "Times New Roman", Times, serif;
}
.style17 {font-size: 9px}
.style25 {color: #999999; font-weight: bold; font-size: 10px; }
.style26 {color: #FF0000}
.style27 {color: #999999 ;font-size: 10px;}
.style28 {color: #000000 ;font-size: 10px;}

</style>

</head>
<body onLoad='LoadForm();'  >
<form name='itemserials' method='post' action=''>
<input type='hidden' name='message' value='$message'>
<input type='hidden' name='PMode' id='PMode' value='$PMode'/>
<input type='hidden' name='SRItemType' value='$SRItemType'>
<input type='hidden' name='SRPrdDtId' value='$SRPrdDtId'>
<input type='hidden' name='SRPrdId' value='$SRPrdId'>
HERE;
//onunLoad='ChangeLocation();'

if ($SRItemType == 'RM') {
	$sub_query = " Select Item_code, Warehouse_Code, ifNull(ProductionRM_Quantity,0) ".
				 " From Temp_ProductionRM As ProductionRM ".
				 " Where ProductionHD_ID = '".$SRPrdId."' ".
				 " And ProductionRM_ID = '".$SRPrdDtId."' ";
}
else if ($SRItemType == 'FG') {
	$sub_query = " Select Item_code, Warehouse_Code, ifNull(ProductionFG_Quantity,0) ".
				 " From Temp_ProductionFG As ProductionFG ".
				 " Where ProductionHD_ID = '".$SRPrdId."' ".
				 " And ProductionFG_ID = '".$SRPrdDtId."' ";
}

$sub_result = selectData($connection, $sub_query);	    

$SRItemCode = $sub_result[0];
$SRWhCode   = $sub_result[1];
$SRItemQty  = $sub_result[2];


if ($SRItemType == 'RM') {
	$sub_query1 = " SELECT Serial_ID, Items.Item_Code As ItemCode, Items.Item_Description As ItemDesc, SerialNo_Code, SerialNo_Used, RelatedSerial_Id, SerialNo_Quantity  ".
				  " FROM temp_purchasesserialno ".
				  " Inner Join Items on temp_purchasesserialno.Item_Code = Items.Item_Code ".
				  " WHERE ProductionRM_ID  = '".$SRPrdDtId."' ". 
				  " AND Items.Item_Code = '".$SRItemCode."'  ".
				  " AND Journal_Type In ('PDT','PDO') ".
				  " ORDER BY Serial_ID Desc ";
}
else if ($SRItemType == 'FG') {
	$sub_query1 = " SELECT Serial_ID, Items.Item_Code As ItemCode, Items.Item_Description As ItemDesc, SerialNo_Code, SerialNo_Used, RelatedSerial_Id, SerialNo_Quantity  ".
				  " FROM temp_purchasesserialno ".
				  " Inner Join Items on temp_purchasesserialno.Item_Code = Items.Item_Code ".
				  " WHERE ProductionFG_ID  = '".$SRPrdDtId."' ". 
				  " AND Items.Item_Code = '".$SRItemCode."'  ".
				  " AND Journal_Type = 'PD' ".
				  " ORDER BY Serial_ID Desc ";
}			 


$sub_result1=mysql_query($sub_query1,$connection) or die(mysql_error());
$totalrows = mysql_num_rows($sub_result1);

$SRCount = $totalrows;
	
if ($startrange=='') $startrange = 0;
	
	
if ($perpage=='' && $config['DftResultPerPage'] != '')
	$perpage = $config['DftResultPerPage'];	
if ($perpage=='')
	$perpage = 5;
		
$sub_query1 = $sub_query1." limit ".$startrange.",".$perpage;

echo"<table border='0' cellspacing='1'  style='table-layout:fixed' align='center'>
	  <col width=20>
	  <col width=60>
	  <col width=250>
	  <col width=150>
	  
	  
		     
	  <tr bgcolor='#DDDDDD'>
	    <td align='center' colspan='4'><span class='style16'>Serial Numbers for Item $SRItemCode</span></td></tr>
	  <tr bgcolor='#DDDDDD'>
	    <td align='left' valign='top' colspan='2'><span class='style11'>Enter Serial No</span></td>
		<td align='left' valign='top' ><span class='style11'>
			<input type='text' name='SRItemSerialNo' id='SRItemSerialNo' value='$SRItemSerialNo' size='29' maxlength='40' onkeydown='return kd(event)'/>
			<input type='hidden' name='SRItemCode' id='SRItemCode' value='$SRItemCode' size='20' maxlength='40'/>
			<input type='hidden' name='SRWhCode' id='SRWhCode' value='$SRWhCode' size='20' maxlength='40'/>
			<input type='hidden' name='SRItemQty' id='SRItemQty' value='$SRItemQty' size='10' maxlength='40'/>
			<input type='hidden' name='SRCount' id='SRCount' value='$SRCount' size='10' maxlength='40'/>
			<input type='hidden' name='SRHideSerialNo' id='SRHideSerialNo' value='$SRHideSerialNo' size='10' maxlength='40'/>
			
			<br>
			<input type='button' name='btnSubmit' value='Submit' onclick='SubmitForm();' style=' background-color:#4069A7; color:#FFFFFF;font-family:Verdana, Arial, Helvetica, sans-serif; font-size:12px;' />
			<input type='button' name='btnCancel' value='Cancel' onclick='ClearForm();' style=' background-color:#4069A7; color:#FFFFFF;font-family:Verdana, Arial, Helvetica, sans-serif; font-size:12px;'/>
			<input type='button' name='btnConfirm' value='Confirm' onclick='ConfirmForm();' style=' background-color:#4069A7; color:#FFFFFF;font-family:Verdana, Arial, Helvetica, sans-serif; font-size:12px;'/></td>
		<td align='left' valign='center'><span class='style25'>Result per page </span><span class='style11'><select name='perpage' id='perpage' value='$perpage' onchange='ClearForm();'>";
              
//<input type='text' name='SRMode' id='SRMode' value='$SRMode' size='10' maxlength='40'/>
//<input type='text' name='SRSerialId' id='SRSerialId' value='$SRSerialId' size='10' maxlength='40'/>
			
        if ($perpage==5)
			$s5 = 'selected';
		else if ($perpage==10)
			$s10 = 'selected';	
		else if ($perpage==15)
			$s15 = 'selected';	
		else if ($perpage==20)
			$s20 = 'selected';	
		else if ($perpage==30)
			$s30 = 'selected';	
		else if ($perpage==60)
			$s60 = 'selected';
		else if ($perpage==90)
			$s90 = 'selected';
		else if ($perpage==120)
			$s120 = 'selected';

			
				
        echo'<option '.$s5.' value="5">5</option>
			 <option '.$s10.' value="10">10</option>
			 <option '.$s15.' value="15">15</option>
			 <option '.$s20.' value="20">20</option>
			 <option '.$s30.' value="30">30</option>
			 <option '.$s60.' value="60">60</option>
			 <option '.$s90.' value="90">90</option>
			 <option '.$s120.' value="120">120</option>
			 </select></span></td>';	
echo" </tr>
	  <tr bgcolor='#DDDDDD'>
		<td height='15' align='center' colspan='4'><font size='1' color='#FF0000' face='Verdana'><b>".$message."</b></font></td>
	  </tr>	
	  <tr bgcolor='#CCD8E6'>
	    <td  height='25' align='left'><span class='style25'>&nbsp; </span></td>
		<td  height='25' align='left'><span class='style25'>S/N </span></td>
		<td  height='25' align='center'><span class='style25'>Serial No </span></td>
		<td  height='25' align='left'><span class='style25'>
		<input name='ChkQuery' type='checkbox' id='ChkQuery' value='1' onClick='HideShowSerialNo();'   "; 
	          if($SRHideSerialNo==1) echo "checked />"; else echo " /> "; 
echo" 	Hide SerialNo</span></td>
	  </tr>";
		

	

	
if ($SRHideSerialNo == '') {

	$result=mysql_query($sub_query1,$connection) or die(mysql_error());		
	while($rows=mysql_fetch_array($result)){
			
		extract($rows);
		$ct+=1;

		if($ct%2==0)
			$Color="style='background-color:".$grid_color1."'";
		else			
			$Color="style='background-color:".$grid_color2."'";	
				

			
		$CurrentRow = $totalrows - $startrange - $ct + 1;
			
				
		echo'<tr '.$Color.'>
				<td height="20" align="center"><span class="style15"><img src="img/c_delete.png" onclick="DeleteSerialNo('.$Serial_ID.',\''.$SerialNo_Code.'\');" onMouseOver="style.cursor=\'hand\'"  title="Delete Record"  ></span></td>
				<td height="20" align="left"><span class="style15">'.$CurrentRow.'</span></td>
				<td height="20" align="left"><span class="style15">'.$SerialNo_Code.'</span></td>
				<td height="20" align="center"><span class="style15">&nbsp;</span></td>';
			
		echo'</tr>';		
		
		
		
	}
			
			
		
echo"   <tr><td colspan='4'>&nbsp;</td></tr>
		<tr height='30'><td colspan=4><table width='100%' border=0><tr><td width='140'>&nbsp;</td><td width='100' align='center'>";
		
		$diff= $startrange - $perpage;
		
		if (($diff) >= 0 ){
			$prvstartrange = $startrange - $perpage;
			echo"<img src='img/arrowleft.jpg' title='Previous' onMouseOver=\"style.cursor='hand'\"  onclick=SetPrvPage('".$prvstartrange."','".$perpage."')>";
		}
		else
			echo "&nbsp;";
		
		echo"</td><td width='100' align='center'>";
		
		
		if ($startrange <= $totalrows){
			$startrange = $startrange + $perpage;
			if  ($startrange < $totalrows)
				echo"<img src='img/arrowright.jpg' title='Next' onMouseOver=\"style.cursor='hand'\" onclick=SetNextPage('".$startrange."','".$perpage."')>";
		} else
			echo "&nbsp;";
			
		echo"</td><td width='140'>&nbsp;</td></tr>";
		echo"</table>";

echo"</td></tr>";

}


echo"</table>";
	


          

		  

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

HERE;



?>