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

include("../opendb.php");
if($sprint)
{ 
 $re=mysqli_query($db,"select max(invno) from veh_invoice where brcode='$brcode' and type='-'")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];

$rs=mysqli_query($db,"select max(invno) from profinv where brcode='$brcode' ")or die(mysqli_error());
 $p_row=mysqli_fetch_row($rs);
 if(mysqli_num_rows($rs)<=0)
  $profno=1;
 else
      $profno=$p_row[0]+1;
 

  $sql="insert into profinv values('$cust','$formno','$model','$rep','$area', '$fin','$pno', '$invdate','$hsn','$config','$unit','$disc','$netamt', '$taxp','$tax', '$amt2','$color', '$engno','$segment','$chasis','$remark','$brcode','-','$addr', '$mmoney','$ptype' ,'$cessp','$cessamt','$qty','$body','$wheel','$power','$tyre','$gvw','$insur','$regis','$spgov','','$tcs')";
 mysqli_query($db,$sql) or die(mysqli_error());

//print "<script>window.open('prnprofnewclt.php?invno=$profno&da=$da');</script>";

print "<script>window.open('prnprofnewgst.php?invno=$profno&da=$da');</script>";
//print "<script>window.open('prnprofrec.php?invno=$profno');</script>";
$cure=mysqli_query($db,"select code,custname from cust_master where code='$cust'");
$cust_row=mysqli_fetch_row($cure);
 $cust_name=strtoupper($cust_row[1]);


} // end save

?>
<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 adr2=[];
var ar1=[];
var config=[];
var model=[];
var rep=[];
var area=[];
var color=[];
var engno=[];
var chasis=[];
var mcode=[];
var cust=[];
function fun1()
{
 var x=document.f1.config.selectedIndex; 
 
 if(x<=0)
   {
     document.f1.mcode.value='';
     document.f1.config.value='';
   }
 else
  { 
    document.f1.mcode.value=mcode[document.f1.config.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];*/

  }

/*  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.qty.value<=0 || isNaN(document.f1.qty.value))
 {
   submitOK="false";
   alert('Invalid Quantity');
   document.f1.qty.focus();
 }
 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.value.length<=0)
 {
   submitOK="false";
   alert('Invalid Financer');
 }

 if(document.f1.formno.value.length<=0)
 {
   submitOK="false";
   alert('Invalid Formno');
 }
*/
 if(document.f1.unit.value.length<=0)
 {
   submitOK="false";
   alert('Invalid unit price');
 }
/*
 if(document.f1.segment.value.length<=0)
 {
   submitOK="false";
   alert('Invalid Segment');
 }
*/
  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];
     document.f1.cust.value=cust[document.f1.formno.selectedIndex];
     document.f1.addr.value=adr[document.f1.formno.selectedIndex];

   }
  else
    {
      document.f1.model.value='';
     document.f1.rep.value='';
     document.f1.area.value='';
   }

}
 

</script>


<style>
input,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'>

<input type=button value='Show All' onClick=document.location='listproforma.php'><br>&nbsp;
<form name='f1' action='<?print $PHP_SELF?>' onSubmit="return valid()">
<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>
<input type=text name='cust' size=30>


<?
/*$i=1;
$re=mysqli_query($db,"select * from orderform where brcode='$brcode'");
while($row=mysqli_fetch_array($re))
{
// print "\n<option value='$row[code]'>$row[acname]</option>";
 print "\n<script>cust[$i]='$row[cust]'; adr[$i]='".str_replace(' ','\n',$row[addr])."' </script>";
 $i++;
}*/ 
//deleted
?>


</select> </td>
<td>Address </td><td><textarea name='addr' cols=40></textarea></td></tr>
<tr><td>Financer</td><td colspan=4>   <select name='fin'> <option></option>
<?
 $re=mysqli_query($db,"select code,finname,city from fin_master order by finname");
  while($row=mysqli_fetch_row($re))
  {
   print "<option $row[1]>$row[1]-[$row[2]]</option>";
  }
