<? 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($save)
{

$rs=mysqli_query($db,"select max(amcno) from schemenew ");
$cr_row=mysqli_fetch_row($rs);
 if($cr_row[0]<=0)
{
   $cpno=1;
}
 else   
{ 
  $cpno=$cr_row[0]+1;
}
 $entrydate=$y.'-'.$m.'-'.$d;
 $dfrom=$y1.'-'.$m1.'-'.$d1;
 $dto=$y2.'-'.$m2.'-'.$d2;
$code=substr($chno,0,4).date("dms");
$sql="create table schemenew (code varchar(100),entrydate date,amcno varchar(100),chno varchar(100),brcode varchar(20),rem varchar(250),schemename varchar(200))";
mysqli_query($db,$sql);
 $re=mysqli_query($db,"select * from schemenew where chno='$chno' and brcode='$brcode'");
  if(mysqli_num_rows($re)>0)
  {
     print "Duplicate entry ". $mcode;
     print "<script> alert('Duplicate entry $chno'); history.back(1);  </script>";
     return;
  }

$sql="insert into schemenew values('$code','$entrydate','$cpno','$chno','$brcode','$rem','$scheme','$dfrom','$dto','$sodo','$eodo')";
mysqli_query($db,$sql);
print "Record has been saved";
include("scheme2.php");
}// end save

if($del)
{
$rr2=mysqli_query($db,"select * from newtrans where chno='$chno' and brcode='$brcode'");
  if(mysqli_num_rows($rr2)>0)
{
 print "<script> alert('Cannot be deleted.Pls empty transactions')</script>";
return;
}
  mysqli_query($db,"delete from schemenew where chno='$chno'")or die('err'.mysqli_error());
 print "<script>document.location='scheme2.php'</script>";

}




?>
<script>

function valid()
{

 if(document.f1.scheme.selectedIndex<=0)
 {
  
   alert('Invalid Scheme');
 document.f1.scheme.focus();
   return false;
 }


 
 if(confirm('Do you want to save?'))
   return true;
else
  return false;
}
</script>







<style>
body,td{font-family:'arial narrow';letter-spacing:1;font-size:13}
th{background-color:c1d7bb;font-size:13}
.mnu{margin-left:5}
</style>
<center>
<form name='f2' action='<?print $PHP_SELF?>' > 
<table>
<th colspan=4>FREE SERVICE SCHEME </TH>

<tr>
<td><font color=red>Schemelist </td><td><select name='schemelist'><option>Select</option>
<?
 $re=mysqli_query($db,"select * from schememaster order by sname");
 while($row=mysqli_fetch_row($re))
 {
   print "<option>$row[1]</option>";
 }
?>
</select></td><td></td>
</tr>
<th colspan=4><input type=submit value='show' name='show'>

</td></tr>
</table>
</form>
<center>
<form name='f1' action='<?print $PHP_SELF?>' onSubmit="return valid()"> 
<table>
<th colspan=4>FREE SERVICE SCHEME </TH>
<tr><td>Entry Date </td><td>dd <input type=text name='d' size=2 maxlength=2 value='<?print date('d')?>'>
mm <input type=text name='m' size=2 maxlength=2 value='<?print date('m')?>'>
yy <input type=text name='y' size=4 maxlength=4 value='<?print date('Y')?>'>
</td></tr>

<tr><td>Start Date </td><td>dd <input type=text name='d1' size=2 maxlength=2 value='<?print date('d')?>'>
mm <input type=text name='m1' size=2 maxlength=2 value='<?print date('m')?>'>
yy <input type=text name='y1' size=4 maxlength=4 value='<?print date('Y')?>'>
</td></tr>


<tr><td>End Date </td><td>dd <input type=text name='d2' size=2 maxlength=2 value='<?print date('d')?>'>
mm <input type=text name='m2' size=2 maxlength=2 value='<?print date('m')?>'>
yy <input type=text name='y2' size=4 maxlength=4 value='<?print date('Y')?>'>
</td></tr>

