Example #1
0
    def _compute_leave_duration(self):
        for holiday in self:
            if all((holiday.date_from, holiday.date_to)):
                dt_date_from = Datetime.from_string(holiday.date_from)
                dt_date_to = Datetime.from_string(holiday.date_to)
                delta = dt_date_to - dt_date_from

                holiday.leave_duration = _("%ddays%dhours%dminutes") % (
                    delta.days, delta.seconds / 3600, (delta.seconds % 3600) / 60)
Example #2
0
    def _compute_leave_duration(self):
        for holiday in self:
            if all((holiday.date_from, holiday.date_to)):
                dt_date_from = Datetime.from_string(holiday.date_from)
                dt_date_to = Datetime.from_string(holiday.date_to)
                delta = dt_date_to - dt_date_from

                holiday.leave_duration = _("%ddays%dhours%dminutes") % (
                    delta.days, delta.seconds / 3600,
                    (delta.seconds % 3600) / 60)
Example #3
0
    def get_absent_on(self, date=None):
        self.ensure_one()
        if not date:
            return None

        querying_day = date
        if isinstance(date, datetime.date):
            querying_day = Date.to_string(date)

        absent_type = self.env.ref("hr_sf.absent_holidays_status")
        absent_type_id = absent_type.id

        for holiday in self.holidays_ids:
            if not holiday.date_from or not holiday.date_to:
                continue
            if holiday.holiday_status_id.id != absent_type_id:
                continue

            if not all((holiday.morning_start_work_time,
                        holiday.morning_end_work_time,
                        holiday.afternoon_start_work_time,
                        holiday.afternoon_end_work_time)):
                return None

            dt_the_day_morning_start_work_time = datetime.datetime.strptime(
                    "%s %s" % (querying_day, holiday.morning_start_work_time), DEFAULT_SERVER_DATETIME_FORMAT)
            dt_the_day_morning_end_work_time = datetime.datetime.strptime(
                    "%s %s" % (querying_day, holiday.morning_end_work_time), DEFAULT_SERVER_DATETIME_FORMAT)
            dt_the_day_afternoon_start_work_time = datetime.datetime.strptime(
                    "%s %s" % (querying_day, holiday.afternoon_start_work_time), DEFAULT_SERVER_DATETIME_FORMAT)
            dt_the_day_afternoon_end_work_time = datetime.datetime.strptime(
                    "%s %s" % (querying_day, holiday.afternoon_end_work_time), DEFAULT_SERVER_DATETIME_FORMAT)

            dt_holiday_from = Datetime.from_string(holiday.date_from) + datetime.timedelta(hours=8)
            dt_holiday_to = Datetime.from_string(holiday.date_to) + datetime.timedelta(hours=8)

            # deal with morning first
            dt_cal_start = max(dt_the_day_morning_start_work_time, dt_holiday_from)
            dt_cal_start = min(dt_cal_start, dt_the_day_morning_end_work_time)

            dt_cal_end = min(dt_the_day_morning_end_work_time, dt_holiday_to)
            dt_cal_end = max(dt_cal_end, dt_the_day_morning_start_work_time)

            if dt_cal_end > dt_cal_start:
                return absent_type.name

            # then deal with afternoon first
            dt_cal_start = max(dt_the_day_afternoon_start_work_time, dt_holiday_from)
            dt_cal_start = min(dt_cal_start, dt_the_day_afternoon_end_work_time)

            dt_cal_end = min(dt_the_day_afternoon_end_work_time, dt_holiday_to)
            dt_cal_end = max(dt_cal_end, dt_the_day_afternoon_start_work_time)

            if dt_cal_end > dt_cal_start:
                return absent_type.name
