CREATE OR REPLACE PROCEDURE GERA_NOTAFISCAL_CORPO
   (P_CDFILIAL     IN  FILIAL.CDFILIAL%TYPE,
    P_CDFILIEMIT   IN  FILIAL.CDFILIAL%TYPE,
    P_CDEMPRESA    IN  EMPRESA.CDEMPRESA%TYPE,
    P_CDTIPOOPER   IN  TIPOOPERAC.CDTIPOOPER%TYPE,
    P_CDSERIE      IN  SERIENF.CDSERIE%TYPE,
    P_CDCLIENTE    IN  CLIENTE.CDCLIENTE%TYPE,
    P_DTENTRSAID   IN  NOTAFISCAL.DTENTRSAID%TYPE,
    P_IDSISTEMA    IN  NOTAFISCAL.IDSISTEMA%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_IDGEROUTDOCU IN  NOTAFISCAL.IDGEROUTDOCU%TYPE    DEFAULT NULL,
    P_DTCOMPFATUR  IN  NOTAFISCAL.DTCOMPFATUR%TYPE     DEFAULT NULL,
    P_CDPTOVNDFATURNF  IN  NOTAFISCAL.CDPTOVNDFATURNF%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_CDFILIRECE   VARCHAR2(21);
   V_CDINSCESTA   VARCHAR2(16);
   V_CDINSCMUNI   VARCHAR2(16);
   V_IDDOCFATUR   VARCHAR2(01);
   V_IDDOCFISCSAI VARCHAR2(01);
   V_IDDOCESTOQUE VARCHAR2(01);
   V_CDMODDOCFISC VARCHAR2(02);
   V_NRLANCTONF   VARCHAR2(20);
   V_NRLANCESTQ   VARCHAR2(20);
   V_CDVIATURA    VARCHAR2(04);
   V_DTINIAPURAC  DATE;
   V_DTFIMAPURAC  DATE;
   V_IDPRESENCIAL VARCHAR2(01);
BEGIN
   V_CDMODDOCFISC := NULL;
   V_CDCLIENTE    := NULL;
   V_CDFORNECED   := NULL;
   V_CDFILIRECE   := NULL;

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

   SELECT MIN(CDMODDOCFISC) CDMODDOCFISC
     INTO V_CDMODDOCFISC
     FROM MODDOCFISCTPOP
    WHERE CDTIPOOPER   = P_CDTIPOOPER
      AND IDENTRSAIDOP = 'S';

   IF V_IDORIGDESTNF IS NULL THEN
      V_IDORIGDESTNF := 'C';
   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_CDFILIRECE
      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_CDFILIEMIT)
      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_CDFILIEMIT)
         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 (P_IDSISTEMA = 'FAI') THEN
      SELECT MAX(CDINSCESTA), MAX(CDINSCMUNI)
        INTO V_CDINSCESTA, V_CDINSCMUNI
        FROM CLIENTE
       WHERE CDCLIENTE = P_CDCLIENTE; 
   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_CDFILIEMIT)
         AND (CDEMPRESA = P_CDEMPRESA);
   END IF;


   IF P_IDSISTEMA IN ('FAI','FAC','FAS') THEN
      V_IDDOCFATUR := 'S';
   ELSE
      V_IDDOCFATUR := 'N';
   END IF;

   IF P_IDSISTEMA IN ('DFS') THEN
      V_IDDOCFISCSAI := 'S';
   ELSE
      V_IDDOCFISCSAI := '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_NT('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_NT('LANCTOEST' || P_CDFILIAL, V_NRLANCESTQ);
         V_NRLANCESTQ := SUBSTR(V_NRLANCESTQ, 11, 10);
      ELSE
      V_NRLANCESTQ := P_NRLANCESTQ;
      END IF;
   ELSE
      V_NRLANCESTQ := '';
   END IF;
   
   BEGIN
      SELECT DTINIAPURAC, DTFIMAPURAC 
        INTO V_DTINIAPURAC, V_DTFIMAPURAC
        FROM CTRLAPURFAT
       WHERE CDFILIAL = P_CDFILIAL
         AND NRAPURACFAT = P_NRAPURACFAT
         AND CDFILIAPUR = P_CDFILIEMIT;
   EXCEPTION 
      WHEN NO_DATA_FOUND THEN 
         V_DTINIAPURAC := NULL;
         V_DTFIMAPURAC := NULL;
   END;

   IF P_NRAPURACFAT IS NOT NULL THEN
      V_IDPRESENCIAL := '1';
   ELSE
      V_IDPRESENCIAL := '9';
   END IF;

   BEGIN
      SELECT CDVIATURA
        INTO V_CDVIATURA
        FROM VIATURA
       WHERE NRPLACA = P_NRPLACAVEIC;
   EXCEPTION
      WHEN NO_DATA_FOUND THEN
         V_CDVIATURA := NULL;
   END;

   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,   CDSERIE,      IDTPIJURTRAN,
       NRINSJURTRAN, IDDOCCONTAB,  IDSTATUS,     IDDOCFATUR,   IDDOCFISCENT,
       IDDOCFISCSAI, IDDOCESTOQUE, IDSISTEMA,    CDMODDOCFISC, IDORIGDESTNF,
       NRAPURACFAT,  CDINTEGRACAO, DTLANCAMENNF, IDGEROUTDOCU, DTCOMPFATUR,
       CDPTOVNDFATURNF, DTINIREFEFAT, DTFINREFEFAT, CDVIATURA, IDPRESENCIAL)
   VALUES
      (P_CDFILIAL,     V_NRLANCTONF,   P_CDEMPRESA,    V_CDINSCESTA,   V_CDINSCMUNI,
       P_CDFILIEMIT,   V_CDFILIRECE,   V_IDTPIJURNF,   V_NRINSJURNF,   V_CDFORNECED,
       V_CDCLIENTE,    'N',            'S',            'S',            TO_DATE(P_DTENTRSAID),
       0,              0,              P_NRPLACAVEIC,  P_SGUFVEIC,     P_IDPGTOFRETE,
       P_DSQTDEVOLU,   P_DSESPECVOLU,  P_DSMARCAVOLU,  P_DSNUMEVOLU,   P_VRPBRUTOVOLU,
       P_VRPLIQUVOLU,  'S',            P_CDTIPOOPER,   P_CDSERIE,      P_IDTPIJURTRAN,
       P_NRINSJURTRAN, 'S',            'P',            V_IDDOCFATUR,   'N',
       V_IDDOCFISCSAI, V_IDDOCESTOQUE, P_IDSISTEMA,    V_CDMODDOCFISC, V_IDORIGDESTNF,
       P_NRAPURACFAT,  P_NRLANCORIG,   P_DTLANCAMENNF, P_IDGEROUTDOCU, P_DTCOMPFATUR,
       P_CDPTOVNDFATURNF, V_DTINIAPURAC, V_DTFIMAPURAC, V_CDVIATURA, V_IDPRESENCIAL);

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