SAP R/3 форум ABAP консультантов
Russian ABAP Developer's Club

Home - FAQ - Search - Memberlist - Usergroups - Profile - Log in to check your private messages - Register - Log in - English
Blogs - Weblogs News

ALV with Dynamic Table



 
Post new topic   Reply to topic    Russian ABAP Developer's Club Forum Index -> Dynamic Programming | Динамическое программирование
View previous topic :: View next topic  
Author Message
admin
Администратор
Администратор



Joined: 01 Sep 2007
Posts: 1639

PostPosted: Sat Sep 22, 2007 6:14 pm    Post subject: ALV with Dynamic Table Reply with quote

Code:
REPORT zcreate_dynamic_alv.

DATA: t_ftab TYPE lvc_t_fcat,
wa_ftab TYPE lvc_s_fcat,
t_file TYPE REF TO data,
wa_file TYPE REF TO data.

FIELD-SYMBOLS: <table> TYPE STANDARD TABLE,
<data> TYPE ANY.
* Below, the fields for the dynamic table are appended to the fieldcatelog.
* Just in the way we build field catelog for ALV output

wa_ftab-tabname = 'MARC'.
wa_ftab-fieldname = 'MATNR'.
APPEND wa_ftab TO t_ftab.
wa_ftab-fieldname = 'WERKS'.
APPEND wa_ftab TO t_ftab.
wa_ftab-fieldname = 'PSTAT'.
APPEND wa_ftab TO t_ftab.
wa_ftab-fieldname = 'LVORM'.
APPEND wa_ftab TO t_ftab.
wa_ftab-fieldname = 'BWTTY'.
APPEND wa_ftab TO t_ftab.

*Call OOPs method to create the structure
CALL METHOD cl_alv_table_create=>create_dynamic_table
  EXPORTING
    it_fieldcatalog = t_ftab
  IMPORTING
    ep_table        = t_file.

ASSIGN t_file->* TO <table>. "This is the table
CREATE DATA wa_file LIKE LINE OF <table>. "Create a structure for the table
ASSIGN wa_file->* TO <data>. "Assigning into Field Symbols

*Selection into table
SELECT matnr werks pstat lvorm bwtty UP TO 10 ROWS FROM marc INTO TABLE <table>.
*In general case, you will be creating the dynamic table when you are not knowing
* how many fields you will need to select or consider in the program.
*Which means you will be using a dynamic select. Since this is an example program,
* I've hard coded the FIELDS in the select statement

*Just in case you need to loop at the table, do the following
LOOP AT <table> ASSIGNING <data>.
ENDLOOP.

*Call function module for ALV output
CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY_LVC'
  EXPORTING
    it_fieldcat_lvc = t_ftab
  TABLES
    t_outtab        = <table>
  EXCEPTIONS
    program_error   = 1
    OTHERS          = 2.
IF sy-subrc <> 0.
  MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
  WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
ENDIF.
Back to top
View user's profile Send private message
admin
Администратор
Администратор



Joined: 01 Sep 2007
Posts: 1639

PostPosted: Mon Mar 02, 2009 2:11 pm    Post subject: Reply with quote

Code:
*&---------------------------------------------------------------------*
*& Report ZDAVID_DYNAMIC_TABLE
*&
*&---------------------------------------------------------------------*
*&
*&
*&---------------------------------------------------------------------*
REPORT zdavid_dynamic_table.

TABLES: vbak, mara, vbap.

TYPE-POOLS: slis.

**********
FIELD-SYMBOLS: <dyn_table> TYPE STANDARD TABLE,
<dyn_wa>,
<dyn_field>,
<dyn_table1> TYPE STANDARD TABLE,
<dyn_wa1>,
<dyn_field1>.


DATA: dy_table TYPE REF TO data,
dy_line TYPE REF TO data,
ifc TYPE lvc_t_fcat,
xfc TYPE lvc_s_fcat,
dy_table1 TYPE REF TO data,
dy_line1 TYPE REF TO data.

DATA: BEGIN OF list_protype OCCURS 0,
extwg LIKE mara-extwg,
END OF list_protype.

DATA: show_protype LIKE list_protype OCCURS 0 WITH HEADER LINE.

DATA: l_title TYPE string.

CONSTANTS: tab_key TYPE c VALUE cl_abap_char_utilities=>horizontal_tab.
**********************
DATA: it_fieldcat TYPE slis_t_fieldcat_alv,
wa_fieldcat LIKE LINE OF it_fieldcat,
wa_layout TYPE slis_layout_alv,
it_extab TYPE slis_t_extab.
DATA: name(20).

DATA: BEGIN OF it_item OCCURS 0,
vkorg LIKE vbak-vkorg, "Sales organization
vtweg LIKE vbak-vtweg, "distribution channel
kunnr LIKE vbak-kunnr, "sold-to party
name1 LIKE kna1-name1, "Customer name
bstnk LIKE vbak-bstnk, "PO number
vbeln LIKE vbak-vbeln, "sales document
erdat LIKE vbak-erdat, "document date
extwg LIKE mara-extwg, "product type
posnr LIKE vbap-posnr, "order item
matnr LIKE vbap-matnr, "Material
kwmeng LIKE vbap-kwmeng, "Order Qty
vrkme LIKE vbap-vrkme, "Sales Unit
netwr LIKE vbap-netwr, "Order Net Value
waerk LIKE vbap-waerk, "Currency
za_kbetr LIKE konv-kbetr, "ZA01 %Discount
ra_kbetr LIKE konv-kbetr, "RA00 %Discount
za_kwert LIKE konv-kwert, "ZA01 discount amount
ra_kwert LIKE konv-kwert, "ZA01 discount amount
knumv LIKE vbak-knumv, "condition number
vbtyp LIKE vbak-vbtyp, "SO type
END OF it_item.

