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.

65 lines
2.4 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", inverse_name="parent_id", string="Child Groups"
  8. )
  9. level = fields.Integer(string="Level", compute="_compute_level", store=True)
  10. account_ids = fields.One2many(
  11. comodel_name="account.account", inverse_name="group_id", string="Accounts"
  12. )
  13. compute_account_ids = fields.Many2many(
  14. "account.account",
  15. compute="_compute_group_accounts",
  16. string="Compute accounts",
  17. store=True,
  18. )
  19. complete_name = fields.Char("Full Name", compute="_compute_complete_name")
  20. complete_code = fields.Char("Full Code", compute="_compute_complete_code")
  21. @api.depends("name", "parent_id.complete_name")
  22. def _compute_complete_name(self):
  23. """ Forms complete name of location from parent location to child location. """
  24. if self.parent_id.complete_name:
  25. self.complete_name = "{}/{}".format(self.parent_id.complete_name, self.name)
  26. else:
  27. self.complete_name = self.name
  28. @api.depends("code_prefix", "parent_id.complete_code")
  29. def _compute_complete_code(self):
  30. """ Forms complete code of location from parent location to child location. """
  31. if self.parent_id.complete_code:
  32. self.complete_code = "{}/{}".format(
  33. self.parent_id.complete_code, self.code_prefix
  34. )
  35. else:
  36. self.complete_code = self.code_prefix
  37. @api.depends("parent_id", "parent_id.level")
  38. def _compute_level(self):
  39. for group in self:
  40. if not group.parent_id:
  41. group.level = 0
  42. else:
  43. group.level = group.parent_id.level + 1
  44. @api.depends(
  45. "code_prefix",
  46. "account_ids",
  47. "account_ids.code",
  48. "group_child_ids",
  49. "group_child_ids.account_ids.code",
  50. )
  51. def _compute_group_accounts(self):
  52. account_obj = self.env["account.account"]
  53. accounts = account_obj.search([])
  54. for group in self:
  55. prefix = group.code_prefix if group.code_prefix else group.name
  56. gr_acc = accounts.filtered(lambda a: a.code.startswith(prefix)).ids
  57. group.compute_account_ids = [(6, 0, gr_acc)]