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.

80 lines
2.9 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 models, fields, api
  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. oldname="better_zip_id",
  30. help='Use the city name or the zip code to search the location',
  31. )
  32. country_enforce_cities = fields.Boolean(
  33. related='country_id.enforce_cities'
  34. )
  35. def _get_company_address_fields(self, partner):
  36. res = super()._get_company_address_fields(partner)
  37. res['city_id'] = partner.city_id
  38. res['zip_id'] = partner.zip_id
  39. return res
  40. def _inverse_city_id(self):
  41. for company in self:
  42. company.with_context(
  43. skip_check_zip=True).partner_id.city_id = company.city_id
  44. def _inverse_zip_id(self):
  45. for company in self:
  46. company.with_context(
  47. skip_check_zip=True).partner_id.zip_id = company.zip_id
  48. def _inverse_state(self):
  49. return super(ResCompany, self.with_context(
  50. skip_check_zip=True))._inverse_state()
  51. def _inverse_country(self):
  52. return super(ResCompany, self.with_context(
  53. skip_check_zip=True))._inverse_country()
  54. @api.onchange('zip_id')
  55. def _onchange_zip_id(self):
  56. if self.zip_id:
  57. self.update({
  58. 'zip': self.zip_id.name,
  59. 'city_id': self.zip_id.city_id,
  60. 'city': self.zip_id.city_id.name,
  61. 'country_id': self.zip_id.city_id.country_id,
  62. 'state_id': self.zip_id.city_id.state_id,
  63. })
  64. @api.onchange('state_id')
  65. def _onchange_state_id(self):
  66. if self.state_id.country_id:
  67. self.country_id = self.state_id.country_id.id