« Back to History
mesr_yarn_in_open_box.php
|
20260722_120325.php
Initial Domain Snapshot
Copy Code
<?php require_once __DIR__ . '/../modules/auth/page_acl.php'; $ctx = page_require_access('mesr_yarn_in_open_box'); require_once __DIR__ . '/../helpers/activity_helper.php'; $company_id = (int)$ctx['company_id']; $pdo = $ctx['pdo']; /* --- 1. SAVE LOGIC --- */ if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['location_id'])) { $month = $_POST['month'] . "-01"; $loc_id = (int)$_POST['location_id']; $mac_id = (int)$_POST['machine_id']; try { $pdo->beginTransaction(); // INSERT IGNORE: Duplicate entries skip hongi, naya data save hoga. $ins = $pdo->prepare("INSERT IGNORE 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())"); $new_count = 0; foreach ($_POST['denier'] as $i => $denier_val) { if (empty($denier_val)) continue; $ins->execute([ $company_id, $month, $loc_id, $mac_id, $denier_val, $_POST['color'][$i], (float)$_POST['total_cons'][$i], (float)$_POST['per_cone_weight'][$i], (float)$_POST['empty_cone_weight'][$i] ]); if($ins->rowCount() > 0) { $new_count++; } } $pdo->commit(); log_activity([ 'activity_type' => 'entry', 'module_name' => 'mesr_yarn_in_open_box', 'action_name' => 'save', 'activity_note' => "Saved open box yarn stock | Month: {$month}, Location: {$loc_id}, Machine: {$mac_id}, New Rows: {$new_count}", 'reference_id' => $mac_id, ]); $msg = ($new_count > 0) ? "Success: $new_count new entries saved!" : "Success: No new changes (Duplicate data skipped)."; header("Location: " . $_SERVER['PHP_SELF'] . "?msg=" . urlencode($msg)); exit; } catch (Exception $e) { if ($pdo->inTransaction()) $pdo->rollBack(); die("DATABASE ERROR: " . $e->getMessage()); } } /* --- 2. MASTER DATA FETCH --- */ $locations = $pdo->prepare("SELECT id, location_name FROM mesr_data_location WHERE company_id = ? AND is_active = 1"); $locations->execute([$company_id]); $locations = $locations->fetchAll(PDO::FETCH_ASSOC); $machines = $pdo->prepare("SELECT id, machine_name FROM mesr_machine WHERE company_id = ?"); $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'"); $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'"); $colors->execute([$company_id]); $colors = $colors->fetchAll(PDO::FETCH_COLUMN); require_once __DIR__ . '/../partials/header.php'; ?> <div class="container-fluid py-4"> <div class="card shadow-sm mb-4"> <div class="card-header bg-primary text-white d-flex justify-content-between align-items-center"> <h5 class="mb-0">Yarn Open Box Entry</h5> <?php if(isset($_GET['msg'])): ?> <span class="badge bg-success"><?= htmlspecialchars($_GET['msg']) ?></span> <?php endif; ?> </div> <div class="card-body"> <form id="form_open_box" method="POST"> <div class="row mb-3"> <div class="col-md-4"> <label class="form-label fw-bold">Month</label> <input type="month" name="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" class="form-control shadow-none" required> <option value="">Select Location</option> <?php foreach($locations as $l): ?> <option value="<?= $l['id'] ?>"><?= $l['location_name'] ?></option> <?php endforeach; ?> </select> </div> <div class="col-md-4"> <label class="form-label fw-bold">Machine</label> <select name="machine_id" class="form-control shadow-none" required> <option value="">Select Machine</option> <?php foreach($machines as $m): ?> <option value="<?= $m['id'] ?>"><?= $m['machine_name'] ?></option> <?php endforeach; ?> </select> </div> </div> <div class="table-responsive"> <table class="table table-bordered align-middle" id="tbl_open"> <thead class="table-light text-center"> <tr> <th>Denier</th> <th>Color</th> <th>Total Cones</th> <th>Per Cone Weight</th> <th>Empty Cone</th> <th width="50px">X</th> </tr> </thead> <tbody> <tr> <td> <select name="denier[]" class="form-control shadow-none"> <?php foreach($deniers as $d): ?> <option value="<?= $d ?>"><?= $d ?></option> <?php endforeach; ?> </select> </td> <td> <select name="color[]" class="form-control shadow-none"> <?php foreach($colors as $c): ?> <option value="<?= $c ?>"><?= $c ?></option> <?php endforeach; ?> </select> </td> <td><input type="number" step="0.001" name="total_cons[]" class="form-control shadow-none" placeholder="0.000" required></td> <td><input type="number" step="0.001" name="per_cone_weight[]" class="form-control shadow-none" placeholder="0.000" required></td> <td><input type="number" step="0.001" name="empty_cone_weight[]" class="form-control shadow-none" placeholder="0.000" required></td> <td class="text-center"><button type="button" class="btn btn-outline-danger btn-sm" onclick="removeRow(this)">X</button></td> </tr> </tbody> </table> </div> <div class="mt-3"> <button type="button" onclick="addRow()" class="btn btn-success">+ Add Row</button> <button type="submit" class="btn btn-primary float-end px-5">Save Data</button> </div> </form> </div> </div> <?php $list = $pdo->prepare(" SELECT o.*, DATE_FORMAT(o.month, '%Y-%m') as display_month, mac.machine_name, loc.location_name FROM mesr_yarn_in_open_box o LEFT JOIN mesr_machine mac ON mac.id = o.machine_id LEFT JOIN mesr_data_location loc ON loc.id = o.location_id WHERE o.company_id = :cid ORDER BY o.id DESC LIMIT 50 "); $list->execute([':cid'=>$company_id]); $rows = $list->fetchAll(PDO::FETCH_ASSOC); ?> <div class="card shadow-sm mt-4"> <div class="card-header bg-dark text-white">Recent Entries (Last 50)</div> <div class="table-responsive"> <table class="table table-sm table-hover mb-0 text-center"> <thead class="table-light"> <tr> <th>ID</th> <th>MONTH</th> <th>LOCATION</th> <th>MACHINE</th> <th>DENIER</th> <th>COLOR</th> <th>TOTAL WT</th> <th>ACTION</th> </tr> </thead> <tbody> <?php foreach($rows as $r): ?> <tr> <td><?= $r['id'] ?></td> <td><?= $r['display_month'] ?></td> <td class="text-start"><?= $r['location_name'] ?></td> <td><?= $r['machine_name'] ?></td> <td><?= $r['denier'] ?></td> <td><?= $r['color'] ?></td> <td><?= number_format($r['total_cons'], 3) ?></td> <td> <a href="open_box_edit.php?month=<?= urlencode($r['month']) ?>&location_id=<?= $r['location_id'] ?>&machine_id=<?= $r['machine_id'] ?>" class="btn btn-warning btn-sm py-0">Edit</a> </td> </tr> <?php endforeach; ?> </tbody> </table> </div> </div> </div> <script> function addRow(){ let tbody = document.querySelector("#tbl_open tbody"); let firstRow = tbody.querySelector("tr"); let newRow = firstRow.cloneNode(true); newRow.querySelectorAll('input').forEach(i => i.value = ''); tbody.appendChild(newRow); } function removeRow(btn){ let rows = document.querySelectorAll("#tbl_open tbody tr"); if(rows.length > 1) { btn.closest('tr').remove(); } else { alert("Pehli row delete nahi ho sakti."); } } </script> <?php require_once __DIR__ . '/../partials/footer.php'; ?>