/******************************************************************************/
/****              Generated by IBExpert 23/06/2022 08:02:49               ****/
/******************************************************************************/

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



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


CREATE GENERATOR GTBPEDIDO_PARCELAMENTO;

CREATE TABLE TBPEDIDO_PARCELAMENTO (
    ID                    INTEGER NOT NULL,
    PEDIDO                INTEGER,
    PAGAMENTO_FORMA       INTEGER,
    PAGAMENTO_CONDICAO    INTEGER,
    NSU                   VARCHAR(50) DEFAULT '',
    CODIGO_AUTORIZACAO    VARCHAR(50) DEFAULT '',
    VALOR                 NUMERIC(15,2)
);


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

CREATE INDEX ITBPEDIDO_PARCELAMENTO_02   ON TBPEDIDO_PARCELAMENTO (PEDIDO);
CREATE INDEX ITBPEDIDO_PARCELAMENTO_IDX1 ON TBPEDIDO_PARCELAMENTO (ID);


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



SET TERM ^ ;



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



/* Trigger: TTBPEDIDO_PARCELAMENTO_B01D */
CREATE OR ALTER TRIGGER TTBPEDIDO_PARCELAMENTO_B01D FOR TBPEDIDO_PARCELAMENTO
ACTIVE BEFORE DELETE POSITION 0
AS
DECLARE VARIABLE HISTORICO VARCHAR(1000);
DECLARE VARIABLE DESC_FORMA VARCHAR(100);
DECLARE VARIABLE DESC_CONDICAO VARCHAR(100);
BEGIN

    HISTORICO = '';
    SELECT TRIM(W.DESCRICAO) FROM TBPAGAMENTO_FORMA    W WHERE W.CODIGO = OLD.PAGAMENTO_FORMA      INTO DESC_FORMA;
    SELECT TRIM(W.DESCRICAO) FROM TBPAGAMENTO_CONDICAO W WHERE W.CODIGO = OLD.PAGAMENTO_CONDICAO INTO DESC_CONDICAO;

    HISTORICO = HISTORICO||' FORMA DE PAGAMENTO: '||FN_LPAD(OLD.PAGAMENTO_FORMA,4,0)||' - '||DESC_FORMA||', CONDICAO:'||FN_LPAD(OLD.PAGAMENTO_CONDICAO,4,0)||' - '||DESC_CONDICAO||', VALOR R$ '||COALESCE(OLD.VALOR,0.00)||' EXCLUIDA';
    
    IF (HISTORICO <> '') THEN
    BEGIN
        EXECUTE PROCEDURE SPI_HISTORICO('TBPEDIDO',OLD.PEDIDO,FN_ELIPSES(HISTORICO,500));
    END
END
^


/* Trigger: TTBPEDIDO_PARCELAMENTO_B01U */
CREATE OR ALTER TRIGGER TTBPEDIDO_PARCELAMENTO_B01U FOR TBPEDIDO_PARCELAMENTO
ACTIVE BEFORE UPDATE POSITION 0
AS
DECLARE VARIABLE HISTORICO VARCHAR(1000);
DECLARE VARIABLE DESC_FORMA VARCHAR(100);
DECLARE VARIABLE DESC_CONDICAO VARCHAR(100);
BEGIN

    HISTORICO = '';
    SELECT TRIM(W.DESCRICAO) FROM TBPAGAMENTO_FORMA    W WHERE W.CODIGO = NEW.PAGAMENTO_FORMA    INTO DESC_FORMA;
    SELECT TRIM(W.DESCRICAO) FROM TBPAGAMENTO_CONDICAO W WHERE W.CODIGO = NEW.PAGAMENTO_CONDICAO INTO DESC_CONDICAO;

    IF(OLD.VALOR <> NEW.VALOR) THEN
    BEGIN
        HISTORICO = HISTORICO||'VALOR: R$ '||COALESCE(OLD.VALOR,0.00)||' -> R$ '||COALESCE(NEW.VALOR,0.00);
    END

    IF (HISTORICO <> '') THEN
    BEGIN
        HISTORICO = ' FORMA DE PAGAMENTO: '||FN_LPAD(OLD.PAGAMENTO_FORMA,4,0)||' - '||:DESC_FORMA||',CONDICAO: '||FN_LPAD(OLD.PAGAMENTO_CONDICAO,4,0)||' - '||DESC_CONDICAO||' ALTERADA: '||HISTORICO;
        EXECUTE PROCEDURE SPI_HISTORICO('TBPEDIDO',NEW.PEDIDO,FN_ELIPSES(HISTORICO,500));
    END
END^

/* Trigger: TTBPEDIDO_PARCELAMENTO_B01I */
CREATE OR ALTER TRIGGER TTBPEDIDO_PARCELAMENTO_B01I FOR TBPEDIDO_PARCELAMENTO
ACTIVE BEFORE INSERT POSITION 0
as
begin
  if (new.id is null) then
    new.id = gen_id(GTBPEDIDO_PARCELAMENTO,1);
end
^

/* Trigger: TTBPEDIDO_PARCELAMENTO_B02I */
CREATE OR ALTER TRIGGER TTBPEDIDO_PARCELAMENTO_B02I FOR TBPEDIDO_PARCELAMENTO
ACTIVE BEFORE INSERT POSITION 1
AS
DECLARE VARIABLE HISTORICO VARCHAR(1000);
DECLARE VARIABLE DESC_FORMA VARCHAR(100);
DECLARE VARIABLE DESC_CONDICAO VARCHAR(100);
BEGIN

    HISTORICO = '';
    SELECT TRIM(W.DESCRICAO) FROM TBPAGAMENTO_FORMA    W WHERE W.CODIGO = NEW.PAGAMENTO_FORMA    INTO DESC_FORMA;
    SELECT TRIM(W.DESCRICAO) FROM TBPAGAMENTO_CONDICAO W WHERE W.CODIGO = NEW.PAGAMENTO_CONDICAO INTO DESC_CONDICAO;

    HISTORICO = HISTORICO||' FORMA DE PAGAMENTO: '||FN_LPAD(NEW.PAGAMENTO_FORMA,4,0)||' - '||DESC_FORMA||', CONDICAO:'||FN_LPAD(NEW.PAGAMENTO_CONDICAO,4,0)||' - '||DESC_CONDICAO||', VALOR R$ '||COALESCE(NEW.VALOR,0.00)||' INCLUIDA';

    IF (HISTORICO <> '') THEN
    BEGIN
        EXECUTE PROCEDURE SPI_HISTORICO('TBPEDIDO',NEW.PEDIDO,FN_ELIPSES(HISTORICO,500));
    END
END
^

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

SET TERM ; ^