docuware_search.py 3.7 KB

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