def reporte_solicitudes_general(id_inst, tipo, estado, desde, hasta, sexo,
                                grupo, motivo, municipio, ocupacion, tipo_edu,
                                organizacion, sector, dependencia):

    reload(sys)
    sys.setdefaultencoding("utf-8")

    pdf = class_pdf.ReporteCandidato(
        orientation='L', unit='mm',
        format='letter')  # HORIENTACION DE LA PAGINA

    pdf.set_author('Marcel Arcuri')
    pdf.alias_nb_pages()  # LLAMADA DE PAGINACION
    pdf.add_page()  # ANADE UNA NUEVA PAGINACION
    pdf.set_margins(15, 10, 10)  # MARGENE DEL DOCUMENTO
    pdf.set_line_width(0.5)
    pdf.ln(5)
    pdf.set_fill_color(255, 255, 255)
    pdf.set_font('Arial', 'B', 12)
    pdf.cell(250, 5, "Listado de Visitas Recibidas".decode("UTF-8"), '', 1,
             'C', 1)
    pdf.set_font('Arial', 'B', 10)
    pdf.cell(
        250, 5,
        "Desde el " + str(desde) + " hasta el " + str(hasta).decode("UTF-8"),
        '', 1, 'C', 1)
    pdf.set_font('Arial', 'B', 10)
    pdf.ln(5)

    cursor = connection.cursor()
    sql_grupo = "SELECT nom_institucion FROM instituciones_institucion WHERE id=%s "
    cursor.execute(sql_grupo, [id_inst])
    row = dictfetchall(cursor)
    nom_institucion = row[0]['nom_institucion']

    pdf.set_fill_color(255, 255, 255)
    pdf.set_font('Arial', 'B', 10)
    pdf.cell(20, 5, "Institución: ".decode("UTF-8"), '', 0, 'L', 1)
    pdf.set_font('Arial', '', 10)
    pdf.cell(150, 5, nom_institucion.decode("UTF-8"), '', 1, 'L', 1)
    pdf.set_font('Arial', 'B', 10)
    pdf.cell(29, 5, "Tipo de reporte:".decode("UTF-8"), '', 0, 'L', 1)
    pdf.set_font('Arial', '', 10)
    if tipo == '1':
        if sexo == '1':
            genero = 'FEMENINO'
        else:
            genero = 'MASCULINO '
        tipo_reporte = 'Por Género (' + str(genero) + ')'
    elif tipo == '2':
        sql_grupo = "SELECT CONCAT(g.descripcion,' (',g.desde,'-',g.hasta,' Años)') descripcion "
        sql_grupo += "FROM grupo_etareo_grupo_etareo AS g  WHERE g.id=%s  "
        cursor.execute(sql_grupo, [grupo])
        row = dictfetchall(cursor)
        descr = row[0]['descripcion']
        tipo_reporte = 'Por Grupo Etáreo (' + str(descr) + ')'
    elif tipo == '3':
        sql_motivo = "SELECT motivo FROM motivo_motivos WHERE id=%s "
        cursor.execute(sql_motivo, [motivo])
        row = dictfetchall(cursor)
        descr = row[0]['motivo']
        tipo_reporte = 'Por Motivo (' + str(descr) + ')'
    elif tipo == '4':
        sql_mun = "SELECT municipio FROM municipios_municipio WHERE cod_municipio=%s"
        cursor.execute(sql_mun, [municipio])
        row = dictfetchall(cursor)
        descr = row[0]['municipio']
        tipo_reporte = 'Por Municipio (' + str(descr) + ')'
    elif tipo == '5':
        sql_ocupacion = "SELECT ocupacion FROM ocupacion_ocupaciones WHERE id=%s "
        cursor.execute(sql_ocupacion, [ocupacion])
        row = dictfetchall(cursor)
        descr = row[0]['ocupacion']
        tipo_reporte = 'Por Ocupación (' + str(descr) + ')'
    elif tipo == '6':
        sql_tipo = "SELECT tipo FROM tipo_educacion_tipoeducacion WHERE id=%s "
        cursor.execute(sql_tipo, [tipo_edu])
        row = dictfetchall(cursor)
        descr = row[0]['tipo']
        tipo_reporte = 'Por Nivel de Educación (' + str(descr) + ')'
    elif tipo == '7':
        sql_organizacion = "SELECT organizacion FROM organizacion_social_organizaciones WHERE id=%s "
        cursor.execute(sql_organizacion, [organizacion])
        row = dictfetchall(cursor)
        descr = row[0]['organizacion']
        tipo_reporte = 'Por Organización Social (' + str(descr) + ')'
    elif tipo == '8':
        sql_sector = "SELECT sector FROM sector_laboral_sectorlaboral WHERE id=%s "
        cursor.execute(sql_sector, [sector])
        row = dictfetchall(cursor)
        descr = row[0]['sector']
        tipo_reporte = 'Por Sector Laboral (' + str(descr) + ')'
    elif tipo == '9':
        sql_dependencia = "SELECT nom_dependencia FROM dependencias_dependencia WHERE id=%s "
        cursor.execute(sql_dependencia, [dependencia])
        row = dictfetchall(cursor)
        descr = row[0]['nom_dependencia']
        tipo_reporte = 'Por Departamento (' + str(descr) + ')'
    elif tipo == '10':
        tipo_reporte = 'Total de Visitas'
    elif tipo == '11':
        tipo_reporte = 'Total de Personas'
    elif tipo == '12':
        tipo_reporte = 'Centro de Votación'
    elif tipo == '13':
        tipo_reporte = 'Twitter'
    elif tipo == '14':
        tipo_reporte = 'Total General'
    pdf.cell(150, 5, (tipo_reporte).decode("UTF-8"), '', 1, 'L', 1)
    pdf.set_font('Arial', 'B', 10)
    pdf.ln(5)

    # Fila de la cabezara de la tabla
    pdf.set_fill_color(0, 0, 0)
    pdf.set_text_color(255, 255, 255)
    pdf.set_font('Arial', 'B', 10)
    if tipo == '12':
        pdf.cell(7, 5, "#".decode("UTF-8"), 'LTBR', 0, 'C', 1)
        pdf.cell(17, 5, "Cédula".decode("UTF-8"), 'LTBR', 0, 'C', 1)
        pdf.cell(50, 5, "Nombre".decode("UTF-8"), 'LTBR', 0, 'C', 1)
        pdf.cell(50, 5, "Apellido".decode("UTF-8"), 'LTBR', 0, 'C', 1)
        pdf.cell(25, 5, "Cod Centro".decode("UTF-8"), 'LTBR', 0, 'C', 1)
        pdf.cell(100, 5, "Centro de Votación".decode("UTF-8"), 'LTBR', 1, 'C',
                 1)
    elif tipo == '13':
        pdf.cell(7, 5, "#".decode("UTF-8"), 'LTBR', 0, 'C', 1)
        pdf.cell(17, 5, "Cédula".decode("UTF-8"), 'LTBR', 0, 'C', 1)
        pdf.cell(50, 5, "Nombre".decode("UTF-8"), 'LTBR', 0, 'C', 1)
        pdf.cell(50, 5, "Apellido".decode("UTF-8"), 'LTBR', 0, 'C', 1)
        pdf.cell(125, 5, "Twitter".decode("UTF-8"), 'LTBR', 1, 'C', 1)
    elif tipo == '14':
        pdf.cell(15, 5, "Cédula".decode("UTF-8"), 'LTBR', 0, 'C', 1)
        pdf.cell(60, 5, "Nombre y Apellido".decode("UTF-8"), 'LTBR', 0, 'C', 1)
        pdf.cell(50, 5, "Motivo".decode("UTF-8"), 'LTBR', 0, 'C', 1)
        pdf.cell(15, 5, "Fecha".decode("UTF-8"), 'LTBR', 0, 'C', 1)
        pdf.cell(50, 5, "Municipio".decode("UTF-8"), 'LTBR', 0, 'C', 1)
        pdf.cell(60, 5, "Organización".decode("UTF-8"), 'LTBR', 0, 'C', 1)
    else:
        pdf.cell(7, 5, "#".decode("UTF-8"), 'LTBR', 0, 'C', 1)
        pdf.cell(15, 5, "Código".decode("UTF-8"), 'LTBR', 0, 'C', 1)
        pdf.cell(17, 5, "Cédula".decode("UTF-8"), 'LTBR', 0, 'C', 1)
        pdf.cell(50, 5, "Nombre".decode("UTF-8"), 'LTBR', 0, 'C', 1)
        pdf.cell(50, 5, "Apellido".decode("UTF-8"), 'LTBR', 0, 'C', 1)
        pdf.cell(20, 5, "Fecha".decode("UTF-8"), 'LTBR', 0, 'C', 1)
        pdf.cell(90, 5, "Departamento".decode("UTF-8"), 'LTBR', 1, 'C', 1)

    pdf.set_fill_color(255, 255, 255)
    pdf.set_font('Arial', '', 10)
    # print tipo
    cursor = connection.cursor()
    if tipo == '11':
        sql_sop = "  SELECT DISTINCT ON (s.cedula) s.codigo, s.cedula, s.nombre, s.apellido, CAST(s.fecha_ent AS date) dia_ent, CAST(s.fecha_ent AS time) hora_ent, "
    elif tipo == '12':
        sql_sop = "  SELECT DISTINCT ON (s.cedula) s.cedula, s.nombre, s.apellido, s.codigo_centro, s.nombre_centro, "
    elif tipo == '13':
        # print "entro"
        sql_sop = "  SELECT DISTINCT ON (s.cedula) s.codigo, s.cedula, s.nombre, s.apellido, s.twitter, "
    elif tipo == '14':
        # print "entro"
        sql_sop = "  SELECT s.cedula, s.nombre, s.apellido, "
        sql_sop += "  (SELECT motivo FROM motivo_motivos WHERE id=s.motivo_id) motivo, CAST(s.fecha_ent AS date) fecha, "
        sql_sop += "  (SELECT municipio FROM municipios_municipio WHERE cod_municipio=s.municipio) municipio, "
        sql_sop += "  (SELECT organizacion FROM organizacion_social_organizaciones WHERE id=s.organizacion_id) organizacion"
        sql_sop += " FROM solicitud_registros s "

    else:
        sql_sop = "  SELECT s.codigo, s.cedula, s.nombre, s.apellido, CAST(s.fecha_ent AS date) dia_ent, CAST(s.fecha_ent AS time) hora_ent, "
        sql_sop += "  CAST(s.fecha_sal AS date) dia_sal, CAST(s.fecha_sal AS time) hora_sal, d.nom_dependencia "
        sql_sop += " FROM solicitud_registros s "
        sql_sop += " INNER JOIN dependencias_dependencia d ON s.departamento_id = d.id"

    if tipo == '1':
        sql_sop += " WHERE s.institucion_id=%s and s.sexo=%s and s.fecha_ent::date between %s and %s "
        sql_sop += " ORDER BY s.codigo ASC "
        cursor.execute(sql_sop, [id_inst, sexo, desde, hasta])
    elif tipo == '2':
        sql_grupo = "SELECT desde, hasta FROM grupo_etareo_grupo_etareo WHERE id=%s "
        cursor.execute(sql_grupo, [grupo])
        row = dictfetchall(cursor)
        desde_e = row[0]['desde']
        hasta_e = row[0]['hasta']
        sql_sop += " WHERE s.institucion_id=%s and s.edad between %s and %s and s.fecha_ent::date between %s and %s "
        sql_sop += " ORDER BY s.codigo ASC "
        cursor.execute(sql_sop, [id_inst, desde_e, hasta_e, desde, hasta])
    elif tipo == '3':
        sql_sop += " WHERE s.institucion_id=%s and s.motivo_id=%s and s.fecha_ent::date between %s and %s "
        sql_sop += " ORDER BY s.codigo ASC "
        cursor.execute(sql_sop, [id_inst, motivo, desde, hasta])
    elif tipo == '4':
        sql_sop += " WHERE s.institucion_id=%s and s.estado_id=%s and s.municipio=%s and s.fecha_ent::date between %s and %s "
        sql_sop += " ORDER BY s.codigo ASC "
        cursor.execute(sql_sop, [id_inst, estado, municipio, desde, hasta])
    elif tipo == '5':
        sql_sop += " WHERE s.institucion_id=%s and s.ocupacion_id=%s and s.fecha_ent::date between %s and %s "
        sql_sop += " ORDER BY s.codigo ASC "
        cursor.execute(sql_sop, [id_inst, ocupacion, desde, hasta])
    elif tipo == '6':
        sql_sop += " WHERE s.institucion_id=%s and s.tipo_edu_id=%s and s.fecha_ent::date between %s and %s "
        sql_sop += " ORDER BY s.codigo ASC "
        cursor.execute(sql_sop, [id_inst, tipo_edu, desde, hasta])
    elif tipo == '7':
        sql_sop += " WHERE s.institucion_id=%s and s.organizacion_id=%s and s.fecha_ent::date between %s and %s "
        sql_sop += " ORDER BY s.codigo ASC "
        cursor.execute(sql_sop, [id_inst, organizacion, desde, hasta])
    elif tipo == '8':
        sql_sop += " WHERE s.institucion_id=%s and s.sector_lab_id=%s and s.fecha_ent::date between %s and %s "
        sql_sop += " ORDER BY s.codigo ASC "
        cursor.execute(sql_sop, [id_inst, sector, desde, hasta])
    elif tipo == '9':
        sql_sop += " WHERE s.institucion_id=%s and s.departamento_id=%s and s.fecha_ent::date between %s and %s "
        sql_sop += " ORDER BY s.codigo ASC "
        cursor.execute(sql_sop, [id_inst, dependencia, desde, hasta])
    elif tipo == '10':
        sql_sop += " WHERE s.institucion_id=%s and s.fecha_ent::date between %s and %s "
        sql_sop += " ORDER BY s.codigo ASC "
        cursor.execute(sql_sop, [id_inst, desde, hasta])
    elif tipo == '14':
        sql_sop += " WHERE s.institucion_id=%s and s.fecha_ent::date between %s and %s "
        sql_sop += " ORDER BY s.codigo ASC "
        cursor.execute(sql_sop, [id_inst, desde, hasta])
    else:
        sql_sop += " WHERE s.institucion_id=%s and s.fecha_ent::date between %s and %s "
        cursor.execute(sql_sop, [id_inst, desde, hasta])
    print sql_sop
    row = dictfetchall(cursor)

    i = 1
    j = 0
    item = 0
    o = 0
    h = 0
    for t in row:
        # id_c = t['id_c']
        if tipo == '12':
            if t['nombre_centro'] == None:
                centro = ''
            else:
                centro = t['nombre_centro']
            l = len(centro)  #Cuenta el numero de letras que viene en la cadena
            p = 1 + (l / 70
                     )  #Numero de lineas que ocupara la descripcion del bien
        elif tipo == '13':
            p = 1
        elif tipo == '14':
            # nom = str(t['organizacion'])+''+str(t['organizacion'])
            l = len(t['organizacion']
                    )  #Cuenta el numero de letras que viene en la cadena
            p = 1 + (l / 40
                     )  #Numero de lineas que ocupara la descripcion del bien
            # l =  len(nom) #Cuenta el numero de letras que viene en la cadena
            # l2 =  len(t['organizacion']) #Cuenta el numero de letras que viene en la cadena
            # if l > l2:
            #         p = 1 + (l/60) #Numero de lineas que ocupara la descripcion del bien
            # else:
            #         p = 1 + (l2/40) #Numero de lineas que ocupara la descripcion del bien
        else:
            l = len(t['nom_dependencia']
                    )  #Cuenta el numero de letras que viene en la cadena
            p = 1 + (l / 50
                     )  #Numero de lineas que ocupara la descripcion del bien
        h = h + (5 * p)  #Numero de lineas que arman la data del pdf
        print h

        #### Validacion en base a  P para el grosor de la celdas
        if p == 1:
            a = 5
        elif p == 2:
            a = 10

        pdf.set_fill_color(255, 255, 255)
        if h >= 110:
            pdf.add_page()
            pdf.ln(5)
            pdf.set_fill_color(255, 255, 255)
            pdf.set_font('Arial', 'B', 12)
            pdf.cell(250, 5, "Listado de Visitas Recibidas".decode("UTF-8"),
                     '', 1, 'C', 1)
            pdf.set_font('Arial', 'B', 10)
            pdf.cell(
                250, 5, "Desde el " + str(desde) + " hasta el " +
                str(hasta).decode("UTF-8"), '', 1, 'C', 1)
            pdf.set_font('Arial', 'B', 10)
            pdf.set_fill_color(255, 255, 255)
            pdf.ln(5)
            pdf.set_fill_color(255, 255, 255)
            pdf.set_font('Arial', 'B', 10)
            pdf.cell(20, 5, "Institución: ".decode("UTF-8"), '', 0, 'L', 1)
            pdf.set_font('Arial', '', 10)
            pdf.cell(150, 5, nom_institucion.decode("UTF-8"), '', 1, 'L', 1)
            pdf.set_font('Arial', 'B', 10)
            pdf.cell(29, 5, "Tipo de reporte:".decode("UTF-8"), '', 0, 'L', 1)
            pdf.set_font('Arial', '', 10)
            pdf.cell(150, 5, (tipo_reporte).decode("UTF-8"), '', 1, 'L', 1)
            pdf.set_font('Arial', 'B', 10)
            pdf.ln(5)

            # Fila de la cabezara de la tabla
            if tipo == '12':
                pdf.set_fill_color(0, 0, 0)
                pdf.set_text_color(255, 255, 255)
                pdf.set_font('Arial', 'B', 10)
                pdf.cell(7, 5, "#".decode("UTF-8"), 'LTBR', 0, 'C', 1)
                pdf.cell(17, 5, "Cédula".decode("UTF-8"), 'LTBR', 0, 'C', 1)
                pdf.cell(50, 5, "Nombre".decode("UTF-8"), 'LTBR', 0, 'C', 1)
                pdf.cell(50, 5, "Apellido".decode("UTF-8"), 'LTBR', 0, 'C', 1)
                pdf.cell(25, 5, "Cod Centro".decode("UTF-8"), 'LTBR', 0, 'C',
                         1)
                pdf.cell(100, 5, "Centro de Votación".decode("UTF-8"), 'LTBR',
                         1, 'C', 1)
                pdf.set_fill_color(255, 255, 255)
                pdf.set_font('Arial', '', 10)
                j = 0
                o = 0
                h = 0
            elif tipo == '13':
                pdf.set_fill_color(0, 0, 0)
                pdf.set_text_color(255, 255, 255)
                pdf.set_font('Arial', 'B', 10)
                pdf.cell(7, 5, "#".decode("UTF-8"), 'LTBR', 0, 'C', 1)
                pdf.cell(17, 5, "Cédula".decode("UTF-8"), 'LTBR', 0, 'C', 1)
                pdf.cell(50, 5, "Nombre".decode("UTF-8"), 'LTBR', 0, 'C', 1)
                pdf.cell(50, 5, "Apellido".decode("UTF-8"), 'LTBR', 0, 'C', 1)
                pdf.cell(125, 5, "Twitter".decode("UTF-8"), 'LTBR', 1, 'C', 1)
                pdf.set_fill_color(255, 255, 255)
                pdf.set_font('Arial', '', 10)
                j = 0
                o = 0
                h = 0
            elif tipo == '14':
                pdf.set_fill_color(0, 0, 0)
                pdf.set_text_color(255, 255, 255)
                pdf.set_font('Arial', 'B', 10)
                pdf.cell(15, 5, "Cédula".decode("UTF-8"), 'LTBR', 0, 'C', 1)
                pdf.cell(60, 5, "Nombre y Apellido".decode("UTF-8"), 'LTBR', 0,
                         'C', 1)
                pdf.cell(50, 5, "Motivo".decode("UTF-8"), 'LTBR', 0, 'C', 1)
                pdf.cell(15, 5, "Fecha".decode("UTF-8"), 'LTBR', 0, 'C', 1)
                pdf.cell(50, 5, "Municipio".decode("UTF-8"), 'LTBR', 0, 'C', 1)
                pdf.cell(60, 5, "Organización".decode("UTF-8"), 'LTBR', 0, 'C',
                         1)
                j = 0
                o = 0
                h = 0
            else:
                pdf.set_fill_color(0, 0, 0)
                pdf.set_text_color(255, 255, 255)
                pdf.set_font('Arial', 'B', 10)
                pdf.cell(7, 5, "#".decode("UTF-8"), 'LTBR', 0, 'C', 1)
                pdf.cell(17, 5, "Cédula".decode("UTF-8"), 'LTBR', 0, 'C', 1)
                pdf.cell(50, 5, "Nombre".decode("UTF-8"), 'LTBR', 0, 'C', 1)
                pdf.cell(50, 5, "Apellido".decode("UTF-8"), 'LTBR', 0, 'C', 1)
                pdf.cell(20, 5, "Fecha".decode("UTF-8"), 'LTBR', 0, 'C', 1)
                pdf.cell(90, 5, "Departamento".decode("UTF-8"), 'LTBR', 1, 'C',
                         1)
                pdf.set_fill_color(255, 255, 255)
                pdf.set_font('Arial', '', 10)
                # Fin Cabezera
                j = 0
                o = 0
                h = 0
        item = int(item) + 1
        # print tipo
        if tipo == '12':
            if t['codigo_centro'] is None:
                codigo_c = ''
            else:
                codigo_c = t['codigo_centro']
            if t['nombre_centro'] is None:
                nombre_c = ''
            else:
                nombre_c = t['nombre_centro']
            pdf.set_font('Arial', '', 8)
            pdf.set_fill_color(255, 255, 255)
            pdf.set_text_color(24, 29, 31)
            pdf.set_y(75 + o)
            pdf.set_x(15)
            pdf.cell(7, a, str(item).decode("UTF-8"), 'LTBR', 0, 'C', 1)
            pdf.set_y(75 + o)
            pdf.set_x(22)
            pdf.cell(17, a, str(t['cedula']), 'LTBR', 0, 'C', 1)
            pdf.set_y(75 + o)
            pdf.set_x(39)
            pdf.cell(50, a,
                     str(t['nombre']).decode("UTF-8"), 'LTBR', 0, 'C', 1)
            pdf.set_y(75 + o)
            pdf.set_x(89)
            pdf.cell(50, a,
                     str(t['apellido']).decode("UTF-8"), 'LTBR', 0, 'C', 1)
            pdf.set_y(75 + o)
            pdf.set_x(139)
            pdf.cell(25, a, str(codigo_c), 'LTBR', 0, 'C', 1)
            pdf.set_y(75 + o)
            pdf.set_x(164)
            pdf.multi_cell(100, 5,
                           str(nombre_c).decode("UTF-8"), 'LTBR', 'C', 1)
            j = j + 1
            ### VAlidacion de la ubicacion (altura J) de los registros en base a la cantidad de lineas de las descripcion
            if p == 1:
                o = o + 5
            elif p == 2:
                o = o + 10
        elif tipo == '13':
            dia_e = ''
            pdf.set_font('Arial', '', 8)
            pdf.set_fill_color(255, 255, 255)
            pdf.set_text_color(24, 29, 31)
            pdf.set_y(75 + o)
            pdf.set_x(15)
            pdf.cell(7, a, str(item).decode("UTF-8"), 'LTBR', 0, 'C', 1)
            pdf.set_y(75 + o)
            pdf.set_x(22)
            pdf.cell(17, a, str(t['cedula']), 'LTBR', 0, 'C', 1)
            pdf.set_y(75 + o)
            pdf.set_x(39)
            pdf.cell(50, a,
                     str(t['nombre']).decode("UTF-8"), 'LTBR', 0, 'C', 1)
            pdf.set_y(75 + o)
            pdf.set_x(89)
            pdf.cell(50, a,
                     str(t['apellido']).decode("UTF-8"), 'LTBR', 0, 'C', 1)
            pdf.set_y(75 + o)
            pdf.set_x(139)
            pdf.cell(125, a,
                     str(t['twitter']).decode("UTF-8"), 'LTBR', 1, 'L', 1)
            j = j + 1
            ### VAlidacion de la ubicacion (altura J) de los registros en base a la cantidad de lineas de las descripcion
            if p == 1:
                o = o + 5
            elif p == 2:
                o = o + 10
        elif tipo == '14':
            dia_e = str(t['fecha']).split('-')
            fecha = dia_e[2] + '-' + dia_e[1] + '-' + dia_e[0]
            pdf.set_font('Arial', '', 6.5)
            pdf.set_fill_color(255, 255, 255)
            pdf.set_text_color(24, 29, 31)
            pdf.set_y(75 + o)
            pdf.set_x(15)
            pdf.cell(15, a, str(t['cedula']), 'LTBR', 0, 'C', 1)
            pdf.set_y(75 + o)
            pdf.set_x(30)
            pdf.multi_cell(
                60, a,
                str(t['nombre']) + ' ' + str(t['apellido']).decode("UTF-8"),
                'LTBR', 'J', 1)
            pdf.set_y(75 + o)
            pdf.set_x(90)
            pdf.cell(50, a,
                     str(t['motivo']).decode("UTF-8"), 'LTBR', 0, 'C', 1)
            pdf.set_y(75 + o)
            pdf.set_x(140)
            pdf.cell(15, a, str(t['fecha']).decode("UTF-8"), 'LTBR', 1, 'L', 1)
            pdf.set_y(75 + o)
            pdf.set_x(155)
            pdf.cell(50, a,
                     str(t['municipio']).decode("UTF-8"), 'LTBR', 1, 'L', 1)
            pdf.set_y(75 + o)
            pdf.set_x(205)
            pdf.multi_cell(60, 5,
                           str(t['organizacion']).decode("UTF-8"), 'LTBR', 'J',
                           1)

            j = j + 1
            ### VAlidacion de la ubicacion (altura J) de los registros en base a la cantidad de lineas de las descripcion
            if p == 1:
                o = o + 5
            elif p == 2:
                o = o + 10
        else:
            dia_e = str(t['dia_ent']).split('-')
            dia_e = dia_e[2] + '-' + dia_e[1] + '-' + dia_e[0]

            pdf.set_font('Arial', '', 8)
            pdf.set_fill_color(255, 255, 255)
            pdf.set_text_color(24, 29, 31)
            pdf.set_y(75 + o)
            pdf.set_x(15)
            pdf.cell(7, a, str(item).decode("UTF-8"), 'LTBR', 0, 'C', 1)
            pdf.set_y(75 + o)
            pdf.set_x(22)
            pdf.cell(15, a, str(t['codigo']), 'LTBR', 0, 'C', 1)
            pdf.set_y(75 + o)
            pdf.set_x(37)
            pdf.cell(17, a, str(t['cedula']), 'LTBR', 0, 'C', 1)
            pdf.set_y(75 + o)
            pdf.set_x(54)
            pdf.cell(50, a,
                     str(t['nombre']).decode("UTF-8"), 'LTBR', 0, 'C', 1)
            pdf.set_y(75 + o)
            pdf.set_x(104)
            pdf.cell(50, a,
                     str(t['apellido']).decode("UTF-8"), 'LTBR', 0, 'C', 1)
            pdf.set_y(75 + o)
            pdf.set_x(154)
            pdf.cell(20, a, str(dia_e), 'LTBR', 0, 'C', 1)
            pdf.set_y(75 + o)
            pdf.set_x(174)
            pdf.multi_cell(90, 5,
                           str(t['nom_dependencia']).decode("UTF-8"), 'LTBR',
                           'C', 1)
            j = j + 1
            ### VAlidacion de la ubicacion (altura J) de los registros en base a la cantidad de lineas de las descripcion
            if p == 1:
                o = o + 5
            elif p == 2:
                o = o + 10

    ruta_reporte = settings.MEDIA_PDF
    nombre = ' Detallado.pdf'
    pdf.output(ruta_reporte + '/' + nombre, 'F')
    archivo = open(ruta_reporte + '/' + nombre, "r")

    ruta = ruta_reporte + '/'
    delete_Files(ruta)

    return nombre, archivo