Example #4
0
    def get_work_duration_on(self, date=None):
        self.ensure_one()
        if not date:
            return None

        sign_in_attendance = self.get_sign_in_attendance(date)
        sign_out_attendance = self.get_sign_out_attendance(date)
        if sign_in_attendance and sign_out_attendance:
            dt_the_day_morning_start_work_time = datetime.datetime.strptime(
                "%s %s" % (date, sign_in_attendance.morning_start_work_time),
                DEFAULT_SERVER_DATETIME_FORMAT)
            dt_the_day_morning_end_work_time = datetime.datetime.strptime(
                "%s %s" % (date, sign_in_attendance.morning_end_work_time),
                DEFAULT_SERVER_DATETIME_FORMAT)
            dt_the_day_afternoon_start_work_time = datetime.datetime.strptime(
                "%s %s" %
                (date, sign_out_attendance.afternoon_start_work_time),
                DEFAULT_SERVER_DATETIME_FORMAT)
            dt_the_day_afternoon_end_work_time = datetime.datetime.strptime(
                "%s %s" % (date, sign_out_attendance.afternoon_end_work_time),
                DEFAULT_SERVER_DATETIME_FORMAT)

            dt_sign_in_time = Datetime.from_string(
                sign_in_attendance.name) + datetime.timedelta(hours=8)
            dt_sign_out_time = Datetime.from_string(
                sign_out_attendance.name) + datetime.timedelta(hours=8)

            # morning first
            dt_cal_start = max(dt_the_day_morning_start_work_time,
                               dt_sign_in_time)
            dt_cal_start = min(dt_cal_start, dt_the_day_morning_end_work_time)

            # dt_cal_end = min(dt_the_day_morning_end_work_time, dt_sign_out_time)
            # dt_cal_end = max(dt_cal_end, dt_the_day_morning_start_work_time)
            dt_cal_end = dt_the_day_morning_end_work_time
            work_duration = datetime.timedelta()
            if dt_cal_end > dt_cal_start:
                work_duration += dt_cal_end - dt_cal_start

            # then deal with afternoon
            # dt_cal_start = max(dt_the_day_afternoon_start_work_time, dt_sign_out_time)
            # dt_cal_start = min(dt_cal_start, dt_the_day_afternoon_end_work_time)
            dt_cal_start = dt_the_day_afternoon_start_work_time

            dt_cal_end = min(dt_the_day_afternoon_end_work_time,
                             dt_sign_out_time)
            dt_cal_end = max(dt_cal_end, dt_the_day_afternoon_start_work_time)

            if dt_cal_end > dt_cal_start:
                work_duration += dt_cal_end - dt_cal_start

            return work_duration.seconds / 3600.0
