from odoo import models, api, _
import stdnum.ar.cbu


def validate_cbu(cbu):
    return stdnum.ar.cbu.validate(cbu)


class ResPartnerBank(models.Model):

    _inherit = 'res.partner.bank'

    @api.model
    def _get_supported_account_types(self):
        """ Add new account type named cbu used in Argentina """
        res = super()._get_supported_account_types()
        res.append(('cbu', _('CBU')))
        return res

    @api.model
    def retrieve_acc_type(self, acc_number):
        try:
            validate_cbu(acc_number)
        except Exception:
            return super().retrieve_acc_type(acc_number)
        return 'cbu'

from odoo import models, api, _
from odoo.exceptions import ValidationError
import stdnum.ar
import logging
_logger = logging.getLogger(__name__)


def validate_cbu(cbu):
    try:
        return stdnum.ar.cbu.validate(cbu)
    except Exception as error:
        msg = _("Argentinian CBU was not validated: %s" % repr(error))
        _logger.log(25, msg)
        raise ValidationError(msg)


class ResPartnerBank(models.Model):

    _inherit = 'res.partner.bank'

    @api.model
    def _get_supported_account_types(self):
        """ Add new account type named cbu used in Argentina """
        res = super()._get_supported_account_types()
        res.append(('cbu', _('CBU')))
        return res

    @api.model
    def retrieve_acc_type(self, acc_number):
        try:
            validate_cbu(acc_number)
        except Exception:
            return super().retrieve_acc_type(acc_number)
        return 'cbu'

Fix ImportError: No module named 'stdnum.ar.cbu'
[FIX] l10n_ar: Fix ImportError: No module named 'stdnum.ar.cbu'

Since stdnum.ar.cbu is not available in odoo saas enviroment because is
using an old version of stdnum package, we add a try exept in order to
catch this and manage the error properly which is raise an exception and
leave a message in the log telling the user that the cbu was not able to
validate.

closes odoo/odoo#40383

X-original-commit: 25d483fc3fc05fd47c72c3d96c02fed12b998b0d
Signed-off-by: Josse Colpaert <1f46e7f017caa89a77c9557ed26b800e8d5d7700@openerp.com>

  from odoo import models, api, _
+ from odoo.exceptions import ValidationError
- import stdnum.ar.cbu
+ import stdnum.ar
+ import logging
+ _logger = logging.getLogger(__name__)
  
  
  def validate_cbu(cbu):
+     try:
-     return stdnum.ar.cbu.validate(cbu)
+         return stdnum.ar.cbu.validate(cbu)
+     except Exception as error:
+         msg = _("Argentinian CBU was not validated: %s" % repr(error))
+         _logger.log(25, msg)
+         raise ValidationError(msg)
  
  
  class ResPartnerBank(models.Model):
  
      _inherit = 'res.partner.bank'
  
      @api.model
      def _get_supported_account_types(self):
          """ Add new account type named cbu used in Argentina """
          res = super()._get_supported_account_types()
          res.append(('cbu', _('CBU')))
          return res
  
      @api.model
      def retrieve_acc_type(self, acc_number):
          try:
              validate_cbu(acc_number)
          except Exception:
              return super().retrieve_acc_type(acc_number)
          return 'cbu'
  
Fix ImportError: No module named 'stdnum.ar.cbu'
## Code Before:
from odoo import models, api, _
import stdnum.ar.cbu


def validate_cbu(cbu):
    return stdnum.ar.cbu.validate(cbu)


class ResPartnerBank(models.Model):

    _inherit = 'res.partner.bank'

    @api.model
    def _get_supported_account_types(self):
        """ Add new account type named cbu used in Argentina """
        res = super()._get_supported_account_types()
        res.append(('cbu', _('CBU')))
        return res

    @api.model
    def retrieve_acc_type(self, acc_number):
        try:
            validate_cbu(acc_number)
        except Exception:
            return super().retrieve_acc_type(acc_number)
        return 'cbu'

## Instruction:
Fix ImportError: No module named 'stdnum.ar.cbu'
## Code After:
from odoo import models, api, _
from odoo.exceptions import ValidationError
import stdnum.ar
import logging
_logger = logging.getLogger(__name__)


def validate_cbu(cbu):
    try:
        return stdnum.ar.cbu.validate(cbu)
    except Exception as error:
        msg = _("Argentinian CBU was not validated: %s" % repr(error))
        _logger.log(25, msg)
        raise ValidationError(msg)


class ResPartnerBank(models.Model):

    _inherit = 'res.partner.bank'

    @api.model
    def _get_supported_account_types(self):
        """ Add new account type named cbu used in Argentina """
        res = super()._get_supported_account_types()
        res.append(('cbu', _('CBU')))
        return res

    @api.model
    def retrieve_acc_type(self, acc_number):
        try:
            validate_cbu(acc_number)
        except Exception:
            return super().retrieve_acc_type(acc_number)
        return 'cbu'
