/******************************************************************************/
/****              Generated by IBExpert 19/02/2024 08:04:23               ****/
/******************************************************************************/

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



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


CREATE GENERATOR GTBPEDIDO_FICHA_TEMPLATE;

CREATE TABLE TBPEDIDO_FICHA_TEMPLATE (
    PEDIDO  	   INTEGER NOT NULL,
    TIPO_CODIGO    INTEGER NOT NULL,
    VALOR          BLOB SUB_TYPE 0 SEGMENT SIZE 80,
    COLUNA_ID      INTEGER,
    LINHA_ID       INTEGER,
	FAMILIA_ID     INTEGER,	
    ID             INTEGER NOT NULL
);


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

ALTER TABLE TBPEDIDO_FICHA_TEMPLATE ADD CONSTRAINT PK_TBPEDIDO_FICHA_TEMPLATE PRIMARY KEY (ID);


/******************************************************************************/
/****                               Indices                                ****/
/******************************************************************************/

CREATE INDEX ITBPEDIDO_FICHA_TEMPLATE_01 ON TBPEDIDO_FICHA_TEMPLATE (PEDIDO);
CREATE INDEX ITBPEDIDO_FICHA_TEMPLATE_02 ON TBPEDIDO_FICHA_TEMPLATE (TIPO_CODIGO);
CREATE INDEX ITBPEDIDO_FICHA_TEMPLATE_03 ON TBPEDIDO_FICHA_TEMPLATE (COLUNA_ID);
CREATE INDEX ITBPEDIDO_FICHA_TEMPLATE_04 ON TBPEDIDO_FICHA_TEMPLATE (LINHA_ID);


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

SET TERM ^ ;


ALTER TABLE TBPEDIDO_FICHA_TEMPLATE
ADD MODELO_ID INTEGER^


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



/* Trigger: TTBPEDIDO_FICHA_TEMPLATE_999 */
CREATE OR ALTER TRIGGER TTBPEDIDO_FICHA_TEMPLATE_999 FOR TBPEDIDO_FICHA_TEMPLATE
ACTIVE BEFORE INSERT OR UPDATE OR DELETE POSITION 999
AS
begin
  update tbreplica_tabela set Status = '0', Status_Qlik = '0'
  Where tabela = 'TBPEDIDO_FICHA_PROCESSO'
    and (Status = '1' or Status_Qlik = '1') ;
end
^


/* Trigger: TTBPEDIDO_FICHA_TEMPLATE_B01I */
CREATE OR ALTER TRIGGER TTBPEDIDO_FICHA_TEMPLATE_B01I FOR TBPEDIDO_FICHA_TEMPLATE
ACTIVE BEFORE INSERT POSITION 0
as
begin
  if (coalesce(new.id, 0) = 0) then
    new.id = gen_id(gTBPEDIDO_FICHA_TEMPLATE,1);
end
^


/* Trigger: TTBPEDIDO_FICHA_TEMPLATE_B02IU */
CREATE OR ALTER TRIGGER TTBPEDIDO_FICHA_TEMPLATE_B02IU FOR TBPEDIDO_FICHA_TEMPLATE
ACTIVE BEFORE INSERT OR UPDATE POSITION 1
AS
DECLARE VARIABLE HISTORICO BLOB SUB_TYPE 1 SEGMENT SIZE 80;
DECLARE VARIABLE TIPO_CAMPO INTEGER;
BEGIN
    HISTORICO = '';

    IF(INSERTING) THEN
    BEGIN
        IF(NEW.VALOR IS NOT NULL AND NEW.VALOR <> '') THEN
        BEGIN
            HISTORICO = HISTORICO||''||'->'||NEW.VALOR;
        END
        ELSE
        BEGIN
            IF(NEW.VALOR IS NOT NULL AND NEW.VALOR <> 0) THEN
            BEGIN
                HISTORICO = HISTORICO||'0'||'->'||NEW.VALOR;
            END
        END

        IF (HISTORICO <> '') THEN
        BEGIN
           HISTORICO = 'PED.FICHA ID:'||NEW.TIPO_CODIGO||' ALTERADO '||HISTORICO;
           EXECUTE PROCEDURE SPI_HISTORICO('TBPEDIDO', NEW.PEDIDO,FN_ELIPSES(HISTORICO,500));
        END
    END
    ELSE
    BEGIN
        IF(OLD.VALOR <> NEW.VALOR)THEN
        BEGIN
            HISTORICO = HISTORICO||OLD.VALOR||'->'||NEW.VALOR;
        END

        IF (HISTORICO <> '') THEN
        BEGIN
           HISTORICO = 'PED.FICHA ID:'||NEW.TIPO_CODIGO||' ALTERADO '||HISTORICO;
           EXECUTE PROCEDURE SPI_HISTORICO('TBPEDIDO', NEW.PEDIDO,FN_ELIPSES(HISTORICO,500));
        END
    END
END
^

SET TERM ; ^