由訂單建立收款憑證

*&---------------------------------------------------------------------*
*& Report ZFI_DOCU_IMPORT
*&
*&---------------------------------------------------------------------*
*&
*&
*&---------------------------------------------------------------------*測試

REPORT ZFIA04 NO STANDARD PAGE HEADING MESSAGE-ID 00.
TYPE-POOLS: SLIS,TRUXS,ICON.
TABLES: VBAK, T001,BKPF,TBSL,SSCRFIELDS.
DATA FUNCTXT TYPE SMP_DYNTXT.
*---------------------------------------------------------------------*
*ALV 使用到的類庫
*---------------------------------------------------------------------*
DATA: GT_FIELDCAT TYPE SLIS_T_FIELDCAT_ALV,
WA_FIELDCAT TYPE SLIS_FIELDCAT_ALV,
GS_LAYOUT TYPE SLIS_LAYOUT_ALV.excel

DATA: IT_SUBFIELDCAT TYPE SLIS_T_FIELDCAT_ALV.
DATA: GT_TKB9G LIKE TABLE OF TKB9G WITH HEADER LINE.
DATA: BEGIN OF IEXCEL OCCURS 0. "excel上載內表
INCLUDE STRUCTURE ZALSMEX_TABLINE.
DATA: END OF IEXCEL.orm

DATA: BEGIN OF IT_DOC OCCURS 0,
ZBOX TYPE C,
* CBOX TYPE C,
ICONID LIKE ICON-ID,
DOC_NO LIKE BKPF-BELNR, "憑證序號
BELNR LIKE BKPF-BELNR,
XBLNR LIKE BKPF-XBLNR, "參照
BLDAT LIKE BKPF-BLDAT, "憑證日期
BUDAT LIKE BKPF-BUDAT, "過賬日期
PERIOD LIKE BKPF-MONAT, "期間
BLART LIKE BKPF-BLART, "憑證類型
BUKRS LIKE BKPF-BUKRS, "公司代碼
WAERS LIKE BKPF-WAERS, "幣別
KURSF LIKE BKPF-KURSF, "匯率
BKTXT LIKE BKPF-BKTXT, "擡頭文本
BRNCH LIKE BKPF-BRNCH,"分支號
BSCHL LIKE BSEG-BSCHL, "記賬代碼
HKONT LIKE BSEG-HKONT, "科目
UMSKZ LIKE BSEG-UMSKZ, "特別總帳標誌
ANBWA LIKE BSEG-ANBWA, "事務類型
WRBTR LIKE BSEG-WRBTR, "憑證金額
DMBTR LIKE BSEG-DMBTR, "本位幣金額
GSBER LIKE BSEG-GSBER, "業務範圍
RSTGR LIKE BSEG-RSTGR, "緣由代碼
ZUONR LIKE BSEG-ZUONR, "分配號
SGTXT LIKE BSEG-SGTXT, "行項目文本
ZFBDT LIKE BSEG-ZFBDT, "基準日期
ZTERM LIKE BSEG-ZTERM, "付款條件
KOSTL LIKE BSEG-KOSTL, "成本中心
PRCTR LIKE BSEG-PRCTR, "利潤中心
AUFNR LIKE BSEG-AUFNR, "訂單號
EBELN LIKE BSEG-EBELN, "採購憑證號
EBELP LIKE BSEG-EBELP, "採購憑證的項目編號
ZEKKN LIKE BSEG-ZEKKN, "賬戶分配的序列號
MWSKZ LIKE BSEG-MWSKZ, "稅碼
KUNNR LIKE CE16666-KUNNR,
KDGRP LIKE CE16666-KDGRP,
VKORG LIKE CE16666-VKORG,
VTWEG LIKE CE16666-VTWEG,
KMVKBU LIKE CE16666-KMVKBU,
WWZZS LIKE CE16666-WWZZS,
BRSCH LIKE CE16666-BRSCH,
SPART LIKE CE16666-SPART,
KMVTNR LIKE CE16666-KMVTNR,
WERKS LIKE CE16666-WERKS,
MATNR LIKE CE16666-MATNR,
EXTWG LIKE CE16666-EXTWG,
PRODH LIKE CE16666-PRODH,
KMMAKL LIKE CE16666-KMMAKL,
WW01 LIKE CE16666-WW01,
RESULT TYPE STRING, "運行結果: 成功 失敗
RETURN LIKE TABLE OF BAPIRET2 , "BAPI MESSAGE
WAERSB TYPE T001-WAERS,
XCPDK TYPE KNA1-XCPDK,"一次性標記
NAME LIKE BAPIACPA09-NAME,
CITY LIKE BAPIACPA09-CITY,
COUNTRY LIKE BAPIACPA09-COUNTRY,
XVBELN TYPE VBELN,
* BANK_ACCT LIKE BAPIACPA09-BANK_ACCT,
* BANK_CTRY LIKE BAPIACPA09-BANK_CTRY,
END OF IT_DOC.
*---------------------------------------------------------------------*
* 選擇屏幕
*---------------------------------------------------------------------*
SELECTION-SCREEN:BEGIN OF BLOCK BK3 WITH FRAME TITLE TEXT-030.
*PARAMETERS:
* P_FILE LIKE RLGRAP-FILENAME OBLIGATORY DEFAULT 'C:\' ."文件目錄
SELECT-OPTIONS: S_DATE FOR BKPF-BLDAT.
SELECT-OPTIONS: S_VBELN FOR VBAK-VBELN.
SELECT-OPTIONS: S_VKORG FOR VBAK-VKORG DEFAULT '6003'.
SELECT-OPTIONS: S_VTWEG FOR VBAK-VTWEG DEFAULT '11'.事務


SELECTION-SCREEN:END OF BLOCK BK3.io

*SELECTION-SCREEN: FUNCTION KEY 1.
*&--------------------------------------------------------------------*
*& I N I T I A L I A T I O N
*&--------------------------------------------------------------------*
*INITIALIZATION.
* FUNCTXT-ICON_ID = ICON_EXPORT.
* FUNCTXT-QUICKINFO = '模版下載'.
* FUNCTXT-ICON_TEXT = '模版下載'.
* SSCRFIELDS-FUNCTXT_01 = FUNCTXT.form

*-----------------------------------------------------------------------
* At Selection Screen
*-----------------------------------------------------------------------
*AT SELECTION-SCREEN.
* IF SSCRFIELDS-UCOMM = 'FC01'.
* PERFORM TEMP_EXCEL_GET USING 'ZFI_DOCU_IMPORT'.
* ELSE.
* IF P_FILE IS INITIAL.
* MESSAGE E001(00) WITH '請選擇導入模版!'.
* ENDIF.
* ENDIF.
*
*AT SELECTION-SCREEN ON VALUE-REQUEST FOR P_FILE.
* PERFORM GET_FILENAME.
*---------------------------------------------------------------------*
* S T A R T - O F - S E L E C T I O N
*---------------------------------------------------------------------*
START-OF-SELECTION.擴展


PERFORM READ_DATA.
PERFORM CHECKVALID."檢查數據
PERFORM SHOW_DOC.下載

END-OF-SELECTION.
*&---------------------------------------------------------------------*
*& FORM BUILD_LAYOUT
*&---------------------------------------------------------------------*
* TEXT
*----------------------------------------------------------------------*
* --> P1 TEXT
* <-- P2 TEXT
*----------------------------------------------------------------------*
FORM BUILD_LAYOUT .
GS_LAYOUT-BOX_FIELDNAME = 'ZBOX'.
GS_LAYOUT-ZEBRA = 'X'.
GS_LAYOUT-DETAIL_POPUP = 'X'.
GS_LAYOUT-COLWIDTH_OPTIMIZE = 'X'.
ENDFORM. " BUILD_LAYOUT
*&---------------------------------------------------------------------*
*& FORM WRITE_DATA_ALV
*&---------------------------------------------------------------------*
* TEXT
*----------------------------------------------------------------------*
* --> P1 TEXT
* <-- P2 TEXT
*----------------------------------------------------------------------*
FORM WRITE_DATA_ALV .
CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
EXPORTING
I_CALLBACK_PROGRAM = SY-REPID
IS_LAYOUT = GS_LAYOUT
IT_FIELDCAT = GT_FIELDCAT
I_CALLBACK_PF_STATUS_SET = 'SET_PF_STATUS'
I_CALLBACK_USER_COMMAND = 'USER_COMMAND'
I_DEFAULT = 'X'
I_SAVE = 'A'
TABLES
T_OUTTAB = IT_DOC
EXCEPTIONS
PROGRAM_ERROR = 1
OTHERS = 2.
IF SY-SUBRC <> 0.
MESSAGE S398 WITH 'Error when output data in ALV format.'.
ENDIF.二進制

