/******************************************************************************/
/***************************** GRAVA PEDIDO ***********************************/
create or replace PROCEDURE GRAVA_PEDIDO (P_CDFILIAL       IN VARCHAR2, 
                                          P_NRPEDIDO       IN VARCHAR2, 
                                          P_CDCLIENTE      IN VARCHAR2, 
                                          P_CDMODPGTO      IN VARCHAR2, 
                                          P_DTPEDIDO       IN DATE, 
                                          P_DTENTPED       IN DATE, 
                                          P_NRINSJURTRAN   IN VARCHAR2, 
                                          P_IDTPIJURTRAN   IN VARCHAR2, 
                                          P_CDOPELANPED    IN VARCHAR2, 
                                          P_CDFILIALPED    IN VARCHAR2, 
                                          P_CDTIPORECE     IN VARCHAR2, 
                                          P_CDVENDEDOR     IN VARCHAR2, 
                                          P_CDTIPOOPER     IN VARCHAR2, 
                                          P_CDPRECOBASE    IN VARCHAR2, 
                                          P_CDFILLANCA     IN VARCHAR2, 
                                          P_CDFILSOLI      IN VARCHAR2, 
                                          P_NRSOLICMP      IN VARCHAR2, 
                                          P_CDTIPOEDITAL   IN VARCHAR2, 
                                          P_NREMPENHO      IN VARCHAR2, 
                                          P_CDFILIORCEVE   IN VARCHAR2 DEFAULT NULL, 
                                          P_NRORCAEVENTO   IN VARCHAR2 DEFAULT NULL, 
                                          P_NRSEQORCAEVE   IN VARCHAR2 DEFAULT NULL, 
                                          P_CDDEPORCAEVE   IN VARCHAR2 DEFAULT NULL, 
                                          P_NRSEQORCAMENTO IN NUMBER   DEFAULT NULL, 
                                          P_CDMENSERRO  OUT VARCHAR2) 
 
AS 
   V_CTREGISTRO      NUMBER; 
   V_CTPARAM         NUMBER; 
   V_DTENTPED        DATE; 
   V_DTPEDIDO        DATE; 
   V_NRDIASENTPED    VARCHAR2(4); 
   V_NRSEQDIAPROD    VARCHAR2(4); 
 
   V_IDSITPEDIDO     PEDIDO.IDSITPEDIDO%TYPE; 
   V_CDCLIENTE       PEDIDO.CDCLIENTE%TYPE; 
   V_CDMODPGTO       PEDIDO.CDMODPGTO%TYPE; 
   V_CDVENDEDOR      PEDIDO.CDVENDEDOR%TYPE; 
   V_CDPRECOBASE     PEDIDO.CDPRECOBASE%TYPE; 
 
   V_IDTPFORPGTOMOD  MODPGTO.IDTPFORPGTOMOD%TYPE; 
   V_NRDIAPGFIXMOD   MODPGTO.NRDIAPGFIXMOD%TYPE; 
   V_NRPARCPGTOMOD   MODPGTO.NRPARCPGTOMOD%TYPE; 
   V_CDFORMPGTO      MODPGTO.CDFORMPGTO%TYPE; 
   V_ERRO            VARCHAR2(400); 
   V_CDTIPOOPER      PARAMPEDIDO.CDTIPOOPER%TYPE; 
   V_IDOBRTPOPPED    PARAMPEDIDO.IDOBRTPOPPED%TYPE; 
   V_CDOPERPED       PARAMPEDIDO.CDTIPOOPER%TYPE; 
   V_CDCLIENTEORIG   PEDIDO.CDCLIENTE%TYPE; 
   V_CDMODPGTOORIG   PEDIDO.CDMODPGTO%TYPE; 
   V_CDVENDEDORORIG  PEDIDO.CDVENDEDOR%TYPE; 
   V_CDCLIENTEDEST   PEDIDO.CDCLIENTE%TYPE; 
   V_CDMODPGTODEST   PEDIDO.CDMODPGTO%TYPE; 
   V_CDVENDEDORDEST  PEDIDO.CDVENDEDOR%TYPE; 
   V_DTVALIPED       PEDIDO.DTVALIPED%TYPE; 
   V_IDUTVALIPED     PARAMPEDIDO.IDUTVALIPED%TYPE; 
   V_QTDIAVALIPED    PARAMPEDIDO.QTDIAVALIPED%TYPE; 
   V_CDROTATRANSPED  VARCHAR2(6); 
   V_IDORIGDESTOP    VARCHAR2(1); 
   V_NRINSJURTRAN    VARCHAR2(20); 
   V_CDTIPTRANS      VARCHAR2(4); 
   V_IDPGFRETTRA     VARCHAR2(1); 
 
