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] [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)