SAP-ABAP 发出商品转主营业务成本平台

&---------------------------------------------------------------------
*& Report ZCOPP002
&---------------------------------------------------------------------

REPORT zcopp002 MESSAGE-ID 00.

INCLUDE zcopp002_top.
INCLUDE zcopp002_sel.
INCLUDE zcopp002_frm.

START-OF-SELECTION.

AUTHORITY-CHECK OBJECT ‘F_BKPF_BUK’ ID ‘BUKRS’ FIELD p_bukrs
ID ‘ACTVT’ DUMMY.
IF sy-subrc <> 0.
MESSAGE e208 WITH ‘没有公司代码权限’.
ENDIF.

p_monat = s_fkdat[ 1 ]-low+4(2).

"取自建表数据
PERFORM frm_get_data.

**显示数据
PERFORM frm_alv_data.

END-OF-SELECTION.


&---------------------------------------------------------------------
*& 包含 ZCOPP002_TOP
&---------------------------------------------------------------------

------------------------------------------------------------

  • 数据声明 - 开始
    ------------------------------------------------------------

------------------------------------------------------------

  • DESC:INCLUDES文件 *
    ------------------------------------------------------------
    *INCLUDE .

------------------------------------------------------------

  • DESC: 表/ 结构 / 视图的声明 *
    ------------------------------------------------------------

TABLES: sscrfields, ztco006, ztco005, vbrk, vbrp.

------------------------------------------------------------

  • DESC: 定义所需要的 TYPES *
    ------------------------------------------------------------
    *TYPE-POOLS: slis.

TYPES: BEGIN OF ty_out.
INCLUDE TYPE ztco006.
TYPES:
butxt TYPE t001-butxt,
sltext TYPE cskt-ltext,
rltext TYPE cskt-ltext,
bkbez TYPE t025t-bkbez,
"cellcolor TYPE lvc_t_scol, "行颜色控制段
stat TYPE char4,
sel TYPE char1,
mess TYPE bapi_msg, "执行信息
END OF ty_out.
------------------------------------------------------------

  • DESC:定义所需要的内表 / 结构 *
    ------------------------------------------------------------
    DATA: gt_out TYPE TABLE OF ty_out,
    wa_out LIKE LINE OF gt_out,
    gt_ztco006 TYPE TABLE OF ztco006,
    wa_ztco006 LIKE LINE OF gt_ztco006,
    gt_ztco005 TYPE TABLE OF ztco005,
    wa_ztco005 LIKE LINE OF gt_ztco005.

------------BAPI相关定义------------------
DATA :gw_documentheader TYPE bapiache09, "凭证头
gt_currencyamount TYPE TABLE OF bapiaccr09 WITH HEADER LINE, "金额行
gt_accountgl TYPE TABLE OF bapiacgl09 WITH HEADER LINE, "总账
gt_return TYPE TABLE OF bapiret2 WITH HEADER LINE, "扩充
gt_extension2 TYPE TABLE OF bapiparex WITH HEADER LINE, "返回 "
gw_zsfi_exten TYPE zsfi004, "扩充结构
gt_accountreceivable TYPE TABLE OF bapiacar09 WITH HEADER LINE, "应收
gt_accountpayable TYPE TABLE OF bapiacap09 WITH HEADER LINE, "应付
gt_criteria TYPE TABLE OF bapiackec9 WITH HEADER LINE.

**–ALV用
DATA: gt_events TYPE slis_t_event,

  •  gw_reprep   TYPE  slis_reprep_id,
    gw_layout   TYPE  lvc_s_layo,
    gw_settings TYPE  lvc_s_glay,
    wa_fieldcat TYPE  lvc_s_fcat,
    gt_fieldcat TYPE  lvc_t_fcat,
    gt_drop     TYPE  lvc_t_drop,
    wa_drop     TYPE  lvc_s_drop,
    wa_sort     TYPE  slis_sortinfo_alv,
    gt_sort     TYPE  slis_t_sortinfo_alv,
    gt_header   TYPE slis_t_listheader,
    wa_header   TYPE slis_listheader.
    

------------------------------------------------------------

  • DESC: 定义变/常量 *
    ------------------------------------------------------------
    DATA: gv_len TYPE i,
    gv_flag TYPE char1.
    DATA: p_monat TYPE monat,
    g_waers TYPE waers."公司代码本位币
    DATA: gv_posnr TYPE buzei.

*CONSTANTS: c_zgxc_hkont TYPE hkont VALUE ‘6601000001’,

  •       c_lwcb_hkont TYPE hkont VALUE '6601000002',
    
  •       c_gjj_hkont  TYPE hkont VALUE '6601000017',
    
  •       c_olda_hkont TYPE hkont VALUE '6601000025',
    
  •       c_medi_hkont TYPE hkont VALUE '6601000026',
    
  •       c_emij_hkont TYPE hkont VALUE '6601000027',
    
  •       c_mate_hkont TYPE hkont VALUE '6601000028',
    
  •       c_unem_hkont TYPE hkont VALUE '6601000029'.
    

