« Back to History
machine_summry.php
|
20260721_154033.php
Initial Bulk Import
Copy Code
<?php /* ============================================================================ File: /erp/machine_summry.php Purpose: Machine Production Summary (per karigar) — horizontal card layout (3 cols) - Reads production_entry.lines_json (karigar_id, meter, date) - Aggregates per karigar - Joins loom_karigar_master for karigar_name - Joins qualities table for quality_name (per production_entry row) - Company-scoped via company_id ============================================================================ */ require_once __DIR__ . '/modules/auth/page_acl.php'; $ctx = page_require_access('machine_summry'); $u = $ctx['user'] ?? null; $company_id = (int)($ctx['company_id'] ?? 0); $pdo = $ctx['pdo'] ?? null; if (!$pdo) { require_once __DIR__ . '/../core/db.php'; if (isset($GLOBALS['pdo'])) $pdo = $GLOBALS['pdo']; } if (!$pdo) die("Database connection not available."); if (!function_exists('h')) { function h($s) { return htmlspecialchars((string)$s, ENT_QUOTES, 'UTF-8'); } } // ---- Input Filters ---- $mc = isset($_GET['mc']) ? (int)$_GET['mc'] : 1; $year = isset($_GET['year']) ? (int)$_GET['year'] : (int)date('Y'); $month = isset($_GET['month']) ? (int)$_GET['month'] : (int)date('m'); $start = isset($_GET['start']) ? (int)$_GET['start'] : 1; $end = isset($_GET['end']) ? (int)$_GET['end'] : 15; $start_date = sprintf('%04d-%02d-%02d', $year, $month, $start); $end_date = sprintf('%04d-%02d-%02d', $year, $month, $end); // ---- Fetch entries (we need lines_json + quality_id per row) ---- $sql = "SELECT id, entry_date, lines_json, quality_id FROM production_entry WHERE company_id = :cid AND machine_id = :mc AND DATE(entry_date) BETWEEN :s AND :e ORDER BY entry_date ASC"; $stmt = $pdo->prepare($sql); $stmt->execute([':cid' => $company_id, ':mc' => $mc, ':s' => $start_date, ':e' => $end_date]); $rows = $stmt->fetchAll(PDO::FETCH_ASSOC); // ---- Aggregate per karigar, keep per-entry quality_id ---- $summary = []; // karigar_id => ['total'=>float, 'entries'=>[['date'=>, 'mtr'=>, 'quality_id'=>]]] $quality_ids_needed = []; foreach ($rows as $r) { $entry_date = substr($r['entry_date'], 0, 10); $q_id = isset($r['quality_id']) ? (int)$r['quality_id'] : 0; $json = json_decode($r['lines_json'], true); if (!is_array($json)) continue; foreach ($json as $line) { $kid = (int)($line['karigar_id'] ?? 0); $mtr = (float)($line['meter'] ?? 0); if (!$kid) continue; if (!isset($summary[$kid])) $summary[$kid] = ['total'=>0.0, 'entries'=>[]]; $summary[$kid]['total'] += $mtr; $summary[$kid]['entries'][] = [ 'date' => $entry_date, 'mtr' => $mtr, 'quality_id' => $q_id ]; if ($q_id) $quality_ids_needed[$q_id] = true; } } // ---- Fetch karigar names ---- $karigar_names = []; if (!empty($summary)) { $kar_ids = array_keys($summary); $placeholders = implode(',', array_fill(0, count($kar_ids), '?')); $sqlK = "SELECT id, karigar_name FROM loom_karigar_master WHERE id IN ($placeholders) AND company_id = ?"; $stK = $pdo->prepare($sqlK); $stK->execute([...$kar_ids, $company_id]); $karigar_names = $stK->fetchAll(PDO::FETCH_KEY_PAIR); } // ---- Fetch quality names used in this period ---- $quality_map = []; if (!empty($quality_ids_needed)) { $qids = array_keys($quality_ids_needed); $ph = implode(',', array_fill(0, count($qids), '?')); $sqlQ = "SELECT id, quality_name FROM qualities WHERE id IN ($ph) AND company_id = ?"; $stQ = $pdo->prepare($sqlQ); $stQ->execute([...$qids, $company_id]); $quality_map = $stQ->fetchAll(PDO::FETCH_KEY_PAIR); } // ---- Sort karigars by total desc (optional) ---- uasort($summary, function($a, $b) { return $b['total'] <=> $a['total']; }); // ---- Output ---- require_once __DIR__ . '/partials/header.php'; ?> <div class="container"> <div class="card"> <div class="card-body"> <h4 class="mb-2">Machine Production Summary</h4> <form method="get" class="form-inline mb-3"> <label style="margin-right:6px;">Machine</label> <input type="number" name="mc" value="<?php echo h($mc); ?>" class="form-control" style="width:90px;margin-right:12px;"> <label style="margin-right:6px;">Month</label> <input type="number" name="month" value="<?php echo h($month); ?>" class="form-control" style="width:70px;margin-right:12px;"> <label style="margin-right:6px;">Year</label> <input type="number" name="year" value="<?php echo h($year); ?>" class="form-control" style="width:90px;margin-right:12px;"> <label style="margin-right:6px;">Period</label> <input type="number" name="start" value="<?php echo h($start); ?>" class="form-control" style="width:60px;margin-right:6px;"> <span style="margin-right:6px;">–</span> <input type="number" name="end" value="<?php echo h($end); ?>" class="form-control" style="width:60px;margin-right:12px;"> <button class="btn btn-primary" type="submit">Load</button> </form> <p> <strong>Company:</strong> <?php echo h($company_id); ?> | <strong>Machine:</strong> <?php echo h($mc); ?> | <strong>Period:</strong> <?php echo h($start_date); ?> → <?php echo h($end_date); ?> </p> <hr> <?php if (empty($summary)): ?> <div class="notice">Koi record nahi mila.</div> <?php else: ?> <!-- Grid: 3 columns responsive. We use simple flex (small inline style only for layout). --> <div class="karigar-grid" style="display:flex;flex-wrap:wrap;gap:18px;"> <?php $col_width = 'calc(33.333% - 12px)'; // three columns foreach ($summary as $kid => $info): $kname = $karigar_names[$kid] ?? ("Karigar #".$kid); ?> <div class="card" style="flex:0 1 <?php echo $col_width; ?>; box-shadow:var(--card-shadow,0 2px 6px rgba(0,0,0,0.06));"> <div class="card-body" style="padding:12px;"> <div style="display:flex;justify-content:space-between;align-items:center;margin-bottom:8px;"> <div style="font-weight:700;"><?php echo h($kname); ?></div> <div style="background:#eef6ff;padding:6px 10px;border-radius:16px;font-weight:600;"><?php echo h($info['total']); ?> mtr</div> </div> <!-- Entries list --> <div> <?php // group entries by date so we can show day => possibly multiple meters per date (sum them) $by_date = []; foreach ($info['entries'] as $e) { $d = $e['date']; if (!isset($by_date[$d])) $by_date[$d] = ['sum'=>0,'qualities'=>[]]; $by_date[$d]['sum'] += $e['mtr']; // collect quality id counts / labels (one per production_entry row) $qid = (int)$e['quality_id']; if ($qid) $by_date[$d]['qualities'][$qid] = ($by_date[$d]['qualities'][$qid] ?? 0) + 1; } // sort dates asc ksort($by_date); foreach ($by_date as $d => $info_d): ?> <div style="display:flex;justify-content:space-between;align-items:center;padding:6px 0;border-bottom:1px dashed #f0f0f0;"> <div style="font-size:13px;color:#333;"> <?php echo h(date('d M', strtotime($d))); ?> → <span style="font-weight:600;"><?php echo h($info_d['sum']); ?> mtr</span> </div> <div style="font-size:12px;"> <?php // show quality badges (if more than one quality appear for that date, show them comma separated) $qlabels = []; foreach ($info_d['qualities'] as $qid => $count) { $qname = $quality_map[$qid] ?? ("Q#".$qid); $qlabels[] = h($qname); } echo implode(', ', $qlabels); ?> </div> </div> <?php endforeach; ?> </div> <div style="margin-top:8px;"> <a href="/erp/production_entry.php?mc=<?php echo h($mc); ?>&karigar=<?php echo h($kid); ?>" class="btn btn-sm" style="margin-right:8px;">Open Entries</a> <span style="font-size:12px;color:#666;">Karigar ID: <?php echo h($kid); ?></span> </div> </div> </div> <?php endforeach; ?> </div> <?php endif; ?> </div> </div> </div> <?php require_once __DIR__ . '/partials/footer.php'; ?>