-
Notifications
You must be signed in to change notification settings - Fork 1
/
test_app.py
326 lines (284 loc) · 14.3 KB
/
test_app.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
import streamlit as st
import mysql.connector as ms
from connection import is_connected, get_database_connection
from tabulate import tabulate
import pandas as pd
# Function to establish connection
def establish_connection():
flag = is_connected()
if flag:
try:
connection = get_database_connection()
return connection
except ms.Error as e:
st.error(f"Error: {e}")
else:
st.error("Failed to connect to MySQL")
return None
# Insert data function for Streamlit
def insert_data(connection):
db_tables = ["employees", "departments", "projects", "employee_project", "salaries"]
table_name = st.selectbox("Select the table to insert data into:", db_tables)
cursor = connection.cursor()
if table_name == "employees":
emp_no = st.number_input("Enter employee number:", min_value=1, step=1)
first_name = st.text_input("Enter first name:")
last_name = st.text_input("Enter last name:")
job_title = st.text_input("Enter job title:")
basic_salary = st.number_input("Enter basic salary:", min_value=0.0, step=0.01)
department_id = st.number_input("Enter department id:", min_value=1, step=1)
if st.button("Insert"):
query = "INSERT INTO Employees (emp_no, first_name, last_name, job_title, basic_salary, department_id) VALUES (%s, %s, %s, %s, %s, %s)"
values = (emp_no, first_name, last_name, job_title, basic_salary, department_id)
cursor.execute(query, values)
connection.commit()
st.success("Data inserted into employees table successfully.")
elif table_name == "departments":
department_id = st.number_input("Enter department id:", min_value=1, step=1)
department_name = st.text_input("Enter department name:")
manager_id = st.number_input("Enter manager id:", min_value=1, step=1)
if st.button("Insert"):
query = "INSERT INTO Departments (department_id, department_name, manager_id) VALUES (%s, %s, %s)"
values = (department_id, department_name, manager_id)
cursor.execute(query, values)
connection.commit()
st.success("Data inserted into departments table successfully.")
elif table_name == "projects":
project_id = st.number_input("Enter project id:", min_value=1, step=1)
project_name = st.text_input("Enter project name:")
start_date = st.date_input("Enter start date:")
end_date = st.date_input("Enter end date:")
department_id = st.number_input("Enter department id:", min_value=1, step=1)
if st.button("Insert"):
query = "INSERT INTO Projects (project_id, project_name, start_date, end_date, department_id) VALUES (%s, %s, %s, %s, %s)"
values = (project_id, project_name, start_date, end_date, department_id)
cursor.execute(query, values)
connection.commit()
st.success("Data inserted into projects table successfully.")
elif table_name == "employee_project":
emp_no = st.number_input("Enter employee number:", min_value=1, step=1)
project_id = st.number_input("Enter project id:", min_value=1, step=1)
hours_worked = st.number_input("Enter hours worked:", min_value=0.0, step=0.01)
if st.button("Insert"):
query = "INSERT INTO Employee_Project (emp_no, project_id, hours_worked) VALUES (%s, %s, %s)"
values = (emp_no, project_id, hours_worked)
cursor.execute(query, values)
connection.commit()
st.success("Data inserted into employee_project table successfully.")
elif table_name == "salaries":
emp_no = st.number_input("Enter employee number:", min_value=1, step=1)
salary_date = st.date_input("Enter salary date:")
basic_salary = st.number_input("Enter basic salary:", min_value=0.0, step=0.01)
if st.radio("Is the Employee's residence rented?", ('Yes', 'No')) == 'Yes':
fda, fhra = 0.5, 0.4
else:
fda, fhra = 0.5, 0
da = fda * basic_salary
hra = fhra * basic_salary
gross_salary = basic_salary + da + hra
taxable_income = gross_salary - basic_salary
if taxable_income <= 250000:
tax = 0
elif taxable_income <= 500000:
tax = 0.05 * (taxable_income - 250000)
elif taxable_income <= 1000000:
tax = 12500 + 0.2 * (taxable_income - 500000)
else:
tax = 112500 + 0.3 * (taxable_income - 1000000)
net_salary = gross_salary - tax
if st.button("Insert"):
query = "INSERT INTO Salaries (emp_no, salary_date, basic_salary, da, hra, gross_salary, tax, net_salary) VALUES (%s, %s, %s, %s, %s, %s, %s, %s)"
values = (emp_no, salary_date, basic_salary, da, hra, gross_salary, tax, net_salary)
cursor.execute(query, values)
connection.commit()
st.success("Data inserted into salaries table successfully.")
# Update data function for Streamlit
def update_data(connection):
db_tables = ["employees", "departments", "projects", "employee_project", "salaries"]
table_name = st.selectbox("Select the table to update data in:", db_tables)
cursor = connection.cursor()
if table_name == "employees":
emp_no = st.number_input("Enter the employee number to update:", min_value=1, step=1)
column_name = st.text_input("Enter the column name to update:")
new_value = st.text_input("Enter the new value:")
if st.button("Update"):
query = f"UPDATE Employees SET {column_name} = %s WHERE emp_no = %s"
values = (new_value, emp_no)
cursor.execute(query, values)
connection.commit()
st.success("Data in employees table updated successfully.")
elif table_name == "departments":
department_id = st.number_input("Enter the department id to update:", min_value=1, step=1)
column_name = st.text_input("Enter the column name to update:")
new_value = st.text_input("Enter the new value:")
if st.button("Update"):
query = f"UPDATE Departments SET {column_name} = %s WHERE department_id = %s"
values = (new_value, department_id)
cursor.execute(query, values)
connection.commit()
st.success("Data in departments table updated successfully.")
elif table_name == "projects":
project_id = st.number_input("Enter the project id to update:", min_value=1, step=1)
column_name = st.text_input("Enter the column name to update:")
new_value = st.text_input("Enter the new value:")
if st.button("Update"):
query = f"UPDATE Projects SET {column_name} = %s WHERE project_id = %s"
values = (new_value, project_id)
cursor.execute(query, values)
connection.commit()
st.success("Data in projects table updated successfully.")
elif table_name == "employee_project":
emp_no = st.number_input("Enter the employee number to update:", min_value=1, step=1)
project_id = st.number_input("Enter the project id to update:", min_value=1, step=1)
column_name = st.text_input("Enter the column name to update:")
new_value = st.text_input("Enter the new value:")
if st.button("Update"):
query = f"UPDATE Employee_Project SET {column_name} = %s WHERE emp_no = %s AND project_id = %s"
values = (new_value, emp_no, project_id)
cursor.execute(query, values)
connection.commit()
st.success("Data in employee_project table updated successfully.")
elif table_name == "salaries":
emp_no = st.number_input("Enter the employee number to update:", min_value=1, step=1)
salary_date = st.date_input("Enter the salary date to update:")
column_name = st.text_input("Enter the column name to update:")
new_value = st.text_input("Enter the new value:")
if st.button("Update"):
query = f"UPDATE Salaries SET {column_name} = %s WHERE emp_no = %s AND salary_date = %s"
values = (new_value, emp_no, salary_date)
cursor.execute(query, values)
connection.commit()
st.success("Data in salaries table updated successfully.")
# Delete data function for Streamlit
def delete_data(connection):
db_tables = ["employees", "departments", "projects", "employee_project", "salaries"]
table_name = st.selectbox("Select the table to delete data from:", db_tables)
cursor = connection.cursor()
if table_name == "employees":
emp_no = st.number_input("Enter the employee number to delete:", min_value=1, step=1)
if st.button("Delete"):
query = "DELETE FROM Employees WHERE emp_no = %s"
values = (emp_no,)
cursor.execute(query, values)
connection.commit()
st.success("Data deleted from employees table successfully.")
elif table_name == "departments":
department_id = st.number_input("Enter the department id to delete:", min_value=1, step=1)
if st.button("Delete"):
query = "DELETE FROM Departments WHERE department_id = %s"
values = (department_id,)
cursor.execute(query, values)
connection.commit()
st.success("Data deleted from departments table successfully.")
elif table_name == "projects":
project_id = st.number_input("Enter the project id to delete:", min_value=1, step=1)
if st.button("Delete"):
query = "DELETE FROM Projects WHERE project_id = %s"
values = (project_id,)
cursor.execute(query, values)
connection.commit()
st.success("Data deleted from projects table successfully.")
elif table_name == "employee_project":
emp_no = st.number_input("Enter the employee number to delete:", min_value=1, step=1)
project_id = st.number_input("Enter the project id to delete:", min_value=1, step=1)
if st.button("Delete"):
query = "DELETE FROM Employee_Project WHERE emp_no = %s AND project_id = %s"
values = (emp_no, project_id)
cursor.execute(query, values)
connection.commit()
st.success("Data deleted from employee_project table successfully.")
elif table_name == "salaries":
emp_no = st.number_input("Enter the employee number to delete:", min_value=1, step=1)
salary_date = st.date_input("Enter the salary date to delete:")
if st.button("Delete"):
query = "DELETE FROM Salaries WHERE emp_no = %s AND salary_date = %s"
values = (emp_no, salary_date)
cursor.execute(query, values)
connection.commit()
st.success("Data deleted from salaries table successfully.")
# Generate payslip function for Streamlit
def generate_payslip(connection):
st.title("Generate Employee Payslip")
emp_no = st.number_input("Enter the employee number:", min_value=1, step=1)
salary_date = st.date_input("Enter the salary date:")
cursor = connection.cursor()
query = """
SELECT emp_no, salary_date, basic_salary, da, hra, gross_salary, tax, net_salary
FROM Salaries
WHERE emp_no = %s AND salary_date = %s
"""
values = (emp_no, salary_date)
cursor.execute(query, values)
result = cursor.fetchone()
if result:
emp_no, salary_date, basic_salary, da, hra, gross_salary, tax, net_salary = result
# Creating a DataFrame for better formatting
data = {
"Description": ["Employee Number", "Salary Date", "Basic Salary", "DA", "HRA", "Gross Salary", "Tax", "Net Salary"],
"Amount": [emp_no, salary_date, basic_salary, da, hra, gross_salary, tax, net_salary]
}
df = pd.DataFrame(data)
st.markdown(f"""
<div style="text-align: center; padding: 20px;">
<h2 style="color: #4CAF50;">PAYSLIP</h2>
</div>
""", unsafe_allow_html=True)
st.table(df)
st.markdown("""
<style>
.stTable tbody tr:nth-child(even) {
background-color: #f2f2f2;
}
.stTable th {
background-color: #4CAF50;
color: white;
}
</style>
""", unsafe_allow_html=True)
st.markdown(f"""
<div style="text-align: center; padding: 20px;">
<p><strong>Generated on:</strong> {pd.Timestamp('now')}</p>
</div>
""", unsafe_allow_html=True)
else:
st.error("No payslip found for the given employee number and salary date.")
# Display table contents function for Streamlit
def display_table_contents(connection):
db_tables = ["employees", "departments", "projects", "employee_project", "salaries"]
table_name = st.selectbox("Select the table to display data:", db_tables)
cursor = connection.cursor()
query = f"SELECT * FROM {table_name}"
try:
cursor.execute(query)
rows = cursor.fetchall()
if not rows:
st.warning(f"No data found in {table_name} table.")
return
column_names = [i[0] for i in cursor.description]
st.write(f"\nContents of {table_name} Table:")
st.table(pd.DataFrame(rows, columns=column_names))
except ms.Error as e:
st.error(f"Error fetching data from {table_name} table: {e}")
# Streamlit app layout
def main():
st.title("Company Management System")
connection = establish_connection()
cursor = connection.cursor()
cursor.execute("USE companymanagementdb")
if connection:
st.sidebar.title("Actions")
option = st.sidebar.selectbox("Select an action:", ["Insert Data", "Update Data", "Delete Data", "Generate Payslip", "Display Table Contents"])
if option == "Insert Data":
insert_data(connection)
elif option == "Update Data":
update_data(connection)
elif option == "Delete Data":
delete_data(connection)
elif option == "Generate Payslip":
generate_payslip(connection)
elif option == "Display Table Contents":
display_table_contents(connection)
if connection.is_connected():
connection.close()
if __name__ == '__main__':
main()