You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

48 lines
1.6 KiB

  1. # © 2018 Forest and Biomass Romania SA
  2. # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
  3. from odoo import api, fields, models
  4. class AccountGroup(models.Model):
  5. _inherit = 'account.group'
  6. group_child_ids = fields.One2many(
  7. comodel_name='account.group',
  8. inverse_name='parent_id',
  9. string='Child Groups')
  10. level = fields.Integer(
  11. string='Level',
  12. compute='_compute_level',
  13. store=True)
  14. account_ids = fields.One2many(
  15. comodel_name='account.account',
  16. inverse_name='group_id',
  17. string="Accounts")
  18. compute_account_ids = fields.Many2many(
  19. 'account.account',
  20. compute='_compute_group_accounts',
  21. string="Accounts", store=True)
  22. @api.multi
  23. @api.depends('parent_id')
  24. def _compute_level(self):
  25. for group in self:
  26. level = 0
  27. new_group = group
  28. while new_group.parent_id:
  29. level += 1
  30. new_group = new_group.parent_id
  31. group.level = level
  32. @api.multi
  33. @api.depends('code_prefix', 'account_ids', 'account_ids.code',
  34. 'group_child_ids', 'group_child_ids.account_ids.code')
  35. def _compute_group_accounts(self):
  36. account_obj = self.env['account.account']
  37. accounts = account_obj.search([])
  38. for group in self:
  39. prefix = group.code_prefix if group.code_prefix else group.name
  40. gr_acc = accounts.filtered(
  41. lambda a: a.code.startswith(prefix)).ids
  42. group.compute_account_ids = [(6, 0, gr_acc)]