
<? 
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 where brcode='$brcode'");
  $ret_row=mysqli_fetch_row($rr);
  $retno=$ret_row[0];
  if($retno<=0)
   $retno=1;
  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 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=='14.5'|| $taxp=='13.5')
{
$sql="insert into journal2 values('$dt','$jvno','Dr','Output tax on credit notes $taxp','$tax', '0','CR Note No($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)','$amt2','0','CR Note No($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($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.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>";

}
?>

<script>
var inv=[];
var mcode=[];
var config=[];
var netamt=[];
var cust=[];
var unit=[];
var disc=[];
var tax=[];
var taxp=[];
var amt2=[];
var ccode=[];
function fun1()
{
 var x=document.f1.da.selectedIndex; 

 if(x<=0)
   {
     document.f1.inv.value='';
    document.f1.mcode.value='';
    document.f1.config.value='';
    document.f1.disamt.value='';
    document.f1.cust.value='';
    document.f1.custcode.value='';
   }
 else
  { 
    document.f1.inv.value=inv[document.f1.da.selectedIndex];
    document.f1.mcode.value=mcode[document.f1.da.selectedIndex];
    document.f1.config.value=config[document.f1.da.selectedIndex];
    document.f1.cust.value=cust[document.f1.da.selectedIndex];
    document.f1.custcode.value=ccode[document.f1.da.selectedIndex];
  }
}

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 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()">
<input type=hidden name=custcode>
<table border=1>
<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><select name='da' onChange=fun1()>
<option></option>
<?
 $res=mysqli_query($db,"select a.custname,a.code,b.invno,b.da,b.model_code,b.config,b.netamt,b.unit, b.disc, b.taxp, b.tax, b.amt2 from cust_master a,veh_invoice b where b.brcode='$brcode' and b.type='-' and a.code=b.cust order by b.da")or die(mysqli_error());
 $i=1;
 while($row=mysqli_fetch_array($res))
 {
   print "\n<option>$row[da]</option>";
   print " \n<script> cust[$i]='$row[custname]'</script>";
   print " \n<script> ccode[$i]='$row[1]'</script>";
   print " \n<script> inv[$i]='$row[invno]'</script>";
   print " \n<script> mcode[$i]='$row[model_code]'</script>";
   print " \n<script> config[$i]='$row[config]'</script>";
   print " \n<script> netamt[$i]='$row[netamt]'</script>";
   print " \n<script> unit[$i]='$row[unit]'</script>";
   print " \n<script> disc[$i]='$row[disc]'</script>";
   print " \n<script> taxp[$i]='$row[taxp]'</script>";
   print " \n<script> tax[$i]='$row[tax]'</script>";
   print " \n<script> amt2[$i]='$row[amt2]'</script>";
  $i++;
 }
?>
</select>
</td><td>Customer </td><td><input type=text readonly name='cust'></td><td>Invoice </td><td>
<input type=text name='inv'></td></tr>

<tr><td>Model Code </td><td><input type=text readonly name='mcode'></td>
    <td>Config </td><td><input type=text readonly name='config'></td>

</tr>
<tr>
<!-- <td><font color=red>Unit Price </td><td><input type=text name='unit' size=8 onChange=calc()> </td> -->
    
<tr><td>Discount</td><td><input type=text name='disamt' 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 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%>
<th>Cr.No<th>Customer <th>Date <th>DA <th>Sale Invoice <th>Net Amount <th>Tax <th>Tax% <th>Before Tax <th>Edit<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><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.php?crno='+a+'&jvno='+b;
}
function edit(a,b)
{
   document.location='editcrnote.php?crno='+a+'&jvno='+b;
}

</script>
