<? 
session_start(); //print "to be updated.... opstock ";
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)
{
 if(strlen($ledger)<=0)
 {
   print "<script> alert('Invalid Supplier'); history.back(1); </script>";
   return;
 }
if($taxp!='12.5')
{
 print "<script> alert('Invalid ledger'); </script>";
 return;
}
  $rr=mysqli_query($db,"select max(ret_num) from veh_purchreturn where brcode='$brcode'");
  $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");
 $j_row=mysqli_fetch_row($jvr);
  if($j_row[0]<=0)
   $jvno=1;
  else
  $jvno=($j_row[0]+1);

$sql="insert into veh_purchreturn values('$retno','$dt','$msno','$pinv','$mcode','$config', '$netamt','$ledger','$brcode','$tax','$taxp','$amt2','$jvno')";
 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','pur_ret','$brcode','$ledger','$msno')";
 mysqli_query($db,$sql) or die(mysqli_error());

 $sql="update veh_purchase set transtype='pur-ret',invno='$pinv' where msno='$msno' and pinvno='$pinv' and brcode='$brcode'";
 mysqli_query($db,$sql) or die(mysqli_error());

 $sql="update vehopstock set transtype='pur-ret', invno='$pinv' where msno='$msno'";
 mysqli_query($db,$sql) or die(mysqli_error());




$sql="insert into journal2 values('$dt','$jvno','Dr','$ledger','$netamt', '0','Purchase Return: $invno','$brcode','P Ret','MSNO','$msno','$invno')";
mysqli_query($db,$sql) or die("J V Write error 1:".mysqli_error());
if($taxp=='12.5')
{
$sql="insert into journal2 values('$dt','$jvno','Cr','Input tax 12.5 (Pur Ret)', '0','$tax','Purchase Return: $invno','$brcode','P Ret','MSNO','$msno','$invno')";
mysqli_query($db,$sql) or die("J V Write error 2:".mysqli_error());

$cess=round(($tax*1)/100,2);
$sql="insert into journal2 values('$dt','$jvno','Cr','Input Cess (Pur Ret)', '0','$cess','Purchase Return: $invno','$brcode','P Ret','MSNO','$msno','$invno')";
mysqli_query($db,$sql) or die("J V Write error 2:".mysqli_error());

 $sql="insert into journal2 values('$dt','$jvno','Cr','Purchase ret 12.5(vehicle)','0','$amt2','Purchase Return: $invno','$brcode','P Ret','MSNO','$msno','$invno')";
mysqli_query($db,$sql) or die("J V Write error 3:".mysqli_error());
}


 print "<font color=green><B>Data has been saved</b></font>";
}
?>
<script>
var pinv=[];
var mcode=[];
var config=[];
var netamt=[];
var ledger=[];
var tax=[];
var disc=[];
var amt2=[];
var sup=[];
function fun1()
{
 var x=document.f1.msno.selectedIndex; 
 
 if(x<=0)
   {
     document.f1.pinv.value='';
    document.f1.mcode.value='';
    document.f1.config.value='';
    document.f1.netamt.value='';
    document.f1.ledger.value='';
   }
 else
  { 
    document.f1.pinv.value=pinv[document.f1.msno.selectedIndex];
    document.f1.mcode.value=mcode[document.f1.msno.selectedIndex];
    document.f1.config.value=config[document.f1.msno.selectedIndex];
    document.f1.netamt.value=netamt[document.f1.msno.selectedIndex]; 
    document.f1.ledger.value=ledger[document.f1.msno.selectedIndex];
    document.f1.tax.value=tax[document.f1.msno.selectedIndex];
    //document.f1.disc.value=disc[document.f1.msno.selectedIndex];
    document.f1.amt2.value=amt2[document.f1.msno.selectedIndex];
//    document.f1.ledger.value=sup[document.f1.msno.selectedIndex];
    document.f1.ledger.value=ledger[document.f1.msno.selectedIndex];

  }
}
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);
}

function valid()
{
 submitOK="true";
 if(document.f1.msno.selectedIndex<=0)
 {
   submitOK="false";
   document.f1.msno.focus();
   alert('Invalid MSNo.');
 }
 if(isNaN(document.f1.netamt.value))
 {
   submitOK="false";
  document.f1.netamt.focus();
   alert('Invalid Net Amount');
 }
 if(isNaN(document.f1.amt2.value))
 {
   submitOK="false";
  document.f1.amt2.focus();
   alert('Invalid Before Tax');
 }

  if(submitOK=="true")
   return true;
  else
   return false;
}

</script>
<link rel="stylesheet" href="../style.css" type="text/css" />
<style>
tr{background-color:white}
th{background-color:steelblue;color:white}
</style>
<body class=bd onSubmit="return valid()">
<center>

