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.

81 lines
3.0 KiB

  1. # Copyright 2016 Nicolas Bessi, Camptocamp SA
  2. # Copyright 2018 Tecnativa - Pedro M. Baeza
  3. # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
  4. from odoo import api, fields, models
  5. class ResCompany(models.Model):
  6. _inherit = "res.company"
  7. # In order to keep the same logic used in Odoo, fields must be computed
  8. # and inversed, not related. This way we can ensure that it works
  9. # correctly on changes and that inconsistencies cannot happen.
  10. # When you make the fields related, the constrains added in res.partner
  11. # will fail, because when you change the city_id in the company, you are
  12. # effectively changing it in the partner. The constrains on the partner
  13. # are evaluated before the inverse methods update the other fields (city,
  14. # etc..) so we need them to ensure consistency.
  15. # As a conclusion, address fields are very related to each other.
  16. # Either you make them all related to the partner in company, or you
  17. # don't for all of them. Mixing both approaches produces inconsistencies.
  18. city_id = fields.Many2one(
  19. "res.city",
  20. compute="_compute_address",
  21. inverse="_inverse_city_id",
  22. string="City ID",
  23. )
  24. zip_id = fields.Many2one(
  25. "res.city.zip",
  26. string="ZIP Location",
  27. compute="_compute_address",
  28. inverse="_inverse_zip_id",
  29. help="Use the city name or the zip code to search the location",
  30. )
  31. country_enforce_cities = fields.Boolean(
  32. related="partner_id.country_id.enforce_cities"
  33. )
  34. def _get_company_address_field_names(self):
  35. """Add to the list of field to populate in _compute_address the new
  36. ZIP field + the city that is not handled at company level in
  37. `base_address_city`.
  38. """
  39. res = super()._get_company_address_field_names()
  40. res += ["city_id", "zip_id"]
  41. return res
  42. def _inverse_city_id(self):
  43. for company in self.with_context(skip_check_zip=True):
  44. company.partner_id.city_id = company.city_id
  45. def _inverse_zip_id(self):
  46. for company in self.with_context(skip_check_zip=True):
  47. company.partner_id.zip_id = company.zip_id
  48. def _inverse_state(self):
  49. self = self.with_context(skip_check_zip=True)
  50. return super(ResCompany, self)._inverse_state()
  51. def _inverse_country(self):
  52. self = self.with_context(skip_check_zip=True)
  53. return super(ResCompany, self)._inverse_country()
  54. @api.onchange("zip_id")
  55. def _onchange_zip_id(self):
  56. if self.zip_id:
  57. self.update(
  58. {
  59. "zip": self.zip_id.name,
  60. "city_id": self.zip_id.city_id,
  61. "city": self.zip_id.city_id.name,
  62. "country_id": self.zip_id.city_id.country_id,
  63. "state_id": self.zip_id.city_id.state_id,
  64. }
  65. )
  66. @api.onchange("state_id")
  67. def _onchange_state_id(self):
  68. if self.state_id.country_id:
  69. self.country_id = self.state_id.country_id.id