Skip to main content
Version: 3.1

Dispense order

Technical reference for the DispenseOrder module in Care EMR. For what a dispense order is in the product, see the Dispense order concept.

A dispense order groups medication dispenses for one patient at one location of a facility. Dispenses can join an order in any status except abandoned and entered_in_error.

Source:

The Django model is the storage layer and the Pydantic specs are the API layer. The model stores tags and tags_data, but no spec reads or writes them.

Models​

ModelPurpose
DispenseOrderGroups dispenses for one patient at one location of a facility
MedicationDispenseEach dispense points to at most one order through MedicationDispense.order. See the Medication dispense reference

DispenseOrder extends EMRBaseModel. It supplies external_id, created_date, modified_date, the soft-delete flag deleted, created_by, updated_by, history and meta.

DispenseOrder fields​

In the table below, the model type is the Django column and the spec type is what the API reads and writes.

FieldModel typeSpec typeRequired (API)Notes
statusCharField(255)MedicationDispenseOrderStatusOptionsYesSee status rules
nameCharField(255), nullablestr | NoneNoThe backend has no naming rule. The encounter's Dispense History shows an order with no name by its creation date and time
noteTextField, nullablestr | NoneNo
patientFK → Patient, CASCADEUUID4 (create)Yes (create)Looked up by id. Cannot change after create
locationFK → FacilityLocation, CASCADEUUID4 (create)Yes (create)Looked up by id. Cannot change after create
facilityFK → Facility, CASCADENot acceptedServer-setThe facility of the URL on a direct create; the encounter's facility on an inline create
alternate_identifierCharField(100), nullableNot in the order specsn/aSet only by create_dispense_order on a dispense. Not returned
tagsArrayField[int], default []Not in any specn/aThe API has no tag actions and does not return tags
tags_dataJSONField, default {}Not in any specn/aAdded in the 3.1 release. Not returned

The unique_alternate_identifier_encounter_location constraint keeps alternate_identifier unique per patient and location. Despite its name, it has no encounter field.

Enum values​

MedicationDispenseOrderStatusOptions values​

Value
draft
in_progress
completed
abandoned
entered_in_error

MEDICATION_DISPENSE_ORDER_COMPLETED_STATUSES lists completed, abandoned and entered_in_error. The update rules below decide which changes Care allows.

CreateDispenseOrderStatusOptions values​

The statuses allowed when a dispense creates its order inline.

Value
draft (default)
in_progress

Status rules​

The rules apply only when an update changes the status. Name and note can change in any status.

Current statusAllowed new statusRefusal
draft, in_progressAny, including completedNone
completedabandoned or entered_in_error"Completed dispense order can only be cancelled"
abandoned, entered_in_errorNone"Dispense order already abandoned or entered in error"

completed is not final: it can still move to abandoned or entered_in_error. abandoned and entered_in_error are final for the status only.

MedicationDispense​

MedicationDispense.order → FK DispenseOrder (CASCADE, nullable)

How a dispense joins an order:

  • With create_dispense_order, Care looks for an order with the same alternate_identifier, patient and location, and creates one when none matches. A match that is not draft or in_progress fails with "Prescription is not active".
  • With order, the dispense joins that existing order.
  • An update can move a dispense to another order. Care locks both orders, and neither can be abandoned or entered_in_error.
  • Care refuses to add a dispense to an abandoned or entered_in_error order, or to move one into or out of it. The error is "Dispense order is abandoned or entered in error".

Resource specs (API schema)​

Spec classRoleNotes
BaseMedicationDispenseOrderSpecshared · write · updateid, status (required), name, note. The viewset uses it as the update spec, so an update changes only these
MedicationDispenseOrderWriteSpecwrite · createAdds patient and location (both required). No alternate_identifier and no tags
MedicationDispenseOrderReadSpecread · liststatus, name, note, patient (list form), location (list form), created_date, modified_date
MedicationDispenseOrderRetrieveSpecread · detailAdds created_by, updated_by and the patient in its retrieve form, with the patient's identifiers for the order's facility
CreateDispenseOrderwrite · nested (dispense spec)name, note, alternate_identifier (required), status (draft or in_progress, default draft)

Methods & save behaviour​

