« Back to History
rapier_daily_production.php
|
20260721_154033.php
Initial Bulk Import
Copy Code
<?php require_once __DIR__ . '/modules/auth/page_acl.php'; // ACL aur context initialization $ctx = page_require_access('rapier_daily_production'); $pdo = $ctx['pdo'] ?? null; $company_id = (int)($ctx['company_id'] ?? 0); // Mandatory company-wise segregation $u = $ctx['user'] ?? null; $user_role = strtolower((string)($u['role'] ?? $u['role_name'] ?? $u['user_role'] ?? '')); $is_folder_user = ($user_role === 'folder'); $today_date = date('Y-m-d'); if (!$pdo) { require_once __DIR__ . '/core/db.php'; $pdo = $GLOBALS['pdo']; } /* CSRF Protection Setup */ if (empty($_SESSION['csrf_token'])) { $_SESSION['csrf_token'] = bin2hex(random_bytes(32)); } $msg = ''; $msg_type = 'success'; /* ── POST Handler: Save/Update Production ── */ if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['save_production'])) { if (!hash_equals($_SESSION['csrf_token'], $_POST['csrf_token'] ?? '')) { die('Invalid CSRF token'); } $id = (int)($_POST['id'] ?? 0); $production_date = $_POST['production_date'] ?? date('Y-m-d'); $meter = (float)($_POST['meter'] ?? 0); $saree = (float)($_POST['saree'] ?? 0); $remarks = substr(trim($_POST['remarks'] ?? ''), 0, 500); try { $pdo->beginTransaction(); // Transaction management // 1. Delete-then-Insert logic for updates if ($id > 0) { $existing_st = $pdo->prepare("SELECT production_date FROM rapier_daily_production WHERE id = ? AND company_id = ? LIMIT 1"); $existing_st->execute([$id, $company_id]); $existing_row = $existing_st->fetch(PDO::FETCH_ASSOC); if (!$existing_row) { throw new Exception('Edit record nahi mila.'); } if ($is_folder_user) { $existing_date = (string)($existing_row['production_date'] ?? ''); if ($existing_date !== $today_date || $production_date !== $today_date) { throw new Exception('Folder user sirf aaj ki production entry edit kar sakta hai.'); } } $del = $pdo->prepare("DELETE FROM rapier_daily_production WHERE id = ? AND company_id = ?"); $del->execute([$id, $company_id]); } // 2. Fresh Insertion with mandatory company_id $sql = "INSERT INTO rapier_daily_production (company_id, production_date, meter, saree, remarks, created_at) VALUES (?, ?, ?, ?, ?, NOW())"; $stmt = $pdo->prepare($sql); $stmt->execute([$company_id, $production_date, $meter, $saree, $remarks]); $pdo->commit(); $msg = "Production entry saved successfully!"; if ($id === 0) { $next_date = date('Y-m-d', strtotime($production_date . ' +1 day')); header('Location: rapier_daily_production.php?production_date=' . urlencode($next_date) . '&msg=' . urlencode($msg) . '&msg_type=success'); exit; } } catch (Exception $e) { if ($pdo->inTransaction()) $pdo->rollBack(); $msg = "Error: " . $e->getMessage(); $msg_type = 'danger'; } } if (isset($_GET['msg']) && $_GET['msg'] !== '') { $msg = (string)$_GET['msg']; $msg_type = ($_GET['msg_type'] ?? 'success') === 'danger' ? 'danger' : 'success'; } /* ── Fetch for Edit ── */ $edit_id = isset($_GET['edit_id']) ? (int)$_GET['edit_id'] : 0; $edit_row = null; $edit_locked = false; if ($edit_id > 0) { $st = $pdo->prepare("SELECT * FROM rapier_daily_production WHERE id = ? AND company_id = ? LIMIT 1"); $st->execute([$edit_id, $company_id]); $edit_row = $st->fetch(PDO::FETCH_ASSOC); if ($edit_row && $is_folder_user && (string)($edit_row['production_date'] ?? '') !== $today_date) { $edit_locked = true; $edit_row = null; $msg = 'Folder user sirf aaj ki production entry edit kar sakta hai.'; $msg_type = 'danger'; } } /* ── Fetch Recent Records (Filtered by Company) ── */ $records = []; try { $st = $pdo->prepare("SELECT * FROM rapier_daily_production WHERE company_id = ? ORDER BY production_date DESC, id DESC LIMIT 30"); $st->execute([$company_id]); $records = $st->fetchAll(PDO::FETCH_ASSOC); } catch (Exception $e) { $msg = $e->getMessage(); } /* Header Include */ $page_title = 'Rapier Daily Production'; require_once __DIR__ . '/partials/header.php'; ?> <div class="container-fluid mt-3" style="max-width: 900px;"> <!-- Entry Form --> <div class="card shadow-sm border-0 mb-4"> <div class="card-header bg-dark text-white py-3"> <h6 class="mb-0 fw-bold"><?= $edit_row ? 'Edit Production Entry' : 'New Rapier Production Entry' ?></h6> </div> <div class="card-body"> <?php if ($msg): ?> <div class="alert alert-<?= $msg_type ?> alert-dismissible fade show py-2"> <?= htmlspecialchars($msg) ?> <button type="button" class="btn-close" data-bs-dismiss="alert"></button> </div> <?php endif; ?> <form method="POST"> <input type="hidden" name="csrf_token" value="<?= $_SESSION['csrf_token'] ?>"> <?php if ($edit_row): ?> <input type="hidden" name="id" value="<?= $edit_id ?>"> <?php endif; ?> <?php $form_production_date = $edit_row['production_date'] ?? ($_GET['production_date'] ?? $today_date); ?> <div class="row g-3"> <div class="col-md-4"> <label class="form-label small fw-bold">Production Date</label> <input type="date" name="production_date" class="form-control" value="<?= htmlspecialchars($form_production_date) ?>" required> </div> <div class="col-md-4"> <label class="form-label small fw-bold">Total Meter</label> <input type="number" step="0.01" name="meter" class="form-control" placeholder="0.00" value="<?= htmlspecialchars($edit_row['meter'] ?? '') ?>" required> </div> <div class="col-md-4"> <label class="form-label small fw-bold">Saree Pieces</label> <input type="number" step="0.01" name="saree" class="form-control" placeholder="0.00" value="<?= htmlspecialchars($edit_row['saree'] ?? '') ?>" required> </div> <div class="col-md-12"> <label class="form-label small fw-bold">Remarks</label> <input type="text" name="remarks" class="form-control" placeholder="Optional notes..." value="<?= htmlspecialchars($edit_row['remarks'] ?? '') ?>"> </div> </div> <div class="mt-4"> <button type="submit" name="save_production" class="btn btn-primary px-4 shadow-sm"> <?= $edit_row ? 'Update Production' : 'Save Production' ?> </button> <?php if ($edit_row): ?> <a href="rapier_daily_production.php" class="btn btn-outline-secondary px-4 ms-2">Cancel</a> <?php endif; ?> </div> </form> </div> </div> <!-- Recent Records Table --> <div class="card shadow-sm border-0"> <div class="card-header bg-white"> <h6 class="mb-0 fw-bold text-muted small">Recent Records</h6> </div> <div class="table-responsive"> <table class="table table-hover align-middle mb-0 small"> <thead class="table-light"> <tr> <th>Date</th> <th class="text-end">Meter</th> <th class="text-end">Saree</th> <th>Remarks</th> <th class="text-center">Action</th> </tr> </thead> <tbody> <?php if (empty($records)): ?> <tr><td colspan="5" class="text-center text-muted py-4">Koi record nahi mila.</td></tr> <?php else: foreach ($records as $r): ?> <?php $row_date = (string)($r['production_date'] ?? ''); $can_edit_row = !$is_folder_user || $row_date === $today_date; ?> <tr> <td class="fw-bold"><?= date('d-m-Y', strtotime($r['production_date'])) ?></td> <td class="text-end font-monospace"><?= number_format($r['meter'], 2) ?></td> <td class="text-end font-monospace"><?= number_format($r['saree'], 2) ?></td> <td class="text-muted"><?= htmlspecialchars($r['remarks']) ?></td> <td class="text-center"> <?php if ($can_edit_row): ?> <a href="?edit_id=<?= $r['id'] ?>" class="btn btn-sm btn-outline-primary py-0">Edit</a> <?php else: ?> <span class="badge bg-secondary-subtle text-secondary border">Same day only</span> <?php endif; ?> </td> </tr> <?php endforeach; endif; ?> </tbody> </table> </div> </div> </div> <?php require_once __DIR__ . '/partials/footer.php'; ?>