Ejemplo n.º 2
0
def reporte_candidato_detallado(eleccion, candidato, tipo, estado, circuito,
                                municipio):

    reload(sys)
    sys.setdefaultencoding("utf-8")
    #candidato = kwargs.get('candidato', None)

    pdf = class_pdf.ReporteCandidato(
        orientation='P', unit='mm',
        format='letter')  # HORIENTACION DE LA PAGINA

    pdf.set_author('Marcel Arcuri')
    pdf.alias_nb_pages()  # LLAMADA DE PAGINACION
    pdf.add_page()  # ANADE UNA NUEVA PAGINACION
    pdf.set_margins(19, 10, 10)  # MARGENE DEL DOCUMENTO

    pdf.set_fill_color(255, 255, 255)
    pdf.set_font('Arial', 'B', 12)

    cursor = connection.cursor()
    sql_cand = "SELECT nombre FROM elecciones_eleccion"
    sql_cand += " WHERE id=%s "
    cursor.execute(sql_cand, [eleccion])
    row = dictfetchall(cursor)
    nombre = row[0]['nombre']
    pdf.ln(1)
    pdf.cell(180, 5, str(nombre).decode("UTF-8"), '', 1, 'C', 1)

    cursor = connection.cursor()
    sql_cand = "SELECT CONCAT(nombre,' ',apellido) nom_ape FROM candidatos_candidatos "
    sql_cand += " WHERE id=%s "
    cursor.execute(sql_cand, [candidato])
    row = dictfetchall(cursor)
    nom_ape = row[0]['nom_ape']

    pdf.set_font('Arial', 'B', 10)
    pdf.cell(180, 5, "(" + str(nom_ape) + ")".decode("UTF-8"), '', 1, 'C', 1)

    pdf.set_font('Arial', 'B', 10)
    if tipo == '2':
        sql_estado = "SELECT estado FROM estados_estado WHERE id=%s"
        cursor.execute(sql_estado, [estado])
        est = dictfetchall(cursor)
        nom_estado = est[0]['estado']
        pdf.cell(15, 5, "Estado:".decode("UTF-8"), '', 0, 'L', 1)
        pdf.set_font('Arial', '', 10)
        pdf.cell(90, 5, str(nom_estado).decode("UTF-8"), '', 1, 'L', 1)
    elif tipo == '3':
        sql_circuito = "SELECT c.n_circuito, e.estado FROM circuitos_circuito AS c "
        sql_circuito += " INNER JOIN estados_estado AS e ON e.id = c.estado_id "
        sql_circuito += " WHERE c.id=%s"
        cursor.execute(sql_circuito, [circuito])
        cir = dictfetchall(cursor)
        n_circuito = cir[0]['n_circuito']
        nom_estado = cir[0]['estado']
        pdf.cell(15, 5, "Estado:".decode("UTF-8"), '', 0, 'L', 1)
        pdf.set_font('Arial', '', 10)
        pdf.cell(90, 5, str(nom_estado).decode("UTF-8"), '', 1, 'L', 1)
        pdf.set_font('Arial', 'B', 10)
        pdf.cell(15, 5, "Circuito:".decode("UTF-8"), '', 0, 'L', 1)
        pdf.set_font('Arial', '', 10)
        pdf.cell(90, 5, str(n_circuito).decode("UTF-8"), '', 1, 'L', 1)
    elif tipo == '4':
        sql_municipio = "SELECT c.n_circuito, e.estado, m.municipio FROM municipios_municipio AS m "
        sql_municipio += " INNER JOIN circuitos_circuito AS c ON c.id = m.circuito"
        sql_municipio += " INNER JOIN estados_estado AS e ON e.id = c.estado_id"
        sql_municipio += " WHERE m.id=%s"
        cursor.execute(sql_municipio, [municipio])
        mun = dictfetchall(cursor)
        n_circuito = mun[0]['n_circuito']
        nom_estado = mun[0]['estado']
        nom_municipio = mun[0]['municipio']
        pdf.cell(14, 5, "Estado:".decode("UTF-8"), '', 0, 'L', 1)
        pdf.set_font('Arial', '', 10)
        pdf.cell(90, 5, str(nom_estado).decode("UTF-8"), '', 1, 'L', 1)
        pdf.set_font('Arial', 'B', 10)
        pdf.cell(16, 5, "Circuito:".decode("UTF-8"), '', 0, 'L', 1)
        pdf.set_font('Arial', '', 10)
        pdf.cell(90, 5, str(n_circuito).decode("UTF-8"), '', 1, 'L', 1)
        pdf.set_font('Arial', 'B', 10)
        pdf.cell(18, 5, "Municipio:".decode("UTF-8"), '', 0, 'L', 1)
        pdf.set_font('Arial', '', 10)
        pdf.cell(90, 5, str(nom_municipio).decode("UTF-8"), '', 1, 'L', 1)

    pdf.ln(5)

    # Fila de la cabezara de la tabla
    pdf.set_fill_color(0, 121, 194)
    pdf.set_text_color(255, 255, 255)
    pdf.set_font('Arial', 'B', 11)
    #pdf.cell(10,10,"N°".decode("UTF-8"),'LTBR',0,'C',1)
    pdf.cell(110, 8, "Ganador / Empate / Perdedor".decode("UTF-8"), 'LTBR', 0,
             'C', 1)
    pdf.cell(25, 8, "Obtenidos".decode("UTF-8"), 'LTBR', 0, 'C', 1)
    pdf.cell(25, 8, "Totales".decode("UTF-8"), 'LTBR', 0, 'C', 1)
    pdf.cell(20, 8, "%".decode("UTF-8"), 'LTBR', 1, 'C', 1)
    pdf.set_fill_color(255, 255, 255)
    item = 0
    for m in row:
        cursor = connection.cursor()
        sql_cant = "SELECT COUNT(v.candidatos_id) can_v"
        sql_cant += " FROM votacion_votacion v "
        sql_cant += " INNER JOIN elecciones_eleccion e ON v.eleccion_id = e.id"
        sql_cant += " INNER JOIN candidatos_candidatos c ON v.candidatos_id = c.id "

        if tipo == '1':
            sql_cant += " WHERE v.eleccion_id=%s and v.candidatos_id !=%s "
        elif tipo == '2':
            sql_cant += " WHERE v.eleccion_id=%s and v.candidatos_id !=%s AND estado=" + str(
                estado) + " "
        elif tipo == '3':
            sql_cant += " WHERE v.eleccion_id=%s and v.candidatos_id !=%s AND circuito=" + str(
                circuito) + " "
        else:
            sql_cant += " WHERE v.eleccion_id=%s and v.candidatos_id !=%s AND municipio=" + str(
                municipio) + " "
        sql_cant += " GROUP BY v.candidatos_id ORDER BY can_v DESC Limit 1"

        cursor.execute(sql_cant, [eleccion, candidato])
        cant = dictfetchall(cursor)
        if cant == []:
            can_v = 0
        else:
            can_v = cant[0]['can_v']

        cursor = connection.cursor()
        sql_res = "SELECT e.nombre eleccion,"
        if tipo == '1':
            sql_res += " COUNT(v.candidatos_id) total_c,(SELECT COUNT (v.candidatos_id) FROM votacion_votacion v WHERE v.eleccion_id=%s) total_v,"
            sql_res += " ROUND (COUNT (v.candidatos_id) * 100.0 / (SELECT COUNT (v.candidatos_id) FROM votacion_votacion v WHERE v.eleccion_id=%s), 2) porcentaje"
        elif tipo == '2':
            sql_res += " COUNT(v.candidatos_id) total_c,(SELECT COUNT (v.candidatos_id) FROM votacion_votacion v WHERE v.eleccion_id=%s AND v.estado=" + str(
                estado) + ") total_v,"
            sql_res += " ROUND (COUNT (v.candidatos_id) * 100.0 / (SELECT COUNT (v.candidatos_id) FROM votacion_votacion v WHERE v.eleccion_id=%s AND v.estado=" + str(
                estado) + "), 2) porcentaje"
        elif tipo == '3':
            sql_res += " COUNT(v.candidatos_id) total_c,(SELECT COUNT (v.candidatos_id) FROM votacion_votacion v WHERE v.eleccion_id=%s AND v.circuito=" + str(
                circuito) + ") total_v,"
            sql_res += " ROUND (COUNT (v.candidatos_id) * 100.0 / (SELECT COUNT (v.candidatos_id) FROM votacion_votacion v WHERE v.eleccion_id=%s AND v.circuito=" + str(
                circuito) + "), 2) porcentaje"
        else:
            sql_res += " COUNT(v.candidatos_id) total_c,(SELECT COUNT (v.candidatos_id) FROM votacion_votacion v WHERE v.eleccion_id=%s AND municipio=" + str(
                municipio) + ") total_v,"
            sql_res += " ROUND (COUNT (v.candidatos_id) * 100.0 / (SELECT COUNT (v.candidatos_id) FROM votacion_votacion v WHERE v.eleccion_id=%s AND municipio=" + str(
                municipio) + "), 2) porcentaje"
        sql_res += " FROM votacion_votacion v"
        sql_res += " INNER JOIN elecciones_eleccion e ON v.eleccion_id = e.id"
        sql_res += " INNER JOIN candidatos_candidatos c ON v.candidatos_id = c.id"
        if tipo == '1':
            sql_res += " WHERE v.candidatos_id=%s and v.eleccion_id=%s "
        elif tipo == '2':
            sql_res += " WHERE v.candidatos_id=%s and v.eleccion_id=%s AND estado=" + str(
                estado) + " "
        elif tipo == '3':
            sql_res += " WHERE v.candidatos_id=%s and v.eleccion_id=%s AND circuito=" + str(
                circuito) + " "
        else:
            sql_res += " WHERE v.candidatos_id=%s and v.eleccion_id=%s AND municipio=" + str(
                municipio) + " "
        sql_res += " GROUP BY e.nombre"

        cursor.execute(sql_res, [eleccion, eleccion, candidato, eleccion])
        row = dictfetchall(cursor)
        # eleccion = row[0]['eleccion']
        total_c = row[0]['total_c']
        total_v = row[0]['total_v']
        porcentaje = row[0]['porcentaje']

        if total_c > can_v:
            resultado = 'Ganador'
        elif total_c == can_v:
            resultado = 'Empate'
        else:
            resultado = 'Perdedor'

        for p in row:

            item = int(item) + 1
            pdf.set_fill_color(0, 0, 0)
            pdf.cell(180, 1, "", 'LTBR', 1, 'C', 1)
            pdf.set_fill_color(217, 236, 247)
            pdf.set_text_color(24, 29, 31)
            pdf.set_font('Arial', 'B', 10)
            #pdf.cell(10,6,str(item).decode("UTF-8"),'LTBR',0,'C',1)
            pdf.cell(110, 6, str(resultado).decode("UTF-8"), 'LTBR', 0, 'L', 1)
            pdf.cell(25, 6,
                     str(p['total_c']).decode("UTF-8"), 'LTBR', 0, 'R', 1)
            pdf.cell(25, 6,
                     str(p['total_v']).decode("UTF-8"), 'LTBR', 0, 'R', 1)
            pdf.cell(20, 6,
                     str(p['porcentaje']) + ' %'.decode("UTF-8"), 'LTBR', 1,
                     'R', 1)
            pdf.set_fill_color(255, 255, 255)
            pdf.set_text_color(24, 29, 31)
            pdf.set_font('Arial', 'B', 10)
            #pdf.cell(10,6,"".decode("UTF-8"),'LTR',0,'C',1)
            pdf.set_fill_color(191, 191, 191)
            pdf.cell(15, 6, "N°".decode("UTF-8"), 'LTBR', 0, 'C', 1)
            pdf.cell(95, 6, "Grupo Etáreo".decode("UTF-8"), 'LTBR', 0, 'C', 1)
            pdf.cell(50, 6, "N° de Votos".decode("UTF-8"), 'LTBR', 0, 'C', 1)
            pdf.cell(20, 6, "(%)".decode("UTF-8"), 'LTBR', 1, 'C', 1)

            pdf.set_font('Arial', '', 9)  # TAMANO DE LA FUENTE
            sql_gru = " SELECT CONCAT (g.descripcion,' (',g.desde,'-',g.hasta,' Años)') descripcion, "

            if tipo == '1':
                sql_gru += " (SELECT COUNT (grupo_etareo) from votacion_votacion WHERE eleccion_id=%s and candidatos_id=%s AND grupo_etareo BETWEEN g.desde::integer AND g.hasta::integer) total, "
                sql_gru += " ROUND ((SELECT COUNT (grupo_etareo) from votacion_votacion WHERE eleccion_id=%s and candidatos_id=%s AND grupo_etareo BETWEEN g.desde::integer AND g.hasta::integer)*100.0 / "
                sql_gru += " (SELECT COUNT (v.grupo_etareo) FROM votacion_votacion v WHERE v.eleccion_id=%s and candidatos_id=%s), 2) porcentaje "
            elif tipo == '2':
                sql_gru += " (SELECT COUNT (grupo_etareo) from votacion_votacion WHERE eleccion_id=%s and candidatos_id=%s AND estado=" + str(
                    estado
                ) + " AND grupo_etareo BETWEEN g.desde::integer AND g.hasta::integer) total, "
                sql_gru += " ROUND ((SELECT COUNT (grupo_etareo) from votacion_votacion WHERE eleccion_id=%s and candidatos_id=%s AND estado=" + str(
                    estado
                ) + " AND grupo_etareo BETWEEN g.desde::integer AND g.hasta::integer)*100.0 / "
                sql_gru += " (SELECT COUNT (v.grupo_etareo) FROM votacion_votacion v WHERE v.eleccion_id=%s and candidatos_id=%s AND estado=" + str(
                    estado) + "), 2) porcentaje "
            elif tipo == '3':
                sql_gru += " (SELECT COUNT (grupo_etareo) from votacion_votacion WHERE eleccion_id=%s and candidatos_id=%s AND circuito=" + str(
                    circuito
                ) + " AND grupo_etareo BETWEEN g.desde::integer AND g.hasta::integer) total, "
                sql_gru += " ROUND ((SELECT COUNT (grupo_etareo) from votacion_votacion WHERE eleccion_id=%s and candidatos_id=%s AND circuito=" + str(
                    circuito
                ) + " AND grupo_etareo BETWEEN g.desde::integer AND g.hasta::integer)*100.0 / "
                sql_gru += " (SELECT COUNT (v.grupo_etareo) FROM votacion_votacion v WHERE v.eleccion_id=%s and candidatos_id=%s AND circuito=" + str(
                    circuito) + "), 2) porcentaje "
            elif tipo == '4':
                sql_gru += " (SELECT COUNT (grupo_etareo) from votacion_votacion WHERE eleccion_id=%s and candidatos_id=%s AND municipio=" + str(
                    municipio
                ) + " AND grupo_etareo BETWEEN g.desde::integer AND g.hasta::integer) total, "
                sql_gru += " ROUND ((SELECT COUNT (grupo_etareo) from votacion_votacion WHERE eleccion_id=%s and candidatos_id=%s AND municipio=" + str(
                    municipio
                ) + " AND grupo_etareo BETWEEN g.desde::integer AND g.hasta::integer)*100.0 / "
                sql_gru += " (SELECT COUNT (v.grupo_etareo) FROM votacion_votacion v WHERE v.eleccion_id=%s and candidatos_id=%s AND municipio=" + str(
                    municipio) + "), 2) porcentaje "
            sql_gru += " FROM grupo_etareo_grupo_etareo AS g ORDER BY porcentaje DESC"
            cursor.execute(sql_gru, [
                eleccion, candidato, eleccion, candidato, eleccion, candidato
            ])

            row = dictfetchall(cursor)
            descripcion = row[0]['descripcion']
            total = row[0]['total']
            porcentaje = row[0]['porcentaje']

            item2 = 0
            for h in row:
                pdf.set_fill_color(255, 255, 255)  # COLOR DE BOLDE DE LA CELDA

                item2 = int(item2) + 1
                pdf.set_text_color(24, 29, 31)
                #pdf.cell(10,6,"".decode("UTF-8"),'LR',0,'C',1)
                pdf.cell(15, 6, str(item2).decode("UTF-8"), 'LTBR', 0, 'C', 1)
                pdf.cell(95, 6,
                         str(h['descripcion']).decode("UTF-8"), 'LTBR', 0, 'L',
                         1)
                pdf.cell(50, 6, str(h['total']), 'LTBR', 0, 'R', 1)
                pdf.cell(20, 6, str(h['porcentaje']) + ' %', 'LTBR', 1, 'R', 1)
                pdf.set_fill_color(255, 255, 255)

            pdf.set_fill_color(255, 255, 255)
            pdf.set_text_color(24, 29, 31)
            pdf.set_font('Arial', 'B', 10)
            #pdf.cell(10,6,"".decode("UTF-8"),'LTR',0,'C',1)
            pdf.set_fill_color(191, 191, 191)
            pdf.cell(15, 6, "N°".decode("UTF-8"), 'LTBR', 0, 'C', 1)
            pdf.cell(95, 6, "Sexo".decode("UTF-8"), 'LTBR', 0, 'C', 1)
            pdf.cell(50, 6, "N° de Votos".decode("UTF-8"), 'LTBR', 0, 'C', 1)
            pdf.cell(20, 6, "(%)".decode("UTF-8"), 'LTBR', 1, 'C', 1)
            pdf.set_fill_color(255, 255, 255)

            pdf.set_font('Arial', '', 9)  # TAMANO DE LA FUENTE
            sql_sexo = "SELECT COUNT(v.sexo) total, v.sexo sexo, "
            if tipo == '1':
                sql_sexo += " ROUND (COUNT (v.sexo) * 100.0 / (SELECT COUNT (v.candidatos_id) FROM votacion_votacion v WHERE v.candidatos_id=%s and v.eleccion_id=%s), 2) porcentaje "
            elif tipo == '2':
                sql_sexo += " ROUND (COUNT (v.sexo) * 100.0 / (SELECT COUNT (v.candidatos_id) FROM votacion_votacion v WHERE v.candidatos_id=%s and v.eleccion_id=%s AND v.estado=" + str(
                    estado) + "), 2) porcentaje "
            elif tipo == '3':
                sql_sexo += " ROUND (COUNT (v.sexo) * 100.0 / (SELECT COUNT (v.candidatos_id) FROM votacion_votacion v WHERE v.candidatos_id=%s and v.eleccion_id=%s AND v.circuito=" + str(
                    circuito) + "), 2) porcentaje "
            else:
                sql_sexo += " ROUND (COUNT (v.sexo) * 100.0 / (SELECT COUNT (v.candidatos_id) FROM votacion_votacion v WHERE v.candidatos_id=%s and v.eleccion_id=%s AND v.municipio=" + str(
                    municipio) + "), 2) porcentaje "
            sql_sexo += " FROM votacion_votacion v"
            sql_sexo += " INNER JOIN elecciones_eleccion e ON v.eleccion_id = e.id"
            sql_sexo += " INNER JOIN candidatos_candidatos c ON v.candidatos_id = c.id"
            if tipo == '1':
                sql_sexo += " WHERE v.eleccion_id=%s and v.candidatos_id=%s "
            elif tipo == '2':
                sql_sexo += " WHERE v.eleccion_id=%s and v.candidatos_id=%s AND v.estado=" + str(
                    estado) + " "
            elif tipo == '3':
                sql_sexo += " WHERE v.eleccion_id=%s and v.candidatos_id=%s AND v.circuito=" + str(
                    circuito) + " "
            else:
                sql_sexo += " WHERE v.eleccion_id=%s and v.candidatos_id=%s AND v.municipio=" + str(
                    municipio) + " "

            sql_sexo += " GROUP BY v.sexo ORDER BY total DESC "
            cursor.execute(sql_sexo,
                           [candidato, eleccion, eleccion, candidato])
            row = dictfetchall(cursor)
            total = row[0]['total']
            porcentaje = row[0]['porcentaje']

            item3 = 0
            for q in row:
                pdf.set_fill_color(255, 255, 255)  # COLOR DE BOLDE DE LA CELDA
                sexo = q['sexo']
                if sexo == '2':
                    genero = 'Masculino'
                else:
                    genero = 'Femenino'

                item3 = int(item3) + 1
                pdf.set_text_color(24, 29, 31)
                pdf.cell(15, 6, str(item3).decode("UTF-8"), 'LTBR', 0, 'C', 1)
                pdf.cell(95, 6, str(genero).decode("UTF-8"), 'LTBR', 0, 'L', 1)
                pdf.cell(50, 6, str(q['total']), 'LTBR', 0, 'R', 1)
                pdf.cell(20, 6, str(q['porcentaje']) + ' %', 'LTBR', 1, 'R', 1)
                pdf.set_fill_color(255, 255, 255)

    pdf.cell(180, 6, "".decode("UTF-8"), 'T', 0, 'C', 1)

    ruta_reporte = settings.MEDIA_PDF
    nombre = str(nombre) + ' ' + str(nom_ape) + '.pdf'
    pdf.output(ruta_reporte + '/' + nombre, 'F')
    archivo = open(ruta_reporte + '/' + nombre, "r")

    ruta = ruta_reporte + '/'
    delete_Files(ruta)

    return nombre, archivo
