This repository has been archived by the owner on Jan 31, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain_tui.py
169 lines (147 loc) · 4.85 KB
/
main_tui.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
from __future__ import annotations
import os
import sys
import textwrap
from dotenv import load_dotenv
from option import Ok, Result
from database.mongo import benefit_repo, department_repo, employee_repo
from frontend.helpers_tui import *
from frontend.tui import *
from models import BenefitPlan, Company, Department, Employee
load_dotenv()
the_company = Company()
def initialize_data():
os.environ["HRMGR_DB"] = "TRUE"
if not employee_repo.find({}):
pass
else:
for employee in employee_repo.find({}):
the_company.employees.append(Employee.parse_obj(employee))
if not department_repo.find({}):
pass
else:
for department in department_repo.find({}):
the_company.departments.append(Department.parse_obj(department))
if not benefit_repo.find({}):
pass
else:
for benefit in benefit_repo.find({}):
the_company.benefits.append(BenefitPlan.parse_obj(benefit))
def main():
last_msg = ""
# fmt: off
if (
not os.getenv("MONGO_USER")
or not os.getenv("MONGO_PASS")
or not os.getenv("MONGO_URI")
):
os.environ["HRMGR_DB"] = "FALSE"
input(
textwrap.dedent(
"""\
It seems like your environment variables are not set up.
The program will now run in memory-only mode.
Press any key to continue."""
)
)
else:
initialize_data()
# fmt: on
# ======================
# WELCOME SCREEN
# ======================
if len(the_company.employees) == 0:
print(
textwrap.dedent(
"""\
Welcome to HR Manager!
It seems like this is your first time using this program.
You will be asked to create an admin account."""
)
)
input(ENTER_TO_CONTINUE_MSG)
else:
first_account_is_admin = the_company.employees[0].is_admin
first_account_name_is_owner = the_company.employees[0].name == "Owner"
only_one_owner = len([employee for employee in the_company.employees if employee.name == "Owner"]) == 1
if not first_account_is_admin:
print(
FCOLORS.RED
+ "WARNING: The first account is not an admin! Contact the IT department immediately!"
+ FCOLORS.END
)
raise KeyboardInterrupt
if not first_account_name_is_owner:
print(
FCOLORS.RED
+ "WARNING: The first account's name is not 'Owner'! Contact the IT department immediately!"
+ FCOLORS.END
)
raise KeyboardInterrupt
elif not only_one_owner:
print(
FCOLORS.RED
+ "WARNING: There are more than one owner accounts! Contact the IT department immediately!"
+ FCOLORS.END
)
raise KeyboardInterrupt
# ==========================
# LOGIN/SIGNUP
# ==========================
menu_login_signup = MenuLoginSignup()
is_logged_in = False
if len(the_company.employees) == 0:
is_logged_in = menu_login_signup.signup_admin()
else:
is_logged_in = menu_login_signup.login()
if not is_logged_in:
raise KeyboardInterrupt
# ==========================
# MAIN MENU
# ==========================
while is_logged_in:
clrscr()
last_msg = ""
if last_msg:
print(last_msg)
last_msg = ""
main_menu = [
"[1] Employee management",
"[2] Benefit plan management",
"[3] Attendance management",
"[4] Payroll management",
"[5] Department management",
"[6] Performance management",
"[7] Exit",
]
user_choice = get_user_option_from_menu("Main menu", main_menu)
if user_choice in [3, 4, 6] and not the_company.employees:
last_msg = NO_EMPLOYEE_MSG
continue
respond: Result[None, str] = Ok(None)
match user_choice:
case 1:
respond = MenuEmployee().mainloop()
case 2:
respond = MenuBenefits().mainloop()
case 3:
respond = MenuAttendance().mainloop()
case 4:
respond = MenuPayroll().mainloop()
case 5:
respond = MenuDepartment().mainloop()
case 6:
respond = MenuPerformance().mainloop()
case 7:
break
case _:
last_msg = FCOLORS.RED + "Invalid choice!" + FCOLORS.END
try:
respond.unwrap()
except (ValueError, TypeError) as e:
last_msg = str(e)
if __name__ == "__main__":
try:
main()
except KeyboardInterrupt:
sys.exit(0)