« Back to History
fabric_quality_master.php
|
20260721_154032.php
Initial Bulk Import
Copy Code
<?php require_once __DIR__ . '/modules/auth/page_acl.php'; $ctx = page_require_access('fabric_quality_master'); $pdo = $ctx['pdo']; $company_id = (int)$ctx['company_id']; $u = $ctx['user']; function h($value){ return htmlspecialchars((string)$value, ENT_QUOTES, 'UTF-8'); } $page_error = ''; $page_notice = ''; $status_column = null; try { $columnStmt = $pdo->query("SHOW COLUMNS FROM fabric_quality_master"); $columns = $columnStmt ? $columnStmt->fetchAll(PDO::FETCH_COLUMN, 0) : []; if (in_array('is_active', $columns, true)) { $status_column = 'is_active'; } elseif (in_array('status', $columns, true)) { $status_column = 'status'; } } catch (Throwable $e) { $page_error = $e->getMessage(); } require_once __DIR__ . '/partials/header.php'; require_once __DIR__ . '/helpers/activity_helper.php'; /* ===== SAVE / UPDATE ===== */ if($_SERVER['REQUEST_METHOD']==='POST' && $page_error === ''){ $name = trim($_POST['quality_name']); $short = trim($_POST['short_name'] ?? ''); $edit_id = (int)($_POST['edit_id'] ?? 0); if($name){ try{ if($edit_id > 0){ // ===== UPDATE ===== $stmt = $pdo->prepare(" UPDATE fabric_quality_master SET quality_name=:name, short_name=:short WHERE id=:id AND company_id=:cid "); $stmt->execute([ ':name'=>$name, ':short'=>$short, ':id'=>$edit_id, ':cid'=>$company_id ]); // Activity Log log_activity([ 'activity_type' => 'master', 'module_name' => 'fabric_quality_master', 'action_name' => 'update', 'activity_note' => "Updated Quality: $name ($short)", 'reference_id' => $edit_id, ]); }else{ // ===== INSERT ===== $stmt = $pdo->prepare(" INSERT INTO fabric_quality_master (company_id, quality_name, short_name) VALUES (:cid,:name,:short) "); $stmt->execute([ ':cid'=>$company_id, ':name'=>$name, ':short'=>$short ]); // Activity Log $new_id = $pdo->lastInsertId(); log_activity([ 'activity_type' => 'master', 'module_name' => 'fabric_quality_master', 'action_name' => 'create', 'activity_note' => "Created Quality: $name ($short)", 'reference_id' => $new_id, ]); } }catch(Throwable $e){ if (($e instanceof PDOException) && (($e->errorInfo[1] ?? 0) == 1062)) { $page_notice = 'Duplicate quality'; } else { $page_error = $e->getMessage(); } } } } /* ===== EDIT FETCH ===== */ $edit = null; if($page_error === '' && isset($_GET['edit'])){ $id = (int)$_GET['edit']; try { $stmt = $pdo->prepare(" SELECT * FROM fabric_quality_master WHERE id=:id AND company_id=:cid "); $stmt->execute([':id'=>$id, ':cid'=>$company_id]); $edit = $stmt->fetch(PDO::FETCH_ASSOC); } catch (Throwable $e) { $page_error = $e->getMessage(); } } /* ===== LIST ===== */ $rows = []; if ($page_error === '') { try { $rows = $pdo->prepare(" SELECT * FROM fabric_quality_master WHERE company_id=:cid ORDER BY id DESC "); $rows->execute([':cid'=>$company_id]); $rows = $rows->fetchAll(PDO::FETCH_ASSOC); } catch (Throwable $e) { $page_error = $e->getMessage(); $rows = []; } } ?> <div class="container mt-4"> <div class="card p-3"> <h4>Fabric Quality Master</h4> <?php if ($page_error !== ''): ?> <div class="alert alert-danger"> <?= h($page_error) ?> </div> <?php endif; ?> <?php if ($page_notice !== ''): ?> <div class="alert alert-warning"> <?= h($page_notice) ?> </div> <?php endif; ?> <form method="post" class="row g-3 mb-3"> <input type="hidden" name="edit_id" value="<?= h($edit['id'] ?? '') ?>"> <div class="col-md-5"> <input type="text" name="quality_name" class="form-control" placeholder="Quality Name" value="<?= h($edit['quality_name'] ?? '') ?>" required> </div> <div class="col-md-3"> <input type="text" name="short_name" class="form-control" placeholder="Short Name" value="<?= h($edit['short_name'] ?? '') ?>"> </div> <div class="col-md-2"> <button class="btn btn-primary w-100"> <?= $edit ? 'Update' : 'Add' ?> </button> </div> <div class="col-md-2"> <?php if($edit): ?> <a href="?" class="btn btn-secondary w-100">Cancel</a> <?php endif; ?> </div> </form> <div class="table-responsive"> <table class="table table-bordered table-sm"> <thead> <tr> <th>ID</th> <th>Name</th> <th>Short</th> <th>Status</th> <th>Action</th> </tr> </thead> <tbody> <?php foreach($rows as $r): ?> <tr> <td><?= $r['id'] ?></td> <td><?= h($r['quality_name']) ?></td> <td><?= h($r['short_name'] ?? '') ?></td> <td> <?php $statusValue = 1; if ($status_column !== null) { $statusValue = (int)($r[$status_column] ?? 0); } echo $statusValue === 1 ? 'Active' : 'Inactive'; ?> </td> <td> <a href="?edit=<?= $r['id'] ?>" class="btn btn-sm btn-warning">Edit</a> </td> </tr> <?php endforeach; ?> </tbody> </table> </div> </div> </div> <?php require_once __DIR__ . '/partials/footer.php'; ?>