BEGIN 
   P_CDMENSERRO := 'OK'; 
   V_CDOPERPED := NULL; 
   V_ERRO := NULL; 
   V_CDROTATRANSPED := NULL; 
   IF P_CDFILIAL IS NULL THEN 
      V_ERRO := 'CDFILIAL;'; 
   ELSE 
      SELECT COUNT(CDFILIAL) INTO V_CTREGISTRO 
      FROM FILIAL 
      WHERE CDFILIAL = P_CDFILIAL; 
 
      IF V_CTREGISTRO = 0 THEN 
         V_ERRO := V_ERRO || 'CDFILIAL01;'; 
      END IF; 
   END IF; 
    
 
   IF NOT(P_CDFILIALPED IS NULL)THEN 
      SELECT COUNT(*) INTO V_CTPARAM 
      FROM PARAMPEDIDO 
      WHERE CDFILIAL = P_CDFILIALPED; 
 
      IF(V_CTPARAM > 0)THEN 
         SELECT CDCLIENTE, CDMODPGTO, CDVENDEDOR, NRDIASENTPED, 
             DECODE(IDLANCPED, '3', '2', IDLANCPED), CDTIPOOPER, IDOBRTPOPPED, 
             IDUTVALIPED, QTDIAVALIPED
         INTO V_CDCLIENTEDEST, V_CDMODPGTODEST, V_CDVENDEDORDEST, V_NRDIASENTPED,
              V_IDSITPEDIDO,  V_CDTIPOOPER, V_IDOBRTPOPPED, V_IDUTVALIPED, V_QTDIAVALIPED
         FROM PARAMPEDIDO
         WHERE
            CDFILIAL = P_CDFILIALPED;
      END IF;
   END IF;

   SELECT COUNT(*) INTO V_CTPARAM
   FROM PARAMPEDIDO
   WHERE CDFILIAL = P_CDFILIAL;

   IF V_CTPARAM > 0 THEN
      SELECT CDCLIENTE, CDMODPGTO, CDVENDEDOR, NRDIASENTPED,
             DECODE(IDLANCPED, '3', '2', IDLANCPED), CDTIPOOPER, IDOBRTPOPPED,
             IDUTVALIPED, QTDIAVALIPED
      INTO   V_CDCLIENTEORIG, V_CDMODPGTOORIG, V_CDVENDEDORORIG, V_NRDIASENTPED,
             V_IDSITPEDIDO,  V_CDTIPOOPER, V_IDOBRTPOPPED, V_IDUTVALIPED, V_QTDIAVALIPED
      FROM PARAMPEDIDO
      WHERE CDFILIAL = P_CDFILIAL;
   END IF;

   IF(V_IDOBRTPOPPED = 'S')THEN
      IF((V_CDTIPOOPER  IS NULL)AND(P_CDTIPOOPER IS NULL)) THEN
         V_ERRO := V_ERRO || 'CDTIPOOPER;';
      ELSE
         IF NOT(P_CDTIPOOPER IS NULL)THEN
            V_CDOPERPED :=  P_CDTIPOOPER; 
         ELSE 
            V_CDOPERPED := V_CDTIPOOPER; 
         END IF; 
      END IF; 
   END IF; 
 
   IF P_DTPEDIDO IS NULL THEN 
      V_DTPEDIDO := TO_DATE(TO_CHAR(SYSDATE, 'DD/MM/YYYY')); 
   ELSE 
      V_DTPEDIDO := TO_DATE(TO_CHAR(P_DTPEDIDO, 'DD/MM/YYYY')); 
   END IF; 
 
   IF P_DTENTPED IS NULL THEN 
      V_DTENTPED := TO_DATE(TO_CHAR(SYSDATE + TO_NUMBER(NVL(V_NRDIASENTPED, 0)), 'DD/MM/YYYY')); 
   ELSE 
      V_DTENTPED := TO_DATE(TO_CHAR(P_DTENTPED, 'DD/MM/YYYY')); 
   END IF; 
 
   IF V_IDUTVALIPED  = 'S' THEN 
      IF V_QTDIAVALIPED > 0 THEN 
         V_DTVALIPED := SYSDATE + TO_NUMBER( NVL(V_QTDIAVALIPED,0)); 
      ELSE 
         V_DTVALIPED := SYSDATE; 
      END IF; 
   ELSE 
      V_DTVALIPED := NULL; 
   END IF; 
 
   IF (P_NRPEDIDO IS NULL) OR 
      (P_NRPEDIDO = '') THEN 
      V_ERRO := V_ERRO || 'NRPEDIDO;'; 
   ELSE 
      SELECT COUNT(*) INTO V_CTREGISTRO 
      FROM PEDIDO 
      WHERE CDFILIAL = P_CDFILIAL 
        AND NRPEDIDO = P_NRPEDIDO; 
 
      IF V_CTREGISTRO > 0 THEN 
         V_ERRO := V_ERRO || 'NRPEDIDO01;'; 
      END IF; 
   END IF; 
 
   IF NOT P_CDCLIENTE IS NULL THEN 
      V_CDCLIENTE := P_CDCLIENTE; 
   ELSE 
      IF NOT(V_CDCLIENTEDEST IS NULL) THEN 
         V_CDCLIENTE := V_CDCLIENTEDEST; 
      ELSE 
         V_CDCLIENTE := V_CDCLIENTEORIG; 
      END IF; 
   END IF; 
    
   BEGIN 
     SELECT NRINSJURTRAN, CDTIPTRANS, IDPGFRETTRANCLI 
       INTO V_NRINSJURTRAN, V_CDTIPTRANS, V_IDPGFRETTRA 
      FROM CLIENTE 
        WHERE CDCLIENTE = V_CDCLIENTE; 
    EXCEPTION  
      WHEN NO_DATA_FOUND THEN 
        V_NRINSJURTRAN := NULL;  
        V_CDTIPTRANS   := NULL; 
        V_IDPGFRETTRA  := NULL; 
    END; 
 
   SELECT COUNT(*) INTO V_CTREGISTRO 
   FROM CLIENTE 
   WHERE CDCLIENTE = V_CDCLIENTE; 
 
   IF V_CTREGISTRO > 0 THEN 
      SELECT NVL(CDMODPGTO, V_CDMODPGTO) 
      INTO V_CDMODPGTO 
      FROM CLIENTE 
      WHERE CDCLIENTE = V_CDCLIENTE; 
   ELSE 
      V_ERRO := V_ERRO || 'CDCLIENTE;'; 
   END IF; 
    
   IF NOT P_CDVENDEDOR IS NULL THEN 
      V_CDVENDEDOR := P_CDVENDEDOR; 
   ELSE 
      IF NOT V_CDVENDEDORDEST IS NULL  THEN 
         V_CDVENDEDOR :=  V_CDVENDEDORDEST; 
      ELSE 
         V_CDVENDEDOR := V_CDVENDEDORORIG; 
      END IF; 
   END IF; 
 
   IF V_CDVENDEDOR IS NULL THEN 
      V_ERRO := V_ERRO || 'CDVENDEDOR;'; 
   END IF; 
 
   IF NOT P_CDMODPGTO IS NULL THEN 
      V_CDMODPGTO := P_CDMODPGTO; 
   ELSE 
      IF NOT V_CDMODPGTODEST IS NULL THEN 
         V_CDMODPGTO :=  V_CDMODPGTODEST; 
      ELSE 
         V_CDMODPGTO :=  V_CDMODPGTOORIG; 
      END IF; 
   END IF; 
 
   IF NOT V_CDMODPGTO IS NULL THEN 
      SELECT COUNT(*) INTO V_CTREGISTRO 
      FROM MODPGTO 
      WHERE CDMODPGTO = V_CDMODPGTO; 
 
      IF V_CTREGISTRO > 0 THEN 
         SELECT MAX(IDTPFORPGTOMOD), MAX(NRDIAPGFIXMOD), MAX(NRPARCPGTOMOD), MAX(CDFORMPGTO), NVL(MAX(DECODE(IDLANCPEDMOD, '3', '2', IDLANCPEDMOD)), V_IDSITPEDIDO) 
         INTO V_IDTPFORPGTOMOD, V_NRDIAPGFIXMOD, V_NRPARCPGTOMOD, V_CDFORMPGTO, V_IDSITPEDIDO 
         FROM MODPGTO 
         WHERE CDMODPGTO = V_CDMODPGTO; 
      ELSE 
         V_ERRO := V_ERRO || 'CDMODPGTO01;'; 
      END IF; 
   ELSE 
      V_ERRO := V_ERRO || 'CDMODPGTO;'; 
   END IF; 
 
   IF V_IDSITPEDIDO IS NULL THEN 
      V_ERRO := V_ERRO || 'IDSITPEDIDO;'; 
   END IF; 
 
   IF NOT P_CDPRECOBASE IS NULL THEN 
      V_CDPRECOBASE := P_CDPRECOBASE; 
   ELSE 
      SELECT COUNT(CDPRECOBASE) INTO V_CTREGISTRO 
      FROM PRECOBASECLI 
      WHERE CDCLIENTE = V_CDCLIENTE 
        AND IDPRBASEPRIN = 'S'; 
 
      IF V_CTREGISTRO > 0 THEN 
         SELECT MAX(CDPRECOBASE) INTO V_CDPRECOBASE 
         FROM PRECOBASECLI 
         WHERE CDCLIENTE = V_CDCLIENTE 
           AND IDPRBASEPRIN = 'S'; 
      ELSE 
         SELECT COUNT(CDPRECOBASE) INTO V_CTREGISTRO 
         FROM VENDEDOR 
         WHERE CDVENDEDOR = V_CDVENDEDOR; 
 
         IF V_CTREGISTRO > 0 THEN 
            SELECT MAX(CDPRECOBASE) INTO V_CDPRECOBASE 
            FROM VENDEDOR 
            WHERE CDVENDEDOR = V_CDVENDEDOR; 
         ELSE 
            SELECT COUNT(CDPRECOBASE) INTO V_CTREGISTRO 
            FROM PRECOUTI 
            WHERE CDFILIAL  = P_CDFILIAL 
              AND CDMODPGTO = V_CDMODPGTO; 
 
            IF V_CTREGISTRO > 0 THEN 
               SELECT MAX(CDPRECOBASE) INTO V_CDPRECOBASE 
               FROM PRECOUTI 
               WHERE CDFILIAL  = P_CDFILIAL 
                 AND CDMODPGTO = V_CDMODPGTO; 
            ELSE 
               V_ERRO := V_ERRO || 'CDPRECOBASE;'; 
            END IF; 
         END IF; 
      END IF; 
   END IF; 
 
   IF NOT V_ERRO IS NULL THEN 
      P_CDMENSERRO := V_ERRO; 
   END IF; 
 
   V_NRSEQDIAPROD := NULL; 
   IF NOT P_NRORCAEVENTO IS NULL THEN 
      SELECT MAX(NRSEQDIAPROD) INTO V_NRSEQDIAPROD 
      FROM PEDIDO 
      WHERE CDFILIAL = P_CDFILIAL 
        AND DTENTPED = TO_DATE(TO_CHAR(P_DTENTPED, 'DD/MM/YYYY')); 
 
      IF NOT V_NRSEQDIAPROD IS NULL THEN 
         V_NRSEQDIAPROD := LPAD(TO_CHAR(TO_NUMBER(V_NRSEQDIAPROD) + 1), 3, '0'); 
      ELSE 
         V_NRSEQDIAPROD := '001'; 
      END IF; 
   END IF; 
 
   IF NOT (V_CDTIPOOPER IS NULL) AND (V_CDTIPOOPER <> '') THEN 
      SELECT IDORIGDESTOP INTO V_IDORIGDESTOP 
        FROM TIPOOPERAC 
       WHERE CDTIPOOPER = V_CDTIPOOPER AND 
             IDENTRSAIDOP = 'S'; 
 
      IF V_IDORIGDESTOP = 'C' THEN 
         SELECT CDROTATRANS INTO V_CDROTATRANSPED 
           FROM CLIENROTA 
          WHERE CDCLIENTE = V_CDCLIENTE AND 
                IDROTAPRINCLI = 'S'; 
 
      END IF; 
 
      IF V_IDORIGDESTOP = 'L' THEN 
         SELECT CDROTATRANS INTO V_CDROTATRANSPED 
           FROM FILIALROTA 
          WHERE CDFILIAL = P_CDFILIAL AND 
                IDROTAPRINCFILI = 'S'; 
      END IF; 
   END IF; 

   IF P_CDMENSERRO = 'OK' THEN
      INSERT INTO PEDIDO
         (CDFILIAL,     NRPEDIDO,     IDSITPEDIDO,   CDCLIENTE,   IDPRIOPED,     DTPEDIDO,
          DTENTPED,     CDMODPGTO,    IDTPFORPGTPED, CDFORMPGTO,  NRDIAPGFIXPED, NRPARCPGTOPED,
          IDTPIJURTRAN, NRINSJURTRAN, CDTIPTRANS, IDPGFRETTRAN, DTHRLANPED,    CDOPELANPED, CDFILIALPED,   CDTIPORECE,
          CDVENDEDOR,   IDENTRSAIDOP, CDTIPOOPER,    CDPRECOBASE, DTVALIPED, CDFILLANCA, CDFILSOLI,
          NRSOLICMP,    CDTIPOEDITAL, CDFILIALORC,   NRORCAEVENTO,  NRSEQORCAEVE,
          CDDEPTO, NREMPENHO, NRSEQDIAPROD, CDROTATRANSPED, NRSEQORCAMENTO, IDUTDIAPAGPED)
      VALUES
         (P_CDFILIAL,     P_NRPEDIDO,     V_IDSITPEDIDO,    V_CDCLIENTE,   '3',             V_DTPEDIDO,
          V_DTENTPED,     V_CDMODPGTO,    V_IDTPFORPGTOMOD, V_CDFORMPGTO,  V_NRDIAPGFIXMOD, V_NRPARCPGTOMOD,
          P_IDTPIJURTRAN, V_NRINSJURTRAN, V_CDTIPTRANS, V_IDPGFRETTRA,  SYSDATE,          P_CDOPELANPED, P_CDFILSOLI,   P_CDTIPORECE,
          V_CDVENDEDOR,   'S',            V_CDOPERPED,     V_CDPRECOBASE, V_DTVALIPED,      P_CDFILLANCA,   P_CDFILSOLI,
          P_NRSOLICMP,    P_CDTIPOEDITAL,   P_CDFILIORCEVE, P_NRORCAEVENTO, P_NRSEQORCAEVE,
          P_CDDEPORCAEVE, P_NREMPENHO, V_NRSEQDIAPROD, V_CDROTATRANSPED, P_NRSEQORCAMENTO, 'I');

      FOR C_PRAZO IN
         (SELECT NRDIASMODPGTO, VRPERCMODPGTO
          FROM PRAZOMODPGTO
          WHERE
             CDMODPGTO = V_CDMODPGTO
          ) LOOP

          INSERT INTO PRAZOPGTOPED( CDFILIAL, NRPEDIDO, NRDIASPGTOPED, VRPERCPGTOPED)
                 VALUES( P_CDFILIAL, P_NRPEDIDO, C_PRAZO.NRDIASMODPGTO, C_PRAZO.VRPERCMODPGTO);
      END LOOP;
   END IF;
END;