You are on page 1of 6

*&---------------------------------------------------------------------*

*& Report ZMM_AUTO_MIGO


*&
*&---------------------------------------------------------------------*
*&
*&
*&---------------------------------------------------------------------*

REPORT zmm_auto_migo.

TYPES: BEGIN OF pohead.


INCLUDE STRUCTURE bapiekkol.
TYPES: END OF pohead.

TYPES: BEGIN OF gmhead.


INCLUDE STRUCTURE bapi2017_gm_head_01.
TYPES: END OF gmhead.

TYPES: BEGIN OF gmcode.


INCLUDE STRUCTURE bapi2017_gm_code.
TYPES: END OF gmcode.

TYPES: BEGIN OF mthead.


INCLUDE STRUCTURE bapi2017_gm_head_ret.
TYPES: END OF mthead.

TYPES: BEGIN OF ty_bapiekbe.


INCLUDE STRUCTURE bapiekbe.
TYPES: END OF ty_bapiekbe.

TYPES: BEGIN OF ty_item_tab.


INCLUDE STRUCTURE bapi2017_gm_item_create.
TYPES: END OF ty_item_tab.

TYPES: BEGIN OF ty_errmsg.


INCLUDE STRUCTURE bapiret2.
TYPES: END OF ty_errmsg.

TYPES: BEGIN OF ty_bapireturn.


INCLUDE STRUCTURE bapireturn.
TYPES: END OF ty_bapireturn.

TYPES: BEGIN OF ty_header,


po_number TYPE ebeln,
doc_date TYPE budat,
pstng_date TYPE bldat,
delv_note TYPE xblnr,
header_txt TYPE bktxt,
bill_of_lading TYPE frbnr,
END OF ty_header.

TYPES: BEGIN OF ty_item,


po_number TYPE ebeln,
po_item TYPE ebelp,
entry_qnt TYPE erfmg,
entry_uom TYPE erfme,
quantity TYPE menge_d,
base_uom TYPE meins,
stge_loc TYPE lgort_d,
END OF ty_item.

DATA: lt_po_items TYPE STANDARD TABLE OF bapiekpo,


lt_exceldata TYPE STANDARD TABLE OF alsmex_tabline,
lwa_exceldata TYPE alsmex_tabline,
wa_po_items TYPE bapiekpo,
it_header TYPE STANDARD TABLE OF ty_header,
it_item TYPE STANDARD TABLE OF ty_item,
lt_item_temp TYPE STANDARD TABLE OF ty_item,
wa_item_temp TYPE ty_item,
wa_header TYPE ty_header,
wa_item TYPE ty_item,
lt_po_history TYPE STANDARD TABLE OF ty_bapiekbe,
wa_po_history TYPE ty_bapiekbe,
it_final TYPE STANDARD TABLE OF ty_item_tab,
wa_final TYPE ty_item_tab,
ls_pohead TYPE pohead,
ls_gmhead TYPE gmhead,
ls_mthead TYPE mthead,
ls_gmcode TYPE gmcode,
it_error TYPE STANDARD TABLE OF ty_errmsg,
wa_error TYPE ty_errmsg,
it_bapireturn TYPE STANDARD TABLE OF ty_bapireturn,
lwa_return TYPE bapiret2,
lv_begin TYPE i VALUE 2,
lv_end_col TYPE i VALUE 12,
lv_end_row TYPE i VALUE 256,
lv_count TYPE i,
lv_po_num TYPE ebeln.

TYPES: BEGIN OF ty_excel,


row TYPE string,
col TYPE string,
value TYPE string,
END OF ty_excel.

DATA: it_excel TYPE STANDARD TABLE OF ty_excel.


DATA: wa_excel TYPE ty_excel.
SELECTION-SCREEN : BEGIN OF BLOCK b1 WITH FRAME TITLE text-001 .
PARAMETERS : p_file TYPE rlgrap-filename OBLIGATORY.
SELECTION-SCREEN : END OF BLOCK b1.

AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_file.

* FM to obtain the popup screen when F4 is clicked for uploading file


CALL FUNCTION 'F4_FILENAME'
EXPORTING
program_name = syst-cprog
dynpro_number = syst-dynnr
IMPORTING
file_name = p_file.

START-OF-SELECTION.

CALL FUNCTION 'ALSM_EXCEL_TO_INTERNAL_TABLE'


EXPORTING
filename = p_file
i_begin_col = 1
i_begin_row = lv_begin
i_end_col = lv_end_col
i_end_row = lv_end_row
TABLES
intern = lt_exceldata
EXCEPTIONS
inconsistent_parameters = 1
upload_ole = 2
OTHERS = 3.

* Move from Row, Col to Flat Structure


LOOP AT lt_exceldata INTO lwa_exceldata.

* Header data
CASE lwa_exceldata-col.
WHEN '0001'.
wa_header-po_number = lwa_exceldata-value.
lv_po_num = wa_header-po_number.
WHEN '0002'.
CALL FUNCTION 'CONVERT_DATE_TO_INTERNAL'
EXPORTING
date_external = lwa_exceldata-value
* ACCEPT_INITIAL_DATE =
IMPORTING
date_internal = wa_header-pstng_date
EXCEPTIONS
date_external_is_invalid = 1
OTHERS = 2.
IF sy-subrc <> 0.
* Implement suitable error handling here
ENDIF.

