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 (o.insurance_case_id = cast(c.id as char) or o.insurance_case_id = c.case_id_code)
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 = cast(c.id as char)
or o.insurance_case_id = c.case_id_code
or o.p_id = c.p_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 hc_items_insurance 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
or i.insurance_case_id = c.case_id_code
or i.attach_id = c.p_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])
->groupStart()
->where('insurance_case_id', (string) $caseId)
->orWhere('insurance_case_id', (string) ($caseRow->case_id_code ?? ''))
->orWhere('p_id', (int) ($caseRow->p_id ?? 0))
->groupEnd()
->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)
->groupStart()
->where('insurance_case_id', $caseId)
->orWhere('insurance_case_id', (string) ($caseRow->case_id_code ?? ''))
->orWhere('attach_id', (int) ($caseRow->p_id ?? 0))
->groupEnd()
->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 .= '
Name of Hospital : ' . H_Name . '
'; $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.…………………………… | |
Expenditure an account of : Treatment/ Super Treatment/ Special investigation / Consultant fee
'; $content .= '| In respect of Name of Patient: ' . strtoupper((string) $data['orgcase'][0]->p_name) . ' | ';
$content .= 'Relation : ' . strtoupper((string) $data['orgcase'][0]->relation_with_cardholder) . ' | '; $content .= 'Service No.: ' . strtoupper((string) $data['orgcase'][0]->insurance_no_2) . ' | '; $content .= '
| ESM Name : ' . strtoupper((string) $data['orgcase'][0]->insurance_card_name) . ' | '; $content .= 'ECHS CARD No. : ' . strtoupper((string) $data['orgcase'][0]->insurance_no) . ' | '; $content .= '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.
| Sr.No. | Date | Details of Expenditure | Number or Quantity | Rate | Per | Amount | |
|---|---|---|---|---|---|---|---|
| ' . $srno . ' | ' . date('Y-m-d') . ' | OPD | '; $tbody .= '' . $opd[0]->rate . ' | ' . $opd[0]->item_qty . ' | '; $tbody .= '' . $opd[0]->rate . ' | CGHS | ' . $opd[0]->tamount . ' | '; $tbody .= '
| ' . $srno . ' | ' . date('Y-m-d') . ' | Package :' . $row->package_name . ' | '; $tbody .= '' . $row->package_Amount . ' | 1 | '; $tbody .= '' . $row->package_Amount . ' | CGHS | ' . $row->package_Amount . ' | '; $tbody .= '
| ' . $srno . ' | ' . date('Y-m-d') . ' | ' . $row->item_name . ' | '; $tbody .= '' . $row->item_rate . ' | ' . $row->unit . ' | '; $tbody .= '' . $row->item_rate . ' | CGHS | ' . $row->tamount . ' | '; $tbody .= '
| ' . $srno . ' | ' . date('Y-m-d') . ' | Medicine | '; $tbody .= '' . $data['MedInvoice'][0]->Med_Amount . ' | ' . $data['MedInvoice'][0]->no_inv . ' | '; $tbody .= '' . $data['MedInvoice'][0]->Med_Amount . ' | ' . $data['MedInvoice'][0]->Med_Amount . ' | '; $tbody .= '|
| 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) . ' | ||||||
Net Amount due(In words) : Rs. ' . number_to_word(round($totalGrossBill, 0)) . '
'; $content .= '| Date : ...................... | Pre Received Payment | ||
| From : ...................... | ..................... |
Certified that
';
$content .= '
| Vetted and Recommended / Not Recommended | Sig of OIC ECHS Polyclinic | |
Date : ...................... Place: ...................... | ..................... |