<?php
declare(strict_types=1);ini_set('display_errors','0');header('Cache-Control: private, no-store, max-age=0');require_once __DIR__.'/../../includes/console_shell.php';require_login();require_acl('can_view','reports/attendance/monthly-first-last-log-summary.php');require_once __DIR__.'/monthly-first-last-log-summary-common.php';
try{$o=mfl_options($con);$f=mfl_filters($_GET,true);$e=mfl_validate($f,$o);if($e){http_response_code(400);exit('Invalid report filters.');}$r=mfl_report($con,$f);}catch(Throwable $x){http_response_code(500);exit('Unable to generate export.');}
function mfl_xls($v):string{$v=(string)$v;if(preg_match('/^[=+\-@]/',$v))$v="'".$v;return h($v);}
header('Content-Type: application/vnd.ms-excel; charset=UTF-8');header('Content-Disposition: attachment; filename="monthly-first-last-log-summary-'.$f['year'].'-'.sprintf('%02d',$f['month']).'.xls"');header('X-Content-Type-Options: nosniff');echo "\xEF\xBB\xBF";?><html xmlns:x="urn:schemas-microsoft-com:office:excel"><head><meta charset="utf-8"></head><body><h2>Employee Monthly First &amp; Last Log Summary - <?=mfl_xls(date('F Y',strtotime($r['from'])))?></h2><table border="1"><thead><tr><th>Employee</th><th>Employee Code</th><th>Detail</th><?php foreach($r['dates']as$d):?><th><?=mfl_xls(date('d-m-Y',strtotime($d)))?></th><?php endforeach;?></tr></thead><tbody><?php foreach($r['rows']as$row):foreach([['Shift','shift'],['Status','status'],['First Log','first'],['Last Log','last'],['Total Hrs','worked'],['EX/LS','extra'],['Late Mark','late']]as$def):?><tr><td><?=mfl_xls($row['employee']['name'])?></td><td><?=mfl_xls($row['employee']['id'])?></td><td><?=mfl_xls($def[0])?></td><?php foreach($r['dates']as$d):$v=$row['days'][$d][$def[1]];if($def[1]==='worked')$v=mfl_hhmm((int)$v);elseif($def[1]==='extra')$v=$v?'EX '.mfl_hhmm((int)$v):($row['days'][$d]['loss']?'LS '.mfl_hhmm($row['days'][$d]['loss']):'-');elseif($def[1]==='late')$v=$v?mfl_hhmm((int)$v):'-';?><td><?=mfl_xls($v?:'-')?></td><?php endforeach;?></tr><?php endforeach;endforeach;?></tbody></table></body></html>
