Friday, June 4, 2010

include program : ZGEN_EXCEL

Include ZGEN_EXCEL - for transfer itab to Excel
use tcode OAOR to place the excel template.


***INCLUDE ZGEN_EXCEL .

TYPE-POOLS: soi.
TYPES : type_doc(64),format_amount(18).
DATA: tranges TYPE soi_range_list,
tcontents TYPE soi_generic_table,
setranges TYPE soi_full_range_table,
rangesdef TYPE soi_dimension_table,
tformat TYPE soi_format_table,
tcell TYPE soi_cell_table.

DATA: struc_ranges TYPE soi_range_item,
struc_generic TYPE soi_generic_item,
struc_setrange TYPE soi_full_range_item,
struc_rangesdef TYPE soi_dimension_item,
struc_cell TYPE soi_cell_item.
*--- Object Instance Declaration ---
DATA: container TYPE REF TO cl_gui_custom_container.
DATA: control TYPE REF TO i_oi_container_control.
DATA: document TYPE REF TO i_oi_document_proxy.
DATA: excel TYPE REF TO i_oi_spreadsheet.

DATA: bds_doc TYPE REF TO cl_bds_document_set.
DATA: link_server TYPE REF TO i_oi_link_server.
*--- Other Objects/Variable Declaration ---
DATA: no_flush,
error TYPE REF TO i_oi_error,
retcode(256) TYPE c,
version(64) TYPE c.
DATA: document_type(80) TYPE c VALUE soi_doctype_word_document,
document_format(6) TYPE c,
doc_url TYPE bapiuri-uri, "MSDocument URL Address
pic_url TYPE bapiuri-uri. "Logo URL Address
* Error Handling after call a method
CLASS c_oi_errors DEFINITION LOAD.

*---------------------------------------------------------------------*
* FORM select_new_sheet *
*---------------------------------------------------------------------*
FORM select_new_sheet USING p_sheet_name.
CALL METHOD excel->select_sheet
EXPORTING name = p_sheet_name
no_flush = no_flush
IMPORTING error = error
retcode = retcode.
CALL METHOD c_oi_errors=>show_message EXPORTING type = 'E'.
CALL METHOD excel->set_selection
EXPORTING
left = 2
top = 2
rows = 4
columns = 1
no_flush = no_flush
updating = -1
IMPORTING error = error
retcode = retcode.
CALL METHOD c_oi_errors=>show_message EXPORTING type = 'E'.
ENDFORM. " select_new_sheet

*---------------------------------------------------------------------*
* FORM select_document *
*---------------------------------------------------------------------*
FORM select_document USING nclass TYPE type_doc
objkey TYPE type_doc
doc_id TYPE type_doc
CHANGING adr_url TYPE bapiuri-uri.

* Tables and WAs:
DATA: doc_signature TYPE sbdst_signature,
wa_doc_signature LIKE LINE OF doc_signature,
doc_components TYPE sbdst_components,
wa_doc_components LIKE LINE OF doc_components,
doc_uris TYPE sbdst_uri,
wa_doc_uris LIKE LINE OF doc_uris.
* IDs:
DATA: doc_classname TYPE sbdst_classname," VALUE 'ZCO_IMR',
doc_classtype TYPE sbdst_classtype VALUE 'OT',
doc_object_key TYPE sbdst_object_key," VALUE 'WPL',
doc_mimetype LIKE bapicompon-mimetype.
*----------------------------------------------------------------------
CLEAR: wa_doc_signature, wa_doc_components, wa_doc_uris.
REFRESH: doc_signature, doc_components, doc_uris.

doc_classname = nclass.
doc_object_key = objkey.

wa_doc_signature-prop_name = 'DESCRIPTION'.
wa_doc_signature-prop_value = doc_id.
APPEND wa_doc_signature TO doc_signature.

* Availability of document checked
CALL METHOD bds_doc->get_info
EXPORTING classname = doc_classname
classtype = doc_classtype
object_key = doc_object_key
client = sy-mandt
CHANGING components = doc_components
signature = doc_signature
EXCEPTIONS nothing_found = 1
error_kpro = 2
internal_error = 3
parameter_error = 4
not_authorized = 5
not_allowed = 6.
IF sy-subrc NE 0 AND sy-subrc NE 1.
"Error in the Business Document Service (BDS)
MESSAGE e016(demoofficeintegratio).
ENDIF.
IF sy-subrc = 1.
MESSAGE e017(demoofficeintegratio).
"There are no documents that meet the search criteria
ENDIF.

* Get URL address
CALL METHOD bds_doc->get_with_url
EXPORTING classname = doc_classname
classtype = doc_classtype
object_key = doc_object_key
CHANGING uris = doc_uris
signature = doc_signature
EXCEPTIONS nothing_found = 1
error_kpro = 2
internal_error = 3
parameter_error = 4
not_authorized = 5
not_allowed = 6.
IF sy-subrc NE 0 AND sy-subrc NE 1.
MESSAGE e016(demoofficeintegratio).
ENDIF.
IF sy-subrc = 1.
MESSAGE e017(demoofficeintegratio).
ENDIF.

READ TABLE doc_components INTO wa_doc_components INDEX 1.
READ TABLE doc_uris INTO wa_doc_uris INDEX 1.
doc_mimetype = wa_doc_components-mimetype.
adr_url = wa_doc_uris-uri.

CASE doc_mimetype.
WHEN 'application/x-rtf' OR 'text/rtf'.
document_format = soi_docformat_rtf.
WHEN 'application/x-oleobject'.
document_format = soi_docformat_compound.
WHEN 'text/plain'.
document_format = soi_docformat_text.
WHEN OTHERS.
document_format = soi_docformat_native.
ENDCASE.
ENDFORM. " select_document
*---------------------------------------------------------------------*
* FORM formatting_rangesdef_tab *
*---------------------------------------------------------------------*
FORM formatting_rangesdef_tab.
LOOP AT tcontents INTO struc_generic.
struc_rangesdef-rows = 1.
struc_rangesdef-columns = 1.
struc_rangesdef-row = struc_generic-row.
struc_rangesdef-column = struc_generic-column.
struc_generic-row = '1'.
struc_generic-column = '1'.
MODIFY tcontents FROM struc_generic INDEX sy-tabix.
APPEND struc_rangesdef TO rangesdef.
ENDLOOP.
ENDFORM.
*---------------------------------------------------------------------*
* FORM set_data *
*---------------------------------------------------------------------*
* ........ *
*---------------------------------------------------------------------*
FORM set_data.
CALL METHOD excel->set_ranges_data
EXPORTING
ranges = tranges
contents = tcontents
rangesdef = rangesdef
updating = -1
no_flush = no_flush
IMPORTING error = error
retcode = retcode.
CALL METHOD excel->screen_update
EXPORTING updating = 'X'
no_flush = no_flush
IMPORTING error = error
retcode = retcode.
CLEAR : tcontents, rangesdef.
REFRESH : tcontents, rangesdef.
ENDFORM. " set_data
*---------------------------------------------------------------------*
* FORM prog_indicator *
*---------------------------------------------------------------------*
FORM prog_indicator_excel USING t_pers t_text.
CALL FUNCTION 'SAPGUI_PROGRESS_INDICATOR'
EXPORTING
percentage = t_pers
text = t_text.
ENDFORM.
*---------------------------------------------------------------------*
* FORM format_amount *
*---------------------------------------------------------------------*
FORM format_amount USING p_amount
CHANGING axx TYPE format_amount.
DATA : p_val TYPE p DECIMALS 2.
p_val = p_amount.
IF p_val < 0.
p_val = p_val * -1.
WRITE p_val TO axx CURRENCY 'IDR'.
CONCATENATE '-' axx INTO axx.
ELSE.
WRITE p_val TO axx CURRENCY 'IDR'.
ENDIF.
CONDENSE axx NO-GAPS.
DO 5 TIMES.
REPLACE '.' WITH '' INTO axx.
ENDDO.
CONDENSE axx NO-GAPS.
ENDFORM.
*---------------------------------------------------------------------*
* FORM format_amount_coma *
*---------------------------------------------------------------------*
FORM format_amount_coma USING p_amount
CHANGING axx TYPE format_amount.
DATA : p_val TYPE p DECIMALS 2.
p_val = p_amount.
IF p_val < 0.
p_val = p_val * -1.
WRITE p_val TO axx CURRENCY 'USD'.
CONCATENATE '-' axx INTO axx.
ELSE.
WRITE p_val TO axx CURRENCY 'USD'.
ENDIF.
CONDENSE axx NO-GAPS.
DO 5 TIMES.
REPLACE '.' WITH '' INTO axx.
ENDDO.
CONDENSE axx NO-GAPS.
ENDFORM.
*---------------------------------------------------------------------*
* FORM format_origin *
*---------------------------------------------------------------------*
FORM format_origin USING p_amount CHANGING axx TYPE format_amount.
DATA : p_val TYPE p DECIMALS 2.
p_val = p_amount.
IF p_val < 0.
p_val = p_val * -1.
WRITE p_val TO axx.
CONCATENATE '-' axx INTO axx.
ELSE.
WRITE p_val TO axx.
ENDIF.
CONDENSE axx NO-GAPS.
DO 5 TIMES.
REPLACE '.' WITH '' INTO axx.
ENDDO.
CONDENSE axx NO-GAPS.
ENDFORM.
*---------------------------------------------------------------------*
* FORM f_format_amt_excel *
*---------------------------------------------------------------------*
FORM f_format_amt_excel USING pu_dmbt pu_curr
CHANGING pc_dmbt TYPE format_amount.
DATA : lv_dmbt TYPE p DECIMALS 2.
lv_dmbt = pu_dmbt.
IF lv_dmbt < 0.
lv_dmbt = lv_dmbt * -1.
WRITE lv_dmbt TO pc_dmbt CURRENCY pu_curr.
CONCATENATE '-' pc_dmbt INTO pc_dmbt.
ELSE.
WRITE lv_dmbt TO pc_dmbt CURRENCY pu_curr.
ENDIF.
CONDENSE pc_dmbt NO-GAPS.
DO 5 TIMES.
REPLACE '.' WITH '' INTO pc_dmbt.
ENDDO.
CONDENSE pc_dmbt NO-GAPS.
ENDFORM.
*---------------------------------------------------------------------*
* FORM format_quantity *
*---------------------------------------------------------------------*
FORM format_quantity USING pu_meng
CHANGING axx TYPE format_amount.
DATA : p_val TYPE p DECIMALS 3.
p_val = pu_meng.
IF p_val < 0.
p_val = p_val * -1.
WRITE p_val TO axx.
CONCATENATE '-' axx INTO axx.
ELSE.
WRITE p_val TO axx.
ENDIF.
CONDENSE axx NO-GAPS.
DO 5 TIMES.
REPLACE '.' WITH '' INTO axx.
ENDDO.
CONDENSE axx NO-GAPS.
ENDFORM.
*&---------------------------------------------------------------------*
*& Form MODIFY_to_cell
*&---------------------------------------------------------------------*
FORM modify_to_cell USING p_cols p_rows
p_value.
struc_generic-column = p_cols.
struc_generic-row = p_rows.
struc_generic-value = p_value.
APPEND struc_generic TO tcontents.
ENDFORM. " MODIFY_to_cell
*---------------------------------------------------------------------*
* FORM modify_to_cell_format *
*---------------------------------------------------------------------*
FORM modify_to_cell_format USING p_typ p_cols p_rows
p_value.
DATA : lvxdmbt(18).
IF p_typ = 'C'.
PERFORM format_amount USING p_value
CHANGING lvxdmbt.
ELSEIF p_typ = 'D'.
PERFORM format_amount_coma USING p_value
CHANGING lvxdmbt.
ELSEIF p_typ = 'Q'.
PERFORM format_quantity USING p_value
CHANGING lvxdmbt.
ELSE.
PERFORM format_origin USING p_value CHANGING lvxdmbt.
ENDIF.
struc_generic-column = p_cols.
struc_generic-row = p_rows.
struc_generic-value = lvxdmbt.
APPEND struc_generic TO tcontents.
ENDFORM.
*&---------------------------------------------------------------------*
*& Form insert_logo
*&---------------------------------------------------------------------*
FORM insert_logo.
CALL METHOD document->paste_clipboard
EXPORTING
no_flush = no_flush
IMPORTING
error = error
retcode = retcode.
ENDFORM. " insert_logo
*---------------------------------------------------------------------*
* FORM insert_logo_posxy *
*---------------------------------------------------------------------*
FORM insert_logo_posxy USING p_left p_top p_rows p_colm.
CALL METHOD excel->set_selection
EXPORTING
left = p_left
top = p_top
rows = p_rows
columns = p_colm
no_flush = no_flush
updating = -1
IMPORTING error = error
retcode = retcode.
CALL METHOD c_oi_errors=>show_message EXPORTING type = 'E'.

CALL METHOD document->paste_clipboard
EXPORTING
no_flush = no_flush
IMPORTING
error = error
retcode = retcode.
ENDFORM. " insert_logo
*---------------------------------------------------------------------*
* FORM connect_to_excel *
*---------------------------------------------------------------------*
FORM connect_to_excel USING p_place p_class p_obj p_doc
p_logo p_nlogo TYPE type_doc.
PERFORM prog_indicator_excel USING 0 'Link SAP to Excel..'.
IF bds_doc IS INITIAL.
CREATE OBJECT bds_doc.
ENDIF.
IF container IS INITIAL.
CREATE OBJECT container
EXPORTING
container_name = 'CONTAINER'.

