<?
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');

$invid=$_REQUEST['invid'];
$exphd=$_REQUEST['expensehd'];
$supnm=$_REQUEST['suppliernm'];
$narrat=$_REQUEST['narration'];

  $re1=mysqli_query($db,"select * from sap_einvoice where brcode='$brcode' and invno='$invid'");
  $saprs=mysqli_fetch_array($re1);
 $refno=$invid;
 
        $rr=mysqli_query($db,"select max(exp_no) from gst_income where brcode='$brcode'");
        $rrrow=mysqli_fetch_row($rr);
        if($rrrow[0]<=0)
        {
 	        $expno=1;
        }
        else
        {
 	        $expno=($rrrow[0]+1);
        }
        
        $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;
	    }
  
 $subval=substr($gstinval,0,2);

 if($subval=='32')
 {
    $taxtype='INTRASTATE';
 }
 else
 {
    $taxtype='INTERSTATE'; 
 }
 $leddt=$saprs['entrydate'];
 $gstinval=$saprs['buygstn'];
 $taxperc=$saprs['gstp'];
 
    //$respres=mysqli_query($db,"select sum(aprice),sum(cgstamt),sum(sgstamt),sum(igstamt) from sap_einvoice where brcode='$brcode' and invno='$invid'");
    $befrtx=$saprs['total_taxable_value'];
    $cgstamt=$saprs['cgst_amt'];
    $sgstamt=$saprs['sgst_amt'];
    $igstamt=$saprs['igst_amt'];
    $ntamt=$saprs['total_invoice_value'];
    $led='Rent';
    $hsncode=$saprs['hsncode'];
            if($taxtype=='INTRASTATE')
            {
                $cgstp=$taxperc/2;
                $sgstp=$taxperc/2;
                
                $sql1="INSERT INTO gst_income(exp_no,date,expense_head,vendor,narr,before_tax,cgstp,cgstamt,sgstp,sgstamt,expense_amount,brcode,vendrinvno,vendrinvdt,jvno,txtype,acchead,refno,hsncode) VALUES ('$expno','$leddt','$exphd','$supnm','$narrat','$befrtx','$cgstp','$cgstamt','$sgstp','$sgstamt','$ntamt','$brcode','0','0000-00-00','$jvno','$taxtype','$led','$refno','$hsncode')";
                mysqli_query($db,$sql1);
            }
            if($taxtype=='INTERSTATE')
            {
                $igstp=$taxperc;
                $sql1="INSERT INTO gst_income(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','$igstp','$igstamt','$ntamt','$brcode','0','0000-00-00','$jvno','$taxtype','$led','$refno','$hsncode')";
                mysqli_query($db,$sql1);
            }
            
            echo "<br>";
            echo $sql1;
            
            
            
            $taxdrres=mysqli_query($db,"select date,exp_no,expense_head,vendor,narr,sum(before_tax),sum(cgstamt),sum(sgstamt),sum(igstamt),sum(expense_amount),cgstp,sgstp,igstp,acchead,refno,jvno from gst_income where exp_no='$expno' and brcode='$brcode' group by exp_no,acchead,cgstp")or die('Led Post Err3'.mysqli_error());
	    while($taxdrrow=mysqli_fetch_array($taxdrres))
        {
            
            
            if($taxdrrow[6]>0)
            {
                $ledger='Output CGST Income'.$taxdrrow[10].'%';
                $sql="insert into journal2 values('$taxdrrow[0]','$taxdrrow[15]','Cr','$ledger','0','$taxdrrow[6]','$taxdrrow[4]','$brcode','GST Income','GST Income','$taxdrrow[14]','$taxdrrow[14]')";
                mysqli_query($db,$sql) or die('Led Post Err1'.mysqli_error());
            }
            if($taxdrrow[7]>0)
            {
                $ledger='Output SGST Income'.$taxdrrow[11].'%';
                $sql="insert into journal2 values('$taxdrrow[0]','$taxdrrow[15]','Cr','$ledger','0','$taxdrrow[7]','$taxdrrow[4]','$brcode','GST Income','GST Income','$taxdrrow[14]','$taxdrrow[14]')";
                mysqli_query($db,$sql) or die('Led Post Err1'.mysqli_error());
            }
            if($taxdrrow[8]>0)
            {
                $ledger='Output IGST Demo Income'.$taxdrrow[12].'%';
                $sql="insert into journal2 values('$taxdrrow[0]','$taxdrrow[15]','Cr','$ledger','0','$taxdrrow[8]','$taxdrrow[4]','$brcode','GST Income','GST Income','$taxdrrow[14]','$taxdrrow[14]')";
                mysqli_query($db,$sql) or die('Led Post Err1'.mysqli_error());
            }
            
            
           
        }
	$taxdrres=mysqli_query($db,"select date,exp_no,expense_head,vendor,narr,sum(before_tax),sum(cgstamt),sum(sgstamt),sum(igstamt),sum(expense_amount),cgstp,sgstp,igstp,acchead,refno,jvno from gst_income where exp_no='$expno' and brcode='$brcode' group by exp_no")or die('Led Post Err3'.mysqli_error());
	    while($taxdrrow=mysqli_fetch_array($taxdrres))
        {
		$ledger=$taxdrrow[2];
            $sql="insert into journal2 values('$taxdrrow[0]','$taxdrrow[15]','Cr','$ledger','0','$taxdrrow[5]','$taxdrrow[4]','$brcode','GST Income','GST Income','$taxdrrow[14]','0')";
            mysqli_query($db,$sql) or die('Led Post Err1'.mysqli_error());
		if($taxdrrow[9]>0)
            {
                $ledger=$taxdrrow['vendor'];
                $dif_p=$taxdrrow[9];
                $dif_exp=round($dif_p);

                $sql="insert into journal2 values('$taxdrrow[0]','$taxdrrow[15]','Dr','$ledger','$dif_exp','0','$taxdrrow[4]','$brcode','GST Income','GST Income','$taxdrrow[14]','0')";
                mysqli_query($db,$sql) or die('Led Post Err1'.mysqli_error());
            }
	 $jvdifres=mysqli_query($db,"select sum(dramt)-sum(cramt) from journal2 where jvno='$taxdrrow[15]' and brcode='$brcode'");
            $jvdifrow=mysqli_fetch_row($jvdifres);
            $dif=$jvdifrow[0];
            if($dif>0)
            {
                $xx=abs($dif);
                $sql="insert into journal2 values('$taxdrrow[0]','$taxdrrow[15]','Cr','Round Off','0', '".$xx."','Round of GST INC No: $taxdrrow[14]','$brcode','GST Income','REF No','$taxdrrow[14]','0')";
                mysqli_query($db,$sql) or die("J V Write error dif1:".mysqli_error());
            }
            else if($dif<0)
            {
                $xx=abs($dif);
                $sql="insert into journal2 values('$taxdrrow[0]','$taxdrrow[15]','Dr','Round Off','".$xx."','0','Round of GST INC No: $taxdrrow[14]','$brcode','GST Income','REF No','$taxdrrow[14]','0')";
                mysqli_query($db,$sql) or die("J V Write error dif:".mysqli_error());
            }
	}
       
           echo "<script>alert('Data saved')</script>";
	       print "<script>window.open('../gst_income_print.php?invno=$refno'); </script>";
          print "<script>document.location='sap_gstmultiple.php?taxtype=".$taxtype."'; </script>";
        
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
 /*
    $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++;
    }
if($taxtype=='INTRASTATE')
{
    $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++;
    }
}
else
{
     $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++;
    }


    $flg=0;
    $spflg=0;
    $totflg=0;
    $flg1=0;
   
   
        $rr=mysqli_query($db,"select max(exp_no) from gst_income where brcode='$brcode'");
        $rrrow=mysqli_fetch_row($rr);
        if($rrrow[0]<=0)
        {
 	        $expno=1;
        }
        else
        {
 	        $expno=($rrrow[0]+1);
        }
        
        $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_income(exp_no,date,expense_head,vendor,narr,before_tax,cgstp,cgstamt,sgstp,sgstamt,expense_amount,brcode,vendrinvno,vendrinvdt,jvno,txtype,acchead,refno,hsncode) VALUES ('$expno','$leddt','$exphd','$supnm','$narrat','$befrtx[$m]','$cgstp[$m]','$cgstamt[$m]','$sgstp[$m]','$sgstamt[$m]','$ntamt[$m]','$brcode','0','0000-00-00','$jvno','$taxtype','$led[$m]','$refno','$hsncode[$m]')";
            }
            if($taxtype=='INTERSTATE')
            {
                $sql1="INSERT INTO gst_income(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','0','0000-00-00','$jvno','$taxtype','$led[$m]','$refno','$hsncode[$m]')";
            }
           
            mysqli_query($db,$sql1);
            $flg1++;
        }
        
        $taxdrres=mysqli_query($db,"select date,exp_no,expense_head,vendor,narr,sum(before_tax),sum(cgstamt),sum(sgstamt),sum(igstamt),sum(expense_amount),cgstp,sgstp,igstp,acchead,refno,jvno from gst_income where exp_no='$expno' and brcode='$brcode' group by exp_no,acchead,cgstp")or die('Led Post Err3'.mysqli_error());
	    while($taxdrrow=mysqli_fetch_array($taxdrres))
        {
            
            
            if($taxdrrow[6]>0)
            {
                $ledger='Output CGST Income'.$taxdrrow[10].'%';
                $sql="insert into journal2 values('$taxdrrow[0]','$taxdrrow[15]','Cr','$ledger','0','$taxdrrow[6]','$taxdrrow[4]','$brcode','GST Income','GST Income','$taxdrrow[14]','$taxdrrow[14]','0','0000-00-00')";
                mysqli_query($db,$sql) or die('Led Post Err1'.mysqli_error());
            }
            if($taxdrrow[7]>0)
            {
                $ledger='Output SGST Income'.$taxdrrow[11].'%';
                $sql="insert into journal2 values('$taxdrrow[0]','$taxdrrow[15]','Cr','$ledger','0','$taxdrrow[7]','$taxdrrow[4]','$brcode','GST Income','GST Income','$taxdrrow[14]','$taxdrrow[14]','0','0000-00-00')";
                mysqli_query($db,$sql) or die('Led Post Err1'.mysqli_error());
            }
            if($taxdrrow[8]>0)
            {
                $ledger='Output IGST Demo Income'.$taxdrrow[12].'%';
                $sql="insert into journal2 values('$taxdrrow[0]','$taxdrrow[15]','Cr','$ledger','0','$taxdrrow[8]','$taxdrrow[4]','$brcode','GST Income','GST Income','$taxdrrow[14]','$taxdrrow[14]','0','0000-00-00')";
                mysqli_query($db,$sql) or die('Led Post Err1'.mysqli_error());
            }
            
            
           
        }
	$taxdrres=mysqli_query($db,"select date,exp_no,expense_head,vendor,narr,sum(before_tax),sum(cgstamt),sum(sgstamt),sum(igstamt),sum(expense_amount),cgstp,sgstp,igstp,acchead,refno,jvno from gst_income where exp_no='$expno' and brcode='$brcode' group by exp_no")or die('Led Post Err3'.mysqli_error());
	    while($taxdrrow=mysqli_fetch_array($taxdrres))
        {
		$ledger=$taxdrrow[2];
            $sql="insert into journal2 values('$taxdrrow[0]','$taxdrrow[15]','Cr','$ledger','0','$taxdrrow[5]','$taxdrrow[4]','$brcode','GST Income','GST Income','$taxdrrow[14]','0','0','0000-00-00')";
            mysqli_query($db,$sql) or die('Led Post Err1'.mysqli_error());
		if($taxdrrow[9]>0)
            {
                $ledger=$taxdrrow[vendor];
                $dif_p=$taxdrrow[9];
                $dif_exp=round($dif_p);

                $sql="insert into journal2 values('$taxdrrow[0]','$taxdrrow[15]','Dr','$ledger','$dif_exp','0','$taxdrrow[4]','$brcode','GST Income','GST Income','$taxdrrow[14]','0','0','0000-00-00')";
                mysqli_query($db,$sql) or die('Led Post Err1'.mysqli_error());
            }
	 $jvdifres=mysqli_query($db,"select sum(dramt)-sum(cramt) from journal2 where jvno='$taxdrrow[15]' and brcode='$brcode'");
            $jvdifrow=mysqli_fetch_row($jvdifres);
            $dif=$jvdifrow[0];
            if($dif>0)
            {
                $xx=abs($dif);
                $sql="insert into journal2 values('$taxdrrow[0]','$taxdrrow[15]','Cr','Round Off','0', '".$xx."','Round of GST INC No: $taxdrrow[14]','$brcode','GST Income','REF No','$taxdrrow[14]','0','0','0000-00-00')";
                mysqli_query($db,$sql) or die("J V Write error dif1:".mysqli_error());
            }
            else if($dif<0)
            {
                $xx=abs($dif);
                $sql="insert into journal2 values('$taxdrrow[0]','$taxdrrow[15]','Dr','Round Off','".$xx."','0','Round of GST INC No: $taxdrrow[14]','$brcode','GST Income','REF No','$taxdrrow[14]','0','0','0000-00-00')";
                mysqli_query($db,$sql) or die("J V Write error dif:".mysqli_error());
            }
	}
        if($flg1==$i)
        {
           echo "<script>alert('Data saved')</script>";
	       print "<script>window.open('gst_income_print.php?invno=$refno'); </script>";
          print "<script>document.getElementById('savecount').value=1; document.location='gstmltple2_out.php?taxtype=".$taxtype."'; </script>";
        }

	*/

?>
