예제 #1
0
def calculate_late_arrivals(doctype):

    attendance_date = datetime.strptime(str(doctype.attendance_date), "%Y-%m-%d")
    attendance_month = attendance_date.month
    attendance_year = attendance_date.year

    if check_processed(doctype.employee, attendance_month, attendance_year, doctype.attendance_date) == True:
        frappe.throw(_("Attendance Cannot be updated after bieng processed"))
    	
	# Calculate Late Arrivals, Working hours, Left Early
    
    if(doctype.shift_start_time and doctype.shift_end_time):
        try:
            if(doctype.check_in and doctype.check_out):
                doctype.working_hours = get_time_diff(doctype.check_out, doctype.check_in, "hours")
            
            attendance_date = attendance_date.strftime("%Y-%m-%d")
            shift_start = "{} {}".format(attendance_date, doctype.shift_start_time)
            shift_end = "{} {}".format(attendance_date, doctype.shift_end_time)

            if get_timestamp(shift_end) < get_timestamp(shift_start):
                _shift_start = datetime.strptime(shift_end, "%Y-%m-%d %H:%M:%S") + timedelta(days=1)
                shift_end = datetime.strftime(_shift_start, "%Y-%m-%d %H:%M:%S")
           
            if(doctype.check_in):
                doctype.late_arrival = get_time_diff(doctype.check_in, shift_start,"mins")
            
            if(doctype.check_out):
                doctype.left_early = get_time_diff(shift_end, doctype.check_out, "mins")
            else:
                doctype.left_early = 0
                    
        except Exception as err:
            print(err)
    def new_attendance(self, today_shift_start, prev_shift_start,
                       prev_shift_end, emp_shift_prev, emp_shift):

        emp_att = EmployeeAttendance()

        if get_timestamp(self.attendance_datetime) >= get_timestamp(prev_shift_start) and\
           get_timestamp(self.attendance_datetime) <= get_timestamp(prev_shift_end):

            emp_att.employee = self.emp_id
            # emp_att.shift = emp_shift_prev.name
            emp_att.grace_mins = self.GRACE_MINS
            emp_att.attendance_date = self.prev_date
            emp_att.check_in = self.attendance_datetime
            emp_att.shift_start_time = emp_shift.start_time
            emp_att.shift_end_time = emp_shift.end_time
            emp_att.late_arrival = get_time_diff(self.attendance_datetime,
                                                 prev_shift_start, "mins")

            return self.add_update_doc(emp_att)

        else:

            emp_att.employee = self.emp_id
            # emp_att.shift = emp_shift.name
            emp_att.grace_mins = self.GRACE_MINS
            emp_att.attendance_date = self.attendance_date
            emp_att.check_in = self.attendance_datetime
            emp_att.shift_start_time = emp_shift.start_time
            emp_att.shift_end_time = emp_shift.end_time
            emp_att.late_arrival = get_time_diff(self.attendance_datetime,
                                                 today_shift_start, "mins")

            return self.add_update_doc(emp_att)
    def get_shift_start_end(self, attendance_date, emp_shift):

        today_shift_start = "{0} {1}".format(attendance_date,
                                             emp_shift.start_time)
        today_shift_end = "{0} {1}".format(attendance_date, emp_shift.end_time)

        if get_timestamp(today_shift_end) < get_timestamp(today_shift_start):
            _shift_start = datetime.strptime(
                today_shift_end, "%Y-%m-%d %H:%M:%S") + timedelta(days=1)
            today_shift_end = datetime.strftime(_shift_start,
                                                "%Y-%m-%d %H:%M:%S")

        return today_shift_start, today_shift_end
    def prev_attendance(self, indexed_list, prev_shift_start, prev_shift_end,
                        emp_shift, today_shift_start):

        min_checkout = min_checkin = datetime.strptime(
            prev_shift_start,
            "%Y-%m-%d %H:%M:%S") - timedelta(minutes=self.MIN_EARLY)
        max_checkin = datetime.strptime(
            prev_shift_end, "%Y-%m-%d %H:%M:%S") - timedelta(minutes=60)

        max_checkout = datetime.strptime(
            prev_shift_end,
            "%Y-%m-%d %H:%M:%S") + timedelta(minutes=self.MAX_LATE)

        emp_att = EmployeeAttendance()

        if get_timestamp(prev_shift_end) >= get_timestamp(self.attendance_datetime) or\
           get_timestamp(max_checkout) >= get_timestamp(self.attendance_datetime):

            prev_att_data = indexed_list[self.prev_date]

            emp_att.check_out = self.attendance_datetime
            emp_att.left_early = get_time_diff(prev_shift_end,
                                               self.attendance_datetime,
                                               "mins")
            emp_att.working_hours = get_time_diff(self.attendance_datetime,
                                                  prev_att_data["check_in"],
                                                  "hours")

            return self.add_update_doc(emp_att, prev_att_data["name"])
        else:

            emp_att.employee = self.emp_id
            # emp_att.shift = emp_shift.name
            emp_att.grace_mins = self.GRACE_MINS
            emp_att.attendance_date = self.attendance_date
            emp_att.check_in = self.attendance_datetime
            emp_att.shift_start_time = emp_shift.start_time
            emp_att.shift_end_time = emp_shift.end_time
            emp_att.late_arrival = get_time_diff(self.attendance_datetime,
                                                 today_shift_start, "mins")

            return self.add_update_doc(emp_att)
    def current_attendance(self, indexed_list, today_shift_start,
                           today_shift_end, prev_shift_end):

        today_attendance = indexed_list[self.attendance_date]
        min_checkout = min_checkin = datetime.strptime(
            today_shift_start,
            "%Y-%m-%d %H:%M:%S") - timedelta(minutes=self.MIN_EARLY)
        max_checkin = datetime.strptime(
            today_shift_end, "%Y-%m-%d %H:%M:%S") - timedelta(minutes=60)

        prev_max_checkout = datetime.strptime(
            prev_shift_end,
            "%Y-%m-%d %H:%M:%S") + timedelta(minutes=self.MAX_LATE)

        emp_att = EmployeeAttendance()

        att_id = today_attendance["name"]

        if get_timestamp(self.attendance_datetime) <= get_timestamp(today_attendance["check_in"]) or \
            today_attendance["check_in"] is None:

            if get_timestamp(self.attendance_datetime) >= get_timestamp(min_checkin) and\
               get_timestamp(self.attendance_datetime) <= get_timestamp(max_checkin):

                emp_att.check_in = self.attendance_datetime
                emp_att.late_arrival = get_time_diff(self.attendance_datetime,
                                                     today_shift_start, "mins")

                if today_attendance["check_in"]:
                    emp_att.check_out = today_attendance["check_in"]
                    emp_att.left_early = get_time_diff(today_shift_end,
                                                       emp_att.check_out,
                                                       "mins")
                    emp_att.working_hours = get_time_diff(
                        emp_att.check_out, emp_att.check_in, "hours")

            else:

                if str(self.prev_date) in indexed_list and indexed_list[self.prev_date]["check_out"] == None \
                    and indexed_list[self.prev_date]["check_in"] != None \
                    and get_timestamp(self.attendance_datetime) <= get_timestamp(prev_max_checkout):

                    att_id = indexed_list[self.prev_date]["name"]

                    emp_att.check_out = self.attendance_datetime
                    emp_att.left_early = get_time_diff(
                        prev_shift_end, self.attendance_datetime, "mins")
                    emp_att.working_hours = get_time_diff(
                        self.attendance_datetime,
                        indexed_list[str(self.prev_date)]["check_in"], "hours")

                else:

                    emp_att.check_in = self.attendance_datetime
                    emp_att.late_arrival = get_time_diff(
                        self.attendance_datetime, today_shift_start, "mins")

                    if today_attendance["check_in"]:
                        emp_att.check_out = today_attendance["check_in"]
                        emp_att.left_early = get_time_diff(
                            today_shift_end, emp_att.check_out, "mins")
                        emp_att.working_hours = get_time_diff(
                            emp_att.check_out, emp_att.check_in, "hours")

        else:

            emp_att.check_out = self.attendance_datetime
            emp_att.left_early = get_time_diff(today_shift_end,
                                               self.attendance_datetime,
                                               "mins")
            emp_att.working_hours = get_time_diff(self.attendance_datetime,
                                                  today_attendance["check_in"],
                                                  "hours")

        return self.add_update_doc(emp_att, att_id)