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.

64 lines
2.4 KiB

  1. # Copyright 2016 Nicolas Bessi, Camptocamp SA
  2. # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
  3. from odoo import models, fields, api
  4. class ResCompany(models.Model):
  5. _inherit = 'res.company'
  6. city_id = fields.Many2one(
  7. 'res.city',
  8. compute='_compute_address',
  9. inverse='_inverse_city_id',
  10. string="City"
  11. )
  12. zip_id = fields.Many2one(
  13. 'res.better.zip',
  14. string='ZIP Location',
  15. compute='_compute_address',
  16. inverse='_inverse_zip_id',
  17. oldname="better_zip_id",
  18. help='Use the city name or the zip code to search the location',
  19. )
  20. # In order to keep the same logic used in odoo, fields must be computed
  21. # and inversed, not related. This way, we can ensure that it works
  22. # correctly on changes and inconsistencies cannot happen.
  23. # When you make the fields related, the constrains added in res.partner
  24. # will fail. because when you change the city_id in the company, you are
  25. # effectively changing it in the partner. The constrains on the partner
  26. # are evaluated before the inverse methods update the other fields (city,
  27. # etc..). And we need constrains in the partner to ensure consistency.
  28. # So, as a conclusion, address fields are very related to each other.
  29. # Either you make them all related to the partner in company, or you
  30. # don't for all of them. But mixing methods produces inconsistencies.
  31. country_enforce_cities = fields.Boolean(
  32. related='country_id.enforce_cities'
  33. )
  34. def _get_company_address_fields(self, partner):
  35. res = super(ResCompany, self)._get_company_address_fields(partner)
  36. res['city_id'] = partner.city_id
  37. res['zip_id'] = partner.zip_id
  38. return res
  39. def _inverse_city_id(self):
  40. for company in self:
  41. company.partner_id.city_id = company.city_id
  42. def _inverse_zip_id(self):
  43. for company in self:
  44. company.partner_id.zip_id = company.zip_id
  45. @api.onchange('zip_id')
  46. def _onchange_zip_id(self):
  47. if self.zip_id:
  48. self.zip = self.zip_id.name
  49. self.city_id = self.zip_id.city_id
  50. self.city = self.zip_id.city
  51. self.country_id = self.zip_id.country_id
  52. if self.country_id.enforce_cities:
  53. self.state_id = self.city_id.state_id
  54. else:
  55. self.state_id = self.zip_id.state_id