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.

68 lines
2.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="Compute accounts", store=True)
  22. complete_name = fields.Char("Full Name",
  23. compute='_compute_complete_name')
  24. complete_code = fields.Char("Full Code",
  25. compute='_compute_complete_code')
  26. @api.depends('name', 'parent_id.complete_name')
  27. def _compute_complete_name(self):
  28. """ Forms complete name of location from parent location to child location. """
  29. if self.parent_id.complete_name:
  30. self.complete_name = '%s/%s' % (self.parent_id.complete_name,
  31. self.name)
  32. else:
  33. self.complete_name = self.name
  34. @api.depends('code_prefix', 'parent_id.complete_code')
  35. def _compute_complete_code(self):
  36. """ Forms complete code of location from parent location to child location. """
  37. if self.parent_id.complete_code:
  38. self.complete_code = '%s/%s' % (self.parent_id.complete_code,
  39. self.code_prefix)
  40. else:
  41. self.complete_code = self.code_prefix
  42. @api.multi
  43. @api.depends('parent_id', 'parent_id.level')
  44. def _compute_level(self):
  45. for group in self:
  46. if not group.parent_id:
  47. group.level = 0
  48. else:
  49. group.level = group.parent_id.level + 1
  50. @api.multi
  51. @api.depends('code_prefix', 'account_ids', 'account_ids.code',
  52. 'group_child_ids', 'group_child_ids.account_ids.code')
  53. def _compute_group_accounts(self):
  54. account_obj = self.env['account.account']
  55. accounts = account_obj.search([])
  56. for group in self:
  57. prefix = group.code_prefix if group.code_prefix else group.name
  58. gr_acc = accounts.filtered(
  59. lambda a: a.code.startswith(prefix)).ids
  60. group.compute_account_ids = [(6, 0, gr_acc)]