<?php

// default config setting
$boolean_ssl_lock = true ;

include '../connect/cms-config.php' ;
include '../requires/function.php' ;

// defualt parameter
$after_14  = date('Y-m-d', strtotime('+14 days')) ;

$check = [
	'passport' => [
		'title' => 'Passport',
		'no'    => 'staff_passportno',
		'date'  => 'staff_passportexpired'
	],
	'permit'   => [
		'title' => 'Permit',
		'no'    => 'staff_permitno',
		'date'  => 'staff_permit_end'
	]
] ;

// password expired
// permit expired
foreach ( $check as $k => $v ){

    // previous date
    $staff_q   = $mysqli->query("SELECT staff_name, staff_idno, ".$v['no'].", ".$v['date']." FROM staff
                                 WHERE ( staff_date_resigned IS NULL || staff_date_resigned = '0000-00-00' || staff_date_resigned >= '".TODAYDATE."' ) AND deleted_at IS NULL AND ".$v['no']." != '' AND ".$v['date']." != '0000-00-00' AND ".$v['date']." IS NOT NULL AND ".$v['date']." <= '".$after_14."'") ;
    if ( $staff_q->num_rows > 0 ){

    	$html  = '' ;
		$count = 0 ;

    	while ( $row = $staff_q->fetch_assoc() ){

    		$staff_idno = ucwords( dataFilter($row['staff_idno']) ) ;

    		$count++ ;
			$html .= '
			<tr>
				<td>'.$count.'</td>
				<td>'.$staff_idno.'</td>
				<td>'.dataFilter($row['staff_name']).'</td>
				<td>'.dataFilter($row[$v['no']]).'</td>
				<td>'.dataFilter($row[$v['date']]).'</td>
			</tr>' ;

    	}

    	$html = '
		<table border="0" width="800px" style="font-size:13px;">
			<thead>
				<tr>
					<th style="text-align:left;">No.</th>
					<th style="text-align:left;">ID No.</th>
					<th style="text-align:left;">Staff</th>
					<th style="text-align:left;">'.$v['title'].' No.</th>
					<th style="text-align:left;">Expired Date</th>
				</tr>
			</thead>
			<tbody>
				'.$html.'
			</tbody>
		</table>' ;
		
		$title = $v['title'] . ' Expired Warning' ;
		foreach ( $EMAILCRON as $vv ){
			sendEmail( $vv, EMAILNOREPLY, $title, $html ) ;
		}

    }

}

// leave apply waiting approve
$leave_q = $mysqli->query("SELECT a.leave_type, a.leave_from, a.leave_to, a.leave_day, a.leave_reason, b.staff_name, b.staff_idno FROM staff_leave a
						   LEFT JOIN staff b ON ( a.staff_id = b.staff_id )
                           WHERE 
                           	a.leave_status = 'pending' AND a.deleted_at IS NULL AND 
                           	( b.staff_date_resigned IS NULL || b.staff_date_resigned = '0000-00-00' || b.staff_date_resigned >= '".TODAYDATE."' ) AND b.deleted_at IS NULL
                           ORDER BY a.leave_from") ;
if ( $leave_q->num_rows > 0 ){

	$html  = '' ;
	$count = 0 ;

	while ( $row = $leave_q->fetch_assoc() ){

		$staff_idno = ucwords( dataFilter($row['staff_idno']) ) ;

		$count++ ;
		$html .= '
		<tr>
			<td>'.$count.'</td>
			<td>'.$staff_idno.'</td>
			<td>'.dataFilter($row['staff_name']).'</td>
			<td>'.ucwords($row['leave_type']).'</td>
			<td>'.$row['leave_from'].' ~ '.$row['leave_to'].' ( '.$row['leave_day'].' days )</td>
			<td>'.dataFilter($row['leave_reason']).'</td>
		</tr>' ;

	}

	$html = '
	<table border="0" width="800px" style="font-size:13px;">
		<thead>
			<tr>
				<th style="text-align:left;">No.</th>
				<th style="text-align:left;">ID No.</th>
				<th style="text-align:left;">Staff</th>
				<th style="text-align:left;">Type</th>
				<th style="text-align:left;">From ~ To ( Days )</th>
				<th style="text-align:left;">Reason</th>
			</tr>
		</thead>
		<tbody>
			'.$html.'
		</tbody>
	</table>' ;
	
	$title = 'Leave Waiting Approve' ;
	foreach ( $EMAILCRON as $vv ){
		sendEmail( $vv, EMAILNOREPLY, $title, $html ) ;
	}

}

// advance apply waiting approve
$advance_q = $mysqli->query("SELECT a.advance_amount, a.advance_reason, b.staff_name, b.staff_idno FROM staff_advance a
						     LEFT JOIN staff b ON ( a.staff_id = b.staff_id )
                             WHERE 
                             	a.advance_status = 'pending' AND a.deleted_at IS NULL AND
                           		( b.staff_date_resigned IS NULL || b.staff_date_resigned = '0000-00-00' || b.staff_date_resigned >= '".TODAYDATE."' ) AND b.deleted_at IS NULL
                             ORDER BY a.created_at") ;
if ( $advance_q->num_rows > 0 ){

	$html  = '' ;
	$count = 0 ;

	while ( $row = $advance_q->fetch_assoc() ){

		$staff_idno = ucwords( dataFilter($row['staff_idno']) ) ;

		$count++ ;
		$html .= '
		<tr>
			<td>'.$count.'</td>
			<td>'.$staff_idno.'</td>
			<td>'.dataFilter($row['staff_name']).'</td>
			<td>'.ucwords($row['advance_amount']).'</td>
			<td>'.dataFilter($row['advance_reason']).'</td>
		</tr>' ;

	}

	$html = '
	<table border="0" width="800px" style="font-size:13px;">
		<thead>
			<tr>
				<th style="text-align:left;">No.</th>
				<th style="text-align:left;">ID No.</th>
				<th style="text-align:left;">Staff</th>
				<th style="text-align:left;">Amount</th>
				<th style="text-align:left;">Reason</th>
			</tr>
		</thead>
		<tbody>
			'.$html.'
		</tbody>
	</table>' ;
	
	$title = 'Advance Waiting Approve' ;
	foreach ( $EMAILCRON as $vv ){
		sendEmail( $vv, EMAILNOREPLY, $title, $html ) ;
	}

}

?>