8101 AA01 30-00A037-001-R000 0000001579
8101 AA01 30-00A037-001-R000 0000001580
8101 AA01 30-00A037-001-R000 0000001581
8101 QJ99 30-00A037-001-R000 0000001579
*&---------------------------------------------------------------------*
*& Progarm : ZMMR002A Author : Jimmy Wong
*& Created : 15 Apr 2013 App : MM
*& Title : Upload Stock 561 & Cancal 561 = 562
*& Description : Upload Stock & Cancel Material Document No.
*&---------------------------------------------------------------------*
*& Version Author Date description
*& Jimmy 15 Apr 2013 the first version
*& the last update time 2013.04.15 14:00
*&---------------------------------------------------------------------*
report zmmr002a no standard page heading.
*&---------------------------------------------------------------------*
* database table
*&---------------------------------------------------------------------*
tables : rlgrap."T148,t001w,t001l,mara,t006.
*&---------------------------------------------------------------------*
* internal table & variables
*&---------------------------------------------------------------------*
data:begin of it_record occurs 0,
order type i,
order_t type c length 30,
bwart like mseg-bwart,
sobkz like mseg-sobkz,
werks like mseg-werks,
lgort like mseg-lgort,
matnr like mseg-matnr,
matnr_c(20) type c,
menge like mseg-menge,
menge_c(20) type c,
bstar like iseg-bstar,
charg like mseg-charg,
lifnr like lfa1-lifnr,
kunnr like kna1-kunnr,
posid like prps-posid,
message(700) type c,
end of it_record.
data:begin of it_err occurs 0.
include structure it_record.
data: matdoc like bapi2017_gm_head_ret-mat_doc,
matdoc_c like bapi2017_gm_head_ret-mat_doc,
end of it_err.
data: it_err2 like it_err occurs 0 with header line.
data: it_err3 like it_err occurs 0 with header line.
data: it_err4 like it_err occurs 0 with header line.
types:lv_type type c length 50.
data:begin of itab occurs 0,
werks type lv_type,
lgort type lv_type,
matnr_c type lv_type,
charg type lv_type,
menge_c type lv_type,
bstar type lv_type,
sobkz type lv_type,
lifnr type lv_type,
kunnr type lv_type,
posid type lv_type,
end of itab.
data:begin of it_tab occurs 0.
include structure itab.
data: message(700) type c,
end of it_tab.
data: lv_tabix like sy-tabix.
data:it_gdmvt_head like bapi2017_gm_head_01,
it_afs_item like bapi2017_gm_item_create
occurs 0 with header line,
it_serial like bapi2017_gm_serialnumber occurs 0 with header line,
lv_mdoc like bapi2017_gm_head_ret-mat_doc,
lv_year like bapi2017_gm_head_ret-doc_year,
it_ret like bapi2017_gm_head_ret.
data:it_gdmvt_code like bapi2017_gm_code value '05'.
data:it_return like bapiret2 occurs 0 with header line.
type-pools: slis.
data: fc_hier type slis_t_fieldcat_alv,
wa_hier type slis_fieldcat_alv .
data: msg type string.
constants:c_chk_num like pa0001-ename value '0123456789.'.
data: begin of it_down occurs 0,
line(1000) type c,
end of it_down .
data: begin of i_file occurs 0,
line(1000) type c,
end of i_file.
data: gv_file like rlgrap-filename.
*----------------------------------------------------------------------*
* Parameter & Select-Options *
*----------------------------------------------------------------------*
selection-screen begin of block 1 with frame title text-001.
parameter : filename like rlgrap-filename obligatory.
parameters: pr_error like rlgrap-filename obligatory default 'C:\'.
parameters: p_test as checkbox default 'X' .
parameters: p_run as checkbox default 'X' .
selection-screen end of block 1.
*----------------------------------------------------------------------*
* Initialize *
*----------------------------------------------------------------------*
initialization.
*----------------------------------------------------------------------*
* AT SELECTION-SCREEN
*----------------------------------------------------------------------*
at selection-screen on value-request for filename.
perform f4_filename using filename.
at selection-screen on value-request for pr_error.
perform f4_filename using pr_error.
*&---------------------------------------------------------------------*
* start of process
*&---------------------------------------------------------------------*
start-of-selection.
refresh: it_record.
perform get_data.
perform get_it_record2.
perform check_it_record.
if p_run is initial.
perform process_data.
else.
it_err3[] = it_record[].
endif.
if it_err3[] is initial.
message i003(zmm).
exit.
endif.
perform down_file .
if p_test = 'X'.
perform download_local.
perform write_data.
else.
perform download_server.
endif.
end-of-selection.
*&---------------------------------------------------------------------*
*& Form UP_SELECT
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
form process_data .
data: lv_sernr type mase-lsernr,
lv_i type i,
lv_j type i,
lv_tabix like sy-tabix,
lv_c type c length 20.
lv_i = 1.
lv_j = 1.
sort it_record by werks lgort sobkz matnr .
loop at it_record.
lv_tabix = sy-tabix.
if not it_record-message = ' OK.'.
move-corresponding it_record to it_err2.
append it_err2.
clear it_err2.
delete it_record index lv_tabix.
else.
lv_c = lv_j.
concatenate 'A' lv_c into it_record-order_t.
condense it_record-order_t no-gaps.
if lv_i = 20.
lv_i = 0.
lv_j = lv_j + 1.
endif.
lv_i = lv_i + 1.
it_err4-werks = it_record-werks.
it_err4-lgort = it_record-lgort.
it_err4-sobkz = it_record-sobkz.
it_err4-order_t = it_record-order_t.
collect it_err4.
clear it_err4.
modify it_record index lv_tabix.
endif.
endloop.
sort it_err4 by werks lgort sobkz order_t .
loop at it_err4 .
lv_tabix = sy-tabix.
it_gdmvt_head-pstng_date = sy-datum. " posting date
it_gdmvt_head-doc_date = sy-datum. "docmunt date
"it_gdmvt_head-header_txt = 'Header text'. "Document header text
"it_gdmvt_head-ref_doc_no = goodsmvt_data-ref_doc. "
"it_gdmvt_head-gr_gi_slip_no = goodsmvt_data-user_id . "
loop at it_record where werks = it_err4-werks and lgort = it_err4-lgort and sobkz = it_err4-sobkz and order_t = it_err4-order_t.
clear: lv_sernr,lv_j.
select single lsernr into lv_sernr
from mase
where matnr = it_record-matnr.
if sy-subrc ne 0.
lv_sernr = 0.
endif.
if it_record-sobkz ne space.
it_afs_item-spec_stock = it_record-sobkz. "Special Stock
if it_record-sobkz = 'Q'.
it_afs_item-val_wbs_elem = it_record-posid."Production Lot
it_afs_item-wbs_elem = it_record-posid."Production Lot
elseif it_record-sobkz = 'K' or it_record-sobkz = 'M' or it_record-sobkz = 'O'.
it_afs_item-vendor = it_record-lifnr."Vendor
elseif it_record-sobkz = 'W' or it_record-sobkz = 'V' .
it_afs_item-customer = it_record-kunnr."customer
endif.
endif.
it_afs_item-material = it_record-matnr. " Material Code
it_afs_item-plant = it_record-werks . "Plant
it_afs_item-stge_loc = it_record-lgort. "Storage Location
it_afs_item-batch = it_record-charg. "goodsmvt_data-rfid_batch' .
it_afs_item-move_type = '561'. "Move type"
it_afs_item-entry_qnt = it_record-menge . " Quantity
* IT_AFS_ITEM-ENTRY_UOM = IT_RECORD-MEINS. " Unit Mark by Lisa wong 11/07/2012
append it_afs_item.
"80* 85* material
lv_j = it_record-menge.
do lv_j times .
lv_sernr = lv_sernr + 1 .
it_serial-matdoc_itm = 1.
it_serial-serialno = lv_sernr.
perform add_pre_zero using it_serial-serialno
changing it_serial-serialno.
append it_serial.
clear:it_serial.
enddo.
endloop.
call function 'BAPI_GOODSMVT_CREATE'
exporting
goodsmvt_header = it_gdmvt_head
goodsmvt_code = it_gdmvt_code
importing
materialdocument = lv_mdoc
matdocumentyear = lv_year
tables
goodsmvt_item = it_afs_item
goodsmvt_serialnumber = it_serial
return = it_return.
read table it_return with key type = 'E'.
if sy-subrc eq 0.
loop at it_return where type = 'E'.
concatenate it_err4-message ' E: ' it_return-message into it_err4-message.
endloop.
else.
call function 'BAPI_TRANSACTION_COMMIT'
exporting
wait = 'X'.
it_err4-matdoc = lv_mdoc.
concatenate lv_mdoc ' is posted.' into it_err4-message..
endif.
wait up to 1 seconds.
clear:it_return ,it_return[].
call function 'BAPI_GOODSMVT_CANCEL'
exporting
materialdocument = lv_mdoc
matdocumentyear = lv_year
goodsmvt_pstng_date = sy-datum
importing
goodsmvt_headret = it_ret
tables
return = it_return.
read table it_return with key type = 'E'.
if sy-subrc eq 0.
loop at it_return where type = 'E'.
concatenate it_err4-message ' E: ' it_return-message into it_err4-message.
endloop.
else.
call function 'BAPI_TRANSACTION_COMMIT'
exporting
wait = 'X'.
it_err4-matdoc_c = it_ret-mat_doc.
concatenate it_err4-message it_ret-mat_doc ' is posted.' into it_err4-message separated by space.
endif.
modify it_err4 index lv_tabix.
clear: it_afs_item, it_return, it_gdmvt_head,lv_mdoc,lv_year,it_serial,it_err4-message,it_ret.
refresh: it_afs_item, it_return,it_serial.
endloop.
clear it_err3.
refresh it_err3.
loop at it_record.
move-corresponding it_record to it_err3.
read table it_err4 with key werks = it_err3-werks lgort = it_err3-lgort sobkz = it_err3-sobkz order_t = it_err3-order_t binary search.
if sy-subrc eq 0.
it_err3-matdoc = it_err4-matdoc.
it_err3-matdoc_c = it_err4-matdoc_c.
it_err3-message = it_err4-message.
endif.
append it_err3.
clear:it_err3.
endloop.
append lines of it_err2 to it_err3.
sort it_err3 by order ascending.
endform. " UP_SELECT " GET_IT_RECORD
*&---------------------------------------------------------------------*
*& Form GET_IT_RECORD2
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
form get_it_record2 .
data:lv_order type i.
check not itab[] is initial.
lv_order = 1.
loop at itab.
move-corresponding itab to it_record.
perform tranfer_material using it_record-matnr_c '' changing it_record-matnr.
perform add_pre_zero using it_record-charg
changing it_record-charg.
perform add_pre_zero using itab-lifnr
changing it_record-lifnr.
perform add_pre_zero using itab-kunnr
changing it_record-kunnr.
if it_record-menge_c cn c_chk_num.
it_record-menge = -1.
else.
it_record-menge = it_record-menge_c .
endif.
translate it_record-posid to upper case.
it_record-order = lv_order.
append it_record.
lv_order = lv_order + 1.
clear : it_record.
endloop.
endform. " GET_IT_RECORD2
*&---------------------------------------------------------------------*
*& Form ADD_PRE_ZERO
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* -->P_IT_RECORD_CHARG text
* <--P_IT_RECORD_CHARG text
*----------------------------------------------------------------------*
form add_pre_zero using p_old
changing p_new .
call function 'CONVERSION_EXIT_ALPHA_INPUT'
exporting
input = p_old
importing
output = p_new.
endform. " ADD_PRE_ZERO
*&---------------------------------------------------------------------*
*& Form CHECK_IT_RECORD
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
form check_it_record .
data: lv_flag2 type c,
lv_werks like t001w-werks,
lv_lgort like t001l-lgort,
lv_mtart like mara-mtart.
clear:lv_flag2.
loop at it_record.
lv_tabix = sy-tabix.
clear:lv_flag2,it_record-message.
" Material code
clear:lv_mtart.
select single mtart into lv_mtart
from mara
where matnr = it_record-matnr.
if sy-subrc <> 0.
lv_flag2 = 'X'.
concatenate it_record-message ' Material Not Exists.'
into it_record-message.
modify it_record index lv_tabix.
endif.
"plant
select single werks into lv_werks
from t001w
where werks = it_record-werks.
if sy-subrc <> 0.
lv_flag2 = 'X'.
concatenate it_record-message ' Plant Not Exists.'
into it_record-message.
modify it_record index lv_tabix.
endif.
"Stock Location
select single lgort into lv_lgort
from t001l
where lgort = it_record-lgort.
if sy-subrc <> 0.
lv_flag2 = 'X'.
concatenate it_record-message
' Storage Location error.' into it_record-message.
modify it_record index lv_tabix.
endif.
select single werks into lv_werks
from mcha
where matnr = it_record-matnr
and werks = it_record-werks
and charg = it_record-charg.
if sy-subrc <> 0.
lv_flag2 = 'X'.
concatenate it_record-message
' Batch Not Exists.' into it_record-message.
modify it_record index lv_tabix.
endif.
if lv_mtart ne 'ZVFG'.
it_record-menge = 1.
endif.
if lv_flag2 <> 'X'.
it_record-message = ' OK.'.
modify it_record index lv_tabix.
endif.
endloop.
endform. " CHECK_IT_RECORD
*&---------------------------------------------------------------------*
*& Form WRITE_DATA
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
form write_data .
refresh fc_hier.
define alv_field.
clear:wa_hier.
wa_hier-col_pos = &1.
wa_hier-fieldname = &2.
wa_hier-seltext_m = &3.
wa_hier-seltext_l = &4.
wa_hier-outputlen = &5.
append wa_hier to fc_hier.
end-of-definition.
define alv_field.
clear:wa_hier.
wa_hier-fieldname = &1.
wa_hier-seltext_m = &2.
wa_hier-seltext_l = &3.
wa_hier-outputlen = &4.
wa_hier-just = &5.
wa_hier-do_sum = &6.
wa_hier-ref_tabname = &7 .
wa_hier-ref_fieldname = &8 .
append wa_hier to fc_hier.
end-of-definition.
alv_field 'WERKS' 'Plant' '' '6' 'L' '' '' ''.
alv_field 'LGORT' '' 'Stock Location' '6' 'L' '' '' ''.
alv_field 'MATNR' 'Material' '' '20' 'L' '' 'MARA' 'MATNR'.
alv_field 'CHARG' 'Batch' '' '10' 'L' '' '' ''.
alv_field 'MENGE' 'Qty' '' '10' 'R' '' '' ''.
alv_field 'BSTAR' 'Stock type' '' '6' 'L' '' '' ''.
alv_field 'SOBKZ' 'Special Stock' '' '6' 'L' '' '' ''.
alv_field 'LIFNR' '' '' '10' 'L' '' 'LFA1' 'LIFNR'.
alv_field 'KUNNR' '' '' '10' 'L' '' 'KNA1' 'KUNNR'.
alv_field 'POSID' '' 'WBS Element' '10' 'L' '' '' ''.
alv_field 'MATDOC' '' 'Material Document No.' '10' 'L' '' '' ''.
alv_field 'MATDOC_C' 'Cancel Material Document No.' '' '10' 'L' '' '' ''.
alv_field 'MESSAGE' 'Message' '' '50' 'L' '' '' ''.
call function 'REUSE_ALV_GRID_DISPLAY'
exporting
it_fieldcat = fc_hier[]
tables
t_outtab = it_err3[]
exceptions
program_error = 1
others = 2.
if sy-subrc ne 0.
" message i004(zmm)
endif.
endform. " WRITE_DATA
*&---------------------------------------------------------------------*
*& Form F4_FILENAME
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
form f4_filename using p_filename .
call function 'F4_FILENAME'
* EXPORTING
* PROGRAM_NAME = SYST-CPROG
* DYNPRO_NUMBER = SYST-DYNNR
* FIELD_NAME =
importing
file_name = p_filename.
endform. " F4_FILENAME
*&---------------------------------------------------------------------*
*& Form down_file
*&---------------------------------------------------------------------*
form down_file .
clear:it_down,it_down[].
concatenate
'Plant'
'Stock Location'
'Material'
'Batch'
'Qty'
'Stock type'
'Special Stock'
'Vendor'
'Cusrtomer'
'WBS Element'
'Material Document No.'
'Cancel Material Document No.'
'Message'
into it_down separated by cl_abap_char_utilities=>horizontal_tab.
append it_down.
loop at it_err3.
move-corresponding it_err3 to it_tab.
it_tab-menge_c = it_err3-menge.
condense it_tab-menge_c.
concatenate it_tab-werks
it_tab-lgort
it_tab-matnr_c
it_tab-charg
it_tab-menge_c
it_tab-bstar
it_tab-sobkz
it_tab-lifnr
it_tab-kunnr
it_tab-posid
it_err3-matdoc
it_err3-matdoc_c
it_tab-message
into it_down separated by cl_abap_char_utilities=>horizontal_tab.
condense it_down .
append it_down .
endloop.
endform. " down_file
*&---------------------------------------------------------------------*
*& Form DOWNLOAD_DATA
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
*&---------------------------------------------------------------------*
*& Form DOWNLOAD_SERVER
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
*&---------------------------------------------------------------------*
*& Form get_file
*&---------------------------------------------------------------------*
form get_file.
data: wa_key(30) type c.
data: lv_i type i.
data: lv_c(1) type c.
data: wa_date(8) type c.
data: wa_time like sy-uzeit.
data: lv_char type c.
wa_date = sy-datum.
wa_time = sy-uzeit.
concatenate 'ZMM002A' wa_date wa_time into wa_key.
concatenate wa_key '.txt' into wa_key.
lv_i = strlen( pr_error ).
lv_i = lv_i - 1 .
if lv_i > 1.
lv_c = pr_error+lv_i(1).
endif.
if p_test = 'X'.
lv_char = '\' .
else.
lv_char = '/' .
endif.
if lv_c eq lv_char.
concatenate pr_error wa_key into gv_file.
else.
concatenate pr_error lv_char wa_key into gv_file.
endif.
endform. " get_file
*&---------------------------------------------------------------------*
*& Form DOWNLOAD_SERVER
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
form download_server .
perform get_file.
open dataset gv_file for output in text mode encoding default.
loop at it_down.
transfer it_down-line to gv_file.
endloop.
close dataset gv_file.
endform. "DOWNLOAD_SERVER
*&---------------------------------------------------------------------*
*& Form DOWNLOAD_Local
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
form download_local .
data: lv_filename type string.
perform get_file.
lv_filename = gv_file.
call function 'GUI_DOWNLOAD'
exporting
filename = lv_filename
filetype = 'ASC'
write_field_separator = 'X'
tables
data_tab = it_down
exceptions
file_write_error = 1
no_batch = 2
gui_refuse_filetransfer = 3
invalid_type = 4
no_authority = 5
unknown_error = 6
header_not_allowed = 7
separator_not_allowed = 8
filesize_not_allowed = 9
header_too_long = 10
dp_error_create = 11
dp_error_send = 12
dp_error_write = 13
unknown_dp_error = 14
access_denied = 15
dp_out_of_memory = 16
disk_full = 17
dp_timeout = 18
file_not_found = 19
dataprovider_exception = 20
control_flush_error = 21
others = 22.
if sy-subrc <> 0.
message id sy-msgid type sy-msgty number sy-msgno
with sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
endif.
endform. " DOWNLOAD_DATA
" UPLOAD_SERVER
*&---------------------------------------------------------------------*
*& Form GET_DATA
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
form get_data .
if p_test = 'X'.
clear:itab,itab[] .
data: lv_file type string.
lv_file = filename.
call function 'GUI_UPLOAD'
exporting
filename = lv_file
filetype = 'ASC'
has_field_separator = 'X'
tables
data_tab = itab
exceptions
file_open_error = 1
file_read_error = 2
no_batch = 3
gui_refuse_filetransfer = 4
invalid_type = 5
no_authority = 6
unknown_error = 7
bad_data_format = 8
header_not_allowed = 9
separator_not_allowed = 10
header_too_long = 11
unknown_dp_error = 12
access_denied = 13
dp_out_of_memory = 14
disk_full = 15
dp_timeout = 16
others = 17.
if sy-subrc ne 0.
clear : msg.
concatenate 'Upload Filename ' filename ' Is Error!' into msg.
message msg type 'I'.
exit.
endif.
else.
clear:i_file,i_file[].
open dataset filename for input in text mode encoding default.
if sy-subrc = 0.
do.
read dataset filename into i_file.
if sy-subrc ne 0.
exit.
endif.
append i_file.
enddo.
close dataset filename.
else.
clear : msg.
concatenate 'Upload Filename ' filename ' Is Error!' into msg.
message msg type 'I'.
exit.
endif.
if not i_file[] is initial.
loop at i_file.
split i_file-line at cl_abap_char_utilities=>horizontal_tab
into itab-werks itab-lgort
itab-matnr_c itab-charg
itab-menge_c itab-bstar
itab-sobkz itab-lifnr
itab-kunnr itab-posid.
append itab.
clear:itab.
endloop.
endif.
endif.
endform. " GET_DATA
*&---------------------------------------------------------------------*
*& Form tranfer_material
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* -->P_OLD text
* -->P_OUTPUT text
* -->P_NEW text
*----------------------------------------------------------------------*
form tranfer_material using p_old
p_output
changing p_new .
if p_output = 'X'.
call function 'CONVERSION_EXIT_MATN1_OUTPUT'
exporting
input = p_old
importing
output = p_new.
else.
data:lv_input type c length 50.
lv_input = p_old.
translate lv_input to upper case.
call function 'CONVERSION_EXIT_MATN1_INPUT'
exporting
input = lv_input
importing
output = p_new
exceptions
length_error = 1
others = 2.
if sy-subrc <> 0.
endif.
endif.
endform. "tranfer_material