create or replace PROCEDURE EXPORTA_PRODUTO_SAGA 
IS 
   V_CDPRODUTO        VARCHAR2(10); 
   V_CDARVPROD        VARCHAR2(20); 
   V_NMPRODUTO        VARCHAR2(50); 
   V_CODSTE           VARCHAR2(20); 
   V_CDCLASFISC       VARCHAR2(10); 
   V_VRPRAZMAXREC     VARCHAR2(4); 
   V_VRPRAZMAXDEV     VARCHAR2(4); 
   V_VRPRAZVALIDADE   VARCHAR2(4); 
   V_VRPERMARMAZEM    VARCHAR2(4); 
   V_CDGRUPREGARMAZ   VARCHAR2(4); 
   V_VRTEMPMEDESTOQ   VARCHAR2(4); 
   V_VRTEMPMEDEXPED   VARCHAR2(4); 
   V_VRTEMPMEDRECEB   VARCHAR2(4); 
   V_VRSHELFLIFE      VARCHAR2(4); 
   V_VRJANELAFIFO     VARCHAR2(4); 
   V_CDMOTIVOBLOQ     VARCHAR2(2); 
   V_VRJANFIFOTRANS   VARCHAR2(4); 
   V_SGUNIDADE        VARCHAR2(10); 
   V_VRFATOCONV       FLOAT; 
   V_VRPALPRODALT     VARCHAR2(10); 
   V_IDPRODMENSU      VARCHAR2(1); 
   V_VRPERCTOLERANCIA FLOAT; 
   V_IDUNIDPADRSAGA   VARCHAR2(1); 
   V_CODGRPMER        VARCHAR2(20); 
   V_BOLCRGESQREC     NUMBER; 
   V_BOLEMIETQ        NUMBER; 
   V_BOLGERUND        NUMBER; 
   V_BOLMERIDTUNI     NUMBER; 
   V_BOLOTMARZ        NUMBER; 
   V_BOLPESOBR        NUMBER; 
   V_BOLREFDATFABVLD  NUMBER; 
   V_BOLUTLMTPSKU     NUMBER; 
   V_BOLVLDDAT        NUMBER; 
   V_BOLVLDPESMTGVOL  NUMBER; 
   V_BOLVLDVOLMTGVOL  NUMBER; 
   V_BOLMERPES        NUMBER; 
   V_CODGRPTOL        INTEGER; 
   V_CODTIPCRG        INTEGER; 
   V_CODUNDCRN        VARCHAR2(5); 
   V_CODUNDMED        VARCHAR2(10); 
   V_CODUNDMEDSEC     VARCHAR2(10); 
   V_IDCALGSEL        NUMBER; 
   V_IDCMOVOCO        NUMBER; 
   V_IDCTIPMER        NUMBER; 
   V_NOMIMG           VARCHAR2(40); 
   V_VLRUNDVND        NUMBER; 
   V_VLRQTDMINESQ     NUMBER; 
   V_DESSKU           VARCHAR2(30); 
   V_BOLSKUPDR        INTEGER; 
   V_BOLDELCODBRR     INTEGER; 
   V_BOLVLDBRR        INTEGER; 
   V_CODTIPEMB        VARCHAR2(1); 
   V_VALDIMX          NUMBER; 
   V_VALDIMY          NUMBER; 
   V_VALDIMZ          NUMBER; 
   V_VALPES           NUMBER; 
   V_VALPESLIQ        NUMBER; 
   V_VALVOL           NUMBER; 
   V_VALPESEMBSEC     NUMBER; 
   V_CODVLDMER        INTEGER; 
   V_CODPSS           VARCHAR2(20); 
   V_CODMERPRP        VARCHAR2(20); 
   V_VALSHFLFEPRP     VARCHAR2(4); 
   V_CODGRPSTS        VARCHAR2(20); 
   V_CODDSP           VARCHAR2(20); 
   V_BOLPDR           NUMBER;  
   V_VALALT           NUMBER; 
   V_VALCAPMAXEMP     INTEGER; 
   V_VALQTDSKUCPR     INTEGER; 
   V_VALQTDSKULRG     INTEGER; 
   V_CODTIPCARMER1    INTEGER; 
   V_CODTIPCARMER2    INTEGER; 
   V_CODTIPCARMER3    INTEGER; 
   V_CODTIPCARMER4    INTEGER; 
   V_CODTIPCARMER5    INTEGER; 
   V_CODTIPCARMER6    INTEGER; 
   V_CODTIPCARMER7    INTEGER; 
   V_CODTIPCARMER8    INTEGER; 
   V_CODGRPCARLGT     VARCHAR2(20); 
   V_OBSMER           VARCHAR2(300); 
   V_BITBOLMIXUMA     NUMBER; 
   V_BITBOLMIXEND     NUMBER; 
   V_BITBOLMND        NUMBER; 
   V_BOLMERMNS        NUMBER; 
   V_VALPESEMB        NUMBER; 
   V_QTDESQPAD        NUMBER;  
   V_BOLFRCMER        NUMBER; 
   V_FLAPRC           VARCHAR2(5); 
   V_DSCERR           VARCHAR2(300); 
   V_DATULTPRC        DATE; 
   V_IDPRODPESAVEL    VARCHAR2(1); 
   V_ARVMENOR         VARCHAR2(20); 
   V_CDPRODNIVANTE    VARCHAR2(20); 
   V_NIVELANTERIOR    VARCHAR2(10); 
   V_VRFATOCONVANT    FLOAT; 
   V_CODMERANT        VARCHAR2(20); 
   V_SGUNIDADEANT     VARCHAR2(10); 
   V_CODMER           VARCHAR2(20); 
   V_BOLPRODMENSU     BOOLEAN; 
   V_NMFANTPROD       VARCHAR2(40); 
    
   CURSOR C_PRODUTO IS  
      SELECT P.CDPRODUTO, PA.CDARVPROD, TRIM(PA.NMPRODUTO) NMPRODUTO, P.CDCLASFISC, 
             C.CDGRUPREGARMAZ, TO_NUMBER(C.CDMOTIVOBLOQ) AS CDMOTIVOBLOQ, TO_NUMBER(VRPERMARMAZEM) AS VRPERMARMAZEM, 
             TO_NUMBER(VRPRAZMAXREC) AS VRPRAZMAXREC, TO_NUMBER(VRJANELAFIFO) AS VRJANELAFIFO, 
             TO_NUMBER(VRJANFIFOTRANS) AS VRJANFIFOTRANS, TO_NUMBER(VRPRAZVALIDADE) AS VRPRAZVALIDADE, 
             TO_NUMBER(VRSHELFLIFE) AS VRSHELFLIFE, TO_NUMBER(VRTEMPMEDESTOQ) AS VRTEMPMEDESTOQ, 
             TO_NUMBER(VRTEMPMEDEXPED) AS VRTEMPMEDEXPED, TO_NUMBER(VRTEMPMEDRECEB) AS VRTEMPMEDRECEB, 
             TO_NUMBER(VRPRAZMAXDEV) AS VRPRAZMAXDEV, C.IDPRODMENSU, C.VRPERCTOLERANCIA, C.IDUNIDPADRSAGA, 
             P.SGUNIDADE, C.VRFATORSKU, C.VRPALPRODALT,  
             CASE WHEN C.IDUNIDPADRSAGA = 'S' 
                  THEN 1 
                  ELSE 0 
              END AS BOLSKUPDR, 
               
              C.VRLARGURA, C.VRALTURA, C.VRPROFUNDIDADE, C.VRPESO, C.VRPESOLIQ, C.VRVOLUME,  
               
              C.VRPALPRODCOM, C.VRPALPRODLAR, NVL(C.IDPRODPESAVEL, 'N') IDPRODPESAVEL, 
              P.CDPRODNIVANTE, P.CDARVPROD ARVMENOR, SUBSTR(P.NMFANTPROD, 1, 40) NMFANTPROD 
        FROM PRODUTO P, CARACPRODSAGA C, PRODUTO PA 
       WHERE P.CDPRODUTO = C.CDPRODUTO 
         AND P.CDNVPRODUTO = (SELECT MAX(CDNVPRODUTO) FROM PRODUTO) 
         AND P.CDPRODESTO = PA.CDPRODUTO 
         AND P.IDPRODATIVPR = 'S' 
         AND P.NMFANTPROD IS NOT NULL 
         AND ((P.DTEXPPRODSAGA IS NULL) OR ((P.DTULTATUPROD >= P.DTEXPPRODSAGA) OR (C.DTULTATUCARACP >= P.DTEXPPRODSAGA))) 
       ORDER BY P.CDARVPROD; 
       
