CREATE OR REPLACE PROCEDURE REPLICA_PRODUTO_PRODFILI(
    P_CDPRODUTO  IN VARCHAR2,
    P_CDARVPROD  IN VARCHAR2)
IS
   V_TAM                FLOAT;
   V_IDREPLICPRODATIV   VARCHAR2(01);
   V_COUNT              NUMBER;
BEGIN
   V_TAM   := 0;
   V_COUNT := 0;

   SELECT LENGTH(REPLACE(CDPICTPROD,'.',NULL)), NVL(IDREPLICPRODATIV,'S')
     INTO V_TAM, V_IDREPLICPRODATIV
     FROM PARAMGERAL
    WHERE CDPARGERAL = '0001';

   IF LENGTH(P_CDARVPROD) = V_TAM THEN
      FOR C_FILIAL IN
         (SELECT F.CDFILIAL, P.IDCTRLESTQ, P.IDUTILLOTE
            FROM FILIAL F, PARAMFILIAL P
           WHERE F.CDFILIAL = P.CDFILIAL
             AND NVL(F.IDCNTRRESC,'N') = 'N'
         )
      LOOP
         INSERT INTO PRODFILI
            (CDFILIAL, CDPRODUTO, IDCNTRESTOQ, IDUTCTRLLOTE, IDPRODATIV, DTULTATUPRFIL)
         VALUES
            (C_FILIAL.CDFILIAL, P_CDPRODUTO, C_FILIAL.IDCTRLESTQ, C_FILIAL.IDUTILLOTE, DECODE(V_IDREPLICPRODATIV, 'S', 'A', 'I'), SYSDATE);
      END LOOP;

      V_COUNT := V_COUNT + 1;

      IF V_COUNT = 50 THEN
         V_COUNT := 0;
         COMMIT;
      END IF;
   END IF;
   COMMIT;
END;
/