/
attendance.py
executable file
·183 lines (155 loc) · 7.06 KB
/
attendance.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
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from __future__ import division
import frappe
from frappe import msgprint
from frappe.utils import getdate, cint, add_months, date_diff, add_days, nowdate, \
get_datetime_str, cstr, get_datetime, time_diff, time_diff_in_seconds
from datetime import datetime, timedelta
def on_update(doc,method):
validate(doc,method)
def validate(doc,method):
global attendance_date
att_tt = []
att_time = []
attendance_date = getdate(doc.attendance_date)
if doc.status != "Present" and doc.status != "Half Day" and doc.status != "On Leave" :
frappe.throw(("Only Present or Half Day or On Leave Attendance is Allowed Check {0}").format(doc.name))
doc.deparment = frappe.get_value("Employee", doc.employee, "department")
check_employee (doc, method)
shft = get_shift(doc,method)
if doc.status != "On Leave" and shft.in_out_required:
calculate_overtime(doc,method)
#Function to check if the attendance is not for a NON-WORKING employee
def check_employee(doc, method):
#Check if the employee is Active for the Dates
emp = frappe.get_doc("Employee", doc.employee)
if emp.status == "Left":
if emp.relieving_date < attendance_date:
frappe.throw(("Cannot create attendance for {0} as he/she has left on {1}").\
format(emp.employee_name, emp.relieving_date))
else:
if emp.date_of_joining > attendance_date:
frappe.throw(("Cannot create attendance for {0} as he/she has joined on {1}").\
format(emp.employee_name, emp.date_of_joining))
#Function to check the shift and update the same from shift assignmentt.
def get_shift(doc,method):
query = """SELECT sa.name, sa.shift_type FROM `tabShift Assignment` sa
WHERE sa.date = '%s' AND sa.employee = '%s' AND sa.docstatus = 1"""\
%(doc.attendance_date, doc.employee)
sa_list = frappe.db.sql(query, as_list=1)
if len(sa_list)<1:
#Check if Date is Holiday or not and if its holiday then take the shift of previous day
holiday_lst = frappe.get_value("Employee", doc.employee, "holiday_list")
hol_lst_doc = frappe.get_doc("Holiday List", holiday_lst)
check = 0
for d in hol_lst_doc.holidays:
if d.holiday_date == getdate(doc.attendance_date):
check = 1
if check == 1:
attendance_date = add_days(getdate(doc.attendance_date), -1)
query = """SELECT sa.name, sa.shift_type FROM `tabShift Assignment` sa
WHERE sa.date = '%s' AND sa.employee = '%s' AND sa.docstatus = 1"""\
%(attendance_date, doc.employee)
sa_list = frappe.db.sql(query, as_list=1)
if len(sa_list)<1:
frappe.throw(("No Shift Assignment defined for {0} for \
date {1} which is a Holiday and its Previous Day").\
format(doc.employee, doc.attendance_date))
else:
doc.shift = sa_list[0][1]
else:
frappe.throw(("No Shift Assignment defined for {0} for \
date {1}").format(doc.employee, doc.attendance_date))
else:
doc.shift = sa_list[0][1]
shft = frappe.get_doc("Shift Type", doc.shift)
return shft
def validate_time_with_shift(doc,method):
shft = frappe.get_doc("Shift Type", doc.shift)
if shft.in_out_required:
if doc.attendance_time:
shft_hrs = shft.hours_required_per_day.seconds
shft_rounding = shft.time_rounding.seconds
shft_marg = shft.time_margin.seconds
if shft_rounding <= 0 or shft_marg <=0:
frappe.throw("Shift Rounding or Shift Margin cannot be Zero")
if shft.start_time > shft.end_time:
#this shows night shift
if shft.next_day != 1:
#this shows night shift is starting on previous day
shft_indate = datetime.combine(add_days(attendance_date, -1), datetime.min.time())
else:
shft_indate = datetime.combine(attendance_date, datetime.min.time())
else:
shft_indate = datetime.combine(attendance_date, datetime.min.time())
shft_intime = shft_indate + timedelta(0, shft.start_time.seconds)
shft_intime_max = shft_intime + timedelta(0, shft.delayed_entry_allowed_time.seconds)
shft_intime_min = shft_intime - timedelta(0, shft.early_entry_allowed_time.seconds)
if shft.lunch_out > shft.start_time:
shft_lunchout = shft_indate + timedelta(0, shft.lunch_in.seconds)
shft_lunchin = shft_indate + timedelta(0, shft.lunch_in.seconds)
else:
shft_lunchout = shft_indate + timedelta(0, 86400+shft.lunch_in.seconds)
shft_lunchin = shft_indate + timedelta(0, 86400+shft.lunch_in.seconds)
for d in doc.attendance_time:
if d.idx == 1:
d.date_time = get_datetime(d.date_time)
if d.date_time >= shft_intime_min and d.date_time <= shft_intime_max:
pass
else:
frappe.throw(("Time {0} in row#1 is not allowed for Shift# {1} for {2}.\
Check early and delayed entry settings in Shift Master").\
format(d.date_time, doc.shift, doc.name))
return shft_intime, shft_lunchout, shft_lunchin, shft_hrs, shft_rounding, shft_marg
else:
if doc.status == "On Leave":
pass
else:
frappe.msgprint(str(doc.status))
frappe.throw("For Shift " + shft.name + " In and Out Punch are required")
def calculate_overtime(doc,method):
doc.overtime = 0
tt_in = 0
tt_out = 0
shft_intime, shft_lunchout, shft_lunchin, shft_hrs, \
shft_rounding, shft_marg = validate_time_with_shift(doc,method)
pu_data = check_punch_data(doc, method)
#only calculate the ot if there are any IN and OUT entries
#if doc.attendance_time:
for i in range(len(doc.attendance_time)-1):
pass
for i in range(len(pu_data)-1):
if pu_data[i][1] == 'In Time':
tt_in += time_diff_in_seconds(pu_data[i+1][2], pu_data[i][2])
else:
tt_out += time_diff_in_seconds(pu_data[i+1][2], pu_data[i][2])
doc.overtime = ((tt_in - shft_hrs + shft_marg)- \
((tt_in + shft_marg - shft_hrs)%shft_rounding))/3600
def check_punch_data(doc,method):
shft_intime, shft_lunchout, shft_lunchin, shft_hrs, \
shft_rounding, shft_marg = validate_time_with_shift(doc,method)
pu_data = []
for d in doc.attendance_time:
if d.idx == 1 and d.time_type != 'In Time':
frappe.throw(("First Punch Data should be In Time for {0}").format(doc.name))
pu_data.append([d.idx, d.time_type, d.date_time])
for i in range(len(pu_data)-1):
#Checks if In and Out are alternating
if pu_data[i][1] == pu_data[i+1][1]:
frappe.throw(("{1} should not be Followed by {1} for {2} check row # {3} & {4}").\
format(pu_data[i][1], pu_data[i][1], doc.name, pu_data[i][0], pu_data[i+1][0]))
#Checks if Time Data is following the minimum time difference rule in shift
if time_diff_in_seconds(pu_data[i+1][2], pu_data[i][2]) <= shft_marg:
frappe.throw(("Difference between 2 punch data cannot be less than \
{0} mins check row# {1} and {2} for {3}").\
format(shft_marg/60, pu_data[i][0], pu_data[i+1][0], doc.name))
return pu_data
#Function to define the minimum and max difference allowed between times
def diff_allowed(time, i, min_in_mins, max_in_hours):
if time < timedelta(minutes=min_in_mins):
frappe.msgprint('{0}{1}'.format("Difference between 2 times cannot be less than 10 mins, Check line #", i+2),
raise_exception=1)
if time > timedelta(hours = max_in_hours):
frappe.msgprint('{0}{1}'.format("Difference between 2 times cannot be greater than 18 hrs, Check line #", i+2),
raise_exception=1)