« Back to History
machine_efficiency_summary.php
|
20260722_120325.php
Initial Domain Snapshot
Copy Code
<?php /* ========================================================== Machine Efficiency Summary Screen = Print = PDF ========================================================== */ error_reporting(E_ALL); ini_set('display_errors', 1); /* ---------------- AUTH ---------------- */ require_once __DIR__ . '/../modules/auth/page_acl.php'; $ctx = page_require_access('machine_efficiency_summary'); $pdo = $ctx['pdo'] ?? null; $company_id = (int)($ctx['company_id'] ?? 0); if (!($pdo instanceof PDO)) { http_response_code(500); exit('DB connection missing'); } /* ---------------- DATE RANGE ---------------- */ $from_date = $_GET['from'] ?? date('Y-m-01'); $to_date = $_GET['to'] ?? date('Y-m-t'); try { $fd = new DateTime($from_date); $td = new DateTime($to_date); if ($fd > $td) { [$fd, $td] = [$td, $fd]; $from_date = $fd->format('Y-m-d'); $to_date = $td->format('Y-m-d'); } } catch (Throwable $e) { $from_date = date('Y-m-01'); $to_date = date('Y-m-t'); } /* ---------------- QUALITIES ---------------- */ $qualityMap = []; $st = $pdo->prepare(" SELECT id, COALESCE(avg_per_day_pro,0) AS expected FROM qualities WHERE company_id = :cid "); $st->execute([':cid'=>$company_id]); foreach ($st->fetchAll(PDO::FETCH_ASSOC) as $r) { $qualityMap[(int)$r['id']] = (float)$r['expected']; } /* ---------------- PRODUCTION ---------------- */ $sql = " SELECT machine_id, entry_date, quality_id, SUM(meter_total) AS day_meter FROM production_entry WHERE company_id = :cid AND entry_date BETWEEN :from AND :to GROUP BY machine_id, entry_date, quality_id "; $st = $pdo->prepare($sql); $st->execute([ ':cid'=>$company_id, ':from'=>$from_date, ':to'=>$to_date ]); $rows = $st->fetchAll(PDO::FETCH_ASSOC); /* ---------------- ORGANIZE ---------------- */ $machines = []; foreach ($rows as $r) { $mid = (int)$r['machine_id']; $d = $r['entry_date']; $qid = (int)$r['quality_id']; $mtr = (float)$r['day_meter']; $machines[$mid][$d][] = ['qid'=>$qid,'meter'=>$mtr]; } /* ---------------- MACHINE TOTALS ---------------- */ $final = []; foreach ($machines as $mid=>$days) { $tm = 0; $ex = 0; foreach ($days as $d=>$rows) { $ds = 0; $de = 0; foreach ($rows as $e) { $ds += $e['meter']; $de += $qualityMap[$e['qid']] ?? 0; } if ($ds > 0) { $tm += $ds; $ex += $de; } } $final[$mid] = ['meter'=>$tm,'expected'=>$ex]; } /* ---------------- GROUPS ---------------- */ $groups = [ 'mst1' => [1,40], 'mst2a' => [41,76], 'mst2b' => [77,116], ]; $summary = [ 'mst1'=>['meter'=>0,'expected'=>0], 'mst2a'=>['meter'=>0,'expected'=>0], 'mst2b'=>['meter'=>0,'expected'=>0], ]; foreach ($final as $mid=>$m) { foreach ($groups as $k=>$g) { if ($mid >= $g[0] && $mid <= $g[1]) { $summary[$k]['meter'] += $m['meter']; $summary[$k]['expected'] += $m['expected']; } } } $summary['mst2'] = [ 'meter'=>$summary['mst2a']['meter']+$summary['mst2b']['meter'], 'expected'=>$summary['mst2a']['expected']+$summary['mst2b']['expected'], ]; $summary['all'] = [ 'meter'=>$summary['mst1']['meter']+$summary['mst2']['meter'], 'expected'=>$summary['mst1']['expected']+$summary['mst2']['expected'], ]; /* ---------------- HEADER ---------------- */ require_once __DIR__ . '/../partials/header.php'; ?> <div class="container-fluid mt-3"> <!-- Page Header --> <div class="d-flex justify-content-between align-items-center flex-wrap mb-3"> <div class="mb-3"> <h4 class="fw-bold mb-1"> Machine Efficiency Report – <?= htmlspecialchars($company_name) ?> </h4> </div> <form method="get" class="d-flex align-items-center gap-2 flex-wrap"> <input type="date" name="from" value="<?=$from_date?>" class="form-control form-control-sm" style="width:150px"> <input type="date" name="to" value="<?=$to_date?>" class="form-control form-control-sm" style="width:150px"> <button class="btn btn-sm btn-primary">Load</button> <button type="button" onclick="window.print()" class="btn btn-sm btn-outline-secondary">Print / PDF</button> </form> </div> <!-- Professional Summary Section --> <div class="card shadow-sm border-0"> <div class="card-body"> <div class="row g-4 justify-content-center"> <?php $cards = [ 'mst1'=>'MST 1 (1–40)', 'mst2a'=>'MST 2A (41–76)', 'mst2b'=>'MST 2B (77–116)', 'mst2'=>'MST 2 (A+B)', 'all'=>'ALL (1–116)', ]; foreach ($cards as $k=>$label): $m = $summary[$k]; $eff = $m['expected']>0 ? round($m['meter']/$m['expected']*100,2) : 0; ?> <div class="col-md-6 col-lg-4"> <div class="text-center p-3 border rounded-3 bg-light h-100"> <div class="fw-bold mb-2"><?=$label?></div> <div class="fs-5 fw-bold"><?=number_format($m['meter'])?> m</div> <div class="small text-muted">Expected: <?=number_format($m['expected'])?> m</div> <div class="fw-bold fs-5 mt-1 text-primary"><?=$eff?>%</div> <div class="d-flex justify-content-center mt-3"> <canvas id="c<?=$k?>" width="110" height="110"></canvas> </div> </div> </div> <?php endforeach; ?> </div> </div> </div> </div> <?php require_once __DIR__ . '/../partials/footer.php'; ?> <script src="https://cdn.jsdelivr.net/npm/chart.js"></script> <script> <?php foreach ($cards as $k=>$v): $m=$summary[$k]; $a=$m['meter']; $b=max(0,$m['expected']-$a); ?> new Chart(document.getElementById('c<?=$k?>'),{ type:'doughnut', data:{datasets:[{data:[<?=$a?>,<?=$b?>],backgroundColor:['#16a34a','#e5e7eb'],borderWidth:0}]}, options:{responsive:false,maintainAspectRatio:false,cutout:'70%',plugins:{legend:{display:false}}} }); <?php endforeach; ?> </script> <style> @media print { /* Remove header */ .main-header-bar, .nav-bottom-strip, footer { display: none !important; } /* RESET ALL SPACING */ html, body { margin: 0 !important; padding: 0 !important; } .container-fluid { margin: 0 !important; padding: 5mm !important; } /* Force 2 columns */ .col-md-6, .col-lg-4 { width: 50% !important; max-width: 50% !important; flex: 0 0 50% !important; } .card { page-break-inside: avoid !important; box-shadow: none !important; margin-bottom: 10px !important; } canvas { width: 95px !important; height: 95px !important; } } </style>