SET TERM ^ ;

CREATE trigger ttbpdv_forma_pagamento_b02i for tbpdv_forma_pagamento
active before insert position 1
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 = NEW.ID),'');
     IF(NEW.ID > 0) THEN
     BEGIN
          HISTORICO = HISTORICO||'FORM.PAGAMENTO INCLUÍDA: '||FORMA_PAG;
     END

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

SET TERM ; ^

