segunda-feira, 4 de outubro de 2010

BAPI BAPI_SALESORDER_CHANGE

Segue um exemplo de como utilizar a bapi BAPI_SALESORDER_CHANGE para retirar quantidade de um item de uma ordem disponivel na VA02.
Como é um trecho de um programa apenas, podem observar que existe lixo nesta copia de código abaixo, podendo descartar algumas coisas...


types:
begin of ty_aufk,
aufnr type aufk-aufnr,
kdauf type aufk-kdauf,
end of ty_aufk,
begin of ty_aufk_aux,
aufnr type zqme_est_placa-aufnr,
charg type zqme_est_placa-charg,
erfmg type zqme_est_placa-erfmg,
kdauf type aufk-kdauf,
end of ty_aufk_aux,

begin of ty_mska,
charg type mska-charg ,
vbeln type mska-vbeln ,
posnr type mska-posnr ,
kalab type mska-kalab ,
end of ty_mska,

begin of ty_schedule_line,
vbeln type vbep-vbeln, "Sales Document
posnr type vbep-posnr, "Sales Document Item
etenr type vbep-etenr, "Delivery Schedule Line Number
ettyp type vbep-ettyp, "Schedule line category
wmeng type vbep-wmeng,
vrkme type vbep-vrkme,
edatu type vbep-edatu,
end of ty_schedule_line.


data: t_aufk type standard table of ty_aufk,
t_aufk_aux type standard table of ty_aufk_aux,
t_mska type standard table of ty_mska,
t_estpl_aux1 type standard table of zqme_est_placa,
t_processcome type standard table of zppt_processcome,
t_schedule_line type standard table of ty_schedule_line,
wa_processcome like line of t_processcome,
wa_aufk like line of t_aufk,
wa_aufk_aux like line of t_aufk_aux,
wa_mska like line of t_mska,
wa_estlp_aux1 like line of t_estpl_aux1,
wa_schedule_line like line of t_schedule_line.

*-BAPI Sales Order change
data: t_order_item_in_bapi type standard table of bapisditm,
t_order_item_inx_bapi type standard table of bapisditmx,
t_schedule_lines_bapi type standard table of bapischdl,
t_schedule_linesx_bapi type standard table of bapischdlx,
t_return_so_change type standard table of bapiret2,
t_schedule_line_loc type standard table of ty_schedule_line,

*-BAPI_SALESORDER_GETSTATUS
l_return like bapireturn,
lt_statusinfo type standard table of bapisdstat,
lwa_statusinfo like line of lt_statusinfo,


wa_order_item_in_bapi like line of t_order_item_in_bapi,
wa_order_item_inx_bapi like line of t_order_item_inx_bapi,
wa_schedule_lines_bapi like line of t_schedule_lines_bapi,
wa_schedule_linesx_bapi like line of t_schedule_linesx_bapi,
wa_return_so_change like line of t_return_so_change,
wa_return_commit type bapiret2,


l_salesdocument type bapivbeln-vbeln,
wa_order_header_inx type bapisdh1x,
l_posnr type mska-posnr,
l_error_bapi type i,
l_message(80) type c,
l_erfmg type erfmg.


l_posnr = 10.

loop at t_estpl_aux1 into wa_estlp_aux1.
read table t_aufk into wa_aufk with key aufnr = wa_estlp_aux1-aufnr.
if sy-subrc = 0.
l_salesdocument = wa_aufk-kdauf.
wa_order_header_inx-updateflag = 'U'.
call function 'BAPI_SALESORDER_GETSTATUS'
exporting
salesdocument = l_salesdocument
importing
return = l_return
tables
statusinfo = lt_statusinfo.

if not lt_statusinfo[] is initial.

select vbeln posnr etenr ettyp wmeng vrkme edatu "#CR
into table t_schedule_line
from vbep
for all entries in lt_statusinfo
where vbeln = l_salesdocument
and posnr = lt_statusinfo-itm_number.
if sy-subrc = 0.

sort t_schedule_line by edatu descending posnr ascending.

t_schedule_line_loc[] = t_schedule_line[].

read table t_aufk_aux into wa_aufk_aux with key kdauf = wa_aufk-kdauf
aufnr = wa_estlp_aux1-aufnr
charg = wa_estlp_aux1-charg.
if sy-subrc = 0.
read table t_mska into wa_mska with key charg = wa_aufk_aux-charg
vbeln = wa_aufk_aux-kdauf.
if sy-subrc = 0.
wa_order_item_in_bapi-itm_number = wa_mska-posnr.
wa_order_item_inx_bapi-itm_number = wa_mska-posnr.
wa_order_item_inx_bapi-updateflag = 'U'.
read table lt_statusinfo into lwa_statusinfo with key itm_number = wa_order_item_in_bapi-itm_number.
if sy-subrc = 0.
wa_order_item_in_bapi-target_qty = lwa_statusinfo-req_qty - wa_estlp_aux1-erfmg.
wa_order_item_inx_bapi-target_qty = 'X'.
append: wa_order_item_in_bapi to t_order_item_in_bapi,
wa_order_item_inx_bapi to t_order_item_inx_bapi.
endif.
t_schedule_line_loc[] = t_schedule_line[].

