« Back to History
machine_edit.php
|
20260722_120325.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']; $month = $_GET['month'] ?? ''; $location_id = (int)($_GET['location_id'] ?? 0); $machine_id = (int)($_GET['machine_id'] ?? 0); // --- SAVE LOGIC --- if ($_SERVER['REQUEST_METHOD'] === 'POST') { $month = $_POST['month'] . '-01'; $location_id = (int)$_POST['location_id']; $machine_id = (int)$_POST['machine_id']; $pdo->beginTransaction(); try { // delete old $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]); // insert new $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 => $d) { if (!$d) continue; $ins->execute([ $company_id, $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(); require_once __DIR__ . '/../helpers/activity_helper.php'; log_activity([ 'activity_type' => 'entry', 'module_name' => 'machine_edit', 'action_name' => 'save', 'activity_note' => "Machine edit: Machine ID: $machine_id", 'reference_id' => $machine_id ?? null, ]); header("Location: mesr_machine_manage.php?success=1"); exit; } catch (Throwable $e) { if ($pdo->inTransaction()) $pdo->rollBack(); die("Error: ".$e->getMessage()); } } // --- FETCH EXISTING DATA --- $stmt = $pdo->prepare(" SELECT * FROM mesr_yarn_in_machine WHERE company_id = :cid AND month = :m AND location_id = :l AND machine_id = :mac "); $stmt->execute([ ':cid'=>$company_id, ':m'=>$month, ':l'=>$location_id, ':mac'=>$machine_id ]); $rows = $stmt->fetchAll(PDO::FETCH_ASSOC); // --- MASTER DATA --- $locations = $pdo->query("SELECT id, location_name FROM mesr_data_location WHERE company_id=$company_id")->fetchAll(PDO::FETCH_ASSOC); $machines = $pdo->query("SELECT id, machine_name FROM mesr_machine WHERE company_id=$company_id")->fetchAll(PDO::FETCH_ASSOC); $deniers = $pdo->query("SELECT value FROM mesr_denier_data WHERE company_id=$company_id AND type='denier'")->fetchAll(PDO::FETCH_COLUMN); $colors = $pdo->query("SELECT value FROM mesr_denier_data WHERE company_id=$company_id AND type='color'")->fetchAll(PDO::FETCH_COLUMN); require_once __DIR__ . '/../partials/header.php'; ?> <div class="container mt-3"> <h4>Edit Yarn in Machine</h4> <form method="POST"> <div class="row mb-3"> <div class="col"> <label>Month</label> <input type="month" name="month" value="<?= substr($month,0,7) ?>" class="form-control"> </div> <div class="col"> <label>Location</label> <select name="location_id" class="form-control"> <?php foreach($locations as $l): ?> <option value="<?= $l['id'] ?>" <?= $l['id']==$location_id?'selected':'' ?>> <?= $l['location_name'] ?> </option> <?php endforeach; ?> </select> </div> <div class="col"> <label>Machine</label> <select name="machine_id" class="form-control"> <?php foreach($machines as $m): ?> <option value="<?= $m['id'] ?>" <?= $m['id']==$machine_id?'selected':'' ?>> <?= $m['machine_name'] ?> </option> <?php endforeach; ?> </select> </div> </div> <table class="table table-bordered" id="tbl"> <thead> <tr> <th>Denier</th> <th>Color</th> <th>Total</th> <th>Per Cone</th> <th>Empty Cone</th> <th>X</th> </tr> </thead> <tbody> <?php if($rows): foreach($rows as $r): ?> <tr> <td> <select name="denier[]" class="form-control"> <?php foreach($deniers as $d): ?> <option value="<?= $d ?>" <?= $d==$r['denier']?'selected':'' ?>><?= $d ?></option> <?php endforeach; ?> </select> </td> <td> <select name="color[]" class="form-control"> <?php foreach($colors as $c): ?> <option value="<?= $c ?>" <?= $c==$r['color']?'selected':'' ?>><?= $c ?></option> <?php endforeach; ?> </select> </td> <td><input name="total_cons[]" value="<?= $r['total_cons'] ?>" class="form-control"></td> <td><input name="per_cone_weight[]" value="<?= $r['per_cone_weight'] ?>" class="form-control"></td> <td><input name="empty_cone_weight[]" value="<?= $r['empty_cone_weight'] ?>" class="form-control"></td> <td><button type="button" onclick="removeRow(this)">X</button></td> </tr> <?php endforeach; else: ?> <tr> <td><select name="denier[]" class="form-control"></select></td> <td><select name="color[]" class="form-control"></select></td> <td><input name="total_cons[]" class="form-control"></td> <td><input name="per_cone_weight[]" class="form-control"></td> <td><input name="empty_cone_weight[]" class="form-control"></td> <td></td> </tr> <?php endif; ?> </tbody> </table> <button type="button" onclick="addRow()" class="btn btn-success">+ Add Row</button> <button class="btn btn-primary">Update</button> </form> </div> <script> function addRow(){ let row = document.querySelector("#tbl tbody tr").cloneNode(true); row.querySelectorAll('input').forEach(i=>i.value=''); document.querySelector("#tbl tbody").appendChild(row); } function removeRow(btn){ if(document.querySelectorAll("#tbl tbody tr").length>1) btn.closest('tr').remove(); } </script> <?php require_once __DIR__ . '/../partials/footer.php'; ?>