/** @see org.openmrs.api.EncounterService#unvoidEncounter(org.openmrs.Encounter) */
  public Encounter unvoidEncounter(Encounter encounter) throws APIException {

    // if authenticated user is not supposed to edit encounter of certain type
    if (!canEditEncounter(encounter, null)) {
      throw new APIException(
          "Encounter.error.privilege.required.unvoid",
          new Object[] {encounter.getEncounterType().getEditPrivilege()});
    }

    String voidReason = encounter.getVoidReason();
    if (voidReason == null) {
      voidReason = "";
    }

    ObsService os = Context.getObsService();
    for (Obs o : encounter.getObsAtTopLevel(true)) {
      if (voidReason.equals(o.getVoidReason())) {
        os.unvoidObs(o);
      }
    }

    OrderService orderService = Context.getOrderService();
    for (Order o : encounter.getOrders()) {
      if (voidReason.equals(o.getVoidReason())) {
        orderService.unvoidOrder(o);
      }
    }

    encounter.setVoided(false);
    encounter.setVoidedBy(null);
    encounter.setDateVoided(null);
    encounter.setVoidReason(null);
    Context.getEncounterService().saveEncounter(encounter);
    return encounter;
  }
  @Test
  public void shouldAddNewDrugOrderWhenPrnIsTrueWIthNoDosageFrequencyOrDosageInstruction() {

    EncounterTransaction.DrugOrder drugOrder =
        new DrugOrderBuilder()
            .withBasicValues("drug-uuid", "test-concept-uuid", today, today, 3, "", "")
            .withPrn(true)
            .build();
    Concept drugConcept = new Concept(3);
    OrderType drugOrderType = new OrderType("Drug Order", "this is a drug order type");

    when(orderService.getAllOrderTypes()).thenReturn(asList(drugOrderType));
    when(conceptService.getConceptByUuid("test-concept-uuid")).thenReturn(drugConcept);
    Drug drug = new Drug();
    drug.setName("test drug");
    when(conceptService.getDrugByUuid("drug-uuid")).thenReturn(drug);

    encounterDrugOrderServiceHelper.update(encounter, asList(drugOrder));

    assertThat(encounter.getOrders().size(), is(1));
    org.openmrs.DrugOrder order = (org.openmrs.DrugOrder) encounter.getOrders().iterator().next();
    assertEquals(drugConcept, order.getConcept());
    assertEquals(drugOrderType, order.getOrderType());
    assertEquals(patient, order.getPatient());
    assertEquals(encounter, order.getEncounter());
    assertEquals(today, order.getStartDate());
    assertEquals(today, order.getAutoExpireDate());
    assertEquals(drug.getDisplayName(), order.getDrug().getDisplayName());
    assertEquals(Double.valueOf(3), order.getDose());

    assertEquals(true, order.getPrn());
    assertEquals(null, order.getFrequency());
    assertEquals(null, order.getUnits());
  }
  @Test
  public void discontinueOrder() throws Exception {
    Provider provider = mock(Provider.class);
    handleEncounterProvider(provider);

    Order mrsOrder = new Order();
    when(orderService.getOrderByUuid("previous order uuid")).thenReturn(mrsOrder);

    Date createdDate = new Date();
    EncounterTransaction.Concept blood =
        new EncounterTransaction.Concept("bloodConceptUuid", "blood");

    EncounterTransaction.Order etOrder =
        new OrderBuilder()
            .withAction(org.openmrs.Order.Action.DISCONTINUE.toString())
            .withUuid("orderUuid")
            .withConcept(blood)
            .withPreviousOrderUuid("previous order uuid")
            .withDateCreated(createdDate)
            .build();

    OpenMRSOrderMapper orderMapper = new OpenMRSOrderMapper(orderService, conceptService);
    Order order = orderMapper.map(etOrder, encounter);

    Assert.assertEquals(org.openmrs.Order.Action.DISCONTINUE, order.getAction());
  }
  @Test
  public void createRevisedOrderFromEtOrder() {
    Provider provider = mock(Provider.class);
    handleEncounterProvider(provider);

    Order originalOrder = new Order();
    when(orderService.getOrderByUuid("previousOrderUuid")).thenReturn(originalOrder);

    Date currentDate = new Date();

    EncounterTransaction.Order etOrder = new EncounterTransaction.Order();
    etOrder.setUuid(null);
    etOrder.setPreviousOrderUuid("previousOrderUuid");
    etOrder.setAutoExpireDate(currentDate);
    etOrder.setCommentToFulfiller("Comment");

    OpenMRSOrderMapper orderMapper = new OpenMRSOrderMapper(orderService, conceptService);

    Order order = orderMapper.map(etOrder, encounter);

    verify(orderService).getOrderByUuid("previousOrderUuid");
    Assert.assertEquals(encounter, order.getEncounter());
    Assert.assertEquals("Comment", order.getCommentToFulfiller());
    Assert.assertEquals(currentDate, order.getAutoExpireDate());
    Assert.assertEquals(provider, order.getOrderer());
  }