DATA: BEGIN OF it_output OCCURS 0,
vkorg(4) TYPE c, "Sales organization
vtweg(2), "distribution channel
kunnr(10), "sold-to party
name1(35), "Customer name
bstnk(20), "PO number
vbeln(10), "sales document
erdat(8), "document date
extwg(18), "product type
posnr(6), "order item
matnr(18), "Material
kwmeng(15), "Order Qty
vrkme(3), "Sales Unit
netwr(15), "Order Net Value
waerk(13), "Currency
za_kbetr(11), "ZA01 %Discount
ra_kbetr(11), "RA00 %Discount
za_kwert(13), "ZA01 discount amount
ra_kwert(13), "ZA01 discount amount
END OF it_output.

DATA: BEGIN OF it_op_sum OCCURS 0,
vkorg(4) TYPE c, "Sales organization
vtweg(2), "distribution channel
kunnr(10), "sold-to party
name1(35), "Customer name
bstnk(20), "PO number
vbeln(10), "sales document
erdat(8), "document date
extwg(18), "product type
kwmeng(15), "Order Qty
vrkme(3), "Sales Unit
netwr(15), "Order Net Value
waerk(13), "Currency
za_kwert(13), "ZA01 discount amount
ra_kwert(13), "ZA01 discount amount
END OF it_op_sum.


TYPES: BEGIN OF ty_sum,
vkorg LIKE vbak-vkorg, "Sales organization
vtweg LIKE vbak-vtweg, "distribution channel
kunnr LIKE vbak-kunnr, "sold-to party
name1 LIKE kna1-name1, "Customer name
bstnk LIKE vbak-bstnk, "PO number
* vbeln like vbak-vbeln, "sales document
erdat LIKE vbak-erdat, "document date
extwg LIKE mara-extwg, "product type
kwmeng LIKE vbap-kwmeng, "Order Qty
vrkme LIKE vbap-vrkme, "Sales Unit
netwr LIKE vbap-netwr, "Order Net Value
waerk LIKE vbap-waerk, "Currency
za_kwert LIKE konv-kwert, "ZA01 discount amount
ra_kwert LIKE konv-kwert, "ZA01 discount amount
END OF ty_sum.

DATA: BEGIN OF sel OCCURS 0,
knumv LIKE vbrk-knumv,
posnn LIKE vbfa-posnn,
vbtyp_n LIKE vbfa-vbtyp_n,
END OF sel.

DATA: wa_sum TYPE ty_sum.

DATA: BEGIN OF it_protype OCCURS 0,
extwg LIKE mara-extwg,
netwr LIKE vbap-netwr,
END OF it_protype.

DATA: it_sum TYPE STANDARD TABLE
OF ty_sum INITIAL SIZE 10 WITH HEADER LINE.

SELECTION-SCREEN BEGIN OF BLOCK fname1 WITH FRAME TITLE text-001.
SELECT-OPTIONS: s_vkorg FOR vbak-vkorg DEFAULT '4800' TO '4801',
s_vtweg FOR vbak-vtweg DEFAULT '31',
s_kunnr FOR vbak-kunnr,
s_erdat FOR vbak-erdat DEFAULT sy-datum,
s_extwg FOR mara-extwg.
SELECTION-SCREEN END OF BLOCK fname1.

SELECTION-SCREEN BEGIN OF BLOCK fname3 WITH FRAME TITLE text-003.
PARAMETER: rbank RADIOBUTTON GROUP rd1,
rintake RADIOBUTTON GROUP rd1 DEFAULT 'X',
rinvoice RADIOBUTTON GROUP rd1.
SELECTION-SCREEN END OF BLOCK fname3.

SELECTION-SCREEN BEGIN OF BLOCK fname4 WITH FRAME TITLE text-004.
PARAMETER: rabap RADIOBUTTON GROUP rd2 DEFAULT 'X',
rfile RADIOBUTTON GROUP rd2,
v_file LIKE rlgrap-filename
DEFAULT 'c:tempacdelco-statis.txt',
rserver RADIOBUTTON GROUP rd2,
v_server TYPE filepath-pathintern
DEFAULT '/icsdata/acdelco/acdelco-statis.txt'.
SELECTION-SCREEN END OF BLOCK fname4.

START-OF-SELECTION.

  PERFORM selectdata.
  PERFORM output.

*&---------------------------------------------------------------------*
*& Form selectdata
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
FORM selectdata .
  IF rbank = 'X'.
    PERFORM selectrbank.
  ELSEIF rintake = 'X'.
    PERFORM selectintake.
  ELSEIF rinvoice = 'X'.
    PERFORM selectrinvoice.
  ENDIF.
ENDFORM. " selectdata
*&---------------------------------------------------------------------*
*& Form output
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
FORM output .
  IF rabap = 'X'.
    PERFORM outputabap.
  ELSEIF rfile = 'X'.
    PERFORM outputfile.
  ELSEIF rserver = 'X'.
    PERFORM outputfile.
  ENDIF.
