ABAP-FB01创建会计凭证BAPI

  1 FUNCTION zfm_fi_0003.
  2 *"----------------------------------------------------------------------
  3 *"*"本地接口:
  4 *"  IMPORTING
  5 *"     VALUE(IS_BKPF) TYPE  ZSFI_0005 OPTIONAL
  6 *"     VALUE(IV_TEST) TYPE  CHAR1 OPTIONAL
  7 *"  EXPORTING
  8 *"     VALUE(EV_TYPE) TYPE  CHAR1
  9 *"     VALUE(EV_MESSAGE) TYPE  CHAR255
 10 *"     VALUE(EV_BELNR) TYPE  BELNR_D
 11 *"  TABLES
 12 *"      TI_BSEG STRUCTURE  ZSFI_0006 OPTIONAL
 13 *"----------------------------------------------------------------------
 14 
 15   DATA:ls_documentheader    TYPE bapiache09,
 16        lt_currencyamount    TYPE TABLE OF bapiaccr09,
 17        ls_currencyamount    TYPE bapiaccr09,
 18        lt_accountgl         TYPE TABLE OF bapiacgl09,
 19        ls_accountgl         TYPE bapiacgl09,
 20        lt_accountreceivable TYPE TABLE OF bapiacar09,
 21        ls_accountreceivable TYPE bapiacar09,
 22        lt_accountpayable    TYPE TABLE OF bapiacap09,
 23        ls_accountpayable    TYPE bapiacap09,
 24        lt_return            TYPE TABLE OF bapiret2,
 25        lt_extension2        TYPE TABLE OF bapiparex,
 26        ls_extension2        TYPE bapiparex,
 27        ls_zsfi_0001         TYPE zsfi_0001,
 28        ls_return            TYPE bapiret2,
 29        lv_poper             TYPE t009b-poper,
 30        lv_waers             TYPE bkpf-waers.
 31 
 32   CLEAR ls_documentheader .
 33   REFRESH lt_currencyamount .
 34   CLEAR ls_currencyamount .
 35   REFRESH lt_accountgl .
 36   CLEAR ls_accountgl .
 37   REFRESH lt_accountreceivable .
 38   CLEAR ls_accountreceivable .
 39   REFRESH lt_accountpayable .
 40   CLEAR ls_accountpayable .
 41   REFRESH lt_return .
 42   CLEAR ls_return .
 43   CLEAR lv_poper .
 44   CLEAR lv_waers .
 45 
 46   LOOP AT ti_bseg ASSIGNING FIELD-SYMBOL(<fs_bseg>) .
 47 
 48     CALL FUNCTION 'CONVERSION_EXIT_ALPHA_INPUT'
 49       EXPORTING
 50         input  = <fs_bseg>-hkont
 51       IMPORTING
 52         output = <fs_bseg>-hkont.
 53 
 54     CALL FUNCTION 'CONVERSION_EXIT_ALPHA_INPUT'
 55       EXPORTING
 56         input  = <fs_bseg>-kostl
 57       IMPORTING
 58         output = <fs_bseg>-kostl.
 59 
 60     CALL FUNCTION 'CONVERSION_EXIT_ALPHA_INPUT'
 61       EXPORTING
 62         input  = <fs_bseg>-prctr
 63       IMPORTING
 64         output = <fs_bseg>-prctr.
 65 
 66     CALL FUNCTION 'CONVERSION_EXIT_ALPHA_INPUT'
 67       EXPORTING
 68         input  = <fs_bseg>-aufnr
 69       IMPORTING
 70         output = <fs_bseg>-aufnr.
 71 
 72     CALL FUNCTION 'CONVERSION_EXIT_ALPHA_INPUT'
 73       EXPORTING
 74         input  = <fs_bseg>-anln1
 75       IMPORTING
 76         output = <fs_bseg>-anln1.
 77 
 78     CALL FUNCTION 'CONVERSION_EXIT_ALPHA_INPUT'
 79       EXPORTING
 80         input  = <fs_bseg>-lifnr
 81       IMPORTING
 82         output = <fs_bseg>-lifnr.
 83 
 84     CALL FUNCTION 'CONVERSION_EXIT_ALPHA_INPUT'
 85       EXPORTING
 86         input  = <fs_bseg>-kunnr
 87       IMPORTING
 88         output = <fs_bseg>-kunnr.
 89 
 90   ENDLOOP .
 91 
 92   IF is_bkpf-monat IS INITIAL .
 93 
 94     CALL FUNCTION 'G_PERIOD_GET'
 95       EXPORTING
 96         company                        = is_bkpf-bukrs
 97         date                           = is_bkpf-budat
 98 *       GLOBAL_COMPANY                 = ' '
 99 *       LEDGER                         = ' '
