﻿ Calculating and Printing Grand Totals

# Calculating and Printing Grand Totals

### Use

You want to calculate a grand total for a column of your table and print it. If you wish, you can print the grand total at the end of the table and subtotals at the end of each page.

If you do not want to calculate totals for different currencies or quantity units, use the Calculations tab of the table node (see: Calculations).

The solution described here refers to printing old tables. However, it can easily be adapted for the new table node type.

Form SF_TOTALS in the system illustrates how to print grand totals for different currencies using the old or the new table types. Use application program SF_TOTALS to call this form.

### Prerequisites

You are already printing the table using the table node, for which you now want to print a grand total.

### Procedure

To print a subtotal of the grand total before a page break, the subtotal must be calculated in a special way. However, you can also use this method if you only want to print the grand total at the end of the table.

We will use the flight data model to explain this summing method. A table containing the flights of the airline carriers is already being printed in a form. The corresponding internal table ( T_SFLIGHT ) is defined in the form interface and has type TABLE OF SFLIGHT . To print a table line, you have defined field GS_SFLIGHT of type SFLIGHT as a global field. You now want to calculate the total on the flight prices, taking into account that the airlines use different currencies to indicate their prices.

1. Create three additional fields in the global definition:
• GT_TOTAL of type TABLE OF SFLIGHT
• GS_TOTAL of type SFLIGHT
• GS_4NEXT_ADDITION of type SFLIGHT
1. In the navigation tree, you are already printing the table in the main window using table node B. To keep it simple, the table output in our example (see below) consists only of the nodes C (containing the price) and E (containing the currency). Enhance the form logic as follows:

Form logic for calculating the grand total

### Result

If the table output covers several pages, a subtotal of the grand total appears at the end of each page. If you want to print a total only at the end of the table, include the condition only at end of section into the loop node ( G).

Discussion

Some steps within the form logic are self-explanatory:

• To be able to calculate the price in accordance with the currency, we need the internal table GT_TOTAL . We can then use the ABAP statement COLLECT to add up the values, thereby easily assigning each new value to the correct currency. Each entry in table GT_TOTAL corresponds to the totals value in one currency.
• At the beginning of the table, all summing fields must be cleared (node A).
• When printing the footer, we use loop G to print the totals sorted by currencies.

Other steps, in contrast, are hard to understand at first sight, for example, what do we need field gs_4next_addition for?. Why don't we simply add the current value of gs_sflight to the corresponding value in the internal table GT_TOTAL ?

The reason for this lies in the output control for loops and tables. Determining the page break requires the output control to buffer table lines before printing them. The output control needs the height specifications of the entire line before it can determine when to trigger the automatic page break. The program lines nodes, in contrast, are not buffered but executed directly during processing.