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.

70 lines
2.5 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.partner_id.city_id = company.city_id
  43. def _inverse_zip_id(self):
  44. for company in self:
  45. company.partner_id.zip_id = company.zip_id
  46. @api.onchange('zip_id')
  47. def _onchange_zip_id(self):
  48. if self.zip_id:
  49. self.update({
  50. 'zip': self.zip_id.name,
  51. 'city_id': self.zip_id.city_id,
  52. 'city': self.zip_id.city_id.name,
  53. 'country_id': self.zip_id.city_id.country_id,
  54. 'state_id': self.zip_id.city_id.state_id,
  55. })
  56. @api.onchange('state_id')
  57. def _onchange_state_id(self):
  58. if self.state_id.country_id:
  59. self.country_id = self.state_id.country_id.id