<?  $rr=mysqli_query($db,"select max(ret_num) from veh_purchreturn where brcode='$brcode'");
  $ret_row=mysqli_fetch_row($rr);
  $retno=$ret_row[0];
  if($retno<=0)
   $retno=1;
  else
   $retno++;

?>

<form name='f1' action='<?print $PHP_SELF?>'>
<table border=0 cellspacing=1 cellpadding=3 bgcolor='steelblue'>
<th colspan=6><h3>PURCHASE RETURN (No. <?print $retno?>)</th>
<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> MSNO </td><td><select name='msno' onChange=fun1()>
<option></option>
<?
$i=1;
$re=mysqli_query($db,"select b.supname,a.pinvno,a.msno,a.model,a.config,a.amt2 from vehopstock a, sup_master b where a.invno='0' and a.brcode='$brcode' group by msno")or die('OP Err'.mysqli_error());
 while($oprow=mysqli_fetch_array($re))
 {
 print "\n<option>$oprow[msno]</option>";
 print "\n <script> pinv[$i]='$oprow[pinv]'; </script>";
 print "\n <script> mcode[$i]='$oprow[mcode]'; </script>";
 print "\n <script> config[$i]='$oprow[config]'; </script>";
 print "\n <script> netamt[$i]='$oprow[amt2]'; </script>";
 print "\n <script> sup[$i]='$oprow[supname]'; </script>";

 $i++;
} 
/*
$result=mysqli_query($db,"select * from vehopstock where invno=0 and brcode='$brcode'")or die(mysqli_error());
while($row=mysqli_fetch_array($result))
{
 print "\n<option>$row[msno]</option>";
 print "\n <script> pinv[$i]='$row[pinv]'; </script>";
 print "\n <script> mcode[$i]='$row[mcode]'; </script>";
 print "\n <script> config[$i]='$row[config]'; </script>";
 print "\n <script> netamt[$i]='$row[pval]'; </script>";
$i++;
}
*/
$result=mysqli_query($db,"select * from veh_purchase where (invno=0) and brcode='$brcode'")or die(mysqli_error());
while($row=mysqli_fetch_array($result))
{
 print "\n<option>$row[msno]</option>";
 print "\n <script> ledger[$i]='$row[sup]'; </script>";
 print "\n <script> pinv[$i]='$row[pinvno]'; </script>";
 print "\n <script> mcode[$i]='$row[model]'; </script>";
 print "\n <script> config[$i]='$row[config]'; </script>";
 print "\n <script> netamt[$i]='$row[amt2]'; </script>";
 print "\n <script> tax[$i]='$row[taxamt]'; </script>";
 print "\n <script> amt2[$i]='$row[amt]'; </script>";
 print "\n <script> disc[$i]='$row[disc]'; </script>";



if($row[transtype]=='trans-in')
{
  $led=mysqli_query($db,"select brname from branch where brcode='$row[ledger]'");
   $led_row=mysqli_fetch_row($led);
  print "\n <script> ledger[$i]='$led_row[0]'; </script>";
}
 else
//  print "\n <script> ledger[$i]='$row[ledger]'; </script>";

 $i++;
}
?>
</select></td>
<td>Pur Invoice</td><td><input type=text name='pinv'></td>
<td>Model Code</td><td><input type=text name='mcode' readonly ></td>
</tr>
<tr><td>Config</td><td colspan=3><input type=text name='config' size=40></td>
<tr><td><font color='red'>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' onChange=calc()></td>
    <td>Amount <br>Before Tax </td><td><input type=text readonly name='amt2' ></td>
</tr>
<tr><td>Return to </td><td colspan=3>
<input type=text name='ledger' size=30>
 
</td>
<td align=center colspan=2><input type=submit value=' SAVE ' name='save'> <input type=button value=' CANCEL ' onClick=document.location='../blank.html'></td></tr>
</table>
</form>
<br>
<style>
.x{font-size:13}
</style>

<table border=1>
<th>Ret.No<th>Date <th>DA <th>Pur_Inv <th>M code<th>Config <th>Net Amt.<th>Ret. To 
<th>Tax <th>Tax % <th>Amount <th>Del
<?
$res=mysqli_query($db,"select * from veh_purchreturn where brcode='$brcode'");
while($row=mysqli_fetch_array($res))
{
 print "<tr class=x><td><center>$row[0]</td><td>".datetohtml($row[1])."</td><td>$row[2]</td><td>$row[3]</td><td>$row[4]</td>";
 print "<td>$row[5]</td><td align=right>$row[6]</td><td>$row[7]</td><td align=right>$row[9]</td><td align=right>$row[10]</td>";
 print "<td align=right>$row[11]</td>";
 print "<td><input type=button value='Del' onClick=delpur('$row[da]')></td></tr>";
}
?>
</table>

<script>
 function delpur(x)
 {
  if(confirm('Do you want to delete da '+x+ ' ?'))
   {
     document.location='delpurreturn.php?da='+x;
   }
 }
</script>

