« Back to History
mesr_yarn_in_machine.php
|
20260921_175631.php
Initial Domain Snapshot
Copy Code
<?php require_once __DIR__ . '/../modules/auth/page_acl.php'; $ctx = page_require_access('mesr_yarn_in_machine'); require_once __DIR__ . '/../helpers/activity_helper.php'; $pdo = $ctx['pdo']; $company_id = (int)$ctx['company_id']; // --- 1. SAVE LOGIC (AJAX) --- if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['action_save'])) { $month = $_POST['month'] . '-01'; $location_id = (int)$_POST['location_id']; $machine_id = (int)$_POST['machine_id']; $pdo->beginTransaction(); try { $del = $pdo->prepare("DELETE FROM mesr_yarn_in_machine WHERE company_id = ? AND month = ? AND location_id = ? AND machine_id = ?"); $del->execute([$company_id, $month, $location_id, $machine_id]); $ins = $pdo->prepare("INSERT INTO mesr_yarn_in_machine (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 => $denier) { if (empty($denier)) continue; $ins->execute([$company_id, $month, $location_id, $machine_id, $denier, $_POST['color'][$i], (float)$_POST['total_cons'][$i], (float)$_POST['per_cone_weight'][$i], (float)$_POST['empty_cone_weight'][$i]]); } $pdo->commit(); $entryCount = 0; if (isset($_POST['denier']) && is_array($_POST['denier'])) { foreach ($_POST['denier'] as $d) { if (!empty($d)) { $entryCount++; } } } log_activity([ 'activity_type' => 'entry', 'module_name' => 'mesr_yarn_in_machine', 'action_name' => 'save', 'activity_note' => "Saved machine yarn stock | Month: {$month}, Location: {$location_id}, Machine: {$machine_id}, Rows: {$entryCount}", 'reference_id' => $machine_id, ]); echo "Success: Saved"; exit; } catch (Throwable $e) { if ($pdo->inTransaction()) $pdo->rollBack(); http_response_code(500); echo "Error: " . $e->getMessage(); exit; } } // --- 2. DATA FETCHING --- $locations = $pdo->prepare("SELECT id, location_name FROM mesr_data_location WHERE company_id = ? AND is_active = 1 ORDER BY location_name"); $locations->execute([$company_id]); $locations = $locations->fetchAll(PDO::FETCH_ASSOC); $machines = $pdo->prepare("SELECT id, machine_name FROM mesr_machine WHERE company_id = ? ORDER BY machine_name"); $machines->execute([$company_id]); $machines = $machines->fetchAll(PDO::FETCH_ASSOC); $deniers = $pdo->prepare("SELECT value FROM mesr_denier_data WHERE company_id = ? AND type = 'denier' ORDER BY value"); $deniers->execute([$company_id]); $deniers = $deniers->fetchAll(PDO::FETCH_COLUMN); $colors = $pdo->prepare("SELECT value FROM mesr_denier_data WHERE company_id = ? AND type = 'color' ORDER BY value"); $colors->execute([$company_id]); $colors = $colors->fetchAll(PDO::FETCH_COLUMN); // --- TAB NAME FIX --- $page_title = "🏭 Yarn in Machine"; // Ab browser tab mein "Machine" dikhega require_once __DIR__ . '/../partials/header.php'; ?> <div class="container-fluid mt-3"> <div id="alert_anchor"></div> <div class="d-flex justify-content-between align-items-center mb-4 border-bottom pb-2"> <h2 class="h4 mb-0 fw-bold">Machine <small class="text-muted">| Yarn Stock Entry</small></h2> </div> <form id="form_machine"> <input type="hidden" name="action_save" value="1"> <div class="row g-3 mb-4"> <div class="col-md-4"> <label class="form-label fw-bold">Month</label> <input type="month" name="month" id="h_month" value="<?= date('Y-m') ?>" class="form-control shadow-none" required> </div> <div class="col-md-4"> <label class="form-label fw-bold">Location</label> <select name="location_id" id="h_loc" class="form-select shadow-none" required> <option value="">-- Select Location --</option> <?php foreach($locations as $l) echo "<option value='{$l['id']}'>{$l['location_name']}</option>"; ?> </select> </div> <div class="col-md-4"> <label class="form-label fw-bold">Machine Name</label> <select name="machine_id" id="h_mac" class="form-select shadow-none" required> <option value="">-- Select Machine --</option> <?php foreach($machines as $m) echo "<option value='{$m['id']}'>{$m['machine_name']}</option>"; ?> </select> </div> </div> <div class="card shadow-sm border-0"> <div class="table-responsive"> <table class="table table-bordered align-middle mb-0" id="tbl_machine"> <thead class="table-light small text-uppercase"> <tr> <th style="width: 18%;">Denier</th> <th style="width: 18%;">Color</th> <th class="text-center">Total Cones</th> <th class="text-center">Per Cone Wt</th> <th class="text-center">Empty Cone Wt</th> <th class="text-center" style="width: 50px;">X</th> </tr> </thead> <tbody> <tr> <td> <select name="denier[]" class="form-select form-select-sm d-sel shadow-none" onchange="applyWeight(this)" required> <option value="">-- Select --</option> <?php foreach($deniers as $d) echo "<option value='$d'>$d</option>"; ?> </select> </td> <td> <select name="color[]" class="form-select form-select-sm shadow-none" required> <option value="">-- Color --</option> <?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 form-control-sm text-center shadow-none" required></td> <td><input type="number" step="0.001" name="per_cone_weight[]" class="form-control form-control-sm text-center shadow-none" required></td> <td><input type="number" step="0.001" name="empty_cone_weight[]" class="form-control form-control-sm text-center shadow-none e-wt" required></td> <td class="text-center"><button type="button" class="btn btn-link text-danger p-0" onclick="removeRow(this)">✖</button></td> </tr> </tbody> </table> </div> <div class="card-footer bg-white d-flex justify-content-between"> <button type="button" class="btn btn-outline-success btn-sm" onclick="addRow()">+ Add Row</button> <button type="submit" id="save_btn" class="btn btn-primary px-5">Save Machine Stock</button> </div> </div> </form> </div> <?php // --- LIST FETCH --- $list = $pdo->prepare(" SELECT m.*, mac.machine_name, loc.location_name FROM mesr_yarn_in_machine m JOIN mesr_machine mac ON mac.id = m.machine_id JOIN mesr_data_location loc ON loc.id = m.location_id WHERE m.company_id = :cid ORDER BY m.id DESC LIMIT 50 "); $list->execute([':cid'=>$company_id]); $rows = $list->fetchAll(PDO::FETCH_ASSOC); ?> <div class="card mt-4"> <div class="card-header fw-bold">Recent Entries</div> <div class="table-responsive"> <table class="table table-bordered table-sm mb-0"> <thead> <tr> <th>ID</th> <th>Month</th> <th>Location</th> <th>Machine</th> <th>Denier</th> <th>Color</th> <th>Total</th> <th>Action</th> </tr> </thead> <tbody> <?php foreach($rows as $r): ?> <tr> <td><?= $r['id'] ?></td> <td><?= $r['month'] ?></td> <td><?= htmlspecialchars($r['location_name']) ?></td> <td><?= htmlspecialchars($r['machine_name']) ?></td> <td><?= $r['denier'] ?></td> <td><?= $r['color'] ?></td> <td><?= $r['total_cons'] ?></td> <td> <a href="machine_edit.php? month=<?= $r['month'] ?>& location_id=<?= $r['location_id'] ?>& machine_id=<?= $r['machine_id'] ?>" class="btn btn-sm btn-warning"> Edit </a> </td> </tr> <?php endforeach; ?> </tbody> </table> </div> </div> <script> function applyWeight(sel) { const row = sel.closest('tr'); const wtInput = row.querySelector('.e-wt'); wtInput.value = (sel.value == "136") ? "0.250" : (sel.value ? "0.070" : ""); } function addRow() { const tbody = document.querySelector("#tbl_machine tbody"); const lastRow = tbody.rows[tbody.rows.length - 1]; const newRow = lastRow.cloneNode(true); const oldSels = lastRow.querySelectorAll('select'); const newSels = newRow.querySelectorAll('select'); oldSels.forEach((s, i) => newSels[i].value = s.value); newRow.querySelectorAll('input').forEach(i => { if(!i.classList.contains('e-wt')) i.value = ''; }); tbody.appendChild(newRow); } function removeRow(btn) { if(document.querySelector("#tbl_machine tbody").rows.length > 1) btn.closest('tr').remove(); } document.getElementById('form_machine').onsubmit = async (e) => { e.preventDefault(); const btn = document.getElementById('save_btn'); btn.disabled = true; btn.innerHTML = "Saving..."; try { const resp = await fetch(window.location.href, { method: 'POST', body: new FormData(e.target) }); const res = await resp.text(); if(res.includes("Success")) { document.getElementById('alert_anchor').innerHTML = `<div class="alert alert-success alert-dismissible fade show">Saved Successfully!<button type="button" class="btn-close" data-bs-dismiss="alert"></button></div>`; const tbody = document.querySelector("#tbl_machine tbody"); while(tbody.rows.length > 1) tbody.deleteRow(1); tbody.rows[0].querySelectorAll('input').forEach(i => i.value = ''); } else { alert(res); } } catch(err) { alert("Error"); } finally { btn.disabled = false; btn.innerHTML = "Save Machine Stock"; } }; </script> <?php require_once __DIR__ . '/../partials/footer.php'; ?>