CREATE OR REPLACE PROCEDURE CALCULA_CUSTOOL
AS
  V_CDGRUPOFISC       VARCHAR2(03) NULL;
  V_PAISORIG          VARCHAR2(04);
  V_ESTAORIG          VARCHAR2(02);
  V_PAISDEST          VARCHAR2(04);
  V_ESTADEST          VARCHAR2(02);
  V_IDACRPRODENTRNF   VARCHAR2(01);
  V_PARDEDTMP         NUMBER;
  V_PREDBCTMP         NUMBER;
  V_ALIQUTMP          NUMBER;
  V_TAXABASECALC      NUMBER;
  V_TAXAALIQUOTA      NUMBER;
  V_BASECALCIT        NUMBER;
  V_PERDBCALC         NUMBER;
  V_IMPOSTOIT         NUMBER;
BEGIN
  FOR C_OL IN
     (SELECT CDFILIAL, CDINSCESTA, LPAD(ROWNUM, 2, '0') CDPRECOBASE
        FROM(SELECT CDFILIAL, CDINSCESTA
               FROM FILIAL
              WHERE IDFUNCAOFILI = 'O'
              ORDER BY CDFILIAL))
  LOOP
     FOR C_CUSTO IN (
     SELECT I.CDPRODUTO, P.CDARVPROD, MAX(I.VRUNITITEM) AS VRUNITITEM,
            MAX(I.CDFILIAL) AS CDFILIAL, MAX(I.NRLANCTONF) AS NRLANCTONF,
            MAX(N.CDFORNECED) AS CDFORNECED, MAX(N.DTENTRSAID) AS DTENTRSAID
     FROM ITEMPRODUTO I, NOTAFISCAL N, PRODUTO P
     WHERE I.CDFILIAL = N.CDFILIAL
       AND I.NRLANCTONF = N.NRLANCTONF
       AND N.CDFILIRECE = C_OL.CDFILIAL
       AND N.IDENTRSAIDOP = 'E'
       AND N.CDTIPOOPER = '01'
       AND P.CDPRODUTO = I.CDPRODUTO
       AND N.DTENTRSAID = (SELECT MAX(NR.DTENTRSAID)
                            FROM NOTAFISCAL NR, ITEMPRODUTO IR
                           WHERE NR.CDFILIAL = IR.CDFILIAL
                             AND NR.NRLANCTONF = IR.NRLANCTONF
                             AND NR.CDFILIRECE = C_OL.CDFILIAL
                             AND NR.IDENTRSAIDOP = 'E'
                             AND NR.CDTIPOOPER = '01'
                             AND IR.CDPRODUTO = I.CDPRODUTO)
     GROUP BY I.CDPRODUTO, P.CDARVPROD)
     LOOP
        BEGIN
           SELECT CDPAIS, SGESTADO
           INTO V_PAISORIG, V_ESTAORIG
           FROM ENDEFORN
           WHERE CDFORNECED = C_CUSTO.CDFORNECED
             AND IDTPENDEFORN = 'P';
        EXCEPTION
           WHEN NO_DATA_FOUND THEN
              V_PAISORIG := NULL; V_ESTAORIG := NULL;
        END;
        BEGIN
           SELECT CDPAIS, SGESTADO
           INTO V_PAISDEST, V_ESTADEST
           FROM INSCRESTAD
           WHERE (CDEMPRESA  = '01')
             AND (CDINSCESTA = C_OL.CDINSCESTA);
        EXCEPTION
           WHEN NO_DATA_FOUND THEN
              V_PAISDEST := NULL; V_ESTADEST := NULL;
        END;
        DELETE FROM INCITEMP WHERE NRINCITEMP = C_CUSTO.CDFILIAL||C_CUSTO.NRLANCTONF;
        CONSULTA_GRUPOFISCAL('F', C_CUSTO.CDFORNECED, '', 'E', '01', V_CDGRUPOFISC);
        CONSULTA_INCIDENCIA('E', '01', C_CUSTO.CDARVPROD, C_OL.CDINSCESTA, '01',
                             C_CUSTO.CDFILIAL, C_CUSTO.NRLANCTONF, V_CDGRUPOFISC,
NULL);
        V_IMPOSTOIT := 0;
        FOR C_INCIDENCIA IN (SELECT CDIMPOTEMP, MAX(CDFISCTEMP) AS CDFISCTEMP,
                                    MAX(IDTRATIMPTMP) AS IDTRATIMPTMP,
MAX(IDCONSISTENT) AS IDCONSISTENT,
                                    MAX(NVL(IDPREIMPENTR, 'N')) AS IDPREIMPENTR,
                                    MAX(IDORGARRECAD) AS IDORGARRECAD,
MAX(IP.IDIMPORECUP) AS IDIMPORECUP,
                                    MAX(IP.CDCCTBLATIV) AS CDCCTBLATIV,
MAX(IP.IDTRATIMPO) AS IDTRATIMPO,
                                    MAX(IP.CDCCTBLPASS) AS CDCCTBLPASS
                             FROM INCITEMP IT, IMPOSTO IP
                             WHERE (IT.NRINCITEMP =
C_CUSTO.CDFILIAL||C_CUSTO.NRLANCTONF)
                               AND (IP.CDIMPOSTO  = IT.CDIMPOTEMP)
                               AND (IP.CDIMPOSTO = '00')
                             GROUP BY CDIMPOTEMP) LOOP
           DELETE FROM ALIQTEMP WHERE NRALIQTEMP =
