« Back to History
taka_out_report.php
|
20260721_154033.php
Initial Bulk Import
Copy Code
<?php /** * taka_out_report.php * Taka Out Report — lists rows from folding_out_entry (date range, party, quality, search, export csv) * Standalone (no header/footer). Uses $ctx['pdo'] if present, else tries core/db.php. * * Save to: /erp/generated_pages/taka_out_report.php */ ini_set('display_errors',1); ini_set('display_startup_errors',1); error_reporting(E_ALL); if (session_status() === PHP_SESSION_NONE) session_start(); // === Try to obtain $pdo, $COMPANY_ID, $USER_ID gracefully === $pdo = null; $COMPANY_ID = 0; $USER_ID = 0; if (isset($ctx) && is_array($ctx)) { $pdo = $ctx['pdo'] ?? null; $COMPANY_ID = (int)($ctx['company_id'] ?? 0); $USER_ID = (int)(($ctx['user']['id'] ?? 0)); } else { $maybe = __DIR__ . '/core/db.php'; if (file_exists($maybe)) { try { require_once $maybe; } catch (Throwable $e) { /* ignore */ } // common names if (!isset($pdo)) { if (isset($DB) && ($DB instanceof PDO)) $pdo = $DB; if (isset($conn) && ($conn instanceof PDO)) $pdo = $conn; } } } function h($s){ return htmlspecialchars((string)$s, ENT_QUOTES|ENT_SUBSTITUTE, 'UTF-8'); } /* ====== Prefetch parties and qualities for filters ====== */ $PARTIES = []; $QUALITIES = []; if ($pdo instanceof PDO) { try { // parties distinct from table $sqlP = "SELECT DISTINCT COALESCE(party,'') AS p FROM folding_out_entry"; if ($COMPANY_ID) $sqlP .= " WHERE company_id = :cid"; $sqlP .= " ORDER BY p LIMIT 1000"; $st = $pdo->prepare($sqlP); $st->execute($COMPANY_ID ? [':cid'=>$COMPANY_ID] : []); $PARTIES = $st->fetchAll(PDO::FETCH_COLUMN); // qualities from qualities table (if exists) $sqlQ = "SELECT DISTINCT TRIM(COALESCE(quality_name,'')) AS qn FROM qualities WHERE COALESCE(quality_name,'')<>'' ORDER BY qn LIMIT 1000"; $st = $pdo->prepare($sqlQ); $st->execute(); $QUALITIES = $st->fetchAll(PDO::FETCH_COLUMN); } catch (Throwable $e) { // ignore any prefetch errors error_log("TakaReport prefetch error: ".$e->getMessage()); } } // ====== Handle CSV export (GET with export=csv) ====== if (isset($_GET['export']) && $_GET['export'] === 'csv') { if (!($pdo instanceof PDO)) { header('Content-Type: text/plain; charset=utf-8'); echo "DB connection not available. CSV export cannot proceed."; exit; } // reuse the same filter-building below (but we will rebuild here) $f_from = $_GET['from'] ?? ''; $f_to = $_GET['to'] ?? ''; $f_party = $_GET['party'] ?? ''; $f_quality = $_GET['quality'] ?? ''; $f_q = trim($_GET['q'] ?? ''); $where = []; $params = []; if ($COMPANY_ID) { $where[] = "company_id = :cid"; $params[':cid'] = $COMPANY_ID; } if ($f_from !== '') { $where[] = "entry_date >= :from"; $params[':from'] = $f_from; } if ($f_to !== '') { $where[] = "entry_date <= :to"; $params[':to'] = $f_to; } if ($f_party !== '') { $where[] = "party = :party"; $params[':party'] = $f_party; } if ($f_quality !== '') { $where[] = "quality = :quality"; $params[':quality'] = $f_quality; } if ($f_q !== '') { $where[] = "(saree_no LIKE :q OR remark LIKE :q)"; $params[':q'] = "%{$f_q}%"; } $where_sql = $where ? ('WHERE '.implode(' AND ', $where)) : ''; $sql = "SELECT id, company_id, entry_date, quality, saree_no, taka_no, party, remark, created_by, created_at, updated_at FROM folding_out_entry {$where_sql} ORDER BY entry_date DESC, id DESC LIMIT 10000"; // safety cap for export try { $st = $pdo->prepare($sql); $st->execute($params); $rows = $st->fetchAll(PDO::FETCH_ASSOC); } catch (Throwable $e) { header('Content-Type: text/plain; charset=utf-8'); echo "Export query failed: " . $e->getMessage(); exit; } // stream CSV $filename = 'taka_out_report_'.date('Ymd_His').'.csv'; header('Content-Type: text/csv; charset=utf-8'); header('Content-Disposition: attachment; filename="'.$filename.'"'); $out = fopen('php://output','w'); // header row fputcsv($out, array_keys($rows[0] ?? ['id'=>'id','company_id'=>'company_id','entry_date'=>'entry_date','quality'=>'quality','saree_no'=>'saree_no','taka_no'=>'taka_no','party'=>'party','remark'=>'remark','created_by'=>'created_by','created_at'=>'created_at'])); foreach ($rows as $r) { fputcsv($out, array_values($r)); } fclose($out); exit; } // ====== Filters from GET/POST ====== $from = $_REQUEST['from'] ?? ''; $to = $_REQUEST['to'] ?? ''; $party = $_REQUEST['party'] ?? ''; $quality = $_REQUEST['quality'] ?? ''; $q = trim($_REQUEST['q'] ?? ''); // pagination $per_page = 50; $page = max(1, (int)($_GET['page'] ?? 1)); $offset = ($page - 1) * $per_page; $rows = []; $total = 0; if ($pdo instanceof PDO) { try { $where = []; $params = []; if ($COMPANY_ID) { $where[] = "company_id = :cid"; $params[':cid'] = $COMPANY_ID; } if ($from !== '') { $where[] = "entry_date >= :from"; $params[':from'] = $from; } if ($to !== '') { $where[] = "entry_date <= :to"; $params[':to'] = $to; } if ($party !== '') { $where[] = "party = :party"; $params[':party'] = $party; } if ($quality !== '') { $where[] = "quality = :quality"; $params[':quality'] = $quality; } if ($q !== '') { $where[] = "(saree_no LIKE :q OR remark LIKE :q OR party LIKE :q)"; $params[':q'] = "%{$q}%"; } $where_sql = $where ? ('WHERE '.implode(' AND ', $where)) : ''; // total count $count_sql = "SELECT COUNT(*) FROM folding_out_entry {$where_sql}"; $st = $pdo->prepare($count_sql); $st->execute($params); $total = (int)$st->fetchColumn(); // fetch rows $sql = "SELECT id, company_id, entry_date, quality, saree_no, taka_no, party, remark, created_by, created_at, updated_at FROM folding_out_entry {$where_sql} ORDER BY entry_date DESC, id DESC LIMIT :limit OFFSET :offset"; $st = $pdo->prepare($sql); // bind normal params foreach ($params as $k=>$v) $st->bindValue($k,$v); $st->bindValue(':limit', (int)$per_page, PDO::PARAM_INT); $st->bindValue(':offset', (int)$offset, PDO::PARAM_INT); $st->execute(); $rows = $st->fetchAll(PDO::FETCH_ASSOC); } catch (Throwable $e) { error_log("TakaOut query error: ".$e->getMessage()); } } $total_pages = $per_page ? max(1, ceil($total / $per_page)) : 1; // ====== HTML Output ====== ?><!doctype html> <html lang="hi"> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width,initial-scale=1"> <title>Taka Out Report</title> <style> :root{--bg:#f7f7f9;--card:#fff;--accent:#1fae4f;--muted:#6b7280} body{margin:0;font-family:system-ui,Segoe UI,Roboto,Arial;background:var(--bg);color:#111} .wrap{max-width:1100px;margin:22px auto;padding:0 16px} .card{background:var(--card);border-radius:10px;padding:18px;box-shadow:0 8px 24px rgba(0,0,0,0.06)} h1{margin:0 0 10px;font-size:18px} .filters{display:flex;gap:10px;flex-wrap:wrap;margin-bottom:12px;align-items:end} label{display:block;font-size:13px;margin-bottom:6px;color:var(--muted);font-weight:600} input,select{padding:8px 10px;border-radius:6px;border:1px solid #e6e7ea;background:#fff} .btn{padding:8px 12px;border-radius:6px;border:none;background:var(--accent);color:#fff;cursor:pointer;font-weight:700} .muted{background:#6b7280} table{width:100%;border-collapse:collapse;margin-top:12px;font-size:14px} th,td{padding:8px 10px;border-bottom:1px solid #f1f1f4;text-align:left} th{background:#fafafa;font-weight:600} .small{font-size:13px;color:#666} .pager{margin-top:12px;display:flex;gap:8px;align-items:center} .export{margin-left:auto} .notice{padding:10px;border-radius:6px;margin-bottom:10px;font-weight:600} .err{background:#fff7f6;color:#9b2b1b;border:1px solid rgba(255,99,71,0.12)} @media (max-width:720px){ .filters{flex-direction:column; align-items:stretch} table{font-size:13px} } </style> </head> <body> <div class="wrap"> <div class="card"> <h1>Taka Out Report</h1> <?php if (!($pdo instanceof PDO)): ?> <div class="notice err">Database connection not available. Ensure core/db.php or $ctx['pdo'] is present.</div> <?php endif; ?> <form method="get" action="" class="filters" style="align-items:flex-end;"> <div> <label for="from">From</label> <input type="date" id="from" name="from" value="<?php echo h($from); ?>"> </div> <div> <label for="to">To</label> <input type="date" id="to" name="to" value="<?php echo h($to); ?>"> </div> <div> <label for="quality">Quality</label> <select id="quality" name="quality"> <option value="">All Qualities</option> <?php foreach ($QUALITIES as $qn): if (trim($qn) === '') continue; ?> <option value="<?php echo h($qn); ?>" <?php if ($qn === $quality) echo 'selected'; ?>><?php echo h($qn); ?></option> <?php endforeach; ?> </select> </div> <div> <label for="party">Party</label> <select id="party" name="party"> <option value="">All Parties</option> <?php foreach ($PARTIES as $p): if (trim($p) === '') continue; ?> <option value="<?php echo h($p); ?>" <?php if ($p === $party) echo 'selected'; ?>><?php echo h($p); ?></option> <?php endforeach; ?> </select> </div> <div style="flex:1; min-width:200px;"> <label for="q">Search</label> <input id="q" name="q" type="text" placeholder="Saree No, Remark, Party..." value="<?php echo h($q); ?>"> </div> <div style="display:flex;gap:8px;"> <button type="submit" class="btn">Filter</button> <a class="btn muted export" href="?<?php // build export url with current filters $params = $_GET; $params['export'] = 'csv'; echo http_build_query($params); ?>">Export CSV</a> </div> </form> <div class="small">Showing <?php echo count($rows); ?> of <?php echo $total; ?> rows<?php if ($total_pages>1) echo " — page {$page} of {$total_pages}"; ?></div> <table> <thead> <tr> <th>#</th> <th>Date</th> <th>Quality</th> <th>Saree No</th> <th>Taka No</th> <th>Party</th> <th>Remark</th> <th>Created At</th> </tr> </thead> <tbody> <?php if (empty($rows)): ?> <tr><td colspan="8" class="small">No rows found for the selected filters.</td></tr> <?php else: foreach ($rows as $r): ?> <tr> <td><?php echo h($r['id']); ?></td> <td><?php echo h($r['entry_date']); ?></td> <td><?php echo h($r['quality']); ?></td> <td><?php echo h($r['saree_no']); ?></td> <td><?php echo h($r['taka_no']); ?></td> <td><?php echo h($r['party']); ?></td> <td><?php echo h($r['remark']); ?></td> <td><?php echo h($r['created_at']); ?></td> </tr> <?php endforeach; endif; ?> </tbody> </table> <div class="pager"> <?php if ($page > 1): ?> <a class="btn" href="?<?php $p = $_GET; $p['page']=$page-1; echo http_build_query($p); ?>">Prev</a> <?php endif; ?> <?php if ($page < $total_pages): ?> <a class="btn" href="?<?php $p = $_GET; $p['page']=$page+1; echo http_build_query($p); ?>">Next</a> <?php endif; ?> <div style="margin-left:auto" class="small">Per page: <?php echo $per_page; ?></div> </div> </div> </div> </body> </html>