***********************************************************************
* Title : ZFIR101 *
* Application : FI   *
* Subject : 利润中心会计与总账合并报表汇率差异调整 *
* Requested by : 财务部 *
* Execution : any time *
* Ref no: : *
* Author : RainyCai *
* Req Date : 20150901 *
***********************************************************************
* Differences between the exchange rate adjustment *
***********************************************************************
***********************************************************************
*1.取数:按照 年度,月度,公司代码=3000,利润中心不等于 COMMON DUMMY 汇总
* 查询GLPCT表中的利润中心,会计科目,
* 以公司代码货币计算金额GLPCT-HSL01,以利润中心本币计算金额GLPCT-KSL01
*2.输出:年 月 利润中心 科目 本位币金额 利润中心金额 汇差调整金额 *
*3.确认金额后,点过账,产生利润中心凭证.
* *
*3.Que:1.月度对应字段值 1月->HSL01 /KSL01, *
* 2.每月仅调整一次,利润中心凭证允许冲销, *
* 3.找BAPI创建利润中心凭证, *
* 4.差额 = 本位币金额(2) * 汇率(4) - 集团币(4),小数位导致尾差
* 0.01 = 0.02 * 1.27872 - 0.02
***********************************************************************
* MODIFICATIONS (latest entry at the top) *
* ------------------------------------------------------------------- *
* REL DATE NAME (COMPANY) DESCRIPTION TASK-NO *
* --- ---- ---- --------- ----------- ------- *
* *
***********************************************************************
REPORT ZFIR101 MESSAGE-ID ZMM_001.



TYPE-POOLS: SLIS.
*TYPE-POOLS: vrm.
*----------------------------------------------------------------------
* Tables
*----------------------------------------------------------------------
TABLES:GLPCT,"EC-PCA: 总表
GLPCA."成本控制-利润中心会计:实际行项目

TYPES:BEGIN OF TY_TAB,
RYEAR TYPE GLPCT-RYEAR,
RPMAX TYPE GLPCT-RPMAX,
RPRCTR TYPE GLPCT-RPRCTR,"利润中心
RACCT TYPE GLPCT-RACCT, "科目

HSL00 TYPE GLPCT-HSL01,
HSL01 TYPE GLPCT-HSL01,
HSL02 TYPE GLPCT-HSL01,
HSL03 TYPE GLPCT-HSL01,
HSL04 TYPE GLPCT-HSL01,
HSL05 TYPE GLPCT-HSL01,
HSL06 TYPE GLPCT-HSL01,
HSL07 TYPE GLPCT-HSL01,
HSL08 TYPE GLPCT-HSL01,
HSL09 TYPE GLPCT-HSL01,
HSL10 TYPE GLPCT-HSL01,
HSL11 TYPE GLPCT-HSL01,
HSL12 TYPE GLPCT-HSL01,
KSL00 TYPE GLPCT-HSL01,
KSL01 TYPE GLPCT-HSL01,
KSL02 TYPE GLPCT-HSL01,
KSL03 TYPE GLPCT-HSL01,
KSL04 TYPE GLPCT-HSL01,
KSL05 TYPE GLPCT-HSL01,
KSL06 TYPE GLPCT-HSL01,
KSL07 TYPE GLPCT-HSL01,
KSL08 TYPE GLPCT-HSL01,
KSL09 TYPE GLPCT-HSL01,
KSL10 TYPE GLPCT-HSL01,
KSL11 TYPE GLPCT-HSL01,
KSL12 TYPE GLPCT-HSL01,

KURSF LIKE BKPF-KURSF, "汇率
ZCHAE TYPE GLPCT-HSL01,
SEL TYPE C,
END OF TY_TAB.

DATA:GT_TAB TYPE STANDARD TABLE OF TY_TAB,
GT_TAB_USE TYPE STANDARD TABLE OF TY_TAB,
GT_TAB_ALV TYPE STANDARD TABLE OF TY_TAB,
WA_TAB TYPE TY_TAB.
DATA:G_LINES TYPE STRING.

DATA:BEGIN OF GT_END OCCURS 0,
RPRCTR TYPE GLPCT-RPRCTR,"利润中心
RACCT TYPE GLPCT-RACCT, "科目
ZCHAE TYPE GLPCT-HSL01,
END OF GT_END.

*&------------------------------------------------------------------*
*& Global Variants Definition
*&------------------------------------------------------------------*
FIELD-SYMBOLS: <FS_TAB> LIKE LINE OF GT_TAB.
DATA:GT_EXCLUDE TYPE UI_FUNCTIONS,
CONTAINER1 TYPE REF TO CL_GUI_CONTAINER,
CONTAINER2 TYPE REF TO CL_GUI_CONTAINER,
SPLITTER1 TYPE REF TO CL_GUI_SPLITTER_CONTAINER.