ENDFORM. " WRITE_DATA_ALV
*&--------------------------------------------------------------------*
*& FORM SET_PF_STATUS
*&--------------------------------------------------------------------*
* TEXT
*---------------------------------------------------------------------*
FORM SET_PF_STATUS USING RT_EXTAB TYPE SLIS_T_EXTAB.
REFRESH: RT_EXTAB.
SET PF-STATUS 'ZSTATUS' EXCLUDING RT_EXTAB.
ENDFORM. "set_pf_status
*&--------------------------------------------------------------------*
*& FORM USER_COMMAND
*&--------------------------------------------------------------------*
* TEXT
*---------------------------------------------------------------------*
FORM USER_COMMAND USING R_UCOMM LIKE SY-UCOMM
RS_SELFIELD TYPE SLIS_SELFIELD.
CASE R_UCOMM.
WHEN 'CHECK'.
PERFORM PROCESS_DATA USING 'X'.
WHEN 'SAVE'.
PERFORM PROCESS_DATA USING ''.
WHEN 'LOG'.
READ TABLE IT_DOC INDEX RS_SELFIELD-TABINDEX.
IF SY-SUBRC = 0.
IF IT_SUBFIELDCAT[] IS INITIAL.
PERFORM INITIALIZE_SUBFIELDCAT.
ENDIF.
PERFORM WRITE_BAPILOG_ALV USING IT_DOC-RETURN.
ENDIF.
WHEN '&IC1'.
IF RS_SELFIELD-FIELDNAME = 'RESULT'.
READ TABLE IT_DOC INDEX RS_SELFIELD-TABINDEX.
IF IT_DOC-RESULT = '失敗'.
IF IT_SUBFIELDCAT[] IS INITIAL.
PERFORM INITIALIZE_SUBFIELDCAT.
ENDIF.
PERFORM WRITE_BAPILOG_ALV USING IT_DOC-RETURN.
ENDIF.
ENDIF.
IF RS_SELFIELD-FIELDNAME = 'BELNR'.
READ TABLE IT_DOC INDEX RS_SELFIELD-TABINDEX.
IF IT_DOC-BELNR <> ''.
SET PARAMETER ID 'BLN' FIELD IT_DOC-BELNR.
SET PARAMETER ID 'BUK' FIELD IT_DOC-BUKRS.
SET PARAMETER ID 'GJR' FIELD IT_DOC-BUDAT+0(4).
CALL TRANSACTION 'FB03' AND SKIP FIRST SCREEN.
ENDIF.
ENDIF.
WHEN OTHERS.
ENDCASE.
RS_SELFIELD-REFRESH = 'X'.
RS_SELFIELD-COL_STABLE = 'X'.
RS_SELFIELD-ROW_STABLE = 'X'.
ENDFORM. "user_command
*&---------------------------------------------------------------------*
*& FORM INITIALIZE_SUBFIELDCAT
*&---------------------------------------------------------------------*
* TEXT
*----------------------------------------------------------------------*
* --> P1 TEXT
* <-- P2 TEXT
*----------------------------------------------------------------------*
FORM INITIALIZE_SUBFIELDCAT .
*獲取數據字典中各個字段的屬性
CALL FUNCTION 'REUSE_ALV_FIELDCATALOG_MERGE'
EXPORTING
I_PROGRAM_NAME = SY-REPID
I_STRUCTURE_NAME = 'BAPIRET2'
I_INCLNAME = SY-REPID
CHANGING
CT_FIELDCAT = IT_SUBFIELDCAT[]
EXCEPTIONS
INCONSISTENT_INTERFACE = 1
PROGRAM_ERROR = 2.
ENDFORM. " INITIALIZE_SUBFIELDCAT
*&---------------------------------------------------------------------*
*& FORM WRITE_BAPILOG_ALV
*&---------------------------------------------------------------------*
* TEXT
*----------------------------------------------------------------------*
* -->P_IT_DOC_RETURN TEXT
*----------------------------------------------------------------------*
FORM WRITE_BAPILOG_ALV USING PT_RETURN LIKE IT_DOC-RETURN.
DATA LS_LAYOUT TYPE SLIS_LAYOUT_ALV.
LS_LAYOUT-ZEBRA = 'X'.
LS_LAYOUT-DETAIL_POPUP = 'X'.
LS_LAYOUT-COLWIDTH_OPTIMIZE = 'X'.
CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
EXPORTING
I_CALLBACK_PROGRAM = SY-REPID
IS_LAYOUT = LS_LAYOUT
IT_FIELDCAT = IT_SUBFIELDCAT
* I_CALLBACK_PF_STATUS_SET = 'SET_PF_STATUS'
* I_CALLBACK_USER_COMMAND = 'USER_COMMAND'
I_DEFAULT = 'X'
I_SAVE = 'A'
TABLES
T_OUTTAB = PT_RETURN
EXCEPTIONS
PROGRAM_ERROR = 1
OTHERS = 2.
IF SY-SUBRC <> 0.
MESSAGE S398 WITH 'Error when output data in ALV format.'.
ENDIF.
ENDFORM. " WRITE_BAPILOG_ALVcommand


*&---------------------------------------------------------------------*
*& FORM READ_DATA
*&---------------------------------------------------------------------*
* TEXT
*----------------------------------------------------------------------*
* --> P1 TEXT
* <-- P2 TEXT
*----------------------------------------------------------------------*
FORM READ_DATA .
DATA: BEGIN OF ITS OCCURS 0,
VBELN TYPE VBELN,
VKORG TYPE VKORG,
KZWI4 TYPE KZWI4,
BUKRS TYPE VBRK-BUKRS,
BSTNK_VF LIKE VBRK-BSTNK_VF,
VGBEL LIKE VBRP-VGBEL,
NRZAS LIKE VBRK-NRZAS,
ERDAT TYPE ERDAT,
END OF ITS.

 

 

SELECT R~VBELN
VKORG
NETWR AS KZWI4
KKBER AS BUKRS
BSTNK AS BSTNK_VF
VBELN AS VGBEL
VSNMR_V AS NRZAS
ERDAT
INTO TABLE ITS
FROM VBAK AS R
* JOIN VBaP AS P ON R~VBELN = P~VBELN
WHERE R~ERDAT IN S_DATE
AND R~VBELN IN S_VBELN
AND R~VKORG IN S_VKORG
AND R~VTWEG IN S_VTWEG
AND KUNNR = '0000000001'.

 

* SELECT R~VBELN
* VKORG
* KZWI4
* BUKRS
* BSTNK_VF
* VGBEL
* NRZAS
* INTO TABLE ITS
* FROM VBRK AS R
* JOIN VBRP AS P ON R~VBELN = P~VBELN
* WHERE P~ERDAT IN S_DATE
* AND KUNAG = '0000000001'.


*擡頭文本 = 科目號.工號-姓名.DRP訂單號

LOOP AT ITS WHERE NRZAS = ''.
IT_DOC-BELNR = ITS-NRZAS.
* IT_DOC-DOC_NO = ITS-VBELN."憑證序號
MOVE ITS-ERDAT TO IT_DOC-DOC_NO .
CONCATENATE IT_DOC-DOC_NO ITS-BUKRS INTO IT_DOC-DOC_NO.

IT_DOC-XVBELN = ITS-VBELN.

IT_DOC-BLDAT = SY-DATUM."憑證日期
IT_DOC-BUDAT = SY-DATUM. "過賬日期
IT_DOC-PERIOD = IT_DOC-BUDAT+4(2).
IT_DOC-BLART = 'DA'. "憑證類型
IT_DOC-BUKRS = ITS-BUKRS."公司代碼
IT_DOC-WAERS = 'CNY'. "幣別
IT_DOC-BKTXT = '員工入賬'.

