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.

86 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:
  44. company.with_context(
  45. skip_check_zip=True
  46. ).partner_id.city_id = company.city_id
  47. def _inverse_zip_id(self):
  48. for company in self:
  49. company.with_context(skip_check_zip=True).partner_id.zip_id = company.zip_id
  50. def _inverse_state(self):
  51. return super(
  52. ResCompany, self.with_context(skip_check_zip=True)
  53. )._inverse_state()
  54. def _inverse_country(self):
  55. return super(
  56. ResCompany, self.with_context(skip_check_zip=True)
  57. )._inverse_country()
  58. @api.onchange("zip_id")
  59. def _onchange_zip_id(self):
  60. if self.zip_id:
  61. self.update(
  62. {
  63. "zip": self.zip_id.name,
  64. "city_id": self.zip_id.city_id,
  65. "city": self.zip_id.city_id.name,
  66. "country_id": self.zip_id.city_id.country_id,
  67. "state_id": self.zip_id.city_id.state_id,
  68. }
  69. )
  70. @api.onchange("state_id")
  71. def _onchange_state_id(self):
  72. if self.state_id.country_id:
  73. self.country_id = self.state_id.country_id.id