Openerp中的序列格式

时间:2012-05-14 07:07:53

标签: python xml openerp

在openerp序列中,我们可以插入当前年份,以世纪为后缀或前缀,如下所示:

    /%(year)s 

我需要将会计年度作为序列中的后缀。有什么办法吗?

例如:PO / 0001 / 2012-2013

4 个答案:

答案 0 :(得分:2)

创建序列时,可以添加此函数以返回带序列的值。

您可以尝试使用此功能查找会计年度:

  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)

答案 1 :(得分:1)

如果你需要生成当前年份,你可以使用日期时间模块这样做

import datetime
print datetime.datetime.strftime(datetime.datetime.now(), '%Y')

哪个会给你'2012'。您可以在datetime docs

中阅读有关该模块的更多信息

编辑: 那么这些更多的东西呢?

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)

答案 2 :(得分:1)

我只能使用序列的后缀或前缀字段来查看任何方法。但是,您可以通过编写扩展ir_sequence类的模块来实现。当前类provides a dictionary,其值可以在序列前缀或后缀中使用。

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),
    }

如果您使用查找current fiscal year并添加其他条目的版本覆盖它,则可能会执行您想要的操作。

在一些需要生成一系列序列号的地方测量性能可能是明智的,因为数据库查找要比检查系统时间慢得多。您可能希望将会计年度缓存一段时间。

答案 3 :(得分:0)

为会计年度添加前缀并重置每个会计年度的序列代码,如下所示。 引入新的序列属性%(fiscal_year)s 。 这是怎么做的。继承ir_sequence并覆盖方法_interpolation_dict和_next

添加了一个新列fiscal_year_name。此列为每个_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>

以下代码:

class 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()