<?
session_start();
$brcode=$_SESSION['brcode'];
$user=$_SESSION['user'];
if(strlen($user)<=0)
{
  print "<script> alert('Invalid User'); document.location='inde.php' </script>";
  return;
}
include_once('opendb.php');
$savecount=$_REQUEST['savecount'];
$taxtype=$_REQUEST['taxtype'];
$pmode=$_REQUEST['pmode'];
$expno = $_REQUEST['expno'];
if($savecount==0) 
{
  
    $exphd=$_REQUEST['exphd'];

    
    $leddt=$_REQUEST['leddt'];
    $vndinvno=$_REQUEST['vndinvno'];
    $supnm=$_REQUEST['supnm'];
   
    $supdt=$_REQUEST['supdt'];
    $narrat=$_REQUEST['narrat'];

     $refno=$_REQUEST['refno'];


            $i=0;
    $hsncode=$_REQUEST['hsncode'];
    foreach($hsncode as $a)
    {
        $hsncode[$i]=$a;
        $i++;
    }
    $i=0;
    $befrtx=$_REQUEST['befrtx'];
    foreach($befrtx as $a)
    {
        $befrtx[$i]=$a;
        $i++;
    }
    
    $i=0;
    $taxprc=$_REQUEST['taxprc'];
    foreach($taxprc as $a)
    {
        $taxprc[$i]=$a;
        $i++;
    }
    
    $i=0;
    $led=$_REQUEST['led'];
    foreach($led as $a)
    {
        $led[$i]=$a;
        $i++;
    }
   
    $i=0;
    $cgstp=$_REQUEST['cgstp'];
    foreach($cgstp as $a)
    {
        $cgstp[$i]=$a;
        $i++;
    }
     
    $i=0;
    $cgstamt=$_REQUEST['cgstamt'];
    foreach($cgstamt as $a)
    {
        $cgstamt[$i]=$a;
        $i++;
    }
    
    $i=0;
    $sgstp=$_REQUEST['sgstp'];
    foreach($sgstp as $a)
    {
        $sgstp[$i]=$a;
        $i++;
    }
    
    $i=0;
    $sgstamt=$_REQUEST['sgstamt'];
    foreach($sgstamt as $a)
    {
        $sgstamt[$i]=$a;
        $i++;
    }
    $i=0;
    $kfcamt=$_REQUEST['kfcamt'];
    foreach($kfcamt as $a)
    {
        $kfcamt[$i]=$a;
        $i++;
    }
    
     $i=0;
    $igstp=$_REQUEST['igstp'];
    foreach($igstp as $a)
    {
        $igstp[$i]=$a;
        $i++;
    }
    $i=0;
    $igstamt=$_REQUEST['igstamt'];
    foreach($igstamt as $a)
    {
        $igstamt[$i]=$a;
        $i++;
    }
    
    $i=0;
    $ntamt=$_REQUEST['ntamt'];
    foreach($ntamt as $a)
    {
        $ntamt[$i]=$a;
        $i++;
    }

mysqli_query($db,"delete from gst_incomeprof where brcode ='$brcode' and exp_no='$expno'");
    $flg=0;
    $spflg=0;
    $totflg=0;
    $flg1=0;
    for($j=0;$j<$i;$j++)
    {
        $k=$j+1;
        if(($befrtx[$j]=='')||($befrtx[$j]=='0'))
        {
            print "<script>document.getElementById('myTable').rows[$k].cells[0].style.color='red';</script>";
            $flg++;
        }
	if($hsncode[$j]=='')
                        {
                            print "<script>document.getElementById('myTable').rows[$k].cells[0].style.color='red';</script>";
                            $hsnflg++;
                        }
          
    }
    if($flg>0)
    {
        echo "<small>Check the Amt of Red marked row(s)</br></small>";
    }
    elseif($hsnflg>0)
    {
        echo "<b style='color: red;'>Enter Hsncode of Red marked row(s)</b>";
    }
    elseif($spflg>0)
    {
        echo "<small>Check the itemcode of Red marked row(s)</br></small>";
    }
    else
    {
	$expno=$_REQUEST['expno'];
     
        
        $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;
	    }
        
        for($m=0;$m<$i;$m++)
        {
            if($taxtype=='INTRASTATE')
            {
                $sql1="INSERT INTO gst_incomeprof(exp_no,date,expense_head,vendor,narr,before_tax,cgstp,cgstamt,sgstp,sgstamt,expense_amount,brcode,vendrinvno,vendrinvdt,jvno,txtype,acchead,refno,hsncode,kfcamt) VALUES ('$expno','$leddt','$exphd','$supnm','$narrat','$befrtx[$m]','$cgstp[$m]','$cgstamt[$m]','$sgstp[$m]','$sgstamt[$m]','$ntamt[$m]','$brcode','$vndinvno','$supdt','$jvno','$taxtype','$led[$m]','$refno','$hsncode[$m]','$kfcamt[$m]')";
            }
            if($taxtype=='INTERSTATE')
            {
                $sql1="INSERT INTO gst_incomeprof(exp_no,date,expense_head,vendor,narr,before_tax,igstp,igstamt,expense_amount,brcode,vendrinvno,vendrinvdt,jvno,txtype,acchead,refno,hsncode) VALUES ('$expno','$leddt','$exphd','$supnm','$narrat','$befrtx[$m]','$igstp[$m]','$igstamt[$m]','$ntamt[$m]','$brcode','$vndinvno','$supdt','$jvno','$taxtype','$led[$m]','$refno','$hsncode[$m]')";
            }
            mysqli_query($db,$sql1);
            $flg1++;
        }
        
        
        if($flg1==$i)
        {
           echo "<script>alert('Data Updated')</script>";
	    print "<script>window.open('payrec/prnvoucher.php?jvno=$jvno'); </script>";
           print "<script>document.getElementById('savecount').value=1; document.location='gstmultislct2_outprof.php?taxtype=$taxtype'; </script>";
        }
    }
	
}
elseif($savecount==1)
{
	echo "Data Updated";
  	echo "<script> document.location='gstmultislct2_outprof.php';</script>";
}
?>
