CREATE OR REPLACE PROCEDURE CONTABILIZA_FOS( 
   P_CDFILIAL      IN FILIAL.CDFILIAL%TYPE,
   P_NRLANCTONF    IN NOTAFISCAL.NRLANCTONF%TYPE,
   P_CDFILIALFIS   IN FILIAL.CDFILIAL%TYPE,
   P_NRLANCTONFFIS IN NOTAFISCAL.NRLANCTONF%TYPE,
   P_CDFILIALVND   IN FILIAL.CDFILIAL%TYPE,
   P_CDCAIXA       IN CAIXA.CDCAIXA%TYPE,
   P_DTREDUCAOZ    IN REDUCAOZ.DTREDUCAOZ%TYPE,
   P_RETORNO       OUT NOCOPY VARCHAR2,
   P_ERROS         OUT NOCOPY VARCHAR2,
   P_CDLOJA        IN CAIXA.CDLOJA%TYPE DEFAULT NULL)
IS
  V_CDHISTPADR      VARCHAR2(4);
  V_DSHISTPADR      VARCHAR2(1000);
  V_SEQUENCIAL      VARCHAR2(20);
  V_CDFILIAL        VARCHAR2(4);
  V_NRLANCTONF      VARCHAR2(10);
  V_STCONTCTBL      VARCHAR2(20);
  V_STCONTCTBLREC   VARCHAR2(20);
  V_LANCCONT        VARCHAR2(20);
  V_NRSEQLANC       VARCHAR2(05);
  V_IDCONTCCUS      VARCHAR2(01);
  V_CDCENTCUST      VARCHAR2(04);
  V_CDLOTE          VARCHAR2(20);
  V_NRLANCLOTE      VARCHAR2(6);
  V_GERALOTE        VARCHAR2(1);
  V_CDEMPRESA       VARCHAR2(02);
  V_DTENTRSAID      DATE;
  V_GERALOTEFIS     VARCHAR2(1);
  V_CDTPCTRECEB     VARCHAR2(06);
  V_ERROS           VARCHAR2(5000);
  V_RETORNO         BOOLEAN;
  V_IDCODCOME       VARCHAR2(1);
  V_CODCOME         VARCHAR2(30);
  V_STCONTCTBLTPREC VARCHAR2(20);
  V_IDCODCOMETPREC  VARCHAR2(1);
  V_CODCOMETPREC    VARCHAR2(30);
  V_STCONTCTBLPROD  VARCHAR2(20);
  V_IDCODCOMEPROD   VARCHAR2(1);
  V_CODCOMEPROD     VARCHAR2(30);
  V_VRAJUSTE        FLOAT;
  V_VRPARTIDA       FLOAT;
  V_CDCONTAAJUSTE VARCHAR2(20);
 BEGIN
   EXCLUI_LANCCONTABIL('12',P_CDFILIAL||P_NRLANCTONF);
   EXCLUI_LANCCONTABIL('12',P_CDFILIALFIS||P_NRLANCTONFFIS);

   V_GERALOTE    := 'S';
   V_GERALOTEFIS := 'S';

   FOR C_REC_CONTA IN(SELECT SUM(NF.VRPRODUTOS) AS VRNOTAFISC,
                             SUM(NF.VRNOTAFISC) AS VRTIPOCONTA, CP.CDTPCTRECEB, NF.CDCLIENTE, NF.CDSERIE,
                             NF.NRNOTAFISC, NF.DTEMISSAO, NF.DTENTRSAID,  NF.IDSTATUS,
                             NF.NRLICENCAUSO, NF.CDVENDEDOR, NF.CDFILIAL, NF.NRLANCTONF, NF.CDEMPRESA,
                             NF.CDTIPOOPER, NF.CDFILIEMIT, NF.IDDOCCONTAB
                        FROM NOTAFISCAL NF, (SELECT C.CDFILIAL, C.NRLANCTONF, MAX(C.CDTPCTRECEB) AS CDTPCTRECEB
                                               FROM CONTARECEBER C
                                              WHERE C.CDFILIAL   = P_CDFILIAL
                                                AND C.NRLANCTONF = P_NRLANCTONF
                                              GROUP BY C.CDFILIAL, C.NRLANCTONF) CP
                       WHERE NF.CDFILIAL   = P_CDFILIAL
                         AND NF.NRLANCTONF = P_NRLANCTONF
                         AND CP.CDFILIAL   = NF.CDFILIAL
                         AND CP.NRLANCTONF = NF.NRLANCTONF
					       GROUP BY CP.CDTPCTRECEB, NF.CDCLIENTE, NF.CDSERIE,
                             NF.NRNOTAFISC,NF.DTEMISSAO, NF.DTENTRSAID,  NF.IDSTATUS,
                             NF.NRLICENCAUSO, NF.CDVENDEDOR, NF.CDFILIAL, NF.NRLANCTONF, NF.CDEMPRESA,
                             NF.CDTIPOOPER,NF.CDFILIEMIT,NF.IDDOCCONTAB) LOOP

      BEGIN
         SELECT T.CDCONTCTBL AS CDCONTCTBLTAJ, SUM(VRAJUSTEREC)
           INTO V_CDCONTAAJUSTE, V_VRAJUSTE
           FROM TIPOAJRECEBER T, AJCTRECEBER A, CONTARECEBER CR, NOTAFISCAL NF
          WHERE A.CDFILIAL     = P_CDFILIAL
            AND A.NRLANCTONF   = P_NRLANCTONF
            AND CR.CDFILIAL    = NF.CDFILIAL
            AND CR.NRLANCTONF  = NF.NRLANCTONF
            AND A.CDFILIAL     = CR.CDFILIAL
            AND A.NRLANCTONF   = CR.NRLANCTONF
            AND A.NRSEQVENREC  = CR.NRSEQVENREC
            AND A.CDAJUCTREC   = T.CDAJUCTREC
            AND NF.IDDOCCONTAB = 'S'
            AND A.IDAJTXADMECF = 'S'
          GROUP BY T.CDCONTCTBL;
      EXCEPTION
         WHEN NO_DATA_FOUND THEN
            V_VRAJUSTE      := NULL;
            V_CDCONTAAJUSTE := NULL;
      END;

      BEGIN
         SELECT T.CDCONTPROD, T.CDHISTPADR, H.DSHISTPADR
           INTO V_STCONTCTBLPROD, V_CDHISTPADR, V_DSHISTPADR
           FROM TIPOOPERAC T, HISTPADR H
          WHERE T.CDTIPOOPER   = C_REC_CONTA.CDTIPOOPER
            AND T.CDHISTPADR   = H.CDHISTPADR(+)
            AND T.IDENTRSAIDOP = 'S';
      EXCEPTION
         WHEN NO_DATA_FOUND THEN
            V_STCONTCTBLPROD := NULL;
            V_CDHISTPADR     := NULL;
            V_DSHISTPADR     := NULL;
      END;

      V_DTENTRSAID := C_REC_CONTA.DTEMISSAO;
      V_CDEMPRESA  := C_REC_CONTA.CDEMPRESA;

      MONTA_HISTORICO(C_REC_CONTA.CDFILIAL,C_REC_CONTA.NRLANCTONF,V_CDHISTPADR,'','','12', '',V_DSHISTPADR);

      IF V_GERALOTE = 'S' THEN
         GERA_LOTECONTABIL(C_REC_CONTA.CDEMPRESA,'12',C_REC_CONTA.CDFILIEMIT,'',C_REC_CONTA.DTEMISSAO,C_REC_CONTA.CDTPCTRECEB,'',V_CDLOTE,V_NRLANCLOTE);
         V_CDTPCTRECEB := C_REC_CONTA.CDTPCTRECEB;
      END IF;
          
      BEGIN
         SELECT CDCONTCTBL
           INTO V_STCONTCTBLTPREC
           FROM TIPOCTRECEBER
          WHERE CDTPCTRECEB = C_REC_CONTA.CDTPCTRECEB;
      EXCEPTION
           WHEN NO_DATA_FOUND THEN
             V_STCONTCTBLTPREC := NULL;
      END;
      BEGIN
        SELECT IDTIPOCOME
          INTO V_IDCODCOMETPREC
          FROM CONTCTBL
         WHERE CDCONTCTBL = V_STCONTCTBLTPREC;
      EXCEPTION
          WHEN NO_DATA_FOUND THEN
               V_IDCODCOMETPREC := NULL;
      END;

      IF V_IDCODCOMETPREC = 'C' THEN
         V_CODCOMETPREC := C_REC_CONTA.CDCLIENTE;
      ELSIF V_IDCODCOMETPREC = 'B' THEN
         BEGIN
            SELECT MAX(NVL(T.CDREDUCONT, TP.CDREDUCONT))
              INTO V_CODCOMETPREC
              FROM TIPORECEFILIAL T, TIPORECE TP, TPRECBOLETO TE, CONTARECEBER CR
             WHERE TP.CDTPCTRECEB  = C_REC_CONTA.CDTPCTRECEB
               AND TP.CDTPCTRECEB  = CR.CDTPCTRECEB
               AND TE.CDFILIAL     = CR.CDFILIBOLREC
               AND TE.NRLANBOLREC  = CR.NRLANBOLREC
               AND T.CDTIPORECE(+) = TE.CDTIPORECE
               AND TP.CDTIPORECE   = TE.CDTIPORECE
               AND CR.CDFILIAL     = P_CDFILIAL
               AND CR.NRLANCTONF   = P_NRLANCTONF
               AND T.CDFILIAL(+)   = C_REC_CONTA.CDFILIEMIT;
         EXCEPTION
            WHEN NO_DATA_FOUND THEN
               V_CODCOMETPREC := NULL;
         END;
      ELSE
         V_CODCOMETPREC := NULL;
      END IF;

      BEGIN
         SELECT CDFILIAL
           INTO V_CDFILIAL
           FROM RATTPCTR
          WHERE (CDTPCTRECEB = C_REC_CONTA.CDTPCTRECEB);
      EXCEPTION
           WHEN NO_DATA_FOUND THEN
             V_CDFILIAL := NULL;
      END;
      IF (C_REC_CONTA.IDDOCCONTAB = 'S') THEN
        V_VRPARTIDA := C_REC_CONTA.VRNOTAFISC - NVL(V_VRAJUSTE,0);
      ELSE 
        V_VRPARTIDA := C_REC_CONTA.VRTIPOCONTA;
      END IF;

      /*DEBITA TIPO CONTA*/
      NOVA_PARTIDA(C_REC_CONTA.CDEMPRESA,V_CDLOTE,C_REC_CONTA.DTEMISSAO,V_NRLANCLOTE, V_STCONTCTBLTPREC, V_CDHISTPADR,
                   V_DSHISTPADR, V_VRPARTIDA * -1, V_IDCODCOMETPREC, V_CODCOMETPREC, '12',C_REC_CONTA.NRNOTAFISC || '/' || C_REC_CONTA.CDSERIE, 'N');

      BEGIN
         SELECT NVL(F.CDCENTCUST, E.CDCENTCUST) AS CDCENTCUST
           INTO V_CDCENTCUST
           FROM FILIAL F, EMPRESA E
          WHERE F.CDFILIAL  = C_REC_CONTA.CDFILIEMIT
            AND E.CDEMPRESA = C_REC_CONTA.CDEMPRESA;
      EXCEPTION
         WHEN NO_DATA_FOUND THEN
            V_CDCENTCUST := NULL;
      END;

      IF V_CDCENTCUST IS NOT NULL THEN
         RATEIO_PARTIDA(C_REC_CONTA.CDEMPRESA,V_CDLOTE,C_REC_CONTA.DTEMISSAO,V_NRLANCLOTE,C_REC_CONTA.CDFILIEMIT,
                        V_CDCENTCUST, V_VRPARTIDA*-1);
      END IF;

      BEGIN
         SELECT IDTIPOCOME
           INTO V_IDCODCOMEPROD
           FROM CONTCTBL
          WHERE CDCONTCTBL = V_STCONTCTBLPROD;
      EXCEPTION
         WHEN NO_DATA_FOUND THEN
            V_IDCODCOMEPROD := NULL;
      END;

      IF V_IDCODCOMEPROD = 'C' THEN
         V_CODCOMEPROD := C_REC_CONTA.CDCLIENTE;
      ELSIF V_IDCODCOMEPROD = 'B' THEN
         BEGIN
            SELECT MAX(NVL(T.CDREDUCONT, TP.CDREDUCONT))
              INTO V_CODCOMEPROD
              FROM TIPORECEFILIAL T, TIPORECE TP, TPRECBOLETO TE, CONTARECEBER CR
             WHERE TP.CDTPCTRECEB  = C_REC_CONTA.CDTPCTRECEB
               AND TP.CDTPCTRECEB  = CR.CDTPCTRECEB
               AND TE.CDFILIAL     = CR.CDFILIBOLREC
               AND TE.NRLANBOLREC  = CR.NRLANBOLREC
               AND T.CDTIPORECE(+) = TE.CDTIPORECE
               AND TP.CDTIPORECE   = TE.CDTIPORECE
               AND CR.CDFILIAL     = P_CDFILIAL
               AND CR.NRLANCTONF   = P_NRLANCTONF
               AND T.CDFILIAL(+)   = C_REC_CONTA.CDFILIEMIT;
         EXCEPTION
            WHEN NO_DATA_FOUND THEN
               V_CODCOMEPROD := NULL;
         END;
      ELSE
         V_CODCOMEPROD := NULL;
      END IF;

      /* CREDITA RECEITA */
      NOVA_PARTIDA(C_REC_CONTA.CDEMPRESA,V_CDLOTE,C_REC_CONTA.DTEMISSAO,V_NRLANCLOTE, V_STCONTCTBLPROD, V_CDHISTPADR,
                     V_DSHISTPADR, V_VRPARTIDA, V_IDCODCOMEPROD, V_CODCOMEPROD, '12', C_REC_CONTA.NRNOTAFISC || '/' || C_REC_CONTA.CDSERIE, 'N');

      BEGIN
         SELECT NVL(F.CDCENTCUST, E.CDCENTCUST) AS CDCENTCUST
           INTO V_CDCENTCUST
           FROM FILIAL F, EMPRESA E
          WHERE F.CDFILIAL  = C_REC_CONTA.CDFILIEMIT
            AND E.CDEMPRESA = C_REC_CONTA.CDEMPRESA;
      EXCEPTION
           WHEN NO_DATA_FOUND THEN
            V_CDCENTCUST := NULL;
      END;

      BEGIN
         SELECT IDCONTCCUS
           INTO V_IDCONTCCUS
           FROM CONTCTBL
          WHERE CDCONTCTBL = V_STCONTCTBLPROD;
      EXCEPTION
          WHEN NO_DATA_FOUND THEN
            V_IDCONTCCUS := NULL;
      END;

      IF V_CDCENTCUST IS NOT NULL THEN
         IF V_IDCONTCCUS <> 'O' THEN
            RATEIO_PARTIDA(C_REC_CONTA.CDEMPRESA, V_CDLOTE, C_REC_CONTA.DTEMISSAO, V_NRLANCLOTE, C_REC_CONTA.CDFILIEMIT,
                           V_CDCENTCUST, V_VRPARTIDA);
         END IF;
      END IF;
      IF V_GERALOTE = 'S' THEN
         INSERT INTO RELACNFLANCLOTE
            (CDEMPRESA, CDLOTE, NRLANCLOTE, NRSEQLANLOTE,CDFILIAL, NRLANCTONF, CDFILIALVND, CDCAIXA, CDLOJA, DTREDUCAOZ, DTEMISSAO)
          VALUES
            (C_REC_CONTA.CDEMPRESA, V_CDLOTE, V_NRLANCLOTE, '00001', C_REC_CONTA.CDFILIAL, C_REC_CONTA.NRLANCTONF, P_CDFILIALVND, P_CDCAIXA, P_CDLOJA, P_DTREDUCAOZ, C_REC_CONTA.DTEMISSAO);
      END IF;
		V_GERALOTE := 'N';
   END LOOP;

   FOR C_AJUSREC IN(SELECT A.CDFILIAL, A.NRLANCTONF, A.NRSEQVENREC, NF.CDEMPRESA,
                           A.NRPARCELAREC, A.DTAJUSTEREC, A.CDAJUCTREC, NF.CDCLIENTE,
                           T.IDAJUCTREC, T.DSAJUCTREC, A.DSAJUSTEREC, NF.DTENTRSAID,
                           NF.DTEMISSAO, A.VRAJUSTEREC, A.CDECTBAJREC, A.NRLCTBAJREC,
                           A.CDCLASSFINA, A.CDEMMFAJCNTR, A.CDFIMFAJCNTR,
                           A.DTMOVFLUXO, A.IDMOVFLUXO, NF.NRNOTAFISC, NF.CDSERIE,
                           NF.CDFILIEMIT, T.CDCONTCTBL AS CDCONTCTBLTAJ, CR.CDTPCTRECEB,
                           NF.CDTIPOOPER
                      FROM TIPOAJRECEBER T, AJCTRECEBER A, CONTARECEBER CR, NOTAFISCAL NF
                     WHERE A.CDFILIAL     = P_CDFILIAL
                       AND A.NRLANCTONF   = P_NRLANCTONF
                       AND CR.CDFILIAL    = NF.CDFILIAL
                       AND CR.NRLANCTONF  = NF.NRLANCTONF
                       AND A.CDFILIAL     = CR.CDFILIAL
                       AND A.NRLANCTONF   = CR.NRLANCTONF
                       AND A.NRSEQVENREC  = CR.NRSEQVENREC
                       AND A.CDAJUCTREC   = T.CDAJUCTREC
                       AND NF.IDDOCCONTAB <> 'S'
                     ORDER BY A.CDFILIAL, A.NRLANCTONF, A.CDAJUCTREC) LOOP

      BEGIN
         SELECT IDTIPOCOME
           INTO V_IDCODCOME
           FROM CONTCTBL
          WHERE CDCONTCTBL = C_AJUSREC.CDCONTCTBLTAJ;
      EXCEPTION
           WHEN NO_DATA_FOUND THEN
             V_IDCODCOME := NULL;
      END;
      IF V_IDCODCOME = 'C' THEN
         V_CODCOME := C_AJUSREC.CDCLIENTE;
      ELSIF V_IDCODCOME = 'B' THEN
         BEGIN
            SELECT MAX(NVL(T.CDREDUCONT, TP.CDREDUCONT))
              INTO V_CODCOME
              FROM TIPORECEFILIAL T, TIPORECE TP, TPRECBOLETO TE, CONTARECEBER CR
             WHERE TP.CDTPCTRECEB  = C_AJUSREC.CDTPCTRECEB
               AND TP.CDTPCTRECEB  = CR.CDTPCTRECEB
               AND TE.CDFILIAL     = CR.CDFILIBOLREC
               AND TE.NRLANBOLREC  = CR.NRLANBOLREC
               AND T.CDTIPORECE(+) = TE.CDTIPORECE
               AND TP.CDTIPORECE   = TE.CDTIPORECE
               AND CR.CDFILIAL     = P_CDFILIAL
               AND CR.NRLANCTONF   = P_NRLANCTONF
               AND T.CDFILIAL(+)   = C_AJUSREC.CDFILIEMIT;
         EXCEPTION
            WHEN NO_DATA_FOUND THEN
               V_CODCOME := NULL;
         END;
      ELSE
         V_CODCOME := NULL;
      END IF;

      /*AJUSTES DE DEDUCAO -  TAXA ADMINISTRATIVA DO CARTAO, AJUSTE DE REDUCAO DIFERENCA ENTRE AS VENDAS E A SANGRIA*/
      IF C_AJUSREC.IDAJUCTREC = 'D' THEN
         IF C_AJUSREC.DSAJUSTEREC = 'AJUSTE DIF-SANGRIA' THEN 
            /*DEBITA TIPO DE AJUSTE (QUEBRA DE CAIXA)*/
            NOVA_PARTIDA(C_AJUSREC.CDEMPRESA,V_CDLOTE,C_AJUSREC.DTEMISSAO,V_NRLANCLOTE, C_AJUSREC.CDCONTCTBLTAJ, V_CDHISTPADR,
                         V_DSHISTPADR, C_AJUSREC.VRAJUSTEREC * (-1), V_IDCODCOME, V_CODCOME, '12', C_AJUSREC.NRNOTAFISC || '/' || C_AJUSREC.CDSERIE, 'N');

            IF V_CDCENTCUST IS NOT NULL THEN
               RATEIO_PARTIDA(C_AJUSREC.CDEMPRESA,V_CDLOTE,C_AJUSREC.DTEMISSAO,V_NRLANCLOTE,C_AJUSREC.CDFILIEMIT,
                              V_CDCENTCUST, C_AJUSREC.VRAJUSTEREC * (-1));
            END IF;

            /*CREDITA TIPO DE CONTA - RECEBIMENTO*/
            NOVA_PARTIDA(C_AJUSREC.CDEMPRESA,V_CDLOTE,C_AJUSREC.DTEMISSAO,V_NRLANCLOTE, V_STCONTCTBLTPREC, V_CDHISTPADR,
                         V_DSHISTPADR, C_AJUSREC.VRAJUSTEREC, V_IDCODCOMETPREC, V_CODCOMETPREC, '12', C_AJUSREC.NRNOTAFISC || '/' || C_AJUSREC.CDSERIE, 'N');

            IF V_CDCENTCUST IS NOT NULL THEN
               RATEIO_PARTIDA(C_AJUSREC.CDEMPRESA,V_CDLOTE,C_AJUSREC.DTEMISSAO,V_NRLANCLOTE,C_AJUSREC.CDFILIEMIT,
                              V_CDCENTCUST, ROUND(C_AJUSREC.VRAJUSTEREC,2));
            END IF;
         ELSE
            /*DEBITA TIPO DE AJUSTE */
            NOVA_PARTIDA(C_AJUSREC.CDEMPRESA,V_CDLOTE,C_AJUSREC.DTEMISSAO,V_NRLANCLOTE, C_AJUSREC.CDCONTCTBLTAJ, V_CDHISTPADR,
                         V_DSHISTPADR, C_AJUSREC.VRAJUSTEREC * (-1), V_IDCODCOME, V_CODCOME, '12', C_AJUSREC.NRNOTAFISC || '/' || C_AJUSREC.CDSERIE, 'N');

            IF V_CDCENTCUST IS NOT NULL THEN
               RATEIO_PARTIDA(C_AJUSREC.CDEMPRESA,V_CDLOTE,C_AJUSREC.DTEMISSAO,V_NRLANCLOTE,C_AJUSREC.CDFILIEMIT,
                              V_CDCENTCUST, C_AJUSREC.VRAJUSTEREC * (-1));
            END IF;

            /* CREDITA TIPO DE CONTA*/
            NOVA_PARTIDA(C_AJUSREC.CDEMPRESA,V_CDLOTE,C_AJUSREC.DTEMISSAO,V_NRLANCLOTE, V_STCONTCTBLTPREC, V_CDHISTPADR,
                         V_DSHISTPADR, C_AJUSREC.VRAJUSTEREC, V_IDCODCOMETPREC, V_CODCOMETPREC, '12', C_AJUSREC.NRNOTAFISC || '/' || C_AJUSREC.CDSERIE, 'N');

            IF V_CDCENTCUST IS NOT NULL THEN
               RATEIO_PARTIDA(C_AJUSREC.CDEMPRESA,V_CDLOTE,C_AJUSREC.DTEMISSAO,V_NRLANCLOTE,C_AJUSREC.CDFILIEMIT,
                              V_CDCENTCUST, ROUND(C_AJUSREC.VRAJUSTEREC,2));
            END IF;
         END IF;
      /*AJUSTES DE ACRESCIMO - TAXA DE SERVICO, TAXA ADMINISTRATIVA SOBRE A TAXA DE SERVICO, AJUSTE DE ACRESCIMO DIFERENCA ENTRE AS VENDAS E A SANGRIA */
      ELSE
         IF C_AJUSREC.DSAJUSTEREC = 'AJUSTE DIF-SANGRIA' THEN
            /*DEBITA TIPO DE CONTA - RECEBIMENTO*/
            NOVA_PARTIDA(C_AJUSREC.CDEMPRESA,V_CDLOTE,C_AJUSREC.DTEMISSAO,V_NRLANCLOTE, V_STCONTCTBLTPREC, V_CDHISTPADR,
                         V_DSHISTPADR, C_AJUSREC.VRAJUSTEREC * (-1), V_IDCODCOMEPROD, V_CODCOMEPROD, '12', C_AJUSREC.NRNOTAFISC || '/' || C_AJUSREC.CDSERIE, 'N');

            IF V_CDCENTCUST IS NOT NULL THEN
               RATEIO_PARTIDA(C_AJUSREC.CDEMPRESA,V_CDLOTE,C_AJUSREC.DTEMISSAO,V_NRLANCLOTE,C_AJUSREC.CDFILIEMIT,
                              V_CDCENTCUST, C_AJUSREC.VRAJUSTEREC * (-1));
            END IF;

            /*CREDITA TIPO DE AJUSTE - QUEBRA DE CAIXA*/
            NOVA_PARTIDA(C_AJUSREC.CDEMPRESA,V_CDLOTE,C_AJUSREC.DTEMISSAO,V_NRLANCLOTE, C_AJUSREC.CDCONTCTBLTAJ, V_CDHISTPADR,
                         V_DSHISTPADR, C_AJUSREC.VRAJUSTEREC, V_IDCODCOME, V_CODCOME, '12', C_AJUSREC.NRNOTAFISC || '/' || C_AJUSREC.CDSERIE, 'N');

            IF V_CDCENTCUST IS NOT NULL THEN
               RATEIO_PARTIDA(C_AJUSREC.CDEMPRESA,V_CDLOTE,C_AJUSREC.DTEMISSAO,V_NRLANCLOTE,C_AJUSREC.CDFILIEMIT,
                              V_CDCENTCUST, ROUND(C_AJUSREC.VRAJUSTEREC,2));
            END IF;
         ELSE
            /*DEBITA TIPO DE OPERACAO (PRODUTO)*/          
            NOVA_PARTIDA(C_AJUSREC.CDEMPRESA,V_CDLOTE,C_AJUSREC.DTEMISSAO,V_NRLANCLOTE, V_STCONTCTBLTPREC, V_CDHISTPADR,
                         V_DSHISTPADR, C_AJUSREC.VRAJUSTEREC * (-1), V_IDCODCOMEPROD, V_CODCOMEPROD, '12', C_AJUSREC.NRNOTAFISC || '/' || C_AJUSREC.CDSERIE, 'N');

            IF V_CDCENTCUST IS NOT NULL THEN
               RATEIO_PARTIDA(C_AJUSREC.CDEMPRESA,V_CDLOTE,C_AJUSREC.DTEMISSAO,V_NRLANCLOTE,C_AJUSREC.CDFILIEMIT,
                              V_CDCENTCUST, C_AJUSREC.VRAJUSTEREC * (-1));
            END IF;

            /* CREDITA TIPO DE AJUSTE*/
            NOVA_PARTIDA(C_AJUSREC.CDEMPRESA,V_CDLOTE,C_AJUSREC.DTEMISSAO,V_NRLANCLOTE, C_AJUSREC.CDCONTCTBLTAJ, V_CDHISTPADR,
                         V_DSHISTPADR, C_AJUSREC.VRAJUSTEREC, V_IDCODCOME, V_CODCOME, '12', C_AJUSREC.NRNOTAFISC || '/' || C_AJUSREC.CDSERIE, 'N');

            IF V_CDCENTCUST IS NOT NULL THEN
               RATEIO_PARTIDA(C_AJUSREC.CDEMPRESA,V_CDLOTE,C_AJUSREC.DTEMISSAO,V_NRLANCLOTE,C_AJUSREC.CDFILIEMIT,
                              V_CDCENTCUST, ROUND(C_AJUSREC.VRAJUSTEREC,2));
            END IF;
         END IF;
      END IF;
		IF V_GERALOTE = 'S' THEN
		   INSERT INTO RELACNFLANCLOTE
			   (CDEMPRESA, CDLOTE, NRLANCLOTE, NRSEQLANLOTE,CDFILIAL, NRLANCTONF, CDFILIALVND, CDCAIXA, CDLOJA, DTREDUCAOZ, DTEMISSAO)
		    VALUES
			   (C_AJUSREC.CDEMPRESA, V_CDLOTE, V_NRLANCLOTE, '00001', C_AJUSREC.CDFILIAL, C_AJUSREC.NRLANCTONF, P_CDFILIALVND, P_CDCAIXA, P_CDLOJA, P_DTREDUCAOZ, C_AJUSREC.DTEMISSAO);
		END IF;
		V_GERALOTE := 'N';
   END LOOP;

   V_GERALOTEFIS := 'S';

   FOR C_IMPOSTO IN (
       SELECT A.CDIMPOSTO, NVL(ROUND(SUM(A.VRIMPOSTONF),2),0) AS VRIMPOSTONF,
              A.IDTRATIMPO, A.IDIMPORECUP, A.IDACRPRODENTRNF, A.IDTRIMPOSTNF,
              NVL(SUM(VRIMPCARGATRIBI),0) AS VRIMPCARGATRIBI, A.CDCLIENTE,A.CDFILIAL,
              MAX(CDCONTATIV) AS CDCONTATIV, MAX(CDCONTPASS)  AS CDCONTPASS,A.CDFILIEMIT,
              MAX(CDCONTDESP)  AS CDCONTDESP,A.CDEMPRESA, A.NRNOTAFISC, A.CDSERIE, A.DTENTRSAID,A.CDTIPOOPER,
              A.DTEMISSAO
         FROM (SELECT INF.CDIMPOSTO, VRIMPOSTONF, VRIMPCARGATRIBI, NVL(IDACRPRODENTRNF, 'N') IDACRPRODENTRNF, NF.CDEMPRESA,
                      NF.NRNOTAFISC,NF.CDSERIE, NF.CDCLIENTE,NF.CDFILIAL,NF.CDFILIEMIT, NF.CDTIPOOPER,
                      PF.IDCARACFIS, I.IDIMPORECUP, I.IDTRATIMPO, INF.CDFISCALNF, INF.IDTRIMPOSTNF,NF.DTENTRSAID,
                      CONTCTBL_IMPOSTO(I.CDIMPOSTO, 'A', NF.IDENTRSAIDOP, NF.CDTIPOOPER, NF.IDTPIJURNF) CDCONTATIV,
                      CONTCTBL_IMPOSTO(I.CDIMPOSTO, 'P', NF.IDENTRSAIDOP, NF.CDTIPOOPER, NF.IDTPIJURNF) CDCONTPASS,
                      CONTCTBL_IMPOSTO(I.CDIMPOSTO, 'D', NF.IDENTRSAIDOP, NF.CDTIPOOPER, NF.IDTPIJURNF) CDCONTDESP,
                      NF.DTEMISSAO
                 FROM IMPOSTONF INF, IMPOSTO I, NOTAFISCAL NF, PARAMFILIAL PF
                WHERE NF.CDFILIAL     = P_CDFILIALFIS
                  AND NF.NRLANCTONF   = P_NRLANCTONFFIS
                  AND PF.CDFILIAL(+)  = NF.CDFILIRECE
                  AND INF.CDFILIAL    = P_CDFILIALFIS
                  AND INF.NRLANCTONF  = P_NRLANCTONFFIS
                  AND INF.CDIMPOSTO   = I.CDIMPOSTO
                  AND INF.VRIMPOSTONF > 0
                  AND INF.IDIMPOSTONF IN ('1','4')
                  AND I.IDTIPCONTAB  = 'D') A
        WHERE ((((A.IDIMPORECUP  = 'S' AND A.CDFISCALNF = '1' AND NVL(A.IDCARACFIS,'C') = 'C') OR (A.IDCARACFIS = 'A')) AND (A.CDCONTATIV IS NOT NULL)) OR
               ((A.IDIMPORECUP <> 'S') AND (A.CDCONTPASS IS NOT NULL)))
        GROUP BY A.CDIMPOSTO, A.IDTRATIMPO,  A.IDIMPORECUP, A.IDACRPRODENTRNF, A.IDTRIMPOSTNF,A.CDEMPRESA, A.NRNOTAFISC, A.CDSERIE, A.DTENTRSAID,
                A.CDCLIENTE,A.CDFILIAL,A.CDFILIEMIT,A.CDTIPOOPER, A.DTEMISSAO) LOOP

       IF V_GERALOTEFIS = 'S' THEN
          GERA_LOTECONTABIL(C_IMPOSTO.CDEMPRESA,'12',C_IMPOSTO.CDFILIEMIT,'',C_IMPOSTO.DTEMISSAO,V_CDTPCTRECEB,'',V_CDLOTE,V_NRLANCLOTE);
       END IF;
            
       V_DTENTRSAID := C_IMPOSTO.DTEMISSAO;
       V_CDEMPRESA  := C_IMPOSTO.CDEMPRESA;

       BEGIN
         SELECT F.CDCENTCUST AS CDCENTCUST
           INTO V_CDCENTCUST
           FROM FILIAL F
          WHERE F.CDFILIAL = C_IMPOSTO.CDFILIEMIT;
       EXCEPTION
          WHEN NO_DATA_FOUND THEN
           V_CDCENTCUST := NULL;
       END;

       BEGIN
         SELECT T.CDHISTPADR,H.DSHISTPADR
           INTO V_CDHISTPADR,V_DSHISTPADR
           FROM TIPOOPERAC T, HISTPADR H
          WHERE T.CDTIPOOPER = C_IMPOSTO.CDTIPOOPER
            AND T.CDHISTPADR = H.CDHISTPADR
            AND T.IDENTRSAIDOP = 'S';
       EXCEPTION
         WHEN NO_DATA_FOUND THEN

         V_CDHISTPADR := NULL;
         V_DSHISTPADR := NULL;
       END;

       MONTA_HISTORICO(P_CDFILIALFIS,P_NRLANCTONFFIS,V_CDHISTPADR,'','','12', '',V_DSHISTPADR);

       NOVA_PARTIDA(C_IMPOSTO.CDEMPRESA,V_CDLOTE,C_IMPOSTO.DTEMISSAO,V_NRLANCLOTE, C_IMPOSTO.CDCONTDESP, V_CDHISTPADR,
                    V_DSHISTPADR, -ROUND(C_IMPOSTO.VRIMPOSTONF,2), '', '', '12', C_IMPOSTO.NRNOTAFISC || '/' || C_IMPOSTO.CDSERIE, 'N');

       IF V_CDCENTCUST IS NOT NULL THEN
          RATEIO_PARTIDA(C_IMPOSTO.CDEMPRESA,V_CDLOTE,C_IMPOSTO.DTEMISSAO,V_NRLANCLOTE, C_IMPOSTO.CDFILIEMIT, V_CDCENTCUST, - ROUND(C_IMPOSTO.VRIMPOSTONF,2));
       END IF;

       NOVA_PARTIDA(C_IMPOSTO.CDEMPRESA,V_CDLOTE,C_IMPOSTO.DTEMISSAO,V_NRLANCLOTE, C_IMPOSTO.CDCONTPASS, V_CDHISTPADR,
                      V_DSHISTPADR, ROUND(C_IMPOSTO.VRIMPOSTONF,2), '', '', '12', C_IMPOSTO.NRNOTAFISC || '/' || C_IMPOSTO.CDSERIE, 'N');

       BEGIN
         SELECT F.CDCENTCUST AS CDCENTCUST
           INTO V_CDCENTCUST
           FROM FILIAL F
          WHERE F.CDFILIAL = C_IMPOSTO.CDFILIEMIT;
       EXCEPTION
          WHEN NO_DATA_FOUND THEN
           V_CDCENTCUST := NULL;
       END;

       IF V_CDCENTCUST IS NOT NULL THEN
          RATEIO_PARTIDA(C_IMPOSTO.CDEMPRESA,V_CDLOTE,C_IMPOSTO.DTEMISSAO,V_NRLANCLOTE, C_IMPOSTO.CDFILIEMIT, V_CDCENTCUST, ROUND(C_IMPOSTO.VRIMPOSTONF,2));
       END IF;

       IF V_GERALOTEFIS = 'S' THEN
          INSERT INTO RELACNFLANCLOTE
               (CDEMPRESA, CDLOTE, NRLANCLOTE, NRSEQLANLOTE,CDFILIAL, NRLANCTONF, CDFILIALVND, CDCAIXA, CDLOJA, DTREDUCAOZ, DTEMISSAO)
          VALUES
               (C_IMPOSTO.CDEMPRESA, V_CDLOTE, V_NRLANCLOTE, '00001', P_CDFILIALFIS, P_NRLANCTONFFIS, P_CDFILIALVND, P_CDCAIXA, P_CDLOJA, P_DTREDUCAOZ, C_IMPOSTO.DTEMISSAO);
      END IF;
       V_GERALOTEFIS := 'N';
   END LOOP;

   /*VERIFICA INCONSISTENCIAS NA CONTABILIZACAO*/
   CONFERE_PARTIDA(V_CDEMPRESA, V_CDLOTE, V_NRLANCLOTE, V_DTENTRSAID, V_ERROS, V_RETORNO);
  /*Variavel que recebe verdadeiro se nao for encontrado nenhuma inconsistencia nas partidas e falso se houver inconsistencia nos lancamentos*/
   IF V_RETORNO THEN
      IF P_CDFILIALFIS IS NOT NULL THEN
         UPDATE NOTAFISCAL SET IDSTATUSCTBNF = 'C'
          WHERE CDFILIAL = P_CDFILIALFIS
            AND NRLANCTONF = P_NRLANCTONFFIS;
      ELSE
         UPDATE NOTAFISCAL SET IDSTATUSCTBNF = 'C'
          WHERE CDFILIAL = P_CDFILIAL
            AND NRLANCTONF = P_NRLANCTONF;
      END IF;
      /*Recebe o retorno para passar para a procedure principal*/
        P_RETORNO := 'S';
      /*Pega os erros para passar para a procedure principal*/  
        P_ERROS   := V_ERROS;
   ELSE
      IF P_CDFILIALFIS IS NOT NULL THEN
         UPDATE NOTAFISCAL SET IDSTATUSCTBNF = 'E'
          WHERE CDFILIAL = P_CDFILIALFIS
            AND NRLANCTONF = P_NRLANCTONFFIS;
      ELSE
         UPDATE NOTAFISCAL SET IDSTATUSCTBNF = 'E'
          WHERE CDFILIAL = P_CDFILIAL
            AND NRLANCTONF = P_NRLANCTONF;
      END IF;
      /*Recebe o retorno para passar para a procedure principal*/
      P_RETORNO := 'N';
      /*Pega os erros para passar para a procedure principal*/  
      P_ERROS   := V_ERROS;
   END IF;
END;
/