<? 
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($submit)
{
$entrydate=$yy.'-'.$mm.'-'.$dd;
$invdate=$y.'-'.$m.'-'.$d;
if(!checkdate($m,$d,$y))
{
 print "<script>alert('Invalid Date'); history.back(1);</script>";
 return;
}

if(!checkdate($mm,$dd,$yy))
{
 print "<script>alert('Invalid Date'); history.back(1);</script>";
 return;
}
$ch=mysqli_query($db,"select * from veh_transin where brcode='$brcode' and (invno='$invno' or da='$da')");

 if(mysqli_num_rows($ch)>0)
 {
  print "<script>alert('Duplicate Entry in Transfer IN'); history.back(1);</script>";
  return; 
 }

$rs=mysqli_query($db,"select max(trans_no) from veh_transin 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;

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

$exist=0;
$rrr=mysqli_query($db,"select * from veh_purchase where da='$da' and sale_invoice='-' and brcode='$brcode'");
if(mysqli_num_rows($rrr)>0)
{
  print "<script>alert('Duplicate DA Entry - Purchase'); history.back(1);</script>";
 $exist=1;
  return; 
 }

$sql="insert into veh_transin values('$ledger','$invno','$invdate','$da','$mcode','$config', '$netamt','$tax','$amt2','$disc','$color','$engno','$chasis','$remark','$brcode','$entrydate', '$pno','eich','-','$jvno','$emcode')";
 mysqli_query($db,$sql) or die('TRANS IN ERR:'.mysqli_error());
if($exist==1)
 $sql="update veh_purchase set type='trans-in',sale_invoice='-'";
else
$sql="insert into veh_purchase values('$ledger','$invno','$invdate','$da','$mcode','$config', '$netamt','$tax','$amt2','$disc','$color','$engno','$chasis','$remark','$brcode',' $entrydate','$pno','trans-in','-','$loc','$taxp','$emcode','$jvno','$config','-','','','')";
 mysqli_query($db,$sql) or die('PURCH ERR:'.mysqli_error());

/*
 $sq="update purorder set purno='$pno',purdate='$invdate' where dano='$da' and brcode='$brcode'";
 mysqli_query($db,$sq) or die(mysqli_error());
*/

 $code=substr($engno,0,3).substr(time(),7,3); 
 $code=str_replace(' ','-',$code);

$sql="insert into veh_movement (date,inv_no,qty,type,brcode,sup_type,da) values('$entrydate','$invno','1','trans_in','$brcode','eich','da')";
mysqli_query($db,$sql) or die('Err Movement:-'.mysqli_error());


$brresult=mysqli_query($db,"select code,brname from br_master where code='$ledger' and brcode='$brcode'");
$br_row=mysqli_fetch_row($brresult);
$brname=$br_row[1];

$sql="insert into journal2 values('$entrydate','$jvno','Dr','Goods recvd on Consig.','$netamt', '0','Veh Transfer in: $invno','$brcode','V Trans in','DA','$da','$invno')";
mysqli_query($db,$sql) or die("J V Write error 1:".mysqli_error());

$sql="insert into journal2 values('$entrydate','$jvno','Cr','$brname','0','$netamt','Veh Transfer in: $invno','$brcode','V Trans in','DA','$da','$invno')";
mysqli_query($db,$sql) or die("J V Write error 3:".mysqli_error());

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

 print "<font color=green><b>Data has been saved</b></font>";
}
?>

<script>
var ar1=[];
var config=[];
function fun1()
{
 var x=document.f1.config.selectedIndex; 
 
 if(x<=0)
   {
     document.f1.mcode.value='';
     document.f1.config.value='';
   }
 else
  {
    document.f1.mcode.value=config[document.f1.config.selectedIndex];
  }
}

function valid()
{
 submitOK="true";
 if(document.f1.invno.value.length<=0)
 {
   submitOK="false";
   alert('Invalid Invoice No.');
 }
 if(document.f1.da.value.length<=0)
 {
   submitOK="false";
   alert('Invalid DA');
 }
 if(document.f1.loc.value.length<=0)
 {
   submitOK="false";
   alert('Invalid Location');
 }

 if(document.f1.engno.value.length<=0)
 {
   submitOK="false";
   alert('Invalid Engine No.');
 }
 if(document.f1.chasis.value.length<=0)
 {
   submitOK="false";
   alert('Invalid Chasis');
 }

 if(document.f1.netamt.value.length<=0 || isNaN(document.f1.netamt.value))
 {
   submitOK="false";
   alert('Invalid Net Amount');
 }

  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='';
  }
}
</script>