IT_DOC-BSCHL = '15'. "記賬代碼
IT_DOC-HKONT = '0000000001' . "科目

* IT_DOC-WRBTR = ITS-KZWI4 * '1.17'.
* IT_DOC-DMBTR = ITS-KZWI4 * '1.17'.
IT_DOC-GSBER = ITS-VKORG.
IT_DOC-PRCTR = ITS-VKORG.

IF IT_DOC-PRCTR+0(1) = '8'.
IT_DOC-PRCTR = '6001'.
IT_DOC-GSBER = '6001'.
ENDIF.

DATA: S(100),T(100),V(100),P(100).
* CLEAR: S,T.
* SPLIT ITS-BSTNK_VF AT '.' INTO S T V .
* CONCATENATE S ITS-VGBEL INTO IT_DOC-SGTXT .

 

 

* IT_DOC-SGTXT = ITS-VGBEL.
IF ITS-VKORG = '6001'.
IT_DOC-ZUONR = '2'.
ELSEIF ITS-VKORG = '6003'.
IT_DOC-ZUONR = '6'.
ELSE.
IT_DOC-ZUONR = '1'.
ENDIF.
IT_DOC-NAME = '一次性客戶'.

IT_DOC-CITY = '弗吉尼亞'.

* APPEND IT_DOC.


DATA: NAME LIKE THEAD-TDNAME,
ITAB_HTXT LIKE TLINE OCCURS 0 WITH HEADER LINE.
CLEAR : NAME, S,T,ITAB_HTXT , ITAB_HTXT[].
MOVE ITS-VBELN TO NAME.
CALL FUNCTION 'READ_TEXT'
EXPORTING
ID = '0001'
LANGUAGE = '1'
NAME = NAME
OBJECT = 'VBBK'
TABLES
LINES = ITAB_HTXT
EXCEPTIONS
ID = 1
LANGUAGE = 2
NAME = 3
NOT_FOUND = 4
OBJECT = 5
REFERENCE_CHECK = 6
WRONG_ACCESS_TO_ARCHIVE = 7
OTHERS = 8.

READ TABLE ITAB_HTXT INDEX 1.
IF SY-SUBRC = 0.
SPLIT ITAB_HTXT-TDLINE AT '_' INTO S T V P .
MOVE V TO IT_DOC-WRBTR .
MOVE V TO IT_DOC-DMBTR .
CONCATENATE T '/' V '/' ITS-VGBEL INTO IT_DOC-SGTXT .
APPEND IT_DOC.


IT_DOC-HKONT = S . "科目
else.
move ITS-VGBEL TO IT_DOC-SGTXT .
ENDIF.
IT_DOC-RSTGR = 'A01'.
IT_DOC-BSCHL = '40'. "記賬代碼
APPEND IT_DOC.

CLEAR ITS.
CLEAR IT_DOC.
ENDLOOP.


ENDFORM. " READ_DATA

*&---------------------------------------------------------------------*
*& Form PROCESS_DATA
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* -->P_TEST text
*----------------------------------------------------------------------*
FORM PROCESS_DATA USING P_TEST TYPE C.
DATA:
WA_DOCUMENTHEADER TYPE BAPIACHE09 ,
WA_CUST TYPE BAPIACPA09,
IT_ACCOUNTGL TYPE BAPIACGL09 OCCURS 0 WITH HEADER LINE,
IT_CURRENCYAMOUNT TYPE BAPIACCR09 OCCURS 0 WITH HEADER LINE,
IT_ACCOUNTRECEIVABLE TYPE BAPIACAR09 OCCURS 0 WITH HEADER LINE,
IT_RETURN TYPE BAPIRET2 OCCURS 0 WITH HEADER LINE,
IT_EXT2 TYPE BAPIPAREX OCCURS 0 WITH HEADER LINE,
IT_ACCOUNTPAYABLE TYPE BAPIACAP09 OCCURS 0 WITH HEADER LINE,
IT_ACCOUNTTAX LIKE TABLE OF BAPIACTX09 WITH HEADER LINE,
LT_ACKEC LIKE TABLE OF BAPIACKEC9 WITH HEADER LINE,
LT_ACKEV LIKE TABLE OF BAPIACKEV9 WITH HEADER LINE.
DATA:
ISNEW_DOC TYPE I,
ITEMNO TYPE I VALUE 0. "當前行項目編號
DATA: L_KEY LIKE WA_DOCUMENTHEADER-OBJ_KEY.
DATA: LV_KOKRS TYPE TKA02-KOKRS,
LV_ERZUO TYPE TKB9G-ERZUO.

LOOP AT IT_DOC WHERE BELNR = ''.
AT NEW DOC_NO.
CLEAR: WA_DOCUMENTHEADER,IT_ACCOUNTGL[],IT_ACCOUNTPAYABLE[],
IT_ACCOUNTRECEIVABLE[],IT_EXT2[],
IT_RETURN[],IT_CURRENCYAMOUNT[],IT_ACCOUNTTAX[].
ISNEW_DOC = 1.
ITEMNO = 0.
ENDAT.

IF ISNEW_DOC = 1."新憑證
ISNEW_DOC = 0.
PERFORM FILL_HEADER USING IT_DOC
CHANGING WA_DOCUMENTHEADER
WA_CUST."輸入憑證擡頭
ENDIF.

ITEMNO = ITEMNO + 1.
SELECT SINGLE * FROM TBSL WHERE BSCHL = IT_DOC-BSCHL."記賬代碼
CASE TBSL-KOART."記賬代碼
"客戶相關統馭科目
WHEN 'D'.
PERFORM FILL_AR TABLES IT_ACCOUNTRECEIVABLE
USING ITEMNO
IT_DOC.
"供應商相關統馭科目
WHEN 'K'.
PERFORM FILL_AP TABLES IT_ACCOUNTPAYABLE
USING ITEMNO
IT_DOC.
WHEN OTHERS.
PERFORM FILL_GL TABLES IT_ACCOUNTGL
USING ITEMNO
IT_DOC.
ENDCASE.

IF IT_DOC-MWSKZ <> ''.
PERFORM FILL_IT_ACCOUNTTAX TABLES IT_CURRENCYAMOUNT
IT_ACCOUNTTAX
USING ITEMNO
CHANGING IT_DOC.
ENDIF.
*"貨幣項目
PERFORM FILL_CURRAMOUNT TABLES IT_CURRENCYAMOUNT
USING ITEMNO
IT_DOC
TBSL.
*COPA字段

SELECT SINGLE KOKRS FROM TKA02
INTO LV_KOKRS
WHERE BUKRS = IT_DOC-BUKRS.

SELECT SINGLE ERZUO
FROM TKB9G
INTO LV_ERZUO
WHERE ERSCH = 'FI'
AND KOKRS = LV_KOKRS
AND VALMIN = IT_DOC-HKONT.
IF SY-SUBRC = 0.
PERFORM FILL_COPA TABLES LT_ACKEC
LT_ACKEV
USING ITEMNO
IT_DOC.
ENDIF.
*緣由代碼
PERFORM FILL_EXT2 TABLES IT_EXT2
USING ITEMNO
IT_DOC.
AT END OF DOC_NO.
IF P_TEST = ''. "執行
CALL FUNCTION 'BAPI_ACC_DOCUMENT_POST'
EXPORTING
DOCUMENTHEADER = WA_DOCUMENTHEADER
CUSTOMERCPD = WA_CUST
IMPORTING
OBJ_KEY = L_KEY
TABLES
ACCOUNTGL = IT_ACCOUNTGL
ACCOUNTRECEIVABLE = IT_ACCOUNTRECEIVABLE
ACCOUNTPAYABLE = IT_ACCOUNTPAYABLE
EXTENSION2 = IT_EXT2
CURRENCYAMOUNT = IT_CURRENCYAMOUNT
ACCOUNTTAX = IT_ACCOUNTTAX
CRITERIA = LT_ACKEC
VALUEFIELD = LT_ACKEV
RETURN = IT_RETURN.
ELSEIF P_TEST = 'X'. "測試
CALL FUNCTION 'BAPI_ACC_DOCUMENT_CHECK'
EXPORTING
DOCUMENTHEADER = WA_DOCUMENTHEADER
CUSTOMERCPD = WA_CUST
TABLES
ACCOUNTGL = IT_ACCOUNTGL
ACCOUNTRECEIVABLE = IT_ACCOUNTRECEIVABLE
ACCOUNTPAYABLE = IT_ACCOUNTPAYABLE
EXTENSION2 = IT_EXT2
CURRENCYAMOUNT = IT_CURRENCYAMOUNT
ACCOUNTTAX = IT_ACCOUNTTAX
CRITERIA = LT_ACKEC
VALUEFIELD = LT_ACKEV
RETURN = IT_RETURN.
ENDIF.

