Die config.json des Rechnungstools wurde nur unter dem festen Entwicklungspfad gesucht - als EXE auf einem anderen Rechner haette der Bericht keinen Briefkopf bekommen. Jetzt: neben dem Programm, im Nachbarordner, in Dokumente, und ueber "Daten -> Firmendaten waehlen". Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
77 lines
2.7 KiB
Python
77 lines
2.7 KiB
Python
# -*- coding: utf-8 -*-
|
||
"""
|
||
Firmendaten für den Briefkopf des Amts-Berichts.
|
||
|
||
Sie stehen bereits in der `config.json` des Rechnungstools – von dort werden sie
|
||
gelesen, damit Rechnung und Steuerbericht garantiert denselben Kopf tragen und
|
||
nichts doppelt gepflegt werden muss.
|
||
|
||
Wird das Rechnungstool nicht gefunden, greift ein neutraler Ersatz und der
|
||
Bericht wird trotzdem gedruckt (nur ohne Logo/Bankzeile).
|
||
"""
|
||
|
||
import os
|
||
import sys
|
||
import json
|
||
import logging
|
||
|
||
log = logging.getLogger("bst.firma")
|
||
|
||
_HIER = os.path.dirname(os.path.abspath(
|
||
sys.argv[0] if getattr(sys, "frozen", False) else __file__))
|
||
_HEIM = os.path.expanduser("~")
|
||
|
||
|
||
def _suchpfade():
|
||
"""Übliche Orte der config.json des Rechnungstools – erst neben uns, dann daneben."""
|
||
kandidaten = [
|
||
os.path.join(_HIER, "config.json"), # direkt daneben gelegt
|
||
os.path.join(_HIER, "..", "rechnungstool", "config.json"),
|
||
os.path.join(_HIER, "..", "Rechnungstool", "config.json"),
|
||
os.path.join(_HEIM, "Documents", "rechnungstool", "config.json"),
|
||
os.path.join(_HEIM, "Documents", "Rechnungstool", "config.json"),
|
||
r"C:\claude\rechnungstool\config.json", # Entwicklungsrechner
|
||
]
|
||
return [os.path.normpath(p) for p in kandidaten]
|
||
|
||
ERSATZ = {
|
||
"name": "Campinghof Bartl",
|
||
"untertitel": "Camping & Ferienwohnung am Markkleeberger See",
|
||
"absender": "Campinghof Bartl - Bornaer Chaussee 36 - 04416 Markkleeberg OT Wachau",
|
||
"inhaber": "", "bank": "", "iban": "", "bic": "",
|
||
"homepage": "", "email": "", "telefon": "", "steuernummer": "", "logo": "",
|
||
}
|
||
|
||
|
||
def config_pfad(gespeichert: str = "") -> str:
|
||
"""Erster gefundener Pfad zur config.json des Rechnungstools ('' = keiner)."""
|
||
for p in ([gespeichert] if gespeichert else []) + _suchpfade():
|
||
if p and os.path.isfile(p):
|
||
return os.path.abspath(p)
|
||
return ""
|
||
|
||
|
||
def lade(gespeichert: str = "") -> dict:
|
||
"""Firmendaten laden. Der Logo-Pfad wird absolut gemacht."""
|
||
pfad = config_pfad(gespeichert)
|
||
if not pfad:
|
||
log.warning("config.json des Rechnungstools nicht gefunden – Ersatz-Briefkopf")
|
||
return dict(ERSATZ)
|
||
|
||
try:
|
||
with open(pfad, encoding="utf-8") as f:
|
||
cfg = json.load(f)
|
||
except Exception as e: # noqa: BLE001 - kaputte/fremde Datei
|
||
log.warning("config.json nicht lesbar (%s): %s", pfad, e)
|
||
return dict(ERSATZ)
|
||
|
||
daten = dict(ERSATZ)
|
||
daten.update(cfg.get("firma", {}))
|
||
|
||
logo = str(daten.get("logo") or "").strip()
|
||
if logo and not os.path.isabs(logo):
|
||
kandidat = os.path.join(os.path.dirname(pfad), logo)
|
||
daten["logo"] = kandidat if os.path.exists(kandidat) else ""
|
||
|
||
log.info("Firmendaten aus %s", pfad)
|
||
return daten
|