/* ----------------------------------------------------------------------------------- */
/* @C:\web_610\sql\end_conv\oracle\520C_DFSSTPO_GERA_NOTAFISCAL_CORPO_ENTRADA.SQL */
/* ----------------------------------------------------------------------------------- */

CREATE OR REPLACE PROCEDURE GERA_NOTAFISCAL_CORPO_ENTRADA
   (P_CDFILIAL     IN  FILIAL.CDFILIAL%TYPE,
    P_CDFILIRECE   IN  FILIAL.CDFILIAL%TYPE,
    P_CDEMPRESA    IN  EMPRESA.CDEMPRESA%TYPE,
    P_CDTIPOOPER   IN  TIPOOPERAC.CDTIPOOPER%TYPE,
    P_CDSERIEENTR  IN  SERIEENTR.CDSERIEENTR%TYPE,
    P_CDCLIENTE    IN  CLIENTE.CDCLIENTE%TYPE,
    P_DTENTRSAID   IN  NOTAFISCAL.DTENTRSAID%TYPE,
    P_IDSISTEMA    IN  NOTAFISCAL.IDSISTEMA%TYPE,
    P_VRNOTAFISC   IN  NOTAFISCAL.VRNOTAFISC%TYPE,
    P_PRXLANCTONF  OUT NOTAFISCAL.NRLANCTONF%TYPE,
    P_PRXLANCESTQ  OUT LANCTOESTOQ.NRLANCESTQ%TYPE,
    P_ATUAESTOQUE  IN  PARAMFILIAL.IDCTRLESTQ%TYPE     DEFAULT 'S',
    P_NRAPURACFAT  IN  NOTAFISCAL.NRAPURACFAT%TYPE     DEFAULT NULL,
    P_NRLANCTONF   IN  NOTAFISCAL.NRLANCTONF%TYPE      DEFAULT NULL,
    P_IDTPIJURTRAN IN  NOTAFISCAL.IDTPIJURTRAN%TYPE    DEFAULT NULL,
    P_NRINSJURTRAN IN  NOTAFISCAL.NRINSJURTRAN%TYPE    DEFAULT NULL,
    P_NRPLACAVEIC  IN  NOTAFISCAL.NRPLACAVEIC%TYPE     DEFAULT NULL,
    P_SGUFVEIC     IN  NOTAFISCAL.SGUFVEIC%TYPE        DEFAULT NULL,
    P_IDPGTOFRETE  IN  NOTAFISCAL.IDPGTOFRETE%TYPE     DEFAULT NULL,
    P_DSQTDEVOLU   IN  NOTAFISCAL.DSQTDEVOLU%TYPE      DEFAULT NULL,
    P_DSESPECVOLU  IN  NOTAFISCAL.DSESPECVOLU%TYPE     DEFAULT NULL,
    P_DSMARCAVOLU  IN  NOTAFISCAL.DSMARCAVOLU%TYPE     DEFAULT NULL,
    P_DSNUMEVOLU   IN  NOTAFISCAL.DSNUMEVOLU%TYPE      DEFAULT NULL,
    P_VRPBRUTOVOLU IN  NOTAFISCAL.VRPBRUTOVOLU%TYPE    DEFAULT NULL,
    P_VRPLIQUVOLU  IN  NOTAFISCAL.VRPLIQUVOLU%TYPE     DEFAULT NULL,
    P_NRLANCESTQ   IN  LANCTOESTOQ.NRLANCESTQ%TYPE     DEFAULT NULL,
    P_NRLANCORIG   IN  NOTAFISCAL.CDINTEGRACAO%TYPE    DEFAULT NULL,
    P_DTLANCAMENNF IN  NOTAFISCAL.DTLANCAMENNF%TYPE    DEFAULT NULL,
    P_NRNOTAFISC   IN  NOTAFISCAL.NRNOTAFISC%TYPE      DEFAULT NULL,
    P_DTEMISSAO    IN  NOTAFISCAL.DTEMISSAO%TYPE       DEFAULT NULL,
    P_NRACESSONFE  IN  NOTAFISCAL.NRACESSONFE%TYPE     DEFAULT NULL,
    P_CDMODDOCFISC IN  NOTAFISCAL.CDMODDOCFISC%TYPE    DEFAULT NULL)
AS

   V_IDORIGDESTNF VARCHAR2(01);
   V_IDTPIJURNF   VARCHAR2(01);
   V_NRINSJURNF   VARCHAR2(20);
   V_CDFORNECED   VARCHAR2(21);
   V_CDCLIENTE    VARCHAR2(21);
   V_CDFILIEMIT   VARCHAR2(21);
   V_CDINSCESTA   VARCHAR2(16);
   V_CDINSCMUNI   VARCHAR2(16);
   V_IDDOCFATUR   VARCHAR2(01);
   V_IDDOCFISCENT VARCHAR2(01);
   V_IDDOCESTOQUE VARCHAR2(01);
   V_CDMODDOCFISC VARCHAR2(02);
   V_NRLANCTONF   VARCHAR2(20);
   V_NRLANCESTQ   VARCHAR2(20);