CLASS GC_EVENT_RECEIVER DEFINITION DEFERRED.
DATA: GCTR_ALV TYPE REF TO CL_GUI_CUSTOM_CONTAINER,
GCT_ALV TYPE REF TO CL_GUI_ALV_GRID,
G_EVENT_RECEIVER TYPE REF TO GC_EVENT_RECEIVER,
LS_ROW_NO_WA TYPE LVC_S_ROID,
LT_ROW_NO TYPE LVC_T_ROID,
LT_CHANGED_ROWS TYPE LVC_T_MODI,
WA_CHANGED_ROWS LIKE LINE OF LT_CHANGED_ROWS,
IT_EVENTS TYPE SLIS_T_EVENT,
WA_EVENTS LIKE LINE OF IT_EVENTS,
LR_GRID TYPE REF TO CL_GUI_ALV_GRID,
STBL TYPE LVC_S_STBL.
DATA:GT_FIELDCAT TYPE LVC_T_FCAT,
GS_LAYOUT TYPE LVC_S_LAYO,
OK_CODE TYPE SY-UCOMM.
*DATA:name1 TYPE vrm_id,
* list1 TYPE vrm_values,
* value1 LIKE LINE OF list1,
* name2 TYPE vrm_id,
* list2 TYPE vrm_values,
* value2 LIKE LINE OF list2,
* name3 TYPE vrm_id,
* list3 TYPE vrm_values,
* value3 LIKE LINE OF list3.
*&------------------------------------------------------------------*
*& BDC
*&------------------------------------------------------------------*
DATA:BDCDATA LIKE BDCDATA OCCURS 0 WITH HEADER LINE,
NODATA_CHARACTER VALUE '/',
NODATA VALUE '/'.
* NODATA DEFAULT '/' LOWER CASE,
* IT_MESSAGE LIKE BDCMSGCOLL OCCURS 0 WITH HEADER LINE.

*----------------------------------------------------------------------*
* CLASS lcl_event_receiver DEFINITION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS GC_EVENT_RECEIVER DEFINITION.
PUBLIC SECTION.
METHODS:
* HANDLE_DATA_CHANGED
* FOR EVENT DATA_CHANGED OF CL_GUI_ALV_GRID
* IMPORTING ER_DATA_CHANGED,
*&工具栏设置
HANDLE_TOOLBAR
FOR EVENT TOOLBAR OF CL_GUI_ALV_GRID
IMPORTING E_OBJECT E_INTERACTIVE,
*&用户事件
HANDLE_USER_COMMAND
FOR EVENT USER_COMMAND OF CL_GUI_ALV_GRID
IMPORTING E_UCOMM,
*&双击事件
HANDLE_DOUBLE_CLICK
FOR EVENT DOUBLE_CLICK OF CL_GUI_ALV_GRID
IMPORTING E_ROW E_COLUMN.

ENDCLASS. "GC_EVENT_RECEIVER DEFINITION
*----------------------------------------------------------------------*
* CLASS lcl_even_receiver IMPLEMENTATION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS GC_EVENT_RECEIVER IMPLEMENTATION.
*&功能H:设定增加自定义ALV工具栏的按钮
METHOD HANDLE_TOOLBAR.
DATA:LS_TOOLBAR TYPE STB_BUTTON.
LS_TOOLBAR-BUTN_TYPE = '3'.
APPEND LS_TOOLBAR TO E_OBJECT->MT_TOOLBAR.

* CLEAR ls_toolbar.
* ls_toolbar-function = 'ALL'.
* ls_toolbar-icon = icon_select_all.
* APPEND ls_toolbar TO e_object->mt_toolbar.
* CLEAR ls_toolbar.
* ls_toolbar-function = 'DALL'.
* ls_toolbar-icon = icon_deselect_all.
* APPEND ls_toolbar TO e_object->mt_toolbar.
* CLEAR LS_TOOLBAR.
* LS_TOOLBAR-FUNCTION = 'EXE'.
* LS_TOOLBAR-ICON = ICON_EXECUTE_OBJECT.
* APPEND LS_TOOLBAR TO E_OBJECT->MT_TOOLBAR.
ENDMETHOD. "HANDLE_TOOLBAR
*&功能J:设定响应USER_COMMAND
METHOD HANDLE_USER_COMMAND.
CASE E_UCOMM.
WHEN 'ALL'.
* PERFORM FRM_SELECT_ALL.
* PERFORM FRM_GRID_REFSH.
WHEN 'DALL'.
* PERFORM FRM_DESELECT_ALL.
* PERFORM FRM_GRID_REFSH.
* WHEN 'EXE'.
* PERFORM FRM_UPDATE_TABLE.
* PERFORM FRM_GRID_REFSH.
* WHEN 'INST'."收货入库
* PERFORM FRM_INSTORE_ORDER.
* WHEN 'OUST'."发货出库
* PERFORM FRM_OUTSTORE_ORDER.
ENDCASE.
ENDMETHOD. "HANDLE_USER_COMMAND
*&功能I:设定响应双击
METHOD HANDLE_DOUBLE_CLICK.
* READ TABLE GT_TAB_ALV INTO WA_TAB INDEX E_ROW-INDEX.
* IF E_COLUMN-FIELDNAME EQ 'VBELN'.
**&CALL ME23N
* IF SY-SUBRC EQ 0.
* SET PARAMETER ID 'VF' FIELD WA_TAB-VBELN.
* CALL TRANSACTION 'VF03' AND SKIP FIRST SCREEN.
** PERFORM frm_call_me23n USING wa_output-ebeln.
* ENDIF.
* ENDIF.

