« Back to History
karigar_performance_report.php
|
20260722_120325.php
Initial Domain Snapshot
Copy Code
<?php // ============================================================ // File: production_karigar_performance.php // Purpose: Matrix-style Karigar Performance Report // ============================================================ declare(strict_types=1); require_once __DIR__ . '/modules/auth/page_acl.php'; $ctx = page_require_access('production_karigar_performance'); $u = $ctx['user'] ?? null; $company_id = (int)($ctx['company_id'] ?? 0); $pdo = $ctx['pdo'] ?? null; if (!$pdo || !$company_id) { exit('Context initialization failed'); } // ------------------------------------------------------------ // Timezone (MANDATORY) // ------------------------------------------------------------ date_default_timezone_set('Asia/Kolkata'); // +05:30 // ------------------------------------------------------------ // Filters // ------------------------------------------------------------ $filter_type = $_GET['filter'] ?? 'month'; // month | select_month | custom $from = $_GET['from'] ?? ''; $to = $_GET['to'] ?? ''; $month = $_GET['month'] ?? date('Y-m'); $today = new DateTime('now'); if ($filter_type === 'custom' && $from && $to) { $from_date = new DateTime($from); $to_date = new DateTime($to); } elseif ($filter_type === 'select_month' && preg_match('/^\d{4}-\d{2}$/', $month)) { $from_date = new DateTime($month . '-01'); $to_date = (clone $from_date)->modify('last day of this month'); } else { // Default: current month $from_date = new DateTime($today->format('Y-m-01')); $to_date = (clone $from_date)->modify('last day of this month'); } $from_sql = $from_date->format('Y-m-d'); $to_sql = $to_date->format('Y-m-d'); // ------------------------------------------------------------ // Optional qualities lookup (ONLY if table exists) // ------------------------------------------------------------ $quality_map = []; $qualities_exist = false; try { $chk = $pdo->query("SELECT 1 FROM qualities LIMIT 1"); if ($chk) { $qualities_exist = true; $qs = $pdo->prepare("SELECT id, quality_name FROM qualities WHERE company_id = :cid"); $qs->execute([':cid' => $company_id]); foreach ($qs->fetchAll(PDO::FETCH_ASSOC) as $r) { $quality_map[(int)$r['id']] = $r['quality_name']; } } } catch (Throwable $e) { $qualities_exist = false; } // ------------------------------------------------------------ // Karigar master // ------------------------------------------------------------ $karigar_map = []; $ks = $pdo->prepare(" SELECT id, karigar_name FROM loom_karigar_master WHERE company_id = :cid ORDER BY id "); $ks->execute([':cid' => $company_id]); foreach ($ks->fetchAll(PDO::FETCH_ASSOC) as $r) { $karigar_map[(int)$r['id']] = $r['karigar_name']; } // ------------------------------------------------------------ // Fetch production entries (NO aggregation) // ------------------------------------------------------------ $ps = $pdo->prepare(" SELECT id, entry_date, machine_id, quality_id, lines_json FROM production_entry WHERE company_id = :cid AND entry_date BETWEEN :f AND :t ORDER BY entry_date ASC, id ASC "); $ps->execute([ ':cid' => $company_id, ':f' => $from_sql, ':t' => $to_sql, ]); // ------------------------------------------------------------ // Data structures // ------------------------------------------------------------ $data_list = []; // [date][karigar_id][] = ['meter'=>, 'label'=>] $dates = []; // unique ordered dates $karigars = []; // unique karigar ids $karigar_labels = []; // [karigar_id] => ordered unique labels (max 4) // ------------------------------------------------------------ // Helper: extract machine // ------------------------------------------------------------ function extract_machine(array $line, $fallback) { foreach (['machine_no','machine','machine_id','mc','mc_no'] as $k) { if (isset($line[$k]) && $line[$k] !== '') { return (string)$line[$k]; } } return $fallback !== null ? (string)$fallback : ''; } // ------------------------------------------------------------ // Helper: extract quality // ------------------------------------------------------------ function extract_quality(array $line, $fallback) { foreach (['quality_id','quality','quality_code'] as $k) { if (isset($line[$k]) && $line[$k] !== '') { return $line[$k]; } } return $fallback; } // ------------------------------------------------------------ // Decode and transform // ------------------------------------------------------------ while ($row = $ps->fetch(PDO::FETCH_ASSOC)) { $entry_date = $row['entry_date']; if (!isset($data_list[$entry_date])) { $data_list[$entry_date] = []; $dates[] = $entry_date; } $json = json_decode($row['lines_json'], true); if (!is_array($json)) { continue; // ignore invalid JSON } foreach ($json as $line) { if (!is_array($line) || !isset($line['karigar_id'])) { continue; } $karigar_id = (int)$line['karigar_id']; $meter = null; if (isset($line['meter'])) { $meter = $line['meter']; } elseif (isset($line['meters'])) { $meter = $line['meters']; } if ($meter === null) { continue; } $meter = (float)$meter; if (!isset($data_list[$entry_date][$karigar_id])) { $data_list[$entry_date][$karigar_id] = []; } $machine = extract_machine($line, $row['machine_id']); $quality_val = extract_quality($line, $row['quality_id']); $quality_label = ''; if ($quality_val !== null && $quality_val !== '') { if ($qualities_exist && is_numeric($quality_val) && isset($quality_map[(int)$quality_val])) { $quality_label = $quality_map[(int)$quality_val]; } else { $quality_label = (string)$quality_val; } } $label = ''; if ($machine !== '' && $quality_label !== '') { $label = $machine . "\n(" . $quality_label . ")"; } elseif ($machine !== '') { $label = $machine; } elseif ($quality_label !== '') { $label = "(" . $quality_label . ")"; } $data_list[$entry_date][$karigar_id][] = [ 'meter' => $meter, 'label' => $label, ]; if (!in_array($karigar_id, $karigars, true)) { $karigars[] = $karigar_id; } if (!isset($karigar_labels[$karigar_id])) { $karigar_labels[$karigar_id] = []; } if ($label !== '' && !in_array($label, $karigar_labels[$karigar_id], true)) { if (count($karigar_labels[$karigar_id]) < 4) { $karigar_labels[$karigar_id][] = $label; } } } } // ------------------------------------------------------------ // CSV Export // ------------------------------------------------------------ if (isset($_GET['export']) && $_GET['export'] === 'csv') { header('Content-Type: text/csv'); header('Content-Disposition: attachment; filename="production_karigar_performance.csv"'); $out = fopen('php://output', 'w'); $header = ['Date']; foreach ($karigars as $kid) { $labels = $karigar_labels[$kid] ?? []; for ($i = 0; $i < 4; $i++) { $header[] = ($labels[$i] ?? ''); } $header[] = 'AVG'; } fputcsv($out, $header); foreach ($dates as $d) { $row = [(string)(int)date('d', strtotime($d))]; foreach ($karigars as $kid) { $entries = $data_list[$d][$kid] ?? []; $sum = 0.0; $cnt = 0; $by_label = []; foreach ($entries as $e) { $sum += $e['meter']; $cnt++; if ($e['label'] !== '') { if (!isset($by_label[$e['label']])) { $by_label[$e['label']] = 0.0; } $by_label[$e['label']] += $e['meter']; } } $labels = $karigar_labels[$kid] ?? []; for ($i = 0; $i < 4; $i++) { $lbl = $labels[$i] ?? ''; $row[] = $lbl !== '' && isset($by_label[$lbl]) ? $by_label[$lbl] : ''; } $row[] = $cnt > 0 ? round($sum / $cnt, 2) : ''; } fputcsv($out, $row); } fclose($out); exit; } // ------------------------------------------------------------ // UI // ------------------------------------------------------------ require_once __DIR__ . '/partials/header.php'; ?> <div class="card"> <div class="card-header"> <strong>Karigar Performance Report</strong> <a class="btn btn-sm btn-secondary" href="?<?php echo http_build_query(array_merge($_GET, ['export' => 'csv'])); ?>">Export CSV</a> </div> <div class="table-responsive" style="overflow-x:auto;"> <table class="table table-sm table-bordered"> <thead style="position:sticky; top:0; background:#fff; z-index:2;"> <tr> <th rowspan="2">Date</th> <?php foreach ($karigars as $kid): ?> <th colspan="5"> <strong><?php echo htmlspecialchars($karigar_map[$kid] ?? ('Karigar ' . $kid)); ?></strong> </th> <?php endforeach; ?> </tr> <tr> <?php foreach ($karigars as $kid): ?> <?php $labels = $karigar_labels[$kid] ?? []; for ($i = 0; $i < 4; $i++): ?> <th style="font-weight:400;"><?php echo nl2br(htmlspecialchars($labels[$i] ?? '')); ?></th> <?php endfor; ?> <th>AVG</th> <?php endforeach; ?> </tr> </thead> <tbody> <?php foreach ($dates as $d): ?> <tr> <td><?php echo (int)date('d', strtotime($d)); ?></td> <?php foreach ($karigars as $kid): ?> <?php $entries = $data_list[$d][$kid] ?? []; $sum = 0.0; $cnt = 0; $by_label = []; foreach ($entries as $e) { $sum += $e['meter']; $cnt++; if ($e['label'] !== '') { if (!isset($by_label[$e['label']])) { $by_label[$e['label']] = 0.0; } $by_label[$e['label']] += $e['meter']; } } $labels = $karigar_labels[$kid] ?? []; for ($i = 0; $i < 4; $i++): $lbl = $labels[$i] ?? ''; ?> <td style="font-family:monospace;"> <?php echo ($lbl !== '' && isset($by_label[$lbl])) ? $by_label[$lbl] : ''; ?> </td> <?php endfor; ?> <td style="font-family:monospace; font-weight:600;"> <?php echo $cnt > 0 ? round($sum / $cnt, 2) : ''; ?> </td> <?php endforeach; ?> </tr> <?php endforeach; ?> </tbody> </table> </div> </div> <?php require_once __DIR__ . '/partials/footer.php'; ?>