<?php


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

$style= "style=' background-color:#4069A7; color:#FFFFFF;font-family:Verdana, Arial, Helvetica, sans-serif; font-size:12px;'";

include("config.inc.php");
include("function.inc.php");
$connection = mysql_connect($config['dbServer'],$config['dbUser'],$config['dbPass'])
		or die("Could not connect to DB");

//echo $connection; 
mysql_select_db($_SESSION['dbName'],$connection) or die("Could not find DB");


//Read From Module Table
$querymodule=" Select ifNull(Module_TransferWithOneWh,0) As Module_TransferWithOneWh,ifNull(Module_Project,0) As Module_Project, ifNull(Module_Costcenter,0) As Module_CostCenter , ifNull(Module_RestrictProjCost,0) As Module_RestrictProjCost, ifNull(Module_RestrictCost,0) As Module_RestrictCost , module_serialno  From Module ";
$resultmodule=mysql_query($querymodule,$connection) or die(mysql_error());
while($rowmodule=mysql_fetch_array($resultmodule)){
	extract($rowmodule);
}

	//Read From Module Table
$querymodule=" Select ifNull(Module_LoadSerials,0) As Module_LoadSerials ".
			 " From Module ";
$resultmodule=mysql_query($querymodule,$connection) or die(mysql_error());
while($rowmodule=mysql_fetch_array($resultmodule)){
	extract($rowmodule);
} 

if ($fromwarehouse=='') {
$queryfromwh=" select TransferDt_WarehouseFrom from temp_transferdt where transferdt_id = '".$transferdt_id."'";
$resultfromwh = mysql_query($queryfromwh,$connection) or die(mysql_error());
$rowfromwh=mysql_fetch_array($resultfromwh);


	$fromwarehouse = $rowfromwh[0];
}

//Warehouse List
$querywarehouse=" select distinct warehouse.warehouse_code, warehouse_name ".
				" from warehouse where warehouse_code in (select warehouse_code from ".$_SESSION['dbLabel'].".loginstk where user_account='".$_SESSION['useraccount']."') ".
				" order by warehouse.warehouse_code";
$resultwh = mysql_query($querywarehouse,$connection) or die(mysql_error());
$whexist   = mysql_num_rows($resultwh);
if ($whexist == 0)
	$querywarehouse="select warehouse_code,warehouse_name from warehouse order by warehouse_code";
	
$resultwarehouse=mysql_query($querywarehouse,$connection) or die(mysql_error());
$resultwarehousefrom=mysql_query($querywarehouse,$connection) or die(mysql_error());


$querysetup= " select DecimalBase, DecimalBase1, DecimalBase2, base1, base2, grid_color1, grid_color2, ".
             " Date_Format(Setup_VatStartDate,'%Y%m%d') As Setup_VatStartDate, ifNull(Setup_VatPercentage,0) As Setup_VatPercentage from setup ";
$resultsetup=mysql_query($querysetup,$connection) or die(mysql_error());
while($rowsetup=mysql_fetch_array($resultsetup)){
	extract($rowsetup);
}

		$serialno='';


		if ($transfer_id!='')	
		{
			$qryct = "select count(serial_id) from temp_purchasesserialno where transferdt_id in 
					(select transferdt_id from temp_transferdt where transfer_id = ".$transfer_id.")
					and item_code = if('".$itemcode."'='',item_code,'".$itemcode."')
					";
			
			$resultct=mysql_query($qryct,$connection);
			$numct=mysql_fetch_row($resultct);	
			$numct=$numct[0];
		}
		
	 
	 
	 //exit;
	//echo $serialno;
	print <<<HERE
<HTML>
<head>
<title>Serial Number</title>
<link rel="stylesheet" type="text/css" media="all" href="css/style.css">

<script type="text/javascript" language="javascript">
function kd(e)
{
	x=document.itemsn;
    var intKey = (window.Event) ? e.which : e.keyCode;
	
    if (intKey == 13) { //enter key
        x.Btnsubmit.click();
		return false;
    }
    return true;
}



function ClearForm()
{
document.itemsn.submit();
}