CALL METHOD c_oi_container_control_creator=>get_container_control
IMPORTING control = control
retcode = retcode.
CALL METHOD c_oi_errors=>show_message EXPORTING type = 'E'.

CALL METHOD control->init_control
EXPORTING r3_application_name = p_doc
inplace_enabled = 'X'
parent = container
IMPORTING retcode = retcode.
CALL METHOD c_oi_errors=>show_message EXPORTING type = 'E'.

* Excel
PERFORM select_document USING p_class p_obj p_doc
CHANGING doc_url.
* logo
IF p_logo = 'X'.
PERFORM select_document USING p_class p_obj p_nlogo
CHANGING pic_url.
ENDIF.
CALL METHOD control->get_document_proxy
EXPORTING
document_format = document_format
document_type = document_type
no_flush = no_flush
IMPORTING
document_proxy = document
error = error
retcode = retcode.
CALL METHOD c_oi_errors=>show_message EXPORTING type = 'E'.

* Open link server
CALL METHOD control->get_link_server
EXPORTING
no_flush = no_flush
IMPORTING
error = error
link_server = link_server
retcode = retcode.
CALL METHOD c_oi_errors=>show_message EXPORTING type = 'E'.

CALL METHOD link_server->start_link_server
EXPORTING
no_flush = no_flush
server_name_suffix = 'SERVER_LINK'
IMPORTING
error = error
retcode = retcode.
CALL METHOD c_oi_errors=>show_message EXPORTING type = 'E'.
IF p_logo = 'X'.
* Open logo to buffer
CALL METHOD link_server->add_binary_item
EXPORTING
item_name = p_nlogo
no_flush = no_flush
table_url = pic_url
IMPORTING
error = error
retcode = retcode.
CALL METHOD c_oi_errors=>show_message EXPORTING type = 'E'.
* Copy logo to buffer
CALL METHOD link_server->copy_link_item_to_clipboard
EXPORTING
item_name = p_nlogo
no_flush = no_flush
IMPORTING
error = error
retcode = retcode.
CALL METHOD c_oi_errors=>show_message EXPORTING type = 'E'.
ENDIF.
* Open the Office document
* ‘X’ : MS Word will displayed in a custom container area
* Blank : MS Word will displayed in a new window.
CALL METHOD document->open_document
EXPORTING document_url = doc_url
open_inplace = p_place
IMPORTING retcode = retcode.
CALL METHOD c_oi_errors=>show_message EXPORTING type = 'E'.

CALL METHOD document->get_spreadsheet_interface
EXPORTING no_flush = no_flush
IMPORTING sheet_interface = excel
error = error
retcode = retcode.
CALL METHOD c_oi_errors=>show_message EXPORTING type = 'E'.
PERFORM prog_indicator_excel USING 100 'Link Complate!'.
ENDIF.
ENDFORM.
*---------------------------------------------------------------------*
* FORM close_excel_link *
*---------------------------------------------------------------------*
FORM close_excel_link.
* Close/release link server object
CALL METHOD link_server->stop_link_server
EXPORTING
no_flush = no_flush
IMPORTING
error = error
retcode = retcode.
CALL METHOD c_oi_errors=>show_message EXPORTING type = 'E'.
CLEAR bds_doc.
CLEAR container.
MESSAGE i016(zcomsg01).
ENDFORM.
*---------------------------------------------------------------------*
* FORM run_macro *
*---------------------------------------------------------------------*
FORM run_macro USING p_macro_string.
CALL METHOD document->execute_macro
EXPORTING macro_string = p_macro_string
no_flush = no_flush
script_name = ''
IMPORTING error = error
retcode = retcode.
CALL METHOD c_oi_errors=>show_message EXPORTING type = 'E'.
ENDFORM.
*----------------------------------------------------------------------
FORM RUN_MACRO_PARAM USING P_MACRO_STRING
P_PARAM_COUNT
P_PARAM01 P_PARAM02 P_PARAM03 P_PARAM04
P_PARAM05 P_PARAM06 P_PARAM07 P_PARAM08
P_PARAM09 P_PARAM10 P_PARAM11 P_PARAM12.
CALL METHOD DOCUMENT->EXECUTE_MACRO
EXPORTING MACRO_STRING = P_MACRO_STRING
NO_FLUSH = NO_FLUSH
SCRIPT_NAME = ''
PARAM_COUNT = P_PARAM_COUNT
PARAM1 = P_PARAM01
PARAM2 = P_PARAM02
PARAM3 = P_PARAM03
PARAM4 = P_PARAM04
PARAM5 = P_PARAM05
PARAM6 = P_PARAM06
PARAM7 = P_PARAM07
PARAM8 = P_PARAM08
PARAM9 = P_PARAM09
PARAM10 = P_PARAM10
PARAM11 = P_PARAM11
PARAM12 = P_PARAM12
IMPORTING ERROR = ERROR
RETCODE = RETCODE.
CALL METHOD C_OI_ERRORS=>SHOW_MESSAGE EXPORTING TYPE = 'E'.
ENDFORM.

*&---------------------------------------------------------------------*
*& Form f_header_std_excel
*&---------------------------------------------------------------------*
FORM f_header_std_excel USING pu_xpos pu_ypos
pu_bukrs pu_spmon pu_title.
DATA : lvtxt(40),lvxpos(4) TYPE c,lvypos(4) TYPE c.
SELECT SINGLE butxt INTO lvtxt FROM t001
WHERE bukrs = pu_bukrs.
CONCATENATE 'COMPANY : PT.' lvtxt INTO lvtxt SEPARATED BY space.
TRANSLATE lvtxt TO UPPER CASE.
lvxpos = pu_xpos. lvypos = pu_ypos.
PERFORM modify_to_cell USING lvxpos lvypos lvtxt.
lvypos = lvypos + 1.
PERFORM modify_to_cell USING lvxpos lvypos pu_title.
PERFORM f_get_month_excel USING pu_spmon+4(2) CHANGING lvtxt.
CONCATENATE 'FOR THE MONTH :' lvtxt ',' pu_spmon+0(4)
INTO lvtxt SEPARATED BY space.
lvypos = lvypos + 1.
PERFORM modify_to_cell USING lvxpos lvypos lvtxt.
ENDFORM. " f_header_std_excel
*---------------------------------------------------------------------*
* FORM f_get_month_EXCEL *
*---------------------------------------------------------------------*
* ........ *
*---------------------------------------------------------------------*
* --> PMONAT *
* --> PMOTXT *
*---------------------------------------------------------------------*
FORM f_get_month_excel USING pmonat CHANGING pmotxt.
CASE pmonat.
WHEN 1. pmotxt = 'JANUARY'.
WHEN 2. pmotxt = 'FEBRUARY'.
WHEN 3. pmotxt = 'MARCH'.
WHEN 4. pmotxt = 'APRIL'.
WHEN 5. pmotxt = 'MAY'.
WHEN 6. pmotxt = 'JUNE'.
WHEN 7. pmotxt = 'JULY'.
WHEN 8. pmotxt = 'AUGUST'.
WHEN 9. pmotxt = 'SEPTEMBER'.
WHEN 10. pmotxt = 'OCTOBER'.
WHEN 11. pmotxt = 'NOVEMBER'.
WHEN 12. pmotxt = 'DECEMBER'.
ENDCASE.
ENDFORM.

include program : ZAB_ALV

Include ZAB_ALV - for create ALV Layout


TYPE-POOLS: slis .

*---------------------------------------------------------------------*
* Data object declaration
*---------------------------------------------------------------------*
DATA: t_alv_fctlg TYPE slis_t_fieldcat_alv,
t_alv_event TYPE slis_t_event WITH HEADER LINE,
t_alv_isort TYPE slis_t_sortinfo_alv WITH HEADER LINE,
t_alv_filtr TYPE slis_t_filter_alv WITH HEADER LINE,
t_header TYPE slis_t_listheader,
d_alv_isort TYPE slis_sortinfo_alv,
d_alv_varnt TYPE disvariant,
d_alv_lscrl TYPE slis_list_scroll,
d_alv_sort_postn TYPE i,
d_alv_save(1) TYPE c,
d_alv_varnm LIKE disvariant-variant,
d_alv_qinfo TYPE slis_keyinfo_alv,
d_alv_fctlg TYPE slis_fieldcat_alv,
d_alv_stats TYPE slis_formname,
d_alv_ucomm TYPE slis_formname,
d_alv_print TYPE slis_print_alv,
d_alv_repid LIKE sy-repid,
d_alv_tabix LIKE sy-tabix,
d_alv_subrc LIKE sy-subrc,
d_alv_bg_id LIKE bapibds01-objkey VALUE 'ALV_BACKGROUND',
d_alv_screen_start_column TYPE i,
d_alv_screen_start_line TYPE i,
d_alv_screen_end_column TYPE i,
d_alv_screen_end_line TYPE i,
d_alv_layout TYPE slis_layout_alv.

CONSTANTS : c_top_of_page TYPE slis_formname VALUE 'F_TOP_OF_PAGE',
c_top_of_page_alv TYPE slis_formname
VALUE 'F_TOP_OF_PAGE_ALV',
c_end_of_page TYPE slis_formname VALUE 'F_END_OF_PAGE',
c_user_command TYPE slis_formname VALUE 'F_USER_COMMAND',
c_pf_status TYPE slis_formname VALUE 'F_PF_STATUS'.

* Variant
SELECTION-SCREEN BEGIN OF BLOCK 0 WITH FRAME TITLE text-v01.
PARAMETERS: p_vari LIKE disvariant-variant no-display.
SELECTION-SCREEN END OF BLOCK 0.

** Process on value request
*AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_vari.
* PERFORM f_alv_variant_f4 CHANGING d_alv_varnt-variant.

*AT SELECTION-SCREEN.
* PERFORM f_alv_selscr_input.

*&---------------------------------------------------------------------*
*& FORM F_ALV_INIT
*&---------------------------------------------------------------------*
* Initialize ALV variables
*----------------------------------------------------------------------*
FORM f_alv_init.
d_alv_repid = sy-repid.
d_alv_save = 'A'.
d_alv_varnt-report = sy-repid.
ENDFORM. " f_alv_init

*---------------------------------------------------------------------*
* FORM F_ALV_BUILD_COMMENT *
*---------------------------------------------------------------------*
* Build ALV Header *
*---------------------------------------------------------------------*
FORM f_alv_build_comment TABLES ft_top_of_page TYPE slis_t_listheader
USING fu_typ fu_key fu_info.

* fu_typ = H, S, or A.
* H = Header (big), S = Selection (reg) , A = Action (small)
ft_top_of_page-typ = fu_typ.
ft_top_of_page-key = fu_key.
ft_top_of_page-info = fu_info.
APPEND ft_top_of_page.

ENDFORM. "f_alv_build_comment

*---------------------------------------------------------------------*
* FORM F_TOP_OF_PAGE *
*---------------------------------------------------------------------*
* TOP-OF-PAGE *
*---------------------------------------------------------------------*
FORM f_top_of_page_alv.
CALL FUNCTION 'REUSE_ALV_COMMENTARY_WRITE'
EXPORTING
i_logo = 'ENJOYSAP_LOGO'
it_list_commentary = t_header.
ENDFORM. "F_TOP_OF_PAGE_ALV

*---------------------------------------------------------------------*
* FORM f_end_of_page *
*---------------------------------------------------------------------*
* END-OF-PAGE *
*---------------------------------------------------------------------*
FORM f_end_of_page_alv.

ENDFORM. "f_end_of_page_alv

*&---------------------------------------------------------------------*
*& Form F_ALV_BUILD_EVENT
*&---------------------------------------------------------------------*
* Build ALV events (TOP-OF-PAGE, END-OF-PAGE, etc)
*----------------------------------------------------------------------*
FORM f_alv_build_event USING fu_pname fu_pform.

* DATA: lX_event TYPE slis_alv_event.
*
* CALL FUNCTION 'REUSE_ALV_EVENTS_GET'
* EXPORTING
* i_list_type = 0
* IMPORTING
* et_events = t_alv_event.
*
* READ TABLE t_alv_event WITH KEY name = slis_ev_top_of_page.
*
* IF sy-subrc = 0.
* MOVE c_top_of_page TO t_alv_event-form.
* APPEND t_alv_event.
* ENDIF.

CLEAR t_alv_event.
t_alv_event-name = fu_pname.
t_alv_event-form = fu_pform.
APPEND t_alv_event.
ENDFORM. " F_BUILD_EVENT

*---------------------------------------------------------------------*
* FORM F_ALV_SET_UCOMM *
*---------------------------------------------------------------------*
* Set ALV user callback command *
*---------------------------------------------------------------------*
FORM f_alv_set_ucomm USING value(fu_ucomm) TYPE slis_formname.
CLEAR d_alv_ucomm.
d_alv_ucomm = fu_ucomm.
ENDFORM. "f_alv_set_ucomm

