diff --git a/odoo/addons/base/models/res_partner.py b/odoo/addons/base/models/res_partner.py index 4ecddf61cf9..c04d8219de3 100644 --- a/odoo/addons/base/models/res_partner.py +++ b/odoo/addons/base/models/res_partner.py @@ -48,6 +48,7 @@ class FormatAddressMixin(models.AbstractModel): def _view_get_address(self, arch): # consider the country of the user, not the country of the partner we want to display address_view_id = self.env.company.country_id.address_view_id.sudo() + address_format = self.env.company.country_id.address_format if address_view_id and not self._context.get('no_address_format') and (not address_view_id.model or address_view_id.model == self._name): #render the partner address accordingly to address_view_id for address_node in arch.xpath("//div[hasclass('o_address_format')]"): @@ -61,6 +62,31 @@ class FormatAddressMixin(models.AbstractModel): except ValueError: return arch address_node.getparent().replace(address_node, sub_arch) + elif address_format and not self._context.get('no_address_format'): + # For the zip, city and state fields we need to move them around in order to follow the country address format. + # The purpose of this is to help the user by following a format he is used to. + city_line = [line.split(' ') for line in address_format.split('\n') if 'city' in line] + if city_line: + field_order = [field.replace('%(', '').replace(')s', '') for field in city_line[0]] + for address_node in arch.xpath("//div[hasclass('o_address_format')]"): + concerned_fields = {'zip', 'city', 'state_id'} - {field_order[0]} + current_field = address_node.find(f".//field[@name='{field_order[0]}']") + # First loop into the fields displayed in the address_format, and order them. + for field in field_order[1:]: + if field in ('state_code', 'state_name'): + field = 'state_id' + previous_field = current_field + current_field = address_node.find(f".//field[@name='{field}']") + if previous_field is not None and current_field is not None: + previous_field.addnext(current_field) + concerned_fields -= {field} + # Add the remaining fields in 'concerned_fields' at the end, after the others + for field in concerned_fields: + previous_field = current_field + current_field = address_node.find(f".//field[@name='{field}']") + if previous_field is not None and current_field is not None: + previous_field.addnext(current_field) + return arch @api.model