WHEN '0003'.
wa_header-doc_date = lwa_exceldata-value.
CALL FUNCTION 'CONVERT_DATE_TO_INTERNAL'
EXPORTING
date_external = lwa_exceldata-value
* ACCEPT_INITIAL_DATE =
IMPORTING
date_internal = wa_header-doc_date
EXCEPTIONS
date_external_is_invalid = 1
OTHERS = 2.
IF sy-subrc <> 0.
* Implement suitable error handling here
ENDIF.

WHEN '0004'.
wa_header-delv_note = lwa_exceldata-value.
WHEN '0005'.
wa_header-header_txt = lwa_exceldata-value.
WHEN '0006'.
wa_header-bill_of_lading = lwa_exceldata-value.
WHEN '0007'.
wa_item-po_item = lwa_exceldata-value.
WHEN '0008'.
wa_item-entry_qnt = lwa_exceldata-value.
WHEN '0009'.
wa_item-entry_uom = lwa_exceldata-value.
WHEN '0010'.
wa_item-quantity = lwa_exceldata-value.
WHEN '0011'.
wa_item-base_uom = lwa_exceldata-value.
WHEN '0012'.
wa_item-stge_loc = lwa_exceldata-value.
WHEN OTHERS.
ENDCASE.
wa_item-po_number = lv_po_num.
AT END OF row.
APPEND wa_header TO it_header.
APPEND wa_item TO it_item.
CLEAR: wa_item.
ENDAT.
ENDLOOP.
IF it_header IS NOT INITIAL.
SORT it_header BY po_number.
DELETE ADJACENT DUPLICATES FROM it_header COMPARING po_number.
ENDIF.

LOOP AT it_header INTO wa_header.


CALL FUNCTION 'BAPI_PO_GETDETAIL'
EXPORTING
purchaseorder = wa_header-po_number
items = 'X'
history = 'X'
IMPORTING
po_header = ls_pohead
TABLES
po_items = lt_po_items
po_item_history = lt_po_history
return = it_bapireturn.

IF sy-subrc IS INITIAL.

* Populate header data


ls_gmhead-pstng_date = wa_header-pstng_date.
ls_gmhead-doc_date = wa_header-pstng_date.
ls_gmhead-ref_doc_no = wa_header-delv_note.
ls_gmhead-bill_of_lading = wa_header-bill_of_lading.
ls_gmhead-pr_uname = sy-uname.
ls_gmhead-header_txt = wa_header-header_txt.

ls_gmcode-gm_code = '01'. "01 - MB01 - Goods Receipts for Purchase


Order

* Populate item data


lt_item_temp[] = it_item[].
SORT lt_item_temp BY po_number.
DELETE lt_item_temp WHERE po_number NE wa_header-po_number.

SORT lt_item_temp BY po_number po_item.


LOOP AT lt_po_items INTO wa_po_items.
READ TABLE lt_item_temp[] INTO wa_item_temp WITH KEY po_number = wa_po
_items-po_number
po_item = wa_po
_items-po_item
BINARY SEARCH.
wa_final-move_type = '101'.
wa_final-mvt_ind = 'B'.
wa_final-plant = wa_po_items-plant.
wa_final-material = wa_po_items-material.
wa_final-entry_qnt = wa_item_temp-entry_qnt.
wa_final-quantity = wa_item_temp-quantity.
wa_final-entry_uom = wa_item_temp-entry_uom.
wa_final-base_uom = wa_item_temp-base_uom.
wa_final-stge_loc = wa_item_temp-stge_loc.
wa_final-po_number = wa_item_temp-po_number.
wa_final-po_item = wa_item_temp-po_item.
wa_final-profit_ctr = wa_po_items-profit_ctr.
wa_final-vendor = wa_po_items-vend_mat.
wa_final-no_more_gr = '1'.
APPEND wa_final TO it_final.
ENDLOOP.

IF lt_po_history IS NOT INITIAL.


SORT lt_po_history BY po_item.
READ TABLE lt_po_history INTO wa_po_history WITH KEY po_item = wa_po_i
tems-po_item BINARY SEARCH.
IF sy-subrc IS NOT INITIAL.
ls_mthead-mat_doc = wa_po_history-mat_doc.
ls_mthead-doc_year = wa_po_history-doc_year.
ENDIF.
ENDIF.

IF it_final IS NOT INITIAL.


* BAPI for goods movement create
CALL FUNCTION 'BAPI_GOODSMVT_CREATE'
EXPORTING
goodsmvt_header = ls_gmhead
goodsmvt_code = ls_gmcode
IMPORTING
goodsmvt_headret = ls_mthead
TABLES
goodsmvt_item = it_final
return = it_error.

IF sy-subrc IS INITIAL.
CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'.
ENDIF.
ENDIF.
ENDIF.
ENDLOOP.

IF it_error IS NOT INITIAL.

LOOP AT it_error INTO wa_error.


WRITE:/ wa_error-message.
ENDLOOP.

ENDIF.