devsofttech_hms_ci4/app/Controllers/Setting/Template.php

847 lines
30 KiB
PHP

<?php
namespace App\Controllers\Setting;
use App\Controllers\BaseController;
use App\Models\PathLabModel;
class Template extends BaseController
{
private const PERMISSIONS = [
'template.pathology',
'template.ultrasound',
'template.xray',
'template.ct',
'template.mri',
'template.echo',
'template.discharge',
];
public function __construct()
{
$this->db = db_connect();
helper(['form']);
}
private function requirePermission(string $permission)
{
$user = auth()->user();
if (! $user || ! $user->can($permission)) {
if ($this->request->isAJAX()) {
return $this->response->setStatusCode(403)->setJSON([
'update' => 0,
'showcontent' => 'Permission denied',
'csrfName' => csrf_token(),
'csrfHash' => csrf_hash(),
]);
}
return $this->response->setStatusCode(403)->setBody('Forbidden');
}
return null;
}
private function requireAnyPermission(array $permissions)
{
$user = auth()->user();
if (! $user) {
return $this->response->setStatusCode(403)->setBody('Forbidden');
}
foreach ($permissions as $permission) {
if ($user->can($permission)) {
return null;
}
}
if ($this->request->isAJAX()) {
return $this->response->setStatusCode(403)->setJSON([
'update' => 0,
'showcontent' => 'Permission denied',
'csrfName' => csrf_token(),
'csrfHash' => csrf_hash(),
]);
}
return $this->response->setStatusCode(403)->setBody('Forbidden');
}
private function permissionForModality(int $modality): ?string
{
$map = [
1 => 'template.ultrasound',
2 => 'template.mri',
3 => 'template.xray',
4 => 'template.ct',
6 => 'template.echo',
];
return $map[$modality] ?? null;
}
private function requireModalityPermission(int $modality)
{
$permission = $this->permissionForModality($modality);
if ($permission === null) {
if ($this->request->isAJAX()) {
return $this->response->setStatusCode(400)->setJSON([
'update' => 0,
'showcontent' => 'Invalid modality',
]);
}
return $this->response->setStatusCode(400)->setBody('Invalid modality');
}
return $this->requirePermission($permission);
}
public function index()
{
if ($resp = $this->requireAnyPermission(self::PERMISSIONS)) {
return $resp;
}
return view('Setting/Template/index');
}
public function report_list()
{
if ($resp = $this->requirePermission('template.pathology')) {
return $resp;
}
$sql = "select g.RepoGrp, r.Title, r.mstRepoKey
from (lab_repo r join lab_rgroups g on r.GrpKey=g.mstRGrpKey)
join hc_items i on r.charge_id=i.id and i.itype in (5,6)";
$query = $this->db->query($sql);
$data['labReport_master'] = $query->getResult();
return view('PathLab_Report/lab_report_list', $data);
}
public function report_ultrasound_list(int $modality = 2)
{
if ($resp = $this->requireModalityPermission($modality)) {
return $resp;
}
$sql = "select * from radiology_ultrasound_template where modality=" . (int) $modality;
$query = $this->db->query($sql);
$data['labReport_master'] = $query->getResult();
$data['modality'] = $modality;
return view('PathLab_Report/ultrasound_template_list', $data);
}
public function report_test_list(int $repoId)
{
if ($resp = $this->requirePermission('template.pathology')) {
return $resp;
}
$sql = "select j.id, r.mstRepoKey, t.mstTestKey, t.Test, t.TestID, t.Result, j.EOrder
from lab_repo r join lab_repotests j join lab_tests t
on r.mstRepoKey=j.mstRepoKey and j.mstTestKey=t.mstTestKey
where r.mstRepoKey=" . (int) $repoId . " order by j.EOrder";
$query = $this->db->query($sql);
$data['lab_Rep_Item_List'] = $query->getResult();
$data['mstRepoKey'] = $repoId;
return view('PathLab_Report/lab_report_test_list', $data);
}
public function test_search_page(int $repoId)
{
if ($resp = $this->requirePermission('template.pathology')) {
return $resp;
}
return view('PathLab_Report/lab_test_search', [
'repo_id' => $repoId,
]);
}
public function test_parameter_load(int $mstTestKey, int $mstRepoKey)
{
if ($resp = $this->requirePermission('template.pathology')) {
return $resp;
}
$sql = "select * from lab_tests where mstTestKey=" . (int) $mstTestKey;
$query = $this->db->query($sql);
$data['lab_test_parameter'] = $query->getResult();
$sql = "select *, if(option_bold=1,'Bold','') as option_bold_str
from lab_tests_option where mstTestKey=" . (int) $mstTestKey . " order by sort_id";
$query = $this->db->query($sql);
$data['lab_test_option'] = $query->getResult();
$data['mstRepoKey'] = $mstRepoKey;
return view('PathLab_Report/lab_report_item_edit', $data);
}
public function reportedit_load(int $repoId = 0)
{
if ($resp = $this->requirePermission('template.pathology')) {
return $resp;
}
$sql = "select * from lab_repo where mstRepoKey=" . (int) $repoId;
$query = $this->db->query($sql);
$data['labReport_master'] = $query->getResult();
$itemId = 0;
if (! empty($data['labReport_master'])) {
$itemId = (int) ($data['labReport_master'][0]->charge_id ?? 0);
}
$sql = "select * from hc_items where itype in (5,30,6)
and id not in (select charge_id from lab_repo where charge_id>0 and charge_id<>" . (int) $itemId . ")
order by idesc";
$query = $this->db->query($sql);
$data['hc_items'] = $query->getResult();
$sql = "select * from lab_rgroups";
$query = $this->db->query($sql);
$data['lab_rgroups'] = $query->getResult();
$sql = "select j.id, r.mstRepoKey, t.mstTestKey, t.Test, t.TestID, t.Result, j.EOrder
from lab_repo r join lab_repotests j join lab_tests t
on r.mstRepoKey=j.mstRepoKey and j.mstTestKey=t.mstTestKey
where r.mstRepoKey=" . (int) $repoId . " order by j.EOrder";
$query = $this->db->query($sql);
$data['lab_Rep_Item_List'] = $query->getResult();
$sql = "select * from color";
$query = $this->db->query($sql);
$data['color_name'] = $query->getResult();
$data['repo_id'] = $repoId;
return view('PathLab_Report/lab_report_edit', $data);
}
public function reportedit_ultrasound_load(int $modality = 2, int $repoId = 0)
{
if ($resp = $this->requireModalityPermission($modality)) {
return $resp;
}
$sql = "select * from radiology_ultrasound_template where modality=" . (int) $modality . " and id=" . (int) $repoId;
$query = $this->db->query($sql);
$data['labReport_master'] = $query->getResult();
$sql = "select * from hc_items where itype=" . (int) $modality . " order by idesc";
$query = $this->db->query($sql);
$data['hc_items'] = $query->getResult();
$data['repo_id'] = $repoId;
$data['modality'] = $modality;
return view('PathLab_Report/ultrasound_report_edit', $data);
}
public function report_update()
{
if ($resp = $this->requirePermission('template.pathology')) {
return $resp;
}
if (! $this->request->isAJAX()) {
return $this->response->setStatusCode(400)->setJSON([
'update_record' => 0,
'showcontent' => 'Invalid request',
]);
}
$repoId = (int) $this->request->getPost('repo_id');
$inputReportName = trim((string) $this->request->getPost('input_Reportname'));
$chargeId = (int) $this->request->getPost('charge_id');
$groupId = (int) $this->request->getPost('group_id');
$htmlData = (string) $this->request->getPost('HTMLData');
if ($repoId <= 0 || $inputReportName === '') {
return $this->response->setJSON([
'update_record' => 0,
'showcontent' => 'Report name is required.',
]);
}
$pathLab = new PathLabModel();
$pathLab->updateReport([
'Title' => $inputReportName,
'GrpKey' => $groupId,
'charge_id' => $chargeId,
'HTMLData' => $htmlData,
], $repoId);
return $this->response->setJSON([
'update_record' => 1,
'showcontent' => 'Data Saved successfully',
]);
}
public function report_insert()
{
if ($resp = $this->requirePermission('template.pathology')) {
return $resp;
}
if (! $this->request->isAJAX()) {
return $this->response->setStatusCode(400)->setJSON([
'insertid' => 0,
'showcontent' => 'Invalid request',
]);
}
$inputReportName = trim((string) $this->request->getPost('input_Reportname'));
$chargeId = (int) $this->request->getPost('charge_id');
$groupId = (int) $this->request->getPost('group_id');
$htmlData = (string) $this->request->getPost('HTMLData');
if ($inputReportName === '') {
return $this->response->setJSON([
'insertid' => 0,
'showcontent' => 'Report name is required.',
]);
}
$pathLab = new PathLabModel();
$insertId = $pathLab->insertReport([
'Title' => $inputReportName,
'GrpKey' => $groupId,
'charge_id' => $chargeId,
'HTMLData' => $htmlData,
]);
return $this->response->setJSON([
'insertid' => $insertId,
'showcontent' => $insertId > 0 ? 'Data Saved successfully' : 'Unable to save data',
]);
}
public function report_ultrasound_insert(int $modality)
{
if ($resp = $this->requireModalityPermission($modality)) {
return $resp;
}
if (! $this->request->isAJAX()) {
return $this->response->setStatusCode(400)->setJSON([
'insertid' => 0,
'showcontent' => 'Invalid request',
]);
}
$inputReportName = trim((string) $this->request->getPost('input_Reportname'));
$chargeId = (int) $this->request->getPost('charge_id');
$groupId = (string) $this->request->getPost('group_id');
$htmlData = (string) $this->request->getPost('HTMLData');
$impression = (string) $this->request->getPost('Impression');
if ($inputReportName === '') {
return $this->response->setJSON([
'insertid' => 0,
'showcontent' => 'Report name is required.',
]);
}
$pathLab = new PathLabModel();
$insertId = $pathLab->insertUltrasoundReport([
'template_name' => $inputReportName,
'title' => $groupId,
'charge_id' => $chargeId,
'Findings' => $htmlData,
'Impression' => $impression,
'modality' => $modality,
]);
return $this->response->setJSON([
'insertid' => $insertId,
'showcontent' => $insertId > 0 ? 'Data Saved successfully' : 'Unable to save data',
]);
}
public function report_ultrasound_update(int $modality = 2)
{
if ($resp = $this->requireModalityPermission($modality)) {
return $resp;
}
if (! $this->request->isAJAX()) {
return $this->response->setStatusCode(400)->setJSON([
'update_record' => 0,
'showcontent' => 'Invalid request',
]);
}
$repoId = (int) $this->request->getPost('repo_id');
$inputReportName = trim((string) $this->request->getPost('input_Reportname'));
$chargeId = (int) $this->request->getPost('charge_id');
$groupId = (string) $this->request->getPost('group_id');
$htmlData = (string) $this->request->getPost('HTMLData');
$impression = (string) $this->request->getPost('Impression');
if ($repoId <= 0 || $inputReportName === '') {
return $this->response->setJSON([
'update_record' => 0,
'showcontent' => 'Report name is required.',
]);
}
$pathLab = new PathLabModel();
$pathLab->updateUltrasoundReport([
'template_name' => $inputReportName,
'title' => $groupId,
'charge_id' => $chargeId,
'Findings' => $htmlData,
'Impression' => $impression,
'modality' => $modality,
], $repoId);
return $this->response->setJSON([
'update_record' => 1,
'showcontent' => 'Data Saved successfully',
]);
}
public function test_parameter_edit()
{
if ($resp = $this->requirePermission('template.pathology')) {
return $resp;
}
if (! $this->request->isAJAX()) {
return $this->response->setStatusCode(400)->setJSON([
'update_value' => 0,
'showcontent' => 'Invalid request',
]);
}
$mstTestKey = (int) $this->request->getPost('mstTestKey');
$pathLab = new PathLabModel();
$data = [
'Test' => (string) $this->request->getPost('input_Test_name'),
'TestID' => (string) $this->request->getPost('input_test_code'),
'Result' => (string) $this->request->getPost('input_Default'),
'Formula' => (string) $this->request->getPost('input_Formula'),
'VRule' => (string) $this->request->getPost('input_Validation'),
'VMsg' => (string) $this->request->getPost('input_Message'),
'Unit' => (string) $this->request->getPost('input_Unit'),
'FixedNormals' => (string) $this->request->getPost('input_Fixed'),
'isGenderSpecific' => (int) $this->request->getPost('input_isChecked'),
'FixedNormalsWomen' => (string) $this->request->getPost('input_FixedNormalsWomen'),
];
$pathLab->updateItemParameter($data, $mstTestKey);
return $this->response->setJSON([
'update_value' => 1,
'showcontent' => 'Data Saved successfully',
]);
}
public function test_parameter_add()
{
if ($resp = $this->requirePermission('template.pathology')) {
return $resp;
}
if (! $this->request->isAJAX()) {
return $this->response->setStatusCode(400)->setJSON([
'insert_id' => 0,
'showcontent' => 'Invalid request',
]);
}
$pathLab = new PathLabModel();
$data = [
'Test' => (string) $this->request->getPost('input_Test_name'),
'TestID' => (string) $this->request->getPost('input_test_code'),
'Result' => (string) $this->request->getPost('input_Default'),
'Formula' => (string) $this->request->getPost('input_Formula'),
'VRule' => (string) $this->request->getPost('input_Validation'),
'VMsg' => (string) $this->request->getPost('input_Message'),
'Unit' => (string) $this->request->getPost('input_Unit'),
'FixedNormals' => (string) $this->request->getPost('input_Fixed'),
];
$insertId = $pathLab->insertItemParameter($data);
return $this->response->setJSON([
'insert_id' => $insertId,
'showcontent' => $insertId > 0 ? 'Data Saved successfully' : 'Unable to save data',
]);
}
public function test_parameter_option_add()
{
if ($resp = $this->requirePermission('template.pathology')) {
return $resp;
}
if (! $this->request->isAJAX()) {
return $this->response->setStatusCode(400)->setJSON([
'insert_id' => 0,
'showcontent' => 'Invalid request',
]);
}
$mstTestKey = (int) $this->request->getPost('mstTestKey');
$pathLab = new PathLabModel();
$nextSort = 1;
$row = $this->db->table('lab_tests_option')
->selectMax('sort_id', 'max_sort')
->where('mstTestKey', $mstTestKey)
->get()
->getRowArray();
if (! empty($row['max_sort'])) {
$nextSort = (int) $row['max_sort'] + 1;
}
$insertId = $pathLab->insertItemParameterOption([
'mstTestKey' => $mstTestKey,
'option_value' => (string) $this->request->getPost('input_op_value'),
'option_bold' => (int) $this->request->getPost('chk_bold'),
'sort_id' => $nextSort,
]);
return $this->response->setJSON([
'insert_id' => $insertId,
'showcontent' => $insertId > 0 ? 'Data Saved successfully' : 'Unable to save data',
'option_content' => $this->renderOptionTable($mstTestKey),
]);
}
public function remove_test_option(int $optionId, int $mstTestKey)
{
if ($resp = $this->requirePermission('template.pathology')) {
return $resp;
}
$this->db->table('lab_tests_option')->where('id', $optionId)->delete();
return $this->response->setJSON([
'insert_id' => 1,
'showcontent' => 'Removed successfully',
'option_content' => $this->renderOptionTable($mstTestKey),
]);
}
public function change_sort(int $mstTestKey, int $optionId, int $current, int $changeOptionId, int $change)
{
if ($resp = $this->requirePermission('template.pathology')) {
return $resp;
}
$pathLab = new PathLabModel();
$pathLab->updateItemParameterOption(['sort_id' => 0], $optionId);
$pathLab->updateItemParameterOption(['sort_id' => $current], $changeOptionId);
$pathLab->updateItemParameterOption(['sort_id' => $change], $optionId);
return $this->response->setJSON([
'insert_id' => 1,
'showcontent' => 'Updated successfully',
'option_content' => $this->renderOptionTable($mstTestKey),
]);
}
public function change_sort_item(int $repoId, int $optionId, int $current, int $changeOptionId, int $change)
{
if ($resp = $this->requirePermission('template.pathology')) {
return $resp;
}
$pathLab = new PathLabModel();
$pathLab->updateItemSortorder(['EOrder' => 0], $optionId);
$pathLab->updateItemSortorder(['EOrder' => $current], $changeOptionId);
$pathLab->updateItemSortorder(['EOrder' => $change], $optionId);
return $this->report_test_list($repoId);
}
public function add_test_repo(int $repoId, int $testId)
{
if ($resp = $this->requirePermission('template.pathology')) {
return $resp;
}
$exists = $this->db->table('lab_repotests')
->where('mstRepoKey', $repoId)
->where('mstTestKey', $testId)
->countAllResults();
if ($exists > 0) {
return $this->response->setJSON([
'insertid' => 0,
'showcontent' => 'Already Added',
]);
}
$row = $this->db->table('lab_repotests')
->selectMax('EOrder', 'max_order')
->where('mstRepoKey', $repoId)
->get()
->getRowArray();
$nextOrder = (int) ($row['max_order'] ?? 0) + 1;
$pathLab = new PathLabModel();
$insertId = $pathLab->insertItemSortorder([
'mstRepoKey' => $repoId,
'mstTestKey' => $testId,
'EOrder' => $nextOrder,
]);
return $this->response->setJSON([
'insertid' => $insertId,
'showcontent' => $insertId > 0 ? 'Add successfully' : 'Unable to add',
]);
}
public function remove_test_item(int $repoId, int $testId)
{
if ($resp = $this->requirePermission('template.pathology')) {
return $resp;
}
$this->db->table('lab_repotests')
->where('mstRepoKey', $repoId)
->where('mstTestKey', $testId)
->delete();
}
public function test_item_search()
{
if ($resp = $this->requirePermission('template.pathology')) {
return $resp;
}
$testName = (string) $this->request->getPost('input_Test_name');
$repoId = (int) $this->request->getPost('repo_id');
$sql = "select * from lab_tests where Test like '%" . $this->db->escapeLikeString($testName) . "%'";
$query = $this->db->query($sql);
$searchResult = $query->getResult();
$html = '<table class="table table-sm">';
foreach ($searchResult as $row) {
$html .= '<tr><td><a href="javascript:add_test(' . (int) $repoId . ',' . (int) $row->mstTestKey . ');">' . esc($row->Test) . '</a></td><td>[ ' . esc($row->TestID) . ' ]</td></tr>';
}
$html .= '</table>';
return $this->response->setBody($html);
}
private function renderOptionTable(int $mstTestKey): string
{
$sql = "select *, if(option_bold=1,'Bold','') as option_bold_str
from lab_tests_option where mstTestKey=" . (int) $mstTestKey . " order by sort_id";
$query = $this->db->query($sql);
$options = $query->getResult();
return view('PathLab_Report/_test_option_table', [
'lab_test_option' => $options,
'mstTestKey' => $mstTestKey,
]);
}
private function ensureDischargeTemplateTable(): void
{
if ($this->db->tableExists('ipd_discharge_templates')) {
return;
}
$sql = "CREATE TABLE IF NOT EXISTS ipd_discharge_templates (
id INT NOT NULL AUTO_INCREMENT,
template_name VARCHAR(120) NOT NULL,
template_html LONGTEXT NOT NULL,
is_default TINYINT(1) NOT NULL DEFAULT 0,
status TINYINT(1) NOT NULL DEFAULT 1,
created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
updated_at DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4";
$this->db->query($sql);
}
private function defaultDischargeTemplateHtml(): string
{
return '<h3 style="margin:0 0 8px 0;">Discharge Summary</h3>'
. '<table style="width:100%;border-collapse:collapse;margin-bottom:10px;" border="1" cellpadding="6">'
. '<tr>'
. '<td><b>Patient</b>: {{PATIENT_NAME}}</td>'
. '<td><b>UHID</b>: {{UHID}}</td>'
. '<td><b>IPD</b>: {{IPD_CODE}}</td>'
. '</tr>'
. '<tr>'
. '<td><b>Age/Gender</b>: {{AGE_GENDER}}</td>'
. '<td><b>Admit Date</b>: {{ADMIT_DATE}}</td>'
. '<td><b>Discharge Date</b>: {{DISCHARGE_DATE}}</td>'
. '</tr>'
. '</table>'
. '<div>{{CONTENT}}</div>';
}
private function nabhDischargeTemplateHtml(): string
{
return '<h2 style="margin:0 0 10px 0;text-align:center;">DISCHARGE SUMMARY</h2>'
. '<table style="width:100%;border-collapse:collapse;margin-bottom:10px;" border="1" cellpadding="6">'
. '<tr>'
. '<td><b>Patient Name</b>: {{PATIENT_NAME}}</td>'
. '<td><b>UHID</b>: {{UHID}}</td>'
. '<td><b>IPD No.</b>: {{IPD_CODE}}</td>'
. '</tr>'
. '<tr>'
. '<td><b>Age/Gender</b>: {{AGE_GENDER}}</td>'
. '<td><b>Date of Admission</b>: {{ADMIT_DATE}}</td>'
. '<td><b>Date of Discharge</b>: {{DISCHARGE_DATE}}</td>'
. '</tr>'
. '<tr>'
. '<td colspan="3"><b>Prepared On</b>: {{CURRENT_DATE}}</td>'
. '</tr>'
. '</table>'
. '<div style="font-size:11px;color:#334155;margin-bottom:10px;">'
. 'NABH guidance note: Ensure diagnosis, procedures, clinical course, condition at discharge, medication with dose/duration, follow-up advice, red-flag signs, and emergency contact are documented.'
. '</div>'
. '<div style="margin-bottom:10px;">{{CONTENT}}</div>'
. '<h4 style="margin:12px 0 6px 0;">Counselling & Handover Confirmation</h4>'
. '<table style="width:100%;border-collapse:collapse;margin-bottom:10px;" border="1" cellpadding="6">'
. '<tr><td style="width:32%;">Medication explained to patient/attendant</td><td style="width:8%;"></td><td style="width:60%;">Remarks:</td></tr>'
. '<tr><td>Follow-up date and department explained</td><td></td><td>Next Visit: __________________</td></tr>'
. '<tr><td>Red-flag symptoms explained</td><td></td><td>Emergency Contact: __________________</td></tr>'
. '<tr><td>Diet and activity instructions explained</td><td></td><td></td></tr>'
. '</table>'
. '<table style="width:100%;border-collapse:collapse;margin-top:20px;" border="1" cellpadding="10">'
. '<tr>'
. '<td style="width:33%;vertical-align:bottom;">____________________<br>Consultant Name/Signature</td>'
. '<td style="width:33%;vertical-align:bottom;">____________________<br>Medical Officer Signature</td>'
. '<td style="width:34%;vertical-align:bottom;">____________________<br>Patient/Attendant Signature & Date</td>'
. '</tr>'
. '</table>';
}
private function ensureDefaultDischargeTemplateSeeded(): void
{
$this->ensureDischargeTemplateTable();
if (! $this->db->tableExists('ipd_discharge_templates')) {
return;
}
$table = $this->db->table('ipd_discharge_templates');
$count = (int) ($table->countAllResults() ?? 0);
if ($count === 0) {
$table->insert([
'template_name' => 'Default Discharge Template',
'template_html' => $this->defaultDischargeTemplateHtml(),
'is_default' => 1,
'status' => 1,
]);
}
$nabhExists = $this->db->table('ipd_discharge_templates')
->where('template_name', 'NABH Compliant Discharge Summary')
->get(1)
->getRowArray();
if (empty($nabhExists)) {
$table->insert([
'template_name' => 'NABH Compliant Discharge Summary',
'template_html' => $this->nabhDischargeTemplateHtml(),
'is_default' => 0,
'status' => 1,
]);
}
}
public function discharge_templates()
{
if ($resp = $this->requireAnyPermission(['template.discharge'])) {
return $resp;
}
$this->ensureDefaultDischargeTemplateSeeded();
$notice = '';
$noticeType = 'success';
if (strtolower($this->request->getMethod()) === 'post') {
$id = (int) ($this->request->getPost('id') ?? 0);
$templateName = trim((string) ($this->request->getPost('template_name') ?? ''));
$templateHtml = (string) ($this->request->getPost('template_html') ?? '');
$isDefault = (int) ($this->request->getPost('is_default') ?? 0) === 1 ? 1 : 0;
$status = (int) ($this->request->getPost('status') ?? 1) === 1 ? 1 : 0;
if ($templateName === '' || trim($templateHtml) === '') {
$notice = 'Template name and template HTML are required.';
$noticeType = 'danger';
} else {
$table = $this->db->table('ipd_discharge_templates');
$data = [
'template_name' => $templateName,
'template_html' => $templateHtml,
'is_default' => $isDefault,
'status' => $status,
];
if ($id > 0) {
$table->where('id', $id)->update($data);
$notice = 'Template updated.';
} else {
$table->insert($data);
$id = (int) $this->db->insertID();
$notice = 'Template created.';
}
if ($isDefault === 1 && $id > 0) {
$this->db->table('ipd_discharge_templates')
->where('id <>', $id)
->update(['is_default' => 0]);
}
}
}
$editId = (int) ($this->request->getGet('edit') ?? 0);
$editRow = [];
if ($editId > 0) {
$editRow = $this->db->table('ipd_discharge_templates')
->where('id', $editId)
->get(1)
->getRowArray() ?? [];
}
$rows = $this->db->table('ipd_discharge_templates')
->orderBy('is_default', 'DESC')
->orderBy('id', 'ASC')
->get()
->getResultArray();
return view('Setting/Template/discharge_templates', [
'rows' => $rows,
'edit_row' => $editRow,
'notice' => $notice,
'notice_type' => $noticeType,
]);
}
public function discharge_template_delete(int $id)
{
if ($resp = $this->requireAnyPermission(['template.discharge'])) {
return $resp;
}
$id = (int) $id;
if ($id > 0 && $this->db->tableExists('ipd_discharge_templates')) {
$this->db->table('ipd_discharge_templates')->where('id', $id)->delete();
}
return redirect()->to(base_url('setting/template/discharge_templates'));
}
}