Ejemplo n.º 3
0
def reporte_expo(desde, hasta):

    reload(sys)
    sys.setdefaultencoding("utf-8")

    pdf = class_pdf.ReporteCandidato(
        orientation='P', unit='mm',
        format='letter')  # HORIENTACION DE LA PAGINA
    year = time.strftime("%Y")
    pdf.set_author('Marcel Arcuri')
    pdf.alias_nb_pages()  # LLAMADA DE PAGINACION
    pdf.add_page()  # ANADE UNA NUEVA PAGINACION
    pdf.set_margins(15, 10, 10)  # MARGENE DEL DOCUMENTO

    pdf.set_text_color(0, 0, 0)
    pdf.set_fill_color(255, 255, 255)
    pdf.set_y(7)
    pdf.set_x(85)
    pdf.write(30, "WIFI EXPO ARAGUA " + str(year).decode("UTF-8"))

    pdf.ln(30)
    pdf.set_text_color(0, 0, 0)
    pdf.set_fill_color(255, 255, 255)
    pdf.set_font('Arial', 'B', 12)
    pdf.cell(
        180, 5,
        "Listado de usuarios conectados al wifi de la Expo Aragua Potencia " +
        str(year).decode("UTF-8"), '', 1, 'C', 1)
    pdf.set_font('Arial', 'B', 10)
    pdf.ln(5)

    # Fila de la cabezara de la tabla
    # pdf.cell(10,5,"".decode("UTF-8"),'',0,'C',1)
    pdf.set_fill_color(0, 0, 0)
    pdf.set_text_color(255, 255, 255)
    pdf.set_font('Arial', 'B', 10)
    pdf.cell(20, 5, "N°".decode("UTF-8"), 'LTBR', 0, 'C', 1)
    pdf.cell(25, 5, "Usuarios".decode("UTF-8"), 'LTBR', 0, 'C', 1)
    pdf.cell(25, 5, "Aumento".decode("UTF-8"), 'LTBR', 0, 'C', 1)
    pdf.cell(30, 5, "Kb/s Bajada".decode("UTF-8"), 'LTBR', 0, 'C', 1)
    pdf.cell(30, 5, "Kb/s Subida".decode("UTF-8"), 'LTBR', 0, 'C', 1)
    pdf.cell(55, 5, "Hora".decode("UTF-8"), 'LTBR', 1, 'C', 1)
    pdf.set_fill_color(255, 255, 255)
    pdf.set_font('Arial', '', 10)

    cursor = connection.cursor()
    sql_det = "SELECT cantidad, subida_usu, bajada_usu, aumento, to_char(date_create, 'dd/mm/YYYY') as fecha_s, hora "
    sql_det += " FROM cantidad_usuario_cantidadusuario WHERE date_create  between %s and %s"
    sql_det += " ORDER BY id "
    # print sql_det
    cursor.execute(sql_det, [desde, hasta])
    row = dictfetchall(cursor)

    i = 1
    j = 0
    item = 0
    for t in row:
        # id_c = t['id_c']
        pdf.set_fill_color(255, 255, 255)
        if j == 33:
            pdf.add_page()
            pdf.set_text_color(0, 0, 0)
            pdf.set_fill_color(255, 255, 255)
            pdf.set_y(7)
            pdf.set_x(85)
            pdf.set_font('Arial', 'B', 16)
            pdf.write(30, "WIFI EXPO ARAGUA 2015".decode("UTF-8"))

            pdf.ln(30)
            pdf.set_text_color(0, 0, 0)
            pdf.set_fill_color(255, 255, 255)
            pdf.set_font('Arial', 'B', 12)
            pdf.cell(
                180, 5,
                "Listado de candidad de equipos conectados al wifi de la Expo Aragua Potencia 2015"
                .decode("UTF-8"), '', 1, 'C', 1)
            pdf.set_font('Arial', 'B', 10)
            pdf.ln(5)

            # Fila de la cabezara de la tabla
            # pdf.cell(10,5,"".decode("UTF-8"),'',0,'C',1)
            pdf.set_fill_color(0, 0, 0)
            pdf.set_text_color(255, 255, 255)
            pdf.set_font('Arial', 'B', 10)
            pdf.cell(20, 5, "N°".decode("UTF-8"), 'LTBR', 0, 'C', 1)
            pdf.cell(25, 5, "Usuarios".decode("UTF-8"), 'LTBR', 0, 'C', 1)
            pdf.cell(25, 5, "Aumento".decode("UTF-8"), 'LTBR', 0, 'C', 1)
            pdf.cell(30, 5, "Kb/s Bajada".decode("UTF-8"), 'LTBR', 0, 'C', 1)
            pdf.cell(30, 5, "Kb/s Subida".decode("UTF-8"), 'LTBR', 0, 'C', 1)
            pdf.cell(55, 5, "Hora".decode("UTF-8"), 'LTBR', 1, 'C', 1)
            pdf.set_fill_color(255, 255, 255)
            pdf.set_font('Arial', '', 10)
            # Fin Cabezera
            j = 0
        item = int(item) + 1

        pdf.set_font('Arial', '', 8)
        pdf.set_fill_color(255, 255, 255)
        pdf.set_text_color(24, 29, 31)
        # pdf.cell(10,5,"".decode("UTF-8"),'',0,'C',1)
        pdf.cell(20, 5, str(item).decode("UTF-8"), 'LTBR', 0, 'C', 1)
        pdf.cell(25, 5, str(t['cantidad']).decode("UTF-8"), 'LTBR', 0, 'C', 1)
        pdf.cell(25, 5, str(t['aumento']), 'LTBR', 0, 'C', 1)
        pdf.cell(30, 5, str(t['subida_usu']) + ' Kb/s', 'LTBR', 0, 'C', 1)
        pdf.cell(30, 5,
                 str(t['bajada_usu']) + ' Kb/s'.decode("UTF-8"), 'LTBR', 0,
                 'C', 1)
        pdf.cell(55, 5,
                 str(t['fecha_s']) + ' ' + str(t['hora']), 'LTBR', 1, 'C', 1)
        j = j + 1

    cursor = connection.cursor()
    sql_tot = "SELECT SUM(aumento) AS aumento_t, SUM(subida_usu) AS subida_t, SUM(bajada_usu) AS bajada_t  "
    sql_tot += " FROM cantidad_usuario_cantidadusuario WHERE date_create  between %s and %s"
    cursor.execute(sql_tot, [desde, hasta])
    row = dictfetchall(cursor)

    aumento_t = row[0]['aumento_t']
    subida_t = row[0]['subida_t']
    bajada_t = row[0]['bajada_t']

    # pdf.set_text_color(24,29,31)
    # pdf.set_font('Arial','B',9)
    # pdf.set_fill_color(191,191,191)
    # pdf.cell(20,5,"TOTALES".decode("UTF-8"),'LTBR',0,'C',1)
    # pdf.cell(25,5,str(aumento_t),'LTBR',0,'C',1)
    # pdf.cell(25,5,str(subida_t)+"KB/s DE SUBIDA",'LTBR',0,'C',1)
    # pdf.cell(30,5,str(bajada_t)+' KB/s DE BAJADA'.decode("UTF-8"),'LTBR',1,'C',1)

    pdf.ln(10)
    pdf.set_text_color(24, 29, 31)
    pdf.set_font('Arial', 'B', 9)
    pdf.set_fill_color(0, 0, 0)
    pdf.set_text_color(255, 255, 255)
    pdf.cell(185, 5, "TOTALES GENERALES".decode("UTF-8"), 'LTBR', 1, 'C', 1)
    pdf.set_text_color(24, 29, 31)
    pdf.set_fill_color(191, 191, 191)
    pdf.cell(31, 5, "USUARIOS".decode("UTF-8"), 'LTBR', 0, 'C', 1)
    pdf.set_fill_color(255, 255, 255)
    pdf.cell(30, 5, str(aumento_t), 'LTBR', 0, 'C', 1)
    pdf.set_fill_color(191, 191, 191)
    pdf.cell(32, 5, "KB/s DE BAJADA".decode("UTF-8"), 'LTBR', 0, 'C', 1)
    pdf.set_fill_color(255, 255, 255)
    pdf.cell(30, 5, str(subida_t) + ' Kb/s', 'LTBR', 0, 'C', 1)
    pdf.set_fill_color(191, 191, 191)
    pdf.cell(32, 5, "KB/s DE SUBIDA".decode("UTF-8"), 'LTBR', 0, 'C', 1)
    pdf.set_fill_color(255, 255, 255)
    pdf.cell(30, 5, str(bajada_t) + ' Kb/s'.decode("UTF-8"), 'LTBR', 1, 'C', 1)

    cursor = connection.cursor()
    sql_sop = " SELECT (CAST(date_create::date AS VARCHAR)) AS fecha, COUNT(*) AS total"
    # sql_sop += " SUM(subida_usu) AS sub, SUM(bajada_usu) AS baj, "
    sql_sop += " FROM cantidad_usuario_cantidadusuario GROUP BY date_create::date ORDER BY fecha ASC "
    cursor.execute(sql_sop)
    row2 = dictfetchall(cursor)

    pdf.ln(5)

    pdf.set_font('Arial', 'B', 9)
    pdf.set_fill_color(0, 0, 0)
    pdf.set_text_color(255, 255, 255)
    pdf.cell(40, 5, "TOTALES DIARIOS".decode("UTF-8"), 'LTBR', 1, 'C', 1)
    pdf.set_fill_color(191, 191, 191)
    pdf.set_text_color(24, 29, 31)
    pdf.cell(20, 5, "DÍA".decode("UTF-8"), 'LTBR', 0, 'C', 1)
    pdf.cell(20, 5, "USUARIOS".decode("UTF-8"), 'LTBR', 1, 'C', 1)

    for m in row2:
        dias = m['fecha']
        dia = dias.split('-')
        pdf.set_fill_color(191, 191, 191)
        pdf.cell(20, 5, str(dia[2]) + ' Octubre', 'LTBR', 0, 'C', 1)
        pdf.set_fill_color(255, 255, 255)
        pdf.cell(20, 5, str(m['total']).decode("UTF-8"), 'LTBR', 1, 'C', 1)

    ruta_reporte = settings.MEDIA_PDF
    nombre = ' Detallado.pdf'
    pdf.output(ruta_reporte + '/' + nombre, 'F')
    archivo = open(ruta_reporte + '/' + nombre, "r")

    ruta = ruta_reporte + '/'
    delete_Files(ruta)

    return nombre, archivo
