<?php
declare(strict_types=1);
ini_set('display_errors','0');
header('Cache-Control: private, no-store, no-cache, must-revalidate, max-age=0');
require_once __DIR__.'/../../includes/auth.php';
require_once __DIR__.'/../../includes/aclhelper.php';
require_login();
require_acl('can_view','reports/attendance/daily-attendance-log.php');
require_once __DIR__.'/daily-attendance-log-common.php';
date_default_timezone_set('Asia/Kolkata');
if($_SERVER['REQUEST_METHOD']!=='POST'){http_response_code(405);exit('Method not allowed.');}
$enabled=false;
if(dal_table_exists($con,'jos_app_exporttoexcel_config')){$x=dal_rows($con,'SELECT status FROM jos_app_exporttoexcel_config WHERE layout=? LIMIT 1','s',['Daily Attendance Log']);$enabled=!empty($x[0]['status']);}
if(!$enabled){http_response_code(404);exit('Export is not enabled.');}
$options=dal_load_options($con);$filters=dal_filters($_POST);$errors=dal_validate($filters,$options);
if($errors){http_response_code(422);exit('Invalid report filters.');}
$report=dal_report($con,$filters);
header('Content-Type: application/vnd.ms-excel; charset=UTF-8');
header('Content-Disposition: attachment; filename="Daily Attendance Log '.date('d-m-Y',strtotime($filters['date'])).'.xls"');
echo "\xEF\xBB\xBF";
$safe=static function($value):string{$v=(string)$value;if(preg_match('/^[=+\\-@]/',$v))$v="'".$v;return h($v);};
?><table border="1"><tr><th colspan="8">Daily Attendance Log - <?=$safe(date('d-m-Y',strtotime($filters['date'])))?></th></tr><tr><th>Sr No</th><th>Name</th><th>Shift / Status</th><th>Logs</th><th>Gate Pass Logs</th><th>Total Hrs</th><th>Extra / Loss Time</th><th>Late Mark</th></tr><?php foreach($report['detail'] as $i=>$row):?><tr><td><?=$i+1?></td><td><?=$safe($row['employee']['name'])?></td><td><?=$safe($row['status'])?></td><td><?php foreach($row['logs'] as $log)echo $safe(($log['location']??'').' '.implode(' - ',$log['pair'])).'<br>';?></td><td><?php foreach($row['gate_logs'] as $log)echo $safe(($log['location']??'').' '.implode(' - ',$log['pair'])).'<br>';?></td><td><?=$safe(dal_hhmm($row['worked']))?></td><td></td><td></td></tr><?php endforeach;?><tr><th colspan="8">Absent / Other Status</th></tr><?php foreach($report['absent'] as $i=>$row):?><tr><td><?=$i+1?></td><td><?=$safe($row['employee']['name'])?></td><td><?=$safe($row['status'])?></td><td colspan="5"></td></tr><?php endforeach;?><tr><th>Log In</th><td><?=$report['summary']['login']?></td><th>Absent</th><td><?=$report['summary']['absent']?></td><th>Weekly Off</th><td><?=$report['summary']['weeklyoff']?></td><th>Holiday / Total</th><td><?=$report['summary']['holiday']?> / <?=$report['summary']['total']?></td></tr></table>
