添加功能
This commit is contained in:
@@ -0,0 +1,40 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
from odoo import models, fields, api
|
||||
|
||||
|
||||
class ResCompanyNameHistory(models.Model):
|
||||
_name = 'res.company.name.history'
|
||||
_description = '公司名称历史'
|
||||
_order = 'start_date'
|
||||
_sql_constraints = [
|
||||
('unique_company_name_start', 'UNIQUE(company_id, name, start_date)',
|
||||
'同一公司同一名称同一生效日期不可重复录入'),
|
||||
]
|
||||
|
||||
company_id = fields.Many2one('res.company', string='公司', required=True, ondelete='cascade', index=True)
|
||||
name = fields.Char(string='名称', required=True)
|
||||
start_date = fields.Date(string='生效日期')
|
||||
end_date = fields.Date(string='失效日期')
|
||||
|
||||
|
||||
class ResCompany(models.Model):
|
||||
_inherit = 'res.company'
|
||||
|
||||
name_history_ids = fields.One2many('res.company.name.history', 'company_id', string='名称历史')
|
||||
|
||||
def name_as_of(self, target_date):
|
||||
"""返回目标日期生效的公司名称;无匹配历史记录时返回当前名称。"""
|
||||
self.ensure_one()
|
||||
if not target_date:
|
||||
return self.name
|
||||
target = target_date.date() if hasattr(target_date, 'date') else target_date
|
||||
matched = self.env['res.company.name.history']
|
||||
for h in self.name_history_ids:
|
||||
start = h.start_date
|
||||
end = h.end_date
|
||||
if (not start or start <= target) and (not end or target < end):
|
||||
matched += h
|
||||
if matched:
|
||||
return matched.sorted('start_date')[-1].name
|
||||
return self.name
|
||||
|
||||
Reference in New Issue
Block a user