CREATE OR REPLACE PROCEDURE APURAC_FATURAMENTOROP(
   P_CDFILIAL       FILIAL.CDFILIAL%TYPE,
   P_FILIALPAR      FILIAL.CDFILIAL%TYPE,
   P_DTINICIAL      IN DATE,
   P_DTFINAL        IN DATE,
   P_CDFILISUBGR    IN VARCHAR,
   P_NRROPFILIAL    IN VARCHAR)
IS
   V_NRSEQFATUROP      NUMBER;
   V_NRSEQDEDUROP      NUMBER;
   V_IDFATUROP         VARCHAR2(02);
   V_NRSEQFATIMPROP    NUMBER;
   V_IDPERFIXCALCIMP   VARCHAR2(01);
   V_NRAPURREL         VARCHAR2(20);
   V_IDSERVFATROP      CHAR;
   V_IDDTAPURFAT       CHAR;
   V_IDGERROPFOS       CHAR;
   V_IDCONSFATUDIV     CHAR;
   V_CDFILCENTPME      VARCHAR2(04);
   V_IDFILCENTPME      VARCHAR2(01);
   V_COUNT             NUMBER;
   V_NRNOTAFISCORIG    VARCHAR2(20);
   V_CDSERIEORIG       VARCHAR2(05);
   V_VRNOTAFISCIT      FLOAT;
   V_IDNOTADEVO        CHAR;
   V_IDNFESTCANC       CHAR;
   V_VRIMPOSTO         FLOAT;
   V_VRNOTAFISC        FLOAT;
   V_VRIMPOSTOIT       FLOAT;
   V_IDPROJECMANU      VARCHAR2(01);
   V_IDVOLUNEGOCIO     VARCHAR2(01);
   V_IDCONSNFEAUTORIZA VARCHAR2(01);
   V_IDCOMPARANFEFET   VARCHAR2(01);
   V_VENDABRUTA        FLOAT;
   V_VRCANCELAMENTO    FLOAT;
   V_VRCONTABIL        FLOAT;
   V_ITVENDACAN        FLOAT;
   V_VRVENDACANCEL     FLOAT;
