<?php

session_start();
header("Content-type: text/html; charset=".$_SESSION['encodingmode']);
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");


if(	$_SESSION['encodingmode'] == 'utf8' || $_SESSION['encodingmode'] == 'utf-8'){
	mysql_query("SET NAMES 'utf8'"); 
	mysql_query('SET CHARACTER SET utf8'); 
	mysql_set_charset('utf8', $connection);
}							
$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);
}


//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());

$querydflwh="select warehouse_code as defaultwarehouse from ".$_SESSION['dbLabel'].".loginstk where user_account='".$_SESSION['useraccount']."' and ifNull(default_warehouse,0) = 1 ";
$resultdflwh=mysql_query($querydflwh,$connection) or die(mysql_error());
while($rows=mysql_fetch_array($resultdflwh)){
  extract($rows);
}

$mainaction  	= $_POST['mainaction']; if ($mainaction == ""){$mainaction = $_GET["mainaction"];}
$actionmode  	= $_POST['actionmode']; if ($actionmode == ""){$actionmode = $_GET["actionmode"];}
$perpage		= $_POST['perpage']; if ($perpage == ""){$perpage = $_GET["perpage"];}
$startrange		= $_POST['startrange']; if ($startrange == ""){$startrange = $_GET["startrange"];}
$PPurId    		= $_POST['PPurId']; if ($PPurId == ""){$PPurId = $_GET["PPurId"];}
$PPurSupplier  	= $_POST['PPurSupplier']; if ($PPurSupplier == ""){$PPurSupplier = $_GET["PPurSupplier"];}
$lstWarehouse	= $_POST['lstWarehouse']; if ($lstWarehouse == ""){$lstWarehouse = $_GET["lstWarehouse"];}
$SRItemSerialNo = $_POST['SRItemSerialNo']; if ($SRItemSerialNo == ""){$SRItemSerialNo = $_GET["SRItemSerialNo"];}




if (empty($lstWarehouse)) {
	if(!empty($defaultwarehouse))
		$lstWarehouse = $defaultwarehouse;
}

print <<<HERE
<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1256" />
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<link rel="stylesheet" type="text/css" media="all" href="css/style.css">
<title>Add Serials</title>
<script type="text/javascript" language="javascript">

function RefreshPage()
{
  x=document.itemserials;
  x.SRItemSerialNo.value = '';
  //x.action='Form_ReturnPurchaseAddSerial.php';
  x.submit();

}


function ConfirmForm()
{
	ChangeLocation();
}

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

function ChangeLocation()
{
	x=document.itemserials;
	var vPurId = x.PPurId.value;
	opener.document.purchase.txtPurId.value = vPurId;
	opener.document.purchase.actionmode.value = 'submitrecord';
	opener.document.purchase.action='Form_ReturnPurchase.php';
	opener.document.purchase.submit();
	
	self.close();
}

function SubmitForm() {
	x=document.itemserials;
	
	var SrPurId   = x.PPurId.value;
	var SrSerial  = x.SRItemSerialNo.value;
	var SrWhCode  = x.lstWarehouse.value;
	var SrPerPage = x.perpage.value;
	
	
	SrWhCode = SrWhCode.replace('&','%26');
	
	if (SrWhCode == '') 
		alert("Warehouse is mandatory.");
		
	if (SrSerial != '' && SrWhCode != '')
	{
		x.SRItemSerialNo.disabled = true;
		
		x.action='Form_ReturnPurchaseAddSerial_Actions.php?Mode=insert&ItemSerialNo='+SrSerial+'&PurId='+SrPurId+'&PerPage='+SrPerPage;
		x.submit();
	}	
	
	//x.SRItemSerialNo.focus();
	//document.getElementById('SRItemSerialNo').focus();
	
}

function DeleteSerialNo(SerialId, SerialNo) {
	x=document.itemserials;
	
	var SrPurId   = x.PPurId.value;
	var SrSerial  = x.SRItemSerialNo.value;
	var SrWhCode  = x.lstWarehouse.value;
	var SrPerPage = x.perpage.value;
	
	SrWhCode = SrWhCode.replace('&','%26');
	
	if (confirm ( "Are you sure you want to Delete this record ?") )
	{
		x.action='Form_ReturnPurchaseAddSerial_Actions.php?Mode=delete&ItemSerialNo='+SerialNo+'&SerialId='+SerialId+'&PurId='+SrPurId+'&PerPage='+SrPerPage;
		x.submit();
	}
	
	//x.SRItemSerialNo.focus();
	document.getElementById('SRItemSerialNo').focus();
}


