CREATE OR REPLACE PROCEDURE GRAVA_RELAT_RCPE(
   P_CDFILIAL      IN RELAT_RCPE.CDFILIALRCPE%TYPE,
   P_NRRELATRCPE   IN RELAT_RCPE.NRRELATRCPE%TYPE,
   P_CDPRODUTO     IN RELAT_RCPE.CDPRODRCPE%TYPE,
   P_IDTPDADORCPE  IN RELAT_RCPE.IDTPDADORCPE%TYPE,
   P_IDTIPOMOVIEST IN RELAT_RCPE.IDTIPOMOVIEST%TYPE,
   P_NRLANCESTQ    IN LANCTOESTOQ.NRLANCESTQ%TYPE,
   P_DTLANCMOVIEST IN RELAT_RCPE.DTLANCMOVIEST%TYPE,
   P_QTLANCAMENTO  IN RELAT_RCPE.QTLANCAMENTO%TYPE,
   P_VRTOTLANCRCPE IN RELAT_RCPE.VRTOTLANCRCPE%TYPE,
   P_QTDEMOVIEST   IN RELAT_RCPE.QTDEMOVIEST%TYPE,
   P_CDTOTMOVIEST  IN RELAT_RCPE.CDTOTMOVIEST%TYPE,
   P_IDORDEMCALC   IN RELAT_RCPE.IDORDEMCALC%TYPE,
   P_NRRELATMOVI   IN RELAT_RCPE.NRRELATMOVI%TYPE,
   P_NRSEQPGRCPE   IN RELAT_RCPE.NRSEQPGRCPE%TYPE,
   P_NRSEQLINHA    IN RELAT_RCPE.NRSEQLINHA%TYPE,
   P_NRLIVRO       IN RELAT_RCPE.NRLIVRO%TYPE,
   P_NRFOLHALIVRO  IN RELAT_RCPE.NRFOLHALIVRO%TYPE,
   P_NRLINHALIVRO  IN RELAT_RCPE.NRLINHALIVRO%TYPE)
IS
   V_NRSEQRCPE     RELAT_RCPE.NRSEQRCPE%TYPE;
   V_CDFILIAL      NOTAFISCAL.CDFILIAL%TYPE;
   V_NRLANCTONF    NOTAFISCAL.NRLANCTONF%TYPE;
   V_NRLANCAMENTO  RELAT_RCPE.NRLANCAMENTO%TYPE;
   V_IDMODDOCNFE   MODDOCFISC.IDMODDOCNFE%TYPE;
   V_IDTIPONFRCPE  RELAT_RCPE.IDTIPONFRCPE%TYPE;
   V_CDSERIENFRCPE NOTAFISCAL.CDSERIE%TYPE;
   V_CFOPNFRCPE    NOTAFISCAL.CDFISOPENTF%TYPE;
   V_IDCODRCPE     RELAT_RCPE.IDCODRCPE%TYPE;
   V_VRIPINFRCPE   RELAT_RCPE.VRIPINFRCPE%TYPE;
   V_IDREMESSAINDU TIPOOPERAC.IDREMESSAINDU%TYPE;
   V_IDNFRETIND TIPOOPERAC.IDNFRETIND%TYPE;
