
<? 
session_start();
if(strlen($brcode)<=0)
{
 print "<script> alert('Session timed out'); parent.document.location='../ind.php'; </script>";
 return;
}

include("../opendb.php");
include("../test.php");
if($save)
{
  	$rr=mysqli_query($db,"select max(ret_num) from crnote_gst where brcode='$brcode'");
  	$ret_row=mysqli_fetch_row($rr);
  	$retno=$ret_row[0];
  	if($retno<=0)
   		$retno=1819001;
  	else	
   		$retno++;
 	$dt=$y.'-'.$m.'-'.$d;
	//$res=mysqli_query($db,"select code from cust_master where custname='$cust'");
	// $cus_row=mysqli_fetch_row($res);
	$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);
	$sql="insert into crnote_gst values('$retno','$dt','$da','$inv','$mcode','$config', '$disamt','$cust','$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')
	{
		$taxp1 = $taxp/2;
		$tax1  = $tax/2;
		$sql="insert into journal2 values('$dt','$jvno','Dr','Input CGST $taxp1% Credit Note','$tax1', '0','CR Note No(PSNCR$retno)','$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)','$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)','$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)','$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>";
	log_entry($brcode,$user,$REMOTE_ADDR,"Credit Note JVNo: $jvno, DA No: $da, Crno:$retno");
	print "<font color=green><B>Data has been saved</b></font>";
}
?>
<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. 
<?  $rr=mysqli_query($db,"select max(ret_num) from crnote_gst where brcode='$brcode'");
  $ret_row=mysqli_fetch_row($rr);
  $retno=$ret_row[0];
  if($retno<=0)
   $retno=1;
  else
   $retno++;
print $retno;
?>
 )</H3>

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

<table border=1 class=report style=" width: 50%;">
<tr><td>Date </td><td colspan=2>  dd <input type=text size=2 name='d' value='<?print date("d",time())?>'>
			mm <input type=text size=2 name='m' value='<?print date("m",time())?>'>
			yy <input type=text size=4 name='y' value='<?print date("Y",time())?>'>
</td><td colspan=3>&nbsp;</td></tr>
<tr><td>D.A</td><td><input type=text name='da'  size=15 class='cr' autocomplete='off' list='htmlListTags'>
</td><td>Customer </td><td><input type=text readonly name='cust' class=cust></td><td>Invoice </td><td>
<input type=text name='inv' class=inv><input type=hidden  name=custcode class='ccode'>

<input type=hidden  readonly name='mcode' class=mcode autocomplete='off' list='htmlListTags'>
<input type=hidden readonly name='config' class='config'>
</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()></td>
    <td>Tax %</td><td><input type=text name='taxp' size=5 value='0' onChange=calc()>
 <input type=text name='tax' size=10 value='0'></td>
    <td>Amount <br>Before Tax </td><td><input type=text readonly name='amt2'></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>
<br>&nbsp;
<?
$sql="select a.custname,b.ret_num,b.date, b.da, b.sale_invoice,b.netamt,b.tax,b.taxp,b.amt2,b.jvno from cust_master a,crnote_gst b where b.brcode='$brcode' and b.retfrom=a.code order by b.ret_num desc ";
   $result=mysqli_query($db,$sql) or die(mysqli_error());
   if(mysqli_num_rows($result)>0)
   {
?>
Showing All records
<table bgcolor=silver cellspacing=1 cellpadding=3 width=100% class=report>
<th>Cr.No<th>Customer <th>Date <th>DA <th>Sale Invoice <th>Net Amount <th>Tax <th>Tax% <th>Before Tax <th>Del
<?
$tnet=0;
$ttax=0;
$bt=0;
  while($row=mysqli_fetch_array($result))
 {
 print "<tr bgcolor=white><td>$row[1]</td><td>$row[0]</td><td>".datetohtml($row[2])."</td><td align=center>$row[3]</td><td>$row[4]</td>";
print "<td align=right>$row[5]</td><td align=right>$row[6]</td><td align=right>$row[7]</td><td align=right>$row[8]</td>";
//print "<td align=center><input type=button value='Edit' onClick=javascript:edit('$row[1]','$row[9]')></td>";
print "<td align=center><input type=button value='del' onClick=javascript:del('$row[1]','$row[9]')></td></tr>";
 $bt+=$row[8];
 $ttax+=$row[6];
 $tnet+=$row[5];
 }
 print "<tr bgcolor=white><td colspan=5 align=center><b>Total</td><td align=right><b>".putComa($tnet)."</td>";
 print "<td align=right><b>".putComa($ttax)."</td><td>&nbsp;</td><td align=right><b>".putComa($bt)."</td></tr>";

 print "</table>";
}

?>

<script>
function del(a,b)
{
 if(confirm('Do you want to delete Cr No.'+a))
   document.location='delcrnote_gst.php?crno='+a+'&jvno='+b;
}
function edit(a,b)
{
   document.location='editcrnote_gst.php?crno='+a+'&jvno='+b;
}

</script>
