
from odoo import _
from odoo.addons.portal.controllers.portal import CustomerPortal
from odoo.http import request

CustomerPortal.OPTIONAL_BILLING_FIELDS.extend(['fiscalcode'])


class WebsitePortalFiscalCode(CustomerPortal):

    def details_form_validate(self, data):
        error, error_message = \
            super(WebsitePortalFiscalCode, self).details_form_validate(data)
        # Check fiscalcode
        dummy_partner = request.env['res.partner'].new({
            'fiscalcode': data.get('fiscalcode'),
            'company_name': data.get('company_name'),
        })
        if not dummy_partner.check_fiscalcode():
            error['fiscalcode'] = 'error'
            error_message.append(_('Fiscal Code not valid'))
        return error, error_message


from odoo import _
from odoo.addons.portal.controllers.portal import CustomerPortal
from odoo.http import request

CustomerPortal.OPTIONAL_BILLING_FIELDS.extend(['fiscalcode'])


class WebsitePortalFiscalCode(CustomerPortal):

    def details_form_validate(self, data):
        error, error_message = \
            super(WebsitePortalFiscalCode, self).details_form_validate(data)
        # Check fiscalcode
        partner = request.env.user.partner_id
        # company_type does not come from page form
        company_type = partner.company_type
        company_name = False
        if 'company_name' in data:
            company_name = data.get('company_name')
        else:
            # when company_name is not posted (readonly)
            if partner.company_name:
                company_name = partner.company_name
            elif partner.company_type == 'company':
                company_name = partner.name
        dummy_partner = request.env['res.partner'].new({
            'fiscalcode': data.get('fiscalcode'),
            'company_name': company_name,
            'company_type': company_type,
        })
        if not dummy_partner.check_fiscalcode():
            error['fiscalcode'] = 'error'
            error_message.append(_('Fiscal Code not valid'))
        return error, error_message

FIX l10n_it_website_portal_fiscalcode check after invoice issuing
FIX l10n_it_website_portal_fiscalcode check after invoice issuing

Steps:

 - Create a partner (type company) and give them portal access
 - With the new user, access to portal
 - Edit partner details setting fiscal code with 11 digits
 - Using admin, create an invoice for that partner and validate
 - Using the new user, access to portal, open partner details and save

Get "The fiscal code doesn't seem to be correct"

  
  from odoo import _
  from odoo.addons.portal.controllers.portal import CustomerPortal
  from odoo.http import request
  
  CustomerPortal.OPTIONAL_BILLING_FIELDS.extend(['fiscalcode'])
  
  
  class WebsitePortalFiscalCode(CustomerPortal):
  
      def details_form_validate(self, data):
          error, error_message = \
              super(WebsitePortalFiscalCode, self).details_form_validate(data)
          # Check fiscalcode
+         partner = request.env.user.partner_id
+         # company_type does not come from page form
+         company_type = partner.company_type
+         company_name = False
+         if 'company_name' in data:
+             company_name = data.get('company_name')
+         else:
+             # when company_name is not posted (readonly)
+             if partner.company_name:
+                 company_name = partner.company_name
+             elif partner.company_type == 'company':
+                 company_name = partner.name
          dummy_partner = request.env['res.partner'].new({
              'fiscalcode': data.get('fiscalcode'),
-             'company_name': data.get('company_name'),
+             'company_name': company_name,
+             'company_type': company_type,
          })
          if not dummy_partner.check_fiscalcode():
              error['fiscalcode'] = 'error'
              error_message.append(_('Fiscal Code not valid'))
          return error, error_message
  
FIX l10n_it_website_portal_fiscalcode check after invoice issuing
## Code Before:

from odoo import _
from odoo.addons.portal.controllers.portal import CustomerPortal
from odoo.http import request

CustomerPortal.OPTIONAL_BILLING_FIELDS.extend(['fiscalcode'])


class WebsitePortalFiscalCode(CustomerPortal):

    def details_form_validate(self, data):
        error, error_message = \
            super(WebsitePortalFiscalCode, self).details_form_validate(data)
        # Check fiscalcode
        dummy_partner = request.env['res.partner'].new({
            'fiscalcode': data.get('fiscalcode'),
            'company_name': data.get('company_name'),
        })
        if not dummy_partner.check_fiscalcode():
            error['fiscalcode'] = 'error'
            error_message.append(_('Fiscal Code not valid'))
        return error, error_message

## Instruction:
FIX l10n_it_website_portal_fiscalcode check after invoice issuing
## Code After:

from odoo import _
from odoo.addons.portal.controllers.portal import CustomerPortal
from odoo.http import request

CustomerPortal.OPTIONAL_BILLING_FIELDS.extend(['fiscalcode'])


class WebsitePortalFiscalCode(CustomerPortal):

    def details_form_validate(self, data):
        error, error_message = \
            super(WebsitePortalFiscalCode, self).details_form_validate(data)
        # Check fiscalcode
        partner = request.env.user.partner_id
        # company_type does not come from page form
        company_type = partner.company_type
        company_name = False
        if 'company_name' in data:
            company_name = data.get('company_name')
        else:
            # when company_name is not posted (readonly)
            if partner.company_name:
                company_name = partner.company_name
            elif partner.company_type == 'company':
                company_name = partner.name
        dummy_partner = request.env['res.partner'].new({
            'fiscalcode': data.get('fiscalcode'),
            'company_name': company_name,
            'company_type': company_type,
        })
        if not dummy_partner.check_fiscalcode():
            error['fiscalcode'] = 'error'
            error_message.append(_('Fiscal Code not valid'))
        return error, error_message
