· 6 years ago · Apr 11, 2019, 07:18 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 p_mudarPickingVazio in number
4356
4357 ) is
4358 v_totaldepositantes number;
4359 v_totalpermitidomover number;
4360 v_totalestoque number;
4361 v_mudarPickingVazio number;
4362 r_loteUnicoComparar pk_lote.t_loteunico;
4363 r_loteunico pk_lote.t_loteunico;
4364 v_loteuniconoendereco number;
4365 v_msgErro varchar2(1000);
4366 v_msg t_message;
4367 v_remanejamentos varchar2(4000);
4368 begin
4369
4370 for c in (select l.idproduto, l.descr, l.dtvenc, l.estado,
4371 pd.loteuniconoendereco, l.iddepositante
4372 from lotelocal ll, lote l, produtodepositante pd
4373 where l.idlote = ll.idlote
4374 and ll.idlocal = p_idlocalorigem
4375 and ll.idarmazem = p_idArmazem
4376 and l.idproduto = p_idproduto
4377 and l.iddepositante = pd.identidade
4378 and pd.idproduto = l.idproduto)
4379 loop
4380
4381 r_loteunicocomparar.idproduto := c.idproduto;
4382 r_loteunicocomparar.estado := c.estado;
4383 r_loteunicocomparar.loteindustria := c.descr;
4384 r_loteunicocomparar.dtvencimento := c.dtvenc;
4385 r_loteunicocomparar.loteuniconoendereco := c.loteuniconoendereco;
4386 r_loteunicocomparar.iddepositante := c.iddepositante;
4387
4388 if (not
4389 pk_lote.isLocalPodeReceberLoteUnico(r_loteunicocomparar,
4390 p_idArmazem, p_idlocaldestino,
4391 v_msgErro)) then
4392 raise_application_error(-20000, v_msgErro);
4393 end if;
4394 end loop;
4395
4396 select count(distinct lt.iddepositante)
4397 into v_totaldepositantes
4398 from lotelocal ll, lote lt
4399 where ll.idlocal = p_idlocalorigem
4400 and ll.idarmazem = p_idarmazem
4401 and lt.idlote = ll.idlote
4402 and lt.idproduto = p_idproduto;
4403
4404 if (v_totaldepositantes > 1) then
4405 v_msg := t_message('Erro ao executar mudança de picking. Local de Origem possui estoque de mais de um depositante.');
4406 raise_application_error(-20000, v_msg.formatMessage);
4407 end if;
4408
4409 select a.transferirpendenciaeadicionar, a.mudarpickingvazio
4410 into p_transfPendenciaAdicionar, v_mudarPickingVazio
4411 from armazem a
4412 where a.idarmazem = p_idArmazem;
4413
4414 select sum(ll.estoque - ll.pendencia) permitido,
4415 sum(ll.estoque + ll.adicionar) total
4416 into v_totalpermitidomover, v_totalestoque
4417 from lotelocal ll, lote lt
4418 where ll.idlocal = p_idlocalorigem
4419 and ll.idarmazem = p_idArmazem
4420 and (ll.estoque > 0 or ll.pendencia > 0 or ll.adicionar > 0)
4421 and lt.idlote = ll.idlote
4422 and lt.idproduto = p_idproduto
4423 and lt.iddepositante = p_iddepositante;
4424
4425 if (v_totalestoque = 0 and v_mudarPickingVazio = 0) then
4426 v_msg := t_message('Endereço de origem sem estoque do produto. Operação cancelada');
4427 raise_application_error(-20000, v_msg.formatMessage);
4428 end if;
4429
4430 if (v_totalpermitidomover = 0 and p_transfPendenciaAdicionar = 0) then
4431 v_msg := t_message('A configuração para transferir pendencia e adicionar na mudança de picking esta destivada.');
4432 raise_application_error(-20000, v_msg.formatMessage);
4433 end if;
4434
4435 select substr(stragg(r.idremanejamento), 1, 4000)
4436 into v_remanejamentos
4437 from remanejamento r, loteremanejamento lr, lote lt
4438 where r.idlocalorigem = p_idlocaldestino
4439 and r.idlocaldestino = p_idlocalorigem
4440 and r.idarmazemorigem = p_idArmazem
4441 and r.idarmazemdestino = p_idArmazem
4442 and lr.idremanejamento = r.idremanejamento
4443 and lt.idlote = lr.idlote
4444 and lt.idproduto = p_idproduto
4445 and lt.iddepositante = p_iddepositante
4446 and r.status <> 'F';
4447
4448 if (v_remanejamentos is not null) then
4449 v_msg := t_message('Existe(m) remanejamento(s) criado(s) com local de origem igual o ' ||
4450 'local destino e local destino igual ao local origem da mudança de ' ||
4451 'picking para este produto e depositante. Finalize ou cancele o(s) remanejamento(s) ' ||
4452 'para continuar. Remanejamento(s): {0}');
4453 v_msg.addParam(v_remanejamentos);
4454 raise_application_error(-20000, v_msg.formatMessage);
4455 end if;
4456
4457 pk_armazem.validarSetorPickingProduto(p_idArmazem, p_idlocaldestino,
4458 p_iddepositante, p_idproduto);
4459
4460 end validarMudancaPicking;
4461
4462 -- Refactored procedure criarRemMudancaPicking
4463 procedure criarRemMudancaPicking
4464 (
4465 p_idusuario in number,
4466 p_iddepositante in number,
4467 p_idproduto in number,
4468 p_idArmazem in number,
4469 p_idlocalorigem in local.idlocal%type,
4470 p_idlocaldestino in local.idlocal%type,
4471 p_transfPendenciaAdicionar in number,
4472 p_idremanejamento in out number
4473 ) is
4474 v_jaexistepickdestino number;
4475 v_qtdelr number;
4476 begin
4477 select seq_remanejamento.nextval idremanejamento
4478 into p_idremanejamento
4479 from dual;
4480
4481 pk_triggers_control.disableTrigger('T_BEFORE_REMANEJAMENTO');
4482
4483 insert into remanejamento
4484 (idremanejamento, idarmazemorigem, idlocalorigem, idarmazemdestino,
4485 idlocaldestino, datahora, idusuariotela, status, cadmanual,
4486 idromaneio, descrpalet, planejado, horainicio, tipo)
4487 values
4488 (p_idremanejamento, p_idarmazem, p_idlocalorigem, p_idarmazem,
4489 p_idlocaldestino, sysdate, p_idusuario, c_aguardando, c_nao, NULL,
4490 'REMANEJAMENTO CRIADO PARA MUDANÇA DE PICKING', 'N', sysdate, 3);
4491
4492 pk_triggers_control.enableTrigger('T_BEFORE_REMANEJAMENTO');
4493
4494 delete from produtolocal pd
4495 where pd.idarmazem = p_idArmazem
4496 and pd.idlocal = p_idlocalorigem
4497 and pd.idproduto = p_idproduto
4498 and pd.identidade = p_iddepositante;
4499
4500 select count(*)
4501 into v_jaexistepickdestino
4502 from produtolocal pd
4503 where pd.idarmazem = p_idArmazem
4504 and pd.idlocal = p_idlocaldestino
4505 and pd.idproduto = p_idproduto
4506 and pd.identidade = p_iddepositante;
4507
4508 if (v_jaexistepickdestino = 0) then
4509 pk_armazem.criarPickingProduto(p_idArmazem, p_idlocaldestino,
4510 p_iddepositante, p_idproduto, 1);
4511 end if;
4512
4513 for c_lote in (select ll.idlote, (ll.estoque - ll.pendencia) permitido,
4514 (ll.estoque + ll.adicionar) total
4515 from lotelocal ll, lote lt
4516 where ll.idlocal = p_idlocalorigem
4517 and ll.idarmazem = p_idArmazem
4518 and (ll.estoque > 0 or ll.pendencia > 0 or
4519 ll.adicionar > 0)
4520 and lt.idlote = ll.idlote
4521 and lt.idproduto = p_idproduto
4522 and lt.iddepositante = p_iddepositante)
4523 loop
4524 if (p_transfPendenciaAdicionar = 1) then
4525 v_qtdelr := c_lote.total;
4526 else
4527 if (c_lote.permitido > 0) then
4528 v_qtdelr := c_lote.permitido;
4529 else
4530 v_qtdelr := 0;
4531 end if;
4532 end if;
4533
4534 if (v_qtdelr > 0) then
4535 insert into loteremanejamento
4536 (idremanejamento, idlote, qtde, conferido, controlaqtde)
4537 values
4538 (p_idremanejamento, c_lote.idlote, v_qtdelr, 'S', 'S');
4539 end if;
4540 end loop;
4541 end criarRemMudancaPicking;
4542
4543 -- Refactored procedure moverEstoqueMudPicking
4544 procedure moverEstoqueMudPicking
4545 (
4546 p_idusuario in number,
4547 p_idArmazem in number,
4548 p_idlocalorigem in local.idlocal%type,
4549 p_idlocaldestino in local.idlocal%type,
4550 v_transfPendenciaAdicionar in out number,
4551 v_idremanejamento in out number
4552 ) is
4553 v_qtdemover number;
4554 begin
4555 for c_estoque in (select ll.idlote, ll.estoque, ll.pendencia,
4556 ll.adicionar, lo.tipo tipoLocalOrigem,
4557 lo.buffer bufferOrigem, lt.idproduto,
4558 lt.iddepositante
4559 from lotelocal ll, local lo, lote lt
4560 where ll.idlocal = p_idlocalorigem
4561 and ll.idarmazem = p_idarmazem
4562 and lo.idlocal = ll.idlocal
4563 and lo.idarmazem = ll.idarmazem
4564 and lt.idlote = ll.idlote
4565 and exists
4566 (select 1
4567 from loteremanejamento lr
4568 where lr.idremanejamento = v_idremanejamento
4569 and lr.idlote = ll.idlote)
4570 order by ll.idlote)
4571 loop
4572 if (v_transfPendenciaAdicionar = 0) then
4573 v_qtdemover := c_estoque.estoque - c_estoque.pendencia;
4574 if (v_qtdemover > 0) then
4575 pk_estoque.retirar_estoque(p_idArmazem, p_idlocalorigem,
4576 c_estoque.idlote, v_qtdemover,
4577 p_idusuario,
4578 'RETIRADO ESTOQUE REFERENTE MUDANCA DE PICKING ID: ' ||
4579 v_idremanejamento, 'N');
4580
4581 if ((c_estoque.tipolocalorigem = 0) AND
4582 (c_estoque.bufferorigem = 'N')) then
4583 pk_picking_dinamico.addGttPickingDinamico(c_estoque.idproduto,
4584 c_estoque.iddepositante,
4585 p_idlocalorigem,
4586 p_idArmazem, 0);
4587 end if;
4588 pk_picking_dinamico.addGttPickingDinamico(c_estoque.idproduto,
4589 c_estoque.iddepositante,
4590 p_idlocaldestino,
4591 p_idArmazem, 1);
4592
4593 pk_estoque.incluir_estoque(p_idArmazem, p_idlocaldestino,
4594 c_estoque.idlote, v_qtdemover,
4595 p_idusuario,
4596 'ADICIONADO ESTOQUE REFERENTE MUDANCA DE PICKING ID: ' ||
4597 v_idremanejamento, 'N');
4598 end if;
4599 else
4600 if (c_estoque.pendencia > 0) then
4601 pk_estoque.retirar_pendencia(p_idArmazem, p_idlocalorigem,
4602 c_estoque.idlote, c_estoque.pendencia,
4603 p_idusuario,
4604 'RETIRADA PENDENCIA REFERENTE MUDANCA DE PICKING ID: ' ||
4605 v_idremanejamento);
4606 end if;
4607
4608 if (c_estoque.estoque > 0) then
4609 pk_estoque.retirar_estoque(p_idArmazem, p_idlocalorigem,
4610 c_estoque.idlote, c_estoque.estoque,
4611 p_idusuario,
4612 'RETIRADO ESTOQUE REFERENTE MUDANCA DE PICKING ID: ' ||
4613 v_idremanejamento, 'N');
4614
4615 if ((c_estoque.tipolocalorigem = 0) AND
4616 (c_estoque.bufferorigem = 'N')) then
4617 pk_picking_dinamico.addGttPickingDinamico(c_estoque.idproduto,
4618 c_estoque.iddepositante,
4619 p_idlocalorigem,
4620 p_idArmazem, 0);
4621 end if;
4622
4623 pk_picking_dinamico.addGttPickingDinamico(c_estoque.idproduto,
4624 c_estoque.iddepositante,
4625 p_idlocaldestino,
4626 p_idArmazem, 1);
4627 pk_estoque.incluir_estoque(p_idArmazem, p_idlocaldestino,
4628 c_estoque.idlote, c_estoque.estoque,
4629 p_idusuario,
4630 'ADICIONADO ESTOQUE REFERENTE MUDANCA DE PICKING ID: ' ||
4631 v_idremanejamento, 'N');
4632 end if;
4633
4634 if (c_estoque.adicionar > 0) then
4635 pk_estoque.retirar_adicionar(p_idArmazem, p_idlocalorigem,
4636 c_estoque.idlote, c_estoque.adicionar,
4637 p_idusuario,
4638 'RETIRADO ADICIONAR REFERENTE MUDANCA DE PICKING ID: ' ||
4639 v_idremanejamento);
4640 pk_estoque.incluir_adicionar(p_idArmazem, p_idlocaldestino,
4641 c_estoque.idlote, c_estoque.adicionar,
4642 p_idusuario,
4643 'INCLUIDO ADICIONAR REFERENTE MUDANCA DE PICKING ID: ' ||
4644 v_idremanejamento);
4645 end if;
4646
4647 if (c_estoque.pendencia > 0) then
4648 pk_estoque.incluir_pendencia(p_idArmazem, p_idlocaldestino,
4649 c_estoque.idlote, c_estoque.pendencia,
4650 p_idusuario,
4651 'ADICIONADA PENDENCIA REFERENTE MUDANCA DE PICKING ID: ' ||
4652 v_idremanejamento);
4653 end if;
4654 end if;
4655 end loop;
4656 pk_picking_dinamico.deletar_picking_dinamico;
4657 pk_picking_dinamico.inserir_picking_dinamico;
4658 end moverEstoqueMudPicking;
4659
4660 -- Refactored procedure alterarRemMudPicking
4661 procedure alterarRemMudPicking
4662 (
4663 p_idusuario in number,
4664 p_iddepositante in number,
4665 p_idproduto in number,
4666 p_idarmazem in number,
4667 p_idlocalorigem in local.idlocal%type,
4668 p_idlocaldestino in local.idlocal%type,
4669 p_idremanejamento in number
4670 ) is
4671 v_totallr number;
4672 v_totaltitulos number;
4673 v_idnovorem number;
4674 v_tiporem number;
4675 v_tiponovorem number;
4676 begin
4677 for c_rem in (select r.idremanejamento, r.idlocaldestino, lr.idlote,
4678 r.idromaneio, lr.qtde, r.idlocalorigem,
4679 lr.controlaqtde, r.tipo
4680 from remanejamento r, loteremanejamento lr, lote lt
4681 where r.status <> 'F'
4682 and r.tipo <> 3
4683 and ((r.idarmazemorigem = p_idarmazem and
4684 r.idlocalorigem = p_idlocalOrigem) or
4685 (r.idarmazemdestino = p_idarmazem and
4686 r.idlocaldestino = p_idlocalorigem))
4687 and lr.idremanejamento = r.idremanejamento
4688 and lt.idlote = lr.idlote
4689 and lt.idproduto = p_idproduto
4690 and lt.iddepositante = p_iddepositante)
4691 loop
4692 select r.tipo
4693 into v_tiporem
4694 from remanejamento r
4695 where r.idremanejamento = c_rem.idremanejamento;
4696
4697 update remanejamento r
4698 set r.tipo = 3
4699 where r.idremanejamento = c_rem.idremanejamento;
4700
4701 -- Se a origem do remanejamento é a mesma origem da mudança de picking
4702 -- o endenreco de origem do remanejamento deve apontar para o destino
4703 -- da mudança de picking
4704 if (c_rem.idlocalorigem = p_idlocalorigem) then
4705 -- se o destino do remanejamento é o destino da mudança de picking
4706 -- o estoque esta sendo movido pela mudança de picking
4707 -- e não é necessario a existencia deste remanejamento
4708 -- portanto a pendencia e adcionar serão removidos
4709 -- do endereço de destino, pois a mudança de picking ja
4710 -- moveu o estoque.
4711 if (c_rem.idlocaldestino = p_idlocaldestino) then
4712 pk_utilities.GeraLog(p_idusuario,
4713 'MUDANÇA DE PICKING ID ' ||
4714 p_idremanejamento ||
4715 ' ALTEROU REMANEJAMENTO ' ||
4716 c_rem.idremanejamento || ', LOTE ' ||
4717 c_rem.idlote || ' FOI REMOVIDO',
4718 p_idremanejamento, 'MP');
4719
4720 delete from loteremanejamento lr
4721 where lr.idremanejamento = c_rem.idremanejamento
4722 and lr.idlote = c_rem.idlote;
4723
4724 if c_rem.controlaqtde = 'S' then
4725 update remanejamento
4726 set qtde = nvl(qtde, 0) - c_rem.qtde
4727 where idremanejamento = c_rem.idremanejamento;
4728 end if;
4729
4730 pk_estoque.retirar_pendencia(p_idArmazem, p_idlocaldestino,
4731 c_rem.idlote, c_rem.qtde, p_idusuario,
4732 'RETIRADA PENDENCIA REFERENTE MUDANCA DE PICKING ID: ' ||
4733 p_idremanejamento);
4734
4735 pk_estoque.retirar_adicionar(p_idArmazem, p_idlocaldestino,
4736 c_rem.idlote, c_rem.qtde, p_idusuario,
4737 'RETIRADO ADICIONAR REFERENTE MUDANCA DE PICKING ID: ' ||
4738 p_idremanejamento);
4739
4740 select count(*)
4741 into v_totallr
4742 from loteremanejamento lr
4743 where lr.idremanejamento = c_rem.idremanejamento;
4744
4745 if (v_totallr = 0) then
4746 pk_utilities.GeraLog(p_idusuario,
4747 'MUDANÇA DE PICKING ID ' ||
4748 p_idremanejamento || '. REMANEJAMENTO ' ||
4749 c_rem.idremanejamento ||
4750 ' EXCLUIDO DEVIDO NÃO POSSUIR MAIS NENHUM LOTE.',
4751 p_idremanejamento, 'MP');
4752
4753 delete from remanejamento r
4754 where r.idremanejamento = c_rem.idremanejamento;
4755 end if;
4756 -- caso o local de destino do remanejamento seja diferente
4757 -- do local de destino da mudança de picking
4758 -- somente o endereço de origem do remanejamento
4759 -- será trocado
4760 else
4761 select count(distinct lt.idproduto)
4762 into v_totaltitulos
4763 from loteremanejamento lr, lote lt
4764 where lr.idremanejamento = c_rem.idremanejamento
4765 and lt.idlote = lr.idlote;
4766
4767 -- se o remanejamento possui apenas lotes do produto que
4768 -- participa da mudança de picking, apenas o local do
4769 -- remanejamento é trocado
4770 if (v_totaltitulos = 1) then
4771 pk_utilities.GeraLog(p_idusuario,
4772 'MUDANÇA DE PICKING ID ' ||
4773 p_idremanejamento ||
4774 ' ALTEROU REMANEJAMENTO ' ||
4775 c_rem.idremanejamento ||
4776 '. LOCAL DE ORIGEM TROCADO. ORIGEM ANTERIOR ' ||
4777 c_rem.idlocalorigem || ', NOVA ORIGEM ' ||
4778 p_idlocaldestino, p_idremanejamento, 'MP');
4779
4780 update remanejamento r
4781 set r.idlocalorigem = p_idlocaldestino
4782 where r.idremanejamento = c_rem.idremanejamento;
4783 -- se o remanejamento possui lotes de outros produtos,
4784 -- será necessario retirar os lotes do produto que participa
4785 -- da mudança de picking e gerar um novo remanejamento
4786 -- para mover o estoque corretamente. A pendencia do primeiro remanejamento
4787 -- não será removida, devido a mudança de picking ja
4788 -- ter movido a pendencia para o local de destino,
4789 -- onde o novo remanejamento irá utilizar esta pendencia
4790 else
4791 delete from loteremanejamento lr
4792 where lr.idremanejamento = c_rem.idremanejamento
4793 and lr.idlote = c_rem.idlote;
4794
4795 if c_rem.controlaqtde = 'S' then
4796 update remanejamento
4797 set qtde = nvl(qtde, 0) - c_rem.qtde
4798 where idremanejamento = c_rem.idremanejamento;
4799 end if;
4800
4801 v_idnovorem := cadastrar_remanejamento(p_idarmazem, p_idarmazem,
4802 p_idlocaldestino,
4803 c_rem.idlocaldestino,
4804 p_idusuario,
4805 c_rem.idromaneio,
4806 'REMANEJAMENTO CRIADO DEVIDO A MUDANCA DE PICKING ID: ' ||
4807 p_idremanejamento, 'N',
4808 'N');
4809
4810 select r.tipo
4811 into v_tiponovorem
4812 from remanejamento r
4813 where r.idremanejamento = v_idnovorem;
4814
4815 update remanejamento r
4816 set r.tipo = 3
4817 where r.idremanejamento = v_idnovorem;
4818
4819 begin
4820 insert into loteremanejamento
4821 (idremanejamento, idlote, qtde, conferido, controlaqtde)
4822 values
4823 (v_idnovorem, c_rem.idlote, c_rem.qtde, 'N', 'S');
4824 exception
4825 when dup_val_on_index then
4826 update loteremanejamento lr
4827 set lr.qtde = lr.qtde + c_rem.qtde
4828 where lr.idremanejamento = v_idnovorem
4829 and lr.idlote = c_rem.idlote;
4830 end;
4831
4832 update remanejamento r
4833 set r.tipo = v_tiponovorem,
4834 r.qtde = decode(c_rem.controlaqtde, 'S',
4835 nvl(r.qtde, 0) - c_rem.qtde, r.qtde)
4836 where r.idremanejamento = v_idnovorem;
4837
4838 pk_utilities.GeraLog(p_idusuario,
4839 'MUDANÇA DE PICKING ID ' ||
4840 p_idremanejamento ||
4841 ' ALTEROU O REMANEJAMENTO ' ||
4842 c_rem.idremanejamento || '. LOTE ' ||
4843 c_rem.idlote ||
4844 ' FOI REMOVIDO E ADICIONADO NO REMANEJAMENTO ' ||
4845 v_idnovorem, p_idremanejamento, 'MP');
4846 end if;
4847 end if;
4848 -- se o destino do remanejamento é igual a origem da mudança
4849 -- de picking, o destino do remanejamento deverá ser trocado para
4850 -- o destino da mudança de picking
4851 else
4852 -- se a origem do remanejamento é igual ao destino da mudança de
4853 -- picking, o estoque não deve ser movido, pois a mudança de picking
4854 -- esta determinando a nova posição do estoque, e este remanejamento
4855 -- não deve mais existir
4856 if (c_rem.idlocalorigem = p_idlocaldestino) then
4857 pk_utilities.GeraLog(p_idusuario,
4858 'MUDANÇA DE PICKING ID ' ||
4859 p_idremanejamento ||
4860 ' ALTEROU REMANEJAMENTO ' ||
4861 c_rem.idremanejamento || ', LOTE ' ||
4862 c_rem.idlote || ' FOI REMOVIDO',
4863 p_idremanejamento, 'MP');
4864
4865 delete from loteremanejamento lr
4866 where lr.idremanejamento = c_rem.idremanejamento
4867 and lr.idlote = c_rem.idlote;
4868
4869 if c_rem.controlaqtde = 'S' then
4870 update remanejamento
4871 set qtde = nvl(qtde, 0) - c_rem.qtde
4872 where idremanejamento = c_rem.idremanejamento;
4873 end if;
4874
4875 pk_estoque.retirar_pendencia(p_idArmazem, p_idlocaldestino,
4876 c_rem.idlote, c_rem.qtde, p_idusuario,
4877 'RETIRADA PENDENCIA REFERENTE MUDANCA DE PICKING ID: ' ||
4878 p_idremanejamento);
4879
4880 pk_estoque.retirar_adicionar(p_idArmazem, p_idlocaldestino,
4881 c_rem.idlote, c_rem.qtde, p_idusuario,
4882 'RETIRADO ADICIONAR REFERENTE MUDANCA DE PICKING ID: ' ||
4883 p_idremanejamento);
4884
4885 select count(*)
4886 into v_totallr
4887 from loteremanejamento lr
4888 where lr.idremanejamento = c_rem.idremanejamento;
4889
4890 if (v_totallr = 0) then
4891 pk_utilities.GeraLog(p_idusuario,
4892 'MUDANÇA DE PICKING ID ' ||
4893 p_idremanejamento || '. REMANEJAMENTO ' ||
4894 c_rem.idremanejamento ||
4895 ' EXCLUIDO DEVIDO NÃO POSSUIR MAIS NENHUM LOTE.',
4896 p_idremanejamento, 'MP');
4897
4898 delete from remanejamento r
4899 where r.idremanejamento = c_rem.idremanejamento;
4900 end if;
4901 -- se a origem do remanejamento não é o destino da mudanca de picking,
4902 -- o destino do remanejamento deverá ser o mesmo destino da mudança de picking
4903 else
4904 select count(distinct lt.idproduto)
4905 into v_totaltitulos
4906 from loteremanejamento lr, lote lt
4907 where lr.idremanejamento = c_rem.idremanejamento
4908 and lt.idlote = lr.idlote;
4909
4910 if (v_totaltitulos = 1) then
4911 pk_utilities.GeraLog(p_idusuario,
4912 'MUDANÇA DE PICKING ID ' ||
4913 p_idremanejamento ||
4914 ' ALTEROU REMANEJAMENTO ' ||
4915 c_rem.idremanejamento ||
4916 '. LOCAL DE DESTINO TROCADO. DESTINO ANTERIOR ' ||
4917 c_rem.idlocaldestino || ', NOVO DESTINO ' ||
4918 p_idlocaldestino, p_idremanejamento, 'MP');
4919
4920 update remanejamento r
4921 set r.idlocaldestino = p_idlocaldestino
4922 where r.idremanejamento = c_rem.idremanejamento;
4923 -- se o remanejamento possuir lotes de outros produtos,
4924 -- deverá ser criado um novo remanejamento para mover
4925 -- somente os lotes do produto que participa da mudança
4926 -- de picking
4927 else
4928 delete from loteremanejamento lr
4929 where lr.idremanejamento = c_rem.idremanejamento
4930 and lr.idlote = c_rem.idlote;
4931
4932 if c_rem.controlaqtde = 'S' then
4933 update remanejamento
4934 set qtde = nvl(qtde, 0) - c_rem.qtde
4935 where idremanejamento = c_rem.idremanejamento;
4936 end if;
4937
4938 v_idnovorem := cadastrar_remanejamento(p_idarmazem, p_idarmazem,
4939 c_rem.idlocalorigem,
4940 p_idlocaldestino,
4941 p_idusuario,
4942 c_rem.idromaneio,
4943 'REMANEJAMENTO CRIADO DEVIDO A MUDANCA DE PICKING ID: ' ||
4944 p_idremanejamento, 'N',
4945 'N');
4946
4947 select r.tipo
4948 into v_tiponovorem
4949 from remanejamento r
4950 where r.idremanejamento = v_idnovorem;
4951
4952 update remanejamento r
4953 set r.tipo = 3
4954 where r.idremanejamento = v_idnovorem;
4955
4956 begin
4957 insert into loteremanejamento
4958 (idremanejamento, idlote, qtde, conferido, controlaqtde)
4959 values
4960 (v_idnovorem, c_rem.idlote, c_rem.qtde, 'N', 'S');
4961 exception
4962 when dup_val_on_index then
4963 update loteremanejamento lr
4964 set lr.qtde = lr.qtde + c_rem.qtde
4965 where lr.idremanejamento = v_idnovorem
4966 and lr.idlote = c_rem.idlote;
4967 end;
4968
4969 update remanejamento r
4970 set r.tipo = v_tiponovorem,
4971 r.qtde = decode(c_rem.controlaqtde, 'S',
4972 nvl(r.qtde, 0) - c_rem.qtde, r.qtde)
4973 where r.idremanejamento = v_idnovorem;
4974
4975 pk_utilities.GeraLog(p_idusuario,
4976 'MUDANÇA DE PICKING ID ' ||
4977 p_idremanejamento ||
4978 ' ALTEROU O REMANEJAMENTO ' ||
4979 c_rem.idremanejamento || '. LOTE ' ||
4980 c_rem.idlote ||
4981 ' FOI REMOVIDO E ADICIONADO NO REMANEJAMENTO ' ||
4982 v_idnovorem, p_idremanejamento, 'MP');
4983 end if;
4984 end if;
4985 end if;
4986
4987 update remanejamento r
4988 set r.tipo = v_tiporem
4989 where r.idremanejamento = c_rem.idremanejamento;
4990 end loop;
4991 end alterarRemMudPicking;
4992
4993 procedure alterarAjusteMudPicking
4994 (
4995 p_idusuario in number,
4996 p_iddepositante in number,
4997 p_idproduto in number,
4998 p_idArmazem in number,
4999 p_idlocalorigem in local.idlocal%type,
5000 p_idlocaldestino in local.idlocal%type,
5001 p_idremanejamento in number
5002 ) is
5003 v_idajuste number;
5004 begin
5005 v_idajuste := 0;
5006 for c_aje in (select ae.idajustemovtoentrada, ae.idajustemovto
5007 from ajustemovto ajm, ajustemovtoentrada ae
5008 where ajm.gerado = 'N'
5009 and ajm.identidade = p_iddepositante
5010 and ae.idajustemovto = ajm.idajustemovto
5011 and ae.idarmazem = p_idarmazem
5012 and ae.idlocal = p_idlocalorigem
5013 and ae.idproduto = p_idproduto)
5014 loop
5015 update ajustemovtoentrada a
5016 set a.idlocal = p_idlocaldestino
5017 where a.idajustemovtoentrada = c_aje.idajustemovtoentrada;
5018
5019 if (v_idajuste <> c_aje.idajustemovto) then
5020 v_idajuste := c_aje.idajustemovto;
5021
5022 pk_utilities.GeraLog(p_idusuario,
5023 'MUDANÇA DE PICKING ID ' || p_idremanejamento ||
5024 ' ALTEROU O AJUSTE DE ENTRADA ' ||
5025 c_aje.idajustemovto ||
5026 '. ENDEREÇO TROCADO. ENDEREÇO ANTERIOR' ||
5027 p_idlocalorigem || ', NOVO ENDEREÇO ' ||
5028 p_idlocaldestino, p_idremanejamento, 'MP');
5029 end if;
5030 end loop;
5031
5032 v_idajuste := 0;
5033 pk_triggers_control.disableTrigger('t_before_ajustemovtosaida');
5034 for c_ajs in (select ajs.idarmazem, ajs.idlocal, ajs.idlote,
5035 ajs.idajustemovto
5036 from ajustemovto ajm, ajustemovtosaida ajs, lote lt
5037 where ajm.gerado = 'N'
5038 and ajm.identidade = p_iddepositante
5039 and ajs.idajustemovto = ajm.idajustemovto
5040 and ajs.idarmazem = p_idarmazem
5041 and ajs.idlocal = p_idlocalorigem
5042 and lt.idlote = ajs.idlote
5043 and lt.idproduto = p_idproduto)
5044 loop
5045 update ajustemovtosaida a
5046 set a.idlocal = p_idlocaldestino
5047 where a.idarmazem = c_ajs.idarmazem
5048 and a.idlocal = c_ajs.idlocal
5049 and a.idlote = c_ajs.idlote
5050 and a.idajustemovto = c_ajs.idajustemovto;
5051
5052 if (v_idajuste <> c_ajs.idajustemovto) then
5053 v_idajuste := c_ajs.idajustemovto;
5054
5055 pk_utilities.GeraLog(p_idusuario,
5056 'MUDANÇA DE PICKING ID ' || p_idremanejamento ||
5057 ' ALTEROU O AJUSTE DE SAIDA ' ||
5058 c_ajs.idajustemovto ||
5059 '. ENDEREÇO TROCADO. ENDEREÇO ANTERIOR' ||
5060 p_idlocalorigem || ', NOVO ENDEREÇO ' ||
5061 p_idlocaldestino, p_idremanejamento, 'MP');
5062 end if;
5063 end loop;
5064 pk_triggers_control.enableTrigger('t_before_ajustemovtosaida');
5065 end;
5066
5067 procedure alterarRomaneioMudPicking
5068 (
5069 p_idusuario in number,
5070 p_iddepositante in number,
5071 p_idproduto in number,
5072 p_idArmazem in number,
5073 p_idlocalorigem in local.idlocal%type,
5074 p_idlocaldestino in local.idlocal%type,
5075 p_idremanejamento in number
5076 ) is
5077 v_paletsepjaexiste number;
5078 begin
5079 delete from gtt_selecao;
5080 for c_lote in (select ps.idarmazem, ps.idlocal, ps.idlote, ps.idproduto,
5081 ps.idromaneio, ps.idpalet, ps.barra, ps.qtde,
5082 ps.qtdeunit
5083 from romaneiopai r, paletseparacao ps, lote lt
5084 where r.tipo = 0
5085 and r.processado = 'N'
5086 and ps.idromaneio = r.idromaneio
5087 and ps.idarmazem = p_idArmazem
5088 and ps.idlocal = p_idlocalorigem
5089 and ps.idproduto = p_idproduto
5090 and lt.idlote = ps.idlote
5091 and lt.iddepositante = p_iddepositante)
5092 loop
5093 insert into gtt_selecao
5094 (idselecionado)
5095 values
5096 (c_lote.idromaneio);
5097
5098 select count(*)
5099 into v_paletsepjaexiste
5100 from paletseparacao ps
5101 where ps.idarmazem = c_lote.idarmazem
5102 and ps.idlocal = p_idlocaldestino
5103 and ps.idlote = c_lote.idlote
5104 and ps.idproduto = c_lote.idproduto
5105 and ps.idromaneio = c_lote.idromaneio
5106 and ps.idpalet = c_lote.idpalet
5107 and ps.barra = c_lote.barra;
5108
5109 if (v_paletsepjaexiste = 0) then
5110 update paletseparacao ps
5111 set ps.idlocal = p_idlocaldestino
5112 where ps.idarmazem = c_lote.idarmazem
5113 and ps.idlocal = c_lote.idlocal
5114 and ps.idlote = c_lote.idlote
5115 and ps.idproduto = c_lote.idproduto
5116 and ps.idromaneio = c_lote.idromaneio
5117 and ps.idpalet = c_lote.idpalet
5118 and ps.barra = c_lote.barra;
5119
5120 pk_utilities.GeraLog(p_idusuario,
5121 'MUDANÇA DE PICKING ID ' || p_idremanejamento ||
5122 ' ALTEROU ROMANEIO ' || c_lote.idromaneio ||
5123 '. ENDEREÇO DO PALETSEPARACAO (idarmazem ' ||
5124 c_lote.idarmazem || ', idlocal ' ||
5125 c_lote.Idlocal || ', idlote ' || c_lote.idlote ||
5126 ', idproduto ' || c_lote.idproduto ||
5127 ', idromaneio ' || c_lote.idromaneio ||
5128 ', idpalet ' || c_lote.idpalet || ', barra ' ||
5129 c_lote.barra ||
5130 ') TROCADO. ENDEREÇO ANTERIOR ' ||
5131 c_lote.idlocal || ', NOVO ENDEREÇO ' ||
5132 p_idlocaldestino, p_idremanejamento, 'MP');
5133 else
5134 update paletseparacao ps
5135 set ps.qtde = ps.qtde + c_lote.qtde,
5136 ps.qtdeunit = ps.qtdeunit + c_lote.qtdeunit
5137 where ps.idarmazem = c_lote.idarmazem
5138 and ps.idlocal = p_idlocaldestino
5139 and ps.idlote = c_lote.idlote
5140 and ps.idproduto = c_lote.idproduto
5141 and ps.idromaneio = c_lote.idromaneio
5142 and ps.idpalet = c_lote.idpalet
5143 and ps.barra = c_lote.barra;
5144
5145 delete from paletseparacao ps
5146 where ps.idarmazem = c_lote.idarmazem
5147 and ps.idlocal = c_lote.idlocal
5148 and ps.idlote = c_lote.idlote
5149 and ps.idproduto = c_lote.idproduto
5150 and ps.idromaneio = c_lote.idromaneio
5151 and ps.idpalet = c_lote.idpalet
5152 and ps.barra = c_lote.barra;
5153
5154 pk_utilities.GeraLog(p_idusuario,
5155 'MUDANÇA DE PICKING ID ' || p_idremanejamento ||
5156 ' ALTEROU O ROMANEIO ' || c_lote.idromaneio ||
5157 '. QUANTIDADE DO PRODUTO NO PALETSEPARACAO (idarmazem ' ||
5158 c_lote.idarmazem || ', idlocal ' ||
5159 c_lote.Idlocal || ', idlote ' || c_lote.idlote ||
5160 ', idproduto ' || c_lote.idproduto ||
5161 ', idromaneio ' || c_lote.idromaneio ||
5162 ', idpalet ' || c_lote.idpalet || ', barra ' ||
5163 c_lote.barra ||
5164 ') MOVIDA PARA O PALETSEPARACAO EXISTENTE (idarmazem ' ||
5165 c_lote.idarmazem || ', idlocal ' ||
5166 p_idlocaldestino || ', idlote ' ||
5167 c_lote.idlote || ', idproduto ' ||
5168 c_lote.idproduto || ', idromaneio ' ||
5169 c_lote.idromaneio || ', idpalet ' ||
5170 c_lote.idpalet || ', barra ' || c_lote.barra || ')' ||
5171 '. QTDEUNIT AUMENTADA ' || c_lote.Qtdeunit ||
5172 ', QTDE AUMENTADA ' || c_lote.qtde,
5173 p_idremanejamento, 'MP');
5174 end if;
5175 end loop;
5176
5177 for c_rom in (select distinct idselecionado
5178 from gtt_selecao)
5179 loop
5180 pk_romaneio.RecalculaPaletSepCliente(c_rom.idselecionado);
5181 pk_romaneio.identificarlotes(c_rom.idselecionado);
5182 end loop;
5183 end;
5184
5185 procedure alterarAlocacaoMudPicking
5186 (
5187 p_idusuario in number,
5188 p_iddepositante in number,
5189 p_idproduto in number,
5190 p_idArmazem in number,
5191 p_idlocalorigem in local.idlocal%type,
5192 p_idlocaldestino in local.idlocal%type,
5193 p_idremanejamento in number
5194 ) is
5195 begin
5196 for c_mapa in (select m.idalocacao
5197 from mapaalocacao m, lote lt
5198 where m.status <> 'F'
5199 and m.idarmazem = p_idArmazem
5200 and m.idlocal = p_idlocalorigem
5201 and lt.idlote = m.idlote
5202 and lt.idproduto = p_idproduto
5203 and lt.iddepositante = p_iddepositante)
5204 loop
5205 update mapaalocacao
5206 set idlocal = p_idlocaldestino
5207 where idalocacao = c_mapa.idalocacao;
5208
5209 pk_utilities.GeraLog(p_idusuario,
5210 'MUDANÇA DE PICKING ID ' || p_idremanejamento ||
5211 ' ALTEROU O MAPA DE ALOCACAO ' ||
5212 c_mapa.Idalocacao ||
5213 '. ENDEREÇO TROCADO. ENDEREÇO ANTERIOR ' ||
5214 p_idlocalorigem || ', NOVO ENDEREÇO ' ||
5215 p_idlocaldestino, p_idremanejamento, 'MP');
5216 end loop;
5217
5218 end;
5219
5220 procedure alterarOndaMudPicking
5221 (
5222 p_idusuario in number,
5223 p_iddepositante in number,
5224 p_idproduto in number,
5225 p_idArmazem in number,
5226 p_idlocalorigem in local.idlocal%type,
5227 p_idlocaldestino in local.idlocal%type,
5228 p_idremanejamento in number
5229 ) is
5230 v_movanterior number;
5231 v_idlocalanterior local.idlocal%type;
5232 v_idnovolocal number;
5233 begin
5234 select id
5235 into v_idnovolocal
5236 from local
5237 where idarmazem = p_idArmazem
5238 and idlocal = p_idlocaldestino;
5239
5240 for c_mov in (select m.idonda, m.id, lt.idlote, lo.idlocal,
5241 m.qtdemovimentada, m.etapa, m.idlocalorigem
5242 from movimentacao m, local lo, lote lt
5243 where m.status in (0, 1)
5244 and lo.id = m.idlocalorigem
5245 and lo.idarmazem = p_idArmazem
5246 and lo.idlocal = p_idlocalorigem
5247 and lt.idlote = m.idlote
5248 and lt.idproduto = p_idproduto
5249 and lt.iddepositante = p_iddepositante)
5250 loop
5251 begin
5252 select m.id
5253 into v_movanterior
5254 from grupomovimentacao g, movimentacao m
5255 where g.idgrupo = c_mov.id
5256 and m.id = g.idmovimentacao
5257 and m.etapa < c_mov.etapa
5258 and m.idlocaldestino = c_mov.idlocal
5259 and m.status in (0, 1);
5260 exception
5261 when no_data_found then
5262 v_movanterior := 0;
5263 end;
5264
5265 if (v_movanterior > 0) then
5266 select ld.idlocal
5267 into v_idlocalanterior
5268 from movimentacao m, local ld
5269 where m.id = v_movanterior
5270 and ld.id = m.idlocaldestino;
5271
5272 update movimentacao m
5273 set m.idlocaldestino = v_idnovolocal
5274 where m.id = v_movanterior;
5275
5276 pk_utilities.GeraLog(p_idusuario,
5277 'MUDANÇA DE PICKING ID ' || p_idremanejamento ||
5278 ' ALTEROU ONDA ' || c_mov.idonda ||
5279 ', MOVIMENTACAO ID ' || v_movanterior ||
5280 '. ENDEREÇO DE DESTINO ANTERIOR ' ||
5281 v_idlocalanterior || ', NOVO ENDEREÇO ' ||
5282 p_idlocaldestino, p_idremanejamento, 'MP');
5283 end if;
5284
5285 update movimentacao m
5286 set m.idlocalorigem = v_idnovolocal
5287 where m.id = c_mov.id;
5288
5289 pk_utilities.GeraLog(p_idusuario,
5290 'MUDANÇA DE PICKING ID ' || p_idremanejamento ||
5291 ' ALTEROU ONDA ' || c_mov.idonda ||
5292 ', MOVIMENTACAO ID ' || c_mov.id ||
5293 '. ENDEREÇO DE ORIGEM ANTERIOR ' ||
5294 c_mov.idlocal || ', NOVO ENDEREÇO ' ||
5295 p_idlocaldestino, p_idremanejamento, 'MP');
5296 end loop;
5297
5298 for c_mov in (select m.idonda, m.id, lt.idlote, ld.idlocal,
5299 m.qtdemovimentada, m.etapa, m.idlocalorigem
5300 from movimentacao m, local ld, lote lt
5301 where m.status in (0, 1)
5302 and ld.id = m.idlocaldestino
5303 and ld.idarmazem = p_idArmazem
5304 and ld.idlocal = p_idlocalorigem
5305 and lt.idlote = m.idlote
5306 and lt.idproduto = p_idproduto
5307 and lt.iddepositante = p_iddepositante)
5308 loop
5309 update movimentacao m
5310 set m.idlocaldestino = v_idnovolocal
5311 where m.id = c_mov.id;
5312
5313 pk_utilities.GeraLog(p_idusuario,
5314 'MUDANÇA DE PICKING ID ' || p_idremanejamento ||
5315 ' ALTEROU ONDA ' || c_mov.idonda ||
5316 ', MOVIMENTACAO ID ' || c_mov.id ||
5317 '. ENDEREÇO DE DESTINO ANTERIOR ' ||
5318 c_mov.idlocal || ', NOVO ENDEREÇO ' ||
5319 p_idlocaldestino, p_idremanejamento, 'MP');
5320 end loop;
5321 end;
5322
5323 procedure alterarCorteFisicoMudPicking
5324 (
5325 p_idusuario in number,
5326 p_iddepositante in number,
5327 p_idproduto in number,
5328 p_idArmazem in number,
5329 p_idlocalorigem in local.idlocal%type,
5330 p_idlocaldestino in local.idlocal%type,
5331 p_idremanejamento in number
5332 ) is
5333 v_idnovolocal number;
5334 begin
5335 select id
5336 into v_idnovolocal
5337 from local
5338 where idarmazem = p_idArmazem
5339 and idlocal = p_idlocaldestino;
5340
5341 for c_corte in (select c.id idcorte
5342 from cortefisico c, local lo
5343 where c.status = 0
5344 and c.idproduto = p_idproduto
5345 and lo.id = c.idenderecofalta
5346 and lo.idarmazem = p_idArmazem
5347 and lo.idlocal = p_idlocalorigem
5348 and exists
5349 (select 1
5350 from cortefisiconf cnf, notafiscal nf
5351 where cnf.idcortefisico = c.id
5352 and cnf.qtdeseparacao <> cnf.qtdeutilizada
5353 and nf.idnotafiscal = cnf.idnotafiscal
5354 and nf.iddepositante = p_iddepositante))
5355 loop
5356 update cortefisico c
5357 set c.idenderecofalta = v_idnovolocal
5358 where c.id = c_corte.idcorte;
5359
5360 update movimentacao m
5361 set m.idlocalorigem = v_idnovolocal
5362 where exists (select 1
5363 from cortefisiconf cnf, resestoquecortefisiconf r
5364 where cnf.idcortefisico = c_corte.idcorte
5365 and cnf.qtdeseparacao <> cnf.qtdeutilizada
5366 and r.idcortefisiconf = cnf.id
5367 and r.idmovimentacaoafetada = m.id);
5368
5369 pk_utilities.GeraLog(p_idusuario,
5370 'MUDANÇA DE PICKING ID ' || p_idremanejamento ||
5371 ' ALTEROU CORTE FÃSICO ID ' || c_corte.idcorte ||
5372 '. ENDEREÇO DE DESTINO ANTERIOR ' ||
5373 p_idlocalorigem || ', NOVO ENDEREÇO ' ||
5374 p_idlocaldestino, p_idremanejamento, 'MP');
5375 end loop;
5376
5377 -- Atualizar local de corte para os cortes de separação e conferência da carga
5378 for c_cortesep in (select c.id idcorte
5379 from cortefisico c, local lo, lote lt, lotelocal ll
5380 where c.status = 0
5381 and c.idproduto = p_idproduto
5382 and ll.idarmazem = lo.idarmazem
5383 and ll.idlocal = lo.idlocal
5384 and ll.idlote = lt.idlote
5385 and lt.idproduto = c.idproduto
5386 and lt.iddepositante = p_iddepositante
5387 and lo.id = c.idenderecofalta
5388 and lo.idarmazem = p_idArmazem
5389 and lo.idlocal = p_idlocalorigem
5390 and (c.idconfexpedicaocarga is not null or exists
5391 (select 1
5392 from separacaoporcarga s
5393 where s.idcortefisico = c.id)))
5394 loop
5395 update cortefisico c
5396 set c.idenderecofalta = v_idnovolocal
5397 where c.id = c_cortesep.idcorte;
5398
5399 update separacaoporcarga
5400 set idlocal = p_idlocaldestino
5401 where idcortefisico = c_cortesep.idcorte;
5402
5403 update paleteondanf pn
5404 set pn.idlocal = p_idlocaldestino
5405 where exists (select 1
5406 from separacaoporcarga s, paleteondanf p,
5407 reservaestoqueondacarga r
5408 where r.idpaleteondanf = pn.idpaleteondanf
5409 and r.idpaleteondanf = p.idpaleteondanf
5410 and r.idseparacaoporcarga = s.idseparacaoporcarga
5411 and s.idcortefisico = c_cortesep.idcorte);
5412
5413 update reservaestoqueondacarga re
5414 set re.idlocal = p_idlocaldestino
5415 where exists (select 1
5416 from separacaoporcarga s, paleteondanf p,
5417 reservaestoqueondacarga r
5418 where r.idseparacaoporcarga = re.idseparacaoporcarga
5419 and r.idpaleteondanf = p.idpaleteondanf
5420 and r.idseparacaoporcarga = s.idseparacaoporcarga
5421 and s.idcortefisico = c_cortesep.idcorte);
5422
5423 pk_utilities.GeraLog(p_idusuario,
5424 'MUDANÇA DE PICKING ID ' || p_idremanejamento ||
5425 ' ALTEROU CORTE FÃSICO ID ' ||
5426 c_cortesep.idcorte ||
5427 '. ENDEREÇO DE DESTINO ANTERIOR ' ||
5428 p_idlocalorigem || ', NOVO ENDEREÇO ' ||
5429 p_idlocaldestino, p_idremanejamento, 'MP');
5430 end loop;
5431 end;
5432
5433 procedure alteraratividadesMudPicking
5434 (
5435 p_idusuario in number,
5436 p_iddepositante in number,
5437 p_idproduto in number,
5438 p_idArmazem in number,
5439 p_idlocalorigem in local.idlocal%type,
5440 p_idlocaldestino in local.idlocal%type,
5441 p_idremanejamento in number
5442 ) is
5443
5444 procedure deletarAtividadeInutilizada(p_codonda in number) is
5445 begin
5446 delete from atividade a
5447 where a.idtipoatividade = 9
5448 and a.idoperacao = p_codonda
5449 and status <> 'F'
5450 and exists
5451 (select 1
5452 from romaneiopai rp, movimentacao m, local lo,
5453 regiaoarmazenagem ro, local ld, regiaoarmazenagem rd,
5454 tipoatividadearmazem ta
5455 where rp.idromaneio = a.idoperacao
5456 and lo.idregiao = a.idregiaoorigem
5457 and lo.buffer = a.bufferorigem
5458 and ld.idregiao = a.idregiaodestino
5459 and ld.buffer = a.bufferdestino
5460 and m.idonda = rp.idromaneio
5461 and lo.id = m.idlocalorigem
5462 and ro.idregiao = lo.idregiao
5463 and ld.id = m.idlocaldestino
5464 and rd.idregiao = ld.idregiao
5465 and ta.idarmazem = lo.idarmazem
5466 and ta.idtipoatividade = 9 -- tipo separação por onda
5467 and m.status in (0, 1)
5468 and not (m.qtdemovimentada - m.qtdeconferida = 0 and
5469 m.status = 2)
5470 and (ld.idregiao not in
5471 (select idregiao
5472 from regiaoarmazenagem
5473 where tipo = decode(ro.tipo, 7, null, 3)) or
5474 (lo.buffer = 'N' and ld.buffer = 'N' and
5475 ro.tipo in (0, 1) and rd.tipo = 3))
5476 and not
5477 (lo.buffer = 'S' and
5478 lo.idregiao in (select idregiao
5479 from regiaoarmazenagem
5480 where tipo = 2) and ld.buffer = 'N' and
5481 ld.idregiao in (select idregiao
5482 from regiaoarmazenagem
5483 where tipo = 2))
5484 and not
5485 (lo.buffer = 'N' and m.status = 1 and
5486 lo.idregiao in (select idregiao
5487 from regiaoarmazenagem
5488 where tipo = 0) and ld.buffer = 'N' and
5489 ld.idregiao in (select idregiao
5490 from regiaoarmazenagem
5491 where tipo = 2))
5492 group by ro.idregiao, ro.descr, lo.buffer, rd.idregiao,
5493 rd.descr, ld.buffer, ro.tipo, rd.tipo, lo.tipo,
5494 lo.picking, ld.tipo, ld.picking,
5495 decode(ro.tipo, 6, 99, 1, 0, 0, 1, ro.tipo));
5496 end;
5497
5498 begin
5499 for c_onda in (select o.idromaneio idonda, o.codigointerno codonda,
5500 o.tituloromaneio titulo, o.idconfiguracaoonda
5501 from romaneiopai o
5502 where exists (select 1
5503 from movimentacao m, local lo, local ld
5504 where lo.id = m.idlocalorigem
5505 and ld.id = m.idlocaldestino
5506 and m.idonda = o.idromaneio
5507 and ((ld.idlocal = p_idlocaldestino and
5508 ld.idarmazem = p_idArmazem) or
5509 (lo.idlocal = p_idlocaldestino and
5510 lo.idarmazem = p_idArmazem)))
5511 and o.statusonda in (2, 4)
5512 and o.tipo = 1
5513 group by o.idromaneio, o.codigointerno, o.tituloromaneio,
5514 o.idconfiguracaoonda)
5515 loop
5516 deletarAtividadeInutilizada(c_onda.codonda);
5517
5518 pk_convocacao.insereSeparacaoOnda(p_idusuario, c_onda.idonda,
5519 c_onda.codonda, p_idArmazem,
5520 c_onda.idconfiguracaoonda,
5521 c_onda.titulo || ' - Onda');
5522
5523 pk_utilities.GeraLog(p_idusuario,
5524 'MUDANÇA DE PICKING ID ' || p_idremanejamento ||
5525 ' ALTEROU ATIVIDADE DE CONVOCAÇÃO ATIVA PARA O IDONDA: ' ||
5526 c_onda.idonda ||
5527 '. ENDEREÇO DE DESTINO ANTERIOR ' ||
5528 p_idlocalorigem || ', NOVO ENDEREÇO ' ||
5529 p_idlocaldestino, p_idremanejamento, 'MP');
5530 end loop;
5531 end;
5532
5533 procedure alterarOndaCargaMudPicking
5534 (
5535 p_idusuario in number,
5536 p_iddepositante in number,
5537 p_idproduto in number,
5538 p_idArmazem in number,
5539 p_idlocalorigem in local.idlocal%type,
5540 p_idlocaldestino in local.idlocal%type,
5541 p_idremanejamento in number
5542 ) is
5543 begin
5544 for c_carga in (select re.idonda, re.idseparacaoporcarga,
5545 re.idpaleteondanf
5546 from separacaoporcarga sc, paleteondanf pf,
5547 reservaestoqueondacarga re, lote lt
5548 where lt.idproduto = p_idproduto
5549 and lt.iddepositante = p_iddepositante
5550 and lt.idlote = re.idlote
5551 and re.idlote = pf.idlote
5552 and re.idpaleteondanf = pf.idpaleteondanf
5553 and re.idseparacaoporcarga = sc.idseparacaoporcarga
5554 and sc.idarmazem = p_idArmazem
5555 and sc.idlocal = p_idlocalorigem
5556 and sc.status in (0, 1))
5557 loop
5558 update separacaoporcarga
5559 set idlocal = p_idlocaldestino
5560 where idseparacaoporcarga = c_carga.idseparacaoporcarga;
5561
5562 update paleteondanf
5563 set idlocal = p_idlocaldestino
5564 where idpaleteondanf = c_carga.idpaleteondanf;
5565
5566 update reservaestoqueondacarga
5567 set idlocal = p_idlocaldestino
5568 where idseparacaoporcarga = c_carga.idseparacaoporcarga;
5569
5570 pk_utilities.GeraLog(p_idusuario,
5571 'MUDANÇA DE PICKING ID ' || p_idremanejamento ||
5572 ' ALTEROU ONDA ' || c_carga.idonda ||
5573 ', SEPARAÇÃO CARGA ID ' ||
5574 c_carga.idseparacaoporcarga ||
5575 '. ENDEREÇO DE DESTINO ANTERIOR ' ||
5576 p_idlocalorigem || ', NOVO ENDEREÇO ' ||
5577 p_idlocaldestino, p_idremanejamento, 'MP');
5578 end loop;
5579 end;
5580
5581 procedure processarMudancaPicking
5582 (
5583 p_idusuario in number,
5584 p_iddepositante in number,
5585 p_idproduto in number,
5586 p_idArmazem in number,
5587 p_idlocalorigem in local.idlocal%type,
5588 p_idlocaldestino in local.idlocal%type
5589 ) is
5590 v_transfPendenciaAdicionar number;
5591 v_mudarPickingVazio number;
5592 v_idremanejamento number;
5593 v_textolog varchar2(100);
5594 v_estoque number;
5595 v_pendencia number;
5596 v_adicionar number;
5597 v_msg t_message;
5598
5599 procedure carregaDados is
5600 begin
5601
5602 select a.transferirpendenciaeadicionar, a.mudarpickingvazio
5603 into v_transfPendenciaAdicionar, v_mudarPickingVazio
5604 from armazem a
5605 where a.idarmazem = p_idArmazem;
5606
5607 if (v_transfPendenciaAdicionar = 1) then
5608 v_textolog := ' MUDANÇA DE PICKING MOVENDO ESTOQUE RESERVADO: ';
5609 else
5610 v_textolog := ' MUDANÇA DE PICKING: ';
5611 end if;
5612 end carregaDados;
5613
5614 procedure validacoes is
5615 begin
5616
5617 validarMudancaPicking(p_iddepositante, p_idproduto, p_idArmazem,
5618 p_idlocalorigem, v_transfPendenciaAdicionar,
5619 p_idlocaldestino, v_mudarPickingVazio);
5620
5621 end validacoes;
5622
5623 procedure iniciaMudancaDePicking is
5624
5625 procedure gravaLog(p_etapa varchar2) is
5626 begin
5627 pk_utilities.GeraLog(p_idusuario,
5628 p_etapa || v_textolog || v_idremanejamento ||
5629 ', IDPRODUTO ' || p_idproduto ||
5630 ', IDDEPOSITANTE ' || p_iddepositante ||
5631 ', LOCAL ORIGEM ' || p_idlocalorigem ||
5632 ', LOCAL DESTINO ' || p_idlocaldestino ||
5633 ', REALIZADA PELO USUARIO ' || p_idusuario,
5634 v_idremanejamento, 'MP');
5635 end gravaLog;
5636
5637 procedure transferirPendenciaPicking is
5638 begin
5639 alterarAjusteMudPicking(p_idusuario, p_iddepositante, p_idproduto,
5640 p_idArmazem, p_idlocalorigem,
5641 p_idlocaldestino, v_idremanejamento);
5642
5643 moverEstoqueMudPicking(p_idusuario, p_idArmazem, p_idlocalorigem,
5644 p_idlocaldestino, v_transfPendenciaAdicionar,
5645 v_idremanejamento);
5646
5647 alterarRemMudPicking(p_idusuario, p_iddepositante, p_idproduto,
5648 p_idArmazem, p_idlocalorigem, p_idlocaldestino,
5649 v_idremanejamento);
5650
5651 alterarRomaneioMudPicking(p_idusuario, p_iddepositante, p_idproduto,
5652 p_idArmazem, p_idlocalorigem,
5653 p_idlocaldestino, v_idremanejamento);
5654
5655 alterarAlocacaoMudPicking(p_idusuario, p_iddepositante, p_idproduto,
5656 p_idArmazem, p_idlocalorigem,
5657 p_idlocaldestino, v_idremanejamento);
5658
5659 alterarOndaMudPicking(p_idusuario, p_iddepositante, p_idproduto,
5660 p_idArmazem, p_idlocalorigem, p_idlocaldestino,
5661 v_idremanejamento);
5662
5663 alterarCorteFisicoMudPicking(p_idusuario, p_iddepositante,
5664 p_idproduto, p_idArmazem,
5665 p_idlocalorigem, p_idlocaldestino,
5666 v_idremanejamento);
5667
5668 alteraratividadesMudPicking(p_idusuario, p_iddepositante,
5669 p_idproduto, p_idArmazem,
5670 p_idlocalorigem, p_idlocaldestino,
5671 v_idremanejamento);
5672
5673 alterarOndaCargaMudPicking(p_idusuario, p_iddepositante,
5674 p_idproduto, p_idArmazem, p_idlocalorigem,
5675 p_idlocaldestino, v_idremanejamento);
5676 end transferirPendenciaPicking;
5677
5678 procedure alteraRemanejamentoGerado(p_idremanejamento number) is
5679 begin
5680
5681 pk_triggers_control.disableTrigger('T_BEFORE_REMANEJAMENTO');
5682
5683 update remanejamento r
5684 set r.planejado = 'S',
5685 r.adicionado = 'S',
5686 r.idusuario = p_idusuario,
5687 r.status = 'F',
5688 r.horafim = sysdate
5689 where r.idremanejamento = p_idremanejamento;
5690
5691 pk_triggers_control.enableTrigger('T_BEFORE_REMANEJAMENTO');
5692
5693 end alteraRemanejamentoGerado;
5694
5695 procedure verificaMovimentacaoEstoque is
5696 begin
5697 SELECT NVL(SUM(LL.ESTOQUE), 0) ESTOQUE,
5698 NVL(SUM(LL.PENDENCIA), 0) PENDENCIA,
5699 NVL(SUM(LL.ADICIONAR), 0) ADICIONAR
5700 into v_estoque, v_pendencia, v_adicionar
5701 FROM LOCAL LO, LOTE LT, LOTELOCAL LL, ENTIDADE D, PRODUTO P
5702 WHERE P.IDPRODUTO = LT.IDPRODUTO
5703 AND D.IDENTIDADE = LT.IDDEPOSITANTE
5704 AND LT.IDPRODUTO = p_idProduto
5705 AND LT.IDDEPOSITANTE = p_idDepositante
5706 AND LL.IDLOTE = LT.IDLOTE
5707 AND LL.IDLOCAL = LO.IDLOCAL
5708 AND LL.IDARMAZEM = LO.IDARMAZEM
5709 AND LO.IDARMAZEM = p_idArmazem
5710 AND LO.IDLOCAL = p_idLocalOrigem;
5711
5712 if (v_estoque > 0 or v_pendencia > 0 or v_adicionar > 0) then
5713 v_msg := t_message('Não foi possÃvel mover todo estoque do produto para novo picking.');
5714 raise_application_error(-20000, v_msg.formatMessage);
5715 end if;
5716 end verificaMovimentacaoEstoque;
5717
5718 begin
5719 gravaLog('INICIOU');
5720
5721 criarRemMudancaPicking(p_idusuario, p_iddepositante, p_idproduto,
5722 p_idArmazem, p_idlocalorigem, p_idlocaldestino,
5723 v_transfPendenciaAdicionar, v_idremanejamento);
5724
5725 if (v_transfPendenciaAdicionar = 1) then
5726 transferirPendenciaPicking;
5727 else
5728 moverEstoqueMudPicking(p_idusuario, p_idArmazem, p_idlocalorigem,
5729 p_idlocaldestino, v_transfPendenciaAdicionar,
5730 v_idremanejamento);
5731 end if;
5732
5733 validarFinalizarRemanejamento(v_idremanejamento);
5734
5735 alteraRemanejamentoGerado(v_idremanejamento);
5736
5737 verificaMovimentacaoEstoque;
5738
5739 gravaLog('FINALIZOU');
5740 end iniciaMudancaDePicking;
5741
5742 begin
5743
5744 begin
5745
5746 pk_locks.executeLock(p_idArmazem, 7);
5747
5748 carregaDados;
5749
5750 validacoes;
5751
5752 iniciaMudancaDePicking;
5753
5754 exception
5755 when others then
5756 raise_application_error(-20000, sqlerrm);
5757 end;
5758
5759 end;
5760
5761 /*
5762 * Funcao que verifica como o remanejamento sera executado
5763 */
5764 function retornarExecRemanejamento(p_idremanejamento in number)
5765 return number is
5766 v_tipoexecucao number;
5767 v_total number;
5768 begin
5769
5770 select count(*)
5771 into v_total
5772 from remanejamento r, local lo, regiaoarmazenagem ro, local ld,
5773 regiaoarmazenagem rd
5774 where r.idremanejamento = p_idremanejamento
5775 and lo.idlocal = r.idlocaldestino
5776 and lo.idarmazem = r.idarmazemdestino
5777 and ro.idregiao = lo.idregiao
5778 and ro.tipo = 1
5779 and ld.idlocal = r.idlocalorigem
5780 and ld.idarmazem = r.idarmazemorigem
5781 and rd.idregiao = ld.idregiao
5782 and rd.tipo = 1
5783 and exists
5784 (select 1
5785 from remanejamentoromaneio rr, romaneiopai rp,
5786 configuracaoonda c
5787 where rr.idremanejamento = r.idremanejamento
5788 and c.idconfiguracaoonda = rp.idconfiguracaoonda
5789 and rp.idromaneio = rr.idromaneio
5790 and (c.idregiaopruadopulmao = ld.idregiao or
5791 c.idregiaopruadopicking = ld.idregiao));
5792
5793 if v_total > 0 then
5794 -- Execucao do remanejamento deverá ser realizada em 2 etapas
5795 v_tipoexecucao := 1;
5796 else
5797 -- Execucao do remanejamento será livre
5798 v_tipoexecucao := 0;
5799 end if;
5800
5801 return v_tipoexecucao;
5802 end;
5803
5804 /*
5805 * Inclui no remanejamento os lotes componentes da montagem
5806 */
5807 procedure verificarReabPendente(p_idremanejamento in number) is
5808 begin
5809 for c in (select lr.idlote, lo.id idEnderecoOrigem
5810 from loteremanejamento lr, remanejamento r, local lo
5811 where lo.idarmazem = r.idarmazemorigem
5812 and lo.idlocal = r.idlocalorigem
5813 and r.idremanejamento = lr.idremanejamento
5814 and lr.conferido = 'N'
5815 and lr.idremanejamento = p_idremanejamento)
5816 loop
5817 insert into gtt_reabpendente
5818 select lt.idproduto, l.id idendereco, r.idremanejamento, 0 idonda,
5819 0 idmovimentacao
5820 from loteremanejamento lr, remanejamento r, lote lt, local l
5821 where lr.idlote = c.idlote
5822 and lr.idremanejamento = r.idremanejamento
5823 and r.status <> 'F'
5824 and r.idremanejamento <> p_idremanejamento
5825 and lr.idlote = lt.idlote
5826 and l.idarmazem = r.idarmazemdestino
5827 and l.idlocal = r.idlocaldestino
5828 and l.id = c.idenderecoorigem;
5829 end loop;
5830 end;
5831
5832 function getQtdeUnitEmbQueCoube
5833 (
5834 p_maxpeso in number,
5835 p_maxcubagem in number,
5836 p_idlote in number,
5837 p_qtde in number,
5838 p_remanejarPorFator in char
5839 ) return number is
5840 v_embalagem embalagem%rowtype;
5841 v_qtdePorPeso_un number;
5842 v_qtdePorCubagem_un number;
5843 v_qtdeQueCoube_un number;
5844 begin
5845 if (p_maxpeso <= 0 or p_maxcubagem <= 0) then
5846 return 0;
5847 end if;
5848
5849 select e.*
5850 into v_embalagem
5851 from lote lt, embalagem e
5852 where lt.idlote = p_idlote
5853 and e.barra = lt.barra
5854 and e.idproduto = lt.idproduto;
5855
5856 v_qtdePorPeso_un := trunc(p_maxpeso / (v_embalagem.pesobruto /
5857 v_embalagem.fatorconversao));
5858
5859 v_qtdePorCubagem_un := trunc(p_maxcubagem /
5860 ((v_embalagem.altura * v_embalagem.largura *
5861 v_embalagem.comprimento) /
5862 v_embalagem.fatorconversao));
5863
5864 if (v_qtdePorPeso_un > v_qtdePorCubagem_un) then
5865 v_qtdeQueCoube_un := v_qtdePorCubagem_un;
5866 else
5867 v_qtdeQueCoube_un := v_qtdePorPeso_un;
5868 end if;
5869
5870 if (v_qtdeQueCoube_un > p_qtde) then
5871 v_qtdeQueCoube_un := p_qtde;
5872 end if;
5873
5874 if (p_remanejarPorFator = 'S') then
5875 if (mod(v_qtdeQueCoube_un, v_embalagem.fatorconversao) <> 0 and
5876 v_qtdeQueCoube_un > v_embalagem.fatorconversao) then
5877 v_qtdeQueCoube_un := 0;
5878 end if;
5879 end if;
5880
5881 return v_qtdeQueCoube_un;
5882 end;
5883
5884 procedure planejarRemanejamentoLote
5885 (
5886 p_idusuario in number,
5887 p_idarmazem in number,
5888 p_idlocalorigem in local.idlocal%type,
5889 p_idlocaldestino in local.idlocal%type,
5890 p_idlote in number,
5891 p_qtde in number,
5892 p_descricao in varchar2,
5893 p_idromaneio in number := null,
5894 p_idnotafiscal in number := null,
5895 p_verificarMaxDimensao in char := 'N',
5896 p_remPorFatorMaxDimensao in char := 'S',
5897 p_idmovimentacao in number := null
5898 ) is
5899 type rec_remlote is record(
5900 idusuario number,
5901 idarmazem number,
5902 idlocalorigem local.idlocal%type,
5903 idlocaldestino local.idlocal%type,
5904 idlote number,
5905 qtde number,
5906 descricao varchar2(1000),
5907 idromaneio number,
5908 maxpeso number,
5909 maxcubagem number,
5910 verificarMaxDimensao char(1),
5911 remanejarPorFator char(1));
5912
5913 r_remlote rec_remlote;
5914 v_qtdedistribuir number;
5915 r_remAberto remanejamento%rowtype;
5916 v_isOrigemBuffer number;
5917 v_msg t_message;
5918
5919 procedure getMaxDimensaoLocal(r_remlote in out rec_remlote) is
5920 begin
5921 select (lo.altura - lo.alturamanobra) * lo.largura * lo.comprimento maxcubagem,
5922 lo.pesomaximo maxpeso
5923 into r_remlote.maxcubagem, r_remlote.maxpeso
5924 from local lo
5925 where lo.idarmazem = r_remlote.idarmazem
5926 and lo.idlocal = r_remlote.idlocaldestino;
5927 end;
5928
5929 procedure inserirLoteRemanejamento
5930 (
5931 r_remlote in rec_remlote,
5932 p_qtde in out number,
5933 p_idremanejamento in number
5934 ) is
5935 begin
5936 begin
5937 insert into loteremanejamento
5938 (idremanejamento, idlote, qtde, conferido, idromaneio)
5939 values
5940 (p_idremanejamento, r_remlote.idlote, p_qtde, 'N',
5941 r_remlote.idromaneio);
5942
5943 Associar(p_idremanejamento, r_remlote.idlote);
5944
5945 exception
5946 when dup_val_on_index then
5947 update loteremanejamento
5948 set qtde = qtde + p_qtde
5949 where idremanejamento = p_idremanejamento
5950 and idlote = r_remlote.idlote;
5951 end;
5952
5953 begin
5954 if (r_remlote.idromaneio is not null) then
5955 insert into remanejamentoromaneio
5956 (idremanejamento, idromaneio, idlote, qtde, data)
5957 values
5958 (p_idremanejamento, r_remlote.idromaneio, r_remlote.idlote,
5959 p_qtde, sysdate);
5960 end if;
5961 exception
5962 when dup_val_on_index then
5963 update remanejamentoromaneio rr
5964 set rr.qtde = rr.qtde + p_qtde,
5965 rr.data = sysdate
5966 where idremanejamento = p_idremanejamento
5967 and idromaneio = r_remlote.idromaneio
5968 and idlote = r_remlote.idlote;
5969 end;
5970
5971 end;
5972
5973 procedure usarRemanejamentoAberto
5974 (
5975 r_remlote in out rec_remlote,
5976 p_qtdedistribuir in out number
5977 ) is
5978 v_pesoocupado number;
5979 v_cubagemocupada number;
5980 v_qtdeQueCoube number;
5981 c_remAberto cursor_remanejamento;
5982 r_remAberto remanejamento%rowtype;
5983 begin
5984 getRemAberto(c_remAberto, r_remAberto, r_remlote.idarmazem,
5985 r_remlote.idlocalorigem, r_remlote.idlocaldestino);
5986 if (c_remAberto%found) then
5987 while (c_remAberto%found)
5988 loop
5989 if (r_remlote.verificarMaxDimensao = 'N') then
5990 inserirLoteRemanejamento(r_remlote, p_qtdedistribuir,
5991 r_remAberto.Idremanejamento);
5992
5993 p_qtdedistribuir := 0;
5994 else
5995 begin
5996 select nvl(sum(lr.qtde * (e.pesobruto / e.fatorconversao)), 0),
5997 nvl(sum(lr.qtde * ((e.altura * e.largura *
5998 e.comprimento) / e.fatorconversao)), 0)
5999 into v_pesoocupado, v_cubagemocupada
6000 from loteremanejamento lr, lote lt, embalagem e
6001 where lr.idremanejamento = r_remAberto.idremanejamento
6002 and lt.idlote = lr.idlote
6003 and e.idproduto = lt.idproduto
6004 and e.barra = lt.barra;
6005 exception
6006 when no_data_found then
6007 v_pesoocupado := 0;
6008 v_cubagemocupada := 0;
6009 end;
6010
6011 v_qtdeQueCoube := getQtdeUnitEmbQueCoube((r_remlote.maxpeso -
6012 v_pesoocupado),
6013 (r_remlote.maxcubagem -
6014 v_cubagemocupada),
6015 r_remlote.idlote,
6016 p_qtdedistribuir,
6017 r_remlote.remanejarPorFator);
6018
6019 if (v_qtdeQueCoube > 0) then
6020 if (v_qtdeQueCoube >= p_qtdedistribuir) then
6021 inserirLoteRemanejamento(r_remlote, p_qtdedistribuir,
6022 r_remAberto.Idremanejamento);
6023
6024 p_qtdedistribuir := 0;
6025 else
6026 inserirLoteRemanejamento(r_remlote, v_qtdeQueCoube,
6027 r_remAberto.Idremanejamento);
6028
6029 insert into gtt_remanejamentogerado
6030 (idremanejamento, idlote, qtdeunit)
6031 values
6032 (r_remAberto.Idremanejamento, r_remlote.idlote,
6033 v_qtdeQueCoube);
6034
6035 p_qtdedistribuir := p_qtdedistribuir - v_qtdeQueCoube;
6036 end if;
6037 end if;
6038 end if;
6039
6040 exit when p_qtdedistribuir = 0;
6041
6042 fetch c_remAberto
6043 into r_remAberto;
6044 end loop;
6045 end if;
6046 close c_remAberto;
6047 end;
6048
6049 procedure verificaLocalOrigemDestino
6050 (
6051 r_remlote in rec_remlote,
6052 p_idnotafiscal in number
6053 ) is
6054 v_regiaoorigem number;
6055 v_regiaodestino number;
6056 v_idproduto produto.idproduto%type;
6057 v_codigointerno produto.codigointerno%type;
6058 v_idnotafiscal notafiscal.idnotafiscal%type;
6059 v_codigointernonf notafiscal.codigointerno%type;
6060 v_sequencia notafiscal.sequencia%type;
6061 v_numpedidofornecedor notafiscal.numpedidofornecedor%type;
6062 begin
6063 select count(1)
6064 into v_regiaoorigem
6065 from regiaoarmazenagem r, local lo
6066 where lo.idregiao = r.idregiao
6067 and lo.idlocal = r_remlote.idlocalorigem
6068 and lo.idarmazem = r_remlote.idarmazem;
6069
6070 if (v_regiaoorigem = 0) then
6071
6072 select p.idproduto, p.codigointerno
6073 into v_idproduto, v_codigointerno
6074 from produto p, lote lt
6075 where lt.idproduto = p.idproduto
6076 and lt.idlote = r_remlote.idlote;
6077
6078 if p_idnotafiscal is not null then
6079 select nf.idnotafiscal, nf.codigointerno, nf.sequencia,
6080 nf.numpedidofornecedor
6081 into v_idnotafiscal, v_codigointernonf, v_sequencia,
6082 v_numpedidofornecedor
6083 from notafiscal nf
6084 where nf.idnotafiscal = p_idnotafiscal;
6085
6086 v_msg := t_message('O Local ({0}' ||
6087 ') de origem não possui região definida.' ||
6088 chr(13) || ' Idproduto: {1}' ||
6089 ', Código Produto: {2}' || chr(13) ||
6090 'Idnotafiscal: {3}, Nota Fiscal: {4}' ||
6091 ', Série: {5}, Pedido: {6}.' || chr(13) ||
6092 'Operação cancelada.');
6093 v_msg.addParam(r_remlote.idlocalorigem);
6094 v_msg.addParam(v_idproduto);
6095 v_msg.addParam(v_codigointerno);
6096 v_msg.addParam(v_idnotafiscal);
6097 v_msg.addParam(v_codigointernonf);
6098 v_msg.addParam(v_sequencia);
6099 v_msg.addParam(v_numpedidofornecedor);
6100 raise_application_error(-20100, v_msg.formatMessage);
6101 else
6102 v_msg := t_message('O Local ({0}' ||
6103 ') de origem não possui região definida. ' ||
6104 chr(13) || ' Idproduto: {1}' ||
6105 ', Código interno: {2}' || chr(13) ||
6106 '.Operação cancelada.');
6107 v_msg.addParam(r_remlote.idlocalorigem);
6108 v_msg.addParam(v_idproduto);
6109 v_msg.addParam(v_codigointerno);
6110 raise_application_error(-20100, v_msg.formatMessage);
6111 end if;
6112
6113 end if;
6114
6115 select count(1)
6116 into v_regiaodestino
6117 from regiaoarmazenagem r, local ld
6118 where ld.idregiao = r.idregiao
6119 and ld.idlocal = r_remlote.idlocaldestino
6120 and ld.idarmazem = r_remlote.idarmazem;
6121
6122 if (v_regiaodestino = 0) then
6123
6124 v_idproduto := null;
6125 v_codigointerno := null;
6126 v_idnotafiscal := null;
6127 v_codigointernonf := null;
6128 v_sequencia := null;
6129 v_numpedidofornecedor := null;
6130
6131 select p.idproduto, p.codigointerno
6132 into v_idproduto, v_codigointerno
6133 from produto p, lote lt
6134 where lt.idproduto = p.idproduto
6135 and lt.idlote = r_remlote.idlote;
6136
6137 select nf.idnotafiscal, nf.codigointerno, nf.sequencia,
6138 nf.numpedidofornecedor
6139 into v_idnotafiscal, v_codigointernonf, v_sequencia,
6140 v_numpedidofornecedor
6141 from notafiscal nf
6142 where nf.idnotafiscal = p_idnotafiscal;
6143
6144 v_msg := t_message('O Local ({0}' ||
6145 ') de destino não possui região definida. ' ||
6146 chr(13) || ' Idproduto: {1}' ||
6147 ', Código Produto: {2}' || chr(13) ||
6148 'Idnotafiscal: {3}, Nota Fiscal: ' ||
6149 '{4}, Série: {5}, Pedido: {6}.' || chr(13) ||
6150 'Operação cancelada.');
6151 v_msg.addParam(r_remlote.idlocaldestino);
6152 v_msg.addParam(v_idproduto);
6153 v_msg.addParam(v_codigointerno);
6154 v_msg.addParam(v_idnotafiscal);
6155 v_msg.addParam(v_codigointernonf);
6156 v_msg.addParam(v_sequencia);
6157 v_msg.addParam(v_numpedidofornecedor);
6158 raise_application_error(-20100, v_msg.formatMessage);
6159
6160 end if;
6161
6162 end;
6163
6164 procedure usarNovoRemanejamento
6165 (
6166 r_remlote in out rec_remlote,
6167 p_qtdedistribuir in out number,
6168 p_idnotafiscal in number
6169 ) is
6170 v_idremanejamento number;
6171 v_qtdeQueCoube number;
6172 v_caixaMovimentacao number;
6173 v_status char(1);
6174
6175 function isCaixaMovimentacao
6176 (
6177 p_idArmazem number,
6178 p_idLocal varchar2
6179 ) return boolean is
6180 v_caixaMovimentacao number;
6181 begin
6182 select count(1) total
6183 into v_caixaMovimentacao
6184 from local l, setor s
6185 where s.idsetor = l.idsetor
6186 and l.tipo = 1
6187 and l.idarmazem = p_idarmazem
6188 and s.usoexclusivocxmov = 1
6189 and l.idlocal = p_idLocal;
6190 return v_caixaMovimentacao > 0;
6191 end;
6192
6193 begin
6194 if (p_qtdedistribuir <= 0) then
6195 return;
6196 end if;
6197
6198 verificaLocalOrigemDestino(r_remlote, p_idnotafiscal);
6199
6200 v_status := c_aguardando;
6201
6202 if (isCaixaMovimentacao(r_remlote.idarmazem, r_remlote.idlocalorigem)) then
6203 v_status := 'G';
6204 end if;
6205
6206 while (p_qtdedistribuir > 0)
6207 loop
6208 select seq_remanejamento.nextval idremanejamento
6209 into v_idremanejamento
6210 from dual;
6211
6212 insert into remanejamento
6213 (idremanejamento, idarmazemorigem, idlocalorigem,
6214 idarmazemdestino, idlocaldestino, datahora, idusuariotela, status,
6215 cadmanual, idromaneio, descrpalet, planejado, idmovimentacao)
6216 values
6217 (v_idremanejamento, r_remlote.idarmazem, r_remlote.idlocalorigem,
6218 r_remlote.idarmazem, r_remlote.idlocaldestino, sysdate,
6219 r_remlote.idusuario, v_status, c_nao, r_remlote.idromaneio,
6220 r_remlote.descricao, 'S', p_idmovimentacao);
6221
6222 if (r_remlote.verificarMaxDimensao = 'N') then
6223 v_qtdeQueCoube := p_qtdedistribuir;
6224 else
6225 v_qtdeQueCoube := getQtdeUnitEmbQueCoube(r_remlote.maxpeso,
6226 r_remlote.maxcubagem,
6227 r_remlote.idlote,
6228 p_qtdedistribuir,
6229 r_remlote.remanejarPorFator);
6230
6231 if (v_qtdeQueCoube = 0) then
6232 v_msg := t_message('O endereço {0}' ||
6233 ' não possui capacidade fÃsica para armazenar uma única embalagem do lote ' ||
6234 '{1}. Operação cancelada.');
6235 v_msg.addParam(r_remlote.idlocaldestino);
6236 v_msg.addParam(r_remlote.idlote);
6237 raise_application_error(-20000, v_msg.formatMessage);
6238 end if;
6239 end if;
6240
6241 inserirLoteRemanejamento(r_remlote, v_qtdeQueCoube,
6242 v_idremanejamento);
6243
6244 insert into gtt_remanejamentogerado
6245 (idremanejamento, idlote, qtdeunit)
6246 values
6247 (v_idremanejamento, r_remlote.idlote, v_qtdeQueCoube);
6248
6249 p_qtdedistribuir := p_qtdedistribuir - v_qtdeQueCoube;
6250 end loop;
6251 end;
6252 begin
6253
6254 select count(*)
6255 into v_isOrigemBuffer
6256 from dual
6257 where exists (select 1
6258 from local lo
6259 where lo.idlocal = p_idlocalorigem
6260 and lo.picking = 'S'
6261 and lo.buffer = 'S');
6262
6263 r_remlote.idusuario := p_idusuario;
6264 r_remlote.idarmazem := p_idarmazem;
6265 r_remlote.idlocalorigem := p_idlocalorigem;
6266 r_remlote.idlocaldestino := p_idlocaldestino;
6267 r_remlote.idlote := p_idlote;
6268 r_remlote.qtde := p_qtde;
6269 r_remlote.descricao := p_descricao;
6270 r_remlote.idromaneio := p_idromaneio;
6271 r_remlote.verificarMaxDimensao := p_verificarMaxDimensao;
6272 r_remlote.remanejarPorFator := p_remPorFatorMaxDimensao;
6273
6274 v_qtdedistribuir := r_remlote.qtde;
6275 if (r_remlote.verificarMaxDimensao = 'S') then
6276 getMaxDimensaoLocal(r_remlote);
6277 end if;
6278
6279 if nvl(v_isOrigemBuffer, 0) > 0 then
6280 usarNovoRemanejamento(r_remlote, v_qtdedistribuir, p_idnotafiscal);
6281 else
6282 usarRemanejamentoAberto(r_remlote, v_qtdedistribuir);
6283 usarNovoRemanejamento(r_remlote, v_qtdedistribuir, p_idnotafiscal);
6284 end if;
6285 end;
6286
6287 function retornarTarefa
6288 (
6289 p_idremanejamento in number,
6290 p_idendereco in number
6291 ) return varchar2 is
6292 v_tarefa varchar2(4000);
6293 begin
6294 select stragg(codbarratarefa)
6295 into v_tarefa
6296 from (select distinct v.codbarratarefa
6297 from remanejamentoromaneio rr, v_tarefas_onda v
6298 where rr.idremanejamento = p_idremanejamento
6299 and v.idonda = rr.idromaneio
6300 and v.idlote = rr.idlote
6301 and v.idlocalorigem = p_idendereco
6302 and v.status < 3);
6303
6304 return v_tarefa;
6305 exception
6306 when no_data_found then
6307 return null;
6308 end;
6309
6310 function retornarQtdeRemPorTarefa(p_codtarefa in varchar2) return number is
6311 v_qtde number;
6312 begin
6313
6314 select count(distinct r.idremanejamento)
6315 into v_qtde
6316 from movimentacao m, loteremanejamento lr, remanejamento r, local l
6317 where lpad(m.idonda, 10, '0') || lpad(m.identificador, 4, '0') =
6318 p_codtarefa
6319 and m.etapa = 1
6320 and lr.idlote = m.idlote
6321 and r.idremanejamento = lr.idremanejamento
6322 and r.status <> 'F'
6323 and (exists (select 1
6324 from remanejamentoromaneio rr
6325 where rr.idremanejamento = r.idremanejamento
6326 and rr.idlote = lr.idlote
6327 and rr.idromaneio <= m.idonda) or
6328 (r.cadmanual = 'N' and r.idromaneio is null and
6329 upper(r.descrpalet) like '%REABASTECIMENTO PARA ONDA%'))
6330 and l.idlocal = r.idlocaldestino
6331 and l.tipo = 0
6332 and l.buffer = 'N';
6333
6334 return v_qtde;
6335
6336 end;
6337
6338 function retornarRemPorTarefa(p_codtarefa in varchar2) return varchar2 is
6339 v_rem varchar2(4000);
6340 begin
6341 select stragg(idremanejamento)
6342 into v_rem
6343 from (select distinct rr.idremanejamento
6344 from v_tarefas_onda v, remanejamentoromaneio rr,
6345 remanejamento r
6346 where v.codbarratarefa = p_codtarefa
6347 and v.status < 3
6348 and rr.idromaneio = v.idonda
6349 and rr.idlote = v.idlote
6350 and r.idremanejamento = rr.idremanejamento
6351 and r.status <> 'F'
6352 order by rr.idremanejamento);
6353
6354 return v_rem;
6355 exception
6356 when no_data_found then
6357 return null;
6358 end;
6359
6360 procedure validarBufPickEsteira
6361 (
6362 p_idRemanejamento in number,
6363 p_telaExecRemanej in number default 0
6364 ) is
6365
6366 C_DESTINOINICIADO CONSTANT CHAR(1) := 'Z';
6367 v_naoFinalizar number;
6368 v_msg t_message;
6369
6370 function isRemanejamentoBufPickEsteira(p_idRemanejamento in number)
6371 return boolean is
6372 v_resultado number;
6373 begin
6374 select count(*)
6375 into v_resultado
6376 from dual
6377 where exists (select 1
6378 from remanejamento r, local lo, local ld, setor sd
6379 where r.idremanejamento = p_idRemanejamento
6380 and r.idarmazemorigem = lo.idarmazem
6381 and r.idlocalorigem = lo.idlocal
6382 and lo.tipo = 0
6383 and lo.buffer = 'S'
6384 and lo.bufferesteira = 1
6385 and r.idlocaldestino = ld.idlocal
6386 and ld.tipo = 0
6387 and ld.buffer = 'N'
6388 and ld.idsetor = sd.idsetor
6389 and sd.codintegracaoesteira is not null
6390 and r.planejado = 'S'
6391 and r.cadmanual = 'N');
6392 return v_resultado > 0;
6393 end;
6394
6395 begin
6396
6397 if isRemanejamentoBufPickEsteira(p_idRemanejamento) then
6398 if nvl(p_telaExecRemanej, 0) in
6399 (C_REMANEJ_WEB, C_REMANEJ_MANUAL_COLETOR, C_REMANEJ_ORIGEM,
6400 C_REMANEJ_PLAN_COLETOR) then
6401 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.');
6402 raise_application_error(-20000, v_msg.formatMessage);
6403 elsif nvl(p_telaExecRemanej, 0) = C_REMANEJ_DESTINO then
6404 select count(1)
6405 into v_naoFinalizar
6406 from remanejamento rem, local ld, setor s
6407 where ld.idarmazem = rem.idarmazemdestino
6408 and ld.idlocal = rem.idlocaldestino
6409 and ld.tipo = 0
6410 and ld.buffer = 'N'
6411 and ld.idsetor = s.idsetor
6412 and s.codintegracaoesteira is not null
6413 and rem.idremanejamento = p_idRemanejamento
6414 and rem.status <> C_DESTINOINICIADO;
6415
6416 if (v_naoFinalizar > 0) then
6417 v_msg := t_message('Não é possÃvel executar esse remanejamento. Verifique se o mesmo já teve sua origem realizada.');
6418 raise_application_error(-20000, v_msg.formatMessage);
6419 end if;
6420 end if;
6421 end if;
6422 end;
6423
6424 procedure finalizarRemanejamento
6425 (
6426 p_idremanejamento in number,
6427 p_idusuario in number,
6428 p_telaExecFinalizacao in number default 0
6429 ) is
6430 type t_rem is record(
6431 status remanejamento.status%type,
6432 planejado remanejamento.planejado%type,
6433 idlocalorigem remanejamento.idlocalorigem%type,
6434 idarmazemorigem number,
6435 idremanejamento number,
6436 idlocaldestino remanejamento.idlocaldestino%type,
6437 idarmazemdestino number,
6438 idcontroleavaria number,
6439 idinsucessoentrega number,
6440 idprodutorecuperado number,
6441 tipoexportacaoprodrecuperado number,
6442 usoexclusivocxmov number);
6443
6444 r_remanejamento t_rem;
6445
6446 v_idendereco number;
6447 v_picking number;
6448 v_qtde_lote number;
6449 v_tipoLocalOrigem number;
6450 v_LocalOrigemBuffer varchar2(1);
6451 v_existeCRN number;
6452 v_msg varchar(2000) := '';
6453 v_erro boolean := false;
6454 v_remDePlParaPkEsteira boolean := false;
6455 v_fatorConversao number;
6456 v_caixaFechada varchar2(1);
6457 v_produto produto.codigointerno%type;
6458 v_msgRaise t_message;
6459
6460 v_tipoPermPkSetorOrigem number;
6461 v_tipoPermPkSetorDestino number;
6462 v_barraUnitaria embalagem.barra%type;
6463 v_qtdeUN number;
6464 v_fatorConversaoLote embalagem.fatorconversao%type;
6465 v_qtdeTotalLote lote.qtdedisponivel%type;
6466 r_lote lote%rowtype;
6467 v_idlote number;
6468 v_complementar lotelocal.complementar%type;
6469 v_qtdeUtilizadaUN number;
6470 v_qtdeCobertaLoteAntigo number;
6471 v_complementar varchar2(200);
6472
6473 procedure regraRemanejamentoLoteUnico(p_idremanejamento in number) is
6474 r_loteunico pk_lote.t_loteunico;
6475 r_loteunicoComp pk_lote.t_loteunico;
6476 r_loteunicoAvaliar pk_lote.t_loteunico;
6477 v_retorno varchar2(500);
6478 begin
6479 for r_loteunico in (select distinct lt.idproduto, lt.estado,
6480 lt.descr loteindustria,
6481 lt.dtvenc dtvencimento,
6482 pd.loteuniconoendereco,
6483 lt.iddepositante
6484 from loteremanejamento lr, lote lt,
6485 produtodepositante pd
6486 where lr.idremanejamento = p_idremanejamento
6487 and lr.idlote = lt.idlote
6488 and lt.idlote = lr.idlote
6489 and pd.idproduto = lt.idproduto
6490 and pd.identidade = lt.iddepositante)
6491 loop
6492
6493 r_loteunicoAvaliar.idProduto := r_loteunico.idproduto;
6494 r_loteunicoAvaliar.estado := r_loteunico.estado;
6495 r_loteunicoAvaliar.loteindustria := r_loteunico.loteindustria;
6496 r_loteunicoAvaliar.dtvencimento := r_loteunico.dtvencimento;
6497 r_loteunicoAvaliar.loteuniconoendereco := r_loteunico.loteuniconoendereco;
6498 r_loteunicoAvaliar.iddepositante := r_loteunico.iddepositante;
6499
6500 for r_loteunicoComp in (select distinct lt.idproduto, lt.estado,
6501 lt.descr loteindustria,
6502 lt.dtvenc dtvencimento,
6503 pd.loteuniconoendereco,
6504 lt.iddepositante
6505 from loteremanejamento lr, lote lt,
6506 produtodepositante pd
6507 where lr.idremanejamento =
6508 p_idremanejamento
6509 and lr.idlote = lt.idlote
6510 and lt.idlote = lr.idlote
6511 and pd.idproduto = lt.idproduto
6512 and pd.identidade = lt.iddepositante
6513 and (lt.idproduto <>
6514 r_loteunico.idproduto or
6515 lt.estado <> r_loteunico.estado or
6516 lt.descr <>
6517 r_loteunico.loteindustria or
6518 lt.dtvenc <>
6519 r_loteunico.dtvencimento or
6520 pd.loteuniconoendereco <>
6521 r_loteunico.loteuniconoendereco))
6522 loop
6523 if not pk_lote.isLoteUnicoCompativel(r_loteunico, r_loteunicoComp,
6524 v_retorno) then
6525 raise_application_error(-20000, v_retorno);
6526 end if;
6527 end loop;
6528 end loop;
6529
6530 if not pk_lote.isLocalPodeReceberLoteUnico(r_loteunicoAvaliar,
6531 r_remanejamento.idarmazemdestino,
6532 r_remanejamento.idlocaldestino,
6533 v_retorno) then
6534 raise_application_error(-20000, v_retorno);
6535 end if;
6536 end;
6537
6538 function isRemPulmParaPickComEsteira
6539 (
6540 p_idremanejamento in number,
6541 p_idArmazem in number,
6542 p_idlocalorigem in varchar2,
6543 p_idlocaldestino in varchar2
6544 ) return boolean is
6545
6546 v_isPulmParaPickEsteira number;
6547
6548 begin
6549 -- Verificar se é fluxo de origem pulmão sem esteira para destino picking com esteira
6550 select count(l.idlocal)
6551 into v_isPulmParaPickEsteira
6552 from remanejamento r, local l, setor s
6553 where r.idremanejamento = p_idremanejamento
6554 and s.idsetor = l.idsetor
6555 and ((l.idlocal = p_idlocalorigem and l.idarmazem = p_idArmazem and
6556 l.tipo in (1, 2) and
6557 length(trim(s.codintegracaoesteira)) is null) or
6558 (l.idlocal = p_idlocaldestino and l.idarmazem = p_idArmazem and
6559 l.tipo = 0 and
6560 length(trim(s.codintegracaoesteira)) is not null));
6561
6562 if (v_isPulmParaPickEsteira = 2) then
6563 return true;
6564 else
6565 return false;
6566 end if;
6567
6568 exception
6569 when no_data_found then
6570 return false;
6571
6572 end isRemPulmParaPickComEsteira;
6573
6574 procedure execOrigemRemBufferPK is
6575 begin
6576 for c_remanejamentoFilho in (select r.idRemanejamento
6577 from remanejamento r
6578 where r.idremanejamentopai =
6579 p_idremanejamento
6580 and r.status = 'A'
6581 and exists (select 1
6582 from local lo
6583 where lo.idarmazem =
6584 r.idarmazemorigem
6585 and lo.idlocal =
6586 r.idlocalorigem
6587 and lo.tipo = 0
6588 and lo.buffer = 'S'
6589 and lo.bufferesteira = 1)
6590 and exists
6591 (select 1
6592 from local ld, setor sd
6593 where sd.idsetor = ld.idsetor
6594 and ld.idarmazem =
6595 r.idarmazemdestino
6596 and ld.idlocal =
6597 r.idlocaldestino
6598 and ld.tipo = 0
6599 and ld.buffer = 'N'
6600 and nvl(length(sd.codintegracaoesteira),
6601 0) > 0))
6602 loop
6603 update loteremanejamento lr
6604 set lr.conferido = 'S'
6605 where lr.idremanejamento = c_remanejamentoFilho.idRemanejamento
6606 and lr.conferido = 'N';
6607
6608 update remanejamento rem
6609 set rem.status = 'G',
6610 rem.idusuarioinicio = p_idusuario,
6611 rem.horafimorigem = sysdate
6612 where rem.Idremanejamento = c_remanejamentoFilho.idRemanejamento;
6613
6614 integrarEsteira(c_remanejamentoFilho.idRemanejamento, p_idusuario);
6615 end loop;
6616 end;
6617 begin
6618 select r.status, r.planejado, r.idlocalorigem, r.idarmazemorigem,
6619 r.idremanejamento, r.idlocaldestino, r.idarmazemdestino,
6620 r.idcontroleavaria, r.idinsucessoentrega, pr.idprodutorecuperado,
6621 d.tipoexportacaoprodrecuperado,
6622 nvl(s.usoexclusivocxmov, 0) usoexclusivocxmov
6623 into r_remanejamento.status, r_remanejamento.planejado,
6624 r_remanejamento.idlocalorigem, r_remanejamento.idarmazemorigem,
6625 r_remanejamento.idremanejamento, r_remanejamento.idlocaldestino,
6626 r_remanejamento.idarmazemdestino,
6627 r_remanejamento.idcontroleavaria,
6628 r_remanejamento.idinsucessoentrega,
6629 r_remanejamento.idprodutorecuperado,
6630 r_remanejamento.tipoexportacaoprodrecuperado,
6631 r_remanejamento.usoexclusivocxmov
6632 from remanejamento r, produtorecuperado pr, depositante d, local l,
6633 setor s
6634 where r.idremanejamento = p_idremanejamento
6635 and r.idlocalorigem = l.idlocal
6636 and r.idarmazemorigem = l.idarmazem
6637 and l.idsetor = s.idsetor(+)
6638 and pr.idremanejamento(+) = r.idremanejamento
6639 and d.identidade(+) = pr.iddepositante
6640 for update;
6641
6642 if (r_remanejamento.status = 'F') then
6643 v_msgRaise := t_message('REMANEJAMENTO JA ESTA FINALIZADO. OPERAÇÃO CANCELADA.');
6644 raise_application_error(-20000, v_msgRaise.formatMessage);
6645 end if;
6646
6647 if r_remanejamento.planejado = 'S' then
6648 if (r_remanejamento.status in ('A', 'Z', 'G')) then
6649
6650 select count(*)
6651 into v_qtde_lote
6652 from loteremanejamento lr
6653 where lr.idremanejamento = p_idremanejamento;
6654
6655 if v_qtde_lote = 0 then
6656 v_msgRaise := t_message('REMANEJAMENTO SEM LOTES. OPERAÇÃO CANCELADA.');
6657 raise_application_error(-20000, v_msgRaise.formatMessage);
6658 end if;
6659
6660 regraRemanejamentoLoteUnico(p_idremanejamento);
6661
6662 v_remDePlParaPkEsteira := isRemPulmParaPickComEsteira(p_idremanejamento,
6663 r_remanejamento.idarmazemorigem,
6664 r_remanejamento.idlocalorigem,
6665 r_remanejamento.idlocaldestino);
6666
6667 select lo.tipo, lo.buffer,
6668 nvl(so.tipopermitirpickingsetor, 0) tipopermitirpickingsetor,
6669 nvl(sd.tipopermitirpickingsetor, 0) tipopermitirpickingsetor
6670 into v_tipoLocalOrigem, v_LocalOrigemBuffer,
6671 v_tipoPermPkSetorOrigem, v_tipoPermPkSetorDestino
6672 from local lo, setor so, local ld, setor sd
6673 where lo.idlocal = r_remanejamento.idlocalorigem
6674 and lo.idarmazem = r_remanejamento.idarmazemorigem
6675 and so.idsetor(+) = lo.idsetor
6676 and ld.idlocal = r_remanejamento.idlocaldestino
6677 and ld.idarmazem = r_remanejamento.idarmazemdestino
6678 and sd.idsetor(+) = ld.idsetor;
6679
6680 if ((v_tipoLocalOrigem = C_LOCAL_PICKING) and
6681 (v_tipoPermPkSetorOrigem = C_SETOR_CAIXAS and
6682 v_tipoPermPkSetorDestino = C_SETOR_UNIDADES)) then
6683
6684 for c_loteRemanejamento in (select lr.qtde, lr.idlote
6685 from loteremanejamento lr
6686 where lr.idremanejamento =
6687 r_remanejamento.idremanejamento)
6688 loop
6689
6690 delete from gtt_formarloteremanejamento;
6691
6692 select e.fatorconversao, lt.qtdedisponivel
6693 into v_fatorConversaoLote, v_qtdeTotalLote
6694 from lote lt, embalagem e
6695 where lt.idlote = c_loteRemanejamento.Idlote
6696 and e.idproduto = lt.idproduto
6697 and e.barra = lt.barra;
6698
6699 if (verificaCompOrigemDestino(v_qtdeTotalLote,
6700 v_fatorConversaoLote,
6701 c_loteRemanejamento.Qtde)) then
6702 begin
6703 select e.barra
6704 into v_barraUnitaria
6705 from lote lt, embalagem e
6706 where lt.idlote = c_loteRemanejamento.Idlote
6707 and e.idproduto = lt.idproduto
6708 and e.fatorconversao = 1
6709 and e.caixafechada = 'N'
6710 and e.ativo = 'S'
6711 and e.precadastro = 'N'
6712 and rownum = 1;
6713 exception
6714 when no_data_found then
6715 v_msgRaise := t_message('Não foi encontrada barra de unidade. Operação não permitida.');
6716 raise_application_error(-20000, v_msgRaise.formatMessage);
6717 end;
6718
6719 insert into gtt_formarloteremanejamento
6720 (idremanejamento, iddepositante, idproduto, estado, barra,
6721 qtde, itemadicional, peso, loteindustria, loteadicional,
6722 dtvenc, dtfabricacao, idloteinfoespec, liberado)
6723 select lr.idremanejamento, lt.iddepositante, lt.idproduto,
6724 lt.estado, v_barraUnitaria, lr.qtde, lt.itemadicional,
6725 (e.pesobruto * lr.qtde) peso, lt.descr,
6726 lt.loteadicional, lt.dtvenc, lt.dtfabricacao,
6727 decode(pk_lote.retinformacoesespecifica(lt.idlote),
6728 'SEM INFORMAÇÃO', null) idloteinfoespec,
6729 lt.liberado
6730 from loteremanejamento lr, lote lt, embalagem e
6731 where lr.idremanejamento = r_remanejamento.idremanejamento
6732 and lr.idlote = c_loteRemanejamento.Idlote
6733 and lt.idlote = lr.idlote
6734 and e.idproduto = lt.idproduto
6735 and e.barra = v_barraUnitaria;
6736
6737 for c_novoLote in (select g.idproduto, g.estado,
6738 g.iddepositante, g.itemadicional,
6739 g.loteindustria, g.dtvenc,
6740 g.dtfabricacao, g.qtde,
6741 e.fatorconversao, e.lastro,
6742 e.qtdecamada, g.idtipocaixa,
6743 g.loteadicional, g.idloteinfoespec,
6744 g.peso, g.barra,
6745 nvl(g.liberado, 'S') liberado
6746 from gtt_formarloteremanejamento g,
6747 embalagem e
6748 where e.barra = g.barra
6749 and e.idproduto = g.idproduto)
6750 loop
6751
6752 v_qtdeUN := (c_novoLote.qtde * c_novoLote.fatorconversao);
6753
6754 -- Informando os dados do Novo Lote Unitário
6755 r_lote.idproduto := c_novoLote.idproduto;
6756 r_lote.iddepositante := c_novoLote.iddepositante;
6757 r_lote.descr := c_novoLote.loteindustria;
6758 r_lote.dtVenc := c_novoLote.dtvenc;
6759 r_lote.estado := c_novoLote.estado;
6760 r_lote.idUsuario := p_idusuario;
6761 r_lote.tipolote := 'L';
6762 r_lote.situacao := 'R';
6763 r_lote.numerovolume := 1;
6764 r_lote.itemadicional := c_novoLote.itemadicional;
6765 r_lote.idtipocaixa := c_novoLote.idtipocaixa;
6766 r_lote.idarmazem := r_remanejamento.idarmazemorigem;
6767 r_lote.dtfabricacao := c_novoLote.dtfabricacao;
6768 r_lote.loteadicional := c_novoLote.loteadicional;
6769 r_lote.barra := c_novoLote.barra;
6770 r_lote.qtdeEntrada := v_qtdeUN;
6771 r_lote.tipoPalet := 'U';
6772
6773 if (pk_lote.isLoteIndustriaComMaisDeUmVenc(c_novoLote.Iddepositante,
6774 c_novoLote.idproduto,
6775 c_novoLote.loteindustria,
6776 c_novoLote.dtvenc)) then
6777 raise_application_error(-20000,
6778 'Não é possÃvel cadastrar lote do mesmo produto/depositante com o mesmo lote indústria e data de vencimento diferentes. Depositante:' ||
6779 c_novoLote.Iddepositante ||
6780 '. Produto: ' ||
6781 c_novoLote.idproduto ||
6782 ' Lote Indústria: ' ||
6783 c_novoLote.loteindustria ||
6784 ' Data Venc: ' ||
6785 c_novoLote.dtvenc);
6786
6787 end if;
6788
6789 v_idlote := pk_lote.cadastrar_lote(r_lote, null, 'N');
6790
6791 if (c_novoLote.liberado = 'N') then
6792 update lote lt
6793 set lt.liberado = 'N'
6794 where lt.idlote = v_idlote;
6795 end if;
6796
6797 if (c_novoLote.peso is not null) then
6798 update lote lt
6799 set lt.pesobruto = c_novoLote.peso
6800 where lt.idlote = v_idlote;
6801 end if;
6802
6803 pk_estoque.incluir_estoque(r_remanejamento.idarmazemdestino,
6804 r_remanejamento.idlocaldestino,
6805 v_idlote, r_lote.qtdeEntrada,
6806 p_idusuario,
6807 'ESTOQUE ADICIONADO EM FUNCAO DA FINALIZACAO DO CADASTRO DE LOTE DO REMANEJAMENTO N:' ||
6808 p_idremanejamento, 'N');
6809
6810 -- Consumindo quantidade dos lotes antigos
6811 v_qtdeUtilizadaUN := 0;
6812 for r_loteantigo in (select ll.idarmazem, l.idlote,
6813 ll.pendencia,
6814 (ll.estoque - ll.pendencia) disponivelestoque,
6815 l.qtdedisponivel qtdecoberta
6816 from lotelocal ll, lote l
6817 where ll.idarmazem =
6818 r_remanejamento.idarmazemorigem
6819 and ll.idlocal =
6820 r_remanejamento.idlocalorigem
6821 and l.idlote =
6822 c_loteRemanejamento.Idlote
6823 and l.idlote = ll.idlote
6824 and l.idproduto =
6825 c_novoLote.idproduto
6826 and l.estado = c_novoLote.estado
6827 and l.iddepositante =
6828 c_novoLote.iddepositante
6829 and case
6830 when c_novoLote.itemadicional is null then
6831 1
6832 else
6833 decode(l.itemadicional,
6834 c_novoLote.itemadicional,
6835 1, 0)
6836 end = 1
6837 and decode(trunc(l.dtvenc),
6838 trunc(c_novoLote.dtvenc), 1,
6839 0) = 1
6840 and case
6841 when c_novoLote.dtfabricacao is null then
6842 1
6843 else
6844 decode(trunc(l.dtfabricacao),
6845 trunc(c_novoLote.dtfabricacao),
6846 1, 0)
6847 end = 1
6848 and decode(l.descr,
6849 c_novoLote.loteindustria, 1,
6850 decode(l.descr,
6851 'LOTE DE PICKING SETOR CAIXAS PARA PICKING SETOR UNIDADES',
6852 1, 0)) = 1
6853 and case
6854 when c_novoLote.loteadicional is null then
6855 1
6856 else
6857 decode(l.loteadicional,
6858 c_novoLote.loteadicional,
6859 1, 0)
6860 end = 1
6861 order by l.dtalocacao)
6862
6863 loop
6864 if (r_loteantigo.disponivelestoque >= v_qtdeUN) then
6865 v_qtdeUtilizadaUN := v_qtdeUN;
6866 else
6867 if ((r_loteantigo.disponivelestoque = 0) or
6868 -- pode estar zerando o estoque do lote antigo ou
6869 (v_qtdeUN >= r_loteantigo.disponivelestoque)) then
6870 -- pode estar retirando uma quantidade maior que o disponÃvel, porém dentro desta operação
6871 -- onde a pendência é deste remanejamento e não de outro
6872 v_qtdeUtilizadaUN := r_loteantigo.pendencia;
6873 else
6874 v_qtdeUtilizadaUN := r_loteantigo.disponivelestoque;
6875 end if;
6876 end if;
6877
6878 v_qtdeUN := v_qtdeUN - v_qtdeUtilizadaUN;
6879
6880 pk_estoque.retirar_adicionar(r_remanejamento.idarmazemdestino,
6881 r_remanejamento.idlocaldestino,
6882 r_loteantigo.idlote,
6883 v_qtdeUtilizadaUN,
6884 p_idusuario,
6885 'ADICIONAR SUBTRAIDO REFERENTE AO REMANEJAMENTO N:' ||
6886 r_remanejamento.idremanejamento);
6887
6888 pk_estoque.retirar_pendencia(r_remanejamento.idarmazemorigem,
6889 r_remanejamento.idlocalorigem,
6890 r_loteantigo.idlote,
6891 v_qtdeUtilizadaUN,
6892 p_idusuario,
6893 'PENDENCIA SUBTRAIDA REFERENTE AO REMANEJAMENTO N:' ||
6894 r_remanejamento.idremanejamento);
6895
6896 pk_estoque.retirar_estoque(r_loteantigo.idarmazem,
6897 r_remanejamento.idlocalorigem,
6898 r_loteantigo.idlote,
6899 v_qtdeUtilizadaUN, p_idusuario,
6900 'ESTOQUE RETIRADO EM FUNCAO DA FINALIZACAO DO CADASTRO DE LOTE DO REMANEJAMENTO N:' ||
6901 p_idremanejamento, 'N');
6902 exit when v_qtdeUN = 0;
6903 end loop;
6904
6905 if (v_qtdeUN > 0) then
6906 v_msgRaise := t_message('Não foi possÃvel encontrar estoque suficiente para o produto.' ||
6907 chr(13) || 'IDPRODUTO: {0}' ||
6908 chr(13) ||
6909 'QTDENECESSARIA UN: {1}');
6910 v_msgRaise.addParam(c_novoLote.idproduto);
6911 v_msgRaise.addParam(c_novoLote.qtde *
6912 c_novoLote.fatorconversao);
6913 raise_application_error(-20000, v_msgRaise.formatMessage);
6914 end if;
6915
6916 end loop;
6917
6918 else
6919 v_msgRaise := t_message('Houve alterações no lote selecionado e não será possÃvel continuar o processo.');
6920 raise_application_error(-20000, v_msgRaise.formatMessage);
6921 end if;
6922 end loop;
6923 else
6924 for c in (select lr.idlote, lr.qtde, lt.idproduto, lt.barra,
6925 lt.iddepositante
6926 from loteremanejamento lr, lote lt
6927 where lt.idlote = lr.idlote
6928 and lr.idremanejamento =
6929 r_remanejamento.idremanejamento
6930 order by lr.idlote)
6931 loop
6932
6933 validarSetor(c.idlote, r_remanejamento.idarmazemdestino,
6934 r_remanejamento.idlocaldestino, v_msg, v_erro);
6935
6936 validarBufPickEsteira(p_idremanejamento, p_telaExecFinalizacao);
6937
6938 if v_remDePlParaPkEsteira then
6939
6940 -- Remanejamento é de Pulmão sem esteira para Picking com esteira
6941 -- Verifica se o fator de conversão é maior que um ou caixa fechada
6942 begin
6943 select e.fatorconversao, e.caixafechada, p.codigointerno
6944 into v_fatorConversao, v_caixaFechada, v_produto
6945 from loteremanejamento lr, lote lt, produto p, embalagem e
6946 where lr.idremanejamento = r_remanejamento.idremanejamento
6947 and lr.idlote = c.idlote
6948 and lt.idlote = lr.idlote
6949 and p.idproduto = lt.idproduto
6950 and e.idproduto = p.idproduto
6951 and e.barra = c.barra;
6952
6953 if (v_fatorConversao = 1 and v_caixaFechada = 'N' and
6954 r_remanejamento.usoexclusivocxmov = 0) then
6955 v_msgRaise := t_message('Não é permitido remanejar produtos unitários diretamente para o Picking com Esteira.' ||
6956 chr(13) || 'Código Interno: {0}.');
6957 v_msgRaise.addParam(v_produto);
6958 raise_application_error(-20000, v_msgRaise.formatMessage);
6959 end if;
6960
6961 end;
6962 end if;
6963
6964 if (v_erro) then
6965 raise_application_error(-20000, v_msg);
6966 end if;
6967
6968 pk_picking_dinamico.addGttPickingDinamico(c.idproduto,
6969 c.iddepositante,
6970 r_remanejamento.idlocaldestino,
6971 r_remanejamento.idarmazemdestino,
6972 1);
6973
6974 pk_estoque.incluir_estoque(r_remanejamento.idarmazemdestino,
6975 r_remanejamento.idlocaldestino,
6976 c.idlote, c.qtde, p_idusuario,
6977 'ESTOQUE ACRESCIDO NO LOTE REFERENTE AO REMANEJAMENTO N:' ||
6978 r_remanejamento.idremanejamento, 'N');
6979
6980 pk_estoque.retirar_adicionar(r_remanejamento.idarmazemdestino,
6981 r_remanejamento.idlocaldestino,
6982 c.idlote, c.qtde, p_idusuario,
6983 'ADICIONAR SUBTRAIDO REFERENTE AO REMANEJAMENTO N:' ||
6984 r_remanejamento.idremanejamento);
6985
6986 pk_estoque.retirar_pendencia(r_remanejamento.idarmazemorigem,
6987 r_remanejamento.idlocalorigem,
6988 c.idlote, c.qtde, p_idusuario,
6989 'PENDENCIA SUBTRAIDA REFERENTE AO REMANEJAMENTO N:' ||
6990 r_remanejamento.idremanejamento);
6991
6992 pk_estoque.retirar_estoque(r_remanejamento.idarmazemorigem,
6993 r_remanejamento.idlocalorigem,
6994 c.idlote, c.qtde, p_idusuario,
6995 'ESTOQUE SUBTRAIDO NO LOTE REFERENTE AO REMANEJAMENTO N:' ||
6996 r_remanejamento.idremanejamento, 'N');
6997
6998 if ((v_tipoLocalOrigem = 0) AND (v_LocalOrigemBuffer = 'N')) then
6999 pk_picking_dinamico.addGttPickingDinamico(c.idproduto,
7000 c.iddepositante,
7001 r_remanejamento.idlocalorigem,
7002 r_remanejamento.idarmazemorigem,
7003 0);
7004 end if;
7005
7006 end loop;
7007
7008 pk_picking_dinamico.deletar_picking_dinamico;
7009 pk_picking_dinamico.inserir_picking_dinamico;
7010
7011 pk_convocacao.finalizaremanejamento(p_idusuario,
7012 r_remanejamento.idremanejamento);
7013
7014 pk_convocacao.finalizaRemOrigem(p_idusuario,
7015 r_remanejamento.idremanejamento);
7016
7017 pk_convocacao.finalizaRemDestino(p_idusuario,
7018 r_remanejamento.idremanejamento);
7019
7020 select l.id, l.tipo
7021 into v_idendereco, v_picking
7022 from local l
7023 where l.idlocal = r_remanejamento.idlocaldestino
7024 and l.idarmazem = r_remanejamento.idarmazemdestino;
7025
7026 if (v_picking = 0) then
7027 for c in (select distinct r_remanejamento.idarmazemdestino idarmazem,
7028 r_remanejamento.idlocaldestino idlocal,
7029 l.idproduto, l.iddepositante
7030 from loteremanejamento lr, lote l,
7031 produtodepositante pd
7032 where l.idlote = lr.idlote
7033 and pd.identidade = l.iddepositante
7034 and pd.idproduto = l.idproduto
7035 and pd.pickingdinamico = 1
7036 and lr.idremanejamento =
7037 r_remanejamento.idremanejamento
7038 and not exists
7039 (select 1
7040 from produtolocal
7041 where idarmazem =
7042 r_remanejamento.idarmazemdestino
7043 and idlocal = r_remanejamento.idlocaldestino
7044 and idproduto = l.idproduto
7045 and identidade = l.iddepositante))
7046 loop
7047 pk_armazem.criarPickingProduto(c.idarmazem, c.idlocal,
7048 c.iddepositante, c.idproduto);
7049 end loop;
7050 end if;
7051 end if;
7052 else
7053 v_msgRaise := t_message('O REMANEJAMENTO NAO FOI PLANEJADO.');
7054 raise_application_error(-20000, v_msgRaise.formatMessage);
7055 end if;
7056
7057 validarFinalizarRemanejamento(p_idremanejamento);
7058
7059 update remanejamento r
7060 set r.status = 'F',
7061 r.idusuario = nvl(r.idusuario, p_idusuario),
7062 r.idusuarioinicio = nvl(r.idusuarioinicio, p_idusuario),
7063 r.idusuariofim = nvl(r.idusuariofim, p_idusuario),
7064 r.datahora = nvl(r.datahora, sysdate),
7065 r.horainicio = nvl(r.horainicio, sysdate),
7066 r.horafim = nvl(r.horafim, sysdate),
7067 r.horainicioorigem = nvl(r.horainicioorigem, sysdate),
7068 r.horafimorigem = nvl(r.horafimorigem, sysdate)
7069 where r.idremanejamento = p_idremanejamento;
7070
7071 -- Removendo os endereços filhos da origem quando houver
7072 RemoveInfMultiEndereco(p_idremanejamento);
7073
7074 -- Finalizar Remanejamento Ordem Devolução (Caso seje)
7075 pk_ordemdevolucao.finalizarremanejamento(p_idremanejamento);
7076
7077 if r_remanejamento.idprodutorecuperado is not null then
7078 if r_remanejamento.tipoexportacaoprodrecuperado = 1 then
7079 pk_integracao.exportarprodutorecuperado(r_remanejamento.idprodutorecuperado);
7080 elsif r_remanejamento.tipoexportacaoprodrecuperado in (2, 3) then
7081 pk_integracao.exportarmovimentacaointerna(r_remanejamento.idprodutorecuperado,
7082 3);
7083 elsif r_remanejamento.tipoexportacaoprodrecuperado in (4, 5, 6) then
7084 pk_integracao.exportarProdutoRecuperadoMV(r_remanejamento.idprodutorecuperado,
7085 r_remanejamento.tipoexportacaoprodrecuperado);
7086 end if;
7087 end if;
7088
7089 if r_remanejamento.idcontroleavaria is not null then
7090 pk_integracao.exportarControleAvaria(r_remanejamento.idcontroleavaria);
7091 pk_integracao.exportarMovimentacaoInterna(r_remanejamento.idcontroleavaria,
7092 1);
7093 end if;
7094
7095 if r_remanejamento.idinsucessoentrega is not null then
7096 select count(*)
7097 into v_existeCRN
7098 from remanejamento r
7099 where r.idinsucessoentrega = r_remanejamento.idinsucessoentrega
7100 and r.idremanejamento <> p_idremanejamento
7101 and r.status <> 'F';
7102
7103 if v_existeCRN = 0 then
7104 pk_integracao.exportarMovimentacaoInterna(r_remanejamento.idinsucessoentrega,
7105 5);
7106
7107 update insucessoentrega
7108 set status = 3
7109 where id = r_remanejamento.idinsucessoentrega;
7110 end if;
7111 end if;
7112
7113 pk_integracao.expRemanejamentoEntreSetores(p_idremanejamento);
7114
7115 pk_utilities.GeraLog(p_idusuario,
7116 'O REMANEJAMENTO ' || p_idremanejamento ||
7117 ' FOI EXECUTADO COM SUCESSO.', p_idremanejamento,
7118 'RP');
7119
7120 execOrigemRemBufferPK;
7121 end if;
7122 end;
7123
7124 procedure finalizarRemanejamentoWEB
7125 (
7126 p_idremanejamento in number,
7127 p_idusuario in number
7128 ) is
7129 v_Pendentes varchar2(2000);
7130 v_finalizado char(1);
7131 v_msg t_message;
7132 begin
7133 /*Verifica se não existe remanejamentos anteriores para o endereço de origem */
7134 select status
7135 into v_finalizado
7136 from remanejamento
7137 where idremanejamento = p_idremanejamento;
7138
7139 if v_finalizado <> 'F' then
7140 select (select stragg(r.idremanejamento) pendente
7141 from remanejamento r, loteremanejamento lr
7142 where r.idarmazemdestino = rp.idarmazemorigem
7143 and r.idlocaldestino = rp.idlocalorigem
7144 and r.status <> 'F'
7145 and lr.idremanejamento = r.idremanejamento
7146 and r.datahora < rp.datahora
7147 and r.idremanejamento < rp.idremanejamento
7148 and exists
7149 (select 1
7150 from loteremanejamento
7151 where idremanejamento = rp.idremanejamento
7152 and idlote = lr.idlote))
7153 into v_Pendentes
7154 from remanejamento rp
7155 where rp.idremanejamento = p_idremanejamento
7156 and rp.status <> 'F';
7157
7158 if v_pendentes is null then
7159
7160 finalizarRemanejamento(p_idremanejamento, p_idusuario,
7161 C_REMANEJ_WEB);
7162 else
7163 v_msg := t_message('EXISTEM REMANEJAMENTOS PENDENTES QUE DEVEM SER EXECUTADOS ANTERIORMENTE: ' ||
7164 '{0}. OPERAÇÃO CANCELADA.');
7165 v_msg.addParam(v_Pendentes);
7166 raise_application_error(-20000, v_msg.formatMessage);
7167 end if;
7168 else
7169 v_msg := t_message('O REMANEJAMENTO Jà FOI FINALIZADO. OPERAÇÃO CANCELADA.');
7170 raise_application_error(-20000, v_msg.formatMessage);
7171 end if;
7172
7173 end finalizarRemanejamentoWEB;
7174
7175 procedure finalizarPlanejamento(p_idremanejamento in number) is
7176 v_lotes number;
7177 v_msg t_message;
7178 begin
7179 select count(*)
7180 into v_lotes
7181 from loteremanejamento
7182 where idremanejamento = p_idremanejamento;
7183
7184 if v_lotes = 0 then
7185 v_msg := t_message('LOTES NÃO VINCULADOS AO REMANEJAMENTO');
7186 raise_application_error(-20000, v_msg.formatMessage);
7187 else
7188 update remanejamento
7189 set planejado = 'S'
7190 where idremanejamento = p_idremanejamento
7191 and planejado = 'N';
7192 end if;
7193 end;
7194
7195 procedure reabrirPlanejamento
7196 (
7197 p_idremanejamento in number,
7198 p_idusuario in number
7199 ) is
7200 v_lotes number;
7201 v_status remanejamento.status%type;
7202 v_tipolocalorigem number;
7203 v_tipolocaldestino number;
7204 v_msg t_message;
7205 begin
7206 -- Realizando lock e verificando status do remanejamento
7207 select r.status
7208 into v_status
7209 from remanejamento r
7210 where r.idremanejamento = p_idremanejamento
7211 for update;
7212
7213 select count(*)
7214 into v_lotes
7215 from loteremanejamento
7216 where idremanejamento = p_idremanejamento
7217 and conferido = 'S';
7218
7219 select lo.tipo, ld.tipo
7220 into v_tipolocalorigem, v_tipolocaldestino
7221 from remanejamento r, local lo, local ld
7222 where r.idremanejamento = p_idremanejamento
7223 and lo.idlocal = r.idlocalorigem
7224 and lo.idarmazem = r.idarmazemorigem
7225 and ld.idlocal = r.idlocaldestino
7226 and ld.idarmazem = r.idarmazemdestino;
7227
7228 if v_tipolocalorigem = 0
7229 and (v_tipolocaldestino = 1 or v_tipolocaldestino = 2) then
7230 v_msg := t_message('O PLANEJAMENTO NÃO PODE SER REABERTO PARA REMANEJAMENTOS DE PICKING PARA PULMÃO');
7231 raise_application_error(-20000, v_msg.formatMessage);
7232 end if;
7233
7234 if v_status = 'F' then
7235 v_msg := t_message('O PLANEJAMENTO NÃO PODE SER REABERTO. REMANEJAMENTO Jà FINALIZADO.');
7236 raise_application_error(-20000, v_msg.formatMessage);
7237 elsif v_lotes > 0
7238 or v_status <> 'A' then
7239 v_msg := t_message('O PLANEJAMENTO NÃO PODE SER REABERTO. REMANEJAMENTO Jà INICIADO.');
7240 raise_application_error(-20000, v_msg.formatMessage);
7241 else
7242 update remanejamento
7243 set planejado = 'N'
7244 where idremanejamento = p_idremanejamento
7245 and planejado = 'S';
7246
7247 pk_utilities.GeraLog(p_idusuario,
7248 'PLANEJAMENTO REABERTO PARA O ID: ' ||
7249 P_IDREMANEJAMENTO, P_IDREMANEJAMENTO, 'RP');
7250 end if;
7251 end;
7252
7253 /*
7254 * Carrega o resumo das embalagens a serem utilizadas
7255 * a partir do produto, local e da qtde a ser movimentada
7256 */
7257 function retResumoEmbalagensLocal
7258 (
7259 p_idproduto in number,
7260 p_qtde in number,
7261 p_idlocal in local.idlocal%type
7262 ) return string is
7263
7264 cursor c_emb
7265 (
7266 p_produto in number,
7267 p_rest in number,
7268 p_idlocal in local.idlocal%type
7269 ) is
7270 select e.barra, e.fatorconversao, e.descrreduzido
7271 from embalagem e, lote l, lotelocal ll
7272 where e.idproduto = p_idproduto
7273 and trunc(p_rest / e.fatorconversao) > 0
7274 and e.ativo = 'S'
7275 and l.idproduto = e.idproduto
7276 and l.barra = e.barra
7277 and ll.idlote = l.idlote
7278 and ll.idlocal = p_idlocal
7279 and ll.estoque > 0
7280 order by e.fatorconversao desc;
7281
7282 r_emb c_emb%rowtype;
7283 v_resumo varchar2(1000);
7284 v_qtdeUtilizada number;
7285 v_qtde number;
7286 begin
7287 v_qtde := p_qtde;
7288
7289 open c_emb(p_idproduto, v_qtde, p_idlocal);
7290 fetch c_emb
7291 into r_emb;
7292 while (v_qtde > 0)
7293 and (c_emb%found)
7294 loop
7295 v_qtdeUtilizada := trunc(v_qtde / r_emb.fatorconversao);
7296 if v_qtdeUtilizada > 0 then
7297 v_qtde := v_qtde - (v_qtdeUtilizada * r_emb.fatorconversao);
7298
7299 if v_resumo is not null then
7300 v_resumo := v_resumo || ', ';
7301 end if;
7302
7303 v_resumo := v_resumo || v_qtdeUtilizada || ' ' ||
7304 r_emb.descrreduzido || ' [' || r_emb.fatorconversao || ']';
7305
7306 end if;
7307 fetch c_emb
7308 into r_emb;
7309 end loop;
7310 close c_emb;
7311
7312 return v_resumo;
7313
7314 end;
7315
7316 procedure associarLote
7317 (
7318 p_idremanejamento in number,
7319 p_idlote in number,
7320 p_quantidade in number,
7321 p_idusuario in number
7322 ) is
7323 v_idproduto number;
7324 v_idarmazemorigem number;
7325 v_idlocalorigem local.idlocal%type;
7326 v_idarmazemdestino number;
7327 v_total number;
7328 v_idlocaldestino local.idlocal%type;
7329 v_tipolote char(1);
7330 v_result varchar(500);
7331 v_pkpl number;
7332 v_travaLocalIntEsteira number;
7333 v_produtoCodigoInt produto.codigointerno%type;
7334 v_msg t_message;
7335
7336 procedure regraLoteUnico(p_idLote in lote.idlote%type) is
7337 r_loteUnicoComparar pk_lote.t_loteunico;
7338 v_idarmazemdestino number;
7339 v_idlocaldestino local.idlocal%type;
7340 v_msgerro varchar2(5000);
7341 begin
7342
7343 select lt.idproduto, lt.estado, lt.descr loteindustria,
7344 lt.dtvenc dtvencimento, pd.loteuniconoendereco,
7345 lt.iddepositante
7346 into r_loteUnicoComparar
7347 from lote lt, produtodepositante pd
7348 where lt.idlote = p_idLote
7349 and pd.identidade = lt.iddepositante
7350 and pd.idproduto = lt.idproduto;
7351
7352 for r_loteunico in (select lt.idproduto, lt.estado,
7353 lt.descr loteindustria,
7354 lt.dtvenc dtvencimento,
7355 pd.loteuniconoendereco, lt.iddepositante
7356 from loteremanejamento lr, lote lt,
7357 produtodepositante pd
7358 where lr.idremanejamento = p_idremanejamento
7359 and pd.idproduto = lt.idproduto
7360 and lt.idlote = lr.idlote
7361 and pd.identidade = lt.iddepositante
7362 and pd.idproduto = lt.idproduto)
7363 loop
7364
7365 if (not pk_lote.isLoteUnicoCompativel(r_loteunicocomparar,
7366 r_loteunico, v_msgerro)) then
7367 raise_application_error(-20000, v_msgErro);
7368 end if;
7369
7370 end loop;
7371
7372 select r.idarmazemdestino, r.idlocaldestino
7373 into v_idarmazemdestino, v_idlocaldestino
7374 from remanejamento r
7375 where r.idremanejamento = p_idremanejamento;
7376
7377 if (not
7378 pk_lote.isLocalPodeReceberLoteUnico(r_loteunicocomparar,
7379 v_idarmazemdestino,
7380 v_idlocaldestino, v_msgerro)) then
7381 raise_application_error(-20000, v_msgerro);
7382 end if;
7383 end;
7384
7385 procedure validarVinculos is
7386 v_localOrigem local.idlocalformatado%type;
7387 v_localDestino local.idlocalformatado%type;
7388 v_tipoLocalOrigem local.tipo%type;
7389 v_tipoLocalDestino local.tipo%type;
7390 v_tipoPermPkSetorOrigem setor.tipopermitirpickingsetor%type;
7391 v_tipoPermPkSetorDestino setor.tipopermitirpickingsetor%type;
7392 v_localIncompativel number;
7393 v_idProduto produto.idproduto%type;
7394 v_barra embalagem.barra%type;
7395 v_tipoPkProdDep produtodepositante.tipopicking%type;
7396 v_idArmazem armazem.idarmazem%type;
7397 v_idLocalDestino local.idlocal%type;
7398 v_idEntidade depositante.identidade%type;
7399 v_unidades number;
7400 v_msg t_message;
7401 v_fatorConversaoLote embalagem.fatorconversao%type;
7402 v_qtdeTotalLote lote.qtdedisponivel%type;
7403 v_barraUnitaria number;
7404 v_existeEstoque number;
7405 v_fatorNoEstoque number;
7406 begin
7407
7408 select lt.idarmazem, lt.iddepositante, lt.idproduto, e.fatorconversao,
7409 lt.qtdedisponivel
7410 into v_idArmazem, v_idEntidade, v_idProduto, v_fatorConversaoLote,
7411 v_qtdeTotalLote
7412 from lote lt, embalagem e
7413 where lt.idlote = p_idlote
7414 and e.idproduto = lt.idproduto
7415 and e.barra = lt.barra;
7416
7417 select lo.idlocalformatado localorigem, lo.tipo,
7418 so.tipopermitirpickingsetor, ld.idlocalformatado localdestino,
7419 ld.tipo, sd.tipopermitirpickingsetor, ld.idlocal
7420 into v_localOrigem, v_tipoLocalOrigem, v_tipoPermPkSetorOrigem,
7421 v_localDestino, v_tipoLocalDestino, v_tipoPermPkSetorDestino,
7422 v_idLocalDestino
7423 from remanejamento r, local lo, local ld, setor so, setor sd
7424 where r.idremanejamento = p_idremanejamento
7425 and lo.idlocal = r.idlocalorigem
7426 and lo.idarmazem = r.idarmazemorigem
7427 and ld.idlocal = r.idlocaldestino
7428 and ld.idarmazem = r.idarmazemdestino
7429 and so.idsetor = lo.idsetor
7430 and sd.idsetor = ld.idsetor;
7431
7432 if (v_tipoLocalDestino = C_LOCAL_PICKING) then
7433 -- Validando se o produto é compatÃvel com o setor/endereço Destino.
7434 pk_armazem.validarSetorPickingProduto(v_idArmazem, v_idLocalDestino,
7435 v_idEntidade, v_idProduto);
7436 if (v_tipoLocalOrigem in
7437 (C_LOCAL_PULMAO_BLOCADO, C_LOCAL_PULMAO_PALETIZADO)) then
7438 -- Validando se o setor/endereço Origem é compatÃvel com o setor/endereço Destino.
7439 if (v_tipoPermPkSetorDestino = C_SETOR_MISTO) then
7440 return;
7441 elsif (v_tipoPermPkSetorDestino = C_SETOR_UNIDADES) then
7442 begin
7443 select count(1), e.barra
7444 into v_localIncompativel, v_barra
7445 from lote lt, produto p, embalagem e
7446 where lt.idlote = p_idlote
7447 and p.idproduto = lt.idproduto
7448 and e.idproduto = p.idproduto
7449 and e.barra = lt.barra
7450 and ((e.fatorconversao = 1 and e.caixafechada = 'S') or
7451 e.fatorconversao > 1)
7452 group by p.idproduto, e.barra;
7453 exception
7454 when no_data_found then
7455 v_localIncompativel := 0;
7456 end;
7457
7458 if (v_localIncompativel > 0) then
7459 v_msg := t_message('Local Origem: {0} incompatÃvel com o Local Destino: {1}.' ||
7460 chr(13) ||
7461 'O Produto: {2} Barra: {3} Lote: {4} não pode ser remanejado para um picking que aceita somente Unidades.');
7462 v_msg.addParam(v_localOrigem);
7463 v_msg.addParam(v_localDestino);
7464 v_msg.addParam(v_idProduto);
7465 v_msg.addParam(v_barra);
7466 v_msg.addParam(p_idlote);
7467
7468 raise_application_error(-20000, v_msg.formatMessage);
7469 end if;
7470
7471 elsif (v_tipoPermPkSetorDestino = C_SETOR_CAIXAS) then
7472 begin
7473 select count(1), e.barra
7474 into v_localIncompativel, v_barra
7475 from lote lt, produto p, embalagem e
7476 where lt.idlote = p_idlote
7477 and p.idproduto = lt.idproduto
7478 and e.idproduto = p.idproduto
7479 and e.barra = lt.barra
7480 and e.fatorconversao = 1
7481 and e.caixafechada = 'N'
7482 group by p.idproduto, e.barra;
7483 exception
7484 when no_data_found then
7485 v_localIncompativel := 0;
7486 end;
7487
7488 if v_localIncompativel > 0 then
7489 v_msg := t_message('Local Origem: {0} incompatÃvel com o Local Destino: {1}.' ||
7490 chr(13) ||
7491 'O Produto: {2} Barra: {3} Lote: {4} não pode ser remanejado para um picking que aceita somente Caixas.');
7492 v_msg.addParam(v_localOrigem);
7493 v_msg.addParam(v_localDestino);
7494 v_msg.addParam(v_idProduto);
7495 v_msg.addParam(v_barra);
7496 v_msg.addParam(p_idlote);
7497
7498 raise_application_error(-20000, v_msg.formatMessage);
7499 end if;
7500
7501 select count(1)
7502 into v_existeEstoque
7503 from remanejamento r, local ld, lotelocal ll
7504 where r.idremanejamento = p_idremanejamento
7505 and ld.idlocal = r.idlocaldestino
7506 and ld.idarmazem = r.idarmazemdestino
7507 and ll.idlocal = ld.idlocal
7508 and ll.idarmazem = ld.idarmazem
7509 and (ll.estoque > 0 or ll.pendencia > 0 or ll.adicionar > 0);
7510
7511 if v_existeEstoque > 0 then
7512 begin
7513 select distinct e.fatorconversao
7514 into v_fatorNoEstoque
7515 from remanejamento r, lotelocal ll, lote lt, embalagem e
7516 where r.idremanejamento = p_idremanejamento
7517 and ll.idlocal = r.idlocaldestino
7518 and lt.idlote = ll.idlote
7519 and (ll.estoque > 0 or ll.pendencia > 0 or
7520 ll.adicionar > 0)
7521 and e.idproduto = lt.idproduto
7522 and e.barra = lt.barra;
7523 exception
7524 when too_many_rows then
7525 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.');
7526 raise_application_error(-20000, v_msg.formatMessage);
7527 end;
7528
7529 if v_fatorNoEstoque <> v_fatorConversaoLote then
7530 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}.' ||
7531 chr(13) ||
7532 'Selecione uma embalagem com o mesmo fator de conversão ou escolha outro endereço para o remanejamento.');
7533 v_msg.addParam(v_fatorNoEstoque);
7534
7535 raise_application_error(-20000, v_msg.formatMessage);
7536 end if;
7537 end if;
7538 end if;
7539
7540 elsif (v_tipoLocalOrigem = C_LOCAL_PICKING) then
7541
7542 if (v_tipoPermPkSetorOrigem = C_SETOR_CAIXAS and
7543 v_tipoPermPkSetorDestino = C_SETOR_UNIDADES) then
7544 begin
7545 select count(1)
7546 into v_barraUnitaria
7547 from lote lt, embalagem e
7548 where lt.idlote = p_idlote
7549 and e.idproduto = lt.idproduto
7550 and e.barra = lt.barra
7551 and e.fatorconversao = 1
7552 and e.caixafechada = 'N'
7553 and e.ativo = 'S'
7554 and e.precadastro = 'N';
7555 exception
7556 when no_data_found then
7557 v_barraUnitaria := 0;
7558 end;
7559
7560 if not (v_barraUnitaria > 0) then
7561 if (verificaCompOrigemDestino(v_qtdeTotalLote,
7562 v_fatorConversaoLote,
7563 p_quantidade)) then
7564 return;
7565 else
7566 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.' ||
7567 chr(13) || 'Escolha outra quantidade.');
7568 v_msg.addParam(p_quantidade);
7569 raise_application_error(-20000, v_msg.formatMessage);
7570 end if;
7571 end if;
7572
7573 elsif (v_tipoPermPkSetorOrigem = C_SETOR_UNIDADES and
7574 v_tipoPermPkSetorDestino = C_SETOR_CAIXAS) then
7575
7576 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¿.' ||
7577 chr(13) ||
7578 'Este Picking só poderá receber remanejamentos vindos do Pulmão ou de outro Picking pertencente à um Setor configurado para ¿Permitir Picking: Caixas¿.');
7579 v_msg.addParam(v_idProduto);
7580
7581 raise_application_error(-20000, v_msg.formatMessage);
7582 end if;
7583 end if;
7584 end if;
7585 end validarVinculos;
7586
7587 begin
7588 if p_quantidade <= 0 then
7589 v_msg := t_message('Quantidade não pode ser menor ou igual a zero');
7590 raise_application_error(-20000, v_msg.formatMessage);
7591 end if;
7592
7593 select count(*)
7594 into v_pkpl
7595 from remanejamento r, local lo, local ld
7596 where lo.idarmazem = r.idarmazemorigem
7597 and lo.idlocal = r.idlocalorigem
7598 and ld.idarmazem = r.idarmazemdestino
7599 and ld.idlocal = r.idlocaldestino
7600 and lo.tipo = 0 -- picking = 'S'
7601 and ld.tipo in (1, 2) --picking = 'N'
7602 and r.idcontroleavaria is null
7603 and r.idremanejamento = p_idremanejamento;
7604
7605 if v_pkpl <> 0 then
7606 v_msg := t_message('Não é possÃvel Incluir Lotes em Remanejamentos de Picking para Pulmão');
7607 raise_application_error(-20000, v_msg.formatMessage);
7608 end if;
7609
7610 validarVinculos;
7611
7612 pk_lote.validarProdutoFracionado(p_idlote, p_quantidade);
7613
7614 select count(*)
7615 into v_total
7616 from loteremanejamento lr
7617 where lr.idremanejamento = p_idremanejamento
7618 and idlote = p_idlote;
7619 if v_total > 0 then
7620 desassociarLote(p_idremanejamento, p_idlote, p_idusuario);
7621 end if;
7622
7623 select tipolote, idproduto
7624 into v_tipolote, v_idproduto
7625 from lote
7626 where idlote = p_idlote;
7627
7628 select idarmazemorigem, idlocalorigem, idarmazemdestino, idlocaldestino
7629 into v_idarmazemorigem, v_idlocalorigem, v_idarmazemdestino,
7630 v_idlocaldestino
7631 from remanejamento
7632 where idremanejamento = p_idremanejamento;
7633
7634 select count(l.idlocal)
7635 into v_travaLocalIntEsteira
7636 from remanejamento r, local l, setor s
7637 where r.idremanejamento = p_idremanejamento
7638 and s.idsetor = l.idsetor
7639 and ((l.idlocal = v_idlocalorigem and
7640 l.idarmazem = v_idarmazemorigem and l.tipo in (1, 2) and
7641 length(trim(s.codintegracaoesteira)) is null) or
7642 (l.idlocal = v_idlocaldestino and
7643 l.idarmazem = v_idarmazemdestino and l.tipo = 0 and
7644 length(trim(s.codintegracaoesteira)) is not null));
7645
7646 if (v_travaLocalIntEsteira = 2) then
7647 begin
7648 select pd.codigointerno
7649 into v_produtoCodigoInt
7650 from lote lt, produto pd, embalagem emb
7651 where lt.idlote = p_idlote
7652 and pd.idproduto = lt.idproduto
7653 and emb.idproduto = pd.idproduto
7654 and emb.barra = lt.barra
7655 and (emb.fatorconversao = 1 and emb.caixafechada = 'N')
7656 and rownum = 1;
7657
7658 v_msg := t_message('Não é possÃvel vincular as unidades do produto cod: "' ||
7659 '{0}" nesse remanejamento.' || CHR(13) ||
7660 'Esse remanejamento possui o endereço de destino id: "' ||
7661 '{1}" parametrizado para ser do tipo picking com setor que utiliza integração com esteira.');
7662 v_msg.addParam(v_produtoCodigoInt);
7663 v_msg.addParam(v_idlocaldestino);
7664 raise_application_error(-20000, v_msg.formatMessage);
7665 exception
7666 when no_data_found then
7667 null;
7668 end;
7669 end if;
7670
7671 v_result := ctrlVincularMaterial(p_idlote, v_tipolote, v_idproduto,
7672 v_idarmazemorigem, v_idlocalorigem,
7673 v_idarmazemdestino, v_idlocaldestino,
7674 p_quantidade);
7675
7676 if v_result is not null then
7677 raise_application_error(-20000, v_result);
7678 end if;
7679
7680 regraLoteUnico(p_idlote);
7681
7682 insert into loteremanejamento
7683 (idremanejamento, idlote, qtde, conferido, controlaqtde)
7684 values
7685 (p_idremanejamento, p_Idlote, p_quantidade, 'N', 'S');
7686
7687 associar(p_idremanejamento, p_idlote);
7688 end;
7689
7690 procedure desassociarLote
7691 (
7692 p_idremanejamento in number,
7693 p_idlote in number,
7694 p_idusuario in number
7695 ) is
7696 begin
7697
7698 DESASSOCIAR(p_idremanejamento, p_idlote);
7699
7700 delete from loteremanejamento
7701 where idremanejamento = p_idremanejamento
7702 and idlote = p_idlote;
7703 end;
7704
7705 procedure MarcarImpressoRemanejamento
7706 (
7707 p_idremanejamento in number,
7708 p_idusuario in number
7709 ) is
7710 v_usuario usuario.nomeusuario%type;
7711 begin
7712 select nomeusuario
7713 into v_usuario
7714 from usuario
7715 where idusuario = p_idusuario;
7716
7717 update loteremanejamento
7718 set impresso = 'S'
7719 where idremanejamento = p_idremanejamento;
7720
7721 pk_utilities.GeraLog(p_idusuario,
7722 'REMANEJAMENTO ID:' || p_idremanejamento ||
7723 ' MARCADO COMO IMPRESSO PELO USUARIO ID:' ||
7724 p_idusuario || ' nome: ' || v_usuario,
7725 p_idremanejamento, 'RM');
7726 end;
7727
7728 procedure formarLotePickingPulmao
7729 (
7730 p_idremanejamento in number,
7731 p_idusuario in number,
7732 p_moduloSistema in number := 0
7733 ) is
7734 MODULO_ENTERPRISE constant number := 0;
7735 MODULO_MWMS constant number := 1;
7736
7737 r_remanejamento remanejamento%rowtype;
7738
7739 v_msg t_message;
7740 v_mgsContratoLote historicomudancacontratolote.mensagem%type;
7741
7742 procedure validacoes is
7743 v_jaPossuiLoteVinculado number;
7744 v_caixaNaoInformada number;
7745 r_loteunicocomparar pk_lote.t_loteunico;
7746 v_msgerro varchar2(4000);
7747
7748 v_existeLoteSemPeso number;
7749 v_existeInfoEspecNaoInformada number;
7750
7751 C_PRODUTO_SEGREGADO constant number := 1;
7752
7753 procedure validarProdutoSegregado is
7754 v_fatorOrigem number;
7755 v_fatorDestino number;
7756 begin
7757 for r in (select g.idproduto, g.iddepositante
7758 from gtt_formarloteremanejamento g, produtodepositante pd
7759 where pd.tipopicking = C_PRODUTO_SEGREGADO
7760 and pd.idproduto = g.idproduto
7761 and pd.identidade = g.iddepositante
7762 group by g.idproduto, g.iddepositante)
7763 loop
7764
7765 select l.fatorconversao
7766 into v_fatorOrigem
7767 from lotelocal lo, lote l
7768 where lo.idlocal = r_remanejamento.idlocalorigem
7769 and lo.idarmazem = r_remanejamento.idarmazemorigem
7770 and l.idproduto = r.idproduto
7771 and l.idlote = lo.idlote
7772 and rownum = 1;
7773
7774 begin
7775 select l.fatorconversao
7776 into v_fatorDestino
7777 from lotelocal ld, lote l
7778 where ld.idlocal = r_remanejamento.idlocaldestino
7779 and ld.idarmazem = r_remanejamento.idarmazemdestino
7780 and l.idproduto = r.idproduto
7781 and l.idlote = ld.idlote
7782 and rownum = 1;
7783 exception
7784 when no_data_found then
7785 v_fatorDestino := 0;
7786 end;
7787
7788 if (v_fatorDestino > 0 and v_fatorDestino <> v_fatorOrigem) then
7789 v_msg := t_message('Local de origem e local de destino, devem possuir lotes ' ||
7790 'com mesmo fator, para produtos segregados. Produto sendo ' ||
7791 'remanejado {0}');
7792 v_msg.addParam(p_idremanejamento);
7793 raise_application_error(-20000, v_msg.formatMessage);
7794 end if;
7795 end loop;
7796 end validarProdutoSegregado;
7797 begin
7798 if (r_remanejamento.planejado = 'S') then
7799 v_msg := t_message('O remanejamento {0}' ||
7800 ' já possui planejamento finalizado.');
7801 v_msg.addParam(p_idremanejamento);
7802 raise_application_error(-20000, v_msg.formatMessage);
7803 end if;
7804
7805 select count(*)
7806 into v_jaPossuiLoteVinculado
7807 from loteremanejamento lr
7808 where lr.idremanejamento = p_idremanejamento;
7809
7810 if (v_jaPossuiLoteVinculado > 0) then
7811 v_msg := t_message('O remanejamento {0}' ||
7812 ' já possui lotes gerados.');
7813 v_msg.addParam(p_idremanejamento);
7814 raise_application_error(-20000, v_msg.formatMessage);
7815 end if;
7816
7817 select count(1)
7818 into v_caixaNaoInformada
7819 from armazem a
7820 where a.idarmazem = r_remanejamento.idarmazemorigem
7821 and a.indicaqtdecaixaremanejamento = 1
7822 and exists (select 1
7823 from gtt_formarloteremanejamento g
7824 where g.idtipocaixa is null);
7825
7826 if (v_caixaNaoInformada = 1) then
7827 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.');
7828 raise_application_error(-20000, v_msg.formatMessage);
7829 end if;
7830
7831 validarProdutoSegregado;
7832
7833 -- validar lote unico no destino
7834 for r_produto in (select g.iddepositante, g.idproduto, g.estado,
7835 g.loteindustria, g.dtvenc,
7836 pd.loteuniconoendereco
7837 from gtt_formarloteremanejamento g,
7838 produtodepositante pd
7839 where pd.idproduto = g.idproduto
7840 and pd.identidade = g.iddepositante)
7841 loop
7842 r_loteunicocomparar.idProduto := r_produto.idproduto;
7843 r_loteunicocomparar.estado := r_produto.estado;
7844 r_loteunicocomparar.loteindustria := r_produto.loteindustria;
7845 r_loteunicocomparar.dtvencimento := r_produto.dtvenc;
7846 r_loteunicocomparar.loteuniconoendereco := r_produto.loteuniconoendereco;
7847 r_loteunicocomparar.iddepositante := r_produto.iddepositante;
7848
7849 if (not pk_lote.isLocalPodeReceberLoteUnico(r_loteunicocomparar,
7850 r_remanejamento.idarmazemdestino,
7851 r_remanejamento.idlocaldestino,
7852 v_msgerro)) then
7853 raise_application_error(-20000, v_msgerro);
7854 end if;
7855 end loop;
7856
7857 select count(*)
7858 into v_existeLoteSemPeso
7859 from dual
7860 where exists (select 1
7861 from gtt_formarloteremanejamento g
7862 where g.idremanejamento = p_idremanejamento
7863 and g.peso is null
7864 and exists
7865 (select 1
7866 from configuracao c
7867 where c.ativo = 'S'
7868 and c.pesagemremanejamento = 'S'));
7869
7870 if (v_existeLoteSemPeso = 1) then
7871 v_msg := t_message('A configuração geral esta parâmetrizada para que os produtos sejam pesados, porém existem produtos não pesados.');
7872 raise_application_error(-20000, v_msg.formatMessage);
7873 end if;
7874
7875 select count(*)
7876 into v_existeInfoEspecNaoInformada
7877 from dual
7878 where exists
7879 (select 1
7880 from gtt_formarloteremanejamento g
7881 where g.idloteinfoespec is null
7882 and exists
7883 (select 1
7884 from produtodepositante pd
7885 where pd.identidade = g.iddepositante
7886 and pd.idproduto = g.idproduto
7887 and pd.rastrearinfoespecifica = 0
7888 and exists
7889 (select 1
7890 from informacaomatdep im
7891 where im.identidade = pd.identidade
7892 and im.idproduto = pd.idproduto)));
7893
7894 if (v_existeInfoEspecNaoInformada = 1) then
7895 v_msg := t_message('Todos os produtos que trabalham com informação especifica devem ser especificados.');
7896 raise_application_error(-20000, v_msg.formatMessage);
7897 end if;
7898 end validacoes;
7899
7900 procedure formarLotes is
7901 cursor c_produto is
7902 select g.idproduto, g.estado, g.iddepositante, g.itemadicional,
7903 g.loteindustria, g.dtvenc, g.dtfabricacao, g.qtde,
7904 e.fatorconversao, e.lastro, e.qtdecamada, g.idtipocaixa,
7905 g.loteadicional, g.idloteinfoespec, g.peso, g.barra,
7906 g.liberado, g.idusuariobloqueio, g.motivobloqueio,
7907 g.idmotivobloqueio, g.databloqueio, g.hashcomplotekit,
7908 e.caixafechada
7909 from gtt_formarloteremanejamento g, embalagem e
7910 where e.barra = g.barra
7911 and e.idproduto = g.idproduto;
7912
7913 r_produto c_produto%rowtype;
7914
7915 v_metodoger_incompl_sobra number;
7916 v_dispCoberturaAntes number;
7917 v_dispCoberturaDepois number;
7918
7919 procedure moverSeparacaoEspecifica
7920 (
7921 p_idloteOrigem in number,
7922 p_idarmazemorigem in number,
7923 p_idlocalorigem in varchar2,
7924 p_qtde in number,
7925 p_idloteDestino in number,
7926 p_idarmazemDestino in number,
7927 p_idlocalDestino in varchar2
7928 ) is
7929 r_sepNova separacaoespecifica%rowtype;
7930 r_seploteindsepespecif seploteindsepespecif%rowtype;
7931 v_idSepLoteInd number;
7932 begin
7933 for r_sepespecifica in (select *
7934 from separacaoespecifica se
7935 where se.idarmazem = p_idarmazemorigem
7936 and se.idlocal = p_idlocalorigem
7937 and se.idlote = p_idloteOrigem
7938 and exists
7939 (select 1
7940 from notafiscal nf
7941 where nf.tipo = 'S'
7942 and nf.statusnf in
7943 ('N', 'C', 'I', 'B')
7944 and (nf.statusroteirizacao = 0 or
7945 nf.statusroteirizacao = 1)
7946 and se.idnotafiscal =
7947 nf.idnotafiscal))
7948 loop
7949
7950 if ((r_sepespecifica.qtde - p_qtde) > 0) then
7951 update separacaoespecifica se
7952 set se.qtde = se.qtde - p_qtde
7953 where se.id = r_sepespecifica.id;
7954 pk_utilities.GeraLog(p_idUsuario,
7955 'Removeu a quantidade ' || p_qtde ||
7956 ' da separação especifica Id: ' ||
7957 r_sepespecifica.id ||
7958 ' por remanejamento picking para pulmão, remanejamento Id: ' ||
7959 p_idremanejamento, r_sepespecifica.id,
7960 'SE');
7961 end if;
7962
7963 begin
7964 select *
7965 into r_sepNova
7966 from separacaoespecifica se
7967 where se.idarmazem = p_idarmazemdestino
7968 and se.idlocal = p_idlocaldestino
7969 and se.idlote = p_idloteDestino
7970 and se.idnotafiscal = r_sepespecifica.idnotafiscal;
7971
7972 r_sepNova.Qtde := r_sepNova.qtde + p_qtde;
7973
7974 update separacaoespecifica
7975 set row = r_sepNova
7976 where id = r_sepNova.id;
7977
7978 pk_utilities.GeraLog(p_idUsuario,
7979 'Adicionou a quantidade ' || p_qtde ||
7980 ' a separação especifica Id: ' ||
7981 r_sepNova.id ||
7982 ' por remanejamento picking para pulmão, remanejamento Id: ' ||
7983 p_idremanejamento, r_sepNova.id, 'SE');
7984
7985 exception
7986 when no_data_found then
7987 select seq_separacaoespecifica.nextval
7988 into r_sepNova.id
7989 from dual;
7990 r_sepNova.idarmazem := p_idarmazemdestino;
7991 r_sepNova.idlocal := p_idlocaldestino;
7992 r_sepNova.idlote := p_idloteDestino;
7993 r_sepNova.idnotafiscal := r_sepespecifica.idnotafiscal;
7994 r_sepNova.qtde := p_qtde;
7995 r_sepNova.cadmanual := r_sepespecifica.cadmanual;
7996 r_sepNova.fonteseparacao := r_sepespecifica.fonteseparacao;
7997 r_sepNova.idusuario := p_idUsuario;
7998 r_sepNova.dtvinculo := sysdate;
7999
8000 insert into separacaoespecifica
8001 (id, idarmazem, idlocal, idlote, idnotafiscal, qtde,
8002 cadmanual, fonteseparacao, idusuario, dtvinculo)
8003 values
8004 (r_sepNova.id, r_sepNova.idarmazem, r_sepNova.idlocal,
8005 r_sepNova.idlote, r_sepNova.idnotafiscal, r_sepNova.qtde,
8006 r_sepNova.cadmanual, r_sepNova.fonteseparacao,
8007 r_sepNova.Idusuario, r_sepNova.Dtvinculo);
8008
8009 pk_utilities.GeraLog(p_idUsuario,
8010 'Criou a separação especifica Id: ' ||
8011 r_sepNova.id || ' com quantidade ' ||
8012 r_sepNova.qtde ||
8013 ' para Nota Fiscal Id: ' ||
8014 r_sepNova.Idnotafiscal ||
8015 ' para o Lote Id: ' || r_sepNova.Idlote ||
8016 ' no Local ID: ' || r_sepNova.Idlocal ||
8017 ' no Armazém Id: ' ||
8018 r_sepNova.Idarmazem ||
8019 ' por remanejamento picking para pulmão, remanejamento Id: ' ||
8020 p_idremanejamento, r_sepNova.id, 'SE');
8021 end;
8022
8023 begin
8024 select sls.idseploteind
8025 into v_idSepLoteInd
8026 from seploteindsepespecif sls
8027 where sls.idsepespecifica = r_sepespecifica.id;
8028
8029 begin
8030 select *
8031 into r_seploteindsepespecif
8032 from seploteindsepespecif s
8033 where s.idseploteind = v_idSepLoteInd
8034 and s.idsepespecifica = r_sepNova.id;
8035
8036 r_seploteindsepespecif.qtde := r_seploteindsepespecif.qtde +
8037 p_qtde;
8038
8039 update seploteindsepespecif r
8040 set row = r_seploteindsepespecif
8041 where r.idsepespecifica =
8042 r_seploteindsepespecif.idsepespecifica
8043 and r.idseploteind = r_seploteindsepespecif.idseploteind;
8044 exception
8045 when no_data_found then
8046 insert into seploteindsepespecif
8047 (idsepespecifica, idseploteind, idlote, qtde)
8048 values
8049 (r_sepNova.id, v_idSepLoteInd, r_sepNova.idlote,
8050 r_sepNova.qtde);
8051 end;
8052
8053 if ((r_sepespecifica.qtde - p_qtde) > 0) then
8054 update seploteindsepespecif s
8055 set s.qtde = s.qtde - p_qtde
8056 where s.idsepespecifica = r_sepespecifica.id;
8057
8058 else
8059 delete from seploteindsepespecif s
8060 where s.idsepespecifica = r_sepespecifica.id
8061 and s.idseploteind = v_idSepLoteInd;
8062 end if;
8063 exception
8064 when no_data_found then
8065 v_idSepLoteInd := null;
8066 end;
8067
8068 if ((r_sepespecifica.qtde - p_qtde) <= 0) then
8069 delete from separacaoespecifica se
8070 where se.id = r_sepespecifica.id;
8071
8072 pk_utilities.GeraLog(p_idUsuario,
8073 'Removeu a separação especifica Id: ' ||
8074 r_sepespecifica.id || ' com quantidade ' ||
8075 r_sepespecifica.qtde ||
8076 ' para Nota Fiscal Id: ' ||
8077 r_sepespecifica.Idnotafiscal ||
8078 ' para o Lote Id: ' ||
8079 r_sepespecifica.Idlote || ' no Local ID: ' ||
8080 r_sepespecifica.Idlocal ||
8081 ' no Armazém Id: ' ||
8082 r_sepespecifica.Idarmazem ||
8083 ' por remanejamento picking para pulmão, remanejamento Id: ' ||
8084 p_idremanejamento, r_sepespecifica.id,
8085 'SE');
8086 end if;
8087 end loop;
8088 end moverSeparacaoEspecifica;
8089
8090 procedure addNovoLoteRemanejamento
8091 (
8092 p_idlote in number,
8093 p_qtde in number,
8094 p_peso in number
8095 ) is
8096 v_complementar lotelocal.complementar%type;
8097
8098 begin
8099 insert into loteremanejamento
8100 (idremanejamento, idlote, qtde, conferido, controlaqtde,
8101 diferenca, liberado, impetiqueta, peso)
8102 values
8103 (p_idremanejamento, p_idlote, p_qtde, 'N', 'S', 0, 'S', 'S',
8104 p_peso);
8105
8106 if (p_peso is not null) then
8107 update lote lt
8108 set lt.pesobruto = p_peso
8109 where lt.idlote = p_idlote;
8110 end if;
8111
8112 v_complementar := 'ESTOQUE ADICIONADO EM FUNCAO DA FINALIZACAO DO CADASTRO DE LOTE DO REMANEJAMENTO N:' ||
8113 p_idremanejamento;
8114 pk_estoque.incluir_estoque(r_remanejamento.idarmazemorigem,
8115 r_remanejamento.idlocalorigem, p_idlote,
8116 p_qtde, p_idusuario, v_complementar, 'N');
8117
8118 v_complementar := 'AUMENTADO PENDENCIA EM FUNCAO DA INCLUSAO DO LOTE NO REMANEJAMENTO N: ' ||
8119 p_idremanejamento;
8120 pk_estoque.incluir_pendencia(r_remanejamento.idarmazemorigem,
8121 r_remanejamento.idlocalorigem, p_idlote,
8122 p_qtde, p_idusuario, v_complementar);
8123
8124 v_complementar := 'AUMENTADO ADICIONAR EM FUNCAO DA INCLUSAO DO LOTE NO REMANEJAMENTO N: ' ||
8125 p_idremanejamento;
8126 pk_estoque.incluir_adicionar(r_remanejamento.idarmazemorigem,
8127 r_remanejamento.idlocaldestino,
8128 p_idlote, p_qtde, p_idusuario,
8129 v_complementar);
8130
8131 end addNovoLoteRemanejamento;
8132
8133 procedure usarLoteUnitario(r_produto in c_produto%rowtype) is
8134 v_complementar lotelocal.complementar%type;
8135
8136 begin
8137 insert into loteremanejamento
8138 (idremanejamento, idlote, qtde, conferido, controlaqtde,
8139 diferenca, liberado, impetiqueta, datapesagem, peso)
8140 values
8141 (p_idremanejamento, r_produto.idloteinfoespec, 1, 'N', 'S', 0,
8142 'S', 'S', sysdate, r_produto.peso);
8143
8144 if (r_produto.peso is not null) then
8145 update lote lt
8146 set lt.pesobruto = r_produto.peso
8147 where lt.idlote = r_produto.idloteinfoespec;
8148 end if;
8149
8150 v_complementar := 'AUMENTADO PENDENCIA EM FUNCAO DA INCLUSAO DO LOTE NO REMANEJAMENTO N: ' ||
8151 p_idremanejamento;
8152 pk_estoque.incluir_pendencia(r_remanejamento.idarmazemorigem,
8153 r_remanejamento.idlocalorigem,
8154 r_produto.idloteinfoespec, 1,
8155 p_idusuario, v_complementar);
8156
8157 v_complementar := 'AUMENTADO ADICIONAR EM FUNCAO DA INCLUSAO DO LOTE NO REMANEJAMENTO N: ' ||
8158 p_idremanejamento;
8159 pk_estoque.incluir_adicionar(r_remanejamento.idarmazemorigem,
8160 r_remanejamento.idlocaldestino,
8161 r_produto.idloteinfoespec, 1,
8162 p_idusuario, v_complementar);
8163
8164 moverSeparacaoEspecifica(r_produto.idloteinfoespec,
8165 r_remanejamento.idarmazemorigem,
8166 r_remanejamento.idlocalorigem, 1,
8167 r_produto.idloteinfoespec,
8168 r_remanejamento.idarmazemorigem,
8169 r_remanejamento.idlocaldestino);
8170
8171 end usarLoteUnitario;
8172
8173 procedure criarNovosLotes(r_produto in c_produto%rowtype) is
8174
8175 v_qtdeUN number;
8176
8177 procedure inserirNovosLotes is
8178 r_lote lote%rowtype;
8179
8180 v_qtdeTotalCaixas number;
8181 v_normaPallet number;
8182 v_qtdePallet number;
8183 v_caixasPallet number;
8184 v_unidadesPallet number;
8185 v_qtdeLastro number;
8186 v_caixasLastro number;
8187 v_unidadesLastro number;
8188 v_qtdeCaixas number;
8189 v_unidadesCaixas number;
8190 v_qtdeUnidades number;
8191 v_idlote number;
8192 begin
8193 pk_lote.CalcularQtdeLotes(v_qtdeUN, r_produto.fatorconversao,
8194 r_produto.lastro, r_produto.qtdecamada,
8195 v_metodoger_incompl_sobra,
8196 r_produto.caixafechada,
8197 v_qtdeTotalCaixas, v_normaPallet,
8198 v_qtdePallet, v_caixasPallet,
8199 v_unidadesPallet, v_qtdeLastro,
8200 v_caixasLastro, v_unidadesLastro,
8201 v_qtdeCaixas, v_unidadesCaixas,
8202 v_qtdeUnidades);
8203
8204 -- Informando os dados do Lote
8205 r_lote.idproduto := r_produto.idproduto;
8206 r_lote.iddepositante := r_produto.iddepositante;
8207 r_lote.descr := r_produto.loteindustria;
8208 r_lote.dtVenc := r_produto.dtvenc;
8209 r_lote.estado := r_produto.estado;
8210 r_lote.idUsuario := p_idusuario;
8211 r_lote.tipolote := 'L';
8212 r_lote.situacao := 'R';
8213 r_lote.numerovolume := 1;
8214 r_lote.itemadicional := r_produto.itemadicional;
8215 r_lote.idtipocaixa := r_produto.idtipocaixa;
8216 r_lote.idarmazem := r_remanejamento.idarmazemorigem;
8217 r_lote.dtfabricacao := r_produto.dtfabricacao;
8218 r_lote.loteadicional := r_produto.loteadicional;
8219 r_lote.idusuariobloqueio := r_produto.idusuariobloqueio;
8220 r_lote.motivobloqueio := r_produto.motivobloqueio;
8221 r_lote.idmotivobloqueio := r_produto.idmotivobloqueio;
8222 r_lote.databloqueio := r_produto.databloqueio;
8223 r_lote.hashcomplotekit := r_produto.hashcomplotekit;
8224
8225 if (pk_lote.isLoteIndustriaComMaisDeUmVenc(r_produto.iddepositante,
8226 r_produto.idproduto,
8227 r_produto.loteindustria,
8228 r_produto.dtvenc)) then
8229 raise_application_error(-20000,
8230 'Não é possÃvel cadastrar lote do mesmo produto/depositante com o mesmo lote indústria e data de vencimento diferentes. Depositante:' ||
8231 r_produto.iddepositante ||
8232 '. Produto: ' || r_produto.idproduto ||
8233 ' Lote Indústria: ' ||
8234 r_produto.loteindustria ||
8235 ' Data Venc: ' || r_produto.dtvenc);
8236
8237 end if;
8238
8239 -- Gerando o Lote Pallet
8240 for i in 1 .. v_qtdePallet
8241 loop
8242 r_lote.barra := r_produto.barra;
8243 r_lote.qtdeEntrada := v_normaPallet;
8244 r_lote.tipoPalet := 'C';
8245 v_idlote := pk_lote.cadastrar_lote(r_lote, null, 'N');
8246
8247 if (r_produto.liberado = 'N') then
8248 update lote lt
8249 set lt.liberado = 'N'
8250 where lt.idlote = v_idlote;
8251 end if;
8252
8253 addNovoLoteRemanejamento(v_idlote,
8254 v_normaPallet *
8255 r_produto.fatorconversao,
8256 r_produto.peso);
8257 end loop;
8258
8259 -- Gerando o Lote Lastro
8260 if v_qtdeLastro > 0 then
8261 r_lote.barra := r_produto.barra;
8262 r_lote.qtdeEntrada := v_caixasLastro;
8263 r_lote.tipoPalet := 'I';
8264 v_idlote := pk_lote.cadastrar_lote(r_lote, null, 'N');
8265
8266 if (r_produto.liberado = 'N') then
8267 update lote lt
8268 set lt.liberado = 'N'
8269 where lt.idlote = v_idlote;
8270 end if;
8271
8272 addNovoLoteRemanejamento(v_idlote,
8273 v_caixasLastro *
8274 r_produto.fatorconversao,
8275 r_produto.peso);
8276 end if;
8277
8278 -- Gerando o Lote Quebrado - Sobra
8279 if v_qtdeCaixas > 0 then
8280 r_lote.barra := r_produto.barra;
8281 r_lote.qtdeEntrada := v_qtdeCaixas;
8282 r_lote.tipoPalet := 'S';
8283 v_idlote := pk_lote.cadastrar_lote(r_lote, null, 'N');
8284
8285 if (r_produto.liberado = 'N') then
8286 update lote lt
8287 set lt.liberado = 'N'
8288 where lt.idlote = v_idlote;
8289 end if;
8290
8291 addNovoLoteRemanejamento(v_idlote,
8292 v_qtdeCaixas *
8293 r_produto.fatorconversao,
8294 r_produto.peso);
8295 end if;
8296
8297 -- Gerando o Lote Quebrado - Unidades
8298 if v_qtdeUnidades > 0 then
8299 r_lote.barra := pk_produto.RetornarCodBarraMenorFator(r_produto.idproduto);
8300 r_lote.qtdeEntrada := v_qtdeUnidades;
8301 r_lote.tipoPalet := 'U';
8302 v_idlote := pk_lote.cadastrar_lote(r_lote, null, 'N');
8303
8304 if (r_produto.liberado = 'N') then
8305 update lote lt
8306 set lt.liberado = 'N'
8307 where lt.idlote = v_idlote;
8308 end if;
8309
8310 addNovoLoteRemanejamento(v_idlote, v_qtdeUnidades,
8311 r_produto.peso);
8312 end if;
8313 end inserirNovosLotes;
8314
8315 procedure consumirLotesAntigos is
8316 cursor c_loteantigo
8317 (
8318 pc_idarmazem number,
8319 pc_idlocal local.idlocal%type,
8320 pc_idproduto number,
8321 pc_estado lote.estado%type,
8322 pc_iddepositante number,
8323 pc_itemadicional lote.itemadicional%type,
8324 pc_dtvenc date,
8325 pc_dtfabricacao date,
8326 pc_loteindustria lote.descr%type,
8327 pc_loteadicional lote.loteadicional%type,
8328 pc_hashcomplotekit lote.hashcomplotekit%type
8329 ) is
8330 select ll.idarmazem, l.idlote,
8331 (ll.estoque - ll.pendencia) disponivelestoque,
8332 l.qtdedisponivel qtdecoberta
8333 from lotelocal ll, lote l
8334 where ll.idarmazem = pc_idarmazem
8335 and ll.idlocal = pc_idlocal
8336 and (ll.estoque - ll.pendencia + ll.adicionar) > 0
8337 and l.idlote = ll.idlote
8338 and l.idproduto = pc_idproduto
8339 and l.estado = pc_estado
8340 and l.iddepositante = pc_iddepositante
8341 and case
8342 when pc_itemadicional is null then
8343 1
8344 else
8345 decode(l.itemadicional, pc_itemadicional, 1, 0)
8346 end = 1
8347 and decode(trunc(l.dtvenc), trunc(pc_dtvenc), 1, 0) = 1
8348 and case
8349 when pc_dtfabricacao is null then
8350 1
8351 else
8352 decode(trunc(l.dtfabricacao), trunc(pc_dtfabricacao), 1,
8353 0)
8354 end = 1
8355 and decode(l.descr, pc_loteindustria, 1,
8356 decode(l.descr, 'LOTE DE PICKING PARA PULMÃO', 1, 0)) = 1
8357 and case
8358 when pc_loteadicional is null then
8359 1
8360 else
8361 decode(l.loteadicional, pc_loteadicional, 1, 0)
8362 end = 1
8363 and case
8364 when pc_hashcomplotekit is null then
8365 1
8366 else
8367 decode(l.hashcomplotekit, pc_hashcomplotekit, 1, 0)
8368 end = 1
8369 order by l.dtalocacao;
8370
8371 r_loteantigo c_loteantigo%rowtype;
8372 v_qtdeUtilizadaUN number;
8373 v_qtdeCobertaLoteAntigo number;
8374 v_complementar varchar2(200);
8375 begin
8376 v_qtdeUtilizadaUN := 0;
8377 for r_loteantigo in c_loteantigo(r_remanejamento.idarmazemorigem,
8378 r_remanejamento.idlocalorigem,
8379 r_produto.idproduto,
8380 r_produto.estado,
8381 r_produto.iddepositante,
8382 r_produto.itemadicional,
8383 r_produto.dtvenc,
8384 r_produto.dtfabricacao,
8385 r_produto.loteindustria,
8386 r_produto.loteadicional,
8387 r_produto.hashcomplotekit)
8388 loop
8389 if (r_loteantigo.disponivelestoque >= v_qtdeUN) then
8390 v_qtdeUtilizadaUN := v_qtdeUN;
8391 else
8392 v_qtdeUtilizadaUN := r_loteantigo.disponivelestoque;
8393 end if;
8394
8395 v_qtdeUN := v_qtdeUN - v_qtdeUtilizadaUN;
8396
8397 v_complementar := 'ESTOQUE RETIRADO EM FUNCAO DA FINALIZACAO DO CADASTRO DE LOTE DO REMANEJAMENTO N:' ||
8398 p_idremanejamento;
8399
8400 pk_estoque.retirar_estoque(r_loteantigo.idarmazem,
8401 r_remanejamento.idlocalorigem,
8402 r_loteantigo.idlote,
8403 v_qtdeUtilizadaUN, p_idusuario,
8404 v_complementar, 'N');
8405
8406 InserirComposicaoLote(p_idremanejamento, r_loteantigo.idlote,
8407 v_qtdeUtilizadaUN,
8408 r_loteantigo.qtdecoberta);
8409
8410 exit when v_qtdeUN = 0;
8411 end loop;
8412
8413 if (v_qtdeUN > 0) then
8414 v_msg := t_message('Não foi possivel encontrar estoque suficiente para o produto.' ||
8415 chr(13) || 'IDPRODUTO: {0}' || chr(13) ||
8416 'QTDENECESSARIA UN: {1}');
8417 v_msg.addParam(r_produto.idproduto);
8418 v_msg.addParam(r_produto.qtde * r_produto.fatorconversao);
8419 raise_application_error(-20000, v_msg.formatMessage);
8420 end if;
8421
8422 end consumirLotesAntigos;
8423
8424 procedure tratarModuloMWMS is
8425 begin
8426
8427 for r_lotenovo in (select lr.idlote
8428 from loteremanejamento lr
8429 where lr.idremanejamento = p_idremanejamento)
8430 loop
8431 update lote lt
8432 set (lt.dtfabricacao, lt.loteadicional, lt.itemadicional) =
8433 (select la.dtfabricacao, la.loteadicional,
8434 la.itemadicional
8435 from composicaolote cl, lote la
8436 where cl.idlotenovo = lt.idlote
8437 and la.idlote = cl.idloteanterior
8438 and rownum = 1)
8439 where lt.idlote = r_lotenovo.idlote;
8440 end loop;
8441
8442 end tratarModuloMWMS;
8443
8444 begin
8445 v_qtdeUN := r_produto.qtde * r_produto.fatorconversao;
8446
8447 inserirNovosLotes;
8448
8449 consumirLotesAntigos;
8450
8451 if (p_moduloSistema = MODULO_MWMS) then
8452 tratarModuloMWMS;
8453 end if;
8454
8455 end criarNovosLotes;
8456
8457 function getQtdeDispCobertura return number is
8458 v_qtde number;
8459 begin
8460 select sum(lt.qtdedisponivel)
8461 into v_qtde
8462 from lotelocal ll, lote lt
8463 where ll.idarmazem = r_remanejamento.idarmazemorigem
8464 and ll.idlocal = r_remanejamento.idlocalorigem
8465 and lt.idlote = ll.idlote;
8466
8467 return v_qtde;
8468 end getQtdeDispCobertura;
8469
8470 procedure moverSepEspecLotesNovos is
8471 v_qtdeMover number;
8472 v_qtdeEstoqueDispLtAnt number;
8473 v_qtdeEmSepEspec number;
8474 begin
8475 for r_lt_spEspecifica in (select lr.idlote idLoteNovo,
8476 cl.qtde qtdeFormadaLtNovo,
8477 cl.idloteanterior
8478 from loteremanejamento lr,
8479 composicaolote cl
8480 where lr.idremanejamento =
8481 p_idremanejamento
8482 and cl.idlotenovo = lr.idlote
8483 and exists
8484 (select 1
8485 from separacaoespecifica sep
8486 where sep.idlote =
8487 cl.idloteanterior
8488 and sep.idlocal =
8489 r_remanejamento.idlocalorigem
8490 and sep.idarmazem =
8491 r_remanejamento.idarmazemorigem
8492 and exists
8493 (select 1
8494 from notafiscal nf
8495 where nf.tipo = 'S'
8496 and nf.statusnf in
8497 ('N', 'C', 'I', 'B')
8498 and (nf.statusroteirizacao = 0 or
8499 nf.statusroteirizacao = 1)
8500 and sep.idnotafiscal =
8501 nf.idnotafiscal)))
8502 loop
8503 select sum(ll.estoque + ll.adicionar - ll.pendencia) qtde
8504 into v_qtdeEstoqueDispLtAnt
8505 from lotelocal ll
8506 where ll.idarmazem = r_remanejamento.idarmazemorigem
8507 and ll.idlocal = r_remanejamento.idlocalorigem
8508 and ll.idlote = r_lt_spEspecifica.Idloteanterior
8509 group by ll.idarmazem, ll.idlocal, ll.idlote;
8510
8511 select sum(sep.qtde)
8512 into v_qtdeEmSepEspec
8513 from separacaoespecifica sep
8514 where sep.idlote = r_lt_spEspecifica.Idloteanterior
8515 and sep.idarmazem = r_remanejamento.idarmazemorigem
8516 and sep.idlocal = r_remanejamento.idlocalorigem
8517 and exists
8518 (select 1
8519 from notafiscal nf
8520 where nf.tipo = 'S'
8521 and nf.statusnf in ('N', 'C', 'I', 'B')
8522 and (nf.statusroteirizacao = 0 or
8523 nf.statusroteirizacao = 1)
8524 and sep.idnotafiscal = nf.idnotafiscal)
8525 group by sep.idlote, sep.idlocal, sep.idarmazem;
8526
8527 if (v_qtdeEstoqueDispLtAnt >= v_qtdeEmSepEspec) then
8528 v_qtdeMover := 0;
8529 end if;
8530
8531 if (v_qtdeEstoqueDispLtAnt < v_qtdeEmSepEspec) then
8532 if (r_lt_spEspecifica.Qtdeformadaltnovo <=
8533 (v_qtdeEmSepEspec - v_qtdeEstoqueDispLtAnt)) then
8534 v_qtdeMover := r_lt_spEspecifica.Qtdeformadaltnovo;
8535 end if;
8536
8537 if (r_lt_spEspecifica.Qtdeformadaltnovo >
8538 (v_qtdeEmSepEspec - v_qtdeEstoqueDispLtAnt)) then
8539 v_qtdeMover := r_lt_spEspecifica.Qtdeformadaltnovo -
8540 (v_qtdeEmSepEspec - v_qtdeEstoqueDispLtAnt);
8541 end if;
8542 end if;
8543
8544 if (v_qtdeMover > 0) then
8545 moverSeparacaoEspecifica(r_lt_spEspecifica.Idloteanterior,
8546 r_remanejamento.idarmazemorigem,
8547 r_remanejamento.idlocalorigem,
8548 v_qtdeMover,
8549 r_lt_spEspecifica.Idlotenovo,
8550 r_remanejamento.idarmazemdestino,
8551 r_remanejamento.idlocaldestino);
8552 end if;
8553
8554 end loop;
8555 end moverSepEspecLotesNovos;
8556
8557 begin
8558
8559 select ar.metodogerlotesobraincompl
8560 into v_metodoger_incompl_sobra
8561 from armazem ar
8562 where ar.idarmazem = r_remanejamento.idarmazemorigem;
8563
8564 v_dispCoberturaAntes := getQtdeDispCobertura;
8565
8566 for r_produto in c_produto
8567 loop
8568
8569 if (r_produto.idloteinfoespec is null) then
8570 criarNovosLotes(r_produto);
8571 else
8572 usarLoteUnitario(r_produto);
8573 end if;
8574
8575 end loop;
8576
8577 moverSepEspecLotesNovos;
8578
8579 v_dispCoberturaDepois := getQtdeDispCobertura;
8580
8581 if (v_dispCoberturaAntes <> v_dispCoberturaDepois) then
8582 v_msg := t_message('A quantidade disponivel de cobertura não deve ser alterada por remanejamento de picking para pulmão.');
8583 raise_application_error(-20000, v_msg.formatMessage);
8584 end if;
8585
8586 end formarLotes;
8587
8588 procedure controlarPesagemLotes is
8589 v_pesar configuracao.pesagemremanejamento%type;
8590 begin
8591 select c.pesagemremanejamento
8592 into v_pesar
8593 from configuracao c
8594 where c.ativo = 'S';
8595
8596 if (v_pesar = 'N') then
8597 for r_lote in (select lr.idlote, (e.pesobruto * lr.qtde) pesoteorico
8598 from loteremanejamento lr, lote l, embalagem e
8599 where e.barra = l.barra
8600 and e.idproduto = l.idproduto
8601 and l.idlote = lr.idlote
8602 and lr.idremanejamento = p_idremanejamento
8603 and not exists
8604 (select 1
8605 from gtt_formarloteremanejamento g
8606 where g.idloteinfoespec = lr.idlote))
8607 loop
8608 update loteremanejamento
8609 set liberado = 'S',
8610 peso = r_lote.pesoteorico,
8611 datapesagem = sysdate
8612 where idremanejamento = p_idremanejamento
8613 and idlote = r_lote.idlote;
8614 end loop;
8615 end if;
8616
8617 end controlarPesagemLotes;
8618
8619 procedure valorarLotes is
8620 begin
8621 for c_loteremanejamento in (select lr.idlote
8622 from loteremanejamento lr
8623 where lr.idremanejamento =
8624 p_idremanejamento
8625 and not exists
8626 (select 1
8627 from gtt_formarloteremanejamento g
8628 where g.idloteinfoespec =
8629 lr.idlote))
8630 loop
8631 pk_lote.valorarLotePeloLoteAnterior(c_loteremanejamento.idlote);
8632
8633 pk_lote.atualizaInfLote(c_loteremanejamento.idlote,
8634 C_MOV_CADASTRO_LOTE);
8635 end loop;
8636 end valorarLotes;
8637
8638 procedure apagarPickingDinamico is
8639 begin
8640 for r_picking in (select distinct lt.idproduto, lt.iddepositante
8641 from loteremanejamento lr, lote lt
8642 where lr.idremanejamento = p_idremanejamento
8643 and lt.idlote = lr.idlote)
8644 loop
8645 pk_picking_dinamico.addGttPickingDinamico(r_picking.idproduto,
8646 r_picking.iddepositante,
8647 r_remanejamento.idlocalorigem,
8648 r_remanejamento.idarmazemorigem,
8649 0);
8650
8651 end loop;
8652
8653 pk_picking_dinamico.deletar_picking_dinamico;
8654 end apagarPickingDinamico;
8655
8656 begin
8657
8658 select *
8659 into r_remanejamento
8660 from remanejamento r
8661 where r.idremanejamento = p_idremanejamento
8662 for update;
8663
8664 validacoes;
8665
8666 formarLotes;
8667
8668 controlarPesagemLotes;
8669
8670 valorarLotes;
8671
8672 apagarPickingDinamico;
8673
8674 update remanejamento
8675 set planejado = 'S'
8676 where idremanejamento = p_idremanejamento;
8677
8678 pk_utilities.GeraLog(p_idusuario,
8679 'LOTES FORMADOS DE PICKING PARA PULMÃO (MODULO SILTWMS). ' ||
8680 chr(13) || 'IDREMANEJAMENTO: ' ||
8681 p_idremanejamento, p_idremanejamento, 'R');
8682
8683 for c_histContratoLote in (select cl.idlote, cl.idcontrato, cl.prazotroca,
8684 cl.status
8685 from loteremanejamento lr, contratolote cl
8686 where lr.idremanejamento = p_idRemanejamento
8687 and cl.idlote = lr.idlote)
8688
8689 loop
8690 if c_histContratoLote.Idcontrato is null then
8691 v_mgsContratoLote := 'INCLUÃDO REGISTRO PARA VINCULAR CONTRATO AO LOTE ID: ' ||
8692 c_histContratoLote.Idlote ||
8693 ' EM FUNÇÃO DO REMANEJAMENTO ID: ' ||
8694 p_idRemanejamento;
8695 else
8696 v_mgsContratoLote := 'VINCULADO O LOTE ID: ' ||
8697 c_histContratoLote.Idlote ||
8698 ' AO CONTRATO ID: ' ||
8699 c_histContratoLote.Idcontrato ||
8700 ' EM FUNÇÃO DO REMANEJAMENTO ID: ' ||
8701 p_idRemanejamento;
8702 end if;
8703 pk_contrato.historicoMudancaContratoLote(c_histContratoLote.Idcontrato,
8704 c_histContratoLote.Idlote,
8705 c_histContratoLote.Prazotroca,
8706 p_idusuario, 1,
8707 v_mgsContratoLote,
8708 c_histContratoLote.Status);
8709 end loop;
8710 end formarLotePickingPulmao;
8711
8712 procedure gerarRemPerdaCorteFisico(p_idusuario in number) is
8713 cursor c_lotesRemanejar is
8714 select g.idarmazem, g.idlocalorigem, g.idlote, g.qtde, g.idcortefisico,
8715 g.idromaneio, e.comprimento * e.altura * e.largura cubagem,
8716 e.pesobruto, e.barra, lt.idproduto, lt.iddepositante,
8717 e.descrreduzido, l.picking, nvl(p.fracionado, 'N') fracionado
8718 from GTT_REMANEJA_CORTE G, lote lt, embalagem e, local l, produto p
8719 where lt.idlote = g.idlote
8720 and lt.idarmazem = g.idarmazem
8721 and e.barra = lt.barra
8722 and e.idproduto = lt.idproduto
8723 and l.idlocal = g.idlocalorigem
8724 and l.idarmazem = g.idarmazem
8725 and p.idproduto = lt.idproduto;
8726
8727 cursor c_locaisDisponiveisSetorPerda
8728 (
8729 p_idArmazem number,
8730 p_iddepositante number,
8731 p_idproduto number,
8732 p_idlocal local.idlocal%type
8733 ) is
8734 select lp.idlocal,
8735 (lp.cubagemMaxima - nvl(lp.cubagemOcupada, 0)) cubagemDisponivel,
8736 (lp.pesomaximo - nvl(lp.pesoOcupado, 0)) pesoDisponivel
8737 from (select l.idlocal,
8738 (l.comprimento * (l.altura - l.alturamanobra) *
8739 l.largura) cubagemMaxima, l.pesomaximo,
8740 (select sum((e.comprimento * e.largura * e.altura) *
8741 (ll.estoque + ll.adicionar))
8742 from lotelocal ll, lote lt, embalagem e
8743 where ll.idarmazem = l.idarmazem
8744 and ll.idlocal = l.idlocal
8745 and lt.idlote = ll.idlote
8746 and e.barra = lt.barra
8747 group by ll.idlocal) cubagemOcupada,
8748 (select sum(e.pesobruto * (ll.estoque + ll.adicionar))
8749 from lotelocal ll, lote lt, embalagem e
8750 where ll.idarmazem = l.idarmazem
8751 and ll.idlocal = l.idlocal
8752 and lt.idlote = ll.idlote
8753 and e.barra = lt.barra
8754 group by ll.idlocal) pesoOcupado
8755 from local l, setor s
8756 where l.idarmazem = p_idArmazem
8757 and s.idsetor = l.idsetor
8758 and s.perda = 1
8759 and exists (select 1
8760 from setordepositante sd
8761 where sd.idsetor = l.idsetor
8762 and sd.iddepositante = p_iddepositante)
8763 and exists (select 1
8764 from setorproduto sp
8765 where sp.idsetor = l.idsetor
8766 and sp.idproduto = p_idproduto)
8767 and l.idlocal <> p_idlocal) lp;
8768
8769 r_lotesRemanejar c_lotesRemanejar%rowtype;
8770 r_locaisDisponiveisSetorPerda c_locaisDisponiveisSetorPerda%rowtype;
8771 v_idRemanejamento number;
8772 v_quantidadeAlocar number;
8773 v_qtdRemanejar number;
8774 v_qtdRemanejada number;
8775 v_qtdPossivelAlocar number;
8776 v_msg t_message;
8777 begin
8778 open c_lotesRemanejar;
8779 fetch c_lotesRemanejar
8780 into r_lotesRemanejar;
8781
8782 if (c_lotesRemanejar%notfound) then
8783 close c_lotesRemanejar;
8784 return;
8785 end if;
8786
8787 while (c_lotesRemanejar%found)
8788 loop
8789 if (r_lotesRemanejar.Picking = 'N') then
8790 pk_estoque.retirar_pendencia(r_lotesRemanejar.Idarmazem,
8791 r_lotesRemanejar.Idlocalorigem,
8792 r_lotesRemanejar.Idlote,
8793 r_lotesRemanejar.Qtde, p_idusuario,
8794 p_complementar => 'RETIRANDO PENDENCIA PARA MOVIMENTACAO PARA SETOR DE PERDA IDCORTE: ' ||
8795 r_lotesRemanejar.Idcortefisico,
8796 p_idmovimentacao => NULL);
8797 end if;
8798
8799 v_qtdRemanejar := r_lotesRemanejar.Qtde;
8800
8801 if (c_locaisDisponiveisSetorPerda%isopen) then
8802 close c_locaisDisponiveisSetorPerda;
8803 end if;
8804
8805 open c_locaisDisponiveisSetorPerda(r_lotesRemanejar.Idarmazem,
8806 r_lotesRemanejar.Iddepositante,
8807 r_lotesRemanejar.Idproduto,
8808 r_lotesRemanejar.Idlocalorigem);
8809 fetch c_locaisDisponiveisSetorPerda
8810 into r_locaisDisponiveisSetorPerda;
8811
8812 if (c_locaisDisponiveisSetorPerda%notfound) then
8813 v_msg := t_message('Não foi possivel encontrar locais em setor de perdas para remanejar o produto Id: ' ||
8814 '{0} pertencente ao depositante Id: ' ||
8815 '{1} em função do aceite do corte fÃsico Id: {2}');
8816 v_msg.addParam(r_lotesRemanejar.Idproduto);
8817 v_msg.addParam(r_lotesRemanejar.Iddepositante);
8818 v_msg.addParam(r_lotesRemanejar.Idcortefisico);
8819 raise_application_error(-20000, v_msg.formatMessage);
8820 end if;
8821
8822 while ((c_locaisDisponiveisSetorPerda%found) and (v_qtdRemanejar > 0))
8823 loop
8824 v_idRemanejamento := 0;
8825 v_qtdPossivelAlocar := 0;
8826 if ((r_locaisDisponiveisSetorPerda.Cubagemdisponivel /
8827 r_lotesRemanejar.Cubagem) > (r_locaisDisponiveisSetorPerda.Pesodisponivel /
8828 r_lotesRemanejar.Pesobruto)) then
8829 v_qtdPossivelAlocar := r_locaisDisponiveisSetorPerda.Pesodisponivel /
8830 r_lotesRemanejar.Pesobruto;
8831
8832 else
8833 v_qtdPossivelAlocar := r_locaisDisponiveisSetorPerda.Cubagemdisponivel /
8834 r_lotesRemanejar.Cubagem;
8835 end if;
8836
8837 if (r_lotesRemanejar.Fracionado = 'N') then
8838 v_qtdPossivelAlocar := trunc(v_qtdPossivelAlocar);
8839 end if;
8840
8841 if (v_qtdPossivelAlocar > 0) then
8842 v_idRemanejamento := cadastrar_remanejamento(r_lotesRemanejar.Idarmazem,
8843 r_lotesRemanejar.Idarmazem,
8844 r_lotesRemanejar.Idlocalorigem,
8845 r_locaisDisponiveisSetorPerda.Idlocal,
8846 p_idusuario, null,
8847 'REMANEJAMENTO PARA SETOR DE PERDA, CORTE FÃSICO ID:' ||
8848 r_lotesRemanejar.idcortefisico,
8849 'N', 'S');
8850 if (v_qtdPossivelAlocar < v_qtdRemanejar) then
8851 v_qtdRemanejada := v_qtdPossivelAlocar;
8852 else
8853 v_qtdRemanejada := v_qtdRemanejar;
8854 end if;
8855
8856 -- caso remanejamento seja origem picking destino pulmao,
8857 -- inclui adiconar e pendencia manualmente e nao gera lote,
8858 -- caso contrario usa triger da loteremanejamento
8859 if (r_lotesRemanejar.Picking = 'S') then
8860 pk_triggers_control.disableTrigger('T_ALTERA_LOTEREMANEJAMENTO');
8861
8862 insert into loteremanejamento
8863 (idremanejamento, idlote, qtde, conferido, controlaqtde,
8864 diferenca)
8865 values
8866 (v_idRemanejamento, r_lotesRemanejar.Idlote, v_qtdRemanejada,
8867 c_nao, c_sim, 0);
8868
8869 pk_triggers_control.enableTrigger('T_ALTERA_LOTEREMANEJAMENTO');
8870
8871 pk_estoque.incluir_adicionar(r_lotesRemanejar.Idarmazem,
8872 r_locaisDisponiveisSetorPerda.Idlocal,
8873 r_lotesRemanejar.Idlote,
8874 v_qtdRemanejada, p_idusuario,
8875 'AUMENTADO ADICIONAR EM FUNCAO DA INCLUSAO DO LOTE NO REMANEJAMENTO N: ' ||
8876 v_idRemanejamento);
8877 else
8878 insert into loteremanejamento
8879 (idremanejamento, idlote, qtde, conferido, controlaqtde,
8880 diferenca)
8881 values
8882 (v_idRemanejamento, r_lotesRemanejar.Idlote, v_qtdRemanejada,
8883 c_nao, c_sim, 0);
8884 end if;
8885
8886 finalizarRemanejamento(v_idRemanejamento, p_idusuario);
8887
8888 insert into cortefisicoremanejamento
8889 (id, idcortefisico, idremanejamento)
8890 values
8891 (seq_cortefisicoremanejamento.nextval,
8892 r_lotesremanejar.idcortefisico, v_idremanejamento);
8893
8894 update remanejamento r
8895 set r.origemCorte = 1
8896 where r.idremanejamento = v_idRemanejamento;
8897
8898 v_qtdRemanejar := v_qtdRemanejar - v_qtdRemanejada;
8899 end if;
8900
8901 fetch c_locaisDisponiveisSetorPerda
8902 into r_locaisDisponiveisSetorPerda;
8903 end loop;
8904
8905 if (v_qtdRemanejar > 0) then
8906 v_msg := t_message('Não foi possÃvel remanejar toda a quantidade ' ||
8907 '{0} {1} do lote {2} do produto Id:' ||
8908 '{3} pertencente ao depositante Id: ' ||
8909 '{4} para locais de setor de perda.');
8910 v_msg.addParam(r_lotesRemanejar.Qtde);
8911 v_msg.addParam(r_lotesRemanejar.Descrreduzido);
8912 v_msg.addParam(r_lotesRemanejar.Idlote);
8913 v_msg.addParam(r_lotesRemanejar.Idproduto);
8914 v_msg.addParam(r_lotesRemanejar.Iddepositante);
8915 raise_application_error(-20000, v_msg.formatMessage);
8916 end if;
8917
8918 fetch c_lotesRemanejar
8919 into r_lotesRemanejar;
8920 end loop;
8921
8922 if (c_locaisDisponiveisSetorPerda%isopen) then
8923 close c_locaisDisponiveisSetorPerda;
8924 end if;
8925
8926 if (c_lotesRemanejar%isopen) then
8927 close c_lotesRemanejar;
8928 end if;
8929
8930 end gerarRemPerdaCorteFisico;
8931
8932 procedure verificarpendencia
8933 (
8934 p_idremanejamento in number,
8935 p_idromaneio in number,
8936 p_idusuario in number
8937 ) is
8938 p_mensagem varchar2(4000);
8939 v_msg t_message;
8940 begin
8941
8942 p_mensagem := 'O REMANEJAMENTO ' || p_idremanejamento ||
8943 ' GERADO PELA ONDA/ROMANEIO ID ' || p_idromaneio ||
8944 ' NÃO PODE SER EXCLUIDO POIS A QUANTIDADE A SER RETIRADA ' ||
8945 'É MAIOR DO QUE A QUANTIDADE DISPONIVEL DO LOTE NO ENDEREÇO:';
8946
8947 for c_lote in (select lr.idlote, lr.qtde, r.idlocaldestino,
8948 r.idarmazemdestino,
8949 (ll.estoque + ll.adicionar - ll.pendencia) disp,
8950 ll.estoque, ll.adicionar, ll.pendencia
8951 from loteremanejamento lr, remanejamento r, lotelocal ll
8952 where r.idremanejamento = p_idremanejamento
8953 and r.idremanejamento = lr.idremanejamento
8954 and ll.idlocal = r.idlocaldestino
8955 and ll.idlote = lr.idlote
8956 and lr.qtde >=
8957 (ll.estoque + ll.adicionar - ll.pendencia)
8958 and r.idremanejamento not in
8959 (select pr.idremanejamento
8960 from produtorecuperado pr
8961 where pr.idremanejamento = p_idremanejamento)
8962 and r.idremanejamento not in
8963 (select rr.idremanejamento
8964 from controleavaria ca, remanejamento rr
8965 where ca.idcontroleavaria = rr.idcontroleavaria
8966 and rr.idremanejamento = p_idremanejamento))
8967 loop
8968 p_mensagem := substr(p_mensagem || chr(13) || '- LOTE: ' ||
8969 c_lote.idlote || ' / QUANTIDADE REMANEJAMENTO: ' ||
8970 c_lote.qtde ||
8971 ' / QUANTIDADES NO DESTINO: (DISP ' ||
8972 c_lote.disp || ', EST ' || c_lote.estoque ||
8973 ', ADIC ' || c_lote.adicionar || ', PEND ' ||
8974 c_lote.pendencia || ')' || ' / OPERAÇÕES:', 1,
8975 4000);
8976
8977 for c_pend in (select iddepositante, idproduto, idlote, idarmazem,
8978 idlocal,
8979 decode(tipo, 0, 'PICKING', 1, 'PULMÃO BLOCADO', 2,
8980 'PULMÃO PALETIZADO', 3, 'COLMÉIA', 4,
8981 'DOCA', 5, 'AUDITORIA', 6, 'RUA EXPEDICAO',
8982 7, 'STAGE', 8, 'PACKING') tipolocal,
8983 buffer, sum(pendencia) pendencia, tipooperacao,
8984 operacao
8985 from ( -- localiza pendencia por onda de picking e pulmão
8986 select lt.iddepositante, lt.idproduto, m.idlote,
8987 lo.idarmazem, lo.idlocal, lo.tipo,
8988 lo.buffer, m.qtdemovimentada pendencia,
8989 'ONDA' tipooperacao, m.idonda operacao
8990 from movimentacao m, local lo, romaneiopai r,
8991 lote lt
8992 where m.status in (0, 1)
8993 and m.etapa = 1
8994 and lo.id = m.idlocalorigem
8995 and lo.tipo < 3
8996 and lo.buffer = 'N'
8997 and r.idromaneio = m.idonda
8998 and r.processado <> 'S'
8999 and lt.idlote = m.idlote
9000 and m.idonda != p_idromaneio
9001 union all
9002 -- localiza pendencia por onda para reabastecimento do picking
9003 select lt.iddepositante, lt.idproduto, m.idlote,
9004 lo.idarmazem, lo.idlocal, lo.tipo,
9005 lo.buffer, m.qtdemovimentada pendencia,
9006 'ONDA' tipooperacao, m.idonda operacao
9007 from movimentacao m, local lo, romaneiopai r,
9008 lote lt
9009 where m.status in (0, 1)
9010 and m.etapa = 3
9011 and lo.id = m.idlocalorigem
9012 and lo.tipo < 3
9013 and lo.buffer = 'N'
9014 and r.idromaneio = m.idonda
9015 and r.processado <> 'S'
9016 and lt.idlote = m.idlote
9017 and m.idonda != p_idromaneio
9018 and exists
9019 (select 1
9020 from grupomovimentacao g, movimentacao ma
9021 where g.idgrupo = m.id
9022 and ma.id = g.idmovimentacao
9023 and ma.etapa < m.etapa
9024 and ma.status = 2)
9025 union all
9026 -- localiza pendencia por onda de colmeia
9027 select iddepositante, idproduto, idlote, idarmazem,
9028 idlocal, tipo, buffer,
9029 sum(pendencia) pendencia, tipooperacao,
9030 operacao
9031 from (select lt.iddepositante, lt.idproduto,
9032 m.idlote, lo.idarmazem, lo.idlocal,
9033 lo.tipo, lo.buffer,
9034 m.qtdemovimentada pendencia,
9035 'ONDA' tipooperacao,
9036 m.idonda operacao
9037 from movimentacao m, local lo,
9038 romaneiopai r, lote lt
9039 where lo.id = m.idlocaldestino
9040 and lo.tipo = 3
9041 and lo.buffer = 'N'
9042 and m.status = 2
9043 and r.idromaneio = m.idonda
9044 and r.processado <> 'S'
9045 and lt.idlote = m.idlote
9046 and m.idonda != p_idromaneio
9047 union all
9048 select lt.iddepositante, lt.idproduto,
9049 c.idlote, lo.idarmazem, lo.idlocal,
9050 lo.tipo, lo.buffer,
9051 c.quantidade * -1 pendencia,
9052 'ONDA' tipooperacao,
9053 m.idonda operacao
9054 from movimentacao m, local lo,
9055 volumeromaneio vr, conteudovolume c,
9056 romaneiopai r, lote lt
9057 where lo.id = m.idlocalorigem
9058 and lo.tipo = 3
9059 and m.status = 2
9060 and vr.idvolumeromaneio =
9061 m.idvolumeromaneio
9062 and c.idvolumeromaneio =
9063 vr.idvolumeromaneio
9064 and r.idromaneio = m.idonda
9065 and r.processado <> 'S'
9066 and lt.idlote = c.idlote
9067 and m.idonda != p_idromaneio)
9068 group by iddepositante, idproduto, idlote,
9069 idarmazem, idlocal, tipo, buffer,
9070 tipooperacao, operacao
9071 union all
9072 -- localiza pendencia por onda da Rua Expedição ou Doca
9073 select lt.iddepositante, lt.idproduto, c.idlote,
9074 lo.idarmazem, lo.idlocal, lo.tipo,
9075 lo.buffer, c.quantidade pendencia,
9076 'ONDA' tipooperacao, m.idonda operacao
9077 from movimentacao m, local lo, volumeromaneio vr,
9078 conteudovolume c, romaneiopai r, lote lt
9079 where lo.id = m.idlocaldestino
9080 and m.status = 2
9081 and vr.idvolumeromaneio = m.idvolumeromaneio
9082 and c.idvolumeromaneio = vr.idvolumeromaneio
9083 and r.idromaneio = m.idonda
9084 and r.processado <> 'S'
9085 and lt.idlote = c.idlote
9086 and m.idonda != p_idromaneio
9087 union all
9088 -- localiza pendencia por onda do buffer de pulmão
9089 select iddepositante, idproduto, idlote, idarmazem,
9090 idlocal, tipo, buffer,
9091 sum(pendencia) pendencia, tipooperacao,
9092 operacao
9093 from (select lt.iddepositante, lt.idproduto,
9094 c.idlote, lo.idarmazem, lo.idlocal,
9095 lo.tipo, lo.buffer,
9096 c.quantidade pendencia,
9097 'ONDA' tipooperacao,
9098 m.idonda operacao
9099 from movimentacao m, local lo,
9100 volumeromaneio vr, conteudovolume c,
9101 romaneiopai r, lote lt
9102 where lo.id = m.idlocalorigem
9103 and lo.buffer = 'S'
9104 and m.status in (0, 1)
9105 and vr.idvolumeromaneio =
9106 m.idvolumeromaneio
9107 and c.idvolumeromaneio =
9108 vr.idvolumeromaneio
9109 and r.idromaneio = m.idonda
9110 and r.processado <> 'S'
9111 and lt.idlote = c.idlote
9112 and m.idonda != p_idromaneio
9113 union all
9114 select lt.iddepositante, lt.idproduto,
9115 m.idlote, lo.idarmazem, lo.idlocal,
9116 lo.tipo, lo.buffer,
9117 (m.qtdemovimentada * -1) pendencia,
9118 'ONDA' tipooperacao,
9119 m.idonda operacao
9120 from movimentacao m, local lo,
9121 romaneiopai r, lote lt
9122 where m.status in (0, 1)
9123 and m.etapa = 1
9124 and lo.id = m.idlocaldestino
9125 and lo.tipo < 3
9126 and lo.buffer = 'S'
9127 and r.idromaneio = m.idonda
9128 and r.processado <> 'S'
9129 and lt.idlote = m.idlote
9130 and m.idonda != p_idromaneio)
9131 group by iddepositante, idproduto, idlote,
9132 idarmazem, idlocal, tipo, buffer,
9133 tipooperacao, operacao
9134 union all
9135 -- localiza pendencia por onda do buffer de picking
9136 select lt.iddepositante, lt.idproduto, m.idlote,
9137 lo.idarmazem, lo.idlocal, lo.tipo,
9138 lo.buffer, m.qtdemovimentada pendencia,
9139 'ONDA' tipooperacao, m.idonda operacao
9140 from movimentacao m, local lo, romaneiopai r,
9141 lote lt
9142 where m.status in (0, 1)
9143 and m.etapa = 2
9144 and lo.id = m.idlocalorigem
9145 and lo.tipo = 0
9146 and lo.buffer = 'S'
9147 and r.idromaneio = m.idonda
9148 and r.processado <> 'S'
9149 and lt.idlote = m.idlote
9150 and m.idonda != p_idromaneio
9151 union all
9152 -- localiza pendencia por romaneio
9153 select lt.iddepositante, lt.idproduto, ps.idlote,
9154 ps.idarmazem, ps.idlocal, lo.tipo,
9155 lo.buffer, ps.qtdeunit pendencia,
9156 'ROMANEIO' tipooperacao,
9157 r.idromaneio operacao
9158 from paletseparacao ps, romaneiopai r, lote lt,
9159 local lo
9160 where r.idromaneio = ps.idromaneio
9161 and r.processado = 'N'
9162 and r.tipo = 0
9163 and lt.idlote = ps.idlote
9164 and lo.idarmazem = ps.idarmazem
9165 and lo.idlocal = ps.idlocal
9166 and r.idromaneio != p_idromaneio
9167 union all
9168 -- localiza pendencia por ajuste de saida
9169 select lt.iddepositante, lt.idproduto, a.idlote,
9170 a.idarmazem, a.idlocal, lo.tipo, lo.buffer,
9171 a.qtde pendencia,
9172 'AJUSTE SAIDA' tipooperacao,
9173 a.idajustemovto operacao
9174 from ajustemovtosaida a, ajustemovto aj, lote lt,
9175 local lo
9176 where aj.idajustemovto = a.idajustemovto
9177 and aj.gerado = 'N'
9178 and lt.idlote = a.idlote
9179 and lo.idarmazem = a.idarmazem
9180 and lo.idlocal = a.idlocal
9181 union all
9182 -- localiza pendencia por remanejamento
9183 select lt.iddepositante, lt.idproduto, lr.idlote,
9184 r.idarmazemorigem idarmazem,
9185 r.idlocalorigem idlocal, lo.tipo, lo.buffer,
9186 lr.qtde pendencia,
9187 'REMANEJAMENTO' tipooperacao,
9188 r.idremanejamento operacao
9189 from loteremanejamento lr, remanejamento r,
9190 lote lt, local lo
9191 where r.idremanejamento = lr.idremanejamento
9192 and r.status <> 'F'
9193 and lt.idlote = lr.idlote
9194 and lo.idarmazem = r.idarmazemorigem
9195 and lo.idlocal = r.idlocalorigem
9196 and r.idremanejamento != p_idremanejamento)
9197 where idlote = c_lote.idlote
9198 and idlocal = c_lote.idlocaldestino
9199 and idarmazem = c_lote.idarmazemdestino
9200 group by iddepositante, idproduto, idlote, idarmazem,
9201 idlocal,
9202 decode(tipo, 0, 'PICKING', 1, 'PULMÃO BLOCADO',
9203 2, 'PULMÃO PALETIZADO', 3, 'COLMÉIA', 4,
9204 'DOCA', 5, 'AUDITORIA', 6,
9205 'RUA EXPEDICAO', 7, 'STAGE', 8,
9206 'PACKING'), buffer, tipooperacao,
9207 operacao
9208 -- having sum(pendencia) <> sum(pendenciaonda + pendenciaromaneio + pendenciaajustesaida + pendenciaremanejamento + pendenciadecorte)
9209 order by idlote, tipolocal)
9210 loop
9211 p_mensagem := substr(p_mensagem || chr(13) || '-- ' ||
9212 c_pend.tipooperacao || ' ID ' ||
9213 c_pend.operacao || ' QUANTIDADE ' ||
9214 c_pend.pendencia, 1, 4000);
9215 end loop;
9216 end loop;
9217
9218 p_mensagem := substr(p_mensagem || chr(13) || 'SQLERR ' || sqlerrm, 1,
9219 4000);
9220 pk_utilities.GeraLog(p_idusuario, p_mensagem, p_idremanejamento, 'DR');
9221 end;
9222
9223 /*
9224 * rotina responsavel por desfazer o romaneio.
9225 */
9226 procedure desfazerRemanejamento
9227 (
9228 p_romaneio in number,
9229 p_usuario in number,
9230 p_tipo in number,
9231 p_idnotafiscal in number
9232 ) is
9233 v_QtdeLotesExcluidos number := 0;
9234
9235 procedure corrigeSepEspecificaOnda is
9236 r_sepespecifica separacaoespecifica%rowtype;
9237 v_idNovaSepEspecifica number;
9238 v_idSepLoteInd number;
9239 v_loteuniconoendereco number;
9240 v_qtdeSepLoteInd number;
9241 begin
9242 for c_rem in (select r.idlocalorigem, r.idlocaldestino, rr.idlote,
9243 m.idnotafiscal, rp.idarmazem
9244 from remanejamentoromaneio rr, remanejamento r,
9245 movimentacao m, local l, romaneiopai rp
9246 where rp.idromaneio = p_romaneio
9247 and rr.idromaneio = rp.idromaneio
9248 and r.idremanejamento = rr.idremanejamento
9249 and r.status in ('A', 'P')
9250 and l.idlocal = r.idlocaldestino
9251 and m.idonda = rr.idromaneio
9252 and m.idlocalorigem = l.id
9253 and m.idlote = rr.idlote)
9254 loop
9255 begin
9256 select pd.loteuniconoendereco
9257 into v_loteuniconoendereco
9258 from produtodepositante pd, lote lt
9259 where lt.idlote = c_rem.idlote
9260 and pd.identidade = lt.iddepositante
9261 and pd.idproduto = lt.idproduto;
9262 exception
9263 when no_data_found then
9264 v_loteuniconoendereco := 0;
9265 end;
9266
9267 begin
9268 select *
9269 into r_sepespecifica
9270 from separacaoespecifica s
9271 where s.idlocal = c_rem.idlocaldestino
9272 and s.idnotafiscal = c_rem.idnotafiscal
9273 and s.idlote = c_rem.idlote
9274 and s.idarmazem = c_rem.idarmazem;
9275 exception
9276 when no_data_found then
9277 r_sepespecifica := null;
9278 end;
9279
9280 if r_sepespecifica.id is not null then
9281 begin
9282 insert into separacaoespecifica
9283 (idarmazem, idlocal, idlote, idnotafiscal, qtde, cadmanual,
9284 fonteseparacao, idusuario, dtvinculo)
9285 values
9286 (r_sepespecifica.idarmazem, c_rem.idlocalorigem,
9287 r_sepespecifica.idlote, r_sepespecifica.idnotafiscal,
9288 r_sepespecifica.qtde, r_sepespecifica.cadmanual,
9289 r_sepespecifica.fonteseparacao, r_sepespecifica.idusuario,
9290 sysdate)
9291 returning id into v_idNovaSepEspecifica;
9292 exception
9293 when dup_val_on_index then
9294 update separacaoespecifica
9295 set qtde = qtde + r_sepespecifica.qtde
9296 where idarmazem = r_sepespecifica.idarmazem
9297 and idlocal = c_rem.idlocalorigem
9298 and idlote = r_sepespecifica.idlote
9299 and idnotafiscal = r_sepespecifica.idnotafiscal
9300 returning id into v_idNovaSepEspecifica;
9301 end;
9302
9303 select count(1)
9304 into v_qtdeSepLoteInd
9305 from seploteindsepespecif sls
9306 where sls.idsepespecifica = r_sepespecifica.id;
9307
9308 for c_sep in (select sls.idseploteind, sls.qtde
9309 from seploteindsepespecif sls
9310 where sls.idsepespecifica = r_sepespecifica.id)
9311 loop
9312 if (v_qtdeSepLoteInd = 1) then
9313 insert into seploteindsepespecif
9314 (idsepespecifica, idseploteind, idlote, qtde)
9315 values
9316 (v_idNovaSepEspecifica, c_sep.idseploteind,
9317 r_sepespecifica.idlote, r_sepespecifica.qtde);
9318 else
9319 insert into seploteindsepespecif
9320 (idsepespecifica, idseploteind, idlote, qtde)
9321 values
9322 (v_idNovaSepEspecifica, c_sep.idseploteind,
9323 r_sepespecifica.idlote, c_sep.qtde);
9324 end if;
9325
9326 delete from seploteindsepespecif s
9327 where s.idsepespecifica = r_sepespecifica.id
9328 and s.idseploteind = c_sep.idseploteind;
9329 end loop;
9330
9331 delete from separacaoespecifica
9332 where id = r_sepespecifica.id;
9333
9334 end if;
9335
9336 end loop;
9337 end corrigeSepEspecificaOnda;
9338
9339 begin
9340 if p_tipo = 1
9341 and pk_onda.validarFluxo('%buffer%', p_romaneio)
9342 and pk_onda.validarFluxo('%colmeia%', p_romaneio) then
9343 return;
9344 end if;
9345
9346 -- No caso de cancelamento de onda, essa rotina avalia as
9347 -- separações especÃficas e volta do picking para o pulmão
9348 -- no caso da formação ter criado remanejamentos e alterado isso.
9349 corrigeSepEspecificaOnda;
9350
9351 delete from gtt_selecao;
9352
9353 if (p_tipo = 0) then
9354 insert into gtt_selecao
9355 select distinct lr.idremanejamento
9356 from paletseparacao ps, loteremanejamento lr, remanejamento r
9357 where r.cadmanual = 'N'
9358 and r.status <> 'F'
9359 and r.idremanejamento = lr.idremanejamento
9360 and lr.idlote = ps.idlote
9361 and ps.idromaneio = p_romaneio
9362 union
9363 select distinct rr.idremanejamento
9364 from remanejamentoromaneio rr, remanejamento r
9365 where rr.idremanejamento = r.idremanejamento
9366 and r.cadmanual = 'N'
9367 and r.status <> 'F'
9368 and rr.idromaneio = p_romaneio;
9369
9370 else
9371 insert into gtt_selecao
9372 select distinct lr.idremanejamento
9373 from movimentacao m, loteremanejamento lr, remanejamento r,
9374 local ld
9375 where m.idonda = p_romaneio
9376 and m.etapa = 1
9377 and m.status = 3
9378 and m.idnotafiscal = p_idnotafiscal
9379 and lr.idlote = m.idlote
9380 and r.idremanejamento = lr.idremanejamento
9381 and r.cadmanual = 'N'
9382 and r.status <> 'F'
9383 and ld.idarmazem = r.idarmazemdestino
9384 and ld.idlocal = r.idlocaldestino;
9385
9386 end if;
9387
9388 for c_remanej in (select idselecionado idremanejamento, r.planejado
9389 from gtt_selecao g, remanejamento r
9390 where r.idremanejamento = g.idselecionado
9391 order by tipo)
9392 loop
9393 begin
9394 update loteremanejamento
9395 set idromaneio = null
9396 where idromaneio = p_romaneio
9397 and idremanejamento = c_remanej.idremanejamento;
9398
9399 update remanejamento
9400 set idromaneio = null
9401 where idromaneio = p_romaneio
9402 and idremanejamento = c_remanej.idremanejamento;
9403
9404 delete remanejamentoromaneio
9405 where idromaneio = p_romaneio
9406 and idremanejamento = c_remanej.idremanejamento;
9407
9408 update remanejamento r
9409 set r.planejado = 'N'
9410 where r.idremanejamento = c_remanej.idremanejamento;
9411
9412 --Excluindo lotes do remanejamento onde não exista remanejamento em produtorecuperado e controle de avaria.
9413 delete from loteremanejamento
9414 where idremanejamento = c_remanej.idremanejamento
9415 and idremanejamento not in
9416 (select pr.idremanejamento
9417 from produtorecuperado pr
9418 where pr.idremanejamento = c_remanej.idremanejamento)
9419 and idremanejamento not in
9420 (select r.idremanejamento
9421 from controleavaria ca, remanejamento r
9422 where ca.idcontroleavaria = r.idcontroleavaria
9423 and r.idremanejamento = c_remanej.idremanejamento);
9424
9425 v_QtdeLotesExcluidos := sql%rowcount;
9426
9427 if (p_tipo = 0) then
9428 pk_utilities.GeraLog(p_usuario,
9429 substr('O REMANEJAMENTO ' ||
9430 c_remanej.idremanejamento ||
9431 ' FOI EXCLUIDO POIS ROMANEIO DE ID: ' ||
9432 p_romaneio || ' FOI DESFEITO.', 1, 1000),
9433 c_remanej.idremanejamento, 'DR');
9434 else
9435 pk_utilities.GeraLog(p_usuario,
9436 substr('O REMANEJAMENTO ' ||
9437 c_remanej.idremanejamento ||
9438 ' FOI EXCLUIDO POIS A ONDA DE ID: ' ||
9439 p_romaneio || ' FOI DESFEITA.', 1, 1000),
9440 c_remanej.idremanejamento, 'DR');
9441
9442 end if;
9443 exception
9444 when others then
9445 v_QtdeLotesExcluidos := 0;
9446
9447 verificarpendencia(c_remanej.idremanejamento, p_romaneio,
9448 p_usuario);
9449 end;
9450
9451 if (v_QtdeLotesExcluidos > 0) then
9452
9453 update trocarlocaldestinorem
9454 set idremanejamento = null
9455 where idremanejamento = c_remanej.idremanejamento;
9456
9457 delete remanejamentoromaneio
9458 where idremanejamento = c_remanej.idremanejamento;
9459
9460 delete from remanejamento
9461 where idremanejamento = c_remanej.idremanejamento;
9462 else
9463 update remanejamento r
9464 set r.planejado = c_remanej.planejado
9465 where r.idremanejamento = c_remanej.idremanejamento;
9466 end if;
9467 end loop;
9468
9469 delete from gtt_selecao;
9470
9471 end;
9472
9473 procedure desfazRemanejBuffPickParaPick
9474 (
9475 p_idonda in number,
9476 p_idusuario in number,
9477 p_idmovimentacoes in varchar2
9478 ) is
9479 v_sqlRem varchar2(4000);
9480 type cursorRem is ref cursor;
9481 c_cursorRem cursorRem;
9482
9483 type rec_cursorRem is record(
9484 idremanejamento number,
9485 status remanejamento.status%type,
9486 idarmazemorigem number,
9487 idlocalorigem remanejamento.idlocalorigem%type,
9488 idarmazemdestino number,
9489 idlocaldestino remanejamento.idlocaldestino%type);
9490 r_cursorRem rec_cursorRem;
9491
9492 function montarSqlRem return varchar2 is
9493 v_sqlRem varchar2(4000);
9494 begin
9495 v_sqlRem := 'select r.idremanejamento, r.status, ' ||
9496 ' r.idarmazemorigem, r.idlocalorigem, r.idarmazemdestino, r.idlocaldestino ' ||
9497 ' from remanejamento r, local lo, local ld ' ||
9498 ' where lo.idarmazem = r.idarmazemorigem ' ||
9499 ' and lo.idlocal = r.idlocalorigem ' ||
9500 ' and ld.idarmazem = r.idarmazemdestino ' ||
9501 ' and ld.idlocal = r.idlocaldestino ' ||
9502 ' and lo.tipo in (0) ' || ' and lo.buffer = ''S'' ' ||
9503 ' and ld.tipo = 0 ' || ' and ld.buffer = ''N'' ' ||
9504 ' and r.status <> ''F'' ' ||
9505 ' and r.cadmanual = ''N'' ' ||
9506 ' and r.idmovimentacao in (' || p_idmovimentacoes || ')';
9507
9508 return v_sqlRem;
9509 end;
9510
9511 procedure integrarRemEsteira(p_idremanejamento in number) is
9512 C_ESTEIRA_PICKING constant number := 1;
9513 C_NAO_INTEGRADO constant number := 0;
9514 C_REMANEJ_CAIXA_GRAFICA constant number := 1;
9515 C_CANCELAMENTO_ONDA constant number := 9;
9516 C_EVENTO_EXCLUSAO constant number := 1;
9517 begin
9518 for r_intEsteira in (select *
9519 from int_automacao_esteira i
9520 where i.esteira = C_ESTEIRA_PICKING
9521 and i.idoperacao = p_idremanejamento
9522 and i.tipooperacao = C_REMANEJ_CAIXA_GRAFICA
9523 and i.integrado = C_NAO_INTEGRADO)
9524 loop
9525 delete from int_automacao_esteira i
9526 where i.id = r_intEsteira.id;
9527
9528 insert into historico_automacao_esteira
9529 (id, data, evento, esteira, codintegracao, identificador,
9530 integrado, pesagemliberada, idoperacao, tipooperacao,
9531 usuariologado)
9532 values
9533 (seq_hist_automacao_esteira.nextval, sysdate, C_EVENTO_EXCLUSAO,
9534 r_intEsteira.Esteira, r_intEsteira.Codintegracao,
9535 r_intEsteira.Identificador, r_intEsteira.Integrado,
9536 r_intEsteira.Pesagemliberada, r_intEsteira.Idoperacao,
9537 C_CANCELAMENTO_ONDA, user);
9538 end loop;
9539 end;
9540
9541 begin
9542 --SITUAÇÕES POSSÃVEIS
9543 --1 ONDA FOI FORMADA, NÃO FORAM FORMADAS ONDAS POSTERIORES DEPENDENDO DESSES REMANEJAMENTOS
9544 --E ESTA ONDA ESTÃ SENDO DESFEITA
9545 --RESULTADO ESPERADO: OS REMANEJAMENTOS DE BUFFER DE PICKING PARA PICKING SERÃO DESFEITOS NORMALMENTE
9546 --2 ONDA FOI FORMADA, POSTERIORMENTE FOI FORMADA OUTRA ONDA DEPENDENDO DO EXCESSO DE DISPONIVEL NO PICKING
9547 --E A PRIMEIRA ONDA ESTÃ SENDO DESFEITA
9548 --RESULTADO ESPERADO: O GRUPO DE REMANEJAMENTOS (GERADO PELO MESMO IDMOVIMENTACAO) SOMENTE SERA DESFEITO
9549 --SE TODO O GRUPO PUDER SER DESFEITO, POSTERIORMENTE VINCULARÃ ESTES REMANEJAMENTOS A UM NOVO QUE SERÃ CRIADO
9550 --PARA SUBSTITUIR A MOVIMENTAÇÃO DE PULMÃO PARA BUFFER DE PICKING
9551 --3 ONDA FOI FORMADA CRIANDO MOVIMENTACAO DE PL PARA BUFFER DE PK, OUTRA ONDA FOI FORMADA UTILIZANDO O
9552 --EXCESSO DO DISPONIVEL DA PRIMEIRA ONDA, PRIMEIRA ONDA FOI DESFEITA, CRIANDO UM REMANEJAMENTO DE PL PARA
9553 --BUFFER DE PK PARA SUBSTITUIR A MOVIMENTACAO, UMA TERCEIRA ONDA FOI FORMADA UTILIZANDO O EXCESSO DE DISPONIVEL
9554 --NO PICKING, A SEGUNDA ONDA ESTÃ SENDO CANCELADA.
9555 --RESULTADO ESPERADO: DEVERà LOCALIZAR OS REMANEJAMENTOS DE PULMÃO PARA BUFFER DE PICKING, CADASTRADOS AUTOMATICAMENTE
9556 --E SEM REMANEJAMENTOROMANEIO, TENTANDO EXCLUIR TODOS OS REMANEJAMENTOS FILHOS, SOMENTE EXCLUIRÃ
9557 --SE PUDER EXCLUIR TODOS OS FILHOS E O PAI
9558 --4 APÓS O CENÃRIO 3, CASO A TERCEIRA ONDA SEJA CANCELADA
9559 --RESULTADO ESPERADO: DEVERÃ CONSEGUIR EXCLUIR TODOS OS REMANEJAMENTOS FILHOS E O PAI POSTERIORMENTE
9560
9561 --identificar remanejamentos pendentes sem REMANEJAMENTOROMANEIO, cadastrados automaticamente,
9562 --de pulmão para buffer de picking e tentar desfazer todos os filhos
9563 --se um dos filhos falhar, efetuar rollback e seguir para o próximo remanejamento pai
9564 for c_rem in (select r.idremanejamento
9565 from remanejamento r, local lo, local ld
9566 where lo.idarmazem = r.idarmazemorigem
9567 and lo.idlocal = r.idlocalorigem
9568 and ld.idarmazem = r.idarmazemdestino
9569 and ld.idlocal = r.idlocaldestino
9570 and lo.tipo in (1, 2)
9571 and lo.buffer = 'N'
9572 and ld.tipo = 0
9573 and ld.buffer = 'S'
9574 and r.status <> 'F'
9575 and r.cadmanual = 'N'
9576 and not exists
9577 (select 1
9578 from remanejamentoromaneio rr
9579 where rr.idremanejamento = r.idremanejamento)
9580 and not exists
9581 (select 1
9582 from loteremanejamento lr
9583 where lr.idremanejamento = r.idremanejamento
9584 and lr.conferido = 'S'))
9585 loop
9586 savepoint sv_remanejamento;
9587
9588 begin
9589 --alterando status dos remanejamentos filhos para permitir exclusão
9590 update remanejamento r
9591 set r.planejado = 'N'
9592 where r.idremanejamentopai = c_rem.idremanejamento;
9593
9594 --excluindo remanejamentoromaneio dos remanejamentos filhos
9595 delete from remanejamentoromaneio rr
9596 where exists
9597 (select 1
9598 from remanejamento r
9599 where r.idremanejamento = rr.idremanejamento
9600 and r.idremanejamentopai = c_rem.idremanejamento);
9601
9602 --excluindo loteremanejamento dos remanejamentos filhos
9603 delete from loteremanejamento lr
9604 where exists
9605 (select 1
9606 from remanejamento r
9607 where r.idremanejamento = lr.idremanejamento
9608 and r.idremanejamentopai = c_rem.idremanejamento);
9609
9610 --excluindo remanejamentos filhos
9611 delete from remanejamento r
9612 where r.idremanejamentopai = c_rem.idremanejamento;
9613
9614 --alterando status do remanejamento pai para permitir exclusão
9615 update remanejamento r
9616 set r.planejado = 'N'
9617 where r.idremanejamento = c_rem.idremanejamento;
9618
9619 --excluindo loteremanejamento do remanejamento pai
9620 delete from loteremanejamento lr
9621 where lr.idremanejamento = c_rem.idremanejamento;
9622
9623 --excluindo remanejamento pai
9624 delete from remanejamento r
9625 where r.idremanejamento = c_rem.idremanejamento;
9626
9627 --integrando remanejamento excluÃdo com a esteira
9628 for c_remFilho in (select r.idremanejamento
9629 from remanejamento r
9630 where r.idremanejamentopai =
9631 c_rem.idremanejamento)
9632 loop
9633 integrarRemEsteira(c_remFilho.idremanejamento);
9634 end loop;
9635 exception
9636 when others then
9637 rollback to sv_remanejamento;
9638
9639 pk_utilities.GeraLog(p_idusuario,
9640 'Erro ao tentar excluir o remanejamento id: ' ||
9641 c_rem.idremanejamento ||
9642 ' durante o cancelamento da onda id: ' ||
9643 p_idonda || '. Erro: ' || sqlerrm,
9644 c_rem.idremanejamento, 'RM');
9645 end;
9646 end loop;
9647
9648 --identificar remanejamentos pendentes de buffer de picking para picking com os IDMOVIMENTACAO passados
9649 --se um deles falhar, efetuar rollback de todos já desfeitos
9650 --se estiver com status G, executar
9651 --desfazer se o status estiver diferente de G
9652 if p_idmovimentacoes is not null then
9653 v_sqlRem := montarSqlRem;
9654
9655 if (c_cursorRem%isopen) then
9656 close c_cursorRem;
9657 end if;
9658
9659 open c_cursorRem for v_sqlRem;
9660
9661 fetch c_cursorRem
9662 into r_cursorRem;
9663
9664 savepoint sv_remanejamento;
9665
9666 begin
9667 while (c_cursorRem%found)
9668 loop
9669 -- Se o status é G, já realizou a origem e está na esteira, sendo assim, executa
9670 if r_cursorRem.status = 'G' then
9671 for c_loteRem in (select lr.idlote, lr.qtde
9672 from loteremanejamento lr
9673 where lr.idremanejamento =
9674 r_cursorRem.idremanejamento)
9675 loop
9676 pk_estoque.retirar_estoque(r_cursorRem.idarmazemorigem,
9677 r_cursorRem.idlocalorigem,
9678 c_loteRem.idlote, c_loteRem.qtde,
9679 p_idusuario,
9680 'RETIRADO ESTOQUE EM FUNÇÃO DA EXECUÇÃO AUTOMÃTICA E EXCLUSÃO DO REMANEJAMENTO: ' ||
9681 r_cursorRem.idremanejamento ||
9682 ' DEVIDO AO CANCELAMENTO DA ONDA ID: ' ||
9683 p_idonda);
9684
9685 pk_estoque.incluir_estoque(r_cursorRem.idarmazemdestino,
9686 r_cursorRem.idlocaldestino,
9687 c_loteRem.idlote, c_loteRem.qtde,
9688 p_idusuario,
9689 'INCLUIDO ESTOQUE REMANESCENTE DO BUFFER DE PICKING EM FUNÇÃO ' ||
9690 'DA EXECUÇÃO AUTOMÃTICA E EXCLUSÃO DO REMANEJAMENTO: ' ||
9691 r_cursorRem.idremanejamento ||
9692 ' PDEVIDO AO CANCELAMENTO DA ONDA ID: ' ||
9693 p_idonda);
9694 end loop;
9695 end if;
9696
9697 --excluindo a remanejamentoromaneio dos remanejamentos
9698 delete from remanejamentoromaneio rr
9699 where rr.idremanejamento = r_cursorRem.idremanejamento;
9700
9701 --excluindo loteremanejamento do remanejamento
9702 delete from loteremanejamento lr
9703 where lr.idremanejamento = r_cursorRem.idremanejamento;
9704
9705 --excluindo remanejamento
9706 delete from remanejamento r
9707 where r.idremanejamento = r_cursorRem.idremanejamento;
9708
9709 --integrando remanejamento excluÃdo com a esteira
9710 integrarRemEsteira(r_cursorRem.idremanejamento);
9711
9712 fetch c_cursorRem
9713 into r_cursorRem;
9714 end loop;
9715 exception
9716 when others then
9717 rollback to sv_remanejamento;
9718
9719 pk_utilities.GeraLog(p_idusuario,
9720 'Ocorreu um erro durante o cancelamento da onda id: ' ||
9721 p_idonda ||
9722 ' ao tentar excluir os remanejamentos criados pelas movimentações id (' ||
9723 p_idmovimentacoes || '). Erro: ' || sqlerrm,
9724 p_idonda, 'RM');
9725 end;
9726
9727 close c_cursorRem;
9728 end if;
9729 end;
9730
9731 procedure validaDispExecucao(p_idremanejamento in number) is
9732 v_idArmazem number;
9733 v_idLocal local.idlocal%type;
9734
9735 v_cubagemRemanej number;
9736 v_pesoRemanej number;
9737 v_qtde number;
9738 v_msg t_message;
9739
9740 function isDestinoBufferPicking
9741 (
9742 p_idArmazem in number,
9743 p_idLocal in varchar2
9744 ) return boolean is
9745 v_resultado number;
9746 begin
9747 select count(*)
9748 into v_resultado
9749 from dual
9750 where exists (select 1
9751 from local l
9752 where l.idarmazem = p_idArmazem
9753 and l.idlocal = p_idLocal
9754 and l.tipo = 0
9755 and l.buffer = 'S');
9756 return v_resultado > 0;
9757 end;
9758
9759 begin
9760 -- Rotina criada para validar se o remanejamento pode ser realizado
9761 -- Caso o local de destino não possua disponibilidade de peso ou cubagem
9762 -- para receber os materiais e a configuração de armazem exibeAtvRemSemDisp
9763 -- estiver marcada ocorrerá erro
9764
9765 select r.idarmazemdestino, r.idlocaldestino,
9766 sum((lr.qtde / e.fatorconversao) * e.altura * e.largura *
9767 e.comprimento) cubagemrem,
9768 sum((lr.qtde / e.fatorconversao) * e.pesobruto) pesorem
9769 into v_idArmazem, v_idLocal, v_cubagemRemanej, v_pesoRemanej
9770 from remanejamento r, loteremanejamento lr, lote lt, embalagem e
9771 where r.idremanejamento = p_idremanejamento
9772 and lr.idremanejamento = r.idremanejamento
9773 and lt.idlote = lr.idlote
9774 and e.barra = lt.barra
9775 and e.idproduto = lt.idproduto
9776 group by r.idarmazemdestino, r.idlocaldestino, r.idremanejamento;
9777
9778 if isDestinoBufferPicking(v_idArmazem, v_idLocal) then
9779 return;
9780 end if;
9781
9782 select count(1)
9783 into v_qtde
9784 from dual
9785 where exists
9786 (select 1
9787 from local ld,
9788 (select ll.idendereco,
9789 sum((ll.estoque / e.fatorconversao) * e.altura *
9790 e.largura * e.comprimento) cubagemutilizada,
9791 sum((ll.estoque / e.fatorconversao) * e.pesobruto) pesoutilizado
9792 from lotelocal ll, lote lt, embalagem e
9793 where lt.idlote = ll.idlote
9794 and e.barra = lt.barra
9795 and e.idproduto = lt.idproduto
9796 and ll.idarmazem = v_idArmazem
9797 and ll.idlocal = v_idlocal
9798 group by ll.idendereco) est, armazem ar
9799 where ld.idarmazem = v_idArmazem
9800 and ld.idlocal = v_idlocal
9801 and est.idendereco(+) = ld.id
9802 and ar.idarmazem = ld.idarmazem
9803 and (ar.exibeAtvRemSemDisp = 1 or
9804 (((ld.altura - ld.alturamanobra) * ld.largura *
9805 ld.comprimento) - nvl(est.cubagemutilizada, 0) -
9806 v_cubagemRemanej >= 0 and
9807 ld.pesomaximo - nvl(est.pesoutilizado, 0) -
9808 v_pesoRemanej >= 0)));
9809
9810 if v_qtde = 0 then
9811 v_msg := t_message('O remanejamento não pode ser realizado, ' ||
9812 'pois local de destino não possui cubagem ' ||
9813 'ou peso disponÃvel no momento.');
9814 raise_application_error(-20000, v_msg.formatMessage);
9815 end if;
9816 end;
9817
9818 procedure excluirRemanejamento
9819 (
9820 p_idremanejamento in number,
9821 p_usuario in number
9822 ) is
9823 r_remanejamento remanejamento%rowtype;
9824
9825 v_tipolocalorigem number;
9826 v_tipolocaldestino number;
9827 v_msg t_message;
9828 begin
9829
9830 r_remanejamento := pk_remanejamento.CarregarRemanejamento(p_idremanejamento);
9831
9832 select lo.tipo, ld.tipo
9833 into v_tipolocalorigem, v_tipolocaldestino
9834 from remanejamento r, local lo, local ld
9835 where r.idremanejamento = p_idremanejamento
9836 and lo.idlocal = r.idlocalorigem
9837 and lo.idarmazem = r.idarmazemorigem
9838 and ld.idlocal = r.idlocaldestino
9839 and ld.idarmazem = r.idarmazemdestino;
9840
9841 if ((v_tipolocalorigem = 0) and
9842 (v_tipolocaldestino = 1 or v_tipolocaldestino = 2)) then
9843 if (r_remanejamento.status = 'F') then
9844 v_msg := t_message('Não é permitido realizar alterações em remanejamento que está finalizado.');
9845 raise_application_error(-20000, v_msg.formatMessage);
9846 end if;
9847 else
9848
9849 if (r_remanejamento.status <> 'A') then
9850 v_msg := t_message('Não é permitido realizar alterações em remanejamento que não esteja aberto.');
9851 raise_application_error(-20000, v_msg.formatMessage);
9852 end if;
9853
9854 if (r_remanejamento.planejado = 'S') then
9855 v_msg := t_message('Não é permitido realizar alterações em remanejamento planejado.');
9856 raise_application_error(-20000, v_msg.formatMessage);
9857 end if;
9858 end if;
9859
9860 if ((v_tipolocalorigem = 0) and
9861 (v_tipolocaldestino = 1 or v_tipolocaldestino = 2)) then
9862
9863 for c_loterem in (select idlote
9864 from loteremanejamento
9865 where idremanejamento = p_idremanejamento)
9866 loop
9867 desassociarLote(p_idremanejamento, c_loterem.idlote, p_usuario);
9868 end loop;
9869 end if;
9870
9871 delete from remanejamento
9872 where idremanejamento = p_idremanejamento;
9873
9874 pk_utilities.GeraLog(p_usuario,
9875 'Tela: Remanejamento - Apagou o Remanejamento Id: ' ||
9876 p_idremanejamento, p_idremanejamento, 'CA');
9877 end;
9878
9879 function getDadosRegraDepositanteLocal
9880 (
9881 p_idLocal in varchar2,
9882 p_idArmazem in number,
9883 p_barraInformada in varchar2
9884 ) return number is
9885
9886 cursor c_depositante is
9887 select distinct lt.iddepositante
9888 from lotelocal ll, lote lt
9889 where ll.estoque > 0
9890 and ll.idarmazem = p_idArmazem
9891 and ll.idlocal = p_idLocal
9892 and lt.idlote = ll.idlote;
9893
9894 v_qtdDepositantes number;
9895 v_retorno number;
9896 v_idproduto number;
9897 v_codProduto produto.codigointerno%type;
9898 v_barraProduto embalagem.barra%type;
9899 v_qtd number;
9900 v_loteIndustria lote.descr%type;
9901 v_dtVencimento lote.dtvenc%type;
9902 begin
9903 v_qtdDepositantes := 0;
9904
9905 delete from GTT_BARRA_REGRADEPOSITANTE;
9906
9907 for r_dep in c_depositante
9908 loop
9909 pk_depositante.encontrarRegraBarraDepositante(r_dep.iddepositante,
9910 p_barraInformada,
9911 v_qtdDepositantes);
9912 end loop;
9913
9914 if (v_qtdDepositantes = 0) then
9915 for r_dep in c_depositante
9916 loop
9917 v_retorno := pk_depositante.getDadosBarraRegraDepositante(r_dep.iddepositante,
9918 p_barraInformada,
9919 v_codProduto,
9920 v_barraProduto,
9921 v_qtd,
9922 v_loteIndustria,
9923 v_dtVencimento);
9924
9925 if (v_retorno = 1) then
9926 if (v_codProduto is null and v_loteIndustria is not null) then
9927 begin
9928 select p.idproduto, p.codigointerno, l.barra
9929 into v_idproduto, v_codProduto, v_barraProduto
9930 from lote l, produto p
9931 where l.descr = v_loteIndustria
9932 and l.iddepositante = r_dep.iddepositante
9933 and l.idproduto = p.idproduto
9934 and rownum = 1;
9935 exception
9936 when no_data_found then
9937 v_qtdDepositantes := 0;
9938 continue;
9939 end;
9940
9941 v_qtdDepositantes := 1;
9942
9943 insert into gtt_barra_regradepositante
9944 (iddepositante, depositante, barrainformada, idproduto,
9945 codproduto, descrproduto, barraproduto, qtd, loteindustria,
9946 dtvencimento)
9947 values
9948 (r_dep.iddepositante, null, p_barrainformada, v_idproduto,
9949 v_codproduto, null, v_barraproduto, v_qtd, v_loteindustria,
9950 v_dtvencimento);
9951
9952 return v_qtdDepositantes;
9953 end if;
9954 end if;
9955 end loop;
9956 end if;
9957
9958 return v_qtdDepositantes;
9959 end getDadosRegraDepositanteLocal;
9960
9961 function getDadosRegraDepositanteLote
9962 (
9963 p_idremanejamento in number,
9964 p_barraInformada in varchar2
9965 ) return number is
9966
9967 v_qtdDepositantes number;
9968 begin
9969 v_qtdDepositantes := 0;
9970
9971 delete from GTT_BARRA_REGRADEPOSITANTE;
9972
9973 for r_dep in (select distinct lt.iddepositante
9974 from loteremanejamento lr, lote lt
9975 where lt.idlote = lr.idlote
9976 and lr.idremanejamento = p_idremanejamento)
9977 loop
9978 pk_depositante.encontrarRegraBarraDepositante(r_dep.iddepositante,
9979 p_barraInformada,
9980 v_qtdDepositantes);
9981 end loop;
9982
9983 return v_qtdDepositantes;
9984 end;
9985
9986 procedure validarFinalizarRemanejamento(p_idRemanejamento number) is
9987 v_idlocalorigem local.idlocal%type;
9988 v_idlocalorigemFormat local.idlocalformatado%type;
9989 v_idarmazemorigem number;
9990 v_pk_origem number;
9991 v_pk_destino number;
9992 v_remanejamentos varchar2(4000);
9993 v_msg t_message;
9994 begin
9995 select r.idlocalorigem, lo.idlocalformatado, r.idarmazemorigem,
9996 decode(lo.picking, 'S', 1, 0) pk_origem,
9997 decode(ld.picking, 'S', 1, 0) pk_destino
9998 into v_idlocalorigem, v_idlocalorigemFormat, v_idarmazemorigem,
9999 v_pk_origem, v_pk_destino
10000 from remanejamento r, local lo, local ld
10001 where 1 = 1
10002 and lo.idlocal = r.idlocalorigem
10003 and lo.idarmazem = r.idarmazemorigem
10004 and ld.idlocal = r.idlocaldestino
10005 and ld.idarmazem = r.idarmazemdestino
10006 and r.idremanejamento = p_idRemanejamento;
10007
10008 if (v_pk_origem = 1 and v_pk_destino = 0) then
10009 select stragg(idrem)
10010 into v_remanejamentos
10011 from (select distinct r.idremanejamento idrem
10012 from remanejamento r, loteremanejamento lr, local lo,
10013 local ld
10014 where 1 = 1
10015 and r.idremanejamento = lr.idremanejamento
10016 and r.idlocaldestino = v_idlocalorigem
10017 and r.idarmazemdestino = v_idarmazemorigem
10018 and lo.idlocal = r.idlocalorigem
10019 and lo.idarmazem = r.idarmazemorigem
10020 and ld.idlocal = r.idlocaldestino
10021 and ld.idarmazem = r.idarmazemdestino
10022 and lo.buffer = 'N'
10023 and ld.buffer = 'N'
10024 and r.status <> 'F'
10025 and lr.idlote in
10026 (select distinct cl.idloteanterior
10027 from composicaolote cl, loteremanejamento lr
10028 where 1 = 1
10029 and lr.idremanejamento = p_idRemanejamento
10030 and cl.idlotenovo = lr.idlote));
10031
10032 if (v_remanejamentos is not null) then
10033 v_msg := t_message('EXISTEM REMANEJAMENTOS PENDENTES CUJO ENDEREÇO DE DESTINO É ' ||
10034 '{0}.' || chr(13) || chr(13) ||
10035 'É NECESSÃRIO EXECUTAR OS SEGUINTES REMANEJAMENTOS(' ||
10036 '{1}).' || chr(13) || chr(13) ||
10037 'OPERAÇÃO CANCELADA.');
10038 v_msg.addParam(v_idlocalorigemFormat);
10039 v_msg.addParam(v_remanejamentos);
10040 raise_application_error(-20000, v_msg.formatMessage);
10041 end if;
10042 end if;
10043 end validarFinalizarRemanejamento;
10044
10045 procedure integrarEsteira
10046 (
10047 p_idremanejamento number,
10048 p_idusuario number
10049 ) is
10050 v_fatorConversao number;
10051 v_caixaFechada char(1);
10052 v_barraEmbalagem varchar2(32);
10053 v_resto number;
10054 v_qtdeCaixa number;
10055 v_idlocalOrigem local.idlocal%type;
10056 v_estoque number;
10057 v_idsOnda varchar2(512);
10058 v_msg t_message;
10059
10060 ESTEIRA_PICKING constant number := 1;
10061
10062 procedure validarIntegracaoEsteira is
10063
10064 v_countValidacao number;
10065 v_idLocalOrigemRem local.idlocal%type;
10066 v_idLocalDestinoRem local.idlocal%type;
10067 v_idSetorOrigemRem setor.idsetor%type;
10068 v_idSetorDestinoRem setor.idsetor%type;
10069
10070 C_BUFFER_ESTEIRA_ATIVADO constant number := 1;
10071 C_BUFFER_ESTEIRA_DESATIVADO constant number := 0;
10072
10073 v_msg t_message;
10074 begin
10075 select lo.idlocal, lo.idsetor, ld.idlocal, ld.idsetor
10076 into v_idLocalOrigemRem, v_idSetorOrigemRem, v_idLocalDestinoRem,
10077 v_idSetorDestinoRem
10078 from remanejamento r, local lo, local ld
10079 where r.idremanejamento = p_idremanejamento
10080 and lo.idlocal = r.idlocalorigem
10081 and ld.idlocal = r.idlocaldestino;
10082
10083 if v_idSetorOrigemRem is null then
10084 v_msg := t_message('Não foi possÃvel criar a integração com esteira do Remanejamento ID: {0} ' ||
10085 'pois o setor Local Origem (ID:{1}) não possui setor configurado. Favor, ' ||
10086 'regularizar o cadastro para continuar com o processo');
10087 v_msg.addParam(p_idremanejamento);
10088 v_msg.addParam(v_idLocalOrigemRem);
10089 raise_application_error(-20000, v_msg.formatMessage);
10090 end if;
10091
10092 if v_idSetorDestinoRem is null then
10093 v_msg := t_message('Não foi possÃvel criar a integração com esteira do Remanejamento ID: {0} pois ' ||
10094 'o setor Local Destino (ID:{1}) não possui setor configurado. Favor, regularizar o ' ||
10095 'cadastro para continuar com o processo');
10096 v_msg.addParam(p_idremanejamento);
10097 v_msg.addParam(v_idLocalDestinoRem);
10098 raise_application_error(-20000, v_msg.formatMessage);
10099
10100 end if;
10101
10102 select count(1)
10103 into v_countValidacao
10104 from remanejamento r, local lo, local ld, setor sd
10105 where r.idremanejamento = p_idremanejamento
10106 and lo.idlocal = r.idlocalorigem
10107 and lo.idarmazem = r.idarmazemorigem
10108 and lo.buffer = 'S'
10109 and lo.bufferesteira = C_BUFFER_ESTEIRA_ATIVADO
10110 and ld.idlocal = r.idlocaldestino
10111 and ld.idarmazem = r.idarmazemdestino
10112 and ld.buffer = 'N'
10113 and ld.bufferesteira = C_BUFFER_ESTEIRA_DESATIVADO
10114 and sd.idsetor = ld.idsetor
10115 and sd.codintegracaoesteira is not null;
10116
10117 if v_countValidacao = 0 then
10118 v_msg := t_message('Não foi possÃvel criar a integração com esteira do Remanejamento ID: {0}. ' ||
10119 'Só é possÃvel criar esta integração para remanejamentos com local origem do tipo Buffer ' ||
10120 'com parâmetro Buffer Esteira ativado e destino com o parâmetro Código ' ||
10121 'de Integração com Esteira configurado no Setor (IDLOCALORIGEM: {1} e IDSETORDESTINO: {2}).');
10122 v_msg.addParam(p_idremanejamento);
10123 v_msg.addParam(v_idLocalOrigemRem);
10124 v_msg.addParam(v_idSetorDestinoRem);
10125 raise_application_error(-20000, v_msg.formatMessage);
10126
10127 end if;
10128 end validarIntegracaoEsteira;
10129 begin
10130
10131 validarIntegracaoEsteira;
10132
10133 for intEsteira in (select sd.codintegracaoesteira codintegracaoesteiraDestino,
10134 lr.qtde, lr.idlote, lt.idproduto, lt.barra,
10135 r.idusuarioinicio, r.horafimorigem, r.status,
10136 r.idlocalorigem
10137 from setor sd, local ld, remanejamento r, local lo,
10138 setor so, loteremanejamento lr, lote lt
10139 where sd.usoexclusivocxmov = 0
10140 and sd.codintegracaoesteira is not null
10141 and sd.ativo = 'S'
10142 and ld.idsetor = sd.idsetor
10143 and ld.ativo = 'S'
10144 and ld.idregiao is not null
10145 and r.idlocaldestino = ld.idlocal
10146 and r.idarmazemdestino = ld.idarmazem
10147 and r.idremanejamento = p_idremanejamento
10148 and lo.idlocal = r.idlocalorigem
10149 and lo.idarmazem = r.idarmazemorigem
10150 and lo.ativo = 'S'
10151 and lo.idregiao is not null
10152 and so.idsetor = lo.idsetor
10153 and so.ativo = 'S'
10154 and lr.idremanejamento = r.idremanejamento
10155 and lt.idlote = lr.idlote)
10156 loop
10157
10158 if (intEsteira.idusuarioinicio is null and
10159 intEsteira.horafimorigem is null and intEsteira.status <> 'G') then
10160 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.');
10161 raise_application_error(-20000, v_msg.formatMessage);
10162 end if;
10163
10164 begin
10165 select e.barra, e.fatorconversao, e.caixafechada, ll.idlocal
10166 into v_barraEmbalagem, v_fatorConversao, v_caixaFechada,
10167 v_idlocalOrigem
10168 from embalagem e, lote l, lotelocal ll
10169 where e.idproduto = intEsteira.Idproduto
10170 and trunc(intEsteira.qtde / e.fatorconversao) > 0
10171 and e.ativo = 'S'
10172 and l.idlote = intEsteira.Idlote
10173 and l.idproduto = e.idproduto
10174 and l.barra = e.barra
10175 and ll.idlote = l.idlote
10176 and ll.idlocal = intEsteira.Idlocalorigem
10177 and ll.estoque > 0
10178 order by e.fatorconversao desc;
10179 exception
10180 when no_data_found then
10181 select nvl(sum(ll.estoque), 0)
10182 into v_estoque
10183 from embalagem e, lote l, lotelocal ll
10184 where e.idproduto = intEsteira.Idproduto
10185 and trunc(intEsteira.qtde / e.fatorconversao) > 0
10186 and e.ativo = 'S'
10187 and l.idproduto = e.idproduto
10188 and l.barra = e.barra
10189 and ll.idlote = l.idlote
10190 and ll.idlocal = intEsteira.Idlocalorigem;
10191
10192 select stragg(rr.idromaneio)
10193 INTO v_idsOnda
10194 from remanejamentoromaneio rr
10195 where rr.idremanejamento = p_idremanejamento
10196 group by rr.idremanejamento;
10197
10198 if (v_estoque = 0) then
10199 v_msg := t_message('Não foi encontrado estoque no local origem: ' ||
10200 '{0}, verifique o processo anterior de separação da(s) onda(s) id(s): ' ||
10201 '{1}. OPERAÇÃO CANCELADA.');
10202 v_msg.addParam(intEsteira.Idlocalorigem);
10203 v_msg.addParam(v_idsOnda);
10204 raise_application_error(-20000, v_msg.formatMessage);
10205 else
10206 v_msg := t_message('Não foi encontrado embalagem cadastrada para o produto de id: ' ||
10207 '{0} ou a embalagem de barra: ' ||
10208 '{1} não está ativa. OPERAÇÃO CANCELADA.');
10209 v_msg.addParam(intEsteira.Idproduto);
10210 v_msg.addParam(v_barraEmbalagem);
10211 raise_application_error(-20000, v_msg.formatMessage);
10212 end if;
10213 end;
10214
10215 if ((v_fatorConversao <> 1) or
10216 (v_fatorConversao = 1 and v_caixaFechada = 'S')) then
10217
10218 v_barraEmbalagem := intEsteira.Barra;
10219
10220 elsif (v_fatorConversao = 1 and v_caixaFechada = 'N') then
10221 v_msg := t_message('Não é permitido realizar integração com esteira de embalagens que não sejam ' ||
10222 'CAIXA FECHADA ou fator de conversão igual a 1.OPERAÇÃO CANCELADA.');
10223 raise_application_error(-20000, v_msg.formatMessage);
10224 end if;
10225
10226 v_resto := mod(intEsteira.Qtde, v_fatorConversao);
10227
10228 if (v_resto = 0) then
10229 v_qtdeCaixa := (intEsteira.Qtde / v_fatorConversao);
10230 else
10231 v_msg := t_message('Não foi possÃvel realizar a integração com a esteira porque a quantidade remanejada do produto de id: ' ||
10232 '{0} deve ser em caixa. OPERAÇÃO CANCELADA.');
10233 v_msg.addParam(intEsteira.Idproduto);
10234 raise_application_error(-20000, v_msg.formatMessage);
10235 end if;
10236
10237 for intAutomacao in 1 .. v_qtdeCaixa
10238 loop
10239
10240 insert into int_automacao_esteira
10241 (id, esteira, codintegracao, identificador, integrado,
10242 pesagemliberada, idoperacao, tipooperacao)
10243 values
10244 (seq_int_automacao_esteira.nextval, ESTEIRA_PICKING,
10245 intEsteira.codintegracaoesteiraDestino, v_barraEmbalagem, 0, 0,
10246 p_idremanejamento, 1);
10247
10248 end loop;
10249
10250 end loop;
10251
10252 end integrarEsteira;
10253
10254 procedure concluirOrigem
10255 (
10256 p_idRemanejamento in number,
10257 p_idUsuario in number,
10258 p_TipoMenuRemanejamento in number
10259 ) is
10260 localOrigem t_localRemanejamento;
10261 localDestino t_localRemanejamento;
10262
10263 r_remanejamento remanejamento%rowtype;
10264
10265 v_existeUsuario number;
10266 v_nomeUsuario usuario.nomeusuario%type;
10267 v_msg t_message;
10268
10269 procedure validarRemanejamento is
10270 begin
10271 if (r_remanejamento.status <> 'A') then
10272 v_msg := t_message('Este remanejamento ja foi finalizado, portanto nao pode ser executado. Operacao Cancelada');
10273 raise_application_error(-20000, v_msg.formatMessage);
10274 end if;
10275
10276 if (r_remanejamento.planejado <> 'S') then
10277 v_msg := t_message('Este remanejamento nao foi planejado, portanto nao pode ser executado. Operacao Cancelada');
10278 raise_application_error(-20000, v_msg.formatMessage);
10279 end if;
10280
10281 if (r_remanejamento.horafimorigem is not null and
10282 r_remanejamento.idusuarioinicio is not null) then
10283 select nomeusuario
10284 into v_nomeUsuario
10285 from usuario
10286 where idusuario = r_remanejamento.idusuarioinicio;
10287 v_msg := t_message('O remanejamento id {0}' ||
10288 ' já teve sua origem finalizada pelo usuario {1}.');
10289 v_msg.addParam(p_idRemanejamento);
10290 v_msg.addParam(v_nomeUsuario);
10291 raise_application_error(-20000, v_msg.formatMessage);
10292 end if;
10293 end validarRemanejamento;
10294
10295 begin
10296 begin
10297 select r.*
10298 into r_remanejamento
10299 from remanejamento r
10300 where r.idremanejamento = p_idRemanejamento;
10301 exception
10302 when no_data_found then
10303 v_msg := t_message('Não foi encontrado remanejamento com id informado. IdRemanejamento {0}');
10304 v_msg.addParam(p_idRemanejamento);
10305 raise_application_error(-20000, v_msg.formatMessage);
10306 end;
10307
10308 select count(1)
10309 into v_existeUsuario
10310 from usuario u
10311 where u.idusuario = p_idUsuario;
10312
10313 if (v_existeUsuario = 0) then
10314 v_msg := t_message('Não foi encontrado usuário com id informado. IdUsuario {0}');
10315 v_msg.addParam(p_idUsuario);
10316 raise_application_error(-20000, v_msg.formatMessage);
10317 end if;
10318
10319 validarRemanejamento;
10320 validarBufPickEsteira(p_idRemanejamento, p_TipoMenuRemanejamento);
10321
10322 update remanejamento
10323 set status = 'G',
10324 idusuarioinicio = p_idUsuario,
10325 horafimorigem = sysdate
10326 where idremanejamento = p_idRemanejamento;
10327
10328 select l.idlocal, l.tipo, l.idarmazem, l.picking, l.buffer,
10329 s.codintegracaoesteira
10330 into localOrigem.idLocal, localOrigem.tipoLocal, localOrigem.idarmazem,
10331 localOrigem.picking, localOrigem.buffer,
10332 localOrigem.codIntegracaoEsteira
10333 from local l, setor s
10334 where l.idlocal = r_remanejamento.idlocalorigem
10335 and l.idarmazem = r_remanejamento.idarmazemorigem
10336 and s.idsetor(+) = l.idsetor;
10337
10338 select l.idlocal, l.tipo, l.idarmazem, l.picking, l.buffer,
10339 s.codintegracaoesteira
10340 into localDestino.idLocal, localDestino.tipoLocal,
10341 localDestino.idarmazem, localDestino.picking, localDestino.buffer,
10342 localDestino.codIntegracaoEsteira
10343 from local l, setor s
10344 where l.idlocal = r_remanejamento.idlocaldestino
10345 and l.idarmazem = r_remanejamento.idarmazemdestino
10346 and s.idsetor(+) = l.idsetor;
10347
10348 if (localOrigem.tipoLocal in (1, 2) and localOrigem.buffer = 'N') then
10349 if (localDestino.tipoLocal = 0)
10350 and (localDestino.picking = 'S')
10351 and (localDestino.buffer = 'N')
10352 and ((localDestino.codIntegracaoEsteira is not null) and
10353 (length(localDestino.codIntegracaoEsteira) > 0)) then
10354
10355 integrarEsteira(p_idRemanejamento, p_idUsuario);
10356 end if;
10357 end if;
10358
10359 pk_utilities.GeraLog(p_idUsuario,
10360 'REABASTECIMENTO ORIGEM, REABASTECIMENTO ' ||
10361 p_idRemanejamento || ', LOCAL ORIGEM ' ||
10362 localOrigem.tipoLocal ||
10363 ' REALIZADO PELO USUARIO ' || p_idUsuario,
10364 p_idRemanejamento, 'RO');
10365
10366 end concluirOrigem;
10367
10368 procedure ExecOrigemRemBufferPicking
10369 (
10370 p_idOnda in number,
10371 p_idLocalBuffer in local.idlocal%type,
10372 p_idProduto in number,
10373 p_idUsuario in number
10374 ) is
10375
10376 localOrigem t_localRemanejamento;
10377 localDestino t_localRemanejamento;
10378
10379 begin
10380 for r_Remanejamentos in (select distinct r.idremanejamento,
10381 r.idlocalorigem,
10382 r.idlocaldestino,
10383 r.idarmazemorigem,
10384 r.idarmazemdestino
10385 from remanejamento r, remanejamentoromaneio rr,
10386 lote lt, gtt_selecao g
10387 where rr.idromaneio = p_idonda
10388 and r.idremanejamento = rr.idremanejamento
10389 and r.status = 'A'
10390 and r.horafimorigem is null
10391 and r.idusuarioinicio is null
10392 and r.idlocalorigem = p_idLocalBuffer
10393 and lt.idlote = rr.idlote
10394 and lt.idproduto = p_idproduto
10395 and r.idmovimentacao = g.idselecionado)
10396 loop
10397
10398 select l.idlocal, l.tipo, l.idarmazem, l.picking, l.buffer,
10399 s.codintegracaoesteira, l.bufferesteira
10400 into localOrigem.idLocal, localOrigem.tipoLocal,
10401 localOrigem.idarmazem, localOrigem.picking, localOrigem.buffer,
10402 localOrigem.codIntegracaoEsteira, localOrigem.bufferesteira
10403 from local l, setor s
10404 where l.idlocal = r_Remanejamentos.idlocalorigem
10405 and l.idarmazem = r_Remanejamentos.idarmazemorigem
10406 and s.idsetor(+) = l.idsetor;
10407
10408 select l.idlocal, l.tipo, l.idarmazem, l.picking, l.buffer,
10409 s.codintegracaoesteira
10410 into localDestino.idLocal, localDestino.tipoLocal,
10411 localDestino.idarmazem, localDestino.picking,
10412 localDestino.buffer, localDestino.codIntegracaoEsteira
10413 from local l, setor s
10414 where l.idlocal = r_Remanejamentos.idlocaldestino
10415 and l.idarmazem = r_Remanejamentos.idarmazemdestino
10416 and s.idsetor(+) = l.idsetor;
10417
10418 if ((localOrigem.tipoLocal = 0) and (localOrigem.picking = 'S') and
10419 (localOrigem.buffer = 'S') and (localOrigem.bufferEsteira = 1)) then
10420
10421 if ((localDestino.tipoLocal = 0) and (localDestino.picking = 'S') and
10422 (localDestino.buffer = 'N') and
10423 (localDestino.codIntegracaoEsteira is not null and
10424 length(localDestino.codIntegracaoEsteira) > 0)) then
10425 update loteremanejamento lr
10426 set lr.conferido = 'S'
10427 where lr.idremanejamento = r_Remanejamentos.Idremanejamento
10428 and lr.conferido = 'N';
10429
10430 update remanejamento rem
10431 set rem.status = 'G',
10432 rem.idusuarioinicio = p_idusuario,
10433 rem.horafimorigem = sysdate
10434 where rem.Idremanejamento = r_Remanejamentos.Idremanejamento;
10435
10436 integrarEsteira(r_Remanejamentos.Idremanejamento, p_idusuario);
10437 end if;
10438 end if;
10439 end loop;
10440 end ExecOrigemRemBufferPicking;
10441
10442 procedure conferirLoteIndustria
10443 (
10444 p_idremanejamento in remanejamento.idremanejamento%type,
10445 p_loteIndustria in lote.descr%type
10446 ) is
10447
10448 v_loteIndustria lote.descr%type;
10449
10450 begin
10451 v_loteIndustria := trim(p_loteIndustria);
10452
10453 if (v_loteIndustria is null) then
10454 return;
10455 end if;
10456
10457 update loteremanejamento lr
10458 set lr.conferido = 'S',
10459 lr.qtdeconferida = lr.qtde
10460 where lr.idremanejamento = p_idremanejamento
10461 and exists (select 1
10462 from loteremanejamento lra, lote lt
10463 where lra.idremanejamento = p_idremanejamento
10464 and lra.idlote = lt.idlote
10465 and lra.idremanejamento = lr.idremanejamento
10466 and lra.idlote = lr.idlote
10467 and lra.conferido = 'N'
10468 and upper(nvl(lt.descr, 'NAOINFORMADO')) =
10469 upper(v_loteIndustria));
10470
10471 end conferirLoteIndustria;
10472
10473 function retorna_qtde_caixa
10474 (
10475 p_idremanejamento in number,
10476 p_idproduto in number default null,
10477 p_loteIndustria in varchar2 default null,
10478 p_conferido in varchar2 default null
10479 ) return varchar2 is
10480 cursor c_cx
10481 (
10482 p_idremanejamento in number,
10483 p_idproduto in number default null,
10484 p_loteIndustria in varchar2 default null,
10485 p_conferido in varchar2 default null
10486 ) is
10487 select sum(lr.qtde), e.fatorconversao
10488 from loteremanejamento lr, lote l, embalagem e
10489 where lr.idremanejamento = p_idremanejamento
10490 and lr.conferido = nvl(p_conferido, lr.conferido)
10491 and l.idlote = lr.idlote
10492 and ((p_loteIndustria is null) or
10493 (l.descr is null and p_loteIndustria = '-1') or
10494 (l.descr = p_loteIndustria))
10495 and l.idproduto = nvl(p_idproduto, l.idproduto)
10496 and e.barra = pk_produto.retornarcodbarramaiorfator(l.idproduto)
10497 and e.idproduto = l.idproduto
10498 group by l.idproduto, e.fatorconversao;
10499
10500 v_qtde number;
10501 v_caixa number;
10502 v_caixaAux number;
10503 v_unidade number;
10504 v_fcmaior number;
10505 v_descr varchar2(254);
10506 begin
10507 v_caixa := 0;
10508 v_unidade := 0;
10509
10510 if c_cx%isopen then
10511 close c_cx;
10512 end if;
10513
10514 open c_cx(p_idremanejamento, p_idproduto, p_loteIndustria, p_conferido);
10515 fetch c_cx
10516 into v_qtde, v_fcmaior;
10517
10518 if c_cx%found then
10519 while (c_cx%found)
10520 loop
10521 if (v_fcmaior > 1) then
10522 v_caixaAux := trunc(v_qtde / v_fcmaior);
10523 else
10524 v_caixaAux := 0;
10525 end if;
10526
10527 v_caixa := v_caixa + v_caixaAux;
10528
10529 v_unidade := v_unidade + (v_qtde - (v_caixaAux * v_fcmaior));
10530
10531 fetch c_cx
10532 into v_qtde, v_fcmaior;
10533 end loop;
10534 else
10535 return 'EMBALAGEM NAO CONFIGURADA';
10536 end if;
10537
10538 select decode(v_caixa, 0,
10539 decode(v_unidade, 0, '', v_unidade || ' UNIDADE(S)'),
10540 v_caixa || ' CAIXA(S)' ||
10541 decode(v_unidade, 0, '',
10542 ' e ' || v_unidade || ' UNIDADE(S)'))
10543 into v_descr
10544 from dual;
10545
10546 return trim(v_descr);
10547 end;
10548
10549 function confirmarTrocaLocalDestino
10550 (
10551 p_idremanejamento in number,
10552 p_idtrocalocal in number,
10553 p_idusuario in number
10554 ) return number is
10555
10556 type tpLoteRem is record(
10557 idproduto lote.idproduto%type,
10558 idlote lote.idlote%type,
10559 estado lote.estado%type,
10560 loteindustria lote.descr%type,
10561 dtvenc lote.dtvenc%type,
10562 iddepositante lote.iddepositante%type,
10563 loteuniconoendereco produtodepositante.loteuniconoendereco%type,
10564 idlocaldestino local.idlocal%type,
10565 qtdeentregue number);
10566
10567 type tpMovCancelada is record(
10568 id number,
10569 idonda number,
10570 idlote number,
10571 idlocaldestino number,
10572 quantidade number,
10573 idnotafiscal number,
10574 identificador number,
10575 ordem number);
10576
10577 type vTpLoteRem is table of tpLoteRem;
10578 type vTpMovCancelada is table of tpMovCancelada;
10579
10580 v_loteremtrocado vTpLoteRem;
10581 v_movcancelada vTpMovCancelada;
10582
10583 TROCALOCAL_ERRO constant number := 0;
10584 TROCALOCAL_SUCESSO constant number := 1;
10585
10586 TROCA_ANDAMENTO constant number := 0;
10587 TROCA_REALIZADA constant number := 1;
10588 TROCA_CANCELADA constant number := 2;
10589
10590 DETALHE_TROCA_ATIVO constant number := 1;
10591 DETALHE_TROCA_ALERTA constant number := 2;
10592 DETALHE_TROCA_PROCESSADO constant number := 3;
10593
10594 r_trocalocal trocarlocaldestinorem%rowtype;
10595 v_alerta number;
10596
10597 v_idlocalorigem number;
10598
10599 v_msg t_message;
10600
10601 v_qtdeCancelada number := 0;
10602 v_qtdeCriadas number := 0;
10603
10604 procedure validarPermissaoAlteracaoLocal is
10605 v_permitiralterarlocalqtdereab number;
10606 begin
10607 select count(1) permitiralterarlocalqtdereab
10608 into v_permitiralterarlocalqtdereab
10609 from remanejamento r, loteremanejamento lr, lote lt, depositante d
10610 where r.idremanejamento = p_idremanejamento
10611 and d.identidade = lt.iddepositante
10612 and lt.idlote = lr.idlote
10613 and lr.idremanejamento = r.idremanejamento
10614 and d.permitiralterarlocalqtdereab = 1;
10615
10616 if (v_permitiralterarlocalqtdereab = 0) then
10617 v_msg := t_message('O Remanejamento {0} não permite entregar em vários endereços de destino.');
10618 v_msg.addParam(p_idremanejamento);
10619 raise_application_error(-20000, v_msg.formatMessage);
10620 end if;
10621 end validarPermissaoAlteracaoLocal;
10622
10623 procedure getTrocaLocalDestino is
10624 begin
10625 begin
10626 select tl.*
10627 into r_trocalocal
10628 from trocarlocaldestinorem tl
10629 where tl.id = p_idtrocalocal;
10630 exception
10631 when no_data_found then
10632 v_msg := t_message('Troca de Local de Destino do remanejamento {0} não encontrada.');
10633 v_msg.addParam(p_idremanejamento);
10634 raise_application_error(-20000, v_msg.formatMessage);
10635 end;
10636
10637 if (r_trocalocal.idremanejamento <> p_idremanejamento) then
10638 v_msg := t_message('Troca de Local de Destino não pertence ao remanejamento {0}.');
10639 v_msg.addParam(p_idremanejamento);
10640 raise_application_error(-20000, v_msg.formatMessage);
10641 end if;
10642
10643 if (r_trocalocal.status = TROCA_REALIZADA) then
10644 v_msg := t_message('Troca de Local de Destino do remanejamento {0} está FINALIZADA.');
10645 v_msg.addParam(p_idremanejamento);
10646 raise_application_error(-20000, v_msg.formatMessage);
10647 end if;
10648
10649 if (r_trocalocal.status = TROCA_CANCELADA) then
10650 v_msg := t_message('Troca de Local de Destino do remanejamento {0} está CANCELADA.');
10651 v_msg.addParam(p_idremanejamento);
10652 raise_application_error(-20000, v_msg.formatMessage);
10653 end if;
10654 end getTrocaLocalDestino;
10655
10656 procedure validarDadosTrocaLocalDestino is
10657 v_qtdeTotalEntregue number;
10658 begin
10659 select sum(tld.qtdeentregue * e.fatorconversao)
10660 into v_qtdeTotalEntregue
10661 from trocarlocaldestinoremdet tld, trocarlocaldestinorem tl,
10662 embalagem e
10663 where tl.id = r_trocalocal.id
10664 and tl.status = TROCA_ANDAMENTO
10665 and tl.id = tld.idtrocalocal
10666 and tld.status = DETALHE_TROCA_ATIVO
10667 and e.idproduto = tld.idproduto
10668 and e.barra = tld.barra;
10669
10670 if (v_qtdeTotalEntregue = 0) then
10671 v_msg := t_message('Não existem quantidade(s) entregue(s) para realizar a troca de local destino.');
10672 raise_application_error(-20000, v_msg.formatMessage);
10673 end if;
10674
10675 if (r_trocalocal.qtdeentregar <> v_qtdeTotalEntregue) then
10676 v_msg := t_message('A quantide total entregue ativa é diferente da quantidade total a entregar.');
10677 raise_application_error(-20000, v_msg.formatMessage);
10678 end if;
10679 end validarDadosTrocaLocalDestino;
10680
10681 procedure validarEstoqueMinimoEntregue is
10682 v_estoqueMinimoEntregue boolean;
10683 begin
10684 --Verifica se existem outras operações diferente da onda que utilizam o estoque a entregar no remanejamento
10685 v_estoqueMinimoEntregue := false;
10686 for c_estreservado in (select idproduto, idlocal,
10687 sum(pendencia - qtdemovimentada) reservado
10688 from (select lt.idproduto, ll.idlocal,
10689 sum(ll.pendencia - ll.estoque) pendencia,
10690 0 qtdemovimentada
10691 from lotelocal ll, local ld, lote lt,
10692 loteremanejamento lr
10693 where lt.idlote = ll.idlote
10694 and ld.idarmazem = ll.idarmazem
10695 and ld.idlocal = ll.idlocal
10696 and ll.idarmazem =
10697 r_trocalocal.idarmazem
10698 and ll.idlocal =
10699 r_trocalocal.idlocaldestino
10700 and ll.pendencia > 0
10701 and lr.idlote = lt.idlote
10702 and lr.idremanejamento =
10703 r_trocalocal.idremanejamento
10704 group by lt.idproduto, ll.idlocal
10705 union
10706 select lt.idproduto, lo.idlocal,
10707 0 pendencia,
10708 sum(m.qtdemovimentada) qtdemovimentada
10709 from movimentacao m, lote lt, local lo,
10710 loteremanejamento lr
10711 where lo.idarmazem =
10712 r_trocalocal.idarmazem
10713 and lo.idlocal =
10714 r_trocalocal.idlocaldestino
10715 and lo.id = m.idlocalorigem
10716 and lt.idlote = m.idlote
10717 and m.status in (0, 1)
10718 and lr.idlote = lt.idlote
10719 and lr.idremanejamento =
10720 r_trocalocal.idremanejamento
10721 group by lt.idproduto, lo.idlocal)
10722 group by idproduto, idlocal
10723 having sum(pendencia - qtdemovimentada) > 0)
10724 loop
10725 v_estoqueMinimoEntregue := false;
10726 for c_entregue in (select tld.idproduto, tld.idlocaldestino,
10727 sum(tld.qtdeentregue * e.fatorconversao) totalentregue
10728 from trocarlocaldestinoremdet tld,
10729 trocarlocaldestinorem tl, embalagem e
10730 where tl.id = r_trocalocal.id
10731 and tl.status = TROCA_ANDAMENTO
10732 and tl.id = tld.idtrocalocal
10733 and tld.status = DETALHE_TROCA_ATIVO
10734 and tld.idproduto = c_estreservado.idproduto
10735 and tld.idlocaldestino =
10736 c_estreservado.idlocal
10737 and e.idproduto = tld.idproduto
10738 and e.barra = tld.barra
10739 group by tld.idproduto, tld.idlocaldestino
10740 having sum(tld.qtdeentregue * e.fatorconversao) >= c_estreservado.reservado)
10741 loop
10742 v_estoqueMinimoEntregue := true;
10743 end loop;
10744
10745 if (not v_estoqueMinimoEntregue) then
10746 update trocarlocaldestinoremdet
10747 set status = DETALHE_TROCA_ALERTA
10748 where idproduto = c_estreservado.idproduto
10749 and status = DETALHE_TROCA_ATIVO;
10750 end if;
10751 end loop;
10752 end validarEstoqueMinimoEntregue;
10753
10754 procedure prepararDadosParaTroca is
10755 v_qtdelote number;
10756 v_qtdeGtt number;
10757 begin
10758 for c_loterem in (select lt.idproduto, lt.idlote, lr.qtde
10759 from loteremanejamento lr, lote lt
10760 where lr.idremanejamento =
10761 r_trocalocal.idremanejamento
10762 and lt.idlote = lr.idlote
10763 order by lt.idproduto, lr.qtde)
10764 loop
10765 v_qtdelote := c_loterem.qtde;
10766 for c_troca in (select tld.idproduto, tld.idlocaldestino,
10767 sum((tld.qtdeentregue -
10768 nvl(tld.qtdeutilizada, 0)) *
10769 e.fatorconversao) totalentregue
10770 from trocarlocaldestinoremdet tld,
10771 trocarlocaldestinorem tl, embalagem e
10772 where tl.id = r_trocalocal.id
10773 and tl.status = TROCA_ANDAMENTO
10774 and tl.id = tld.idtrocalocal
10775 and tld.status = DETALHE_TROCA_ATIVO
10776 and tld.idproduto = c_loterem.idproduto
10777 and e.idproduto = tld.idproduto
10778 and e.barra = tld.barra
10779 group by tld.idproduto, tld.idlocaldestino
10780 order by sum(tld.qtdeentregue * e.fatorconversao),
10781 tld.idproduto, tld.idlocaldestino)
10782 loop
10783
10784 if (c_troca.totalentregue > v_qtdelote) then
10785 v_qtdeGtt := v_qtdelote;
10786 else
10787 v_qtdeGtt := c_troca.totalentregue;
10788
10789 end if;
10790
10791 update trocarlocaldestinoremdet tld
10792 set tld.qtdeutilizada = nvl(tld.qtdeutilizada, 0) + v_qtdeGtt
10793 where idproduto = c_troca.idproduto
10794 and idlocaldestino = c_troca.idlocaldestino
10795 and status = DETALHE_TROCA_ATIVO;
10796
10797 update trocarlocaldestinoremdet tld
10798 set status = DETALHE_TROCA_PROCESSADO
10799 where idproduto = c_troca.idproduto
10800 and idlocaldestino = c_troca.idlocaldestino
10801 and status = DETALHE_TROCA_ATIVO
10802 and tld.qtdeutilizada = tld.qtdeentregue;
10803
10804 insert into gtt_trocaloteremanejamento
10805 (idremanejamento, idlote, idlocaldestino, qtde)
10806 values
10807 (r_trocalocal.idremanejamento, c_loterem.idlote,
10808 c_troca.idlocaldestino, v_qtdeGtt);
10809
10810 v_qtdelote := v_qtdelote - v_qtdeGtt;
10811 exit when v_qtdelote = 0;
10812 end loop;
10813 end loop;
10814 end prepararDadosParaTroca;
10815
10816 procedure cancelarMovLoteRemAtual is
10817 begin
10818 -- Cancelar movimentações de lote que estão no remanejamento
10819 -- quando não foi entregue a quantidade suficiente que atenda a onda
10820 v_movcancelada := vTpMovCancelada();
10821 for c_movafet in (select m.id, m.idonda, lo.idarmazem, lo.idlocal,
10822 m.idlote, m.quantidade, m.idlocaldestino,
10823 m.idnotafiscal, m.identificador, m.ordem
10824 from movimentacao m, local lo, lote lt
10825 where lt.idlote = m.idlote
10826 and lo.idarmazem = r_trocalocal.idarmazem
10827 and lo.idlocal = r_trocalocal.idlocaldestino
10828 and lo.id = m.idlocalorigem
10829 and m.status in (0, 1)
10830 and exists
10831 (select 1
10832 from gtt_trocaloteremanejamento g
10833 where g.idremanejamento =
10834 r_trocalocal.idremanejamento
10835 and g.idlote = m.idlote))
10836 loop
10837 pk_estoque.retirar_pendencia(c_movafet.idarmazem, c_movafet.idlocal,
10838 c_movafet.idlote, c_movafet.quantidade,
10839 p_idusuario,
10840 'RETIRADA PENDENCIA PARA MOVIMENTACAO LOTE POR MOTIVO DE TROCA DE ENDEREÇO DE DESTINO DO REMANEJAMENTO:' ||
10841 r_trocalocal.idremanejamento ||
10842 ', ONDA ID: ' || c_movafet.idonda ||
10843 ', MOVIMENTAÇÃO ID: ' || c_movafet.id);
10844
10845 update movimentacao m
10846 set m.status = 3
10847 where m.id = c_movafet.id;
10848
10849 v_movcancelada.extend();
10850 v_movcancelada(v_movcancelada.count).id := c_movafet.id;
10851 v_movcancelada(v_movcancelada.count).idonda := c_movafet.idonda;
10852 v_movcancelada(v_movcancelada.count).idlote := c_movafet.idlote;
10853 v_movcancelada(v_movcancelada.count).idlocaldestino := c_movafet.idlocaldestino;
10854 v_movcancelada(v_movcancelada.count).quantidade := c_movafet.quantidade;
10855 v_movcancelada(v_movcancelada.count).idnotafiscal := c_movafet.idnotafiscal;
10856 v_movcancelada(v_movcancelada.count).identificador := c_movafet.identificador;
10857 v_movcancelada(v_movcancelada.count).ordem := c_movafet.ordem;
10858
10859 v_qtdeCancelada := v_qtdeCancelada + c_movafet.quantidade;
10860 end loop;
10861 end cancelarMovLoteRemAtual;
10862
10863 procedure finalizarRemanejamentoAtual is
10864 v_totallote number;
10865 begin
10866 -- Atualizando lotes do remanejamento atual para finaliza-lo
10867 v_loteremtrocado := vTpLoteRem();
10868
10869 update remanejamento
10870 set planejado = 'N',
10871 cadmanual = 'N'
10872 where idremanejamento = r_trocalocal.idremanejamento;
10873
10874 for c_troca in (select lt.idproduto, g.idlote, lt.estado,
10875 lt.descr loteindustria, lt.dtvenc,
10876 lt.iddepositante, pd.loteuniconoendereco,
10877 g.idlocaldestino, sum(g.qtde) totalentregue
10878 from gtt_trocaloteremanejamento g, lote lt,
10879 produtodepositante pd
10880 where g.idremanejamento =
10881 r_trocalocal.idremanejamento
10882 and pd.idproduto = lt.idproduto
10883 and pd.identidade = lt.iddepositante
10884 and lt.idlote = g.idlote
10885 group by lt.idproduto, g.idlote, lt.estado, lt.descr,
10886 lt.dtvenc, lt.iddepositante,
10887 pd.loteuniconoendereco, g.idlocaldestino)
10888 loop
10889 if (c_troca.idlocaldestino <> r_trocalocal.idlocaldestino) then
10890 update loteremanejamento
10891 set qtde = qtde - c_troca.totalentregue
10892 where idremanejamento = r_trocalocal.idremanejamento
10893 and idlote = c_troca.idlote;
10894
10895 v_loteremtrocado.extend();
10896 v_loteremtrocado(v_loteremtrocado.count).idproduto := c_troca.idproduto;
10897 v_loteremtrocado(v_loteremtrocado.count).idlote := c_troca.idlote;
10898 v_loteremtrocado(v_loteremtrocado.count).estado := c_troca.estado;
10899 v_loteremtrocado(v_loteremtrocado.count).loteindustria := c_troca.loteindustria;
10900 v_loteremtrocado(v_loteremtrocado.count).dtvenc := c_troca.dtvenc;
10901 v_loteremtrocado(v_loteremtrocado.count).iddepositante := c_troca.iddepositante;
10902 v_loteremtrocado(v_loteremtrocado.count).loteuniconoendereco := c_troca.loteuniconoendereco;
10903 v_loteremtrocado(v_loteremtrocado.count).idlocaldestino := c_troca.idlocaldestino;
10904 v_loteremtrocado(v_loteremtrocado.count).qtdeentregue := c_troca.totalentregue;
10905 end if;
10906 end loop;
10907
10908 -- Retirando lote que não serão entregues no remanejamento atual
10909 for c_loteretirar in (select lr.idremanejamento, lt.idlote
10910 from loteremanejamento lr, lote lt
10911 where lr.idremanejamento =
10912 r_trocalocal.idremanejamento
10913 and lt.idlote = lr.idlote
10914 and lr.qtde = 0)
10915 loop
10916 delete from loteremanejamento
10917 where idremanejamento = c_loteretirar.idremanejamento
10918 and idlote = c_loteretirar.idlote;
10919 end loop;
10920
10921 -- Finalizando o remanejamento atual
10922 select count(*)
10923 into v_totallote
10924 from loteremanejamento
10925 where idremanejamento = r_trocalocal.idremanejamento;
10926
10927 if (v_totallote > 0) then
10928 -- Finaliza o planejamento
10929 update remanejamento
10930 set planejado = 'S'
10931 where idremanejamento = r_trocalocal.idremanejamento;
10932
10933 pk_Remanejamento.finalizarRemanejamento(r_trocalocal.idremanejamento,
10934 p_idusuario);
10935 else
10936 update trocarlocaldestinorem
10937 set idremanejamento = null
10938 where idremanejamento = r_trocalocal.idremanejamento;
10939
10940 delete from remanejamentoromaneio
10941 where idremanejamento = r_trocalocal.idremanejamento;
10942
10943 delete from remanejamento
10944 where idremanejamento = r_trocalocal.idremanejamento;
10945 end if;
10946 end finalizarRemanejamentoAtual;
10947
10948 procedure criarNovoRemParaNovoDestino is
10949 r_loteunico pk_lote.t_loteunico;
10950 v_mensagem varchar2(1000);
10951 v_idremanejamento number;
10952 begin
10953 -- Criando remanejamento do lotes que vão para outros endereços
10954 for i in 1 .. v_loteremtrocado.count
10955 loop
10956 r_loteunico.idProduto := v_loteremtrocado(i).idproduto;
10957 r_loteunico.estado := v_loteremtrocado(i).estado;
10958 r_loteunico.loteindustria := v_loteremtrocado(i).loteindustria;
10959 r_loteunico.dtvencimento := v_loteremtrocado(i).dtvenc;
10960 r_loteunico.loteuniconoendereco := v_loteremtrocado(i)
10961 .loteuniconoendereco;
10962 r_loteunico.iddepositante := v_loteremtrocado(i).iddepositante;
10963
10964 -- Validação de compatibilidade para o endereço que o lote será remanejado.
10965 if not
10966 pk_lote.isLocalPodeReceberLoteUnico(r_loteunico,
10967 r_trocalocal.idarmazem,
10968 v_loteremtrocado(i)
10969 .idlocaldestino, v_mensagem) then
10970 if v_mensagem is not null then
10971 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}');
10972 v_msg.addParam(r_loteunico.loteindustria);
10973 v_msg.addParam(v_loteremtrocado(i).idlocaldestino);
10974 v_msg.addParam(v_mensagem);
10975 raise_application_error(-20000, v_msg.formatMessage);
10976 end if;
10977 end if;
10978
10979 -- Cria o remanejamento
10980 select seq_remanejamento.nextval
10981 into v_idremanejamento
10982 from dual;
10983
10984 insert into remanejamento
10985 (idremanejamento, idarmazemorigem, idlocalorigem,
10986 idarmazemdestino, idlocaldestino, datahora, idusuariotela, status,
10987 cadmanual, descrpalet, planejado)
10988 values
10989 (v_idremanejamento, r_trocalocal.idarmazem,
10990 r_trocalocal.idlocalorigem, r_trocalocal.idarmazem,
10991 v_loteremtrocado(i).idlocaldestino, sysdate, p_idusuario, 'A',
10992 'N',
10993 'TROCA DE LOCAL DESTINO DO REMANEJAMENTO ' ||
10994 r_trocalocal.idremanejamento, 'N');
10995
10996 -- Vincula o lote no remanejamento
10997 insert into loteremanejamento
10998 (idremanejamento, idlote, qtde, conferido, controlaqtde)
10999 values
11000 (v_idremanejamento, v_loteremtrocado(i).idlote,
11001 v_loteremtrocado(i).qtdeentregue, 'S', 'S');
11002
11003 -- Finaliza o planejamento
11004 update remanejamento
11005 set planejado = 'S'
11006 where idremanejamento = v_idremanejamento;
11007
11008 -- Executa o remanejamento
11009 pk_Remanejamento.finalizarRemanejamento(v_idremanejamento,
11010 p_idusuario);
11011 end loop;
11012 end criarNovoRemParaNovoDestino;
11013
11014 -- Refactored procedure criarMovNovoLocalDestino
11015 procedure criarMovNovoLocalDestino is
11016 v_idconfiguracaoonda number;
11017 r_movNovaTrocada movimentacao%rowtype;
11018 v_idgrupomovimentacao number;
11019 v_qtdemov number;
11020 v_qtdenova number;
11021 begin
11022 -- Criar movimentações novas para os lotes que foram afetados pelo remanejamento para pegar das novas origens
11023 for i in 1 .. v_movcancelada.count
11024 loop
11025 select r.idconfiguracaoonda
11026 into v_idconfiguracaoonda
11027 from romaneiopai r
11028 where r.idromaneio = v_movcancelada(i).idonda;
11029
11030 v_qtdemov := v_movcancelada(i).quantidade;
11031 for c_novolocal in (select idlote, idendereco, idarmazem, idlocal,
11032 sum(totalentregue - totalmov) totalrestante
11033 from (select g.idlote, ld.id idendereco,
11034 ld.idarmazem,
11035 g.idlocaldestino idlocal,
11036 sum(g.qtde) totalentregue,
11037 0 totalmov
11038 from gtt_trocaloteremanejamento g,
11039 lote lt, local ld
11040 where g.idremanejamento =
11041 r_trocalocal.idremanejamento
11042 and lt.idlote = g.idlote
11043 and g.idlote = v_movcancelada(i)
11044 .idlote
11045 and ld.idarmazem =
11046 r_trocalocal.idarmazem
11047 and ld.idlocal = g.idlocaldestino
11048 group by g.idlote, ld.id, ld.idarmazem,
11049 g.idlocaldestino
11050 union
11051 select m.idlote, lo.id idendereco,
11052 lo.idarmazem, lo.idlocal,
11053 0 totalentregue,
11054 sum(m.quantidade) totalmov
11055 from movimentacao m, local lo,
11056 gtt_trocaloteremanejamento g
11057 where g.idlote = m.idlote
11058 and g.idlocaldestino = lo.idlocal
11059 and lo.idarmazem =
11060 r_trocalocal.idarmazem
11061 and lo.id = m.idlocalorigem
11062 and m.idlote = v_movcancelada(i)
11063 .idlote
11064 and m.status in (0, 1)
11065 group by m.idlote, lo.id, lo.idarmazem,
11066 lo.idlocal)
11067 group by idlote, idendereco, idarmazem, idlocal
11068 having sum(totalentregue - totalmov) > 0)
11069 loop
11070 -- calcula a qtde correta para a geração da movimentação
11071 if (c_novolocal.totalrestante > v_qtdemov) then
11072 v_qtdenova := v_qtdemov;
11073 else
11074 v_qtdenova := c_novolocal.totalrestante;
11075 end if;
11076
11077 pk_estoque.incluir_pendencia(c_novolocal.idarmazem,
11078 c_novolocal.idlocal,
11079 c_novolocal.idlote, v_qtdenova,
11080 p_idusuario,
11081 'ADICIONADO PENDENCIA PARA NOVA MOVIMENTACAO CRIADA POR MOTIVO DE TROCA DE ENDEREÇO DESTINO NO REMANEJAMENTO: ' ||
11082 r_trocalocal.idremanejamento ||
11083 ' IDTROCALOCAL:' || r_trocalocal.id ||
11084 ', ONDA ID: ' || v_movcancelada(i)
11085 .idonda);
11086
11087 r_movNovaTrocada.id := null;
11088 r_movNovaTrocada.idlocalorigem := c_novolocal.idendereco;
11089 r_movNovaTrocada.idlocaldestino := v_movcancelada(i)
11090 .idlocaldestino;
11091 r_movNovaTrocada.quantidade := v_qtdenova;
11092 r_movNovaTrocada.etapa := 1;
11093 r_movNovaTrocada.idlote := c_novolocal.idlote;
11094 r_movNovaTrocada.status := 0;
11095 r_movNovaTrocada.idonda := v_movcancelada(i).idonda;
11096 r_movNovaTrocada.qtdemovimentada := v_qtdenova;
11097 r_movNovaTrocada.idnotafiscal := v_movcancelada(i).idnotafiscal;
11098 r_movNovaTrocada.datainicio := null;
11099 r_movNovaTrocada.idusuario := null;
11100 r_movNovaTrocada.qtdeconferida := 0;
11101 r_movNovaTrocada.identificador := v_movcancelada(i)
11102 .identificador;
11103 r_movNovaTrocada.tiposeparacao := null;
11104 r_movNovaTrocada.ordem := v_movcancelada(i).ordem;
11105
11106 pk_triggers_control.disableTrigger('T_insereMovimentacao');
11107
11108 v_idgrupomovimentacao := pk_onda.inserirMovimentacao(r_movNovaTrocada,
11109 v_idgrupomovimentacao);
11110
11111 pk_triggers_control.enableTrigger('T_insereMovimentacao');
11112
11113 v_qtdemov := v_qtdemov - v_qtdenova;
11114
11115 v_qtdeCriadas := v_qtdeCriadas + v_qtdenova;
11116 exit when v_qtdemov = 0;
11117 end loop;
11118 pk_romaneio.preencherInfSeparacao(v_movcancelada(i).idonda,
11119 v_idconfiguracaoonda);
11120 end loop;
11121
11122 if (v_qtdeCriadas <> v_qtdeCancelada) then
11123 raise_application_error(-20000,
11124 'Não foi possÃvel concluir a operação, a quantidade de movimentação cancelada: ' ||
11125 v_qtdeCancelada ||
11126 ' é diferente da quantidade nova criada: ' ||
11127 v_qtdeCriadas);
11128 end if;
11129 end criarMovNovoLocalDestino;
11130 begin
11131 validarPermissaoAlteracaoLocal;
11132 getTrocaLocalDestino;
11133 validarDadosTrocaLocalDestino;
11134 validarEstoqueMinimoEntregue;
11135
11136 select count(*)
11137 into v_alerta
11138 from trocarlocaldestinoremdet
11139 where idtrocalocal = r_trocalocal.id
11140 and status = DETALHE_TROCA_ALERTA;
11141
11142 if (v_alerta > 0) then
11143 return TROCALOCAL_ERRO;
11144 end if;
11145
11146 prepararDadosParaTroca;
11147 cancelarMovLoteRemAtual;
11148 finalizarRemanejamentoAtual;
11149 criarNovoRemParaNovoDestino;
11150 criarMovNovoLocalDestino;
11151
11152 update trocarlocaldestinorem
11153 set status = TROCA_REALIZADA,
11154 datafinalizacao = sysdate
11155 where id = r_trocalocal.id;
11156
11157 pk_utilities.GeraLog(p_idusuario,
11158 'Troca Local Destino id: ' || r_trocalocal.id ||
11159 ' Realizada.', r_trocalocal.idremanejamento, 'TL');
11160
11161 return TROCALOCAL_SUCESSO;
11162 end;
11163
11164 function verificaCompOrigemDestino
11165 (
11166 p_qtdeTotalLote in lote.qtdedisponivel%type,
11167 p_fatorConversaoLote in lote.fatorconversao%type,
11168 p_qtdeLoteRemanejado in lote.qtdedisponivel%type
11169 ) return boolean is
11170 begin
11171 -- verifica se a quantidade escolhida pode formar uma ou mais caixas exatas
11172 -- baseando-se no fator do lote selecionado
11173 -- e se com a quantidade remanescente na origem também será possÃvel formar
11174 -- caixas exatas com o mesmo fator do lote selecionado, garantindo assim,
11175 -- a compatibiliadade com o setor.
11176 if ((mod(p_qtdeLoteRemanejado, p_fatorConversaoLote) = 0) and
11177 (mod((p_qtdeTotalLote - p_qtdeLoteRemanejado), p_fatorConversaoLote) = 0)) then
11178 return true;
11179 else
11180 return false;
11181 end if;
11182 end verificaCompOrigemDestino;
11183
11184 procedure infLoteIndRemanejarMaterial
11185 (
11186 p_idLote in number,
11187 p_loteIndustria in varchar2
11188 ) is
11189 v_iddepositante number;
11190 v_idproduto number;
11191 v_dtvenc date;
11192 begin
11193
11194 select l.iddepositante, l.idproduto, l.dtvenc
11195 into v_iddepositante, v_idproduto, v_dtvenc
11196 from lote l
11197 where l.idlote = p_idLote;
11198
11199 if (pk_lote.isLoteIndustriaComMaisDeUmVenc(v_iddepositante, v_idproduto,
11200 p_loteIndustria, v_dtvenc)) then
11201 raise_application_error(-20000,
11202 'Não é possÃvel cadastrar lote do mesmo produto/depositante com o mesmo lote indústria e data de vencimento. Lote: ' ||
11203 p_idLote);
11204 end if;
11205
11206 update lote
11207 set descr = p_loteIndustria
11208 where idlote = p_idLote;
11209 end;
11210
11211 function validarImpressaoEtqRemanej
11212 (
11213 p_idUsuario in number,
11214 p_idsupervisor in number := 0
11215 ) return number is
11216
11217 SOLICITAR_SENHASUPERVISOR constant number := 0;
11218 IMPRIMIR_DIRETO constant number := 1;
11219 PRE_VISUALIZAR_IMPRESSAO constant number := 2;
11220
11221 C_NAO constant number := 0;
11222 C_SIM constant number := 1;
11223
11224 v_solSenhaSuperReimpEtqRemanej number;
11225 v_preVisualizarEtqRemanej number;
11226 v_existeEtqImpressa number;
11227
11228 v_resultado number;
11229 v_finalizar number;
11230 v_validar number;
11231
11232 v_msg t_message;
11233
11234 begin
11235
11236 v_resultado := PRE_VISUALIZAR_IMPRESSAO;
11237 v_finalizar := C_NAO;
11238
11239 select count(1)
11240 into v_existeEtqImpressa
11241 from remanejamento rm, gtt_selecao g
11242 where rm.idremanejamento = g.idselecionado
11243 and rm.etiquetaremanejamentoimpressa = C_SIM;
11244
11245 select count(*)
11246 into v_validar
11247 from dual
11248 where exists
11249 (select 1
11250 from gtt_selecao g, remanejamento r
11251 where r.idremanejamento = g.idselecionado
11252 and not exists
11253 (select 1
11254 from loteremanejamento lr
11255 where lr.idremanejamento = r.idremanejamento));
11256
11257 if (v_validar > 0) then
11258 v_msg := t_message('Existem remanejamentos selecionados que não possuem lotes vinculados.');
11259 raise_application_error(-20000, v_msg.formatMessage);
11260 end if;
11261
11262 for c_idRemanejamento in (select idselecionado
11263 from gtt_selecao)
11264 loop
11265
11266 if (v_existeEtqImpressa > 0) then
11267 for c_depositantes in (select distinct d.identidade,
11268 d.solsenhasuperreimpetiqremaneja,
11269 d.previsualizaretiqremanejamento
11270 from loteremanejamento lmr, lote lt,
11271 depositante d
11272 where lmr.idremanejamento =
11273 c_idRemanejamento.Idselecionado
11274 and lt.idlote = lmr.idlote
11275 and d.identidade = lt.iddepositante)
11276 loop
11277 if (c_depositantes.solsenhasuperreimpetiqremaneja = C_SIM and
11278 p_idSupervisor = 0) then
11279 v_resultado := SOLICITAR_SENHASUPERVISOR;
11280 v_finalizar := C_SIM;
11281 exit;
11282 else
11283 if (c_depositantes.previsualizaretiqremanejamento = C_NAO) then
11284 v_resultado := IMPRIMIR_DIRETO;
11285 v_finalizar := C_SIM;
11286 exit;
11287 end if;
11288 end if;
11289 end loop;
11290 else
11291 for c_depositantes in (select distinct d.identidade,
11292 d.previsualizaretiqremanejamento
11293 from loteremanejamento lmr, lote lt,
11294 depositante d
11295 where lmr.idremanejamento =
11296 c_idRemanejamento.Idselecionado
11297 and lt.idlote = lmr.idlote
11298 and d.identidade = lt.iddepositante)
11299 loop
11300 if (c_depositantes.previsualizaretiqremanejamento = C_NAO) then
11301 v_resultado := IMPRIMIR_DIRETO;
11302 v_finalizar := C_SIM;
11303 exit;
11304 end if;
11305 end loop;
11306 end if;
11307
11308 if (v_finalizar = C_SIM) then
11309 exit;
11310 end if;
11311
11312 end loop;
11313
11314 return v_resultado;
11315
11316 end validarImpressaoEtqRemanej;
11317end pk_remanejamento;