From c003cac091c829878fda8eed5e2962afb93a2be3 Mon Sep 17 00:00:00 2001 From: "Maximilien (malb)" Date: Wed, 15 Nov 2023 08:25:00 +0100 Subject: [PATCH] [IMP] l10n_br_website_sale: vat required + identification type latam At the moment, there are two possible flows to modify the personal information of the user. Either during the first checkout (website_sale) or by modifying the information on the account present on the website (portal). For both of these flows, when the client and the website are Brazilian, the input for the VAT must be required. This commit will override the corresponding function to make the input required under these conditions. Also, we have added a new field on the two forms. Just before the vat input, we can now find the l10n_latam_identification_type_id. Task: 3576516 Part-of: odoo/odoo#142165 --- addons/l10n_br_website_sale/__init__.py | 2 + addons/l10n_br_website_sale/__manifest__.py | 18 ++++++++ .../controllers/__init__.py | 3 ++ .../l10n_br_website_sale/controllers/main.py | 34 +++++++++++++++ .../controllers/portal.py | 42 +++++++++++++++++++ addons/l10n_br_website_sale/views/portal.xml | 28 +++++++++++++ .../l10n_br_website_sale/views/templates.xml | 31 ++++++++++++++ addons/portal/controllers/portal.py | 16 +++++-- 8 files changed, 170 insertions(+), 4 deletions(-) create mode 100644 addons/l10n_br_website_sale/__init__.py create mode 100644 addons/l10n_br_website_sale/__manifest__.py create mode 100644 addons/l10n_br_website_sale/controllers/__init__.py create mode 100644 addons/l10n_br_website_sale/controllers/main.py create mode 100644 addons/l10n_br_website_sale/controllers/portal.py create mode 100644 addons/l10n_br_website_sale/views/portal.xml create mode 100644 addons/l10n_br_website_sale/views/templates.xml diff --git a/addons/l10n_br_website_sale/__init__.py b/addons/l10n_br_website_sale/__init__.py new file mode 100644 index 00000000000..2e55169e4f2 --- /dev/null +++ b/addons/l10n_br_website_sale/__init__.py @@ -0,0 +1,2 @@ +# Part of Odoo. See LICENSE file for full copyright and licensing details. +from . import controllers diff --git a/addons/l10n_br_website_sale/__manifest__.py b/addons/l10n_br_website_sale/__manifest__.py new file mode 100644 index 00000000000..389c919fa40 --- /dev/null +++ b/addons/l10n_br_website_sale/__manifest__.py @@ -0,0 +1,18 @@ +# Part of Odoo. See LICENSE file for full copyright and licensing details. +{ + 'name': 'Brazil - Website Sale', + 'version': '1.0', + 'description': 'Bridge Website Sale for Brazil', + 'category': 'Localization', + 'depends': [ + 'l10n_br', + 'website_sale', + ], + 'data': [ + 'views/portal.xml', + 'views/templates.xml', + ], + 'installable': True, + 'auto_install': True, + 'license': 'LGPL-3', +} diff --git a/addons/l10n_br_website_sale/controllers/__init__.py b/addons/l10n_br_website_sale/controllers/__init__.py new file mode 100644 index 00000000000..f30051a1fd9 --- /dev/null +++ b/addons/l10n_br_website_sale/controllers/__init__.py @@ -0,0 +1,3 @@ +# Part of Odoo. See LICENSE file for full copyright and licensing details. +from . import portal +from . import main diff --git a/addons/l10n_br_website_sale/controllers/main.py b/addons/l10n_br_website_sale/controllers/main.py new file mode 100644 index 00000000000..1e596da5e3e --- /dev/null +++ b/addons/l10n_br_website_sale/controllers/main.py @@ -0,0 +1,34 @@ +from odoo.addons.website_sale.controllers.main import WebsiteSale +from odoo.http import request + + +class WebsiteSaleBr(WebsiteSale): + + def _get_mandatory_fields_billing(self, country_id=False): + """Extend mandatory fields to add the vat in case the website and the customer are from brazil""" + mandatory_fields = super()._get_mandatory_fields_billing(country_id) + + if request.params.get('country_id'): + country = request.env['res.country'].browse(int(request.params['country_id'])) + if request.website.sudo().company_id.country_id.code == "BR" and country.code == "BR" and "vat" not in mandatory_fields: + mandatory_fields += ['vat'] + # Needed because the user could put brazil and then change to another country, we don't want the field to stay mandatory + elif 'vat' in mandatory_fields and country.code != 'BR': + mandatory_fields.remove('vat') + return mandatory_fields + + def values_postprocess(self, order, mode, values, errors, error_msg): + post, errors, error_msg = super().values_postprocess(order, mode, values, errors, error_msg) + website = request.env['website'].get_current_website() + # This is needed so that the field is correctly write on the partner + if values.get('l10n_latam_identification_type_id') and website.company_id.country_code == 'BR': + post['l10n_latam_identification_type_id'] = values['l10n_latam_identification_type_id'] + + return post, errors, error_msg + + def _get_country_related_render_values(self, kw, render_values): + country_related_values = super()._get_country_related_render_values(kw, render_values) + website = request.env['website'].get_current_website() + if website.company_id.country_code == 'BR': + country_related_values['identification_types'] = request.env['l10n_latam.identification.type'].search(['|', ('country_id', '=', False), ('country_id.code', '=', 'BR')]) + return country_related_values diff --git a/addons/l10n_br_website_sale/controllers/portal.py b/addons/l10n_br_website_sale/controllers/portal.py new file mode 100644 index 00000000000..760ebc468e3 --- /dev/null +++ b/addons/l10n_br_website_sale/controllers/portal.py @@ -0,0 +1,42 @@ +# Part of Odoo. See LICENSE file for full copyright and licensing details. +from odoo.addons.account.controllers.portal import CustomerPortal +from odoo.http import request + +class CustomerPortalBr(CustomerPortal): + + def _get_mandatory_fields(self): + """Extend mandatory fields to add the vat in case the website and the customer are from brazil""" + mandatory_fields = super()._get_mandatory_fields() + + if request.params.get('country_id'): + country = request.env['res.country'].browse(int(request.params['country_id'])) + if request.website.sudo().company_id.country_id.code == "BR" and country.code == "BR" and "vat" not in mandatory_fields: + mandatory_fields += ['vat'] + # Needed because the user could put brazil and then change to another country, we don't want the field to stay mandatory + elif 'vat' in mandatory_fields and country.code != 'BR': + mandatory_fields.remove('vat') + + return mandatory_fields + + def _get_optional_fields(self): + """Extend optional fields to add the identification type to avoid having the unknown field error""" + optional_fields = super()._get_optional_fields() + if request.website.sudo().company_id.country_id.code == "BR" and 'l10n_latam_identification_type_id' not in optional_fields: + optional_fields += ['l10n_latam_identification_type_id'] + return optional_fields + + def details_form_validate(self, data, partner_creation=False): + error, error_message = super().details_form_validate(data, partner_creation) + + website = request.env['website'].get_current_website() + # This is needed so that the field is correctly write on the partner + if data.get('l10n_latam_identification_type_id') and website.company_id.country_code == 'BR': + data['l10n_latam_identification_type_id'] = int(data['l10n_latam_identification_type_id']) + return error, error_message + + def _prepare_portal_layout_values(self): + portal_layout_values = super()._prepare_portal_layout_values() + website = request.env['website'].get_current_website() + if website.company_id.country_code == 'BR': + portal_layout_values['identification_types'] = request.env['l10n_latam.identification.type'].search(['|', ('country_id', '=', False), ('country_id.code', '=', 'BR')]) + return portal_layout_values diff --git a/addons/l10n_br_website_sale/views/portal.xml b/addons/l10n_br_website_sale/views/portal.xml new file mode 100644 index 00000000000..280b4166e4f --- /dev/null +++ b/addons/l10n_br_website_sale/views/portal.xml @@ -0,0 +1,28 @@ + + + + diff --git a/addons/l10n_br_website_sale/views/templates.xml b/addons/l10n_br_website_sale/views/templates.xml new file mode 100644 index 00000000000..06fbe7d4f39 --- /dev/null +++ b/addons/l10n_br_website_sale/views/templates.xml @@ -0,0 +1,31 @@ + + + + diff --git a/addons/portal/controllers/portal.py b/addons/portal/controllers/portal.py index b4a147bacb4..dee90bfe88a 100644 --- a/addons/portal/controllers/portal.py +++ b/addons/portal/controllers/portal.py @@ -190,8 +190,8 @@ class CustomerPortal(Controller): values.update({'error': error, 'error_message': error_message}) values.update(post) if not error: - values = {key: post[key] for key in self.MANDATORY_BILLING_FIELDS} - values.update({key: post[key] for key in self.OPTIONAL_BILLING_FIELDS if key in post}) + values = {key: post[key] for key in self._get_mandatory_fields()} + values.update({key: post[key] for key in self._get_optional_fields() if key in post}) for field in set(['country_id', 'state_id']) & set(values.keys()): try: values[field] = int(values[field]) @@ -376,7 +376,7 @@ class CustomerPortal(Controller): error_message = [] # Validation - for field_name in self.MANDATORY_BILLING_FIELDS: + for field_name in self._get_mandatory_fields(): if not data.get(field_name): error[field_name] = 'missing' @@ -410,13 +410,21 @@ class CustomerPortal(Controller): if [err for err in error.values() if err == 'missing']: error_message.append(_('Some required fields are empty.')) - unknown = [k for k in data if k not in self.MANDATORY_BILLING_FIELDS + self.OPTIONAL_BILLING_FIELDS] + unknown = [k for k in data if k not in self._get_mandatory_fields() + self._get_optional_fields()] if unknown: error['common'] = 'Unknown field' error_message.append("Unknown field '%s'" % ','.join(unknown)) return error, error_message + def _get_mandatory_fields(self): + """ This method is there so that we can override the mandatory fields """ + return self.MANDATORY_BILLING_FIELDS + + def _get_optional_fields(self): + """ This method is there so that we can override the optional fields """ + return self.OPTIONAL_BILLING_FIELDS + def _document_check_access(self, model_name, document_id, access_token=None): """Check if current user is allowed to access the specified record.