SET TERM ^ ;

CREATE OR ALTER TRIGGER TTBPAGAMENTO_FORMA_B02IU FOR TBPAGAMENTO_FORMA
ACTIVE BEFORE INSERT OR UPDATE POSITION 2
AS
BEGIN
    IF(NEW.NFE_TIPPAG <> '03' AND NEW.NFE_TIPPAG <> '04')THEN
    BEGIN
        NEW.PDV_ADQUIRENTE_ID   = 0;
    END

    IF(NEW.NFE_TIPPAG <> '80')THEN
    BEGIN
       NEW.IMPRESSAO_AUTOMATICA = 0;
    END

    IF(INSERTING) THEN
    BEGIN
        IF(NEW.PDV_ADQUIRENTE_ID > 0) THEN
        BEGIN
            NEW.EMPRESA_ID = COALESCE((SELECT FIRST 1 T.EMPRESA_ID FROM TBPDV_ADQUIRENTE T WHERE T.ID = NEW.PDV_ADQUIRENTE_ID), 0);
        END
    END

    IF(UPDATING) THEN
    BEGIN
        IF(OLD.PDV_ADQUIRENTE_ID IS DISTINCT FROM NEW.PDV_ADQUIRENTE_ID) THEN
        BEGIN
            NEW.EMPRESA_ID = COALESCE((SELECT FIRST 1 T.EMPRESA_ID FROM TBPDV_ADQUIRENTE T WHERE T.ID = NEW.PDV_ADQUIRENTE_ID), 0);
        END
    END
END
^

SET TERM ; ^

