« Back to History
mesr_yarn_pending_from_running_beam.php
|
20260721_154033.php
Initial Bulk Import
Copy Code
<?php error_reporting(E_ALL); ini_set('display_errors', 1); /* ================== BOOTSTRAP ================== */ require_once __DIR__ . '/modules/auth/page_acl.php'; $ctx = page_require_access('mesr_yarn_pending'); $pdo = $ctx['pdo']; $company_id = (int)$ctx['company_id']; $user_id = (int)($ctx['user']['id'] ?? 0); /* ================== HELPERS ================== */ function h($s){ return htmlspecialchars((string)$s, ENT_QUOTES, 'UTF-8'); } /* ================== STEP 1: GET RUNNING BEAMS ================== */ $machineStmt = $pdo->prepare(" SELECT machine_no, primary_running_beam AS p_beam, secondary_running_beam AS s_beam FROM machine_beam_status WHERE company_id = ? "); $machineStmt->execute([$company_id]); $machines = $machineStmt->fetchAll(PDO::FETCH_ASSOC); /* ================== PREPARED STATEMENTS ================== */ $pendingStmt = $pdo->prepare(" SELECT pending_meter FROM beam_book WHERE company_id = ? AND beam_no = ? "); $qualityStmt = $pdo->prepare(" SELECT beam_quality_id FROM beam_load_data WHERE company_id = ? AND beam_no = ? AND event_type = 'loaded' ORDER BY load_date DESC LIMIT 1 "); $yarnStmt = $pdo->prepare(" SELECT y.stock_type, y.yarn_type, y.color, y.denier, y.total_tar FROM beam_quality_yarns y WHERE y.beam_quality_id = ? ORDER BY y.seq_no "); $denierMap = []; $dm = $pdo->query("SELECT stock_type, yarn_type, denier, new_denier FROM yarn_weight_data"); foreach ($dm->fetchAll(PDO::FETCH_ASSOC) as $r) { $k = strtolower($r['stock_type'].'||'.$r['yarn_type'].'||'.$r['denier']); $denierMap[$k] = (float)$r['new_denier']; } /* ================== STEP 2: CALCULATE AGGREGATION ================== */ $final = []; foreach ($machines as $m) { foreach (['p_beam','s_beam'] as $k) { if (empty($m[$k])) continue; $beam_no = (int)$m[$k]; /* pending meter (Beam Book = truth) */ $pendingStmt->execute([$company_id, $beam_no]); $pending_meter = (float)$pendingStmt->fetchColumn(); if ($pending_meter <= 0) continue; /* beam -> quality */ $qualityStmt->execute([$beam_no]); $bq = (int)$qualityStmt->fetchColumn(); if ($bq <= 0) continue; /* yarn breakup */ $yarnStmt->execute([$bq]); $yarns = $yarnStmt->fetchAll(PDO::FETCH_ASSOC); foreach ($yarns as $y) { $key = strtolower( $y['stock_type'].'||'.$y['yarn_type'].'||'.$y['color'].'||'.$y['denier'] ); $new_denier = $denierMap[ strtolower($y['stock_type'].'||'.$y['yarn_type'].'||'.$y['denier']) ] ?? (float)$y['denier']; $meters = $pending_meter * (float)$y['total_tar']; $weight = ($meters * $new_denier) / 9000000; if (!isset($final[$key])) { $final[$key] = [ 'stock_type' => $y['stock_type'], 'yarn_type' => $y['yarn_type'], 'color' => $y['color'], 'denier' => $new_denier, 'meters' => 0, 'weight_kg' => 0 ]; } $final[$key]['meters'] += $meters; $final[$key]['weight_kg'] += $weight; } } } /* ================== SAVE HANDLER ================== */ if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['save_snapshot'])) { $json = json_encode(array_values($final), JSON_UNESCAPED_UNICODE); $ins = $pdo->prepare(" INSERT INTO mesr_yarn_in_pending_meter (company_id, snapshot_date, yarns_json, created_by) VALUES (?, CURDATE(), ?, ?) "); $ins->execute([$company_id, $json, $user_id]); $saved = true; } /* ================== VIEW ================== */ require_once __DIR__ . '/partials/header.php'; ?> <div class="card"> <h3>Running Beam Pending Yarn Report</h3> <?php if (!empty($saved)): ?> <div class="notice notice--success">Snapshot saved successfully.</div> <?php endif; ?> <table class="table table--bordered"> <thead> <tr> <th>Stock Type</th> <th>Yarn Type</th> <th>Color</th> <th>Denier</th> <th>Total Meter</th> <th>Total Weight (kg)</th> </tr> </thead> <tbody> <?php foreach ($final as $r): ?> <tr> <td><?=h($r['stock_type'])?></td> <td><?=h($r['yarn_type'])?></td> <td><?=h($r['color'])?></td> <td><?=number_format($r['denier'],2)?></td> <td><?=number_format($r['meters'],2)?></td> <td><?=number_format($r['weight_kg'],4)?></td> </tr> <?php endforeach; ?> </tbody> </table> <form method="post"> <button class="btn btn-success" name="save_snapshot"> Save Pending Yarn Snapshot </button> </form> </div> <?php require_once __DIR__ . '/partials/footer.php'; ?>