# -*- coding: utf-8 -*-

from odoo import api, fields, models


class WeApprovalRecord(models.Model):
    _name = 'we.approval.record'
    _description = '审批记录'

    sp_no = fields.Char('审批单号')
    employee_id = fields.Many2one('hr.employee', '审批人', index=True)
    state = fields.Selection([('1', '审批中'), ('2', '已同意'), ('3', '已驳回'), ('4', '已转审')], string='审批状态')
    approval_time = fields.Datetime('审批时间')
    approval_options = fields.Char('审批意见')

    def create_approve_record(self, sp_no, userid, sp_state, approval_time, approval_options):
        """
        创建审批记录
        :param sp_no:
        :param userid:
        :param sp_state:
        :param approval_time:
        :param approval_options:
        :return:
        """
        employee_obj = self.env['hr.employee'].search([('we_employee_id', '=', userid)])
        approval_order = self.env['we.approval.record'].search(
            [('employee_id', '=', employee_obj.id), ('sp_no', '=', sp_no)])
        vals = {
            'sp_no': sp_no,
            'employee_id': employee_obj.id,
            'state': sp_state,
            'approval_time': approval_time,
            'approval_options': approval_options
        }
        if not approval_order:
            approval_order = super(WeApprovalRecord, self).create(vals)
        else:
            approval_order.write(vals)
        return approval_order
