コード例 #1
0
 def check(self, db, uid, passwd):
     super(res_users, self).check(db, uid, passwd)
     lastLogKey = (db, uid)
     lastLog = self._uid_lastlog.get(lastLogKey)
     curTime = datetime.now()
     logAccess = not lastLog or (curTime - lastLog).seconds >= 600         
     if logAccess:
         url = request.httprequest.url
         self._uid_lastlog[lastLogKey] = curTime
         cr =  self.pool.cursor()
         try:
             self.pool.get("res.user.log").create(cr, uid, {"user_id" : uid, 
                                                            "date" :  util.timeToStr(curTime), 
                                                            "name" : url } )
             cr.commit()
         finally:
             cr.rollback()                
             cr.close()
コード例 #2
0
    def _get_date_planned(self,
                          cr,
                          uid,
                          order,
                          line,
                          start_date,
                          context=None):
        start_date = util.strToTime(start_date)
        company = order.company_id
        date_planned = None

        real_delay = (line.delay or 0.0) - company.security_lead
        if real_delay:
            if company.calendar_id:
                date_planned = self.pool.get('resource.calendar').passed_range(
                    cr, uid, company.calendar_id.id, start_date,
                    real_delay)["to"]
            else:
                date_planned = start_date + relativedelta(days=real_delay)
        if date_planned:
            date_planned = util.timeToStr(date_planned)
        else:
            return util.currentDate()
        return date_planned
コード例 #3
0
ファイル: hr_timesheet.py プロジェクト: suningwz/fdoo
    def get_timesheet_data(self, cr, uid, oid, context=None):
        #inner function for get lines
        def _get_attendances(in_days, in_day):
            data = in_days.get(in_day, None)
            if data == None:
                data = {}
                in_days[in_day] = data

            attendance = data.get("attendances", None)
            if attendance == None:
                attendance = []
                in_days[in_day]["attendances"] = attendance

            return attendance

        timesheet = self.browse(cr, uid, oid, context)
        attendance_obj = self.pool.get("hr.attendance")
        attendance_ids = attendance_obj.search(
            cr, uid, [("sheet_id", "=", timesheet.id)], order="name asc")

        next_range = {"day": None, "from": None, "to": None}

        days = {}
        for att in attendance_obj.browse(cr, uid, attendance_ids, context):
            cur_day = util.timeToDateStr(att.name)
            if att.action == "sign_in":
                next_range = {
                    "day": cur_day,
                    "from": helper.strToLocalTimeStr(cr, uid, att.name,
                                                     context),
                    "to": None
                }
                _get_attendances(days, cur_day).append(next_range)
            elif att.action == "sign_out":
                if next_range["day"] != cur_day:
                    next_range = {
                        "day": cur_day,
                        "from": None,
                        "to": helper.strToLocalTimeStr(cr, uid, att.name,
                                                       context)
                    }
                    _get_attendances(days, cur_day).append(next_range)
                else:
                    next_range["to"] = helper.strToLocalTimeStr(
                        cr, uid, att.name, context)

        leaves = self.get_leaves(cr, uid, oid, context)
        date_from = util.strToDate(timesheet.date_from)
        date_to = util.strToDate(timesheet.date_to)
        date_cur = date_from
        delta_day = relativedelta(days=1)
        range_open = False

        while date_cur <= date_to:
            date_cur_str = util.dateToStr(date_cur)
            attendances = _get_attendances(days, date_cur_str)

            days[date_cur_str]['total_attendance_day'] = 0.0
            days[date_cur_str]['total_timesheet_day'] = 0.0
            days[date_cur_str]['total_difference_day'] = 0.0

            leaves_for_day = leaves.get(date_cur_str)
            if leaves_for_day:
                days[date_cur_str]["leaves"] = leaves_for_day

            if not attendances and range_open:
                attendances.append({
                    "day": date_cur_str,
                    "from": date_cur_str + " 00:00:00",
                    "to": date_cur_str + " 24:00:00"
                })
            elif attendances:
                if range_open:
                    attendances[0]["from"] = date_cur_str + " 00:00:00"
                    range_open = False
                last_range = attendances[-1]
                if not last_range.get("to"):
                    range_open = True
                    last_range["to"] = util.timeToStr(date_cur + delta_day)
            date_cur += delta_day

        #get total days
        cr.execute(
            'SELECT day.name, day.total_attendance, day.total_timesheet, day.total_difference\
                FROM hr_timesheet_sheet_sheet AS sheet \
                LEFT JOIN hr_timesheet_sheet_sheet_day AS day \
                    ON (sheet.id = day.sheet_id \
                        AND day.name IN %s) \
                WHERE sheet.id = %s', (tuple(days.keys()), oid))

        for total_day in cr.fetchall():
            if total_day[0]:
                days[total_day[0]]['total_attendance_day'] = total_day[1]
                days[total_day[0]]['total_timesheet_day'] = total_day[2]
                days[total_day[0]]['total_difference_day'] = total_day[3]

        #get analytic lines
        line_obj = self.pool.get("hr.analytic.timesheet")
        for key, value in days.items():
            value["lines"] = line_obj.search(cr,
                                             uid,
                                             [("date", "=", key),
                                              ("sheet_id", "=", timesheet.id)],
                                             order="id asc")

        return days
