L_TO_CREATE_POSTING_CHANGE

Create TO according to posting change document

 

 

demo

function zwm_xxx_goods_move.

*"----------------------------------------------------------------------

*"*"Local Interface:

*" IMPORTING

*" VALUE(IV_TYPE) TYPE CHAR2

*" VALUE(IV_CHAR) TYPE CHAR50

*" VALUE(IT_DATA) TYPE ZWM_T_xxx_GOODS_MOVE

*" EXPORTING

*" VALUE(RTYPE) TYPE BAPI_MTYPE

*" VALUE(RTMSG) TYPE BAPI_MSG

*" VALUE(EV_MBLNR) TYPE MBLNR

*" VALUE(EV_TANUM) TYPE TANUM

*"----------------------------------------------------------------------

 

zfmparavalsave1 'ZWM_xxx_GOODS_MOVE'.

zfmparavalsave2 'B'.

 

iv_char = iv_char+0(32).

 

*lock

data:

lv_key(255),

lv_rtype type bapi_mtype,

lv_rtmsg type bapi_msg,

lv_bwart type zwm_s_xxx_goods_move-bwart,

lv_rs38l_fnam type rs38l_fnam,

lv_guid type /aeb/wu_pb_guid_32,

ls_data type zwm_s_xxx_goods_move,

sto_data type zwm_s_xxx_to_confirm,

to_data like table of zwm_s_xxx_to_confirm,

s_data like zwm_s_xxx_goods_move.

lv_rs38l_fnam = 'ZWM_xxx_PO_CREATE'.

lv_guid = iv_char.

lv_key = lv_rs38l_fnam && lv_guid.

data:lt_ltbp like table of ltbp,

ls_ltbp like ltbp.

data: ls_lubui type lubui,

ls_lubqu type lubqu,

lt_lubqu type table of lubqu,

ls_lqua type lqua,

lt_t_ltap_vb like table of ltap_vb,

ls_t_ltap_vb like ltap_vb.

perform frm_lock using 'ZWM_T_LOG' lv_key changing rtype rtmsg.

if rtype = 'E'.

zfmparavalsave2 'R'.

return.

endif.

 

*log

select single *

into @data (ls_zwm_t_log)

from zwm_t_log

where rs38l_fnam = 'ZWM_xxx_GOODS_MOVE' and guid = @iv_char and rtype = 'S'.

if sy-subrc = 0.

rtype = ls_zwm_t_log-rtype.

rtmsg = ls_zwm_t_log-rtmsg.

ev_mblnr = ls_zwm_t_log-mblnr.

ev_tanum = ls_zwm_t_log-tanum.

zfmparavalsave2 'R'.

return.

endif.

 

 

if iv_type <> '03' and iv_type <> '04'.

rtype = 'E'.

rtmsg = 'Business type must be 03 or 04'.

zfmparavalsave2 'R'.

return.

endif.

 

data:

ls_header like bapi2017_gm_head_01,

lv_code like bapi2017_gm_code,

iv_sonum like lqua-sonum,

lt_item like table of bapi2017_gm_item_create,

lt_return like table of bapiret2,

ls_item like bapi2017_gm_item_create,

ls_return like bapiret2,

ls_goodsmvt_headret like bapi2017_gm_head_ret,

lv_lgnum like ltak-lgnum,

lv_tbnum like ltak-tbnum,

lv_tanum like ltak-tanum,

lv_msg(255),

lt_ltak like table of ltak_vb,

lt_ltap_vb like table of ltap_vb,

ls_ltak like ltak_vb,

ls_ltap_vb like ltap_vb,

lt_fun_it_trite type l03b_trite_t,

wa_fun_it_trite like line of lt_fun_it_trite,

lt_fun_wmgrp_msg like table of wmgrp_msg,

lt_ltap_conf like table of ltap_conf,

ls_fun_wmgrp_msg like wmgrp_msg,

ls_ltap_conf like ltap_conf.

 

 

lv_code = iv_type.

loop at it_data into s_data.

at first.

read table it_data into ls_data index 1.

ls_header-pstng_date = ls_data-budat.

