db = db_connect(); helper(['common', 'form']); } public function newcase(int $pno, int $insId, int $caseType = 0) { $data['case_type'] = $caseType; $data['data_insurance'] = $this->db->table('hc_insurance') ->where('id', $insId) ->get() ->getResult(); $data['hc_insurance_card'] = $this->db->table('hc_insurance_card') ->where('p_id', $pno) ->where('insurance_id', $insId) ->orderBy('id', 'DESC') ->get() ->getResult(); $data['org_case_master'] = $this->db->table('organization_case_master') ->where('case_type', $caseType) ->where('p_id', $pno) ->where('status', 0) ->get() ->getResult(); $newCase = count($data['org_case_master']) < 1; $data['data'] = $this->db->table('patient_master') ->where('id', $pno) ->get() ->getResult(); $data['insurance_list'] = $this->db->table('hc_insurance')->get()->getResult(); if ($newCase) { return view('Invoice/Case_Form_V', $data); } return view('Invoice/Case_Form_profile_V', $data); } public function open_case(int $orgId, int $caseType = 0) { $data['org_case_master'] = $this->db->table('organization_case_master') ->where('case_type', $caseType) ->where('id', $orgId) ->get() ->getResult(); if (empty($data['org_case_master'])) { return $this->response->setStatusCode(404)->setBody('Case not found'); } $pno = (int) $data['org_case_master'][0]->p_id; $insId = (int) $data['org_case_master'][0]->insurance_id; $data['hc_insurance_card'] = $this->db->table('hc_insurance_card') ->where('p_id', $pno) ->orderBy('id', 'DESC') ->get() ->getResult(); $data['data_insurance'] = $this->db->table('hc_insurance') ->where('id', $insId) ->get() ->getResult(); $data['insurance_list'] = $this->db->table('hc_insurance')->get()->getResult(); $data['ipd_id'] = 0; $data['case_type'] = $caseType; if ($caseType === 1) { $ipd = $this->db->table('ipd_master') ->where('case_id', $orgId) ->get() ->getResult(); if (! empty($ipd)) { $data['ipd_id'] = (int) $ipd[0]->id; } } $data['data'] = $this->db->table('patient_master') ->where('id', $pno) ->get() ->getResult(); return view('Invoice/Case_Form_profile_V', $data); } public function create() { if (! $this->request->isAJAX()) { return $this->response ->setStatusCode(400) ->setJSON(['insertid' => 0, 'error_text' => 'Invalid request']); } $rules = [ 'input_mphone1' => 'required|min_length[10]|max_length[30]', 'input_name' => 'required|min_length[2]|max_length[30]', 'input_insurance_id' => 'required|min_length[2]|max_length[30]', 'input_card_holder_name' => 'required|min_length[2]|max_length[50]', ]; $validation = service('validation'); $validation->setRules($rules); if (! $validation->withRequest($this->request)->run()) { $errorText = implode("\n", $validation->getErrors()); return $this->response->setJSON([ 'insertid' => 0, 'error_text' => $errorText, ]); } $insId = (int) $this->request->getPost('Insurance_id'); $caseType = (int) $this->request->getPost('case_type'); $pId = (int) $this->request->getPost('p_id'); $insurance = $this->db->table('hc_insurance') ->where('id', $insId) ->get() ->getRow(); $insName = $insurance->ins_company_name ?? ''; $caseData = [ 'p_phone_number' => (string) $this->request->getPost('input_mphone1'), 'p_name' => (string) $this->request->getPost('input_name'), 'p_gender' => (string) $this->request->getPost('optionsRadios_gender'), 'date_registration' => str_to_MysqlDate((string) $this->request->getPost('datepicker_dob')), 'p_id' => $pId, 'case_type' => $caseType, 'insurance_card_id' => (int) $this->request->getPost('inc_card_id'), 'insurance_no' => (string) $this->request->getPost('input_insurance_id'), 'insurance_no_1' => (string) $this->request->getPost('input_insurance_no_1'), 'insurance_no_2' => (string) $this->request->getPost('input_insurance_no_2'), 'insurance_no_3' => (string) $this->request->getPost('input_insurance_no_3'), 'insurance_card_name' => (string) $this->request->getPost('input_card_holder_name'), 'insurance_id' => $insId, 'insurance_company_name' => $insName, ]; $existing = $this->db->table('organization_case_master') ->where('case_type', $caseType) ->where('p_id', $pId) ->where('status', 0) ->get() ->getResult(); $caseModel = new OrganizationCaseModel(); if (! empty($existing)) { $insertId = (int) $existing[0]->id; } else { $insertId = $caseModel->insertCase($caseData); } if ($caseType === 1 && $insertId > 0) { $ipd = $this->db->table('ipd_master') ->where('case_id', 0) ->where('ipd_status', 0) ->where('p_id', $pId) ->get() ->getResult(); if (! empty($ipd)) { $ipdId = (int) $ipd[0]->id; $this->db->table('ipd_master') ->where('id', $ipdId) ->update(['case_id' => $insertId]); $caseModel->updateCase(['ipd_id' => $ipdId], $insertId); } } return $this->response->setJSON([ 'insertid' => $insertId, 'p_id' => $pId, 'ins_id' => $insId, ]); } public function update() { if (! $this->request->isAJAX()) { return $this->response ->setStatusCode(400) ->setJSON(['update' => 0, 'error_text' => 'Invalid request']); } $rules = [ 'input_mphone1' => 'required|min_length[10]|max_length[30]', 'input_name' => 'required|min_length[2]|max_length[30]', 'input_insurance_id' => 'required|min_length[2]|max_length[30]', 'datepicker_dob' => 'required|min_length[2]|max_length[30]', 'input_card_holder_name' => 'required|min_length[2]|max_length[50]', ]; $validation = service('validation'); $validation->setRules($rules); if (! $validation->withRequest($this->request)->run()) { $errorText = implode("\n", $validation->getErrors()); return $this->response->setJSON([ 'update' => 0, 'error_text' => $errorText, ]); } $insId = (int) $this->request->getPost('Insurance_id'); $pId = (int) $this->request->getPost('patient_id'); $caseType = (int) $this->request->getPost('case_type'); $caseId = (int) $this->request->getPost('c_id'); $insurance = $this->db->table('hc_insurance') ->where('id', $insId) ->get() ->getRow(); $insName = $insurance->ins_company_name ?? ''; $orgCase = $this->db->table('organization_case_master') ->where('id', $caseId) ->get() ->getResult(); $caseData = [ 'p_phone_number' => (string) $this->request->getPost('input_mphone1'), 'p_name' => (string) $this->request->getPost('input_name'), 'p_gender' => (string) $this->request->getPost('optionsRadios_gender'), 'date_registration' => str_to_MysqlDate((string) $this->request->getPost('datepicker_dob')), 'remark' => (string) $this->request->getPost('remark'), 'insurance_no' => (string) $this->request->getPost('input_insurance_id'), 'insurance_no_1' => (string) $this->request->getPost('input_insurance_no_1'), 'insurance_no_2' => (string) $this->request->getPost('input_insurance_no_2'), 'insurance_no_3' => (string) $this->request->getPost('input_insurance_no_3'), 'insurance_card_name' => (string) $this->request->getPost('input_card_holder_name'), 'insurance_id' => $insId, 'insurance_company_name' => $insName, ]; $caseModel = new OrganizationCaseModel(); $caseModel->updateCase($caseData, $caseId); if ($caseType === 0) { $this->updateOrgTotals($caseId); } if ($caseType === 1 && ! empty($orgCase) && (int) $orgCase[0]->ipd_id === 0) { $ipd = $this->db->table('ipd_master') ->where('ipd_status', 0) ->where('case_id', 0) ->where('p_id', $pId) ->get() ->getResult(); if (! empty($ipd)) { $ipdId = (int) $ipd[0]->id; $this->db->table('ipd_master') ->where('id', $ipdId) ->update(['case_id' => $caseId]); $caseModel->updateCase(['ipd_id' => $ipdId], $caseId); } } return $this->response->setJSON([ 'update' => 1, 'showcontent' => 'Data Saved successfully', ]); } public function addPathTest(int $caseId) { $data['or_case_master'] = $this->db->table('organization_case_master') ->where('id', $caseId) ->get() ->getResult(); if (empty($data['or_case_master'])) { return $this->response->setStatusCode(404)->setBody('Case not found'); } $pId = (int) $data['or_case_master'][0]->p_id; $sql = "select *,if(gender=1,'Male','Female') as xgender from patient_master where id='" . $pId . "'"; $data['person_info'] = $this->db->query($sql)->getResult(); $this->applyAge($data['person_info'], 'age'); $data['labitemtype'] = $this->db->table('hc_item_type')->get()->getResult(); $sql = 'select id,Concat(idesc,\' : [\',amount1,\']\',if(hc_insurance_id>0,\'Ins.\',\'Cash\')) as sdesc ' . 'from v_hc_items_with_insurance where itype=1 and hc_insurance_id in (0,' . (int) $data['or_case_master'][0]->insurance_id . ')'; $data['labitem'] = $this->db->query($sql)->getResult(); $chkDraft = $this->db->table('invoice_master') ->selectCount('id', 'no_invoice') ->where('invoice_status', 0) ->where('case_id', $caseId) ->get() ->getResult(); $invoiceId = 0; if (! empty($chkDraft) && (int) ($chkDraft[0]->no_invoice ?? 0) < 1) { $insertInvoice = [ 'attach_type' => 0, 'attach_id' => $pId, 'case_id' => $caseId, 'inv_date' => str_to_MysqlDate(date('d/m/Y')), 'inv_name' => $data['person_info'][0]->p_fname ?? '', 'inv_a_code' => $data['person_info'][0]->p_code ?? '', ]; $invoiceModel = new InvoiceModel(); $invoiceId = $invoiceModel->createInvoice($insertInvoice); } else { $invoiceRow = $this->db->table('invoice_master') ->select('id') ->where('invoice_status', 0) ->where('attach_id', $pId) ->get() ->getResult(); $invoiceId = ! empty($invoiceRow) ? (int) $invoiceRow[0]->id : 0; } if ($invoiceId > 0) { $data['invoiceMaster'] = $this->db->table('invoice_master') ->where('id', $invoiceId) ->get() ->getResult(); $sql = 'select i.*,t.desc from invoice_item i join hc_item_type t on i.item_type=t.itype_id where i.inv_master_id=' . $invoiceId; $data['invoiceDetails'] = $this->db->query($sql)->getResult(); $data['invoiceGtotal'] = $this->db->table('invoice_item') ->selectSum('item_amount', 'Gtotal') ->where('inv_master_id', $invoiceId) ->get() ->getResult(); } return view('Invoice/Organization_Invoice_PathLab', $data); } public function search() { $sdata = (string) $this->request->getPost('txtsearch'); $sdata = preg_replace('/[^A-Za-z0-9]/', '', $sdata); $sql = "select c.id,c.p_id,c.case_id_code,sum(o.opd_fee_amount) as OPD_Amount,sum(i.net_amount) as Invoice_Amount, c.insurance_company_name,c.p_name,p.p_code,c.date_registration,insurance_card_name, (case c.status when 0 then 'Pending' when 1 then 'Ready for submission' when 2 then 'Submitted' when 3 then 'Payment Done' else 'Other' end) as str_status, Date_Format(c.date_registration,'%d-%m-%Y') as date_registration_in from (organization_case_master c join patient_master p on c.p_id=p.id ) left join opd_master o on c.id=o.insurance_case_id left join invoice_master i on c.id=i.insurance_case_id group by c.id "; if ($sdata !== '') { $sql .= " WHERE case_id_code = '" . $sdata . "' or p_name like '%" . $sdata . "%' or p_code = '" . $sdata . " order by c.id desc limit 20"; } else { $sql .= ' order by c.id desc limit 20'; } $data['searchdata'] = $this->db->query($sql)->getResult(); return view('Invoice/Case_List', $data); } public function search_all() { return view('Invoice/case_list_all'); } public function case_invoice(string $casecode, int $print = 0, int $caseid = 0, int $medInclude = 1) { if ($caseid < 1) { $sql = "select * from organization_case_master where case_id_code='" . $casecode . "'"; } else { $sql = "select * from organization_case_master where id='" . $caseid . "'"; } $data['orgcase'] = $this->db->query($sql)->getResult(); if (empty($data['orgcase'])) { return $this->response->setStatusCode(404)->setBody('Case not found'); } $pno = (int) $data['orgcase'][0]->p_id; $caseid = (int) $data['orgcase'][0]->id; $this->updateOrgTotals($caseid); $sql = "select *,if(gender=1,'Male','Female') as xgender from patient_master where id='" . $pno . "'"; $data['person_info'] = $this->db->query($sql)->getResult(); $this->applyAge($data['person_info'], 'age'); $insCompId = (int) $data['orgcase'][0]->insurance_id; $incCardId = (int) $data['orgcase'][0]->insurance_card_id; $data['hc_insurance_card'] = $this->db->table('hc_insurance_card') ->where('id', $incCardId) ->get() ->getResult(); $data['insurance'] = $this->db->table('hc_insurance') ->where('id', $insCompId) ->get() ->getResult(); $invoiceRows = $this->getOrganizationCaseInvoiceRows($caseid); $data['showinvoice1'] = $invoiceRows['showinvoice1']; $data['showinvoice2'] = $invoiceRows['showinvoice2']; $sql = 'select m.*,m.id as med_id from invoice_med_master m join ipd_master p join organization_case_master o on m.ipd_id=p.id and p.case_id=o.id and m.ipd_credit=1 and m.ipd_credit_type=1 where o.id=' . $caseid; $data['MedInvoice_data'] = $this->db->query($sql)->getResult(); $sql = 'select sum(m.net_amount) as Med_Amount,o.id ,count(m.id) as no_inv from invoice_med_master m join ipd_master p join organization_case_master o on m.ipd_id=p.id and p.case_id=o.id and m.ipd_credit=1 and m.ipd_credit_type=1 where o.id=' . $caseid . ' group by o.id'; $data['MedInvoice'] = $this->db->query($sql)->getResult(); if (count($data['MedInvoice']) === 0) { $sql = 'select m.* ,m.id as med_id from invoice_med_master m join organization_case_master o on m.case_id=o.id and m.case_credit=1 where o.id=' . $caseid; $data['MedInvoice_data'] = $this->db->query($sql)->getResult(); $sql = 'select sum(m.net_amount) as Med_Amount,o.id ,count(m.id) as no_inv from invoice_med_master m join organization_case_master o on m.case_id=o.id and m.case_credit=1 where o.id=' . $caseid . ' group by o.id'; $data['MedInvoice'] = $this->db->query($sql)->getResult(); } $data['med_include'] = $medInclude; $sql = "select p.* ,date_format(payment_date,'%d-%m-%Y') as str_date, if(credit_debit=0,if(payment_mode=1,'CASH','BANK'),if(payment_mode=1,'CASH RETURN','BANK RETURN')) as Pay_mode from payment_history p where p.payof_type=3 and p.payof_id=" . $caseid; $data['payment_history'] = $this->db->query($sql)->getResult(); if ($print > 0) { $html = view('Invoice/caseinvoice_print_V', $data); $caseCode = (string) ($data['orgcase'][0]->case_id_code ?? $casecode); $fileName = 'Org_Invoice_' . preg_replace('/[^A-Za-z0-9\-_]/', '_', $caseCode) . '_' . date('YmdHis') . '.pdf'; return $this->renderInlinePdf($html, $fileName, 'Organization Invoice ' . $caseCode); } return view('Invoice/caseinvoice_V', $data); } public function case_invoice_ipd(int $caseid) { $data['orgcase'] = $this->db->table('organization_case_master') ->where('id', $caseid) ->get() ->getResult(); if (empty($data['orgcase'])) { return $this->response->setStatusCode(404)->setBody('Case not found'); } $pno = (int) $data['orgcase'][0]->p_id; $caseid = (int) $data['orgcase'][0]->id; $this->updateOrgTotals($caseid); $sql = "select *,if(gender=1,'Male','Female') as xgender from patient_master where id='" . $pno . "'"; $data['person_info'] = $this->db->query($sql)->getResult(); $this->applyAge($data['person_info'], 'age'); $insCompId = (int) $data['orgcase'][0]->insurance_id; $incCardId = (int) $data['orgcase'][0]->insurance_card_id; $data['hc_insurance_card'] = $this->db->table('hc_insurance_card') ->where('id', $incCardId) ->get() ->getResult(); $data['insurance'] = $this->db->table('hc_insurance') ->where('id', $insCompId) ->get() ->getResult(); $invoiceRows = $this->getOrganizationCaseInvoiceRows($caseid); $data['showinvoice1'] = $invoiceRows['showinvoice1']; $data['showinvoice2'] = $invoiceRows['showinvoice2']; return view('Invoice/caseinvoice_ipd_V', $data); } public function ORG_info() { $orgNo = (string) $this->request->getPost('input_org_no'); $sql = "select * from organization_case_master where case_type=0 and case_id_code='" . $orgNo . "' "; $dataOrg = $this->db->query($sql)->getResult(); $orgId = 0; $orgInfo = ''; if (! empty($dataOrg)) { $uhid = (int) $dataOrg[0]->p_id; $sql = "select * from patient_master where id='" . $uhid . "' "; $dataPerson = $this->db->query($sql)->getResult(); $patientInfo = $dataPerson[0]->p_code . '/' . $dataPerson[0]->p_fname . ' ' . $dataPerson[0]->p_relative . ' ' . $dataPerson[0]->p_rname . ' /Age :' . ($dataPerson[0]->str_age ?? ''); $orgInfo = $patientInfo . '/' . $dataOrg[0]->insurance_company_name . '/Reg.:' . $dataOrg[0]->insurance_no_1 . '/Reg.Dt:' . $dataOrg[0]->date_registration; $orgId = (int) $dataOrg[0]->id; } return $this->response->setJSON([ 'org_id' => $orgId, 'org_info' => $orgInfo, ]); } /** * Old CI3-compatible invoice fetch: OPD + invoice item rows via UNION ALL. * Returns rows split for existing views that still expect showinvoice1/showinvoice2. */ private function getOrganizationCaseInvoiceRows(int $caseid): array { $sql = " select * from ( select c.id AS id,c.case_id_code AS case_id_code,c.p_id AS p_id, o.apointment_date AS s_Date,'OPD' AS Charge_type,0 AS Charge_type_id, o.opd_id AS item_id,0 AS master_item_id,o.apointment_date AS Adate, o.opd_fee_amount AS item_rate,1 AS item_qty, date_format(o.apointment_date,'%d-%m-%Y') AS str_date, concat('OPD Charge: Dr. ',o.doc_name) AS Description, o.opd_code AS Code,o.opd_fee_amount AS Amount, '1' AS orgcode,0 AS discount_amount,null AS d_rate from opd_master o join organization_case_master c on c.id={$caseid} where o.opd_status in (1,2) and o.insurance_case_id = c.id union all select c.id AS id,c.case_id_code AS case_id_code,c.p_id AS p_id, i.inv_date AS s_Date,l.group_desc AS Charge_type,l.itype_id AS Charge_type_id, t.id AS item_id,t.item_id AS master_item_id,i.inv_date AS Adate, t.item_rate AS item_rate,t.item_qty AS item_qty, date_format(i.inv_date,'%d-%m-%Y') AS str_date, concat(t.item_name) AS Description,i.invoice_code AS Code, t.item_amount AS Amount,t.org_code AS orgcode, case when it.amount1 is not null and it.amount1 <> t.item_rate then 1 else 0 end AS discount_amount, it.amount1 AS d_rate from invoice_master i join organization_case_master c on c.id={$caseid} join invoice_item t on t.inv_master_id = i.id join hc_item_type l on t.item_type = l.itype_id left join ( select hc_items_id, hc_insurance_id, amount1 from hc_items_insurance group by hc_items_id, hc_insurance_id ) it on t.item_id = it.hc_items_id and i.insurance_id = it.hc_insurance_id where i.ipd_include = 1 and i.invoice_status = 1 and i.insurance_case_id = c.id ) v order by v.Charge_type, v.Adate "; $rows = $this->db->query($sql)->getResult(); $showinvoice1 = []; $showinvoice2 = []; foreach ($rows as $row) { if ((int) ($row->Charge_type_id ?? 0) === 0) { $showinvoice1[] = $row; continue; } $showinvoice2[] = $row; } return [ 'showinvoice1' => $showinvoice1, 'showinvoice2' => $showinvoice2, ]; } private function updateOrgTotals(int $caseId): void { $caseRow = $this->db->table('organization_case_master') ->select('case_type,p_id,case_id_code') ->where('id', $caseId) ->get() ->getRow(); if (! $caseRow || (int) $caseRow->case_type !== 0) { return; } $opdFeeRow = $this->db->table('opd_master') ->selectSum('opd_fee_amount', 'total') ->whereIn('opd_status', [1, 2]) ->where('insurance_case_id', $caseId) ->get() ->getRow(); $opdFee = (float) ($opdFeeRow->total ?? 0); $medRow = $this->db->table('invoice_med_master') ->selectSum('net_amount', 'total') ->where('case_id', $caseId) ->get() ->getRow(); $medTotal = (float) ($medRow->total ?? 0); $chargeRow = $this->db->table('invoice_master') ->selectSum('net_amount', 'total') ->where('invoice_status', 1) ->where('insurance_case_id', $caseId) ->get() ->getRow(); $chargeTotal = (float) ($chargeRow->total ?? 0); $this->db->table('organization_case_master') ->where('id', $caseId) ->update([ 'inv_opd_amt' => $opdFee, 'inv_opd_charge_amt' => $chargeTotal, 'inv_opd_med_amt' => $medTotal, ]); } public function update_itemrateqty() { $itemTypeId = (int) $this->request->getPost('item_type_id'); $itemId = (int) $this->request->getPost('item_id'); $rate = (float) $this->request->getPost('rate'); $orgcode = (string) $this->request->getPost('orgcode'); $insuranceId = (int) $this->request->getPost('insurance_id'); $masterItemId = (int) $this->request->getPost('master_item_id'); $invoiceItem = $this->db->table('invoice_item') ->where('id', $itemId) ->get() ->getResult(); $qty = 0; if (! empty($invoiceItem)) { $qty = (float) $invoiceItem[0]->item_qty; } $amountValue = $qty * $rate; $invoiceModel = new InvoiceModel(); $invoiceModel->updateItem([ 'item_qty' => $qty, 'item_rate' => $rate, 'item_amount' => $amountValue, 'org_code' => $orgcode, ], $itemId); if ($insuranceId === 2) { $existing = $this->db->table('hc_items_insurance') ->where('hc_items_id', $masterItemId) ->where('hc_insurance_id', $insuranceId) ->get() ->getResult(); $insData = [ 'hc_items_id' => $masterItemId, 'hc_insurance_id' => $insuranceId, 'amount1' => $amountValue, 'code' => $orgcode, ]; if (! empty($existing)) { $this->db->table('hc_items_insurance') ->where('id', $existing[0]->id) ->update($insData); } else { $this->db->table('hc_items_insurance')->insert($insData); } } $invoiceId = 0; if (! empty($invoiceItem)) { $invoiceId = (int) $invoiceItem[0]->inv_master_id; } $data['invoiceDetails'] = $this->db->query('select i.*,t.group_desc from invoice_item i join hc_item_type t on i.item_type=t.itype_id where i.inv_master_id=' . $invoiceId)->getResult(); $data['invoiceGtotal'] = $this->db->table('invoice_item')->selectSum('item_amount', 'Gtotal')->where('inv_master_id', $invoiceId)->get()->getResult(); $data['invoice_master'] = $this->db->query("select *,(case payment_mode when 1 Then 'Cash' when 2 then 'Bank Card' when 3 then 'IPD Credit' when 4 then 'Org. Credit' else 'Pending' end) as Payment_type_str from invoice_master where id=" . $invoiceId)->getResult(); $discountAmount = (float) ($data['invoice_master'][0]->discount_amount ?? 0); $correctionAmount = (float) ($data['invoice_master'][0]->correction_amount ?? 0); $totalAmount = (float) ($data['invoiceGtotal'][0]->Gtotal ?? 0); $netAmount = $totalAmount - $discountAmount - $correctionAmount; $invoiceModel->updateInvoice([ 'total_amount' => $totalAmount, 'net_amount' => $netAmount, ], $invoiceId); } public function update_status() { if (! $this->request->isAJAX()) { return $this->response->setStatusCode(400)->setBody('Invalid request'); } $caseId = (int) $this->request->getPost('caseid'); $status = (int) $this->request->getPost('status'); $user = auth()->user(); $userId = $user->id ?? ''; $userName = $user->username ?? $user->email ?? 'User'; $caseMaster = $this->db->table('organization_case_master') ->where('id', $caseId) ->get() ->getResult(); $log = ''; if (! empty($caseMaster)) { $log = (string) ($caseMaster[0]->log ?? ''); } $log .= '\nUpdate Status:' . $status . '-Upd By:' . $userName . '[' . $userId . ']' . date('d/m/Y H:i:s'); $data = [ 'status' => $status, 'log' => $log, ]; if ($status === 1) { $orgSubmitDate = str_to_MysqlDate((string) $this->request->getPost('org_submit_date')); $data['org_submit_date'] = $orgSubmitDate; } $caseModel = new OrganizationCaseModel(); $caseModel->updateCase($data, $caseId); } public function org_contingent_update() { if (! $this->request->isAJAX()) { return $this->response->setStatusCode(400)->setBody('Invalid request'); } $user = auth()->user(); $userName = $user->username ?? $user->email ?? 'User'; $htmlData = (string) $this->request->getPost('HTMLData'); $caseId = (int) $this->request->getPost('case_id'); $caseModel = new OrganizationCaseModel(); $caseModel->updateCase([ 'contingent_bill' => $htmlData, 'contingent_update_by' => $userName, ], $caseId); return $this->response->setJSON([ 'update' => 1, 'msg' => 'Update Done ' . $caseId, ]); } public function case_invoice_print(int $caseid) { $data['orgcase'] = $this->db->table('organization_case_master') ->where('id', $caseid) ->get() ->getResult(); if (empty($data['orgcase'])) { return $this->response->setStatusCode(404)->setBody('Case not found'); } $pno = (int) $data['orgcase'][0]->p_id; $sql = "select *,if(gender=1,'Male','Female') as xgender from patient_master where id='" . $pno . "'"; $data['person_info'] = $this->db->query($sql)->getResult(); $this->applyAge($data['person_info'], 'age'); $insCompId = (int) $data['orgcase'][0]->insurance_id; $incCardId = (int) $data['orgcase'][0]->insurance_card_id; $data['hc_insurance_card'] = $this->db->table('hc_insurance_card') ->where('id', $incCardId) ->get() ->getResult(); $data['insurance'] = $this->db->table('hc_insurance') ->where('id', $insCompId) ->get() ->getResult(); $organizationCaseModel = new OrganizationCaseModel(); $invoiceData = $organizationCaseModel->getInvoiceRowsAndTotal($caseid); $data['showinvoice'] = $invoiceData['rows']; $data['invoiceGtotal'] = [(object) ['GTotal' => $invoiceData['total']]]; return view('Invoice/caseinvoice_V', $data); } public function getCaseTable() { $requestData = $_REQUEST; $columns = [ 0 => 'case_id_code', 1 => 'Reg_No', 2 => 'p_f_info', 3 => 'date_registration', 4 => 'insurance_card_name', 5 => 'insurance_company_name', 6 => 'str_status', ]; $sqlAll = "select c.id,c.p_id,c.case_id_code,Concat(c.insurance_no,'/',c.insurance_no_1) as Reg_No, c.insurance_company_name,Concat(c.p_name,'
','[',p_code,if(i.id is null,'',concat('/',i.ipd_code)),']') as p_f_info,p.p_code, c.date_registration,c.insurance_card_name,c.status, (case c.status when 0 then 'Pending' when 1 then 'Ready for submission' when 2 then 'submitted' else 'Other' end) as str_status,c.insurance_id, Date_Format(c.date_registration,'%d-%m-%Y') as date_registration_in ,i.ipd_code "; $sqlCount = 'Select count(*) as no_rec '; $sqlFrom = ' from ((organization_case_master c join patient_master p on c.p_id=p.id ) left join ipd_master i on c.id=i.case_id and p.id=i.p_id) '; $totalSql = $sqlCount . $sqlFrom; $totalData = (int) ($this->db->query($totalSql)->getResult()[0]->no_rec ?? 0); $totalFiltered = $totalData; $sqlWhere = ' WHERE c.case_type=0 '; if (! empty($requestData['columns'][0]['search']['value'])) { $sqlWhere .= " AND case_id_code LIKE '%" . $requestData['columns'][0]['search']['value'] . "' "; } if (! empty($requestData['columns'][1]['search']['value'])) { $term = $requestData['columns'][1]['search']['value']; $sqlWhere .= " AND ( p.p_fname LIKE '%" . $term . "%' "; $sqlWhere .= " OR p.mphone1 LIKE '" . $term . "' "; $sqlWhere .= " OR i.ipd_code LIKE '%" . $term . "' "; $sqlWhere .= " OR p_code LIKE '%" . $term . "%' )"; } if (! empty($requestData['columns'][2]['search']['value'])) { $sqlWhere .= " AND c.insurance_card_name LIKE '%" . $requestData['columns'][2]['search']['value'] . "%' "; } if (! empty($requestData['columns'][3]['search']['value'])) { if ($requestData['columns'][3]['search']['value'] < 0) { $sqlWhere .= ' AND c.insurance_id not in (1,2,53,63) '; } if ($requestData['columns'][3]['search']['value'] > 0) { $sqlWhere .= " AND c.insurance_id = '" . $requestData['columns'][3]['search']['value'] . "' "; } } if (! empty($requestData['columns'][4]['search']['value'])) { $sqlWhere .= " AND ( c.insurance_no LIKE '" . $requestData['columns'][4]['search']['value'] . "%' "; $sqlWhere .= " OR c.insurance_no_1 LIKE '" . $requestData['columns'][4]['search']['value'] . "%' ) "; } if (! empty($requestData['columns'][5]['search']['value'])) { $sqlWhere .= " AND c.status = '" . $requestData['columns'][5]['search']['value'] . "' "; } $totalFilterSql = $sqlCount . $sqlFrom . $sqlWhere; $totalFiltered = (int) ($this->db->query($totalFilterSql)->getResult()[0]->no_rec ?? 0); $orderSql = ' group by c.id ORDER BY ' . $columns[$requestData['order'][0]['column']] . ' ' . $requestData['order'][0]['dir'] . ' LIMIT ' . $requestData['start'] . ' ,' . $requestData['length']; $resultSql = $sqlAll . $sqlFrom . $sqlWhere . $orderSql; $rdata = $this->db->query($resultSql)->getResultArray(); $output = [ 'draw' => (int) $requestData['draw'], 'recordsTotal' => $totalData, 'recordsFiltered' => $totalFiltered, 'data' => [], 'sql' => $resultSql, ]; foreach ($rdata as $aRow) { $row = []; foreach ($columns as $col) { $row[] = $aRow[$col]; } $output['data'][] = $row; } return $this->response->setJSON($output); } public function contingent_bill(int $caseid, int $print = 0) { $data['orgcase'] = $this->db->table('organization_case_master') ->where('id', $caseid) ->get() ->getResult(); if (empty($data['orgcase'])) { return $this->response->setStatusCode(404)->setBody('Case not found'); } $pno = (int) $data['orgcase'][0]->p_id; $sql = "select *,if(gender=1,'Male','Female') as xgender from patient_master where id='" . $pno . "'"; $data['person_info'] = $this->db->query($sql)->getResult(); $this->applyAge($data['person_info'], 'age'); $insCompId = (int) $data['orgcase'][0]->insurance_id; $incCardId = (int) $data['orgcase'][0]->insurance_card_id; $data['hc_insurance_card'] = $this->db->table('hc_insurance_card') ->where('id', $incCardId) ->get() ->getResult(); $data['insurance'] = $this->db->table('hc_insurance') ->where('id', $insCompId) ->get() ->getResult(); if ($print === 1) { $content = (string) ($data['orgcase'][0]->contingent_bill ?? ''); $pageHead = ''; $html = $pageHead . $content; $caseCode = (string) ($data['orgcase'][0]->case_id_code ?? $caseid); $fileName = 'Contingent_Bill_' . preg_replace('/[^A-Za-z0-9\-_]/', '_', $caseCode) . '_' . date('YmdHis') . '.pdf'; return $this->renderInlinePdf($html, $fileName, 'Contingent Bill ' . $caseCode); } return view('Invoice/Org_Contingent_bill', $data); } public function create_contingent_bill(int $caseid) { $data['orgcase'] = $this->db->table('organization_case_master') ->where('id', $caseid) ->get() ->getResult(); if (empty($data['orgcase'])) { return $this->response->setStatusCode(404)->setBody('Case not found'); } $pno = (int) $data['orgcase'][0]->p_id; $sql = "select *,if(gender=1,'Male','Female') as xgender from patient_master where id='" . $pno . "'"; $data['person_info'] = $this->db->query($sql)->getResult(); $this->applyAge($data['person_info'], 'age'); $insCompId = (int) $data['orgcase'][0]->insurance_id; $incCardId = (int) $data['orgcase'][0]->insurance_card_id; $data['hc_insurance_card'] = $this->db->table('hc_insurance_card') ->where('id', $incCardId) ->get() ->getResult(); $data['insurance'] = $this->db->table('hc_insurance') ->where('id', $insCompId) ->get() ->getResult(); $organizationCaseModel = new OrganizationCaseModel(); $invoiceData = $organizationCaseModel->getInvoiceRowsAndTotal($caseid); $data['showinvoice'] = $invoiceData['rows']; $data['invoiceGtotal'] = [(object) ['GTotal' => $invoiceData['total']]]; $claimId = (string) ($data['orgcase'][0]->insurance_no_1 ?? ''); $content = ''; $content .= '

CLAIM ID : ' . $claimId . '

'; $content .= '

Name of Hospital : ' . H_Name . '

'; $content .= '

CONTINGENT BILL

'; $content .= ''; $content .= ''; $content .= ''; $content .= ''; $content .= ''; $content .= '
Voucher No. ______________________________Date : ………………………
Amount of allotment : Rs.……………………………
Amount expended and for which bills have already been submitted for payment :Rs.……………………………
Balance of allotment excliding the amount of this bills :Rs.……………………………
'; $content .= '

Expenditure an account of : Treatment/ Super Treatment/ Special investigation / Consultant fee

'; $content .= ''; $content .= ''; $content .= ''; $content .= ''; $content .= ''; $content .= ''; $content .= ''; $content .= ''; $content .= ''; $content .= ''; $content .= ''; $content .= '
In respect of
Name of Patient: ' . strtoupper((string) $data['orgcase'][0]->p_name) . '
Relation : ' . strtoupper((string) $data['orgcase'][0]->relation_with_cardholder) . 'Service No.: ' . strtoupper((string) $data['orgcase'][0]->insurance_no_2) . '
ESM Name : ' . strtoupper((string) $data['orgcase'][0]->insurance_card_name) . 'ECHS CARD No. : ' . strtoupper((string) $data['orgcase'][0]->insurance_no) . 'Rank : ' . strtoupper((string) $data['orgcase'][0]->card_remark) . '
'; $content .= '

Authority:
'; $content .= 'Government of India letter No. 24 (3)/03/US/(WE)/D (Res) (i) dated 08 Sep 2003
'; $content .= 'Government of India letter No. 24 (8)/03/US/(WE)/D (Res) dated 19 Dec 2003
'; $content .= 'Central Organization ECHS Letter NoB/49773/AG/ECHS dated 25 May 2004.

'; $opd = $this->db->query('select c.id,c.case_id_code,c.p_id,sum(o.opd_fee_amount) as tamount, count(o.opd_id) as item_qty,Min(o.opd_fee_amount) as rate from opd_master o join organization_case_master c on (o.insurance_case_id = cast(c.id as char) or o.insurance_case_id = c.case_id_code) where opd_status in (1,2) and id=' . $caseid . ' group by c.id')->getResult(); $data['invoice_list_1'] = $this->db->query("select i.item_type,t.`desc` AS item_name,sum(i.item_amount) as tamount,sum(i.item_qty) as unit,i.item_rate from invoice_master m join invoice_item i join hc_item_type t on m.id=i.inv_master_id and i.item_type=t.itype_id where item_type=34 and m.ipd_include=1 and invoice_status=1 and insurance_case_id=$caseid group by i.item_type")->getResult(); $data['invoice_list_2'] = $this->db->query("select i.item_type,t.`desc` AS item_name,sum(i.item_amount) as tamount,sum(i.item_qty) as unit,i.item_rate from invoice_master m join invoice_item i join hc_item_type t on m.id=i.inv_master_id and i.item_type=t.itype_id where item_type not in (34,35) and m.ipd_include=1 and invoice_status=1 and insurance_case_id=$caseid group by i.item_type")->getResult(); $data['invoice_list_3'] = $this->db->query("select i.item_type,t.`desc` AS item_name,sum(i.item_amount) as tamount,sum(i.item_qty) as unit,i.item_rate from invoice_master m join invoice_item i join hc_item_type t on m.id=i.inv_master_id and i.item_type=t.itype_id where item_type=35 and m.ipd_include=1 and invoice_status=1 and insurance_case_id=$caseid group by i.item_type")->getResult(); $data['ipd_invoice_list_1'] = $this->db->query("select i.item_type,i.item_name,t.group_desc,sum(i.item_amount) as tamount,sum(i.item_qty) as unit, i.item_rate FROM (ipd_master m join ipd_invoice_item i on m.id=i.ipd_id) join ipd_item_type t ON i.item_type=t.itype_id and i.item_type=t.itype_id WHERE i.package_id=0 and t.itype_id=2 AND m.case_id=$caseid group by i.item_type")->getResult(); $data['ipd_invoice_list_2'] = $this->db->query("select i.item_type,i.item_name,t.group_desc,sum(i.item_amount) as tamount,sum(i.item_qty) as unit, i.item_rate FROM (ipd_master m join ipd_invoice_item i on m.id=i.ipd_id) join ipd_item_type t ON i.item_type=t.itype_id and i.item_type=t.itype_id WHERE i.package_id=0 and t.itype_id not in (2,6) AND m.case_id=$caseid group by i.item_type")->getResult(); $data['ipd_invoice_list_3'] = $this->db->query("select i.item_type,i.item_name,t.group_desc,sum(i.item_amount) as tamount,sum(i.item_qty) as unit, i.item_rate FROM (ipd_master m join ipd_invoice_item i on m.id=i.ipd_id) join ipd_item_type t ON i.item_type=t.itype_id and i.item_type=t.itype_id WHERE i.package_id=0 and t.itype_id=6 AND m.case_id=$caseid group by i.item_type")->getResult(); $data['MedInvoice'] = $this->db->query('select sum(m.net_amount) as Med_Amount,o.id ,count(m.id) as no_inv from invoice_med_master m join ipd_master p join organization_case_master o on m.ipd_id=p.id and p.case_id=o.id and m.ipd_credit=1 and m.ipd_credit_type=1 where o.id=' . $caseid . ' group by o.id')->getResult(); $data['ipd_package'] = $this->db->query('SELECT p.* FROM ipd_package p JOIN ipd_master m ON p.ipd_id=m.id WHERE m.case_id=' . $caseid)->getResult(); if (count($data['MedInvoice']) === 0) { $data['MedInvoice'] = $this->db->query('select sum(m.net_amount) as Med_Amount,o.id ,count(m.id) as no_inv from invoice_med_master m join organization_case_master o on m.case_id=o.id and m.case_credit=1 where o.id=' . $caseid . ' group by o.id')->getResult(); } $ipdMaster = $this->db->query('select p.* from ipd_master p join organization_case_master o on p.case_id=o.id where o.id=' . $caseid)->getResult(); $tableHead = '' . ''; $tbody = ''; $srno = 0; $totalGrossBill = 0.00; if (! empty($opd)) { $srno++; $tbody .= ''; $tbody .= ''; $tbody .= ''; $tbody .= ''; $tbody .= ''; $totalGrossBill += $opd[0]->tamount; } foreach ($data['ipd_package'] as $row) { $srno++; $tbody .= ''; $tbody .= ''; $tbody .= ''; $tbody .= ''; $tbody .= ''; $totalGrossBill += $row->package_Amount; } $lists = ['invoice_list_1', 'ipd_invoice_list_1', 'invoice_list_2', 'ipd_invoice_list_2', 'invoice_list_3', 'ipd_invoice_list_3']; foreach ($lists as $listKey) { foreach ($data[$listKey] as $row) { if ($row->item_type !== '') { $srno++; $tbody .= ''; $tbody .= ''; $tbody .= ''; $tbody .= ''; $tbody .= ''; $totalGrossBill += $row->tamount; } } } if (! empty($data['MedInvoice'])) { $srno++; $tbody .= ''; $tbody .= ''; $tbody .= ''; $tbody .= ''; $tbody .= ''; $totalGrossBill += $data['MedInvoice'][0]->Med_Amount; } $tbody .= ''; if (! empty($ipdMaster)) { if ((float) $ipdMaster[0]->chargeamount1 > 0) { $tbody .= ''; $totalGrossBill += (float) $ipdMaster[0]->chargeamount1; } if ((float) $ipdMaster[0]->chargeamount2 > 0) { $tbody .= ''; $totalGrossBill += (float) $ipdMaster[0]->chargeamount2; } if ((float) $ipdMaster[0]->Discount > 0) { $tbody .= ''; $totalGrossBill -= (float) $ipdMaster[0]->Discount; } if ((float) $ipdMaster[0]->Discount2 > 0) { $tbody .= ''; $totalGrossBill -= (float) $ipdMaster[0]->Discount2; } if ((float) $ipdMaster[0]->Discount3 > 0) { $tbody .= ''; $totalGrossBill -= (float) $ipdMaster[0]->Discount3; } } $tableFooter = ''; $tableFooter .= '
Sr.No.DateDetails of ExpenditureNumber or QuantityRatePerAmount
' . $srno . '' . date('Y-m-d') . 'OPD' . $opd[0]->rate . '' . $opd[0]->item_qty . '' . $opd[0]->rate . 'CGHS' . $opd[0]->tamount . '
' . $srno . '' . date('Y-m-d') . 'Package :' . $row->package_name . '' . $row->package_Amount . '1' . $row->package_Amount . 'CGHS' . $row->package_Amount . '
' . $srno . '' . date('Y-m-d') . '' . $row->item_name . '' . $row->item_rate . '' . $row->unit . '' . $row->item_rate . 'CGHS' . $row->tamount . '
' . $srno . '' . date('Y-m-d') . 'Medicine' . $data['MedInvoice'][0]->Med_Amount . '' . $data['MedInvoice'][0]->no_inv . '' . $data['MedInvoice'][0]->Med_Amount . '' . $data['MedInvoice'][0]->Med_Amount . '
Gross Total ' . round($totalGrossBill, 0) . '
' . $ipdMaster[0]->charge1 . '' . $ipdMaster[0]->chargeamount1 . '
' . $ipdMaster[0]->charge2 . '' . $ipdMaster[0]->chargeamount2 . '
' . $ipdMaster[0]->Discount_Remark . '' . $ipdMaster[0]->Discount . '
' . $ipdMaster[0]->Discount_Remark2 . '' . $ipdMaster[0]->Discount3 . '
' . $ipdMaster[0]->Discount_Remark3 . '' . $ipdMaster[0]->Discount3 . '
Net Total ' . round($totalGrossBill, 0) . '
'; $content .= $tableHead . $tbody . $tableFooter; $content .= '

Net Amount due(In words) : Rs. ' . number_to_word(round($totalGrossBill, 0)) . '

'; $content .= ''; $content .= '
Date : ......................Pre Received Payment
From : ...........................................
'; $content .= '

Certified that
'; $content .= '

  1. Certified that above have been claimed on the basis of approved charges as noted in MOA signed for the treatment of ECHS member.
  2. '; $content .= '
  3. The rate is/are fair and reasonable and less than CGHS rates
  4. '; $content .= '
  5. The expenditure incurred is debatable to major Head 2076 Minor head 107, Sub Head E-Medical treatment related expenditure code head 365/00.
  6. '; $content .= '
  7. The expenditure has been incurred the interest of Ex-Serviceman

'; $content .= ''; $content .= '
Vetted and Recommended / Not RecommendedSig of OIC ECHS Polyclinic

Date : ......................

Place: ......................
.....................
'; $caseModel = new OrganizationCaseModel(); $caseModel->updateCase(['contingent_bill' => $content], $caseid); return $this->response->setBody('Created'); } public function org_confirm_payment() { $amountR = (float) $this->request->getPost('amount_r'); $amountD = (float) $this->request->getPost('amount_d'); $amountClaim = (float) $this->request->getPost('amount_claim'); $amountTds = (float) $this->request->getPost('amount_tds'); $datePayment = str_to_MysqlDate((string) $this->request->getPost('date_payment')); $payInfo = (string) $this->request->getPost('pay_info'); $orgId = (int) $this->request->getPost('org_id'); $user = auth()->user(); $userId = $user->id ?? ''; $userName = $user->username ?? $user->email ?? 'User'; $updateBy = $userName . '[' . $userId . ']' . date('d-m-Y H:i:s'); $caseModel = new OrganizationCaseModel(); $caseModel->updateCase([ 'pay_date_received' => $datePayment, 'amount_recived' => $amountR, 'amount_deduction' => $amountD, 'amount_claim' => $amountClaim, 'amount_tds' => $amountTds, 'amount_payment_info' => $payInfo, 'status' => '3', 'amount_update_by' => $updateBy, ], $orgId); return $this->response->setJSON([ 'update' => 1, 'showcontent' => 'Update', ]); } public function org_payment_request() { $data['org_info'] = $this->db->table('org_payment_request')->get()->getResult(); if (empty($data['org_info'])) { return $this->response->setStatusCode(404)->setBody('No payment requests'); } $orgId = (int) $data['org_info'][0]->id; $data['ipd_info'] = $this->db->query("select * from v_ipd_list where org_id='" . $orgId . "' ")->getResult(); $sql = "select *,if(gender=1,'Male','Female') as xgender from patient_master where id='" . $data['org_info'][0]->p_id . "' "; $data['person_info'] = $this->db->query($sql)->getResult(); $this->applyAge($data['person_info'], 'str_age'); return view('Invoice/org_case_payment', $data); } public function load_model_box(string $caseidcode) { $data['org_info'] = $this->db->query("select * from organization_case_master where case_id_code='" . $caseidcode . "' ")->getResult(); if (empty($data['org_info'])) { return $this->response->setStatusCode(404)->setBody('Case not found'); } $orgId = (int) $data['org_info'][0]->id; $data['ipd_info'] = $this->db->query("select * from v_ipd_list where org_id='" . $orgId . "' ")->getResult(); $sql = "select *,if(gender=1,'Male','Female') as xgender from patient_master where id='" . $data['org_info'][0]->p_id . "' "; $data['person_info'] = $this->db->query($sql)->getResult(); $this->applyAge($data['person_info'], 'str_age'); return view('Invoice/org_case_payment', $data); } public function payment_request() { $inputPayAmount = (float) $this->request->getPost('input_pay_amount'); $caseidcode = (int) $this->request->getPost('caseid'); $inputRemark = (string) $this->request->getPost('input_remark'); $user = auth()->user(); $userId = $user->id ?? ''; $userName = $user->username ?? $user->email ?? 'User'; $userLabel = $userName . '[' . $userId . ']' . date('d-m-Y H:i:s'); $orgInfo = $this->db->table('organization_case_master') ->where('id', $caseidcode) ->get() ->getResult(); $orgPaymentRequest = $this->db->table('org_payment_request') ->where('payment_process', 0) ->where('org_id', $caseidcode) ->get() ->getResult(); $patientId = 0; $orgDate = date('Y-m-d H:i:s'); $patientName = ''; $orgCode = ''; if (! empty($orgInfo)) { $patientId = (int) $orgInfo[0]->p_id; $personInfo = $this->db->query("select *,if(gender=1,'Male','Female') as xgender from patient_master where id='" . $patientId . "' ")->getResult(); $this->applyAge($personInfo, 'str_age'); $orgDate = $orgInfo[0]->insert_datetime; $patientName = $personInfo[0]->p_fname ?? ''; $orgCode = $orgInfo[0]->case_id_code ?? ''; } $payData = [ 'org_id' => $caseidcode, 'org_code' => $orgCode, 'patient_id' => $patientId, 'org_date' => $orgDate, 'patient_name' => $patientName, 'payment_amount' => $inputPayAmount, 'payment_request_datetime' => date('Y-m-d H:i:s'), 'request_by_id' => $userId, 'request_by' => $userLabel, 'remark' => $inputRemark, ]; if (count($orgPaymentRequest) < 1) { $this->db->table('org_payment_request')->insert($payData); $insertId = (int) $this->db->insertID(); } else { $this->db->table('org_payment_request') ->where('id', $orgPaymentRequest[0]->id) ->update($payData); $insertId = (int) $orgPaymentRequest[0]->id; } return $this->response->setBody('Request has been Created : Req. ID -> ' . $insertId); } public function refund_request() { $inputRefundAmount = (float) $this->request->getPost('input_refund_amount'); $caseidcode = (int) $this->request->getPost('caseid'); $inputRemark = (string) $this->request->getPost('input_refund_remark'); $user = auth()->user(); $userId = $user->id ?? ''; $userName = $user->username ?? $user->email ?? 'User'; $userLabel = $userName . '[' . $userId . ']' . date('d-m-Y H:i:s'); $orgInfo = $this->db->table('organization_case_master') ->where('id', $caseidcode) ->get() ->getResult(); $refundOrder = $this->db->table('refund_order') ->where('refund_process', 0) ->where('refund_type', 3) ->where('refund_type_id', $caseidcode) ->get() ->getResult(); $patientId = 0; $orgDate = date('Y-m-d H:i:s'); $patientName = ''; $orgCode = ''; if (! empty($orgInfo)) { $patientId = (int) $orgInfo[0]->p_id; $personInfo = $this->db->query("select *,if(gender=1,'Male','Female') as xgender from patient_master where id='" . $patientId . "' ")->getResult(); $this->applyAge($personInfo, 'str_age'); $orgDate = $orgInfo[0]->insert_datetime; $patientName = $personInfo[0]->p_fname ?? ''; $orgCode = $orgInfo[0]->case_id_code ?? ''; } $refundRequest = [ 'refund_type' => 3, 'refund_type_id' => $caseidcode, 'refund_type_code' => $orgCode, 'refund_type_reason' => $inputRemark, 'approved_by_id' => $userId, 'approved_by' => $userLabel, 'refund_amount' => $inputRefundAmount, 'patient_id' => $patientId, 'patient_name' => $patientName, ]; if (count($refundOrder) < 1) { $this->db->table('refund_order')->insert($refundRequest); $insertId = (int) $this->db->insertID(); return $this->response->setBody('Refund Request has been Created : Req. ID -> ' . $insertId); } return $this->response->setBody('Refund Request Pending, Please Refund or Cancel Last Refund Request'); } private function applyAge(array $rows, string $field = 'age'): void { foreach ($rows as $row) { $row->{$field} = get_age_1( $row->dob ?? null, $row->age ?? '', $row->age_in_month ?? '', $row->estimate_dob ?? '' ); } } private function renderInlinePdf(string $html, string $fileName, string $title) { $mpdf = new Mpdf([ 'mode' => 'utf-8', 'format' => 'A4', 'orientation' => 'P', 'margin_left' => 8, 'margin_right' => 8, 'margin_top' => 8, 'margin_bottom' => 8, 'default_font' => 'dejavusans', 'tempDir' => WRITEPATH . 'cache', ]); $mpdf->SetTitle($title); $mpdf->WriteHTML($html); return $this->response ->setHeader('Content-Type', 'application/pdf') ->setHeader('Content-Disposition', 'inline; filename="' . $fileName . '"') ->setBody($mpdf->Output($fileName, 'S')); } }