function dosubmit(TransType){


//var transferid=document.itemsn.transfer_id.value;
//opener.document.transfer.actionmode.value='insert';
//opener.document.transfer.transfer_id.value=transferid;
//opener.document.transfer.submit();
var costcent= document.itemsn.costcent.value;
var tocostcent= document.itemsn.tocostcent.value;
	var fromwarehouse= document.itemsn.fromwarehouse.value;
	var towarehouse= document.itemsn.towarehouse.value;
	var toproject = document.itemsn.toproject.value;
	var fromproject = document.itemsn.fromproject.value;
	var actionmode = document.itemsn.actionmode.value;
	var transfer_id = document.itemsn.transfer_id.value;
	var transferdt_id = document.itemsn.transferdt_id.value;
	var transfer_remark = document.itemsn.transfer_remark.value;
	var transfer_date = document.itemsn.transfer_date.value;
	var driver_name = document.itemsn.driver_name.value;
	var vehicle_no = document.itemsn.vehicle_no.value;
	var perpage = document.itemsn.perpage.value;
	var serialno = document.itemsn.serialno.value;
	var documentrefno = document.itemsn.documentrefno.value;
	var actionmoded = 'is';

	fromwarehouse = fromwarehouse.replace('&','%26');
	towarehouse = towarehouse.replace('&','%26');
	var x = document.itemsn;
	
	if (towarehouse == ''){
		alert('Please select To warehouse.');
	}
	else if (fromwarehouse == ''){
		alert('Please select From warehouse.');
	}	
	else {
		x.action= 'Serial_Actions.php?fromproject='+fromproject+'&toproject='+toproject+'&fromwarehouse='+fromwarehouse+'&towarehouse='+towarehouse+'&actionmode='+actionmode+'&transfer_id='+transfer_id+'&transfer_remark='+transfer_remark+'&transfer_date='+transfer_date+'&drivername='+driver_name+'&documentrefno='+documentrefno+'&vehicle_no='+vehicle_no+'&costcent='+costcent+'&tocostcent='+tocostcent+'&actionmoded='+actionmoded+'&perpage='+perpage+'&serialno='+serialno+'&TransType='+TransType;
		x.submit();
	}	
}

function dosubmit1(TransType){

var transferid=document.itemsn.transfer_id.value;
opener.document.transfer.actionmode.value='insert';
opener.document.transfer.transfer_id.value=transferid;
opener.document.transfer.submit();
self.close();
}

function showlist1(obj){

if (obj.checked==1)
document.itemsn.list.value =1;
else
document.itemsn.list.value =0;


}



function deletepurchasesserialno(purchasesserialno_id,type,itemcode,tansferdtid,edit,fromwarehouse)
	{
	fromwarehouse = fromwarehouse.replace('&','%26');
	

	if (confirm ( "Are you sure you want to Delete the item?"))
	{
	document.itemsn.action='d_purchasesserialno.php?purchasesserialno_id='+purchasesserialno_id+'&type='+type+'&itemcode='+itemcode+'&tansferdtid='+tansferdtid+'&edit='+edit+'&fromwarehouse='+fromwarehouse;
	document.itemsn.submit();	
	}
	}


function applayserialno(checkname,itemcode,serialno){
if (checkname.checked==true)
	str1= 'checkserialno.php?itemcode='+itemcode+'&serialno='+serialno+'&chk=true';
	
else
	str1= 'checkserialno.php?itemcode='+itemcode+'&serialno='+serialno+'&chk=false';
	
	width = screen.width - 999;
	height = screen.height - 999;
	window.open(str1,"", "width=" + width + ",height=" + height + ",screenX=0,left=250,screenY=0,top=0,status=no,menubar=no,scrollbars=yes,resizable=yes");
	
}

function deleteserialno(serialnum,indexno,fromwarehouse){
var edit = document.itemsn.edit.value;
document.itemsn.indexno.value = indexno;
type = document.itemsn.type.value;
fromwarehouse = fromwarehouse.replace('&','%26');

document.itemsn.action="d_transferSerialNo.php?indexno="+indexno+"&edit="+edit+"&type="+type+"&fromwarehouse="+fromwarehouse;
document.itemsn.submit();

}

