« Back to History
yarn_sent_entry.php
|
20260722_120325.php
Initial Domain Snapshot
Copy Code
<?php // /erp/yarn_sent_entry.php // Single file: Show form + Save data + Show last entry /* ------------------------------------------ 1) PAGE ACL (do not modify global system) ------------------------------------------ */ $acl_candidates = [ __DIR__ . '/modules/auth/page_acl.php', __DIR__ . '/../modules/auth/page_acl.php', __DIR__ . '/../../modules/auth/page_acl.php', __DIR__ . '/erp/modules/auth/page_acl.php', ]; $loaded = false; foreach ($acl_candidates as $f) { if (file_exists($f)) { require_once $f; $loaded = true; break; } } if (!$loaded) { die("ACL missing"); } require_once __DIR__ . '/modules/activity/activity_logger.php'; $ctx = page_require_access('yarn_sent_entry'); $u = $ctx['user'] ?? null; $company_id = (int)($ctx['company_id'] ?? 0); $pdo = $ctx['pdo'] ?? null; /* ------------------------------------------ 2) CSRF Token ------------------------------------------ */ if (!isset($_SESSION['csrf_token'])) { $_SESSION['csrf_token'] = bin2hex(random_bytes(20)); } /* ----------------------------------------------------------- 3) HANDLE POST (SAVE INSIDE SAME PAGE) ----------------------------------------------------------- */ $flash_success = ""; $flash_error = ""; if ($_SERVER['REQUEST_METHOD'] === 'POST') { // CSRF check $token = $_POST['csrf_token'] ?? ''; if (!hash_equals($_SESSION['csrf_token'], $token)) { $flash_error = "Invalid security token."; } else { // Collect POST data $sent_date = trim($_POST['sent_date'] ?? ''); $yarn_type = trim($_POST['yarn_type'] ?? ''); $company = trim($_POST['company'] ?? ''); $denier = trim($_POST['denier'] ?? ''); $color = trim($_POST['color'] ?? ''); $box = trim($_POST['box'] ?? ''); $weight = trim($_POST['weight'] ?? ''); $party = trim($_POST['party'] ?? ''); // Basic validation $errors = []; if ($sent_date == '') $errors[] = "Date required"; if ($yarn_type == '') $errors[] = "Yarn Type required"; if ($company == '') $errors[] = "Company required"; if (count($errors)) { $flash_error = implode(", ", $errors); } else { try { $sql = "INSERT INTO yarn_sent (company_id, sent_date, yarn_type, company_name, denier, color, box, weight, party, created_by, created_at) VALUES (:cid, :d, :t, :c, :dn, :co, :b, :w, :p, :uid, NOW())"; $stmt = $pdo->prepare($sql); $stmt->execute([ ':cid' => $company_id, ':d' => $sent_date, ':t' => $yarn_type, ':c' => $company, ':dn' => $denier, ':co' => $color, ':b' => $box, ':w' => $weight, ':p' => $party, ':uid' => $u['id'] ?? 0, ]); activity_log([ 'company_id' => $company_id ?? 0, 'user_id' => $u['id'] ?? ($_SESSION['user_id'] ?? 0), 'module' => 'yarn', 'action_name' => 'create', 'entity_type' => 'yarn_sent', 'entity_id' => (int)$pdo->lastInsertId(), 'remarks' => 'Yarn sent entry created' ]); $flash_success = "Saved successfully!"; } catch (Exception $e) { $flash_error = "DB Error: " . $e->getMessage(); } } } } /* ----------------------------------------------------------- 4) FETCH LAST ENTRY ----------------------------------------------------------- */ $last_entry_text = ""; try { $q = $pdo->prepare("SELECT * FROM yarn_sent WHERE company_id = :cid ORDER BY id DESC LIMIT 1"); $q->execute([':cid' => $company_id]); $r = $q->fetch(PDO::FETCH_ASSOC); if ($r) { $last_entry_text = date("d-M-Y", strtotime($r['sent_date'])) . " | " . ($r['yarn_type'] ?? '') . " | " . ($r['color'] ?? '') . " | " . ($r['box'] ?? '') . " | " . ($r['weight'] ?? ''); } } catch (Exception $e) { $last_entry_text = ""; } /* ------------------------------------------ 5) LOAD HEADER ------------------------------------------ */ require_once __DIR__ . '/partials/header.php'; ?> <div class="erp-container"> <div class="erp-card erp-card-centered page-yarn-sent"> <h2 class="erp-title">Yarn sent entry</h2> <div class="erp-badges"> <span class="erp-badge active">YARN</span> <span class="erp-badge">TPM</span> <span class="erp-badge">ZARI</span> </div> <!-- Flash Messages --> <?php if ($flash_error): ?> <div class="alert alert-danger" style="margin-bottom:10px; background:#ffdddd; padding:8px; border-radius:4px; color:#900;"> <?php echo h($flash_error); ?> </div> <?php endif; ?> <?php if ($flash_success): ?> <div class="alert alert-success" style="margin-bottom:10px; background:#ddffdd; padding:8px; border-radius:4px; color:#060;"> <?php echo h($flash_success); ?> </div> <?php endif; ?> <!-- FORM --> <form method="post" class="erp-form"> <input type="hidden" name="csrf_token" value="<?php echo h($_SESSION['csrf_token']); ?>"> <div class="form-grid"> <div class="form-row"> <label>Date</label> <input type="date" name="sent_date" class="erp-input" value="<?php echo date('Y-m-d'); ?>"> </div> <div class="form-row"> <label>Yarn Type</label> <input type="text" name="yarn_type" class="erp-input"> </div> <div class="form-row"> <label>Company</label> <input type="text" name="company" class="erp-input"> </div> <div class="form-row"> <label>Denier</label> <input type="text" name="denier" class="erp-input"> </div> <div class="form-row"> <label>Color</label> <input type="text" name="color" class="erp-input"> </div> <div class="form-row"> <label>Box</label> <input type="number" name="box" class="erp-input"> </div> <div class="form-row"> <label>Weight</label> <input type="text" name="weight" class="erp-input"> </div> <div class="form-row"> <label>Party</label> <input type="text" name="party" class="erp-input"> </div> </div> <div class="form-actions"> <button type="submit" class="erp-btn erp-btn-primary">Submit</button> <a href="#" class="erp-btn erp-btn-secondary">Records</a> </div> </form> </div> <!-- LAST ENTRY --> <div class="erp-last-entry"> <input type="text" readonly class="erp-input erp-last-entry-input" value="<?php echo h($last_entry_text); ?>"> </div> </div> <?php require_once __DIR__ . '/partials/footer.php'; ?>