diff --git a/app/Config/Routes.php b/app/Config/Routes.php index 963faa7..bc3b95a 100644 --- a/app/Config/Routes.php +++ b/app/Config/Routes.php @@ -36,7 +36,7 @@ $routes->group('api', function ($routes) { // Requests - All Roles (0,1,2,3,4) $routes->group('requests', ['filter' => 'role:0,1,2,3,4'], function ($routes) { $routes->get('', 'RequestsController::index'); -$routes->get('(:any)/audit', 'ApiRequestsAuditController::show/$1'); + $routes->get('(:any)/audit', 'ApiRequestsAuditController::show/$1'); $routes->post('validate/(:any)', 'RequestsController::val/$1'); $routes->delete('validate/(:any)', 'RequestsController::unval/$1'); $routes->post('(:any)/eng', 'RequestsController::setEngLanguage/$1'); @@ -108,6 +108,9 @@ $routes->group('report', ['filter' => 'role:0,1,2,4'], function ($routes) { $routes->get('report/status/(:any)', 'ReportController::checkPdfStatus/$1'); +// Batch PDF endpoint - no auth required (internal use only) +$routes->get('batch/pdf/(:num)', 'ReportController::generatePdfNoAuth/$1'); + // External PDF generator endpoint - no auth required $routes->post('api/requests/(:any)/pdf', 'RequestsController::setPdfFlag/$1'); diff --git a/app/Controllers/ReportController.php b/app/Controllers/ReportController.php index 629be87..7b95422 100644 --- a/app/Controllers/ReportController.php +++ b/app/Controllers/ReportController.php @@ -240,4 +240,82 @@ try { $data = json_decode($response, true); return $data['jobId']; } + + /** + * Generate PDF without authentication check + * Used by batch processing script (internal use only) + */ + public function generatePdfNoAuth($accessnumber) + { + // Get language: URL parameter > REPORT_LANG > default (0) + $engParam = $this->request->getVar('eng') ?? null; + if ($engParam !== null) { + $eng = (int) $engParam; + } else { + // Read REPORT_LANG from CM_REQUESTS + $sql = "SELECT REPORT_LANG FROM GDC_CMOD.dbo.CM_REQUESTS WHERE ACCESSNUMBER=?"; + $row = $this->db->query($sql, [$accessnumber])->getRowArray(); + $eng = (int) ($row['REPORT_LANG'] ?? 0); + } + + $data = $this->reportHelper->getReportData($accessnumber, $eng); + $data['eng'] = $eng; + $data['accessnumber'] = $accessnumber; + $data['ispdf'] = 1; + + $html = view('report/template', $data); + $filename = $accessnumber . ($eng == 1 ? '_eng' : '') . '.pdf'; + $collectionDate = $data['collectionDate'] ?? ''; + $hostnumber = $data['hostnumber'] ?? ''; + + try { + $jobId = $this->postToSpooler($html, $filename, $collectionDate, $accessnumber, $hostnumber); + + $sqlCheck = "SELECT COUNT(*) as cnt FROM GDC_CMOD.dbo.AUDIT_REQUESTS + WHERE ACCESSNUMBER = ? AND STEPTYPE IN ('GEN_PDF', 'REGEN_PDF')"; + $result = $this->db->query($sqlCheck, [$accessnumber])->getRowArray(); + + $stepType = ($result['cnt'] > 0) ? 'REGEN_PDF' : 'GEN_PDF'; + $stepStatus = $eng == 1 ? 'English' : 'Indonesian'; + + $sqlLog = "INSERT INTO GDC_CMOD.dbo.AUDIT_REQUESTS(ACCESSNUMBER, STEPDATE, STEPTYPE, STEPSTATUS) + VALUES (?, GETDATE(), ?, ?)"; + $this->db->query($sqlLog, [$accessnumber, $stepType, $stepStatus]); + + try { + $oruDir = 'c:\inetpub\wwwroot\spooler_db\process_oru'; + if (!is_dir($oruDir)) { + mkdir($oruDir, 0777, true); + } + + $oruFile = "$oruDir/$accessnumber.oru"; + $date = date('Y-m-d H:i'); + $status = $data['status'] ?? 'PENDING'; + $file = fopen($oruFile, 'w+'); + fwrite($file, "$accessnumber\r\n$hostnumber\r\n$date\r\n$status\r\n-"); + fclose($file); + + $sqlOruLog = "INSERT INTO GDC_CMOD.dbo.AUDIT_REQUESTS(ACCESSNUMBER, STEPDATE, STEPTYPE, STEPSTATUS) + VALUES (?, GETDATE(), 'ORU_FILE', 'Created')"; + $this->db->query($sqlOruLog, [$accessnumber]); + } catch (\Throwable $e) { + log_message('error', "ORU file creation failed for $accessnumber: " . $e->getMessage()); + } + + return $this->response->setJSON([ + 'success' => true, + 'jobId' => $jobId, + 'message' => 'PDF queued for generation', + 'status' => 'queued', + 'lang' => $eng == 1 ? 'English' : 'Indonesian', + 'isRegen' => ($stepType === 'REGEN_PDF') + ]); + } 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' + ]); + } + } } diff --git a/script.php b/script.php index 0057da1..a43a353 100644 --- a/script.php +++ b/script.php @@ -28,7 +28,9 @@ if (!file_exists($inputFile)) { } // Base URL for CI4 endpoint -$baseUrl = 'http://glenlis/cmod/report'; +// Using batch/pdf endpoint (no auth required) +// Dev: localhost/gdc_cmod/ | Prod: glenlis/cmod/ +$baseUrl = 'http://localhost/gdc_cmod/batch/pdf'; // Read accessnumbers from file $lines = file($inputFile, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES); @@ -71,9 +73,12 @@ foreach ($accessnumbers as $index => $accessnumber) { curl_setopt($ch, CURLOPT_TIMEOUT, 60); curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); + curl_setopt($ch, CURLOPT_HEADER, true); // Include headers in output $response = curl_exec($ch); $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE); + $redirectUrl = curl_getinfo($ch, CURLINFO_REDIRECT_URL); + $finalUrl = curl_getinfo($ch, CURLINFO_EFFECTIVE_URL); $curlError = curl_error($ch); curl_close($ch); @@ -81,8 +86,22 @@ foreach ($accessnumbers as $index => $accessnumber) { throw new \Exception("cURL error: $curlError"); } + // Debug: Show redirect info if ($httpCode !== 200) { - throw new \Exception("HTTP $httpCode"); + $debug = "HTTP $httpCode"; + if ($redirectUrl) { + $debug .= " -> $redirectUrl"; + } + if ($finalUrl !== $url) { + $debug .= " (final: $finalUrl)"; + } + throw new \Exception($debug); + } + + // Remove headers from response body + $headerSize = strpos($response, "\r\n\r\n"); + if ($headerSize !== false) { + $response = substr($response, $headerSize + 4); } $data = json_decode($response, true);