'After DB I/O, check the correctness of fiscal code (ITALY)
@ToDo: The function should be made a deployment_setting when anyone else wishes to use this module'

@staticmethod
def evr_case_onaccept(form):

    fiscal_code = form.vars.fiscal_code
    if ((fiscal_code == '') or (fiscal_code == None)):
        return
    fiscal_code = fiscal_code.upper()
    MALE = 3
    CONSONANTS = 'BCDFGHJKLMNPQRSTVWXYZ'
    VOWELS = 'AEIOU'
    MONTHS = 'ABCDEHLMPRST'
    T = current.T
    ptable = current.s3db.pr_person
    query = (form.vars.person_id == ptable.id)
    row = current.db(query).select(ptable.first_name, ptable.last_name, ptable.date_of_birth, ptable.gender, limitby=(0, 1)).first()
    name = row.first_name.upper()
    surname = row.last_name.upper()
    date_of_birth = row.date_of_birth
    year = date_of_birth.year
    month = date_of_birth.month
    day = date_of_birth.day
    gender = row.gender
    cons = ''
    for c in surname:
        if (c in CONSONANTS):
            cons += c
    vow = ''
    for c in surname:
        if (c in VOWELS):
            vow += c
    chars = (cons + vow)
    if (len(chars) < 3):
        chars += ['X', 'X']
    if (fiscal_code[:3] != chars[0:3].upper()):
        current.response.warning = T("Warning:    fiscal    code    isn't                                                                                                                                                                        consistent    with    personal    data")
        return
    cons = ''
    for c in name:
        if (c in CONSONANTS):
            cons += c
    if (len(cons) > 3):
        chars = ((cons[0] + cons[2]) + cons[3])
    else:
        vow = ''
        for c in name:
            if (c in VOWELS):
                vow += c
        chars = (cons + vow)
        if (len(chars) < 3):
            chars += ['X', 'X']
    if (fiscal_code[3:6] != chars[0:3].upper()):
        current.response.warning = T("Warning:    fiscal    code    isn't                                                                                                                                                                        consistent    with    personal    data")
        return
    year = str(year)[2:4]
    if ((fiscal_code[6:8] != year) or (fiscal_code[8] != MONTHS[(month - 1)])):
        current.response.warning = T("Warning:    fiscal    code    isn't                                                                                                                                                                        consistent    with    personal    data")
        return
    if (gender == MALE):
        birthday_in_cf = fiscal_code[9:11]
        if (not birthday_in_cf.isdigit()):
            current.response.warning = T("Warning:    fiscal    code    isn't                                                                                                                                                                                        consistent    with    personal    data")
            return
        else:
            birthday_in_cf = int(birthday_in_cf)
            if (birthday_in_cf != day):
                current.response.warning = T("Warning:    fiscal    code    isn't                                                                                                                                                                                                        consistent    with    personal    data")
                return
    elif (fiscal_code[9:11] != str((day + 40))):
        current.response.warning = T("Warning:    fiscal    code    isn't                                                                                                                                                                                        consistent    with    personal    data")
        return
    return
