*&---------------------------------------------------------------------*
*& Include ZIC4_PO_IMPL
*&---------------------------------------------------------------------*
CLASS lcl_execute IMPLEMENTATION.
* execute method for update one time customer & purchased products
METHOD execute.
NEW lcl_po( )->start_execution( ).
ENDMETHOD.
ENDCLASS.
CLASS lcl_po IMPLEMENTATION.
* Start of Execution
METHOD start_execution.
* Internal Tables
DATA :
lt_poheader TYPE gy_t_poheader,
lt_poitem TYPE gy_t_poitem,
lt_seritem TYPE gy_t_seritem,
lt_display_log TYPE gy_t_display_log,
lt_coas TYPE gy_t_coas.
**********************************************************************
* Fetch data
me->fetch_data(
EXPORTING
iv_file = p_file
IMPORTING
et_poheader = lt_poheader
et_poitem = lt_poitem
et_seritem = lt_seritem
et_coas = lt_coas
).
* process data
me->process_data(
EXPORTING
it_poheader = lt_poheader
it_poitem = lt_poitem
it_seritem = lt_seritem
it_coas = lt_coas
IMPORTING
et_display_log = lt_display_log
).
* Display data ALV
me->display_alv(
CHANGING
ct_display_log = lt_display_log
).
ENDMETHOD.
* Fetch Data
METHOD fetch_data.
DATA:
lrt_aufnr TYPE RANGE OF aufnr.
** Tables Declaration
DATA :
lt_file TYPE STANDARD TABLE OF zalsmex_tab,
lt_sheet1 TYPE STANDARD TABLE OF zalsmex_tab,
lt_sheet2 TYPE STANDARD TABLE OF zalsmex_tab,
lt_sheet3 TYPE STANDARD TABLE OF zalsmex_tab.
** Structures Declaration
DATA :
ls_poheader TYPE gy_s_poheader,
ls_poitem TYPE gy_s_poitem,
ls_seritem TYPE gy_s_seritem.
* Variables Declaration
DATA :
lv_file TYPE localfile,
lv_row TYPE kcd_ex_row_n.
** Field-Symbols
FIELD-SYMBOLS :
<lfs_value> TYPE any,
<lfs_file> LIKE LINE OF lt_file.
**********************************************************************
CLEAR: et_poitem[], et_seritem[].
lv_file = p_file.
** Fetch data from Excel file
CALL FUNCTION 'ZALSM_EXCEL_TO_INTERNAL_TAB'
EXPORTING
filename = lv_file
i_begin_col = 1
i_begin_row = 2
i_end_col = 15
i_end_row = 65536
sheets = 3
TABLES
it_data = lt_file
EXCEPTIONS
inconsistent_parameters = 1
upload_ole = 2
OTHERS = 3.
IF sy-subrc EQ 0.
SORT lt_file BY sheetno p_row.
lt_sheet1 = VALUE #( FOR ls_file1 IN lt_file
WHERE ( sheetno = 1 )
(
sheetno = ls_file1-sheetno
p_row = ls_file1-p_row
p_col = ls_file1-p_col
value = ls_file1-value
) ).
lt_sheet2 = VALUE #( FOR ls_file2 IN lt_file
WHERE ( sheetno = 2 )
(
sheetno = ls_file2-sheetno
p_row = ls_file2-p_row
p_col = ls_file2-p_col
value = ls_file2-value
) ).
lt_sheet3 = VALUE #( FOR ls_file3 IN lt_file
WHERE ( sheetno = 3 )
(
sheetno = ls_file3-sheetno
p_row = ls_file3-p_row
p_col = ls_file3-p_col
value = ls_file3-value
) ).
ENDIF.
** Fill Data into Table based on sheets in excel
*** Sheet 1 - Header
IF line_exists( lt_sheet1[ 1 ] ).
lv_row = lt_sheet1[ 1 ]-p_row.
ENDIF.
LOOP AT lt_sheet1 ASSIGNING <lfs_file>.
IF lv_row NE <lfs_file>-p_row.
APPEND ls_poheader TO et_poheader.
CLEAR : ls_poheader, lv_row.
lv_row = <lfs_file>-p_row.
ENDIF.
ASSIGN COMPONENT <lfs_file>-p_col OF STRUCTURE ls_poheader TO <lfs_value>.
IF sy-subrc EQ 0.
<lfs_value> = <lfs_file>-value.
ENDIF.
ENDLOOP.
IF sy-subrc EQ 0.
APPEND ls_poheader TO et_poheader.
ENDIF.
UNASSIGN <lfs_file>.
CLEAR:lv_row.
*** Sheet 2 - PO Item
IF line_exists( lt_sheet2[ 1 ] ).
lv_row = lt_sheet2[ 1 ]-p_row.
ENDIF.
LOOP AT lt_sheet2 ASSIGNING <lfs_file>.
IF lv_row NE <lfs_file>-p_row.
APPEND ls_poitem TO et_poitem.
CLEAR : ls_poitem, lv_row.
lv_row = <lfs_file>-p_row.
ENDIF.
ASSIGN COMPONENT <lfs_file>-p_col OF STRUCTURE ls_poitem TO <lfs_value>.
IF sy-subrc EQ 0.
<lfs_value> = <lfs_file>-value.
ENDIF.
ENDLOOP.
IF sy-subrc EQ 0.
APPEND ls_poitem TO et_poitem.
ENDIF.
UNASSIGN <lfs_file>.
CLEAR:lv_row.
*** Sheet 3 - PO Service Item
IF line_exists( lt_sheet3[ 1 ] ).
lv_row = lt_sheet3[ 1 ]-p_row.
ENDIF.
LOOP AT lt_sheet3 ASSIGNING <lfs_file>.
IF lv_row NE <lfs_file>-p_row.
APPEND ls_seritem TO et_seritem.
CLEAR : ls_seritem, lv_row.
lv_row = <lfs_file>-p_row.
ENDIF.
ASSIGN COMPONENT <lfs_file>-p_col OF STRUCTURE ls_seritem TO <lfs_value>.
IF sy-subrc EQ 0.
<lfs_value> = <lfs_file>-value.
ENDIF.
ENDLOOP.
IF sy-subrc EQ 0.
APPEND ls_seritem TO et_seritem.
ENDIF.
IF et_seritem IS NOT INITIAL.
lrt_aufnr = VALUE #( FOR ls_sitem IN et_seritem
( sign = 'I'
option = 'EQ'
low = |{ ls_sitem-aufnr ALPHA = IN }|
)
).
SELECT aufnr,
prctr
FROM coas
INTO TABLE @et_coas
WHERE aufnr IN @lrt_aufnr.
IF sy-subrc EQ 0.
SORT et_coas BY aufnr.
ENDIF.
ENDIF.
ENDMETHOD.
* Process Data
METHOD process_data.
DATA:
lt_return TYPE STANDARD TABLE OF bapiret2,
lt_poitem TYPE STANDARD TABLE OF bapimepoitem,
lt_poitemx TYPE STANDARD TABLE OF bapimepoitemx,
lt_poaccount TYPE STANDARD TABLE OF bapimepoaccount,
lt_poaccountx TYPE STANDARD TABLE OF bapimepoaccountx,
lt_poservices TYPE STANDARD TABLE OF bapiesllc,
lt_posrvaccessvalues TYPE STANDARD TABLE OF bapiesklc.
DATA:
ls_poheader TYPE bapimepoheader,
ls_poheaderx TYPE bapimepoheaderx,
ls_poitem TYPE bapimepoitem,
ls_poitemx TYPE bapimepoitemx,
ls_poaccount TYPE bapimepoaccount,
ls_poaccountx TYPE bapimepoaccountx,
ls_posrvaccessvalues TYPE bapiesklc,
ls_output TYPE gy_s_display_log.
DATA:
lv_ebeln TYPE ebeln,
lv_ebelp TYPE ebelp,
lv_pckg_no TYPE packno,
lv_subpckg_no TYPE packno,
lv_serial_no TYPE dzekkn.
CONSTANTS:
lc_aed TYPE waers VALUE 'AED',
lc_pckg_no TYPE packno VALUE '0000000001'.
**********************************************************************
lv_pckg_no = lc_pckg_no.
IF it_poheader IS NOT INITIAL.
READ TABLE it_poheader ASSIGNING FIELD-SYMBOL(<lfs_poheader>) INDEX 1.
IF sy-subrc EQ 0.
* Purchase Order Header Data
ls_poheader-comp_code = <lfs_poheader>-bukrs.
ls_poheader-doc_type = <lfs_poheader>-bsart.
ls_poheader-vendor = |{ <lfs_poheader>-vendor ALPHA = IN }|.
ls_poheader-purch_org = <lfs_poheader>-ekorg.
ls_poheader-pur_group = <lfs_poheader>-ekgrp.
ls_poheader-currency = lc_aed.
ls_poheader-creat_date = sy-datum.
ls_poheader-doc_date = sy-datum.
ls_poheader-created_by = sy-uname.
* Purchase Order Header Data (Change Parameter)
ls_poheaderx-po_number = abap_true.
ls_poheaderx-comp_code = abap_true.
ls_poheaderx-doc_type = abap_true.
ls_poheaderx-vendor = abap_true.
ls_poheaderx-purch_org = abap_true.
ls_poheaderx-pur_group = abap_true.
ls_poheaderx-currency = abap_true.
ls_poheaderx-creat_date = abap_true.
ls_poheaderx-doc_date = abap_true.
ls_poheaderx-created_by = abap_true.
ENDIF.
ENDIF.
LOOP AT it_poitem ASSIGNING FIELD-SYMBOL(<lfs_poitem>).
ls_poitem-po_item = <lfs_poitem>-ebelp.
ls_poitem-acctasscat = <lfs_poitem>-knttp.
ls_poitem-item_cat = <lfs_poitem>-epstp.
ls_poitem-material = <lfs_poitem>-matnr.
ls_poitem-short_text = <lfs_poitem>-txz01.
ls_poitem-quantity = <lfs_poitem>-bstmg.
ls_poitem-po_unit = <lfs_poitem>-bstme.
ls_poitem-matl_group = <lfs_poitem>-matkl.
ls_poitem-plant = <lfs_poitem>-werks.
ls_poitem-pckg_no = lv_pckg_no.
IF <lfs_poitem>-matnr IS NOT INITIAL.
ls_poitemx-material = abap_true.
ENDIF.
IF <lfs_poitem>-knttp IS NOT INITIAL.
ls_poitemx-acctasscat = abap_true.
ENDIF.
ls_poitemx-item_cat = abap_true.
ls_poitemx-po_item = <lfs_poitem>-ebelp.
ls_poitemx-po_itemx = abap_true.
ls_poitemx-short_text = abap_true.
ls_poitemx-quantity = abap_true.
ls_poitemx-po_unit = abap_true.
ls_poitemx-matl_group = abap_true.
ls_poitemx-plant = abap_true.
ls_poitemx-pckg_no = abap_true.
APPEND: ls_poitem TO lt_poitem,
ls_poitemx TO lt_poitemx.
IF lt_poservices IS INITIAL.
lv_subpckg_no = lv_pckg_no + 1.
APPEND VALUE #(
pckg_no = lv_pckg_no
line_no = lv_pckg_no
outl_level = 0
outl_ind = abap_true
subpckg_no = lv_subpckg_no ) TO lt_poservices.
ENDIF.
LOOP AT it_seritem ASSIGNING FIELD-SYMBOL(<lfs_seritem>)
WHERE extrow = <lfs_poitem>-ebelp.
lv_pckg_no = lv_pckg_no + 1.
lv_ebelp = lv_ebelp + <lfs_poitem>-ebelp.
lv_serial_no = lv_serial_no + 1.
ls_poaccount-po_item = <lfs_poitem>-ebelp."lv_ebelp.
ls_poaccount-serial_no = lv_serial_no."'01'.
ls_poaccount-gl_account = |{ <lfs_seritem>-saknr ALPHA = IN }|.
ls_poaccountx-po_item = <lfs_poitem>-ebelp."lv_ebelp.
ls_poaccountx-serial_no = lv_serial_no."'01'.
ls_poaccountx-po_itemx = abap_true.
ls_poaccountx-serial_nox = abap_true.
ls_poaccountx-gl_account = abap_true.
IF <lfs_poitem>-knttp EQ 'P' OR
<lfs_poitem>-knttp EQ 'Q'.
ls_poaccount-wbs_element = <lfs_seritem>-aufnr.
ls_poaccountx-wbs_element = abap_true.
ELSE.
ls_poaccount-orderid = |{ <lfs_seritem>-aufnr ALPHA = IN }|.
ls_poaccount-costcenter = VALUE #( it_coas[ aufnr = ls_poaccount-orderid
]-prctr OPTIONAL ).
ls_poaccountx-costcenter = abap_true.
ls_poaccountx-orderid = abap_true.
ENDIF.
APPEND:ls_poaccount TO lt_poaccount,
ls_poaccountx TO lt_poaccountx.
APPEND VALUE #(
pckg_no = lv_subpckg_no
line_no = lv_pckg_no
ext_line = lv_ebelp
outl_level = 0
service = |{ <lfs_seritem>-asnum ALPHA = IN }|
short_text = <lfs_seritem>-sh_text1
quantity = <lfs_seritem>-mengev
base_uom = <lfs_seritem>-meins
gr_price = <lfs_seritem>-sbrtwr
ext_serv = <lfs_seritem>-ext_serv ) TO lt_poservices.
APPEND VALUE #(
pckg_no = lv_subpckg_no
line_no = lv_pckg_no
serno_line = 01
percentage = '100'
serial_no = lv_serial_no ) TO lt_posrvaccessvalues.
ENDLOOP.
ENDLOOP.
CALL FUNCTION 'BAPI_PO_CREATE1'
EXPORTING
poheader = ls_poheader
poheaderx = ls_poheaderx
IMPORTING
exppurchaseorder = lv_ebeln
TABLES
return = lt_return
poitem = lt_poitem
poitemx = lt_poitemx
poaccount = lt_poaccount
poaccountx = lt_poaccountx
poservices = lt_poservices
posrvaccessvalues = lt_posrvaccessvalues.
IF NOT lv_ebeln IS INITIAL.
CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'
EXPORTING
wait = abap_true.
APPEND VALUE #( ebeln = lv_ebeln
msgtyp = 'S'
messg = 'PO Created' ) TO et_display_log.
ELSE.
LOOP AT lt_return ASSIGNING FIELD-SYMBOL(<lfs_return>)
WHERE type EQ 'E'.
ls_output-msgtyp = <lfs_return>-type.
ls_output-messg = <lfs_return>-message.
APPEND ls_output TO et_display_log.
CLEAR: ls_output.
ENDLOOP.
ENDIF.
ENDMETHOD.
* Display data ALV
METHOD display_alv.
CALL FUNCTION 'ZC4_DISPLAY_ALV'
CHANGING
ct_table = ct_display_log.
ENDMETHOD.
ENDCLASS.