------------------------------------------------------------

  • DESC: 定义屏幕号变量 *
    ------------------------------------------------------------
    *DATA: "sub(3) TYPE c VALUE ‘100’, "初始屏幕的屏幕号变量
  •  subrc_main(3) TYPE c VALUE '100',   "显示平台初始屏幕下面的屏幕号变量
    
  •  sub_target(3) TYPE c VALUE '221'.    "下半屏幕下得屏幕号变量
    

------------------------------------------------------------

  • DESC: 定义表条目变量和表控制变量 *
    ------------------------------------------------------------
    *CONTROLS: triptab TYPE TABSTRIP.
    --------------------------------------------------------------------
  • DESC: 定义类 *
    --------------------------------------------------------------------
    ------------------------------------------------------------
  • 数据定义 - 结束 *
    ------------------------------------------------------------

&---------------------------------------------------------------------
*& 包含 ZCOPP002_SEL
&---------------------------------------------------------------------
------------------------------------------------------------

  • DESC: 定义选择屏幕 —开始 *
    ------------------------------------------------------------
    SELECTION-SCREEN: FUNCTION KEY 2. "激活

SELECTION-SCREEN: BEGIN OF BLOCK blk0 WITH FRAME TITLE TEXT-001.
PARAMETERS: p_bukrs TYPE bukrs OBLIGATORY DEFAULT ‘1100’, " File location Input
p_gjahr TYPE gjahr OBLIGATORY.

SELECT-OPTIONS: s_fkdat FOR vbrk-fkdat OBLIGATORY NO-EXTENSION,
s_aubel FOR vbrp-aubel,
s_aupos FOR vbrp-aupos.
SELECTION-SCREEN END OF BLOCK blk0.

------------------------------------------------------------

  • DESC: 定义选择屏幕 —结束 *
    ------------------------------------------------------------
    INITIALIZATION.
    DATA: lv_date TYPE sy-datum.
    CALL FUNCTION ‘FIMA_DATE_CREATE’
    EXPORTING
    i_date = sy-datum
    i_months = -1
    i_set_last_day_of_month = ‘X’
    IMPORTING
    e_date = lv_date.
    p_gjahr = lv_date(4).
    REFRESH: s_fkdat.
    s_fkdat[] = VALUE #(
    ( sign = ‘I’ option = ‘BT’ low = |{ lv_date(6) }01| high = lv_date )
    ).

sscrfields-functxt_02 = icon_history && ‘历史凭证’.

AT SELECTION-SCREEN.
CASE sscrfields-ucomm.
WHEN ‘FC02’."历史凭证
SUBMIT zcorp003 WITH p_bukrs = p_bukrs
WITH p_gjahr = p_gjahr
VIA SELECTION-SCREEN AND RETURN.
ENDCASE.


&---------------------------------------------------------------------
*& 包含 ZCOPP002_FRM
&---------------------------------------------------------------------
&---------------------------------------------------------------------
*& Form frm_alv_data
&---------------------------------------------------------------------
*& text
&---------------------------------------------------------------------
*& --> p1 text
*& <-- p2 text
&---------------------------------------------------------------------
FORM frm_alv_data .
REFRESH: gt_fieldcat.
CLEAR: gw_layout,gw_settings.
gw_layout-cwidth_opt = ‘X’.
gw_layout-zebra = ‘X’.

  • gw_layout-box_fname = ‘SEL’.

  • gw_layout-stylefname = ‘EDITMARK’.

  • gw_settings-edt_cll_cb = ‘X’.

PERFORM frm_fill_dieldcat.

SORT gt_out BY vbeln posnr.

