« Back to History
mesr_yarn_in_closed_box.php
|
20260921_175631.php
Initial Domain Snapshot
Copy Code
<?php require_once __DIR__ . '/../modules/auth/page_acl.php'; $ctx = page_require_access('mesr_yarn_in_closed_box'); require_once __DIR__ . '/../helpers/activity_helper.php'; $company_id = (int)$ctx['company_id']; $pdo = $ctx['pdo']; $message = ""; $post = $_POST; if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['action_save'])) { $month = !empty($_POST['month']) ? $_POST['month'] . '-01' : null; $location_id = !empty($_POST['location_id']) ? (int)$_POST['location_id'] : null; $machine_id = !empty($_POST['machine_id']) ? (int)$_POST['machine_id'] : null; $denier = trim($_POST['denier'] ?? ''); $color = trim($_POST['color'] ?? ''); $total_box = (int)($_POST['total_box'] ?? 0); $weights = $_POST['weights'] ?? []; $weights = array_map('floatval', $weights); $weight_json = json_encode($weights, JSON_UNESCAPED_UNICODE); $total_weight = array_sum($weights); if (!$month || !$location_id || !$machine_id || !$denier || !$color || $total_box <= 0) { $message = "danger|All fields are required."; } else { try { $pdo->beginTransaction(); // ✅ DELETE logic poori tarah hata diya gaya hai. // ✅ INSERT IGNORE: Ye sirf tabhi save karega jab unique row hogi. // Duplicate hone par ye skip kar dega, purana data delete nahi hoga. $ins = $pdo->prepare(" INSERT IGNORE INTO mesr_yarn_in_closed_box (company_id, month, location_id, machine_id, denier, color, total_box, weight_json, total_weight, created_at) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, NOW()) "); $ins->execute([$company_id, $month, $location_id, $machine_id, $denier, $color, $total_box, $weight_json, $total_weight]); $pdo->commit(); log_activity([ 'activity_type' => 'entry', 'module_name' => 'mesr_yarn_in_closed_box', 'action_name' => 'save', 'activity_note' => "Saved closed box yarn stock | Month: {$month}, Location: {$location_id}, Machine: {$machine_id}, Denier: {$denier}, Color: {$color}, Boxes: {$total_box}, Total Weight: {$total_weight}", 'reference_id' => $machine_id, ]); if($ins->rowCount() > 0) { $message = "success|New entry saved successfully."; // Form reset (optional) unset($post['color'], $post['total_box'], $post['weights']); } else { $message = "warning|No data saved. This exact entry already exists."; } } catch (Exception $e) { if ($pdo->inTransaction()) $pdo->rollBack(); $message = "danger|" . $e->getMessage(); } } } /* MASTER DATA FETCH */ $locations = $pdo->prepare("SELECT id, location_name FROM mesr_data_location WHERE company_id = ? AND is_active = 1 ORDER BY location_name"); $locations->execute([$company_id]); $locations = $locations->fetchAll(PDO::FETCH_ASSOC); $machines = $pdo->prepare("SELECT id, machine_name FROM mesr_machine WHERE company_id = ? ORDER BY machine_name"); $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' ORDER BY CAST(value AS UNSIGNED)"); $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' ORDER BY value"); $colors->execute([$company_id]); $colors = $colors->fetchAll(PDO::FETCH_COLUMN); $page_title = "📦 Closed Box Entry"; require_once __DIR__ . '/../partials/header.php'; ?> <div class="container-fluid py-4"> <div class="d-flex justify-content-between align-items-center mb-4 border-bottom pb-2"> <h2 class="h4 mb-0 text-primary">Yarn in Closed Box <small class="text-muted">| Inventory</small></h2> </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="entryForm"> <input type="hidden" name="action_save" value="1"> <div class="card shadow-sm mb-4"> <div class="card-body bg-light"> <div class="row g-3"> <div class="col-md-3"> <label class="form-label fw-bold small text-muted">MONTH</label> <input type="month" name="month" value="<?= htmlspecialchars($post['month'] ?? date('Y-m')) ?>" class="form-control shadow-none" required> </div> <div class="col-md-3"> <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'] ?>" <?= (($post['location_id'] ?? '') == $l['id']) ? 'selected' : '' ?>> <?= htmlspecialchars($l['location_name']) ?> </option> <?php endforeach; ?> </select> </div> <div class="col-md-3"> <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'] ?>" <?= (($post['machine_id'] ?? '') == $m['id']) ? 'selected' : '' ?>> <?= htmlspecialchars($m['machine_name']) ?> </option> <?php endforeach; ?> </select> </div> <div class="col-md-3"> <label class="form-label fw-bold small text-muted">DENIER</label> <select name="denier" class="form-select shadow-none" required> <option value="">-- Select --</option> <?php foreach ($deniers as $d): ?> <option value="<?= $d ?>" <?= (($post['denier'] ?? '') == $d) ? 'selected' : '' ?>><?= $d ?></option> <?php endforeach; ?> </select> </div> </div> </div> </div> <div class="card shadow-sm border-primary"> <div class="card-header bg-primary text-white d-flex justify-content-between align-items-center py-2"> <div class="d-flex align-items-center gap-3"> <span class="fw-bold">Weight Details</span> <select name="color" class="form-select form-select-sm shadow-none" style="width: 180px;" required> <option value="">-- Select Color --</option> <?php foreach ($colors as $c): ?> <option value="<?= $c ?>" <?= (($post['color'] ?? '') == $c) ? 'selected' : '' ?>><?= $c ?></option> <?php endforeach; ?> </select> </div> <div class="d-flex align-items-center" style="width: 220px;"> <span class="me-2 small fw-bold">Box Count:</span> <input type="number" id="total_box" name="total_box" class="form-control form-control-sm shadow-none" min="1" value="<?= htmlspecialchars($post['total_box'] ?? '') ?>" oninput="generateBoxInputs()" required placeholder="Enter Qty"> </div> </div> <div class="card-body" style="min-height: 120px;"> <div id="box_container" class="row g-2"></div> </div> <div class="card-footer bg-white d-flex justify-content-between align-items-center"> <h4 class="mb-0 text-primary" id="total_display">Total Weight: 0.000 kg</h4> <button type="submit" class="btn btn-primary px-5 fw-bold shadow">Save Data</button> </div> </div> </form> </div> <script> const savedWeights = <?= json_encode($post['weights'] ?? []) ?>; function generateBoxInputs() { const count = parseInt(document.getElementById('total_box').value) || 0; const boxContainer = document.getElementById('box_container'); boxContainer.innerHTML = ''; if(count === 0) { boxContainer.innerHTML = '<div class="col-12 text-center text-muted py-5 small italic">Enter box count to generate weight inputs.</div>'; return; } for (let i = 1; i <= count; i++) { const val = savedWeights[i-1] || ''; const div = document.createElement('div'); div.className = 'col-6 col-md-2 col-lg-1 mb-2'; div.innerHTML = ` <div class="input-group input-group-sm"> <span class="input-group-text bg-light text-muted" style="font-size: 9px; padding: 2px 4px;">#${i}</span> <input type="number" step="0.01" name="weights[]" class="form-control box-weight text-center shadow-none px-1" value="${val}" placeholder="0.00" oninput="calculateTotal()" required> </div>`; boxContainer.appendChild(div); } calculateTotal(); } function calculateTotal() { let sum = 0; document.querySelectorAll('.box-weight').forEach(e => { sum += parseFloat(e.value) || 0; }); document.getElementById('total_display').innerText = "Total Weight: " + sum.toFixed(3) + " kg"; } window.onload = function() { generateBoxInputs(); }; </script> <?php require_once __DIR__ . '/../partials/footer.php'; ?>