Skip to content

Commit cf62f54

Browse files
authored
void corresponding appointment when form encounter is voided (#671)
1 parent 5524b41 commit cf62f54

4 files changed

Lines changed: 250 additions & 391 deletions

File tree

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
package org.openmrs.module.pihcore.encounter;
2+
3+
import java.util.Date;
4+
5+
import org.apache.commons.logging.Log;
6+
import org.apache.commons.logging.LogFactory;
7+
import org.openmrs.Concept;
8+
import org.openmrs.Encounter;
9+
import org.openmrs.Obs;
10+
import org.openmrs.User;
11+
import org.openmrs.annotation.Handler;
12+
import org.openmrs.api.context.Context;
13+
import org.openmrs.api.handler.SaveHandler;
14+
import org.openmrs.module.appointments.model.Appointment;
15+
import org.openmrs.module.appointments.model.AppointmentStatus;
16+
import org.openmrs.module.appointments.service.AppointmentsService;
17+
18+
/**
19+
* When an encounter is voided, voids any active appointment linked to the encounter via a
20+
* top-level "PIH:Appointment uuid" obs (as saved by the scheduleAppointment tag handler).
21+
*
22+
* This fires inside the saveEncounter transaction (after voidEncounter has already voided the
23+
* obs tree), so the appointment entity is mutated in-place rather than re-saved through
24+
* AppointmentsService.validateAndSave() — calling a @Transactional save here could force an
25+
* early Hibernate flush and trip over not-yet-populated entities in the same session.
26+
*/
27+
@Handler(supports = Encounter.class)
28+
public class PihEncounterSaveHandler implements SaveHandler<Encounter> {
29+
30+
private final Log log = LogFactory.getLog(getClass());
31+
32+
@Override
33+
public void handle(Encounter encounter, User user, Date date, String reason) {
34+
voidAppointmentIfEncounterVoided(encounter, user, date);
35+
}
36+
37+
private void voidAppointmentIfEncounterVoided(Encounter encounter, User user, Date date) {
38+
if (encounter.getEncounterId() == null || !encounter.getVoided()) {
39+
return;
40+
}
41+
42+
Concept uuidConcept = Context.getConceptService().getConceptByMapping("Appointment uuid", "PIH");
43+
if (uuidConcept == null) {
44+
return;
45+
}
46+
47+
AppointmentsService appointmentsService;
48+
try {
49+
appointmentsService = Context.getService(AppointmentsService.class);
50+
} catch (Exception e) {
51+
return;
52+
}
53+
54+
for (Obs obs : encounter.getObsAtTopLevel(true)) {
55+
if (!uuidConcept.equals(obs.getConcept()) || obs.getValueText() == null) {
56+
continue;
57+
}
58+
Appointment appointment = appointmentsService.getAppointmentByUuid(obs.getValueText());
59+
if (appointment == null
60+
|| Boolean.TRUE.equals(appointment.getVoided())
61+
|| appointment.getStatus() == AppointmentStatus.Cancelled) {
62+
continue;
63+
}
64+
appointment.setVoided(true);
65+
appointment.setVoidedBy(user);
66+
appointment.setDateVoided(date);
67+
appointment.setVoidReason("Encounter voided");
68+
log.debug("Voided appointment " + appointment.getUuid()
69+
+ " due to voided encounter " + encounter.getUuid());
70+
}
71+
}
72+
}

api/src/main/java/org/openmrs/module/pihcore/htmlformentry/action/ScheduleAppointmentAction.java

Lines changed: 0 additions & 168 deletions
This file was deleted.

0 commit comments

Comments
 (0)