The model defines no save() or delete() override. The behaviour below lives in the specs and the viewsets.

  • A direct create takes its facility from the URL.
  • An inline create from a dispense takes the encounter's facility, the given status, name and note, and records the user as its creator.
  • An order cannot be created as completed inline. A direct create accepts any status.
  • Moving an order to abandoned sets each of its dispenses to cancelled. Moving it to entered_in_error sets each to entered_in_error.
  • For each of those dispenses, Care cancels the charge item (aborted), sets the medication request's dispense_status to incomplete and clears the request link.
  • If a charge item is on an invoice that is not a draft, the whole update fails with "Cannot cancel charge item in a non-draft invoice".
  • Care then recalculates the stock of every inventory item involved, so the quantities return to stock.
  • Moving an order to completed changes nothing else: not the dispenses, not stock, not billing.

API integration notes​

Endpoints​

All paths are under /api/v1/. List, retrieve and update find orders only in the facility in the URL.

MethodPathAction
GETfacility/{facility_id}/order/dispense/List
POSTfacility/{facility_id}/order/dispense/Create
GETfacility/{facility_id}/order/dispense/{id}/Retrieve
PUT, PATCHfacility/{facility_id}/order/dispense/{id}/Update. Both use BaseMedicationDispenseOrderSpec
POSTfacility/{facility_id}/order/dispense/upsert/Batch create or update

There is no delete and no tag action.

Authorization​

The order API uses "Pharmacist in Care" for the facility, or the supply delivery permissions for the order's location.

ActionRequirement
List"Pharmacist in Care" in any department of the facility: every order of the facility, and the location filter is not applied. Others must send ?location= (a location of that facility) and hold "Can Read Supply Delivery" there
List without location, not a pharmacistHTTP 400 "Location is required for non-pharmacists"
List with location, no permission thereHTTP 403 "You do not have permission to read dispense order"
Retrieve"Pharmacist in Care" in the facility, or "Can Read Supply Delivery" for the order's location. Else 403 "You do not have permission to read dispense order"
Create"Pharmacist in Care" in the facility, or "Can Create Supply Delivery on Facility" for the order's location. Else 403 "You do not have permission to create dispense order"
Update"Pharmacist in Care" in the order's facility, or "Can Create Supply Delivery on Facility" for the order's location. Else 403 "You do not have permission to update dispense order"
Inline create from a dispenseOnly "Write Medication Dispense" for the dispensing location, the permission that creates the dispense

"Can Read Supply Delivery" and "Can Create Supply Delivery on Facility" count when held in the location's departments. These are its parent location's departments, the departments it is linked to and their parents, and the facility's Administration department.

Default role lists from the permission definitions:

PermissionDefault roles
"Pharmacist in Care"Facility Admin, Pharmacist
"Can Read Supply Delivery"Facility Admin, Administrator, Staff, Doctor, Nurse, Volunteer, Pharmacist
"Can Create Supply Delivery on Facility"Facility Admin
"Write Medication Dispense"Facility Admin, Staff, Doctor, Nurse, Pharmacist

The permission definitions also name an Admin role. Roles are matched by name, and the only role named Admin in the database is for role organizations, which the screen that links users to departments does not offer. The lists above leave it out.

So by default Staff, Doctor and Nurse can create dispenses, and orders inline with them, but cannot change an order's status.

A permission refusal is HTTP 403 with the message in detail. A business-rule refusal is HTTP 400 with errors: [{type: "validation_error", msg}].

Filters and ordering​

ParameterMeaning
statusComma-separated statuses
created_dateRange of creation dates
patientPatient id
created_byId of the user who created the order
locationLocation id. Does nothing as a filter by itself; the list code applies it for non-pharmacists only
include_childrenWith location, also orders at the locations under it (non-pharmacists only)
orderingcreated_date or modified_date. Without it, newest record first

Lists are paginated with limit and offset (default 14, at most 200) and return count and results.

Notes​

  • The Care frontend's encounter Dispense drawer creates the order inline with the dispenses, sending only an identifier, so the order starts as draft. It then sends PUT to set the order to completed.
  • The pharmacy screens change an order's status with PATCH, in one batch with an upsert of its dispenses.
  • A medication return is a separate delivery order. It does not change the dispense order or its dispenses. See Medication dispense.