<?
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;
}
$inno=$_GET['key'];
$json="";
                //*****Equal inno********
               
$re=mysqli_query($db,"select * from ledger where led='$inno' and brcode='$brcode' and grp!='Cash in hand' and grp!='BALANCE WITH BANKS' order by led asc limit 1") or die("ERR6:".mysqli_error($db));

//$re=mysqli_query($db,"select * from ledger where led='$inno' and brcode='$brcode' and grp!='Cash in hand' and led not like 'valvoline%' order by led asc limit 1") or die("ERR6:".mysqli_error($db));
//$re=mysqli_query($db,"select * from ledger where led like '%$inno%' and brcode='$brcode' and grp!='Cash in hand'  order by led asc limit 9") 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.= '{"cr":"'.$row[0].'","stock":"'.$test.'","crcbal":"'.$bal.'","rate":"'.addslashes($row[18]).'","reduct":"'.addslashes($row[8]).'","tax":"'.addslashes($row[5]).'"},';
}                
                //*****like inno********

//$re=mysqli_query($db,"select * from ledger where led like '%$inno%' and brcode='$brcode' and grp!='Cash in hand' and grp!='BALANCE WITH BANKS' order by led asc limit 9") or die("ERR6:".mysqli_error($db));

$re=mysqli_query($db,"select * from ledger where led like '%$inno%' and brcode='$brcode' and grp!='Cash in hand'  order by led asc limit 9") 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];

 }
    $test="";
$json.= '{"cr":"'.$row[0].'","stock":"'.$test.'","crcbal":"'.$bal.'","rate":"'.addslashes($row[18]).'","reduct":"'.addslashes($row[8]).'","tax":"'.addslashes($row[5]).'"},';

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