-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatabase.py
More file actions
257 lines (208 loc) · 7.77 KB
/
Copy pathdatabase.py
File metadata and controls
257 lines (208 loc) · 7.77 KB
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
import sqlite3
import hashlib
import os
from datetime import datetime, date
from typing import Optional
from models import Employee, TimeEntry
DB_PATH = os.path.join(os.path.dirname(os.path.abspath(__file__)), "workinghours.db")
def _hash_password(password: str) -> str:
return hashlib.sha256(password.encode()).hexdigest()
def get_connection() -> sqlite3.Connection:
conn = sqlite3.connect(DB_PATH)
conn.execute("PRAGMA foreign_keys = ON")
return conn
def init_db():
conn = get_connection()
cursor = conn.cursor()
cursor.executescript("""
CREATE TABLE IF NOT EXISTS admin (
id INTEGER PRIMARY KEY,
username TEXT UNIQUE NOT NULL,
password_hash TEXT NOT NULL,
overtime_threshold REAL DEFAULT 8.0
);
CREATE TABLE IF NOT EXISTS employees (
id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT NOT NULL,
pin TEXT NOT NULL UNIQUE,
active INTEGER DEFAULT 1,
created_at TEXT DEFAULT CURRENT_TIMESTAMP
);
CREATE TABLE IF NOT EXISTS time_entries (
id INTEGER PRIMARY KEY AUTOINCREMENT,
employee_id INTEGER NOT NULL,
event_type TEXT NOT NULL CHECK(event_type IN ('IN', 'OUT')),
timestamp TEXT NOT NULL,
FOREIGN KEY (employee_id) REFERENCES employees(id)
);
""")
# Seed default admin if not exists
cursor.execute("SELECT COUNT(*) FROM admin")
if cursor.fetchone()[0] == 0:
cursor.execute(
"INSERT INTO admin (username, password_hash) VALUES (?, ?)",
("admin", _hash_password("admin")),
)
conn.commit()
conn.close()
# --- Admin ---
def verify_admin(username: str, password: str) -> bool:
conn = get_connection()
row = conn.execute(
"SELECT password_hash FROM admin WHERE username = ?", (username,)
).fetchone()
conn.close()
if row is None:
return False
return row[0] == _hash_password(password)
def change_admin_password(username: str, new_password: str):
conn = get_connection()
conn.execute(
"UPDATE admin SET password_hash = ? WHERE username = ?",
(_hash_password(new_password), username),
)
conn.commit()
conn.close()
def get_overtime_threshold() -> float:
conn = get_connection()
row = conn.execute("SELECT overtime_threshold FROM admin LIMIT 1").fetchone()
conn.close()
return row[0] if row else 8.0
def set_overtime_threshold(hours: float):
conn = get_connection()
conn.execute("UPDATE admin SET overtime_threshold = ?", (hours,))
conn.commit()
conn.close()
# --- Employees ---
def add_employee(name: str, pin: str) -> int:
conn = get_connection()
cursor = conn.execute(
"INSERT INTO employees (name, pin) VALUES (?, ?)", (name, pin)
)
conn.commit()
emp_id = cursor.lastrowid
conn.close()
return emp_id
def update_employee(emp_id: int, name: str, pin: str):
conn = get_connection()
conn.execute(
"UPDATE employees SET name = ?, pin = ? WHERE id = ?", (name, pin, emp_id)
)
conn.commit()
conn.close()
def deactivate_employee(emp_id: int):
conn = get_connection()
conn.execute("UPDATE employees SET active = 0 WHERE id = ?", (emp_id,))
conn.commit()
conn.close()
def activate_employee(emp_id: int):
conn = get_connection()
conn.execute("UPDATE employees SET active = 1 WHERE id = ?", (emp_id,))
conn.commit()
conn.close()
def get_all_employees(include_inactive: bool = False) -> list[Employee]:
conn = get_connection()
if include_inactive:
rows = conn.execute("SELECT id, name, pin, active, created_at FROM employees ORDER BY name").fetchall()
else:
rows = conn.execute(
"SELECT id, name, pin, active, created_at FROM employees WHERE active = 1 ORDER BY name"
).fetchall()
conn.close()
return [Employee(id=r[0], name=r[1], pin=r[2], active=bool(r[3]), created_at=r[4]) for r in rows]
def get_employee_by_pin(pin: str) -> Optional[Employee]:
conn = get_connection()
row = conn.execute(
"SELECT id, name, pin, active, created_at FROM employees WHERE pin = ? AND active = 1",
(pin,),
).fetchone()
conn.close()
if row is None:
return None
return Employee(id=row[0], name=row[1], pin=row[2], active=bool(row[3]), created_at=row[4])
def get_employee_by_id(emp_id: int) -> Optional[Employee]:
conn = get_connection()
row = conn.execute(
"SELECT id, name, pin, active, created_at FROM employees WHERE id = ?",
(emp_id,),
).fetchone()
conn.close()
if row is None:
return None
return Employee(id=row[0], name=row[1], pin=row[2], active=bool(row[3]), created_at=row[4])
# --- Time Entries ---
def clock_event(employee_id: int, event_type: str):
conn = get_connection()
now = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
conn.execute(
"INSERT INTO time_entries (employee_id, event_type, timestamp) VALUES (?, ?, ?)",
(employee_id, event_type, now),
)
conn.commit()
conn.close()
def get_last_event(employee_id: int) -> Optional[TimeEntry]:
conn = get_connection()
row = conn.execute(
"SELECT id, employee_id, event_type, timestamp FROM time_entries "
"WHERE employee_id = ? ORDER BY timestamp DESC LIMIT 1",
(employee_id,),
).fetchone()
conn.close()
if row is None:
return None
return TimeEntry(id=row[0], employee_id=row[1], event_type=row[2], timestamp=row[3])
def get_today_entries(employee_id: int) -> list[TimeEntry]:
conn = get_connection()
today = date.today().isoformat()
rows = conn.execute(
"SELECT id, employee_id, event_type, timestamp FROM time_entries "
"WHERE employee_id = ? AND timestamp LIKE ? ORDER BY timestamp",
(employee_id, f"{today}%"),
).fetchall()
conn.close()
return [TimeEntry(id=r[0], employee_id=r[1], event_type=r[2], timestamp=r[3]) for r in rows]
def get_entries_in_range(
start_date: str, end_date: str, employee_id: Optional[int] = None
) -> list[TimeEntry]:
conn = get_connection()
if employee_id:
rows = conn.execute(
"SELECT id, employee_id, event_type, timestamp FROM time_entries "
"WHERE employee_id = ? AND timestamp >= ? AND timestamp < ? || ' 23:59:59' "
"ORDER BY timestamp",
(employee_id, start_date, end_date),
).fetchall()
else:
rows = conn.execute(
"SELECT id, employee_id, event_type, timestamp FROM time_entries "
"WHERE timestamp >= ? AND timestamp <= ? || ' 23:59:59' "
"ORDER BY timestamp",
(start_date, end_date),
).fetchall()
conn.close()
return [TimeEntry(id=r[0], employee_id=r[1], event_type=r[2], timestamp=r[3]) for r in rows]
def calc_today_hours(employee_id: int) -> float:
entries = get_today_entries(employee_id)
return _calc_hours_from_entries(entries)
def _calc_hours_from_entries(entries: list[TimeEntry]) -> float:
total_seconds = 0.0
i = 0
while i < len(entries):
if entries[i].event_type == "IN":
clock_in = entries[i].datetime
if i + 1 < len(entries) and entries[i + 1].event_type == "OUT":
clock_out = entries[i + 1].datetime
total_seconds += (clock_out - clock_in).total_seconds()
i += 2
else:
# Still clocked in - count up to now
total_seconds += (datetime.now() - clock_in).total_seconds()
i += 1
else:
i += 1
return total_seconds / 3600.0
def delete_time_entry(entry_id: int):
conn = get_connection()
conn.execute("DELETE FROM time_entries WHERE id = ?", (entry_id,))
conn.commit()
conn.close()