<?
session_start();
$brcode = $_SESSION['brcode'];
$user = $_SESSION['user'];
if(strlen($brcode)<=0)
{
 print "<script> alert('Session timed out'); parent.document.location='../ind.php'; </script>";
 return;
}
include("../opendb.php");
$us=mysqli_query($db,"select level from bruser where username='$user'")or die(mysqli_error());
$r=mysqli_fetch_row($us);
$user_level=$r[0];
$save = $_REQUEST['save'];
if($save)
{
$d = $_REQUEST['d'];
$m = $_REQUEST['m'];
$y = $_REQUEST['y'];
$dod = $_REQUEST['dod'];
$dom = $_REQUEST['dom'];
$doy = $_REQUEST['doy'];
$d1 = $_REQUEST['d1'];
$m1 = $_REQUEST['m1'];
$y1 = $_REQUEST['y1'];
$d3 = $_REQUEST['d3'];
$m3 = $_REQUEST['m3'];
$y3 = $_REQUEST['y3'];
$cust=$_REQUEST['cust'];
$addr=$_REQUEST['addr'];
$phone=$_REQUEST['phone'];
$rname=$_REQUEST['rname'];
$mcode=$_REQUEST['mcode'];
$config=$_REQUEST['config'];
$seg=$_REQUEST['seg'];
$fine=$_REQUEST['fine'];
$exch=$_REQUEST['exch'];
$descr=$_REQUEST['descr'];
$exvalue=$_REQUEST['exvalue'];
$invalue=$_REQUEST['invalue'];
$disamt=$_REQUEST['disamt'];
$netamt=$_REQUEST['netamt'];
$oinf=$_REQUEST['oinf'];
$loc=$_REQUEST['loc'];
$dob=$_REQUEST['dob'];
$op=$_REQUEST['op'];
$mofno=$_REQUEST['mofno'];
$recptno=$_REQUEST['recptno'];
$exvehno=$_REQUEST['exvehno'];
$exmvalue=$_REQUEST['exmvalue'];
$exsvalue=$_REQUEST['exsvalue'];
$exexpvalue=$_REQUEST['exexpvalue'];
$bodydetails=$_REQUEST['bodydetails'];
$ctin=$_REQUEST['ctin'];
$cpin=$_REQUEST['cpin'];
$em=$_REQUEST['em'];
$dt=$y.'-'.$m.'-'.$d;
$ofdate=$y1.'-'.$m1.'-'.$d1;
$recdt=$y3.'-'.$m3.'-'.$d3;
if(!checkdate($m,$d,$y))
{
 print "<script>alert('Invalid Date'); history.back(1);</script>";
 return;
}
$cust=str_replace('  ',' ',$cust);

$re=mysqli_query($db,"select max(formno) from orderform where brcode='$brcode'")or die(mysqli_error());
$r=mysqli_fetch_row($re);

if($r[0]<=0)
 $form_no=1;
else
 $form_no=$r[0]+1;
 
$dob=$doy.'-'.$dom.'-'.$dod;
if($dob<'0000-00-00')
$dob='0000-00-00';
if($exmvalue<='')
$exmvalue=0;
if($exvalue<='')
$exvalue=0;
if($invalue<='')
$invalue=0;
if($disamt<='')
$disamt=0;
if($netamt<='')
$netamt=0;
if($exsvalue<='')
$exsvalue=0;
if($exexpvalue<='')
$exexpvalue=0;
if($op<='')
$op=0;
if($recptno<='')
$recptno=0;
$sql="insert into orderform  (formno,dt,cust,addr,phone,rname,model,config,seg,fine,exch,descr,exvalue,invalue,disamt, netamt,oinf,brcode,location,dob,op,stat,actvtd,ofdate,user,mofno,recptno,recdt,exvehno,exdivrate,exsalevalue,exexpenses,bodydetails,tin,pan,email) values('$form_no', '$dt','$cust', '$addr','$phone', '$rname', '$mcode','$config','$seg', '$fine','$exch','$descr', '$exvalue', '$invalue', '$disamt','$netamt','$oinf', '$brcode','$loc','$dob', '$op','Active','n', '$ofdate', '$user','$mofno','$recptno','$recdt','$exvehno','$exmvalue','$exsvalue','$exexpvalue','$bodydetails','$ctin','$cpin','$em')";
mysqli_query($db,$sql) or die (mysqli_error());

//$msg="Of No:".$form_no." Customer:".$cust." Config:".$config." Net:".$netamt." Rep:".$brcode;
//print "<script> window.open('sendsms.php?msg=$msg&to=91%209447779986'); window.open('sendsms.php?msg=$msg&to=91%209895211033');</script>";
}
?>
<script>
var ar1=[];
function valid()
{
var p1=document.f1.config.selectedIndex; 
var c=document.f1.cust.value;
var inv=document.f1.invalue.value;
var net=document.f1.netamt.value;
var cc=document.f1.exch.value;
var xx=document.f1.descr.value;
var xreg=document.f1.exvehno.value;
var xmval=document.f1.exmvalue.value;
var xval=document.f1.exvalue.value;





 submit="true";
 if(document.f1.mofno.value.length<=0)
  {submit="false"; alert('Invalid Order form number');
   document.f1.mofno.focus();
  }
 
 if(p1<=0)
  {submit="false"; alert('Invalid Model Code');}
 if(c.length<=0)
  {submit="false"; alert('Invalid Customer Name');}
 if(document.f1.rname.value.length<=0)
  {submit="false"; alert('Invalid Rep. Name');}

if(cc=='Y')

{

if(xx.length<=0)
  {submit="false"; alert('Invalid exchange descr');}

if(xreg.length<=0)
  {submit="false"; alert('Invalid Ex-Reg No.');}

if(xmval.length<=0)
  {submit="false"; alert('Invalid Ex-Market Value.');}

if(xval.length<=0)
  {submit="false"; alert('Invalid Exchange Value.');}

}

 if(document.f1.phone.value.length<=0)
  {submit="false"; alert('Invalid Phone Number');}


 if(inv.length<=0 || isNaN(inv)||net.length<=0 ||isNaN(net))
  {submit="false"; alert('Invalid invoice');}
 if(isNaN(net)||net<=0)
  {submit="false"; alert('Invalid Net Amount');}

 if(submit=="false")
  return false;
}


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

}

