Module 3: Output Table Builder - build_output_table_new Form
← Back to Overview | ← Previous: Data Processing | Next: ALV Display →
Module Overview
Lines: 6566-7060
Importance: Tier 1 (Critical) - Data structuring and presentation engine
Purpose: Transforms aggregated forecast data into the final ALV display structure with time-bucketed columns
Business Context - The "Report Assembly Line"
Think of this module as a sophisticated report assembly line that:
- Takes processed demand data (ingredients) from
t_matfcast - Organizes it into a grid format with time periods as columns
- Handles duplicate materials and follow-up material logic
- Adds safety stock, pricing, and vendor information
- Produces the final
t_outputtable ready for ALV display
High-Level Processing Flow
Detailed Section Analysis
Section 1: Data Consolidation and Cleanup (Lines 6566-6690)
SORT t_matfcast BY lifnr werks matnr dat01 mng01 extra.
DATA: lt_matfcast TYPE type_t_matfcast,
lw_matfcast2 TYPE type_matfcast,
lv_qty1 TYPE mng01,
lv_qty2 TYPE mng01,
...
lv_qty19 TYPE mng01
What happens:
- Lines 6596-6600: Sorts forecast data for efficient processing
- Lines 6625-6686: Consolidates multiple forecast entries for the same date
- Lines 6631-6634: Optionally excludes safety stock based on user selection
Key Business Logic:
Section 2: Follow-up Material Duplicate Handling (Lines 6709-6799)
This is one of the most complex business logic sections:
LOOP AT t_output ASSIGNING <wa_output>.
READ TABLE t_output INTO lw_output WITH KEY
lifnr = <wa_output>-lifnr
werks = <wa_output>-werks
matnr = <wa_output>-matnr
delete_flag = ' '.
IF sy-subrc = 0.
" Handle duplicate follow-up materials
Business Problem: When Material A has follow-up Material B, and Material B has follow-up Material C, we can get:
- Entry 1: Material A (followup = B)
- Entry 2: Material B (followup = C)
- Entry 3: Material B (as standalone)
Solution Logic:
Section 3: Material Valuation and Pricing (Lines 6800-6846)
SELECT matnr
bwkey
verpr
stprs
peinh
FROM mbew INTO TABLE t_mbew
FOR ALL ENTRIES IN lt_mbew_temp
Purpose:
- Gets standard price (
stprs) and moving average price (verpr) - Calculates per-unit prices using price unit (
peinh) - Provides cost information for each material
Price Calculation Logic:
<wa_output>-stprs = lw_mbew-stprs / lw_mbew-peinh.
<wa_output>-verpr = lw_mbew-verpr / lw_mbew-peinh.
Section 4: Time Bucket Processing - Weekly Mode (Lines 6848-6892)
When r_weeks = 'X':
DO w_columns TIMES VARYING lw_qty FROM <wa_output>-qty1
NEXT <wa_output>-qty2.
READ TABLE r_dates INDEX sy-index.
CLEAR lr_range[].
APPEND r_dates TO lr_range.
LOOP AT t_matfcast ASSIGNING <lw_matfcast> FROM lv_indx.
IF <lw_matfcast>-dat01 IN lr_range.
lw_qty = lw_qty + <lw_matfcast>-mng01.
Processing Flow:
Section 5: Time Bucket Processing - Period Mode (Lines 6894-6980)
When r_accprd = 'X' or r_calprd = 'X':
IF r_accprd = c_x.
lv_prd = p_prd1_l. " Starting accounting period
lv_bdatj = p_yr1_l. " Starting year
ELSEIF r_calprd = c_x.
lv_prd = p_prd2_l. " Starting calendar period
lv_bdatj = p_yr2_l. " Starting year
ENDIF.
DO w_columns TIMES.
IF lv_prd GT 12. " Handle year rollover
lv_prd = 1.
lv_bdatj = lv_bdatj + 1.
ENDIF.
Key Features:
- Year Rollover: Automatically handles period 13 → period 1 of next year
- Period Lookup: Uses
t_future_periodsto get exact date ranges - Overdue Handling: First period includes overdue requirements
Period Processing Logic:
Section 6: Safety Stock Integration (Lines 6881-6885)
IF p_sftstk <> 'X' AND
<lw_matfcast>-extra = 'Safety Stock'.
<lw_matfcast>-mng01 = <lw_matfcast>-mng01 + lv_safety_stk.
ENDIF.
Business Logic:
- Safety stock can be included or excluded from the first period
- When excluded (
p_sftstk <> 'X'), it's added back to prevent double-counting - Safety stock affects the total demand calculation
Data Structures and Relationships
Input Tables:
| Table | Source | Purpose |
|---|---|---|
t_matfcast | process_data | Aggregated demand by date |
t_output | Pre-populated | Material master info |
t_safety_stk | select_data | Safety stock quantities |
r_dates | Period calculations | Week/month definitions |
Output Structure:
| Field | Purpose | Source |
|---|---|---|
qty1-qty19 | Time bucket quantities | Aggregated from t_matfcast |
total_qty | Sum of all periods | Calculated during processing |
eisbe | Safety stock level | t_safety_stk |
stprs/verpr | Material prices | mbew table |
Processing Variables:
Algorithm Deep-Dive: Time Bucket Aggregation
Core Algorithm:
- For each material in the output table
- For each time period (week/month/accounting period)
- Define the date range for this period
- Find all forecast entries that fall within this range
- Sum the quantities
- Store in the appropriate column (qty1, qty2, etc.)
- Mark processed entries to avoid double-counting
- Calculate totals across all periods
Efficiency Optimizations:
- BINARY SEARCH: Used for t_matfcast lookups
- FROM clause: Starts loop from last known position
- Date range filtering: Uses IN operator for fast date matching
- Process flag:
dflg = 'X'prevents double-counting
Common Beginner Challenges
Challenge 1: Understanding VARYING
DO w_columns TIMES VARYING lw_qty FROM <wa_output>-qty1
NEXT <wa_output>-qty2.
Explanation: This ABAP construct automatically moves from qty1 to qty2 to qty3, etc., without manual field specification.
Challenge 2: Date Range Logic
The program handles three different time bucketing approaches:
- Weekly: Fixed 7-day periods
- Accounting: Company fiscal periods (may be 4-4-5 weeks)
- Calendar: Standard months
Challenge 3: Follow-up Material Logic
The duplicate detection logic ensures that if Material A is replaced by Material B, both entries are shown but duplicates of Material B are removed.
Performance Considerations
Critical Performance Sections:
- Lines 6869-6890: Nested loops through t_matfcast (potentially large)
- Lines 6944-6970: Similar nested processing for period mode
- Lines 6714-6765: Complex duplicate checking logic
Optimization Techniques Used:
- Pre-sorting of tables for binary search
- Using index positions to start loops from last known position
- Date range operations instead of individual date checks
- Process flags to avoid re-processing data
Error Handling and Edge Cases
- Year Rollover: Lines 6909-6912 handle period 13 becoming period 1
- Missing Periods: Lines 6930-6941 handle cases where period data is missing
- Empty Forecast Data: sy-subrc checks prevent processing empty results
- Safety Stock Toggle: Conditional logic handles inclusion/exclusion
Integration Points
Incoming Dependencies:
t_matfcastfromprocess_datat_outputstructure pre-built with material info- Time period definitions from period calculation forms
- User parameters for safety stock and time bucket type
Outgoing Results:
t_outputwith populated quantity columns → Used by ALV display- Hotspot-enabled fields → Enable drill-down functionality
- Total quantities → Used for summary reporting
Next Module Connection
The completed t_output table becomes the input for fill_field_catalog which defines the ALV display structure and column headers.
Navigation
← Back to Overview | ← Previous: Data Processing | Next: ALV Display →
Related Modules
- Data Processing Core - Provides the t_matfcast input
- ALV Display Controller - Displays the t_output created here
- Period Calculations - Time bucket framework used for column mapping