« Back to History
yarn_entry_view.php
|
20260721_154034.php
Initial Bulk Import
Copy Code
<?php /* ========================================================= yarn_entry_view.php Purpose: Yarn View (Date Range + Denier Filter) ========================================================= */ require_once __DIR__ . '/modules/auth/page_acl.php'; $ctx = page_require_access('yarn_entry_view'); $pdo = $ctx['pdo']; $company_id = (int)$ctx['company_id']; function h($s){ return htmlspecialchars((string)$s, ENT_QUOTES, 'UTF-8'); } function q1(PDO $pdo,$sql,$p=[]){ $st=$pdo->prepare($sql); $st->execute($p); return $st->fetch(PDO::FETCH_ASSOC); } $allowed_tables = [ 'yarn_in' => 'Yarn In', 'yarn_out' => 'Yarn Out', 'tpm_in' => 'TPM In', 'tpm_out' => 'TPM Out', 'tfo_in' => 'TFO In', 'tfo_out' => 'TFO Out', 'zari_in' => 'Zari In', 'zari_out' => 'Zari Out', ]; $tbl = $_GET['tbl'] ?? 'yarn_out'; if (!isset($allowed_tables[$tbl])) $tbl = 'yarn_out'; /* ---------- pagination ---------- */ $page = max(1, (int)($_GET['page'] ?? 1)); $perPage = 50; $offset = ($page - 1) * $perPage; /* ---------- filters ---------- */ $f_company = trim($_GET['f_company'] ?? ''); $f_yarn_type = trim($_GET['f_yarn_type'] ?? ''); $f_denier = trim($_GET['f_denier'] ?? ''); $f_date_from = trim($_GET['f_date_from'] ?? ''); $f_date_to = trim($_GET['f_date_to'] ?? ''); /* ---------- table columns ---------- */ $cols = $pdo->prepare("DESCRIBE `$tbl`"); $cols->execute(); $table_cols = array_column($cols->fetchAll(PDO::FETCH_ASSOC),'Field'); /* ---------- build where ---------- */ $where = " WHERE company_id = :cid "; $params = [':cid'=>$company_id]; if ($f_company !== '' && in_array('company',$table_cols)) { $where .= " AND company LIKE :company "; $params[':company'] = "%$f_company%"; } if ($f_yarn_type !== '' && in_array('yarn_type',$table_cols)) { $where .= " AND yarn_type LIKE :yarn_type "; $params[':yarn_type'] = "%$f_yarn_type%"; } if ($f_denier !== '' && in_array('denier',$table_cols)) { $where .= " AND denier LIKE :denier "; $params[':denier'] = "%$f_denier%"; } /* ----------- DATE RANGE LOGIC ----------- */ if ($f_date_from !== '' && $f_date_to !== '' && in_array('txn_date',$table_cols)) { $where .= " AND txn_date BETWEEN :from AND :to "; $params[':from'] = $f_date_from; $params[':to'] = $f_date_to; } elseif ($f_date_from !== '' && in_array('txn_date',$table_cols)) { $where .= " AND txn_date >= :from "; $params[':from'] = $f_date_from; } elseif ($f_date_to !== '' && in_array('txn_date',$table_cols)) { $where .= " AND txn_date <= :to "; $params[':to'] = $f_date_to; } /* ---------- count ---------- */ $totalRow = q1($pdo,"SELECT COUNT(*) c FROM `$tbl` $where",$params); $total = (int)($totalRow['c'] ?? 0); /* ---------- fetch rows ---------- */ $sql = "SELECT * FROM `$tbl` $where ORDER BY txn_date DESC, id DESC LIMIT :lim OFFSET :off"; $st = $pdo->prepare($sql); foreach ($params as $k=>$v) $st->bindValue($k,$v); $st->bindValue(':lim',$perPage,PDO::PARAM_INT); $st->bindValue(':off',$offset,PDO::PARAM_INT); $st->execute(); $rows = $st->fetchAll(PDO::FETCH_ASSOC); require_once __DIR__ . '/partials/header.php'; ?> <div class="card"> <h3><?=h($allowed_tables[$tbl])?> - View</h3> <form method="get" class="flex" style="gap:12px;align-items:end;margin-bottom:12px;"> <div> <label class="text-muted">Table</label><br> <select name="tbl" onchange="this.form.submit()"> <?php foreach ($allowed_tables as $k=>$v): ?> <option value="<?=h($k)?>" <?= $k===$tbl?'selected':'' ?>><?=h($v)?></option> <?php endforeach; ?> </select> </div> <div> <label class="text-muted">Company</label><br> <input type="text" name="f_company" value="<?=h($f_company)?>"> </div> <div> <label class="text-muted">Yarn Type</label><br> <input type="text" name="f_yarn_type" value="<?=h($f_yarn_type)?>"> </div> <div> <label class="text-muted">Denier</label><br> <input type="text" name="f_denier" value="<?=h($f_denier)?>"> </div> <div> <label class="text-muted">From</label><br> <input type="date" name="f_date_from" value="<?=h($f_date_from)?>"> </div> <div> <label class="text-muted">To</label><br> <input type="date" name="f_date_to" value="<?=h($f_date_to)?>"> </div> <div> <button class="btn">Filter</button> <a class="btn" href="<?=h(basename($_SERVER['PHP_SELF']))?>?tbl=<?=h($tbl)?>">Reset</a> </div> </form> <table class="table"> <thead> <tr> <th>ID</th> <?php $show_cols = ['txn_date','yarn_type','company','denier','color','boxes','weight','assign_to']; foreach ($show_cols as $c) { if (in_array($c,$table_cols)) { echo "<th>".h(ucfirst(str_replace('_',' ',$c)))."</th>"; } } ?> </tr> </thead> <tbody> <?php if(empty($rows)): ?> <tr><td colspan="20" class="text-muted">No records found.</td></tr> <?php else: foreach($rows as $r): ?> <tr> <td><?=h($r['id'])?></td> <?php foreach($show_cols as $c): if(in_array($c,$table_cols)): ?> <td> <?php if($c==='txn_date') echo h(substr($r[$c],0,10)); elseif($c==='boxes') { $bx = (float)($r[$c] ?? 0); $rbx = round($bx); echo (abs($bx - $rbx) <= 0.01) ? h((string)(int)$rbx) : h(number_format($bx,3)); } elseif($c==='weight') echo number_format((float)$r[$c],2); else echo h($r[$c] ?? ''); ?> </td> <?php endif; endforeach; ?> </tr> <?php endforeach; endif; ?> </tbody> </table> <div class="flex" style="justify-content:space-between;margin-top:10px;"> <div class="text-muted">Showing <?=count($rows)?> of <?=$total?></div> <div> <?php $totalPages = (int)ceil($total/$perPage); for($p=1;$p<=max(1,$totalPages);$p++){ $qs = http_build_query(array_merge($_GET,['page'=>$p])); echo '<a class="btn btn-sm" href="'.h(basename($_SERVER['PHP_SELF']).'?'.$qs).'">'.$p.'</a> '; } ?> </div> </div> </div> <?php require_once __DIR__ . '/partials/footer.php'; ?>