From 1f4fcf6546a8261e249f12be83e6244bcbb131ff Mon Sep 17 00:00:00 2001 From: DevSoftTech Server Date: Thu, 1 Oct 2026 11:24:29 +0530 Subject: [PATCH] fix(diagnosis): resolve person name null concat in report list and handle large dicom file uploads --- app/Controllers/Diagnosis.php | 55 ++++++++++++++++----- app/Views/diagnosis/lab_report_tab.php | 4 +- app/Views/diagnosis/lab_report_tab_path.php | 4 +- app/Views/diagnosis/pathology_detail.php | 36 +++++++++++--- 4 files changed, 74 insertions(+), 25 deletions(-) diff --git a/app/Controllers/Diagnosis.php b/app/Controllers/Diagnosis.php index 2a1590f..9a7a26e 100644 --- a/app/Controllers/Diagnosis.php +++ b/app/Controllers/Diagnosis.php @@ -111,7 +111,7 @@ class Diagnosis extends BaseController if (trim($searchText) == '') { // Get recent 200 records $sql = "SELECT m.id as inv_id, m.invoice_code, m.inv_date, - CONCAT(p.p_fname, '/', IF(p.gender=1,'M','F'), '/', p.p_rname) as inv_name, + COALESCE(NULLIF(CONCAT_WS('/', NULLIF(TRIM(p.p_fname), ''), IF(p.gender=1,'M',IF(p.gender=2,'F',NULL)), NULLIF(TRIM(CONCAT_WS(' ', p.p_relative, p.p_rname)), '')), ''), NULLIF(TRIM(m.inv_name), ''), NULLIF(TRIM(p.p_fname), ''), '-') as inv_name, p.dob, p.age, p.age_in_month, p.estimate_dob, GROUP_CONCAT(CONCAT_WS(';', i.item_name, i.item_name, i.id, check_item_request(m.id, i.id)) SEPARATOR '#') as data_array, @@ -130,7 +130,7 @@ class Diagnosis extends BaseController } else { // Search with criteria $sql = "SELECT m.id as inv_id, m.invoice_code, m.inv_date, - CONCAT(p.p_fname, '/', IF(p.gender=1,'M','F'), '/', p.p_rname) as inv_name, + COALESCE(NULLIF(CONCAT_WS('/', NULLIF(TRIM(p.p_fname), ''), IF(p.gender=1,'M',IF(p.gender=2,'F',NULL)), NULLIF(TRIM(CONCAT_WS(' ', p.p_relative, p.p_rname)), '')), ''), NULLIF(TRIM(m.inv_name), ''), NULLIF(TRIM(p.p_fname), ''), '-') as inv_name, p.dob, p.age, p.age_in_month, p.estimate_dob, GROUP_CONCAT(CONCAT_WS(';', i.item_name, i.item_name, i.id, check_item_request(m.id, i.id)) SEPARATOR '#') as data_array, @@ -180,7 +180,7 @@ class Diagnosis extends BaseController if (trim($srNo) == '') { // Get recent 3 days records with serial number $sql = "SELECT m.id as inv_id, m.invoice_code, m.inv_date, - CONCAT(p.p_fname, '/', IF(p.gender=1,'M','F'), '/', p.p_rname) as inv_name, + COALESCE(NULLIF(CONCAT_WS('/', NULLIF(TRIM(p.p_fname), ''), IF(p.gender=1,'M',IF(p.gender=2,'F',NULL)), NULLIF(TRIM(CONCAT_WS(' ', p.p_relative, p.p_rname)), '')), ''), NULLIF(TRIM(m.inv_name), ''), NULLIF(TRIM(p.p_fname), ''), '-') as inv_name, p.dob, p.age, p.age_in_month, p.estimate_dob, GROUP_CONCAT(CONCAT_WS(';', i.item_name, i.item_name, i.id, check_item_request(m.id, i.id)) SEPARATOR '#') as data_array, @@ -200,7 +200,7 @@ class Diagnosis extends BaseController } else { // Search by specific serial number $sql = "SELECT m.id as inv_id, m.invoice_code, m.inv_date, - CONCAT(p.p_fname, '/', IF(p.gender=1,'M','F'), '/', p.p_rname) as inv_name, + COALESCE(NULLIF(CONCAT_WS('/', NULLIF(TRIM(p.p_fname), ''), IF(p.gender=1,'M',IF(p.gender=2,'F',NULL)), NULLIF(TRIM(CONCAT_WS(' ', p.p_relative, p.p_rname)), '')), ''), NULLIF(TRIM(m.inv_name), ''), NULLIF(TRIM(p.p_fname), ''), '-') as inv_name, p.dob, p.age, p.age_in_month, p.estimate_dob, GROUP_CONCAT(CONCAT_WS(';', i.item_name, i.item_name, i.id, check_item_request(m.id, i.id)) SEPARATOR '#') as data_array, @@ -243,7 +243,7 @@ class Diagnosis extends BaseController if (trim($labNo) == '') { // Get recent 3 days records with lab number $sql = "SELECT m.id as inv_id, m.invoice_code, m.inv_date, - CONCAT(p.p_fname, '/', IF(p.gender=1,'M','F'), '/', p.p_rname) as inv_name, + COALESCE(NULLIF(CONCAT_WS('/', NULLIF(TRIM(p.p_fname), ''), IF(p.gender=1,'M',IF(p.gender=2,'F',NULL)), NULLIF(TRIM(CONCAT_WS(' ', p.p_relative, p.p_rname)), '')), ''), NULLIF(TRIM(m.inv_name), ''), NULLIF(TRIM(p.p_fname), ''), '-') as inv_name, p.dob, p.age, p.age_in_month, p.estimate_dob, GROUP_CONCAT(CONCAT_WS(';', i.item_name, i.item_name, i.id, check_item_request(m.id, i.id)) SEPARATOR '#') as data_array, @@ -263,7 +263,7 @@ class Diagnosis extends BaseController } else { // Search by specific lab number $sql = "SELECT m.id as inv_id, m.invoice_code, m.inv_date, - CONCAT(p.p_fname, '/', IF(p.gender=1,'M','F'), '/', p.p_rname) as inv_name, + COALESCE(NULLIF(CONCAT_WS('/', NULLIF(TRIM(p.p_fname), ''), IF(p.gender=1,'M',IF(p.gender=2,'F',NULL)), NULLIF(TRIM(CONCAT_WS(' ', p.p_relative, p.p_rname)), '')), ''), NULLIF(TRIM(m.inv_name), ''), NULLIF(TRIM(p.p_fname), ''), '-') as inv_name, p.dob, p.age, p.age_in_month, p.estimate_dob, GROUP_CONCAT(CONCAT_WS(';', i.item_name, i.item_name, i.id, check_item_request(m.id, i.id)) SEPARATOR '#') as data_array, @@ -344,6 +344,7 @@ class Diagnosis extends BaseController $data = [ 'invoice' => $invoice, + 'invoice_id' => (int) $invoiceId, 'tests' => $tests ?? [], 'items' => $items ?? [], 'lab_invoice' => $labInvoice, @@ -395,7 +396,7 @@ class Diagnosis extends BaseController // Get test list with all details - matching old code structure $sql = "SELECT m.id as inv_id, m.invoice_code, m.inv_date, - CONCAT(p.p_fname, '/', IF(p.gender=1,'M','F'), '/', p.p_rname) as inv_name, + COALESCE(NULLIF(CONCAT_WS('/', NULLIF(TRIM(p.p_fname), ''), IF(p.gender=1,'M',IF(p.gender=2,'F',NULL)), NULLIF(TRIM(CONCAT_WS(' ', p.p_relative, p.p_rname)), '')), ''), NULLIF(TRIM(m.inv_name), ''), NULLIF(TRIM(p.p_fname), ''), '-') as inv_name, i.item_name, i.id as test_id, i.item_id, r.lab_repo_id, y.group_desc as item_type_desc, r.status, r.id as req_id, r.charge_item_id, r.print_combine @@ -3103,15 +3104,37 @@ class Diagnosis extends BaseController return $this->response->setJSON(['update' => 0, 'error_text' => 'Invalid request']); } + // Detect PHP post_max_size overflow (when upload exceeds post_max_size, PHP discards $_POST & $_FILES) + if (empty($_POST) && empty($_FILES) && isset($_SERVER['CONTENT_LENGTH']) && (int) $_SERVER['CONTENT_LENGTH'] > 0) { + $maxPost = ini_get('post_max_size') ?: 'server limit'; + return $this->response->setJSON([ + 'update' => 0, + 'error_text' => "Uploaded file exceeds server post limit ({$maxPost}). Please increase PHP post_max_size or upload a smaller file." + ]); + } + if (! $this->db->tableExists('file_upload_data')) { return $this->response->setJSON(['update' => 0, 'error_text' => 'file_upload_data table not found']); } - $invoiceId = (int) $this->request->getPost('invoice_id'); - $labType = (int) $this->request->getPost('lab_type'); - $reqId = (int) $this->request->getPost('req_id'); - $fileDesc = trim((string) $this->request->getPost('file_desc')); - $scanType = trim((string) $this->request->getPost('scan_type')); + $invoiceId = (int) ($this->request->getPost('invoice_id') ?: $this->request->getGet('invoice_id') ?: $this->request->getHeaderLine('X-Invoice-Id')); + $labType = (int) ($this->request->getPost('lab_type') ?: $this->request->getGet('lab_type') ?: $this->request->getHeaderLine('X-Lab-Type')); + $reqId = (int) ($this->request->getPost('req_id') ?: $this->request->getGet('req_id') ?: $this->request->getHeaderLine('X-Req-Id')); + $fileDesc = trim((string) ($this->request->getPost('file_desc') ?: $this->request->getGet('file_desc'))); + $scanType = trim((string) ($this->request->getPost('scan_type') ?: $this->request->getGet('scan_type'))); + + // If req_id is provided, resolve missing invoice_id or lab_type from lab_request + if ($reqId > 0 && ($invoiceId <= 0 || $labType <= 0)) { + $lr = $this->db->table('lab_request')->select('charge_id, lab_type, invoice_code')->where('id', $reqId)->get(1)->getRowArray(); + if (!empty($lr)) { + if ($invoiceId <= 0) { + $invoiceId = (int) ($lr['charge_id'] ?? 0); + } + if ($labType <= 0) { + $labType = (int) ($lr['lab_type'] ?? 0); + } + } + } if ($invoiceId <= 0 || $labType <= 0) { return $this->response->setJSON(['update' => 0, 'error_text' => 'Missing invoice/lab context']); @@ -3119,7 +3142,13 @@ class Diagnosis extends BaseController $upload = $this->request->getFile('report_file'); if (! $upload || ! $upload->isValid()) { - return $this->response->setJSON(['update' => 0, 'error_text' => 'No valid file uploaded']); + $errCode = $upload ? $upload->getError() : UPLOAD_ERR_NO_FILE; + $msg = 'No valid file uploaded'; + if ($errCode === UPLOAD_ERR_INI_SIZE || $errCode === UPLOAD_ERR_FORM_SIZE) { + $maxUpload = ini_get('upload_max_filesize') ?: 'server limit'; + $msg = "Uploaded file exceeds maximum allowed file size ({$maxUpload})."; + } + return $this->response->setJSON(['update' => 0, 'error_text' => $msg]); } $ext = strtolower((string) $upload->getExtension()); diff --git a/app/Views/diagnosis/lab_report_tab.php b/app/Views/diagnosis/lab_report_tab.php index 361acd2..0195101 100644 --- a/app/Views/diagnosis/lab_report_tab.php +++ b/app/Views/diagnosis/lab_report_tab.php @@ -27,8 +27,8 @@ invoice_code ?? '-') ?> daily_sr_no ?? '-') ?> - lab_test_no ?? '-') ?> - inv_name ?? '-') ?> | age ?? '-') ?> + inv_name) && trim((string) $row->inv_name) !== '-') ? $row->inv_name : (!empty($row->p_fname) ? $row->p_fname : '-'); ?> + | age ?? '-') ?> inv_date ?? '-') ?> invoice_code ?? '-') ?> daily_sr_no ?? '-') ?> - lab_test_no ?? '-') ?> - inv_name ?? '-') ?> | age ?? '-') ?> + inv_name) && trim((string) $row->inv_name) !== '-') ? $row->inv_name : (!empty($row->p_fname) ? $row->p_fname : '-'); ?> + | age ?? '-') ?> inv_date ?? '-') ?> var baseUrl = '/'; -var invoiceId = 'inv_id ?? $invoice->id ?? '0'); ?>'; -var labType = ''; +var invoiceId = 'inv_id ?? $invoice->id ?? '0')); ?>'; +var labType = ''; console.log('Page loaded - Invoice ID:', invoiceId, 'Lab Type:', labType); @@ -886,7 +886,14 @@ function diagnosisUploadFromEditor(mode) { } function diagnosisUploadForReq(reqId, testName, mode) { - if (!invoiceId || !labType) { + var invId = (typeof invoiceId !== 'undefined' && invoiceId && invoiceId !== '0') + ? invoiceId + : (document.getElementById('invoiceId') ? document.getElementById('invoiceId').value : ''); + var lType = (typeof labType !== 'undefined' && labType && labType !== '0') + ? labType + : (document.getElementById('labType') ? document.getElementById('labType').value : ''); + + if (!invId || !lType) { alert('Invoice context missing'); return; } @@ -905,13 +912,18 @@ function diagnosisUploadForReq(reqId, testName, mode) { return; } + if (file.size > 150 * 1024 * 1024) { + alert('File is too large (' + (file.size / (1024 * 1024)).toFixed(1) + ' MB). Maximum allowed size is 128 MB.'); + return; + } + const modalBody = document.getElementById('testDataEntryBody'); const csrfField = modalBody ? modalBody.querySelector('input[name]') : null; const formData = new FormData(); formData.append('report_file', file); - formData.append('invoice_id', String(invoiceId)); - formData.append('lab_type', String(labType)); + formData.append('invoice_id', String(invId)); + formData.append('lab_type', String(lType)); formData.append('req_id', String(reqId || 0)); formData.append('file_desc', (testName || 'Imaging Report') + ((mode === 'camera') ? ' (Camera)' : ' (Upload)')); formData.append('scan_type', (mode === 'camera') ? 'camera' : 'upload'); @@ -919,11 +931,19 @@ function diagnosisUploadForReq(reqId, testName, mode) { formData.append(csrfField.name, csrfField.value); } - fetch(baseUrl + 'diagnosis/upload-report-file', { + const uploadUrl = baseUrl + 'diagnosis/upload-report-file' + + '?invoice_id=' + encodeURIComponent(invId) + + '&lab_type=' + encodeURIComponent(lType) + + '&req_id=' + encodeURIComponent(reqId || 0); + + fetch(uploadUrl, { method: 'POST', body: formData, headers: { - 'X-Requested-With': 'XMLHttpRequest' + 'X-Requested-With': 'XMLHttpRequest', + 'X-Invoice-Id': String(invId), + 'X-Lab-Type': String(lType), + 'X-Req-Id': String(reqId || 0) } }) .then(response => response.json()) @@ -937,7 +957,7 @@ function diagnosisUploadForReq(reqId, testName, mode) { }) .catch(error => { console.error('Upload error:', error); - alert('Upload request failed'); + alert('Upload request failed: ' + (error && error.message ? error.message : 'Please check file size and server connection')); }); };