# -*- coding: utf-8 -*- from odoo import models, fields, api, _, SUPERUSER_ID from odoo.exceptions import ValidationError, UserError import xlrd import base64 import datetime from datetime import datetime import pandas as pd class LeaveAllocationWizard(models.Model): _name = 'leave.allocation.wizard' _description = '初始化假期弹窗' excel_file = fields.Binary(string="上传Excel表") excel_name = fields.Char(string="文件名") def confirm(self): """ 导入excel文件,按行读取数据并处理 :return: 导入员工信息表并更新 """ if not self.excel_file: raise ValidationError('请上传文件。') book = xlrd.open_workbook(file_contents=base64.decodebytes(self.excel_file)) sh = book.sheet_by_index(0) for rx in range(1, sh.nrows): row = sh.row(rx) self.salary_import(row) def salary_import(self, row): employee_id = self.env['hr.employee'].search([('name', '=', row[0].value)], limit=1) if not employee_id: raise ValidationError(f'员工姓名{row[0].value}不存在,请检查。') holiday_status_id = self.env['hr.leave.type'].search([('name', '=', '年假'), ('company_id', '=', '天河物管')], limit=1) holiday_status_id1 = self.env['hr.leave.type'].search([('name', '=', '补休')], limit=1) accrual_plan_id = self.env['hr.leave.accrual.plan'].search([('name', '=', '年假(天河物管)')], limit=1) if row[1].value and int(row[1].value) != 0: number_of_days_display = int(row[1].value) allocation_id = self.env['hr.leave.allocation'].search([('employee_id', '=', employee_id.id), ('holiday_status_id', '=', holiday_status_id.id)]) if allocation_id: allocation_id.write({ 'number_of_days': number_of_days_display }) else: self.env['hr.leave.allocation'].create({ 'employee_id': employee_id.id, 'name': '年假公司分配', 'number_of_days': number_of_days_display, 'employee_ids': [(6, 0, [employee_id.id])], 'holiday_status_id': holiday_status_id.id, 'accrual_plan_id': accrual_plan_id.id, 'allocation_type': 'accrual', 'date_from': datetime(2025, 1, 1), 'notes': '年假公司分配' }) if row[2].value and int(row[2].value) != 0: allocation_id = self.env['hr.leave.allocation'].search([('employee_id', '=', employee_id.id), ('holiday_status_id', '=', holiday_status_id1.id)]) if allocation_id: allocation_id.write({ 'number_of_hours': int(row[2].value), }) else: self.env['hr.leave.allocation'].create({ 'holiday_status_id': holiday_status_id1.id, 'employee_id': employee_id.id, 'name': '补休', 'date_from': datetime(2025, 12, 1), 'employee_ids': [(6, 0, [employee_id.id])], 'number_of_hours_display': int(row[2].value) })