« Back to History
packing_material_party_add.php
|
20260722_120325.php
Initial Domain Snapshot
Copy Code
<?php require_once __DIR__ . '/modules/auth/page_acl.php'; $ctx = page_require_access('packing_material_party_add'); $pdo = $ctx['pdo']; $company_id = $ctx['company_id']; $u = $ctx['user']; function h($value) { return htmlspecialchars((string)$value, ENT_QUOTES, 'UTF-8'); } function normalize_party_status($value): int { if (is_numeric($value)) { return ((int)$value) === 1 ? 1 : 0; } $value = strtolower(trim((string)$value)); return in_array($value, ['1', 'active', 'yes'], true) ? 1 : 0; } $form = [ 'id' => 0, 'name' => '', 'gst_no' => '', 'address' => '', 'contact_no' => '', 'status' => 1, ]; $flash = null; /* ========================= EDIT FETCH ========================= */ if (isset($_GET['edit'])) { $editStmt = $pdo->prepare("SELECT * FROM paking_material_party WHERE id = ? AND company_id = ? LIMIT 1"); $editStmt->execute([(int)$_GET['edit'], $company_id]); $editRow = $editStmt->fetch(PDO::FETCH_ASSOC); if ($editRow) { $form = [ 'id' => (int)$editRow['id'], 'name' => (string)($editRow['name'] ?? ''), 'gst_no' => (string)($editRow['gst_no'] ?? ''), 'address' => (string)($editRow['address'] ?? ''), 'contact_no' => (string)($editRow['contact_no'] ?? ''), 'status' => normalize_party_status($editRow['status'] ?? 1), ]; } } /* ========================= SAVE / UPDATE LOGIC ========================= */ if ($_SERVER['REQUEST_METHOD'] === 'POST') { $id = (int)($_POST['id'] ?? 0); $name = trim($_POST['name'] ?? ''); $gst_no = trim($_POST['gst_no'] ?? ''); $address = trim($_POST['address'] ?? ''); $contact_no = trim($_POST['contact_no'] ?? ''); $status = normalize_party_status($_POST['status'] ?? 1); $form = [ 'id' => $id, 'name' => $name, 'gst_no' => $gst_no, 'address' => $address, 'contact_no' => $contact_no, 'status' => $status, ]; if ($name !== '') { if ($id > 0) { $stmt = $pdo->prepare(" UPDATE paking_material_party SET name = ?, gst_no = ?, address = ?, contact_no = ?, status = ? WHERE id = ? AND company_id = ? "); $stmt->execute([ $name, $gst_no, $address, $contact_no, $status, $id, $company_id ]); $flash = ['type' => 'success', 'message' => 'Party Updated']; } else { $stmt = $pdo->prepare(" INSERT INTO paking_material_party (company_id, name, gst_no, address, contact_no, status) VALUES (?, ?, ?, ?, ?, ?) "); $stmt->execute([ $company_id, $name, $gst_no, $address, $contact_no, $status ]); $flash = ['type' => 'success', 'message' => 'Party Saved']; } $form = [ 'id' => 0, 'name' => '', 'gst_no' => '', 'address' => '', 'contact_no' => '', 'status' => 1, ]; } else { $flash = ['type' => 'danger', 'message' => 'Name is required']; } } /* ========================= FETCH DATA ========================= */ $data = $pdo->prepare(" SELECT * FROM paking_material_party WHERE company_id = ? ORDER BY id DESC "); $data->execute([$company_id]); $rows = $data->fetchAll(PDO::FETCH_ASSOC); require_once __DIR__ . '/partials/header.php'; ?> <div class="container mt-3"> <h4 class="mb-3">Packing Material Party</h4> <?php if ($flash): ?> <div class="alert alert-<?= h($flash['type']) ?>"><?= h($flash['message']) ?></div> <?php endif; ?> <!-- FORM --> <form method="POST" class="card p-3 mb-4"> <input type="hidden" name="id" value="<?= h($form['id']) ?>"> <div class="row"> <div class="col-md-3"> <label>Party Name *</label> <input type="text" name="name" class="form-control" value="<?= h($form['name']) ?>" required> </div> <div class="col-md-3"> <label>GST No</label> <input type="text" name="gst_no" class="form-control" value="<?= h($form['gst_no']) ?>"> </div> <div class="col-md-3"> <label>Contact No</label> <input type="text" name="contact_no" class="form-control" value="<?= h($form['contact_no']) ?>"> </div> <div class="col-md-3"> <label>Status</label> <select name="status" class="form-control"> <option value="1" <?= (int)$form['status'] === 1 ? 'selected' : '' ?>>Active</option> <option value="0" <?= (int)$form['status'] === 0 ? 'selected' : '' ?>>Inactive</option> </select> </div> <div class="col-md-12 mt-2"> <label>Address</label> <textarea name="address" class="form-control"><?= h($form['address']) ?></textarea> </div> </div> <div class="mt-3 d-flex gap-2"> <button class="btn btn-primary"><?= $form['id'] > 0 ? 'Update Party' : 'Save Party' ?></button> <?php if ($form['id'] > 0): ?> <a href="<?= h($_SERVER['PHP_SELF']) ?>" class="btn btn-secondary">Cancel</a> <?php endif; ?> </div> </form> <!-- LIST --> <div class="card p-3"> <h5>Party List</h5> <table class="table table-bordered table-sm"> <thead> <tr> <th>Name</th> <th>GST</th> <th>Contact</th> <th>Status</th> <th>Address</th> <th>Action</th> </tr> </thead> <tbody> <?php foreach ($rows as $r): ?> <tr> <td><?= h($r['name']) ?></td> <td><?= h($r['gst_no']) ?></td> <td><?= h($r['contact_no']) ?></td> <td><?= ((int)normalize_party_status($r['status'] ?? 0) === 1) ? 'Active' : 'Inactive' ?></td> <td><?= h($r['address']) ?></td> <td> <a href="?edit=<?= (int)$r['id'] ?>" class="btn btn-sm btn-outline-primary">Edit</a> </td> </tr> <?php endforeach; ?> </tbody> </table> </div> </div> <?php require_once __DIR__ . '/partials/footer.php'; ?>