-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlexoffice_OOP.py
59 lines (47 loc) · 1.86 KB
/
lexoffice_OOP.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# Standard library imports
import time
from typing import Any
import json
# Third-party imports
import requests
class Lexoffice:
def __init__(self) -> None:
self.session = self._read_cookies_into_session()
def _read_cookies_into_session(self) -> requests.Session:
session = requests.Session()
with open("cookies.json", "r") as file:
cookies = json.load(file)
for cookie in cookies:
session.cookies.set(
cookie["name"], cookie["value"], domain=cookie["domain"]
)
return session
def _get_invoices(self) -> list[dict[str, Any]]:
response = self.session.get(
"https://app.lexoffice.de/grld-rest/voucherservice/1/v200/vouchers?ascending=true&deletedIds=&firstRow=0&numRows=9999&orderBy=computedDueDate&status=open&voucherType=SalesInvoice"
)
invoices = response.json()["content"]
return invoices
def book_invoices(self) -> None:
for invoice in self._get_invoices():
url = f"https://app.lexoffice.de/grld-rest/personalsalesinvoiceservice/1/v100/payment/{invoice['entityId']}"
body = json.dumps(
{
"amount": float(invoice["amount"]),
"postingDate": invoice["voucherDate"].split("T")[0],
"method": "personal",
"balanceReferenceType": "PartPayment",
}
)
response = self.session.put(
url, data=body, headers={"Content-Type": "application/json"}
)
print(
f"[{invoice['identificationNumber']}] Status {response.status_code}: Successfully submitted invoice!"
)
time.sleep(0.1)
def main() -> None:
lexoffice = Lexoffice()
lexoffice.book_invoices()
if __name__ == "__main__":
main()