The events in the smartforms are helpful in the requirements, where we need the following scenarios to be done:

  1. Page break based on the document(sales order number, delivery number, ship-to number)
  2. To print the subtotal of the smartform.
  3. Page number Reset.

For printing the sub total of the smartforms, the links are already available.


This blog contains the detailed description about how to set the page break, based on the document number and page number reset.

1.1 Steps to be followed in Page Break

The below steps are followed and code is applied for the page break:

  • Sort the table, in which our loopis present on the basis of the field where the page break is to be applied; in this case, it would be “Ship-to” (KUNNR).

          /wp-content/uploads/2015/02/cap1_652065.png

  • An automatic event would be created named KUNNR.

          Capture.PNG

  • Right click on the event and create a command. This command would be to print the new ship-to party and its corresponding data in the new page.

        /wp-content/uploads/2015/02/cap3_652082.png


The output of the form would be as below:

/wp-content/uploads/2015/02/cap4_652083.png


This would be the first page and the next page would have a different ship to number

/wp-content/uploads/2015/02/cap5_652084.png


2.    How to Reset Page Number

Generally, in smartforms, we would use the system fields for printing the page number. There could be a requirement, wherein the page numbers are to be reset according to the document number. Taking the above given example, in case the page break is in the occurrence of new ship-to number, the page number should reset, rather than the continuous increment of the page number.

Say, for instance, there are 2 ship-to numbers; the first ship-to number has 2 pages of data, so, the page number here should be printed as:

      • Page 1 of 2
      • Page 2 of 2

The second ship-to number has 1 page, so here the page number would be Page 1 of 1. To achieve the above requirement, events in the smartforms would be used. The flow here for page break logic would always go as below:

      • Initialization
      • Header
      • Sort Beginning
      • Sort End
      • Sort Beginning (would go here again for the next new document number)
      • Footer

Sort beginning and end areas are created automatically at the time when the sort is applied at the table. We just need to click on sort begin and sort end. Below is the screenshot for the same:

           /wp-content/uploads/2015/02/cap6_652080.png


Following is the code to be executed:

In the initialization, we need to count the total number of pages for each new ship-to, the same variable is saved in the final table (used as in loop).

DATA: lw_count TYPE i,
      lw_tabix TYPE i,
      lst_lips_kna1 TYPE gty_lips_kna1.

gw_page = 1.
LOOP AT gt_lips_kna1 INTO gst_lips_kna1.
  ADD 1 TO lw_tabix.
  AT NEW kunnr.

    lw_tabix = 1.
  ENDAT.
  IF gw_page IS INITIAL.
    lw_count = 31.   ”  It is set 31 here for the number of line items, the value would differ according to the line items one needs to print in a page

  ELSE.
   lw_count = ( gw_page * 30 ) + 1.”All the line items would be in the multiple of 30 + 1. For eg : 60 + 1, 90 + 1 etc.
  ENDIF.
  IF lw_tabix >= lw_count.
    ADD 1 TO gw_page.
  ENDIF.
  CLEAR gw_page_total.
  AT END OF kunnr.
   
gw_page_total = gw_page.
    gw_page = 1.
  ENDAT.
  IF gw_page_total IS NOT INITIAL.
    gst_lips_kna1-page_total = gw_page_total.
    MODIFY gt_lips_kna1 FROM gst_lips_kna1
    INDEX sy-tabix TRANSPORTING page_total.
    READ TABLE gt_lips_kna1 INTO lst_lips_kna1
    WITH KEY kunnr = gst_lips_kna1-kunnr.
    IF sy-subrc EQ 0.
      lst_lips_kna1-page_total = gw_page_total.
      MODIFY gt_lips_kna1 FROM lst_lips_kna1
      INDEX sy-tabix TRANSPORTING page_total.
    ENDIF.
  ENDIF.

ENDLOOP.
CLEAR gw_page.

lw_count is taken as the counter for the number of line items that would fit in one page.

gw_page_total is the variable to print the total number of pages in one ship-to.

gw_page will store the value for the current page to be printed.


The header portion will have the following code:

      IF gw_new_sort EQ ‘X’.
        CLEAR gw_new_sort.
      ELSE.
        ADD 1 TO gw_page.
      ENDIF.


This code sets in the flag, which is used later in the footer part.

Sort begin would have this code to set the current page of the ship-to:

CLEAR gw_count1.
IF gw_count <= 1.” This is done to avoid setting the flag as ‘X’ at the first run of the loop

ELSE.
  gw_new_sort = ‘X’. ” This flag is set to identify the beginning of a new page
ENDIF.

READ TABLE gt_lips_kna1 INTO lst_lips_kna1
      WITH KEY kunnr = gst_lips_kna1-kunnr.
IF sy-subrc EQ 0.
  IF sy-tabix = 1 .
  ELSE.
  lw_tabix = sy-tabix – 1. “ This marks begin of next document so to fetch the total pages for current ship-to number, subtracted 1 to get last record of current.
    READ TABLE gt_lips_kna1 INTO lst_lips_kna1 INDEX lw_tabix.
  ENDIF.
  gw_page_total = lst_lips_kna1-page_total.
ENDIF.

/wp-content/uploads/2015/02/cap8_652194.png


Sort End:

To fetch the total number of pages of the current document, as per the logic coded in initialization. 


/wp-content/uploads/2015/02/cap9_652195.png

READ TABLE gt_lips_kna1 INTO lst_lips_kna1
      WITH KEY kunnr = gst_lips_kna1-kunnr.
IF sy-subrc EQ 0.
  gw_page_total = lst_lips_kna1-page_total.
ENDIF.

The footer would be the area where the page number needs to be printed. After placing the text with the fields of the current page and page total, the code is to be placed in order to reset the page values.

/wp-content/uploads/2015/02/cap10_652199.png

IF gw_new_sort EQ ‘X’.
  gw_page = 1.
  READ TABLE gt_lips_kna1 INTO lst_lips_kna1_n
      WITH KEY kunnr = gst_lips_kna1-kunnr.
  IF sy-subrc EQ 0.
    gw_page_total = lst_lips_kna1_n-page_total.
  ENDIF.
ENDIF.

To report this post you need to login first.

Be the first to leave a comment

You must be Logged on to comment or reply to a post.

Leave a Reply