« Back to History
beam_book.php
|
20260723_000646.php
Initial Domain Snapshot
Copy Code
<?php error_reporting(E_ALL); ini_set('display_errors', 1); require_once __DIR__ . '/modules/auth/page_acl.php'; $ctx = page_require_access('beam_book'); $company_id = (int)$ctx['company_id']; $pdo = $ctx['pdo']; require_once __DIR__ . '/helpers/activity_helper.php'; function h($s){ return htmlspecialchars((string)$s, ENT_QUOTES, 'UTF-8'); } /* ========================================================= REBUILD BEAM BOOK (UNCHANGED CORE LOGIC) ========================================================= */ $stockStmt = $pdo->prepare(" SELECT meter FROM company_beam_stock WHERE company_id=? AND beam_no=? "); $machineIdStmt = $pdo->prepare(" SELECT id FROM machines WHERE company_id=? AND name=? LIMIT 1 "); $prodStmt = $pdo->prepare(" SELECT COALESCE(SUM(meter_total),0) FROM production_entry WHERE company_id=? AND machine_id=? AND entry_date BETWEEN ? AND ? "); $upsertBeam = $pdo->prepare(" INSERT INTO beam_book (company_id, beam_no, total_meter, production, pending_meter) VALUES (?,?,?,?,?) ON DUPLICATE KEY UPDATE total_meter = VALUES(total_meter), production = VALUES(production), pending_meter = VALUES(pending_meter) "); $lcStmt = $pdo->query(" SELECT beam_no, life_events FROM beam_life_cycle "); while ($lc = $lcStmt->fetch(PDO::FETCH_ASSOC)) { $beam_no = (int)$lc['beam_no']; $events = json_decode($lc['life_events'], true); if (!is_array($events)) continue; if (isset($events['machine_no'])) { $events = [$events]; } $stockStmt->execute([$company_id, $beam_no]); $total_meter = (float)$stockStmt->fetchColumn(); if ($total_meter <= 0) continue; $production = 0; foreach ($events as $ev) { if (empty($ev['load_date'])) continue; $machine_id = 0; if (!empty($ev['machine_id'])) { $machine_id = (int)$ev['machine_id']; } if ($machine_id <= 0 && !empty($ev['machine_no'])) { $machine_no = str_pad((string)$ev['machine_no'], 3, '0', STR_PAD_LEFT); $machineIdStmt->execute([$company_id, $machine_no]); $machine_id = (int)$machineIdStmt->fetchColumn(); } if ($machine_id <= 0) continue; $from = $ev['load_date']; if (!empty($ev['cut_date'])) { $to = $ev['cut_date']; } elseif (!empty($ev['finish_date'])) { $to = $ev['finish_date']; } else { $to = date('Y-m-d'); } $prodStmt->execute([$company_id, $machine_id, $from, $to]); $production += (float)$prodStmt->fetchColumn(); } $pending = max(0, $total_meter - $production); $upsertBeam->execute([ $company_id, $beam_no, $total_meter, $production, $pending ]); } $u = $ctx['user']; // Required by activity helper log_activity([ 'company_id' => $company_id, 'user_id' => $ctx['user']['id'] ?? 0, 'module' => 'beam', 'page' => basename(__FILE__), 'action' => 'UPDATE', 'entity_type' => 'beam_book', 'entity_id' => 0, 'description' => "Rebuilt Beam Book production and pending meters from lifecycle events" ]); /* ========================================================= MACHINE-WISE GROUPED VIEW ========================================================= */ $loadDateStmt = $pdo->prepare(" SELECT load_date FROM beam_load_data WHERE company_id=? AND beam_no=? AND machine_no=? AND event_type='loaded' ORDER BY load_date DESC LIMIT 1 "); $list = $pdo->prepare(" SELECT m.machine_no, m.primary_running_beam AS p_beam, bp.total_meter AS p_total, bp.production AS p_prod, bp.pending_meter AS p_pending, m.secondary_running_beam AS s_beam, bs.total_meter AS s_total, bs.production AS s_prod, bs.pending_meter AS s_pending FROM machine_beam_status m LEFT JOIN beam_book bp ON bp.company_id=m.company_id AND bp.beam_no=m.primary_running_beam LEFT JOIN beam_book bs ON bs.company_id=m.company_id AND bs.beam_no=m.secondary_running_beam WHERE m.company_id=? ORDER BY m.machine_no "); $list->execute([$company_id]); $rows = $list->fetchAll(PDO::FETCH_ASSOC); $page_title = 'Beam Book'; require_once __DIR__ . '/partials/header.php'; ?> <?php $page_title = 'Beam Book'; require_once __DIR__ . '/partials/header.php'; ?> <div class="container-fluid py-4"> <div class="d-flex justify-content-between align-items-center mb-4"> <h3 class="mb-0">Beam Book – Machine Wise</h3> </div> <div class="card shadow-sm"> <div class="card-body p-0"> <div class="table-responsive"> <table class="table table-bordered table-hover align-middle mb-0"> <thead class="table-light text-uppercase small"> <tr> <th>Machine</th> <th>Beam Type</th> <th>Beam No</th> <th>Load Date</th> <th class="text-end">Total Meter</th> <th class="text-end">Produced</th> <th class="text-end">Pending</th> </tr> </thead> <tbody> <?php foreach ($rows as $r): ?> <?php $p_load = $r['p_beam'] ? ($loadDateStmt->execute([$company_id, $r['p_beam'], $r['machine_no']]) ? $loadDateStmt->fetchColumn() : null) : null; $s_load = $r['s_beam'] ? ($loadDateStmt->execute([$company_id, $r['s_beam'], $r['machine_no']]) ? $loadDateStmt->fetchColumn() : null) : null; ?> <!-- PRIMARY ROW --> <tr class="table-white"> <td rowspan="2" class="fw-bold text-center align-middle"> <?=h($r['machine_no'])?> </td> <td> <span class="badge bg-primary">Primary</span> </td> <td><?=h($r['p_beam'] ?? '-')?></td> <td><?=h($p_load ?? '-')?></td> <td class="text-end"><?=number_format($r['p_total'] ?? 0,2)?></td> <td class="text-end"><?=number_format($r['p_prod'] ?? 0,2)?></td> <td class="text-end fw-semibold text-success"> <?=number_format($r['p_pending'] ?? 0,2)?> </td> </tr> <!-- SECONDARY ROW --> <tr class="table-light"> <td> <span class="badge bg-secondary">Secondary</span> </td> <td><?=h($r['s_beam'] ?? '-')?></td> <td><?=h($s_load ?? '-')?></td> <td class="text-end"><?=number_format($r['s_total'] ?? 0,2)?></td> <td class="text-end"><?=number_format($r['s_prod'] ?? 0,2)?></td> <td class="text-end fw-semibold text-success"> <?=number_format($r['s_pending'] ?? 0,2)?> </td> </tr> <?php endforeach; ?> </tbody> </table> </div> </div> </div> </div> <?php require_once __DIR__ . '/partials/footer.php'; ?>