ENDMETHOD. "HANDLE_DOUBLE_CLICK

ENDCLASS. "handle_modify

************************************************************************
* Select Screen *
************************************************************************
SELECTION-SCREEN BEGIN OF BLOCK B1 WITH FRAME TITLE TEXT-001.

PARAMETER:P_RYEAR LIKE GLPCT-RYEAR DEFAULT SY-DATUM(4) OBLIGATORY,
P_MONTH LIKE GLPCT-RPMAX OBLIGATORY."DEFAULT SY-DATUM+4(2)

PARAMETER:P_RBUKRS LIKE GLPCT-RBUKRS DEFAULT '3000' NO-DISPLAY.

SELECTION-SCREEN END OF BLOCK B1.

*SELECTION-SCREEN BEGIN OF BLOCK B2 WITH FRAME TITLE TEXT-002 .
*PARAMETERS:RB_01 RADIOBUTTON GROUP G1 DEFAULT 'X',
* RB_02 RADIOBUTTON GROUP G1.
*SELECTION-SCREEN END OF BLOCK B2.



*&------------------------------------------------------------------*
*& INITIALIZATION
*&------------------------------------------------------------------*
INITIALIZATION.
*&I1.输入初始化
PERFORM FRM_INIT_DATA.

*&------------------------------------------------------------------*
*& AT SELECTION-SCREEN
*&------------------------------------------------------------------*

AT SELECTION-SCREEN.
*&I2.权限检查
PERFORM FRM_AUTHORITY_CHECK.

*&------------------------------------------------------------------*
*& START-OF-SELECTION
*&------------------------------------------------------------------*

START-OF-SELECTION.

PERFORM FRM_GET_DATA.

**&S2.显示数据
CALL SCREEN 100.

*&---------------------------------------------------------------------*
*& Form FRM_INIT_DATA
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
FORM FRM_INIT_DATA .
*&月份默认当前上一月
P_MONTH = SY-DATUM+4(2) - 1.

ENDFORM. " FRM_INIT_DATA
*&---------------------------------------------------------------------*
*& Form FRM_AUTHORITY_CHECK
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
FORM FRM_AUTHORITY_CHECK .

ENDFORM. " FRM_AUTHORITY_CHECK
*&---------------------------------------------------------------------*
*& Form FRM_GET_DATA
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
FORM FRM_GET_DATA .
DATA:L_DATE TYPE D.
DATA:RT_RATE LIKE BAPI1093_0,
L_RATE LIKE BKPF-KURSF."汇率

*&S1.BAPI取汇率(汇率时间取选择年+月+01)
*& OB08取当月的汇率类型=GL,RMB:HKD的汇率
CLEAR:L_DATE,L_RATE.
CONCATENATE P_RYEAR P_MONTH+1(2) '01' INTO L_DATE.
*& YYYYMMDD-> DDMMYYYY
CALL FUNCTION 'CONVERT_DATE_FORMAT'
EXPORTING
I_DATE = L_DATE
IMPORTING
E_CALC_DATE = L_DATE.
*& 时间需要转换 CONVERSION_EXIT_INVDT_INPUT(转换退出 INVDT)
CALL FUNCTION 'CONVERSION_EXIT_INVDT_INPUT'
EXPORTING
INPUT = L_DATE
IMPORTING
OUTPUT = L_DATE.

CALL FUNCTION 'BAPI_EXCHANGERATE_GETDETAIL'
EXPORTING
RATE_TYPE = 'GL'
FROM_CURR = 'RMB' "GT_OUTPUT01-WAERS
TO_CURRNCY = 'HKD'
DATE = L_DATE
IMPORTING
EXCH_RATE = RT_RATE.
IF SY-SUBRC EQ 0.
L_RATE = RT_RATE-EXCH_RATE.
ELSE.
MESSAGE E001 WITH '所选月汇率未维护,请先维护汇率!'.
ENDIF.

