« Back to History
patia.php
|
20261119_140743.php
Initial Domain Snapshot
Copy Code
<?php // /erp/karigar/patia.php declare(strict_types=1); /* --- Session handling --- */ $__sess_dir = dirname(__DIR__, 2) . '/tmp_sessions'; if (is_dir($__sess_dir)) { @ini_set('session.save_path', $__sess_dir); } if (session_status() !== PHP_SESSION_ACTIVE) { session_start(); } if (empty($_SESSION['karigar'])) { header('Location: /erp/karigar/login.php'); exit; } $karigar = $_SESSION['karigar']; $karigar_id = (int)($karigar['id'] ?? $karigar['karigar_id'] ?? 0); $company_id = (int)($karigar['company_id'] ?? $_SESSION['company_id'] ?? 0); require_once dirname(__DIR__) . '/core/db.php'; $pdo = $GLOBALS['pdo'] ?? null; if (!($pdo instanceof PDO) || $karigar_id <= 0) { http_response_code(500); exit('Patia data unavailable.'); } function h($v){ return htmlspecialchars((string)$v, ENT_QUOTES, 'UTF-8'); } function clean_number($v) { if (!is_numeric($v)) return (string)$v; $f = (float)$v; if (floor($f) === $f) return number_format((int)$f); $s = number_format($f, 2, '.', ''); return rtrim(rtrim($s, '0'), '.'); } function period_range($month_input, $period) { $parts = explode('-', $month_input); $y = (int)($parts[0] ?? date('Y')); $m = (int)($parts[1] ?? date('m')); if ($period === '1to15') { return [sprintf('%04d-%02d-01', $y, $m), sprintf('%04d-%02d-15', $y, $m)]; } return [sprintf('%04d-%02d-16', $y, $m), date('Y-m-t', strtotime(sprintf('%04d-%02d-01', $y, $m)))]; } function get_karigar_profile(PDO $pdo, int $company_id, int $karigar_id): array { try { $stmt = $pdo->prepare( "SELECT karigar_name, entry_name, machine_from, machine_to FROM loom_karigar_master WHERE id = :id AND (company_id = :cid OR company_id IS NULL) LIMIT 1" ); $stmt->execute([':id' => $karigar_id, ':cid' => $company_id]); return $stmt->fetch(PDO::FETCH_ASSOC) ?: []; } catch (Throwable $e) { return []; } } function get_machine_name(PDO $pdo, int $company_id, int $machine_id): string { $tables = ['machines', 'machine_list', 'looms', 'machines_master']; foreach ($tables as $table) { try { $stmt = $pdo->prepare( "SELECT COALESCE(name, CONCAT('MC ', id)) AS name FROM `$table` WHERE id = :id AND (company_id = :cid OR company_id IS NULL) LIMIT 1" ); $stmt->execute([':id' => $machine_id, ':cid' => $company_id]); $name = $stmt->fetchColumn(); if ($name) { return (string)$name; } } catch (Throwable $e) { continue; } } return 'MC ' . $machine_id; } function get_machine_options(PDO $pdo, int $company_id, array $profile): array { $options = []; $from = (int)($profile['machine_from'] ?? 0); $to = (int)($profile['machine_to'] ?? 0); if ($from > 0 && $to >= $from) { for ($machine = $from; $machine <= $to; $machine++) { $options[] = ['id' => $machine, 'name' => get_machine_name($pdo, $company_id, $machine)]; } return $options; } try { $stmt = $pdo->prepare( "SELECT DISTINCT machine_id FROM production_entry WHERE company_id = :cid AND machine_id IS NOT NULL ORDER BY machine_id" ); $stmt->execute([':cid' => $company_id]); foreach ($stmt->fetchAll(PDO::FETCH_COLUMN) as $machineId) { $machineId = (int)$machineId; if ($machineId > 0) { $options[] = ['id' => $machineId, 'name' => get_machine_name($pdo, $company_id, $machineId)]; } } } catch (Throwable $e) { } return $options; } $quality_map = []; try { $qualityStmt = $pdo->prepare( "SELECT quality_id, quality_name, rate FROM quality_rates WHERE (company_id = :cid OR company_id IS NULL)" ); $qualityStmt->execute([':cid' => $company_id]); foreach ($qualityStmt->fetchAll(PDO::FETCH_ASSOC) as $qualityRow) { $qid = (int)($qualityRow['quality_id'] ?? 0); if ($qid > 0) { $quality_map[$qid] = [ 'name' => (string)($qualityRow['quality_name'] ?? ('Q' . $qid)), 'rate' => (float)($qualityRow['rate'] ?? 0), ]; } } } catch (Throwable $e) { } $profile = get_karigar_profile($pdo, $company_id, $karigar_id); $machine_quality = []; try { $mqStmt = $pdo->prepare( "SELECT machine_id, quality_id FROM loom_quality_master WHERE (company_id = :cid OR company_id IS NULL) AND machine_id > 0 AND quality_id > 0" ); $mqStmt->execute([':cid' => $company_id]); foreach ($mqStmt->fetchAll(PDO::FETCH_ASSOC) as $mqRow) { $machine_quality[(int)$mqRow['machine_id']] = (int)$mqRow['quality_id']; } } catch (Throwable $e) { $machine_quality = []; } $month_input = $_GET['month'] ?? date('Y-m'); $period = $_GET['period'] ?? '1to15'; $machine_id = isset($_GET['machine_id']) && $_GET['machine_id'] !== '' ? (int)$_GET['machine_id'] : null; $mode = (($_GET['mode'] ?? 'machine') === 'taka') ? 'taka' : 'machine'; $src_table = ($mode === 'taka') ? 'loom_production_entry' : 'production_entry'; [$start_date, $end_date] = period_range($month_input, $period); $query = "SELECT id, entry_date, machine_id, meter_total, lines_json, quality_id FROM `$src_table` WHERE company_id = :cid AND entry_date BETWEEN :start AND :end"; $params = [':cid' => $company_id, ':start' => $start_date, ':end' => $end_date]; if ($machine_id) { $query .= " AND machine_id = :mid"; $params[':mid'] = $machine_id; } $query .= " ORDER BY entry_date, machine_id, id"; $stmt = $pdo->prepare($query); $stmt->execute($params); $rows = $stmt->fetchAll(PDO::FETCH_ASSOC); $period_dates = []; $startDt = new DateTime($start_date); $endDt = new DateTime($end_date); $endDt->modify('+1 day'); $dateRange = new DatePeriod($startDt, new DateInterval('P1D'), $endDt); foreach ($dateRange as $dateItem) { $period_dates[] = $dateItem->format('Y-m-d'); } $col_keys = []; $agg_mtr = []; $totals_col_mtr = []; $summary_quality = []; $totals_date_mtr = []; foreach ($rows as $row) { $entryDate = substr((string)$row['entry_date'], 0, 10); $rowMachineId = (int)($row['machine_id'] ?? 0); $rowQualityId = isset($row['quality_id']) ? (int)$row['quality_id'] : 0; if ($rowQualityId <= 0) { $rowQualityId = $machine_quality[$rowMachineId] ?? 0; } $decoded = json_decode((string)($row['lines_json'] ?? ''), true); if (!is_array($decoded)) { continue; } foreach ($decoded as $line) { $lineKarigar = (int)($line['karigar_id'] ?? $line['karigar'] ?? $line['employee_id'] ?? 0); if ($lineKarigar !== $karigar_id) { continue; } $lineMachineId = (int)($line['machine_id'] ?? $rowMachineId); if ($machine_id && $lineMachineId !== $machine_id) { continue; } $lineDate = substr((string)($line['date'] ?? $entryDate), 0, 10); $lineMeter = (float)($line['meter'] ?? 0); $lineQualityId = (int)($line['quality_id'] ?? $rowQualityId); if ($lineMeter <= 0) { continue; } $colKey = 'machine_' . $lineMachineId; $col_keys[$colKey] = $lineMachineId; if (!isset($agg_mtr[$lineDate])) { $agg_mtr[$lineDate] = []; } if (!isset($agg_mtr[$lineDate][$colKey])) { $agg_mtr[$lineDate][$colKey] = 0.0; } $agg_mtr[$lineDate][$colKey] += $lineMeter; if (!isset($totals_col_mtr[$colKey])) { $totals_col_mtr[$colKey] = 0.0; } $totals_col_mtr[$colKey] += $lineMeter; if (!isset($totals_date_mtr[$lineDate])) { $totals_date_mtr[$lineDate] = 0.0; } $totals_date_mtr[$lineDate] += $lineMeter; if ($lineQualityId > 0) { if (!isset($summary_quality[$lineQualityId])) { $summary_quality[$lineQualityId] = ['mtr' => 0.0, 'amt' => 0.0]; } $rate = isset($quality_map[$lineQualityId]) ? (float)$quality_map[$lineQualityId]['rate'] : 0.0; $summary_quality[$lineQualityId]['mtr'] += $lineMeter; $summary_quality[$lineQualityId]['amt'] += $lineMeter * $rate; } } } asort($col_keys, SORT_NATURAL); $col_names = []; foreach ($col_keys as $colKey => $machineKey) { $col_names[$colKey] = get_machine_name($pdo, $company_id, (int)$machineKey); } $karigarName = trim((string)($profile['entry_name'] ?? $profile['karigar_name'] ?? $karigar['name'] ?? 'Karigar')); $header_label = $karigarName; if (!empty($profile['machine_from']) || !empty($profile['machine_to'])) { $header_label .= ' (' . ($profile['machine_from'] ?? '?') . '-' . ($profile['machine_to'] ?? '?') . ')'; } $grand_total_mtr = array_sum($totals_col_mtr); $grand_total_amt = array_sum(array_column($summary_quality, 'amt')); $machineOptions = get_machine_options($pdo, $company_id, $profile); if (isset($_GET['export_pdf']) && $_GET['export_pdf']) { ob_start(); ?> <h2>My Patia</h2> <div><?= h($header_label) ?> Mode: <?= h($mode === 'taka' ? 'Taka Wise' : 'Machine Wise') ?> Month: <?= h($month_input) ?> Period: <?= h($period) ?></div> <br> <table style="width:50%; border-collapse:collapse; margin-bottom:10px;"> <thead> <tr><th style="border:1px solid #000;padding:6px;">Quality</th><th style="border:1px solid #000;padding:6px;">Meters</th><th style="border:1px solid #000;padding:6px;">Rate</th><th style="border:1px solid #000;padding:6px;">Amount</th></tr> </thead> <tbody> <?php foreach ($summary_quality as $qualityId => $vals): $qname = isset($quality_map[$qualityId]) ? $quality_map[$qualityId]['name'] : ('Q' . $qualityId); $rate = isset($quality_map[$qualityId]) ? (float)$quality_map[$qualityId]['rate'] : 0.0; ?> <tr> <td style="border:1px solid #000;padding:6px;"><?= h($qname) ?></td> <td style="border:1px solid #000;padding:6px;"><?= h(clean_number($vals['mtr'])) ?></td> <td style="border:1px solid #000;padding:6px;"><?= h(clean_number($rate)) ?></td> <td style="border:1px solid #000;padding:6px;"><?= h(clean_number($vals['amt'])) ?></td> </tr> <?php endforeach; ?> </tbody> <tfoot> <tr> <th style="border:1px solid #000;padding:6px;">Grand</th> <th style="border:1px solid #000;padding:6px;"><?= h(clean_number($grand_total_mtr)) ?></th> <th style="border:1px solid #000;padding:6px;">-</th> <th style="border:1px solid #000;padding:6px;"><?= h(clean_number($grand_total_amt)) ?></th> </tr> </tfoot> </table> <table style="width:100%; border-collapse:collapse;"> <thead> <tr> <th style="border:1px solid #000;padding:6px;">Date</th> <?php foreach ($col_keys as $colKey => $machineKey): ?> <th style="border:1px solid #000;padding:6px;"><?= h($col_names[$colKey]) ?> (mtr)</th> <?php endforeach; ?> <th style="border:1px solid #000;padding:6px;">Total Mtr</th> </tr> </thead> <tbody> <?php foreach ($period_dates as $dateKey): ?> <tr> <td style="border:1px solid #000;padding:6px;"><?= h($dateKey) ?></td> <?php foreach ($col_keys as $colKey => $machineKey): ?> <?php $meter = isset($agg_mtr[$dateKey][$colKey]) ? (float)$agg_mtr[$dateKey][$colKey] : 0.0; ?> <td style="border:1px solid #000;padding:6px;"><?= $meter > 0 ? h(clean_number($meter)) : '-' ?></td> <?php endforeach; ?> <td style="border:1px solid #000;padding:6px;"><?= !empty($totals_date_mtr[$dateKey]) ? h(clean_number($totals_date_mtr[$dateKey])) : '-' ?></td> </tr> <?php endforeach; ?> </tbody> <tfoot> <tr> <th style="border:1px solid #000;padding:6px;">Grand Total</th> <?php foreach ($col_keys as $colKey => $machineKey): ?> <?php $meter = isset($totals_col_mtr[$colKey]) ? (float)$totals_col_mtr[$colKey] : 0.0; ?> <th style="border:1px solid #000;padding:6px;"><?= $meter > 0 ? h(clean_number($meter)) : '-' ?></th> <?php endforeach; ?> <th style="border:1px solid #000;padding:6px;"><?= $grand_total_mtr > 0 ? h(clean_number($grand_total_mtr)) : '-' ?></th> </tr> </tfoot> </table> <?php $html = ob_get_clean(); $dompdf_path = __DIR__ . '/../lib/dompdf/autoload.inc.php'; if (file_exists($dompdf_path)) { try { require_once $dompdf_path; if (class_exists('Dompdf\\Dompdf')) { $dompdf = new \Dompdf\Dompdf(['isHtml5ParserEnabled' => true]); $dompdf->loadHtml('<style>table{border-collapse:collapse;}th,td{border:1px solid #000;padding:6px;}</style>' . $html); $dompdf->setPaper('A4', 'landscape'); $dompdf->render(); header('Content-Type: application/pdf'); header('Content-Disposition: attachment; filename="my_patia_' . date('Ymd_Hi') . '.pdf"'); echo $dompdf->output(); exit; } } catch (Throwable $e) { } } echo $html; exit; } ?> <!doctype html> <html lang="en"> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width,initial-scale=1"> <title>My Patia — Karigar Portal</title> <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet"> <style> body{font-family:Inter,Arial,sans-serif;background:#F5FFF7;margin:0;padding:20px;} .card{background:#fff;border:1px solid #d9e3e8;border-radius:18px;padding:24px;box-shadow:0 10px 24px rgba(21,50,67,.06);margin-bottom:20px;} @media print { .no-print { display: none !important; } } </style> </head> <body> <div class="container my-4"> <div class="mb-3 no-print"> <a href="dashboard.php" class="btn btn-secondary btn-sm">← Back to Dashboard</a> </div> <div class="card" id="karigar-patia-card"> <div class="card-header bg-white border-0 pb-0 no-print"> <h3>My Patia</h3> <div class="text-muted"><?= h($header_label) ?> Mode: <?= h($mode === 'taka' ? 'Taka Wise' : 'Machine Wise') ?> Month: <?= h($month_input) ?> Period: <?= h($period) ?></div> </div> <div class="card-body"> <div class="summary-panel mb-4" id="karigar-summary"> <div class="table-responsive"> <table class="table table-sm table-bordered w-50"> <thead class="table-light"><tr><th>Quality</th><th>Meters</th><th>Rate</th><th>Amount</th></tr></thead> <tbody> <?php if (!$summary_quality): ?> <tr><td colspan="4" class="text-center text-muted">No summary data</td></tr> <?php else: ?> <?php foreach ($summary_quality as $qualityId => $vals): ?> <?php $qname = isset($quality_map[$qualityId]) ? $quality_map[$qualityId]['name'] : ('Q' . $qualityId); ?> <?php $rate = isset($quality_map[$qualityId]) ? (float)$quality_map[$qualityId]['rate'] : 0.0; ?> <tr> <td><?= h($qname) ?></td> <td><?= h(clean_number($vals['mtr'])) ?></td> <td><?= h(clean_number($rate)) ?></td> <td><strong><?= h(clean_number($vals['amt'])) ?></strong></td> </tr> <?php endforeach; ?> <?php endif; ?> </tbody> <tfoot class="table-light"> <tr> <th>Grand</th> <th><?= h(clean_number($grand_total_mtr)) ?></th> <th>-</th> <th><strong><?= h(clean_number($grand_total_amt)) ?></strong></th> </tr> </tfoot> </table> </div> </div> <form method="get" class="row g-2 align-items-center mb-3 no-print" id="karigar-filters"> <div class="col-auto"> <label class="col-form-label">Mode:</label> </div> <div class="col-auto"> <div class="btn-group" role="group"> <a class="btn btn-sm <?= $mode !== 'taka' ? 'btn-primary' : 'btn-outline-primary' ?>" href="?<?= h(http_build_query(array_merge(array_filter(['month' => $month_input, 'period' => $period, 'machine_id' => $machine_id], static function ($v) { return $v !== null && $v !== ''; }), ['mode' => 'machine']))) ?>">Machine Wise</a> <a class="btn btn-sm <?= $mode === 'taka' ? 'btn-primary' : 'btn-outline-primary' ?>" href="?<?= h(http_build_query(array_merge(array_filter(['month' => $month_input, 'period' => $period, 'machine_id' => $machine_id], static function ($v) { return $v !== null && $v !== ''; }), ['mode' => 'taka']))) ?>">Taka Wise</a> </div> </div> <div class="col-auto"> <label class="col-form-label">Month:</label> </div> <div class="col-auto"> <input class="form-control" type="month" name="month" value="<?= h($month_input) ?>"> </div> <div class="col-auto"> <label class="col-form-label">Period:</label> </div> <div class="col-auto"> <select class="form-select" name="period"> <option value="1to15" <?= $period === '1to15' ? 'selected' : '' ?>>1 to 15</option> <option value="16to31" <?= $period === '16to31' ? 'selected' : '' ?>>16 to end</option> </select> </div> <div class="col-auto"> <label class="col-form-label">Machine:</label> </div> <div class="col-auto"> <select class="form-select" name="machine_id"> <option value="">-- All --</option> <?php foreach ($machineOptions as $machineOption): ?> <option value="<?= h($machineOption['id']) ?>" <?= ($machine_id && $machine_id === (int)$machineOption['id']) ? 'selected' : '' ?>><?= h($machineOption['name']) ?></option> <?php endforeach; ?> </select> </div> <div class="col-auto"> <button type="submit" class="btn btn-primary">Show</button> <a class="btn btn-secondary" href="<?= h($_SERVER['PHP_SELF']) ?>">Reset</a> <button type="button" class="btn btn-outline-primary" onclick="openPrintWindow();">Print</button> <?php $qs = http_build_query(array_filter([ 'mode' => $mode, 'month' => $month_input, 'period' => $period, 'machine_id' => $machine_id, ], static function ($value) { return $value !== null && $value !== ''; })); $pdf_url = $_SERVER['PHP_SELF'] . '?' . $qs . '&export_pdf=1'; ?> <a class="btn btn-success" href="<?= h($pdf_url) ?>">Export PDF</a> </div> </form> <div class="table-responsive" id="karigar-table-wrap"> <table class="table table-striped table-bordered" id="karigar-table"> <thead class="table-dark"> <tr> <th>Date</th> <?php foreach ($col_keys as $colKey => $machineKey): ?> <th><?= h($col_names[$colKey]) ?> (mtr)</th> <?php endforeach; ?> <th>Total Mtr</th> </tr> </thead> <tbody> <?php foreach ($period_dates as $dateKey): ?> <tr> <td><?= h($dateKey) ?></td> <?php foreach ($col_keys as $colKey => $machineKey): ?> <?php $meter = isset($agg_mtr[$dateKey][$colKey]) ? (float)$agg_mtr[$dateKey][$colKey] : 0.0; ?> <td><?= $meter > 0 ? h(clean_number($meter)) : '-' ?></td> <?php endforeach; ?> <td><?= !empty($totals_date_mtr[$dateKey]) ? h(clean_number($totals_date_mtr[$dateKey])) : '-' ?></td> </tr> <?php endforeach; ?> </tbody> <tfoot class="table-light"> <tr> <th>Grand Total</th> <?php foreach ($col_keys as $colKey => $machineKey): ?> <?php $meter = isset($totals_col_mtr[$colKey]) ? (float)$totals_col_mtr[$colKey] : 0.0; ?> <th><?= $meter > 0 ? h(clean_number($meter)) : '-' ?></th> <?php endforeach; ?> <th><?= $grand_total_mtr > 0 ? h(clean_number($grand_total_mtr)) : '-' ?></th> </tr> </tfoot> </table> </div> </div> </div> </div> <script> function openPrintWindow() { var card = document.getElementById('karigar-patia-card'); if (!card) { alert('Report content not found.'); return; } var clone = card.cloneNode(true); clone.querySelectorAll('.no-print, form, .btn, .card-header, .page-controls, .topbar, header, footer').forEach(function(el) { if (el && el.parentNode) { el.parentNode.removeChild(el); } }); clone.querySelectorAll('script').forEach(function(node) { node.remove(); }); var html = '<!doctype html><html><head><meta charset="utf-8">'; html += '<title>My Patia - Print</title>'; html += '<style>'; html += '@page { size: A4 landscape; margin: 10mm; }'; html += 'body{font-family:Arial, Helvetica, sans-serif; color:#000; background:#fff; margin:8mm;}'; html += 'table{border-collapse:collapse; width:100%; font-size:12px;}'; html += 'th,td{border:1px solid #000; padding:6px; vertical-align:middle;}'; html += '.summary-panel{margin-bottom:10px;}'; html += '</style></head><body>'; html += clone.innerHTML; html += '</body></html>'; var printWindow = window.open('', '_blank', 'toolbar=0,location=0,menubar=0,width=1200,height=800'); if (!printWindow) { alert('Pop-up blocked. Allow pop-ups for this site and try again.'); return; } printWindow.document.open(); printWindow.document.write(html); printWindow.document.close(); printWindow.onload = function() { try { printWindow.focus(); printWindow.print(); setTimeout(function() { try { printWindow.close(); } catch (e) {} }, 800); } catch (e) { console.error(e); } }; } </script> </body> </html>