CREATE OR ALTER TRIGGER TBCLIENTE_B06I FOR TBCLIENTE
ACTIVE BEFORE INSERT POSITION 6
AS
BEGIN

  IF(COALESCE(NEW.REPRESENTANTE_CODIGO, 0) = 0) THEN
  BEGIN
      NEW.REPRESENTANTE_CODIGO = FN_PARAMETRO('SISTEMA.REPRESENTANTE_PADRAO');

      NEW.REPRESENTANTE_COMISSAO = COALESCE((SELECT T.COMISSAO FROM TBREPRESENTANTE T WHERE T.CODIGO = FN_PARAMETRO('SISTEMA.REPRESENTANTE_PADRAO')), 0);
  END

  IF(COALESCE(NEW.EMPRESA_VINCULO, 0) = 0) THEN
  BEGIN
      NEW.EMPRESA_VINCULO = NEW.CODIGO;
  END

END;

CREATE OR ALTER TRIGGER TTBREPRESENTANTE_B05I FOR TBREPRESENTANTE
ACTIVE BEFORE INSERT POSITION 0
AS
begin
  IF(TRIM(COALESCE(NEW.FORMA_COMISSAO,'')) = '')THEN
  BEGIN
     NEW.FORMA_COMISSAO = 'F';
  END

  IF(COALESCE(NEW.SUPERVISOR_ID,0) = 0)THEN
  BEGIN
     NEW.SUPERVISOR_ID = FN_PARAMETRO('SISTEMA.SUPERVISOR_PADRAO');
  END
end;

INSERT INTO TBPARAMETRO (
    ID, 
    TABELA, 
    CONTROLE, 
    CODIGO, 
    DESCRICAO, 
    VALOR, 
    OBSERVACAO, 
    GRUPO
) VALUES (
    GEN_ID(GTBPARAMETRO,1), 
    'SISTEMA', 
    NULL,
    'SUPERVISOR_PADRAO', 
    'SUPERVISOR PADRÃO PARA REPRESENTANTES DO SISTEMA', 
    0, 
    '', 
    'WEB14011'            
);