*&查找存在汇率差异数据
SELECT *
INTO CORRESPONDING FIELDS OF TABLE GT_TAB
FROM GLPCT
WHERE RYEAR EQ P_RYEAR
AND RBUKRS EQ P_RBUKRS
AND RPRCTR NOT IN ('COMMON', 'DUMMY')."不等于这两个值是否能实现

IF SY-SUBRC NE 0.
MESSAGE S000(OO) WITH '没有找到符合条件的数据!' DISPLAY LIKE 'E'.
LEAVE TO LIST-PROCESSING.
ENDIF.

*&S3.处理数据
LOOP AT GT_TAB INTO WA_TAB.

*&L1.月份 = 期间
WA_TAB-RPMAX = P_MONTH.

*&L2.取得对应期间的金额
*&公司代码货币金额/ 利润中心货币金额
CASE P_MONTH.
WHEN '001'.
WA_TAB-HSL00 = WA_TAB-HSL01.
WA_TAB-KSL00 = WA_TAB-KSL01.
WHEN '002'.
WA_TAB-HSL00 = WA_TAB-HSL02.
WA_TAB-KSL00 = WA_TAB-KSL02.
WHEN '003'.
WA_TAB-HSL00 = WA_TAB-HSL03.
WA_TAB-KSL00 = WA_TAB-KSL03.
WHEN '004'.
WA_TAB-HSL00 = WA_TAB-HSL04.
WA_TAB-KSL00 = WA_TAB-KSL04.
WHEN '005'.
WA_TAB-HSL00 = WA_TAB-HSL05.
WA_TAB-KSL00 = WA_TAB-KSL05.
WHEN '006'.
WA_TAB-HSL00 = WA_TAB-HSL06.
WA_TAB-KSL00 = WA_TAB-KSL06.
WHEN '007'.
WA_TAB-HSL00 = WA_TAB-HSL07.
WA_TAB-KSL00 = WA_TAB-KSL07.
WHEN '008'.
WA_TAB-HSL00 = WA_TAB-HSL08.
WA_TAB-KSL00 = WA_TAB-KSL08.
WHEN '009'.
WA_TAB-HSL00 = WA_TAB-HSL09.
WA_TAB-KSL00 = WA_TAB-KSL09.
WHEN '010'.
WA_TAB-HSL00 = WA_TAB-HSL10.
WA_TAB-KSL00 = WA_TAB-KSL10.
WHEN '011'.
WA_TAB-HSL00 = WA_TAB-HSL11.
WA_TAB-KSL00 = WA_TAB-KSL11.
WHEN '012'.
WA_TAB-HSL00 = WA_TAB-HSL12.
WA_TAB-KSL00 = WA_TAB-KSL12.
ENDCASE.

*&排除本位币 和 集团币都为0
IF WA_TAB-HSL00 IS NOT INITIAL OR WA_TAB-KSL00 IS NOT INITIAL.
*&L3.公式:差额= HSL00 * 汇率 - KSL00.
WA_TAB-ZCHAE = WA_TAB-HSL00 * L_RATE - WA_TAB-KSL00.
IF WA_TAB-ZCHAE IS NOT INITIAL.
COLLECT WA_TAB INTO GT_TAB_ALV.
ENDIF.
ENDIF.
ENDLOOP.

DELETE GT_TAB_ALV WHERE ZCHAE IS INITIAL.

*&W1.获取汇总金额
DATA:L_ZCHAE LIKE GLPCT-HSL01.

LOOP AT GT_TAB_ALV INTO WA_TAB.
AT LAST.
*&汇总 WA_TAB-ZCHAE.
SUM.
MOVE WA_TAB-ZCHAE TO L_ZCHAE..
ENDAT.
* AT LAST.
* SUM.
* WRITE: / 'Overall Sum',
* SFLIGHT_WA-SEATSOCC UNDER SFLIGHT_WA-SEATSOCC.
* ENDAT.
*&W2.获取汇总金额 GT_END.
GT_END-RPRCTR = 'DUMMY'.
GT_END-RACCT = 'PCA001'.
MOVE WA_TAB-ZCHAE TO GT_END-ZCHAE.
COLLECT GT_END.
ENDLOOP.

G_LINES = LINES( GT_TAB_ALV[] ).
IF G_LINES EQ 0.
MESSAGE S000(OO) WITH '没有找到符合条件的数据!' DISPLAY LIKE 'E'.
LEAVE TO LIST-PROCESSING.
ELSE.
MESSAGE S000(OO) WITH '找到数据' G_LINES '条!'.
ENDIF.