READ TABLE IT_RETURN WITH KEY TYPE = 'E'.
IF SY-SUBRC <> 0. "找不到錯誤
IT_DOC-ICONID = ICON_LED_GREEN.
IF P_TEST = 'X'.
IT_DOC-BELNR = ''.
IT_DOC-RESULT = '檢查OK'.
ELSE.
CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'
EXPORTING
WAIT = 'X'.
CONCATENATE '過帳成功!憑證號爲' L_KEY(10) INTO
IT_DOC-RESULT.
IT_DOC-BELNR = L_KEY(10).
ENDIF.

 

ELSE.
IT_DOC-RESULT = '失敗'.
IT_DOC-ICONID = ICON_LED_RED.
ENDIF.
LOOP AT IT_RETURN.
APPEND IT_RETURN TO IT_DOC-RETURN.
ENDLOOP.
MODIFY IT_DOC FROM IT_DOC
TRANSPORTING RESULT ICONID BELNR RETURN
WHERE DOC_NO = IT_DOC-DOC_NO.
CLEAR: WA_DOCUMENTHEADER,IT_ACCOUNTGL[],IT_ACCOUNTPAYABLE[],
IT_ACCOUNTRECEIVABLE[],IT_EXT2[],IT_RETURN[],
IT_CURRENCYAMOUNT[],IT_ACCOUNTTAX[].
ENDAT.

 

ENDLOOP.

DATA: SV LIKE VBRK-VBELN.
DATA: BEGIN OF ITAB OCCURS 0,
VBELN TYPE VBELN,
BELNR LIKE IT_DOC-BELNR,
END OF ITAB.

LOOP AT IT_DOC WHERE ICONID = ICON_LED_GREEN.
MOVE IT_DOC-XVBELN TO ITAB-VBELN.
MOVE IT_DOC-BELNR TO ITAB-BELNR.
COLLECT ITAB.
* MOVE IT_DOC-DOC_NO TO SV.
ENDLOOP.

LOOP AT ITAB.

MOVE ITAB-VBELN TO SV.
CALL FUNCTION 'CONVERSION_EXIT_ALPHA_INPUT'
EXPORTING
INPUT = SV
IMPORTING
OUTPUT = SV.

UPDATE VBAK SET VSNMR_V = ITAB-BELNR
WHERE VBELN = SV .

ENDLOOP.


ENDFORM. " PROCESS_DATA
*&---------------------------------------------------------------------*
*& FORM FILL_HEADER
*&---------------------------------------------------------------------*
* TEXT
*----------------------------------------------------------------------*
* --> P1 TEXT
* <-- P2 TEXT
*----------------------------------------------------------------------*
FORM FILL_HEADER USING PS_DOCLINE LIKE IT_DOC
CHANGING DOC_HEADER TYPE BAPIACHE09
C_CUST TYPE BAPIACPA09.
DOC_HEADER-USERNAME = SY-UNAME.
DOC_HEADER-FISC_YEAR = PS_DOCLINE-BUDAT+0(4).
DOC_HEADER-REF_DOC_NO = PS_DOCLINE-XBLNR.
DOC_HEADER-DOC_DATE = PS_DOCLINE-BLDAT. "憑證日期
DOC_HEADER-PSTNG_DATE = PS_DOCLINE-BUDAT. "過帳日期
DOC_HEADER-DOC_TYPE = PS_DOCLINE-BLART. "憑證類型
DOC_HEADER-COMP_CODE = PS_DOCLINE-BUKRS. "公司代碼
DOC_HEADER-HEADER_TXT = PS_DOCLINE-BKTXT. "擡頭文本
DOC_HEADER-FIS_PERIOD = PS_DOCLINE-PERIOD. "期間
DOC_HEADER-BUS_ACT = 'RFBU'.
IF PS_DOCLINE-XCPDK = 'X'.
C_CUST-NAME = PS_DOCLINE-NAME.
C_CUST-CITY = PS_DOCLINE-CITY.
C_CUST-COUNTRY = PS_DOCLINE-COUNTRY.
* C_CUST-BANK_ACCT = PS_DOCLINE-BANK_ACCT.
* C_CUST-BANK_CTRY = PS_DOCLINE-BANK_CTRY.
ENDIF.
ENDFORM. " FILL_HEADER
*&---------------------------------------------------------------------*
*& FORM FILL_GL
*&---------------------------------------------------------------------*
* TEXT
*----------------------------------------------------------------------*
* --> P1 TEXT
* <-- P2 TEXT
*----------------------------------------------------------------------*
FORM FILL_GL TABLES PT_ACCOUNTGL STRUCTURE BAPIACGL09
USING P_ITEMNO TYPE I
DOCLINE LIKE IT_DOC.
DATA:TKTOGR LIKE ANLA-KTOGR.
DATA:TANLN1 LIKE ANLA-ANLN1 .

CLEAR PT_ACCOUNTGL.
PT_ACCOUNTGL-ITEMNO_ACC = P_ITEMNO.
CALL FUNCTION 'CONVERSION_EXIT_ALPHA_INPUT'
EXPORTING
INPUT = DOCLINE-HKONT "科目
IMPORTING
OUTPUT = PT_ACCOUNTGL-GL_ACCOUNT.

PT_ACCOUNTGL-COMP_CODE = DOCLINE-BUKRS. "公司代碼
PT_ACCOUNTGL-ALLOC_NMBR = DOCLINE-ZUONR . "分配號
PT_ACCOUNTGL-ITEM_TEXT = DOCLINE-SGTXT . "行項目文本
* PT_ACCOUNTGL-WBS_ELEMENT = <FS_DOCLINE>-PROJK . "WBS元素
* PT_ACCOUNTGL-MATERIAL = <FS_DOCLINE>-MATNR. "物料號
* PT_ACCOUNTGL-QUANTITY = <FS_DOCLINE>-MENGE. "數量
* PT_ACCOUNTGL-BASE_UOM = <FS_DOCLINE>-MEINS."基本計量單位

CALL FUNCTION 'CONVERSION_EXIT_ALPHA_INPUT'"成本中心
EXPORTING
INPUT = DOCLINE-KOSTL
IMPORTING
OUTPUT = PT_ACCOUNTGL-COSTCENTER.

CALL FUNCTION 'CONVERSION_EXIT_ALPHA_INPUT' "內部訂單號
EXPORTING
INPUT = DOCLINE-AUFNR
IMPORTING
OUTPUT = PT_ACCOUNTGL-ORDERID.
* PT_ACCOUNTGL-FUNC_AREA = <FS_DOCLINE>-FKBER. "功能範圍
PT_ACCOUNTGL-PO_NUMBER = DOCLINE-EBELN. "採購憑證號
PT_ACCOUNTGL-PO_ITEM = DOCLINE-EBELP.
* PT_ACCOUNTGL-REF_KEY_1 = <FS_DOCLINE>-XREF1. "參考代碼1
* PT_ACCOUNTGL-REF_KEY_2 = <FS_DOCLINE>-XREF2. "參考代碼2
* PT_ACCOUNTGL-REF_KEY_3 = <FS_DOCLINE>-XREF3. "參考代碼3
PT_ACCOUNTGL-TAX_CODE = DOCLINE-MWSKZ.
* CALL FUNCTION 'CONVERSION_EXIT_ALPHA_INPUT'
* EXPORTING
* INPUT = <FS_DOCLINE>-VBUND
* IMPORTING
* OUTPUT = PT_ACCOUNTGL-TRADE_ID. "貿易伙伴
* PT_ACCOUNTGL-CS_TRANS_T = <FS_DOCLINE>-BEWAR. "事物類型
PT_ACCOUNTGL-PROFIT_CTR = DOCLINE-PRCTR."利潤中心