ls_header-doc_date = ls_data-bldat.

ls_header-header_txt = ls_data-bktxt.

lv_lgnum = ls_data-lgnum = '610'.

endat.

 

if s_data-bwart = '201' or s_data-bwart = '202' or s_data-bwart = '551' or s_data-bwart = '221' or s_data-bwart = '222'.

if lv_code <> '03'.

rtype = 'E'.

rtmsg = 'When the business type is 03, the move type must be 201,202,551,221,222'.

zfmparavalsave2 'R'.

return.

endif.

endif.

 

if s_data-bwart = '343' or s_data-bwart = '344' or s_data-bwart = '411' or s_data-bwart = '412' or s_data-bwart = '413'.

if lv_code <> '04'.

rtype = 'E'.

rtmsg = 'When the business type is 04, the move type must be 343,344,411,412,413'.

zfmparavalsave2 'R'.

return.

endif.

endif.

if sy-tcode ne 'SE37'.

perform frm_cunit_in changing s_data-erfme.

endif.

clear:ls_item,lt_ltap_vb,lt_fun_it_trite.

ls_item-plant = s_data-werks.

ls_item-material = s_data-matnr.

perform frm_matn1_input changing ls_item-material.

ls_item-entry_qnt = s_data-erfmg.

ls_item-entry_uom = s_data-erfme.

ls_item-stge_loc = s_data-lgort.

ls_item-move_type = s_data-bwart.

ls_item-spec_stock = s_data-sobkz.

ls_item-stck_type = s_data-insmk.

select single xchpf from mara into @data(lv_xchpf) where matnr = @ls_item-material and xchpf = 'X'.

if sy-subrc eq 0.

ls_item-batch = s_data-charg.

endif.

 

 

if s_data-kostl is initial and ( s_data-bwart = '201' or s_data-bwart = '202' ).

rtype = 'E'.

rtmsg = 'When the move type is 201 or 202, the cost center must have a value'.

zfmparavalsave2 'R'.

return.

endif.

"zfmparavalsave2 'R'.

ls_item-costcenter = s_data-kostl.

perform frm_alpha_input changing ls_item-costcenter.

"zfmparavalsave2 'R'.

* if t_data-umlgo is initial and t_data-sobkz ne 'E' and ( t_data-bwart = '411' or t_data-bwart = '412' ).

* rtype = 'E'.

* rtmsg = 'When the move type is 411 or 412, the UMLGO must have a value'.

* zfmparavalsave2 'R'.

* return.

* endif.

* lt_item-move_stloc = t_data-umlgo.

if s_data-ps_psp_pnr is initial and ( s_data-bwart = '221' or s_data-bwart = '222' or s_data-bwart = '551' or ( s_data-bwart = '412' and s_data-sobkz = 'E' ) ).

rtype = 'E'.

rtmsg = 'When the move type is 221 or 222, the WBS element must have a value'.

zfmparavalsave2 'R'.

return.

endif.

if s_data-ps_psp_pnr is initial and s_data-bwart = '412' and s_data-sobkz = 'E'.

rtype = 'E'.

rtmsg = 'When the move type is 412 E, the WBS element must have a value'.

zfmparavalsave2 'R'.

return.

endif.

ls_item-wbs_elem = s_data-ps_psp_pnr.

if s_data-lifnr is initial and s_data-bwart = '411' and s_data-sobkz = 'K'.

rtype = 'E'.

rtmsg = 'When the move type is 411, the vendor must have a value'.

zfmparavalsave2 'R'.

return.

endif.

 

ls_item-vendor = s_data-lifnr.

perform frm_alpha_input changing ls_item-vendor.

if ( s_data-bwart = '413' or s_data-bwart = '412' or s_data-bwart = '411' ) and ( s_data-kdauf is initial or s_data-kdpos is initial ) and s_data-sobkz = 'E'.

rtype = 'E'.

rtmsg = 'When the move type is 413-E or 412-E or 411-E, the Sales Order and Sales Ord item must have a value'.

zfmparavalsave2 'R'.

return.

endif.

