« Back to History
salary_paid_summary.php
|
20260920_164915.php
Initial Domain Snapshot
Copy Code
<?php /* ============================================================================= File: /salary_paid_summary.php Title: Salary By Department ============================================================================= */ require_once __DIR__ . '/modules/auth/page_acl.php'; $ctx = page_require_access('salary_summary'); if (!class_exists(\Dompdf\Dompdf::class)) { foreach ([ __DIR__ . '/lib/dompdf/autoload.inc.php', __DIR__ . '/vendor/autoload.php', ] as $dompdfAutoload) { if (is_file($dompdfAutoload)) { require_once $dompdfAutoload; if (class_exists(\Dompdf\Dompdf::class)) { break; } } } } $pdo = $ctx['pdo']; $company_id = (int)$ctx['company_id']; $user_id = (int)($ctx['user']['id'] ?? 0); function h($s){ return htmlspecialchars((string)$s, ENT_QUOTES, 'UTF-8'); } function n0($v){ return number_format((float)$v, 0); } $month = $_GET['month'] ?? date('Y-m'); $period = $_GET['period'] ?? 'H1'; $month_key = $month.'-01'; $y = (int)date('Y', strtotime($month_key)); $m = (int)date('m', strtotime($month_key)); if ($period==='H1'){ $from="$month-01"; $to="$month-15"; } else { $from="$month-16"; $to=date('Y-m-t', strtotime($month_key)); } /* Ensure master_salary_snapshots table exists with report_type */ $snapshot_ddl = <<<SQL CREATE TABLE IF NOT EXISTS master_salary_snapshots ( id INT AUTO_INCREMENT PRIMARY KEY, company_id BIGINT UNSIGNED NOT NULL, salary_year INT NOT NULL, salary_month INT NOT NULL, period VARCHAR(20) NOT NULL, report_type VARCHAR(50) NOT NULL, report_html LONGTEXT NOT NULL, created_by BIGINT UNSIGNED NOT NULL, created_at DATETIME DEFAULT CURRENT_TIMESTAMP, UNIQUE KEY unique_report (company_id, salary_year, salary_month, period, report_type) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4; SQL; $pdo->exec($snapshot_ddl); /* Handle Save Snapshot Action with report_type = 'Salary By Department' */ if (($_POST['action'] ?? '') === 'save_snapshot') { $report_html = $_POST['report_html'] ?? ''; $snapshot_period = $period; // 'H1' or 'H2' $report_type = 'Salary By Department'; if (!empty($report_html)) { try { $stmt = $pdo->prepare(" INSERT INTO master_salary_snapshots (company_id, salary_year, salary_month, period, report_type, report_html, created_by) VALUES (?, ?, ?, ?, ?, ?, ?) ON DUPLICATE KEY UPDATE report_html = VALUES(report_html), created_by = VALUES(created_by), created_at = NOW() "); $stmt->execute([$company_id, $y, $m, $snapshot_period, $report_type, $report_html, $user_id]); header("Location: salary_paid_summary.php?month={$month}&period={$period}&snapshot_saved=1"); exit; } catch (Throwable $e) { http_response_code(500); exit("Error saving snapshot: " . htmlspecialchars($e->getMessage())); } } } require_once __DIR__.'/modules/salary/summary_paid_helpers.php'; $rows = []; // 1. Core Payroll Reports Pull (Semi-Monthly) $st = $pdo->prepare(" SELECT department_name, SUM(gross_amount) as total_gross, SUM(net_pay) as total_payable FROM semi_monthly_salary_report WHERE company_id=? AND month_key=? AND period_label=? GROUP BY department_id "); $st->execute([$company_id, $month_key, $period]); foreach($st as $r){ $dept = $r['department_name']; $g = (float)$r['total_gross']; $p = (float)$r['total_payable']; $rows[$dept] = [ 'gross' => $g, 'paid' => $g - $p ]; } // 2. Core Payroll Reports Pull (Monthly for H2 - Compound addition) if($period==='H2'){ $st = $pdo->prepare(" SELECT department_name, SUM(gross_amount) as total_gross, SUM(net_pay) as total_payable FROM monthly_salary_report WHERE company_id=? AND month_key=? GROUP BY department_id "); $st->execute([$company_id, $month_key]); foreach($st as $r){ $dept = $r['department_name']; $g = (float)$r['total_gross']; $p = (float)$r['total_payable']; if (!isset($rows[$dept])) { $rows[$dept] = ['gross' => 0.0, 'paid' => 0.0]; } $rows[$dept]['gross'] += $g; $rows[$dept]['paid'] += ($g - $p); } } /* Modules Manual Sync Overrides via Helper Outputs */ $loom_data = get_summary_loom_data($pdo, $company_id, $from, $to); $rows['Loom'] = [ 'gross' => $loom_data['gross'], 'paid' => $loom_data['paid'] ]; $warper_data = get_summary_warper_data($pdo, $company_id, $from, $to); $rows['Warping'] = [ 'gross' => $warper_data['gross'], 'paid' => $warper_data['paid'] ]; $pissing_data = get_summary_pissing_data($pdo, $company_id, $from, $to); $rows['Pissing'] = [ 'gross' => $pissing_data['gross'], 'paid' => $pissing_data['paid'] ]; $pasaria_data = get_summary_pasaria_data($pdo, $company_id, $y, $m, $period); $rows['Pasaria'] = [ 'gross' => $pasaria_data['gross'], 'paid' => $pasaria_data['paid'] ]; // Clean empty zero entry lines foreach($rows as $k => $v) { if($v['gross'] == 0 && $v['paid'] == 0) { unset($rows[$k]); } } $grand_gross = 0; $grand_paid = 0; foreach($rows as $r) { $grand_gross += $r['gross']; $grand_paid += $r['paid']; } $grand_balance = $grand_gross - $grand_paid; if (($_GET['export'] ?? '') === 'pdf') { if (!class_exists(\Dompdf\Dompdf::class)) { http_response_code(503); exit('PDF export is not available right now.'); } $period_label = ($period === 'H1') ? '1-15' : '16-End'; ob_start(); ?> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <style> @page { margin: 16px; } body { margin: 0; font-family: DejaVu Sans, sans-serif; font-size: 10px; color: #111; } h1 { margin: 0 0 4px; text-align: center; font-size: 15px; text-transform: uppercase; } h2 { margin: 0 0 12px; text-align: center; font-size: 11px; font-weight: normal; color: #444; } table { width: 100%; border-collapse: collapse; table-layout: fixed; } th, td { border: 1px solid #b0b0b0; padding: 7px; text-align: left; } th { background: #f4f5f7; font-size: 9.5px; } .text-end { text-align: right; } tfoot th { background: #eef3ff; font-weight: 700; } </style> </head> <body> <h1>Salary By Department</h1> <h2><?= h(date('F Y', strtotime($month_key))) ?> | Period Half: <?= h($period_label) ?></h2> <table> <thead> <tr> <th>Department Name</th> <th class="text-end">Gross Salary (A)</th> <th class="text-end">Total Paid Amount (B)</th> <th class="text-end">Net Pay / Balance (A - B)</th> </tr> </thead> <tbody> <?php foreach($rows as $k => $v): $bal = $v['gross'] - $v['paid']; ?> <tr> <td><?= h($k) ?></td> <td class="text-end">₹<?= n0($v['gross']) ?></td> <td class="text-end">₹<?= n0($v['paid']) ?></td> <td class="text-end fw-bold">₹<?= n0($bal) ?></td> </tr> <?php endforeach; ?> </tbody> <tfoot> <tr> <th>Grand Total</th> <th class="text-end">₹<?= n0($grand_gross) ?></th> <th class="text-end">₹<?= n0($grand_paid) ?></th> <th class="text-end">₹<?= n0($grand_balance) ?></th> </tr> </tfoot> </table> </body> </html> <?php $pdfHtml = ob_get_clean(); $dompdf = new \Dompdf\Dompdf(['isRemoteEnabled' => false, 'defaultFont' => 'DejaVu Sans']); $dompdf->loadHtml($pdfHtml, 'UTF-8'); $dompdf->setPaper('A4', 'portrait'); $dompdf->render(); $dompdf->stream('salary_by_department_' . $month . '_' . $period . '.pdf', ['Attachment' => true]); exit; } $page_title = 'Salary By Department'; require_once __DIR__.'/partials/header.php'; ?> <div class="container py-4"> <?php if (isset($_GET['snapshot_saved'])): ?> <div class="alert alert-success alert-dismissible fade show border-0 shadow-sm no-print" role="alert"> <i class="bi bi-cloud-check-fill me-2"></i> Successfully saved to Master Salary Report! <button type="button" class="btn-close" data-bs-dismiss="alert"></button> </div> <?php endif; ?> <div class="card mb-4 no-print shadow-sm border-0 bg-light"> <div class="card-body"> <form method="get" class="row g-3 align-items-end"> <div class="col-md-3"> <label class="form-label fw-bold small text-muted text-uppercase">Select Month</label> <input type="month" name="month" class="form-control" value="<?= h($month) ?>"> </div> <div class="col-md-3"> <label class="form-label fw-bold small text-muted text-uppercase">Select Period</label> <select name="period" class="form-select"> <option value="H1" <?= $period==='H1'?'selected':'' ?>>1–15 (First Half)</option> <option value="H2" <?= $period==='H2'?'selected':'' ?>>16–End (Second Half)</option> </select> </div> <div class="col-md-6 d-flex gap-2"> <button class="btn btn-primary fw-bold flex-grow-1">Apply Filters</button> <button type="button" onclick="window.print()" class="btn btn-dark fw-bold">Print</button> <button class="btn btn-danger fw-bold" name="export" value="pdf">PDF</button> <button type="button" onclick="saveReportSnapshot()" class="btn btn-dark fw-bold">Save to Master Salary</button> </div> </form> </div> </div> <form id="snapshotForm" method="post" action="" style="display:none;"> <input type="hidden" name="action" value="save_snapshot"> <textarea name="report_html" id="reportHtmlField"></textarea> </form> <div class="text-center mb-4"> <h4 class="fw-bold text-dark mb-1">Salary By Department</h4> <p class="text-muted small">Statement for <?= h(date('F Y', strtotime($month_key))) ?> (<?= $period === 'H1' ? '1-15 Half' : '16-End Half' ?>)</p> </div> <div class="card shadow-sm border-0 overflow-hidden mb-4"> <div id="reportContent"> <table class="table table-striped table-bordered align-middle mb-0"> <thead class="table-dark text-uppercase" style="font-size: 11px; letter-spacing: 0.5px;"> <tr> <th>Department</th> <th class="text-end">Gross Salary (A)</th> <th class="text-end">Total Paid Amount (B)</th> <th class="text-end">Net Pay / Balance (A - B)</th> </tr> </thead> <tbody> <?php foreach($rows as $k => $v): $balance = $v['gross'] - $v['paid']; ?> <tr> <td class="fw-bold text-secondary"><?= h($k) ?></td> <td class="text-end text-dark fw-bold">₹<?= n0($v['gross']) ?></td> <td class="text-end text-success fw-bold">₹<?= n0($v['paid']) ?></td> <td class="text-end text-danger fs-6 fw-bold">₹<?= n0($balance) ?></td> </tr> <?php endforeach; ?> </tbody> <tfoot class="table-light fw-bold" style="border-top: 2px solid #222;"> <tr> <td class="text-uppercase text-dark">Grand Total Summary</td> <td class="text-end text-dark fs-5">₹<?= n0($grand_gross) ?></td> <td class="text-end text-success fs-5">₹<?= n0($grand_paid) ?></td> <td class="text-end text-danger fs-5 fw-bolder">₹<?= n0($grand_balance) ?></td> </tr> </tfoot> </table> </div> </div> </div> <script> function saveReportSnapshot() { const reportContent = document.getElementById('reportContent').innerHTML; document.getElementById('reportHtmlField').value = reportContent; document.getElementById('snapshotForm').submit(); } </script> <?php require_once __DIR__.'/partials/footer.php'; ?>