BEGIN
   V_CDCLIENTE    := NULL;
   V_CDFORNECED   := NULL;
   V_CDFILIEMIT   := NULL;

   SELECT MAX(IDORIGDESTOP)
   INTO V_IDORIGDESTNF
   FROM TIPOOPERAC
   WHERE CDTIPOOPER   = P_CDTIPOOPER AND
         IDENTRSAIDOP = 'E';

   SELECT MIN(MT.CDMODDOCFISC)
     INTO V_CDMODDOCFISC
     FROM MODDOCFISCTPOP MT
    WHERE MT.IDENTRSAIDOP = 'E'
      AND MT.CDTIPOOPER   = P_CDTIPOOPER
      AND MT.CDMODDOCFISC = P_CDMODDOCFISC;

   IF (V_CDMODDOCFISC IS NULL) THEN
      SELECT MIN(MT.CDMODDOCFISC)
        INTO V_CDMODDOCFISC
        FROM MODDOCFISCTPOP MT
       WHERE MT.IDENTRSAIDOP = 'E'
         AND MT.CDTIPOOPER   = P_CDTIPOOPER;
   END IF;

   IF V_IDORIGDESTNF IS NULL THEN
      V_IDORIGDESTNF := 'F';
   END IF;

   IF V_IDORIGDESTNF = 'C' THEN
      SELECT MAX(IDTPIJURCLIE), MAX(NRINSJURCLIE),
             MAX(CDCLIENTE)
      INTO V_IDTPIJURNF, V_NRINSJURNF,
           V_CDCLIENTE
      FROM CLIENTE
      WHERE CDCLIENTE = P_CDCLIENTE;

   ELSIF V_IDORIGDESTNF = 'F' THEN
      SELECT MAX(IDTPIJURFORN), MAX(NRINSJURFORN),
             MAX(CDFORNECED)
      INTO V_IDTPIJURNF, V_NRINSJURNF,
           V_CDFORNECED
      FROM FORNECEDOR
      WHERE CDFORNECED = P_CDCLIENTE;

   ELSE
      SELECT MAX(IDTPIJURFILI), MAX(NRINSJURFILI), MAX(CDFILIAL)
      INTO V_IDTPIJURNF, V_NRINSJURNF, V_CDFILIEMIT
      FROM FILIAL
      WHERE CDFILIAL = P_CDCLIENTE;
   END IF;

   SELECT MAX(F.CDINSCESTA), MAX(F.CDINSCMUNI)
     INTO V_CDINSCESTA, V_CDINSCMUNI
     FROM FILIAL F, INSCRESTAD I
    WHERE (F.CDFILIAL   = P_CDFILIRECE)
      AND (F.CDEMPRESA  = P_CDEMPRESA)
      AND (I.CDINSCESTA = F.CDINSCESTA)
      AND (I.CDEMPRESA  = F.CDEMPRESA)
      AND (NVL(P_DTENTRSAID, TRUNC(SYSDATE)) >= NVL(NVL(I.DTINIVIGIEST, P_DTENTRSAID), TRUNC(SYSDATE)))
      AND (NVL(P_DTENTRSAID, TRUNC(SYSDATE)) <= NVL(NVL(I.DTFIMVIGIEST, P_DTENTRSAID), TRUNC(SYSDATE)));

   IF (V_CDINSCESTA IS NULL) OR (V_CDINSCMUNI IS NULL) THEN
      SELECT MAX(E.CDINSCESTA), MAX(E.CDINSCMUNI)
        INTO V_CDINSCESTA, V_CDINSCMUNI
        FROM EMPRCNTRFIL E, INSCRESTAD I
       WHERE (E.CDFILIAL  = P_CDFILIRECE)
         AND (E.CDEMPRESA = P_CDEMPRESA)
         AND (E.CDINSCESTA = I.CDINSCESTA)
         AND (E.CDEMPRESA  = I.CDEMPRESA)
         AND (NVL(P_DTENTRSAID, TRUNC(SYSDATE)) >= NVL(NVL(I.DTINIVIGIEST, P_DTENTRSAID), TRUNC(SYSDATE)))
         AND (NVL(P_DTENTRSAID, TRUNC(SYSDATE)) <= NVL(NVL(I.DTFIMVIGIEST, P_DTENTRSAID), TRUNC(SYSDATE)));
   END IF;

   IF (V_CDINSCESTA IS NULL) OR (V_CDINSCMUNI IS NULL) THEN
      SELECT MAX(CDINSCESTA), MAX(CDINSCMUNI)
        INTO V_CDINSCESTA, V_CDINSCMUNI
        FROM FILIAL
       WHERE (CDFILIAL  = P_CDFILIRECE)
         AND (CDEMPRESA = P_CDEMPRESA);
   END IF;

   IF P_IDSISTEMA IN ('DFE') THEN
      V_IDDOCFISCENT := 'S';
   ELSE
      V_IDDOCFISCENT := 'N';
   END IF;

   IF P_IDSISTEMA IN ('EST') THEN
      V_IDDOCESTOQUE := 'S';
   ELSE
      V_IDDOCESTOQUE := 'N';
   END IF;

   IF P_NRLANCTONF IS NULL THEN
      NOVO_CODIGO('LANCNF/FILIAL' || P_CDFILIAL, V_NRLANCTONF);
      V_NRLANCTONF := SUBSTR(V_NRLANCTONF, 11, 10);
   ELSE
      V_NRLANCTONF := P_NRLANCTONF;
   END IF;


   IF P_ATUAESTOQUE = 'S' THEN
      IF P_NRLANCESTQ IS NULL THEN
         NOVO_CODIGO('LANCTOEST' || P_CDFILIAL, V_NRLANCESTQ);
         V_NRLANCESTQ := SUBSTR(V_NRLANCESTQ, 11, 10);
      ELSE
      V_NRLANCESTQ := P_NRLANCESTQ;
      END IF;
   ELSE
      V_NRLANCESTQ := '';
   END IF;

   INSERT INTO NOTAFISCAL
      (CDFILIAL,     NRLANCTONF,   CDEMPRESA,    CDINSCESTA,   CDINSCMUNI,
       CDFILIEMIT,   CDFILIRECE,   IDTPIJURNF,   NRINSJURNF,   CDFORNECED,
       CDCLIENTE,    IDBLOQUEADO,  IDDOCFISCAL,  IDDOCFINANC,  DTENTRSAID,
       VRPRODUTOS,   VRNOTAFISC,   NRPLACAVEIC,  SGUFVEIC,     IDPGTOFRETE,
       DSQTDEVOLU,   DSESPECVOLU,  DSMARCAVOLU,  DSNUMEVOLU,   VRPBRUTOVOLU,
       VRPLIQUVOLU,  IDENTRSAIDOP, CDTIPOOPER,   CDSERIEENTR,  IDTPIJURTRAN,
       NRINSJURTRAN, IDDOCCONTAB,  IDSTATUS,     IDDOCFATUR,   IDDOCFISCENT,
       IDDOCFISCSAI, IDDOCESTOQUE, IDSISTEMA,    CDMODDOCFISC, IDORIGDESTNF,
       NRAPURACFAT,  CDINTEGRACAO, DTLANCAMENNF, NRNOTAFISC, DTEMISSAO, NRACESSONFE)
   VALUES
      (P_CDFILIAL,     V_NRLANCTONF,   P_CDEMPRESA,    V_CDINSCESTA,   V_CDINSCMUNI,
       V_CDFILIEMIT,   P_CDFILIRECE,   V_IDTPIJURNF,   V_NRINSJURNF,   V_CDFORNECED,
       V_CDCLIENTE,    'N',            'S',            'S',            TO_DATE(P_DTENTRSAID),
       0,              P_VRNOTAFISC,   P_NRPLACAVEIC,  P_SGUFVEIC,     P_IDPGTOFRETE,
       P_DSQTDEVOLU,   P_DSESPECVOLU,  P_DSMARCAVOLU,  P_DSNUMEVOLU,   P_VRPBRUTOVOLU,
       P_VRPLIQUVOLU,  'E',            P_CDTIPOOPER,   P_CDSERIEENTR,  P_IDTPIJURTRAN,
       P_NRINSJURTRAN, 'S',            'P',            V_IDDOCFATUR,   'N',
       V_IDDOCFISCENT, V_IDDOCESTOQUE, P_IDSISTEMA,    V_CDMODDOCFISC, V_IDORIGDESTNF,
       P_NRAPURACFAT,  P_NRLANCORIG,   P_DTLANCAMENNF, P_NRNOTAFISC, P_DTEMISSAO, P_NRACESSONFE);

   P_PRXLANCTONF := V_NRLANCTONF;
   P_PRXLANCESTQ := V_NRLANCESTQ;
END;
/


-- VERSAO --
BEGIN ATUVEROBJ('GERA_NOTAFISCAL_CORPO_ENTRADA','6.05.XXX'); END;
/