ls_item-val_sales_ord = s_data-kdauf.

ls_item-val_s_ord_item = s_data-kdpos.

ls_item-sales_ord = s_data-zkdauf.

ls_item-s_ord_item = s_data-zkdpos.

 

if s_data-aufnr is initial and ( s_data-bwart = '261' or s_data-bwart = '262' ).

rtype = 'E'.

rtmsg = 'When the move type is 262 or 261, the order number must have a value'.

zfmparavalsave2 'R'.

return.

endif.

"if t_data-bwart = '343' or t_data-bwart = '344'.

lv_bwart = s_data-bwart.

"endif.

 

ls_item-orderid = s_data-aufnr.

 

* lt_item-stge_type_pc = t_data-vltyp .

* lt_item-stge_bin_pc = t_data-lgpla.

*lt_item-NO_PST_CHGNT = 'X'.

if s_data-bwart = '411' and s_data-sobkz = 'K'.

ls_item-move_mat = ls_item-material.

ls_item-move_plant = s_data-werks.

ls_item-move_stloc = ls_item-stge_loc.

endif.

 

append ls_item to lt_item.

endloop.

"zfmparavalsave2 'R'.

set update task local.

"zfmparavalsave2 'R'.

call function 'BAPI_GOODSMVT_CREATE'

exporting

goodsmvt_header = ls_header

goodsmvt_code = lv_code

importing

goodsmvt_headret = ls_goodsmvt_headret

tables

goodsmvt_item = lt_item

return = lt_return.

 

if ls_goodsmvt_headret-mat_doc is not initial.

call function 'BAPI_TRANSACTION_COMMIT'

exporting

wait = 'X'.

 

ev_mblnr = ls_goodsmvt_headret-mat_doc.

"rtype = 'S'.

"rtmsg = 'success'.

 