コード例 #4
0
    def barkeeper_status(self, cr, uid, options, context=None):
        order_obj = self.pool["pos.order"]
        session_obj = self.pool["pos.session"]
        config_obj = self.pool["pos.config"]
        company_obj = self.pool["res.company"]

        company_id = company_obj._company_default_get(cr, uid, context=context)
        company = company_obj.browse(cr, uid, company_id, context=context)
        currency = company.currency_id.symbol

        # result
        stat = {
            "title": _("No Data"),
            "currency": currency,
            "name": "",
            "group": "",
            "range": ""
        }

        # search first order if
        # and build default options
        # if no options are passed
        config_id = None
        config = None
        date = None
        mode = "today"
        if options:
            config_id = options.get("config_id")
            # if mode is today
            # date is none
            mode = options.get("mode")
            if not mode or mode == "today":
                date = None
                options["mode"] = "today"
            else:
                date = options.get("date")

        nodata = False
        if not config_id or not date:
            # get latest order
            order_id = None
            if config_id:
                config = config_obj.browse(cr, uid, config_id, context=context)
                order_id = order_obj.search_id(
                    cr,
                    uid, [
                        '|', ("session_id.config_id", "=", config_id),
                        ('session_id.config_id.parent_user_id', '=',
                         config.user_id.id)
                    ],
                    order="date_order desc",
                    context=None)
            else:
                order_id = order_obj.search_id(cr,
                                               uid,
                                               [("session_id", "!=", False)],
                                               order="date_order desc",
                                               context=None)

            # check if order exist
            if order_id:
                order = order_obj.browse(cr, uid, order_id, context=context)

                # get parent
                config = order.session_id.config_id
                while config.parent_user_id:
                    parent_config_id = config_obj.search_id(
                        cr,
                        uid, [("user_id", "=", config.parent_user_id.id)],
                        context=context)
                    if not parent_config_id or parent_config_id == config.id:
                        break
                    config = config_obj.browse(cr,
                                               uid,
                                               parent_config_id,
                                               context=context)

                config_id = config.id
                options = {
                    "mode": "today",
                    "date": util.timeToDateStr(order.date_order),
                    "config_id": config_id
                }
            else:
                nodata = True

        # set options
        stat["options"] = options

        # if not data return
        if not config_id:
            return stat

        # get current mode
        if not config or config.id != config_id:
            config = config_obj.browse(cr, uid, config_id, context=context)
        config_ids = [config_id]

        # build title and group description
        stat["name"] = config.name
        if config.user_id:
            group = []
            childEntries = config_obj.search_read(
                cr,
                uid, [("parent_user_id", "=", config.user_id.id)],
                ["name", "sign_pid"],
                context=context)
            for childEntry in childEntries:
                group.append(childEntry["sign_pid"] or childEntry["name"])
                config_ids.append(childEntry["id"])
            if group:
                group.insert(0, config.sign_pid or config.name)
            stat["group"] = ", ".join(group)

        # return if no default data
        if nodata:
            return stat

        # find start
        statDate = options.get("date")
        if not statDate:
            options["date"] = statDate = util.currentDate()

        # add amount
        orderDicts = []

        def addAmount(amount, amountStat, sections, count=False):
            for section in sections:

                subsections = None
                sectionStat = None

                if len(section) == 1:
                    key = section[0]
                    sectionStat = amountStat.get(key)
                    if sectionStat is None:
                        sectionStat = {
                            "amount": 0.0,
                            "count": 0,
                            "currency": currency
                        }
                        amountStat[key] = sectionStat

                elif len(section) >= 2:
                    field = section[0]
                    key = section[1]

                    if len(section) >= 3:
                        subsections = section[2]

                    keyStat = amountStat.get(field)
                    if keyStat is None:
                        keyStat = {}
                        amountStat[field] = keyStat
                        orderDicts.append((amountStat, field, keyStat))

                    sectionStat = keyStat.get(key)
                    if sectionStat is None:
                        sectionStat = {
                            "key": key,
                            "amount": 0.0,
                            "count": 0,
                            "currency": currency
                        }
                        keyStat[key] = sectionStat

                sectionStat["amount"] = sectionStat["amount"] + amount
                if count:
                    sectionStat["count"] = sectionStat["count"] + 1

                if subsections:
                    addAmount(amount, sectionStat, subsections, count=count)

        # setup range
        time_keyfunc = lambda date_order: helper.strToLocalDateStr(
            cr, uid, date_order, context=context)
        if mode == "year":
            stat["title"] = _("Year")
            startDate = util.getFirstOfYear(statDate)
            endDate = util.getFirstOfNextYear(statDate)
            dt_delta = relativedelta(years=1)
            time_keyfunc = lambda date_order: helper.strToLocalDateStr(
                cr, uid, date_order, context=context)[:7]
        elif mode == "month":
            stat["title"] = _("Month")
            startDate = util.getFirstOfMonth(statDate)
            endDate = util.getFirstOfNextMonth(statDate)
            dt_delta = relativedelta(months=1)
        elif mode == "week":
            stat["title"] = _("Week")
            startDate = util.getFirstOfWeek(statDate)
            endDate = util.getFirstOfNextWeek(statDate)
            dt_delta = relativedelta(weeks=1)
        else:
            time_keyfunc = lambda date_order: "%s:00" % helper.strToLocalTimeStr(
                cr, uid, date_order, context=context).split(" ")[1][:2]
            stat["title"] = _("Day")
            startDate = statDate
            endDate = util.getNextDayDate(statDate)
            dt_delta = relativedelta(hours=1)

        # query session build range
        session_ids = session_obj.search(cr,
                                         uid,
                                         [("start_at", ">=", startDate),
                                          ("start_at", "<", endDate),
                                          ("config_id", "in", config_ids)],
                                         order="start_at asc",
                                         context=context)
        stat["range"] = helper.getRangeName(cr,
                                            uid,
                                            startDate,
                                            util.getPrevDayDate(endDate),
                                            context=context)

        # query orders
        order_ids = order_obj.search(cr,
                                     uid, [("session_id", "in", session_ids)],
                                     order="date_order asc",
                                     context=context)
        orders = order_obj.browse(cr, uid, order_ids, context=context)

        # build base
        addAmount(0, stat, [("total", )])
        if orders:
            firstOrder = orders[0]
            lastOrder = orders[-1]

            curTime = util.strToTime(firstOrder.date_order)
            timeEnd = util.strToTime(lastOrder.date_order)
            while curTime <= timeEnd:
                time_key = time_keyfunc(util.timeToStr(curTime))
                addAmount(0, stat, [("byTime", time_key)])
                curTime += dt_delta

        # iterate orders
        for order in orders:
            # check if it is relevant
            fpos_order = order.fpos_order_id
            notbalance = fpos_order.tag != "s"
            user = order.user_id

            # add turnover for journal
            for st in order.statement_ids:
                journal = st.journal_id
                amount = notbalance and st.amount or 0.0
                addAmount(
                    amount, stat,
                    [("byJournal", journal.name),
                     ("byUser", user.name, [("byJournal", journal.name)])],
                    True)

            # add to order
            time_key = time_keyfunc(order.date_order)
            amount = notbalance and order.amount_total or 0.0
            addAmount(amount, stat,
                      [("total", ),
                       ("byTime", time_key, [("byUser", user.name)])], True)

        # sort dicts
        for (parent, key, value) in orderDicts:
            parent[key] = value and sorted(value.values(),
                                           key=lambda en: en["key"]) or []

        return stat
