« Back to History
beam_installation_record.php
|
20260723_000646.php
Initial Domain Snapshot
Copy Code
<?php // beam_installation_record.php require_once __DIR__ . '/modules/auth/page_acl.php'; $ctx = page_require_access('beam_installation_record'); $u = $ctx['user'] ?? null; $company_id = (int)($ctx['company_id'] ?? 0); $pdo = $ctx['pdo'] ?? null; if (!$pdo) { require_once __DIR__ . '/core/db.php'; if (!$pdo) die("DB connection failed."); } if (!function_exists('h')) { function h($s){ return htmlspecialchars((string)$s, ENT_QUOTES, 'UTF-8'); } } require_once __DIR__ . '/helpers/activity_helper.php'; // CSRF if (empty($_SESSION['csrf_token'])) $_SESSION['csrf_token'] = bin2hex(random_bytes(32)); $csrf = $_SESSION['csrf_token']; /** * Inserts a beam installation record programmatically (callable from pissing/pasaria flow) * $data keys accepted: * source_table, source_id, beam_no, beam_type, machine_no, * pasaria_id, pissing_entry_id, installation_date (Y-m-d), note, details (assoc array), created_by * * Returns array: [ 'ok' => bool, 'msg' => string, 'insert_id' => int|null ] */ function insert_beam_installation_record_from_source($pdo, $company_id, $data) { $minDate = '2025-12-01'; // rule: only records on/after this date allowed $inst = $data['installation_date'] ?? null; if (!$inst) return ['ok'=>false, 'msg'=>'installation_date missing', 'insert_id'=>null]; // normalize to date $d = date('Y-m-d', strtotime($inst)); if ($d < $minDate) { return ['ok'=>false, 'msg'=> "installation_date must be {$minDate} or later", 'insert_id'=>null]; } $sql = "INSERT INTO beam_installation_record (company_id, source_table, source_id, beam_no, beam_type, machine_no, pasaria_id, pissing_entry_id, installation_date, note, details, created_by, created_at) VALUES (:company_id, :source_table, :source_id, :beam_no, :beam_type, :machine_no, :pasaria_id, :pissing_entry_id, :installation_date, :note, :details, :created_by, NOW())"; $stmt = $pdo->prepare($sql); try { $stmt->execute([ ':company_id' => $company_id, ':source_table' => $data['source_table'] ?? null, ':source_id' => $data['source_id'] ?? null, ':beam_no' => $data['beam_no'] ?? null, ':beam_type' => $data['beam_type'] ?? null, ':machine_no' => $data['machine_no'] ?? null, ':pasaria_id' => $data['pasaria_id'] ?? null, ':pissing_entry_id' => $data['pissing_entry_id'] ?? null, ':installation_date' => $d, ':note' => $data['note'] ?? null, ':details' => isset($data['details']) ? json_encode($data['details'], JSON_UNESCAPED_UNICODE) : null, ':created_by' => $data['created_by'] ?? null ]); return ['ok'=>true, 'msg'=>'Inserted', 'insert_id'=>$pdo->lastInsertId()]; } catch (Exception $e) { error_log("insert_beam_installation_record_from_source error: ".$e->getMessage()); return ['ok'=>false, 'msg'=>'DB error: '.$e->getMessage(), 'insert_id'=>null]; } } /* ---------------- Handle form POST (manual add via UI) ---------------- */ $notice = ''; if ($_SERVER['REQUEST_METHOD'] === 'POST' && ($_POST['action'] ?? '') === 'add') { if (!hash_equals($csrf, $_POST['csrf_token'] ?? '')) { die("Invalid CSRF"); } $data = [ 'source_table' => $_POST['source_table'] ?? null, 'source_id' => ($_POST['source_id'] ?? '') === '' ? null : (int)$_POST['source_id'], 'beam_no' => trim($_POST['beam_no'] ?? ''), 'beam_type' => trim($_POST['beam_type'] ?? ''), 'machine_no' => trim($_POST['machine_no'] ?? ''), 'pasaria_id' => ($_POST['pasaria_id'] ?? '') === '' ? null : (int)$_POST['pasaria_id'], 'pissing_entry_id' => ($_POST['pissing_entry_id'] ?? '') === '' ? null : (int)$_POST['pissing_entry_id'], 'installation_date' => $_POST['installation_date'] ?? null, 'note' => trim($_POST['note'] ?? ''), 'details' => null, 'created_by' => $u['id'] ?? null ]; $res = insert_beam_installation_record_from_source($pdo, $company_id, $data); if ($res['ok']) { activity_create('beam', 'beam_installation_record_create', (int)($res['insert_id'] ?? 0), 'Created beam installation record'); $notice = "Record added (id {$res['insert_id']})."; } else { $notice = "Failed: " . h($res['msg']); } } /* ---------------- Fetch list for display ---------------- */ $limit = 200; $stmt = $pdo->prepare("SELECT * FROM beam_installation_record WHERE company_id = :cid ORDER BY installation_date DESC, id DESC LIMIT :lim"); $stmt->bindValue(':cid', $company_id, PDO::PARAM_INT); $stmt->bindValue(':lim', (int)$limit, PDO::PARAM_INT); $stmt->execute(); $rows = $stmt->fetchAll(PDO::FETCH_ASSOC); ?> <!doctype html> <html> <head> <meta charset="utf-8"> <title>Beam Installation Record</title> <style> table{border-collapse:collapse;width:100%} th,td{border:1px solid #ddd;padding:6px;font-size:13px} th{background:#f7f7f7} .notice{margin:8px 0;color:green} .err{color:red} form .row{margin-bottom:8px;} </style> </head> <body> <?php if (file_exists(__DIR__.'/erp/partials/header.php')) require_once __DIR__.'/erp/partials/header.php'; ?> <div style="padding:16px;"> <h2>Beam Installation Record</h2> <?php if ($notice): ?><div class="notice"><?= h($notice) ?></div><?php endif; ?> <h3>Add record (manual)</h3> <form method="post"> <input type="hidden" name="csrf_token" value="<?= $csrf ?>"> <input type="hidden" name="action" value="add"> <div class="row"> <label>Installation Date (required, >= 2025-12-01)</label> <input type="date" name="installation_date" required> </div> <div class="row"> <label>Source Table (optional)</label> <input type="text" name="source_table" placeholder="pissing_entry or pasaria_entry"> </div> <div class="row"> <label>Source ID (optional)</label> <input type="number" name="source_id"> </div> <div class="row"> <label>Machine No</label> <input type="text" name="machine_no"> </div> <div class="row"> <label>Beam No</label> <input type="text" name="beam_no"> </div> <div class="row"> <label>Beam Type</label> <input type="text" name="beam_type" placeholder="Primary/Secondary"> </div> <div class="row"> <label>Pasaria ID</label> <input type="number" name="pasaria_id"> </div> <div class="row"> <label>Pissing Entry ID</label> <input type="number" name="pissing_entry_id"> </div> <div class="row"> <label>Note</label> <input type="text" name="note"> </div> <div class="row"> <button type="submit">Add Record</button> </div> </form> <h3>Recent Installation Records (limit <?= $limit ?>)</h3> <table> <thead> <tr> <th>ID</th> <th>Inst Date</th> <th>Beam No</th> <th>Type</th> <th>Machine</th> <th>Source</th> <th>Created At</th> <th>Note</th> </tr> </thead> <tbody> <?php if (!$rows): ?> <tr><td colspan="8">No records.</td></tr> <?php else: foreach ($rows as $r): ?> <tr> <td><?= h($r['id']) ?></td> <td><?= h($r['installation_date']) ?></td> <td><?= h($r['beam_no']) ?></td> <td><?= h($r['beam_type']) ?></td> <td><?= h($r['machine_no']) ?></td> <td><?= h($r['source_table']).' #'.h($r['source_id']) ?></td> <td><?= h($r['created_at']) ?></td> <td><?= h($r['note']) ?></td> </tr> <?php endforeach; endif; ?> </tbody> </table> </div> <?php if (file_exists(__DIR__.'/erp/partials/footer.php')) require_once __DIR__.'/erp/partials/footer.php'; ?> </body> </html>