Example #5
0
    def index(self, **kwargs):
        method = request.httprequest.method
        if method == "GET":
            return request.render("hr_sf.attendance_index")
        elif method == "POST":
            Attendance = request.env["hr.attendance"].sudo()
            Employee = request.env["hr.employee"].sudo()
            UploadLog = request.env["hr_sf.attendance_upload_log"].sudo()

            try:
                upload_file = kwargs.get("upload_file", None)
                source = kwargs.get("source", None)
                if upload_file is None:
                    raise Exception("can not get upload file from http post data")
                upload_file_content = upload_file.stream.read()
                sniffer = csv.Sniffer()
                dialect = sniffer.sniff(upload_file_content, delimiters=',\t')
                rows = csv.reader(upload_file_content.splitlines(), dialect=dialect)

                upload_log = None
                if rows:
                    base64_file = base64.standard_b64encode(upload_file_content)
                    upload_log = UploadLog.create({"upload_file": base64_file, "file_name": upload_file.filename,
                                                   "date": Datetime.now(), "source": source})
                upload_log_id = upload_log.id

                employees = Employee.search([])
                employee_ids = dict(employees.mapped(lambda r: (r.internal_code, r.id)))

                line_number = 0
                values = []
                for row in rows:
                    code, name, year, month, day, hour, minute = row[0:7]
                    location = str(int(row[7]))

                    dt_str = "%s-%s-%s %s:%s:00" % (year, month, day, hour, minute)
                    dt = Datetime.from_string(dt_str)
                    dt = dt - datetime.timedelta(hours=8)
                    odoo_dt_str = Datetime.to_string(dt)

                    exist_count = Attendance.search_count([("code", "=", code), ("name", "=", odoo_dt_str)])
                    if exist_count <= 0:
                        emp_id = employee_ids.get(code, None)
                        if emp_id is not None:
                            # Attendance.create({"employee_id": emp_id, "name": odoo_dt_str, "location": location,
                            #                    "action": "action", "upload_log_id": upload_log_id})
                            values.append({"employee_id": emp_id, "name": odoo_dt_str, "location": location,
                                           "action": "action", "upload_log_id": upload_log_id,
                                           "forget_card": False})
                        else:
                            raise Exception("error in line:%d,employee with code:%s not found" % (line_number, code))
                    line_number += 1

                for value in values:
                    Attendance.create(value)

                return request.render("hr_sf.attendance_upload_finish", {"import_count": len(values)})
            except Exception, e:
                request.env.cr.rollback()
                return e.message or e.value
 def _check_delay(self, cr, uid, action, record, record_dt, context=None):
     """ Override the check of delay to try to use a user-related calendar.
     If no calendar is found, fallback on the default behavior. """
     if action.trg_date_calendar_id and action.trg_date_range_type == 'day' and action.trg_date_resource_field_id:
         user = record[action.trg_date_resource_field_id.name]
         if user.employee_ids and user.employee_ids[0].contract_id \
                 and user.employee_ids[0].contract_id.working_hours:
             calendar = user.employee_ids[0].contract_id.working_hours
             start_dt = Datetime.from_string(record_dt)
             resource_id = user.employee_ids[0].resource_id.id
             action_dt = self.pool[
                 'resource.calendar'].schedule_days_get_date(
                     cr,
                     uid,
                     calendar.id,
                     action.trg_date_range,
                     day_date=start_dt,
                     compute_leaves=True,
                     resource_id=resource_id,
                     context=context)
             return action_dt
     return super(base_action_rule, self)._check_delay(cr,
                                                       uid,
                                                       action,
                                                       record,
                                                       record_dt,
                                                       context=context)
Example #7
0
    def _compute_leave_duration(self):
        for holiday in self:
            if all((holiday.morning_start_work_time, holiday.morning_end_work_time,
                    holiday.afternoon_start_work_time, holiday.afternoon_end_work_time)):
                if not all((holiday.date_from, holiday.date_to)):
                    continue
                dt_holiday_from = Datetime.from_string(holiday.date_from) + datetime.timedelta(hours=8)
                dt_holiday_to = Datetime.from_string(holiday.date_to) + datetime.timedelta(hours=8)

                leave = datetime.timedelta()

                dt_date = dt_holiday_from.date()
                while dt_date <= dt_holiday_to.date():
                    querying_day = dt_date.strftime(DEFAULT_SERVER_DATE_FORMAT)
                    dt_the_day_morning_start_work_time = datetime.datetime.strptime(
                            "%s %s" % (querying_day, holiday.morning_start_work_time), DEFAULT_SERVER_DATETIME_FORMAT)
                    dt_the_day_morning_end_work_time = datetime.datetime.strptime(
                            "%s %s" % (querying_day, holiday.morning_end_work_time), DEFAULT_SERVER_DATETIME_FORMAT)
                    dt_the_day_afternoon_start_work_time = datetime.datetime.strptime(
                            "%s %s" % (querying_day, holiday.afternoon_start_work_time), DEFAULT_SERVER_DATETIME_FORMAT)
                    dt_the_day_afternoon_end_work_time = datetime.datetime.strptime(
                            "%s %s" % (querying_day, holiday.afternoon_end_work_time), DEFAULT_SERVER_DATETIME_FORMAT)

                    # deal with morning first
                    dt_cal_start = max(dt_the_day_morning_start_work_time, dt_holiday_from)
                    dt_cal_start = min(dt_cal_start, dt_the_day_morning_end_work_time)

                    dt_cal_end = min(dt_the_day_morning_end_work_time, dt_holiday_to)
                    dt_cal_end = max(dt_cal_end, dt_the_day_morning_start_work_time)

                    if dt_cal_end > dt_cal_start:
                        leave += dt_cal_end - dt_cal_start

                    # then deal with afternoon first
                    dt_cal_start = max(dt_the_day_afternoon_start_work_time, dt_holiday_from)
                    dt_cal_start = min(dt_cal_start, dt_the_day_afternoon_end_work_time)

                    dt_cal_end = min(dt_the_day_afternoon_end_work_time, dt_holiday_to)
                    dt_cal_end = max(dt_cal_end, dt_the_day_afternoon_start_work_time)

                    if dt_cal_end > dt_cal_start:
                        leave += dt_cal_end - dt_cal_start

                    dt_date += datetime.timedelta(days=1)

                holiday.leave_duration = leave.days * 24.0 + leave.seconds / 3600.0
