--@C:\Web_409\Srv\Src\Cli\SAP\SQL\0005_SAPTRGO_USR_PRAZOPGTOFORN.sql

CREATE OR REPLACE TRIGGER USR_PRAZOPGTOFORN
BEFORE INSERT OR UPDATE ON FORNECEDOR
FOR EACH ROW
DECLARE
   V_USR_CODCPG  FORMPGTO.USR_CODCPG%TYPE;

BEGIN
   IF :NEW.CDFORMPGTO IS NOT NULL THEN
      BEGIN
         SELECT USR_CODCPG
         INTO V_USR_CODCPG
         FROM FORMPGTO
         WHERE CDFORMPGTO = :NEW.CDFORMPGTO;
      EXCEPTION
         WHEN NO_DATA_FOUND THEN
            NULL;
      END;

      IF V_USR_CODCPG IS NOT NULL THEN
         DELETE FROM PRAZOPGTFORN
         WHERE CDFORNECED = :NEW.CDFORNECED;

         FOR CP IN (
         SELECT CODCPG, NUMPAR, DIASEMI, CHAVE, PCEMISSAO
         FROM USR_TCONDPAGTO
         WHERE CHAVE = V_USR_CODCPG) LOOP
            INSERT INTO PRAZOPGTFORN
            (CDFORNECED, NRDIASPGTOFO, VRPERCPGTOFO)
            VALUES
            (:NEW.CDFORNECED, CP.DIASEMI, CP.PCEMISSAO);
         END LOOP;
      END IF;
   END IF;
END;
/



-- VERSAO --
BEGIN ATUVEROBJ('USR_PRAZOPGTOFORN','5.01.003'); END;
/