delete t_schedule_line_loc where posnr ne wa_mska-posnr.
sort t_schedule_line_loc by edatu descending posnr ascending.
loop at t_schedule_line_loc into wa_schedule_line.
wa_schedule_lines_bapi-itm_number = wa_schedule_line-posnr.
wa_schedule_linesx_bapi-itm_number = wa_schedule_line-posnr.
wa_schedule_lines_bapi-sched_line = wa_schedule_line-etenr.
wa_schedule_linesx_bapi-sched_line = wa_schedule_line-etenr.
wa_schedule_lines_bapi-sched_type = wa_schedule_line-ettyp.
wa_schedule_lines_bapi-req_date = wa_schedule_line-edatu.
wa_schedule_linesx_bapi-req_date = 'X'.
wa_schedule_linesx_bapi-sched_type = 'X'.
if sy-tabix = '1'.
l_erfmg = wa_estlp_aux1-erfmg.
endif.
if wa_schedule_line-wmeng > l_erfmg.
wa_schedule_lines_bapi-req_qty = wa_schedule_line-wmeng - l_erfmg.
wa_schedule_linesx_bapi-req_qty = 'X'.
wa_schedule_linesx_bapi-updateflag = 'U'.
append wa_schedule_lines_bapi to t_schedule_lines_bapi[].
append wa_schedule_linesx_bapi to t_schedule_linesx_bapi[].
else.
l_erfmg = l_erfmg - wa_schedule_line-wmeng.
wa_schedule_lines_bapi-req_qty = 0.
wa_schedule_linesx_bapi-req_qty = 'X'.
wa_schedule_linesx_bapi-updateflag = 'D'.
append wa_schedule_lines_bapi to t_schedule_lines_bapi[].
append wa_schedule_linesx_bapi to t_schedule_linesx_bapi[].
endif.
endloop.

clear: wa_order_item_in_bapi, wa_order_item_inx_bapi.
read table lt_statusinfo into lwa_statusinfo with key itm_number = l_posnr.
if sy-subrc = 0.
wa_order_item_in_bapi-itm_number = l_posnr.
wa_order_item_inx_bapi-itm_number = l_posnr.
wa_order_item_inx_bapi-updateflag = 'U'.
wa_order_item_in_bapi-target_qty = lwa_statusinfo-req_qty + wa_estlp_aux1-erfmg.
wa_order_item_inx_bapi-target_qty = 'X'.
append: wa_order_item_in_bapi to t_order_item_in_bapi,
wa_order_item_inx_bapi to t_order_item_inx_bapi.
endif.

t_schedule_line_loc[] = t_schedule_line[].

delete t_schedule_line_loc where posnr ne l_posnr.
sort t_schedule_line_loc by edatu descending posnr ascending.
read table t_schedule_line_loc into wa_schedule_line index 1.
if sy-subrc = 0.
wa_schedule_lines_bapi-itm_number = wa_schedule_line-posnr.
wa_schedule_linesx_bapi-itm_number = wa_schedule_line-posnr.
wa_schedule_lines_bapi-sched_line = wa_schedule_line-etenr.
wa_schedule_linesx_bapi-sched_line = wa_schedule_line-etenr.
wa_schedule_lines_bapi-sched_type = wa_schedule_line-ettyp.
wa_schedule_lines_bapi-req_date = wa_schedule_line-edatu.
wa_schedule_linesx_bapi-req_date = 'X'.
wa_schedule_linesx_bapi-sched_type = 'X'.

wa_schedule_lines_bapi-req_qty = wa_schedule_line-wmeng + wa_estlp_aux1-erfmg.
wa_schedule_linesx_bapi-req_qty = 'X'.
wa_schedule_linesx_bapi-updateflag = 'U'.
append wa_schedule_lines_bapi to t_schedule_lines_bapi[].
append wa_schedule_linesx_bapi to t_schedule_linesx_bapi[].
endif.
endif.
endif.
call function 'BAPI_SALESORDER_CHANGE'
exporting
salesdocument = l_salesdocument
order_header_inx = wa_order_header_inx
tables
return = t_return_so_change
order_item_in = t_order_item_in_bapi
order_item_inx = t_order_item_inx_bapi
schedule_lines = t_schedule_lines_bapi
schedule_linesx = t_schedule_linesx_bapi.

