« Back to History
yarn_company_list.php
|
20260721_154034.php
Initial Bulk Import
Copy Code
<?php /* ========================================================= File: /erp/yarn_company_list.php Purpose: List & Search Yarn Company Data (company-scoped) ========================================================= */ error_reporting(E_ALL); ini_set('display_errors', 1); /* ---- 1. Auth & Context ---- */ require __DIR__ . '/modules/auth/auth.php'; require_login(); $u = auth_user(); $company_id = (int)$u['company_id']; $pdo = $GLOBALS['pdo'] ?? null; if (!$pdo) { require __DIR__ . '/core/db.php'; $pdo = $GLOBALS['pdo']; } /* ---- 2. Helpers ---- */ function h($s){ return htmlspecialchars((string)$s, ENT_QUOTES, 'UTF-8'); } function qkept(array $extra=[]){ $keep = $_GET; foreach($extra as $k=>$v){ $keep[$k]=$v; } return '?' . http_build_query($keep); } /* ---- 3. Params (search + pagination + sort) ---- */ $q_stock = trim($_GET['stock_type'] ?? ''); $q_ytypes = trim($_GET['yarn_types'] ?? ''); $q_comp = trim($_GET['company_name'] ?? ''); $q_denier = trim($_GET['denier'] ?? ''); $q_color = trim($_GET['color'] ?? ''); $page = max(1, (int)($_GET['page'] ?? 1)); $per_page = 25; $sort = in_array($_GET['sort'] ?? '', ['stock_type','yarn_types','company_name','denier','color','created_at']) ? $_GET['sort'] : 'created_at'; $dir = strtoupper($_GET['dir'] ?? 'DESC') === 'ASC' ? 'ASC' : 'DESC'; /* ---- 4. Build WHERE & Bindings ---- */ $where = ['company_id = :company_id']; $bind = [':company_id' => $company_id]; if ($q_stock !== '') { $where[] = 'stock_type = :stock_type'; $bind[':stock_type'] = $q_stock; } if ($q_ytypes !== '') { $where[] = 'yarn_types LIKE :yarn_types'; $bind[':yarn_types'] = '%'.$q_ytypes.'%'; } if ($q_comp !== '') { $where[] = 'company_name LIKE :company_name';$bind[':company_name']= '%'.$q_comp.'%'; } if ($q_denier !== '') { $where[] = 'denier LIKE :denier'; $bind[':denier'] = '%'.$q_denier.'%'; } if ($q_color !== '') { $where[] = 'color LIKE :color'; $bind[':color'] = '%'.$q_color.'%'; } $where_sql = 'WHERE ' . implode(' AND ', $where); /* ---- 5. Export CSV (Execute before any HTML) ---- */ if (isset($_GET['export']) && $_GET['export']==='csv') { $sqlCsv = "SELECT stock_type, yarn_types, company_name, denier, color, created_at FROM yarn_company_data $where_sql ORDER BY $sort $dir"; $st = $pdo->prepare($sqlCsv); $st->execute($bind); header('Content-Type: text/csv'); header('Content-Disposition: attachment; filename="yarn_company_'.date('Ymd').'.csv"'); $out = fopen('php://output','w'); fputcsv($out, ['Stock Type','Yarn Type(s)','Company','Denier','Color','Created']); while($row = $st->fetch(PDO::FETCH_ASSOC)) fputcsv($out, $row); fclose($out); exit; } /* ---- 6. Total Count & Pagination ---- */ $stmt = $pdo->prepare("SELECT COUNT(*) FROM yarn_company_data $where_sql"); $stmt->execute($bind); $total = (int)$stmt->fetchColumn(); $pages = max(1, (int)ceil($total / $per_page)); $page = min($page, $pages); $offset = ($page - 1) * $per_page; /* ---- 7. Fetch Data ---- */ $sql = "SELECT * FROM yarn_company_data $where_sql ORDER BY $sort $dir LIMIT $per_page OFFSET $offset"; $stmt = $pdo->prepare($sql); foreach ($bind as $k=>$v) $stmt->bindValue($k,$v); $stmt->execute(); $rows = $stmt->fetchAll(PDO::FETCH_ASSOC); function sort_link($col, $label){ global $sort, $dir; $nextDir = ($sort === $col && $dir === 'DESC') ? 'asc' : 'desc'; $icon = ($sort === $col) ? ($dir === 'ASC' ? ' <i class="bi bi-sort-up"></i>' : ' <i class="bi bi-sort-down"></i>') : ''; return '<a class="text-decoration-none fw-bold text-dark" href="'.qkept(['sort'=>$col, 'dir'=>$nextDir, 'page'=>1]).'">'.$label.$icon.'</a>'; } require_once __DIR__ . '/partials/header.php'; ?> <div class="container-fluid py-4"> <div class="d-flex justify-content-between align-items-center mb-3"> <div> <h4 class="mb-0 fw-bold text-uppercase">Yarn Company Master</h4> <small class="text-muted">Total Records: <strong><?= $total ?></strong></small> </div> <div class="d-flex gap-2"> <a class="btn btn-outline-success btn-sm px-3 shadow-sm" href="<?= h(qkept(['export'=>'csv'])) ?>"> <i class="bi bi-file-earmark-excel"></i> Export CSV </a> <a class="btn btn-primary btn-sm px-3 shadow-sm" href="yarn_company_add.php"> <i class="bi bi-plus-lg"></i> Add New </a> </div> </div> <div class="card border-0 shadow-sm mb-4"> <div class="card-body bg-light"> <form method="get" class="row g-2 align-items-end"> <div class="col-md-2"> <label class="form-label small fw-bold">Stock Type</label> <select name="stock_type" class="form-select form-select-sm"> <option value="">All Types</option> <?php foreach(['Yarn','TFO','TPM','Zari','Kasab'] as $t): ?> <option value="<?= $t ?>" <?= ($q_stock===$t)?'selected':'' ?>><?= $t ?></option> <?php endforeach; ?> </select> </div> <div class="col-md-2"> <label class="form-label small fw-bold">Yarn Type</label> <input type="text" name="yarn_types" class="form-control form-control-sm" placeholder="Yarn Type..." value="<?= h($q_ytypes) ?>"> </div> <div class="col-md-2"> <label class="form-label small fw-bold">Company Name</label> <input type="text" name="company_name" class="form-control form-control-sm" placeholder="Company..." value="<?= h($q_comp) ?>"> </div> <div class="col-md-2"> <label class="form-label small fw-bold">Denier</label> <input type="text" name="denier" class="form-control form-control-sm" placeholder="Denier..." value="<?= h($q_denier) ?>"> </div> <div class="col-md-2"> <label class="form-label small fw-bold">Color</label> <input type="text" name="color" class="form-control form-control-sm" placeholder="Color..." value="<?= h($q_color) ?>"> </div> <div class="col-md-2 d-flex gap-1"> <button class="btn btn-dark btn-sm w-100" type="submit">Search</button> <a class="btn btn-secondary btn-sm w-100" href="?">Reset</a> </div> <input type="hidden" name="sort" value="<?= h($sort) ?>"> <input type="hidden" name="dir" value="<?= h(strtolower($dir)) ?>"> </form> </div> </div> <div class="card border-0 shadow-sm overflow-hidden"> <div class="table-responsive"> <table class="table table-hover align-middle mb-0"> <thead class="table-light"> <tr> <th class="ps-3"><?= sort_link('stock_type','Stock Type') ?></th> <th><?= sort_link('yarn_types','Yarn Type(s)') ?></th> <th><?= sort_link('company_name','Company') ?></th> <th><?= sort_link('denier','Denier') ?></th> <th><?= sort_link('color','Color') ?></th> <th class="pe-3"><?= sort_link('created_at','Created') ?></th> </tr> </thead> <tbody> <?php if(!$rows): ?> <tr><td colspan="6" class="text-center py-5 text-muted">No records found.</td></tr> <?php else: foreach($rows as $r): ?> <tr> <td class="ps-3 fw-bold text-primary"><?= h($r['stock_type']) ?></td> <td><?= h($r['yarn_types']) ?></td> <td><?= h($r['company_name']) ?></td> <td><span class="badge bg-secondary"><?= h($r['denier']) ?></span></td> <td><?= h($r['color']) ?></td> <td class="text-muted small pe-3"><?= date('d-M-y H:i', strtotime($r['created_at'])) ?></td> </tr> <?php endforeach; endif; ?> </tbody> </table> </div> </div> <?php if($pages > 1): ?> <div class="d-flex justify-content-between align-items-center mt-3"> <div class="text-muted small">Page <?= $page ?> of <?= $pages ?></div> <nav> <ul class="pagination pagination-sm mb-0 shadow-sm"> <li class="page-item <?= ($page<=1)?'disabled':'' ?>"> <a class="page-link" href="<?= qkept(['page'=>1]) ?>">«</a> </li> <li class="page-item <?= ($page<=1)?'disabled':'' ?>"> <a class="page-link" href="<?= qkept(['page'=>$page-1]) ?>">‹</a> </li> <li class="page-item active"><span class="page-link"><?= $page ?></span></li> <li class="page-item <?= ($page>=$pages)?'disabled':'' ?>"> <a class="page-link" href="<?= qkept(['page'=>$page+1]) ?>">›</a> </li> <li class="page-item <?= ($page>=$pages)?'disabled':'' ?>"> <a class="page-link" href="<?= qkept(['page'=>$pages]) ?>">»</a> </li> </ul> </nav> </div> <?php endif; ?> </div> <?php require_once __DIR__ . '/partials/footer.php'; ?>