create or replace PROCEDURE REGERA_COMISSAOVEND(P_CDFILIAL   IN ITEMPEDIDO.CDFILIAL%TYPE,     
                                                P_CDVENDEDOR IN VENDITEMPED.CDVENDEDOR%TYPE,
                                                P_CDOPERADOR IN OPERADOR.CDOPERADOR%TYPE,
                                                P_DTINICIAL  IN DATE,
                                                P_DTFINAL    IN DATE)
IS

BEGIN                                                
   FOR C_PED IN (SELECT PE.CDFILIAL, PE.NRPEDIDO, PE.CDVENDEDOR,  IP.NRITEMPED
                   FROM ITEMPEDIDO IP, PEDIDO PE, VENDITEMPED VI
                  WHERE IP.IDSITITEMPED IN ('1', '2', '3','4','6','7')  
                    AND IP.CDFILIAL   = PE.CDFILIAL  
                    AND IP.NRPEDIDO   = PE.NRPEDIDO 
                    AND VI.CDFILIAL   <> IP.CDFILIAL
                    AND VI.NRPEDIDO   <> IP.NRPEDIDO
                    AND VI.NRITEMPED  <> IP.NRITEMPED
                    AND TRUNC(PE.DTPEDIDO) BETWEEN TRUNC(P_DTINICIAL) AND TRUNC(P_DTFINAL)
                    AND PE.CDVENDEDOR = P_CDVENDEDOR
                    AND IP.CDFILIAL   = P_CDFILIAL
                ) LOOP

      GERA_COMISSAOVEND(C_PED.CDFILIAL, C_PED.NRPEDIDO, C_PED.CDVENDEDOR, P_CDOPERADOR, NULL, NULL, NULL, NULL, NULL); 
   END LOOP;  
END;
/