-
Notifications
You must be signed in to change notification settings - Fork 0
/
main copy.py
155 lines (114 loc) · 5.18 KB
/
main copy.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
import json
import ntpath
import tkinter
from tkinter import filedialog, simpledialog
import ics
import invoice
import time_entry
from debitoor_client import DebitoorClient
class AutoInvoice:
def __init__(self):
self.auth_success = False
self.auth_status = None
self.auth_button = None
self.file_info = None
self.loaded_filename = None
self.time_entries = []
self.api_config = {
"clientId": "",
"clientSecret": ""
}
self.settings = {
"accessToken": "",
"serviceName": "Dienstleistung Entwicklung",
"customerId": "5e97dcd2c223eb0023c58ada",
"entriesPerInvoice": 10,
"hourlyRate": 30.0
}
self.load_config()
self.api_client = DebitoorClient(self.api_config["clientId"], self.api_config["clientSecret"])
if self.settings["accessToken"]:
try:
self.api_client.use_access_token(self.settings["accessToken"])
self.auth_success = True
except:
pass
root = tkinter.Tk()
self.create_debitoor_auth(root)
self.create_ics_file_select(root)
self.create_invoices_button(root)
root.mainloop()
def load_config(self):
with open("api.json", "r") as f:
self.api_config = json.load(f)
with open("settings.json", "r") as f:
self.settings = json.load(f)
def save_settings(self):
with open("settings.json", "w") as settings_file:
settings_file.write(json.dumps(self.settings))
def retrieve_access_token(self, parent):
self.api_client.open_oauth_page()
oauth_code = simpledialog.askstring("Authorize AutoInvoice", "Authentication code: ")
access_token = self.api_client.request_access_token(oauth_code)
if access_token:
self.settings["accessToken"] = access_token
self.save_settings()
self.auth_success = True
self.auth_status.set("Authorized")
self.auth_button["state"] = tkinter.DISABLED
def show_ics_file_dialog(self, parent):
ics_filename = filedialog.askopenfilename(parent=parent, initialdir=".", title="Select exported iCalendar file",
filetypes=(("iCalendar files", "*.ics"), ("All files", "*.*")))
if ics_filename:
with open(ics_filename, "r") as ics_file:
c = ics.Calendar(ics_file.read())
print(c.todos)
print(ics_file)
name = "time list"
for extra in c.extra:
if str(extra).startswith("X-WR-CALNAME:"):
name = str(extra)[13:]
self.loaded_filename.set(ntpath.basename(ics_filename))
self.file_info.set("Loaded " + name + " with " + str(len(c.todos)) + " entries")
self.time_entries = time_entry.parse_work_times(c.todos)
def create_debitoor_auth(self, parent):
self.auth_status = tkinter.StringVar(parent, "Debitoor access pending...")
def on_authorize_click():
self.retrieve_access_token(parent)
frame = tkinter.Frame(parent)
frame.pack(fill=tkinter.X, padx=5, pady=5)
auth_info = tkinter.Label(frame, textvariable=self.auth_status)
auth_info.pack(side=tkinter.TOP)
self.auth_button = tkinter.Button(frame, text="Authorize", command=on_authorize_click)
if self.auth_success:
self.auth_status.set("Authorized")
self.auth_button["state"] = tkinter.DISABLED
self.auth_button.pack(side=tkinter.BOTTOM)
def create_ics_file_select(self, parent):
self.file_info = tkinter.StringVar(parent, "No file loaded")
self.loaded_filename = tkinter.StringVar(parent, "")
def on_open_click():
self.show_ics_file_dialog(parent)
frame = tkinter.Frame(parent)
frame.pack(fill=tkinter.X, padx=5, pady=5)
top_frame = tkinter.Frame(frame)
top_frame.pack(side=tkinter.TOP)
ics_file_entry = tkinter.Entry(top_frame, state=tkinter.DISABLED, textvariable=self.loaded_filename)
ics_file_entry.pack(side=tkinter.LEFT, padx=5)
ics_file_dialog_button = tkinter.Button(top_frame, text="Open file", command=on_open_click)
ics_file_dialog_button.pack(side=tkinter.RIGHT)
ics_file_info = tkinter.Label(frame, textvariable=self.file_info)
ics_file_info.pack(side=tkinter.BOTTOM)
def create_invoices_button(self, parent):
def create_invoices():
if len(self.time_entries) < 1:
return
invoices = invoice.create_invoices(self.time_entries, self.settings)
for inv in invoices:
self.api_client.create_invoice_draft(inv)
frame = tkinter.Frame(parent)
frame.pack(fill=tkinter.X, padx=5, pady=5)
button = tkinter.Button(frame, text="Create invoices", command=create_invoices)
button.pack()
if __name__ == '__main__':
AutoInvoice()