# Supply Request

Technical reference for the `SupplyRequest` model in Care EMR. A `SupplyRequest` is one requested item: a quantity of one catalog entry (`ProductKnowledge`), not a stocked batch, on one request order.

Each requested item belongs to a `RequestOrder`, which is a [purchase order](https://docs.ohc.network/concepts/supply/purchase-order.md) or a [stock request](https://docs.ohc.network/concepts/supply/supply-request.md). The order has its own reference: [Request Order](https://docs.ohc.network/references/supply/request-order.md).

**Source:**

* Model: [`care/emr/models/supply_request.py`](https://github.com/ohcnetwork/care/blob/develop/care/emr/models/supply_request.py)
* Spec: [`care/emr/resources/inventory/supply_request/spec.py`](https://github.com/ohcnetwork/care/blob/develop/care/emr/resources/inventory/supply_request/spec.py)
* Viewset: [`care/emr/api/viewsets/inventory/supply_request.py`](https://github.com/ohcnetwork/care/blob/develop/care/emr/api/viewsets/inventory/supply_request.py)

The Django model is the storage layer. `status` and `supplied_item_condition` are plain `CharField`s there. The allowed values, the validation and the read and write schemas are in the Pydantic resource specs.

## Models

| Model | Purpose |
| - | - |
| `SupplyRequest` | One requested item: a quantity of one catalog entry on a request order |
| `RequestOrder` | The purchase order or stock request that groups the requested items. Same model file; documented in [Request Order](https://docs.ohc.network/references/supply/request-order.md) |

## `SupplyRequest` fields

| Field | Type | Notes |
| - | - | - |
| `status` | `CharField(255)` | Spec-bound to `SupplyRequestStatusOptions`. Required on create and on update |
| `quantity` | `DecimalField`, `max_digits=20`, `decimal_places=6`, nullable | The write specs take a whole number of at least 1 (`decimal_places=0`, `ge=1`). The read spec returns an `int` |
| `supplied_item_condition` | `CharField(255)` | Model only. No spec reads or writes it, so the API never reaches it |
| `item` | `FK → ProductKnowledge`, `CASCADE` | The catalog entry, not a stocked batch. Required on create, given as a UUID. Not in the update spec, so it cannot change. Care does not check it against the order's facility |
| `order` | `FK → RequestOrder`, nullable, `CASCADE` | Required on create and on update, given as a UUID. The update resolves it again, so an update can move the item to another order |

### `SupplyRequestStatusOptions` values

Defined in `spec.py` and bound to `SupplyRequest.status`. The labels are the ones Care's frontend shows.

| Value | Label | Notes |
| - | - | - |
| `draft` | Draft | |
| `active` | Active | Care's frontend creates every new item with this status |
| `suspended` | Suspended | |
| `cancelled` | Cancelled | Set on every item when its order moves to `abandoned`. A `cancelled` item can still move back to `draft` or `active` |
| `processed` | Processed | |
| `completed` | Completed | Final for status changes |
| `entered_in_error` | Entered in Error | Set on every item when its order moves to `entered_in_error`. Final for status changes |

No delivery sets a requested item to `completed` or `processed`. The order cascade is the only writer of item status outside a direct edit.

## Related models

| Relation | Notes |
| - | - |
| `order → RequestOrder` | The order that holds the item. See [Request Order](https://docs.ohc.network/references/supply/request-order.md) |
| `item → ProductKnowledge` | The catalog entry. See [Product Knowledge](https://docs.ohc.network/references/definitions/product-knowledge.md) |
| `SupplyDelivery.supply_request → SupplyRequest` | A delivered item can point at one requested item. This is the only link between deliveries and request orders. See [Supply Delivery](https://docs.ohc.network/references/supply/supply-delivery.md) |

## Resource specs (API schema)

| Spec class | Used for | Fields and behaviour |
| - | - | - |
| `BaseSupplyRequestSpec` | Shared base | `id` (optional), `status: SupplyRequestStatusOptions`, `quantity: Decimal` (`max_digits=20`, `decimal_places=0`, `ge=1`). `__exclude__ = ["item"]` |
| `SupplyRequestWriteSpec` | Create | Base fields plus `item: UUID4` and `order: UUID4`. Resolves `item` to a `ProductKnowledge` and `order` to a `RequestOrder` with `get_object_or_404` |
| `SupplyRequestUpdateSpec` | Update (`pydantic_update_model`) | Base fields plus `order: UUID4`, resolved again. It has no `item` |
| `SupplyRequestReadSpec` | Read | Base fields, with `quantity` as an `int`, `item` as a nested `ProductKnowledgeReadSpec` object and `order` as a nested `SupplyRequestOrderReadSpec` object |

### Validation

* `status` and `quantity` are required. `quantity` is a whole number of at least 1.
* Create requires `item` and `order`. Update requires `order` and takes no `item`.
* `supplied_item_condition` is not in any spec.

### Status rules

`SupplyRequestViewSet` runs these rules on create and on update.

* An item cannot be added to an order that is `abandoned` or `entered_in_error` ("Request order is abandoned or entered in error") or `completed` ("Request order is completed"). Orders in `draft`, `pending` and `in_progress` accept new items.
* The item's own status rule compares the item's old status with the order status values. Only an item in `entered_in_error` or `completed` cannot change status. The messages are "Request order is abandoned or entered in error" and "Request order is completed".
* `abandoned` is not an item status. So a `cancelled` item, for example one cancelled when its order was abandoned, can move back to `draft` or `active`.
* An update never checks the order's status. Items of a `completed`, `abandoned` or `entered_in_error` order can still be edited: `quantity`, and `status` within the rule above.
* When its order moves to `abandoned` or `entered_in_error`, every item of the order changes status. See [Request Order](https://docs.ohc.network/references/supply/request-order.md).

## API integration notes

### Endpoints

`SupplyRequestViewSet` serves requested items at `/api/v1/supply_request/`, outside the facility URL.

| Action | Request | Notes |
| - | - | - |
| create | `POST /api/v1/supply_request/` | `SupplyRequestWriteSpec`. Care's frontend sends one create per item, with status `active` |
| list | `GET /api/v1/supply_request/` | Needs at least one of `order`, `origin`, `destination`. Care's frontend lists an order's items with `?order={id}` |
| retrieve | `GET /api/v1/supply_request/{id}/` | |
| update | `PUT` or `PATCH /api/v1/supply_request/{id}/` | `SupplyRequestUpdateSpec`. Both methods run the same update |
| upsert | `POST /api/v1/supply_request/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 |
| request orders of a delivery order | `GET /api/v1/supply_request/request_orders/?delivery_order={id}` | Up to 100 request orders that the delivery order's items point at. See [Request Order](https://docs.ohc.network/references/supply/request-order.md) |

The API has no delete for requested items.

### Filters

| Parameter | Behaviour |
| - | - |
| `order`, `origin`, `destination`, `include_children` | Scope parameters. A list needs at least one of `order`, `origin`, `destination`, else "No filters provided" |
| `status` | One or more statuses |
| `item` | The catalog entry (`item__external_id`) |
| `supplier` | The supplier of the order (`order__supplier__external_id`) |

### Authorization

| Permission | Slug | Default roles |
| - | - | - |
| "Can Create Supply Request on Facility" | `can_write_supply_request` | Facility Admin, Staff, Doctor, Nurse |
| "Can Read Supply Request" | `can_read_supply_request` | Facility Admin, Administrator, Staff, Doctor, Nurse, Volunteer, Pharmacist |

The permission must be held in one of the location's organizations, as described in [Request Order](https://docs.ohc.network/references/supply/request-order.md).

| Action | Care checks | Refusal |
| - | - | - |
| create, update | "Can Create Supply Request on Facility" at the order's origin or at its destination. So for a stock request the supplying location can also add or edit items | "Cannot write supply requests" |
| retrieve, list | "Can Read Supply Request" at the order's origin or at its destination | "Cannot read supply requests" |

* Care's frontend item picker lists the facility's catalog entries. That list needs "Can Read Product Knowledge" in any organization of the facility.
* 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 |
| - | - |
| "Request order is abandoned or entered in error" | Create on an `abandoned` or `entered_in_error` order, or a status change of an item in `entered_in_error` |
| "Request order is completed" | Create on a `completed` order, or a status change of a `completed` item |
| "Cannot write supply requests" | No write permission at the order's origin or destination |
| "Cannot read supply requests" | No read permission at the order's origin or destination |
| "No filters provided" | A list without `order`, `origin` or `destination` |
| "No datapoints provided", "Too many datapoints provided" | An upsert with no items, or with more than `MAX_DATAPOINTS_PER_UPSERT` items |

## Related

* Concept: [Purchase Order](https://docs.ohc.network/concepts/supply/purchase-order.md)
* Concept: [Stock Request](https://docs.ohc.network/concepts/supply/supply-request.md)
* Reference: [Request Order](https://docs.ohc.network/references/supply/request-order.md)
* Reference: [Product Knowledge](https://docs.ohc.network/references/definitions/product-knowledge.md)
* Reference: [Supply Delivery](https://docs.ohc.network/references/supply/supply-delivery.md)
* Reference: [Inventory Item](https://docs.ohc.network/references/supply/inventory-item.md)
* Reference: [Location](https://docs.ohc.network/references/facility/location.md)
* Reference: [EMR base model](https://docs.ohc.network/references/foundation/base-model.md)
* Source: [supply\_request.py on GitHub](https://github.com/ohcnetwork/care/blob/develop/care/emr/models/supply_request.py)
* Spec: [supply\_request/spec.py on GitHub](https://github.com/ohcnetwork/care/blob/develop/care/emr/resources/inventory/supply_request/spec.py)
* Viewset: [supply\_request.py viewset on GitHub](https://github.com/ohcnetwork/care/blob/develop/care/emr/api/viewsets/inventory/supply_request.py)
