<?
session_start();
$brcode=$_SESSION['brcode'];
$user=$_SESSION['user'];
if(strlen($brcode)<=0)
{
 print "<script> alert('Session timed out'); parent.document.location='ind.php'; </script>";
 return;
}
include('opendb.php');

$savecount=$_REQUEST['savecount'];
if($savecount==0)
{
   $vatcst=$_REQUEST['vatcst'];
   $i=0;
   foreach($vatcst as $a)
   {
      $vatcst[$i]=$a; 
      $i++;
   }
   $grnno=$_REQUEST['grnno'];
   $i=0;
   foreach($grnno as $a)
   {
     $grnno[$i]=$a;
     $i++;
   }
   $i=0;
   $grndt=$_REQUEST['grndt'];
   foreach($grndt as $a)
   {
      $grndt[$i]=$a; 
      $i++;
   }
   $i=0;
   $supnm=$_REQUEST['supnm'];
   foreach($supnm as $a)
   {
      $supnm[$i]=$a; 
      $i++;
   }
   
    $sprfvebfrtx=$_REQUEST['sprfvebfrtx'];
   $i=0;
   foreach($sprfvebfrtx as $a)
   {
     $sprfvebfrtx[$i]=$a;
     $i++;
   }

$i=0;
$sprfvepntbfrtx=$_REQUEST['sprfvepntbfrtx'];
foreach($sprfvepntbfrtx as $a)
{
 $sprfvepntbfrtx[$i]=$a; 
 $i++;
}
$i=0;
$sprfrtnbfrtx=$_REQUEST['sprfrtnbfrtx'];
foreach($sprfrtnbfrtx as $a)
{
 $sprfrtnbfrtx[$i]=$a; 
 $i++;
}
$i=0;
$lubamt=$_REQUEST['lubamt'];
foreach($lubamt as $a)
{
 $lubamt[$i]=$a; 
 $i++;
}
$i=0;
$labamt=$_REQUEST['labamt'];
foreach($labamt as $a)
{
 $labamt[$i]=$a; 
 $i++;
}
$i=0;
$fivetax=$_REQUEST['fivetax'];
foreach($fivetax as $a)
{
 $fivetax[$i]=$a; 
 $i++;
}
$i=0;
$fivepnttax=$_REQUEST['fivepnttax'];
foreach($fivepnttax as $a)
{
 $fivepnttax[$i]=$a; 
 $i++;
}
$i=0;
$fourteentax=$_REQUEST['fourteentax'];
foreach($fourteentax as $a)
{
 $fourteentax[$i]=$a; 
 $i++;
}
$i=0;
$lubtx=$_REQUEST['lubtx'];
foreach($lubtx as $a)
{
 $lubtx[$i]=$a; 
 $i++;
}
$i=0;
$servtx=$_REQUEST['servtx'];
foreach($servtx as $a)
{
 $servtx[$i]=$a; 
 $i++;
}
$i=0;
$kkcs=$_REQUEST['kkcs'];
foreach($kkcs as $a)
{
 $kkcs[$i]=$a; 
 $i++;
}
$i=0;
$sbc=$_REQUEST['sbc'];
foreach($sbc as $a)
{
 $sbc[$i]=$a; 
 $i++;
}
$i=0;
$others=$_REQUEST['others'];
foreach($others as $a)
{
 $others[$i]=$a; 
 $i++;
}
$i=0;
$roundof=$_REQUEST['roundof'];
foreach($roundof as $a)
{
 $roundof[$i]=$a; 
 $i++;
}
$i=0;
$ntamt=$_REQUEST['ntamt'];
foreach($ntamt as $a)
{
 $ntamt[$i]=$a; 
 $i++;
}

$jtype=$_REQUEST['jtype'];
$vattyp=$_REQUEST['vattyp'];
$flg=0;
$flg2=0;

       for($j=0;$j<$i;$j++)
       {
           $k=$j+1;
           $jrqry=mysqli_query($db,"select invno from itemsale where brcode='$brcode' and trnstyp='$jtype' and vattyp='$vattyp' and invno='$grnno[$j]'");

           if(mysqli_num_rows($jrqry)>0)
           {
              print "<script>document.getElementById('myTable').rows[$k].cells[0].style.color='red';</script>";
              $flg++;
           }
       }
       if($flg>0)
       {
           echo "<small>Invoice No. Already exists</small>";
       }
       else
       {

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

                $sql="insert into itemsale(invno,invdate,customer,vehno,bildt,fvtx,fivepntx,sprfrtn,lubfrtn,lubamount,labamt,sprfivetax,sprfvpnt,sprfrtntx,lubtx,servtx,kkcs,sbc,others,roundof,total,trnstyp,vattyp,brcode) values('$grnno[$j]','$grndt[$j]','$supnm[$j]','$pbilno[$j]','$pbildt[$j]','$sprfvebfrtx[$j]','$sprfvepntbfrtx[$j]','$sprfrtnbfrtx[$j]','$vatcst[$j]','$lubamt[$j]','$labamt[$j]','$fivetax[$j]','$fivepnttax[$j]','$fourteentax[$j]','$lubtx[$j]','$servtx[$j]','$kkcs[$j]','$sbc[$j]','$others[$j]','$roundof[$j]','$ntamt[$j]','$jtype','$vattyp','$brcode')";
echo $sql;
               mysqli_query($db,$sql) or die(mysqli_error($db));

 
           }
//print "<font color=black>Data has been posted</font><br>";
     echo "<script> document.getElementById('savecount').value=1;document.location='item_bill.php?jtype=$jtype&&vatvl=$vattyp';</script>";
//log_entry($brcode,$user,$REMOTE_ADDR,"Cash Receipt JVNo: $jvno, CR No:$crno");
  }
}
elseif($savecount==1)
{
echo "Data already saved";
  echo "<script> document.location='item_bill.php';</script>";
}

?>
