发出商品科目余额

report  zfi_r_29_ar_001.
include zrmm_alv_top.
include zfi_r_29_ar_top.
include zfi_r_29_ar_frm.
include zfi_r_29_ar_out.


*----------------------------------------------------------------------*
*        INITIALIZATION                                                *
*----------------------------------------------------------------------*
initialization.
  perform alv_init.
  perform init_parameter.

*AT SELECTION-SCREEN ON VALUE-REQUEST FOR P_LAYT.
  perform f4_for_variant.
*----------------------------------------------------------------------*
*        AT SELECTION-SCREEN                                           *
*----------------------------------------------------------------------*
at selection-screen output.
*----------------------------------------------------------------------*
*        START-OF-SELECTION                                            *
*----------------------------------------------------------------------*
start-of-selection.
  perform check_input.
  perform sub_get_data.
  perform process_data.
  perform get_zbv.
  perform output.

*******************************************************************************************************
*&---------------------------------------------------------------------*
*&  包括                ZRMM_ALV_TOP
*&---------------------------------------------------------------------*

* ALV
type-poolsslis,kkblo.
include .
include .

data  g_repid     like sy-repid.
* Internal table to store saved display variant
data gt_fcat_sav    type kkblo_fieldcat occurs with header line.
data gt_sort_sav    type kkblo_sortinfo occurs with header line.
data gt_filter_sav  type kkblo_filter   occurs with header line.
data gs_layout_sav  type kkblo_layout.
datagt_fieldcat type slis_t_fieldcat_alv,
      gt_list_top_of_page type slis_t_listheader,
      gs_layout   type slis_layout_alv,
      gs_event    type slis_alv_event,      "Def. of call back routines
      gs_sort     type slis_t_sortinfo_alv with header line,
      gt_events   type slis_t_event.
datait_fcat      type lvc_t_fcat.
dataalv_keyinfo      type slis_keyinfo_alv.
dataalv_variant      like disvariant.
dataalv_layout       type slis_layout_alv.
dataalv_repid        like sy-repid.
dataalv_print        type slis_print_alv.
dataalv_detail_func(30).
dataalv_default_variant    like  disvariant-variant.
dataalv_colourize_fields   like  mmim_rep_print-color.
data:sortcat             type slis_t_sortinfo_alv,
     sortcat_ln          type slis_sortinfo_alv.

datat_fc   type slis_t_fieldcat_alv,
        t_sort type slis_t_sortinfo_alv.
data:g_save(01)     type value 'A',
     g_exit(01)     type c,
     g_popr(03)     type n,
     g_variant      like disvariant,
     gx_variant     like disvariant.
data:alv_def     like disvariant-variant.
*"Callback
data:

    g_status_set   type slis_formname value 'PF_STATUS_SET',
    g_user_command type slis_formname value 'USER_COMMAND',
    g_top_of_page  type slis_formname value 'TOP_OF_PAGE',
    g_top_of_list  type slis_formname value 'TOP_OF_LIST',
    g_end_of_list  type slis_formname value 'END_OF_LIST'.
constants:
    c_f2code          like sy-ucomm value '&ETA',
    c_top_of_page     type slis_formname value 'TOP_OF_PAGE',
    c_a(1)            type value 'A',
    c_h(1)            type value 'H',
    c_s(1)            type value 'S',
    c_x(1)            type value 'X',
    c_tbl like t881-tab value 'CCSS'.     "TAB

*&---------------------------------------------------------------------*
*&      Form  ALV_INIT
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
form alv_init.
  clearalv_keyinfoalv_variantalv_layoutalv_print.
  alv_repid sy-repid.
  clear alv_variant.
  gs_layout-f2code 'DOUBLE'.
  alv_variant-report alv_repid.
*  CALL FUNCTION 'REUSE_ALV_VARIANT_DEFAULT_GET'
*       EXPORTING
*            I_SAVE     = G_SAVE
*       CHANGING
*            CS_VARIANT = ALV_VARIANT
*       EXCEPTIONS
*            NOT_FOUND  = 2.
*  IF SY-SUBRC = 0.
*    ALV_DEF = ALV_VARIANT-VARIANT.
**   save the initial, e.g. default variant                  "n579976
*    MOVE ALV_VARIANT-VARIANT TO  ALV_DEFAULT_VARIANT.       "n579976
*  ENDIF.
  select  variant into alv_variant-variant
    from ltdx
    where report alv_variant-report
    and   username sy-uname.
  endselect.
* Printing settings
  gs_layout-get_selinfos 'X'.
  gs_layout-group_change_edit 'X'.

  clear g_variant.
  g_variant-report sy-repid.

endform.                    " ALV_INIT
*&---------------------------------------------------------------------*
*&      Form  SUB_F4_FOR_VARIANT
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
form sub_f4_for_variant.
  call function 'REUSE_ALV_VARIANT_F4'
    exporting
      is_variant alv_variant
      i_save     g_save
    importing
      e_exit     g_exit
      es_variant gx_variant
    exceptions
      not_found  2.

  if sy-subrc 2.
    message id sy-msgid type c_s number sy-msgno
          with sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
  else.
    if g_exit space.
      alv_def gx_variant-variant.
    endif.
  endif.

endform.                    " SUB_F4_FOR_VARIANT
*&---------------------------------------------------------------------*
*&      Form  Retrieve_ALV_Variant
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
form retrieve_alv_variant.
**  GX_VARIANT-VARIANT = P_VARI.
*  CALL FUNCTION 'REUSE_ALV_VARIANT_DEFAULT_GET'
*       EXPORTING
*            I_SAVE     = G_SAVE
*       CHANGING
*            CS_VARIANT = ALV_VARIANT
*       EXCEPTIONS
*            NOT_FOUND  = 2.
* CLEAR: ALV_KEYINFO, ALV_VARIANT, ALV_LAYOUT, ALV_PRINT.
*  ALV_REPID = SY-REPID.
*  CLEAR ALV_VARIANT.
*  ALV_VARIANT-REPORT = ALV_REPID.
*  CALL FUNCTION 'REUSE_ALV_VARIANT_DEFAULT_GET'
*       EXPORTING
*            I_SAVE     = 'A'
*       CHANGING
*            CS_VARIANT = ALV_VARIANT
*       EXCEPTIONS
*            NOT_FOUND  = 2.
*  IF SY-SUBRC = 0.
*    ALV_DEF = ALV_VARIANT-VARIANT.
**   save the initial, e.g. default variant                  "n579976
*    MOVE ALV_VARIANT-VARIANT TO  ALV_DEFAULT_VARIANT.       "n579976
*  ENDIF.
** Printing settings
*  GS_LAYOUT-GET_SELINFOS = 'X'.
*  GS_LAYOUT-GROUP_CHANGE_EDIT = 'X'.
*
endform.                    " Retrieve_ALV_Variant

**********************************************************************************************************
*&---------------------------------------------------------------------*
*&  包括                ZFI_R_29_AR_TOP
*&---------------------------------------------------------------------*
tables:bsis,
       vbap,
       vbak,
       likp.