CALL FUNCTION ‘REUSE_ALV_GRID_DISPLAY_LVC’
EXPORTING

  • I_INTERFACE_CHECK        = ' '
    
  • I_BYPASSING_BUFFER       =
    
  • I_BUFFER_ACTIVE          =
    i_callback_program       = sy-repid
    i_callback_pf_status_set = 'SET_PF_STATUS'
    i_callback_user_command  = 'USER_COMMAND'
    
  • I_CALLBACK_TOP_OF_PAGE   = ' '
    
  • I_CALLBACK_HTML_TOP_OF_PAGE       = ' '
    
  • I_CALLBACK_HTML_END_OF_LIST       = ' '
    
  • I_STRUCTURE_NAME         =
    
  • I_BACKGROUND_ID          = ' '
    
  • i_grid_title             = gv_title
    
  • i_grid_settings          = gw_settings
    is_layout_lvc            = gw_layout
    it_fieldcat_lvc          = gt_fieldcat
    
  • it_excluding             = gt_excluding
    
  • IT_SPECIAL_GROUPS_LVC    =
    
  • IT_SORT_LVC              =
    
  • IT_FILTER_LVC            =
    
  • IT_HYPERLINK             =
    
  • IS_SEL_HIDE              =
    i_default                = 'X'
    i_save                   = 'A'
    
  • IS_VARIANT               =
    
  • IT_EVENTS                =
    
  • IT_EVENT_EXIT            =
    
  • IS_PRINT_LVC             =
    
  • IS_REPREP_ID_LVC         =
    
  • I_SCREEN_START_COLUMN    = 0
    
  • I_SCREEN_START_LINE      = 0
    
  • I_SCREEN_END_COLUMN      = 0
    
  • I_SCREEN_END_LINE        = 0
    
  • I_HTML_HEIGHT_TOP        =
    
  • I_HTML_HEIGHT_END        =
    
  • IT_ALV_GRAPHICS          =
    
  • IT_EXCEPT_QINFO_LVC      =
    
  • IR_SALV_FULLSCREEN_ADAPTER        =
    
  • IMPORTING
  • E_EXIT_CAUSED_BY_CALLER  =
    
  • ES_EXIT_CAUSED_BY_USER   =
    
    TABLES
    t_outtab = gt_out
    EXCEPTIONS
    program_error = 1
    OTHERS = 2.
    IF sy-subrc <> 0.
  • Implement suitable error handling here
    MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
    WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
    ENDIF.

ENDFORM.
FORM set_pf_status USING rt_extab TYPE slis_t_extab.

REFRESH: rt_extab.
SET PF-STATUS ‘STANDARD’ EXCLUDING rt_extab.

ENDFORM.
FORM user_command USING r_ucomm LIKE sy-ucomm
rs_selfield TYPE slis_selfield.

CASE r_ucomm.
WHEN ‘ZPOST’.
PERFORM frm_post_document.

WHEN 'ZREVERSE'.
  PERFORM frm_reverse_document.

WHEN OTHERS.

ENDCASE.

rs_selfield-refresh = ‘X’.
rs_selfield-row_stable = ‘X’.
rs_selfield-col_stable = ‘X’.
ENDFORM.
&---------------------------------------------------------------------
*& Form frm_fill_dieldcat
&---------------------------------------------------------------------
*& text
&---------------------------------------------------------------------
*& --> p1 text
*& <-- p2 text
&---------------------------------------------------------------------
FORM frm_fill_dieldcat .
DEFINE def_fcat.
CLEAR: wa_fieldcat.
wa_fieldcat-fieldname = &1.
wa_fieldcat-coltext = &2.
wa_fieldcat-ref_field = &3.
wa_fieldcat-ref_table = &4.

  • wa_fieldcat-edit = &5.
  • wa_fieldcat-checkbox = &6.
  • wa_fieldcat-no_zero = &7.
    APPEND wa_fieldcat TO gt_fieldcat.
    END-OF-DEFINITION.

def_fcat ‘STAT’ ‘消息状态’ ‘’ ‘’.
def_fcat ‘MESS’ ‘消息’ ‘’ ‘’.
def_fcat ‘FKDAT’ ‘开票日期’ ‘FKDAT’ ‘ZTCO006’.
def_fcat ‘VBELN’ ‘开票凭证’ ‘VBELN’ ‘T001’.
def_fcat ‘POSNR’ ‘开票行项目’ ‘POSNR’ ‘ZTCO006’.
def_fcat ‘ZPRONO’ ‘机台项目号’ ‘ZPRONO’ ‘ZTCO006’.
def_fcat ‘FKIMG’ ‘已出发票数量’ ‘FKIMG’ ‘ZTCO006’.
def_fcat ‘AUBEL’ ‘销售订单’ ‘AUBEL’ ‘CSKT’.
def_fcat ‘AUPOS’ ‘销售订单行’ ‘AUPOS’ ‘ZTCO006’.
def_fcat ‘VGBEL’ ‘交货单’ ‘VGBEL’ ‘ZTCO006’.
def_fcat ‘VGPOS’ ‘交货单行项目’ ‘VGPOS’ ‘T025T’.
def_fcat ‘MATNR’ ‘物料’ ‘MATNR’ ‘ZTCO006’.
def_fcat ‘WERKS’ ‘工厂’ ‘WERKS’ ‘CSKT’.
def_fcat ‘KURSK’ ‘汇率’ ‘KURSK’ ‘ZTCO006’.
def_fcat ‘NETWR’ ‘净值’ ‘NETWR’ ‘ZTCO006’.
def_fcat ‘MWSBP’ ‘税额’ ‘MWSBP’ ‘ZTCO006’.
def_fcat ‘WAERK’ ‘凭证货币’ ‘WAERK’ ‘ZTCO006’.
def_fcat ‘MWSK1’ ‘税码’ ‘MWSK1’ ‘ZTCO006’.
def_fcat ‘WAVWR’ ‘标准成本’ ‘WAVWR’ ‘ZTCO006’.
def_fcat ‘VKORG_AUFT’ ‘订单的销售组织’ ‘VKORG_AUFT’ ‘ZTCO006’.
def_fcat ‘VTWEG_AUFT’ ‘销售渠道’ ‘VTWEG_AUFT’ ‘ZTCO006’.
def_fcat ‘SPART’ ‘产品组’ ‘SPART’ ‘ZTCO006’.
def_fcat ‘KTGRD’ ‘客户科目分配组’ ‘KTGRD’ ‘ZTCO006’.
def_fcat ‘KTGRM’ ‘物料科目分配组’ ‘KTGRM’ ‘ZTCO006’.
def_fcat ‘BUKRS_ANA’ ‘公司代码’ ‘BUKRS_ANA’ ‘ZTCO006’.
def_fcat ‘KALNR’ ‘成本估算号’ ‘KALNR’ ‘ZTCO006’.
def_fcat ‘ZAMOUNT’ ‘发出商品该转入主营业务成本金额’ ‘ZAMOUNT’ ‘ZTCO006’.
def_fcat ‘RBUKRS’ ‘会计凭证公司代码’ ‘RBUKRS’ ‘ZTCO006’.
def_fcat ‘GJAHR’ ‘会计凭证年度’ ‘GJAHR’ ‘ZTCO006’.
def_fcat ‘BELNR’ ‘会计凭证’ ‘BELNR’ ‘ZTCO006’.
ENDFORM.
&---------------------------------------------------------------------
*& Form frm_get_data
&---------------------------------------------------------------------
*& text
&---------------------------------------------------------------------
*& --> p1 text
*& <-- p2 text
&---------------------------------------------------------------------
FORM frm_get_data .
DATA: l_jahrper TYPE ml4h_jahrper.

