<?
session_start();
$brcode=$_SESSION['brcode'];
$user=$_SESSION['user'];
if(strlen($brcode)<=0)
{
 print "<script> alert('Session timed out'); parent.document.location='inde.php'; </script>";
 return;
}
include("../opendb.php");
include("../test.php");
date_default_timezone_set("Asia/Kolkata");
$recrdid=$_REQUEST['recrdid'];
?>
<html>
<script type="text/javascript" src="../js/jquery.js"></script>
<script type="text/javascript" src="../jquery/jquery-1.7.min.js"></script>
<script type="text/javascript" src="../jquery/jquery-ui.min.js"></script>
<link type="text/css" href="../jquery/jquery-ui.min.css" rel="stylesheet" />	
<link rel="stylesheet" href="../css/form.css" type="text/css" /> 

<script type="text/javascript">
$(function(){
  $(".exphd").live("keydown.autocomplete",function(){
  $led = $(this);
  $row = $led.parent().parent();
  $(this).autocomplete({
    source: function( request, response ) {
    $.ajax({
      url: "../searchled_new.php",
      dataType: "json",
      data: "key="+$led.val(),
      success: function( data ) {
        response( $.map( data, function( item ) {
          return {
            label: '<span class="la-code">'+item.led+' </span><span class="la-name">'+  item.partname + '</span>',
            value: item.led,
            name:item.partname,
            }
        }));
      }
    });
  }
  ,
  minLength: 2,
  select: function( event, ui ) {
  $row.find('.led').val(ui.item.value);
  $row.find('.stok').val(ui.item.stock);
  $row.find('.qty').focus();
  /*log( ui.item ?
  "Selected: " + ui.item.label :
  "Nothing selected, input was " + this.value);
  */
   }

  }).data("ui-autocomplete")._renderItem = function (ul, item) {
         return $("<li></li>")
             .data("item.autocomplete", item)
             .append( item.label )
             .appendTo(ul);
     };
  });
});
</script>
<style>
    .la-code {
    display: inline-block;
    width: 200px;
}
</style>
<?
/*
if(isset($_POST['save']))
{
    
    $recrdid=$_REQUEST['recrdid']; 
    $customer_name1=$_REQUEST['customer_name1']; 
    if($customer_name1>'')
    {
    
    $customer_name2=$_REQUEST['customer_name2']; 
    $customer_name3=$_REQUEST['customer_name3']; 
    $customer_name4=$_REQUEST['customer_name4']; 
    $customer_name5=$_REQUEST['customer_name5']; 
  
    $narration=$_REQUEST['customer_address']; 
    $banks=$_REQUEST['banks']; 
    $invre=mysqli_query($db,"select * from psnindia_gen4psnauto_psn_psndb.upentry where brcode='PSN236' and id='$recrdid'");
    $cr_row=mysqli_fetch_array($invre);
    $dt=$cr_row['date'];
    $mref=$cr_row['partic'];
    
    
   
    
 
    $resbp=mysqli_query($db,"select max(bpno) from psnindia_psndbdn.bnkpay where brcode='$brcode'");
    $rowbp=mysqli_fetch_row($resbp);
    if($rowbp[0]<=0)
    {
        $crnodn=1;
    }
    else
    {
        $crnodn=($rowbp[0]+1);
    }
   
    $rsdn=mysqli_query($db,"select max(brno) from psnindia_psndbdn.bnkrec where brcode='$brcode'");
$cr_rowdn=mysqli_fetch_row($rsdn);
if($cr_rowdn[0]<=0)
{
   $brnodn=1;
}
 else  
 {
   $brnodn=$cr_rowdn[0]+1;
 }
 
 
$resdn=mysqli_query($db,"select max(jvno) from psnindia_psndbdn.journal2 where brcode='$brcode'");

 $rowdn=mysqli_fetch_row($resdn);
 if($rowdn[0]<=0)
 {
    $jvnodn=1;
 }
 else
 {
    $jvnodn=($rowdn[0]+1);
 }

   
    
    $brqry=mysqli_query($db,"select brname from branch where brcode='$brcode'");
    $brqrow=mysqli_fetch_array($brqry);
    $brname=$brqrow[0];
    
    $cr_again='Bank Stmnt';
    
    $allocdate=date("Y-m-d");
    $entrytime=date("Y-m-d H:i:s");
    $customer_amt1=$_REQUEST['customer_amt1'];
    $customer_amt2=$_REQUEST['customer_amt2'];
    $customer_amt3=$_REQUEST['customer_amt3'];
    $customer_amt4=$_REQUEST['customer_amt4'];
    $customer_amt5=$_REQUEST['customer_amt5'];
   
    if($cr_row['dramt']>0)
    {
        $totcramt=$cr_row['dramt'];
        
       
        if($customer_amt1>0)
        {
            $ledqry1=mysqli_query($db,"select led from ledger where brcode='$brcode' and led='$customer_name1'");
            $lednum1=mysqli_num_rows($ledqry1);
            
            if($lednum1>0)
            {
                $sqldn="insert into psnindia_psndbdn.journal2(date,jvno,type,partic,dramt,cramt,narr,brcode,j_type,against,no,tr_no) values('$dt','$jvnodn','Dr','$customer_name1','$customer_amt1','0','$narration','$brcode','BP','$banks','$mref','$crnodn')";
                mysqli_query($db,$sqldn) or die(mysqli_error());
                
                
                $sql="insert into psnindia_psndbdn.bnkpay(date,j_type,type,partic,dramt,cramt,narr,brcode,bpno,against,no) values('$dt','$jvnodn','Dr','$customer_name1','$customer_amt1','0','$narration','$brcode','$crnodn','$banks','$mref')";
                //echo $sql;
                //echo "<br>";
                mysqli_query($db,$sql) or die(mysqli_error($db));
                
            }
            else
            {
                print "<script>alert('Invalid customer $customer_name1')</script>";
            }
        }
        if($customer_amt2>0)
        {
            
            $ledqry2=mysqli_query($db,"select led from ledger where brcode='$brcode' and led='$customer_name2'");
            $lednum2=mysqli_num_rows($ledqry2);
            
            if($lednum2>0)
            {
                
                $sqldn="insert into psnindia_psndbdn.journal2(date,jvno,type,partic,dramt,cramt,narr,brcode,j_type,against,no,tr_no) values('$dt','$jvnodn','Dr','$customer_name2','$customer_amt2','0','$narration','$brcode','BP','$banks','$mref','$crnodn')";
                mysqli_query($db,$sqldn) or die(mysqli_error());
                $sql="insert into psnindia_psndbdn.bnkpay(date,j_type,type,partic,dramt,cramt,narr,brcode,bpno,against,no) values('$dt','$jvnodn','Dr','$customer_name2','$customer_amt2','0','$narration','$brcode','$crnodn','$banks','$mref')";
                //echo $sql;
                //echo "<br>";
                mysqli_query($db,$sql) or die(mysqli_error($db));
                
                
            }
            else
            {
                print "<script>alert('Invalid customer $customer_name2')</script>";
            }
        }
        if($customer_amt3>0)
        {
            $ledqry3=mysqli_query($db,"select led from ledger where brcode='$brcode' and led='$customer_name3'");
            $lednum3=mysqli_num_rows($ledqry3);
            
            if($lednum3>0)
            {
                
                 $sqldn="insert into psnindia_psndbdn.journal2(date,jvno,type,partic,dramt,cramt,narr,brcode,j_type,against,no,tr_no) values('$dt','$jvnodn','Dr','$customer_name3','$customer_amt3','0','$narration','$brcode','BP','$banks','$mref','$crnodn')";
                mysqli_query($db,$sqldn) or die(mysqli_error());
                $sql="insert into psnindia_psndbdn.bnkpay(date,j_type,type,partic,dramt,cramt,narr,brcode,bpno,against,no) values('$dt','$jvnodn','Dr','$customer_name3','$customer_amt3','0','$narration','$brcode','$crnodn','$banks','$mref')";
                //echo $sql;
                //echo "<br>";
                mysqli_query($db,$sql) or die(mysqli_error($db));
                
                
            }
            else
            {
                print "<script>alert('Invalid customer $customer_name3')</script>";
            }
        }
        if($customer_amt4>0)
        {
            $ledqry4=mysqli_query($db,"select led from ledger where brcode='$brcode' and led='$customer_name4'");
            $lednum4=mysqli_num_rows($ledqry4);
            
            if($lednum4>0)
            {
                
                $sqldn="insert into psnindia_psndbdn.journal2(date,jvno,type,partic,dramt,cramt,narr,brcode,j_type,against,no,tr_no) values('$dt','$jvnodn','Dr','$customer_name4','$customer_amt4','0','$narration','$brcode','BP','$banks','$mref','$crnodn')";
                mysqli_query($db,$sqldn) or die(mysqli_error());
                $sql="insert into psnindia_psndbdn.bnkpay(date,j_type,type,partic,dramt,cramt,narr,brcode,bpno,against,no) values('$dt','$jvnodn','Dr','$customer_name4','$customer_amt4','0','$narration','$brcode','$crnodn','$banks','$mref')";
                //echo $sql;
                //echo "<br>";
                mysqli_query($db,$sql) or die(mysqli_error($db));
                
            }
            else
            {
                print "<script>alert('Invalid customer $customer_name4')</script>";
            }
        }
        if($customer_amt5>0)
        {
            $ledqry5=mysqli_query($db,"select led from ledger where brcode='$brcode' and led='$customer_name5'");
            $lednum5=mysqli_num_rows($ledqry5);
            
            if($lednum5>0)
            {
                
                $sqldn="insert into psnindia_psndbdn.journal2(date,jvno,type,partic,dramt,cramt,narr,brcode,j_type,against,no,tr_no) values('$dt','$jvnodn','Dr','$customer_name5','$customer_amt5','0','$narration','$brcode','BP','$banks','$mref','$crnodn')";
                mysqli_query($db,$sqldn) or die(mysqli_error());
                $sql="insert into psnindia_psndbdn.bnkpay(date,j_type,type,partic,dramt,cramt,narr,brcode,bpno,against,no) values('$dt','$jvnodn','Dr','$customer_name5','$customer_amt5','0','$narration','$brcode','$crnodn','$banks','$mref')";
                //echo $sql;
                //echo "<br>";
                mysqli_query($db,$sql) or die(mysqli_error($db));
                
                
                
            }
            else
            {
                print "<script>alert('Invalid customer $customer_name5')</script>";
            }
        }
        
                
                
                
                
                $sqldn="insert into psnindia_psndbdn.journal2(date,jvno,type,partic,dramt,cramt,narr,brcode,j_type,against,no,tr_no) values('$dt','$jvnodn','Cr','$banks','0','$totcramt','$narration','$brcode','BP','$banks','$mref','$crnodn')";
                mysqli_query($db,$sqldn) or die(mysqli_error());
                $sql="insert into psnindia_psndbdn.bnkpay(date,j_type,type,partic,dramt,cramt,narr,brcode,bpno,against,no) values('$dt','$jvnodn','Cr','$banks','0','$totcramt','$narration','$brcode','$crnodn','$banks','$mref')";
                //echo $sql;
                //echo "<br>";
                mysqli_query($db,$sql) or die(mysqli_error($db));
                
                
                
                
    }
    else
    {
    
        $totcramt=$cr_row['cramt'];
        
        
        $sqldn="insert into psnindia_psndbdn.journal2(date,jvno,type,partic,dramt,cramt,narr,brcode,j_type,against,no,tr_no) values('$dt','$jvnodn','Dr','$banks','$totcramt','0','$narration','$brcode','BP','$banks','$mref','$brnodn')";
                mysqli_query($db,$sqldn) or die(mysqli_error());
                $sql="insert into psnindia_psndbdn.bnkrec(date,j_type,type,partic,dramt,cramt,narr,brcode,brno,against,no) values('$dt','$jvnodn','Dr','$banks','$totcramt','0','$narration','$brcode','$brnodn','$banks','$mref')";
                //echo $sql;
                //echo "<br>";
                mysqli_query($db,$sql) or die(mysqli_error($db));
        
        
        
        
        
        if($customer_amt1>0)
        {
            $ledqry1=mysqli_query($db,"select led from ledger where brcode='$brcode' and led='$customer_name1'");
            $lednum1=mysqli_num_rows($ledqry1);
            
            if($lednum1>0)
            {
                
                $sql="insert into psnindia_psndbdn.journal2(date,jvno,type,partic,dramt,cramt,narr,brcode,j_type,against,no,tr_no) values('$dt','$jvnodn','Cr','$customer_name1','0','$customer_amt1','$narration','$brcode','BR','$banks','$mref','$brnodn')";
        //echo $sql;
        //echo "<br>";
        mysqli_query($db,$sql) or die(mysqli_error($db));
         $sql="insert into psnindia_psndbdn.bnkrec(date,j_type,type,partic,dramt,cramt,narr,brcode,brno,against,no) values('$dt','$jvnodn','Cr','$customer_name1','0','$customer_amt1','$narration','$brcode','$brnodn','$banks','$mref')";
                //echo $sql;
                //echo "<br>";
                mysqli_query($db,$sql) or die(mysqli_error($db));
                
                
                
            }
            else
            {
                print "<script>alert('Invalid customer $customer_name1')</script>";
            }
        }
        if($customer_amt2>0)
        {
            $ledqry2=mysqli_query($db,"select led from ledger where brcode='$brcode' and led='$customer_name2'");
            $lednum2=mysqli_num_rows($ledqry2);
            
            if($lednum2>0)
            {
                $sql="insert into psnindia_psndbdn.journal2(date,jvno,type,partic,dramt,cramt,narr,brcode,j_type,against,no,tr_no) values('$dt','$jvnodn','Cr','$customer_name2','0','$customer_amt2','$narration','$brcode','BR','$banks','$mref','$brnodn')";
        //echo $sql;
        //echo "<br>";
        mysqli_query($db,$sql) or die(mysqli_error($db));
         $sql="insert into psnindia_psndbdn.bnkrec(date,j_type,type,partic,dramt,cramt,narr,brcode,brno,against,no) values('$dt','$jvnodn','Cr','$customer_name2','0','$customer_amt2','$narration','$brcode','$brnodn','$banks','$mref')";
                //echo $sql;
                //echo "<br>";
                mysqli_query($db,$sql) or die(mysqli_error($db));
                
                
                
            }
            else
            {
                print "<script>alert('Invalid customer $customer_name2')</script>";
            }
        }
            
        
        if($customer_amt3>0)
        {
            $ledqry3=mysqli_query($db,"select led from ledger where brcode='$brcode' and led='$customer_name3'");
            $lednum3=mysqli_num_rows($ledqry3);
            
            if($lednum3>0)
            {
                
                $sql="insert into psnindia_psndbdn.journal2(date,jvno,type,partic,dramt,cramt,narr,brcode,j_type,against,no,tr_no) values('$dt','$jvnodn','Cr','$customer_name3','0','$customer_amt3','$narration','$brcode','BR','$banks','$mref','$brnodn')";
        //echo $sql;
        //echo "<br>";
        mysqli_query($db,$sql) or die(mysqli_error($db));
         $sql="insert into psnindia_psndbdn.bnkrec(date,j_type,type,partic,dramt,cramt,narr,brcode,brno,against,no) values('$dt','$jvnodn','Cr','$customer_name3','0','$customer_amt3','$narration','$brcode','$brnodn','$banks','$mref')";
                //echo $sql;
                //echo "<br>";
                mysqli_query($db,$sql) or die(mysqli_error($db));
                
                
            }
            else
            {
                print "<script>alert('Invalid customer $customer_name3')</script>";
            }
        }
        if($customer_amt4>0)
        {
            $ledqry4=mysqli_query($db,"select led from ledger where brcode='$brcode' and led='$customer_name4'");
            $lednum4=mysqli_num_rows($ledqry4);
            
            if($lednum4>0)
            {
                $sql="insert into psnindia_psndbdn.journal2(date,jvno,type,partic,dramt,cramt,narr,brcode,j_type,against,no,tr_no) values('$dt','$jvnodn','Cr','$customer_name4','0','$customer_amt4','$narration','$brcode','BR','$banks','$mref','$brnodn')";
        //echo $sql;
        //echo "<br>";
        mysqli_query($db,$sql) or die(mysqli_error($db));
         $sql="insert into psnindia_psndbdn.bnkrec(date,j_type,type,partic,dramt,cramt,narr,brcode,brno,against,no) values('$dt','$jvnodn','Cr','$customer_name4','0','$customer_amt4','$narration','$brcode','$brnodn','$banks','$mref')";
                //echo $sql;
                //echo "<br>";
                mysqli_query($db,$sql) or die(mysqli_error($db));
                
                
            }
            else
            {
                print "<script>alert('Invalid customer $customer_name4')</script>";
            }
        }
        if($customer_amt5>0)
        {
            $ledqry5=mysqli_query($db,"select led from ledger where brcode='$brcode' and led='$customer_name5'");
            $lednum5=mysqli_num_rows($ledqry5);
            
            if($lednum5>0)
            {
                
                $sql="insert into psnindia_psndbdn.journal2(date,jvno,type,partic,dramt,cramt,narr,brcode,j_type,against,no,tr_no) values('$dt','$jvnodn','Cr','$customer_name5','0','$customer_amt5','$narration','$brcode','BR','$banks','$mref','$brnodn')";
        //echo $sql;
        //echo "<br>";
        mysqli_query($db,$sql) or die(mysqli_error($db));
         $sql="insert into psnindia_psndbdn.bnkrec(date,j_type,type,partic,dramt,cramt,narr,brcode,brno,against,no) values('$dt','$jvnodn','Cr','$customer_name5','0','$customer_amt5','$narration','$brcode','$brnodn','$banks','$mref')";
                //echo $sql;
                //echo "<br>";
                mysqli_query($db,$sql) or die(mysqli_error($db));
                
                
                
                
            }
            else
            {
                print "<script>alert('Invalid customer $customer_name5')</script>";
            }
        }
    }
    
    }
    else
    {
        print "<script>alert('Invalid customer Name')</script>";
    }
    print "<script>window.close()</script>";
    header("Refresh:0; url=bankstate.php");
   
}
 */
