CREATE OR REPLACE TRIGGER USR_EXCLUSAO_AUTOFORNE
AFTER DELETE ON AUTOFORNE FOR EACH ROW
DECLARE
   V_AFDELETADA INTEGER;
   V_SEQUENCIAL INTEGER;
   V_CHAVE      VARCHAR2(18);
   V_CDSITESAP  VARCHAR2(4);
BEGIN
   V_CHAVE := :OLD.CDFILLANC||:OLD.CDFILDEST||:OLD.NRAUTOFORN;
   
   BEGIN
      SELECT P.CDEXFILIAL
        INTO V_CDSITESAP
        FROM PARAMFILIAL P
       WHERE P.CDFILIAL = :OLD.CDFILDEST;
   EXCEPTION
      WHEN OTHERS THEN
         V_CDSITESAP := NULL;
   END;

   SELECT COUNT(1)
     INTO V_AFDELETADA
     FROM USR_SAP_AUTOFORNE
    WHERE CDSITETECFOOD = :OLD.CDFILLANC
      AND CDSITESAP     = V_CDSITESAP
      AND ORDEN         = :OLD.NRAUTOFORN;
   
   IF (V_AFDELETADA > 0) THEN
      FOR C_AFEXISTE IN (SELECT ORDEN, ORG_COMPRA, GRUPO_COMPRA, BUYER,
                                MATERIAL, CANTIDAD, FEC_ENTREGA,  IDGERAORDCOMP,
                                IDINTERFACE, TPINTERFACE, DTINTERFACEORI, DTINTERFACEDEST, STATUS_TECFOOD, APPORIGEM, APPDESTINO,
                                TIPO_DOCUM, POSICION, NIF_PROVEE, SOCIEDAD, UOM_ORDEN, MONEDA,
                                CDSITETECFOOD, CDSITESAP, PRECIO_UNITARIO, CDPROVEESAP
                           FROM USR_SAP_AUTOFORNE
                          WHERE CDSITETECFOOD = :OLD.CDFILLANC
                            AND CDSITESAP     = V_CDSITESAP
                            AND ORDEN         = :OLD.NRAUTOFORN)
      LOOP
         SELECT USR_SAP_IDINTERFACE.NEXTVAL
           INTO V_SEQUENCIAL
           FROM DUAL;
           
         INSERT INTO USR_SAP_AUTOFORNE(ORDEN, ORG_COMPRA, GRUPO_COMPRA, BUYER,
                                       MATERIAL, CANTIDAD, FEC_ENTREGA,  IDGERAORDCOMP,
                                       IDINTERFACE, TPINTERFACE, DTINTERFACEORI, DTINTERFACEDEST, STATUS_TECFOOD, APPORIGEM, APPDESTINO,
                                       TIPO_DOCUM, POSICION, NIF_PROVEE, SOCIEDAD, UOM_ORDEN, MONEDA,
                                       CDSITETECFOOD, CDSITESAP, PRECIO_UNITARIO, CDPROVEESAP)
         VALUES(C_AFEXISTE.ORDEN, C_AFEXISTE.ORG_COMPRA, C_AFEXISTE.GRUPO_COMPRA, C_AFEXISTE.BUYER,
                C_AFEXISTE.MATERIAL, C_AFEXISTE.CANTIDAD, C_AFEXISTE.FEC_ENTREGA, C_AFEXISTE.IDGERAORDCOMP,
                V_SEQUENCIAL, 'E', SYSDATE, NULL, 1, 1, 2,
                C_AFEXISTE.TIPO_DOCUM, C_AFEXISTE.POSICION, C_AFEXISTE.NIF_PROVEE, C_AFEXISTE.SOCIEDAD, C_AFEXISTE.UOM_ORDEN, C_AFEXISTE.MONEDA,
                C_AFEXISTE.CDSITETECFOOD, C_AFEXISTE.CDSITESAP, C_AFEXISTE.PRECIO_UNITARIO, C_AFEXISTE.CDPROVEESAP);
         
      END LOOP;
   END IF;
END;
/

-- VERSAO --
BEGIN ATUVEROBJ('USR_EXCLUSAO_AUTOFORNE','5.45.XXX'); END;
/

