Metadata-Version: 2.4
Name: fatoority
Version: 0.7.0
Summary: Python bindings for the fatoority library, providing ZATCA-compliant e-invoicing functionality.
Author: mqqz
License-Expression: MIT OR Apache-2.0
Project-URL: Homepage, https://docs.fatoority.com
Project-URL: Repository, https://github.com/mqqz/fatoority
Project-URL: Documentation, https://docs.fatoority.com
Project-URL: Issues, https://github.com/mqqz/fatoority/issues
Keywords: zatca,invoice,e-invoicing,sdk,ffi
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3 :: Only
Classifier: Operating System :: OS Independent
Classifier: Topic :: Software Development :: Libraries
Requires-Python: >=3.10
Description-Content-Type: text/markdown
License-File: LICENSE-MIT
License-File: LICENSE-APACHE
License-File: THIRD_PARTY_NOTICES.md
Provides-Extra: async
Requires-Dist: httpx>=0.27; extra == "async"
Provides-Extra: dev
Requires-Dist: pytest>=8.0; extra == "dev"
Requires-Dist: httpx>=0.27; extra == "dev"
Dynamic: license-file

## Fatoority Python bindings

This package wraps Diplomat-generated native bindings with the public `fatoora`
Python API. Install with `pip install fatoority` and import as `fatoora`. See [Diplomat bindings](../../docs/development/diplomat.md).

### Development (uv)

```bash
uv venv
uv pip install .
```

### Build a wheel (uv)

```bash
uv build --wheel
```

### Wheels (CI)

- GitHub Actions uses `cibuildwheel` to produce platform wheels in `dist/` (wheel-only publishing).
- See `.github/workflows/python.yml` for the build matrix.

### Notes

- The build step compiles `fatoora-ffi` with `cargo build -p fatoora-ffi --release`.
- The shared library is bundled into the Python package.
- Building the native extension requires CMake and a C++20 compiler. Nanobind is
  pinned in the build dependencies. Reinstall the package after native changes.
- ZATCA API responses are exposed via owned objects with read-only properties (no JSON payloads).
- Errors are raised as `FatooraError` subclasses, one per `ErrorKind` (such as `InvalidInputError` or `NetworkError`). Each exception exposes `.code`, `.kind`, `.message`, and `.details`, a dictionary containing structured validation issues or diagnostics. Unknown codes raise plain `FatooraError` with `.kind` set to `None`.
- Reinstall the package after changes; the extension loads its bundled shared library.

### Examples

```bash
python examples/load_lib.py
python examples/invoice_basic.py
python examples/invoice_parse.py
```

### Tests

```bash
uv pip install -e .[dev]
SKIP_ZATCA_LIVE_API=1 uv run pytest tests
```

### High-level API

```python
from datetime import datetime, timezone
from uuid import uuid4

from fatoora import (Address, InvoiceBuilder, InvoiceSubType, InvoiceTypeKind,
                     PaymentMeans, VatCategory)

invoice = (
    InvoiceBuilder(
        InvoiceTypeKind.TAX, InvoiceSubType.SIMPLIFIED,
        id="INV-1",
        uuid=uuid4(),
        issue_datetime=datetime.now(timezone.utc),
        currency="SAR",
        previous_invoice_hash="hash",
        invoice_counter=1,
        vat_category=VatCategory.STANDARD,
    )
    .set_seller("Acme Inc", vat_id="399999999900003",
                address=Address("SAU", "Riyadh", "King Fahd", "1234", "12222"))
    .add_payment_means(PaymentMeans("10"))
    .add_line_item(description="Item", quantity="1.0", unit_code="PCE",
                   unit_price="100.0", vat_rate="15.0", vat_category=VatCategory.STANDARD)
    .build()
)
print(invoice.id, invoice.issue_datetime, invoice.totals.payable_amount)
print(invoice.xml)
```

Header fields can be passed to the constructor or set with the chaining setters.
Timestamps accept a timezone-aware `datetime` or `YYYY-MM-DDTHH:MM:SSZ` text; UUIDs
accept `uuid.UUID` or text. Invoice data is read through properties
(`invoice.seller.address.city`) and returned as frozen dataclasses (`Party`,
`Address`, `Adjustment`, `InvoiceLineItem`, ...) that can be passed back to a
builder: `InvoiceBuilder(..., seller=parsed.seller)`.

### Local ZATCA validation

```python
from fatoora import validate_zatca

report = validate_zatca(xml, previous_invoice_hash=previous_hash_from_history)
if not report.is_valid:
    for finding in report.findings:
        print(finding.severity.value, finding.code, finding.message)
```

`validate_zatca` returns a frozen `ZatcaReport` dataclass (`report.to_json()` gives
the JSON report). The report retains warnings and distinguishes rejection from
incomplete coverage. Missing predecessor context leaves the report incomplete.
Execution failures raise a typed exception with any partial report in
`error.details["report"]`. Standard invoices skip signature/QR checks in this SDK
profile. Local integrity does not establish issuer trust or remote acceptance. The
wheel embeds runtime schemas and native rules; Java, SDK files and a source
checkout are unnecessary.

### Upgrading

0.7 removes every name that 0.6 deprecated (`Signer`, `parse_signed_invoice_xml`,
`clearance_status=`, ...). See the
[0.6 to 0.7 migration guide](../../docs/guides/migrate-0.7/python.md) for each
replacement, and the [Python reference](../../docs/reference/bindings/python.md#migrate-from-earlier-versions)
for older upgrades. The public surface is recorded in `tests/public_api.json`; regenerate it after an
intentional change with `python tests/test_public_api.py --update`.
