FORM frm_create_ab.
*目的:财务分别做预收款和开billing,需要后续做清账对冲DZ和RV.
*逻辑:由于只需要模拟人工F-32做清账,只需要根据XBLNR找到借贷方进行对冲即可,因此程序非常简单,核心传递的值为it_ftclear-selfd.
*由于VF01开票中已经做了拆分增强,所以billing的金额和DZ的金额是无法对应上的,因此无法采用标准自动清账配置,必须开发实现。
*函数:POSTING_INTERFACE_CLEARING.
*POSTING_INTERFACE_CLEARING并非纯函数,本质上是call bdc.
*examples: http://www.se80.co.uk/sapfms/p/post/posting_interface_clearing.htm
* https://blogs.sap.com/2013/06/11/fb05-through-fm-postinginterfaceclearing/
* https://archive.sap.com/discussions/thread/1873193
* https://archive.sap.com/discussions/thread/2005974
* http://blog.csdn.net/chfeijj/article/details/51443437
* http://blog.csdn.net/zhongguomao/article/details/50214341
*清账逻辑:取出RV、DZ、DG,将XBLNR字段相同的行组合在一起,清账.
*处理异常,事务代码:F-32.
*重点需要解决的两个问题:1. 清账函数的使用;
* 2. 清账数据的准备(按照公司业务逻辑).
DATA: it_blntab TYPE TABLE OF blntab WITH HEADER LINE,
it_ftclear TYPE TABLE OF ftclear WITH HEADER LINE,
it_ftpost TYPE TABLE OF ftpost WITH HEADER LINE,
it_fttax TYPE TABLE OF fttax WITH HEADER LINE.
DATA: lt_bsad_clearing TYPE TABLE OF bsad WITH HEADER LINE. "客户未清项表.
DATA: lt_kunnr TYPE TABLE OF zthy_ecc_kunnr WITH HEADER LINE.
DATA: lv_ftclear_agbuk TYPE bukrs. "清账公司代码.
"仅电商平台用自动清账程序,其他平台人工处理.
SELECT * INTO CORRESPONDING FIELDS OF TABLE lt_kunnr
FROM zthy_ecc_kunnr.
"每次处理一个客户的清账业务.
*宏.
*SELFD = Field Name from the Document Index
*SELVON = Input Field for Search Criterion for Selecting Open Items.
DEFINE populate_ftclear.
it_ftclear-agkoa = &1. "科目类型,based on account type, Vendor or Customer for Out Going / Incoming Payments.
it_ftclear-agbuk = &2. "公司代码.
it_ftclear-xnops = 'X'. "标准未清项目.
it_ftclear-AGUMS = 'A'. "特别总账未清项目.
it_ftclear-selfd = 'XBLNR'. "凭证索引中的字段名(使用该字段搜索用来做对冲的借贷方).
it_ftclear-selvon = &3. "Input Field for Search Criterion for Selecting Open Items.
append it_ftclear.
END-OF-DEFINITION.
DEFINE populate_ftpost.
it_ftpost-stype = &1. "K为header,P为item.
it_ftpost-count = &2. "凭证抬头或行项目的计数器(记帐界面).
it_ftpost-fnam = &3. "BDC 字段名.
it_ftpost-fval = &4. "BDC 字段值.
append it_ftpost.
END-OF-DEFINITION.
*1.Start of call transaction: SHDB.
CALL FUNCTION 'POSTING_INTERFACE_START'
EXPORTING
i_client = sy-mandt
i_function = 'C' "B= BDC, C= Call Trans.
i_mode = 'N' "N – no screen, A – all screen, E – Error –> Mode we use in Call transaction
i_update = 'S' "S: 数据更新完成后执行下一个操作
i_keep = 'X' "用于已处理会话的队列删除标志.
i_user = sy-uname
EXCEPTIONS
client_incorrect = 1
function_invalid = 2
group_name_missing = 3
mode_invalid = 4
update_invalid = 5
OTHERS = 6.
IF sy-subrc <> 0.
* Implement suitable error handling here
ENDIF.
*2.Fill data for SHDB.
"调用两次,一次借方,一次贷方.
populate_ftclear 'D' '1000' 'SA00012090'.
*每执行一次添加一条清账行项目.
*如果借贷方分录不是正好等于两条,则应使用loop追加清账条目.
populate_ftpost: 'K' 1 'BKPF-BUKRS' '1000',
'K' 1 'BKPF-BLART' 'AB',
'K' 1 'BKPF-BLDAT' sy-datum,
'K' 1 'BKPF-BUDAT' sy-datum,
'K' 1 'BKPF-XBLNR' 'SA00012090', "清账参考号.
'K' 1 'BKPF-WAERS' 'CNY'.
**由于根据参考号(XBLNR)自动搜索清账的方式进行清账,因此行项目级别均不需要传值.
** 'P' 1 'RF05A-NEWBS' '17',
* 'P' 1 'RF05A-NEWBS' '18', "通过FB05进行预收和应收对冲清账时,记账码为18(手工做F-32为17)
* 'P' 1 'RF05A-NEWUM' '', "Special G/L indicator.
** 'P' 1 'BSEG-HKONT' '0011220001',
* 'P' 1 'BSEG-KUNNR' '0011000000', "统驭科目.
** 'P' 1 'BSEG-WRBTR' '347.09', "程序根据搜索条件自动搜出金额,无需指定.
* 'P' 1 'BSEG-ZFBDT' sy-datum,
* 'P' 1 'BSEG-SGTXT' '清账测试'.
*
populate_ftpost: 'K' 2 'BKPF-BUKRS' '1000',
'K' 2 'BKPF-BLART' 'AB',
'K' 2 'BKPF-BLDAT' sy-datum,
'K' 2 'BKPF-BUDAT' sy-datum,
'K' 2 'BKPF-XBLNR' 'SA00012090', "清账参考号.
'K' 2 'BKPF-WAERS' 'CNY'.
**由于根据参考号(XBLNR)自动搜索清账的方式进行清账,因此行项目级别均不需要传值.
* 'P' 2 'RF05A-NEWBS' '09',
* 'P' 2 'RF05A-NEWUM' 'A', "Special G/L indicator.
** 'P' 2 'BSEG-HKONT' '0022030001',
* 'P' 2 'BSEG-KUNNR' '0011000000', "统驭科目.
** 'P' 2 'BSEG-WRBTR' '347.09', "程序根据搜索条件自动搜出金额,无需指定.
* 'P' 2 'BSEG-ZFBDT' sy-datum,
* 'P' 2 'BSEG-SGTXT' '清账测试'.
*SE16查询表BSAD按BUKRS、AUGBL、GJAHR查询,可查询到清账凭证行及原始行信息.
*3.Process BDC.
*每次只能传入一个清账分录.
*AUGLV Purpose
*AUSGZAHL Outgoing payment
*EINGZAHL Incoming payment
*GUTSCHRI Credit memo(W为承兑汇票,可能比较特殊)
*UMBUCHNG Transfer posting with clearing
*Note the following :
*---------------------------------------
*The data is marked with a record type (header, code = K; item, code = P) and counter. – LCF_STYPE_K and LCF_STYPE_S
*Up to 950 line items can be transferred.
*The data is transferred with the field name and field value.
*The data must be transferred in the following sequence:
*Document header data ( K 1)
*Document item 1 ( P 1)
*Document item 2 ( P 2) etc.
*The field names are the same as those used on the entry screen (e.g. ‘BKPF-WAERS’, ‘BSEG-WRBTR’, ‘COBL-GSBER’…).
*Suppose if it Cost Center / Profit Center – its COBL-KOSTL and COBL-PRCTR
*The footer data can be transferred using the following fields:
*Posting key: RF05A-NEWBS or BSEG-BSCHL
*Account number: RF05A-NEWKO or BSEG-KONTO
* or BSEG-KUNNR (for customer accounts)
* or BSEG-LIFNR (for vendor accounts)
* or BSEG-HKONT (for G/L accounts)
*Special G/L indicator: RF05A-NEWUM or BSEG-UMSKZ
*Company code (for intercompany posting):
* RF05A-NEWBK or BSEG-BUKRS.
*The field names are analyzed by the function module so that the data can be assigned to the right screens.
*Field names for one-time data or data for an alternative payer must start with ‘BSEC-‘, for example (see screen SAPLFCPD 0100).
*Processing with ‘Call Transaction … Using ‘
*
*If processing takes place with ‘Call Transaction .. Using’, the initiator can see whether processing was successful by looking at the export parameter’SUBRC’.
*
*SUBRC = 0 means that the document was posted. The document number is stored in table T_BLNTAB. For intercompany documents, several document numbers are stored here.
*
*SUBRC <> 0 means that the document could not be posted. Information concerning the error which has arisen is returned via the message fields MSGID, MSGNO, etc.
*
*Support of transaction ‘FBB1’
*
*If you want to access transaction FBB1, you should note the following:
*
*Enter the foreign currency key in field BKPF-WAERS.
*In this transaction you should enter the local currency amount in field BSEG-WRBTR for each document item.
CALL FUNCTION 'POSTING_INTERFACE_CLEARING' "Post with clearing (FB05) using internal posting interface
EXPORTING
i_auglv = 'AUSGZAHL' " t041a-auglv Clearing Transaction
i_tcode = 'FB05' " sy-tcode Transaction code
i_sgfunct = space " rfipi-sgfunct Different FUNCT function
IMPORTING
e_msgid = sy-msgid " sy-msgid Message ID (only for Call Trans. ..Using)
e_msgno = sy-msgno " sy-msgno Message number (only for Call Trans. ..Using)
e_msgty = sy-msgty " sy-msgty Message category (only for Call Trans. ..Using)
e_msgv1 = sy-msgv1 " sy-msgv1 Message variable 1 (only for Call Trans. ..Using)
e_msgv2 = sy-msgv2 " sy-msgv2 Message variable 2 (only for Call Trans. ..Using)
e_msgv3 = sy-msgv3 " sy-msgv3 Message variable 3 (only for Call Trans. ..Using)
e_msgv4 = sy-msgv4 " sy-msgv4 Message variable 4 (only for Call Trans. ..Using)
TABLES
t_blntab = it_blntab " blntab Table of the document numbers (only for Call Trans ..)
t_ftclear = it_ftclear " ftclear Clearing data
t_ftpost = it_ftpost " ftpost Document header and item data
t_fttax = it_fttax " fttax Taxes
EXCEPTIONS
clearing_procedure_invalid = 1
clearing_procedure_missing = 2
table_t041a_empty = 3
transaction_code_invalid = 4
amount_format_error = 5
too_many_line_items = 6
company_code_invalid = 7
screen_not_found = 8
no_authorization = 9
OTHERS = 10.
IF sy-subrc EQ 0.
"清账成功,清账凭证信息存储在it_blntab中.
ELSE.
"清账失败,查看importing中的消息.
ENDIF.
*4.After bdc processing.
CALL FUNCTION 'POSTING_INTERFACE_END'
EXPORTING
i_bdcimmed = 'X'
EXCEPTIONS
session_not_processable = 1
OTHERS = 2.
*5.log record.
ENDFORM.