*&---------------------------------------------------------------------*
*& Form F_ALV_BUILD_LAYOUT
*&---------------------------------------------------------------------*
* Build ALV layout
*----------------------------------------------------------------------*
FORM f_alv_build_layout USING fu_field fu_value.
* No Headings
IF fu_field = 'NO_COLHEAD'.
d_alv_layout-no_colhead = fu_value.
* Headings not as hotspot
ELSEIF fu_field = 'NO_HOTSPOT'.
d_alv_layout-no_hotspot = fu_value.
* Striped pattern
ELSEIF fu_field = 'ZEBRA'.
d_alv_layout-zebra = fu_value.
* Columns separated by space
ELSEIF fu_field = 'NO_VLINE'.
d_alv_layout-no_vline = fu_value.
* Not suppress field replication
ELSEIF fu_field = 'CELL_MERGE'.
d_alv_layout-cell_merge = fu_value.
* For GRID only
ELSEIF fu_field = 'EDIT'.
d_alv_layout-edit = fu_value.
* For GRID only
ELSEIF fu_field = 'EDIT_MODE'.
d_alv_layout-edit_mode = fu_value.
* Totals for NUMC-Fields possible.
ELSEIF fu_field = 'NUMC_SUM'.
d_alv_layout-numc_sum = fu_value.
* Only display fields
ELSEIF fu_field = 'NO_INPUT'.
d_alv_layout-no_input = fu_value.
ELSEIF fu_field = 'F2CODE'.
d_alv_layout-f2code = fu_value.
* Report interface active
ELSEIF fu_field = 'REPREP'.
d_alv_layout-reprep = fu_value.
* No fix key columns - moving possible
ELSEIF fu_field = 'NO_KEYFIX'.
d_alv_layout-no_keyfix = fu_value.
* Expand all positions
ELSEIF fu_field = 'EXPAND_ALL'.
d_alv_layout-expand_all = fu_value.
* No standard authority check
ELSEIF fu_field = 'NO_AUTHOR'.
d_alv_layout-no_author = fu_value.
* Default status space or 'A'
ELSEIF fu_field = 'DEF_STATUS'.
d_alv_layout-def_status = fu_value.
* Text for item button
ELSEIF fu_field = 'ITEM_TEXT'.
d_alv_layout-item_text = fu_value.
ELSEIF fu_field = 'COLWIDTH_OPTIMIZE'.
d_alv_layout-colwidth_optimize = fu_value.
* line size = width of the list
ELSEIF fu_field = 'NO_MIN_LINESIZE'.
d_alv_layout-no_min_linesize = fu_value.
* if initial min_linesize = 80
ELSEIF fu_field = 'MIN_LINESIZE'.
d_alv_layout-min_linesize = fu_value.
* Default = 250
ELSEIF fu_field = 'MAX_LINESIZE'.
d_alv_layout-max_linesize = fu_value.
ELSEIF fu_field = 'WINDOW_TITLEBAR'.
d_alv_layout-window_titlebar = fu_value.
ELSEIF fu_field = 'NO_ULINE_HS'.
d_alv_layout-no_uline_hs = fu_value.
** Exceptions
ELSEIF fu_field = 'LIGHTS_FIELDNAME'.
d_alv_layout-lights_fieldname = fu_value.
ELSEIF fu_field = 'LIGHTS_TABNAME'.
d_alv_layout-lights_tabname = fu_value.
ELSEIF fu_field = 'LIGHTS_ROLLNAME'.
d_alv_layout-lights_rollname = fu_value.
ELSEIF fu_field = 'LIGHTS_CONDENSE'.
d_alv_layout-lights_condense = fu_value.
** Sums
* No choice for summing up
ELSEIF fu_field = 'NO_SUMCHOICE'.
d_alv_layout-no_sumchoice = fu_value.
* No total line
ELSEIF fu_field = 'NO_TOTALLINE'.
d_alv_layout-no_totalline = fu_value.
* No choice for subtotals
ELSEIF fu_field = 'NO_SUBCHOICE'.
d_alv_layout-no_subchoice = fu_value.
* No subtotals possible
ELSEIF fu_field = 'NO_SUBTOTALS'.
d_alv_layout-no_subtotals = fu_value.
* no sep. tot.lines by inh.units
ELSEIF fu_field = 'NO_UNIT_SPLITTING'.
d_alv_layout-no_unit_splitting = fu_value.
* Display totals before the items
ELSEIF fu_field = 'TOTALS_BEFORE_ITEMS'.
d_alv_layout-totals_before_items = fu_value.
* Show only totals
ELSEIF fu_field = 'TOTALS_ONLY'.
d_alv_layout-totals_only = fu_value.
* Text for 1st col. in total line
ELSEIF fu_field = 'TOTALS_TEXT'.
d_alv_layout-totals_text = fu_value.
* Text for 1st col. in subtotals
ELSEIF fu_field = 'SUBTOTALS_TEXT'.
d_alv_layout-subtotals_text = fu_value.
** Interaction
ELSEIF fu_field = 'BOX_FIELDNAME'.
d_alv_layout-box_fieldname = fu_value.
ELSEIF fu_field = 'BOX_TABNAME'.
d_alv_layout-box_tabname = fu_value.
ELSEIF fu_field = 'BOX_ROLLNAME'.
d_alv_layout-box_rollname = fu_value.
ELSEIF fu_field = 'EXPAND_FIELDNAME'.
d_alv_layout-expand_fieldname = fu_value.
ELSEIF fu_field = 'HOTSPOT_FIELDNAME'.
d_alv_layout-hotspot_fieldname = fu_value.
ELSEIF fu_field = 'CONFIRMATION_PROMPT'.
d_alv_layout-confirmation_prompt = fu_value.
ELSEIF fu_field = 'KEY_HOTSPOT'.
d_alv_layout-key_hotspot = fu_value.
ELSEIF fu_field = 'FLEXIBLE_KEY'.
d_alv_layout-flexible_key = fu_value.
ELSEIF fu_field = 'GROUP_BUTTONS'.
d_alv_layout-group_buttons = fu_value.
ELSEIF fu_field = 'GET_SELINFOS'.
d_alv_layout-get_selinfos = fu_value.
ELSEIF fu_field = 'GROUP_CHANGE_EDIT'.
d_alv_layout-group_change_edit = fu_value.
ELSEIF fu_field = 'NO_SCROLLING'.
d_alv_layout-no_scrolling = fu_value.
** Detailed screen
ELSEIF fu_field = 'DETAIL_POPUP'.
d_alv_layout-detail_popup = fu_value.
ELSEIF fu_field = 'DETAIL_INITIAL_LINES'.
d_alv_layout-detail_initial_lines = fu_value.
ELSEIF fu_field = 'DETAIL_TITLEBAR'.
d_alv_layout-detail_titlebar = fu_value.
ELSEIF fu_field = 'DETAIL_POPUP'.
d_alv_layout-detail_popup = fu_value.
** Variant
ELSEIF fu_field = 'HEADER_TEXT'.
d_alv_layout-header_text = fu_value.
ELSEIF fu_field = 'DEFAULT_ITEM'.
d_alv_layout-default_item = fu_value.
** Color
ELSEIF fu_field = 'INFO_FIELDNAME'.
d_alv_layout-info_fieldname = fu_value.
ELSEIF fu_field = 'COLTAB_FIELDNAME'.
d_alv_layout-coltab_fieldname = fu_value.
** Others
ELSEIF fu_field = 'LIST_APPEND'.
d_alv_layout-coltab_fieldname = fu_value.
ELSEIF fu_field = 'XIFUNCKEY'.
d_alv_layout-xifunckey = fu_value.
ELSEIF fu_field = 'XIDIRECT'.
d_alv_layout-xidirect = fu_value.
ELSEIF fu_field = 'DTC_LAYOUT'.
d_alv_layout-dtc_layout = fu_value.
ENDIF.

ENDFORM. " F_ALV_BUILD_LAYOUT

*&---------------------------------------------------------------------*
*& Form f_check_box
*&---------------------------------------------------------------------*
FORM f_alv_check_box USING fu_tname.
d_alv_layout-box_fieldname = 'CHKBX'.
d_alv_layout-box_tabname = fu_tname.
ENDFORM. " F_CHECK_BOX

*&---------------------------------------------------------------------*
*& Form f_alv_sort
*&---------------------------------------------------------------------*
* Sort ALV catalog
*----------------------------------------------------------------------*
FORM f_alv_sort USING fu_table fu_field fu_ascnd fu_subtot fu_group.
DATA: ld_isort TYPE slis_sortinfo_alv.
d_alv_sort_postn = d_alv_sort_postn + 1.
ld_isort-tabname = fu_table.
ld_isort-fieldname = fu_field.
IF fu_ascnd = 'X'.
ld_isort-up = 'X'.
ELSE.
ld_isort-down = 'X'.
ENDIF.
ld_isort-spos = d_alv_sort_postn.
ld_isort-subtot = fu_subtot.
ld_isort-group = fu_group.
APPEND ld_isort TO t_alv_isort.
ENDFORM. " F_ALV_SORT

*&---------------------------------------------------------------------*
*& Form F_ALV_BUILD_CATALOG
*&---------------------------------------------------------------------*
FORM f_alv_build_catalog USING ft_fctlg TYPE slis_t_fieldcat_alv
fu_tbnam fu_fname fu_dscrp
fu_reftb fu_refld
fu_noout fu_outln fu_hotsp fu_jstfy
fu_dosum fu_rowpos fu_colpos
fu_icon fu_spgroup fu_keysel.
DATA: lv_fctlg TYPE slis_fieldcat_alv.

lv_fctlg-seltext_s = fu_dscrp.
lv_fctlg-seltext_m = fu_dscrp.
lv_fctlg-seltext_l = fu_dscrp.
lv_fctlg-reptext_ddic = fu_dscrp.
lv_fctlg-tabname = fu_tbnam.
lv_fctlg-fieldname = fu_fname.
lv_fctlg-ref_tabname = fu_reftb.
lv_fctlg-ref_fieldname = fu_refld.
lv_fctlg-no_out = fu_noout.
lv_fctlg-outputlen = fu_outln.
lv_fctlg-hotspot = fu_hotsp.
lv_fctlg-just = fu_jstfy.
lv_fctlg-do_sum = fu_dosum.
lv_fctlg-row_pos = fu_rowpos.
lv_fctlg-col_pos = fu_colpos.
lv_fctlg-icon = fu_icon.
lv_fctlg-sp_group = fu_spgroup.
lv_fctlg-key_sel = fu_keysel.
APPEND lv_fctlg TO ft_fctlg.
ENDFORM. " F_ALV_BUILD_CATALOG

*&---------------------------------------------------------------------*
*& Form F_ALV_BUILD_CATALOG_CRNCY
*&---------------------------------------------------------------------*
FORM f_alv_build_catalog_crncy USING ft_fctlg TYPE slis_t_fieldcat_alv
fu_tbnam fu_fname fu_dscrp
fu_reftb fu_refld fu_outln fu_dosum
fu_curky fu_cfild fu_ctbnm fu_jstfy
fu_rowpos fu_colpos fu_noout.
DATA: lv_fctlg TYPE slis_fieldcat_alv,
ld_lngth TYPE i.
ld_lngth = STRLEN( fu_dscrp ).
IF ld_lngth > 25.
lv_fctlg-seltext_l = fu_dscrp.
ELSEIF ld_lngth > 10.
lv_fctlg-seltext_l = fu_dscrp.
lv_fctlg-seltext_m = fu_dscrp.
ELSE.
lv_fctlg-seltext_s = fu_dscrp.
lv_fctlg-seltext_m = fu_dscrp.
lv_fctlg-seltext_l = fu_dscrp.
ENDIF.
lv_fctlg-tabname = fu_tbnam.
lv_fctlg-fieldname = fu_fname.
lv_fctlg-ctabname = fu_reftb.
lv_fctlg-cfieldname = fu_refld.
lv_fctlg-outputlen = fu_outln.
lv_fctlg-do_sum = fu_dosum.
lv_fctlg-just = fu_jstfy.
lv_fctlg-currency = fu_curky.
lv_fctlg-cfieldname = fu_cfild.
lv_fctlg-ctabname = fu_ctbnm.
lv_fctlg-datatype = 'CURR'.
lv_fctlg-row_pos = fu_rowpos.
lv_fctlg-col_pos = fu_colpos.
lv_fctlg-no_out = fu_noout.
APPEND lv_fctlg TO ft_fctlg.
ENDFORM. " F_ALV_BUILD_CATALOG_CRNCY

*&---------------------------------------------------------------------*
*& Form F_ALV_CATALOG_EXPNT
*&---------------------------------------------------------------------*
FORM f_alv_catalog_expnt USING ft_fctlg TYPE slis_t_fieldcat_alv
fu_tbnam fu_fname fu_dscrp
fu_reftb fu_refld
fu_outln fu_decml fu_expnt fu_jstfy
fu_rowpos fu_colpos fu_noout.
DATA: lv_fctlg TYPE slis_fieldcat_alv,
ld_lngth TYPE i.
ld_lngth = STRLEN( fu_dscrp ).
IF ld_lngth > 25.
lv_fctlg-seltext_l = fu_dscrp.
ELSEIF ld_lngth > 10.
lv_fctlg-seltext_l = fu_dscrp.
lv_fctlg-seltext_m = fu_dscrp.
ELSE.
lv_fctlg-seltext_s = fu_dscrp.
lv_fctlg-seltext_m = fu_dscrp.
lv_fctlg-seltext_l = fu_dscrp.
ENDIF.
lv_fctlg-tabname = fu_tbnam.
lv_fctlg-fieldname = fu_fname.
lv_fctlg-ref_tabname = fu_reftb.
lv_fctlg-ref_fieldname = fu_refld.
lv_fctlg-outputlen = fu_outln.
lv_fctlg-decimals_out = fu_decml.
lv_fctlg-exponent = fu_expnt.
lv_fctlg-datatype = 'FLTP'.
lv_fctlg-just = fu_jstfy.
lv_fctlg-row_pos = fu_rowpos.
lv_fctlg-col_pos = fu_colpos.
lv_fctlg-no_out = fu_noout.
APPEND lv_fctlg TO ft_fctlg.
ENDFORM. " F_ALV_CATALOG_EXPNT


