[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
This commit is contained in:
Maximilien (malb)
2023-12-08 09:53:38 +00:00
parent 1e39c7b448
commit c003cac091
8 changed files with 170 additions and 4 deletions
+2
View File
@@ -0,0 +1,2 @@
# Part of Odoo. See LICENSE file for full copyright and licensing details.
from . import controllers
@@ -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',
}
@@ -0,0 +1,3 @@
# Part of Odoo. See LICENSE file for full copyright and licensing details.
from . import portal
from . import main
@@ -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
@@ -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
@@ -0,0 +1,28 @@
<?xml version="1.0" encoding="utf-8"?>
<odoo>
<template id="portal_my_details_fields" inherit_id="portal.portal_my_details_fields">
<xpath expr="//input[@name='vat']/.." position="before">
<t t-if="res_company.country_id.code == 'BR'">
<div class="mb-1 col-xl-6"/> <!-- Empty div to put the vat and identification type on the same line -->
<div t-attf-class="mb-1 #{error.get('l10n_ar_afip_responsibility_type_id') and 'o_has_error' or ''} col-xl-6">
<label class="col-form-label" for="l10n_latam_identification_type_id">Identification Type</label>
<t t-if="partner.can_edit_vat()">
<select name="l10n_latam_identification_type_id" t-attf-class="form-select #{error.get('l10n_latam_identification_type_id') and 'is-invalid' or ''}">
<option value="">Identification Type...</option>
<t t-foreach="identification_types or []" t-as="id_type">
<option t-att-value="id_type.id" t-att-selected="id_type.id == partner.l10n_latam_identification_type_id.id">
<t t-esc="id_type.name"/>
</option>
</t>
</select>
</t>
<t t-else="">
<p class="form-control" t-esc="partner.l10n_latam_identification_type_id.name" readonly="1" title="Changing Identification type is not allowed once document(s) have been issued for your account. Please contact us directly for this operation."/>
<input name="l10n_latam_identification_type_id" class="form-control" t-att-value="partner.l10n_latam_identification_type_id.id" type='hidden'/>
</t>
</div>
</t>
</xpath>
</template>
</odoo>
@@ -0,0 +1,31 @@
<?xml version="1.0" encoding="utf-8"?>
<odoo>
<template id="address" inherit_id="website_sale.address">
<xpath expr="//input[@name='vat']/.." position="before">
<t t-if="mode[1] == 'billing'" positon="inside">
<t t-if="res_company.country_id.code == 'BR'">
<t t-set="partner" t-value="website_sale_order.partner_id"/>
<div class="col-lg-6 mb-2"/> <!-- Empty div to put the vat and identification type on the same line -->
<div t-attf-class="mb-3 #{error.get('l10n_ar_afip_responsibility_type_id') and 'o_has_error' or ''} col-xl-6">
<label class="col-form-label" for="l10n_latam_identification_type_id">Identification Type</label>
<t t-if="partner.can_edit_vat()">
<select name="l10n_latam_identification_type_id" t-attf-class="form-select #{error.get('l10n_latam_identification_type_id') and 'is-invalid' or ''}">
<option value="">Identification Type...</option>
<t t-foreach="identification_types or []" t-as="id_type">
<option t-att-value="id_type.id" t-att-selected="id_type.id == partner.l10n_latam_identification_type_id.id">
<t t-esc="id_type.name"/>
</option>
</t>
</select>
</t>
<t t-else="">
<p class="form-control" t-esc="partner.l10n_latam_identification_type_id.name" readonly="1" title="Changing Identification type is not allowed once document(s) have been issued for your account. Please contact us directly for this operation."/>
<input name="l10n_latam_identification_type_id" class="form-control" t-att-value="partner.l10n_latam_identification_type_id.id" type='hidden'/>
</t>
</div>
</t>
</t>
</xpath>
</template>
</odoo>
+12 -4
View File
@@ -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.