‎2008 Feb 01 9:19 AM
Dear Experts,
Im using BAPI_PBSRVAPS_GETDETAIL2 to fetch the data from Planning Book for a certain key figure suppose key1.I'm getting all the values perfectly.The same BAPI is used in the same program to fetch the values of other keyfigure suppose key2,but im getting wrong values.
What i observed is when im using dat BAPI, first to fetch values of key2 and then for key1, its givin correct values for key2 and for the key 1 its picking wrong value.
So please help me to resolve this.
Thanking You,
Dayakar
‎2008 Feb 03 4:25 PM
Neetesh,
Some insight into the code you have used for calling the BAPI and processing the data thereafter would help us in helping you..
Thanks,
Mani
‎2008 Feb 04 7:32 AM
Check this code:
&----
*& Report ZUSFORECAST
*&
&----
REPORT ZPLIUSFCAST MESSAGE-ID zj01
LINE-SIZE 132.
************************************************************************
PROGRAM....... ZPLIUSFCAST
TITLE......... APO Forecast Interface
AUTHOR........ Aveek Ghose
DATE WRITTEN.. 15/09/2006
R/3 RELEASE... 4.6C
=====================================================================*
COPIED FROM... N/A
DESCRIPTION... This program reads a forecast file from PPC and loads
forecast quantities into APO Demand Planning LiveCache
- Key Figure 'Customer Forecast Units' (Z_CUSFU).
Include change request PD7K900203 in initial transport.
=====================================================================*
PROGRAM TYPE.. Inbound Interface
DEV. CLASS.... ZPLN
LOGICAL DB.... N/A
=====================================================================*
SCREENS....... N/A
GUI TITLE..... N/A
GUI STATUS.... N/A
TRANSACTIONS.. N/A
USER EXITS.... N/A
=====================================================================*
CHANGE HISTORY *
Date By Correction Number & Brief Description Release
----
Tables / Structures
----
*MOD-011
*TABLES: zpln_ppc_loc,
zpln_ppc_mat.
*MOD-011
Selection table for BAPI function modules
DATA: t_selection TYPE STANDARD TABLE OF bapi10030pbselection,
g_selection_wa LIKE LINE OF t_selection.
Key Figure table for BAPI function module
DATA: t_key_figure_selection TYPE STANDARD TABLE OF bapi10030keyfigure,
g_key_figure_selection_wa LIKE LINE OF t_key_figure_selection.
Group By table for BAPI function module
DATA: t_group_by TYPE STANDARD TABLE OF bapi10030pbgroupby,
g_group_by_wa LIKE LINE OF t_group_by.
Time Series table output from BAPI_PBSRVAPS_GETDETAIL
DATA: t_time_series TYPE STANDARD TABLE OF bapi10030pbtimeserieso,
g_time_series_wa LIKE LINE OF t_time_series.
Time Series table input to BAPI_PBSRVAPS_CHANGEKEYFIGVAL
DATA: t_time_series_chg TYPE STANDARD TABLE OF bapi10030pbtimeseries,
g_time_series_chg_wa LIKE LINE OF t_time_series_chg.
Time Bucket data input to BAPI_PBSRVAPS_CHANGEKEYFIGVAL
DATA: t_time_series_item TYPE STANDARD TABLE OF
bapi10030pbtimeseriesitem,
g_time_series_item_wa LIKE LINE OF t_time_series_item.
Characteristic Combination table for BAPI_PBSRVAPS_CHANGEKEYFIGVAL
DATA: t_char_comb TYPE STANDARD TABLE OF bapi10030pbcharacteristics,
g_char_comb_wa LIKE LINE OF t_char_comb.
Return table from BAPI function modules
DATA: t_return TYPE STANDARD TABLE OF bapiret2,
g_return_wa LIKE LINE OF t_return.
TYPES: BEGIN OF ty_forecast_data,
source_code(2) type c, "Source Code
lang_group(21) type c, "Language Group
pres_code(6) type c, "Presentation Code
pres_name(40) type c, "Presentation Name
site(4) TYPE c, " PPC Location
indication(3) type c, "Indication
date type datum, "Date
gi_date type datum, "Date
ord_number(8) type c, "Order Number
ord_line(6) type c, "Order Line Number
units(20) type c, "Units
END OF ty_forecast_data.
TYPES: BEGIN OF ty_forecast_data1,
source_code(2) type c, "Source Code
lang_group(21) type c, "Language Group
pres_code(6) type c, "Presentation Code
r3_material(40) TYPE c, " R/3 Material Code
pres_name(40) type c, "Presentation Name
site(4) TYPE c, " PPC Location
r3_loc(10) TYPE c, " R/3 Location
indication(3) type c, "Indication
date type datum, "Date
gi_date type datum, "Date
ord_number(8) type c, "Order Number
ord_line(6) type c, "Order Line Number
units(20) type c, "Units
forecast_cv(20) type c, " Forecast Quantity (converted)
calweek like scal-week, " Calendar Week
calweek_end like scal-week, " Calendar Week
calmonth(6) type c, " Calendar Month
calweekend_date type scal-date, " Calendar Week End date
calyear(6) type c, " Calendar Year
calweek_diff(4) type c, "Week Diff
startweek like scal-week,
endweek like scal-week,
start_day like scal-date,
end_day like scal-date,
calyear_start(4) type c, " Year Start
calyear_end(4) type c, " Year End
delete(1) type c, "Delete Flag
END OF ty_forecast_data1.
TYPES: BEGIN OF ty_forecast_data2,
source_code(2) type c, "Source Code
lang_group(21) type c, "Language Group
pres_code(6) type c, "Presentation Code
r3_material(40) TYPE c, " R/3 Material Code
pres_name(40) type c, "Presentation Name
site(4) TYPE c, " PPC Location
r3_loc(10) TYPE c, " R/3 Location
indication(3) type c, "Indication
calweek like scal-week, " Calendar Week
calweek_end type scal-week, " Calendar Week
endweek like scal-week,
forecast_cv(20) type c, " Forecast Quantity (converted)
date type datum, "Date
gi_date type datum, "Date
ord_number(8) type c, "Order Number
ord_line(6) type c, "Order Line Number
units(20) type c, "Units
calyear_start(4) type c, " Year Start
calyear_end(4) type c, " Year End
calmonth(6) type c, " Calendar Month
calyear(6) type c, " Calendar Year
startweek like scal-week,
start_day like scal-date,
end_day like scal-date,
delete(1) type c, "Delete Flag
END OF ty_forecast_data2.
TYPES: BEGIN OF ty_forecast_data4,
r3_material(40) TYPE c, " R/3 Material Code
site(4) TYPE c, " PPC Location
r3_loc(10) TYPE c, " R/3 Location
indication(3) type c, "Indication
calweek like scal-week, " Calendar Week
source_code(2) type c, "Source Code
lang_group(21) type c, "Language Group
pres_code(6) type c, "Presentation Code
pres_name(40) type c, "Presentation Name
calweek_end type scal-week, " Calendar Week
endweek like scal-week,
forecast_cv(20) type c, " Forecast Quantity (converted)
date type datum, "Date
gi_date type datum, "Date
ord_number(8) type c, "Order Number
ord_line(6) type c, "Order Line Number
units(20) type c, "Units
calyear_start(4) type c, " Year Start
calyear_end(4) type c, " Year End
calmonth(6) type c, " Calendar Month
calyear(6) type c, " Calendar Year
startweek like scal-week,
start_day like scal-date,
end_day like scal-date,
delete(1) type c, "Delete Flag
END OF ty_forecast_data4.
DATA: t_forecast_data2 TYPE STANDARD TABLE OF
ty_forecast_data2 initial size 0 with header line.
DATA: t_forecast_data3 TYPE STANDARD TABLE OF
ty_forecast_data2 initial size 0 with header line.
DATA: t_forecast_data4 TYPE STANDARD TABLE OF
ty_forecast_data4 initial size 0 with header line.
DATA: t_forecast_data TYPE STANDARD TABLE OF
ty_forecast_data initial size 0 with header line,
t_forecast_data1 TYPE STANDARD TABLE OF
ty_forecast_data1 initial size 0 with header line,
g_forecast_data_wa LIKE LINE OF t_forecast_data1,
g_forecast_data_wa2 LIKE LINE OF t_forecast_data2,
g_forecast_data_wa3 LIKE LINE OF t_forecast_data3,
l_forecast_data TYPE STANDARD TABLE OF ty_forecast_data1,
l_forecast_data_wa like line of t_forecast_data1,
l_forecast_data2 TYPE STANDARD TABLE OF ty_forecast_data2,
l_forecast_data_wa2 like line of t_forecast_data2.
Smaller forecast table to hold data for specific product/plant
combination]
*MOD-006
*DATA: t_forecast_sub TYPE STANDARD TABLE OF ty_forecast_data1,
g_forecast_sub_wa LIKE LINE OF t_forecast_sub.
*MOD-006
DATA: t_forecast_sub TYPE STANDARD TABLE OF ty_forecast_data2,
t_forecast_sub1 TYPE STANDARD TABLE OF ty_forecast_data2,
g_forecast_sub_wa LIKE LINE OF t_forecast_sub.
Error table
TYPES: BEGIN OF ty_error_data.
INCLUDE TYPE ty_forecast_data1.
TYPES: error_type(1) TYPE c,
END OF ty_error_data.
DATA: t_error_data TYPE STANDARD TABLE OF ty_error_data,
g_error_data_wa LIKE LINE OF t_error_data.
Structure to hold input file data
TYPES: BEGIN OF g_input_data1,
line(255) type c,
END OF g_input_data1.
DATA: g_input_data type standard table of
g_input_data1 initial size 0 with header line.
----
Global Variables
----
DATA: g_date_from TYPE /sapapo/cdps_eval_date_from, " From Date
g_date_to TYPE /sapapo/cdps_eval_date_to. " To Date
DATA: g_file LIKE dxfile-filename, " Filename
g_material TYPE /sapapo/matnr, " Material
g_cnt_input_recs TYPE i, " Number of file records
g_cnt_changes TYPE i, " Number of changes attempted
g_cnt_error_recs TYPE i, " Number of records with errors
g_flg_error. " Error
----
Objects
----
DATA: i_file TYPE REF TO zcl_file.
----
Constants
----
CONSTANTS: c_planningbook TYPE /sapapo/pb_mview " Planning Book
VALUE 'Z_PPC_FCST_DP',
c_period_type TYPE /sapapo/cdps_eval_type " Period Type
VALUE 'A'.
CONSTANTS:
c_version(22) TYPE c VALUE '000', " Version
c_log_type TYPE c VALUE 'L', " Logical Filetype
c_logqs TYPE /sapapo/logqs VALUE 'PSGA', " Business Sys Grp
c_ppc_mat_error(1) TYPE c VALUE '1',
" PPC Material not found in custom mapping table
c_ppc_loc_error(1) TYPE c VALUE '2',
" PPC Location not found in custom mapping table
c_r3_mat_error(1) TYPE c VALUE '3',
" R/3 to APO material mapping error
c_r3_loc_error(1) TYPE c VALUE '4',
" R/3 to APO location mapping error
c_pbook_error(1) TYPE c VALUE '5',
" Error getting planning book information
c_date_error(1) TYPE c VALUE '6',
" Invalid date error
c_change_error(1) TYPE c VALUE '7',
" Error changing key figure values
c_per_ind TYPE /sapapo/perkz VALUE 'P', " Period Indicator
c_fisc_var TYPE /sapapo/periv VALUE 'JJ', " Fiscal year variant
c_separator TYPE c VALUE ',', " Field separator
c_no_hdr_material(1) TYPE c VALUE '1',
C_LOCTYPE TYPE /SAPAPO/C_LOCTYPE VALUE '1002'.
" No header material in file record
----
Selection Screen
----
Files to process
SELECTION-SCREEN BEGIN OF BLOCK file WITH FRAME TITLE text-001.
SELECTION-SCREEN SKIP.
SELECTION-SCREEN BEGIN OF BLOCK infile WITH FRAME TITLE text-002.
PARAMETERS: p_login LIKE filename-fileintern OBLIGATORY
DEFAULT 'Z_DDS_43525_GPSG_EMEA_DEMAND'.
PARAMETERS: p_phyin LIKE filename-fileextern.
PARAMETERS: p_horz(4) type c default '60'.
PARAMETERS: P_DIST(4) TYPE C default '0200'.
PARAMETERS: P_LOGSYS TYPE EDIPPARNUM DEFAULT 'PD1120DEV'.
SELECTION-SCREEN END OF BLOCK infile.
SELECTION-SCREEN END OF BLOCK file.
&----
At Selection Screen
&----
AT SELECTION-SCREEN.
PERFORM sub_get_physical_file USING p_login p_phyin.
&----
At Selection Screen Output
&----
Make the physical file name display only
AT SELECTION-SCREEN OUTPUT.
LOOP AT SCREEN.
IF screen-name = 'P_PHYIN'.
screen-input = 0.
MODIFY SCREEN.
ENDIF.
ENDLOOP.
&----
INITIALIZATION.
&----
INITIALIZATION.
CLEAR: g_return_wa,
g_selection_wa,
g_key_figure_selection_wa,
g_group_by_wa,
g_time_series_wa,
g_time_series_chg_wa,
g_time_series_item_wa,
g_char_comb_wa,
g_forecast_data_wa,
g_forecast_sub_wa,
g_error_data_wa,
g_input_data,
g_date_from,
g_date_to,
g_cnt_input_recs,
g_cnt_changes,
g_cnt_error_recs,
g_flg_error.
REFRESH: t_return,
t_selection,
t_key_figure_selection,
t_group_by,
t_time_series,
t_time_series_chg,
t_time_series_item,
t_char_comb,
t_forecast_data,
t_forecast_sub,
t_error_data.
PERFORM sub_get_physical_file USING p_login p_phyin.
&----
Start-Of-Selection
&----
START-OF-SELECTION.
Read input file into internal table
PERFORM sub_read_file.
PERFORM validate_locmap.
IF NOT t_forecast_data[] IS INITIAL.
Map data to format for processing
PERFORM sub_map_data.
IF NOT t_forecast_data2[] IS INITIAL.
Process valid data records
PERFORM sub_process_data.
ENDIF.
ENDIF.
Display program results
PERFORM sub_display_results.
**&----
**
*& Form sub_display_results
&----
Write results to spool
----
FORM sub_display_results.
Program Results
SKIP.
WRITE: / text-025.
File locations
Input File
SKIP.
WRITE: / text-026,
15 p_phyin.
Record Counts
SKIP.
Number of records in input file
WRITE: / text-027,
48 g_cnt_input_recs.
Number of records with errors
WRITE: / text-028,
48 g_cnt_error_recs.
Number of records successfully updated in planning book
WRITE: / text-029,
48 g_cnt_changes.
*MOD-010
IF NOT t_error_data[] IS INITIAL.
Error Details:
SKIP.
WRITE: / text-024.
SKIP.
WRITE: / text-006,
13 text-007,
29 text-008,
41 text-009,
61 text-010.
WRITE: / text-011 UNDER text-006,
text-012 UNDER text-007,
text-013 UNDER text-008,
text-014 UNDER text-009,
text-015 UNDER text-010.
*MOD-010
SORT T_ERROR_DATA BY pres_code
site
date
forecast_cv.
Delete adjacent duplicates from t_error_data comparing pres_code
site
date
forecast_cv.
*MOD-010
LOOP AT t_error_data INTO g_error_data_wa.
WRITE: / g_error_data_wa-pres_code UNDER text-006,
g_error_data_wa-site UNDER text-007,
g_error_data_wa-date UNDER text-008,
48 g_error_data_wa-forecast_cv RIGHT-JUSTIFIED.
CASE g_error_data_wa-error_type.
WHEN c_ppc_mat_error.
WRITE text-031 UNDER text-010.
WHEN c_r3_mat_error.
WRITE text-019 UNDER text-010.
WHEN c_r3_loc_error.
WRITE text-020 UNDER text-010.
WHEN c_pbook_error.
WRITE text-021 UNDER text-010.
WHEN c_date_error.
WRITE text-022 UNDER text-010.
WHEN c_change_error.
WRITE text-023 UNDER text-010.
ENDCASE.
CLEAR: g_error_data_wa.
ENDLOOP.
IF sy-batch IS INITIAL.
MESSAGE i000 WITH text-030.
ELSE.
MESSAGE e000 WITH text-030. "TPR1336-
MESSAGE i000 WITH text-030. "TPR1336+
ENDIF.
ENDIF.
ENDFORM. " sub_display_results
&----
*& Form sub_map_data
&----
Map PPC codes to SAP codes and convert input values
----
FORM sub_map_data.
DATA: l_forecast(6) type c,
l_uom TYPE i.
DATA:
l_sold_num(10) TYPE n, " Sold To Quantity (numerical char.)
l_sold(7) TYPE p DECIMALS 3, " Sold To Quantity (Packed, 3 dec.)
l_calweek like scal-week, " Calendar Week
l_calweek_end like scal-week, " Calendar Week
l_startcalweek like scal-week, " Calendar Week
l_calweek1(2) type c, " Calendar Week
l_calweek_end1(2) type c, " Calendar Week
l_calweekend_date like scal-week, " Calendar Week
l_calmonth(6) type c, " Calendar Month
l_calyear_start(4) type c, " Calendar Year
l_calyear_end(4) type c, " Calendar Year
l_calweek_diff type scal-week, " Diff in week
l_fiscper(7) TYPE c. " Fiscal Period
DATA: l_temp_wa LIKE g_forecast_data_wa2,
l_hold_wa LIKE g_forecast_data_wa2.
DATA: lt_forecast_data2 TYPE STANDARD TABLE OF
ty_forecast_data2 initial size 0 with header line.
DATA: lt_forecast_data3 TYPE STANDARD TABLE OF
ty_forecast_data2 initial size 0 with header line.
DATA: lt_forecast_data4 TYPE STANDARD TABLE OF
ty_forecast_data2 initial size 0 with header line.
DATA: l_wa_forecast_data type ty_forecast_data2.
DATA: l_wa_forecast_data1 type ty_forecast_data1.
DATA: l_wa_forecast_data2 type ty_forecast_data2.
*MOD-008
Local variables
DATA: l_curr_date TYPE sydatum, " Program Run Date
l_record_date TYPE sydatum, " Formatted Record Date
l_curr_year(2) TYPE c, " Program Year (without century)
l_curr_year_num TYPE i, " Integer value of Program Year
l_century(2) TYPE c, " Program Century (without year)
l_century_num TYPE i, " Integer value of Century
l_input_year_num TYPE i. " Integer value of Record Year
Record date - format MM/DD/YY
DATA: BEGIN OF l_input_date,
month(2) TYPE c,
filler1(1) TYPE c,
day(2) TYPE c,
filler2(1) TYPE c,
year(2) TYPE c,
END OF l_input_date.
*MOD-008
FIELD-SYMBOLS:
<L_WA_FORECAST> TYPE ty_forecast_data2.
DATA: LV_FORECAST_CV type f.
CLEAR: lv_forecast_cv.
DATA: l_first_day LIKE SCAL-DATE,
l_last_day LIKE SCAL-DATE.
DATA: l_first_day1 LIKE SCAL-DATE,
l_last_day1 LIKE SCAL-DATE.
DATA: l_start_week LIKE SCAL-WEEK,
l_end_week LIKE SCAL-WEEK.
DATA: l_start_week1 LIKE SCAL-WEEK,
l_end_week1 LIKE SCAL-WEEK.
DATA: lv_sap_code(50) type c.
DATA: RETURN TYPE BAPIRET2.
DATA: l_date_tabix like sy-tabix.
DATA: L_TABIX TYPE SY-TABIX.
DATA: l_start_year(4) type c,
l_end_year(4) type c,
l_calyear_diff(4) type c,
l_calyear_data_start(4) type c.
LOOP AT T_FORECAST_DATA.
MOVE-CORRESPONDING T_FORECAST_DATA TO T_FORECAST_DATA1.
APPEND T_FORECAST_DATA1.
CLEAR: T_FORECAST_DATA1.
ENDLOOP.
Loop through internal table
LOOP AT t_forecast_data1 INTO g_forecast_data_wa.
Map Material code to SAP product and conversion factor using
cross-reference table; if not found, delete record and store
error.
CLEAR: LV_SAP_CODE.
CALL FUNCTION 'Z_XREF_LOOKUP' DESTINATION P_LOGSYS
EXPORTING
TRANSID = 'ZELCUSMATLOOKUP'
INVALUE = g_forecast_data_wa-pres_code
IMPORTING
OUTVALUE = lv_sap_code
RETURN = RETURN.
IF SY-SUBRC = 0.
IF NOT LV_SAP_CODE IS INITIAL. "RETURN IS INITIAL.
Convert SAP Material Code to APO Material Number; if not found,
delete record and store error
SELECT SINGLE matnr FROM /sapapo/matmap
INTO g_forecast_data_wa-r3_material
WHERE ext_matnr = lv_sap_code
AND logqs = c_logqs.
IF sy-subrc <> 0.
MOVE-CORRESPONDING g_forecast_data_wa TO g_error_data_wa.
g_error_data_wa-error_type = c_r3_mat_error.
APPEND g_error_data_wa TO t_error_data.
CLEAR g_error_data_wa.
DELETE t_forecast_data1.
CLEAR g_forecast_data_wa.
ADD 1 TO g_cnt_error_recs.
CONTINUE.
ENDIF.
*MOD-011
l_uom = 1.
*MOD-011
ELSE.
Store material in error table; delete current record of data
table; continue
MOVE-CORRESPONDING g_forecast_data_wa TO g_error_data_wa.
g_error_data_wa-error_type = c_ppc_mat_error.
APPEND g_error_data_wa TO t_error_data.
CLEAR g_error_data_wa.
DELETE t_forecast_data1.
CLEAR g_forecast_data_wa.
ADD 1 TO g_cnt_error_recs.
CONTINUE.
ENDIF.
Convert Forecast Quantity using conversion factor
l_forecast = g_forecast_data_wa-units.
g_forecast_data_wa-forecast_cv = g_forecast_data_wa-units.
*MOD-009
*MOD-006
CLEAR: l_calweek,
l_calweek_end,
l_calweekend_date,
l_calweek_diff,
l_fiscper,
l_calmonth,
l_calyear_start,
l_calyear_end,
l_start_week,
l_first_day,
l_end_week,
l_last_day.
*MOD-006
*MOD-006
*Convert Monday Date to Calendar Week and Fiscal Period
*MOD-010
PERFORM sub_format_dates USING g_forecast_data_wa-gi_date
l_calweek
l_calweek_end
l_calweekend_date
l_calweek_diff
l_fiscper
l_calmonth
l_calyear_start
l_calyear_end
l_start_week
l_first_day
l_end_week
l_last_day.
g_forecast_data_wa-calweek = l_calweek.
g_forecast_data_wa-calweek_end = l_calweek_end.
g_forecast_data_wa-calmonth = l_calmonth.
g_forecast_data_wa-calyear_start = l_calyear_start.
g_forecast_data_wa-calyear_end = l_calyear_end.
g_forecast_data_wa-startweek = l_start_week.
g_forecast_data_wa-endweek = l_end_week.
g_forecast_data_wa-start_day = l_first_day.
g_forecast_data_wa-end_day = l_last_day.
*MOD-010
Modify table record
MODIFY t_forecast_data1 FROM g_forecast_data_wa.
CLEAR g_forecast_data_wa.
ENDLOOP.
*MOD-010
LOOP AT T_FORECAST_DATA1 INTO L_WA_FORECAST_DATA1.
L_WA_FORECAST_DATA2-source_code =
L_WA_FORECAST_DATA1-source_code.
L_WA_FORECAST_DATA2-lang_group =
L_WA_FORECAST_DATA1-lang_group.
L_WA_FORECAST_DATA2-pres_code = L_WA_FORECAST_DATA1-pres_code.
L_WA_FORECAST_DATA2-r3_material = L_WA_FORECAST_DATA1-r3_material.
L_WA_FORECAST_DATA2-pres_name = L_WA_FORECAST_DATA1-pres_name.
L_WA_FORECAST_DATA2-site = L_WA_FORECAST_DATA1-site.
L_WA_FORECAST_DATA2-r3_loc = L_WA_FORECAST_DATA1-r3_loc.
L_WA_FORECAST_DATA2-indication = L_WA_FORECAST_DATA1-indication.
L_WA_FORECAST_DATA2-date = L_WA_FORECAST_DATA1-date.
L_WA_FORECAST_DATA2-gi_date = L_WA_FORECAST_DATA1-gi_date.
L_WA_FORECAST_DATA2-ord_number = L_WA_FORECAST_DATA1-ord_number.
L_WA_FORECAST_DATA2-ord_line = L_WA_FORECAST_DATA1-ord_line.
L_WA_FORECAST_DATA2-units = L_WA_FORECAST_DATA1-units.
L_WA_FORECAST_DATA2-forecast_cv = L_WA_FORECAST_DATA1-forecast_cv.
L_WA_FORECAST_DATA2-calweek = L_WA_FORECAST_DATA1-calweek.
L_WA_FORECAST_DATA2-calweek_end = L_WA_FORECAST_DATA1-calweek_end.
L_WA_FORECAST_DATA2-startweek = L_WA_FORECAST_DATA1-startweek.
L_WA_FORECAST_DATA2-endweek = L_WA_FORECAST_DATA1-endweek.
L_WA_FORECAST_DATA2-start_day = L_WA_FORECAST_DATA1-start_day.
L_WA_FORECAST_DATA2-end_day = L_WA_FORECAST_DATA1-end_day.
L_WA_FORECAST_DATA2-calyear_start =
L_WA_FORECAST_DATA1-calyear_start.
L_WA_FORECAST_DATA2-calyear_end = L_WA_FORECAST_DATA1-calyear_end.
L_WA_FORECAST_DATA2-delete = L_WA_FORECAST_DATA1-delete.
APPEND l_wa_forecast_data2 TO t_forecast_data2.
CLEAR: l_wa_forecast_data1.
Clear: t_forecast_data2.
ENDLOOP.
*MOD-010
SORT T_FORECAST_DATA2 BY
source_code
lang_group
pres_code
r3_material
pres_name
site
r3_loc
indication
calweek
endweek
calmonth
calyear_start
calyear_end
calweek_end
startweek
start_day
end_day
delete.
*****************************************************************
**MOD-010
*****************************************************************
clear: lv_forecast_cv.
collecting the product Variant.
*MOD-007
lt_forecast_data2[] = t_forecast_data2[].
Aggregation for JOR and NOR.
delete lt_forecast_data2 where indication <> 'JOR'
and indication <> 'NOR'.
IF NOT lt_forecast_data2[] is initial.
LOOP AT lt_forecast_data2 ASSIGNING <L_WA_FORECAST>.
LV_FORECAST_CV = LV_FORECAST_CV + <L_WA_FORECAST>-forecast_cv.
<L_WA_FORECAST>-DELETE = 'X'.
AT END OF CALWEEK.
<L_WA_FORECAST>-DELETE = ' '.
<L_WA_FORECAST>-forecast_cv = lv_forecast_cv.
CLEAR lv_forecast_cv.
ENDAT.
ENDLOOP.
ENDIF.
DELETE lt_forecast_data2 WHERE delete = 'X'.
Aggregation where Indication ne JOR and NOR.
lt_forecast_data3[] = t_forecast_data2[].
loop at lt_forecast_data3.
if lt_forecast_data3-indication = 'JOR'
or lt_forecast_data3-indication = 'NOR'.
delete lt_forecast_data3.
else.
*MOD-009
*if lt_forecast_data3-calweek > lt_forecast_data3-calweek_end.
*MOD-009
***MOD-011
if lt_forecast_data3-calweek > lt_forecast_data3-endweek.
delete lt_forecast_data3.
else.
append lt_forecast_data3 to lt_forecast_data4.
endif.
***MOD-011
append lt_forecast_data3 to lt_forecast_data4.
**MOD-011
*MOD-010
endif.
*MOD-010
endif.
endloop.
Aggregation beyond the horizon for all indication.
IF NOT lt_forecast_data3[] is initial.
LOOP AT lt_forecast_data3 ASSIGNING <L_WA_FORECAST>.
LV_FORECAST_CV = LV_FORECAST_CV + <L_WA_FORECAST>-forecast_cv.
<L_WA_FORECAST>-DELETE = 'X'.
AT END OF CALWEEK.
<L_WA_FORECAST>-DELETE = ' '.
<L_WA_FORECAST>-forecast_cv = lv_forecast_cv.
CLEAR lv_forecast_cv.
ENDAT.
ENDLOOP.
ENDIF.
DELETE lt_forecast_data3 WHERE delete = 'X'.
append lines of lt_forecast_data3 to lt_forecast_data2.
*MOD-007
*MOD-007
Refresh: t_forecast_data2.
append lines of lt_forecast_data2 to t_forecast_data2.
SORT T_FORECAST_DATA2 BY
source_code
lang_group
pres_code
r3_material
pres_name
site
r3_loc
indication
calweek
endweek
calweek_end
startweek
start_day
end_day
calyear_start
calyear_end
delete.
lt_forecast_data2[] = t_forecast_data2[].
READ TABLE lt_FORECAST_DATA2 INDEX 1.
l_start_year = lt_forecast_data2-calyear_start.
DESCRIBE TABLE lt_forecast_data2 lines l_tabix.
READ TABLE lt_forecast_data2 index l_tabix.
l_end_year = lt_forecast_data2-calyear_end.
loop at lt_forecast_data2.
if sy-tabix = 1.
l_calyear_data_start = ( lt_forecast_data2-calyear_start + 2 ).
endif.
exit.
endloop.
*MOD-009
delete lt_forecast_data2 where
calyear_start > l_calyear_data_start. "#EC PORTABLE
*MOD-009
delete lt_forecast_data2 where gi_date < sy-datum.
*MOD-009
Refresh: t_forecast_data2.
append lines of lt_forecast_data2 to t_forecast_data2.
*MOD-009
*****************************************************************
**MOD-010
*****************************************************************
ENDFORM. " sub_map_data
**&----
**
**& Form sub_process_data
**&----
**
Loop through data records, separate into product/location
sub-groups, and process
**----
**
FORM sub_process_data.
DATA: l_temp_wa LIKE g_forecast_data_wa2,
l_hold_wa LIKE g_forecast_data_wa2.
CLEAR l_hold_wa.
**MOD-010
SORT t_forecast_data2 BY r3_material
r3_loc
date
calweek ASCENDING.
**MOD-010
Loop through internal table, collecting sub-table for each
material/plant
LOOP AT t_forecast_data2 INTO g_forecast_data_wa2.
l_temp_wa = g_forecast_data_wa2.
Initial Loop Pass -
Save hold variables during first loop pass
AT FIRST.
l_hold_wa = l_temp_wa.
ENDAT.
Normal Loop Processing -
If control break occurs:
(1) process existing sub-table for
material plant combination;
(2) clear sub-table;
(3) set new hold values;
(4) append current work area to sub-table.
*
Otherwise, append current work area to sub-table
IF ( l_hold_wa-r3_material <> g_forecast_data_wa2-r3_material ) OR
( l_hold_wa-r3_loc <> g_forecast_data_wa2-r3_loc ) OR
( l_hold_wa-calweek <> g_forecast_data_wa2-calweek ).
Process sub-table; clear sub-table
Set new hold values; append sub-table.
l_hold_wa = l_temp_wa.
APPEND g_forecast_data_wa2 TO t_forecast_sub.
CLEAR g_forecast_data_wa2.
*MOD-009
PERFORM sub_call_bapi.
REFRESH t_forecast_sub.
*MOD-009
ELSE.
Append sub-table
APPEND g_forecast_data_wa2 TO t_forecast_sub.
CLEAR g_forecast_data_wa2.
ENDIF.
Final Loop Pass -
Process existing sub-table
AT LAST.
PERFORM sub_call_bapi.
ENDAT.
ENDLOOP.
ENDFORM. " sub_process_data
**&----
**
*& Form sub_read_file
&----
Read interface file
----
FORM sub_read_file.
Get Physical Filename
IF p_phyin IS INITIAL.
PERFORM sub_get_physical_file USING p_login p_phyin.
ENDIF.
Open a file object based on the selection screen information.
g_file = p_login.
CREATE OBJECT i_file EXPORTING u_f_filename = g_file
u_f_filetype = c_log_type.
Check authority for reading the interface file.
CALL METHOD i_file->check_read_authority.
Attempt to open file
CALL METHOD i_file->open_for_input.
Read file into data record and append to internal table
PERFORM sub_read_data.
Close file.
CALL METHOD i_file->close.
ENDFORM. " sub_read_file
&----
*& Form sub_get_physical_file
&----
Determine Physical filename from Logical Filename
----
-->P_LFILE Logical Filename
-->P_PFILE Physical Filename
----
FORM sub_get_physical_file USING p_lfile
p_pfile.
CHECK NOT p_lfile IS INITIAL.
CALL FUNCTION 'FILE_GET_NAME'
EXPORTING
client = sy-mandt
logical_filename = p_lfile
operating_system = sy-opsys
IMPORTING
file_name = p_pfile
EXCEPTIONS
file_not_found = 1
OTHERS = 2.
IF sy-subrc NE 0.
p_pfile = '' .
MESSAGE e002 WITH p_lfile.
ENDIF.
ENDFORM. " sub_get_physical_file
&----
*& Form sub_read_data
&----
Read data from input file
----
FORM sub_read_data.
DATA: DELIMETER(2) TYPE C VALUE ';'.
DATA: L_WA_INPUT_DATA TYPE G_INPUT_DATA1.
DO.
READ DATASET i_file->phys_filename INTO g_input_data.
IF sy-subrc NE 0.
IF g_cnt_input_recs = 0.
MESSAGE s000 WITH Text-004 i_file->phys_filename
Text-005.
MESSAGE s000 WITH 'Input file' i_file->phys_filename
'is empty'.
g_flg_error = 'Y'.
STOP.
ENDIF.
EXIT.
ELSE.
APPEND g_input_data.
ADD 1 TO g_cnt_input_recs.
CLEAR: g_input_data,
g_forecast_data_wa.
ENDIF.
ENDDO.
*MOD-002
LOOP AT g_input_data.
t_forecast_data-source_code = g_input_data-line+0(4).
SEARCH t_forecast_data-source_code FOR ';'.
IF sy-subrc EQ 0.
t_forecast_data-source_code+sy-fdpos = space.
ENDIF.
t_forecast_data-lang_group = g_input_data-line+6(24).
CONDENSE t_forecast_data-lang_group.
SEARCH t_forecast_data-lang_group FOR ';'.
IF sy-subrc EQ 0.
t_forecast_data-lang_group+sy-fdpos = space.
ENDIF.
t_forecast_data-pres_code = g_input_data-line+30(9).
CONDENSE t_forecast_data-pres_code.
SEARCH t_forecast_data-pres_code FOR ';'.
IF sy-subrc EQ 0.
t_forecast_data-pres_code+sy-fdpos = space.
ENDIF.
t_forecast_data-pres_name = g_input_data-line+39(38).
CONDENSE t_forecast_data-pres_name.
SEARCH t_forecast_data-pres_name FOR ';'.
IF sy-subrc EQ 0.
t_forecast_data-pres_name+sy-fdpos = space.
ENDIF.
t_forecast_data-site = p_dist.
t_forecast_data-indication = g_input_data-line+77(6).
CONDENSE t_forecast_data-indication.
SEARCH t_forecast_data-indication FOR ';'.
IF sy-subrc EQ 0.
t_forecast_data-indication+sy-fdpos = space.
ENDIF.
t_forecast_data-date = g_input_data-line+83(11).
CONDENSE t_forecast_data-date.
SEARCH t_forecast_data-date FOR ';'.
IF sy-subrc EQ 0.
t_forecast_data-date+sy-fdpos = space.
ENDIF.
t_forecast_data-gi_date = g_input_data-line+94(12).
CONDENSE t_forecast_data-gi_date.
SEARCH t_forecast_data-gi_date FOR ';'.
IF sy-subrc EQ 0.
t_forecast_data-gi_date+sy-fdpos = space.
ENDIF.
t_forecast_data-ord_number = g_input_data-line+106(10).
CONDENSE t_forecast_data-ord_number.
SEARCH t_forecast_data-ord_number FOR ';'.
IF sy-subrc EQ 0.
t_forecast_data-ord_number+sy-fdpos = space.
ENDIF.
t_forecast_data-ord_line = g_input_data-line+116(8).
CONDENSE t_forecast_data-ord_line.
SEARCH t_forecast_data-ord_line FOR ';'.
IF sy-subrc EQ 0.
t_forecast_data-ord_line+sy-fdpos = space.
ENDIF.
t_forecast_data-units = g_input_data-line+124(28).
CONDENSE t_forecast_data-units.
SEARCH t_forecast_data-units FOR ';'.
IF sy-subrc EQ 0.
t_forecast_data-units+sy-fdpos = space.
ENDIF.
APPEND t_forecast_data.
CLEAR t_forecast_data.
CLEAR: g_input_data,
g_forecast_data_wa.
ENDLOOP.
*MOD-002
ENDFORM. " sub_read_data
&----
*& Form sub_call_bapi
&----
Read values of sub-table and update the Customer Forecast
key figure in the correct time buckets
----
FORM sub_call_bapi.
DATA: l_date TYPE sydatum,
l_start_date TYPE sydatum,
l_end_date TYPE sydatum,
l_period_start TYPE bapi10030pbperiodstart,
l_period_end TYPE bapi10030pbperiodend,
l_matnr TYPE /sapapo/matnr,
l_locno TYPE /sapapo/locno,
l_lines TYPE i.
*MOD-011
DATA:
l_calweek like scal-week, " Calendar Week
l_calweek_end like scal-week, " Calendar Week
l_startcalweek like scal-week, " Calendar Week
l_calweek1(2) type c, " Calendar Week
l_calweek_end1(2) type c, " Calendar Week
l_calweekend_date like scal-week, " Calendar Week
l_calmonth(6) type c, " Calendar Month
l_calyear_start(4) type c, " Calendar Year
l_calyear_end(4) type c, " Calendar Year
l_calweek_diff type scal-week, " Diff in week
p_calweek like scal-week, " Calendar Week
p_calweek_end like scal-week, " Calendar Week
p_startcalweek like scal-week, " Calendar Week
p_calweek1(2) type c, " Calendar Week
p_calweek_end1(2) type c, " Calendar Week
p_calweekend_date like scal-week, " Calendar Week
p_calmonth(6) type c, " Calendar Month
p_calyear_start(4) type c, " Calendar Year
p_calyear_end(4) type c, " Calendar Year
p_calweek_diff type scal-week, " Diff in week
p_calweek_start type scal-week, " Diff in week
p_start_week_day type SCDATUM, " Date
l_record_date type sy-datum, " Date
lv_year_start type CYEAR, " Year
lv_week_start type scal-week, " Week
lv_week_end type scal-week. " Week
*MOD-011
DATA: l_time_series_id TYPE /sapapo/mxrow.
*MOD-011
DATA: t_periotab type /SAPAPO/PERIOTAB OCCURS 0 WITH HEADER LINE.
DATA: g_periotab_wa type /SAPAPO/PERIOTAB.
*MOD-011
REFRESH: t_selection,
t_group_by,
t_key_figure_selection,
t_time_series,
t_time_series_chg,
t_time_series_item,
t_return.
READ TABLE t_forecast_sub INDEX 1 INTO g_forecast_sub_wa.
l_matnr = g_forecast_sub_wa-r3_material.
l_locno = g_forecast_sub_wa-r3_loc.
**********************************************************
Fill parameters/tables for BAPI_PBSRVAPS_GETDETAIL ***
**********************************************************
**MOD-006
Date Parameters
g_date_from = g_forecast_sub_wa-date.
g_date_to = g_forecast_sub_wa-gi_date.
**MOD-006
*MOD-006
Selection table
g_selection_wa-characteristic_name = '9AVERSION'. " Version
g_selection_wa-char_val_sign = 'I'.
g_selection_wa-char_val_option = 'EQ'.
g_selection_wa-char_val_low = c_version. " Default '000'
APPEND g_selection_wa TO t_selection.
CLEAR g_selection_wa.
Begin of change for TPR0817
g_selection_wa-characteristic_name = 'ZMATNR'. " APO-DP Product
End of change for TPR0817
g_selection_wa-char_val_sign = 'I'.
g_selection_wa-char_val_option = 'EQ'.
g_selection_wa-char_val_low = l_matnr.
APPEND g_selection_wa TO t_selection.
CLEAR g_selection_wa.
*MOD-006
g_selection_wa-characteristic_name = 'ZDCENTER'. " Location
g_selection_wa-char_val_sign = 'I'.
g_selection_wa-char_val_option = 'EQ'.
g_selection_wa-char_val_low = P_DIST.
APPEND g_selection_wa TO t_selection.
CLEAR g_selection_wa.
*MOD-006
Group By Table
Begin of change for TPR0817
g_group_by_wa-characteristic_name = 'ZMATNR'.
End of change for TPR0817
APPEND g_group_by_wa TO t_group_by.
CLEAR g_group_by_wa.
Key Figure Table - Customer Forecast in Units
g_key_figure_selection_wa-key_figure = 'Z_CUSFU'.
APPEND g_key_figure_selection_wa TO t_key_figure_selection.
CLEAR g_key_figure_selection_wa.
REFRESH t_return.
*MOD-006
Call FM to get time series number
CALL FUNCTION 'BAPI_PBSRVAPS_GETDETAIL'
EXPORTING
planningbook = c_planningbook
SELECTION_ID =
period_type = c_period_type
date_from = g_date_from
date_to = g_date_to
TABLES
selection = t_selection
group_by = t_group_by
key_figure_selection = t_key_figure_selection
time_series = t_time_series
return = t_return.
*MOD-006
IF NOT t_return[] IS INITIAL.
LOOP AT t_forecast_sub INTO g_forecast_sub_wa.
MOVE-CORRESPONDING g_forecast_sub_wa TO g_error_data_wa.
g_error_data_wa-error_type = c_pbook_error.
APPEND g_error_data_wa TO t_error_data.
CLEAR g_error_data_wa.
DELETE t_forecast_sub.
CLEAR g_forecast_sub_wa.
ADD 1 TO g_cnt_error_recs.
ENDLOOP.
EXIT.
ENDIF.
****************************************************************
Fill parameters/tables for BAPI_PBSRVAPS_CHANGEKEYFIGVAL ***
****************************************************************
Time Series table (containing time series ID)
READ TABLE t_time_series INDEX 1 INTO g_time_series_wa.
MOVE-CORRESPONDING g_time_series_wa TO g_time_series_chg_wa.
APPEND g_time_series_chg_wa TO t_time_series_chg.
l_time_series_id = g_time_series_wa-time_series_id.
CLEAR g_time_series_chg_wa.
*MOD-009
T_FORECAST_SUB1[] = T_FORECAST_SUB[].
Time Series Item table (contains all values to be loaded)
LOOP AT t_forecast_sub INTO g_forecast_sub_wa.
Time Series ID
g_time_series_item_wa-time_series_id = l_time_series_id.
Check date validity
CALL FUNCTION 'DATE_CHECK_PLAUSIBILITY'
EXPORTING
date = g_forecast_sub_wa-gi_date
EXCEPTIONS
plausibility_check_failed = 1
OTHERS = 2.
IF sy-subrc <> 0.
*MOD-010
MOVE-CORRESPONDING g_forecast_sub_wa TO g_error_data_wa.
g_error_data_wa-error_type = c_date_error.
APPEND g_error_data_wa TO t_error_data.
CLEAR g_error_data_wa.
DELETE t_forecast_sub.
CLEAR g_forecast_sub_wa.
ADD 1 TO g_cnt_error_recs.
CONTINUE.
ENDIF.
Get Date Range for given date
CALL FUNCTION '/SAPAPO/MC_DATE_GET_FIRST_LAST'
EXPORTING
i_date = g_forecast_sub_wa-gi_date
i_perkz = 'W'
I_FABKL = ' '
I_PERIV = ' '
IMPORTING
i_firstdate = l_start_date
i_lastdate = l_end_date
.
**********
*MOD-011
**********
Starting Timestamp
CONVERT DATE l_start_date INTO TIME STAMP l_period_start
TIME ZONE 'UTC '.
g_time_series_item_wa-period_begin = l_period_start.
*
Ending Timestamp
CONVERT DATE l_end_date TIME '235959' INTO TIME STAMP l_period_end
TIME ZONE 'UTC '.
g_time_series_item_wa-period_end = l_period_end.
*MOD-011
*********
*MOD-011
*********
Data for forecast run date.
l_record_date = sy-datum.
CALL FUNCTION '/SAPAPO/DATE_GET_WEEK'
EXPORTING
date = l_record_date
IMPORTING
week = l_calweek
EXCEPTIONS
date_invalid = 1
OTHERS = 2.
IF sy-subrc <> 0.
g_flg_error = 'X'.
EXIT.
ELSE.
p_calweek(2) = l_calweek+4(2).
p_calweek+2(4) = l_calweek(4).
ENDIF.
lv_year_start = p_calweek+2(4).
lv_week_start = p_calweek+0(2).
lv_week_end = lv_week_start + 1.
concatenate lv_year_start lv_week_end+4(2) into p_calweek_start.
CALL FUNCTION 'WEEK_GET_FIRST_DAY'
EXPORTING
WEEK = p_calweek_start
IMPORTING
DATE = p_start_week_day
EXCEPTIONS
WEEK_INVALID = 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.
CALL FUNCTION '/SAPAPO/SDP_TB_GET_PERIOTAB'
EXPORTING
I_TBID = '104 WEEKS'
I_DIRECTION = '+'
I_STARTDATE = p_start_week_day
I_OFFSET_IN_DAYS = '0'
TABLES
E_PERIOTAB = t_periotab
EXCEPTIONS
TB_NOT_EXISTS = 1
NO_TB_DEFINED = 2
WRONG_TB_DEFINITION = 3
OTHERS = 4.
IF SY-SUBRC <> 0.
MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
ENDIF.
LOOP AT T_PERIOTAB INTO G_PERIOTAB_WA.
Starting Timestamp
CONVERT DATE G_PERIOTAB_WA-ertag
INTO TIME STAMP l_period_start
TIME ZONE 'UTC '.
g_time_series_item_wa-period_begin = l_period_start.
Ending Timestamp
CONVERT DATE G_PERIOTAB_WA-bistag
TIME '235959' INTO TIME STAMP l_period_end
TIME ZONE 'UTC '.
g_time_series_item_wa-period_end = l_period_end.
Forecast Value
g_time_series_item_wa-time_series_value = 0.
g_time_series_item_wa-time_series_id = l_time_series_id.
APPEND g_time_series_item_wa TO t_time_series_item.
CLEAR g_time_series_item_wa.
ENDLOOP.
*MOD-011
ENDLOOP.
REFRESH: t_return,
t_char_comb.
SORT t_time_series_item by TIME_SERIES_ID
period_begin
period_end.
delete adjacent duplicates from t_time_series_item
comparing
TIME_SERIES_ID
PERIOD_BEGIN
PERIOD_END.
*********
**MOD-011
*********
*Delete the first and the last record from the time series table.
data: l_tabix type sy-tabix.
DESCRIBE TABLE t_time_series_item lines l_tabix.
if l_tabix ne 0.
delete t_time_series_item index l_tabix.
delete t_time_series_item index 1.
endif.
*********
**MOD-011
*********
*MOD-011
if g_material ne g_forecast_sub_wa-r3_material.
*MOD-011
Refresh the Planning Book
CALL FUNCTION 'BAPI_PBSRVAPS_CHANGEKEYFIGVAL'
EXPORTING
planningbook = c_planningbook
commit_control = 'E'
TABLES
selection = t_selection
time_series = t_time_series_chg
time_series_item = t_time_series_item
characteristics_combination = t_char_comb
return = t_return.
If error in return table, store message in error table and attempt
rollback; otherwise commit
IF NOT t_return[] IS INITIAL.
CALL FUNCTION 'BAPI_TRANSACTION_ROLLBACK'.
ELSE.
CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'.
ENDIF.
*MOD-011
g_material = g_forecast_sub_wa-r3_material.
ENDIF.
*MOD-011
*MOD-009
Refresh: t_time_series_item.
**************
***MOD-011
**************
Time Series Item table (contains all values to be loaded)
LOOP AT t_forecast_sub1 INTO g_forecast_sub_wa.
Time Series ID
g_time_series_item_wa-time_series_id = l_time_series_id.
Check date validity
CALL FUNCTION 'DATE_CHECK_PLAUSIBILITY'
EXPORTING
date = g_forecast_sub_wa-gi_date
EXCEPTIONS
plausibility_check_failed = 1
OTHERS = 2.
IF sy-subrc <> 0.
MOVE-CORRESPONDING g_forecast_sub_wa TO g_error_data_wa.
g_error_data_wa-error_type = c_date_error.
APPEND g_error_data_wa TO t_error_data.
CLEAR g_error_data_wa.
DELETE t_forecast_sub1.
CLEAR g_forecast_sub_wa.
ADD 1 TO g_cnt_error_recs.
CONTINUE.
ENDIF.
Get Date Range for given date
CALL FUNCTION '/SAPAPO/MC_DATE_GET_FIRST_LAST'
EXPORTING
i_date = g_forecast_sub_wa-gi_date
i_perkz = 'W'
I_FABKL = ' '
I_PERIV = ' '
IMPORTING
i_firstdate = l_start_date
i_lastdate = l_end_date
.
Starting Timestamp
CONVERT DATE l_start_date INTO TIME STAMP l_period_start
TIME ZONE 'UTC '.
g_time_series_item_wa-period_begin = l_period_start.
Ending Timestamp
CONVERT DATE l_end_date TIME '235959' INTO TIME STAMP l_period_end
TIME ZONE 'UTC '.
g_time_series_item_wa-period_end = l_period_end.
Forecast Value
g_time_series_item_wa-time_series_value =
g_forecast_sub_wa-forecast_cv.
APPEND g_time_series_item_wa TO t_time_series_item.
CLEAR g_time_series_item_wa.
ENDLOOP.
Call FM BAPI_PBSRVAPS_CHANGEKEYFIGVAL to change Z_CUSFU for all
time buckets
REFRESH: t_return,
t_char_comb.
*MOD-006
SORT t_time_series_item by TIME_SERIES_ID
period_begin
period_end.
delete adjacent duplicates from t_time_series_item
comparing
TIME_SERIES_ID
PERIOD_BEGIN
PERIOD_END.
*MOD-006
CALL FUNCTION 'BAPI_PBSRVAPS_CHANGEKEYFIGVAL'
EXPORTING
planningbook = c_planningbook
commit_control = 'E'
TABLES
selection = t_selection
time_series = t_time_series_chg
time_series_item = t_time_series_item
characteristics_combination = t_char_comb
return = t_return.
If error in return table, store message in error table and attempt
rollback; otherwise commit
IF NOT t_return[] IS INITIAL.
CALL FUNCTION 'BAPI_TRANSACTION_ROLLBACK'
IMPORTING
RETURN =
.
LOOP AT t_forecast_sub1 INTO g_forecast_sub_wa.
*MOD-010
MOVE-CORRESPONDING g_forecast_sub_wa TO g_error_data_wa.
g_error_data_wa-error_type = c_change_error.
APPEND g_error_data_wa TO t_error_data.
CLEAR g_error_data_wa.
DELETE t_forecast_sub1.
CLEAR g_forecast_sub_wa.
ADD 1 TO g_cnt_error_recs.
CONTINUE.
ENDLOOP.
ELSE.
CALL FUNCTION 'BAPI_TRANSACTION_COMMIT' .
DESCRIBE TABLE t_forecast_sub1 LINES l_lines.
g_cnt_changes = g_cnt_changes + l_lines.
ENDIF.
************
*MOD-011
************
ENDFORM. " sub_call_bapi
&----
*& Form sub_format_dates
&----
text
----
-->P_L_HOLD_WA_DATE text
-->P_L_CALWEEK text
-->P_L_FISCPER text
-->P_L_CALMONTH text
----
FORM sub_format_dates USING p_date
p_calweek
p_calweek_end
p_weekend_date
p_week_diff
p_fiscper
p_calmonth
p_calyear_start
p_calyear_end
p_start_week
p_start_week_day
p_end_week
p_end_week_day.
Local variables
DATA: l_curr_date TYPE sydatum, " Program Run Date
l_record_date TYPE sydatum, " Formatted Record Date
l_curr_year(2) TYPE c, " Program Year (without century)
l_curr_year_num TYPE i, " Integer value of Program Year
l_century(2) TYPE c, " Program Century (without year)
l_century_num TYPE i, " Integer value of Century
l_input_year_num TYPE i. " Integer value of Record Year
DATA: con_period_week type /SAPAPO/PERKZ value 'W'.
Record date - format MM/DD/YY
DATA: BEGIN OF l_input_date,
month(2) TYPE c,
filler1(1) TYPE c,
day(2) TYPE c,
filler2(1) TYPE c,
year(2) TYPE c,
END OF l_input_date.
DATA: l_calweek TYPE kweek,
l_fiscper TYPE /sapapo/perid.
*MOD-006
data: l_calmonth(6) type c,
l_calyear(6) type c,
l_month(2) type c,
l_year(4) type c,
l_perid type /SAPAPO/PERID.
DATA: p_calweek_start like scal-week.
DATA: F_DATE TYPE D,
L_FROMDATE LIKE SCAL-DATE,
L_TODATE LIKE SCAL-DATE.
DATA: lv_year_start(4) type c,
lv_week_start(2) type c,
lv_week_end(2) type c.
DATA: lv_calweek_start type scal-week.
DATA: lv_start_week_day type scal-date.
DATA: lv_end_week_day type scal-date.
DATA: lv_length type i.
clear: l_perid.
**MOD-006
Data for forecast run date.
l_record_date = sy-datum.
Use /SAPAPO/DATE_GET_WEEK, /SAPAPO/PERIOD_GET_FOR_DATE (P/JJ)... but
must convert to DATS format first
CALL FUNCTION '/SAPAPO/DATE_GET_WEEK'
EXPORTING
date = l_record_date
IMPORTING
week = l_calweek
EXCEPTIONS
date_invalid = 1
OTHERS = 2.
IF sy-subrc <> 0.
g_flg_error = 'X'.
EXIT.
ELSE.
p_calweek(2) = l_calweek+4(2).
p_calweek+2(4) = l_calweek(4).
ENDIF.
lv_year_start = p_calweek+2(4).
lv_week_start = p_calweek+0(2).
lv_week_end = lv_week_start + 1.
concatenate lv_year_start lv_week_end into p_calweek_start.
CALL FUNCTION 'WEEK_GET_FIRST_DAY'
EXPORTING
WEEK = p_calweek_start
IMPORTING
DATE = p_start_week_day
EXCEPTIONS
WEEK_INVALID = 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.
p_end_week_day = p_start_week_day + p_horz.
l_record_date = p_end_week_day.
CLEAR: l_calweek.
CALL FUNCTION '/SAPAPO/DATE_GET_WEEK'
EXPORTING
date = l_record_date
IMPORTING
week = l_calweek
EXCEPTIONS
date_invalid = 1
OTHERS = 2.
IF sy-subrc <> 0.
g_flg_error = 'X'.
EXIT.
ELSE.
p_calweek_end = l_calweek.
p_calyear_end = l_calweek+2(4).
ENDIF.
CLEAR: P_END_WEEK_DAY.
CALL FUNCTION 'WEEK_GET_FIRST_DAY'
EXPORTING
WEEK = p_calweek_end
IMPORTING
DATE = p_end_week_day
EXCEPTIONS
WEEK_INVALID = 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.
Clear: lv_week_start, lv_week_end.
*MOD-006
lv_week_start = p_calweek_start+4(2).
lv_year_start = p_calweek_start+0(4).
concatenate lv_week_start lv_year_start into p_start_week.
p_calyear_start = lv_year_start.
lv_week_start = p_calweek_end+4(2).
lv_year_start = p_calweek_end+0(4).
concatenate lv_week_start lv_year_start into p_end_week.
*MOD-006
p_week_diff = p_calweek_end0(2) - p_calweek0(2).
condense p_week_diff.
p_week_diff = ceil( p_week_diff ).
*MOD-006
*Data for availability date.
l_record_date = p_date.
Use /SAPAPO/DATE_GET_WEEK, /SAPAPO/PERIOD_GET_FOR_DATE (P/JJ)... but
must convert to DATS format first
CALL FUNCTION '/SAPAPO/DATE_GET_WEEK'
EXPORTING
date = l_record_date
IMPORTING
week = l_calweek
EXCEPTIONS
date_invalid = 1
OTHERS = 2.
IF sy-subrc <> 0.
g_flg_error = 'X'.
EXIT.
ELSE.
p_calweek(2) = l_calweek+4(2).
p_calweek+2(4) = l_calweek(4).
ENDIF.
lv_year_start = p_calweek+2(4).
lv_week_start = p_calweek+0(2).
lv_week_end = lv_week_start + 1.
lv_length = strlen( lv_week_end ).
if lv_length < 2.
concatenate '0' lv_week_end into lv_week_end.
endif.
CONSTANTS: C_52(2) TYPE C VALUE '52'.
concatenate lv_year_start lv_week_end into lv_calweek_start.
if lv_calweek_start+4(2) > c_52.
lv_calweek_start+4(2) = c_52.
endif.
CALL FUNCTION 'WEEK_GET_FIRST_DAY'
EXPORTING
WEEK = lv_calweek_start
IMPORTING
DATE = lv_start_week_day
EXCEPTIONS
WEEK_INVALID = 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.
lv_end_week_day = lv_start_week_day + p_horz.
l_record_date = lv_end_week_day.
CLEAR: l_calweek.
CALL FUNCTION '/SAPAPO/DATE_GET_WEEK'
EXPORTING
date = l_record_date
IMPORTING
week = l_calweek
EXCEPTIONS
date_invalid = 1
OTHERS = 2.
IF sy-subrc <> 0.
g_flg_error = 'X'.
EXIT.
ELSE.
p_calweek_end = l_calweek.
ENDIF.
lv_year_start = p_calweek_end+0(4).
lv_week_start = p_calweek_end+4(2).
concatenate lv_week_start lv_year_start into p_calweek_end.
p_calyear_end = lv_year_start.
*MOD-008
clear: l_perid.
l_record_date = p_date.
call function '/SAPAPO/PERIOD_GET_FOR_DATE'
EXPORTING
i_date = l_record_date
i_perkz = 'M'
I_PERIV = 'K0'
IMPORTING
E_PERID = l_perid
EXCEPTIONS
WRONG_PERKZ = 1
WRONG_DATE = 2
WRONG_PERIV = 3
OTHERS = 4.
if sy-subrc <> 0.
g_flg_error = 'X'.
EXIT.
ELSE.
p_calmonth(2) = l_perid+4(2).
p_calmonth+2(4) = l_perid(4).
p_calyear_start = l_perid+0(4).
endif.
*MOD-008
ENDFORM. " sub_format_dates
&----
*& Form validate_locmap
&----
text
----
--> p1 text
<-- p2 text
----
FORM validate_locmap .
*MOD-011
Convert SAP Location Code to APO Location
if not t_forecast_data[] is initial.
READ TABLE t_forecast_data into g_forecast_data_wa index 1.
SELECT SINGLE locno FROM /sapapo/locmap
INTO g_forecast_data_wa-r3_loc
WHERE ext_locno = p_dist
AND loctype = c_loctype
AND LOGQS = c_logqs.
IF sy-subrc <> 0.
MOVE-CORRESPONDING g_forecast_data_wa TO g_error_data_wa.
g_error_data_wa-error_type = c_r3_loc_error.
APPEND g_error_data_wa TO t_error_data.
CLEAR g_error_data_wa.
CLEAR g_forecast_data_wa.
ADD 1 TO g_cnt_error_recs.
ENDIF.
ENDIF.
*MOD-011
ENDFORM. " validate_locmap
‎2008 Feb 04 8:48 AM
Hi,
There are 2 things i would like to share with you...
The Bapi's used in the code i guess are obsolete now and BAPI_PBSRVAPS_GETDETAIL2 is the current one. similarly for CHANGE_KEY_FIGVAL2 as well.
That apart, even if you are using the older one, it should still be fetching the values that you need. One thing I observe from the code is (could not go thru the entire code) you are using a READ statement to extract you key id. I could not find the place where you are actually coming at that read statement again to read your second value (maybe i missed it as the code was huge on long).
Things you could do to solve the problem...
READ at the key_figure_item table. With the key figure id you get read the Key figure table. This way you can be sure that you do not miss any of the key figures.
P.S. Another good practice is to sort the internal table based on the key field and READ that table using BINARY SEARCH, this will also make the read faster.
Thanks,
Mani