def detallado_eleccion_candidatos(exitpoll, tipo, estado, circuito, municipio):

    reload(sys)
    sys.setdefaultencoding("utf-8")

    pdf = class_pdf.ReporteCandidato(
        orientation='P', unit='mm',
        format='letter')  # HORIENTACION DE LA PAGINA

    pdf.set_author('Marcel Arcuri')
    pdf.alias_nb_pages()  # LLAMADA DE PAGINACION
    pdf.add_page()  # ANADE UNA NUEVA PAGINACION
    pdf.set_margins(19, 10, 10)  # MARGENE DEL DOCUMENTO

    cursor = connection.cursor()
    sql_ele = "SELECT e.nombre FROM votacion_votacion v INNER JOIN elecciones_eleccion e ON v.eleccion_id = e.id "
    sql_ele += " WHERE v.eleccion_id=%s GROUP BY e.nombre "
    cursor.execute(sql_ele, [exitpoll])
    row = dictfetchall(cursor)
    eleccion = row[0]['nombre']

    pdf.ln(1)
    pdf.set_fill_color(255, 255, 255)
    pdf.set_font('Arial', 'B', 12)
    pdf.cell(180, 5, eleccion.decode("UTF-8"), '', 1, 'C', 1)
    pdf.set_font('Arial', 'B', 10)
    if tipo == '2':
        sql_estado = "SELECT estado FROM estados_estado WHERE id=%s"
        cursor.execute(sql_estado, [estado])
        est = dictfetchall(cursor)
        nom_estado = est[0]['estado']
        pdf.cell(15, 5, "Estado:".decode("UTF-8"), '', 0, 'L', 1)
        pdf.set_font('Arial', '', 10)
        pdf.cell(90, 5, str(nom_estado).decode("UTF-8"), '', 1, 'L', 1)
    elif tipo == '3':
        sql_circuito = "SELECT c.n_circuito, e.estado FROM circuitos_circuito AS c "
        sql_circuito += " INNER JOIN estados_estado AS e ON e.id = c.estado_id "
        sql_circuito += " WHERE c.id=%s"
        cursor.execute(sql_circuito, [circuito])
        cir = dictfetchall(cursor)
        n_circuito = cir[0]['n_circuito']
        nom_estado = cir[0]['estado']
        pdf.cell(15, 5, "Estado:".decode("UTF-8"), '', 0, 'L', 1)
        pdf.set_font('Arial', '', 10)
        pdf.cell(90, 5, str(nom_estado).decode("UTF-8"), '', 1, 'L', 1)
        pdf.set_font('Arial', 'B', 10)
        pdf.cell(15, 5, "Circuito:".decode("UTF-8"), '', 0, 'L', 1)
        pdf.set_font('Arial', '', 10)
        pdf.cell(90, 5, str(n_circuito).decode("UTF-8"), '', 1, 'L', 1)
    elif tipo == '4':
        sql_municipio = "SELECT c.n_circuito, e.estado, m.municipio FROM municipios_municipio AS m "
        sql_municipio += " INNER JOIN circuitos_circuito AS c ON c.id = m.circuito"
        sql_municipio += " INNER JOIN estados_estado AS e ON e.id = c.estado_id"
        sql_municipio += " WHERE m.id=%s"
        cursor.execute(sql_municipio, [municipio])
        mun = dictfetchall(cursor)
        n_circuito = mun[0]['n_circuito']
        nom_estado = mun[0]['estado']
        nom_municipio = mun[0]['municipio']
        pdf.cell(14, 5, "Estado:".decode("UTF-8"), '', 0, 'L', 1)
        pdf.set_font('Arial', '', 10)
        pdf.cell(90, 5, str(nom_estado).decode("UTF-8"), '', 1, 'L', 1)
        pdf.set_font('Arial', 'B', 10)
        pdf.cell(16, 5, "Circuito:".decode("UTF-8"), '', 0, 'L', 1)
        pdf.set_font('Arial', '', 10)
        pdf.cell(90, 5, str(n_circuito).decode("UTF-8"), '', 1, 'L', 1)
        pdf.set_font('Arial', 'B', 10)
        pdf.cell(18, 5, "Municipio:".decode("UTF-8"), '', 0, 'L', 1)
        pdf.set_font('Arial', '', 10)
        pdf.cell(90, 5, str(nom_municipio).decode("UTF-8"), '', 1, 'L', 1)

    pdf.ln(5)

    # Fila de la cabezara de la tabla
    pdf.set_fill_color(0, 121, 194)
    pdf.set_text_color(255, 255, 255)
    pdf.set_font('Arial', 'B', 11)
    pdf.cell(15, 8, "N°".decode("UTF-8"), 'LTBR', 0, 'C', 1)
    pdf.cell(105, 8, "Candidatos".decode("UTF-8"), 'LTBR', 0, 'C', 1)
    pdf.cell(40, 8, "N° de Votos".decode("UTF-8"), 'LTBR', 0, 'C', 1)
    pdf.cell(20, 8, "%".decode("UTF-8"), 'LTBR', 1, 'C', 1)
    pdf.set_fill_color(255, 255, 255)

    # Fin Cabezera
    pdf.set_text_color(24, 29, 31)
    pdf.set_font('Arial', '', 10)  # TAMANO DE LA FUENTE
    sql_res = "SELECT CONCAT(c.nombre,' ',c.apellido) nom_ape, COUNT(v.candidatos_id) can_v, "
    if tipo == '1':
        sql_res += " ROUND (COUNT (v.candidatos_id) * 100.0 / (SELECT COUNT (v.candidatos_id) FROM votacion_votacion v WHERE v.eleccion_id=%s), 2) porcentaje"
    elif tipo == '2':
        sql_res += " ROUND (COUNT (v.candidatos_id) * 100.0 / (SELECT COUNT (v.candidatos_id) FROM votacion_votacion v WHERE v.eleccion_id=%s  AND v.estado=" + str(
            estado) + "), 2) porcentaje"
    elif tipo == '3':
        sql_res += " ROUND (COUNT (v.candidatos_id) * 100.0 / (SELECT COUNT (v.candidatos_id) FROM votacion_votacion v WHERE v.eleccion_id=%s  AND v.circuito=" + str(
            circuito) + "), 2) porcentaje"
    else:
        sql_res += "ROUND (COUNT (v.candidatos_id) * 100.0 / (SELECT COUNT (v.candidatos_id) FROM votacion_votacion v WHERE v.eleccion_id=%s  AND v.municipio=" + str(
            municipio) + "), 2) porcentaje"
    sql_res += " FROM votacion_votacion v "
    sql_res += " INNER JOIN elecciones_eleccion e ON v.eleccion_id = e.id"
    sql_res += " INNER JOIN candidatos_candidatos c ON v.candidatos_id = c.id"
    if tipo == '1':
        sql_res += " WHERE v.eleccion_id=%s "
    elif tipo == '2':
        sql_res += " WHERE v.eleccion_id=%s AND v.estado=" + str(estado) + ""
    elif tipo == '3':
        sql_res += " WHERE v.eleccion_id=%s AND v.circuito=" + str(
            circuito) + ""
    else:
        sql_res += " WHERE v.eleccion_id=%s AND v.municipio=" + str(
            municipio) + ""
    sql_res += " GROUP BY v.candidatos_id, c.nombre, c.apellido ORDER BY porcentaje DESC"
    cursor.execute(sql_res, [exitpoll, exitpoll])
    j = 0
    item = 0
    row = dictfetchall(cursor)
    nom_ape = row[0]['nom_ape']
    can_v = row[0]['can_v']
    porcentaje = row[0]['porcentaje']

    pdf.set_fill_color(255, 255, 255)
    i = 1
    for t in row:
        pdf.set_fill_color(255, 255, 255)
        if i % 2 == 0:
            pdf.set_fill_color(221, 219, 219)
        if j == 25:
            pdf.add_page()
            pdf.set_fill_color(255, 255, 255)
            pdf.set_font('Arial', 'B', 12)
            pdf.cell(175, 5, eleccion, 1, 'C', 1)
            pdf.ln(5)
            # Fin Cabezera
            j = 0
        item = int(item) + 1
        # Filas que vienen de la BD
        pdf.set_font('Arial', '', 10)
        pdf.cell(15, 6, str(item).decode("UTF-8"), 'LTBR', 0, 'C', 1)
        pdf.cell(105, 6, str(t['nom_ape']).decode("UTF-8"), 'LTBR', 0, 'L', 1)
        pdf.cell(40, 6, str(t['can_v']), 'LTBR', 0, 'R', 1)
        pdf.cell(20, 6, str(t['porcentaje']) + ' %', 'LTBR', 1, 'R', 1)

        pdf.set_font('Arial', '', 10)  # TAMANO DE LA FUENTE
        j = j + 1

    pdf.set_font('Arial', 'B', 11)
    pdf.set_fill_color(97, 97, 97)
    pdf.set_text_color(255, 255, 255)
    if tipo == '1':
        sql_tot = 'SELECT COUNT(v.id) total FROM votacion_votacion v WHERE v.eleccion_id=%s'
    elif tipo == '2':
        sql_tot = "SELECT COUNT(v.id) total FROM votacion_votacion v WHERE v.eleccion_id=%s AND v.estado=" + str(
            estado) + ""
    elif tipo == '3':
        sql_tot = "SELECT COUNT(v.id) total FROM votacion_votacion v WHERE v.eleccion_id=%s AND v.circuito=" + str(
            circuito) + ""
    else:
        sql_tot = "SELECT COUNT(v.id) total FROM votacion_votacion v WHERE v.eleccion_id=%s AND v.municipio=" + str(
            municipio) + ""

    cursor.execute(sql_tot, [exitpoll])
    row = dictfetchall(cursor)
    total = row[0]['total']

    pdf.cell(120, 6, "TOTAL".decode("UTF-8"), 'LTBR', 0, 'C', 1)
    pdf.cell(40, 6, str(total), 'LTBR', 0, 'R', 1)
    pdf.cell(20, 6, "100 %", 'LTBR', 1, 'R', 1)

    ruta_reporte = settings.MEDIA_PDF
    nombre = str(eleccion) + ' Candidatos.pdf'
    pdf.output(ruta_reporte + '/' + nombre, 'F')
    archivo = open(ruta_reporte + '/' + nombre, "r")

    ruta = ruta_reporte + '/'
    delete_Files(ruta)

    return nombre, archivo