function calcnet()
{
 var inv=parseFloat(document.f1.invalue.value);
 var dis=parseFloat(document.f1.disamt.value);
 if(dis>0)
 var net=(inv-dis);
 else
 net=inv;
 document.f1.netamt.value=net;
}
</script>

<style>
body,td{font-family:'arial narrow';letter-spacing:1;}
tr{background-color:c1d7bb}
.mnu{margin-left:5}
</style>
<body bgcolor='ivory'>
<center>
<form name='f1' action='<?print $PHP_SELF?>' method='post' onSubmit="return valid()">


<table cellspacing=1 cellpadding=1 bgcolor=green>
<tr><td bgcolor='green' align=center> <font color=ivory><b>ORDER FORM</td></tr>
<tr><td bgcolor='ivory' align=center>
<table border=0>
<tr><td>SL No. <td>
<?
$re=mysqli_query($db,"select max(formno) from orderform where brcode='$brcode'")or die(mysqli_error());
$r=mysqli_fetch_row($re);
if($r[0]<=0)
 $form_no=1;
else
 $form_no=$r[0]+1;

?>
<input type=text name='formno' readonly size=10 value='<?print $form_no?>'> 
Entry Date dd <input type=text readonly name='d' size=2 value='<?print date("d",time())?>'>
mm <input type=text name='m'  readonly size=2 value='<?print date("m",time())?>'>
yy <input type=text name='y' readonly size=4 value='<?print date("Y",time())?>'>
</td></tr>
<tr><td>Order form Date </td><td>dd <input type=text name='d1' size=2 value='<?print date("d",time())?>'>
mm <input type=text name='m1' size=2 value='<?print date("m",time())?>'>
yy <input type=text name='y1' size=4 value='<?print date("Y",time())?>'>
Order form No <input type=text name='mofno' size=5>
</td></tr>
<tr><td><font color='red'> Customer Name</td><td colspan=3>
<input type=text name='cust' size=50 onKeyDown=chkey(this);></td></tr>
<tr><td>Address</td><td colspan=3><input type=text name='addr' size=50></td></tr>
<tr><td>Date of birth</td> <td>dd<input type=text name='dod' size=2>
		 mm<input type=text name='dom' size=2>
		 yy<input type=text name='doy' size=4>

       TIN <input type=text name='ctin' size=15>
