Skip to main content
Version: 3.1

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:

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​

ModelPurpose
InventoryItemThe stock of one product (one batch) at one location

InventoryItem fields​

FieldTypeNotes
locationFK → FacilityLocation, PROTECTRequired. The location that holds the stock
productFK → Product, PROTECTRequired. The product is one batch: it carries lot_number and expiration_date. So stock is kept per batch per location
statusCharField(255)Spec-bound to InventoryItemStatusOptions. Care sets only active, when it creates the row. No endpoint changes it
net_contentDecimalField, 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.

ValueLabelNotes
activeActiveSet by create_inventory_item when it creates the row. The only status the code sets
inactiveInactiveDefined, but no code sets it
entered_in_errorEntered in ErrorDefined, but no code sets it

The Status filter of Care's frontend stock list offers all three values.

RelationNotes
location → FacilityLocationSee Location
product → ProductSee Product
SupplyDelivery.supplied_inventory_item → InventoryItemThe stock record a delivered item takes from or adds to. See Supply Delivery
MedicationDispenseA dispense from this stock record lowers it. See Medication Dispense

sync_inventory_item calculates net_content from these rows:

RowsEffect on net_content
SupplyDelivery with status completed, whose order's destination is this location and whose stock record's product is this productAdds supplied_item_quantity
SupplyDelivery of an internal delivery (order.origin set) that takes from this stock record, with status in_progress or completedSubtracts supplied_item_quantity
MedicationDispense from this stock record that is not cancelledSubtracts the dispensed quantity

Resource specs (API schema)​

Spec classUsed forFields and behaviour
BaseInventoryItemSpecShared baseid (optional), status: InventoryItemStatusOptions. __model__ = InventoryItem, __exclude__ = []
InventoryItemWriteSpecNot usedThe viewset names it as pydantic_model, but the viewset has only list and retrieve, so no request uses it
InventoryItemReadSpecRead, listid, status, net_content (decimal_places=0), product as a nested ProductReadSpec object with its catalog entry, and location as a nested FacilityLocationListSpec object
InventoryItemRetrieveSpecRead, one itemA 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 active and net_content 0.
  • 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_content from 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. InventoryLock in care/emr/locks/inventory.py exists, 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 abandoned or entered_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.

ActionRequestNotes
listGET …/location/{location}/product/Care's frontend stock list sends status, catalog entry, expiration range and ordering, 14 rows per page
retrieveGET …/location/{location}/product/{id}/The item must be at the location in the URL

Filters​

ParameterBehaviour
product_knowledgeThe catalog entry (product__product_knowledge__external_id)
statusThe inventory item status
net_content_gtStock greater than the value. Care's frontend stock picker for internal deliveries sends net_content_gt=0
include_childrenWith 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_content and product__expiration_date.
  • There is no filter by product (batch) id.

Authorization​

PermissionSlugDefault roles
"Can Read Inventory Item"can_read_inventory_itemFacility Admin, Administrator, Staff, Doctor, Nurse, Volunteer, Pharmacist
ActionCare checksRefusal
list"Can Read Inventory Item" in the organizations of the location in the URL"You do not have permission to read inventory items"
retrieveThe 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 status or net_content: no endpoint takes them, and the next recalculation overwrites net_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) and location objects.

Errors​

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