*&---------------------------------------------------------------------*
*& Form F_ALV_BUILD_CATALOG_QTY
*&---------------------------------------------------------------------*
*& field catalog for quantity unit
*&---------------------------------------------------------------------*
FORM f_alv_build_catalog_qty USING ft_fctlg TYPE slis_t_fieldcat_alv
fu_tbnam fu_fname fu_dscrp
fu_reftb fu_refld fu_noout
fu_outln fu_dosum fu_jstfy
fu_meins fu_qfild fu_qtbnm
fu_rowpos fu_colpos.
DATA: lv_fctlg TYPE slis_fieldcat_alv.

lv_fctlg-seltext_s = fu_dscrp.
lv_fctlg-seltext_m = fu_dscrp.
lv_fctlg-seltext_l = fu_dscrp.
lv_fctlg-reptext_ddic = fu_dscrp.
lv_fctlg-tabname = fu_tbnam.
lv_fctlg-fieldname = fu_fname.
lv_fctlg-ref_tabname = fu_reftb.
lv_fctlg-ref_fieldname = fu_refld.
lv_fctlg-no_out = fu_noout.
lv_fctlg-outputlen = fu_outln.
lv_fctlg-do_sum = fu_dosum.
lv_fctlg-just = fu_jstfy.
lv_fctlg-quantity = fu_meins.
lv_fctlg-qfieldname = fu_qfild.
lv_fctlg-qtabname = fu_qtbnm.
lv_fctlg-row_pos = fu_rowpos.
lv_fctlg-col_pos = fu_colpos.
APPEND lv_fctlg TO ft_fctlg.
ENDFORM. " F_ALV_BUILD_CATALOG_QTY

*&---------------------------------------------------------------------*
*& Form F_ALV_BUILD_CATALOG_INPUT
*&---------------------------------------------------------------------*
FORM f_alv_build_catalog_input USING ft_fctlg TYPE slis_t_fieldcat_alv
fu_tbnam fu_fname fu_dscrp
fu_reftb fu_refld
fu_noout fu_outln fu_hotsp fu_jstfy
fu_dosum fu_rowpos fu_colpos
fu_icon fu_spgroup fu_keysel
fu_input fu_edit.
DATA: lv_fctlg TYPE slis_fieldcat_alv.

lv_fctlg-seltext_s = fu_dscrp.
lv_fctlg-seltext_m = fu_dscrp.
lv_fctlg-seltext_l = fu_dscrp.
lv_fctlg-reptext_ddic = fu_dscrp.
lv_fctlg-tabname = fu_tbnam.
lv_fctlg-fieldname = fu_fname.
lv_fctlg-ref_tabname = fu_reftb.
lv_fctlg-ref_fieldname = fu_refld.
lv_fctlg-no_out = fu_noout.
lv_fctlg-outputlen = fu_outln.
lv_fctlg-hotspot = fu_hotsp.
lv_fctlg-just = fu_jstfy.
lv_fctlg-do_sum = fu_dosum.
lv_fctlg-row_pos = fu_rowpos.
lv_fctlg-col_pos = fu_colpos.
lv_fctlg-icon = fu_icon.
lv_fctlg-sp_group = fu_spgroup.
lv_fctlg-key_sel = fu_keysel.
lv_fctlg-input = fu_input.
lv_fctlg-edit = fu_edit.
APPEND lv_fctlg TO ft_fctlg.
ENDFORM. " F_ALV_BUILD_CATALOG_INPUT

*&---------------------------------------------------------------------*
*& Form F_GET_CATALOG
*&---------------------------------------------------------------------*
* @FT_FCTLG = internal table to store field catalog result
* @FU_TABLE = internal table name
* @FU_STRCT = Table/Structure name
* description: Purpose to get field attribute from data dictionary
* (Table/Structure) and save as ALV field catalog
FORM f_alv_get_catalog TABLES ft_fctlg TYPE slis_t_fieldcat_alv
USING fu_table
fu_strct.
DATA: ld_table TYPE slis_tabname,
ld_strct LIKE dd02l-tabname,
ld_fctlg TYPE slis_fieldcat_alv,
lt_fctlg TYPE slis_t_fieldcat_alv.
ld_table = fu_table.
ld_strct = fu_strct.
CALL FUNCTION 'REUSE_ALV_FIELDCATALOG_MERGE'
EXPORTING
i_internal_tabname = ld_table
i_structure_name = ld_strct
i_bypassing_buffer = 'X'
CHANGING
ct_fieldcat = lt_fctlg
EXCEPTIONS
inconsistent_interface = 1
program_error = 2
OTHERS = 3.
IF sy-subrc <> 0.
ENDIF.
* ld_fctlg-currency = 'IDR'.
* MODIFY lt_fctlg FROM ld_fctlg TRANSPORTING currency
* WHERE datatype EQ 'CURR'.
APPEND LINES OF lt_fctlg TO ft_fctlg.
ENDFORM. " F_GET_CATALOG

*&---------------------------------------------------------------------*
*& Form F_ALV_LIST_DISPLAY
*&---------------------------------------------------------------------*
* Generate ALV classic display
*----------------------------------------------------------------------*
FORM f_alv_list_display TABLES ft_table
USING fu_table TYPE slis_tabname
fu_fsave.
d_alv_varnt-report = d_alv_repid.
CALL FUNCTION 'REUSE_ALV_LIST_DISPLAY'
EXPORTING
i_callback_program = d_alv_repid
i_callback_pf_status_set = d_alv_stats
i_callback_user_command = d_alv_ucomm
* I_STRUCTURE_NAME =
is_layout = d_alv_layout
it_fieldcat = t_alv_fctlg
it_sort = t_alv_isort[]
i_default = 'X'
i_save = fu_fsave
is_variant = d_alv_varnt
it_events = t_alv_event[]
is_print = d_alv_print
i_screen_start_column = d_alv_screen_start_column
i_screen_start_line = d_alv_screen_start_line
i_screen_end_column = d_alv_screen_end_column
i_screen_end_line = d_alv_screen_end_line
TABLES
t_outtab = ft_table[]
EXCEPTIONS
program_error = 1
OTHERS = 2.
ENDFORM. " F_ALV_LIST_DISPLAY

*---------------------------------------------------------------------*
* FORM F_ALV_GRID_DISPLAY *
*---------------------------------------------------------------------*
* Generate ALV grid display *
*---------------------------------------------------------------------*
FORM f_alv_grid_display TABLES ft_table.
d_alv_varnt-report = d_alv_repid.
CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
EXPORTING
i_callback_program = d_alv_repid
i_callback_pf_status_set = d_alv_stats
i_callback_user_command = d_alv_ucomm
i_background_id = d_alv_bg_id
is_layout = d_alv_layout
it_fieldcat = t_alv_fctlg
it_sort = t_alv_isort[]
i_default = 'X'
i_save = d_alv_save
is_variant = d_alv_varnt
it_events = t_alv_event[]
is_print = d_alv_print
i_screen_start_column = d_alv_screen_start_column
i_screen_start_line = d_alv_screen_start_line
i_screen_end_column = d_alv_screen_end_column
i_screen_end_line = d_alv_screen_end_line
TABLES
t_outtab = ft_table
EXCEPTIONS
program_error = 1
OTHERS = 2.
ENDFORM. " F_ALV_GRID_DISPLAY

FORM f_alv_grid_display_neu TABLES ft_table
USING fu_scol fu_sline fu_ecol fu_eline fu_default.
d_alv_varnt-report = d_alv_repid.
* d_alv_layout-COLWIDTH_OPTIMIZE = 'X'.
CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
EXPORTING
i_callback_program = d_alv_repid
i_callback_pf_status_set = d_alv_stats
i_callback_user_command = d_alv_ucomm
i_background_id = d_alv_bg_id
is_layout = d_alv_layout
it_fieldcat = t_alv_fctlg
it_sort = t_alv_isort[]
i_default = fu_default
i_save = d_alv_save
is_variant = d_alv_varnt
it_events = t_alv_event[]
is_print = d_alv_print
i_screen_start_column = fu_scol
i_screen_start_line = fu_sline
i_screen_end_column = fu_ecol
i_screen_end_line = fu_eline
TABLES
t_outtab = ft_table
EXCEPTIONS
program_error = 1
OTHERS = 2.
ENDFORM. " F_ALV_GRID_DISPLAY_NEU

*&---------------------------------------------------------------------*
*& Form F_ALV_HIER_DISPLAY
*&---------------------------------------------------------------------*
* Generate ALV hierarchical display (header & item level)
*----------------------------------------------------------------------*
FORM f_alv_hier_display TABLES ft_headr
ft_items
USING fu_headr TYPE slis_tabname
fu_items TYPE slis_tabname
fu_fsave.
d_alv_varnt-report = d_alv_repid.
CALL FUNCTION 'REUSE_ALV_HIERSEQ_LIST_DISPLAY'
EXPORTING
i_callback_program = d_alv_repid
i_callback_pf_status_set = d_alv_stats
i_callback_user_command = d_alv_ucomm
is_layout = d_alv_layout
i_save = fu_fsave
it_fieldcat = t_alv_fctlg
it_sort = t_alv_isort[]
it_events = t_alv_event[]
is_variant = d_alv_varnt
i_tabname_header = fu_headr
i_tabname_item = fu_items
is_keyinfo = d_alv_qinfo
is_print = d_alv_print
i_screen_start_column = d_alv_screen_start_column
i_screen_start_line = d_alv_screen_start_line
i_screen_end_column = d_alv_screen_end_column
i_screen_end_line = d_alv_screen_end_line
TABLES
t_outtab_header = ft_headr
t_outtab_item = ft_items
EXCEPTIONS
program_error = 1
OTHERS = 2.
ENDFORM. " F_ALV_HIER_DISPLAY

*&---------------------------------------------------------------------*
*& Form F_GET_TABLES2
*&---------------------------------------------------------------------*
FORM f_alv_get_tables2 TABLES ft_headr ft_items.
CALL FUNCTION 'REUSE_ALV_HS_TABLES_GET'
TABLES
et_outtab_master = ft_headr
et_outtab_detail = ft_items
EXCEPTIONS
no_infos = 1
OTHERS = 2.
ENDFORM. " F_GET_TABLES

*---------------------------------------------------------------------*
* FORM f_alv_get_tables *
*---------------------------------------------------------------------*
FORM f_alv_get_tables1 TABLES ft_table.
CALL FUNCTION 'REUSE_ALV_TABLES_GET'
TABLES
et_outtab = ft_table
EXCEPTIONS
no_infos = 1
OTHERS = 2.
ENDFORM. " F_GET_TABLES

*&---------------------------------------------------------------------*
*& Form f_alv_variant_f4
*&---------------------------------------------------------------------*
FORM f_alv_variant_f4 CHANGING fc_varnt.
DATA: rs_variant LIKE disvariant.
rs_variant-report = d_alv_repid.
rs_variant-username = sy-uname.
CALL FUNCTION 'REUSE_ALV_VARIANT_F4'
EXPORTING
is_variant = rs_variant
i_save = 'A'
IMPORTING
es_variant = rs_variant
EXCEPTIONS
OTHERS = 1.
IF sy-subrc = 0.
fc_varnt = rs_variant-variant.
p_vari = rs_variant-variant.
ENDIF.
ENDFORM. " ALV_VARIANT_F4

*&---------------------------------------------------------------------*
*& Form f_alv_selscr_input
*&---------------------------------------------------------------------*
* PAI of ALV selection screen
*----------------------------------------------------------------------*
FORM f_alv_selscr_input.
DATA : rs_variant LIKE disvariant.

IF NOT p_vari IS INITIAL.
MOVE d_alv_varnt TO rs_variant.
MOVE p_vari TO rs_variant-variant.
CALL FUNCTION 'REUSE_ALV_VARIANT_EXISTENCE'
EXPORTING
i_save = d_alv_save
CHANGING
cs_variant = rs_variant.

d_alv_varnt = rs_variant.
ELSE.
CLEAR d_alv_varnt.
d_alv_varnt = d_alv_repid.
ENDIF.

ENDFORM. " f_alv_selscr_input
*&---------------------------------------------------------------------*
*& Form F_BUILD_PRINT
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* -->P_D_ALV_PRINT text
*----------------------------------------------------------------------*
FORM f_build_print CHANGING fc_print TYPE slis_print_alv.

fc_print-no_print_listinfos = 'X'.
fc_print-no_print_selinfos = 'X'.
fc_print-no_coverpage = 'X'.

ENDFORM. " F_BUILD_PRINT

*&---------------------------------------------------------------------*
*& Form F_ALV_BUILD_CATALOG_INPUT
*&---------------------------------------------------------------------*
FORM f_alv_build_catalog_checkbox
USING ft_fctlg TYPE slis_t_fieldcat_alv
fu_tbnam fu_fname fu_dscrp
fu_reftb fu_refld
fu_noout fu_outln fu_hotsp fu_jstfy
fu_dosum fu_rowpos fu_colpos
fu_icon fu_spgroup fu_keysel
fu_input fu_edit fu_box.
DATA: lv_fctlg TYPE slis_fieldcat_alv.

