CREATE INDEX ITBMODELO_17
ON TBMODELO (HABILITA_ECOMMERCE);


CREATE OR ALTER trigger ttbmodelo_biud_api for tbmodelo
active before insert or update or delete position 999
AS
begin

    if(updating)then
    begin
        if(
            new.descricao is distinct from old.descricao                        or
            new.habilita_ecommerce is distinct from old.habilita_ecommerce      or
            new.descricao_comercial is distinct from old.descricao_comercial    or
            new.categoria_id is distinct from old.categoria_id                  or

            new.t01 is distinct from old.t01 or
            new.t02 is distinct from old.t02 or
            new.t03 is distinct from old.t03 or
            new.t04 is distinct from old.t04 or
            new.t05 is distinct from old.t05 or
            new.t06 is distinct from old.t06 or
            new.t07 is distinct from old.t07 or
            new.t08 is distinct from old.t08 or
            new.t09 is distinct from old.t09 or
            new.t10 is distinct from old.t10 or
            new.t11 is distinct from old.t11 or
            new.t12 is distinct from old.t12 or
            new.t13 is distinct from old.t13 or
            new.t14 is distinct from old.t14 or
            new.t15 is distinct from old.t15 or
            new.t16 is distinct from old.t16 or
            new.t17 is distinct from old.t17 or
            new.t18 is distinct from old.t18 or
            new.t19 is distinct from old.t19 or
            new.t20 is distinct from old.t20
        )then
        begin
            execute procedure spi_integracao_api('TBMODELO',new.codigo,'A');
        end
    end else
    begin
        if(inserting)then execute procedure spi_integracao_api('TBMODELO',new.codigo,'I');
        if(deleting )then execute procedure spi_integracao_api('TBMODELO',old.codigo,'D');
    end

end;

DROP TRIGGER TTBCOR_BIUD_API;

CREATE or alter trigger ttbmodelo_cor_biud_api for tbmodelo_cor
active after insert or update or delete position 999
AS
begin
    if(inserting)then execute procedure spi_integracao_api('TBMODELO',new.modelo_id,'I');
    if(updating )then execute procedure spi_integracao_api('TBMODELO',new.modelo_id,'A');
    if(deleting )then execute procedure spi_integracao_api('TBMODELO',old.modelo_id,'D');
end;

CREATE trigger ttbproduto_biud_api for tbproduto
active before insert or update or delete position 999
AS
begin
  if(updating)then
    begin
        if(
            new.descricao is distinct from old.descricao
        )then
        begin
            execute procedure spi_integracao_api('TBMODELO',new.modelo_codigo,'A');
        end
    end else
    begin
        if(inserting)then execute procedure spi_integracao_api('TBMODELO',new.modelo_codigo,'I');
        if(deleting )then execute procedure spi_integracao_api('TBMODELO',old.modelo_codigo,'D');
    end
end;

CREATE trigger ttbean_biud_api for tbean
active before insert or update or delete position 999
AS
begin
    if(inserting)then execute procedure spi_integracao_api('TBMODELO',new.modelo_id,'I');
    if(updating )then execute procedure spi_integracao_api('TBMODELO',new.modelo_id,'A');
    if(deleting )then execute procedure spi_integracao_api('TBMODELO',old.modelo_id,'D');
end;

ALTER TABLE TBAPI ADD TABELA_PRECO_ID INTEGER;

CREATE trigger ttbmodelo_preco_biud_api for tbmodelo_preco
active before insert or update or delete position 999
AS
begin
    if(inserting)then execute procedure spi_integracao_api('TBMODELO_PRECO',new.modelo_codigo,'I');
    if(updating )then execute procedure spi_integracao_api('TBMODELO_PRECO',new.modelo_codigo,'A');
    if(deleting )then execute procedure spi_integracao_api('TBMODELO_PRECO',old.modelo_codigo,'D');
end;


CREATE OR ALTER trigger ttbestoque_saldo_999 for tbestoque_saldo
active before insert or update or delete position 999
AS
declare variable produto integer;
begin

  update tbreplica_tabela set Status = '0'
  Where tabela = 'TBESTOQUE_SALDO'
    and Status = '1';

  update tbreplica_tabela set Status = '0'
  Where tabela = 'TBREPOSICAO_CONSUMO'
    and Status = '1';

  --se vende no ecommerce
  produto = coalesce((select first 1 k.produto_id from SPC_SKUS_ECOMMERCE2(0,new.produto_codigo) k),0);
  if(produto > 0)then
  begin
      UPDATE OR INSERT INTO TBUPDATE_ECOMMERCE (TABELA, TABELA_ID, SUB_ID)
            VALUES ('TBESTOQUE_SALDO', new.produto_codigo, new.estabelecimento_codigo)
          MATCHING (TABELA, TABELA_ID, SUB_ID);
  end

  --se he lacloft
  produto = coalesce((
            select first 1
                p.codigo
            from tbproduto p, tbmodelo m
            where p.codigo = new.produto_codigo
            and p.status = '1'
            and m.codigo = p.modelo_codigo
            and m.habilita_ecommerce = 1
            and m.status = '1'
  ),0);

  if(produto > 0)then
  begin
      UPDATE OR INSERT INTO TBUPDATE_ECOM_LACLOFIT (TABELA, TABELA_ID, SUB_ID, TIPO)
            VALUES ('TBESTOQUE_SALDO', new.produto_codigo, 0, 1)
          MATCHING (TABELA, TABELA_ID, SUB_ID);
  end

  if(updating or inserting)then
  begin
    produto = coalesce((select first 1 y.id from tbapi y where y.estabelecimento_id = new.estabelecimento_codigo and y.localizacao_id = new.localizacao_codigo),0);
    if(produto > 0)then
    begin
        if(updating )then execute procedure spi_integracao_api('TBESTOQUE_SALDO',new.produto_codigo,'A');
    end
  end
end;

ALTER TABLE TBINTEGRACAO_API
    ADD DATA_UPDATE TIMESTAMP;

CREATE OR ALTER trigger ttbintegracao_api_b01i for tbintegracao_api
active before insert position 0
as
begin

  new.VERSSAO = 1;  
  new.data_update = current_timestamp;

  if (new.id is null) then
    new.id = gen_id(gTBINTEGRACAO_API,1);
end;

CREATE OR ALTER trigger ttbintegracao_api_b01u for tbintegracao_api
active before update position 0
AS
begin
  new.data_update = current_timestamp;
  new.verssao = new.verssao + 1;
end;

CREATE trigger ttbpedido_bu_api for tbpedido
active before update position 999
AS
begin
    if(new.codigo_rastreio is distinct from old.codigo_rastreio)then
    begin
        if(updating )then execute procedure spi_integracao_api('TBPEDIDO_RATREAMENTO',new.pedido,'A');
    end
end;

ALTER TABLE TBAPI
    ADD PERFIL_PEDIDO VARCHAR(10);