Example #8
0
 def _check_session_timing(self):
     self.ensure_one()
     date_today = datetime.utcnow()
     session_start = Datetime.from_string(self.start_at)
     if not date_today - datetime.timedelta(hours=24) <= session_start:
         raise UserError(
             _("This session has been opened another day. To comply with the French law, you should close sessions on a daily basis. Please close session %s and open a new one."
               ) % self.name)
     return True
Example #9
0
    def get_work_duration_on(self, date=None):
        self.ensure_one()
        if not date:
            return None

        sign_in_attendance = self.get_sign_in_attendance(date)
        sign_out_attendance = self.get_sign_out_attendance(date)
        if sign_in_attendance and sign_out_attendance:
            dt_the_day_morning_start_work_time = datetime.datetime.strptime(
                    "%s %s" % (date, sign_in_attendance.morning_start_work_time), DEFAULT_SERVER_DATETIME_FORMAT)
            dt_the_day_morning_end_work_time = datetime.datetime.strptime(
                    "%s %s" % (date, sign_in_attendance.morning_end_work_time), DEFAULT_SERVER_DATETIME_FORMAT)
            dt_the_day_afternoon_start_work_time = datetime.datetime.strptime(
                    "%s %s" % (date, sign_out_attendance.afternoon_start_work_time), DEFAULT_SERVER_DATETIME_FORMAT)
            dt_the_day_afternoon_end_work_time = datetime.datetime.strptime(
                    "%s %s" % (date, sign_out_attendance.afternoon_end_work_time), DEFAULT_SERVER_DATETIME_FORMAT)

            dt_sign_in_time = Datetime.from_string(sign_in_attendance.name) + datetime.timedelta(hours=8)
            dt_sign_out_time = Datetime.from_string(sign_out_attendance.name) + datetime.timedelta(hours=8)

            # morning first
            dt_cal_start = max(dt_the_day_morning_start_work_time, dt_sign_in_time)
            dt_cal_start = min(dt_cal_start, dt_the_day_morning_end_work_time)

            # dt_cal_end = min(dt_the_day_morning_end_work_time, dt_sign_out_time)
            # dt_cal_end = max(dt_cal_end, dt_the_day_morning_start_work_time)
            dt_cal_end = dt_the_day_morning_end_work_time
            work_duration = datetime.timedelta()
            if dt_cal_end > dt_cal_start:
                work_duration += dt_cal_end - dt_cal_start

            # then deal with afternoon
            # dt_cal_start = max(dt_the_day_afternoon_start_work_time, dt_sign_out_time)
            # dt_cal_start = min(dt_cal_start, dt_the_day_afternoon_end_work_time)
            dt_cal_start = dt_the_day_afternoon_start_work_time

            dt_cal_end = min(dt_the_day_afternoon_end_work_time, dt_sign_out_time)
            dt_cal_end = max(dt_cal_end, dt_the_day_afternoon_start_work_time)

            if dt_cal_end > dt_cal_start:
                work_duration += dt_cal_end - dt_cal_start

            return work_duration.seconds / 3600.0
