diff --git a/api/src/main/java/org/openmrs/module/ugandaemr/api/impl/UgandaEMRServiceImpl.java b/api/src/main/java/org/openmrs/module/ugandaemr/api/impl/UgandaEMRServiceImpl.java index c6b46c101..4ea6079fb 100644 --- a/api/src/main/java/org/openmrs/module/ugandaemr/api/impl/UgandaEMRServiceImpl.java +++ b/api/src/main/java/org/openmrs/module/ugandaemr/api/impl/UgandaEMRServiceImpl.java @@ -2423,21 +2423,40 @@ private void deleteDirectory(Path path) throws IOException { } private boolean validateCPHLBarCode(String accessionNumber) { - Integer minimumCPHLBarCodeLength = Integer.parseInt(Context.getAdministrationService().getGlobalProperty("ugandaemrsync.minimumCPHLBarCodeLength")); - if (accessionNumber == null || accessionNumber.length() < minimumCPHLBarCodeLength) { + if (accessionNumber == null || accessionNumber.trim().isEmpty()) { + return false; + } + accessionNumber = accessionNumber.trim(); + + int minimumCPHLBarCodeLength = 10; + try { + String gpValue = Context.getAdministrationService() + .getGlobalProperty("ugandaemrsync.minimumCPHLBarCodeLength"); + if (gpValue != null) { + minimumCPHLBarCodeLength = Integer.parseInt(gpValue); + } + } catch (NumberFormatException ignored) { + log.error(ignored); + } + + if (accessionNumber.length() < minimumCPHLBarCodeLength || accessionNumber.length() < 2) { return false; } int currentYearSuffix = Year.now().getValue() % 100; + int previousYearSuffix = (currentYearSuffix == 0) ? 99 : currentYearSuffix - 1; + int prefix; try { - int prefix = Integer.parseInt(accessionNumber.substring(0, 2)); - return prefix == currentYearSuffix || prefix == (currentYearSuffix - 1); + prefix = Integer.parseInt(accessionNumber.substring(0, 2)); } catch (NumberFormatException e) { return false; } + + return prefix == currentYearSuffix || prefix == previousYearSuffix; } + private boolean accessionNumberAlreadyUsed(String accessionNumber) { // Escape single quotes to prevent SQL injection or errors String safeAccessionNumber = accessionNumber.replace("'", "''");