<? 
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 da='$da' and sale_invoice='-' and brcode='$brcode'") or die('Veh trans in Err:'.mysqli_error());
  if(mysqli_num_rows($rrs)>0)
    mysqli_query($db,"update veh_transin set sale_invoice='$invno' where da='$da' and sale_invoice='-' and brcode='$brcode'");


  $sql="insert into veh_invoice values('$branch1','$formno','$model','$rep','$area', '$fin','$invno', '$invdate','$da','$config','$unit','$disc','$netamt', '$taxp','$tax', '$amt2','$color','$engno','$segment','$chasis','$remark','$brcode','trans-out','$jvno','$emcode','','','','')";
 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','eich','$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 dano='$da' and brcode='$brcode'");
 if(mysqli_num_rows($re)>0)
 {
   mysqli_query($db,"update vehopstock set sold='$invno',transtype='trans-out' where dano='$da' and brcode='$brcode'");
 }
 $sql="update veh_purchase set sale_invoice='$invno',type='trans-out' where da='$da' and brcode='$brcode'";
   mysqli_query($db,$sql)or die(mysqli_error());

$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="insert into journal2 values('$invdate','$jvno','Dr','$brname','$netamt', '0','Veh Transfer out: $invno','$brcode','V Trans out','DA','$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','$netamt','Veh Trans out: $invno','$brcode','V Trans out','DA','$da','$invno')";
mysqli_query($db,$sql) or die("J V Write error 2:".mysqli_error());

log_entry($brcode,$user,$REMOTE_ADDR,"Veh trans out entry-$da");

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 config2=[];
var model=[];
var rep=[];
var area=[];
var color=[];
var engno=[];
var chasis=[];
var em_code=[];
function fun1()
{
 var x=document.f1.da.selectedIndex; 
 
 if(x<=0)
   {
     document.f1.mcode.value='';
     document.f1.config.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.emcode.value=em_code[document.f1.da.selectedIndex];
  }

/*  if(document.f1.model.value != document.f1.mcode.value)
   {
       alert('Model code doesn\'t match with order form');
       document.f1.mcode.value='';
       document.f1.config.value='';
   }
*/
}

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 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 $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>DA </td><td>
<select name='da' onChange=fun1()>
<option></option>
<?
$i=1;
$result=mysqli_query($db,"select * from veh_purchase where sale_invoice='-' and  brcode='$brcode' order by da")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 "em_code[$i]='$row[emcode]'; </script>";
 $i++;
}
$result=mysqli_query($db,"select * from vehopstock where brcode='$brcode' and sold='-' order by dano")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]'; </script>";
 $i++;
}
?>
</select>
</td>
<td>Model Code </td><td><input type=text readonly name='mcode' size=9></td>
<td>E.Mcode</td><td><input type=text name='emcode' size=10></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>
    <td>Discount </td><td><input type=text name='disc' 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='0' 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>Color</td>
<td><input type=text name='color'></td><td>Engine No</td><td><input type=text readonly name='engno' size=12></td><td> </td><td></td>
</tr>
<tr><td>Chasis No</td><td><input type=text readonly name='chasis'></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()
{
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>
<font color=blue>Last 50 transfers:</font>
<table border=1>
<th>To <th>Invno <th>inv Date <th>DA <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=da 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>