lv_fctlg-seltext_s = fu_dscrp.
lv_fctlg-seltext_m = fu_dscrp.
lv_fctlg-seltext_l = fu_dscrp.
lv_fctlg-reptext_ddic = fu_dscrp.
lv_fctlg-tabname = fu_tbnam.
lv_fctlg-fieldname = fu_fname.
lv_fctlg-ref_tabname = fu_reftb.
lv_fctlg-ref_fieldname = fu_refld.
lv_fctlg-no_out = fu_noout.
lv_fctlg-outputlen = fu_outln.
lv_fctlg-hotspot = fu_hotsp.
lv_fctlg-just = fu_jstfy.
lv_fctlg-do_sum = fu_dosum.
lv_fctlg-row_pos = fu_rowpos.
lv_fctlg-col_pos = fu_colpos.
lv_fctlg-icon = fu_icon.
lv_fctlg-sp_group = fu_spgroup.
lv_fctlg-key_sel = fu_keysel.
lv_fctlg-input = fu_input.
lv_fctlg-edit = fu_edit.
lv_fctlg-checkbox = fu_box.
APPEND lv_fctlg TO ft_fctlg.
ENDFORM. " F_ALV_BUILD_CATALOG_INPUT

*---------------------------------------------------------------------*
* FORM F_ALV_GRID_DISPLAY_EXIT *
*---------------------------------------------------------------------*
* Generate ALV grid display with event exit *
*---------------------------------------------------------------------*
FORM f_alv_grid_display_exit TABLES ft_table
ft_evt_exit TYPE slis_t_event_exit.

d_alv_varnt-report = d_alv_repid.
CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
EXPORTING
i_callback_program = d_alv_repid
i_callback_pf_status_set = d_alv_stats
i_callback_user_command = d_alv_ucomm
i_background_id = d_alv_bg_id
is_layout = d_alv_layout
it_fieldcat = t_alv_fctlg
it_sort = t_alv_isort[]
i_default = 'X'
i_save = d_alv_save
is_variant = d_alv_varnt
it_events = t_alv_event[]
is_print = d_alv_print
it_event_exit = ft_evt_exit[]
i_screen_start_column = d_alv_screen_start_column
i_screen_start_line = d_alv_screen_start_line
i_screen_end_column = d_alv_screen_end_column
i_screen_end_line = d_alv_screen_end_line
TABLES
t_outtab = ft_table
EXCEPTIONS
program_error = 1
OTHERS = 2.
ENDFORM. " F_ALV_GRID_DISPLAY

Sample : ALV Report and Transfer to Excel with Macro defined on excel (make column lines)

Here's a report using simple ALV to display and transfer to Excel.
And it run macro also, to create column lines.
Place your excel template using tcode OAOR

for include program, please refer to this link:
ZGEN_EXCEL
ZAB_ALV
File Excel (contain macro)

This program also contains :
- how to get Material initial stock balance (MB5B) (include backposting - stock in transfer)
- how to get Material end stock balance
- how to get Quantity received or quantity required for MRP (MD04)
- outstanding PO Qty
- display the report using ALV and Excel


REPORT zmm_realisasi NO STANDARD PAGE HEADING LINE-SIZE 237.

TABLES : mara, t001w, makt, mbewh, mardh.
DATA : BEGIN OF t_mara OCCURS 0,
matnr LIKE mara-matnr,
meins LIKE mara-meins,
mtart LIKE mara-mtart,
maktx LIKE makt-maktx,
END OF t_mara,
BEGIN OF t_mbewh OCCURS 0,
matnr LIKE mbewh-matnr,
bwkey LIKE mbewh-bwkey,
lbkum LIKE mbewh-lbkum,
END OF t_mbewh,
BEGIN OF t_t_mbewh OCCURS 0,
matnr LIKE ekpo-matnr,
werks LIKE ekpo-werks,
END OF t_t_mbewh,
BEGIN OF t_mbew OCCURS 0,
matnr LIKE mbew-matnr,
bwkey LIKE mbew-bwkey,
lbkum LIKE mbew-lbkum,
lfgja LIKE mbew-lfgja,
lfmon LIKE mbew-lfmon,
END OF t_mbew,
BEGIN OF t_mardh OCCURS 0,
matnr LIKE mardh-matnr,
werks LIKE mardh-werks,
lgort LIKE mardh-lgort,
umlme LIKE mardh-umlme,
END OF t_mardh,
BEGIN OF t_qtypo OCCURS 0,
bedat LIKE ekko-bedat,
ebeln LIKE ekpo-ebeln,
ebelp LIKE ekpo-ebelp,
matnr LIKE ekpo-matnr,
werks LIKE ekpo-werks,
menge LIKE ekpo-menge,
elikz LIKE ekpo-elikz,
END OF t_qtypo,
BEGIN OF t_qtypo2 OCCURS 0,
matnr LIKE ekpo-matnr,
werks LIKE ekpo-werks,
menge LIKE ekpo-menge,
END OF t_qtypo2,
t_qtypooutst LIKE t_qtypo2 OCCURS 0 WITH HEADER LINE,
BEGIN OF t_io OCCURS 0,
budat LIKE mkpf-budat,
mblnr LIKE mseg-mblnr,
zeile LIKE mseg-zeile,
werks LIKE mseg-werks,
matnr LIKE mseg-matnr,
bwart LIKE mseg-bwart,
shkzg LIKE mseg-shkzg,
menge LIKE mseg-menge,
END OF t_io,
BEGIN OF t_outpo OCCURS 0,
bedat LIKE ekko-bedat,
matnr LIKE ekpo-matnr,
werks LIKE ekpo-werks,
ebeln LIKE ekpo-ebeln,
ebelp LIKE ekpo-ebelp,
belnr LIKE ekbe-belnr,
buzei LIKE ekbe-buzei,
bwart LIKE ekbe-bwart,
menge LIKE ekbe-menge,
END OF t_outpo,
* begin of t_outpo occurs 0,
* bedat like ekko-bedat,
* matnr like ekpo-matnr,
* werks like ekpo-werks,
* ebeln like ekpo-ebeln,
* ebelp like ekpo-ebelp,
* bwart like ekbe-bwart,
* menge like ekbe-menge,
* end of t_outpo,
* ekko~bedat
* ekpo~matnr ekpo~werks
* ekbe~ebeln ekbe~ebelp
* ekbe~bwart ekbe~menge
BEGIN OF t_input2 OCCURS 0,
matnr LIKE mseg-matnr,
werks LIKE mseg-werks,
menge LIKE mseg-menge,
END OF t_input2,
BEGIN OF t_rata2 OCCURS 0,
matnr LIKE mseg-matnr,
werks LIKE mseg-werks,
menge LIKE mseg-menge,
rbagi(1) TYPE p DECIMALS 0,
END OF t_rata2,
t_outpt2 LIKE t_input2 OCCURS 0 WITH HEADER LINE,
t_kumi2 LIKE t_input2 OCCURS 0 WITH HEADER LINE,
t_kumo2 LIKE t_input2 OCCURS 0 WITH HEADER LINE,
t_other LIKE t_input2 OCCURS 0 WITH HEADER LINE,
t_kumot LIKE t_input2 OCCURS 0 WITH HEADER LINE,
t_outstx LIKE t_input2 OCCURS 0 WITH HEADER LINE,
t_stockcur LIKE t_input2 OCCURS 0 WITH HEADER LINE,
t_io2 LIKE t_io OCCURS 0 WITH HEADER LINE,
t_iobck LIKE t_io OCCURS 0 WITH HEADER LINE.

DATA : BEGIN OF t_data OCCURS 0,
matnr LIKE mara-matnr,
maktx LIKE makt-maktx,
meins LIKE mara-meins,
werks LIKE t001w-werks,
awal LIKE mbewh-lbkum,
qtypo LIKE ekpo-menge,
input LIKE mseg-menge,
kumi LIKE mseg-menge,
outst LIKE ekpo-menge,
estpk LIKE ekpo-menge,
outpt LIKE mseg-menge,
kumo LIKE mseg-menge,
pctus LIKE ekpo-zwert,
other LIKE mseg-menge,
kumot LIKE mseg-menge,
akhir LIKE mbew-lbkum,
rata2 LIKE mseg-menge,
tod(8) TYPE p DECIMALS 0, " LIKE ekpo-zwert,

END OF t_data.

DATA : v_year LIKE mbewh-lfgja,
v_lastmo LIKE mbewh-lfmon,
v_tglawal LIKE sy-datum,
v_trata_b LIKE sy-datum,
v_trata_e LIKE sy-datum,
x TYPE i,
v_var(30),
v_bln(2).
DATA: w_msg(50).

INCLUDE zab_alv.
INCLUDE zgen_excel.

**************************************
SELECTION-SCREEN BEGIN OF BLOCK blk1 WITH FRAME.
SELECT-OPTIONS : s_matnr FOR mara-matnr .
SELECT-OPTIONS : s_werks FOR t001w-werks OBLIGATORY.
PARAMETERS: p_mtart LIKE mara-mtart OBLIGATORY.
PARAMETERS: p_date LIKE sy-datum OBLIGATORY.



SELECTION-SCREEN END OF BLOCK blk1.
SELECTION-SCREEN BEGIN OF BLOCK blc2 WITH FRAME TITLE text-s20.
PARAMETERS : rb_chk1 RADIOBUTTON GROUP a DEFAULT 'X',
rb_chk2 RADIOBUTTON GROUP a.
SELECTION-SCREEN END OF BLOCK blc2.
***************************************
INITIALIZATION.
* ALV init
PERFORM f_alv_init.

AT SELECTION-SCREEN.
PERFORM authority_check CHANGING w_msg.
IF NOT w_msg IS INITIAL.
MESSAGE e398(00) WITH w_msg.
EXIT.
ENDIF.
*--

START-OF-SELECTION.
PERFORM f_getdata.
IF rb_chk1 = 'X'.
PERFORM f_displayalv.
ELSE.
WRITE:/ 'Transfer to excel complete !.'.
WRITE:/ 'Please save excel file before closing this page'.
PERFORM f_to_excel.
ENDIF.

*-----------------------------------------------------------------------
FORM authority_check CHANGING msg.
DATA: BEGIN OF lit_t001w OCCURS 0,
werks LIKE t001w-werks,
END OF lit_t001w.
CLEAR msg.
SELECT werks
FROM t001w
INTO TABLE lit_t001w
WHERE werks IN s_werks.
IF lit_t001w[] IS INITIAL.
w_msg = 'Plant Not Found'.
ELSE.
LOOP AT lit_t001w.
AUTHORITY-CHECK OBJECT 'M_BEST_WRK'
ID 'ACTVT' FIELD '03'
ID 'WERKS' FIELD lit_t001w-werks.
IF sy-subrc <> 0.
CONCATENATE 'Not Authorized to Plant' lit_t001w-werks
INTO w_msg
SEPARATED BY space.
EXIT.
ENDIF.
ENDLOOP. "lit_t001w
ENDIF.
ENDFORM. "authority_check

*&---------------------------------------------------------------------*
*& Form F_GETDATA
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
FORM f_getdata.
DATA : v_estimasi_a LIKE mdps-mng01.

SELECT a~matnr a~meins a~mtart b~maktx
INTO CORRESPONDING FIELDS OF TABLE t_mara
FROM mara AS a INNER JOIN makt AS b
ON a~matnr = b~matnr
WHERE a~matnr IN s_matnr
AND a~mtart = p_mtart.


* TGL UTK RATA2 OUTPUT, AMBIL 4 BULAN KEBELAKANG, CURRENT TDK TRMSK.
CALL FUNCTION 'HR_PT_ADD_MONTH_TO_DATE'
EXPORTING
dmm_datin = p_date
dmm_count = '4'
dmm_oper = '-'
dmm_pos = space
IMPORTING
dmm_daout = v_trata_b.
v_trata_b+6(2) = '01'.

CALL FUNCTION 'HR_PT_ADD_MONTH_TO_DATE'
EXPORTING
dmm_datin = p_date
dmm_count = '1'
dmm_oper = '-'
dmm_pos = space
IMPORTING
dmm_daout = v_trata_e.
CALL FUNCTION 'LAST_DAY_OF_MONTHS'
EXPORTING
day_in = v_trata_e
IMPORTING
last_day_of_month = v_trata_e.

v_year = v_trata_e(4).
v_lastmo = v_trata_e+4(2).

v_tglawal = p_date.
v_tglawal+6(2) = '01'.


IF NOT t_mara[] IS INITIAL.
* STOCK AWAL
SELECT * FROM mbewh
INTO CORRESPONDING FIELDS OF TABLE t_mbewh
FOR ALL ENTRIES IN t_mara
WHERE matnr = t_mara-matnr
AND bwkey IN s_werks
AND lfgja = v_year
AND lfmon = v_lastmo.
* stock awal - stock in transfer
SELECT * FROM mardh
INTO CORRESPONDING FIELDS OF TABLE t_mardh
FOR ALL ENTRIES IN t_mara
WHERE matnr = t_mara-matnr
AND werks IN s_werks
AND lfgja = v_year
AND lfmon = v_lastmo
AND umlme > 0.