Example #10
0
def ctx_tz(record, field):
    res_lang = None
    ctx = record._context
    tz_name = pytz.timezone(ctx.get('tz') or record.env.user.tz)
    timestamp = Datetime.from_string(record[field])
    if ctx.get('lang'):
        res_lang = record.env['res.lang'].search([('code', '=', ctx['lang'])],
                                                 limit=1)
    if res_lang:
        timestamp = pytz.utc.localize(timestamp, is_dst=False)
        return datetime.strftime(
            timestamp.astimezone(tz_name),
            res_lang.date_format + ' ' + res_lang.time_format)
    return Datetime.context_timestamp(record, timestamp)
Example #11
0
 def _check_delay(self, cr, uid, action, record, record_dt, context=None):
     """ Override the check of delay to try to use a user-related calendar.
     If no calendar is found, fallback on the default behavior. """
     if action.trg_date_calendar_id and action.trg_date_range_type == 'day' and action.trg_date_resource_field_id:
         user = record[action.trg_date_resource_field_id.name]
         if user.employee_ids and user.employee_ids[0].contract_id \
                 and user.employee_ids[0].contract_id.working_hours:
             calendar = user.employee_ids[0].contract_id.working_hours
             start_dt = Datetime.from_string(record_dt)
             resource_id = user.employee_ids[0].resource_id.id
             action_dt = self.pool['resource.calendar'].schedule_days_get_date(
                 cr, uid, calendar.id, action.trg_date_range,
                 day_date=start_dt, compute_leaves=True, resource_id=resource_id,
                 context=context
             )
             return action_dt
     return super(base_action_rule, self)._check_delay(cr, uid, action, record, record_dt, context=context)
Example #12
0
 def time_ago(from_):
     if from_ is None:
         from_ = Datetime.now()
     return human(Datetime.from_string(from_), 1)
Example #13
0
 def time_ago(from_):
     if from_ is None:
         from_ = Datetime.now()
     return human(Datetime.from_string(from_), 1)