コード例 #5
0
ファイル: official_holiday.py プロジェクト: suningwz/fdoo
    def create_calendar_entries(self,
                                cr,
                                uid,
                                ids,
                                fiscalyear_id,
                                company_id=None,
                                calendar_id=None,
                                resource_id=None,
                                context=None):
        leave_obj = self.pool.get("resource.calendar.leaves")
        event_obj = self.pool.get("calendar.event")
        account_fiscalyear_obj = self.pool.get("account.fiscalyear")
        fiscal_year_date = util.strToDate(
            account_fiscalyear_obj.browse(cr, uid, fiscalyear_id,
                                          context).date_start)
        easter_sunday_date = util.dateEasterSunday(fiscal_year_date.year)

        for holiday in self.browse(cr, uid, ids, context):
            holiday_date = util.strToDate(holiday.date)
            if holiday.calc_type == "none":
                holiday_date = holiday_date.replace(year=fiscal_year_date.year)
            elif holiday.calc_type == "easter":
                holiday_easter_sunday_date = util.dateEasterSunday(
                    holiday_date.year)
                easter_delta = holiday_easter_sunday_date - easter_sunday_date
                holiday_date = holiday_date + abs(easter_delta)
            leave_ids = leave_obj.search(
                cr, uid, [("official_holiday_id", "=", holiday.id),
                          ("calendar_id", "=", calendar_id),
                          ("date_from", "=", util.timeToStr(holiday_date))])
            if not leave_ids:
                leave_values = {
                    "name": holiday.name,
                    "date_from": util.timeToStr(holiday_date),
                    "date_to":
                    util.timeToStr(util.getLastTimeOfDay(holiday_date)),
                    "resource_id": resource_id,
                    "company_id": company_id,
                    "calendar_id": calendar_id,
                    "official_holiday_id": holiday.id
                }
                leave_obj.create(cr, uid, leave_values, context=context)
            event_ids = event_obj.search(
                cr, uid, [("official_holiday_id", "=", holiday.id)])
            if not event_ids:
                event_values_start = event_obj.onchange_dates(
                    cr,
                    uid, [],
                    fromtype="start",
                    start=util.timeToDateStr(holiday_date),
                    checkallday=True,
                    allday=True,
                    context=context)
                event_values_end = event_obj.onchange_dates(
                    cr,
                    uid, [],
                    fromtype="stop",
                    end=util.timeToDateStr(
                        util.getLastTimeOfDay(holiday_date)),
                    checkallday=True,
                    allday=True,
                    context=context)
                if event_values_start and event_values_end:
                    event_values = event_values_start["value"]
                    event_values.update(event_values_end["value"])
                    event_values["name"] = holiday.name
                    event_values["class"] = "public"
                    event_values["user_id"] = None
                    event_values["show_as"] = "busy"
                    event_values["official_holiday_id"] = holiday.id
                    event_values["partner_ids"] = None
                    event_obj.create(cr, uid, event_values, context=context)