def reporte_eleccion_sexo(sexo, tipo, estado, circuito, municipio):

    reload(sys)
    sys.setdefaultencoding("utf-8")

    pdf = class_pdf.ReporteCandidato(
        orientation='P', unit='mm',
        format='letter')  # HORIENTACION DE LA PAGINA

    pdf.set_author('Marcel Arcuri')
    pdf.alias_nb_pages()  # LLAMADA DE PAGINACION
    pdf.add_page()  # ANADE UNA NUEVA PAGINACION
    pdf.set_margins(19, 10, 10)  # MARGENE DEL DOCUMENTO

    cursor = connection.cursor()
    sql_ele = "SELECT e.nombre FROM votacion_votacion v INNER JOIN elecciones_eleccion e ON v.eleccion_id = e.id "
    sql_ele += " WHERE v.eleccion_id=%s GROUP BY e.nombre "
    cursor.execute(sql_ele, [sexo])
    row = dictfetchall(cursor)
    eleccion = row[0]['nombre']

    pdf.ln(1)
    pdf.set_fill_color(255, 255, 255)
    pdf.set_font('Arial', 'B', 12)
    pdf.cell(180, 5, eleccion.decode("UTF-8"), '', 1, 'C', 1)
    pdf.set_font('Arial', 'B', 10)
    if tipo == '2':
        sql_estado = "SELECT estado FROM estados_estado WHERE id=%s"
        cursor.execute(sql_estado, [estado])
        est = dictfetchall(cursor)
        nom_estado = est[0]['estado']
        pdf.cell(15, 5, "Estado:".decode("UTF-8"), '', 0, 'L', 1)
        pdf.set_font('Arial', '', 10)
        pdf.cell(90, 5, str(nom_estado).decode("UTF-8"), '', 1, 'L', 1)
    elif tipo == '3':
        sql_circuito = "SELECT c.n_circuito, e.estado FROM circuitos_circuito AS c "
        sql_circuito += " INNER JOIN estados_estado AS e ON e.id = c.estado_id "
        sql_circuito += " WHERE c.id=%s"
        cursor.execute(sql_circuito, [circuito])
        cir = dictfetchall(cursor)
        n_circuito = cir[0]['n_circuito']
        nom_estado = cir[0]['estado']
        pdf.cell(15, 5, "Estado:".decode("UTF-8"), '', 0, 'L', 1)
        pdf.set_font('Arial', '', 10)
        pdf.cell(90, 5, str(nom_estado).decode("UTF-8"), '', 1, 'L', 1)
        pdf.set_font('Arial', 'B', 10)
        pdf.cell(15, 5, "Circuito:".decode("UTF-8"), '', 0, 'L', 1)
        pdf.set_font('Arial', '', 10)
        pdf.cell(90, 5, str(n_circuito).decode("UTF-8"), '', 1, 'L', 1)
    elif tipo == '4':
        sql_municipio = "SELECT c.n_circuito, e.estado, m.municipio FROM municipios_municipio AS m "
        sql_municipio += " INNER JOIN circuitos_circuito AS c ON c.id = m.circuito"
        sql_municipio += " INNER JOIN estados_estado AS e ON e.id = c.estado_id"
        sql_municipio += " WHERE m.id=%s"
        cursor.execute(sql_municipio, [municipio])
        mun = dictfetchall(cursor)
        n_circuito = mun[0]['n_circuito']
        nom_estado = mun[0]['estado']
        nom_municipio = mun[0]['municipio']
        pdf.cell(14, 5, "Estado:".decode("UTF-8"), '', 0, 'L', 1)
        pdf.set_font('Arial', '', 10)
        pdf.cell(90, 5, str(nom_estado).decode("UTF-8"), '', 1, 'L', 1)
        pdf.set_font('Arial', 'B', 10)
        pdf.cell(16, 5, "Circuito:".decode("UTF-8"), '', 0, 'L', 1)
        pdf.set_font('Arial', '', 10)
        pdf.cell(90, 5, str(n_circuito).decode("UTF-8"), '', 1, 'L', 1)
        pdf.set_font('Arial', 'B', 10)
        pdf.cell(18, 5, "Municipio:".decode("UTF-8"), '', 0, 'L', 1)
        pdf.set_font('Arial', '', 10)
        pdf.cell(90, 5, str(nom_municipio).decode("UTF-8"), '', 1, 'L', 1)

    pdf.ln(5)

    # Fila de la cabezara de la tabla
    pdf.set_fill_color(0, 121, 194)
    pdf.set_text_color(255, 255, 255)
    pdf.set_font('Arial', 'B', 11)
    pdf.cell(15, 8, "N°".decode("UTF-8"), 'LTBR', 0, 'C', 1)
    pdf.cell(95, 8, "Género".decode("UTF-8"), 'LTBR', 0, 'C', 1)
    pdf.cell(50, 8, "N° de Votos".decode("UTF-8"), 'LTBR', 0, 'C', 1)
    pdf.cell(20, 8, "%".decode("UTF-8"), 'LTBR', 1, 'C', 1)
    pdf.set_fill_color(255, 255, 255)

    #Fin Cabezera
    pdf.set_text_color(24, 29, 31)
    pdf.set_font('Arial', '', 10)  # TAMANO DE LA FUENTE
    sql_sexo = "SELECT COUNT(v.sexo) total, v.sexo sexo, "
    if tipo == '1':
        sql_sexo += " ROUND (COUNT (v.sexo) * 100.0 / (SELECT COUNT (v.candidatos_id) FROM votacion_votacion v WHERE v.eleccion_id=%s), 2) porcentaje"
    elif tipo == '2':
        sql_sexo += " ROUND (COUNT (v.sexo) * 100.0 / (SELECT COUNT (v.candidatos_id) FROM votacion_votacion v WHERE v.eleccion_id=%s  AND v.estado=" + str(
            estado) + "), 2) porcentaje"
    elif tipo == '3':
        sql_sexo += " ROUND (COUNT (v.sexo) * 100.0 / (SELECT COUNT (v.candidatos_id) FROM votacion_votacion v WHERE v.eleccion_id=%s  AND v.circuito=" + str(
            circuito) + "), 2) porcentaje"
    else:
        sql_sexo += " ROUND (COUNT (v.sexo) * 100.0 / (SELECT COUNT (v.candidatos_id) FROM votacion_votacion v WHERE v.eleccion_id=%s  AND v.municipio=" + str(
            municipio) + "), 2) porcentaje"
    sql_sexo += " FROM votacion_votacion v"
    sql_sexo += " INNER JOIN elecciones_eleccion e ON v.eleccion_id = e.id"
    sql_sexo += " INNER JOIN candidatos_candidatos c ON v.candidatos_id = c.id"
    if tipo == '1':
        sql_sexo += " WHERE v.eleccion_id=%s "
    elif tipo == '2':
        sql_sexo += " WHERE v.eleccion_id=%s AND v.estado=" + str(estado) + ""
    elif tipo == '3':
        sql_sexo += " WHERE v.eleccion_id=%s AND v.circuito=" + str(
            circuito) + ""
    else:
        sql_sexo += " WHERE v.eleccion_id=%s AND v.municipio=" + str(
            municipio) + ""
    sql_sexo += " GROUP BY v.sexo ORDER BY total DESC "

    cursor.execute(sql_sexo, [sexo, sexo])
    row = dictfetchall(cursor)
    total = row[0]['total']
    porcentaje = row[0]['porcentaje']

    item3 = 0
    for q in row:
        pdf.set_fill_color(255, 255, 255)  # COLOR DE BOLDE DE LA CELDA
        sexos = q['sexo']
        if sexos == '2':
            genero = 'Masculino'
        else:
            genero = 'Femenino'

        item3 = int(item3) + 1
        pdf.set_text_color(24, 29, 31)
        pdf.cell(15, 6, str(item3).decode("UTF-8"), 'LTBR', 0, 'C', 1)
        pdf.cell(95, 6, str(genero).decode("UTF-8"), 'LTBR', 0, 'L', 1)
        pdf.cell(50, 6, str(q['total']), 'LTBR', 0, 'R', 1)
        pdf.cell(20, 6, str(q['porcentaje']) + ' %', 'LTBR', 1, 'R', 1)
        pdf.set_fill_color(255, 255, 255)

    pdf.set_font('Arial', 'B', 12)
    pdf.set_fill_color(97, 97, 97)
    pdf.set_text_color(255, 255, 255)
    if tipo == '1':
        sql_tot = 'SELECT COUNT(id) total FROM votacion_votacion WHERE eleccion_id=%s'
    elif tipo == '2':
        sql_tot = "SELECT COUNT(id) total FROM votacion_votacion WHERE eleccion_id=%s AND estado=" + str(
            estado) + ""
    elif tipo == '3':
        sql_tot = "SELECT COUNT(id) total FROM votacion_votacion WHERE eleccion_id=%s AND circuito=" + str(
            circuito) + ""
    else:
        sql_tot = "SELECT COUNT(id) total FROM votacion_votacion WHERE eleccion_id=%s AND municipio=" + str(
            municipio) + ""
    cursor.execute(sql_tot, [sexo])
    row = dictfetchall(cursor)

    total = row[0]['total']

    pdf.cell(110, 6, "TOTAL".decode("UTF-8"), 'LTBR', 0, 'C', 1)
    pdf.cell(50, 6, str(total), 'LTBR', 0, 'R', 1)
    pdf.cell(20, 6, "100 %", 'LTBR', 1, 'R', 1)

    ruta_reporte = settings.MEDIA_PDF
    nombre = str(eleccion) + ' Genero.pdf'
    pdf.output(ruta_reporte + '/' + nombre, 'F')
    archivo = open(ruta_reporte + '/' + nombre, "r")

    ruta = ruta_reporte + '/'
    delete_Files(ruta)
    return nombre, archivo
