From 4811ffdd7d7623c693a87de6202778216e9ecff7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phane=20Bidoul?= Date: Wed, 5 Nov 2014 18:52:46 +0100 Subject: [PATCH 1/8] [ADD] account_auto_fy_sequence --- account_auto_fy_sequence/__init__.py | 26 +++++++ account_auto_fy_sequence/__openerp__.py | 53 +++++++++++++ account_auto_fy_sequence/i18n/fr.po | 3 + account_auto_fy_sequence/models/__init__.py | 26 +++++++ .../models/ir_sequence.py | 77 +++++++++++++++++++ account_auto_fy_sequence/tests/__init__.py | 30 ++++++++ .../tests/test_auto_fy_sequence.py | 69 +++++++++++++++++ 7 files changed, 284 insertions(+) create mode 100644 account_auto_fy_sequence/__init__.py create mode 100644 account_auto_fy_sequence/__openerp__.py create mode 100644 account_auto_fy_sequence/i18n/fr.po create mode 100644 account_auto_fy_sequence/models/__init__.py create mode 100644 account_auto_fy_sequence/models/ir_sequence.py create mode 100644 account_auto_fy_sequence/tests/__init__.py create mode 100644 account_auto_fy_sequence/tests/test_auto_fy_sequence.py diff --git a/account_auto_fy_sequence/__init__.py b/account_auto_fy_sequence/__init__.py new file mode 100644 index 000000000..1bc695ede --- /dev/null +++ b/account_auto_fy_sequence/__init__.py @@ -0,0 +1,26 @@ +# coding=utf-8 +############################################################################## +# +# account_auto_fy_sequence module for Odoo +# Copyright (C) 2014 ACSONE SA/NV () +# @author Stéphane Bidoul +# +# account_auto_fy_sequence is free software: +# you can redistribute it and/or modify +# it under the terms of the GNU Affero General Public License v3 or later +# as published by the Free Software Foundation, either version 3 of the +# License, or (at your option) any later version. +# +# account_auto_fy_sequence 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 v3 or later for more details. +# +# You should have received a copy of the GNU Affero General Public License +# v3 or later along with this program. +# If not, see . +# +############################################################################## + +from . import models diff --git a/account_auto_fy_sequence/__openerp__.py b/account_auto_fy_sequence/__openerp__.py new file mode 100644 index 000000000..407328866 --- /dev/null +++ b/account_auto_fy_sequence/__openerp__.py @@ -0,0 +1,53 @@ +# coding=utf-8 +############################################################################## +# +# account_auto_fy_sequence module for Odoo +# Copyright (C) 2014 ACSONE SA/NV () +# @author Stéphane Bidoul +# +# account_auto_fy_sequence is free software: +# you can redistribute it and/or modify +# it under the terms of the GNU Affero General Public License v3 or later +# as published by the Free Software Foundation, either version 3 of the +# License, or (at your option) any later version. +# +# account_auto_fy_sequence 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 v3 or later for more details. +# +# You should have received a copy of the GNU Affero General Public License +# v3 or later along with this program. +# If not, see . +# +############################################################################## +{ + 'name': 'Automatic Fiscal Year Sequences', + 'version': '0.1', + 'category': 'Accounting', + 'description': """ + Automatic creation of fiscal year sequences. + + This modules adds the possibility to use the %(fy)s placeholder + in sequences. %(fy)s is replaced by the fiscal year code when + using the sequence. + + The first time the sequence is used for a given fiscal year, + a specific fiscal year sequence starting at 1 is created automatically. + + /!\ If you change %(year)s to %(fy)s on a sequence that has + already been used for the current fiscal year, make sure to manually + create the fiscal year sequence for the current fiscal year and + initialize it's next number to the correct value. + """, + 'author': 'ACSONE SA/NV', + 'website': 'http://acsone.eu', + 'depends': ['account'], + 'data': [ + ], + 'installable': True, + 'application': False, + 'auto_install': False, + 'license': 'AGPL-3', +} diff --git a/account_auto_fy_sequence/i18n/fr.po b/account_auto_fy_sequence/i18n/fr.po new file mode 100644 index 000000000..1588630fd --- /dev/null +++ b/account_auto_fy_sequence/i18n/fr.po @@ -0,0 +1,3 @@ +# Translation of OpenERP Server. +# This file contains the translation of the following modules: +# * account_auto_fy_sequence diff --git a/account_auto_fy_sequence/models/__init__.py b/account_auto_fy_sequence/models/__init__.py new file mode 100644 index 000000000..8833fa6e1 --- /dev/null +++ b/account_auto_fy_sequence/models/__init__.py @@ -0,0 +1,26 @@ +# coding=utf-8 +############################################################################## +# +# account_auto_fy_sequence module for Odoo +# Copyright (C) 2014 ACSONE SA/NV () +# @author Stéphane Bidoul +# +# account_auto_fy_sequence is free software: +# you can redistribute it and/or modify +# it under the terms of the GNU Affero General Public License v3 or later +# as published by the Free Software Foundation, either version 3 of the +# License, or (at your option) any later version. +# +# account_auto_fy_sequence 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 v3 or later for more details. +# +# You should have received a copy of the GNU Affero General Public License +# v3 or later along with this program. +# If not, see . +# +############################################################################## + +from . import ir_sequence diff --git a/account_auto_fy_sequence/models/ir_sequence.py b/account_auto_fy_sequence/models/ir_sequence.py new file mode 100644 index 000000000..60a06f892 --- /dev/null +++ b/account_auto_fy_sequence/models/ir_sequence.py @@ -0,0 +1,77 @@ +# coding=utf-8 +############################################################################## +# +# account_auto_fy_sequence module for Odoo +# Copyright (C) 2014 ACSONE SA/NV () +# @author Stéphane Bidoul +# +# account_auto_fy_sequence is free software: +# you can redistribute it and/or modify +# it under the terms of the GNU Affero General Public License v3 or later +# as published by the Free Software Foundation, either version 3 of the +# License, or (at your option) any later version. +# +# account_auto_fy_sequence 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 v3 or later for more details. +# +# You should have received a copy of the GNU Affero General Public License +# v3 or later along with this program. +# If not, see . +# +############################################################################## + +from openerp.osv import orm +from openerp.tools.translate import _ + +FY_SLOT = '%(fy)s' + + +class Sequence(orm.Model): + _inherit = 'ir.sequence' + + def _next(self, cr, uid, seq_ids, context=None): + if context is None: + context = {} + assert len(seq_ids) == 1 + seq = self.browse(cr, uid, seq_ids[0], context) + if (seq.prefix and FY_SLOT in seq.prefix) or \ + (seq.suffix and FY_SLOT in seq.suffix): + fiscalyear_id = context.get('fiscalyear_id') + if not fiscalyear_id: + raise orm.except_orm(_('Error!'), + _('The system tried to access ' + 'a fiscal year sequence ' + 'without specifying the actual ' + 'fiscal year.')) + for line in seq.fiscal_ids: + if line.fiscalyear_id.id == fiscalyear_id: + return super(Sequence, self)\ + ._next(cr, uid, [line.sequence_id.id], context) + # no fiscal year sequence found, auto create it + fiscalyear = self.pool['account.fiscalyear']\ + .browse(cr, uid, fiscalyear_id, context=context) + fy_seq_id = self.create(cr, uid, { + 'name': seq.name + ' - ' + fiscalyear.code, + 'code': seq.code, + 'implementation': seq.implementation, + 'prefix': (seq.prefix and + seq.prefix.replace(FY_SLOT, fiscalyear.code)), + 'suffix': (seq.suffix and + seq.suffix.replace(FY_SLOT, fiscalyear.code)), + 'number_next': 1, + 'number_increment': seq.number_increment, + 'padding': seq.padding, + 'company_id': seq.company_id.id, + }, context=context) + self.pool['account.sequence.fiscalyear']\ + .create(cr, uid, { + 'sequence_id': fy_seq_id, + 'sequence_main_id': seq.id, + 'fiscalyear_id': fiscalyear_id, + }, context=context) + return super(Sequence, self)\ + ._next(cr, uid, [fy_seq_id], context) + return super(Sequence, self)._next(cr, uid, seq_ids, context) diff --git a/account_auto_fy_sequence/tests/__init__.py b/account_auto_fy_sequence/tests/__init__.py new file mode 100644 index 000000000..a31764f72 --- /dev/null +++ b/account_auto_fy_sequence/tests/__init__.py @@ -0,0 +1,30 @@ +# coding=utf-8 +############################################################################## +# +# account_auto_fy_sequence module for Odoo +# Copyright (C) 2014 ACSONE SA/NV () +# @author Stéphane Bidoul +# +# account_auto_fy_sequence is free software: +# you can redistribute it and/or modify +# it under the terms of the GNU Affero General Public License v3 or later +# as published by the Free Software Foundation, either version 3 of the +# License, or (at your option) any later version. +# +# account_auto_fy_sequence 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 v3 or later for more details. +# +# You should have received a copy of the GNU Affero General Public License +# v3 or later along with this program. +# If not, see . +# +############################################################################## + +from . import test_auto_fy_sequence + +checks = [ + test_auto_fy_sequence, +] diff --git a/account_auto_fy_sequence/tests/test_auto_fy_sequence.py b/account_auto_fy_sequence/tests/test_auto_fy_sequence.py new file mode 100644 index 000000000..235822ca7 --- /dev/null +++ b/account_auto_fy_sequence/tests/test_auto_fy_sequence.py @@ -0,0 +1,69 @@ +# coding=utf-8 +############################################################################## +# +# account_auto_fy_sequence module for Odoo +# Copyright (C) 2014 ACSONE SA/NV () +# @author Stéphane Bidoul +# +# account_auto_fy_sequence is free software: +# you can redistribute it and/or modify +# it under the terms of the GNU Affero General Public License v3 or later +# as published by the Free Software Foundation, either version 3 of the +# License, or (at your option) any later version. +# +# account_auto_fy_sequence 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 v3 or later for more details. +# +# You should have received a copy of the GNU Affero General Public License +# v3 or later along with this program. +# If not, see . +# +############################################################################## + +import time + +import openerp.tests.common as common +from openerp.osv import orm + + +class TestAutoFYSequence(common.TransactionCase): + + def setUp(self): + super(TestAutoFYSequence, self).setUp() + self.seq_obj = self.registry('ir.sequence') + + def _create_seq(self, prefix): + seq_id = self.seq_obj.create(self.cr, self.uid, { + 'name': 'test sequence', + 'implementation': 'no_gap', + 'prefix': prefix, + }) + return seq_id + + def test_0(self): + """ normal sequence """ + seq_id = self._create_seq('SEQ/%(year)s/') + n = self.seq_obj._next(self.cr, self.uid, [seq_id]) + self.assertEqual(n, "SEQ/%s/1" % time.strftime("%Y")) + + def test_1(self): + """ invoke fiscal year sequence + without specifying the fiscal year """ + seq_id = self._create_seq('SEQ/%(fy)s/') + with self.assertRaises(orm.except_orm): + self.seq_obj._next(self.cr, self.uid, [seq_id]) + + def test_2(self): + """ invoke fiscal year sequence """ + fiscalyear_id = self.ref('account.data_fiscalyear') + context = {'fiscalyear_id': fiscalyear_id} + fiscalyear = self.registry('account.fiscalyear')\ + .browse(self.cr, self.uid, fiscalyear_id) + seq_id = self._create_seq('SEQ/%(fy)s/') + n = self.seq_obj._next(self.cr, self.uid, [seq_id], context) + self.assertEqual(n, "SEQ/%s/1" % fiscalyear.code) + n = self.seq_obj._next(self.cr, self.uid, [seq_id], context) + self.assertEqual(n, "SEQ/%s/2" % fiscalyear.code) From 8a37b9f7a79e47c7bb80e19ab80b1d3df8751c0b Mon Sep 17 00:00:00 2001 From: Laetitia Gangloff Date: Mon, 24 Nov 2014 15:29:05 +0100 Subject: [PATCH 2/8] [IMP] account_auto_fy_sequence: add fr translation --- .../i18n/account_auto_fy_sequence.pot | 34 +++++++++++++++++++ account_auto_fy_sequence/i18n/fr.po | 33 +++++++++++++++++- 2 files changed, 66 insertions(+), 1 deletion(-) create mode 100644 account_auto_fy_sequence/i18n/account_auto_fy_sequence.pot diff --git a/account_auto_fy_sequence/i18n/account_auto_fy_sequence.pot b/account_auto_fy_sequence/i18n/account_auto_fy_sequence.pot new file mode 100644 index 000000000..47d80b514 --- /dev/null +++ b/account_auto_fy_sequence/i18n/account_auto_fy_sequence.pot @@ -0,0 +1,34 @@ +# Translation of OpenERP Server. +# This file contains the translation of the following modules: +# * account_auto_fy_sequence +# +msgid "" +msgstr "" +"Project-Id-Version: OpenERP Server 7.0\n" +"Report-Msgid-Bugs-To: \n" +"POT-Creation-Date: 2014-11-24 11:26+0000\n" +"PO-Revision-Date: 2014-11-24 11:26+0000\n" +"Last-Translator: <>\n" +"Language-Team: \n" +"MIME-Version: 1.0\n" +"Content-Type: text/plain; charset=UTF-8\n" +"Content-Transfer-Encoding: \n" +"Plural-Forms: \n" + +#. module: account_auto_fy_sequence +#: code:addons/account_auto_fy_sequence/models/ir_sequence.py:44 +#, python-format +msgid "Error!" +msgstr "" + +#. module: account_auto_fy_sequence +#: code:addons/account_auto_fy_sequence/models/ir_sequence.py:45 +#, python-format +msgid "The system tried to access a fiscal year sequence without specifying the actual fiscal year." +msgstr "" + +#. module: account_auto_fy_sequence +#: model:ir.model,name:account_auto_fy_sequence.model_ir_sequence +msgid "ir.sequence" +msgstr "" + diff --git a/account_auto_fy_sequence/i18n/fr.po b/account_auto_fy_sequence/i18n/fr.po index 1588630fd..b74bbe1ab 100644 --- a/account_auto_fy_sequence/i18n/fr.po +++ b/account_auto_fy_sequence/i18n/fr.po @@ -1,3 +1,34 @@ # Translation of OpenERP Server. # This file contains the translation of the following modules: -# * account_auto_fy_sequence +# * account_auto_fy_sequence +# +msgid "" +msgstr "" +"Project-Id-Version: OpenERP Server 7.0\n" +"Report-Msgid-Bugs-To: \n" +"POT-Creation-Date: 2014-11-24 11:26+0000\n" +"PO-Revision-Date: 2014-11-24 11:26+0000\n" +"Last-Translator: <>\n" +"Language-Team: \n" +"MIME-Version: 1.0\n" +"Content-Type: text/plain; charset=UTF-8\n" +"Content-Transfer-Encoding: \n" +"Plural-Forms: \n" + +#. module: account_auto_fy_sequence +#: code:addons/account_auto_fy_sequence/models/ir_sequence.py:44 +#, python-format +msgid "Error!" +msgstr "Erreur!" + +#. module: account_auto_fy_sequence +#: code:addons/account_auto_fy_sequence/models/ir_sequence.py:45 +#, python-format +msgid "The system tried to access a fiscal year sequence without specifying the actual fiscal year." +msgstr "Le système tente d'accéder à une séquence pour exercice fiscal sans préciser d'exercice." + +#. module: account_auto_fy_sequence +#: model:ir.model,name:account_auto_fy_sequence.model_ir_sequence +msgid "ir.sequence" +msgstr "" + From 37868844f4e1126e7bb4059ee5dd3455ec684d40 Mon Sep 17 00:00:00 2001 From: Laetitia Gangloff Date: Mon, 24 Nov 2014 15:50:27 +0100 Subject: [PATCH 3/8] [IMP] account_auto_fy_sequence: update sequence view to add %(fy)s in legend --- account_auto_fy_sequence/__openerp__.py | 2 +- .../views/ir_sequence_view.xml | 16 ++++++++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) create mode 100644 account_auto_fy_sequence/views/ir_sequence_view.xml diff --git a/account_auto_fy_sequence/__openerp__.py b/account_auto_fy_sequence/__openerp__.py index 407328866..0f084e6cd 100644 --- a/account_auto_fy_sequence/__openerp__.py +++ b/account_auto_fy_sequence/__openerp__.py @@ -44,7 +44,7 @@ 'author': 'ACSONE SA/NV', 'website': 'http://acsone.eu', 'depends': ['account'], - 'data': [ + 'data': ['views/ir_sequence_view.xml', ], 'installable': True, 'application': False, diff --git a/account_auto_fy_sequence/views/ir_sequence_view.xml b/account_auto_fy_sequence/views/ir_sequence_view.xml new file mode 100644 index 000000000..2a26e144b --- /dev/null +++ b/account_auto_fy_sequence/views/ir_sequence_view.xml @@ -0,0 +1,16 @@ + + + + + ir.sequence.form + ir.sequence + + + + + + + From 7bde17a2ba4e4ebcc9c0474dcf3aa61138a9b0f6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phane=20Bidoul?= Date: Mon, 24 Nov 2014 18:37:52 +0100 Subject: [PATCH 4/8] [FIX] account_auto_fy_sequence: pep8 --- account_auto_fy_sequence/__openerp__.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/account_auto_fy_sequence/__openerp__.py b/account_auto_fy_sequence/__openerp__.py index 0f084e6cd..b1fb9cbaf 100644 --- a/account_auto_fy_sequence/__openerp__.py +++ b/account_auto_fy_sequence/__openerp__.py @@ -44,7 +44,8 @@ 'author': 'ACSONE SA/NV', 'website': 'http://acsone.eu', 'depends': ['account'], - 'data': ['views/ir_sequence_view.xml', + 'data': [ + 'views/ir_sequence_view.xml', ], 'installable': True, 'application': False, From 475e22a719a6eb9c5b588d1a6a772ce2b3ac36fc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phane=20Bidoul?= Date: Tue, 25 Nov 2014 15:07:23 +0100 Subject: [PATCH 5/8] [FIX] account_auto_fy_sequence: spelling --- account_auto_fy_sequence/__openerp__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/account_auto_fy_sequence/__openerp__.py b/account_auto_fy_sequence/__openerp__.py index b1fb9cbaf..04533616f 100644 --- a/account_auto_fy_sequence/__openerp__.py +++ b/account_auto_fy_sequence/__openerp__.py @@ -29,7 +29,7 @@ 'description': """ Automatic creation of fiscal year sequences. - This modules adds the possibility to use the %(fy)s placeholder + This module adds the possibility to use the %(fy)s placeholder in sequences. %(fy)s is replaced by the fiscal year code when using the sequence. From 748ae53495c7550c5812d9ef434a826bed1e123a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phane=20Bidoul?= Date: Tue, 25 Nov 2014 16:26:27 +0100 Subject: [PATCH 6/8] [IMP] auto_fy_sequence: refactor for readability --- .../models/ir_sequence.py | 46 +++++++++++-------- 1 file changed, 27 insertions(+), 19 deletions(-) diff --git a/account_auto_fy_sequence/models/ir_sequence.py b/account_auto_fy_sequence/models/ir_sequence.py index 60a06f892..918c4b81d 100644 --- a/account_auto_fy_sequence/models/ir_sequence.py +++ b/account_auto_fy_sequence/models/ir_sequence.py @@ -32,6 +32,30 @@ FY_SLOT = '%(fy)s' class Sequence(orm.Model): _inherit = 'ir.sequence' + def _create_fy_sequence(self, cr, uid, seq, fiscalyear, context=None): + """ Create a FY sequence by cloning a sequence + which has %(fy)s in prefix or suffix """ + fy_seq_id = self.create(cr, uid, { + 'name': seq.name + ' - ' + fiscalyear.code, + 'code': seq.code, + 'implementation': seq.implementation, + 'prefix': (seq.prefix and + seq.prefix.replace(FY_SLOT, fiscalyear.code)), + 'suffix': (seq.suffix and + seq.suffix.replace(FY_SLOT, fiscalyear.code)), + 'number_next': 1, + 'number_increment': seq.number_increment, + 'padding': seq.padding, + 'company_id': seq.company_id.id, + }, context=context) + self.pool['account.sequence.fiscalyear']\ + .create(cr, uid, { + 'sequence_id': fy_seq_id, + 'sequence_main_id': seq.id, + 'fiscalyear_id': fiscalyear.id, + }, context=context) + return fy_seq_id + def _next(self, cr, uid, seq_ids, context=None): if context is None: context = {} @@ -46,6 +70,7 @@ class Sequence(orm.Model): 'a fiscal year sequence ' 'without specifying the actual ' 'fiscal year.')) + # search for existing fiscal year sequence for line in seq.fiscal_ids: if line.fiscalyear_id.id == fiscalyear_id: return super(Sequence, self)\ @@ -53,25 +78,8 @@ class Sequence(orm.Model): # no fiscal year sequence found, auto create it fiscalyear = self.pool['account.fiscalyear']\ .browse(cr, uid, fiscalyear_id, context=context) - fy_seq_id = self.create(cr, uid, { - 'name': seq.name + ' - ' + fiscalyear.code, - 'code': seq.code, - 'implementation': seq.implementation, - 'prefix': (seq.prefix and - seq.prefix.replace(FY_SLOT, fiscalyear.code)), - 'suffix': (seq.suffix and - seq.suffix.replace(FY_SLOT, fiscalyear.code)), - 'number_next': 1, - 'number_increment': seq.number_increment, - 'padding': seq.padding, - 'company_id': seq.company_id.id, - }, context=context) - self.pool['account.sequence.fiscalyear']\ - .create(cr, uid, { - 'sequence_id': fy_seq_id, - 'sequence_main_id': seq.id, - 'fiscalyear_id': fiscalyear_id, - }, context=context) + fy_seq_id = self._create_fy_sequence(cr, uid, seq, + fiscalyear, context) return super(Sequence, self)\ ._next(cr, uid, [fy_seq_id], context) return super(Sequence, self)._next(cr, uid, seq_ids, context) From ff0cc9a82871c7e4e08e6ac57ecf5d27dad177d4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phane=20Bidoul?= Date: Tue, 25 Nov 2014 16:23:53 +0100 Subject: [PATCH 7/8] [FIX] account_auto_fy_sequence: correct behaviour in presence of multiple sequences to choose from --- .../models/ir_sequence.py | 60 +++++++++++-------- 1 file changed, 36 insertions(+), 24 deletions(-) diff --git a/account_auto_fy_sequence/models/ir_sequence.py b/account_auto_fy_sequence/models/ir_sequence.py index 918c4b81d..b6b929656 100644 --- a/account_auto_fy_sequence/models/ir_sequence.py +++ b/account_auto_fy_sequence/models/ir_sequence.py @@ -34,7 +34,7 @@ class Sequence(orm.Model): def _create_fy_sequence(self, cr, uid, seq, fiscalyear, context=None): """ Create a FY sequence by cloning a sequence - which has %(fy)s in prefix or suffix """ + which has %(fy)s in prefix or suffix """ fy_seq_id = self.create(cr, uid, { 'name': seq.name + ' - ' + fiscalyear.code, 'code': seq.code, @@ -59,27 +59,39 @@ class Sequence(orm.Model): def _next(self, cr, uid, seq_ids, context=None): if context is None: context = {} - assert len(seq_ids) == 1 - seq = self.browse(cr, uid, seq_ids[0], context) - if (seq.prefix and FY_SLOT in seq.prefix) or \ - (seq.suffix and FY_SLOT in seq.suffix): - fiscalyear_id = context.get('fiscalyear_id') - if not fiscalyear_id: - raise orm.except_orm(_('Error!'), - _('The system tried to access ' - 'a fiscal year sequence ' - 'without specifying the actual ' - 'fiscal year.')) - # search for existing fiscal year sequence - for line in seq.fiscal_ids: - if line.fiscalyear_id.id == fiscalyear_id: - return super(Sequence, self)\ - ._next(cr, uid, [line.sequence_id.id], context) - # no fiscal year sequence found, auto create it - fiscalyear = self.pool['account.fiscalyear']\ - .browse(cr, uid, fiscalyear_id, context=context) - fy_seq_id = self._create_fy_sequence(cr, uid, seq, - fiscalyear, context) - return super(Sequence, self)\ - ._next(cr, uid, [fy_seq_id], context) + for seq in self.browse(cr, uid, seq_ids, context): + if (seq.prefix and FY_SLOT in seq.prefix) or \ + (seq.suffix and FY_SLOT in seq.suffix): + fiscalyear_id = context.get('fiscalyear_id') + if not fiscalyear_id: + raise orm.except_orm(_('Error!'), + _('The system tried to access ' + 'a fiscal year sequence ' + 'without specifying the actual ' + 'fiscal year.')) + # search for existing fiscal year sequence + # here we behave exactly like addons/account/ir_sequence.py + for line in seq.fiscal_ids: + if line.fiscalyear_id.id == fiscalyear_id: + return super(Sequence, self)\ + ._next(cr, uid, [line.sequence_id.id], context) + # no fiscal year sequence found, auto create it + if len(seq_ids) != 1: + # Where fiscal year sequences are used, we + # should always have one and only one sequence + # (which is the one associated to the journal). + # If this is not the case we'll need to investigate + # why, but we prefer to abort here instead of + # doing something potentially harmful. + raise orm.except_orm(_('Error!'), + _('The system tried to access ' + 'a fiscal year sequence ' + 'but there is more than one ' + 'sequence to choose from.')) + fiscalyear = self.pool['account.fiscalyear']\ + .browse(cr, uid, fiscalyear_id, context=context) + fy_seq_id = self\ + ._create_fy_sequence(cr, uid, seq, fiscalyear, context) + return super(Sequence, self)\ + ._next(cr, uid, [fy_seq_id], context) return super(Sequence, self)._next(cr, uid, seq_ids, context) From 1d05c67787fa605e636b0fa7c7d43140eb606f9e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phane=20Bidoul?= Date: Tue, 25 Nov 2014 19:16:31 +0100 Subject: [PATCH 8/8] [IMP] account_auto_fy_sequence: check that the sequence has not been used... when changing from %(year)s to %(fy)s on an existing sequence. --- account_auto_fy_sequence/__openerp__.py | 2 ++ .../models/ir_sequence.py | 29 +++++++++++++++++++ 2 files changed, 31 insertions(+) diff --git a/account_auto_fy_sequence/__openerp__.py b/account_auto_fy_sequence/__openerp__.py index 04533616f..de6ba5da3 100644 --- a/account_auto_fy_sequence/__openerp__.py +++ b/account_auto_fy_sequence/__openerp__.py @@ -40,6 +40,8 @@ already been used for the current fiscal year, make sure to manually create the fiscal year sequence for the current fiscal year and initialize it's next number to the correct value. + For this reason, the module will forbid the user to change + a sequence from %(year)s to %(fy)s if it's next number is > 1. """, 'author': 'ACSONE SA/NV', 'website': 'http://acsone.eu', diff --git a/account_auto_fy_sequence/models/ir_sequence.py b/account_auto_fy_sequence/models/ir_sequence.py index b6b929656..855e1370e 100644 --- a/account_auto_fy_sequence/models/ir_sequence.py +++ b/account_auto_fy_sequence/models/ir_sequence.py @@ -27,6 +27,7 @@ from openerp.osv import orm from openerp.tools.translate import _ FY_SLOT = '%(fy)s' +YEAR_SLOT = '%(year)s' class Sequence(orm.Model): @@ -95,3 +96,31 @@ class Sequence(orm.Model): return super(Sequence, self)\ ._next(cr, uid, [fy_seq_id], context) return super(Sequence, self)._next(cr, uid, seq_ids, context) + + def write(self, cr, uid, ids, vals, context=None): + if isinstance(ids, (int, long)): + ids = [ids] + new_prefix = vals.get('prefix') + new_suffix = vals.get('suffix') + if (new_prefix and FY_SLOT in new_prefix) or \ + (new_suffix and FY_SLOT in new_suffix): + for seq in self.browse(cr, uid, ids, context=context): + if (seq.prefix and + YEAR_SLOT in seq.prefix and + new_prefix and + FY_SLOT in new_prefix) or \ + (seq.suffix and + YEAR_SLOT in seq.suffix and + new_suffix and + FY_SLOT in new_suffix): + if seq.number_next > 1 or vals.get('number_next', 1) > 1: + # we are converting from %(year)s to %(fy)s + # and the next number is > 1; this means the + # sequence has already been used. + raise orm.except_orm(_('Error!'), + _('You cannot change from ' + '%s to %s ' + 'for a sequence with ' + 'next number > 1' % + (YEAR_SLOT, FY_SLOT))) + return super(Sequence, self).write(cr, uid, ids, vals, context=context)