« Back to History
production_karigar_report.php
|
20260721_154033.php
Initial Bulk Import
Copy Code
<?php // ============================================================ // File: /erp/production_karigar_report.php // Purpose: Karigar Meter Report by Date (vertical view + print split pages) // ============================================================ error_reporting(E_ALL); ini_set('display_errors', 1); require_once __DIR__ . '/modules/auth/page_acl.php'; $ctx = page_require_access('production_report'); $u = $ctx['user'] ?? null; $pdo = $ctx['pdo'] ?? null; $company_id = (int)($ctx['company_id'] ?? 0); if (!$pdo) { require_once __DIR__ . '/core/db.php'; $pdo = $GLOBALS['pdo'] ?? null; } if (!function_exists('h')) { function h($s) { return htmlspecialchars((string)$s, ENT_QUOTES, 'UTF-8'); } } // ==== FILTERS ==== $filter_type = $_GET['filter'] ?? 'this_month'; $from_date = $_GET['from'] ?? null; $to_date = $_GET['to'] ?? null; $month = $_GET['month'] ?? null; $tz = new DateTimeZone('+05:30'); $today = new DateTime('now', $tz); switch ($filter_type) { case 'select_month': $month = $month ?: $today->format('Y-m'); $from_date = (new DateTime($month . '-01', $tz))->format('Y-m-d'); $to_date = (new DateTime($from_date, $tz))->modify('last day of this month')->format('Y-m-d'); break; case 'period': if (!$from_date || !$to_date) { $to_date = $today->format('Y-m-d'); $from_date = (new DateTime($to_date, $tz))->modify('-6 days')->format('Y-m-d'); } break; default: $from_date = (new DateTime($today->format('Y-m-01'), $tz))->format('Y-m-d'); $to_date = (new DateTime($from_date, $tz))->modify('last day of this month')->format('Y-m-d'); break; } // ==== QUERY ==== $sql = " SELECT j.karigar_id, COALESCE(k.karigar_name, CONCAT('K-', j.karigar_id)) AS karigar_name, pe.entry_date, SUM(j.meter) AS total_meter FROM production_entry AS pe JOIN JSON_TABLE( pe.lines_json, '$[*]' COLUMNS ( karigar_id INT PATH '$.karigar_id', meter DECIMAL(10,3) PATH '$.meter' ) ) AS j ON 1=1 LEFT JOIN loom_karigar_master AS k ON k.id = j.karigar_id AND k.company_id = pe.company_id WHERE pe.company_id = :cid AND pe.entry_date BETWEEN :from_date AND :to_date GROUP BY j.karigar_id, k.karigar_name, pe.entry_date ORDER BY pe.entry_date ASC, total_meter DESC "; $stmt = $pdo->prepare($sql); $stmt->execute([ ':cid' => $company_id, ':from_date' => $from_date, ':to_date' => $to_date ]); $rows = $stmt->fetchAll(PDO::FETCH_ASSOC); // ==== Pivot ==== $dates = []; $karigars = []; $data = []; foreach ($rows as $r) { $d = $r['entry_date']; $kid = (int)$r['karigar_id']; $kname = $r['karigar_name'] ?? ('K-' . $kid); $m = (float)$r['total_meter']; $dates[$d] = $d; $karigars[$kid] = $kname; $data[$d][$kid] = ($data[$d][$kid] ?? 0) + $m; } asort($karigars); sort($dates); require_once __DIR__ . '/partials/header.php'; ?> <div style="background:#fff;padding:10px;border-radius:8px;box-shadow:0 0 6px rgba(0,0,0,0.1);"> <div style="display:flex;justify-content:space-between;align-items:center;"> <div> <h3 style="margin:0 0 6px 0;">Karigar Meter Report</h3> <p style="margin:0;font-size:13px;">Period: <b><?=h($from_date)?></b> → <b><?=h($to_date)?></b></p> </div> <div> <form method="get" style="display:inline-block;margin-right:8px;"> <select name="filter" onchange="this.form.submit()" style="padding:3px;font-size:13px;"> <option value="this_month" <?= $filter_type==='this_month'?'selected':'' ?>>This month</option> <option value="select_month" <?= $filter_type==='select_month'?'selected':'' ?>>Select month</option> <option value="period" <?= $filter_type==='period'?'selected':'' ?>>Custom period</option> </select> <input type="month" name="month" value="<?=h($month)?>" style="padding:3px;font-size:13px;"> <input type="date" name="from" value="<?=h($from_date)?>" style="padding:3px;font-size:13px;"> <input type="date" name="to" value="<?=h($to_date)?>" style="padding:3px;font-size:13px;"> <button type="submit" style="padding:3px 8px;">Apply</button> </form> <button id="printBtn" style="padding:5px 10px;background:#3b82f6;color:#fff;border:none;border-radius:4px;cursor:pointer;">Print</button> </div> </div> <div id="report-wrapper" style="overflow-x:auto;max-height:75vh;margin-top:8px;border:1px solid #ddd;"> <table id="karigar-report-table" style="border-collapse:collapse;width:100%;font-size:11px;text-align:center;"> <thead> <tr> <th style="border:1px solid #ccc;padding:3px 6px;position:sticky;left:0;background:#f7f7f7;">Date</th> <?php foreach ($karigars as $kid => $kname): ?> <th style="border:1px solid #ccc;width:28px;white-space:nowrap;vertical-align:bottom;writing-mode:vertical-rl;text-orientation:mixed;transform:rotate(180deg);font-weight:600;padding:2px 3px;font-size:10px;background:#f0f0f0;"> <?=h($kname)?> </th> <?php endforeach; ?> </tr> </thead> <tbody> <?php foreach ($dates as $d): ?> <tr> <td style="border:1px solid #ccc;padding:3px 6px;position:sticky;left:0;background:#fff;"><?=h($d)?></td> <?php foreach ($karigars as $kid => $kname): $val = $data[$d][$kid] ?? 0; $valStr = $val ? (fmod($val,1)==0 ? (int)$val : rtrim(rtrim(number_format($val,2,'.',''),'0'),'.')) : ''; ?> <td style="border:1px solid #eee;padding:1px 3px;"><?=h($valStr)?></td> <?php endforeach; ?> </tr> <?php endforeach; ?> </tbody> </table> </div> </div> <!-- PRINT SCRIPT (smart split for 2 pages) --> <script> document.getElementById('printBtn').addEventListener('click', function(e){ e.preventDefault(); var table = document.getElementById('karigar-report-table'); if (!table) { alert('Table not found'); return; } var ths = Array.from(table.querySelectorAll('thead th')); var karigarThs = ths.slice(1); var numKarigars = karigarThs.length; var half = Math.ceil(numKarigars / 2); var groups = [karigarThs.slice(0, half), karigarThs.slice(half)]; var bodyRows = Array.from(table.querySelectorAll('tbody tr')).map(tr => Array.from(tr.querySelectorAll('td')).map(td => td.outerHTML)); var style = ` <style> body{font-family:Arial;margin:10px;} table{border-collapse:collapse;width:100%;font-size:11px;} th,td{border:1px solid #999;padding:6px 8px;text-align:center;} th{background:#f3f3f3;font-weight:700;} .vhead{writing-mode:vertical-rl;text-orientation:mixed;transform:rotate(180deg);} .page{page-break-after:always;margin-bottom:10px;} @media print{@page{size:landscape;margin:8mm;}} </style> `; var win = window.open('', '_blank'); win.document.open(); win.document.write('<html><head><title>Print - Karigar Report</title>'+style+'</head><body>'); win.document.write('<div style="font-size:14px;margin-bottom:8px;">Karigar Meter Report<br><small>Period: <?=h($from_date)?> → <?=h($to_date)?></small></div>'); groups.forEach((group,i)=>{ var header = '<tr><th>Date</th>' + group.map(th=>'<th class="vhead">'+th.innerText+'</th>').join('') + '</tr>'; var body = bodyRows.map(row=>{ var dateCell = row[0]; var rowHtml = '<tr>'+dateCell; var start = i===0 ? 1 : half+1; var end = i===0 ? half+1 : numKarigars+1; for (let j=start;j<end;j++){ rowHtml += row[j] ?? '<td></td>'; } rowHtml += '</tr>'; return rowHtml; }).join(''); win.document.write('<div class="page"><table><thead>'+header+'</thead><tbody>'+body+'</tbody></table></div>'); }); win.document.write('</body></html>'); win.document.close(); setTimeout(()=>{ win.print(); setTimeout(()=>{win.close();},300); },500); }); </script> <?php require_once __DIR__ . '/partials/footer.php'; ?>