Tuesday, May 6, 2008

Sample ABAP Program to Implement EDI

REPORT YLMM015A

MESSAGE-ID YL.

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

*

* EDI FORECASTING INTERFACE - SHEILA TITCHENER JAN 1998

* L_IDOC_HEADER_CREATE, L_IDOC_SEGMENT_CREATE & L_IDOC_SEND

* left idoc ready to send but did not send automatically.

* these were replaced by ALE_MODEL_DETERMINE_IF_TO_SEND

* ALE_MODEL_INFO_GET & MASTER_IDOC_DISTRIBUTE

* 'in update task'. This solved the problem. Records are set up in table

* t_edidd.

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

* TABLES - Database *

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

TABLES: EORD,

MARC,

MARM,

EINA,

PLAF,

EBAN,

EDIDD.

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

* DATA - Work Fields

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

DATA: W_START_MONTH LIKE SY-DATUM.

DATA: W_NEXT_MONTH LIKE SY-DATUM.

DATA: W_THIRD_MONTH LIKE SY-DATUM.

DATA: W_END_DATE LIKE SY-DATUM.

DATA: W_COMP_DATE LIKE SY-DATUM.

DATA: W_BEG_DATE LIKE SY-DATUM.

DATA: W_DESP_DATE LIKE SY-DATUM.

DATA: W_DAY LIKE HRVSCHED-DAYNR.

DATA: W_DAY_TXT LIKE HRVSCHED-DAYTXT.

DATA: W_INDEX LIKE SY-TABIX.

DATA: W_TOTAL LIKE EBAN-MENGE.

DATA: W_ITEM_NUMBER TYPE I.

* IDOC DATA

DATA: W_E1EDK01_DATA LIKE E1EDK01.

DATA: W_E1EDK03_DATA LIKE E1EDK03.

DATA: W_E1EDKA1_DATA LIKE E1EDKA1.

DATA: W_E1EDP01_DATA LIKE E1EDP01.

DATA: W_E1EDP20_DATA LIKE E1EDP20.

DATA: W_E1EDP19_DATA LIKE E1EDP19.

* DATA - INTERNAL TABLES

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

* forecast buckets - 8/9 weekly for first 2 months then 10 monthly

DATA: BEGIN OF FORECAST OCCURS 19,

DATE TYPE D, "start date of bucket

WEEK_MON, "weekly or monthly indicator

QTY LIKE EBAN-MENGE. "accumulated qty

DATA: END OF FORECAST.

*DATA: PRODUCTS LIKE EORD OCCURS 10 WITH HEADER LINE.

DATA: BEGIN OF PRODUCTS OCCURS 10,

MATNR LIKE EORD-MATNR,

WERKS LIKE EORD-WERKS,

LIFNR LIKE EORD-LIFNR.

DATA: END OF PRODUCTS.

* planned orders table PLAF

DATA: BEGIN OF T_PLAF OCCURS 10,

GSMNG LIKE PLAF-GSMNG,

PEDTR LIKE PLAF-PEDTR.

DATA: END OF T_PLAF.

* planned orders table EBAN

DATA: BEGIN OF T_EBAN OCCURS 10,

MENGE LIKE EBAN-MENGE,

LFDAT LIKE EBAN-LFDAT.

DATA: END OF T_EBAN.

* IDOC _SEND parameter

DATA: COMM_IDOC_CONTROL LIKE EDIDC OCCURS 1 WITH HEADER LINE.

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

* new fields for new way of sending IDOC



DATA: W_EDIDC LIKE EDIDC OCCURS 5 WITH HEADER LINE,

L_EDIDC LIKE EDIDC,

L_SEND_FLAG,

W_SDATA LIKE EDIDD-SDATA.

DATA: T_BDI_MODEL LIKE BDI_MODEL OCCURS 0 WITH HEADER LINE.

DATA: T_EDIDC LIKE EDIDC OCCURS 0 WITH HEADER LINE.

