<?php
include('../include/session.php');

date_default_timezone_set('Africa/Gaborone');
$entry_date = date("Y-m-d H:i:s"); 

$m = date("m");	
$check_date = date("Y-m-31");

if ($m=='02') {
$end = date("Y-m-28");
}

if ( ($m=='04') OR ($m=='06')  OR ($m=='09')  OR ($m=='11') ) {
$check_date = date("Y-m-30");
}
$check_date_display = date("M-Y", strtotime($check_date));

$table_name2 = $_SESSION['db_prefix']."_vehicle";
$road_license_query = "SELECT registration_no, make, model, department, road_license_date, clock_reading FROM $table_name2 WHERE road_license_date < '".$check_date."' AND road_license_date <> '0000-00-00'"; 
$road_license_result = mySQL_query($road_license_query);
$road_license_num = mysql_num_rows($road_license_result);

$table_name2 = $_SESSION['db_prefix']."_vehicle";
$ba_license_query = "SELECT registration_no, make, model, department, ba_license_date, clock_reading FROM $table_name2 WHERE ba_license_date < '".$check_date."' AND ba_license_date <> '0000-00-00'"; 
$ba_license_result = mySQL_query($ba_license_query);
$ba_license_num = mysql_num_rows($ba_license_result);

$table_name2 = $_SESSION['db_prefix']."_vehicle";
$insurance_query = "SELECT registration_no, make, model, department, renewal_date, insurance, clock_reading FROM $table_name2 WHERE renewal_date < '".$check_date."' AND renewal_date <> '0000-00-00'"; 
$insurance_result = mySQL_query($insurance_query);
$insurance_num = mysql_num_rows($insurance_result);


?>
<script language="javascript" src="../jscript/calendar.js"></script>
</head>
<?php
$road_license_content='';
$i=0;
while ($i<$road_license_num) {
	$a0 = mysql_result($road_license_result, $i, "registration_no");
	$a1 = mysql_result($road_license_result, $i, "make")." ".mysql_result($road_license_result, $i, "model");
	$a2 = mysql_result($road_license_result, $i, "department");
	$a3 = date("M-Y", strtotime(mysql_result($road_license_result, $i, "road_license_date")));
	

$road_license_content = $road_license_content.'<tr><td>'.$a0.'</td><td>'.$a1.'</td><td>'.$a2.'</td><td align=center>'.$a3.'</td></tr>';
$i++;
}
?>

<?php
$ba_license_content=''; 
$i=0;
while ($i<$ba_license_num) {

	$b0 = mysql_result($ba_license_result, $i, "registration_no");
	$b1 = mysql_result($ba_license_result, $i, "make")." ".mysql_result($ba_license_result, $i, "model");
	$b2 = mysql_result($ba_license_result, $i, "department");
	$b3 = date("M-Y", strtotime(mysql_result($ba_license_result, $i, "ba_license_date")));
	
	
$ba_license_content = $ba_license_content.'<tr><td>'.$b0.'</td><td>'.$b1.'</td><td>'.$b2.'</td><td align=center>'.$b3.'</td></tr>';

  $i++;
}
?>
<?php 
$insurance_content = '';
$i=0;
while ($i<$insurance_num) {

	$c0 = mysql_result($insurance_result, $i, "registration_no");
	$c1 = mysql_result($insurance_result, $i, "make")." ".mysql_result($insurance_result, $i, "model");
	$c2 = mysql_result($insurance_result, $i, "department");
	$c3 = date("d-m-Y", strtotime(mysql_result($insurance_result, $i, "renewal_date")));
	$c5 = mysql_result($insurance_result, $i, "insurance");
	
	if ($c5=='') {$c5='--';}

$insurance_content = $insurance_content.'<tr><td>'.$c0.'</td><td>'.$c1.'</td><td>'.$c2.'</td><td align=center>'.$c3.'</td><td>'.$c5.'</td></tr>';

  $i++;
}
?>

<?php

			$report_text = 'Road licenses for the following vehicles need to be renewed before the end of: '.$check_date_display.'.<br><br><table border=1 style=border-collapse:collapse; font-size:10px;><tr><td width=125>Registration no</td><td width=150>Make</td><td width=125>Department</td><td width=125>Renew before</td></tr>'.$road_license_content.'</table><br><br>BA licenses for the following vehicles need to be renewed before the end of: '.$check_date_display.'.<br><br><table border=1 style=border-collapse:collapse; font-size:10px;><tr><td width=125>Registration no</td><td width=150>Make</td><td width=125>Department</td><td width=125>Renew before</td></tr>'.$ba_license_content.'</table><br><br>Vehicle insurances for the following vehicles need to be renewed before the end of: '.$check_date_display.'.<br><br><table border=1 style=border-collapse:collapse; font-size:10px;><tr><td width=125>Registration no</td><td width=150>Make</td><td width=125>Department</td><td width=125>Renew before</td><td width=125>Insured with</td></tr>'.$insurance_content.'</table><br>';

$table_name = $_SESSION['db_prefix']."_saved_transport_reports";
$report_query = "INSERT INTO $table_name (department, saved_report_name, saved_report_date, saved_report_text, user_name ) VALUES ('Transport', 'License & Insurance expiry', '".$entry_date."', '".$report_text."', '".$_SESSION['username']."' ) ";
execsql($report_query);	
			
?>

<script language="javascript" type="text/javascript">
       window.location="report_license_renewal.php";
</script>