* IF <FS_DOCLINE>-BSCHL = '70' OR <FS_DOCLINE>-BSCHL = '75'.
* PT_ACCOUNTGL-ACCT_TYPE = 'A'.
* CLEAR:TKTOGR.
* CLEAR:TANLN1 .
* TANLN1 = PT_ACCOUNTGL-GL_ACCOUNT.
* CALL FUNCTION 'CONVERSION_EXIT_ALPHA_INPUT'
* EXPORTING
* INPUT = TANLN1
* IMPORTING
* OUTPUT = TANLN1.
* SELECT SINGLE KTOGR INTO TKTOGR
* FROM ANLA
* WHERE BUKRS = DOCLINE-BUKRS
* AND ANLN1 = TANLN1.
* PT_ACCOUNTGL-ASSET_NO = TANLN1.
* PT_ACCOUNTGL-SUB_NUMBER = '0000'.
*
* SELECT SINGLE * FROM T001
* WHERE BUKRS = DOCLINE-BUKRS.
* IF SY-SUBRC = 0.
* SELECT SINGLE KTANSW
* INTO PT_ACCOUNTGL-GL_ACCOUNT
* FROM T095
* WHERE KTOGR = TKTOGR
* AND KTOPL = T001-KTOPL.
* ENDIF.
* ENDIF.

APPEND PT_ACCOUNTGL.
CLEAR PT_ACCOUNTGL.
ENDFORM. " FILL_GL


*&---------------------------------------------------------------------*
*& FORM FILL_CURRAMOUNT
*&---------------------------------------------------------------------*
* TEXT
*----------------------------------------------------------------------*
* --> P1 TEXT
* <-- P2 TEXT
*----------------------------------------------------------------------*
FORM FILL_CURRAMOUNT TABLES PT_CURRENCYAMOUNT STRUCTURE BAPIACCR09
USING P_ITEMNO TYPE I
DOCLINE LIKE IT_DOC
LS_TBSL TYPE TBSL.
CLEAR PT_CURRENCYAMOUNT.
PT_CURRENCYAMOUNT-ITEMNO_ACC = P_ITEMNO.
PT_CURRENCYAMOUNT-CURR_TYPE = '00'.
PT_CURRENCYAMOUNT-CURRENCY = DOCLINE-WAERS."幣別
CASE LS_TBSL-SHKZG."記賬代碼對應的借貸標識
WHEN 'H'. "貸方DM
PT_CURRENCYAMOUNT-AMT_DOCCUR = 0 - DOCLINE-WRBTR.
WHEN OTHERS.
PT_CURRENCYAMOUNT-AMT_DOCCUR = DOCLINE-WRBTR. "金額
ENDCASE.
IF DOCLINE-KURSF <> 0."直接指定匯率
PT_CURRENCYAMOUNT-EXCH_RATE = DOCLINE-KURSF.
ENDIF.
APPEND PT_CURRENCYAMOUNT.

IF DOCLINE-WAERS <> DOCLINE-WAERSB."外幣
IF DOCLINE-DMBTR <> 0 AND DOCLINE-KURSF = 0.
PT_CURRENCYAMOUNT-ITEMNO_ACC = P_ITEMNO.
PT_CURRENCYAMOUNT-CURR_TYPE = '10'.
PT_CURRENCYAMOUNT-CURRENCY = DOCLINE-WAERSB."幣別
CASE LS_TBSL-SHKZG."記賬代碼對應的借貸標識
WHEN 'H'. "貸方DM
PT_CURRENCYAMOUNT-AMT_DOCCUR = 0 - DOCLINE-DMBTR.
WHEN OTHERS.
PT_CURRENCYAMOUNT-AMT_DOCCUR = DOCLINE-DMBTR."本位金額
ENDCASE.
APPEND PT_CURRENCYAMOUNT.
ENDIF.

ENDIF.

CLEAR PT_CURRENCYAMOUNT.
ENDFORM. " FILL_CURRAMOUNT
*&---------------------------------------------------------------------*
*& FORM FILL_AR
*&---------------------------------------------------------------------*
* TEXT
*----------------------------------------------------------------------*
* --> P1 TEXT
* <-- P2 TEXT
*----------------------------------------------------------------------*
FORM FILL_AR TABLES PT_ACCOUNTRECEIVABLE STRUCTURE BAPIACAR09
USING P_ITEMNO TYPE I
DOCLINE LIKE IT_DOC.
CLEAR PT_ACCOUNTRECEIVABLE.
PT_ACCOUNTRECEIVABLE-ITEMNO_ACC = P_ITEMNO.
CALL FUNCTION 'CONVERSION_EXIT_ALPHA_INPUT'
EXPORTING
INPUT = DOCLINE-HKONT
IMPORTING
OUTPUT = PT_ACCOUNTRECEIVABLE-CUSTOMER. "客戶

SELECT SINGLE ZTERM FROM KNB1
INTO PT_ACCOUNTRECEIVABLE-PMNTTRMS "付款條件
WHERE KUNNR = PT_ACCOUNTRECEIVABLE-CUSTOMER
AND BUKRS = DOCLINE-BUKRS.

PT_ACCOUNTRECEIVABLE-COMP_CODE = DOCLINE-BUKRS. "公司代碼
PT_ACCOUNTRECEIVABLE-ITEM_TEXT = DOCLINE-SGTXT . "行項目文本
PT_ACCOUNTRECEIVABLE-ALLOC_NMBR = DOCLINE-ZUONR.
PT_ACCOUNTRECEIVABLE-BLINE_DATE = DOCLINE-ZFBDT. "到期日
* PT_ACCOUNTRECEIVABLE-REF_KEY_1 = DOCLINE-XREF1."參考代碼1
* PT_ACCOUNTRECEIVABLE-REF_KEY_2 = DOCLINE-XREF2."參考代碼2
* PT_ACCOUNTRECEIVABLE-REF_KEY_3 = DOCLINE-XREF3."參考代碼3
* PT_ACCOUNTRECEIVABLE-PARTNER_BK = DOCLINE-BVTYP.
"合做銀行類型
PT_ACCOUNTRECEIVABLE-BLINE_DATE = DOCLINE-ZFBDT.
"到日期/付款基準日
PT_ACCOUNTRECEIVABLE-SP_GL_IND = DOCLINE-UMSKZ.
"特殊總分類賬標誌
PT_ACCOUNTRECEIVABLE-BUS_AREA = DOCLINE-GSBER."業務範圍
* PT_ACCOUNTRECEIVABLE-DSCT_PCT1 = 0. "折扣百分比
PT_ACCOUNTRECEIVABLE-TAX_CODE = DOCLINE-MWSKZ.
* PT_ACCOUNTRECEIVABLE-sp_gl_ind = 'V'.
APPEND PT_ACCOUNTRECEIVABLE.
CLEAR PT_ACCOUNTRECEIVABLE.
ENDFORM. " FILL_AR
*&---------------------------------------------------------------------*
*& FORM FILL_AP
*&---------------------------------------------------------------------*
* TEXT
*----------------------------------------------------------------------*
* --> P1 TEXT
* <-- P2 TEXT
*----------------------------------------------------------------------*
FORM FILL_AP TABLES PT_ACCOUNTPAYABLE STRUCTURE BAPIACAP09
USING P_ITEMNO TYPE I
DOCLINE LIKE IT_DOC.
CLEAR PT_ACCOUNTPAYABLE.
PT_ACCOUNTPAYABLE-ITEMNO_ACC = P_ITEMNO.
CALL FUNCTION 'CONVERSION_EXIT_ALPHA_INPUT'
EXPORTING
INPUT = DOCLINE-HKONT
IMPORTING
OUTPUT = PT_ACCOUNTPAYABLE-VENDOR_NO. "供應商
* PT_ACCOUNTPAYABLE-GL_ACCOUNT = PT_ACCOUNTPAYABLE-VENDOR_NO.
IF DOCLINE-ZTERM = ''.
SELECT SINGLE ZTERM FROM LFB1
INTO PT_ACCOUNTPAYABLE-PMNTTRMS
WHERE LIFNR = PT_ACCOUNTPAYABLE-VENDOR_NO
AND BUKRS = DOCLINE-BUKRS.
ELSE.
PT_ACCOUNTPAYABLE-PMNTTRMS = DOCLINE-ZTERM. "付款條件
ENDIF.
PT_ACCOUNTPAYABLE-COMP_CODE = DOCLINE-BUKRS. "公司代碼
PT_ACCOUNTPAYABLE-ITEM_TEXT = DOCLINE-SGTXT .
"行項目文本
PT_ACCOUNTPAYABLE-ALLOC_NMBR = DOCLINE-ZUONR.
PT_ACCOUNTPAYABLE-BLINE_DATE = DOCLINE-ZFBDT. "到期日
PT_ACCOUNTPAYABLE-BUS_AREA = DOCLINE-GSBER."業務範圍
PT_ACCOUNTPAYABLE-BLINE_DATE = DOCLINE-ZFBDT.
"到日期/付款基準日
PT_ACCOUNTPAYABLE-TAX_CODE = DOCLINE-MWSKZ.
APPEND PT_ACCOUNTPAYABLE.
CLEAR PT_ACCOUNTPAYABLE.
ENDFORM. " FILL_AP