コード例 #6
0
ファイル: employee.py プロジェクト: suningwz/fdoo
    def attendance_action_change(self, cr, uid, ids, context=None):
        if not context:
            context = {}

        action = context.get('action', False)
        attendance_obj = self.pool.get("hr.attendance")

        for employee in self.browse(cr, uid, ids, context):
            #
            if not action:
                if employee.state == "present": action = "sign_out"
                if employee.state == "absent": action = "sign_in"
            #
            if action == "sign_out":
                contract = employee.contract_id
                if contract:
                    break_interval = contract.break_interval
                    break_duration = contract.break_duration
                    if break_interval and break_duration and contract.break_auto:
                        attendance_ids = attendance_obj.search(
                            cr,
                            uid, [("employee_id", "=", employee.id)],
                            limit=1)
                        if attendance_ids:
                            last_att = attendance_obj.browse(
                                cr, uid, attendance_ids[0], context)
                            until_time = util.strToTime(
                                context.get("default_name",
                                            time.strftime(util.DHM_FORMAT)))
                            if last_att.action == "sign_in":
                                from_time = util.strToTime(last_att.name)
                                break_interval_delta = relativedelta(
                                    hours=break_interval)
                                break_duration_delta = relativedelta(
                                    hours=break_duration)
                                next_break = from_time + break_interval_delta
                                while next_break < until_time:
                                    att_id = attendance_obj.create(
                                        cr, uid, {
                                            "name": util.timeToStr(next_break),
                                            "action": "sign_out",
                                            "employee_id": employee.id
                                        }, context)

                                    # check if after break sign in would be after sign out
                                    next_break += break_duration_delta
                                    if next_break >= until_time:
                                        return att_id

                                    att_id = attendance_obj.create(
                                        cr, uid, {
                                            "name": util.timeToStr(next_break),
                                            "action": "sign_in",
                                            "employee_id": employee.id
                                        }, context)
                                    next_break += break_interval_delta

        att_id = super(hr_employee,
                       self).attendance_action_change(cr,
                                                      uid,
                                                      ids,
                                                      context=context)
        return att_id