<tr><td>Chasis No </td><td><input type=text name='chno'>
</td></tr>
<tr><td>Starting odo meter</td><td><input type=text name='sodo'>
</td></tr>
<tr><td>ending odo meter</td><td><input type=text name='eodo'>
</td></tr>
<tr>
<td><font color=red>Scheme </td><td><select name='scheme'><option></option>
<?
 $re=mysqli_query($db,"select * from schememaster order by sname");
 while($row=mysqli_fetch_row($re))
 {
   print "<option>$row[1]</option>";
 }
?>
</select></td><td></td>
</tr>
<tr><td>Customer Name and Address</td><td><textarea name='rem' rows=3 cols=38 ></textarea></td></tr>

<th colspan=4><input type=submit value='Save' name='save'>
<input type=button value='Cancel' onClick=document.location='../blank.html'>
</td></tr>
</table>
</form>


<hr>
<?
$i=1;
print "<table cellspacing=1 cellpadding=3 bgcolor='gray'>";
print "<th>No<th>Scheme <th>Entry date <th>Chasis";
print "<th>Customer<th>Address<th>Address2<th>Invno<th>Date<th>Start Date<th>End Date<th>Starting Odo<th>Ending Odo";

if($show)
{
$re=mysqli_query($db,"select * from schemenew where schemename='$schemelist' and chno not in (select chno from scheme2011extdcmpltd) order by entrydate,chno");
while($row=mysqli_fetch_array($re))
{

$re22=mysqli_query($db,"select cust,invno,invdate from veh_invoice where chasis='$row[chno]' and brcode='$brcode' order by invdate desc;");
  if(mysqli_num_rows($re22)>0)
{
$row12=mysqli_fetch_array($re22);
}

else if(mysqli_num_rows($re22)==0)
{
$re22=mysqli_query($db,"select cust,invno,invdate from psndb1314.veh_invoice where chasis='$row[chno]' and brcode='$brcode' order by invdate desc;");
if(mysqli_num_rows($re22)>0)
{
$row12=mysqli_fetch_array($re22);
}


else if(mysqli_num_rows($re22)==0)
{
$re22=mysqli_query($db,"select cust,invno,invdate from psndb1011.veh_invoice where chasis='$row[chno]' and brcode='$brcode' order by invdate desc;");
if(mysqli_num_rows($re22)>0)
{
$row12=mysqli_fetch_array($re22);
}
else if(mysqli_num_rows($re22)==0)
{
$re22=mysqli_query($db,"select cust,invno,invdate from psndb1213.veh_invoice where chasis='$row[chno]' and brcode='$brcode' order by invdate desc;");
if(mysqli_num_rows($re22)>0)
{
$row12=mysqli_fetch_array($re22);
}
else 
{
$re22=mysqli_query($db,"select cust,invno,invdate from psndb1112.veh_invoice where chasis='$row[chno]' and brcode='$brcode' order by invdate desc;");
$row12=mysqli_fetch_array($re22);
}
}
}
}
$re2=mysqli_query($db,"select * from cust_master where code='$row12[cust]' and branch='$brcode'");
$row1=mysqli_fetch_array($re2);

 print "\n<form action='$PHP_SELF'> <input type=hidden name='chno' value='$row[3]'><tr><td>";

 print "<tr bgcolor='white'><td>".$i++."</td><td>$row[6]</td><td>".datetohtml($row[1])."</td><td>$row[3]</td><td>$row1[1]</td><td>$row1[2]</td><td>$row1[3]</td><td>$row12[1]</td><td>$row12[2]</td><td>".datetohtml($row[7])."</td><td>".datetohtml($row[8])."</td><td>$row[9]</td><td>$row[10]</td>";
if($level>=4)
{
print "<td align=right><input type=submit value='Del' name='del'</td></form>";
}  
}
}
?>

