在 openerp 序列中,我们可以插入带有世纪作为后缀或前缀的当前年份,如下所示:
/%(year)s
我需要将财政年度作为序列中的后缀。有没有可能的方法?
例如:PO/0001/2012-2013
创建序列时,可以添加此函数以使用序列返回值。
您可以尝试使用此功能来查找会计年度:
def finyear(self):
today=date.today()
months=today.month
if months<=3:
return '%d-%d'%(today.year-1,today.year)
return '%d-%d'%(today.year,today.year+1)
如果您需要生成当前年份,您可以使用 datetime 模块执行此操作,如下所示
import datetime
print datetime.datetime.strftime(datetime.datetime.now(), '%Y')
这会给你'2012'。您可以在datetime 文档中阅读有关该模块的更多信息
编辑:那么沿着这些路线还有更多的东西吗?
import datetime
def get_fiscal_year(start_month=3):
now = datetime.datetime.now()
if now.month >= start_month:
return '%d-%d' % (now.year, now.year + 1)
return '%d-%d' % (now.year - 1, now.year)
我看不到仅使用序列的后缀或前缀字段的任何方法。但是,您可能可以通过编写扩展ir_sequence
类的模块来做到这一点。当前类提供了一个字典,其中包含您可以在序列前缀或后缀中使用的值。
def _interpolation_dict(self):
t = time.localtime() # Actually, the server is always in UTC.
return {
'year': time.strftime('%Y', t),
'month': time.strftime('%m', t),
'day': time.strftime('%d', t),
'y': time.strftime('%y', t),
'doy': time.strftime('%j', t),
'woy': time.strftime('%W', t),
'weekday': time.strftime('%w', t),
'h24': time.strftime('%H', t),
'h12': time.strftime('%I', t),
'min': time.strftime('%M', t),
'sec': time.strftime('%S', t),
}
如果您要使用查找当前会计年度并添加另一个条目的版本来覆盖它,它可能会执行您想要的操作。
在某些需要生成一堆序列号的地方测量性能可能是明智的,因为数据库查找比检查系统时间要慢得多。您可能希望将会计年度缓存一段时间。
可以像下面那样为每个会计年度添加前缀并重置每个会计年度的序列码。引入一个新的序列属性%(fiscal_year)s。这是如何做到的。继承 ir_sequence 并覆盖方法 _interpolation_dict 和 _next
添加了一个新的财政年度名称列。此列为每个 _next 调用设置一个值会计年度名称,并在会计年度名称更改时重置。
序列xml
<record id="seq_cust_invoice_no" model="ir.sequence">
<field name="name">Customer Invoice No.</field>
<field name="code">cust_invoice_no</field>
<field name="prefix">%(fiscal_year)s/</field>
<field name="padding">4</field>
<field name="number_increment">1</field>
</record>
下面的代码:
类 ir_sequence(osv.osv): _inherit = 'ir.sequence'
_columns = {
'fiscal_year_name': fields.char('Created Fiscal year', size=64, help="Created Fiscal year"),
}
def _interpolation_dict(self, cr, uid, fy):
res = super(ir_sequence, self)._interpolation_dict()
res['fiscal_year'] = fy.name
return res
def _next(self, cr, uid, seq_ids, context=None):
if not seq_ids:
return False
if context is None:
context = {}
company_id = self.pool.get('res.users').browse(cr, uid, uid).company_id.id
fiscal_year_id = self.pool.get('account.fiscalyear').search(cr, uid, [('state','=','draft'), ('company_id', '=', company_id)])
fy = self.pool.get('account.fiscalyear').browse(cr, uid, fiscal_year_id)[0]
# Honoring the changes for ir_sequence.py override in accounts
sequence_ids = seq_ids
for seq in self.browse(cr, uid, seq_ids, context):
for line in seq.fiscal_ids:
if line.fiscalyear_id.id == context.get('fiscalyear_id'):
sequence_ids = [line.sequence_id.id]
break
force_company = context.get('force_company')
if not force_company:
force_company = self.pool.get('res.users').browse(cr, uid, uid).company_id.id
sequences = self.read(cr, uid, sequence_ids, ['name','company_id','implementation','number_next','prefix','suffix','padding', 'fiscal_year_name'])
preferred_sequences = [s for s in sequences if s['company_id'] and s['company_id'][0] == force_company ]
seq = preferred_sequences[0] if preferred_sequences else sequences[0]
if seq['implementation'] == 'standard':
if seq['prefix'] == '%(fiscal_year)s/':
if not seq['fiscal_year_name'] or fy.name != seq['fiscal_year_name']:
cr.execute("SELECT setval('ir_sequence_%03d', 1, true)" % seq['id'])
seq['number_next'] = cr.fetchone()
else:
cr.execute("SELECT nextval('ir_sequence_%03d')" % seq['id'])
seq['number_next'] = cr.fetchone()
else:
cr.execute("SELECT nextval('ir_sequence_%03d')" % seq['id'])
seq['number_next'] = cr.fetchone()
else:
cr.execute("SELECT number_next FROM ir_sequence WHERE id=%s FOR UPDATE NOWAIT", (seq['id'],))
cr.execute("UPDATE ir_sequence SET number_next=number_next+number_increment WHERE id=%s ", (seq['id'],))
d = self._interpolation_dict(cr, uid, fy)
try:
interpolated_prefix = self._interpolate(seq['prefix'], d)
interpolated_suffix = self._interpolate(seq['suffix'], d)
except ValueError:
raise osv.except_osv(_('Warning'), _('Invalid prefix or suffix for sequence \'%s\'') % (seq.get('name')))
self.write(cr, uid, sequence_ids, {'fiscal_year_name': fy.name}, context=context)
return interpolated_prefix + '%%0%sd' % seq['padding'] % seq['number_next'] + interpolated_suffix
ir_sequence()