REFRESH: gt_out.

"查询已执行发出商品转主营业务成本的记录
SELECT * FROM ztco006
INTO CORRESPONDING FIELDS OF TABLE gt_out
WHERE bukrs_ana = p_bukrs
AND fkdat IN s_fkdat
AND aubel IN s_aubel
AND aupos IN s_aupos.

"根据屏幕查询条件查询待执行发票数据
SELECT k~fkdat, k~ktgrd, p~vbeln, pposnr,pfkimg,paubel,paupos,pkursk,pnetwr,pmwsbp,pvkorg_auft,pwavwr,pmwsk1,
pvtweg_auft,pspart,pbukrs_ana,pmatnr,pwaerk,pvgbel,pvgpos,pwerks, p~ktgrm, a~zprono, a~bedae, l~wadat_ist
FROM vbrk AS k
JOIN vbrp AS p ON k~vbeln = p~vbeln
JOIN vbap AS a ON p~aubel = a~vbeln AND p~aupos = a~posnr
JOIN likp AS l ON p~vgbel = l~vbeln
INTO TABLE @DATA(lt_vbrp)
WHERE k~fkdat IN @s_fkdat
AND k~bukrs = @p_bukrs
AND k~fksto = ‘’ "排除已被冲销发票
AND k~sfakn = ‘’ "排除冲销发票
AND p~vgtyp = ‘J’"前继凭证为交货单
AND p~aubel IN @s_aubel
AND p~aupos IN @s_aupos
AND NOT EXISTS ( SELECT mandt FROM ztco006 WHERE vbeln = p~vbeln AND posnr = p~posnr )."只取未执行发出商品转主营业务成本操作的发票

"估算号:
"VBAP-BEDAE=‘KEV’,根据VBRP-WERKS = CKMLHD-BWKEY、VBRP-MATNR =CKMLHD- MATNR、VBRP-AUBEL = CKMLHD-VBELN、VBRP-AUPOS= CKMLHD-POSNR,查询:CKMLHD-KALNR;
"VBAP-BEDAE不等于’KEV’时,根据VBRP-WERKS = CKMLHD-BWKEY、VBRP-MATNR =CKMLHD- MATNR、查询:CKMLHD-KALNR;
"差异金额:
"根据CKMLHD-KALNR = MLDOC-KALNR、MLDOC-JAHRPER=期间A,MLDOC-RUNREF=‘ACT’,货币取MLDOC-CURTP(公司代码货币),
"MLDOC-CATEG=“VN” , MLDOC-PTYP=“V+”, MLDOC-XABR=‘X’取金额=MLDOC-PRD,MLDOC-PROCESS显示例如“2021009就是9月份,根据开票凭证日期来判断”;
"根据MLDOC-PROCESS = CKMLMV005-KALNR,排除CKMLMV005-OBJNR_ND有值的项目;
"期间A:
"根据VBRP-VGPOS = VBRP-VBELN取交货单实际发货日期LIKP-WADAT_IST,根据发货日期确定开票凭证取的是哪个期间的数据;
"例如:开票12月份,但是实际交货日期是9月份,那么本程序在12月运行,产生凭证是12月31日,但是数据是9月份结账的差异数据。

