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.6 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. city_id = fields.Many2one(
  8. 'res.city',
  9. compute='_compute_address',
  10. inverse='_inverse_city_id',
  11. string="City"
  12. )
  13. zip_id = fields.Many2one(
  14. 'res.better.zip',
  15. string='ZIP Location',
  16. compute='_compute_address',
  17. inverse='_inverse_zip_id',
  18. oldname="better_zip_id",
  19. help='Use the city name or the zip code to search the location',
  20. )
  21. # In order to keep the same logic used in odoo, fields must be computed
  22. # and inversed, not related. This way, we can ensure that it works
  23. # correctly on changes and inconsistencies cannot happen.
  24. # When you make the fields related, the constrains added in res.partner
  25. # will fail. because when you change the city_id in the company, you are
  26. # effectively changing it in the partner. The constrains on the partner
  27. # are evaluated before the inverse methods update the other fields (city,
  28. # etc..). And we need constrains in the partner to ensure consistency.
  29. # So, as a conclusion, address fields are very related to each other.
  30. # Either you make them all related to the partner in company, or you
  31. # don't for all of them. But mixing methods produces inconsistencies.
  32. country_enforce_cities = fields.Boolean(
  33. related='country_id.enforce_cities'
  34. )
  35. def _get_company_address_fields(self, partner):
  36. res = super(ResCompany, self)._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.zip = self.zip_id.name
  50. self.city_id = self.zip_id.city_id
  51. self.city = self.zip_id.city
  52. self.country_id = self.zip_id.country_id
  53. if self.country_id.enforce_cities:
  54. self.state_id = self.city_id.state_id
  55. else:
  56. self.state_id = self.zip_id.state_id
  57. @api.onchange('state_id')
  58. def onchange_state_id(self):
  59. if self.state_id.country_id:
  60. self.country_id = self.state_id.country_id.id