C_CUSTO.CDFILIAL||C_CUSTO.NRLANCTONF;
           CONSULTA_ALIQUOTA(C_INCIDENCIA.CDIMPOTEMP, 'E', '01',
                             C_CUSTO.CDARVPROD, C_OL.CDINSCESTA,
                             V_PAISORIG, V_ESTAORIG, V_PAISDEST, V_ESTADEST,
                             NULL, C_CUSTO.VRUNITITEM, SYSDATE,
                             C_CUSTO.CDFILIAL, C_CUSTO.NRLANCTONF, V_CDGRUPOFISC,
'01');
           SELECT NVL(MAX(VRPARDEDTMP),0), NVL(MAX(VRPREDBCTMP),0),
NVL(MAX(VRALIQUTMP),0)
           INTO V_PARDEDTMP, V_PREDBCTMP, V_ALIQUTMP
           FROM ALIQTEMP
           WHERE (NRALIQTEMP = C_CUSTO.CDFILIAL||C_CUSTO.NRLANCTONF);
           DELETE FROM ALIQTEMP WHERE NRALIQTEMP =
C_CUSTO.CDFILIAL||C_CUSTO.NRLANCTONF;
           V_TAXABASECALC := (100 - V_PREDBCTMP)/100;
           V_TAXAALIQUOTA := (V_ALIQUTMP/100);
           V_BASECALCIT   := C_CUSTO.VRUNITITEM * V_TAXABASECALC;
           V_PERDBCALC    := 100 - V_PREDBCTMP;
           IF (C_INCIDENCIA.CDFISCTEMP = '1') THEN
              IF (C_INCIDENCIA.CDCCTBLATIV IS NOT NULL) THEN
                 IF C_INCIDENCIA.IDTRATIMPO = 'V' THEN
                    V_IMPOSTOIT := (V_BASECALCIT * V_TAXAALIQUOTA);
                 END IF;
              END IF;
           END IF;
        END LOOP;
        DELETE FROM INCITEMP WHERE NRINCITEMP = C_CUSTO.CDFILIAL||C_CUSTO.NRLANCTONF;
        INSERT INTO ITEMPREBASE
           (CDPRECOBASE, DTINIVALIPB, CDPRODUTO, VRITEPREBASE, DTHRATZIPB,
CDOPERATZIPB)
        VALUES
           (C_OL.CDPRECOBASE, TRUNC(SYSDATE), C_CUSTO.CDPRODUTO, C_CUSTO.VRUNITITEM
- V_IMPOSTOIT, SYSDATE, '000000000999');
     END LOOP;
     DELETE
       FROM ITEMPREBASE I
      WHERE DTINIVALIPB = TO_DATE('01/05/2009','DD/MM/YYYY')
        AND CDPRECOBASE = C_OL.CDPRECOBASE
        AND EXISTS (SELECT CDPRECOBASE
                      FROM ITEMPREBASE T
                     WHERE T.CDPRECOBASE = I.CDPRECOBASE
                       AND T.DTINIVALIPB = TRUNC(SYSDATE)
                       AND T.CDPRODUTO = I.CDPRODUTO);
     UPDATE ITEMPREBASE
        SET DTINIVALIPB = TO_DATE('01/05/2009','DD/MM/YYYY')
      WHERE DTINIVALIPB = TRUNC(SYSDATE)
        AND CDPRECOBASE = C_OL.CDPRECOBASE;
     COMMIT;
  END LOOP;
END;
/

-- VERSAO --
BEGIN ATUVEROBJ('CALCULA_CUSTOOL','5.05.000'); END;
/
