« Back to History
mesr_yarn_in_bobin.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_bobin'); $company_id = (int)$ctx['company_id']; $pdo = $ctx['pdo']; $u_id = $ctx['user']['id'] ?? null; require_once __DIR__ . '/../helpers/activity_helper.php'; $message = ""; /* BOBIN WEIGHT FROM MASTER TABLE */ $stmt = $pdo->prepare(" SELECT filled_weight, empty_weight FROM container_type WHERE company_id=? AND name='Bobin' AND is_active=1 LIMIT 1 "); $stmt->execute([$company_id]); $bobin = $stmt->fetch(PDO::FETCH_ASSOC); $PER_BOBIN_WEIGHT = $bobin['filled_weight'] ?? 0; $EMPTY_BOBIN_WEIGHT = $bobin['empty_weight'] ?? 0; /* SAVE LOGIC */ if ($_SERVER['REQUEST_METHOD'] === 'POST') { $month = $_POST['month'] . '-01'; $location_id = (int)$_POST['location_id']; $machine_id = (int)$_POST['machine_id']; $deniers = $_POST['denier'] ?? []; $colors = $_POST['color'] ?? []; $weights = $_POST['weight'] ?? []; try { $pdo->beginTransaction(); // โ DELETE query ko yahan se bhi hata diya gaya hai. // โ INSERT IGNORE use kiya hai taaki agar same record (Month, Location, Machine, Denier, Color) // pehle se ho toh error na aaye aur purana data delete na ho. $ins = $pdo->prepare(" INSERT IGNORE 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()) "); $new_count = 0; foreach ($deniers as $i => $d) { $w = (float)($weights[$i] ?? 0); if ($w <= 0 || empty($d)) continue; $ins->execute([ $company_id, $month, $location_id, $machine_id, $d, $colors[$i], $w, $PER_BOBIN_WEIGHT, $EMPTY_BOBIN_WEIGHT ]); if($ins->rowCount() > 0) $new_count++; } $pdo->commit(); if($new_count > 0) { log_activity([ 'activity_type' => 'entry', 'module_name' => 'mesr_yarn_in_bobin', 'action_name' => 'save', 'activity_note' => "Saved Yarn in Bobin | Month: {$month}, Location: {$location_id}, Machine: {$machine_id}, Rows: {$new_count}", 'reference_id' => $location_id, ]); $message = "success|$new_count new record(s) saved successfully."; } else { $message = "warning|No new data added. Records might already exist."; } } catch (Exception $e) { if ($pdo->inTransaction()) $pdo->rollBack(); $message = "danger|Error: " . $e->getMessage(); } } /* MASTER DATA FETCH (Dropdowns) */ $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_list = $pdo->prepare("SELECT value FROM mesr_denier_data WHERE company_id=? AND type='denier' ORDER BY CAST(value AS UNSIGNED)"); $deniers_list->execute([$company_id]); $deniers_list = $deniers_list->fetchAll(PDO::FETCH_COLUMN); $colors_list = $pdo->prepare("SELECT value FROM mesr_denier_data WHERE company_id=? AND type='color' ORDER BY value"); $colors_list->execute([$company_id]); $colors_list = $colors_list->fetchAll(PDO::FETCH_COLUMN); $page_title = "๐งต Bobin"; require_once __DIR__ . '/../partials/header.php'; ?> <div class="container-fluid py-4"> <div class="d-flex justify-content-between align-items-center mb-4"> <h4 class="fw-bold text-primary">Yarn in Bobin <small class="text-muted fs-6">| Inventory</small></h4> </div> <?php if ($message): list($type, $txt) = explode('|', $message); ?> <div class="alert alert-<?= $type ?> alert-dismissible fade show" role="alert"> <?= htmlspecialchars($txt) ?> <button type="button" class="btn-close" data-bs-dismiss="alert"></button> </div> <?php endif; ?> <form method="POST" id="bobin_form"> <div class="card shadow-sm mb-4 border-0"> <div class="card-body bg-light rounded"> <div class="row g-3"> <div class="col-md-4"> <label class="form-label fw-bold small text-muted">MONTH</label> <input type="month" name="month" class="form-control shadow-none" value="<?= isset($_POST['month']) ? $_POST['month'] : date('Y-m') ?>" required> </div> <div class="col-md-4"> <label class="form-label fw-bold small text-muted">LOCATION</label> <select name="location_id" class="form-select shadow-none" required> <option value="">-- Select --</option> <?php foreach($locations as $l): ?> <option value="<?=$l['id']?>" <?= (isset($_POST['location_id']) && $_POST['location_id'] == $l['id']) ? 'selected' : '' ?>> <?=$l['location_name']?> </option> <?php endforeach; ?> </select> </div> <div class="col-md-4"> <label class="form-label fw-bold small text-muted">MACHINE</label> <select name="machine_id" class="form-select shadow-none" required> <option value="">-- Select --</option> <?php foreach($machines as $m): ?> <option value="<?=$m['id']?>" <?= (isset($_POST['machine_id']) && $_POST['machine_id'] == $m['id']) ? 'selected' : '' ?>> <?=$m['machine_name']?> </option> <?php endforeach; ?> </select> </div> </div> </div> </div> <div class="card shadow-sm border-0"> <div class="table-responsive"> <table class="table table-hover align-middle mb-0" id="tbl_bobin"> <thead class="table-dark text-uppercase small"> <tr> <th style="width: 35%;">Denier</th> <th style="width: 35%;">Color</th> <th>Weight (KG)</th> <th class="text-center" style="width: 50px;"></th> </tr> </thead> <tbody> <tr> <td> <select name="denier[]" class="form-select shadow-none"> <?php foreach($deniers_list as $d) echo "<option value='$d'>$d</option>"; ?> </select> </td> <td> <select name="color[]" class="form-select shadow-none"> <?php foreach($colors_list as $c) echo "<option value='$c'>$c</option>"; ?> </select> </td> <td> <input type="number" step="0.001" name="weight[]" class="form-control shadow-none" placeholder="0.000" required> </td> <td class="text-center"> <button type="button" class="btn btn-link text-danger p-0" onclick="removeRow(this)"> <i class="bi bi-x-circle-fill fs-5"></i> </button> </td> </tr> </tbody> </table> </div> <div class="card-footer bg-white d-flex justify-content-between py-3 border-top-0"> <button type="button" class="btn btn-outline-success btn-sm fw-bold" onclick="addRow()"> <i class="bi bi-plus-lg"></i> Add Row </button> <button type="submit" class="btn btn-primary px-5 fw-bold shadow"> Save Bobin Stock </button> </div> </div> </form> </div> <script> function addRow() { const tbody = document.querySelector('#tbl_bobin tbody'); const firstRow = tbody.querySelector('tr'); const newRow = firstRow.cloneNode(true); // Reset values in the new row newRow.querySelectorAll('input').forEach(i => i.value = ''); // Copy select values from the current last row const lastRow = tbody.rows[tbody.rows.length - 1]; const oldSels = lastRow.querySelectorAll('select'); const newSels = newRow.querySelectorAll('select'); oldSels.forEach((s, i) => newSels[i].value = s.value); tbody.appendChild(newRow); } function removeRow(btn) { const tbody = document.querySelector('#tbl_bobin tbody'); if(tbody.rows.length > 1) { btn.closest('tr').remove(); } else { alert("At least one row is required."); } } </script> <?php require_once __DIR__ . '/../partials/footer.php'; ?>