<? 
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)
{
$dt=$y.'-'.$m.'-'.$d;
$sql="update crnote set date='$dt',netamt='$disamt',tax='$tax',taxp='$taxp',amt2='$amt2' where ret_num='$crno' and brcode='$brcode'";

mysqli_query($db,$sql)or die('Err1'.mysqli_error());
 
$sql="delete from journal2 where jvno='$jvno' and brcode='$brcode'";
mysqli_query($db,$sql);

if($taxp=='12.5')
{
$sql="insert into journal2 values('$dt','$jvno','Dr','Output tax on credit notes 12.5','$tax', '0','CR Note No($crno)','$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($crno)','$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','0','$disamt', 'CR Note No($retno)','$brcode','CR Note','DA','$da','$crno')";
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: $crno','$brcode','Dis All','DA','$da','$crno')";
mysqli_query($db,$sql) or die("J V Write error 3:".mysqli_error());

$sql="insert into journal2 values('$dt','$jvno','Cr','$cust','0','$disamt', 'Dis All No: $crno','$brcode','Dis All','DA','$da','$crno')";
mysqli_query($db,$sql) or die("J V Write error 1:".mysqli_error());
}
print "<script> alert('Data has been changed'); document.location='crnote.php' </script>";
return;
}
$crres=mysqli_query($db,"select * from crnote where ret_num='$crno' and brcode='$brcode'")or die('Err :'.mysqli_error());
$crrow=mysqli_fetch_array($crres);
$crdt=explode('-',$crrow[date]);
?>

<script>
var inv=[];
var mcode=[];
var config=[];
var netamt=[];
var cust=[];
var unit=[];
var disc=[];
var tax=[];
var taxp=[];
var amt2=[];
var ccode=[];
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);
}


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>EDIT CREDIT NOTE <BR>DISCOUNT ALLOWED (VEHICLE) (No. 
<?  
print $crno;
?>
 )</H3>

<form name='f1' action='<?print $PHP_SELF?>' onSubmit="return valid()">
<input type=hidden name=crno value='<?print $crno?>'>
<input type=hidden name='jvno' value='<?print $crrow[jvno]?>'>
<table border=1>
<tr><td>Date </td><td colspan=2>  dd <input type=text  size=2 name='d' value='<?print $crdt[2]?>'>
			mm <input type=text size=2   name='m' value='<?print $crdt[1]?>'>
			yy <input type=text size=4   name='y' value='<?print $crdt[0]?>'>
</td><td colspan=3>&nbsp;</td></tr>
<tr><td>D.A</td><td><select name='da' onChange=fun1()>
<option><?print $crrow[da]?></option>
</select>
</td><td>Customer </td><td>
<?$cusre=mysqli_query($db,"select acname from cust_master where code='$crrow[retfrom]'");
  $cusrow=mysqli_fetch_array($cusre);
?>
<input type=text readonly name='cust' size=40 value='<?print $cusrow[0]?>'>
</td><td>Invoice </td><td>
<input type=text name='inv' readonly size=7 value='<?print $crrow[sale_invoice]?>'></td></tr>

<tr><td>Model Code </td><td><input type=text readonly name='mcode' value='<?print $crrow[mcode]?>'></td>
    <td>Config </td><td><input type=text readonly name='config' value='<?print $crrow[config]?>'></td>

</tr>
<tr>
<!-- <td><font color=red>Unit Price </td><td><input type=text name='unit' size=8 onChange=calc() value='<?print $crrow[netamt]?>'> </td> -->
    
<tr><td>Discount</td><td><input type=text name='disamt' onChange=calc() value='<?print $crrow[netamt]?>'></td>
    <td>Tax %</td><td><input type=text name='taxp' size=5 value='<?print $crrow[taxp]?>' onChange=calc()>
 <input type=text name='tax' size=10 value='<?print $crrow[tax]?>'></td>
    <td>Amount <br>Before Tax </td><td><input type=text readonly name='amt2' value='<?print $crrow[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>
</form>
