<?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/console_shell.php';require_login();$pageLink='reports/attendance/daily-misspunch-log.php';require_acl('can_view',$pageLink);require_once __DIR__.'/daily-misspunch-log-common.php';date_default_timezone_set('Asia/Kolkata');
$run=isset($_GET['run']);$errors=[];$rows=null;try{$options=dml_options($con);$filters=dml_filters($_GET);if($run){$errors=dml_validate($filters,$options);if(!$errors)$rows=dml_report($con,$filters);}$setting=dal_table_exists($con,'jos_app_usersetting')?(dal_rows($con,'SELECT store_name FROM jos_app_usersetting LIMIT 1')[0]??[]):[];$company=$setting['store_name']??APP_NAME;}catch(Throwable $e){$options=$options??['payrolls'=>[],'departments'=>[],'categories'=>[],'branches'=>[],'shifts'=>[]];$filters=dml_filters($_GET);$errors[]='Unable to generate the Daily Misspunch Log.';$company=APP_NAME;}
$more=$filters['department']||$filters['category']||$filters['branch']||$filters['shift'];$input='mt-1 w-full rounded-lg border border-slate-200 bg-white px-3 py-2.5 text-sm outline-none focus:border-brand-500 focus:ring-2 focus:ring-brand-100';$sel=fn($a,$b)=>$a==$b?'selected':'';console_header('Daily Misspunch Log',$pageLink);?>
<div class="mb-6"><div class="mb-2 flex items-center gap-2 text-xs font-semibold text-slate-500"><span>Reports</span><i class="fa-solid fa-chevron-right text-[9px]"></i><span>Attendance Report</span><i class="fa-solid fa-chevron-right text-[9px]"></i><span class="text-brand-600">Daily Misspunch Log</span></div><h1 class="text-2xl font-bold text-slate-900">Daily Misspunch Log</h1><p class="mt-1 text-sm text-slate-500">Employees having an odd number of attendance punches in the selected date range.</p></div>
<?php foreach($errors as$e):?><div class="mb-4 rounded-xl border border-red-200 bg-red-50 px-4 py-3 text-sm font-semibold text-red-700"><?=h($e)?></div><?php endforeach;?>
<form method="get" id="dml-form" class="no-print mb-6 rounded-2xl border border-slate-200 bg-white p-5 shadow-sm"><div class="grid gap-4 md:grid-cols-2 xl:grid-cols-4"><label class="text-sm font-bold">From Date<input required type="date" name="filter_fdate" value="<?=h($filters['from'])?>" class="<?=$input?>"></label><label class="text-sm font-bold">To Date<input required type="date" name="filter_tdate" value="<?=h($filters['to'])?>" class="<?=$input?>"></label><label class="text-sm font-bold">Payroll Location<select required name="filter_payrolllocation" class="<?=$input?>"><option value="">Select Payroll Location</option><?php foreach($options['payrolls']as$o):?><option value="<?=$o['id']?>" <?=$sel($filters['payroll'],$o['id'])?>><?=h($o['name'])?></option><?php endforeach;?></select></label></div>
<div class="mt-4 flex flex-wrap items-center justify-between gap-3"><button type="button" id="dml-more" class="rounded-lg border border-slate-200 px-4 py-2.5 text-sm font-bold"><i class="fa-solid fa-sliders mr-2"></i><span><?=$more?'Hide Additional Filters':'Use Additional Filters'?></span></button><div class="flex flex-wrap gap-2"><button name="run" value="1" class="rounded-lg bg-red-500 px-5 py-2.5 text-sm font-bold text-white"><i class="fa-solid fa-filter mr-2"></i>Go</button><button name="run" value="1" formaction="/reports/attendance/daily-misspunch-log-print.php" formtarget="_blank" class="rounded-lg bg-red-500 px-5 py-2.5 text-sm font-bold text-white"><i class="fa-solid fa-print mr-2"></i>Print</button><button name="run" value="1" formaction="/reports/attendance/daily-misspunch-log-export.php" class="rounded-lg bg-emerald-600 px-5 py-2.5 text-sm font-bold text-white"><i class="fa-solid fa-file-excel mr-2"></i>Export to XLS</button></div></div>
<div id="dml-extra" class="mt-4 <?=$more?'':'hidden'?> border-t border-slate-100 pt-4"><div class="grid gap-4 md:grid-cols-2 xl:grid-cols-4"><label class="text-sm font-bold">Department<select name="filter_department" class="<?=$input?>"><option value="0">Select department</option><?php foreach($options['departments']as$o):?><option value="<?=$o['id']?>" <?=$sel($filters['department'],$o['id'])?>><?=h($o['name'])?></option><?php endforeach;?></select></label><label class="text-sm font-bold">Category<select name="filt_cat" class="<?=$input?>"><option value="0">Select category</option><?php foreach($options['categories']as$o):?><option value="<?=$o['id']?>" <?=$sel($filters['category'],$o['id'])?>><?=h($o['name'])?></option><?php endforeach;?></select></label><label class="text-sm font-bold">Branch Location<select name="filter_orglocation" class="<?=$input?>"><option value="0">-All-</option><?php foreach($options['branches']as$o):?><option value="<?=$o['id']?>" <?=$sel($filters['branch'],$o['id'])?>><?=h($o['name'])?></option><?php endforeach;?></select></label><label class="text-sm font-bold">Select Shift<select name="filter_shift" class="<?=$input?>"><option value="0">-All-</option><?php foreach($options['shifts']as$o):?><option value="<?=$o['id']?>" <?=$sel($filters['shift'],$o['id'])?>><?=h($o['name'])?></option><?php endforeach;?></select></label></div></div></form>
<?php if($rows!==null):?><section class="overflow-hidden rounded-2xl border border-slate-200 bg-white shadow-sm"><div class="p-5 text-center"><b><?=h((string)$company)?></b><h2 class="text-lg font-bold">Daily Misspunch Log</h2><div class="text-sm font-semibold text-slate-500"><?=h(date('d-m-Y',strtotime($filters['from'])))?> to <?=h(date('d-m-Y',strtotime($filters['to'])))?></div></div><?php dml_render($rows);?></section><?php endif;?>
<script>const b=document.getElementById('dml-more'),x=document.getElementById('dml-extra'),f=document.getElementById('dml-form');b?.addEventListener('click',()=>{const o=x.classList.contains('hidden');x.classList.toggle('hidden');b.querySelector('span').textContent=o?'Hide Additional Filters':'Use Additional Filters';});f?.querySelectorAll('input,select').forEach(el=>el.addEventListener('change',()=>document.querySelector('section')?.classList.add('hidden')));</script><?php console_footer();