DATA: T_EDIDD LIKE EDIDD OCCURS 0 WITH HEADER LINE.

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











* OUTPUT file layout

DATA: BEGIN OF OUTREC,

MATNR LIKE EBAN-MATNR,

D1 VALUE '$',

IDNLF LIKE EINA-IDNLF,

D2 VALUE '$',

WEEK_MON,

D3 VALUE '$',

PERIOD LIKE SY-DATUM,

D4 VALUE '$',

QTY(8),

ENDOFLINE TYPE X VALUE '0D',

END OF OUTREC.

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

* SELECT-OPTIONS

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



SELECT-OPTIONS P_LIFNR FOR EORD-LIFNR.



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

* PARAMETERS *

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

PARAMETERS: P_PART LIKE EDIDC-RCVPRN DEFAULT 'EDIFCAST',

P_FILE(30) DEFAULT '/vmedata/XXX/mdafcstddmmyy' LOWER CASE.





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

* INITIALIZATION.

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

INITIALIZATION.

* move hoot name to output file name

P_FILE+9(3) = SY-SYSID.

* move todays date to output file name

P_FILE+20(2) = SY-DATUM+6(2).

P_FILE+22(2) = SY-DATUM+4(2).

P_FILE+24(2) = SY-DATUM+2(2).

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

START-OF-SELECTION.

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

* OPEN $ delimited file

OPEN DATASET P_FILE FOR OUTPUT IN TEXT MODE.



* 4.1 create header idoc

* REPLACE function with alternative

* CALL FUNCTION 'L_IDOC_HEADER_CREATE'

* EXPORTING

* I_MESTYP = 'ORDERS'

* I_MESCOD = ' '

* I_IDOCTP = 'ORDERS01'

* I_RCVPRN = P_PART

* EXCEPTIONS

* OTHERS = 1.

*- Call function module to determine if message is to be distributed



CALL FUNCTION 'ALE_MODEL_DETERMINE_IF_TO_SEND'

EXPORTING

MESSAGE_TYPE = 'ORDERS'

IMPORTING

IDOC_MUST_BE_SENT = L_SEND_FLAG

EXCEPTIONS

OWN_SYSTEM_NOT_DEFINED = 1

OTHERS = 2.



*- Determine recipient systems



CALL FUNCTION 'ALE_MODEL_INFO_GET'

EXPORTING

MESSAGE_TYPE = 'ORDERS'

* RECEIVING_SYSTEM = ' '

* SENDING_SYSTEM = ' '

* VALIDDATE = SY-DATUM

TABLES

MODEL_DATA = T_BDI_MODEL

EXCEPTIONS

NO_MODEL_INFO_FOUND = 1

OWN_SYSTEM_NOT_DEFINED = 2

OTHERS = 3.





* 4.2 - 4.5 create idoc segments



PERFORM IDOC_CREATE.



* 4.6 Set up forecast bucket dates



* determine start of the next forecast month

W_START_MONTH = SY-DATUM.

* 12 months ago

*_LAST_12_MTH = SY-DATUM - 365.



IF W_START_MONTH+4(2) = '12'.

W_START_MONTH+4(2) = '01'.

W_START_MONTH(4) = W_START_MONTH(4) + '0001'.

ELSE.

W_START_MONTH+4(2) = W_START_MONTH+4(2) + '01'.

ENDIF.

W_START_MONTH+6 = '01'.

CALL FUNCTION 'RH_GET_DATE_DAYNAME'

EXPORTING

LANGU = 'E'

DATE = W_START_MONTH

* CHECK =

IMPORTING

DAYNR = W_DAY

DAYTXT = W_DAY_TXT

EXCEPTIONS

NO_LANGU = 1

NO_DATE = 2

NO_DAYTXT_FOR_LANGU = 3

INVALID_DATE = 4

OTHERS = 5.

CASE W_DAY.

WHEN 1.

WHEN 2.