databegin of it_ship occurs 0,
        vbeln like lips-vbeln,
        posnr like lips-posnr,
        uecha like lips-uecha,
        vgbel like lips-vgbel,
        vgpos like lips-vgpos,
        werks like vbrp-werks,
        matnr like vbrp-matnr,
        lfimg like lips-lfimg,
        knumv like vbak-knumv,
        kunnr like vbak-kunnr,
        kursk like vbkd-kursk,
        vkorg like vbak-vkorg,
        arktx like lips-arktx,
        wadat_ist type d,
        netpr like vbap-netpr,
        netwr1 like vbap-netwr,
        kpein like vbap-kpein,
        mwsbp like vbap-mwsbp,
        kwmeng like vbap-kwmeng,
        waerk like vbak-waerk,
        xblnr like bkpf-xblnr,
        name1 like kna1-name1,
        billing(10),
        billing_item(6type n,
        item  like vbfa-posnn,
        fksta like vbup-fksta,
      end of it_ship.

databegin of it_dn_sum occurs 0,
        vbeln like vbrp-vbeln,    "DN
        posnr like vbrp-posnr,    "DN ITEM
        vgbel like lips-vgbel,    "SO
        vgpos like lips-vgpos,    "SO ITEM
        dmbtr like bsis-dmbtr,    "AMOUNT
        item  like vbfa-posnn,    "DN SERIAL ITEM
      end of it_dn_sum.

databegin of it_wl occurs 100,
       belnr like bsis-belnr,
       gjahr like bsis-gjahr,
       buzei like bsis-buzei,
       budat like bsis-budat,
       shkzg like bsis-shkzg,
       xblnr like bsis-xblnr,
       blart like bsis-blart,
       pswsl like bsis-pswsl,
       wrbtr like bsis-wrbtr,
       dmbtr like bsis-dmbtr,
       awkey like bkpf-awkey,
       vbel2 like bseg-vbel2,
       posn2 like bseg-posn2,
       item  like vbfa-posnn,
      end of it_wl.
datait_bill like table of it_wl with header line.

databegin of it_vbfa occurs 100,
       vbeln like vbfa-vbeln,  "billing docu or material docu
       posnn like vbfa-posnn,  "billing item or material item
       vbelv like vbfa-vbelv,  "DN
       posnv like vbfa-posnv,  "DN item
       xblnr like bkpf-xblnr,
       item  like vbfa-posnn,
       awkey like bkpf-awkey,
       vbtyp_v like vbfa-vbtyp_v,
       vbtyp_n like vbfa-vbtyp_n,
       mjahr like vbfa-mjahr,
      end of it_vbfa.

datait_vbfa_md like table of it_vbfa with header line.
databegin of it_vbrp occurs 100.
        include structure vbrp.
data:  awkey like bkpf-awkey,
       item  like vbfa-posnn,
      end of it_vbrp.
databegin of it_output occurs 100.
        include structure it_ship.
data:  dmbtr like bsis-dmbtr,
       belnr like bsis-belnr,
       buzei like bsis-buzei,
       gjahr like bsis-gjahr,
       netwr(16type decimals 2,"外币净值
       tax_amt(16type decimals 2,"外币税金
        netwr2(16type decimals 2,"本位币净值
       tax_amt1(16type decimals 2,"本位币税金
       end of it_output.
datait_other like table of it_bill initial size 500 with header line.

data:itab  like table of it_output with header line.

*----------------------------------------------------------------------*
*        RANGE                                                         *
*----------------------------------------------------------------------*
data:g_end.
data:g_svfile like rlgrap-filename.
data:g_fdate like sy-datum,
     g_ldate like sy-datum.

*----------------------------------------------------------------------*
*        SELECTION SCREEN                                              *
*----------------------------------------------------------------------*
selection-screen begin of block b1 with frame title text-t01.


select-options:s_bukrs for bsis-bukrs memory id buk
                       no-extension no intervals obligatory default '1000',
               s_werks for vbap-werks memory id wks,
               s_budat for bsis-budat memory id bud,
               s_hkont for bsis-hkont memory id hko obligatory
                       default '14060000' to '14060005',
               s_kunnr for vbak-kunnr memory id kun,
               s_vbeln for likp-vbeln memory id vbe,
               s_order for vbap-vbeln memory id ord,
               s_item  for vbap-posnr memory id pos,
               s_matnr for vbap-matnr memory id mat.
selection-screen end of block b1.

selection-screen begin of block b3 with frame title text-t03.
parametersp_alv radiobutton group a2 default 'X' user-command a2,
            p_layt like disvariant-variant,
            p_dnlod radiobutton group a2.
parametersp_svpath like rlgrap-filename modif id pth memory id pat.
parametersp_svfile like rlgrap-filename no-display.
selection-screen end of block b3.

*公司代码的权限控制
at selection-screen on s_bukrs.
  authority-check object 'F_BKPF_BUK' id 'BUKRS' field s_bukrs-low
                                      id 'ACTVT' field '03'.
  if sy-subrc ne 0.
    message  e007(zfiwith '你没有权限查看' s_bukrs-low '公司代码的数据!'.
    exit.
  endif.

********************************************************************************************************

*&---------------------------------------------------------------------
*
*&  Include           ZFI_R_29_AR_FRM
*
*&---------------------------------------------------------------------

*&---------------------------------------------------------------------
*
*&      Form  PROGRESS_INDICATOR
*&---------------------------------------------------------------------
*
*       text
*----------------------------------------------------------------------
*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------
*
form progress_indicator .
  call function 'SAPGUI_PROGRESS_INDICATOR'
         exporting
*              PERCENTAGE = SY-INDEX
              text       text-pin
         exceptions
              others     0.
endform.                    " PROGRESS_INDICATOR
*&---------------------------------------------------------------------
*
*&      Form  INIT_PARAMTER
*&---------------------------------------------------------------------
*
*       text
*----------------------------------------------------------------------
*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------
*
form init_paramter .
  data:l_date like sy-datum.
*  CLEAR:P_LAEDA,
*        S_MTD.

*  L_DATE = SY-DATUM.
*  P_LAEDA = L_DATE.
*
*  IF S_MTD[] IS INITIAL.
*    S_MTD-OPTION = 'BT'.
*    S_MTD-SIGN = 'I'.
*    CONCATENATE SY-DATUM(6) '01' INTO S_MTD-LOW.
*    L_DATE = SY-DATUM - 1.
*    S_MTD-HIGH = L_DATE.
*    IF S_MTD-HIGH < S_MTD-LOW.
*      S_MTD-HIGH = S_MTD-LOW.
*    ENDIF.
*    APPEND S_MTD.
*  ENDIF.

endform.                    " INIT_PARAMTER
*&---------------------------------------------------------------------
*
*&      Form  CHECK_INPUT
*&---------------------------------------------------------------------
*
*       text
*----------------------------------------------------------------------
*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------
*
form check_input .
  if p_dnlod 'X'.
    if p_svpath is initial.
      message i001(00with 'Please input a file path!'.
      stop.
    endif.
  endif.
  if s_budat-high is initial.
    s_budat-high s_budat-low.
  endif.
*  IF P_SEND = 'X'.
*    IF S_MAIL01[] IS INITIAL OR S_MAIL02[] IS INITIAL .
*      MESSAGE I001(00) WITH 'Please input email address!'.
*      STOP.
*    ENDIF.
*  ENDIF.
endform.                    " CHECK_INPUT
*&---------------------------------------------------------------------*
*&      Form  SUB_GET_DATA
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
form sub_get_data .
  perform progress_indicator .
* Get PGI DN
  perform get_post_dn.
  check it_ship[] is not initial.
* Get billing data for DN or SO
  perform get_document_flow.
* Get account document generated by PGI
  perform get_gi_account_document.
* Get account document generated by billing
  perform get_billing_account_document.
* Get manual create account document
  perform get_other_account_document.
endform.                    " SUB_GET_DATA
*&---------------------------------------------------------------------*
*&      Form  MODIFY_SCREEN
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
form modify_screen .
  loop at screen.
    if p_alv 'X'.
      if screen-group1 'PTH'.
        screen-active '0'.
        screen-invisible '1'.
        modify screen.
      endif.
    endif.
  endloop.
endform.                    " MODIFY_SCREEN
*&---------------------------------------------------------------------*
*&      Form  convert_matnr_output
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*      -->P_IN  text
*      <--P_OUT  text
*----------------------------------------------------------------------*
form convert_matnr_output  using    p_in
                           changing p_out.
  call function 'CONVERSION_EXIT_MATN1_OUTPUT'
    exporting
      input  p_in
    importing
      output p_out.

endform.                    " convert_matnr_output
*&---------------------------------------------------------------------*
*&      Form  convert_mat_input
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*      -->P_MATNR  text
*      <--P_MATNR  text
*----------------------------------------------------------------------*
form convert_mat_input  using    p_in_matnr
                        changing p_out_matnr.
  call function 'CONVERSION_EXIT_MATN1_INPUT'
    exporting
      input        p_in_matnr
    importing
      output       p_out_matnr
    exceptions
      length_error 1
      others       2.
endform.                    " convert_mat_input

*&---------------------------------------------------------------------*
*&      Form  INIT_PARAMETER
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
form init_parameter .
  move:  'I'          to s_budat-sign,
         'BT'         to s_budat-option,
         '20100630'   to s_budat-low.
  append s_budat.
** Customer code
*  IF S_KUNNR[] IS INITIAL.
*    S_KUNNR-OPTION = 'EQ'.
*    S_KUNNR-SIGN = 'I'.
*    S_KUNNR-LOW = '0000010152'.
*    APPEND S_KUNNR.
*    S_KUNNR-LOW = '0000012573'.
*    APPEND S_KUNNR.
*  ENDIF.
endform.                    " INIT_PARAMETER
*&---------------------------------------------------------------------*
*&      Form  get_post_dn
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
form get_post_dn .
  data:l_item like sy-tabix.
  data:wa like it_ship.

  datavbtyp_n like vbfa-vbtyp_n,
        fkimg like vbrp-fkimg.

  select lips~vbeln
         lips~posnr
         lips~vgbel
         lips~vgpos
         lips~werks
         lips~matnr
         lips~lfimg
         lips~arktx
         lips~vbeln as xblnr
         likp~kunag as kunnr
         likp~wadat_ist
         vbak~vkorg
         vbap~netpr
         vbap~netwr as netwr1
         vbap~kpein
         vbap~mwsbp
         vbap~kwmeng
         vbkd~kursk
         vbak~waerk
         kna1~name1
         vbup~fksta
    into corresponding fields of table it_ship
    from likp
    inner join lips  on likp~vbeln lips~vbeln
    inner join vbak  on lips~vgbel vbak~vbeln
    inner join vbap  on lips~vgbel vbap~vbeln and lips~vgpos vbap~posnr
    inner join t001k on t001k~bwkey lips~werks
    inner join kna1  on kna1~kunnr likp~kunag
    inner join vbkd  on vbkd~vbeln vbap~vbeln "and vbkd~posnr = vbap~posnr
*** 2013.7 去掉已开票的
    inner join vbup  on lips~vbeln vbup~vbeln and lips~posnr vbup~posnr
***
    where likp~wadat_ist =< s_budat-high
      and likp~kunag in s_kunnr
      and likp~vbeln in s_vbeln
      and lips~vgbel in s_order
      and lips~vgpos in s_item
      and lips~matnr in s_matnr
      and lips~lfimg > 0
      and vbkd~posnr '000000'
      and t001k~bukrs in s_bukrs
*** 2013.7 去掉已开票的
      and vbup~fksta ne 'C'   "未开票、部分开票、与开票无关
      and vbup~wbsta =  'C'   "货物移动已完成
***
***2013.9 去掉类型为销售订单类型为ZBV的(一体机销售订单)
      and vbak~auart ne 'ZBV'.
***

  loop at it_ship where fksta 'B'.

    clear:fkimgit_vbfa[].

    select distinct vbeln posnn
      into corresponding fields of table it_vbfa
      from vbfa
      where vbelv it_ship-vbeln
        and posnv it_ship-posnr
        and vbtyp_n 'M'
        order by vbeln posnn ascending.

    if it_vbfa[] is not initial.
      loop at it_vbfa.
        clear vbtyp_n.

        select single vbtyp_n
          into vbtyp_n
          from vbfa
          where vbelv it_vbfa-vbeln
            and posnv it_vbfa-posnn.

        if vbtyp_n 'N'.
          delete it_vbfa.
        endif.
      endloop.
    endif.

    select fkimg
      from vbrp
      into fkimg
      for all entries in it_vbfa
      where vgbel it_ship-vbeln
        and vgpos it_ship-posnr
        and vbeln it_vbfa-vbeln
        and posnr it_vbfa-posnn.

      it_ship-lfimg =  it_ship-lfimg fkimg.

    endselect.
    modify it_ship.
  endloop.

  clear it_vbfa[].
***

*  loop at it_ship.
*    wa = it_ship.
*    l_item = l_item + 1.
*    wa-item = l_item.
*    at end of vbeln.
*      clear l_item.
*    endat.
*    modify it_ship from wa transporting item.
*    clear wa.
*  endloop.
endform.                    " get_post_dn
*&---------------------------------------------------------------------*
*&      Form  GET_GI_ACCOUNT_DOCUMENT
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
form get_gi_account_document .
  datal_item like sy-tabix.
  datawa like it_wl.
  datait_bkpf like table of bkpf with header line.
  datait_bseg like table of bseg with header line.
  check it_vbfa_md[] is not initial.
  sort it_vbfa_md by awkey.
  select bkpf~belnr
         bkpf~gjahr
         bkpf~bukrs
         bkpf~budat
         bkpf~awkey
         bkpf~blart
         bkpf~xblnr
  into corresponding fields of table it_bkpf
  from bkpf
  for all entries in it_vbfa_md
  where bkpf~awtyp 'MKPF'
    and bkpf~awkey it_vbfa_md-awkey
    and bkpf~budat in s_budat.
  if sy-subrc 0.
    sort it_bkpf by bukrs belnr gjahr.
    select
           bseg~bukrs
           bseg~belnr
           bseg~gjahr
           bseg~buzei
           bseg~shkzg
           bseg~dmbtr
           bseg~pswsl
           bseg~wrbtr
    into corresponding fields of table it_bseg
    from bseg
    for all entries in it_bkpf
    where bseg~bukrs it_bkpf-bukrs
      and bseg~belnr it_bkpf-belnr
      and bseg~gjahr it_bkpf-gjahr
      and bseg~hkont in s_hkont
      and bseg~vbel2 in s_order
      and bseg~posn2 in s_item
      and bseg~matnr in s_matnr.
  endif.
  sort it_bseg by bukrs belnr gjahr buzei.
  loop at it_bkpf.
    read table it_bseg with key bukrs it_bkpf-bukrs
                                belnr it_bkpf-belnr
                                gjahr it_bkpf-gjahr
                                binary search.
    if sy-subrc 0.
      loop at it_bseg from sy-tabix.
        if it_bseg-bukrs it_bkpf-bukrs and
           it_bseg-belnr it_bkpf-belnr and
           it_bseg-gjahr it_bkpf-gjahr.
          move-corresponding it_bkpf to it_wl.
          move-corresponding it_bseg to it_wl.
          append it_wl.
          clear it_wl.
        else.
          exit.
        endif.
      endloop.
    endif.
  endloop.
  loop at it_wl.
    wa it_wl.
    l_item l_item + 1.
    wa-item l_item.
    at end of gjahr.
      clear l_item.
    endat.
    modify it_wl from wa transporting item.
    clear wa.
  endloop.
endform.                    " GET_GI_ACCOUNT_DOCUMENT
*&---------------------------------------------------------------------*
*&      Form  GET_BILLING_ACCOUNT_DOCUMENT
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
form get_billing_account_document .
  datait_bkpf like table of bkpf with header line.
  datait_bseg like table of bseg with header line.
  datal_item like sy-tabix.
  datawa like it_bill.
  check it_vbrp[] is not initial.

  select bkpf~belnr
         bkpf~gjahr
         bkpf~bukrs
         bkpf~budat
         bkpf~awkey
         bkpf~blart
  into corresponding fields of table it_bkpf
  from bkpf
  for all entries in it_vbrp
  where bkpf~awtyp 'VBRK'
    and bkpf~awkey it_vbrp-awkey
    and bkpf~budat in s_budat
    .
  if sy-subrc 0.
    sort it_bkpf by bukrs belnr gjahr.
    select
           bseg~bukrs
           bseg~belnr
           bseg~gjahr
           bseg~buzei
           bseg~shkzg
           bseg~dmbtr
           bseg~pswsl
           bseg~wrbtr
    into corresponding fields of table it_bseg
    from bseg
    for all entries in it_bkpf
    where bseg~bukrs it_bkpf-bukrs
      and bseg~belnr it_bkpf-belnr
      and bseg~gjahr it_bkpf-gjahr
      and bseg~hkont in s_hkont
      and bseg~vbel2 in s_order
      and bseg~posn2 in s_item
      and bseg~matnr in s_matnr.
  endif.
  sort it_bseg by bukrs belnr gjahr buzei.
  loop at it_bkpf.
    read table it_bseg with key bukrs it_bkpf-bukrs
                                belnr it_bkpf-belnr
                                gjahr it_bkpf-gjahr
                                binary search.
    if sy-subrc 0.
      loop at it_bseg from sy-tabix.
        if it_bseg-bukrs it_bkpf-bukrs and
           it_bseg-belnr it_bkpf-belnr and
           it_bseg-gjahr it_bkpf-gjahr.
          move-corresponding it_bkpf to it_bill.
          move-corresponding it_bseg to it_bill.
          it_bill-xblnr it_bkpf-awkey.
          append it_bill.
          clear it_bill.
        else.
          exit.
        endif.
      endloop.
    endif.
  endloop.
  loop at it_bill.
    wa it_bill.
    l_item l_item + 1.
    wa-item l_item.
    at end of gjahr.
      clear l_item.
    endat.
    modify it_bill from wa transporting item.
    clear wa.
  endloop.
endform.                    " GET_BILLING_ACCOUNT_DOCUMENT
*&---------------------------------------------------------------------*
*&      Form  GET_BILLING_DATA
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
form get_document_flow.
  datal_item like sy-tabix.
  datawa like it_vbfa.
  datawa_vbrp like it_vbrp.
  datait_vbfa_bill like table of it_vbfa with header line.
  sort it_ship by vbeln posnr.
*Get billing according to DN
  select vbfa~vbelv as xblnr
         vbfa~vbelv
         vbfa~posnv
         vbfa~vbeln
         vbfa~vbeln as awkey
         vbfa~posnn
         vbfa~vbtyp_v
         vbfa~vbtyp_n
         vbfa~mjahr
  into corresponding fields of table it_vbfa
  from vbfa
  for all entries in it_ship
  where vbfa~vbelv it_ship-vbeln
*    and vbfa~posnv = it_ship-posnr
    and vbfa~vbtyp_n in ('M','N','R','h','O')
    and vbfa~vbtyp_v in ('J','T')
    and vbfa~rfmng > 0.

  loop at it_vbfa where vbtyp_n 'R'
                   or vbtyp_n 'h'.
    wa it_vbfa.
*    concatenate wa-vbeln wa-mjahr into wa-awkey.
    append wa to it_vbfa_md.
    clear wa.
    delete it_vbfa.
  endloop.
  perform get_awkey tables it_vbfa_md.
  delete it_vbfa where vbtyp_n 'R'
                    or vbtyp_n 'h'.
  check it_vbfa[] is not initial.
  select vbeln
         vbeln as awkey
         posnr
         vgbel
         vgpos
         aubel
         aupos
         wavwr
    into corresponding fields of table it_vbrp
    from vbrp
    for all entries in it_vbfa
    where vbeln it_vbfa-vbeln
      and wavwr > 0.

  sort it_vbrp by vbeln posnr.
  loop at it_vbrp.
    wa_vbrp it_vbrp.
    l_item l_item + 1.
    wa_vbrp-item l_item.
    at end of vbeln.
      clear l_item.
    endat.
    modify it_vbrp from wa_vbrp transporting item.
    clear wa_vbrp.
  endloop.
*  it_vbfa[] =  it_vbfa_bill[].
endform.                    " GET_BILLING_DATA
*&---------------------------------------------------------------------*
*&      Form  get_other_account_document
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
form get_other_account_document .
  data:l_tabix like sy-tabix.
  check s_kunnr[] is initial.
  check s_vbeln[] is initial.
  check s_order[] is initial.
  check s_matnr[] is initial.
  check s_hkont[] is not initial.
  select bsis~belnr
         bsis~gjahr
         bsis~buzei
         bsis~budat
         bsis~shkzg
         bsis~xblnr
         bsis~blart
         bsis~dmbtr
         bsis~pswsl
         bsis~wrbtr
  into corresponding fields of table it_other
  from bkpf inner join bsis
  on bkpf~bukrs bsis~bukrs
  and bkpf~belnr bsis~belnr
  and bkpf~gjahr bsis~gjahr
  where bkpf~bukrs in s_bukrs
    and bkpf~budat in s_budat
    and bsis~hkont in s_hkont
    and bkpf~blart 'SA'.

  sort it_wl by belnr gjahr buzei.
endform.                    " get_other_account_document
*&---------------------------------------------------------------------*
*&      Form  PROCESS_DATA
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
form process_data .
  perform collect_dn_amount.
  perform collect_billing_amount.
  perform delete_dn.
  perform combine_data.
endform.                    " PROCESS_DATA
*&---------------------------------------------------------------------*
*&      Form  collect_dn
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
form collect_dn_amount .
  data:l_tabix like sy-tabix,
       l_tabix1 like sy-tabix.
  sort it_wl by awkey item.
  sort it_vbfa_md by vbelv posnv.
  loop at it_ship.
*  find GI account document
    read table it_vbfa_md with key vbelv it_ship-vbeln
                                   posnv it_ship-posnr
                                   binary search.
    if sy-subrc 0.
      l_tabix1 sy-tabix.
      loop at it_vbfa_md from l_tabix1.
        l_tabix1 l_tabix1 + 1.
        if it_vbfa_md-vbelv <> it_ship-vbeln or
           it_vbfa_md-posnv <> it_ship-posnr.
          exit.
        endif.
        read table it_wl with key awkey it_vbfa_md-awkey
                                  item  it_vbfa_md-posnn
                                  binary search.
        if sy-subrc 0.
          l_tabix sy-tabix.
          loop at it_wl from l_tabix.
            l_tabix l_tabix + 1.
            if it_wl-awkey <> it_vbfa_md-awkey or
               it_wl-item  <> it_vbfa_md-posnn.
              exit.
            endif.
            if it_wl-shkzg eq 'H'.
              it_wl-dmbtr -* it_wl-dmbtr.
            endif.

            it_dn_sum-vbeln it_ship-vbeln.
            it_dn_sum-posnr it_ship-posnr.
            it_dn_sum-vgbel it_ship-vgbel.
            it_dn_sum-vgpos it_ship-vgpos.
            it_dn_sum-dmbtr it_wl-dmbtr.

            collect it_dn_sum.
            clear it_dn_sum.

            modify it_wl transporting dmbtr.
          endloop.
        endif.
      endloop.
    endif.
  endloop.
endform.                    " collect_dn
*&---------------------------------------------------------------------*
*&      Form  COLLECT_BILLING_AMOUNT
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
form collect_billing_amount .
  data:l_tabix like sy-tabix,
       l_tabix1 like sy-tabix.

  sort it_vbrp by vgbel vgpos.
  sort it_bill by awkey item wrbtr.

  loop at it_ship.
*  find GI account document
    read table it_vbrp with key vgbel it_ship-vbeln
                                vgpos it_ship-posnr
                                binary search.
    if sy-subrc 0.
      l_tabix1 sy-tabix.
      loop at it_vbrp from l_tabix1.
        l_tabix1 l_tabix1 + 1.
        if it_vbrp-vgbel <> it_ship-vbeln or
           it_vbrp-vgpos <> it_ship-posnr.
          exit.
        endif.
        read table it_bill with key awkey it_vbrp-awkey
                                    item  it_vbrp-item
                                    binary search.
        if sy-subrc 0.
          l_tabix sy-tabix.
          loop at it_bill from l_tabix.
            l_tabix l_tabix + 1.
            if it_bill-awkey <> it_vbrp-awkey or
               it_bill-item  <> it_vbrp-item.
              exit.
            endif.
            if it_bill-shkzg eq 'H'.
              it_bill-dmbtr -* it_bill-dmbtr.
            endif.

            it_dn_sum-vbeln it_ship-vbeln.
            it_dn_sum-posnr it_ship-posnr.
            it_dn_sum-vgbel it_ship-vgbel.
            it_dn_sum-vgpos it_ship-vgpos.
            it_dn_sum-dmbtr it_bill-dmbtr.

            collect it_dn_sum.
            clear it_dn_sum.
            modify it_bill transporting dmbtr.
          endloop.
        endif.
      endloop.
    endif.
  endloop.
endform.                    " COLLECT_BILLING_AMOUNT
*&---------------------------------------------------------------------*
*&      Form  delete_DN
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
form delete_dn .
  data:l_tabix like sy-tabix.
  delete it_dn_sum where dmbtr is initial.
  sort it_dn_sum by vbeln posnr.

  loop at it_ship.
    l_tabix sy-tabix.
    read table it_dn_sum with key vbeln it_ship-vbeln
                                  posnr it_ship-posnr
                                  binary search.
    if sy-subrc <> 0.
      delete it_ship index l_tabix.
    endif.
  endloop.
endform.                    " delete_DN
*&---------------------------------------------------------------------*
*&      Form  COMBINE_DATA
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
form combine_data .
  data:l_tabix like sy-tabix,
       l_tabix1 like sy-tabix,
       l_flag,
       l_awkey like bkpf-awkey.

  sort it_wl by awkey item.
  sort it_vbfa_md by vbelv posnv.
  sort it_vbrp by vgbel vgpos.
  sort it_bill by awkey item.
  loop at it_ship.
    move-corresponding it_ship to it_output.
    clear:l_tabix,l_tabix1.
*  Append GI account document to output table
    read table it_vbfa_md with key vbelv it_ship-vbeln
                                   posnv it_ship-posnr
                                   binary search.
    if sy-subrc 0.
      l_tabix1 sy-tabix.
      loop at it_vbfa_md from l_tabix1.
        l_tabix1 l_tabix1 + 1.
        if it_vbfa_md-vbelv <> it_ship-vbeln or
           it_vbfa_md-posnv <> it_ship-posnr.
          exit.
        endif.
        read table it_wl with key awkey it_vbfa_md-awkey
                                  item  it_vbfa_md-posnn
                                  binary search.
        if sy-subrc 0.
          l_tabix sy-tabix.
          loop at it_wl from l_tabix.
            l_tabix l_tabix + 1.
            if it_wl-awkey <> it_vbfa_md-awkey or
               it_wl-item  <> it_vbfa_md-posnn.
              exit.
            endif.
            move-corresponding it_wl to it_output.
            if it_output-dmbtr < 0.
              it_output-lfimg absit_output-lfimg -).
            else.
              it_output-lfimg absit_output-lfimg ).
            endif.
*         净价值
*            it_output-netwr = ( it_ship-lfimg * it_ship-netpr ) / it_ship-kpein.
*             it_output-netwr = ( it_output-lfimg * it_ship-netpr ) / it_ship-kpein.
            it_output-netwr it_output-lfimg * it_ship-netwr1 / it_ship-kwmeng.
            it_output-netwr2 it_output-netwr * it_ship-kursk.
*         税额
*            it_output-tax_amt = ( it_ship-lfimg * it_ship-mwsbp ) / it_ship-kwmeng.
            it_output-tax_amt it_output-lfimg * it_ship-mwsbp / it_ship-kwmeng.
            it_output-tax_amt1 it_output-tax_amt * it_ship-kursk.
            append it_output.
          endloop.
          l_flag 'X'.
        endif.
      endloop.
    endif.

    clear:l_tabix,l_tabix1.
*  Append billing account document to output table
    read table it_vbrp with key vgbel it_ship-vbeln
                                vgpos it_ship-posnr
                                binary search.
    if sy-subrc 0.
      l_tabix1 sy-tabix.
      loop at it_vbrp from l_tabix1.
        l_tabix1 l_tabix1 + 1.
        if it_vbrp-vgbel <> it_ship-vbeln or
           it_vbrp-vgpos <> it_ship-posnr.
          exit.
        endif.
        read table it_bill with key awkey it_vbrp-awkey
                                    item  it_vbrp-item
                                    binary search.
        if sy-subrc 0.
          l_tabix sy-tabix.
          loop at it_bill from l_tabix.
            l_tabix l_tabix + 1.
            if it_bill-awkey it_vbrp-awkey and
               it_bill-item  it_vbrp-item.
              move-corresponding it_bill to it_output.
              if it_output-dmbtr < 0.
                it_output-lfimg absit_output-lfimg -).
              else.
                it_output-lfimg absit_output-lfimg ).
              endif.
*         净价值
*              it_output-netwr = ( it_ship-lfimg * it_ship-netpr ) / it_ship-kpein.
*              it_output-netwr = ( it_output-lfimg * it_ship-netpr ) / it_ship-kpein.
              it_output-netwr it_output-lfimg * it_ship-netwr1 / it_ship-kwmeng.
              it_output-netwr2 it_output-netwr * it_ship-kursk.
*         税额
*              it_output-tax_amt = ( it_ship-lfimg * it_ship-mwsbp ) / it_ship-kwmeng.
              it_output-tax_amt it_output-lfimg * it_ship-mwsbp / it_ship-kwmeng.
              it_output-tax_amt1 it_output-tax_amt * it_ship-kursk.
              append it_output.
            else.
              exit.
            endif.
          endloop.
          l_flag 'X'.
        endif.
      endloop.
    endif.

    if l_flag is initial.
      append it_output.
    endif.
    clearit_output,l_flag,l_tabix.
  endloop.



* append manual account document
  loop at it_other.
    if it_other-shkzg eq 'H'.
      it_other-dmbtr -* it_other-dmbtr.
    endif.
    move-corresponding it_other to it_output.
    append it_output.
    clear it_output.
  endloop.
endform.                    " COMBINE_DATA
*&---------------------------------------------------------------------*
*&      Form  GET_AWKEY
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*      -->P_IT_VBFA_MD  text
*----------------------------------------------------------------------*
form get_awkey tables pt_table like it_vbfa[].
  data:it_mkpf like table of mkpf with header line.
  check pt_table[] is not initial.
  select mblnr mjahr xblnr into corresponding fields of table it_mkpf
    from mkpf
    for all entries in pt_table
    where mblnr pt_table-vbeln
      and xblnr pt_table-xblnr.

  sort it_mkpf by mblnr xblnr.
  loop at pt_table.
    read table it_mkpf with key mblnr pt_table-vbeln
                                xblnr pt_table-xblnr
                                binary search.
    check sy-subrc 0.
    concatenate pt_table-vbeln it_mkpf-mjahr into pt_table-awkey.
    pt_table-mjahr it_mkpf-mjahr.
    modify pt_table transporting awkey mjahr.
  endloop.
endform.                    " GET_AWKEY
*&---------------------------------------------------------------------*
*&      Form  GET_ZBV
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*

form get_zbv .

  data  vbelv like vbfa-vbelv.
  datakbetr type decimals 2,
        netpr like vbap-netpr,
        knumv like vbak-knumv.

  dataline type i,
        num type i.

  select distinct
      vbak~vkorg                "销售组织
      vbak~vbeln as vgbel       "销售凭证
      vbap~posnr as vgpos       "销售凭证项目
      vbap~waerk                "SD 凭证货币
      vbap~matnr                "物料号
      vbap~arktx                "物料文本
      vbap~kwmeng as lfimg      "数量
      vbak~kunnr
      kna1~name1                "客户名称
      vbak~knumv
    appending corresponding fields of table itab
    from vbak
    inner join vbap    on vbak~vbeln vbap~vbeln
    inner join kna1    on kna1~kunnr vbak~kunnr
    where vbak~vbeln in s_order
      and vbap~matnr in s_matnr
      and kna1~kunnr in s_kunnr
      and vbak~auart 'ZBV'.

  loop at itab.
    clearit_vbfa[],it_vbfa.

    select *
      into corresponding fields of table it_vbfa
      from vbfa
       where vbelv itab-vgbel
         and posnv itab-vgpos
         and vbtyp_n 'R'
          or vbtyp_n 'h' ).

    describe table it_vbfa lines line.

    if line 0.
      num line div 2.
      num num * 2.

      if num line.
        delete itab.
        continue.
      endif.
    else .
      delete itab.
      continue.
    endif.

    refresh it_vbfa.

    select   vbfa~vbelv as xblnr
             vbfa~vbelv
             vbfa~posnv
             vbfa~vbeln
             vbfa~vbeln as awkey
             vbfa~posnn
             vbfa~vbtyp_v
             vbfa~vbtyp_n
             vbfa~mjahr
      into corresponding fields of table it_vbfa
      from vbfa
      inner join vbrk on vbrk~vbeln vbfa~vbeln
      where vbfa~vbelv itab-vgbel
        and vbfa~posnv itab-vgpos
        and vbfa~vbtyp_n 'M'
        and vbrk~fkdat in s_budat
        and vbfa~rfmng > 0.

    loop at it_vbfa.
      select single vbelv
        into vbelv
        from vbfa
        inner join vbrk on vbrk~vbeln vbfa~vbeln
        where vbfa~vbelv it_vbfa-vbeln
          and vbfa~posnv it_vbfa-posnn
          and vbrk~fkdat in s_budat
          and vbfa~vbtyp_n 'N'.

      if sy-subrc 0.
        delete it_vbfa.
      endif.
    endloop.

    if it_vbfa[] is not initial.
      delete itab.
    endif.

  endloop.

  loop at itab.
***汇率
    select single kursk
      into itab-kursk
      from vbkd
      where vbeln itab-vgbel
        and posnr ''.

***计算不含税价格
    clear:knumvkbetr.
    select single knumh
      into knumv
      from konv
      where knumv itab-knumv
        and kposn itab-vgpos
        and kschl 'MWSI'.

    select single kbetr
      into kbetr
      from konp
      where knumh knumv.

    kbetr kbetr / 1000.

    select single kbetr
      into netpr  "单价含税价
      from konv
      where knumv itab-knumv
        and kposn itab-vgpos
        and kappl 'V'
        and kschl 'ZR00'.

    itab-netwr2   =  netpr / + kbetr * itab-lfimg.  "净价
    itab-tax_amt1 =  netpr * itab-lfimg itab-netwr2.    "税

    if itab-waerk 'RMB'.
      itab-netwr   itab-netwr2.
      itab-tax_amt itab-tax_amt1.
    else.
      itab-netwr   itab-netwr2   * itab-kursk .
      itab-tax_amt itab-tax_amt1 * itab-kursk .
    endif.

    select single kwert "kbetr
      into itab-dmbtr
      from konv
      where knumv itab-knumv
        and kposn itab-vgpos
        and kappl 'V'
        and kschl 'Z001'.

    move-corresponding itab to it_output.
    append it_output.
  endloop.
endform.                    " GET_ZBV

****************************************************************************************************************

*&---------------------------------------------------------------------*
*&  Include           ZRSD0139_OUT                                     *
*&---------------------------------------------------------------------*
*&---------------------------------------------------------------------*
*&      Form  F4_FOR_VARIANT
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
form f4_for_variant .
  data:is_variant like disvariant.
  is_variant          sy-repid.
  call function 'REUSE_ALV_VARIANT_F4'
          exporting
               is_variant          is_variant
               i_save              g_save
*           it_default_fieldcat =
          importing
               e_exit              g_exit
               es_variant          gx_variant
          exceptions
               not_found 2.

  if sy-subrc 2.
    message id sy-msgid type 'S' number sy-msgno
    with sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
  else.
    if g_exit is initial.
      p_layt gx_variant-variant.
    endif.
  endif.

endform.                    " F4_FOR_VARIANT

*&---------------------------------------------------------------------
*
*&      Form  output
*&---------------------------------------------------------------------
*
*       text
*----------------------------------------------------------------------
*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------
*
form output .
  if p_layt is not initial.
    alv_variant-variant p_layt.
  endif.
* build fieldcat
  perform sub_build_fieldcat.
  if p_alv 'X'.
    perform output_alv.
  else.
    perform download_to_server.
  endif.
endform.                    " output
*&---------------------------------------------------------------------*
*&      Form  SUB_BUILD_FIELDCAT
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
form sub_build_fieldcat .
  data:l_text1(40),
       l_field1(40),
       l_text2(40),
       l_fiel2(40),
       l_no_out.
  datafieldcat_ln type slis_fieldcat_alv.
  data:ls_fcat      like line of it_fcat.
  datatab(10type .
  data col_pos type value 0.
  define m1.
    add to col_pos.
    fieldcat_ln-fieldname     &1.
    fieldcat_ln-seltext_l     &2.
    fieldcat_ln-sp_group      &3"group specification
    fieldcat_ln-outputlen     &4.
    fieldcat_ln-no_zero       &5"write no-zero
    fieldcat_ln-do_sum        &6.
    fieldcat_ln-no_out        &7.
    fieldcat_ln-col_pos       col_pos.
    if fieldcat_ln-fieldname 'MATNR'.
      fieldcat_ln-ref_fieldname 'MATNR'.
      fieldcat_ln-ref_tabname   'MARA'.
    endif.
    if fieldcat_ln-fieldname 'KUNNR'.
      fieldcat_ln-ref_fieldname 'KUNNR'.
      fieldcat_ln-ref_tabname   'KNA1'.
    endif.
    append fieldcat_ln to gt_fieldcat.
    clear fieldcat_ln.
  end-of-definition.

  m1 'KUNNR' '客户编号'   'M' '20' 'X' '' ''.
  m1 'NAME1' '客户名称'   'M' '40' ' ' '' ''.
  m1 'VKORG' '销售组织 '  'M' '18' '' '' ''.
  m1 'VBELN' '发货单号'   'M' '20' 'X' '' ''.
  m1 'POSNR' '发货单行项目'   'M' '20' 'X' '' ''.
  m1 'VGBEL' '销售订单'   'M' '20' 'X' '' ''.
  m1 'VGPOS' '订单行项目' 'M' '20' 'X' '' ''.
  m1 'MATNR' '物料'   'M' '20' '' '' ''.
  m1 'ARKTX' '物料描述'   'M' '40' '' '' ''.
  m1 'LFIMG' '发货数量'   'M' '20' '' 'X' ''.
  m1 'NETWR' '外币净价值'     'M' '20' '' 'X' ''.
  m1 'TAX_AMT' '外币税额'     'M' '20' '' 'X' ''.
  m1 'WAERK'  '币别'      'M' '20' '' '' ''.
  m1 'DMBTR'  '成本'      'M' '20' '' 'X' ''.
  m1 'WADAT_IST' '发货日期' 'M' '20' '' '' ''.
  m1 'KURSK'  '汇率'      'M' '20' '' '' ''.
  m1 'NETWR2' '本位币币净价值'     'M' '20' '' 'X' ''.
  m1 'TAX_AMT1' '本位币税额'     'M' '20' '' 'X' ''.
  m1 'BELNR'  '会计凭证'   'M' '20' '' '' ''.
  m1 'BUZEI'  '凭证项目'   'M' '20' '' '' ''.
  m1 'GJAHR'  '会计年度'   'M' '20' '' '' ''.
endform.                    " SUB_BUILD_FIELDCAT

*&---------------------------------------------------------------------
*
*&      Form  OUTPUT_ALV
*&---------------------------------------------------------------------
*
*       text
*----------------------------------------------------------------------
*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------
*
form output_alv .
  g_repid sy-repid.
  perform eventtab_build using gt_events[].
*"List Header for Top-Of-Page
  perform comment_build using gt_list_top_of_page[].

  gs_layout-window_titlebar  sy-title.
  gs_layout-zebra 'X'.
  gs_layout-colwidth_optimize 'X'.

  call function 'REUSE_ALV_GRID_DISPLAY'
    exporting
      i_background_id    'ALV_BACKGROUND'
      i_callback_program      g_repid
      i_bypassing_buffer      'X'
      is_layout               gs_layout
      it_fieldcat             gt_fieldcat[]
*      IT_SORT                 = GS_SORT[]
      i_callback_user_command 'USER_COMMAND'
      i_default               'X'
      i_save                  'A'
      it_events               gt_events[]
      is_variant              alv_variant
      is_print                alv_print
    tables
      t_outtab                it_output.

endform.                    " OUTPUT_ALV
*&---------------------------------------------------------------------
*
*&      Form  fieldcat_init
*&---------------------------------------------------------------------
*
*       text
*----------------------------------------------------------------------
*
*      -->P_GT_FIELDCAT  text
*----------------------------------------------------------------------
*
form fieldcat_init using    rt_fieldcat type slis_t_fieldcat_alv
                          .
  datafieldcat_ln type slis_fieldcat_alv.

  loop at gt_fieldcat into fieldcat_ln.
    fieldcat_ln-no_out ''.
    append fieldcat_ln to rt_fieldcat.
  endloop.

endform.                    " fieldcat_init
*&---------------------------------------------------------------------
*
*&      Form  build_sort
*&---------------------------------------------------------------------
*
*       text
*----------------------------------------------------------------------
*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------
*
form build_sort .
  clear gs_sort.
  gs_sort-spos       1.
  gs_sort-up         'X'.
  gs_sort-subtot     'X'.
  gs_sort-obligatory 'X'.
  gs_sort-group      '*'.
  gs_sort-fieldname  'TYPE'.
  append gs_sort.
endform.                    " build_sort
*&---------------------------------------------------------------------
*
*&      Form  download_to_server
*&---------------------------------------------------------------------
*
*       text
*----------------------------------------------------------------------
*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------
*
form download_to_server .
  data:p_det type value 'X'.
  data:lt_fieldcat type slis_t_fieldcat_alv.
  data:ls_fcat like line of gt_fieldcat.
  data:l_field(400)   type c.
  data:l_string(4000type c,
       l_index type i.
  field-symbols:  type any,
                 type any.
  data:wa_line type ref to data.
  field-symbols type any,
                  type any,
                  type any.

  data:begin of lt_out occurs 0,
      text(60),
      qty(20),
      amt(20),
      end of lt_out.

  if p_layt is initial.         "No Variant is selected
    lt_fieldcat[] gt_fieldcat[].
  else.
    perform get_alv_variant_detail using alv_variant
                                         gt_fieldcat[]
                                         gs_layout
                                changing lt_fieldcat.
  endif.
  try.
      concatenate sy-datum '_PR_Aging _Del'  '.xls'
          into p_svfile.

      concatenate p_svpath p_svfile into g_svfile.
      open dataset g_svfile for output in text mode encoding default.
      if sy-subrc 0.

        loop at lt_fieldcat into ls_fcat where no_out ''.
          l_index l_index + 1.
          condense l_field.
          if l_index 1.
            l_string ls_fcat-seltext_l.
          else.
            concatenate l_string  ls_fcat-seltext_l into l_string
                 separated by
                 cl_abap_char_utilities=>horizontal_tab.
          endif.
        endloop.

        transfer l_string to g_svfile.
        clear  l_string.

        loop at it_output.
          clear l_index.
          loop at lt_fieldcat into ls_fcat where no_out ''.
            l_index l_index + 1.
            assign component  ls_fcat-fieldname
                of structure it_output to .
            l_field =  .
            condense l_field.
            perform process_special_character changing l_field.
            if l_index 1.
              l_string =  l_field.
            else.
              concatenate l_string  l_field into l_string
                    separated by
                    cl_abap_char_utilities=>horizontal_tab.
            endif.
          endloop.
          transfer l_string to g_svfile.
          clear  l_string.
        endloop.

        close dataset g_svfile.
        write:'Export is successful.'.
        write:'Temporary file created on server:'g_svfile.
      else.
        write:'Export is failed.'.
      endif.
    catch cx_root.
      write:'Temporary file writing error on server.'.
  endtry.

endform.                    " download_to_server
*&--------------------------------------------------------------------*
*&      Form  PROCESS_SPECIAL_CHARACTER
*&--------------------------------------------------------------------*
*       text
*---------------------------------------------------------------------*
*      -->P_FIELD    text
*---------------------------------------------------------------------*
form process_special_character  changing p_field.
  condense p_field.
  search p_field for '"'.
  if sy-subrc 0.
    if p_field(1'"'.
      replace all occurrences of '"' in p_field with '""'.
      concatenate '"' p_field '"' into p_field.
    else.
      replace all occurrences of '"' in p_field with '""'.
      search p_field for ','.
      if sy-subrc 0.
        concatenate '"' p_field '"' into p_field.
      endif.
    endif.
  else.
    search p_field for ','.
    if sy-subrc 0.
      concatenate '"' p_field '"' into p_field.
    endif.
  endif.

endform.                    " PROCESS_SPECIAL_CHARACTER
*&--------------------------------------------------------------------*
*&      Form  USER_COMMAND
*&--------------------------------------------------------------------*
*       text
*---------------------------------------------------------------------*
*      -->I_UCOMM    text
*      -->RS_SELFIELDtext
*---------------------------------------------------------------------*
form user_command using i_ucomm like sy-ucomm
                      rs_selfield type slis_selfield.
  field-symbols type any,
                  type any.
  case i_ucomm.
    when 'DOUBLE'.
*      READ TABLE IT_EBAN INTO WA_EBAN INDEX RS_SELFIELD-TABINDEX.
*      CHECK SY-SUBRC = 0.
*      IF RS_SELFIELD-FIELDNAME = 'MATNR' OR
*         RS_SELFIELD-FIELDNAME = 'MAKTX' OR
*         RS_SELFIELD-FIELDNAME(4) = 'FCST'.
*        ASSIGN COMPONENT 'MATNR'
*                OF STRUCTURE  TO .
*      ELSE.
*        ASSIGN COMPONENT 'ROW_MAT'
*                OF STRUCTURE  TO .
*      ENDIF.
*      ASSIGN COMPONENT 'WERKS'
*        OF STRUCTURE  TO .

*      SET PARAMETER ID 'BAN' FIELD WA_EBAN-BANFN.
*      CALL TRANSACTION 'ME53N' AND SKIP FIRST SCREEN.
  endcase.

  clear i_ucomm.
endform.                    "USER_COMMAND

*&---------------------------------------------------------------------*
*&      Form  COMMENT_BUILD
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*      -->P_GT_LIST_TOP_OF_PAGE[]  text
*----------------------------------------------------------------------*
form comment_build using lt_top_of_page type
                                        slis_t_listheader.
  datals_line type slis_listheader.
  datals_rm61r type rm61r.
  data:l_bukrs(20).


* Zeitstempel ermitteln
  perform get_timestamp changing ls_rm61r-memda
                                 ls_rm61r-memtm.

* LIST HEADING LINE: TYPE H
  clear ls_line.
  ls_line-typ  'H'.
* LS_LINE-KEY:  NOT USED FOR THIS TYPE
  ls_line-info sy-title.
  append ls_line to lt_top_of_page.

* Kopfinfo: Typ S
  clear ls_line.
  ls_line-typ  'S'.
  ls_line-key  '公司代码:'.
  ls_line-info s_bukrs-low.
  append ls_line to lt_top_of_page.

* Kopfinfo: Typ S
  clear ls_line.
  ls_line-typ  'S'.
  ls_line-key  '过账日期:'.
  concatenate s_budat-low ' -  ' s_budat-high
    into ls_line-info.
  append ls_line to lt_top_of_page.

* Aktionsinfo: Typ A
  clear ls_line.
  ls_line-typ  'A'.
* LS_LINE-KEY:  not used for this type
  ls_line-info 'Current data from'.
  write ls_rm61r-memda to ls_line-info+20(10).
  ls_line-info+32(3)  'At'.
  ls_line-info+35(5)  ls_rm61r-memtm.
  ls_line-info+41(4)  'Time'.
  append ls_line to  lt_top_of_page.


endform.                    "COMMENT_BUILD
*&--------------------------------------------------------------------*
*&      Form  TOP_OF_PAGE
*&--------------------------------------------------------------------*
*       text
*---------------------------------------------------------------------*
form top_of_page.
  call function 'REUSE_ALV_COMMENTARY_WRITE'
    exporting
      i_logo             'ASAT_LOGO'
      it_list_commentary gt_list_top_of_page.
endform.                    "TOP_OF_PAGE
*&--------------------------------------------------------------------*
*&      Form  GET_TIMESTAMP
*&--------------------------------------------------------------------*
*       text
*---------------------------------------------------------------------*
*      -->MEMDA      text
*      -->MEMTM      text
*---------------------------------------------------------------------*
form get_timestamp  changing memda like rm61r-memda
                             memtm like rm61r-memtm.
  datalv_tstmp type timestamp,
        lv_time  type sy-uzeit.

  get time stamp field lv_tstmp.
*--> Ermitteln Datum und Zeit aus dem Timestamp
  convert time stamp lv_tstmp time zone sy-zonlo
          into date memda time lv_time.
*--> Zeit #ernehmen ohne Sekunden
  write lv_time using edit mask '__:__' to memtm.

endform.                    " get_timestamp
*&---------------------------------------------------------------------*
*&      Form  EVENTTAB_BUILD
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*      -->P_GT_EVENTS[]  text
*----------------------------------------------------------------------*
form eventtab_build using rt_events type slis_t_event.
*"Registration of events to happen during list display
  datals_event type slis_alv_event.
*
  call function 'REUSE_ALV_EVENTS_GET'
    exporting
      i_list_type 0
    importing
      et_events   rt_events.
*  READ TABLE RT_EVENTS WITH KEY NAME = SLIS_EV_TOP_OF_PAGE
*                           INTO LS_EVENT.
*  IF SY-SUBRC = 0.
*    MOVE G_TOP_OF_PAGE TO LS_EVENT-FORM.
*    APPEND LS_EVENT TO RT_EVENTS.
*  ENDIF.
endform.                    "EVENTTAB_BUILD
*&---------------------------------------------------------------------*
*&      Form  GET_ALV_VARIANT_DETAIL
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*      -->P_G_VARIANT  text
*      -->P_GT_FIELDCAT[]  text
*      -->P_G_LAYOUT  text
*      <--P_GT_FCAT_SAV[]  text
*      <--P_GT_SORT_SAV[]  text
*      <--P_GT_FILTER_SAV[]  text
*      <--P_GS_LAYOUT_SAV  text
*----------------------------------------------------------------------*
form get_alv_variant_detail
                    using    p_variant
                             p_fieldcat type  slis_t_fieldcat_alv
                             ps_layout
                    changing pt_fieldcat type slis_t_fieldcat_alv.

  datalt_fcat_def    type kkblo_fieldcat occurs with header line.
  datalt_fcat_sav    type kkblo_fieldcat occurs with header line.
  datalt_sort        type kkblo_sortinfo occurs with header line.
  datalt_filter      type kkblo_filter   occurs with header line.

  datalt_layout      type kkblo_layout.
  datalt_fieldcat    type slis_fieldcat_alv.
  datawa_sav like gt_fcat_sav.
  datafieldcat_ln type slis_fieldcat_alv.


  loop at p_fieldcat into lt_fieldcat.
    move-corresponding lt_fieldcat   to lt_fcat_def.
    append lt_fcat_def.
  endloop.

  move-corresponding ps_layout   to lt_layout.

  call function 'LT_FC_LOAD'
    exporting
*     I_TOOL                    = 'LT'
      is_variant                p_variant
      i_tabname                 'MASTER'
*     I_TABNAME_SLAVE           =
    importing
      et_fieldcat               lt_fcat_sav[]
*      ET_SORT                   = LT_SORT[]
*      ET_FILTER                 = LT_FILTER[]
    changing
      cs_layout                 lt_layout
      ct_default_fieldcat       lt_fcat_def[]
    exceptions
      fc_not_complete           1
      others                    2
            .
  if sy-subrc <> 0.
  else.
*    PT_FCAT_SAV[]   = LT_FCAT_SAV[].
*    PT_SORT_SAV[]   = LT_SORT[].
*    PT_FILTER_SAV[] = LT_FILTER[].
*    MOVE-CORRESPONDING LT_LAYOUT   TO PS_LAYOUT_SAV.
  endif.
  sort  lt_fcat_sav by  col_pos.
  loop at lt_fcat_sav into wa_sav where no_out <> 'X'.
    move-corresponding wa_sav to fieldcat_ln.
    append fieldcat_ln to pt_fieldcat.
    clear fieldcat_ln.
  endloop.

endform.                    " GET_ALV_VARIANT_DETAIL
*&--------------------------------------------------------------------*
*&      Form  CONVERSION_EXIT_MATN1_OUTPUT
*&--------------------------------------------------------------------*
*       text
*---------------------------------------------------------------------*
*      -->P_IN       text
*      -->P_OUT      text
*---------------------------------------------------------------------*
form conversion_exit_matn1_output  using    p_in
                                   changing p_out.
  call function 'CONVERSION_EXIT_MATN1_OUTPUT'
    exporting
      input  p_in
    importing
      output p_out.
endform.                    " CONVERSION_EXIT_MATN1_OUTPUT
*&---------------------------------------------------------------------*
*&      Form  CONVERSION_EXIT_ALPHA_OUTPUT
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*      <--P_IT_TAB_LIFNR  text
*----------------------------------------------------------------------*
form conversion_exit_alpha_output using p_in changing p_out.
  call function 'CONVERSION_EXIT_ALPHA_OUTPUT'
    exporting
      input  p_in
    importing
      output p_out.

endform.                    " CONVERSION_EXIT_ALPHA_OUTPUT**

***************************************************************************************************************
P_ALV ALV Output
P_DNLOD Download on Server
P_LAYT Layout
P_SVPATH File Path on Server
S_BUDAT 过账日期
S_BUKRS 公司代码
S_HKONT 总帐科目
S_ITEM 销售订单行项目
S_KUNNR 客户
S_MATNR 物料号 
S_ORDER 销售订单 
S_VBELN 交货单 
S_WERKS 工厂

请使用浏览器的分享功能分享到微信等