Commit 522e0aa4 authored by Actarus's avatar Actarus
Browse files

site model: Autocompletion for address (zip, dpt, state, country); Default coutry = France

parent 132a4ed1
......@@ -27,7 +27,7 @@ class CustomerSite(models.Model):
street2 = fields.Char(string="Rue 2")
city = fields.Char(string="Ville")
zip = fields.Char(string="Code postal")
country_id = fields.Many2one("res.country", string="Pays")
country_id = fields.Many2one("res.country", string="Pays", default=lambda self: self._default_country())
state_id = fields.Many2one("res.country.state", string="Région")
department_id = fields.Many2one("res.country.department", string="Département")
......@@ -39,6 +39,8 @@ class CustomerSite(models.Model):
# Computations and actions
# ========================
# Partners and contacts
@api.onchange('partner_id')
def _onchange_partner_id(site):
if site.contact_id and site.contact_id.partner_id != site.partner_id:
......@@ -59,6 +61,64 @@ class CustomerSite(models.Model):
if site.maintenance_contact_id and site.maintenance_contact_id.partner_id and site.maintenance_contact_id.partner_id != site.maintenance_partner_id:
site.maintenance_partner_id = site.maintenance_contact_id.partner_id
# Address
# There is a inclusion relation that allows to deduce other fields from the completion of some of them:
# zip < department < state < country
# TODO: Check if onchanges are called recursively. If so, check: - When?; - Can this generate loops of some sort?
# TODO: This does not allow exceptions. See e.g. https://fr.wikipedia.org/wiki/Code_postal_en_France#Cas_particuliers
# TODO: This only works for France (and it assumes that a country called "France" is France)
# TODO: It should probably be an option to disable auto-completion by zipcode for France...
# TODO: Add cities to these?
# TODO: Not very smart when changing the country...
@api.onchange('zip')
def _onchange_zip(site):
if site.country_id.name == "France" and site.zip and len(site.zip) == 5:
department_code = site.zip[:2]
if not site.department_id or site.department_id.code != department_code:
departments = site.env['res.country.department'].search([
('code', '=', department_code),
('country_id', '=', site.country_id.id)],
limit=2)
if len(departments) == 1:
site.department_id = departments[0]
elif site.department_id:
site.department_id = False
@api.onchange('department_id')
def _onchange_department_id(site):
if site.department_id and site.department_id.state_id != site.state_id:
site.state_id = site.department_id.state_id
if site.country_id.name == "France" and site.zip and len(site.zip) == 5 and (not site.department_id or site.zip[:2] != site.department_id.code):
site.zip = False
@api.onchange('state_id')
def _onchange_state_id(site):
if site.state_id and site.state_id.country_id != site.country_id:
site.country_id = site.state_id.country_id
if site.department_id and site.department_id.state_id != site.state_id:
site.department_id = False
@api.onchange('country_id')
def _onchange_country_id(site):
if site.state_id and site.state_id.country_id != site.country_id:
site.state_id = False
# TODO: Add an option to manage default country?
def _default_country(self):
france_ids = self.env['res.country'].name_search('France', operator='=', limit=2)
print(france_ids)
if len(france_ids) != 1:
# Default exit if France does not exists or there are too many Frances.. (should not happen?)
return None
else:
print(france_ids[0])
print(france_ids[0][0])
return france_ids[0][0]
# Warnings
# TODO: Add warnings for address
@api.depends(
"partner_id", "partner_id.active",
"contact_id", "contact_id.active", "contact_id.partner_id",
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment