Add script to create account groups

This commit is contained in:
Alexis de Lattre
2018-11-22 21:25:40 +01:00
parent 441df56422
commit 49e9cd0d06

View File

@@ -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'