<?php

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


include("config.inc.php");
include("Functions_Trans.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");


$queryledger= " select distinct ledger_number, ledger_include ".
              " from ".$HTTP_SESSION_VARS['dbLabel'].".ledgerstk  ".
			  " where user_account='".$HTTP_SESSION_VARS['useraccount']."' ".
			  " order by ledger_number";
$ledgerresult = mysql_query($queryledger,$connection) or die(mysql_error());
$ledgerexist  = mysql_num_rows($ledgerresult);	
$rows=mysql_fetch_array($ledgerresult) ;
$ledgerinclude = $rows[1];

$querysetup="select DecimalBase, DecimalBase1, DecimalBase2, 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);
}

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

$querylog=" select ifNull(login_belongstogrp,0) As FI_BelongsToGrp ".
          " from ".$HTTP_SESSION_VARS['dbLabel'].".login where user_account='".$HTTP_SESSION_VARS['useraccount']."' and Upper(Login_Schema) = Upper('".$HTTP_SESSION_VARS['dbName']."')  ";
$resultlog=mysql_query($querylog,$connection) or die(mysql_error());
while($rows=mysql_fetch_array($resultlog)){
	extract($rows);
}


	$title='Search Manual No';
	$label='List of Manual No';

		
		
print <<<HERE
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />

<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />


<title>$title</title>
<script type="text/javascript" language="javascript">


function SelectRecord(value1){

	str=document.search.str.value;
	var vIndex	= document.search.index.value; 

	if(str=='1'){
		opener.document.transdt.txtTransDtManualNo.value=value1;
		opener.document.transdt.submit();
	}
	else if(str=='2'){
	 	opener.document.salesjvdt.txtDtManualNo.value=value1;
		//opener.document.salesjvdt.submit();
	}
	else if(str=='3'){
	 	opener.document.purchasesjvdt.txtDtManualNo.value=value1;
		//opener.document.purchasesjvdt.submit();
	}
	else if (str == 4){
		opener.document.getElementById('transdtmanualno['+vIndex+']').value=value1;
		opener.document.getElementById('transdtmanualno['+vIndex+']').focus();
	}
	else if (str == 5){
		opener.document.getElementById('transdtmanualno2['+vIndex+']').value=value1;
		opener.document.getElementById('transdtmanualno2['+vIndex+']').focus();
	}
	else if (str == 6){ 
		opener.document.getElementById('txtDtManualNo['+vIndex+']').value=value1;
		opener.document.getElementById('txtDtManualNo['+vIndex+']').focus();
	} 
	self.close();
}

function clearform()
{
  document.search.PRef.value='';
  document.search.action='Form_Trans_ListManualNo.php';
  document.search.submit();

}

function LoadForm() 
{
	x=document.search;
	x.PRef.focus();
	

}

function SetNextPage(startrange,perpage){
	document.search.action='Form_Trans_ListManualNo.php?PRef=&startrange='+startrange+'&perpage='+perpage;
	document.search.submit();
}

function SetPrvPage(startrange,perpage){
	document.search.action='Form_Trans_ListManualNo.php?PRef=&startrange='+startrange+'&perpage='+perpage;
	document.search.submit();
}




</script>

<style type="text/css">

body,td,th {
	font-family: Verdana, Arial, Helvetica, sans-serif;
	font-size: 12px;
}
body {
	margin-top: 2px;
	margin-left: 1px;
}



