-- TRIGGER DE INSERT

CREATE TRIGGER TBESTOQUE_TRANSACAO_ITEM_B12I FOR TBESTOQUE_TRANSACAO_ITEM
ACTIVE BEFORE INSERT POSITION 1
AS
DECLARE VARIABLE HISTORICO VARCHAR(500);
BEGIN

    HISTORICO = '';

    IF ( NEW.CONTROLE > 0) THEN
    BEGIN
        HISTORICO = 'REGISTRO INCLUIDO';
    END

    IF (HISTORICO <> '') THEN
    BEGIN
        EXECUTE PROCEDURE SPI_HISTORICO('TBESTOQUE_TRANSACAO_ITEM',NEW.CONTROLE, HISTORICO);
    END
END;


-- TRIGGER DE UPDATE
CREATE or alter trigger tbestoque_transacao_item_b02u for tbestoque_transacao_item
active before update position 2
AS
DECLARE VARIABLE HISTORICO VARCHAR(500);
BEGIN

    HISTORICO = '';    
    
    IF (OLD.ESTABELECIMENTO_CODIGO <> NEW.ESTABELECIMENTO_CODIGO) THEN
    BEGIN
        HISTORICO = HISTORICO||'EST.'|| OLD.ESTABELECIMENTO_CODIGO||'->'||NEW.ESTABELECIMENTO_CODIGO;
    END

    IF (OLD.LOCALIZACAO_CODIGO <> NEW.LOCALIZACAO_CODIGO) THEN
    BEGIN
        HISTORICO = HISTORICO||' LOC.'|| OLD.LOCALIZACAO_CODIGO||'->'||NEW.LOCALIZACAO_CODIGO;
    END

    IF (OLD.PRODUTO_CODIGO <> NEW.PRODUTO_CODIGO) THEN
    BEGIN
        HISTORICO = HISTORICO||' PROD.'|| OLD.PRODUTO_CODIGO||'->'||NEW.PRODUTO_CODIGO;
    END

    IF (OLD.DOCUMENTO <> NEW.DOCUMENTO) THEN
    BEGIN
        HISTORICO = HISTORICO||' DOC.'|| OLD.DOCUMENTO||'->'||NEW.DOCUMENTO;
    END

    IF (OLD.OPERACAO_CODIGO <> NEW.OPERACAO_CODIGO) THEN
    BEGIN
        HISTORICO = HISTORICO||' OPER.'|| OLD.OPERACAO_CODIGO||'->'||NEW.OPERACAO_CODIGO;
    END

    IF (OLD.TIPO <> NEW.TIPO) THEN
    BEGIN
        HISTORICO = HISTORICO||' TIPO:'|| OLD.TIPO||'->'||NEW.TIPO;
    END

    IF (OLD.QUANTIDADE <> NEW.QUANTIDADE) THEN
    BEGIN
        HISTORICO = HISTORICO||' QTD.'|| OLD.QUANTIDADE||'->'||NEW.QUANTIDADE;
    END

    IF (OLD.T01 <> NEW.T01) THEN
    BEGIN
        HISTORICO = HISTORICO||' T01:'|| OLD.T01||'->'||NEW.T01;
    END

    IF (OLD.T02 <> NEW.T02) THEN
    BEGIN
        HISTORICO = HISTORICO||' T02:'|| OLD.T02||'->'||NEW.T02;
    END

    IF (OLD.T03 <> NEW.T03) THEN
    BEGIN
        HISTORICO = HISTORICO||' T03:'|| OLD.T03||'->'||NEW.T03;
    END

    IF (OLD.T04 <> NEW.T04) THEN
    BEGIN
        HISTORICO = HISTORICO||' T04:'|| OLD.T04||'->'||NEW.T04;
    END

    IF (OLD.T05 <> NEW.T05) THEN
    BEGIN
        HISTORICO = HISTORICO||' T05:'|| OLD.T05||'->'||NEW.T05;
    END

    IF (OLD.T06 <> NEW.T06) THEN
    BEGIN
        HISTORICO = HISTORICO||' T06:'|| OLD.T06||'->'||NEW.T06;
    END

    IF (OLD.T07 <> NEW.T07) THEN
    BEGIN
        HISTORICO = HISTORICO||' T07:'|| OLD.T07||'->'||NEW.T07;
    END

    IF (OLD.T08 <> NEW.T08) THEN
    BEGIN
        HISTORICO = HISTORICO||' T08:'|| OLD.T08||'->'||NEW.T08;
    END

    IF (OLD.T09 <> NEW.T09) THEN
    BEGIN
        HISTORICO = HISTORICO||' T09:'|| OLD.T09||'->'||NEW.T09;
    END

    IF (OLD.T10 <> NEW.T10) THEN
    BEGIN
        HISTORICO = HISTORICO||' T10:'|| OLD.T10||'->'||NEW.T10;
    END

    IF (OLD.T11 <> NEW.T11) THEN
    BEGIN
        HISTORICO = HISTORICO||' T11:'|| OLD.T11||'->'||NEW.T11;
    END

    IF (OLD.T12 <> NEW.T12) THEN
    BEGIN
        HISTORICO = HISTORICO||' T12:'|| OLD.T12||'->'||NEW.T12;
    END

    IF (OLD.T13 <> NEW.T13) THEN
    BEGIN
        HISTORICO = HISTORICO||' T13:'|| OLD.T13||'->'||NEW.T13;
    END

    IF (OLD.T14 <> NEW.T14) THEN
    BEGIN
        HISTORICO = HISTORICO||' T14:'|| OLD.T14||'->'||NEW.T14;
    END

    IF (OLD.T15 <> NEW.T15) THEN
    BEGIN
        HISTORICO = HISTORICO||' T15:'|| OLD.T15||'->'||NEW.T15;
    END

    IF (OLD.T16 <> NEW.T16) THEN
    BEGIN
        HISTORICO = HISTORICO||' T16:'|| OLD.T16||'->'||NEW.T16;
    END

    IF (OLD.T17 <> NEW.T17) THEN
    BEGIN
        HISTORICO = HISTORICO||' T17:'|| OLD.T17||'->'||NEW.T17;
    END

    IF (OLD.T18 <> NEW.T18) THEN
    BEGIN
        HISTORICO = HISTORICO||' T18:'|| OLD.T18||'->'||NEW.T18;
    END

    IF (OLD.T19 <> NEW.T19) THEN
    BEGIN
        HISTORICO = HISTORICO||' T19:'|| OLD.T19||'->'||NEW.T19;
    END

    IF (OLD.T20 <> NEW.T20) THEN
    BEGIN
        HISTORICO = HISTORICO||' T20:'|| OLD.T20||'->'||NEW.T20;
    END

    IF (OLD.CENTRO_DE_CUSTO <> NEW.CENTRO_DE_CUSTO) THEN
    BEGIN
        HISTORICO = HISTORICO||' C.CUSTO:'|| OLD.CENTRO_DE_CUSTO||'->'||NEW.CENTRO_DE_CUSTO;
    END

    IF (OLD.TURNO_CODIGO <> NEW.TURNO_CODIGO) THEN
    BEGIN
        HISTORICO = HISTORICO||' TURNO:'|| OLD.TURNO_CODIGO||'->'||NEW.TURNO_CODIGO;
    END

    IF (OLD.OBSERVACAO <> NEW.OBSERVACAO) THEN
    BEGIN
        HISTORICO = HISTORICO||' OBS:'|| OLD.OBSERVACAO||'->'||NEW.OBSERVACAO;
    END

    IF (HISTORICO <> '') THEN
    BEGIN
        EXECUTE PROCEDURE SPI_HISTORICO('TBESTOQUE_TRANSACAO_ITEM',NEW.CONTROLE, HISTORICO);
    END
END;

-- TRIGGER DE DELETE 

CREATE OR ALTER TRIGGER TBESTOQUE_TRANSACAO_ITEM_B12D FOR TBESTOQUE_TRANSACAO_ITEM
ACTIVE BEFORE DELETE POSITION 0
AS
DECLARE VARIABLE HISTORICO VARCHAR(500);
BEGIN

    HISTORICO = '';

    IF ( OLD.CONTROLE > 0) THEN
    BEGIN
        HISTORICO = 'REGISTRO EXCLUIDO';
    END

    IF (HISTORICO <> '') THEN
    BEGIN
        EXECUTE PROCEDURE SPI_HISTORICO('TBESTOQUE_TRANSACAO_ITEM',OLD.CONTROLE, HISTORICO);
    END
END;