W_START_MONTH = W_START_MONTH - 1.

WHEN 3.

W_START_MONTH = W_START_MONTH - 2.

WHEN 4.

W_START_MONTH = W_START_MONTH + 4.

WHEN 5.

W_START_MONTH = W_START_MONTH + 3.

WHEN 6.

W_START_MONTH = W_START_MONTH + 2.

WHEN 7.

W_START_MONTH = W_START_MONTH + 1.

ENDCASE.

* date of start of monthly forecasting

W_THIRD_MONTH = W_START_MONTH.



IF W_START_MONTH+4(2) < '11'.

W_THIRD_MONTH+4(2) = W_THIRD_MONTH+4(2) + '02'.

ELSE.

W_THIRD_MONTH+4(2) = W_THIRD_MONTH+4(2) - '10'.

W_THIRD_MONTH(4) = W_THIRD_MONTH(4) + '0001'.

ENDIF.

* day will be the first

W_THIRD_MONTH+6 = '01'.

* find nearest monday - removed

*CALL FUNCTION 'RH_GET_DATE_DAYNAME'

* EXPORTING

* LANGU = 'E'

* DATE = W_THIRD_MONTH

** CHECK =

* IMPORTING

* DAYNR = W_DAY

* DAYTXT = W_DAY_TXT

* EXCEPTIONS

* NO_LANGU = 1

* NO_DATE = 2

* NO_DAYTXT_FOR_LANGU = 3

* INVALID_DATE = 4

* OTHERS = 5.

*CASE W_DAY.

* WHEN 1.

* WHEN 2.

* W_THIRD_MONTH = W_THIRD_MONTH - 1.

* WHEN 3.

* W_THIRD_MONTH = W_THIRD_MONTH - 2.

* WHEN 4.

* W_THIRD_MONTH = W_THIRD_MONTH + 4.

* WHEN 5.

* W_THIRD_MONTH = W_THIRD_MONTH + 3.

* WHEN 6.

* W_THIRD_MONTH = W_THIRD_MONTH + 2.

* WHEN 7.

* W_THIRD_MONTH = W_THIRD_MONTH + 1.

*ENDCASE.

* set up all dates in table



DO.

IF SY-INDEX = 1.

FORECAST-DATE = W_START_MONTH.

W_NEXT_MONTH = W_START_MONTH.

ELSE.

* if this takes us into the third month set to monthly

IF W_NEXT_MONTH GE W_THIRD_MONTH.

IF W_NEXT_MONTH+4(2) = '12'. " add 1 month

W_NEXT_MONTH+4(2) = '01'.

W_NEXT_MONTH(4) = W_NEXT_MONTH(4) + '0001'.

ELSE.

W_NEXT_MONTH+4(2) = W_NEXT_MONTH+4(2) + '01'.

ENDIF.

* W_NEXT_MONTH = W_NEXT_MONTH + 28.

* IF W_NEXT_MONTH+4(2) = FORECAST-DATE+4(2).

* W_NEXT_MONTH = W_NEXT_MONTH + 7.

* ENDIF.

ELSE.

* add 1 week

W_NEXT_MONTH = W_NEXT_MONTH + 7.

ENDIF.

FORECAST-DATE = W_NEXT_MONTH.

ENDIF.



IF FORECAST-DATE GE W_THIRD_MONTH.



FORECAST-DATE+6(2) = '01'. "set start day to 1

FORECAST-WEEK_MON = '2'.

ELSE.

FORECAST-WEEK_MON = '1'.

ENDIF.

* year complete?

IF FORECAST-DATE+4(2) = W_START_MONTH+4(2)

AND FORECAST-DATE(4) > W_START_MONTH(4).

W_END_DATE = FORECAST-DATE.

EXIT.

ENDIF.



APPEND FORECAST.



ENDDO.

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

* Start of selection processing

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

* 4.7 select records from Purchasing Source list

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

