« Back to History
daily_loom_tracker_input.php
|
20261118_125741.php
Initial Domain Snapshot
Copy Code
<?php /* daily_loom_tracker_input.php Input-only page: saves raw inputs. No computation here. Updated: Bootstrap 5 Compatible Version */ error_reporting(E_ALL); ini_set('display_errors',1); // bootstrap per project conventions require_once __DIR__ . '/modules/auth/page_acl.php'; $ctx = page_require_access('daily_loom_tracker_input'); $u = $ctx['user'] ?? null; $company_id = (int)($ctx['company_id'] ?? 0); $pdo = $ctx['pdo'] ?? null; if (!$pdo) { require_once __DIR__ . '/core/db.php'; } // session + CSRF if (session_status() === PHP_SESSION_NONE) session_start(); if (empty($_SESSION['csrf_token'])) $_SESSION['csrf_token'] = bin2hex(random_bytes(16)); $csrf = $_SESSION['csrf_token']; // helper function h($v){ return htmlspecialchars($v ?? '', ENT_QUOTES, 'UTF-8'); } // defaults $default_start_time = '00:00'; $default_end_time = '24:00'; $default_target_rpm = 126; // default date: GET date or latest date for company or today $default_date = $_GET['date'] ?? null; if (!$default_date) { $stmtMax = $pdo->prepare("SELECT MAX(date) FROM daily_loom_tracker_input WHERE company_id = :cid"); $stmtMax->execute([':cid'=>$company_id]); $maxdate = $stmtMax->fetchColumn(); $default_date = $maxdate ?: date('Y-m-d'); } // prefill (UI fields only) $prefill = [ 'id' => null, 'date' => $default_date, 'machine_id' => $_GET['machine'] ?? '', 'picks_repeat' => '', 'start_repeat' => '', 'start_pick' => '', 'end_repeat' => '', 'end_pick' => '', 'target_rpm' => $default_target_rpm, 'start_time' => $default_start_time, 'end_time' => $default_end_time, 'remarks' => '' ]; $errors = []; $success = null; // load edit if id in GET if (!empty($_GET['id'])) { $id_get = (int)$_GET['id']; $stmt = $pdo->prepare("SELECT * FROM daily_loom_tracker_input WHERE id = :id AND company_id = :cid LIMIT 1"); $stmt->execute([':id'=>$id_get, ':cid'=>$company_id]); $row = $stmt->fetch(PDO::FETCH_ASSOC); if ($row) { $prefill['id'] = $row['id']; $prefill['date'] = $row['date']; $prefill['machine_id'] = $row['machine_id']; $prefill['picks_repeat'] = $row['picks_repeat']; $prefill['start_repeat'] = $row['start_repeat']; $prefill['start_pick'] = $row['start_pick']; $prefill['end_repeat'] = $row['end_repeat']; $prefill['end_pick'] = $row['end_pick']; $prefill['target_rpm'] = $row['target_rpm'] ?? $default_target_rpm; $prefill['start_time'] = $row['start_time'] ?? $default_start_time; $prefill['end_time'] = $row['end_time'] ?? $default_end_time; $prefill['remarks'] = $row['remarks'] ?? ''; } else { $errors[] = "Record not found for edit."; } } // POST = insert/update (raw inputs only) if ($_SERVER['REQUEST_METHOD'] === 'POST') { if (empty($_POST['csrf_token']) || $_POST['csrf_token'] !== ($_SESSION['csrf_token'] ?? '')) { $errors[] = 'Invalid CSRF token.'; } // gather raw inputs $id = isset($_POST['id']) && $_POST['id'] !== '' ? (int)$_POST['id'] : null; $date = $_POST['date'] ?? ''; $machine_id = trim($_POST['machine_id'] ?? ''); $picks_repeat = $_POST['picks_repeat'] !== '' ? (int)$_POST['picks_repeat'] : null; $start_repeat = $_POST['start_repeat'] !== '' ? (int)$_POST['start_repeat'] : null; $start_pick = $_POST['start_pick'] !== '' ? (int)$_POST['start_pick'] : null; $end_repeat = $_POST['end_repeat'] !== '' ? (int)$_POST['end_repeat'] : null; $end_pick = $_POST['end_pick'] !== '' ? (int)$_POST['end_pick'] : null; $target_rpm = $_POST['target_rpm'] !== '' ? (int)$_POST['target_rpm'] : $default_target_rpm; $start_time = trim($_POST['start_time'] ?? $default_start_time); $end_time = trim($_POST['end_time'] ?? $default_end_time); $remarks = trim($_POST['remarks'] ?? ''); // basic validation if (!$date) $errors[] = 'Date required.'; if ($machine_id === '') $errors[] = 'Machine ID required.'; if (empty($errors)) { if ($id) { // update raw columns $sql = "UPDATE daily_loom_tracker_input SET date=:date, machine_id=:machine_id, picks_repeat=:picks_repeat, start_repeat=:start_repeat, start_pick=:start_pick, end_repeat=:end_repeat, end_pick=:end_pick, target_rpm=:target_rpm, start_time=:start_time, end_time=:end_time, remarks=:remarks WHERE id = :id AND company_id = :cid"; $stmt = $pdo->prepare($sql); $stmt->execute([ ':date'=>$date, ':machine_id'=>$machine_id, ':picks_repeat'=>$picks_repeat, ':start_repeat'=>$start_repeat, ':start_pick'=>$start_pick, ':end_repeat'=>$end_repeat, ':end_pick'=>$end_pick, ':target_rpm'=>$target_rpm, ':start_time'=>$start_time, ':end_time'=>$end_time, ':remarks'=>$remarks, ':id'=>$id, ':cid'=>$company_id ]); $success = "Updated (ID $id)."; $saved_machine = $machine_id; } else { // insert raw inputs $sql = "INSERT INTO daily_loom_tracker_input (company_id, date, machine_id, picks_repeat, start_repeat, start_pick, end_repeat, end_pick, target_rpm, start_time, end_time, remarks, created_by, created_at) VALUES (:company_id,:date,:machine_id,:picks_repeat,:start_repeat,:start_pick,:end_repeat,:end_pick, :target_rpm,:start_time,:end_time,:remarks,:created_by,NOW())"; $stmt = $pdo->prepare($sql); $stmt->execute([ ':company_id'=>$company_id, ':date'=>$date, ':machine_id'=>$machine_id, ':picks_repeat'=>$picks_repeat, ':start_repeat'=>$start_repeat, ':start_pick'=>$start_pick, ':end_repeat'=>$end_repeat, ':end_pick'=>$end_pick, ':target_rpm'=>$target_rpm, ':start_time'=>$start_time, ':end_time'=>$end_time, ':remarks'=>$remarks, ':created_by'=>($u['id'] ?? null) ]); $newId = $pdo->lastInsertId(); $success = "Inserted (ID $newId)."; $saved_machine = $machine_id; } // next machine auto-increment $next_machine = $saved_machine; if (is_numeric($saved_machine)) { $next_machine = (string)(((int)$saved_machine) + 1); } else { if (preg_match('/^(.*?)(\d+)$/', $saved_machine, $m)) { $prefix = $m[1]; $num = $m[2]; $len = strlen($num); $newnum = str_pad((string)((int)$num+1), $len, '0', STR_PAD_LEFT); $next_machine = $prefix . $newnum; } } header('Location: daily_loom_tracker_input.php?ok=1&date=' . rawurlencode($date) . '&machine=' . rawurlencode($next_machine)); exit; } else { $prefill = [ 'id' => $id, 'date' => $date, 'machine_id' => $machine_id, 'picks_repeat' => $picks_repeat, 'start_repeat' => $start_repeat, 'start_pick' => $start_pick, 'end_repeat' => $end_repeat, 'end_pick' => $end_pick, 'target_rpm' => $target_rpm, 'start_time' => $start_time, 'end_time' => $end_time, 'remarks' => $remarks ]; } } // deletion if (isset($_GET['del'])) { $delid = (int)$_GET['del']; $stmt = $pdo->prepare("DELETE FROM daily_loom_tracker_input WHERE id = :id AND company_id = :cid"); $stmt->execute([':id'=>$delid, ':cid'=>$company_id]); header('Location: daily_loom_tracker_input.php?date=' . rawurlencode($_GET['date'] ?? $default_date)); exit; } // load existing rows $list_date = $_GET['date'] ?? $prefill['date'] ?? $default_date; $stmt = $pdo->prepare("SELECT * FROM daily_loom_tracker_input WHERE company_id = :cid AND date = :date ORDER BY machine_id ASC"); $stmt->execute([':cid'=>$company_id, ':date'=>$list_date]); $existing_rows = $stmt->fetchAll(PDO::FETCH_ASSOC); // header include $header_paths = [ __DIR__.'/erp/partials/header.php', __DIR__.'/partials/header.php', __DIR__ . '/../erp/partials/header.php', __DIR__ . '/../partials/header.php' ]; $foundh = false; foreach ($header_paths as $p) { if (file_exists($p)) { require_once $p; $foundh = true; break; } } if (!$foundh) { trigger_error('Header partial not found.', E_USER_WARNING); echo "<div class='alert alert-warning'>Header missing</div>"; } ?> <div class="container-fluid my-4"> <div class="card shadow-sm"> <div class="card-header bg-primary text-white"> <h4 class="mb-0 fs-5">Daily Loom Tracker - Input</h4> </div> <div class="card-body"> <?php if (!empty($errors)): ?> <div class="alert alert-danger alert-dismissible fade show" role="alert"> <ul class="mb-0"> <?php foreach($errors as $e) echo '<li>'.h($e).'</li>'; ?> </ul> <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button> </div> <?php elseif (!empty($success) || isset($_GET['ok'])): ?> <div class="alert alert-success alert-dismissible fade show" role="alert"> <?php echo h($success ?? 'Saved. Ready for next entry.'); ?> <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button> </div> <?php endif; ?> <form method="post" action=""> <input type="hidden" name="csrf_token" value="<?php echo h($csrf); ?>"> <?php if (!empty($prefill['id'])): ?> <input type="hidden" name="id" value="<?php echo h($prefill['id']); ?>"> <?php endif; ?> <!-- Form Grid Layout with Bootstrap --> <div class="row g-3"> <div class="col-md-3 col-sm-6"> <label for="fld-date" class="form-label font-weight-bold">Date</label> <input type="date" name="date" id="fld-date" class="form-control" value="<?php echo h($prefill['date']); ?>" required> </div> <div class="col-md-3 col-sm-6"> <label for="fld-machine" class="form-label font-weight-bold">Machine ID</label> <input type="text" name="machine_id" id="fld-machine" class="form-control" value="<?php echo h($prefill['machine_id']); ?>" required> </div> <div class="col-md-2 col-sm-6"> <label for="fld-picks_repeat" class="form-label">Picks/Repeat</label> <input type="number" name="picks_repeat" id="fld-picks_repeat" class="form-control" value="<?php echo h($prefill['picks_repeat']); ?>"> </div> <div class="col-md-2 col-sm-6"> <label for="fld-start_repeat" class="form-label">Start Repeat</label> <input type="number" name="start_repeat" id="fld-start_repeat" class="form-control" value="<?php echo h($prefill['start_repeat']); ?>"> </div> <div class="col-md-2 col-sm-6"> <label for="fld-start_pick" class="form-label">Start Pick</label> <input type="number" name="start_pick" id="fld-start_pick" class="form-control" value="<?php echo h($prefill['start_pick']); ?>"> </div> <div class="col-md-2 col-sm-6"> <label for="fld-end_repeat" class="form-label">End Repeat</label> <input type="number" name="end_repeat" id="fld-end_repeat" class="form-control" value="<?php echo h($prefill['end_repeat']); ?>"> </div> <div class="col-md-2 col-sm-6"> <label for="fld-end_pick" class="form-label">End Pick</label> <input type="number" name="end_pick" id="fld-end_pick" class="form-control" value="<?php echo h($prefill['end_pick']); ?>"> </div> <div class="col-md-2 col-sm-6"> <label for="fld-target_rpm" class="form-label">Target RPM</label> <input type="number" name="target_rpm" id="fld-target_rpm" class="form-control" value="<?php echo h($prefill['target_rpm'] ?: $default_target_rpm); ?>"> </div> <div class="col-md-3 col-sm-6"> <label for="fld-start_time" class="form-label">Start Time</label> <input type="text" name="start_time" id="fld-start_time" class="form-control" placeholder="HH:MM" value="<?php echo h($prefill['start_time'] ?: $default_start_time); ?>"> </div> <div class="col-md-3 col-sm-6"> <label for="fld-end_time" class="form-label">End Time</label> <input type="text" name="end_time" id="fld-end_time" class="form-control" placeholder="HH:MM or 24:00" value="<?php echo h($prefill['end_time'] ?: $default_end_time); ?>"> </div> <div class="col-12"> <label for="fld-remarks" class="form-label">Remarks</label> <textarea name="remarks" id="fld-remarks" class="form-control" rows="2"><?php echo h($prefill['remarks']); ?></textarea> </div> </div> <div class="mt-4 d-flex gap-2"> <button class="btn btn-primary px-4" type="submit">Save</button> <a class="btn btn-outline-secondary" href="daily_loom_tracker_report.php?date=<?php echo rawurlencode($prefill['date']); ?>">View Report</a> </div> </form> <hr class="my-4"> <!-- Table Section Header --> <div class="d-flex flex-wrap justify-content-between align-items-center mb-3 gap-2"> <h5 class="mb-0">Existing entries for <strong><?php echo h($list_date); ?></strong></h5> <div class="d-flex gap-2"> <button class="btn btn-sm btn-outline-dark" id="btn-print" type="button">Print (no header)</button> <button class="btn btn-sm btn-outline-danger" id="btn-export-pdf" type="button">Export PDF</button> </div> </div> <?php if (!empty($existing_rows)): ?> <div class="table-responsive" id="report-table-wrapper"> <table class="table table-bordered table-hover align-middle" id="report-table"> <thead class="table-light"> <tr> <th>Machine</th> <th>Picks/Repeat</th> <th>Start Repeat</th> <th>Start Pick</th> <th>End Repeat</th> <th>End Pick</th> <th>Start Time</th> <th>End Time</th> <th>Remarks</th> <th class="text-center">Actions</th> </tr> </thead> <tbody> <?php foreach ($existing_rows as $er): ?> <tr> <td class="text-wrap"><?php echo h($er['machine_id']); ?></td> <td class="text-wrap"><?php echo h($er['picks_repeat']); ?></td> <td class="text-wrap"><?php echo h($er['start_repeat']); ?></td> <td class="text-wrap"><?php echo h($er['start_pick']); ?></td> <td class="text-wrap"><?php echo h($er['end_repeat']); ?></td> <td class="text-wrap"><?php echo h($er['end_pick']); ?></td> <td class="text-wrap"><?php echo h($er['start_time']); ?></td> <td class="text-wrap"><?php echo h($er['end_time']); ?></td> <td class="text-wrap"><?php echo h($er['remarks']); ?></td> <td class="text-center text-nowrap"> <a class="btn btn-sm btn-outline-primary me-1" href="?date=<?php echo rawurlencode($list_date); ?>&id=<?php echo (int)$er['id']; ?>">Edit</a> <a class="btn btn-sm btn-outline-danger" href="daily_loom_tracker_input.php?del=<?php echo (int)$er['id']; ?>&date=<?php echo rawurlencode($list_date); ?>" onclick="return confirm('Delete this row?');">Delete</a> </td> </tr> <?php endforeach; ?> </tbody> </table> </div> <?php else: ?> <div class="alert alert-info mb-0">No entries for this date.</div> <?php endif; ?> </div> </div> </div> <script> (function(){ // Enter key handling const fields = ['fld-date','fld-machine','fld-picks_repeat','fld-start_repeat','fld-start_pick','fld-end_repeat','fld-end_pick','fld-target_rpm','fld-start_time','fld-end_time','fld-remarks'].map(id=>document.getElementById(id)).filter(Boolean); fields.forEach((el,idx)=>{ if(!el) return; el.addEventListener('keydown', function(e){ if(e.key==='Enter' && el.tagName.toLowerCase()!=='textarea'){ e.preventDefault(); const nxt = fields[idx+1]; if(nxt) nxt.focus(); else el.closest('form').submit(); } }); }); // Defaults const tRPM=document.getElementById('fld-target_rpm'); if(tRPM && (!tRPM.value)) tRPM.value = '<?php echo $default_target_rpm; ?>'; const st=document.getElementById('fld-start_time'); if(st && (!st.value)) st.value = '<?php echo $default_start_time; ?>'; const et=document.getElementById('fld-end_time'); if(et && (!et.value)) et.value = '<?php echo $default_end_time; ?>'; // Date change reload const dateInput = document.getElementById('fld-date'); if (dateInput) dateInput.addEventListener('change', function(){ const d = dateInput.value; const params = new URLSearchParams(window.location.search); params.set('date', d); params.delete('id'); params.delete('ok'); window.location = window.location.pathname + '?' + params.toString(); }); // Print view function openPrintView() { const title = 'Daily Loom Tracker - ' + '<?php echo h($list_date); ?>'; const table = document.getElementById('report-table'); if (!table) { alert('No data to print'); return; } const win = window.open('', '_blank', 'noopener'); const doc = win.document; doc.open(); doc.write('<!doctype html><html><head><meta charset="utf-8"><title>' + title + '</title>'); doc.write('<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css">'); doc.write('<style>@media print{@page{size: auto; margin:10mm;} body{font-family:system-ui,-apple-system,Segoe UI,Roboto,Arial;} table th:last-child, table td:last-child {display:none;} }</style>'); doc.write('</head><body class="p-4">'); doc.write('<h3 class="mb-3">' + title + '</h3>'); doc.write(table.outerHTML); doc.write('</body></html>'); doc.close(); setTimeout(()=>{ win.focus(); win.print(); }, 400); } document.getElementById('btn-print') && document.getElementById('btn-print').addEventListener('click', openPrintView); document.getElementById('btn-export-pdf') && document.getElementById('btn-export-pdf').addEventListener('click', openPrintView); })(); </script> <?php // footer include $footer_paths = [__DIR__.'/erp/partials/footer.php', __DIR__.'/partials/footer.php', __DIR__.'/../erp/partials/footer.php', __DIR__.'/../partials/footer.php']; $foundf = false; foreach ($footer_paths as $p) { if (file_exists($p)) { require_once $p; $foundf = true; break; } } if (!$foundf) { trigger_error('Footer partial not found.', E_USER_WARNING); echo "<div class='alert alert-warning'>Footer missing</div>"; } ?>