From f4e5f0fb356b58ad2c999435f1167dee82c25894 Mon Sep 17 00:00:00 2001 From: Jairo Llopis Date: Tue, 17 May 2016 12:08:42 +0200 Subject: [PATCH] [9.0][MIG][account_renumber] Migrate to 9.0, refactor. - In v9 `account.period` has disappeared, so now the wizard asks for start and end dates. - `ir.sequence` has changed logic too, this module is now adapted. - Old tests had to be run through the CLI. New tests work with standard test system. - Old menu parent does not exist anymore, so it is now moved to other place. - Old license & copyright headers are now updated, respecting old copyrights where found. - `.pot` file removed. - README added. Related to https://github.com/OCA/account-financial-tools/issues/311#issuecomment-219378169. --- account_renumber/README.rst | 81 +++++++ account_renumber/__init__.py | 26 +-- account_renumber/__openerp__.py | 55 +---- account_renumber/i18n/account_renumber.pot | 139 ----------- account_renumber/test/create_moves.py | 103 --------- account_renumber/tests/__init__.py | 5 + .../tests/test_account_renumber.py | 117 ++++++++++ account_renumber/wizard/__init__.py | 26 +-- account_renumber/wizard/wizard_renumber.py | 215 +++++++----------- .../wizard/wizard_renumber_view.xml | 86 +++---- 10 files changed, 349 insertions(+), 504 deletions(-) create mode 100644 account_renumber/README.rst delete mode 100644 account_renumber/i18n/account_renumber.pot delete mode 100644 account_renumber/test/create_moves.py create mode 100644 account_renumber/tests/__init__.py create mode 100644 account_renumber/tests/test_account_renumber.py diff --git a/account_renumber/README.rst b/account_renumber/README.rst new file mode 100644 index 000000000..bbbe9b330 --- /dev/null +++ b/account_renumber/README.rst @@ -0,0 +1,81 @@ +.. image:: https://img.shields.io/badge/licence-AGPL--3-blue.svg + :target: http://www.gnu.org/licenses/agpl-3.0-standalone.html + :alt: License: AGPL-3 + +======================= +Account Renumber Wizard +======================= + +This module extends the functionality of accounting to allow the accounting +manager to renumber account moves by date only for admin. + +The wizard, which is accesible from the "End of Period" menuitem, +lets you select journals, periods, and a starting number. When +launched, it renumbers all posted moves that match selected criteria +(after ordering them by date). + +It will recreate the sequence number for each account move +using its journal sequence, which means that: + +- Sequences per journal are supported. +- Sequences with prefixes and suffixes based on the move date are also + supported. + +Usage +===== + +To use this module, you need to: + +#. Be an accounting manager. +#. Go to *Accounting > Adviser > Renumber journal entries*. +#. Choose the *First number* of the journal entry that you want. It will be + used to start numbering from there on. +#. Choose the *Starting date* and *Ending date*, to set when you want the + process to begin and end. +#. Choose the journals where you want to perform the renumberings. +#. Press *Renumber*. + +Now, the wizard will locate all journal entries found in those journals and +dates, and start numbering them without gaps in a sequential order that starts +with the *First number* you chose and matches the entry date order. + +If no matches are found, you will be alerted. Otherwise, you will be redirected +to a view of all the entries that have been renumbered. + +.. image:: https://odoo-community.org/website/image/ir.attachment/5784_f2813bd/datas + :alt: Try me on Runbot + :target: https://runbot.odoo-community.org/runbot/92/8.0 + +Bug Tracker +=========== + +Bugs are tracked on `GitHub Issues +`_. In case of trouble, +please check there if your issue has already been reported. If you spotted it +first, help us smashing it by providing a detailed and welcomed feedback. + +Credits +======= + +Contributors +------------ + +* Pedro M. Baeza +* Jordi Llinares +* Joaquín Gutiérrez +* Jairo Llopis + +Maintainer +---------- + +.. image:: https://odoo-community.org/logo.png + :alt: Odoo Community Association + :target: https://odoo-community.org + +This module is maintained by the OCA. + +OCA, or the Odoo Community Association, is a nonprofit organization whose +mission is to support the collaborative development of Odoo features and +promote its widespread use. + +To contribute to this module, please visit https://odoo-community.org. diff --git a/account_renumber/__init__.py b/account_renumber/__init__.py index f781080db..29bf39f36 100644 --- a/account_renumber/__init__.py +++ b/account_renumber/__init__.py @@ -1,26 +1,6 @@ # -*- coding: utf-8 -*- -############################################################################## -# -# OpenERP - Account renumber wizard -# Copyright (C) 2009 Pexego Sistemas Informáticos. All Rights Reserved -# $Id$ -# -# 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 . -# -############################################################################## -""" -Account renumber wizard -""" +# © 2009 Pexego Sistemas Informáticos. All Rights Reserved +# © 2016 Jairo Llopis +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). from . import wizard diff --git a/account_renumber/__openerp__.py b/account_renumber/__openerp__.py index 6880edaf8..da17ac157 100644 --- a/account_renumber/__openerp__.py +++ b/account_renumber/__openerp__.py @@ -1,59 +1,22 @@ # -*- coding: utf-8 -*- -############################################################################## -# -# OpenERP - Account renumber wizard -# Copyright (C) 2009 Pexego Sistemas Informáticos. All Rights Reserved -# Copyright (c) 2013 Servicios Tecnológicos Avanzados -# (http://www.serviciosbaeza.com) -# Pedro Manuel Baeza -# Copyright (c) 2013 Joaquin Gutierrez (http://www.gutierrezweb.es) -# $Id$ -# -# 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 . -# -############################################################################## +# © 2009 Pexego Sistemas Informáticos. All Rights Reserved +# © 2013 Pedro Manuel Baeza +# © 2013 Joaquin Gutierrez +# © 2016 Jairo Llopis +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). { - 'name': "Account renumber wizard", - 'version': "8.0.1.0.0", - 'author': "Pexego,Odoo Community Association (OCA)", + 'name': "Account Renumber Wizard", + 'version': "9.0.1.0.0", + 'author': "Pexego,Tecnativa,Odoo Community Association (OCA)", 'website': "http://www.pexego.es", 'category': "Accounting & Finance", - 'contributors': ['Pedro M. Baeza', 'Jordi Llinares', 'Joaquín Gutierrez'], - 'description': """ -This module adds a wizard to renumber account moves by date only for admin. -=========================================================================== - -The wizard, which is accesible from the "End of Period" menuitem, -lets you select journals, periods, and a starting number. When -launched, it renumbers all posted moves that match selected criteria -(after ordering them by date). - -It will recreate the sequence number for each account move -using its journal sequence, which means that: - - Sequences per journal are supported. - - Sequences with prefixes and suffixes based on the move - date are also supported. - """, "license": "AGPL-3", "depends": [ 'account', ], - "demo": [], "data": [ 'wizard/wizard_renumber_view.xml', ], - 'installable': False, - 'auto_install': False, + 'installable': True, } diff --git a/account_renumber/i18n/account_renumber.pot b/account_renumber/i18n/account_renumber.pot deleted file mode 100644 index 9ad454f51..000000000 --- a/account_renumber/i18n/account_renumber.pot +++ /dev/null @@ -1,139 +0,0 @@ -# Translation of OpenERP Server. -# This file contains the translation of the following modules: -# * account_renumber -# -msgid "" -msgstr "" -"Project-Id-Version: OpenERP Server 7.0\n" -"Report-Msgid-Bugs-To: \n" -"POT-Creation-Date: 2014-04-14 12:33+0000\n" -"PO-Revision-Date: 2014-04-14 12:33+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_renumber -#: code:_description:0 -#, python-format -msgid "Account renumber wizard" -msgstr "" - -#. module: account_renumber -#: view:wizard.renumber:0 -msgid "Cancel" -msgstr "" - -#. module: account_renumber -#: field:wizard.renumber,number_next:0 -msgid "First Number" -msgstr "" - -#. module: account_renumber -#: help:wizard.renumber,period_ids:0 -msgid "Fiscal periods to renumber" -msgstr "" - -#. module: account_renumber -#: view:wizard.renumber:0 -msgid "General Data" -msgstr "" - -#. module: account_renumber -#: selection:wizard.renumber,state:0 -msgid "Initial" -msgstr "" - -#. module: account_renumber -#: help:wizard.renumber,number_next:0 -msgid "Journal sequences will start counting on this number" -msgstr "" - -#. module: account_renumber -#: view:wizard.renumber:0 -#: field:wizard.renumber,journal_ids:0 -msgid "Journals" -msgstr "" - -#. module: account_renumber -#: view:wizard.renumber:0 -msgid "Journals and periods to consider" -msgstr "" - -#. module: account_renumber -#: help:wizard.renumber,journal_ids:0 -msgid "Journals to renumber" -msgstr "" - -#. module: account_renumber -#: code:addons/account_renumber/wizard/wizard_renumber.py:86 -#, python-format -msgid "No Data Available" -msgstr "" - -#. module: account_renumber -#: code:addons/account_renumber/wizard/wizard_renumber.py:87 -#, python-format -msgid "No records found for your selection!" -msgstr "" - -#. module: account_renumber -#: view:wizard.renumber:0 -#: field:wizard.renumber,period_ids:0 -msgid "Periods" -msgstr "" - -#. module: account_renumber -#: view:wizard.renumber:0 -msgid "Posted moves from those journals will be sorted by date and then assigned sequential numbers using their journal sequence." -msgstr "" - -#. module: account_renumber -#: view:wizard.renumber:0 -msgid "Renumber" -msgstr "" - -#. module: account_renumber -#: view:wizard.renumber:0 -msgid "Renumber Account Moves" -msgstr "" - -#. module: account_renumber -#: model:ir.actions.act_window,name:account_renumber.action_account_renumber -#: model:ir.ui.menu,name:account_renumber.menu_account_renumber -msgid "Renumber journal entries" -msgstr "" - -#. module: account_renumber -#: code:addons/account_renumber/wizard/wizard_renumber.py:130 -#, python-format -msgid "Renumbered account moves" -msgstr "" - -#. module: account_renumber -#: selection:wizard.renumber,state:0 -msgid "Renumbering" -msgstr "" - -#. module: account_renumber -#: view:wizard.renumber:0 -msgid "This wizard will help you renumber entries in one or more journals." -msgstr "" - -#. module: account_renumber -#: view:wizard.renumber:0 -msgid "or" -msgstr "" - -#. module: account_renumber -#: field:wizard.renumber,state:0 -msgid "unknown" -msgstr "" - -#. module: account_renumber -#: model:ir.model,name:account_renumber.model_wizard_renumber -msgid "wizard.renumber" -msgstr "" - diff --git a/account_renumber/test/create_moves.py b/account_renumber/test/create_moves.py deleted file mode 100644 index 996855acc..000000000 --- a/account_renumber/test/create_moves.py +++ /dev/null @@ -1,103 +0,0 @@ -# -*- coding: utf-8 -*- -############################################################################## -# -# OpenERP - Account renumber wizard -# Copyright (C) 2009 Pexego Sistemas Informáticos. -# -# 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 . -# -############################################################################## -import sys -import xmlrpclib -import logging - -logger = logging.getLogger("create_lots_of_account_moves") - - -def create_lots_of_account_moves(dbname, user, passwd, howmany): - """Small Odoo function that will create lots of account moves on the - selected database, that can later be used for testing the renumber wizard. - Note: The database must have demo data, and a fiscal year 2009 created. - """ - url_template = "http://%s:%s/xmlrpc/%s" - server = "localhost" - port = 8069 - - login_facade = xmlrpclib.ServerProxy( - url_template % (server, port, 'common')) - user_id = login_facade.login(dbname, user, passwd) - object_facade = xmlrpclib.ServerProxy( - url_template % (server, port, 'object')) - - for i in range(1, howmany): - # Create one account move - move_id = object_facade.execute(dbname, user_id, passwd, - 'account.move', 'create', { - 'ref': 'Test%s' % i, - 'type': 'journal_voucher', - 'journal_id': 5, - 'line_id': [ - (0, 0, { - 'analytic_account_id': False, - 'currency_id': False, - 'tax_amount': False, - 'account_id': 2, - 'partner_id': False, - 'tax_code_id': False, - 'credit': 1000.0, - 'date_maturity': False, - 'debit': False, - 'amount_currency': False, - 'ref': False, - 'name': 'Test_l1' - }), - (0, 0, { - 'analytic_account_id': False, - 'currency_id': False, - 'tax_amount': False, - 'account_id': 4, - 'partner_id': False, - 'tax_code_id': False, - 'credit': False, - 'date_maturity': False, - 'debit': 1000.0, - 'amount_currency': False, - 'ref': False, - 'name': 'Test_l2'}) - ], - 'period_id': 1, - 'date': '2009-01-%s' % ( - (i % 31) or 1 - ), - 'partner_id': False, - 'to_check': 0 - }, - {}) - - # Validate the move - object_facade.execute(dbname, user_id, passwd, - u'account.move', 'button_validate', - [move_id], {}) - -# ------------------------------------------------------------------------ -# ------------------------------------------------------------------------ -# ------------------------------------------------------------------------ - -if __name__ == "__main__": - if len(sys.argv) < 5: - logger.info(u"Usage: %s " % - sys.argv[0]) - else: - create_lots_of_account_moves( - sys.argv[1], sys.argv[2], sys.argv[3], int(sys.argv[4])) diff --git a/account_renumber/tests/__init__.py b/account_renumber/tests/__init__.py new file mode 100644 index 000000000..023c444c8 --- /dev/null +++ b/account_renumber/tests/__init__.py @@ -0,0 +1,5 @@ +# -*- coding: utf-8 -*- +# © 2016 Jairo Llopis +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). + +from . import test_account_renumber diff --git a/account_renumber/tests/test_account_renumber.py b/account_renumber/tests/test_account_renumber.py new file mode 100644 index 000000000..54c0f03f8 --- /dev/null +++ b/account_renumber/tests/test_account_renumber.py @@ -0,0 +1,117 @@ +# -*- coding: utf-8 -*- +# © 2016 Jairo Llopis +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). + +from datetime import date +from openerp import fields +from openerp.tests.common import TransactionCase + + +class AccountRenumberCase(TransactionCase): + def setUp(self): + super(AccountRenumberCase, self).setUp() + self.today = date.today() + + self.accounts = self.env["account.account"] + for n in range(2): + self.accounts |= self.accounts.create({ + "name": "Account %d" % n, + "code": "lalala%d" % n, + "user_type_id": + self.env.ref("account.data_account_type_liquidity").id, + }) + + self.sequence = self.env["ir.sequence"].create({ + "name": u"Test Sëquence", + "implementation": "no_gap", + "prefix": "TEST/%(year)s/", + "use_date_range": True, + }) + + self.date_range = self.env["ir.sequence.date_range"].create({ + "date_from": date(self.today.year, 1, 1), + "date_to": date(self.today.year, 12, 31), + "sequence_id": self.sequence.id, + }) + + self.journal = self.env["account.journal"].create({ + "name": u"Test Jöurnal", + "type": "cash", + "sequence_id": self.sequence.id, + }) + + # Create some dummy accounting entries + moves = self.env["account.move"] + for n in range(9): + move = moves.create({ + "ref": "Test %s" % n, + "journal_id": self.journal.id, + "date": date(self.today.year, 12 - n, 1), + "line_ids": [ + (0, False, { + "account_id": self.accounts[0].id, + "name": "Line %d.1" % n, + "debit": 100, + }), + (0, False, { + "account_id": self.accounts[1].id, + "name": "Line %d.2" % n, + "credit": 100, + }), + ], + }) + move.post() + moves |= move + + # Get them sorted by default order + self.moves = moves.search([("id", "in", moves.ids)]) + + def moves_by_name(self, moves): + """Search moves sorted by name (move number, inversed).""" + return moves.search( + [("id", "in", moves.ids)], + order="name desc") + + def test_renumber_all(self): + """All moves are renumbered.""" + wizard = self.env["wizard.renumber"].create({ + "date_to": date(self.today.year, 12, 31), + }) + wizard.journal_ids = self.journal + wizard.renumber() + new_moves = self.moves_by_name(self.moves) + for n, move in enumerate(self.moves): + self.assertEqual(move, new_moves[n]) + + def test_renumber_only_one_journal(self): + """Only moves from one journal are renumbered.""" + new_journal = self.journal.copy() + self.moves[:4].write({"journal_id": new_journal.id}) + wizard = self.env["wizard.renumber"].create({ + "date_to": date(self.today.year, 12, 31), + }) + wizard.journal_ids = self.journal + renumbered_ids = wizard.renumber()["domain"][0][2] + + for move in self.moves[:4]: + self.assertNotIn(move.id, renumbered_ids) + for move in self.moves[4:]: + self.assertIn(move.id, renumbered_ids) + + def test_renumber_half_year(self): + """Only moves from the second half of the year are renumbered.""" + date_from = fields.Date.to_string(date(self.today.year, 7, 1)) + wizard = self.env["wizard.renumber"].create({ + "date_from": date_from, + "date_to": date(self.today.year, 12, 31), + }) + wizard.journal_ids = self.journal + wizard.renumber() + + expected_month = range(4, 7) + range(12, 6, -1) + expected_number = range(9, 0, -1) + for n, move in enumerate(self.moves_by_name(self.moves)): + self.assertEqual(int(move.name[-1]), expected_number[n]) + self.assertEqual( + fields.Date.from_string(move.date).month, + expected_month[n]) diff --git a/account_renumber/wizard/__init__.py b/account_renumber/wizard/__init__.py index c1ceefa32..f264c69b6 100644 --- a/account_renumber/wizard/__init__.py +++ b/account_renumber/wizard/__init__.py @@ -1,26 +1,6 @@ # -*- coding: utf-8 -*- -############################################################################## -# -# OpenERP - Account renumber wizard -# Copyright (C) 2009 Pexego Sistemas Informáticos. All Rights Reserved -# $Id$ -# -# 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 . -# -############################################################################## -""" -Account renumber wizard -""" +# © 2009 Pexego Sistemas Informáticos. All Rights Reserved +# © 2016 Jairo Llopis +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). from . import wizard_renumber diff --git a/account_renumber/wizard/wizard_renumber.py b/account_renumber/wizard/wizard_renumber.py index d7e3338aa..ff971988a 100644 --- a/account_renumber/wizard/wizard_renumber.py +++ b/account_renumber/wizard/wizard_renumber.py @@ -1,149 +1,108 @@ # -*- coding: utf-8 -*- -############################################################################## -# -# OpenERP - Account renumber wizard -# Copyright (C) 2009 Pexego Sistemas Informáticos. All Rights Reserved -# $Id$ -# -# 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 . -# -############################################################################## -from openerp.osv import fields, orm -from openerp.tools.translate import _ -from openerp import SUPERUSER_ID +# © 2009 Pexego Sistemas Informáticos. All Rights Reserved +# © 2016 Jairo Llopis +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). + import logging +from datetime import date +from openerp import _, api, exceptions, fields, models _logger = logging.getLogger(__name__) -class wizard_renumber(orm.TransientModel): +class WizardRenumber(models.TransientModel): _name = "wizard.renumber" _description = "Account renumber wizard" - _columns = { - 'journal_ids': fields.many2many('account.journal', - 'account_journal_wzd_renumber_rel', - 'wizard_id', 'journal_id', - required=True, - help="Journals to renumber", - string="Journals"), - 'period_ids': fields.many2many('account.period', - 'account_period_wzd_renumber_rel', - 'wizard_id', 'period_id', - required=True, - help='Fiscal periods to renumber', - string="Periods", ondelete='null'), - 'number_next': fields.integer('First Number', required=True, - help="Journal sequences will start " - "counting on this number"), - 'state': fields.selection([('init', 'Initial'), - ('renumber', 'Renumbering')], readonly=True) - } - _defaults = { - 'number_next': 1, - 'state': 'init' - } + date_from = fields.Date( + string="Starting date", + required=True, + default=lambda self: self._default_date_from(), + help="Start renumbering in this date, inclusive.", + ) + date_to = fields.Date( + string="Finish date", + required=True, + default=lambda self: self._default_date_to(), + help="Finish renumbering in this date, inclusive.", + ) + journal_ids = fields.Many2many( + comodel_name='account.journal', + relation='account_journal_wzd_renumber_rel', + column1='wizard_id', + column2='journal_id', + string="Journals", + required=True, + help="Journals to renumber", + ) + number_next = fields.Integer( + string='First Number', + default=1, + required=True, + help="Journal sequences will start counting on this number", + ) - ############################### - # Helper methods - ############################### + @api.model + def _default_date_from(self): + """Day 1 of current year by default.""" + return date(self._default_date_to().year, 1, 1) - def get_sequence_id_for_fiscalyear_id(self, cr, uid, sequence_id, - fiscalyear_id, context=None): + @api.model + def _default_date_to(self): + """Today by default.""" + return fields.Date.from_string(fields.Date.context_today(self)) + + @api.multi + def renumber(self): + """Renumber all the posted moves on the given journal and periods. + + :return dict: + Window action to open the renumbered moves, to review them. """ - Based on ir_sequence.get_id from the account module. - Allows us to get the real sequence for the given fiscal year. - """ - sequence = self.pool['ir.sequence'].browse(cr, uid, sequence_id, - context=context) - for line in sequence.fiscal_ids: - if line.fiscalyear_id.id == fiscalyear_id: - return line.sequence_id.id - return sequence_id + reset_sequences = self.env["ir.sequence"] + reset_ranges = self.env["ir.sequence.date_range"] - ########################################################################## - # Renumber form/action - ########################################################################## - - def renumber(self, cr, uid, ids, context=None): - """ - Action that renumbers all the posted moves on the given - journal and periods, and returns their ids. - """ - form = self.browse(cr, uid, ids[0], context=context) - period_ids = [x.id for x in form.period_ids] - journal_ids = [x.id for x in form.journal_ids] - number_next = form.number_next or 1 - if not (period_ids and journal_ids): - raise orm.except_orm(_('No Data Available'), - _('No records found for your selection!')) _logger.debug("Searching for account moves to renumber.") - move_obj = self.pool['account.move'] - sequence_obj = self.pool['ir.sequence'] - sequences_seen = [] - for period in period_ids: - move_ids = move_obj.search(cr, uid, - [('journal_id', 'in', journal_ids), - ('period_id', '=', period), - ('state', '=', 'posted')], - limit=0, order='date,id', - context=context) - if not move_ids: - continue - _logger.debug("Renumbering %d account moves.", len(move_ids)) - for move in move_obj.browse(cr, uid, move_ids, context=context): - sequence_id = self.get_sequence_id_for_fiscalyear_id( - cr, uid, - sequence_id=move.journal_id.sequence_id.id, - fiscalyear_id=move.period_id.fiscalyear_id.id - ) - if sequence_id not in sequences_seen: - sequence_obj.write(cr, SUPERUSER_ID, [sequence_id], - {'number_next': number_next}) - sequences_seen.append(sequence_id) - # Generate (using our own get_id) and write the new move number - c = {'fiscalyear_id': move.period_id.fiscalyear_id.id} - new_name = sequence_obj.next_by_id( - cr, uid, - move.journal_id.sequence_id.id, - context=c - ) - # Note: We can't just do a - # "move_obj.write(cr, uid, [move.id], {'name': new_name})" - # cause it might raise a - # ``You can't do this modification on a confirmed entry`` - # exception. - cr.execute('UPDATE account_move SET name=%s WHERE id=%s', - (new_name, move.id)) - _logger.debug("%d account moves renumbered.", len(move_ids)) - sequences_seen = [] - form.write({'state': 'renumber'}) - data_obj = self.pool['ir.model.data'] - view_ref = data_obj.get_object_reference(cr, uid, 'account', - 'view_move_tree') - view_id = view_ref and view_ref[1] or False, - res = { + move_ids = self.env['account.move'].search( + [('journal_id', 'in', self.journal_ids.ids), + ('date', '>=', self.date_from), + ('date', '<=', self.date_to), + ('state', '=', 'posted')], + order='date, id') + if not move_ids: + raise exceptions.MissingError( + _('No records found for your selection!')) + + _logger.debug("Renumbering %d account moves.", len(move_ids)) + for move in move_ids: + sequence = move.journal_id.sequence_id + if sequence not in reset_sequences: + if sequence.use_date_range: + date_range = self.env["ir.sequence.date_range"].search( + [("sequence_id", "=", sequence.id), + ("date_from", "<=", move.date), + ("date_to", ">=", move.date)] + ) + if date_range not in reset_ranges: + date_range.number_next = self.number_next + reset_ranges |= date_range + else: + sequence.number_next = self.number_next + reset_sequences |= sequence + + # Generate (using our own get_id) and write the new move number + move.name = (sequence.with_context(ir_sequence_date=move.date) + .next_by_id()) + + _logger.debug("%d account moves renumbered.", len(move_ids)) + + return { 'type': 'ir.actions.act_window', 'name': _("Renumbered account moves"), 'res_model': 'account.move', - 'domain': ("[('journal_id','in',%s), ('period_id','in',%s), " - "('state','=','posted')]" - % (journal_ids, period_ids)), + 'domain': [("id", "in", move_ids.ids)], 'view_type': 'form', 'view_mode': 'tree', - 'view_id': view_id, - 'context': context, + 'context': self.env.context, 'target': 'current', } - return res diff --git a/account_renumber/wizard/wizard_renumber_view.xml b/account_renumber/wizard/wizard_renumber_view.xml index abf02e767..b7671e68f 100644 --- a/account_renumber/wizard/wizard_renumber_view.xml +++ b/account_renumber/wizard/wizard_renumber_view.xml @@ -1,51 +1,53 @@ + - - - account_renumber.form - wizard.renumber - -
- -

This wizard will help you renumber entries in one or more journals.

-

Posted moves from those journals will be sorted by date and then assigned sequential numbers using their journal sequence.

+ + account_renumber.form + wizard.renumber + + +

This wizard will help you renumber entries in one or more journals.

+

Posted moves from those journals will be sorted by date and then assigned sequential numbers using their journal sequence.

+ - - - - - - - - - + + + -
-
-
-
- -
-
- - Renumber journal entries - wizard.renumber - form - form - - new - - + + + + +
+
+ +
+
+ + + Renumber Journal Entries + wizard.renumber + form + form + + new + + + -