ENDFORM. " output
*&---------------------------------------------------------------------*
*& Form selectintake
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
FORM selectintake .

* select basic order data
  PERFORM selectbasic.


  LOOP AT it_item.

* get customer name
    SELECT SINGLE kna1~name1 FROM kna1
    INTO it_item-name1
    WHERE kna1~kunnr = it_item-kunnr.


* If SO is 'H'(return)
    IF it_item-vbtyp = 'H'.
      it_item-netwr = - it_item-netwr.
    ENDIF.

    MODIFY it_item.

    MOVE-CORRESPONDING it_item TO wa_sum.
    COLLECT wa_sum INTO it_sum.

  ENDLOOP.

  PERFORM dynamictable.

ENDFORM. " selectintake
*&---------------------------------------------------------------------*
*& Form selectrbank
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
FORM selectrbank .
  PERFORM selectbasic.
  LOOP AT it_item.

* get customer name
    SELECT SINGLE kna1~name1 FROM kna1
    INTO it_item-name1
    WHERE kna1~kunnr = it_item-kunnr.

* clear: it_item-kwmeng, it_item-netwr.
    DATA: t_kwmeng LIKE it_item-kwmeng,
    t_netwr LIKE it_item-netwr.

* When SD document category (VBAK~VBTYP) is ??(Order)
    IF it_item-vbtyp = 'C'.
* If VBFA-VBTYP_N = M (invoice) ++
      SELECT SUM( vbfa~rfmng ) SUM( vbrp~netwr )
      FROM vbfa JOIN vbrp ON vbfa~vbeln = vbrp~vbeln AND vbfa~posnn =
      vbrp~posnr
      INTO (t_kwmeng, t_netwr)
      WHERE vbfa~vbelv = it_item-vbeln
      AND vbfa~posnv = it_item-posnr
      AND vbfa~vbtyp_n = 'M'.
* If VBFA-VBTYP_N = N (invoice cancellation) --

      it_item-kwmeng = it_item-kwmeng - t_kwmeng.
      it_item-netwr = it_item-netwr - t_netwr.

      CLEAR: t_kwmeng, t_netwr.

      SELECT SUM( vbfa~rfmng ) SUM( vbrp~netwr )
      FROM vbfa JOIN vbrp ON vbfa~vbeln = vbrp~vbeln AND vbfa~posnn =
      vbrp~posnr
      INTO (t_kwmeng, t_netwr)
      WHERE vbfa~vbelv = it_item-vbeln
      AND vbfa~posnv = it_item-posnr
      AND vbfa~vbtyp_n = 'N'.

      it_item-kwmeng = it_item-kwmeng + t_kwmeng.
      it_item-netwr = it_item-netwr + t_netwr.

      IF it_item-kwmeng = 0.
        DELETE it_item.
        CONTINUE.
      ENDIF.


* When SD document category (VBAK~VBTYP) is H (Returns)
    ELSEIF it_item-vbtyp = 'H'.
* If VBFA-VBTYP_N = O (Credit memo) ++
      SELECT SUM( vbfa~rfmng ) SUM( vbrp~netwr )
      FROM vbfa JOIN vbrp ON vbfa~vbeln = vbrp~vbeln AND vbfa~posnn =
      vbrp~posnr
      INTO (t_kwmeng, t_netwr)
      WHERE vbfa~vbelv = it_item-vbeln
      AND vbfa~posnv = it_item-posnr
      AND vbfa~vbtyp_n = 'O'.

      it_item-kwmeng = it_item-kwmeng - t_kwmeng.
      it_item-netwr = it_item-netwr - t_netwr.


* If VBFA-VBTYP_N = S (Credit memo cancellation)
      CLEAR: t_kwmeng, t_netwr.
      SELECT SUM( vbfa~rfmng ) SUM( vbrp~netwr )
      FROM vbfa JOIN vbrp ON vbfa~vbeln = vbrp~vbeln AND vbfa~posnn =
      vbrp~posnr
      INTO (t_kwmeng, t_netwr)
      WHERE vbfa~vbelv = it_item-vbeln
      AND vbfa~posnv = it_item-posnr
      AND vbfa~vbtyp_n = 'S'.

      it_item-kwmeng = it_item-kwmeng + t_kwmeng.
      it_item-netwr = it_item-netwr + t_netwr.

      IF it_item-kwmeng = 0.
        DELETE it_item.
        CONTINUE.
      ENDIF.

    ENDIF.

* If SO is 'H'(return)
    IF it_item-vbtyp = 'H'.
      it_item-netwr = - it_item-netwr.
    ENDIF.


    MODIFY it_item.

    MOVE-CORRESPONDING it_item TO wa_sum.
    COLLECT wa_sum INTO it_sum.


  ENDLOOP.

  PERFORM dynamictable.

ENDFORM. " selectrbank
*&---------------------------------------------------------------------*
*& Form selectrinvoice
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
FORM selectrinvoice .
  PERFORM selectbasic.

  LOOP AT it_item.

* get customer name
    SELECT SINGLE kna1~name1 FROM kna1
    INTO it_item-name1
    WHERE kna1~kunnr = it_item-kunnr.

    CLEAR: it_item-kwmeng, it_item-netwr.
    DATA: t_kwmeng LIKE it_item-kwmeng,
    t_netwr LIKE it_item-netwr.

