Files
gzth/yuthon_holiday/wizard/leave_allocation_wizard.py
T
2025-09-05 17:09:58 +08:00

64 lines
2.4 KiB
Python

# -*- 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:
number_of_days_display = int(row[1].value)/8
else:
number_of_days_display = 0
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',
"number_of_days_display":number_of_days_display,
'notes':'年假公司分配'
})
self.env['hr.leave.allocation'].create({
'employee_id': employee_id.id,
'name': '补休',
'employee_ids': [(6, 0, [employee_id.id])],
'holiday_status_id' : holiday_status_id1.id,
'number_of_hours_display':row[2].value,
})