/******************************************************************************/
/****              Generated by IBExpert 13/11/2024 07:03:34               ****/
/******************************************************************************/

/******************************************************************************/
/****     Following SET SQL DIALECT is just for the Database Comparer      ****/
/******************************************************************************/
SET SQL DIALECT 3;



/******************************************************************************/
/****                                Tables                                ****/
/******************************************************************************/



CREATE TABLE TBMODELO_REMESSA_COTA_GP (
    MODELO_ID  INTEGER NOT NULL,
    GP_ID  	   INTEGER NOT NULL,
    MI01       INTEGER,
    MI02       INTEGER,
    MI03       INTEGER,
    MI04       INTEGER,
    MI05       INTEGER,
    MI06       INTEGER,
    MI07       INTEGER,
    MI08       INTEGER,
    MI09       INTEGER,
    MI10       INTEGER,
    MI11       INTEGER,
    MI12       INTEGER,
    MI13       INTEGER,
    MI14       INTEGER,
    MI15       INTEGER,
    MI16       INTEGER,
    MI17       INTEGER,
    MI18       INTEGER,
    MI19       INTEGER,
    MI20       INTEGER,
    MU01       INTEGER,
    MU02       INTEGER,
    MU03       INTEGER,
    MU04       INTEGER,
    MU05       INTEGER,
    MU06       INTEGER,
    MU07       INTEGER,
    MU08       INTEGER,
    MU09       INTEGER,
    MU10       INTEGER,
    MU11       INTEGER,
    MU12       INTEGER,
    MU13       INTEGER,
    MU14       INTEGER,
    MU15       INTEGER,
    MU16       INTEGER,
    MU17       INTEGER,
    MU18       INTEGER,
    MU19       INTEGER,
    MU20       INTEGER
);




/******************************************************************************/
/****                             Primary keys                             ****/
/******************************************************************************/

ALTER TABLE TBMODELO_REMESSA_COTA_GP ADD PRIMARY KEY (MODELO_ID, GP_ID);


/******************************************************************************/
/****                               Triggers                               ****/
/******************************************************************************/



SET TERM ^ ;



/******************************************************************************/
/****                         Triggers for tables                          ****/
/******************************************************************************/