$invres1=mysqli_query($db,"select * from psnindia_gen4psnauto_psn_psndb.upentry where brcode='PSN236' and id='$recrdid' and jvno>0 and allocat_brcode='PSN236'");
$invrow=mysqli_fetch_array($invres1);

$invnnum=mysqli_num_rows($invres1);
if($invnnum>0)
{
    $resbp=mysqli_query($db,"select max(bpno) from psnindia_psndbdn.bnkpay where brcode='$brcode'");
    $rowbp=mysqli_fetch_row($resbp);
    if($rowbp[0]<=0)
    {
        $crnodn=1;
    }
    else
    {
        $crnodn=($rowbp[0]+1);
    }
   
    $rsdn=mysqli_query($db,"select max(brno) from psnindia_psndbdn.bnkrec where brcode='$brcode'");
$cr_rowdn=mysqli_fetch_row($rsdn);
if($cr_rowdn[0]<=0)
{
   $brnodn=1;
}
 else  
 {
   $brnodn=$cr_rowdn[0]+1;
 }
 
 
$resdn=mysqli_query($db,"select max(jvno) from psnindia_psndbdn.journal2 where brcode='$brcode'");

 $rowdn=mysqli_fetch_row($resdn);
 if($rowdn[0]<=0)
 {
    $jvnodn=1;
 }
 else
 {
    $jvnodn=($rowdn[0]+1);
 }
    $brflg=0;
    $bpflg=0;
    $invjvno=$invrow['jvno'];
    $narrationdat=$invrow['partic'];  
    $jvqry=mysqli_query($db,"select * from journal2 where jvno='$invjvno' and brcode='PSN236'");
    while($jvrow=mysqli_fetch_array($jvqry))
    {
        if($jvrow['j_type']=='BR')
        {
            $sqldn="insert into psnindia_psndbdn.journal2(date,jvno,type,partic,dramt,cramt,narr,brcode,j_type,against,no,tr_no) values('$jvrow[date]','$jvnodn','$jvrow[type]','$jvrow[partic]','$jvrow[dramt]','$jvrow[cramt]','$jvrow[narr]','$jvrow[brcode]','$jvrow[j_type]','$jvrow[against]','$jvrow[no]','$brnodn')";
                echo $sqldn;
                echo "<br>";
            mysqli_query($db,$sqldn) or die(mysqli_error());
            $brflg=1;
        }
        if($jvrow['j_type']=='BP')
        {
            $sqldn="insert into psnindia_psndbdn.journal2(date,jvno,type,partic,dramt,cramt,narr,brcode,j_type,against,no,tr_no) values('$jvrow[date]','$jvnodn','$jvrow[type]','$jvrow[partic]','$jvrow[dramt]','$jvrow[cramt]','$jvrow[narr]','$jvrow[brcode]','$jvrow[j_type]','$jvrow[against]','$jvrow[no]','$crnodn')";
            echo $sqldn;
                echo "<br>";
            mysqli_query($db,$sqldn) or die(mysqli_error());
            $bpflg=1;
        }
    }
    
    if($brflg=='1')
    {
        $jvbnkqry=mysqli_query($db,"select * from bnkrec where j_type='$invjvno' and brcode='PSN236'");
        while($jvbnkrow=mysqli_fetch_array($jvbnkqry))
        {
        
            $sqlbr="insert into psnindia_psndbdn.bnkrec(date,j_type,type,partic,dramt,cramt,narr,brcode,brno,against,no) values('$jvbnkrow[date]','$jvnodn','$jvbnkrow[type]','$jvbnkrow[partic]','$jvbnkrow[dramt]','$jvbnkrow[cramt]','$jvbnkrow[narr]','$brcode','$brnodn','$jvbnkrow[against]','$jvbnkrow[no]')";
            echo $sqlbr;
                echo "<br>";
               
                mysqli_query($db,$sqlbr) or die(mysqli_error($db));
        }
        
    }
    if($bpflg=='1')
    {
        $jvbpqry=mysqli_query($db,"select * from bnkpay where j_type='$invjvno' and brcode='PSN236'");
        while($jvbprow=mysqli_fetch_array($jvbpqry))
        {
            $sqlbnk="insert into psnindia_psndbdn.bnkpay(date,j_type,type,partic,dramt,cramt,narr,brcode,bpno,against,no) values('$jvbprow[date]','$jvnodn','$jvbprow[type]','$jvbprow[partic]','$jvbprow[dramt]','$jvbprow[cramt]','$jvbprow[narr]','$jvbprow[brcode]','$crnodn','$jvbprow[against]','$jvbprow[no]')";
                echo $sqlbnk;
                echo "<br>";
            mysqli_query($db,$sqlbnk) or die(mysqli_error($db));
        }
        
    }
    
    
}
print "<script>window.close()</script>";
    header("Refresh:0; url=bankstate.php");
?>
</html>