Inventory Item
Technical reference for the InventoryItem model in Care EMR. For what an inventory item is, see the Inventory Item concept.
An InventoryItem is the stock of one Product, which is one batch, at one location. The API is read-only. Care creates the rows and calculates net_content from delivered items and dispenses.
Source:
- Model:
care/emr/models/inventory_item.py· constraint migration0083_inventoryitem_unique_active_inventory_product_location.py - Spec:
care/emr/resources/inventory/inventory_item/spec.py - Helpers:
create_inventory_item.py·sync_inventory_item.py - Viewset:
care/emr/api/viewsets/inventory/inventory_item.py - Lock:
InventoryItemLockincare/emr/locks/billing.py
The Django model is the storage layer. The Pydantic resource specs define the status enum and the read schema, which nests the product and location objects that the model holds as foreign keys.
Models
| Model | Purpose |
|---|---|
InventoryItem | The stock of one product (one batch) at one location |
InventoryItem fields
| Field | Type | Notes |
|---|---|---|
location | FK → FacilityLocation, PROTECT | Required. The location that holds the stock |
product | FK → Product, PROTECT | Required. The product is one batch: it carries lot_number and expiration_date. So stock is kept per batch per location |
status | CharField(255) | Spec-bound to InventoryItemStatusOptions. Care sets only active, when it creates the row. No endpoint changes it |
net_content | DecimalField, max_digits=20, decimal_places=6, default Decimal(0) | The stock. Calculated from scratch by sync_inventory_item. Returned as a whole number |
A database constraint, unique_active_inventory_product_location, allows one row per product and location among rows that are not deleted (deleted=False). The model has no save() override.
InventoryItemStatusOptions values
Defined in spec.py. The labels are the ones Care's frontend shows.
| Value | Label | Notes |
|---|---|---|
active | Active | Set by create_inventory_item when it creates the row. The only status the code sets |
inactive | Inactive | Defined, but no code sets it |
entered_in_error | Entered in Error | Defined, but no code sets it |
The Status filter of Care's frontend stock list offers all three values.
Related models
| Relation | Notes |
|---|---|
location → FacilityLocation | See Location |
product → Product | See Product |
SupplyDelivery.supplied_inventory_item → InventoryItem | The stock record a delivered item takes from or adds to. See Supply Delivery |
MedicationDispense | A dispense from this stock record lowers it. See Medication Dispense |
sync_inventory_item calculates net_content from these rows:
| Rows | Effect on net_content |
|---|---|
SupplyDelivery with status completed, whose order's destination is this location and whose stock record's product is this product | Adds supplied_item_quantity |
SupplyDelivery of an internal delivery (order.origin set) that takes from this stock record, with status in_progress or completed | Subtracts supplied_item_quantity |
MedicationDispense from this stock record that is not cancelled | Subtracts the dispensed quantity |
Resource specs (API schema)
| Spec class | Used for | Fields and behaviour |
|---|---|---|
BaseInventoryItemSpec | Shared base | id (optional), status: InventoryItemStatusOptions. __model__ = InventoryItem, __exclude__ = [] |
InventoryItemWriteSpec | Not used | The viewset names it as pydantic_model, but the viewset has only list and retrieve, so no request uses it |
InventoryItemReadSpec | Read, list | id, status, net_content (decimal_places=0), product as a nested ProductReadSpec object with its catalog entry, and location as a nested FacilityLocationListSpec object |
InventoryItemRetrieveSpec | Read, one item | A pass subclass of InventoryItemReadSpec: the same shape |
The API has no create spec in use and no write path. Care creates every row itself.
Methods & save behaviour
create_inventory_item(product, location)
- The only code that creates stock records. It returns the existing row for the product and location, or creates one with status
activeandnet_content0. - For a delivered item of an order without origin (a purchase delivery or a medication return), Care calls it for the destination and links the item to that row.
- For an internal delivered item, Care also creates the destination's row for the same product if it is missing.
sync_inventory_item(inventory_item)
- It recalculates
net_contentfrom scratch, with the rows in "Related models", and saves the total. It does not adjust the old value. - It holds
InventoryItemLock, a row lock on the inventory item.InventoryLockincare/emr/locks/inventory.pyexists, but no code uses it. - Care refuses the writes that would need more stock than a record holds: an internal delivered item ("Insufficient stock"), a dispense and a delivery order cancel ("Inventory item does not have enough stock").
When stock is recalculated
Care calls sync_inventory_item at exactly these points. There are no signals or save hooks for stock.
- After every delivered item create and update, whatever the status. Care recalculates the item's stock record and, for an internal item, also the destination's record for the same product.
- When a delivery order moves to
abandonedorentered_in_error. - After a dispense is created or updated.
- When a dispense order is cancelled.
For what each delivery event does to stock, see "Stock effects" in Supply Delivery.
API integration notes
Endpoints
InventoryItemViewSet serves inventory items at /api/v1/facility/{facility}/location/{location}/product/. It has list and retrieve only. No endpoint creates, edits or deletes an inventory item.
| Action | Request | Notes |
|---|---|---|
| list | GET …/location/{location}/product/ | Care's frontend stock list sends status, catalog entry, expiration range and ordering, 14 rows per page |
| retrieve | GET …/location/{location}/product/{id}/ | The item must be at the location in the URL |
Filters
| Parameter | Behaviour |
|---|---|
product_knowledge | The catalog entry (product__product_knowledge__external_id) |
status | The inventory item status |
net_content_gt | Stock greater than the value. Care's frontend stock picker for internal deliveries sends net_content_gt=0 |
include_children | With true, the list also takes the stock of locations below the location in the URL |
- The list also filters on a range of the product's expiration date.
- The list sorts by created date, modified date,
net_contentandproduct__expiration_date. - There is no filter by product (batch) id.
Authorization
| Permission | Slug | Default roles |
|---|---|---|
| "Can Read Inventory Item" | can_read_inventory_item | Facility Admin, Administrator, Staff, Doctor, Nurse, Volunteer, Pharmacist |
| Action | Care checks | Refusal |
|---|---|---|
| list | "Can Read Inventory Item" in the organizations of the location in the URL | "You do not have permission to read inventory items" |
| retrieve | The same permission at the location in the URL. The item must be at that location | "Inventory item does not belong to the specified location" |
- The location's organizations are the departments linked to the location with their parent departments, the parent location's organizations, and the facility's root organization "Administration".
- To open a location, Care's API also needs "Can List Facility Locations" in the location's organizations. By default Volunteer does not hold it.
- 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.
Other notes
- Do not try to set
statusornet_content: no endpoint takes them, and the next recalculation overwritesnet_content. - One row exists per product and location. Stock changes update that row; they do not add rows.
- Read responses nest the full
product(with its catalog entry) andlocationobjects.
Errors
| Message | Cause |
|---|---|
| "You do not have permission to read inventory items" | List without "Can Read Inventory Item" at the location |
| "Inventory item does not belong to the specified location" | Retrieve of an item that is at another location |
| "Inventory item does not have enough stock" | A dispense or a delivery order cancel that needs more stock than the record holds |
| "Insufficient stock" | An internal delivered item for more than the record holds |
Related
- Concept: Inventory Item
- Reference: Product
- Reference: Supply Delivery
- Reference: Request Order
- Reference: Medication Dispense
- Reference: Location
- Reference: EMR base model
- Source: inventory_item.py on GitHub
- Source: inventory_item spec on GitHub