Medication return
Technical reference for medication returns in Care EMR. A medication return records medicines that come back from a patient to a pharmacy location. Care has no separate model for it: a return is a DeliveryOrder with a patient and no origin, and each returned item is a SupplyDelivery in that order.
This page covers only what is specific to returns. For every field of the two models, see Supply delivery.
Source:
- Models:
care/emr/models/supply_delivery.py(DeliveryOrder,SupplyDelivery) - Specs:
care/emr/resources/inventory/supply_delivery/delivery_order.py·spec.py·cancel_delivery_order.py - Refund invoice:
care/emr/resources/invoice/return_items_invoice.py - Viewsets:
care/emr/api/viewsets/inventory/delivery_order.py·supply_delivery.py - Permissions:
care/security/permissions/supply_delivery.py·care/security/authorization/supply_delivery.py
Models
| Model | Role in a return |
|---|---|
DeliveryOrder | The return. patient is set, origin is empty, destination is the location that takes the medicines back |
SupplyDelivery | One returned item: a product (lot) and a quantity |
Invoice | The refund invoice that Care creates when the return is completed. See Invoice |
Both supply models extend EMRBaseModel.
A return does not point to the dispense that it reverses. care_fe can copy the product and the quantity of a dispense into a return item, but Care stores no link between them.
How a return is stored
DeliveryOrder fields for a return
| Field | Value in a return | Notes |
|---|---|---|
patient | Required | FK → Patient, PROTECT. Create only. Its presence makes the order a return |
origin | Empty | Care refuses patient and origin together |
supplier | Empty in care_fe | The spec does not forbid it |
destination | Required | FK → FacilityLocation. Create only. The location that takes the medicines back |
name | Required | CharField(255) |
note | Optional | TextField, nullable |
status | Required | draft or pending on create. See Status rules |
patient_invoice | Server-set | FK → Invoice, PROTECT. The refund invoice. Read as patient_invoice_id. An update cannot change it |
extensions | Optional | Additional fields, validated by ExtensionValidator |
SupplyDelivery fields for a returned item
| Field | Value in a return | Notes |
|---|---|---|
order | Required | The return's DeliveryOrder. Create only |
supplied_item | Required | FK → Product: the lot that comes back. Must be a product of the destination's facility |
supplied_inventory_item | Not sent | Care refuses it in the request. Care sets it to the stock row of the product at the destination, and creates that row with zero stock if it does not exist |
supplied_item_quantity | Required | Whole number, at least 1. If supplied_item_pack_quantity and supplied_item_pack_size are both set, Care replaces it with their product |
status | Required | in_progress, completed, abandoned or entered_in_error. The client sets it, also on create |
supplied_item_condition | Optional | normal or damaged |
Status rules
DeliveryOrder.status uses SupplyDeliveryOrderStatusOptions: draft, pending, in_progress, completed, abandoned, entered_in_error.
| Change | Rule for a return |
|---|---|
| Create | Status must be draft or pending |
Any change from abandoned or entered_in_error | Refused |
From completed | Only to entered_in_error. The general rule also allows abandoned, but a return cannot be abandoned |
To abandoned | Refused: Cannot abandon a delivery order with medication return |
To completed | Every item must be completed, abandoned or entered_in_error. Then Care creates the refund invoice |
To entered_in_error | Care cancels the refund invoice and the items (see below) |
A returned item cannot change status after it is completed, abandoned or entered_in_error. Care refuses a new item when the order is completed, abandoned or entered_in_error.
Stock
- An item adds stock at the destination only while its status is
completed. Stock of a product at a location is the sum of completed deliveries into it, less outgoing deliveries and dispenses (sync_inventory_item). - Care recalculates the stock of the destination row after each create and update of an item.
Refund invoice (order to completed)
generate_return_invoice() runs under a lock on the order and on the patient's account:
- If the order already has
patient_invoice, Care keeps it and stops. - Care takes the patient's default account at the destination's facility.
- Care creates an
Invoicewithis_refund = true,issue_datenow, and a number from the facility's invoice number expression. - For each
completeditem whose product has a charge item definition, Care applies that definition in reverse for the item's quantity. Each charge item gets statusbilledand the invoice aspaid_invoice. Items whose product has no charge item definition get no charge. - Care syncs the invoice totals, sets the invoice to
issued, and links it to the order. - Care queues a rebalance of the account.
The invoice takes created_by and updated_by from the order. Care checks no billing permission.
Entered in error (order to entered_in_error)
cancel_return_invoice() runs under a lock on the order:
- If the order has a refund invoice, Care sets the invoice to
cancelled. Its charge items becomeentered_in_error, withpaid_invoiceandpaid_oncleared. Care queues a rebalance of the account. - Care sets every item that is not already
abandonedorentered_in_errortoentered_in_error, and takes the completed quantities out of the destination's stock. - If the destination does not have enough stock left, Care refuses the change:
Inventory item does not have enough stock.
Resource specs (API schema)
A return uses the delivery order and supply delivery specs. See Supply delivery for the full spec tables.
| Spec | Use in a return |
|---|---|
SupplyDeliveryOrderWriteSpec | Create the return: name, status, note, destination, patient |
BaseSupplyDeliveryOrderSpec | Update the return: name, status, note and extensions only |
SupplyDeliveryOrderReadSpec / SupplyDeliveryOrderRetrieveSpec | Read the return. patient is a PatientListSpec object. patient_invoice_id is the refund invoice |
SupplyDeliveryWriteSpec | Add an item: order, supplied_item, supplied_item_quantity, status |
SupplyDeliveryUpdateSpec | Change an item: status, supplied_item_condition, total_purchase_price |
API integration notes
- Returns:
/api/v1/facility/{facility_external_id}/order/delivery/. Items:/api/v1/supply_delivery/. Neither viewset has a delete. - To list returns, send
patient_return=trueanddestination. Withoutpatient_return=true, the list leaves returns out. A return list needsdestination;originalone is refused. Other filters:status(comma-separated),patient,created_daterange,include_children. Ordering:created_date,modified_date. - To list the items of a return, send
order={return_id}to/api/v1/supply_delivery/. - Read (return or item) needs
can_read_medication_returnin an organization of the destination'sfacility_organization_cache. Write (create, update, add or change items, status changes) needscan_write_medication_returnthere. The supply delivery permissions do not apply to returns. - Default roles with
can_write_medication_return: Facility Admin. Default roles withcan_read_medication_return: Facility Admin, Administrator, Staff, Doctor, Nurse, Volunteer, Pharmacist.
Related
- Reference: Supply delivery
- Reference: Medication dispense
- Reference: Invoice
- Reference: Charge item
- Reference: Account
- Reference: Inventory item
- Reference: Product
- Source (refund invoice): return_items_invoice.py on GitHub