function fillinarray()
	{
	alert('wqe');
	//dosubmit();
	
	}
	

function DeleteTrDt(serial_id,transferdt_id,TransType)
					{
	var costcent= document.itemsn.costcent.value;
	var tocostcent= document.itemsn.tocostcent.value;
	var fromwarehouse= document.itemsn.fromwarehouse.value;
	var towarehouse= document.itemsn.towarehouse.value;
	var toproject = document.itemsn.toproject.value;
	var fromproject = document.itemsn.fromproject.value;
	var actionmode = document.itemsn.actionmode.value;
	var transfer_id = document.itemsn.transfer_id.value;
	//var transferdt_id = document.itemsn.transferdt_id.value;
	var transfer_remark = document.itemsn.transfer_remark.value;
	var transfer_date = document.itemsn.transfer_date.value;
	var driver_name = document.itemsn.driver_name.value;
	var vehicle_no = document.itemsn.vehicle_no.value;
	var perpage = document.itemsn.perpage.value;
	var actionmoded = 'd';
	document.itemsn.actionmoded.value='d';

	
	
	fromwarehouse = fromwarehouse.replace('&','%26');
	towarehouse = towarehouse.replace('&','%26');
	if (confirm ( "Are you sure you want to Delete current Serial?"))
	{
	
	document.itemsn.action= 'Form_Transfer_Actions.php?trs_id='+transferdt_id+'&fromproject='+fromproject+'&toproject='+toproject+'&fromwarehouse='+fromwarehouse+'&towarehouse='+towarehouse+'&actionmode='+actionmode+'&transfer_id='+transfer_id+'&transfer_remark='+transfer_remark+'&transfer_date='+transfer_date+'&drivername='+driver_name+'&vehicle_no='+vehicle_no+'&costcent='+costcent+'&tocostcent='+tocostcent+'&actionmoded='+actionmoded+'&perserial=yes&serial_id='+serial_id+'&item_withserial=1&perpage='+perpage+'&TransType='+TransType;
	document.itemsn.submit();
	}
}





function GenerateSerials(TransType) {
	x=document.itemsn;
	

	var costcent= document.itemsn.costcent.value;
	var tocostcent= document.itemsn.tocostcent.value;
	var fromwarehouse= document.itemsn.fromwarehouse.value;
	var towarehouse= document.itemsn.towarehouse.value;
	var toproject = document.itemsn.toproject.value;
	var fromproject = document.itemsn.fromproject.value;
	var actionmode = document.itemsn.actionmode.value;
	var transfer_id = document.itemsn.transfer_id.value;
	var transferdt_id = document.itemsn.transferdt_id.value;
	var transfer_remark = document.itemsn.transfer_remark.value;
	var transfer_date = document.itemsn.transfer_date.value;
	var driver_name = document.itemsn.driver_name.value;
	var vehicle_no = document.itemsn.vehicle_no.value;
	var perpage = document.itemsn.perpage.value;
	var serialno = document.itemsn.serialno.value;
	var actionmoded = 'is';
	var Vgenerate;
	fromwarehouse = fromwarehouse.replace('&','%26');
	towarehouse = towarehouse.replace('&','%26');
	
	
	
	var vString  = x.SRString.value;
	var vSeqFrom = x.SRSeqFrom.value;
	var vSeqTo 	 = x.SRSeqTo.value;
	

	
	
	if (vString == '' || vSeqFrom == '' || vSeqTo == '') {
		alert('Please fill missing fields.');
		x.SRString.focus();
	}
	else if ((vSeqFrom*1 != vSeqFrom/1) || (vSeqTo*1 != vSeqTo/1) ) {
		alert('Sequence From/To should be a NUMERIC value.');
		x.SRSeqFrom.focus();
	}
	else if ((vString.length +  vSeqFrom.length) > 40) {
		alert('Length of Serial No should be less than 40 characters.');
		x.SRString.focus();
	}	
	else
	{
	x.btnGenerate.disabled = true;
	
	x.action= 'Serial_Actions.php?fromproject='+fromproject+'&toproject='+toproject+'&fromwarehouse='+fromwarehouse+'&towarehouse='+towarehouse+'&actionmode='+actionmode+'&transfer_id='+transfer_id+'&transfer_remark='+transfer_remark+'&transfer_date='+transfer_date+'&drivername='+driver_name+'&vehicle_no='+vehicle_no+'&costcent='+costcent+'&tocostcent='+tocostcent+'&actionmoded='+actionmoded+'&perpage='+perpage+'&serialno='+serialno+'&TransType='+TransType+'&Vgenerate=generate'+'&vString='+vString+'&vSeqFrom='+vSeqFrom+'&vSeqTo='+vSeqTo;

	x.submit();

	}	
}


