<?
session_start();
if(strlen($brcode)<=0)
{
  print "<script> alert('Invalid User'); parent.document.location='ind.php' </script>";
  return;
}
include("opendb.php");
include("test.php");

$dt=date("Y-m-d");


if((bnk_rec)||(bnk_rec)){
 $j=$pos;
 $pos2=$j+1;
 }

if($upd1){

  $j=1;
 $pos2=$pos1;
 }
$k=1;


for($i=$j;$i<$pos2;$i++){

     $narr="Jobno:(".$jcno[$i].") Invno:(".$invno[$i].") Regno:(".$regno[$i].") Customer:(".$cus[$i].")";

      $jvres=mysqli_query($db,"select max(jvno) from journal2 where brcode='$brcode'");

		$jvrow=mysqli_fetch_row($jvres);

	         if($jvrow[0]>0)
 			$jvno=($jvrow[0]+1);
		  else
                     $jvno=1;


   if($bnk_rec){

       $rs=mysqli_query($db,"select max(brno) from bnkrec where brcode='$brcode'");
        $cr_row=mysqli_fetch_row($rs);
         if($cr_row[0]<=0)

             $trno=1;
            else   

            $trno=$cr_row[0]+1;

      $tr='BR';

      

	 $sql="insert into bnkrec values('$date[$i]','$trno','Dr','$dr[$i]','$marked_amt[$i]','0','$narr','$brcode','$jvno','$invno[$i]','$jcno[$i]','')";

             mysqli_query($db,$sql)or die(mysqli_error($db));

        
        $sql="insert into bnkrec values('$date[$i]','$trno', 'Cr','$cr[$i]','0','$marked_amt[$i]','$narr','$brcode','$jvno','$invno[$i]','$jcno[$i]','')";
             mysqli_query($db,$sql) or die(mysqli_error($db));


    }

    if($csh_rec){

       $rs=mysqli_query($db,"select max(crno) from cshrec where brcode='$brcode'");
        $cr_row=mysqli_fetch_row($rs);
         if($cr_row[0]<=0)

            $trno=1;
          else   

            $trno=$cr_row[0]+1;

        $tr='CR';


        $sql="insert into cshrec values('$date[$i]','$trno','Dr','$dr[$i]','$marked_amt[$i]','0','$narr','$brcode','$jvno','$invno[$i]','$jcno[$i]')";
             mysqli_query($db,$sql)or die(mysqli_error($db));


        $sql="insert into cshrec values('$date[$i]','$trno', 'Cr','$cr[$i]','0','$marked_amt[$i]','$narr','$brcode','$jvno','$invno[$i]','$jcno[$i]')";
             mysqli_query($db,$sql) or die(mysqli_error($db));



    }

        mysqli_query($db,"update jcardinvoice set marked_amt='$marked_amt[$i]',marked_jvno='$jvno',marked_trno='$trno',marked_tr_type='$tr' where invno='$invno[$i]' and brcode='$brcode'");

        mysqli_query($db,"insert into payment_marker values('', '$jcno[$i]', '$invno[$i]', '$marked_amt[$i]', '$date[$i]', '$jvno', '$trno', '$tr', '$brcode' )");

        $sql="insert into journal2 values('$date[$i]','$jvno','Dr','$dr[$i]','$marked_amt[$i]','0','job no($jcno[$i]), invno( $invno[$i]),regno($regno[$i])','$brcode','$tr','JobCrd invoice','$jcno[$i]','$trno')";

       	mysqli_query($db,$sql) or die('jv1 err:'.mysqli_error($db));
 
        $sql="insert into journal2 values('$date[$i]','$jvno','Cr','$cr[$i]','0','$marked_amt[$i]','job no($jcno[$i]), invno( $invno[$i]),regno($regno[$i])','$brcode','$tr','JobCrd invoice','$jcno[$i]','$trno')";

     		      mysqli_query($db,$sql) or die('Led Post Err2'.mysqli_error($db)); 


        

} 

?>
<script>
 alert('Updated');
 document.location='payment_marker.php';
</script>
 
