« Back to History
rapier_attendance_report.php
|
20260721_154033.php
Initial Bulk Import
Copy Code
<?php // Rapier Karigar Attendance Report $__sess_dir = __DIR__ . '/tmp_sessions'; if (!is_dir($__sess_dir)) { @mkdir($__sess_dir, 0775, true); } @ini_set('session.save_path', $__sess_dir); if (session_status() !== PHP_SESSION_ACTIVE) { session_start(); } $u = $_SESSION['user'] ?? null; if (!$u) { header('Location: /erp/public/login.php?next=' . rawurlencode($_SERVER['REQUEST_URI'])); exit; } require_once __DIR__ . '/core/db.php'; $pdo = $GLOBALS['pdo'] ?? null; $company_id = (int)($u['company_id'] ?? 0); // employee_id resolve (same as dashboard) $employee_id = (int)($u['employee_id'] ?? 0); if ($employee_id === 0 && (int)($u['id'] ?? 0) > 0 && $pdo) { $st = $pdo->prepare("SELECT employee_id FROM users WHERE id = ? LIMIT 1"); $st->execute([(int)$u['id']]); $employee_id = (int)$st->fetchColumn(); } if ($employee_id === 0) { $employee_id = (int)($u['id'] ?? 0); } // Month filter (default: current month) $sel_month = $_GET['month'] ?? date('Y-m'); if (!preg_match('/^\d{4}-\d{2}$/', $sel_month)) { $sel_month = date('Y-m'); } $from = $sel_month . '-01'; $to = date('Y-m-t', strtotime($from)); // Fetch attendance grouped by date $stmt = $pdo->prepare(" SELECT date, shift, COUNT(machine_id) AS machine_count, GROUP_CONCAT(m.code ORDER BY m.code SEPARATOR ', ') AS machine_names FROM rapier_machine_shift_log rsl LEFT JOIN machines m ON m.id = rsl.machine_id WHERE rsl.company_id = :cid AND rsl.employee_id = :eid AND rsl.date BETWEEN :from AND :to GROUP BY rsl.date, rsl.shift ORDER BY rsl.date DESC "); $stmt->execute([ ':cid' => $company_id, ':eid' => $employee_id, ':from' => $from, ':to' => $to, ]); $rows = $stmt->fetchAll(PDO::FETCH_ASSOC); // Summary $total_days = count(array_unique(array_column($rows, 'date'))); $total_shifts = count($rows); function fmt_machine_code(string $code): string { if (preg_match('/\d+/', $code, $m)) { return 'R-' . str_pad($m[0], 2, '0', STR_PAD_LEFT); } return strtoupper($code); } function fmt_machines(string $names): string { $parts = array_map('trim', explode(',', $names)); return implode(', ', array_map('fmt_machine_code', $parts)); } // Month list for filter (last 6 months) $month_opts = []; for ($i = 0; $i < 6; $i++) { $month_opts[] = date('Y-m', strtotime("-$i months")); } require_once __DIR__ . '/partials/header.php'; ?> <div class="container-fluid mt-3" style="max-width:700px;"> <!-- Header --> <div class="card mb-3 border-0 shadow-sm"> <div class="card-body d-flex justify-content-between align-items-center py-3"> <div> <h5 class="mb-1 fw-bold">Attendance Report</h5> <div class="text-muted small"><?= htmlspecialchars($u['name'] ?? '') ?></div> </div> <a href="rapier_karigar_dashboard.php" class="btn btn-sm btn-outline-secondary">← Back</a> </div> </div> <!-- Month Filter --> <form method="get" class="mb-3 d-flex gap-2 align-items-center"> <select name="month" class="form-select form-select-sm" style="width:auto;" onchange="this.form.submit()"> <?php foreach ($month_opts as $mo): ?> <option value="<?= $mo ?>" <?= $mo === $sel_month ? 'selected' : '' ?>> <?= date('F Y', strtotime($mo . '-01')) ?> </option> <?php endforeach; ?> </select> </form> <!-- Summary badges --> <div class="d-flex gap-3 mb-3"> <div class="bg-success text-white rounded-3 px-4 py-3 text-center"> <div class="fs-4 fw-bold"><?= $total_days ?></div> <div class="small">Present Days</div> </div> <div class="bg-primary text-white rounded-3 px-4 py-3 text-center"> <div class="fs-4 fw-bold"><?= $total_shifts ?></div> <div class="small">Total Shifts</div> </div> </div> <!-- Attendance Table --> <div class="card border-0 shadow-sm"> <table class="table table-hover mb-0 align-middle"> <thead class="table-light"> <tr> <th class="ps-3">Date</th> <th>Shift</th> <th>Status</th> <th>Machines</th> <th class="text-center">Count</th> </tr> </thead> <tbody> <?php if (empty($rows)): ?> <tr> <td colspan="5" class="text-center py-4 text-muted"> Is mahine koi attendance nahi mili. </td> </tr> <?php else: foreach ($rows as $r): ?> <tr> <td class="ps-3 fw-bold"> <?= date('d M Y', strtotime($r['date'])) ?> </td> <td> <span class="badge <?= $r['shift'] === 'day' ? 'bg-warning text-dark' : 'bg-dark' ?> px-2"> <?= strtoupper($r['shift']) ?> </span> </td> <td> <span class="badge bg-success px-3">P</span> </td> <td class="small text-muted"> <?= htmlspecialchars(fmt_machines($r['machine_names'] ?? '')) ?> </td> <td class="text-center fw-bold"> <?= (int)$r['machine_count'] ?> </td> </tr> <?php endforeach; endif; ?> </tbody> </table> </div> </div> <?php require_once __DIR__ . '/partials/footer.php'; ?>