PAN <input type=text name='cpan' size=15>
</tr>
<tr><td>
Phone </td><td><input type=text name='phone' size=50>Email <input type=text name='em' size=15></td></tr>

<tr><td><font color=red>Rep.Name</td><td colspan=3><select name='rname' onChange=cng()><option></option>
<?
 $result=mysqli_query($db,"select * from rep_master where branch='$brcode' order by repname");
 $i=1;
 while($row=mysqli_fetch_row($result))
 {
   print "\n<option value='$row[0]'>".strtoupper($row[1])."</option>";
   print "\n  <script> br[$i]='$row[5]'; </script>";
  $i++;
 }
?>
</select>
Branch <input type=text name='loc' readonly>
</td></tr>
<tr><td><font color=red> Config</td>
<td colspan=3>

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


 

</td>
</tr>
<tr><td><font color=red>Body Details </td><td>Eg: 150x8x6x4 P + 30 Blade + Back Open<input type=text name='bodydetails' size=75></td>
</tr>
<tr><td>Segment</td><td><select name='seg'>
<?
 $result=mysqli_query($db,"select * from seg_master order by sname");
 while($row=mysqli_fetch_row($result))
 {
   print "<option value='$row[0]'>".strtoupper($row[1])."</option>";
 }
?>
</select>
</td></tr>


<tr><td>Financier/Bank</td><td colspan=3><select name='fine'>
<?
 $result=mysqli_query($db,"select * from fin_master where branch='$brcode' order by finname");
 while($row=mysqli_fetch_array($result))
 {
   print "<option value='$row[0]'>".strtoupper($row[1]).'-['.strtoupper($row['city'])."]</option>";
 }
?>
</select>
</td></tr>

<tr><td>Exchange (y/n)</td><td colspan=3><select name ='exch'><option>N</option><option>Y</option></select>Exch.Veh.No<input type=text name='exvehno' size=10 style={text-align:right}>Exch.Divison Value<input type=text name='exmvalue' size=10 style={text-align:right}></td></tr>
<tr><td>Exchange Description </td><td colspan=3><textarea name='descr' cols=48 rows=1></textarea></td></tr>
<tr><td>Exchange Offer Value</td><td colspan=1><input type=text name='exvalue' size=10 style={text-align:right}>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<font color=red>Invoice Value <input type=text name='invalue' size=10 style={text-align:right} onChange=calcnet()></td></tr>
<tr><td>Discount Amt.</td><td colspan=3><input type=text name='disamt' size=10 style={text-align:right} onChange=calcnet()>
<font color=red>Net Amount to customer</font> <input type=text name='netamt' size=10 style={text-align:right}></td></tr>
<tr><td><font color=blue>Opening Balance</td><td colspan=3><input type=text name='op'>
Receipt No <input type=text name='recptno' size=5> Rec Date dd <input type=text name='d3' size=2 value='<?print date("d",time())?>'>
mm <input type=text name='m3' size=2 value='<?print date("m",time())?>'>
yy <input type=text name='y3' size=4 value='<?print date("Y",time())?>'>
</td></tr>
<tr><td>Other Information</td><td colspan=3><textarea name='oinf' cols=48 rows=2></textarea></td></tr>
<?
if($user_level>=4)
{
?>
<tr><td>Ex.Veh.Sale Value</td><td colspan=1><input type=text name='exsvalue' size=10 style={text-align:right}>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;


Ex.Veh.Expenses <input type=text name='exexpvalue' size=10 style={text-align:right}></td></tr>
<?
}
?>

<tr><td colspan=2 align=center bgcolor=green> <input type=submit value='Save' name='save'>
<input type=submit value='Cancel' onClick=document.location='../blank.html'></td></tr>
</table>
</form>
<script>
function chkey(abc)
{
 yy=abc.value
 if(yy.indexOf('  ')>-1) 
  {
    alert('Multiple space is not allowed');
    abc.value='';
  }
 if(yy.indexOf("'")>-1) 
  {
    alert("\' is not allowed");
    abc.value='';
  }
if(yy.indexOf("&")>-1) 
  {
    alert("& is not allowed");
    abc.value='';
  }

}
</script>



