« Back to History
machine_bobin_report.php
|
20260721_154033.php
Initial Bulk Import
Copy Code
<?php error_reporting(E_ALL); ini_set('display_errors', '0'); // Production pe 0 rakhein, debug ke liye 1 karein require __DIR__ . '/modules/auth/auth.php'; require_login(); $u = auth_user(); $COMPANY_ID = (int)$u['company_id']; // PDO check: Aapke code me $pdo ya $GLOBALS['pdo'] use hota hai $pdo = $GLOBALS['pdo'] ?? $pdo; if (!$pdo) { die("Database Connection Error"); } // --- 1. SAVE LOGIC (AJAX) --- if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['action_save'])) { header('Content-Type: application/json'); $report_date = $_POST['report_date'] ?? date('Y-m-d'); try { $pdo->beginTransaction(); // Delete-then-Insert pattern $del = $pdo->prepare("DELETE FROM mesr_machine_bobbin_report WHERE company_id = ? AND report_date = ?"); $del->execute([$COMPANY_ID, $report_date]); $ins = $pdo->prepare("INSERT INTO mesr_machine_bobbin_report (company_id, report_date, machine_id, quality, color, total_bobbins, red_bobbins, created_at) VALUES (?, ?, ?, ?, ?, ?, ?, NOW())"); if (isset($_POST['machine_id']) && is_array($_POST['machine_id'])) { foreach ($_POST['machine_id'] as $i => $m_id) { if (empty($_POST['quality'][$i])) continue; $ins->execute([ $COMPANY_ID, $report_date, $m_id, $_POST['quality'][$i], $_POST['color'][$i], (int)$_POST['total_bobbins'][$i], (int)$_POST['red_bobbins'][$i] ]); } } $pdo->commit(); echo json_encode(['status' => 'success']); exit; } catch (Exception $e) { if ($pdo->inTransaction()) $pdo->rollBack(); http_response_code(500); echo json_encode(['status' => 'error', 'message' => $e->getMessage()]); exit; } } // --- 2. FETCH MACHINES --- // Same logic as your loom entry page $st = $pdo->prepare("SELECT id, machine_name FROM mesr_machine WHERE company_id=? ORDER BY machine_name ASC"); $st->execute([$COMPANY_ID]); $machines = $st->fetchAll(PDO::FETCH_ASSOC); $page_title = "Machine Bobbin Report"; require_once __DIR__ . '/partials/header.php'; ?> <div class="container-fluid mt-3"> <div id="alert_anchor"></div> <div class="d-flex justify-content-between align-items-center mb-3 border-bottom pb-2"> <h2 class="h4 fw-bold">Machine <small class="text-muted">| Bobbin Report</small></h2> <input type="date" id="report_date" value="<?= date('Y-m-d') ?>" class="form-control w-auto shadow-none"> </div> <form id="form_bobbin"> <input type="hidden" name="action_save" value="1"> <div class="card shadow-sm border-0"> <div class="table-responsive"> <table class="table table-bordered align-middle mb-0"> <thead class="table-light small"> <tr> <th>Machine</th> <th>Quality</th> <th>Color</th> <th class="text-center">Total Bobbin</th> <th class="text-center">Red (Pallu)</th> <th class="text-center bg-light">Yarn (Grams)</th> </tr> </thead> <tbody> <?php foreach($machines as $m): ?> <tr> <td class="fw-bold"> <?= htmlspecialchars($m['machine_name']) ?> <input type="hidden" name="machine_id[]" value="<?= $m['id'] ?>"> </td> <td><input type="text" name="quality[]" class="form-control form-control-sm q-in" placeholder="Quality" oninput="calc(this)"></td> <td><input type="text" name="color[]" class="form-control form-control-sm" placeholder="Color"></td> <td><input type="number" name="total_bobbins[]" value="40" class="form-control form-control-sm text-center t-in" oninput="calc(this)"></td> <td><input type="number" name="red_bobbins[]" value="20" class="form-control form-control-sm text-center" oninput="calc(this)"></td> <td class="text-center"><span class="fw-bold text-success res-g">0.000</span> g</td> </tr> <?php endforeach; ?> </tbody> </table> </div> <div class="card-footer bg-white py-3"> <button type="submit" id="save_btn" class="btn btn-primary px-5 float-end shadow-sm">Save Report</button> </div> </div> </form> </div> <script> function calc(el) { const row = el.closest('tr'); const total = parseFloat(row.querySelector('.t-in').value) || 0; // Calculation: 0.040 grams per bobbin const grams = total * 0.040; row.querySelector('.res-g').innerText = grams.toFixed(3); } // Run once on load document.querySelectorAll('.t-in').forEach(i => calc(i)); document.getElementById('form_bobbin').onsubmit = async (e) => { e.preventDefault(); const btn = document.getElementById('save_btn'); btn.disabled = true; btn.innerHTML = "Saving..."; const fd = new FormData(e.target); fd.append('report_date', document.getElementById('report_date').value); try { const resp = await fetch(window.location.href, { method: 'POST', body: fd }); if (!resp.ok) throw new Error('Server Error 500'); const res = await resp.json(); if(res.status === 'success') { document.getElementById('alert_anchor').innerHTML = '<div class="alert alert-success">Saved Successfully!</div>'; } } catch(err) { alert("Error: " + err.message + ". Make sure the database table exists."); } finally { btn.disabled = false; btn.innerHTML = "Save Report"; } }; </script> <?php require_once __DIR__ . '/partials/footer.php'; ?>