Skip to content

About

Multi-tenant invoicing SaaS in Django: invoices, PDFs, bank reconciliation and an AI assistant.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Latest commit

 

History

366 Commits

Folders and files

Repository files navigation

InvoiceHub

CI License: MIT

Multi-tenant invoicing for small businesses — invoices, bank reconciliation and an AI assistant — with each company's data isolated by PostgreSQL Row-Level Security.

Try it

With Docker and make:

git clone https://github.com/eamigo86/invoicehub.git && cd invoicehub
make demo

In about two minutes this starts the stack, seeds a demo company with fictitious US invoices, imports a bank statement built from them and reconciles it in the Celery worker. It prints these links and the demo login (local use only):

Link What it opens
Dashboard, Invoices, Bank The tenant UI: sign in with the printed email and password
Queue The review queue, which holds a suggested and an ambiguous match
GraphiQL The GraphQL explorer, with a public read-only demo token
Admin The Django admin

The statement itself is in media/demo/bank_statement.csv. The links use the port the stack publishes: to run the demo on another port, set it first, for example WEB_PORT=8014 make demo.

At a glance

Area What it does Status
Tenant isolation Every query is scoped twice: by the application and by Row-Level Security. An isolation suite runs as its own CI gate Done
Accounts and roles Email login with mandatory verification (django-allauth), owner/admin/accountant/viewer roles, hashed and revocable per-tenant API tokens with scopes Done
Billing Invoices and credit notes with gap-free numbering, PDFs, email delivery, configurable sales tax rates Done
Reconciliation CSV bank statements matched to invoices with remitmatch, with a review queue for ambiguous matches Done
Integrations GraphQL API for integrators (django-graphex) and an MCP server for agents Done
AI assistant Drafts invoices and reminders, answers questions about the business, explains ambiguous matches, suggests a product and tax rates while typing a line; cost, latency and error rate per use case in the admin; evals in CI Done

Architecture

A Django monolith with three entry points (HTMX UI, GraphQL API, MCP server) that share one services layer, plus a Celery worker for everything slow. The web app and the worker use PostgreSQL (with Row-Level Security) and Redis as the task queue.

flowchart TB
    subgraph clients[Clients]
        B["Browser (HTMX)<br/>Tenant user"]
        G["GraphQL client<br/>Integrator with token"]
        A["Agent via MCP<br/>Claude or another client"]
    end
    subgraph web["Web · Django on Cloud Run"]
        V[HTMX views]
        Q["GraphQL API (graphex)"]
        M["MCP server + assistant"]
        S["Domain services<br/>permissions · audit · business logic"]
    end
    subgraph worker["Worker · Celery on Cloud Run"]
        P[PDFs with WeasyPrint]
        R["Import and reconciliation (remitmatch)"]
        E[Email delivery]
    end
    subgraph shared[Shared by web and worker]
        PG[(PostgreSQL · RLS)]
        RD[(Redis · task queue)]
        GCS[(GCS · PDFs and statements)]
        OR[OpenRouter · external LLM]
    end
    B --> V
    G --> Q
    A --> M
    V --> S
    Q --> S
    M --> S
    S -- "reads, writes and enqueues" --> shared
    RD --> R
Loading

Quick start

Only Docker is needed.

git clone https://github.com/eamigo86/invoicehub.git && cd invoicehub
docker compose up -d --build --wait
docker compose exec web python manage.py createsuperuser

Then sign in at http://localhost:8010/accounts/login/. Email verification is mandatory; the verification email lands in Mailpit at http://localhost:8025.

Details

Signing in and choosing a company

The admin login goes through the same sign-in page. A user with a single company gets it selected on login; otherwise /tenants/ lists the user's companies, switches between them and creates a new one. Tenant models stay hidden in the admin until a company is active. After signing in or choosing a company you land on the dashboard at /.

Styles

The UI uses Tailwind through its standalone CLI, with no Node. The Docker build compiles the CSS into /opt/invoicehub-static, outside /app so the Compose bind mount does not hide it, and Django serves it from there. Running on the host without Docker, build it yourself with tailwindcss -i static/src/input.css -o static/css/app.css (the file is git-ignored).

Ports

They listen on 127.0.0.1 only, so the demo login and the databases stay on this machine. Override the ports with environment variables or a .env file.

Service URL / port Variable
Web (admin at /admin/) http://localhost:8010 WEB_PORT
MCP server (Streamable HTTP) http://localhost:8011/mcp MCP_PORT
PostgreSQL localhost:5442 POSTGRES_PORT
Redis localhost:6390 REDIS_PORT
Mailpit UI http://localhost:8025 MAILPIT_UI_PORT
Mailpit SMTP localhost:1025 MAILPIT_SMTP_PORT

AI assistant

The assistant calls models through OpenRouter. Put OPENROUTER_API_KEY in your environment or in .env: Compose passes it to the web and worker containers, and host-side commands read it from .env. Without it, model calls fail with an error and the rest of the app keeps working.

Connect Claude Desktop

The MCP server gives an agent the assistant tools with a per-tenant API token. Create one under Settings → API tokens (/settings/tokens/) with invoices:read, and invoices:write if the agent may draft invoices and reminders. With the host setup below (.env and uv sync), add the server to Claude Desktop's claude_desktop_config.json and restart it:

{
  "mcpServers": {
    "invoicehub": {
      "command": "uv",
      "args": ["--directory", "/path/to/invoicehub", "run", "python", "manage.py", "mcp_server"],
      "env": { "INVOICEHUB_API_TOKEN": "ih_..." }
    }
  }
}

Issuing, sending and applying payments stay in the app. Remote MCP clients use the Compose mcp service at http://localhost:8011/mcp instead, with the header Authorization: Bearer ih_....

Database roles

Containers connect to PostgreSQL as the unprivileged invoicehub_app role; only the migrate step uses invoicehub_migrator, which owns the tables. Host-side commands (pytest, migrate, runserver) connect as the migrator; FORCE ROW LEVEL SECURITY keeps the tenant policies applying to it as well.

Checks and tests on the host

Requires uv and Python 3.13, with the Compose stack running, and Pango for the invoice PDFs (WeasyPrint): brew install pango on macOS, or libpango-1.0-0 libpangoft2-1.0-0 libharfbuzz-subset0 on Debian and Ubuntu.

cp .env.example .env             # connects as invoicehub_migrator on localhost:5442
uv sync
uv run ruff check . && uv run ruff format --check .
uv run mypy .
uv run python manage.py makemigrations --check
uv run pytest --cov              # needs the Compose postgres and redis running
uv run pytest -m isolation       # tenant isolation suite only (also part of the full run)
uv run pre-commit install        # optional: run the checks on every commit

Design documents

The design lives in RFCs under docs/rfc/. They are drafts and may change.

RFC Area Main decision
0000 Overview Scope, architecture, cross-cutting decisions and plan
0001 remitmatch library Rust + PyO3, split and grouped payments in v1
0002 Multi-tenancy and authentication tenant_id column + Row-Level Security
0003 Billing domain Gap-free numbering per series; issued invoice is immutable
0004 API and UI GraphQL with django-graphex; HTMX for the UI
0005 Bank reconciliation Asynchronous import + review queue
0006 Applied AI OpenRouter, tools with user permissions, evals in CI
0007 Infrastructure Cloud Run + Cloud SQL; OpenTelemetry and Sentry

License

MIT

About

Multi-tenant invoicing SaaS in Django: invoices, PDFs, bank reconciliation and an AI assistant.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages