ALTER TABLE TBPEDIDO_ITEM_PROCESSADO
    ADD DATA_PRODUCAO_ETAPA DATE,
    ADD HORA_PRODUCAO_ETAPA TIMESTAMP;

SET TERM ^ ;

CREATE OR ALTER trigger ttbremessa_setor_b1000u for tbremessa_setor
active before update position 1000
AS
begin
  if(new.situacao is distinct from old.situacao)then
  begin
    if(new.situacao = 1 and new.esteira_producao = '1')then
    begin
        update tbpedido_item_processado p set
            p.data_producao_etapa = coalesce((
                select
                    dateadd(day, a.add_dias_ini, dateadd(day, a.add_dias_fim * -1, current_date))
                from TBHORARIO_GP a
                where a.gp_id = p.programacao_esteira
                and iif(a.hora_1 < a.hora_2,
                        current_time between a.hora_1 and a.hora_2,
                        current_time between a.hora_2 and '00:00' or current_time between '00:00' and a.hora_1
                )
            ), current_date),
            p.hora_producao_etapa = current_timestamp
        where p.remessa = new.remessa
        and p.controle = new.talao_id;
    end

    if(new.situacao = 0 and new.esteira_producao = '1')then
    begin
        update tbpedido_item_processado p set
            p.data_producao_etapa = null,
            p.hora_producao_etapa = null
        where p.remessa = new.remessa
        and p.controle = new.talao_id;
    end

  end
end
^

SET TERM ; ^



COMMENT ON TRIGGER TTBREMESSA_SETOR_B1000U IS 'calcula data de producao e aplica no taao ao registrar a etapa de producao do talao';

CREATE INDEX ITBPEDIDO_ITEM_PROCESSADO_45
ON TBPEDIDO_ITEM_PROCESSADO (HORA_PRODUCAO_ETAPA);

CREATE INDEX ITBPEDIDO_ITEM_PROCESSADO_44
ON TBPEDIDO_ITEM_PROCESSADO (DATA_PRODUCAO_ETAPA);


SET TERM ^ ;

CREATE OR ALTER trigger ttbremessa_setor_b1000u for tbremessa_setor
active before update position 1000
AS
begin
  if(new.situacao is distinct from old.situacao)then
  begin
    if(new.situacao = 1 and new.esteira_producao = '1')then
    begin
        update tbpedido_item_processado p set
            p.data_producao_etapa = coalesce((
                select first 1
                    dateadd(day, a.add_dias_ini, dateadd(day, a.add_dias_fim * -1, current_date))
                from TBHORARIO_GP a
                where a.gp_id = p.programacao_esteira
                and iif(a.hora_1 < a.hora_2,
                        current_time between a.hora_1 and a.hora_2,
                        current_time between a.hora_1 and '23:59:59' or current_time between '00:00' and a.hora_2
                )
            ), current_date),
            p.hora_producao_etapa = current_timestamp
        where p.remessa = new.remessa
        and p.controle = new.talao_id;
    end

    if(new.situacao = 0 and new.esteira_producao = '1')then
    begin
        update tbpedido_item_processado p set
            p.data_producao_etapa = null,
            p.hora_producao_etapa = null
        where p.remessa = new.remessa
        and p.controle = new.talao_id;
    end

  end
end
^

SET TERM ; ^

