
CREATE OR ALTER trigger ttbremessa_item_processado_b98u for tbremessa_item_processado
active before update position 98
AS
begin
  if(new.situacao is distinct from old.situacao)then
  begin
     IF(new.situacao < OLD.situacao)THEN
     BEGIN
        execute procedure spi_historico('TBREMESSA', new.remessa, 'TALAO:'||fn_lpad(NEW.controle,5,'0')||' STATUS:'||coalesce(OLD.situacao,0)||'->'||coalesce(NEW.situacao,0));
     END

     IF(new.situacao = 3 AND OLD.situacao <> 3 AND NEW.operador_liberacao > 0 AND NEW.data_liberacao > '01.01.2000') THEN
     BEGIN
        execute procedure spi_historico('TBREMESSA', new.remessa, 'TALAO:'||fn_lpad(NEW.controle,5,'0')||' LIBERADO.');
     END

     IF(new.situacao = 6) THEN
     BEGIN
        execute procedure spi_historico('TBREMESSA', new.remessa, 'TALAO:'||fn_lpad(NEW.controle,5,'0')||' ENCERRADO.');
     END

  end
end;

