<?php
session_start();

include("config.inc.php");
include("Functions_Sales.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 DecimalBase, DecimalBase1, DecimalBase2, base1, base2, grid_color1, grid_color2 from setup";
$resultsetup=mysql_query($querysetup,$connection) or die(mysql_error());
while($rows=mysql_fetch_array($resultsetup)){
extract($rows);

}

/// Currency Rates

	$query = "SELECT Curdt_Rate FROM curdt WHERE Currency_Code = '".$chk_currency."' AND Curdt_date = (SELECT MAX(Curdt_date) FROM curdt WHERE Currency_Code = '".$chk_currency."' )";
	$result = selectData($connection, $query);
	$rate_dt = $result[0];
	
	$query = "SELECT Curdt_Rate FROM curdt WHERE Currency_Code = '".$base1."' AND Curdt_date = (SELECT MAX(Curdt_date) FROM curdt WHERE Currency_Code = '".$base1."' )";
	$result = selectData($connection, $query);
	$rate_dt1 = $result[0];
	
	$query = "SELECT Curdt_Rate FROM curdt WHERE Currency_Code = '".$base2."' AND Curdt_date = (SELECT MAX(Curdt_date) FROM curdt WHERE Currency_Code = '".$base2."' )";
	$result = selectData($connection, $query);
	$rate_dt2 = $result[0];		
	
	
if(empty($bill_increment))
	$bill_increment=0;
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>List of Bills</title>
<script type="text/javascript" language="javascript">

function clearform()
{
  document.form1.PLedger_number.value='';
  document.form1.action='List_Bills.php';
  document.form1.submit();

}
</script>
<style type="text/css">
<!--
body,td,th {
	font-family: Verdana, Arial, Helvetica, sans-serif;
	font-size: 14px;
}
.style1 {
	color: #333333;
	font-weight: bold;
}
.style2 {
	color: #999999;
	font-weight: bold;
}
.style3 {color: #999999}
-->
</style></head>

<body>
<form id="form1" name="form1" method="post" action="">
  <table width="800" border="0" align="center">
    <tr>
      <th colspan="4" bgcolor="#DDDDDD"><span class="style1"> Checks</span></th>
    </tr>
    <tr>
      <td bgcolor="#DDDDDD"> <span style="font-size: 11px; color: #999999; font-weight: bold;">Search for Checks <br> Number / Bank</span></td>
      <td bgcolor="#DDDDDD" colspan='3'><input type="text" name="PLedger_number" value="<?php echo $PLedger_number; ?>"  />
        <input name="code1" type="hidden" id="code1"  value="<?php echo $lcode; ?> ">
	    <input name="bill_currency" type="hidden" id="bill_currency"  value="<?php echo $bill_currency; ?> "> 
		<input name="ledger_number" type="hidden" id="ledger_number"  value="<?php echo $ledger_number; ?> "> 
	    <input name="activity_num" type="hidden" id="activity_num"  value="<?php echo $activity_num; ?> "> 
		<input name="increment" type="hidden" id="increment" value="<?php echo $increment; ?>" />
        <input name="desc1" type="hidden" id="desc1" value="<?php echo $desc1; ?>" />
		<input name="amount" type="hidden" id="amount" value="<?php echo $amount; ?>" />
		<input name="amount1" type="hidden" id="amount1" value="<?php echo $amount1; ?>" />
		<input name="amount2" type="hidden" id="amount2" value="<?php echo $amount2; ?>" />
		<input type='hidden' name='base2'  value="<?php echo $base2;?>">
		<input type='hidden' name='base1'  value="<?php echo $base1;?>">
		<input type='hidden' name='DecimalBase1'  value="<?php echo $DecimalBase1;?>">
		<input type='hidden' name='DecimalBase2'  value="<?php echo $DecimalBase2;?>">
		<input type='hidden' name='rate_dt'  value="<?php echo $rate_dt;?>">
		<input type='hidden' name='rate_dt1'  value="<?php echo $rate_dt1;?>">
		<input type='hidden' name='rate_dt2'  value="<?php echo $rate_dt2;?>">
		<input name="valuedate" type="hidden" id="valuedate" value="<?php echo $valuedate; ?>" />
		<input name="journaldt_id" type="hidden" id="journaldt_id" value="<?php echo $journaldt_id; ?>" />
		<input name="journal_id" type="hidden" id="journal_id" value="<?php echo $journal_id; ?>" />
		<input name="chk_increment" type="hidden" id="chk_increment" value="<?php echo $chk_increment; ?>" />
	    <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="cancelbtn" value="Cancel" onclick='clearform();'style=' background-color:#4069A7; color:#FFFFFF;font-family:Verdana, Arial, Helvetica, sans-serif; font-size:12px;'/></td>
   
    </tr>
<?php if($post=="1") { ?>
	
<script type="text/javascript" language="javascript">

x=document.form1;

opener.document.bill.bill_number.value=document.form1.code1.value;
opener.document.bill.bill_bank.value=document.form1.desc1.value;
opener.document.bill.bill_Amount.value=document.form1.amount.value;

var dec1=x.DecimalBase1.value;
var dec2=x.DecimalBase2.value;
	
dec1=parseInt(dec1);
dec2=parseInt(dec2);
	
	
var amount1=document.form1.amount1.value;
var amount1=Math.round((amount1)*Math.pow(10,dec1))/Math.pow(10,dec1);

var amount2=document.form1.amount2.value;
var amount2=Math.round((amount2)*Math.pow(10,dec2))/Math.pow(10,dec2);

opener.document.bill.bill_Amount1.value=amount1;
opener.document.bill.bill_Amount2.value=amount2;
opener.document.bill.billjour_amountnotrounded.value=document.form1.amount.value;
opener.document.bill.billjour_amountnotrounded1.value=document.form1.amount1.value;
opener.document.bill.billjour_amountnotrounded2.value=document.form1.amount2.value;
opener.document.bill.bill_valuedate.value=document.form1.valuedate.value;

self.close();

</script>
<?php } ?>
    <tr bgcolor="#E5E5E5">
      <td width="136"><span class="style2">Bill Number </span></td>
      <td width="411" colspan="3"><span class="style2">Bank </span></td>
  
    </tr>
<?php



$query_bills=" select bill_increment,bill_number,bill_bank,bill_amount,date_format(bill_ValueDate,'%d-%m-%Y'),bill_amount1,bill_amount2 from bills";
		
$query_bills=$query_bills." where ((Journaldt_id2 is null or Journaldt_id2=0 ) and ledger_number='".$ledger_number."' and Ledger_Activity=".$activity_num." and 
			Currency_code='".$bill_currency."'" ;
	///		and ";
			
$query_bills=$query_bills. " or bills.bill_number  in (select bill_number from billjour,journaldt where billjour.journaldt_id=journaldt.journaldt_id
			and journaldt.journal_id=".$journal_id." 
			)) and (bill_number like"."('%".$PLedger_number."%') or bill_bank like"."('%".$PLedger_number."%'))
			and bills.bill_number not in (select bill_number from temp_billjour,temp_journaldt where temp_billjour.journaldt_id=temp_journaldt.journaldt_id
			and temp_journaldt.journal_id=".$journal_id." and bill_increment<>$bill_increment)";
			

	
$query_bills=$query_bills." order by bill_number";

echo $query_bills;
$result_bills=mysql_query($query_bills,$connection);
while($rows=mysql_fetch_array($result_bills)){


$ct+=1;

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

?>
    <tr <?php echo $Color; 
	
	
	?>>
	
	 <?php
		$desc=ereg_replace("'","%27",$rows[2]);
		?>
		
		
     <td align="left"><a href="List_Bills.php?valuedate=<?php echo $rows[4]; ?>&amount2=<?php echo $rows[6]; ?>&amount1=<?php echo $rows[5]; ?>&amount=<?php echo $rows[3]; ?>&post=1&lcode=<?php echo $rows[1]; ?>&desc1=<?php echo $desc; ?>"><?php echo $rows[1]; ?></a></td>
     <td colspan="1"><a href="List_Bills.php?valuedate=<?php echo $rows[4]; ?>&amount2=<?php echo $rows[6]; ?>&amount1=<?php echo $rows[5]; ?>&amount=<?php echo $rows[3]; ?>&post=1&lcode=<?php echo $rows[1]; ?>&desc1=<?php echo $desc; ?>"><?php echo $rows[2]; ?></a></td>
	 <td colspan="1"><a href="List_Bills.php?valuedate=<?php echo $rows[4]; ?>&amount2=<?php echo $rows[6]; ?>&amount1=<?php echo $rows[5]; ?>&amount=<?php echo $rows[3]; ?>&post=1&lcode=<?php echo $rows[1]; ?>&desc1=<?php echo $desc; ?>"><?php echo $rows[3]; ?></a></td>
	 <td colspan="1"><a href="List_Bills.php?valuedate=<?php echo $rows[4]; ?>&amount2=<?php echo $rows[6]; ?>&amount1=<?php echo $rows[5]; ?>&amount=<?php echo $rows[3]; ?>&post=1&lcode=<?php echo $rows[1]; ?>&desc1=<?php echo $desc; ?>"><?php echo $rows[4]; ?></a></td>
	</tr>
	
<?php }?>
  </table>
</form>
</body>
</html>
