Sequences in Openerp

Asked by Pravitha V on 2012-05-14

In openerp sequences we can insert current year with century as suffix or prefix as following:
Code:
    /%(year)s

I need to get the fiscal year as suffix in the sequence. Is there any possible way?

for eg: PO/0001/2012-2013

_________________
Regards,
Nightfury

Question information

Language:
English Edit question
Status:
Answered
For:
OpenERP Server Edit question
Assignee:
No assignee Edit question
Last query:
2012-07-12
Last reply:
2012-07-18

Hello,

Happy to help you.
Right now OpenERP is supporting only YEAR option for suffix which will be in 4 later.

This is the code written in python as:
'year': time.strftime('%Y', t) # will result current year string format.

So if you write %(year)s, then '2012' will be answer.

If you want to modify it, then you have to add in ir.sequence something like this:
'fiscal_year' = current year + "-" + next year (which you can generate by converting in integer)

So, %(financial_year)s = '2012-2013'

Its working, I have tried this.

Thanks,
IntellecTSeed Technologies.
www.intellectseed.com

Pravitha V (pravithavarghese1) said : #2

were should i edit in ir.sequence sir?

Hello,

In ir.sequence file, you have to make changes in "_interpolation_dict" method.

You can make calculation above return dictionary, and then whatever result is there, you can use it as fiscal_year.

Thanks,
IntellecTSeed Technologies
<email address hidden>

Pravitha V (pravithavarghese1) said : #4

you mean ir.sequence.py file?

Pravitha V (pravithavarghese1) said : #6

this is my ir.sequence.py file can u just help me with the coding u done?
pease

code:
# -*- coding: utf-8 -*-
##############################################################################
#
# OpenERP, Open Source Management Solution
# Copyright (C) 2004-TODAY OpenERP S.A. <http://www.openerp.com>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
##############################################################################

import time
from osv import fields,osv
import pooler

class ir_sequence_type(osv.osv):
    _name = 'ir.sequence.type'
    _order = 'name'
    _columns = {
        'name': fields.char('Name',size=64, required=True),
        'code': fields.char('Code',size=32, required=True),
    }
ir_sequence_type()

def _code_get(self, cr, uid, context={}):
    cr.execute('select code, name from ir_sequence_type')
    return cr.fetchall()

class ir_sequence(osv.osv):
    _name = 'ir.sequence'
    _order = 'name'
    _columns = {
        'name': fields.char('Name',size=64, required=True),
        'code': fields.selection(_code_get, 'Code',size=64, required=True),
        'active': fields.boolean('Active'),
        'prefix': fields.char('Prefix',size=64, help="Prefix value of the record for the sequence"),
        'suffix': fields.char('Suffix',size=64, help="Suffix value of the record for the sequence"),
        'number_next': fields.integer('Next Number', required=True, help="Next number of this sequence"),
        'number_increment': fields.integer('Increment Number', required=True, help="The next number of the sequence will be incremented by this number"),
        'padding' : fields.integer('Number padding', required=True, help="OpenERP will automatically adds some '0' on the left of the 'Next Number' to get the required padding size."),
        'company_id': fields.many2one('res.company', 'Company'),
    }
    _defaults = {
        'active': lambda *a: True,
        'company_id': lambda s,cr,uid,c: s.pool.get('res.company')._company_default_get(cr, uid, 'ir.sequence', context=c),
        'number_increment': lambda *a: 1,
        'number_next': lambda *a: 1,
        'padding' : lambda *a : 0,
    }

    def _process(self, s):
        return (s or '') % {
            'year':time.strftime('%Y'),
            'month': time.strftime('%m'),
            'day':time.strftime('%d'),
            'y': time.strftime('%y'),
            'doy': time.strftime('%j'),
            'woy': time.strftime('%W'),
            'weekday': time.strftime('%w'),
            'h24': time.strftime('%H'),
            'h12': time.strftime('%I'),
            'min': time.strftime('%M'),
            'sec': time.strftime('%S'),
        }

    def get_id(self, cr, uid, sequence_id, test='id', context=None):
        assert test in ('code','id')
        company_ids = self.pool.get('res.company').search(cr, uid, [], context=context)
        cr.execute('''SELECT id, number_next, prefix, suffix, padding
                      FROM ir_sequence
                      WHERE %s=%%s
                       AND active=true
                       AND (company_id in %%s or company_id is NULL)
                      ORDER BY company_id, id
                      FOR UPDATE NOWAIT''' % test,
                      (sequence_id, tuple(company_ids)))
        res = cr.dictfetchone()
        if res:
            cr.execute('UPDATE ir_sequence SET number_next=number_next+number_increment WHERE id=%s AND active=true', (res['id'],))
            if res['number_next']:
                return self._process(res['prefix']) + '%%0%sd' % res['padding'] % res['number_next'] + self._process(res['suffix'])
            else:
                return self._process(res['prefix']) + self._process(res['suffix'])
        return False

    def get(self, cr, uid, code):
        return self.get_id(cr, uid, code, test='code')
