« Back to History
company_bank.php
|
20260723_000646.php
Initial Domain Snapshot
Copy Code
<?php /* ============================================================================= File: /erp/company_bank_manage.php Purpose: Company-scoped add/edit/delete UI for company_bank_accounts Rules : - Header/Footer include (global CSS only, no inline CSS) - Auth + ACL; company_id session se - PRG redirect: self URL (no hard-coded path) ============================================================================= */ error_reporting(E_ALL); ini_set('display_errors', 1); require __DIR__ . '/modules/auth/page_acl.php'; $ctx = page_require_access('company_bank_edit'); // <- apna ACL slug rakh sakte hain $pdo = $ctx['pdo'] ?? null; if (!$pdo) { require __DIR__ . '/core/db.php'; } // config.php se creds aayenge (as per your rule) $company_id = (int)($ctx['company_id'] ?? 0); if ($company_id <= 0) { http_response_code(400); exit('company_id missing'); } require_once __DIR__ . '/helpers/activity_helper.php'; function h($s){ return htmlspecialchars((string)$s, ENT_QUOTES, 'UTF-8'); } /** Redirect to current page path (PRG: POST->GET), preventing 404s due to base path. */ function redirect_self(string $query=''): void { $path = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH); // e.g. /erp/company_bank_manage.php $loc = $path . ($query !== '' ? ('?' . $query) : ''); header('Location: ' . $loc, true, 303); exit; } /* -------------------- Handle Delete (company-scoped) --------------------- */ if (isset($_GET['del'])) { $del_id = (int)$_GET['del']; if ($del_id > 0) { $st = $pdo->prepare("DELETE FROM company_bank_accounts WHERE id = :id AND company_id = :cid"); $st->execute([':id'=>$del_id, ':cid'=>$company_id]); activity_delete('company', 'company_bank_delete', $del_id, 'Deleted company bank account'); } redirect_self('ok=1'); } /* -------------------- Handle Create/Update (company-scoped) -------------- */ $msg=''; $err=''; if ($_SERVER['REQUEST_METHOD'] === 'POST') { $id = (int)($_POST['id'] ?? 0); // hidden for edit $bank_user_id = trim($_POST['bank_user_id'] ?? ''); $debit_ac_no = trim($_POST['debit_ac_no'] ?? ''); $debit_account = trim($_POST['debit_account'] ?? ''); // basic validation if ($bank_user_id === '' || $debit_ac_no === '' || $debit_account === '') { $err = 'Please fill all required fields.'; } elseif (mb_strlen($bank_user_id) > 191) { $err = 'Bank User ID too long.'; } if ($err === '') { if ($id > 0) { // Update ONLY within this company $sql = "UPDATE company_bank_accounts SET bank_user_id = :bu, debit_ac_no = :dan, debit_account = :da WHERE id = :id AND company_id = :cid"; $st = $pdo->prepare($sql); $st->execute([ ':bu'=>$bank_user_id, ':dan'=>$debit_ac_no, ':da'=>$debit_account, ':id'=>$id, ':cid'=>$company_id ]); activity_update('company', 'company_bank_update', $id, 'Updated company bank account'); } else { // Insert for this company $sql = "INSERT INTO company_bank_accounts (company_id, bank_user_id, debit_ac_no, debit_account, created_at) VALUES (:cid, :bu, :dan, :da, NOW())"; $st = $pdo->prepare($sql); $st->execute([ ':cid'=>$company_id, ':bu'=>$bank_user_id, ':dan'=>$debit_ac_no, ':da'=>$debit_account ]); activity_create('company', 'company_bank_create', (int)$pdo->lastInsertId(), 'Created company bank account'); } redirect_self('ok=1'); } } /* -------------------- Edit mode (company-scoped) ------------------------- */ $edit = null; if (isset($_GET['edit'])) { $edit_id = (int)$_GET['edit']; if ($edit_id > 0) { $st = $pdo->prepare("SELECT * FROM company_bank_accounts WHERE id = :id AND company_id = :cid"); $st->execute([':id'=>$edit_id, ':cid'=>$company_id]); $edit = $st->fetch(PDO::FETCH_ASSOC) ?: null; } } /* -------------------- Fetch list (company-scoped) ------------------------ */ $st = $pdo->prepare("SELECT * FROM company_bank_accounts WHERE company_id = :cid ORDER BY id DESC"); $st->execute([':cid'=>$company_id]); $rows = $st->fetchAll(PDO::FETCH_ASSOC); /* -------------------- Render ------------------------------------------------ */ $PAGE_TITLE = 'Company Bank Accounts'; require __DIR__ . '/partials/header.php'; // must include /public/assets/css/main.css ?> <div class="container py-4"> <div class="card p-4 max-w-xl mx-auto"> <h2 class="mb-2"><?= h($PAGE_TITLE) ?></h2> <?php if (isset($_GET['ok'])): ?> <div class="notice notice-success mb-2">Saved.</div> <?php endif; ?> <?php if ($err): ?> <div class="notice notice-error mb-2"><?= h($err) ?></div> <?php endif; ?> <form method="post" class="form-grid" autocomplete="off"> <input type="hidden" name="id" value="<?= h($edit['id'] ?? 0) ?>"> <!-- company_id not in form; enforced from session --> <div class="form-row"> <label class="form-label">Bank User ID *</label> <input class="input" type="text" name="bank_user_id" value="<?= h($edit['bank_user_id'] ?? '') ?>" required> </div> <div class="form-row"> <label class="form-label">Debit Account No *</label> <input class="input" type="text" name="debit_ac_no" value="<?= h($edit['debit_ac_no'] ?? '') ?>" required> </div> <div class="form-row"> <label class="form-label">Debit Account (Name/Alias) *</label> <input class="input" type="text" name="debit_account" value="<?= h($edit['debit_account'] ?? '') ?>" required> </div> <div class="form-row"> <button class="btn btn-primary" type="submit"><?= $edit ? 'Update' : 'Save' ?></button> <?php if ($edit): ?> <a class="btn" href="<?= h(parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH)) ?>" style="margin-left:.5rem;">New</a> <?php endif; ?> </div> </form> </div> <div class="card p-4 mt-4"> <div class="table-responsive"> <table class="table"> <thead> <tr> <th class="text-left">ID</th> <th class="text-left">Bank User ID</th> <th class="text-left">Debit AC No</th> <th class="text-left">Debit Account</th> <th class="text-left">Created</th> <th class="text-left">Updated</th> <th class="text-left">Actions</th> </tr> </thead> <tbody> <?php if (!$rows): ?> <tr><td colspan="7"><em>No records</em></td></tr> <?php else: foreach ($rows as $r): ?> <tr> <td><?= h($r['id']) ?></td> <td><?= h($r['bank_user_id']) ?></td> <td><?= h($r['debit_ac_no']) ?></td> <td><?= h($r['debit_account']) ?></td> <td><?= h($r['created_at']) ?></td> <td><?= h($r['updated_at']) ?></td> <td> <a class="btn btn-link" href="?edit=<?= h($r['id']) ?>">Edit</a> <a class="btn btn-danger" href="?del=<?= h($r['id']) ?>" onclick="return confirm('Delete this record?')">Delete</a> </td> </tr> <?php endforeach; endif; ?> </tbody> </table> </div> </div> </div> <?php require __DIR__ . '/partials/footer.php'; ?>