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

include_once('test.php');
$savecount=$_REQUEST['savecount'];
$taxtype=$_REQUEST['taxtype'];
$refno=$_REQUEST['refno'];

date_default_timezone_set("Asia/Kolkata");
$logdt=date("Y-m-d");
$logtime=date("H:i:s");
if($savecount==0) 
{
  
    $exphd=$_REQUEST['exphd'];
    if($exphd=='-select-')
    {
        print "<script>alert('Invalid Exp Head...!!!')</script>";
        return;
    }
    $leddt=$_REQUEST['leddt'];
    $supnm=$_REQUEST['supnm'];
    $narrat=$_REQUEST['narrat'];
$query111=mysqli_query($db,"select refno from gst_incomecust where brcode='$brcode' and refno='$refno' ");
if(mysqli_num_rows($query111)>0)
{
print "<script>alert('Invoice Number Already Exist...!!!')</script>";
        return;
}

$acpd=mysqli_query($db,"select * from acperiod") or die ('err'.mysqli_error($db));
$acrow=mysqli_fetch_row($acpd);

if($leddt<$acrow[0] || $leddt>$acrow[1] )
{
  print "<script>alert('Date Should Be in Current Financial Year...!!!')</script>";
        return;  
}
$query11=mysqli_query($db,"select max(refno) from gst_incomecust where brcode='$brcode'");
$rw11=mysqli_fetch_array($query11);
$mprefix = $brcode;
if($rw11[0]<='')
{
  //  $refno=$mprefix.'000001';
}
else
{
//	$refno=substr($rw11[0],-6)+1;
//	$refno=substr('000000'.$refno,-6);
//	$refno=$mprefix.$refno;
}

    
    $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;
    $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;
    $cessp=$_REQUEST['cessp'];
    foreach($cessp as $a)
    {
        $cessp[$i]=$a;
        $i++;
    }
    
     $i=0;
    $cessamt=$_REQUEST['cessamt'];
    foreach($cessamt as $a)
    {
        $cessamt[$i]=$a;
        $i++;
    }
    
     $i=0;
    $tcsp=$_REQUEST['tcsp'];
    foreach($tcsp as $a)
    {
        $tcsp[$i]=$a;
        $i++;
    }
    
     $i=0;
    $tcsamt=$_REQUEST['tcsamt'];
    foreach($tcsamt as $a)
    {
        $tcsamt[$i]=$a;
        $i++;
    }
    
    $i=0;
    $gndamt=$_REQUEST['gndamt'];
    foreach($gndamt as $a)
    {
        $gndamt[$i]=$a;
        $i++;
    }
    
    $i=0;
    $ntamt=$_REQUEST['ntamt'];
    foreach($ntamt as $a)
    {
        $ntamt[$i]=$a;
        $i++;
    }
         

    $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(($tot[$j]=='')||($tot[$j]=='0'))
                        {
                            print "<script>document.getElementById('myTable').rows[$k].cells[0].style.color='red';</script>";
                            $totflg++;
                        }
                    $qry=mysqli_query($db,"select * from partmaster where partcode ='$partcode[$j]'");
                    if(mysqli_num_rows($qry)<=0)
                        {
                            print "<script>document.getElementById('myTable').rows[$k].cells[0].style.color='red';</script>";
                            $spflg++;
                        }
                        */
    }
    if($flg>0)
    {
        echo "<small>Check the Amt of Red marked row(s)</br></small>";
    }
    elseif($totflg>0)
    {
        echo "<small>Enter Total of Red marked row(s)</small>";
    }
    elseif($spflg>0)
    {
        echo "<small>Check the itemcode of Red marked row(s)</br></small>";
    }
    else
    {
        $rr=mysqli_query($db,"select max(exp_no) from gst_incomecust 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++)
        {
            
            $result22=mysqli_query($db,"select hsncode from partmaster where partname='$exphd' ")or die(mysqli_error($db));

$row22=mysqli_fetch_row($result22);
            if($taxtype=='INTRASTATE')
            {
                $exp_amt = $befrtx[$m]+$cgstamt[$m]+$sgstamt[$m];
                $sql1="INSERT INTO gst_incomecust(exp_no,date,expense_head,vendor,narr,before_tax,cgstp,cgstamt,sgstp,sgstamt,expense_amount,brcode,vendrinvno,vendrinvdt,jvno,txtype,acchead,refno,fcessp,fcessamt,tcsp,tcsamt,hsncode) VALUES ('$expno','$leddt','$exphd','$supnm','$narrat','$befrtx[$m]','$cgstp[$m]','$cgstamt[$m]','$sgstp[$m]','$sgstamt[$m]','$exp_amt','$brcode','0','0000-00-00','$jvno','$taxtype','$led[$m]','$refno','$cessp[$m]','$cessamt[$m]','$tcsp[$m]','$tcsamt[$m]','$row22[0]')";
            }
            if($taxtype=='INTERSTATE')
            {
                $exp_amt = $befrtx[$m]+$igstamt[$m];
                $sql1="INSERT INTO gst_incomecust(exp_no,date,expense_head,vendor,narr,before_tax,igstp,igstamt,expense_amount,brcode,vendrinvno,vendrinvdt,jvno,txtype,acchead,refno,tcsp,tcsamt,hsncode) VALUES ('$expno','$leddt','$exphd','$supnm','$narrat','$befrtx[$m]','$igstp[$m]','$igstamt[$m]','$exp_amt','$brcode','0','0000-00-00','$jvno','$taxtype','$led[$m]','$refno','$tcsp[$m]','$tcsamt[$m]','$row22[0]')";
            }
           
          // print $sql1;
            mysqli_query($db,$sql1) or die('insert Err1'.mysqli_error($db));
            $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,sum(fcessamt),sum(tcsamt) from gst_incomecust where exp_no='$expno' and brcode='$brcode' group by exp_no,acchead,cgstp")or die('Led Post Err3'.mysqli_error($db));
	    while($taxdrrow=mysqli_fetch_array($taxdrres))
        {
            
            
            if($taxdrrow[6]>0)
            {
                $ledger='OUTPUT TAX';
                $sql="insert into journal2 values('$taxdrrow[0]','$taxdrrow[15]','Cr','$ledger','0','$taxdrrow[6]','$taxdrrow[4]','$brcode','GST Income Inv','GST Income','$taxdrrow[14]','0','$logtime $user','$logdt')";
                mysqli_query($db,$sql) or die('Led Post Err1'.mysqli_error($db));
            }
            if($taxdrrow[7]>0)
            {
                $ledger='OUTPUT TAX';
                $sql="insert into journal2 values('$taxdrrow[0]','$taxdrrow[15]','Cr','$ledger','0','$taxdrrow[7]','$taxdrrow[4]','$brcode','GST Income Inv','GST Income','$taxdrrow[14]','0','$logtime $user','$logdt')";
                mysqli_query($db,$sql) or die('Led Post Err1'.mysqli_error($db));
            }
            if($taxdrrow[8]>0)
            {
                $ledger='OUTPUT TAX';
                $sql="insert into journal2 values('$taxdrrow[0]','$taxdrrow[15]','Cr','$ledger','0','$taxdrrow[8]','$taxdrrow[4]','$brcode','GST Income Inv','GST Income','$taxdrrow[14]','0','$logtime $user','$logdt')";
                mysqli_query($db,$sql) or die('Led Post Err1'.mysqli_error($db));
            }
            
            
            if($taxdrrow[16]>0)
            {
                $ledger='Flood Cess';
                $sql="insert into journal2 values('$taxdrrow[0]','$taxdrrow[15]','Cr','$ledger','0','$taxdrrow[16]','$taxdrrow[4]','$brcode','GST Income Inv','GST Income','$taxdrrow[14]','0','$logtime $user','$logdt')";
                mysqli_query($db,$sql) or die('Led Post Err1'.mysqli_error($db));
            }
            
            if($taxdrrow[17]>0)
            {
                $ledger='TCS PAYABLE';
                $sql="insert into journal2 values('$taxdrrow[0]','$taxdrrow[15]','Cr','$ledger','0','$taxdrrow[17]','$taxdrrow[4]','$brcode','GST Income Inv','GST Income','$taxdrrow[14]','0','$logtime $user','$logdt')";
                mysqli_query($db,$sql) or die('Led Post Err1 tcs'.mysqli_error($db));
            }
           
        }
        
	$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_incomecust where exp_no='$expno' and brcode='$brcode' group by exp_no")or die('Led Post Err3'.mysqli_error($db));
	    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 Inv','GST Income','$taxdrrow[14]','0','$logtime $user','$logdt')";
            mysqli_query($db,$sql) or die('Led Post Err1'.mysqli_error($db));
		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 Inv','GST Income','$taxdrrow[14]','0','$logtime $user','$logdt')";
                mysqli_query($db,$sql) or die('Led Post Err1'.mysqli_error($db));
            }
	 $jvdifres=mysqli_query($db,"select sum(cramt)-sum(dramt) 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 Inv','REF No','$taxdrrow[14]','0','$logtime $user','$logdt')";
                mysqli_query($db,$sql) or die("J V Write error dif1:".mysqli_error($db));
            }
            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 Inv','REF No','$taxdrrow[14]','0','$logtime $user','$logdt')";
                mysqli_query($db,$sql) or die("J V Write error dif:".mysqli_error($db));
            }
	}
        if($flg1==$i)
        {
            $ip_address = "$jvno|GST Income|Add";
log_entry($brcode,$user,$ip_address,"GST Income entry-$refno",$db);

           echo "<script>alert('Data saved')</script>";
	    print "<script>window.open('gst_income_printcust.php?invno=$refno'); </script>";
           print "<script>document.getElementById('savecount').value=1; document.location='gstmultislct2_outcust.php?taxtype=".$taxtype."'; </script>";
        }
    }
	
}
elseif($savecount==1)
{
	echo "Data already saved";
  	echo "<script> document.location='gstmultislct2_outcust.php';</script>";
}
?>