?>
</select>
</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>PROFORMA INVOICE  (Inv No:

<?
$rs=mysqli_query($db,"select max(invno) from profinv 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 readonly 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>
<?
if($brcode=='AUT009'||$brcode=='AUT103')
{

?>
<td><center><font color='red'>Qty</td><td><input type=text name='qty' size=2 onChange=calcKA()></td>
<?
}
else
{
?>

<td><center><font color='red'>Qty</td><td><input type=text name='qty' size=2 onChange=calc()></td>
<?
}
?>
</tr>
<tr><td>config </td><td>
<select name='config' onChange=fun1()>
<option></option>
<?
$x=1;
$cc=mysqli_query($db,"select * from veh_master where brcode='$brcode' and active='y' order by config");
while($crow=mysqli_fetch_row($cc))
{
  print "\n<option>$crow[2]</option>";
  print "\n<script> mcode[$x]='$crow[1]'; </script>";
 $x++;
}
?>
</select>

</td><td>Wheel Base</td><td><input type=text name='wheel'></td><td>Power</td><td><input type=text name='power'></td></tr>

<?
if($brcode=='AUT009'||$brcode=='AUT103')
{

?>
<td><font color=red>Unit Price </td><td><input type=text name='unit' size=8 onChange=calcKA()>  HSN Code <input type=text name='hsn' size=8 ></td>
    <td>Discount </td><td><input type=text name='disc' SIZE=8 onChange=calcKA()></td><td>GVW</td><td><input type=text name='gvw'></td>
<?
}
else
{
?>

<tr><td><font color=red>Unit Price </td><td><input type=text name='unit' size=8 onChange=calc()> HSN Code <input type=text name='hsn' size=8 > </td>
    <td>Discount </td><td><input type=text name='disc' SIZE=8 onChange=calc()></td><td>GVW</td><td><input type=text name='gvw'></td>
<?
}
?>
<!--
<td>Margin Money Recpt.</td><td><input type=text name='mmoney' size=9> <br>
<input type=text name='ptype' value='Cash'>
-->
</td>
<?
if ($brcode=='AUT009'||$brcode=='AUT103')
{
?>
<tr><td>Net Amount</td><td><input type=text readonly name='netamt' onChange=calcKA()></td>
    <td>Tax %</td><td><input type=text readonly name='taxp' size=5 value='28'>
 <input type=text name='tax' readonly size=10 value='0'></td>

<?
}
else 
{
?>
<tr><td>Net Amount</td><td><input type=text readonly name='netamt' onChange=calc()></td>
    <td>Tax %</td><td><input type=text readonly name='taxp' size=5 value='28'>
 <input type=text name='tax' readonly size=10 value='0'></td>
<?
}
?>


<td>Amount <br>Before Tax </td><td><input type=text readonly  name='amt2'></td>
</tr>
<tr>
<td>
Cess % </td><td>
<?
if($brcode=='AUT009'||$brcode=='AUT103')
{
?>
<input type=text name='cessp' value='0' size=1>
<?
}
else
{
?>
<input type=text name='cessp' value='0' size=1>
<?
}

?>
TCS %<input type=text name='tcs' value=1 size=2>

</td>
<td>
Cess Amt </td><td><input type=text name='cessamt' value='' size=4>
</td><td>Speed governor amount</td><td><input type=text  name='spgov'></td>

</tr>
<tr><td>Color</td>
<td><input type=text name='color'></td><td>Engine No</td><td><input type=text readonly name='engno'></td><td>Tyre size</td><td><input type=text name='tyre'></td></tr>
<tr><td><font color=red>Segment </td><td><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><td>Insurance charge</td><td><input type=text  name='insur'></td><td>Registr,permit charge</td><td><input type=text  name='regis'></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=55></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=history.back(1)></td></tr>
</table>
</form>
</td></tr>
</table>
</center>
<br>
<script>
function calc()
{

/*
qty=parseInt(document.f1.qty.value);
document.f1.amt2.value=(document.f1.unit.value-document.f1.disc.value)*qty;
 //net=parseFloat(document.f1.netamt.value);
 amt=(document.f1.amt2.value* 14.5)/100; 
 document.f1.tax.value=parseFloat(amt).toFixed(2);
var netamt2=parseFloat(document.f1.amt2.value)+parseFloat(document.f1.tax.value);
document.f1.netamt.value=netamt2.toFixed(0);

*/

qty=parseInt(document.f1.qty.value);
document.f1.netamt.value=(document.f1.unit.value-document.f1.disc.value)*qty;
 net=parseFloat(document.f1.netamt.value);
 amt=(net* 100)/128; 
 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);
 document.f1.tax.value=(tax).toFixed(2);
 var cessper=parseFloat(document.f1.cessp.value).toFixed(0);
 var ces_amt=parseFloat((document.f1.tax.value*cessper)/100);
 document.f1.cessamt.value=ces_amt.toFixed(2);


}

function calcKA()
{
qty=parseInt(document.f1.qty.value);
document.f1.netamt.value=(document.f1.unit.value-document.f1.disc.value)*qty;
 net=parseFloat(document.f1.netamt.value);
 amt=(net* 100)/114; 
 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);
 document.f1.tax.value=(tax).toFixed(2);
 var cessper=parseFloat(document.f1.cessp.value).toFixed(0);
 var ces_amt=parseFloat((document.f1.tax.value*cessper)/100);
 document.f1.cessamt.value=ces_amt.toFixed(2);

/*
document.f1.netamt.value=(document.f1.unit.value-document.f1.disc.value);
 net=parseFloat(document.f1.netamt.value);
 amt=(net*100)/114; 
 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);
*/
}
</script>