ir_sequence()

Hello,

You can apply this patch.

     def _interpolation_dict(self):
         t = time.localtime() # Actually, the server is always in UTC.
+
+ current = int(time.strftime('%Y', t))
+ next = int(time.strftime('%Y', t)) + 1
+ fiscal_year = str(current) + '-' + str(next)
+
         return {
+ 'fiscal_year': fiscal_year,
             'year': time.strftime('%Y', t),
             'month': time.strftime('%m', t),
             'day': time.strftime('%d', t),

Pravitha V (pravithavarghese1) said : #8

hello IntellecTSeed Technologies (intellectseed) ,

"Hello,

You can apply this patch.

     def _interpolation_dict(self):
         t = time.localtime() # Actually, the server is always in UTC.
+
+ current = int(time.strftime('%Y', t))
+ next = int(time.strftime('%Y', t)) + 1
+ fiscal_year = str(current) + '-' + str(next)
+
         return {
+ 'fiscal_year': fiscal_year,
             'year': time.strftime('%Y', t),
             'month': time.strftime('%m', t),
             'day': time.strftime('%d', t),"

i applyed the above code you have sujjested
but it raised a key error in my terminal. the error is as follows:

[2012-07-13 00:40:41,894][admin] ERROR:web-services:Uncaught exception
Traceback (most recent call last):
  File "workspace/openERP/src/openerp-server/bin/osv/osv.py", line 122, in wrapper
    return f(self, dbname, *args, **kwargs)
  File "workspace/openERP/src/openerp-server/bin/osv/osv.py", line 176, in execute
    res = self.execute_cr(cr, uid, obj, method, *args, **kw)
  File "workspace/openERP/src/openerp-server/bin/osv/osv.py", line 167, in execute_cr
    return getattr(object, method)(cr, uid, *args, **kw)
  File "workspace/openERP/src/openerp-server/bin/osv/orm.py", line 983, in default_get
    defaults[f] = self._defaults[f](self, cr, uid, context)
  File "workspace/openERP/src/openerp-server/bin/addons/sale/sale.py", line 327, in <lambda>
    'name': lambda obj, cr, uid, context: obj.pool.get('ir.sequence').get(cr, uid, 'sale.order'),
  File "workspace/openERP/src/openerp-server/bin/addons/base/ir/ir_sequence.py", line 111, in get
    return self.get_id(cr, uid, code, test='code')
  File "workspace/openERP/src/openerp-server/bin/addons/account/sequence.py", line 64, in get_id
    context=context)
  File "workspace/openERP/src/openerp-server/bin/addons/base/ir/ir_sequence.py", line 91, in get_id
    return self._process(res['prefix']) + '%%0%sd' % res['padding'] % res['number_next'] + self._process(res['suffix'])
  File "workspace/openERP/src/openerp-server/bin/addons/base/ir/ir_sequence.py", line 73, in _process
    'sec': time.strftime('%S'),
KeyError: u'fiscal_year'

the below given is my code:

code:

# -*- coding: utf-8 -*-
##############################################################################
#
# OpenERP, Open Source Management Solution
# Copyright (C) 2004-TODAY OpenERP S.A. <http://www.openerp.com>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
##############################################################################

import time
from osv import fields,osv
import pooler

class ir_sequence_type(osv.osv):
    _name = 'ir.sequence.type'
    _order = 'name'
    _columns = {
        'name': fields.char('Name',size=64, required=True),
        'code': fields.char('Code',size=32, required=True),
    }
ir_sequence_type()

def _code_get(self, cr, uid, context={}):
    cr.execute('select code, name from ir_sequence_type')
    return cr.fetchall()

class ir_sequence(osv.osv):
    _name = 'ir.sequence'
    _order = 'name'
    _columns = {
        'name': fields.char('Name',size=64, required=True),
        'code': fields.selection(_code_get, 'Code',size=64, required=True),
        'active': fields.boolean('Active'),
        'prefix': fields.char('Prefix',size=64, help="Prefix value of the record for the sequence"),
        'suffix': fields.char('Suffix',size=64, help="Suffix value of the record for the sequence"),
        'number_next': fields.integer('Next Number', required=True, help="Next number of this sequence"),
        'number_increment': fields.integer('Increment Number', required=True, help="The next number of the sequence will be incremented by this number"),
        'padding' : fields.integer('Number padding', required=True, help="OpenERP will automatically adds some '0' on the left of the 'Next Number' to get the required padding size."),
        'company_id': fields.many2one('res.company', 'Company'),
    }
    _defaults = {
        'active': lambda *a: True,
        'company_id': lambda s,cr,uid,c: s.pool.get('res.company')._company_default_get(cr, uid, 'ir.sequence', context=c),
        'number_increment': lambda *a: 1,
        'number_next': lambda *a: 1,
        'padding' : lambda *a : 0,
    }

    def _process(self, s):
        return (s or '') % {
            'year':time.strftime('%Y'),
            'month': time.strftime('%m'),
            'day':time.strftime('%d'),
            'y': time.strftime('%y'),
            'doy': time.strftime('%j'),
            'woy': time.strftime('%W'),
            'weekday': time.strftime('%w'),
            'h24': time.strftime('%H'),
            'h12': time.strftime('%I'),
            'min': time.strftime('%M'),
            'sec': time.strftime('%S'),
        }

    def get_id(self, cr, uid, sequence_id, test='id', context=None):
        assert test in ('code','id')
        company_ids = self.pool.get('res.company').search(cr, uid, [], context=context)
        cr.execute('''SELECT id, number_next, prefix, suffix, padding
                      FROM ir_sequence
                      WHERE %s=%%s
                       AND active=true
                       AND (company_id in %%s or company_id is NULL)
                      ORDER BY company_id, id
                      FOR UPDATE NOWAIT''' % test,
                      (sequence_id, tuple(company_ids)))
        res = cr.dictfetchone()
        if res:
            cr.execute('UPDATE ir_sequence SET number_next=number_next+number_increment WHERE id=%s AND active=true', (res['id'],))
            if res['number_next']:
                return self._process(res['prefix']) + '%%0%sd' % res['padding'] % res['number_next'] + self._process(res['suffix'])
            else:
                return self._process(res['prefix']) + self._process(res['suffix'])
        return False

    def _interpolation_dict(self):
         t = time.localtime() # Actually, the server is always in UTC.
         current = int(time.strftime('%Y', t))
         next = int(time.strftime('%Y', t)) + 1
         fiscal_year = str(current) + '-' + str(next)
         return {
             'fiscal_year': fiscal_year,
             'year': time.strftime('%Y', t),
             'month': time.strftime('%m', t),
             'day': time.strftime('%d', t),
             }

    def get(self, cr, uid, code):
        return self.get_id(cr, uid, code, test='code')
ir_sequence()

# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:

if you dont mind an you just paste the ir.sequence.py code of yours so that i can refer mine with yours. please help as soon as possible

Pravitha V (pravithavarghese1) said : #9

someone please help....................

Can you help with this problem?

Provide an answer of your own, or ask Pravitha V for more information if necessary.

To post a message you must log in.