* STOCK AKHIR
SELECT * FROM mbew
INTO CORRESPONDING FIELDS OF TABLE t_mbew
FOR ALL ENTRIES IN t_mara
WHERE matnr = t_mara-matnr
AND bwkey IN s_werks.
* AND lfgja = p_date(4)
* AND lfmon = p_date+4(2).


* QTY PO
SELECT ekko~bedat
ekpo~ebeln ekpo~ebelp ekpo~werks ekpo~matnr ekpo~menge ekpo~elikz
FROM ekko AS ekko
INNER JOIN ekpo AS ekpo ON ekko~ebeln = ekpo~ebeln
INTO CORRESPONDING FIELDS OF TABLE t_qtypo
FOR ALL ENTRIES IN t_mara
* WHERE ekko~bedat >= v_tglawal AND ekko~bedat <= p_date
WHERE ekko~bedat <= p_date
AND ekpo~werks IN s_werks
AND ekpo~matnr = t_mara-matnr
AND ekpo~loekz NE 'L'.
* COLLECT QTY PO
LOOP AT t_qtypo.
IF t_qtypo-bedat >= v_tglawal.
* qty po kolom F
* COLLECT t_qtypo INTO t_qtypo2.
READ TABLE t_qtypo2 WITH KEY matnr = t_qtypo-matnr.
IF sy-subrc = 0.
t_qtypo2-menge = t_qtypo2-menge + t_qtypo-menge.
MODIFY t_qtypo2 INDEX sy-tabix.
ELSE.
t_qtypo2-matnr = t_qtypo-matnr.
t_qtypo2-menge = t_qtypo-menge.
t_qtypo2-werks = t_qtypo-werks.
APPEND t_qtypo2.
ENDIF.
ENDIF.
* qty all outstanding po per material
IF t_qtypo-elikz NE 'X'.
READ TABLE t_qtypooutst WITH KEY matnr = t_qtypo-matnr.
IF sy-subrc = 0.
t_qtypooutst-menge = t_qtypooutst-menge + t_qtypo-menge.
MODIFY t_qtypooutst INDEX sy-tabix.
ELSE.
t_qtypooutst-matnr = t_qtypo-matnr.
t_qtypooutst-menge = t_qtypo-menge.
t_qtypooutst-werks = t_qtypo-werks.
APPEND t_qtypooutst.
ENDIF.
ENDIF.


ENDLOOP.

* INPUT HARI INI (P_DATE) DAN KUMULATIF (AWAL S/D P_DATE)
* SELECT mkpf~budat mseg~matnr mseg~bwart mseg~menge
* FROM mkpf AS mkpf
* INNER JOIN mseg AS mseg
* ON mkpf~mblnr = mseg~mblnr AND mkpf~mjahr = mseg~mjahr
* INTO CORRESPONDING FIELDS OF TABLE t_io
* FOR ALL ENTRIES IN t_mara
* WHERE mkpf~budat >= v_tglawal AND mkpf~budat <= p_date
* AND mseg~matnr = t_mara-matnr
* AND mseg~werks IN s_werks
* AND mseg~bwart IN ('101', '102', '261', '262' ).

SELECT mkpf~budat mseg~mblnr mseg~zeile mseg~werks mseg~matnr
mseg~bwart mseg~shkzg mseg~menge
FROM mkpf AS mkpf
INNER JOIN mseg AS mseg
ON mkpf~mblnr = mseg~mblnr AND mkpf~mjahr = mseg~mjahr
INTO CORRESPONDING FIELDS OF TABLE t_io
FOR ALL ENTRIES IN t_mara
WHERE mkpf~budat >= v_trata_b AND mkpf~budat <= p_date
AND mseg~matnr = t_mara-matnr
AND mseg~werks IN s_werks
AND mseg~xauto EQ space.
* AND mseg~bwart IN ('101', '102', '261', '262' ).

* all outstanding po, utk material2 selected di mbewh
IF NOT t_mbewh[] IS INITIAL.
LOOP AT t_mbewh.
t_t_mbewh-matnr = t_mbewh-matnr.
t_t_mbewh-werks = t_mbewh-bwkey.
APPEND t_t_mbewh.
ENDLOOP.
SELECT ekko~bedat
ekpo~ebeln ekpo~ebelp
ekpo~matnr ekpo~werks
ekbe~belnr ekbe~buzei ekbe~bwart ekbe~menge
FROM ekko AS ekko
INNER JOIN ekpo AS ekpo ON ekko~ebeln = ekpo~ebeln
INNER JOIN ekbe AS ekbe ON ekpo~ebeln = ekbe~ebeln
AND ekpo~ebelp = ekbe~ebelp
INTO CORRESPONDING FIELDS OF TABLE t_outpo
FOR ALL ENTRIES IN t_t_mbewh
WHERE ( ekpo~elikz EQ space AND ekpo~loekz EQ space )
AND ekko~bedat <= p_date
AND ekpo~matnr = t_t_mbewh-matnr
AND ekpo~werks = t_t_mbewh-werks
AND ekbe~budat <= p_date.


* calculate var pengurang total qty all po outst per mat
LOOP AT t_outpo.
IF t_outpo-bwart = '101' OR t_outpo-bwart = '123'.
READ TABLE t_outstx WITH KEY matnr = t_outpo-matnr
werks = t_outpo-werks.
IF sy-subrc = 0.
t_outstx-menge = t_outstx-menge + t_outpo-menge.
MODIFY t_outstx INDEX sy-tabix.
ELSE.
t_outstx-matnr = t_outpo-matnr.
t_outstx-menge = t_outpo-menge.
t_outstx-werks = t_outpo-werks.
APPEND t_outstx.
ENDIF.
ELSEIF t_outpo-bwart = '102' OR t_outpo-bwart = '122'.
READ TABLE t_outstx WITH KEY matnr = t_outpo-matnr
werks = t_outpo-werks.
IF sy-subrc = 0.
t_outstx-menge = t_outstx-menge - t_outpo-menge.
MODIFY t_outstx INDEX sy-tabix.
ELSE.
t_outstx-matnr = t_outpo-matnr.
t_outstx-menge = t_outpo-menge * - 1.
t_outstx-werks = t_outpo-werks.
APPEND t_outstx.
ENDIF.

ENDIF.
ENDLOOP.
ENDIF.
DATA : v_tglx LIKE sy-datum,
var_menge LIKE mseg-menge.
SORT t_io BY budat werks mblnr matnr.
LOOP AT t_io.
*---
IF t_io-budat = p_date.
* I/O HARI INI
IF t_io-bwart = '101'.
READ TABLE t_input2 WITH KEY matnr = t_io-matnr
werks = t_io-werks.
IF sy-subrc = 0.
t_input2-menge = t_input2-menge + t_io-menge.
MODIFY t_input2 INDEX sy-tabix.
ELSE.
t_input2-matnr = t_io-matnr.
t_input2-menge = t_io-menge.
t_input2-werks = t_io-werks.
APPEND t_input2.
ENDIF.
ELSEIF t_io-bwart = '102'.
READ TABLE t_input2 WITH KEY matnr = t_io-matnr
werks = t_io-werks.
IF sy-subrc = 0.
t_input2-menge = t_input2-menge - t_io-menge.
MODIFY t_input2 INDEX sy-tabix.
ELSE.
t_input2-matnr = t_io-matnr.
t_input2-menge = t_io-menge * -1.
t_input2-werks = t_io-werks.
APPEND t_input2.
ENDIF.
ELSEIF t_io-bwart = '261'.
READ TABLE t_outpt2 WITH KEY matnr = t_io-matnr
werks = t_io-werks.
IF sy-subrc = 0.
t_outpt2-menge = t_outpt2-menge + t_io-menge.
MODIFY t_outpt2 INDEX sy-tabix.
ELSE.
t_outpt2-matnr = t_io-matnr.
t_outpt2-menge = t_io-menge.
t_outpt2-werks = t_io-werks.
APPEND t_outpt2.
ENDIF.
ELSEIF t_io-bwart = '262'.
READ TABLE t_outpt2 WITH KEY matnr = t_io-matnr
werks = t_io-werks.
IF sy-subrc = 0.
t_outpt2-menge = t_outpt2-menge - t_io-menge.
MODIFY t_outpt2 INDEX sy-tabix.
ELSE.
t_outpt2-matnr = t_io-matnr.
t_outpt2-menge = t_io-menge * -1.
t_outpt2-werks = t_io-werks.
APPEND t_outpt2.
ENDIF.
ELSE.
*---
* OTHERS - H = MINUS
IF t_io-shkzg = 'H'. t_io-menge = t_io-menge * -1. ENDIF.
READ TABLE t_other WITH KEY matnr = t_io-matnr
werks = t_io-werks.
IF sy-subrc = 0.
t_other-menge = t_other-menge + t_io-menge.
MODIFY t_other INDEX sy-tabix.
ELSE.
t_other-matnr = t_io-matnr.
t_other-menge = t_io-menge.
t_other-werks = t_io-werks.
APPEND t_other.
ENDIF.
* balikin lagi qty nya
IF t_io-shkzg = 'H'.
t_io-menge = t_io-menge * -1.
ENDIF.
ENDIF.
ENDIF.
*---
* I/O KUM DAN OTHER KUM
IF t_io-budat >= v_tglawal AND t_io-budat <= p_date.
IF t_io-bwart = '101'.
READ TABLE t_kumi2 WITH KEY matnr = t_io-matnr
werks = t_io-werks.
IF sy-subrc = 0.
t_kumi2-menge = t_kumi2-menge + t_io-menge.
MODIFY t_kumi2 INDEX sy-tabix.
ELSE.
t_kumi2-matnr = t_io-matnr.
t_kumi2-menge = t_io-menge.
t_kumi2-werks = t_io-werks.
APPEND t_kumi2.
ENDIF.
ELSEIF t_io-bwart = '102'.
READ TABLE t_kumi2 WITH KEY matnr = t_io-matnr
werks = t_io-werks.
IF sy-subrc = 0.
t_kumi2-menge = t_kumi2-menge - t_io-menge.
MODIFY t_kumi2 INDEX sy-tabix.
ELSE.
t_kumi2-matnr = t_io-matnr.
t_kumi2-menge = t_io-menge * -1.
t_kumi2-werks = t_io-werks.
APPEND t_kumi2.
ENDIF.
ELSEIF t_io-bwart = '261'.
READ TABLE t_kumo2 WITH KEY matnr = t_io-matnr
werks = t_io-werks.
IF sy-subrc = 0.
t_kumo2-menge = t_kumo2-menge + t_io-menge.
MODIFY t_kumo2 INDEX sy-tabix.
ELSE.
t_kumo2-matnr = t_io-matnr.
t_kumo2-menge = t_io-menge.
t_kumo2-werks = t_io-werks.
APPEND t_kumo2.
ENDIF.
ELSEIF t_io-bwart = '262'.
READ TABLE t_kumo2 WITH KEY matnr = t_io-matnr
werks = t_io-werks.
IF sy-subrc = 0.
t_kumo2-menge = t_kumo2-menge - t_io-menge.
MODIFY t_kumo2 INDEX sy-tabix.
ELSE.
t_kumo2-matnr = t_io-matnr.
t_kumo2-menge = t_io-menge * -1.
t_kumo2-werks = t_io-werks.
APPEND t_kumo2.
ENDIF.
ELSE.
*---
* KUMULATIF OTHERS - H = MINUS
IF t_io-shkzg = 'H'. t_io-menge = t_io-menge * -1. ENDIF.
READ TABLE t_kumot WITH KEY matnr = t_io-matnr
werks = t_io-werks.
IF sy-subrc = 0.
t_kumot-menge = t_kumot-menge + t_io-menge.
MODIFY t_kumot INDEX sy-tabix.
ELSE.
t_kumot-matnr = t_io-matnr.
t_kumot-menge = t_io-menge.
t_kumot-werks = t_io-werks.
APPEND t_kumot.
ENDIF.
* balikin lagi qty nya
IF t_io-shkzg = 'H'. t_io-menge = t_io-menge * -1. ENDIF.


ENDIF.
*---
* hitung stock current di mseg, S = MINUS
IF t_io-shkzg = 'S'. t_io-menge = t_io-menge * -1. ENDIF.
READ TABLE t_stockcur WITH KEY matnr = t_io-matnr
werks = t_io-werks.
IF sy-subrc = 0.
t_stockcur-menge = t_stockcur-menge - t_io-menge.
MODIFY t_stockcur INDEX sy-tabix.
ELSE.
t_stockcur-matnr = t_io-matnr.
t_stockcur-menge = t_io-menge * -1.
t_stockcur-werks = t_io-werks.
APPEND t_stockcur.
ENDIF.
* balikin lagi qty nya
IF t_io-shkzg = 'S'. t_io-menge = t_io-menge * -1. ENDIF.

