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
2.0 KiB

  1. # Copyright 2016 Vauxoo
  2. # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
  3. import logging
  4. import os
  5. from odoo import _, api, models
  6. from odoo.exceptions import ValidationError
  7. _logger = logging.getLogger(__name__)
  8. class CompanyCountryConfigSettings(models.AbstractModel):
  9. _name = 'company.country.config.settings'
  10. _description = 'Company Country Configuration Settings'
  11. @api.model
  12. def load_company_country(self, country_code=None):
  13. account_installed = self.env['ir.module.module'].search([
  14. ('name', '=', 'account'),
  15. ('state', '=', 'installed'),
  16. ], limit=1)
  17. if account_installed:
  18. # If the account module is installed, that means changing the
  19. # company's country will have no effect, as the account hook was
  20. # already run and an l10n module was already been installed
  21. _logger.info("account module already installed, skipping")
  22. return
  23. if not country_code:
  24. country_code = os.environ.get('COUNTRY')
  25. if country_code == "":
  26. self.env.ref('base.main_company').write({'country_id': False})
  27. return
  28. if not country_code:
  29. l10n_to_install = self.env['ir.module.module'].search([
  30. ('state', '=', 'to install'),
  31. ('name', '=like', 'l10n_%')], limit=1)
  32. if not l10n_to_install:
  33. raise ValidationError(
  34. _('Error COUNTRY environment variable with country code '
  35. 'not defined and no localization found in pool.'))
  36. country_code = l10n_to_install.name.split('l10n_')[1][:2].upper()
  37. country = self.env['res.country'].search([
  38. ('code', 'ilike', country_code)], limit=1)
  39. if not country:
  40. raise ValidationError(
  41. _('Country code %s not found. Use ISO 3166 codes 2 letters'))
  42. self.env.ref('base.main_company').write({'country_id': country.id})