<?

session_start();
include("test.php");
include("opendb.php");

for($invno='1';$invno<'388';$invno++){


$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);




$qry=mysqli_query($db,"select * from  gateentry where invno='$invno' and brcode='$brcode' ")or die('jpres err'.mysqli_error($db));

if(mysqli_num_rows($qry)>0){

  $res=mysqli_fetch_array($qry);

$jvno_old=$res['jvno'];

$jcno=$res['jcno'];

$entrydate=$res['inv_date'];
$cname= $res[customer];

$jpres=mysqli_query($db,"select sum(gtot) from jcardparts where invno='$invno' and brcode='$brcode'")or die('jpres err'.mysqli_error($db));

  $jprow=mysqli_fetch_row($jpres);

$tot= $jprow[0];
if(strlen($tot)==0)
$tot=0;



$qry1=mysqli_query($db,"select *  from journal2  where jvno=' $jvno_old' and brcode='$brcode' ")or die('jpres err'.mysqli_error($db));

  if(mysqli_num_rows($qry1)>0){




                     $qry=mysqli_query($db,"delete from journal2 where jvno='$jvno_old'  and brcode='$brcode' ");

$jpres=mysqli_query($db,"select sum(gtot) from jcardparts where jcno='$jcno' and brcode='$brcode'")or die('jpres err'.mysqli_error($db));
  $jprow=mysqli_fetch_row($jpres);
 
if($jprow[0]>0)
  $x=1; 

  $jservres=mysqli_query($db,"select sum(servamt),sum(dis) from jcardserv where jcno='$jcno' and brcode='$brcode'")or die('jservres err'.mysqli_error($db));
  $jservrow=mysqli_fetch_row($jservres);
if($jservrow[0]>0)
  $x=1; 

                     $ltot=$jservrow[0]-$jservrow[1];
  $gtot=($jprow[0]+$ltot);

                     $round_prt=round($jprow[0]);
                     $round_lab=round($ltot);
                      
                   
  $sql="insert into journal2 values('$entrydate','$jvno','Dr','$cname','$round_prt','0','Job Card Invoice:(Invno: $invno)','$brcode','Job Crd','JB Crd','$jcno','$jcno')";
mysqli_query($db,$sql) or die('jv1 err:'.mysqli_error($db));


                      $sql="insert into journal2 values('$entrydate','$jvno','Cr','sale-jobcard','0','$jprow[0]','Job Card Invoice:(Invno: $invno)','$brcode','JC Spare Invoice','Job Crd','$jcno','0')";
     mysqli_query($db,$sql) or die('Led Post Err2'.mysqli_error($db));



                    
                   $gtot=$round_lab+$round_prt;

                    $sql="insert into journal2 values('$entrydate','$jvno','Dr','$cname','$gtot','0','Job Card(labour) Invoice:(Invno: $invno)','$brcode','JB Crd-labour','JB Crd-labour','$jcno','$jcno')";
mysqli_query($db,$sql) or die('jv1 err:'.mysqli_error($db));


                    $sql="insert into journal2 values('$entrydate','$jvno','Cr','Labour','0','$ltot','Job Card Invoice:(Invno: $invno)','$brcode','JC Labour Invoice','Job Crd-labour','$jcno','0')";
     mysqli_query($db,$sql) or die('Led Post Err3'.mysqli_error($db));


                                     
                            
$jvdifres=mysqli_query($db,"select sum(dramt)-sum(cramt) from journal2 where jvno='$jvno' and brcode='$brcode'");
$jvdifrow=mysqli_fetch_row($jvdifres);
$dif=$jvdifrow[0];

if($dif<0)
{
$sql="insert into journal2 values('$entrydate','$jvno','Dr','Round_of','".abs($dif)."', '0','Round of adjusted (JC)Inv No: $invno','$brcode','Job Card','jcno','$jcno','$invno')";
mysqli_query($db,$sql) or die("J V Write error dif1:".mysqli_error($db));
}
else if($dif>0)
{
  $sql="insert into journal2 values('$entrydate','$jvno','Cr','Round_of','0','".abs($dif)."','Round of adjusted (JC)Inv No: $invno','$brcode','Job Card','jcno','$jcno','$invno')";
mysqli_query($db,$sql) or die("J V Write error dif:".mysqli_error($db));
}



                 $sql="update gateentry set jvno='$jvno'  where jcno='$jcno' and invno='$invno' and brcode='$brcode'";
                     mysqli_query($db,$sql) or die("Err 0".mysqli_error($db));




}

}


}


echo 'ok';
?>
