<? 
session_start();
$brcode=$_SESSION['brcode'];
$user=$_SESSION['user'];
if(strlen($brcode)<=0)
{
 print "<script> alert('Session timed out'); parent.document.location='../ind.php'; </script>";
 return;
}
$crno=$_REQUEST['crno'];
$jvno=$_REQUEST['jvno'];
include("../opendb.php");
include("../test.php");
if(isset($_POST['save']))
{
    $retno=$_REQUEST['retno'];
  	$rr=mysqli_query($db,"select jvno from crnote_gst where brcode='$brcode' and ret_num='$retno'");
  	$ret_row=mysqli_fetch_array($rr);
  	
	mysqli_query($db,"delete from journal2 where jvno='$ret_row[0]' and brcode='$brcode'");
	mysqli_query($db,"delete from crnote_gst where jvno='$ret_row[0]' and ret_num='$retno' and brcode='$brcode'");

   		  	
 	//$cust=$custcode;
	$jvr=mysqli_query($db,"select max(jvno) from journal2 where brcode='$brcode'");
	$j_row=mysqli_fetch_row($jvr);
  	if($j_row[0]<=0)
   		$jvno=1;
  	else
  		$jvno=($j_row[0]+1);
  		
  		$da=$_REQUEST['da'];
  		$dt=$_REQUEST['dt'];
   		$inv=$_REQUEST['inv'];
   		$mcode=$_REQUEST['mcode'];
   		$config=$_REQUEST['config'];
   		$disamt=$_REQUEST['disamt'];
   		$cust=$_REQUEST['cust'];
   		$tax=$_REQUEST['tax'];
   		$taxp=$_REQUEST['taxp'];
   		$amt2=$_REQUEST['amt2'];
   		$custcode=$_REQUEST['custcode'];
  		$taxtyp=$_REQUEST['taxtyp'];
	$sql="insert into crnote_gst values('$retno','$dt','$da','$inv','$taxtyp','$config', '$disamt','$custcode','$brcode','$tax','$taxp','$amt2','$jvno')";
	//print $sql.'<br>';
	mysqli_query($db,$sql) or die(mysqli_error());
	$kk=mysqli_query($db,"select code,acname from cust_master where code='$custcode' and branch='$brcode'") or die('Customer error:'.mysqli_error());
	$rcus=mysqli_fetch_row($kk);
	$cust_name=$rcus[1];
	if($taxp=='28' || $taxp=='18')
	{
		$taxp1 = $taxp/2;
		$tax1  = $tax/2;
		if($taxtyp=='INTRASTATE')
		{
		    
		    
		$sql="insert into journal2 values('$dt','$jvno','Dr','Input CGST $taxp1% Credit Note','$tax1', '0','CR Note No(PSNCR$retno) Invoice No.:$inv','$brcode','CR Note','DA','$da','$retno')";
		mysqli_query($db,$sql) or die("J V Write error 2:".mysqli_error());
		$sql="insert into journal2 values('$dt','$jvno','Dr','Input SGST $taxp1% Credit Note','$tax1', '0','CR Note No(PSNCR$retno) Invoice No.:$inv','$brcode','CR Note','DA','$da','$retno')";
		mysqli_query($db,$sql) or die("J V Write error 2:".mysqli_error());
		$sql="insert into journal2 values('$dt','$jvno','Dr','Discount allowed(vehicles)GST $taxp%','$amt2','0','CR Note No(PSNCR$retno) Invoice No.:$inv','$brcode','CR Note','DA','$da','$retno')";
		mysqli_query($db,$sql) or die("J V Write error 3:".mysqli_error());
		}
		if($taxtyp=='INTERSTATE')
		{
		    $sql="insert into journal2 values('$dt','$jvno','Dr','Input IGST $taxp% Credit Note','$tax', '0','CR Note No(PSNCR$retno) Invoice No.:$inv','$brcode','CR Note','DA','$da','$retno')";
		    mysqli_query($db,$sql) or die("J V Write error 2:".mysqli_error());
		    $sql="insert into journal2 values('$dt','$jvno','Dr','Discount allowed(vehicles)IGST $taxp%','$amt2','0','CR Note No(PSNCR$retno) Invoice No.:$inv','$brcode','CR Note','DA','$da','$retno')";
		    mysqli_query($db,$sql) or die("J V Write error 3:".mysqli_error());
		}
		
		
	
		$sql="insert into journal2 values('$dt','$jvno','Cr','$cust_name','0','$disamt', 'CR Note No(PSNCR$retno) Invoice No.:$inv','$brcode','CR Note','DA','$da','$retno')";
		mysqli_query($db,$sql) or die("J V Write error 1:".mysqli_error());
	}
	else
	{	
	 	$sql="insert into journal2 values('$dt','$jvno','Dr','Discount allowed(vehicles)' ,'$amt2','0','Dis All No: $retno','$brcode','Dis All','DA','$da','$retno')";
		mysqli_query($db,$sql) or die("J V Write error 3:".mysqli_error());
	
		$sql="insert into journal2 values('$dt','$jvno','Cr','$cust_name','0','$disamt', 'Dis All No: $retno','$brcode','Dis All','DA','$da','$retno')";
		mysqli_query($db,$sql) or die("J V Write error 1:".mysqli_error());
	}
	print"<script>window.open('prncrnote_gst.php?no=$retno'); </script>";
	print "<font color=green><B>Data has been saved</b></font>";
	print"<script>document.location='crnote_gst.php';</script>";

	log_entry($brcode,$user,$REMOTE_ADDR,"Credit Note JVNo: $jvno, DA No: $da, Crno:$retno");


}
?>
<link type="text/css" href="../css/form.css" rel="stylesheet" />
<link type="text/css" href="../jquery/jquery-ui.min.css" rel="stylesheet" />

