res_partner.py 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. # -*- coding: utf-8 -*-
  2. # Part of Odoo. See LICENSE file for full copyright and licensing details.
  3. from odoo import api, fields, models, tools
  4. class Partner(models.Model):
  5. _inherit = ['res.partner']
  6. street_name = fields.Char(
  7. 'Street Name', compute='_compute_street_data', inverse='_inverse_street_data', store=True)
  8. street_number = fields.Char(
  9. 'House', compute='_compute_street_data', inverse='_inverse_street_data', store=True)
  10. street_number2 = fields.Char(
  11. 'Door', compute='_compute_street_data', inverse='_inverse_street_data', store=True)
  12. city_id = fields.Many2one(comodel_name='res.city', string='City ID')
  13. country_enforce_cities = fields.Boolean(related='country_id.enforce_cities')
  14. def _inverse_street_data(self):
  15. """ update self.street based on street_name, street_number and street_number2 """
  16. for partner in self:
  17. street = ((partner.street_name or '') + " " + (partner.street_number or '')).strip()
  18. if partner.street_number2:
  19. street = street + " - " + partner.street_number2
  20. partner.street = street
  21. @api.depends('street')
  22. def _compute_street_data(self):
  23. """Splits street value into sub-fields.
  24. Recomputes the fields of STREET_FIELDS when `street` of a partner is updated"""
  25. for partner in self:
  26. partner.update(tools.street_split(partner.street))
  27. def _get_street_split(self):
  28. self.ensure_one()
  29. return {
  30. 'street_name': self.street_name,
  31. 'street_number': self.street_number,
  32. 'street_number2': self.street_number2
  33. }
  34. @api.onchange('city_id')
  35. def _onchange_city_id(self):
  36. if self.city_id:
  37. self.city = self.city_id.name
  38. self.zip = self.city_id.zipcode
  39. self.state_id = self.city_id.state_id
  40. elif self._origin:
  41. self.city = False
  42. self.zip = False
  43. self.state_id = False