« Back to History
beam_auto_finish_helper.php
|
20260721_154032.php
Initial Bulk Import
Copy Code
<?php /** * Auto finish running beam on a machine slot before new load * Reuses EXACT Beam Manager finish logic * NOTE: * - Must be called INSIDE an active transaction * - Silent if no running beam exists * - Throws exception only on data corruption */ function auto_finish_running_beam( PDO $pdo, int $company_id, string $machine_no, string $slot, string $finish_date, int $user_id ): void { /* determine slot column */ $col = ($slot === 'primary') ? 'primary_running_beam' : 'secondary_running_beam'; /* ------------------------------------------------- LOCK MACHINE STATUS ------------------------------------------------- */ $st = $pdo->prepare(" SELECT $col FROM machine_beam_status WHERE company_id = ? AND machine_no = ? FOR UPDATE "); $st->execute([$company_id, $machine_no]); $beam_no = $st->fetchColumn(); /* no running beam → nothing to do */ if (!$beam_no) { return; } /* ------------------------------------------------- LOCK LIFECYCLE ------------------------------------------------- */ $st = $pdo->prepare(" SELECT life_events FROM beam_life_cycle WHERE beam_no = ? FOR UPDATE "); $st->execute([$beam_no]); $lifeJson = $st->fetchColumn(); if (!$lifeJson) { throw new Exception("Lifecycle missing for running beam {$beam_no}"); } $events = json_decode($lifeJson, true); if (!is_array($events)) { throw new Exception("Invalid lifecycle JSON for beam {$beam_no}"); } /* ------------------------------------------------- FIND OPEN RUN (same logic as Beam Manager) ------------------------------------------------- */ $runIndex = null; for ($i = count($events) - 1; $i >= 0; $i--) { $ev = $events[$i] ?? null; if (!is_array($ev)) continue; if ( (string)($ev['machine_no'] ?? '') === (string)$machine_no && ($ev['slot'] ?? '') === $slot && ($ev['action'] ?? '') === 'running' && !isset($ev['cut_date']) && !isset($ev['finish_date']) ) { $runIndex = $i; break; } } if ($runIndex === null) { throw new Exception("Open running lifecycle not found for beam {$beam_no}"); } /* ------------------------------------------------- REMOVE FROM MACHINE ------------------------------------------------- */ $pdo->prepare(" UPDATE machine_beam_status SET $col = NULL WHERE company_id = ? AND machine_no = ? ")->execute([$company_id, $machine_no]); /* ------------------------------------------------- MARK BEAM AS FINISHED ------------------------------------------------- */ $pdo->prepare(" UPDATE company_beam_stock SET load_type = 'finished' WHERE company_id = ? AND beam_no = ? ")->execute([$company_id, $beam_no]); /* ------------------------------------------------- CLOSE LIFECYCLE EVENT ------------------------------------------------- */ $events[$runIndex]['finish_date'] = $finish_date; $events[$runIndex]['action'] = 'finish'; $events[$runIndex]['machine_id'] = (string)$machine_no; $pdo->prepare(" UPDATE beam_life_cycle SET life_events = ? WHERE beam_no = ? ")->execute([ json_encode($events, JSON_UNESCAPED_UNICODE), $beam_no ]); }