From 49e9cd0d06c1e028e764343f9cd7f626b5afa90f Mon Sep 17 00:00:00 2001 From: Alexis de Lattre Date: Thu, 22 Nov 2018 21:25:40 +0100 Subject: [PATCH] Add script to create account groups --- account_usability/account.py | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/account_usability/account.py b/account_usability/account.py index b38f35b..eb2f74d 100644 --- a/account_usability/account.py +++ b/account_usability/account.py @@ -253,6 +253,36 @@ class AccountAccount(models.Model): logger.info("END of the script 'fix bank and cash account types'") return True + def create_account_groups(self, level=2, name_prefix=u'Compte ', root_name='Plan comptable'): + '''Should be launched by a script. Make sure the account_group module is installed + (the account_usability module doesn't depend on it currently''' + # TODO: convert to multi-company + assert level >= 1 + assert isinstance(level, int) + ago = self.env['account.group'] + groups = ago.search([]) + if groups: + raise UserError(_("Some account groups already exists")) + accounts = self.search([('company_id', '=', self.env.user.company_id.id)]) + struct = {'childs': {}} + for account in accounts: + assert len(account.code) > level + n = 1 + parent = struct + gparent = False + while n <= level: + group_code = account.code[:n] + if group_code not in parent['childs']: + new_group = ago.create({ + 'name': u'%s%s' % (name_prefix, group_code), + 'parent_id': gparent and gparent.id or False, + }) + parent['childs'][group_code] = {'obj': new_group, 'childs': {}} + parent = parent['childs'][group_code] + gparent = parent['obj'] + n += 1 + account.group_id = gparent.id + class AccountAnalyticAccount(models.Model): _inherit = 'account.analytic.account'