Skip to content

Commit

Permalink
style: format code with black
Browse files Browse the repository at this point in the history
  • Loading branch information
ruchamahabal committed Mar 29, 2022
1 parent be1e2d7 commit d44ae7d
Show file tree
Hide file tree
Showing 6 changed files with 575 additions and 391 deletions.
91 changes: 57 additions & 34 deletions erpnext/hr/doctype/attendance/attendance.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
class DuplicateAttendanceError(frappe.ValidationError):
pass


class Attendance(Document):
def validate(self):
from erpnext.controllers.status_updater import validate_status
Expand All @@ -39,12 +40,20 @@ def validate_attendance_date(self):
frappe.throw(_("Attendance date can not be less than employee's joining date"))

def validate_duplicate_record(self):
duplicate = get_duplicate_attendance_record(self.employee, self.attendance_date, self.shift, self.name)
duplicate = get_duplicate_attendance_record(
self.employee, self.attendance_date, self.shift, self.name
)

if duplicate:
frappe.throw(_("Attendance for employee {0} is already marked for the date {1}: {2}").format(
frappe.bold(self.employee), frappe.bold(self.attendance_date), get_link_to_form("Attendance", duplicate[0].name)),
title=_("Duplicate Attendance"), exc=DuplicateAttendanceError)
frappe.throw(
_("Attendance for employee {0} is already marked for the date {1}: {2}").format(
frappe.bold(self.employee),
frappe.bold(self.attendance_date),
get_link_to_form("Attendance", duplicate[0].name),
),
title=_("Duplicate Attendance"),
exc=DuplicateAttendanceError,
)

def validate_employee_status(self):
if frappe.db.get_value("Employee", self.employee, "status") == "Inactive":
Expand Down Expand Up @@ -101,26 +110,29 @@ def get_duplicate_attendance_record(employee, attendance_date, shift, name=None)
attendance = frappe.qb.DocType("Attendance")
query = (
frappe.qb.from_(attendance)
.select(attendance.name)
.where(
(attendance.employee == employee)
& (attendance.docstatus < 2)
)
.select(attendance.name)
.where((attendance.employee == employee) & (attendance.docstatus < 2))
)

if shift:
query = query.where(
Criterion.any([
Criterion.all([
((attendance.shift.isnull()) | (attendance.shift == "")),
(attendance.attendance_date == attendance_date)
]),
Criterion.all([
((attendance.shift.isnotnull()) | (attendance.shift != "")),
(attendance.attendance_date == attendance_date),
(attendance.shift == shift)
])
])
Criterion.any(
[
Criterion.all(
[
((attendance.shift.isnull()) | (attendance.shift == "")),
(attendance.attendance_date == attendance_date),
]
),
Criterion.all(
[
((attendance.shift.isnotnull()) | (attendance.shift != "")),
(attendance.attendance_date == attendance_date),
(attendance.shift == shift),
]
),
]
)
)
else:
query = query.where((attendance.attendance_date == attendance_date))
Expand Down Expand Up @@ -167,21 +179,32 @@ def add_attendance(events, start, end, conditions=None):
if e not in events:
events.append(e)

def mark_attendance(employee, attendance_date, status, shift=None, leave_type=None, ignore_validate=False,
late_entry=False, early_exit=False):

def mark_attendance(
employee,
attendance_date,
status,
shift=None,
leave_type=None,
ignore_validate=False,
late_entry=False,
early_exit=False,
):
if not get_duplicate_attendance_record(employee, attendance_date, shift):
company = frappe.db.get_value('Employee', employee, 'company')
attendance = frappe.get_doc({
'doctype': 'Attendance',
'employee': employee,
'attendance_date': attendance_date,
'status': status,
'company': company,
'shift': shift,
'leave_type': leave_type,
'late_entry': late_entry,
'early_exit': early_exit
})
company = frappe.db.get_value("Employee", employee, "company")
attendance = frappe.get_doc(
{
"doctype": "Attendance",
"employee": employee,
"attendance_date": attendance_date,
"status": status,
"company": company,
"shift": shift,
"leave_type": leave_type,
"late_entry": late_entry,
"early_exit": early_exit,
}
)
attendance.flags.ignore_validate = ignore_validate
attendance.insert()
attendance.submit()
Expand Down
22 changes: 16 additions & 6 deletions erpnext/hr/doctype/employee_checkin/employee_checkin.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,21 @@ def validate_duplicate_log(self):
)

def fetch_shift(self):
shift_actual_timings = get_actual_start_end_datetime_of_shift(self.employee, get_datetime(self.time), True)
shift_actual_timings = get_actual_start_end_datetime_of_shift(
self.employee, get_datetime(self.time), True
)
if shift_actual_timings:
if shift_actual_timings.shift_type.determine_check_in_and_check_out == 'Strictly based on Log Type in Employee Checkin' \
and not self.log_type and not self.skip_auto_attendance:
frappe.throw(_('Log Type is required for check-ins falling in the shift: {0}.').format(shift_actual_timings.shift_type.name))
if (
shift_actual_timings.shift_type.determine_check_in_and_check_out
== "Strictly based on Log Type in Employee Checkin"
and not self.log_type
and not self.skip_auto_attendance
):
frappe.throw(
_("Log Type is required for check-ins falling in the shift: {0}.").format(
shift_actual_timings.shift_type.name
)
)
if not self.attendance:
self.shift = shift_actual_timings.shift_type.name
self.shift_actual_start = shift_actual_timings.actual_start
Expand Down Expand Up @@ -125,8 +135,8 @@ def mark_attendance_and_link_log(
("1", log_names),
)
return None
elif attendance_status in ('Present', 'Absent', 'Half Day'):
employee_doc = frappe.get_doc('Employee', employee)
elif attendance_status in ("Present", "Absent", "Half Day"):
employee_doc = frappe.get_doc("Employee", employee)
if not get_duplicate_attendance_record(employee, attendance_date, shift):
doc_dict = {
"doctype": "Attendance",
Expand Down
Loading

0 comments on commit d44ae7d

Please sign in to comment.