From c0144244e778b3afc87442799f0d347d43fbf44d Mon Sep 17 00:00:00 2001 From: Chi Bong Ho Date: Wed, 6 Nov 2024 22:23:16 -0500 Subject: [PATCH] EA-209 fix date comparison in getMothersAndChildren() to determine whether child is born during mother's visit --- api/src/main/resources/hql/mother_child.hql | 5 ++-- .../maternal/MaternalServiceImplTest.java | 26 +++++++++++++++++++ 2 files changed, 28 insertions(+), 3 deletions(-) diff --git a/api/src/main/resources/hql/mother_child.hql b/api/src/main/resources/hql/mother_child.hql index 2223a536..653de127 100644 --- a/api/src/main/resources/hql/mother_child.hql +++ b/api/src/main/resources/hql/mother_child.hql @@ -12,7 +12,6 @@ where and (:motherRequiredToHaveActiveVisit = false or (select count(motherVisit) from Visit as motherVisit where motherVisit.patient = mother and motherVisit.stopDatetime is null and motherVisit.voided = false) > 0) and (:childRequiredToHaveActiveVisit = false or (select count(childVisit) from Visit as childVisit where childVisit.patient = child and childVisit.stopDatetime is null and childVisit.voided = false) > 0) and (:childRequiredToBeBornDuringMothersActiveVisit = false or (select count(motherVisit) from Visit as motherVisit where motherVisit.patient = mother and motherVisit.stopDatetime is null and motherVisit.voided = false - and year(child.birthdate) >= year(motherVisit.startDatetime) - and month(child.birthdate) >= month(motherVisit.startDatetime) - and day(child.birthdate) >= day(motherVisit.startDatetime)) > 0) + and (year(child.birthdate)||'-'||month(child.birthdate)||'-'||day(child.birthdate)) >= + (year(motherVisit.startDatetime)||'-'||month(motherVisit.startDatetime)||'-'||day(motherVisit.startDatetime))) > 0) and mother.voided = false and child.voided = false and motherChildRelationship.voided = false diff --git a/api/src/test/java/org/openmrs/module/emrapi/maternal/MaternalServiceImplTest.java b/api/src/test/java/org/openmrs/module/emrapi/maternal/MaternalServiceImplTest.java index 01bcec40..4b4faffa 100644 --- a/api/src/test/java/org/openmrs/module/emrapi/maternal/MaternalServiceImplTest.java +++ b/api/src/test/java/org/openmrs/module/emrapi/maternal/MaternalServiceImplTest.java @@ -217,6 +217,32 @@ public void shouldGetChildByMotherIfChildBornBeforeVisitStartButSameDayAndChildB assertThat(motherAndChildList.get(0).getMother(), equalTo(mother)); assertNull(motherAndChildList.get(0).getChildAdmission()); } + + @Test + public void shouldGetChildByMotherIfChildBornInNextCalendarYearAfterVisitStart() { + DateTime lastDayOfYear = new DateTime(1980, 12, 31, 0, 0); + Date oneDayLater = lastDayOfYear.plusDays(1).toDate(); + + Location visitLocation = testDataManager.location().name("Visit Location").save(); + + Patient mother = testDataManager.randomPatient().birthdate("1980-01-01").gender("F").save(); + Patient child = testDataManager.randomPatient().birthdate(oneDayLater).save(); + + Relationship motherChildRelationship = new Relationship(); + motherChildRelationship.setRelationshipType(emrApiProperties.getMotherChildRelationshipType()); + motherChildRelationship.setPersonA(mother); + motherChildRelationship.setPersonB(child); + personService.saveRelationship(motherChildRelationship); + + Visit motherVisit = testDataManager.visit().visitType(emrApiProperties.getAtFacilityVisitType()).location(visitLocation).patient(mother).started(lastDayOfYear.toDate()).save(); + + List motherAndChildList = maternalService.getMothersAndChildren(new MothersAndChildrenSearchCriteria(Collections.singletonList(mother.getUuid()), null, false, false, true)); + + assertThat(motherAndChildList.size(),equalTo(1)); + assertThat(motherAndChildList.get(0).getChild(),equalTo(child)); + assertThat(motherAndChildList.get(0).getMother(), equalTo(mother)); + assertNull(motherAndChildList.get(0).getChildAdmission()); + } @Test public void shouldGetMultipleChildByMother() {