function SetNextPage(startrange, perpage){
	x=document.itemserials;
	
	vPurId = x.PPurId.value;
	//WhCode = WhCode.replace('&','%26');
	
	x.action='Form_ReturnPurchaseAddSerial.php?PPurId='+vPurId+'&startrange='+startrange+'&perpage='+perpage;
	x.submit();
}

function SetPrvPage(startrange, perpage){
	x=document.itemserials;
	
	vPurId = x.PPurId.value;
	//WhCode = WhCode.replace('&','%26');
	
	x.action='Form_ReturnPurchaseAddSerial.php?PPurId='+vPurId+'&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: 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;
}


   
.style16 {
	font-size: 16px;
	color: #FF0000;
	font-weight: bold;
	font-family: "Times New Roman", Times, serif;
}
.style17 {font-size: 9px}

.style26 {color: #FF0000}
.style27 {color: #999999 ;font-size: 10px;}
.style28 {color: #000000 ;font-size: 10px;}

</style>

</head>
<body onLoad='LoadForm();'  class='bodybackground'>
<form name='itemserials' method='post' action=''>
<input type='hidden' name='mainaction' value='$mainaction'>
<input type='hidden' name='actionmode' value='$actionmode'>
<input type='hidden' name='PPurId' value='$PPurId'>
<input type='hidden' name='PPurSupplier' value='$PPurSupplier'>
			
HERE;
	
$query = " SELECT Temp_Purchasesserialno.Serial_ID, Items.Item_Code As ItemCode, Items.Item_Description As ItemDesc, Temp_Purchasesserialno.SerialNo_Code, ".
		 " Temp_Purchasesserialno.Warehouse_Code As WarehouseCode, SerialNo_Used, RelatedSerial_Id, SerialNo_Quantity  ".
		 " FROM Temp_Purchasesserialno ".
		 " Inner Join Items on Temp_Purchasesserialno.Item_Code = Items.Item_Code ".
		 " Inner Join Temp_PurchasesDt on Temp_Purchasesserialno.PurchaseDt_ID = Temp_PurchasesDt.PurchaseDt_ID ".
		 " WHERE Temp_PurchasesDt.Purchase_ID  = '".$PPurId."' ".
		 " AND Temp_PurchasesDt.Journal_Type = 'PR' ".
		 " AND Temp_Purchasesserialno.Journal_Type = 'PR' ".
		 " ORDER BY Serial_ID Desc ";
		 
			 
//echo $query;

$result=mysql_query($query,$connection) or die(mysql_error());
$totalrows = mysql_num_rows($result);


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

echo"<table cellpadding='0' cellspacing='0' border='0'  align='left' style='table-layout:fixed' >";
echo"<tr>
		<td id='search-top1' style='height:34px; width:31px;'>&nbsp;</td>
	  	<td id='search-top2' style='height:34px; width:750px' align='center' valign='middle'>
			<table border='0'>
			<tr><td><span class='style3'>Items and Serials
			</span></td></tr>
			</table>
		</td>
		<td id='search-top3' style='height:34px; width:31px;'>&nbsp;</td>
	</tr>";

echo"<tr><td id='search-content1' style='width:31px;'>&nbsp;</td>
		 <td id='search-content2' >";
		 
echo"<table border='0' cellspacing='1' style='table-layout:fixed' align='center'>
	  <col width=30>
	  <col width=50>
	  <col width=150>
	  <col width=140>
	  <col width=150>
	  <col width=100>
	  <col width=100>
	
	  <tr bgcolor='#DDDDDD'>
		<td align='center' colspan='7'><font size='1' color='#FF0000' face='Verdana'><b>".$message."</b></font></td>
	  </tr>	
	  	
	  <tr class='Search-header-bg'>	
		<td valign='center' align='left' colspan='2'><span class='style4'>Warehouse&nbsp;<span class='style26'>*</span></span></td>";
		
	echo"<td colspan='5'><select name='lstWarehouse' id='lstWarehouse' style='font-size:11px; width:250px; height:18px;' >";
	echo"<option></option>";
	while($wrhf=mysql_fetch_row($resultwarehouse)){
		echo"<option ";
		if ($lstWarehouse==$wrhf[0]) echo " selected ";
		echo" value='$wrhf[0]'>$wrhf[0] - $wrhf[1]</option>";
	}
			
	echo"</select></td>";
	
	
echo" </tr>			
	  <tr class='Search-header-bg'>
	    <td align='left' valign='center' colspan='2'><span class='style4'>Enter Serial No</span></td>
		<td align='left' valign='top' colspan='3'><span class='style4'>
			<input type='text' name='SRItemSerialNo' id='SRItemSerialNo' value='$SRItemSerialNo' style='width:250px;' maxlength='40' onkeydown='return kd(event)'/>
			
			 <button type='button' id='btnSubmit' name='btnSubmit' onclick='SubmitForm(); return false;' title='Submit'><span><span>Submit</span></span></button>
			 <button type='button' id='btnConfirm' name='btnConfirm' onclick='ConfirmForm(); return false;' title='Confirm'><span><span>Confirm</span></span></button>
	</td>
		<td align='right' valign='center' colspan='2'><span class='style4'>Result per page </span><span class='style4'><select name='perpage' id='perpage' value='$perpage' onchange='RefreshPage();'>";
              
			
        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 class='Search-Label-bg'>
	    <td  height='25' align='left'><span class='style4'>&nbsp; </span></td>
		<td  height='25' align='left'><span class='style4'>S/N </span></td>
		<td  height='25' align='center'><span class='style4'>Serial Code </span></td>
		<td  height='25' align='center'><span class='style4'>Item Code </span></td>
		<td  height='25' align='center' colspan='2'><span class='style4'>Item Description </span></td>
		<td  height='25' align='center'><span class='style4'>Warehouse Code </span></td>
	  </tr>";
		

	

	


	$result=mysql_query($query,$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 align="center"><span class="style5"><img src="img/c_delete.png" onclick="DeleteSerialNo('.$Serial_ID.',\''.$SerialNo_Code.'\');" onMouseOver="style.cursor=\'hand\'"  title="Delete Record"  ></span></td>
				<td align="left"><span class="style5">'.$CurrentRow.'</span></td>
				<td align="left"><span class="style5">'.$SerialNo_Code.'</span></td>
				<td align="left"><span class="style5">'.$ItemCode.'</span></td>
				<td align="left" colspan="2"><span class="style5">'.$ItemDesc.'</span></td>
				<td align="center"><span class="style5">'.$WarehouseCode.'</span></td>';
			
		echo'</tr>';		
		
		
		
	}
			
			
		
echo"   <tr><td colspan='7'>&nbsp;</td></tr>
		<tr height='30'><td colspan='7'><table width='100%' border=0><tr><td width='250'>&nbsp;</td><td width='110' align='center'>";
		
		$diff= $startrange - $perpage;
		
		if (($diff) >= 0 ){
			$prvstartrange = $startrange - $perpage;
			echo"<img src='img/arrowleft.png' title='Previous' onMouseOver=\"style.cursor='hand'\"  onclick=SetPrvPage('".$prvstartrange."','".$perpage."')>";
		}
		else
			echo "&nbsp;";
		
		echo"</td><td width='110' align='center'>";
		
		
		if ($startrange <= $totalrows){
			$startrange = $startrange + $perpage;
			if  ($startrange <= $totalrows)
				echo"<img src='img/arrowright.png' title='Next' onMouseOver=\"style.cursor='hand'\" onclick=SetNextPage('".$startrange."','".$perpage."')>";
		} else
			echo "&nbsp;";
			
		echo"</td><td width='250'>&nbsp;</td></tr>";
		echo"</table>";

echo"</td></tr></table><td id='search-content3' style='width:31px;'>&nbsp;</td></tr>
<tr >
		<td id='search-bottom1' style='height:34px; width:31px;'>&nbsp;</td>
		<td id='search-bottom2' style='height:34px; ' valign='top'><td id='search-bottom3' style='height:34px; width:31px;'>&nbsp;</td></tr>";

echo"</table>";
	


          

		  

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

HERE;



?>