*&---------------------------------------------------------------------*
*& FORM FILL_EXT2
*&---------------------------------------------------------------------*
* EXTENSION2 擴展字段加強部分
*----------------------------------------------------------------------*
* --> P1 TEXT
* <-- P2 TEXT
*---------------------------------------------------------------------
FORM FILL_EXT2 TABLES PT_EXT2 STRUCTURE BAPIPAREX
USING P_ITEMNO TYPE I
DOCLINE LIKE IT_DOC.
DATA: WA_ZEXTEN LIKE ZRSTRG.
CLEAR:WA_ZEXTEN.
WA_ZEXTEN-POSNR = P_ITEMNO.
WA_ZEXTEN-RSTGR = DOCLINE-RSTGR. "緣由代碼
WA_ZEXTEN-BSCHL = DOCLINE-BSCHL. "記帳代碼
WA_ZEXTEN-UMSKZ = DOCLINE-UMSKZ. "特殊總帳標誌
WA_ZEXTEN-BRNCH = DOCLINE-BRNCH.
PT_EXT2-STRUCTURE = 'ZRSTRG'.
PT_EXT2-VALUEPART1 = WA_ZEXTEN.
APPEND PT_EXT2.
CLEAR PT_EXT2.
ENDFORM. " FILL_EXT2
*&---------------------------------------------------------------------*
*& FORM SHOW_DOC
*&---------------------------------------------------------------------*
* TEXT
*----------------------------------------------------------------------*
* --> P1 TEXT
* <-- P2 TEXT
*----------------------------------------------------------------------*
FORM SHOW_DOC .
PERFORM GET_FIELDCATALOG.
PERFORM BUILD_LAYOUT.
PERFORM WRITE_DATA_ALV.
ENDFORM. " SHOW_DOC
*&---------------------------------------------------------------------*
*& Form GET_FIELDCATALOG
*&---------------------------------------------------------------------*
* 設置各個字段的屬性
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
FORM GET_FIELDCATALOG .
*獲取數據字典中各個字段的屬性
CALL FUNCTION 'REUSE_ALV_FIELDCATALOG_MERGE'
EXPORTING
I_PROGRAM_NAME = SY-REPID
I_INTERNAL_TABNAME = 'IT_DOC'
I_INCLNAME = SY-REPID
CHANGING
CT_FIELDCAT = GT_FIELDCAT[]
EXCEPTIONS
INCONSISTENT_INTERFACE = 1
PROGRAM_ERROR = 2.
*修改字段屬性
READ TABLE GT_FIELDCAT INTO WA_FIELDCAT WITH KEY FIELDNAME = 'DOC_NO'.
IF SY-SUBRC = 0.
WA_FIELDCAT-SELTEXT_S = '憑證序號'.
WA_FIELDCAT-SELTEXT_M = WA_FIELDCAT-SELTEXT_S.
WA_FIELDCAT-SELTEXT_L = WA_FIELDCAT-SELTEXT_S.
WA_FIELDCAT-DDICTXT = 'M'.
MODIFY GT_FIELDCAT FROM WA_FIELDCAT INDEX SY-TABIX.
ENDIF.
READ TABLE GT_FIELDCAT INTO WA_FIELDCAT WITH KEY FIELDNAME = 'RESULT'.
IF SY-SUBRC = 0.
WA_FIELDCAT-SELTEXT_S = '返回消息'.
WA_FIELDCAT-SELTEXT_M = WA_FIELDCAT-SELTEXT_S.
WA_FIELDCAT-SELTEXT_L = WA_FIELDCAT-SELTEXT_S.
WA_FIELDCAT-DDICTXT = 'M'.
MODIFY GT_FIELDCAT FROM WA_FIELDCAT INDEX SY-TABIX.
ENDIF.

* READ TABLE GT_FIELDCAT INTO WA_FIELDCAT WITH KEY FIELDNAME = 'ZBOX'.
* IF SY-SUBRC = 0.
* WA_FIELDCAT-checkbox = 'X'.
* WA_FIELDCAT-SELTEXT_S = '測試信息'.
* WA_FIELDCAT-SELTEXT_M = WA_FIELDCAT-SELTEXT_S.
* WA_FIELDCAT-SELTEXT_L = WA_FIELDCAT-SELTEXT_S.
* WA_FIELDCAT-DDICTXT = 'M'.
* MODIFY GT_FIELDCAT FROM WA_FIELDCAT INDEX SY-TABIX.
* ENDIF.

ENDFORM. " GET_FIELDCATALOG

*&---------------------------------------------------------------------*
*& FORM CHECKVALID
*&---------------------------------------------------------------------*
* TEXT
*----------------------------------------------------------------------*
* --> P1 TEXT
* <-- P2 TEXT
*----------------------------------------------------------------------*
FORM CHECKVALID .
FIELD-SYMBOLS: <FS_DOC> LIKE LINE OF IT_DOC.
DATA: LS_KNA1 TYPE KNA1,
LS_LFA1 TYPE LFA1.
DATA RSTGR LIKE BSEG-RSTGR.
DATA MSG LIKE BAPIRET2.