"""create tr

select single tbnum into lv_tbnum from mseg where mblnr = ls_goodsmvt_headret-mat_doc and mjahr = ls_header-doc_date+0(4).

"ls_ltak-mblnr = ls_goodsmvt_headret-mat_doc.

"lt_ltak-lgnum = '610'.

"ls_ltak-mjahr = ls_header-doc_date+0(4).

"append ls_ltak to lt_ltak.

 

clear:lt_fun_it_trite[],lt_ltak[],lt_ltap_vb[],lt_ltbp,ls_ltbp.

select * into corresponding fields of table lt_ltbp

from ltbp

where lgnum = lv_lgnum and tbnum = lv_tbnum.

if sy-subrc eq 0.

 

loop at lt_ltbp into ls_ltbp.

clear:s_data,wa_fun_it_trite,ls_t_ltap_vb,ls_lqua.

move-corresponding ls_ltbp to wa_fun_it_trite.

read table it_data into s_data with key matnr = ls_ltbp-matnr.

if sy-subrc = 0.

perform frm_matn1_input changing s_data-matnr.

select single * from lqua into ls_lqua

where lgnum = lv_lgnum

and matnr = s_data-matnr

and werks = s_data-werks

and lgort = s_data-lgort

and bestq = ' '

and lgtyp = s_data-vltyp and lgpla = s_data-lgpla "chenyl for 25.10.2020 00:47:57

and verme > 0

and skzsi = ' '.

if sy-subrc ne 0.

ls_t_ltap_vb-nlqnr = ls_lqua-lqnum.

endif.

if sy-tcode ne 'SE37'.

perform frm_cunit_in changing s_data-erfme.

endif.

wa_fun_it_trite-anfme = s_data-erfmg." ????

wa_fun_it_trite-altme = s_data-erfme."

 

wa_fun_it_trite-nltyp = s_data-nltyp." ??????

wa_fun_it_trite-nlpla = s_data-nlpla." ????

wa_fun_it_trite-vltyp = s_data-vltyp." ??????

wa_fun_it_trite-vlpla = s_data-lgpla. " ????

wa_fun_it_trite-nlber = s_data-nlber.

select single xchpf from mara into @data(lv_xchpf1) where matnr = @s_data-matnr and xchpf = 'X'.

if sy-subrc eq 0.

wa_fun_it_trite-charg = s_data-charg.

ls_t_ltap_vb-charg = s_data-charg.

endif.

append wa_fun_it_trite to lt_fun_it_trite.

 

ls_t_ltap_vb-lgnum = s_data-lgnum .

ls_t_ltap_vb-vltyp = s_data-vltyp .

ls_t_ltap_vb-vlpla = s_data-lgpla .

ls_t_ltap_vb-vlber = s_data-vlber .

 

append ls_t_ltap_vb to lt_t_ltap_vb.

endif.

 

endloop.

endif.

 

select single lgnum,ubnum from mseg into @data(ls_mseg) where mblnr = @ls_goodsmvt_headret-mat_doc

and mjahr = @ls_goodsmvt_headret-doc_year

and line = 1.

if sy-subrc <> 0.

rtype = 'E'.

rtmsg = rtmsg && '#But Document Segment not found!'.

zfmparavalsave2 'R'.

return.

endif.

clear:s_data.

read table it_data into s_data index 1.

"if lv_bwart = '411' and t_data-sobkz = 'K'.

if s_data-sobkz = 'K'.

select single tanum into ev_tanum from ltak where lgnum = '610' and ubnum = ls_mseg-ubnum.

if sy-subrc eq 0.

rtype = 'S'.

rtmsg = 'SUCCESS'.

ls_zwm_t_log-rs38l_fnam = 'ZWM_xxx_GOODS_MOVE'.

ls_zwm_t_log-guid = iv_char.

ls_zwm_t_log-mblnr = ev_mblnr.

ls_zwm_t_log-tanum = ev_tanum.

ls_zwm_t_log-rtype = rtype.

ls_zwm_t_log-rtmsg = rtmsg.

modify zwm_t_log from ls_zwm_t_log.

 

zfmparavalsave2 'R'.

return.

endif.

endif.

 

*wait for lock

data lt_seqg3 like table of seqg3.

data ls_seqg3 like seqg3.

data(lv_garg) = sy-mandt && ls_mseg-lgnum && ls_mseg-ubnum.

do.

call function 'ENQUEUE_READ'

exporting

guname = sy-uname

tables

enq = lt_seqg3.

* read table lt_seqg3 into ls_seqg3 with key gname = 'LUBU'.

* if sy-subrc ne 0 or ls_seqg3-garg ne lv_garg.

read table lt_seqg3 into ls_seqg3 with key gname = 'LUBU' garg = lv_garg. "chenyl for 07.02.2021 16:00:02

if sy-subrc ne 0.

exit.

endif.

clear:ls_seqg3.

enddo.

set update task local.

 

if iv_type = '04'.

clear:lt_lubqu,iv_sonum.

do 1 times.

clear:s_data.

read table it_data into s_data index 1.

clear:ls_lqua.

* select single * from lqua into ls_lqua

* where lgnum = lv_lgnum

* and matnr = t_data-matnr

* and werks = t_data-werks

* and lgort = t_data-lgort

* and sobkz = t_data-sobkz

* and verme > 0

* and skzsi = ' '.

 

perform frm_alpha_input changing s_data-kdauf.

perform frm_alpha_input changing s_data-kdpos.

perform frm_matn1_input changing s_data-matnr.

iv_sonum = s_data-kdauf && s_data-kdpos.

if lv_bwart = '344'.

iv_sonum = ''.

endif.

 

if lv_bwart = '411' and s_data-sobkz = 'K'.

iv_sonum = s_data-lifnr.

endif.

select single * from lqua into ls_lqua

where lgnum = lv_lgnum

and matnr = s_data-matnr

and werks = s_data-werks

and lgort = s_data-lgort

and sobkz = s_data-sobkz

and sonum = iv_sonum

and bestq = ''

and lgtyp = s_data-vltyp and lgpla = s_data-lgpla "chenyl for 25.10.2020 00:47:57

and verme > 0

and skzsi = ' '.

if lv_bwart = '343'.

select single * from lqua into ls_lqua

where lgnum = lv_lgnum

and matnr = s_data-matnr

and werks = s_data-werks

and lgort = s_data-lgort

and sobkz = s_data-sobkz

"And tap = iv_sonum

and bestq = 'S'

and lgtyp = s_data-vltyp and lgpla = s_data-lgpla "chenyl for 25.10.2020 00:47:57

and verme > 0

and skzsi = ' '.

endif.

if lv_bwart = '412' and s_data-sobkz = 'E'.

select single * from lqua into ls_lqua

where lgnum = lv_lgnum

and matnr = s_data-matnr

and werks = s_data-werks

and lgort = s_data-lgort

and sobkz = ''

"And tap = iv_sonum

and bestq = ''

and lgtyp = s_data-vltyp and lgpla = s_data-lgpla "chenyl for 25.10.2020 00:47:57

"and verme > 0

and skzsi = ' '.

endif.

if sy-subrc ne 0. "do not specify source storage, there must be only one correct quant

select single * from lqua into ls_lqua

where lgnum = lv_lgnum

and matnr = s_data-matnr

and werks = s_data-werks

and lgort = s_data-lgort

and sobkz = s_data-sobkz

* and LGTYP = t_data-vltyp and LGPLA = t_data-lgpla "chenyl for 25.10.2020 00:47:57

and bestq = '' "maybe last sql also need this condition

and verme > 0

and skzsi = ' '.

endif.

 

if sy-subrc ne 0.

rtype = 'E'.

rtmsg = rtmsg && '#But quant not found!'.

zfmparavalsave2 'R'.

return.

endif.

 

clear ls_lubqu.

ls_lubqu-lqnum = ls_lqua-lqnum.

ls_lubqu-quantity = s_data-erfmg.

ls_lubqu-kzuap = 'X'.

* ls_lubqu-nltyp = t_data-nltyp. "Destination Storage Type

* ls_lubqu-nlber = t_data-nlber. "Destination storage section

* ls_lubqu-nlpla = t_data-nlpla . "Destination Storage Bin

* ls_lubqu-nppos = p_ls_lqua-plpos.

* ls_lubqu-nlenr = ''.

* ls_lubqu-squit = 'X'.

* ls_lubqu-letyp = p_ls_lqua-letyp.

* ls_lubqu-zeugn = ''.

ls_lubqu-wdatu = sy-datum.

* ls_lubqu-vfdat = ls_lqua-vfdat.

* ls_lubqu-wempf = ''.

append ls_lubqu to lt_lubqu.

 

ls_t_ltap_vb-lgnum = s_data-lgnum .

ls_t_ltap_vb-vltyp = s_data-vltyp .

ls_t_ltap_vb-vlpla = s_data-lgpla .

ls_t_ltap_vb-vlber = s_data-vlber .

select single xchpf from mara into @data(lv_xchpf2) where matnr = @s_data-matnr and xchpf = 'X'.

if sy-subrc eq 0.

ls_t_ltap_vb-charg = s_data-charg.

endif.

append ls_t_ltap_vb to lt_t_ltap_vb.

 

enddo.

" lv_tanum = lines( lt_lubqu ).

WAIT UP TO 1 SECONDS.

call function 'L_TO_CREATE_POSTING_CHANGE'

exporting

i_lgnum = lv_lgnum

i_ubnum = ls_mseg-ubnum

* i_lubui = lubui

i_squit = 'X'

* i_nidru = con_x

i_nidru = 'X'

i_update_task = 'X'

i_commit_work = 'X'

* i_bname = sy-uname

importing

e_tanum = lv_tanum

tables

t_lubqu = lt_lubqu

t_ltap_vb = lt_t_ltap_vb

exceptions

error_message = 99.

else.

call function 'L_TO_CREATE_TR'

exporting

i_lgnum = lv_lgnum "

i_tbnum = lv_tbnum "

i_update_task = 'X'

i_commit_work = 'X'

* i_squit = 'X'

* i_bname = sy-uname

i_nidru = 'X'

it_trite = lt_fun_it_trite[]

importing

e_tanum = lv_tanum

* E_TEILK =

tables

* t_ltak = lt_ltak

t_ltap_vb = lt_t_ltap_vb

t_wmgrp_msg = lt_fun_wmgrp_msg

exceptions

foreign_lock = 1

qm_relevant = 2

tr_completed = 3

xfeld_wrong = 4

ldest_wrong = 5

drukz_wrong = 6

tr_wrong = 7

squit_forbidden = 8

no_to_created = 9

update_without_commit = 10

no_authority = 11

preallocated_stock = 12

partial_transfer_req_forbidden = 13

input_error = 14

others = 15.

endif.

 

if lv_tanum is not initial.

call function 'BAPI_TRANSACTION_COMMIT'

exporting

wait = 'X'.

 

ev_tanum = lv_tanum.

rtype = 'S'.

rtmsg = 'Success'.

"""" 03 need confirm TO