SELECT MATNR WERKS LIFNR FROM EORD INTO TABLE PRODUCTS

WHERE LIFNR IN P_LIFNR

AND FLIFN = 'X'

AND VDATU LE SY-DATUM

AND BDATU GE SY-DATUM.

* check within selection range

* CHECK P_LIFNR.

* add to table

* APPEND PRODUCTS.

*ENDSELECT.

* delete duplicates products

* table already in product code sequence

DELETE ADJACENT DUPLICATES FROM PRODUCTS COMPARING MATNR.

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

* process selected vendors materials

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

LOOP AT PRODUCTS.

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

* 4.7 access material master

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

SELECT SINGLE * FROM MARC

WHERE MATNR = PRODUCTS-MATNR

AND WERKS = PRODUCTS-WERKS .



CHECK MARC-DISMM = 'PD' OR MARC-DISMM = 'P3'

OR MARC-DISMM = 'ZD' OR MARC-DISMM = 'Z3'.



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

* 4.8 access purchasing info record

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

SELECT SINGLE * FROM EINA

WHERE MATNR = PRODUCTS-MATNR

AND LIFNR = PRODUCTS-LIFNR.

* record found?

CHECK SY-SUBRC = 0.

* vendor's material number begins with 45?

CHECK EINA-IDNLF(2) = '45'.

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

* 4.10 clear forecast quantities

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

LOOP AT FORECAST.

FORECAST-QTY = 0.

MODIFY FORECAST TRANSPORTING QTY.

ENDLOOP.

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

* 4.10

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

* calculate start & end dates

W_COMP_DATE = W_END_DATE + 9.

* W_BEG_DATE = W_START_MONTH + MARC-WEBAZ + 9.

* read all matching material records from planned orders table PLAF

SELECT GSMNG PEDTR FROM PLAF INTO CORRESPONDING FIELDS OF TABLE T_PLAF

WHERE MATNR = PRODUCTS-MATNR

AND PEDTR < W_COMP_DATE.

* AND PEDTR > W_BEG_DATE.

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

* 4.11 process any records found

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

IF SY-SUBRC = 0.

LOOP AT T_PLAF.

W_DESP_DATE = T_PLAF-PEDTR - 9.

* IF date is < first date add to first date

READ TABLE FORECAST INDEX 1.

IF FORECAST-DATE GE W_DESP_DATE.

W_INDEX = 1.

ELSE.

* find first slot where date > despatch date.

LOOP AT FORECAST

WHERE DATE GE W_DESP_DATE.

W_INDEX = SY-TABIX.

EXIT.

ENDLOOP.

* read previous entry.

W_INDEX = W_INDEX - 1.

READ TABLE FORECAST INDEX W_INDEX.

ENDIF.

* convert to purchasing unit of measure

IF EINA-UMREZ NE 0.

T_PLAF-GSMNG = T_PLAF-GSMNG * EINA-UMREN / EINA-UMREZ.

ENDIF.

*

FORECAST-QTY = FORECAST-QTY + T_PLAF-GSMNG.

MODIFY FORECAST INDEX W_INDEX.

ENDLOOP.

ENDIF.



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

* 4.12 read all matching material records from planned orders table EBAN

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

SELECT MENGE LFDAT FROM EBAN INTO CORRESPONDING FIELDS OF TABLE T_EBAN

WHERE MATNR = PRODUCTS-MATNR

AND LFDAT < W_COMP_DATE.

* AND LFDAT > W_BEG_DATE.

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

* 4.13 process any records found

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

IF SY-SUBRC = 0.

LOOP AT T_EBAN.

W_DESP_DATE = T_EBAN-LFDAT - 9.

* IF date is < first date add to first date

READ TABLE FORECAST INDEX 1.

IF FORECAST-DATE GE W_DESP_DATE.

W_INDEX = 1.

ELSE.

* find first slot where date > despatch date.

LOOP AT FORECAST

WHERE DATE GE W_DESP_DATE.

