SET TERM ^ ;

CREATE trigger ttbpdv_forma_pagamento_b01d for tbpdv_forma_pagamento
active before delete position 0
AS
DECLARE VARIABLE HISTORICO VARCHAR(500);
DECLARE VARIABLE FORMA_PAG VARCHAR(100);
BEGIN
     HISTORICO = '';
     FORMA_PAG = COALESCE((SELECT FIRST 1
                                FN_LPAD(A.CODIGO, 4, '0')||' - '||A.DESCRICAO AS FORMA_PAG
                           FROM TBPDV_FORMA_PAGAMENTO T
                           LEFT JOIN TBPAGAMENTO_FORMA A ON A.CODIGO = T.PAGAMENTO_FORMA_ID
                           WHERE T.ID = OLD.ID),'');
     IF(OLD.ID > 0) THEN
     BEGIN
          HISTORICO = HISTORICO||'FORM.PAGAMENTO EXCLUÍDA: '||FORMA_PAG;
     END

     IF(HISTORICO <> '') THEN
     BEGIN
          EXECUTE PROCEDURE SPI_HISTORICO('TBPDV', OLD.PDV_ID, HISTORICO);
     END
END
^

SET TERM ; ^