* When SD document category (VBAK~VBTYP) is ??(Order)
    IF it_item-vbtyp = 'C'.
* If VBFA-VBTYP_N = M (invoice) ++
      SELECT SUM( vbfa~rfmng ) SUM( vbrp~netwr )
      FROM vbfa JOIN vbrp ON vbfa~vbeln = vbrp~vbeln AND vbfa~posnn =
      vbrp~posnr
      INTO (it_item-kwmeng, it_item-netwr)
      WHERE vbfa~vbelv = it_item-vbeln
      AND vbfa~posnv = it_item-posnr
      AND vbfa~vbtyp_n = 'M'.
* If VBFA-VBTYP_N = N (invoice cancellation) --
      CLEAR: t_kwmeng, t_netwr.

      SELECT SUM( vbfa~rfmng ) SUM( vbrp~netwr )
      FROM vbfa JOIN vbrp ON vbfa~vbeln = vbrp~vbeln AND vbfa~posnn =
      vbrp~posnr
      INTO (t_kwmeng, t_netwr)
      WHERE vbfa~vbelv = it_item-vbeln
      AND vbfa~posnv = it_item-posnr
      AND vbfa~vbtyp_n = 'N'.

* M (invoice) - N (invoice cancellation)
      it_item-kwmeng = it_item-kwmeng - t_kwmeng.
      it_item-netwr = it_item-netwr - t_netwr.

      IF it_item-kwmeng = 0.
        DELETE it_item.
        CONTINUE.
      ENDIF.


* When SD document category (VBAK~VBTYP) is H (Returns)
    ELSEIF it_item-vbtyp = 'H'.
* If VBFA-VBTYP_N = O (Credit memo) ++
      SELECT SUM( vbfa~rfmng ) SUM( vbrp~netwr )
      FROM vbfa JOIN vbrp ON vbfa~vbeln = vbrp~vbeln AND vbfa~posnn =
      vbrp~posnr
      INTO (it_item-kwmeng, it_item-netwr)
      WHERE vbfa~vbelv = it_item-vbeln
      AND vbfa~posnv = it_item-posnr
      AND vbfa~vbtyp_n = 'O'.

* If VBFA-VBTYP_N = S (Credit memo cancellation)
      CLEAR: t_kwmeng, t_netwr.
      SELECT SUM( vbfa~rfmng ) SUM( vbrp~netwr )
      FROM vbfa JOIN vbrp ON vbfa~vbeln = vbrp~vbeln AND vbfa~posnn =
      vbrp~posnr
      INTO (t_kwmeng, t_netwr)
      WHERE vbfa~vbelv = it_item-vbeln
      AND vbfa~posnv = it_item-posnr
      AND vbfa~vbtyp_n = 'S'.

* O (Credit memo) - S (Credit memo cancellation)
      it_item-kwmeng = it_item-kwmeng - t_kwmeng.
      it_item-netwr = it_item-netwr - t_netwr.

      IF it_item-kwmeng = 0.
        DELETE it_item.
        CONTINUE.
      ENDIF.


    ENDIF.

* If SO is 'H'(return)
    IF it_item-vbtyp = 'H'.
      it_item-netwr = - it_item-netwr.
    ENDIF.


    MODIFY it_item.

    MOVE-CORRESPONDING it_item TO wa_sum.
    COLLECT wa_sum INTO it_sum.
  ENDLOOP.

  PERFORM dynamictable.

ENDFORM. " selectrinvoice
*&---------------------------------------------------------------------*
*& Form selectbasic
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
FORM selectbasic .

* select basic order data
  SELECT vbak~kunnr vbap~kwmeng vbak~vbeln
  vbap~posnr vbap~matnr mara~extwg vbap~netwr vbap~waerk vbak~vbtyp
FROM vbak JOIN vbap ON vbap~vbeln = vbak~vbeln JOIN mara ON vbap~matnr =
  mara~matnr
  INTO CORRESPONDING FIELDS OF TABLE it_item
  WHERE vbak~vkorg NE '4802'
  AND vbak~vkorg NE '4803'
  AND ( vbak~vbtyp EQ 'C' OR vbak~vbtyp EQ 'H')
  AND vbap~abgru = ''
  AND vbak~vkorg IN s_vkorg
  AND vbak~vtweg IN s_vtweg
  AND vbak~kunnr IN s_kunnr
  AND vbak~erdat IN s_erdat
  AND mara~extwg IN s_extwg
  AND vbap~abgru EQ ''
  AND mara~extwg NE ''.


ENDFORM. " selectbasic
*&---------------------------------------------------------------------*
*& Form outputabap
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
FORM outputabap .
  DEFINE a_fieldcat.
    clear wa_fieldcat.
    wa_fieldcat-fieldname = &1.
    wa_fieldcat-seltext_l = &2.
* wa_fieldcat-just = 'C'.
* wa_fieldcat-outputlen = &3.
    wa_fieldcat-key = &3.
    append wa_fieldcat to it_fieldcat.
  END-OF-DEFINITION.

  a_fieldcat 'KUNNR' 'Sold-to Party' 'X'.
  a_fieldcat 'NAME1' 'Name' ''.
  a_fieldcat 'PERIOD' 'Period Required' ''.