def detallado_candidato_completo(candidato):

    reload(sys)
    sys.setdefaultencoding("utf-8")
    #candidato = kwargs.get('candidato', None)

    pdf = class_pdf.ReporteCandidato(
        orientation='P', unit='mm',
        format='letter')  # HORIENTACION DE LA PAGINA

    pdf.set_author('Marcel Arcuri')
    pdf.alias_nb_pages()  # LLAMADA DE PAGINACION
    pdf.add_page()  # ANADE UNA NUEVA PAGINACION
    pdf.set_margins(19, 10, 10)  # MARGENE DEL DOCUMENTO

    pdf.set_fill_color(255, 255, 255)
    pdf.set_font('Arial', 'B', 12)

    cursor = connection.cursor()
    sql_cand = "SELECT CONCAT(nombre,' ',apellido) nom_ape FROM candidatos_candidatos "
    sql_cand += " WHERE id=%s "
    cursor.execute(sql_cand, [candidato])
    row = dictfetchall(cursor)
    nom_ape = row[0]['nom_ape']

    pdf.cell(
        180, 5,
        "Listado de Exit Polls que participo: " + str(nom_ape).decode("UTF-8"),
        '', 1, 'C', 1)
    pdf.ln(5)

    cursor = connection.cursor()
    sql_id = "SELECT CONCAT(c.nombre,' ',c.apellido) nom_ape, v.eleccion_id id_e"
    sql_id += " FROM candidatos_candidatos c"
    sql_id += " INNER JOIN votacion_votacion v ON v.candidatos_id = c.id "
    sql_id += " WHERE c.id=%s GROUP BY c.nombre, c.apellido, v.eleccion_id "
    cursor.execute(sql_id, [candidato])
    row = dictfetchall(cursor)

    # Fila de la cabezara de la tabla
    pdf.set_fill_color(0, 121, 194)
    pdf.set_text_color(255, 255, 255)
    pdf.set_font('Arial', 'B', 11)
    pdf.cell(10, 8, "N°".decode("UTF-8"), 'LTBR', 0, 'C', 1)
    pdf.cell(100, 8, "Exit Poll".decode("UTF-8"), 'LTBR', 0, 'C', 1)
    pdf.cell(25, 8, "Obtenidos".decode("UTF-8"), 'LTBR', 0, 'C', 1)
    pdf.cell(25, 8, "Totales".decode("UTF-8"), 'LTBR', 0, 'C', 1)
    pdf.cell(20, 8, "%".decode("UTF-8"), 'LTBR', 1, 'C', 1)
    pdf.set_fill_color(255, 255, 255)
    item = 0
    for m in row:
        id_e = m['id_e']

        cursor = connection.cursor()
        sql_res = "SELECT e.nombre eleccion,"
        sql_res += " COUNT(v.candidatos_id) total_c,(SELECT COUNT (v.candidatos_id) FROM votacion_votacion v WHERE v.eleccion_id=%s) total_v,"
        sql_res += " ROUND (COUNT (v.candidatos_id) * 100.0 / (SELECT COUNT (v.candidatos_id) FROM votacion_votacion v WHERE v.eleccion_id=%s), 2) porcentaje"
        sql_res += " FROM votacion_votacion v"
        sql_res += " INNER JOIN elecciones_eleccion e ON v.eleccion_id = e.id"
        sql_res += " INNER JOIN candidatos_candidatos c ON v.candidatos_id = c.id"
        sql_res += " WHERE v.candidatos_id=%s and v.eleccion_id=%s GROUP BY e.nombre "
        cursor.execute(sql_res, [id_e, id_e, candidato, id_e])
        row = dictfetchall(cursor)
        eleccion = row[0]['eleccion']
        total_c = row[0]['total_c']
        total_v = row[0]['total_v']
        porcentaje = row[0]['porcentaje']

        for p in row:

            item = int(item) + 1
            pdf.set_fill_color(0, 0, 0)
            pdf.cell(180, 1, "", 'LTBR', 1, 'C', 1)
            pdf.set_fill_color(217, 236, 247)
            pdf.set_text_color(24, 29, 31)
            pdf.set_font('Arial', 'B', 10)
            pdf.cell(10, 6, str(item).decode("UTF-8"), 'LTBR', 0, 'C', 1)
            pdf.cell(100, 6,
                     str(p['eleccion']).decode("UTF-8"), 'LTBR', 0, 'L', 1)
            pdf.cell(25, 6,
                     str(p['total_c']).decode("UTF-8"), 'LTBR', 0, 'R', 1)
            pdf.cell(25, 6,
                     str(p['total_v']).decode("UTF-8"), 'LTBR', 0, 'R', 1)
            pdf.cell(20, 6,
                     str(p['porcentaje']) + ' %'.decode("UTF-8"), 'LTBR', 1,
                     'R', 1)
            pdf.set_fill_color(255, 255, 255)
            pdf.set_text_color(24, 29, 31)
            pdf.set_font('Arial', 'B', 10)
            pdf.cell(10, 6, "".decode("UTF-8"), 'LTR', 0, 'C', 1)
            pdf.set_fill_color(191, 191, 191)
            pdf.cell(15, 6, "N°".decode("UTF-8"), 'LTBR', 0, 'C', 1)
            pdf.cell(85, 6, "Grupo Etáreo".decode("UTF-8"), 'LTBR', 0, 'C', 1)
            pdf.cell(50, 6, "N° de Votos".decode("UTF-8"), 'LTBR', 0, 'C', 1)
            pdf.cell(20, 6, "(%)".decode("UTF-8"), 'LTBR', 1, 'C', 1)

            pdf.set_font('Arial', '', 9)  # TAMANO DE LA FUENTE
            sql_gru = " SELECT CONCAT (g.descripcion,' (',g.desde,'-',g.hasta,' Años)') descripcion, "
            sql_gru += " (SELECT COUNT (grupo_etareo) from votacion_votacion WHERE eleccion_id=%s and candidatos_id=%s AND grupo_etareo BETWEEN g.desde::integer AND g.hasta::integer) total, "
            sql_gru += " ROUND ((SELECT COUNT (grupo_etareo) from votacion_votacion WHERE eleccion_id=%s and candidatos_id=%s AND grupo_etareo BETWEEN g.desde::integer AND g.hasta::integer)*100.0 / "
            sql_gru += " (SELECT COUNT (v.grupo_etareo) FROM votacion_votacion v WHERE v.eleccion_id=%s and candidatos_id=%s), 2) porcentaje "
            sql_gru += " FROM grupo_etareo_grupo_etareo AS g ORDER BY porcentaje DESC"
            cursor.execute(sql_gru,
                           [id_e, candidato, id_e, candidato, id_e, candidato])
            # cursor.execute(sql_gru, [candidato,id_e,id_e,candidato])
            row = dictfetchall(cursor)
            descripcion = row[0]['descripcion']
            total = row[0]['total']
            porcentaje = row[0]['porcentaje']

            item2 = 0
            for h in row:
                pdf.set_fill_color(255, 255, 255)  # COLOR DE BOLDE DE LA CELDA

                item2 = int(item2) + 1
                pdf.set_text_color(24, 29, 31)
                pdf.cell(10, 6, "".decode("UTF-8"), 'LR', 0, 'C', 1)
                pdf.cell(15, 6, str(item2).decode("UTF-8"), 'LTBR', 0, 'C', 1)
                pdf.cell(85, 6,
                         str(h['descripcion']).decode("UTF-8"), 'LTBR', 0, 'L',
                         1)
                pdf.cell(50, 6, str(h['total']), 'LTBR', 0, 'R', 1)
                pdf.cell(20, 6, str(h['porcentaje']) + ' %', 'LTBR', 1, 'R', 1)
                pdf.set_fill_color(255, 255, 255)

    pdf.cell(180, 6, "".decode("UTF-8"), 'T', 0, 'C', 1)

    ruta_reporte = settings.MEDIA_PDF
    nombre = str(nom_ape) + ' Detallado.pdf'
    pdf.output(ruta_reporte + '/' + nombre, 'F')
    archivo = open(ruta_reporte + '/' + nombre, "r")

    ruta = ruta_reporte + '/'
    delete_Files(ruta)

    return nombre, archivo
def detallado_candidato_sencillo(candidato):

    reload(sys)
    sys.setdefaultencoding("utf-8")

    pdf = class_pdf.ReporteCandidato(
        orientation='P', unit='mm',
        format='letter')  # HORIENTACION DE LA PAGINA

    pdf.set_author('Marcel Arcuri')
    pdf.alias_nb_pages()  # LLAMADA DE PAGINACION
    pdf.add_page()  # ANADE UNA NUEVA PAGINACION
    pdf.set_margins(19, 10, 10)  # MARGENE DEL DOCUMENTO

    pdf.set_fill_color(255, 255, 255)
    pdf.set_font('Arial', 'B', 12)

    cursor = connection.cursor()
    sql_cand = "SELECT CONCAT(nombre,' ',apellido) nom_ape FROM candidatos_candidatos "
    sql_cand += " WHERE id=%s "
    cursor.execute(sql_cand, [candidato])
    row = dictfetchall(cursor)
    nom_ape = row[0]['nom_ape']

    pdf.cell(
        180, 5,
        "Listado de Exit Polls que participo: " + str(nom_ape).decode("UTF-8"),
        '', 1, 'C', 1)
    pdf.ln(5)

    cursor = connection.cursor()
    sql_id = "SELECT CONCAT(c.nombre,' ',c.apellido) nom_ape, v.eleccion_id id_e"
    sql_id += " FROM candidatos_candidatos c"
    sql_id += " INNER JOIN votacion_votacion v ON v.candidatos_id = c.id "
    sql_id += " WHERE c.id=%s GROUP BY c.nombre, c.apellido, v.eleccion_id "
    cursor.execute(sql_id, [candidato])
    row = dictfetchall(cursor)

    # Fila de la cabezara de la tabla
    pdf.set_fill_color(0, 121, 194)
    pdf.set_text_color(255, 255, 255)
    pdf.set_font('Arial', 'B', 11)
    pdf.cell(10, 8, "N°".decode("UTF-8"), 'LTBR', 0, 'C', 1)
    pdf.cell(100, 8, "Exit Poll".decode("UTF-8"), 'LTBR', 0, 'C', 1)
    pdf.cell(25, 8, "Obtenidos".decode("UTF-8"), 'LTBR', 0, 'C', 1)
    pdf.cell(25, 8, "Totales".decode("UTF-8"), 'LTBR', 0, 'C', 1)
    pdf.cell(20, 8, "%".decode("UTF-8"), 'LTBR', 1, 'C', 1)
    pdf.set_fill_color(255, 255, 255)

    for m in row:
        id_e = m['id_e']

        cursor = connection.cursor()
        sql_res = "SELECT e.nombre eleccion,"
        sql_res += " COUNT(v.candidatos_id) total_c,(SELECT COUNT (v.candidatos_id) FROM votacion_votacion v WHERE v.eleccion_id=%s) total_v,"
        sql_res += " ROUND (COUNT (v.candidatos_id) * 100.0 / (SELECT COUNT (v.candidatos_id) FROM votacion_votacion v WHERE v.eleccion_id=%s), 2) porcentaje"
        sql_res += " FROM votacion_votacion v"
        sql_res += " INNER JOIN elecciones_eleccion e ON v.eleccion_id = e.id"
        sql_res += " INNER JOIN candidatos_candidatos c ON v.candidatos_id = c.id"
        sql_res += " WHERE v.candidatos_id=%s and v.eleccion_id=%s GROUP BY e.nombre "
        cursor.execute(sql_res, [id_e, id_e, candidato, id_e])
        row = dictfetchall(cursor)
        eleccion = row[0]['eleccion']
        total_c = row[0]['total_c']
        total_v = row[0]['total_v']
        porcentaje = row[0]['porcentaje']

        item = 0
        for p in row:
            item = int(item) + 1
            pdf.set_text_color(24, 29, 31)
            pdf.set_font('Arial', '', 9)
            pdf.cell(10, 6, str(item).decode("UTF-8"), 'LTBR', 0, 'C', 1)
            pdf.cell(100, 6,
                     str(p['eleccion']).decode("UTF-8"), 'LTBR', 0, 'L', 1)
            pdf.cell(25, 6,
                     str(p['total_c']).decode("UTF-8"), 'LTBR', 0, 'R', 1)
            pdf.cell(25, 6,
                     str(p['total_v']).decode("UTF-8"), 'LTBR', 0, 'R', 1)
            pdf.cell(20, 6,
                     str(p['porcentaje']) + ' %'.decode("UTF-8"), 'LTBR', 1,
                     'R', 1)

    ruta_reporte = settings.MEDIA_PDF
    nombre = str(nom_ape) + ' Sencillo.pdf'
    pdf.output(ruta_reporte + '/' + nombre, 'F')
    archivo = open(ruta_reporte + '/' + nombre, "r")

    ruta = ruta_reporte + '/'
    delete_Files(ruta)

    return nombre, archivo