<script type="text/javascript" src="../js/jquery.js"></script>
<script type="text/javascript" src="../jquery/jquery-1.7.min.js"></script>
<script type="text/javascript" src="../jquery/jquery-ui.min.js"></script>
<script type="text/javascript" src="../jquery/jquery.jqEasyCharCounter.min.js"></script>

<script type="text/javascript">
  var rawTags;
   var inputField;
   var  initInputFieldValue;
   var dataList;
    var initNbVirgules;

$(function(){
$(".cr").live("keydown.autocomplete",function(){
$cr = $(this);

$row = $cr.parent().parent();
$(this).autocomplete({
		source: function(request, response ) {

		$.ajax({
			url: "searchda.php",
			dataType: "json",
			data: "key="+$cr.val(),
			success: function( data ) {
				response( $.map( data, function( item ) {


					return {
						label: '<span class="la-code">'+item.cr+' </span><span class="la-name">'+item.crcbal+'</span><span class="la-stok"> '+ item.stock +'</span><span class="la-rate"> '+item.rate +'</span><span class="la-reduct"> '+item.reduct+'</span><span class="la-tax"> '+item.tax+'</span>',
						value: item.cr,
						crcbal:item.crcbal,
						tax:item.tax,

						stock:item.stock,
						rate:item.rate,
						reduct:item.reduct,


					}
				}));
			}
		});
	}

,
	minLength: 2,
	select: function( event, ui ) {
	$row.find('.cr').val(ui.item.cr);

       $row.find('.config').val(ui.item.crcbal);
	$row.find('.cust').val(ui.item.stock);
	$row.find('.inv').val(ui.item.rate);

	$row.find('.disc').val(ui.item.tax);
	$row.find('.ccode').val(ui.item.reduct);
	$row.find('.cr_amt').focus();
	
	 }

	}).data("ui-autocomplete")._renderItem = function (ul, item) {
         return $("<li></li>")
             .data("item.autocomplete", item)
             .append( item.label )
             .appendTo(ul);
     };


});

}); 

</script>
<script>
var inv=[];
var mcode=[];
var config=[];
var netamt=[];
var cust=[];
var unit=[];
var disc=[];
var tax=[];
var taxp=[];
var amt2=[];
var ccode=[];