* a_fieldcat 'ERDAT' 'Period Required' ''.
* a_fieldcat 'EXTWG' 'Product Type' ''.

  DATA: name1(20),
  count TYPE i.

  LOOP AT list_protype .
    count = count + 1.
    CONCATENATE list_protype-extwg 'RMB' INTO name.

    READ TABLE show_protype INDEX count.
    CONCATENATE show_protype-extwg '(RMB)' INTO name1.
    a_fieldcat name name1 ''.

    CONCATENATE list_protype-extwg 'USD' INTO name.
    CONCATENATE show_protype-extwg '(USD)' INTO name1.
    a_fieldcat name name1 ''.

    CONCATENATE list_protype-extwg 'EURO' INTO name.
    CONCATENATE show_protype-extwg '(EURO)' INTO name1.
    a_fieldcat name name1 ''.
  ENDLOOP.

  a_fieldcat 'TOTALRMB' 'Total(RMB)' ''.
  a_fieldcat 'TOTALUSD' 'Total(USD)' ''.
  a_fieldcat 'TOTALEURO' 'Total(EURO)' ''.

* if rbank = 'X'.
* a_fieldcat 'NETWR' 'Bank Net Value' ''.
* elseif rintake = 'X'.
* a_fieldcat 'NETWR' 'Order Net Value' ''.
* elseif rinvoice = 'X'.
* a_fieldcat 'NETWR' 'Invoiced Net Value' ''.
* endif.
*
* a_fieldcat 'WAERK' 'Currency' ''.

  CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
  EXPORTING
  is_layout = wa_layout
  it_fieldcat = it_fieldcat
* IMPORTING
* E_EXIT_CAUSED_BY_CALLER =
* ES_EXIT_CAUSED_BY_USER =
  TABLES
  t_outtab = <dyn_table>.
* EXCEPTIONS
* PROGRAM_ERROR = 1
* OTHERS = 2
  .
  IF sy-subrc <> 0.
* MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
* WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
  ENDIF.

ENDFORM. " outputabap
*&---------------------------------------------------------------------*
*& Form outputfile
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
FORM outputfile .

*loop at show_protype.
* concaterate
*endloop.


******* print head **********
  CLEAR xfc.
  REFRESH ifc.

  xfc-fieldname = 'kunnr'.
  xfc-datatype = 'CHAR'.
  xfc-intlen = '13'.
  APPEND xfc TO ifc.
  xfc-fieldname = 'name1'.
  xfc-datatype = 'CHAR'.
  xfc-intlen = '35'.
  APPEND xfc TO ifc.
  xfc-fieldname = 'period'.
  xfc-datatype = 'CHAR'.
  xfc-intlen = '25'.
  APPEND xfc TO ifc.


  LOOP AT list_protype.


    REPLACE '/' WITH '' INTO list_protype-extwg.
    REPLACE '-' WITH '' INTO list_protype-extwg.
    CONDENSE list_protype-extwg NO-GAPS.

    CONCATENATE list_protype-extwg 'RMB' INTO name.
    xfc-fieldname = name.
    xfc-datatype = 'CHAR'.
    xfc-intlen = 18.
* xfc-decimals = 2.
    APPEND xfc TO ifc.

    CONCATENATE list_protype-extwg 'USD' INTO name.

    xfc-fieldname = name.
    xfc-datatype = 'CHAR'.
    xfc-intlen = 18.
* xfc-decimals = 2.
    APPEND xfc TO ifc.

    CONCATENATE list_protype-extwg 'EURO' INTO name.
    xfc-fieldname = name.
    xfc-datatype = 'CHAR'.
    xfc-intlen = 18.
* xfc-decimals = 2.
    APPEND xfc TO ifc.

    MODIFY list_protype.

  ENDLOOP.

** total ****

  xfc-fieldname = 'TotalRMB'.
  xfc-datatype = 'CHAR'.
  xfc-intlen = 18.
* xfc-decimals = 2.
  APPEND xfc TO ifc.

  xfc-fieldname = 'TotalUSD'.
  xfc-datatype = 'CHAR'.
  xfc-intlen = 18.
* xfc-decimals = 2.
  APPEND xfc TO ifc.

  xfc-fieldname = 'TotalEURO'.
  xfc-datatype = 'CHAR'.
  xfc-intlen = 18.
* xfc-decimals = 2.
  APPEND xfc TO ifc.

