# vinonachi/pharma_inventory architecture

> Architecture diagram of the GitHub repository vinonachi/pharma_inventory, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-10-14). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/vinonachi/pharma_inventory
- Repository: https://github.com/vinonachi/pharma_inventory

## Overview

PharmaIQ is a pharmaceutical distribution management application with inventory, purchasing, sales, supplier, warehouse, reporting, forecasting, analytics, and AI-assistant/agent capabilities. Users enter through the web interface; the FastAPI backend exposes domain APIs, with sales explicitly allocating stock from soonest-expiring batches. Database initialization seeds the operational data. The README is empty, and frontend page-to-API wiring was not visible in the excerpts, so those links are left unverified rather than presented as confirmed calls.

Main entry points: [`backend/app/main.py`](https://github.com/vinonachi/pharma_inventory/blob/main/backend/app/main.py) (FastAPI application), [`backend/app/services/ai_query_router.py`](https://github.com/vinonachi/pharma_inventory/blob/main/backend/app/services/ai_query_router.py) (Grounded query tools), [`backend/app/core/auto_seed.py`](https://github.com/vinonachi/pharma_inventory/blob/main/backend/app/core/auto_seed.py) (Schema and seeding), [`backend/app/database.py`](https://github.com/vinonachi/pharma_inventory/blob/main/backend/app/database.py) (Database sessions)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_web["Web application"]
  node_web_pages["Operations pages"]
  node_login_page["Login page<br/>[page.tsx]"]
  node_auth_client["Auth client<br/>[auth.ts]"]
  node_api_client["API client<br/>[api.ts]"]
end

subgraph group_api["API and operations"]
  node_app_server["FastAPI application<br/>[main.py]"]
  node_auth_api["Authentication<br/>[auth.py]"]
  node_catalog_api["Medicine catalog<br/>[medicines.py]"]
  node_purchasing_api["Purchasing<br/>[purchases.py]"]
  node_sales_api["Sales<br/>[sales.py]"]
  node_inventory_api["Inventory<br/>[inventory.py]"]
  node_supplier_api["Suppliers<br/>[suppliers.py]"]
  node_warehouse_api["Warehouses<br/>[warehouses.py]"]
  node_dashboard_api["Dashboard<br/>[dashboard.py]"]
end

subgraph group_intelligence["Analytics and AI"]
  node_analytics_api["Analytics<br/>[analytics.py]"]
  node_forecast_api["Demand forecasting<br/>[forecasting.py]"]
  node_reports_api["Reports<br/>[reports.py]"]
  node_assistant_api["AI assistant<br/>[ai_assistant.py]"]
  node_agent_api["Agent cycle API<br/>[agent.py]"]
  node_query_tools["Grounded query tools<br/>[ai_query_router.py]"]
  node_agent_engine["Agent engine<br/>[agent_engine.py]"]
  node_fefo["FEFO allocator<br/>[fefo_allocator.py]"]
  node_expiry["Expiry alerts<br/>[expiry_alerts.py]"]
  node_forecast_service["Forecast service<br/>[demand_forecast.py]"]
  node_report_service["Report generator"]
  node_scorecard["Supplier scorecard"]
end

subgraph group_data["Data and access"]
  node_database[("Database sessions<br/>[database.py]")]
  node_models["Domain models<br/>[core.py]"]
  node_seed["Schema and seeding<br/>[auto_seed.py]"]
  node_security["Password and tokens<br/>[security.py]"]
  node_rbac["Role authorization<br/>[rbac.py]"]
end

node_operator(("Staff or admin"))
node_store[("SQL database")]

node_operator -.->|"uses"| node_web_pages
node_operator -.->|"signs in"| node_login_page
node_login_page -.->|"uses"| node_auth_client
node_auth_client -->|"gets base URL"| node_api_client
node_api_client -.->|"sends API requests"| node_app_server
node_app_server -->|"mounts"| node_auth_api
node_app_server -->|"mounts"| node_catalog_api
node_app_server -->|"mounts"| node_purchasing_api
node_app_server -->|"mounts"| node_sales_api
node_app_server -->|"mounts"| node_inventory_api
node_app_server -->|"mounts"| node_supplier_api
node_app_server -->|"mounts"| node_warehouse_api
node_app_server -->|"mounts"| node_dashboard_api
node_app_server -->|"mounts"| node_analytics_api
node_app_server -->|"mounts"| node_forecast_api
node_app_server -->|"mounts"| node_reports_api
node_app_server -->|"mounts"| node_assistant_api
node_app_server -->|"mounts"| node_agent_api
node_app_server -->|"initializes on startup"| node_seed
node_auth_api -->|"opens session"| node_database
node_auth_api -->|"verifies and signs"| node_security
node_sales_api -->|"allocates batches"| node_fefo
node_sales_api -->|"reads and writes"| node_database
node_agent_api -->|"invokes"| node_agent_engine
node_agent_engine -->|"uses grounded tools"| node_query_tools
node_query_tools -->|"calls"| node_expiry
node_query_tools -->|"calls"| node_fefo
node_query_tools -->|"calls"| node_scorecard
node_assistant_api -->|"routes queries"| node_query_tools
node_analytics_api -->|"uses"| node_expiry
node_analytics_api -->|"uses"| node_fefo
node_forecast_api -->|"uses"| node_forecast_service
node_reports_api -->|"uses"| node_report_service
node_seed -->|"creates schema"| node_database
node_seed -->|"loads models"| node_models
node_seed -->|"hashes passwords"| node_security
node_database -->|"connects to"| node_store

click node_web_pages "https://github.com/vinonachi/pharma_inventory/tree/main/frontend/app"
click node_login_page "https://github.com/vinonachi/pharma_inventory/blob/main/frontend/app/login/page.tsx"
click node_auth_client "https://github.com/vinonachi/pharma_inventory/blob/main/frontend/lib/auth.ts"
click node_api_client "https://github.com/vinonachi/pharma_inventory/blob/main/frontend/lib/api.ts"
click node_app_server "https://github.com/vinonachi/pharma_inventory/blob/main/backend/app/main.py"
click node_auth_api "https://github.com/vinonachi/pharma_inventory/blob/main/backend/app/routers/auth.py"
click node_catalog_api "https://github.com/vinonachi/pharma_inventory/blob/main/backend/app/routers/medicines.py"
click node_purchasing_api "https://github.com/vinonachi/pharma_inventory/blob/main/backend/app/routers/purchases.py"
click node_sales_api "https://github.com/vinonachi/pharma_inventory/blob/main/backend/app/routers/sales.py"
click node_inventory_api "https://github.com/vinonachi/pharma_inventory/blob/main/backend/app/routers/inventory.py"
click node_supplier_api "https://github.com/vinonachi/pharma_inventory/blob/main/backend/app/routers/suppliers.py"
click node_warehouse_api "https://github.com/vinonachi/pharma_inventory/blob/main/backend/app/routers/warehouses.py"
click node_dashboard_api "https://github.com/vinonachi/pharma_inventory/blob/main/backend/app/routers/dashboard.py"
click node_analytics_api "https://github.com/vinonachi/pharma_inventory/blob/main/backend/app/routers/analytics.py"
click node_forecast_api "https://github.com/vinonachi/pharma_inventory/blob/main/backend/app/routers/forecasting.py"
click node_reports_api "https://github.com/vinonachi/pharma_inventory/blob/main/backend/app/routers/reports.py"
click node_assistant_api "https://github.com/vinonachi/pharma_inventory/blob/main/backend/app/routers/ai_assistant.py"
click node_agent_api "https://github.com/vinonachi/pharma_inventory/blob/main/backend/app/routers/agent.py"
click node_query_tools "https://github.com/vinonachi/pharma_inventory/blob/main/backend/app/services/ai_query_router.py"
click node_agent_engine "https://github.com/vinonachi/pharma_inventory/blob/main/backend/app/services/agent_engine.py"
click node_fefo "https://github.com/vinonachi/pharma_inventory/blob/main/backend/app/services/fefo_allocator.py"
click node_expiry "https://github.com/vinonachi/pharma_inventory/blob/main/backend/app/services/expiry_alerts.py"
click node_forecast_service "https://github.com/vinonachi/pharma_inventory/blob/main/backend/app/services/demand_forecast.py"
click node_report_service "https://github.com/vinonachi/pharma_inventory/blob/main/backend/app/services/report_generator.py"
click node_scorecard "https://github.com/vinonachi/pharma_inventory/blob/main/backend/app/services/supplier_scorecard.py"
click node_database "https://github.com/vinonachi/pharma_inventory/blob/main/backend/app/database.py"
click node_models "https://github.com/vinonachi/pharma_inventory/blob/main/backend/app/models/core.py"
click node_seed "https://github.com/vinonachi/pharma_inventory/blob/main/backend/app/core/auto_seed.py"
click node_security "https://github.com/vinonachi/pharma_inventory/blob/main/backend/app/core/security.py"
click node_rbac "https://github.com/vinonachi/pharma_inventory/blob/main/backend/app/core/rbac.py"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_web_pages,node_login_page,node_auth_client,node_api_client toneBlue
class node_app_server,node_auth_api,node_catalog_api,node_purchasing_api,node_sales_api,node_inventory_api,node_supplier_api,node_warehouse_api,node_dashboard_api,node_store toneAmber
class node_analytics_api,node_forecast_api,node_reports_api,node_assistant_api,node_agent_api,node_query_tools,node_agent_engine,node_fefo,node_expiry,node_forecast_service,node_report_service,node_scorecard toneMint
class node_database,node_models,node_seed,node_security,node_rbac toneRose
class node_operator toneIndigo
```

## Components

### Web application

- [Operations pages](https://github.com/vinonachi/pharma_inventory/tree/main/frontend/app): `frontend/app`
- [Login page](https://github.com/vinonachi/pharma_inventory/blob/main/frontend/app/login/page.tsx): `frontend/app/login/page.tsx`
- [Auth client](https://github.com/vinonachi/pharma_inventory/blob/main/frontend/lib/auth.ts): `frontend/lib/auth.ts`
- [API client](https://github.com/vinonachi/pharma_inventory/blob/main/frontend/lib/api.ts): `frontend/lib/api.ts`

### API and operations

- [FastAPI application](https://github.com/vinonachi/pharma_inventory/blob/main/backend/app/main.py): `backend/app/main.py`
- [Authentication](https://github.com/vinonachi/pharma_inventory/blob/main/backend/app/routers/auth.py): `backend/app/routers/auth.py`
- [Medicine catalog](https://github.com/vinonachi/pharma_inventory/blob/main/backend/app/routers/medicines.py): `backend/app/routers/medicines.py`
- [Purchasing](https://github.com/vinonachi/pharma_inventory/blob/main/backend/app/routers/purchases.py): `backend/app/routers/purchases.py`
- [Sales](https://github.com/vinonachi/pharma_inventory/blob/main/backend/app/routers/sales.py): `backend/app/routers/sales.py`
- [Inventory](https://github.com/vinonachi/pharma_inventory/blob/main/backend/app/routers/inventory.py): `backend/app/routers/inventory.py`
- [Suppliers](https://github.com/vinonachi/pharma_inventory/blob/main/backend/app/routers/suppliers.py): `backend/app/routers/suppliers.py`
- [Warehouses](https://github.com/vinonachi/pharma_inventory/blob/main/backend/app/routers/warehouses.py): `backend/app/routers/warehouses.py`
- [Dashboard](https://github.com/vinonachi/pharma_inventory/blob/main/backend/app/routers/dashboard.py): `backend/app/routers/dashboard.py`

### Analytics and AI

- [Analytics](https://github.com/vinonachi/pharma_inventory/blob/main/backend/app/routers/analytics.py): `backend/app/routers/analytics.py`
- [Demand forecasting](https://github.com/vinonachi/pharma_inventory/blob/main/backend/app/routers/forecasting.py): `backend/app/routers/forecasting.py`
- [Reports](https://github.com/vinonachi/pharma_inventory/blob/main/backend/app/routers/reports.py): `backend/app/routers/reports.py`
- [AI assistant](https://github.com/vinonachi/pharma_inventory/blob/main/backend/app/routers/ai_assistant.py): `backend/app/routers/ai_assistant.py`
- [Agent cycle API](https://github.com/vinonachi/pharma_inventory/blob/main/backend/app/routers/agent.py): `backend/app/routers/agent.py`
- [Grounded query tools](https://github.com/vinonachi/pharma_inventory/blob/main/backend/app/services/ai_query_router.py): `backend/app/services/ai_query_router.py`
- [Agent engine](https://github.com/vinonachi/pharma_inventory/blob/main/backend/app/services/agent_engine.py): `backend/app/services/agent_engine.py`
- [FEFO allocator](https://github.com/vinonachi/pharma_inventory/blob/main/backend/app/services/fefo_allocator.py): `backend/app/services/fefo_allocator.py`
- [Expiry alerts](https://github.com/vinonachi/pharma_inventory/blob/main/backend/app/services/expiry_alerts.py): `backend/app/services/expiry_alerts.py`
- [Forecast service](https://github.com/vinonachi/pharma_inventory/blob/main/backend/app/services/demand_forecast.py): `backend/app/services/demand_forecast.py`
- [Report generator](https://github.com/vinonachi/pharma_inventory/blob/main/backend/app/services/report_generator.py): `backend/app/services/report_generator.py`
- [Supplier scorecard](https://github.com/vinonachi/pharma_inventory/blob/main/backend/app/services/supplier_scorecard.py): `backend/app/services/supplier_scorecard.py`

### Data and access

- [Database sessions](https://github.com/vinonachi/pharma_inventory/blob/main/backend/app/database.py): `backend/app/database.py`
- [Domain models](https://github.com/vinonachi/pharma_inventory/blob/main/backend/app/models/core.py): `backend/app/models/core.py`
- [Schema and seeding](https://github.com/vinonachi/pharma_inventory/blob/main/backend/app/core/auto_seed.py): `backend/app/core/auto_seed.py`
- [Password and tokens](https://github.com/vinonachi/pharma_inventory/blob/main/backend/app/core/security.py): `backend/app/core/security.py`
- [Role authorization](https://github.com/vinonachi/pharma_inventory/blob/main/backend/app/core/rbac.py): `backend/app/core/rbac.py`

### Other components

- **Staff or admin**
- **SQL database**

## Connections

- Staff or admin → Operations pages: uses
- Staff or admin → Login page: signs in
- Login page → Auth client: uses
- Auth client → API client: gets base URL (evidence: `frontend/lib/auth.ts`)
- API client → FastAPI application: sends API requests
- FastAPI application → Authentication: mounts (evidence: `backend/app/main.py`)
- FastAPI application → Medicine catalog: mounts (evidence: `backend/app/main.py`)
- FastAPI application → Purchasing: mounts (evidence: `backend/app/main.py`)
- FastAPI application → Sales: mounts (evidence: `backend/app/main.py`)
- FastAPI application → Inventory: mounts (evidence: `backend/app/main.py`)
- FastAPI application → Suppliers: mounts (evidence: `backend/app/main.py`)
- FastAPI application → Warehouses: mounts (evidence: `backend/app/main.py`)
- FastAPI application → Dashboard: mounts (evidence: `backend/app/main.py`)
- FastAPI application → Analytics: mounts (evidence: `backend/app/main.py`)
- FastAPI application → Demand forecasting: mounts (evidence: `backend/app/main.py`)
- FastAPI application → Reports: mounts (evidence: `backend/app/main.py`)
- FastAPI application → AI assistant: mounts (evidence: `backend/app/main.py`)
- FastAPI application → Agent cycle API: mounts (evidence: `backend/app/main.py`)
- FastAPI application → Schema and seeding: initializes on startup (evidence: `backend/app/main.py`)
- Authentication → Database sessions: opens session (evidence: `backend/app/routers/auth.py`)
- Authentication → Password and tokens: verifies and signs (evidence: `backend/app/routers/auth.py`)
- Sales → FEFO allocator: allocates batches (evidence: `backend/app/routers/sales.py`)
- Sales → Database sessions: reads and writes (evidence: `backend/app/routers/sales.py`)
- Agent cycle API → Agent engine: invokes (evidence: `backend/app/routers/agent.py`)
- Agent engine → Grounded query tools: uses grounded tools (evidence: `backend/app/services/agent_engine.py`)
- Grounded query tools → Expiry alerts: calls (evidence: `backend/app/services/ai_query_router.py`)
- Grounded query tools → FEFO allocator: calls (evidence: `backend/app/services/ai_query_router.py`)
- Grounded query tools → Supplier scorecard: calls (evidence: `backend/app/services/ai_query_router.py`)
- AI assistant → Grounded query tools: routes queries (evidence: `backend/app/routers/ai_assistant.py`)
- Analytics → Expiry alerts: uses (evidence: `backend/app/routers/analytics.py`)
- Analytics → FEFO allocator: uses (evidence: `backend/app/routers/analytics.py`)
- Demand forecasting → Forecast service: uses (evidence: `backend/app/routers/forecasting.py`)
- Reports → Report generator: uses (evidence: `backend/app/routers/reports.py`)
- Schema and seeding → Database sessions: creates schema (evidence: `backend/app/core/auto_seed.py`)
- Schema and seeding → Domain models: loads models (evidence: `backend/app/core/auto_seed.py`)
- Schema and seeding → Password and tokens: hashes passwords (evidence: `backend/app/core/auto_seed.py`)
- Database sessions → SQL database: connects to (evidence: `backend/app/database.py`)
