Technical reference for the SupplyDelivery and DeliveryOrder models in Care EMR. For what a delivery is, see the Purchase Delivery and Internal Delivery concepts.
A DeliveryOrder is one delivery. One model holds three kinds: purchase deliveries from a supplier, internal deliveries between locations of one facility, and patient medication returns. A SupplyDelivery is one delivered item of a delivery order.
Delivered items move stock. Care calculates the stock of a location from the delivered items it received, the internal delivered items sent from it, and dispenses. See Inventory Item and "Stock effects" below.
A delivery order has no field that links it to a purchase order or stock request. Each delivered item can point at one requested item instead.
Source:
The Django model is the storage layer. status, delivery_type and supplied_item_condition are plain CharFields, and extensions is a JSONField. The enum values, the validation and the read and write schemas are in the Pydantic resource specs.
Models
| Model | Purpose |
|---|
SupplyDelivery | One delivered item: a quantity of a product or of a stock record, on a delivery order |
DeliveryOrder | One delivery: a purchase delivery, an internal delivery or a medication return |
Both extend EMRBaseModel. That page describes the inherited fields.
SupplyDelivery fields
Delivered item and quantity
| Field | Type | Notes |
|---|
supplied_item | FK → Product, nullable, CASCADE | The product delivered. Required when the order has no origin; not allowed when it has one. Must belong to the facility of the order's destination |
supplied_inventory_item | FK → InventoryItem, nullable, CASCADE | The stock record. Required when the order has an origin: a record at the origin, or at a location below it, in the origin's facility. For an item on an order without origin, Care sets it to the destination's stock record |
supplied_item_quantity | DecimalField, max_digits=20, decimal_places=6, nullable | Required on create: a whole number of at least 1. Returned as an int. Cannot change after create |
supplied_item_pack_quantity | IntegerField, nullable, default None | Optional, at least 1. Number of packs |
supplied_item_pack_size | IntegerField, nullable, default None | Optional, at least 1. Units per pack. With both pack fields, the quantity becomes pack quantity times pack size |
supplied_item_condition | CharField(255) | Optional. SupplyDeliveryConditionOptions. Editable on update. Has no effect on stock |
Status and classification
| Field | Type | Notes |
|---|
status | CharField(255) | SupplyDeliveryStatusOptions. Required on create and in every update. See "Delivered item rules" |
delivery_type | CharField(255) | SupplyDeliveryTypeOptions exists, but no spec has the field, so the API never sets or returns it |
Links and pricing
| Field | Type | Notes |
|---|
supply_request | FK → SupplyRequest, nullable, CASCADE | The requested item this delivered item answers. Optional, looked up by id only. Cannot change after create |
order | FK → DeliveryOrder, nullable, CASCADE | Nullable in the database, but required on create. Not in the update spec, so it cannot change |
total_purchase_price | DecimalField, max_digits=20, decimal_places=6, nullable | Optional. Editable on update |
extensions | JSONField, default {} | Checked by each registered extension handler; unknown keys are dropped. Editable on update |
SupplyDelivery enum values
The labels are the ones Care's frontend shows.
SupplyDeliveryStatusOptions values
| Value | Label | Notes |
|---|
in_progress | In Progress | Every item of an internal delivery is saved with this status on create, whatever status was sent |
completed | Completed | Received. Final for status changes |
abandoned | Abandoned | Final for status changes |
entered_in_error | Entered in Error | Final for status changes |
There is no draft status for a delivered item.
SupplyDeliveryConditionOptions values
| Value | Label |
|---|
normal | Normal |
damaged | Damaged |
SupplyDeliveryTypeOptions values
| Value | Notes |
|---|
product | Not reachable through the API |
device | Not reachable through the API |
DeliveryOrder fields
The fields of the order decide its kind. The supplier does not.
| Kind | Decided by | Create check at |
|---|
| Medication return | patient is set | The destination |
| Internal delivery | origin is set, no patient | The origin |
| Purchase delivery | No patient, no origin | The destination |
Updates of an internal delivery are checked at its origin, and updates of a purchase delivery at its destination. See "Authorization".
| Field | Type | Notes |
|---|
name | CharField(255) | Required on create and on update |
status | CharField(255) | Spec-bound to SupplyDeliveryOrderStatusOptions. Required on create and on update. Create allows only draft or pending |
note | TextField, nullable | Optional (str | None) |
tags | ArrayField(IntegerField), default list | Tags of resource type supply_delivery_order. Saved on create from the request body. Changed later with set_tags and remove_tags |
tags_data | JSONField, default {} | |
supplier | FK → Organization, nullable, CASCADE | Optional. Must be an organization of type product_supplier, else "not found". Cannot change after create |
origin | FK → FacilityLocation, nullable, CASCADE, related_name="origin_delivery_orders" | Optional. Set for an internal delivery. Must differ from the destination and be in the same facility. Cannot be given with patient. Cannot change after create |
destination | FK → FacilityLocation, CASCADE, related_name="destination_delivery_orders" | Required on create. Cannot change after create |
extensions | JSONField, default {} | Checked by each registered extension handler; unknown keys are dropped |
patient | FK → Patient, nullable, PROTECT | Optional. Set for a medication return: stock that comes back from the patient. Cannot be given with origin. Cannot change after create |
patient_invoice | FK → Invoice, nullable, PROTECT | Optional |
SupplyDeliveryOrderStatusOptions values
| Value | Label | Notes |
|---|
draft | Draft | Allowed on create. Care's frontend creates every delivery order with this status |
pending | Pending | Allowed on create |
in_progress | — | Not in the status list of Care's frontend |
completed | Completed | Needs every delivered item in completed, abandoned or entered_in_error. From completed, the order can move only to abandoned or entered_in_error |
abandoned | Abandoned | Final for status changes. Not allowed for a medication return |
entered_in_error | Entered in Error | Final for status changes |
SUPPLY_DELIVERY_ORDER_COMPLETED_STATUSES is defined in the spec file, but no code uses it.
| Relation | Notes |
|---|
SupplyDelivery.order → DeliveryOrder | The delivery that holds the item. Both models are described on this page |
SupplyDelivery.supplied_item → Product | See Product |
SupplyDelivery.supplied_inventory_item → InventoryItem | See Inventory Item |
SupplyDelivery.supply_request → SupplyRequest | The only link from a delivery to a request order. See Supply Request |
DeliveryOrder.supplier → Organization | See Organization |
DeliveryOrder.origin, destination → FacilityLocation | See Location |
DeliveryOrder.patient → Patient | See Patient |
DeliveryOrder.patient_invoice → Invoice | See Invoice |
Resource specs (API schema)
SupplyDelivery specs
| Spec class | Used for | Fields and behaviour |
|---|
BaseSupplyDeliverySpec | Shared base | id (optional), status (required), supplied_item_condition (optional), total_purchase_price (optional, max_digits=20, decimal_places=6). __exclude__ = ["supplied_item", "supply_request", "supplied_inventory_item"] |
SupplyDeliveryWriteSpec | Create | Base fields plus supplied_item_quantity (required), supplied_item_pack_quantity, supplied_item_pack_size, supplied_item, supplied_inventory_item, supply_request (optional), order (required) and extensions. Mixes in ExtensionValidator |
SupplyDeliveryUpdateSpec | Update (pydantic_update_model) | ExtensionValidator plus the base fields: status, supplied_item_condition, total_purchase_price and extensions. It has no order, quantity, product, stock record or requested item |
SupplyDeliveryReadSpec | Read | supplied_item_quantity as an int, pack quantity and pack size, extensions, created_date, modified_date, and nested supplied_item, supplied_inventory_item (InventoryItemReadSpec), supply_request and order objects. Mixes in ExtensionListRenderer |
SupplyDeliveryRetrieveSpec | Read, one item | SupplyDeliveryReadSpec plus created_by and updated_by (UserSpec). Mixes in ExtensionRetrieveRenderer |
DeliveryOrder specs
| Spec class | Used for | Fields and behaviour |
|---|
BaseSupplyDeliveryOrderSpec | Shared base, and the update body (pydantic_update_model) | Mixes in ExtensionValidator. id (optional), status, name, note (optional) and extensions. It has no supplier, origin, destination or patient |
SupplyDeliveryOrderWriteSpec | Create (pydantic_model) | Base fields plus supplier, origin, patient (optional) and destination (required) |
Validation
Delivery order create
destination is required. supplier, origin and patient are optional.
- A
supplier that is not of type product_supplier fails with "not found".
patient and origin together fail with "Patient and origin cannot be provided together".
- An
origin equal to the destination is refused.
- An
origin in another facility than the destination fails with HTTP 403 and "Origin and destination must be in the same facility".
status must be draft or pending: "Status must be draft or pending on create".
Delivery order update
- The update body has only
status, name, note and extensions. A supplier, origin, destination or patient in an update is ignored without an error.
- Care's frontend edit form sends supplier, origin, destination, tag IDs, the current status and extensions. The server applies only the fields above.
Delivered item create
status, supplied_item_quantity and order are required.
- With both pack fields,
supplied_item_quantity becomes pack quantity times pack size.
- If the order has an origin, the item must name a stock record: "supplied_inventory_item is required when origin is provided".
- If the order has no origin, the item must name a product: "supplied_item is required when origin is not provided".
- An item cannot name both: "supplied_item and supplied_inventory_item cannot both be provided".
- A product must belong to the facility of the order's destination. A stock record must be at a location of the origin's facility. Otherwise the request fails with "not found".
Delivered item update
- Only
status (required), supplied_item_condition, total_purchase_price and extensions can change.
Delivery order status rules
DeliveryOrderViewSet.perform_update runs these rules under DeliveryOrderLock.
- The lock does not wait. If another request holds the order row, the call fails with HTTP 423 "The resource you are trying to access is locked".
- An
abandoned or entered_in_error order cannot change status: "Delivery order already abandoned or entered in error".
- A
completed order can move only to abandoned or entered_in_error. Any other change fails with "Completed delivery order can only be cancelled".
- A move to
completed needs every delivered item in completed, abandoned or entered_in_error: "Finalise Deliveries before completing order". An order with no items passes.
- Completing a purchase or internal delivery changes nothing else: it does not complete items and does not recalculate stock. Only an order with a patient gets a return invoice at that point (
generate_return_invoice).
- For an order without a patient, a move to
abandoned or entered_in_error sets every item that is not already abandoned or entered_in_error to the order's new status. completed items change too.
- That move also reverses the stock of those items. See "Stock effects". If the destination no longer holds enough stock, the change fails with "Inventory item does not have enough stock", and nothing changes.
- An order with a patient cannot move to
abandoned: "Cannot abandon a delivery order with medication return".
- The rules run only when the status changes. An
abandoned, entered_in_error or completed order still accepts updates of name, note and extensions.
Delivered item rules
- An item cannot be added to an order that is
abandoned or entered_in_error ("Delivery order is abandoned or entered in error") or completed ("Delivery order is completed"). Orders in draft, pending and in_progress accept new items.
- An item of an internal delivery is always saved as
in_progress, whatever status was sent. An item of a purchase delivery keeps the status sent, so an item created as completed through the API adds stock at once.
- For an internal delivery, the stock record must be at the origin or at a location below it ("Supplied inventory item is not part of the origin or its children"). It must hold at least the quantity ("Insufficient stock"). The stock already counts items in transit, so the same stock cannot be sent twice.
- For an item of a purchase delivery, Care makes sure the destination has a stock record for the product, with zero stock if new, and links the item to it. For an internal item, Care also creates the destination's stock record for the same product if it is missing.
- No item of an
abandoned or entered_in_error order can be updated: "Delivery order is abandoned or entered in error".
- An item's status cannot change once it is
abandoned or entered_in_error ("Supply delivery is abandoned or entered in error") or completed ("Supply delivery already completed").
- A received item can be reversed only by moving the whole order to
abandoned or entered_in_error.
- Items of a
completed order can still be updated. The condition and price of a completed item stay editable.
- Item create takes the order lock without waiting. If the order is locked, the create fails with "Supply delivery creation failed . try again after a while".
- A delivered item can never be deleted. It can only be set to
abandoned or entered_in_error.
Stock effects
Care recalculates stock after every item create and update, whatever the status, and when an order moves to abandoned or entered_in_error.
| Event | Purchase delivery | Internal delivery |
|---|
Item created as in_progress | No change | The origin's stock drops by the quantity at once. The destination does not change |
Item moves to completed | The destination's stock rises by the quantity | The destination's stock rises by the quantity. The origin stays lower |
in_progress item moves to abandoned or entered_in_error | No change | The quantity goes back to the origin. The destination does not change |
Order moves to completed | No change | No change |
Order moves to abandoned or entered_in_error | Each completed item's quantity leaves the destination | Each completed item's quantity leaves the destination. Each in_progress or completed item's quantity goes back to the origin |
- The condition has no effect on stock. An item received as
damaged counts like a normal one.
- A medication return works like a purchase delivery for stock: its
completed items add to the destination's stock.
API integration notes
Endpoints
| Action | Request | Notes |
|---|
| Delivery order: create, list, retrieve, update, upsert | /api/v1/facility/{facility}/order/delivery/ | DeliveryOrderViewSet. Care's frontend creates with POST and status draft, and sends every status change as PUT …/order/delivery/{id}/ with the whole order |
| Delivery order tags | POST …/order/delivery/{id}/set_tags/, POST …/order/delivery/{id}/remove_tags/ | Run the order's update check |
| Delivered item: create, list, retrieve, update, upsert | /api/v1/supply_delivery/ | SupplyDeliveryViewSet, outside the facility URL |
| Delivered item upsert | POST /api/v1/supply_delivery/upsert/ | At most MAX_DATAPOINTS_PER_UPSERT items (default 100). Items with an id go through the normal update. If one item fails, nothing is saved. Care's frontend receives items with it |
Neither API has a delete.
Filters
Delivery order list
| Parameter | Behaviour |
|---|
origin, destination | At least one is required ("Origin or destination is required"). Needs "Can Read Supply Delivery" at each location given |
include_children | With true, the list also takes orders of locations below the given one |
patient_return | Orders with a patient are left out unless patient_return=true |
status | One or more statuses |
created_date_after, created_date_before | A range on the created date |
supplier, created_by, origin_isnull, patient, patient_isnull, tags | Match on that field |
- The list returns only orders whose destination is in the facility of the URL.
- The list sorts by created date or modified date. It has no priority filter, and an unknown parameter is ignored.
Delivered item list
| Parameter | Behaviour |
|---|
order | Required ("No filters provided"). Used by the scope check only |
status | One or more statuses |
supply_request | The requested item |
origin, destination, include_children, request_order | Declared as dummy filters that do nothing. request_order filters nothing |
The delivered item list also filters by product, by the stock record's product, by catalog entry, by origin_isnull and by supplier.
Authorization
| Permission | Slug | Default roles |
|---|
| "Can Create Supply Delivery on Facility" | can_write_supply_delivery | Facility Admin |
| "Can Read Supply Delivery" | can_read_supply_delivery | Facility Admin, Administrator, Staff, Doctor, Nurse, Volunteer, Pharmacist |
| "Can Create External Supply Delivery on Facility" | can_write_external_supply_delivery | Facility Admin |
| "Can Read Medication Returns" | — | Facility Admin, Administrator, Staff, Doctor, Nurse, Volunteer, Pharmacist |
| "Can Write Medication Returns" | — | Facility Admin |
Every check needs the permission in one of the location's organizations. These are the departments linked to the location with their parent departments, the parent location's organizations, and the facility's root organization "Administration".
| Action | Care checks | Refusal |
|---|
| Create an order with a patient | "Can Write Medication Returns" at the destination | "Cannot write medication return orders" |
| Create an internal delivery | "Can Create Supply Delivery on Facility" at the origin | |
| Create a purchase delivery | "Can Create External Supply Delivery on Facility" at the destination | "Cannot write delivery orders" |
| Update an internal delivery: edit, every status change, tags | "Can Create Supply Delivery on Facility" at the origin only | "Cannot write delivery orders" |
| Update a purchase delivery: edit, every status change, tags | "Can Create External Supply Delivery on Facility" at the destination | "Cannot write delivery orders" |
| Retrieve an order | Without a patient: "Can Read Supply Delivery" at the origin or the destination. With a patient: "Can Read Medication Returns" at the destination. The order is looked up across all facilities; the check on its own locations still applies | "Cannot read delivery orders" |
| List orders | "Can Read Supply Delivery" at each location given | "Cannot list delivery orders" |
| Create or update an item | Patient order: "Can Write Medication Returns" at the destination. Internal: "Can Create Supply Delivery on Facility" at the origin or the destination. Purchase: "Can Create External Supply Delivery on Facility" at the destination | "Cannot write supply requests" (sic) |
| Retrieve or list items | "Can Read Supply Delivery" at the order's origin or destination; the medication return permission for a patient order | "Cannot read supply requests" (sic) |
- The receiving location of an internal delivery can add and receive items with the permission held only there. It cannot approve or complete the order: those are order updates, checked at the origin only.
- A role held in "Administration" counts at every location of the facility, so it passes both checks.
- The role lists are defaults. The Admin role of the permission files is left out: role sync leaves it with the responsibility-organization context only.
Errors
| Message | Cause |
|---|
| "Status must be draft or pending on create" | A delivery order created with another status |
| "Patient and origin cannot be provided together" | Create with both patient and origin |
| "Origin and destination must be in the same facility" | HTTP 403. The origin is in another facility |
| "Delivery order already abandoned or entered in error" | A status change on an abandoned or entered_in_error order |
| "Completed delivery order can only be cancelled" | A completed order moved to a status other than abandoned or entered_in_error |
| "Finalise Deliveries before completing order" | A move to completed while an item is still in_progress |
| "Inventory item does not have enough stock" | An order cancel that would take more stock from the destination than it holds |
| "Cannot abandon a delivery order with medication return" | A move to abandoned for an order with a patient |
| "The resource you are trying to access is locked" | HTTP 423. Another request holds the order lock |
| "Supply delivery creation failed . try again after a while" | Item create while the order is locked |
| "supplied_inventory_item is required when origin is provided" | An internal item without a stock record |
| "supplied_item is required when origin is not provided" | An item without a product on an order without origin |
| "supplied_item and supplied_inventory_item cannot both be provided" | An item with both |
| "Delivery order is abandoned or entered in error" | Item create or update on an abandoned or entered_in_error order |
| "Delivery order is completed" | Item create on a completed order |
| "Supplied inventory item is not part of the origin or its children" | An internal item whose stock record is not at the origin or below it |
| "Insufficient stock" | An internal item for more than the stock record holds |
| "Supply delivery is abandoned or entered in error" | A status change of an abandoned or entered_in_error item |
| "Supply delivery already completed" | A status change of a completed item |
| "Origin or destination is required" | An order list without origin or destination |
| "No filters provided" | An item list without order |
| "Tag resource does not match resource type" | A tag not configured for supply_delivery_order |