METHOD execute_db BY DATABASE FUNCTION FOR HDB LANGUAGE
SQLSCRIPT OPTIONS READ-ONLY USING mslb matdoc nsdm_e_marc t156 ekpo.
declare lv_index, lv_rows, lv_pos integer;
it_mat = select mslb.mandt as client,
mslb.matnr as Material,
mslb.werks as Centro,
'0' as QuantTransf,
mslb.charg as Lote,
mslb.lifnr as Fornecedor,
'S' as tipo
FROM mslb
WHERE mandt = session_context('CLIENT')
and sobkz = 'O'
-- AND matnr = '000000000001157206'
UNION ALL
SELECT marc.mandt as Client,
marc.matnr as Material,
marc.werks as Centro,
marc.umlmc as QuantTransf,
'' as Lote,
'' as Fornecedor,
'T' as tipo
FROM nsdm_e_marc as Marc
where marc.mandt = session_context('CLIENT')
and marc.umlmc > 0;
it_mov = select mandt as Client,
bwart
FROM t156;
IF :p_sql_sub IS NOT null then
it_mov_sub = apply_filter (:it_mov, :p_sql_sub);
END if;
IF :p_sql_trc IS NOT NULL then
it_mov_trc = apply_filter (:it_mov, :p_sql_trc);
END if;
-- Documentos subcontratacao
it_doc = SELECT Doc.mandt as Client,
Doc.ebeln as Pedido,
Doc.ebelp as ItemPedido,
Doc.matnr as Material,
Doc.werks as Centro,
Doc.lifnr as Fornecedor,
Doc.budat as DtLancamento,
Doc.charg as Lote,
Doc.vfdat as DtVencLote,
Doc.ebeln as PedCKD,
Doc.lgort as Deposito,
Mat.tipo as Tipo,
CASE when Doc.blart = 'WE' then Doc.menge * -1
else Doc.menge end as Quantidade,
CASE when Doc.blart = 'WE' then ''
ELSE 'X' END as Saida
from matdoc as Doc
inner join :it_mat as Mat
ON Mat.Material = Doc.matnr
and Mat.Centro = Doc.werks
and Mat.Fornecedor = Doc.lifnr
and Mat.tipo = 'S'
inner join :it_mov_sub as mov
ON mov.bwart = Doc.bwart
where Doc.xauto = ''
AND Doc.cancelled = ''
AND Doc.reversal_movement = ''
union all
-- Documento de transferencia
SELECT Doc.mandt as Client,
Doc.ebeln as Pedido,
Doc.ebelp as ItemPedido,
Doc.matnr as Material,
Doc.werks as Centro,
Doc.lifnr as Fornecedor,
Doc.budat as DtLancamento,
Doc.charg as Lote,
Doc.vfdat as DtVencLote,
Doc.ebeln as PedCKD,
Doc.lgort as Deposito,
Mat.tipo as Tipo,
CASE when Doc.blart = 'WE' then Doc.menge * -1
else Doc.menge end as Quantidade,
CASE when Doc.blart = 'WE' then ''
ELSE 'X' END as Saida
from matdoc as Doc
inner join :it_mat as Mat
ON Mat.Material = Doc.matnr
and Mat.Centro = Doc.werks
and Mat.tipo = 'T'
inner join :it_mov_trc as mov
ON mov.bwart = Doc.bwart
where Doc.xauto = ''
AND Doc.cancelled = ''
AND Doc.reversal_movement = '';
it_doc = APPLY_FILTER( :it_doc, :p_sql_werks );
-- Documento ckd
it_ckd = SELECT Doc.Material as Material,
Doc.Lote as Lote,
ItPedido.zzpaickd as PedCKD
from :it_doc as Doc
inner join matdoc as MatDoc
on MatDoc.matnr = Doc.Material
and MatDoc.charg = Doc.Lote
and MatDoc.lifnr = Doc.Fornecedor
and MatDoc.bwart = '101'
INNER JOIN ekpo as ItPedido
ON ItPedido.ebeln = MatDoc.ebeln
and ItPedido.ebelp = MatDoc.ebelp
and ItPedido.zzpaickd <> ''
where Doc.Lote <> '';
lv_index = 1;
lv_rows = record_count( :it_doc );
-- Coloco a DATA de lancamento da saída igual aos documentos de entrada para agrupar os valores
WHILE lv_index <= lv_rows DO
if :it_doc.Saida[ :lv_index ] = '' then
lv_pos = :it_doc.search( ( Pedido, Saida ), ( :it_doc.Pedido[ :lv_index ], 'X' ) );
IF lv_pos <> 0 then
it_doc.DtLancamento[ :lv_index ] = :it_doc.DtLancamento[ :lv_pos ] ;
it_doc.Deposito[ :lv_index ] = :it_doc.Deposito[ :lv_pos ] ;
END if;
END if;
lv_pos = :it_ckd.search( ( Material, Lote ),
( :it_doc.Pedido[ :lv_index ], :it_doc.Lote[ :lv_index ] ) );
IF lv_pos > 0 then
it_doc.PedCKD[ :lv_index ] = :it_ckd.PedCKD[ :lv_pos ];
ELSE it_doc.PedCKD[ :lv_index ] = '';
END if;
lv_index = lv_index + 1 ;
END WHILE ;
Nenhum comentário:
Postar um comentário