** total ****

  CALL METHOD cl_alv_table_create=>create_dynamic_table
  EXPORTING
  it_fieldcatalog = ifc
  IMPORTING
  ep_table = dy_table1.

  ASSIGN dy_table1->* TO <dyn_table1>.

  CREATE DATA dy_line LIKE LINE OF <dyn_table1>.

  ASSIGN dy_line->* TO <dyn_wa1>.


  DATA: count TYPE i.
  count = 1.

  ASSIGN COMPONENT count OF STRUCTURE <dyn_wa1> TO <dyn_field1>.
  <dyn_field1> = 'Sold-to Party'.
  count = count + 1.

  ASSIGN COMPONENT count OF STRUCTURE <dyn_wa1> TO <dyn_field1>.
  <dyn_field1> = 'Name'.
  count = count + 1.

  ASSIGN COMPONENT count OF STRUCTURE <dyn_wa1> TO <dyn_field1>.
  <dyn_field1> = 'Period Required'.
  count = count + 1.


  LOOP AT show_protype .
    ASSIGN COMPONENT count OF STRUCTURE <dyn_wa1> TO <dyn_field1>.
    CONCATENATE show_protype-extwg '(RMB)' INTO <dyn_field1>.
    count = count + 1.
    ASSIGN COMPONENT count OF STRUCTURE <dyn_wa1> TO <dyn_field1>.
    CONCATENATE show_protype-extwg '(USD)' INTO <dyn_field1>.
    count = count + 1.
    ASSIGN COMPONENT count OF STRUCTURE <dyn_wa1> TO <dyn_field1>.
    CONCATENATE show_protype-extwg '(EURO)' INTO <dyn_field1>.
    count = count + 1.

  ENDLOOP.

  ASSIGN COMPONENT count OF STRUCTURE <dyn_wa1> TO <dyn_field1>.
  <dyn_field1> = 'Total(RMB)'.
  count = count + 1.
  ASSIGN COMPONENT count OF STRUCTURE <dyn_wa1> TO <dyn_field1>.
  <dyn_field1> = 'Total(USD)'.
  count = count + 1.
  ASSIGN COMPONENT count OF STRUCTURE <dyn_wa1> TO <dyn_field1>.
  <dyn_field1> = 'Total(EURO)'.
  count = count + 1.


  APPEND <dyn_wa1> TO <dyn_table1>.

  LOOP AT <dyn_table> INTO <dyn_wa>.
    MOVE-CORRESPONDING <dyn_wa> TO <dyn_wa1>.
    APPEND <dyn_wa1> TO <dyn_table1>.
  ENDLOOP.


* a_fieldcat 'TOTALRMB' 'Total(RMB)' ''.
* a_fieldcat 'TOTALUSD' 'Total(USD)' ''.
* a_fieldcat 'TOTALEURO' 'Total(EURO)' ''.

******* print head **********

  IF rserver = ''.
    CALL FUNCTION 'Z_UT_DOWNLOAD'
    EXPORTING
    i_path_file = v_file
    i_local_pc = 'X'
    i_fileformat = 'TAB'
* I_DELIMITER =
* IT_FIELD_CATALOG =
    TABLES
    it_input = <dyn_table1>
* EXCEPTIONS
* FILE_OPEN_ERROR = 1
* FILE_WRITE_ERROR = 2
* INVALID_FILESIZE = 3
* INVALID_TYPE = 4
* NO_BATCH = 5
* UNKNOWN_ERROR = 6
* INVALID_TABLE_WIDTH = 7
* GUI_REFUSE_FILETRANSFER = 8
* CUSTOMER_ERROR = 9
* DATASET_NO_AUTHORITY = 10
* DATASET_CANT_OPEN = 11
* DATASET_CANT_CLOSE = 12
* DATASET_NO_PIPE = 13
* DATASET_READ_ERROR = 14
* DATASET_TOO_MANY_FILES = 15
* DATASET_WRITE_ERROR = 16
* EXPORT_DATASET_CANNOT_OPEN = 17
* EXPORT_DATASET_WRITE_ERROR = 18
* OPEN_DATASET_NO_AUTHORITY = 19
* OPEN_PIPE_NO_AUTHORITY = 20
* USER_DEFINED_DELIMITER_MISSING = 21
* DELIMITER_NOT_IDENTIFIED = 22
* INCONSISTENT_FIELD_CATALOG = 23
* OTHERS = 24
    .
    IF sy-subrc <> 0.
* MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
* WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
    ENDIF.
    IF sy-subrc = 0.
      MESSAGE s999(fr) WITH 'Downloaded succesfully'.
    ENDIF.

  ELSE.
    CALL FUNCTION 'Z_UT_DOWNLOAD'
    EXPORTING
    i_path_file = v_server
* I_LOCAL_PC = 'X'
    i_fileformat = 'TAB'
* I_DELIMITER =
* IT_FIELD_CATALOG =
    TABLES
    it_input = <dyn_table1>
* EXCEPTIONS
* FILE_OPEN_ERROR = 1
* FILE_WRITE_ERROR = 2
* INVALID_FILESIZE = 3
* INVALID_TYPE = 4
* NO_BATCH = 5
* UNKNOWN_ERROR = 6
* INVALID_TABLE_WIDTH = 7
* GUI_REFUSE_FILETRANSFER = 8
* CUSTOMER_ERROR = 9
* DATASET_NO_AUTHORITY = 10
* DATASET_CANT_OPEN = 11
* DATASET_CANT_CLOSE = 12
* DATASET_NO_PIPE = 13
* DATASET_READ_ERROR = 14
* DATASET_TOO_MANY_FILES = 15
* DATASET_WRITE_ERROR = 16
* EXPORT_DATASET_CANNOT_OPEN = 17
* EXPORT_DATASET_WRITE_ERROR = 18
* OPEN_DATASET_NO_AUTHORITY = 19
* OPEN_PIPE_NO_AUTHORITY = 20
* USER_DEFINED_DELIMITER_MISSING = 21
* DELIMITER_NOT_IDENTIFIED = 22
* INCONSISTENT_FIELD_CATALOG = 23
* OTHERS = 24
    .
    IF sy-subrc <> 0.
* MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
* WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
    ENDIF.

    IF sy-subrc = 0.
      MESSAGE s999(fr) WITH 'Downloaded succesfully'.
    ENDIF.

  ENDIF.


ENDFORM. " outputfile

*&--------------------------------------------------------------------*
*& Form dynamictable
*&--------------------------------------------------------------------*
* text
*---------------------------------------------------------------------*
FORM dynamictable.
  LOOP AT it_sum.
    MOVE-CORRESPONDING it_sum TO it_protype.
    APPEND it_protype.
    MOVE-CORRESPONDING it_sum TO list_protype.
    APPEND list_protype.
  ENDLOOP.

  DATA period(25) TYPE c.

  READ TABLE s_erdat INDEX 1.
  CONCATENATE s_erdat-low '~' s_erdat-high INTO period.


  SORT list_protype BY extwg.
  DELETE ADJACENT DUPLICATES FROM list_protype.

*************** sold to party, name **********
  xfc-fieldname = 'kunnr'.
  xfc-datatype = 'CHAR'.
  xfc-intlen = '10'.
  APPEND xfc TO ifc.
  xfc-fieldname = 'name1'.
  xfc-datatype = 'CHAR'.
  xfc-intlen = '35'.
  APPEND xfc TO ifc.
  xfc-fieldname = 'period'.
  xfc-datatype = 'CHAR'.
  xfc-intlen = '25'.
  APPEND xfc TO ifc.


  LOOP AT list_protype.
    MOVE list_protype TO show_protype.
    APPEND show_protype.

    REPLACE '/' WITH '' INTO list_protype-extwg.
    REPLACE '-' WITH '' INTO list_protype-extwg.
    CONDENSE list_protype-extwg NO-GAPS.

    CONCATENATE list_protype-extwg 'RMB' INTO name.
    xfc-fieldname = name.
    xfc-datatype = 'CURR'.
    xfc-intlen = 15.
    xfc-decimals = 2.
    APPEND xfc TO ifc.

    CONCATENATE list_protype-extwg 'USD' INTO name.

    xfc-fieldname = name.
    xfc-datatype = 'CURR'.
    xfc-intlen = 15.
    xfc-decimals = 2.
    APPEND xfc TO ifc.

    CONCATENATE list_protype-extwg 'EURO' INTO name.
    xfc-fieldname = name.
    xfc-datatype = 'CURR'.
    xfc-intlen = 15.
    xfc-decimals = 2.
    APPEND xfc TO ifc.

    MODIFY list_protype.

  ENDLOOP.

** total ****

  xfc-fieldname = 'TotalRMB'.
  xfc-datatype = 'CURR'.
  xfc-intlen = 15.
  xfc-decimals = 2.
  APPEND xfc TO ifc.

  xfc-fieldname = 'TotalUSD'.
  xfc-datatype = 'CURR'.
  xfc-intlen = 15.
  xfc-decimals = 2.
  APPEND xfc TO ifc.

  xfc-fieldname = 'TotalEURO'.
  xfc-datatype = 'CURR'.
  xfc-intlen = 15.
  xfc-decimals = 2.
  APPEND xfc TO ifc.

** total ****

  CALL METHOD cl_alv_table_create=>create_dynamic_table
  EXPORTING
  it_fieldcatalog = ifc
  IMPORTING
  ep_table = dy_table.

  ASSIGN dy_table->* TO <dyn_table>.

  CREATE DATA dy_line LIKE LINE OF <dyn_table>.

  ASSIGN dy_line->* TO <dyn_wa>.

  SORT it_sum BY kunnr.


************ period time***********
  DATA: sumrmb LIKE it_sum-netwr,
  sumusd LIKE it_sum-netwr,
  sumeuro LIKE it_sum-netwr,
  isfirst.

  LOOP AT it_sum.

    AT NEW kunnr.
      isfirst = ''.
      AT FIRST.
        isfirst = 'X'.
      ENDAT.

      IF isfirst <> 'X'.
        APPEND <dyn_wa> TO <dyn_table>.
        CLEAR: <dyn_wa>, sumrmb, sumusd, sumeuro.
      ENDIF.
    ENDAT.

    ASSIGN COMPONENT 1 OF STRUCTURE <dyn_wa> TO <dyn_field>.
    <dyn_field> = it_sum-kunnr.
    ASSIGN COMPONENT 2 OF STRUCTURE <dyn_wa> TO <dyn_field>.
    <dyn_field> = it_sum-name1.

    ASSIGN COMPONENT 3 OF STRUCTURE <dyn_wa> TO <dyn_field>.
    <dyn_field> = period.

    REPLACE '/' WITH '' INTO it_sum-extwg.
    REPLACE '-' WITH '' INTO it_sum-extwg.
    CONDENSE it_sum-extwg NO-GAPS.

    CONCATENATE it_sum-extwg it_sum-waerk INTO name.
    ASSIGN COMPONENT name OF STRUCTURE <dyn_wa> TO <dyn_field>.
    <dyn_field> = it_sum-netwr.

    IF it_sum-waerk = 'RMB'.
      sumrmb = sumrmb + it_sum-netwr.
    ELSEIF it_sum-waerk = 'USD'.
      sumusd = sumusd + it_sum-netwr.
    ELSEIF it_sum-waerk = 'EURO'.
      sumeuro = sumeuro + it_sum-netwr.
    ENDIF.

    ASSIGN COMPONENT 'TOTALRMB' OF STRUCTURE <dyn_wa> TO <dyn_field>.
    <dyn_field> = sumrmb.
    ASSIGN COMPONENT 'TOTALUSD' OF STRUCTURE <dyn_wa> TO <dyn_field>.
    <dyn_field> = sumusd.
    ASSIGN COMPONENT 'TOTALEURO' OF STRUCTURE <dyn_wa> TO <dyn_field>.
    <dyn_field> = sumeuro.


    AT LAST.
      APPEND <dyn_wa> TO <dyn_table>.
      CLEAR: <dyn_wa>, sumrmb, sumusd, sumeuro.
    ENDAT.

  ENDLOOP.

