docuware_search.py 3.7 KB

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