W_INDEX = SY-TABIX.

EXIT.

ENDLOOP.

* read previous entry.

W_INDEX = W_INDEX - 1.

READ TABLE FORECAST INDEX W_INDEX.

ENDIF.

* convert to purchasing unit of measure

IF EINA-UMREZ NE 0.

T_EBAN-MENGE = T_EBAN-MENGE * EINA-UMREN / EINA-UMREZ.

ENDIF.

* add to table

FORECAST-QTY = FORECAST-QTY + T_EBAN-MENGE.

MODIFY FORECAST INDEX W_INDEX.

ENDLOOP.

ENDIF.

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

* 4.14 total all forecast buckets

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

W_TOTAL = 0.

LOOP AT FORECAST.

W_TOTAL = W_TOTAL + FORECAST-QTY.

ENDLOOP.

CHECK W_TOTAL NE 0.

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

* create idocs for material forecast

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

W_ITEM_NUMBER = W_ITEM_NUMBER + 1.

IF W_ITEM_NUMBER > 80.

W_ITEM_NUMBER = 1.

PERFORM IDOC_CREATE.

ENDIF.

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

* 4.14.3 create idoc E1EDP01

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

W_E1EDP01_DATA-POSEX = W_ITEM_NUMBER.

WRITE W_TOTAL TO W_E1EDP01_DATA-MENGE DECIMALS 0.

* EDIDD-SDATA = W_E1EDP01_DATA.

T_EDIDD-SDATA = W_E1EDP01_DATA.

W_SDATA = W_E1EDP01_DATA. "????

T_EDIDD-SEGNAM = 'E1EDP01'.

APPEND T_EDIDD.

* CALL FUNCTION 'L_IDOC_SEGMENT_CREATE'

* EXPORTING

* I_SEGNAM = 'E1EDP01'

* I_SDATA = EDIDD-SDATA

* EXCEPTIONS

* OTHERS = 1.



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

* 4.14.4 create idoc E1EDP20

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

LOOP AT FORECAST.

IF FORECAST-QTY NE 0.

WRITE FORECAST-QTY TO W_E1EDP20_DATA-WMENG DECIMALS 0.

W_E1EDP20_DATA-AMENG = FORECAST-WEEK_MON.

W_E1EDP20_DATA-EDATU = FORECAST-DATE.

* EDIDD-SDATA = W_E1EDP20_DATA.

T_EDIDD-SDATA = W_E1EDP20_DATA.

W_SDATA = W_E1EDP20_DATA. "????

T_EDIDD-SEGNAM = 'E1EDP20'.

APPEND T_EDIDD.

* CALL FUNCTION 'L_IDOC_SEGMENT_CREATE'

* EXPORTING

* I_SEGNAM = 'E1EDP20'

* I_SDATA = EDIDD-SDATA

* EXCEPTIONS

* OTHERS = 1.

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

* write record to $ delimited file

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

OUTREC-MATNR = PRODUCTS-MATNR.

OUTREC-IDNLF = EINA-IDNLF.

OUTREC-WEEK_MON = FORECAST-WEEK_MON.

OUTREC-PERIOD = FORECAST-DATE.

WRITE FORECAST-QTY TO OUTREC-QTY DECIMALS 0.

TRANSFER OUTREC TO P_FILE.





ENDIF.

ENDLOOP.

* 4.14.5 End of this product

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



W_E1EDP19_DATA-IDTNR = EINA-IDNLF.

* EDIDD-SDATA = W_E1EDP19_DATA.

T_EDIDD-SDATA = W_E1EDP19_DATA.

W_SDATA = W_E1EDP19_DATA. "????

T_EDIDD-SEGNAM = 'E1EDP19'.

APPEND T_EDIDD.

* CALL FUNCTION 'L_IDOC_SEGMENT_CREATE'

* EXPORTING

* I_SEGNAM = 'E1EDP19'

* I_SDATA = EDIDD-SDATA