"VBAP-BEDAE=‘KEV’
DATA(lt_temp) = lt_vbrp[].
DELETE lt_temp WHERE bedae <> ‘KEV’.
IF lt_temp[] IS NOT INITIAL.
SELECT a~kalnr, a~matnr, a~bwkey, a~vbeln, a~posnr, b~docref, jahrper, prd
FROM ckmlhd AS a
JOIN mldoc AS b ON a~kalnr = b~kalnr
JOIN ckmlmv005 AS c ON b~process = c~kalnr
INTO TABLE @DATA(lt_mldoc1)
FOR ALL ENTRIES IN @lt_temp
WHERE a~matnr = @lt_temp-matnr
AND a~bwkey = @lt_temp-werks
AND a~vbeln = @lt_temp-aubel
AND a~posnr = @lt_temp-aupos
AND b~curtp = ‘10’
AND b~runref = ‘ACT’
AND b~xabr = ‘X’
AND b~categ = ‘VN’
AND b~ptyp = ‘V+’
AND b~prd <> 0
AND c~objnr_nd = ‘’.
ENDIF.

"VBAP-BEDAE<>‘KEV’
REFRESH lt_temp.
lt_temp = lt_vbrp[].
DELETE lt_temp WHERE bedae = ‘KEV’.
SORT lt_temp BY matnr werks.
DELETE ADJACENT DUPLICATES FROM lt_temp COMPARING matnr werks.
IF lt_temp[] IS NOT INITIAL.
SELECT a~kalnr, a~matnr, a~bwkey, b~docref, jahrper, prd
FROM ckmlhd AS a
JOIN mldoc AS b ON a~kalnr = b~kalnr
JOIN ckmlmv005 AS c ON b~process = c~kalnr
INTO TABLE @DATA(lt_mldoc2)
FOR ALL ENTRIES IN @lt_temp
WHERE a~matnr = @lt_temp-matnr
AND a~bwkey = @lt_temp-werks
AND a~vbeln = ‘’
AND a~posnr = ‘000000’
AND b~curtp = ‘10’
AND b~runref = ‘ACT’
AND b~xabr = ‘X’
AND b~categ = ‘VN’
AND b~ptyp = ‘V+’
AND b~prd <> 0
AND c~objnr_nd = ‘’.
ENDIF.

SORT lt_mldoc1 BY matnr bwkey vbeln posnr jahrper.
SORT lt_mldoc2 BY matnr bwkey jahrper.

LOOP AT lt_vbrp INTO DATA(ls_vbrp).
CLEAR: wa_out.
MOVE-CORRESPONDING ls_vbrp TO wa_out.

l_jahrper = ls_vbrp-wadat_ist(4) && '0' && ls_vbrp-wadat_ist+4(2).
CASE ls_vbrp-bedae.
  WHEN 'KEV'.
    READ TABLE lt_mldoc1 INTO DATA(ls_mldoc1) WITH KEY matnr = ls_vbrp-matnr
                                                       bwkey = ls_vbrp-werks
                                                       vbeln = ls_vbrp-aubel
                                                       posnr = ls_vbrp-aupos
                                                       jahrper = l_jahrper BINARY SEARCH.
    IF sy-subrc = 0.
      wa_out-kalnr = ls_mldoc1-kalnr.
      wa_out-zamount = ls_mldoc1-prd.
    ELSE."无差异不需要转
      CONTINUE.
    ENDIF.

  WHEN OTHERS.
    READ TABLE lt_mldoc2 INTO DATA(ls_mldoc2) WITH KEY matnr = ls_vbrp-matnr
                                                       bwkey = ls_vbrp-werks
                                                       jahrper = l_jahrper BINARY SEARCH.
    IF sy-subrc = 0.
      wa_out-kalnr = ls_mldoc2-kalnr.
      wa_out-zamount = ls_mldoc2-prd.
    ELSE."无差异不需要转
      CONTINUE.
    ENDIF.

ENDCASE.

APPEND wa_out TO gt_out.

ENDLOOP.

ENDFORM.
&---------------------------------------------------------------------
*& Form frm_post_document
&---------------------------------------------------------------------
*& 生产会计凭证 一发票一会计凭证
&---------------------------------------------------------------------
*& --> p1 text
*& <-- p2 text
&---------------------------------------------------------------------
FORM frm_post_document .

DATA: l_dmbtr TYPE dmbtr,
l_sgtxt TYPE sgtxt,
l_objnr TYPE j_objnr,
l_field TYPE char30,
l_errflg TYPE char1,
l_date TYPE sy-datum,
l_waers TYPE t001-waers,
l_lines TYPE i,
l_tabix TYPE sy-tabix.
DATA: lt_out TYPE TABLE OF ty_out, "用于生成凭证
ls_out TYPE ty_out.
FIELD-SYMBOLS: .