def reporte_ficha_personal(cedula, desde, hasta, id_inst, tipo):

        reload(sys)
        sys.setdefaultencoding("utf-8")

        pdf = class_pdf.ReporteFicha(orientation='P', unit='mm', format='letter')  # HORIENTACION DE LA PAGINA

        pdf.set_author('Marcel Arcuri')
        pdf.alias_nb_pages()  # LLAMADA DE PAGINACION
        pdf.add_page()  # ANADE UNA NUEVA PAGINACION
        pdf.set_margins(15,10,10)  # MARGENE DEL DOCUMENTO
        pdf.set_line_width(0.5)
        pdf.ln(5)
        cedula = cedula
        # print cedula
        cursor = connection.cursor()
        sql_usuario = "SELECT s.nombre, s.apellido, s.sexo, s.edad, s.telefono, s.twitter, s.codigo_centro, s.nombre_centro, "
        sql_usuario += "(SELECT estado FROM estados_estado WHERE id=s.estado_id) estado, "
        sql_usuario += "(SELECT municipio FROM municipios_municipio WHERE id=s.municipio) municipio, "
        sql_usuario += "(SELECT parroquia FROM parroquias_parroquia WHERE id=s.parroquia) parroquia, "
        sql_usuario += "(SELECT tipo FROM tipo_educacion_tipoeducacion WHERE id=s.tipo_edu_id) tipo_edu, "
        sql_usuario += "(SELECT ocupacion FROM ocupacion_ocupaciones WHERE id=s.ocupacion_id) ocupacion, "
        sql_usuario += "(SELECT organizacion FROM organizacion_social_organizaciones WHERE id=s.organizacion_id) organizacion, "
        sql_usuario += "(SELECT sector FROM sector_laboral_sectorlaboral WHERE id=s.sector_lab_id) sector "
        sql_usuario += "FROM solicitud_registros AS s "
        sql_usuario += "WHERE cedula = %s  ORDER BY s.id DESC LIMIT 1"
        cursor.execute(sql_usuario,[cedula])
        row = dictfetchall(cursor)

        nombre = row[0]['nombre']
        apellido = row[0]['apellido']
        sexo = row[0]['sexo']
        if sexo == '1':
                sex = 'FEMENINO'
        elif sexo == '2':
                sex = 'MASCULINO'
        edad = row[0]['edad']
        telefono = row[0]['telefono']
        twitter = row[0]['twitter']
        if twitter == '':
                twit = 'NO TIENE'
        else:
                twit = twitter
        codigo_centro = row[0]['codigo_centro']
        if codigo_centro is None:
                centro = 'N/A'
        else:
                centro = str(codigo_centro)+' - '+str(row[0]['nombre_centro'])

        estado = row[0]['estado']
        municipio = row[0]['municipio']
        parroquia = row[0]['parroquia']
        tipo_edu = row[0]['tipo_edu']
        ocupacion = row[0]['ocupacion']
        organizacion = row[0]['organizacion']
        sector = row[0]['sector']

        pdf.set_fill_color(255,255,255)
        pdf.set_font('Arial','B',14)
        pdf.cell(185,7,"FICHA PERSONAL".decode("UTF-8"),'LTBR',1,'C',1)
        pdf.set_fill_color(171,171,171)
        pdf.set_text_color(0,0,0)
        pdf.set_font('Arial','B',10)
        pdf.cell(185,5,"DATOS PERSONALES".decode("UTF-8"),'LTBR',1,'C',1)
        pdf.set_fill_color(255,255,255)
        pdf.set_text_color(24,29,31)
        
        cursor = connection.cursor()
        sql_tot_v = "SELECT count(cedula) total FROM solicitud_registros WHERE cedula = %s "
        cursor.execute(sql_tot_v,[cedula])
        row = dictfetchall(cursor)
        total = row[0]['total']
        if tipo == '1':
                cursor = connection.cursor()
                sql_inst = "SELECT nom_institucion FROM instituciones_institucion WHERE id=%s "
                cursor.execute(sql_inst,[id_inst])
                row3 = dictfetchall(cursor)
                nom_institucion = row3[0]['nom_institucion']
        
        
        
        pdf.set_font('Arial','B',9)
        pdf.cell(16,5,"CÉDULA:".decode("UTF-8"),'LT',0,'L',1)
        pdf.set_font('Arial','',9)
        pdf.cell(20,5,str(cedula).decode("UTF-8"),'T',0,'L',1)
        pdf.set_font('Arial','B',9)
        pdf.cell(38,5,"/ NOMBRE Y APELLIDO:".decode("UTF-8"),'T',0,'L',1)
        pdf.set_font('Arial','',9)
        pdf.cell(111,5,str(nombre)+' '+str(apellido).decode("UTF-8"),'TR',1,'L',1)
        
        pdf.set_font('Arial','B',9)
        pdf.cell(12,5,"SEXO:".decode("UTF-8"),'L',0,'L',1)
        pdf.set_font('Arial','',9)
        pdf.cell(70,5,str(sex).decode("UTF-8"),'',0,'',1)
        pdf.set_font('Arial','B',9)
        pdf.cell(28,5,"/ TOTAL VISITAS:".decode("UTF-8"),'',0,'L',1)
        pdf.set_font('Arial','',9)
        pdf.cell(75,5,str(total).decode("UTF-8"),'R',1,'L',1)

        
       
        pdf.set_font('Arial','B',9)
        pdf.cell(20,5,"TELÉFONO:".decode("UTF-8"),'L',0,'L',1)
        pdf.set_font('Arial','',9)
        pdf.cell(25,5,str(telefono).decode("UTF-8"),'',0,'L',1)
        pdf.set_font('Arial','B',9)
        pdf.cell(14,5,"/ EDAD:".decode("UTF-8"),'',0,'L',1)
        pdf.set_font('Arial','',9)
        pdf.cell(23,5,str(edad)+' AÑOS'.decode("UTF-8"),'',0,'L',1)
        pdf.set_font('Arial','B',9)
        pdf.cell(18,5,"/ TWITTER:".decode("UTF-8"),'',0,'L',1)
        pdf.set_font('Arial','',9)
        pdf.cell(85,5,str(twit).decode("UTF-8"),'R',1,'',1)
        
        if tipo == '1':
                pdf.set_font('Arial','B',9)
                pdf.cell(39,5,"CENTRO DE VOTACIÓN:".decode("UTF-8"),'L',0,'L',1)
                pdf.set_font('Arial','',9)
                pdf.cell(146,5,str(centro).decode("UTF-8"),'R',1,'L',1)
                pdf.set_font('Arial','B',9)
                pdf.cell(25,5,"INSTITUCIÓN:".decode("UTF-8"),'L',0,'L',1)
                pdf.set_font('Arial','',9)
                pdf.cell(160,5,str(nom_institucion).decode("UTF-8"),'R',1,'L',1)
                pdf.set_font('Arial','B',9)
                pdf.cell(18,5,"PERÍODO:".decode("UTF-8"),'LB',0,'L',1)
                pdf.set_font('Arial','',9)
                pdf.cell(167,5,"DESDE EL "+str(desde)+" HASTA EL "+str(hasta).decode("UTF-8"),'BR',1,'L',1)
        else:
                pdf.set_font('Arial','B',9)
                pdf.cell(39,5,"CENTRO DE VOTACIÓN:".decode("UTF-8"),'LB',0,'L',1)
                pdf.set_font('Arial','',9)
                pdf.cell(146,5,str(centro).decode("UTF-8"),'BR',1,'L',1)
        
        pdf.set_fill_color(236,236,236)
        pdf.set_text_color(24,29,31)
        pdf.set_font('Arial','B',10)
        pdf.cell(185,5,"DIRECCIÓN".decode("UTF-8"),'LTBR',1,'C',1)
        pdf.set_fill_color(255,255,255)
        pdf.set_text_color(24,29,31)
        pdf.set_font('Arial','B',9)
        pdf.set_font('Arial','B',9)
        pdf.cell(16,5,"ESTADO:".decode("UTF-8"),'LT',0,'L',1)
        pdf.set_font('Arial','',9)
        pdf.cell(169,5,str(estado).decode("UTF-8"),'TR',1,'L',1)
        pdf.set_font('Arial','B',9)
        pdf.cell(19,5,"MUNICIPIO:".decode("UTF-8"),'L',0,'L',1)
        pdf.set_font('Arial','',9)
        pdf.cell(166,5,str(municipio).decode("UTF-8"),'R',1,'L',1)
        pdf.set_font('Arial','B',9)
        pdf.cell(22,5,"PARROQUIA:".decode("UTF-8"),'LB',0,'L',1)
        pdf.set_font('Arial','',9)
        pdf.cell(163,5,str(parroquia).decode("UTF-8"),'RB',1,'L',1)
        
        
        pdf.set_fill_color(171,171,171)
        pdf.set_text_color(0,0,0)
        pdf.set_font('Arial','B',10)
        pdf.cell(185,5,"INFORMACIÓN PROFESIONAL".decode("UTF-8"),'LTBR',1,'C',1)
        pdf.set_fill_color(255,255,255)
        pdf.set_text_color(24,29,31)
        
        pdf.set_font('Arial','B',9)
        pdf.set_font('Arial','B',9)
        pdf.cell(22,5,"EDUCACIÓN:".decode("UTF-8"),'LT',0,'L',1)
        pdf.set_font('Arial','',9)
        pdf.cell(163,5,str(tipo_edu).decode("UTF-8"),'TR',1,'L',1)

        pdf.set_font('Arial','B',9)
        pdf.cell(22,5,"OCUPACIÓN:".decode("UTF-8"),'L',0,'L',1)
        pdf.set_font('Arial','',9)
        pdf.cell(163,5,str(ocupacion).decode("UTF-8"),'R',1,'L',1)
       
        pdf.set_font('Arial','B',9)
        pdf.cell(28,5,"ORGANIZACIÓN:".decode("UTF-8"),'L',0,'L',1)
        pdf.set_font('Arial','',9)
        pdf.cell(157,5,str(organizacion).decode("UTF-8"),'R',1,'L',1)
        pdf.set_font('Arial','B',9)
        pdf.cell(16,5,"SECTOR:".decode("UTF-8"),'LB',0,'L',1)
        pdf.set_font('Arial','',9)
        pdf.cell(169,5,str(sector).decode("UTF-8"),'BR',1,'L',1)
        
        pdf.ln(5)
        pdf.set_fill_color(171,171,171)
        pdf.set_text_color(0,0,0)
        pdf.set_font('Arial','B',9)
        pdf.cell(185,5,"VISITAS REALIZADAS".decode("UTF-8"),'LTBR',1,'C',1)
        pdf.set_fill_color(236,236,236)
        pdf.set_text_color(24,29,31)
        pdf.cell(5,5,"V".decode("UTF-8"),'LTBR',0,'C',1)
        pdf.cell(15,5,"FECHA".decode("UTF-8"),'LTBR',0,'C',1)
        pdf.cell(95,5,"DEPARTAMENTO".decode("UTF-8"),'LTBR',0,'C',1)
        pdf.cell(70,5,"MOTIVO".decode("UTF-8"),'LTBR',1,'C',1)
        pdf.set_fill_color(255,255,255)
        pdf.set_text_color(24,29,31)
        
        cursor = connection.cursor()
        sql_visitas = "SELECT date(s.fecha_ent) fecha, "
        sql_visitas += "(SELECT nom_dependencia FROM dependencias_dependencia WHERE id=s.departamento_id) departamento, "
        sql_visitas += "(SELECT motivo FROM motivo_motivos WHERE id=s.motivo_id) motivo "
        sql_visitas += "FROM solicitud_registros AS s "
        sql_visitas += "WHERE cedula = %s  ORDER BY s.id ASC "
        cursor.execute(sql_visitas,[cedula])
        row2 = dictfetchall(cursor)
        item = 1
        j = 1
        for i in row2:
                
                if j >= 17:
                        pdf.add_page()
                        pdf.ln(5)
                        pdf.set_fill_color(171,171,171)
                        pdf.set_text_color(0,0,0)
                        pdf.set_font('Arial','B',9)
                        pdf.cell(185,5,"VISITAS REALIZADAS".decode("UTF-8"),'LTBR',1,'C',1)
                        pdf.set_fill_color(236,236,236)
                        pdf.set_text_color(24,29,31)
                        pdf.cell(5,5,"V".decode("UTF-8"),'LTBR',0,'C',1)
                        pdf.cell(15,5,"FECHA".decode("UTF-8"),'LTBR',0,'C',1)
                        pdf.cell(95,5,"DEPARTAMENTO".decode("UTF-8"),'LTBR',0,'C',1)
                        pdf.cell(70,5,"MOTIVO".decode("UTF-8"),'LTBR',1,'C',1)
                        pdf.set_fill_color(255,255,255)
                        pdf.set_text_color(24,29,31)
                        j = 0
                        
                fec = str(i['fecha']).split('-')
                fecha = fec[2]+'-'+fec[1]+'-'+fec[0]
                departamento = i['departamento']
                motivo = i['motivo']
                # print fecha
                pdf.set_font('Arial','',7)
                pdf.cell(5,5,str(item).decode("UTF-8"),'LTBR',0,'C',1)
                pdf.cell(15,5,str(fecha).decode("UTF-8"),'LTBR',0,'C',1)
                pdf.cell(95,5,str(departamento).decode("UTF-8"),'LTBR',0,'C',1)
                pdf.cell(70,5,str(motivo).decode("UTF-8"),'LTBR',1,'C',1) 
                # 
                item = item + 1
                j = j + 1

        ruta_reporte = settings.MEDIA_PDF
        nombre = ' Ficha Personal.pdf'
        pdf.output(ruta_reporte+'/'+nombre, 'F')
        archivo = open(ruta_reporte+'/'+nombre, "r")

        ruta = ruta_reporte+'/'
        delete_Files(ruta)
        
        return nombre, archivo
Ejemplo n.º 9
0
def reporte_encuesta(id_enc):

        reload(sys)
        sys.setdefaultencoding("utf-8")

        pdf = class_pdf.ReporteCandidato(orientation='P', unit='mm', format='letter')  # HORIENTACION DE LA PAGINA

        pdf.set_author('Marcel Arcuri')
        pdf.alias_nb_pages()  # LLAMADA DE PAGINACION
        pdf.add_page()  # ANADE UNA NUEVA PAGINACION
        pdf.set_margins(12,10,10)  # MARGENE DEL DOCUMENTO
        
        pdf.set_font('Arial','B',12)
        pdf.ln(5)
        pdf.set_fill_color(0,0,0)
        pdf.set_text_color(255,255,255)
        pdf.cell(190,8,"SISTEMA DINÁMICO DE ENCUESTAS".decode("UTF-8"),'LTBR',1,'C',1)
        
        cursor = connection.cursor()
        sql_det = "SELECT en.nombre, TO_CHAR(en.fecha_update,'dd-mm-yyyy')fecha_update, en.user_create_id, en.estatus estatus, "
        sql_det += " (SELECT username FROM auth_user WHERE id = en.user_create_id) n_user, "
        sql_det += " (SELECT num_encuestado FROM aplicada_encuestaresultado WHERE cod_encuesta = en.id ORDER BY id DESC LIMIT 1) num_enc"
        sql_det += " FROM encuestas_encuesta as en "
        sql_det += " WHERE en.id = %s "

        cursor.execute(sql_det, [id_enc])
        row = dictfetchall(cursor)
        encuesta = row[0]['nombre']
        fecha = row[0]['fecha_update']
        usuario = row[0]['n_user']
        estatus = row[0]['estatus']
        num_encuestados = row[0]['num_enc']
        
        if estatus == '1':
            est = 'Borrador'
        elif estatus == '2':
            est = 'Activa'
        else:
            est = 'Cerrada'

        pdf.ln(5)
        pdf.set_font('Arial','',10)
        pdf.set_fill_color(255,255,255)
        pdf.set_text_color(0,0,0)
        pdf.set_font('Arial','B',10)
        pdf.cell(42,6,"Nombre de la Encuesta: ".decode("UTF-8"),'',0,'L',1)
        pdf.set_font('Arial','',10)
        pdf.cell(148,6,str(encuesta).decode("UTF-8"),'',1,'L',1)
        pdf.set_font('Arial','B',10)
        pdf.cell(26,6,"Elaborada por: ".decode("UTF-8"),'',0,'L',1)
        pdf.set_font('Arial','',10)
        pdf.cell(135,6,str(usuario).decode("UTF-8"),'',0,'L',1)
        pdf.set_font('Arial','B',10)
        pdf.cell(15,6,"Estatus: ".decode("UTF-8"),'',0,'R',1)
        pdf.set_font('Arial','',10)
        pdf.cell(14,6,str(est).decode("UTF-8"),'',1,'R',1)
        pdf.set_font('Arial','B',10)
        pdf.cell(13,6,"Fecha: ".decode("UTF-8"),'',0,'L',1)
        pdf.set_font('Arial','',10)
        pdf.cell(123,6,str(fecha).decode("UTF-8"),'',0,'L',1)
        pdf.set_font('Arial','B',10)
        pdf.cell(40,6,"N° de participantes: ".decode("UTF-8"),'',0,'R',1)
        pdf.set_font('Arial','',10)
        pdf.cell(14,6,str(num_encuestados).decode("UTF-8"),'',1,'R',1)
        
        pdf.ln(5)
        pdf.set_font('Arial','B',8)
        pdf.set_fill_color(0,0,0)
        pdf.set_text_color(255,255,255)
        pdf.cell(190,6,"Resultados".decode("UTF-8"),'LTBR',1,'C',1)
        pdf.set_fill_color(255,255,255)
        pdf.set_text_color(24,29,31)

        cursor = connection.cursor()
        sql_pre = "SELECT id, cod_encuesta, cod_pregunta, pregunta, tipo"
        sql_pre += " FROM preguntas_preguntas"
        sql_pre += " WHERE cod_encuesta = %s ORDER BY id"
        cursor.execute(sql_pre, [id_enc])
        row = dictfetchall(cursor)
        id_pre = row[0]['id']
        cod_pregunta = row[0]['cod_pregunta']
        pregunta = row[0]['pregunta']
        tipo = row[0]['tipo']
        
        
        k = 0
        for m in row:
            if m['tipo'] == '1':
                tipo_p = "Selección Simple"
            else:
                tipo_p = "Selección Multiple"
                    
            id_pre = m['id']
            pdf.set_font('Arial','B',8)
            pdf.set_fill_color(191,191,191)
            pdf.multi_cell(190,5,str(m['cod_pregunta'])+'. '+str(m['pregunta']).decode("UTF-8"),'LTBR','J',1)
            pdf.set_fill_color(228,228,228)
            pdf.cell(20,5,"Respuesta:".decode("UTF-8"),'LTB',0,'R',1)
            pdf.set_font('Arial','',8)
            pdf.cell(140,5,"("+str(tipo_p)+")".decode("UTF-8"),'TBR',0,'L',1)
            pdf.set_font('Arial','B',8)
            pdf.cell(15,5,"Total".decode("UTF-8"),'LTBR',0,'C',1)
            pdf.cell(15,5,"%".decode("UTF-8"),'LTBR',1,'C',1)
            pdf.set_fill_color(255,255,255)
            pdf.set_font('Arial','',8)

            cursor = connection.cursor()
            sql_res = "SELECT rr.id, rr.respuesta respuesta, "
            sql_res += " COALESCE((SELECT COUNT(ar.cod_respuesta)"
            sql_res += " FROM aplicada_encuestaresultado as ar "
            sql_res += " WHERE ar.cod_respuesta = rr.id GROUP BY ar.cod_respuesta),0) cantidad, "
            sql_res += " COALESCE(ROUND ( (SELECT COUNT(ar.cod_respuesta) "
            sql_res += " FROM aplicada_encuestaresultado as ar "
            sql_res += " WHERE ar.cod_respuesta = rr.id GROUP BY ar.cod_respuesta) * 100.0 /  "
            sql_res += " (SELECT COUNT(rr_s.id) "
            sql_res += " FROM respuestas_respuestas AS rr_s "
            sql_res += " INNER JOIN aplicada_encuestaresultado AS ar_s ON rr_s.id=ar_s.cod_respuesta "
            sql_res += " WHERE rr_s.cod_pregunta_id = rr.cod_pregunta_id), 2),0.00) porcentaje "
            sql_res += " FROM respuestas_respuestas as rr"
            sql_res += " WHERE rr.cod_pregunta_id = %s GROUP BY rr.id, rr.cod_respuesta, rr.respuesta  "
            sql_res += " ORDER BY rr.id, rr.respuesta"
            cursor.execute(sql_res, [id_pre])
            row = dictfetchall(cursor)

            if row == []:
                pdf.cell(190,5,"N/A",'LTBR',1,'L',1)

            else:
                # cod_respuesta = row[0]['cod_respuesta']
                respuesta = row[0]['respuesta']
                cantidad = row[0]['cantidad']
                porcentaje = row[0]['porcentaje']

                item = 0
                for j in row:
                    if k == 25:
                        pdf.add_page()
                        pdf.ln(10)
                        pdf.set_font('Arial','',8)
                        pdf.set_fill_color(255,255,255)
                        pdf.set_text_color(24,29,31)
                    
                    item = int(item) + 1
                    pdf.cell(5,5,"",'LTB',0,'C',1)
                    pdf.cell(5,5,str(item)+".",'TB',0,'C',1)
                    pdf.cell(150,5,j['respuesta'],'TBR',0,'L',1)
                    pdf.cell(15,5,str(j['cantidad']),'TBR',0,'R',1)
                    pdf.cell(15,5,str(j['porcentaje']),'TBR',1,'R',1)
                    k = k +1
        
        ruta_reporte = settings.MEDIA_PDF
        nombre = str(encuesta)+'.pdf'
        pdf.output(ruta_reporte+'/'+nombre, 'F')
        archivo = open(ruta_reporte+'/'+nombre, "r")

        ruta = ruta_reporte+'/'
        delete_Files(ruta)

        return nombre, archivo
