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