SORT gt_out BY vbeln posnr.

REFRESH: lt_out.

lt_out[] = gt_out[].

DELETE lt_out WHERE belnr IS NOT INITIAL.
DELETE ADJACENT DUPLICATES FROM lt_out COMPARING vbeln.

IF lt_out[] IS INITIAL.
MESSAGE ‘不可重复创建会计凭证’ TYPE ‘W’.
RETURN.
ENDIF.

SELECT SINGLE waers FROM t001 INTO l_waers WHERE bukrs = p_bukrs.
SELECT * FROM c001 INTO TABLE @DATA(lt_c001) WHERE kappl = ‘V’ AND kschl = ‘KOFI’ AND ktopl = ‘8000’ AND kvsl1 = ‘ZCO’.
SORT lt_c001 BY vkorg ktgrd ktgrm.

LOOP AT lt_out INTO ls_out.

CLEAR: l_errflg.
CLEAR: gv_posnr.
REFRESH : gt_accountgl,gt_accountreceivable,gt_accountpayable,gt_currencyamount,gt_return,gt_extension2,gt_criteria.
REFRESH: gt_ztco006.
CLEAR : gw_documentheader.

READ TABLE gt_out TRANSPORTING NO FIELDS WITH KEY vbeln = ls_out-vbeln BINARY SEARCH.
IF sy-subrc = 0.
  l_tabix = sy-tabix.

  LOOP AT gt_out INTO wa_out FROM l_tabix.
    IF wa_out-vbeln <> ls_out-vbeln.
      EXIT.
    ENDIF.

    READ TABLE lt_c001 INTO DATA(ls_c001) WITH KEY vkorg = wa_out-vkorg_auft ktgrd = wa_out-ktgrd ktgrm = wa_out-ktgrm BINARY SEARCH.
    IF sy-subrc = 0.
      CLEAR wa_ztco006.
      MOVE-CORRESPONDING wa_out TO wa_ztco006.
      APPEND wa_ztco006 TO gt_ztco006.

      l_sgtxt = |EG:{ p_gjahr }年{ p_monat }月份已开票发出商品实际与标准价差异调整至主营业务成本|.
      "差异是正数时:
      "D:发出商品          +物料字段 +销售订单+销售订单行  +项目号
      "C:主营业务成本          主营业务成本-国内      +物料字段 +销售订单+销售订单行 +项目号
      "差异是负数时:
      "D:主营业务成本      +物料字段 +销售订单+销售订单行 +项目号
      "C:发出商品          +物料字段 +销售订单+销售订单行  +项目号
      PERFORM frm_hkont_item USING wa_out-zamount ls_c001-sakn1 l_sgtxt l_waers wa_out-matnr wa_out-aubel wa_out-aupos wa_out-zprono."发出商品
      l_dmbtr = wa_out-zamount * -1.
      PERFORM frm_hkont_item USING l_dmbtr ls_c001-sakn2 l_sgtxt l_waers wa_out-matnr wa_out-aubel wa_out-aupos wa_out-zprono."主营业务成本
    ELSE.
      wa_out-stat = icon_red_light.
      wa_out-mess = '科目确定失败'.
      MODIFY gt_out FROM wa_out.
      l_errflg = 'X'.
      EXIT.
    ENDIF.

  ENDLOOP.
ENDIF.

IF l_errflg = 'X'.
  CONTINUE.
ENDIF.

