· 6 years ago · Apr 11, 2019, 07:34 PM
1create or replace package body pk_remanejamento is
2
3 C_MOV_COBERTURA constant number := 0;
4 C_MOV_CONTROLE_AVARIA constant number := 1;
5 C_MOV_DIVIDIR_LOTE constant number := 2;
6 C_MOV_DESMONTAGEM_KIT constant number := 3;
7 C_MOV_PROD_RECUPERADO constant number := 4;
8 C_MOV_CADASTRO_LOTE constant number := 5;
9
10 type cursor_remanejamento is ref cursor;
11
12 type t_localRemanejamento is record(
13 idLocal local.idlocal%type,
14 tipoLocal local.tipo%type,
15 idarmazem local.idarmazem%type,
16 buffer local.buffer%type,
17 picking local.picking%type,
18 codIntegracaoEsteira setor.codintegracaoesteira%type,
19 bufferEsteira local.bufferesteira%type);
20
21 procedure getRemAberto
22 (
23 c_remAberto in out cursor_remanejamento,
24 r_remAberto in out remanejamento%rowtype,
25 p_idarmazem in number,
26 p_idlocalorigem in local.idlocal%type,
27 p_idlocaldestino in local.idlocal%type
28 ) is
29 begin
30 open c_remAberto for
31 select r.*
32 from remanejamento r
33 where ((r.planejado = 'N') or (r.cadmanual = 'N'))
34 and r.status = 'A'
35 and r.idarmazemdestino = p_idarmazem
36 and r.idlocaldestino = p_idlocaldestino
37 and r.idarmazemorigem = p_idarmazem
38 and r.idlocalorigem = p_idlocalorigem
39 and not exists (select 1
40 from loteremanejamento lr
41 where lr.idremanejamento = r.idremanejamento
42 and ((nvl(lr.conferido, 'N') = 'S') or
43 (nvl(lr.impresso, 'N') = 'S')))
44 and not exists
45 (select 1
46 from remanejamento re, local lo, local ld, setor sd
47 where re.idremanejamento = r.idremanejamento
48 and re.idlocalorigem = lo.idlocal
49 and lo.picking = 'S'
50 and lo.buffer = 'S'
51 and lo.bufferesteira = 1
52 and re.idlocaldestino = ld.idlocal
53 and ld.picking = 'S'
54 and ld.buffer = 'N'
55 and ld.idsetor = sd.idsetor
56 and sd.codintegracaoesteira is not null)
57 order by r.idremanejamento
58 for update;
59
60 fetch c_remAberto
61 into r_remAberto;
62 end;
63
64 /*
65 * Funcao que cadastra o remanejamento
66 */
67 function cadastrar_remanejamento
68 (
69 p_idarmazemorigem in number,
70 p_idarmazemdestino in number,
71 p_idlocalorigem in local.idlocal%type,
72 p_idlocaldestino in local.idlocal%type,
73 p_idusuario in number,
74 p_idromaneio in number,
75 p_descr in remanejamento.descrpalet%type,
76 p_commit in varchar2 := C_SIM,
77 p_planejado in varchar2 := C_NAO
78 ) return number is
79 c_remAberto cursor_remanejamento;
80 r_remaAberto remanejamento%rowtype;
81 v_idremanejamento number;
82 v_descr remanejamento.descrpalet%type;
83 v_qtdelote number;
84 v_msg t_message;
85 begin
86 -- conferir se o lote esta em OS nao finalizada
87 begin
88 select nvl(count(*), 0)
89 into v_qtdelote
90 from v_estoque_local ll, ordemlote ol, ordemservico os
91 where ll.idlocal = p_idlocalorigem
92 and ol.idlote = ll.idlote
93 and os.idordemservico = ol.idordemservico
94 and os.tiposervico = c_conserto
95 and os.situacao <> 'P';
96 exception
97 when others then
98 v_qtdelote := 0;
99 end;
100
101 if v_qtdelote > 0 then
102 v_msg := t_message('EXISTE ORDEM DE SERVICO ABERTA PARA ESTES LOTES.');
103 raise_application_error(-20000, v_msg.formatMessage);
104 end if;
105
106 v_descr := nvl(trim(p_descr), 'N/D');
107
108 getRemAberto(c_remAberto, r_remaAberto, p_idarmazemorigem,
109 p_idlocalorigem, p_idlocaldestino);
110 if (c_remAberto%found) then
111 v_idremanejamento := r_remaAberto.idremanejamento;
112 else
113 select seq_remanejamento.nextval idremanejamento
114 into v_idremanejamento
115 from dual;
116
117 insert into remanejamento
118 (idremanejamento, idarmazemorigem, idlocalorigem, idarmazemdestino,
119 idlocaldestino, datahora, idusuariotela, status, cadmanual,
120 idromaneio, descrpalet, planejado)
121 values
122 (v_idremanejamento, p_idarmazemorigem, p_idlocalorigem,
123 p_idarmazemdestino, p_idlocaldestino, sysdate, p_idusuario,
124 c_aguardando, c_nao, p_idromaneio, v_descr, p_planejado);
125 end if;
126
127 close c_remAberto;
128
129 if p_commit = c_sim then
130 commit;
131 end if;
132
133 return v_idremanejamento;
134 end;
135
136 /*
137 * Responsavel por trocar os lotes no remanejamento.
138 */
139 procedure alterar_remanejamento
140 (
141 p_idremanejamento in number,
142 p_lote in number,
143 p_qtde in number,
144 p_usuario in number
145 ) is
146 -- verifica se o lote pego ja esta no remanejamento
147 cursor c_lote
148 (
149 p_remanejamento in number,
150 p_conferido in varchar2,
151 p_lote in varchar2
152 ) is
153 select l.idlote, lt.idproduto, lt.barra, lt.iddepositante, l.qtde
154 from loteremanejamento l, lote lt
155 where l.idremanejamento = p_remanejamento
156 and l.conferido like p_conferido
157 and l.idlote like p_lote
158 and lt.idlote = l.idlote;
159
160 cursor c_llocal
161 (
162 p_arm in number,
163 p_lo in varchar2,
164 p_lt in number,
165 p_idpro in number,
166 p_bar in varchar2,
167 p_dep in number
168 ) is
169 select l.idlote, l.disp estoque
170 from v_estoque_local l, lote lt
171 where l.idlote = p_lt
172 and l.idarmazem = p_arm
173 and l.idlocal = p_lo
174 and lt.idproduto = p_idpro
175 and lt.barra = p_bar
176 and lt.iddepositante = p_dep
177 and l.loteliberado = c_sim;
178
179 r_lote c_lote%rowtype;
180 r_llocal c_llocal%rowtype;
181 v_idarm_ori local.idarmazem%type;
182 v_idarm_des local.idarmazem%type;
183 v_local_ori local.idlocal%type;
184 v_local_des local.idlocal%type;
185 -- v_roma_proc number;
186 v_achou boolean;
187 v_qtde number;
188 v_pend number;
189 v_dif number;
190 v_msg t_message;
191 begin
192 -- localizo o lote que vai ser substituido.
193 if c_lote%isopen then
194 close c_lote;
195 end if;
196 open c_lote(p_idremanejamento, '%', p_lote);
197 fetch c_lote
198 into r_lote;
199 if c_lote%notfound then
200 close c_lote;
201 open c_lote(p_idremanejamento, c_nao, '%');
202 fetch c_lote
203 into r_lote;
204 v_achou := false;
205 else
206 v_achou := true;
207 end if;
208
209 if v_achou = false then
210 -- localizo o remanejamento em andamento
211 select r.idarmazemorigem, r.idlocalorigem, r.idarmazemdestino,
212 r.idlocaldestino
213 into v_idarm_ori, v_local_ori, v_idarm_des, v_local_des
214 from remanejamento r
215 where r.idremanejamento = p_idremanejamento;
216 -- verifica se o lote selecionado e valido
217 if c_llocal%isopen then
218 close c_llocal;
219 end if;
220 open c_llocal(v_idarm_ori, v_local_ori, p_lote, r_lote.idproduto,
221 r_lote.barra, r_lote.iddepositante);
222 fetch c_llocal
223 into r_llocal;
224 if ((c_llocal%found) and (nvl(r_llocal.estoque, 0) > 0)) then
225 /* atualiza a pendencia do lote antigo, retirando a qtde que
226 anteriormente foi colocada como pendencia... */
227 pk_estoque.incluir_pendencia(v_idarm_ori, v_local_ori,
228 r_lote.idlote, r_lote.qtde, p_usuario,
229 'LOTE SUBSTITUIDO PELO LOTE N:' ||
230 P_LOTE || ' DO REMANEJAMENTO N:' ||
231 P_IDREMANEJAMENTO ||
232 ' ATUALIZA A PENDENCIA DO LOTE ANTIGO');
233 /* - atualiza a pendencia e o adicionar do endereco de destino ref.
234 ao lote antigo.
235 - seleciona a pendencia do lote antigo.*/
236 select pendencia
237 into v_qtde
238 from v_estoque_local
239 where idarmazem = v_idarm_des
240 and idlocal = v_local_des
241 and idlote = r_lote.idlote;
242
243 pk_estoque.retirar_pendencia(v_idarm_des, v_local_des,
244 r_lote.idlote, v_qtde, p_usuario,
245 'LOTE SUBSTITUIDO PELO LOTE N:' ||
246 P_LOTE || ' DO REMANEJAMENTO N:' ||
247 P_IDREMANEJAMENTO ||
248 '. ATUALIZA LOTE ANTIGO.');
249
250 pk_estoque.retirar_adicionar(v_idarm_des, v_local_des,
251 r_lote.idlote, r_lote.qtde, p_usuario,
252 'LOTE SUBSTITUIDO PELO LOTE N:' ||
253 P_LOTE || ' DO REMANEJAMENTO N:' ||
254 P_IDREMANEJAMENTO ||
255 '. ATUALIZA LOTE ANTIGO.');
256
257 /* - atualiza a pendencia e o adicionar do novo lote para o endereco de origem,*/
258 pk_estoque.incluir_pendencia(v_idarm_ori, v_local_ori, p_lote,
259 p_qtde, p_usuario,
260 'REMANEJAMENTO N. ' ||
261 P_IDREMANEJAMENTO ||
262 ' ATUALIZA A PENDENCIA E O ADICIONAR DO NOVO LOTE PARA O ENDERECO DE ORIGEM');
263 /* - verifica se a pendencia do lote anterior e maior que a do novo lote
264 - caso for, insere a qtde do lote novo */
265 if (v_qtde > p_qtde) then
266 -- grava como pendencia a qtde do lote novo.
267 -- grava a diferenca para que seja adicionada no proximo lote.
268 v_pend := p_qtde;
269 v_dif := v_qtde - p_qtde;
270 else
271 -- verifica se existe alguma pendencia de diferenca nos lotes anteriores.
272 select nvl(sum(diferenca), 0)
273 into v_dif
274 from loteremanejamento
275 where idremanejamento = p_idremanejamento
276 and diferenca > 0;
277 if (v_dif + v_qtde) < p_qtde then
278 v_pend := v_qtde + v_dif;
279 v_dif := 0;
280 else
281 v_pend := v_qtde + (p_qtde - v_qtde);
282 v_dif := (v_dif + v_qtde) - v_pend;
283 end if;
284 end if;
285 /* - atualiza a pendencia e o adicionar do novo lote para o endereco de destino,
286 verifica se ja existe o lote no local, se nao existir ele cadastra. */
287
288 pk_estoque.incluir_pendencia(v_idarm_des, v_local_des, p_lote,
289 v_pend, p_usuario,
290 'REMANEJAMENTO N. ' ||
291 P_IDREMANEJAMENTO ||
292 ' ATUALIZA A PENDENCIA E O ADICIONAR DO NOVO LOTE PARA O ENDERECO DE DESTINO');
293
294 pk_estoque.incluir_adicionar(v_idarm_des, v_local_des, p_lote,
295 p_qtde, p_usuario,
296 'REMANEJAMENTO N. ' ||
297 P_IDREMANEJAMENTO ||
298 ' ATUALIZA A PENDENCIA E O ADICIONAR DO NOVO LOTE PARA O ENDERECO DE DESTINO');
299
300 -- atualiza a tabela de lotes no remanejamento, trocando o lote antigo pelo lote novo.
301 update loteremanejamento
302 set idlote = p_lote,
303 qtde = p_qtde,
304 conferido = c_sim,
305 controlaqtde = c_nao,
306 diferenca = v_dif
307 where idremanejamento = p_idremanejamento
308 and idlote = r_lote.idlote;
309 -- atualizando paletseparacao
310 update paletseparacao
311 set idlote = p_lote
312 where idarmazem = v_idarm_des
313 and idlocal = v_local_des
314 and idlote = r_lote.idlote
315 and idromaneio in (select idromaneio
316 from romaneiopai
317 where processado = c_nao);
318 else
319 v_msg := t_message('LOTE NAO ENCONTRADO NO ENDERECO DE ORIGEM, OU QUANTIDADE VINCULADA A OUTRO REMANEJAMENTO.');
320 raise_application_error(-20000, v_msg.formatMessage);
321 end if;
322 else
323 -- lote encontra-se na tab. de lote remanejamento.
324 update loteremanejamento
325 set conferido = c_sim
326 where idremanejamento = p_idremanejamento
327 and idlote = p_lote;
328 end if;
329 commit;
330 exception
331 when no_data_found then
332 v_msg := t_message('LOTE NAO ENCONTRADO PARA REMANEJAMENTO ESPECIFICADO.');
333 raise_application_error(-20000, v_msg.formatMessage);
334 end;
335
336 /*
337 * Gera os lotes para serem remanejados do picking para o pulmao
338 */
339 procedure Gerar_Lotes
340 (
341 p_remanejamento in number,
342 p_idarmazem in number,
343 p_idlocal in local.idlocal%type,
344 p_lotes in number,
345 p_fator in number,
346 p_iduser in number,
347 p_idproduto in produto.idproduto%type
348 ) is
349 cursor c_llocal is
350 select v.idarmazem, v.idlocal, v.tipo, v.buffer, v.iddepositante,
351 v.idlote, v.idproduto, v.estoque, v.pendencia, v.reservado,
352 v.adicionar, v.disp, l.barra, l.estado, l.tipopalet,
353 l.qtdedisponivel, l.descr descrlote, l.dtvenc, l.dtfabricacao,
354 l.loteadicional
355 from v_estoque_local v, lote l
356 where v.idarmazem = p_idarmazem
357 and v.idlocal = p_idlocal
358 and v.idproduto = p_idproduto
359 and l.idlote = v.idlote
360 and (v.estoque + v.adicionar) - (v.pendencia + v.reservado) > 0
361 and v.loteliberado = c_sim
362 order by l.qtdedisponivel desc, v.dtalocacao;
363
364 C_REMANJ_PK_PL constant number := 3;
365
366 r c_llocal%rowtype;
367 v_idlote number;
368 v_i number;
369 v_qtdecoberta number;
370 v_disp number;
371 v_lote lote%rowtype;
372 v_qtde number;
373 v_restante number;
374 v_msg t_message;
375 begin
376 select sum(v.disp)
377 into v_disp
378 from v_estoque_local v
379 where v.idarmazem = p_idarmazem
380 and v.idlocal = p_idlocal
381 and v.loteliberado = c_sim;
382
383 if v_disp < p_lotes * p_fator then
384 v_msg := t_message('Quantidade Nao Disponivel no endereco de picking.' ||
385 CHR(13) || 'Geracao de Lotes Cancelada.');
386 raise_application_error(-20358, v_msg.formatMessage);
387 else
388 if c_llocal%isopen then
389 close c_llocal;
390 end if;
391 open c_llocal;
392 fetch c_llocal
393 into r;
394 if c_llocal%notfound then
395 v_msg := t_message('Não existem lotes cadastrados para esse remanejamento.');
396 raise_application_error(-20000, v_msg.formatMessage);
397 end if;
398
399 for v_i in 1 .. p_lotes
400 loop
401 select seq_lote.nextval
402 into v_idlote
403 from dual;
404
405 -- preenchendo os dados para o lote
406 v_lote.idproduto := r.idproduto;
407 v_lote.barra := pk_produto.retornar_codbarra(r.idproduto, 1);
408 v_lote.liberado := c_sim;
409 v_lote.qtdeentrada := p_fator;
410 v_lote.qtdemapaaloc := p_fator;
411 v_lote.idusuario := p_iduser;
412 v_lote.iddepositante := r.iddepositante;
413 v_lote.descr := r.descrlote;
414 v_lote.dtvenc := r.dtvenc;
415 v_lote.dtalocacao := sysdate;
416 v_lote.dtentrada := sysdate;
417 v_lote.situacao := c_liberado;
418 v_lote.tipopalet := r.tipopalet;
419 v_lote.estado := r.estado;
420 v_lote.idarmazem := p_idarmazem;
421 v_lote.dtfabricacao := r.dtfabricacao;
422 v_lote.loteadicional := r.loteadicional;
423
424 if (pk_lote.isLoteIndustriaComMaisDeUmVenc(r.iddepositante,
425 r.idproduto, r.descrlote,
426 r.dtvenc)) then
427 raise_application_error(-20000,
428 'Não é possÃvel cadastrar lote do mesmo produto/depositante com o mesmo lote indústria e data de vencimento diferentes. Depositante:' ||
429 r.iddepositante || '. Produto: ' ||
430 r.idproduto || ' Lote Indústria: ' ||
431 r.descrlote || ' Data Venc: ' || r.dtvenc);
432
433 end if;
434
435 v_idlote := pk_lote.cadastrar_lote(v_lote, null, 'N');
436
437 pk_picking_dinamico.addGttPickingDinamico(v_lote.idproduto,
438 v_lote.iddepositante,
439 p_idlocal,
440 v_lote.idarmazem, 1);
441
442 pk_estoque.incluir_estoque(p_idarmazem, p_idlocal, v_idlote,
443 p_fator, p_iduser,
444 'LOTE GERADO INTERNAMENTE', c_nao);
445 v_restante := p_fator;
446
447 insert into loteremanejamento
448 (idremanejamento, idlote, qtde, conferido, controlaqtde,
449 diferenca)
450 values
451 (p_remanejamento, v_idlote, p_fator, c_nao, c_sim, 0);
452
453 while (v_restante > 0)
454 and (c_llocal%found)
455 loop
456 -- calcula a quantidade do lote que sera criado
457 if r.disp > v_restante then
458 v_qtde := v_restante;
459 else
460 v_qtde := r.disp;
461 end if;
462
463 -- calcula a quantidade disponivel coberta
464 if (v_restante > r.qtdedisponivel) then
465 v_qtdecoberta := r.qtdedisponivel;
466 else
467 v_qtdecoberta := v_restante;
468 end if;
469
470 if v_qtdecoberta > v_qtde then
471 v_qtdecoberta := v_qtde;
472 end if;
473
474 -- insere o composicao do lote para rastreabilidade de cobertura
475 pk_lote.inserirComposicaoLote(v_idlote, r.idlote, v_qtde,
476 v_qtdecoberta, C_REMANJ_PK_PL,
477 p_remanejamento);
478
479 pk_lote.inserirComposicaoLoteKit(r.idlote, v_idlote, v_qtde);
480
481 pk_lote.inserirBarraOriginal(v_idlote);
482
483 -- adiciona quantidade disponivel para o lote formado no inventario
484 update lote
485 set qtdedisponivel = nvl(qtdedisponivel, 0) + v_qtdecoberta
486 where idlote = v_idlote;
487
488 -- retira quantidade disponivel do lote baixado no inventario
489 update lote
490 set qtdedisponivel = nvl(qtdedisponivel, 0) - v_qtdecoberta
491 where idlote = r.idlote;
492
493 pk_estoque.retirar_estoque(r.idarmazem, r.idlocal, r.idlote,
494 v_qtde, p_iduser,
495 'QUANTIDADE DISPONIVEL NO PICKING ZERADA.(rotina gerar lotes)',
496 'N');
497
498 if ((r.tipo = 0) AND (r.buffer = 'N')) then
499 pk_picking_dinamico.addGttPickingDinamico(r.idproduto,
500 r.iddepositante,
501 r.idlocal, r.idarmazem,
502 0);
503 end if;
504
505 r.qtdedisponivel := r.qtdedisponivel - v_qtdecoberta;
506 r.disp := r.disp - v_qtde;
507 v_restante := v_restante - v_qtde;
508
509 if r.disp <= 0 then
510 fetch c_llocal
511 into r;
512 end if;
513 end loop;
514
515 pk_picking_dinamico.deletar_picking_dinamico;
516
517 pk_lote.valorarLote(v_idlote);
518
519 pk_lote.atualizaInfLote(v_idlote, C_MOV_CADASTRO_LOTE);
520
521 if v_restante > 0 then
522 v_msg := t_message('NAO FOI ENCONTRADO LOTE SUFICIENTE PARA FORMACAO. REMANEJAMENTO PK->PL.');
523 raise_application_error(-20000, v_msg.formatMessage);
524 end if;
525 end loop;
526 pk_picking_dinamico.inserir_picking_dinamico;
527 end if;
528 end;
529
530 procedure recalcula_paletseparacao
531 (
532 p_lote in lote.idlote%type,
533 p_remanejamento in remanejamento.idremanejamento%type,
534 p_usuario in usuario.idusuario%type
535 ) is
536 cursor c_local
537 (
538 p_idarmazem local.idarmazem%type,
539 p_idlocal in local.idlocal%type,
540 p_lote in lote.idlote%type,
541 p_idproduto in number
542 ) is
543 select idlote, idarmazem, idlocal, l.disp
544 from v_estoque_local l
545 where l.idarmazem = p_idarmazem
546 and l.idlocal = p_idlocal
547 and l.idlote <> p_lote
548 and l.loteliberado = c_sim
549 and l.idproduto = p_idproduto
550 and l.idlote not in (select idlote
551 from conferenciaentradadet c
552 where c.idlote is not null)
553 order by disp desc;
554
555 r_local c_local%rowtype;
556 v_qtde number;
557 v_idlocal local.idlocal%type;
558 v_idarmazem local.idarmazem%type;
559 v_id number;
560 begin
561 -- SELECIONA OS ROMANEIOS QUE ESTAVAO NECESSITANDO DA QTDE NAO REMANEJADA
562 for c_paletsep in (select rp.idromaneio, ps.idarmazem, ps.idlocal,
563 ps.idlote, ps.qtdeunit qtde, ps.ordem,
564 l.idproduto, ps.idpalet, l.barra, ps.separado
565 from romaneiopai rp, palet pa, paletseparacao ps,
566 lote l, remanejamento r
567 where l.idlote = ps.idlote
568 and rp.erro = c_nao
569 and rp.gerado = c_sim
570 and rp.processado = c_nao
571 and ps.idlote = p_lote
572 and pa.idromaneio = rp.idromaneio
573 and ps.idromaneio = pa.idromaneio
574 and ps.idpalet = pa.idpalet
575 and ps.idlocal = r.idlocaldestino
576 and ps.idarmazem = r.idarmazemdestino
577 and r.idremanejamento = p_remanejamento
578 and rp.tipo = 0
579 order by ps.idarmazem, ps.idlocal, ps.idlote)
580 loop
581 -- busca os locais em remanejamento que podem suprir a qtde.
582 if c_local%isopen then
583 close c_local;
584 end if;
585 open c_local(c_paletsep.idarmazem, c_paletsep.idlocal, p_lote,
586 c_paletsep.idproduto);
587 fetch c_local
588 into r_local;
589 v_qtde := c_paletsep.qtde;
590 while ((c_local%found) and (v_qtde > 0))
591 loop
592 -- verifica se o lote atende a qtde do romaneio
593 if v_qtde <= r_local.disp then
594 -- o lote selecionado supri a qtde a ser trocada.
595 update paletseparacao
596 set idlote = r_local.idlote
597 where idarmazem = c_paletsep.idarmazem
598 and idlocal = c_paletsep.idlocal
599 and idlote = c_paletsep.idlote;
600
601 pk_estoque.retirar_pendencia(c_paletsep.idarmazem,
602 c_paletsep.idlocal, c_paletsep.idlote,
603 v_qtde, p_usuario,
604 'SUBSTITUICAO DE LOTE REMANEJAMENTO N:' ||
605 P_REMANEJAMENTO ||
606 '. SUBTRAIDA A PENDENCIA DO LOTE E TRANSFERIDA A PENDENCIA DO LOTE :' ||
607 P_LOTE);
608
609 pk_estoque.incluir_pendencia(r_local.idarmazem, r_local.idlocal,
610 r_local.idlote, v_qtde, p_usuario,
611 'SUBSTITUICAO DE LOTE REMANEJAMENTO N:' ||
612 P_REMANEJAMENTO ||
613 '. ADICIONADA A PENDENCIA PROVENIENTE DO LOTE :' ||
614 P_LOTE);
615 v_qtde := 0;
616 else
617 --se a quantidade nao for suficiente
618 insert into paletseparacao
619 (idromaneio, idpalet, idarmazem, idlocal, idlote, idusuario,
620 ordem, qtde, separado, qtdeunit)
621 values
622 (c_paletsep.idromaneio, c_paletsep.idpalet,
623 c_paletsep.idarmazem, c_paletsep.idlocal, r_local.idlote,
624 p_usuario, c_paletsep.ordem, r_local.disp, c_paletsep.separado,
625 r_local.disp);
626
627 pk_estoque.incluir_pendencia(r_local.idarmazem, r_local.idlocal,
628 r_local.idlote, r_local.disp,
629 p_usuario,
630 'SUBSTITUICAO DE LOTE REMANEJAMENTO N:' ||
631 P_REMANEJAMENTO ||
632 '. ADICIONADA A PENDENCIA PROVENIENTE DO LOTE :' ||
633 P_LOTE);
634
635 update paletseparacao
636 set qtde = qtde - r_local.disp,
637 qtdeunit = qtdeunit - r_local.disp
638 where idarmazem = c_paletsep.idarmazem
639 and idlocal = c_paletsep.idlocal
640 and idlote = c_paletsep.idlote;
641
642 pk_estoque.retirar_pendencia(c_paletsep.idarmazem,
643 c_paletsep.idlocal, c_paletsep.idlote,
644 r_local.disp, p_usuario,
645 'SUBSTITUICAO DE LOTE REMANEJAMENTO N:' ||
646 P_REMANEJAMENTO ||
647 '. SUBTRAIDA A PENDENCIA DO LOTE E TRANSFERIDA A PENDENCIA DO LOTE :' ||
648 P_LOTE);
649
650 v_qtde := v_qtde - r_local.disp;
651 end if;
652 fetch c_local
653 into r_local;
654 end loop;
655 if v_qtde > 0 then
656 select r.idlocalorigem, r.idarmazemorigem
657 into v_idlocal, v_idarmazem
658 from remanejamento r
659 where r.idremanejamento = p_remanejamento;
660
661 v_id := cadastrar_remanejamento(v_idarmazem, v_idarmazem, v_idlocal,
662 c_paletsep.idlocal, p_usuario,
663 c_paletsep.idromaneio,
664 'REABASTECIMENTO PARA ROMANEIO: ' ||
665 c_paletsep.idromaneio);
666 insert into loteremanejamento
667 (idremanejamento, idlote, qtde, conferido, idromaneio)
668 values
669 (v_id, c_paletsep.idlote, v_qtde, c_nao, c_paletsep.idromaneio);
670
671 end if;
672 end loop;
673 end;
674
675 procedure reabastecerPorLastro
676 (
677 p_idarmazem in number,
678 p_iddepositante in number,
679 p_idproduto in number,
680 p_idlocal in local.idlocal%type,
681 p_idusuario in number
682 ) is
683
684 c_estlocal cursor_estlocal;
685 r_estlocal rec_estlocal;
686 v_id number;
687 v_qtdeRemanejamento number;
688 v_qtdeConsumirLastro number;
689 v_gerouRemanejamento number;
690 r_loteunico pk_lote.t_loteunico;
691 v_enderecoCompativel boolean;
692 v_idLocalPickNovo local.idlocal%type;
693 v_msgErro varchar2(1000);
694 v_enderPickUtilizar local.idlocal%type;
695 v_msg t_message;
696 v_msgDescr t_message;
697 v_planejado char(1) := 'S';
698 v_reabNaoPlanejado number;
699
700 procedure openCursorEstLocal
701 (
702 p_cursor in out cursor_estlocal,
703 p_estlocal in out rec_estlocal,
704 p_depositante in number,
705 p_produto in number,
706 p_estado in lote.estado%type,
707 p_armazem in number,
708 p_tipopermitirpickingsetor in setor.tipopermitirpickingsetor%type
709 ) is
710 v_sql varchar2(5000);
711 begin
712 if (p_cursor%isopen) then
713 close p_cursor;
714 end if;
715
716 if (p_tipopermitirpickingsetor = 0) then
717
718 v_sql := 'select lt.idlote, lo.idlocal,
719 (ll.estoque - ll.pendencia + ll.adicionar) disp,
720 lt.descr loteindustria, lt.dtvenc
721 from lotelocal ll, local lo, lote lt, produtodepositante pd
722 where (ll.estoque - ll.pendencia + ll.adicionar) > 0
723 and lo.id = ll.idendereco
724 and lo.tipo in (1, 2)
725 and lo.ativo = ''S''
726 and lo.idarmazem = ' || p_armazem || '
727 and pd.idproduto = lt.idproduto
728 and pd.identidade = lt.iddepositante
729 and pd.tipopicking = ' ||
730 p_tipopermitirpickingsetor || '
731 and not exists (select 1
732 from setorrecebimento sr, tiporecebimento tr
733 where sr.idsetor = lo.idsetor
734 and tr.idtiporecebimento = sr.idtiporecebimento
735 and tr.classificacao = ''R'')
736 and lt.idlote = ll.idlote
737 and lt.liberado = ''S''
738 and lt.tipolote = ''L''
739 and lt.estado = ''' || p_estado || '''
740 and lt.idproduto = ' || p_produto || '
741 and lt.iddepositante = ' ||
742 p_depositante || '
743 order by nvl(lt.dtvenc, sysdate), lo.idlocal ';
744
745 elsif (p_tipopermitirpickingsetor = 1) then
746
747 v_sql := 'select lt.idlote, lo.idlocal,
748 (ll.estoque - ll.pendencia + ll.adicionar) disp,
749 lt.descr loteindustria, lt.dtvenc
750 from lotelocal ll, local lo, lote lt, produtodepositante pd, embalagem e
751 where (ll.estoque - ll.pendencia + ll.adicionar) > 0
752 and lo.id = ll.idendereco
753 and lo.tipo in (1, 2)
754 and lo.ativo = ''S''
755 and lo.idarmazem = ' || p_armazem || '
756 and pd.idproduto = lt.idproduto
757 and pd.identidade = lt.iddepositante
758 and pd.tipopicking = ' ||
759 p_tipopermitirpickingsetor || '
760 and lt.fatorconversao = 1
761 and e.idproduto = lt.idproduto
762 and e.barra = lt.barra
763 and e.caixafechada = ''N''
764 and not exists (select 1
765 from setorrecebimento sr, tiporecebimento tr
766 where sr.idsetor = lo.idsetor
767 and tr.idtiporecebimento = sr.idtiporecebimento
768 and tr.classificacao = ''R'')
769 and lt.idlote = ll.idlote
770 and lt.liberado = ''S''
771 and lt.tipolote = ''L''
772 and lt.estado = ''' || p_estado || '''
773 and lt.idproduto = ' || p_produto || '
774 and lt.iddepositante = ' ||
775 p_depositante || '
776 order by nvl(lt.dtvenc, sysdate), lo.idlocal ';
777
778 end if;
779
780 open p_cursor for v_sql;
781
782 fetch p_cursor
783 into p_estlocal;
784 end;
785
786 begin
787
788 -- selecionando os enderecos que necessitam de reabastecimento
789 for r_end in (select v.idarmazem, v.idlocal, v.estanteria, v.idproduto,
790 v.iddepositante, v.estadolote,
791 v.pontoreabastecimento,
792 nvl(v.limiteressuprimento, 0) pontoressuprimento,
793 v.fatorconversao, v.disponivelpk disponivel,
794 v.fracionado, pd.loteuniconoendereco,
795 (e.lastro * e.fatorconversao) qtdelastroun,
796 v.tipopermitirpickingsetor
797 from v_reabastecimento_planejado v, produtodepositante pd,
798 embalagem e
799 where v.idproduto = p_idproduto
800 and v.iddepositante = p_iddepositante
801 and v.idlocal = p_idlocal
802 and v.idarmazem = p_idarmazem
803 and pd.idproduto = p_idproduto
804 and pd.identidade = p_iddepositante
805 and e.idproduto = v.idproduto
806 and e.barra =
807 pk_produto.RetornarCodBarraMaiorFator(v.idproduto))
808
809 loop
810
811 v_enderPickUtilizar := r_end.idlocal;
812
813 delete from gtt_selecao;
814
815 v_qtdeRemanejamento := 0;
816
817 v_qtdeConsumirLastro := r_end.qtdelastroun;
818
819 openCursorEstLocal(c_estlocal, r_estlocal, r_end.iddepositante,
820 r_end.idproduto, r_end.estadolote, r_end.idarmazem,
821 r_end.tipopermitirpickingsetor);
822
823 while (c_estlocal%found)
824 loop
825 /* if (r_end.loteuniconoendereco = 1) then
826
827 select count(1)
828 into v_estoqueIncompativel
829 from dual
830 where exists
831 (select 1
832 from lotelocal ll, lote lt
833 where ll.idarmazem = r_end.idarmazem
834 and ll.idlocal = r_end.idlocal
835 and ll.estoque + ll.adicionar > 0
836 and lt.idlote = ll.idlote
837 and lt.idproduto = r_end.idproduto
838 and lt.iddepositante = r_end.iddepositante
839 and (decode(lt.descr, r_estlocal.loteindustria, 1, 0) = 0 or
840 decode(lt.dtvenc, r_estlocal.dtvenc, 1, 0) = 0));
841
842 end if;*/
843
844 r_loteunico.idProduto := r_end.idproduto;
845 r_loteunico.estado := r_end.estadolote;
846 r_loteunico.loteindustria := r_estlocal.loteindustria;
847 r_loteunico.dtvencimento := r_estlocal.dtvenc;
848 r_loteunico.loteuniconoendereco := r_end.loteuniconoendereco;
849 r_loteunico.iddepositante := r_end.iddepositante;
850
851 v_enderecoCompativel := pk_lote.isLocalPodeReceberLoteUnico(r_loteUnico,
852 r_end.idarmazem,
853 r_end.idlocal,
854 v_msgErro);
855
856 if not v_enderecoCompativel then
857 v_idLocalPickNovo := null;
858 for c_endPicking in (select idarmazem, idlocal
859 from (select pl.idarmazem, pl.idlocal,
860 0 ordem
861 from produtolocal pl, local l,
862 setor s, tiposetor ts,
863 lotelocal ll, lote lt
864 where pl.identidade =
865 r_end.iddepositante
866 and pl.idproduto = r_end.idproduto
867 and pl.idarmazem = r_end.idarmazem
868 and l.idlocal = pl.idlocal
869 and l.idarmazem = pl.idarmazem
870 and s.idsetor = l.idsetor
871 and s.tipopermitirpickingsetor =
872 r_end.tipopermitirpickingsetor
873 and ts.idtiposetor = s.idtiposetor
874 and ts.normal = 'S'
875 and l.ativo = 'S'
876 and l.tipo = 0
877 and ll.idendereco = l.id
878 and (ll.estoque + ll.adicionar) > 0
879 and lt.idlote = ll.idlote
880 and lt.idproduto = pl.idproduto
881 and lt.iddepositante =
882 pl.identidade
883 and lt.descr =
884 r_estlocal.loteindustria
885 and lt.dtvenc = r_estlocal.dtvenc
886 union
887 select pl.idarmazem, pl.idlocal,
888 1 ordem
889 from produtolocal pl, local l,
890 setor s, tiposetor ts
891 where pl.identidade =
892 r_end.iddepositante
893 and pl.idproduto = r_end.idproduto
894 and pl.idarmazem = r_end.idarmazem
895 and l.idlocal = pl.idlocal
896 and l.idarmazem = pl.idarmazem
897 and s.idsetor = l.idsetor
898 and s.tipopermitirpickingsetor =
899 r_end.tipopermitirpickingsetor
900 and ts.idtiposetor = s.idtiposetor
901 and ts.normal = 'S'
902 and l.ativo = 'S'
903 and l.tipo = 0
904 and not exists
905 (select 1
906 from lotelocal ll
907 where ll.idendereco = l.id
908 and (ll.estoque +
909 ll.adicionar) > 0)
910 union
911 select pl.idarmazem, pl.idlocal,
912 2 ordem
913 from produtolocal pl, local l,
914 setor s, tiposetor ts,
915 lotelocal ll, lote lt
916 where pl.identidade =
917 r_end.iddepositante
918 and pl.idproduto = r_end.idproduto
919 and pl.idarmazem = r_end.idarmazem
920 and l.idlocal = pl.idlocal
921 and l.idarmazem = pl.idarmazem
922 and s.idsetor = l.idsetor
923 and s.tipopermitirpickingsetor =
924 r_end.tipopermitirpickingsetor
925 and ts.idtiposetor = s.idtiposetor
926 and ts.normal = 'S'
927 and l.ativo = 'S'
928 and l.tipo = 0
929 and ll.idendereco = l.id
930 and (ll.estoque + ll.adicionar) > 0
931 and lt.idlote = ll.idlote
932 and lt.idproduto = pl.idproduto
933 and lt.iddepositante =
934 pl.identidade
935 and (lt.descr <>
936 r_estlocal.loteindustria or
937 lt.dtvenc <> r_estlocal.dtvenc))
938 order by ordem)
939
940 loop
941
942 v_enderecoCompativel := pk_lote.isLocalPodeReceberLoteUnico(r_loteUnico,
943 c_endPicking.Idarmazem,
944 c_endPicking.Idlocal,
945 v_msgErro);
946
947 if (v_enderecoCompativel) then
948 v_idLocalPickNovo := c_endPicking.Idlocal;
949 v_enderPickUtilizar := v_idLocalPickNovo;
950
951 v_msg := t_message('Reabastecimento Planejado');
952 v_msgDescr := t_message('Local: {0}' ||
953 ' não foi reabastecido por falta de estoque compatÃvel. ' ||
954 chr(13) ||
955 'Reabastecimento planejado para o local: ' ||
956 '{1}.');
957 v_msgDescr.addParam(r_end.idlocal);
958 v_msgDescr.addParam(v_enderPickUtilizar);
959 pk_gttresumoexecucao.addResumo(v_msg.formatMessage,
960 v_msgDescr.formatMessage,
961 pk_gttresumoexecucao.TIPO_ALERTA);
962 exit;
963 end if;
964
965 end loop;
966
967 if ((pk_onda.usaPickingDinamico(r_end.idproduto,
968 r_end.iddepositante,
969 r_end.idarmazem) = 1) and
970 (v_idLocalPickNovo is null)) then
971
972 v_idLocalPickNovo := pk_picking_dinamico.inserir_picking_dinamico_auto(r_end.idproduto,
973 r_end.iddepositante,
974 r_end.idarmazem,
975 0,
976 r_loteunico);
977
978 if v_idLocalPickNovo is not null then
979 v_enderPickUtilizar := v_idLocalPickNovo;
980 v_enderecoCompativel := true;
981
982 v_msg := t_message('Reabastecimento Planejado');
983 v_msgDescr := t_message('Local: {0}' ||
984 ' não foi reabastecido por falta de estoque compatÃvel. ' ||
985 chr(13) ||
986 'Reabastecimento planejado para o local: ' ||
987 '{1}.');
988 v_msgDescr.addParam(r_end.idlocal);
989 v_msgDescr.addParam(v_enderPickUtilizar);
990 pk_gttresumoexecucao.addResumo(v_msg.formatMessage,
991 v_msgDescr.formatMessage,
992 pk_gttresumoexecucao.TIPO_ALERTA);
993 end if;
994
995 end if;
996 end if;
997
998 if (v_enderecoCompativel) then
999 v_qtdeRemanejamento := v_qtdeConsumirLastro;
1000
1001 if v_qtdeRemanejamento > r_estlocal.disp then
1002 v_qtdeRemanejamento := r_estlocal.disp;
1003 end if;
1004
1005 v_qtdeConsumirLastro := v_qtdeConsumirLastro -
1006 v_qtdeRemanejamento;
1007
1008 v_id := cadastrar_remanejamento(r_end.idarmazem, r_end.idarmazem,
1009 r_estlocal.idlocal,
1010 v_enderPickUtilizar, p_idusuario,
1011 NULL,
1012 'REABASTECIMENTO POR DEMANDA PLANEJADA',
1013 'N', 'N');
1014
1015 insert into gtt_selecao
1016 values
1017 (v_id);
1018
1019 insert into loteremanejamento
1020 (idremanejamento, idlote, qtde, conferido)
1021 values
1022 (v_id, r_estlocal.idlote, v_qtdeRemanejamento, c_normal);
1023
1024 exit when v_qtdeConsumirLastro = 0;
1025
1026 end if;
1027
1028 end loop;
1029
1030 select count(1)
1031 into v_gerouRemanejamento
1032 from gtt_selecao;
1033
1034 if (v_gerouRemanejamento = 0) then
1035
1036 if (v_qtdeConsumirLastro > 0) then
1037 v_msg := t_message('Reabastecimento Planejado');
1038 v_msgDescr := t_message('Local: {0} não foi reabastecido por falta de estoque compatÃvel. ' ||
1039 'Não foi possÃvel cadastrar novo picking para o produto.');
1040 v_msgDescr.addParam(r_end.idlocal);
1041 pk_gttresumoexecucao.addResumo(v_msg.formatMessage,
1042 v_msgDescr.formatMessage,
1043 pk_gttresumoexecucao.TIPO_ALERTA);
1044
1045 else
1046 v_msg := t_message('Reabastecimento Planejado');
1047 v_msgDescr := t_message('Local: {0} não foi reabastecido por falta de estoque.');
1048 v_msgDescr.addParam(r_end.idlocal);
1049 pk_gttresumoexecucao.addResumo(v_msg.formatMessage,
1050 v_msgDescr.formatMessage,
1051 pk_gttresumoexecucao.TIPO_ALERTA);
1052
1053 end if;
1054
1055 end if;
1056
1057 end loop;
1058
1059 select a.gerareabnaoplanejado
1060 into v_reabNaoPlanejado
1061 from armazem a
1062 where a.idarmazem = p_idarmazem;
1063
1064 if (v_reabNaoPlanejado = 1) then
1065 v_planejado := 'N';
1066 end if;
1067
1068 update remanejamento
1069 set planejado = v_planejado
1070 where idremanejamento in (select idselecionado
1071 from gtt_selecao);
1072 end;
1073
1074 procedure reabastecerPorEmbalagem
1075 (
1076 p_idarmazem in number,
1077 p_iddepositante in number,
1078 p_idproduto in number,
1079 p_idlocal in local.idlocal%type,
1080 p_idusuario in number
1081 ) is
1082
1083 c_lote cursor_lote;
1084 r_lote rec_lote;
1085 v_ressuprimento number;
1086 v_qtde number;
1087 v_qtdelote number;
1088 v_idremanejmanto number;
1089 v_exit boolean;
1090 v_estoqueIncompativel number := 0;
1091 v_gerouRemanejamento number;
1092 r_loteunico pk_lote.t_loteunico;
1093 v_enderecoCompativel boolean;
1094 v_idLocalPickNovo local.idlocal%type;
1095 v_msgErro varchar2(1000);
1096 v_enderPickUtilizar local.idlocal%type;
1097 v_msg t_message;
1098 v_msgDescr t_message;
1099 v_planejado char(1) := 'S';
1100 v_reabNaoPlanejado number;
1101
1102 procedure openCursorLote
1103 (
1104 p_cursor in out cursor_lote,
1105 p_lote in out rec_lote,
1106 p_depositante number,
1107 p_produto number,
1108 p_estado lote.estado%type,
1109 p_armazem number,
1110 p_tipopermitirpickingsetor setor.tipopermitirpickingsetor%type
1111 ) is
1112 v_sql varchar2(5000);
1113 begin
1114 if (p_cursor%isopen) then
1115 close p_cursor;
1116 end if;
1117
1118 if (p_tipopermitirpickingsetor = 0) then
1119
1120 v_sql := 'select ll.idarmazem, ll.idlocal, ll.idlote,
1121 ll.estoque - ll.pendencia + ll.adicionar disponivel,
1122 nvl(lt.dtvenc, trunc(sysdate)) datavencimento, s.idsetor,
1123 (case
1124 when e.fatorconversao = 1 then
1125 ec.fatorconversao
1126 else
1127 e.fatorconversao
1128 end) fatorconversao, lt.descr loteindustria
1129 from lotelocal ll, lote lt, local l, setor s, embalagem e, embalagem ec,
1130 produtodepositante pd
1131 where l.tipo in (1, 2)
1132 and decode(l.ativo, ''S'', 1, 0) = 1
1133 and decode(l.buffer, ''S'', 1, 0) = 0
1134 and decode(lt.tipolote, ''L'', 1, 0) = 1
1135 and decode(lt.liberado, ''S'', 1, 0) = 1
1136 and decode(s.expedicao, ''S'', 1, 0) = 1
1137 and lt.estado = ''' || p_estado || '''
1138 and lt.idproduto = ' || p_produto || '
1139 and lt.iddepositante = ' ||
1140 p_depositante || '
1141 and ll.idarmazem = ' || p_armazem || '
1142 and s.idsetor = l.idsetor
1143 and e.barra = lt.barra
1144 and e.idproduto = lt.idproduto
1145 and ec.barra = pk_produto.RetBarraMaiorFatorSemErro(lt.idproduto)
1146 and ec.idproduto = lt.idproduto
1147 and l.idlocal = ll.idlocal
1148 and l.idarmazem = ll.idarmazem
1149 and lt.idlote = ll.idlote
1150 and ll.estoque - ll.pendencia + ll.adicionar > 0
1151 and pd.idproduto = lt.idproduto
1152 and pd.identidade = lt.iddepositante
1153 and pd.tipopicking = ' ||
1154 p_tipopermitirpickingsetor || '
1155 and not exists (select 1
1156 from setorrecebimento sr, tiporecebimento tr
1157 where sr.idsetor = l.idsetor
1158 and tr.idtiporecebimento = sr.idtiporecebimento
1159 and tr.classificacao = ''R'')
1160 order by nvl(lt.dtvenc, trunc(sysdate)), ll.idlote ';
1161
1162 elsif (p_tipopermitirpickingsetor = 1) then
1163
1164 v_sql := 'select ll.idarmazem, ll.idlocal, ll.idlote,
1165 ll.estoque - ll.pendencia + ll.adicionar disponivel,
1166 nvl(lt.dtvenc, trunc(sysdate)) datavencimento, s.idsetor,
1167 (case
1168 when e.fatorconversao = 1 then
1169 ec.fatorconversao
1170 else
1171 e.fatorconversao
1172 end) fatorconversao, lt.descr loteindustria
1173 from lotelocal ll, lote lt, local l, setor s, embalagem e, embalagem ec,
1174 produtodepositante pd
1175 where l.tipo in (1, 2)
1176 and decode(l.ativo, ''S'', 1, 0) = 1
1177 and decode(l.buffer, ''S'', 1, 0) = 0
1178 and decode(lt.tipolote, ''L'', 1, 0) = 1
1179 and decode(lt.liberado, ''S'', 1, 0) = 1
1180 and decode(s.expedicao, ''S'', 1, 0) = 1
1181 and lt.estado = ''' || p_estado || '''
1182 and lt.idproduto = ' || p_produto || '
1183 and lt.iddepositante = ' ||
1184 p_depositante || '
1185 and ll.idarmazem = ' || p_armazem || '
1186 and s.idsetor = l.idsetor
1187 and e.barra = lt.barra
1188 and e.idproduto = lt.idproduto
1189 and e.fatorconversao = 1
1190 and e.caixafechada = ''N''
1191 and ec.barra = pk_produto.RetBarraMaiorFatorSemErro(lt.idproduto)
1192 and ec.idproduto = lt.idproduto
1193 and l.idlocal = ll.idlocal
1194 and l.idarmazem = ll.idarmazem
1195 and lt.idlote = ll.idlote
1196 and ll.estoque - ll.pendencia + ll.adicionar > 0
1197 and pd.idproduto = lt.idproduto
1198 and pd.identidade = lt.iddepositante
1199 and pd.tipopicking = ' ||
1200 p_tipopermitirpickingsetor || '
1201 and not exists (select 1
1202 from setorrecebimento sr, tiporecebimento tr
1203 where sr.idsetor = l.idsetor
1204 and tr.idtiporecebimento = sr.idtiporecebimento
1205 and tr.classificacao = ''R'')
1206 order by nvl(lt.dtvenc, trunc(sysdate)), ll.idlote ';
1207
1208 end if;
1209
1210 open p_cursor for v_sql;
1211
1212 fetch p_cursor
1213 into p_lote;
1214 end;
1215
1216 begin
1217
1218 for c_origem in (select v.idarmazem, v.idlocal, v.estanteria, v.idproduto,
1219 v.iddepositante, v.estadolote,
1220 v.pontoreabastecimento,
1221 nvl(v.limiteressuprimento, 0) pontoressuprimento,
1222 v.fatorconversao, v.disponivelpk disponivel,
1223 v.fracionado, pd.loteuniconoendereco,
1224 v.tipopermitirpickingsetor
1225 from v_reabastecimento_planejado v,
1226 produtodepositante pd
1227 where v.idproduto = p_idproduto
1228 and v.iddepositante = p_iddepositante
1229 and v.idlocal = p_idlocal
1230 and v.idarmazem = p_idarmazem
1231 and pd.idproduto = p_idproduto
1232 and pd.identidade = p_iddepositante)
1233 loop
1234
1235 v_enderPickUtilizar := c_origem.idlocal;
1236
1237 delete from gtt_selecao;
1238
1239 v_ressuprimento := (c_origem.pontoressuprimento *
1240 c_origem.fatorconversao) - c_origem.disponivel;
1241 v_qtde := 0;
1242 v_exit := false;
1243
1244 openCursorLote(c_lote, r_lote, c_origem.iddepositante,
1245 c_origem.idproduto, c_origem.estadolote,
1246 c_origem.idarmazem, c_origem.tipopermitirpickingsetor);
1247
1248 if (c_origem.pontoressuprimento = 0) then
1249 v_msg := t_message('Reabastecimento Planejado');
1250 v_msgDescr := t_message('Local: {0}' ||
1251 'O Local não será reabastecido pois o "Limite de Ressuprimento" no cadastro do produto deve ser configurado.');
1252 v_msgDescr.addParam(c_origem.idlocal);
1253 pk_gttresumoexecucao.addResumo(v_msg.formatMessage,
1254 v_msgDescr.formatMessage,
1255 pk_gttresumoexecucao.TIPO_ALERTA);
1256 return;
1257 end if;
1258
1259 while c_lote%found
1260 and (v_qtde <
1261 (c_origem.pontoressuprimento * c_origem.fatorconversao))
1262 and (not v_exit)
1263 loop
1264
1265 if (c_origem.loteuniconoendereco = 1) then
1266
1267 select count(1)
1268 into v_estoqueIncompativel
1269 from dual
1270 where exists
1271 (select 1
1272 from lotelocal ll, lote lt
1273 where ll.idarmazem = c_origem.idarmazem
1274 and ll.idlocal = c_origem.idlocal
1275 and ll.estoque + ll.adicionar > 0
1276 and lt.idlote = ll.idlote
1277 and lt.idproduto = c_origem.idproduto
1278 and lt.iddepositante = c_origem.iddepositante
1279 and (decode(lt.descr, r_lote.loteindustria, 1, 0) = 0 or
1280 decode(lt.dtvenc, r_lote.datavencimento, 1, 0) = 0));
1281
1282 end if;
1283
1284 if v_estoqueIncompativel > 0 then
1285
1286 v_idLocalPickNovo := null;
1287
1288 r_loteunico.idProduto := c_origem.idproduto;
1289 r_loteunico.estado := c_origem.estadolote;
1290 r_loteunico.loteindustria := r_lote.loteindustria;
1291 r_loteunico.dtvencimento := r_lote.datavencimento;
1292 r_loteunico.loteuniconoendereco := c_origem.loteuniconoendereco;
1293 r_loteunico.iddepositante := c_origem.iddepositante;
1294
1295 for c_endPicking in (select idarmazem, idlocal
1296 from (select pl.idarmazem, pl.idlocal,
1297 0 ordem
1298 from produtolocal pl, local l,
1299 setor s, tiposetor ts,
1300 lotelocal ll, lote lt
1301 where pl.identidade =
1302 c_origem.iddepositante
1303 and pl.idproduto =
1304 c_origem.idproduto
1305 and pl.idarmazem =
1306 c_origem.idarmazem
1307 and l.idlocal = pl.idlocal
1308 and l.idarmazem = pl.idarmazem
1309 and s.idsetor = l.idsetor
1310 and s.tipopermitirpickingsetor =
1311 c_origem.tipopermitirpickingsetor
1312 and ts.idtiposetor = s.idtiposetor
1313 and ts.normal = 'S'
1314 and l.ativo = 'S'
1315 and l.tipo = 0
1316 and ll.idendereco = l.id
1317 and (ll.estoque + ll.adicionar) > 0
1318 and lt.idlote = ll.idlote
1319 and lt.idproduto = pl.idproduto
1320 and lt.iddepositante =
1321 pl.identidade
1322 and lt.descr = r_lote.loteindustria
1323 and lt.dtvenc =
1324 r_lote.datavencimento
1325 union
1326 select pl.idarmazem, pl.idlocal,
1327 1 ordem
1328 from produtolocal pl, local l,
1329 setor s, tiposetor ts
1330 where pl.identidade =
1331 c_origem.iddepositante
1332 and pl.idproduto =
1333 c_origem.idproduto
1334 and pl.idarmazem =
1335 c_origem.idarmazem
1336 and l.idlocal = pl.idlocal
1337 and l.idarmazem = pl.idarmazem
1338 and s.idsetor = l.idsetor
1339 and s.tipopermitirpickingsetor =
1340 c_origem.tipopermitirpickingsetor
1341 and ts.idtiposetor = s.idtiposetor
1342 and ts.normal = 'S'
1343 and l.ativo = 'S'
1344 and l.tipo = 0
1345 and not exists
1346 (select 1
1347 from lotelocal ll
1348 where ll.idendereco = l.id
1349 and (ll.estoque +
1350 ll.adicionar) > 0)
1351 union
1352 select pl.idarmazem, pl.idlocal,
1353 2 ordem
1354 from produtolocal pl, local l,
1355 setor s, tiposetor ts,
1356 lotelocal ll, lote lt
1357 where pl.identidade =
1358 c_origem.iddepositante
1359 and pl.idproduto =
1360 c_origem.idproduto
1361 and pl.idarmazem =
1362 c_origem.idarmazem
1363 and l.idlocal = pl.idlocal
1364 and l.idarmazem = pl.idarmazem
1365 and s.idsetor = l.idsetor
1366 and s.tipopermitirpickingsetor =
1367 c_origem.tipopermitirpickingsetor
1368 and ts.idtiposetor = s.idtiposetor
1369 and ts.normal = 'S'
1370 and l.ativo = 'S'
1371 and l.tipo = 0
1372 and ll.idendereco = l.id
1373 and (ll.estoque + ll.adicionar) > 0
1374 and lt.idlote = ll.idlote
1375 and lt.idproduto = pl.idproduto
1376 and lt.iddepositante =
1377 pl.identidade
1378 and (lt.descr <>
1379 r_lote.loteindustria or
1380 lt.dtvenc <>
1381 r_lote.datavencimento))
1382 order by ordem)
1383
1384 loop
1385
1386 v_enderecoCompativel := pk_lote.isLocalPodeReceberLoteUnico(r_loteUnico,
1387 c_endPicking.Idarmazem,
1388 c_endPicking.Idlocal,
1389 v_msgErro);
1390
1391 if (v_enderecoCompativel) then
1392 v_idLocalPickNovo := c_endPicking.Idlocal;
1393 v_enderPickUtilizar := v_idLocalPickNovo;
1394 v_estoqueIncompativel := 0;
1395
1396 v_msg := t_message('Reabastecimento Planejado');
1397 v_msgDescr := t_message('Local: {0}' ||
1398 ' não foi reabastecido por falta de estoque compatÃvel. ' ||
1399 chr(13) ||
1400 'Reabastecimento planejado para o local: {1}.');
1401 v_msgDescr.addParam(c_origem.idlocal);
1402 v_msgDescr.addParam(v_enderPickUtilizar);
1403 pk_gttresumoexecucao.addResumo(v_msg.formatMessage,
1404 v_msgDescr.formatMessage,
1405 pk_gttresumoexecucao.TIPO_ALERTA);
1406 exit;
1407 end if;
1408
1409 end loop;
1410
1411 if ((pk_onda.usaPickingDinamico(c_origem.idproduto,
1412 c_origem.iddepositante,
1413 c_origem.idarmazem) = 1) and
1414 (v_idLocalPickNovo is null)) then
1415
1416 v_idLocalPickNovo := pk_picking_dinamico.inserir_picking_dinamico_auto(c_origem.idproduto,
1417 c_origem.iddepositante,
1418 c_origem.idarmazem,
1419 0,
1420 r_loteUnico);
1421
1422 if v_idLocalPickNovo is not null then
1423 v_enderPickUtilizar := v_idLocalPickNovo;
1424 v_estoqueIncompativel := 0;
1425
1426 v_msg := t_message('Reabastecimento Planejado');
1427 v_msgDescr := t_message('Local: {0}' ||
1428 ' não foi reabastecido por falta de estoque compatÃvel. ' ||
1429 chr(13) ||
1430 'Reabastecimento planejado para o local: ' ||
1431 '{1}.');
1432 v_msgDescr.addParam(c_origem.idlocal);
1433 v_msgDescr.addParam(v_enderPickUtilizar);
1434 pk_gttresumoexecucao.addResumo(v_msg.formatMessage,
1435 v_msgDescr.formatMessage,
1436 pk_gttresumoexecucao.TIPO_ALERTA);
1437 end if;
1438
1439 end if;
1440
1441 end if;
1442
1443 if v_estoqueIncompativel = 0 then
1444
1445 v_qtdelote := 0;
1446
1447 if r_lote.disponivel = v_ressuprimento then
1448 v_qtdelote := r_lote.disponivel;
1449 v_exit := true;
1450 elsif r_lote.disponivel < v_ressuprimento then
1451 v_qtdelote := r_lote.disponivel;
1452 else
1453 if c_origem.fracionado = 'S' then
1454 v_qtdelote := v_ressuprimento;
1455 else
1456 while (v_qtdelote < v_ressuprimento)
1457 and (not v_exit)
1458 loop
1459 if (v_qtdelote + r_lote.fatorconversao) <= v_ressuprimento then
1460 v_qtdelote := v_qtdelote + r_lote.fatorconversao;
1461 v_exit := false;
1462 else
1463 v_exit := true;
1464 end if;
1465 end loop;
1466 end if;
1467 end if;
1468
1469 if v_qtdelote > 0 then
1470
1471 v_idremanejmanto := cadastrar_remanejamento(c_origem.idarmazem,
1472 c_origem.idarmazem,
1473 r_lote.idlocal,
1474 v_enderPickUtilizar,
1475 p_idusuario, NULL,
1476 'REABASTECIMENTO POR DEMANDA PLANEJADA',
1477 'N', 'N');
1478
1479 insert into gtt_selecao
1480 values
1481 (v_idremanejmanto);
1482
1483 begin
1484 insert into loteremanejamento
1485 (idremanejamento, idlote, qtde, conferido)
1486 values
1487 (v_idremanejmanto, r_lote.idlote, v_qtdelote, 'N');
1488 exception
1489 when dup_val_on_index then
1490 update loteremanejamento
1491 set qtde = qtde + v_qtdelote
1492 where idremanejamento = v_idremanejmanto
1493 and idlote = r_lote.idlote;
1494 end;
1495 end if;
1496
1497 v_qtde := v_qtde + v_qtdelote;
1498 v_ressuprimento := v_ressuprimento - v_qtdelote;
1499
1500 end if;
1501
1502 fetch c_lote
1503 into r_lote;
1504 end loop;
1505
1506 select count(1)
1507 into v_gerouRemanejamento
1508 from gtt_selecao;
1509
1510 if (v_gerouRemanejamento = 0) then
1511
1512 if (v_estoqueIncompativel > 0) then
1513 v_msg := t_message('Reabastecimento Planejado');
1514 v_msgDescr := t_message('Local: {0}' ||
1515 ' não foi reabastecido por falta de estoque compatÃvel. ' ||
1516 'Não foi possÃvel cadastrar novo picking para o produto.');
1517 v_msgDescr.addParam(c_origem.idlocal);
1518 pk_gttresumoexecucao.addResumo(v_msg.formatMessage,
1519 v_msgDescr.formatMessage,
1520 pk_gttresumoexecucao.TIPO_ALERTA);
1521
1522 else
1523 v_msg := t_message('Reabastecimento Planejado');
1524 v_msgDescr := t_message('Local: {0}' ||
1525 ' não foi reabastecido por falta de estoque.');
1526 v_msgDescr.addParam(c_origem.idlocal);
1527 pk_gttresumoexecucao.addResumo(v_msg.formatMessage,
1528 v_msgDescr.formatMessage,
1529 pk_gttresumoexecucao.TIPO_ALERTA);
1530
1531 end if;
1532
1533 end if;
1534
1535 close c_lote;
1536
1537 end loop;
1538
1539 select a.gerareabnaoplanejado
1540 into v_reabNaoPlanejado
1541 from armazem a
1542 where a.idarmazem = p_idarmazem;
1543
1544 if (v_reabNaoPlanejado = 1) then
1545 v_planejado := 'N';
1546 end if;
1547
1548 update remanejamento
1549 set planejado = v_planejado,
1550 cadmanual = 'S'
1551 where idremanejamento in (select idselecionado
1552 from gtt_selecao);
1553 end;
1554
1555 /*
1556 * procedure responsavel por gerar os reabastecimentos automaticos
1557 */
1558 procedure reabastecer_automatico
1559 (
1560 p_idarmazem in number,
1561 p_iddepositante in number,
1562 p_idproduto in number,
1563 p_idlocal in local.idlocal%type,
1564 p_idusuario in number
1565 ) is
1566 v_tiporeabastecimento number;
1567 begin
1568 select tiporeabastecimento
1569 into v_tiporeabastecimento
1570 from configuracao
1571 where ativo = 'S';
1572
1573 if v_tiporeabastecimento = 0 then
1574 reabastecerPorLastro(p_idarmazem, p_iddepositante, p_idproduto,
1575 p_idlocal, p_idusuario);
1576 else
1577 reabastecerPorEmbalagem(p_idarmazem, p_iddepositante, p_idproduto,
1578 p_idlocal, p_idusuario);
1579 end if;
1580 end;
1581
1582 /*
1583 * Carrega as informacoes do remanejamento
1584 */
1585 function CarregarRemanejamento(p_idremanejamento in number)
1586 return remanejamento%rowtype is
1587 r_remanejamento remanejamento%rowtype;
1588 begin
1589 select *
1590 into r_remanejamento
1591 from remanejamento
1592 where idremanejamento = p_idremanejamento;
1593 return r_remanejamento;
1594 end;
1595
1596 procedure validarInclusaoLote
1597 (
1598 p_idlote in lote.idlote%type,
1599 p_idarmazemorigem in armazem.idarmazem%type,
1600 p_idlocalorigem in local.idlocal%type,
1601 p_idarmazemdestino in armazem.idarmazem%type,
1602 p_idlocaldestino in local.idlocal%type,
1603 v_msg in out varchar,
1604 v_erro in out boolean
1605 ) is
1606 v_idremanejamento number;
1607 begin
1608 -- Valida se o lote pode ser associado ao remanejamento
1609 begin
1610 select re.idremanejamento
1611 into v_idremanejamento
1612 from remanejamento re, loteremanejamento lr
1613 where lr.idremanejamento = re.idremanejamento
1614 and re.idarmazemdestino = p_idarmazemorigem
1615 and re.idlocaldestino = p_idlocalorigem
1616 and re.idarmazemorigem = p_idarmazemdestino
1617 and re.idlocalorigem = p_idlocaldestino
1618 and re.status <> 'F'
1619 and lr.idlote = p_idlote
1620 and rownum = 1;
1621
1622 v_erro := True;
1623 v_msg := v_msg || 'O remanejamento ' || v_idremanejamento ||
1624 ', contém o mesmo lote e com origem e destino inversos a este remanejamento. ' ||
1625 'Deve-se processar o remanejamento anterior ou escolher outro lote.';
1626 exception
1627 when no_data_found then
1628 v_idremanejamento := 0;
1629 end;
1630 end;
1631
1632 procedure validarInclusaoLoteMontagem
1633 (
1634 p_idlote in lote.idlote%type,
1635 p_idarmazemorigem in armazem.idarmazem%type,
1636 p_idlocalorigem in local.idlocal%type,
1637 p_idarmazemdestino in armazem.idarmazem%type,
1638 p_idlocaldestino in local.idlocal%type,
1639 v_msg in out varchar,
1640 v_erro in out boolean
1641 ) is
1642 v_tipoLocalOrigem number;
1643 v_tipoLocalDestino number;
1644 v_total number;
1645 begin
1646 -- validando inclusao de lote de montagem
1647 begin
1648 select l.tipo
1649 into v_tipolocalOrigem
1650 from local l
1651 where l.idarmazem = p_idarmazemorigem
1652 and l.idlocal = p_idlocalorigem;
1653 exception
1654 when no_data_found then
1655 v_erro := true;
1656 v_msg := v_msg || 'Local de origem não encontrado.';
1657 end;
1658 begin
1659 select l.tipo
1660 into v_tipolocalDestino
1661 from local l
1662 where l.idarmazem = p_idarmazemdestino
1663 and l.idlocal = p_idlocaldestino;
1664 exception
1665 when no_data_found then
1666 v_erro := true;
1667 v_msg := v_msg || 'Local de destino não encontrado.';
1668 end;
1669 if (v_tipoLocalOrigem = 0 and v_tipoLocalDestino in (1, 2)) then
1670 select count(*) total
1671 into v_total
1672 from lote
1673 where idlotemont = p_idlote;
1674 if v_total <> 0 then
1675 v_erro := true;
1676 v_msg := v_msg ||
1677 'Não é permitido incluir lotes avulsos neste tipo de remanejamento ou ' ||
1678 'o lote é componente de montagem, utilize a função "Formar Lotes" ou escolha ' ||
1679 'o lote agrupador de montagem.';
1680 else
1681 v_erro := true;
1682 v_msg := v_msg ||
1683 'Não é possÃvel Incluir Lotes em Remanejamentos de Picking para Pulmão.';
1684 end if;
1685 end if;
1686 end;
1687
1688 procedure validarInclusaoCompMontagem
1689 (
1690 p_idlote in lote.idlote%type,
1691 v_msg in out varchar,
1692 v_erro in out boolean
1693 ) is
1694 begin
1695 -- valida se o lote e um componente de uma montagem, se for nao permite incluir o lote
1696 -- ao remanejamento
1697 if not CtrlValManipCompMont(p_idlote) then
1698 v_erro := True;
1699 v_msg := v_msg ||
1700 'Lote componente de uma montagem não pode ser associado a um remanejamento. Selecione o lote principal da montagem.';
1701 end if;
1702 end;
1703
1704 procedure validarSetor
1705 (
1706 p_idlote in lote.idlote%type,
1707 p_idarmazemdestino in armazem.idarmazem%type,
1708 p_idlocaldestino in local.idlocal%type,
1709 v_msg in out varchar,
1710 v_erro in out boolean
1711 ) is
1712 v_msgRaise t_message;
1713 begin
1714 -- Validando o Setor
1715 if CtrlvalidarSetor(p_idlote, p_idarmazemdestino, p_idlocaldestino) =
1716 false then
1717 v_msgRaise := t_message('{0} O setor do Lote ou Volume {1}' ||
1718 ' não está compatÃvel com o setor do endereço de destino do remanejamento.' ||
1719 chr(13));
1720 v_msgRaise.addParam(v_msg);
1721 v_msgRaise.addParam(to_char(p_idLote));
1722 v_msg := v_msgRaise.formatMessage;
1723 v_erro := true;
1724 end if;
1725 end;
1726
1727 procedure validarPicking
1728 (
1729 p_idlote in lote.idlote%type,
1730 p_idarmazemdestino in armazem.idarmazem%type,
1731 p_idlocaldestino in local.idlocal%type,
1732 v_msg in out varchar,
1733 v_erro in out boolean
1734 ) is
1735 v_picking local.picking%type;
1736 begin
1737 -- Validando o picking
1738 select picking
1739 into v_picking
1740 from local
1741 where idlocal = p_idlocaldestino
1742 and idarmazem = p_idarmazemdestino;
1743
1744 if v_picking = 'S' then
1745 if CtrlValidarPicking(p_idlote, p_idarmazemdestino, p_idlocaldestino) =
1746 false then
1747 v_msg := v_msg ||
1748 'O endereço de destino do remanejamento não é picking do Lote ou Volume ' ||
1749 to_char(p_idLote) || '.' || chr(13);
1750 v_erro := true;
1751 end if;
1752 end if;
1753 end;
1754
1755 procedure validarRestricoesFisicas
1756 (
1757 p_idlote in lote.idlote%type,
1758 p_tipolote in lote.tipolote%type,
1759 p_idproduto in lote.idproduto%type,
1760 p_idarmazemdestino in armazem.idarmazem%type,
1761 p_idlocaldestino in local.idlocal%type,
1762 p_qtdedisponivel in number,
1763 v_msg in out varchar,
1764 v_erro in out boolean
1765 ) is
1766 v_barra embalagem.barra%type;
1767 v_tipoerro number;
1768 begin
1769 -- Validando restrições Fisicas
1770 if p_tipolote = c_lote then
1771 select barra
1772 into v_barra
1773 from lote
1774 where idlote = p_idlote;
1775 if CtrlValidarRestriFisicas(p_idproduto, p_qtdeDisponivel,
1776 p_idarmazemDestino, p_idlocalDestino,
1777 v_barra, v_tipoerro) = false then
1778
1779 if v_tipoerro = 0 then
1780 v_msg := v_msg ||
1781 'O endereço de destino do remanejamento não possui capacidade de peso para alocar o Lote ou Volume ' ||
1782 to_char(p_idLote) || '.' || chr(13);
1783 v_erro := true;
1784 elsif v_tipoerro = 1 then
1785 v_msg := v_msg ||
1786 'O endereço de destino do remanejamento não possui capacidade de cubagem para alocar o Lote ou Volume ' ||
1787 to_char(p_idLote) || '.' || chr(13);
1788 v_erro := true;
1789 elsif v_tipoerro = 2 then
1790 v_msg := v_msg ||
1791 'O endereço de destino do remanejamento não possui capacidade de paletização para alocar o Lote ou Volume ' ||
1792 to_char(p_idLote) || '.' || chr(13);
1793 v_erro := true;
1794 end if;
1795 end if;
1796 end if;
1797 end;
1798
1799 /*
1800 * Procedure para Vincular os produtos ao remanejamento
1801 */
1802 function CtrlVincularMaterial
1803 (
1804 p_idlote in lote.idlote%type,
1805 p_tipolote in lote.tipolote%type,
1806 p_idproduto in lote.idproduto%type,
1807 p_idarmazemorigem in armazem.idarmazem%type,
1808 p_idlocalorigem in local.idlocal%type,
1809 p_idarmazemdestino in armazem.idarmazem%type,
1810 p_idlocaldestino in local.idlocal%type,
1811 p_qtdedisponivel in number
1812 ) return varchar is
1813 v_msg varchar(2000);
1814 v_erro boolean := false;
1815 v_isDestinoPacking number;
1816 begin
1817 v_msg := '';
1818
1819 validarInclusaoLoteMontagem(p_idlote, p_idarmazemorigem,
1820 p_idlocalorigem, p_idarmazemdestino,
1821 p_idlocaldestino, v_msg, v_erro);
1822
1823 validarInclusaoLote(p_idlote, p_idarmazemorigem, p_idlocalorigem,
1824 p_idarmazemdestino, p_idlocaldestino, v_msg, v_erro);
1825
1826 validarInclusaoCompMontagem(p_idlote, v_msg, v_erro);
1827
1828 validarSetor(p_idlote, p_idarmazemdestino, p_idlocaldestino, v_msg,
1829 v_erro);
1830
1831 validarPicking(p_idlote, p_idarmazemdestino, p_idlocaldestino, v_msg,
1832 v_erro);
1833
1834 select count(1)
1835 into v_isDestinoPacking
1836 from local l
1837 where l.idlocal = p_idlocaldestino
1838 and l.idarmazem = p_idarmazemdestino
1839 and exists (select 1
1840 from packing p
1841 where p.idendereco = l.id);
1842
1843 if (v_isDestinoPacking = 0) then
1844 validarRestricoesFisicas(p_idlote, p_tipolote, p_idproduto,
1845 p_idarmazemdestino, p_idlocaldestino,
1846 p_qtdedisponivel, v_msg, v_erro);
1847 end if;
1848
1849 if v_erro then
1850 return v_msg;
1851 else
1852 v_msg := '';
1853 return v_msg;
1854 end if;
1855 end;
1856
1857 /*
1858 * Retorna a quantidade a ser remanejada
1859 */
1860 function CtrlObterQtdeRem
1861 (
1862 p_idlocal in local.idlocal%type,
1863 p_idLote in lote.idlote%type,
1864 p_idProduto in lote.idproduto%type,
1865 p_qtdeDispLote in number,
1866 p_pedirCodBarra in boolean := false
1867 ) return number is
1868 v_fatorConversao embalagem.fatorconversao%type;
1869 v_qtdeRemanejar number;
1870
1871 begin
1872 if p_qtdeDispLote <= 1 then
1873 return p_qtdeDispLote;
1874 else
1875 if p_pedirCodBarra = false then
1876 select em.fatorconversao
1877 into v_fatorConversao
1878 from embalagem em
1879 where em.idproduto = p_idProduto
1880 and em.barra =
1881 pk_produto.RetornarCodBarraMenorFator(p_idproduto);
1882 v_qtdeRemanejar := p_qtdeDispLote / v_fatorConversao;
1883 return v_qtdeRemanejar;
1884 end if;
1885 end if;
1886 end;
1887
1888 /*
1889 * Função para retornar se um setor é valido para alocação e remanejamento
1890 */
1891 function CtrlvalidarSetor
1892 (
1893 p_idlote in lote.idlote%type,
1894 p_idarmazem in armazem.idarmazem%type,
1895 p_idlocal in local.idlocal%type
1896 ) return boolean is
1897 v_idsetor number;
1898 v_qtdesetor number;
1899 begin
1900 begin
1901 select idsetor
1902 into v_idsetor
1903 from local lo
1904 where lo.idarmazem = p_idarmazem
1905 and lo.idlocal = p_idlocal;
1906 exception
1907 when no_data_found then
1908 return false;
1909 end;
1910
1911 select distinct count(sp.idsetor)
1912 into v_qtdesetor
1913 from lote l, setorproduto sp, setordepositante sd
1914 where l.idlote = p_idlote
1915 and sp.idproduto = l.idproduto
1916 and sd.idsetor = sp.idsetor
1917 and sd.iddepositante = l.iddepositante
1918 and sp.idsetor = v_idsetor;
1919 if v_qtdesetor > 0 then
1920 return true;
1921 else
1922 return false;
1923 end if;
1924 end;
1925
1926 /*
1927 * Validação do picking
1928 */
1929 function CtrlValidarPicking
1930 (
1931 p_idlote in lote.idlote%type,
1932 p_idarmazem in armazem.idarmazem%type,
1933 p_idlocal in local.idlocal%type
1934 ) return boolean is
1935 v_retorno boolean;
1936 v_qtdelocal number;
1937 v_pickingdinamico number;
1938
1939 v_picking char(1);
1940 begin
1941 v_retorno := false;
1942
1943 begin
1944 select sum(nvl(pl.qtdepk, 0)), nvl(pd.pickingdinamico, 0)
1945 into v_qtdelocal, v_pickingdinamico
1946 from lote l, produtodepositante pd,
1947 (select pk.identidade, pk.idproduto, pk.idarmazem, pk.idlocal,
1948 count(1) qtdepk
1949 from produtolocal pk, local lo
1950 where lo.idarmazem = pk.idarmazem
1951 and lo.idlocal = pk.idlocal
1952 and pk.idarmazem = p_idarmazem
1953 and pk.idlocal = p_idlocal
1954 and lo.ativo = 'S'
1955 group by pk.identidade, pk.idproduto, pk.idarmazem, pk.idlocal) pl
1956 where l.idlote = p_idlote
1957 and pd.identidade(+) = l.iddepositante
1958 and pd.idproduto(+) = l.idproduto
1959 and pl.identidade(+) = l.iddepositante
1960 and pl.idproduto(+) = l.idproduto
1961 group by pd.pickingdinamico;
1962 exception
1963 when no_data_found then
1964 v_qtdelocal := 0;
1965 v_pickingdinamico := 0;
1966 end;
1967 if (v_qtdelocal > 0)
1968 or (v_pickingdinamico = 1) then
1969 v_retorno := true;
1970 else
1971 v_retorno := false;
1972 end if;
1973 return v_retorno;
1974 end;
1975
1976 /*
1977 * Validar as restrições fÃsicas do destino
1978
1979 p_tipoerro retorna 0= Restrições de Peso / 1= Restrições de Cubagem / 2= Restrições de Paletização
1980
1981 */
1982 function CtrlValidarRestriFisicas
1983 (
1984 p_idproduto in produto.idproduto%type,
1985 p_qtde in number,
1986 p_idarmazem in armazem.idarmazem%type,
1987 p_idlocal in local.idlocal%type,
1988 p_barra in embalagem.barra%type,
1989 p_tipoerro in out number
1990 ) return boolean is
1991
1992 cursor c_embalagem
1993 (
1994 p_produto in produto.idproduto%type,
1995 pi_barra in embalagem.barra%type
1996 ) is
1997 select em.idproduto, em.barra, em.pesobruto,
1998 (em.altura * em.largura * em.comprimento) cubagem,
1999 em.fatorconversao,
2000 (1 / (em.lastro * nvl(em.empilhamentomax, em.qtdecamada))) qtdeplt
2001 from embalagem em
2002 where em.idproduto = p_produto
2003 and barra = pi_barra;
2004
2005 cursor c_dimensoes
2006 (
2007 p_armazem in armazem.idarmazem%type,
2008 p_local in local.idlocal%type
2009 ) is
2010 select l.idarmazem, l.idlocal,
2011 l.cubagem_end - nvl(oc.cubagem_est, 0) cubagemDisponivel,
2012 l.peso_end - nvl(oc.peso_est, 0) pesoDisponivel,
2013 (1 - nvl(oc.qtdeplt_est, 0)) qtdepltdisponivel, l.tipo
2014 from (select l.idarmazem, l.idlocal,
2015 (l.altura - l.alturamanobra) * l.largura *
2016 l.comprimento cubagem_end, l.pesomaximo peso_end,
2017 l.tipo
2018 from local l
2019 where l.idarmazem = p_armazem
2020 and l.idlocal = p_local) l,
2021 (select idarmazem, idlocal, round(sum(cubagem_est)) cubagem_est,
2022 round(sum(peso_est)) peso_est,
2023 sum(qtdeplt_est) qtdeplt_est
2024 from (select ll.idarmazem, idlocal,
2025 (e.altura * e.largura * e.comprimento) *
2026 ((ll.estoque + ll.adicionar) / e.fatorconversao) cubagem_est,
2027 e.pesobruto *
2028 ((ll.estoque + ll.adicionar) / e.fatorconversao) peso_est,
2029 ((ll.estoque + ll.adicionar) /
2030 (e.lastro *
2031 nvl(e.empilhamentomax, e.qtdecamada) *
2032 e.fatorconversao)) qtdeplt_est
2033 from lotelocal ll, lote l, embalagem e
2034 where ll.idarmazem = p_armazem
2035 and ll.idlocal = p_local
2036 and (ll.estoque - ll.pendencia + ll.adicionar) > 0
2037 and l.idlote = ll.idlote
2038 and l.tipolote = 'L'
2039 and e.idproduto = l.idproduto
2040 and e.barra = l.barra
2041 union all
2042 select m.idarmazem, idlocal,
2043 (e.altura * e.largura * e.comprimento) *
2044 l.qtdeentrada cubagem_est,
2045 e.pesobruto * l.qtdeentrada,
2046 ((m.qtde * e.fatorconversao) /
2047 (e.lastro *
2048 nvl(e.empilhamentomax, e.qtdecamada) *
2049 e.fatorconversao)) qtdeplt_est
2050 from mapaalocacao m, lote l, embalagem e
2051 where m.idarmazem = p_armazem
2052 and m.idlocal = p_local
2053 and m.status in ('P', 'A', 'M')
2054 and l.tipolote = 'L'
2055 and l.idlote = m.idlote
2056 and e.idproduto = l.idproduto
2057 and e.barra = l.barra) est
2058 group by idarmazem, idlocal) oc
2059 where oc.idarmazem(+) = l.idarmazem
2060 and oc.idlocal(+) = l.idlocal;
2061
2062 r_dimensoes c_dimensoes%rowtype;
2063 r_embalagem c_embalagem%rowtype;
2064 r_dimensoesFisicas t_dimensoesFisicas;
2065 v_pesoproduto number;
2066 v_cubagemproduto number;
2067 v_qtdepltproduto number;
2068 v_pesodisponivel number;
2069 v_cubagemdisponivel number;
2070 v_qtdepltdisponivel number;
2071 v_multiendereco produto.multiendereco%type;
2072 begin
2073 if c_embalagem%isopen then
2074 close c_embalagem;
2075 end if;
2076 open c_embalagem(p_idproduto, p_barra);
2077 fetch c_embalagem
2078 into r_embalagem;
2079 close c_embalagem;
2080
2081 v_pesoproduto := r_embalagem.pesobruto *
2082 (p_qtde / r_embalagem.fatorconversao);
2083 v_cubagemproduto := r_embalagem.cubagem *
2084 (p_qtde / r_embalagem.fatorconversao);
2085 v_qtdepltproduto := r_embalagem.qtdeplt *
2086 (p_qtde / r_embalagem.fatorconversao);
2087 r_dimensoesFisicas.peso := v_pesoproduto;
2088 r_dimensoesFisicas.cubagem := v_cubagemproduto;
2089 r_dimensoesFisicas.qtdeplt := v_qtdepltproduto;
2090
2091 -- Aqui é avaliado a gtt para saber se a validação esta acumulando dados anteriores
2092 for c_produto in (select g.idproduto, g.barra, g.qtde
2093 from gtt_produtoremanejamento g)
2094 loop
2095 if c_embalagem%isopen then
2096 close c_embalagem;
2097 end if;
2098 open c_embalagem(c_produto.idproduto, c_produto.barra);
2099 fetch c_embalagem
2100 into r_embalagem;
2101 close c_embalagem;
2102
2103 v_pesoproduto := r_embalagem.pesobruto *
2104 (c_produto.qtde /
2105 r_embalagem.fatorconversao);
2106 v_cubagemproduto := r_embalagem.cubagem *
2107 (c_produto.qtde /
2108 r_embalagem.fatorconversao);
2109 v_qtdepltproduto := r_embalagem.qtdeplt *
2110 (c_produto.qtde /
2111 r_embalagem.fatorconversao);
2112 r_dimensoesFisicas.peso := r_dimensoesFisicas.peso + v_pesoproduto;
2113 r_dimensoesFisicas.cubagem := r_dimensoesFisicas.cubagem +
2114 v_cubagemproduto;
2115 r_dimensoesFisicas.qtdeplt := r_dimensoesFisicas.qtdeplt +
2116 v_qtdepltproduto;
2117 end loop;
2118
2119 -- Checando a disponibilidade fÃsica
2120 if c_dimensoes%isopen then
2121 close c_dimensoes;
2122 end if;
2123 open c_dimensoes(p_idarmazem, p_idlocal);
2124 fetch c_dimensoes
2125 into r_dimensoes;
2126 close c_dimensoes;
2127
2128 v_pesodisponivel := r_dimensoes.pesodisponivel;
2129 v_cubagemdisponivel := r_dimensoes.cubagemdisponivel;
2130 v_qtdepltdisponivel := r_dimensoes.qtdepltdisponivel;
2131
2132 if v_pesodisponivel < 0 then
2133 v_pesodisponivel := 0;
2134 end if;
2135 if v_cubagemdisponivel < 0 then
2136 v_cubagemdisponivel := 0;
2137 end if;
2138 if v_qtdepltdisponivel < 0 then
2139 v_qtdepltdisponivel := 0;
2140 end if;
2141
2142 r_dimensoesFisicas.peso := r_dimensoesFisicas.peso -
2143 v_pesodisponivel;
2144 r_dimensoesFisicas.cubagem := r_dimensoesFisicas.cubagem -
2145 v_cubagemdisponivel;
2146 r_dimensoesFisicas.qtdeplt := trunc(r_dimensoesFisicas.qtdeplt -
2147 v_qtdepltdisponivel, 6);
2148
2149 -- Checando se o produto é multiendereco
2150 select pr.multiendereco
2151 into v_multiendereco
2152 from produto pr
2153 where pr.idproduto = p_idproduto;
2154
2155 -- Validando Multiendereco
2156 if ((r_dimensoesFisicas.peso > 0 or r_dimensoesFisicas.cubagem > 0) and
2157 v_multiendereco = 'S') then
2158 r_dimensoesFisicas := validarMultiendereco(r_dimensoesFisicas,
2159 v_multiEndereco, p_idarmazem,
2160 p_idlocal);
2161 end if;
2162
2163 -- p_tipoerro retorna 0= Restrições de Peso / 1= Restrições de Cubagem / 2= Restrições de Paletização
2164 if (r_dimensoesFisicas.peso > 0) then
2165 p_tipoerro := 0;
2166 return false;
2167 elsif (r_dimensoesFisicas.cubagem > 0) then
2168 p_tipoerro := 1;
2169 return false;
2170 elsif (r_dimensoesFisicas.qtdeplt > 0 and r_dimensoes.tipo = 2) then
2171 p_tipoerro := 2;
2172 return false;
2173 else
2174 return true;
2175 end if;
2176 end;
2177
2178 /*
2179 * Validação de multiendereco
2180 */
2181 function validarMultiendereco
2182 (
2183 p_dimensoes in t_dimensoesFisicas,
2184 p_multiendereco in produto.multiendereco%type,
2185 p_idarmazem in armazem.idarmazem%type,
2186 p_idlocal in local.idlocal%type
2187 ) return t_dimensoesFisicas is
2188
2189 cursor c_listaVizinhos
2190 (
2191 p_armazem in armazem.idarmazem%type,
2192 p_local in local.idlocal%type
2193 ) is
2194 select l.idarmazem, l.idlocal, l.rua, l.predio, l.andar, l.apartamento,
2195 l.numeroordem, l.pesomaximo, l.volumemaximo, l.altura,
2196 l.largura, l.comprimento, l.temperaturamin, l.temperaturamax,
2197 l.shelflife, l.classificacaoabc, l.estanteria, l.picking,
2198 l.ativo, l.qtdemax, s.descr setor, l.idSetor, l.modulo,
2199 l.alturamanobra,
2200 nvl(l.modulo, nvl(a.modulopadrao, 2)) modulopadrao,
2201 oc.cubagemDisponivel, oc.pesoDisponivel
2202 from (select l.idarmazem, l.idlocal, l.rua, l.predio, l.andar,
2203 l.apartamento,
2204 nvl(nvl(l.modulo, a.modulopadrao), 2) modulo,
2205 mod(l.predio, 2) MOD
2206 from armazem a, local l
2207 where a.idarmazem = p_armazem
2208 and l.idarmazem = a.idarmazem
2209 and l.idlocal = p_local) lo, armazem a, local l, setor s,
2210 (select l.idarmazem, l.idlocal,
2211 l.cubagem_end - nvl(oc.cubagem_est, 0) cubagemDisponivel,
2212 l.peso_end - nvl(oc.peso_est, 0) pesoDisponivel
2213 from (select l.idarmazem, l.idlocal,
2214 (l.altura - l.alturamanobra) * l.largura *
2215 l.comprimento cubagem_end, l.pesomaximo peso_end
2216 from local l) l,
2217 (select idarmazem, idlocal,
2218 round(sum(cubagem_est)) cubagem_est,
2219 round(sum(peso_est)) peso_est
2220 from (select ll.idarmazem, idlocal,
2221 (e.altura * e.largura * e.comprimento) *
2222 ((ll.estoque + ll.adicionar) /
2223 e.fatorconversao) cubagem_est,
2224 e.pesobruto * ((ll.estoque + ll.adicionar) /
2225 e.fatorconversao) peso_est
2226 from lotelocal ll, lote l, embalagem e
2227 where (ll.estoque - ll.pendencia + ll.adicionar) > 0
2228 and l.idlote = ll.idlote
2229 and l.tipolote = 'L'
2230 and e.idproduto = l.idproduto
2231 and e.barra = l.barra
2232 union all
2233 select m.idarmazem, idlocal,
2234 (e.altura * e.largura * e.comprimento) *
2235 l.qtdeentrada cubagem_est,
2236 e.pesobruto * l.qtdeentrada
2237 from mapaalocacao m, lote l, embalagem e
2238 where m.status in ('P', 'A', 'M')
2239 and l.idlote = m.idlote
2240 and l.tipolote = 'L'
2241 and e.idproduto = l.idproduto
2242 and e.barra = l.barra) est
2243 group by idarmazem, idlocal) oc
2244 where oc.idarmazem(+) = l.idarmazem
2245 and oc.idlocal(+) = l.idlocal) oc
2246 where a.idarmazem = lo.idarmazem
2247 and l.idarmazem = lo.idarmazem
2248 and l.rua = lo.rua
2249 and l.andar = lo.andar
2250 and l.apartamento = lo.apartamento
2251 and l.predio between lo.predio - (lo.modulo - 1) * 2 and
2252 lo.predio + (lo.modulo - 1) * 2
2253 and nvl(nvl(l.modulo, a.modulopadrao), 2) = lo.modulo
2254 and mod(l.predio, 2) = lo.mod
2255 and s.idsetor(+) = l.idsetor
2256 and oc.idarmazem(+) = l.idarmazem
2257 and oc.idlocal(+) = l.idlocal;
2258
2259 r_listaVizinhos c_listaVizinhos%rowtype;
2260 type t_listaIrmaos is table of local%rowtype;
2261 type t_listaVizinhos is table of c_listaVizinhos%rowtype;
2262 v_listaVizinhos t_listaVizinhos := t_listaVizinhos();
2263 v_listaIrmaos t_listaIrmaos := t_listaIrmaos();
2264 v_retorno t_dimensoesFisicas;
2265 v_indice number;
2266 v_anterior number;
2267 v_posterior number;
2268 v_sair boolean;
2269 i number;
2270 d number;
2271 v_qtdeVizinhos number;
2272 v_localAnterior local.idlocal%type;
2273 v_localPosterior local.idlocal%type;
2274 v_localAtual local.idlocal%type;
2275 v_inicioModulo boolean;
2276 v_predioAtual local.predio%type;
2277 v_predioAnterior local.predio%type;
2278 v_predioPosterior local.predio%type;
2279 v_predioValido boolean;
2280 v_peso number;
2281 v_cubagem number;
2282
2283 begin
2284 v_peso := p_dimensoes.peso;
2285 v_cubagem := p_dimensoes.cubagem;
2286 v_qtdeVizinhos := 1;
2287 if c_listaVizinhos%isopen then
2288 close c_listaVizinhos;
2289 end if;
2290 open c_listaVizinhos(p_idarmazem, p_idlocal);
2291 fetch c_listaVizinhos
2292 into r_listaVizinhos;
2293 while c_listaVizinhos%found
2294 loop
2295 v_listaVizinhos.Extend;
2296 v_listaVizinhos(v_qtdeVizinhos).idarmazem := r_listaVizinhos.idarmazem;
2297 v_listaVizinhos(v_qtdeVizinhos).idlocal := r_listaVizinhos.idlocal;
2298 v_listaVizinhos(v_qtdeVizinhos).rua := r_listaVizinhos.rua;
2299 v_listaVizinhos(v_qtdeVizinhos).predio := r_listaVizinhos.predio;
2300 v_listaVizinhos(v_qtdeVizinhos).andar := r_listaVizinhos.andar;
2301 v_listaVizinhos(v_qtdeVizinhos).apartamento := r_listaVizinhos.apartamento;
2302 v_listaVizinhos(v_qtdeVizinhos).numeroordem := r_listaVizinhos.numeroordem;
2303 v_listaVizinhos(v_qtdeVizinhos).pesomaximo := r_listaVizinhos.pesomaximo;
2304 v_listaVizinhos(v_qtdeVizinhos).volumemaximo := r_listaVizinhos.volumemaximo;
2305 v_listaVizinhos(v_qtdeVizinhos).altura := r_listaVizinhos.altura;
2306 v_listaVizinhos(v_qtdeVizinhos).largura := r_listaVizinhos.largura;
2307 v_listaVizinhos(v_qtdeVizinhos).comprimento := r_listaVizinhos.comprimento;
2308 v_listaVizinhos(v_qtdeVizinhos).temperaturamin := r_listaVizinhos.temperaturamin;
2309 v_listaVizinhos(v_qtdeVizinhos).temperaturamax := r_listaVizinhos.temperaturamax;
2310 v_listaVizinhos(v_qtdeVizinhos).shelflife := r_listaVizinhos.shelflife;
2311 v_listaVizinhos(v_qtdeVizinhos).classificacaoabc := r_listaVizinhos.classificacaoabc;
2312 v_listaVizinhos(v_qtdeVizinhos).estanteria := r_listaVizinhos.estanteria;
2313 v_listaVizinhos(v_qtdeVizinhos).picking := r_listaVizinhos.picking;
2314 v_listaVizinhos(v_qtdeVizinhos).ativo := r_listaVizinhos.ativo;
2315 v_listaVizinhos(v_qtdeVizinhos).qtdemax := r_listaVizinhos.qtdemax;
2316 v_listaVizinhos(v_qtdeVizinhos).setor := r_listaVizinhos.setor;
2317 v_listaVizinhos(v_qtdeVizinhos).idSetor := r_listaVizinhos.idSetor;
2318 v_listaVizinhos(v_qtdeVizinhos).modulo := r_listaVizinhos.modulo;
2319 v_listaVizinhos(v_qtdeVizinhos).alturamanobra := r_listaVizinhos.alturamanobra;
2320 v_listaVizinhos(v_qtdeVizinhos).modulopadrao := r_listaVizinhos.modulopadrao;
2321 v_qtdeVizinhos := v_qtdeVizinhos + 1;
2322
2323 fetch c_listaVizinhos
2324 into r_listaVizinhos;
2325 end loop;
2326 close c_listaVizinhos;
2327
2328 v_qtdeVizinhos := v_listaVizinhos.Count;
2329 i := 1;
2330 v_indice := -1;
2331 while (v_indice < 0)
2332 and (i < v_qtdeVizinhos)
2333 loop
2334 if v_listaVizinhos(i).idlocal = p_idlocal then
2335 v_indice := i;
2336 end if;
2337 i := i + 1;
2338 end loop;
2339
2340 i := v_indice + 1;
2341 if ((v_peso > 0) or (v_cubagem > 0))
2342 and (i < v_qtdeVizinhos) then
2343 v_anterior := v_indice;
2344 v_sair := false;
2345 d := 0;
2346
2347 while v_sair = false
2348 loop
2349 v_localAnterior := v_listaVizinhos(v_anterior).idlocal;
2350 v_localAtual := v_listaVizinhos(i).idlocal;
2351 v_inicioModulo := pk_alocacao.retornaEnderecoModulo(v_listaVizinhos(i)
2352 .idarmazem,
2353 v_listaVizinhos(i)
2354 .idlocal,
2355 v_listaVizinhos(i)
2356 .predio,
2357 v_listaVizinhos(i)
2358 .modulo);
2359 v_predioAtual := v_listaVizinhos(i).predio;
2360 v_predioAnterior := v_listaVizinhos(v_anterior).predio;
2361 if v_predioAtual = (v_predioAnterior + 2) then
2362 v_predioValido := true;
2363 else
2364 v_predioValido := false;
2365 end if;
2366
2367 if (v_inicioModulo = true)
2368 or (v_predioValido = false) then
2369 v_sair := true;
2370 end if;
2371
2372 if v_sair = false then
2373 v_peso := v_peso - v_listaVizinhos(i).pesomaximo;
2374 v_cubagem := v_cubagem - (v_listaVizinhos(i)
2375 .altura * v_listaVizinhos(i).largura * v_listaVizinhos(i)
2376 .comprimento);
2377 v_listaIrmaos.Extend;
2378 v_listaIrmaos(d).idlocal := v_listaVizinhos(i).idlocal;
2379 v_listaIrmaos(d).idarmazempai := v_listaVizinhos(i).idarmazem;
2380 d := d + 1;
2381 v_anterior := i;
2382 end if;
2383 if ((v_peso <= 0) and (v_cubagem <= 0) or (v_sair = true)) then
2384 i := i + 1;
2385 if (i >= v_qtdeVizinhos)
2386 or (v_sair = true) then
2387 v_sair := true;
2388 end if;
2389 end if;
2390 end loop;
2391 end if;
2392
2393 i := i - 1;
2394 if ((v_peso > 0) or (v_cubagem > 0) and (i >= 0)) then
2395 v_posterior := v_indice;
2396 v_sair := false;
2397
2398 while v_sair = false
2399 loop
2400 v_localPosterior := v_listaVizinhos(v_posterior).idLocal;
2401 v_localAtual := v_listaVizinhos(i).idlocal;
2402 v_inicioModulo := pk_alocacao.retornaEnderecoModulo(v_listaVizinhos(i)
2403 .idarmazem,
2404 v_listaVizinhos(i)
2405 .idlocal,
2406 v_listaVizinhos(i)
2407 .predio,
2408 v_listaVizinhos(i)
2409 .modulo);
2410 v_predioAtual := v_listaVizinhos(i).predio;
2411 v_predioPosterior := v_listaVizinhos(v_posterior).predio;
2412 if v_predioAtual = (v_predioPosterior - 2) then
2413 v_predioValido := true;
2414 else
2415 v_predioValido := false;
2416 end if;
2417
2418 if v_predioValido = false then
2419 v_sair := true;
2420 end if;
2421
2422 if v_sair = false then
2423 v_peso := v_peso - v_listaVizinhos(i).pesoMaximo;
2424 v_cubagem := v_cubagem - (v_listaVizinhos(i)
2425 .altura * v_listaVizinhos(i).largura * v_listaVizinhos(i)
2426 .comprimento);
2427 v_listaIrmaos.Extend;
2428 v_listaIrmaos(d).idlocal := v_listaVizinhos(i).idlocal;
2429 v_listaIrmaos(d).idarmazempai := v_listaVizinhos(i).idarmazem;
2430 d := d + 1;
2431 v_posterior := i;
2432 end if;
2433
2434 if (v_inicioModulo = true)
2435 and (v_sair = true) then
2436 v_sair := true;
2437 end if;
2438
2439 if ((v_peso <= 0) and (v_cubagem <= 0) and v_sair) then
2440 v_sair := true;
2441 end if;
2442 i := i - 1;
2443 if (i < 0)
2444 or (v_sair = true) then
2445 v_sair := true;
2446 end if;
2447 end loop;
2448 end if;
2449
2450 if (v_peso <= 0)
2451 and (v_cubagem <= 0) then
2452 for i in 0 .. v_listaIrmaos.count
2453 loop
2454 update local
2455 set idarmazempai = v_listaIrmaos(i).idarmazempai,
2456 idlocalpai = v_listaIrmaos(i).idlocalPai
2457 where idarmazem = p_idArmazem
2458 and idLocal = p_idlocal;
2459 end loop;
2460 v_peso := 0;
2461 v_cubagem := 0;
2462 end if;
2463 v_retorno.peso := v_peso;
2464 v_retorno.cubagem := v_cubagem;
2465 return v_retorno;
2466 end;
2467
2468 /*
2469 * Limpa os endereços filhos de alocação multiendereço
2470 */
2471 procedure RemoveInfMultiEndereco(p_idremanejamento in remanejamento.idremanejamento%type) is
2472 cursor c_remanejamento(p_idremanejamento in remanejamento.idremanejamento%type) is
2473 select re.idarmazemorigem, re.idlocalorigem
2474 from remanejamento re
2475 where re.idremanejamento = p_idremanejamento;
2476
2477 r_remanejamento c_remanejamento%rowtype;
2478 begin
2479 if c_remanejamento%isopen then
2480 close c_remanejamento;
2481 end if;
2482 open c_remanejamento(p_idremanejamento);
2483 fetch c_remanejamento
2484 into r_remanejamento;
2485 close c_remanejamento;
2486
2487 for c_local in (select l.idarmazem, l.idlocal
2488 from local l, setor s, armazem a
2489 where (l.idarmazempai, l.idlocalpai) =
2490 (select r.idarmazemorigem, r.idlocalorigem
2491 from remanejamento r
2492 where r.idremanejamento = p_idremanejamento
2493 and (r.idarmazemorigem, r.idlocalorigem) not in
2494 (select ll.idarmazem, ll.idlocal
2495 from lotelocal ll, lote l, produto p
2496 where ll.idarmazem = r.idarmazemorigem
2497 and ll.idlocal = r.idlocalorigem
2498 and l.idlote = ll.idlote
2499 and p.idproduto = l.idproduto
2500 and p.multiendereco = c_multiendereco
2501 and l.tipolote = c_lote
2502 and ll.estoque - ll.pendencia +
2503 ll.adicionar > 0))
2504 and s.idsetor(+) = l.idsetor
2505 and a.idarmazem = l.idarmazem)
2506 loop
2507 update local
2508 set idarmazempai = null,
2509 idlocalpai = null
2510 where idarmazem = r_remanejamento.idarmazemorigem
2511 and idlocal = r_remanejamento.idlocalorigem;
2512 end loop;
2513 end;
2514
2515 /*
2516 * Valida se o lote e componente de montagem
2517 */
2518 function CtrlValManipCompMont(p_idlote in lote.idlote%type) return boolean is
2519 v_Resultado number;
2520 begin
2521 select decode(idlotemont, null, 0, 1)
2522 into v_resultado
2523 from lote
2524 where idlote = p_idlote;
2525
2526 Return v_Resultado = 0;
2527 end;
2528
2529 /*
2530 * Inclui no remanejamento os lotes componentes da montagem
2531 */
2532 procedure Associar
2533 (
2534 p_idRemanejamento in remanejamento.idremanejamento%type,
2535 p_idlote in lote.idlote%type
2536 ) is
2537
2538 begin
2539 insert into loteremanejamento
2540 (idRemanejamento, idLote, qtde, conferido, controlaQtde)
2541 select p_idRemanejamento, l.idlote, l.qtdeentrada, 'S', 'N'
2542 from lote l
2543 where l.idlotemont = p_idlote;
2544 end;
2545
2546 /*
2547 * Excluir do remanejamento os lotes componentes da montagem
2548 */
2549 procedure Desassociar
2550 (
2551 p_idRemanejamento in remanejamento.idremanejamento%type,
2552 p_idlote in lote.idlote%type
2553 ) is
2554 v_msg t_message;
2555 begin
2556 if not CtrlValManipCompMont(p_idlote) then
2557 v_msg := t_message('Lote componente de uma montagem não pode ser desassociado de um remanejamento. Selecione o lote principal da montagem.');
2558 raise_application_error(-20000, v_msg.formatMessage);
2559 end if;
2560
2561 for c_comps in (select idlote
2562 from lote
2563 where idlotemont = p_idlote)
2564 loop
2565 delete from loteremanejamento
2566 where idlote = c_comps.idlote
2567 and idremanejamento = p_idRemanejamento;
2568 end loop;
2569 end;
2570
2571 procedure validarFormarLoteUnico
2572 (
2573 p_idlote number,
2574 p_idremanejamento number,
2575 p_idlocaldestino local.idlocal%type
2576 ) is
2577 r_loteunicocomparar pk_lote.t_loteunico;
2578 v_idarmazem number;
2579 v_msgerro varchar2(5000);
2580 v_msg t_message;
2581 begin
2582
2583 select lt.idarmazem
2584 into v_idarmazem
2585 from lote lt
2586 where lt.idlote = p_idlote;
2587
2588 select lt.idproduto, lt.estado, lt.descr, lt.dtvenc,
2589 pd.loteuniconoendereco, lt.iddepositante
2590 into r_loteunicocomparar
2591 from lote lt, produtodepositante pd
2592 where lt.idlote = p_idlote
2593 and pd.idproduto = lt.idproduto
2594 and pd.identidade = lt.iddepositante;
2595
2596 for r_loteunico in (select distinct lt.idproduto, lt.estado,
2597 lt.descr loteindustria,
2598 lt.dtvenc dtvencimento,
2599 pd.loteuniconoendereco,
2600 lt.iddepositante
2601 from loteremanejamento lr, lote lt,
2602 produtodepositante pd
2603 where lr.idremanejamento = p_idremanejamento
2604 and lr.idlote = lt.idlote
2605 and lt.idlote = lr.idlote
2606 and pd.idproduto = lt.idproduto
2607 and pd.identidade = lt.iddepositante)
2608 loop
2609
2610 for r_loteunicoComp in (select distinct lt.idproduto, lt.estado,
2611 lt.descr loteindustria,
2612 lt.dtvenc dtvencimento,
2613 pd.loteuniconoendereco,
2614 lt.iddepositante
2615 from loteremanejamento lr, lote lt,
2616 produtodepositante pd
2617 where lr.idremanejamento = p_idremanejamento
2618 and lr.idlote = lt.idlote
2619 and lt.idlote = lr.idlote
2620 and pd.idproduto = lt.idproduto
2621 and pd.identidade = lt.iddepositante
2622 and (lt.idproduto <> r_loteunico.idproduto or
2623 lt.estado <> r_loteunico.estado or
2624 lt.descr <> r_loteunico.loteindustria or
2625 lt.dtvenc <> r_loteunico.dtvencimento or
2626 pd.loteuniconoendereco <>
2627 r_loteunico.loteuniconoendereco))
2628 loop
2629 if not pk_lote.isLoteUnicoCompativel(r_loteunico, r_loteunicoComp,
2630 v_msgerro) then
2631 raise_application_error(-20000, v_msgerro);
2632 end if;
2633 end loop;
2634 end loop;
2635
2636 if (not
2637 pk_lote.isLocalPodeReceberLoteUnico(r_loteunicocomparar, v_idarmazem,
2638 p_idlocaldestino, v_msgerro)) then
2639 raise_application_error(-20000, v_msgerro);
2640 end if;
2641
2642 end;
2643
2644 /*
2645 * Valida possibilidade de geracao de lotes no remanejamento de picking para pulmao
2646 */
2647 function ValidarGeracaoLotes
2648 (
2649 p_idarmazem in number,
2650 p_idlocal local.idlocal%type,
2651 p_idproduto in number,
2652 p_iddepositante in number,
2653 p_estado lote.estado%type,
2654 p_fator in number,
2655 p_qtde in number,
2656 p_itemadicional in lote.itemadicional%type,
2657 p_mensagem out varchar2,
2658 p_idlote in number
2659 ) return boolean is
2660
2661 v_qtdedisp number;
2662 v_msg t_message;
2663 begin
2664 if p_qtde <= 0 then
2665 v_msg := t_message('Quantidade de lotes a ser gerado não pode ser zero.');
2666 p_mensagem := v_msg.formatMessage;
2667 else
2668 select sum(ll.estoque - ll.pendencia + ll.adicionar) disp
2669 into v_qtdedisp
2670 from lotelocal ll, lote l
2671 where l.idproduto = p_idproduto
2672 and l.estado = p_estado
2673 and l.idlote = ll.idlote
2674 and ll.idarmazem = p_idarmazem
2675 and ll.idlocal = p_idlocal
2676 and l.liberado = 'S'
2677 and l.itemadicional = p_itemadicional
2678 and decode(p_idlote, 0, 0, ll.idlote) = p_idlote
2679 and ((ll.estoque > 0) or (ll.adicionar > 0) or (ll.pendencia > 0) or
2680 (ll.reservado > 0));
2681
2682 if nvl(v_qtdedisp, 0) < (p_qtde * p_fator) then
2683 v_msg := t_message('Quantidade disponÃvel no endereço não atende a quantidade a ser remanejada.');
2684 p_mensagem := v_msg.formatMessage;
2685 end if;
2686
2687 end if;
2688
2689 if length(p_mensagem) > 0 then
2690 return false;
2691 else
2692 return true;
2693 end if;
2694 end;
2695
2696 procedure InserirLoteRemanejamento
2697 (
2698 p_idremanejamento in number,
2699 p_idarmazem in number,
2700 p_idlocal local.idlocal%type,
2701 p_idusuario in number,
2702 p_idlote in number,
2703 p_qtde in number
2704 ) is
2705 v_complementar lotelocal.complementar%type;
2706 begin
2707 insert into loteremanejamento
2708 (idremanejamento, idlote, qtde, conferido, controlaqtde, diferenca)
2709 values
2710 (p_idremanejamento, p_idlote, p_qtde, c_nao, c_sim, 0);
2711
2712 -- Inserir adicionar e pendenica no lotes novos.
2713 v_complementar := 'AUMENTADO ADICIONAR EM FUNCAO DA INCLUSAO DO LOTE NO REMANEJAMENTO N: ' ||
2714 p_idremanejamento;
2715 pk_estoque.incluir_adicionar(p_idarmazem, p_idlocal, p_idlote, p_qtde,
2716 p_idusuario, v_complementar);
2717
2718 v_complementar := 'AUMENTADO PENDENCIA EM FUNCAO DA INCLUSAO DO LOTE NO REMANEJAMENTO N: ' ||
2719 p_idremanejamento;
2720 pk_estoque.incluir_pendencia(p_idarmazem, p_idlocal, p_idlote, p_qtde,
2721 p_idusuario, v_complementar);
2722 end;
2723
2724 procedure InserirComposicaoLote
2725 (
2726 p_idremanejamento in number,
2727 p_idlote in number,
2728 p_qtde in number,
2729 p_qtdecoberta in number
2730 ) is
2731 cursor c_lotes(p_idremanejamento number) is
2732 select idremanejamento, idlote, qtde, disp
2733 from (select lr.idremanejamento, lr.idlote, lr.qtde,
2734 lr.qtde -
2735 nvl((select sum(cl.qtde) qtde
2736 from composicaolote cl
2737 where cl.idlotenovo = lr.idlote), 0) disp
2738 from loteremanejamento lr
2739 where lr.idremanejamento = p_idremanejamento)
2740 where disp > 0
2741 order by idlote;
2742
2743 C_REMANJ_PK_PL constant number := 3;
2744
2745 r_lotes c_lotes%rowtype;
2746 v_qtde number;
2747 v_baixado number;
2748 v_qtdecoberta number;
2749 v_cobertura number;
2750 begin
2751 if (c_lotes%isopen) then
2752 close c_lotes;
2753 end if;
2754
2755 open c_lotes(p_idremanejamento);
2756 fetch c_lotes
2757 into r_lotes;
2758
2759 v_baixado := p_qtde;
2760 v_cobertura := p_qtdecoberta;
2761
2762 while (v_baixado > 0)
2763 loop
2764 if (r_lotes.disp >= v_baixado) then
2765 v_qtde := v_baixado;
2766 else
2767 v_qtde := r_lotes.disp;
2768 end if;
2769
2770 v_baixado := v_baixado - v_qtde;
2771
2772 if (v_qtde >= v_cobertura) then
2773 v_qtdecoberta := v_cobertura;
2774 v_cobertura := 0;
2775 else
2776 v_qtdecoberta := v_qtde;
2777 v_cobertura := v_cobertura - v_qtde;
2778 end if;
2779
2780 -- insere o composicao do lote para rastreabilidade de cobertura
2781 pk_lote.inserirComposicaoLote(r_lotes.idlote, p_idlote, v_qtde,
2782 v_qtdecoberta, C_REMANJ_PK_PL,
2783 p_idremanejamento);
2784
2785 pk_lote.inserirComposicaoLoteKit(p_idlote, r_lotes.idlote, v_qtde);
2786
2787 pk_lote.inserirBarraOriginal(r_lotes.idlote);
2788 -- adiciona quantidade disponivel para o lote formado no inventario
2789 update lote
2790 set qtdedisponivel = nvl(qtdedisponivel, 0) + v_qtdecoberta
2791 where idlote = r_lotes.idlote;
2792
2793 -- retira quantidade disponivel do lote baixado no remanejamento
2794 update lote
2795 set qtdedisponivel = nvl(qtdedisponivel, 0) - v_qtdecoberta
2796 where idlote = p_idlote;
2797
2798 fetch c_lotes
2799 into r_lotes;
2800 end loop;
2801 end;
2802
2803 procedure GerarDocumentos
2804 (
2805 p_idremanejamento in number,
2806 p_idusuario in number,
2807 p_iddepositante in number,
2808 p_mensagem out varchar2
2809 ) is
2810 r_notafiscal notafiscal%rowtype;
2811 r_nfdet nfdet%rowtype;
2812 v_idproprietario number;
2813 v_idnfsaida number;
2814 v_idnfentrada number;
2815 v_msg t_message;
2816 begin
2817 begin
2818 select c.idproprietario
2819 into v_idproprietario
2820 from configuracao c
2821 where c.ativo = 'S';
2822 exception
2823 when no_data_found then
2824 v_msg := t_message('Proprietario não foi localizado.');
2825 p_mensagem := v_msg.formatMessage;
2826 end;
2827 if p_mensagem is null then
2828 -- insere o cabecalho da NF de saida
2829 r_notafiscal.codigointerno := p_idremanejamento;
2830 r_notafiscal.sequencia := 'AVARIA';
2831 r_notafiscal.usuario := p_idusuario;
2832 r_notafiscal.remetente := v_idproprietario;
2833 r_notafiscal.destinatario := v_idproprietario;
2834 r_notafiscal.dataemissao := sysdate;
2835 r_notafiscal.datacadastro := sysdate;
2836 r_notafiscal.statusnf := 'P';
2837 r_notafiscal.estoqueverificado := 'S';
2838 r_notafiscal.statusroteirizacao := 2;
2839 r_notafiscal.digitada := 'N';
2840 r_notafiscal.impresso := 'N';
2841 r_notafiscal.ident_entrega := v_idproprietario;
2842 r_notafiscal.movestoque := 'S';
2843 r_notafiscal.iddepositante := p_iddepositante;
2844 --r_notafiscal.operacao := r_nf.idoperacao; --????????????????? Ver com Leandro
2845
2846 --Inserir NF de Saida
2847 r_notafiscal.tipo := 'S';
2848 r_notafiscal.estado := 'N';
2849 v_idnfsaida := pk_notafiscal.insere_cabecalho_nf(r_notafiscal);
2850
2851 --Inserir NF de Entrada
2852 r_notafiscal.tipo := 'E';
2853 r_notafiscal.estado := 'T';
2854 v_idnfentrada := pk_notafiscal.insere_cabecalho_nf(r_notafiscal);
2855
2856 for c_itens in (select l.idproduto, l.barra,
2857 sum(lr.qtde / e.fatorconversao) qtde
2858 from loteremanejamento lr, lote l, embalagem e
2859 where e.barra = l.barra
2860 and e.idproduto = l.idproduto
2861 and l.idlote = lr.idlote
2862 and lr.idremanejamento = p_idremanejamento
2863 group by l.idproduto, l.barra)
2864 loop
2865 r_nfdet.idnfdet := null;
2866 r_nfdet.nf := v_idnfsaida;
2867 r_nfdet.idproduto := c_itens.idproduto;
2868 r_nfdet.barra := c_itens.barra;
2869 r_nfdet.qtde := c_itens.qtde;
2870 pk_notafiscal.insere_detalhe_nf(r_nfdet);
2871
2872 r_nfdet.idnfdet := null;
2873 r_nfdet.nf := v_idnfentrada;
2874 pk_notafiscal.insere_detalhe_nf(r_nfdet);
2875 end loop;
2876 pk_notafiscal.p_gerar_nfimpressao(v_idnfsaida, 'N');
2877 pk_notafiscal.p_gerar_nfimpressao(v_idnfentrada, 'N');
2878
2879 end if;
2880 end;
2881
2882 /*
2883 * Rotina que pesa os lotes do remanejamento
2884 */
2885 procedure PesarLotes
2886 (
2887 p_idremanejamento in number,
2888 p_idlote in number,
2889 p_peso in number,
2890 p_mensagem out varchar2
2891 ) is
2892
2893 cursor c_pesocalc
2894 (
2895 p_idremanejamento in number,
2896 p_idlote in number
2897 ) is
2898 select (e.pesobruto * lr.qtde) + nvl(tc.peso, 0) peso
2899 from loteremanejamento lr, lote l, embalagem e, tipocaixavolume tc
2900 where e.barra =
2901 pk_produto.retornar_codbarra_nao_precad(l.idproduto, 1)
2902 and e.idproduto = l.idproduto
2903 and l.idlote = lr.idlote
2904 and tc.idtipocaixavolume(+) = l.idtipocaixa
2905 and lr.idlote = p_idlote
2906 and lr.idremanejamento = p_idremanejamento;
2907
2908 cursor c_tolerancia(p_peso in number) is
2909 select ep.percentual
2910 from escalapesagem ep
2911 where (p_peso) between pesoinicial and pesofinal
2912 and ep.tipo = 'R'
2913 order by ep.percentual;
2914
2915 v_peso number;
2916 v_tolerancia number;
2917 v_pesotolerancia number;
2918 v_difmaior number;
2919 v_difmenor number;
2920 v_erro char(1) := 'N';
2921 v_msg t_message;
2922 begin
2923 if p_peso = 0 then
2924 v_msg := t_message('O peso não pode ser zero. Operação Cancelada.');
2925 p_mensagem := v_msg.formatMessage;
2926 end if;
2927
2928 if p_mensagem is null then
2929 if c_pesocalc%isopen then
2930 close c_pesocalc;
2931 end if;
2932 open c_pesocalc(p_idremanejamento, p_idlote);
2933 fetch c_pesocalc
2934 into v_peso;
2935
2936 -- procurando uma escala de tolerancia adequada para o romaneio
2937 if c_tolerancia%isopen then
2938 close c_tolerancia;
2939 end if;
2940 open c_tolerancia(v_peso);
2941 fetch c_tolerancia
2942 into v_tolerancia;
2943
2944 if c_tolerancia%found then
2945 -- obtendo os limites de peso tolerado
2946 v_pesotolerancia := (v_tolerancia / 100) * v_peso;
2947 v_difmaior := v_peso + v_pesotolerancia;
2948 v_difmenor := v_peso - v_pesotolerancia;
2949 -- verificando se o peso da balanca esta no intervalo tolerante
2950 if (p_peso >= v_difmenor)
2951 and (p_peso <= v_difmaior) then
2952 v_erro := 'N';
2953 else
2954 v_erro := 'S';
2955 end if;
2956
2957 update loteremanejamento
2958 set liberado = decode(v_erro, 'N', 'S', 'E'),
2959 peso = p_peso,
2960 datapesagem = sysdate
2961 where idremanejamento = p_idremanejamento
2962 and idlote = p_idlote;
2963
2964 if c_pesocalc%isopen then
2965 close c_pesocalc;
2966 end if;
2967 if c_tolerancia%isopen then
2968 close c_tolerancia;
2969 end if;
2970
2971 v_msg := t_message('Lote pesado com sucesso.');
2972 p_mensagem := v_msg.formatMessage;
2973 else
2974 v_msg := t_message('Não existe uma escala de tolerancia configurada no sistema para o peso: {0}.' ||
2975 chr(13) ||
2976 'Configure a escala e tente novamente.');
2977 v_msg.addParam(p_peso);
2978 p_mensagem := v_msg.formatMessage;
2979 end if;
2980 end if;
2981 end;
2982
2983 procedure EtiquetaImpressaCorreta
2984 (
2985 p_idremanejamento in number,
2986 p_idlote in number
2987 ) is
2988 begin
2989 update loteremanejamento
2990 set impetiqueta = 'S'
2991 where idremanejamento = p_idremanejamento
2992 and idlote = p_idlote;
2993
2994 end;
2995
2996 /*
2997 * Rotina que libera lotes com divergencia do remanejamento de PK para Pulmao
2998 */
2999 procedure LiberarLotesDivergente
3000 (
3001 p_idremanejamento in number,
3002 p_idlote in number,
3003 p_idusuario in number
3004 ) is
3005 begin
3006 update loteremanejamento
3007 set liberado = 'S',
3008 idusuario = p_idusuario
3009 where idremanejamento = p_idremanejamento
3010 and idlote = p_idlote;
3011
3012 pk_utilities.GeraLog(p_idusuario,
3013 'LOTE ' || p_idlote ||
3014 ' LIBERADO COM DIVERGENCIA DE PESO NO REMANEJAMENTO DE PICKING PARA PULMAO: ' ||
3015 p_idremanejamento, p_idlote, 'R');
3016 end;
3017
3018 /*
3019 * Rotina que cancela cadastramento de lotes
3020 */
3021 procedure CancelarGerarLotes
3022 (
3023 p_idremanejamento in number,
3024 p_idusuario in number
3025 ) is
3026 begin
3027 rollback;
3028 pk_utilities.GeraLog(p_idusuario,
3029 'CANCELADO O CADASTRO DE LOTES DO REMANEJAMENTO: ' ||
3030 p_idremanejamento, p_idremanejamento, 'R');
3031 end;
3032
3033 /*
3034 * Rotina que finaliza o cadastramento de Lotes
3035 */
3036 procedure FinalizarGerarLotes
3037 (
3038 p_idremanejamento in number,
3039 p_idusuario in number,
3040 p_mensagem out varchar2,
3041 p_finalizar out number,
3042 p_commit in char := 'S'
3043 ) is
3044 cursor c_loteslib(p_idremanejamento in number) is
3045 select count(*) qtdelotes
3046 from loteremanejamento
3047 where idremanejamento = p_idremanejamento
3048 and (liberado = 'N' or impetiqueta = 'N');
3049
3050 r_loteslib c_loteslib%rowtype;
3051 v_complementar lotelocal.complementar%type;
3052 v_msg t_message;
3053
3054 function temInfEspecRem(p_idremanejamento in number) return boolean is
3055 v_qtdeInfEspRem number;
3056 begin
3057 select count(1)
3058 into v_qtdeInfEspRem
3059 from loteremanejamento lr, lote l, informacaomatdep imd
3060 where lr.idremanejamento = p_idremanejamento
3061 and l.idlote = lr.idlote
3062 and imd.idproduto = l.idproduto
3063 and imd.identidade = l.iddepositante;
3064
3065 return v_qtdeInfEspRem > 0;
3066 end;
3067 begin
3068 if not temInfEspecRem(p_idremanejamento) then
3069 if c_loteslib%isopen then
3070 close c_loteslib;
3071 end if;
3072 open c_loteslib(p_idremanejamento);
3073 fetch c_loteslib
3074 into r_loteslib;
3075 if r_loteslib.qtdelotes > 0 then
3076 v_msg := t_message('Existem Lotes que não foram liberados ou etiquetas que não foram impressas.' ||
3077 CHR(13) ||
3078 'Cadastramento não pode ser finalizado. Operação Cancelada.');
3079 p_mensagem := v_msg.formatMessage;
3080 p_finalizar := 0;
3081
3082 else
3083 for c_lotegerados in (select r.idarmazemorigem, r.idlocalorigem,
3084 r.idarmazemdestino, r.idlocaldestino,
3085 lr.idlote, lr.qtde, lt.idproduto,
3086 lt.iddepositante
3087 from loteremanejamento lr, remanejamento r,
3088 lote lt
3089 where r.idremanejamento = lr.idremanejamento
3090 and lr.idremanejamento = p_idremanejamento
3091 and lt.idlote = lr.idlote)
3092 loop
3093 -- Passa Adicionar para o estoque dos lotes criados que estão na origem
3094 v_complementar := 'ESTOQUE ADICIONADO EM FUNCAO DA FINALIZACAO DO CADASTRO DE LOTE DO REMANEJAMENTO N:' ||
3095 p_idremanejamento;
3096
3097 pk_picking_dinamico.addGttPickingDinamico(c_lotegerados.idproduto,
3098 c_lotegerados.iddepositante,
3099 c_lotegerados.idlocaldestino,
3100 c_lotegerados.idarmazemdestino,
3101 1);
3102
3103 pk_estoque.incluir_estoque(c_lotegerados.idarmazemorigem,
3104 c_lotegerados.idlocalorigem,
3105 c_lotegerados.idlote,
3106 c_lotegerados.qtde, p_idusuario,
3107 v_complementar, 'N');
3108
3109 v_complementar := 'ADICIONAR RETIRADO EM FUNCAO DA FINALIZACAO DO CADASTRO DE LOTE DO REMANEJAMENTO N:' ||
3110 p_idremanejamento;
3111 pk_estoque.retirar_adicionar(c_lotegerados.idarmazemorigem,
3112 c_lotegerados.idlocalorigem,
3113 c_lotegerados.idlote,
3114 c_lotegerados.qtde, p_idusuario,
3115 v_complementar);
3116
3117 -- Incluir lotes criados no destino com adicionar
3118 v_complementar := 'AUMENTADO ADICIONAR EM FUNCAO DA FINALIZACAO DO CADASTRO DE LOTE DO REMANEJAMENTO N: ' ||
3119 p_idremanejamento;
3120 pk_estoque.incluir_adicionar(c_lotegerados.idarmazemdestino,
3121 c_lotegerados.idlocaldestino,
3122 c_lotegerados.idlote,
3123 c_lotegerados.qtde, p_idusuario,
3124 v_complementar);
3125
3126 end loop;
3127
3128 pk_picking_dinamico.inserir_picking_dinamico;
3129
3130 for c_lotes in (select r.idarmazemorigem idarmazem,
3131 r.idlocalorigem idlocal,
3132 lo.tipo tipoLocalOrigem,
3133 lo.buffer BufferOrigem,
3134 c.idloteanterior idlote, lt.idproduto,
3135 lt.iddepositante, sum(c.qtde) qtdependencia
3136 from remanejamento r, loteremanejamento lr,
3137 composicaolote c, local lo, lote lt
3138 where r.idremanejamento = p_idremanejamento
3139 and lr.idremanejamento = r.idremanejamento
3140 and c.idlotenovo = lr.idlote
3141 and lo.idlocal = r.idlocalorigem
3142 and lo.idarmazem = r.idarmazemorigem
3143 and lt.idlote = lr.idlote
3144 group by r.idarmazemorigem, r.idlocalorigem, lo.tipo,
3145 lo.buffer, c.idloteanterior, lt.idproduto,
3146 lt.iddepositante)
3147 loop
3148
3149 -- Subtrair pendencia do estoque dos lotes que estão na origem
3150 v_complementar := 'PENDENCIA RETIRADA EM FUNCAO DA FINALIZACAO DO CADASTRO DE LOTE DO REMANEJAMENTO N:' ||
3151 p_idremanejamento;
3152
3153 pk_estoque.retirar_pendencia(c_lotes.idarmazem, c_lotes.idlocal,
3154 c_lotes.idlote, c_lotes.qtdependencia,
3155 p_idusuario, v_complementar);
3156 v_complementar := 'ESTOQUE RETIRADO EM FUNCAO DA FINALIZACAO DO CADASTRO DE LOTE DO REMANEJAMENTO N:' ||
3157 p_idremanejamento;
3158
3159 pk_estoque.retirar_estoque(c_lotes.idarmazem, c_lotes.idlocal,
3160 c_lotes.idlote, c_lotes.qtdependencia,
3161 p_idusuario, v_complementar, 'N');
3162
3163 if ((c_lotes.tipoLocalOrigem = 0) AND
3164 (c_lotes.BufferOrigem = 'N')) then
3165 pk_picking_dinamico.addGttPickingDinamico(c_lotes.idproduto,
3166 c_lotes.iddepositante,
3167 c_lotes.idlocal,
3168 c_lotes.idarmazem, 0);
3169 end if;
3170 end loop;
3171
3172 pk_picking_dinamico.deletar_picking_dinamico;
3173
3174 update remanejamento
3175 set planejado = 'S'
3176 where idremanejamento = p_idremanejamento;
3177
3178 if p_commit = 'S' then
3179 commit;
3180 end if;
3181
3182 v_msg := t_message('Formação de lotes finalizada com sucesso.');
3183 p_mensagem := v_msg.formatMessage;
3184 p_finalizar := 1;
3185 end if;
3186 else
3187 update remanejamento
3188 set planejado = 'S'
3189 where idremanejamento = p_idremanejamento;
3190
3191 if p_commit = 'S' then
3192 commit;
3193 end if;
3194
3195 v_msg := t_message('Formação de lotes finalizada com sucesso.');
3196 p_mensagem := v_msg.formatMessage;
3197 p_finalizar := 1;
3198 end if;
3199 end;
3200
3201 /*
3202 * Retornar consulta de reabastecimentos
3203 */
3204 function ConsultaLoteReab
3205 (
3206 p_idremanejamento in number,
3207 p_idlote in number default 0
3208 ) return varchar2 is
3209 v_idLote varchar2(4000);
3210 v_lotesIndustria varchar2(4000);
3211 begin
3212 if p_idlote = 0 then
3213 v_idLote := '';
3214 for c_id in (select lr.idlote
3215 from loteremanejamento lr
3216 where lr.idremanejamento = p_idremanejamento)
3217 loop
3218 if length(trim(v_idLote)) > 0 then
3219 v_idLote := substr(v_idLote || ',' || to_char(c_id.idlote), 1,
3220 4000);
3221 else
3222 v_idLote := substr(to_char(c_id.idlote), 1, 4000);
3223 end if;
3224 end loop;
3225
3226 return v_idLote;
3227 else
3228 v_lotesIndustria := '';
3229 for c_li in (select distinct l.descr loteIndustria
3230 from loteremanejamento lr, lote l
3231 where lr.idremanejamento = p_idremanejamento
3232 and lr.idlote = p_idlote
3233 and lr.idlote = l.idlote)
3234 loop
3235 if length(trim(v_lotesIndustria)) > 0 then
3236 v_lotesIndustria := substr(v_lotesIndustria || ',' ||
3237 to_char(c_li.loteIndustria), 1, 4000);
3238 else
3239 v_lotesIndustria := substr(to_char(c_li.loteIndustria), 1, 4000);
3240 end if;
3241 end loop;
3242
3243 return v_lotesIndustria;
3244 end if;
3245 end;
3246
3247 /*
3248 * Rotina que gera etiquetas de remanejamento
3249 */
3250 procedure GeraEtiqRemanejamento is
3251
3252 v_cont number := 1;
3253 begin
3254 delete from gtt_geraetiqremanej;
3255
3256 for c_etq in (select lr.idremanejamento,
3257 sum(lr.qtde / e.fatorconversao) qtdeetiq
3258 from loteremanejamento lr, lote l, embalagem e,
3259 tipoembalagem te
3260 where te.idtipoembalagem(+) = e.idtipoembalagem
3261 and e.barra = l.barra
3262 and e.idproduto = l.idproduto
3263 and l.idlote = lr.idlote
3264 and lr.idremanejamento in
3265 (select idselecionado
3266 from gtt_selecao)
3267 group by lr.idremanejamento, l.idproduto)
3268 loop
3269 while (v_cont <= c_etq.qtdeetiq)
3270 loop
3271
3272 insert into gtt_geraetiqremanej
3273 (idetiqremanej, idremanejamento)
3274 values
3275 (seq_gttgeraetiqremanej.nextval, c_etq.idremanejamento);
3276
3277 v_cont := v_cont + 1;
3278
3279 end loop;
3280 v_cont := 1;
3281 end loop;
3282 end;
3283
3284 -- Refactored procedure validarCubagemMudancaSetor
3285 procedure validarCubagemMudancaSetor
3286 (
3287 p_idusuario in number,
3288 p_idproduto in number,
3289 p_idarmazem in number,
3290 p_idlocal in local.idlocal%type,
3291 p_qtdelotes in out number,
3292 p_qtdecoube in out number
3293 ) is
3294 v_coube boolean;
3295 v_tipoerro number;
3296 begin
3297 p_qtdelotes := 0;
3298 p_qtdecoube := 0;
3299
3300 for c_lote in (select ll.idlote, lt.barra,
3301 (ll.estoque - ll.pendencia + ll.adicionar) disponivel
3302 from lotelocal ll, lote lt, usuariodepositante ud
3303 where ll.idarmazem = p_idarmazem
3304 and ll.idlocal = p_idlocal
3305 and ll.estoque - ll.pendencia + ll.adicionar > 0
3306 and lt.idlote = ll.idlote
3307 and lt.idproduto = p_idproduto
3308 and ud.identidade = lt.iddepositante
3309 and ud.idusuario = p_idusuario
3310 and lt.idlotemont is null)
3311 loop
3312 p_qtdelotes := p_qtdelotes + 1;
3313 v_coube := CtrlValidarRestriFisicas(p_idproduto, c_lote.disponivel,
3314 p_idarmazem, p_idlocal,
3315 c_lote.barra, v_tipoerro);
3316
3317 if v_coube then
3318 p_qtdecoube := p_qtdecoube + 1;
3319 insert into gtt_produtoremanejamento
3320 (idproduto, barra, qtde)
3321 values
3322 (p_idproduto, c_lote.barra, c_lote.disponivel);
3323 else
3324 delete from gtt_produtoremanejamento;
3325 exit when not v_coube;
3326 end if;
3327 end loop;
3328 end validarCubagemMudancaSetor;
3329
3330 /*
3331 * Localiza um setor com prioridade 1 que seja do tipo compra ou devolucao
3332 */
3333 procedure getSetorPrioridadeUm
3334 (
3335 p_idusuario in number,
3336 p_idproduto in number,
3337 p_idsetor in out number
3338 ) is
3339 v_msg t_message;
3340 begin
3341 begin
3342 select sp.idsetor
3343 into p_idsetor
3344 from setor s, setorproduto sp, setordepositante sd,
3345 usuariodepositante ud
3346 where s.ativo = 'S'
3347 and sp.idsetor = s.idsetor
3348 and sp.idproduto = p_idproduto
3349 and sp.prioridade = 1
3350 and sd.idsetor = s.idsetor
3351 and ud.identidade = sd.iddepositante
3352 and ud.idusuario = p_idusuario
3353 and exists
3354 (select 1
3355 from setorrecebimento sr, tiporecebimento tr
3356 where sr.idsetor = s.idsetor
3357 and sr.idtiporecebimento = tr.idtiporecebimento
3358 and tr.classificacao in ('C', 'D', 'T'))
3359 and rownum = 1;
3360 exception
3361 when no_data_found then
3362 v_msg := t_message('NENHUM SETOR ATIVO COM PRIORIDADE 1 QUE POSSUA TIPO DE RECEBIMENTO "COMPRA", OU "DEVOLUCAO" DEFINIDO PARA OS DEPOSITANTES DO USUARIO ID ' ||
3363 '{0}, PRODUTO {1}.');
3364 v_msg.addParam(p_idusuario);
3365 v_msg.addParam(p_idproduto);
3366 raise_application_error(-20000, v_msg.formatMessage);
3367 end;
3368 end getSetorPrioridadeUm;
3369
3370 -- Refactored procedure CtrlPickingParaRemMudSetor
3371 procedure CtrlPickingParaRemMudSetor
3372 (
3373 p_idproduto in number,
3374 p_idarmazem in number,
3375 p_idlocaldestino in local.idlocal%type,
3376 p_iddepositante in number,
3377 p_idendereco in number,
3378 p_idlocalorigem in local.idlocal%type
3379 ) is
3380 v_tipodestino number;
3381 v_idpicking number;
3382 begin
3383 select l.tipo
3384 into v_tipodestino
3385 from local l
3386 where l.idlocal = p_idlocaldestino
3387 and l.idarmazem = p_idarmazem;
3388
3389 delete from produtolocal pl
3390 where pl.idproduto = p_idproduto
3391 and pl.idarmazem = p_idarmazem
3392 and pl.idlocal = p_idlocalorigem
3393 and pl.identidade = p_iddepositante;
3394
3395 if v_tipodestino = 0 then
3396 begin
3397 select pl.idprodutolocal
3398 into v_idpicking
3399 from produtolocal pl
3400 where pl.idproduto = p_idproduto
3401 and pl.idarmazem = p_idarmazem
3402 and pl.idlocal = p_idlocaldestino
3403 and pl.identidade = p_iddepositante;
3404 exception
3405 when no_data_found then
3406 pk_armazem.criarPickingProduto(p_idarmazem, p_idlocaldestino,
3407 p_iddepositante, p_idproduto);
3408 end;
3409 end if;
3410 end CtrlPickingParaRemMudSetor;
3411
3412 /*
3413 * Rotina responsavel por gerar o remanejamento por motivo de mudança de setor.
3414 */
3415 function NovoRemPorMudancaSetor
3416 (
3417 p_idusuario in number,
3418 p_idproduto in number,
3419 p_idarmazem in number,
3420 p_idlocalorigem in local.idlocal%type
3421 ) return number is
3422 v_idlocaldestino local.idlocal%type;
3423 v_idremanejamento number;
3424 v_idsetor number;
3425 v_bufferorigem char;
3426 v_tipoorigem number;
3427 v_qtdelotes number;
3428 v_qtdecoube number;
3429 v_prioridade number;
3430 v_totaldisponivel number;
3431 v_msg t_message;
3432 begin
3433 getSetorPrioridadeUm(p_idusuario, p_idproduto, v_idsetor);
3434
3435 begin
3436 select l.buffer, l.tipo, sp.prioridade
3437 into v_bufferorigem, v_tipoorigem, v_prioridade
3438 from local l, setorproduto sp
3439 where l.idlocal = p_idlocalorigem
3440 and l.idarmazem = p_idarmazem
3441 and sp.idsetor = l.idsetor
3442 and sp.idproduto = p_idproduto;
3443
3444 if (v_tipoorigem not in (0, 1, 2) or v_bufferorigem = 'S') then
3445 v_msg := t_message('O LOCAL DE ORIGEM {0}' ||
3446 ' DEVE SER UM LOCAL DO TIPO PULMAO OU PICKING');
3447 v_msg.addParam(p_idlocalorigem);
3448 raise_application_error(-20000, v_msg.formatMessage);
3449 end if;
3450
3451 if (v_prioridade <> 10) then
3452 v_msg := t_message('O LOCAL DE ORIGEM {0}' ||
3453 ' ESTA EM UM SETOR COM PRIORIDADE {1}' ||
3454 '. O CORRETO É O LOCAL ESTAR EM UM SETOR COM PRIORIDADE 10.');
3455 v_msg.addParam(p_idlocalorigem);
3456 v_msg.addParam(v_prioridade);
3457 raise_application_error(-20000, v_msg.formatMessage);
3458 end if;
3459 exception
3460 when no_data_found then
3461 v_msg := t_message('LOCAL {0} PARA O ARMAZEM {1} NÃO ENCONTRADO.');
3462 v_msg.addParam(p_idlocalorigem);
3463 v_msg.addParam(p_idarmazem);
3464 raise_application_error(-20000, v_msg.formatMessage);
3465 end;
3466
3467 begin
3468 select sum((ll.estoque - ll.pendencia + ll.adicionar)) total
3469 into v_totaldisponivel
3470 from lotelocal ll, lote lt, usuariodepositante ud
3471 where ll.idarmazem = p_idarmazem
3472 and ll.idlocal = p_idlocalorigem
3473 and lt.idlote = ll.idlote
3474 and lt.idproduto = p_idproduto
3475 and ud.idusuario = p_idusuario
3476 and lt.iddepositante = ud.identidade
3477 and lt.idlotemont is null;
3478 exception
3479 when no_data_found then
3480 v_msg := t_message('O LOCAL DE ORIGEM {0}' ||
3481 ' NAO POSSUI QTDE EM ESTOQUE DISPONIVEL PARA CRIAR O REMANEJAMENTO.');
3482 v_msg.addParam(p_idlocalorigem);
3483 raise_application_error(-20000, v_msg.formatMessage);
3484 end;
3485
3486 -- Aplicando 1 regra: Buscar endereco de destino onde haja estoque do produto e permita cubagem do mesmo
3487 for c_local in (select l.idlocal
3488 from lotelocal ll, lote lt, usuariodepositante ud,
3489 local l
3490 where lt.idlote = ll.idlote
3491 and lt.idproduto = p_idproduto
3492 and ud.idusuario = p_idusuario
3493 and ud.identidade = lt.iddepositante
3494 and l.idlocal = ll.idlocal
3495 and l.idarmazem = ll.idarmazem
3496 and l.idarmazem = p_idarmazem
3497 and l.idsetor = v_idsetor
3498 and l.buffer = v_bufferorigem
3499 and decode(l.tipo, 2, 1, l.tipo) =
3500 decode(v_tipoorigem, 2, 1, v_tipoorigem)
3501 and l.ativo = 'S')
3502 loop
3503 validarCubagemMudancaSetor(p_idusuario, p_idproduto, p_idarmazem,
3504 c_local.idlocal, v_qtdelotes, v_qtdecoube);
3505
3506 if v_qtdelotes = v_qtdecoube then
3507 v_idlocaldestino := c_local.idlocal;
3508 exit when v_qtdelotes = v_qtdecoube;
3509 end if;
3510 end loop;
3511
3512 -- Aplicando 2 regra: Buscar endereco de destino vazio que permita cubagem do mesmo
3513 if v_idlocaldestino is null then
3514 for c_localvazio in (select l.idlocal
3515 from local l
3516 where l.idarmazem = p_idarmazem
3517 and l.idsetor = v_idsetor
3518 and l.buffer = v_bufferorigem
3519 and decode(l.tipo, 2, 1, l.tipo) =
3520 decode(v_tipoorigem, 2, 1, v_tipoorigem)
3521 and l.ativo = 'S'
3522 and not exists
3523 (select 1
3524 from v_estoque_local
3525 where idlocal = l.idlocal
3526 and idarmazem = l.idarmazem
3527 group by idarmazem, idlocal
3528 having nvl(sum(disp), 0) > 0))
3529 loop
3530 validarCubagemMudancaSetor(p_idusuario, p_idproduto, p_idarmazem,
3531 c_localvazio.idlocal, v_qtdelotes,
3532 v_qtdecoube);
3533
3534 if v_qtdelotes = v_qtdecoube then
3535 v_idlocaldestino := c_localvazio.idlocal;
3536 exit when v_qtdelotes = v_qtdecoube;
3537 end if;
3538 end loop;
3539 end if;
3540
3541 -- Aplicando 3 regra: Buscar endereco com menor quantidade de SKU do mesmo depositante
3542 if v_idlocaldestino is null then
3543 for c_localsku in (select l.idlocal, count(distinct lt.idproduto) sku
3544 from lotelocal ll, lote lt, usuariodepositante ud,
3545 local l
3546 where lt.idlote = ll.idlote
3547 and ud.idusuario = p_idusuario
3548 and lt.iddepositante = ud.identidade
3549 and l.idlocal = ll.idlocal
3550 and l.idarmazem = ll.idarmazem
3551 and l.idarmazem = p_idarmazem
3552 and l.idsetor = v_idsetor
3553 and l.buffer = v_bufferorigem
3554 and decode(l.tipo, 2, 1, l.tipo) =
3555 decode(v_tipoorigem, 2, 1, v_tipoorigem)
3556 and l.ativo = 'S'
3557 group by l.idlocal
3558 order by sku, l.idlocal)
3559 loop
3560 validarCubagemMudancaSetor(p_idusuario, p_idproduto, p_idarmazem,
3561 c_localsku.idlocal, v_qtdelotes,
3562 v_qtdecoube);
3563
3564 if v_qtdelotes = v_qtdecoube then
3565 v_idlocaldestino := c_localsku.idlocal;
3566 exit when v_qtdelotes = v_qtdecoube;
3567 end if;
3568 end loop;
3569 end if;
3570
3571 if v_idlocaldestino is null then
3572 v_msg := t_message('NENHUM LOCAL NO ARMAZEM PERMITE ALOCAR TODA QTDE DO PRODUTO VALIDANDO A CUBAGEM.');
3573 raise_application_error(-20000, v_msg.formatMessage);
3574 end if;
3575
3576 -- Criando remanejamento
3577 v_idremanejamento := cadastrar_remanejamento(p_idarmazem, p_idarmazem,
3578 p_idlocalorigem,
3579 v_idlocaldestino,
3580 p_idusuario, null,
3581 'REMANEJAMENTO CRIADO POR MOTIVO DE ALTERACAO DE SETOR',
3582 C_NAO, C_SIM);
3583 update remanejamento r
3584 set r.tipo = 1
3585 where r.idremanejamento = v_idremanejamento;
3586
3587 for c_lote in (select ll.idlote, lt.barra,
3588 (ll.estoque - ll.pendencia + ll.adicionar) disponivel,
3589 lt.iddepositante, l.id idendereco
3590 from lotelocal ll, lote lt, usuariodepositante ud,
3591 local l
3592 where ll.idarmazem = p_idarmazem
3593 and ll.idlocal = p_idlocalorigem
3594 and ll.estoque - ll.pendencia + ll.adicionar > 0
3595 and lt.idlote = ll.idlote
3596 and lt.idproduto = p_idproduto
3597 and ud.idusuario = p_idusuario
3598 and lt.iddepositante = ud.identidade
3599 and lt.idlotemont is null
3600 and l.idlocal = ll.idlocal
3601 and l.idarmazem = ll.idarmazem)
3602 loop
3603 CtrlPickingParaRemMudSetor(p_idproduto, p_idarmazem, v_idlocaldestino,
3604 c_lote.iddepositante, c_lote.idendereco,
3605 p_idlocalorigem);
3606
3607 insert into loteremanejamento
3608 (idremanejamento, idlote, qtde, conferido, controlaqtde, diferenca)
3609 values
3610 (v_idremanejamento, c_lote.idlote, c_lote.disponivel, c_nao, c_sim,
3611 0);
3612
3613 Associar(v_idremanejamento, c_lote.idlote);
3614 end loop;
3615
3616 return v_idremanejamento;
3617 end;
3618
3619 /*
3620 * Localiza um setor com prioridade 1 que seja do tipo compra ou devolucao
3621 */
3622 procedure getSetorMaiorPrioridade
3623 (
3624 p_idusuario in number,
3625 p_idproduto in number,
3626 p_idsetor in out number
3627 ) is
3628 v_msg t_message;
3629 begin
3630 begin
3631 select idsetor
3632 into p_idsetor
3633 from (select sp.idsetor
3634 from setor s, setorproduto sp, setordepositante sd,
3635 usuariodepositante ud
3636 where s.ativo = 'S'
3637 and sp.idsetor = s.idsetor
3638 and sp.idproduto = p_idproduto
3639 and sp.prioridade = 1
3640 and sd.idsetor = s.idsetor
3641 and ud.identidade = sd.iddepositante
3642 and ud.idusuario = p_idusuario
3643 and exists
3644 (select 1
3645 from setorrecebimento sr, tiporecebimento tr
3646 where sr.idsetor = s.idsetor
3647 and sr.idtiporecebimento = tr.idtiporecebimento
3648 and tr.classificacao in ('C', 'D', 'T'))
3649 order by sp.prioridade)
3650 where rownum = 1;
3651 exception
3652 when no_data_found then
3653 v_msg := t_message('NENHUM SETOR ATIVO QUE POSSUA TIPO DE RECEBIMENTO "COMPRA", OU "DEVOLUCAO" DEFINIDO PARA OS DEPOSITANTES DO USUARIO ID ' ||
3654 '{0}, PRODUTO {1}.');
3655 v_msg.addParam(p_idusuario);
3656 v_msg.addParam(p_idproduto);
3657 raise_application_error(-20000, v_msg.formatMessage);
3658 end;
3659 end getSetorMaiorPrioridade;
3660
3661 -- Refactored procedure buscarEnderecoPulmaoVazio
3662 function buscarEnderecoPulmaoVazio
3663 (
3664 p_idusuario in number,
3665 v_encomenda in encomenda%rowtype
3666 ) return local.idlocal%type is
3667 v_qtdelotes number;
3668 v_qtdecoube number;
3669 v_idlocal local.idlocal%type := null;
3670 begin
3671 for c_localvazio in (select l.idlocal
3672 from local l, setor s, setorproduto sp
3673 where l.idarmazem = v_encomenda.idarmazem
3674 and decode(l.buffer, 'N', 0, 1) = 0
3675 and l.tipo in (1, 2)
3676 and decode(l.ativo, 'N', 0, 1) = 1
3677 and s.idsetor = l.idsetor
3678 and sp.idsetor = s.idsetor
3679 and sp.idproduto = v_encomenda.idproduto
3680 and not exists
3681 (select 1
3682 from setorrecebimento sr,
3683 tiporecebimento tr
3684 where tr.classificacao = 'E'
3685 and tr.idtiporecebimento =
3686 sr.idtiporecebimento
3687 and sr.idsetor = l.idsetor)
3688 and not exists
3689 (select 1
3690 from lotelocal ll
3691 where ll.idlocal = l.idlocal
3692 and ll.idarmazem = l.idarmazem
3693 and ll.estoque - ll.pendencia +
3694 ll.adicionar > 0)
3695 order by sp.prioridade, l.idlocal)
3696 loop
3697 validarCubagemMudancaSetor(p_idusuario, v_encomenda.idproduto,
3698 v_encomenda.idarmazem, c_localvazio.idlocal,
3699 v_qtdelotes, v_qtdecoube);
3700
3701 if v_qtdelotes = v_qtdecoube then
3702 v_idlocal := c_localvazio.idlocal;
3703 exit when v_qtdelotes = v_qtdecoube;
3704 end if;
3705 end loop;
3706
3707 return v_idlocal;
3708 end;
3709
3710 -- Refactored procedure buscarEnderecoPulmaoComEstoque
3711 function buscarEnderecoPulmaoComEstoque
3712 (
3713 p_idusuario in number,
3714 v_encomenda in encomenda%rowtype
3715 ) return local.idlocal%type is
3716 v_qtdelotes number;
3717 v_qtdecoube number;
3718 v_idlocal local.idlocal%type := null;
3719 begin
3720 for c_local in (select l.idlocal
3721 from lotelocal ll, lote lt, usuariodepositante ud,
3722 local l, setor s, setorproduto sp
3723 where lt.idlote = ll.idlote
3724 and lt.idproduto = v_encomenda.idproduto
3725 and lt.iddepositante = v_encomenda.iddepositante
3726 and ud.idusuario = p_idusuario
3727 and ud.identidade = lt.iddepositante
3728 and l.idlocal = ll.idlocal
3729 and l.idarmazem = ll.idarmazem
3730 and l.idarmazem = v_encomenda.idarmazem
3731 and decode(l.buffer, 'N', 0, 1) = 0
3732 and l.tipo in (1, 2)
3733 and decode(l.ativo, 'N', 0, 1) = 1
3734 and ll.estoque - ll.pendencia + ll.adicionar > 0
3735 and s.idsetor = l.idsetor
3736 and sp.idsetor = s.idsetor
3737 and sp.idproduto = v_encomenda.idproduto
3738 and not exists
3739 (select 1
3740 from setorrecebimento sr, tiporecebimento tr
3741 where tr.classificacao = 'E'
3742 and tr.idtiporecebimento =
3743 sr.idtiporecebimento
3744 and sr.idsetor = l.idsetor)
3745 order by l.idlocal)
3746 loop
3747 validarCubagemMudancaSetor(p_idusuario, v_encomenda.idproduto,
3748 v_encomenda.idarmazem, c_local.idlocal,
3749 v_qtdelotes, v_qtdecoube);
3750
3751 if v_qtdelotes = v_qtdecoube then
3752 v_idlocal := c_local.idlocal;
3753 exit when v_qtdelotes = v_qtdecoube;
3754 end if;
3755 end loop;
3756
3757 return v_idlocal;
3758 end;
3759
3760 -- Refactored procedure buscarEnderecoPickingMaiorEspaco
3761 function buscarEndPickingMaiorEspaco
3762 (
3763 p_idusuario in number,
3764 v_encomenda in encomenda%rowtype
3765 ) return local.idlocal%type is
3766 v_qtdelotes number;
3767 v_qtdecoube number;
3768 v_idlocal local.idlocal%type := null;
3769 begin
3770 for c_localmaior in (select l.idlocal,
3771 ((l.altura - l.alturamanobra) * l.largura *
3772 l.comprimento) -
3773 sum(e.altura * e.largura * e.comprimento *
3774 (ll.estoque - ll.pendencia +
3775 ll.adicionar)) cubestoque
3776 from lotelocal ll, lote lt, local l, embalagem e,
3777 produtolocal pl, setor s, setorproduto sp,
3778 usuariodepositante ud
3779 where ud.idusuario = p_idusuario
3780 and ud.identidade = lt.iddepositante
3781 and sp.idproduto = lt.idproduto
3782 and sp.idsetor = s.idsetor
3783 and s.idsetor = l.idsetor
3784 and pl.idproduto = lt.idproduto
3785 and pl.identidade = lt.iddepositante
3786 and pl.idlocal = ll.idlocal
3787 and pl.idarmazem = ll.idarmazem
3788 and e.barra = lt.barra
3789 and e.idproduto = lt.idproduto
3790 and l.tipo = 0
3791 and decode(l.buffer, 'N', 0, 1) = 0
3792 and decode(l.ativo, 'N', 0, 1) = 1
3793 and l.idlocal = ll.idlocal
3794 and l.idarmazem = ll.idarmazem
3795 and lt.iddepositante = v_encomenda.iddepositante
3796 and lt.idproduto = v_encomenda.idproduto
3797 and lt.idlote = ll.idlote
3798 and ll.estoque + ll.adicionar > 0
3799 and ll.idarmazem = v_encomenda.idarmazem
3800 and not exists
3801 (select 1
3802 from setorrecebimento sr,
3803 tiporecebimento tr
3804 where tr.classificacao = 'E'
3805 and tr.idtiporecebimento =
3806 sr.idtiporecebimento
3807 and sr.idsetor = l.idsetor)
3808 group by l.idlocal, l.altura, l.largura,
3809 l.comprimento, l.alturamanobra
3810 order by cubestoque desc)
3811 loop
3812 validarCubagemMudancaSetor(p_idusuario, v_encomenda.idproduto,
3813 v_encomenda.idarmazem, c_localmaior.idlocal,
3814 v_qtdelotes, v_qtdecoube);
3815
3816 if v_qtdelotes = v_qtdecoube then
3817 v_idlocal := c_localmaior.idlocal;
3818 exit when v_qtdelotes = v_qtdecoube;
3819 end if;
3820 end loop;
3821
3822 return v_idlocal;
3823 end;
3824
3825 /*
3826 * Rotina responsavel por gerar remanejamentos de itens ja alocados de encomendas
3827 */
3828 procedure GerarRemItensAlocados
3829 (
3830 p_idencomenda in number,
3831 p_idusuario in number
3832 ) is
3833 v_encomenda encomenda%rowtype;
3834 v_qtderemanejar number;
3835 v_qtdelote number;
3836 v_idlocaldestino local.idlocal%type;
3837 v_idremanejamento number;
3838 v_qtdeemremanejamento number;
3839 v_qtdeTotalCaixas number;
3840 v_normaPallet number;
3841 v_qtdePallet number;
3842 v_caixasPallet number;
3843 v_unidadesPallet number;
3844 v_qtdeLastro number;
3845 v_caixasLastro number;
3846 v_unidadesLastro number;
3847 v_qtdeCaixas number;
3848 v_unidadesCaixas number;
3849 v_qtdeunidades number;
3850 v_metodoger_incompl_sobra number;
3851 v_msg t_message;
3852 begin
3853 begin
3854 select ec.id, ec.pedido, ec.serie, ec.idproduto, ec.codigoencomenda,
3855 ec.qtde, ec.idsetor, ec.iddepositante, ec.idarmazem, ec.status,
3856 ec.data, ec.qtdealocada, ec.qtdeconferida, ec.qtderemanejada,
3857 ec.idnotafiscalsaida
3858 into v_encomenda
3859 from encomenda ec
3860 where ec.id = p_idencomenda;
3861 exception
3862 when no_data_found then
3863 v_msg := t_message('ENCOMENDA ID {0} NAO ENCONTRADA.');
3864 v_msg.addParam(p_idencomenda);
3865 raise_application_error(-20000, v_msg.formatMessage);
3866 end;
3867
3868 -- Seleciona o método de geração de lotes para palet incompleto e sobra
3869 select ar.metodogerlotesobraincompl
3870 into v_metodoger_incompl_sobra
3871 from encomenda e, armazem ar
3872 where ar.idarmazem = e.idarmazem
3873 and e.id = p_idencomenda;
3874
3875 v_qtdeemremanejamento := 0;
3876 v_qtderemanejar := (v_encomenda.qtdealocada -
3877 v_encomenda.qtderemanejada) - v_encomenda.qtde;
3878
3879 if (v_qtderemanejar > 0) then
3880 for c_estoque in (select ll.idlocal, lt.idlote,
3881 (ll.estoque - ll.pendencia + ll.adicionar) disponivel,
3882 lt.iddepositante, l.id idendereco,
3883 e.fatorconversao,
3884 pd.tipoalocpalletincompleto palletimcompleto,
3885 e.lastro, e.qtdecamada,
3886 pd.tipoalocpalletquebra,
3887 pd.tipoalocpalletsobra, e.caixafechada
3888 from lote lt, lotelocal ll, local l, embalagem e,
3889 produtodepositante pd
3890 where pd.idproduto = lt.idproduto
3891 and pd.identidade = lt.iddepositante
3892 and e.barra =
3893 pk_produto.ret_codbarra_nao_precad(lt.idproduto,
3894 1)
3895 and e.idproduto = lt.idproduto
3896 and l.idsetor = v_encomenda.idsetor
3897 and l.idlocal = ll.idlocal
3898 and l.idarmazem = ll.idarmazem
3899 and (ll.estoque - ll.pendencia + ll.adicionar) > 0
3900 and ll.idlote = lt.idlote
3901 and lt.idencomenda = p_idencomenda)
3902 loop
3903 if (c_estoque.disponivel > v_qtderemanejar) then
3904 v_qtdelote := v_qtderemanejar;
3905 else
3906 v_qtdelote := c_estoque.disponivel;
3907 end if;
3908
3909 pk_lote.CalcularQtdeLotes(v_qtdelote, c_estoque.fatorconversao,
3910 c_estoque.lastro, c_estoque.qtdecamada,
3911 v_metodoger_incompl_sobra,
3912 c_estoque.caixafechada, v_qtdeTotalCaixas,
3913 v_normaPallet, v_qtdePallet,
3914 v_caixasPallet, v_unidadesPallet,
3915 v_qtdeLastro, v_caixasLastro,
3916 v_unidadesLastro, v_qtdeCaixas,
3917 v_unidadesCaixas, v_qtdeunidades);
3918
3919 if (v_qtdePallet > 0) then
3920 -- Aplicando 1 regra: Buscar endereco de destino vazio que permita cubagem do mesmo
3921 v_idlocaldestino := buscarEnderecoPulmaoVazio(p_idusuario,
3922 v_encomenda);
3923
3924 -- Aplicando 2 regra: Buscar endereco de destino onde haja estoque do produto e permita cubagem do mesmo
3925 if v_idlocaldestino is null then
3926 v_idlocaldestino := buscarEnderecoPulmaoComEstoque(p_idusuario,
3927 v_encomenda);
3928 end if;
3929
3930 if v_idlocaldestino is not null then
3931 v_qtdeemremanejamento := v_qtdeemremanejamento +
3932 v_unidadesPallet;
3933
3934 -- Criando remanejamento
3935 v_idremanejamento := cadastrar_remanejamento(v_encomenda.idarmazem,
3936 v_encomenda.idarmazem,
3937 c_estoque.idlocal,
3938 v_idlocaldestino,
3939 p_idusuario, null,
3940 'REMANEJAMENTO CRIADO POR MOTIVO DE ALTERACAO DE ENCOMENDA',
3941 C_NAO, C_SIM);
3942 update remanejamento r
3943 set r.tipo = 2
3944 where r.idremanejamento = v_idremanejamento;
3945
3946 begin
3947 insert into loteremanejamento
3948 (idremanejamento, idlote, qtde, conferido, controlaqtde,
3949 diferenca)
3950 values
3951 (v_idremanejamento, c_estoque.idlote, v_unidadesPallet,
3952 c_nao, c_sim, 0);
3953 exception
3954 when dup_val_on_index then
3955 update loteremanejamento
3956 set qtde = qtde + v_unidadesPallet
3957 where idremanejamento = v_idremanejamento
3958 and idlote = c_estoque.idlote;
3959 end;
3960
3961 Associar(v_idremanejamento, c_estoque.idlote);
3962 else
3963 v_msg := t_message('ENDERECO DE DESTINO PARA REMANEJAMENTO DE PALLET COMPLETO NAO ENCONTRADO.');
3964 raise_application_error(-20000, v_msg.formatMessage);
3965 end if;
3966 end if;
3967
3968 if (v_qtdeLastro > 0) then
3969 v_idlocaldestino := null;
3970 if (c_estoque.palletimcompleto = 0 or
3971 c_estoque.palletimcompleto = 1) then
3972
3973 -- Aplicando 1 regra: Buscar endereco de destino onde haja estoque do produto e permita cubagem do mesmo
3974 if (v_idlocaldestino is null and c_estoque.palletimcompleto = 1) then
3975 v_idlocaldestino := buscarEnderecoPulmaoComEstoque(p_idusuario,
3976 v_encomenda);
3977 end if;
3978
3979 -- Aplicando 2 regra: Buscar endereco de destino vazio que permita cubagem do mesmo
3980 if v_idlocaldestino is null then
3981 v_idlocaldestino := buscarEnderecoPulmaoVazio(p_idusuario,
3982 v_encomenda);
3983 end if;
3984
3985 if v_idlocaldestino is not null then
3986 v_qtdeemremanejamento := v_qtdeemremanejamento +
3987 v_unidadesLastro;
3988
3989 -- Criando remanejamento
3990 v_idremanejamento := cadastrar_remanejamento(v_encomenda.idarmazem,
3991 v_encomenda.idarmazem,
3992 c_estoque.idlocal,
3993 v_idlocaldestino,
3994 p_idusuario, null,
3995 'REMANEJAMENTO CRIADO POR MOTIVO DE ALTERACAO DE ENCOMENDA',
3996 C_NAO, C_SIM);
3997 update remanejamento r
3998 set r.tipo = 2
3999 where r.idremanejamento = v_idremanejamento;
4000
4001 begin
4002 insert into loteremanejamento
4003 (idremanejamento, idlote, qtde, conferido, controlaqtde,
4004 diferenca)
4005 values
4006 (v_idremanejamento, c_estoque.idlote, v_unidadesLastro,
4007 c_nao, c_sim, 0);
4008 exception
4009 when dup_val_on_index then
4010 update loteremanejamento
4011 set qtde = qtde + v_unidadesLastro
4012 where idremanejamento = v_idremanejamento
4013 and idlote = c_estoque.idlote;
4014 end;
4015
4016 Associar(v_idremanejamento, c_estoque.idlote);
4017 else
4018 v_msg := t_message('ENDERECO DE PULMAO PARA REMANEJAMENTO DE PALLET INCOMPLETO NAO ENCONTRADO.');
4019 raise_application_error(-20000, v_msg.formatMessage);
4020 end if;
4021 elsif (c_estoque.palletimcompleto = 2) then
4022 -- Aplicando 1 regra: Buscar endereco de destino onde haja maior espaço disponÃvel e permita cubagem do mesmo
4023 if v_idlocaldestino is null then
4024 v_idlocaldestino := buscarEndPickingMaiorEspaco(p_idusuario,
4025 v_encomenda);
4026 end if;
4027
4028 if v_idlocaldestino is not null then
4029 v_qtdeemremanejamento := v_qtdeemremanejamento +
4030 v_unidadesLastro;
4031
4032 -- Criando remanejamento
4033 v_idremanejamento := cadastrar_remanejamento(v_encomenda.idarmazem,
4034 v_encomenda.idarmazem,
4035 c_estoque.idlocal,
4036 v_idlocaldestino,
4037 p_idusuario, null,
4038 'REMANEJAMENTO CRIADO POR MOTIVO DE ALTERACAO DE ENCOMENDA',
4039 C_NAO, C_SIM);
4040 update remanejamento r
4041 set r.tipo = 2
4042 where r.idremanejamento = v_idremanejamento;
4043
4044 begin
4045 insert into loteremanejamento
4046 (idremanejamento, idlote, qtde, conferido, controlaqtde,
4047 diferenca)
4048 values
4049 (v_idremanejamento, c_estoque.idlote, v_unidadesLastro,
4050 c_nao, c_sim, 0);
4051 exception
4052 when dup_val_on_index then
4053 update loteremanejamento
4054 set qtde = qtde + v_unidadesLastro
4055 where idremanejamento = v_idremanejamento
4056 and idlote = c_estoque.idlote;
4057 end;
4058
4059 Associar(v_idremanejamento, c_estoque.idlote);
4060 else
4061 v_msg := t_message('ENDERECO DE PICKING PARA REMANEJAMENTO DE PALLET INCOMPLETO NAO ENCONTRADO.');
4062 raise_application_error(-20000, v_msg.formatMessage);
4063 end if;
4064 end if;
4065 end if;
4066
4067 if (v_qtdeCaixas > 0) then
4068 v_idlocaldestino := null;
4069 if (c_estoque.tipoalocpalletsobra = 0) then
4070 -- Aplicando 1 regra: Buscar endereco de destino vazio que permita cubagem do mesmo
4071 if v_idlocaldestino is null then
4072 v_idlocaldestino := buscarEnderecoPulmaoVazio(p_idusuario,
4073 v_encomenda);
4074 end if;
4075
4076 if v_idlocaldestino is not null then
4077 v_qtdeemremanejamento := v_qtdeemremanejamento +
4078 v_unidadesCaixas;
4079
4080 -- Criando remanejamento
4081 v_idremanejamento := cadastrar_remanejamento(v_encomenda.idarmazem,
4082 v_encomenda.idarmazem,
4083 c_estoque.idlocal,
4084 v_idlocaldestino,
4085 p_idusuario, null,
4086 'REMANEJAMENTO CRIADO POR MOTIVO DE ALTERACAO DE ENCOMENDA',
4087 C_NAO, C_SIM);
4088 update remanejamento r
4089 set r.tipo = 2
4090 where r.idremanejamento = v_idremanejamento;
4091
4092 begin
4093 insert into loteremanejamento
4094 (idremanejamento, idlote, qtde, conferido, controlaqtde,
4095 diferenca)
4096 values
4097 (v_idremanejamento, c_estoque.idlote, v_unidadesCaixas,
4098 c_nao, c_sim, 0);
4099 exception
4100 when dup_val_on_index then
4101 update loteremanejamento
4102 set qtde = qtde + v_unidadesCaixas
4103 where idremanejamento = v_idremanejamento
4104 and idlote = c_estoque.idlote;
4105 end;
4106
4107 Associar(v_idremanejamento, c_estoque.idlote);
4108 else
4109 v_msg := t_message('ENDERECO DE PULMAO VAZIO PARA REMANEJAMENTO DE PALLET SOBRA NAO ENCONTRADO.');
4110 raise_application_error(-20000, v_msg.formatMessage);
4111 end if;
4112 elsif (c_estoque.tipoalocpalletsobra = 1) then
4113 -- Aplicando 1 regra: Buscar endereco de destino onde haja estoque do produto e permita cubagem do mesmo
4114 if v_idlocaldestino is null then
4115 v_idlocaldestino := buscarEnderecoPulmaoComEstoque(p_idusuario,
4116 v_encomenda);
4117 end if;
4118
4119 if v_idlocaldestino is not null then
4120 v_qtdeemremanejamento := v_qtdeemremanejamento +
4121 v_unidadesCaixas;
4122
4123 -- Criando remanejamento
4124 v_idremanejamento := cadastrar_remanejamento(v_encomenda.idarmazem,
4125 v_encomenda.idarmazem,
4126 c_estoque.idlocal,
4127 v_idlocaldestino,
4128 p_idusuario, null,
4129 'REMANEJAMENTO CRIADO POR MOTIVO DE ALTERACAO DE ENCOMENDA',
4130 C_NAO, C_SIM);
4131 update remanejamento r
4132 set r.tipo = 2
4133 where r.idremanejamento = v_idremanejamento;
4134
4135 begin
4136 insert into loteremanejamento
4137 (idremanejamento, idlote, qtde, conferido, controlaqtde,
4138 diferenca)
4139 values
4140 (v_idremanejamento, c_estoque.idlote, v_unidadesCaixas,
4141 c_nao, c_sim, 0);
4142 exception
4143 when dup_val_on_index then
4144 update loteremanejamento
4145 set qtde = qtde + v_unidadesCaixas
4146 where idremanejamento = v_idremanejamento
4147 and idlote = c_estoque.idlote;
4148 end;
4149
4150 Associar(v_idremanejamento, c_estoque.idlote);
4151 else
4152 v_msg := t_message('ENDERECO DE PULMAO UTILIZADO PARA REMANEJAMENTO DE PALLET SOBRA NAO ENCONTRADO.');
4153 raise_application_error(-20000, v_msg.formatMessage);
4154 end if;
4155
4156 elsif (c_estoque.tipoalocpalletsobra = 2) then
4157 -- Aplicando 1 regra: Buscar endereco de destino onde haja maior espaço disponÃvel e permita cubagem do mesmo
4158 if v_idlocaldestino is null then
4159 v_idlocaldestino := buscarEndPickingMaiorEspaco(p_idusuario,
4160 v_encomenda);
4161 end if;
4162
4163 if v_idlocaldestino is not null then
4164 v_qtdeemremanejamento := v_qtdeemremanejamento +
4165 v_unidadesCaixas;
4166
4167 -- Criando remanejamento
4168 v_idremanejamento := cadastrar_remanejamento(v_encomenda.idarmazem,
4169 v_encomenda.idarmazem,
4170 c_estoque.idlocal,
4171 v_idlocaldestino,
4172 p_idusuario, null,
4173 'REMANEJAMENTO CRIADO POR MOTIVO DE ALTERACAO DE ENCOMENDA',
4174 C_NAO, C_SIM);
4175 update remanejamento r
4176 set r.tipo = 2
4177 where r.idremanejamento = v_idremanejamento;
4178
4179 begin
4180 insert into loteremanejamento
4181 (idremanejamento, idlote, qtde, conferido, controlaqtde,
4182 diferenca)
4183 values
4184 (v_idremanejamento, c_estoque.idlote, v_unidadesCaixas,
4185 c_nao, c_sim, 0);
4186 exception
4187 when dup_val_on_index then
4188 update loteremanejamento
4189 set qtde = qtde + v_unidadesCaixas
4190 where idremanejamento = v_idremanejamento
4191 and idlote = c_estoque.idlote;
4192 end;
4193
4194 Associar(v_idremanejamento, c_estoque.idlote);
4195 else
4196 v_msg := t_message('ENDERECO DE PICKING PARA REMANEJAMENTO DE PALLET SOBRA NAO ENCONTRADO.');
4197 raise_application_error(-20000, v_msg.formatMessage);
4198 end if;
4199 end if;
4200 end if;
4201
4202 if (v_qtdeunidades > 0) then
4203 v_idlocaldestino := null;
4204 if (c_estoque.tipoalocpalletquebra = 0) then
4205 -- Aplicando 1 regra: Buscar endereco de destino vazio que permita cubagem do mesmo
4206 if v_idlocaldestino is null then
4207 v_idlocaldestino := buscarEnderecoPulmaoVazio(p_idusuario,
4208 v_encomenda);
4209 end if;
4210
4211 if v_idlocaldestino is not null then
4212 v_qtdeemremanejamento := v_qtdeemremanejamento +
4213 v_qtdeunidades;
4214
4215 -- Criando remanejamento
4216 v_idremanejamento := cadastrar_remanejamento(v_encomenda.idarmazem,
4217 v_encomenda.idarmazem,
4218 c_estoque.idlocal,
4219 v_idlocaldestino,
4220 p_idusuario, null,
4221 'REMANEJAMENTO CRIADO POR MOTIVO DE ALTERACAO DE ENCOMENDA',
4222 C_NAO, C_SIM);
4223 update remanejamento r
4224 set r.tipo = 2
4225 where r.idremanejamento = v_idremanejamento;
4226
4227 begin
4228 insert into loteremanejamento
4229 (idremanejamento, idlote, qtde, conferido, controlaqtde,
4230 diferenca)
4231 values
4232 (v_idremanejamento, c_estoque.idlote, v_qtdeunidades,
4233 c_nao, c_sim, 0);
4234 exception
4235 when dup_val_on_index then
4236 update loteremanejamento
4237 set qtde = qtde + v_qtdeunidades
4238 where idremanejamento = v_idremanejamento
4239 and idlote = c_estoque.idlote;
4240 end;
4241
4242 Associar(v_idremanejamento, c_estoque.idlote);
4243 else
4244 v_msg := t_message('ENDERECO DE PULMAO VAZIO PARA REMANEJAMENTO DE PALLET INCOMPLETO NAO ENCONTRADO.');
4245 raise_application_error(-20000, v_msg.formatMessage);
4246 end if;
4247 elsif (c_estoque.tipoalocpalletquebra = 1) then
4248 -- Aplicando 1 regra: Buscar endereco de destino onde haja estoque do produto e permita cubagem do mesmo
4249 if v_idlocaldestino is null then
4250 v_idlocaldestino := buscarEnderecoPulmaoComEstoque(p_idusuario,
4251 v_encomenda);
4252 end if;
4253
4254 if v_idlocaldestino is not null then
4255 v_qtdeemremanejamento := v_qtdeemremanejamento +
4256 v_qtdeunidades;
4257
4258 -- Criando remanejamento
4259 v_idremanejamento := cadastrar_remanejamento(v_encomenda.idarmazem,
4260 v_encomenda.idarmazem,
4261 c_estoque.idlocal,
4262 v_idlocaldestino,
4263 p_idusuario, null,
4264 'REMANEJAMENTO CRIADO POR MOTIVO DE ALTERACAO DE ENCOMENDA',
4265 C_NAO, C_SIM);
4266 update remanejamento r
4267 set r.tipo = 2
4268 where r.idremanejamento = v_idremanejamento;
4269
4270 begin
4271 insert into loteremanejamento
4272 (idremanejamento, idlote, qtde, conferido, controlaqtde,
4273 diferenca)
4274 values
4275 (v_idremanejamento, c_estoque.idlote, v_qtdeunidades,
4276 c_nao, c_sim, 0);
4277 exception
4278 when dup_val_on_index then
4279 update loteremanejamento
4280 set qtde = qtde + v_qtdeunidades
4281 where idremanejamento = v_idremanejamento
4282 and idlote = c_estoque.idlote;
4283 end;
4284
4285 Associar(v_idremanejamento, c_estoque.idlote);
4286 else
4287 v_msg := t_message('ENDERECO DE PULMAO UTILIZADO PARA REMANEJAMENTO DE PALLET INCOMPLETO NAO ENCONTRADO.');
4288 raise_application_error(-20000, v_msg.formatMessage);
4289 end if;
4290 elsif (c_estoque.tipoalocpalletquebra = 2) then
4291 -- Aplicando 1 regra: Buscar endereco de destino onde haja maior espaço disponÃvel e permita cubagem do mesmo
4292 if v_idlocaldestino is null then
4293 v_idlocaldestino := buscarEndPickingMaiorEspaco(p_idusuario,
4294 v_encomenda);
4295 end if;
4296
4297 if v_idlocaldestino is not null then
4298 v_qtdeemremanejamento := v_qtdeemremanejamento +
4299 v_qtdeunidades;
4300
4301 -- Criando remanejamento
4302 v_idremanejamento := cadastrar_remanejamento(v_encomenda.idarmazem,
4303 v_encomenda.idarmazem,
4304 c_estoque.idlocal,
4305 v_idlocaldestino,
4306 p_idusuario, null,
4307 'REMANEJAMENTO CRIADO POR MOTIVO DE ALTERACAO DE ENCOMENDA',
4308 C_NAO, C_SIM);
4309 update remanejamento r
4310 set r.tipo = 2
4311 where r.idremanejamento = v_idremanejamento;
4312
4313 begin
4314 insert into loteremanejamento
4315 (idremanejamento, idlote, qtde, conferido, controlaqtde,
4316 diferenca)
4317 values
4318 (v_idremanejamento, c_estoque.idlote, v_qtdeunidades,
4319 c_nao, c_sim, 0);
4320 exception
4321 when dup_val_on_index then
4322 update loteremanejamento
4323 set qtde = qtde + v_qtdeunidades
4324 where idremanejamento = v_idremanejamento
4325 and idlote = c_estoque.idlote;
4326 end;
4327
4328 Associar(v_idremanejamento, c_estoque.idlote);
4329 else
4330 v_msg := t_message('ENDERECO DE PICKING PARA REMANEJAMENTO DE PALLET INCOMPLETO NAO ENCONTRADO.');
4331 raise_application_error(-20000, v_msg.formatMessage);
4332 end if;
4333 end if;
4334 end if;
4335
4336 v_idlocaldestino := null;
4337 exit when v_qtdeemremanejamento = v_qtderemanejar;
4338 end loop;
4339
4340 update encomenda
4341 set qtderemanejada = qtderemanejada + v_qtderemanejar
4342 where id = p_idencomenda;
4343 end if;
4344 end;
4345
4346 -- Refactored procedure validarMudancaPicking
4347 procedure validarMudancaPicking
4348 (
4349 p_iddepositante in number,
4350 p_idproduto in number,
4351 p_idArmazem in number,
4352 p_idlocalorigem in local.idlocal%type,
4353 p_transfPendenciaAdicionar in out number,
4354 p_idlocaldestino in local.idlocal%type
4355 ) is
4356 v_totaldepositantes number;
4357 v_totalpermitidomover number;
4358 v_totalestoque number;
4359 v_mudarPickingVazio number;
4360 r_loteUnicoComparar pk_lote.t_loteunico;
4361 r_loteunico pk_lote.t_loteunico;
4362 v_loteuniconoendereco number;
4363 v_msgErro varchar2(1000);
4364 v_msg t_message;
4365 v_remanejamentos varchar2(4000);
4366 begin
4367
4368 for c in (select l.idproduto, l.descr, l.dtvenc, l.estado,
4369 pd.loteuniconoendereco, l.iddepositante
4370 from lotelocal ll, lote l, produtodepositante pd
4371 where l.idlote = ll.idlote
4372 and ll.idlocal = p_idlocalorigem
4373 and ll.idarmazem = p_idArmazem
4374 and l.idproduto = p_idproduto
4375 and l.iddepositante = pd.identidade
4376 and pd.idproduto = l.idproduto)
4377 loop
4378
4379 r_loteunicocomparar.idproduto := c.idproduto;
4380 r_loteunicocomparar.estado := c.estado;
4381 r_loteunicocomparar.loteindustria := c.descr;
4382 r_loteunicocomparar.dtvencimento := c.dtvenc;
4383 r_loteunicocomparar.loteuniconoendereco := c.loteuniconoendereco;
4384 r_loteunicocomparar.iddepositante := c.iddepositante;
4385
4386 if (not
4387 pk_lote.isLocalPodeReceberLoteUnico(r_loteunicocomparar,
4388 p_idArmazem, p_idlocaldestino,
4389 v_msgErro)) then
4390 raise_application_error(-20000, v_msgErro);
4391 end if;
4392 end loop;
4393
4394 select count(distinct lt.iddepositante)
4395 into v_totaldepositantes
4396 from lotelocal ll, lote lt
4397 where ll.idlocal = p_idlocalorigem
4398 and ll.idarmazem = p_idarmazem
4399 and lt.idlote = ll.idlote
4400 and lt.idproduto = p_idproduto;
4401
4402 if (v_totaldepositantes > 1) then
4403 v_msg := t_message('Erro ao executar mudança de picking. Local de Origem possui estoque de mais de um depositante.');
4404 raise_application_error(-20000, v_msg.formatMessage);
4405 end if;
4406
4407 select a.transferirpendenciaeadicionar, a.mudarpickingvazio
4408 into p_transfPendenciaAdicionar, v_mudarPickingVazio
4409 from armazem a
4410 where a.idarmazem = p_idArmazem;
4411
4412 select sum(ll.estoque - ll.pendencia) permitido,
4413 sum(ll.estoque + ll.adicionar) total
4414 into v_totalpermitidomover, v_totalestoque
4415 from lotelocal ll, lote lt
4416 where ll.idlocal = p_idlocalorigem
4417 and ll.idarmazem = p_idArmazem
4418 and (ll.estoque > 0 or ll.pendencia > 0 or ll.adicionar > 0)
4419 and lt.idlote = ll.idlote
4420 and lt.idproduto = p_idproduto
4421 and lt.iddepositante = p_iddepositante;
4422
4423 if (v_totalestoque = 0 and v_mudarPickingVazio = 0) then
4424 v_msg := t_message('Endereço de origem sem estoque do produto. Operação cancelada');
4425 raise_application_error(-20000, v_msg.formatMessage);
4426 end if;
4427
4428 if (v_totalpermitidomover = 0 and p_transfPendenciaAdicionar = 0) then
4429 v_msg := t_message('A configuração para transferir pendencia e adicionar na mudança de picking esta destivada.');
4430 raise_application_error(-20000, v_msg.formatMessage);
4431 end if;
4432
4433 select substr(stragg(r.idremanejamento), 1, 4000)
4434 into v_remanejamentos
4435 from remanejamento r, loteremanejamento lr, lote lt
4436 where r.idlocalorigem = p_idlocaldestino
4437 and r.idlocaldestino = p_idlocalorigem
4438 and r.idarmazemorigem = p_idArmazem
4439 and r.idarmazemdestino = p_idArmazem
4440 and lr.idremanejamento = r.idremanejamento
4441 and lt.idlote = lr.idlote
4442 and lt.idproduto = p_idproduto
4443 and lt.iddepositante = p_iddepositante
4444 and r.status <> 'F';
4445
4446 if (v_remanejamentos is not null) then
4447 v_msg := t_message('Existe(m) remanejamento(s) criado(s) com local de origem igual o ' ||
4448 'local destino e local destino igual ao local origem da mudança de ' ||
4449 'picking para este produto e depositante. Finalize ou cancele o(s) remanejamento(s) ' ||
4450 'para continuar. Remanejamento(s): {0}');
4451 v_msg.addParam(v_remanejamentos);
4452 raise_application_error(-20000, v_msg.formatMessage);
4453 end if;
4454
4455 pk_armazem.validarSetorPickingProduto(p_idArmazem, p_idlocaldestino,
4456 p_iddepositante, p_idproduto);
4457
4458 end validarMudancaPicking;
4459
4460 -- Refactored procedure criarRemMudancaPicking
4461 procedure criarRemMudancaPicking
4462 (
4463 p_idusuario in number,
4464 p_iddepositante in number,
4465 p_idproduto in number,
4466 p_idArmazem in number,
4467 p_idlocalorigem in local.idlocal%type,
4468 p_idlocaldestino in local.idlocal%type,
4469 p_transfPendenciaAdicionar in number,
4470 p_idremanejamento in out number
4471 ) is
4472 v_jaexistepickdestino number;
4473 v_qtdelr number;
4474 begin
4475 select seq_remanejamento.nextval idremanejamento
4476 into p_idremanejamento
4477 from dual;
4478
4479 pk_triggers_control.disableTrigger('T_BEFORE_REMANEJAMENTO');
4480
4481 insert into remanejamento
4482 (idremanejamento, idarmazemorigem, idlocalorigem, idarmazemdestino,
4483 idlocaldestino, datahora, idusuariotela, status, cadmanual,
4484 idromaneio, descrpalet, planejado, horainicio, tipo)
4485 values
4486 (p_idremanejamento, p_idarmazem, p_idlocalorigem, p_idarmazem,
4487 p_idlocaldestino, sysdate, p_idusuario, c_aguardando, c_nao, NULL,
4488 'REMANEJAMENTO CRIADO PARA MUDANÇA DE PICKING', 'N', sysdate, 3);
4489
4490 pk_triggers_control.enableTrigger('T_BEFORE_REMANEJAMENTO');
4491
4492 delete from produtolocal pd
4493 where pd.idarmazem = p_idArmazem
4494 and pd.idlocal = p_idlocalorigem
4495 and pd.idproduto = p_idproduto
4496 and pd.identidade = p_iddepositante;
4497
4498 select count(*)
4499 into v_jaexistepickdestino
4500 from produtolocal pd
4501 where pd.idarmazem = p_idArmazem
4502 and pd.idlocal = p_idlocaldestino
4503 and pd.idproduto = p_idproduto
4504 and pd.identidade = p_iddepositante;
4505
4506 if (v_jaexistepickdestino = 0) then
4507 pk_armazem.criarPickingProduto(p_idArmazem, p_idlocaldestino,
4508 p_iddepositante, p_idproduto, 1);
4509 end if;
4510
4511 for c_lote in (select ll.idlote, (ll.estoque - ll.pendencia) permitido,
4512 (ll.estoque + ll.adicionar) total
4513 from lotelocal ll, lote lt
4514 where ll.idlocal = p_idlocalorigem
4515 and ll.idarmazem = p_idArmazem
4516 and (ll.estoque > 0 or ll.pendencia > 0 or
4517 ll.adicionar > 0)
4518 and lt.idlote = ll.idlote
4519 and lt.idproduto = p_idproduto
4520 and lt.iddepositante = p_iddepositante)
4521 loop
4522 if (p_transfPendenciaAdicionar = 1) then
4523 v_qtdelr := c_lote.total;
4524 else
4525 if (c_lote.permitido > 0) then
4526 v_qtdelr := c_lote.permitido;
4527 else
4528 v_qtdelr := 0;
4529 end if;
4530 end if;
4531
4532 if (v_qtdelr > 0) then
4533 insert into loteremanejamento
4534 (idremanejamento, idlote, qtde, conferido, controlaqtde)
4535 values
4536 (p_idremanejamento, c_lote.idlote, v_qtdelr, 'S', 'S');
4537 end if;
4538 end loop;
4539 end criarRemMudancaPicking;
4540
4541 -- Refactored procedure moverEstoqueMudPicking
4542 procedure moverEstoqueMudPicking
4543 (
4544 p_idusuario in number,
4545 p_idArmazem in number,
4546 p_idlocalorigem in local.idlocal%type,
4547 p_idlocaldestino in local.idlocal%type,
4548 v_transfPendenciaAdicionar in out number,
4549 v_idremanejamento in out number
4550 ) is
4551 v_qtdemover number;
4552 begin
4553 for c_estoque in (select ll.idlote, ll.estoque, ll.pendencia,
4554 ll.adicionar, lo.tipo tipoLocalOrigem,
4555 lo.buffer bufferOrigem, lt.idproduto,
4556 lt.iddepositante
4557 from lotelocal ll, local lo, lote lt
4558 where ll.idlocal = p_idlocalorigem
4559 and ll.idarmazem = p_idarmazem
4560 and lo.idlocal = ll.idlocal
4561 and lo.idarmazem = ll.idarmazem
4562 and lt.idlote = ll.idlote
4563 and exists
4564 (select 1
4565 from loteremanejamento lr
4566 where lr.idremanejamento = v_idremanejamento
4567 and lr.idlote = ll.idlote)
4568 order by ll.idlote)
4569 loop
4570 if (v_transfPendenciaAdicionar = 0) then
4571 v_qtdemover := c_estoque.estoque - c_estoque.pendencia;
4572 if (v_qtdemover > 0) then
4573 pk_estoque.retirar_estoque(p_idArmazem, p_idlocalorigem,
4574 c_estoque.idlote, v_qtdemover,
4575 p_idusuario,
4576 'RETIRADO ESTOQUE REFERENTE MUDANCA DE PICKING ID: ' ||
4577 v_idremanejamento, 'N');
4578
4579 if ((c_estoque.tipolocalorigem = 0) AND
4580 (c_estoque.bufferorigem = 'N')) then
4581 pk_picking_dinamico.addGttPickingDinamico(c_estoque.idproduto,
4582 c_estoque.iddepositante,
4583 p_idlocalorigem,
4584 p_idArmazem, 0);
4585 end if;
4586 pk_picking_dinamico.addGttPickingDinamico(c_estoque.idproduto,
4587 c_estoque.iddepositante,
4588 p_idlocaldestino,
4589 p_idArmazem, 1);
4590
4591 pk_estoque.incluir_estoque(p_idArmazem, p_idlocaldestino,
4592 c_estoque.idlote, v_qtdemover,
4593 p_idusuario,
4594 'ADICIONADO ESTOQUE REFERENTE MUDANCA DE PICKING ID: ' ||
4595 v_idremanejamento, 'N');
4596 end if;
4597 else
4598 if (c_estoque.pendencia > 0) then
4599 pk_estoque.retirar_pendencia(p_idArmazem, p_idlocalorigem,
4600 c_estoque.idlote, c_estoque.pendencia,
4601 p_idusuario,
4602 'RETIRADA PENDENCIA REFERENTE MUDANCA DE PICKING ID: ' ||
4603 v_idremanejamento);
4604 end if;
4605
4606 if (c_estoque.estoque > 0) then
4607 pk_estoque.retirar_estoque(p_idArmazem, p_idlocalorigem,
4608 c_estoque.idlote, c_estoque.estoque,
4609 p_idusuario,
4610 'RETIRADO ESTOQUE REFERENTE MUDANCA DE PICKING ID: ' ||
4611 v_idremanejamento, 'N');
4612
4613 if ((c_estoque.tipolocalorigem = 0) AND
4614 (c_estoque.bufferorigem = 'N')) then
4615 pk_picking_dinamico.addGttPickingDinamico(c_estoque.idproduto,
4616 c_estoque.iddepositante,
4617 p_idlocalorigem,
4618 p_idArmazem, 0);
4619 end if;
4620
4621 pk_picking_dinamico.addGttPickingDinamico(c_estoque.idproduto,
4622 c_estoque.iddepositante,
4623 p_idlocaldestino,
4624 p_idArmazem, 1);
4625 pk_estoque.incluir_estoque(p_idArmazem, p_idlocaldestino,
4626 c_estoque.idlote, c_estoque.estoque,
4627 p_idusuario,
4628 'ADICIONADO ESTOQUE REFERENTE MUDANCA DE PICKING ID: ' ||
4629 v_idremanejamento, 'N');
4630 end if;
4631
4632 if (c_estoque.adicionar > 0) then
4633 pk_estoque.retirar_adicionar(p_idArmazem, p_idlocalorigem,
4634 c_estoque.idlote, c_estoque.adicionar,
4635 p_idusuario,
4636 'RETIRADO ADICIONAR REFERENTE MUDANCA DE PICKING ID: ' ||
4637 v_idremanejamento);
4638 pk_estoque.incluir_adicionar(p_idArmazem, p_idlocaldestino,
4639 c_estoque.idlote, c_estoque.adicionar,
4640 p_idusuario,
4641 'INCLUIDO ADICIONAR REFERENTE MUDANCA DE PICKING ID: ' ||
4642 v_idremanejamento);
4643 end if;
4644
4645 if (c_estoque.pendencia > 0) then
4646 pk_estoque.incluir_pendencia(p_idArmazem, p_idlocaldestino,
4647 c_estoque.idlote, c_estoque.pendencia,
4648 p_idusuario,
4649 'ADICIONADA PENDENCIA REFERENTE MUDANCA DE PICKING ID: ' ||
4650 v_idremanejamento);
4651 end if;
4652 end if;
4653 end loop;
4654 pk_picking_dinamico.deletar_picking_dinamico;
4655 pk_picking_dinamico.inserir_picking_dinamico;
4656 end moverEstoqueMudPicking;
4657
4658 -- Refactored procedure alterarRemMudPicking
4659 procedure alterarRemMudPicking
4660 (
4661 p_idusuario in number,
4662 p_iddepositante in number,
4663 p_idproduto in number,
4664 p_idarmazem in number,
4665 p_idlocalorigem in local.idlocal%type,
4666 p_idlocaldestino in local.idlocal%type,
4667 p_idremanejamento in number
4668 ) is
4669 v_totallr number;
4670 v_totaltitulos number;
4671 v_idnovorem number;
4672 v_tiporem number;
4673 v_tiponovorem number;
4674 begin
4675 for c_rem in (select r.idremanejamento, r.idlocaldestino, lr.idlote,
4676 r.idromaneio, lr.qtde, r.idlocalorigem,
4677 lr.controlaqtde, r.tipo
4678 from remanejamento r, loteremanejamento lr, lote lt
4679 where r.status <> 'F'
4680 and r.tipo <> 3
4681 and ((r.idarmazemorigem = p_idarmazem and
4682 r.idlocalorigem = p_idlocalOrigem) or
4683 (r.idarmazemdestino = p_idarmazem and
4684 r.idlocaldestino = p_idlocalorigem))
4685 and lr.idremanejamento = r.idremanejamento
4686 and lt.idlote = lr.idlote
4687 and lt.idproduto = p_idproduto
4688 and lt.iddepositante = p_iddepositante)
4689 loop
4690 select r.tipo
4691 into v_tiporem
4692 from remanejamento r
4693 where r.idremanejamento = c_rem.idremanejamento;
4694
4695 update remanejamento r
4696 set r.tipo = 3
4697 where r.idremanejamento = c_rem.idremanejamento;
4698
4699 -- Se a origem do remanejamento é a mesma origem da mudança de picking
4700 -- o endenreco de origem do remanejamento deve apontar para o destino
4701 -- da mudança de picking
4702 if (c_rem.idlocalorigem = p_idlocalorigem) then
4703 -- se o destino do remanejamento é o destino da mudança de picking
4704 -- o estoque esta sendo movido pela mudança de picking
4705 -- e não é necessario a existencia deste remanejamento
4706 -- portanto a pendencia e adcionar serão removidos
4707 -- do endereço de destino, pois a mudança de picking ja
4708 -- moveu o estoque.
4709 if (c_rem.idlocaldestino = p_idlocaldestino) then
4710 pk_utilities.GeraLog(p_idusuario,
4711 'MUDANÇA DE PICKING ID ' ||
4712 p_idremanejamento ||
4713 ' ALTEROU REMANEJAMENTO ' ||
4714 c_rem.idremanejamento || ', LOTE ' ||
4715 c_rem.idlote || ' FOI REMOVIDO',
4716 p_idremanejamento, 'MP');
4717
4718 delete from loteremanejamento lr
4719 where lr.idremanejamento = c_rem.idremanejamento
4720 and lr.idlote = c_rem.idlote;
4721
4722 if c_rem.controlaqtde = 'S' then
4723 update remanejamento
4724 set qtde = nvl(qtde, 0) - c_rem.qtde
4725 where idremanejamento = c_rem.idremanejamento;
4726 end if;
4727
4728 pk_estoque.retirar_pendencia(p_idArmazem, p_idlocaldestino,
4729 c_rem.idlote, c_rem.qtde, p_idusuario,
4730 'RETIRADA PENDENCIA REFERENTE MUDANCA DE PICKING ID: ' ||
4731 p_idremanejamento);
4732
4733 pk_estoque.retirar_adicionar(p_idArmazem, p_idlocaldestino,
4734 c_rem.idlote, c_rem.qtde, p_idusuario,
4735 'RETIRADO ADICIONAR REFERENTE MUDANCA DE PICKING ID: ' ||
4736 p_idremanejamento);
4737
4738 select count(*)
4739 into v_totallr
4740 from loteremanejamento lr
4741 where lr.idremanejamento = c_rem.idremanejamento;
4742
4743 if (v_totallr = 0) then
4744 pk_utilities.GeraLog(p_idusuario,
4745 'MUDANÇA DE PICKING ID ' ||
4746 p_idremanejamento || '. REMANEJAMENTO ' ||
4747 c_rem.idremanejamento ||
4748 ' EXCLUIDO DEVIDO NÃO POSSUIR MAIS NENHUM LOTE.',
4749 p_idremanejamento, 'MP');
4750
4751 delete from remanejamento r
4752 where r.idremanejamento = c_rem.idremanejamento;
4753 end if;
4754 -- caso o local de destino do remanejamento seja diferente
4755 -- do local de destino da mudança de picking
4756 -- somente o endereço de origem do remanejamento
4757 -- será trocado
4758 else
4759 select count(distinct lt.idproduto)
4760 into v_totaltitulos
4761 from loteremanejamento lr, lote lt
4762 where lr.idremanejamento = c_rem.idremanejamento
4763 and lt.idlote = lr.idlote;
4764
4765 -- se o remanejamento possui apenas lotes do produto que
4766 -- participa da mudança de picking, apenas o local do
4767 -- remanejamento é trocado
4768 if (v_totaltitulos = 1) then
4769 pk_utilities.GeraLog(p_idusuario,
4770 'MUDANÇA DE PICKING ID ' ||
4771 p_idremanejamento ||
4772 ' ALTEROU REMANEJAMENTO ' ||
4773 c_rem.idremanejamento ||
4774 '. LOCAL DE ORIGEM TROCADO. ORIGEM ANTERIOR ' ||
4775 c_rem.idlocalorigem || ', NOVA ORIGEM ' ||
4776 p_idlocaldestino, p_idremanejamento, 'MP');
4777
4778 update remanejamento r
4779 set r.idlocalorigem = p_idlocaldestino
4780 where r.idremanejamento = c_rem.idremanejamento;
4781 -- se o remanejamento possui lotes de outros produtos,
4782 -- será necessario retirar os lotes do produto que participa
4783 -- da mudança de picking e gerar um novo remanejamento
4784 -- para mover o estoque corretamente. A pendencia do primeiro remanejamento
4785 -- não será removida, devido a mudança de picking ja
4786 -- ter movido a pendencia para o local de destino,
4787 -- onde o novo remanejamento irá utilizar esta pendencia
4788 else
4789 delete from loteremanejamento lr
4790 where lr.idremanejamento = c_rem.idremanejamento
4791 and lr.idlote = c_rem.idlote;
4792
4793 if c_rem.controlaqtde = 'S' then
4794 update remanejamento
4795 set qtde = nvl(qtde, 0) - c_rem.qtde
4796 where idremanejamento = c_rem.idremanejamento;
4797 end if;
4798
4799 v_idnovorem := cadastrar_remanejamento(p_idarmazem, p_idarmazem,
4800 p_idlocaldestino,
4801 c_rem.idlocaldestino,
4802 p_idusuario,
4803 c_rem.idromaneio,
4804 'REMANEJAMENTO CRIADO DEVIDO A MUDANCA DE PICKING ID: ' ||
4805 p_idremanejamento, 'N',
4806 'N');
4807
4808 select r.tipo
4809 into v_tiponovorem
4810 from remanejamento r
4811 where r.idremanejamento = v_idnovorem;
4812
4813 update remanejamento r
4814 set r.tipo = 3
4815 where r.idremanejamento = v_idnovorem;
4816
4817 begin
4818 insert into loteremanejamento
4819 (idremanejamento, idlote, qtde, conferido, controlaqtde)
4820 values
4821 (v_idnovorem, c_rem.idlote, c_rem.qtde, 'N', 'S');
4822 exception
4823 when dup_val_on_index then
4824 update loteremanejamento lr
4825 set lr.qtde = lr.qtde + c_rem.qtde
4826 where lr.idremanejamento = v_idnovorem
4827 and lr.idlote = c_rem.idlote;
4828 end;
4829
4830 update remanejamento r
4831 set r.tipo = v_tiponovorem,
4832 r.qtde = decode(c_rem.controlaqtde, 'S',
4833 nvl(r.qtde, 0) - c_rem.qtde, r.qtde)
4834 where r.idremanejamento = v_idnovorem;
4835
4836 pk_utilities.GeraLog(p_idusuario,
4837 'MUDANÇA DE PICKING ID ' ||
4838 p_idremanejamento ||
4839 ' ALTEROU O REMANEJAMENTO ' ||
4840 c_rem.idremanejamento || '. LOTE ' ||
4841 c_rem.idlote ||
4842 ' FOI REMOVIDO E ADICIONADO NO REMANEJAMENTO ' ||
4843 v_idnovorem, p_idremanejamento, 'MP');
4844 end if;
4845 end if;
4846 -- se o destino do remanejamento é igual a origem da mudança
4847 -- de picking, o destino do remanejamento deverá ser trocado para
4848 -- o destino da mudança de picking
4849 else
4850 -- se a origem do remanejamento é igual ao destino da mudança de
4851 -- picking, o estoque não deve ser movido, pois a mudança de picking
4852 -- esta determinando a nova posição do estoque, e este remanejamento
4853 -- não deve mais existir
4854 if (c_rem.idlocalorigem = p_idlocaldestino) then
4855 pk_utilities.GeraLog(p_idusuario,
4856 'MUDANÇA DE PICKING ID ' ||
4857 p_idremanejamento ||
4858 ' ALTEROU REMANEJAMENTO ' ||
4859 c_rem.idremanejamento || ', LOTE ' ||
4860 c_rem.idlote || ' FOI REMOVIDO',
4861 p_idremanejamento, 'MP');
4862
4863 delete from loteremanejamento lr
4864 where lr.idremanejamento = c_rem.idremanejamento
4865 and lr.idlote = c_rem.idlote;
4866
4867 if c_rem.controlaqtde = 'S' then
4868 update remanejamento
4869 set qtde = nvl(qtde, 0) - c_rem.qtde
4870 where idremanejamento = c_rem.idremanejamento;
4871 end if;
4872
4873 pk_estoque.retirar_pendencia(p_idArmazem, p_idlocaldestino,
4874 c_rem.idlote, c_rem.qtde, p_idusuario,
4875 'RETIRADA PENDENCIA REFERENTE MUDANCA DE PICKING ID: ' ||
4876 p_idremanejamento);
4877
4878 pk_estoque.retirar_adicionar(p_idArmazem, p_idlocaldestino,
4879 c_rem.idlote, c_rem.qtde, p_idusuario,
4880 'RETIRADO ADICIONAR REFERENTE MUDANCA DE PICKING ID: ' ||
4881 p_idremanejamento);
4882
4883 select count(*)
4884 into v_totallr
4885 from loteremanejamento lr
4886 where lr.idremanejamento = c_rem.idremanejamento;
4887
4888 if (v_totallr = 0) then
4889 pk_utilities.GeraLog(p_idusuario,
4890 'MUDANÇA DE PICKING ID ' ||
4891 p_idremanejamento || '. REMANEJAMENTO ' ||
4892 c_rem.idremanejamento ||
4893 ' EXCLUIDO DEVIDO NÃO POSSUIR MAIS NENHUM LOTE.',
4894 p_idremanejamento, 'MP');
4895
4896 delete from remanejamento r
4897 where r.idremanejamento = c_rem.idremanejamento;
4898 end if;
4899 -- se a origem do remanejamento não é o destino da mudanca de picking,
4900 -- o destino do remanejamento deverá ser o mesmo destino da mudança de picking
4901 else
4902 select count(distinct lt.idproduto)
4903 into v_totaltitulos
4904 from loteremanejamento lr, lote lt
4905 where lr.idremanejamento = c_rem.idremanejamento
4906 and lt.idlote = lr.idlote;
4907
4908 if (v_totaltitulos = 1) then
4909 pk_utilities.GeraLog(p_idusuario,
4910 'MUDANÇA DE PICKING ID ' ||
4911 p_idremanejamento ||
4912 ' ALTEROU REMANEJAMENTO ' ||
4913 c_rem.idremanejamento ||
4914 '. LOCAL DE DESTINO TROCADO. DESTINO ANTERIOR ' ||
4915 c_rem.idlocaldestino || ', NOVO DESTINO ' ||
4916 p_idlocaldestino, p_idremanejamento, 'MP');
4917
4918 update remanejamento r
4919 set r.idlocaldestino = p_idlocaldestino
4920 where r.idremanejamento = c_rem.idremanejamento;
4921 -- se o remanejamento possuir lotes de outros produtos,
4922 -- deverá ser criado um novo remanejamento para mover
4923 -- somente os lotes do produto que participa da mudança
4924 -- de picking
4925 else
4926 delete from loteremanejamento lr
4927 where lr.idremanejamento = c_rem.idremanejamento
4928 and lr.idlote = c_rem.idlote;
4929
4930 if c_rem.controlaqtde = 'S' then
4931 update remanejamento
4932 set qtde = nvl(qtde, 0) - c_rem.qtde
4933 where idremanejamento = c_rem.idremanejamento;
4934 end if;
4935
4936 v_idnovorem := cadastrar_remanejamento(p_idarmazem, p_idarmazem,
4937 c_rem.idlocalorigem,
4938 p_idlocaldestino,
4939 p_idusuario,
4940 c_rem.idromaneio,
4941 'REMANEJAMENTO CRIADO DEVIDO A MUDANCA DE PICKING ID: ' ||
4942 p_idremanejamento, 'N',
4943 'N');
4944
4945 select r.tipo
4946 into v_tiponovorem
4947 from remanejamento r
4948 where r.idremanejamento = v_idnovorem;
4949
4950 update remanejamento r
4951 set r.tipo = 3
4952 where r.idremanejamento = v_idnovorem;
4953
4954 begin
4955 insert into loteremanejamento
4956 (idremanejamento, idlote, qtde, conferido, controlaqtde)
4957 values
4958 (v_idnovorem, c_rem.idlote, c_rem.qtde, 'N', 'S');
4959 exception
4960 when dup_val_on_index then
4961 update loteremanejamento lr
4962 set lr.qtde = lr.qtde + c_rem.qtde
4963 where lr.idremanejamento = v_idnovorem
4964 and lr.idlote = c_rem.idlote;
4965 end;
4966
4967 update remanejamento r
4968 set r.tipo = v_tiponovorem,
4969 r.qtde = decode(c_rem.controlaqtde, 'S',
4970 nvl(r.qtde, 0) - c_rem.qtde, r.qtde)
4971 where r.idremanejamento = v_idnovorem;
4972
4973 pk_utilities.GeraLog(p_idusuario,
4974 'MUDANÇA DE PICKING ID ' ||
4975 p_idremanejamento ||
4976 ' ALTEROU O REMANEJAMENTO ' ||
4977 c_rem.idremanejamento || '. LOTE ' ||
4978 c_rem.idlote ||
4979 ' FOI REMOVIDO E ADICIONADO NO REMANEJAMENTO ' ||
4980 v_idnovorem, p_idremanejamento, 'MP');
4981 end if;
4982 end if;
4983 end if;
4984
4985 update remanejamento r
4986 set r.tipo = v_tiporem
4987 where r.idremanejamento = c_rem.idremanejamento;
4988 end loop;
4989 end alterarRemMudPicking;
4990
4991 procedure alterarAjusteMudPicking
4992 (
4993 p_idusuario in number,
4994 p_iddepositante in number,
4995 p_idproduto in number,
4996 p_idArmazem in number,
4997 p_idlocalorigem in local.idlocal%type,
4998 p_idlocaldestino in local.idlocal%type,
4999 p_idremanejamento in number
5000 ) is
5001 v_idajuste number;
5002 begin
5003 v_idajuste := 0;
5004 for c_aje in (select ae.idajustemovtoentrada, ae.idajustemovto
5005 from ajustemovto ajm, ajustemovtoentrada ae
5006 where ajm.gerado = 'N'
5007 and ajm.identidade = p_iddepositante
5008 and ae.idajustemovto = ajm.idajustemovto
5009 and ae.idarmazem = p_idarmazem
5010 and ae.idlocal = p_idlocalorigem
5011 and ae.idproduto = p_idproduto)
5012 loop
5013 update ajustemovtoentrada a
5014 set a.idlocal = p_idlocaldestino
5015 where a.idajustemovtoentrada = c_aje.idajustemovtoentrada;
5016
5017 if (v_idajuste <> c_aje.idajustemovto) then
5018 v_idajuste := c_aje.idajustemovto;
5019
5020 pk_utilities.GeraLog(p_idusuario,
5021 'MUDANÇA DE PICKING ID ' || p_idremanejamento ||
5022 ' ALTEROU O AJUSTE DE ENTRADA ' ||
5023 c_aje.idajustemovto ||
5024 '. ENDEREÇO TROCADO. ENDEREÇO ANTERIOR' ||
5025 p_idlocalorigem || ', NOVO ENDEREÇO ' ||
5026 p_idlocaldestino, p_idremanejamento, 'MP');
5027 end if;
5028 end loop;
5029
5030 v_idajuste := 0;
5031 pk_triggers_control.disableTrigger('t_before_ajustemovtosaida');
5032 for c_ajs in (select ajs.idarmazem, ajs.idlocal, ajs.idlote,
5033 ajs.idajustemovto
5034 from ajustemovto ajm, ajustemovtosaida ajs, lote lt
5035 where ajm.gerado = 'N'
5036 and ajm.identidade = p_iddepositante
5037 and ajs.idajustemovto = ajm.idajustemovto
5038 and ajs.idarmazem = p_idarmazem
5039 and ajs.idlocal = p_idlocalorigem
5040 and lt.idlote = ajs.idlote
5041 and lt.idproduto = p_idproduto)
5042 loop
5043 update ajustemovtosaida a
5044 set a.idlocal = p_idlocaldestino
5045 where a.idarmazem = c_ajs.idarmazem
5046 and a.idlocal = c_ajs.idlocal
5047 and a.idlote = c_ajs.idlote
5048 and a.idajustemovto = c_ajs.idajustemovto;
5049
5050 if (v_idajuste <> c_ajs.idajustemovto) then
5051 v_idajuste := c_ajs.idajustemovto;
5052
5053 pk_utilities.GeraLog(p_idusuario,
5054 'MUDANÇA DE PICKING ID ' || p_idremanejamento ||
5055 ' ALTEROU O AJUSTE DE SAIDA ' ||
5056 c_ajs.idajustemovto ||
5057 '. ENDEREÇO TROCADO. ENDEREÇO ANTERIOR' ||
5058 p_idlocalorigem || ', NOVO ENDEREÇO ' ||
5059 p_idlocaldestino, p_idremanejamento, 'MP');
5060 end if;
5061 end loop;
5062 pk_triggers_control.enableTrigger('t_before_ajustemovtosaida');
5063 end;
5064
5065 procedure alterarRomaneioMudPicking
5066 (
5067 p_idusuario in number,
5068 p_iddepositante in number,
5069 p_idproduto in number,
5070 p_idArmazem in number,
5071 p_idlocalorigem in local.idlocal%type,
5072 p_idlocaldestino in local.idlocal%type,
5073 p_idremanejamento in number
5074 ) is
5075 v_paletsepjaexiste number;
5076 begin
5077 delete from gtt_selecao;
5078 for c_lote in (select ps.idarmazem, ps.idlocal, ps.idlote, ps.idproduto,
5079 ps.idromaneio, ps.idpalet, ps.barra, ps.qtde,
5080 ps.qtdeunit
5081 from romaneiopai r, paletseparacao ps, lote lt
5082 where r.tipo = 0
5083 and r.processado = 'N'
5084 and ps.idromaneio = r.idromaneio
5085 and ps.idarmazem = p_idArmazem
5086 and ps.idlocal = p_idlocalorigem
5087 and ps.idproduto = p_idproduto
5088 and lt.idlote = ps.idlote
5089 and lt.iddepositante = p_iddepositante)
5090 loop
5091 insert into gtt_selecao
5092 (idselecionado)
5093 values
5094 (c_lote.idromaneio);
5095
5096 select count(*)
5097 into v_paletsepjaexiste
5098 from paletseparacao ps
5099 where ps.idarmazem = c_lote.idarmazem
5100 and ps.idlocal = p_idlocaldestino
5101 and ps.idlote = c_lote.idlote
5102 and ps.idproduto = c_lote.idproduto
5103 and ps.idromaneio = c_lote.idromaneio
5104 and ps.idpalet = c_lote.idpalet
5105 and ps.barra = c_lote.barra;
5106
5107 if (v_paletsepjaexiste = 0) then
5108 update paletseparacao ps
5109 set ps.idlocal = p_idlocaldestino
5110 where ps.idarmazem = c_lote.idarmazem
5111 and ps.idlocal = c_lote.idlocal
5112 and ps.idlote = c_lote.idlote
5113 and ps.idproduto = c_lote.idproduto
5114 and ps.idromaneio = c_lote.idromaneio
5115 and ps.idpalet = c_lote.idpalet
5116 and ps.barra = c_lote.barra;
5117
5118 pk_utilities.GeraLog(p_idusuario,
5119 'MUDANÇA DE PICKING ID ' || p_idremanejamento ||
5120 ' ALTEROU ROMANEIO ' || c_lote.idromaneio ||
5121 '. ENDEREÇO DO PALETSEPARACAO (idarmazem ' ||
5122 c_lote.idarmazem || ', idlocal ' ||
5123 c_lote.Idlocal || ', idlote ' || c_lote.idlote ||
5124 ', idproduto ' || c_lote.idproduto ||
5125 ', idromaneio ' || c_lote.idromaneio ||
5126 ', idpalet ' || c_lote.idpalet || ', barra ' ||
5127 c_lote.barra ||
5128 ') TROCADO. ENDEREÇO ANTERIOR ' ||
5129 c_lote.idlocal || ', NOVO ENDEREÇO ' ||
5130 p_idlocaldestino, p_idremanejamento, 'MP');
5131 else
5132 update paletseparacao ps
5133 set ps.qtde = ps.qtde + c_lote.qtde,
5134 ps.qtdeunit = ps.qtdeunit + c_lote.qtdeunit
5135 where ps.idarmazem = c_lote.idarmazem
5136 and ps.idlocal = p_idlocaldestino
5137 and ps.idlote = c_lote.idlote
5138 and ps.idproduto = c_lote.idproduto
5139 and ps.idromaneio = c_lote.idromaneio
5140 and ps.idpalet = c_lote.idpalet
5141 and ps.barra = c_lote.barra;
5142
5143 delete from paletseparacao ps
5144 where ps.idarmazem = c_lote.idarmazem
5145 and ps.idlocal = c_lote.idlocal
5146 and ps.idlote = c_lote.idlote
5147 and ps.idproduto = c_lote.idproduto
5148 and ps.idromaneio = c_lote.idromaneio
5149 and ps.idpalet = c_lote.idpalet
5150 and ps.barra = c_lote.barra;
5151
5152 pk_utilities.GeraLog(p_idusuario,
5153 'MUDANÇA DE PICKING ID ' || p_idremanejamento ||
5154 ' ALTEROU O ROMANEIO ' || c_lote.idromaneio ||
5155 '. QUANTIDADE DO PRODUTO NO PALETSEPARACAO (idarmazem ' ||
5156 c_lote.idarmazem || ', idlocal ' ||
5157 c_lote.Idlocal || ', idlote ' || c_lote.idlote ||
5158 ', idproduto ' || c_lote.idproduto ||
5159 ', idromaneio ' || c_lote.idromaneio ||
5160 ', idpalet ' || c_lote.idpalet || ', barra ' ||
5161 c_lote.barra ||
5162 ') MOVIDA PARA O PALETSEPARACAO EXISTENTE (idarmazem ' ||
5163 c_lote.idarmazem || ', idlocal ' ||
5164 p_idlocaldestino || ', idlote ' ||
5165 c_lote.idlote || ', idproduto ' ||
5166 c_lote.idproduto || ', idromaneio ' ||
5167 c_lote.idromaneio || ', idpalet ' ||
5168 c_lote.idpalet || ', barra ' || c_lote.barra || ')' ||
5169 '. QTDEUNIT AUMENTADA ' || c_lote.Qtdeunit ||
5170 ', QTDE AUMENTADA ' || c_lote.qtde,
5171 p_idremanejamento, 'MP');
5172 end if;
5173 end loop;
5174
5175 for c_rom in (select distinct idselecionado
5176 from gtt_selecao)
5177 loop
5178 pk_romaneio.RecalculaPaletSepCliente(c_rom.idselecionado);
5179 pk_romaneio.identificarlotes(c_rom.idselecionado);
5180 end loop;
5181 end;
5182
5183 procedure alterarAlocacaoMudPicking
5184 (
5185 p_idusuario in number,
5186 p_iddepositante in number,
5187 p_idproduto in number,
5188 p_idArmazem in number,
5189 p_idlocalorigem in local.idlocal%type,
5190 p_idlocaldestino in local.idlocal%type,
5191 p_idremanejamento in number
5192 ) is
5193 begin
5194 for c_mapa in (select m.idalocacao
5195 from mapaalocacao m, lote lt
5196 where m.status <> 'F'
5197 and m.idarmazem = p_idArmazem
5198 and m.idlocal = p_idlocalorigem
5199 and lt.idlote = m.idlote
5200 and lt.idproduto = p_idproduto
5201 and lt.iddepositante = p_iddepositante)
5202 loop
5203 update mapaalocacao
5204 set idlocal = p_idlocaldestino
5205 where idalocacao = c_mapa.idalocacao;
5206
5207 pk_utilities.GeraLog(p_idusuario,
5208 'MUDANÇA DE PICKING ID ' || p_idremanejamento ||
5209 ' ALTEROU O MAPA DE ALOCACAO ' ||
5210 c_mapa.Idalocacao ||
5211 '. ENDEREÇO TROCADO. ENDEREÇO ANTERIOR ' ||
5212 p_idlocalorigem || ', NOVO ENDEREÇO ' ||
5213 p_idlocaldestino, p_idremanejamento, 'MP');
5214 end loop;
5215
5216 end;
5217
5218 procedure alterarOndaMudPicking
5219 (
5220 p_idusuario in number,
5221 p_iddepositante in number,
5222 p_idproduto in number,
5223 p_idArmazem in number,
5224 p_idlocalorigem in local.idlocal%type,
5225 p_idlocaldestino in local.idlocal%type,
5226 p_idremanejamento in number
5227 ) is
5228 v_movanterior number;
5229 v_idlocalanterior local.idlocal%type;
5230 v_idnovolocal number;
5231 begin
5232 select id
5233 into v_idnovolocal
5234 from local
5235 where idarmazem = p_idArmazem
5236 and idlocal = p_idlocaldestino;
5237
5238 for c_mov in (select m.idonda, m.id, lt.idlote, lo.idlocal,
5239 m.qtdemovimentada, m.etapa, m.idlocalorigem
5240 from movimentacao m, local lo, lote lt
5241 where m.status in (0, 1)
5242 and lo.id = m.idlocalorigem
5243 and lo.idarmazem = p_idArmazem
5244 and lo.idlocal = p_idlocalorigem
5245 and lt.idlote = m.idlote
5246 and lt.idproduto = p_idproduto
5247 and lt.iddepositante = p_iddepositante)
5248 loop
5249 begin
5250 select m.id
5251 into v_movanterior
5252 from grupomovimentacao g, movimentacao m
5253 where g.idgrupo = c_mov.id
5254 and m.id = g.idmovimentacao
5255 and m.etapa < c_mov.etapa
5256 and m.idlocaldestino = c_mov.idlocal
5257 and m.status in (0, 1);
5258 exception
5259 when no_data_found then
5260 v_movanterior := 0;
5261 end;
5262
5263 if (v_movanterior > 0) then
5264 select ld.idlocal
5265 into v_idlocalanterior
5266 from movimentacao m, local ld
5267 where m.id = v_movanterior
5268 and ld.id = m.idlocaldestino;
5269
5270 update movimentacao m
5271 set m.idlocaldestino = v_idnovolocal
5272 where m.id = v_movanterior;
5273
5274 pk_utilities.GeraLog(p_idusuario,
5275 'MUDANÇA DE PICKING ID ' || p_idremanejamento ||
5276 ' ALTEROU ONDA ' || c_mov.idonda ||
5277 ', MOVIMENTACAO ID ' || v_movanterior ||
5278 '. ENDEREÇO DE DESTINO ANTERIOR ' ||
5279 v_idlocalanterior || ', NOVO ENDEREÇO ' ||
5280 p_idlocaldestino, p_idremanejamento, 'MP');
5281 end if;
5282
5283 update movimentacao m
5284 set m.idlocalorigem = v_idnovolocal
5285 where m.id = c_mov.id;
5286
5287 pk_utilities.GeraLog(p_idusuario,
5288 'MUDANÇA DE PICKING ID ' || p_idremanejamento ||
5289 ' ALTEROU ONDA ' || c_mov.idonda ||
5290 ', MOVIMENTACAO ID ' || c_mov.id ||
5291 '. ENDEREÇO DE ORIGEM ANTERIOR ' ||
5292 c_mov.idlocal || ', NOVO ENDEREÇO ' ||
5293 p_idlocaldestino, p_idremanejamento, 'MP');
5294 end loop;
5295
5296 for c_mov in (select m.idonda, m.id, lt.idlote, ld.idlocal,
5297 m.qtdemovimentada, m.etapa, m.idlocalorigem
5298 from movimentacao m, local ld, lote lt
5299 where m.status in (0, 1)
5300 and ld.id = m.idlocaldestino
5301 and ld.idarmazem = p_idArmazem
5302 and ld.idlocal = p_idlocalorigem
5303 and lt.idlote = m.idlote
5304 and lt.idproduto = p_idproduto
5305 and lt.iddepositante = p_iddepositante)
5306 loop
5307 update movimentacao m
5308 set m.idlocaldestino = v_idnovolocal
5309 where m.id = c_mov.id;
5310
5311 pk_utilities.GeraLog(p_idusuario,
5312 'MUDANÇA DE PICKING ID ' || p_idremanejamento ||
5313 ' ALTEROU ONDA ' || c_mov.idonda ||
5314 ', MOVIMENTACAO ID ' || c_mov.id ||
5315 '. ENDEREÇO DE DESTINO ANTERIOR ' ||
5316 c_mov.idlocal || ', NOVO ENDEREÇO ' ||
5317 p_idlocaldestino, p_idremanejamento, 'MP');
5318 end loop;
5319 end;
5320
5321 procedure alterarCorteFisicoMudPicking
5322 (
5323 p_idusuario in number,
5324 p_iddepositante in number,
5325 p_idproduto in number,
5326 p_idArmazem in number,
5327 p_idlocalorigem in local.idlocal%type,
5328 p_idlocaldestino in local.idlocal%type,
5329 p_idremanejamento in number
5330 ) is
5331 v_idnovolocal number;
5332 begin
5333 select id
5334 into v_idnovolocal
5335 from local
5336 where idarmazem = p_idArmazem
5337 and idlocal = p_idlocaldestino;
5338
5339 for c_corte in (select c.id idcorte
5340 from cortefisico c, local lo
5341 where c.status = 0
5342 and c.idproduto = p_idproduto
5343 and lo.id = c.idenderecofalta
5344 and lo.idarmazem = p_idArmazem
5345 and lo.idlocal = p_idlocalorigem
5346 and exists
5347 (select 1
5348 from cortefisiconf cnf, notafiscal nf
5349 where cnf.idcortefisico = c.id
5350 and cnf.qtdeseparacao <> cnf.qtdeutilizada
5351 and nf.idnotafiscal = cnf.idnotafiscal
5352 and nf.iddepositante = p_iddepositante))
5353 loop
5354 update cortefisico c
5355 set c.idenderecofalta = v_idnovolocal
5356 where c.id = c_corte.idcorte;
5357
5358 update movimentacao m
5359 set m.idlocalorigem = v_idnovolocal
5360 where exists (select 1
5361 from cortefisiconf cnf, resestoquecortefisiconf r
5362 where cnf.idcortefisico = c_corte.idcorte
5363 and cnf.qtdeseparacao <> cnf.qtdeutilizada
5364 and r.idcortefisiconf = cnf.id
5365 and r.idmovimentacaoafetada = m.id);
5366
5367 pk_utilities.GeraLog(p_idusuario,
5368 'MUDANÇA DE PICKING ID ' || p_idremanejamento ||
5369 ' ALTEROU CORTE FÃSICO ID ' || c_corte.idcorte ||
5370 '. ENDEREÇO DE DESTINO ANTERIOR ' ||
5371 p_idlocalorigem || ', NOVO ENDEREÇO ' ||
5372 p_idlocaldestino, p_idremanejamento, 'MP');
5373 end loop;
5374
5375 -- Atualizar local de corte para os cortes de separação e conferência da carga
5376 for c_cortesep in (select c.id idcorte
5377 from cortefisico c, local lo, lote lt, lotelocal ll
5378 where c.status = 0
5379 and c.idproduto = p_idproduto
5380 and ll.idarmazem = lo.idarmazem
5381 and ll.idlocal = lo.idlocal
5382 and ll.idlote = lt.idlote
5383 and lt.idproduto = c.idproduto
5384 and lt.iddepositante = p_iddepositante
5385 and lo.id = c.idenderecofalta
5386 and lo.idarmazem = p_idArmazem
5387 and lo.idlocal = p_idlocalorigem
5388 and (c.idconfexpedicaocarga is not null or exists
5389 (select 1
5390 from separacaoporcarga s
5391 where s.idcortefisico = c.id)))
5392 loop
5393 update cortefisico c
5394 set c.idenderecofalta = v_idnovolocal
5395 where c.id = c_cortesep.idcorte;
5396
5397 update separacaoporcarga
5398 set idlocal = p_idlocaldestino
5399 where idcortefisico = c_cortesep.idcorte;
5400
5401 update paleteondanf pn
5402 set pn.idlocal = p_idlocaldestino
5403 where exists (select 1
5404 from separacaoporcarga s, paleteondanf p,
5405 reservaestoqueondacarga r
5406 where r.idpaleteondanf = pn.idpaleteondanf
5407 and r.idpaleteondanf = p.idpaleteondanf
5408 and r.idseparacaoporcarga = s.idseparacaoporcarga
5409 and s.idcortefisico = c_cortesep.idcorte);
5410
5411 update reservaestoqueondacarga re
5412 set re.idlocal = p_idlocaldestino
5413 where exists (select 1
5414 from separacaoporcarga s, paleteondanf p,
5415 reservaestoqueondacarga r
5416 where r.idseparacaoporcarga = re.idseparacaoporcarga
5417 and r.idpaleteondanf = p.idpaleteondanf
5418 and r.idseparacaoporcarga = s.idseparacaoporcarga
5419 and s.idcortefisico = c_cortesep.idcorte);
5420
5421 pk_utilities.GeraLog(p_idusuario,
5422 'MUDANÇA DE PICKING ID ' || p_idremanejamento ||
5423 ' ALTEROU CORTE FÃSICO ID ' ||
5424 c_cortesep.idcorte ||
5425 '. ENDEREÇO DE DESTINO ANTERIOR ' ||
5426 p_idlocalorigem || ', NOVO ENDEREÇO ' ||
5427 p_idlocaldestino, p_idremanejamento, 'MP');
5428 end loop;
5429 end;
5430
5431 procedure alteraratividadesMudPicking
5432 (
5433 p_idusuario in number,
5434 p_iddepositante in number,
5435 p_idproduto in number,
5436 p_idArmazem in number,
5437 p_idlocalorigem in local.idlocal%type,
5438 p_idlocaldestino in local.idlocal%type,
5439 p_idremanejamento in number
5440 ) is
5441
5442 procedure deletarAtividadeInutilizada(p_codonda in number) is
5443 begin
5444 delete from atividade a
5445 where a.idtipoatividade = 9
5446 and a.idoperacao = p_codonda
5447 and status <> 'F'
5448 and exists
5449 (select 1
5450 from romaneiopai rp, movimentacao m, local lo,
5451 regiaoarmazenagem ro, local ld, regiaoarmazenagem rd,
5452 tipoatividadearmazem ta
5453 where rp.idromaneio = a.idoperacao
5454 and lo.idregiao = a.idregiaoorigem
5455 and lo.buffer = a.bufferorigem
5456 and ld.idregiao = a.idregiaodestino
5457 and ld.buffer = a.bufferdestino
5458 and m.idonda = rp.idromaneio
5459 and lo.id = m.idlocalorigem
5460 and ro.idregiao = lo.idregiao
5461 and ld.id = m.idlocaldestino
5462 and rd.idregiao = ld.idregiao
5463 and ta.idarmazem = lo.idarmazem
5464 and ta.idtipoatividade = 9 -- tipo separação por onda
5465 and m.status in (0, 1)
5466 and not (m.qtdemovimentada - m.qtdeconferida = 0 and
5467 m.status = 2)
5468 and (ld.idregiao not in
5469 (select idregiao
5470 from regiaoarmazenagem
5471 where tipo = decode(ro.tipo, 7, null, 3)) or
5472 (lo.buffer = 'N' and ld.buffer = 'N' and
5473 ro.tipo in (0, 1) and rd.tipo = 3))
5474 and not
5475 (lo.buffer = 'S' and
5476 lo.idregiao in (select idregiao
5477 from regiaoarmazenagem
5478 where tipo = 2) and ld.buffer = 'N' and
5479 ld.idregiao in (select idregiao
5480 from regiaoarmazenagem
5481 where tipo = 2))
5482 and not
5483 (lo.buffer = 'N' and m.status = 1 and
5484 lo.idregiao in (select idregiao
5485 from regiaoarmazenagem
5486 where tipo = 0) and ld.buffer = 'N' and
5487 ld.idregiao in (select idregiao
5488 from regiaoarmazenagem
5489 where tipo = 2))
5490 group by ro.idregiao, ro.descr, lo.buffer, rd.idregiao,
5491 rd.descr, ld.buffer, ro.tipo, rd.tipo, lo.tipo,
5492 lo.picking, ld.tipo, ld.picking,
5493 decode(ro.tipo, 6, 99, 1, 0, 0, 1, ro.tipo));
5494 end;
5495
5496 begin
5497 for c_onda in (select o.idromaneio idonda, o.codigointerno codonda,
5498 o.tituloromaneio titulo, o.idconfiguracaoonda
5499 from romaneiopai o
5500 where exists (select 1
5501 from movimentacao m, local lo, local ld
5502 where lo.id = m.idlocalorigem
5503 and ld.id = m.idlocaldestino
5504 and m.idonda = o.idromaneio
5505 and ((ld.idlocal = p_idlocaldestino and
5506 ld.idarmazem = p_idArmazem) or
5507 (lo.idlocal = p_idlocaldestino and
5508 lo.idarmazem = p_idArmazem)))
5509 and o.statusonda in (2, 4)
5510 and o.tipo = 1
5511 group by o.idromaneio, o.codigointerno, o.tituloromaneio,
5512 o.idconfiguracaoonda)
5513 loop
5514 deletarAtividadeInutilizada(c_onda.codonda);
5515
5516 pk_convocacao.insereSeparacaoOnda(p_idusuario, c_onda.idonda,
5517 c_onda.codonda, p_idArmazem,
5518 c_onda.idconfiguracaoonda,
5519 c_onda.titulo || ' - Onda');
5520
5521 pk_utilities.GeraLog(p_idusuario,
5522 'MUDANÇA DE PICKING ID ' || p_idremanejamento ||
5523 ' ALTEROU ATIVIDADE DE CONVOCAÇÃO ATIVA PARA O IDONDA: ' ||
5524 c_onda.idonda ||
5525 '. ENDEREÇO DE DESTINO ANTERIOR ' ||
5526 p_idlocalorigem || ', NOVO ENDEREÇO ' ||
5527 p_idlocaldestino, p_idremanejamento, 'MP');
5528 end loop;
5529 end;
5530
5531 procedure alterarOndaCargaMudPicking
5532 (
5533 p_idusuario in number,
5534 p_iddepositante in number,
5535 p_idproduto in number,
5536 p_idArmazem in number,
5537 p_idlocalorigem in local.idlocal%type,
5538 p_idlocaldestino in local.idlocal%type,
5539 p_idremanejamento in number
5540 ) is
5541 begin
5542 for c_carga in (select re.idonda, re.idseparacaoporcarga,
5543 re.idpaleteondanf
5544 from separacaoporcarga sc, paleteondanf pf,
5545 reservaestoqueondacarga re, lote lt
5546 where lt.idproduto = p_idproduto
5547 and lt.iddepositante = p_iddepositante
5548 and lt.idlote = re.idlote
5549 and re.idlote = pf.idlote
5550 and re.idpaleteondanf = pf.idpaleteondanf
5551 and re.idseparacaoporcarga = sc.idseparacaoporcarga
5552 and sc.idarmazem = p_idArmazem
5553 and sc.idlocal = p_idlocalorigem
5554 and sc.status in (0, 1))
5555 loop
5556 update separacaoporcarga
5557 set idlocal = p_idlocaldestino
5558 where idseparacaoporcarga = c_carga.idseparacaoporcarga;
5559
5560 update paleteondanf
5561 set idlocal = p_idlocaldestino
5562 where idpaleteondanf = c_carga.idpaleteondanf;
5563
5564 update reservaestoqueondacarga
5565 set idlocal = p_idlocaldestino
5566 where idseparacaoporcarga = c_carga.idseparacaoporcarga;
5567
5568 pk_utilities.GeraLog(p_idusuario,
5569 'MUDANÇA DE PICKING ID ' || p_idremanejamento ||
5570 ' ALTEROU ONDA ' || c_carga.idonda ||
5571 ', SEPARAÇÃO CARGA ID ' ||
5572 c_carga.idseparacaoporcarga ||
5573 '. ENDEREÇO DE DESTINO ANTERIOR ' ||
5574 p_idlocalorigem || ', NOVO ENDEREÇO ' ||
5575 p_idlocaldestino, p_idremanejamento, 'MP');
5576 end loop;
5577 end;
5578
5579
5580 procedure processarMudancaPicking
5581 (
5582 p_idusuario in number,
5583 p_iddepositante in number,
5584 p_idproduto in number,
5585 p_idArmazem in number,
5586 p_idlocalorigem in local.idlocal%type,
5587 p_idlocaldestino in local.idlocal%type
5588 ) is
5589 v_transfPendenciaAdicionar number;
5590 v_mudarPickingVazio number;
5591 v_idremanejamento number;
5592 v_textolog varchar2(100);
5593 v_estoque number;
5594 v_pendencia number;
5595 v_adicionar number;
5596 v_msg t_message;
5597
5598 procedure carregaDados is
5599 begin
5600
5601 select a.transferirpendenciaeadicionar, a.mudarpickingvazio
5602 into v_transfPendenciaAdicionar, v_mudarPickingVazio
5603 from armazem a
5604 where a.idarmazem = p_idArmazem;
5605
5606 if (v_transfPendenciaAdicionar = 1) then
5607 v_textolog := ' MUDANÇA DE PICKING MOVENDO ESTOQUE RESERVADO: ';
5608 else
5609 v_textolog := ' MUDANÇA DE PICKING: ';
5610 end if;
5611 end carregaDados;
5612
5613 procedure validacoes is
5614 begin
5615
5616 validarMudancaPicking(p_iddepositante, p_idproduto, p_idArmazem,
5617 p_idlocalorigem, v_transfPendenciaAdicionar,
5618 p_idlocaldestino, v_mudarPickingVazio);
5619
5620 end validacoes;
5621
5622 procedure iniciaMudancaDePicking is
5623
5624 procedure gravaLog(p_etapa varchar2) is
5625 begin
5626 pk_utilities.GeraLog(p_idusuario,
5627 p_etapa || v_textolog || v_idremanejamento ||
5628 ', IDPRODUTO ' || p_idproduto ||
5629 ', IDDEPOSITANTE ' || p_iddepositante ||
5630 ', LOCAL ORIGEM ' || p_idlocalorigem ||
5631 ', LOCAL DESTINO ' || p_idlocaldestino ||
5632 ', REALIZADA PELO USUARIO ' || p_idusuario,
5633 v_idremanejamento, 'MP');
5634 end gravaLog;
5635
5636 procedure transferirPendenciaPicking is
5637 begin
5638 alterarAjusteMudPicking(p_idusuario, p_iddepositante, p_idproduto,
5639 p_idArmazem, p_idlocalorigem,
5640 p_idlocaldestino, v_idremanejamento);
5641
5642 moverEstoqueMudPicking(p_idusuario, p_idArmazem, p_idlocalorigem,
5643 p_idlocaldestino, v_transfPendenciaAdicionar,
5644 v_idremanejamento);
5645
5646 alterarRemMudPicking(p_idusuario, p_iddepositante, p_idproduto,
5647 p_idArmazem, p_idlocalorigem, p_idlocaldestino,
5648 v_idremanejamento);
5649
5650 alterarRomaneioMudPicking(p_idusuario, p_iddepositante, p_idproduto,
5651 p_idArmazem, p_idlocalorigem,
5652 p_idlocaldestino, v_idremanejamento);
5653
5654 alterarAlocacaoMudPicking(p_idusuario, p_iddepositante, p_idproduto,
5655 p_idArmazem, p_idlocalorigem,
5656 p_idlocaldestino, v_idremanejamento);
5657
5658 alterarOndaMudPicking(p_idusuario, p_iddepositante, p_idproduto,
5659 p_idArmazem, p_idlocalorigem, p_idlocaldestino,
5660 v_idremanejamento);
5661
5662 alterarCorteFisicoMudPicking(p_idusuario, p_iddepositante,
5663 p_idproduto, p_idArmazem,
5664 p_idlocalorigem, p_idlocaldestino,
5665 v_idremanejamento);
5666
5667 alteraratividadesMudPicking(p_idusuario, p_iddepositante,
5668 p_idproduto, p_idArmazem,
5669 p_idlocalorigem, p_idlocaldestino,
5670 v_idremanejamento);
5671
5672 alterarOndaCargaMudPicking(p_idusuario, p_iddepositante,
5673 p_idproduto, p_idArmazem, p_idlocalorigem,
5674 p_idlocaldestino, v_idremanejamento);
5675 end transferirPendenciaPicking;
5676
5677 procedure alteraRemanejamentoGerado(p_idremanejamento number) is
5678 begin
5679
5680 pk_triggers_control.disableTrigger('T_BEFORE_REMANEJAMENTO');
5681
5682 update remanejamento r
5683 set r.planejado = 'S',
5684 r.adicionado = 'S',
5685 r.idusuario = p_idusuario,
5686 r.status = 'F',
5687 r.horafim = sysdate
5688 where r.idremanejamento = p_idremanejamento;
5689
5690 pk_triggers_control.enableTrigger('T_BEFORE_REMANEJAMENTO');
5691
5692 end alteraRemanejamentoGerado;
5693
5694 procedure verificaMovimentacaoEstoque is
5695 begin
5696 SELECT NVL(SUM(LL.ESTOQUE), 0) ESTOQUE,
5697 NVL(SUM(LL.PENDENCIA), 0) PENDENCIA,
5698 NVL(SUM(LL.ADICIONAR), 0) ADICIONAR
5699 into v_estoque, v_pendencia, v_adicionar
5700 FROM LOCAL LO, LOTE LT, LOTELOCAL LL, ENTIDADE D, PRODUTO P
5701 WHERE P.IDPRODUTO = LT.IDPRODUTO
5702 AND D.IDENTIDADE = LT.IDDEPOSITANTE
5703 AND LT.IDPRODUTO = p_idProduto
5704 AND LT.IDDEPOSITANTE = p_idDepositante
5705 AND LL.IDLOTE = LT.IDLOTE
5706 AND LL.IDLOCAL = LO.IDLOCAL
5707 AND LL.IDARMAZEM = LO.IDARMAZEM
5708 AND LO.IDARMAZEM = p_idArmazem
5709 AND LO.IDLOCAL = p_idLocalOrigem;
5710
5711 if (v_estoque > 0 or v_pendencia > 0 or v_adicionar > 0) then
5712 v_msg := t_message('Não foi possÃvel mover todo estoque do produto para novo picking.');
5713 raise_application_error(-20000, v_msg.formatMessage);
5714 end if;
5715 end verificaMovimentacaoEstoque;
5716
5717 begin
5718 gravaLog('INICIOU');
5719
5720 criarRemMudancaPicking(p_idusuario, p_iddepositante, p_idproduto,
5721 p_idArmazem, p_idlocalorigem, p_idlocaldestino,
5722 v_transfPendenciaAdicionar, v_idremanejamento);
5723
5724 if (v_transfPendenciaAdicionar = 1) then
5725 transferirPendenciaPicking;
5726 else
5727 moverEstoqueMudPicking(p_idusuario, p_idArmazem, p_idlocalorigem,
5728 p_idlocaldestino, v_transfPendenciaAdicionar,
5729 v_idremanejamento);
5730 end if;
5731
5732 validarFinalizarRemanejamento(v_idremanejamento);
5733
5734 alteraRemanejamentoGerado(v_idremanejamento);
5735
5736 verificaMovimentacaoEstoque;
5737
5738 gravaLog('FINALIZOU');
5739 end iniciaMudancaDePicking;
5740
5741 begin
5742
5743
5744
5745 pk_locks.executeLock(p_idArmazem, 7);
5746
5747 carregaDados;
5748
5749 validacoes;
5750
5751 iniciaMudancaDePicking;
5752
5753
5754 end;
5755
5756 /*
5757 * Funcao que verifica como o remanejamento sera executado
5758 */
5759 function retornarExecRemanejamento(p_idremanejamento in number)
5760 return number is
5761 v_tipoexecucao number;
5762 v_total number;
5763 begin
5764
5765 select count(*)
5766 into v_total
5767 from remanejamento r, local lo, regiaoarmazenagem ro, local ld,
5768 regiaoarmazenagem rd
5769 where r.idremanejamento = p_idremanejamento
5770 and lo.idlocal = r.idlocaldestino
5771 and lo.idarmazem = r.idarmazemdestino
5772 and ro.idregiao = lo.idregiao
5773 and ro.tipo = 1
5774 and ld.idlocal = r.idlocalorigem
5775 and ld.idarmazem = r.idarmazemorigem
5776 and rd.idregiao = ld.idregiao
5777 and rd.tipo = 1
5778 and exists
5779 (select 1
5780 from remanejamentoromaneio rr, romaneiopai rp,
5781 configuracaoonda c
5782 where rr.idremanejamento = r.idremanejamento
5783 and c.idconfiguracaoonda = rp.idconfiguracaoonda
5784 and rp.idromaneio = rr.idromaneio
5785 and (c.idregiaopruadopulmao = ld.idregiao or
5786 c.idregiaopruadopicking = ld.idregiao));
5787
5788 if v_total > 0 then
5789 -- Execucao do remanejamento deverá ser realizada em 2 etapas
5790 v_tipoexecucao := 1;
5791 else
5792 -- Execucao do remanejamento será livre
5793 v_tipoexecucao := 0;
5794 end if;
5795
5796 return v_tipoexecucao;
5797 end;
5798
5799 /*
5800 * Inclui no remanejamento os lotes componentes da montagem
5801 */
5802 procedure verificarReabPendente(p_idremanejamento in number) is
5803 begin
5804 for c in (select lr.idlote, lo.id idEnderecoOrigem
5805 from loteremanejamento lr, remanejamento r, local lo
5806 where lo.idarmazem = r.idarmazemorigem
5807 and lo.idlocal = r.idlocalorigem
5808 and r.idremanejamento = lr.idremanejamento
5809 and lr.conferido = 'N'
5810 and lr.idremanejamento = p_idremanejamento)
5811 loop
5812 insert into gtt_reabpendente
5813 select lt.idproduto, l.id idendereco, r.idremanejamento, 0 idonda,
5814 0 idmovimentacao
5815 from loteremanejamento lr, remanejamento r, lote lt, local l
5816 where lr.idlote = c.idlote
5817 and lr.idremanejamento = r.idremanejamento
5818 and r.status <> 'F'
5819 and r.idremanejamento <> p_idremanejamento
5820 and lr.idlote = lt.idlote
5821 and l.idarmazem = r.idarmazemdestino
5822 and l.idlocal = r.idlocaldestino
5823 and l.id = c.idenderecoorigem;
5824 end loop;
5825 end;
5826
5827 function getQtdeUnitEmbQueCoube
5828 (
5829 p_maxpeso in number,
5830 p_maxcubagem in number,
5831 p_idlote in number,
5832 p_qtde in number,
5833 p_remanejarPorFator in char
5834 ) return number is
5835 v_embalagem embalagem%rowtype;
5836 v_qtdePorPeso_un number;
5837 v_qtdePorCubagem_un number;
5838 v_qtdeQueCoube_un number;
5839 begin
5840 if (p_maxpeso <= 0 or p_maxcubagem <= 0) then
5841 return 0;
5842 end if;
5843
5844 select e.*
5845 into v_embalagem
5846 from lote lt, embalagem e
5847 where lt.idlote = p_idlote
5848 and e.barra = lt.barra
5849 and e.idproduto = lt.idproduto;
5850
5851 v_qtdePorPeso_un := trunc(p_maxpeso / (v_embalagem.pesobruto /
5852 v_embalagem.fatorconversao));
5853
5854 v_qtdePorCubagem_un := trunc(p_maxcubagem /
5855 ((v_embalagem.altura * v_embalagem.largura *
5856 v_embalagem.comprimento) /
5857 v_embalagem.fatorconversao));
5858
5859 if (v_qtdePorPeso_un > v_qtdePorCubagem_un) then
5860 v_qtdeQueCoube_un := v_qtdePorCubagem_un;
5861 else
5862 v_qtdeQueCoube_un := v_qtdePorPeso_un;
5863 end if;
5864
5865 if (v_qtdeQueCoube_un > p_qtde) then
5866 v_qtdeQueCoube_un := p_qtde;
5867 end if;
5868
5869 if (p_remanejarPorFator = 'S') then
5870 if (mod(v_qtdeQueCoube_un, v_embalagem.fatorconversao) <> 0 and
5871 v_qtdeQueCoube_un > v_embalagem.fatorconversao) then
5872 v_qtdeQueCoube_un := 0;
5873 end if;
5874 end if;
5875
5876 return v_qtdeQueCoube_un;
5877 end;
5878
5879 procedure planejarRemanejamentoLote
5880 (
5881 p_idusuario in number,
5882 p_idarmazem in number,
5883 p_idlocalorigem in local.idlocal%type,
5884 p_idlocaldestino in local.idlocal%type,
5885 p_idlote in number,
5886 p_qtde in number,
5887 p_descricao in varchar2,
5888 p_idromaneio in number := null,
5889 p_idnotafiscal in number := null,
5890 p_verificarMaxDimensao in char := 'N',
5891 p_remPorFatorMaxDimensao in char := 'S',
5892 p_idmovimentacao in number := null
5893 ) is
5894 type rec_remlote is record(
5895 idusuario number,
5896 idarmazem number,
5897 idlocalorigem local.idlocal%type,
5898 idlocaldestino local.idlocal%type,
5899 idlote number,
5900 qtde number,
5901 descricao varchar2(1000),
5902 idromaneio number,
5903 maxpeso number,
5904 maxcubagem number,
5905 verificarMaxDimensao char(1),
5906 remanejarPorFator char(1));
5907
5908 r_remlote rec_remlote;
5909 v_qtdedistribuir number;
5910 r_remAberto remanejamento%rowtype;
5911 v_isOrigemBuffer number;
5912 v_msg t_message;
5913
5914 procedure getMaxDimensaoLocal(r_remlote in out rec_remlote) is
5915 begin
5916 select (lo.altura - lo.alturamanobra) * lo.largura * lo.comprimento maxcubagem,
5917 lo.pesomaximo maxpeso
5918 into r_remlote.maxcubagem, r_remlote.maxpeso
5919 from local lo
5920 where lo.idarmazem = r_remlote.idarmazem
5921 and lo.idlocal = r_remlote.idlocaldestino;
5922 end;
5923
5924 procedure inserirLoteRemanejamento
5925 (
5926 r_remlote in rec_remlote,
5927 p_qtde in out number,
5928 p_idremanejamento in number
5929 ) is
5930 begin
5931 begin
5932 insert into loteremanejamento
5933 (idremanejamento, idlote, qtde, conferido, idromaneio)
5934 values
5935 (p_idremanejamento, r_remlote.idlote, p_qtde, 'N',
5936 r_remlote.idromaneio);
5937
5938 Associar(p_idremanejamento, r_remlote.idlote);
5939
5940 exception
5941 when dup_val_on_index then
5942 update loteremanejamento
5943 set qtde = qtde + p_qtde
5944 where idremanejamento = p_idremanejamento
5945 and idlote = r_remlote.idlote;
5946 end;
5947
5948 begin
5949 if (r_remlote.idromaneio is not null) then
5950 insert into remanejamentoromaneio
5951 (idremanejamento, idromaneio, idlote, qtde, data)
5952 values
5953 (p_idremanejamento, r_remlote.idromaneio, r_remlote.idlote,
5954 p_qtde, sysdate);
5955 end if;
5956 exception
5957 when dup_val_on_index then
5958 update remanejamentoromaneio rr
5959 set rr.qtde = rr.qtde + p_qtde,
5960 rr.data = sysdate
5961 where idremanejamento = p_idremanejamento
5962 and idromaneio = r_remlote.idromaneio
5963 and idlote = r_remlote.idlote;
5964 end;
5965
5966 end;
5967
5968 procedure usarRemanejamentoAberto
5969 (
5970 r_remlote in out rec_remlote,
5971 p_qtdedistribuir in out number
5972 ) is
5973 v_pesoocupado number;
5974 v_cubagemocupada number;
5975 v_qtdeQueCoube number;
5976 c_remAberto cursor_remanejamento;
5977 r_remAberto remanejamento%rowtype;
5978 begin
5979 getRemAberto(c_remAberto, r_remAberto, r_remlote.idarmazem,
5980 r_remlote.idlocalorigem, r_remlote.idlocaldestino);
5981 if (c_remAberto%found) then
5982 while (c_remAberto%found)
5983 loop
5984 if (r_remlote.verificarMaxDimensao = 'N') then
5985 inserirLoteRemanejamento(r_remlote, p_qtdedistribuir,
5986 r_remAberto.Idremanejamento);
5987
5988 p_qtdedistribuir := 0;
5989 else
5990 begin
5991 select nvl(sum(lr.qtde * (e.pesobruto / e.fatorconversao)), 0),
5992 nvl(sum(lr.qtde * ((e.altura * e.largura *
5993 e.comprimento) / e.fatorconversao)), 0)
5994 into v_pesoocupado, v_cubagemocupada
5995 from loteremanejamento lr, lote lt, embalagem e
5996 where lr.idremanejamento = r_remAberto.idremanejamento
5997 and lt.idlote = lr.idlote
5998 and e.idproduto = lt.idproduto
5999 and e.barra = lt.barra;
6000 exception
6001 when no_data_found then
6002 v_pesoocupado := 0;
6003 v_cubagemocupada := 0;
6004 end;
6005
6006 v_qtdeQueCoube := getQtdeUnitEmbQueCoube((r_remlote.maxpeso -
6007 v_pesoocupado),
6008 (r_remlote.maxcubagem -
6009 v_cubagemocupada),
6010 r_remlote.idlote,
6011 p_qtdedistribuir,
6012 r_remlote.remanejarPorFator);
6013
6014 if (v_qtdeQueCoube > 0) then
6015 if (v_qtdeQueCoube >= p_qtdedistribuir) then
6016 inserirLoteRemanejamento(r_remlote, p_qtdedistribuir,
6017 r_remAberto.Idremanejamento);
6018
6019 p_qtdedistribuir := 0;
6020 else
6021 inserirLoteRemanejamento(r_remlote, v_qtdeQueCoube,
6022 r_remAberto.Idremanejamento);
6023
6024 insert into gtt_remanejamentogerado
6025 (idremanejamento, idlote, qtdeunit)
6026 values
6027 (r_remAberto.Idremanejamento, r_remlote.idlote,
6028 v_qtdeQueCoube);
6029
6030 p_qtdedistribuir := p_qtdedistribuir - v_qtdeQueCoube;
6031 end if;
6032 end if;
6033 end if;
6034
6035 exit when p_qtdedistribuir = 0;
6036
6037 fetch c_remAberto
6038 into r_remAberto;
6039 end loop;
6040 end if;
6041 close c_remAberto;
6042 end;
6043
6044 procedure verificaLocalOrigemDestino
6045 (
6046 r_remlote in rec_remlote,
6047 p_idnotafiscal in number
6048 ) is
6049 v_regiaoorigem number;
6050 v_regiaodestino number;
6051 v_idproduto produto.idproduto%type;
6052 v_codigointerno produto.codigointerno%type;
6053 v_idnotafiscal notafiscal.idnotafiscal%type;
6054 v_codigointernonf notafiscal.codigointerno%type;
6055 v_sequencia notafiscal.sequencia%type;
6056 v_numpedidofornecedor notafiscal.numpedidofornecedor%type;
6057 begin
6058 select count(1)
6059 into v_regiaoorigem
6060 from regiaoarmazenagem r, local lo
6061 where lo.idregiao = r.idregiao
6062 and lo.idlocal = r_remlote.idlocalorigem
6063 and lo.idarmazem = r_remlote.idarmazem;
6064
6065 if (v_regiaoorigem = 0) then
6066
6067 select p.idproduto, p.codigointerno
6068 into v_idproduto, v_codigointerno
6069 from produto p, lote lt
6070 where lt.idproduto = p.idproduto
6071 and lt.idlote = r_remlote.idlote;
6072
6073 if p_idnotafiscal is not null then
6074 select nf.idnotafiscal, nf.codigointerno, nf.sequencia,
6075 nf.numpedidofornecedor
6076 into v_idnotafiscal, v_codigointernonf, v_sequencia,
6077 v_numpedidofornecedor
6078 from notafiscal nf
6079 where nf.idnotafiscal = p_idnotafiscal;
6080
6081 v_msg := t_message('O Local ({0}' ||
6082 ') de origem não possui região definida.' ||
6083 chr(13) || ' Idproduto: {1}' ||
6084 ', Código Produto: {2}' || chr(13) ||
6085 'Idnotafiscal: {3}, Nota Fiscal: {4}' ||
6086 ', Série: {5}, Pedido: {6}.' || chr(13) ||
6087 'Operação cancelada.');
6088 v_msg.addParam(r_remlote.idlocalorigem);
6089 v_msg.addParam(v_idproduto);
6090 v_msg.addParam(v_codigointerno);
6091 v_msg.addParam(v_idnotafiscal);
6092 v_msg.addParam(v_codigointernonf);
6093 v_msg.addParam(v_sequencia);
6094 v_msg.addParam(v_numpedidofornecedor);
6095 raise_application_error(-20100, v_msg.formatMessage);
6096 else
6097 v_msg := t_message('O Local ({0}' ||
6098 ') de origem não possui região definida. ' ||
6099 chr(13) || ' Idproduto: {1}' ||
6100 ', Código interno: {2}' || chr(13) ||
6101 '.Operação cancelada.');
6102 v_msg.addParam(r_remlote.idlocalorigem);
6103 v_msg.addParam(v_idproduto);
6104 v_msg.addParam(v_codigointerno);
6105 raise_application_error(-20100, v_msg.formatMessage);
6106 end if;
6107
6108 end if;
6109
6110 select count(1)
6111 into v_regiaodestino
6112 from regiaoarmazenagem r, local ld
6113 where ld.idregiao = r.idregiao
6114 and ld.idlocal = r_remlote.idlocaldestino
6115 and ld.idarmazem = r_remlote.idarmazem;
6116
6117 if (v_regiaodestino = 0) then
6118
6119 v_idproduto := null;
6120 v_codigointerno := null;
6121 v_idnotafiscal := null;
6122 v_codigointernonf := null;
6123 v_sequencia := null;
6124 v_numpedidofornecedor := null;
6125
6126 select p.idproduto, p.codigointerno
6127 into v_idproduto, v_codigointerno
6128 from produto p, lote lt
6129 where lt.idproduto = p.idproduto
6130 and lt.idlote = r_remlote.idlote;
6131
6132 select nf.idnotafiscal, nf.codigointerno, nf.sequencia,
6133 nf.numpedidofornecedor
6134 into v_idnotafiscal, v_codigointernonf, v_sequencia,
6135 v_numpedidofornecedor
6136 from notafiscal nf
6137 where nf.idnotafiscal = p_idnotafiscal;
6138
6139 v_msg := t_message('O Local ({0}' ||
6140 ') de destino não possui região definida. ' ||
6141 chr(13) || ' Idproduto: {1}' ||
6142 ', Código Produto: {2}' || chr(13) ||
6143 'Idnotafiscal: {3}, Nota Fiscal: ' ||
6144 '{4}, Série: {5}, Pedido: {6}.' || chr(13) ||
6145 'Operação cancelada.');
6146 v_msg.addParam(r_remlote.idlocaldestino);
6147 v_msg.addParam(v_idproduto);
6148 v_msg.addParam(v_codigointerno);
6149 v_msg.addParam(v_idnotafiscal);
6150 v_msg.addParam(v_codigointernonf);
6151 v_msg.addParam(v_sequencia);
6152 v_msg.addParam(v_numpedidofornecedor);
6153 raise_application_error(-20100, v_msg.formatMessage);
6154
6155 end if;
6156
6157 end;
6158
6159 procedure usarNovoRemanejamento
6160 (
6161 r_remlote in out rec_remlote,
6162 p_qtdedistribuir in out number,
6163 p_idnotafiscal in number
6164 ) is
6165 v_idremanejamento number;
6166 v_qtdeQueCoube number;
6167 v_caixaMovimentacao number;
6168 v_status char(1);
6169
6170 function isCaixaMovimentacao
6171 (
6172 p_idArmazem number,
6173 p_idLocal varchar2
6174 ) return boolean is
6175 v_caixaMovimentacao number;
6176 begin
6177 select count(1) total
6178 into v_caixaMovimentacao
6179 from local l, setor s
6180 where s.idsetor = l.idsetor
6181 and l.tipo = 1
6182 and l.idarmazem = p_idarmazem
6183 and s.usoexclusivocxmov = 1
6184 and l.idlocal = p_idLocal;
6185 return v_caixaMovimentacao > 0;
6186 end;
6187
6188 begin
6189 if (p_qtdedistribuir <= 0) then
6190 return;
6191 end if;
6192
6193 verificaLocalOrigemDestino(r_remlote, p_idnotafiscal);
6194
6195 v_status := c_aguardando;
6196
6197 if (isCaixaMovimentacao(r_remlote.idarmazem, r_remlote.idlocalorigem)) then
6198 v_status := 'G';
6199 end if;
6200
6201 while (p_qtdedistribuir > 0)
6202 loop
6203 select seq_remanejamento.nextval idremanejamento
6204 into v_idremanejamento
6205 from dual;
6206
6207 insert into remanejamento
6208 (idremanejamento, idarmazemorigem, idlocalorigem,
6209 idarmazemdestino, idlocaldestino, datahora, idusuariotela, status,
6210 cadmanual, idromaneio, descrpalet, planejado, idmovimentacao)
6211 values
6212 (v_idremanejamento, r_remlote.idarmazem, r_remlote.idlocalorigem,
6213 r_remlote.idarmazem, r_remlote.idlocaldestino, sysdate,
6214 r_remlote.idusuario, v_status, c_nao, r_remlote.idromaneio,
6215 r_remlote.descricao, 'S', p_idmovimentacao);
6216
6217 if (r_remlote.verificarMaxDimensao = 'N') then
6218 v_qtdeQueCoube := p_qtdedistribuir;
6219 else
6220 v_qtdeQueCoube := getQtdeUnitEmbQueCoube(r_remlote.maxpeso,
6221 r_remlote.maxcubagem,
6222 r_remlote.idlote,
6223 p_qtdedistribuir,
6224 r_remlote.remanejarPorFator);
6225
6226 if (v_qtdeQueCoube = 0) then
6227 v_msg := t_message('O endereço {0}' ||
6228 ' não possui capacidade fÃsica para armazenar uma única embalagem do lote ' ||
6229 '{1}. Operação cancelada.');
6230 v_msg.addParam(r_remlote.idlocaldestino);
6231 v_msg.addParam(r_remlote.idlote);
6232 raise_application_error(-20000, v_msg.formatMessage);
6233 end if;
6234 end if;
6235
6236 inserirLoteRemanejamento(r_remlote, v_qtdeQueCoube,
6237 v_idremanejamento);
6238
6239 insert into gtt_remanejamentogerado
6240 (idremanejamento, idlote, qtdeunit)
6241 values
6242 (v_idremanejamento, r_remlote.idlote, v_qtdeQueCoube);
6243
6244 p_qtdedistribuir := p_qtdedistribuir - v_qtdeQueCoube;
6245 end loop;
6246 end;
6247 begin
6248
6249 select count(*)
6250 into v_isOrigemBuffer
6251 from dual
6252 where exists (select 1
6253 from local lo
6254 where lo.idlocal = p_idlocalorigem
6255 and lo.picking = 'S'
6256 and lo.buffer = 'S');
6257
6258 r_remlote.idusuario := p_idusuario;
6259 r_remlote.idarmazem := p_idarmazem;
6260 r_remlote.idlocalorigem := p_idlocalorigem;
6261 r_remlote.idlocaldestino := p_idlocaldestino;
6262 r_remlote.idlote := p_idlote;
6263 r_remlote.qtde := p_qtde;
6264 r_remlote.descricao := p_descricao;
6265 r_remlote.idromaneio := p_idromaneio;
6266 r_remlote.verificarMaxDimensao := p_verificarMaxDimensao;
6267 r_remlote.remanejarPorFator := p_remPorFatorMaxDimensao;
6268
6269 v_qtdedistribuir := r_remlote.qtde;
6270 if (r_remlote.verificarMaxDimensao = 'S') then
6271 getMaxDimensaoLocal(r_remlote);
6272 end if;
6273
6274 if nvl(v_isOrigemBuffer, 0) > 0 then
6275 usarNovoRemanejamento(r_remlote, v_qtdedistribuir, p_idnotafiscal);
6276 else
6277 usarRemanejamentoAberto(r_remlote, v_qtdedistribuir);
6278 usarNovoRemanejamento(r_remlote, v_qtdedistribuir, p_idnotafiscal);
6279 end if;
6280 end;
6281
6282 function retornarTarefa
6283 (
6284 p_idremanejamento in number,
6285 p_idendereco in number
6286 ) return varchar2 is
6287 v_tarefa varchar2(4000);
6288 begin
6289 select stragg(codbarratarefa)
6290 into v_tarefa
6291 from (select distinct v.codbarratarefa
6292 from remanejamentoromaneio rr, v_tarefas_onda v
6293 where rr.idremanejamento = p_idremanejamento
6294 and v.idonda = rr.idromaneio
6295 and v.idlote = rr.idlote
6296 and v.idlocalorigem = p_idendereco
6297 and v.status < 3);
6298
6299 return v_tarefa;
6300 exception
6301 when no_data_found then
6302 return null;
6303 end;
6304
6305 function retornarQtdeRemPorTarefa(p_codtarefa in varchar2) return number is
6306 v_qtde number;
6307 begin
6308
6309 select count(distinct r.idremanejamento)
6310 into v_qtde
6311 from movimentacao m, loteremanejamento lr, remanejamento r, local l
6312 where lpad(m.idonda, 10, '0') || lpad(m.identificador, 4, '0') =
6313 p_codtarefa
6314 and m.etapa = 1
6315 and lr.idlote = m.idlote
6316 and r.idremanejamento = lr.idremanejamento
6317 and r.status <> 'F'
6318 and (exists (select 1
6319 from remanejamentoromaneio rr
6320 where rr.idremanejamento = r.idremanejamento
6321 and rr.idlote = lr.idlote
6322 and rr.idromaneio <= m.idonda) or
6323 (r.cadmanual = 'N' and r.idromaneio is null and
6324 upper(r.descrpalet) like '%REABASTECIMENTO PARA ONDA%'))
6325 and l.idlocal = r.idlocaldestino
6326 and l.tipo = 0
6327 and l.buffer = 'N';
6328
6329 return v_qtde;
6330
6331 end;
6332
6333 function retornarRemPorTarefa(p_codtarefa in varchar2) return varchar2 is
6334 v_rem varchar2(4000);
6335 begin
6336 select stragg(idremanejamento)
6337 into v_rem
6338 from (select distinct rr.idremanejamento
6339 from v_tarefas_onda v, remanejamentoromaneio rr,
6340 remanejamento r
6341 where v.codbarratarefa = p_codtarefa
6342 and v.status < 3
6343 and rr.idromaneio = v.idonda
6344 and rr.idlote = v.idlote
6345 and r.idremanejamento = rr.idremanejamento
6346 and r.status <> 'F'
6347 order by rr.idremanejamento);
6348
6349 return v_rem;
6350 exception
6351 when no_data_found then
6352 return null;
6353 end;
6354
6355 procedure validarBufPickEsteira
6356 (
6357 p_idRemanejamento in number,
6358 p_telaExecRemanej in number default 0
6359 ) is
6360
6361 C_DESTINOINICIADO CONSTANT CHAR(1) := 'Z';
6362 v_naoFinalizar number;
6363 v_msg t_message;
6364
6365 function isRemanejamentoBufPickEsteira(p_idRemanejamento in number)
6366 return boolean is
6367 v_resultado number;
6368 begin
6369 select count(*)
6370 into v_resultado
6371 from dual
6372 where exists (select 1
6373 from remanejamento r, local lo, local ld, setor sd
6374 where r.idremanejamento = p_idRemanejamento
6375 and r.idarmazemorigem = lo.idarmazem
6376 and r.idlocalorigem = lo.idlocal
6377 and lo.tipo = 0
6378 and lo.buffer = 'S'
6379 and lo.bufferesteira = 1
6380 and r.idlocaldestino = ld.idlocal
6381 and ld.tipo = 0
6382 and ld.buffer = 'N'
6383 and ld.idsetor = sd.idsetor
6384 and sd.codintegracaoesteira is not null
6385 and r.planejado = 'S'
6386 and r.cadmanual = 'N');
6387 return v_resultado > 0;
6388 end;
6389
6390 begin
6391
6392 if isRemanejamentoBufPickEsteira(p_idRemanejamento) then
6393 if nvl(p_telaExecRemanej, 0) in
6394 (C_REMANEJ_WEB, C_REMANEJ_MANUAL_COLETOR, C_REMANEJ_ORIGEM,
6395 C_REMANEJ_PLAN_COLETOR) then
6396 v_msg := t_message('Não é possÃvel executar esse remanejamento por essa tela. Realize-o através da opção Remanejamento Destino via Coletor de Dados.');
6397 raise_application_error(-20000, v_msg.formatMessage);
6398 elsif nvl(p_telaExecRemanej, 0) = C_REMANEJ_DESTINO then
6399 select count(1)
6400 into v_naoFinalizar
6401 from remanejamento rem, local ld, setor s
6402 where ld.idarmazem = rem.idarmazemdestino
6403 and ld.idlocal = rem.idlocaldestino
6404 and ld.tipo = 0
6405 and ld.buffer = 'N'
6406 and ld.idsetor = s.idsetor
6407 and s.codintegracaoesteira is not null
6408 and rem.idremanejamento = p_idRemanejamento
6409 and rem.status <> C_DESTINOINICIADO;
6410
6411 if (v_naoFinalizar > 0) then
6412 v_msg := t_message('Não é possÃvel executar esse remanejamento. Verifique se o mesmo já teve sua origem realizada.');
6413 raise_application_error(-20000, v_msg.formatMessage);
6414 end if;
6415 end if;
6416 end if;
6417 end;
6418
6419 procedure finalizarRemanejamento
6420 (
6421 p_idremanejamento in number,
6422 p_idusuario in number,
6423 p_telaExecFinalizacao in number default 0
6424 ) is
6425 type t_rem is record(
6426 status remanejamento.status%type,
6427 planejado remanejamento.planejado%type,
6428 idlocalorigem remanejamento.idlocalorigem%type,
6429 idarmazemorigem number,
6430 idremanejamento number,
6431 idlocaldestino remanejamento.idlocaldestino%type,
6432 idarmazemdestino number,
6433 idcontroleavaria number,
6434 idinsucessoentrega number,
6435 idprodutorecuperado number,
6436 tipoexportacaoprodrecuperado number,
6437 usoexclusivocxmov number);
6438
6439 r_remanejamento t_rem;
6440
6441 v_idendereco number;
6442 v_picking number;
6443 v_qtde_lote number;
6444 v_tipoLocalOrigem number;
6445 v_LocalOrigemBuffer varchar2(1);
6446 v_existeCRN number;
6447 v_msg varchar(2000) := '';
6448 v_erro boolean := false;
6449 v_remDePlParaPkEsteira boolean := false;
6450 v_fatorConversao number;
6451 v_caixaFechada varchar2(1);
6452 v_produto produto.codigointerno%type;
6453 v_msgRaise t_message;
6454
6455 v_tipoPermPkSetorOrigem number;
6456 v_tipoPermPkSetorDestino number;
6457 v_barraUnitaria embalagem.barra%type;
6458 v_qtdeUN number;
6459 v_fatorConversaoLote embalagem.fatorconversao%type;
6460 v_qtdeTotalLote lote.qtdedisponivel%type;
6461 r_lote lote%rowtype;
6462 v_idlote number;
6463 v_complementar lotelocal.complementar%type;
6464 v_qtdeUtilizadaUN number;
6465 v_qtdeCobertaLoteAntigo number;
6466 v_complementar varchar2(200);
6467
6468 procedure regraRemanejamentoLoteUnico(p_idremanejamento in number) is
6469 r_loteunico pk_lote.t_loteunico;
6470 r_loteunicoComp pk_lote.t_loteunico;
6471 r_loteunicoAvaliar pk_lote.t_loteunico;
6472 v_retorno varchar2(500);
6473 begin
6474 for r_loteunico in (select distinct lt.idproduto, lt.estado,
6475 lt.descr loteindustria,
6476 lt.dtvenc dtvencimento,
6477 pd.loteuniconoendereco,
6478 lt.iddepositante
6479 from loteremanejamento lr, lote lt,
6480 produtodepositante pd
6481 where lr.idremanejamento = p_idremanejamento
6482 and lr.idlote = lt.idlote
6483 and lt.idlote = lr.idlote
6484 and pd.idproduto = lt.idproduto
6485 and pd.identidade = lt.iddepositante)
6486 loop
6487
6488 r_loteunicoAvaliar.idProduto := r_loteunico.idproduto;
6489 r_loteunicoAvaliar.estado := r_loteunico.estado;
6490 r_loteunicoAvaliar.loteindustria := r_loteunico.loteindustria;
6491 r_loteunicoAvaliar.dtvencimento := r_loteunico.dtvencimento;
6492 r_loteunicoAvaliar.loteuniconoendereco := r_loteunico.loteuniconoendereco;
6493 r_loteunicoAvaliar.iddepositante := r_loteunico.iddepositante;
6494
6495 for r_loteunicoComp in (select distinct lt.idproduto, lt.estado,
6496 lt.descr loteindustria,
6497 lt.dtvenc dtvencimento,
6498 pd.loteuniconoendereco,
6499 lt.iddepositante
6500 from loteremanejamento lr, lote lt,
6501 produtodepositante pd
6502 where lr.idremanejamento =
6503 p_idremanejamento
6504 and lr.idlote = lt.idlote
6505 and lt.idlote = lr.idlote
6506 and pd.idproduto = lt.idproduto
6507 and pd.identidade = lt.iddepositante
6508 and (lt.idproduto <>
6509 r_loteunico.idproduto or
6510 lt.estado <> r_loteunico.estado or
6511 lt.descr <>
6512 r_loteunico.loteindustria or
6513 lt.dtvenc <>
6514 r_loteunico.dtvencimento or
6515 pd.loteuniconoendereco <>
6516 r_loteunico.loteuniconoendereco))
6517 loop
6518 if not pk_lote.isLoteUnicoCompativel(r_loteunico, r_loteunicoComp,
6519 v_retorno) then
6520 raise_application_error(-20000, v_retorno);
6521 end if;
6522 end loop;
6523 end loop;
6524
6525 if not pk_lote.isLocalPodeReceberLoteUnico(r_loteunicoAvaliar,
6526 r_remanejamento.idarmazemdestino,
6527 r_remanejamento.idlocaldestino,
6528 v_retorno) then
6529 raise_application_error(-20000, v_retorno);
6530 end if;
6531 end;
6532
6533 function isRemPulmParaPickComEsteira
6534 (
6535 p_idremanejamento in number,
6536 p_idArmazem in number,
6537 p_idlocalorigem in varchar2,
6538 p_idlocaldestino in varchar2
6539 ) return boolean is
6540
6541 v_isPulmParaPickEsteira number;
6542
6543 begin
6544 -- Verificar se é fluxo de origem pulmão sem esteira para destino picking com esteira
6545 select count(l.idlocal)
6546 into v_isPulmParaPickEsteira
6547 from remanejamento r, local l, setor s
6548 where r.idremanejamento = p_idremanejamento
6549 and s.idsetor = l.idsetor
6550 and ((l.idlocal = p_idlocalorigem and l.idarmazem = p_idArmazem and
6551 l.tipo in (1, 2) and
6552 length(trim(s.codintegracaoesteira)) is null) or
6553 (l.idlocal = p_idlocaldestino and l.idarmazem = p_idArmazem and
6554 l.tipo = 0 and
6555 length(trim(s.codintegracaoesteira)) is not null));
6556
6557 if (v_isPulmParaPickEsteira = 2) then
6558 return true;
6559 else
6560 return false;
6561 end if;
6562
6563 exception
6564 when no_data_found then
6565 return false;
6566
6567 end isRemPulmParaPickComEsteira;
6568
6569 procedure execOrigemRemBufferPK is
6570 begin
6571 for c_remanejamentoFilho in (select r.idRemanejamento
6572 from remanejamento r
6573 where r.idremanejamentopai =
6574 p_idremanejamento
6575 and r.status = 'A'
6576 and exists (select 1
6577 from local lo
6578 where lo.idarmazem =
6579 r.idarmazemorigem
6580 and lo.idlocal =
6581 r.idlocalorigem
6582 and lo.tipo = 0
6583 and lo.buffer = 'S'
6584 and lo.bufferesteira = 1)
6585 and exists
6586 (select 1
6587 from local ld, setor sd
6588 where sd.idsetor = ld.idsetor
6589 and ld.idarmazem =
6590 r.idarmazemdestino
6591 and ld.idlocal =
6592 r.idlocaldestino
6593 and ld.tipo = 0
6594 and ld.buffer = 'N'
6595 and nvl(length(sd.codintegracaoesteira),
6596 0) > 0))
6597 loop
6598 update loteremanejamento lr
6599 set lr.conferido = 'S'
6600 where lr.idremanejamento = c_remanejamentoFilho.idRemanejamento
6601 and lr.conferido = 'N';
6602
6603 update remanejamento rem
6604 set rem.status = 'G',
6605 rem.idusuarioinicio = p_idusuario,
6606 rem.horafimorigem = sysdate
6607 where rem.Idremanejamento = c_remanejamentoFilho.idRemanejamento;
6608
6609 integrarEsteira(c_remanejamentoFilho.idRemanejamento, p_idusuario);
6610 end loop;
6611 end;
6612 begin
6613 select r.status, r.planejado, r.idlocalorigem, r.idarmazemorigem,
6614 r.idremanejamento, r.idlocaldestino, r.idarmazemdestino,
6615 r.idcontroleavaria, r.idinsucessoentrega, pr.idprodutorecuperado,
6616 d.tipoexportacaoprodrecuperado,
6617 nvl(s.usoexclusivocxmov, 0) usoexclusivocxmov
6618 into r_remanejamento.status, r_remanejamento.planejado,
6619 r_remanejamento.idlocalorigem, r_remanejamento.idarmazemorigem,
6620 r_remanejamento.idremanejamento, r_remanejamento.idlocaldestino,
6621 r_remanejamento.idarmazemdestino,
6622 r_remanejamento.idcontroleavaria,
6623 r_remanejamento.idinsucessoentrega,
6624 r_remanejamento.idprodutorecuperado,
6625 r_remanejamento.tipoexportacaoprodrecuperado,
6626 r_remanejamento.usoexclusivocxmov
6627 from remanejamento r, produtorecuperado pr, depositante d, local l,
6628 setor s
6629 where r.idremanejamento = p_idremanejamento
6630 and r.idlocalorigem = l.idlocal
6631 and r.idarmazemorigem = l.idarmazem
6632 and l.idsetor = s.idsetor(+)
6633 and pr.idremanejamento(+) = r.idremanejamento
6634 and d.identidade(+) = pr.iddepositante
6635 for update;
6636
6637 if (r_remanejamento.status = 'F') then
6638 v_msgRaise := t_message('REMANEJAMENTO JA ESTA FINALIZADO. OPERAÇÃO CANCELADA.');
6639 raise_application_error(-20000, v_msgRaise.formatMessage);
6640 end if;
6641
6642 if r_remanejamento.planejado = 'S' then
6643 if (r_remanejamento.status in ('A', 'Z', 'G')) then
6644
6645 select count(*)
6646 into v_qtde_lote
6647 from loteremanejamento lr
6648 where lr.idremanejamento = p_idremanejamento;
6649
6650 if v_qtde_lote = 0 then
6651 v_msgRaise := t_message('REMANEJAMENTO SEM LOTES. OPERAÇÃO CANCELADA.');
6652 raise_application_error(-20000, v_msgRaise.formatMessage);
6653 end if;
6654
6655 regraRemanejamentoLoteUnico(p_idremanejamento);
6656
6657 v_remDePlParaPkEsteira := isRemPulmParaPickComEsteira(p_idremanejamento,
6658 r_remanejamento.idarmazemorigem,
6659 r_remanejamento.idlocalorigem,
6660 r_remanejamento.idlocaldestino);
6661
6662 select lo.tipo, lo.buffer,
6663 nvl(so.tipopermitirpickingsetor, 0) tipopermitirpickingsetor,
6664 nvl(sd.tipopermitirpickingsetor, 0) tipopermitirpickingsetor
6665 into v_tipoLocalOrigem, v_LocalOrigemBuffer,
6666 v_tipoPermPkSetorOrigem, v_tipoPermPkSetorDestino
6667 from local lo, setor so, local ld, setor sd
6668 where lo.idlocal = r_remanejamento.idlocalorigem
6669 and lo.idarmazem = r_remanejamento.idarmazemorigem
6670 and so.idsetor(+) = lo.idsetor
6671 and ld.idlocal = r_remanejamento.idlocaldestino
6672 and ld.idarmazem = r_remanejamento.idarmazemdestino
6673 and sd.idsetor(+) = ld.idsetor;
6674
6675 if ((v_tipoLocalOrigem = C_LOCAL_PICKING) and
6676 (v_tipoPermPkSetorOrigem = C_SETOR_CAIXAS and
6677 v_tipoPermPkSetorDestino = C_SETOR_UNIDADES)) then
6678
6679 for c_loteRemanejamento in (select lr.qtde, lr.idlote
6680 from loteremanejamento lr
6681 where lr.idremanejamento =
6682 r_remanejamento.idremanejamento)
6683 loop
6684
6685 delete from gtt_formarloteremanejamento;
6686
6687 select e.fatorconversao, lt.qtdedisponivel
6688 into v_fatorConversaoLote, v_qtdeTotalLote
6689 from lote lt, embalagem e
6690 where lt.idlote = c_loteRemanejamento.Idlote
6691 and e.idproduto = lt.idproduto
6692 and e.barra = lt.barra;
6693
6694 if (verificaCompOrigemDestino(v_qtdeTotalLote,
6695 v_fatorConversaoLote,
6696 c_loteRemanejamento.Qtde)) then
6697 begin
6698 select e.barra
6699 into v_barraUnitaria
6700 from lote lt, embalagem e
6701 where lt.idlote = c_loteRemanejamento.Idlote
6702 and e.idproduto = lt.idproduto
6703 and e.fatorconversao = 1
6704 and e.caixafechada = 'N'
6705 and e.ativo = 'S'
6706 and e.precadastro = 'N'
6707 and rownum = 1;
6708 exception
6709 when no_data_found then
6710 v_msgRaise := t_message('Não foi encontrada barra de unidade. Operação não permitida.');
6711 raise_application_error(-20000, v_msgRaise.formatMessage);
6712 end;
6713
6714 insert into gtt_formarloteremanejamento
6715 (idremanejamento, iddepositante, idproduto, estado, barra,
6716 qtde, itemadicional, peso, loteindustria, loteadicional,
6717 dtvenc, dtfabricacao, idloteinfoespec, liberado)
6718 select lr.idremanejamento, lt.iddepositante, lt.idproduto,
6719 lt.estado, v_barraUnitaria, lr.qtde, lt.itemadicional,
6720 (e.pesobruto * lr.qtde) peso, lt.descr,
6721 lt.loteadicional, lt.dtvenc, lt.dtfabricacao,
6722 decode(pk_lote.retinformacoesespecifica(lt.idlote),
6723 'SEM INFORMAÇÃO', null) idloteinfoespec,
6724 lt.liberado
6725 from loteremanejamento lr, lote lt, embalagem e
6726 where lr.idremanejamento = r_remanejamento.idremanejamento
6727 and lr.idlote = c_loteRemanejamento.Idlote
6728 and lt.idlote = lr.idlote
6729 and e.idproduto = lt.idproduto
6730 and e.barra = v_barraUnitaria;
6731
6732 for c_novoLote in (select g.idproduto, g.estado,
6733 g.iddepositante, g.itemadicional,
6734 g.loteindustria, g.dtvenc,
6735 g.dtfabricacao, g.qtde,
6736 e.fatorconversao, e.lastro,
6737 e.qtdecamada, g.idtipocaixa,
6738 g.loteadicional, g.idloteinfoespec,
6739 g.peso, g.barra,
6740 nvl(g.liberado, 'S') liberado
6741 from gtt_formarloteremanejamento g,
6742 embalagem e
6743 where e.barra = g.barra
6744 and e.idproduto = g.idproduto)
6745 loop
6746
6747 v_qtdeUN := (c_novoLote.qtde * c_novoLote.fatorconversao);
6748
6749 -- Informando os dados do Novo Lote Unitário
6750 r_lote.idproduto := c_novoLote.idproduto;
6751 r_lote.iddepositante := c_novoLote.iddepositante;
6752 r_lote.descr := c_novoLote.loteindustria;
6753 r_lote.dtVenc := c_novoLote.dtvenc;
6754 r_lote.estado := c_novoLote.estado;
6755 r_lote.idUsuario := p_idusuario;
6756 r_lote.tipolote := 'L';
6757 r_lote.situacao := 'R';
6758 r_lote.numerovolume := 1;
6759 r_lote.itemadicional := c_novoLote.itemadicional;
6760 r_lote.idtipocaixa := c_novoLote.idtipocaixa;
6761 r_lote.idarmazem := r_remanejamento.idarmazemorigem;
6762 r_lote.dtfabricacao := c_novoLote.dtfabricacao;
6763 r_lote.loteadicional := c_novoLote.loteadicional;
6764 r_lote.barra := c_novoLote.barra;
6765 r_lote.qtdeEntrada := v_qtdeUN;
6766 r_lote.tipoPalet := 'U';
6767
6768 if (pk_lote.isLoteIndustriaComMaisDeUmVenc(c_novoLote.Iddepositante,
6769 c_novoLote.idproduto,
6770 c_novoLote.loteindustria,
6771 c_novoLote.dtvenc)) then
6772 raise_application_error(-20000,
6773 'Não é possÃvel cadastrar lote do mesmo produto/depositante com o mesmo lote indústria e data de vencimento diferentes. Depositante:' ||
6774 c_novoLote.Iddepositante ||
6775 '. Produto: ' ||
6776 c_novoLote.idproduto ||
6777 ' Lote Indústria: ' ||
6778 c_novoLote.loteindustria ||
6779 ' Data Venc: ' ||
6780 c_novoLote.dtvenc);
6781
6782 end if;
6783
6784 v_idlote := pk_lote.cadastrar_lote(r_lote, null, 'N');
6785
6786 if (c_novoLote.liberado = 'N') then
6787 update lote lt
6788 set lt.liberado = 'N'
6789 where lt.idlote = v_idlote;
6790 end if;
6791
6792 if (c_novoLote.peso is not null) then
6793 update lote lt
6794 set lt.pesobruto = c_novoLote.peso
6795 where lt.idlote = v_idlote;
6796 end if;
6797
6798 pk_estoque.incluir_estoque(r_remanejamento.idarmazemdestino,
6799 r_remanejamento.idlocaldestino,
6800 v_idlote, r_lote.qtdeEntrada,
6801 p_idusuario,
6802 'ESTOQUE ADICIONADO EM FUNCAO DA FINALIZACAO DO CADASTRO DE LOTE DO REMANEJAMENTO N:' ||
6803 p_idremanejamento, 'N');
6804
6805 -- Consumindo quantidade dos lotes antigos
6806 v_qtdeUtilizadaUN := 0;
6807 for r_loteantigo in (select ll.idarmazem, l.idlote,
6808 ll.pendencia,
6809 (ll.estoque - ll.pendencia) disponivelestoque,
6810 l.qtdedisponivel qtdecoberta
6811 from lotelocal ll, lote l
6812 where ll.idarmazem =
6813 r_remanejamento.idarmazemorigem
6814 and ll.idlocal =
6815 r_remanejamento.idlocalorigem
6816 and l.idlote =
6817 c_loteRemanejamento.Idlote
6818 and l.idlote = ll.idlote
6819 and l.idproduto =
6820 c_novoLote.idproduto
6821 and l.estado = c_novoLote.estado
6822 and l.iddepositante =
6823 c_novoLote.iddepositante
6824 and case
6825 when c_novoLote.itemadicional is null then
6826 1
6827 else
6828 decode(l.itemadicional,
6829 c_novoLote.itemadicional,
6830 1, 0)
6831 end = 1
6832 and decode(trunc(l.dtvenc),
6833 trunc(c_novoLote.dtvenc), 1,
6834 0) = 1
6835 and case
6836 when c_novoLote.dtfabricacao is null then
6837 1
6838 else
6839 decode(trunc(l.dtfabricacao),
6840 trunc(c_novoLote.dtfabricacao),
6841 1, 0)
6842 end = 1
6843 and decode(l.descr,
6844 c_novoLote.loteindustria, 1,
6845 decode(l.descr,
6846 'LOTE DE PICKING SETOR CAIXAS PARA PICKING SETOR UNIDADES',
6847 1, 0)) = 1
6848 and case
6849 when c_novoLote.loteadicional is null then
6850 1
6851 else
6852 decode(l.loteadicional,
6853 c_novoLote.loteadicional,
6854 1, 0)
6855 end = 1
6856 order by l.dtalocacao)
6857
6858 loop
6859 if (r_loteantigo.disponivelestoque >= v_qtdeUN) then
6860 v_qtdeUtilizadaUN := v_qtdeUN;
6861 else
6862 if ((r_loteantigo.disponivelestoque = 0) or
6863 -- pode estar zerando o estoque do lote antigo ou
6864 (v_qtdeUN >= r_loteantigo.disponivelestoque)) then
6865 -- pode estar retirando uma quantidade maior que o disponÃvel, porém dentro desta operação
6866 -- onde a pendência é deste remanejamento e não de outro
6867 v_qtdeUtilizadaUN := r_loteantigo.pendencia;
6868 else
6869 v_qtdeUtilizadaUN := r_loteantigo.disponivelestoque;
6870 end if;
6871 end if;
6872
6873 v_qtdeUN := v_qtdeUN - v_qtdeUtilizadaUN;
6874
6875 pk_estoque.retirar_adicionar(r_remanejamento.idarmazemdestino,
6876 r_remanejamento.idlocaldestino,
6877 r_loteantigo.idlote,
6878 v_qtdeUtilizadaUN,
6879 p_idusuario,
6880 'ADICIONAR SUBTRAIDO REFERENTE AO REMANEJAMENTO N:' ||
6881 r_remanejamento.idremanejamento);
6882
6883 pk_estoque.retirar_pendencia(r_remanejamento.idarmazemorigem,
6884 r_remanejamento.idlocalorigem,
6885 r_loteantigo.idlote,
6886 v_qtdeUtilizadaUN,
6887 p_idusuario,
6888 'PENDENCIA SUBTRAIDA REFERENTE AO REMANEJAMENTO N:' ||
6889 r_remanejamento.idremanejamento);
6890
6891 pk_estoque.retirar_estoque(r_loteantigo.idarmazem,
6892 r_remanejamento.idlocalorigem,
6893 r_loteantigo.idlote,
6894 v_qtdeUtilizadaUN, p_idusuario,
6895 'ESTOQUE RETIRADO EM FUNCAO DA FINALIZACAO DO CADASTRO DE LOTE DO REMANEJAMENTO N:' ||
6896 p_idremanejamento, 'N');
6897 exit when v_qtdeUN = 0;
6898 end loop;
6899
6900 if (v_qtdeUN > 0) then
6901 v_msgRaise := t_message('Não foi possÃvel encontrar estoque suficiente para o produto.' ||
6902 chr(13) || 'IDPRODUTO: {0}' ||
6903 chr(13) ||
6904 'QTDENECESSARIA UN: {1}');
6905 v_msgRaise.addParam(c_novoLote.idproduto);
6906 v_msgRaise.addParam(c_novoLote.qtde *
6907 c_novoLote.fatorconversao);
6908 raise_application_error(-20000, v_msgRaise.formatMessage);
6909 end if;
6910
6911 end loop;
6912
6913 else
6914 v_msgRaise := t_message('Houve alterações no lote selecionado e não será possÃvel continuar o processo.');
6915 raise_application_error(-20000, v_msgRaise.formatMessage);
6916 end if;
6917 end loop;
6918 else
6919 for c in (select lr.idlote, lr.qtde, lt.idproduto, lt.barra,
6920 lt.iddepositante
6921 from loteremanejamento lr, lote lt
6922 where lt.idlote = lr.idlote
6923 and lr.idremanejamento =
6924 r_remanejamento.idremanejamento
6925 order by lr.idlote)
6926 loop
6927
6928 validarSetor(c.idlote, r_remanejamento.idarmazemdestino,
6929 r_remanejamento.idlocaldestino, v_msg, v_erro);
6930
6931 validarBufPickEsteira(p_idremanejamento, p_telaExecFinalizacao);
6932
6933 if v_remDePlParaPkEsteira then
6934
6935 -- Remanejamento é de Pulmão sem esteira para Picking com esteira
6936 -- Verifica se o fator de conversão é maior que um ou caixa fechada
6937 begin
6938 select e.fatorconversao, e.caixafechada, p.codigointerno
6939 into v_fatorConversao, v_caixaFechada, v_produto
6940 from loteremanejamento lr, lote lt, produto p, embalagem e
6941 where lr.idremanejamento = r_remanejamento.idremanejamento
6942 and lr.idlote = c.idlote
6943 and lt.idlote = lr.idlote
6944 and p.idproduto = lt.idproduto
6945 and e.idproduto = p.idproduto
6946 and e.barra = c.barra;
6947
6948 if (v_fatorConversao = 1 and v_caixaFechada = 'N' and
6949 r_remanejamento.usoexclusivocxmov = 0) then
6950 v_msgRaise := t_message('Não é permitido remanejar produtos unitários diretamente para o Picking com Esteira.' ||
6951 chr(13) || 'Código Interno: {0}.');
6952 v_msgRaise.addParam(v_produto);
6953 raise_application_error(-20000, v_msgRaise.formatMessage);
6954 end if;
6955
6956 end;
6957 end if;
6958
6959 if (v_erro) then
6960 raise_application_error(-20000, v_msg);
6961 end if;
6962
6963 pk_picking_dinamico.addGttPickingDinamico(c.idproduto,
6964 c.iddepositante,
6965 r_remanejamento.idlocaldestino,
6966 r_remanejamento.idarmazemdestino,
6967 1);
6968
6969 pk_estoque.incluir_estoque(r_remanejamento.idarmazemdestino,
6970 r_remanejamento.idlocaldestino,
6971 c.idlote, c.qtde, p_idusuario,
6972 'ESTOQUE ACRESCIDO NO LOTE REFERENTE AO REMANEJAMENTO N:' ||
6973 r_remanejamento.idremanejamento, 'N');
6974
6975 pk_estoque.retirar_adicionar(r_remanejamento.idarmazemdestino,
6976 r_remanejamento.idlocaldestino,
6977 c.idlote, c.qtde, p_idusuario,
6978 'ADICIONAR SUBTRAIDO REFERENTE AO REMANEJAMENTO N:' ||
6979 r_remanejamento.idremanejamento);
6980
6981 pk_estoque.retirar_pendencia(r_remanejamento.idarmazemorigem,
6982 r_remanejamento.idlocalorigem,
6983 c.idlote, c.qtde, p_idusuario,
6984 'PENDENCIA SUBTRAIDA REFERENTE AO REMANEJAMENTO N:' ||
6985 r_remanejamento.idremanejamento);
6986
6987 pk_estoque.retirar_estoque(r_remanejamento.idarmazemorigem,
6988 r_remanejamento.idlocalorigem,
6989 c.idlote, c.qtde, p_idusuario,
6990 'ESTOQUE SUBTRAIDO NO LOTE REFERENTE AO REMANEJAMENTO N:' ||
6991 r_remanejamento.idremanejamento, 'N');
6992
6993 if ((v_tipoLocalOrigem = 0) AND (v_LocalOrigemBuffer = 'N')) then
6994 pk_picking_dinamico.addGttPickingDinamico(c.idproduto,
6995 c.iddepositante,
6996 r_remanejamento.idlocalorigem,
6997 r_remanejamento.idarmazemorigem,
6998 0);
6999 end if;
7000
7001 end loop;
7002
7003 pk_picking_dinamico.deletar_picking_dinamico;
7004 pk_picking_dinamico.inserir_picking_dinamico;
7005
7006 pk_convocacao.finalizaremanejamento(p_idusuario,
7007 r_remanejamento.idremanejamento);
7008
7009 pk_convocacao.finalizaRemOrigem(p_idusuario,
7010 r_remanejamento.idremanejamento);
7011
7012 pk_convocacao.finalizaRemDestino(p_idusuario,
7013 r_remanejamento.idremanejamento);
7014
7015 select l.id, l.tipo
7016 into v_idendereco, v_picking
7017 from local l
7018 where l.idlocal = r_remanejamento.idlocaldestino
7019 and l.idarmazem = r_remanejamento.idarmazemdestino;
7020
7021 if (v_picking = 0) then
7022 for c in (select distinct r_remanejamento.idarmazemdestino idarmazem,
7023 r_remanejamento.idlocaldestino idlocal,
7024 l.idproduto, l.iddepositante
7025 from loteremanejamento lr, lote l,
7026 produtodepositante pd
7027 where l.idlote = lr.idlote
7028 and pd.identidade = l.iddepositante
7029 and pd.idproduto = l.idproduto
7030 and pd.pickingdinamico = 1
7031 and lr.idremanejamento =
7032 r_remanejamento.idremanejamento
7033 and not exists
7034 (select 1
7035 from produtolocal
7036 where idarmazem =
7037 r_remanejamento.idarmazemdestino
7038 and idlocal = r_remanejamento.idlocaldestino
7039 and idproduto = l.idproduto
7040 and identidade = l.iddepositante))
7041 loop
7042 pk_armazem.criarPickingProduto(c.idarmazem, c.idlocal,
7043 c.iddepositante, c.idproduto);
7044 end loop;
7045 end if;
7046 end if;
7047 else
7048 v_msgRaise := t_message('O REMANEJAMENTO NAO FOI PLANEJADO.');
7049 raise_application_error(-20000, v_msgRaise.formatMessage);
7050 end if;
7051
7052 validarFinalizarRemanejamento(p_idremanejamento);
7053
7054 update remanejamento r
7055 set r.status = 'F',
7056 r.idusuario = nvl(r.idusuario, p_idusuario),
7057 r.idusuarioinicio = nvl(r.idusuarioinicio, p_idusuario),
7058 r.idusuariofim = nvl(r.idusuariofim, p_idusuario),
7059 r.datahora = nvl(r.datahora, sysdate),
7060 r.horainicio = nvl(r.horainicio, sysdate),
7061 r.horafim = nvl(r.horafim, sysdate),
7062 r.horainicioorigem = nvl(r.horainicioorigem, sysdate),
7063 r.horafimorigem = nvl(r.horafimorigem, sysdate)
7064 where r.idremanejamento = p_idremanejamento;
7065
7066 -- Removendo os endereços filhos da origem quando houver
7067 RemoveInfMultiEndereco(p_idremanejamento);
7068
7069 -- Finalizar Remanejamento Ordem Devolução (Caso seje)
7070 pk_ordemdevolucao.finalizarremanejamento(p_idremanejamento);
7071
7072 if r_remanejamento.idprodutorecuperado is not null then
7073 if r_remanejamento.tipoexportacaoprodrecuperado = 1 then
7074 pk_integracao.exportarprodutorecuperado(r_remanejamento.idprodutorecuperado);
7075 elsif r_remanejamento.tipoexportacaoprodrecuperado in (2, 3) then
7076 pk_integracao.exportarmovimentacaointerna(r_remanejamento.idprodutorecuperado,
7077 3);
7078 elsif r_remanejamento.tipoexportacaoprodrecuperado in (4, 5, 6) then
7079 pk_integracao.exportarProdutoRecuperadoMV(r_remanejamento.idprodutorecuperado,
7080 r_remanejamento.tipoexportacaoprodrecuperado);
7081 end if;
7082 end if;
7083
7084 if r_remanejamento.idcontroleavaria is not null then
7085 pk_integracao.exportarControleAvaria(r_remanejamento.idcontroleavaria);
7086 pk_integracao.exportarMovimentacaoInterna(r_remanejamento.idcontroleavaria,
7087 1);
7088 end if;
7089
7090 if r_remanejamento.idinsucessoentrega is not null then
7091 select count(*)
7092 into v_existeCRN
7093 from remanejamento r
7094 where r.idinsucessoentrega = r_remanejamento.idinsucessoentrega
7095 and r.idremanejamento <> p_idremanejamento
7096 and r.status <> 'F';
7097
7098 if v_existeCRN = 0 then
7099 pk_integracao.exportarMovimentacaoInterna(r_remanejamento.idinsucessoentrega,
7100 5);
7101
7102 update insucessoentrega
7103 set status = 3
7104 where id = r_remanejamento.idinsucessoentrega;
7105 end if;
7106 end if;
7107
7108 pk_integracao.expRemanejamentoEntreSetores(p_idremanejamento);
7109
7110 pk_utilities.GeraLog(p_idusuario,
7111 'O REMANEJAMENTO ' || p_idremanejamento ||
7112 ' FOI EXECUTADO COM SUCESSO.', p_idremanejamento,
7113 'RP');
7114
7115 execOrigemRemBufferPK;
7116 end if;
7117 end;
7118
7119 procedure finalizarRemanejamentoWEB
7120 (
7121 p_idremanejamento in number,
7122 p_idusuario in number
7123 ) is
7124 v_Pendentes varchar2(2000);
7125 v_finalizado char(1);
7126 v_msg t_message;
7127 begin
7128 /*Verifica se não existe remanejamentos anteriores para o endereço de origem */
7129 select status
7130 into v_finalizado
7131 from remanejamento
7132 where idremanejamento = p_idremanejamento;
7133
7134 if v_finalizado <> 'F' then
7135 select (select stragg(r.idremanejamento) pendente
7136 from remanejamento r, loteremanejamento lr
7137 where r.idarmazemdestino = rp.idarmazemorigem
7138 and r.idlocaldestino = rp.idlocalorigem
7139 and r.status <> 'F'
7140 and lr.idremanejamento = r.idremanejamento
7141 and r.datahora < rp.datahora
7142 and r.idremanejamento < rp.idremanejamento
7143 and exists
7144 (select 1
7145 from loteremanejamento
7146 where idremanejamento = rp.idremanejamento
7147 and idlote = lr.idlote))
7148 into v_Pendentes
7149 from remanejamento rp
7150 where rp.idremanejamento = p_idremanejamento
7151 and rp.status <> 'F';
7152
7153 if v_pendentes is null then
7154
7155 finalizarRemanejamento(p_idremanejamento, p_idusuario,
7156 C_REMANEJ_WEB);
7157 else
7158 v_msg := t_message('EXISTEM REMANEJAMENTOS PENDENTES QUE DEVEM SER EXECUTADOS ANTERIORMENTE: ' ||
7159 '{0}. OPERAÇÃO CANCELADA.');
7160 v_msg.addParam(v_Pendentes);
7161 raise_application_error(-20000, v_msg.formatMessage);
7162 end if;
7163 else
7164 v_msg := t_message('O REMANEJAMENTO Jà FOI FINALIZADO. OPERAÇÃO CANCELADA.');
7165 raise_application_error(-20000, v_msg.formatMessage);
7166 end if;
7167
7168 end finalizarRemanejamentoWEB;
7169
7170 procedure finalizarPlanejamento(p_idremanejamento in number) is
7171 v_lotes number;
7172 v_msg t_message;
7173 begin
7174 select count(*)
7175 into v_lotes
7176 from loteremanejamento
7177 where idremanejamento = p_idremanejamento;
7178
7179 if v_lotes = 0 then
7180 v_msg := t_message('LOTES NÃO VINCULADOS AO REMANEJAMENTO');
7181 raise_application_error(-20000, v_msg.formatMessage);
7182 else
7183 update remanejamento
7184 set planejado = 'S'
7185 where idremanejamento = p_idremanejamento
7186 and planejado = 'N';
7187 end if;
7188 end;
7189
7190 procedure reabrirPlanejamento
7191 (
7192 p_idremanejamento in number,
7193 p_idusuario in number
7194 ) is
7195 v_lotes number;
7196 v_status remanejamento.status%type;
7197 v_tipolocalorigem number;
7198 v_tipolocaldestino number;
7199 v_msg t_message;
7200 begin
7201 -- Realizando lock e verificando status do remanejamento
7202 select r.status
7203 into v_status
7204 from remanejamento r
7205 where r.idremanejamento = p_idremanejamento
7206 for update;
7207
7208 select count(*)
7209 into v_lotes
7210 from loteremanejamento
7211 where idremanejamento = p_idremanejamento
7212 and conferido = 'S';
7213
7214 select lo.tipo, ld.tipo
7215 into v_tipolocalorigem, v_tipolocaldestino
7216 from remanejamento r, local lo, local ld
7217 where r.idremanejamento = p_idremanejamento
7218 and lo.idlocal = r.idlocalorigem
7219 and lo.idarmazem = r.idarmazemorigem
7220 and ld.idlocal = r.idlocaldestino
7221 and ld.idarmazem = r.idarmazemdestino;
7222
7223 if v_tipolocalorigem = 0
7224 and (v_tipolocaldestino = 1 or v_tipolocaldestino = 2) then
7225 v_msg := t_message('O PLANEJAMENTO NÃO PODE SER REABERTO PARA REMANEJAMENTOS DE PICKING PARA PULMÃO');
7226 raise_application_error(-20000, v_msg.formatMessage);
7227 end if;
7228
7229 if v_status = 'F' then
7230 v_msg := t_message('O PLANEJAMENTO NÃO PODE SER REABERTO. REMANEJAMENTO Jà FINALIZADO.');
7231 raise_application_error(-20000, v_msg.formatMessage);
7232 elsif v_lotes > 0
7233 or v_status <> 'A' then
7234 v_msg := t_message('O PLANEJAMENTO NÃO PODE SER REABERTO. REMANEJAMENTO Jà INICIADO.');
7235 raise_application_error(-20000, v_msg.formatMessage);
7236 else
7237 update remanejamento
7238 set planejado = 'N'
7239 where idremanejamento = p_idremanejamento
7240 and planejado = 'S';
7241
7242 pk_utilities.GeraLog(p_idusuario,
7243 'PLANEJAMENTO REABERTO PARA O ID: ' ||
7244 P_IDREMANEJAMENTO, P_IDREMANEJAMENTO, 'RP');
7245 end if;
7246 end;
7247
7248 /*
7249 * Carrega o resumo das embalagens a serem utilizadas
7250 * a partir do produto, local e da qtde a ser movimentada
7251 */
7252 function retResumoEmbalagensLocal
7253 (
7254 p_idproduto in number,
7255 p_qtde in number,
7256 p_idlocal in local.idlocal%type
7257 ) return string is
7258
7259 cursor c_emb
7260 (
7261 p_produto in number,
7262 p_rest in number,
7263 p_idlocal in local.idlocal%type
7264 ) is
7265 select e.barra, e.fatorconversao, e.descrreduzido
7266 from embalagem e, lote l, lotelocal ll
7267 where e.idproduto = p_idproduto
7268 and trunc(p_rest / e.fatorconversao) > 0
7269 and e.ativo = 'S'
7270 and l.idproduto = e.idproduto
7271 and l.barra = e.barra
7272 and ll.idlote = l.idlote
7273 and ll.idlocal = p_idlocal
7274 and ll.estoque > 0
7275 order by e.fatorconversao desc;
7276
7277 r_emb c_emb%rowtype;
7278 v_resumo varchar2(1000);
7279 v_qtdeUtilizada number;
7280 v_qtde number;
7281 begin
7282 v_qtde := p_qtde;
7283
7284 open c_emb(p_idproduto, v_qtde, p_idlocal);
7285 fetch c_emb
7286 into r_emb;
7287 while (v_qtde > 0)
7288 and (c_emb%found)
7289 loop
7290 v_qtdeUtilizada := trunc(v_qtde / r_emb.fatorconversao);
7291 if v_qtdeUtilizada > 0 then
7292 v_qtde := v_qtde - (v_qtdeUtilizada * r_emb.fatorconversao);
7293
7294 if v_resumo is not null then
7295 v_resumo := v_resumo || ', ';
7296 end if;
7297
7298 v_resumo := v_resumo || v_qtdeUtilizada || ' ' ||
7299 r_emb.descrreduzido || ' [' || r_emb.fatorconversao || ']';
7300
7301 end if;
7302 fetch c_emb
7303 into r_emb;
7304 end loop;
7305 close c_emb;
7306
7307 return v_resumo;
7308
7309 end;
7310
7311 procedure associarLote
7312 (
7313 p_idremanejamento in number,
7314 p_idlote in number,
7315 p_quantidade in number,
7316 p_idusuario in number
7317 ) is
7318 v_idproduto number;
7319 v_idarmazemorigem number;
7320 v_idlocalorigem local.idlocal%type;
7321 v_idarmazemdestino number;
7322 v_total number;
7323 v_idlocaldestino local.idlocal%type;
7324 v_tipolote char(1);
7325 v_result varchar(500);
7326 v_pkpl number;
7327 v_travaLocalIntEsteira number;
7328 v_produtoCodigoInt produto.codigointerno%type;
7329 v_msg t_message;
7330
7331 procedure regraLoteUnico(p_idLote in lote.idlote%type) is
7332 r_loteUnicoComparar pk_lote.t_loteunico;
7333 v_idarmazemdestino number;
7334 v_idlocaldestino local.idlocal%type;
7335 v_msgerro varchar2(5000);
7336 begin
7337
7338 select lt.idproduto, lt.estado, lt.descr loteindustria,
7339 lt.dtvenc dtvencimento, pd.loteuniconoendereco,
7340 lt.iddepositante
7341 into r_loteUnicoComparar
7342 from lote lt, produtodepositante pd
7343 where lt.idlote = p_idLote
7344 and pd.identidade = lt.iddepositante
7345 and pd.idproduto = lt.idproduto;
7346
7347 for r_loteunico in (select lt.idproduto, lt.estado,
7348 lt.descr loteindustria,
7349 lt.dtvenc dtvencimento,
7350 pd.loteuniconoendereco, lt.iddepositante
7351 from loteremanejamento lr, lote lt,
7352 produtodepositante pd
7353 where lr.idremanejamento = p_idremanejamento
7354 and pd.idproduto = lt.idproduto
7355 and lt.idlote = lr.idlote
7356 and pd.identidade = lt.iddepositante
7357 and pd.idproduto = lt.idproduto)
7358 loop
7359
7360 if (not pk_lote.isLoteUnicoCompativel(r_loteunicocomparar,
7361 r_loteunico, v_msgerro)) then
7362 raise_application_error(-20000, v_msgErro);
7363 end if;
7364
7365 end loop;
7366
7367 select r.idarmazemdestino, r.idlocaldestino
7368 into v_idarmazemdestino, v_idlocaldestino
7369 from remanejamento r
7370 where r.idremanejamento = p_idremanejamento;
7371
7372 if (not
7373 pk_lote.isLocalPodeReceberLoteUnico(r_loteunicocomparar,
7374 v_idarmazemdestino,
7375 v_idlocaldestino, v_msgerro)) then
7376 raise_application_error(-20000, v_msgerro);
7377 end if;
7378 end;
7379
7380 procedure validarVinculos is
7381 v_localOrigem local.idlocalformatado%type;
7382 v_localDestino local.idlocalformatado%type;
7383 v_tipoLocalOrigem local.tipo%type;
7384 v_tipoLocalDestino local.tipo%type;
7385 v_tipoPermPkSetorOrigem setor.tipopermitirpickingsetor%type;
7386 v_tipoPermPkSetorDestino setor.tipopermitirpickingsetor%type;
7387 v_localIncompativel number;
7388 v_idProduto produto.idproduto%type;
7389 v_barra embalagem.barra%type;
7390 v_tipoPkProdDep produtodepositante.tipopicking%type;
7391 v_idArmazem armazem.idarmazem%type;
7392 v_idLocalDestino local.idlocal%type;
7393 v_idEntidade depositante.identidade%type;
7394 v_unidades number;
7395 v_msg t_message;
7396 v_fatorConversaoLote embalagem.fatorconversao%type;
7397 v_qtdeTotalLote lote.qtdedisponivel%type;
7398 v_barraUnitaria number;
7399 v_existeEstoque number;
7400 v_fatorNoEstoque number;
7401 begin
7402
7403 select lt.idarmazem, lt.iddepositante, lt.idproduto, e.fatorconversao,
7404 lt.qtdedisponivel
7405 into v_idArmazem, v_idEntidade, v_idProduto, v_fatorConversaoLote,
7406 v_qtdeTotalLote
7407 from lote lt, embalagem e
7408 where lt.idlote = p_idlote
7409 and e.idproduto = lt.idproduto
7410 and e.barra = lt.barra;
7411
7412 select lo.idlocalformatado localorigem, lo.tipo,
7413 so.tipopermitirpickingsetor, ld.idlocalformatado localdestino,
7414 ld.tipo, sd.tipopermitirpickingsetor, ld.idlocal
7415 into v_localOrigem, v_tipoLocalOrigem, v_tipoPermPkSetorOrigem,
7416 v_localDestino, v_tipoLocalDestino, v_tipoPermPkSetorDestino,
7417 v_idLocalDestino
7418 from remanejamento r, local lo, local ld, setor so, setor sd
7419 where r.idremanejamento = p_idremanejamento
7420 and lo.idlocal = r.idlocalorigem
7421 and lo.idarmazem = r.idarmazemorigem
7422 and ld.idlocal = r.idlocaldestino
7423 and ld.idarmazem = r.idarmazemdestino
7424 and so.idsetor = lo.idsetor
7425 and sd.idsetor = ld.idsetor;
7426
7427 if (v_tipoLocalDestino = C_LOCAL_PICKING) then
7428 -- Validando se o produto é compatÃvel com o setor/endereço Destino.
7429 pk_armazem.validarSetorPickingProduto(v_idArmazem, v_idLocalDestino,
7430 v_idEntidade, v_idProduto);
7431 if (v_tipoLocalOrigem in
7432 (C_LOCAL_PULMAO_BLOCADO, C_LOCAL_PULMAO_PALETIZADO)) then
7433 -- Validando se o setor/endereço Origem é compatÃvel com o setor/endereço Destino.
7434 if (v_tipoPermPkSetorDestino = C_SETOR_MISTO) then
7435 return;
7436 elsif (v_tipoPermPkSetorDestino = C_SETOR_UNIDADES) then
7437 begin
7438 select count(1), e.barra
7439 into v_localIncompativel, v_barra
7440 from lote lt, produto p, embalagem e
7441 where lt.idlote = p_idlote
7442 and p.idproduto = lt.idproduto
7443 and e.idproduto = p.idproduto
7444 and e.barra = lt.barra
7445 and ((e.fatorconversao = 1 and e.caixafechada = 'S') or
7446 e.fatorconversao > 1)
7447 group by p.idproduto, e.barra;
7448 exception
7449 when no_data_found then
7450 v_localIncompativel := 0;
7451 end;
7452
7453 if (v_localIncompativel > 0) then
7454 v_msg := t_message('Local Origem: {0} incompatÃvel com o Local Destino: {1}.' ||
7455 chr(13) ||
7456 'O Produto: {2} Barra: {3} Lote: {4} não pode ser remanejado para um picking que aceita somente Unidades.');
7457 v_msg.addParam(v_localOrigem);
7458 v_msg.addParam(v_localDestino);
7459 v_msg.addParam(v_idProduto);
7460 v_msg.addParam(v_barra);
7461 v_msg.addParam(p_idlote);
7462
7463 raise_application_error(-20000, v_msg.formatMessage);
7464 end if;
7465
7466 elsif (v_tipoPermPkSetorDestino = C_SETOR_CAIXAS) then
7467 begin
7468 select count(1), e.barra
7469 into v_localIncompativel, v_barra
7470 from lote lt, produto p, embalagem e
7471 where lt.idlote = p_idlote
7472 and p.idproduto = lt.idproduto
7473 and e.idproduto = p.idproduto
7474 and e.barra = lt.barra
7475 and e.fatorconversao = 1
7476 and e.caixafechada = 'N'
7477 group by p.idproduto, e.barra;
7478 exception
7479 when no_data_found then
7480 v_localIncompativel := 0;
7481 end;
7482
7483 if v_localIncompativel > 0 then
7484 v_msg := t_message('Local Origem: {0} incompatÃvel com o Local Destino: {1}.' ||
7485 chr(13) ||
7486 'O Produto: {2} Barra: {3} Lote: {4} não pode ser remanejado para um picking que aceita somente Caixas.');
7487 v_msg.addParam(v_localOrigem);
7488 v_msg.addParam(v_localDestino);
7489 v_msg.addParam(v_idProduto);
7490 v_msg.addParam(v_barra);
7491 v_msg.addParam(p_idlote);
7492
7493 raise_application_error(-20000, v_msg.formatMessage);
7494 end if;
7495
7496 select count(1)
7497 into v_existeEstoque
7498 from remanejamento r, local ld, lotelocal ll
7499 where r.idremanejamento = p_idremanejamento
7500 and ld.idlocal = r.idlocaldestino
7501 and ld.idarmazem = r.idarmazemdestino
7502 and ll.idlocal = ld.idlocal
7503 and ll.idarmazem = ld.idarmazem
7504 and (ll.estoque > 0 or ll.pendencia > 0 or ll.adicionar > 0);
7505
7506 if v_existeEstoque > 0 then
7507 begin
7508 select distinct e.fatorconversao
7509 into v_fatorNoEstoque
7510 from remanejamento r, lotelocal ll, lote lt, embalagem e
7511 where r.idremanejamento = p_idremanejamento
7512 and ll.idlocal = r.idlocaldestino
7513 and lt.idlote = ll.idlote
7514 and (ll.estoque > 0 or ll.pendencia > 0 or
7515 ll.adicionar > 0)
7516 and e.idproduto = lt.idproduto
7517 and e.barra = lt.barra;
7518 exception
7519 when too_many_rows then
7520 v_msg := t_message('Verifique o endereço de destino pois Picking pertencente à um Setor configurado para ¿Permitir Picking: Caixas¿ não pode ter estoque com diferentes fatores de conversão.');
7521 raise_application_error(-20000, v_msg.formatMessage);
7522 end;
7523
7524 if v_fatorNoEstoque <> v_fatorConversaoLote then
7525 v_msg := t_message('O endereço destino é um Picking pertencente à um Setor configurado para ¿Permitir Picking: Caixas¿ e existe estoque com fator de conversão {0}.' ||
7526 chr(13) ||
7527 'Selecione uma embalagem com o mesmo fator de conversão ou escolha outro endereço para o remanejamento.');
7528 v_msg.addParam(v_fatorNoEstoque);
7529
7530 raise_application_error(-20000, v_msg.formatMessage);
7531 end if;
7532 end if;
7533 end if;
7534
7535 elsif (v_tipoLocalOrigem = C_LOCAL_PICKING) then
7536
7537 if (v_tipoPermPkSetorOrigem = C_SETOR_CAIXAS and
7538 v_tipoPermPkSetorDestino = C_SETOR_UNIDADES) then
7539 begin
7540 select count(1)
7541 into v_barraUnitaria
7542 from lote lt, embalagem e
7543 where lt.idlote = p_idlote
7544 and e.idproduto = lt.idproduto
7545 and e.barra = lt.barra
7546 and e.fatorconversao = 1
7547 and e.caixafechada = 'N'
7548 and e.ativo = 'S'
7549 and e.precadastro = 'N';
7550 exception
7551 when no_data_found then
7552 v_barraUnitaria := 0;
7553 end;
7554
7555 if not (v_barraUnitaria > 0) then
7556 if (verificaCompOrigemDestino(v_qtdeTotalLote,
7557 v_fatorConversaoLote,
7558 p_quantidade)) then
7559 return;
7560 else
7561 v_msg := t_message('A quantidade em unidades {0} escolhida para o remanejamento, não permite manter a compatibilidade das configurações dos setores nos endereços de Origem e Destino.' ||
7562 chr(13) || 'Escolha outra quantidade.');
7563 v_msg.addParam(p_quantidade);
7564 raise_application_error(-20000, v_msg.formatMessage);
7565 end if;
7566 end if;
7567
7568 elsif (v_tipoPermPkSetorOrigem = C_SETOR_UNIDADES and
7569 v_tipoPermPkSetorDestino = C_SETOR_CAIXAS) then
7570
7571 v_msg := t_message('O Produto[ idProduto: {0} ] está em um Picking pertencente à um Setor configurado para ¿Permitir Picking: Unidades¿ e o destino é um Picking pertencente à um Setor configurado para ¿Permitir Picking: Caixas¿.' ||
7572 chr(13) ||
7573 'Este Picking só poderá receber remanejamentos vindos do Pulmão ou de outro Picking pertencente à um Setor configurado para ¿Permitir Picking: Caixas¿.');
7574 v_msg.addParam(v_idProduto);
7575
7576 raise_application_error(-20000, v_msg.formatMessage);
7577 end if;
7578 end if;
7579 end if;
7580 end validarVinculos;
7581
7582 begin
7583 if p_quantidade <= 0 then
7584 v_msg := t_message('Quantidade não pode ser menor ou igual a zero');
7585 raise_application_error(-20000, v_msg.formatMessage);
7586 end if;
7587
7588 select count(*)
7589 into v_pkpl
7590 from remanejamento r, local lo, local ld
7591 where lo.idarmazem = r.idarmazemorigem
7592 and lo.idlocal = r.idlocalorigem
7593 and ld.idarmazem = r.idarmazemdestino
7594 and ld.idlocal = r.idlocaldestino
7595 and lo.tipo = 0 -- picking = 'S'
7596 and ld.tipo in (1, 2) --picking = 'N'
7597 and r.idcontroleavaria is null
7598 and r.idremanejamento = p_idremanejamento;
7599
7600 if v_pkpl <> 0 then
7601 v_msg := t_message('Não é possÃvel Incluir Lotes em Remanejamentos de Picking para Pulmão');
7602 raise_application_error(-20000, v_msg.formatMessage);
7603 end if;
7604
7605 validarVinculos;
7606
7607 pk_lote.validarProdutoFracionado(p_idlote, p_quantidade);
7608
7609 select count(*)
7610 into v_total
7611 from loteremanejamento lr
7612 where lr.idremanejamento = p_idremanejamento
7613 and idlote = p_idlote;
7614 if v_total > 0 then
7615 desassociarLote(p_idremanejamento, p_idlote, p_idusuario);
7616 end if;
7617
7618 select tipolote, idproduto
7619 into v_tipolote, v_idproduto
7620 from lote
7621 where idlote = p_idlote;
7622
7623 select idarmazemorigem, idlocalorigem, idarmazemdestino, idlocaldestino
7624 into v_idarmazemorigem, v_idlocalorigem, v_idarmazemdestino,
7625 v_idlocaldestino
7626 from remanejamento
7627 where idremanejamento = p_idremanejamento;
7628
7629 select count(l.idlocal)
7630 into v_travaLocalIntEsteira
7631 from remanejamento r, local l, setor s
7632 where r.idremanejamento = p_idremanejamento
7633 and s.idsetor = l.idsetor
7634 and ((l.idlocal = v_idlocalorigem and
7635 l.idarmazem = v_idarmazemorigem and l.tipo in (1, 2) and
7636 length(trim(s.codintegracaoesteira)) is null) or
7637 (l.idlocal = v_idlocaldestino and
7638 l.idarmazem = v_idarmazemdestino and l.tipo = 0 and
7639 length(trim(s.codintegracaoesteira)) is not null));
7640
7641 if (v_travaLocalIntEsteira = 2) then
7642 begin
7643 select pd.codigointerno
7644 into v_produtoCodigoInt
7645 from lote lt, produto pd, embalagem emb
7646 where lt.idlote = p_idlote
7647 and pd.idproduto = lt.idproduto
7648 and emb.idproduto = pd.idproduto
7649 and emb.barra = lt.barra
7650 and (emb.fatorconversao = 1 and emb.caixafechada = 'N')
7651 and rownum = 1;
7652
7653 v_msg := t_message('Não é possÃvel vincular as unidades do produto cod: "' ||
7654 '{0}" nesse remanejamento.' || CHR(13) ||
7655 'Esse remanejamento possui o endereço de destino id: "' ||
7656 '{1}" parametrizado para ser do tipo picking com setor que utiliza integração com esteira.');
7657 v_msg.addParam(v_produtoCodigoInt);
7658 v_msg.addParam(v_idlocaldestino);
7659 raise_application_error(-20000, v_msg.formatMessage);
7660 exception
7661 when no_data_found then
7662 null;
7663 end;
7664 end if;
7665
7666 v_result := ctrlVincularMaterial(p_idlote, v_tipolote, v_idproduto,
7667 v_idarmazemorigem, v_idlocalorigem,
7668 v_idarmazemdestino, v_idlocaldestino,
7669 p_quantidade);
7670
7671 if v_result is not null then
7672 raise_application_error(-20000, v_result);
7673 end if;
7674
7675 regraLoteUnico(p_idlote);
7676
7677 insert into loteremanejamento
7678 (idremanejamento, idlote, qtde, conferido, controlaqtde)
7679 values
7680 (p_idremanejamento, p_Idlote, p_quantidade, 'N', 'S');
7681
7682 associar(p_idremanejamento, p_idlote);
7683 end;
7684
7685 procedure desassociarLote
7686 (
7687 p_idremanejamento in number,
7688 p_idlote in number,
7689 p_idusuario in number
7690 ) is
7691 begin
7692
7693 DESASSOCIAR(p_idremanejamento, p_idlote);
7694
7695 delete from loteremanejamento
7696 where idremanejamento = p_idremanejamento
7697 and idlote = p_idlote;
7698 end;
7699
7700 procedure MarcarImpressoRemanejamento
7701 (
7702 p_idremanejamento in number,
7703 p_idusuario in number
7704 ) is
7705 v_usuario usuario.nomeusuario%type;
7706 begin
7707 select nomeusuario
7708 into v_usuario
7709 from usuario
7710 where idusuario = p_idusuario;
7711
7712 update loteremanejamento
7713 set impresso = 'S'
7714 where idremanejamento = p_idremanejamento;
7715
7716 pk_utilities.GeraLog(p_idusuario,
7717 'REMANEJAMENTO ID:' || p_idremanejamento ||
7718 ' MARCADO COMO IMPRESSO PELO USUARIO ID:' ||
7719 p_idusuario || ' nome: ' || v_usuario,
7720 p_idremanejamento, 'RM');
7721 end;
7722
7723 procedure formarLotePickingPulmao
7724 (
7725 p_idremanejamento in number,
7726 p_idusuario in number,
7727 p_moduloSistema in number := 0
7728 ) is
7729 MODULO_ENTERPRISE constant number := 0;
7730 MODULO_MWMS constant number := 1;
7731
7732 r_remanejamento remanejamento%rowtype;
7733
7734 v_msg t_message;
7735 v_mgsContratoLote historicomudancacontratolote.mensagem%type;
7736
7737 procedure validacoes is
7738 v_jaPossuiLoteVinculado number;
7739 v_caixaNaoInformada number;
7740 r_loteunicocomparar pk_lote.t_loteunico;
7741 v_msgerro varchar2(4000);
7742
7743 v_existeLoteSemPeso number;
7744 v_existeInfoEspecNaoInformada number;
7745
7746 C_PRODUTO_SEGREGADO constant number := 1;
7747
7748 procedure validarProdutoSegregado is
7749 v_fatorOrigem number;
7750 v_fatorDestino number;
7751 begin
7752 for r in (select g.idproduto, g.iddepositante
7753 from gtt_formarloteremanejamento g, produtodepositante pd
7754 where pd.tipopicking = C_PRODUTO_SEGREGADO
7755 and pd.idproduto = g.idproduto
7756 and pd.identidade = g.iddepositante
7757 group by g.idproduto, g.iddepositante)
7758 loop
7759
7760 select l.fatorconversao
7761 into v_fatorOrigem
7762 from lotelocal lo, lote l
7763 where lo.idlocal = r_remanejamento.idlocalorigem
7764 and lo.idarmazem = r_remanejamento.idarmazemorigem
7765 and l.idproduto = r.idproduto
7766 and l.idlote = lo.idlote
7767 and rownum = 1;
7768
7769 begin
7770 select l.fatorconversao
7771 into v_fatorDestino
7772 from lotelocal ld, lote l
7773 where ld.idlocal = r_remanejamento.idlocaldestino
7774 and ld.idarmazem = r_remanejamento.idarmazemdestino
7775 and l.idproduto = r.idproduto
7776 and l.idlote = ld.idlote
7777 and rownum = 1;
7778 exception
7779 when no_data_found then
7780 v_fatorDestino := 0;
7781 end;
7782
7783 if (v_fatorDestino > 0 and v_fatorDestino <> v_fatorOrigem) then
7784 v_msg := t_message('Local de origem e local de destino, devem possuir lotes ' ||
7785 'com mesmo fator, para produtos segregados. Produto sendo ' ||
7786 'remanejado {0}');
7787 v_msg.addParam(p_idremanejamento);
7788 raise_application_error(-20000, v_msg.formatMessage);
7789 end if;
7790 end loop;
7791 end validarProdutoSegregado;
7792 begin
7793 if (r_remanejamento.planejado = 'S') then
7794 v_msg := t_message('O remanejamento {0}' ||
7795 ' já possui planejamento finalizado.');
7796 v_msg.addParam(p_idremanejamento);
7797 raise_application_error(-20000, v_msg.formatMessage);
7798 end if;
7799
7800 select count(*)
7801 into v_jaPossuiLoteVinculado
7802 from loteremanejamento lr
7803 where lr.idremanejamento = p_idremanejamento;
7804
7805 if (v_jaPossuiLoteVinculado > 0) then
7806 v_msg := t_message('O remanejamento {0}' ||
7807 ' já possui lotes gerados.');
7808 v_msg.addParam(p_idremanejamento);
7809 raise_application_error(-20000, v_msg.formatMessage);
7810 end if;
7811
7812 select count(1)
7813 into v_caixaNaoInformada
7814 from armazem a
7815 where a.idarmazem = r_remanejamento.idarmazemorigem
7816 and a.indicaqtdecaixaremanejamento = 1
7817 and exists (select 1
7818 from gtt_formarloteremanejamento g
7819 where g.idtipocaixa is null);
7820
7821 if (v_caixaNaoInformada = 1) then
7822 v_msg := t_message('A configuração do armazém esta definida para que o tipo de caixa seja obrigatório. Selecione um tipo de caixa.');
7823 raise_application_error(-20000, v_msg.formatMessage);
7824 end if;
7825
7826 validarProdutoSegregado;
7827
7828 -- validar lote unico no destino
7829 for r_produto in (select g.iddepositante, g.idproduto, g.estado,
7830 g.loteindustria, g.dtvenc,
7831 pd.loteuniconoendereco
7832 from gtt_formarloteremanejamento g,
7833 produtodepositante pd
7834 where pd.idproduto = g.idproduto
7835 and pd.identidade = g.iddepositante)
7836 loop
7837 r_loteunicocomparar.idProduto := r_produto.idproduto;
7838 r_loteunicocomparar.estado := r_produto.estado;
7839 r_loteunicocomparar.loteindustria := r_produto.loteindustria;
7840 r_loteunicocomparar.dtvencimento := r_produto.dtvenc;
7841 r_loteunicocomparar.loteuniconoendereco := r_produto.loteuniconoendereco;
7842 r_loteunicocomparar.iddepositante := r_produto.iddepositante;
7843
7844 if (not pk_lote.isLocalPodeReceberLoteUnico(r_loteunicocomparar,
7845 r_remanejamento.idarmazemdestino,
7846 r_remanejamento.idlocaldestino,
7847 v_msgerro)) then
7848 raise_application_error(-20000, v_msgerro);
7849 end if;
7850 end loop;
7851
7852 select count(*)
7853 into v_existeLoteSemPeso
7854 from dual
7855 where exists (select 1
7856 from gtt_formarloteremanejamento g
7857 where g.idremanejamento = p_idremanejamento
7858 and g.peso is null
7859 and exists
7860 (select 1
7861 from configuracao c
7862 where c.ativo = 'S'
7863 and c.pesagemremanejamento = 'S'));
7864
7865 if (v_existeLoteSemPeso = 1) then
7866 v_msg := t_message('A configuração geral esta parâmetrizada para que os produtos sejam pesados, porém existem produtos não pesados.');
7867 raise_application_error(-20000, v_msg.formatMessage);
7868 end if;
7869
7870 select count(*)
7871 into v_existeInfoEspecNaoInformada
7872 from dual
7873 where exists
7874 (select 1
7875 from gtt_formarloteremanejamento g
7876 where g.idloteinfoespec is null
7877 and exists
7878 (select 1
7879 from produtodepositante pd
7880 where pd.identidade = g.iddepositante
7881 and pd.idproduto = g.idproduto
7882 and pd.rastrearinfoespecifica = 0
7883 and exists
7884 (select 1
7885 from informacaomatdep im
7886 where im.identidade = pd.identidade
7887 and im.idproduto = pd.idproduto)));
7888
7889 if (v_existeInfoEspecNaoInformada = 1) then
7890 v_msg := t_message('Todos os produtos que trabalham com informação especifica devem ser especificados.');
7891 raise_application_error(-20000, v_msg.formatMessage);
7892 end if;
7893 end validacoes;
7894
7895 procedure formarLotes is
7896 cursor c_produto is
7897 select g.idproduto, g.estado, g.iddepositante, g.itemadicional,
7898 g.loteindustria, g.dtvenc, g.dtfabricacao, g.qtde,
7899 e.fatorconversao, e.lastro, e.qtdecamada, g.idtipocaixa,
7900 g.loteadicional, g.idloteinfoespec, g.peso, g.barra,
7901 g.liberado, g.idusuariobloqueio, g.motivobloqueio,
7902 g.idmotivobloqueio, g.databloqueio, g.hashcomplotekit,
7903 e.caixafechada
7904 from gtt_formarloteremanejamento g, embalagem e
7905 where e.barra = g.barra
7906 and e.idproduto = g.idproduto;
7907
7908 r_produto c_produto%rowtype;
7909
7910 v_metodoger_incompl_sobra number;
7911 v_dispCoberturaAntes number;
7912 v_dispCoberturaDepois number;
7913
7914 procedure moverSeparacaoEspecifica
7915 (
7916 p_idloteOrigem in number,
7917 p_idarmazemorigem in number,
7918 p_idlocalorigem in varchar2,
7919 p_qtde in number,
7920 p_idloteDestino in number,
7921 p_idarmazemDestino in number,
7922 p_idlocalDestino in varchar2
7923 ) is
7924 r_sepNova separacaoespecifica%rowtype;
7925 r_seploteindsepespecif seploteindsepespecif%rowtype;
7926 v_idSepLoteInd number;
7927 begin
7928 for r_sepespecifica in (select *
7929 from separacaoespecifica se
7930 where se.idarmazem = p_idarmazemorigem
7931 and se.idlocal = p_idlocalorigem
7932 and se.idlote = p_idloteOrigem
7933 and exists
7934 (select 1
7935 from notafiscal nf
7936 where nf.tipo = 'S'
7937 and nf.statusnf in
7938 ('N', 'C', 'I', 'B')
7939 and (nf.statusroteirizacao = 0 or
7940 nf.statusroteirizacao = 1)
7941 and se.idnotafiscal =
7942 nf.idnotafiscal))
7943 loop
7944
7945 if ((r_sepespecifica.qtde - p_qtde) > 0) then
7946 update separacaoespecifica se
7947 set se.qtde = se.qtde - p_qtde
7948 where se.id = r_sepespecifica.id;
7949 pk_utilities.GeraLog(p_idUsuario,
7950 'Removeu a quantidade ' || p_qtde ||
7951 ' da separação especifica Id: ' ||
7952 r_sepespecifica.id ||
7953 ' por remanejamento picking para pulmão, remanejamento Id: ' ||
7954 p_idremanejamento, r_sepespecifica.id,
7955 'SE');
7956 end if;
7957
7958 begin
7959 select *
7960 into r_sepNova
7961 from separacaoespecifica se
7962 where se.idarmazem = p_idarmazemdestino
7963 and se.idlocal = p_idlocaldestino
7964 and se.idlote = p_idloteDestino
7965 and se.idnotafiscal = r_sepespecifica.idnotafiscal;
7966
7967 r_sepNova.Qtde := r_sepNova.qtde + p_qtde;
7968
7969 update separacaoespecifica
7970 set row = r_sepNova
7971 where id = r_sepNova.id;
7972
7973 pk_utilities.GeraLog(p_idUsuario,
7974 'Adicionou a quantidade ' || p_qtde ||
7975 ' a separação especifica Id: ' ||
7976 r_sepNova.id ||
7977 ' por remanejamento picking para pulmão, remanejamento Id: ' ||
7978 p_idremanejamento, r_sepNova.id, 'SE');
7979
7980 exception
7981 when no_data_found then
7982 select seq_separacaoespecifica.nextval
7983 into r_sepNova.id
7984 from dual;
7985 r_sepNova.idarmazem := p_idarmazemdestino;
7986 r_sepNova.idlocal := p_idlocaldestino;
7987 r_sepNova.idlote := p_idloteDestino;
7988 r_sepNova.idnotafiscal := r_sepespecifica.idnotafiscal;
7989 r_sepNova.qtde := p_qtde;
7990 r_sepNova.cadmanual := r_sepespecifica.cadmanual;
7991 r_sepNova.fonteseparacao := r_sepespecifica.fonteseparacao;
7992 r_sepNova.idusuario := p_idUsuario;
7993 r_sepNova.dtvinculo := sysdate;
7994
7995 insert into separacaoespecifica
7996 (id, idarmazem, idlocal, idlote, idnotafiscal, qtde,
7997 cadmanual, fonteseparacao, idusuario, dtvinculo)
7998 values
7999 (r_sepNova.id, r_sepNova.idarmazem, r_sepNova.idlocal,
8000 r_sepNova.idlote, r_sepNova.idnotafiscal, r_sepNova.qtde,
8001 r_sepNova.cadmanual, r_sepNova.fonteseparacao,
8002 r_sepNova.Idusuario, r_sepNova.Dtvinculo);
8003
8004 pk_utilities.GeraLog(p_idUsuario,
8005 'Criou a separação especifica Id: ' ||
8006 r_sepNova.id || ' com quantidade ' ||
8007 r_sepNova.qtde ||
8008 ' para Nota Fiscal Id: ' ||
8009 r_sepNova.Idnotafiscal ||
8010 ' para o Lote Id: ' || r_sepNova.Idlote ||
8011 ' no Local ID: ' || r_sepNova.Idlocal ||
8012 ' no Armazém Id: ' ||
8013 r_sepNova.Idarmazem ||
8014 ' por remanejamento picking para pulmão, remanejamento Id: ' ||
8015 p_idremanejamento, r_sepNova.id, 'SE');
8016 end;
8017
8018 begin
8019 select sls.idseploteind
8020 into v_idSepLoteInd
8021 from seploteindsepespecif sls
8022 where sls.idsepespecifica = r_sepespecifica.id;
8023
8024 begin
8025 select *
8026 into r_seploteindsepespecif
8027 from seploteindsepespecif s
8028 where s.idseploteind = v_idSepLoteInd
8029 and s.idsepespecifica = r_sepNova.id;
8030
8031 r_seploteindsepespecif.qtde := r_seploteindsepespecif.qtde +
8032 p_qtde;
8033
8034 update seploteindsepespecif r
8035 set row = r_seploteindsepespecif
8036 where r.idsepespecifica =
8037 r_seploteindsepespecif.idsepespecifica
8038 and r.idseploteind = r_seploteindsepespecif.idseploteind;
8039 exception
8040 when no_data_found then
8041 insert into seploteindsepespecif
8042 (idsepespecifica, idseploteind, idlote, qtde)
8043 values
8044 (r_sepNova.id, v_idSepLoteInd, r_sepNova.idlote,
8045 r_sepNova.qtde);
8046 end;
8047
8048 if ((r_sepespecifica.qtde - p_qtde) > 0) then
8049 update seploteindsepespecif s
8050 set s.qtde = s.qtde - p_qtde
8051 where s.idsepespecifica = r_sepespecifica.id;
8052
8053 else
8054 delete from seploteindsepespecif s
8055 where s.idsepespecifica = r_sepespecifica.id
8056 and s.idseploteind = v_idSepLoteInd;
8057 end if;
8058 exception
8059 when no_data_found then
8060 v_idSepLoteInd := null;
8061 end;
8062
8063 if ((r_sepespecifica.qtde - p_qtde) <= 0) then
8064 delete from separacaoespecifica se
8065 where se.id = r_sepespecifica.id;
8066
8067 pk_utilities.GeraLog(p_idUsuario,
8068 'Removeu a separação especifica Id: ' ||
8069 r_sepespecifica.id || ' com quantidade ' ||
8070 r_sepespecifica.qtde ||
8071 ' para Nota Fiscal Id: ' ||
8072 r_sepespecifica.Idnotafiscal ||
8073 ' para o Lote Id: ' ||
8074 r_sepespecifica.Idlote || ' no Local ID: ' ||
8075 r_sepespecifica.Idlocal ||
8076 ' no Armazém Id: ' ||
8077 r_sepespecifica.Idarmazem ||
8078 ' por remanejamento picking para pulmão, remanejamento Id: ' ||
8079 p_idremanejamento, r_sepespecifica.id,
8080 'SE');
8081 end if;
8082 end loop;
8083 end moverSeparacaoEspecifica;
8084
8085 procedure addNovoLoteRemanejamento
8086 (
8087 p_idlote in number,
8088 p_qtde in number,
8089 p_peso in number
8090 ) is
8091 v_complementar lotelocal.complementar%type;
8092
8093 begin
8094 insert into loteremanejamento
8095 (idremanejamento, idlote, qtde, conferido, controlaqtde,
8096 diferenca, liberado, impetiqueta, peso)
8097 values
8098 (p_idremanejamento, p_idlote, p_qtde, 'N', 'S', 0, 'S', 'S',
8099 p_peso);
8100
8101 if (p_peso is not null) then
8102 update lote lt
8103 set lt.pesobruto = p_peso
8104 where lt.idlote = p_idlote;
8105 end if;
8106
8107 v_complementar := 'ESTOQUE ADICIONADO EM FUNCAO DA FINALIZACAO DO CADASTRO DE LOTE DO REMANEJAMENTO N:' ||
8108 p_idremanejamento;
8109 pk_estoque.incluir_estoque(r_remanejamento.idarmazemorigem,
8110 r_remanejamento.idlocalorigem, p_idlote,
8111 p_qtde, p_idusuario, v_complementar, 'N');
8112
8113 v_complementar := 'AUMENTADO PENDENCIA EM FUNCAO DA INCLUSAO DO LOTE NO REMANEJAMENTO N: ' ||
8114 p_idremanejamento;
8115 pk_estoque.incluir_pendencia(r_remanejamento.idarmazemorigem,
8116 r_remanejamento.idlocalorigem, p_idlote,
8117 p_qtde, p_idusuario, v_complementar);
8118
8119 v_complementar := 'AUMENTADO ADICIONAR EM FUNCAO DA INCLUSAO DO LOTE NO REMANEJAMENTO N: ' ||
8120 p_idremanejamento;
8121 pk_estoque.incluir_adicionar(r_remanejamento.idarmazemorigem,
8122 r_remanejamento.idlocaldestino,
8123 p_idlote, p_qtde, p_idusuario,
8124 v_complementar);
8125
8126 end addNovoLoteRemanejamento;
8127
8128 procedure usarLoteUnitario(r_produto in c_produto%rowtype) is
8129 v_complementar lotelocal.complementar%type;
8130
8131 begin
8132 insert into loteremanejamento
8133 (idremanejamento, idlote, qtde, conferido, controlaqtde,
8134 diferenca, liberado, impetiqueta, datapesagem, peso)
8135 values
8136 (p_idremanejamento, r_produto.idloteinfoespec, 1, 'N', 'S', 0,
8137 'S', 'S', sysdate, r_produto.peso);
8138
8139 if (r_produto.peso is not null) then
8140 update lote lt
8141 set lt.pesobruto = r_produto.peso
8142 where lt.idlote = r_produto.idloteinfoespec;
8143 end if;
8144
8145 v_complementar := 'AUMENTADO PENDENCIA EM FUNCAO DA INCLUSAO DO LOTE NO REMANEJAMENTO N: ' ||
8146 p_idremanejamento;
8147 pk_estoque.incluir_pendencia(r_remanejamento.idarmazemorigem,
8148 r_remanejamento.idlocalorigem,
8149 r_produto.idloteinfoespec, 1,
8150 p_idusuario, v_complementar);
8151
8152 v_complementar := 'AUMENTADO ADICIONAR EM FUNCAO DA INCLUSAO DO LOTE NO REMANEJAMENTO N: ' ||
8153 p_idremanejamento;
8154 pk_estoque.incluir_adicionar(r_remanejamento.idarmazemorigem,
8155 r_remanejamento.idlocaldestino,
8156 r_produto.idloteinfoespec, 1,
8157 p_idusuario, v_complementar);
8158
8159 moverSeparacaoEspecifica(r_produto.idloteinfoespec,
8160 r_remanejamento.idarmazemorigem,
8161 r_remanejamento.idlocalorigem, 1,
8162 r_produto.idloteinfoespec,
8163 r_remanejamento.idarmazemorigem,
8164 r_remanejamento.idlocaldestino);
8165
8166 end usarLoteUnitario;
8167
8168 procedure criarNovosLotes(r_produto in c_produto%rowtype) is
8169
8170 v_qtdeUN number;
8171
8172 procedure inserirNovosLotes is
8173 r_lote lote%rowtype;
8174
8175 v_qtdeTotalCaixas number;
8176 v_normaPallet number;
8177 v_qtdePallet number;
8178 v_caixasPallet number;
8179 v_unidadesPallet number;
8180 v_qtdeLastro number;
8181 v_caixasLastro number;
8182 v_unidadesLastro number;
8183 v_qtdeCaixas number;
8184 v_unidadesCaixas number;
8185 v_qtdeUnidades number;
8186 v_idlote number;
8187 begin
8188 pk_lote.CalcularQtdeLotes(v_qtdeUN, r_produto.fatorconversao,
8189 r_produto.lastro, r_produto.qtdecamada,
8190 v_metodoger_incompl_sobra,
8191 r_produto.caixafechada,
8192 v_qtdeTotalCaixas, v_normaPallet,
8193 v_qtdePallet, v_caixasPallet,
8194 v_unidadesPallet, v_qtdeLastro,
8195 v_caixasLastro, v_unidadesLastro,
8196 v_qtdeCaixas, v_unidadesCaixas,
8197 v_qtdeUnidades);
8198
8199 -- Informando os dados do Lote
8200 r_lote.idproduto := r_produto.idproduto;
8201 r_lote.iddepositante := r_produto.iddepositante;
8202 r_lote.descr := r_produto.loteindustria;
8203 r_lote.dtVenc := r_produto.dtvenc;
8204 r_lote.estado := r_produto.estado;
8205 r_lote.idUsuario := p_idusuario;
8206 r_lote.tipolote := 'L';
8207 r_lote.situacao := 'R';
8208 r_lote.numerovolume := 1;
8209 r_lote.itemadicional := r_produto.itemadicional;
8210 r_lote.idtipocaixa := r_produto.idtipocaixa;
8211 r_lote.idarmazem := r_remanejamento.idarmazemorigem;
8212 r_lote.dtfabricacao := r_produto.dtfabricacao;
8213 r_lote.loteadicional := r_produto.loteadicional;
8214 r_lote.idusuariobloqueio := r_produto.idusuariobloqueio;
8215 r_lote.motivobloqueio := r_produto.motivobloqueio;
8216 r_lote.idmotivobloqueio := r_produto.idmotivobloqueio;
8217 r_lote.databloqueio := r_produto.databloqueio;
8218 r_lote.hashcomplotekit := r_produto.hashcomplotekit;
8219
8220 if (pk_lote.isLoteIndustriaComMaisDeUmVenc(r_produto.iddepositante,
8221 r_produto.idproduto,
8222 r_produto.loteindustria,
8223 r_produto.dtvenc)) then
8224 raise_application_error(-20000,
8225 'Não é possÃvel cadastrar lote do mesmo produto/depositante com o mesmo lote indústria e data de vencimento diferentes. Depositante:' ||
8226 r_produto.iddepositante ||
8227 '. Produto: ' || r_produto.idproduto ||
8228 ' Lote Indústria: ' ||
8229 r_produto.loteindustria ||
8230 ' Data Venc: ' || r_produto.dtvenc);
8231
8232 end if;
8233
8234 -- Gerando o Lote Pallet
8235 for i in 1 .. v_qtdePallet
8236 loop
8237 r_lote.barra := r_produto.barra;
8238 r_lote.qtdeEntrada := v_normaPallet;
8239 r_lote.tipoPalet := 'C';
8240 v_idlote := pk_lote.cadastrar_lote(r_lote, null, 'N');
8241
8242 if (r_produto.liberado = 'N') then
8243 update lote lt
8244 set lt.liberado = 'N'
8245 where lt.idlote = v_idlote;
8246 end if;
8247
8248 addNovoLoteRemanejamento(v_idlote,
8249 v_normaPallet *
8250 r_produto.fatorconversao,
8251 r_produto.peso);
8252 end loop;
8253
8254 -- Gerando o Lote Lastro
8255 if v_qtdeLastro > 0 then
8256 r_lote.barra := r_produto.barra;
8257 r_lote.qtdeEntrada := v_caixasLastro;
8258 r_lote.tipoPalet := 'I';
8259 v_idlote := pk_lote.cadastrar_lote(r_lote, null, 'N');
8260
8261 if (r_produto.liberado = 'N') then
8262 update lote lt
8263 set lt.liberado = 'N'
8264 where lt.idlote = v_idlote;
8265 end if;
8266
8267 addNovoLoteRemanejamento(v_idlote,
8268 v_caixasLastro *
8269 r_produto.fatorconversao,
8270 r_produto.peso);
8271 end if;
8272
8273 -- Gerando o Lote Quebrado - Sobra
8274 if v_qtdeCaixas > 0 then
8275 r_lote.barra := r_produto.barra;
8276 r_lote.qtdeEntrada := v_qtdeCaixas;
8277 r_lote.tipoPalet := 'S';
8278 v_idlote := pk_lote.cadastrar_lote(r_lote, null, 'N');
8279
8280 if (r_produto.liberado = 'N') then
8281 update lote lt
8282 set lt.liberado = 'N'
8283 where lt.idlote = v_idlote;
8284 end if;
8285
8286 addNovoLoteRemanejamento(v_idlote,
8287 v_qtdeCaixas *
8288 r_produto.fatorconversao,
8289 r_produto.peso);
8290 end if;
8291
8292 -- Gerando o Lote Quebrado - Unidades
8293 if v_qtdeUnidades > 0 then
8294 r_lote.barra := pk_produto.RetornarCodBarraMenorFator(r_produto.idproduto);
8295 r_lote.qtdeEntrada := v_qtdeUnidades;
8296 r_lote.tipoPalet := 'U';
8297 v_idlote := pk_lote.cadastrar_lote(r_lote, null, 'N');
8298
8299 if (r_produto.liberado = 'N') then
8300 update lote lt
8301 set lt.liberado = 'N'
8302 where lt.idlote = v_idlote;
8303 end if;
8304
8305 addNovoLoteRemanejamento(v_idlote, v_qtdeUnidades,
8306 r_produto.peso);
8307 end if;
8308 end inserirNovosLotes;
8309
8310 procedure consumirLotesAntigos is
8311 cursor c_loteantigo
8312 (
8313 pc_idarmazem number,
8314 pc_idlocal local.idlocal%type,
8315 pc_idproduto number,
8316 pc_estado lote.estado%type,
8317 pc_iddepositante number,
8318 pc_itemadicional lote.itemadicional%type,
8319 pc_dtvenc date,
8320 pc_dtfabricacao date,
8321 pc_loteindustria lote.descr%type,
8322 pc_loteadicional lote.loteadicional%type,
8323 pc_hashcomplotekit lote.hashcomplotekit%type
8324 ) is
8325 select ll.idarmazem, l.idlote,
8326 (ll.estoque - ll.pendencia) disponivelestoque,
8327 l.qtdedisponivel qtdecoberta
8328 from lotelocal ll, lote l
8329 where ll.idarmazem = pc_idarmazem
8330 and ll.idlocal = pc_idlocal
8331 and (ll.estoque - ll.pendencia + ll.adicionar) > 0
8332 and l.idlote = ll.idlote
8333 and l.idproduto = pc_idproduto
8334 and l.estado = pc_estado
8335 and l.iddepositante = pc_iddepositante
8336 and case
8337 when pc_itemadicional is null then
8338 1
8339 else
8340 decode(l.itemadicional, pc_itemadicional, 1, 0)
8341 end = 1
8342 and decode(trunc(l.dtvenc), trunc(pc_dtvenc), 1, 0) = 1
8343 and case
8344 when pc_dtfabricacao is null then
8345 1
8346 else
8347 decode(trunc(l.dtfabricacao), trunc(pc_dtfabricacao), 1,
8348 0)
8349 end = 1
8350 and decode(l.descr, pc_loteindustria, 1,
8351 decode(l.descr, 'LOTE DE PICKING PARA PULMÃO', 1, 0)) = 1
8352 and case
8353 when pc_loteadicional is null then
8354 1
8355 else
8356 decode(l.loteadicional, pc_loteadicional, 1, 0)
8357 end = 1
8358 and case
8359 when pc_hashcomplotekit is null then
8360 1
8361 else
8362 decode(l.hashcomplotekit, pc_hashcomplotekit, 1, 0)
8363 end = 1
8364 order by l.dtalocacao;
8365
8366 r_loteantigo c_loteantigo%rowtype;
8367 v_qtdeUtilizadaUN number;
8368 v_qtdeCobertaLoteAntigo number;
8369 v_complementar varchar2(200);
8370 begin
8371 v_qtdeUtilizadaUN := 0;
8372 for r_loteantigo in c_loteantigo(r_remanejamento.idarmazemorigem,
8373 r_remanejamento.idlocalorigem,
8374 r_produto.idproduto,
8375 r_produto.estado,
8376 r_produto.iddepositante,
8377 r_produto.itemadicional,
8378 r_produto.dtvenc,
8379 r_produto.dtfabricacao,
8380 r_produto.loteindustria,
8381 r_produto.loteadicional,
8382 r_produto.hashcomplotekit)
8383 loop
8384 if (r_loteantigo.disponivelestoque >= v_qtdeUN) then
8385 v_qtdeUtilizadaUN := v_qtdeUN;
8386 else
8387 v_qtdeUtilizadaUN := r_loteantigo.disponivelestoque;
8388 end if;
8389
8390 v_qtdeUN := v_qtdeUN - v_qtdeUtilizadaUN;
8391
8392 v_complementar := 'ESTOQUE RETIRADO EM FUNCAO DA FINALIZACAO DO CADASTRO DE LOTE DO REMANEJAMENTO N:' ||
8393 p_idremanejamento;
8394
8395 pk_estoque.retirar_estoque(r_loteantigo.idarmazem,
8396 r_remanejamento.idlocalorigem,
8397 r_loteantigo.idlote,
8398 v_qtdeUtilizadaUN, p_idusuario,
8399 v_complementar, 'N');
8400
8401 InserirComposicaoLote(p_idremanejamento, r_loteantigo.idlote,
8402 v_qtdeUtilizadaUN,
8403 r_loteantigo.qtdecoberta);
8404
8405 exit when v_qtdeUN = 0;
8406 end loop;
8407
8408 if (v_qtdeUN > 0) then
8409 v_msg := t_message('Não foi possivel encontrar estoque suficiente para o produto.' ||
8410 chr(13) || 'IDPRODUTO: {0}' || chr(13) ||
8411 'QTDENECESSARIA UN: {1}');
8412 v_msg.addParam(r_produto.idproduto);
8413 v_msg.addParam(r_produto.qtde * r_produto.fatorconversao);
8414 raise_application_error(-20000, v_msg.formatMessage);
8415 end if;
8416
8417 end consumirLotesAntigos;
8418
8419 procedure tratarModuloMWMS is
8420 begin
8421
8422 for r_lotenovo in (select lr.idlote
8423 from loteremanejamento lr
8424 where lr.idremanejamento = p_idremanejamento)
8425 loop
8426 update lote lt
8427 set (lt.dtfabricacao, lt.loteadicional, lt.itemadicional) =
8428 (select la.dtfabricacao, la.loteadicional,
8429 la.itemadicional
8430 from composicaolote cl, lote la
8431 where cl.idlotenovo = lt.idlote
8432 and la.idlote = cl.idloteanterior
8433 and rownum = 1)
8434 where lt.idlote = r_lotenovo.idlote;
8435 end loop;
8436
8437 end tratarModuloMWMS;
8438
8439 begin
8440 v_qtdeUN := r_produto.qtde * r_produto.fatorconversao;
8441
8442 inserirNovosLotes;
8443
8444 consumirLotesAntigos;
8445
8446 if (p_moduloSistema = MODULO_MWMS) then
8447 tratarModuloMWMS;
8448 end if;
8449
8450 end criarNovosLotes;
8451
8452 function getQtdeDispCobertura return number is
8453 v_qtde number;
8454 begin
8455 select sum(lt.qtdedisponivel)
8456 into v_qtde
8457 from lotelocal ll, lote lt
8458 where ll.idarmazem = r_remanejamento.idarmazemorigem
8459 and ll.idlocal = r_remanejamento.idlocalorigem
8460 and lt.idlote = ll.idlote;
8461
8462 return v_qtde;
8463 end getQtdeDispCobertura;
8464
8465 procedure moverSepEspecLotesNovos is
8466 v_qtdeMover number;
8467 v_qtdeEstoqueDispLtAnt number;
8468 v_qtdeEmSepEspec number;
8469 begin
8470 for r_lt_spEspecifica in (select lr.idlote idLoteNovo,
8471 cl.qtde qtdeFormadaLtNovo,
8472 cl.idloteanterior
8473 from loteremanejamento lr,
8474 composicaolote cl
8475 where lr.idremanejamento =
8476 p_idremanejamento
8477 and cl.idlotenovo = lr.idlote
8478 and exists
8479 (select 1
8480 from separacaoespecifica sep
8481 where sep.idlote =
8482 cl.idloteanterior
8483 and sep.idlocal =
8484 r_remanejamento.idlocalorigem
8485 and sep.idarmazem =
8486 r_remanejamento.idarmazemorigem
8487 and exists
8488 (select 1
8489 from notafiscal nf
8490 where nf.tipo = 'S'
8491 and nf.statusnf in
8492 ('N', 'C', 'I', 'B')
8493 and (nf.statusroteirizacao = 0 or
8494 nf.statusroteirizacao = 1)
8495 and sep.idnotafiscal =
8496 nf.idnotafiscal)))
8497 loop
8498 select sum(ll.estoque + ll.adicionar - ll.pendencia) qtde
8499 into v_qtdeEstoqueDispLtAnt
8500 from lotelocal ll
8501 where ll.idarmazem = r_remanejamento.idarmazemorigem
8502 and ll.idlocal = r_remanejamento.idlocalorigem
8503 and ll.idlote = r_lt_spEspecifica.Idloteanterior
8504 group by ll.idarmazem, ll.idlocal, ll.idlote;
8505
8506 select sum(sep.qtde)
8507 into v_qtdeEmSepEspec
8508 from separacaoespecifica sep
8509 where sep.idlote = r_lt_spEspecifica.Idloteanterior
8510 and sep.idarmazem = r_remanejamento.idarmazemorigem
8511 and sep.idlocal = r_remanejamento.idlocalorigem
8512 and exists
8513 (select 1
8514 from notafiscal nf
8515 where nf.tipo = 'S'
8516 and nf.statusnf in ('N', 'C', 'I', 'B')
8517 and (nf.statusroteirizacao = 0 or
8518 nf.statusroteirizacao = 1)
8519 and sep.idnotafiscal = nf.idnotafiscal)
8520 group by sep.idlote, sep.idlocal, sep.idarmazem;
8521
8522 if (v_qtdeEstoqueDispLtAnt >= v_qtdeEmSepEspec) then
8523 v_qtdeMover := 0;
8524 end if;
8525
8526 if (v_qtdeEstoqueDispLtAnt < v_qtdeEmSepEspec) then
8527 if (r_lt_spEspecifica.Qtdeformadaltnovo <=
8528 (v_qtdeEmSepEspec - v_qtdeEstoqueDispLtAnt)) then
8529 v_qtdeMover := r_lt_spEspecifica.Qtdeformadaltnovo;
8530 end if;
8531
8532 if (r_lt_spEspecifica.Qtdeformadaltnovo >
8533 (v_qtdeEmSepEspec - v_qtdeEstoqueDispLtAnt)) then
8534 v_qtdeMover := r_lt_spEspecifica.Qtdeformadaltnovo -
8535 (v_qtdeEmSepEspec - v_qtdeEstoqueDispLtAnt);
8536 end if;
8537 end if;
8538
8539 if (v_qtdeMover > 0) then
8540 moverSeparacaoEspecifica(r_lt_spEspecifica.Idloteanterior,
8541 r_remanejamento.idarmazemorigem,
8542 r_remanejamento.idlocalorigem,
8543 v_qtdeMover,
8544 r_lt_spEspecifica.Idlotenovo,
8545 r_remanejamento.idarmazemdestino,
8546 r_remanejamento.idlocaldestino);
8547 end if;
8548
8549 end loop;
8550 end moverSepEspecLotesNovos;
8551
8552 begin
8553
8554 select ar.metodogerlotesobraincompl
8555 into v_metodoger_incompl_sobra
8556 from armazem ar
8557 where ar.idarmazem = r_remanejamento.idarmazemorigem;
8558
8559 v_dispCoberturaAntes := getQtdeDispCobertura;
8560
8561 for r_produto in c_produto
8562 loop
8563
8564 if (r_produto.idloteinfoespec is null) then
8565 criarNovosLotes(r_produto);
8566 else
8567 usarLoteUnitario(r_produto);
8568 end if;
8569
8570 end loop;
8571
8572 moverSepEspecLotesNovos;
8573
8574 v_dispCoberturaDepois := getQtdeDispCobertura;
8575
8576 if (v_dispCoberturaAntes <> v_dispCoberturaDepois) then
8577 v_msg := t_message('A quantidade disponivel de cobertura não deve ser alterada por remanejamento de picking para pulmão.');
8578 raise_application_error(-20000, v_msg.formatMessage);
8579 end if;
8580
8581 end formarLotes;
8582
8583 procedure controlarPesagemLotes is
8584 v_pesar configuracao.pesagemremanejamento%type;
8585 begin
8586 select c.pesagemremanejamento
8587 into v_pesar
8588 from configuracao c
8589 where c.ativo = 'S';
8590
8591 if (v_pesar = 'N') then
8592 for r_lote in (select lr.idlote, (e.pesobruto * lr.qtde) pesoteorico
8593 from loteremanejamento lr, lote l, embalagem e
8594 where e.barra = l.barra
8595 and e.idproduto = l.idproduto
8596 and l.idlote = lr.idlote
8597 and lr.idremanejamento = p_idremanejamento
8598 and not exists
8599 (select 1
8600 from gtt_formarloteremanejamento g
8601 where g.idloteinfoespec = lr.idlote))
8602 loop
8603 update loteremanejamento
8604 set liberado = 'S',
8605 peso = r_lote.pesoteorico,
8606 datapesagem = sysdate
8607 where idremanejamento = p_idremanejamento
8608 and idlote = r_lote.idlote;
8609 end loop;
8610 end if;
8611
8612 end controlarPesagemLotes;
8613
8614 procedure valorarLotes is
8615 begin
8616 for c_loteremanejamento in (select lr.idlote
8617 from loteremanejamento lr
8618 where lr.idremanejamento =
8619 p_idremanejamento
8620 and not exists
8621 (select 1
8622 from gtt_formarloteremanejamento g
8623 where g.idloteinfoespec =
8624 lr.idlote))
8625 loop
8626 pk_lote.valorarLotePeloLoteAnterior(c_loteremanejamento.idlote);
8627
8628 pk_lote.atualizaInfLote(c_loteremanejamento.idlote,
8629 C_MOV_CADASTRO_LOTE);
8630 end loop;
8631 end valorarLotes;
8632
8633 procedure apagarPickingDinamico is
8634 begin
8635 for r_picking in (select distinct lt.idproduto, lt.iddepositante
8636 from loteremanejamento lr, lote lt
8637 where lr.idremanejamento = p_idremanejamento
8638 and lt.idlote = lr.idlote)
8639 loop
8640 pk_picking_dinamico.addGttPickingDinamico(r_picking.idproduto,
8641 r_picking.iddepositante,
8642 r_remanejamento.idlocalorigem,
8643 r_remanejamento.idarmazemorigem,
8644 0);
8645
8646 end loop;
8647
8648 pk_picking_dinamico.deletar_picking_dinamico;
8649 end apagarPickingDinamico;
8650
8651 begin
8652
8653 select *
8654 into r_remanejamento
8655 from remanejamento r
8656 where r.idremanejamento = p_idremanejamento
8657 for update;
8658
8659 validacoes;
8660
8661 formarLotes;
8662
8663 controlarPesagemLotes;
8664
8665 valorarLotes;
8666
8667 apagarPickingDinamico;
8668
8669 update remanejamento
8670 set planejado = 'S'
8671 where idremanejamento = p_idremanejamento;
8672
8673 pk_utilities.GeraLog(p_idusuario,
8674 'LOTES FORMADOS DE PICKING PARA PULMÃO (MODULO SILTWMS). ' ||
8675 chr(13) || 'IDREMANEJAMENTO: ' ||
8676 p_idremanejamento, p_idremanejamento, 'R');
8677
8678 for c_histContratoLote in (select cl.idlote, cl.idcontrato, cl.prazotroca,
8679 cl.status
8680 from loteremanejamento lr, contratolote cl
8681 where lr.idremanejamento = p_idRemanejamento
8682 and cl.idlote = lr.idlote)
8683
8684 loop
8685 if c_histContratoLote.Idcontrato is null then
8686 v_mgsContratoLote := 'INCLUÃDO REGISTRO PARA VINCULAR CONTRATO AO LOTE ID: ' ||
8687 c_histContratoLote.Idlote ||
8688 ' EM FUNÇÃO DO REMANEJAMENTO ID: ' ||
8689 p_idRemanejamento;
8690 else
8691 v_mgsContratoLote := 'VINCULADO O LOTE ID: ' ||
8692 c_histContratoLote.Idlote ||
8693 ' AO CONTRATO ID: ' ||
8694 c_histContratoLote.Idcontrato ||
8695 ' EM FUNÇÃO DO REMANEJAMENTO ID: ' ||
8696 p_idRemanejamento;
8697 end if;
8698 pk_contrato.historicoMudancaContratoLote(c_histContratoLote.Idcontrato,
8699 c_histContratoLote.Idlote,
8700 c_histContratoLote.Prazotroca,
8701 p_idusuario, 1,
8702 v_mgsContratoLote,
8703 c_histContratoLote.Status);
8704 end loop;
8705 end formarLotePickingPulmao;
8706
8707 procedure gerarRemPerdaCorteFisico(p_idusuario in number) is
8708 cursor c_lotesRemanejar is
8709 select g.idarmazem, g.idlocalorigem, g.idlote, g.qtde, g.idcortefisico,
8710 g.idromaneio, e.comprimento * e.altura * e.largura cubagem,
8711 e.pesobruto, e.barra, lt.idproduto, lt.iddepositante,
8712 e.descrreduzido, l.picking, nvl(p.fracionado, 'N') fracionado
8713 from GTT_REMANEJA_CORTE G, lote lt, embalagem e, local l, produto p
8714 where lt.idlote = g.idlote
8715 and lt.idarmazem = g.idarmazem
8716 and e.barra = lt.barra
8717 and e.idproduto = lt.idproduto
8718 and l.idlocal = g.idlocalorigem
8719 and l.idarmazem = g.idarmazem
8720 and p.idproduto = lt.idproduto;
8721
8722 cursor c_locaisDisponiveisSetorPerda
8723 (
8724 p_idArmazem number,
8725 p_iddepositante number,
8726 p_idproduto number,
8727 p_idlocal local.idlocal%type
8728 ) is
8729 select lp.idlocal,
8730 (lp.cubagemMaxima - nvl(lp.cubagemOcupada, 0)) cubagemDisponivel,
8731 (lp.pesomaximo - nvl(lp.pesoOcupado, 0)) pesoDisponivel
8732 from (select l.idlocal,
8733 (l.comprimento * (l.altura - l.alturamanobra) *
8734 l.largura) cubagemMaxima, l.pesomaximo,
8735 (select sum((e.comprimento * e.largura * e.altura) *
8736 (ll.estoque + ll.adicionar))
8737 from lotelocal ll, lote lt, embalagem e
8738 where ll.idarmazem = l.idarmazem
8739 and ll.idlocal = l.idlocal
8740 and lt.idlote = ll.idlote
8741 and e.barra = lt.barra
8742 group by ll.idlocal) cubagemOcupada,
8743 (select sum(e.pesobruto * (ll.estoque + ll.adicionar))
8744 from lotelocal ll, lote lt, embalagem e
8745 where ll.idarmazem = l.idarmazem
8746 and ll.idlocal = l.idlocal
8747 and lt.idlote = ll.idlote
8748 and e.barra = lt.barra
8749 group by ll.idlocal) pesoOcupado
8750 from local l, setor s
8751 where l.idarmazem = p_idArmazem
8752 and s.idsetor = l.idsetor
8753 and s.perda = 1
8754 and exists (select 1
8755 from setordepositante sd
8756 where sd.idsetor = l.idsetor
8757 and sd.iddepositante = p_iddepositante)
8758 and exists (select 1
8759 from setorproduto sp
8760 where sp.idsetor = l.idsetor
8761 and sp.idproduto = p_idproduto)
8762 and l.idlocal <> p_idlocal) lp;
8763
8764 r_lotesRemanejar c_lotesRemanejar%rowtype;
8765 r_locaisDisponiveisSetorPerda c_locaisDisponiveisSetorPerda%rowtype;
8766 v_idRemanejamento number;
8767 v_quantidadeAlocar number;
8768 v_qtdRemanejar number;
8769 v_qtdRemanejada number;
8770 v_qtdPossivelAlocar number;
8771 v_msg t_message;
8772 begin
8773 open c_lotesRemanejar;
8774 fetch c_lotesRemanejar
8775 into r_lotesRemanejar;
8776
8777 if (c_lotesRemanejar%notfound) then
8778 close c_lotesRemanejar;
8779 return;
8780 end if;
8781
8782 while (c_lotesRemanejar%found)
8783 loop
8784 if (r_lotesRemanejar.Picking = 'N') then
8785 pk_estoque.retirar_pendencia(r_lotesRemanejar.Idarmazem,
8786 r_lotesRemanejar.Idlocalorigem,
8787 r_lotesRemanejar.Idlote,
8788 r_lotesRemanejar.Qtde, p_idusuario,
8789 p_complementar => 'RETIRANDO PENDENCIA PARA MOVIMENTACAO PARA SETOR DE PERDA IDCORTE: ' ||
8790 r_lotesRemanejar.Idcortefisico,
8791 p_idmovimentacao => NULL);
8792 end if;
8793
8794 v_qtdRemanejar := r_lotesRemanejar.Qtde;
8795
8796 if (c_locaisDisponiveisSetorPerda%isopen) then
8797 close c_locaisDisponiveisSetorPerda;
8798 end if;
8799
8800 open c_locaisDisponiveisSetorPerda(r_lotesRemanejar.Idarmazem,
8801 r_lotesRemanejar.Iddepositante,
8802 r_lotesRemanejar.Idproduto,
8803 r_lotesRemanejar.Idlocalorigem);
8804 fetch c_locaisDisponiveisSetorPerda
8805 into r_locaisDisponiveisSetorPerda;
8806
8807 if (c_locaisDisponiveisSetorPerda%notfound) then
8808 v_msg := t_message('Não foi possivel encontrar locais em setor de perdas para remanejar o produto Id: ' ||
8809 '{0} pertencente ao depositante Id: ' ||
8810 '{1} em função do aceite do corte fÃsico Id: {2}');
8811 v_msg.addParam(r_lotesRemanejar.Idproduto);
8812 v_msg.addParam(r_lotesRemanejar.Iddepositante);
8813 v_msg.addParam(r_lotesRemanejar.Idcortefisico);
8814 raise_application_error(-20000, v_msg.formatMessage);
8815 end if;
8816
8817 while ((c_locaisDisponiveisSetorPerda%found) and (v_qtdRemanejar > 0))
8818 loop
8819 v_idRemanejamento := 0;
8820 v_qtdPossivelAlocar := 0;
8821 if ((r_locaisDisponiveisSetorPerda.Cubagemdisponivel /
8822 r_lotesRemanejar.Cubagem) > (r_locaisDisponiveisSetorPerda.Pesodisponivel /
8823 r_lotesRemanejar.Pesobruto)) then
8824 v_qtdPossivelAlocar := r_locaisDisponiveisSetorPerda.Pesodisponivel /
8825 r_lotesRemanejar.Pesobruto;
8826
8827 else
8828 v_qtdPossivelAlocar := r_locaisDisponiveisSetorPerda.Cubagemdisponivel /
8829 r_lotesRemanejar.Cubagem;
8830 end if;
8831
8832 if (r_lotesRemanejar.Fracionado = 'N') then
8833 v_qtdPossivelAlocar := trunc(v_qtdPossivelAlocar);
8834 end if;
8835
8836 if (v_qtdPossivelAlocar > 0) then
8837 v_idRemanejamento := cadastrar_remanejamento(r_lotesRemanejar.Idarmazem,
8838 r_lotesRemanejar.Idarmazem,
8839 r_lotesRemanejar.Idlocalorigem,
8840 r_locaisDisponiveisSetorPerda.Idlocal,
8841 p_idusuario, null,
8842 'REMANEJAMENTO PARA SETOR DE PERDA, CORTE FÃSICO ID:' ||
8843 r_lotesRemanejar.idcortefisico,
8844 'N', 'S');
8845 if (v_qtdPossivelAlocar < v_qtdRemanejar) then
8846 v_qtdRemanejada := v_qtdPossivelAlocar;
8847 else
8848 v_qtdRemanejada := v_qtdRemanejar;
8849 end if;
8850
8851 -- caso remanejamento seja origem picking destino pulmao,
8852 -- inclui adiconar e pendencia manualmente e nao gera lote,
8853 -- caso contrario usa triger da loteremanejamento
8854 if (r_lotesRemanejar.Picking = 'S') then
8855 pk_triggers_control.disableTrigger('T_ALTERA_LOTEREMANEJAMENTO');
8856
8857 insert into loteremanejamento
8858 (idremanejamento, idlote, qtde, conferido, controlaqtde,
8859 diferenca)
8860 values
8861 (v_idRemanejamento, r_lotesRemanejar.Idlote, v_qtdRemanejada,
8862 c_nao, c_sim, 0);
8863
8864 pk_triggers_control.enableTrigger('T_ALTERA_LOTEREMANEJAMENTO');
8865
8866 pk_estoque.incluir_adicionar(r_lotesRemanejar.Idarmazem,
8867 r_locaisDisponiveisSetorPerda.Idlocal,
8868 r_lotesRemanejar.Idlote,
8869 v_qtdRemanejada, p_idusuario,
8870 'AUMENTADO ADICIONAR EM FUNCAO DA INCLUSAO DO LOTE NO REMANEJAMENTO N: ' ||
8871 v_idRemanejamento);
8872 else
8873 insert into loteremanejamento
8874 (idremanejamento, idlote, qtde, conferido, controlaqtde,
8875 diferenca)
8876 values
8877 (v_idRemanejamento, r_lotesRemanejar.Idlote, v_qtdRemanejada,
8878 c_nao, c_sim, 0);
8879 end if;
8880
8881 finalizarRemanejamento(v_idRemanejamento, p_idusuario);
8882
8883 insert into cortefisicoremanejamento
8884 (id, idcortefisico, idremanejamento)
8885 values
8886 (seq_cortefisicoremanejamento.nextval,
8887 r_lotesremanejar.idcortefisico, v_idremanejamento);
8888
8889 update remanejamento r
8890 set r.origemCorte = 1
8891 where r.idremanejamento = v_idRemanejamento;
8892
8893 v_qtdRemanejar := v_qtdRemanejar - v_qtdRemanejada;
8894 end if;
8895
8896 fetch c_locaisDisponiveisSetorPerda
8897 into r_locaisDisponiveisSetorPerda;
8898 end loop;
8899
8900 if (v_qtdRemanejar > 0) then
8901 v_msg := t_message('Não foi possÃvel remanejar toda a quantidade ' ||
8902 '{0} {1} do lote {2} do produto Id:' ||
8903 '{3} pertencente ao depositante Id: ' ||
8904 '{4} para locais de setor de perda.');
8905 v_msg.addParam(r_lotesRemanejar.Qtde);
8906 v_msg.addParam(r_lotesRemanejar.Descrreduzido);
8907 v_msg.addParam(r_lotesRemanejar.Idlote);
8908 v_msg.addParam(r_lotesRemanejar.Idproduto);
8909 v_msg.addParam(r_lotesRemanejar.Iddepositante);
8910 raise_application_error(-20000, v_msg.formatMessage);
8911 end if;
8912
8913 fetch c_lotesRemanejar
8914 into r_lotesRemanejar;
8915 end loop;
8916
8917 if (c_locaisDisponiveisSetorPerda%isopen) then
8918 close c_locaisDisponiveisSetorPerda;
8919 end if;
8920
8921 if (c_lotesRemanejar%isopen) then
8922 close c_lotesRemanejar;
8923 end if;
8924
8925 end gerarRemPerdaCorteFisico;
8926
8927 procedure verificarpendencia
8928 (
8929 p_idremanejamento in number,
8930 p_idromaneio in number,
8931 p_idusuario in number
8932 ) is
8933 p_mensagem varchar2(4000);
8934 v_msg t_message;
8935 begin
8936
8937 p_mensagem := 'O REMANEJAMENTO ' || p_idremanejamento ||
8938 ' GERADO PELA ONDA/ROMANEIO ID ' || p_idromaneio ||
8939 ' NÃO PODE SER EXCLUIDO POIS A QUANTIDADE A SER RETIRADA ' ||
8940 'É MAIOR DO QUE A QUANTIDADE DISPONIVEL DO LOTE NO ENDEREÇO:';
8941
8942 for c_lote in (select lr.idlote, lr.qtde, r.idlocaldestino,
8943 r.idarmazemdestino,
8944 (ll.estoque + ll.adicionar - ll.pendencia) disp,
8945 ll.estoque, ll.adicionar, ll.pendencia
8946 from loteremanejamento lr, remanejamento r, lotelocal ll
8947 where r.idremanejamento = p_idremanejamento
8948 and r.idremanejamento = lr.idremanejamento
8949 and ll.idlocal = r.idlocaldestino
8950 and ll.idlote = lr.idlote
8951 and lr.qtde >=
8952 (ll.estoque + ll.adicionar - ll.pendencia)
8953 and r.idremanejamento not in
8954 (select pr.idremanejamento
8955 from produtorecuperado pr
8956 where pr.idremanejamento = p_idremanejamento)
8957 and r.idremanejamento not in
8958 (select rr.idremanejamento
8959 from controleavaria ca, remanejamento rr
8960 where ca.idcontroleavaria = rr.idcontroleavaria
8961 and rr.idremanejamento = p_idremanejamento))
8962 loop
8963 p_mensagem := substr(p_mensagem || chr(13) || '- LOTE: ' ||
8964 c_lote.idlote || ' / QUANTIDADE REMANEJAMENTO: ' ||
8965 c_lote.qtde ||
8966 ' / QUANTIDADES NO DESTINO: (DISP ' ||
8967 c_lote.disp || ', EST ' || c_lote.estoque ||
8968 ', ADIC ' || c_lote.adicionar || ', PEND ' ||
8969 c_lote.pendencia || ')' || ' / OPERAÇÕES:', 1,
8970 4000);
8971
8972 for c_pend in (select iddepositante, idproduto, idlote, idarmazem,
8973 idlocal,
8974 decode(tipo, 0, 'PICKING', 1, 'PULMÃO BLOCADO', 2,
8975 'PULMÃO PALETIZADO', 3, 'COLMÉIA', 4,
8976 'DOCA', 5, 'AUDITORIA', 6, 'RUA EXPEDICAO',
8977 7, 'STAGE', 8, 'PACKING') tipolocal,
8978 buffer, sum(pendencia) pendencia, tipooperacao,
8979 operacao
8980 from ( -- localiza pendencia por onda de picking e pulmão
8981 select lt.iddepositante, lt.idproduto, m.idlote,
8982 lo.idarmazem, lo.idlocal, lo.tipo,
8983 lo.buffer, m.qtdemovimentada pendencia,
8984 'ONDA' tipooperacao, m.idonda operacao
8985 from movimentacao m, local lo, romaneiopai r,
8986 lote lt
8987 where m.status in (0, 1)
8988 and m.etapa = 1
8989 and lo.id = m.idlocalorigem
8990 and lo.tipo < 3
8991 and lo.buffer = 'N'
8992 and r.idromaneio = m.idonda
8993 and r.processado <> 'S'
8994 and lt.idlote = m.idlote
8995 and m.idonda != p_idromaneio
8996 union all
8997 -- localiza pendencia por onda para reabastecimento do picking
8998 select lt.iddepositante, lt.idproduto, m.idlote,
8999 lo.idarmazem, lo.idlocal, lo.tipo,
9000 lo.buffer, m.qtdemovimentada pendencia,
9001 'ONDA' tipooperacao, m.idonda operacao
9002 from movimentacao m, local lo, romaneiopai r,
9003 lote lt
9004 where m.status in (0, 1)
9005 and m.etapa = 3
9006 and lo.id = m.idlocalorigem
9007 and lo.tipo < 3
9008 and lo.buffer = 'N'
9009 and r.idromaneio = m.idonda
9010 and r.processado <> 'S'
9011 and lt.idlote = m.idlote
9012 and m.idonda != p_idromaneio
9013 and exists
9014 (select 1
9015 from grupomovimentacao g, movimentacao ma
9016 where g.idgrupo = m.id
9017 and ma.id = g.idmovimentacao
9018 and ma.etapa < m.etapa
9019 and ma.status = 2)
9020 union all
9021 -- localiza pendencia por onda de colmeia
9022 select iddepositante, idproduto, idlote, idarmazem,
9023 idlocal, tipo, buffer,
9024 sum(pendencia) pendencia, tipooperacao,
9025 operacao
9026 from (select lt.iddepositante, lt.idproduto,
9027 m.idlote, lo.idarmazem, lo.idlocal,
9028 lo.tipo, lo.buffer,
9029 m.qtdemovimentada pendencia,
9030 'ONDA' tipooperacao,
9031 m.idonda operacao
9032 from movimentacao m, local lo,
9033 romaneiopai r, lote lt
9034 where lo.id = m.idlocaldestino
9035 and lo.tipo = 3
9036 and lo.buffer = 'N'
9037 and m.status = 2
9038 and r.idromaneio = m.idonda
9039 and r.processado <> 'S'
9040 and lt.idlote = m.idlote
9041 and m.idonda != p_idromaneio
9042 union all
9043 select lt.iddepositante, lt.idproduto,
9044 c.idlote, lo.idarmazem, lo.idlocal,
9045 lo.tipo, lo.buffer,
9046 c.quantidade * -1 pendencia,
9047 'ONDA' tipooperacao,
9048 m.idonda operacao
9049 from movimentacao m, local lo,
9050 volumeromaneio vr, conteudovolume c,
9051 romaneiopai r, lote lt
9052 where lo.id = m.idlocalorigem
9053 and lo.tipo = 3
9054 and m.status = 2
9055 and vr.idvolumeromaneio =
9056 m.idvolumeromaneio
9057 and c.idvolumeromaneio =
9058 vr.idvolumeromaneio
9059 and r.idromaneio = m.idonda
9060 and r.processado <> 'S'
9061 and lt.idlote = c.idlote
9062 and m.idonda != p_idromaneio)
9063 group by iddepositante, idproduto, idlote,
9064 idarmazem, idlocal, tipo, buffer,
9065 tipooperacao, operacao
9066 union all
9067 -- localiza pendencia por onda da Rua Expedição ou Doca
9068 select lt.iddepositante, lt.idproduto, c.idlote,
9069 lo.idarmazem, lo.idlocal, lo.tipo,
9070 lo.buffer, c.quantidade pendencia,
9071 'ONDA' tipooperacao, m.idonda operacao
9072 from movimentacao m, local lo, volumeromaneio vr,
9073 conteudovolume c, romaneiopai r, lote lt
9074 where lo.id = m.idlocaldestino
9075 and m.status = 2
9076 and vr.idvolumeromaneio = m.idvolumeromaneio
9077 and c.idvolumeromaneio = vr.idvolumeromaneio
9078 and r.idromaneio = m.idonda
9079 and r.processado <> 'S'
9080 and lt.idlote = c.idlote
9081 and m.idonda != p_idromaneio
9082 union all
9083 -- localiza pendencia por onda do buffer de pulmão
9084 select iddepositante, idproduto, idlote, idarmazem,
9085 idlocal, tipo, buffer,
9086 sum(pendencia) pendencia, tipooperacao,
9087 operacao
9088 from (select lt.iddepositante, lt.idproduto,
9089 c.idlote, lo.idarmazem, lo.idlocal,
9090 lo.tipo, lo.buffer,
9091 c.quantidade pendencia,
9092 'ONDA' tipooperacao,
9093 m.idonda operacao
9094 from movimentacao m, local lo,
9095 volumeromaneio vr, conteudovolume c,
9096 romaneiopai r, lote lt
9097 where lo.id = m.idlocalorigem
9098 and lo.buffer = 'S'
9099 and m.status in (0, 1)
9100 and vr.idvolumeromaneio =
9101 m.idvolumeromaneio
9102 and c.idvolumeromaneio =
9103 vr.idvolumeromaneio
9104 and r.idromaneio = m.idonda
9105 and r.processado <> 'S'
9106 and lt.idlote = c.idlote
9107 and m.idonda != p_idromaneio
9108 union all
9109 select lt.iddepositante, lt.idproduto,
9110 m.idlote, lo.idarmazem, lo.idlocal,
9111 lo.tipo, lo.buffer,
9112 (m.qtdemovimentada * -1) pendencia,
9113 'ONDA' tipooperacao,
9114 m.idonda operacao
9115 from movimentacao m, local lo,
9116 romaneiopai r, lote lt
9117 where m.status in (0, 1)
9118 and m.etapa = 1
9119 and lo.id = m.idlocaldestino
9120 and lo.tipo < 3
9121 and lo.buffer = 'S'
9122 and r.idromaneio = m.idonda
9123 and r.processado <> 'S'
9124 and lt.idlote = m.idlote
9125 and m.idonda != p_idromaneio)
9126 group by iddepositante, idproduto, idlote,
9127 idarmazem, idlocal, tipo, buffer,
9128 tipooperacao, operacao
9129 union all
9130 -- localiza pendencia por onda do buffer de picking
9131 select lt.iddepositante, lt.idproduto, m.idlote,
9132 lo.idarmazem, lo.idlocal, lo.tipo,
9133 lo.buffer, m.qtdemovimentada pendencia,
9134 'ONDA' tipooperacao, m.idonda operacao
9135 from movimentacao m, local lo, romaneiopai r,
9136 lote lt
9137 where m.status in (0, 1)
9138 and m.etapa = 2
9139 and lo.id = m.idlocalorigem
9140 and lo.tipo = 0
9141 and lo.buffer = 'S'
9142 and r.idromaneio = m.idonda
9143 and r.processado <> 'S'
9144 and lt.idlote = m.idlote
9145 and m.idonda != p_idromaneio
9146 union all
9147 -- localiza pendencia por romaneio
9148 select lt.iddepositante, lt.idproduto, ps.idlote,
9149 ps.idarmazem, ps.idlocal, lo.tipo,
9150 lo.buffer, ps.qtdeunit pendencia,
9151 'ROMANEIO' tipooperacao,
9152 r.idromaneio operacao
9153 from paletseparacao ps, romaneiopai r, lote lt,
9154 local lo
9155 where r.idromaneio = ps.idromaneio
9156 and r.processado = 'N'
9157 and r.tipo = 0
9158 and lt.idlote = ps.idlote
9159 and lo.idarmazem = ps.idarmazem
9160 and lo.idlocal = ps.idlocal
9161 and r.idromaneio != p_idromaneio
9162 union all
9163 -- localiza pendencia por ajuste de saida
9164 select lt.iddepositante, lt.idproduto, a.idlote,
9165 a.idarmazem, a.idlocal, lo.tipo, lo.buffer,
9166 a.qtde pendencia,
9167 'AJUSTE SAIDA' tipooperacao,
9168 a.idajustemovto operacao
9169 from ajustemovtosaida a, ajustemovto aj, lote lt,
9170 local lo
9171 where aj.idajustemovto = a.idajustemovto
9172 and aj.gerado = 'N'
9173 and lt.idlote = a.idlote
9174 and lo.idarmazem = a.idarmazem
9175 and lo.idlocal = a.idlocal
9176 union all
9177 -- localiza pendencia por remanejamento
9178 select lt.iddepositante, lt.idproduto, lr.idlote,
9179 r.idarmazemorigem idarmazem,
9180 r.idlocalorigem idlocal, lo.tipo, lo.buffer,
9181 lr.qtde pendencia,
9182 'REMANEJAMENTO' tipooperacao,
9183 r.idremanejamento operacao
9184 from loteremanejamento lr, remanejamento r,
9185 lote lt, local lo
9186 where r.idremanejamento = lr.idremanejamento
9187 and r.status <> 'F'
9188 and lt.idlote = lr.idlote
9189 and lo.idarmazem = r.idarmazemorigem
9190 and lo.idlocal = r.idlocalorigem
9191 and r.idremanejamento != p_idremanejamento)
9192 where idlote = c_lote.idlote
9193 and idlocal = c_lote.idlocaldestino
9194 and idarmazem = c_lote.idarmazemdestino
9195 group by iddepositante, idproduto, idlote, idarmazem,
9196 idlocal,
9197 decode(tipo, 0, 'PICKING', 1, 'PULMÃO BLOCADO',
9198 2, 'PULMÃO PALETIZADO', 3, 'COLMÉIA', 4,
9199 'DOCA', 5, 'AUDITORIA', 6,
9200 'RUA EXPEDICAO', 7, 'STAGE', 8,
9201 'PACKING'), buffer, tipooperacao,
9202 operacao
9203 -- having sum(pendencia) <> sum(pendenciaonda + pendenciaromaneio + pendenciaajustesaida + pendenciaremanejamento + pendenciadecorte)
9204 order by idlote, tipolocal)
9205 loop
9206 p_mensagem := substr(p_mensagem || chr(13) || '-- ' ||
9207 c_pend.tipooperacao || ' ID ' ||
9208 c_pend.operacao || ' QUANTIDADE ' ||
9209 c_pend.pendencia, 1, 4000);
9210 end loop;
9211 end loop;
9212
9213 p_mensagem := substr(p_mensagem || chr(13) || 'SQLERR ' || sqlerrm, 1,
9214 4000);
9215 pk_utilities.GeraLog(p_idusuario, p_mensagem, p_idremanejamento, 'DR');
9216 end;
9217
9218 /*
9219 * rotina responsavel por desfazer o romaneio.
9220 */
9221 procedure desfazerRemanejamento
9222 (
9223 p_romaneio in number,
9224 p_usuario in number,
9225 p_tipo in number,
9226 p_idnotafiscal in number
9227 ) is
9228 v_QtdeLotesExcluidos number := 0;
9229
9230 procedure corrigeSepEspecificaOnda is
9231 r_sepespecifica separacaoespecifica%rowtype;
9232 v_idNovaSepEspecifica number;
9233 v_idSepLoteInd number;
9234 v_loteuniconoendereco number;
9235 v_qtdeSepLoteInd number;
9236 begin
9237 for c_rem in (select r.idlocalorigem, r.idlocaldestino, rr.idlote,
9238 m.idnotafiscal, rp.idarmazem
9239 from remanejamentoromaneio rr, remanejamento r,
9240 movimentacao m, local l, romaneiopai rp
9241 where rp.idromaneio = p_romaneio
9242 and rr.idromaneio = rp.idromaneio
9243 and r.idremanejamento = rr.idremanejamento
9244 and r.status in ('A', 'P')
9245 and l.idlocal = r.idlocaldestino
9246 and m.idonda = rr.idromaneio
9247 and m.idlocalorigem = l.id
9248 and m.idlote = rr.idlote)
9249 loop
9250 begin
9251 select pd.loteuniconoendereco
9252 into v_loteuniconoendereco
9253 from produtodepositante pd, lote lt
9254 where lt.idlote = c_rem.idlote
9255 and pd.identidade = lt.iddepositante
9256 and pd.idproduto = lt.idproduto;
9257 exception
9258 when no_data_found then
9259 v_loteuniconoendereco := 0;
9260 end;
9261
9262 begin
9263 select *
9264 into r_sepespecifica
9265 from separacaoespecifica s
9266 where s.idlocal = c_rem.idlocaldestino
9267 and s.idnotafiscal = c_rem.idnotafiscal
9268 and s.idlote = c_rem.idlote
9269 and s.idarmazem = c_rem.idarmazem;
9270 exception
9271 when no_data_found then
9272 r_sepespecifica := null;
9273 end;
9274
9275 if r_sepespecifica.id is not null then
9276 begin
9277 insert into separacaoespecifica
9278 (idarmazem, idlocal, idlote, idnotafiscal, qtde, cadmanual,
9279 fonteseparacao, idusuario, dtvinculo)
9280 values
9281 (r_sepespecifica.idarmazem, c_rem.idlocalorigem,
9282 r_sepespecifica.idlote, r_sepespecifica.idnotafiscal,
9283 r_sepespecifica.qtde, r_sepespecifica.cadmanual,
9284 r_sepespecifica.fonteseparacao, r_sepespecifica.idusuario,
9285 sysdate)
9286 returning id into v_idNovaSepEspecifica;
9287 exception
9288 when dup_val_on_index then
9289 update separacaoespecifica
9290 set qtde = qtde + r_sepespecifica.qtde
9291 where idarmazem = r_sepespecifica.idarmazem
9292 and idlocal = c_rem.idlocalorigem
9293 and idlote = r_sepespecifica.idlote
9294 and idnotafiscal = r_sepespecifica.idnotafiscal
9295 returning id into v_idNovaSepEspecifica;
9296 end;
9297
9298 select count(1)
9299 into v_qtdeSepLoteInd
9300 from seploteindsepespecif sls
9301 where sls.idsepespecifica = r_sepespecifica.id;
9302
9303 for c_sep in (select sls.idseploteind, sls.qtde
9304 from seploteindsepespecif sls
9305 where sls.idsepespecifica = r_sepespecifica.id)
9306 loop
9307 if (v_qtdeSepLoteInd = 1) then
9308 insert into seploteindsepespecif
9309 (idsepespecifica, idseploteind, idlote, qtde)
9310 values
9311 (v_idNovaSepEspecifica, c_sep.idseploteind,
9312 r_sepespecifica.idlote, r_sepespecifica.qtde);
9313 else
9314 insert into seploteindsepespecif
9315 (idsepespecifica, idseploteind, idlote, qtde)
9316 values
9317 (v_idNovaSepEspecifica, c_sep.idseploteind,
9318 r_sepespecifica.idlote, c_sep.qtde);
9319 end if;
9320
9321 delete from seploteindsepespecif s
9322 where s.idsepespecifica = r_sepespecifica.id
9323 and s.idseploteind = c_sep.idseploteind;
9324 end loop;
9325
9326 delete from separacaoespecifica
9327 where id = r_sepespecifica.id;
9328
9329 end if;
9330
9331 end loop;
9332 end corrigeSepEspecificaOnda;
9333
9334 begin
9335 if p_tipo = 1
9336 and pk_onda.validarFluxo('%buffer%', p_romaneio)
9337 and pk_onda.validarFluxo('%colmeia%', p_romaneio) then
9338 return;
9339 end if;
9340
9341 -- No caso de cancelamento de onda, essa rotina avalia as
9342 -- separações especÃficas e volta do picking para o pulmão
9343 -- no caso da formação ter criado remanejamentos e alterado isso.
9344 corrigeSepEspecificaOnda;
9345
9346 delete from gtt_selecao;
9347
9348 if (p_tipo = 0) then
9349 insert into gtt_selecao
9350 select distinct lr.idremanejamento
9351 from paletseparacao ps, loteremanejamento lr, remanejamento r
9352 where r.cadmanual = 'N'
9353 and r.status <> 'F'
9354 and r.idremanejamento = lr.idremanejamento
9355 and lr.idlote = ps.idlote
9356 and ps.idromaneio = p_romaneio
9357 union
9358 select distinct rr.idremanejamento
9359 from remanejamentoromaneio rr, remanejamento r
9360 where rr.idremanejamento = r.idremanejamento
9361 and r.cadmanual = 'N'
9362 and r.status <> 'F'
9363 and rr.idromaneio = p_romaneio;
9364
9365 else
9366 insert into gtt_selecao
9367 select distinct lr.idremanejamento
9368 from movimentacao m, loteremanejamento lr, remanejamento r,
9369 local ld
9370 where m.idonda = p_romaneio
9371 and m.etapa = 1
9372 and m.status = 3
9373 and m.idnotafiscal = p_idnotafiscal
9374 and lr.idlote = m.idlote
9375 and r.idremanejamento = lr.idremanejamento
9376 and r.cadmanual = 'N'
9377 and r.status <> 'F'
9378 and ld.idarmazem = r.idarmazemdestino
9379 and ld.idlocal = r.idlocaldestino;
9380
9381 end if;
9382
9383 for c_remanej in (select idselecionado idremanejamento, r.planejado
9384 from gtt_selecao g, remanejamento r
9385 where r.idremanejamento = g.idselecionado
9386 order by tipo)
9387 loop
9388 begin
9389 update loteremanejamento
9390 set idromaneio = null
9391 where idromaneio = p_romaneio
9392 and idremanejamento = c_remanej.idremanejamento;
9393
9394 update remanejamento
9395 set idromaneio = null
9396 where idromaneio = p_romaneio
9397 and idremanejamento = c_remanej.idremanejamento;
9398
9399 delete remanejamentoromaneio
9400 where idromaneio = p_romaneio
9401 and idremanejamento = c_remanej.idremanejamento;
9402
9403 update remanejamento r
9404 set r.planejado = 'N'
9405 where r.idremanejamento = c_remanej.idremanejamento;
9406
9407 --Excluindo lotes do remanejamento onde não exista remanejamento em produtorecuperado e controle de avaria.
9408 delete from loteremanejamento
9409 where idremanejamento = c_remanej.idremanejamento
9410 and idremanejamento not in
9411 (select pr.idremanejamento
9412 from produtorecuperado pr
9413 where pr.idremanejamento = c_remanej.idremanejamento)
9414 and idremanejamento not in
9415 (select r.idremanejamento
9416 from controleavaria ca, remanejamento r
9417 where ca.idcontroleavaria = r.idcontroleavaria
9418 and r.idremanejamento = c_remanej.idremanejamento);
9419
9420 v_QtdeLotesExcluidos := sql%rowcount;
9421
9422 if (p_tipo = 0) then
9423 pk_utilities.GeraLog(p_usuario,
9424 substr('O REMANEJAMENTO ' ||
9425 c_remanej.idremanejamento ||
9426 ' FOI EXCLUIDO POIS ROMANEIO DE ID: ' ||
9427 p_romaneio || ' FOI DESFEITO.', 1, 1000),
9428 c_remanej.idremanejamento, 'DR');
9429 else
9430 pk_utilities.GeraLog(p_usuario,
9431 substr('O REMANEJAMENTO ' ||
9432 c_remanej.idremanejamento ||
9433 ' FOI EXCLUIDO POIS A ONDA DE ID: ' ||
9434 p_romaneio || ' FOI DESFEITA.', 1, 1000),
9435 c_remanej.idremanejamento, 'DR');
9436
9437 end if;
9438 exception
9439 when others then
9440 v_QtdeLotesExcluidos := 0;
9441
9442 verificarpendencia(c_remanej.idremanejamento, p_romaneio,
9443 p_usuario);
9444 end;
9445
9446 if (v_QtdeLotesExcluidos > 0) then
9447
9448 update trocarlocaldestinorem
9449 set idremanejamento = null
9450 where idremanejamento = c_remanej.idremanejamento;
9451
9452 delete remanejamentoromaneio
9453 where idremanejamento = c_remanej.idremanejamento;
9454
9455 delete from remanejamento
9456 where idremanejamento = c_remanej.idremanejamento;
9457 else
9458 update remanejamento r
9459 set r.planejado = c_remanej.planejado
9460 where r.idremanejamento = c_remanej.idremanejamento;
9461 end if;
9462 end loop;
9463
9464 delete from gtt_selecao;
9465
9466 end;
9467
9468 procedure desfazRemanejBuffPickParaPick
9469 (
9470 p_idonda in number,
9471 p_idusuario in number,
9472 p_idmovimentacoes in varchar2
9473 ) is
9474 v_sqlRem varchar2(4000);
9475 type cursorRem is ref cursor;
9476 c_cursorRem cursorRem;
9477
9478 type rec_cursorRem is record(
9479 idremanejamento number,
9480 status remanejamento.status%type,
9481 idarmazemorigem number,
9482 idlocalorigem remanejamento.idlocalorigem%type,
9483 idarmazemdestino number,
9484 idlocaldestino remanejamento.idlocaldestino%type);
9485 r_cursorRem rec_cursorRem;
9486
9487 function montarSqlRem return varchar2 is
9488 v_sqlRem varchar2(4000);
9489 begin
9490 v_sqlRem := 'select r.idremanejamento, r.status, ' ||
9491 ' r.idarmazemorigem, r.idlocalorigem, r.idarmazemdestino, r.idlocaldestino ' ||
9492 ' from remanejamento r, local lo, local ld ' ||
9493 ' where lo.idarmazem = r.idarmazemorigem ' ||
9494 ' and lo.idlocal = r.idlocalorigem ' ||
9495 ' and ld.idarmazem = r.idarmazemdestino ' ||
9496 ' and ld.idlocal = r.idlocaldestino ' ||
9497 ' and lo.tipo in (0) ' || ' and lo.buffer = ''S'' ' ||
9498 ' and ld.tipo = 0 ' || ' and ld.buffer = ''N'' ' ||
9499 ' and r.status <> ''F'' ' ||
9500 ' and r.cadmanual = ''N'' ' ||
9501 ' and r.idmovimentacao in (' || p_idmovimentacoes || ')';
9502
9503 return v_sqlRem;
9504 end;
9505
9506 procedure integrarRemEsteira(p_idremanejamento in number) is
9507 C_ESTEIRA_PICKING constant number := 1;
9508 C_NAO_INTEGRADO constant number := 0;
9509 C_REMANEJ_CAIXA_GRAFICA constant number := 1;
9510 C_CANCELAMENTO_ONDA constant number := 9;
9511 C_EVENTO_EXCLUSAO constant number := 1;
9512 begin
9513 for r_intEsteira in (select *
9514 from int_automacao_esteira i
9515 where i.esteira = C_ESTEIRA_PICKING
9516 and i.idoperacao = p_idremanejamento
9517 and i.tipooperacao = C_REMANEJ_CAIXA_GRAFICA
9518 and i.integrado = C_NAO_INTEGRADO)
9519 loop
9520 delete from int_automacao_esteira i
9521 where i.id = r_intEsteira.id;
9522
9523 insert into historico_automacao_esteira
9524 (id, data, evento, esteira, codintegracao, identificador,
9525 integrado, pesagemliberada, idoperacao, tipooperacao,
9526 usuariologado)
9527 values
9528 (seq_hist_automacao_esteira.nextval, sysdate, C_EVENTO_EXCLUSAO,
9529 r_intEsteira.Esteira, r_intEsteira.Codintegracao,
9530 r_intEsteira.Identificador, r_intEsteira.Integrado,
9531 r_intEsteira.Pesagemliberada, r_intEsteira.Idoperacao,
9532 C_CANCELAMENTO_ONDA, user);
9533 end loop;
9534 end;
9535
9536 begin
9537 --SITUAÇÕES POSSÃVEIS
9538 --1 ONDA FOI FORMADA, NÃO FORAM FORMADAS ONDAS POSTERIORES DEPENDENDO DESSES REMANEJAMENTOS
9539 --E ESTA ONDA ESTÃ SENDO DESFEITA
9540 --RESULTADO ESPERADO: OS REMANEJAMENTOS DE BUFFER DE PICKING PARA PICKING SERÃO DESFEITOS NORMALMENTE
9541 --2 ONDA FOI FORMADA, POSTERIORMENTE FOI FORMADA OUTRA ONDA DEPENDENDO DO EXCESSO DE DISPONIVEL NO PICKING
9542 --E A PRIMEIRA ONDA ESTÃ SENDO DESFEITA
9543 --RESULTADO ESPERADO: O GRUPO DE REMANEJAMENTOS (GERADO PELO MESMO IDMOVIMENTACAO) SOMENTE SERA DESFEITO
9544 --SE TODO O GRUPO PUDER SER DESFEITO, POSTERIORMENTE VINCULARÃ ESTES REMANEJAMENTOS A UM NOVO QUE SERÃ CRIADO
9545 --PARA SUBSTITUIR A MOVIMENTAÇÃO DE PULMÃO PARA BUFFER DE PICKING
9546 --3 ONDA FOI FORMADA CRIANDO MOVIMENTACAO DE PL PARA BUFFER DE PK, OUTRA ONDA FOI FORMADA UTILIZANDO O
9547 --EXCESSO DO DISPONIVEL DA PRIMEIRA ONDA, PRIMEIRA ONDA FOI DESFEITA, CRIANDO UM REMANEJAMENTO DE PL PARA
9548 --BUFFER DE PK PARA SUBSTITUIR A MOVIMENTACAO, UMA TERCEIRA ONDA FOI FORMADA UTILIZANDO O EXCESSO DE DISPONIVEL
9549 --NO PICKING, A SEGUNDA ONDA ESTÃ SENDO CANCELADA.
9550 --RESULTADO ESPERADO: DEVERà LOCALIZAR OS REMANEJAMENTOS DE PULMÃO PARA BUFFER DE PICKING, CADASTRADOS AUTOMATICAMENTE
9551 --E SEM REMANEJAMENTOROMANEIO, TENTANDO EXCLUIR TODOS OS REMANEJAMENTOS FILHOS, SOMENTE EXCLUIRÃ
9552 --SE PUDER EXCLUIR TODOS OS FILHOS E O PAI
9553 --4 APÓS O CENÃRIO 3, CASO A TERCEIRA ONDA SEJA CANCELADA
9554 --RESULTADO ESPERADO: DEVERÃ CONSEGUIR EXCLUIR TODOS OS REMANEJAMENTOS FILHOS E O PAI POSTERIORMENTE
9555
9556 --identificar remanejamentos pendentes sem REMANEJAMENTOROMANEIO, cadastrados automaticamente,
9557 --de pulmão para buffer de picking e tentar desfazer todos os filhos
9558 --se um dos filhos falhar, efetuar rollback e seguir para o próximo remanejamento pai
9559 for c_rem in (select r.idremanejamento
9560 from remanejamento r, local lo, local ld
9561 where lo.idarmazem = r.idarmazemorigem
9562 and lo.idlocal = r.idlocalorigem
9563 and ld.idarmazem = r.idarmazemdestino
9564 and ld.idlocal = r.idlocaldestino
9565 and lo.tipo in (1, 2)
9566 and lo.buffer = 'N'
9567 and ld.tipo = 0
9568 and ld.buffer = 'S'
9569 and r.status <> 'F'
9570 and r.cadmanual = 'N'
9571 and not exists
9572 (select 1
9573 from remanejamentoromaneio rr
9574 where rr.idremanejamento = r.idremanejamento)
9575 and not exists
9576 (select 1
9577 from loteremanejamento lr
9578 where lr.idremanejamento = r.idremanejamento
9579 and lr.conferido = 'S'))
9580 loop
9581 savepoint sv_remanejamento;
9582
9583 begin
9584 --alterando status dos remanejamentos filhos para permitir exclusão
9585 update remanejamento r
9586 set r.planejado = 'N'
9587 where r.idremanejamentopai = c_rem.idremanejamento;
9588
9589 --excluindo remanejamentoromaneio dos remanejamentos filhos
9590 delete from remanejamentoromaneio rr
9591 where exists
9592 (select 1
9593 from remanejamento r
9594 where r.idremanejamento = rr.idremanejamento
9595 and r.idremanejamentopai = c_rem.idremanejamento);
9596
9597 --excluindo loteremanejamento dos remanejamentos filhos
9598 delete from loteremanejamento lr
9599 where exists
9600 (select 1
9601 from remanejamento r
9602 where r.idremanejamento = lr.idremanejamento
9603 and r.idremanejamentopai = c_rem.idremanejamento);
9604
9605 --excluindo remanejamentos filhos
9606 delete from remanejamento r
9607 where r.idremanejamentopai = c_rem.idremanejamento;
9608
9609 --alterando status do remanejamento pai para permitir exclusão
9610 update remanejamento r
9611 set r.planejado = 'N'
9612 where r.idremanejamento = c_rem.idremanejamento;
9613
9614 --excluindo loteremanejamento do remanejamento pai
9615 delete from loteremanejamento lr
9616 where lr.idremanejamento = c_rem.idremanejamento;
9617
9618 --excluindo remanejamento pai
9619 delete from remanejamento r
9620 where r.idremanejamento = c_rem.idremanejamento;
9621
9622 --integrando remanejamento excluÃdo com a esteira
9623 for c_remFilho in (select r.idremanejamento
9624 from remanejamento r
9625 where r.idremanejamentopai =
9626 c_rem.idremanejamento)
9627 loop
9628 integrarRemEsteira(c_remFilho.idremanejamento);
9629 end loop;
9630 exception
9631 when others then
9632 rollback to sv_remanejamento;
9633
9634 pk_utilities.GeraLog(p_idusuario,
9635 'Erro ao tentar excluir o remanejamento id: ' ||
9636 c_rem.idremanejamento ||
9637 ' durante o cancelamento da onda id: ' ||
9638 p_idonda || '. Erro: ' || sqlerrm,
9639 c_rem.idremanejamento, 'RM');
9640 end;
9641 end loop;
9642
9643 --identificar remanejamentos pendentes de buffer de picking para picking com os IDMOVIMENTACAO passados
9644 --se um deles falhar, efetuar rollback de todos já desfeitos
9645 --se estiver com status G, executar
9646 --desfazer se o status estiver diferente de G
9647 if p_idmovimentacoes is not null then
9648 v_sqlRem := montarSqlRem;
9649
9650 if (c_cursorRem%isopen) then
9651 close c_cursorRem;
9652 end if;
9653
9654 open c_cursorRem for v_sqlRem;
9655
9656 fetch c_cursorRem
9657 into r_cursorRem;
9658
9659 savepoint sv_remanejamento;
9660
9661 begin
9662 while (c_cursorRem%found)
9663 loop
9664 -- Se o status é G, já realizou a origem e está na esteira, sendo assim, executa
9665 if r_cursorRem.status = 'G' then
9666 for c_loteRem in (select lr.idlote, lr.qtde
9667 from loteremanejamento lr
9668 where lr.idremanejamento =
9669 r_cursorRem.idremanejamento)
9670 loop
9671 pk_estoque.retirar_estoque(r_cursorRem.idarmazemorigem,
9672 r_cursorRem.idlocalorigem,
9673 c_loteRem.idlote, c_loteRem.qtde,
9674 p_idusuario,
9675 'RETIRADO ESTOQUE EM FUNÇÃO DA EXECUÇÃO AUTOMÃTICA E EXCLUSÃO DO REMANEJAMENTO: ' ||
9676 r_cursorRem.idremanejamento ||
9677 ' DEVIDO AO CANCELAMENTO DA ONDA ID: ' ||
9678 p_idonda);
9679
9680 pk_estoque.incluir_estoque(r_cursorRem.idarmazemdestino,
9681 r_cursorRem.idlocaldestino,
9682 c_loteRem.idlote, c_loteRem.qtde,
9683 p_idusuario,
9684 'INCLUIDO ESTOQUE REMANESCENTE DO BUFFER DE PICKING EM FUNÇÃO ' ||
9685 'DA EXECUÇÃO AUTOMÃTICA E EXCLUSÃO DO REMANEJAMENTO: ' ||
9686 r_cursorRem.idremanejamento ||
9687 ' PDEVIDO AO CANCELAMENTO DA ONDA ID: ' ||
9688 p_idonda);
9689 end loop;
9690 end if;
9691
9692 --excluindo a remanejamentoromaneio dos remanejamentos
9693 delete from remanejamentoromaneio rr
9694 where rr.idremanejamento = r_cursorRem.idremanejamento;
9695
9696 --excluindo loteremanejamento do remanejamento
9697 delete from loteremanejamento lr
9698 where lr.idremanejamento = r_cursorRem.idremanejamento;
9699
9700 --excluindo remanejamento
9701 delete from remanejamento r
9702 where r.idremanejamento = r_cursorRem.idremanejamento;
9703
9704 --integrando remanejamento excluÃdo com a esteira
9705 integrarRemEsteira(r_cursorRem.idremanejamento);
9706
9707 fetch c_cursorRem
9708 into r_cursorRem;
9709 end loop;
9710 exception
9711 when others then
9712 rollback to sv_remanejamento;
9713
9714 pk_utilities.GeraLog(p_idusuario,
9715 'Ocorreu um erro durante o cancelamento da onda id: ' ||
9716 p_idonda ||
9717 ' ao tentar excluir os remanejamentos criados pelas movimentações id (' ||
9718 p_idmovimentacoes || '). Erro: ' || sqlerrm,
9719 p_idonda, 'RM');
9720 end;
9721
9722 close c_cursorRem;
9723 end if;
9724 end;
9725
9726 procedure validaDispExecucao(p_idremanejamento in number) is
9727 v_idArmazem number;
9728 v_idLocal local.idlocal%type;
9729
9730 v_cubagemRemanej number;
9731 v_pesoRemanej number;
9732 v_qtde number;
9733 v_msg t_message;
9734
9735 function isDestinoBufferPicking
9736 (
9737 p_idArmazem in number,
9738 p_idLocal in varchar2
9739 ) return boolean is
9740 v_resultado number;
9741 begin
9742 select count(*)
9743 into v_resultado
9744 from dual
9745 where exists (select 1
9746 from local l
9747 where l.idarmazem = p_idArmazem
9748 and l.idlocal = p_idLocal
9749 and l.tipo = 0
9750 and l.buffer = 'S');
9751 return v_resultado > 0;
9752 end;
9753
9754 begin
9755 -- Rotina criada para validar se o remanejamento pode ser realizado
9756 -- Caso o local de destino não possua disponibilidade de peso ou cubagem
9757 -- para receber os materiais e a configuração de armazem exibeAtvRemSemDisp
9758 -- estiver marcada ocorrerá erro
9759
9760 select r.idarmazemdestino, r.idlocaldestino,
9761 sum((lr.qtde / e.fatorconversao) * e.altura * e.largura *
9762 e.comprimento) cubagemrem,
9763 sum((lr.qtde / e.fatorconversao) * e.pesobruto) pesorem
9764 into v_idArmazem, v_idLocal, v_cubagemRemanej, v_pesoRemanej
9765 from remanejamento r, loteremanejamento lr, lote lt, embalagem e
9766 where r.idremanejamento = p_idremanejamento
9767 and lr.idremanejamento = r.idremanejamento
9768 and lt.idlote = lr.idlote
9769 and e.barra = lt.barra
9770 and e.idproduto = lt.idproduto
9771 group by r.idarmazemdestino, r.idlocaldestino, r.idremanejamento;
9772
9773 if isDestinoBufferPicking(v_idArmazem, v_idLocal) then
9774 return;
9775 end if;
9776
9777 select count(1)
9778 into v_qtde
9779 from dual
9780 where exists
9781 (select 1
9782 from local ld,
9783 (select ll.idendereco,
9784 sum((ll.estoque / e.fatorconversao) * e.altura *
9785 e.largura * e.comprimento) cubagemutilizada,
9786 sum((ll.estoque / e.fatorconversao) * e.pesobruto) pesoutilizado
9787 from lotelocal ll, lote lt, embalagem e
9788 where lt.idlote = ll.idlote
9789 and e.barra = lt.barra
9790 and e.idproduto = lt.idproduto
9791 and ll.idarmazem = v_idArmazem
9792 and ll.idlocal = v_idlocal
9793 group by ll.idendereco) est, armazem ar
9794 where ld.idarmazem = v_idArmazem
9795 and ld.idlocal = v_idlocal
9796 and est.idendereco(+) = ld.id
9797 and ar.idarmazem = ld.idarmazem
9798 and (ar.exibeAtvRemSemDisp = 1 or
9799 (((ld.altura - ld.alturamanobra) * ld.largura *
9800 ld.comprimento) - nvl(est.cubagemutilizada, 0) -
9801 v_cubagemRemanej >= 0 and
9802 ld.pesomaximo - nvl(est.pesoutilizado, 0) -
9803 v_pesoRemanej >= 0)));
9804
9805 if v_qtde = 0 then
9806 v_msg := t_message('O remanejamento não pode ser realizado, ' ||
9807 'pois local de destino não possui cubagem ' ||
9808 'ou peso disponÃvel no momento.');
9809 raise_application_error(-20000, v_msg.formatMessage);
9810 end if;
9811 end;
9812
9813 procedure excluirRemanejamento
9814 (
9815 p_idremanejamento in number,
9816 p_usuario in number
9817 ) is
9818 r_remanejamento remanejamento%rowtype;
9819
9820 v_tipolocalorigem number;
9821 v_tipolocaldestino number;
9822 v_msg t_message;
9823 begin
9824
9825 r_remanejamento := pk_remanejamento.CarregarRemanejamento(p_idremanejamento);
9826
9827 select lo.tipo, ld.tipo
9828 into v_tipolocalorigem, v_tipolocaldestino
9829 from remanejamento r, local lo, local ld
9830 where r.idremanejamento = p_idremanejamento
9831 and lo.idlocal = r.idlocalorigem
9832 and lo.idarmazem = r.idarmazemorigem
9833 and ld.idlocal = r.idlocaldestino
9834 and ld.idarmazem = r.idarmazemdestino;
9835
9836 if ((v_tipolocalorigem = 0) and
9837 (v_tipolocaldestino = 1 or v_tipolocaldestino = 2)) then
9838 if (r_remanejamento.status = 'F') then
9839 v_msg := t_message('Não é permitido realizar alterações em remanejamento que está finalizado.');
9840 raise_application_error(-20000, v_msg.formatMessage);
9841 end if;
9842 else
9843
9844 if (r_remanejamento.status <> 'A') then
9845 v_msg := t_message('Não é permitido realizar alterações em remanejamento que não esteja aberto.');
9846 raise_application_error(-20000, v_msg.formatMessage);
9847 end if;
9848
9849 if (r_remanejamento.planejado = 'S') then
9850 v_msg := t_message('Não é permitido realizar alterações em remanejamento planejado.');
9851 raise_application_error(-20000, v_msg.formatMessage);
9852 end if;
9853 end if;
9854
9855 if ((v_tipolocalorigem = 0) and
9856 (v_tipolocaldestino = 1 or v_tipolocaldestino = 2)) then
9857
9858 for c_loterem in (select idlote
9859 from loteremanejamento
9860 where idremanejamento = p_idremanejamento)
9861 loop
9862 desassociarLote(p_idremanejamento, c_loterem.idlote, p_usuario);
9863 end loop;
9864 end if;
9865
9866 delete from remanejamento
9867 where idremanejamento = p_idremanejamento;
9868
9869 pk_utilities.GeraLog(p_usuario,
9870 'Tela: Remanejamento - Apagou o Remanejamento Id: ' ||
9871 p_idremanejamento, p_idremanejamento, 'CA');
9872 end;
9873
9874 function getDadosRegraDepositanteLocal
9875 (
9876 p_idLocal in varchar2,
9877 p_idArmazem in number,
9878 p_barraInformada in varchar2
9879 ) return number is
9880
9881 cursor c_depositante is
9882 select distinct lt.iddepositante
9883 from lotelocal ll, lote lt
9884 where ll.estoque > 0
9885 and ll.idarmazem = p_idArmazem
9886 and ll.idlocal = p_idLocal
9887 and lt.idlote = ll.idlote;
9888
9889 v_qtdDepositantes number;
9890 v_retorno number;
9891 v_idproduto number;
9892 v_codProduto produto.codigointerno%type;
9893 v_barraProduto embalagem.barra%type;
9894 v_qtd number;
9895 v_loteIndustria lote.descr%type;
9896 v_dtVencimento lote.dtvenc%type;
9897 begin
9898 v_qtdDepositantes := 0;
9899
9900 delete from GTT_BARRA_REGRADEPOSITANTE;
9901
9902 for r_dep in c_depositante
9903 loop
9904 pk_depositante.encontrarRegraBarraDepositante(r_dep.iddepositante,
9905 p_barraInformada,
9906 v_qtdDepositantes);
9907 end loop;
9908
9909 if (v_qtdDepositantes = 0) then
9910 for r_dep in c_depositante
9911 loop
9912 v_retorno := pk_depositante.getDadosBarraRegraDepositante(r_dep.iddepositante,
9913 p_barraInformada,
9914 v_codProduto,
9915 v_barraProduto,
9916 v_qtd,
9917 v_loteIndustria,
9918 v_dtVencimento);
9919
9920 if (v_retorno = 1) then
9921 if (v_codProduto is null and v_loteIndustria is not null) then
9922 begin
9923 select p.idproduto, p.codigointerno, l.barra
9924 into v_idproduto, v_codProduto, v_barraProduto
9925 from lote l, produto p
9926 where l.descr = v_loteIndustria
9927 and l.iddepositante = r_dep.iddepositante
9928 and l.idproduto = p.idproduto
9929 and rownum = 1;
9930 exception
9931 when no_data_found then
9932 v_qtdDepositantes := 0;
9933 continue;
9934 end;
9935
9936 v_qtdDepositantes := 1;
9937
9938 insert into gtt_barra_regradepositante
9939 (iddepositante, depositante, barrainformada, idproduto,
9940 codproduto, descrproduto, barraproduto, qtd, loteindustria,
9941 dtvencimento)
9942 values
9943 (r_dep.iddepositante, null, p_barrainformada, v_idproduto,
9944 v_codproduto, null, v_barraproduto, v_qtd, v_loteindustria,
9945 v_dtvencimento);
9946
9947 return v_qtdDepositantes;
9948 end if;
9949 end if;
9950 end loop;
9951 end if;
9952
9953 return v_qtdDepositantes;
9954 end getDadosRegraDepositanteLocal;
9955
9956 function getDadosRegraDepositanteLote
9957 (
9958 p_idremanejamento in number,
9959 p_barraInformada in varchar2
9960 ) return number is
9961
9962 v_qtdDepositantes number;
9963 begin
9964 v_qtdDepositantes := 0;
9965
9966 delete from GTT_BARRA_REGRADEPOSITANTE;
9967
9968 for r_dep in (select distinct lt.iddepositante
9969 from loteremanejamento lr, lote lt
9970 where lt.idlote = lr.idlote
9971 and lr.idremanejamento = p_idremanejamento)
9972 loop
9973 pk_depositante.encontrarRegraBarraDepositante(r_dep.iddepositante,
9974 p_barraInformada,
9975 v_qtdDepositantes);
9976 end loop;
9977
9978 return v_qtdDepositantes;
9979 end;
9980
9981 procedure validarFinalizarRemanejamento(p_idRemanejamento number) is
9982 v_idlocalorigem local.idlocal%type;
9983 v_idlocalorigemFormat local.idlocalformatado%type;
9984 v_idarmazemorigem number;
9985 v_pk_origem number;
9986 v_pk_destino number;
9987 v_remanejamentos varchar2(4000);
9988 v_msg t_message;
9989 begin
9990 select r.idlocalorigem, lo.idlocalformatado, r.idarmazemorigem,
9991 decode(lo.picking, 'S', 1, 0) pk_origem,
9992 decode(ld.picking, 'S', 1, 0) pk_destino
9993 into v_idlocalorigem, v_idlocalorigemFormat, v_idarmazemorigem,
9994 v_pk_origem, v_pk_destino
9995 from remanejamento r, local lo, local ld
9996 where 1 = 1
9997 and lo.idlocal = r.idlocalorigem
9998 and lo.idarmazem = r.idarmazemorigem
9999 and ld.idlocal = r.idlocaldestino
10000 and ld.idarmazem = r.idarmazemdestino
10001 and r.idremanejamento = p_idRemanejamento;
10002
10003 if (v_pk_origem = 1 and v_pk_destino = 0) then
10004 select stragg(idrem)
10005 into v_remanejamentos
10006 from (select distinct r.idremanejamento idrem
10007 from remanejamento r, loteremanejamento lr, local lo,
10008 local ld
10009 where 1 = 1
10010 and r.idremanejamento = lr.idremanejamento
10011 and r.idlocaldestino = v_idlocalorigem
10012 and r.idarmazemdestino = v_idarmazemorigem
10013 and lo.idlocal = r.idlocalorigem
10014 and lo.idarmazem = r.idarmazemorigem
10015 and ld.idlocal = r.idlocaldestino
10016 and ld.idarmazem = r.idarmazemdestino
10017 and lo.buffer = 'N'
10018 and ld.buffer = 'N'
10019 and r.status <> 'F'
10020 and lr.idlote in
10021 (select distinct cl.idloteanterior
10022 from composicaolote cl, loteremanejamento lr
10023 where 1 = 1
10024 and lr.idremanejamento = p_idRemanejamento
10025 and cl.idlotenovo = lr.idlote));
10026
10027 if (v_remanejamentos is not null) then
10028 v_msg := t_message('EXISTEM REMANEJAMENTOS PENDENTES CUJO ENDEREÇO DE DESTINO É ' ||
10029 '{0}.' || chr(13) || chr(13) ||
10030 'É NECESSÃRIO EXECUTAR OS SEGUINTES REMANEJAMENTOS(' ||
10031 '{1}).' || chr(13) || chr(13) ||
10032 'OPERAÇÃO CANCELADA.');
10033 v_msg.addParam(v_idlocalorigemFormat);
10034 v_msg.addParam(v_remanejamentos);
10035 raise_application_error(-20000, v_msg.formatMessage);
10036 end if;
10037 end if;
10038 end validarFinalizarRemanejamento;
10039
10040 procedure integrarEsteira
10041 (
10042 p_idremanejamento number,
10043 p_idusuario number
10044 ) is
10045 v_fatorConversao number;
10046 v_caixaFechada char(1);
10047 v_barraEmbalagem varchar2(32);
10048 v_resto number;
10049 v_qtdeCaixa number;
10050 v_idlocalOrigem local.idlocal%type;
10051 v_estoque number;
10052 v_idsOnda varchar2(512);
10053 v_msg t_message;
10054
10055 ESTEIRA_PICKING constant number := 1;
10056
10057 procedure validarIntegracaoEsteira is
10058
10059 v_countValidacao number;
10060 v_idLocalOrigemRem local.idlocal%type;
10061 v_idLocalDestinoRem local.idlocal%type;
10062 v_idSetorOrigemRem setor.idsetor%type;
10063 v_idSetorDestinoRem setor.idsetor%type;
10064
10065 C_BUFFER_ESTEIRA_ATIVADO constant number := 1;
10066 C_BUFFER_ESTEIRA_DESATIVADO constant number := 0;
10067
10068 v_msg t_message;
10069 begin
10070 select lo.idlocal, lo.idsetor, ld.idlocal, ld.idsetor
10071 into v_idLocalOrigemRem, v_idSetorOrigemRem, v_idLocalDestinoRem,
10072 v_idSetorDestinoRem
10073 from remanejamento r, local lo, local ld
10074 where r.idremanejamento = p_idremanejamento
10075 and lo.idlocal = r.idlocalorigem
10076 and ld.idlocal = r.idlocaldestino;
10077
10078 if v_idSetorOrigemRem is null then
10079 v_msg := t_message('Não foi possÃvel criar a integração com esteira do Remanejamento ID: {0} ' ||
10080 'pois o setor Local Origem (ID:{1}) não possui setor configurado. Favor, ' ||
10081 'regularizar o cadastro para continuar com o processo');
10082 v_msg.addParam(p_idremanejamento);
10083 v_msg.addParam(v_idLocalOrigemRem);
10084 raise_application_error(-20000, v_msg.formatMessage);
10085 end if;
10086
10087 if v_idSetorDestinoRem is null then
10088 v_msg := t_message('Não foi possÃvel criar a integração com esteira do Remanejamento ID: {0} pois ' ||
10089 'o setor Local Destino (ID:{1}) não possui setor configurado. Favor, regularizar o ' ||
10090 'cadastro para continuar com o processo');
10091 v_msg.addParam(p_idremanejamento);
10092 v_msg.addParam(v_idLocalDestinoRem);
10093 raise_application_error(-20000, v_msg.formatMessage);
10094
10095 end if;
10096
10097 select count(1)
10098 into v_countValidacao
10099 from remanejamento r, local lo, local ld, setor sd
10100 where r.idremanejamento = p_idremanejamento
10101 and lo.idlocal = r.idlocalorigem
10102 and lo.idarmazem = r.idarmazemorigem
10103 and lo.buffer = 'S'
10104 and lo.bufferesteira = C_BUFFER_ESTEIRA_ATIVADO
10105 and ld.idlocal = r.idlocaldestino
10106 and ld.idarmazem = r.idarmazemdestino
10107 and ld.buffer = 'N'
10108 and ld.bufferesteira = C_BUFFER_ESTEIRA_DESATIVADO
10109 and sd.idsetor = ld.idsetor
10110 and sd.codintegracaoesteira is not null;
10111
10112 if v_countValidacao = 0 then
10113 v_msg := t_message('Não foi possÃvel criar a integração com esteira do Remanejamento ID: {0}. ' ||
10114 'Só é possÃvel criar esta integração para remanejamentos com local origem do tipo Buffer ' ||
10115 'com parâmetro Buffer Esteira ativado e destino com o parâmetro Código ' ||
10116 'de Integração com Esteira configurado no Setor (IDLOCALORIGEM: {1} e IDSETORDESTINO: {2}).');
10117 v_msg.addParam(p_idremanejamento);
10118 v_msg.addParam(v_idLocalOrigemRem);
10119 v_msg.addParam(v_idSetorDestinoRem);
10120 raise_application_error(-20000, v_msg.formatMessage);
10121
10122 end if;
10123 end validarIntegracaoEsteira;
10124 begin
10125
10126 validarIntegracaoEsteira;
10127
10128 for intEsteira in (select sd.codintegracaoesteira codintegracaoesteiraDestino,
10129 lr.qtde, lr.idlote, lt.idproduto, lt.barra,
10130 r.idusuarioinicio, r.horafimorigem, r.status,
10131 r.idlocalorigem
10132 from setor sd, local ld, remanejamento r, local lo,
10133 setor so, loteremanejamento lr, lote lt
10134 where sd.usoexclusivocxmov = 0
10135 and sd.codintegracaoesteira is not null
10136 and sd.ativo = 'S'
10137 and ld.idsetor = sd.idsetor
10138 and ld.ativo = 'S'
10139 and ld.idregiao is not null
10140 and r.idlocaldestino = ld.idlocal
10141 and r.idarmazemdestino = ld.idarmazem
10142 and r.idremanejamento = p_idremanejamento
10143 and lo.idlocal = r.idlocalorigem
10144 and lo.idarmazem = r.idarmazemorigem
10145 and lo.ativo = 'S'
10146 and lo.idregiao is not null
10147 and so.idsetor = lo.idsetor
10148 and so.ativo = 'S'
10149 and lr.idremanejamento = r.idremanejamento
10150 and lt.idlote = lr.idlote)
10151 loop
10152
10153 if (intEsteira.idusuarioinicio is null and
10154 intEsteira.horafimorigem is null and intEsteira.status <> 'G') then
10155 v_msg := t_message('Não é possÃvel gerar Integração para o setor de Picking com Esteira, pois o remanejamento de Origem não foi finalizado. OPERAÇÃO CANCELADA.');
10156 raise_application_error(-20000, v_msg.formatMessage);
10157 end if;
10158
10159 begin
10160 select e.barra, e.fatorconversao, e.caixafechada, ll.idlocal
10161 into v_barraEmbalagem, v_fatorConversao, v_caixaFechada,
10162 v_idlocalOrigem
10163 from embalagem e, lote l, lotelocal ll
10164 where e.idproduto = intEsteira.Idproduto
10165 and trunc(intEsteira.qtde / e.fatorconversao) > 0
10166 and e.ativo = 'S'
10167 and l.idlote = intEsteira.Idlote
10168 and l.idproduto = e.idproduto
10169 and l.barra = e.barra
10170 and ll.idlote = l.idlote
10171 and ll.idlocal = intEsteira.Idlocalorigem
10172 and ll.estoque > 0
10173 order by e.fatorconversao desc;
10174 exception
10175 when no_data_found then
10176 select nvl(sum(ll.estoque), 0)
10177 into v_estoque
10178 from embalagem e, lote l, lotelocal ll
10179 where e.idproduto = intEsteira.Idproduto
10180 and trunc(intEsteira.qtde / e.fatorconversao) > 0
10181 and e.ativo = 'S'
10182 and l.idproduto = e.idproduto
10183 and l.barra = e.barra
10184 and ll.idlote = l.idlote
10185 and ll.idlocal = intEsteira.Idlocalorigem;
10186
10187 select stragg(rr.idromaneio)
10188 INTO v_idsOnda
10189 from remanejamentoromaneio rr
10190 where rr.idremanejamento = p_idremanejamento
10191 group by rr.idremanejamento;
10192
10193 if (v_estoque = 0) then
10194 v_msg := t_message('Não foi encontrado estoque no local origem: ' ||
10195 '{0}, verifique o processo anterior de separação da(s) onda(s) id(s): ' ||
10196 '{1}. OPERAÇÃO CANCELADA.');
10197 v_msg.addParam(intEsteira.Idlocalorigem);
10198 v_msg.addParam(v_idsOnda);
10199 raise_application_error(-20000, v_msg.formatMessage);
10200 else
10201 v_msg := t_message('Não foi encontrado embalagem cadastrada para o produto de id: ' ||
10202 '{0} ou a embalagem de barra: ' ||
10203 '{1} não está ativa. OPERAÇÃO CANCELADA.');
10204 v_msg.addParam(intEsteira.Idproduto);
10205 v_msg.addParam(v_barraEmbalagem);
10206 raise_application_error(-20000, v_msg.formatMessage);
10207 end if;
10208 end;
10209
10210 if ((v_fatorConversao <> 1) or
10211 (v_fatorConversao = 1 and v_caixaFechada = 'S')) then
10212
10213 v_barraEmbalagem := intEsteira.Barra;
10214
10215 elsif (v_fatorConversao = 1 and v_caixaFechada = 'N') then
10216 v_msg := t_message('Não é permitido realizar integração com esteira de embalagens que não sejam ' ||
10217 'CAIXA FECHADA ou fator de conversão igual a 1.OPERAÇÃO CANCELADA.');
10218 raise_application_error(-20000, v_msg.formatMessage);
10219 end if;
10220
10221 v_resto := mod(intEsteira.Qtde, v_fatorConversao);
10222
10223 if (v_resto = 0) then
10224 v_qtdeCaixa := (intEsteira.Qtde / v_fatorConversao);
10225 else
10226 v_msg := t_message('Não foi possÃvel realizar a integração com a esteira porque a quantidade remanejada do produto de id: ' ||
10227 '{0} deve ser em caixa. OPERAÇÃO CANCELADA.');
10228 v_msg.addParam(intEsteira.Idproduto);
10229 raise_application_error(-20000, v_msg.formatMessage);
10230 end if;
10231
10232 for intAutomacao in 1 .. v_qtdeCaixa
10233 loop
10234
10235 insert into int_automacao_esteira
10236 (id, esteira, codintegracao, identificador, integrado,
10237 pesagemliberada, idoperacao, tipooperacao)
10238 values
10239 (seq_int_automacao_esteira.nextval, ESTEIRA_PICKING,
10240 intEsteira.codintegracaoesteiraDestino, v_barraEmbalagem, 0, 0,
10241 p_idremanejamento, 1);
10242
10243 end loop;
10244
10245 end loop;
10246
10247 end integrarEsteira;
10248
10249 procedure concluirOrigem
10250 (
10251 p_idRemanejamento in number,
10252 p_idUsuario in number,
10253 p_TipoMenuRemanejamento in number
10254 ) is
10255 localOrigem t_localRemanejamento;
10256 localDestino t_localRemanejamento;
10257
10258 r_remanejamento remanejamento%rowtype;
10259
10260 v_existeUsuario number;
10261 v_nomeUsuario usuario.nomeusuario%type;
10262 v_msg t_message;
10263
10264 procedure validarRemanejamento is
10265 begin
10266 if (r_remanejamento.status <> 'A') then
10267 v_msg := t_message('Este remanejamento ja foi finalizado, portanto nao pode ser executado. Operacao Cancelada');
10268 raise_application_error(-20000, v_msg.formatMessage);
10269 end if;
10270
10271 if (r_remanejamento.planejado <> 'S') then
10272 v_msg := t_message('Este remanejamento nao foi planejado, portanto nao pode ser executado. Operacao Cancelada');
10273 raise_application_error(-20000, v_msg.formatMessage);
10274 end if;
10275
10276 if (r_remanejamento.horafimorigem is not null and
10277 r_remanejamento.idusuarioinicio is not null) then
10278 select nomeusuario
10279 into v_nomeUsuario
10280 from usuario
10281 where idusuario = r_remanejamento.idusuarioinicio;
10282 v_msg := t_message('O remanejamento id {0}' ||
10283 ' já teve sua origem finalizada pelo usuario {1}.');
10284 v_msg.addParam(p_idRemanejamento);
10285 v_msg.addParam(v_nomeUsuario);
10286 raise_application_error(-20000, v_msg.formatMessage);
10287 end if;
10288 end validarRemanejamento;
10289
10290 begin
10291 begin
10292 select r.*
10293 into r_remanejamento
10294 from remanejamento r
10295 where r.idremanejamento = p_idRemanejamento;
10296 exception
10297 when no_data_found then
10298 v_msg := t_message('Não foi encontrado remanejamento com id informado. IdRemanejamento {0}');
10299 v_msg.addParam(p_idRemanejamento);
10300 raise_application_error(-20000, v_msg.formatMessage);
10301 end;
10302
10303 select count(1)
10304 into v_existeUsuario
10305 from usuario u
10306 where u.idusuario = p_idUsuario;
10307
10308 if (v_existeUsuario = 0) then
10309 v_msg := t_message('Não foi encontrado usuário com id informado. IdUsuario {0}');
10310 v_msg.addParam(p_idUsuario);
10311 raise_application_error(-20000, v_msg.formatMessage);
10312 end if;
10313
10314 validarRemanejamento;
10315 validarBufPickEsteira(p_idRemanejamento, p_TipoMenuRemanejamento);
10316
10317 update remanejamento
10318 set status = 'G',
10319 idusuarioinicio = p_idUsuario,
10320 horafimorigem = sysdate
10321 where idremanejamento = p_idRemanejamento;
10322
10323 select l.idlocal, l.tipo, l.idarmazem, l.picking, l.buffer,
10324 s.codintegracaoesteira
10325 into localOrigem.idLocal, localOrigem.tipoLocal, localOrigem.idarmazem,
10326 localOrigem.picking, localOrigem.buffer,
10327 localOrigem.codIntegracaoEsteira
10328 from local l, setor s
10329 where l.idlocal = r_remanejamento.idlocalorigem
10330 and l.idarmazem = r_remanejamento.idarmazemorigem
10331 and s.idsetor(+) = l.idsetor;
10332
10333 select l.idlocal, l.tipo, l.idarmazem, l.picking, l.buffer,
10334 s.codintegracaoesteira
10335 into localDestino.idLocal, localDestino.tipoLocal,
10336 localDestino.idarmazem, localDestino.picking, localDestino.buffer,
10337 localDestino.codIntegracaoEsteira
10338 from local l, setor s
10339 where l.idlocal = r_remanejamento.idlocaldestino
10340 and l.idarmazem = r_remanejamento.idarmazemdestino
10341 and s.idsetor(+) = l.idsetor;
10342
10343 if (localOrigem.tipoLocal in (1, 2) and localOrigem.buffer = 'N') then
10344 if (localDestino.tipoLocal = 0)
10345 and (localDestino.picking = 'S')
10346 and (localDestino.buffer = 'N')
10347 and ((localDestino.codIntegracaoEsteira is not null) and
10348 (length(localDestino.codIntegracaoEsteira) > 0)) then
10349
10350 integrarEsteira(p_idRemanejamento, p_idUsuario);
10351 end if;
10352 end if;
10353
10354 pk_utilities.GeraLog(p_idUsuario,
10355 'REABASTECIMENTO ORIGEM, REABASTECIMENTO ' ||
10356 p_idRemanejamento || ', LOCAL ORIGEM ' ||
10357 localOrigem.tipoLocal ||
10358 ' REALIZADO PELO USUARIO ' || p_idUsuario,
10359 p_idRemanejamento, 'RO');
10360
10361 end concluirOrigem;
10362
10363 procedure ExecOrigemRemBufferPicking
10364 (
10365 p_idOnda in number,
10366 p_idLocalBuffer in local.idlocal%type,
10367 p_idProduto in number,
10368 p_idUsuario in number
10369 ) is
10370
10371 localOrigem t_localRemanejamento;
10372 localDestino t_localRemanejamento;
10373
10374 begin
10375 for r_Remanejamentos in (select distinct r.idremanejamento,
10376 r.idlocalorigem,
10377 r.idlocaldestino,
10378 r.idarmazemorigem,
10379 r.idarmazemdestino
10380 from remanejamento r, remanejamentoromaneio rr,
10381 lote lt, gtt_selecao g
10382 where rr.idromaneio = p_idonda
10383 and r.idremanejamento = rr.idremanejamento
10384 and r.status = 'A'
10385 and r.horafimorigem is null
10386 and r.idusuarioinicio is null
10387 and r.idlocalorigem = p_idLocalBuffer
10388 and lt.idlote = rr.idlote
10389 and lt.idproduto = p_idproduto
10390 and r.idmovimentacao = g.idselecionado)
10391 loop
10392
10393 select l.idlocal, l.tipo, l.idarmazem, l.picking, l.buffer,
10394 s.codintegracaoesteira, l.bufferesteira
10395 into localOrigem.idLocal, localOrigem.tipoLocal,
10396 localOrigem.idarmazem, localOrigem.picking, localOrigem.buffer,
10397 localOrigem.codIntegracaoEsteira, localOrigem.bufferesteira
10398 from local l, setor s
10399 where l.idlocal = r_Remanejamentos.idlocalorigem
10400 and l.idarmazem = r_Remanejamentos.idarmazemorigem
10401 and s.idsetor(+) = l.idsetor;
10402
10403 select l.idlocal, l.tipo, l.idarmazem, l.picking, l.buffer,
10404 s.codintegracaoesteira
10405 into localDestino.idLocal, localDestino.tipoLocal,
10406 localDestino.idarmazem, localDestino.picking,
10407 localDestino.buffer, localDestino.codIntegracaoEsteira
10408 from local l, setor s
10409 where l.idlocal = r_Remanejamentos.idlocaldestino
10410 and l.idarmazem = r_Remanejamentos.idarmazemdestino
10411 and s.idsetor(+) = l.idsetor;
10412
10413 if ((localOrigem.tipoLocal = 0) and (localOrigem.picking = 'S') and
10414 (localOrigem.buffer = 'S') and (localOrigem.bufferEsteira = 1)) then
10415
10416 if ((localDestino.tipoLocal = 0) and (localDestino.picking = 'S') and
10417 (localDestino.buffer = 'N') and
10418 (localDestino.codIntegracaoEsteira is not null and
10419 length(localDestino.codIntegracaoEsteira) > 0)) then
10420 update loteremanejamento lr
10421 set lr.conferido = 'S'
10422 where lr.idremanejamento = r_Remanejamentos.Idremanejamento
10423 and lr.conferido = 'N';
10424
10425 update remanejamento rem
10426 set rem.status = 'G',
10427 rem.idusuarioinicio = p_idusuario,
10428 rem.horafimorigem = sysdate
10429 where rem.Idremanejamento = r_Remanejamentos.Idremanejamento;
10430
10431 integrarEsteira(r_Remanejamentos.Idremanejamento, p_idusuario);
10432 end if;
10433 end if;
10434 end loop;
10435 end ExecOrigemRemBufferPicking;
10436
10437 procedure conferirLoteIndustria
10438 (
10439 p_idremanejamento in remanejamento.idremanejamento%type,
10440 p_loteIndustria in lote.descr%type
10441 ) is
10442
10443 v_loteIndustria lote.descr%type;
10444
10445 begin
10446 v_loteIndustria := trim(p_loteIndustria);
10447
10448 if (v_loteIndustria is null) then
10449 return;
10450 end if;
10451
10452 update loteremanejamento lr
10453 set lr.conferido = 'S',
10454 lr.qtdeconferida = lr.qtde
10455 where lr.idremanejamento = p_idremanejamento
10456 and exists (select 1
10457 from loteremanejamento lra, lote lt
10458 where lra.idremanejamento = p_idremanejamento
10459 and lra.idlote = lt.idlote
10460 and lra.idremanejamento = lr.idremanejamento
10461 and lra.idlote = lr.idlote
10462 and lra.conferido = 'N'
10463 and upper(nvl(lt.descr, 'NAOINFORMADO')) =
10464 upper(v_loteIndustria));
10465
10466 end conferirLoteIndustria;
10467
10468 function retorna_qtde_caixa
10469 (
10470 p_idremanejamento in number,
10471 p_idproduto in number default null,
10472 p_loteIndustria in varchar2 default null,
10473 p_conferido in varchar2 default null
10474 ) return varchar2 is
10475 cursor c_cx
10476 (
10477 p_idremanejamento in number,
10478 p_idproduto in number default null,
10479 p_loteIndustria in varchar2 default null,
10480 p_conferido in varchar2 default null
10481 ) is
10482 select sum(lr.qtde), e.fatorconversao
10483 from loteremanejamento lr, lote l, embalagem e
10484 where lr.idremanejamento = p_idremanejamento
10485 and lr.conferido = nvl(p_conferido, lr.conferido)
10486 and l.idlote = lr.idlote
10487 and ((p_loteIndustria is null) or
10488 (l.descr is null and p_loteIndustria = '-1') or
10489 (l.descr = p_loteIndustria))
10490 and l.idproduto = nvl(p_idproduto, l.idproduto)
10491 and e.barra = pk_produto.retornarcodbarramaiorfator(l.idproduto)
10492 and e.idproduto = l.idproduto
10493 group by l.idproduto, e.fatorconversao;
10494
10495 v_qtde number;
10496 v_caixa number;
10497 v_caixaAux number;
10498 v_unidade number;
10499 v_fcmaior number;
10500 v_descr varchar2(254);
10501 begin
10502 v_caixa := 0;
10503 v_unidade := 0;
10504
10505 if c_cx%isopen then
10506 close c_cx;
10507 end if;
10508
10509 open c_cx(p_idremanejamento, p_idproduto, p_loteIndustria, p_conferido);
10510 fetch c_cx
10511 into v_qtde, v_fcmaior;
10512
10513 if c_cx%found then
10514 while (c_cx%found)
10515 loop
10516 if (v_fcmaior > 1) then
10517 v_caixaAux := trunc(v_qtde / v_fcmaior);
10518 else
10519 v_caixaAux := 0;
10520 end if;
10521
10522 v_caixa := v_caixa + v_caixaAux;
10523
10524 v_unidade := v_unidade + (v_qtde - (v_caixaAux * v_fcmaior));
10525
10526 fetch c_cx
10527 into v_qtde, v_fcmaior;
10528 end loop;
10529 else
10530 return 'EMBALAGEM NAO CONFIGURADA';
10531 end if;
10532
10533 select decode(v_caixa, 0,
10534 decode(v_unidade, 0, '', v_unidade || ' UNIDADE(S)'),
10535 v_caixa || ' CAIXA(S)' ||
10536 decode(v_unidade, 0, '',
10537 ' e ' || v_unidade || ' UNIDADE(S)'))
10538 into v_descr
10539 from dual;
10540
10541 return trim(v_descr);
10542 end;
10543
10544 function confirmarTrocaLocalDestino
10545 (
10546 p_idremanejamento in number,
10547 p_idtrocalocal in number,
10548 p_idusuario in number
10549 ) return number is
10550
10551 type tpLoteRem is record(
10552 idproduto lote.idproduto%type,
10553 idlote lote.idlote%type,
10554 estado lote.estado%type,
10555 loteindustria lote.descr%type,
10556 dtvenc lote.dtvenc%type,
10557 iddepositante lote.iddepositante%type,
10558 loteuniconoendereco produtodepositante.loteuniconoendereco%type,
10559 idlocaldestino local.idlocal%type,
10560 qtdeentregue number);
10561
10562 type tpMovCancelada is record(
10563 id number,
10564 idonda number,
10565 idlote number,
10566 idlocaldestino number,
10567 quantidade number,
10568 idnotafiscal number,
10569 identificador number,
10570 ordem number);
10571
10572 type vTpLoteRem is table of tpLoteRem;
10573 type vTpMovCancelada is table of tpMovCancelada;
10574
10575 v_loteremtrocado vTpLoteRem;
10576 v_movcancelada vTpMovCancelada;
10577
10578 TROCALOCAL_ERRO constant number := 0;
10579 TROCALOCAL_SUCESSO constant number := 1;
10580
10581 TROCA_ANDAMENTO constant number := 0;
10582 TROCA_REALIZADA constant number := 1;
10583 TROCA_CANCELADA constant number := 2;
10584
10585 DETALHE_TROCA_ATIVO constant number := 1;
10586 DETALHE_TROCA_ALERTA constant number := 2;
10587 DETALHE_TROCA_PROCESSADO constant number := 3;
10588
10589 r_trocalocal trocarlocaldestinorem%rowtype;
10590 v_alerta number;
10591
10592 v_idlocalorigem number;
10593
10594 v_msg t_message;
10595
10596 v_qtdeCancelada number := 0;
10597 v_qtdeCriadas number := 0;
10598
10599 procedure validarPermissaoAlteracaoLocal is
10600 v_permitiralterarlocalqtdereab number;
10601 begin
10602 select count(1) permitiralterarlocalqtdereab
10603 into v_permitiralterarlocalqtdereab
10604 from remanejamento r, loteremanejamento lr, lote lt, depositante d
10605 where r.idremanejamento = p_idremanejamento
10606 and d.identidade = lt.iddepositante
10607 and lt.idlote = lr.idlote
10608 and lr.idremanejamento = r.idremanejamento
10609 and d.permitiralterarlocalqtdereab = 1;
10610
10611 if (v_permitiralterarlocalqtdereab = 0) then
10612 v_msg := t_message('O Remanejamento {0} não permite entregar em vários endereços de destino.');
10613 v_msg.addParam(p_idremanejamento);
10614 raise_application_error(-20000, v_msg.formatMessage);
10615 end if;
10616 end validarPermissaoAlteracaoLocal;
10617
10618 procedure getTrocaLocalDestino is
10619 begin
10620 begin
10621 select tl.*
10622 into r_trocalocal
10623 from trocarlocaldestinorem tl
10624 where tl.id = p_idtrocalocal;
10625 exception
10626 when no_data_found then
10627 v_msg := t_message('Troca de Local de Destino do remanejamento {0} não encontrada.');
10628 v_msg.addParam(p_idremanejamento);
10629 raise_application_error(-20000, v_msg.formatMessage);
10630 end;
10631
10632 if (r_trocalocal.idremanejamento <> p_idremanejamento) then
10633 v_msg := t_message('Troca de Local de Destino não pertence ao remanejamento {0}.');
10634 v_msg.addParam(p_idremanejamento);
10635 raise_application_error(-20000, v_msg.formatMessage);
10636 end if;
10637
10638 if (r_trocalocal.status = TROCA_REALIZADA) then
10639 v_msg := t_message('Troca de Local de Destino do remanejamento {0} está FINALIZADA.');
10640 v_msg.addParam(p_idremanejamento);
10641 raise_application_error(-20000, v_msg.formatMessage);
10642 end if;
10643
10644 if (r_trocalocal.status = TROCA_CANCELADA) then
10645 v_msg := t_message('Troca de Local de Destino do remanejamento {0} está CANCELADA.');
10646 v_msg.addParam(p_idremanejamento);
10647 raise_application_error(-20000, v_msg.formatMessage);
10648 end if;
10649 end getTrocaLocalDestino;
10650
10651 procedure validarDadosTrocaLocalDestino is
10652 v_qtdeTotalEntregue number;
10653 begin
10654 select sum(tld.qtdeentregue * e.fatorconversao)
10655 into v_qtdeTotalEntregue
10656 from trocarlocaldestinoremdet tld, trocarlocaldestinorem tl,
10657 embalagem e
10658 where tl.id = r_trocalocal.id
10659 and tl.status = TROCA_ANDAMENTO
10660 and tl.id = tld.idtrocalocal
10661 and tld.status = DETALHE_TROCA_ATIVO
10662 and e.idproduto = tld.idproduto
10663 and e.barra = tld.barra;
10664
10665 if (v_qtdeTotalEntregue = 0) then
10666 v_msg := t_message('Não existem quantidade(s) entregue(s) para realizar a troca de local destino.');
10667 raise_application_error(-20000, v_msg.formatMessage);
10668 end if;
10669
10670 if (r_trocalocal.qtdeentregar <> v_qtdeTotalEntregue) then
10671 v_msg := t_message('A quantide total entregue ativa é diferente da quantidade total a entregar.');
10672 raise_application_error(-20000, v_msg.formatMessage);
10673 end if;
10674 end validarDadosTrocaLocalDestino;
10675
10676 procedure validarEstoqueMinimoEntregue is
10677 v_estoqueMinimoEntregue boolean;
10678 begin
10679 --Verifica se existem outras operações diferente da onda que utilizam o estoque a entregar no remanejamento
10680 v_estoqueMinimoEntregue := false;
10681 for c_estreservado in (select idproduto, idlocal,
10682 sum(pendencia - qtdemovimentada) reservado
10683 from (select lt.idproduto, ll.idlocal,
10684 sum(ll.pendencia - ll.estoque) pendencia,
10685 0 qtdemovimentada
10686 from lotelocal ll, local ld, lote lt,
10687 loteremanejamento lr
10688 where lt.idlote = ll.idlote
10689 and ld.idarmazem = ll.idarmazem
10690 and ld.idlocal = ll.idlocal
10691 and ll.idarmazem =
10692 r_trocalocal.idarmazem
10693 and ll.idlocal =
10694 r_trocalocal.idlocaldestino
10695 and ll.pendencia > 0
10696 and lr.idlote = lt.idlote
10697 and lr.idremanejamento =
10698 r_trocalocal.idremanejamento
10699 group by lt.idproduto, ll.idlocal
10700 union
10701 select lt.idproduto, lo.idlocal,
10702 0 pendencia,
10703 sum(m.qtdemovimentada) qtdemovimentada
10704 from movimentacao m, lote lt, local lo,
10705 loteremanejamento lr
10706 where lo.idarmazem =
10707 r_trocalocal.idarmazem
10708 and lo.idlocal =
10709 r_trocalocal.idlocaldestino
10710 and lo.id = m.idlocalorigem
10711 and lt.idlote = m.idlote
10712 and m.status in (0, 1)
10713 and lr.idlote = lt.idlote
10714 and lr.idremanejamento =
10715 r_trocalocal.idremanejamento
10716 group by lt.idproduto, lo.idlocal)
10717 group by idproduto, idlocal
10718 having sum(pendencia - qtdemovimentada) > 0)
10719 loop
10720 v_estoqueMinimoEntregue := false;
10721 for c_entregue in (select tld.idproduto, tld.idlocaldestino,
10722 sum(tld.qtdeentregue * e.fatorconversao) totalentregue
10723 from trocarlocaldestinoremdet tld,
10724 trocarlocaldestinorem tl, embalagem e
10725 where tl.id = r_trocalocal.id
10726 and tl.status = TROCA_ANDAMENTO
10727 and tl.id = tld.idtrocalocal
10728 and tld.status = DETALHE_TROCA_ATIVO
10729 and tld.idproduto = c_estreservado.idproduto
10730 and tld.idlocaldestino =
10731 c_estreservado.idlocal
10732 and e.idproduto = tld.idproduto
10733 and e.barra = tld.barra
10734 group by tld.idproduto, tld.idlocaldestino
10735 having sum(tld.qtdeentregue * e.fatorconversao) >= c_estreservado.reservado)
10736 loop
10737 v_estoqueMinimoEntregue := true;
10738 end loop;
10739
10740 if (not v_estoqueMinimoEntregue) then
10741 update trocarlocaldestinoremdet
10742 set status = DETALHE_TROCA_ALERTA
10743 where idproduto = c_estreservado.idproduto
10744 and status = DETALHE_TROCA_ATIVO;
10745 end if;
10746 end loop;
10747 end validarEstoqueMinimoEntregue;
10748
10749 procedure prepararDadosParaTroca is
10750 v_qtdelote number;
10751 v_qtdeGtt number;
10752 begin
10753 for c_loterem in (select lt.idproduto, lt.idlote, lr.qtde
10754 from loteremanejamento lr, lote lt
10755 where lr.idremanejamento =
10756 r_trocalocal.idremanejamento
10757 and lt.idlote = lr.idlote
10758 order by lt.idproduto, lr.qtde)
10759 loop
10760 v_qtdelote := c_loterem.qtde;
10761 for c_troca in (select tld.idproduto, tld.idlocaldestino,
10762 sum((tld.qtdeentregue -
10763 nvl(tld.qtdeutilizada, 0)) *
10764 e.fatorconversao) totalentregue
10765 from trocarlocaldestinoremdet tld,
10766 trocarlocaldestinorem tl, embalagem e
10767 where tl.id = r_trocalocal.id
10768 and tl.status = TROCA_ANDAMENTO
10769 and tl.id = tld.idtrocalocal
10770 and tld.status = DETALHE_TROCA_ATIVO
10771 and tld.idproduto = c_loterem.idproduto
10772 and e.idproduto = tld.idproduto
10773 and e.barra = tld.barra
10774 group by tld.idproduto, tld.idlocaldestino
10775 order by sum(tld.qtdeentregue * e.fatorconversao),
10776 tld.idproduto, tld.idlocaldestino)
10777 loop
10778
10779 if (c_troca.totalentregue > v_qtdelote) then
10780 v_qtdeGtt := v_qtdelote;
10781 else
10782 v_qtdeGtt := c_troca.totalentregue;
10783
10784 end if;
10785
10786 update trocarlocaldestinoremdet tld
10787 set tld.qtdeutilizada = nvl(tld.qtdeutilizada, 0) + v_qtdeGtt
10788 where idproduto = c_troca.idproduto
10789 and idlocaldestino = c_troca.idlocaldestino
10790 and status = DETALHE_TROCA_ATIVO;
10791
10792 update trocarlocaldestinoremdet tld
10793 set status = DETALHE_TROCA_PROCESSADO
10794 where idproduto = c_troca.idproduto
10795 and idlocaldestino = c_troca.idlocaldestino
10796 and status = DETALHE_TROCA_ATIVO
10797 and tld.qtdeutilizada = tld.qtdeentregue;
10798
10799 insert into gtt_trocaloteremanejamento
10800 (idremanejamento, idlote, idlocaldestino, qtde)
10801 values
10802 (r_trocalocal.idremanejamento, c_loterem.idlote,
10803 c_troca.idlocaldestino, v_qtdeGtt);
10804
10805 v_qtdelote := v_qtdelote - v_qtdeGtt;
10806 exit when v_qtdelote = 0;
10807 end loop;
10808 end loop;
10809 end prepararDadosParaTroca;
10810
10811 procedure cancelarMovLoteRemAtual is
10812 begin
10813 -- Cancelar movimentações de lote que estão no remanejamento
10814 -- quando não foi entregue a quantidade suficiente que atenda a onda
10815 v_movcancelada := vTpMovCancelada();
10816 for c_movafet in (select m.id, m.idonda, lo.idarmazem, lo.idlocal,
10817 m.idlote, m.quantidade, m.idlocaldestino,
10818 m.idnotafiscal, m.identificador, m.ordem
10819 from movimentacao m, local lo, lote lt
10820 where lt.idlote = m.idlote
10821 and lo.idarmazem = r_trocalocal.idarmazem
10822 and lo.idlocal = r_trocalocal.idlocaldestino
10823 and lo.id = m.idlocalorigem
10824 and m.status in (0, 1)
10825 and exists
10826 (select 1
10827 from gtt_trocaloteremanejamento g
10828 where g.idremanejamento =
10829 r_trocalocal.idremanejamento
10830 and g.idlote = m.idlote))
10831 loop
10832 pk_estoque.retirar_pendencia(c_movafet.idarmazem, c_movafet.idlocal,
10833 c_movafet.idlote, c_movafet.quantidade,
10834 p_idusuario,
10835 'RETIRADA PENDENCIA PARA MOVIMENTACAO LOTE POR MOTIVO DE TROCA DE ENDEREÇO DE DESTINO DO REMANEJAMENTO:' ||
10836 r_trocalocal.idremanejamento ||
10837 ', ONDA ID: ' || c_movafet.idonda ||
10838 ', MOVIMENTAÇÃO ID: ' || c_movafet.id);
10839
10840 update movimentacao m
10841 set m.status = 3
10842 where m.id = c_movafet.id;
10843
10844 v_movcancelada.extend();
10845 v_movcancelada(v_movcancelada.count).id := c_movafet.id;
10846 v_movcancelada(v_movcancelada.count).idonda := c_movafet.idonda;
10847 v_movcancelada(v_movcancelada.count).idlote := c_movafet.idlote;
10848 v_movcancelada(v_movcancelada.count).idlocaldestino := c_movafet.idlocaldestino;
10849 v_movcancelada(v_movcancelada.count).quantidade := c_movafet.quantidade;
10850 v_movcancelada(v_movcancelada.count).idnotafiscal := c_movafet.idnotafiscal;
10851 v_movcancelada(v_movcancelada.count).identificador := c_movafet.identificador;
10852 v_movcancelada(v_movcancelada.count).ordem := c_movafet.ordem;
10853
10854 v_qtdeCancelada := v_qtdeCancelada + c_movafet.quantidade;
10855 end loop;
10856 end cancelarMovLoteRemAtual;
10857
10858 procedure finalizarRemanejamentoAtual is
10859 v_totallote number;
10860 begin
10861 -- Atualizando lotes do remanejamento atual para finaliza-lo
10862 v_loteremtrocado := vTpLoteRem();
10863
10864 update remanejamento
10865 set planejado = 'N',
10866 cadmanual = 'N'
10867 where idremanejamento = r_trocalocal.idremanejamento;
10868
10869 for c_troca in (select lt.idproduto, g.idlote, lt.estado,
10870 lt.descr loteindustria, lt.dtvenc,
10871 lt.iddepositante, pd.loteuniconoendereco,
10872 g.idlocaldestino, sum(g.qtde) totalentregue
10873 from gtt_trocaloteremanejamento g, lote lt,
10874 produtodepositante pd
10875 where g.idremanejamento =
10876 r_trocalocal.idremanejamento
10877 and pd.idproduto = lt.idproduto
10878 and pd.identidade = lt.iddepositante
10879 and lt.idlote = g.idlote
10880 group by lt.idproduto, g.idlote, lt.estado, lt.descr,
10881 lt.dtvenc, lt.iddepositante,
10882 pd.loteuniconoendereco, g.idlocaldestino)
10883 loop
10884 if (c_troca.idlocaldestino <> r_trocalocal.idlocaldestino) then
10885 update loteremanejamento
10886 set qtde = qtde - c_troca.totalentregue
10887 where idremanejamento = r_trocalocal.idremanejamento
10888 and idlote = c_troca.idlote;
10889
10890 v_loteremtrocado.extend();
10891 v_loteremtrocado(v_loteremtrocado.count).idproduto := c_troca.idproduto;
10892 v_loteremtrocado(v_loteremtrocado.count).idlote := c_troca.idlote;
10893 v_loteremtrocado(v_loteremtrocado.count).estado := c_troca.estado;
10894 v_loteremtrocado(v_loteremtrocado.count).loteindustria := c_troca.loteindustria;
10895 v_loteremtrocado(v_loteremtrocado.count).dtvenc := c_troca.dtvenc;
10896 v_loteremtrocado(v_loteremtrocado.count).iddepositante := c_troca.iddepositante;
10897 v_loteremtrocado(v_loteremtrocado.count).loteuniconoendereco := c_troca.loteuniconoendereco;
10898 v_loteremtrocado(v_loteremtrocado.count).idlocaldestino := c_troca.idlocaldestino;
10899 v_loteremtrocado(v_loteremtrocado.count).qtdeentregue := c_troca.totalentregue;
10900 end if;
10901 end loop;
10902
10903 -- Retirando lote que não serão entregues no remanejamento atual
10904 for c_loteretirar in (select lr.idremanejamento, lt.idlote
10905 from loteremanejamento lr, lote lt
10906 where lr.idremanejamento =
10907 r_trocalocal.idremanejamento
10908 and lt.idlote = lr.idlote
10909 and lr.qtde = 0)
10910 loop
10911 delete from loteremanejamento
10912 where idremanejamento = c_loteretirar.idremanejamento
10913 and idlote = c_loteretirar.idlote;
10914 end loop;
10915
10916 -- Finalizando o remanejamento atual
10917 select count(*)
10918 into v_totallote
10919 from loteremanejamento
10920 where idremanejamento = r_trocalocal.idremanejamento;
10921
10922 if (v_totallote > 0) then
10923 -- Finaliza o planejamento
10924 update remanejamento
10925 set planejado = 'S'
10926 where idremanejamento = r_trocalocal.idremanejamento;
10927
10928 pk_Remanejamento.finalizarRemanejamento(r_trocalocal.idremanejamento,
10929 p_idusuario);
10930 else
10931 update trocarlocaldestinorem
10932 set idremanejamento = null
10933 where idremanejamento = r_trocalocal.idremanejamento;
10934
10935 delete from remanejamentoromaneio
10936 where idremanejamento = r_trocalocal.idremanejamento;
10937
10938 delete from remanejamento
10939 where idremanejamento = r_trocalocal.idremanejamento;
10940 end if;
10941 end finalizarRemanejamentoAtual;
10942
10943 procedure criarNovoRemParaNovoDestino is
10944 r_loteunico pk_lote.t_loteunico;
10945 v_mensagem varchar2(1000);
10946 v_idremanejamento number;
10947 begin
10948 -- Criando remanejamento do lotes que vão para outros endereços
10949 for i in 1 .. v_loteremtrocado.count
10950 loop
10951 r_loteunico.idProduto := v_loteremtrocado(i).idproduto;
10952 r_loteunico.estado := v_loteremtrocado(i).estado;
10953 r_loteunico.loteindustria := v_loteremtrocado(i).loteindustria;
10954 r_loteunico.dtvencimento := v_loteremtrocado(i).dtvenc;
10955 r_loteunico.loteuniconoendereco := v_loteremtrocado(i)
10956 .loteuniconoendereco;
10957 r_loteunico.iddepositante := v_loteremtrocado(i).iddepositante;
10958
10959 -- Validação de compatibilidade para o endereço que o lote será remanejado.
10960 if not
10961 pk_lote.isLocalPodeReceberLoteUnico(r_loteunico,
10962 r_trocalocal.idarmazem,
10963 v_loteremtrocado(i)
10964 .idlocaldestino, v_mensagem) then
10965 if v_mensagem is not null then
10966 v_msg := t_message('ERRO NO LOTE INDÚSTRIA: {0}. LOTE NÃO PODE SER ALOCADO NO ENDEREÇO {1}. ERRO NA VALIDAÇÃO DO REMANEJAMENTO: {2}');
10967 v_msg.addParam(r_loteunico.loteindustria);
10968 v_msg.addParam(v_loteremtrocado(i).idlocaldestino);
10969 v_msg.addParam(v_mensagem);
10970 raise_application_error(-20000, v_msg.formatMessage);
10971 end if;
10972 end if;
10973
10974 -- Cria o remanejamento
10975 select seq_remanejamento.nextval
10976 into v_idremanejamento
10977 from dual;
10978
10979 insert into remanejamento
10980 (idremanejamento, idarmazemorigem, idlocalorigem,
10981 idarmazemdestino, idlocaldestino, datahora, idusuariotela, status,
10982 cadmanual, descrpalet, planejado)
10983 values
10984 (v_idremanejamento, r_trocalocal.idarmazem,
10985 r_trocalocal.idlocalorigem, r_trocalocal.idarmazem,
10986 v_loteremtrocado(i).idlocaldestino, sysdate, p_idusuario, 'A',
10987 'N',
10988 'TROCA DE LOCAL DESTINO DO REMANEJAMENTO ' ||
10989 r_trocalocal.idremanejamento, 'N');
10990
10991 -- Vincula o lote no remanejamento
10992 insert into loteremanejamento
10993 (idremanejamento, idlote, qtde, conferido, controlaqtde)
10994 values
10995 (v_idremanejamento, v_loteremtrocado(i).idlote,
10996 v_loteremtrocado(i).qtdeentregue, 'S', 'S');
10997
10998 -- Finaliza o planejamento
10999 update remanejamento
11000 set planejado = 'S'
11001 where idremanejamento = v_idremanejamento;
11002
11003 -- Executa o remanejamento
11004 pk_Remanejamento.finalizarRemanejamento(v_idremanejamento,
11005 p_idusuario);
11006 end loop;
11007 end criarNovoRemParaNovoDestino;
11008
11009 -- Refactored procedure criarMovNovoLocalDestino
11010 procedure criarMovNovoLocalDestino is
11011 v_idconfiguracaoonda number;
11012 r_movNovaTrocada movimentacao%rowtype;
11013 v_idgrupomovimentacao number;
11014 v_qtdemov number;
11015 v_qtdenova number;
11016 begin
11017 -- Criar movimentações novas para os lotes que foram afetados pelo remanejamento para pegar das novas origens
11018 for i in 1 .. v_movcancelada.count
11019 loop
11020 select r.idconfiguracaoonda
11021 into v_idconfiguracaoonda
11022 from romaneiopai r
11023 where r.idromaneio = v_movcancelada(i).idonda;
11024
11025 v_qtdemov := v_movcancelada(i).quantidade;
11026 for c_novolocal in (select idlote, idendereco, idarmazem, idlocal,
11027 sum(totalentregue - totalmov) totalrestante
11028 from (select g.idlote, ld.id idendereco,
11029 ld.idarmazem,
11030 g.idlocaldestino idlocal,
11031 sum(g.qtde) totalentregue,
11032 0 totalmov
11033 from gtt_trocaloteremanejamento g,
11034 lote lt, local ld
11035 where g.idremanejamento =
11036 r_trocalocal.idremanejamento
11037 and lt.idlote = g.idlote
11038 and g.idlote = v_movcancelada(i)
11039 .idlote
11040 and ld.idarmazem =
11041 r_trocalocal.idarmazem
11042 and ld.idlocal = g.idlocaldestino
11043 group by g.idlote, ld.id, ld.idarmazem,
11044 g.idlocaldestino
11045 union
11046 select m.idlote, lo.id idendereco,
11047 lo.idarmazem, lo.idlocal,
11048 0 totalentregue,
11049 sum(m.quantidade) totalmov
11050 from movimentacao m, local lo,
11051 gtt_trocaloteremanejamento g
11052 where g.idlote = m.idlote
11053 and g.idlocaldestino = lo.idlocal
11054 and lo.idarmazem =
11055 r_trocalocal.idarmazem
11056 and lo.id = m.idlocalorigem
11057 and m.idlote = v_movcancelada(i)
11058 .idlote
11059 and m.status in (0, 1)
11060 group by m.idlote, lo.id, lo.idarmazem,
11061 lo.idlocal)
11062 group by idlote, idendereco, idarmazem, idlocal
11063 having sum(totalentregue - totalmov) > 0)
11064 loop
11065 -- calcula a qtde correta para a geração da movimentação
11066 if (c_novolocal.totalrestante > v_qtdemov) then
11067 v_qtdenova := v_qtdemov;
11068 else
11069 v_qtdenova := c_novolocal.totalrestante;
11070 end if;
11071
11072 pk_estoque.incluir_pendencia(c_novolocal.idarmazem,
11073 c_novolocal.idlocal,
11074 c_novolocal.idlote, v_qtdenova,
11075 p_idusuario,
11076 'ADICIONADO PENDENCIA PARA NOVA MOVIMENTACAO CRIADA POR MOTIVO DE TROCA DE ENDEREÇO DESTINO NO REMANEJAMENTO: ' ||
11077 r_trocalocal.idremanejamento ||
11078 ' IDTROCALOCAL:' || r_trocalocal.id ||
11079 ', ONDA ID: ' || v_movcancelada(i)
11080 .idonda);
11081
11082 r_movNovaTrocada.id := null;
11083 r_movNovaTrocada.idlocalorigem := c_novolocal.idendereco;
11084 r_movNovaTrocada.idlocaldestino := v_movcancelada(i)
11085 .idlocaldestino;
11086 r_movNovaTrocada.quantidade := v_qtdenova;
11087 r_movNovaTrocada.etapa := 1;
11088 r_movNovaTrocada.idlote := c_novolocal.idlote;
11089 r_movNovaTrocada.status := 0;
11090 r_movNovaTrocada.idonda := v_movcancelada(i).idonda;
11091 r_movNovaTrocada.qtdemovimentada := v_qtdenova;
11092 r_movNovaTrocada.idnotafiscal := v_movcancelada(i).idnotafiscal;
11093 r_movNovaTrocada.datainicio := null;
11094 r_movNovaTrocada.idusuario := null;
11095 r_movNovaTrocada.qtdeconferida := 0;
11096 r_movNovaTrocada.identificador := v_movcancelada(i)
11097 .identificador;
11098 r_movNovaTrocada.tiposeparacao := null;
11099 r_movNovaTrocada.ordem := v_movcancelada(i).ordem;
11100
11101 pk_triggers_control.disableTrigger('T_insereMovimentacao');
11102
11103 v_idgrupomovimentacao := pk_onda.inserirMovimentacao(r_movNovaTrocada,
11104 v_idgrupomovimentacao);
11105
11106 pk_triggers_control.enableTrigger('T_insereMovimentacao');
11107
11108 v_qtdemov := v_qtdemov - v_qtdenova;
11109
11110 v_qtdeCriadas := v_qtdeCriadas + v_qtdenova;
11111 exit when v_qtdemov = 0;
11112 end loop;
11113 pk_romaneio.preencherInfSeparacao(v_movcancelada(i).idonda,
11114 v_idconfiguracaoonda);
11115 end loop;
11116
11117 if (v_qtdeCriadas <> v_qtdeCancelada) then
11118 raise_application_error(-20000,
11119 'Não foi possÃvel concluir a operação, a quantidade de movimentação cancelada: ' ||
11120 v_qtdeCancelada ||
11121 ' é diferente da quantidade nova criada: ' ||
11122 v_qtdeCriadas);
11123 end if;
11124 end criarMovNovoLocalDestino;
11125 begin
11126 validarPermissaoAlteracaoLocal;
11127 getTrocaLocalDestino;
11128 validarDadosTrocaLocalDestino;
11129 validarEstoqueMinimoEntregue;
11130
11131 select count(*)
11132 into v_alerta
11133 from trocarlocaldestinoremdet
11134 where idtrocalocal = r_trocalocal.id
11135 and status = DETALHE_TROCA_ALERTA;
11136
11137 if (v_alerta > 0) then
11138 return TROCALOCAL_ERRO;
11139 end if;
11140
11141 prepararDadosParaTroca;
11142 cancelarMovLoteRemAtual;
11143 finalizarRemanejamentoAtual;
11144 criarNovoRemParaNovoDestino;
11145 criarMovNovoLocalDestino;
11146
11147 update trocarlocaldestinorem
11148 set status = TROCA_REALIZADA,
11149 datafinalizacao = sysdate
11150 where id = r_trocalocal.id;
11151
11152 pk_utilities.GeraLog(p_idusuario,
11153 'Troca Local Destino id: ' || r_trocalocal.id ||
11154 ' Realizada.', r_trocalocal.idremanejamento, 'TL');
11155
11156 return TROCALOCAL_SUCESSO;
11157 end;
11158
11159 function verificaCompOrigemDestino
11160 (
11161 p_qtdeTotalLote in lote.qtdedisponivel%type,
11162 p_fatorConversaoLote in lote.fatorconversao%type,
11163 p_qtdeLoteRemanejado in lote.qtdedisponivel%type
11164 ) return boolean is
11165 begin
11166 -- verifica se a quantidade escolhida pode formar uma ou mais caixas exatas
11167 -- baseando-se no fator do lote selecionado
11168 -- e se com a quantidade remanescente na origem também será possÃvel formar
11169 -- caixas exatas com o mesmo fator do lote selecionado, garantindo assim,
11170 -- a compatibiliadade com o setor.
11171 if ((mod(p_qtdeLoteRemanejado, p_fatorConversaoLote) = 0) and
11172 (mod((p_qtdeTotalLote - p_qtdeLoteRemanejado), p_fatorConversaoLote) = 0)) then
11173 return true;
11174 else
11175 return false;
11176 end if;
11177 end verificaCompOrigemDestino;
11178
11179 procedure infLoteIndRemanejarMaterial
11180 (
11181 p_idLote in number,
11182 p_loteIndustria in varchar2
11183 ) is
11184 v_iddepositante number;
11185 v_idproduto number;
11186 v_dtvenc date;
11187 begin
11188
11189 select l.iddepositante, l.idproduto, l.dtvenc
11190 into v_iddepositante, v_idproduto, v_dtvenc
11191 from lote l
11192 where l.idlote = p_idLote;
11193
11194 if (pk_lote.isLoteIndustriaComMaisDeUmVenc(v_iddepositante, v_idproduto,
11195 p_loteIndustria, v_dtvenc)) then
11196 raise_application_error(-20000,
11197 'Não é possÃvel cadastrar lote do mesmo produto/depositante com o mesmo lote indústria e data de vencimento. Lote: ' ||
11198 p_idLote);
11199 end if;
11200
11201 update lote
11202 set descr = p_loteIndustria
11203 where idlote = p_idLote;
11204 end;
11205
11206 function validarImpressaoEtqRemanej
11207 (
11208 p_idUsuario in number,
11209 p_idsupervisor in number := 0
11210 ) return number is
11211
11212 SOLICITAR_SENHASUPERVISOR constant number := 0;
11213 IMPRIMIR_DIRETO constant number := 1;
11214 PRE_VISUALIZAR_IMPRESSAO constant number := 2;
11215
11216 C_NAO constant number := 0;
11217 C_SIM constant number := 1;
11218
11219 v_solSenhaSuperReimpEtqRemanej number;
11220 v_preVisualizarEtqRemanej number;
11221 v_existeEtqImpressa number;
11222
11223 v_resultado number;
11224 v_finalizar number;
11225 v_validar number;
11226
11227 v_msg t_message;
11228
11229 begin
11230
11231 v_resultado := PRE_VISUALIZAR_IMPRESSAO;
11232 v_finalizar := C_NAO;
11233
11234 select count(1)
11235 into v_existeEtqImpressa
11236 from remanejamento rm, gtt_selecao g
11237 where rm.idremanejamento = g.idselecionado
11238 and rm.etiquetaremanejamentoimpressa = C_SIM;
11239
11240 select count(*)
11241 into v_validar
11242 from dual
11243 where exists
11244 (select 1
11245 from gtt_selecao g, remanejamento r
11246 where r.idremanejamento = g.idselecionado
11247 and not exists
11248 (select 1
11249 from loteremanejamento lr
11250 where lr.idremanejamento = r.idremanejamento));
11251
11252 if (v_validar > 0) then
11253 v_msg := t_message('Existem remanejamentos selecionados que não possuem lotes vinculados.');
11254 raise_application_error(-20000, v_msg.formatMessage);
11255 end if;
11256
11257 for c_idRemanejamento in (select idselecionado
11258 from gtt_selecao)
11259 loop
11260
11261 if (v_existeEtqImpressa > 0) then
11262 for c_depositantes in (select distinct d.identidade,
11263 d.solsenhasuperreimpetiqremaneja,
11264 d.previsualizaretiqremanejamento
11265 from loteremanejamento lmr, lote lt,
11266 depositante d
11267 where lmr.idremanejamento =
11268 c_idRemanejamento.Idselecionado
11269 and lt.idlote = lmr.idlote
11270 and d.identidade = lt.iddepositante)
11271 loop
11272 if (c_depositantes.solsenhasuperreimpetiqremaneja = C_SIM and
11273 p_idSupervisor = 0) then
11274 v_resultado := SOLICITAR_SENHASUPERVISOR;
11275 v_finalizar := C_SIM;
11276 exit;
11277 else
11278 if (c_depositantes.previsualizaretiqremanejamento = C_NAO) then
11279 v_resultado := IMPRIMIR_DIRETO;
11280 v_finalizar := C_SIM;
11281 exit;
11282 end if;
11283 end if;
11284 end loop;
11285 else
11286 for c_depositantes in (select distinct d.identidade,
11287 d.previsualizaretiqremanejamento
11288 from loteremanejamento lmr, lote lt,
11289 depositante d
11290 where lmr.idremanejamento =
11291 c_idRemanejamento.Idselecionado
11292 and lt.idlote = lmr.idlote
11293 and d.identidade = lt.iddepositante)
11294 loop
11295 if (c_depositantes.previsualizaretiqremanejamento = C_NAO) then
11296 v_resultado := IMPRIMIR_DIRETO;
11297 v_finalizar := C_SIM;
11298 exit;
11299 end if;
11300 end loop;
11301 end if;
11302
11303 if (v_finalizar = C_SIM) then
11304 exit;
11305 end if;
11306
11307 end loop;
11308
11309 return v_resultado;
11310
11311 end validarImpressaoEtqRemanej;
11312end pk_remanejamento;
11313/