docuware_search.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. import json
  2. import logging
  3. import docuware
  4. from dotenv import load_dotenv
  5. from .logging_setup import run_cli
  6. logger = logging.getLogger(__name__)
  7. load_dotenv()
  8. FILE_CABINETS = {
  9. "1": "Belegpool Reisacher",
  10. "2": "Belegpool Reisacher",
  11. "3": "Belegpool Reisacher Electric Mobility",
  12. "4": "Belegpool Reisacher SEK",
  13. }
  14. def main():
  15. dw = docuware.connect()
  16. for org in dw.organizations:
  17. logger.debug("DocuWare-Organisation verfügbar")
  18. for fc in org.file_cabinets:
  19. logger.debug("DocuWare-Ablage verfügbar")
  20. for b in org.baskets:
  21. logger.debug("DocuWare-Briefkorb verfügbar")
  22. def get_file_cabinet(client_db: str) -> docuware.FileCabinet:
  23. dw = docuware.connect()
  24. return dw.organization("1").file_cabinet(FILE_CABINETS[client_db])
  25. def search_files(
  26. client_db: str, customer_no: str | None = None, vehicle_no: str | None = None, document_no: str | None = None
  27. ) -> list[list[str]]:
  28. search_condition = {}
  29. if customer_no:
  30. search_condition["KUNDENNUMMER"] = customer_no
  31. if vehicle_no:
  32. search_condition["FAHRGESTELLNUMMER"] = vehicle_no
  33. if document_no:
  34. search_condition["BELEGNUMMER"] = document_no
  35. fc = get_file_cabinet(client_db)
  36. dlg = fc.search_dialog()
  37. res = []
  38. for doc in dlg.search(search_condition):
  39. # r = doc.document.__dict__
  40. # r["file_cabinet"] = None
  41. # r["attachments"] = r["attachments"][0].pages
  42. # r["modified"] = str(r["modified"])
  43. # r["created"] = str(r["created"])
  44. # r["fields"] = {f.name: str(f.value) for f in doc.document.fields }
  45. # r["endpoints"] = {k: str(v) for k, v in doc.document.endpoints.items() }
  46. # res.append(r)
  47. fields = {f.name: str(f.value) for f in doc.document.fields}
  48. r = {
  49. "ID": doc.document.id,
  50. "Datei_Typ": fields["File type"],
  51. "Datei_Link": doc.document.endpoints["self"],
  52. "Beleg_Typ": fields["Dok.-/ Belegtyp"],
  53. "Beleg_Nr": fields["Belegnummer"],
  54. "Kunde_Nr": fields["Kundennummer"],
  55. "Kunde_Name": fields["Name"],
  56. "Fahrzeug_Nr": fields["Fahrgestellnummer"],
  57. "Scan_Datum": str(doc.document.modified),
  58. "Beleg_Datum": fields["Beleg-Datum"],
  59. "Bearbeiter": fields["Bearbeitet von:"],
  60. "Seiten": doc.document.attachments[0].pages,
  61. "Betrag": fields["Rechnungsbetrag"],
  62. }
  63. res.append(r)
  64. # Document ID 6809334
  65. # File type .pdf
  66. # Dok.-/ Belegtyp GA-KAUFVERTRAG MIT UNTERSCHRIFT
  67. # Belegnummer 153968
  68. # Kundennummer None
  69. # Fahrgestellnummer WBA21FZ070FT19353
  70. # Modified on 2025-09-29 07:32:23.893000
  71. # Beleg-Datum 2025-09-29
  72. # Bearbeitet von: SCANENGINE
  73. # Rechnungsbetrag None
  74. with open("docuware.json", "w") as fwh:
  75. json.dump(res, fwh, indent=2)
  76. def get_invoice_pdf_by_document_no(client_db: str, document_no: str) -> bytes:
  77. search_condition = {}
  78. search_condition["BELEGNUMMER"] = document_no
  79. fc = get_file_cabinet(client_db)
  80. dlg = fc.search_dialog()
  81. for doc in dlg.search(search_condition):
  82. # return os.getenv("DW_URL").strip("/") + doc.document.endpoints["content"]
  83. data, content_type, filename = doc.document.download(keep_annotations=True)
  84. return data
  85. raise ValueError(f"Document with document_no {document_no} not found in client_db {client_db}")
  86. if __name__ == "__main__":
  87. # main()
  88. # search_files(client_db="1", vehicle_no="WBA21FZ070FT19353")
  89. def download_sample():
  90. data = get_invoice_pdf_by_document_no("1", "WRG25117581")
  91. logger.info("PDF abgerufen", extra={"event": "docuware_pdf_downloaded", "size_bytes": len(data)})
  92. run_cli(download_sample)