|
| 1 | +package model |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "log/slog" |
| 6 | + "os" |
| 7 | + "path/filepath" |
| 8 | +) |
| 9 | + |
| 10 | +// PDFEngine selects which engine renders invoice PDFs. Both engines are always |
| 11 | +// compiled in; the choice is made at runtime per owner (settings field |
| 12 | +// pdf_engine), see docs/pdf-engine-dispatch.md. |
| 13 | +type PDFEngine string |
| 14 | + |
| 15 | +const ( |
| 16 | + // PDFEngineAuto picks speedata when the owner has a layout.xml, otherwise |
| 17 | + // boxesandglue. This is the default. |
| 18 | + PDFEngineAuto PDFEngine = "auto" |
| 19 | + // PDFEngineSpeedata renders on the remote speedata Publishing Server. |
| 20 | + PDFEngineSpeedata PDFEngine = "speedata" |
| 21 | + // PDFEngineBag renders locally in-process with boxesandglue/bagme. |
| 22 | + PDFEngineBag PDFEngine = "boxesandglue" |
| 23 | +) |
| 24 | + |
| 25 | +// resolvePDFEngine decides which engine renders the PDF. It is pure so the |
| 26 | +// full decision matrix (choice × layout.xml × server config) is testable. |
| 27 | +// |
| 28 | +// A layout.xml only makes sense with speedata, so "auto" without a configured |
| 29 | +// publishing server is an error rather than a silent fallback to boxesandglue: |
| 30 | +// whoever maintains a layout.xml must not unknowingly get a different layout |
| 31 | +// on their invoices. |
| 32 | +func resolvePDFEngine(choice PDFEngine, hasLayoutXML, serverConfigured bool) (PDFEngine, error) { |
| 33 | + switch choice { |
| 34 | + case PDFEngineBag: |
| 35 | + return PDFEngineBag, nil |
| 36 | + case PDFEngineSpeedata: |
| 37 | + if !serverConfigured { |
| 38 | + return "", fmt.Errorf("PDF-Engine speedata gewählt, aber kein Publishing-Server konfiguriert") |
| 39 | + } |
| 40 | + return PDFEngineSpeedata, nil |
| 41 | + case PDFEngineAuto, "": |
| 42 | + if !hasLayoutXML { |
| 43 | + return PDFEngineBag, nil |
| 44 | + } |
| 45 | + if !serverConfigured { |
| 46 | + return "", fmt.Errorf("layout.xml vorhanden, aber kein Publishing-Server konfiguriert") |
| 47 | + } |
| 48 | + return PDFEngineSpeedata, nil |
| 49 | + default: |
| 50 | + return "", fmt.Errorf("unbekannte PDF-Engine %q", choice) |
| 51 | + } |
| 52 | +} |
| 53 | + |
| 54 | +// hasUserLayoutXML reports whether the owner keeps a layout.xml in their |
| 55 | +// userassets directory (the speedata layout file). |
| 56 | +func (s *Store) hasUserLayoutXML(ownerID uint) bool { |
| 57 | + p := filepath.Join(s.Config.Basedir, "assets", "userassets", fmt.Sprintf("owner%d", ownerID), "layout.xml") |
| 58 | + _, err := os.Stat(p) |
| 59 | + return err == nil |
| 60 | +} |
| 61 | + |
| 62 | +// speedataConfigured reports whether config.toml points to a publishing server. |
| 63 | +func (s *Store) speedataConfigured() bool { |
| 64 | + return s.Config.PublishingServerAddress != "" |
| 65 | +} |
| 66 | + |
| 67 | +// ResolvePDFEngine determines the effective engine for the owner from their |
| 68 | +// settings, their userassets and the server configuration. |
| 69 | +func (s *Store) ResolvePDFEngine(ownerID uint) (PDFEngine, error) { |
| 70 | + settings, err := s.LoadSettings(ownerID) |
| 71 | + if err != nil { |
| 72 | + return "", fmt.Errorf("load settings: %w", err) |
| 73 | + } |
| 74 | + return resolvePDFEngine(PDFEngine(settings.PDFEngine), s.hasUserLayoutXML(ownerID), s.speedataConfigured()) |
| 75 | +} |
| 76 | + |
| 77 | +// CreateZUGFeRDPDF creates a ZUGFeRD PDF file for the invoice with the engine |
| 78 | +// resolved for the owner. The CII XML is expected to exist at xmlpath and the |
| 79 | +// PDF gets written to pdfpath. |
| 80 | +func (s *Store) CreateZUGFeRDPDF(inv *Invoice, ownerID uint, xmlpath string, pdfpath string, logger *slog.Logger) error { |
| 81 | + engine, err := s.ResolvePDFEngine(ownerID) |
| 82 | + if err != nil { |
| 83 | + return err |
| 84 | + } |
| 85 | + if engine == PDFEngineSpeedata { |
| 86 | + return s.createZUGFeRDPDFSpeedata(inv, ownerID, xmlpath, pdfpath, logger) |
| 87 | + } |
| 88 | + return s.createZUGFeRDPDFBag(inv, ownerID, xmlpath, pdfpath, logger) |
| 89 | +} |
| 90 | + |
| 91 | +// AutoLayoutNote describes, for the UI, what the "Automatisch" letterhead |
| 92 | +// choice renders for this owner with the engine resolved from their settings. |
| 93 | +func (s *Store) AutoLayoutNote(ownerID uint) string { |
| 94 | + engine, err := s.ResolvePDFEngine(ownerID) |
| 95 | + if err != nil { |
| 96 | + return "Achtung: " + err.Error() + " — die PDF-Erzeugung schlägt fehl." |
| 97 | + } |
| 98 | + if engine == PDFEngineSpeedata { |
| 99 | + return `Verwendet "layout.xml" über den speedata Publishing-Server.` |
| 100 | + } |
| 101 | + return "Verwendet das eingebaute Standard-Layout (DIN 5008) mit den Firmendaten aus den Einstellungen." |
| 102 | +} |
0 commit comments