<? 
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($sprint)
{ 
 $re=mysqli_query($db,"select max(invno) from veh_transout where brcode='$brcode'")or die(mysqli_error());
 $inv_row=mysqli_fetch_row($re);
 if($inv_row[0]<=0)
  $invno=1;
 else 
  $invno=$inv_row[0]+1;

 $invdate=$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);

 $rrs=mysqli_query($db,"select * from veh_transin where msno='$da' and invno=0 and brcode='$brcode'") or die('Veh trans in Err:'.mysqli_error());
  if(mysqli_num_rows($rrs)>0)
    mysqli_query($db,"update veh_transin set invno='$invno' where msno='$da' and invno=0 and brcode='$brcode'");


  $sql="insert into veh_invoice values('$branch1',' ',' ',' ',' ', ' ','$invno', '$invdate','$da','$mcode','$config','$unit',' ','$netamt', '$taxp','$tax', '$amt2','$engno','$segment','$remark','$jvno','$brcode','trans-out','')";
 mysqli_query($db,$sql) or die('INV ERR'.mysqli_error());

 $sql="insert into veh_movement (date,inv_no,qty,type,brcode,sup_type,da)values('$invdate', '$invno','1','trans-out','$brcode','$branch1','$da')";
 mysqli_query($db,$sql)or die(mysqli_error());
$etdate=date("Y-m-d",time());

 $sql="insert into veh_transout values('$branch1','$invno','$invdate','$da','$mcode','$config', '$netamt','$tax','$amt2','$disc','$color','$engno','$chasis','$remark','$brcode','$etdate', '$pno','eich','-','$emcode','$unit','$taxp','$jvno')";
 mysqli_query($db,$sql) or die('TRAN ERR'.mysqli_error());
 
 $re=mysqli_query($db,"select * from vehopstock where msno='$da' and brcode='$brcode'");
 if(mysqli_num_rows($re)>0)
 {
   mysqli_query($db,"update vehopstock set sold='$invno',transtype='trans-out' where msno='$da' and brcode='$brcode'");
 }
 $sql="update veh_purchase set invno='$invno',transtype='trans-out' where msno='$da' and brcode='$brcode'";
   mysqli_query($db,$sql)or die(mysqli_error());

$brresult=mysqli_query($db,"select brcode,brname from branch where brcode='$branch1'");
$br_row=mysqli_fetch_row($brresult);
$brname=$br_row[1];

$sql="insert into journal2 values('$invdate','$jvno','Dr','$brname','$unit', '0','Veh Transfer out: $invno','$brcode','V Trans out','MSNO','$da','$invno')";
mysqli_query($db,$sql) or die("J V Write error 1:".mysqli_error());

$sql="insert into journal2 values('$invdate','$jvno','Cr','Goods Sent on Consig.','0','$unit','Veh Trans out: $invno','$brcode','V Trans out','MSNO','$da','$invno')";
mysqli_query($db,$sql) or die("J V Write error 2:".mysqli_error());
print "<script> window.open('prntransout.php?invno=$invno')</script>";
}

?>
<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 engno=[];
var chasis=[];
var price=[];
var unit=[];
var tax=[];
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='';
	document.f1.engno.value='';
	document.f1.unit.value='';
	document.f1.tax.value='';
	document.f1.amt2.value='';

   }
 else
  { 
    document.f1.mcode.value=ar1[document.f1.da.selectedIndex];
    document.f1.config.value=config[document.f1.da.selectedIndex];
     document.f1.engno.value=engno[document.f1.da.selectedIndex];
     document.f1.netamt.value=price[document.f1.da.selectedIndex];
     document.f1.unit.value=unit[document.f1.da.selectedIndex];
     document.f1.amt2.value=unit[document.f1.da.selectedIndex];
     document.f1.tax.value=tax[document.f1.da.selectedIndex];

  }

}

