BREAKING CHANGE: Remove public/spooler_db/ legacy system Changes: - Migrate validation preview from http://glenlis/spooler_db/main_dev.php to CI4 /report/{accessnumber} - Add ReportController::preview() for HTML preview in validation dialog - Add ReportController::generatePdf() to queue PDF generation via node_spooler at http://glenlis:3030 - Add ReportController::checkPdfStatus() to poll spooler job status - Add ReportController::postToSpooler() helper for curl requests to spooler API - Add routes: GET /report/(:num)/preview, GET /report/(:num)/pdf, GET /report/status/(:any) - Delete public/spooler_db/ directory (40+ legacy files) - Compact node_spooler/README.md from 577 to 342 lines Technical Details: - New architecture: CI4 Controller -> node_spooler (port 3030) -> Chrome CDP (port 42020) - API endpoints: POST /api/pdf/generate, GET /api/pdf/status/:jobId, GET /api/queue/stats - Features: Max 5 concurrent jobs, max 100 in queue, auto-cleanup after 60 min - Error handling: Chrome crash detection, manual error review in data/error/ - PDF infrastructure ready, frontend PDF buttons to be updated later in production Migration verified: - No external code references spooler_db - All assets duplicated in public/assets/report/ - Syntax checks passed for ReportController.php and Routes.php Refs: node_spooler/README.md
151 lines
4.9 KiB
PHP
151 lines
4.9 KiB
PHP
<?php
|
|
namespace App\Controllers;
|
|
|
|
use App\Controllers\BaseController;
|
|
|
|
class ReportController extends BaseController
|
|
{
|
|
protected $db;
|
|
protected $reportHelper;
|
|
|
|
public function __construct()
|
|
{
|
|
$this->db = \Config\Database::connect();
|
|
$this->reportHelper = new \App\Libraries\ReportHelper($this->db);
|
|
helper(['url', 'text']);
|
|
}
|
|
|
|
public function generate($accessnumber, $eng = 0, $ispdf = 0)
|
|
{
|
|
if ($ispdf == 0) {
|
|
$ispdf = $this->request->getVar('ispdf') ?? 0;
|
|
}
|
|
|
|
return $this->renderReport($accessnumber, $eng, $ispdf, false);
|
|
}
|
|
|
|
public function print($accessnumber, $eng = 0, $ispdf = 0)
|
|
{
|
|
if ($ispdf == 0) {
|
|
$ispdf = $this->request->getVar('ispdf') ?? 0;
|
|
}
|
|
|
|
return $this->renderReport($accessnumber, $eng, $ispdf, true);
|
|
}
|
|
|
|
private function renderReport($accessnumber, $eng, $ispdf, $shouldLog)
|
|
{
|
|
$userroleid = session()->get('userroleid');
|
|
if (!in_array($userroleid, [0, 1, 2, 4])) {
|
|
return $this->response->setStatusCode(403)->setJSON(['message' => 'Unauthorized']);
|
|
}
|
|
|
|
$data = $this->reportHelper->getReportData($accessnumber, $eng);
|
|
$data['eng'] = $eng;
|
|
$data['accessnumber'] = $accessnumber;
|
|
$data['ispdf'] = $ispdf;
|
|
|
|
if ($shouldLog == true) {
|
|
$this->logPrintAudit($accessnumber, $data['status']);
|
|
}
|
|
|
|
return view('report/template', $data);
|
|
}
|
|
|
|
private function logPrintAudit($accessnumber, $status)
|
|
{
|
|
$sql = "INSERT INTO GDC_CMOD.dbo.AUDIT_REQUESTS(ACCESSNUMBER, STEPDATE, STEPTYPE, STEPSTATUS)
|
|
VALUES(?, GETDATE(), 'PRINT', ?)";
|
|
$this->db->query($sql, [$accessnumber, $status]);
|
|
}
|
|
|
|
public function preview($accessnumber)
|
|
{
|
|
$eng = $this->request->getVar('eng') ?? 0;
|
|
return $this->renderReport($accessnumber, $eng, 0, false);
|
|
}
|
|
|
|
public function generatePdf($accessnumber)
|
|
{
|
|
$userroleid = session()->get('userroleid');
|
|
if (!in_array($userroleid, [0, 1, 2, 4])) {
|
|
return $this->response->setStatusCode(403)->setJSON(['success' => false, 'error' => 'Unauthorized']);
|
|
}
|
|
|
|
$eng = $this->request->getVar('eng') ?? 0;
|
|
$data = $this->reportHelper->getReportData($accessnumber, $eng);
|
|
$data['eng'] = $eng;
|
|
$data['accessnumber'] = $accessnumber;
|
|
$data['ispdf'] = 1;
|
|
|
|
$html = view('report/template', $data);
|
|
$filename = $accessnumber . '.pdf';
|
|
|
|
try {
|
|
$jobId = $this->postToSpooler($html, $filename);
|
|
return $this->response->setJSON([
|
|
'success' => true,
|
|
'jobId' => $jobId,
|
|
'message' => 'PDF queued for generation',
|
|
'status' => 'queued'
|
|
]);
|
|
} catch (\Exception $e) {
|
|
log_message('error', "PDF generation failed: " . $e->getMessage());
|
|
return $this->response->setStatusCode(500)->setJSON([
|
|
'success' => false,
|
|
'error' => 'Failed to queue PDF generation'
|
|
]);
|
|
}
|
|
}
|
|
|
|
public function checkPdfStatus($jobId)
|
|
{
|
|
$ch = curl_init();
|
|
curl_setopt($ch, CURLOPT_URL, "http://glenlis:3030/api/pdf/status/$jobId");
|
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
|
curl_setopt($ch, CURLOPT_TIMEOUT, 5);
|
|
|
|
$response = curl_exec($ch);
|
|
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
|
|
curl_close($ch);
|
|
|
|
if ($httpCode !== 200) {
|
|
log_message('error', "Spooler status check returned HTTP $httpCode");
|
|
return $this->response->setStatusCode(500)->setJSON([
|
|
'success' => false,
|
|
'error' => 'Failed to check job status'
|
|
]);
|
|
}
|
|
|
|
return $this->response->setJSON($response);
|
|
}
|
|
|
|
private function postToSpooler($html, $filename)
|
|
{
|
|
$ch = curl_init();
|
|
curl_setopt($ch, CURLOPT_URL, 'http://glenlis:3030/api/pdf/generate');
|
|
curl_setopt($ch, CURLOPT_POST, 1);
|
|
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode([
|
|
'html' => $html,
|
|
'filename' => $filename
|
|
]));
|
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
|
curl_setopt($ch, CURLOPT_HTTPHEADER, [
|
|
'Content-Type: application/json'
|
|
]);
|
|
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
|
|
|
|
$response = curl_exec($ch);
|
|
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
|
|
curl_close($ch);
|
|
|
|
if ($httpCode !== 200) {
|
|
log_message('error', "Spooler API returned HTTP $httpCode");
|
|
throw new \Exception('Failed to queue PDF generation');
|
|
}
|
|
|
|
$data = json_decode($response, true);
|
|
return $data['jobId'];
|
|
}
|
|
}
|