<? 
session_start();
include("../opendb.php");
include("../test.php");
print "<FONT COLOR=RED SIZE=4><B> <BLINK>in complete........</BLINK></FONT>";
if(strlen($brcode)<=0)
{
 print "<script> alert('Session timed out'); parent.document.location='../ind.php'; </script>";
 return;
}
if($sprint)
{
 $invdate=$y.'-'.$m.'-'.$d;
 $sql="update veh_transout set ledger='$branch1',invdate='$invdate',da='$da',mcode='$mcode', config='$config', netamt='$netamt', tax='$tax', amt2='$amt2', disc='$disc', color='$color', engno='$engno', chasis='$chasis',remark= '$remark',emcode='$emcode',unit='$unit', taxp='$taxp' where brcode='$brcode' and invno='$invno'";
 mysqli_query($db,$sql) or die('TRAN ERR'.mysqli_error());

$sql="update veh_movement set da='$da' where da='$oldda' and type='trans-out' and brcode='$brcode'";
mysqli_query($db,$sql) or die('veh mvmt err:'.mysqli_error());

 $sql="update veh_invoice set model_code='$mcode',da='$da',invdate='$invdate',config='$config', unit='$unit',disc='$disc',netamt='$netamt',taxp='$taxp',tax='$tax',amt2= '$amt2', color='$color', engno='$engno', chasis='$chasis',remark= '$remark',emcode='$emcode' where brcode='$brcode' and type='trans-out' and da='oldda'";
 mysqli_query($db,$sql) or die('INV ERR'.mysqli_error());

$sql="update veh_purchase set sale_invoice='-' where da='$oldda' and brcode='$brcode'";
 mysqli_query($db,$sql) or die('Purch ERR'.mysqli_error());

$sql="update veh_purchase set sale_invoice='trans-out' where da='$da' and brcode='$brcode'";
 mysqli_query($db,$sql) or die('Purch ERR2'.mysqli_error());

 
 $re=mysqli_query($db,"select * from vehopstock where dano='$oldda' and brcode='$brcode'");
 if(mysqli_num_rows($re)>0)
 {
   mysqli_query($db,"update vehopstock set sold='N',transtype=' ' where dano='$oldda' and brcode='$brcode'");
   mysqli_query($db,"update vehopstock set sold='$invno',transtype='trans-out' where dano='$da' and brcode='$brcode'");
 }
$brresult=mysqli_query($db,"select code,brname from br_master where code='$branch1' and brcode='$brcode'");
$br_row=mysqli_fetch_row($brresult);
$brname=$br_row[1];


$sql="update journal2 set date='$invdate',partic='$brname',dramt='$netamt',no='$da' where jvno='$jvno' and type='Dr' and brcode='$brcode'";
mysqli_query($db,$sql) or die("J V Write error 1:".mysqli_error());

$sql="update journal2 set date='$invdate',cramt='$netamt',no='$da' where brcode='$brcode' and type='Cr' and jvno='$jvno'";
mysqli_query($db,$sql) or die("J V Write error 2:".mysqli_error());
print "<script>window.open('prnvehtransout.php?invno=$invno')</script>";
print "<script>document.location='veh_transout.php' </script>";
} // end sprint

$rs=mysqli_query($db,"select * from veh_transout where invno='$invno' and brcode='$brcode'");
$tr_row=mysqli_fetch_array($rs);
?>
<style>
body,td{font-family:'arial narrow';letter-spacing:1;}
tr{background-color:c1d7bb}
.mnu{margin-left:5}
</style>
<body bgcolor='ivory'>