read table t_return_so_change into wa_return_so_change with key type = 'E'.
if sy-subrc = 0.
l_message = wa_return_so_change-message.

m_insere_log: 'Documento não foi modificado ' .
m_insere_log: l_salesdocument .
m_insere_log: l_message.
m_insere_log: '------------------------------------------' .

else.
call function 'BAPI_TRANSACTION_COMMIT'
exporting
wait = 'X'
importing
return = wa_return_commit.
endif.


endif.
endif.
endif.

endloop.



Inserindo botão em tela 1000

Para inserirmos um botão no topo de uma tela 1000 existe a seguinte linha de comando:

selection-screen function key 1.

Para utiliza-la basta utilizar a colinha abaixo:

report zinsertrequest.

type-pools: ctslg , icon, slis.
include: <icon>.

tables: e070, sscrfields.
include: rsvcmacr.

************************************************************************
* Definição de estruturas: TY_... *
************************************************************************
types:
begin of ty_requests,
trkorr like e070-trkorr, "REQUEST
ddtext type dd07t-ddtext, "TIPO REQUEST
ambiente(100), "LIKE tstrfcofil-tarsystem, "Ambiente
as4user like e070-as4user,"USUARIO
autor type sy-uname,
as4date like e070-as4date,"DATA
as4time like e070-as4time,"HORA
as4text like e07t-as4text,"DESCRICAO
end of ty_requests,
begin of ty_dd07t,
domvalue_l type dd07t-domvalue_l,
ddtext type dd07t-ddtext,
end of ty_dd07t,
begin of ty_ctslg_cofile,
exists type c,
imported type c,
del_lines_only type c,
systems type ctslg_systems,
merges type ctslg_mergelines,
rc type i,
end of ty_ctslg_cofile.

data: d_butt1(4).

************************************************************************
* Variáveis de Referência ( RV_...) *
************************************************************************
data: rv_structure type ref to data,
rv_table type ref to data,
rv_struc_desc type ref to cl_abap_structdescr,

************************************************************************
* Tabela Interna ( T_...) *
************************************************************************
t_lvc_fieldcatalogue type lvc_t_fcat,
t_fieldcatalogue type slis_t_fieldcat_alv,
t_es_cofile type ty_ctslg_cofile,
t_e070 type standard table of e070,
t_e07t type standard table of e07t,
t_amb type standard table of ctslg_system,
t_requests type standard table of ty_requests,
t_dd07t type standard table of ty_dd07t,
t_tp_obj type standard table of ko100,

************************************************************************
* Work Area ( wa_...) *
************************************************************************
wa_layout type slis_layout_alv,
wa_fieldcatalogue type slis_fieldcat_alv,
wa_lvc_fieldcatalogue type lvc_s_fcat,
wa_e070 like line of t_e070,
wa_e07t like line of t_e07t,
wa_amb like line of t_amb,
wa_requests like line of t_requests,
wa_dd07t like line of t_dd07t,
wa_tp_obj like line of t_tp_obj.

************************************************************************
* Field Symbols (< fs_...> ) *
************************************************************************
*field symbols declaration
field-symbols :
<fs_it_table> type standard table,
<fs_dyn_str> type any,
<fs_str_comp> type abap_compdescr.

************************************************************************
* Constantes ( c_...) *
************************************************************************
data: c_tab type dd02l-tabname value 'ZTUPDREQ'.

************************************************************************
* Variáveis Globais (v_...) *
************************************************************************
data : v_t1 type dd02t-ddtext,
v_t2(50) type c,
v_t3(50) type c,
v_table type dd02l-tabname.


************************************************************************
* Tela de seleção
************************************************************************
selection-screen begin of block b1 with frame title text-001.

select-options: s_req for e070-trkorr.
selection-screen function key 1.

selection-screen end of block b1.

************************************************************************
initialization.
************************************************************************
move 'Exibir Registros' to sscrfields-functxt_01.
move 'Exibir Registros' to sscrfields-functxt_02.
d_butt1 = 'NO'.

v_table = c_tab.
select single ddtext
from dd02t
into v_t1
where tabname = c_tab
and ddlanguage = sy-langu.

************************************************************************
at selection-screen.
************************************************************************
if sy-ucomm = 'FC01'.
d_butt1 = 'YES'.
sscrfields-ucomm = 'ONLI'.
endif.

************************************************************************
start-of-selection.
************************************************************************
if d_butt1 = 'NO'.
if not s_req[] is initial.
perform seleciona_dados.
if not t_requests[] is initial.
perform insere_reg_tab.
endif.
endif.
elseif d_butt1 = 'YES'.
perform exibe_alv.
d_butt1 = 'NO'.
endif.