<?php

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


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



$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);
}

//Getting Global Values
$frm        = $_POST["frm"]; if ($frm == ""){$frm = $_GET["frm"];}
$item       = $_POST["item"]; if ($item == ""){$item = $_GET["item"];}
$index      = $_POST["index"]; if ($index == ""){$index = $_GET["index"];}
$PJourDtId  = $_POST["PJourDtId"]; if ($PJourDtId == ""){$PJourDtId = $_GET["PJourDtId"];}
$PChkJourId = $_POST["PChkJourId"]; if ($PChkJourId == ""){$PChkJourId = $_GET["PChkJourId"];}
$PSearch    = $_POST["PSearch"]; if ($PSearch == ""){$PSearch = $_GET["PSearch"];}
$startrange = $_POST["startrange"]; if ($startrange == ""){$startrange = $_GET["startrange"];}
$message    = $_POST["message"]; if ($message == ""){$message = $_GET["message"];}
$perpage    = $_POST["perpage"]; if ($perpage == ""){$perpage = $_GET["perpage"];}

		
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>Search Checks</title>
<script type="text/javascript" language="javascript">

function roundNumber(num, dec) { // Arguments: number to round, number of decimal places
	var newnumber = Math.round(num*Math.pow(10,dec))/Math.pow(10,dec);
	newnumber = parseFloat(newnumber); 
  
	return newnumber;
}

function SelectRecord(value1,value2,value3,value4,value5,value6){

	vFrm   = document.search.frm.value;
	vItem  = document.search.item.value;
	vIndex = document.search.index.value; 
	
	value1=decodeURIComponent(value1);
	value2=decodeURIComponent(value2);

	if(vFrm=='1'){
		opener.document.getElementById('chkjournumber['+vIndex+']').value   = value1;
		opener.document.getElementById('chkjourbank['+vIndex+']').value = value2;
		opener.document.getElementById('chkjourvaluedate['+vIndex+']').value = value3;
		opener.document.getElementById('chkjouramount['+vIndex+']').value = roundNumber(value4,2);
		opener.document.getElementById('chkjouramount1['+vIndex+']').value = roundNumber(value5,2);
		opener.document.getElementById('chkjouramount2['+vIndex+']').value = roundNumber(value6,2);
		opener.document.getElementById('chkjouramount_h['+vIndex+']').value = value4;
		opener.document.getElementById('chkjouramount1_h['+vIndex+']').value = value5;
		opener.document.getElementById('chkjouramount2_h['+vIndex+']').value = value6;
		opener.document.getElementById('chkjournumber['+vIndex+']').focus();
	}
	else if(vFrm=='2' && vItem==1){
	
		opener.document.getElementById('chkjournumber['+vIndex+']').value   = value1;
		opener.document.getElementById('chkjourbank['+vIndex+']').value = value2;
		opener.document.getElementById('chkjourvaluedate['+vIndex+']').value = value3;
		opener.document.getElementById('chkjouramount['+vIndex+']').value = roundNumber(value4,2);
		opener.document.getElementById('chkjouramount1['+vIndex+']').value = roundNumber(value5,2);
		opener.document.getElementById('chkjouramount2['+vIndex+']').value = roundNumber(value6,2);
		opener.document.getElementById('chkjouramount_h['+vIndex+']').value = value4;
		opener.document.getElementById('chkjouramount1_h['+vIndex+']').value = value5;
		opener.document.getElementById('chkjouramount2_h['+vIndex+']').value = value6;
		opener.document.getElementById('chkjournumber['+vIndex+']').focus();
	}
	else if(vFrm=='2' && vItem==2){
		 
			
			
		opener.document.getElementById('chkjournumber2['+vIndex+']').value   = value1;
	    opener.document.getElementById('chkjourbank2['+vIndex+']').value = value2;
		opener.document.getElementById('chkjourvaluedate2['+vIndex+']').value = value3;
		opener.document.getElementById('chkjouramount02['+vIndex+']').value = roundNumber(value4,2);
		opener.document.getElementById('chkjouramount21['+vIndex+']').value = roundNumber(value5,2);
		opener.document.getElementById('chkjouramount22['+vIndex+']').value = roundNumber(value6,2);
		opener.document.getElementById('chkjouramount02_h['+vIndex+']').value = value4;
		opener.document.getElementById('chkjouramount21_h['+vIndex+']').value = value5;
	 	opener.document.getElementById('chkjouramount22_h['+vIndex+']').value = value6;  
		opener.document.getElementById('chkjournumber2['+vIndex+']').focus();
	}
	
	self.close();
}


function ClearForm()
{
	vFrm   = document.search.frm.value;
	vItem  = document.search.item.value;
	
	document.search.PSearch.value='';
	document.search.action='List_FrmChecks.php?frm='+vFrm+'&item='+vItem;
	document.search.submit();

}