* EXCEPTIONS

* OTHERS = 1.

ENDLOOP.

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

* end of all products

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

* REPLACE WITH MASTER_IDOC_DISTRIBUTE

* CALL FUNCTION 'L_IDOC_SEND'

* TABLES

* T_COMM_IDOC = COMM_IDOC_CONTROL

* EXCEPTIONS

* ERROR_DISTRIBUTE_IDOC = 1

* OTHERS = 2.



READ TABLE T_BDI_MODEL INDEX 1. " maximum 1 recipient

MOVE 'ORDERS' TO L_EDIDC-MESTYP.

MOVE 'ORDERS01' TO L_EDIDC-IDOCTP.

MOVE 'LS' TO L_EDIDC-RCVPRT.

* MOVE T_BDI_MODEL-RCVSYSTEM TO L_EDIDC-RCVPRN.

* partner profile parameter

MOVE P_PART TO L_EDIDC-RCVPRN.



*- Distribute the iDoc



CALL FUNCTION 'MASTER_IDOC_DISTRIBUTE' IN UPDATE TASK

EXPORTING

MASTER_IDOC_CONTROL = L_EDIDC

TABLES

COMMUNICATION_IDOC_CONTROL = COMM_IDOC_CONTROL

MASTER_IDOC_DATA = T_EDIDD

EXCEPTIONS

ERROR_IN_IDOC_CONTROL = 01

ERROR_WRITING_IDOC_STATUS = 02

ERROR_IN_IDOC_DATA = 03

SENDING_LOGICAL_SYSTEM_UNKNOWN = 04.



COMMIT WORK.



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

END-OF-SELECTION.



CLOSE DATASET P_FILE.



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

FORM IDOC_CREATE.

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



* 4.2 Create idoc segment E1EDK01

W_E1EDK01_DATA-BELNR = 'EDI FORECAST'.

* EDIDD-SDATA = W_E1EDK01_DATA.

T_EDIDD-SDATA = W_E1EDK01_DATA.

W_SDATA = W_E1EDK01_DATA. "????

T_EDIDD-SEGNAM = 'E1EDK01'.

APPEND T_EDIDD.

* CALL FUNCTION 'L_IDOC_SEGMENT_CREATE'

* EXPORTING

* I_SEGNAM = 'E1EDK01'

* I_SDATA = EDIDD-SDATA

* EXCEPTIONS

* OTHERS = 1.

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

* 4.3 Create idoc segments E1EDK03

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

W_E1EDK03_DATA-IDDAT = '002'.

W_E1EDK03_DATA-DATUM = SY-DATUM.

* EDIDD-SDATA = W_E1EDK03_DATA.

T_EDIDD-SDATA = W_E1EDK03_DATA.

W_SDATA = W_E1EDK03_DATA. "????

T_EDIDD-SEGNAM = 'E1EDK03'.

APPEND T_EDIDD.

* CALL FUNCTION 'L_IDOC_SEGMENT_CREATE'

* EXPORTING

* I_SEGNAM = 'E1EDK03'

* I_SDATA = EDIDD-SDATA

* EXCEPTIONS

* OTHERS = 1.

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

* 4.4 Create idco E1EDKA1

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

W_E1EDKA1_DATA-PARVW = 'LI'.

W_E1EDKA1_DATA-PARTN = P_PART.

* EDIDD-SDATA = W_E1EDKA1_DATA.

T_EDIDD-SDATA = W_E1EDKA1_DATA.

W_SDATA = W_E1EDKA1_DATA. "????

T_EDIDD-SEGNAM = 'E1EDKA1'.

APPEND T_EDIDD.

* CALL FUNCTION 'L_IDOC_SEGMENT_CREATE'

* EXPORTING

* I_SEGNAM = 'E1EDKA1'

* I_SDATA = EDIDD-SDATA

* EXCEPTIONS

* OTHERS = 1.



ENDFORM.

No comments:

Blog Archive