<? 
session_start();

if(strlen($brcode)<=0)
{
 print "<script> alert('Session timed out'); parent.document.location='../ind.php'; </script>";
 return;
}

include("../opendb.php");

if($sprint)
{ 
 $cusres=mysqli_query($db,"select * from ledger where led='$cust'");
  if(mysqli_num_rows($cusres)<=0)
  {
    print "<script> alert('Customer ledger cannot be found'); </script>";
    //return;
  }
 $re=mysqli_query($db,"select max(invno) from veh_invoice 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;
//*********************8 jv
 $jvre=mysqli_query($db,"select max(jvno) from journal2 where brcode='$brcode'")or die(mysqli_error());
 $jv_row=mysqli_fetch_row($jvre);
 if($jv_row[0]<=0)
  $jvno=1;
 else 
  $jvno=$jv_row[0]+1;


 $invdate=$y.'-'.$m.'-'.$d;
$sql="insert into veh_invoice values('$cust','$addr','$formno','$rep', '$model1', '$fin','$invno', '$invdate','$msno','$model','$config','$unit','$disc','$netamt', '$taxp','$tax', '$amt2', '$engno', '$segment', '$remark','$jvno','$brcode','$ttype','sale')";
 mysqli_query($db,$sql) or die(mysqli_error());

mysqli_query($db,"update orderform set stat='In-Active',sale_invno='$invno' where formno='$formno' and brcode='$brcode'");

$sql="update veh_purchase set invno='$invno', invtype='sale' where msno='$msno' and brcode='$brcode'";
   mysqli_query($db,$sql)or die(mysqli_error());
$re=mysqli_query($db,"select * from vehopstock where msno='$msno' and brcode='$brcode'");
 if(mysqli_num_rows($re)>0)
 {
   mysqli_query($db,"update vehopstock set invno='$invno',invtype='$ttype' where msno='$msno' and brcode='$brcode'");
 }

$jvr=mysqli_query($db,"select max(jvno) from journal2  where brcode='$brcode'");
 $j_row=mysqli_fetch_row($jvr);
  if($j_row[0]<=0)
   $jvno=1;
  else
  $jvno=($j_row[0]+1);

$sql="insert into journal2 values('$invdate','$jvno','Dr','$cust','$amt2', '0','Sales INV No: $invno','$brcode','V sale','MSNO','$msno','$invno')";
mysqli_query($db,$sql) or die("J V Write error 1:".mysqli_error());

$sql="insert into journal2 values('$invdate','$jvno','Cr','Output tax 12.5', '0','$tax','Sales INV No: $invno','$brcode','V sale','MSNO','$msno','$invno')";
mysqli_query($db,$sql) or die("J V Write error 2:".mysqli_error());

$sql="insert into journal2 values('$invdate','$jvno','Cr','Output Cess (VEH)', '0','$cessamt','Sales INV No: $invno','$brcode','V sale','MSNO','$msno','$invno')";
mysqli_query($db,$sql) or die("J V Write error 2:".mysqli_error());


 $sql="insert into journal2 values('$invdate','$jvno','Cr','Sales 12.5(vehicle)','0','$netamt','Sales INV No: $invno','$brcode','V sale','MSNO','$msno','$invno')";
mysqli_query($db,$sql) or die("J V Write error 3:".mysqli_error());

if($ttype=='8B')
 print "<script>window.open('prnvehinvoice.php?invno=$invno&msno=$msno&ttype=$ttype');</script>";
else
 print "<script>window.open('prnvehinvoice2.php?invno=$invno&msno=$msno');</script>";
print "<script>document.location='invoice1.php' </script>";

}

/*if($sprint5)
{ 
$mmm=mysqli_query($db,"select invno from veh_invoice where invno='$pno' and type !='d' and brcode='$brcode'");
 if(mysqli_num_rows($mmm)>0)
 { print "<script> alert('Duplicate invoice no'); history.back(1); </script>"; return;}

$jvr=mysqli_query($db,"select max(jvno) from journal2  where brcode='$brcode'");
 $j_row=mysqli_fetch_row($jvr);
  if($j_row[0]<=0)
   $jvno=1;
  else
  $jvno=($j_row[0]+1);

 $re=mysqli_query($db,"select max(invno) from veh_invoice 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;
 $rr=mysqli_query($db,"select code from rep_master where repname='$rep'");
 $rep_row=mysqli_fetch_row($rr);
 $rep=$rep_row[0];


  $sql="insert into veh_invoice values('$cust','$formno','$mcode','$rep','$area', '$fin','$invno', '$invdate','$da','$config','$unit','$disc','$netamt', '$taxp','$tax', '$amt2','$color','$engno','$segment','$chasis','$remark','$brcode','-','$jvno','$emcode','$tp')";
 mysqli_query($db,$sql) or die(mysqli_error());

 $sql="insert into veh_movement (date,inv_no,qty,type,brcode,sup_type,da)values('$invdate', '$invno','1','sale','$brcode','$cust','$da')";
 mysqli_query($db,$sql)or die(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' where dano='$da' and brcode='$brcode'");
// }
 $sql="update veh_purchase set sale_invoice='$invno', type='sale' where da='$da' and brcode='$brcode'";
   mysqli_query($db,$sql)or die(mysqli_error());

 mysqli_query($db,"update veh_transin set sale_invoice='$invno' where da='$da' and brcode='$brcode'") or die(mysqli_error());

$cure=mysqli_query($db,"select code,acname from cust_master where code='$cust'");
$cust_row=mysqli_fetch_row($cure);
 $cust_name=strtoupper($cust_row[1]);

$sql="insert into journal2 values('$invdate','$jvno','Dr','$cust_name','$netamt', '0','Sales INV No: $invno','$brcode','V sale','DA','$da','$invno')";
mysqli_query($db,$sql) or die("J V Write error 1:".mysqli_error());

$sql="insert into journal2 values('$invdate','$jvno','Cr','Output tax 12.5', '0','$tax','Sales INV No: $invno','$brcode','V sale','MSNO','$msno','$invno')";
mysqli_query($db,$sql) or die("J V Write error 2:".mysqli_error());

 $sql="insert into journal2 values('$invdate','$jvno','Cr','Sales 12.5(vehicle)','0','$amt2','Sales INV No: $invno','$brcode','V sale','MSNO','$msno','$invno')";
mysqli_query($db,$sql) or die("J V Write error 3:".mysqli_error());
if($tp=='8a')
 print "<script>window.open('prnvehinvoice.php?invno=$invno&da=$da');</script>";
else
 print "<script>window.open('prnvehinvoice2.php?invno=$invno&da=$da');</script>";

} // end save
*/
?>
<style>
select,input,body,td{font-family:'arial narrow';letter-spacing:1;font-size:13}
tr{background-color:c1d7bb}
.mnu{margin-left:5}
</style>
<body bgcolor='ivory'>

<script>
var adr=[];
var ar1=[]; 
var config2=[];
var rep=[];
var model1=[];

var config=[];
var model=[];
var em_code=[];
var engno=[];
function fun1()
{
 var x=document.f1.msno.selectedIndex; 
// alert(model[document.f1.da.selectedIndex]);
 if(x<=0)
   {
     document.f1.model.value='';
     document.f1.config.value='';
     document.f1.engno.value='';
   }
 else
  { 
    document.f1.config.value=config[document.f1.msno.selectedIndex];
    document.f1.model.value=model[document.f1.msno.selectedIndex];
    document.f1.engno.value=engno[document.f1.msno.selectedIndex];
  }


}
function valid()
{
 submitOK="true";

 if(document.f1.msno.selectedIndex<=0)
 {
   submitOK="false";
   alert('Invalid MSNO.');
 }

 if(document.f1.invno.value.length<=0)
 {
   submitOK="false";
   alert('Invalid Invoice No.');
 }
 if(document.f1.cust.value.length<=0)
 {
   submitOK="false";
   alert('Invalid Customer');
 }
 if(document.f1.fin.selectedIndex<=0)
 {
   submitOK="false";
   alert('Invalid Financer');
 }

 if(document.f1.formno.seletedIndex<=0)
 {
   submitOK="false";
   alert('Invalid Formno');
 }
 if(document.f1.unit.value.length<=0)
 {
   submitOK="false";
   alert('Invalid unit price');
 }
 if(document.f1.segment.selectedIndex<=0)
 {
   submitOK="false";
   alert('Invalid Segment No.');
 }
if(submitOK=="true")
{
 x=confirm("Do you want to save?");
 if(x)
   submitOK="true";
 else
    submitOK="false";
}
  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.model1.value=model1[document.f1.formno.selectedIndex];
     document.f1.rep.value=rep[document.f1.formno.selectedIndex];

   }
  else
    {
      document.f1.model1.value='';
     document.f1.rep.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()">
<input type=hidden name='ttype' value='<?print $ttype?>'>
<table cellspacing=1 cellpadding=1 bgcolor=black>
<tr><td align=center bgcolor='GRAY'><FONT COLOR='IVORY'><B>CUSTOMER DETAILS</TD></TR>
<TR><td align=center bgcolor='ivory'>
<table>
<tr><td><font color=red>Customer</td><td colspan=3> <select name='cust' onChange='setadr()'>
<option></option>
<?
$i=1;
$re=mysqli_query($db,"select * from customer where brcode='$brcode' order by acname")or die(mysqli_error());
while($row=mysqli_fetch_array($re))
{
 print "\n<option>$row[acname]</option>";
 print "\n<script>adr[$i]='$row[addr1] - $row[city] - $row[dist] - $row[stat] - $row[pin]' </script>";
 $i++;
}
?>
</select> </td></tr>
<tr>
<td>Address </td><td><textarea name='addr' cols=40></textarea></td>
<td><font color=red>Form No</td><td> <select name='formno' onChange='setModel()'>
<option></option>
<?
$rr=mysqli_query($db,"select a.formno,a.model,b.empname from orderform a,empmaster b where a.rname=b.empcode and a.sale_invno='-' and a.brcode='$brcode'")or die(mysqli_error());
 $i=1;
while($row=mysqli_fetch_row($rr))
{
 print "\n<option>$row[0]</option>";
 print "\n<script> model1[$i]='$row[1]' </script>";
 print "\n<script> rep[$i]='$row[2]' </script>";

 $i++;
}
?>
</select> 

<!-- <input type=text readonly name='model'>-->
</td></tr>
<tr><td>Rep<td><input type=text readonly name='rep'></td> 
<td>Mod Code</td><td><input type=text name='model1' readonly></td></tr>
<tr><td><font color=red>Financer</font> </td><td colspan=3> <select name='fin'> <option></option>
<?
 $re=mysqli_query($db,"select code,finname from fin_master where branch='$brcode' order by finname");
  while($row=mysqli_fetch_row($re))
  {
   print "<option value='$row[1]'>$row[1]</option>";
  }
?>
</select>
</td></tr>
</table>
 </td></tr>

</table>




<br>
<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 SALES  (Inv No:

<?
$rs=mysqli_query($db,"select max(invno) from veh_invoice 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>

</tr>
<tr>
<td>Invoice No </td><td><input type=text  name='invno' value='<?print $pno?>' size=8 style={background-color:silver}></td>
<td>Invoice 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>MSNO </td><td>
<select name='msno' onChange=fun1()>
<option></option>
<?
$i=1;
$result=mysqli_query($db,"select * from veh_purchase where invno='-' and  brcode='$brcode' order by msno")or die(mysqli_error());
while($row=mysqli_fetch_array($result))
{
 print "\n<option>$row[msno]</option>";
 print "\n<script> \n config[$i]='$row[config]'; \n";
 print "model[$i]='$row[model]'; engno[$i]='$row[engno]';</script>";
 $i++;
}
$result=mysqli_query($db,"select * from vehopstock where brcode='$brcode' and invno='0' order by msno")or die('err'.mysqli_error());
while($row=mysqli_fetch_array($result))
{
 print "\n<option>$row[msno]</option>";
 print "\n<script> ar1[$i]='$row[mcode]';model[$i]='$row[model]'; engno[$i]='$row[engno]'\n config[$i]='$row[config]'; \n";
 print "\n; </script>";
 $i++;
}
?>
</select>
</td>
<td>Model  </td><td><input type=text name='model'>
</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'  onChange=calc()> </td>
    
<tr><td>Before Tax</td><td><input type=text  name='netamt' onChange=calc()></td>
    <td>Tax %</td><td><input type=text readonly name='taxp' size=5 value='12.5'>
 <input type=text name='tax' readonly size=10 value='0'></td>
    
</tr>
<tr><td>Cess%</td><td><input type=text name='cessp' value='1' size=4></td>
<td>Cess Amt </td><td><input type=text name='cessamt' size=5></td>
<td>Net Amount </td><td><input type=text readonly  name='amt2' ></td>
</tr>

<tr><td>Engine No</td><td><input type=text readonly name='engno'></td>


<td><font color=red>Segment </td><td colspan=5><select name='segment'><option></option>
<?
 $re=mysqli_query($db,"select * from seg_master order by sname");
 while($row=mysqli_fetch_row($re))
 {
   print "<option>$row[1]</option>";
 }
?>
</select></td>
</tr>
<tr>
<td>Remarks </td><td colspan=3><textarea name='remark' cols=55></textarea></td></tr>
<tr><td colspan=6 align=center bgcolor='green'> 
<input type=submit  value=' SAVE AND PRINT BILL ' name='sprint'>


<!--<input type=BUTTON value='  SAVE AND ADD ITEMS  ' name='svadd'> -->
<input type=button value='  CANCEL  ' onClick=history.back(1)></td></tr>
</table>
</form>
</td></tr>
</table>
</center>
<br>
<script>
function calc()
{
document.f1.netamt.value=document.f1.unit.value;
 net=parseFloat(document.f1.netamt.value);
 amt=(net*parseFloat(document.f1.taxp.value))/100; 

 tax=parseFloat(document.f1.amt2.value)*parseFloat(document.f1.taxp.value)/100;
 document.f1.tax.value=(amt).toFixed(2);

  vattax=parseFloat(document.f1.tax.value);
  cess=vattax*parseFloat(document.f1.cessp.value)/100;
  document.f1.cessamt.value=parseFloat(cess).toFixed(2);
 document.f1.amt2.value=parseFloat((net+amt+cess).toFixed(2));

}
</script>



