<? 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 scheme1 ");
$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 scheme1 (code varchar(100),entrydate date,amcno varchar(100),chno varchar(100),brcode varchar(20),rem varchar(250))";
mysqli_query($db,$sql);
 $re=mysqli_query($db,"select * from scheme1 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 scheme1 values('$code','$entrydate','$cpno','$chno','$brcode','$rem')";
mysqli_query($db,$sql);
print "Record has been saved";
include("scheme1.php");
}// end save

if($del)
{
$rr2=mysqli_query($db,"select * from 1ltrans 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 scheme1 where chno='$chno'")or die('err'.mysqli_error());
 print "<script>document.location='scheme1.php'</script>";

}




?>
<script>
var conf=[];
var cust=[];
var addr=[];
function getconfig()
{
 document.f1.config.value=conf[document.f1.chno.selectedIndex];
 document.f1.cust.value=cust[document.f1.chno.selectedIndex];
 document.f1.addr.value=addr[document.f1.chno.selectedIndex];
}
function valid()
{
 if(document.f1.amt.value<=0 || isNaN(document.f1.amt.value))
 {
  alert("Invalid Amount");
  document.f1.amt.focus();
   return false;
 }
 if(document.f1.chno.value.length<=0 ||document.f1.chno.value=='undefined')
 {
  alert("Invalid  Chasis");
  document.f1.chno.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='f1' action='<?print $PHP_SELF?>' onSubmit="return valid()"> 
<table>
<th colspan=4>FREE SERVICE SCHEME 1 lakh FOR CUSTOMERS</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>Chasis No </td><td><input type=text name='chno'>
</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>Entry date <th>Chasis";
print "<th>Customer<th>Address<th>Address2<th>Invno<th>Date";
$re=mysqli_query($db,"select * from scheme1 order by entrydate,chno");
while($row=mysqli_fetch_array($re))
{
$re23=mysqli_query($db,"select cust,invno,invdate from psndb1011.veh_invoice where chasis='$row[chno]' and brcode='$brcode';");

$re24=mysqli_query($db,"select cust,invno,invdate from psndb1112.veh_invoice where chasis='$row[chno]' and brcode='$brcode';");


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

$row12=mysqli_fetch_array($re23);
}
else if (mysqli_num_rows($re24)>0)
{

$row12=mysqli_fetch_array($re24);
}
else
{
$re22=mysqli_query($db,"select cust,invno,invdate from psndb0910.veh_invoice where chasis='$row[chno]' and brcode='$brcode';");
$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);

$sq="select to_days(curdate())-to_days('$row12[2]')";
$dtre=mysqli_query($db,$sq)or die(mysqli_error());
$dt=mysqli_fetch_row($dtre);

//$curdt=date("Y-m-d");
//$dt=$curdt-$row12[2];
//print $dt[0];

if($dt[0]<=365)
{

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

 print "<tr bgcolor='white'><td>".$i++."</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>";
if($level>=4)
{
print "<td align=right><input type=submit value='Del' name='del'</td></form>";
}  
}
}
?>

