GWM : Merge multiple file for claims
This commit is contained in:
parent
a7a1da2ed3
commit
98b2583ccd
@ -3923,6 +3923,16 @@ class EmployeeRestController extends AdminController
|
||||
}
|
||||
}
|
||||
|
||||
// Merge all uploaded PDFs for this ticket into a single combined PDF
|
||||
if ($pdf_exist_in_the_file) {
|
||||
try {
|
||||
helper('merge_pdf');
|
||||
merge_ticket_pdfs((int) $ticket_id);
|
||||
} catch (\Throwable $e) {
|
||||
log_message('error', 'EmployeeRestController::handleCliamFiles | merge_ticket_pdfs failed | ticket_id=' . $ticket_id . ' | ' . $e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
$count = $this->ticketMaster->where('id', $ticket_id)->where('is_active', 1)->where('tpa_claim_push_reference_no IS NULL')->countAllResults();
|
||||
log_message('error', 'Ticket validation | Ticket ID: ' . $ticket_id . ' | Matching active tickets with NULL TPA reference: ' . $count);
|
||||
if ($count > 0 && $pdf_exist_in_the_file && $tpa_claim_push == true) {
|
||||
|
||||
@ -1621,6 +1621,16 @@ class TicketController extends BaseController
|
||||
$this->ticketMasterModel->where('id', $return_value)->set($updateUploadedLetterData)->update();
|
||||
}
|
||||
|
||||
// Merge all uploaded PDFs for this ticket into a single combined PDF
|
||||
try {
|
||||
helper('merge_pdf');
|
||||
merge_ticket_pdfs((int) $return_value, [
|
||||
'ticket_type' => (int) ($ticket_data['ticket_type_id'] ?? 1),
|
||||
]);
|
||||
} catch (\Throwable $e) {
|
||||
log_message('error', 'TicketController::createTicket | merge_ticket_pdfs failed | ticket_id=' . $return_value . ' | ' . $e->getMessage());
|
||||
}
|
||||
|
||||
//mail trigger part
|
||||
$this->putHistoryAfterInsert($ticket_data, $return_value);
|
||||
$mail_responce = $this->sendAutoMailTrigger($return_value);
|
||||
|
||||
194
app/Helpers/merge_pdf_helper.php
Normal file
194
app/Helpers/merge_pdf_helper.php
Normal file
@ -0,0 +1,194 @@
|
||||
<?php
|
||||
|
||||
use App\Models\ClaimFilesModel;
|
||||
use Mpdf\Mpdf;
|
||||
use Mpdf\Output\Destination;
|
||||
|
||||
if (! defined('MERGED_CLAIM_FILE_TYPE')) {
|
||||
// claim_files.file_type values currently in use:
|
||||
// 1 = legacy letter, 2 = uploaded claim doc, 3 = approved/settle letter
|
||||
// 4 = merged combined PDF (registered by this helper)
|
||||
define('MERGED_CLAIM_FILE_TYPE', 4);
|
||||
}
|
||||
|
||||
if (! function_exists('merge_ticket_pdfs')) {
|
||||
/**
|
||||
* Merge all active PDF rows in claim_files for a given ticket_master id
|
||||
* into one combined PDF and register that PDF as a new claim_files row
|
||||
* with file_type = MERGED_CLAIM_FILE_TYPE.
|
||||
*
|
||||
* Source rows are picked from claim_files where:
|
||||
* - ticket_id = $ticket_master_id
|
||||
* - is_active = 1
|
||||
* - mime_type = 'application/pdf'
|
||||
* - file_type IN $opts['include_file_types'] (default [2, 3])
|
||||
*
|
||||
* @param int $ticket_master_id
|
||||
* @param array $opts {
|
||||
* @var bool $replace Default true. Soft-delete previous merged row before re-creating.
|
||||
* @var int $created_by Override created_by user id on the inserted row.
|
||||
* @var int $ticket_type Default 1. Stored on the inserted claim_files row.
|
||||
* @var array $include_file_types Default [2, 3].
|
||||
* }
|
||||
* @return array {status, merged_file_id, file_name, pages, source_count, message}
|
||||
*/
|
||||
function merge_ticket_pdfs(int $ticket_master_id, array $opts = []): array
|
||||
{
|
||||
$opts += [
|
||||
'replace' => true,
|
||||
'created_by' => null,
|
||||
'ticket_type' => 1,
|
||||
'include_file_types' => [2, 3],
|
||||
];
|
||||
|
||||
$result = [
|
||||
'status' => false,
|
||||
'merged_file_id' => null,
|
||||
'file_name' => null,
|
||||
'pages' => 0,
|
||||
'source_count' => 0,
|
||||
'message' => '',
|
||||
];
|
||||
|
||||
if ($ticket_master_id <= 0) {
|
||||
$result['message'] = 'Invalid ticket_master_id';
|
||||
return $result;
|
||||
}
|
||||
|
||||
$claimFiles = new ClaimFilesModel();
|
||||
|
||||
$rows = $claimFiles
|
||||
->where('ticket_id', $ticket_master_id)
|
||||
->where('is_active', 1)
|
||||
->where('mime_type', 'application/pdf')
|
||||
->whereIn('file_type', $opts['include_file_types'])
|
||||
->orderBy('id', 'ASC')
|
||||
->findAll();
|
||||
|
||||
if (empty($rows)) {
|
||||
$result['status'] = true;
|
||||
$result['message'] = 'No PDF files to merge';
|
||||
return $result;
|
||||
}
|
||||
|
||||
$uploadDir = rtrim(WRITEPATH, '/\\') . DIRECTORY_SEPARATOR
|
||||
. 'uploads' . DIRECTORY_SEPARATOR
|
||||
. 'claim_files' . DIRECTORY_SEPARATOR;
|
||||
|
||||
$sourcePaths = [];
|
||||
foreach ($rows as $row) {
|
||||
$name = ! empty($row['url']) ? $row['url'] : ($row['file_name'] ?? '');
|
||||
if (empty($name)) {
|
||||
continue;
|
||||
}
|
||||
// url column may sometimes hold a full URL; we only care about the file basename on disk.
|
||||
$full = $uploadDir . basename($name);
|
||||
if (is_file($full) && is_readable($full)) {
|
||||
$sourcePaths[] = $full;
|
||||
} else {
|
||||
log_message('error', "merge_ticket_pdfs | missing PDF on disk | claim_file_id={$row['id']} | path={$full}");
|
||||
}
|
||||
}
|
||||
|
||||
if (empty($sourcePaths)) {
|
||||
$result['message'] = 'No readable PDF files on disk';
|
||||
return $result;
|
||||
}
|
||||
|
||||
$result['source_count'] = count($sourcePaths);
|
||||
|
||||
$tempDir = rtrim(WRITEPATH, '/\\') . DIRECTORY_SEPARATOR . 'cache' . DIRECTORY_SEPARATOR . 'mpdf';
|
||||
if (! is_dir($tempDir)) {
|
||||
@mkdir($tempDir, 0775, true);
|
||||
}
|
||||
|
||||
$mergedName = 'merged_' . $ticket_master_id . '_' . time() . '_' . bin2hex(random_bytes(5)) . '.pdf';
|
||||
$mergedPath = $uploadDir . $mergedName;
|
||||
|
||||
try {
|
||||
$mpdf = new Mpdf([
|
||||
'tempDir' => $tempDir,
|
||||
'mode' => 'utf-8',
|
||||
]);
|
||||
|
||||
$totalPages = 0;
|
||||
foreach ($sourcePaths as $src) {
|
||||
try {
|
||||
$pageCount = $mpdf->setSourceFile($src);
|
||||
for ($p = 1; $p <= $pageCount; $p++) {
|
||||
$tplId = $mpdf->importPage($p);
|
||||
$size = $mpdf->getTemplateSize($tplId);
|
||||
$mpdf->AddPageByArray([
|
||||
'orientation' => ($size['width'] > $size['height']) ? 'L' : 'P',
|
||||
'sheet-size' => [$size['width'], $size['height']],
|
||||
]);
|
||||
$mpdf->useTemplate($tplId);
|
||||
$totalPages++;
|
||||
}
|
||||
} catch (\Throwable $e) {
|
||||
log_message('error', "merge_ticket_pdfs | failed to import {$src} | " . $e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
if ($totalPages === 0) {
|
||||
$result['message'] = 'All source PDFs failed to import';
|
||||
return $result;
|
||||
}
|
||||
|
||||
$mpdf->Output($mergedPath, Destination::FILE);
|
||||
} catch (\Throwable $e) {
|
||||
log_message('error', 'merge_ticket_pdfs | mpdf failure | ticket_id=' . $ticket_master_id . ' | ' . $e->getMessage());
|
||||
$result['message'] = 'Merge failed: ' . $e->getMessage();
|
||||
return $result;
|
||||
}
|
||||
|
||||
if (! is_file($mergedPath)) {
|
||||
$result['message'] = 'Merged file was not created';
|
||||
return $result;
|
||||
}
|
||||
|
||||
if ($opts['replace']) {
|
||||
$claimFiles
|
||||
->where('ticket_id', $ticket_master_id)
|
||||
->where('file_type', MERGED_CLAIM_FILE_TYPE)
|
||||
->where('is_active', 1)
|
||||
->set(['is_active' => 0])
|
||||
->update();
|
||||
}
|
||||
|
||||
$insertData = [
|
||||
'ticket_id' => $ticket_master_id,
|
||||
'ticket_type' => $opts['ticket_type'],
|
||||
'file_type' => MERGED_CLAIM_FILE_TYPE,
|
||||
'doc_name' => 'MERGED_CLAIM_DOCS_PDF',
|
||||
'file_name' => $mergedName,
|
||||
'url' => $mergedName,
|
||||
'mime_type' => 'application/pdf',
|
||||
'is_active' => 1,
|
||||
];
|
||||
if (! empty($opts['created_by'])) {
|
||||
$insertData['created_by'] = $opts['created_by'];
|
||||
}
|
||||
|
||||
$insertedId = $claimFiles->insert($insertData);
|
||||
|
||||
if (! $insertedId) {
|
||||
log_message('error', 'merge_ticket_pdfs | DB insert failed for merged file | ticket_id=' . $ticket_master_id);
|
||||
@unlink($mergedPath);
|
||||
$result['message'] = 'Failed to register merged file in claim_files';
|
||||
return $result;
|
||||
}
|
||||
|
||||
log_message(
|
||||
'info',
|
||||
"merge_ticket_pdfs | OK | ticket_id={$ticket_master_id} | sources={$result['source_count']} | pages={$totalPages} | claim_file_id={$insertedId}"
|
||||
);
|
||||
|
||||
$result['status'] = true;
|
||||
$result['merged_file_id'] = (int) $insertedId;
|
||||
$result['file_name'] = $mergedName;
|
||||
$result['pages'] = $totalPages;
|
||||
$result['message'] = 'Merged successfully';
|
||||
return $result;
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user