BEGIN
   BEGIN
      SELECT IDFATUROP, IDPERFIXCALCIMP, IDSERVFATROP, IDDTAPURFAT, IDGERROPFOS,
             IDCONSFATUDIV, IDPROJECMANU, IDVOLUNEGOCIO, IDCONSNFEAUTORIZA, IDCOMPARANFEFET
        INTO V_IDFATUROP, V_IDPERFIXCALCIMP, V_IDSERVFATROP, V_IDDTAPURFAT, V_IDGERROPFOS,
             V_IDCONSFATUDIV, V_IDPROJECMANU, V_IDVOLUNEGOCIO, V_IDCONSNFEAUTORIZA, V_IDCOMPARANFEFET
        FROM PARFILIALROP P
       WHERE P.CDFILIAL = P_FILIALPAR;
   EXCEPTION
     WHEN NO_DATA_FOUND THEN BEGIN
       V_IDFATUROP := NULL;        V_IDGERROPFOS := NULL;
       V_IDPERFIXCALCIMP := NULL;  V_IDDTAPURFAT := NULL;
       V_IDSERVFATROP := NULL;     V_IDCONSFATUDIV := NULL;
       V_IDPROJECMANU := NULL;     V_IDVOLUNEGOCIO := NULL;
       V_IDCONSNFEAUTORIZA := NULL; V_IDCOMPARANFEFET := NULL;
     END;
   END;

   BEGIN
     SELECT CASE WHEN IDFILCENTPME = 'S'
                 THEN CDFILIAL
                 ELSE CDFILCENTPME
            END, IDFILCENTPME
       INTO V_CDFILCENTPME, V_IDFILCENTPME
       FROM FILIAL
      WHERE CDFILIAL = P_CDFILIAL;
   EXCEPTION
     WHEN NO_DATA_FOUND THEN
       V_CDFILCENTPME := NULL;
       V_IDFILCENTPME := NULL;
   END;

   IF V_IDFATUROP = 'FS' OR V_IDFATUROP = 'AB' OR V_IDCOMPARANFEFET = 'S' THEN
      FOR C_FATURANF IN (SELECT N.CDCLIENTE, N.CDTIPOOPER, N.NRNOTAFISC, N.CDFILIAL,
                                N.NRLANCTONF, N.DTENTRSAID, N.CDSERIE, N.IDSTATUS,
                                N.DTINIREFEFAT, N.DTFINREFEFAT,
                                /* CASE WHEN IDSISTEMA = 'PDV'
                                     THEN N.VRPRODUTOS
                                     ELSE N.VRNOTAFISC
                                END */ N.VRPRODUTOS AS VALORTOTAL, C.NMFANTCLIE, N.IDENTRSAIDOP, N.NRAPURACFAT
                           FROM NOTAFISCAL N INNER JOIN TIPOOPERACROP P ON N.CDTIPOOPER = P.CDTIPOOPER
                                                                       AND N.IDENTRSAIDOP = P.IDENTRSAIDOP
                                             LEFT  JOIN CLIENTE C ON N.CDCLIENTE = C.CDCLIENTE
                                             LEFT  JOIN MODDOCFISC M ON M.CDMODDOCFISC = N.CDMODDOCFISC
                          WHERE N.CDFILIEMIT = P_CDFILIAL
                            AND N.IDENTRSAIDOP = 'S'
                            AND ((('S' = V_IDDTAPURFAT) AND (TRUNC(N.DTENTRSAID)   = P_DTINICIAL)) OR
--                                 (('E' = V_IDDTAPURFAT) AND (TRUNC(N.DTEMISSAO)   = P_DTINICIAL)) OR
                                (( 'R' = V_IDDTAPURFAT) AND (TRUNC(N.DTFINREFEFAT) = P_DTINICIAL)))
                            AND P.CDFILIAL = P_FILIALPAR
                            AND P.IDTPOPERACROP = 'FS'
                            AND (N.IDSTATUS IN ('A','E', 'P') OR(N.IDSTATUSNFE = 'A') OR (N.IDSISTEMA = 'PDV'))
                            AND (N.IDSISTEMA NOT IN ('REC','FIS','ORE') OR N.IDSISTEMA IS NULL)
                            AND N.IDSTATUSNFE <> 'D'
                            AND ((NVL(M.IDMODDOCNFE,'N') = 'S' AND N.IDSTATUSNFE = 'A')
                              OR (NVL(V_IDCONSNFEAUTORIZA,'N') = 'N')
                              OR (NVL(V_IDCONSNFEAUTORIZA,'N') = 'S' AND NVL(M.IDMODDOCNFE,'N') = 'N'))
                            AND NOT EXISTS (SELECT CDTIPOOPER
                                              FROM TIPOOPERACROP
                                             WHERE CDFILIAL = P_FILIALPAR
                                               AND IDENTRSAIDOP = 'S'
                                               AND IDTPOPERACROP = 'CS'
                                               AND CDTIPOOPER = N.CDTIPOOPER)) LOOP
         V_VRNOTAFISC := C_FATURANF.VALORTOTAL;

         NOVO_CODIGO('GESFATURAMENTOROP', V_NRSEQFATUROP);

         INSERT INTO FATURAMENTOROP (NRFILIALROP, NRFATURAMENTOROP, CDFILIAL, DTENTRSAID, DTREFERENC,
                                     CDFATUDIVER, CDTIPOOPER, IDENTRSAIDOP, CDCLIENTE, DSFATURAMENTO,
                                     CDFILIALLANC, NRLANCTONF, NRNOTAFISC, CDSERIE, NRNOTAFISCORIG,
                                     CDSERIEORIG, CDSERVFILI, NRRECEXTRAFILI, IDTPLANCROP, CDFILIALCENT,
                                     IDSTATUS, DTINIREFEFAT, DTFINREFEFAT, VRFATURAMENTO, QTFATURAMENTO, VRFATDEVCANC,
                                     NRAPURACFAT)
              VALUES (P_NRROPFILIAL, V_NRSEQFATUROP, P_CDFILIAL, C_FATURANF.DTENTRSAID, C_FATURANF.DTFINREFEFAT,
                      NULL, C_FATURANF.CDTIPOOPER, C_FATURANF.IDENTRSAIDOP, C_FATURANF.CDCLIENTE, C_FATURANF.NMFANTCLIE,
                      C_FATURANF.CDFILIAL, C_FATURANF.NRLANCTONF, C_FATURANF.NRNOTAFISC, C_FATURANF.CDSERIE, NULL,
                      NULL, NULL, NULL, 'FAT_NOTAFISCAL', V_CDFILCENTPME,
                      C_FATURANF.IDSTATUS, C_FATURANF.DTINIREFEFAT, C_FATURANF.DTFINREFEFAT, V_VRNOTAFISC, 0, NULL,
                      C_FATURANF.NRAPURACFAT);

         IF V_IDPERFIXCALCIMP = 'N' THEN
            FOR C_IMPOSTO IN (SELECT P.CDIMPOSTO, SUM(VALOR_IMPOSTO_NF(C_FATURANF.CDFILIAL, C_FATURANF.NRLANCTONF, P.CDIMPOSTO,'I')) AS VRIMPOSTO,
                                     I.NMIMPOSTO
                                FROM IMPOSTOROP P INNER JOIN IMPOSTO I ON P.CDIMPOSTO = I.CDIMPOSTO
                               WHERE P.CDFILIAL = P_FILIALPAR
                                 AND IDTPIMPOSTOROP = 'FS'
                               GROUP BY P.CDIMPOSTO, I.NMIMPOSTO
                               ORDER BY P.CDIMPOSTO) LOOP

               V_VRIMPOSTO := C_IMPOSTO.VRIMPOSTO;

               NOVO_CODIGO('GESDEDUCAOROP', V_NRSEQDEDUROP);

               INSERT INTO DEDUCAOROP
                         (NRFILIALROP, NRDEDUCAOROP, CDFILIAL, DTREFERENC, NRNOTAFISC,
                          CDIMPOSTO, CDENCARGO, DSDEDUCAO, CDCLIENTE, CDFILIALBENE,
                          CDFORNECED, DSBENEFICIARIO, CDFILIALLANC, NRLANCTONF, NRRECEXTRAFILI,
                          IDTPLANCROP, CDFILIALCENT, VRDEDUCAO, VRPERFIXCALCIMP)
                    VALUES
                         (P_NRROPFILIAL, V_NRSEQDEDUROP, P_CDFILIAL, P_DTINICIAL, C_FATURANF.NRNOTAFISC,
                          C_IMPOSTO.CDIMPOSTO, NULL, C_IMPOSTO.NMIMPOSTO, C_FATURANF.CDCLIENTE, NULL,
                          NULL, C_FATURANF.NMFANTCLIE, C_FATURANF.CDFILIAL, C_FATURANF.NRLANCTONF, NULL,
                          'FAT_NOTAFISCAL', V_CDFILCENTPME, V_VRIMPOSTO, NULL);
            END LOOP;
         ELSE
            FOR C_IMPOSTO IN(SELECT P.CDIMPOSTO, (C_FATURANF.VALORTOTAL * NVL(VRPERFIXCALCIMP,0))/100 AS VRIMPOSTO,IDTRATIMPROP, VRPERFIXCALCIMP,
                                    I.NMIMPOSTO
                               FROM IMPOSTOROP P INNER JOIN IMPOSTO I ON P.CDIMPOSTO = I.CDIMPOSTO
                              WHERE P.CDFILIAL = P_FILIALPAR
                                AND IDTPIMPOSTOROP = 'FS'
                              ORDER BY P.CDIMPOSTO) LOOP

               NOVO_CODIGO('GESDEDUCAOROP', V_NRSEQDEDUROP);

               INSERT INTO DEDUCAOROP
                         (NRFILIALROP, NRDEDUCAOROP, CDFILIAL, DTREFERENC, NRNOTAFISC,
                          CDIMPOSTO, CDENCARGO, DSDEDUCAO, CDCLIENTE, CDFILIALBENE,
                          CDFORNECED, DSBENEFICIARIO, CDFILIALLANC, NRLANCTONF, NRRECEXTRAFILI,
                          IDTPLANCROP, CDFILIALCENT, VRDEDUCAO, VRPERFIXCALCIMP)
                    VALUES
                         (P_NRROPFILIAL, V_NRSEQDEDUROP, P_CDFILIAL, P_DTINICIAL, C_FATURANF.NRNOTAFISC,
                          C_IMPOSTO.CDIMPOSTO, NULL, C_IMPOSTO.NMIMPOSTO, C_FATURANF.CDCLIENTE, NULL,
                          NULL, C_FATURANF.NMFANTCLIE, C_FATURANF.CDFILIAL, C_FATURANF.NRLANCTONF, NULL,
                          'FAT_NOTAFISCAL', V_CDFILCENTPME, DECODE(C_IMPOSTO.IDTRATIMPROP, 'D',-1,1) * C_IMPOSTO.VRIMPOSTO, C_IMPOSTO.VRPERFIXCALCIMP);
            END LOOP;
         END IF;
      END LOOP;

      FOR C_DEVOLUCAO IN (SELECT CASE P.IDDATADEVOLROP WHEN 'SAIDA_NF_ORIGEM' THEN TRUNC(ORIG.DTENTRSAID)
                                                       ELSE TRUNC(NF.DTENTRSAID)
                                 END DTENTRSAID, TRUNC(NF.DTFINREFEFAT) DTREFE,
                                 NF.CDTIPOOPER, NF.IDENTRSAIDOP, NF.CDCLIENTE, C.NMFANTCLIE,
                                 NF.CDFILIAL, NF.NRLANCTONF, NF.NRNOTAFISC, NF.CDSERIE, ORIG.NRNOTAFISC NRNOTAFISCORIG,
                                 ORIG.CDSERIE CDSERIEORIG, NF.IDSTATUS, NF.DTINIREFEFAT, NF.DTFINREFEFAT,
                                 NF.VRNOTAFISC, ORIG.VRNOTAFISC VRNOTAFISCORIG
                            FROM NOTAFISCAL NF INNER JOIN ITEMNOTAFISC I ON NF.CDFILIAL = I.CDFILIAL
                                                                        AND NF.NRLANCTONF = I.NRLANCTONF
                                               INNER JOIN NOTAFISCAL ORIG ON ORIG.CDFILIAL = I.CDFILIALIT
                                                                         AND ORIG.NRLANCTONF = I.NRLANCTONFIT
                                               INNER JOIN PARFILIALROP P ON P.CDFILIAL = TIPOPARAM_DRO(NF.CDFILIRECE)
                                               LEFT  JOIN CLIENTE C ON NF.CDCLIENTE = C.CDCLIENTE
                           WHERE NF.CDFILIRECE = P_CDFILIAL
                             AND NF.IDENTRSAIDOP = 'E'
                             AND ((P.IDDATADEVOLROP = 'SAIDA_NF_ORIGEM' AND TRUNC(ORIG.DTENTRSAID) = P_DTINICIAL)
                               OR (NVL(P.IDDATADEVOLROP,'SAIDA_NF') = 'SAIDA_NF' AND TRUNC(NF.DTENTRSAID) = P_DTINICIAL))
                             AND EXISTS (SELECT 1
                                           FROM DEDURECEITAROP DD
                                          WHERE DD.CDTIPOOPER = NF.CDTIPOOPER
                                            AND DD.IDENTRSAIDOP = NF.IDENTRSAIDOP
                                            AND DD.CDFILIAL = TIPOPARAM_DRO(NF.CDFILIRECE))) LOOP

         NOVO_CODIGO('GESFATURAMENTOROP', V_NRSEQFATUROP);

         INSERT INTO FATURAMENTOROP (NRFILIALROP, NRFATURAMENTOROP, CDFILIAL, DTENTRSAID, DTREFERENC,
                                     CDFATUDIVER, CDTIPOOPER, IDENTRSAIDOP, CDCLIENTE, DSFATURAMENTO,
                                     CDFILIALLANC, NRLANCTONF, NRNOTAFISC, CDSERIE, NRNOTAFISCORIG,
                                     CDSERIEORIG, CDSERVFILI, NRRECEXTRAFILI, IDTPLANCROP, CDFILIALCENT,
                                     IDSTATUS, DTINIREFEFAT, DTFINREFEFAT, VRFATURAMENTO, QTFATURAMENTO, VRFATDEVCANC,
                                     NRAPURACFAT)
              VALUES (P_NRROPFILIAL, V_NRSEQFATUROP, P_CDFILIAL, C_DEVOLUCAO.DTENTRSAID, C_DEVOLUCAO.DTREFE,
                      NULL, C_DEVOLUCAO.CDTIPOOPER, C_DEVOLUCAO.IDENTRSAIDOP, C_DEVOLUCAO.CDCLIENTE, C_DEVOLUCAO.NMFANTCLIE,
                      C_DEVOLUCAO.CDFILIAL, C_DEVOLUCAO.NRLANCTONF, C_DEVOLUCAO.NRNOTAFISC, C_DEVOLUCAO.CDSERIE, C_DEVOLUCAO.NRNOTAFISCORIG,
                      C_DEVOLUCAO.CDSERIEORIG, NULL, NULL, 'FAT_NOTAFISCAL', V_CDFILCENTPME,
                      C_DEVOLUCAO.IDSTATUS, C_DEVOLUCAO.DTINIREFEFAT, C_DEVOLUCAO.DTFINREFEFAT, 0, 0, C_DEVOLUCAO.VRNOTAFISC,
                      NULL);
      END LOOP;
   END IF;

   IF V_IDFATUROP = 'FE' OR V_IDFATUROP = 'AB' OR V_IDCOMPARANFEFET = 'S' THEN
      SELECT LPAD(ABS(ROUND(DBMS_RANDOM.VALUE(9999,9999999999))),10,'0')
        INTO V_NRAPURREL
        FROM DUAL;

      APURAC_RELGESDIRETA(P_CDFILIAL, P_DTINICIAL, P_DTINICIAL, V_NRAPURREL);

      FOR C_EFET_PME IN (SELECT E.CDFILCENTPME, E.CDFILIAL, E.CDSERVFILI, E.DTEFETCLPME, E.CDCLIENTE,
                                NVL(SUM(E.QTCOMEFCLPME),0) AS QTCOMEFCLPME, NVL(SUM(VRSERVICO * E.QTCOMEFCLPME),0) VRDIETFATU,
                                CC.NMFANTCLIE
                           FROM EFETCLIEPME E INNER JOIN HISTVRSERVPME C ON E.CDFILCENTPME = C.CDFILCENTPME
                                                                        AND E.CDSERVFILI = C.CDSERVFILI
                                                                        AND E.CDCLIENTE = C.CDCLIENTE
                                                                        AND E.CDFILIAL = C.CDFILIAL
                                              INNER JOIN FILIAL F ON E.CDFILIAL = F.CDFILIAL
                                              INNER JOIN (SELECT CDFILIAL, CDSERVFILI, 'E' AS IDTPSERVICOROP
                                                            FROM SERVICO
                                                           WHERE CDFILIAL = P_CDFILIAL
                                                             AND V_IDSERVFATROP = 'S'
                                                           UNION
                                                          SELECT CDFILIAL,CDSERVFILI,IDTPSERVICOROP
                                                            FROM SERVICOROP
                                                           WHERE CDFILIAL = P_FILIALPAR
                                                             AND V_IDSERVFATROP ='N') S ON E.CDSERVFILI = S.CDSERVFILI
                                              LEFT  JOIN CLIENTE CC ON CC.CDCLIENTE = E.CDCLIENTE 
                          WHERE E.CDFILCENTPME = V_CDFILCENTPME
                            AND V_IDFILCENTPME = 'S'
                            AND TRUNC(DTEFETCLPME) = P_DTINICIAL
                            AND IDTPSERVICOROP = 'E'
                            AND E.QTCOMEFCLPME > 0
                            AND F.CDFILIAL IN (SELECT CDFILIAL
                                                 FROM FILIAL
                                                WHERE CDFILCENTPME = E.CDFILCENTPME)
                          GROUP BY E.CDFILCENTPME, E.CDFILIAL, E.CDSERVFILI, E.DTEFETCLPME, E.CDCLIENTE, CC.NMFANTCLIE
                          UNION ALL
                         SELECT E.CDFILCENTPME, E.CDFILIAL, E.CDSERVFILI, E.DTEFETCLPME, E.CDCLIENTE,
                                NVL(SUM(E.QTCOMEFCLPME),0) AS QTCOMEFCLPME, NVL(SUM(VRSERVICO * E.QTCOMEFCLPME),0) VRDIETFATU,
                                CC.NMFANTCLIE
                           FROM EFETCLIEPME E INNER JOIN HISTVRSERVPME C ON E.CDFILCENTPME = C.CDFILCENTPME
                                                                        AND E.CDSERVFILI = C.CDSERVFILI
                                                                        AND E.CDCLIENTE = C.CDCLIENTE
                                                                        AND E.CDFILIAL = C.CDFILIAL
                                              INNER JOIN FILIAL F ON E.CDFILIAL = F.CDFILIAL
                                              INNER JOIN (SELECT CDFILIAL, CDSERVFILI, 'E' AS IDTPSERVICOROP
                                                            FROM SERVICO
                                                           WHERE CDFILIAL = P_CDFILIAL
                                                             AND V_IDSERVFATROP ='S'
                                                           UNION
                                                          SELECT CDFILIAL, CDSERVFILI, IDTPSERVICOROP
                                                            FROM SERVICOROP
                                                           WHERE CDFILIAL = P_FILIALPAR
                                                             AND V_IDSERVFATROP ='N') S ON E.CDSERVFILI = S.CDSERVFILI
                                                                                       AND S.CDSERVFILI = C.CDSERVFILI
                                              LEFT  JOIN CLIENTE CC ON CC.CDCLIENTE = E.CDCLIENTE 
                          WHERE E.CDFILIAL = P_CDFILIAL
                            AND V_IDFILCENTPME = 'N'
                            AND TRUNC(DTEFETCLPME) = P_DTINICIAL
                            AND IDTPSERVICOROP = 'E'
                            AND E.QTCOMEFCLPME > 0
                            AND F.CDFILIAL IN (SELECT CDFILIAL
                                                 FROM FILIAL
                                                WHERE CDFILCENTPME = E.CDFILCENTPME)
                          GROUP BY E.CDFILCENTPME, E.CDFILIAL, E.CDSERVFILI, E.DTEFETCLPME, E.CDCLIENTE, CC.NMFANTCLIE) LOOP

         NOVO_CODIGO('GESFATURAMENTOROP', V_NRSEQFATUROP);

         INSERT INTO FATURAMENTOROP (NRFILIALROP, NRFATURAMENTOROP, CDFILIAL, DTENTRSAID, DTREFERENC,
                                     CDFATUDIVER, CDTIPOOPER, IDENTRSAIDOP, CDCLIENTE, DSFATURAMENTO,
                                     CDFILIALLANC, NRLANCTONF, NRNOTAFISC, CDSERIE, NRNOTAFISCORIG,
                                     CDSERIEORIG, CDSERVFILI, NRRECEXTRAFILI, IDTPLANCROP, CDFILIALCENT,
                                     IDSTATUS, DTINIREFEFAT, DTFINREFEFAT, VRFATURAMENTO, QTFATURAMENTO)
              VALUES (P_NRROPFILIAL, V_NRSEQFATUROP, P_CDFILIAL, C_EFET_PME.DTEFETCLPME, C_EFET_PME.DTEFETCLPME,
                      NULL, NULL, NULL, C_EFET_PME.CDCLIENTE, C_EFET_PME.NMFANTCLIE,
                      C_EFET_PME.CDFILIAL, NULL, NULL, NULL, NULL,
                      NULL, C_EFET_PME.CDSERVFILI, NULL, 'FAT_EFETIVO', V_CDFILCENTPME,
                      NULL, NULL, NULL, C_EFET_PME.VRDIETFATU, C_EFET_PME.QTCOMEFCLPME);
                      
         IF V_IDPERFIXCALCIMP = 'N' THEN             
            IMPOSTOROP_RELGESPME(P_CDFILIAL, P_DTINICIAL, P_DTINICIAL, P_NRROPFILIAL, V_NRSEQFATUROP, V_NRSEQFATIMPROP, C_EFET_PME.CDCLIENTE,
                                 C_EFET_PME.DTEFETCLPME, C_EFET_PME.CDSERVFILI, 'S');
         ELSE
            FOR C_IMPOSTO IN(SELECT P.CDIMPOSTO, (C_EFET_PME.VRDIETFATU * NVL(VRPERFIXCALCIMP,0))/100 AS VRIMPOSTO, IDTRATIMPROP,VRPERFIXCALCIMP,
                                    I.NMIMPOSTO
                               FROM IMPOSTOROP P INNER JOIN IMPOSTO I ON P.CDIMPOSTO = I.CDIMPOSTO
                              WHERE P.CDFILIAL = P_FILIALPAR
                                AND IDTPIMPOSTOROP = 'FE'
                              ORDER BY P.CDIMPOSTO) LOOP
               
               NOVO_CODIGO('GESDEDUCAOROP', V_NRSEQDEDUROP);

               INSERT INTO DEDUCAOROP
                         (NRFILIALROP, NRDEDUCAOROP, CDFILIAL, DTREFERENC, NRNOTAFISC,
                          CDIMPOSTO, CDENCARGO, DSDEDUCAO, CDCLIENTE, CDFILIALBENE,
                          CDFORNECED, DSBENEFICIARIO, CDFILIALLANC, NRLANCTONF, NRRECEXTRAFILI,
                          IDTPLANCROP, CDFILIALCENT, VRDEDUCAO, VRPERFIXCALCIMP,CDSERVFILI)
                    VALUES
                         (P_NRROPFILIAL, V_NRSEQDEDUROP, P_CDFILIAL, P_DTINICIAL, NULL,
                          C_IMPOSTO.CDIMPOSTO, NULL, C_IMPOSTO.NMIMPOSTO, C_EFET_PME.CDCLIENTE, NULL,
                          NULL, C_EFET_PME.NMFANTCLIE, NULL, NULL, NULL,
                          'FAT_EFETIVO', V_CDFILCENTPME, DECODE(C_IMPOSTO.IDTRATIMPROP, 'D',-1,1) * C_IMPOSTO.VRIMPOSTO, C_IMPOSTO.VRPERFIXCALCIMP,C_EFET_PME.CDSERVFILI);
            END LOOP;
         END IF;
      END LOOP;

      FOR C_EFET_HOSP IN (SELECT E.CDFILIAL, E.DTEFETDIET, E.CDSERVFILI,   E.CDCLIENTE,
                                 NVL(SUM(UTIL_APURAC.CalcValrServ(E.CDFILIAL, E.CDCLIENTE, E.CDSERVFILI,
                                   DTEFETDIET, NULL) * E.QTCOMEFETDIE),0) AS VRDIETFATU,
                                 NVL(SUM(E.QTCOMEFETDIE),0) AS QTCOMEFETDIE, CC.NMFANTCLIE
                            FROM EFETDIET E INNER JOIN SERVICOROP S ON E.CDSERVFILI = S.CDSERVFILI
                                            LEFT  JOIN CLIENTE CC ON CC.CDCLIENTE = E.CDCLIENTE
                           WHERE E.CDFILIAL = P_CDFILIAL
                             AND TRUNC(E.DTEFETDIET) = P_DTINICIAL
                             AND P_FILIALPAR = S.CDFILIAL
                             AND IDTPSERVICOROP = 'E'
                             AND V_IDSERVFATROP <> 'S'
                           GROUP BY E.CDFILIAL, E.DTEFETDIET, E.CDSERVFILI,   E.CDCLIENTE, CC.NMFANTCLIE
                           UNION ALL
                          SELECT E.CDFILIAL, E.DTEFETDIET, E.CDSERVFILI,   E.CDCLIENTE,
                                 NVL(SUM(UTIL_APURAC.CalcValrServ(E.CDFILIAL, E.CDCLIENTE, E.CDSERVFILI,
                                   DTEFETDIET, NULL) * E.QTCOMEFETDIE),0) AS VRDIETFATU,
                                 NVL(SUM(E.QTCOMEFETDIE),0) AS QTCOMEFETDIE, CC.NMFANTCLIE
                            FROM EFETDIET E LEFT  JOIN CLIENTE CC ON CC.CDCLIENTE = E.CDCLIENTE
                           WHERE E.CDFILIAL = P_CDFILIAL
                             AND TRUNC(E.DTEFETDIET) = P_DTINICIAL
                             AND V_IDSERVFATROP = 'S'
                           GROUP BY E.CDFILIAL, E.DTEFETDIET, E.CDSERVFILI, E.CDCLIENTE, CC.NMFANTCLIE) LOOP

         NOVO_CODIGO('GESFATURAMENTOROP', V_NRSEQFATUROP);

         INSERT INTO FATURAMENTOROP (NRFILIALROP, NRFATURAMENTOROP, CDFILIAL, DTENTRSAID, DTREFERENC,
                                     CDFATUDIVER, CDTIPOOPER, IDENTRSAIDOP, CDCLIENTE, DSFATURAMENTO,
                                     CDFILIALLANC, NRLANCTONF, NRNOTAFISC, CDSERIE, NRNOTAFISCORIG,
                                     CDSERIEORIG, CDSERVFILI, NRRECEXTRAFILI, IDTPLANCROP, CDFILIALCENT,
                                     IDSTATUS, DTINIREFEFAT, DTFINREFEFAT, VRFATURAMENTO, QTFATURAMENTO)
              VALUES (P_NRROPFILIAL, V_NRSEQFATUROP, P_CDFILIAL, C_EFET_HOSP.DTEFETDIET, C_EFET_HOSP.DTEFETDIET,
                      NULL, NULL, NULL, C_EFET_HOSP.CDCLIENTE, C_EFET_HOSP.NMFANTCLIE,
                      C_EFET_HOSP.CDFILIAL, NULL, NULL, NULL, NULL,
                      NULL, C_EFET_HOSP.CDSERVFILI, NULL, 'FAT_EFETIVO', V_CDFILCENTPME,
                      NULL, NULL, NULL, C_EFET_HOSP.VRDIETFATU, C_EFET_HOSP.QTCOMEFETDIE);

         IF V_IDPERFIXCALCIMP  = 'S' THEN         
            FOR C_IMPOSTO IN(SELECT P.CDIMPOSTO, (C_EFET_HOSP.VRDIETFATU * NVL(VRPERFIXCALCIMP,0))/100 AS VRIMPOSTO,IDTRATIMPROP,
                                    VRPERFIXCALCIMP, I.NMIMPOSTO
                               FROM IMPOSTOROP P INNER JOIN IMPOSTO I ON P.CDIMPOSTO = I.CDIMPOSTO
                              WHERE P.CDFILIAL = P_FILIALPAR
                                AND IDTPIMPOSTOROP = 'FE'
                              ORDER BY P.CDIMPOSTO) LOOP
               
               NOVO_CODIGO('GESDEDUCAOROP', V_NRSEQDEDUROP);

               INSERT INTO DEDUCAOROP
                         (NRFILIALROP, NRDEDUCAOROP, CDFILIAL, DTREFERENC, NRNOTAFISC,
                          CDIMPOSTO, CDENCARGO, DSDEDUCAO, CDCLIENTE, CDFILIALBENE,
                          CDFORNECED, DSBENEFICIARIO, CDFILIALLANC, NRLANCTONF, NRRECEXTRAFILI,
                          IDTPLANCROP, CDFILIALCENT, VRDEDUCAO, VRPERFIXCALCIMP,CDSERVFILI)
                    VALUES
                         (P_NRROPFILIAL, V_NRSEQDEDUROP, P_CDFILIAL, P_DTINICIAL, NULL,
                          C_IMPOSTO.CDIMPOSTO, NULL, C_IMPOSTO.NMIMPOSTO, C_EFET_HOSP.CDCLIENTE, NULL,
                          NULL, C_EFET_HOSP.NMFANTCLIE, NULL, NULL, NULL,
                          'FAT_EFETIVO', V_CDFILCENTPME,DECODE(C_IMPOSTO.IDTRATIMPROP, 'D',-1,1) * C_IMPOSTO.VRIMPOSTO, C_IMPOSTO.VRPERFIXCALCIMP,C_EFET_HOSP.CDSERVFILI);
            END LOOP;
         ELSE
            IMPOSTOROP_RELGESHOSP(P_CDFILIAL, P_DTINICIAL, P_DTINICIAL, P_NRROPFILIAL, V_NRSEQFATUROP, V_NRSEQFATIMPROP, C_EFET_HOSP.CDCLIENTE,
                                  C_EFET_HOSP.DTEFETDIET, C_EFET_HOSP.CDSERVFILI, 'S');
         END IF;
      END LOOP;

      FOR C_EFETIVOS IN (SELECT DISTINCT CDCLIENTEAPGD, DTAPURACFATGD, CDSERVFILIAPGD, CDFILIALAPGD, NRAPURACGD,
                                NVL(SUM(T.NREFETIVOSAPGD),0) AS QUANTIDADE, NVL(SUM(T.VRPRECUNITAPGD * T.NREFETIVOSAPGD),0) AS VALORTOTAL,
                                CC.NMFANTCLIE
                           FROM SERVICOROP S INNER JOIN TRAB_APURAC_GD T ON T.CDSERVFILIAPGD = S.CDSERVFILI
                                             LEFT  JOIN CLIENTE CC ON T.CDCLIENTEAPGD = CC.CDCLIENTE
                          WHERE T.CDFILIALAPGD = P_CDFILIAL
                            AND T.NRAPURACGD = V_NRAPURREL
                            AND TRUNC(T.DTAPURACFATGD) = P_DTINICIAL
                            AND S.CDFILIAL = P_FILIALPAR
                            AND IDTPSERVICOROP = 'E'
                            AND V_IDSERVFATROP <> 'S'
                          GROUP BY  CDFILIALAPGD, NRAPURACGD, T.CDSERVFILIAPGD, DTAPURACFATGD, CDCLIENTEAPGD, CC.NMFANTCLIE
                          UNION ALL
                          SELECT DISTINCT CDCLIENTEAPGD, DTAPURACFATGD,
                                 CDSERVFILIAPGD, CDFILIALAPGD, NRAPURACGD,
                                 NVL(SUM(T.NREFETIVOSAPGD),0) AS QUANTIDADE,
                                 NVL(SUM(T.VRPRECUNITAPGD * T.NREFETIVOSAPGD),0) AS VALORTOTAL,
                                 CC.NMFANTCLIE
                            FROM TRAB_APURAC_GD T LEFT  JOIN CLIENTE CC ON T.CDCLIENTEAPGD = CC.CDCLIENTE
                           WHERE T.CDFILIALAPGD = P_CDFILIAL
                             AND T.NRAPURACGD = V_NRAPURREL
                             AND TRUNC(T.DTAPURACFATGD) = P_DTINICIAL
                             AND V_IDSERVFATROP = 'S'
                           GROUP BY  CDFILIALAPGD, NRAPURACGD, T.CDSERVFILIAPGD, DTAPURACFATGD, CDCLIENTEAPGD, CC.NMFANTCLIE
                           UNION ALL
                          SELECT DISTINCT CDCLIENTEAPTU, DTAPURACFATTU,
                                 CDSERVFILIAPTU, CDFILIALAPTU, NRAPURACTU,
                                 NVL(SUM(T.NREFETIVOSAPTU),0) AS QUANTIDADE,
                                 NVL(SUM(T.VRPRECUNITAPTU * T.NREFETIVOSAPTU),0) AS VALORTOTAL,
                                 CC.NMFANTCLIE
                            FROM TRAB_APURAC_TU T LEFT  JOIN CLIENTE CC ON T.CDCLIENTEAPTU = CC.CDCLIENTE
                           WHERE T.CDFILIALAPTU = P_CDFILIAL
                             AND T.NRAPURACTU = V_NRAPURREL
                             AND TRUNC(T.DTAPURACFATTU) = P_DTINICIAL
                             AND V_IDSERVFATROP = 'S'
                           GROUP BY  CDFILIALAPTU, NRAPURACTU, T.CDSERVFILIAPTU, DTAPURACFATTU, CDCLIENTEAPTU, CC.NMFANTCLIE
                           UNION ALL
                          SELECT DISTINCT T.CDCLIENTEAPEQ, T.DTAPURACFATEQ,
                                 T.CDSERVFILIAPEQ, T.CDFILIALAPEQ, T.NRAPURACEQ,
                                 NVL(SUM(T.NREFETIVOSAPEQ ),0) AS QUANTIDADE,
                                 NVL(SUM(T.VRPRECUNITAPEQ * T.NREFETIVOSAPEQ),0) AS VALORTOTAL,
                                CC.NMFANTCLIE
                            FROM TRAB_APURAC_EQ T LEFT  JOIN CLIENTE CC ON T.CDCLIENTEAPEQ = CC.CDCLIENTE
                           WHERE T.CDFILIALAPEQ = P_CDFILIAL
                             AND T.NRAPURACEQ = V_NRAPURREL
                             AND TRUNC(T.DTAPURACFATEQ) = P_DTINICIAL
                             AND V_IDSERVFATROP = 'S'
                           GROUP BY CDFILIALAPEQ, NRAPURACEQ, T.CDSERVFILIAPEQ, DTAPURACFATEQ, CDCLIENTEAPEQ, CC.NMFANTCLIE) LOOP

         NOVO_CODIGO('GESFATURAMENTOROP', V_NRSEQFATUROP);

         INSERT INTO FATURAMENTOROP (NRFILIALROP, NRFATURAMENTOROP, CDFILIAL, DTENTRSAID, DTREFERENC,
                                     CDFATUDIVER, CDTIPOOPER, IDENTRSAIDOP, CDCLIENTE, DSFATURAMENTO,
                                     CDFILIALLANC, NRLANCTONF, NRNOTAFISC, CDSERIE, NRNOTAFISCORIG,
                                     CDSERIEORIG, CDSERVFILI, NRRECEXTRAFILI, IDTPLANCROP, CDFILIALCENT,
                                     IDSTATUS, DTINIREFEFAT, DTFINREFEFAT, VRFATURAMENTO, QTFATURAMENTO)
              VALUES (P_NRROPFILIAL, V_NRSEQFATUROP, P_CDFILIAL, C_EFETIVOS.DTAPURACFATGD, C_EFETIVOS.DTAPURACFATGD,
                      NULL, NULL, NULL, C_EFETIVOS.CDCLIENTEAPGD, C_EFETIVOS.NMFANTCLIE,
                      C_EFETIVOS.CDFILIALAPGD, NULL, NULL, NULL, NULL,
                      NULL, C_EFETIVOS.CDSERVFILIAPGD, NULL, 'FAT_EFETIVO', V_CDFILCENTPME,
                      NULL, NULL, NULL, C_EFETIVOS.VALORTOTAL, C_EFETIVOS.QUANTIDADE);
                      
         IF V_IDPERFIXCALCIMP  = 'S' THEN
         
            FOR C_IMPOSTO IN (SELECT P.CDIMPOSTO, (C_EFETIVOS.VALORTOTAL * NVL(VRPERFIXCALCIMP,0))/100 AS VRIMPOSTO, IDTRATIMPROP, VRPERFIXCALCIMP,
                                     I.NMIMPOSTO
                                FROM IMPOSTOROP P INNER JOIN IMPOSTO I ON P.CDIMPOSTO = I.CDIMPOSTO
                               WHERE P.CDFILIAL = P_FILIALPAR
                                 AND IDTPIMPOSTOROP = 'FE'
                               ORDER BY P.CDIMPOSTO) LOOP
               
               NOVO_CODIGO('GESDEDUCAOROP', V_NRSEQDEDUROP);

               INSERT INTO DEDUCAOROP
                         (NRFILIALROP, NRDEDUCAOROP, CDFILIAL, DTREFERENC, NRNOTAFISC,
                          CDIMPOSTO, CDENCARGO, DSDEDUCAO, CDCLIENTE, CDFILIALBENE,
                          CDFORNECED, DSBENEFICIARIO, CDFILIALLANC, NRLANCTONF, NRRECEXTRAFILI,
                          IDTPLANCROP, CDFILIALCENT, VRDEDUCAO, VRPERFIXCALCIMP,CDSERVFILI)
                    VALUES
                         (P_NRROPFILIAL, V_NRSEQDEDUROP, P_CDFILIAL, P_DTINICIAL, NULL,
                          C_IMPOSTO.CDIMPOSTO, NULL, C_IMPOSTO.NMIMPOSTO, C_EFETIVOS.CDCLIENTEAPGD, NULL,
                          NULL, C_EFETIVOS.NMFANTCLIE, NULL, NULL, NULL,
                          'FAT_EFETIVO', V_CDFILCENTPME, DECODE(C_IMPOSTO.IDTRATIMPROP, 'D',-1,1) * C_IMPOSTO.VRIMPOSTO, C_IMPOSTO.VRPERFIXCALCIMP,C_EFETIVOS.CDSERVFILIAPGD);
            END LOOP;
         ELSE                
            IMPOSTOROP_RELGESDIRETA(P_CDFILIAL, P_DTINICIAL, P_DTINICIAL, V_NRAPURREL,
                                    P_NRROPFILIAL, V_NRSEQFATUROP, V_NRSEQFATIMPROP, C_EFETIVOS.CDCLIENTEAPGD,
                                    C_EFETIVOS.DTAPURACFATGD, C_EFETIVOS.CDSERVFILIAPGD, 'S');
         END IF;
      END LOOP;
      DELETE FROM TRAB_APURAC_GD
            WHERE CDFILIALAPGD = P_CDFILIAL
              AND NRAPURACGD = V_NRAPURREL
              AND TRUNC(DTAPURACFATGD) = P_DTINICIAL;
   END IF;

   IF V_IDVOLUNEGOCIO = 'S' THEN
      FOR C_VOLUME IN (SELECT RF.CDFILIAL, R.CDRECEXTRA, R.DESCRECEXTRA, RF.DTRECEXTRAFILI, CASE WHEN IDTPRECEXTRAFILI = 'DEDUCAO'
                                                                                                 THEN RF.VRRECEXTRAFILI * (-1)
                                                                                                 ELSE RF.VRRECEXTRAFILI
                                                                                            END VRRECEXTRAFILI, RF.NRRECEXTRAFILI
                         FROM RECEXTRAFILI RF,RECEXTRA R
                        WHERE RF.CDFILIAL = P_CDFILIAL
                          AND RF.CDRECEXTRA = R.CDRECEXTRA
                          AND TRUNC(RF.DTRECEXTRAFILI) = P_DTINICIAL) LOOP

         NOVO_CODIGO('GESFATURAMENTOROP', V_NRSEQFATUROP);

         INSERT INTO FATURAMENTOROP (NRFILIALROP, NRFATURAMENTOROP, CDFILIAL, DTENTRSAID, DTREFERENC,
                                     CDFATUDIVER, CDTIPOOPER, IDENTRSAIDOP, CDCLIENTE, DSFATURAMENTO,
                                     CDFILIALLANC, NRLANCTONF, NRNOTAFISC, CDSERIE, NRNOTAFISCORIG,
                                     CDSERIEORIG, CDSERVFILI, NRRECEXTRAFILI, IDTPLANCROP, CDFILIALCENT,
                                     IDSTATUS, DTINIREFEFAT, DTFINREFEFAT, VRFATURAMENTO, QTFATURAMENTO)
              VALUES (P_NRROPFILIAL, V_NRSEQFATUROP, P_CDFILIAL, C_VOLUME.DTRECEXTRAFILI, C_VOLUME.DTRECEXTRAFILI,
                      NULL, NULL, NULL, NULL, NULL,
                      C_VOLUME.CDFILIAL, NULL, C_VOLUME.CDRECEXTRA, NULL, NULL,
                      NULL, NULL, C_VOLUME.NRRECEXTRAFILI, 'RECEITA_EXTRA', V_CDFILCENTPME,
                      NULL, NULL, NULL, C_VOLUME.VRRECEXTRAFILI, 1);
      END LOOP;
   END IF;

   IF V_IDGERROPFOS  = 'S' THEN
      /* FOR C_REG IN (SELECT A.DTREDUCAOZ as DTENTRVENDA, RNF.NRNUMERNF, NVL(SUM(VRBRUTO),0) AS VRBRUTO,
                           NVL(SUM(VRTOTCANREDZ),0) AS VENDASCANC, NVL(SUM(VRTOTDESREDZ),0) AS VRDESCONTOS,
                           NVL(SUM(VRCONTABIL),0) AS VRCONTABIL
                      FROM (SELECT DTREDUCAOZ, SUM(VRVENBRUREDZ) AS VRBRUTO,
                                   SUM(VRTOTCANREDZ) AS VRTOTCANREDZ, SUM(VRTOTDESREDZ) AS VRTOTDESREDZ,
                                   SUM(VRVENLIQREDZ) AS VRCONTABIL
                              FROM REDUCAOZ R
                             WHERE R.CDFILIAL = P_CDFILIAL
                               AND TRUNC(R.DTREDUCAOZ) = P_DTINICIAL
                             GROUP BY DTREDUCAOZ) A LEFT JOIN (SELECT DTNUMERNF, MAX(NRNUMERNF) AS NRNUMERNF
                                                                 FROM NUMERORNF
                                                                WHERE CDFILIAL = P_CDFILIAL
                                                                  AND TRUNC(DTNUMERNF) = P_DTINICIAL
                                                                  AND IDENTRSAID = 'S'
                                                                GROUP BY DTNUMERNF) RNF ON A.DTREDUCAOZ = RNF.DTNUMERNF
                    GROUP BY A.DTREDUCAOZ, RNF.NRNUMERNF
                    ORDER BY A.DTREDUCAOZ) LOOP
      */   
      FOR C_REG IN (SELECT DTENTRVENDA, NRNUMERNF, SUM(VRVENDBRUT) VRVENDAS, SUM(VRACRESCIMO) VRACRESCIMO, SUM(VRDESCONTO) VRDESCONTOS
                      FROM (SELECT V.CDFILIAL, V.DTENTRVENDA,V.CDLOJA,V.CDCAIXA,
                                   SUM (TRUNC(I.QTPRODVEND *(NVL(I.VRUNITVEND,0) + NVL(I.VRUNITVENDCL,0)),2)) AS VRVENDBRUT,
                                   DECODE(V.IDSITUVENDA,'O',SUM (NVL(I.VRACRITVEND,0)),0) AS VRACRESCIMO,
                                   DECODE(V.IDSITUVENDA,'O',SUM (NVL(I.VRDESITVEND,0)),0) AS VRDESCONTO,
                                   SUM (I.QTPRODVEND) AS QTVENDIDA,
                                   0 AS NRNUMERNF
                              FROM VENDA V INNER JOIN ITEMVENDA I ON I.CDFILIAL = V.CDFILIAL
                                                                 AND I.CDCAIXA = V.CDCAIXA
                                                                 AND I.NRSEQVENDA = V.NRSEQVENDA
                                           INNER JOIN CAIXA CX ON V.CDFILIAL = CX.CDFILIAL
                                                              AND V.CDCAIXA = CX.CDCAIXA
                                                              AND V.CDLOJA = CX.CDLOJA
                             WHERE V.CDFILIAL = P_CDFILIAL
                               AND TRUNC(V.DTENTRVENDA) = P_DTINICIAL
                             GROUP BY V.CDFILIAL,V.DTENTRVENDA,V.IDSITUVENDA ,V.CDLOJA,V.CDCAIXA)
                      GROUP BY CDFILIAL, DTENTRVENDA, NRNUMERNF) LOOP

         /*Valor de itens cancelados (Pode haver vendas concluidas,porem com itens cancelados)*/
         BEGIN
            SELECT NVL(SUM(VRITCANCEL),0) VRVENDBRUT
              INTO V_ITVENDACAN
              FROM (SELECT V.CDFILIAL, V.DTENTRVENDA, V.CDLOJA, V.CDCAIXA,
                           SUM(TRUNC(C.QTPRODVENDC * (NVL(C.VRUNITVENDC,0) + NVL(C.VRUNITVENCLC,0)),2)) AS VRITCANCEL
                      FROM VENDA V INNER JOIN ITVENDACAN C ON C.CDFILIAL = V.CDFILIAL
                                                          AND C.CDCAIXA = V.CDCAIXA
                                                          AND C.NRSEQVENDA = V.NRSEQVENDA
                                   INNER JOIN CAIXA CX ON V.CDFILIAL = CX.CDFILIAL
                                                      AND V.CDCAIXA = CX.CDCAIXA
                                                      AND V.CDLOJA = CX.CDLOJA
                     WHERE V.CDFILIAL = P_CDFILIAL
                       AND V.DTENTRVENDA = C_REG.DTENTRVENDA
                     GROUP BY V.CDFILIAL,V.DTENTRVENDA,V.IDSITUVENDA,V.CDLOJA,V.CDCAIXA);
         EXCEPTION
           WHEN NO_DATA_FOUND THEN
              V_ITVENDACAN := 0;
         END;

         /*Valor de vendas canceladas*/
         BEGIN
            SELECT NVL(SUM(VRITCANCEL),0)
              INTO V_VRVENDACANCEL
              FROM (SELECT V.CDFILIAL, V.DTENTRVENDA, V.CDLOJA, V.CDCAIXA,
                           SUM (TRUNC(I.QTPRODVEND *(NVL(I.VRUNITVEND,0) + NVL(NVL(I.VRUNITVENDCL,0),0)),2))AS VRITCANCEL,
                           SUM (I.QTPRODVEND) AS QTVENDIDA
                      FROM VENDA V INNER JOIN ITEMVENDA I ON I.CDFILIAL = V.CDFILIAL
                                                         AND I.CDCAIXA = V.CDCAIXA
                                                         AND I.NRSEQVENDA = V.NRSEQVENDA
                                   INNER JOIN CAIXA CX ON V.CDFILIAL = CX.CDFILIAL
                                                      AND V.CDCAIXA = CX.CDCAIXA
                                                      AND V.CDLOJA = CX.CDLOJA
                     WHERE V.CDFILIAL = P_CDFILIAL
                       AND V.DTENTRVENDA = C_REG.DTENTRVENDA
                       AND V.IDSITUVENDA = 'C'
                     GROUP BY V.CDFILIAL,V.DTENTRVENDA,V.IDSITUVENDA ,V.CDLOJA,V.CDCAIXA);
         EXCEPTION
           WHEN NO_DATA_FOUND THEN
              V_VRVENDACANCEL := 0;
         END;

         V_VENDABRUTA     := C_REG.VRVENDAS + V_ITVENDACAN;
         V_VRCANCELAMENTO := V_VRVENDACANCEL + V_ITVENDACAN;
         V_VRCONTABIL     := V_VENDABRUTA - C_REG.VRDESCONTOS - V_VRCANCELAMENTO;
                    
         IF /* C_REG.VRBRUTO */ V_VENDABRUTA > 0 THEN
            NOVO_CODIGO('GESFATURAMENTOROP', V_NRSEQFATUROP);

            INSERT INTO FATURAMENTOROP (NRFILIALROP, NRFATURAMENTOROP, CDFILIAL, DTENTRSAID, DTREFERENC,
                                        CDFATUDIVER, CDTIPOOPER, IDENTRSAIDOP, CDCLIENTE, DSFATURAMENTO,
                                        CDFILIALLANC, NRLANCTONF, NRNOTAFISC, CDSERIE, NRNOTAFISCORIG,
                                        CDSERIEORIG, CDSERVFILI, NRRECEXTRAFILI, IDTPLANCROP, CDFILIALCENT,
                                        IDSTATUS, DTINIREFEFAT, DTFINREFEFAT, VRFATURAMENTO, QTFATURAMENTO)
                 VALUES (P_NRROPFILIAL, V_NRSEQFATUROP, P_CDFILIAL, C_REG.DTENTRVENDA, C_REG.DTENTRVENDA,
                         NULL, NULL, NULL, NULL, NULL,
                         P_CDFILIAL, NULL, C_REG.NRNUMERNF, NULL, NULL,
                         NULL, NULL, NULL, 'FAT_FORSALE', V_CDFILCENTPME,
                         'B', NULL, NULL, /* C_REG.VRBRUTO */ V_VENDABRUTA, 1);
         END IF;

         IF /* C_REG.VENDASCANC */ V_VRCANCELAMENTO > 0 THEN
            NOVO_CODIGO('GESFATURAMENTOROP', V_NRSEQFATUROP);

            INSERT INTO FATURAMENTOROP (NRFILIALROP, NRFATURAMENTOROP, CDFILIAL, DTENTRSAID, DTREFERENC,
                                        CDFATUDIVER, CDTIPOOPER, IDENTRSAIDOP, CDCLIENTE, DSFATURAMENTO,
                                        CDFILIALLANC, NRLANCTONF, NRNOTAFISC, CDSERIE, NRNOTAFISCORIG,
                                        CDSERIEORIG, CDSERVFILI, NRRECEXTRAFILI, IDTPLANCROP, CDFILIALCENT,
                                        IDSTATUS, DTINIREFEFAT, DTFINREFEFAT, VRFATURAMENTO, QTFATURAMENTO)
                 VALUES (P_NRROPFILIAL, V_NRSEQFATUROP, P_CDFILIAL, C_REG.DTENTRVENDA, C_REG.DTENTRVENDA,
                         NULL, NULL, NULL, NULL, NULL,
                         P_CDFILIAL, NULL, C_REG.NRNUMERNF, NULL, NULL,
                         NULL, NULL, NULL, 'FAT_FORSALE', V_CDFILCENTPME,
                         'V', NULL, NULL, /* C_REG.VENDASCANC */ V_VRCANCELAMENTO, 1);
         END IF;

         IF C_REG.VRDESCONTOS > 0 THEN
            NOVO_CODIGO('GESFATURAMENTOROP', V_NRSEQFATUROP);

            INSERT INTO FATURAMENTOROP (NRFILIALROP, NRFATURAMENTOROP, CDFILIAL, DTENTRSAID, DTREFERENC,
                                        CDFATUDIVER, CDTIPOOPER, IDENTRSAIDOP, CDCLIENTE, DSFATURAMENTO,
                                        CDFILIALLANC, NRLANCTONF, NRNOTAFISC, CDSERIE, NRNOTAFISCORIG,
                                        CDSERIEORIG, CDSERVFILI, NRRECEXTRAFILI, IDTPLANCROP, CDFILIALCENT,
                                        IDSTATUS, DTINIREFEFAT, DTFINREFEFAT, VRFATURAMENTO, QTFATURAMENTO)
                 VALUES (P_NRROPFILIAL, V_NRSEQFATUROP, P_CDFILIAL, C_REG.DTENTRVENDA, C_REG.DTENTRVENDA,
                         NULL, NULL, NULL, NULL, NULL,
                         P_CDFILIAL, NULL, C_REG.NRNUMERNF, NULL, NULL,
                         NULL, NULL, NULL, 'FAT_FORSALE', V_CDFILCENTPME,
                         'D', NULL, NULL, C_REG.VRDESCONTOS, 1);
         END IF;

         IF /* C_REG.VRCONTABIL */ V_VRCONTABIL > 0 THEN
            NOVO_CODIGO('GESFATURAMENTOROP', V_NRSEQFATUROP);

            INSERT INTO FATURAMENTOROP (NRFILIALROP, NRFATURAMENTOROP, CDFILIAL, DTENTRSAID, DTREFERENC,
                                        CDFATUDIVER, CDTIPOOPER, IDENTRSAIDOP, CDCLIENTE, DSFATURAMENTO,
                                        CDFILIALLANC, NRLANCTONF, NRNOTAFISC, CDSERIE, NRNOTAFISCORIG,
                                        CDSERIEORIG, CDSERVFILI, NRRECEXTRAFILI, IDTPLANCROP, CDFILIALCENT,
                                        IDSTATUS, DTINIREFEFAT, DTFINREFEFAT, VRFATURAMENTO, QTFATURAMENTO)
                 VALUES (P_NRROPFILIAL, V_NRSEQFATUROP, P_CDFILIAL, C_REG.DTENTRVENDA, C_REG.DTENTRVENDA,
                         NULL, NULL, NULL, NULL, NULL,
                         P_CDFILIAL, NULL, C_REG.NRNUMERNF, NULL, NULL,
                         NULL, NULL, NULL, 'FAT_FORSALE', V_CDFILCENTPME,
                         'C', NULL, NULL, /* C_REG.VRCONTABIL */ V_VRCONTABIL, 1);
         END IF;
      END LOOP;
   END IF;

   IF V_IDCONSFATUDIV = 'S' THEN
      FOR C_FATUDIV IN (SELECT L.CDFILIAL, L.CDFATUDIVER, L.DTLANCFATDIV, L.VRLANCFATDIV
                          FROM LANCFATUDIVE L
                         WHERE L.CDFILIAL = P_CDFILIAL
                           AND TRUNC(L.DTLANCFATDIV) = P_DTINICIAL
                         ORDER BY L.DTLANCFATDIV, L.CDFATUDIVER) LOOP                          

         NOVO_CODIGO('GESFATURAMENTOROP', V_NRSEQFATUROP);

         INSERT INTO FATURAMENTOROP (NRFILIALROP, NRFATURAMENTOROP, CDFILIAL, DTENTRSAID, DTREFERENC,
                                     CDFATUDIVER, CDTIPOOPER, IDENTRSAIDOP, CDCLIENTE, DSFATURAMENTO,
                                     CDFILIALLANC, NRLANCTONF, NRNOTAFISC, CDSERIE, NRNOTAFISCORIG,
                                     CDSERIEORIG, CDSERVFILI, NRRECEXTRAFILI, IDTPLANCROP, CDFILIALCENT,
                                     IDSTATUS, DTINIREFEFAT, DTFINREFEFAT, VRFATURAMENTO, QTFATURAMENTO)
              VALUES (P_NRROPFILIAL, V_NRSEQFATUROP, P_CDFILIAL, C_FATUDIV.DTLANCFATDIV, C_FATUDIV.DTLANCFATDIV,
                      C_FATUDIV.CDFATUDIVER, NULL, NULL, NULL, NULL,
                      C_FATUDIV.CDFILIAL, NULL, NULL, NULL, NULL,
                      NULL, NULL, NULL, 'FAT_DIVERSOS', V_CDFILCENTPME,
                      NULL, NULL, NULL, C_FATUDIV.VRLANCFATDIV, 1);
      END LOOP;
   END IF;
END;