def update_attendance(self):
		if self.status == "Approved":
			for dt in daterange(getdate(self.from_date), getdate(self.to_date)):
				date = dt.strftime("%Y-%m-%d")
				status = "Half Day" if getdate(date) == getdate(self.half_day_date) else "On Leave"

				attendance_name = frappe.db.exists('Attendance', dict(employee = self.employee,
					attendance_date = date, docstatus = ('!=', 2)))

				if attendance_name:
					# update existing attendance, change absent to on leave
					doc = frappe.get_doc('Attendance', attendance_name)
					if doc.status != status:
						doc.db_set('status', status)
						doc.db_set('leave_type', self.leave_type)
						doc.db_set('leave_application', self.name)
				else:
					# make new attendance and submit it
					doc = frappe.new_doc("Attendance")
					doc.employee = self.employee
					doc.employee_name = self.employee_name
					doc.attendance_date = date
					doc.company = self.company
					doc.leave_type = self.leave_type
					doc.leave_application = self.name
					doc.status = status
					doc.flags.ignore_validate = True
					doc.insert(ignore_permissions=True)
					doc.submit()
Beispiel #2
0
	def update_attendance(self):
		if self.status == "Approved":
			attendance = frappe.db.sql("""select name from `tabAttendance` where employee = %s\
				and (attendance_date between %s and %s) and docstatus < 2""",(self.employee, self.from_date, self.to_date), as_dict=1)

			if attendance:
				for d in attendance:
					doc = frappe.get_doc("Attendance", d.name)
					if getdate(self.half_day_date) == doc.attendance_date:
						status = "Half Day"
					else:
						status = "On Leave"
					frappe.db.sql("""update `tabAttendance` set status = %s, leave_type = %s\
						where name = %s""",(status, self.leave_type, d.name))

			elif getdate(self.to_date) <= getdate(nowdate()):
				for dt in daterange(getdate(self.from_date), getdate(self.to_date)):
					date = dt.strftime("%Y-%m-%d")
					doc = frappe.new_doc("Attendance")
					doc.employee = self.employee
					doc.attendance_date = date
					doc.company = self.company
					doc.leave_type = self.leave_type
					doc.status = "Half Day" if date == self.half_day_date else "On Leave"
					doc.flags.ignore_validate = True
					doc.insert(ignore_permissions=True)
					doc.submit()
Beispiel #3
0
	def update_attendance(self):
		if self.status != "Approved":
			return

		holiday_dates = []
		if not frappe.db.get_value("Leave Type", self.leave_type, "include_holiday"):
			holiday_dates = get_holiday_dates_for_employee(self.employee, self.from_date, self.to_date)

		for dt in daterange(getdate(self.from_date), getdate(self.to_date)):
			date = dt.strftime("%Y-%m-%d")
			attendance_name = frappe.db.exists(
				"Attendance", dict(employee=self.employee, attendance_date=date, docstatus=("!=", 2))
			)

			# don't mark attendance for holidays
			# if leave type does not include holidays within leaves as leaves
			if date in holiday_dates:
				if attendance_name:
					# cancel and delete existing attendance for holidays
					attendance = frappe.get_doc("Attendance", attendance_name)
					attendance.flags.ignore_permissions = True
					if attendance.docstatus == 1:
						attendance.cancel()
					frappe.delete_doc("Attendance", attendance_name, force=1)
				continue

			self.create_or_update_attendance(attendance_name, date)
Beispiel #4
0
def update_attendance_override(self):
    ''' Function to overide Roster Statuses on applying leaves'''
    if self.status == LEAVE_STATUS_APPROVED:
        attendance = frappe.db.sql(
            """select name from `tabAttendance` where employee = %s\
			and (attendance_date between %s and %s) and docstatus < 2""",
            (self.employee, self.from_date, self.to_date),
            as_dict=1)
        if attendance:
            for d in attendance:
                doc = frappe.get_doc("Attendance", d.name)
                status = ATTENDANCE_STATUS_ON_LEAVE
                if (self.half_day and getdate(self.half_day_date)
                        == doc.attendance_date):
                    status = ATTENDANCE_STATUS_HALF_DAY_LEAVE
                if (self.hourly and getdate(self.hourly_day_date)
                        == doc.attendance_date):
                    status = ATTENDANCE_STATUS_HOURLY_LEAVE
                frappe.db.sql(
                    """update `tabAttendance` set status = %s, leave_type = %s\
					where name = %s""", (status, self.leave_type, d.name))

        else:
            for dt in daterange(getdate(self.from_date),
                                getdate(self.to_date)):
                date = dt.strftime("%Y-%m-%d")
                if get_holidays(self.employee, date, date) == 0:
                    doc = frappe.new_doc("Attendance")
                    doc.employee = self.employee
                    doc.employee_name = self.employee_name
                    doc.attendance_date = date
                    doc.company = self.company
                    shift_detail = get_employee_shift_timings(self.employee)
                    if shift_detail:
                        doc.shift_start_time = shift_detail.start_time
                        doc.shift_end_time = shift_detail.end_time
                    doc.leave_type = self.leave_type
                    doc.attendance_status = ROSTER_STATUS_ON
                    doc.status = ATTENDANCE_STATUS_ON_LEAVE
                    if (self.half_day and date == self.half_day_date):
                        doc.status = ATTENDANCE_STATUS_HALF_DAY_LEAVE
                    elif (self.hourly and date == self.hourly_day_date):
                        doc.status = ATTENDANCE_STATUS_HOURLY_LEAVE
                    doc.flags.ignore_validate = True
                    doc.insert(ignore_permissions=True)
                    doc.submit()
Beispiel #5
0
    def mark_absent_for_dates_with_no_attendance(self, employee):
        """Marks Absents for the given employee on working days in this shift which have no attendance marked.
		The Absent is marked starting from 'process_attendance_after' or employee creation date.
		"""
        start_date, end_date = self.get_start_and_end_dates(employee)

        # no shift assignment found, no need to process absent attendance records
        if start_date is None:
            return

        holiday_list_name = self.holiday_list
        if not holiday_list_name:
            holiday_list_name = get_holiday_list_for_employee(employee, False)

        start_time = get_time(self.start_time)

        for date in daterange(getdate(start_date), getdate(end_date)):
            if is_holiday(holiday_list_name, date):
                # skip marking absent on a holiday
                continue

            timestamp = datetime.combine(date, start_time)
            shift_details = get_employee_shift(employee, timestamp, True)

            if shift_details and shift_details.shift_type.name == self.name:
                attendance = mark_attendance(employee, date, "Absent",
                                             self.name)
                if attendance:
                    frappe.get_doc({
                        "doctype":
                        "Comment",
                        "comment_type":
                        "Comment",
                        "reference_doctype":
                        "Attendance",
                        "reference_name":
                        attendance,
                        "content":
                        frappe.
                        _("Employee was marked Absent due to missing Employee Checkins."
                          ),
                    }).insert(ignore_permissions=True)