BEGIN
   BEGIN
      SELECT LPAD(RTRIM(TO_CHAR(NVL(TO_NUMBER(MAX(NRSEQRCPE)),0) + 1)), 10, '0')
        INTO V_NRSEQRCPE
        FROM RELAT_RCPE
       WHERE CDFILIALRCPE = P_CDFILIAL
         AND NRRELATRCPE = P_NRRELATRCPE;
   EXCEPTION
      WHEN NO_DATA_FOUND THEN BEGIN
         V_NRSEQRCPE := '0000000001';
      END;
   END;
   V_NRLANCAMENTO  := P_NRLANCESTQ;
   V_IDMODDOCNFE   := NULL;
   V_IDTIPONFRCPE  := NULL;
   V_CDSERIENFRCPE := NULL;
   V_CFOPNFRCPE    := NULL;
   V_IDCODRCPE     := '1';
   V_VRIPINFRCPE   := 0;
   IF (P_IDTPDADORCPE = 'DA') THEN
      BEGIN
         SELECT CDFILIAL, NRLANCTONF
           INTO V_CDFILIAL, V_NRLANCTONF
           FROM LANCTOESTOQ
          WHERE CDFILIMOVI = P_CDFILIAL
            AND NRLANCESTQ = P_NRLANCESTQ;
      EXCEPTION
         WHEN NO_DATA_FOUND THEN BEGIN
            V_CDFILIAL := NULL;
            V_NRLANCTONF := NULL;
         END;
      END;
      IF V_NRLANCTONF IS NOT NULL THEN
          BEGIN
             SELECT N.NRNOTAFISC, M.IDMODDOCNFE, N.CDSERIE, N.CDFISOPENTF, T.IDREMESSAINDU, IDNFRETIND
               INTO V_NRLANCAMENTO, V_IDMODDOCNFE, V_CDSERIENFRCPE, V_CFOPNFRCPE, V_IDREMESSAINDU, V_IDNFRETIND
               FROM NOTAFISCAL N LEFT JOIN MODDOCFISC M
                                    ON N.CDMODDOCFISC = M.CDMODDOCFISC
                                 LEFT JOIN TIPOOPERAC T
                                        ON N.IDENTRSAIDOP = T.IDENTRSAIDOP
                                   AND N.CDTIPOOPER = T.CDTIPOOPER
              WHERE N.CDFILIAL   = V_CDFILIAL
                AND N.NRLANCTONF = V_NRLANCTONF;
          EXCEPTION
             WHEN NO_DATA_FOUND THEN BEGIN
                V_NRLANCAMENTO  := NULL;
                V_IDMODDOCNFE   := NULL;
                V_CDSERIENFRCPE := NULL;
                V_CFOPNFRCPE    := NULL;
                V_IDREMESSAINDU := NULL;
                V_IDNFRETIND    := NULL;
             END;
          END;

          IF V_IDMODDOCNFE = 'S' THEN
             V_IDTIPONFRCPE := 'NFe';
          ELSE
             V_IDTIPONFRCPE := 'NF';
          END IF;

          IF (V_IDREMESSAINDU = 'S') OR (V_IDNFRETIND = 'S') THEN
             V_IDCODRCPE := '2';
          END IF;

          BEGIN
             SELECT SUM(VRTOTIMPONF)
               INTO V_VRIPINFRCPE
               FROM IMPOSNFTOT IP JOIN IMPOSTO I
                                    ON IP.CDIMPOSTO = I.CDIMPOSTO
              WHERE CDFILIAL = V_CDFILIAL
                AND NRLANCTONF = V_NRLANCTONF
                AND I.CDIMPOSTONFE = '10';
          EXCEPTION
             WHEN NO_DATA_FOUND THEN BEGIN
                V_VRIPINFRCPE  := 0;
             END;
          END;
      END IF;
   END IF;

   INSERT INTO RELAT_RCPE
      (CDFILIALRCPE,    NRRELATRCPE,     NRSEQRCPE,       CDPRODRCPE,      IDTPDADORCPE,
       IDTIPOMOVIEST,   IDTIPONFRCPE,    CDSERIENFRCPE,   NRLANCAMENTO,    CFOPNFRCPE,
       IDCODRCPE,       VRIPINFRCPE,     DTLANCMOVIEST,   QTLANCAMENTO,    VRTOTLANCRCPE,
       QTDEMOVIEST,     CDTOTMOVIEST,    IDORDEMCALC,     NRRELATMOVI,     NRSEQPGRCPE,
       NRSEQLINHA,      NRLIVRO,         NRFOLHALIVRO,    NRLINHALIVRO)
   VALUES
      (P_CDFILIAL,      P_NRRELATRCPE,   V_NRSEQRCPE,     P_CDPRODUTO,     P_IDTPDADORCPE,
       P_IDTIPOMOVIEST, V_IDTIPONFRCPE,  V_CDSERIENFRCPE, V_NRLANCAMENTO,  V_CFOPNFRCPE,
       V_IDCODRCPE,     V_VRIPINFRCPE,   P_DTLANCMOVIEST, P_QTLANCAMENTO,  P_VRTOTLANCRCPE,
       P_QTDEMOVIEST,   P_CDTOTMOVIEST,  P_IDORDEMCALC,   P_NRRELATMOVI,   P_NRSEQPGRCPE,
       P_NRSEQLINHA,    P_NRLIVRO,       P_NRFOLHALIVRO,  P_NRLINHALIVRO);
END;
/