Esempio n. 1
0
def calculate_id_number_control_value(id_number):
    """
    Calculates the control value for the id number.

    @type id_number: int
    @param id_number: The id number to calculate the
    control value.
    @rtype: int
    @return: The calculated control value.
    """

    # calculates the number of digits in the number
    number_digits = number_util.get_number_length(id_number)

    # in case the number of digits is invalid
    # must raise an exception indicating the error
    if number_digits < 6 or number_digits > 8:
        raise RuntimeError("Invalid id number length '%d'" % number_digits)

    # adds zeros to the right of the number (if necessary)
    id_number *= 10 ** (8 - number_digits)

    # calculates the control value for the number
    control_value = calculate_control_value(id_number)

    # returns the control value
    return control_value
Esempio n. 2
0
def calculate_control_value(number):
    """
    Calculates the control value for the given
    number.

    @type number: int
    @param number: The number to calculate the control value.
    @rtype: int
    @return: The control value for the given
    number.
    """

    # calculates the number of digits in the number
    number_digits = number_util.get_number_length(number)

    # starts the accumulator
    accumulator = 0

    # iterates over the range of the number of digits
    for index in range(number_digits):
        # retrieves the current digit
        current_digit = number_util.get_digit(number, index)

        # retrieves the current coefficient
        current_coefficient = COEFFICIENT_VALUES[index]

        # increments the accumulator with the current digit
        # multiplied with the current coefficient
        accumulator += current_digit * current_coefficient

    # calculates the partial value from
    # the accumulator value
    partial_value = accumulator % 11

    # in case the partial value is zero
    # or one
    if partial_value in (0, 1):
        # sets the control value as zero
        control_value = 0
    # otherwise
    else:
        # calculates the control value based
        # on the partial value
        control_value = 11 - partial_value

    # returns the control value
    return control_value
Esempio n. 3
0
def calculate_control_value(number):
    """
    Calculates the control value for the given
    number.

    @type number: int
    @param number: The number to calculate the control value.
    @rtype: int
    @return: The control value for the given
    number.
    """

    # calculates the number of digits in the number
    number_digits = number_util.get_number_length(number)

    # starts the accumulator
    accumulator = 0

    # iterates over the range of the number of digits
    for index in range(number_digits):
        # in case it's even, sets the multiplier as one
        # otherwise it must be odd and the multiplier
        # should be set as three
        if index % 2: multiplier = 1
        else: multiplier = 3

        # retrieves the current digit
        current_digit = number_util.get_digit(number, index)

        # increments the accumulator with the current digit
        # multiplied with the current multiplier
        accumulator += current_digit * multiplier

    # calculates the partial value from
    # the accumulator value
    partial_value = accumulator % 10

    # calculates the control value based
    # on the partial value
    control_value = 10 - partial_value

    # returns the control value
    return control_value
Esempio n. 4
0
def calculate_tax_number_control_value(tax_number):
    """
    Calculates the control value for the tax number.

    @type id_number: int
    @param id_number: The tax number to calculate the
    control value.
    @rtype: int
    @return: The calculated control value.
    """

    # calculates the number of digits in the number
    number_digits = number_util.get_number_length(tax_number)

    # in case the number of digits is invalid
    # must raise an exception indicating the error
    if not number_digits == 8:
        raise RuntimeError("Invalid tax number length '%d'" % number_digits)

    # calculates the control value for the number
    control_value = calculate_control_value(tax_number)

    # returns the control value
    return control_value