Example #14
0
    def get_holiday_on(self, date=None):
        self.ensure_one()
        if not date:
            return None

        querying_day = date
        if isinstance(date, datetime.date):
            querying_day = Date.to_string(date)

        leaves = defaultdict(lambda: list())  # [None, None, datetime.timedelta()] _time = datetime.timedelta()

        absent_type_id = self.env.ref("hr_sf.absent_holidays_status").id

        for holiday in self.holidays_ids:
            if not holiday.date_from or not holiday.date_to:
                continue
            if holiday.holiday_status_id.id == absent_type_id:
                continue

            if not all((holiday.morning_start_work_time,
                        holiday.morning_end_work_time,
                        holiday.afternoon_start_work_time,
                        holiday.afternoon_end_work_time)):
                return None

            dt_the_day_morning_start_work_time = datetime.datetime.strptime(
                    "%s %s" % (querying_day, holiday.morning_start_work_time), DEFAULT_SERVER_DATETIME_FORMAT)
            dt_the_day_morning_end_work_time = datetime.datetime.strptime(
                    "%s %s" % (querying_day, holiday.morning_end_work_time), DEFAULT_SERVER_DATETIME_FORMAT)
            dt_the_day_afternoon_start_work_time = datetime.datetime.strptime(
                    "%s %s" % (querying_day, holiday.afternoon_start_work_time), DEFAULT_SERVER_DATETIME_FORMAT)
            dt_the_day_afternoon_end_work_time = datetime.datetime.strptime(
                    "%s %s" % (querying_day, holiday.afternoon_end_work_time), DEFAULT_SERVER_DATETIME_FORMAT)

            dt_holiday_from = Datetime.from_string(holiday.date_from) + datetime.timedelta(hours=8)
            dt_holiday_to = Datetime.from_string(holiday.date_to) + datetime.timedelta(hours=8)
            # deal with morning first
            dt_cal_start = max(dt_the_day_morning_start_work_time, dt_holiday_from)
            dt_cal_start = min(dt_cal_start, dt_the_day_morning_end_work_time)

            dt_cal_end = min(dt_the_day_morning_end_work_time, dt_holiday_to)
            dt_cal_end = max(dt_cal_end, dt_the_day_morning_start_work_time)

            if dt_cal_end > dt_cal_start:
                leaves[holiday.holiday_status_id.name].append((dt_cal_start, dt_cal_end, dt_cal_end - dt_cal_start))
                # leaves[holiday.holiday_status_id.name][0] = dt_cal_start
                # leaves[holiday.holiday_status_id.name][1] = dt_cal_end
                # leaves[holiday.holiday_status_id.name][2] += dt_cal_end - dt_cal_start

            # then deal with afternoon first
            dt_cal_start = max(dt_the_day_afternoon_start_work_time, dt_holiday_from)
            dt_cal_start = min(dt_cal_start, dt_the_day_afternoon_end_work_time)

            dt_cal_end = min(dt_the_day_afternoon_end_work_time, dt_holiday_to)
            dt_cal_end = max(dt_cal_end, dt_the_day_afternoon_start_work_time)

            if dt_cal_end > dt_cal_start:
                leaves[holiday.holiday_status_id.name].append((dt_cal_start, dt_cal_end, dt_cal_end - dt_cal_start))
                # leaves[holiday.holiday_status_id.name][0] = dt_cal_start
                # leaves[holiday.holiday_status_id.name][1] = dt_cal_end
                # leaves[holiday.holiday_status_id.name][2] += dt_cal_end - dt_cal_start

        return leaves
Example #15
0
def UTC_Datetime_To_TW_TZ(timestr):
    if not timestr:
        return None

    return Datetime.context_timestamp(TimeZoneHelper_TW2, Datetime.from_string(timestr))