CREATE OR REPLACE TRIGGER USR_ALTERACAO_ITEMAUTF
AFTER UPDATE OR INSERT OR DELETE ON ITEMAUTF FOR EACH ROW
BEGIN
   UPDATE AUTOFORNE
      SET USR_DTEXPORTA = NULL
    WHERE CDFILDEST  = :NEW.CDFILDEST
      AND CDFILLANC  = :NEW.CDFILLANC 
      AND NRAUTOFORN = :NEW.NRAUTOFORN;
END;
/

-- VERSAO --
BEGIN ATUVEROBJ('USR_ALTERACAO_ITEMAUTF','5.45.XXX'); END;
/

CREATE OR REPLACE TRIGGER USR_EXCLUSAO_COLETAPR
AFTER DELETE ON COLETAPR FOR EACH ROW
DECLARE
   V_COLETADEL  INTEGER;
   V_SEQUENCIAL INTEGER;
   V_CHAVE      VARCHAR2(53);
   V_CDSITESAP  VARCHAR2(4);
BEGIN
   BEGIN
      SELECT CDFILIAL||NRCOLEPRECO||CDMARCA||CDREGIAO||CDFORNECED||CDPRODUTO
        INTO V_CHAVE
        FROM ITEMCOLE
       WHERE CDFILIAL    = :OLD.CDFILIAL
         AND NRCOLEPRECO = :OLD.NRCOLEPRECO;
   EXCEPTION
      WHEN NO_DATA_FOUND THEN
         V_CHAVE := NULL;
   END;
   
   SELECT COUNT(1)
     INTO V_COLETADEL
     FROM USR_SAP_LISTPRECO
    WHERE CDCHAVE = V_CHAVE;
   
   IF (V_COLETADEL > 0) THEN
      FOR C_LISTAEXIST IN (SELECT CDCHAVE, CDSITESAP, CDPRODSAP, UOMSAP, CDPROVEESAP, NIF_PROVEE, VRUNITARIO,
                                  DTINIVIG, DTFINVIG
                             FROM USR_SAP_LISTPRECO
                            WHERE CDCHAVE = V_CHAVE)
      LOOP
         SELECT USR_SAP_IDINTERFACE.NEXTVAL
           INTO V_SEQUENCIAL
           FROM DUAL;
         
         INSERT INTO USR_SAP_LISTPRECO(CDCHAVE, CDSITESAP, CDPRODSAP, UOMSAP, CDPROVEESAP, 
                                       NIF_PROVEE, VRUNITARIO, DTINIVIG, DTFINVIG,
                                       IDINTERFACE, TPINTERFACE, DTINTERFACEORI, DTINTERFACEDEST, STATUS, APPORIGEM, APPDESTINO)
         VALUES(C_LISTAEXIST.CDCHAVE, C_LISTAEXIST.CDSITESAP, C_LISTAEXIST.CDPRODSAP, C_LISTAEXIST.UOMSAP, C_LISTAEXIST.CDPROVEESAP,
                C_LISTAEXIST.NIF_PROVEE, C_LISTAEXIST.VRUNITARIO, C_LISTAEXIST.DTINIVIG, C_LISTAEXIST.DTFINVIG,
                V_SEQUENCIAL, 'E', SYSDATE, NULL, 1, 1, 2);
      END LOOP;
   END IF;
END;
/

-- VERSAO --
BEGIN ATUVEROBJ('USR_EXCLUSAO_COLETAPR','5.45.XXX'); END;
/

CREATE OR REPLACE TRIGGER USR_ALTERACAO_ITEMCOLE
AFTER UPDATE OR INSERT OR DELETE ON ITEMCOLE FOR EACH ROW
BEGIN
   UPDATE COLETAPR
      SET USR_DTEXPORTA = NULL
    WHERE CDFILIAL     = :NEW.CDFILIAL
      AND NRCOLEPRECO  = :NEW.NRCOLEPRECO;
END;
/

ALTER TRIGGER CALC_PRECO_ECON DISABLE
/
-- VERSAO --
BEGIN ATUVEROBJ('USR_ALTERACAO_ITEMCOLE','5.45.XXX'); END;
/