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 _interval_dates(self, frequency, company): """ Method used to compute the theoretical date from which account move lines should be fetched @param {string} frequency: a valid value of the selection field on the object (daily, monthly, annually) frequencies are literal (daily means 24 hours and so on) @param {recordset} company: the company for which the closing is done @return {dict} the theoretical date from which account move lines are fetched. date_stop date to which the move lines are fetched, always now() the dates are in their Odoo Database string representation """ date_stop = datetime.utcnow() interval_from = None name_interval = '' if frequency == 'daily': interval_from = date_stop - timedelta(days=1) name_interval = _('Daily Closing') elif frequency == 'monthly': month_target = date_stop.month > 1 and date_stop.month - 1 or 12 year_target = month_target < 12 and date_stop.year or date_stop.year - 1 interval_from = date_stop.replace(year=year_target, month=month_target) name_interval = _('Monthly Closing') elif frequency == 'annually': year_target = date_stop.year - 1 interval_from = date_stop.replace(year=year_target) name_interval = _('Annual Closing') return { 'interval_from': FieldDateTime.to_string(interval_from), 'date_stop': FieldDateTime.to_string(date_stop), 'name_interval': name_interval }
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)
def setUpClass(cls): super(TestRmaPurchase, cls).setUpClass() cls.rma_obj = cls.env['rma.order'] cls.rma_line_obj = cls.env['rma.order.line'] cls.rma_op_obj = cls.env['rma.operation'] cls.rma_add_purchase_wiz = cls.env['rma_add_purchase'] cls.po_obj = cls.env['purchase.order'] cls.pol_obj = cls.env['purchase.order.line'] cls.product_obj = cls.env['product.product'] cls.partner_obj = cls.env['res.partner'] cls.rma_route_cust = cls.env.ref('rma.route_rma_customer') # Create supplier supplier1 = cls.partner_obj.create({'name': 'Supplier 1'}) # Create products cls.product_1 = cls.product_obj.create({ 'name': 'Test Product 1', 'type': 'product', }) cls.product_2 = cls.product_obj.create({ 'name': 'Test Product 2', 'type': 'product', }) # Create PO: cls.po = cls.po_obj.create({ 'partner_id': supplier1.id, }) cls.pol_1 = cls.pol_obj.create({ 'name': cls.product_1.name, 'order_id': cls.po.id, 'product_id': cls.product_1.id, 'product_qty': 20.0, 'product_uom': cls.product_1.uom_id.id, 'price_unit': 100.0, 'date_planned': Datetime.now(), }) cls.pol_2 = cls.pol_obj.create({ 'name': cls.product_2.name, 'order_id': cls.po.id, 'product_id': cls.product_2.id, 'product_qty': 18.0, 'product_uom': cls.product_2.uom_id.id, 'price_unit': 150.0, 'date_planned': Datetime.now(), }) # Create RMA group: cls.rma_group = cls.rma_obj.create({ 'partner_id': supplier1.id, 'type': 'supplier', })
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
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
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)
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)
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
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
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
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)
def reformat_server_datetime_for_frontend(cls, date_time, date_first=False, two_character_year=False, context_with_timezone=None): """ Reformat a datetime in Odoo's 'default server datetime format' (see imports) to one more appropriate for the front end. Can choose whether the date or time comes first and optionally convert to client timezone. :param date_time: :type date_time: str :param date_first: :type date_first: bool :param two_character_year: :type two_character_year: bool :param context_with_timezone: A record's context with a 'tz' key specifying the timezone of the current client. :type context_with_timezone: dict :return: :rtype: str """ date_time = cls.zero_seconds(date_time) date_time = datetime.strptime(date_time, DTF) time_format = cls.time_format_front_end date_format = cls.date_format_front_end if two_character_year: date_format = cls.date_format_front_end_two_character_year if date_first: datetime_format = cls.format_string.format(date_format, time_format) else: datetime_format = cls.format_string.format(time_format, date_format) if context_with_timezone: cls._context = context_with_timezone date_time = Datetime.context_timestamp(cls, date_time) date_time = date_time.strftime(datetime_format) return date_time
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
def attendance_per_location(self, date=None, location=None): if not date: date = Date.today() dt_from = datetime.datetime.strptime("%s 00:00:00" % date, DEFAULT_SERVER_DATETIME_FORMAT) - datetime.timedelta( hours=8) dt_to = datetime.datetime.strptime("%s 23:59:59" % date, DEFAULT_SERVER_DATETIME_FORMAT) - datetime.timedelta( hours=8) if not location: location = "1" Attendance = request.env["hr.attendance"].sudo() Employee = request.env["hr.employee"].sudo() values = {} domain = [] if date: domain.append(("name", ">=", Datetime.to_string(dt_from))) domain.append(("name", "<=", Datetime.to_string(dt_to))) # if location: # domain.append(("location", "=", location)) all_employees = Employee.search([]) emp_attendances_values = [] for emp in all_employees: attendance = dict() attendance["name"] = emp.name attendance["dep"] = emp.department_id.name records = Attendance.search(domain + [("employee_id", "=", emp.id)], order="name") if records and records[-1].location: latest_rec = records[-1] attendance["state"] = "打卡" dt = UTC_Datetime_To_TW_TZ(latest_rec.name) date_part = dt.strftime(DEFAULT_SERVER_DATE_FORMAT) time_part = dt.strftime(DEFAULT_SERVER_TIME_FORMAT) attendance["date"] = date_part attendance["time"] = time_part attendance["location"] = latest_rec.location else: attendance["state"] = "未打卡" attendance["date"] = None attendance["time"] = None attendance["location"] = None leave_time = emp.get_holiday_on(date) attendance["leave"] = string.join(leave_time.keys(), ",") if leave_time else None emp_attendances_values.append(attendance) # attendance_grouped_by_location = itertools.groupby(emp_attendances_values, key=lambda a: a["location"]) attendances = defaultdict(lambda: list()) for attendance in emp_attendances_values: attendances[attendance["location"]].append(attendance) # or _("not attended") print_time = UTC_Datetime_To_TW_TZ(Datetime.now()) values["print_time"] = Datetime.to_string(print_time) values["date"] = date values["location"] = location values["emp_attendances"] = attendances keys = sorted(attendances.keys()) if None in keys: keys.remove(None) keys.append(None) values["attendance_keys"] = keys values["action_count"] = len(filter(lambda a: a.get("date", None), emp_attendances_values)) values["un_action_count"] = len(filter(lambda a: not a.get("date", None), emp_attendances_values)) return request.render("hr_sf.attendance_per_location", values)
def setUp(self): super(TestPurchaseOpenQty, self).setUp() self.purchase_order_model = self.env['purchase.order'] purchase_order_line_model = self.env['purchase.order.line'] partner_model = self.env['res.partner'] prod_model = self.env['product.product'] analytic_account_model = self.env['account.analytic.account'] self.product_uom_model = self.env['product.uom'] # partners pa_dict = { 'name': 'Partner 1', 'supplier': True, } self.partner = partner_model.sudo().create(pa_dict) pa_dict2 = { 'name': 'Partner 2', 'supplier': True, } self.partner2 = partner_model.sudo().create(pa_dict2) # account ac_dict = { 'name': 'analytic account 1', } self.analytic_account_1 = \ analytic_account_model.sudo().create(ac_dict) # Purchase Order Num 1 po_dict = { 'partner_id': self.partner.id, } self.purchase_order_1 = self.purchase_order_model.create(po_dict) uom_id = self.product_uom_model.search([ ('name', '=', 'Unit(s)')])[0].id pr_dict = { 'name': 'Product Test', 'uom_id': uom_id, 'purchase_method': 'purchase', } self.product = prod_model.sudo().create(pr_dict) pl_dict1 = { 'date_planned': Datetime.now(), 'name': 'PO01', 'order_id': self.purchase_order_1.id, 'product_id': self.product.id, 'product_uom': uom_id, 'price_unit': 1.0, 'product_qty': 5.0, 'account_analytic_id': self.analytic_account_1.id, } self.purchase_order_line_1 = \ purchase_order_line_model.sudo().create(pl_dict1) self.purchase_order_1.button_confirm() # Purchase Order Num 2 po_dict2 = { 'partner_id': self.partner2.id, } self.purchase_order_2 = self.purchase_order_model.create(po_dict2) pr_dict2 = { 'name': 'Product Test 2', 'uom_id': uom_id, 'purchase_method': 'receive', } self.product2 = prod_model.sudo().create(pr_dict2) pl_dict2 = { 'date_planned': Datetime.now(), 'name': 'PO02', 'order_id': self.purchase_order_2.id, 'product_id': self.product2.id, 'product_uom': uom_id, 'price_unit': 1.0, 'product_qty': 5.0, 'account_analytic_id': self.analytic_account_1.id, } self.purchase_order_line_2 = \ purchase_order_line_model.sudo().create(pl_dict2) self.purchase_order_2.button_confirm()
def UTC_String_To_TW_TZ(timestr): if not timestr: return None dt_time = UTC_Datetime_To_TW_TZ(timestr) return Datetime.to_string(dt_time)
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
def UTC_Datetime_To_TW_TZ(timestr): if not timestr: return None return Datetime.context_timestamp(TimeZoneHelper_TW2, Datetime.from_string(timestr))
def time_ago(from_): if from_ is None: from_ = Datetime.now() return human(Datetime.from_string(from_), 1)
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
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