# 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](https://docs.ohc.network/references/supply/supply-delivery.md).

**Source:**

* Models: [`care/emr/models/supply_delivery.py`](https://github.com/ohcnetwork/care/blob/develop/care/emr/models/supply_delivery.py) (`DeliveryOrder`, `SupplyDelivery`)
* Specs: [`care/emr/resources/inventory/supply_delivery/delivery_order.py`](https://github.com/ohcnetwork/care/blob/develop/care/emr/resources/inventory/supply_delivery/delivery_order.py) · [`spec.py`](https://github.com/ohcnetwork/care/blob/develop/care/emr/resources/inventory/supply_delivery/spec.py) · [`cancel_delivery_order.py`](https://github.com/ohcnetwork/care/blob/develop/care/emr/resources/inventory/supply_delivery/cancel_delivery_order.py)
* Refund invoice: [`care/emr/resources/invoice/return_items_invoice.py`](https://github.com/ohcnetwork/care/blob/develop/care/emr/resources/invoice/return_items_invoice.py)
* Viewsets: [`care/emr/api/viewsets/inventory/delivery_order.py`](https://github.com/ohcnetwork/care/blob/develop/care/emr/api/viewsets/inventory/delivery_order.py) · [`supply_delivery.py`](https://github.com/ohcnetwork/care/blob/develop/care/emr/api/viewsets/inventory/supply_delivery.py)
* Permissions: [`care/security/permissions/supply_delivery.py`](https://github.com/ohcnetwork/care/blob/develop/care/security/permissions/supply_delivery.py) · [`care/security/authorization/supply_delivery.py`](https://github.com/ohcnetwork/care/blob/develop/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](https://docs.ohc.network/references/billing/invoice.md) |

Both supply models extend [`EMRBaseModel`](https://docs.ohc.network/references/foundation/base-model.md).

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](#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:

1. If the order already has `patient_invoice`, Care keeps it and stops.
2. Care takes the patient's default account at the destination's facility.
3. Care creates an `Invoice` with `is_refund = true`, `issue_date` now, and a number from the facility's invoice number expression.
4. For each `completed` item whose product has a charge item definition, Care applies that definition in reverse for the item's quantity. Each charge item gets status `billed` and the invoice as `paid_invoice`. Items whose product has no charge item definition get no charge.
5. Care syncs the invoice totals, sets the invoice to `issued`, and links it to the order.
6. 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:

1. If the order has a refund invoice, Care sets the invoice to `cancelled`. Its charge items become `entered_in_error`, with `paid_invoice` and `paid_on` cleared. Care queues a rebalance of the account.
2. Care sets every item that is not already `abandoned` or `entered_in_error` to `entered_in_error`, and takes the completed quantities out of the destination's stock.
3. 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](https://docs.ohc.network/references/supply/supply-delivery.md) 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=true` and `destination`. Without `patient_return=true`, the list leaves returns out. A return list needs `destination`; `origin` alone is refused. Other filters: `status` (comma-separated), `patient`, `created_date` range, `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_return` in an organization of the destination's `facility_organization_cache`. Write (create, update, add or change items, status changes) needs `can_write_medication_return` there. The supply delivery permissions do not apply to returns.
* Default roles with `can_write_medication_return`: Facility Admin. Default roles with `can_read_medication_return`: Facility Admin, Administrator, Staff, Doctor, Nurse, Volunteer, Pharmacist.

## Related

* Reference: [Supply delivery](https://docs.ohc.network/references/supply/supply-delivery.md)
* Reference: [Medication dispense](https://docs.ohc.network/references/medications/medication-dispense.md)
* Reference: [Invoice](https://docs.ohc.network/references/billing/invoice.md)
* Reference: [Charge item](https://docs.ohc.network/references/billing/charge-item.md)
* Reference: [Account](https://docs.ohc.network/references/billing/account.md)
* Reference: [Inventory item](https://docs.ohc.network/references/supply/inventory-item.md)
* Reference: [Product](https://docs.ohc.network/references/supply/product.md)
* Source (refund invoice): [return\_items\_invoice.py on GitHub](https://github.com/ohcnetwork/care/blob/develop/care/emr/resources/invoice/return_items_invoice.py)