BEGIN 
   V_BOLCRGESQREC    := 0; 
   V_BOLEMIETQ       := 1; 
   V_BOLGERUND       := 1; 
   V_BOLMERIDTUNI    := 0; 
   V_BOLOTMARZ       := 1; 
   V_BOLPESOBR       := 0; 
   V_BOLREFDATFABVLD := 0; 
   V_BOLUTLMTPSKU    := 1; 
   V_BOLVLDDAT       := 1; 
   V_BOLVLDPESMTGVOL := 1; 
   V_BOLVLDVOLMTGVOL := 1; 
   V_CODGRPTOL       := NULL; 
   V_CODTIPCRG       := 2; 
   V_CODUNDCRN       := NULL; 
   V_CODUNDMED       := '12'; 
   V_CODUNDMEDSEC    := NULL; 
   V_IDCALGSEL       := 1; 
   V_IDCMOVOCO       := NULL; 
   V_IDCTIPMER       := 1; 
   V_NOMIMG          := NULL; 
   V_VLRUNDVND       := 1; 
   V_VLRQTDMINESQ    := 0; 
   V_BOLDELCODBRR    := 0; 
   V_BOLVLDBRR       := 0; 
   V_CODTIPEMB       := '1'; 
   V_VALPESEMBSEC    := 0; 
   V_CODVLDMER       := 3; 
   V_CODPSS          := '1'; 
   V_CODMERPRP       := NULL; 
   V_VALSHFLFEPRP    := NULL; 
   V_CODGRPSTS       := NULL; 
   V_CODDSP          := '201'; 
   V_BOLPDR          := 1; 
   V_VALALT          := 1000; 
   V_VALCAPMAXEMP    := 1; 
   V_CODTIPCARMER1   := 1; 
   V_CODTIPCARMER2   := NULL; 
   V_CODTIPCARMER3   := NULL; 
   V_CODTIPCARMER4   := NULL; 
   V_CODTIPCARMER5   := NULL; 
   V_CODTIPCARMER6   := NULL; 
   V_CODTIPCARMER7   := NULL; 
   V_CODTIPCARMER8   := NULL; 
   V_CODGRPCARLGT    := NULL; 
   V_OBSMER          := NULL; 
   V_BITBOLMIXUMA    := 1; 
   V_BITBOLMIXEND    := 1; 
   V_BITBOLMND       := 1; 
   V_VALPESEMB       := NULL; 
   V_QTDESQPAD       := NULL;  
   V_FLAPRC          := '0'; 
   V_DSCERR          := NULL; 
   V_DATULTPRC       := NULL; 
   V_NIVELANTERIOR   := ' '; 
   V_CODMERANT       := ' '; 
    
   SELECT MAX(CDFILIALSAGA) 
     INTO V_CODSTE 
     FROM PARAMFILIAL; 
    
   OPEN C_PRODUTO; 
   LOOP 
      FETCH C_PRODUTO INTO V_CDPRODUTO, V_CDARVPROD, V_NMPRODUTO, V_CDCLASFISC, 
                           V_CDGRUPREGARMAZ, V_CDMOTIVOBLOQ, V_VRPERMARMAZEM,  
                           V_VRPRAZMAXREC, V_VRJANELAFIFO,  
                           V_VRJANFIFOTRANS, V_VRPRAZVALIDADE,  
                           V_VRSHELFLIFE, V_VRTEMPMEDESTOQ,  
                           V_VRTEMPMEDEXPED, V_VRTEMPMEDRECEB, 
                           V_VRPRAZMAXDEV, V_IDPRODMENSU, V_VRPERCTOLERANCIA, V_IDUNIDPADRSAGA, 
                           V_SGUNIDADE, V_VRFATOCONV, V_VRPALPRODALT,  
                           V_BOLSKUPDR, 
                            
                           V_VALDIMX, V_VALDIMY, V_VALDIMZ, V_VALPES, V_VALPESLIQ, V_VALVOL, 
                            
                           V_VALQTDSKUCPR, V_VALQTDSKULRG, V_IDPRODPESAVEL, 
                            
                           V_CDPRODNIVANTE, V_ARVMENOR, V_NMFANTPROD; 
      EXIT WHEN C_PRODUTO%NOTFOUND; 
       
      IF (V_BOLSKUPDR = 1)  THEN 
         --V_CODMER       := V_CDARVPROD;
         --V_VRFATOCONV := 1;
         V_BOLPRODMENSU := FALSE; 
      END IF; 
       
      V_CODGRPMER := SUBSTR(V_CDARVPROD, 1, 3); 
       
      SELECT DSUNIDADE 
        INTO V_DESSKU 
        FROM UNIDMEDIDA 
       WHERE SGUNIDADE = V_SGUNIDADE; 
        
      V_BOLFRCMER := 0; 
      V_BOLMERMNS := 0; 
      IF V_IDPRODMENSU = 'S' THEN 
         V_BOLMERMNS := 1; 
         V_BOLFRCMER := 1; 
         --V_CODMER    := V_CDARVPROD; 
         V_BOLPRODMENSU := TRUE; 
       
      END IF; 
      
      V_CODMER    := V_CDARVPROD;
       
      V_BOLMERPES := 0; 
      IF V_IDPRODPESAVEL = 'S' THEN 
         V_BOLMERPES := 1; 
      END IF; 
       
      INSERT INTO INT_CADMERCPL(CODMER, CODSTE, CODGRPMER, NOMABRMER, DESMER, BOLCRGESQREC, BOLEMIETQ, BOLGERUND, BOLMERIDTUNI, BOLOTMARZ, 
                                BOLPESOBR, BOLREFDATFABVLD, BOLUTLMTPSKU, BOLVLDDAT, BOLVLDPESMTGVOL, BOLVLDVOLMTGVOL, BOLMERPES, CODGRPREGARM, 
                                CODGRPTOL, CODMTVBLQ, CODNCM, CODTIPCRG, CODUNDCRN, CODUNDMED, CODUNDMEDSEC, IDCALGSEL, IDCMOVOCO, IDCTIPMER, 
                                NOMIMG, NRODIAPRMARM, VALDIAPRZMAXREC, VALDIAPRZMAXRECDEV, VALJANFIF, VALJANFIFTRF, VALPRZVALMER, 
                                VALSHFLFE, VALTEMMEDETQ, VALTEMMEDEXP, VALTEMMEDREC, VLRQTDPDCMER, VLRUNDVND, VLRQTDMINESQ, NOMABRSKU, DESSKU, 
                                BOLSKUPDR, BOLDELCODBRR, BOLVLDBRR, CODBRR, CODTIPEMB, VALEMBSKU, VALDIMX, VALDIMY, VALDIMZ, VALPES, VALPESLIQ, 
                                VALVOL, VALPESEMBSEC, CODVLDMER, CODPSS, CODMERPRP, VALSHFLFEPRP, CODGRPSTS, CODDSP, BOLPDR, VALALT, VALCAPMAXEMP, 
                                VALQTDSKUALT, VALQTDSKUCPR, VALQTDSKULRG, CODTIPCARMER1, CODTIPCARMER2, CODTIPCARMER3, CODTIPCARMER4, CODTIPCARMER5, 
                                CODTIPCARMER6, CODTIPCARMER7, CODTIPCARMER8, CODGRPCARLGT, OBSMER, BITBOLMIXUMA, BITBOLMIXEND, BITBOLMND, BOLMERMNS, 
                                VALPESEMB, QTDESQPAD, BOLFRCMER, FLAPRC, DSCERR, DATULTPRC) 
      VALUES(V_CODMER, V_CODSTE, V_CODGRPMER, V_NMPRODUTO, V_NMPRODUTO, V_BOLCRGESQREC, V_BOLEMIETQ, V_BOLGERUND, V_BOLMERIDTUNI, V_BOLOTMARZ, 
             V_BOLPESOBR, V_BOLREFDATFABVLD, V_BOLUTLMTPSKU, V_BOLVLDDAT, V_BOLVLDPESMTGVOL, V_BOLVLDVOLMTGVOL, V_BOLMERPES, V_CDGRUPREGARMAZ, 
             V_VRPERCTOLERANCIA, V_CODGRPTOL, V_CDCLASFISC, V_CODTIPCRG, V_CODUNDCRN, V_CODUNDMED, V_CODUNDMEDSEC, V_IDCALGSEL, V_IDCMOVOCO, V_IDCTIPMER, 
             V_NOMIMG, V_VRPERMARMAZEM, V_VRPRAZMAXREC, V_VRPRAZMAXDEV, V_VRJANELAFIFO, V_VRJANFIFOTRANS, V_VRPRAZVALIDADE, 
             V_VRSHELFLIFE, V_VRTEMPMEDESTOQ, V_VRTEMPMEDEXPED, V_VRTEMPMEDRECEB, V_CODUNDCRN, V_VLRUNDVND, V_VLRQTDMINESQ, V_NMFANTPROD, V_DESSKU, 
             V_BOLSKUPDR, V_BOLDELCODBRR, V_BOLVLDBRR, V_CDARVPROD, V_CODTIPEMB, V_VRFATOCONV, V_VALDIMX, V_VALDIMY, V_VALDIMZ, V_VALPES, V_VALPESLIQ, 
             V_VALVOL, V_VALPESEMBSEC, V_CODVLDMER, V_CODPSS, V_CODMERPRP, V_VALSHFLFEPRP, V_CODGRPSTS, V_CODDSP, V_BOLPDR, V_VALALT, V_VALCAPMAXEMP, 
             V_VRPALPRODALT, V_VALQTDSKUCPR, V_VALQTDSKULRG, V_CODTIPCARMER1, V_CODTIPCARMER2, V_CODTIPCARMER3, V_CODTIPCARMER4, V_CODTIPCARMER5, 
             V_CODTIPCARMER6, V_CODTIPCARMER7, V_CODTIPCARMER8, V_CODGRPCARLGT, V_OBSMER, V_BITBOLMIXUMA, V_BITBOLMIXEND, V_BITBOLMND, V_BOLMERMNS, 
             V_VALPESEMB, V_QTDESQPAD, V_BOLFRCMER, V_FLAPRC, V_DSCERR, V_DATULTPRC); 
       
       
       
      UPDATE PRODUTO 
         SET DTEXPPRODSAGA = SYSDATE 
       WHERE CDPRODUTO = V_CDPRODUTO; 
   END LOOP; 
    
   COMMIT; 
   CLOSE C_PRODUTO; 
/*EXCEPTION 
   WHEN OTHERS THEN 
      ROLLBACK; 
      CLOSE C_PRODUTO; */
END;
/
-- VERSAO --
BEGIN ATUVEROBJ('EXPORTA_PRODUTO_SAGA','5.45.XXX'); END;
/