<?
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++;
   }
   $i=0;
   $pbilno=$_REQUEST['pbilno'];
   foreach($pbilno as $a)
   {
      $pbilno[$i]=$a; 
      $i++;
   }
    $lubamt=$_REQUEST['lubamt'];
   $i=0;
   foreach($lubamt as $a)
   {
     $lubamt[$i]=$a;
     $i++;
   }

$i=0;
$pbildt=$_REQUEST['pbildt'];
foreach($pbildt as $a)
{
 $pbildt[$i]=$a; 
 $i++;
}
$i=0;
$zerobfrtx=$_REQUEST['zerobfrtx'];
foreach($zerobfrtx as $a)
{
 $zerobfrtx[$i]=$a; 
 $i++;
}
$i=0;
$fivebfrtx=$_REQUEST['fivebfrtx'];
foreach($fivebfrtx as $a)
{
 $fivebfrtx[$i]=$a; 
 $i++;
}
$i=0;
$sprfrtn=$_REQUEST['sprfrtn'];
foreach($sprfrtn as $a)
{
 $sprfrtn[$i]=$a; 
 $i++;
}
$i=0;
$btryfrtn=$_REQUEST['btryfrtn'];
foreach($btryfrtn as $a)
{
 $btryfrtn[$i]=$a; 
 $i++;
}
$i=0;
$discnt=$_REQUEST['discnt'];
foreach($discnt as $a)
{
 $discnt[$i]=$a; 
 $i++;
}
$i=0;
$zerotax=$_REQUEST['zerotax'];
foreach($zerotax as $a)
{
 $zerotax[$i]=$a; 
 $i++;
}
$i=0;
$fivetax=$_REQUEST['fivetax'];
foreach($fivetax as $a)
{
 $fivetax[$i]=$a; 
 $i++;
}
$i=0;
$fourteentax=$_REQUEST['fourteentax'];
foreach($fourteentax as $a)
{
 $fourteentax[$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 amplcpurchase 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 amplcpurchase(invno,invdate,customer,vehno,bildt,zertx,fivetx,sprfrtn,btrfrtn,discount,vatcst,brcode,zerprc,fiveprc,frtntx,others,rndof,total,trnstyp,vattyp,lubamt) values('$grnno[$j]','$grndt[$j]','$supnm[$j]','$pbilno[$j]','$pbildt[$j]','$zerobfrtx[$j]','$fivebfrtx[$j]','$sprfrtn[$j]','$btryfrtn[$j]','$discnt[$j]','$vatcst[$j]','$brcode','$zerotax[$j]','$fivetax[$j]','$fourteentax[$j]','$others[$j]','$roundof[$j]','$ntamt[$j]','$jtype','$vattyp','$lubamt[$j]')";
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_purchasenew.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_purchasenew.php';</script>";
}

?>
