Skip to content

Commit

Permalink
Changes to stop/void slots associated with drugorder when its stopped
Browse files Browse the repository at this point in the history
  • Loading branch information
kalai-tw committed Jan 12, 2024
1 parent 28efc13 commit 272cc44
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 4 deletions.
4 changes: 4 additions & 0 deletions api/src/main/java/org/openmrs/module/ipd/api/model/Slot.java
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,10 @@ public enum SlotStatus {
@OneToOne
@JoinColumn(name = "medication_administration_id", referencedColumnName = "medication_administration_id")
private MedicationAdministration medicationAdministration;

public Boolean isStopped() {
return this.status !=null && this.status == SlotStatus.STOPPED;
}
}


Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package org.openmrs.module.ipd.postprocessor;

import org.openmrs.Encounter;
import org.openmrs.module.emrapi.encounter.domain.EncounterTransaction;
import org.openmrs.module.emrapi.encounter.postprocessor.EncounterTransactionHandler;
import org.openmrs.module.ipd.service.IPDScheduleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class IPDTransactionHandler implements EncounterTransactionHandler {

@Autowired
IPDScheduleService ipdScheduleService;

@Override
public void forRead(Encounter encounter, EncounterTransaction encounterTransaction) {
// No Implementation needed as of now
}

@Override
public void forSave(Encounter encounter, EncounterTransaction encounterTransaction) {
ipdScheduleService.handlePostProcessEncounterTransaction(encounter,encounterTransaction);
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package org.openmrs.module.ipd.service;

import org.openmrs.Encounter;
import org.openmrs.module.emrapi.encounter.domain.EncounterTransaction;
import org.openmrs.module.ipd.api.model.Schedule;
import org.openmrs.module.ipd.api.model.ServiceType;
import org.openmrs.module.ipd.api.model.Slot;
Expand All @@ -16,4 +18,5 @@ public interface IPDScheduleService {
List<Slot> getMedicationSlots(String patientUuid, ServiceType serviceType, List<String> orderUuids);
Schedule updateMedicationSchedule(ScheduleMedicationRequest scheduleMedicationRequest);
List<Slot> getMedicationSlotsForTheGivenTimeFrame(String patientUuid, LocalDateTime localStartDate, LocalDateTime localEndDate,Boolean considerAdministeredTime);
void handlePostProcessEncounterTransaction(Encounter encounter, EncounterTransaction encounterTransaction);
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
package org.openmrs.module.ipd.service.impl;

import org.openmrs.Concept;
import org.openmrs.DrugOrder;
import org.openmrs.Patient;
import org.openmrs.Visit;
import org.openmrs.*;
import org.openmrs.api.ConceptService;
import org.openmrs.api.OrderService;
import org.openmrs.api.PatientService;
import org.openmrs.api.VisitService;
import org.openmrs.api.context.Context;
import org.openmrs.module.emrapi.encounter.domain.EncounterTransaction;
import org.openmrs.module.ipd.api.model.Reference;
import org.openmrs.module.ipd.api.model.Schedule;
import org.openmrs.module.ipd.api.model.ServiceType;
Expand All @@ -26,6 +25,7 @@
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.*;
import java.util.stream.Collectors;

import static org.openmrs.module.ipd.api.model.Slot.SlotStatus.SCHEDULED;

Expand Down Expand Up @@ -124,4 +124,29 @@ public List<Slot> getMedicationSlotsForTheGivenTimeFrame(String patientUuid, Loc
}
return slotService.getSlotsBySubjectReferenceIdAndForTheGivenTimeFrame(subjectReference.get(), localStartDate,localEndDate);
}

@Override
public void handlePostProcessEncounterTransaction(Encounter encounter, EncounterTransaction encounterTransaction) {
if (Boolean.valueOf(Context.getAdministrationService().getGlobalProperty("bahmni.ipd.allowSlotStopOnDrugOrderStop","false"))) {
handleDrugOrderStop(encounterTransaction);
}
}

private void handleDrugOrderStop(EncounterTransaction encounterTransaction){
List<EncounterTransaction.DrugOrder> stoppedDrugOrders = encounterTransaction.getDrugOrders().stream().filter(drugOrder -> drugOrder.getDateStopped() !=null).collect(Collectors.toList());
String patientUuid = encounterTransaction.getPatientUuid();
for (EncounterTransaction.DrugOrder drugOrder : stoppedDrugOrders) {
List<Slot> existingSlots = getMedicationSlots(patientUuid,ServiceType.MEDICATION_REQUEST,new ArrayList<>(Arrays.asList(new String[]{drugOrder.getPreviousOrderUuid()})));
if (existingSlots == null || existingSlots.isEmpty()) {
continue;
}
boolean atleastOneMedicationAdministered = existingSlots.stream().anyMatch(slot -> slot.getMedicationAdministration() != null);
if (atleastOneMedicationAdministered){ // Mark status of non administered slots to stopped
existingSlots.stream().forEach(slot -> { if ((slot.getMedicationAdministration() ==null) && !slot.isStopped()) {slot.setStatus(Slot.SlotStatus.STOPPED); slotService.saveSlot(slot);}});
} else { // Void all slots
existingSlots.stream().forEach(slot -> slotService.voidSlot(slot, ""));
}
}

}
}

0 comments on commit 272cc44

Please sign in to comment.