"生成会计凭证
IF gt_currencyamount[] IS NOT INITIAL.
  l_date = p_gjahr && p_monat && '01'.
  CALL FUNCTION 'FIMA_DATE_CREATE'
    EXPORTING
      i_date                  = l_date
      i_set_last_day_of_month = 'X'
    IMPORTING
      e_date                  = l_date.

  "抬头数据
  CLEAR gw_documentheader.
  gw_documentheader-comp_code = wa_out-bukrs_ana.   "公司代码

  gw_documentheader-doc_date = sy-datum.    "凭证日期

  gw_documentheader-pstng_date = l_date.  "过账日期

  gw_documentheader-doc_type = 'SA'.    "凭证类型

  gw_documentheader-header_txt = |已开票发出商品实际与标准价差异调整至主营业务成本|.  "凭证抬头文本

  gw_documentheader-fisc_year = p_gjahr.  "会计年度

  gw_documentheader-fis_period = p_monat.  "期间

  gw_documentheader-username = sy-uname.


  CALL FUNCTION 'BAPI_ACC_DOCUMENT_POST'
    EXPORTING
      documentheader    = gw_documentheader
  •     CUSTOMERCPD       =
    
  •     CONTRACTHEADER    =
    
  •    IMPORTING
    
  •     OBJ_TYPE          =
    
  •     OBJ_KEY           = LV_OBJKEY
    
  •     OBJ_SYS           =
      TABLES
        accountgl         = gt_accountgl[]
        accountreceivable = gt_accountreceivable[]
        accountpayable    = gt_accountpayable[]
    
  •     ACCOUNTTAX        =
        currencyamount    = gt_currencyamount[]
    
  •     criteria          = gt_criteria[]
    
  •     VALUEFIELD        =
    
  •     EXTENSION1        =
        return            = gt_return[]
    
  •     PAYMENTCARD       =
    
  •     CONTRACTITEM      =
        extension2        = gt_extension2[]
    
  •     REALESTATE        =
    
  •     ACCOUNTWT         =
      .
    LOOP AT gt_return WHERE type = 'E' OR type = 'A'.
    ENDLOOP.
    IF sy-subrc = 0.
      wa_out-stat  = icon_red_light.
      READ TABLE gt_return INDEX 2.
      IF sy-subrc = 0.
        wa_out-mess  = gt_return-message.
      ELSE.
        wa_out-mess = '会计凭证创建失败'.
      ENDIF.
    
      MODIFY gt_out FROM wa_out TRANSPORTING stat mess belnr
        WHERE vbeln = ls_out-vbeln.
    
      CALL FUNCTION 'BAPI_TRANSACTION_ROLLBACK'.
    
    ELSE.
    
      READ TABLE gt_return INDEX 1.
      wa_out-rbukrs = gt_return-message_v2+10(4). "公司代码
      wa_out-belnr = gt_return-message_v2+0(10) .                         "凭证编号
      wa_out-gjahr = gt_return-message_v2+14(4).
      wa_out-stat  = icon_green_light.
    
    
      MODIFY gt_out FROM wa_out TRANSPORTING rbukrs belnr gjahr stat
        WHERE vbeln = ls_out-vbeln.
      wa_ztco006-rbukrs = wa_out-rbukrs.
      wa_ztco006-gjahr = wa_out-gjahr.
      wa_ztco006-belnr = wa_out-belnr.
      MODIFY gt_ztco006 FROM wa_ztco006 TRANSPORTING rbukrs belnr gjahr
        WHERE vbeln = ls_out-vbeln.
      IF gt_ztco006[] IS NOT INITIAL.
        MODIFY ztco006 FROM TABLE gt_ztco006.
      ENDIF.
    
      CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'
        EXPORTING
          wait = 'X'.
    
    ENDIF.
    

    ENDIF.

    ENDLOOP.

ENDFORM.
&---------------------------------------------------------------------
*& Form frm_hkont_item
&---------------------------------------------------------------------
*& text
&---------------------------------------------------------------------
*& --> GS_OUTTAB_800_10
*& --> P_
*& --> GS_OUTTAB_AUFNR
*& --> P_
*& --> GS_OUTTAB_WAERK
*& --> GS_OUTTAB_MATNR
&---------------------------------------------------------------------
FORM frm_hkont_item USING pv_kmze pv_hkont pv_sgtxt pv_waerk pv_matnr pv_aubel pv_aupos pv_zprono.
ADD 1 TO gv_posnr.
"金额
gt_currencyamount-itemno_acc = gv_posnr. "项目编号

gt_currencyamount-currency = pv_waerk."‘GBP’. " 货币码

gt_currencyamount-amt_doccur = pv_kmze . "货币金额

APPEND gt_currencyamount.
CLEAR gt_currencyamount.

"总账
gt_accountgl-itemno_acc = gv_posnr. "项目编号

gt_accountgl-gl_account = pv_hkont . "总账科目

gt_accountgl-material_long = pv_matnr . "物料号

gt_accountgl-sales_ord = pv_aubel . "销售订单

gt_accountgl-s_ord_item = pv_aupos . "销售订单行

gt_accountgl-item_text = pv_sgtxt. "文本

APPEND gt_accountgl.
CLEAR gt_accountgl.

"扩展项
gw_zsfi_exten-posnr = gv_posnr. "项目编号
gw_zsfi_exten-zzxmbm = pv_zprono. "项目号
IF pv_kmze < 0."总账科目金额为负,过账码为50;总账科目金额为正,过账码为40
gw_zsfi_exten-bschl = ‘50’. "记账码
ELSE.
gw_zsfi_exten-bschl = ‘40’. "记账码
ENDIF.

gt_extension2-structure = ‘ZSFI004’.

gt_extension2-valuepart1 = gw_zsfi_exten.

APPEND gt_extension2.
CLEAR : gt_extension2,gw_zsfi_exten.
ENDFORM.
&---------------------------------------------------------------------
*& Form frm_reverse_document
&---------------------------------------------------------------------
*& text
&---------------------------------------------------------------------
*& --> p1 text
*& <-- p2 text
&---------------------------------------------------------------------
FORM frm_reverse_document .
DATA: lt_belnr TYPE TABLE OF ty_out,
ls_reversal LIKE bapiacrev,
lv_bus_act LIKE bapiache09-bus_act,
lv_obj_key LIKE bapiacrev-obj_key,
lt_return TYPE TABLE OF bapiret2,
ls_return TYPE bapiret2.