ENDFORM. " FRM_GET_DATA

*&---------------------------------------------------------------------*
*& Form FRM_SELECT_ALL
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
FORM FRM_SELECT_ALL .
LOOP AT GT_TAB_ALV ASSIGNING <FS_TAB> WHERE SEL = SPACE.
WA_TAB-SEL = 'X'.
ENDLOOP.
ENDFORM. " FRM_SELECT_ALL
*&---------------------------------------------------------------------*
*& Form FRM_GRID_REFSH
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
FORM FRM_GRID_REFSH .

CALL METHOD GCT_ALV->REFRESH_TABLE_DISPLAY
EXPORTING
IS_STABLE = STBL.
* CALL METHOD gct_alv->refresh_fieldcat_display
* EXPORTING
* is_stable = stbl.

ENDFORM. " FRM_GRID_REFSH
*&---------------------------------------------------------------------*
*& Form FRM_DESELECT_ALL
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
FORM FRM_DESELECT_ALL .
LOOP AT GT_TAB_ALV ASSIGNING <FS_TAB> WHERE SEL = 'X'.
WA_TAB-SEL = SPACE.
ENDLOOP.
ENDFORM. " FRM_DESELECT_ALL

*&---------------------------------------------------------------------*
*& Module STATUS_9000 OUTPUT
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
MODULE STATUS_9000 OUTPUT.
DATA:TITLE TYPE LVC_TITLE.

SET PF-STATUS '0100'.
SET TITLEBAR '010'.

CONCATENATE '共输出' G_LINES '条符合条件的数据!' INTO TITLE.

*&1.Fieldcat 设置
PERFORM FRM_DISPLAY_ALV.
*&2.按钮设置
PERFORM EXCLUDE_TB_FUNCTIONS TABLES GT_EXCLUDE.

IF GCTR_ALV IS INITIAL.

CREATE OBJECT GCTR_ALV
EXPORTING
CONTAINER_NAME = 'GCTR_ALV'.

* CREATE OBJECT SPLITTER1
* EXPORTING
* PARENT = GCTR_ALV
* ROWS = 2
* COLUMNS = 1.
* CALL METHOD SPLITTER1->SET_BORDER
* EXPORTING
* BORDER = CL_GUI_CFW=>FALSE.
* CALL METHOD SPLITTER1->SET_ROW_HEIGHT
* EXPORTING
* ID = 1
* HEIGHT = 100.
* CALL METHOD SPLITTER1->SET_VISIBLE
* EXPORTING
* VISIBLE = 'X'.
* CALL METHOD SPLITTER1->GET_CONTAINER
* EXPORTING
* ROW = 1
* COLUMN = 1
* RECEIVING
* CONTAINER = CONTAINER1.
* CALL METHOD SPLITTER1->GET_CONTAINER
* EXPORTING
* ROW = 2
* COLUMN = 1
* RECEIVING
* CONTAINER = CONTAINER2.

CREATE OBJECT GCT_ALV
EXPORTING
I_PARENT = CONTAINER1
I_APPLOGPARENT = CONTAINER2.

*& METHOD 显示ALV.
CALL METHOD GCT_ALV->SET_TABLE_FOR_FIRST_DISPLAY
EXPORTING
I_SAVE = 'A'
IS_LAYOUT = GS_LAYOUT
IT_TOOLBAR_EXCLUDING = GT_EXCLUDE
CHANGING
IT_OUTTAB = GT_TAB_ALV[]
IT_FIELDCATALOG = GT_FIELDCAT[].

CREATE OBJECT G_EVENT_RECEIVER.

CALL METHOD GCT_ALV->REGISTER_EDIT_EVENT
EXPORTING
I_EVENT_ID = CL_GUI_ALV_GRID=>MC_EVT_MODIFIED.

* SET HANDLER G_EVENT_RECEIVER->HANDLE_MODIFY FOR GCT_ALV.
* SET HANDLER G_EVENT_RECEIVER->HANDLE_DATA_CHANGED FOR GCT_ALV.
* SET HANDLER G_EVENT_RECEIVER->HANDLE_DATA_CHANGED FOR GCT_ALV.
SET HANDLER G_EVENT_RECEIVER->HANDLE_USER_COMMAND FOR GCT_ALV.
SET HANDLER G_EVENT_RECEIVER->HANDLE_DOUBLE_CLICK FOR GCT_ALV.
SET HANDLER G_EVENT_RECEIVER->HANDLE_TOOLBAR FOR GCT_ALV.
CALL METHOD GCT_ALV->SET_TOOLBAR_INTERACTIVE.
CALL METHOD GCT_ALV->REGISTER_EDIT_EVENT
EXPORTING
I_EVENT_ID = CL_GUI_ALV_GRID=>MC_EVT_MODIFIED.
CALL METHOD CL_GUI_CONTROL=>SET_FOCUS "设置焦点在REF_ALVG1上
EXPORTING
CONTROL = GCT_ALV.
ELSE.
CALL METHOD GCT_ALV->CHECK_CHANGED_DATA.
CALL METHOD GCT_ALV->REFRESH_TABLE_DISPLAY.
* CALL METHOD G_EVENT_RECEIVER->HANDLE_MODIFY.
ENDIF.
PERFORM FRM_GRID_REFSH.

