def es_cif(doi):
    """
    Validate a Spanish CIF.

    Each company in Spain prior to 2008 had a distinct CIF and has been
    discontinued. For more information see `wikipedia.org/cif`_.

    The new replacement is to use NIF for absolutely everything. The issue is
    that there are "types" of NIFs now: company, person[citizen vs recident]
    all distinguished by the first character of the DOI. For this reason we
    will continue to call CIF NIFs that are used for companies.

    This validator is based on `generadordni.es`_.

    .. _generadordni.es:
        https://generadordni.es/

    .. _wikipedia.org/cif:
        https://es.wikipedia.org/wiki/C%C3%B3digo_de_identificaci%C3%B3n_fiscal

    Examples::

        >>> es_cif('B25162520')
        True

        >>> es_cif('B25162529')
        ValidationFailure(func=es_cif, args=...)

    .. versionadded:: 0.13.0

    :param doi: DOI to validate
    """
    doi = doi.upper()

    if len(doi) != 9:
        return False

    table = 'JABCDEFGHI'
    first_chr = doi[0]
    doi_body = doi[1:8]
    control = doi[8]

    if not doi_body.isdigit():
        return False

    odd_result = 0
    even_result = 0
    for index, char in enumerate(doi_body):
        if index % 2 == 0:
            # Multiply each each odd position doi digit by 2 and sum it all
            # together
            odd_result += sum(map(int, str(int(char) * 2)))
        else:
            even_result += int(char)

    res = (10 - (even_result + odd_result) % 10) % 10

    if first_chr in 'ABEH':  # Number type
        return str(res) == control
    elif first_chr in 'PSQW':  # Letter type
        return table[res] == control
    elif first_chr not in 'CDFGJNRUV':
        return False

    return control == str(res) or control == table[res]