<? 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 xwty ");
$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 xwty (code varchar(100),entrydate date,fromdate date,todate date,amcno varchar(100),chno varchar(100),brcode varchar(20),cust varchar(250),add1 varchar(250),dist varchar(250),invno varchar(25),invdt date,exwarby varchar(25))";
mysqli_query($db,$sql);
 $re=mysqli_query($db,"select * from xwty where chno='$chno' and brcode='$brcode'");
  if(mysqli_num_rows($re)>0)
  {
     print "Duplicate entry ". $chno;
     print "<script> alert('Duplicate entry $chno'); history.back(1);  </script>";
     return;
  }


else
{

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

}

if(mysqli_num_rows($re22)<=0)
{
 print "<script> alert('Cannot be saved', No invoices)</script>";
 print "<script>document.location='school.php'</script>";
return;
}

$sql="insert into xwty values('$code','$entrydate','$dfrom','$dto','$cpno','$chno','$brcode','$row1[custname]','$row1[addr1]','$row1[dist]','$row12[invno]','$row12[invdate]','$exwarby')";
mysqli_query($db,$sql);
print "Record has been saved";
include("xwty.php");
}// end save

if($del)
{
$sql="delete from xwty where chno='$chno' and brcode='$brcode' ";
mysqli_query($db,$sql)or die(mysqli_error());

}




?>
<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;
 }

function delfun(chno)
{
 if(confirm('Do you want to delete  '+chno+' ?'))
   document.location='delxwty.php?chno='+chno;
}

 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>EXTENDED WARRANTY SCHEME FOR CUSTOMERS</TH>
<tr><td>Entry Date </td><td>dd <input type=text readonly name='d' size=2 maxlength=2 value='<?print date('d')?>'>
mm <input type=text readonly name='m' size=2 maxlength=2 value='<?print date('m')?>'>
yy <input type=text readonly name='y' size=4 maxlength=4 value='<?print date('Y')?>'>
</td></tr>
<tr><td>From 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>To 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>
<td>Extended Warranty by </td><td><select name='exwarby'>
<option>AMPL</option>
<option>PSN</option>
<option>OTHERS</option> 
</select> </td>

<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>From Date<th>To date<th>Chasis";
print "<th>Customer<th>Add1<th>Dist<th>Invno<th>Invdate<th>By";
$re=mysqli_query($db,"select * from xwty order by entrydate,amcno");
while($row=mysqli_fetch_array($re))
{
 print "\n<form action='$PHP_SELF'> <input type=hidden name='chno' value='$row[5]'><tr><td>";
 print "<tr bgcolor='white'><td>".$i++."</td><td>".datetohtml($row[1])."</td><td>".datetohtml($row[2])."</td><td>".datetohtml($row[3])."</td><td>$row[5]</td><td>$row[cust]</td><td>$row[add1]</td><td>$row[dist]</td><td>$row[invno]</td><td>".datetohtml($row[invdt])."</td><td>$row[exwarby]</td>";
if($level>=4)
{
print "\n<td align=right><input type=submit value='Del' name='del'></td></form>";
}  
}
?>

