"""The fiscal driver interface (plan step 3.3e, D-27). A driver speaks to one kind of fiscal device. Callers never talk to a device directly: they ask services.fiscal for the venue's driver (setting `fiscal.type`) and use these methods. Adding a machine means adding a module with a FiscalDriver subclass and listing it in services/fiscal/__init__.py DRIVERS; nothing else changes. Items are plain dicts built by services.fiscal.build_fiscal_items: {"name": str, "qty": int | float, "unit_price": float, "vat_group_id": int} Results are dicts: {"success": bool, "reference": str | None, "reply": str | None, "error": str | None} (`reference`: what the device or bridge calls this document, e.g. the command file's name). Two steps, so the slow part can run off the request: prepare_*(db, ...) reads the settings and builds the document with the request's session and returns send(timeout_seconds) -> result, which talks to the device without touching the database. issue_* does both (blocking). """ from __future__ import annotations from typing import Callable Send = Callable[[int], dict] class FiscalDriver: key: str = "" name: str = "" # Whether issue_credit_note works. A driver without it: refunds are still recorded and the # cashier is told to issue the credit note on the machine itself (owner decision 2026-11-15). credit_notes: bool = False def prepare_receipt(self, db, items: list[dict], method: str, total: float, payments: list[dict] | None = None) -> Send: """A sales receipt, ready to send. `payments` [{"method", "amount"}] when it was paid in several ways (a split bill, 6F.6); else all of it by `method`.""" raise NotImplementedError def prepare_credit_note(self, db, items: list[dict], method: str, total: float, original: dict | None = None) -> Send: """A credit note for refunded items, ready to send. `original`: what is known about the receipt being refunded ({"order_id", "reference"}), for devices that need it.""" raise NotImplementedError(f"{self.key}: credit notes are not supported") def issue_receipt(self, db, items: list[dict], method: str, total: float, timeout_seconds: int = 30) -> dict: """Issue a sales receipt and wait for the device's answer (blocking).""" return self.prepare_receipt(db, items, method, total)(timeout_seconds) def issue_credit_note(self, db, items: list[dict], method: str, total: float, original: dict | None = None, timeout_seconds: int = 30) -> dict: """Issue a credit note and wait for the device's answer (blocking).""" return self.prepare_credit_note(db, items, method, total, original)(timeout_seconds) def status(self, db) -> dict: """{"configured": bool, "detail": str | None}: whether the driver has what it needs.""" return {"configured": True, "detail": None}