Example #16
0
    def get_holiday_on(self, date=None):
        self.ensure_one()
        if not date:
            return None

        querying_day = date
        if isinstance(date, datetime.date):
            querying_day = Date.to_string(date)

        leaves = defaultdict(lambda: list(
        ))  # [None, None, datetime.timedelta()] _time = datetime.timedelta()

        absent_type_id = self.env.ref("hr_sf.absent_holidays_status").id

        for holiday in self.holidays_ids:
            if not holiday.date_from or not holiday.date_to:
                continue
            if holiday.holiday_status_id.id == absent_type_id:
                continue

            if not all((holiday.morning_start_work_time,
                        holiday.morning_end_work_time,
                        holiday.afternoon_start_work_time,
                        holiday.afternoon_end_work_time)):
                return None

            dt_the_day_morning_start_work_time = datetime.datetime.strptime(
                "%s %s" % (querying_day, holiday.morning_start_work_time),
                DEFAULT_SERVER_DATETIME_FORMAT)
            dt_the_day_morning_end_work_time = datetime.datetime.strptime(
                "%s %s" % (querying_day, holiday.morning_end_work_time),
                DEFAULT_SERVER_DATETIME_FORMAT)
            dt_the_day_afternoon_start_work_time = datetime.datetime.strptime(
                "%s %s" % (querying_day, holiday.afternoon_start_work_time),
                DEFAULT_SERVER_DATETIME_FORMAT)
            dt_the_day_afternoon_end_work_time = datetime.datetime.strptime(
                "%s %s" % (querying_day, holiday.afternoon_end_work_time),
                DEFAULT_SERVER_DATETIME_FORMAT)

            dt_holiday_from = Datetime.from_string(
                holiday.date_from) + datetime.timedelta(hours=8)
            dt_holiday_to = Datetime.from_string(
                holiday.date_to) + datetime.timedelta(hours=8)
            # deal with morning first
            dt_cal_start = max(dt_the_day_morning_start_work_time,
                               dt_holiday_from)
            dt_cal_start = min(dt_cal_start, dt_the_day_morning_end_work_time)

            dt_cal_end = min(dt_the_day_morning_end_work_time, dt_holiday_to)
            dt_cal_end = max(dt_cal_end, dt_the_day_morning_start_work_time)

            if dt_cal_end > dt_cal_start:
                leaves[holiday.holiday_status_id.name].append(
                    (dt_cal_start, dt_cal_end, dt_cal_end - dt_cal_start))
                # leaves[holiday.holiday_status_id.name][0] = dt_cal_start
                # leaves[holiday.holiday_status_id.name][1] = dt_cal_end
                # leaves[holiday.holiday_status_id.name][2] += dt_cal_end - dt_cal_start

            # then deal with afternoon first
            dt_cal_start = max(dt_the_day_afternoon_start_work_time,
                               dt_holiday_from)
            dt_cal_start = min(dt_cal_start,
                               dt_the_day_afternoon_end_work_time)

            dt_cal_end = min(dt_the_day_afternoon_end_work_time, dt_holiday_to)
            dt_cal_end = max(dt_cal_end, dt_the_day_afternoon_start_work_time)

            if dt_cal_end > dt_cal_start:
                leaves[holiday.holiday_status_id.name].append(
                    (dt_cal_start, dt_cal_end, dt_cal_end - dt_cal_start))
                # leaves[holiday.holiday_status_id.name][0] = dt_cal_start
                # leaves[holiday.holiday_status_id.name][1] = dt_cal_end
                # leaves[holiday.holiday_status_id.name][2] += dt_cal_end - dt_cal_start

        return leaves
Example #17
0
    def _compute_leave_duration(self):
        for holiday in self:
            if all((holiday.morning_start_work_time,
                    holiday.morning_end_work_time,
                    holiday.afternoon_start_work_time,
                    holiday.afternoon_end_work_time)):
                if not all((holiday.date_from, holiday.date_to)):
                    continue
                dt_holiday_from = Datetime.from_string(
                    holiday.date_from) + datetime.timedelta(hours=8)
                dt_holiday_to = Datetime.from_string(
                    holiday.date_to) + datetime.timedelta(hours=8)

                leave = datetime.timedelta()

                dt_date = dt_holiday_from.date()
                while dt_date <= dt_holiday_to.date():
                    querying_day = dt_date.strftime(DEFAULT_SERVER_DATE_FORMAT)
                    dt_the_day_morning_start_work_time = datetime.datetime.strptime(
                        "%s %s" %
                        (querying_day, holiday.morning_start_work_time),
                        DEFAULT_SERVER_DATETIME_FORMAT)
                    dt_the_day_morning_end_work_time = datetime.datetime.strptime(
                        "%s %s" %
                        (querying_day, holiday.morning_end_work_time),
                        DEFAULT_SERVER_DATETIME_FORMAT)
                    dt_the_day_afternoon_start_work_time = datetime.datetime.strptime(
                        "%s %s" %
                        (querying_day, holiday.afternoon_start_work_time),
                        DEFAULT_SERVER_DATETIME_FORMAT)
                    dt_the_day_afternoon_end_work_time = datetime.datetime.strptime(
                        "%s %s" %
                        (querying_day, holiday.afternoon_end_work_time),
                        DEFAULT_SERVER_DATETIME_FORMAT)

                    # deal with morning first
                    dt_cal_start = max(dt_the_day_morning_start_work_time,
                                       dt_holiday_from)
                    dt_cal_start = min(dt_cal_start,
                                       dt_the_day_morning_end_work_time)

                    dt_cal_end = min(dt_the_day_morning_end_work_time,
                                     dt_holiday_to)
                    dt_cal_end = max(dt_cal_end,
                                     dt_the_day_morning_start_work_time)

                    if dt_cal_end > dt_cal_start:
                        leave += dt_cal_end - dt_cal_start

                    # then deal with afternoon first
                    dt_cal_start = max(dt_the_day_afternoon_start_work_time,
                                       dt_holiday_from)
                    dt_cal_start = min(dt_cal_start,
                                       dt_the_day_afternoon_end_work_time)

                    dt_cal_end = min(dt_the_day_afternoon_end_work_time,
                                     dt_holiday_to)
                    dt_cal_end = max(dt_cal_end,
                                     dt_the_day_afternoon_start_work_time)

                    if dt_cal_end > dt_cal_start:
                        leave += dt_cal_end - dt_cal_start

                    dt_date += datetime.timedelta(days=1)

                holiday.leave_duration = leave.days * 24.0 + leave.seconds / 3600.0