LOOP AT IT_DOC ASSIGNING <FS_DOC>.
IF <FS_DOC>-BUKRS = ''.
MSG-TYPE = 'E'.
<FS_DOC>-ICONID = ICON_LED_RED.
MSG-MESSAGE = '公司代碼不能爲空!'.
APPEND MSG TO <FS_DOC>-RETURN.
CONCATENATE <FS_DOC>-RESULT MSG-MESSAGE
INTO <FS_DOC>-RESULT.
ELSE.
SELECT SINGLE WAERS FROM T001
INTO <FS_DOC>-WAERSB
WHERE BUKRS = <FS_DOC>-BUKRS.
ENDIF.
IF <FS_DOC>-WAERS = ''.
MSG-TYPE = 'E'.
<FS_DOC>-ICONID = ICON_LED_RED.
MSG-MESSAGE = '幣別不能爲空!'.
APPEND MSG TO <FS_DOC>-RETURN.
CONCATENATE <FS_DOC>-RESULT MSG-MESSAGE
INTO <FS_DOC>-RESULT.
ENDIF.
IF <FS_DOC>-WAERS <> <FS_DOC>-WAERSB."外幣
IF <FS_DOC>-DMBTR = 0 AND <FS_DOC>-KURSF = 0.
MSG-TYPE = 'E'.
<FS_DOC>-ICONID = ICON_LED_RED.
MSG-MESSAGE = '外幣憑證必須填寫匯率或本位幣!'.
APPEND MSG TO <FS_DOC>-RETURN.
CONCATENATE <FS_DOC>-RESULT MSG-MESSAGE
INTO <FS_DOC>-RESULT.
ENDIF.
ENDIF.
IF <FS_DOC>-HKONT = ''.
MSG-TYPE = 'E'.
<FS_DOC>-ICONID = ICON_LED_RED.
MSG-MESSAGE = '科目不能爲空!'.
APPEND MSG TO <FS_DOC>-RETURN.
CONCATENATE <FS_DOC>-RESULT MSG-MESSAGE
INTO <FS_DOC>-RESULT.
ENDIF.
"緣由代碼校驗
IF <FS_DOC>-RSTGR <> SPACE.
SELECT SINGLE RSTGR FROM T053R
INTO RSTGR
WHERE BUKRS = <FS_DOC>-BUKRS
AND RSTGR = <FS_DOC>-RSTGR.
IF SY-SUBRC <> 0.
MSG-TYPE = 'E'.
<FS_DOC>-ICONID = ICON_LED_RED.
CONCATENATE '緣由代碼 ' <FS_DOC>-RSTGR ' 在公司 '
<FS_DOC>-BUKRS '不存在!' INTO MSG-MESSAGE.
APPEND MSG TO <FS_DOC>-RETURN.
ENDIF.
ENDIF.
IF <FS_DOC>-HKONT <> ''.
CALL FUNCTION 'CONVERSION_EXIT_ALPHA_INPUT'
EXPORTING
INPUT = <FS_DOC>-HKONT
IMPORTING
OUTPUT = <FS_DOC>-HKONT.
ENDIF.
IF <FS_DOC>-PRCTR <> ''.
CALL FUNCTION 'CONVERSION_EXIT_ALPHA_INPUT'
EXPORTING
INPUT = <FS_DOC>-PRCTR
IMPORTING
OUTPUT = <FS_DOC>-PRCTR.
ENDIF.
IF <FS_DOC>-AUFNR <> ''.
CALL FUNCTION 'CONVERSION_EXIT_ALPHA_INPUT'
EXPORTING
INPUT = <FS_DOC>-AUFNR
IMPORTING
OUTPUT = <FS_DOC>-AUFNR.
ENDIF.
IF <FS_DOC>-EBELN <> ''.
CALL FUNCTION 'CONVERSION_EXIT_ALPHA_INPUT'
EXPORTING
INPUT = <FS_DOC>-EBELN
IMPORTING
OUTPUT = <FS_DOC>-EBELN.
ENDIF.
"記賬代碼檢查
SELECT SINGLE * FROM TBSL
WHERE BSCHL = <FS_DOC>-BSCHL."記賬代碼
IF SY-SUBRC = 0.
CASE TBSL-KOART."記賬代碼
WHEN 'K'.
SELECT SINGLE * FROM LFA1 INTO LS_LFA1
WHERE LIFNR = <FS_DOC>-HKONT.
IF SY-SUBRC = 0.
<FS_DOC>-COUNTRY = LS_LFA1-LAND1.
<FS_DOC>-XCPDK = LS_LFA1-XCPDK.
ELSE.
MSG-TYPE = 'E'.
<FS_DOC>-ICONID = ICON_LED_RED.
CONCATENATE '供應商代碼' <FS_DOC>-HKONT '不存在!'
INTO MSG-MESSAGE.
APPEND MSG TO <FS_DOC>-RETURN.
ENDIF.
WHEN 'D'."客戶相關統馭科目
SELECT SINGLE * FROM KNA1 INTO LS_KNA1
WHERE KUNNR = <FS_DOC>-HKONT.
IF SY-SUBRC = 0.
<FS_DOC>-COUNTRY = LS_KNA1-LAND1.
<FS_DOC>-XCPDK = LS_KNA1-XCPDK.
ELSE.
MSG-TYPE = 'E'.
<FS_DOC>-ICONID = ICON_LED_RED.
CONCATENATE '客戶代碼' <FS_DOC>-HKONT '不存在!'
INTO MSG-MESSAGE.
APPEND MSG TO <FS_DOC>-RETURN.
ENDIF.
ENDCASE.
ELSE.
MSG-TYPE = 'E'.
<FS_DOC>-ICONID = ICON_LED_RED.
CONCATENATE '記賬代碼' <FS_DOC>-BSCHL '不存在!'
INTO MSG-MESSAGE.
APPEND MSG TO <FS_DOC>-RETURN.
ENDIF.
IF <FS_DOC>-PERIOD = ''."過帳期間
<FS_DOC>-PERIOD = <FS_DOC>-BUDAT+4(2).
ENDIF.
ENDLOOP.
ENDFORM. " CHECKVALID
*&---------------------------------------------------------------------*
*& FORM FILL_IT_ACCOUNTTAX
*&---------------------------------------------------------------------
*
* TEXT
*----------------------------------------------------------------------
*
* --> P1 TEXT
* <-- P2 TEXT
*---------------------------------------------------------------------*
FORM FILL_IT_ACCOUNTTAX TABLES PT_CURRENCYAMOUNT STRUCTURE BAPIACCR09
PT_ACCOUNTTAX STRUCTURE BAPIACTX09
USING P_ITEMNO TYPE I
CHANGING DOCLINE LIKE IT_DOC.
DATA: LT_TAX LIKE TABLE OF RTAX1U15 WITH HEADER LINE.

CALL FUNCTION 'CALCULATE_TAX_FROM_GROSSAMOUNT'
EXPORTING
I_BUKRS = DOCLINE-BUKRS "公司代碼
I_MWSKZ = DOCLINE-MWSKZ "稅碼
I_WAERS = DOCLINE-WAERS "幣種
I_WRBTR = DOCLINE-WRBTR "金額
TABLES
T_MWDAT = LT_TAX.
LOOP AT LT_TAX.
CLEAR PT_ACCOUNTTAX.
PT_ACCOUNTTAX-ITEMNO_ACC = P_ITEMNO + 1.
PT_ACCOUNTTAX-TAX_CODE = DOCLINE-MWSKZ.
PT_ACCOUNTTAX-ACCT_KEY = LT_TAX-KTOSL.
PT_ACCOUNTTAX-COND_KEY = LT_TAX-KSCHL.
PT_ACCOUNTTAX-TAXJURCODE = LT_TAX-TXJCD.
PT_ACCOUNTTAX-TAXJURCODE_DEEP = LT_TAX-TXJCD_DEEP.
PT_ACCOUNTTAX-TAXJURCODE_LEVEL = LT_TAX-TXJLV.
APPEND PT_ACCOUNTTAX.
CLEAR PT_CURRENCYAMOUNT.
PT_CURRENCYAMOUNT-ITEMNO_ACC = PT_ACCOUNTTAX-ITEMNO_ACC.
PT_CURRENCYAMOUNT-CURR_TYPE = '00'.
PT_CURRENCYAMOUNT-CURRENCY = DOCLINE-WAERS.
PT_CURRENCYAMOUNT-AMT_DOCCUR = LT_TAX-WMWST.
PT_CURRENCYAMOUNT-AMT_BASE = 100."DOCLINE-WRBTR.
APPEND PT_CURRENCYAMOUNT.

DOCLINE-WRBTR = LT_TAX-KAWRT.
EXIT.
ENDLOOP.
ENDFORM. " FILL_IT_ACCOUNTTAX


*&---------------------------------------------------------------------*
*& FORM TEMP_EXCEL_GET
*&---------------------------------------------------------------------*
* TCODE: SMW0 WEBRFC 的二進制數據
*----------------------------------------------------------------------*
* -->P_OBJID TYPE WWWDATA-OBJID
*---------------------------------------------------------------------*
FORM TEMP_EXCEL_GET USING P_OBJID TYPE WWWDATA-OBJID.
DATA:L_OBJDATA LIKE WWWDATATAB,
L_DESTINATION LIKE RLGRAP-FILENAME,
L_RC LIKE SY-SUBRC,
L_ERRTXT TYPE STRING.

DATA: L_FULLPATH TYPE STRING,
L_EXTENSION TYPE STRING,
L_FNAME LIKE RLGRAP-FILENAME,
" L_RC LIKE SY-SUBRC,
L_FORMKEY LIKE WWWDATATAB.

L_EXTENSION = P_OBJID.