if iv_type = '03'.

data:iv_kvqui,iv_quknz.

if lv_bwart = '221'. "two step

iv_kvqui = 'X'.

else."one step

iv_quknz = 'X'.

endif.

 

"""""call ZWM_xxx_TO_CONFIRM to confirm TO

sto_data-lgnum = lv_lgnum.

sto_data-tanum = lv_tanum.

append sto_data to to_data.

 

call function 'ZWM_xxx_TO_CONFIRM'

exporting

iv_lgnum = lv_lgnum

iv_kvqui = iv_kvqui "two step

iv_quknz = iv_quknz "one step

iv_qtran = '' "transfer

it_data = to_data

importing

rtype = lv_rtype

rtmsg = lv_rtmsg.

"tables

" t_data = to_data.

 

if lv_rtype <> 'S'.

rtype = 'E'.

rtmsg = 'Material document create success, Transfer Odr create success,But TO confirm fail:' && lv_rtmsg.

endif.

clear:sto_data,to_data.

endif.

else.

message id sy-msgid

type sy-msgty

number sy-msgno

with sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4 into rtmsg.

rtype = 'E'.

rtmsg = 'Material document create success, Transfer Order create fail #' && rtmsg.

call function 'BAPI_TRANSACTION_ROLLBACK'.

 

if iv_type = '04'.

clear:s_data.

read table it_data into s_data index 1.

"if lv_bwart = '411' and t_data-sobkz = 'K'.

"if s_data-sobkz = 'K'.

select single tanum into ev_tanum from ltak where lgnum = '610' and ubnum = ls_mseg-ubnum.

if ev_tanum is not initial.

rtype = 'S'.

rtmsg = 'SUCCESS'.

ls_zwm_t_log-rs38l_fnam = 'ZWM_xxx_GOODS_MOVE'.

ls_zwm_t_log-guid = iv_char.

ls_zwm_t_log-mblnr = ev_mblnr.

ls_zwm_t_log-tanum = ev_tanum.

ls_zwm_t_log-rtype = rtype.

ls_zwm_t_log-rtmsg = rtmsg.

modify zwm_t_log from ls_zwm_t_log.

 

zfmparavalsave2 'R'.

return.

endif.

"endif.

endif.

 

endif.

else.

call function 'BAPI_TRANSACTION_ROLLBACK'.

loop at lt_return into ls_return where type ca 'AEX'.

rtype = 'E'.

message id ls_return-id type ls_return-type number ls_return-number

with ls_return-message_v1 ls_return-message_v2

ls_return-message_v3 ls_return-message_v4

into lv_msg.

rtmsg = rtmsg && lv_msg && '#'.

endloop.

endif.

 

" RTTAB = it_data.

 

*save log

ls_zwm_t_log-rs38l_fnam = 'ZWM_xxx_GOODS_MOVE'.

ls_zwm_t_log-guid = iv_char.

ls_zwm_t_log-mblnr = ev_mblnr.

ls_zwm_t_log-tanum = ev_tanum.

ls_zwm_t_log-rtype = rtype.

ls_zwm_t_log-rtmsg = rtmsg.

modify zwm_t_log from ls_zwm_t_log.

 

zfmparavalsave2 'R'.

endfunction.

 

Guess you like

Origin blog.csdn.net/cylcylcylcylwo/article/details/114078905