Ejemplo n.º 5
0
  /**
   * @see org.openmrs.api.EncounterService#voidEncounter(org.openmrs.Encounter, java.lang.String)
   */
  public Encounter voidEncounter(Encounter encounter, String reason) {

    // if authenticated user is not supposed to edit encounter of certain type
    if (!canEditEncounter(encounter, null)) {
      throw new APIException(
          String.format(
              "Privilege %s required to void encounters of this type",
              encounter.getEncounterType().getEditPrivilege()));
    }

    if (reason == null) {
      throw new IllegalArgumentException("The argument 'reason' is required and so cannot be null");
    }

    ObsService os = Context.getObsService();
    for (Obs o : encounter.getObsAtTopLevel(false)) {
      if (!o.isVoided()) {
        os.voidObs(o, reason);
      }
    }

    OrderService orderService = Context.getOrderService();
    for (Order o : encounter.getOrders()) {
      if (!o.isVoided()) {
        orderService.voidOrder(o, reason);
      }
    }

    encounter.setVoided(true);
    encounter.setVoidedBy(Context.getAuthenticatedUser());
    // we expect the dateVoided to be already set by AOP logic at this point unless this method was
    // called within the API,
    // this ensures that original ParentVoidedDate and the dateVoided of associated objects will
    // always match for the
    // unvoid handler to work
    if (encounter.getDateVoided() == null) {
      encounter.setDateVoided(new Date());
    }
    encounter.setVoidReason(reason);
    Context.getEncounterService().saveEncounter(encounter);
    return encounter;
  }
  /**
   * @verifies not allow a future dateActivated
   * @see OrderValidator#validate(Object, org.springframework.validation.Errors)
   */
  @Test
  public void validate_shouldNotAllowAFutureDateActivated() throws Exception {
    Patient patient = Context.getPatientService().getPatient(7);
    TestOrder order = new TestOrder();
    order.setPatient(patient);
    order.setOrderType(orderService.getOrderTypeByName("Test order"));
    order.setEncounter(Context.getEncounterService().getEncounter(3));
    order.setConcept(Context.getConceptService().getConcept(5497));
    order.setOrderer(Context.getProviderService().getProvider(1));
    order.setCareSetting(orderService.getCareSetting(1));
    Calendar cal = Calendar.getInstance();
    cal.add(Calendar.HOUR_OF_DAY, 1);
    order.setDateActivated(cal.getTime());

    Errors errors = new BindException(order, "order");
    new OrderValidator().validate(order, errors);

    Assert.assertTrue(errors.hasFieldErrors("dateActivated"));
    Assert.assertEquals(
        "Order.error.dateActivatedInFuture", errors.getFieldError("dateActivated").getCode());
  }
  /* here's the logic for what happens if there is a future regimen:
  *
  *
  *      //1.  if no drug order of a specific DrugOrder, (create the new order).  (OUTCOME1)

         //2.  if DrugOrder, for all DrugOrders:
             //a.  if new start is before old start
                 //NEW ORDER HAS STOP DATE -- create the older as is, and make adjustments:
                     //1.  if new end is before or equal to old start (create the new order)  (OUTCOME2)
                     //2.  if new end is after old start and ( before old end or old end is infinite)
                        //if order is different, adjust the start date of the old order to the end date of the new (create the new order) (OUTCOME3)
                        //if order is same void old order(doesn't matter if old order has infinite stop date or not) (create the new order) (OUTCOME4)
                     //3. if end date is greater than or equal to old end -- void old order   (create the new order)  (OUTCOME5)
                 //NEW ORDER DOESN'T HAVE STOP DATE
                     //4. orders are different
                          //set end date of new to beginning of old and stop iterating over existing drug orders time sequence (create the new order, modified) (OUTCOME6)
                     //5. orders are the same
                         //delete the old order (create the new order) (OUTCOME7)
             //b. if start is the same
                 // void existing (create the new order) (OUTCOME8)

             //c.  if start is after existing start
                 //1. if order is after old drug end  (create the new order) (OUTCOME9)
                 //2. if order is before old drug end or equal to old drug end or old drug end date is infinite
                     //if orders are the same update the old order with the new, taking the new end date value (Do not create new order) (OUTCOME10)
                     //if orders are different adjust the old to end on new start date (create the new order) (OUTCOME11)

  * */
  public static void setRegimen(
      Patient patient,
      Date effectiveDate,
      Collection<DrugOrder> drugOrders,
      Concept reasonForChange,
      Encounter encounterForChange) {
    RegimenHistory history = getRegimenHistory(patient);
    Regimen regOnDate = history.getRegimen(effectiveDate);
    List<Regimen> regAfterDate = history.getRegimensAfter(effectiveDate);
    OrderService os = Context.getOrderService();
    if (encounterForChange != null) {
      Context.getEncounterService().saveEncounter(encounterForChange);
      for (DrugOrder drugOrder : drugOrders) {
        drugOrder.setEncounter(encounterForChange);
      }
    }

    if (!anyRegimens(regAfterDate)) {
      if (regOnDate == null || regOnDate.getComponents().isEmpty()) {
        // case:  there is no existing regimen on the regimen start date, and there are no new
        // regimens after this date
        // go ahead and create the regimen:
        for (DrugOrder drugOrder : drugOrders) {
          Context.getOrderService().saveOrder(drugOrder);
        }
      } else {

        // case: there are still open orders and there are no new regimens after this date
        // first see what existing things we need to stop
        for (RegimenComponent before : regOnDate.getComponents()) {
          // stop the old order only if it isn't exactly identical to a new order (excluding
          // discontinued_date)
          for (DrugOrder newOrder : drugOrders) {
            // but either concept or drug is the same
            if (!before.getDrugOrder().getDiscontinued()
                && drugOrderMatchesDrugConcept(before.getDrugOrder(), newOrder)
                && !regimenComponentIsTheSameAsDrugOrderExcludingDates(
                    before.getDrugOrder(), newOrder)) {
              discontinueOrder(before.getDrugOrder(), effectiveDate, reasonForChange);
            }
          }
        }
        // now see what new things to start (or extend)
        for (DrugOrder newOrder : drugOrders) {

          // create a new order if there isn't already an existing match,
          // or if there is (excluding discontinued date) you need to extend, or null the stop date

          boolean alreadyExists = false;
          for (RegimenComponent before : regOnDate.getComponents()) {

            if (!before.getDrugOrder().getDiscontinued()
                && regimenComponentIsTheSameAsDrugOrderExcludingDates(
                    before.getDrugOrder(), newOrder)) {
              alreadyExists = true;
              before.getDrugOrder().setDiscontinuedDate(newOrder.getDiscontinuedDate());
              before.getDrugOrder().setAutoExpireDate(newOrder.getAutoExpireDate());
              before.getDrugOrder().setPrn(newOrder.getPrn());
              before.getDrugOrder().setInstructions(newOrder.getInstructions());
              os.saveOrder(before.getDrugOrder());
              newOrder.setOrderId(before.getDrugOrder().getOrderId());
              break;
            }
          }
          if (!alreadyExists) {
            os.saveOrder(newOrder);
          }
        }
      }
    } else { // there is a regimen change after the new drug order start date
      for (DrugOrder newOrder : drugOrders) {
        boolean saveOrder = false;
        boolean merged = false;
        history = getRegimenHistory(patient);
        List<DrugOrder> existingDrugOrders = getDrugOrdersInOrderByDrugOrConcept(history, newOrder);
        if (existingDrugOrders.size() == 0) {
          saveOrder = setSaveOrder(merged); // (OUTCOME1)
        } else {
          for (DrugOrder before : existingDrugOrders) {
            if (newOrder.getStartDate().before(before.getStartDate())) {
              if (newOrder.getDiscontinuedDate() != null) {
                if (newOrder.getDiscontinuedDate().before(before.getStartDate())
                    || newOrder.getDiscontinuedDate().equals(before.getStartDate())) {
                  saveOrder = setSaveOrder(merged); // (OUTCOME2)
                } else if (newOrder.getDiscontinuedDate().after(before.getStartDate())
                    && (before.getDiscontinuedDate() == null
                        || newOrder.getDiscontinuedDate().before(before.getDiscontinuedDate()))) {
                  if (!regimenComponentIsTheSameAsDrugOrderExcludingDates(before, newOrder)) {
                    // (OUTCOME3)
                    before.setStartDate(newOrder.getDiscontinuedDate());
                    os.saveOrder(before);
                    saveOrder = setSaveOrder(merged);
                  } else {
                    // (OUTCOME4)
                    os.voidOrder(before, "overwritten");
                    saveOrder = setSaveOrder(merged);
                  }
                } else if (before.getDiscontinuedDate() != null
                    && (newOrder.getDiscontinuedDate().after(before.getDiscontinuedDate())
                        || newOrder.getDiscontinuedDate().equals(before.getDiscontinuedDate()))) {
                  // (OUTCOME5)
                  os.voidOrder(before, "overwritten");
                  saveOrder = setSaveOrder(merged);
                }
              } else { // new order has infinite end date
                if (!regimenComponentIsTheSameAsDrugOrderExcludingDates(before, newOrder)) {
                  // (OUTCOME6)
                  newOrder.setDiscontinuedDate(before.getStartDate());
                  saveOrder = setSaveOrder(merged);
                  break;
                } else {
                  // (OUTCOME7)
                  os.voidOrder(before, "overwritten");
                  saveOrder = setSaveOrder(merged);
                }
              }
            } else if (newOrder.getStartDate().equals(before.getStartDate())) { // b
              // (OUTCOME8)
              os.voidOrder(before, "overwritten");
              saveOrder = setSaveOrder(merged);
            } else { // c -- start date is after or equal to old end date
              if (before.getDiscontinuedDate() != null
                  && newOrder.getStartDate().after(before.getDiscontinuedDate())) // 1
                // (OUTCOME9)
                saveOrder = setSaveOrder(merged);

              if (before.getDiscontinuedDate() == null
                  || newOrder.getStartDate().before(before.getDiscontinuedDate())
                  || newOrder.getStartDate().equals(before.getDiscontinuedDate())) { // 2
                if (regimenComponentIsTheSameAsDrugOrderExcludingDates(before, newOrder)) {
                  // (OUTCOME10)
                  before.setDiscontinuedDate(newOrder.getDiscontinuedDate());
                  before.setAutoExpireDate(newOrder.getAutoExpireDate());
                  before.setPrn(newOrder.getPrn());
                  before.setInstructions(newOrder.getInstructions());
                  os.saveOrder(before);
                  saveOrder = false;
                  newOrder.setOrderId(before.getOrderId());
                  merged = true;
                } else {
                  // (OUTCOME11)
                  before.setDiscontinuedDate(newOrder.getStartDate());
                  os.saveOrder(before);
                  saveOrder = setSaveOrder(merged);
                }
              }
            }
          }
        }
        if (saveOrder) os.saveOrder(newOrder);
      }
    }
  }