ENDFORM. "dynamictable


*&---------------------------------------------------------------------*
*& Report ZDAVID_DYNAMIC_TABLE_2
*&
*&---------------------------------------------------------------------*
*&
*&
*&---------------------------------------------------------------------*
report zdavid_dynamic_table_2.

TYPE-POOLS : abap.
FIELD-SYMBOLS: <dyn_table> TYPE STANDARD TABLE,
<dyn_wa>,
<dyn_field>.

DATA: dy_table TYPE REF TO data,
dy_line TYPE REF TO data,
xfc TYPE lvc_s_fcat,
ifc TYPE lvc_t_fcat.

SELECTION-SCREEN BEGIN OF BLOCK b1 WITH FRAME.
PARAMETERS: p_table(30) TYPE c DEFAULT 'T001'.
SELECTION-SCREEN END OF BLOCK b1.

START-OF-SELECTION.

  PERFORM get_structure.

  PERFORM create_dynamic_itab.
**********Creates a dyanamic internal table**********
  PERFORM get_data.

  PERFORM write_out.

*&--------------------------------------------------------------------*
*& Form get_structure
*&--------------------------------------------------------------------*
* text
*---------------------------------------------------------------------*
FORM get_structure.

  DATA : idetails TYPE abap_compdescr_tab,
  xdetails TYPE abap_compdescr.
  DATA : ref_table_des TYPE REF TO cl_abap_structdescr.

* DATA : ref_table_des TYPE REF TO "cl_abap_structdescr
* CL_ABAP_TABLEDESCR.

* Get the structure of the table.
  ref_table_des ?=
  cl_abap_typedescr=>describe_by_name( p_table ).
  idetails[] = ref_table_des->components[].
  LOOP AT idetails INTO xdetails.
    CLEAR xfc.
    xfc-fieldname = xdetails-name .
    xfc-datatype = xdetails-type_kind.
    xfc-inttype = xdetails-type_kind.
    xfc-intlen = xdetails-length.
    xfc-decimals = xdetails-decimals.
    APPEND xfc TO ifc.
  ENDLOOP.

* TYPES: BEGIN OF typ_01,
* clo1,
* clo2,
* END OF typ_01.
*
* DATA: it_01 TYPE TABLE OF typ_01.
* DATA: wa_01 TYPE typ_01.
* wa_01 = '11'.
* APPEND wa_01 TO it_01.
* ref_table_des ?=
* cl_abap_typedescr=>describe_by_data( it_01 ).


ENDFORM. "get_structure
*&--------------------------------------------------------------------*
*& Form create_dynamic_itab
*&--------------------------------------------------------------------*
* text
*---------------------------------------------------------------------*
FORM create_dynamic_itab.
* Create dynamic internal table and assign to FS
  CALL METHOD cl_alv_table_create=>create_dynamic_table
  EXPORTING
  it_fieldcatalog = ifc
  IMPORTING
  ep_table = dy_table.
  ASSIGN dy_table->* TO <dyn_table>.
* Create dynamic work area and assign to FS
  CREATE DATA dy_line LIKE LINE OF <dyn_table>.
  ASSIGN dy_line->* TO <dyn_wa>.
ENDFORM. "create_dynamic_itab

*&--------------------------------------------------------------------*
*& Form get_data
*&--------------------------------------------------------------------*
* text
*---------------------------------------------------------------------*
FORM get_data.
* Select Data from table.
  SELECT * INTO TABLE <dyn_table>
  FROM (p_table).
ENDFORM. "get_data
"get_data
*&--------------------------------------------------------------------*
*& Form write_out
*&--------------------------------------------------------------------*
* text
*---------------------------------------------------------------------*
FORM write_out.
* Write out data from table.
  LOOP AT <dyn_table> INTO <dyn_wa>.
    DO.
      ASSIGN COMPONENT sy-index
      OF STRUCTURE <dyn_wa> TO <dyn_field>.
      IF sy-subrc <> 0.
        EXIT.
      ENDIF.
      IF sy-index = 1.
        WRITE:/ <dyn_field>.
      ELSE.
        WRITE: <dyn_field>.
      ENDIF.
    ENDDO.
  ENDLOOP.
ENDFORM. "write_out
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    Russian ABAP Developer's Club Forum Index -> Dynamic Programming | Динамическое программирование All times are GMT + 4 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


All product names are trademarks of their respective companies. SAPNET.RU websites are in no way affiliated with SAP AG.
SAP, SAP R/3, R/3 software, mySAP, ABAP, BAPI, xApps, SAP NetWeaver and any other are registered trademarks of SAP AG.
Every effort is made to ensure content integrity. Use information on this site at your own risk.