« Back to History
bobin_edit.php
|
20260721_154033.php
Initial Bulk Import
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); if (!$month || !$location_id || !$machine_id) { die("Invalid parameters"); } // FETCH EXISTING $stmt = $pdo->prepare(" SELECT * FROM mesr_yarn_in_bobin 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 $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); // SAVE (DELETE + INSERT) if ($_SERVER['REQUEST_METHOD'] === 'POST') { $month_post = $_POST['month'].'-01'; $pdo->beginTransaction(); try { // DELETE OLD GROUP $pdo->prepare(" DELETE FROM mesr_yarn_in_bobin WHERE company_id=? AND month=? AND location_id=? AND machine_id=? ")->execute([$company_id,$month_post,$location_id,$machine_id]); // INSERT NEW $ins = $pdo->prepare(" INSERT INTO mesr_yarn_in_bobin (company_id, month, location_id, machine_id, denier, color, weight, per_bobin_weight, empty_bobin_weight, created_at) VALUES (?,?,?,?,?,?,?,?,?,NOW()) "); foreach ($_POST['denier'] as $i=>$d) { $w = (float)($_POST['weight'][$i] ?? 0); if ($w <= 0) continue; $ins->execute([ $company_id, $month_post, $location_id, $machine_id, $d, $_POST['color'][$i], $w, 0, // keep same as entry page if needed 0 ]); } $pdo->commit(); require_once __DIR__ . '/../helpers/activity_helper.php'; log_activity([ 'activity_type' => 'entry', 'module_name' => 'bobin_edit', 'action_name' => 'save', 'activity_note' => "Bobin edit: Month: $month_post, Location: $location_id, Machine: $machine_id", 'reference_id' => null, ]); header("Location: mesr_yarn_in_bobin.php?success=1"); exit; } catch(Exception $e){ $pdo->rollBack(); die($e->getMessage()); } } require_once __DIR__ . '/../partials/header.php'; ?> <div class="container mt-3"> <h4>Edit Bobin</h4> <form method="POST"> <input type="month" name="month" value="<?= substr($month,0,7) ?>" class="form-control mb-3"> <table class="table table-bordered" id="tbl"> <thead> <tr> <th>Denier</th> <th>Color</th> <th>Weight</th> <th>X</th> </tr> </thead> <tbody> <?php 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="weight[]" value="<?= $r['weight'] ?>" class="form-control"></td> <td><button type="button" onclick="removeRow(this)">X</button></td> </tr> <?php endforeach; ?> </tbody> </table> <button type="button" onclick="addRow()" class="btn btn-success">+ 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'; ?>