Restore legacy pharmacy reports and fix net/payment totals

This commit is contained in:
DevSoftTech Server 2026-06-01 10:53:27 +05:30
parent 252f911fad
commit f079f32f58
9 changed files with 1083 additions and 0 deletions

View File

@ -210,6 +210,17 @@ $routes->get('Medical_Report/purchase_invoice_report', 'Medical::purchase_invoic
$routes->get('Medical_Report/ipd_discharge', 'Medical::ipd_discharge');
$routes->get('Medical_Report/Report_6_data/(:segment)', 'Medical::report_6_data/$1');
$routes->get('Medical_Report/Report_6_data/(:segment)/(:num)', 'Medical::report_6_data/$1/$2');
$routes->get('Medical_Report/Report_1', 'Medical::report_1');
$routes->get('Medical_Report/Report_1_data/(:segment)', 'Medical::report_1_data/$1');
$routes->get('Medical_Report/Report_1_data/(:segment)/(:num)', 'Medical::report_1_data/$1/$2');
$routes->get('Medical_Report/Report_2', 'Medical::report_2');
$routes->get('Medical_Report/Report_2_data/(:segment)/(:num)', 'Medical::report_2_data/$1/$2');
$routes->get('Medical_Report/Report_2_data/(:segment)/(:num)/(:num)', 'Medical::report_2_data/$1/$2/$3');
$routes->get('Medical_Report/Report_2_short_data/(:segment)/(:num)', 'Medical::report_2_data/$1/$2');
$routes->get('Medical_Report/Report_2_short_data/(:segment)/(:num)/(:num)', 'Medical::report_2_data/$1/$2/$3');
$routes->get('Medical_Report/Report_Payment_Recieved', 'Medical::report_payment_recieved');
$routes->get('Medical_Report/Report_Payment_Recieved_data/(:segment)/(:segment)', 'Medical::report_payment_recieved_data/$1/$2');
$routes->get('Medical_Report/Report_Payment_Recieved_data/(:segment)/(:segment)/(:num)', 'Medical::report_payment_recieved_data/$1/$2/$3');
$routes->get('Medical_Report/Report_daily_med_sale', 'Medical::report_daily_med_sale');
$routes->get('Medical_Report/Report_daily_med_sale_data/(:segment)', 'Medical::report_daily_med_sale_data/$1');
$routes->get('Medical_Report/Report_daily_med_sale_data/(:segment)/(:num)', 'Medical::report_daily_med_sale_data/$1/$2');

View File

@ -8933,6 +8933,212 @@ class Medical extends BaseController
]);
}
public function report_1()
{
if ($deny = $this->ensurePharmacyAccess()) {
return $deny;
}
return view('medical/report_sale_day', [
'today' => date('Y-m-d'),
]);
}
public function report_1_data($dateRange, $output = 0)
{
if ($deny = $this->ensurePharmacyAccess()) {
return $deny;
}
[$dateFrom, $dateTo] = $this->parseLegacyDateRange((string) $dateRange);
$output = (int) $output;
$rows = $this->getSaleDaySummaryRows($dateFrom, $dateTo);
if ($output === 1) {
$content = view('medical/report_sale_day_data', [
'rows' => $rows,
'dateFrom' => $dateFrom,
'dateTo' => $dateTo,
'showHeader' => true,
]);
ExportExcel($content, 'Report_Sale_Day_' . date('YmdHis'));
return;
}
if ($output === 2) {
$html = view('medical/report_sale_day_data', [
'rows' => $rows,
'dateFrom' => $dateFrom,
'dateTo' => $dateTo,
'showHeader' => true,
]);
$mpdf = new Mpdf([
'mode' => 'utf-8',
'format' => 'A4',
'orientation' => 'L',
'margin_left' => 8,
'margin_right' => 8,
'margin_top' => 8,
'margin_bottom' => 8,
'tempDir' => WRITEPATH . 'cache',
]);
$mpdf->SetTitle('Sale Day Report');
$mpdf->WriteHTML($html);
return $this->response
->setHeader('Content-Type', 'application/pdf')
->setBody($mpdf->Output('Report_Sale_Day_' . date('YmdHis') . '.pdf', 'S'));
}
return view('medical/report_sale_day_data', [
'rows' => $rows,
'dateFrom' => $dateFrom,
'dateTo' => $dateTo,
'showHeader' => false,
]);
}
public function report_2()
{
if ($deny = $this->ensurePharmacyAccess()) {
return $deny;
}
return view('medical/report_day_cash', [
'today' => date('Y-m-d'),
]);
}
public function report_2_data($saleDate, $billType = 0, $output = 0)
{
if ($deny = $this->ensurePharmacyAccess()) {
return $deny;
}
$saleDate = $this->normalizeLegacyDateInput((string) $saleDate);
$billType = (int) $billType;
$output = (int) $output;
$rows = $this->getDayCashReportRows($saleDate, $billType);
if ($output === 1) {
$content = view('medical/report_day_cash_data', [
'rows' => $rows,
'saleDate' => $saleDate,
'billType' => $billType,
'showHeader' => true,
]);
ExportExcel($content, 'Report_Day_' . $saleDate . '_' . date('YmdHis'));
return;
}
if ($output === 2) {
$html = view('medical/report_day_cash_data', [
'rows' => $rows,
'saleDate' => $saleDate,
'billType' => $billType,
'showHeader' => true,
]);
$mpdf = new Mpdf([
'mode' => 'utf-8',
'format' => 'A4',
'orientation' => 'L',
'margin_left' => 8,
'margin_right' => 8,
'margin_top' => 8,
'margin_bottom' => 8,
'tempDir' => WRITEPATH . 'cache',
]);
$mpdf->SetTitle('Day Report');
$mpdf->WriteHTML($html);
return $this->response
->setHeader('Content-Type', 'application/pdf')
->setBody($mpdf->Output('Report_Day_' . $saleDate . '_' . date('YmdHis') . '.pdf', 'S'));
}
return view('medical/report_day_cash_data', [
'rows' => $rows,
'saleDate' => $saleDate,
'billType' => $billType,
'showHeader' => false,
]);
}
public function report_payment_recieved()
{
if ($deny = $this->ensurePharmacyAccess()) {
return $deny;
}
return view('medical/report_payment_received', [
'today' => date('Y-m-d'),
'actors' => $this->getPaymentReportActors(),
]);
}
public function report_payment_recieved_data($dateRange, $empNameId = '0', $output = 0)
{
if ($deny = $this->ensurePharmacyAccess()) {
return $deny;
}
[$dateFrom, $dateTo] = $this->parseLegacyDateRange((string) $dateRange);
$empNameId = trim((string) $empNameId);
$output = (int) $output;
$rows = $this->getPaymentReceivedRows($dateFrom, $dateTo, $empNameId);
if ($output === 1) {
$content = view('medical/report_payment_received_data', [
'rows' => $rows,
'dateFrom' => $dateFrom,
'dateTo' => $dateTo,
'showHeader' => true,
]);
ExportExcel($content, 'Report_Payment_Received_' . date('YmdHis'));
return;
}
if ($output === 2) {
$html = view('medical/report_payment_received_data', [
'rows' => $rows,
'dateFrom' => $dateFrom,
'dateTo' => $dateTo,
'showHeader' => true,
]);
$mpdf = new Mpdf([
'mode' => 'utf-8',
'format' => 'A4',
'orientation' => 'L',
'margin_left' => 8,
'margin_right' => 8,
'margin_top' => 8,
'margin_bottom' => 8,
'tempDir' => WRITEPATH . 'cache',
]);
$mpdf->SetTitle('Payment Report');
$mpdf->WriteHTML($html);
return $this->response
->setHeader('Content-Type', 'application/pdf')
->setBody($mpdf->Output('Report_Payment_Received_' . date('YmdHis') . '.pdf', 'S'));
}
return view('medical/report_payment_received_data', [
'rows' => $rows,
'dateFrom' => $dateFrom,
'dateTo' => $dateTo,
'showHeader' => false,
]);
}
public function report_4()
{
if ($deny = $this->ensurePharmacyAccess()) {
@ -9685,6 +9891,327 @@ class Medical extends BaseController
]);
}
private function normalizeLegacyDateInput(string $value): string
{
$value = trim($value);
if ($value === '') {
return date('Y-m-d');
}
$value = str_replace(['_', '.'], '-', $value);
$ts = strtotime($value);
if ($ts === false) {
return date('Y-m-d');
}
return date('Y-m-d', $ts);
}
private function getInvoiceMasterAmountExpressions(): array
{
if (! $this->db->tableExists('invoice_med_master')) {
return [
'netExpr' => '0',
'discountExpr' => '0',
];
}
$masterFields = $this->db->getFieldNames('invoice_med_master') ?? [];
$resolveField = static function (array $fields, array $candidates): ?string {
foreach ($candidates as $candidate) {
foreach ($fields as $field) {
if (strcasecmp((string) $field, (string) $candidate) === 0) {
return (string) $field;
}
}
}
return null;
};
$grossField = $resolveField($masterFields, ['gross_amount', 'gross', 'total_amount', 'bill_amount', 'amount']);
$netField = $resolveField($masterFields, ['net_amount', 'tamount', 'twdisc_amount']);
$discountParts = [];
foreach (['discount_amount', 'disc_amount', 'item_discount_amount'] as $discountCol) {
$discountField = $resolveField($masterFields, [$discountCol]);
if ($discountField !== null) {
$discountParts[] = 'IFNULL(m.' . $discountField . ',0)';
}
}
$discountExpr = ! empty($discountParts) ? implode(' + ', $discountParts) : '0';
if ($grossField !== null) {
$netExpr = 'GREATEST(0, IFNULL(m.' . $grossField . ',0) - (' . $discountExpr . '))';
} elseif ($netField !== null && ! empty($discountParts)) {
$netExpr = 'GREATEST(0, IFNULL(m.' . $netField . ',0) - (' . $discountExpr . '))';
} elseif ($netField !== null) {
$netExpr = 'IFNULL(m.' . $netField . ',0)';
} else {
$netExpr = '0';
}
return [
'netExpr' => $netExpr,
'discountExpr' => $discountExpr,
];
}
private function getSaleDaySummaryRows(string $dateFrom, string $dateTo): array
{
if (! $this->db->tableExists('invoice_med_master')) {
return [];
}
$amountExpr = $this->getInvoiceMasterAmountExpressions();
$netExpr = $amountExpr['netExpr'];
$sql = "SELECT
DATE(m.inv_date) AS sale_date,
COUNT(DISTINCT m.id) AS no_invoice,
COUNT(DISTINCT IFNULL(m.patient_id,0)) AS no_patients,
SUM(" . $netExpr . ") AS net_amount,
SUM(CASE
WHEN IFNULL(m.ipd_id,0)=0 AND IFNULL(m.case_id,0)=0 AND IFNULL(m.case_credit,0)=0 THEN " . $netExpr . "
ELSE 0
END) AS opd_cash_amount,
SUM(CASE
WHEN IFNULL(m.ipd_id,0)=0 AND IFNULL(m.case_id,0)>0 THEN " . $netExpr . "
ELSE 0
END) AS opd_org_amount,
SUM(CASE
WHEN IFNULL(m.ipd_id,0)>0 AND IFNULL(m.ipd_credit,0)=0 THEN " . $netExpr . "
ELSE 0
END) AS ipd_cash_amount,
SUM(CASE
WHEN IFNULL(m.ipd_id,0)>0 AND IFNULL(m.ipd_credit,0)=1 AND IFNULL(m.group_invoice_id,0)=0 THEN " . $netExpr . "
ELSE 0
END) AS ipd_credit_amount,
SUM(CASE
WHEN IFNULL(m.ipd_id,0)>0 AND IFNULL(m.ipd_credit,0)=1 AND IFNULL(m.group_invoice_id,0)>0 THEN " . $netExpr . "
ELSE 0
END) AS ipd_package_amount
FROM invoice_med_master m
WHERE DATE(m.inv_date) BETWEEN ? AND ?
AND IFNULL(m.sale_return,0)=0
GROUP BY DATE(m.inv_date)
ORDER BY DATE(m.inv_date)";
return $this->db->query($sql, [$dateFrom, $dateTo])->getResultArray();
}
private function getDayCashReportRows(string $saleDate, int $billType = 0): array
{
if (! $this->db->tableExists('invoice_med_master')) {
return [];
}
$amountExpr = $this->getInvoiceMasterAmountExpressions();
$netExpr = $amountExpr['netExpr'];
$hasOrgCase = $this->db->tableExists('organization_case_master');
$hasInsurance = $this->db->tableExists('hc_insurance');
$orgFields = $hasOrgCase ? ($this->db->getFieldNames('organization_case_master') ?? []) : [];
$insuranceFields = $hasInsurance ? ($this->db->getFieldNames('hc_insurance') ?? []) : [];
$billCondition = '';
switch ($billType) {
case 1:
$billCondition = ' AND IFNULL(m.ipd_id,0)=0 AND IFNULL(m.case_id,0)=0 AND IFNULL(m.case_credit,0)=0';
break;
case 2:
$billCondition = ' AND IFNULL(m.ipd_id,0)=0 AND IFNULL(m.case_id,0)>0';
break;
case 3:
$billCondition = ' AND IFNULL(m.ipd_id,0)>0 AND IFNULL(m.ipd_credit,0)=0';
break;
case 4:
$billCondition = ' AND IFNULL(m.ipd_id,0)>0 AND IFNULL(m.ipd_credit,0)=1 AND IFNULL(m.group_invoice_id,0)=0';
break;
case 5:
$billCondition = ' AND IFNULL(m.ipd_id,0)>0 AND IFNULL(m.ipd_credit,0)=1 AND IFNULL(m.group_invoice_id,0)>0';
break;
case 6:
$billCondition = ' AND IFNULL(m.sale_return,0)=1';
break;
default:
break;
}
$joinSql = '';
$orgNameExpr = "''";
if ($hasOrgCase && $hasInsurance && in_array('id', $orgFields, true) && in_array('id', $insuranceFields, true)) {
$insuranceKey = null;
foreach (['hc_insurance_id', 'insurance_id', 'insurence_id'] as $candidate) {
if (in_array($candidate, $orgFields, true)) {
$insuranceKey = $candidate;
break;
}
}
if ($insuranceKey !== null) {
$shortNameCol = in_array('short_name', $insuranceFields, true)
? 'short_name'
: (in_array('name', $insuranceFields, true) ? 'name' : null);
if ($shortNameCol !== null) {
$joinSql = ' LEFT JOIN organization_case_master o ON o.id=m.case_id LEFT JOIN hc_insurance i ON i.id=o.' . $insuranceKey;
$orgNameExpr = 'IFNULL(i.' . $shortNameCol . ',\'\')';
}
}
}
$sql = "SELECT
m.id,
m.inv_date,
IFNULL(m.inv_med_code,'-') AS bill_no,
IFNULL(m.patient_code,'-') AS uhid,
IFNULL(m.inv_name,'-') AS patient_name,
" . $netExpr . " AS net_amount,
IFNULL(m.payment_received,0) AS payment_received,
IFNULL(m.payment_balance,0) AS payment_balance,
CASE
WHEN IFNULL(m.sale_return,0)=1 THEN 'SALE RETURN'
WHEN IFNULL(m.ipd_id,0)=0 AND IFNULL(m.case_id,0)=0 AND IFNULL(m.case_credit,0)=0 THEN 'OPD CASH'
WHEN IFNULL(m.ipd_id,0)=0 AND IFNULL(m.case_id,0)>0 THEN 'OPD ORG'
WHEN IFNULL(m.ipd_id,0)>0 AND IFNULL(m.ipd_credit,0)=0 THEN 'IPD CASH'
WHEN IFNULL(m.ipd_id,0)>0 AND IFNULL(m.ipd_credit,0)=1 AND IFNULL(m.group_invoice_id,0)=0 THEN 'IPD CREDIT'
WHEN IFNULL(m.ipd_id,0)>0 AND IFNULL(m.ipd_credit,0)=1 AND IFNULL(m.group_invoice_id,0)>0 THEN 'IPD PACKAGE'
ELSE 'OTHER'
END AS bill_type,
" . $orgNameExpr . " AS org_name
FROM invoice_med_master m
" . $joinSql . "
WHERE DATE(m.inv_date)=?
" . $billCondition . "
ORDER BY m.id";
return $this->db->query($sql, [$saleDate])->getResultArray();
}
private function getPaymentReportActors(): array
{
if (! $this->db->tableExists('payment_history_medical')) {
return [];
}
$joinUsers = '';
$userNameExpr = "''";
if ($this->db->tableExists('users')) {
$userFields = $this->db->getFieldNames('users') ?? [];
if (in_array('id', $userFields, true)) {
$joinUsers = ' LEFT JOIN users u ON u.id=IFNULL(p.update_by_id,0) ';
$nameParts = [];
if (in_array('first_name', $userFields, true)) {
$nameParts[] = "NULLIF(TRIM(u.first_name),'')";
}
if (in_array('last_name', $userFields, true)) {
$nameParts[] = "NULLIF(TRIM(u.last_name),'')";
}
if (! empty($nameParts)) {
$userNameExpr = 'TRIM(CONCAT_WS(\' \' , ' . implode(', ', $nameParts) . '))';
}
if (in_array('username', $userFields, true)) {
$userNameExpr = "COALESCE(NULLIF($userNameExpr,''), NULLIF(TRIM(u.username),''))";
}
if (in_array('email', $userFields, true)) {
$userNameExpr = "COALESCE(NULLIF($userNameExpr,''), NULLIF(TRIM(u.email),''))";
}
}
}
$displayExpr = "CASE
WHEN TRIM(IFNULL(p.update_by,''))<>'' THEN p.update_by
WHEN TRIM(IFNULL($userNameExpr,''))<>'' THEN $userNameExpr
ELSE CONCAT('User-', IFNULL(p.update_by_id,0))
END";
$sql = "SELECT
IFNULL(p.update_by_id,0) AS update_by_id,
$displayExpr AS update_by
FROM payment_history_medical p
$joinUsers
GROUP BY IFNULL(p.update_by_id,0), $displayExpr
ORDER BY update_by ASC";
return $this->db->query($sql)->getResultArray();
}
private function getPaymentReceivedRows(string $dateFrom, string $dateTo, string $empNameId = '0'): array
{
if (! $this->db->tableExists('payment_history_medical')) {
return [];
}
$params = [$dateFrom, $dateTo];
$userFilter = '';
if ($empNameId !== '' && $empNameId !== '0') {
$userFilter = ' AND IFNULL(p.update_by_id,0)=?';
$params[] = (int) $empNameId;
}
$joinInvoice = $this->db->tableExists('invoice_med_master')
? ' LEFT JOIN invoice_med_master m ON m.id=p.Medical_invoice_id '
: '';
$joinUsers = '';
$userNameExpr = "''";
if ($this->db->tableExists('users')) {
$userFields = $this->db->getFieldNames('users') ?? [];
if (in_array('id', $userFields, true)) {
$joinUsers = ' LEFT JOIN users u ON u.id=IFNULL(p.update_by_id,0) ';
$nameParts = [];
if (in_array('first_name', $userFields, true)) {
$nameParts[] = "NULLIF(TRIM(u.first_name),'')";
}
if (in_array('last_name', $userFields, true)) {
$nameParts[] = "NULLIF(TRIM(u.last_name),'')";
}
if (! empty($nameParts)) {
$userNameExpr = 'TRIM(CONCAT_WS(\' \' , ' . implode(', ', $nameParts) . '))';
}
if (in_array('username', $userFields, true)) {
$userNameExpr = "COALESCE(NULLIF($userNameExpr,''), NULLIF(TRIM(u.username),''))";
}
if (in_array('email', $userFields, true)) {
$userNameExpr = "COALESCE(NULLIF($userNameExpr,''), NULLIF(TRIM(u.email),''))";
}
}
}
$displayExpr = "CASE
WHEN TRIM(IFNULL(p.update_by,''))<>'' THEN p.update_by
WHEN TRIM(IFNULL($userNameExpr,''))<>'' THEN $userNameExpr
ELSE CONCAT('User-', IFNULL(p.update_by_id,0))
END";
$sql = "SELECT
p.id,
p.payment_date,
IFNULL(p.Medical_invoice_id,0) AS medical_invoice_id,
IFNULL(m.inv_med_code,'-') AS bill_no,
IFNULL(m.patient_code,'-') AS uhid,
IFNULL(m.inv_name,'-') AS patient_name,
IFNULL(p.amount,0) AS amount,
IFNULL(p.credit_debit,0) AS credit_debit,
CASE
WHEN IFNULL(p.credit_debit,0)=0 THEN 'RECEIVED'
ELSE 'REFUND/DEBIT'
END AS payment_type,
$displayExpr AS update_by
FROM payment_history_medical p
{$joinInvoice}
{$joinUsers}
WHERE DATE(p.payment_date) BETWEEN ? AND ?
{$userFilter}
ORDER BY p.id DESC";
return $this->db->query($sql, $params)->getResultArray();
}
private function getDailyMedicineSaleRows(string $dateFrom, string $dateTo): array
{
if (! $this->db->tableExists('invoice_med_master') || ! $this->db->tableExists('inv_med_item')) {

View File

@ -32,6 +32,15 @@
<a class="btn btn-outline-primary btn-sm" href="javascript:load_form_div('<?= base_url('Medical/master') ?>','medical-main','Master :Pharmacy');">
<i class="bi bi-sliders"></i> Master
</a>
<a class="btn btn-outline-primary btn-sm" href="javascript:load_form_div('<?= base_url('Medical_Report/Report_2') ?>','medical-main','Day Report :Pharmacy');">
<i class="bi bi-calendar-day"></i> Day Report
</a>
<a class="btn btn-outline-primary btn-sm" href="javascript:load_form_div('<?= base_url('Medical_Report/Report_1') ?>','medical-main','Sale Day Report :Pharmacy');">
<i class="bi bi-graph-up-arrow"></i> Sale Day Report
</a>
<a class="btn btn-outline-primary btn-sm" href="javascript:load_form_div('<?= base_url('Medical_Report/Report_Payment_Recieved') ?>','medical-main','Payment Report :Pharmacy');">
<i class="bi bi-cash-stack"></i> Payment Report
</a>
<a class="btn btn-outline-warning btn-sm" href="javascript:load_form_div('<?= base_url('Medical/credit_payout_request') ?>','medical-main','Credit Payout Request :Pharmacy');">
<i class="bi bi-cash-stack"></i> Credit Payout Request
</a>

View File

@ -0,0 +1,94 @@
<div class="pagetitle">
<h1>Day Report <small class="text-muted">Panel</small></h1>
</div>
<section class="section">
<div class="card">
<div class="card-body pt-3">
<div class="row g-3 align-items-end">
<div class="col-md-3">
<label class="form-label">Date</label>
<input type="date" class="form-control" id="sale_date" value="<?= esc($today ?? date('Y-m-d')) ?>">
</div>
<div class="col-md-4">
<label class="form-label">Bill Type</label>
<select class="form-select" id="bill_type">
<option value="0">All Bills</option>
<option value="1">OPD Cash</option>
<option value="2">OPD Organisation</option>
<option value="3">IPD Cash</option>
<option value="4">IPD Credit</option>
<option value="5">IPD Package</option>
<option value="6">Sale Return</option>
</select>
</div>
<div class="col-md-5">
<div class="btn-group">
<button type="button" class="btn btn-primary" id="showreport">Show Report</button>
<button type="button" class="btn btn-outline-primary" id="showreportexport">Export</button>
<button type="button" class="btn btn-outline-secondary" id="showreportprint">Print A4</button>
</div>
</div>
</div>
<div class="row mt-3">
<div class="col-12">
<div id="show_report"></div>
</div>
</div>
</div>
</div>
</section>
<script>
(function () {
function buildUrl(output) {
var saleDate = document.getElementById('sale_date').value || '';
if (!saleDate) {
alert('Please select date');
return '';
}
var billType = document.getElementById('bill_type').value || '0';
return '<?= base_url('Medical_Report/Report_2_data') ?>/' + saleDate + '/' + billType + '/' + output;
}
function loadReport(url) {
if (!url) {
return;
}
if (typeof load_report_div === 'function') {
load_report_div(url, 'show_report');
return;
}
var target = document.getElementById('show_report');
target.innerHTML = '<div class="alert alert-info">Loading...</div>';
fetch(url, { headers: { 'X-Requested-With': 'XMLHttpRequest' } })
.then(function (response) { return response.text(); })
.then(function (html) { target.innerHTML = html; })
.catch(function () {
target.innerHTML = '<div class="alert alert-danger">Unable to load report.</div>';
});
}
document.getElementById('showreport').addEventListener('click', function () {
loadReport(buildUrl(0));
});
document.getElementById('showreportexport').addEventListener('click', function () {
var exportUrl = buildUrl(1);
if (exportUrl) {
window.open(exportUrl, '_blank');
}
});
document.getElementById('showreportprint').addEventListener('click', function () {
var pdfUrl = buildUrl(2);
if (pdfUrl) {
window.open(pdfUrl, '_blank');
}
});
})();
</script>

View File

@ -0,0 +1,81 @@
<?php
$rows = $rows ?? [];
$showHeader = (bool) ($showHeader ?? false);
$saleDate = $saleDate ?? date('Y-m-d');
$billType = (int) ($billType ?? 0);
$billTypeLabel = [
0 => 'All Bills',
1 => 'OPD Cash',
2 => 'OPD Organisation',
3 => 'IPD Cash',
4 => 'IPD Credit',
5 => 'IPD Package',
6 => 'Sale Return',
][$billType] ?? 'All Bills';
$totals = [
'net_amount' => 0.0,
'payment_received' => 0.0,
'payment_balance' => 0.0,
];
?>
<?php if ($showHeader): ?>
<h3 style="margin:0 0 8px 0;">Day Report</h3>
<p style="margin:0 0 12px 0;">Date: <?= esc($saleDate) ?> | Bill Type: <?= esc($billTypeLabel) ?></p>
<?php endif; ?>
<div class="table-responsive">
<table class="table table-bordered table-striped table-sm align-middle">
<thead class="table-light">
<tr>
<th>#</th>
<th>Bill No</th>
<th>Date</th>
<th>UHID</th>
<th>Patient Name</th>
<th>Org</th>
<th>Bill Type</th>
<th class="text-end">Net Amount</th>
<th class="text-end">Paid</th>
<th class="text-end">Balance</th>
</tr>
</thead>
<tbody>
<?php if (empty($rows)): ?>
<tr>
<td colspan="10" class="text-center text-muted">No records found.</td>
</tr>
<?php else: ?>
<?php foreach ($rows as $index => $row): ?>
<?php
$totals['net_amount'] += (float) ($row['net_amount'] ?? 0);
$totals['payment_received'] += (float) ($row['payment_received'] ?? 0);
$totals['payment_balance'] += (float) ($row['payment_balance'] ?? 0);
?>
<tr>
<td><?= esc((string) ($index + 1)) ?></td>
<td><?= esc((string) ($row['bill_no'] ?? '-')) ?></td>
<td><?= esc((string) ($row['inv_date'] ?? '')) ?></td>
<td><?= esc((string) ($row['uhid'] ?? '-')) ?></td>
<td><?= esc((string) ($row['patient_name'] ?? '-')) ?></td>
<td><?= esc((string) ($row['org_name'] ?? '')) ?></td>
<td><?= esc((string) ($row['bill_type'] ?? '')) ?></td>
<td class="text-end"><?= esc(number_format((float) ($row['net_amount'] ?? 0), 2)) ?></td>
<td class="text-end"><?= esc(number_format((float) ($row['payment_received'] ?? 0), 2)) ?></td>
<td class="text-end"><?= esc(number_format((float) ($row['payment_balance'] ?? 0), 2)) ?></td>
</tr>
<?php endforeach; ?>
<?php endif; ?>
</tbody>
<tfoot class="table-light">
<tr>
<th colspan="7" class="text-end">Total</th>
<th class="text-end"><?= esc(number_format($totals['net_amount'], 2)) ?></th>
<th class="text-end"><?= esc(number_format($totals['payment_received'], 2)) ?></th>
<th class="text-end"><?= esc(number_format($totals['payment_balance'], 2)) ?></th>
</tr>
</tfoot>
</table>
</div>

View File

@ -0,0 +1,111 @@
<div class="pagetitle">
<h1>Payment Report <small class="text-muted">Panel</small></h1>
</div>
<section class="section">
<div class="card">
<div class="card-body pt-3">
<div class="row g-3 align-items-end">
<div class="col-md-3">
<label class="form-label">From Date</label>
<input type="date" class="form-control" id="date_from" value="<?= esc($today ?? date('Y-m-d')) ?>">
</div>
<div class="col-md-3">
<label class="form-label">To Date</label>
<input type="date" class="form-control" id="date_to" value="<?= esc($today ?? date('Y-m-d')) ?>">
<input type="hidden" id="inv_date_range" value="">
</div>
<div class="col-md-3">
<label class="form-label">Received By</label>
<select class="form-select" id="emp_name_id">
<option value="0">All Users</option>
<?php foreach (($actors ?? []) as $actor): ?>
<option value="<?= esc((string) ($actor['update_by_id'] ?? '0')) ?>"><?= esc((string) ($actor['update_by'] ?? '')) ?></option>
<?php endforeach; ?>
</select>
</div>
<div class="col-md-3">
<div class="btn-group">
<button type="button" class="btn btn-primary" id="showreport">Show Report</button>
<button type="button" class="btn btn-outline-primary" id="showreportexport">Export</button>
<button type="button" class="btn btn-outline-secondary" id="showreportprint">Print A4</button>
</div>
</div>
</div>
<div class="row mt-3">
<div class="col-12">
<div id="show_report"></div>
</div>
</div>
</div>
</div>
</section>
<script>
(function () {
function syncDateRange() {
var from = document.getElementById('date_from').value || '';
var to = document.getElementById('date_to').value || '';
if (!from || !to) {
document.getElementById('inv_date_range').value = '';
return false;
}
document.getElementById('inv_date_range').value = from + 'S' + to;
return true;
}
function buildUrl(output) {
if (!syncDateRange()) {
alert('Please select From and To date');
return '';
}
var empNameId = document.getElementById('emp_name_id').value || '0';
return '<?= base_url('Medical_Report/Report_Payment_Recieved_data') ?>/' +
document.getElementById('inv_date_range').value + '/' + empNameId + '/' + output;
}
function loadReport(url) {
if (!url) {
return;
}
if (typeof load_report_div === 'function') {
load_report_div(url, 'show_report');
return;
}
var target = document.getElementById('show_report');
target.innerHTML = '<div class="alert alert-info">Loading...</div>';
fetch(url, { headers: { 'X-Requested-With': 'XMLHttpRequest' } })
.then(function (response) { return response.text(); })
.then(function (html) { target.innerHTML = html; })
.catch(function () {
target.innerHTML = '<div class="alert alert-danger">Unable to load report.</div>';
});
}
document.getElementById('date_from').addEventListener('change', syncDateRange);
document.getElementById('date_to').addEventListener('change', syncDateRange);
document.getElementById('showreport').addEventListener('click', function () {
loadReport(buildUrl(0));
});
document.getElementById('showreportexport').addEventListener('click', function () {
var exportUrl = buildUrl(1);
if (exportUrl) {
window.open(exportUrl, '_blank');
}
});
document.getElementById('showreportprint').addEventListener('click', function () {
var pdfUrl = buildUrl(2);
if (pdfUrl) {
window.open(pdfUrl, '_blank');
}
});
syncDateRange();
})();
</script>

View File

@ -0,0 +1,63 @@
<?php
$rows = $rows ?? [];
$showHeader = (bool) ($showHeader ?? false);
$dateFrom = $dateFrom ?? date('Y-m-d');
$dateTo = $dateTo ?? date('Y-m-d');
$totalAmount = 0.0;
?>
<?php if ($showHeader): ?>
<h3 style="margin:0 0 8px 0;">Payment Report</h3>
<p style="margin:0 0 12px 0;">From: <?= esc($dateFrom) ?> | To: <?= esc($dateTo) ?></p>
<?php endif; ?>
<div class="table-responsive">
<table class="table table-bordered table-striped table-sm align-middle">
<thead class="table-light">
<tr>
<th>#</th>
<th>Payment Date</th>
<th>Bill No</th>
<th>UHID</th>
<th>Patient Name</th>
<th>Payment Type</th>
<th class="text-end">Amount</th>
<th>Received By</th>
</tr>
</thead>
<tbody>
<?php if (empty($rows)): ?>
<tr>
<td colspan="8" class="text-center text-muted">No records found.</td>
</tr>
<?php else: ?>
<?php foreach ($rows as $index => $row): ?>
<?php
$amount = (float) ($row['amount'] ?? 0);
$isDebit = (int) ($row['credit_debit'] ?? 0) !== 0;
$signedAmount = $isDebit ? (-1 * abs($amount)) : abs($amount);
$totalAmount += $signedAmount;
?>
<tr>
<td><?= esc((string) ($index + 1)) ?></td>
<td><?= esc((string) ($row['payment_date'] ?? '')) ?></td>
<td><?= esc((string) ($row['bill_no'] ?? '-')) ?></td>
<td><?= esc((string) ($row['uhid'] ?? '-')) ?></td>
<td><?= esc((string) ($row['patient_name'] ?? '-')) ?></td>
<td><?= esc((string) ($row['payment_type'] ?? '-')) ?></td>
<td class="text-end\"><?= esc(number_format($signedAmount, 2)) ?></td>
<td><?= esc((string) ($row['update_by'] ?? '-')) ?></td>
</tr>
<?php endforeach; ?>
<?php endif; ?>
</tbody>
<tfoot class="table-light">
<tr>
<th colspan="6" class="text-end">Total</th>
<th class="text-end"><?= esc(number_format($totalAmount, 2)) ?></th>
<th></th>
</tr>
</tfoot>
</table>
</div>

View File

@ -0,0 +1,101 @@
<div class="pagetitle">
<h1>Sale Day Report <small class="text-muted">Panel</small></h1>
</div>
<section class="section">
<div class="card">
<div class="card-body pt-3">
<div class="row g-3 align-items-end">
<div class="col-md-3">
<label class="form-label">From Date</label>
<input type="date" class="form-control" id="date_from" value="<?= esc($today ?? date('Y-m-d')) ?>">
</div>
<div class="col-md-3">
<label class="form-label">To Date</label>
<input type="date" class="form-control" id="date_to" value="<?= esc($today ?? date('Y-m-d')) ?>">
<input type="hidden" id="inv_date_range" value="">
</div>
<div class="col-md-6">
<div class="btn-group">
<button type="button" class="btn btn-primary" id="showreport">Show Report</button>
<button type="button" class="btn btn-outline-primary" id="showreportexport">Export</button>
<button type="button" class="btn btn-outline-secondary" id="showreportprint">Print A4</button>
</div>
</div>
</div>
<div class="row mt-3">
<div class="col-12">
<div id="show_report"></div>
</div>
</div>
</div>
</div>
</section>
<script>
(function () {
function syncDateRange() {
var from = document.getElementById('date_from').value || '';
var to = document.getElementById('date_to').value || '';
if (!from || !to) {
document.getElementById('inv_date_range').value = '';
return false;
}
document.getElementById('inv_date_range').value = from + 'S' + to;
return true;
}
function buildUrl(output) {
if (!syncDateRange()) {
alert('Please select From and To date');
return '';
}
return '<?= base_url('Medical_Report/Report_1_data') ?>/' +
document.getElementById('inv_date_range').value + '/' + output;
}
function loadReport(url) {
if (!url) {
return;
}
if (typeof load_report_div === 'function') {
load_report_div(url, 'show_report');
return;
}
var target = document.getElementById('show_report');
target.innerHTML = '<div class="alert alert-info">Loading...</div>';
fetch(url, { headers: { 'X-Requested-With': 'XMLHttpRequest' } })
.then(function (response) { return response.text(); })
.then(function (html) { target.innerHTML = html; })
.catch(function () {
target.innerHTML = '<div class="alert alert-danger">Unable to load report.</div>';
});
}
document.getElementById('date_from').addEventListener('change', syncDateRange);
document.getElementById('date_to').addEventListener('change', syncDateRange);
document.getElementById('showreport').addEventListener('click', function () {
loadReport(buildUrl(0));
});
document.getElementById('showreportexport').addEventListener('click', function () {
var exportUrl = buildUrl(1);
if (exportUrl) {
window.open(exportUrl, '_blank');
}
});
document.getElementById('showreportprint').addEventListener('click', function () {
var pdfUrl = buildUrl(2);
if (pdfUrl) {
window.open(pdfUrl, '_blank');
}
});
syncDateRange();
})();
</script>

View File

@ -0,0 +1,86 @@
<?php
$rows = $rows ?? [];
$showHeader = (bool) ($showHeader ?? false);
$dateFrom = $dateFrom ?? date('Y-m-d');
$dateTo = $dateTo ?? date('Y-m-d');
$totals = [
'no_invoice' => 0,
'no_patients' => 0,
'net_amount' => 0.0,
'opd_cash_amount' => 0.0,
'opd_org_amount' => 0.0,
'ipd_cash_amount' => 0.0,
'ipd_credit_amount' => 0.0,
'ipd_package_amount' => 0.0,
];
?>
<?php if ($showHeader): ?>
<h3 style="margin:0 0 8px 0;">Sale Day Report</h3>
<p style="margin:0 0 12px 0;">From: <?= esc($dateFrom) ?> | To: <?= esc($dateTo) ?></p>
<?php endif; ?>
<div class="table-responsive">
<table class="table table-bordered table-striped table-sm align-middle">
<thead class="table-light">
<tr>
<th>#</th>
<th>Sale Date</th>
<th class="text-end">No Invoice</th>
<th class="text-end">No Patient</th>
<th class="text-end">Total Net</th>
<th class="text-end">OPD Cash</th>
<th class="text-end">OPD Org</th>
<th class="text-end">IPD Cash</th>
<th class="text-end">IPD Credit</th>
<th class="text-end">IPD Package</th>
</tr>
</thead>
<tbody>
<?php if (empty($rows)): ?>
<tr>
<td colspan="10" class="text-center text-muted">No records found.</td>
</tr>
<?php else: ?>
<?php foreach ($rows as $index => $row): ?>
<?php
$totals['no_invoice'] += (int) ($row['no_invoice'] ?? 0);
$totals['no_patients'] += (int) ($row['no_patients'] ?? 0);
$totals['net_amount'] += (float) ($row['net_amount'] ?? 0);
$totals['opd_cash_amount'] += (float) ($row['opd_cash_amount'] ?? 0);
$totals['opd_org_amount'] += (float) ($row['opd_org_amount'] ?? 0);
$totals['ipd_cash_amount'] += (float) ($row['ipd_cash_amount'] ?? 0);
$totals['ipd_credit_amount'] += (float) ($row['ipd_credit_amount'] ?? 0);
$totals['ipd_package_amount'] += (float) ($row['ipd_package_amount'] ?? 0);
?>
<tr>
<td><?= esc((string) ($index + 1)) ?></td>
<td><?= esc((string) ($row['sale_date'] ?? '')) ?></td>
<td class="text-end"><?= esc((string) ((int) ($row['no_invoice'] ?? 0))) ?></td>
<td class="text-end"><?= esc((string) ((int) ($row['no_patients'] ?? 0))) ?></td>
<td class="text-end"><?= esc(number_format((float) ($row['net_amount'] ?? 0), 2)) ?></td>
<td class="text-end"><?= esc(number_format((float) ($row['opd_cash_amount'] ?? 0), 2)) ?></td>
<td class="text-end"><?= esc(number_format((float) ($row['opd_org_amount'] ?? 0), 2)) ?></td>
<td class="text-end"><?= esc(number_format((float) ($row['ipd_cash_amount'] ?? 0), 2)) ?></td>
<td class="text-end"><?= esc(number_format((float) ($row['ipd_credit_amount'] ?? 0), 2)) ?></td>
<td class="text-end"><?= esc(number_format((float) ($row['ipd_package_amount'] ?? 0), 2)) ?></td>
</tr>
<?php endforeach; ?>
<?php endif; ?>
</tbody>
<tfoot class="table-light">
<tr>
<th colspan="2" class="text-end">Total</th>
<th class="text-end"><?= esc((string) $totals['no_invoice']) ?></th>
<th class="text-end"><?= esc((string) $totals['no_patients']) ?></th>
<th class="text-end"><?= esc(number_format($totals['net_amount'], 2)) ?></th>
<th class="text-end"><?= esc(number_format($totals['opd_cash_amount'], 2)) ?></th>
<th class="text-end"><?= esc(number_format($totals['opd_org_amount'], 2)) ?></th>
<th class="text-end"><?= esc(number_format($totals['ipd_cash_amount'], 2)) ?></th>
<th class="text-end"><?= esc(number_format($totals['ipd_credit_amount'], 2)) ?></th>
<th class="text-end"><?= esc(number_format($totals['ipd_package_amount'], 2)) ?></th>
</tr>
</tfoot>
</table>
</div>