ENDMODULE. " STATUS_9000 OUTPUT
*&---------------------------------------------------------------------*
*& Form FRM_DISPLAY_ALV
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
FORM FRM_DISPLAY_ALV .

*&设置需要输出的字段
PERFORM FRM_DISPLAY_FIELDCAT.

*&显示设置
GS_LAYOUT-CWIDTH_OPT = 'A'.
GS_LAYOUT-ZEBRA = 'X'.
* GS_LAYOUT-BOX_FNAME = 'SEL'.
GS_LAYOUT-NO_ROWMARK = 'X'.
GS_LAYOUT-SEL_MODE = 'D'.
GS_LAYOUT-STYLEFNAME = 'CELLTAB'.
GS_LAYOUT-GRID_TITLE = TITLE.
*GS_LAYOUT-DISPLAY = 'X'.

ENDFORM. " FRM_DISPLAY_ALV
*&---------------------------------------------------------------------*
*& Form FRM_DISPLAY_FIELDCAT
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
FORM FRM_DISPLAY_FIELDCAT .
DATA:LS_FIELDCAT TYPE LVC_S_FCAT.

* LS_FIELDCAT-FIELDNAME = 'SEL' .
* LS_FIELDCAT-INTTYPE = 'C' .
* LS_FIELDCAT-OUTPUTLEN = '1' .
* LS_FIELDCAT-COLTEXT = 'SEL' .
* LS_FIELDCAT-SELTEXT = 'SEL' .
* LS_FIELDCAT-CHECKBOX = 'X' .
* LS_FIELDCAT-EDIT = 'X' .
* APPEND LS_FIELDCAT TO GT_FIELDCAT .
* CLEAR LS_FIELDCAT.

LS_FIELDCAT-FIELDNAME = 'RYEAR' .
LS_FIELDCAT-INTTYPE = 'C' .
LS_FIELDCAT-OUTPUTLEN = '5'.
LS_FIELDCAT-COLTEXT = '年'.
LS_FIELDCAT-SELTEXT = '年'.
APPEND LS_FIELDCAT TO GT_FIELDCAT .
CLEAR LS_FIELDCAT.

LS_FIELDCAT-FIELDNAME = 'RPMAX' .
LS_FIELDCAT-INTTYPE = 'C' .
LS_FIELDCAT-OUTPUTLEN = '4' .
LS_FIELDCAT-NO_ZERO = 'X' .
LS_FIELDCAT-COLTEXT = '月' .
LS_FIELDCAT-SELTEXT = '月' .
APPEND LS_FIELDCAT TO GT_FIELDCAT .
CLEAR LS_FIELDCAT.

LS_FIELDCAT-FIELDNAME = 'RPRCTR' .
LS_FIELDCAT-INTTYPE = 'C' .
LS_FIELDCAT-OUTPUTLEN = '10' .
LS_FIELDCAT-COLTEXT = '利润中心' .
LS_FIELDCAT-SELTEXT = '利润中心' .
APPEND LS_FIELDCAT TO GT_FIELDCAT .
CLEAR LS_FIELDCAT.

LS_FIELDCAT-FIELDNAME = 'RACCT' .
LS_FIELDCAT-INTTYPE = 'C' .
LS_FIELDCAT-OUTPUTLEN = '10' .
LS_FIELDCAT-NO_ZERO = 'X' .
LS_FIELDCAT-COLTEXT = '科目' .
LS_FIELDCAT-SELTEXT = '科目' .
APPEND LS_FIELDCAT TO GT_FIELDCAT .
CLEAR LS_FIELDCAT.


LS_FIELDCAT-FIELDNAME = 'HSL00' .
LS_FIELDCAT-INTTYPE = 'CURR' .
LS_FIELDCAT-OUTPUTLEN = '17' .
LS_FIELDCAT-REF_TABLE = 'GLPCT'.
LS_FIELDCAT-REF_FIELD = 'HSL01'.
LS_FIELDCAT-COLTEXT = '本位币金额' .
LS_FIELDCAT-SELTEXT = '本位币金额' .
APPEND LS_FIELDCAT TO GT_FIELDCAT .
CLEAR LS_FIELDCAT.

