<? 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");

$us=mysqli_query($db,"select level from bruser where username='$user'")or die(mysqli_error());
$r=mysqli_fetch_row($us);
$user_level=$r[0];

$sql="select * from orderform where formno='$code' and brcode='$brcode'";

$re=mysqli_query($db,$sql)or die(mysqli_error());
$frm_row=mysqli_fetch_array($re);
$dt=explode('-',$frm_row[dt]);
if($save)
{
$dtt=$y.'-'.$m.'-'.$d;

if(!checkdate($m,$d,$y))
{
 print "<script>alert('Invalid Date'); history.back(1);</script>";
 return;
}

$sql="update orderform set dt='$dtt',cust='$cust',addr='$addr',phone='$phone',rname='$rname', model='$mcode',config='$config',seg='$seg',fine='$fine',exch='$exch', descr='$descr',exvalue='$exvalue', invalue='$invalue',disamt='$disamt',netamt='$netamt',oinf='$oinf',location='$loc',op='$op',
stat='$status',exvehno='$exvehno',exdivrate='$exmvalue',exsalevalue='$exsvalue',
exexpenses='$exexpvalue',bodydetails='$bodydetails',tin='$ctin',pan='$cpan',email='$em'
 where formno='$formno' and brcode='$brcode'";
mysqli_query($db,$sql) or die (mysqli_error());
print "<script>document.location='seaoform.php'</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'>

<script>
var ar1=[];

function valid()
{
var p1=document.f1.mcode.value;
var c=document.f1.cust.value;
var inv=document.f1.invalue.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(p1.length<=0)
  {submit="false"; alert('Invalid Model Code');}
 if(c.length<=0)
  {submit="false"; alert('Invalid Customer Name');}
 if(inv.length<=0 || isNaN(inv))
  {submit="false"; alert('Invalid invoice no.');}

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(submit=="false")
  return false;
}
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;
}


function fun1()
{
 var x=document.f1.config.selectedIndex;
 document.f1.mcode.value=ar1[document.f1.config.selectedIndex];
}
</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>EDIT ORDER FORM</td></tr>
<tr><td bgcolor='ivory' align=center>
<table border=0>
<tr><td>Order form number <td>

<input type=text name='formno' readonly size=10 value='<?print $frm_row[formno]?>'> 
Date dd <input type=text name='d' size=2 value='<?print $dt[2]?>'>
mm <input type=text name='m' size=2 value='<?print $dt[1]?>'>
yy <input type=text name='y' size=4 value='<?print $dt[0]?>'>
</td></tr>
<tr><td><font color='red'> Customer Name</td><td colspan=3>
<input type=text name='cust' size=50 value='<?print $frm_row[cust]?>'></td></tr>
<tr><td>Address</td><td colspan=3><textarea name='addr' cols=48><?print $frm_row[addr]?></textarea></td></tr>
<tr><td>Phone</td><td colspan=3>
  <input type=text name='phone' size=40 value='<?print $frm_row[phone]?>'>E_Mail <input type=text name='em' size=20 value='<?print $frm_row[email]?>'></td></tr>
<tr><td>TIN</td><td colspan=3>
  <input type=text name='ctin' size=20 value='<?print $frm_row[tin]?>'>PAN <input type=text name='cpan' size=20 value='<?print $frm_row[pan]?>'></td></tr>

<tr><td>Rep.Name</td><td colspan=3><select name='rname'>
<?
 $sq="select code,repname from rep_master where code='$frm_row[rname]' and branch='$brcode'";
// print $sq;
 $result1=mysqli_query($db,$sq) or die(mysqli_error());
 $rep_row=mysqli_fetch_row($result1) or die(mysqli_error());
 print "<option value='$rep_row[0]'>".strtoupper($rep_row[1])."</option>";

 $result=mysqli_query($db,"select * from rep_master where branch='$brcode' order by repname");
 while($row=mysqli_fetch_row($result))
 {
   print "\n<option value='$row[0]'>".strtoupper($row[1])."</option>";
 }
?>
</select>
District
<input type =text name='loc' value='<?print $frm_row[location]?>'>
</select>
</td></tr>
<tr><td><font color=red> Config</td><td colspan=3>

<select name='config' onChange=fun1()>
<option><? print $frm_row[config] ?></option>
<script>ar1[0]='<?print $frm_row[model]?>'</script>
    <?
     $i=1;
     $res=mysqli_query($db,"select * from veh_master where brcode='$brcode' 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' size=30 value='<?print $frm_row[model]?>'></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  value='<?print $frm_row[bodydetails]?>'></td>
</tr>
<tr><td>Segment</td><td><select name='seg'>
<?
 $result=mysqli_query($db,"select code,sname from seg_master where code='$frm_row[seg]'");
 $seg_row=mysqli_fetch_row($result);
 print "<option value='$seg_row[0]'>$seg_row[1]</option>";

 $result=mysqli_query($db,"select * from seg_master order by sname")or die(mysqli_error());
 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 code,finname,city from fin_master where code='$frm_row[fine]'");
 $fin_row=mysqli_fetch_row($result);
 print "<option value='$fin_row[0]'>$fin_row[1]-$fin_row[2]</option>";

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

<tr><td>Exchange (y/n)</td><td colspan=3><select name ='exch'>
<option><?print $frm_row[exch]?></option>
<option>N</option>
<option>Y</option>
</select>Exch.Veh.No<input type=text name='exvehno' size=10 value='<?print $frm_row[exvehno]?>'style={text-align:right}>Exch.Divison Value<input type=text name='exmvalue' size=10 value='<?print $frm_row[exdivrate]?>'style={text-align:right}></td></tr>
<tr><td>Exchange Description </td><td colspan=3>
<textarea name='descr' cols=48><?print $frm_row[descr]?></textarea>
</td></tr>
<tr><td>Exchange Value</td><td colspan=1>
<input type=text name='exvalue' size=10 value='<?print $frm_row[exvalue]?>' 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 value='<?print $frm_row[invalue]?>'style={text-align:right}onChange=calcnet()></td></tr>
<tr><td>Discount Amt.</td><td colspan=3>
 <input type=text name='disamt' size=10 value='<?print $frm_row[disamt]?>' style={text-align:right} onChange=calcnet()>
Net Amount to customer 
  <input type=text name='netamt' size=10 value='<?print $frm_row[netamt]?>' style={text-align:right}></td></tr>
<tr><td><font color=blue>Opening Balance</td><td colspan=3><input type=text name='op' value='<?print $frm_row[op]?>'></td></tr>
<tr><td>Other Information</td><td colspan=3>
<textarea name='oinf' cols=55 rows=3> <?print $frm_row[oinf]?></textarea></td></tr>

<?
if($user_level>=4)
{
?>
<tr><td>Ex.Veh.Sale Value</td><td colspan=1><input type=text name='exsvalue' size=10 value='<?print $frm_row[exsalevalue]?>'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 value='<?print $frm_row[exexpenses]?>' style={text-align:right}></td></tr>
<?
}
?>

<tr><td>Status</td><td><select name='status'>
<option><?print $frm_row[stat]?></option>
<option>In-Active</option>
<option>Active</option></select>
</td></tr>
<tr><td colspan=2 align=center bgcolor=green> <input type=submit value='Save' name='save'>
<input type=button value='Cancel' onClick=history.back(1)></td></tr>
</table>
</form>