def reporte_listado_exit_sencillo(desde, hasta):

    reload(sys)
    fecha_hora = time.strftime("%d/%m/%Y")
    fecha = time.strftime("%d-%m-%Y")
    sys.setdefaultencoding("utf-8")

    pdf = class_pdf.ReporteCandidato(
        orientation='P', unit='mm',
        format='letter')  # HORIENTACION DE LA PAGINA

    pdf.set_author('Marcel Arcuri')
    pdf.alias_nb_pages()  # LLAMADA DE PAGINACION
    pdf.add_page()  # ANADE UNA NUEVA PAGINACION
    pdf.set_margins(19, 10, 10)  # MARGENE DEL DOCUMENTO

    pdf.set_fill_color(255, 255, 255)
    pdf.set_font('Arial', 'B', 12)

    pdf.cell(
        180, 5, " Listado de Exit Polls desde el " + str(desde) + " " +
        "hasta el " + str(hasta), '', 1, 'C', 1)
    pdf.ln(5)

    # Fila de la cabezara de la tabla
    pdf.set_fill_color(0, 121, 194)
    pdf.set_text_color(255, 255, 255)
    pdf.cell(15, 10, "N°".decode("UTF-8"), 'LTBR', 0, 'C', 1)
    pdf.cell(165, 10, "Exit Poll".decode("UTF-8"), 'LTBR', 1, 'C', 1)
    pdf.set_fill_color(255, 255, 255)

    cursor = connection.cursor()
    sql_ele = "SELECT v.eleccion_id, e.nombre, COUNT(v.id) total FROM votacion_votacion v"
    sql_ele += " INNER JOIN elecciones_eleccion e ON v.eleccion_id = e.id"
    sql_ele += " WHERE e.fecha_create  between %s and %s"
    sql_ele += " GROUP BY e.nombre, v.eleccion_id "

    cursor.execute(sql_ele, [desde, hasta])
    row = dictfetchall(cursor)

    nombre = row[0]['nombre']
    total = row[0]['total']
    eleccion_id = row[0]['eleccion_id']
    i = 0
    k = 0
    j = 0
    item = 0

    for t in row:
        eleccion_id = t['eleccion_id']
        pdf.set_fill_color(255, 255, 255)
        if j == 1:
            pdf.add_page()
            pdf.set_fill_color(255, 255, 255)
            pdf.set_font('Arial', 'B', 12)
            pdf.cell(
                180, 5, " Listado de Exit Polls desde el " + str(desde) + " " +
                "hasta el " + str(hasta), '', 1, 'C', 1)
            pdf.ln(5)
            # Fin Cabezera
            j = 0

        item = int(item) + 1
        # Filas que vienen de la BD
        pdf.set_font('Arial', 'B', 11)
        pdf.set_fill_color(217, 236, 247)
        pdf.set_text_color(24, 29, 31)
        pdf.cell(15, 8, str(item).decode("UTF-8"), 'LTBR', 0, 'C', 1)
        pdf.cell(165, 8, str(t['nombre']).decode("UTF-8"), 'LTBR', 1, 'L', 1)

        pdf.set_font('Arial', 'B', 10)

        pdf.set_fill_color(255, 255, 255)
        pdf.cell(15, 6, "".decode("UTF-8"), 'LTR', 0, 'C', 1)
        pdf.set_fill_color(191, 191, 191)
        pdf.cell(10, 6, "N°".decode("UTF-8"), 'LTBR', 0, 'C', 1)
        pdf.cell(30, 6, "Cédula".decode("UTF-8"), 'LTBR', 0, 'C', 1)
        pdf.cell(90, 6, "Candidato".decode("UTF-8"), 'LTBR', 0, 'C', 1)
        pdf.cell(35, 6, "Votos".decode("UTF-8"), 'LTBR', 1, 'C', 1)
        pdf.set_text_color(24, 29, 31)
        item2 = 0

        sql_exit = "SELECT c.cedula, CONCAT(c.nombre,' ',c.apellido) nom_ape, e.nombre, COUNT(v.id) total "
        sql_exit += " FROM votacion_votacion v"
        sql_exit += " INNER JOIN elecciones_eleccion e ON v.eleccion_id = e.id"
        sql_exit += " INNER JOIN candidatos_candidatos c ON v.candidatos_id = c.id"
        sql_exit += " WHERE v.eleccion_id=%s GROUP BY v.eleccion_id, c.nombre, c.apellido, e.nombre, c.cedula ORDER BY total DESC"

        cursor.execute(sql_exit, [eleccion_id])
        row = dictfetchall(cursor)

        cedula = row[0]['cedula']
        nom_ape = row[0]['nom_ape']
        total = row[0]['total']

        item2 = 0
        for h in row:
            pdf.set_fill_color(255, 255, 255)  # COLOR DE BOLDE DE LA CELDA

            item2 = int(item2) + 1
            pdf.set_font('Arial', '', 10)
            pdf.set_text_color(24, 29, 31)
            pdf.cell(15, 6, "".decode("UTF-8"), 'LR', 0, 'C', 1)
            pdf.cell(10, 6, str(item2).decode("UTF-8"), 'LTBR', 0, 'C', 1)
            pdf.cell(30, 6,
                     str(h['cedula']).decode("UTF-8"), 'LTBR', 0, 'C', 1)
            pdf.cell(90, 6,
                     str(h['nom_ape']).decode("UTF-8"), 'LTBR', 0, 'L', 1)
            pdf.cell(35, 6, str(h['total']), 'LTBR', 1, 'R', 1)
            pdf.set_fill_color(255, 255, 255)

        i = i + 1
        j = j + 1

        pdf.set_fill_color(127, 127, 127)
        pdf.set_text_color(255, 255, 255)
        pdf.set_font('Arial', 'B', 10)
        pdf.cell(145, 8, "TOTAL".decode("UTF-8"), 'LTBR', 0, 'C', 1)
        pdf.cell(35, 8, str(t['total']), 'LTBR', 1, 'R', 1)
        pdf.set_fill_color(255, 255, 255)

    ruta_reporte = settings.MEDIA_PDF
    nombre = 'Listado de Exit Polls Detallados al ' + str(
        desde) + " " + "hasta el " + str(hasta) + '.pdf'
    pdf.output(ruta_reporte + '/' + nombre, 'F')
    archivo = open(ruta_reporte + '/' + nombre, "r")

    ruta = ruta_reporte + '/'
    delete_Files(ruta)

    return nombre, archivo
Ejemplo n.º 11
0
def reporte_listado_exit_datallado(desde, hasta):

    reload(sys)
    fecha_hora = time.strftime("%d/%m/%Y")
    fecha = time.strftime("%d-%m-%Y")
    sys.setdefaultencoding("utf-8")
    #todo = kwargs.get('todo', None)
    #desde = kwargs.get('from_date', None)
    #hasta = kwargs.get('to_date', None)

    pdf = class_pdf.ReporteCandidato(
        orientation='P', unit='mm',
        format='letter')  # HORIENTACION DE LA PAGINA

    pdf.set_author('Marcel Arcuri')
    pdf.alias_nb_pages()  # LLAMADA DE PAGINACION
    pdf.add_page()  # ANADE UNA NUEVA PAGINACION
    pdf.set_margins(19, 10, 10)  # MARGENE DEL DOCUMENTO

    pdf.set_fill_color(255, 255, 255)
    pdf.set_font('Arial', 'B', 12)

    pdf.cell(
        180, 5, " Listado Detallado de Exit Polls desde el " + str(desde) +
        " " + "hasta el " + str(hasta), '', 1, 'C', 1)
    pdf.ln(5)

    # Fila de la cabezara de la tabla
    pdf.set_fill_color(0, 121, 194)
    pdf.set_text_color(255, 255, 255)
    pdf.cell(15, 10, "N°".decode("UTF-8"), 'LTBR', 0, 'C', 1)
    pdf.cell(125, 10, "Exit Poll".decode("UTF-8"), 'LTBR', 0, 'C', 1)
    pdf.cell(40, 10, "Votos".decode("UTF-8"), 'LTBR', 1, 'C', 1)
    pdf.set_fill_color(255, 255, 255)

    cursor = connection.cursor()
    sql_ele = "SELECT v.eleccion_id, e.nombre, COUNT(v.id) total FROM votacion_votacion v"
    sql_ele += " INNER JOIN elecciones_eleccion e ON v.eleccion_id = e.id"
    sql_ele += " WHERE e.fecha_create  between %s and %s"
    sql_ele += " GROUP BY e.nombre, v.eleccion_id "

    cursor.execute(sql_ele, [desde, hasta])
    row = dictfetchall(cursor)

    nombre = row[0]['nombre']
    total = row[0]['total']
    eleccion_id = row[0]['eleccion_id']
    i = 0
    k = 0
    j = 0
    item = 0

    for t in row:
        eleccion_id = t['eleccion_id']
        pdf.set_fill_color(255, 255, 255)
        if j == 1:
            pdf.add_page()
            pdf.set_fill_color(255, 255, 255)
            pdf.set_font('Arial', 'B', 12)
            pdf.cell(
                180, 5, " Listado de Exit Polls desde el " + str(desde) + " " +
                "hasta el " + str(hasta), '', 1, 'C', 1)
            pdf.ln(5)

            # Fila de la cabezara de la tabla
            pdf.set_fill_color(0, 121, 194)
            pdf.set_text_color(255, 255, 255)
            pdf.cell(15, 10, "N°".decode("UTF-8"), 'LTBR', 0, 'C', 1)
            pdf.cell(125, 10, "Exit Poll".decode("UTF-8"), 'LTBR', 0, 'C', 1)
            pdf.cell(40, 10, "Votos".decode("UTF-8"), 'LTBR', 1, 'C', 1)
            pdf.set_fill_color(255, 255, 255)

            j = 0

        item = int(item) + 1
        # Filas que vienen de la BD
        pdf.set_font('Arial', 'B', 11)
        pdf.set_fill_color(217, 236, 247)
        pdf.set_text_color(24, 29, 31)
        pdf.cell(15, 8, str(item).decode("UTF-8"), 'LTBR', 0, 'C', 1)
        pdf.cell(125, 8, str(t['nombre']).decode("UTF-8"), 'LTBR', 0, 'L', 1)
        pdf.cell(40, 8, str(t['total']), 'LTBR', 1, 'C', 1)
        pdf.set_fill_color(255, 255, 255)
        pdf.set_font('Arial', 'B', 10)

        #pdf.set_text_color(255,255,255)
        pdf.cell(15, 8, "".decode("UTF-8"), 'LTR', 0, 'C', 1)
        pdf.set_fill_color(191, 191, 191)
        pdf.cell(10, 8, "N°".decode("UTF-8"), 'LTBR', 0, 'C', 1)
        pdf.cell(30, 8, "Cédula".decode("UTF-8"), 'LTBR', 0, 'C', 1)
        pdf.cell(85, 8, "Candidato".decode("UTF-8"), 'LTBR', 0, 'C', 1)
        pdf.cell(40, 8, "Votos".decode("UTF-8"), 'LTBR', 1, 'C', 1)
        pdf.set_fill_color(255, 255, 255)
        pdf.set_text_color(24, 29, 31)

        sql_exit = "SELECT c.cedula, CONCAT(c.nombre,' ',c.apellido) nom_ape, e.nombre, COUNT(v.id) total, v.candidatos_id id_c "
        sql_exit += " FROM votacion_votacion v"
        sql_exit += " INNER JOIN elecciones_eleccion e ON v.eleccion_id = e.id"
        sql_exit += " INNER JOIN candidatos_candidatos c ON v.candidatos_id = c.id"
        sql_exit += " WHERE v.eleccion_id=%s GROUP BY v.eleccion_id, c.nombre, c.apellido, e.nombre, c.cedula, v.candidatos_id ORDER BY total DESC"

        cursor.execute(sql_exit, [eleccion_id])
        row = dictfetchall(cursor)

        cedula = row[0]['cedula']
        nom_ape = row[0]['nom_ape']
        total = row[0]['total']

        item2 = 0
        for h in row:
            id_c = h['id_c']
            pdf.set_text_color(24, 29, 31)  # COLOR DE BOLDE DE LA CELDA

            item2 = int(item2) + 1
            pdf.set_text_color(255, 255, 255)
            pdf.cell(15, 6, "".decode("UTF-8"), 'LR', 0, 'C', 1)
            pdf.set_fill_color(127, 127, 127)
            pdf.cell(10, 6, str(item2).decode("UTF-8"), 'LTBR', 0, 'C', 1)
            pdf.cell(30, 6,
                     str(h['cedula']).decode("UTF-8"), 'LTBR', 0, 'C', 1)
            pdf.cell(85, 6,
                     str(h['nom_ape']).decode("UTF-8"), 'LTBR', 0, 'L', 1)
            pdf.cell(40, 6, str(h['total']), 'LTBR', 1, 'R', 1)
            pdf.set_fill_color(255, 255, 255)

            pdf.set_text_color(24, 29, 31)
            pdf.set_font('Arial', 'B', 10)
            pdf.cell(15, 6, "".decode("UTF-8"), 'LR', 0, 'C', 1)
            pdf.cell(10, 6, "".decode("UTF-8"), 'LTR', 0, 'C', 1)
            pdf.set_fill_color(226, 219, 219)
            pdf.cell(15, 6, "N°".decode("UTF-8"), 'LTBR', 0, 'C', 1)
            pdf.cell(100, 6, "Grupo Etáreo".decode("UTF-8"), 'LTBR', 0, 'C', 1)
            pdf.cell(40, 6, "N° de Votos".decode("UTF-8"), 'LTBR', 1, 'C', 1)

            sql_gru = " SELECT CONCAT (g.descripcion,' (',g.desde,'-',g.hasta,' Años)') descripcion, "
            sql_gru += " (SELECT COUNT (grupo_etareo) from votacion_votacion WHERE eleccion_id=%s and candidatos_id=%s AND grupo_etareo BETWEEN g.desde::integer AND g.hasta::integer) total, "
            sql_gru += " ROUND ((SELECT COUNT (grupo_etareo) from votacion_votacion WHERE eleccion_id=%s and candidatos_id=%s AND grupo_etareo BETWEEN g.desde::integer AND g.hasta::integer)*100.0 / "
            sql_gru += " (SELECT COUNT (v.grupo_etareo) FROM votacion_votacion v WHERE v.eleccion_id=%s and candidatos_id=%s), 2) porcentaje "
            sql_gru += " FROM grupo_etareo_grupo_etareo AS g ORDER BY porcentaje DESC"
            cursor.execute(
                sql_gru,
                [eleccion_id, id_c, eleccion_id, id_c, eleccion_id, id_c])

            row = dictfetchall(cursor)

            descripcion = row[0]['descripcion']
            total = row[0]['total']

            item3 = 0
            for p in row:
                pdf.set_fill_color(255, 255, 255)  # COLOR DE BOLDE DE LA CELDA
                pdf.set_font('Arial', '', 10)
                item3 = int(item3) + 1
                pdf.set_text_color(24, 29, 31)
                pdf.cell(15, 6, "".decode("UTF-8"), 'LR', 0, 'C', 1)
                pdf.cell(10, 6, "".decode("UTF-8"), 'LR', 0, 'C', 1)
                pdf.cell(15, 6, str(item3).decode("UTF-8"), 'LTBR', 0, 'C', 1)
                pdf.cell(100, 6,
                         str(p['descripcion']).decode("UTF-8"), 'LTBR', 0, 'L',
                         1)
                pdf.cell(40, 6, str(p['total']), 'LTBR', 1, 'R', 1)
                pdf.set_font('Arial', 'B', 10)
                pdf.set_fill_color(255, 255, 255)

        i = i + 1
        j = j + 1
        pdf.cell(180, 1, "", 'T', 0, 'L', 1)

    ruta_reporte = settings.MEDIA_PDF
    nombre = 'Listado de Exit Polls Detallados al ' + str(
        desde) + " " + "hasta el " + str(hasta) + '.pdf'
    pdf.output(ruta_reporte + '/' + nombre, 'F')
    archivo = open(ruta_reporte + '/' + nombre, "r")

    ruta = ruta_reporte + '/'
    delete_Files(ruta)

    return nombre, archivo