LS_FIELDCAT-FIELDNAME = 'KSL00' .
LS_FIELDCAT-INTTYPE = 'CURR' .
LS_FIELDCAT-OUTPUTLEN = '17' .
LS_FIELDCAT-REF_TABLE = 'GLPCT'.
LS_FIELDCAT-REF_FIELD = 'HSL01'.
LS_FIELDCAT-COLTEXT = '利润中心金额' .
LS_FIELDCAT-SELTEXT = '利润中心金额' .
APPEND LS_FIELDCAT TO GT_FIELDCAT .
CLEAR LS_FIELDCAT.

LS_FIELDCAT-FIELDNAME = 'ZCHAE' .
LS_FIELDCAT-INTTYPE = 'CURR' .
LS_FIELDCAT-OUTPUTLEN = '17' .
LS_FIELDCAT-REF_TABLE = 'GLPCT'.
LS_FIELDCAT-REF_FIELD = 'HSL01'.
LS_FIELDCAT-COLTEXT = '汇差调整金额' .
LS_FIELDCAT-SELTEXT = '汇差调整金额' .
APPEND LS_FIELDCAT TO GT_FIELDCAT .
CLEAR LS_FIELDCAT.



ENDFORM. " FRM_DISPLAY_FIELDCAT
*&---------------------------------------------------------------------*
*& Form EXCLUDE_TB_FUNCTIONS
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* -->P_GT_EXCLUDE text
*----------------------------------------------------------------------*
FORM EXCLUDE_TB_FUNCTIONS TABLES PT_EXCLUDE TYPE UI_FUNCTIONS.
DATA: LS_EXCLUDE TYPE UI_FUNC.

LS_EXCLUDE = CL_GUI_ALV_GRID=>MC_FC_LOC_DELETE_ROW.
APPEND LS_EXCLUDE TO PT_EXCLUDE.
LS_EXCLUDE = CL_GUI_ALV_GRID=>MC_FC_LOC_INSERT_ROW.
APPEND LS_EXCLUDE TO PT_EXCLUDE.
LS_EXCLUDE = CL_GUI_ALV_GRID=>MC_FC_LOC_CUT.
APPEND LS_EXCLUDE TO PT_EXCLUDE.
LS_EXCLUDE = CL_GUI_ALV_GRID=>MC_FC_LOC_APPEND_ROW.
APPEND LS_EXCLUDE TO PT_EXCLUDE.

ENDFORM. " EXCLUDE_TB_FUNCTIONS
*&---------------------------------------------------------------------*
*& Module USER_COMMAND_9000 INPUT
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
MODULE USER_COMMAND_9000 INPUT.
DATA: CODE_OK TYPE SY-UCOMM.

CLEAR CODE_OK.
CODE_OK = SY-UCOMM.
*&强制退出
CASE CODE_OK.
WHEN 'BACK' OR 'EXIT' OR 'CANCEL'.
LEAVE TO SCREEN 0.
WHEN OTHERS.
ENDCASE.

ENDMODULE. " USER_COMMAND_9000 INPUT
*&---------------------------------------------------------------------*
*& Module USER_COMMAND INPUT
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
MODULE USER_COMMAND INPUT.

CODE_OK = SY-UCOMM.

BREAK ABAP30.
CASE CODE_OK.
WHEN 'SAVE'.
*&数据保存自建表
PERFORM FRM_SAVE_DATA.

WHEN 'BACK' OR 'EXIT'.
LEAVE TO SCREEN 0.
WHEN OTHERS.
ENDCASE.
CLEAR CODE_OK.

ENDMODULE. " USER_COMMAND INPUT
*&---------------------------------------------------------------------*
*& Form FRM_SAVE_DATA
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
FORM FRM_SAVE_DATA .
DATA:L_BUDAT TYPE SY-DATUM,"过帐日期
L_DAT TYPE SY-DATUM.
DATA:L_ZCHAE TYPE CHAR40.
DATA:LS_HEADDATA LIKE PROFITCENTER_HEAD,
LT_LINEDATA LIKE TABLE OF PROFITCENTER_DATA WITH HEADER LINE,
LT_MESSAGE LIKE TABLE OF BAPIRET2 WITH HEADER LINE,
LS_RETURN LIKE BAPIRET2.
DATA:LS_RETURN_BAPI LIKE BAPIRETURN.

CLEAR:L_BUDAT,L_DAT.
CONCATENATE P_RYEAR P_MONTH+1(2) '01' INTO L_BUDAT.
*&Get last day of month
* /DSD/PR_LAST_DAY_OF_MONTHS
CALL FUNCTION 'LAST_DAY_OF_MONTHS'
EXPORTING
DAY_IN = L_BUDAT
IMPORTING
LAST_DAY_OF_MONTH = L_DAT.
IF SY-SUBRC <> 0.
* Implement suitable error handling here
ENDIF.

