Um pequeno exemplo de uma store procedure em Firebird
February 9th, 2009Aqui vc encontra um exemplo de uma procedure que ao mesmo
tempo que dá entrada ou saída no saldo de estoque por lote/validade já
lança numa tabela de registros os itens que entraram ou saíram para
uma posterior auditoria.
CREATE OR ALTER PROCEDURE AJUSTA_ESTOQUE_LOTE (
vis_produto integer,
vis_filial integer,
vis_setor integer,
vis_setordestino integer,
vop varchar(2),
vqtde numeric(18,3),
vlote varchar(30),
vvalidade date,
vis_orcamento double precision,
vis_usuario integer)
returns (
r integer)
as
declare variable id integer;
declare variable id2 integer;
declare variable id3 integer;
declare variable sentenca varchar(3000);
declare variable nomecampo varchar(32);
declare variable data date;
declare variable hora time;
declare variable mes varchar(3);
declare variable ano smallint;
declare variable mes_int smallint;
begin
r=0;
id=0;
id2=0;
if (:vis_setor IS NULL) then
suspend;
sentenca =
’select
case
when extract(month from cast(”’||:vvalidade||”’ as date))=1 then ”JAN”
when extract(month from cast(”’||:vvalidade||”’ as date))=2 then ”FEV”
when extract(month from cast(”’||:vvalidade||”’ as date))=3 then ”MAR”
when extract(month from cast(”’||:vvalidade||”’ as date))=4 then ”ABR”
when extract(month from cast(”’||:vvalidade||”’ as date))=5 then ”MAI”
when extract(month from cast(”’||:vvalidade||”’ as date))=6 then ”JUN”
when extract(month from cast(”’||:vvalidade||”’ as date))=7 then ”JUL”
when extract(month from cast(”’||:vvalidade||”’ as date))=8 then ”AGO”
when extract(month from cast(”’||:vvalidade||”’ as date))=9 then ”SET”
when extract(month from cast(”’||:vvalidade||”’ as date))=10 then ”OUT”
when extract(month from cast(”’||:vvalidade||”’ as date))=11 then ”NOV”
when extract(month from cast(”’||:vvalidade||”’ as date))=12 then ”DEZ”
end,
extract(month from cast(”’||:vvalidade||”’ as date)),
extract(year from cast(”’||:vvalidade||”’ as date))
from RDB$DATABASE’;
execute statement Sentenca into :mes, :mes_int, :ano;
select first 1 pe.id_produtoloteestoque from produtos_lote_estoque pe
where pe.is_produto=:vis_produto
and pe.is_filial=:vis_filial
and pe.is_setor=:vis_setor
and pe.lote=:vlote
and pe.validade=:vvalidade
into: id;
if (vop=’T') then
begin
select first 1 pe.id_produtoloteestoque from produtos_lote_estoque pe
where pe.is_produto=:vis_produto
and pe.is_filial=:vis_filial
and pe.is_setor=:vis_setordestino
and pe.lote=:vlote
and pe.validade=:vvalidade
into: id2;
end
/* Se não encontrou produtos_Estoque, incluir um registro */
if (id<=0) then
begin
nomeCampo=’GEN_ID_PRODUTOLOTEESTOQUE’;
Sentenca = ’select cast(gen_id(’ || :nomeCampo || ‘, 1) as integer) from RDB$DATABASE’;
execute statement Sentenca into :ID;
insert into PRODUTOS_LOTE_ESTOQUE
(ID_PRODUTOLOTEESTOQUE,IS_FILIAL,IS_SETOR,IS_PRODUTO,LOTE,VALIDADE,ENTRADA,SAIDA,SALDO,MES,ANO, MES_INT)
VALUES (:ID, :vis_filial, :vis_setor, :vis_produto, :vlote, :vvalidade, 0, 0, 0,:mes,:ano,:mes_int);
end
if ((id2<=0) and (:vop=’T')) then
begin
nomeCampo=’GEN_ID_PRODUTOLOTEESTOQUE’;
Sentenca = ’select cast(gen_id(’ || :nomeCampo || ‘, 1) as integer) from RDB$DATABASE’;
execute statement Sentenca into :ID2;
insert into PRODUTOS_LOTE_ESTOQUE
(ID_PRODUTOLOTEESTOQUE,IS_FILIAL,IS_SETOR,IS_PRODUTO,LOTE,VALIDADE,ENTRADA,SAIDA,SALDO,MES,ANO, MES_INT)
VALUES (:ID2, :vis_filial, :vis_setordestino, :vis_produto, :vlote, :vvalidade, 0, 0, 0,:mes,:ano,:mes_int);
end
if (:vop=’XX’) then
begin
update produtos_lote_estoque pe
set
pe.entrada=:vqtde,
pe.saldo=:vqtde
where pe.id_produtoloteestoque=:id;
r=1;
end
if ((:vop=’E') or (:vop=’DV’)) then
begin
update produtos_lote_estoque pe
set
pe.entrada=pe.entrada+:vqtde,
pe.saldo=pe.saldo+:vqtde
where pe.id_produtoloteestoque=:id;
r=1;
end
if ((:vop=’S') or (:vop=’DC’) or (:vop=’SA’) or (:vop=’SI’) or (:vop=’SG’)) then
begin
update produtos_lote_estoque pe
set
pe.saida=pe.saida+:vqtde,
pe.saldo=pe.saldo-:vqtde
where pe.id_produtoloteestoque=:id;
r=1;
end
if (:vop=’T') then
begin
update produtos_lote_estoque pe
set
pe.saida=pe.saida+:vqtde,
pe.saldo=pe.saldo-:vqtde
where pe.id_produtoloteestoque=:id;
update produtos_lote_estoque pe
set
pe.entrada=pe.entrada+:vqtde,
pe.saldo=pe.saldo+:vqtde
where pe.id_produtoloteestoque=:id2;
r=1;
end
nomeCampo=’GEN_ID_ITEMLOTEMOVIMENTACAO’;
Sentenca = ’select cast(gen_id(’ || :nomeCampo || ‘, 1) as integer) from RDB$DATABASE’;
execute statement Sentenca into :ID3;
Sentenca =’select current_date from RDB$DATABASE’;
execute statement Sentenca into :data;
Sentenca =’select current_time from RDB$DATABASE’;
execute statement Sentenca into :hora;
insert into ITENS_LOTE_MOVIMENTACOES
(ID_ITEMLOTEMOVIMENTACAO, IS_PRODUTOLOTEESTOQUE, IS_PRODUTO, QTDE, VALIDADE,
LOTE, IS_FILIAL, IS_SETOR, OP, IS_ORCAMENTO, DATA_LOTE, HORARIO_LOTE, IS_USUARIO, MES, ANO, MES_INT)
VALUES (:id3, :id, :vis_produto, :vqtde,:vvalidade,:vlote,:vis_filial,:vis_setor,:vop,
:vis_orcamento,:data,:hora,:vis_usuario,:mes, :ano, :mes_int);
suspend;
end
Autor: Farnetani