« Back to History
jobwork_in_add.php
|
20260723_000646.php
Initial Domain Snapshot
Copy Code
<?php require_once __DIR__ . '/modules/auth/page_acl.php'; $ctx = page_require_access('jobwork_in_add'); $pdo = $ctx['pdo'] ?? null; $company_id = (int)($ctx['company_id'] ?? 0); $u = $ctx['user'] ?? null; require_once __DIR__ . '/partials/header.php'; /* ================= FETCH PARTIES ================= */ $stmt = $pdo->prepare("SELECT id, party_name FROM job_work_party WHERE company_id = :cid AND status='active' ORDER BY party_name"); $stmt->execute([':cid' => $company_id]); $parties = $stmt->fetchAll(PDO::FETCH_ASSOC); /* ================= SAVE ================= */ if ($_SERVER['REQUEST_METHOD'] === 'POST') { $party_id = (int)($_POST['party_id'] ?? 0); $date = $_POST['job_date'] ?? date('Y-m-d'); $weights = $_POST['weight'] ?? []; $data = []; $total = 0; foreach ($weights as $w) { $w = (float)$w; if ($w > 0) { $data[] = ["weight" => $w]; $total += $w; } } $sql = "INSERT INTO jobwork_in (company_id, party, job_date, weight_json, total_weight) VALUES (:cid, :party, :date, :json, :total)"; $stmt = $pdo->prepare($sql); $stmt->execute([ ':cid' => $company_id, ':party' => $party_id, ':date' => $date, ':json' => json_encode($data), ':total' => $total ]); echo "<div class='alert alert-success'>Saved Successfully</div>"; } ?> <div class="container mt-4"> <div class="card p-3"> <h4 class="mb-3">Job Work In Entry</h4> <form method="POST"> <!-- Party --> <div class="mb-3"> <label class="form-label">Party</label> <select name="party_id" class="form-control" required> <option value="">Select Party</option> <?php foreach ($parties as $p): ?> <option value="<?= $p['id'] ?>"> <?= htmlspecialchars($p['party_name']) ?> </option> <?php endforeach; ?> </select> </div> <!-- Date --> <div class="mb-3"> <label class="form-label">Date</label> <input type="date" name="job_date" value="<?= date('Y-m-d') ?>" class="form-control"> </div> <!-- Weight Rows --> <label class="form-label">Weights</label> <table class="table table-bordered" id="weightTable"> <thead> <tr> <th style="width:80%">Weight</th> <th>Action</th> </tr> </thead> <tbody> <tr> <td> <input type="number" step="0.001" name="weight[]" class="form-control weight"> </td> <td> <button type="button" class="btn btn-danger removeRow">X</button> </td> </tr> </tbody> </table> <button type="button" class="btn btn-primary mb-3" id="addRow"> + Add Row </button> <!-- Total --> <div class="mb-3"> <label class="form-label">Total Weight</label> <input type="text" id="total" class="form-control" readonly> </div> <button type="submit" class="btn btn-success"> Save </button> </form> </div> </div> <script> function calculateTotal() { let total = 0; document.querySelectorAll('.weight').forEach(el => { let val = parseFloat(el.value) || 0; total += val; }); document.getElementById('total').value = total.toFixed(3); } // ENTER → new row add document.addEventListener('keydown', function(e) { if (e.target.classList.contains('weight') && e.key === 'Enter') { e.preventDefault(); let currentRow = e.target.closest('tr'); let tbody = document.querySelector('#weightTable tbody'); // check: last row hai ya nahi if (currentRow === tbody.lastElementChild) { let row = document.createElement('tr'); row.innerHTML = ` <td> <input type="number" step="0.001" name="weight[]" class="form-control weight"> </td> <td> <button type="button" class="btn btn-danger removeRow">X</button> </td> `; tbody.appendChild(row); } // focus next row input let nextRow = currentRow.nextElementSibling; if (nextRow) { nextRow.querySelector('.weight').focus(); } calculateTotal(); } }); // input change → total update document.addEventListener('input', function(e) { if (e.target.classList.contains('weight')) { calculateTotal(); } }); // remove row document.addEventListener('click', function(e) { if (e.target.classList.contains('removeRow')) { e.target.closest('tr').remove(); calculateTotal(); } }); </script> <?php require_once __DIR__ . '/partials/footer.php'; ?>