-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathleave_functions.py
1115 lines (873 loc) · 37.6 KB
/
leave_functions.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
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# TODO:
# 1. fix earned leave calculation -- done
# 2. prettify the output
# 3. design a useable GUI
# 4. make the reports exportable to text file
# 5. improve the leave record exported text file -- preferably to PDF file if possible
# 6. optimise the code - avoid DRY (dont repeat yourself)
# 7. write a leave letter status module
# 8. write help strings for each function for clarify and easier maintenance
# 9. Validate for LOP and strikes -- done
# 10. validate for special leave type -- done
# 11. Move old year data to a different file to prevent clog up -- all data belonging to previous year can be moved to a different file etc
# 12. move backup data stuff to a different folder to prevent clog up of the whole directory
import json
import leave_program
from json.decoder import JSONDecodeError
from datetime import timedelta
import datetime
import pandas as pd
from fractions import Fraction
from contextlib import redirect_stdout
try:
f = open('data.json')
try:
d = json.load(f)
except JSONDecodeError:
d = {}
pass
except FileNotFoundError:
d = {}
pass
def daterange(date1, date2):
for n in range(int ((date2 - date1).days)+1):
yield date1 + timedelta(n)
def numOfDays(date1, date2):
return (date2-date1).days
def validate_employee_number():
while True:
print("Employees already present in database: ", d.keys())
employeeNumber_input = input("Enter new employee number: ")
if employeeNumber_input.isnumeric():
if employeeNumber_input not in d.keys():
print("Accepted")
return employeeNumber_input
else:
print("Employee number already exists. Enter unique employee number.")
else:
print("Please enter numbers only.")
def validate_date(date):
try:
date = datetime.datetime.strptime(date,"%d-%m-%Y")
date = date.strftime("%d-%m-%Y")
except ValueError:
date = input('Incorrect format. Kindly enter the date in dd-mm-yyyy format: ')
date = validate_date(date)
return date
def validate_letters(letter):
if letter.isascii() is False:
letter = input("Kindly enter letters only: ")
letter = validate_letters(letter)
return letter
def update_employee(emp_no, update_type, update):
d[emp_no][update_type] = update
print("%s has been updated to %s." % (update_type, update))
save_data()
def mixed_to_float(x):
"""Function credit to https://stackoverflow.com/a/46303199"""
return float(sum(Fraction(term) for term in x.split()))
def create_employee():
employee_number = validate_employee_number()
employee_name = input("Enter name of the employee: ")
employee_name = validate_letters(employee_name)
DOJ = input("Leave record last updated on: Enter date in dd-mm-yyyy format: ")
DOJ = validate_date(DOJ)
casual_leave = input("Enter current casual leave: ")
casual_leave = max(0,min(int(casual_leave), 12))
earned_leave = input("Enter current earned leave: ")
print(mixed_to_float(earned_leave))
earned_leave = mixed_to_float(earned_leave)
earned_leave = max(0,(min(float(earned_leave),270)))
sick_leave = input("Enter current sick leave: ")
sick_leave = max(0,min(int(sick_leave),240))
RH = input("Enter current RH count: ")
RH = max(0,min(int(RH),2))
d.update({employee_number:{
"name": employee_name,
"employee number" : employee_number,
"Leave updated as on" : DOJ,
"casual leave": casual_leave,
"earned leave" : earned_leave,
"sick leave" : sick_leave,
"RH": RH,
"casual leave dict": {},
"RH list": [],
"Earned leave list": [],
"sick leave dict": {},
"Special leave list":{},
"LOP":{},
"Leave encashment": {}
}
}
)
save_data()
print("Employee %s with employee number %s has been created." % (employee_name, employee_number))
display_emp(employee_number)
def update_leave(emp_no, leave_type, no_of_days):
current_balance = d[emp_no][leave_type]
newbalance = float(current_balance) - float(no_of_days)
d[emp_no][leave_type] = newbalance
save_data()
if leave_type != "earned leave":
print("%s has been updated. New balance: %s" % (leave_type, newbalance))
elif leave_type == "earned leave":
mixed_frac = dec_to_proper_frac(emp_no)
print("%s has been updated. New balance: %s" % (leave_type, mixed_frac))
def del_casual_leave(emp_no):
print(d[emp_no]['casual leave dict'])
delete_cl = input("Enter date in dd-mm-yyyy format: ")
delete_cl = validate_date(delete_cl)
delete_cl_date = datetime.datetime.strptime(delete_cl, "%d-%m-%Y")
delete_cl_date_string = delete_cl_date.strftime("%d-%m-%Y")
if delete_cl_date_string in d[emp_no]['casual leave dict'].keys():
casualleave = delete_cl_date.strftime("%d-%m-%Y")
else:
print("Entered date is not present.")
return
if d[emp_no]['casual leave dict'][casualleave] == "half":
update_leave(emp_no, "casual leave", -0.5)
else:
update_leave(emp_no, "casual leave", -1)
d[emp_no]['casual leave dict'].pop(casualleave)
save_data()
print("%s has been deleted from casual leave list." % delete_cl)
def check_leave_count(emp_no, leave_type, no_of_days):
current_leave_balance = d[emp_no][leave_type]
if (float(current_leave_balance) - float(no_of_days)) < 0:
print("Insufficient leave balance.")
return False
def add_casual_leave(emp_no):
print("Current CL count for employee", d[emp_no]['casual leave'])
dict_casual_leave_list = d[emp_no]['casual leave dict'].keys()
print(dict_casual_leave_list)
type_CL = input("Enter whether full or half CL: ")
if type_CL != "half":
type_CL = "full"
#TODO: To validate full or half CL input
# while True:
# if type_CL != "full" or "half":
# type_CL = input("Enter full or half: ")
# if type_CL == "full" or "half":
# break
# #return type_CL
# #else return
start_cl = input("Enter start date in dd-mm-yyyy format: ")
start_cl = validate_date(start_cl)
start_cl_date = datetime.datetime.strptime(start_cl, "%d-%m-%Y")
if start_cl_date.strftime("%d-%m-%Y") in dict_casual_leave_list:
print("Casual leave already entered")
return
if type_CL == "half":
end_cl = ""
else:
end_cl = input("Enter end date if there are more than one days, otherwise press enter: ")
if end_cl == "":
end_cl_date = start_cl_date
elif end_cl != "":
end_cl_ = validate_date(end_cl)
end_cl_date = datetime.datetime.strptime(end_cl, "%d-%m-%Y")
no_of_days = numOfDays(start_cl_date, end_cl_date)+1
if check_leave_count(emp_no,"casual leave",no_of_days) is False:
return
if type_CL == "full":
update_leave(emp_no, "casual leave", no_of_days)
elif type_CL == "half":
update_leave(emp_no, "casual leave", no_of_days/2)
casual_leave_list = []
local_dict = {}
if end_cl_date == start_cl_date:
start_cl_date_string = start_cl_date.strftime('%d-%m-%Y')
local_dict.update({start_cl_date_string: type_CL})
else:
for dt in daterange(start_cl_date, end_cl_date):
casual_leave_list.append(dt)
for dt in casual_leave_list:
date_string = dt.strftime('%d-%m-%Y')
local_dict.update({date_string: type_CL})
d[emp_no]['casual leave dict'].update(local_dict)
save_data()
print("Casual leave has been added.")
def delete_employee(emp_no):
backup_data()
d.pop(emp_no)
print("Employee number %s has been deleted." %emp_no)
save_data()
def del_list_leave(emp_no, leave_type, leave_list):
"""Function to delete leaves stored in list type: earned leave and Restricted holiday"""
print(d[emp_no][leave_type])
print(d[emp_no][leave_list])
delete_leave = input("Enter date in dd-mm-yyyy format: ")
delete_leave = validate_date(delete_leave)
delete_leave_date = datetime.datetime.strptime(delete_leave, "%d-%m-%Y")
delete_leave_date_string = delete_leave_date.strftime("%d-%m-%Y")
if delete_leave_date_string in d[emp_no][leave_list]:
update_leave(emp_no, leave_type, -1)
leave_to_be_removed = delete_leave_date.strftime("%d-%m-%Y")
else:
print("Entered date is not present.")
return
d[emp_no][leave_list].remove(leave_to_be_removed)
print("%s %s has been deleted." % (leave_type, delete_leave))
def add_earned_leave(emp_no):
#mixed_frac = dec_to_proper_frac(emp_no)
#print(mixed_frac)
#print(d[emp_no]['earned leave'])
print("Already entered earned leave for the employee is as follows: ",d[emp_no]['Earned leave list'])
earned_leave_list_string = d[emp_no]['Earned leave list']
earned_leave_list = []
start_el = input("Enter start date in dd-mm-yyyy format: ")
start_el = validate_date(start_el)
start_el_date = datetime.datetime.strptime(start_el, "%d-%m-%Y")
if start_el_date.strftime("%d-%m-%Y") in earned_leave_list_string:
print("Earned leave already entered.")
return
end_el = input("Enter end date in dd-mm-yyyy format: ")
end_el = validate_date(end_el)
end_el_date = datetime.datetime.strptime(end_el, "%d-%m-%Y")
no_of_days = numOfDays(start_el_date, end_el_date)+1
# calculating the current leave balance before checking for sufficient leave balance
calculate_el_emp(emp_no, start_el_date, end_el_date,True)
doj = d[emp_no]['Leave updated as on']
doj_datetime = datetime.datetime.strptime(doj, "%d-%m-%Y")
if check_leave_count(emp_no,"earned leave",no_of_days) is False:
cal_one_day_before = doj_datetime + timedelta(days=-1)
cal_one_day_before_string = cal_one_day_before.strftime("%d-%m-%Y")
d[emp_no]['Leave updated as on'] = cal_one_day_before_string
return
cal_el_date_string = end_el_date.strftime("%d-%m-%Y")
d[emp_no]['Leave updated as on'] = cal_el_date_string
update_leave(emp_no, "earned leave", no_of_days)
#print(no_of_days)
for dt in daterange(start_el_date, end_el_date):
earned_leave_list.append(dt)
for dt in earned_leave_list:
date_string = dt.strftime('%d-%m-%Y')
earned_leave_list_string.append(date_string)
d[emp_no]['Earned leave list'] = earned_leave_list_string
save_data()
#print("Earned leave has been added.")
def add_sick_leave(emp_no):
print("Current SL count for employee", d[emp_no]['sick leave'])
dict_sick_leave_list = d[emp_no]['sick leave dict'].keys()
print(dict_sick_leave_list)
type_SL = input("Enter whether full or half pay SL: ")
if type_SL != "half":
type_SL = "full"
#TODO: Validate full or half pay input in casual leave
start_sl = input("Enter start date in dd-mm-yyyy format: ")
start_sl = validate_date(start_sl)
start_sl_date = datetime.datetime.strptime(start_sl, "%d-%m-%Y")
if start_sl_date.strftime("%d-%m-%Y") in dict_sick_leave_list:
print("Sick leave already entered")
return
end_sl = input("Enter end date or press enter for one day leave: ")
if end_sl == "":
end_sl_date = start_sl_date
elif end_sl != "":
end_sl = validate_date(end_sl)
end_sl_date = datetime.datetime.strptime(end_sl, "%d-%m-%Y")
no_of_days = numOfDays(start_sl_date, end_sl_date)+1
if type_SL == "full":
no_of_days = no_of_days * 2
if check_leave_count(emp_no,"sick leave",no_of_days) is False:
return
update_leave(emp_no, "sick leave", no_of_days)
sick_leave_list = []
local_dict = {}
if end_sl_date == start_sl_date:
start_sl_date_string = start_sl_date.strftime('%d-%m-%Y')
local_dict.update({start_sl_date_string: type_SL})
else:
for dt in daterange(start_sl_date, end_sl_date):
sick_leave_list.append(dt)
for dt in sick_leave_list:
date_string = dt.strftime('%d-%m-%Y')
local_dict.update({date_string: type_SL})
d[emp_no]['sick leave dict'].update(local_dict)
save_data()
print("Sick leave has been added.")
def del_sick_leave(emp_no):
print(d[emp_no]['sick leave dict'])
delete_sl = input("Enter date in dd-mm-yyyy format: ")
delete_sl = validate_date(delete_sl)
delete_sl_date = datetime.datetime.strptime(delete_sl, "%d-%m-%Y")
delete_sl_date_string = delete_sl_date.strftime("%d-%m-%Y")
if delete_sl_date_string in d[emp_no]['sick leave dict'].keys():
sickleave = delete_sl_date.strftime("%d-%m-%Y")
else:
print("Entered date is not present.")
return
if d[emp_no]['sick leave dict'][sickleave] == "half":
update_leave(emp_no,"sick leave", -1)
else:
update_leave(emp_no, "sick leave", -2)
d[emp_no]['sick leave dict'].pop(sickleave)
save_data()
print("%s has been deleted." % delete_sl)
def leave_encashment(emp_no):
mixed_frac = dec_to_proper_frac(emp_no)
print("Current earned leave count for %s is %s" % (emp_no, mixed_frac))
block_year = input("Block year for which leave is being encashed (Enter in YYYY-YYYY format - eg. 2022-2023): ")
local_dict = {}
if block_year not in d[emp_no]['Leave encashment'].keys():
day_of_encash = input("When was the leave encashed? Enter in dd-mm-yyyy format: ")
day_of_encash = validate_date(day_of_encash)
day_of_encash_date = datetime.datetime.strptime(day_of_encash,"%d-%m-%Y")
day_of_encash_string = day_of_encash_date.strftime('%d-%m-%Y')
no_of_days = input("How many days earned leave is encashed? ")
while int(no_of_days) > 15:
#if int(no_of_days) > 15:
no_of_days = input("Maximum allowed days is 15. Please recheck and enter again: ")
#no_of_days = min(int(no_of_days),15)
day_of_encash_plus_one = day_of_encash_date + timedelta(days=1)
calculate_el_emp(emp_no, day_of_encash_plus_one, day_of_encash_plus_one,True)
doj = d[emp_no]['Leave updated as on']
doj_datetime = datetime.datetime.strptime(doj, "%d-%m-%Y")
day_of_encash_string = day_of_encash_date.strftime("%d-%m-%Y")
if doj_datetime < day_of_encash_date:
d[emp_no]['Leave updated as on'] = day_of_encash_string
if check_leave_count(emp_no,"earned leave",int(no_of_days)) is False:
return
local_dict.update({block_year: no_of_days})
update_leave(emp_no, "earned leave",no_of_days)
d[emp_no]['Leave encashment'].update(local_dict)
print("Block year %s has been entered." % (block_year))
local_dict_special = {day_of_encash_string: "Leave encashment"}
d[emp_no]['Special leave list'].update(local_dict_special)
else:
print("Block year %s has already been entered." % block_year)
save_data()
def del_leave_encashment(emp_no):
print(d[emp_no]['Leave encashment'])
block_year = input("Enter block year in YYYY-YYYY (eg. 2022-2023) format: ")
if block_year in d[emp_no]['Leave encashment'].keys():
no_of_days = d[emp_no]['Leave encashment'][block_year]
int_no_days = int(no_of_days) * -1
d[emp_no]['Leave encashment'].pop(block_year)
update_leave(emp_no, "earned leave", int_no_days)
print("Block year %s has been deleted." % block_year)
else:
print("Entered block year is not present.")
save_data()
def add_rh_leave(emp_no):
print(d[emp_no]['RH'])
rh_list = d[emp_no]["RH list"]
start_rh = input("Enter start date in dd-mm-yyyy format: ")
start_rh = validate_date(start_rh)
start_date_rh = datetime.datetime.strptime(start_rh, "%d-%m-%Y")
if start_date_rh.strftime("%d-%m-%Y") in rh_list:
print("Restricted holiday already entered.")
else:
date_string = start_date_rh.strftime('%d-%m-%Y')
if check_leave_count(emp_no,"RH",1) is False:
return
rh_list.append(date_string)
update_leave(emp_no,"RH", 1)
save_data()
print("Restricted holiday has been added.")
def add_LOP_leave(emp_no):
dict_sick_leave_list = d[emp_no]['LOP'].keys()
print(dict_sick_leave_list)
input_type_SL = input("Enter 1 for LOP and 2 for Strike: ")
if input_type_SL == "1":
type_SL = "LOP"
elif input_type_SL == "2":
type_SL = "Strike"
else:
print("Received %s as input instead of 1 or 2. Assuming the entry to be LOP." %(input_type_SL))
type_SL = "LOP"
start_sl = input("Enter start date in dd-mm-yyyy format: ")
start_sl = validate_date(start_sl)
start_sl_date = datetime.datetime.strptime(start_sl, "%d-%m-%Y")
if start_sl_date.strftime("%d-%m-%Y") in dict_sick_leave_list:
print("This date has already been entered.")
return
end_sl = input("Enter end date if there are more than one days, otherwise press enter: ")
if end_sl == "":
end_sl_date = start_sl_date
elif end_sl != "":
end_sl = validate_date(end_sl)
end_sl_date = datetime.datetime.strptime(end_sl, "%d-%m-%Y")
sick_leave_list = []
local_dict = {}
if end_sl_date == start_sl_date:
start_sl_date_string = start_sl_date.strftime('%d-%m-%Y')
local_dict.update({start_sl_date_string: type_SL})
else:
for dt in daterange(start_sl_date, end_sl_date):
sick_leave_list.append(dt)
for dt in sick_leave_list:
date_string = dt.strftime('%d-%m-%Y')
local_dict.update({date_string: type_SL})
d[emp_no]['LOP'].update(local_dict)
print("%s has been added." % type_SL)
def del_leave(emp_no, leave_type):
print(d[emp_no][leave_type])
delete_sl = input("Enter date in dd-mm-yyyy format: ")
delete_sl = validate_date(delete_sl)
delete_sl_date = datetime.datetime.strptime(delete_sl, "%d-%m-%Y")
delete_sl_date_string = delete_sl_date.strftime("%d-%m-%Y")
if delete_sl_date_string in d[emp_no][leave_type].keys():
sickleave = delete_sl_date.strftime("%d-%m-%Y")
else:
print("Entered date is not present")
return
d[emp_no][leave_type].pop(sickleave)
save_data()
print("%s has been deleted." % delete_sl)
def add_special_leave(emp_no):
print("Current SL count for employee", d[emp_no]['Special leave list'])
dict_sick_leave_list = d[emp_no]['Special leave list'].keys()
print(dict_sick_leave_list)
input_type_SL = input("""Enter 1 for Maternity leave.
Enter 2 for Paternity leave.
Enter 3 for Quarantine leave.
Enter 4 for Examination leave.
Enter 5 for Others: """)
if input_type_SL == "1":
type_SL = "Maternity leave"
elif input_type_SL == "2":
type_SL = "Paternity leave"
elif input_type_SL == "3":
type_SL = "Quarantine leave"
elif input_type_SL == "4":
type_SL = "Examination leave"
elif input_type_SL == "5":
type_SL = "Others"
else:
print("Received %s as input instead of 1-5. Assuming the entry to be Others." %(input_type_SL))
type_SL = "Others"
start_sl = input("Enter start date in dd-mm-yyyy format: ")
start_sl = validate_date(start_sl)
start_sl_date = datetime.datetime.strptime(start_sl, "%d-%m-%Y")
if start_sl_date.strftime("%d-%m-%Y") in dict_sick_leave_list:
print("Special leave already entered")
return
end_sl = input("Enter end date if there are more than one days, otherwise press enter: ")
if end_sl == "":
end_sl_date = start_sl_date
elif end_sl != "":
end_sl = validate_date(end_sl)
end_sl_date = datetime.datetime.strptime(end_sl, "%d-%m-%Y")
sick_leave_list = []
local_dict = {}
if end_sl_date == start_sl_date:
start_sl_date_string = start_sl_date.strftime('%d-%m-%Y')
local_dict.update({start_sl_date_string: type_SL})
else:
for dt in daterange(start_sl_date, end_sl_date):
sick_leave_list.append(dt)
for dt in sick_leave_list:
date_string = dt.strftime('%d-%m-%Y')
local_dict.update({date_string: type_SL})
d[emp_no]['Special leave list'].update(local_dict)
print("Special leave has been added.")
def req_emp_no():
print("Employees already present", d.keys())
if len(d) != 0:
while True:
user_input = input("Enter employee number: ")
if user_input not in d.keys():
print("Employee number not in record. Enter again.")
else:
return user_input
def new_year_reset():
# export data of all employees to text file
# reset RH list
# reset casual leave dictionary
employeelist = d.keys()
for i in employeelist:
export_to_text(i)
d[i]['casual leave'] = 12
d[i]['casual leave dict'] = {}
d[i]['RH'] = 2
d[i]['RH list'] = []
if (int(d[i]['sick leave']) + 30) > 240:
d[i]['sick leave'] = 240
else:
update_leave(i, "sick leave",-30)
save_data()
def calculate_el_emp(emp_no, el_start_date, el_end_date,notfromthere):
# we are going to update earned leave balance of an employee just before we are going to add new earned leave
# this is to make sure the employee doesn't go over their 270 or something like that
# sick leave will not contribute to earned leave accruing
# same for earned leave also
# we will set the last update of earned leave to the final leave of earned leave hopefully
cal_el_date_string = el_start_date.strftime("%d-%m-%Y")
doj = d[emp_no]['Leave updated as on']
doj_datetime = datetime.datetime.strptime(doj, "%d-%m-%Y")
# an employee will accrue earned leave when he/she is "on duty"
# all leaves other than casual leave, quarantine leave, examination leave and trade union leave
# to be excluded when calculating earned leave
# major leaves to be discounted for calculation of earned leaves are the following:
# 1. sick leave
# 2. earned leave
# 3. strike
# 4. LOP
# 5. maternity leave
# 6. paternity leave
# counting number of sick leaves
new_sick_leave_list = []
for j in d[emp_no]['sick leave dict']:
date_j = datetime.datetime.strptime(j, "%d-%m-%Y")
if doj_datetime < date_j and date_j < el_start_date:
new_sick_leave_list.append(j)
new_sl_count = len(new_sick_leave_list)
# counting number of Earned leaves
new_el_list = []
for j in d[emp_no]['Earned leave list']:
date_j = datetime.datetime.strptime(j,"%d-%m-%Y")
if doj_datetime < date_j and date_j < el_start_date:
new_el_list.append(j)
new_el_count_2 = len(new_el_list)
# counting number of lop
# LOP dict has both LOP entries and strike entries
new_lop_list = []
for j in d[emp_no]['LOP']:
date_j = datetime.datetime.strptime(j, "%d-%m-%Y")
if doj_datetime < date_j and date_j < el_start_date:
new_lop_list.append(j)
new_lop_count = len(new_lop_list)
# counting number of paternity leave and maternity leave
new_special_leave_list = []
for j in d[emp_no]['Special leave list'].keys():
if d[emp_no]["Special leave list"][j] == 'Maternity leave':
date_j = datetime.datetime.strptime(j, "%d-%m-%Y")
if doj_datetime < date_j and date_j < el_start_date:
new_special_leave_list.append(j)
elif d[emp_no]['Special leave list'][j] == "Paternity leave":
date_j = datetime.datetime.strptime(j, "%d-%m-%Y")
if doj_datetime < date_j and date_j < el_start_date:
new_special_leave_list.append(j)
new_special_leave_count = len(new_special_leave_list)
if notfromthere:
no_of_days = numOfDays(doj_datetime,el_start_date)-1
else:
no_of_days = numOfDays(doj_datetime,el_start_date)
current_leave_count = d[emp_no]['earned leave']
earnedleavedays = no_of_days - int(new_sl_count) - new_el_count_2 - int(new_lop_count) - int(new_special_leave_count)
accruedleaves = (earnedleavedays) / 11
new_el_count = float(accruedleaves) + float(current_leave_count)
new_el_count = min(new_el_count,270)
if doj_datetime < el_start_date:
d[emp_no]['earned leave'] = new_el_count
d[emp_no]['Leave updated as on'] = cal_el_date_string
frac_earned_leave = dec_to_proper_frac(emp_no)
print("Earned leave has been updated for employee number %s up to %s. Updated earned leave balance: %s."
%(emp_no, cal_el_date_string, frac_earned_leave))
else:
print("Earned leave has already been updated until %s" % doj_datetime)
def calculate_el():
print("WORD OF CAUTION. MAKE SURE ALL SICK LEAVES AND EARNED LEAVES ARE ENTERED BEFORE RUNNING FURTHER")
cal_el_input = input("Upto which date do you want the leaves to be calculated? Enter in dd-mm-yyyy format: ")
cal_el_input = validate_date(cal_el_input)
cal_el_date = datetime.datetime.strptime(cal_el_input, "%d-%m-%Y")
cal_el_date_string = cal_el_date.strftime("%d-%m-%Y")
employeelist = d.keys()
for i in employeelist:
doj = d[i]['Leave updated as on']
doj_datetime = datetime.datetime.strptime(doj, "%d-%m-%Y")
# if the updated date is newer than entered date, break the calculation rightaway
if doj_datetime > cal_el_date:
print("Leave of %s already updated upto: %s" % (d[i]['name'], doj))
new_sick_leave_list = []
for j in d[i]['sick leave dict']:
date_j = datetime.datetime.strptime(j, "%d-%m-%Y")
if doj_datetime < date_j and date_j < cal_el_date:
new_sick_leave_list.append(j)
new_sl_count = len(new_sick_leave_list)
new_el_list = []
for j in d[i]['Earned leave list']:
date_j = datetime.datetime.strptime(j,"%d-%m-%Y")
if doj_datetime < date_j and date_j < cal_el_date:
new_el_list.append(j)
new_el_count_2 = len(new_el_list)
# counting number of lop
# LOP dict has both LOP entries and strike entries
new_lop_list = []
for j in d[i]['LOP']:
date_j = datetime.datetime.strptime(j, "%d-%m-%Y")
if doj_datetime < date_j and date_j < cal_el_date:
new_lop_list.append(j)
new_lop_count = len(new_lop_list)
# counting number of paternity leave and maternity leave
new_special_leave_list = []
for j in d[i]['Special leave list'].keys():
if d[i]["Special leave list"][j] == 'Maternity leave':
date_j = datetime.datetime.strptime(j, "%d-%m-%Y")
if doj_datetime < date_j and date_j < cal_el_date:
new_special_leave_list.append(j)
elif d[i]['Special leave list'][j] == "Paternity leave":
date_j = datetime.datetime.strptime(j, "%d-%m-%Y")
if doj_datetime < date_j and date_j < cal_el_date:
new_special_leave_list.append(j)
new_special_leave_count = len(new_special_leave_list)
no_of_days = numOfDays(doj_datetime,cal_el_date)
current_leave_count = d[i]['earned leave']
earnedleavedays = no_of_days - int(new_sl_count) - new_el_count_2 - int(new_lop_count) - int(new_special_leave_count)
accruedleaves = earnedleavedays / 11
new_el_count = float(accruedleaves) + float(current_leave_count)
new_el_count = min(new_el_count,270)
if doj_datetime < cal_el_date:
d[i]['earned leave'] = new_el_count
d[i]['Leave updated as on'] = cal_el_date_string
frac_earned_leave = dec_to_proper_frac(i)
print("Earned leave has been updated for employee number %s up to %s. Updated earned leave balance: %s."
%(i, cal_el_date_string, frac_earned_leave))
# else:
# print("Earned leave has already been updated until %s" % doj_datetime)
save_data()
def report_leave(date):
emp_list = d.keys()
print("List of employees who were absent on %s." % date)
for i in emp_list:
leave_type = ['Earned leave list','RH list']
dict_type = ['casual leave dict', 'Special leave list','LOP','sick leave dict']
for j in leave_type:
if date in d[i][j]:
print("Name: %s. Type of leave: %s" % (str(d[i]['name']), j))
for j in dict_type:
if date in d[i][j]:
print("Name: %s. Type of leave: %s Nature of leave: %s" % (str(d[i]['name']), j, d[i][j][date]))
def report_leave_encashment():
emp_list = d.keys()
print("List of employees who have taken leave encashment: ")
for i in emp_list:
for j in d[i]['Leave encashment']:
print("%s has taken leave encashment in block year %s." % (d[i]['name'],j))
def report_lop(leave_type,lop_choice):
emp_list = d.keys()
print("List of employees who are on %s: " % lop_choice)
for i in emp_list:
for j in d[i][leave_type].keys():
if d[i][leave_type][j] == lop_choice:
print("Name: %s. Date: %s" % (d[i]['name'], j))
def dec_to_proper_frac(emp_no):
earned_leave = float(d[emp_no]['earned leave'])
if not (earned_leave).is_integer():
a = int(earned_leave)
new = earned_leave - a
b = Fraction(new % 11).limit_denominator(100)
fraction = str(a)+" " + str(b)
return fraction
else:
return earned_leave
def export_to_text(emp_no):
date = datetime.datetime.now()
str_date = str(date)
emp_name = d[emp_no]['name']
string = ""
for character in str_date:
if character.isalnum():
string = string+character
file_name = emp_name+emp_no + string+".txt"
with open(file_name, 'w') as f:
with redirect_stdout(f):
display_emp(emp_no)
print("Leave data of %s has been exported to text file." % emp_no)
def display_emp(emp_no):
frac_earned_leave = dec_to_proper_frac(emp_no)
leave_encashment_status = ""
if len(d[emp_no]['Leave encashment']) == 0:
leave_encashment_status = "Leave encashment not availed"
else:
block = list(d[emp_no]['Leave encashment'].keys())[-1]
leave_encashment_status = "Leave encashment last availed for the block year " + block
print("""=========================================================================
Employee name : %s
Employee number : %s
Current Casual leave balance : %s
Current EL balance : %s
EL balance last updated on : %s
Leave encashment : %s.
Current sick leave balance : %s
Current Restricted holiday balance : %s
List of casual leaves : %s
List of earned leaves : %s
List of sick leaves : %s
List of RH : %s
List of LOP : %s
List of special leaves : %s
=========================================================================
""" % (d[emp_no]['name'], d[emp_no]['employee number'], d[emp_no]['casual leave'], frac_earned_leave, d[emp_no]['Leave updated as on'],leave_encashment_status, d[emp_no]['sick leave'], d[emp_no]['RH'], d[emp_no]['casual leave dict'], d[emp_no]['Earned leave list'],d[emp_no]['sick leave dict'],d[emp_no]['RH list'], d[emp_no]['LOP'],d[emp_no]['Special leave list'] ))
def save_data():
a_file = open("data.json", "w")
json.dump(d,a_file)
a_file.close()
def backup_data():
date = datetime.datetime.now()
str_date = str(date)
string = ""
for character in str_date:
if character.isalnum():
string = string+character
backup_file_name = "data" + string +".json"
backup_file = open(backup_file_name, "w")
json.dump(d,backup_file)
backup_file.close()
def employeeloop(empno, choice):
if choice == "1":
display_emp(empno)
elif choice == "2":
add_casual_leave(empno)
elif choice == "3":
add_earned_leave(empno)
elif choice == "4":
add_sick_leave(empno)
elif choice == "8":
export_to_text(empno)
elif choice == "9":
# calculate earned leave for the employee number here
start_el = input("Enter date up to which earned leave to be calculated for the employee in dd-mm-yyyy format: ")
# validate input
start_el = validate_date(start_el)
start_el_date = datetime.datetime.strptime(start_el, "%d-%m-%Y")
calculate_el_emp(empno, start_el_date, start_el_date,False)
elif choice == "5":
leave_input = input("""
Press 1 to enter leave encashment.
Press 2 to enter restricted holiday.
Press 3 to enter LOP or strike.
Press 4 to enter Special leave (eg. Examination leave, Maternity leave, paternity leave, quarantine leave, etc.)
Press 9 to return to previous menu.
Enter your option: """)
if leave_input == "1":
leave_encashment(empno)
elif leave_input == "2":
add_rh_leave(empno)
elif leave_input == "3":
add_LOP_leave(empno)
elif leave_input == "4":
add_special_leave(empno)
else:
return
elif choice == "6":
leave_input = input("""
Press 1 to delete casual leave.
Press 2 to delete earned leave.
Press 3 to delete sick leave.
Press 4 to delete Restricted holiday.
Press 5 to delete leave encashment.
Press 6 to delete LOP / Strike.
Press 7 to delete Special leave.
Press 9 to return to previous menu.
Enter your option: """)
if leave_input == "1":
del_casual_leave(empno)
elif leave_input == "2":
del_list_leave(empno,"earned leave","Earned leave list")
elif leave_input == "3":
del_sick_leave(empno)
elif leave_input == "4":
del_list_leave(empno,"RH", "RH list")
elif leave_input == "5":
del_leave_encashment(empno)
elif leave_input == "6":