<? 
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 veh_salereturn where brcode='$brcode' and vatorcst='$taxtype'");
  $ret_row=mysqli_fetch_row($rr);
  $retno=$ret_row[0];
  if($retno<=0)
   $retno=1;
  else
   $retno++;
 $dt=$y.'-'.$m.'-'.$d;


$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);
//print $cust;
$res=mysqli_query($db,"select code from cust_master where acname='$cust' and branch='$brcode'");
 $cus_row=mysqli_fetch_row($res)or die(mysqli_error());
$cust=$cus_row[0]; 
$sql="insert into veh_salereturn values('$retno','$dt','$da','$inv','$mcode','$config', '$netamt','$cust','$brcode','$tax','$taxp','$amt2','$jvno','$taxtype','$cessp','$cessamt','regular')";
 mysqli_query($db,$sql) or die(mysqli_error());

 $sql="insert into veh_movement(date,inv_no,qty,type,brcode,sup_type,da) values('$dt','$retno','1','sale_retold','$brcode','$cust','$da')";


//print $sql.'<br>';

mysqli_query($db,$sql) or die(mysqli_error());

 $sql="insert into veh_purchase (select * from psndb1314.veh_purchase where da='$da' and brcode='$brcode')";
 mysqli_query($db,$sql) or die('PURCH ERR:'.mysqli_error());

$rs=mysqli_query($db,"select max(pur_no) from veh_purchase where brcode='$brcode'")or die(mysqli_error());
 $p_row=mysqli_fetch_row($rs);
 if(mysqli_num_rows($rs)<=0)
  $pno=1;
 else
    $pno=$p_row[0]+1;

$sql="update veh_purchase set sale_invoice='-',type='sale-ret',oldornew='old',pur_no='$pno' where da='$da'  and brcode='$brcode'";
mysqli_query($db,$sql) or die(mysqli_error());

$sq="select code,custname,acname from cust_master where code='$cust' and branch='$brcode'";

$kk=mysqli_query($db,$sq) or die('Customer error:'.mysqli_error());
$rcus=mysqli_fetch_row($kk);
$cust_name=$rcus[2];

if($taxtype=='VAT')
{
$sql="insert into journal2 values('$dt','$jvno','Dr','Output tax $taxp (Sale Ret)','$tax', '0','V Sales Ret INV No: $retno','$brcode','Sale ret','DA','$da','$retno')";
mysqli_query($db,$sql) or die("J V Write error 2:".mysqli_error());

 $sql="insert into journal2 values('$dt','$jvno','Dr','Output Cess','$cessamt','0','V Sales Ret INV No: $retno','$brcode','Sale ret','DA','$da','$retno')";
mysqli_query($db,$sql) or die("J V Write error 3:".mysqli_error());

 $sql="insert into journal2 values('$dt','$jvno','Dr','Sales ret $taxp(vehicle)','$amt2','0','V Sales Ret INV No: $retno','$brcode','Sale ret','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','$netamt', 'V Sales Ret INV No: $retno','$brcode','sale ret','DA','$da','$retno')";
//print $sql;
mysqli_query($db,$sql) or die("J V Write error 1:".mysqli_error());


}

if($taxtype=='CST')
{
$sql="insert into journal2 values('$dt','$jvno','Dr','CST $taxp (Sale Ret)','$tax', '0','V Sales Ret INV No: $retno','$brcode','Sale ret CST','DA','$da','$retno')";
mysqli_query($db,$sql) or die("J V Write error 2:".mysqli_error());

$sql="insert into journal2 values('$dt','$jvno','Dr','VEHICLE CST SALES RETURN $taxp','$amt2','0','V Sales Ret INV No: $retno','$brcode','Sale ret CST','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','$netamt', 'V Sales Ret INV No: $retno','$brcode','Sale ret CST','DA','$da','$retno')";
mysqli_query($db,$sql) or die("J V Write error 1:".mysqli_error());
}

$jvd=mysqli_query($db,"select sum(dramt)-sum(cramt) from journal2 where jvno='$jvno' and brcode='$brcode'");
$difrow=mysqli_fetch_array($jvd);
$dif=$difrow[0];
$x=abs($dif);
if($dif<0)
{
$sql="insert into journal2 values('$invdate','$jvno','Dr','Round Off','$x', '0','Round ofSRET No: $invno','$brcode','V sale','DA','$da','$invno')";
mysqli_query($db,$sql) or die("J V Write error dif1:".mysqli_error());
}
else if($dif>0)
{
 $sql="insert into journal2 values('$invdate','$jvno','Cr','Round Off','0','$x','Round of adjusted SRET No: $invno','$brcode','V sale','DA','$da','$invno')";
mysqli_query($db,$sql) or die("J V Write error dif:".mysqli_error());
}


 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 cessp=[];