function RefreshPage()
{
	document.search.action='List_FrmChecks.php?startrange=0';
	document.search.submit();
}

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

}

function SearchKeyword(){
	document.search.action='List_FrmChecks.php?startrange=0';
	document.search.submit();
}

function SetNextPage(startrange,perpage){
	vFrm   = document.search.frm.value;
	vItem  = document.search.item.value;
	
	document.search.action='List_FrmChecks.php?PSearch=&frm='+vFrm+'&item='+vItem+'&startrange='+startrange+'&perpage='+perpage;
	document.search.submit();
}

function SetPrvPage(startrange,perpage){
    vFrm   = document.search.frm.value;
	vItem  = document.search.item.value;
	
	document.search.action='List_FrmChecks.php?PSearch=&frm='+vFrm+'&item='+vItem+'&startrange='+startrange+'&perpage='+perpage;
	document.search.submit();
}

function SetFirstPage(startrange,perpage){
	vFrm   = document.search.frm.value;
	vItem  = document.search.item.value;
	
	document.search.action='List_FrmChecks.php?PSearch=&frm='+vFrm+'&item='+vItem+'&startrange=0&perpage='+perpage;
	document.search.submit();
}

function SetLastPage(perpage,totalrows){
	vFrm   = document.search.frm.value;
	vItem  = document.search.item.value;
	
	var result=totalrows % perpage;
	if (result == 0) result = perpage;
	startrange=totalrows-result;
	document.search.action='List_FrmChecks.php?PSearch=&frm='+vFrm+'&item='+vItem+'&startrange='+startrange+'&perpage='+perpage;
	document.search.submit();
}


</script>
</head>
<body onLoad='LoadForm();' class='bodybackground'>
<form name='search' method='post' action=''>
<input type='hidden' name='item'  value='$item'>
<input type='hidden' name='frm' value='$frm'>
<input type='hidden' name='index' value='$index'>
<input type='hidden' name='PJourDtId' value='$PJourDtId'>
<input type='hidden' name='PChkJourId' value='$PChkJourId'>

HERE;

//Select Data From JournalDt For Selected Record
$sub_query = " Select JournalDt.Ledger_Number, ifNull(Ledger.Ledger_Activity,0), JournalDt.Currency_Code, JournalDt.Journal_ID ".
			 " From Temp_JournalDt As JournalDt Inner Join Ledger on JournalDt.Ledger_Number = Ledger.Ledger_Number ".
	         " Where JournalDt.JournalDt_ID = '".$PJourDtId."' ";
			 


if ($_GET['frm'] == "2") // payment
{
	
	
	         $sub_query = " Select transdt.Ledger_Number , ifNull(Ledger.Ledger_Activity,0), transdt.Currency_Code, transdt.transdt_id ".
			 " From Temp_transdt As transdt Inner Join Ledger on transdt.Ledger_Number = Ledger.Ledger_Number ".
	         " Where transdt.transdt_id = '".$PJourDtId."' ";
			 
			 
	 
}
	
 		 
					      
//echo $sub_query;
$sub_result = selectData($connection, $sub_query);

$LedgerNO = $sub_result[0];
$LedgerActivity = $sub_result[1];
$JourDtCurrCode = $sub_result[2];
$JourID   = $sub_result[3];

$querysearch = " select Chk_increment, Chk_number, Chk_bank, date_format(Chk_ValueDate,'%d/%m/%Y') as Chk_ValueDate, ifNull(Chk_amount,0) as Chk_amount, ".
			   " convertcurfunc(Currency_code, '".$base1."', now(), 0, 0, 0, 0, ifNull(Chk_amount,0)) as Chk_amount1, ".
			   " convertcurfunc(Currency_code, '".$base2."', now(), 0, 0, 0, 0, ifNull(Chk_amount,0)) as Chk_amount2 ".
			   " from Checks ".
               " where ((Journaldt_id2 is null or Journaldt_id2 = 0 or Journaldt_id2 = '') ".
			   " and Checks.Ledger_Number = '".$LedgerNO."' ".
			   " and Checks.Currency_code = '".$JourDtCurrCode."' ".
			   " or Chk_number in ( select Chk_number from chkjour inner join journaldt on chkjour.journaldt_id=journaldt.journaldt_id and journaldt.journal_id='".$JourID."'))  ".
			   " and Chk_number not in ( select Chk_number from temp_chkjour inner join temp_journaldt on temp_chkjour.journaldt_id=temp_journaldt.journaldt_id and temp_journaldt.journal_id='".$JourID."' ".
			   "                         and temp_chkjour.Chk_Increment != '".$PChkJourId."' ) ".
			   " and (Chk_number like ".GetSQLValueString('%'.$PSearch.'%','text')." or Chk_bank like ".GetSQLValueString('%'.$PSearch.'%','text').") ".
			   " order by Chk_number ";
			   
			   
