diff --git a/app/Config/Routes.php b/app/Config/Routes.php index 3e5b041..36dd7d0 100644 --- a/app/Config/Routes.php +++ b/app/Config/Routes.php @@ -569,6 +569,7 @@ $routes->group('billing', function($routes) { $routes->get('patient/show_profile_opd/(:num)', 'Patient::show_profile_opd/$1'); $routes->get('patient/show_profile_opd/(:num)/(:num)', 'Patient::show_profile_opd/$1/$2'); $routes->get('patient/abdm_documents/(:num)', 'Patient::abdm_documents/$1'); + $routes->get('patient/abdm_records_pdf/(:num)', 'Patient::abdm_records_pdf/$1'); $routes->get('patient/abdm_document_detail/(:num)/(:num)', 'Patient::abdm_document_detail/$1/$2'); $routes->get('patient/abdm_content_request/(:num)', 'Patient::abdm_content_request/$1'); $routes->get('patient/abdm_content_auto_flow/(:num)', 'Patient::abdm_content_auto_flow/$1'); diff --git a/app/Controllers/Patient.php b/app/Controllers/Patient.php index 8405e22..57d3ff6 100644 --- a/app/Controllers/Patient.php +++ b/app/Controllers/Patient.php @@ -2128,6 +2128,173 @@ class Patient extends BaseController ]); } + public function abdm_records_pdf(int $pno) + { + if (! $this->db->tableExists('patient_master')) { + return $this->response->setStatusCode(500)->setBody('patient_master table not found'); + } + + $patientRow = $this->db->table('patient_master')->where('id', $pno)->get()->getRowArray(); + if (! is_array($patientRow)) { + return $this->response->setStatusCode(404)->setBody('Patient not found'); + } + + $abhaContext = $this->buildAbhaProfileContext($patientRow); + $patientAbha = trim((string) ($abhaContext['abha_address'] ?? '')); + + if (! $this->db->tableExists('abdm_hiu_documents')) { + return $this->response->setStatusCode(404)->setBody('No ABDM documents table found'); + } + + $docId = (int) ($this->request->getGet('doc_id') ?? 0); + $consentRequestId = trim((string) ($this->request->getGet('consent_request_id') ?? '')); + + $builder = $this->db->table('abdm_hiu_documents d') + ->select('*') + ->orderBy('d.document_date', 'DESC') + ->orderBy('d.id', 'DESC'); + + $builder->groupStart() + ->where('d.patient_id', $pno); + if ($patientAbha !== '') { + $builder->orWhere('d.abha_address', $patientAbha); + } + $builder->groupEnd(); + + if ($docId > 0) { + $builder->where('d.id', $docId); + } elseif ($consentRequestId !== '') { + $docFields = $this->db->getFieldNames('abdm_hiu_documents') ?? []; + $sessionIds = $this->resolveAllRelatedConsentIds($consentRequestId); + if ($sessionIds !== []) { + $builder->groupStart(); + $hasClause = false; + if (in_array('consent_request_id', $docFields, true)) { + $builder->whereIn('d.consent_request_id', $sessionIds); + $hasClause = true; + } + if (in_array('consent_artifact_id', $docFields, true)) { + if ($hasClause) { + $builder->orWhereIn('d.consent_artifact_id', $sessionIds); + } else { + $builder->whereIn('d.consent_artifact_id', $sessionIds); + $hasClause = true; + } + } + if (in_array('consent_ref', $docFields, true)) { + if ($hasClause) { + $builder->orWhereIn('d.consent_ref', $sessionIds); + } else { + $builder->whereIn('d.consent_ref', $sessionIds); + $hasClause = true; + } + } + $builder->groupEnd(); + } + } + + $rows = $builder->get()->getResultArray(); + + foreach ($rows as &$r) { + $summary = json_decode((string) ($r['summary_json'] ?? ''), true); + $r['summary'] = is_array($summary) ? $summary : []; + } + unset($r); + + $hospitalName = defined('H_Name') ? (string) constant('H_Name') : 'Hospital'; + $hospitalAddress1 = defined('H_address_1') ? (string) constant('H_address_1') : ''; + $hospitalAddress2 = defined('H_address_2') ? (string) constant('H_address_2') : ''; + $hospitalPhone = defined('H_phone_No') ? (string) constant('H_phone_No') : ''; + $hospitalEmail = defined('H_Email') ? (string) constant('H_Email') : ''; + $hospitalLogoName = defined('H_logo') ? trim((string) constant('H_logo')) : ''; + + $logoPathCandidates = []; + if ($hospitalLogoName !== '') { + $logoPathCandidates[] = FCPATH . 'assets/images/' . $hospitalLogoName; + $logoPathCandidates[] = FCPATH . 'assets/img/' . $hospitalLogoName; + } + $logoPathCandidates[] = FCPATH . 'assets/img/logo.png'; + $logoPathCandidates[] = FCPATH . 'assets/images/logo.png'; + + $logoSrc = ''; + foreach ($logoPathCandidates as $candidate) { + if (is_file($candidate)) { + $logoSrc = str_replace('\\', '/', $candidate); + break; + } + } + + $patientName = trim((string) (($patientRow['title'] ?? '') . ' ' . ($patientRow['p_fname'] ?? '') . ' ' . ($patientRow['p_lname'] ?? ''))); + if ($patientName === '' || $patientName === '0') { + $patientName = trim((string) ($patientRow['p_fname'] ?? '')); + } + $genderVal = (int) ($patientRow['gender'] ?? 0); + $genderStr = $genderVal === 1 ? 'Male' : ($genderVal === 2 ? 'Female' : ($genderVal === 3 ? 'Other' : '-')); + $ageStr = ! empty($patientRow['age']) ? ($patientRow['age'] . ' Year') : ''; + $ageGender = trim($ageStr . ($ageStr !== '' && $genderStr !== '-' ? ' / ' : '') . ($genderStr !== '-' ? $genderStr : '')); + + $data = [ + 'title' => 'ABDM Health Records - ' . $patientName, + 'patient' => $patientRow, + 'patientId' => (int) ($patientRow['id'] ?? $pno), + 'patientCode' => (string) ($patientRow['p_code'] ?? ('#' . $pno)), + 'patientName' => $patientName, + 'ageGender' => $ageGender, + 'mobile' => (string) ($patientRow['mphone1'] ?? ''), + 'abhaAddress' => $patientAbha, + 'abhaNumber' => (string) ($patientRow['abha_id'] ?? ''), + 'consentRequestId' => $consentRequestId, + 'documents' => $rows, + 'hospitalName' => $hospitalName, + 'hospitalAddress' => trim($hospitalAddress1 . ($hospitalAddress2 !== '' ? ', ' . $hospitalAddress2 : '')), + 'hospitalPhone' => $hospitalPhone, + 'hospitalEmail' => $hospitalEmail, + 'logoSrc' => $logoSrc, + 'printedOn' => date('d-m-Y h:i A'), + ]; + + $html = view('billing/abdm_records_pdf_v', $data); + + $mpdfTempDir = WRITEPATH . 'cache' . DIRECTORY_SEPARATOR . 'mpdf'; + if (! is_dir($mpdfTempDir)) { + mkdir($mpdfTempDir, 0755, true); + } + + $mpdf = new \Mpdf\Mpdf([ + 'mode' => 'utf-8', + 'format' => 'A4', + 'orientation' => 'P', + 'margin_left' => 10, + 'margin_right' => 10, + 'margin_top' => 10, + 'margin_bottom' => 12, + 'default_font' => 'freeserif', + 'tempDir' => $mpdfTempDir, + ]); + $mpdf->autoScriptToLang = true; + $mpdf->autoLangToFont = true; + + $footerHtml = '' + . '' + . '' + . '' + . '' + . '
Ayushman Bharat Digital Mission (ABDM) HIU • Confidential • For Clinical Evaluation OnlyPage {PAGENO} of {nbpg}
'; + $mpdf->SetHTMLFooter($footerHtml, 'O'); + $mpdf->SetHTMLFooter($footerHtml, 'E'); + + $mpdf->SetTitle('ABDM Health Records - ' . $patientName); + $mpdf->WriteHTML($html); + + $safeName = preg_replace('/[^A-Za-z0-9_-]/', '_', $patientName); + $fileName = 'ABDM_Records_' . $safeName . '_' . date('Ymd_His') . '.pdf'; + + return $this->response + ->setHeader('Content-Type', 'application/pdf') + ->setHeader('Content-Disposition', 'inline; filename="' . $fileName . '"') + ->setBody($mpdf->Output($fileName, 'S')); + } + public function abdm_content_request(int $pno) { if (! $this->db->tableExists('patient_master')) { diff --git a/app/Libraries/Abdm/M3HiuGatewayClient.php b/app/Libraries/Abdm/M3HiuGatewayClient.php index 2c0ee5b..58bb3e2 100644 --- a/app/Libraries/Abdm/M3HiuGatewayClient.php +++ b/app/Libraries/Abdm/M3HiuGatewayClient.php @@ -76,6 +76,9 @@ class M3HiuGatewayClient if (isset($payload['consentId']) && is_string($payload['consentId']) && str_contains($payload['consentId'], ':')) { $payload['consentId'] = trim((string) explode(':', $payload['consentId'])[0]); } + if (empty($payload['consent_request_id']) && !empty($payload['abdm_consent_request_id'])) { + $payload['consent_request_id'] = trim((string) $payload['abdm_consent_request_id']); + } return $this->callGet('/v1/hiu/consent/status', $payload, 'consent.status.reconcile'); } diff --git a/app/Libraries/Abdm/M3HiuWorkflowService.php b/app/Libraries/Abdm/M3HiuWorkflowService.php index 27ab132..01f40f6 100644 --- a/app/Libraries/Abdm/M3HiuWorkflowService.php +++ b/app/Libraries/Abdm/M3HiuWorkflowService.php @@ -433,7 +433,7 @@ class M3HiuWorkflowService $this->rememberArtifact($resolvedConsentId, $singleArtifactId, $payload['abha_address'] ?? '', $payload['hfr_id'] ?? ''); } - foreach ($this->getKnownArtifactIds($resolvedConsentId) as $artifactId) { + foreach ($this->getKnownArtifactIds($resolvedConsentId, true) as $artifactId) { if ($artifactId === '' || $artifactId === $resolvedConsentId) { continue; } @@ -534,16 +534,20 @@ class M3HiuWorkflowService ->update($update); } - private function getKnownArtifactIds(string $consentRequestId): array + private function getKnownArtifactIds(string $consentRequestId, bool $pendingOnly = false): array { if (! $this->db->tableExists('abdm_hiu_consent_artifacts') || $consentRequestId === '') { return []; } - $rows = $this->db->table('abdm_hiu_consent_artifacts') + $builder = $this->db->table('abdm_hiu_consent_artifacts') ->select('artifact_id') - ->where('consent_request_id', $consentRequestId) - ->get() - ->getResultArray(); + ->where('consent_request_id', $consentRequestId); + + if ($pendingOnly) { + $builder->whereNotIn('last_status', ['RECEIVED', 'DATA_RECEIVED', 'COMPLETED', 'EXPIRED', 'REVOKED', 'DENIED']); + } + + $rows = $builder->get()->getResultArray(); return array_values(array_unique(array_map( static fn (array $row): string => trim((string) ($row['artifact_id'] ?? '')), diff --git a/app/Views/billing/Patient_Profile_Opd_V.php b/app/Views/billing/Patient_Profile_Opd_V.php index 5d31f02..2e6ede9 100644 --- a/app/Views/billing/Patient_Profile_Opd_V.php +++ b/app/Views/billing/Patient_Profile_Opd_V.php @@ -376,7 +376,10 @@ if ($patientPhotoPath === '') {
Consent Request History
-
+
+ + Print All Records + +