« Back to History
yarn_stock1.php
|
20260722_120325.php
Initial Domain Snapshot
Copy Code
<?php /* ========================================================= File: /erp/yarn_stock.php Purpose: Yarn / TFO / TPM / Zari / Kasab Stock Report Default Sort: Weight DESC ========================================================= */ error_reporting(E_ALL); ini_set('display_errors', 1); /* ---------- PRINT MODE ---------- */ $print_only = (isset($_GET['print']) && (int)$_GET['print'] === 1); $USE_HEADER = !$print_only; /* ---------- AUTH ---------- */ require_once __DIR__ . '/modules/auth/page_acl.php'; $ctx = page_require_access('yarn_entry'); $company_id = (int)($ctx['company_id'] ?? 0); $u = $ctx['user'] ?? null; $pdo = $ctx['pdo'] ?? null; if (!$pdo) require_once __DIR__ . '/core/db.php'; /* ---------- HELPERS ---------- */ function h($s){ return htmlspecialchars((string)$s, ENT_QUOTES, 'UTF-8'); } /* ---------- FILTERS ---------- */ $flt_type = trim($_GET['yarn_type'] ?? ''); $flt_comp = trim($_GET['company'] ?? ''); $flt_denier = trim($_GET['denier'] ?? ''); $flt_color = trim($_GET['color'] ?? ''); $flt_stock_type = trim($_GET['stock_type'] ?? 'Yarn'); $flt_show_zero = trim($_GET['show_zero'] ?? 'with_zero'); $sort = $_GET['sort'] ?? 'weight_desc'; // ✅ DEFAULT $hide_zero = ($flt_show_zero === 'without_zero'); /* ---------- TABLE MAP ---------- */ function in_out_tables_for(string $type): array { $type = ucfirst(strtolower($type)); return match ($type) { 'Tfo' => ['tfo_in','tfo_out'], 'Tpm' => ['tpm_in','tpm_out'], 'Zari' => ['zari_in','zari_out'], 'Kasab' => ['kasab_in','kasab_out'], default => ['yarn_in','yarn_out'], }; } /* ---------- CORE AGGREGATION ---------- */ function build_stock(PDO $pdo, int $company_id, array $filters, string $stock_type, bool $hide_zero, string $sort): array { $types = (strtolower($stock_type)==='all') ? ['Yarn','TFO','TPM','Zari','Kasab'] : [ucfirst(strtolower($stock_type))]; $rows = []; $tot_boxes = 0; $tot_weight = 0; foreach ($types as $stype) { [$in_tbl,$out_tbl] = in_out_tables_for($stype); $where = "company_id = :cid"; $params = [':cid'=>$company_id]; foreach (['yarn_type','company','denier','color'] as $c) { if (!empty($filters[$c])) { $where .= " AND $c = :$c"; $params[":$c"] = $filters[$c]; } } $sql = " SELECT i.yarn_type,i.company,i.denier,i.color, SUM(i.boxes) AS boxes_in, SUM(i.weight) AS weight_in, COALESCE(o.boxes_out,0) AS boxes_out, COALESCE(o.weight_out,0) AS weight_out FROM $in_tbl i LEFT JOIN ( SELECT yarn_type,company,denier,color, SUM(boxes) boxes_out, SUM(weight) weight_out FROM $out_tbl WHERE $where GROUP BY yarn_type,company,denier,color ) o USING (yarn_type,company,denier,color) WHERE $where GROUP BY yarn_type,company,denier,color "; $st = $pdo->prepare($sql); $st->execute($params); foreach ($st as $r) { $cur_boxes = max(0,$r['boxes_in'] - $r['boxes_out']); $cur_weight = max(0,$r['weight_in'] - $r['weight_out']); if ($hide_zero && $cur_boxes==0 && $cur_weight==0) continue; $rows[] = [ 'stock_type'=>$stype, 'yarn_type'=>$r['yarn_type'], 'company'=>$r['company'], 'denier'=>$r['denier'], 'color'=>$r['color'], 'cur_boxes'=>$cur_boxes, 'cur_weight'=>$cur_weight, 'avg_per_box'=>($cur_boxes>0?$cur_weight/$cur_boxes:0), 'status'=>($cur_boxes==0?'Empty':'OK') ]; $tot_boxes += $cur_boxes; $tot_weight += $cur_weight; } } /* ---------- SORT (DEFAULT = WEIGHT DESC) ---------- */ usort($rows, function($a,$b) use ($sort){ return match ($sort) { 'weight_asc' => $a['cur_weight'] <=> $b['cur_weight'], 'boxes_desc' => $b['cur_boxes'] <=> $a['cur_boxes'], 'boxes_asc' => $a['cur_boxes'] <=> $b['cur_boxes'], default => $b['cur_weight'] <=> $a['cur_weight'], // weight_desc }; }); return [ 'rows'=>$rows, 'tot_boxes'=>$tot_boxes, 'tot_weight'=>$tot_weight, 'tot_avg'=>($tot_boxes>0?$tot_weight/$tot_boxes:0) ]; } ?> <!doctype html> <html> <head> <meta charset="utf-8"> <title>Yarn Stock Report</title> <link rel="stylesheet" href="/erp/public/assets/css/main.css"> </head> <body> <?php if($USE_HEADER) require __DIR__.'/partials/header.php'; ?> <div class="wrap"> <div class="card"> <form method="get" class="form-grid"> <select name="stock_type" onchange="this.form.submit()"> <?php foreach(['All','Yarn','TFO','TPM','Zari','Kasab'] as $s): ?> <option <?=($flt_stock_type===$s?'selected':'')?>><?=$s?></option> <?php endforeach; ?> </select> <select name="sort" onchange="this.form.submit()"> <option value="weight_desc" <?=($sort==='weight_desc'?'selected':'')?>>Weight ↓</option> <option value="weight_asc" <?=($sort==='weight_asc'?'selected':'')?>>Weight ↑</option> <option value="boxes_desc" <?=($sort==='boxes_desc'?'selected':'')?>>Boxes ↓</option> <option value="boxes_asc" <?=($sort==='boxes_asc'?'selected':'')?>>Boxes ↑</option> </select> </form> <?php $data = build_stock( $pdo,$company_id, ['yarn_type'=>$flt_type,'company'=>$flt_comp,'denier'=>$flt_denier,'color'=>$flt_color], $flt_stock_type, $hide_zero, $sort ); ?> <table class="table"> <thead> <tr> <th>Yarn</th><th>Company</th><th>Denier</th><th>Color</th> <th class="right">Boxes</th> <th class="right">Weight</th> <th class="right">AVG</th> </tr> </thead> <tbody> <?php foreach($data['rows'] as $r): ?> <tr> <td><?=h($r['yarn_type'])?></td> <td><?=h($r['company'])?></td> <td><?=h($r['denier'])?></td> <td><?=h($r['color'])?></td> <td class="right"><?=number_format($r['cur_boxes'],3)?></td> <td class="right"><?=number_format($r['cur_weight'],3)?></td> <td class="right"><?=number_format($r['avg_per_box'],3)?></td> </tr> <?php endforeach; ?> </tbody> </table> </div> </div> <?php if($USE_HEADER) require __DIR__.'/partials/footer.php'; ?> </body> </html>