« Back to History
yarn_rates_manage.php
|
20260723_000646.php
Initial Domain Snapshot
Copy Code
<?php /* ========================================================= File: /erp/yarn_rates_manage.php Features: Bootstrap 5, Modal-based Edit, Delete-then-Insert Pattern ========================================================= */ require_once __DIR__ . '/modules/auth/page_acl.php'; require_once __DIR__ . '/modules/activity/activity_logger.php'; $ctx = page_require_access('yarn_rates_manage'); $u = $ctx['user'] ?? null; $company_id = (int)($ctx['company_id'] ?? 0); $pdo = $ctx['pdo'] ?? null; if (session_status() === PHP_SESSION_NONE) { session_start(); } if (empty($_SESSION['csrf_token'])) { $_SESSION['csrf_token'] = bin2hex(random_bytes(16)); } $csrf = $_SESSION['csrf_token']; $messages = []; /* ---- 1. Handle POST Actions (Delete-then-Insert) ---- */ if ($_SERVER['REQUEST_METHOD'] === 'POST') { if (!isset($_POST['csrf']) || $_POST['csrf'] !== $csrf) { $messages[] = ['type'=>'danger','text'=>'CSRF token invalid.']; } else { $action = $_POST['action'] ?? ''; $id = (int)($_POST['id'] ?? 0); $denier = trim($_POST['denier'] ?? ''); $color = trim($_POST['color'] ?? ''); $y_type = trim($_POST['yarn_type'] ?? '0'); $rate = $_POST['rate'] !== '' ? (float)str_replace(',', '', $_POST['rate']) : null; $now = date('Y-m-d H:i:s'); try { if ($action === 'add' || $action === 'edit') { if ($denier === '' || $color === '' || $rate === null) { throw new Exception("Denier, Color and Rate are required."); } $pdo->beginTransaction(); // Pattern: Delete-then-Insert if ($action === 'edit' && $id > 0) { $del = $pdo->prepare("DELETE FROM yarn_rates WHERE id = ? AND company_id = ?"); $del->execute([$id, $company_id]); } else { // Pre-emptively clear existing rate for this specific combo $del = $pdo->prepare("DELETE FROM yarn_rates WHERE denier=? AND color=? AND company_id=?"); $del->execute([$denier, $color, $company_id]); } $ins = $pdo->prepare("INSERT INTO yarn_rates (company_id, yarn_type, denier, color, rate, created_at, updated_at) VALUES (?,?,?,?,?,?,?)"); $ins->execute([$company_id, $y_type, $denier, $color, $rate, $now, $now]); $insert_id = (int)$pdo->lastInsertId(); $pdo->commit(); activity_log([ 'company_id' => $company_id ?? 0, 'user_id' => $u['id'] ?? ($_SESSION['user_id'] ?? 0), 'module' => 'yarn', 'action_name' => $action === 'edit' ? 'edit' : 'create', 'entity_type' => 'yarn_rate', 'entity_id' => $insert_id ?? 0, 'remarks' => $action === 'edit' ? 'Yarn rate updated' : 'Yarn rate created' ]); $messages[] = ['type'=>'success','text'=>"Rate successfully saved."]; } elseif ($action === 'delete') { $stmt = $pdo->prepare("DELETE FROM yarn_rates WHERE id = ? AND company_id = ?"); $stmt->execute([$id, $company_id]); activity_log([ 'company_id' => $company_id ?? 0, 'user_id' => $u['id'] ?? ($_SESSION['user_id'] ?? 0), 'module' => 'yarn', 'action_name' => 'delete', 'entity_type' => 'yarn_rate', 'entity_id' => $id ?? 0, 'remarks' => 'Yarn rate deleted' ]); $messages[] = ['type'=>'success','text'=>'Record deleted.']; } } catch (Exception $ex) { if($pdo->inTransaction()) $pdo->rollBack(); $messages[] = ['type'=>'danger','text'=>$ex->getMessage()]; } } } /* ---- 2. Fetch Data ---- */ $rowsStmt = $pdo->prepare("SELECT * FROM yarn_rates WHERE company_id = ? ORDER BY denier, color"); $rowsStmt->execute([$company_id]); $rates = $rowsStmt->fetchAll(PDO::FETCH_ASSOC); $optStmt = $pdo->prepare("SELECT DISTINCT denier, color FROM yarn_company_data WHERE company_id = ? ORDER BY denier, color"); $optStmt->execute([$company_id]); $companyOptions = $optStmt->fetchAll(PDO::FETCH_ASSOC); function h($s){ return htmlspecialchars((string)$s, ENT_QUOTES, 'UTF-8'); } require_once __DIR__.'/partials/header.php'; ?> <div class="container-fluid py-4" style="max-width: 1200px;"> <div class="d-flex justify-content-between align-items-center mb-4"> <h2 class="fw-bold text-dark mb-0">Yarn Rates Manager</h2> <span class="badge bg-dark rounded-pill shadow-sm">Total Records: <?= count($rates) ?></span> </div> <?php foreach($messages as $m): ?> <div class="alert alert-<?= $m['type'] ?> alert-dismissible fade show border-0 shadow-sm" role="alert"> <i class="bi bi-info-circle me-2"></i><?= h($m['text']) ?> <button type="button" class="btn-close" data-bs-dismiss="alert"></button> </div> <?php endforeach; ?> <div class="card border-0 shadow-sm mb-4"> <div class="card-body p-4 bg-light"> <h5 class="fw-bold mb-3">Add New Rate</h5> <form method="post" class="row g-3 align-items-end"> <input type="hidden" name="csrf" value="<?= h($csrf) ?>"> <input type="hidden" name="action" value="add"> <div class="col-md-3"> <label class="form-label small fw-bold text-muted">Denier</label> <select name="denier" class="form-select border-2" required> <option value="">-- select --</option> <?php foreach($companyOptions as $opt): ?> <option value="<?= h($opt['denier']) ?>"><?= h($opt['denier']) ?></option> <?php endforeach; ?> </select> </div> <div class="col-md-3"> <label class="form-label small fw-bold text-muted">Color</label> <select name="color" class="form-select border-2" required> <option value="">-- select --</option> <?php foreach($companyOptions as $opt): ?> <option value="<?= h($opt['color']) ?>"><?= h($opt['color']) ?></option> <?php endforeach; ?> </select> </div> <div class="col-md-2"> <label class="form-label small fw-bold text-muted">Yarn Type</label> <input type="text" name="yarn_type" class="form-control border-2" value="0"> </div> <div class="col-md-2"> <label class="form-label small fw-bold text-muted">Rate (₹)</label> <input type="number" step="0.01" name="rate" class="form-control border-2" required placeholder="0.00"> </div> <div class="col-md-2"> <button type="submit" class="btn btn-primary w-100 fw-bold shadow-sm">Add Rate</button> </div> </form> </div> </div> <div class="card border-0 shadow-sm overflow-hidden"> <div class="table-responsive"> <table class="table table-hover align-middle mb-0" id="ratesTable"> <thead class="table-light"> <tr> <th class="ps-3 text-uppercase small fw-bold">Denier</th> <th class="text-uppercase small fw-bold">Color</th> <th class="text-uppercase small fw-bold">Yarn Type</th> <th class="text-uppercase small fw-bold text-primary">Rate (₹)</th> <th class="text-uppercase small fw-bold text-muted">Updated</th> <th class="text-end pe-3">Actions</th> </tr> </thead> <tbody> <?php if (!$rates): ?> <tr><td colspan="6" class="text-center py-5 text-muted small">No rates found in the system.</td></tr> <?php else: foreach($rates as $r): ?> <tr> <td class="ps-3 fw-bold"><?= h($r['denier']) ?></td> <td><?= h($r['color']) ?></td> <td><span class="badge bg-white text-dark border"><?= h($r['yarn_type']) ?></span></td> <td class="fw-bold text-primary">₹ <?= number_format($r['rate'], 2) ?></td> <td class="text-muted small"><?= date('d M, Y', strtotime($r['updated_at'])) ?></td> <td class="text-end pe-3"> <button type="button" class="btn btn-sm btn-outline-dark btn-edit me-1 px-3" data-bs-toggle="modal" data-bs-target="#editModal" data-id="<?= $r['id'] ?>" data-denier="<?= h($r['denier']) ?>" data-color="<?= h($r['color']) ?>" data-ytype="<?= h($r['yarn_type']) ?>" data-rate="<?= h($r['rate']) ?>"> Edit </button> <form method="post" class="d-inline" onsubmit="return confirm('Permanently delete this record?');"> <input type="hidden" name="csrf" value="<?= h($csrf) ?>"> <input type="hidden" name="action" value="delete"> <input type="hidden" name="id" value="<?= (int)$r['id'] ?>"> <button class="btn btn-sm btn-outline-danger" type="submit">Delete</button> </form> </td> </tr> <?php endforeach; endif; ?> </tbody> </table> </div> </div> </div> <div class="modal fade" id="editModal" tabindex="-1" aria-labelledby="editModalLabel" aria-hidden="true"> <div class="modal-dialog modal-dialog-centered"> <form method="post" class="modal-content border-0 shadow-lg"> <div class="modal-header bg-dark text-white"> <h5 class="modal-title fw-bold" id="editModalLabel">Update Yarn Rate</h5> <button type="button" class="btn-close btn-close-white" data-bs-dismiss="modal" aria-label="Close"></button> </div> <div class="modal-body p-4"> <input type="hidden" name="csrf" value="<?= h($csrf) ?>"> <input type="hidden" name="action" value="edit"> <input type="hidden" name="id" id="edit_id"> <div class="mb-3"> <label class="form-label fw-bold small">Denier</label> <select name="denier" id="edit_denier" class="form-select border-2" required> <?php foreach($companyOptions as $opt): ?> <option value="<?= h($opt['denier']) ?>"><?= h($opt['denier']) ?></option> <?php endforeach; ?> </select> </div> <div class="mb-3"> <label class="form-label fw-bold small">Color</label> <select name="color" id="edit_color" class="form-select border-2" required> <?php foreach($companyOptions as $opt): ?> <option value="<?= h($opt['color']) ?>"><?= h($opt['color']) ?></option> <?php endforeach; ?> </select> </div> <div class="row"> <div class="col-6 mb-3"> <label class="form-label fw-bold small">Yarn Type</label> <input type="text" name="yarn_type" id="edit_ytype" class="form-control border-2"> </div> <div class="col-6 mb-3"> <label class="form-label fw-bold small">Rate (₹)</label> <input type="number" step="0.01" name="rate" id="edit_rate" class="form-control border-2" required> </div> </div> </div> <div class="modal-footer bg-light border-0"> <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Close</button> <button type="submit" class="btn btn-primary px-4 fw-bold">Save Changes</button> </div> </form> </div> </div> <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script> <script> document.addEventListener('DOMContentLoaded', function() { // Select all buttons with the class .btn-edit const editButtons = document.querySelectorAll('.btn-edit'); editButtons.forEach(button => { button.addEventListener('click', function() { // Get data from data attributes const id = this.getAttribute('data-id'); const denier = this.getAttribute('data-denier'); const color = this.getAttribute('data-color'); const ytype = this.getAttribute('data-ytype'); const rate = this.getAttribute('data-rate'); // Set values into modal inputs document.getElementById('edit_id').value = id; document.getElementById('edit_denier').value = denier; document.getElementById('edit_color').value = color; document.getElementById('edit_ytype').value = ytype; document.getElementById('edit_rate').value = rate; }); }); }); </script> <?php require_once __DIR__.'/partials/footer.php'; ?>