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.

50 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. 'COUNTRY environment variable with country code is not '
  35. 'set and no localization module is marked to be '
  36. 'installed.'))
  37. country_code = l10n_to_install.name.split('l10n_')[1][:2].upper()
  38. country = self.env['res.country'].search([
  39. ('code', 'ilike', country_code)], limit=1)
  40. if not country:
  41. raise ValidationError(_(
  42. 'Country code %s was not found. Please use a valid two-letter '
  43. 'ISO 3166 code.'))
  44. self.env.ref('base.main_company').write({'country_id': country.id})