MOVE-CORRESPONDING gt_out TO lt_belnr.

SORT lt_belnr BY rbukrs gjahr belnr.
DELETE ADJACENT DUPLICATES FROM lt_belnr COMPARING rbukrs gjahr belnr.

LOOP AT lt_belnr INTO DATA(ls_belnr) WHERE belnr IS NOT INITIAL.
SELECT SINGLE * FROM bkpf INTO @DATA(ls_bkpf)
WHERE bukrs = @ls_belnr-rbukrs
AND belnr = @ls_belnr-belnr
AND gjahr = @ls_belnr-gjahr
AND xreversal = ‘’.
IF sy-subrc <> 0.
CLEAR: wa_out-rbukrs, wa_out-belnr, wa_out-gjahr.
wa_out-stat = icon_red_light.
wa_out-mess = ‘会计凭证不存在或已冲销’.

  DELETE FROM ztco006 WHERE rbukrs = ls_belnr-rbukrs
                       AND belnr = ls_belnr-belnr
                       AND gjahr = ls_belnr-gjahr.
  COMMIT WORK AND WAIT.

  MODIFY gt_out FROM wa_out TRANSPORTING stat mess rbukrs belnr gjahr
    WHERE rbukrs = ls_belnr-rbukrs
      AND belnr = ls_belnr-belnr
      AND gjahr = ls_belnr-gjahr.
  CONTINUE.
ENDIF.

CLEAR: ls_reversal, lv_bus_act, lv_obj_key.
REFRESH: lt_return.
  • bapi 参数赋值
    ls_reversal-obj_type = ls_bkpf-awtyp.

  • lS_reversal-obj_key = lS_bkpf-awkey.
    ls_reversal-obj_key_r = ls_bkpf-awkey.
    ls_reversal-pstng_date = ls_bkpf-budat.
    ls_reversal-fis_period = ls_bkpf-monat.
    ls_reversal-comp_code = ls_bkpf-bukrs.

  • ls_reversal-ac_doc_no = ls_bkpf-belnr.
    ls_reversal-reason_rev = ‘03’.
    lv_bus_act = ls_bkpf-glvor.

  • 取得系统 LOGICAL SYSTEM
    CALL FUNCTION ‘OWN_LOGICAL_SYSTEM_GET’
    IMPORTING
    own_logical_system = ls_reversal-obj_sys.

  • 调用 BAPI 函数,冲销会计凭证
    CALL FUNCTION ‘BAPI_ACC_DOCUMENT_REV_POST’
    EXPORTING
    reversal = ls_reversal
    bus_act = lv_bus_act
    IMPORTING

  •   OBJ_TYPE =
      obj_key  = lv_obj_key
    
  •   OBJ_SYS  =
    TABLES
      return   = lt_return.
    

    IF lv_obj_key IS NOT INITIAL AND lv_obj_key NE ‘$’.
    CLEAR: wa_ztco005.
    wa_ztco005-rbukrs = ls_belnr-rbukrs.
    wa_ztco005-belnr = ls_belnr-belnr.
    wa_ztco005-gjahr = ls_belnr-gjahr.
    wa_ztco005-r_belnr = lv_obj_key(10).
    wa_ztco005-r_rbukrs = lv_obj_key+10(4).
    wa_ztco005-r_gjahr = lv_obj_key+14(4).
    INSERT ztco005 FROM wa_ztco005.

    CLEAR: wa_out-rbukrs, wa_out-belnr, wa_out-gjahr.
    
    DELETE FROM ztco006 WHERE rbukrs = ls_belnr-rbukrs
                         AND belnr = ls_belnr-belnr
                         AND gjahr = ls_belnr-gjahr.
    
    CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'
      EXPORTING
        wait = 'X'.
    
    wa_out-stat = icon_green_light.
    wa_out-mess = '冲销成功'.
    
    MODIFY gt_out FROM wa_out TRANSPORTING stat mess rbukrs belnr gjahr
      WHERE rbukrs = ls_belnr-rbukrs
        AND belnr = ls_belnr-belnr
        AND gjahr = ls_belnr-gjahr.
    

    ELSE.
    CALL FUNCTION ‘BAPI_TRANSACTION_ROLLBACK’.

    wa_out-stat = icon_red_light.
    LOOP AT lt_return INTO ls_return.
      wa_out-mess = wa_out-mess && ls_return-message && ';'.
    ENDLOOP.
    MODIFY gt_out FROM wa_out TRANSPORTING stat mess
      WHERE rbukrs = ls_belnr-rbukrs
        AND belnr = ls_belnr-belnr
        AND gjahr = ls_belnr-gjahr.
    

    ENDIF.

    ENDLOOP.
    ENDFORM.

你可能感兴趣的:(SAP-ABAP,sap,开发语言)