var cessamt=[];


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.netamt.value='';
    document.f1.cust.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.netamt.value=netamt[document.f1.da.selectedIndex];
    document.f1.cust.value=cust[document.f1.da.selectedIndex];
    //document.f1.unit.value=unit[document.f1.da.selectedIndex];
    //document.f1.disc.value=disc[document.f1.da.selectedIndex];
    document.f1.tax.value=tax[document.f1.da.selectedIndex];
    document.f1.taxp.value=taxp[document.f1.da.selectedIndex];
    document.f1.amt2.value=amt2[document.f1.da.selectedIndex];
	document.f1.cessp.value=cessp[document.f1.da.selectedIndex];
    document.f1.cessamt.value=cessamt[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.netamt.value))
 {
   submitOK="false";
  document.f1.netamt.focus();
   alert('Invalid Net Amount');
 }
  if(submitOK=="true")
   return true;
  else
   return false;
}

function setttype()
{
 if(document.f1.taxtype.selectedIndex==1)
  {
    document.f1.taxp.value=2;
    document.f1.cessp.value=0;
    document.f1.cessamt.value=0;

 }
 else
 {
  document.f1.cessp.value=1;
    document.f1.taxp.value=12.5;
 }

 calc();
}
</script>
<link rel="stylesheet" href="../style.css" type="text/css" />
<body class=bd onSubmit="return valid()">
<CENTER>
<h3>VEHICLE SALES RETURN (No. 
<?  $rr=mysqli_query($db,"select max(ret_num) from veh_salereturn 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?>'>
<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>&nbsp;</td><td>Tax Type</td><td><select name='taxtype' onChange='setttype()'><option>VAT</option><option>CST</option></select>
</td></tr>
<tr><td>D.A</td><td>
<?
$sql="select a.acname,a.code,b.invno,b.da,b.model_code,b.config,b.netamt,b.unit, b.disc, b.taxp, b.tax, b.amt2,b.cessp,b.cessamt from psndb1314.cust_master a,psndb1314.veh_invoice b where b.brcode='$brcode' and b.type='-' and a.code=b.cust order by b.da";
//print $sql;
?>
<select name='da' onChange=fun1()>
<option></option>
<?
 $res=mysqli_query($db,$sql)or die(mysqli_error());
 $i=1;
 while($row=mysqli_fetch_array($res))
 {
   print "\n<option>$row[da]</option>";
   print " \n<script> cust[$i]='$row[acname]'</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>";
   print " \n<script> cessp[$i]='$row[cessp]'</script>";
   print " \n<script> cessamt[$i]='$row[cessamt]'</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>Net Amount</td><td><input type=text name='netamt' onChange=calc()></td>
    <td>Tax %</td><td><input type=text name='taxp' size=5 value='12.5' onChange=calc()>
 <input type=text name='tax' size=10 value='12.5'></td></tr>
 <tr><td>Cess%</td><td><input type=text name='cessp' value='1' size=4></td><td>Cess Amt </td><td><input type=text name='cessamt' size=5></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.netamt.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>
<style>
.x{background-color:e4ecc4}
</style>
<?
$res=mysqli_query($db," select a.ret_num,a.date,a.da,a.sale_invoice, a.mcode,a.config,a.netamt,b.custname,a.tax,a.taxp,a.amt2,b.acname from veh_salereturn a,cust_master b where a.retfrom=b.code and a.brcode='$brcode' order by ret_num,a.date desc")or die(mysqli_error());
print "<table border=1> <th>Ret <br>No. <th>Date <th>DA <th>Sale <br>Invoice <th>MCode <th>Config";
print "<th>Net Amt <th>Customer <th>Tax <th>Tax% <th>Amount <br>Before Tax";
while($row=mysqli_fetch_array($res))
{
 print "<tr class=x><td>$row[0]</td><td>$row[1]</td><td>$row[2]</td><td>$row[3]</td><td>$row[4]</td>";
 print "<td>$row[5]</td><td>$row[6]</td><td>$row[11]</td><td>$row[8]</td><td>$row[9]</td>";
 print "<td>$row[10]</td></tr>";
}
?>
</table>

