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

if(isset($_GET['type']))
{
   $transtyp=$_GET['type'];

   $qr=mysqli_query($db,"select * from transactmast where brcode='$brcode' and trnsid='$transtyp'");
   $countno=mysqli_num_rows($qr);

   if($countno>0)
   {
      $qurow=mysqli_fetch_array($qr);
      $trnrqr=$qurow[3];
      if($trnrqr=='1')
      {
         $sql="select col3,col4,col5,col6,col7,col8,col9,col10 from reportmast where brcode='$brcode' and trnstype='$transtyp'";
//echo $sql;
         $res=mysqli_query($db,$sql);
         $rw=mysqli_fetch_array($res);

         $re=mysqli_query($db,"select * from $tbl2 where led like '$inno%' and brcode='$brcode' and grp IN('$rw[0]','$rw[1]','$rw[2]','$rw[3]','$rw[4]','$rw[5]','$rw[6]','$rw[7]') ORDER BY led asc limit 10") or die("ERR6:".mysqli_error($db));
      }
      else
      {
         $re=mysqli_query($db,"select * from $tbl2 where led like '$inno%' and brcode='$brcode' ORDER BY led asc limit 10") or die("ERR6:".mysqli_error($db));

      }
   }
   else
   {
      $re=mysqli_query($db,"select * from $tbl2 where brcode='$brcode' and led like '%$inno%' ORDER BY led asc limit 10") or die("ERR6:".mysqli_error($db));
$sql="select * from $tbl2 where brcode='$brcode' and led like '$inno%' ORDER BY led asc limit 10";
//echo $sql;
   }


}
else
{

  $re=mysqli_query($db,"select * from $tbl2 where led like '$inno%' and brcode='$brcode' ORDER BY led asc limit 10") or die("ERR6:".mysqli_error($db));
}

$json="";
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];
 }
$crname=$row[0];
echo $crname;
echo "<br>";

$partcv=explode(" ",$crname);
print_r($partcv);
echo "<br>";

$part1=trim($partcv[0]);
$part2=trim($partcv[1]);

$part3=trim($partcv[2]);
$part4=trim($partcv[3]);
$crname=$part1.'$'.$part2.'$'.$part3.'$'.$part4;
$crname=str_replace(' $ ','',$crname); 
//$crname=$part1.$part2.$part3.$part4;
//$neprtcd=$partcv[0].'%20'.$partcv[1];

  //$crname= str_replace('    ',' ',$crname);
//$crname=preg_replace('/[\s]+/','',$crname);
//$crname= preg_replace('!\s+!', ' ', $crname);
//$crname=str_replace('    ','',$crname); 
//echo "<br>";
echo $crname; 
echo "<br>";
$json.= '{"cr":"'.$crname.'","stock":"'.$test.'","cbal":"'.$bal.'","rate":"'.addslashes($row[18]).'","reduct":"'.addslashes($row[8]).'","tax":"'.addslashes($row[5]).'"},';

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