/*

***********************************************************************
****    Procedure para calculo do demonstrativo de resultado operacional
*************************************************************************

Criado em  : 03/2018
       Por : Jefferson Gouvea

Descri��o:

          - Com base nos dados parametrizados para a unidade realiza apura��o do
            resultado operacional da mesma, grava em tabelas espec�ficas a fim de
            possibilitar a gera��o de relat�rios gerenciais a partir de dados
            previamente calculados.

*/

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,            
   P_PRIMEEXEC      IN VARCHAR DEFAULT 'N')
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;
   V_VRTOTIMPOSTO      FLOAT;
   V_IDPERFIXIMPDRF    VARCHAR2(01);
   V_VRPISECF          FLOAT;
   V_VRCOFINSECF       FLOAT;
   V_VRPIS             FLOAT;
   V_VRCOFINS          FLOAT;
   V_EXISTCLIENFILIAL  VARCHAR2(04);
   V_IDTPAPURACAO      VARCHAR2(01);
   V_NRTIPOCUSTO       NUMBER;
   V_QTDECUPONS        NUMBER;
   V_QTPESSOAS         NUMBER;
BEGIN
   BEGIN
      SELECT IDFATUROP, IDPERFIXCALCIMP, IDSERVFATROP, IDDTAPURFAT, IDGERROPFOS,
             IDCONSFATUDIV, IDPROJECMANU, IDVOLUNEGOCIO, IDCONSNFEAUTORIZA, IDCOMPARANFEFET,
             IDPERFIXIMPDRF, IDTPAPURACAO
        INTO V_IDFATUROP, V_IDPERFIXCALCIMP, V_IDSERVFATROP, V_IDDTAPURFAT, V_IDGERROPFOS,
             V_IDCONSFATUDIV, V_IDPROJECMANU, V_IDVOLUNEGOCIO, V_IDCONSNFEAUTORIZA, V_IDCOMPARANFEFET,
             V_IDPERFIXIMPDRF, V_IDTPAPURACAO
        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;
       V_IDPERFIXIMPDRF := NULL;    V_IDTPAPURACAO := 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') AND (NVL(V_IDTPAPURACAO,'3') <> '2') THEN
      FOR C_FATURANF IN (SELECT N.CDCLIENTE, N.CDTIPOOPER, N.NRNOTAFISC, N.CDFILIAL,
                                N.NRLANCTONF, CASE WHEN V_IDDTAPURFAT = 'E'
                                                   THEN N.DTEMISSAO
                                                   ELSE N.DTENTRSAID
                                              END 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 DISTINCT CASE P.IDDATADEVOLROP WHEN 'SAIDA_NF_ORIGEM' THEN TRUNC(ORIG.DTENTRSAID)
                                                                WHEN 'REFE_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.*/ NF.NRNOTAFISC NRNOTAFISCORIG,
                                 /* ORIG.*/ NF.CDSERIE CDSERIEORIG, NF.IDSTATUS, NF.DTINIREFEFAT, NF.DTFINREFEFAT,
                                 NF.VRNOTAFISC, /*ORIG.*/ NF.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)
                               OR (NVL(P.IDDATADEVOLROP,'SAIDA_NF') = 'REFE_NF_ORIGEM' AND TRUNC(ORIG.DTFINREFEFAT) = 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') AND (P_PRIMEEXEC = 'S')) AND (NVL(V_IDTPAPURACAO,'3') <> '2') THEN
      SELECT LPAD(ABS(ROUND(DBMS_RANDOM.VALUE(9999,9999999999))),10,'0')
        INTO V_NRAPURREL
        FROM DUAL;
        
      APURAC_RELGESDIRETA(P_CDFILIAL, P_DTINICIAL, P_DTFINAL, 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) BETWEEN P_DTINICIAL AND P_DTFINAL
                            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) BETWEEN P_DTINICIAL AND P_DTFINAL
                            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_DTFINAL, 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, C_EFET_PME.DTEFETCLPME, 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) BETWEEN P_DTINICIAL AND P_DTFINAL
                             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) BETWEEN P_DTINICIAL AND P_DTFINAL
                             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, C_EFET_HOSP.DTEFETDIET, 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_DTFINAL, 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) BETWEEN P_DTINICIAL AND P_DTFINAL
                            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) BETWEEN P_DTINICIAL AND P_DTFINAL
                             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) BETWEEN P_DTINICIAL AND P_DTFINAL
                             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) BETWEEN P_DTINICIAL AND P_DTFINAL
                             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, C_EFETIVOS.DTAPURACFATGD, 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_DTFINAL, 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) BETWEEN P_DTINICIAL AND P_DTFINAL;
   END IF;

   IF (V_IDVOLUNEGOCIO = 'S') AND (NVL(V_IDTPAPURACAO,'3') <> '2') 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') OR (NVL(V_IDTPAPURACAO,'3') <> '1') THEN
      BEGIN
         SELECT TO_NUMBER(NVL(CDTIPCUSTEST,'3'))
           INTO V_NRTIPOCUSTO
           FROM PARAMFILIAL P
          WHERE P.CDFILIAL = P_CDFILIAL;
      EXCEPTION
         WHEN NO_DATA_FOUND THEN
            V_NRTIPOCUSTO := '3';
      END;
   
      FOR C_VENDAS IN(SELECT V.CDFILIAL,V.CDLOJA, V.DTENTRVENDA,
                             SUM(TRUNC(I.QTPRODVEND * (I.VRUNITVEND + NVL(I.VRUNITVENDCL,0)),2) + NVL(I.VRACRITVEND,0) - NVL(I.VRDESITVEND,0)) AS VALORTOTAL,
                             SUM(NVL(I.VRACRITVEND,0)) AS VRACRITVEND,
                             SUM(NVL(I.VRDESITVEND,0)) AS VRDESITVEND,
                             SUM(I.QTPRODVEND) AS QTVENDIDA,
                             SUM(CUSTO_COMPOSICAO(I.CDPRODUTO,V.CDFILIAL,V.CDFILIAL,V_NRTIPOCUSTO,I.QTPRODVEND,'',V.DTENTRVENDA)) AS VALORTOTAL_CUSTO,
                             SUM((TRUNC(I.QTPRODVEND * (I.VRUNITVEND + NVL(I.VRUNITVENDCL,0)),2) + NVL(I.VRACRITVEND,0) - NVL(I.VRDESITVEND,0)) * P.VRPEALPRODIT / 100) AS VRIMPOSTO,
                             0 NRNUMERNF
					    FROM VENDA V, ITEMVENDA I, ITVENDAIMPOS P
					   WHERE V.CDFILIAL = P_CDFILIAL
						 AND TRUNC(V.DTENTRVENDA) = P_DTINICIAL
					     AND V.IDSITUVENDA = 'O'
					     AND I.CDFILIAL = V.CDFILIAL
					     AND I.CDCAIXA = V.CDCAIXA
					     AND I.NRSEQVENDA = V.NRSEQVENDA
					     AND I.CDFILIAL = P.CDFILIAL
                         AND I.CDCAIXA = P.CDCAIXA
                         AND I.NRSEQVENDA = P.NRSEQVENDA
                         AND I.NRSEQUITVEND = P.NRSEQUITVEND
                       GROUP BY V.CDFILIAL, V.CDLOJA, V.DTENTRVENDA) LOOP

         /* CALCULA A QUANTIDADE DE CUPONS PARA A FILIAL,LOJA E DATA */
         SELECT COUNT(1) 
           INTO V_QTDECUPONS
           FROM VENDA V
          WHERE V.CDFILIAL    = C_VENDAS.CDFILIAL
            AND V.DTENTRVENDA = C_VENDAS.DTENTRVENDA
            AND V.CDLOJA      = C_VENDAS.CDLOJA
            AND V.IDSITUVENDA = 'O';

         /* CALCULA A QUANTIDADE DE PESSOAS PARA A FILIAL,LOJA E DATA */
         SELECT NVL(SUM(V.NRPESMESAVENDA),1) 
           INTO V_QTPESSOAS
           FROM VENDA V
          WHERE V.CDFILIAL    = C_VENDAS.CDFILIAL
            AND V.DTENTRVENDA = C_VENDAS.DTENTRVENDA
            AND V.CDLOJA      = C_VENDAS.CDLOJA
            AND V.IDSITUVENDA = 'O';
			
		 /* CALCULA O VALOR DOS IMPOSTOS PIS E COFINS PARA VENDAS SAT e NFC-e */
		 SELECT SUM(NVL(P.VRIMPPIS,0)), SUM(NVL(P.VRIMPCOFINS,0)) INTO V_VRPIS, V_VRCOFINS
		   FROM VENDA V, ITVENDAIMPOS P
		  WHERE V.CDFILIAL    = C_VENDAS.CDFILIAL
		    AND V.DTENTRVENDA = C_VENDAS.DTENTRVENDA
			AND V.CDLOJA      = C_VENDAS.CDLOJA
			AND V.IDSITUVENDA = 'O'
			AND V.IDTPEMISVEND IN ('S','N')
		    AND V.CDFILIAL    = P.CDFILIAL
			AND V.CDCAIXA     = P.CDCAIXA
			AND V.NRSEQVENDA  = P.NRSEQVENDA;
			
		 IF (V_IDPERFIXIMPDRF = 'S') THEN
			V_VRTOTIMPOSTO := 0;
		 ELSE
			V_VRTOTIMPOSTO := C_VENDAS.VRIMPOSTO + NVL(V_VRPIS,0) + NVL(V_VRCOFINS,0);
		 END IF;

         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,
                                     CDLOJA, VRACRITVEND, VRDESITVEND, QTPESSOAS,
                                     QTDECUPONS, VRTOTIMPOSTO, CUSTOVENDA)
              VALUES (P_NRROPFILIAL, V_NRSEQFATUROP, P_CDFILIAL, C_VENDAS.DTENTRVENDA, C_VENDAS.DTENTRVENDA,
                      NULL, NULL, NULL, NULL, NULL,
                      P_CDFILIAL, NULL, C_VENDAS.NRNUMERNF, NULL, NULL,
                      NULL, NULL, NULL, 'FAT_FORSALE', V_CDFILCENTPME,
                      'B', NULL, NULL, C_VENDAS.VALORTOTAL, C_VENDAS.QTVENDIDA,
                      C_VENDAS.CDLOJA, C_VENDAS.VRACRITVEND, C_VENDAS.VRDESITVEND, V_QTPESSOAS,
                      V_QTDECUPONS, V_VRTOTIMPOSTO, C_VENDAS.VALORTOTAL_CUSTO);
			 
		 /* CONSIDERA PERCENTUAL FIXO PARA CÁLCULO DO IMPOSTO */		 
		 IF (V_IDPERFIXIMPDRF = 'S') THEN
			FOR C_IMPOSTO IN(SELECT IR.CDIMPOSTO, IR.VRPERFIXCALCIMP, (C_VENDAS.VALORTOTAL * NVL(IR.VRPERFIXCALCIMP,0)) / 100 AS VRIMPOSTO, I.NMIMPOSTO 
                               FROM IMPOSTOROP IR, IMPOSTO I
                              WHERE IR.CDFILIAL = P_FILIALPAR 
                                AND IR.IDTPIMPOSTOROP = 'FN'
                                AND IR.CDIMPOSTO = I.CDIMPOSTO
                              ORDER BY IR.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,
                           CDLOJA)
                    VALUES
                          (P_NRROPFILIAL, V_NRSEQDEDUROP, P_CDFILIAL, P_DTINICIAL, NULL,
                           C_IMPOSTO.CDIMPOSTO, NULL, C_IMPOSTO.NMIMPOSTO, NULL, NULL,
                           NULL, NULL, NULL, NULL, NULL,
                           'FAT_FORSALE', V_CDFILCENTPME, C_IMPOSTO.VRIMPOSTO, C_IMPOSTO.VRPERFIXCALCIMP,
                           C_VENDAS.CDLOJA);
			   
            END LOOP;
		 END IF;
      END LOOP;
      
      FOR C_VENDAS_CANC IN(SELECT VE.CDFILIAL,VE.CDLOJA, VE.DTENTRVENDA,
                                  SUM(TRUNC(IC.QTPRODVENDC * (IC.VRUNITVENDC + NVL(IC.VRUNITVENCLC,0)),2) + NVL(IC.VRACRITVENDC,0) - NVL(IC.VRDESITVENDC,0)) AS VALORTOTAL,
                                  SUM(NVL(IC.VRACRITVENDC,0)) AS VRACRITVEND,
                                  SUM(NVL(IC.VRDESITVENDC,0)) AS VRDESITVEND,
                                  SUM(IC.QTPRODVENDC) AS QTVENDIDA, NULL NRNUMERNF
                             FROM VENDA VE, ITVENDACAN IC
                            WHERE VE.CDFILIAL    = P_CDFILIAL
                              AND VE.DTENTRVENDA = P_DTINICIAL
                              AND VE.CDFILIAL    = IC.CDFILIAL
                              AND VE.CDCAIXA     = IC.CDCAIXA
                              AND VE.NRSEQVENDA  = IC.NRSEQVENDA
                            GROUP BY VE.CDFILIAL, VE.CDLOJA, VE.DTENTRVENDA) 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,
                                     CDLOJA, VRACRITVEND, VRDESITVEND)
              VALUES (P_NRROPFILIAL, V_NRSEQFATUROP, P_CDFILIAL, C_VENDAS_CANC.DTENTRVENDA, C_VENDAS_CANC.DTENTRVENDA,
                      NULL, NULL, NULL, NULL, NULL,
                      P_CDFILIAL, NULL, C_VENDAS_CANC.NRNUMERNF, NULL, NULL,
                      NULL, NULL, NULL, 'FAT_FORSALE', V_CDFILCENTPME,
                      'I', NULL, NULL, C_VENDAS_CANC.VALORTOTAL, C_VENDAS_CANC.QTVENDIDA,
                      C_VENDAS_CANC.CDLOJA, C_VENDAS_CANC.VRACRITVEND, C_VENDAS_CANC.VRDESITVEND);
      END LOOP;
      
      FOR C_VENDAS_CANC IN(SELECT V.CDFILIAL,V.CDLOJA, V.DTENTRVENDA,
                                  SUM(TRUNC(I.QTPRODVEND * (I.VRUNITVEND + NVL(I.VRUNITVENDCL,0)),2) + NVL(I.VRACRITVEND,0) - NVL(I.VRDESITVEND,0)) AS VALORTOTAL,
                                  SUM(NVL(I.VRACRITVEND,0)) AS VRACRITVEND,
                                  SUM(CASE WHEN I.VRDESITVEND = 0 THEN NULL ELSE I.VRDESITVEND END) AS VRDESITVEND,
                                  SUM(I.QTPRODVEND) AS QTVENDIDA,
                                  COUNT(DISTINCT V.CDFILIAL||V.CDCAIXA||V.NRSEQVENDA) AS QTCUPONS,
                                  NULL NRNUMERNF
                             FROM VENDA V, ITEMVENDA I
                            WHERE V.CDFILIAL    = P_CDFILIAL
                              AND V.DTENTRVENDA = P_DTINICIAL
                              AND V.CDFILIAL    = I.CDFILIAL(+)
                              AND V.CDCAIXA     = I.CDCAIXA(+)
                              AND V.NRSEQVENDA  = I.NRSEQVENDA(+)
                              AND V.IDSITUVENDA = 'C'
                            GROUP BY V.CDFILIAL, V.CDLOJA, V.DTENTRVENDA) 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,
                                     CDLOJA, VRACRITVEND, VRDESITVEND, QTDECUPONS)
              VALUES (P_NRROPFILIAL, V_NRSEQFATUROP, P_CDFILIAL, C_VENDAS_CANC.DTENTRVENDA, C_VENDAS_CANC.DTENTRVENDA,
                      NULL, NULL, NULL, NULL, NULL,
                      P_CDFILIAL, NULL, C_VENDAS_CANC.NRNUMERNF, NULL, NULL,
                      NULL, NULL, NULL, 'FAT_FORSALE', V_CDFILCENTPME,
                      'V', NULL, NULL, C_VENDAS_CANC.VALORTOTAL, C_VENDAS_CANC.QTVENDIDA,
                      C_VENDAS_CANC.CDLOJA, C_VENDAS_CANC.VRACRITVEND, C_VENDAS_CANC.VRDESITVEND,
                      C_VENDAS_CANC.QTCUPONS);
      END LOOP;
   END IF;

   IF (V_IDCONSFATUDIV = 'S') AND (NVL(V_IDTPAPURACAO,'3') <> '2') 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;
   
   IF (NVL(V_IDTPAPURACAO,'3') <> '1') THEN
      FOR C_SANGRIA IN(SELECT M.CDFILIAL,C.CDLOJA,M.DTMOVIMCAIXA AS DATAREF,
                              SUM(VRMOVIVEND) AS VALOR_SANGRIA
                         FROM MOVCAIXA M, CAIXA C
                        WHERE M.CDFILIAL    = P_CDFILIAL
                          AND M.DTMOVIMCAIXA = P_DTINICIAL
                          AND M.IDTIPOMOVIVE = 'G'
                          AND M.CDFILIAL = C.CDFILIAL
                          AND M.CDCAIXA  = C.CDCAIXA
                        GROUP BY M.CDFILIAL, C.CDLOJA, M.DTMOVIMCAIXA
                        ORDER BY M.DTMOVIMCAIXA) 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,
                                     CDLOJA)
              VALUES (P_NRROPFILIAL, V_NRSEQFATUROP, P_CDFILIAL, C_SANGRIA.DATAREF, C_SANGRIA.DATAREF,
                      NULL, NULL, NULL, NULL, NULL,
                      P_CDFILIAL, NULL, NULL, NULL, NULL,
                      NULL, NULL, NULL, 'FAT_SANGRIA', V_CDFILCENTPME,
                      NULL, NULL, NULL, C_SANGRIA.VALOR_SANGRIA, 1,
                      C_SANGRIA.CDLOJA);

      END LOOP;
      
      FOR C_CONSTRAVALE IN(SELECT M.CDFILIAL,C.CDLOJA,M.DTMOVIMCAIXA AS DATAREF,
                                  SUM(VRMOVIVEND) AS VALOR_CONTRAVALE
                             FROM MOVCAIXA M, CAIXA C, TIPORECE T
                            WHERE M.CDFILIAL    = P_CDFILIAL
                              AND M.DTMOVIMCAIXA = P_DTINICIAL
                              AND M.IDTIPOMOVIVE = 'S'
                              AND M.CDTIPORECE = T.CDTIPORECE
                              AND T.IDTIPORECE = '7'
                              AND M.CDFILIAL = C.CDFILIAL
                              AND M.CDCAIXA  = C.CDCAIXA
                            GROUP BY M.CDFILIAL,C.CDLOJA,M.DTMOVIMCAIXA
                            ORDER BY M.DTMOVIMCAIXA) LOOP

         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,
                                     CDLOJA)
              VALUES (P_NRROPFILIAL, V_NRSEQFATUROP, P_CDFILIAL, C_CONSTRAVALE.DATAREF, C_CONSTRAVALE.DATAREF,
                      NULL, NULL, NULL, NULL, NULL,
                      P_CDFILIAL, NULL, NULL, NULL, NULL,
                      NULL, NULL, NULL, 'FAT_CONTRAVALE', V_CDFILCENTPME,
                      NULL, NULL, NULL, C_CONSTRAVALE.VALOR_CONTRAVALE, 1,
                      C_CONSTRAVALE.CDLOJA);
      END LOOP;
      
      FOR C_SUPRIMENTOS IN(SELECT M.CDFILIAL,C.CDLOJA,M.DTMOVIMCAIXA AS DATAREF,
                                  SUM(VRMOVIVEND) AS VALOR_SUPRIMENTOS
                             FROM MOVCAIXA M, CAIXA C, TIPORECE T
                            WHERE M.CDFILIAL    = P_CDFILIAL
                              AND M.DTMOVIMCAIXA = P_DTINICIAL
                              AND M.IDTIPOMOVIVE = 'S'
                              AND M.CDTIPORECE = T.CDTIPORECE
                              AND T.IDTIPORECE = '7'
                              AND M.CDFILIAL = C.CDFILIAL
                              AND M.CDCAIXA  = C.CDCAIXA
                            GROUP BY M.CDFILIAL,C.CDLOJA,M.DTMOVIMCAIXA
                            ORDER BY M.DTMOVIMCAIXA) LOOP

         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,
                                     CDLOJA)
              VALUES (P_NRROPFILIAL, V_NRSEQFATUROP, P_CDFILIAL, C_SUPRIMENTOS.DATAREF, C_SUPRIMENTOS.DATAREF,
                      NULL, NULL, NULL, NULL, NULL,
                      P_CDFILIAL, NULL, NULL, NULL, NULL,
                      NULL, NULL, NULL, 'FAT_SUPRIMENTOS', V_CDFILCENTPME,
                      NULL, NULL, NULL, C_SUPRIMENTOS.VALOR_SUPRIMENTOS, 1,
                      C_SUPRIMENTOS.CDLOJA);
      END LOOP;
   END IF;
END;
/

-- VERSAO --
BEGIN ATUVEROBJ('APURAC_FATURAMENTOROP','6.35.xxx'); END;
/
