Registration

Dear SAP Community Member,
In order to fully benefit from what the SAP Community has to offer, please register at:
http://scn.sap.com
Thank you,
The SAP Community team.
Skip to end of metadata
Go to start of metadata

REPORT zapofcast MESSAGE-ID zfpv LINE-SIZE 132.************************************************************************

* PROGRAM....... ZAPOFCAST* 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 moduleDATA:

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_GETDETAILDATA:

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_CHANGEKEYFIGVALDATA:

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_CHANGEKEYFIGVALDATA:

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_CHANGEKEYFIGVALDATA

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_code(7) 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(7) 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(7) 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(7) 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.

DATA: ilines TYPE STANDARD TABLE OF string 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 TYPE string, " 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

*data: i_file type ref to cl_gui_frontend_services.

* Internal tables *

DATA: BEGIN OF i_file OCCURS 0,

field(3000) TYPE c,

END OF i_file.

*DATA: i_file TYPE REF TO CL_WD_FILE_UPLOAD.

*----------------------------------------------------------------------*

* Objects

*----------------------------------------------------------------------*

*----------------------------------------------------------------------*

* Constants

*----------------------------------------------------------------------*

CONSTANTS: c_planningbook TYPE /sapapo/pb_mview " Planning Book

* VALUE 'Z_PPC_FCST_DP',

VALUE '9ASNP94',

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'.

c_loctype TYPE /sapapo/c_loctype VALUE '1001'.

" 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 TYPE string.

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 LEFT-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.

***************************************************************

*MOD-004

***************************************************************

* 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.

*************************************************************

*MOD-004

*************************************************************

* 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.

*************************************************************

*MOD-004

*************************************************************

** 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-004

***************************************************************

****************************************************************

**MOD-004

****************************************************************

*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.

*

* CREATE OBJECT i_file.

* call method i_file->gui_upload

* EXPORTING

* filename = p_phyin

* filetype = 'ASC'

* CHANGING

* data_tab = ilines[]

* EXCEPTIONS

* FILE_OPEN_ERROR = 1

* FILE_READ_ERROR = 2

* NO_BATCH = 3

* GUI_REFUSE_FILETRANSFER = 4

* INVALID_TYPE = 5

* NO_AUTHORITY = 6

* UNKNOWN_ERROR = 7

* BAD_DATA_FORMAT = 8

* HEADER_NOT_ALLOWED = 9

* SEPARATOR_NOT_ALLOWED = 10

* HEADER_TOO_LONG = 11

* UNKNOWN_DP_ERROR = 12

* ACCESS_DENIED = 13

* DP_OUT_OF_MEMORY = 14

* DISK_FULL = 15

* DP_TIMEOUT = 16

* OTHERS = 17.

* CALL FUNCTION 'GUI_UPLOAD'

* EXPORTING

* filename = p_phyin

* filetype = 'ASC'

* TABLES

* data_tab = ilines[]

* EXCEPTIONS

* file_open_error = 1

* file_read_error = 2

* no_batch = 3

* gui_refuse_filetransfer = 4

* invalid_type = 5

* no_authority = 6

* unknown_error = 7

* bad_data_format = 8

* header_not_allowed = 9

* separator_not_allowed = 10

* header_too_long = 11

* unknown_dp_error = 12

* access_denied = 13

* dp_out_of_memory = 14

* disk_full = 15

* dp_timeout = 16

* OTHERS = 17.

* CALL FUNCTION 'GUI_UPLOAD'

* EXPORTING

* FILENAME = p_phyin

* HAS_FIELD_SEPARATOR = 'X'

* TABLES

* DATA_TAB = I_FILE

* EXCEPTIONS

* FILE_OPEN_ERROR = 1

* FILE_READ_ERROR = 2

* NO_BATCH = 3

* GUI_REFUSE_FILETRANSFER = 4

* INVALID_TYPE = 5

* NO_AUTHORITY = 6

* UNKNOWN_ERROR = 7

* BAD_DATA_FORMAT = 8

* HEADER_NOT_ALLOWED = 9

* SEPARATOR_NOT_ALLOWED = 10

* HEADER_TOO_LONG = 11

* UNKNOWN_DP_ERROR = 12

* ACCESS_DENIED = 13

* DP_OUT_OF_MEMORY = 14

* DISK_FULL = 15

* DP_TIMEOUT = 16

* OTHERS = 17.

*

* IF SY-SUBRC <> 0.

*

* MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO

* WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.

* endif.

** Check authority for reading the interface file.

* CALL METHOD g_file->check_read_authority.

*

** Attempt to open file

* CALL METHOD g_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.

OPEN DATASET p_phyin FOR INPUT IN TEXT MODE ENCODING DEFAULT.

" message v_msg.

IF sy-subrc = 0.

REFRESH : g_input_data.

DO.

READ DATASET p_phyin INTO g_input_data.

IF sy-subrc NE 0.

IF g_cnt_input_recs = 0.

MESSAGE s000 WITH 'Input file 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.

ELSE.

MESSAGE e000 WITH 'File cannot be opened'.

ENDIF.

CLOSE DATASET p_phyin.

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.

******************************************

*MOD-005

******************************************

* l_matnr = g_forecast_sub_wa-r3_material.

* l_locno = g_forecast_sub_wa-r3_loc.

******************************************

*MOD-005

******************************************

l_matnr = g_forecast_sub_wa-pres_code.

l_locno = g_forecast_sub_wa-site.

**********************************************************

** 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.

* g_selection_wa-characteristic_name = 'ZMATNR'. " APO-DP Product

g_selection_wa-characteristic_name = '9AMATNR'. " APO-DP Product

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.

g_selection_wa-characteristic_name = '9ALOCNO'. " APO-DP Product

g_selection_wa-char_val_sign = 'I'.

g_selection_wa-char_val_option = 'EQ'.

g_selection_wa-char_val_low = l_locno.

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'.

g_group_by_wa-characteristic_name = '9AMATNR'.

*** 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'.

g_key_figure_selection_wa-key_figure = '9AATDZU'.

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.

*

l_record_date = sy-datum.

CALL FUNCTION '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_tbid = '9ASNP'

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.

CALL FUNCTION '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.

CALL FUNCTION '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_end+0(2) - p_calweek+0(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.

CALL FUNCTION '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.

CALL FUNCTION '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

  • No labels