function valid()
{ 
 submitOK="true";
 if(document.f1.da.value.length<=0)
 {
   //submitOK="false";
   //document.f1.da.focus();
   //alert('Invalid DA No.');
 }
 if(isNaN(document.f1.disamt.value)||document.f1.disamt.value<=0)
 {
   submitOK="false";
  document.f1.disamt.focus();
   alert('Invalid Discount Amount');
 }
  if(submitOK=="true")
   return true;
  else
   return false;
}

</script>
<style>
body,td{font-family:'arial narrow';letter-spacing:1;font-size:13}
th{background-color:c1d799}
</style>
<body bgcolor=ivory>

<body class=bd >
<CENTER>
<h3>CREDIT NOTE <BR>DISCOUNT ALLOWED (VEHICLE) (No. 
<?  

  $sql="select a.custname,b.ret_num,b.date, b.da, b.sale_invoice,b.netamt,b.tax,b.taxp,b.amt2,b.jvno,a.code,b.mcode,b.config from cust_master a,crnote_gst b where b.brcode='$brcode' and b.retfrom=a.code and b.ret_num='$crno'";
  $rr=mysqli_query($db,$sql);
  $ret=mysqli_fetch_array($rr);
  
  print $crno;


?>
 )</H3>


<form name='f1' action='<?print $PHP_SELF?>' method='POST' onSubmit="return valid()">

<table border=1 class=report style=" width: 50%;">
<tr><td>Date </td><td>   <input type=text size=10 name='dt' value='<?print $ret[2]?>'>
			
</td><td colspan=1>CrNo</td><td><input type=text name=retno value=<?echo $crno?> readonly></td>
<td>Tax Type</td>
    
<td><select name='taxtyp'>

<option value="INTRASTATE"<?php if ($ret[11] == 'INTRASTATE') echo ' selected="selected"'; ?>>LOCAL</option>
<option value="INTERSTATE"<?php if ($ret[11] == 'INTERSTATE') echo ' selected="selected"'; ?>>IGST</option>
</td>


</tr>
<tr><td>D.A</td><td><input type=text name='da' value='<?echo $ret[3]?>'  size=15 class='cr' autocomplete='off' list='htmlListTags'>
</td><td>Customer </td><td><input type=text readonly name='cust' class=cust value='<?echo $ret[0]?>' ></td><td>Invoice </td><td>
<input type=text name='inv' class=inv value='<?echo $ret[4]?>' ><input type=hidden  name=custcode class='ccode' value='<?echo $ret[10]?>' >

<input type=hidden  readonly name='mcode' class=mcode autocomplete='off' list='htmlListTags' value='<?echo $ret[11]?>' >
<input type=hidden readonly name='config' class='config' value='<?echo $ret[12]?>' >
</td>
</tr>
<tr>
<!-- <td><font color=red>Unit Price </td><td><input type=text name='unit' size=8 onChange=calc()> </td> -->
    
<td>Discount</td><td><input type=text name='disamt' class=disc onChange=calc() value='<?echo $ret[5]?>' ></td>
    <td>Tax %</td><td><input type=text name='taxp' size=5 onChange=calc() value='<?echo $ret[7]?>' >
 <input type=text name='tax' size=10 value='<?echo $ret[6]?>' ></td>
    <td>Amount <br>Before Tax </td><td><input type=text readonly name='amt2' value='<?echo $ret[8]?>' ></td>
</tr>
<tr><td colspan=6 align=center><input type=submit value='SAVE' name='save'> 
    <input type=button value='CANCEL' onClick=document.location='../blank.html'></td>
</tr>
</table>
<script>
function calc()
{
 net=parseFloat(document.f1.disamt.value);
 amt=(net* 100)/(100+parseFloat(document.f1.taxp.value))
 document.f1.amt2.value=parseFloat(amt.toFixed(2));

 tax=parseFloat(document.f1.amt2.value)*parseFloat(document.f1.taxp.value)/100;
 document.f1.tax.value=(net-amt).toFixed(2);
}
</script>





