ALTER TABLE TBTRANSPORTADORA
    ADD ECOMMERCE INTEGER DEFAULT 0;

CREATE OR ALTER trigger ttbtransportadora_999 for tbtransportadora
active before insert or update or delete position 999
AS
begin

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

  if(new.ecommerce = 1)then
  begin
  UPDATE OR INSERT INTO TBUPDATE_ECOMMERCE (ID, TABELA, TABELA_ID, SUB_ID)
        VALUES (gen_id(gtbupdate_ecommerce,1), 'TBTRANSPORTADORA', new.codigo, 0)
      MATCHING (ID, TABELA, TABELA_ID, SUB_ID);

  end

end;

CREATE OR ALTER trigger ttbtransportadora_999 for tbtransportadora
active before insert or update or delete position 999
AS
begin

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

  if(new.ecommerce = 1 and old.ecommerce = 0)then
  begin
  UPDATE OR INSERT INTO TBUPDATE_ECOMMERCE (ID, TABELA, TABELA_ID, SUB_ID)
        VALUES (gen_id(gtbupdate_ecommerce,1), 'TBTRANSPORTADORA', new.codigo, 0)
      MATCHING (TABELA, TABELA_ID, SUB_ID);

  end

end;


CREATE OR ALTER trigger ttbestoque_saldo_999 for tbestoque_saldo
active before insert or update or delete position 999
AS
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';

  UPDATE OR INSERT INTO TBUPDATE_ECOMMERCE (ID, TABELA, TABELA_ID, SUB_ID)
        VALUES (gen_id(gtbupdate_ecommerce,1), 'TBESTOQUE_SALDO', new.produto_codigo, new.estabelecimento_codigo)
      MATCHING (TABELA, TABELA_ID, SUB_ID);

end;

CREATE OR ALTER trigger ttbempresa_b03i for tbempresa
active before insert position 0
AS
begin
    if(new.ecommerce = 1)then
    begin
       INSERT INTO TBCLIENTE (
             RAZAOSOCIAL,
             CODIGO,
             REPRESENTANTE_CODIGO,
             TRANSPORTADORA_CODIGO,
             PAGAMENTO_CONDICAO,
             PAGAMENTO_FORMA,
             STATUS,
             UF,
             BLOQUEIA_PEDIDO,
             BLOQUEIA_NOTAFISCAL,
             REPRESENTANTE_COMISSAO,
             GRADE_SECUNDARIA,
             LIMITE_CREDITO,
             SUFRAMA,
             GRUPO,
             BOQUEIA_PAGAMENTO_CONDICAO,
             TIPO_CODIGO,
             VENDEDOR_CODIGO,
             MATRIZ_ID,
             VENDEDOR_COMISSAO,
             ATENDE_GRADE_COMPLETA,
             SUFRAMA_VENCIMENTO,
             LOJA,
             BLOQUEIO_DIAS_ATRASO,
             BLOQUEIO_DIAS_SEMMOV,
             TAG,
             PRIORIDADE,
             INATIVA_DIAS_SEMMOV,
             CONFIRMACAO_AUTOMATICA,
             PAFECF,
             FRETE,
             SUBSTITUIDO_TRIBUTARIO,
             DATA_BLOQUEIO,
             BLOQUEIO_MOTIVO,
             NOTIFICAR_DIAS_ATRASO,
             NOTIFICAR_ATRASO,
             ETIQUETA,
             EMPRESA_VINCULO,
             BLOQUEIO_DIAS_ATRASO_NF,
             ECOMMERCE
       ) VALUES (
             new.razaosocial,
             new.codigo,
             coalesce((SELECT FIRST 1 P.valor_ext  FROM tbcontrole_n P WHERE P.id = 272),0),
             0,
             0,
             0,
             '0   ',
             new.uf,
             '',
             '',
             0,
             '0   ',
             0,
             '',
             '0   ',
             '    ',
             1,
             0,
             0,
             0,
             '    ',
             NULL,
             '',
             0,
             0,
             '    ',
             10,
             0,
             '    ',
             '',
             0,
             '0   ',
             NULL,
             '',
             0,
             0,
             substring(new.razaosocial from 1 for 6),
             new.codigo,
             0,
             1
        );
    end
end;

CREATE trigger ttbpedido_b10i for tbpedido
active before insert position 0
AS
begin
  if(new.ecommerce = 1)then
  begin
    new.representante_codigo = coalesce((SELECT FIRST 1 P.valor_ext  FROM tbcontrole_n P WHERE P.id = 272),0);
	new.familia_codigo = null;
  end
end;

COMMENT ON TRIGGER TTBPEDIDO_B10I IS 'adiciona representante se for ecommerce';

