| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115 |
- import json
- import os
- import docuware
- from dotenv import load_dotenv
- load_dotenv()
- FILE_CABINETS = {
- "1": "Belegpool Reisacher",
- "2": "Belegpool Reisacher",
- "3": "Belegpool Reisacher Electric Mobility",
- "4": "Belegpool Reisacher SEK",
- }
- def main():
- dw = docuware.connect()
- for org in dw.organizations:
- print(org)
- for fc in org.file_cabinets:
- print(" ", fc)
- for b in org.baskets:
- print(" ", b)
- # dlg = fc.search_dialog()
- # for field in dlg.fields.values():
- # print("Id =", field.id)
- # print("Length=", field.length)
- # print("Name =", field.name)
- # print("Type =", field.type)
- # print("-------")
- def get_file_cabinet(client_db: str) -> docuware.FileCabinet:
- dw = docuware.connect()
- return dw.organization("1").file_cabinet(FILE_CABINETS[client_db])
- def search_files(
- client_db: str, customer_no: str | None = None, vehicle_no: str | None = None, document_no: str | None = None
- ) -> list[list[str]]:
- search_condition = {}
- if customer_no:
- search_condition["KUNDENNUMMER"] = customer_no
- if vehicle_no:
- search_condition["FAHRGESTELLNUMMER"] = vehicle_no
- if document_no:
- search_condition["BELEGNUMMER"] = document_no
- fc = get_file_cabinet(client_db)
- dlg = fc.search_dialog()
- res = []
- for doc in dlg.search(search_condition):
- # r = doc.document.__dict__
- # r["file_cabinet"] = None
- # r["attachments"] = r["attachments"][0].pages
- # r["modified"] = str(r["modified"])
- # r["created"] = str(r["created"])
- # r["fields"] = {f.name: str(f.value) for f in doc.document.fields }
- # r["endpoints"] = {k: str(v) for k, v in doc.document.endpoints.items() }
- # res.append(r)
- fields = {f.name: str(f.value) for f in doc.document.fields}
- r = {
- "ID": doc.document.id,
- "Datei_Typ": fields["File type"],
- "Datei_Link": doc.document.endpoints["self"],
- "Beleg_Typ": fields["Dok.-/ Belegtyp"],
- "Beleg_Nr": fields["Belegnummer"],
- "Kunde_Nr": fields["Kundennummer"],
- "Kunde_Name": fields["Name"],
- "Fahrzeug_Nr": fields["Fahrgestellnummer"],
- "Scan_Datum": str(doc.document.modified),
- "Beleg_Datum": fields["Beleg-Datum"],
- "Bearbeiter": fields["Bearbeitet von:"],
- "Seiten": doc.document.attachments[0].pages,
- "Betrag": fields["Rechnungsbetrag"],
- }
- res.append(r)
- # Document ID 6809334
- # File type .pdf
- # Dok.-/ Belegtyp GA-KAUFVERTRAG MIT UNTERSCHRIFT
- # Belegnummer 153968
- # Kundennummer None
- # Fahrgestellnummer WBA21FZ070FT19353
- # Modified on 2025-09-29 07:32:23.893000
- # Beleg-Datum 2025-09-29
- # Bearbeitet von: SCANENGINE
- # Rechnungsbetrag None
- with open("docuware.json", "w") as fwh:
- json.dump(res, fwh, indent=2)
- def get_invoice_pdf_by_document_no(client_db: str, document_no: str) -> bytes:
- search_condition = {}
- search_condition["BELEGNUMMER"] = document_no
- fc = get_file_cabinet(client_db)
- dlg = fc.search_dialog()
- for doc in dlg.search(search_condition):
- # return os.getenv("DW_URL").strip("/") + doc.document.endpoints["content"]
- data, content_type, filename = doc.download(keep_annotations=True)
- return data
- raise ValueError(f"Document with document_no {document_no} not found in client_db {client_db}")
- if __name__ == "__main__":
- # main()
- # search_files(client_db="1", vehicle_no="WBA21FZ070FT19353")
- print(get_invoice_pdf_by_document_no("1", "WRG25117581"))
|