« Back to History
beam_load_simple.php
|
20260721_154032.php
Initial Bulk Import
Copy Code
<?php /* ============================================================ File: beam_load_simple.php Purpose: Simple Beam Load (NO auto-finish, NO smart logic) ============================================================ */ error_reporting(E_ALL); ini_set('display_errors', 1); /* ---------- AUTH / CONTEXT ---------- */ require_once __DIR__ . '/modules/auth/page_acl.php'; $ctx = page_require_access('beam_load'); $pdo = $ctx['pdo']; $company_id = (int)$ctx['company_id']; $user_id = (int)$ctx['user']['id']; function h($s){ return htmlspecialchars((string)$s, ENT_QUOTES, 'UTF-8'); } require_once __DIR__ . '/helpers/activity_helper.php'; /* ============================================================ MASTER DATA ============================================================ */ /* ---------- MACHINES ---------- */ $st = $pdo->prepare(" SELECT id, code FROM machines WHERE company_id = ? AND is_active = 1 AND group_slug IN ('loom','rapier') ORDER BY code "); $st->execute([$company_id]); $machines = $st->fetchAll(PDO::FETCH_ASSOC); /* ---------- STOCK BEAMS ---------- */ $st = $pdo->prepare(" SELECT bs.id, bs.beam_no, bs.beam_slot, q.name AS quality_name FROM company_beam_stock bs JOIN beam_qualities q ON q.id = bs.quality_id WHERE bs.company_id = ? AND bs.load_type = 'stock' "); $st->execute([$company_id]); $beams = $st->fetchAll(PDO::FETCH_ASSOC); /* ---------- EMPLOYEES ---------- */ $st = $pdo->prepare(" SELECT id, name, designation_name FROM company_employee_master WHERE company_id = ? AND is_active = 1 AND designation_name IN ('Pissing','Pasaria') ORDER BY name "); $st->execute([$company_id]); $employees = $st->fetchAll(PDO::FETCH_ASSOC); /* ============================================================ SUBMIT ============================================================ */ $msg = $err = ''; if ($_SERVER['REQUEST_METHOD'] === 'POST') { $beam_id = (int)($_POST['beam_id'] ?? 0); $beam_slot = $_POST['beam_slot'] ?? ''; $machine_id = (int)($_POST['machine_id'] ?? 0); $machine_type = $_POST['machine_type'] ?? ''; $load_type = $_POST['load_type'] ?? ''; $employee_id = (int)($_POST['employee_id'] ?? 0); $load_date = $_POST['load_date'] ?? date('Y-m-d'); try { $pdo->beginTransaction(); /* ---------- FETCH BEAM ---------- */ $st = $pdo->prepare(" SELECT beam_no, beam_slot, quality_id FROM company_beam_stock WHERE id = ? AND company_id = ? AND load_type = 'stock' "); $st->execute([$beam_id, $company_id]); $beam = $st->fetch(PDO::FETCH_ASSOC); if (!$beam) { throw new Exception('Beam stock me nahi hai.'); } if ($beam['beam_slot'] !== $beam_slot) { throw new Exception('Beam slot mismatch.'); } /* ---------- MACHINE NO ---------- */ $st = $pdo->prepare(" SELECT code FROM machines WHERE id = ? AND company_id = ? "); $st->execute([$machine_id, $company_id]); $code = (string)$st->fetchColumn(); if (!preg_match('/(\d+)$/', $code, $m)) { throw new Exception('Invalid machine code.'); } $machine_no = ltrim($m[1], '0'); if ($machine_no === '') { $machine_no = '0'; } /* ---------- LOG LOAD ---------- */ $insLd = $pdo->prepare(" INSERT INTO beam_load_data (company_id, beam_no, event_type, event_date, machine_no, machine_type, beam_slot, quality_id, load_type, employee_id, load_date, created_by) VALUES (?,?,?,?,?,?,?,?,?,?,?,?) "); $insLd->execute([ $company_id, $beam['beam_no'], 'loaded', $load_date, $machine_no, $machine_type, $beam_slot, $beam['quality_id'], $load_type, $employee_id, $load_date, $user_id ]); $beam_load_data_id = (int)$pdo->lastInsertId(); /* ---------- UPDATE STOCK ---------- */ $pdo->prepare(" UPDATE company_beam_stock SET load_type = ?, beam_load_date = ? WHERE company_id = ? AND beam_no = ? ")->execute([ $load_type, $load_date, $company_id, $beam['beam_no'] ]); /* ---------- START / APPEND LIFECYCLE ---------- */ $st = $pdo->prepare(" INSERT INTO beam_life_cycle (company_id, beam_no, life_events) VALUES ( ?, ?, JSON_ARRAY( JSON_OBJECT( 'machine_no', ?, 'slot', ?, 'load_date', ?, 'action', 'running', 'machine_id', ? ) ) ) ON DUPLICATE KEY UPDATE life_events = JSON_ARRAY_APPEND( IFNULL(life_events, JSON_ARRAY()), '$', JSON_OBJECT( 'machine_no', ?, 'slot', ?, 'load_date', ?, 'action', 'running', 'machine_id', ? ) ) "); $st->execute([ $company_id, $beam['beam_no'], (string)$machine_no, $beam_slot, $load_date, (string)$machine_no, (string)$machine_no, $beam_slot, $load_date, (string)$machine_no ]); /* ---------- COMMIT ---------- */ $pdo->commit(); activity_create('beam', 'beam_load_simple_create', $beam_load_data_id, 'Recorded beam load (simple)'); header( 'Location: beam_load_simple.php?loaded=1&beam_no=' . urlencode($beam['beam_no']) ); exit; } catch (Exception $e) { if ($pdo->inTransaction()) { $pdo->rollBack(); } $err = $e->getMessage(); } } /* ============================================================ VIEW ============================================================ */ require_once __DIR__ . '/partials/header.php'; ?> <div class="wrap"> <h2>Beam Load (Simple)</h2> <?php if ($err): ?> <div class="notice notice--error"><?=h($err)?></div> <?php endif; ?> <form method="post" class="form-grid form-cols-4"> <div> <label>Beam</label> <select name="beam_id" id="beam_id" required></select> </div> <div> <label>Slot</label> <select name="beam_slot" id="beam_slot" required></select> </div> <div> <label>Quality</label> <input id="quality_name" readonly> </div> <div> <label>Machine</label> <select name="machine_id" required> <?php foreach ($machines as $m): ?> <?php if (preg_match('/(\d+)$/', $m['code'], $mm)) { $label = (int)$mm[1]; } else { $label = h($m['code']); } ?> <option value="<?=$m['id']?>"><?=$label?></option> <?php endforeach; ?> </select> </div> <div> <label>Load Type</label> <select name="load_type"> <option value="pissing">Pissing</option> <option value="pasaria">Pasaria</option> </select> </div> <div> <label>Employee</label> <select name="employee_id" id="employee_id" required></select> </div> <div> <label>Date</label> <input type="date" name="load_date" value="<?=h(date('Y-m-d'))?>"> </div> <div class="form-group--full"> <button class="btn btn--primary">Load Beam</button> </div> </form> </div> <script> const BEAMS = <?=json_encode($beams)?>; const EMP = <?=json_encode($employees)?>; const bSel = document.getElementById('beam_id'); const sSel = document.getElementById('beam_slot'); const qInp = document.getElementById('quality_name'); const eSel = document.getElementById('employee_id'); bSel.innerHTML = '<option value="">Select</option>'; BEAMS.forEach(b => { const o = document.createElement('option'); o.value = b.id; o.textContent = b.beam_no; o.dataset.slot = b.beam_slot; o.dataset.q = b.quality_name; bSel.appendChild(o); }); bSel.onchange = () => { const o = bSel.selectedOptions[0]; if (!o) return; sSel.innerHTML = ''; ['primary','secondary'].forEach(s => { const opt = document.createElement('option'); opt.value = s; opt.textContent = s; if (s === o.dataset.slot) opt.selected = true; sSel.appendChild(opt); }); qInp.value = o.dataset.q || ''; }; EMP.forEach(e => { const o = document.createElement('option'); o.value = e.id; o.textContent = e.name; eSel.appendChild(o); }); </script> <?php require_once __DIR__ . '/partials/footer.php'; ?>