« Back to History
roll_detail_manage.php
|
20260721_154033.php
Initial Bulk Import
Copy Code
<?php error_reporting(E_ALL); ini_set('display_errors', 1); /* ========================= AUTH / CONTEXT ========================= */ require_once __DIR__ . '/modules/auth/page_acl.php'; $ctx = page_require_access('roll_detail_manage'); require_once __DIR__ . '/modules/activity/activity_logger.php'; $pdo = $ctx['pdo']; $company_id = (int)$ctx['company_id']; $user = $ctx['user'] ?? null; $user_id = (int)($user['id'] ?? 0); /* ========================= CSRF ========================= */ if (empty($_SESSION['csrf_token'])) { $_SESSION['csrf_token'] = bin2hex(random_bytes(32)); } $csrf = $_SESSION['csrf_token']; /* ========================= SAVE / UPDATE ========================= */ $message = ''; $error = ''; if ($_SERVER['REQUEST_METHOD'] === 'POST') { if (!hash_equals($_SESSION['csrf_token'], $_POST['csrf_token'] ?? '')) { die('Invalid CSRF token'); } $roll = trim($_POST['roll'] ?? ''); $roll_weight = (float)($_POST['roll_weight'] ?? 0); $filled_roll_weight = (float)($_POST['filled_roll_weight'] ?? 0); if ($roll === '') { $error = 'Roll is required'; } else { // check existing $chk = $pdo->prepare(" SELECT id FROM roll_detail WHERE company_id = :cid AND roll = :roll LIMIT 1 "); $chk->execute([ ':cid' => $company_id, ':roll' => $roll ]); $existing = $chk->fetch(PDO::FETCH_ASSOC); if ($existing) { // UPDATE $upd = $pdo->prepare(" UPDATE roll_detail SET roll_weight = :rw, filled_roll_weight = :frw WHERE id = :id AND company_id = :cid "); $upd->execute([ ':rw' => $roll_weight, ':frw' => $filled_roll_weight, ':id' => $existing['id'], ':cid' => $company_id ]); activity_log([ 'company_id' => $company_id, 'user_id' => $user_id, 'module' => 'roll', 'action_name' => 'edit', 'entity_type' => 'roll_detail', 'entity_id' => (int)$existing['id'], 'remarks' => 'Updated roll detail' ]); $message = 'Roll updated successfully'; } else { // INSERT $ins = $pdo->prepare(" INSERT INTO roll_detail (company_id, roll, roll_weight, filled_roll_weight) VALUES (:cid, :roll, :rw, :frw) "); $ins->execute([ ':cid' => $company_id, ':roll' => $roll, ':rw' => $roll_weight, ':frw' => $filled_roll_weight ]); activity_log([ 'company_id' => $company_id, 'user_id' => $user_id, 'module' => 'roll', 'action_name' => 'create', 'entity_type' => 'roll_detail', 'entity_id' => (int)$pdo->lastInsertId(), 'remarks' => 'Created roll detail' ]); $message = 'Roll added successfully'; } } } /* ========================= FETCH LIST ========================= */ $list = $pdo->prepare(" SELECT * FROM roll_detail WHERE company_id = :cid ORDER BY id DESC "); $list->execute([':cid' => $company_id]); $rows = $list->fetchAll(PDO::FETCH_ASSOC); ?> <?php require_once __DIR__ . '/partials/header.php'; ?> <div class="card"> <h3>Roll Detail Manage</h3> <?php if ($message): ?> <div class="notice success"><?=htmlspecialchars($message)?></div> <?php endif; ?> <?php if ($error): ?> <div class="notice error"><?=htmlspecialchars($error)?></div> <?php endif; ?> <form method="post" class="form-grid"> <input type="hidden" name="csrf_token" value="<?=htmlspecialchars($csrf)?>"> <div> <label>Roll</label> <input type="text" name="roll" required> </div> <div> <label>Roll Weight</label> <input type="number" step="0.01" name="roll_weight" value="0"> </div> <div> <label>Filled Roll Weight</label> <input type="number" step="0.01" name="filled_roll_weight" value="0"> </div> <div style="align-self:end"> <button type="submit" class="btn primary">Save</button> </div> </form> </div> <div class="card"> <h3>Roll List</h3> <table class="table"> <thead> <tr> <th>ID</th> <th>Roll</th> <th>Roll Weight</th> <th>Filled Weight</th> <th>Remaining</th> </tr> </thead> <tbody> <?php if (!$rows): ?> <tr><td colspan="5">No records</td></tr> <?php else: ?> <?php foreach ($rows as $r): ?> <tr> <td><?=htmlspecialchars($r['id'])?></td> <td><?=htmlspecialchars($r['roll'])?></td> <td><?=htmlspecialchars($r['roll_weight'])?></td> <td><?=htmlspecialchars($r['filled_roll_weight'])?></td> <td> <?=htmlspecialchars( (float)$r['roll_weight'] - (float)$r['filled_roll_weight'] )?> </td> </tr> <?php endforeach; ?> <?php endif; ?> </tbody> </table> </div> <?php require_once __DIR__ . '/partials/footer.php'; ?>