-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathemployee.py
334 lines (279 loc) · 14 KB
/
employee.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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
from tkinter import *
import tkinter.ttk as ttk
import os
import calendar
import io
import json
import datetime
# Employee at the top
class Employee(ttk.LabelFrame):
def __init__(self, parent, controller, *args, **kwargs):
ttk.LabelFrame.__init__(self, parent, *args, **kwargs)
# styles
style = ttk.Style()
style.configure('Next.TButton', width=3, height=3)
self.controller = controller
self.names = []
self.hol_days = 0
# initialize employee list
for file in sorted(os.listdir("./Pracownicy")):
if not str(file).startswith("_"):
self.names.append(str(file))
self.index = 0 # index of names list
self.name = StringVar()
self.employee_data = 0
self.employee_sett = {}
# Combobox with employee names
self.name_box = ttk.Combobox(self, justify = "center", textvariable = self.name, height = 30, width=25, font=('Arial', 10))
self.name_box['values'] = self.names
self.name_box.bind("<<ComboboxSelected>>", lambda _ : self.change_combo())
self.name.set(self.names[self.index])
self.name_box.grid(row=0, column=1)
# buttons to navigate through employees
self.button_left = ttk.Button(self, text="<<", command=lambda : self.change_employee(-1), cursor = "sb_left_arrow", style='Next.TButton')
self.button_right = ttk.Button(self, text=">>", command=lambda: self.change_employee(1), cursor = "sb_right_arrow", style='Next.TButton')
self.button_left.grid(row=0, column = 0)
self.button_right.grid(row=0, column = 2)
# hire/fire date
self.hire_s = StringVar()
self.fire_s = StringVar()
self.hire = ttk.Label(self, textvariable = self.hire_s)
self.fire = ttk.Label(self, textvariable = self.fire_s)
self.hire_s.set("Zatrudniony od: ")
self.fire_s.set("Zatrudniony do: ")
self.hire.grid(row=1, column=0, columnspan=3)
self.fire.grid(row=2, column=0, columnspan=3)
# changing the employee
def change_employee(self, offstet):
self.save_data()
self.index += offstet
if self.index >= len(self.names):
self.index = 0
elif self.index < 0:
self.index = len(self.names) -1
self.name.set(self.names[self.index])
x = self.master.month.get_date()
date = datetime.datetime(year= x[1], month=x[0], day = 1)
self.master.input_table.make_month(date)
self.master.work_time.make_month(date)
self.read_data()
def change_combo(self):
self.name.set(self.name.get())
self.index = self.name_box.current()
x = self.master.month.get_date()
date = datetime.datetime(year= x[1], month=x[0], day = 1)
self.master.input_table.make_month(date)
self.master.work_time.make_month(date)
self.read_data()
def reload_list(self):
del self.names[:]
for file in sorted(os.listdir("./Pracownicy")):
if self.controller.menu.show_all.get():
if "." not in str(file):
if str(file).startswith("_"):
file = "[Z]" + str(file)[1:]
self.names.append(str(file))
self.name_box['values'] = self.names
self.index = 0
self.name.set(self.names[self.index])
else:
if not str(file).startswith("_"):
if "." not in str(file):
self.names.append(str(file))
self.name_box['values'] = self.names
self.index = 0
self.name.set(self.names[self.index])
def read_data(self):
date = self.master.month.get_date() # selected date
curr_empl = self.name.get() # selected employee
month_len = calendar.monthrange(date[1], date[0])[1]
if curr_empl.startswith("[Z]"):
curr_empl = "_" + curr_empl[3:]
#if file doesnt exist -> jump to next year
if not os.path.isfile('./Pracownicy/' + curr_empl + '/' + str(date[1]) + ".json"):
self.make_new_year(date, curr_empl)
# load the self.employee_sett
try:
with open ('./Pracownicy/' + curr_empl + "/settings.json", "r") as f:
self.employee_sett = json.load(f)
except FileNotFoundError:
self.employee_sett = {
"hire_date" : "01/01/2000",
"fire_date" : ""
}
with open ('./Pracownicy/' + curr_empl + "/settings.json", "w") as f:
json.dump(self.employee_sett, f, indent=4, ensure_ascii=False)
self.hire_s.set("Zatrudniony od: "+ str(self.employee_sett["hire_date"]))
self.fire_s.set("Zatrudniony do: " + str(self.employee_sett["fire_date"]))
self.fire.grid()
# check the hire/fire dates
if self.employee_sett["fire_date"] == "":
self.fire.grid_remove()
if datetime.datetime.strptime(self.employee_sett["hire_date"], "%d/%m/%Y") > datetime.datetime(date[1], date[0], month_len):
self.controller.set_status("Pracownik nie był zatrudniony w tym miesiącu")
return
if self.employee_sett["fire_date"] != "":
if datetime.datetime.strptime(self.employee_sett["fire_date"], "%d/%m/%Y") < datetime.datetime(date[1], date[0], 1):
self.controller.set_status("Pracownik nie był zatrudniony w tym miesiącu")
return
with open ('./Pracownicy/' + curr_empl + "/" + str(date[1]) + ".json", "r") as f1:
data = json.load(f1)
#check if all empty
def is_empty():
if not data["worked"][str(date[0])]:
return True
for day in data["worked"][str(date[0])]:
for val in data["worked"][str(date[0])][day]:
if val != "":
return False
return True
if is_empty():
sum1 = 0
for row in range(1, month_len + 1):
data["worked"][str(date[0])][str(row)] = []
# iterate over columns
for col in range(1, 5):
if col < 3:
self.master.work_time.entries[row, col].delete(0, END)
self.master.work_time.entries[row, col].insert(0, self.master.input_table.entries[row, col].get())
data["worked"][str(date[0])][str(row)].append(self.master.work_time.entries[row, col].get())
data["worked"][str(date[0])][str(row)].append(self.master.work_time.entries[row, 10].get())
self.master.summary.sum_hours()
self.save_data()
# deal with holidays
self.hol_days = data["yearly_holiday"]
self.master.summary.back_1.delete(0, END)
self.master.summary.back_1.insert(0, data[str("holiday_left_" + str(date[1] - 1))])
self.master.summary.back_2.delete(0, END)
self.master.summary.back_2.insert(0, data[str("holiday_left_" + str(date[1] - 2))])
# iterate over days in current month
for row, day in enumerate(data["worked"][str(date[0])], 1):
# iterate over hours in current day
for col, hour in enumerate(data["worked"][str(date[0])][day], 1):
# 5th item in array is a description
if col == 5:
self.master.work_time.entries[row, 10].delete(0, END)
self.master.work_time.entries[row, 10].insert(0, hour)
else:
self.master.work_time.entries[row, col].delete(0, END)
self.master.work_time.entries[row, col].insert(0, hour)
# sum used holiday
self.master.summary.used_leave_y.delete(0, 10)
self.master.summary.used_leave_y.insert(0, sum(data["holiday"]))
self.master.work_time.calculate_all()
self.master.summary.sum_holiday()
self.controller.set_status("Wczytano dane pracownika " + self.name.get())
def save_data(self, y = 0, b_1 = 0, b_2 = 0):
self.master.summary.sum_holiday()
date = self.master.month.get_date() # selected date
curr_empl = self.name.get() # selected employee
if curr_empl.startswith("[Z]"):
curr_empl = "_" + curr_empl[3:]
# if employee file doesnt exist, create a new one
if not os.path.isfile('./Pracownicy/' + curr_empl + '/' + str(date[1]) + ".json"):
with open ('./Pracownicy/' + curr_empl + "/" + str(date[1]) + ".json", "w") as f1:
to_save = {
"yearly_holiday" : y,
str("holiday_left_" + str(date[1] - 1)) : b_1,
str("holiday_left_" + str(date[1] - 2)) : b_2,
"holiday" : [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
"worked" : {"1" : {}, "2" : {}, "3" : {}, "4" : {}, "5" : {}, "6" : {}, "7" : {}, "8" : {}, "9" : {}, "10" : {}, "11" : {}, "12" : {}}
}
json.dump(to_save, f1, indent=4)
# change to save
with open ('./Pracownicy/' + curr_empl + "/" + str(date[1]) + ".json", "r") as f1:
x = json.load(f1)
lenght = calendar.monthrange(date[1], date[0])[1]
to_save = {} # dictionary to change
sum1 = 0
# iterate over rows
for row in range(1, lenght + 1):
x["worked"][str(date[0])][str(row)] = []
# iterate over columns
for col in range(1, 5):
x["worked"][str(date[0])][str(row)].append(self.master.work_time.entries[row, col].get())
x["worked"][str(date[0])][str(row)].append(self.master.work_time.entries[row, 10].get())
# add holiday
x["holiday"][date[0]-1] = int(self.master.summary.used_leave_m.get())
# save to employee file
with open ('./Pracownicy/' + curr_empl + "/" + str(date[1]) + ".json", "w") as f1:
json.dump(x, f1,indent=4, ensure_ascii=False)
# save to yearly summary
# if yearly summary doesnt exist
if not os.path.isfile('./Roczne Podsumowanie/' + str(date[1]) + ".json"):
self.make_summary(date)
with open('./Roczne Podsumowanie/' + str(date[1]) + ".json", 'r') as f:
summ = json.load(f)
# check if employee is in summary
if self.name.get() in summ["employees"]:
summ["employees"][self.name.get()]["holiday"][date[0]-1] = int(self.master.summary.used_leave_m.get())
summ["employees"][self.name.get()]["work"][date[0]-1] = self.master.summary.sum_entries[1,1].get()
summ["employees"][self.name.get()]["yearly_holiday"] = x["yearly_holiday"]
summ["employees"][self.name.get()][str("holiday_left_" + str(date[1] - 1))] = x[str("holiday_left_" + str(date[1] - 1))]
summ["employees"][self.name.get()][str("holiday_left_" + str(date[1] - 2))] = x[str("holiday_left_" + str(date[1] - 2))]
else:
summ["employees"][self.name.get()] = {
"work" : 12 * [0],
"holiday" : 12 * [0],
"yearly_holiday" : x["yearly_holiday"],
str("holiday_left_" + str(date[1] - 1)) : 0,
str("holiday_left_" + str(date[1] - 2)) : 0,
}
with open('./Roczne Podsumowanie/' + str(date[1]) + ".json", 'w') as f:
json.dump(summ, f, indent=4)
#self.read_data()
self.master.work_time.calculate_all()
self.master.summary.sum_holiday()
self.controller.set_status("Ostatni zapis: " + datetime.datetime.now().strftime("%d/%m/%Y, %H:%M:%S"))
def make_new_year(self, date, curr_empl):
# copy the values to summary from previous year file
try:
with open ('./Pracownicy/' + curr_empl + "/" + str(date[1] - 1) + ".json", "r") as f1:
year_1 = json.load(f1)
# if employee didnt use everything from back_2
b2 = int(year_1[str("holiday_left_" + str(date[1] - 2))])
b1 = int(year_1[str("holiday_left_" + str(date[1] - 3))])
u_y = sum(year_1["holiday"])
t = int(year_1["yearly_holiday"]) - u_y + b2 + b1
except FileNotFoundError:
b2 = 0
b1 = 0
u_y = 0
t = 26
if b2 >= u_y:
back_2 = b1
yearly_hol = t - b1 - b2 + u_y
back_1 = yearly_hol
# if employee used everything from back_2 but not from back_1
else:
if b2 + b1 >= u_y:
back_2 = 0
yearly_hol = t - b1 - b2 + u_y
back_1 = yearly_hol
# if employee used part of holiday from last year
else:
back_2 = 0
yearly_hol = t - b1 - b2 + u_y
back_1 = t
self.master.summary.used_leave_m.delete(0, END)
self.master.summary.used_leave_m.insert(0, "0")
self.save_data(y=yearly_hol, b_1=back_1, b_2=back_2)
def make_summary(self, date):
# create a new dict
summary = {
"employees" : {}
}
names = []
# iterate over employees
for file in sorted(os.listdir("./Pracownicy")):
names.append(str(file))
for name in names:
summary["employees"][name] = {
"work" : 12 * [0],
"holiday" : 12 * [0],
"yearly_holiday" : 0,
str("holiday_left_" + str(date[1] - 1)) : 0,
str("holiday_left_" + str(date[1] - 2)) : 0,
}
with open('./Roczne Podsumowanie/' + str(date[1]) + ".json", 'w') as to_save:
json.dump(summary, to_save, indent=4, ensure_ascii=False)