Skip to main content
Version: 3.1

Supply Delivery

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​

ModelPurpose
SupplyDeliveryOne delivered item: a quantity of a product or of a stock record, on a delivery order
DeliveryOrderOne 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​

FieldTypeNotes
supplied_itemFK → Product, nullable, CASCADEThe 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_itemFK → InventoryItem, nullable, CASCADEThe 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_quantityDecimalField, max_digits=20, decimal_places=6, nullableRequired on create: a whole number of at least 1. Returned as an int. Cannot change after create
supplied_item_pack_quantityIntegerField, nullable, default NoneOptional, at least 1. Number of packs
supplied_item_pack_sizeIntegerField, nullable, default NoneOptional, at least 1. Units per pack. With both pack fields, the quantity becomes pack quantity times pack size
supplied_item_conditionCharField(255)Optional. SupplyDeliveryConditionOptions. Editable on update. Has no effect on stock

Status and classification​

FieldTypeNotes
statusCharField(255)SupplyDeliveryStatusOptions. Required on create and in every update. See "Delivered item rules"
delivery_typeCharField(255)SupplyDeliveryTypeOptions exists, but no spec has the field, so the API never sets or returns it
FieldTypeNotes
supply_requestFK → SupplyRequest, nullable, CASCADEThe requested item this delivered item answers. Optional, looked up by id only. Cannot change after create
orderFK → DeliveryOrder, nullable, CASCADENullable in the database, but required on create. Not in the update spec, so it cannot change
total_purchase_priceDecimalField, max_digits=20, decimal_places=6, nullableOptional. Editable on update
extensionsJSONField, 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​

ValueLabelNotes
in_progressIn ProgressEvery item of an internal delivery is saved with this status on create, whatever status was sent
completedCompletedReceived. Final for status changes
abandonedAbandonedFinal for status changes
entered_in_errorEntered in ErrorFinal for status changes

There is no draft status for a delivered item.

SupplyDeliveryConditionOptions values​

ValueLabel
normalNormal
damagedDamaged

SupplyDeliveryTypeOptions values​

ValueNotes
productNot reachable through the API
deviceNot reachable through the API

DeliveryOrder fields​

The fields of the order decide its kind. The supplier does not.

KindDecided byCreate check at
Medication returnpatient is setThe destination
Internal deliveryorigin is set, no patientThe origin
Purchase deliveryNo patient, no originThe destination

Updates of an internal delivery are checked at its origin, and updates of a purchase delivery at its destination. See "Authorization".

FieldTypeNotes
nameCharField(255)Required on create and on update
statusCharField(255)Spec-bound to SupplyDeliveryOrderStatusOptions. Required on create and on update. Create allows only draft or pending
noteTextField, nullableOptional (str | None)
tagsArrayField(IntegerField), default listTags of resource type supply_delivery_order. Saved on create from the request body. Changed later with set_tags and remove_tags
tags_dataJSONField, default {}
supplierFK → Organization, nullable, CASCADEOptional. Must be an organization of type product_supplier, else "not found". Cannot change after create
originFK → 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
destinationFK → FacilityLocation, CASCADE, related_name="destination_delivery_orders"Required on create. Cannot change after create
extensionsJSONField, default {}Checked by each registered extension handler; unknown keys are dropped
patientFK → Patient, nullable, PROTECTOptional. Set for a medication return: stock that comes back from the patient. Cannot be given with origin. Cannot change after create
patient_invoiceFK → Invoice, nullable, PROTECTOptional

SupplyDeliveryOrderStatusOptions values​

ValueLabelNotes
draftDraftAllowed on create. Care's frontend creates every delivery order with this status
pendingPendingAllowed on create
in_progress—Not in the status list of Care's frontend
completedCompletedNeeds every delivered item in completed, abandoned or entered_in_error. From completed, the order can move only to abandoned or entered_in_error
abandonedAbandonedFinal for status changes. Not allowed for a medication return
entered_in_errorEntered in ErrorFinal for status changes

SUPPLY_DELIVERY_ORDER_COMPLETED_STATUSES is defined in the spec file, but no code uses it.

RelationNotes
SupplyDelivery.order → DeliveryOrderThe delivery that holds the item. Both models are described on this page
SupplyDelivery.supplied_item → ProductSee Product
SupplyDelivery.supplied_inventory_item → InventoryItemSee Inventory Item
SupplyDelivery.supply_request → SupplyRequestThe only link from a delivery to a request order. See Supply Request
DeliveryOrder.supplier → OrganizationSee Organization
DeliveryOrder.origin, destination → FacilityLocationSee Location
DeliveryOrder.patient → PatientSee Patient
DeliveryOrder.patient_invoice → InvoiceSee Invoice

Resource specs (API schema)​

SupplyDelivery specs​

Spec classUsed forFields and behaviour
BaseSupplyDeliverySpecShared baseid (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"]
SupplyDeliveryWriteSpecCreateBase 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
SupplyDeliveryUpdateSpecUpdate (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
SupplyDeliveryReadSpecReadsupplied_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
SupplyDeliveryRetrieveSpecRead, one itemSupplyDeliveryReadSpec plus created_by and updated_by (UserSpec). Mixes in ExtensionRetrieveRenderer

DeliveryOrder specs​

Spec classUsed forFields and behaviour
BaseSupplyDeliveryOrderSpecShared 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
SupplyDeliveryOrderWriteSpecCreate (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.

EventPurchase deliveryInternal delivery
Item created as in_progressNo changeThe origin's stock drops by the quantity at once. The destination does not change
Item moves to completedThe destination's stock rises by the quantityThe destination's stock rises by the quantity. The origin stays lower
in_progress item moves to abandoned or entered_in_errorNo changeThe quantity goes back to the origin. The destination does not change
Order moves to completedNo changeNo change
Order moves to abandoned or entered_in_errorEach completed item's quantity leaves the destinationEach 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​

ActionRequestNotes
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 tagsPOST …/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 upsertPOST /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​

ParameterBehaviour
origin, destinationAt least one is required ("Origin or destination is required"). Needs "Can Read Supply Delivery" at each location given
include_childrenWith true, the list also takes orders of locations below the given one
patient_returnOrders with a patient are left out unless patient_return=true
statusOne or more statuses
created_date_after, created_date_beforeA range on the created date
supplier, created_by, origin_isnull, patient, patient_isnull, tagsMatch 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​

ParameterBehaviour
orderRequired ("No filters provided"). Used by the scope check only
statusOne or more statuses
supply_requestThe requested item
origin, destination, include_children, request_orderDeclared 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​

PermissionSlugDefault roles
"Can Create Supply Delivery on Facility"can_write_supply_deliveryFacility Admin
"Can Read Supply Delivery"can_read_supply_deliveryFacility Admin, Administrator, Staff, Doctor, Nurse, Volunteer, Pharmacist
"Can Create External Supply Delivery on Facility"can_write_external_supply_deliveryFacility 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".

ActionCare checksRefusal
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 orderWithout 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 itemPatient 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​

MessageCause
"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