<?
session_start();
include_once("test.php");
include_once('opendb.php');
$brcode=$_SESSION['brcode'];
$user=$_SESSION['user'];
//$br=$_SESSION['branch'];
$led=$_REQUEST['led'];
$grp=$_REQUEST['cr'];
$grp=trim($grp);
$op=$_REQUEST['op'];
$desc=$_REQUEST['desc'];
$dbno=$_REQUEST['dbno'];
$tax=$_REQUEST['tax'];
$amt2=$_REQUEST['amt2'];
$taxp=$_REQUEST['taxp'];
$taxtype=$_REQUEST['taxtype'];
$d=$_REQUEST['d'];
$m=$_REQUEST['m'];
$y=$_REQUEST['y'];
$dt=$y.'-'.$m.'-'.$d;
if(strlen($user)<=0)
{
  	print "<script> alert('Invalid User'); document.location='inde.php' </script>";
  	return;
}
$led=trim($led);
if(strlen($led)<=0 || strlen($grp)<=0 || strlen($op)<=0)
{
 	print "<script>alert('Insufficient Entry'); history.back(1);</script>";
 	return;
}
//if(ereg("[&']",$led))
//{
 	//print "<script> alert('Please avoid & and \''); history.back(1);</script>";
 	//return;
//}

$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($dt<$acf || $dt>$act)
             {
                 print "<script> alert('Invalid Date, Entry Possible for Current Financial Year only'); </script>";
                 return;
             }
             
$code=substr($led,0,3).substr(time(),7,3);
$code=str_replace(' ','-',$code);
$led=str_replace('  ',' ',$led);

//$jvno=$brcode.'*'.$user.'*'.$tim;
 
 $re1=mysqli_query($db,"select max(jvno) from journal2 where brcode='$brcode'");
       $jvrow=mysqli_fetch_row($re1);
       $jvno = $jvrow[0]+1;
      
$sql="insert into crnote(date,dbno,type,partic,dramt,cramt,narr,brcode,no,against,dbto,j_type,b4tax,taxp,taxamt,taxtype) values('$dt','$dbno','Cr','$grp','0','$op','$desc' ,'$brcode','0','Discount Allowed','$led','$jvno','$amt2','$taxp','$tax','$taxtype')";
//print $sql;
 mysqli_query($db,$sql)or die("cr reg ins error 21:".mysqli_error($db));

if($taxp>0)
{
if($taxtype=='INTRA STATE')
{
$gstp=$taxp/2;
$gsta=$tax/2;



$sql="insert into journal2(date,jvno,type,partic,dramt,cramt,narr,brcode,j_type,against,no,tr_no,docdt) values('$dt','$jvno','Dr','Output CGST $gstp on credit notes','$gsta', '0','CR Note No($dbno) $desc','$brcode','CR NOTE','inv','$dbno','$dbno','$dt')";
mysqli_query($db,$sql) or die("J V Write error 2:".mysqli_error($db));
$sql="insert into journal2 (date,jvno,type,partic,dramt,cramt,narr,brcode,j_type,against,no,tr_no,docdt) values('$dt','$jvno','Dr','Output SGST $gstp on credit notes','$gsta', '0','CR Note No($dbno) $desc','$brcode','CR NOTE','inv','$dbno','$dbno','$dt')";
mysqli_query($db,$sql) or die("J V Write error 2:".mysqli_error($db));

 $sql="insert into journal2 (date,jvno,type,partic,dramt,cramt,narr,brcode,j_type,against,no,tr_no,docdt)values('$dt','$jvno','Dr','$led','$amt2','0','CR Note No($dbno) $desc','$brcode','CR NOTE','inv','$dbno','$dbno','$dt')";
mysqli_query($db,$sql) or die("J V Write error 3:".mysqli_error($db));

$sql="insert into journal2 (date,jvno,type,partic,dramt,cramt,narr,brcode,j_type,against,no,tr_no,docdt) values('$dt','$jvno','Cr','$grp','0','$op', 'CR Note No($retno) $desc','$brcode','CR NOTE','inv','$dbno','$dbno','$dt')";
mysqli_query($db,$sql) or die("J V Write error 1:".mysqli_error($db));
}
else
{
$sql="insert into journal2 (date,jvno,type,partic,dramt,cramt,narr,brcode,j_type,against,no,tr_no,docdt) values('$dt','$jvno','Dr','Output IGST $taxp on credit notes','$tax', '0','CR Note No($dbno) $desc','$brcode','CR NOTE','inv','$dbno','$dbno','$dt')";
mysqli_query($db,$sql) or die("J V Write error 2:".mysqli_error($db));

 $sql="insert into journal2(date,jvno,type,partic,dramt,cramt,narr,brcode,j_type,against,no,tr_no,docdt) values('$dt','$jvno','Dr','$led','$amt2','0','CR Note No($dbno) $desc','$brcode','CR NOTE','inv','$dbno','$dbno','$dt')";
mysqli_query($db,$sql) or die("J V Write error 3:".mysqli_error($db));

$sql="insert into journal2(date,jvno,type,partic,dramt,cramt,narr,brcode,j_type,against,no,tr_no,docdt) values('$dt','$jvno','Cr','$grp','0','$op', 'CR Note No($retno) $desc','$brcode','CR NOTE','inv','$dbno','$dbno','$dt')";
mysqli_query($db,$sql) or die("J V Write error 1:".mysqli_error($db));


}
}
else
{


$sql="insert into journal2(date,jvno,type,partic,dramt,cramt,narr,brcode,j_type,against,no,tr_no,docdt) values('$dt','$jvno','Dr','$led','$op','0','$desc','$brcode','CR NOTE','inv','$dbno','$dbno','$dt')";
           
 mysqli_query($db,$sql);
 
 $sql="insert into journal2(date,jvno,type,partic,dramt,cramt,narr,brcode,j_type,against,no,tr_no,docdt) values('$dt','$jvno','Cr','$grp','0','$op','$desc','$brcode','CR NOTE','inv','$dbno','$dbno','$dt')";
           
 mysqli_query($db,$sql);
}
           
            


print "Credit not has been posted";
include("crnote1.php");
?>