.style11 {color: #999999; font-weight: bold; }
.style12 {font-size: 11px; color: #000000;}
.style15 {color: #5151FF; }
.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 >
<form name='search' method='post' action=''>
<input type='hidden' name='str'  value='$str'>
<input type='hidden' name='PLedgerNo' value='$PLedgerNo'>
<input type='hidden' name='PTransType' value='$PTransType'>
<input type='hidden' name='index' value='$index'>


HERE;

echo"<table border='0' cellspacing=1 style='table-layout:fixed; width:700px;'>
	  <col width='130'>
	  <col width='50'>
	  <col width='50'>
	  <col width='120'>
	  <col width='100'>
	  <col width='250'>
		     
	  <tr bgcolor='#DDDDDD'>
	    <td align='center' colspan='6'><span class='style16'>$label</span></td></tr>
	  <tr bgcolor='#DDDDDD' height=30px>
	    <td align='left'><span class='style11'>Search</span></td>
		<td align='left' colspan='5'><span class='style11'><input type='text' name='PRef' id='PRef' value='$PRef' size='15' maxlength='30'/><input type='hidden' name='PMode' id='PMode' value='$PMode' size='15' maxlength='30'/></span>
		    <input type='submit' name='Submit' value='Find' 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;'/></td>
		";
echo" </tr>
	  <tr bgcolor='#DDDDDD'>
		<td height='15' align='center' colspan='6'><font size='1' color='#FF0000' face='Verdana'><b>".$message."</b></font></td>
	  </tr>	
	  <tr bgcolor='#CCD8E6'>
		<td  height='25' align='center'><span class='style25'>Manual No</span></td>
		<td  height='25' align='center'><span class='style25'>D/C</span></td>
		<td  height='25' align='center'><span class='style25'>Cur.</span></td>
		<td  height='25' align='center'><span class='style25'>Amount</span></td>
		<td  height='25' align='center'><span class='style25'>Value Date</span></td>
		<td  height='25' align='left'><span class='style25'>Description</span></td>
	  </tr>";
		


	
$querysearch=" Select JournalDt.Manual_No, Currency_code, Journaldt_amount, Journaldt_dbcd, Journaldt_desc, date_format(Journaldt_ValueDate,'%d/%m/%Y') ".
			 " From JournalDt ".
			 " Inner Join ( ".
			 " 		Select JournalDt.Manual_No, Round(Sum(if(Journaldt_dbcd='D',ifNull(Journaldt_amount1,0),0)),2) As DebitAmount, ".
			 "		Round(Sum(if(Journaldt_dbcd='C',ifNull(Journaldt_amount1,0),0)),2) As CreditAmount ".
			 "		From JournalDt".
			 " 		Where JournalDt.Manual_No Is Not Null and JournalDt.Manual_No != '' ".
			 " 		And JournalDt.ledger_number = '".$PLedgerNo."' ".
			 " 		Group By JournalDt.Manual_No ".
			 " 		Having DebitAmount != CreditAmount ". 
			 "  ) as Q1 on Q1.Manual_No = JournalDt.Manual_No ".
             " Where JournalDt.ledger_number = '".$PLedgerNo."' ".
			 " And JournalDt.Manual_No Is Not Null and JournalDt.Manual_No != '' ";

	
if ($Module_BelongsToGroup == 1)			 
	$querysearch= $querysearch." And ifNull(JournalDt.Journal_BelongsToGroup,0) >= ifNull($FI_BelongsToGrp,0) ";
	
$querysearch=$querysearch." and JournalDt.Manual_No like"."('%".$PRef."%') order by JournalDt.Manual_No ";

//echo $querysearch;
$resultsearch=mysql_query($querysearch,$connection) or die(mysql_error());
$totalrows = mysql_num_rows($resultsearch);
	
if ($startrange=='')
	$startrange = 0;
if ($perpage=='')
	$perpage = 20;
		
$querysearch = $querysearch." limit ".$startrange.",".$perpage;
$resultsearch=mysql_query($querysearch,$connection) or die(mysql_error());		


while($rows=mysql_fetch_array($resultsearch)){
		
	extract($rows);
	$ct+=1;

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

	$desc=ereg_replace("'","%27",$rows[1]);

	echo"<tr $Color>";
    echo"	<td class='style12' align='left' valign='top'><a href='javascript:void();' onclick=\"SelectRecord('$rows[0]');\" >$rows[0]</a></td>";
	echo"	<td class='style12' align='center' valign='top'>$rows[3]</td>";
	echo"	<td class='style12' align='center' valign='top'>$rows[1]</td>";
	echo"	<td class='style12' align='right' valign='top'>".number_format($rows[2], 2)."</td>";
    echo"	<td class='style12' align='center' valign='top'>$rows[5]</td>";
	echo"	<td class='style12' align='left' valign='top'>$rows[4]</td>";
    echo"</tr>";


}
							
	
		
echo"   <tr><td colspan='6'>&nbsp;</td></tr>
		<tr><td colspan='6'><table width='100%' border=0><tr><td width='250'>&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='250'>&nbsp;</td></tr>";
		echo"<table>";

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


          

		  

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

HERE;

?>