if ($_GET['frm'] == "2")
{
	$querysearch = " select Chk_increment, Chk_number, Chk_bank, date_format(Chk_ValueDate,'%d/%m/%Y') as Chk_ValueDate, ifNull(Chk_amount,0) as Chk_amount, ".
			   " convertcurfunc(Currency_code, '".$base1."', now(), 0, 0, 0, 0, ifNull(Chk_amount,0)) as Chk_amount1, ".
			   " convertcurfunc(Currency_code, '".$base2."', now(), 0, 0, 0, 0, ifNull(Chk_amount,0)) as Chk_amount2 ".
			   " from Checks ".
               " where ((Journaldt_id2 is null or Journaldt_id2 = 0 or Journaldt_id2 = '') ".
			   " and Checks.Ledger_Number = '".$LedgerNO."' ".
			   " and Checks.Currency_code = '".$JourDtCurrCode."' ".
			   " or Chk_number in ( select Chk_number from chkjour inner join journaldt on chkjour.journaldt_id=journaldt.journaldt_id and journaldt.journal_id='".$JourID."'))  ".
			   " and Chk_number not in ( select Chk_number from temp_chkjour inner join temp_journaldt on temp_chkjour.journaldt_id=temp_journaldt.journaldt_id and temp_journaldt.journal_id='".$JourID."' ".
			   "                         and temp_chkjour.Chk_Increment != '".$PChkJourId."' ) ".
			   " and (Chk_number like ".GetSQLValueString('%'.$PSearch.'%','text')." or Chk_bank like ".GetSQLValueString('%'.$PSearch.'%','text').") ".
			   " order by Chk_number ";
			   
			   
}	


 
$resultsearch=mysql_query($querysearch,$connection) or die(mysql_error($connection));
$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($connection));	

echo"<table cellpadding='0' cellspacing='0' border='0'  align='left' style='table-layout:fixed' width='680'>";
echo"<tr>
		<td id='search-top1' style='height:34px; width:31px;'>&nbsp;</td>
	  	<td id='search-top2' style='height:34px; width:618px' align='center' valign='middle'>
			<table border='0'>
			<tr><td><span class='style3'>List of Checks</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' >
			<table border='0' cellspacing='1' style='table-layout:fixed' align='center'>
			  <col width=150>
			  <col width=468>
			  <tr class='Search-header-bg' height='34px'>
				<td align='left'><span class='style4'>Search by Number/Bank</span></td>
				<td align='left'><span class='style4'><input type='text' name='PSearch' id='PSearch' value='$PSearch' style='width:150px;' maxlength='100'/></span>
					<button id='btnFind' name='btnFind' onclick='SearchKeyword(); return false;' title='Find'><span><span>Find</span></span></button>
					<button id='btnCancel' name='btnCancel' onclick='ClearForm(); return false;' title='Clear'><span><span>Clear</span></span></button>
				</td>
			  </tr>";
		
		if($message!=''){
			echo"<tr class='Search-header-bg'>
					<td height='15' align='center' colspan='2' class='message'>".$message."</td>
				</tr>";
		}
		
echo"		</table>
		</td>
		<td id='search-content3' style='width:31px;'>&nbsp;</td>
	</tr>";
		
echo"<tr>
		<td  id='search-content1' style='height:34px;width:31px;'>&nbsp;</td>
		<td  id='search-content2' style='height:34px;width:618px;'>
		<table width='100%' border=0>
		<tr>
			<td align='right' style='width:235px;'><span class='style4'>Per Page </span><span class='style4'><select name='perpage' id='perpage' value='$perpage' onchange='RefreshPage();'>";
              
			if ($perpage==20)
				$s20 = 'selected';
			else if ($perpage==30)
				$s30 = 'selected';
			else if ($perpage==40)
				$s40 = 'selected';
			else if ($perpage==50)
				$s50 = 'selected';

						
							
			echo'<option '.$s20.' value="20">20</option>
				 <option '.$s30.' value="30">30</option>
				 <option '.$s40.' value="40">40</option>
				 <option '.$s50.' value="50">50</option>
				 </select></span></td>
			<td  style="width:74px;border-right:1px solid #000000;text-align:right;">';
			
			if($startrange > 0)
				echo"<img src='img/firstpage.png' title='First Page' onMouseOver=\"style.cursor='hand'\" onclick=SetFirstPage('".$startrange."','".$perpage."')>";

			$diff= $startrange - $perpage;
				
			if (($diff) >= 0 ){
				$prvstartrange = $startrange - $perpage;
				echo"&nbsp;&nbsp;<img src='img/arrowleft.png' title='Previous' onMouseOver=\"style.cursor='hand'\"  onclick=SetPrvPage('".$prvstartrange."','".$perpage."','".$option."')>";
			}
			else
				echo "&nbsp;";
		
