« Back to History
roll_palti_entry.php
|
20260722_120325.php
Initial Domain Snapshot
Copy Code
<?php /* ========================================================= File: /erp/roll_palti_entry.php Purpose: Roll/Palti daily entry (machine-wise) Uses : roll_palti_machine_data for machine + khata + employee Scope : Page-local only; multi-company aware ========================================================= */ error_reporting(E_ALL); ini_set('display_errors',1); /* ---- Auth + PDO ---- */ require __DIR__ . '/modules/auth/auth.php'; require_login(); $u = auth_user(); $company_id = (int)$u['company_id']; $user_id = (int)$u['id']; $pdo = $GLOBALS['pdo'] ?? null; if (!$pdo) { require __DIR__ . '/core/db.php'; } /* ---- Header friendly vars ---- */ if (!isset($company) || !is_array($company)) { $company = ['id'=>$company_id, 'name'=>($u['company_name'] ?? '')]; } /* ---- Header include decision ---- */ $__header = __DIR__ . '/partials/header.php'; if (!isset($USE_HEADER)) { $IS_AJAX = !empty($_GET['ajax']) || !empty($_GET['act']) || (isset($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH'])==='xmlhttprequest'); $IS_EXPORT = isset($_GET['fmt']) || isset($_GET['download']); $USE_HEADER = !($IS_AJAX || $IS_EXPORT); } if (!isset($PAGE_ID) || $PAGE_ID==='') { $PAGE_ID = 'roll_palti_entry'; } $PAGE = $PAGE_ID; /* ---- Ensure report table ---- */ $pdo->exec(" CREATE TABLE IF NOT EXISTS roll_palti_report ( id BIGINT PRIMARY KEY AUTO_INCREMENT, company_id BIGINT NOT NULL, report_date DATE NOT NULL, machine_no VARCHAR(50) NOT NULL, total_roll INT NOT NULL, employee_id BIGINT NULL, employee_name VARCHAR(120) NOT NULL, updated_by BIGINT NOT NULL, updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, INDEX idx_company_date (company_id, report_date), INDEX idx_company_machine (company_id, machine_no), INDEX idx_company_emp (company_id, employee_id) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4"); /* ---- Ensure machine master table (safety) ---- */ $pdo->exec(" CREATE TABLE IF NOT EXISTS roll_palti_machine_data ( id BIGINT PRIMARY KEY AUTO_INCREMENT, company_id BIGINT NOT NULL, machine_no VARCHAR(50) NOT NULL, khata VARCHAR(100) NULL, employee VARCHAR(120) NOT NULL, created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, updated_at TIMESTAMP NULL DEFAULT NULL ON UPDATE CURRENT_TIMESTAMP, UNIQUE KEY uniq_company_machine (company_id, machine_no), INDEX idx_company (company_id), INDEX idx_emp (company_id, employee) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4"); /* ---- Load machine list + defaults from roll_palti_machine_data ---- */ $machineMap = []; // machine_no => ['khata'=>..., 'employee'=>...] $machines = []; // flat list $st = $pdo->prepare("SELECT machine_no, khata, employee FROM roll_palti_machine_data WHERE company_id = ? ORDER BY machine_no"); $st->execute([$company_id]); foreach ($st->fetchAll(PDO::FETCH_ASSOC) as $r) { $m = trim((string)$r['machine_no']); if ($m==='') continue; $machineMap[$m] = ['khata'=>$r['khata'] ?? '', 'employee'=>$r['employee'] ?? '']; $machines[] = $m; } /* ---- Optional employees dropdown (if master table exists) ---- */ $emps = []; if ($pdo->query("SHOW TABLES LIKE 'company_employee_master'")->fetchColumn()) { $st = $pdo->prepare("SELECT id, name FROM company_employee_master WHERE company_id = ? ORDER BY name"); $st->execute([$company_id]); $emps = $st->fetchAll(PDO::FETCH_ASSOC); } /* ---- POST save ---- */ $msg = null; if ($_SERVER['REQUEST_METHOD']==='POST' && ($_POST['do'] ?? '')==='save') { $report_date = trim($_POST['report_date'] ?? ''); $machine_no = trim($_POST['machine_no'] ?? ''); $total_roll = (int)($_POST['total_roll'] ?? 0); $employee_id = isset($_POST['employee_id']) && $_POST['employee_id'] !== '' ? (int)$_POST['employee_id'] : null; $employee_name = trim($_POST['employee_name'] ?? ''); if ($employee_id && !$employee_name && $pdo->query("SHOW TABLES LIKE 'company_employee_master'")->fetchColumn()) { $st=$pdo->prepare("SELECT name FROM company_employee_master WHERE id=? LIMIT 1"); $st->execute([$employee_id]); $nm = $st->fetchColumn(); if ($nm) $employee_name = trim((string)$nm); } $errs = []; if ($report_date==='') $errs[] = 'Date required'; if ($machine_no==='') $errs[] = 'Machine required'; if ($total_roll<=0) $errs[] = 'Total roll must be > 0'; if ($employee_name==='') $errs[] = 'Employee name required'; if ($errs) { $msg = ['type'=>'error','text'=>implode(', ', $errs)]; } else { $ins = $pdo->prepare("INSERT INTO roll_palti_report (company_id, report_date, machine_no, total_roll, employee_id, employee_name, updated_by) VALUES (:cid,:dt,:m,:tr,:eid,:ename,:uid)"); $ok = $ins->execute([ ':cid'=>$company_id, ':dt'=>$report_date, ':m'=>$machine_no, ':tr'=>$total_roll, ':eid'=>$employee_id, ':ename'=>$employee_name, ':uid'=>$user_id ]); $msg = $ok ? ['type'=>'success','text'=>'Saved!'] : ['type'=>'error','text'=>'DB error while saving']; } } /* ---- Prefill from GET ?machine= ---- */ $pref_machine = $_POST['machine_no'] ?? ($_GET['machine'] ?? ''); $pref_khata = $pref_machine && isset($machineMap[$pref_machine]) ? ($machineMap[$pref_machine]['khata'] ?? '') : ''; $pref_empname = $pref_machine && isset($machineMap[$pref_machine]) ? ($machineMap[$pref_machine]['employee'] ?? '') : ''; ?> <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>Roll/Palti Entry • Mister Manager</title> <meta name="viewport" content="width=device-width, initial-scale=1" /> <style> :root{ --primary:#34A853; --bg:#F5FFF7; --text:#202124; --radius:16px; --shadow:0 8px 20px rgba(0,0,0,.06) } *{box-sizing:border-box} body{margin:0;font-family:Inter,system-ui,Arial,sans-serif;background:var(--bg);color:var(--text)} .wrap{max-width:1024px;margin:24px auto;padding:12px} .card{background:#fff;border-radius:16px;padding:20px;box-shadow:var(--shadow)} .row{display:grid;grid-template-columns:1fr 1fr;gap:14px} label{font-weight:600} input,select{width:100%;padding:12px;border:1px solid #ddd;border-radius:10px} .btn{border:0;border-radius:10px;padding:12px 16px;font-weight:700;cursor:pointer} .btn.primary{background:var(--primary);color:#fff} .btn.link{background:#fff;border:1px solid #ddd} .flash{margin:10px 0;padding:10px;border-radius:10px} .flash.success{background:#E3FCEF;color:#0b7a28} .flash.error{background:#FFEBEE;color:#9c1c1c} table{width:100%;border-collapse:collapse;margin-top:14px} th,td{border-bottom:1px dashed #eee;padding:8px;text-align:left} .muted{color:#666;font-size:.9rem} </style> </head> <body> <?php if ($USE_HEADER && is_file($__header)) { include_once $__header; } ?> <div class="wrap"> <div style="display:flex;justify-content:space-between;align-items:center;margin-bottom:10px"> <h2>Roll / Palti Entry</h2> <div> <a class="btn link" href="/erp/roll_palti_machine_master.php">Machine Master</a> <a class="btn link" href="/erp/roll_palti_report.php">Open Report</a> </div> </div> <?php if($msg){ ?> <div class="flash <?=$msg['type']?>"><?=htmlspecialchars($msg['text'])?></div> <?php } ?> <div class="card"> <form method="post" autocomplete="off" id="entryForm"> <input type="hidden" name="do" value="save"> <div class="row"> <div> <label>Date</label> <input type="date" name="report_date" required value="<?=htmlspecialchars($_POST['report_date'] ?? date('Y-m-d'))?>"> </div> <div> <label>Machine</label> <?php if ($machines) { ?> <select name="machine_no" id="f_machine" required> <option value="">— Select —</option> <?php $sel = htmlspecialchars($pref_machine, ENT_QUOTES, 'UTF-8'); foreach ($machines as $m) { $s = ($sel===$m)?'selected':''; echo "<option $s>".htmlspecialchars($m)."</option>"; } ?> </select> <div class="muted" style="margin-top:6px">Khata: <span id="khataView"><?=htmlspecialchars($pref_khata)?></span></div> <?php } else { ?> <input type="text" name="machine_no" id="f_machine_text" placeholder="e.g., M12" required value="<?=htmlspecialchars($pref_machine)?>"> <?php } ?> </div> </div> <div class="row"> <div> <label>Total Roll</label> <input type="number" name="total_roll" min="1" step="1" required value="<?=htmlspecialchars($_POST['total_roll'] ?? '')?>"> </div> <div> <label>Employee</label> <?php if ($emps) { $prefId = $_POST['employee_id'] ?? ''; ?> <select name="employee_id" id="f_employee_id"> <option value="">— Select —</option> <?php foreach($emps as $e){ $s = ($prefId==$e['id'])?'selected':''; ?> <option value="<?=$e['id']?>" <?=$s?>><?=htmlspecialchars($e['name'])?></option> <?php } ?> </select> <div style="margin-top:6px"> <input type="text" name="employee_name" id="f_employee_name" placeholder="Employee name (auto from machine or dropdown)" value="<?=htmlspecialchars($_POST['employee_name'] ?? $pref_empname)?>"> <div class="muted">Machine select karte hi naam yahan aa jayega (edit allowed).</div> </div> <?php } else { ?> <input type="text" name="employee_name" id="f_employee_name" required placeholder="Employee name" value="<?=htmlspecialchars($_POST['employee_name'] ?? $pref_empname)?>"> <?php } ?> </div> </div> <div style="margin-top:12px;display:flex;gap:10px"> <button class="btn primary" type="submit">Save</button> <a class="btn link" href="/erp/roll_palti_report.php?from=<?=date('Y-m-01')?>&to=<?=date('Y-m-d')?>">This Month Report</a> </div> </form> </div> <div class="card" style="margin-top:16px"> <div style="font-weight:700;margin-bottom:8px">Recent entries</div> <table> <thead><tr><th>Date</th><th>Machine</th><th>Total Roll</th><th>Employee</th><th>Updated By</th><th>Updated At</th></tr></thead> <tbody> <?php $q = $pdo->prepare("SELECT r.*, u.name AS updated_by_name FROM roll_palti_report r LEFT JOIN users u ON u.id = r.updated_by WHERE r.company_id = ? ORDER BY r.id DESC LIMIT 10"); $q->execute([$company_id]); $rows = $q->fetchAll(PDO::FETCH_ASSOC); if(!$rows){ echo '<tr><td colspan="6">No entries yet.</td></tr>'; } foreach($rows as $r){ echo '<tr>'. '<td>'.htmlspecialchars($r['report_date']).'</td>'. '<td>'.htmlspecialchars($r['machine_no']).'</td>'. '<td>'.(int)$r['total_roll'].'</td>'. '<td>'.htmlspecialchars($r['employee_name']).'</td>'. '<td>'.htmlspecialchars($r['updated_by_name'] ?? $r['updated_by']).'</td>'. '<td>'.htmlspecialchars($r['updated_at']).'</td>'. '</tr>'; } ?> </tbody> </table> </div> </div> <?php if ($machines) { ?> <script> // Machine -> khata + employee auto-fill const MACHINE_MAP = <?=json_encode($machineMap, JSON_UNESCAPED_UNICODE|JSON_UNESCAPED_SLASHES)?>; const sel = document.getElementById('f_machine'); const khv = document.getElementById('khataView'); const inpName = document.getElementById('f_employee_name'); const ddEmp = document.getElementById('f_employee_id'); function applyMachineDefaults(m){ const info = MACHINE_MAP[m] || {khata:'', employee:''}; if (khv) khv.textContent = info.khata || ''; if (inpName && (!inpName.value || inpName.value.trim()==='')) inpName.value = info.employee || ''; // if dropdown exists, try to match by name if (ddEmp && ddEmp.options) { const target = (info.employee || '').toLowerCase(); let matched = false; for (const opt of ddEmp.options) { if (opt.textContent && opt.textContent.toLowerCase() === target) { opt.selected = true; matched = true; break; } } if (!matched) ddEmp.value = ''; } } sel.addEventListener('change', e => applyMachineDefaults(e.target.value)); // apply on load if preselected if (sel.value) applyMachineDefaults(sel.value); </script> <?php } ?> </body> </html>