PERFORM GET_FILE_NAME USING '.xls'
'Excel|*.xls'
CHANGING L_FULLPATH.
IF L_FULLPATH = SPACE.
MESSAGE '請選擇下載文件名' TYPE 'E'.
ELSE.
L_FNAME = L_FULLPATH.
SELECT SINGLE RELID OBJID
FROM WWWDATA
INTO CORRESPONDING FIELDS OF L_OBJDATA
WHERE RELID = 'MI'
AND OBJID = P_OBJID .

IF SY-SUBRC NE 0 OR L_OBJDATA-OBJID = SPACE.
MESSAGE E001(00) WITH '文件不存在!'.
ELSE.
CALL FUNCTION 'DOWNLOAD_WEB_OBJECT'
EXPORTING
KEY = L_OBJDATA
DESTINATION = L_FNAME
IMPORTING
RC = L_RC
CHANGING
TEMP = L_FNAME.
IF L_RC NE 0.
MESSAGE E001(00) WITH '文件下載失敗!'.
ENDIF.
ENDIF.
ENDIF.

ENDFORM. "TEMP_EXCEL_GET
*&---------------------------------------------------------------------*
*& FORM GET_FILE_NAME
*&--------------------------------------------------------------------*
* TEXT
*----------------------------------------------------------------------*
* -->P_EXTENSION TEXT
* -->P_FULLPATH TEXT
*----------------------------------------------------------------------*
FORM GET_FILE_NAME USING P_EXTENSION
P_FILE_FILTER
CHANGING P_FULLPATH.
DATA: L_FILENAME TYPE STRING,
L_PATH TYPE STRING,
L_FULLPATH TYPE STRING,
L_TITILE TYPE STRING,
L_INIT_DIR TYPE STRING.

CLEAR P_FULLPATH.

* L_TITILE = TEXT-T04.
* L_INIT_DIR = TEXT-T03.
CALL METHOD CL_GUI_FRONTEND_SERVICES=>FILE_SAVE_DIALOG
EXPORTING
WINDOW_TITLE = L_TITILE
DEFAULT_EXTENSION = '.xls'
INITIAL_DIRECTORY = L_INIT_DIR
PROMPT_ON_OVERWRITE = 'X'
FILE_FILTER = P_FILE_FILTER
CHANGING
FILENAME = L_FILENAME
PATH = L_PATH
FULLPATH = L_FULLPATH
EXCEPTIONS
CNTL_ERROR = 1
ERROR_NO_GUI = 2
NOT_SUPPORTED_BY_GUI = 3
OTHERS = 4.
IF SY-SUBRC <> 0.
MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
ENDIF.

P_FULLPATH = L_FULLPATH.
ENDFORM. " GET_FILE_NAME
*&---------------------------------------------------------------------*
*& Form FILL_COPA
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* -->P_LT_ACKEC text
* -->P_LT_ACKEV text
* -->P_ITEMNO text
* -->P_<FS_DOCLINE> text
*----------------------------------------------------------------------*
FORM FILL_COPA TABLES PT_ACKEC STRUCTURE BAPIACKEC9
PT_ACKEV STRUCTURE BAPIACKEV9
USING P_ITEMNO TYPE I
DOCLINE LIKE IT_DOC.
*COPA特性字段賦值
IF DOCLINE-PRCTR <> ''.
PT_ACKEC-ITEMNO_ACC = P_ITEMNO.
PT_ACKEC-FIELDNAME = 'PRCTR'.
PT_ACKEC-CHARACTER = DOCLINE-PRCTR.
APPEND PT_ACKEC.
ENDIF.
IF DOCLINE-KUNNR <> ''.
PT_ACKEC-ITEMNO_ACC = P_ITEMNO.
PT_ACKEC-FIELDNAME = 'KUNNR'.
PT_ACKEC-CHARACTER = DOCLINE-KUNNR.
APPEND PT_ACKEC.
ENDIF.
IF DOCLINE-KDGRP <> ''.
PT_ACKEC-ITEMNO_ACC = P_ITEMNO.
PT_ACKEC-FIELDNAME = 'KDGRP'.
PT_ACKEC-CHARACTER = DOCLINE-KDGRP.
APPEND PT_ACKEC.
ENDIF.
IF DOCLINE-VKORG <> ''.
PT_ACKEC-ITEMNO_ACC = P_ITEMNO.
PT_ACKEC-FIELDNAME = 'VKORG'.
PT_ACKEC-CHARACTER = DOCLINE-VKORG.
APPEND PT_ACKEC.
ENDIF.
IF DOCLINE-VTWEG <> ''.
PT_ACKEC-ITEMNO_ACC = P_ITEMNO.
PT_ACKEC-FIELDNAME = 'VTWEG'.
PT_ACKEC-CHARACTER = DOCLINE-VTWEG.
APPEND PT_ACKEC.
ENDIF.

IF DOCLINE-KMVKBU <> ''.
PT_ACKEC-ITEMNO_ACC = P_ITEMNO.
PT_ACKEC-FIELDNAME = 'KMVKBU'.
PT_ACKEC-CHARACTER = DOCLINE-KMVKBU.
APPEND PT_ACKEC.
ENDIF.
IF DOCLINE-WWZZS <> ''.
PT_ACKEC-ITEMNO_ACC = P_ITEMNO.
PT_ACKEC-FIELDNAME = 'WWZZS'.
PT_ACKEC-CHARACTER = DOCLINE-WWZZS.
APPEND PT_ACKEC.
ENDIF.
IF DOCLINE-BRSCH <> ''.
PT_ACKEC-ITEMNO_ACC = P_ITEMNO.
PT_ACKEC-FIELDNAME = 'BRSCH'.
PT_ACKEC-CHARACTER = DOCLINE-BRSCH.
APPEND PT_ACKEC.
ENDIF.
IF DOCLINE-SPART <> ''.
PT_ACKEC-ITEMNO_ACC = P_ITEMNO.
PT_ACKEC-FIELDNAME = 'SPART'.
PT_ACKEC-CHARACTER = DOCLINE-SPART.
APPEND PT_ACKEC.
ENDIF.
IF DOCLINE-KMVTNR <> ''.
PT_ACKEC-ITEMNO_ACC = P_ITEMNO.
PT_ACKEC-FIELDNAME = 'KMVTNR'.
PT_ACKEC-CHARACTER = DOCLINE-KMVTNR.
APPEND PT_ACKEC.
ENDIF.
IF DOCLINE-WERKS <> ''.
PT_ACKEC-ITEMNO_ACC = P_ITEMNO.
PT_ACKEC-FIELDNAME = 'WERKS'.
PT_ACKEC-CHARACTER = DOCLINE-WERKS.
APPEND PT_ACKEC.
ENDIF.
IF DOCLINE-MATNR <> ''.
PT_ACKEC-ITEMNO_ACC = P_ITEMNO.
PT_ACKEC-FIELDNAME = 'MATNR'.
PT_ACKEC-CHARACTER = DOCLINE-MATNR.
APPEND PT_ACKEC.
SELECT SINGLE EXTWG MATKL FROM MARA
INTO (DOCLINE-EXTWG,DOCLINE-KMMAKL)
WHERE MATNR = DOCLINE-MATNR.
PT_ACKEC-ITEMNO_ACC = P_ITEMNO.
PT_ACKEC-FIELDNAME = 'EXTWG'.
PT_ACKEC-CHARACTER = DOCLINE-EXTWG.
APPEND PT_ACKEC.
PT_ACKEC-ITEMNO_ACC = P_ITEMNO.
PT_ACKEC-FIELDNAME = 'KMMAKL'.
PT_ACKEC-CHARACTER = DOCLINE-KMMAKL.
APPEND PT_ACKEC.
ENDIF.
IF DOCLINE-WW01 <> ''.
PT_ACKEC-ITEMNO_ACC = P_ITEMNO.
PT_ACKEC-FIELDNAME = 'WW01'.
PT_ACKEC-CHARACTER = DOCLINE-WW01.
APPEND PT_ACKEC.
ENDIF.

*COPA值字段賦值* PT_ACKEV-ITEMNO_ACC = 1.* PT_ACKEV-FIELDNAME = 'VV210'.* PT_ACKEV-CURRENCY = 'CNY'.* PT_ACKEV-AMT_VALCOM = 100.* APPEND PT_ACKEV.ENDFORM. " FILL_COPA

相關文章
相關標籤/搜索