</script>
<style type="text/css">

body,td,th {
	font-family: Verdana, Arial, Helvetica, sans-serif;
	font-size: 11px;
}
body {
	margin-top: 2px;
	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="javascript:document.getElementById('serialno').focus()"> 

<form name='itemsn' method='post' action='serial_actions.php?TransType=$TransType'>
<input type='hidden' name='transfer_id' value='$transfer_id'>

<script type="text/javascript" language="javascript">
	if (opener.document.transfer.transfer_id.value != '')
		document.itemsn.transfer_id.value = opener.document.transfer.transfer_id.value;
</script>


<table border=0 width='600'>
<tr>
<input type='hidden' name='transferid' value='$transferid'>
<input type='hidden' name='transferdtid' value='$transferdtid'>
<input type='hidden' name='indexno' value='$indexno'>
<input type='hidden' name='edit' value='$edit'>
<input type='hidden' name='type' value='$type'>
<input type='hidden' name='list' value='$list'>


<input type='hidden' name='item' value='$item'>
<input type='hidden' name='fromproject' value='$fromproject'>
<input type='hidden' name='toproject' value='$toproject'>
<input type='hidden' name='actionmode' value='$actionmode'>
HERE;
//echo "<input type='hidden' name='towarehouse' value='$towarehouse'>";
echo"<input type='hidden' name='transfer_remark' value='".htmlspecialchars($transfer_remark, ENT_QUOTES)."'>";

	print<<<HERE
<input type='hidden' name='transfer_date' value='$transfer_date'>
<input type='hidden' name='driver_name' value='$driver_name'>
<input type='hidden' name='vehicle_no' value='$vehicle_no'>
<input type='hidden' name='costcent' value='$costcent'>
<input type='hidden' name='tocostcent' value='$tocostcent'>
<input type='hidden' name='transferdt_id' value='$transferdt_id'>
<input type='hidden' name='actionmoded' value='$actionmoded'>
<input type='hidden' name='itemcode' value='$itemcode'>
<input type='hidden' name='documentrefno' value='$documentrefno'>
</tr>
<tr>
HERE;

   // echo"<table cellpadding='0' cellspacing='0' border='0'  align='left' style='table-layout:fixed' >";
echo"<tr><td><span class='style11'>Serial Numbers for Item ".$itemcode."
			</span></td></tr>
		";  
     
		
		print <<<HERE
	 
	  <tr bgcolor='#E6E6E6'>
	  <td ><span class='style11'>Enter Serial No</span></td>
      <td colspan=3><input name="serialno" type="text" id="serialno" size="15" onkeydown="return kd(event)" value='$serialno'/></td>  
	  <td colspan=2><font color=red>&nbsp;</font>
	
HERE;


	echo"
	<input type='button' value='Submit' name='Btnsubmit' $style onclick=dosubmit('$TransType')>&nbsp;&nbsp;<input type='button' value='Confirm' $style onclick=dosubmit1('$TransType')></td>
	<td align='left' colspan=2 valign='center'><span class='style25'><input type='text' name='op' size=12 readonly value='Result per page'> </span><span class='style11'><select name='perpage' id='perpage' value='$perpage' onchange='ClearForm();'>
	";    
		
		if ($perpage==20)
			$s20 = 'selected';	
		else if ($perpage==100)
			$s100 = 'selected';	
		else if ($perpage==200)
			$s200 = 'selected';
		else if ($perpage==500)
			$s500 = 'selected';
		else if ($perpage==1000)
			$s1000 = 'selected';
		else if ($perpage==2000)
			$s2000 = 'selected';
		else if ($perpage=='all')
			$sall = 'selected';

			
				
        echo'<option '.$s20.' value="20">20</option>
			 <option '.$s100.' value="100">100</option>
			 <option '.$s200.' value="200">200</option>
			 <option '.$s500.' value="300">300</option>
			 <option '.$s1000.' value="300">300</option>
			 <option '.$s2000.' value="300">300</option>
			 <option '.$sall.' value="all">All</option>
			 </select></span></td>';	
			 
	echo"
	</tr>";
	
		if ($Module_LoadSerials == 1) {
	echo" <tr bgcolor='#E6E6E6'>
			<td align='left' valign='center' ><span class='style11'>String</span></td>
			<td align='left' valign='center' colspan='10'><span class='style11'>
				<input type='text' name='SRString' id='SRString' value='$SRString' style='width:210px;' maxlength='35'/></span>
				<input type='button' name='btnGenerate' value='Generate' onclick=GenerateSerials('$TransType'); style=' background-color:#4069A7; color:#FFFFFF;font-family:Verdana, Arial, Helvetica, sans-serif; font-size:12px;' />
			</td>
		  </tr>	
		  <tr bgcolor='#E6E6E6'>
			<td align='left' valign='center' ><span class='style11'>Sequence</span></td>
			<td align='left' valign='center' colspan='10'><span class='style11'> From: </span>
				<input type='text' name='SRSeqFrom' id='SRSeqFrom' value='$SRSeqFrom' style='width:70px;' maxlength='7'/>
				<span class='style11'> To: </span>
				<input type='text' name='SRSeqTo' id='SRSeqTo' value='$SRSeqTo' style='width:70px;' maxlength='7'/>
			</td>
		  </tr>		";
}
	echo "<tr bgcolor='#E6E6E6'><td><span class='style4'>Prefix</span></td><td><input type='text' name='txtprefixserialnb' id='txtprefixserialnb' value='$txtprefixserialnb' width='50px'>
			</td><td></td><td></td>";
echo "<tr bgcolor='#E6E6E6'><td><span class='style4'>From</span></td><td><input type='text' name='txtfromserialnb' id='txtfromserialnb' value='$txtfromserialnb' width='50px'>
			</td>";
echo "<td><span class='style4'>To</span>&nbsp;<input type='text' name='txttoserialnb' id='txttoserialnb' value='$txttoserialnb' width='50px'></td>";			
echo "<td><button type='button' id='btnGeneratesrl' name='btnGeneratesrl' onclick='GenerateSerialNbr(); return false;' title='Generate'><span><span>Generate</span></span></button>
		</td></td>";			
echo "</tr>	";
echo" <tr bgcolor='#DDDDDD'>
		<td align='left' colspan='4'><span class='style4' style='margin:0px 5px 0px 5px;'><input type='checkbox' name='txtselectall' id='txtselectall' onclick='selectAll();'>Select All</span>
		<button type='button' id='btnDeleteall' name='btnDeleteall' onclick='deleteAll(); return false;' title='Delete All'><span><span>Delete All</span></span></button></td>
	  </tr>	";
	echo"<tr><td colspan='8'><font color=red>$serialerror</font></td></tr>
	";
	if ($message!='')
	echo"<tr><td colspan='8'><font color=red>$message</font></td></tr>";

if ($Module_TransferWithOneWh==0)
	{
	echo	  " <tr bgcolor='#DDDDDD'><td colspan=10  ><span class='style11'>From Warehouse:&nbsp;</style><select name='fromwarehouse' id='fromwarehouse' style='font-size:11px; width:200px; height:18px;' >";
			echo"<option></option>";
			while($wrhf=mysql_fetch_row($resultwarehousefrom)){
			echo"<option ";
				if (($fromwarehouse==$wrhf[0]))
					echo " selected ";
			echo" value='$wrhf[0]'>$wrhf[0] / $wrhf[1]</option>";
			}
			echo"</select></td></tr>";
    }
	else
		echo"<input type='hidden' name='fromwarehouse' value='$fromwarehouse'>";
	
if ($Module_TransferWithOneWh==0)
	{
	echo	  " <tr bgcolor='#DDDDDD'><td colspan=10  ><span class='style11'>To Warehouse:&nbsp;</style>
			<select name='towarehouse' id='towarehouse' style='font-size:11px; width:200px; height:18px;' >";
			echo"<option></option>";
			while($wrhf=mysql_fetch_row($resultwarehouse)){
			echo"<option ";
				if (($towarehouse==$wrhf[0]))
					echo " selected ";
			echo" value='$wrhf[0]'>$wrhf[0] / $wrhf[1]</option>";
			}
			echo"</select></td></tr>";
    }
		else
		echo"<input type='hidden' name='towarehouse' value='$towarehouse'>";
	echo"</table><br>";
	echo"<div style='width:600px;height:400px;overflow:auto;'>";
					echo"<table width='600'>";
	$j=0;
	
	
	echo"<tr  bgcolor='#CCD8E6'><td></td>
	<td  height='25'><span class='style25'>S/N</span></td><td><span class='style25'>Serial No</span></td><td><span class='style25'>Item Code</span></td><td><span class='style25'>Item Description</span></td></tr>";
	
	if ($transfer_id<>'')
	{ if  ($perpage=='')
		$perpage = 5;
	$query = "select Serial_ID,temp_purchasesserialno.Item_Code,SerialNo_Code,SerialNo_Quantity,temp_purchasesserialno.Logged_Code ,item_description
				,temp_purchasesserialno.transferdt_id
				from temp_purchasesserialno left join items on items.item_code=temp_purchasesserialno.item_code 
				left join temp_transferdt on temp_transferdt.transferdt_id=temp_purchasesserialno.transferdt_id
				left join temp_transfer on temp_transferdt.transfer_id=temp_transfer.transfer_id
				where
				temp_transfer.transfer_id= ".$transfer_id."
				and temp_purchasesserialno.item_code = if('".$itemcode."'='',temp_purchasesserialno.item_code,'".$itemcode."')
				order by serial_id desc
				";
	if ($perpage!='all')
	$query = $query . " limit $perpage ";
	
	$result=mysql_query($query) or die(mysql_error());
	$j=0;
	$j = $numct;
	
	while($rows=mysql_fetch_array($result))
		{
			
		$ct+=1;

				if($ct%2==0)
					$Color="style='background-color:".$grid_color1."'";
				else			
					$Color="style='background-color:".$grid_color2."'";	
			
				echo "<tr ".$Color." >
		<td><img src='img/c_delete.png'  onMouseOver=\"style.cursor='hand'\" onclick=DeleteTrDt('".htmlentities($rows[0])."','".htmlentities($rows[6])."',\"$TransType\") title='Delete Record'  ></td>
		<td><span class=\"style4\">$j</span></td><td><span class=\"style44\">$rows[2]</span></td><td><span class=\"style44\">$rows[1]</span></td><td><span class=\"style44\">$rows[5]</span></td></tr>";
		$j=$j-1;
		}
	}
	print <<<HERE
	<script type="text/javascript" language="javascript">
	
	if (opener.document.transfer.transfer_id.value == '')
	{
	var transferid=document.itemsn.transfer_id.value;
	opener.document.transfer.actionmode.value='insert';
	opener.document.transfer.transfer_id.value=transferid;
	opener.document.transfer.submit();
	}
	</script>
HERE;
	
	echo"</table></div></form></body></HTML>";
	
	
	function displayParadoxDate($data){
	return substr($data, 0, 4) ."-". substr($data, 4, 2) ."-".substr($data, 6, 2);
	}
	function insertDate($data){
	return substr($data, 6, 4) . substr($data, 3, 2) . substr($data, 0, 2);
	}
?>