CREATE OR ALTER TRIGGER TTBESTABELECIMENTO_B03I FOR TBESTABELECIMENTO
ACTIVE BEFORE INSERT POSITION 0
AS
DECLARE VARIABLE CODIGO INTEGER;
BEGIN
  IF(NEW.CODIGO IS NULL) THEN
  BEGIN
      NEW.CODIGO = GEN_ID(GTBESTABELECIMENTO,1);
  END

  FOR SELECT D.CODIGO FROM TBMODULO D INTO :CODIGO DO
  BEGIN
      INSERT INTO TBCONFIGURACAO_PERIODO(ESTABELECIMENTO_CODIGO,MODULO_CODIGO,DATAINICIAL,DATAFINAL)
         VALUES(NEW.CODIGO,:CODIGO,FN_START_OF_MONTH_2(),FN_END_OF_MONTH_2());
  END
END;