create or replace PROCEDURE TRABRELCARDMES_PME(
    P_NRORG IN VARCHAR2,
    P_NRSEQTRABCARD IN VARCHAR2,
    P_CDFILCENTPME IN VARCHAR2,
    P_CDFILIAL IN VARCHAR2,
    P_NRCARDPME IN VARCHAR2,
    P_DTINICIAL IN DATE,
    P_DTFINAL IN DATE,
    P_CDSERVFILI IN VARCHAR2,
    P_QTCOLUNA IN NUMBER
) IS
    
    V_CT_COLUNA NUMBER;
    V_NR_PAGINA NUMBER;

    V_CDFILIAL VARCHAR2(4);
    V_CDSERVFILI VARCHAR2(5);
    V_DTINIMES DATE;
    V_DTCARDAPIO DATE;

    V_NMPRATO VARCHAR2(80);
    V_CDPRATO VARCHAR2(10);
    V_CDFILAUXPRAT VARCHAR2(4);

    V_ROW NUMBER;
    V_NOVO VARCHAR2(1);

BEGIN

    V_CT_COLUNA := 0;
    V_NR_PAGINA := 0;
    V_ROW := 1;
    V_NOVO := 'S';  
        
    V_CDFILIAL := NULL;
    V_CDSERVFILI := NULL;
    V_DTINIMES := NULL;
    V_DTCARDAPIO := NULL;

    FOR C_PRATOS IN (
        SELECT CC.NRSEQPRACPME, CC.NRORDPRACPME, CC.CDFILAUXPRAT, CC.CDPRATO, CC.CDTIPOPRAT, CC.VRPECONSCPME, A.CDFILCENTPME,
               A.NMTIPOPRAT, A.NRCARDPME, A.NRCARDIAPME, A.CDFILIAL, A.CDSERVFILI, A.DTCARDIAPME, A.DSCARDPME, A.DTINICARDPME,
               A.DTFINCARDPME, A.CDTIPOCARD, A.NRSEQCNFCPME, A.IDOPCCNFCPME, A.NRORDCNFCPME, A.QTCOMESTCPME, A.QTCOMEFECPME,
               A.NMFILICENTPME, A.NMSERVICO, A.NMTIPOCARD, A.CDFILIESP, CC.NRORG, FIRST_DAY(A.DTINICARDPME) AS PRIMEIRODIA,
               ROUND(CC.VRPECONSCPME / 100 * A.QTCOMESTCPME, 3) AS QTCOMESTPRAT
         FROM (SELECT CD.CDFILCENTPME, TP.NMTIPOPRAT, CD.NRCARDPME, CD.NRCARDIAPME, CD.CDFILIAL AS CDFILIAL, CD.CDSERVFILI,
                      CD.DTCARDIAPME, CP.DSCARDPME, CP.DTINICARDPME, CP.DTFINCARDPME, CP.CDTIPOCARD, CF.NRSEQCNFCPME,
                      CF.IDOPCCNFCPME, CF.NRORDCNFCPME, CD.QTCOMESTCPME, CD.QTCOMEFECPME, FC.NMFILIAL AS NMFILICENTPME,
                      SC.NMSERVICO, TC.NMTIPOCARD, CD.CDFILIAL AS CDFILIESP,
                      CF.CDTIPOPRAT, CD.NRORG
                 FROM TIPOPRAT TP, CARDAPIOPME CP, CONFCARDPME CF, FILIAL FC,
                      SERVICO SC, TIPOCARD TC,
                      (SELECT CDFILCENTPME, NRCARDPME, NRCARDIAPME, CDFILIAL, DTCARDIAPME, CDSERVFILI, QTCOMESTCPME, 
                              QTCOMEFECPME, NRORG
                         FROM CARDIAPME P
                        WHERE (P.NRORG = P_NRORG)
                          AND (P.CDFILCENTPME = P_CDFILCENTPME) 
                          AND (P.NRCARDPME    = P_NRCARDPME)    
                          AND (P.CDFILIAL     = P.CDFILCENTPME)
                          AND (P.DTCARDIAPME BETWEEN P_DTINICIAL AND P_DTFINAL)
                          AND (INSTR(P_CDSERVFILI, P.CDSERVFILI) <> 0 OR P_CDSERVFILI = 'T') 
                          AND NOT EXISTS(SELECT CDFILIAL
                                           FROM  CARDIAPME C
                                          WHERE (C.NRORG = P.NRORG) AND
                                                (C.CDFILCENTPME = P.CDFILCENTPME) AND
                                                (C.NRCARDPME    = P.NRCARDPME)    AND
                                                (C.CDSERVFILI   = P.CDSERVFILI)   AND
                                                (C.CDFILIAL     = P_CDFILIAL)    AND
                                                (C.DTCARDIAPME  = P.DTCARDIAPME)  AND
                                                (C.IDCARDESPEC  = 'S') )
                        UNION ALL
                       SELECT CDFILCENTPME, NRCARDPME, NRCARDIAPME, CDFILIAL, DTCARDIAPME, CDSERVFILI, QTCOMESTCPME, 
                              QTCOMEFECPME, NRORG
                         FROM CARDIAPME
                        WHERE (NRORG = P_NRORG) 
                          AND (CDFILCENTPME = P_CDFILCENTPME) 
                          AND (NRCARDPME    = P_NRCARDPME)
                          AND (CDFILIAL     = P_CDFILIAL)
                          AND (DTCARDIAPME BETWEEN P_DTINICIAL AND P_DTFINAL)
                          AND (INSTR(P_CDSERVFILI, CDSERVFILI) <> 0 OR P_CDSERVFILI = 'T')
                          AND (IDCARDESPEC  = 'S')) CD
                WHERE (CP.NRORG = CD.NRORG) 
                  AND (CP.CDFILCENTPME = CD.CDFILCENTPME)
                  AND (CP.NRCARDPME    = CD.NRCARDPME)
                  AND (CF.NRORG = CP.NRORG)
                  AND (CF.CDFILCENTPME = CP.CDFILCENTPME)
                  AND (CF.NRCARDPME    = CP.NRCARDPME)
                  AND (CF.IDIMPCNFCPME = 'S') 
                  AND (CF.NRORG = CD.NRORG)
                  AND (CF.CDFILCENTPME = CD.CDFILCENTPME)
                  AND (CF.NRCARDPME    = CD.NRCARDPME)
                  AND (CF.CDSERVFILI   = CD.CDSERVFILI)
                  AND (FC.NRORG = CD.NRORG)
                  AND (FC.CDFILIAL     = CD.CDFILCENTPME)
                  AND (SC.NRORG = CD.NRORG)
                  AND (SC.CDFILIAL     = CD.CDFILCENTPME)
                  AND (SC.CDSERVFILI   = CD.CDSERVFILI)
                  AND (TC.NRORG = CP.NRORG)
                  AND (TC.CDFILCENTPME = CP.CDFILCENTPME)
                  AND (TC.CDTIPOCARD   = CP.CDTIPOCARD)
                  AND (TP.NRORG = CF.NRORG)
                  AND (TP.CDTIPOPRAT   = CF.CDTIPOPRAT)) A
         LEFT JOIN COMPCARDPME CC ON (A.NRORG = CC.NRORG) AND (A.CDFILCENTPME  = CC.CDFILCENTPME ) AND
                                     (A.NRCARDPME     = CC.NRCARDPME ) AND (A.NRCARDIAPME   = CC.NRCARDIAPME ) AND
                                     (A.NRSEQCNFCPME  = CC.NRSEQPRACPME) AND (A.CDTIPOPRAT    = CC.CDTIPOPRAT)
        ORDER BY  A.CDFILIAL, A.CDSERVFILI, A.DTCARDIAPME, TO_NUMBER(A.NRORDCNFCPME)
    ) LOOP
    
        IF ( ( V_CDFILIAL IS NULL OR V_CDFILIAL <> C_PRATOS.CDFILIAL ) OR 
             ( V_CDSERVFILI IS NULL OR V_CDSERVFILI <> C_PRATOS.CDSERVFILI ) OR 
             ( V_DTINIMES IS NULL OR V_DTINIMES <> C_PRATOS.PRIMEIRODIA ) ) THEN
                    
            V_CT_COLUNA := 1;
            V_NR_PAGINA := 1;
            V_NOVO := 'S';
            
            V_CDFILIAL := C_PRATOS.CDFILIAL;
            V_CDSERVFILI := C_PRATOS.CDSERVFILI;
            V_DTINIMES := C_PRATOS.PRIMEIRODIA;
        ELSE 
            V_CT_COLUNA := V_CT_COLUNA + 1;
        END IF;

        IF V_DTCARDAPIO IS NULL OR V_DTCARDAPIO <> C_PRATOS.DTCARDIAPME THEN 
            V_CT_COLUNA := 1;
            V_NR_PAGINA := 1;
            V_DTCARDAPIO := C_PRATOS.DTCARDIAPME;
            V_NOVO := 'S';
        END IF;
        
        IF V_CT_COLUNA > P_QTCOLUNA THEN
            V_CT_COLUNA := 1;
            V_NR_PAGINA := V_NR_PAGINA + 1;
            V_NOVO := 'S';
        END IF;

        IF C_PRATOS.CDPRATO IS NOT NULL THEN
            SELECT NVL(FANT.NMFANTASIA, PRATO.NMPRATO) AS NMFANTASIA,
                   PRATO.CDPRATO, PRATO.CDFILAUXPRAT
              INTO V_NMPRATO, V_CDPRATO, V_CDFILAUXPRAT
              FROM PRATO
              LEFT JOIN 
                   (SELECT P.NMFANTASIASE AS NMFANTASIA, P.CDPRATO, P.CDFILAUXPRAT, P.NRORG
                      FROM PRATSERV P
                     WHERE (P.NRORG = C_PRATOS.NRORG)
                       AND (P.CDFILIAL = C_PRATOS.CDFILIAL)
                       AND (P.CDPRATO = C_PRATOS.CDPRATO)
                       AND (P.CDFILAUXPRAT = C_PRATOS.CDFILAUXPRAT)
                       AND (P.CDSERVFILI = C_PRATOS.CDSERVFILI)
                       AND (NOT P.NMFANTASIASE IS NULL)
                       AND EXISTS (SELECT RS.CDPRATO
                                     FROM RECESERV RS
                                    WHERE (RS.NRORG = C_PRATOS.NRORG)
                                      AND (RS.CDFILAUXPRAT = P.CDFILAUXPRAT)
                                      AND (RS.CDPRATO = P.CDPRATO)
                                      AND (RS.CDFILIAL = C_PRATOS.CDFILIAL)
                                      AND (RS.CDSERVFILI = P.CDSERVFILI))
                     UNION
                    SELECT P1.NMFANTASIAPF AS NMFANTASIA, P1.CDPRATO, P1.CDFILAUXPRAT, P1.NRORG
                      FROM PRATFILI P1
                     WHERE (P1.NRORG = C_PRATOS.NRORG)
                       AND (P1.CDFILIAL = C_PRATOS.CDFILIAL)
                       AND (P1.CDPRATO = C_PRATOS.CDPRATO)
                       AND (P1.CDFILAUXPRAT = C_PRATOS.CDFILAUXPRAT)
                       AND (NOT P1.NMFANTASIAPF IS NULL)
                       AND EXISTS (SELECT RF.CDPRATO
                                     FROM RECEFILI RF
                                    WHERE (RF.NRORG = C_PRATOS.NRORG)
                                      AND (RF.CDFILAUXPRAT = P1.CDFILAUXPRAT)
                                      AND (RF.CDPRATO = P1.CDPRATO)
                                      AND (RF.CDFILIAL = C_PRATOS.CDFILIAL))
                       AND NOT EXISTS (SELECT RS1.CDPRATO
                                         FROM RECESERV RS1
                                        WHERE (RS1.NRORG = C_PRATOS.NRORG)
                                          AND (RS1.CDFILAUXPRAT = P1.CDFILAUXPRAT)
                                          AND (RS1.CDPRATO = P1.CDPRATO)
                                          AND (RS1.CDFILIAL = C_PRATOS.CDFILIAL)
                                          AND (RS1.CDSERVFILI = C_PRATOS.CDSERVFILI))
                     UNION
                    SELECT P2.NMFANTASIAPRTP AS NMFANTASIA, P2.CDPRATO, P2.CDFILAUXPRAT, P2.NRORG
                      FROM PRATTPCARD P2
                     WHERE (P2.NRORG = C_PRATOS.NRORG)
                       AND (P2.CDFILCENTPME = C_PRATOS.CDFILCENTPME)
                       AND (P2.CDTIPOCARD = C_PRATOS.CDTIPOCARD)
                       AND (P2.CDFILAUXPRAT = C_PRATOS.CDFILAUXPRAT)
                       AND (P2.CDPRATO = C_PRATOS.CDPRATO)
                       AND (NOT P2.NMFANTASIAPRTP IS NULL)
                       AND EXISTS (SELECT RC.CDPRATO
                                     FROM RECETPCARD RC
                                    WHERE (RC.NRORG = C_PRATOS.NRORG)
                                      AND (RC.CDFILCENTPME = C_PRATOS.CDFILCENTPME)
                                      AND (RC.CDTIPOCARD = C_PRATOS.CDTIPOCARD)
                                      AND (RC.CDFILAUXPRAT = C_PRATOS.CDFILAUXPRAT)
                                      AND (RC.CDPRATO = C_PRATOS.CDPRATO))
                       AND NOT EXISTS (SELECT RF1.CDPRATO
                                         FROM RECEFILI RF1
                                        WHERE (RF1.NRORG = C_PRATOS.NRORG)
                                          AND (RF1.CDFILAUXPRAT = C_PRATOS.CDFILAUXPRAT)
                                          AND (RF1.CDPRATO = C_PRATOS.CDPRATO)
                                          AND (RF1.CDFILIAL = C_PRATOS.CDFILIAL))
                       AND NOT EXISTS (SELECT RS2.CDPRATO
                                         FROM RECESERV RS2
                                        WHERE (RS2.NRORG = C_PRATOS.NRORG)
                                          AND (RS2.CDFILAUXPRAT = C_PRATOS.CDFILAUXPRAT)
                                          AND (RS2.CDPRATO = C_PRATOS.CDPRATO)
                                          AND (RS2.CDFILIAL = C_PRATOS.CDFILIAL)
                                          AND (RS2.CDSERVFILI = C_PRATOS.CDSERVFILI))
                     UNION
                    SELECT P3.NMFANTASIAPR AS NMFANTASIA, P3.CDPRATO, P3.CDFILAUXPRAT, P3.NRORG
                      FROM PRATPADR P3
                     WHERE (P3.NRORG = C_PRATOS.NRORG)
                       AND (P3.CDFILAUXPRAT = C_PRATOS.CDFILAUXPRAT)
                       AND (P3.CDPRATO = C_PRATOS.CDPRATO)
                       AND (NOT P3.NMFANTASIAPR IS NULL)
                       AND EXISTS (SELECT RP.CDPRATO
                                     FROM RECEPADR RP
                                    WHERE (RP.NRORG = C_PRATOS.NRORG)
                                      AND (RP.CDFILAUXPRAT = C_PRATOS.CDFILAUXPRAT)
                                      AND (RP.CDPRATO = C_PRATOS.CDPRATO))
                       AND NOT EXISTS (SELECT RF2.CDPRATO
                                         FROM RECEFILI RF2
                                        WHERE (RF2.NRORG = C_PRATOS.NRORG)
                                          AND (RF2.CDFILAUXPRAT = C_PRATOS.CDFILAUXPRAT)
                                          AND (RF2.CDPRATO = C_PRATOS.CDPRATO)
                                          AND (RF2.CDFILIAL = C_PRATOS.CDFILIAL))
                       AND NOT EXISTS (SELECT RS3.CDPRATO
                                         FROM RECESERV RS3
                                        WHERE (RS3.NRORG = C_PRATOS.NRORG)
                                          AND (RS3.CDFILAUXPRAT = C_PRATOS.CDFILAUXPRAT)
                                          AND (RS3.CDPRATO = C_PRATOS.CDPRATO)
                                          AND (RS3.CDFILIAL = C_PRATOS.CDFILIAL)
                                          AND (RS3.CDSERVFILI = C_PRATOS.CDSERVFILI))
                       AND NOT EXISTS (SELECT RC1.CDPRATO
                                         FROM RECETPCARD RC1
                                        WHERE (RC1.NRORG = C_PRATOS.NRORG)
                                          AND (RC1.CDFILCENTPME = C_PRATOS.CDFILCENTPME)
                                          AND (RC1.CDTIPOCARD = C_PRATOS.CDTIPOCARD)
                                          AND (RC1.CDFILAUXPRAT = C_PRATOS.CDFILAUXPRAT)
                                          AND (RC1.CDPRATO = C_PRATOS.CDPRATO))) FANT ON (FANT.NRORG = PRATO.NRORG)
                                                  AND (FANT.CDPRATO = PRATO.CDPRATO)
                                                  AND (FANT.CDFILAUXPRAT = PRATO.CDFILAUXPRAT)
             WHERE (PRATO.NRORG = C_PRATOS.NRORG)
               AND (PRATO.CDPRATO = C_PRATOS.CDPRATO)
               AND (PRATO.CDFILAUXPRAT = C_PRATOS.CDFILAUXPRAT);
        ELSE 
            V_NMPRATO := NULL;
        END IF;
            
        IF V_NOVO = 'S' THEN
           INSERT INTO TRABCARDMES(NRSEQTRABCARD, NRSEQUITEMTRAB, CDFILIAL, CDSERVFILI, NRSEQCARDMES, NRORDCARDMES,
                                   DTCARDAPIO, DTMESCARD, IDOPCCNFMES1, CDTIPOPRAT1, NRPAGINA,
                                   CDPRATO1, CDFILAUXPRAT1, QTCOMENS1, VRPERCENT1, NMTIPOPRAT1, NMPRATO1,
                                   DTINCLUSAO)
           VALUES(P_NRSEQTRABCARD, V_ROW, C_PRATOS.CDFILIAL, C_PRATOS.CDSERVFILI, C_PRATOS.NRSEQCNFCPME, C_PRATOS.NRORDPRACPME,
                  C_PRATOS.DTCARDIAPME, C_PRATOS.DTINICARDPME, C_PRATOS.IDOPCCNFCPME, C_PRATOS.CDTIPOPRAT, V_NR_PAGINA,
                  C_PRATOS.CDPRATO,  C_PRATOS.CDFILAUXPRAT, C_PRATOS.QTCOMESTPRAT, C_PRATOS.VRPECONSCPME, C_PRATOS.NMTIPOPRAT, V_NMPRATO,
                  SYSDATE);
           V_ROW := V_ROW + 1;  
           V_NOVO := 'N';  
        ELSE
           UPDATE TRABCARDMES
           SET IDOPCCNFMES2  = DECODE(V_CT_COLUNA, 2, C_PRATOS.IDOPCCNFCPME, IDOPCCNFMES2),
               CDTIPOPRAT2   = DECODE(V_CT_COLUNA, 2, C_PRATOS.CDTIPOPRAT, CDTIPOPRAT2),
               CDPRATO2      = DECODE(V_CT_COLUNA, 2, C_PRATOS.CDPRATO, CDPRATO2),
               CDFILAUXPRAT2 = DECODE(V_CT_COLUNA, 2, C_PRATOS.CDFILAUXPRAT, CDFILAUXPRAT2),
               QTCOMENS2     = DECODE(V_CT_COLUNA, 2, C_PRATOS.QTCOMESTPRAT, QTCOMENS2),
               VRPERCENT2    = DECODE(V_CT_COLUNA, 2, C_PRATOS.VRPECONSCPME, VRPERCENT2),
               NMTIPOPRAT2   = DECODE(V_CT_COLUNA, 2, C_PRATOS.NMTIPOPRAT, NMTIPOPRAT2),
               NMPRATO2      = DECODE(V_CT_COLUNA, 2, V_NMPRATO, NMPRATO2),
               IDOPCCNFMES3  = DECODE(V_CT_COLUNA, 3, C_PRATOS.IDOPCCNFCPME, IDOPCCNFMES3),
               CDTIPOPRAT3   = DECODE(V_CT_COLUNA, 3, C_PRATOS.CDTIPOPRAT, CDTIPOPRAT3),
               CDPRATO3      = DECODE(V_CT_COLUNA, 3, C_PRATOS.CDPRATO, CDPRATO3),
               CDFILAUXPRAT3 = DECODE(V_CT_COLUNA, 3, C_PRATOS.CDFILAUXPRAT, CDFILAUXPRAT3),
               QTCOMENS3     = DECODE(V_CT_COLUNA, 3, C_PRATOS.QTCOMESTPRAT, QTCOMENS3),
               VRPERCENT3    = DECODE(V_CT_COLUNA, 3, C_PRATOS.VRPECONSCPME, VRPERCENT3),
               NMTIPOPRAT3   = DECODE(V_CT_COLUNA, 3, C_PRATOS.NMTIPOPRAT, NMTIPOPRAT3),
               NMPRATO3      = DECODE(V_CT_COLUNA, 3, V_NMPRATO, NMPRATO3),
               IDOPCCNFMES4  = DECODE(V_CT_COLUNA, 4, C_PRATOS.IDOPCCNFCPME, IDOPCCNFMES4),
               CDTIPOPRAT4   = DECODE(V_CT_COLUNA, 4, C_PRATOS.CDTIPOPRAT, CDTIPOPRAT4),
               CDPRATO4      = DECODE(V_CT_COLUNA, 4, C_PRATOS.CDPRATO, CDPRATO4),
               CDFILAUXPRAT4 = DECODE(V_CT_COLUNA, 4, C_PRATOS.CDFILAUXPRAT, CDFILAUXPRAT4),
               QTCOMENS4     = DECODE(V_CT_COLUNA, 4, C_PRATOS.QTCOMESTPRAT, QTCOMENS4),
               VRPERCENT4    = DECODE(V_CT_COLUNA, 4, C_PRATOS.VRPECONSCPME, VRPERCENT4),
               NMTIPOPRAT4   = DECODE(V_CT_COLUNA, 4, C_PRATOS.NMTIPOPRAT, NMTIPOPRAT4),
               NMPRATO4      = DECODE(V_CT_COLUNA, 4, V_NMPRATO, NMPRATO4),
               IDOPCCNFMES5   = DECODE(V_CT_COLUNA, 5, C_PRATOS.IDOPCCNFCPME, IDOPCCNFMES5),
               CDTIPOPRAT5    = DECODE(V_CT_COLUNA, 5, C_PRATOS.CDTIPOPRAT, CDTIPOPRAT5),
               CDPRATO5       = DECODE(V_CT_COLUNA, 5, C_PRATOS.CDPRATO, CDPRATO5),
               CDFILAUXPRAT5  = DECODE(V_CT_COLUNA, 5, C_PRATOS.CDFILAUXPRAT, CDFILAUXPRAT5),
               QTCOMENS5      = DECODE(V_CT_COLUNA, 5, C_PRATOS.QTCOMESTPRAT, QTCOMENS5),
               VRPERCENT5     = DECODE(V_CT_COLUNA, 5, C_PRATOS.VRPECONSCPME, VRPERCENT5),
               NMTIPOPRAT5    = DECODE(V_CT_COLUNA, 5, C_PRATOS.NMTIPOPRAT, NMTIPOPRAT5),
               NMPRATO5       = DECODE(V_CT_COLUNA, 5, V_NMPRATO, NMPRATO5),
               IDOPCCNFMES6   = DECODE(V_CT_COLUNA, 6, C_PRATOS.IDOPCCNFCPME, IDOPCCNFMES6),
               CDTIPOPRAT6    = DECODE(V_CT_COLUNA, 6, C_PRATOS.CDTIPOPRAT, CDTIPOPRAT6),
               CDPRATO6       = DECODE(V_CT_COLUNA, 6, C_PRATOS.CDPRATO, CDPRATO6),
               CDFILAUXPRAT6  = DECODE(V_CT_COLUNA, 6, C_PRATOS.CDFILAUXPRAT, CDFILAUXPRAT6),
               QTCOMENS6      = DECODE(V_CT_COLUNA, 6, C_PRATOS.QTCOMESTPRAT, QTCOMENS6),
               VRPERCENT6     = DECODE(V_CT_COLUNA, 6, C_PRATOS.VRPECONSCPME, VRPERCENT6),
               NMTIPOPRAT6    = DECODE(V_CT_COLUNA, 6, C_PRATOS.NMTIPOPRAT, NMTIPOPRAT6),
               NMPRATO6       = DECODE(V_CT_COLUNA, 6, V_NMPRATO, NMPRATO6),
               IDOPCCNFMES7   = DECODE(V_CT_COLUNA, 7, C_PRATOS.IDOPCCNFCPME, IDOPCCNFMES7),
               CDTIPOPRAT7    = DECODE(V_CT_COLUNA, 7, C_PRATOS.CDTIPOPRAT, CDTIPOPRAT7),
               CDPRATO7       = DECODE(V_CT_COLUNA, 7, C_PRATOS.CDPRATO, CDPRATO7),
               CDFILAUXPRAT7  = DECODE(V_CT_COLUNA, 7, C_PRATOS.CDFILAUXPRAT, CDFILAUXPRAT7),
               QTCOMENS7      = DECODE(V_CT_COLUNA, 7, C_PRATOS.QTCOMESTPRAT, QTCOMENS7),
               VRPERCENT7     = DECODE(V_CT_COLUNA, 7, C_PRATOS.VRPECONSCPME, VRPERCENT7),
               NMTIPOPRAT7    = DECODE(V_CT_COLUNA, 7, C_PRATOS.NMTIPOPRAT, NMTIPOPRAT7),
               NMPRATO7       = DECODE(V_CT_COLUNA, 7, V_NMPRATO, NMPRATO7),
               IDOPCCNFMES8   = DECODE(V_CT_COLUNA, 8, C_PRATOS.IDOPCCNFCPME, IDOPCCNFMES8),
               CDTIPOPRAT8    = DECODE(V_CT_COLUNA, 8, C_PRATOS.CDTIPOPRAT, CDTIPOPRAT8),
               CDPRATO8       = DECODE(V_CT_COLUNA, 8, C_PRATOS.CDPRATO, CDPRATO8),
               CDFILAUXPRAT8  = DECODE(V_CT_COLUNA, 8, C_PRATOS.CDFILAUXPRAT, CDFILAUXPRAT8),
               QTCOMENS8      = DECODE(V_CT_COLUNA, 8, C_PRATOS.QTCOMESTPRAT, QTCOMENS8),
               VRPERCENT8     = DECODE(V_CT_COLUNA, 8, C_PRATOS.VRPECONSCPME, VRPERCENT8),
               NMTIPOPRAT8    = DECODE(V_CT_COLUNA, 8, C_PRATOS.NMTIPOPRAT, NMTIPOPRAT8),
               NMPRATO8       = DECODE(V_CT_COLUNA, 8, V_NMPRATO, NMPRATO8),
               IDOPCCNFMES9   = DECODE(V_CT_COLUNA, 9, C_PRATOS.IDOPCCNFCPME, IDOPCCNFMES9),
               CDTIPOPRAT9    = DECODE(V_CT_COLUNA, 9, C_PRATOS.CDTIPOPRAT, CDTIPOPRAT9),
               CDPRATO9       = DECODE(V_CT_COLUNA, 9, C_PRATOS.CDPRATO, CDPRATO9),
               CDFILAUXPRAT9  = DECODE(V_CT_COLUNA, 9, C_PRATOS.CDFILAUXPRAT, CDFILAUXPRAT9),
               QTCOMENS9      = DECODE(V_CT_COLUNA, 9, C_PRATOS.QTCOMESTPRAT, QTCOMENS9),
               VRPERCENT9     = DECODE(V_CT_COLUNA, 9, C_PRATOS.VRPECONSCPME, VRPERCENT9),
               NMTIPOPRAT9    = DECODE(V_CT_COLUNA, 9, C_PRATOS.NMTIPOPRAT, NMTIPOPRAT9),
               NMPRATO9       = DECODE(V_CT_COLUNA, 9, V_NMPRATO, NMPRATO9),
               IDOPCCNFMES10  = DECODE(V_CT_COLUNA, 10, C_PRATOS.IDOPCCNFCPME, IDOPCCNFMES10),
               CDTIPOPRAT10   = DECODE(V_CT_COLUNA, 10, C_PRATOS.CDTIPOPRAT, CDTIPOPRAT10),
               CDPRATO10      = DECODE(V_CT_COLUNA, 10, C_PRATOS.CDPRATO, CDPRATO10),
               CDFILAUXPRAT10 = DECODE(V_CT_COLUNA, 10, C_PRATOS.CDFILAUXPRAT, CDFILAUXPRAT10),
               QTCOMENS10     = DECODE(V_CT_COLUNA, 10, C_PRATOS.QTCOMESTPRAT, QTCOMENS10),
               VRPERCENT10    = DECODE(V_CT_COLUNA, 10, C_PRATOS.VRPECONSCPME, VRPERCENT10),
               NMTIPOPRAT10   = DECODE(V_CT_COLUNA, 10, C_PRATOS.NMTIPOPRAT, NMTIPOPRAT10),
               NMPRATO10      = DECODE(V_CT_COLUNA, 10, V_NMPRATO, NMPRATO10)  
           WHERE CDFILIAL = C_PRATOS.CDFILIAL
             AND CDSERVFILI = C_PRATOS.CDSERVFILI
             AND DTCARDAPIO = C_PRATOS.DTCARDIAPME
             AND NRPAGINA = V_NR_PAGINA;
        END IF;
        
    END LOOP;

   COMMIT;
END;
/

-- VERSAO --
BEGIN ATUVEROBJ('TRABRELCARDMES_PME','X.XX.XXX'); END;
/
