create or replace TRIGGER LOG_PED_PRAZOPGTOPED AFTER 
    DELETE OR INSERT OR UPDATE ON PRAZOPGTOPED 
    FOR EACH ROW 
 
DECLARE 
    V_USUARIO           VARCHAR2(40); 
    V_TIPO              VARCHAR2(1); 
    V_ITPESQ            VARCHAR2(40); 
    V_DESC              VARCHAR2(4000); 
    V_CDFILLOGOPER      VARCHAR2(4); 
    V_DSREMOTEADDR      VARCHAR2(15); 
    V_DSREMOTEHOST      VARCHAR2(15); 
    V_DSSERVERRDM       VARCHAR2(20); 
    V_DSSERVMETHOD      VARCHAR2(30); 
    V_DSREMOTEOPER      VARCHAR2(12); 
    V_DTOPER            DATE; 
    V_TAM               INTEGER; 
BEGIN 

    V_TIPO         := '0'; 
    V_DESC         := ''; 
    V_CDFILLOGOPER := ''; 

    IF DELETING THEN

        IF :OLD.CDFILIAL IS NOT NULL THEN 
            V_DESC := V_DESC|| ' Cod.Filial='|| :OLD.CDFILIAL; 
        END IF; 

        IF :OLD.NRPEDIDO IS NOT NULL THEN 
            V_DESC := V_DESC|| ' Nr.Pedido='|| :OLD.NRPEDIDO; 
        END IF; 

        IF :OLD.NRDIASPGTOPED IS NOT NULL THEN 
            V_DESC := V_DESC || ' Dias pgto='|| :OLD.NRDIASPGTOPED; 
        END IF; 

        IF :OLD.VRPERCPGTOPED IS NOT NULL THEN 
            V_DESC := V_DESC|| ' Perc pgto='|| :OLD.VRPERCPGTOPED; 
        END IF; 

        IF :OLD.DSPRAZOPGTOPED IS NOT NULL THEN 
            V_DESC := V_DESC|| ' Desc. Prazo pgto='|| :OLD.DSPRAZOPGTOPED; 
        END IF; 

        IF :OLD.VRPARCELA IS NOT NULL THEN 
            V_DESC := V_DESC|| ' Vr. Parcela='|| :OLD.VRPARCELA; 
        END IF; 

        V_TIPO         := '3'; 
        V_ITPESQ       := :OLD.CDFILIAL|| :OLD.NRPEDIDO;
        V_CDFILLOGOPER := :OLD.CDFILIAL; 
    ELSE 
        IF UPDATING THEN 

            IF :OLD.CDFILIAL IS NOT NULL THEN 
                V_DESC := V_DESC|| ' Cod.Filial='|| :OLD.CDFILIAL; 
            END IF; 

            IF :OLD.NRPEDIDO IS NOT NULL THEN 
                V_DESC := V_DESC|| ' Nr.Pedido='|| :OLD.NRPEDIDO; 
            END IF; 

            IF :OLD.NRDIASPGTOPED IS NOT NULL THEN 
                V_DESC := V_DESC || ' Dias pgto='|| :OLD.NRDIASPGTOPED; 
            END IF; 

            IF :OLD.VRPERCPGTOPED IS NOT NULL THEN 
                V_DESC := V_DESC|| ' Perc pgto='|| :OLD.VRPERCPGTOPED; 
            END IF; 

            IF :OLD.DSPRAZOPGTOPED IS NOT NULL THEN 
                V_DESC := V_DESC|| ' Desc. Prazo pgto='|| :OLD.DSPRAZOPGTOPED; 
            END IF; 

            IF :OLD.VRPARCELA IS NOT NULL THEN 
                V_DESC := V_DESC|| ' Vr. Parcela='|| :OLD.VRPARCELA; 
            END IF; 

            V_TIPO         := '2'; 
            V_ITPESQ       := :OLD.CDFILIAL|| :OLD.NRPEDIDO; 
            V_CDFILLOGOPER := :OLD.CDFILIAL; 

        ELSE 

            IF :NEW.CDFILIAL IS NOT NULL THEN 
                V_DESC := V_DESC|| ' Cod.Filial='|| :NEW.CDFILIAL; 
            END IF; 

            IF :NEW.NRPEDIDO IS NOT NULL THEN 
                V_DESC := V_DESC|| ' Nr.Pedido='|| :NEW.NRPEDIDO; 
            END IF; 

            IF :NEW.NRDIASPGTOPED IS NOT NULL THEN 
                V_DESC := V_DESC || ' Dias pgto='|| :NEW.NRDIASPGTOPED; 
            END IF; 

            IF :NEW.VRPERCPGTOPED IS NOT NULL THEN 
                V_DESC := V_DESC|| ' Perc pgto='|| :NEW.VRPERCPGTOPED; 
            END IF; 

            IF :NEW.DSPRAZOPGTOPED IS NOT NULL THEN 
                V_DESC := V_DESC|| ' Desc. Prazo pgto='|| :NEW.DSPRAZOPGTOPED; 
            END IF; 

            IF :NEW.VRPARCELA IS NOT NULL THEN 
                V_DESC := V_DESC|| ' Vr. Parcela='|| :NEW.VRPARCELA; 
            END IF; 


            V_TIPO         := '1'; 
            V_ITPESQ       := :NEW.CDFILIAL || :NEW.NRPEDIDO; 
            V_CDFILLOGOPER := :NEW.CDFILIAL; 

        END IF; 
        
    END IF; 

    IF V_TIPO <> '0' THEN 

        NMUSUARIO(V_USUARIO); 
        WEBTUNNING(V_DSREMOTEADDR, V_DSREMOTEHOST, V_DSSERVERRDM, V_DSSERVMETHOD, V_DSREMOTEOPER); 
        IF NOT V_DESC IS NULL THEN 
            V_TAM := LENGTH(V_DESC); 
            WHILE ( V_TAM >= 0 ) LOOP 
                V_DTOPER := SYSDATE; 
                INSERT INTO LOGOPERA ( 
                    NMTABELA, 
                    DTOPERACAO, 
                    NMRESPOPER, 
                    IDTIPOOPER, 
                    CDPESQITEM, 
                    DSOPERACAO, 
                    CDFILLOGOPER, 
                    DSRDMUSUOPER, 
                    DSENDUSUOPER, 
                    DSNMUSUOPER, 
                    DSMETUSUOPER, 
                    CDORESPOPER 
                ) VALUES ( 
                    'PRAZOPGTOPED', 
                    V_DTOPER, 
                    V_USUARIO, 
                    V_TIPO, 
                    V_ITPESQ, 
                    SUBSTR(V_DESC, 1, 300), 
                    V_CDFILLOGOPER, 
                    V_DSSERVERRDM, 
                    V_DSREMOTEADDR, 
                    V_DSREMOTEHOST, 
                    V_DSSERVMETHOD, 
                    V_DSREMOTEOPER 
                ); 
                V_DESC := SUBSTR(V_DESC, 300, V_TAM); 
                V_TAM  := LENGTH(V_DESC); 
            END LOOP; 
        END IF; 
    END IF; 
END;

/