/* Trigger: TTBMODELO_REMESSA_COTA_GP_B9U */
CREATE OR ALTER TRIGGER TTBMODELO_REMESSA_COTA_GP_B9U FOR TBMODELO_REMESSA_COTA_GP
ACTIVE BEFORE UPDATE POSITION 9
AS
DECLARE VARIABLE HISTORICO VARCHAR(500);
DECLARE VARIABLE Grade Integer;
DECLARE VARIABLE G01 VarChar(10); DECLARE VARIABLE G02 VarChar(10);
DECLARE VARIABLE G03 VarChar(10); DECLARE VARIABLE G04 VarChar(10);
DECLARE VARIABLE G05 VarChar(10); DECLARE VARIABLE G06 VarChar(10);
DECLARE VARIABLE G07 VarChar(10); DECLARE VARIABLE G08 VarChar(10);
DECLARE VARIABLE G09 VarChar(10); DECLARE VARIABLE G10 VarChar(10);
DECLARE VARIABLE G11 VarChar(10); DECLARE VARIABLE G12 VarChar(10);
DECLARE VARIABLE G13 VarChar(10); DECLARE VARIABLE G14 VarChar(10);
DECLARE VARIABLE G15 VarChar(10); DECLARE VARIABLE G16 VarChar(10);
DECLARE VARIABLE G17 VarChar(10); DECLARE VARIABLE G18 VarChar(10);
DECLARE VARIABLE G19 VarChar(10); DECLARE VARIABLE G20 VarChar(10);
begin

  For
  Select Coalesce(M.Grade_Codigo,0),
         Coalesce(G.T01,''), Coalesce(G.T02,''), Coalesce(G.T03,''), Coalesce(G.T04,''),
         Coalesce(G.T05,''), Coalesce(G.T06,''), Coalesce(G.T07,''), Coalesce(G.T08,''),
         Coalesce(G.T09,''), Coalesce(G.T10,''), Coalesce(G.T11,''), Coalesce(G.T12,''),
         Coalesce(G.T13,''), Coalesce(G.T14,''), Coalesce(G.T15,''), Coalesce(G.T16,''),
         Coalesce(G.T17,''), Coalesce(G.T18,''), Coalesce(G.T19,''), Coalesce(G.T20,'')
    From TbModelo M
   Left join TbGrade G On G.Codigo = M.Grade_Codigo
   Where M.Codigo = Old.Modelo_Id
   Into :Grade,:G01,:G02,:G03,:G04,:G05,:G06,:G07,:G08,:G09,
     :G10,:G11,:G12,:G13,:G14,:G15,:G16,:G17,:G18,:G19,:G20
   DO

  if (:Grade > 0) then
      Begin

      
      if (OLD.MI01 <> NEW.MI01) then
           BEGIN
           HISTORICO = HISTORICO||' GP.: '||OLD.GP_ID||' ['||:G01||': '||Trunc(OLD.MI01)||'->'||Trunc(NEW.MI01)||']';
           END
    
       if (OLD.MI02 <> NEW.MI02) then
           BEGIN
           HISTORICO = HISTORICO||' GP.: '||OLD.GP_ID||' ['||:G02||': '||Trunc(OLD.MI02)||'->'||Trunc(NEW.MI02)||']';
           END
    
       if (OLD.MI03 <> NEW.MI03) then
           BEGIN
           HISTORICO = HISTORICO||' GP.: '||OLD.GP_ID||' ['||:G03||': '||Trunc(OLD.MI03)||'->'||Trunc(NEW.MI03)||']';
           END
    
       if (OLD.MI04 <> NEW.MI04) then
           BEGIN
           HISTORICO = HISTORICO||' GP.: '||OLD.GP_ID||' ['||:G04||': '||Trunc(OLD.MI04)||'->'||Trunc(NEW.MI04)||']';
           END
    
       if (OLD.MI05 <> NEW.MI05) then
           BEGIN
           HISTORICO = HISTORICO||' GP.: '||OLD.GP_ID||' ['||:G05||': '||Trunc(OLD.MI05)||'->'||Trunc(NEW.MI05)||']';
           END
    
       if (OLD.MI06 <> NEW.MI06) then
           BEGIN
           HISTORICO = HISTORICO||' GP.: '||OLD.GP_ID||' ['||:G06||': '||Trunc(OLD.MI06)||'->'||Trunc(NEW.MI06)||']';
           END
    
       if (OLD.MI07 <> NEW.MI07) then
           BEGIN
           HISTORICO = HISTORICO||' GP.: '||OLD.GP_ID||' ['||:G07||': '||Trunc(OLD.MI07)||'->'||Trunc(NEW.MI07)||']';
           END
    
       if (OLD.MI08 <> NEW.MI08) then
           BEGIN
           HISTORICO = HISTORICO||' GP.: '||OLD.GP_ID||' ['||:G08||': '||Trunc(OLD.MI08)||'->'||Trunc(NEW.MI08)||']';
           END
    
       if (OLD.MI09 <> NEW.MI09) then
           BEGIN
           HISTORICO = HISTORICO||' GP.: '||OLD.GP_ID||' ['||:G09||': '||Trunc(OLD.MI09)||'->'||Trunc(NEW.MI09)||']';
           END
    
       if (OLD.MI10 <> NEW.MI10) then
           BEGIN
           HISTORICO = HISTORICO||' GP.: '||OLD.GP_ID||' ['||:G10||': '||Trunc(OLD.MI10)||'->'||Trunc(NEW.MI10)||']';
           END
    
       if (OLD.MI11 <> NEW.MI11) then
           BEGIN
           HISTORICO = HISTORICO||' GP.: '||OLD.GP_ID||' ['||:G11||': '||Trunc(OLD.MI11)||'->'||Trunc(NEW.MI11)||']';
           END
    
       if (OLD.MI12 <> NEW.MI12) then
           BEGIN
           HISTORICO = HISTORICO||' GP.: '||OLD.GP_ID||' ['||:G12||': '||Trunc(OLD.MI12)||'->'||Trunc(NEW.MI12)||']';
           END
    
       if (OLD.MI13 <> NEW.MI13) then
           BEGIN
           HISTORICO = HISTORICO||' GP.: '||OLD.GP_ID||' ['||:G13||': '||Trunc(OLD.MI13)||'->'||Trunc(NEW.MI13)||']';
           END
    
       if (OLD.MI14 <> NEW.MI14) then
           BEGIN
           HISTORICO = HISTORICO||' GP.: '||OLD.GP_ID||' ['||:G14||': '||Trunc(OLD.MI14)||'->'||Trunc(NEW.MI14)||']';
           END
    
       if (OLD.MI15 <> NEW.MI15) then
           BEGIN
           HISTORICO = HISTORICO||' GP.: '||OLD.GP_ID||' ['||:G15||': '||Trunc(OLD.MI15)||'->'||Trunc(NEW.MI15)||']';
           END
    
       if (OLD.MI16 <> NEW.MI16) then
           BEGIN
           HISTORICO = HISTORICO||' GP.: '||OLD.GP_ID||' ['||:G16||': '||Trunc(OLD.MI16)||'->'||Trunc(NEW.MI16)||']';
           END
    
       if (OLD.MI17 <> NEW.MI17) then
           BEGIN
           HISTORICO = HISTORICO||' GP.: '||OLD.GP_ID||' ['||:G17||': '||Trunc(OLD.MI17)||'->'||Trunc(NEW.MI17)||']';
           END
    
       if (OLD.MI18 <> NEW.MI18) then
           BEGIN
           HISTORICO = HISTORICO||' GP.: '||OLD.GP_ID||' ['||:G18||': '||Trunc(OLD.MI18)||'->'||Trunc(NEW.MI18)||']';
           END
    
       if (OLD.MI19 <> NEW.MI19) then
           BEGIN
           HISTORICO = HISTORICO||' GP.: '||OLD.GP_ID||' ['||:G19||': '||Trunc(OLD.MI19)||'->'||Trunc(NEW.MI19)||']';
           END
    
       if (OLD.MI20 <> NEW.MI20) then
           BEGIN
           HISTORICO = HISTORICO||' GP.: '||OLD.GP_ID||' ['||:G20||': '||Trunc(OLD.MI20)||'->'||Trunc(NEW.MI20)||']';
           END

   if (HISTORICO <> ' ') Then
       Begin
        HISTORICO = 'MODELO: '||COALESCE(OLD.MODELO_ID, 0)||' - COTAS PARA REMESSAS ALTERADAS: '||:HISTORICO;
       INSERT INTO TBHISTORICO (
       TABELA, TABELA_ID, USUARIO, IP, HISTORICO) VALUES (
      'TBMODELO', OLD.MODELO_ID,
      (Select First 1 MON$USER from mon$attachments where mon$attachment_id = current_connection),
      (Select First 1 MON$REMOTE_ADDRESS from mon$attachments where mon$attachment_id = current_connection),
       :Historico);
       End

        HISTORICO = '';

      if (OLD.MU01 <> NEW.MU01) then
           BEGIN
           HISTORICO = HISTORICO||' GP.: '||OLD.GP_ID||' ['||:G01||': '||Trunc(OLD.MU01)||'->'||Trunc(NEW.MU01)||']';
           END
    
       if (OLD.MU02 <> NEW.MU02) then
           BEGIN
           HISTORICO = HISTORICO||' GP.: '||OLD.GP_ID||' ['||:G02||': '||Trunc(OLD.MU02)||'->'||Trunc(NEW.MU02)||']';
           END
    
       if (OLD.MU03 <> NEW.MU03) then
           BEGIN
           HISTORICO = HISTORICO||' GP.: '||OLD.GP_ID||' ['||:G03||': '||Trunc(OLD.MU03)||'->'||Trunc(NEW.MU03)||']';
           END
    
       if (OLD.MU04 <> NEW.MU04) then
           BEGIN
           HISTORICO = HISTORICO||' GP.: '||OLD.GP_ID||' ['||:G04||': '||Trunc(OLD.MU04)||'->'||Trunc(NEW.MU04)||']';
           END
    
       if (OLD.MU05 <> NEW.MU05) then
           BEGIN
           HISTORICO = HISTORICO||' GP.: '||OLD.GP_ID||' ['||:G05||': '||Trunc(OLD.MU05)||'->'||Trunc(NEW.MU05)||']';
           END
    
       if (OLD.MU06 <> NEW.MU06) then
           BEGIN
           HISTORICO = HISTORICO||' GP.: '||OLD.GP_ID||' ['||:G06||': '||Trunc(OLD.MU06)||'->'||Trunc(NEW.MU06)||']';
           END
    
       if (OLD.MU07 <> NEW.MU07) then
           BEGIN
           HISTORICO = HISTORICO||' GP.: '||OLD.GP_ID||' ['||:G07||': '||Trunc(OLD.MU07)||'->'||Trunc(NEW.MU07)||']';
           END
    
       if (OLD.MU08 <> NEW.MU08) then
           BEGIN
           HISTORICO = HISTORICO||' GP.: '||OLD.GP_ID||' ['||:G08||': '||Trunc(OLD.MU08)||'->'||Trunc(NEW.MU08)||']';
           END
    
       if (OLD.MU09 <> NEW.MU09) then
           BEGIN
           HISTORICO = HISTORICO||' GP.: '||OLD.GP_ID||' ['||:G09||': '||Trunc(OLD.MU09)||'->'||Trunc(NEW.MU09)||']';
           END
    
       if (OLD.MU10 <> NEW.MU10) then
           BEGIN
           HISTORICO = HISTORICO||' GP.: '||OLD.GP_ID||' ['||:G10||': '||Trunc(OLD.MU10)||'->'||Trunc(NEW.MU10)||']';
           END
    
       if (OLD.MU11 <> NEW.MU11) then
           BEGIN
           HISTORICO = HISTORICO||' GP.: '||OLD.GP_ID||' ['||:G11||': '||Trunc(OLD.MU11)||'->'||Trunc(NEW.MU11)||']';
           END
    
       if (OLD.MU12 <> NEW.MU12) then
           BEGIN
           HISTORICO = HISTORICO||' GP.: '||OLD.GP_ID||' ['||:G12||': '||Trunc(OLD.MU12)||'->'||Trunc(NEW.MU12)||']';
           END
    
       if (OLD.MU13 <> NEW.MU13) then
           BEGIN
           HISTORICO = HISTORICO||' GP.: '||OLD.GP_ID||' ['||:G13||': '||Trunc(OLD.MU13)||'->'||Trunc(NEW.MU13)||']';
           END
    
       if (OLD.MU14 <> NEW.MU14) then
           BEGIN
           HISTORICO = HISTORICO||' GP.: '||OLD.GP_ID||' ['||:G14||': '||Trunc(OLD.MU14)||'->'||Trunc(NEW.MU14)||']';
           END
    
       if (OLD.MU15 <> NEW.MU15) then
           BEGIN
           HISTORICO = HISTORICO||' GP.: '||OLD.GP_ID||' ['||:G15||': '||Trunc(OLD.MU15)||'->'||Trunc(NEW.MU15)||']';
           END
    
       if (OLD.MU16 <> NEW.MU16) then
           BEGIN
           HISTORICO = HISTORICO||' GP.: '||OLD.GP_ID||' ['||:G16||': '||Trunc(OLD.MU16)||'->'||Trunc(NEW.MU16)||']';
           END
    
       if (OLD.MU17 <> NEW.MU17) then
           BEGIN
           HISTORICO = HISTORICO||' GP.: '||OLD.GP_ID||' ['||:G17||': '||Trunc(OLD.MU17)||'->'||Trunc(NEW.MU17)||']';
           END
    
       if (OLD.MU18 <> NEW.MU18) then
           BEGIN
           HISTORICO = HISTORICO||' GP.: '||OLD.GP_ID||' ['||:G18||': '||Trunc(OLD.MU18)||'->'||Trunc(NEW.MU18)||']';
           END
    
       if (OLD.MU19 <> NEW.MU19) then
           BEGIN
           HISTORICO = HISTORICO||' GP.: '||OLD.GP_ID||' ['||:G19||': '||Trunc(OLD.MU19)||'->'||Trunc(NEW.MU19)||']';
           END
    
       if (OLD.MU20 <> NEW.MU20) then
           BEGIN
           HISTORICO = HISTORICO||' GP.: '||OLD.GP_ID||' ['||:G20||': '||Trunc(OLD.MU20)||'->'||Trunc(NEW.MU20)||']';
           END

   if (HISTORICO <> '') Then
       Begin
       
       HISTORICO = 'MODELO: '||COALESCE(OLD.MODELO_ID, 0)||' - COTAS PARA REMESSAS ALTERADAS: '||:HISTORICO;
       
       INSERT INTO TBHISTORICO (
       TABELA, TABELA_ID, USUARIO, IP, HISTORICO) VALUES (
      'TBMODELO', OLD.MODELO_ID,
      (Select First 1 MON$USER from mon$attachments where mon$attachment_id = current_connection),
      (Select First 1 MON$REMOTE_ADDRESS from mon$attachments where mon$attachment_id = current_connection),
       :Historico);
       End

   End

End
^

SET TERM ; ^


