res_company.py 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. # -*- coding: utf-8 -*-
  2. """Classes extending the populate factory for Companies and related models.
  3. Only adding specificities of basic accounting applications.
  4. """
  5. from odoo import models, _
  6. from odoo.tools import populate
  7. from odoo.exceptions import UserError
  8. import logging
  9. from functools import lru_cache
  10. _logger = logging.getLogger(__name__)
  11. class ResCompany(models.Model):
  12. """Populate factory part for the accountings applications of res.company."""
  13. _inherit = "res.company"
  14. def _populate(self, size):
  15. @lru_cache()
  16. def search_coa_ids(currency_id):
  17. return self.env['account.chart.template'].search([('currency_id', '=', currency_id)])
  18. records = super()._populate(size)
  19. _logger.info('Loading Chart Template')
  20. default_chart_templates = self.env['account.chart.template'].search([], limit=1)
  21. if not default_chart_templates:
  22. # TODO install l10n_generic_coa ?
  23. raise UserError(_(
  24. "At least one localization is needed to be installed in order to populate the "
  25. "database with accounting"
  26. ))
  27. random = populate.Random('res.company+chart_template_selector')
  28. # Load the a chart of accounts matching the currency of the company for the 3 first created companies
  29. # We are loading an existing CoA and not populating it because:
  30. # * it reflects best real use cases.
  31. # * it allows checking reports by localization
  32. # * the config is complete with try_loading(), no need to adapt when the model changes
  33. # * it is way easier :-)
  34. # We are loading only for 3 companies because:
  35. # * It takes a few hundreds of a second to create account.move records in batch.
  36. # Because we want to have a lot of entries for at least one company (in order to test
  37. # reports, functions and widgets performances for instance), we can't afford to do it for
  38. # a lot of companies.
  39. # * it would be useless to have entries for all the companies, we can already do everything with
  40. # entries in only a few (but multiple) companies.
  41. # Note that we can still populate some new records on top of the CoA if it makes sense,
  42. # like account.journal for instance.
  43. for company in records[:3]:
  44. chart_templates_cur = search_coa_ids(company.currency_id.id) or default_chart_templates
  45. random.choice(chart_templates_cur).with_company(company.id).try_loading()
  46. return records