« Back to History
beam_sent_entry.php
|
20260722_120325.php
Initial Domain Snapshot
Copy Code
<?php /* ========================================================================= File: /erp/beam_sent_entry.php Purpose: Beam Sent Entry + auto-update beam_entry.installation_source = 'sent' and beam_entry.installation_date = sent_date (latest row for beam_no) Scope : Page-local only; multi-company aware; NO global/base edits Notes : This version fixes a matching issue by updating beam_entry via a safe subquery (works if beam_no is VARCHAR or numeric). ========================================================================= */ header('X-Frame-Options: SAMEORIGIN'); error_reporting(E_ALL); ini_set('display_errors', 1); /* -------- Auth + PDO -------- */ require __DIR__ . '/modules/auth/auth.php'; require_login(); $u = auth_user(); $COMPANY_ID = (int)($u['company_id'] ?? 0); $USER_ID = (int)($u['id'] ?? 0); $pdo = $GLOBALS['pdo'] ?? null; if (!$pdo) { require __DIR__ . '/core/db.php'; } require_once __DIR__ . '/helpers/activity_helper.php'; /* -------- Session + CSRF -------- */ if (session_status() !== PHP_SESSION_ACTIVE) session_start(); if (empty($_SESSION['csrf'])) $_SESSION['csrf'] = bin2hex(random_bytes(16)); function csrf_field(){ echo '<input type="hidden" name="csrf" value="'.htmlspecialchars($_SESSION['csrf']).'">'; } function check_csrf(){ if (($_POST['csrf'] ?? '') !== ($_SESSION['csrf'] ?? '')) { http_response_code(400); exit('Bad CSRF'); } } /* -------- Ensure table (local) -------- */ $pdo->exec("CREATE TABLE IF NOT EXISTS beam_sent ( id BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY, company_id BIGINT UNSIGNED NOT NULL, sent_date DATE NOT NULL, beam_no VARCHAR(100) NOT NULL, meter DECIMAL(10,2) NOT NULL, quality_id BIGINT UNSIGNED NOT NULL, sent_to VARCHAR(255) NOT NULL, employee_id BIGINT UNSIGNED NOT NULL, created_by BIGINT UNSIGNED NOT NULL, created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, KEY(company_id), KEY(beam_no), KEY(quality_id), KEY(employee_id) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4"); /* -------- Handle submit -------- */ $msg = ''; $err = ''; if ($_SERVER['REQUEST_METHOD'] === 'POST') { try { check_csrf(); $sent_date = trim((string)($_POST['sent_date'] ?? date('Y-m-d'))); // Validate date format (basic) if (!preg_match('/^\d{4}-\d{2}-\d{2}$/', $sent_date)) { throw new RuntimeException('Invalid date format.'); } $beam_no = trim((string)($_POST['beam_no'] ?? '')); $meter = (float)($_POST['meter'] ?? 0); $quality_id = (int)($_POST['quality_id'] ?? 0); $sent_to = trim((string)($_POST['sent_to'] ?? '')); $employee_id = (int)($_POST['employee_id'] ?? 0); if ($beam_no === '' || $meter <= 0 || $quality_id <= 0 || $sent_to === '' || $employee_id <= 0) { throw new RuntimeException('Please fill all required fields properly.'); }