*&S1.实现9KE0,创建利润中心凭证
*& 分录1:利润中心-SBU-A , 科目 65203501 金额100
*& 汇总分录: 利润中心DUMMY 科目 PCA001 金额100.00-(-汇总金额)

CLEAR:LS_HEADDATA,
LT_LINEDATA,LT_LINEDATA[],
LT_MESSAGE,LT_MESSAGE[],LS_RETURN.
*&S1.利润中心凭证HEAD
LS_HEADDATA-KURST = 'M'. "汇率类型
LS_HEADDATA-DOCUMENT_TYPE = 'A0'."FI-SL凭证类型
LS_HEADDATA-PSTNG_DATE = L_DAT. "凭证中的过帐日期
LS_HEADDATA-SPEC_PERIOD = ''. "特别期间/结转期间
LS_HEADDATA-COMP_CODE = '3000'. "公司代码
LS_HEADDATA-CURRENCY = 'HKD'. "货币码 obligatory
LS_HEADDATA-LOG_SYSTEM = ''. "逻辑系统

*&S2.line
LOOP AT GT_TAB_ALV INTO WA_TAB.

*&科目补0
*CALL FUNCTION 'CONVERSION_EXIT_ALPHA_INPUT'
* EXPORTING
* INPUT = WA_TAB-RACCT
* IMPORTING
* OUTPUT = WA_TAB-RACCT.
LT_LINEDATA-PROFIT_CTR = WA_TAB-RPRCTR."利润中心
LT_LINEDATA-GL_ACCOUNT = WA_TAB-RACCT. "帐号
LT_LINEDATA-IN_TC = WA_TAB-ZCHAE. "业务货币计价的金额
* LT_LINEDATA-PART_PRCTR = ''. "伙伴利润中心
* LT_LINEDATA-PLANT = ''. "工厂
* LT_LINEDATA-FUNC_AREA = '1000'. "功能范围
* LT_LINEDATA-TRADE_ID = ''. "贸易伙伴的公司标识
* LT_LINEDATA-S_COMP_CODE = ''. "发送者公司代码
* LT_LINEDATA-PARTNER_FUNC_AREA = ''. "伙伴功能范围
* LT_LINEDATA-CS_TRANS_T = '105'. "事务类型
* LT_LINEDATA-OBJECTCLASS = ''. "对象类
* LT_LINEDATA-REP_MATERIAL = ''. "利润中心记帐的代表性物料
* LT_LINEDATA-DEPR_AREA = ''. "实际折旧范围
APPEND LT_LINEDATA.

ENDLOOP.

READ TABLE GT_END INDEX 1.
IF SY-SUBRC EQ 0.
GT_END-ZCHAE = -1 * GT_END-ZCHAE.
ENDIF.
LT_LINEDATA-PROFIT_CTR = GT_END-RPRCTR."利润中心
LT_LINEDATA-GL_ACCOUNT = GT_END-RACCT. "帐号
LT_LINEDATA-IN_TC = GT_END-ZCHAE. "业务货币计价的金额
APPEND LT_LINEDATA.

*&BAPI-9KE0 创建利润中心凭证
CALL FUNCTION 'PROFITCENTER_DOCUMENT_POST'
EXPORTING
HEADDATA = LS_HEADDATA
IMPORTING
RETURN = LS_RETURN
TABLES
LINEDATA = LT_LINEDATA
LMESSAGE = LT_MESSAGE.

IF LS_RETURN-TYPE EQ 'E'.
ROLLBACK WORK.
ELSE.
COMMIT WORK.
ENDIF.
*&MESSAGE
CALL FUNCTION 'BALW_BAPIRETURN_GET'
EXPORTING
TYPE = LS_RETURN-TYPE
CL = LS_RETURN-ID
NUMBER = LS_RETURN-NUMBER
PAR1 = LS_RETURN-MESSAGE_V1
PAR2 = LS_RETURN-MESSAGE_V2
PAR3 = LS_RETURN-MESSAGE_V3
PAR4 = LS_RETURN-MESSAGE_V4
IMPORTING
BAPIRETURN = LS_RETURN_BAPI
EXCEPTIONS
OTHERS = 1.
MESSAGE ID LS_RETURN-ID
TYPE LS_RETURN-TYPE
NUMBER LS_RETURN-NUMBER
WITH LS_RETURN-MESSAGE_V1 LS_RETURN-MESSAGE_V2 LS_RETURN-MESSAGE_V3 LS_RETURN-MESSAGE_V4.

ENDFORM. " FRM_SAVE_DATA