/******************************************************************************/
/****              Generated by IBExpert 30/03/2022 09:10:42               ****/
/******************************************************************************/

/******************************************************************************/
/****     Following SET SQL DIALECT is just for the Database Comparer      ****/
/******************************************************************************/
SET SQL DIALECT 3;



/******************************************************************************/
/****                                Tables                                ****/
/******************************************************************************/


CREATE GENERATOR GTBTABELA_PRECO_CLIENTE;

CREATE TABLE TBTABELA_PRECO_CLIENTE (
    ID               INTEGER NOT NULL,
    TABELA_PRECO_ID  INTEGER NOT NULL,
    CLIENTE_ID       INTEGER NOT NULL
);




/******************************************************************************/
/****                             Primary keys                             ****/
/******************************************************************************/

ALTER TABLE TBTABELA_PRECO_CLIENTE ADD CONSTRAINT PK_TBTABELA_PRECO_CLIENTE PRIMARY KEY (ID);


/******************************************************************************/
/****                               Triggers                               ****/
/******************************************************************************/



SET TERM ^ ;



/******************************************************************************/
/****                         Triggers for tables                          ****/
/******************************************************************************/



/* Trigger: TTBTABELA_PRECO_CLIENTE_B01D */
CREATE OR ALTER TRIGGER TTBTABELA_PRECO_CLIENTE_B01D FOR TBTABELA_PRECO_CLIENTE
ACTIVE BEFORE DELETE POSITION 0
AS
DECLARE VARIABLE HISTORICO VARCHAR(500);
DECLARE VARIABLE CLIENTE VARCHAR(500);
BEGIN

    HISTORICO = '';
    CLIENTE = COALESCE((SELECT FN_LPAD(A.CODIGO, 4, '0')||' - '||B.NOMEFANTASIA FROM TBCLIENTE A, TBEMPRESA B WHERE A.CODIGO = B.CODIGO AND A.CODIGO = OLD.CLIENTE_ID), '');

    IF ( OLD.ID > 0) THEN
    BEGIN
        HISTORICO = HISTORICO||'CLIENTE: '||CLIENTE||' EXCLUIDO';
    END

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


/* Trigger: TTBTABELA_PRECO_CLIENTE_B01I */
CREATE OR ALTER TRIGGER TTBTABELA_PRECO_CLIENTE_B01I FOR TBTABELA_PRECO_CLIENTE
ACTIVE BEFORE INSERT POSITION 0
AS
BEGIN
  IF (NEW.ID IS NULL) THEN
    NEW.ID = GEN_ID(GTBTABELA_PRECO_CLIENTE,1);
END
^


/* Trigger: TTBTABELA_PRECO_CLIENTE_B02I */
CREATE OR ALTER TRIGGER TTBTABELA_PRECO_CLIENTE_B02I FOR TBTABELA_PRECO_CLIENTE
ACTIVE BEFORE INSERT POSITION 1
AS
DECLARE VARIABLE HISTORICO VARCHAR(500);
DECLARE VARIABLE CLIENTE VARCHAR(500);
BEGIN

    HISTORICO = '';
    CLIENTE = COALESCE((SELECT FN_LPAD(A.CODIGO, 4, '0')||' - '||B.NOMEFANTASIA FROM TBCLIENTE A, TBEMPRESA B WHERE A.CODIGO = B.CODIGO AND A.CODIGO = NEW.CLIENTE_ID), '');

    IF ( NEW.ID > 0) THEN
    BEGIN
        HISTORICO = HISTORICO||'CLIENTE: '||CLIENTE||' INCLUIDO';
    END

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

SET TERM ; ^