ENDIF.
*---
* RATA2 OUTPUT
IF t_io-budat >= v_trata_b
AND t_io-budat <= v_trata_e
AND ( t_io-bwart = '261' OR t_io-bwart = '262' ) .
IF t_io-bwart = '261'.
READ TABLE t_rata2 WITH KEY matnr = t_io-matnr
werks = t_io-werks.
IF sy-subrc = 0.
IF v_bln = t_io-budat+4(2).
ELSE.
t_rata2-rbagi = t_rata2-rbagi + 1.
v_bln = t_io-budat+4(2).
ENDIF.
t_rata2-menge = t_rata2-menge + t_io-menge.
MODIFY t_rata2 INDEX sy-tabix.
ELSE.
t_rata2-matnr = t_io-matnr.
t_rata2-menge = t_io-menge.
t_rata2-werks = t_io-werks.
t_rata2-rbagi = 1.
v_bln = t_io-budat+4(2).
APPEND t_rata2.
ENDIF.
ELSEIF t_io-bwart = '262'.
READ TABLE t_rata2 WITH KEY matnr = t_io-matnr
werks = t_io-werks.
IF sy-subrc = 0.
IF v_bln = t_io-budat+4(2).
ELSE.
t_rata2-rbagi = t_rata2-rbagi + 1.
v_bln = t_io-budat+4(2).
ENDIF.
t_rata2-menge = t_rata2-menge - t_io-menge.
MODIFY t_rata2 INDEX sy-tabix.
ELSE.
t_rata2-matnr = t_io-matnr.
t_rata2-menge = t_io-menge * -1.
t_rata2-werks = t_io-werks.
t_rata2-rbagi = 1.
v_bln = t_io-budat+4(2).
APPEND t_rata2.
ENDIF.

ENDIF.
* count jumlah bulan utk variable pembagi rata2
* if v_bln = t_io-budat+4(2).
*
* else.
* v_monthr = v_monthr + 1.
* v_bln = t_io-budat+4(2).
* endif.

ENDIF.
ENDLOOP.


ENDIF.


* COMBINE ALL DATA INTO 1 TABLE.
SORT t_mara BY matnr.
SORT t_mbewh BY matnr bwkey.
SORT t_mbew BY matnr bwkey.
SORT t_qtypo BY matnr werks.
SORT t_input2 BY matnr werks.
SORT t_outpt2 BY matnr werks.
SORT t_kumi2 BY matnr werks.
SORT t_kumo2 BY matnr werks.
SORT t_rata2 BY matnr werks.
SORT t_other BY matnr werks.
SORT t_kumot BY matnr werks.
* HEADER EXCEL
CONCATENATE 'Tgl. ' p_date+6(2) '-' p_date+4(2) '-' p_date(4)
INTO v_var SEPARATED BY space.
PERFORM modify_to_cell USING 9 2 v_var.

x = 7.
LOOP AT t_mbewh.
CLEAR t_data.
v_estimasi_a = 0.
* MATERIAL DATA
t_data-matnr = t_mbewh-matnr.
READ TABLE t_mara WITH KEY matnr = t_mbewh-matnr.
t_data-maktx = t_mara-maktx.
t_data-meins = t_mara-meins.
t_data-werks = t_mbewh-bwkey.

* STOCK AWAL
t_data-awal = t_mbewh-lbkum.
var_menge = 0.
* stock awal v.4 - ditambahkan dengan transaksi back-posting.
SELECT mkpf~budat mseg~mblnr mseg~zeile mseg~werks mseg~matnr
mseg~bwart mseg~shkzg mseg~menge
FROM mkpf AS mkpf
INNER JOIN mseg AS mseg
ON mkpf~mblnr = mseg~mblnr AND mkpf~mjahr = mseg~mjahr
INTO CORRESPONDING FIELDS OF TABLE t_iobck
WHERE mkpf~budat <= v_trata_e AND mkpf~bldat > v_trata_e
AND mseg~matnr = t_data-matnr
AND mseg~werks = t_data-werks.
IF NOT t_iobck[] IS INITIAL.
LOOP AT t_iobck.
IF t_iobck-bwart EQ '321' OR t_iobck-bwart EQ '322' OR
t_iobck-bwart EQ '343' OR t_iobck-bwart EQ '344' OR
t_iobck-bwart EQ '349' OR t_iobck-bwart EQ '350' .
ELSE.
IF t_iobck-shkzg = 'H'.
t_iobck-menge = t_iobck-menge * -1.
ENDIF.
var_menge = var_menge + t_iobck-menge.
ENDIF.
ENDLOOP.
t_data-awal = t_data-awal + var_menge.
ENDIF.
* stock awal v.4 - dikurangi dengan stock in transfer
LOOP AT t_mardh WHERE matnr = t_data-matnr AND werks = t_data-werks.
t_data-awal = t_data-awal - t_mardh-umlme.
ENDLOOP.


* QTY PO
READ TABLE t_qtypo2 WITH KEY matnr = t_mbewh-matnr
werks = t_mbewh-bwkey.
IF sy-subrc = 0.
t_data-qtypo = t_qtypo2-menge.
ENDIF.
* INPUT HARI INI
READ TABLE t_input2 WITH KEY matnr = t_mbewh-matnr
werks = t_mbewh-bwkey.
IF sy-subrc = 0.
t_data-input = t_input2-menge.
ENDIF.
* KUM INPUT
READ TABLE t_kumi2 WITH KEY matnr = t_mbewh-matnr
werks = t_mbewh-bwkey.
IF sy-subrc = 0.
t_data-kumi = t_kumi2-menge.
ENDIF.

* OUTSTANDING PO QTY, ALL PO QTY DARI SEJAK LIVE - EKBE(BWART 101,102,
*123,122)
t_data-outst = 0.
READ TABLE t_qtypooutst WITH KEY matnr = t_mbewh-matnr
werks = t_mbewh-bwkey.
IF sy-subrc = 0.
READ TABLE t_outstx WITH KEY matnr = t_mbewh-matnr
werks = t_mbewh-bwkey.
IF sy-subrc = 0.
t_data-outst = t_qtypooutst-menge - t_outstx-menge.
ELSE.
t_data-outst = t_qtypooutst-menge.
ENDIF.
ENDIF.


* OUTPUT HARI INI
READ TABLE t_outpt2 WITH KEY matnr = t_mbewh-matnr
werks = t_mbewh-bwkey.
IF sy-subrc = 0.
t_data-outpt = t_outpt2-menge.
ENDIF.
* KUM OUTPUT
READ TABLE t_kumo2 WITH KEY matnr = t_mbewh-matnr
werks = t_mbewh-bwkey.
IF sy-subrc = 0.
t_data-kumo = t_kumo2-menge.
ENDIF.


* ESTIMASI PAKAI
PERFORM f_get_md01 USING v_tglawal t_mbewh-matnr t_mbewh-bwkey
CHANGING v_estimasi_a.
t_data-estpk = v_estimasi_a + t_data-kumo.


* PERSEN USAGE
IF t_data-estpk = 0.
t_data-pctus = 0.
ELSE.
t_data-pctus = ( t_data-kumo / t_data-estpk ) * 100.
ENDIF.

* OTHERS
READ TABLE t_other WITH KEY matnr = t_mbewh-matnr
werks = t_mbewh-bwkey.
IF sy-subrc = 0.
t_data-other = t_other-menge.
ENDIF.

* KUM OTHER
READ TABLE t_kumot WITH KEY matnr = t_mbewh-matnr
werks = t_mbewh-bwkey.
IF sy-subrc = 0.
t_data-kumot = t_kumot-menge.
ENDIF.

** STOCK AKHIR, v.2
t_data-akhir = 0.
IF p_date = sy-datum.
* kalau hari ini, brati ambil dari mbew lfgja dan lfmon = now.
READ TABLE t_mbew WITH KEY matnr = t_mbewh-matnr
bwkey = t_mbewh-bwkey
lfgja = sy-datum(4)
lfmon = sy-datum+4(2).
IF sy-subrc = 0 AND t_mbew-lbkum > 0.
t_data-akhir = t_mbew-lbkum.
ENDIF.
ENDIF.
IF t_data-akhir = 0.
* stock awal + transaksi (stock cur)
READ TABLE t_stockcur WITH KEY matnr = t_data-matnr
werks = t_data-werks.
IF sy-subrc = 0.
t_data-akhir = t_data-awal + t_stockcur-menge.
ELSE.
t_data-akhir = t_data-awal .
ENDIF.

ENDIF.
* kalau masih 0 juga, pake cara trakhir.
IF t_data-akhir = 0.
DATA : temp_mbewh LIKE mbewh OCCURS 0 WITH HEADER LINE,
v_mbewh(6), var_ok(1).
* CARI DATA DARI T_MBEWH TRAKHIR YG LBKUM > 0.
SELECT * FROM mbewh
INTO CORRESPONDING FIELDS OF TABLE temp_mbewh
* FOR ALL ENTRIES IN t_mara
WHERE matnr = t_mbewh-matnr
AND bwkey = t_mbewh-bwkey
AND lbkum > 0 ORDER BY lfgja DESCENDING lfmon DESCENDING.
* ditambah dgn transaksi dari awal bulan selanjutnya - tgl yg diinput
CLEAR : var_ok.
LOOP AT temp_mbewh.
CONCATENATE temp_mbewh-lfgja temp_mbewh-lfmon INTO v_mbewh.
var_ok = 'N'.
IF v_mbewh < p_date(6).
var_ok = 'Y'.
EXIT.
ENDIF.
ENDLOOP.
IF var_ok NE 'Y'.
CLEAR temp_mbewh.
t_data-akhir = 0.
ELSE.
v_tglx = '00000000'.
* ambil transaksi dari awal bulan temp_mbewh-lfmon + 1 smp p_date
CONCATENATE temp_mbewh-lfgja temp_mbewh-lfmon '01' INTO v_tglx.

var_menge = 0.
CLEAR t_io2.
REFRESH t_io2.
CALL FUNCTION 'HR_PT_ADD_MONTH_TO_DATE'
EXPORTING
dmm_datin = v_tglx
dmm_count = '1'
dmm_oper = '+'
dmm_pos = space
IMPORTING
dmm_daout = v_tglx.

SELECT mkpf~budat mseg~mblnr mseg~zeile mseg~werks mseg~matnr
mseg~bwart mseg~shkzg mseg~menge
FROM mkpf AS mkpf
INNER JOIN mseg AS mseg
ON mkpf~mblnr = mseg~mblnr AND mkpf~mjahr = mseg~mjahr
INTO CORRESPONDING FIELDS OF TABLE t_io2
WHERE mkpf~budat >= v_tglx AND mkpf~budat <= p_date
AND mseg~matnr = t_data-matnr
AND mseg~werks = t_data-werks.
LOOP AT t_io2.
IF t_io2-bwart EQ '321' OR t_io2-bwart EQ '322' OR
t_io2-bwart EQ '343' OR t_io2-bwart EQ '344' OR
t_io2-bwart EQ '349' OR t_io2-bwart EQ '350' .
ELSE.
IF t_io2-shkzg = 'H'. t_io2-menge = t_io2-menge * -1. ENDIF.
var_menge = var_menge + t_io2-menge.
ENDIF.
ENDLOOP.
t_data-akhir = temp_mbewh-lbkum + var_menge.
ENDIF.
ENDIF.
* RATA2 OUTPUT
READ TABLE t_rata2 WITH KEY matnr = t_mbewh-matnr
werks = t_mbewh-bwkey.
IF sy-subrc = 0.
t_data-rata2 = t_rata2-menge / t_rata2-rbagi.
ENDIF.

* T O D
IF t_data-akhir = 0 OR t_data-rata2 = 0.
t_data-tod = 0.
ELSE.
t_data-tod = t_data-akhir / ( t_data-rata2 / 25 ) .
ENDIF.

* filter yg 0 ga usah di add.
IF t_data-awal = 0
AND t_data-qtypo = 0
AND t_data-input = 0
AND t_data-kumi = 0
AND t_data-outst = 0
AND t_data-estpk = 0
AND t_data-kumo = 0
AND t_data-pctus = 0
AND t_data-other = 0
AND t_data-kumot = 0
AND t_data-akhir = 0
AND t_data-rata2 = 0
AND t_data-tod = 0.
ELSE.
APPEND t_data.


* PREPARE EXCEL
PERFORM modify_to_cell USING 1 x t_data-matnr.
PERFORM modify_to_cell USING 2 x t_data-maktx.
PERFORM modify_to_cell USING 3 x t_data-meins.
PERFORM modify_to_cell USING 4 x t_data-werks.
PERFORM append_to_cell USING 'C' 5 x t_data-awal.
PERFORM append_to_cell USING 'C' 6 x t_data-qtypo.
PERFORM append_to_cell USING 'C' 7 x t_data-input.
PERFORM append_to_cell USING 'C' 8 x t_data-kumi.
PERFORM append_to_cell USING 'C' 9 x t_data-outst.
PERFORM append_to_cell USING 'C' 10 x t_data-estpk.
PERFORM append_to_cell USING 'C' 11 x t_data-outpt.
PERFORM append_to_cell USING 'C' 12 x t_data-kumo.
PERFORM append_to_cell USING 'C' 13 x t_data-pctus.
PERFORM append_to_cell USING 'C' 14 x t_data-other.
PERFORM append_to_cell USING 'C' 15 x t_data-kumot.
PERFORM append_to_cell USING 'C' 16 x t_data-akhir.
PERFORM append_to_cell USING 'C' 17 x t_data-rata2.
PERFORM append_to_cell USING 'C' 18 x t_data-tod.

x = x + 1.
ENDIF.



ENDLOOP.

x = x - 7.
PERFORM modify_to_cell USING 1 1 x.


ENDFORM. " F_GETDATA





