« Back to History
production_machine_performance.php
|
20260721_154033.php
Initial Bulk Import
Copy Code
<?php // ============================================================ // File: /erp/production_machine_performance.php // Purpose: Machine Performance Report - per date total meter per machine (with quality label) // ============================================================ error_reporting(E_ALL); ini_set('display_errors', 1); require_once __DIR__ . '/modules/auth/page_acl.php'; $ctx = page_require_access('production_performance'); $u = $ctx['user'] ?? null; $pdo = $ctx['pdo'] ?? null; $company_id = (int)($ctx['company_id'] ?? 0); if (!$pdo) { if (file_exists(__DIR__ . '/core/db.php')) require_once __DIR__ . '/core/db.php'; $pdo = $GLOBALS['pdo'] ?? null; } if (!function_exists('h')) { function h($s){ return htmlspecialchars((string)$s, ENT_QUOTES, 'UTF-8'); } } // ---- Filters ---- $filter_type = $_GET['filter'] ?? 'this_month'; $from_date = $_GET['from'] ?? null; $to_date = $_GET['to'] ?? null; $month = $_GET['month'] ?? null; $tz = new DateTimeZone('+05:30'); $today = new DateTime('now', $tz); switch ($filter_type) { case 'select_month': $month = $month ?: $today->format('Y-m'); $from_date = (new DateTime($month . '-01', $tz))->format('Y-m-d'); $to_date = (new DateTime($from_date, $tz))->modify('last day of this month')->format('Y-m-d'); break; case 'period': if (!$from_date || !$to_date) { $to_date = $today->format('Y-m-d'); $from_date = (new DateTime($to_date, $tz))->modify('-6 days')->format('Y-m-d'); } break; default: $from_date = (new DateTime($today->format('Y-m-01'), $tz))->format('Y-m-d'); $to_date = (new DateTime($from_date, $tz))->modify('last day of this month')->format('Y-m-d'); break; } // ---- Fetch production_entry rows ---- $sql = "SELECT id, entry_date, lines_json, machine_id AS entry_machine_id, quality_id AS entry_quality_id FROM production_entry WHERE company_id = :cid AND entry_date BETWEEN :from_date AND :to_date ORDER BY entry_date ASC, id ASC"; $stmt = $pdo->prepare($sql); $stmt->execute([ ':cid' => $company_id, ':from_date' => $from_date, ':to_date' => $to_date ]); $rawRows = $stmt->fetchAll(PDO::FETCH_ASSOC); // ---- Fetch qualities map ---- $qualities = []; try { $qst = $pdo->prepare("SELECT id, quality_name FROM qualities WHERE company_id = :cid"); $qst->execute([':cid' => $company_id]); while ($qr = $qst->fetch(PDO::FETCH_ASSOC)) { $qualities[(int)$qr['id']] = $qr['quality_name']; } } catch (Exception $e) {} // ---- Extract data ---- $dates_map = []; $machines_data = []; // data[date][machine_label] = total meter $machine_labels = []; // unique machine labels with numeric id function make_machine_label($machine_id, $quality_id, $qualities_map) { $qname = ''; if (!empty($quality_id)) { if (isset($qualities_map[$quality_id])) $qname = $qualities_map[$quality_id]; else $qname = $quality_id; } return $machine_id . "\n(" . $qname . ")"; } foreach ($rawRows as $r) { $d = $r['entry_date']; $dates_map[$d] = $d; $entry_machine = (int)($r['entry_machine_id'] ?? 0); $entry_quality = (int)($r['entry_quality_id'] ?? 0); $json = $r['lines_json'] ?? '[]'; $arr = json_decode($json, true); if (!is_array($arr)) continue; foreach ($arr as $it) { $machine_no = (int)($it['machine_no'] ?? $it['machine'] ?? $it['machine_id'] ?? $entry_machine); if ($machine_no <= 0) continue; $meter = isset($it['meter']) ? (float)$it['meter'] : (isset($it['meters']) ? (float)$it['meters'] : 0); $quality_id = (int)($it['quality_id'] ?? $entry_quality); $label = make_machine_label($machine_no, $quality_id, $qualities); $machines_data[$d][$label] = ($machines_data[$d][$label] ?? 0) + $meter; $machine_labels[$label] = $machine_no; } } // ---- Sort machines by numeric part ---- asort($machine_labels, SORT_NUMERIC); $machines = array_keys($machine_labels); $dates = array_values($dates_map); sort($dates, SORT_STRING); // ---- Compute avg per machine ---- $machine_avg = []; foreach ($machines as $label) { $vals = []; foreach ($dates as $d) { if (isset($machines_data[$d][$label])) $vals[] = $machines_data[$d][$label]; } $machine_avg[$label] = count($vals) ? round(array_sum($vals)/count($vals), 2) : ''; } // ---- CSV export ---- if (isset($_GET['export']) && $_GET['export'] === 'csv') { header('Content-Type: text/csv; charset=utf-8'); header('Content-Disposition: attachment; filename=machine_performance_' . $from_date . '_' . $to_date . '.csv'); $out = fopen('php://output', 'w'); $head = array_merge(['Date'], $machines); fputcsv($out, $head); foreach ($dates as $d) { $row = [$d]; foreach ($machines as $label) { $val = $machines_data[$d][$label] ?? ''; if ($val !== '') $val = (fmod($val,1)==0)?(int)$val:$val; $row[] = $val; } fputcsv($out, $row); } fclose($out); exit; } require_once __DIR__ . '/partials/header.php'; ?> <div style="padding:6px;background:#fff;border-radius:6px;box-shadow:0 0 6px rgba(0,0,0,0.05);font-family:Arial,Helvetica,sans-serif;"> <div style="display:flex;justify-content:space-between;align-items:center;margin-bottom:6px;"> <div> <h3 style="margin:0 0 4px 0;font-size:15px;">Machine Performance Report</h3> <div style="font-size:12px;color:#333;">Period: <strong><?= h($from_date) ?></strong> → <strong><?= h($to_date) ?></strong></div> </div> <div style="display:flex;gap:8px;align-items:center;"> <form method="get" style="display:inline-flex;gap:6px;align-items:center;"> <select name="filter" onchange="this.form.submit()" style="padding:4px;font-size:13px;"> <option value="this_month" <?= $filter_type==='this_month'?'selected':'' ?>>This month</option> <option value="select_month" <?= $filter_type==='select_month'?'selected':'' ?>>Select month</option> <option value="period" <?= $filter_type==='period'?'selected':'' ?>>Custom period</option> </select> <input type="month" name="month" value="<?= h($month) ?>" style="padding:4px;"> <input type="date" name="from" value="<?= h($from_date) ?>" style="padding:4px;"> <input type="date" name="to" value="<?= h($to_date) ?>" style="padding:4px;"> <button type="submit" style="padding:6px 10px;">Apply</button> </form> <a href="?<?= http_build_query(array_merge($_GET, ['export'=>'csv'])) ?>" style="padding:6px 10px;background:#eee;border-radius:4px;text-decoration:none;color:#111;">Export CSV</a> </div> </div> <div style="overflow:auto;border:1px solid #e6e6e6;padding:6px;"> <table style="border-collapse:collapse;width:100%;font-size:11px;"> <thead> <tr> <th style="border:1px solid #bbb;padding:4px;background:#dfe6ee;width:70px;">Date</th> <?php foreach ($machines as $label): ?> <th style="border:1px solid #bbb;padding:4px;background:#000;color:#fff;text-align:center;white-space:pre-line;"><?= h($label) ?></th> <?php endforeach; ?> </tr> <tr> <th style="border:1px solid #333;padding:3px;background:#000;color:#fff;">AVG</th> <?php foreach ($machines as $label): ?> <th style="border:1px solid #333;padding:3px;background:#000;color:#fff;text-align:center;"><?= h($machine_avg[$label]) ?></th> <?php endforeach; ?> </tr> </thead> <tbody> <?php if (empty($dates)): ?> <tr><td colspan="<?= 1 + count($machines) ?>" style="padding:8px;text-align:center;">No data found for this period.</td></tr> <?php else: ?> <?php foreach ($dates as $d): ?> <tr> <td style="border:1px solid #ddd;padding:4px;"><?= h($d) ?></td> <?php foreach ($machines as $label): $val = $machines_data[$d][$label] ?? ''; $valStr = ($val==='')?'':((fmod($val,1)==0)?(int)$val:$val); ?> <td style="border:1px solid #eee;padding:3px;text-align:center;"><?= h($valStr) ?></td> <?php endforeach; ?> </tr> <?php endforeach; ?> <?php endif; ?> </tbody> </table> </div> </div> <?php require_once __DIR__ . '/partials/footer.php'; ?>