-
Notifications
You must be signed in to change notification settings - Fork 135
Admin Fill Bill Department Type SQL
When a department type filter is selected on reports such as the Stock Transfer Report
(stock_transfer_report.xhtml), the JPQL query adds:
AND b.departmentType IN :departmentTypesBills created before the departmentType feature was introduced have NULL in that
column and are therefore excluded, causing the report to return no data even though transfers
exist for the selected date range.
The Bill entity stores departmentType as a string enum column (DEPARTMENTTYPE in MySQL).
Bills created by older versions of the application (pharmacy transfer requests/issues/receives,
GRNs, store issues, etc.) were never backfilled with this value.
This is the recommended approach. A single UPDATE statement completes in seconds regardless
of how many bills are affected.
Run this first to see how many rows will be changed, broken down by bill type:
SELECT BILLTYPEATOMIC, COUNT(*) AS cnt
FROM bill
WHERE DEPARTMENTTYPE IS NULL
AND RETIRED = 0
GROUP BY BILLTYPEATOMIC
ORDER BY cnt DESC;All PHARMACY_* bill types belong to the Pharmacy department type:
UPDATE bill
SET DEPARTMENTTYPE = 'Pharmacy'
WHERE DEPARTMENTTYPE IS NULL
AND RETIRED = 0
AND BILLTYPEATOMIC LIKE 'PHARMACY_%';UPDATE bill
SET DEPARTMENTTYPE = 'Store'
WHERE DEPARTMENTTYPE IS NULL
AND RETIRED = 0
AND BILLTYPEATOMIC LIKE 'STORE_%';SELECT BILLTYPEATOMIC, COUNT(*) AS cnt
FROM bill
WHERE DEPARTMENTTYPE IS NULL
AND RETIRED = 0
AND BILLTYPEATOMIC LIKE 'PHARMACY_%'
OR BILLTYPEATOMIC LIKE 'STORE_%'
GROUP BY BILLTYPEATOMIC;Expected: zero rows returned.
The Bill.DEPARTMENTTYPE column stores the Java enum name exactly as declared
(EnumType.STRING). Use these exact string values:
| Enum constant | SQL string value |
|---|---|
Pharmacy |
'Pharmacy' |
Store |
'Store' |
Lab |
'Lab' |
Clinical |
'Clinical' |
NonClinical |
'NonClinical' |
Opd |
'Opd' |
Inward |
'Inward' |
Theatre |
'Theatre' |
Etu |
'Etu' |
Kitchen |
'Kitchen' |
Other |
'Other' |
The previous "Fill Bill Department Type" button in admin_functions.xhtml called
DataAdministrationController.fillBillDepartmentTypeFromBillItems(), which:
- Loads all NULL-departmentType bills into JVM memory with one JPQL query.
- For each bill, fires a separate JPQL query to fetch its bill items.
- Updates each bill individually via JPA.
This is an O(n) round-trip pattern — thousands of JPA calls for a large database.
The native UPDATE … WHERE … LIKE 'PHARMACY_%' replaces all of that with a single
server-side operation.
- The
billtable name is lowercase on all known HMIS deployments (Linux MySQL). - Column names are UPPERCASE (
DEPARTMENTTYPE,BILLTYPEATOMIC,RETIRED). - The enum values in
BILLTYPEATOMICare UPPER_SNAKE_CASE (e.g.,PHARMACY_ISSUE). - The enum values in
DEPARTMENTTYPEare CamelCase (e.g.,Pharmacy,Store). - The
LIKE 'PHARMACY_%'pattern is safe: all pharmacy-service bill types use this prefix and all of them belong to thePharmacydepartment type. - Safe to run multiple times — the
WHERE DEPARTMENTTYPE IS NULLguard is idempotent.