-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPayroll_Management.py
789 lines (650 loc) · 24 KB
/
Payroll_Management.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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
"""
Program for Payroll Management System.
"""
from pickle import dump, load
from os import remove, rename
class employee:
def __init__(self, nempcode=0, nname="", nsector="",npost="", nsalary=0):
"""
Constructor function
Parameters
----------
nempcode : Numeric, optional
Employee Code. The default is 0.
nname : String, optional
Employee Name. The default is "".
nsector : String, optional
Sector in which employee works. The default is "".
npost : String, optional
Post of Employee. The default is "".
nsalary : Numeric, optional
Employee's salary. The default is 0.
Returns
-------
None.
"""
self.empcode=nempcode
self.name=nname
self.sector=nsector
self.post=npost
self.salary=nsalary
def new_record(self):
"""
To add new Employee record.
Returns
-------
None.
"""
self.empcode=int(input("Enter the Employee Number: "))
self.name=input("Enter the name of the Employee: ")
self.sector=input("Enter the sector Employee comes under: ")
self.post=input("Enter the Designation/Grade of the Employee: ")
self.salary=int(input("Enter the Basic Salary of the Employee: "))
def display_records(self):
"""
To display records.
Returns
-------
None.
"""
global s_no
print("\t",
"| {:>4d}. | {:>8d} | {:<25s} | {:<20s} | {:<15s} | {:>6d} | {:>8d} |".
format(s_no, self.empcode, self.name, self.sector, self.post,
self.salary, int(self.salary*1.28+50)))
print("\t","-"*109)
s_no+=1
def salary_slip(self, empcode):
"""
To view salary slip of any employee.
Parameters
----------
empcode : Numeric
Empoyee code whose salary slip is required.
Returns
-------
None.
"""
if self.empcode==empcode:
date=input("Enter month & year(mm/yy): ")
global count
count+=1
print("\n")
print('\t',"{:^57s}".format("Company Name"))
print('\t',"{:^57s}".format("Pay Slip : "+ months[int(date[:2])]+
"-20"+str(date[3:])))
print()
print('\t',"Employee Name/Code:",self.name+"/"+ str(self.empcode))
print('\t',"Designation/Grade:",self.post)
print('\t',"-"*57)
print('\t',"| {:^25s} | {:^25s} |".format("Earnings",
"Deductions"))
print('\t',"-"*57)
print('\t',"| Basic:{:>19d} | PF:{:>22d} |".format(self.salary,
int(self.salary*0.22)))
print('\t',"| DA:{:>22d} | ESI:{:>21d} |".format(
int(self.salary*0.10), 0))
print('\t',"| HRA:{:>21d} | Others:{:>18d} |".format(
int(self.salary*0.40), 50))
print('\t',"-"*57)
print('\t',"| {:^25s} | {:^25s} |".format("Total Earnings: "+
str(self.salary*1.5),
"Total Deductions: "+
str(self.salary*0.22+50)))
print('\t',"-"*57)
print('\t',"| {:<53s} |".format("Net Payable Amount: Rs "+
str(self.salary*1.28+50)))
print('\t',"-"*57)
def update_record(self):
"""
To update any record.
Returns
-------
None.
"""
attribute=input("Enter key(A/B/C/D): ").upper()
value=input("Enter new value: ")
if attribute=="A": #A: to update name
self.name=value
elif attribute=="B": #B: to update sector
self.sector=value
elif attribute=="C": #C: to update post
self.post=value
elif attribute=="D": #D: to update salary
self.salary=int(value)
class overtime:
def __init__(self, ndate="", nemps=[]):
"""
Constructor function
Parameters
----------
ndate : String, optional
Date of Overtime. The default is "".
nemps : List, optional
List of employee who worked overtime. The default is [].
Returns
-------
None.
"""
self.date=ndate
self.emps=nemps
def add_record(self):
"""
To add new overtime record.
Returns
-------
None.
"""
self.emps=[]
self.date=input("Enter date of overtime(dd/mm/yy): ")
emps=input("Enter Employee codes(code1,code2,...): ")
lst=emps.split(",")
for emp in lst:
file1=open('Files/payroll.dat',"rb")
count=0
try: #for referential integrity
while True:
rec=load(file1)
if rec.empcode==int(emp):
count+=1
except EOFError:
file1.close()
if count==1:
if emp!='':
self.emps.append(emp.strip(" "))
else:
print("No record with Employee code "+ emp+
" stored in main datafile !")
self.emps.sort()
print("Record stored successfully.")
def modify_record(self,date):
"""
To modify overtime record.
Parameters
----------
date : String
Date of which record is to be updated in (dd/mm/yy) format.
Returns
-------
None.
"""
if self.date==date:
global count
count+=1
empid=input("Enter Employee Code to ADD/REMOVE it: ")
for emp in self.emps:
if emp==empid:
self.emps.remove(empid)
print("Entry of "+empid+" removed successfully.")
break
else: #for referential integrity
file1=open('Files/payroll.dat',"rb")
try:
while True:
rec=load(file1)
if rec.empcode==int(empid):
count+=1
except EOFError:
file1.close()
if count==3:
self.emps.append(empid)
self.emps.sort()
print("Entry of "+empid+" added successfully.")
else:
print("No record with Employee code "+empid+
" stored in main datafile !")
def view_record(self,date):
"""
To view stored data.
Parameters
----------
date : String
Date of which records of overtime are required.
Returns
-------
None.
"""
if self.date==date:
global count,s_no
count+=1
print("\n\n")
print("\t {:^53s}".format("EMPLOYEES WHO WORKED OVERTIME ON "+
months[int(date[3:5])].upper()+
"-20"+date[6:]))
print("\t", "-"*53)
print("\t", '| {:^5s} | {:^13s} | {:^25s} |'.
format("S.No.", "Employee Code", "Employee Name"))
print("\t", "-"*53)
file1=open('Files/payroll.dat',"rb")
try:
while True:
rec=load(file1)
for emp in self.emps:
if int(emp)==rec.empcode:
print("\t", '| {:>4d}. | {:>13d} | {:<25s} |'.
format(s_no, rec.empcode, rec.name))
print("\t", "-"*53)
s_no+=1
except EOFError:
file1.close()
if self.emps==[]:
print("\t", "| {:^49s} |".format("No records stored"))
print("\t", "-"*53)
class attendance:
def __init__(self, nmonth='', nrecord=dict()):
"""
Constructor function.
Parameters
----------
nmonth : String, optional
Month of attendance in (mm/yy) format. The default is ''.
nrecord : Dictionary, optional
Key:Value pairs of Employee code and respective monthly attendance.
The default is dict().
Returns
-------
None.
"""
self.month=nmonth
self.record=nrecord
def input_records(self):
"""
To add new monthly attendance record.
Returns
-------
None.
"""
self.record={}
self.month=input("Enter month(mm/yy): ")
ans="y"
while ans!="n":
empcode=int(input("Enter Employee code: "))
file1=open('Files/payroll.dat',"rb")
count=0
try: #for referential integrity
while True:
rec=load(file1)
if rec.empcode==empcode:
count+=1
except EOFError:
file1.close()
if count==1:
attendance=int(input("Enter Empoyee's Attendance: "))
self.record[empcode]=attendance
else:
print("No record with Employee code "+ str(empcode)+
" stored in main datafile !")
ans=input("Add more records([y]/n): ")
def modify_record(self, month):
"""
To modify monthly attendance.
Parameters
----------
month : String
Month of attendance in (mm/yy) format.
Returns
-------
None.
"""
if month==self.month:
global count
count+=1
empid=input("Enter Employee Code to ADD/REMOVE it: ")
if int(empid) in self.record:
del self.record[int(empid)]
print("Entry of "+empid+" removed successfully.")
else:
file1=open('Files/payroll.dat',"rb")
try: #for referential integrity
while True:
rec=load(file1)
if rec.empcode==empid:
count+=1
except EOFError:
file1.close()
if count==2:
attendance=int(input("Enter Employee's Attendance: "))
self.record[int(empid)]=attendance
print("Attendance of "+empid+" added successfully.")
else:
print("No record with Employee code "+ str(empid)+
" stored in main datafile !")
def view_attendance(self, nmonth):
"""
To view monthly attendance of any month.
Parameters
----------
nmonth : String
Month whose records are required in (mm/yy) format.
Returns
-------
None.
"""
if self.month==nmonth:
global count,s_no
count+=1
file1=open('Files/payroll.dat', "rb")
print("\n")
print("\t","{:^91s}".format("MONTHLY ATTENDANCE OF "+
months[int(nmonth[:2])].upper()+"-20"+
nmonth[3:]))
print("\t","-"*91)
print("\t","| {:^5s} | {:^8s} | {:^25s} | {:^19s} | {:^18s} |".
format("S.No.","EMP CODE","EMPLOYEE NAME",
"ACTUAL WORKING DAYS","TOTAL WORKING DAYS"))
print("\t","-"*91)
try:
while True:
rec=load(file1)
for emp in self.record:
if emp==rec.empcode:
print("\t",
"| {:>4d}. | {:>8d} | {:<25s} | {:>19d} | {:>18d} |".
format(s_no, rec.empcode, rec.name, self.record[emp],
26))
print("\t","-"*91)
s_no+=1
except EOFError:
file1.close()
if self.record=={}:
print("\t","| {:^87s} |".format("No records stored"))
print("\t","-"*91)
def create_datafile():
"""
To create/reset Datafiles of Payroll Management.
Returns
-------
None.
"""
open("Files/payroll.dat","wb").close()
open("Files/overtime.dat","wb").close()
open("Files/Files/attendance.dat","wb").close()
#----------------------------------main---------------------------------------
menu='''
----------------------------------
| {:^30s} |
----------------------------------
| {:<30s} |
| {:<30s} |
| {:<30s} |
| {:<30s} |
| {:<30s} |
| {:<30s} |
----------------------------------
| {:<30s} |
| {:<30s} |
| {:<30s} |
----------------------------------
| {:<30s} |
| {:<30s} |
| {:<30s} |
----------------------------------
'''.format("MENU",
" 1. Create/Reset Datafiles.", " 2. Add New Record.",
" 3. View Records stored.", " 4. Modify Records.",
" 5. Delete Employee Records.", " 6. View Salary Slip.",
" 7. Add Overtime Record.", " 8. Modify Overtime Record.",
" 9. View Overtime Records.", "10. Add Monthly Attendance.",
"11. Modify Monthly Attendance.", "12. View Monthy Attendance.")
attributes="""
A: To update name
B: To update sector
C: To update post
D: To update salary
"""
rec=employee()
orec=overtime()
arec=attendance()
months={1:"Jan",2:"Feb",3:"March",4:"April",5:"May",6:"June",7:"July",
8:"Aug",9:"Sep",10:"Oct",11:"Nov",12:"Dec"}
act='y'
while act!='n':
print(menu)
ans=input("Enter serial number of the task you want to perform: ")
if ans=="1":
create_datafile()
print("Datafile created/reset successfully.")
elif ans=="2":
with open("Files/payroll.dat","ab") as file1:
rec.new_record()
dump(rec, file1)
print("New record added successfully.")
file1.flush()
file1.close()
elif ans=="3":
print("\n\n\t{:^109s}".format("EMPLOYEES DETAILS"))
print("\t","-"*109)
print("\t",
"| {:^5s} | {:^8s} | {:^25s} | {:^20s} | {:^15s} | {:^6s} | {:^8} |".
format("S.No.","EMP CODE", "EMPLOYEE NAME", "SECTOR", "POST", "BASIC",
"NET PAY"))
print("\t","-"*109)
s_no=1
with open('Files/payroll.dat',"rb") as file1:
try:
while True:
rec=load(file1)
rec.display_records()
except EOFError:
file1.close()
elif ans=="4":
empid=int(input("Enter Employee Code: "))
with open("Files/payroll.dat","rb") as file1:
with open("new.dat","wb") as nfile:
try:
while True:
rec=load(file1)
if rec.empcode==empid:
print(attributes)
rec.update_record()
dump(rec, nfile)
nfile.flush()
print("Record updated successfully.")
else:
dump(rec, nfile)
nfile.flush()
except EOFError:
nfile.close()
file1.close()
remove("Files/payroll.dat")
rename("new.dat", "Files/payroll.dat")
elif ans=="5":
empid=input("Enter Employee code: ")
# to delete record from 'Files/payroll.dat'
with open('Files/payroll.dat',"rb") as file1:
with open('new.dat',"wb") as nfile:
try:
while True:
rec=load(file1)
if int(empid)!=rec.empcode:
dump(rec, nfile)
nfile.flush()
except EOFError:
file1.close()
nfile.close()
remove("Files/payroll.dat")
rename('new.dat', "Files/payroll.dat")
# to delete record from 'Files/overtime.dat'
with open('Files/overtime.dat',"rb") as file2:
with open('new.dat',"wb") as nfile:
try:
while True:
orec=load(file2)
if empid in orec.emps:
orec.emps.remove(empid)
dump(orec, nfile)
nfile.flush()
except EOFError:
file2.close()
nfile.close()
remove('Files/overtime.dat')
rename('new.dat', 'Files/overtime.dat')
# to delete record from 'Files/Files/attendance.dat'
with open('Files/Files/attendance.dat',"rb") as file3:
with open('new.dat',"wb") as nfile:
try:
while True:
arec=load(file3)
if empid in arec.record:
del arec.record[empid]
dump(arec, nfile)
except EOFError:
file3.close()
nfile.close()
remove('Files/Files/attendance.dat')
rename('new.dat', 'Files/Files/attendance.dat')
print("Record with Employee Code "+empid+" is deleted successfully.")
elif ans=="6":
empcode=int(input("Enter Employee's Code: "))
file1=open("Files/payroll.dat","rb")
count=0
try:
while True:
rec=load(file1)
rec.salary_slip(empcode)
except EOFError:
file1.close()
if count==0:
print("No record with this code is stored.")
elif ans=="7":
with open("Files/overtime.dat","ab") as file2:
orec.add_record()
dump(orec, file2)
file2.close()
elif ans=="8":
# to display stored dates
print("-"*16)
print("| Dates Stored |")
print("-"*16)
with open('Files/overtime.dat',"rb") as file2:
try:
while True:
orec=load(file2)
print("| {:>12s} |".format(orec.date))
print("-"*16)
except EOFError:
file2.close()
# to view and modify records
ndate=input("Enter date(dd/mm/yy): ")
count=0
s_no=1
with open('Files/overtime.dat',"rb") as file2:
with open('new.dat',"wb") as nfile:
try:
while True:
orec=load(file2)
orec.view_record(ndate)
orec.modify_record(ndate)
dump(orec, nfile)
except EOFError:
file2.close()
nfile.close()
if count==0:
print("No record available for date "+ndate+" to modify.")
remove('Files/overtime.dat')
rename('new.dat', 'Files/overtime.dat')
elif ans=="9":
# to display stored dates
print("-"*16)
print("| Dates Stored |")
print("-"*16)
with open('Files/overtime.dat',"rb") as file2:
try:
while True:
orec=load(file2)
print("| {:>12s} |".format(orec.date))
print("-"*16)
except EOFError:
file2.close()
# to view records
ndate=input("Enter date(dd/mm/yy): ")
count=0
s_no=1
with open('Files/overtime.dat',"rb") as file2:
try:
while True:
orec=load(file2)
orec.view_record(ndate)
except EOFError:
file2.close()
if count==0:
print("No record available for date "+ndate+".")
elif ans=="10":
with open('Files/Files/attendance.dat', "ab") as file3:
arec.input_records()
dump(arec, file3)
file3.flush()
file3.close()
elif ans=="11":
# to display stored months
print("-"*17)
print("| MONTHS STORED |")
print("-"*17)
for nmonth in months:
with open('Files/Files/attendance.dat', "rb") as file3:
try:
while True:
arec=load(file3)
if nmonth==int(arec.month[:2]):
print("| {:>13s} |".format(
months[int(arec.month[:2])]+
"-20"+ arec.month[3:]))
print("-"*17)
except:
file3.close()
# to view and modify records
month=input("Enter Month(mm/yy): ")
count=0
s_no=1
with open('Files/Files/attendance.dat',"rb") as file3:
with open('new.dat', "wb") as nfile:
try:
while True:
arec=load(file3)
arec.view_attendance(month)
arec.modify_record(month)
dump(arec, nfile)
except EOFError:
file3.close()
nfile.close()
if count==0:
print("No record of month "+months[int(month[:2])]+
"-20"+month[3:]+".")
remove("Files/Files/attendance.dat")
rename('new.dat', 'Files/Files/attendance.dat')
elif ans=="12":
# to display stored months
print("-"*17)
print("| MONTHS STORED |")
print("-"*17)
for nmonth in months:
with open('Files/Files/attendance.dat', "rb") as file3:
try:
while True:
arec=load(file3)
if nmonth==int(arec.month[:2]):
print("| {:>13s} |".format(
months[int(arec.month[:2])]+
"-20"+ arec.month[3:]))
print("-"*17)
except:
file3.close()
# to view records
s_no=1
month=input("Enter Month(mm/yy): ")
count=0
with open('Files/Files/attendance.dat',"rb") as file3:
try:
while True:
arec=load(file3)
arec.view_attendance(month)
except EOFError:
file3.close()
if count!=1:
print("No record of month "+months[int(month[:2])]+"-20"+
month[3:]+".")
else:
print("Invalid input !!!")
act=input("Do you want to perform more tasks([y]/n): ")
else:
print("Thanks for using this program.")