<style>
body,td{font-family:'arial narrow';letter-spacing:1;}
th{background-color:green;color:white}
.mnu{margin-left:5}
</style>
<body bgcolor='ivory'>

<CENTER>
<form name='f1' action='<?print $PHP_SELF?>' onSubmit="return valid()">

<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 IN (No:

<?
$rs=mysqli_query($db,"select max(trans_no) from veh_transin 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>From </td><td colspan=2><select name='ledger'>
<?
$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 colspan=1 align=right>Current Date  </td><td colspan=2>
	dd <input type=text name='dd' size=2 value='<?print date("d",time())?>'>
	mm <input type=text name='mm' size=2 value='<?print date("m",time())?>'>
	yy <input type=text name='yy' size=4 value='<?print date("Y",time())?>'>
</td>
</tr>
<tr>
<td><font color=red>Invoice No </td><td><input type=text name='invno' ></td>
<td>Transfer Date </td><td colspan=3>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())?>'>

<br>Location <select name='loc'>
<option></option>
<?
$re=mysqli_query($db,"select code,lname from loc_master where brcode='$brcode' order by lname");
while($lorow=mysqli_fetch_row($re))
{
 print "<option value='$lorow[0]'>$lorow[1]</option>";
}
?>
</select>
</td></tr>
<tr><td><font color='red'>DA </td><td>
<input type=text name='da'>
</td>
<td>Config </td><td>

<select name='config' onChange=fun1()><option></option>
    <?
     $i=1;
     $res=mysqli_query($db,"select * from veh_master where brcode='$brcode'");
     while($row=mysqli_fetch_row($res))
     {
       print "\n<option>$row[2]</option>"; 
       print "\n<script>config[$i]='".strtoupper($row[1])."'</script>";$i++;
     }
    ?>
  </select>
</td>
<td>Model Code </td><td><input type=text readonly name='mcode'></td></tr>

<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>
 <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>Discount </td><td><input type=text name='disc'></td><td>Color</td>
<td><input type=text name='color'></td><td><font color='red'>Engine No</td><td><input type=text name='engno' onKeyDown=chkey(this)></td>
</tr>
<tr><td><font color='red'>Chasis No</td><td><input type=text name='chasis' onKeyDown=chkey(this)></td>
<td colspan=4>Eich.MCode <input type=text name='emcode' size=10> 
 Remarks  <textarea name='remark' cols=32></textarea></td></tr>
<tr><td colspan=6 align=right bgcolor='green'> <input type=submit name='submit' value=' Save '>
<input type=button value='  Cancel  ' onClick=history.back(1)></td></tr>
</table>
</form>
</td></tr>
</table>
</center>
<br>
<script>
function calc()
{
 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;
  }
}
</script>
<style>
th{background-color:brown}
</style>
<form action="<?print $PHP_SELF?>">
Search DA <input type=text name='sea'> <input type=submit value='Search' name='search'>
</form>
<?
if($search)
 $sql="select * from veh_transin where brcode='$brcode' and da='$sea'";
else
 { $sql="select * from veh_transin where brcode='$brcode'  order by trans_no desc limit 50";
    print "<font color=blue>Showing last 50 Trasfers</font>";
 }

$result=mysqli_query($db,$sql)or die(mysqli_error());
 if(mysqli_num_rows($result)<=0)
 {
   print "<font color=red>Record not found</font>";
   return;
 }
print "<table border=1> <th>Trans <br> No <th>Inv No. <th>Inv Date <th>DA <th>M.Code <th>Config";
print "<th>Engine <th>Chasis <th>Delete";
while($row=mysqli_fetch_array($result))
{
 print "<tr><td>$row[trans_no]</td><td><a href='vmpur.php?code=$row[trans_no]'>$row[invno]</a></td><td>".datetohtml($row[invdate])."</td><td>$row[da]</td>";
 print "<td>$row[mcode]</td><td>$row[config]</td><td>$row[engno]</td><td>$row[chasis]</td>";
 if($row[sale_invoice]=='-')
 print "<td align=center><a href=javascript:del('$row[da]','$row[trans_no]')><img src='../img/delete2.gif' border=0></a></td></tr>";
}
?>
<script>
function del(da,pno,engine)
{
if(confirm("Do you want to delete Da "+da+" ?"))
 document.location="deltranveh.php?da="+da+"&pno="+pno;
}
</script>