100         variant                        = 'Z1'
101       IMPORTING
102 *       ANZBP                          =
103 *       ANZSP                          =
104         period                         = lv_poper
105 *       YEAR                           =
106 *       VARIANT                        =
107       EXCEPTIONS
108         ledger_not_assigned_to_company = 1
109         period_not_defined             = 2
110         variant_not_defined            = 3
111         parameter_error                = 4
112         OTHERS                         = 5.
113 
114     is_bkpf-monat = lv_poper .
115 
116   ENDIF .
117 
118   ls_documentheader-header_txt = is_bkpf-bktxt.
119   ls_documentheader-comp_code = is_bkpf-bukrs.
120   ls_documentheader-doc_date = is_bkpf-bldat.
121   ls_documentheader-pstng_date = is_bkpf-budat.
122   ls_documentheader-fisc_year = is_bkpf-gjahr.
123   ls_documentheader-fis_period = is_bkpf-monat.
124   ls_documentheader-doc_type = is_bkpf-blart.
125   ls_documentheader-ref_doc_no = is_bkpf-xblnr.
126   ls_documentheader-bus_act = 'RFBU'.
127   ls_documentheader-username   = sy-uname.
128 
129   SELECT SINGLE waers INTO lv_waers FROM t001 WHERE bukrs = is_bkpf-bukrs.
130   IF lv_waers IS INITIAL.
131     lv_waers = 'CNY'.
132   ENDIF.
133 
134   LOOP AT ti_bseg.
135 
136     IF ti_bseg-lifnr IS NOT INITIAL.
137 
138       CLEAR ls_accountpayable .
139       ls_accountpayable-itemno_acc = ti_bseg-buzei .
140       ls_accountpayable-vendor_no = ti_bseg-lifnr.
141       ls_accountpayable-sp_gl_ind = ti_bseg-umskz.
142       ls_accountpayable-item_text = ti_bseg-sgtxt.
143       ls_accountpayable-profit_ctr = ti_bseg-prctr.
144       ls_accountpayable-alloc_nmbr = ti_bseg-zuonr.
145       ls_accountpayable-bline_date = ti_bseg-zfbdt.
146       ls_accountpayable-gl_account = ti_bseg-hkont .
147       ls_accountpayable-bus_area = ti_bseg-gsber .
148       APPEND ls_accountpayable TO lt_accountpayable .
149 
150     ELSEIF ti_bseg-kunnr IS NOT INITIAL.
151 
152       CLEAR ls_accountreceivable .
153       ls_accountreceivable-itemno_acc = ti_bseg-buzei.
154       ls_accountreceivable-customer = ti_bseg-kunnr.
155       ls_accountreceivable-sp_gl_ind = ti_bseg-umskz.
156       ls_accountreceivable-item_text = ti_bseg-sgtxt.
157       ls_accountreceivable-profit_ctr = ti_bseg-prctr.
158       ls_accountreceivable-alloc_nmbr = ti_bseg-zuonr.
159       ls_accountreceivable-bline_date = ti_bseg-zfbdt.
160       ls_accountreceivable-bus_area = ti_bseg-gsber .
161       ls_accountreceivable-gl_account = ti_bseg-hkont .
162       APPEND ls_accountreceivable TO lt_accountreceivable .
163 
164     ELSE .
165 
166       CLEAR ls_accountgl .
167       ls_accountgl-itemno_acc = ti_bseg-buzei.
168       IF ti_bseg-anln1 IS INITIAL.
169         ls_accountgl-acct_type = 'S'.
170         ls_accountgl-gl_account = ti_bseg-hkont.
171       ELSE.
172         ls_accountgl-acct_type = 'A'.
173         ls_accountgl-asset_no = ti_bseg-anln1.
174         ls_accountgl-sub_number = '0000'.
175         ls_accountgl-gl_account = ti_bseg-hkont.
176       ENDIF.
177       ls_accountgl-quantity = ti_bseg-menge.
178       ls_accountgl-costcenter = ti_bseg-kostl.
179       ls_accountgl-profit_ctr = ti_bseg-prctr.
180       ls_accountgl-orderid = ti_bseg-aufnr.
181       ls_accountgl-item_text = ti_bseg-sgtxt.
182       ls_accountgl-alloc_nmbr = ti_bseg-zuonr.
183       ls_accountgl-bus_area = ti_bseg-gsber.
184       ls_accountgl-orderid = ti_bseg-aufnr .
185       APPEND ls_accountgl TO lt_accountgl .
186 
187     ENDIF .
188 
189     IF is_bkpf-waers = lv_waers OR is_bkpf-waers = '' . "人民币
190 
191       CLEAR ls_currencyamount .
192       ls_currencyamount-itemno_acc = ti_bseg-buzei.
193       ls_currencyamount-currency = lv_waers.
194       IF ti_bseg-shkzg = 'H' AND ti_bseg-wrbtr > 0 .
195         ls_currencyamount-amt_doccur = 0 - ti_bseg-wrbtr .
196       ELSE .
197         ls_currencyamount-amt_doccur = ti_bseg-wrbtr.
198       ENDIF .
199       APPEND ls_currencyamount TO lt_currencyamount.
200 
201     ELSE.
202       CLEAR ls_currencyamount .
203       ls_currencyamount-itemno_acc = ti_bseg-buzei.
204       ls_currencyamount-curr_type  = '00'.  "凭证货币
205       ls_currencyamount-currency = is_bkpf-waers.
206       IF ti_bseg-shkzg = 'H' AND ti_bseg-wrbtr > 0 .
207         ls_currencyamount-amt_doccur = ti_bseg-wrbtr * - 1 .
208       ELSE .
209         ls_currencyamount-amt_doccur = ti_bseg-wrbtr.
210       ENDIF .
211       APPEND ls_currencyamount TO lt_currencyamount.
212       CLEAR ls_currencyamount.
213       ls_currencyamount-itemno_acc = ti_bseg-buzei.
214       ls_currencyamount-curr_type = '10'.  "本位币
215       ls_currencyamount-currency = lv_waers.
216       IF ti_bseg-shkzg = 'H' AND ti_bseg-dmbtr > 0 .
217         ls_currencyamount-amt_doccur = ti_bseg-dmbtr * - 1 .
218       ELSE .
219         ls_currencyamount-amt_doccur = ti_bseg-dmbtr.
220       ENDIF .
221       APPEND ls_currencyamount TO lt_currencyamount.
222     ENDIF.
223 
224     CLEAR ls_extension2 .
225     CLEAR ls_zsfi_0001 .
226     ls_zsfi_0001-posnr = ti_bseg-buzei."凭证行项目
227     ls_zsfi_0001-bschl = ti_bseg-bschl."凭证行项目过账码
228     ls_zsfi_0001-rstgr = ti_bseg-rstgr."原因代码增强
229     ls_zsfi_0001-umskz = ti_bseg-umskz. "凭证行项目特别总账标识
230     ls_zsfi_0001-zzxmhsbm = ti_bseg-zzxmhsbm.
231     ls_zsfi_0001-wdate = ti_bseg-wdate."签发日期
232     ls_zsfi_0001-anbwa = ti_bseg-anbwa .
233     ls_zsfi_0001-xnegp = ti_bseg-xnegp .
234     ls_extension2-structure  = 'ZSFI_0001'.
235     ls_extension2-valuepart1 = ls_zsfi_0001.
236     APPEND ls_extension2 TO lt_extension2 .
237 
238   ENDLOOP .
239 
240   IF iv_test IS INITIAL .
241 
242     CALL FUNCTION 'BAPI_ACC_DOCUMENT_POST'
243       EXPORTING
244         documentheader    = ls_documentheader
245 *       CUSTOMERCPD       =
246 *       CONTRACTHEADER    =
247 *     IMPORTING
248 *       OBJ_TYPE          =
249 *       OBJ_KEY           =
250 *       OBJ_SYS           =
251       TABLES
252         accountgl         = lt_accountgl
253         accountreceivable = lt_accountreceivable
254         accountpayable    = lt_accountpayable
255 *       ACCOUNTTAX        =
256         currencyamount    = lt_currencyamount
257 *       CRITERIA          =
258 *       VALUEFIELD        =
259 *       EXTENSION1        =
260         return            = lt_return
261 *       PAYMENTCARD       =
262 *       CONTRACTITEM      =
263         extension2        = lt_extension2
264 *       REALESTATE        =
265 *       ACCOUNTWT         =
266       .
267 
268     DELETE lt_return WHERE type = 'S' AND message_v2 IS INITIAL.
269     READ TABLE lt_return INTO ls_return WITH KEY type = 'S'.
270     IF sy-subrc = 0.
271 
272       ev_type = 'S'.
273       ev_message = ls_return-message.
274       ev_belnr = ls_return-message_v2(10).
275       CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'
276         EXPORTING
277           wait = 'X'.
278 
279     ENDIF .
280 
281     CHECK ev_belnr IS INITIAL.
282     ev_type = 'E'.
283     ev_message = '生成凭证时出错:'.
284     CALL FUNCTION 'BAPI_TRANSACTION_ROLLBACK'.
285 
286     DELETE lt_return WHERE id = 'RW' AND number = '609'.
287     DELETE lt_return WHERE type = 'W'.
288     CHECK  lt_return IS NOT INITIAL.
289 
290     SORT lt_return[] BY message.
291     DELETE ADJACENT DUPLICATES FROM lt_return COMPARING message.
292     LOOP AT lt_return INTO ls_return WHERE type = 'A' OR type = 'E' OR type = 'X'.
293       ev_type = 'E'.
294       ev_message = ev_message && ls_return-message && ';'.
295     ENDLOOP.
296 
297   ELSE .
298 
299     CALL FUNCTION 'BAPI_ACC_DOCUMENT_CHECK'
300       EXPORTING
301         documentheader    = ls_documentheader
302 *       CUSTOMERCPD       =
303 *       CONTRACTHEADER    =
304       TABLES
305         accountgl         = lt_accountgl
306         accountreceivable = lt_accountreceivable
307         accountpayable    = lt_accountpayable
308 *       ACCOUNTTAX        =
309         currencyamount    = lt_currencyamount
310 *       CRITERIA          =
311 *       VALUEFIELD        =
312 *       EXTENSION1        =
313         return            = lt_return
314 *       PAYMENTCARD       =
315 *       CONTRACTITEM      =
316         extension2        = lt_extension2
317 *       REALESTATE        =
318 *       ACCOUNTWT         =
319       .
320 
321     ev_type = 'S'.
322     ev_message = '凭证检查成功'.
323     SORT lt_return BY message.
324     DELETE ADJACENT DUPLICATES FROM lt_return COMPARING message.
325     LOOP AT lt_return INTO ls_return WHERE type = 'A' OR type = 'E' OR type = 'X'.
326       ev_type = 'E'.
327       ev_message = ev_message && ls_return-message && ';'.
328     ENDLOOP.
329 
330   ENDIF .
331 
332 ENDFUNCTION.
posted @ 2023-07-11 15:47  TigerMan  阅读(184)  评论(0编辑  收藏  举报