SET TERM ^ ;

CREATE OR ALTER procedure SPC_LISTAR_REMSSA (
    DATA1 date = null,
    DATA2 date = null,
    FAMILIAS blob sub_type 1 segment size 80 = '',
    ESTABELECIMENTO integer = 0)
returns (
    REMESSA varchar(40),
    REMESSA_ID integer)
as
declare variable OREMESSA integer;
declare variable TIPO_REMESSA integer;
begin

    for
        SELECT first 50
        
            R.REMESSA,
            R.NUMERO
        
        FROM TBPEDIDO_ITEM_PROCESSADO A, TBREMESSA R, TBMODELO M
        WHERE A.ESTABELECIMENTO_CODIGO = :ESTABELECIMENTO
        AND A.ESTABELECIMENTO_CODIGO = R.ESTABELECIMENTO_CODIGO
        AND A.REMESSA = R.NUMERO
        AND A.MODELO_CODIGO = M.CODIGO
        
        --and iif(:STATUS_TALAO > 0 , iif(:STATUS_TALAO = 2, A.SITUACAO < 3 and A.DATA_PRODUCAO is NULL, A.SITUACAO = 3 and A.DATA_PRODUCAO is NULL), true)
        and iif(:DATA1 is distinct from null and :DATA2 is distinct from null, R.DATA BETWEEN :DATA1 AND :DATA2, true)
        and iif(char_length(:FAMILIAS) > 1, M.FAMILIA_CODIGO in (select osplit from split(:FAMILIAS,',')),true)
        
    into
        REMESSA,
        REMESSA_ID
    do begin
        suspend;
    end

end
^

SET TERM ; ^

SET TERM ^ ;

CREATE trigger ttbrepresentante_b987iud for tbrepresentante
active before insert or update or delete position 987
AS
begin
    if(inserting)then execute procedure spi_integracao_mobile('TBREPRESENTANTE',new.codigo,'I');
    if(updating )then execute procedure spi_integracao_mobile('TBREPRESENTANTE',new.codigo,'A');
    if(deleting )then execute procedure spi_integracao_mobile('TBREPRESENTANTE',old.codigo,'D');
end
^

SET TERM ; ^

update TBREPRESENTANTE f set f.status = f.status;

SET TERM ^ ;

CREATE trigger ttbperfil_b987iud for tbperfil
active before insert or update or delete position 987
AS
begin
    if(inserting)then execute procedure spi_integracao_mobile('TBPERFIL',new.CONTROLE,'I');
    if(updating )then execute procedure spi_integracao_mobile('TBPERFIL',new.CONTROLE,'A');
    if(deleting )then execute procedure spi_integracao_mobile('TBPERFIL',old.CONTROLE,'D');
end
^

SET TERM ; ^

update TBPERFIL f set f.qtd_tipo = f.qtd_tipo;

SET TERM ^ ;

CREATE trigger ttbtransportadora_b987iud for tbtransportadora
active before insert or update or delete position 987
AS
begin
    if(inserting)then execute procedure spi_integracao_mobile('TBTRANSPORTADORA',new.codigo,'I');
    if(updating )then execute procedure spi_integracao_mobile('TBTRANSPORTADORA',new.codigo,'A');
    if(deleting )then execute procedure spi_integracao_mobile('TBTRANSPORTADORA',old.codigo,'D');
end
^

SET TERM ; ^

update TBTRANSPORTADORA f set f.status = f.status;

SET TERM ^ ;

CREATE trigger ttbcliente_tipo_b987iud for tbcliente_tipo
active before insert or update or delete position 987
AS
begin
    if(inserting)then execute procedure spi_integracao_mobile('TBCLIENTE_TIPO',new.codigo,'I');
    if(updating )then execute procedure spi_integracao_mobile('TBCLIENTE_TIPO',new.codigo,'A');
    if(deleting )then execute procedure spi_integracao_mobile('TBCLIENTE_TIPO',old.codigo,'D');
end
^

SET TERM ; ^

update TBCLIENTE_TIPO f set f.status = f.status;

SET TERM ^ ;

CREATE trigger ttbcliente_subtipo_b987iud for TBCLIENTE_SUBTIPO
active before insert or update or delete position 987
AS
begin
    if(inserting)then execute procedure spi_integracao_mobile('TBCLIENTE_SUBTIPO',new.ID,'I');
    if(updating )then execute procedure spi_integracao_mobile('TBCLIENTE_SUBTIPO',new.ID,'A');
    if(deleting )then execute procedure spi_integracao_mobile('TBCLIENTE_SUBTIPO',old.ID,'D');
end
^

SET TERM ; ^

update TBCLIENTE_SUBTIPO f set f.status = f.status;