Skip to main content
Glama
README.md
# ZATCA QR Toolkit

Small, exact helpers for Saudi e-invoices — VAT-number checks, VAT and
invoice totals in exact halalas, and the simplified-invoice QR code — usable
from Python, the command line, or **by Claude as MCP tools**.

<img src="docs/img/sample-qr.png" alt="Sample e-invoice QR code for a sample store" width="160">

## Why

Every Saudi point-of-sale, booking system and online store has to print a QR
code on simplified invoices and get VAT right to the halala. The common bugs
are small and expensive:

- **Floats in money.** `0.1 + 0.2` and a 15% multiply later, the VAT on the
  receipt is a halala off from the VAT in the books.
- **Arabic seller names counted in characters.** The QR code's length byte
  counts *bytes*; an Arabic letter is two. Get it wrong and the code scans as
  garbage — and a name over 255 bytes cannot be encoded at all.
- **VAT numbers that fail the format rule** (15 digits, first and last digit
  3), caught only when an invoice is rejected.
- **Letting an AI assistant do the arithmetic.** A model asked "what's the VAT
  on these lines?" will answer confidently and sometimes wrongly.

## What it does

- **`check_vat_number`** — normalises Arabic-Indic digits, spaces and dashes,
  then checks the 15-digit, 3…3 format. A valid format does not prove a number
  is registered; only ZATCA's taxpayer lookup can.
- **`invoice_totals`** — net, VAT per rate (15%, 0%, …) and total from invoice
  lines with quantities, unit prices and discounts. Exact fractions in,
  integer halalas out; VAT is rounded half up once per rate.
- **QR code (Phase 1 fields, tags 1–5)** — build the Base64 TLV payload and a
  PNG; read a payload back, including the Phase 2 tags 6–9 as raw bytes. The
  check separates **errors** (malformed, breaks a format rule — never encoded)
  from **warnings** (well-formed but worth a look, e.g. VAT that is not 15% of
  the net amount, which is fine for zero-rated lines).
- **MCP server** — the four operations as tools, so Claude computes nothing
  itself: it passes text in, gets exact text back.

Encoding is checked against the widely published "Bobs Records" example from
ZATCA's QR guidance, byte for byte.

## Use it

```bash
uv sync --frozen

# CLI
uv run python -m zatcakit.cli vat 300000000000003
uv run python -m zatcakit.cli qr build --seller "متجر ركن البن" --vat 300000000000003 \
    --time 2026-12-03T14:30:00+03:00 --total 1035.00 --vat-total 135.00 --png qr.png
uv run python -m zatcakit.cli qr read "AQxCb2JzIFJlY29yZHMC..."
```

```python
from fractions import Fraction
from zatcakit.money import Line, invoice_totals, format_sar

totals = invoice_totals(
    [
        Line("Hand grinder", Fraction(1), 30348),
        Line("Coffee beans", Fraction(3), 5652, discount_halalas=500),
        Line("Export shipment", Fraction(1), 10000, vat_rate=Fraction(0)),
    ]
)
format_sar(totals.vat_halalas)  # '70.21'
```

### As Claude tools (MCP)

Add the server to Claude Code:

```bash
claude mcp add zatca -- uv --directory /path/to/zatca-qr-toolkit run python -m zatcakit.server
```

or to any MCP client's config:

```json
{
  "mcpServers": {
    "zatca": {
      "command": "uv",
      "args": ["--directory", "/path/to/zatca-qr-toolkit", "run", "python", "-m", "zatcakit.server"]
    }
  }
}
```

Then ask: *"Calculate the invoice for 2 kettles at 275 SAR and a pack of
filters at 24 SAR, and give me the QR payload for seller Rukn Al Bunn, VAT
300000000000003."*

## Scope

- **In:** Phase 1 QR fields, reading Phase 2 tags, VAT-number format, VAT and
  totals arithmetic.
- **Out:** Phase 2 (integration) — UBL XML, cryptographic stamps and
  clearance/reporting with ZATCA need a device certificate from ZATCA's
  onboarding and belong in a certified e-invoicing solution. This toolkit does
  not make a system ZATCA-compliant on its own; check requirements with your
  accountant or e-invoicing provider.

## Quality gates

Every push runs ruff, mypy `--strict`, 52 tests at ≥ 95% branch coverage
(99% today, warnings as errors), a dependency audit and a secret scan.
Built with Claude Code under the rules in
[claude-code-production-playbook](https://github.com/Akairo27/claude-code-production-playbook).

## About

Built by Mahmoud ([@Akairo27](https://github.com/Akairo27)), freelance
developer since 2012, based in Makkah, building invoicing, booking and AI
systems for businesses in Saudi Arabia.

MIT licence.