<?
session_start();
include_once('opendb.php');
$brcode=$_SESSION['brcode'];
$user=$_SESSION['user'];
if(strlen($brcode)<=0)
{
 print "<script> alert('Session timed out'); parent.document.location='index.php'; </script>";
 return;
}
$tbl2="ledger";
$inno=$_GET['key'];
$json="";

                //*****Equal inno********
                
//$re=mysqli_query($db,"select * from $tbl2 where led='$inno' and brcode='$brcode' and grp!='Cash in hand' and grp!='BALANCE WITH BANKS' and led not like 'valvoline%'  order by led asc limit 10") or die("ERR6:".mysqli_error($db));
$re=mysqli_query($db,"select * from $tbl2 where led='$inno' and brcode='$brcode' and grp!='Cash in hand' and grp!='BALANCE WITH BANKS'   order by led asc limit 10") or die("ERR6:".mysqli_error($db));
$row=mysqli_fetch_row($re);
if($row[0]>'')
{
$balres=mysqli_query($db,"select sum(dramt),sum(cramt) from journal2 where brcode='$brcode' and partic='$row[0]'");
 $balrow=mysqli_fetch_row($balres);
 if($row['drcr']=='Dr')
 {
 $bal=($balrow[0]+$row[2])-$balrow[1]; 
 }
 else
 {
 $bal=$balrow[1]+$row[2]-$balrow[0];
 }
    
$json.= '{"dr":"'.$row[0].'","stock":"'.$test.'","drcbal":"'.$bal.'","rate":"'.addslashes($row[18]).'","reduct":"'.addslashes($row[8]).'","tax":"'.addslashes($row[5]).'"},';
} 

            //*******like inno********
            
//$re=mysqli_query($db,"select * from $tbl2 where led like '%$inno%' and brcode='$brcode' and grp!='Cash in hand' and led not like 'valvoline%'  order by led asc limit 10") or die("ERR6:".mysqli_error($db));
$re=mysqli_query($db,"select * from $tbl2 where led like '%$inno%' and brcode='$brcode' and grp!='Cash in hand' and led not in('T D S RECEIVABLE','TDS A.Y 2023-24','TDS A.Y 2024-25','TDS A.Y 2024-25','TDS A.Y 2025-26') order by led asc limit 10") or die("ERR6:".mysqli_error($db));
while($row=mysqli_fetch_array($re))
{
$balres=mysqli_query($db,"select sum(dramt),sum(cramt) from journal2 where brcode='$brcode' and partic='$row[0]'");
 $balrow=mysqli_fetch_row($balres);
 if($row['drcr']=='Dr')
 {
 $bal=($balrow[0]+$row[2])-$balrow[1]; 
 }
 else
 {
 $bal=$balrow[1]+$row[2]-$balrow[0];
 }
    
$json.= '{"dr":"'.$row[0].'","stock":"'.$test.'","drcbal":"'.$bal.'","rate":"'.addslashes($row[18]).'","reduct":"'.addslashes($row[8]).'","tax":"'.addslashes($row[5]).'"},';

}
echo '['.trim($json,',').']';
?>
