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


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'];
if($taxtype=='INTRASTATE')
{
$query11=mysqli_query($db,"select max(refno) from gst_creditnote  where brcode='$brcode'");
$rw11=mysqli_fetch_array($query11);
if($brcode=='PSN236')
	$mprefix = 'PSNVDN2526';


if($rw11[0]<='')
{
    $refno=$mprefix.'001';
}
else
{
	$refno=substr($rw11[0],-3)+1;
	$refno=substr('000'.$refno,-3);
	$refno=$mprefix.$refno;
}
}
else
{
	    $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;
    $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++;
    }

echo $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($hsncode[$j]=='')
                        {
                            print "<script>document.getElementById('myTable').rows[$k].cells[0].style.color='red';</script>";
                            $hsnflg++;
                        }
	/*
                    $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 "<b style='color: red;'>Check the Amt of Red marked row(s)</b>";
    }
    elseif($hsnflg>0)
    {
        echo "<b style='color: red;'>Enter Hsncode of Red marked row(s)</b>";
    }
    elseif($spflg>0)
    {
        echo "<b style='color: red;'>Check the itemcode of Red marked row(s)</b>";
    }
    else
    {
        $rr=mysqli_query($db,"select max(exp_no) from gst_creditnote  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_creditnote (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_creditnote (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]')";
                //echo $sql1;
            }
           
            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_creditnote  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='CGST Input '.$taxdrrow[10].'%';
                $sql="insert into journal2 values('$taxdrrow[0]','$taxdrrow[15]','Cr','$ledger','0','$taxdrrow[6]','$taxdrrow[4]','$brcode','GST Credit Note','GST Credit Note','$taxdrrow[14]','0')";
                mysqli_query($db,$sql) or die('Led Post Err1'.mysqli_error());
            }
            if($taxdrrow[7]>0)
            {
                $ledger='SGST Input '.$taxdrrow[11].'%';
                $sql="insert into journal2 values('$taxdrrow[0]','$taxdrrow[15]','Cr','$ledger','0','$taxdrrow[7]','$taxdrrow[4]','$brcode','GST Credit Note','GST Credit Note','$taxdrrow[14]','0')";
                mysqli_query($db,$sql) or die('Led Post Err1'.mysqli_error());
            }
            if($taxdrrow[8]>0)
            {
                $ledger='IGST Input '.$taxdrrow[12].'%';
                $sql="insert into journal2 values('$taxdrrow[0]','$taxdrrow[15]','Cr','$ledger','0','$taxdrrow[8]','$taxdrrow[4]','$brcode','GST Credit Note','GST Credit Note','$taxdrrow[14]','0')";
                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_creditnote  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 Credit Note','GST Credit Note','$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 Credit Note','GST Credit Note','$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 Credit Note','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 Credit Note','REF No','$taxdrrow[14]','0')";
                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_creditnote_print.php?invno=$refno'); </script>";
           print "<script>document.getElementById('savecount').value=1; document.location='gst_creditnote.php?taxtype=".$taxtype."'; </script>";
        }
    }
	
}
elseif($savecount==1)
{
	echo "Data already saved";
  	echo "<script> document.location='gst_creditnote.php';</script>";
}
?>