<script>
var adr=[];
var ar1=[];
var config=[];
var model=[];
var rep=[];
var area=[];
var color=[];
var engno=[];
var chasis=[];
var price=[];
var em_code=[];
function fun1()
{
 var x=document.f1.da.selectedIndex; 
 
 if(x<=0)
   {
     document.f1.mcode.value='';
     document.f1.config.value='';
     document.f1.netamt.value='';
   }
 else
  { 
    document.f1.mcode.value=ar1[document.f1.da.selectedIndex];
    document.f1.config.value=config[document.f1.da.selectedIndex];
    document.f1.color.value=color[document.f1.da.selectedIndex];
     document.f1.engno.value=engno[document.f1.da.selectedIndex];
     document.f1.chasis.value=chasis[document.f1.da.selectedIndex];
     document.f1.netamt.value=price[document.f1.da.selectedIndex];
     document.f1.unit.value=price[document.f1.da.selectedIndex];
     document.f1.amt2.value=price[document.f1.da.selectedIndex];
     document.f1.emcode.value=em_code[document.f1.da.selectedIndex];

  }

}

function valid()
{
 submitOK="true";
 if(document.f1.unit.value.length<=0)
 {
   submitOK="false";
   alert('Invalid unit price');
 }

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

function chkey(abc)
{
 yy=abc.value
 if(yy.indexOf(' ')>-1) 
  {
    alert('Space is not allowed');
    abc.value='';
  }
}

function setadr()
{
  var x= document.f1.cust.selectedIndex ; 
  if(x>0)
    document.f1.addr.value=adr[document.f1.cust.selectedIndex];
  else
    document.f1.addr.value='';
}
function setModel()
{
  var x= document.f1.formno.selectedIndex ; 
  if(x>0)
    {
     document.f1.model.value=model[document.f1.formno.selectedIndex];
     document.f1.rep.value=rep[document.f1.formno.selectedIndex];
     document.f1.area.value=area[document.f1.formno.selectedIndex];
   }
  else
    {
      document.f1.model.value='';
     document.f1.rep.value='';
     document.f1.area.value='';
   }

}
 

</script>


<style>
body,td{font-family:'arial narrow';letter-spacing:1;font-size:13}
th{background-color:green;color:white}
.mnu{margin-left:5}
</style>
<body bgcolor='ivory'>
<form name='f1' action='<?print $PHP_SELF?>' onSubmit="return valid()">
<input type=hidden name='invno' value='<?print $invno?>'>
<input type=hidden name='jvno' value='<?print $tr_row[jvno]?>'>
<input type=hidden name='oldda' value='<?print $tr_row[da]?>'>

<CENTER>
<table cellspacing=1 cellpadding=1 bgcolor=green>
<tr  bgColor='c1d7bb'><td align=center>
<table border=1 cellspacing=1 cellpadding=2>
<tr bgColor='c1d7bb'><td colspan=7 align=center bgcolor='green'>
<font color='ivory'><STRONG> EDIT VEHICLE TRANSFER - OUT (No:<?print $invno?>)

<input type=hidden name='pno' value='<?print $pno?>'>
</TD></TR>
<tr>
<td>To</td><td><select name='branch1'>
<?
$res=mysqli_query($db,"select code,brname from br_master where brcode='$brcode' and code='$tr_row[ledger]'");
 $br_row=mysqli_fetch_row($res);
?>
<option value='<?print $br_row[0]?>'><? print $br_row[1]?></option>
<?
$res=mysqli_query($db,"select code,brname from br_master where brcode='$brcode' order by brname");
while($row=mysqli_fetch_row($res))
{
 print "<option value='$row[0]'>$row[1]</option>";
}
?>
</select>
</td>
<td>Invoice No </td><td><input type=text readonly name='invno' value='<?print $invno?>' size=8 style={background-color:silver}></td>
<?
$dt=explode('-',$tr_row[invdate]);
?>
<td>Tr Date </td><td>dd <input type=text name='d' size=2 value='<?print $dt[2]?>'>
mm <input type=text name='m' size=2 value='<?print  $dt[1]?>'>
yy <input type=text name='y' size=4 value='<?print  $dt[0]?>'>
</td></tr>
<tr><td><font color=red>DA </td><td>
<select name='da' onChange=fun1()>
<option><? print $tr_row[da]?></option>
<?
$i=0;
 print "\n<script> ar1[$i]='$tr_row[mcode]';\n config[$i]='$tr_row[config]'; \n";
 print "color[$i]='$tr_row[color]'; \n engno[$i]='$tr_row[engno]'; \n chasis[$i]='$tr_row[chasis]';";
 print "price[$i]='$tr_row[amt2]'; em_code[$i]='$tr_row[emcode]'</script>";
$i=1;
$result=mysqli_query($db,"select * from veh_purchase where sale_invoice='-' and brcode='$brcode'")or die(mysqli_error());
while($row=mysqli_fetch_array($result))
{
 print "\n<option>$row[da]</option>";
 print "\n<script> ar1[$i]='$row[mcode]';\n config[$i]='$row[config]'; \n";
 print "color[$i]='$row[color]'; \n engno[$i]='$row[engno]'; \n chasis[$i]='$row[chasis]';";
 print "price[$i]='$row[amt2]'; em_code[$i]='$row[emcode]'</script>";
 $i++;
}
$result=mysqli_query($db,"select * from vehopstock where brcode='$brcode' and sold='N'")or die(mysqli_error());
while($row=mysqli_fetch_array($result))
{
 print "\n<option>$row[dano]</option>";
 print "\n<script> ar1[$i]=$row[mcode];\n config[$i]='$row[config]'; \n";
 print "color[$i]='$row[color]'; \n engno[$i]='$row[engno]'; \n chasis[$i]='$row[chno]';";
 print "price[$i]='$row[pval]'; </script>";
 $i++;
}
?>
</select>
</td>
<td>Model Code </td><td><input type=text readonly name='mcode' size=9 value='<?print $tr_row[mcode]?>'></td>
<td>E.Mcode</td><td><input type=text name='emcode' size=10 value='<?print $tr_row[emcode]?>'></td></tr>
<tr><td>config </td><td colspan=5><input type=text name='config' size=93 value='<?print $tr_row[config]?>'></td></tr>
<tr><td><font color=red>Unit Price </td><td><input type=text name='unit' size=12 value='<?print $tr_row[unit]?>'onChange=calc()> </td>
    <td>Disc </td><td><input type=text name='disc' SIZE=8 value='<?print $tr_row[disc]?>' onChange=calc()></td>
<tr><td>Net Amount</td><td><input type=text name='netamt' value='<?print $tr_row[netamt]?>' onChange=calc()></td>
    <td>Tax %</td><td><input type=text name='taxp' size=5 value='0' value='<?print $tr_row[taxp]?>' onChange=calc()>
 <input type=text name='tax' size=10 value='0'></td>
    <td>Amount <br>Before Tax </td><td><input type=text name='amt2' size=12 value='<?print $tr_row[amt2]?>' ></td>
</tr>
<tr><td>Color</td>
<td><input type=text name='color' value='<?print $tr_row[color]?>'></td><td>Engine No</td><td><input type=text readonly name='engno' size=12 value='<?print $tr_row[engno]?>'></td><td> </td><td></td>
</tr>
<tr><td>Chasis No</td><td><input type=text readonly name='chasis' value='<?print $tr_row[chasis]?>'></td>
<td>Rem </td><td colspan=3><textarea name='remark' cols=45><?print $tr_row[remark]?></textarea></td></tr>
<tr><td colspan=6 align=right bgcolor='green'> 

<input type=submit  value=' UPDATE AND PRINT BILL ' name='sprint'>
<input type=button value='  CANCEL  ' onClick=document.location='veh_transout.php'></td></tr>
</table>
</form>
</td></tr>
</table>
</center>
<br>
<script>
function calc()
{
document.f1.netamt.value=document.f1.unit.value-document.f1.disc.value;
 net=parseFloat(document.f1.netamt.value);
 if(document.f1.taxp.value>0){
 amt=(net* 100)/112.5; 
 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);}
else
 {
  document.f1.amt2.value=net;
 document.f1.tax.value=0;
 }
}
</script>