*&---------------------------------------------------------------------*
*& Form f_get_md01
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* -->P_V_TGLAWAL text
* -->P_V_MATNR text
* -->P_V_WERKS text
* <--P_V_ESTIMASI_A text
*----------------------------------------------------------------------*
FORM f_get_md01 USING p_v_tglawal
p_v_matnr
p_v_werks
CHANGING p_v_estimasi_a.

TYPES:
BEGIN OF m61x_mdlg_r,
sign(1),
option(2),
low LIKE mdlg-lgort,
high LIKE mdlg-lgort,
END OF m61x_mdlg_r.

TYPES:
BEGIN OF m61x_mdll_r,
sign(1),
option(2),
low LIKE mdll-lbear,
high LIKE mdll-lbear,
END OF m61x_mdll_r.

TYPES:
m61x_mdlg_range TYPE m61x_mdlg_r OCCURS 0,
m61x_mdll_range TYPE m61x_mdll_r OCCURS 0.
DATA: BEGIN OF v_cm61b,
disfg LIKE t000md-disfg,
dbxxx TYPE c,
berid LIKE mdlv-berid,
berty LIKE mdlv-berty,
bertx LIKE mdlv-bertx,
werzg LIKE mdlv-werzg,
ortzg LIKE mdlv-ortzg,
lbear LIKE mdll-lbear,
mdlgx_range TYPE m61x_mdlg_range, " OCCURS 0,
mdlgy_range TYPE m61x_mdlg_range, " OCCURS 0,
mdlly_range TYPE m61x_mdll_range,
sobsl LIKE mdma-sobsl,
disgr LIKE mdma-disgr,
END OF v_cm61b.

DATA v_mt61d LIKE mt61d.
DATA v_cm61x LIKE cm61x.
DATA v_cm61w LIKE cm61w.
DATA v_t399d LIKE t399d.
DATA v_mdkp LIKE mdkp .
DATA v_cm61m LIKE cm61m .

*MRP table used in MD04
DATA i_mdpsx LIKE mdps OCCURS 0 WITH HEADER LINE.

DATA :
p_v_tglakhir LIKE sy-datum.
*get akhir bulan

CALL FUNCTION 'LAST_DAY_OF_MONTHS'
EXPORTING
day_in = p_v_tglawal
IMPORTING
last_day_of_month = p_v_tglakhir.


CALL FUNCTION 'MD_READ_MATERIAL'
EXPORTING
ewerks = p_v_werks
ematnr = p_v_matnr
IMPORTING
imt61d = v_mt61d
icm61b = v_cm61b
EXCEPTIONS
error = 1
OTHERS = 2.
*if no record found, write out error
IF sy-subrc <> 0.
* MESSAGE e000(vz) WITH 'MD_READ_MATERIAL failed on' v_matnr v_werks.
ENDIF.

CALL FUNCTION 'MD_READ_PLANT'
EXPORTING
ewerks = p_v_werks
IMPORTING
icm61w = v_cm61w
it399d = v_t399d
EXCEPTIONS
error = 01.

*CONSTRUCT MDPSX ANNOUNCEMENTS
CALL FUNCTION 'AUFBAUEN_MDPSX_ANZEIGEN'
EXPORTING
ecm61w = v_cm61w
ecm61x = v_cm61x
emt61d = v_mt61d
et399d = v_t399d
ecm61b = v_cm61b
IMPORTING
icm61m = v_cm61m
imdkp = v_mdkp
TABLES
mdpsx = i_mdpsx.

*if no record, write out error
IF sy-subrc <> 0.
* MESSAGE e000(vz) WITH 'MD04 read failed' v_matnr v_werks.
ELSE .
CALL FUNCTION 'PRUEFEN_UMTERMINIERUNG'
EXPORTING
ecm61m = v_cm61m
ecm61w = v_cm61w
emdkp = v_mdkp
et399d = v_t399d
TABLES
mdpsx = i_mdpsx.
LOOP AT i_mdpsx.
IF i_mdpsx-dat00 >= p_v_tglawal AND i_mdpsx-dat00 <= p_v_tglakhir
AND i_mdpsx-plumi = '-'.
* IF i_mdpsx-plumi = '+'.
* i_mdpsx-mng01 = i_mdpsx-mng01 * -1.
* ENDIF.
p_v_estimasi_a = p_v_estimasi_a + i_mdpsx-mng01.
ENDIF.
ENDLOOP.
ENDIF.
ENDFORM. " f_get_md01
*&---------------------------------------------------------------------*
*& Form f_displayalv
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
FORM f_displayalv.
* ALV Layout
PERFORM f_alv_build_layout USING :
'ZEBRA' 'X',
'NO_COLHEAD' '',
'GROUP_CHANGE_EDIT' 'X',
'F2CODE' 'F2'.
* Sort Field
* perform f_alv_sort using:
* 'T_DATA' 'ERDAT' 'X' '' ''.
* print setup
PERFORM f_build_print USING d_alv_print.
PERFORM f_create_header.
PERFORM f_alv_build_event USING:
slis_ev_top_of_page 'F_TOP_OF_PAGE', "c_top_of_page_alv,
slis_ev_user_command c_user_command.

* ALV Fieldcat
PERFORM f_build_fieldcat.
* Execute ALV Function
d_alv_varnt-variant = p_vari.
* PERFORM f_alv_grid_display TABLES t_data.
* clear t_header.
* PERFORM f_top_of_page_alv.
PERFORM f_alv_grid_display TABLES t_data.
** using '' d_alv_save.
* perform f_alv_list_display tables t_data
* using '' d_alv_save.

ENDFORM. " f_displayalv
*&---------------------------------------------------------------------*
*& Form f_build_fieldcat
*&---------------------------------------------------------------------*
* Build Fieldcat ALV
*----------------------------------------------------------------------*
FORM f_build_fieldcat .
PERFORM f_alv_build_catalog USING :
t_alv_fctlg 'T_DATA' 'MATNR' 'Material' '' ''
'' '10' '' 'X' '' '1' '1' '' '' '',
t_alv_fctlg 'T_DATA' 'MAKTX' 'Material Desc' '' ''
'' '30' '' 'X' '' '1' '2' '' '' '',
t_alv_fctlg 'T_DATA' 'MEINS' 'BaseUnit' '' ''
'' '8' '' 'X' '' '1' '3' '' '' '',
t_alv_fctlg 'T_DATA' 'WERKS' 'Plant' '' ''
'' '6' '' 'X' '' '1' '4' '' '' ''.

PERFORM f_alv_build_catalog_qty USING:
t_alv_fctlg 'T_DATA' 'AWAL' 'Stock Awal'
'MBEWH' 'LBKUM' '' '16' '' '' '' 'MEINS' '' '1' '5',
t_alv_fctlg 'T_DATA' 'QTYPO' 'QTY Po'
'EKPO' 'MENGE' '' '16' '' '' '' 'MEINS' '' '1' '6',
t_alv_fctlg 'T_DATA' 'INPUT' 'Input - HariIni'
'MSEG' 'MENGE' '' '16' '' '' '' 'MEINS' '' '1' '7',
t_alv_fctlg 'T_DATA' 'KUMI' 'Input - Kumulatif'
'MSEG' 'MENGE' '' '16' '' '' '' 'MEINS' '' '1' '8',
t_alv_fctlg 'T_DATA' 'OUTST' 'Outstanding PO'
'EKPO' 'MENGE' '' '16' '' '' '' 'MEINS' '' '1' '9',
t_alv_fctlg 'T_DATA' 'ESTPK' 'Estimasi Pakai'
'EKPO' 'MENGE' '' '16' '' '' '' 'MEINS' '' '1' '10',
t_alv_fctlg 'T_DATA' 'OUTPT' 'Output - HariIni'
'MSEG' 'MENGE' '' '16' '' '' '' 'MEINS' '' '1' '11',
t_alv_fctlg 'T_DATA' 'KUMO' 'Output - Kumulatif'
'MSEG' 'MENGE' '' '16' '' '' '' 'MEINS' '' '1' '12'.

PERFORM f_alv_build_catalog_crncy USING:
t_alv_fctlg 'T_DATA' 'PCTUS' '% Usage'
'EKPO' 'ZWERT' '16' '' '' 'WAERS' '' '' '1' '13' ''.

PERFORM f_alv_build_catalog_qty USING:
t_alv_fctlg 'T_DATA' 'OTHER' 'Other - Hari ini'
'EKPO' 'MENGE' '' '16' '' '' '' 'MEINS' '' '1' '14',
t_alv_fctlg 'T_DATA' 'KUMOT' 'Other - Kumulatif'
'MSEG' 'MENGE' '' '16' '' '' '' 'MEINS' '' '1' '15',
t_alv_fctlg 'T_DATA' 'AKHIR' 'Stock Akhir'
'MBEW' 'LBKUM' '' '16' '' '' '' 'MEINS' '' '1' '16',
t_alv_fctlg 'T_DATA' 'RATA2' 'Rata-rata Output'
'MSEG' 'MENGE' '' '16' '' '' '' 'MEINS' '' '1' '17'.

PERFORM f_alv_build_catalog_qty USING:
t_alv_fctlg 'T_DATA' 'TOD' 'T O D'
'EKPO' 'UMREZ' '' '16' '' '' '' 'MEINS' '' '1' '18'.

* perform f_alv_build_catalog_crncy using:
* t_alv_fctlg 'T_DATA' 'TOD' 'T O D'
* 'EKPO' 'UMREZ' '16' '' '' 'WAERS' '' '' '1' '17' ''.

ENDFORM.
*&---------------------------------------------------------------------*
*& Form f_to_excel
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
FORM f_to_excel.
CALL SCREEN 8999.
ENDFORM. " f_to_excel
*&---------------------------------------------------------------------*
*& Module STATUS_8999 OUTPUT
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
MODULE status_8999 OUTPUT.
SET PF-STATUS '8999'.
SET TITLEBAR '8999'.

ENDMODULE. " STATUS_8999 OUTPUT
*&---------------------------------------------------------------------*
*& Module m_init OUTPUT
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
MODULE m_init OUTPUT.
DATA : w_para TYPE i.
PERFORM connect_to_excel USING '' "Excel in SAP
'ZMM' "Object Name
'ZMM' "Object ID
'TEMP_ZMM_REALISASI' "Descr
'' "Status Logo
''. "Logo
PERFORM select_new_sheet USING 'Sheet1'.
PERFORM formatting_rangesdef_tab.
PERFORM set_data.

*--
PERFORM run_macro_param USING 'kotak_ok'
w_para 'Sheet1'
'' '' '' '' '' '' '' '' '' '' ''.

PERFORM close_excel_link.
LEAVE TO SCREEN 0.
ENDMODULE. " m_init OUTPUT

*&---------------------------------------------------------------------*
*& Form f_create_header
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
FORM f_create_header.
DATA: l_wa_listheader TYPE slis_listheader,
lv_date1(10),
lv_date2(10).

l_wa_listheader-typ = 'H'.
l_wa_listheader-key = 'LAPORAN REALISASI HARIAN'.
l_wa_listheader-info = 'LAPORAN REALISASI HARIAN'.
APPEND l_wa_listheader TO t_header.

l_wa_listheader-typ = 'A'.
l_wa_listheader-key = 'LAPORAN REALISASI HARIAN'.
CONCATENATE p_date+6(2) p_date+4(2) p_date(4) INTO
l_wa_listheader-info
SEPARATED BY '/'.
CONCATENATE 'Tgl. :' l_wa_listheader-info INTO
l_wa_listheader-info SEPARATED BY space.
APPEND l_wa_listheader TO t_header.
ENDFORM. " f_create_header

*---------------------------------------------------------------------*
* FORM F_TOP_OF_PAGE *
*---------------------------------------------------------------------*
* TOP-OF-PAGE *
*---------------------------------------------------------------------*
FORM f_top_of_page.
CALL FUNCTION 'REUSE_ALV_COMMENTARY_WRITE'
EXPORTING
it_list_commentary = t_header.
ENDFORM. "F_TOP_OF_PAGE
*---------------------------------------------------------------------*
* FORM append_to_cell *
*---------------------------------------------------------------------*
* ........ *
*---------------------------------------------------------------------*
* --> P_CT *
* --> P_COLS *
* --> P_ROWS *
* --> P_VALUE *
*---------------------------------------------------------------------*
FORM append_to_cell USING p_ct p_cols p_rows
CHANGING p_value.
DATA : lv_value(18).
lv_value = p_value.
IF p_ct = 'C'.
PERFORM f_format_xx USING p_value CHANGING lv_value.
ENDIF.
struc_generic-column = p_cols.
struc_generic-row = p_rows.
struc_generic-value = lv_value.
APPEND struc_generic TO tcontents.
ENDFORM. " append_to_cell
*---------------------------------------------------------------------*
* FORM f_format_xx *
*---------------------------------------------------------------------*
* ........ *
*---------------------------------------------------------------------*
* --> FV_AMO *
* --> FV_AMOUNT *
*---------------------------------------------------------------------*
FORM f_format_xx USING fv_amo CHANGING fv_amount TYPE format_amount.
DATA : p_val TYPE p DECIMALS 2,
y TYPE i, x TYPE i.
p_val = fv_amo.
IF p_val < 0.
p_val = p_val * -1.
WRITE p_val TO fv_amount CURRENCY 'USD'.
CONCATENATE '-' fv_amount INTO fv_amount.
ELSE.
WRITE p_val TO fv_amount CURRENCY 'USD'.
ENDIF.
CONDENSE fv_amount NO-GAPS.
ENDFORM. " f_format_xx