echo"		</td><td  align='left' style='width:74px;'>";
		
		
			if ($startrange <= $totalrows){
				$startrange1 = $startrange + $perpage;
				if  ($startrange1 < $totalrows){
					echo"<img src='img/arrowright.png' title='Next' onMouseOver=\"style.cursor='hand'\" onclick=SetNextPage('".$startrange1."','".$perpage."')>";
					echo"&nbsp;&nbsp;<img src='img/lastpage.png' title='Last Page' onMouseOver=\"style.cursor='hand'\" onclick=SetLastPage('".$perpage."','".$totalrows."')>";	
				}
			} else
				echo "&nbsp;";
				
			
echo"		</td><td style='width:235px;'>&nbsp;</td>
		</tr>";
echo"	</table></td>
		<td id='search-content3' style='width:31px;'>&nbsp;</td>";
echo"</tr>";	

echo"<tr><td id='search-content1' style='width:31px;'>&nbsp;</td>
		<td id='search-content2'>
			<div style='overflow:scroll;height:280px;width:618px;'>
			<table border='0' style='table-layout:fixed' cellpadding='0' cellspacing='1'>
			<col width=120>
			<col width=250>
			<col width=90>
			<col width=120>";

echo"		<tr class='Search-Label-bg'>
				<td  height='25' align='center' ><span class='style4'>Number </span></td>
				<td  height='25' align='center'><span class='style4'>Bank </span></td>
				<td  height='25' align='center'><span class='style4'>Value Date </span></td>
				<td  height='25' align='center'><span class='style4'>Amount</span></td>
			</tr>";		
	
		

	


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."'";	
			


	$DCode = rawurlencode($Chk_number);
	$DName = rawurlencode($Chk_bank);
	
	echo"<tr $Color>";
	echo"  <td align='left'><a onclick=\"SelectRecord('$DCode','$DName','$Chk_ValueDate','$Chk_amount','$Chk_amount1','$Chk_amount2');\" href='#'><span class='style5'>$Chk_number</span></a></td>";
	echo"  <td align='left'><span class='style5'>$Chk_bank</span></td>";
	echo"  <td align='center'><span class='style5'>$Chk_ValueDate</span></td>";
	echo"  <td align='right'><span class='style5'>".number_format($Chk_amount,$DecimalBase)."</span></td>";
    echo"</tr>";
	
}

echo"</table></div></td><td id='search-content3' style='width:31px;'>&nbsp;</td></tr>";

echo"   <tr ><td id='search-content1'>&nbsp;</td><td id='search-content2'>&nbsp;</td><td id='search-content3'>&nbsp;</td></tr>";
echo"	<tr >
		<td id='search-bottom1' style='height:34px; width:31px;'>&nbsp;</td>
		<td id='search-bottom2' style='height:34px; width:618px;' valign='top'><table width='100%' border=0 height=28px>
		<tr><td style='width:270px;'>&nbsp;</td><td align='right' style='width:74px;border-right:1px solid #000000;'>";

		
		if($startrange>0)
			echo"<img src='img/firstpage.png' title='First Page' onMouseOver=\"style.cursor='hand'\" onclick=SetFirstPage('".$startrange."','".$perpage."','".$option."')>";

		//$diff= $startrange - $perpage;
		
		if (($diff) >= 0 ){
			//$prvstartrange = $startrange - $perpage;
			echo"&nbsp;&nbsp;<img src='img/arrowleft.png' title='Previous' onMouseOver=\"style.cursor='hand'\"  onclick=SetPrvPage('".$prvstartrange."','".$perpage."','".$option."')>";
		}
		else
			echo "&nbsp;";
		
		echo"</td><td  align='left' style='width:74px;'>";
		
		
		if ($startrange <= $totalrows){
			//$startrange = $startrange + $perpage;
			if  ($startrange1 < $totalrows){
				echo"<img src='img/arrowright.png' title='Next' onMouseOver=\"style.cursor='hand'\" onclick=SetNextPage('".$startrange1."','".$perpage."')>";
				echo"&nbsp;&nbsp;<img src='img/lastpage.png' title='Last Page' onMouseOver=\"style.cursor='hand'\" onclick=SetLastPage('".$perpage."','".$totalrows."')>";	
			}
		} else
			echo "&nbsp;";
	
			
		echo"</td><td style='width:270px;'>&nbsp;</td></tr>";
		echo"</table></td><td id='search-bottom3' style='height:34px; width:31px;'>&nbsp;</td>";

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

		  

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

HERE;

?>