« Back to History
open_box_edit.php
|
20260723_000646.php
Initial Domain Snapshot
Copy Code
<?php require_once __DIR__ . '/../modules/auth/page_acl.php'; $ctx = page_require_access('mesr_stock_entry'); $pdo = $ctx['pdo']; $company_id = (int)$ctx['company_id']; // 1. URL Parameters (GET) $month = $_GET['month'] ?? ''; // Example: 2026-03-01 $location_id = (int)($_GET['location_id'] ?? 0); $machine_id = (int)($_GET['machine_id'] ?? 0); // HTML input type="month" ke liye format (YYYY-MM) $display_month = !empty($month) ? substr($month, 0, 7) : ''; if (!$month || !$location_id || !$machine_id) { die("Error: URL Parameters missing (Month/Location/Machine). Check your link."); } // 2. SAVE LOGIC (POST) if ($_SERVER['REQUEST_METHOD'] === 'POST') { // Form se aane wala month (YYYY-MM) ko DB format (YYYY-MM-01) mein badlo $post_month = $_POST['month'] . '-01'; $pdo->beginTransaction(); try { // Purana data saaf karo (Usi specific machine/location/month ka) $del = $pdo->prepare("DELETE FROM mesr_yarn_in_open_box WHERE company_id = ? AND month = ? AND location_id = ? AND machine_id = ?"); $del->execute([$company_id, $post_month, $location_id, $machine_id]); $ins = $pdo->prepare("INSERT INTO mesr_yarn_in_open_box (company_id, month, location_id, machine_id, denier, color, total_cons, per_cone_weight, empty_cone_weight, created_at) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, NOW())"); foreach ($_POST['denier'] as $i => $d) { if (empty($d)) continue; $ins->execute([ $company_id, $post_month, $location_id, $machine_id, $d, $_POST['color'][$i], (float)$_POST['total_cons'][$i], (float)$_POST['per_cone_weight'][$i], (float)$_POST['empty_cone_weight'][$i] ]); } $pdo->commit(); // Success ke baad wapas list page par bhej do header("Location: mesr_yarn_in_open_box.php?success=1"); exit; } catch (Exception $e) { $pdo->rollBack(); die("Database Error: " . $e->getMessage()); } } // 3. FETCH DATA (Table mein dikhane ke liye) $stmt = $pdo->prepare("SELECT * FROM mesr_yarn_in_open_box WHERE company_id=? AND month=? AND location_id=? AND machine_id=?"); $stmt->execute([$company_id, $month, $location_id, $machine_id]); $rows = $stmt->fetchAll(PDO::FETCH_ASSOC); // Dropdowns $deniers = $pdo->query("SELECT value FROM mesr_denier_data WHERE company_id=$company_id AND type='denier' ORDER BY CAST(value AS UNSIGNED)")->fetchAll(PDO::FETCH_COLUMN); $colors = $pdo->query("SELECT value FROM mesr_denier_data WHERE company_id=$company_id AND type='color' ORDER BY value")->fetchAll(PDO::FETCH_COLUMN); require_once __DIR__ . '/../partials/header.php'; ?> <div class="container mt-3"> <div class="card shadow-sm border-0"> <div class="card-header bg-primary text-white d-flex justify-content-between"> <h5 class="mb-0">Edit Stock Record</h5> <small>Loc ID: <?= $location_id ?> | Mac ID: <?= $machine_id ?></small> </div> <div class="card-body"> <form method="POST" action="open_box_edit.php?month=<?= $month ?>&location_id=<?= $location_id ?>&machine_id=<?= $machine_id ?>"> <div class="row mb-3"> <div class="col-md-3"> <label class="fw-bold small">Reporting Month</label> <input type="month" name="month" value="<?= $display_month ?>" class="form-control" required> </div> </div> <table class="table table-bordered align-middle" id="tbl"> <thead class="table-dark small text-center"> <tr> <th>Denier</th> <th>Color</th> <th>No. of Cones</th> <th>Per Cone (kg)</th> <th>Empty Cone (kg)</th> <th>Action</th> </tr> </thead> <tbody> <?php if($rows): foreach($rows as $r): ?> <tr> <td> <select name="denier[]" class="form-select border-0"> <?php foreach($deniers as $d): ?> <option value="<?= $d ?>" <?= $d==$r['denier']?'selected':'' ?>><?= $d ?></option> <?php endforeach; ?> </select> </td> <td> <select name="color[]" class="form-select border-0"> <?php foreach($colors as $c): ?> <option value="<?= $c ?>" <?= $c==$r['color']?'selected':'' ?>><?= $c ?></option> <?php endforeach; ?> </select> </td> <td><input type="number" step="0.001" name="total_cons[]" value="<?= $r['total_cons'] ?>" class="form-control border-0 text-center"></td> <td><input type="number" step="0.001" name="per_cone_weight[]" value="<?= $r['per_cone_weight'] ?>" class="form-control border-0 text-center"></td> <td><input type="number" step="0.001" name="empty_cone_weight[]" value="<?= $r['empty_cone_weight'] ?>" class="form-control border-0 text-center"></td> <td class="text-center"> <button type="button" class="btn btn-sm btn-outline-danger border-0" onclick="removeRow(this)">X</button> </td> </tr> <?php endforeach; else: ?> <tr> <td><select name="denier[]" class="form-select border-0"><?php foreach($deniers as $d) echo "<option value='$d'>$d</option>"; ?></select></td> <td><select name="color[]" class="form-select border-0"><?php foreach($colors as $c) echo "<option value='$c'>$c</option>"; ?></select></td> <td><input type="number" step="0.001" name="total_cons[]" class="form-control border-0 text-center"></td> <td><input type="number" step="0.001" name="per_cone_weight[]" class="form-control border-0 text-center"></td> <td><input type="number" step="0.001" name="empty_cone_weight[]" class="form-control border-0 text-center"></td> <td class="text-center"><button type="button" class="btn btn-sm btn-outline-danger border-0" onclick="removeRow(this)">X</button></td> </tr> <?php endif; ?> </tbody> </table> <div class="mt-3"> <button type="button" onclick="addRow()" class="btn btn-success btn-sm">+ Add More</button> <button type="submit" class="btn btn-primary float-end px-5 fw-bold">UPDATE STOCK</button> </div> </form> </div> </div> </div> <script> function addRow(){ const tbody = document.querySelector("#tbl tbody"); const firstRow = tbody.querySelector("tr"); if(firstRow) { const newRow = firstRow.cloneNode(true); newRow.querySelectorAll('input').forEach(i => i.value = ''); tbody.appendChild(newRow); } } function removeRow(btn){ const tbody = document.querySelector("#tbl tbody"); if(tbody.querySelectorAll("tr").length > 1) { btn.closest('tr').remove(); } } </script> <?php require_once __DIR__ . '/../partials/footer.php'; ?>