Example #18
0
    def get_absent_on(self, date=None):
        self.ensure_one()
        if not date:
            return None

        querying_day = date
        if isinstance(date, datetime.date):
            querying_day = Date.to_string(date)

        absent_type_id = self.env.ref("hr_sf.absent_holidays_status").id

        for holiday in self.holidays_ids:
            if not holiday.date_from or not holiday.date_to:
                continue
            if holiday.holiday_status_id.id != absent_type_id:
                continue

            if not all((holiday.morning_start_work_time,
                        holiday.morning_end_work_time,
                        holiday.afternoon_start_work_time,
                        holiday.afternoon_end_work_time)):
                return None

            dt_the_day_morning_start_work_time = datetime.datetime.strptime(
                "%s %s" % (querying_day, holiday.morning_start_work_time),
                DEFAULT_SERVER_DATETIME_FORMAT)
            dt_the_day_morning_end_work_time = datetime.datetime.strptime(
                "%s %s" % (querying_day, holiday.morning_end_work_time),
                DEFAULT_SERVER_DATETIME_FORMAT)
            dt_the_day_afternoon_start_work_time = datetime.datetime.strptime(
                "%s %s" % (querying_day, holiday.afternoon_start_work_time),
                DEFAULT_SERVER_DATETIME_FORMAT)
            dt_the_day_afternoon_end_work_time = datetime.datetime.strptime(
                "%s %s" % (querying_day, holiday.afternoon_end_work_time),
                DEFAULT_SERVER_DATETIME_FORMAT)

            dt_holiday_from = Datetime.from_string(
                holiday.date_from) + datetime.timedelta(hours=8)
            dt_holiday_to = Datetime.from_string(
                holiday.date_to) + datetime.timedelta(hours=8)

            # deal with morning first
            dt_cal_start = max(dt_the_day_morning_start_work_time,
                               dt_holiday_from)
            dt_cal_start = min(dt_cal_start, dt_the_day_morning_end_work_time)

            dt_cal_end = min(dt_the_day_morning_end_work_time, dt_holiday_to)
            dt_cal_end = max(dt_cal_end, dt_the_day_morning_start_work_time)

            if dt_cal_end > dt_cal_start:
                return 1

            # then deal with afternoon first
            dt_cal_start = max(dt_the_day_afternoon_start_work_time,
                               dt_holiday_from)
            dt_cal_start = min(dt_cal_start,
                               dt_the_day_afternoon_end_work_time)

            dt_cal_end = min(dt_the_day_afternoon_end_work_time, dt_holiday_to)
            dt_cal_end = max(dt_cal_end, dt_the_day_afternoon_start_work_time)

            if dt_cal_end > dt_cal_start:
                return 1
Example #19
0
def UTC_Datetime_To_TW_TZ(timestr):
    if not timestr:
        return None

    return Datetime.context_timestamp(TimeZoneHelper_TW2,
                                      Datetime.from_string(timestr))