« Back to History
cms_detail_print.php
|
20260920_164915.php
Initial Domain Snapshot
Copy Code
<?php /* ============================================================================ File: /erp/cms_detail_print.php Purpose: CMS Detail Print & Master Salary Snapshot Integration (Strict H1/H2 Period) ============================================================================ */ require_once __DIR__ . '/modules/auth/page_acl.php'; $ctx = page_require_access('cms_detail_print'); 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); if (!function_exists('h')) { function h($v) { return htmlspecialchars((string)$v, ENT_QUOTES, 'UTF-8'); } } $id = (int)($_GET['id'] ?? 0); if ($id <= 0) { exit('Invalid CMS ID'); } $stmt = $pdo->prepare(" SELECT * FROM cms_detail WHERE id = :id AND company_id = :cid LIMIT 1 "); $stmt->execute([ ':id' => $id, ':cid' => $company_id ]); $row = $stmt->fetch(PDO::FETCH_ASSOC); if (!$row) { exit('CMS Detail not found'); } /* ---------- Labels ---------- */ $monthLabel = date('M', mktime(0, 0, 0, (int)$row['month'], 1)); /* ---------- Amounts ---------- */ $items = [ 'Warper' => (int)$row['warper_amount'], 'Pasaria' => (int)$row['pasaria_amount'], 'Pissing' => (int)$row['pissing_amount'], 'Loom Karigar Salary' => (int)$row['loom_salary_amount'], 'Monthly Salary' => (int)$row['monthly_salary_amount'], 'Semi Monthly Salary' => (int)$row['semi_monthly_salary_amount'], ]; $total = array_sum($items); /* 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 = 'CMS Detail' and Strict H1/H2 Period Mapping */ if (($_POST['action'] ?? '') === 'save_snapshot') { $report_html = $_POST['report_html'] ?? ''; $salary_year = (int)$row['year']; $salary_month = (int)$row['month']; // Strict normalization: ensure period is saved as H1 or H2 $raw_period = strtoupper(trim($row['period'])); $snapshot_period = 'H1'; if (strpos($raw_period, 'H2') !== false || strpos($raw_period, '16') !== false || strpos($raw_period, 'END') !== false) { $snapshot_period = 'H2'; } $report_type = 'CMS Detail'; 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, $salary_year, $salary_month, $snapshot_period, $report_type, $report_html, $user_id]); header("Location: cms_detail_print.php?id={$id}&snapshot_saved=1"); exit; } catch (Throwable $e) { http_response_code(500); exit("Error saving snapshot: " . htmlspecialchars($e->getMessage())); } } } if (!function_exists('render_cms_detail_report_html')) { function render_cms_detail_report_html(array $row, array $items, int $total, string $monthLabel, int $id, bool $isPdf = false): string { $pdfUrl = '?'.http_build_query([ 'id' => $id, 'export' => 'pdf', ]); ob_start(); ?> <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>CMS Detail<?= $isPdf ? ' PDF' : ' Preview' ?></title> <style> body { font-family: Arial, sans-serif; margin: <?= $isPdf ? '18px' : '24px' ?>; color: #000; } .actions { margin-bottom: 18px; text-align: right; } .actions button, .actions a { display: inline-block; border: 1px solid #111; background: #111; color: #fff; padding: 8px 14px; text-decoration: none; font-size: 13px; line-height: 1.2; cursor: pointer; margin-left: 8px; } .actions button.btn-save { background: #198754; border-color: #198754; } h1 { text-align: center; margin-bottom: 6px; font-size: <?= $isPdf ? '22px' : '28px' ?>; } .sub { text-align: center; margin-bottom: 20px; font-size: <?= $isPdf ? '12px' : '14px' ?>; } table { width: 100%; border-collapse: collapse; font-size: <?= $isPdf ? '12px' : '14px' ?>; } th, td { border: 1px solid #000; padding: <?= $isPdf ? '7px 8px' : '8px 10px' ?>; } th { text-align: right; width: 30%; } td { text-align: left; } .total-row th, .total-row td { font-weight: bold; border-top: 2px solid #000; } .remarks { margin-top: 16px; font-size: <?= $isPdf ? '12px' : '14px' ?>; } <?php if (!$isPdf): ?> @media print { body { margin: 0; } .actions { display: none; } } <?php else: ?> @page { margin: 16px; } <?php endif; ?> </style> </head> <body> <?php if (!$isPdf): ?> <?php if (isset($_GET['snapshot_saved'])): ?> <div style="background:#d4edda; color:#155724; padding:10px; border-radius:5px; margin-bottom:15px; text-align:center; font-weight:bold;"> Successfully saved to Master Salary Report! </div> <?php endif; ?> <div class="actions"> <button type="button" onclick="window.print()">Print</button> <a href="<?= h($pdfUrl) ?>">Export PDF</a> <button type="button" class="btn-save" onclick="document.getElementById('snapshotForm').submit()">Save to Master Salary</button> </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> <script> window.addEventListener('DOMContentLoaded', function() { const reportContent = document.querySelector('table').outerHTML + (document.querySelector('.remarks') ? document.querySelector('.remarks').outerHTML : ''); document.getElementById('reportHtmlField').value = reportContent; }); </script> <?php endif; ?> <div id="reportContent"> <h1>Maa Sharda Textiles : CMS Detail</h1> <div class="sub"> <?= h($row['year']) ?> | <?= h($monthLabel) ?> <?= h($row['period']) ?> </div> <table> <?php foreach ($items as $name => $amount): ?> <tr> <th><?= h(number_format($amount)) ?></th> <td><?= h($name) ?></td> </tr> <?php endforeach; ?> <tr class="total-row"> <th><?= h(number_format($total)) ?></th> <td>TOTAL</td> </tr> </table> <?php if (!empty($row['remarks'])): ?> <div class="remarks"> <strong>Remarks:</strong> <?= h($row['remarks']) ?> </div> <?php endif; ?> </div> </body> </html> <?php return (string)ob_get_clean(); } } if (($_GET['export'] ?? '') === 'pdf') { if (!class_exists(\Dompdf\Dompdf::class)) { http_response_code(503); exit('PDF export is not available right now.'); } $dompdf = new \Dompdf\Dompdf([ 'isRemoteEnabled' => false, 'defaultFont' => 'DejaVu Sans', ]); $dompdf->loadHtml(render_cms_detail_report_html($row, $items, $total, $monthLabel, $id, true), 'UTF-8'); $dompdf->setPaper('A4', 'portrait'); $dompdf->render(); $dompdf->stream('cms_detail_' . $id . '.pdf', ['Attachment' => true]); exit; } echo render_cms_detail_report_html($row, $items, $total, $monthLabel, $id);