From 7e9009f93a76f5cf3694d17a41d272083e613f90 Mon Sep 17 00:00:00 2001 From: wan Date: Wed, 9 Jan 2019 15:31:20 +0000 Subject: [PATCH] [IMP] base: choice of state/country easier in banks and partners OPW 1891131 closes #27497 When creating a new bank account/partner address, the view displays the 'state 'field before the 'country_id' field, which naturally leads users to select the state before selecting the country. But there was problem: in order to select a state, one had to first select a country, which was confusing. Done: - allow the user to select the state before the country (without restriction on country) - remove the state from the form when the country changes, if the state (name) is not in the country - automatically set the country when the state is set - always display the country code in the name_get of res.country.state, to deal with the case where different countries have a state with the same name --- odoo/addons/base/models/res_bank.py | 12 +++++++++++- odoo/addons/base/models/res_country.py | 7 +++++++ odoo/addons/base/models/res_partner.py | 13 ++++++++----- 3 files changed, 26 insertions(+), 6 deletions(-) diff --git a/odoo/addons/base/models/res_bank.py b/odoo/addons/base/models/res_bank.py index 88ff7b7984c..1658dacb8a1 100644 --- a/odoo/addons/base/models/res_bank.py +++ b/odoo/addons/base/models/res_bank.py @@ -26,7 +26,7 @@ class Bank(models.Model): street2 = fields.Char() zip = fields.Char() city = fields.Char() - state = fields.Many2one('res.country.state', 'Fed. State', domain="[('country_id', '=', country)]") + state = fields.Many2one('res.country.state', 'Fed. State', domain="[('country_id', '=?', country)]") country = fields.Many2one('res.country') email = fields.Char() phone = fields.Char() @@ -52,6 +52,16 @@ class Bank(models.Model): domain = ['&'] + domain bank_ids = self._search(domain + args, limit=limit, access_rights_uid=name_get_uid) return self.browse(bank_ids).name_get() + + @api.onchange('country') + def _onchange_country_id(self): + if self.country and self.country != self.state.country_id: + self.state = False + + @api.onchange('state') + def _onchange_state(self): + if self.state.country_id: + self.country = self.state.country_id class ResPartnerBank(models.Model): diff --git a/odoo/addons/base/models/res_country.py b/odoo/addons/base/models/res_country.py index 3c6434cf18f..f1bcf72ed3a 100644 --- a/odoo/addons/base/models/res_country.py +++ b/odoo/addons/base/models/res_country.py @@ -128,3 +128,10 @@ class CountryState(models.Model): search_domain.append(('id', 'not in', first_state_ids)) state_ids = first_state_ids + self._search(search_domain + args, limit=limit, access_rights_uid=name_get_uid) return [(state.id, state.display_name) for state in self.browse(state_ids)] + + @api.multi + def name_get(self): + result = [] + for record in self: + result.append((record.id, "{} ({})".format(record.name, record.country_id.code))) + return result diff --git a/odoo/addons/base/models/res_partner.py b/odoo/addons/base/models/res_partner.py index f34bcb6d928..f717f32f413 100644 --- a/odoo/addons/base/models/res_partner.py +++ b/odoo/addons/base/models/res_partner.py @@ -191,7 +191,7 @@ class Partner(models.Model): street2 = fields.Char() zip = fields.Char(change_default=True) city = fields.Char() - state_id = fields.Many2one("res.country.state", string='State', ondelete='restrict') + state_id = fields.Many2one("res.country.state", string='State', ondelete='restrict', domain="[('country_id', '=?', country_id)]") country_id = fields.Many2one('res.country', string='Country', ondelete='restrict') email = fields.Char() email_formatted = fields.Char( @@ -364,10 +364,13 @@ class Partner(models.Model): @api.onchange('country_id') def _onchange_country_id(self): - if self.country_id: - return {'domain': {'state_id': [('country_id', '=', self.country_id.id)]}} - else: - return {'domain': {'state_id': []}} + if self.country_id and self.country_id != self.state_id.country_id: + self.state_id = False + + @api.onchange('state_id') + def _onchange_state(self): + if self.state_id.country_id: + self.country_id = self.state_id.country_id @api.onchange('email') def onchange_email(self):