3
3
import sys
4
4
import backend
5
5
backend .connect_database ()
6
+ employee_data = None
6
7
def create_styled_frame (parent , min_size = None , style = "" ):
7
8
"""Create a styled QFrame with optional minimum size and custom style."""
8
9
frame = QtWidgets .QFrame (parent )
@@ -68,6 +69,7 @@ def create_input_field(parent, label_text, min_label_size=(120, 0)):
68
69
layout .addWidget (label )
69
70
layout .addWidget (line_edit )
70
71
return frame , line_edit
72
+
71
73
def show_popup_message (parent , message : str , page : int = None , show_cancel : bool = True ):
72
74
"""Reusable popup message box.
73
75
@@ -131,6 +133,7 @@ def on_reject():
131
133
button_box .rejected .connect (on_reject )
132
134
133
135
dialog .exec_ ()
136
+
134
137
def get_employee_name (parent , name_field_text = "Enter Employee Name" ):
135
138
page , main_layout = create_page_with_header (parent , "Employee Data Update" )
136
139
@@ -152,18 +155,28 @@ def get_employee_name(parent, name_field_text="Enter Employee Name"):
152
155
main_layout .addWidget (content_frame )
153
156
154
157
def on_search_button_clicked ():
158
+ global employee_data
155
159
entered_name = name_field .text ().strip ()
160
+ print (f"Entered Name: { entered_name } " )
156
161
if not entered_name :
157
162
QtWidgets .QMessageBox .warning (parent , "Input Error" , "Please enter an employee name." )
158
163
return
159
164
160
165
try :
161
- cur = backend .cur
162
- cur .execute ("SELECT * FROM staff WHERE name = ?" , (entered_name ,))
163
- fetch = cur .fetchone ()
164
- if fetch :
165
- QtWidgets .QMessageBox .information (parent , "Employee Found" ,
166
- f"Employee data:\n ID: { fetch [0 ]} \n Name: { fetch [1 ]} \n Dept: { fetch [2 ]} \n Role: { fetch [3 ]} " )
166
+ employee_check = backend .check_name_in_staff (entered_name )
167
+ print (f"Employee Check: { type (employee_check )} ,{ employee_check } " )
168
+ if employee_check :
169
+ cur = backend .cur
170
+ cur .execute ("SELECT * FROM staff WHERE name = ?" , (entered_name ,))
171
+ employee_data = cur .fetchone ()
172
+ print (f"Employee Data: { employee_data } " )
173
+ parent .setCurrentIndex (6 )
174
+
175
+ # if employee_data:
176
+ # QtWidgets.QMessageBox.information(parent, "Employee Found",
177
+ # f"Employee data:\nID: {fetch[0]}\nName: {fetch[1]}\nDept: {fetch[2]}\nRole: {fetch[3]}")
178
+
179
+
167
180
else :
168
181
QtWidgets .QMessageBox .information (parent , "Not Found" , "Employee not found." )
169
182
except Exception as e :
@@ -175,6 +188,7 @@ def on_search_button_clicked():
175
188
176
189
177
190
#backend.check_name_in_staff()
191
+
178
192
def create_login_page (parent ,title , name_field_text = "Name :" , password_field_text = "Password :" , submit_text = "Submit" ,):
179
193
"""Create a login page with a title, name and password fields, and a submit button."""
180
194
page , main_layout = create_page_with_header (parent , "Admin Menu" )
@@ -210,6 +224,7 @@ def create_login_page(parent ,title, name_field_text="Name :", password_field_te
210
224
211
225
212
226
return page , name_edit , password_edit , submit_button
227
+
213
228
def on_login_button_clicked (parent , name_field , password_field ):
214
229
name = name_field .text ().strip ()
215
230
password = password_field .text ().strip ()
@@ -277,6 +292,7 @@ def create_home_page(parent, on_admin_clicked, on_employee_clicked, on_exit_clic
277
292
exit_button .clicked .connect (on_exit_clicked )
278
293
279
294
return page
295
+
280
296
def create_page_with_header (parent , title_text ):
281
297
"""Create a page with a styled header and return the page + main layout."""
282
298
page = QtWidgets .QWidget (parent )
@@ -291,6 +307,7 @@ def create_page_with_header(parent, title_text):
291
307
292
308
main_layout .addWidget (header_frame , 0 , QtCore .Qt .AlignTop )
293
309
return page , main_layout
310
+
294
311
def create_admin_menu_page (parent ):
295
312
page , main_layout = create_page_with_header (parent , "Admin Menu" )
296
313
@@ -315,7 +332,6 @@ def create_admin_menu_page(parent):
315
332
main_layout .addWidget (button_frame )
316
333
317
334
return page , * buttons # Unpack as add_button, update_employee, etc.
318
-
319
335
320
336
def create_add_employee_page (parent , title , submit_text = "Submit" ,update_btn :bool = False ):
321
337
page , main_layout = create_page_with_header (parent , title )
@@ -330,31 +346,42 @@ def create_add_employee_page(parent, title, submit_text="Submit",update_btn:bool
330
346
331
347
# Define input fields
332
348
fields = ["Name :" , "Password :" , "Salary :" , "Position :" ]
349
+ name_edit = None
350
+ password_edit = None
351
+ salary_edit = None
352
+ position_edit = None
333
353
edits = []
334
354
335
- for field in fields :
355
+ for i , field in enumerate ( fields ) :
336
356
field_frame , field_edit = create_input_field (form_frame , field )
337
357
form_layout .addWidget (field_frame )
358
+ if i == 0 :
359
+ name_edit = field_edit
360
+ elif i == 1 :
361
+ password_edit = field_edit
362
+ elif i == 2 :
363
+ salary_edit = field_edit
364
+ elif i == 3 :
365
+ position_edit = field_edit
338
366
edits .append (field_edit )
339
-
340
367
# Submit button
341
368
button_frame = create_styled_frame (form_frame , style = "padding: 7px;" )
342
369
button_layout = QtWidgets .QVBoxLayout (button_frame )
343
370
if update_btn :
344
- update_button = create_styled_button (button_frame , "Update" , min_size = (150 , 0 ))
371
+ update_button = create_styled_button (button_frame , "Update" , min_size = (100 , 50 ))
345
372
button_layout .addWidget (update_button , 0 , QtCore .Qt .AlignHCenter )
346
373
else :
347
- submit_button = create_styled_button (button_frame , submit_text , min_size = (150 , 0 ))
374
+ submit_button = create_styled_button (button_frame , submit_text , min_size = (100 , 50 ))
348
375
button_layout .addWidget (submit_button , 0 , QtCore .Qt .AlignHCenter )
349
376
350
377
351
378
form_layout .addWidget (button_frame )
352
379
content_layout .addWidget (form_frame , 0 , QtCore .Qt .AlignHCenter | QtCore .Qt .AlignVCenter )
353
380
main_layout .addWidget (content_frame )
354
381
if update_btn :
355
- return page , * edits , update_button
382
+ return page , name_edit , password_edit , salary_edit , position_edit , update_button
356
383
else :
357
- return page , * edits , submit_button # Unpack as name_edit, password_edit, etc.
384
+ return page , name_edit , password_edit , salary_edit , position_edit , submit_button # Unpack as name_edit, password_edit, etc.
358
385
359
386
def setup_main_window (main_window ):
360
387
"""Set up the main window with a stacked widget containing home, admin, and employee pages."""
@@ -474,10 +501,65 @@ def fetch_employee_data(name):
474
501
update_employee_page1 = get_employee_name (stacked_widget )
475
502
# apply the update_employee_data function to the submit button
476
503
504
+ update_employee_page2 ,update_employee_name , update_employee_password , update_employee_salary , update_employee_position ,update_employee_update = create_add_employee_page (stacked_widget ,"Update Employee Details" ,update_btn = True )
505
+ def populate_employee_data ():
506
+ global employee_data
507
+ if employee_data :
508
+ print ("employee_data is not None" )
509
+ update_employee_name .setText (str (employee_data [0 ])) # Name
510
+ update_employee_password .setText (str (employee_data [1 ])) # Password
511
+ update_employee_salary .setText (str (employee_data [2 ])) # Salary
512
+ update_employee_position .setText (str (employee_data [3 ])) # Position
513
+ else :
514
+ # Clear fields if no employee data is available
515
+ print ("employee_data is None" )
516
+ update_employee_name .clear ()
517
+ update_employee_password .clear ()
518
+ update_employee_salary .clear ()
519
+ update_employee_position .clear ()
520
+ QtWidgets .QMessageBox .warning (stacked_widget , "No Data" , "No employee data available to display." )
521
+ def on_page_changed (index ):
522
+ if index == 6 : # update_employee_page2 is at index 6
523
+ populate_employee_data ()
524
+
525
+ # Connect the currentChanged signal to the on_page_changed function
526
+ stacked_widget .currentChanged .connect (on_page_changed )
527
+ def update_employee_data (name , password , salary , position , name_to_update ):
528
+ try :
529
+ if not name_to_update :
530
+ show_popup_message (stacked_widget , "Original employee name is missing." , 5 )
531
+ return
532
+ if not (name or password or salary or position ):
533
+ show_popup_message (stacked_widget , "Please fill at least one field to update." , 5 )
534
+ return
535
+ if name :
536
+ backend .update_employee_name (name , name_to_update )
537
+ if password :
538
+ backend .update_employee_password (password , name_to_update )
539
+ if salary :
540
+ try :
541
+ salary = int (salary )
542
+ backend .update_employee_salary (salary , name_to_update )
543
+ except ValueError :
544
+ show_popup_message (stacked_widget , "Salary must be a valid number." , 5 )
545
+ return
546
+ if position :
547
+ backend .update_employee_position (position , name_to_update )
548
+ show_popup_message (stacked_widget , "Employee updated successfully." , 3 , False )
549
+ except Exception as e :
550
+ show_popup_message (stacked_widget , f"Error updating employee: { str (e )} " , 5 )
551
+ update_employee_update .clicked .connect (
552
+ lambda : update_employee_data (
553
+ update_employee_name .text ().strip (),
554
+ update_employee_password .text ().strip (),
555
+ update_employee_salary .text ().strip (),
556
+ update_employee_position .text ().strip (),
557
+ employee_data [0 ] if employee_data else ""
558
+ )
559
+ )
477
560
561
+
478
562
479
-
480
- # ///////////////////////////
481
563
emp_submit .clicked .connect (
482
564
lambda : add_employee_form_submit (
483
565
emp_name .text (),
@@ -501,6 +583,7 @@ def fetch_employee_data(name):
501
583
stacked_widget .addWidget (admin_menu_page )#3
502
584
stacked_widget .addWidget (add_employee_page )#4
503
585
stacked_widget .addWidget (update_employee_page1 )#5
586
+ stacked_widget .addWidget (update_employee_page2 )#6
504
587
505
588
main_layout .addWidget (stacked_widget )
506
589
main_window .setCentralWidget (central_widget )
0 commit comments