« Back to History
beam_life_helpers.php
|
20260721_154032.php
Initial Bulk Import
Copy Code
<?php /* ========================================================= Beam Lifecycle Helpers beam_load_data is READ-ONLY (salary critical) ========================================================= */ function rebuild_machine_beam_status(PDO $pdo, int $company_id, int $beam_no, array $events) { /* ===================================================== 1️⃣ Remove this beam from all machines (safe reset) ===================================================== */ $stmt = $pdo->prepare(" UPDATE machine_beam_status SET primary_running_beam = IF(primary_running_beam = ?, NULL, primary_running_beam), secondary_running_beam = IF(secondary_running_beam = ?, NULL, secondary_running_beam) WHERE company_id = ? "); $stmt->execute([$beam_no, $beam_no, $company_id]); if (empty($events)) { return; } /* ===================================================== 2️⃣ Determine last lifecycle action ===================================================== */ $last = end($events); $last_action = $last['action'] ?? ''; /* ===================================================== 3️⃣ Update company_beam_stock.load_type ===================================================== */ if ($last_action === 'running') { // Set beam as running error_log("[DEBUG] Setting beam_no=$beam_no company_id=$company_id to running"); $result = $pdo->prepare(" UPDATE company_beam_stock SET load_type = 'running', updated_at = NOW() WHERE company_id = ? AND beam_no = ? "); $ok = $result->execute([$company_id, $beam_no]); error_log("[DEBUG] UPDATE running result: " . ($ok ? 'OK' : 'FAIL')); } elseif ($last_action === 'cut') { // Beam back to stock error_log("[DEBUG] Setting beam_no=$beam_no company_id=$company_id to stock"); $result = $pdo->prepare(" UPDATE company_beam_stock SET load_type = 'stock', updated_at = NOW() WHERE company_id = ? AND beam_no = ? "); $ok = $result->execute([$company_id, $beam_no]); error_log("[DEBUG] UPDATE stock result: " . ($ok ? 'OK' : 'FAIL')); // No machine assignment needed return; } else { // finish or any other action → do nothing extra return; } /* ===================================================== 4️⃣ If running → assign to machine ===================================================== */ $machine_no = (int)($last['machine_no'] ?? 0); if ($machine_no <= 0) { return; } $field = ($last['slot'] ?? '') === 'primary' ? 'primary_running_beam' : 'secondary_running_beam'; $pdo->prepare(" UPDATE machine_beam_status SET {$field} = ? WHERE company_id = ? AND machine_no = ? ")->execute([ $beam_no, $company_id, $machine_no ]); }