function valid()
{
 submitOK="true";
 if(document.f1.da.selectedIndex<=0)
 {
   submitOK="false";
   alert('Invalid DA');
 }

 if(document.f1.invno.value.length<=0)
 {
   submitOK="false";
   alert('Invalid Invoice No.');
 }
 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()">

<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>VEHICLE TRANSFER - OUT (No:

<?
$rs=mysqli_query($db,"select max(invno) from veh_transout 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;
print $pno.')';
?>
<input type=hidden name='pno' value='<?print $pno?>'>
</TD></TR>
<tr>
<td>To</td><td><select name='branch1'>
<?
$res=mysqli_query($db,"select brcode,brname from branch  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 $pno?>' size=8 style={background-color:silver}></td>
<td>Tr Date </td><td>dd <input type=text name='d' size=2 value='<?print date("d",time())?>'>
mm <input type=text name='m' size=2 value='<?print date("m",time())?>'>
yy <input type=text name='y' size=4 value='<?print date("Y",time())?>'>
</td></tr>
<tr><td><font color=red>MSNo. </td><td>
<select name='da' onChange=fun1()>
<option></option>
<?
$i=1;
$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> ar1[$i]='$row[model]';\n config[$i]='$row[config]'; \n";
 print " \n engno[$i]='$row[engno]'; \n chasis[$i]='$row[chasis]';";
 print "price[$i]='$row[amt2]';unit[$i]='$row[amt]'; em_code[$i]='$row[model]'; ";
 print "tax[$i]='$row[taxamt]';</script>";
 $i++;
}
$result=mysqli_query($db,"select * from vehopstock where brcode='$brcode' and invno=0")or die(mysqli_error());
while($row=mysqli_fetch_array($result))
{
 print "\n<option>$row[msno]</option>";
 print "\n<script> ar1[$i]='$row[model]';\n config[$i]='$row[config]'; \n";
 print " \nunit[$i]='$row[amt]'; engno[$i]='$row[engno]'; \n chasis[$i]='$row[chno]';";
 print " \nprice[$i]='$row[amt2]'; </script>";
 $i++;
}
?>
</select>
</td>
<td>Model Code </td><td><input type=text readonly name='mcode' size=9></td>
</tr>
<tr><td>config </td><td colspan=5><input type=text name='config' size=93></td></tr>
<tr><td><font color=red>Unit Price </td><td><input type=text name='unit' size=12 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='0'></td>
    <td>Amount <br>Before Tax </td><td><input type=text   name='amt2' ></td>
</tr>
<tr><td>Engine No</td><td><input type=text readonly name='engno' size=12></td>

<td>Remarks </td><td colspan=3><textarea name='remark' cols=45></textarea></td></tr>
<tr><td colspan=6 align=right bgcolor='green'> 

<input type=submit  value=' SAVE AND PRINT BILL ' name='sprint'>
<input type=button value='  CANCEL  ' onClick=document.location='../blank.html'></td></tr>
</table>
</form>
</td></tr>
</table>
</center>
<br>
<script>
function calc()
{
 tax=parseFloat(document.f1.netamt.value)*parseFloat(document.f1.taxp.value)/100;
 document.f1.tax.value=tax.toFixed(2);
 document.f1.amt2.value=(parseFloat(document.f1.netamt.value)+tax).toFixed(2);
}
</script>
<font color=blue>Last 50 transfers:</font>
<table border=1>
<th>To <th>Invno <th>inv Date <th>MSNO <th>M.Code <th>Config<th>Net Amt<th>Action
<?
$re=mysqli_query($db,"select * from veh_transout where brcode='$brcode' order by invno desc limit 50");
while($row=mysqli_fetch_array($re))
{
 print "\n<form action='edit_tr_out.php'> <input type=hidden name='invno' value='$row[invno]'>";
 print "<input type=hidden name=msno value='$row[da]'> ";
 print "\n<tr><td>$row[ledger]</td><td>$row[invno]</td><td>".datetohtml($row[invdate])."</td><td>$row[da]</td>";
 print "<td>$row[mcode]</td><td>$row[config]</td><td>$row[netamt]</td>";
print "<td align=center><input type=submit value='Edit'>";
 print " <input type=button value='Del' onClick=delfun('$row[invno]','$row[da]','$row[invdate]')></td></tr></form>";
}
?>

<script>
 function delfun(a,b,c)
 {
  if(confirm('Do you want to delete invoice '+a+' ?'))
   document.location='deltransout.php?invno='+a+'&da='+b+'&dt='+c;
 }
</script>


