<?
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'];
$pmode=$_REQUEST['pmode'];
//$updprint=$_REQUEST['updprint'];
//echo $updprint;

$us=mysqli_query($db,"select level from bruser where username='$user' and brcode='$brcode' ")or die(mysqli_error($db));
$r=mysqli_fetch_row($us);
$user_level=$r[0];

if($savecount==0) 
{
  
    $exphd=$_REQUEST['exphd'];
    if($exphd=='')
    {
        print "<script>alert('Invalid Exp Head...!!!')</script>";
        return;
    }
    
    
    $rndoff=$_REQUEST['rndoff'];
    $leddt=$_REQUEST['leddt'];
    $vndinvno=$_REQUEST['vndinvno'];
    $supnm=$_REQUEST['supnm'];
    $supdt=$_REQUEST['supdt'];
    $narrat=$_REQUEST['narrat'];
    $tdsledger=$_REQUEST['tds'];
    
    $acpd=mysqli_query($db,"select * from acperiod") or die ('err'.mysqli_error($db));
     $acrow=mysqli_fetch_row($acpd);

      $acf= $acrow[0];
      $act= $acrow[1];
      
      if($leddt<$acf || $leddt>$act)
             {
                 print "<script> alert('Invalid Date, Entry Possible for Current Financial Year only'); </script>";
                 return;
             }
        
        if($user_level<=4 && $user!='midhun')
{
$sq="select to_days(curdate())-to_days('$leddt')";
$dtre=mysqli_query($db,$sq)or die(mysqli_error($db));
$dtrow=mysqli_fetch_row($dtre);

if($dtrow[0]>10 || $dtrow[0]<0)
{
 print "<script> alert('Invalid Date, Entry possible for last 7 days only'); history.back(1); </script>";
 return;
}

}



    
    if($exphd!='')
    {
     $qry=mysqli_query($db,"select * from ledger where led='$exphd' and brcode='$brcode'");
 		if(mysqli_num_rows($qry)<=0)
 		{

  		  print "<script>alert('Invalid Exp Head...!!!')</script>";
          return;
 		}
    }  
     //$rndoff
     
    
    
      if($supnm=='')
    {
        print "<script>alert('Invalid supplier Head...!!!')</script>";
        return;
        
        
    } 
    
     if($supnm!='')
    {
     $qry=mysqli_query($db,"select * from ledger where led='$supnm' and brcode='$brcode'");
 		if(mysqli_num_rows($qry)<=0)
 		{

  		  print "<script>alert('Invalid Supplier Head led...!!!')</script>";
          return;
 		}
    }
    
    
    
    
$query11=mysqli_query($db,"select max(refno) from gst_expense where brcode='$brcode'");
$rw11=mysqli_fetch_array($query11);
if($rw11[0]<1)
{
     $refno='000001';
}
else
{
	$refno=substr($rw11[0],-6)+1;
	$refno=substr('000000'.$refno,-6);
}

    
    $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;
    $tcsamt1=$_REQUEST['tcsamt1'];
    foreach($tcsamt1 as $a)
    {
        $tcsamt1[$i]=$a;
     $i++;
        
    }
    
    $i=0;
    $tcsamt=$_REQUEST['tcsamt'];
    foreach($tcsamt as $a)
    {
        $tcsamt[$i]=$a;
     
        if($tcsamt[$i]>0)
    {
       
    if($tdsledger=='')
    {
        print "<script>alert('Invalid TDS Head...!!!')</script>";
        return;
    } 
    
         if($tdsledger!='')
    {
     $qry=mysqli_query($db,"select * from ledger where led='$tdsledger' and brcode='$brcode'");
 		if(mysqli_num_rows($qry)<=0)
 		{

  		  print "<script>alert('Invalid TDS Head...!!!')</script>";
          return;
 		}
    }
    
    
        
    }
        
        
        
        $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++;
    }
         

    $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_expense 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_expense(exp_no,date,expense_head,vendor,narr,before_tax,cgstp,cgstamt,sgstp,sgstamt,expense_amount,brcode,vendrinvno,vendrinvdt,jvno,txtype,acchead,refno,reverse,tdsledger,tdsamt,tcsamt) 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','N','$tdsledger','$tcsamt[$m]','$tcsamt1[$m]')";
            }
            if($taxtype=='INTERSTATE')
            {
                $sql1="INSERT INTO gst_expense(exp_no,date,expense_head,vendor,narr,before_tax,igstp,igstamt,expense_amount,brcode,vendrinvno,vendrinvdt,jvno,txtype,acchead,refno,reverse,tdsledger,tdsamt,tcsamt) VALUES ('$expno','$leddt','$exphd','$supnm','$narrat','$befrtx[$m]','$igstp[$m]','$igstamt[$m]','$ntamt[$m]','$brcode','$vndinvno','$supdt','$jvno','$taxtype','$led[$m]','$refno','N','$tdsledger','$tcsamt[$m]','$tcsamt1[$m]')";
            }
           
            mysqli_query($db,$sql1) or die('gst 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 from gst_expense where exp_no='$expno' and brcode='$brcode' group by exp_no,acchead")or die('Led Post Err3'.mysqli_error($db));
	    while($taxdrrow=mysqli_fetch_array($taxdrres))
        {
            
            
            if($taxdrrow[6]>0)
            {
                $gsta=$taxdrrow[6]+$taxdrrow[7];
                $ledger='Input Tax';
                $sql="insert into journal2 values('$taxdrrow[0]','$taxdrrow[15]','Dr','$ledger','$gsta','0','$taxdrrow[4]','$brcode','GST Expense','GST Expense','$taxdrrow[14]','0','0','0000-00-00')";
                mysqli_query($db,$sql) or die('Led Post Err1'.mysqli_error($db));
            }
           /* if($taxdrrow[7]>0)
            {
                $ledger='Input SGST Expense'.$taxdrrow[11].'%';
                $sql="insert into journal2 values('$taxdrrow[0]','$taxdrrow[15]','Dr','$ledger','$taxdrrow[7]','0','$taxdrrow[4]','$brcode','GST Expense','GST Expense','$taxdrrow[14]','0','0','0000-00-00')";
                mysqli_query($db,$sql) or die('Led Post Err1'.mysqli_error($db));
            }*/
            if($taxdrrow[8]>0)
            {
                $ledger='Input Tax';
                $sql="insert into journal2 values('$taxdrrow[0]','$taxdrrow[15]','Dr','$ledger','$taxdrrow[8]','0','$taxdrrow[4]','$brcode','GST Expense','GST Expense','$taxdrrow[14]','0','0','0000-00-00')";
                mysqli_query($db,$sql) or die('Led Post Err1'.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,tdsledger,tdsamt,tcsamt from gst_expense 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]','Dr','$ledger','$taxdrrow[5]','0','$taxdrrow[4]','$brcode','GST Expense','GST Expense','$taxdrrow[14]','0','0','0000-00-00')";
            mysqli_query($db,$sql) or die('Led Post Err1'.mysqli_error($db));
		
		if($taxdrrow[9]>0)
            {
                $ledger=$taxdrrow['vendor'];
                $dif_p=$taxdrrow[9];
                 if($rndoff=='0' || $rndoff=='')
				{
				$dif_exp=$dif_p;
				}
				else
				{
               		 $dif_exp=round($dif_p);
				}
                //$dif_exp=round($dif_p);

                $sql="insert into journal2 values('$taxdrrow[0]','$taxdrrow[15]','Cr','$ledger','0','$dif_exp','$taxdrrow[4]','$brcode','GST Expense','GST Expense','$taxdrrow[14]','0','0','0000-00-00')";
                mysqli_query($db,$sql) or die('Led Post Err1'.mysqli_error($db));
       
    
                	if($taxdrrow[17]>0)
            {
                $ledger=$taxdrrow['tdsledger'];
                 $sql="insert into journal2 values('$taxdrrow[0]','$taxdrrow[15]','Cr','$ledger','0','$taxdrrow[17]','$taxdrrow[4]','$brcode','GST Expense','GST Expense','$taxdrrow[14]','0','0','0000-00-00')";
                mysqli_query($db,$sql) or die('Led Post Err1'.mysqli_error($db));
            }   
              
                	if($taxdrrow[18]>0)
            {
                $ledger='TCS RECEIVABLE';
                 $sql="insert into journal2 values('$taxdrrow[0]','$taxdrrow[15]','Dr','$ledger','$taxdrrow[18]','0','$taxdrrow[4]','$brcode','GST Expense','GST Expense','$taxdrrow[14]','0','0','0000-00-00')";
                mysqli_query($db,$sql) or die('Led Post Err1tcs'.mysqli_error($db));
            }     
            }
	 $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]','Dr','Round Off','".$xx."', '0','Round of GST EXP No: $taxdrrow[14]','$brcode','GST Expense','REF No','$taxdrrow[14]','0','0','0000-00-00')";
                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]','Cr','Round Off','0','".$xx."','Round of GST EXP No: $taxdrrow[14]','$brcode','GST Expense','REF No','$taxdrrow[14]','0','0','0000-00-00')";
                mysqli_query($db,$sql) or die("J V Write error dif:".mysqli_error($db));
            }
	}
        if($flg1==$i)
        {
            $ip_address = "$jvno|GST Exp|Add";
            log_entry($brcode,$user,$ip_address,"GST exp entry-$refno",$db);

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