/******************************************************************************/
/***************************** REPLICA PEDIDO ***********************************/
CREATE OR REPLACE PROCEDURE REPLICA_PEDIDO (P_CDFILIAL        IN VARCHAR2,
                                            P_NRPEDIDONEW     IN VARCHAR2,
                                            P_CDCLIENTE       IN VARCHAR2,
                                            P_CDFILIDEST      IN VARCHAR2,
                                            P_DATAPEDIDO      IN DATE,
                                            P_DATAENTREGA     IN DATE,
                                            P_DATAVALIDADE    IN DATE,
                                            P_NRORDCOMPRA     IN VARCHAR2,
                                            P_CDFILIALPEDORIG IN VARCHAR2,
                                            P_NRPEDIDO        IN VARCHAR2,
                                            P_CDOPERADOR      IN VARCHAR2,
                                            P_CDMENSERRO  OUT VARCHAR2)

AS
   V_CTREGISTRO      NUMBER;

   V_DTENTPED        DATE;
   V_DTPEDIDO        DATE;
   V_SEQUENCIAL      VARCHAR2(20);
   V_NRDIASENTPED    VARCHAR2(4);

   V_CDVENDEDOR      PARAMPEDIDO.CDVENDEDOR%TYPE;
   V_IDLANCPED       PARAMPEDIDO.IDLANCPED%TYPE;
   V_IDUTRESELOTE    PARAMPEDIDO.IDUTRESELOTE%TYPE;
   V_CDTIPOOPER      PARAMPEDIDO.CDTIPOOPER%TYPE;
   V_IDEXIBIMPITPED  PARAMPEDIDO.IDEXIBIMPITPED%TYPE;
   V_IDSITPEDIDO     PEDIDO.IDSITPEDIDO%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_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);
   V_CDPRECOBASE     VARCHAR2(02);

BEGIN
   P_CDMENSERRO := 'OK';
   V_ERRO := NULL;
   V_CDROTATRANSPED := NULL;
   V_CDPRECOBASE    := NULL;

   BEGIN
      SELECT COUNT(*)
        INTO V_CTREGISTRO
        FROM PARAMPEDIDO
       WHERE CDFILIAL = P_CDFILIAL;
   EXCEPTION
      WHEN NO_DATA_FOUND THEN
         V_CTREGISTRO := 0;
   END;

   IF V_CTREGISTRO = 0 THEN
      P_CDMENSERRO := 'FILIAL_N_PARAM';
   END IF;

   IF V_CTREGISTRO > 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;

      SELECT MAX(PC.CDPRECOBASE)
        INTO V_CDPRECOBASE
        FROM PRECOBASECLI PC
       WHERE (PC.CDCLIENTE    = P_CDCLIENTE)
         AND (PC.IDPRBASEPRIN = 'S');
   END IF;

   BEGIN
      SELECT CDVENDEDOR, IDLANCPED, IDUTRESELOTE, CDTIPOOPER, NVL(IDEXIBIMPITPED, 'N')
        INTO V_CDVENDEDOR, V_IDLANCPED, V_IDUTRESELOTE, V_CDTIPOOPER, V_IDEXIBIMPITPED
        FROM PARAMPEDIDO
       WHERE CDFILIAL = P_CDFILIAL;
   EXCEPTION
      WHEN NO_DATA_FOUND THEN
         V_CDVENDEDOR     := NULL;
         V_IDLANCPED      := NULL;
         V_IDUTRESELOTE   := NULL;
         V_CDTIPOOPER     := NULL;
         V_IDEXIBIMPITPED := NULL;
   END;

   IF V_CDVENDEDOR IS NULL THEN
      P_CDMENSERRO := 'VENDEDOR_INF';
   ELSE
      IF ((V_CDTIPOOPER IS NULL) AND (V_IDEXIBIMPITPED = 'S')) THEN
         P_CDMENSERRO := 'TIPOOPER_INF';
      END IF;
      IF V_CDMODPGTOORIG IS NULL THEN
         P_CDMENSERRO := 'MOD_PAGAMENTO';
      END IF;
   END IF;

   IF P_CDMENSERRO = 'OK' THEN

      --NOVO_CODIGO_SC('PEDIDO'||P_CDFILIAL, V_SEQUENCIAL);
      --V_SEQUENCIAL := SUBSTR(V_SEQUENCIAL, 11,10);

      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, IDPEDREPLICA, NRPEDREPLICA, IDPEDINDUSTRIA,
             NRORDEMCMPPED)
      SELECT P_CDFILIAL,   P_NRPEDIDONEW, V_IDSITPEDIDO,   P_CDCLIENTE, IDPRIOPED,     P_DATAPEDIDO,
             P_DATAENTREGA,V_CDMODPGTOORIG,    IDTPFORPGTPED, CDFORMPGTO,  NRDIAPGFIXPED, NRPARCPGTOPED,
             IDTPIJURTRAN, NRINSJURTRAN, CDTIPTRANS,  IDPGFRETTRAN,   SYSDATE,      P_CDOPERADOR, P_CDFILIDEST,   CDTIPORECE,
             V_CDVENDEDOR, IDENTRSAIDOP, V_CDTIPOOPER,NVL(V_CDPRECOBASE, CDPRECOBASE), P_DATAVALIDADE, CDFILLANCA, '',
             '',    CDTIPOEDITAL, '',   '',  '',
             CDDEPTO, NREMPENHO, NRSEQDIAPROD, CDROTATRANSPED, NRSEQORCAMENTO, 'S', P_NRPEDIDO, IDPEDINDUSTRIA,
             P_NRORDCOMPRA
        FROM PEDIDO
       WHERE CDFILIAL = P_CDFILIALPEDORIG AND
             NRPEDIDO = P_NRPEDIDO;

   END IF;
END;
/

-- VERSAO --
BEGIN ATUVEROBJ('REPLICA_PEDIDO','6.05.xxx'); END;
/


