Vendor Blocking in SAP
Scenario : Block the vendor as per Last Transaction
Fields Responsible for vendor Blocking in table LFA1, LFB1, LFM1
- SPERR
- SPERM
Declaration of variables
DATA:LS_LFA1 TYPE LFA1,
LS_LFB1 TYPE LFB1,
LS_LFM1 TYPE LFM1,
LS_YLFA1 TYPE LFA1,
LS_YLFB1 TYPE LFB1,
LS_YLFM1 TYPE LFM1.
DATA: LT_XLFAS TYPE TABLE OF FLFAS,
LT_XLFB5 TYPE TABLE OF FLFB5,
LT_XLFBK TYPE TABLE OF FLFBK,
LT_XLFZA TYPE TABLE OF FLFZA,
LT_YLFAS TYPE TABLE OF FLFAS,
LT_YLFB5 TYPE TABLE OF FLFB5,
LT_YLFBK TYPE TABLE OF FLFBK,
LT_YLFZA TYPE TABLE OF FLFZA.
Bapi call
CALL FUNCTION 'VENDOR_UPDATE'
EXPORTING
I_LFA1 = LS_LFA1
I_LFB1 = LS_LFB1
I_LFM1 = LS_LFM1
I_YLFA1 = LS_YLFA1
I_YLFB1 = LS_YLFB1
I_YLFM1 = LS_YLFM1
TABLES
T_XLFAS = LT_XLFAS
T_XLFB5 = LT_XLFB5
T_XLFBK = LT_XLFBK
T_XLFZA = LT_XLFZA
T_YLFAS = LT_YLFAS
T_YLFB5 = LT_YLFB5
T_YLFBK = LT_YLFBK
T_YLFZA = LT_YLFZA
* T_XKNVK =
* T_YKNVK =
.
T-Code : BP
complete program.
*&---------------------------------------------------------------------*
*& Report ZVENDOR_BLOCKING
*&---------------------------------------------------------------------*
*&
*&---------------------------------------------------------------------*
REPORT ZVENDOR_BLOCKING.
TABLES: LFA1, LFB1, BSAK, BSIK.
SELECTION-SCREEN : BEGIN OF BLOCK B1 WITH FRAME TITLE TEXT-001.
PARAMETERS: P_DATE TYPE BUDAT OBLIGATORY DEFAULT SY-DATUM.
PARAMETERS: P_LIFNR TYPE LIFNR OBLIGATORY.
SELECTION-SCREEN : END OF BLOCK B1.
SELECTION-SCREEN : BEGIN OF BLOCK B2 WITH FRAME TITLE TEXT-002.
PARAMETERS: R1 RADIOBUTTON GROUP D1,
R2 RADIOBUTTON GROUP D1,
R3 RADIOBUTTON GROUP D1,
R4 RADIOBUTTON GROUP D1.
SELECTION-SCREEN : END OF BLOCK B2.
TYPES: BEGIN OF TY_FINAL,
VENDOR TYPE LIFNR,
LAST_TRANSACTION TYPE BUDAT,
DAYS TYPE PEA_SCRDD,
FLAG TYPE C,
STATUS(7) TYPE C,
END OF TY_FINAL.
DATA: CURRENT_DATE LIKE P0001-BEGDA,
DOC_DATE LIKE P0001-BEGDA,
DAYS TYPE PEA_SCRDD.
DATA: IT_FINAL TYPE TABLE OF TY_FINAL, WA_FINAL TYPE TY_FINAL.
DATA: IT_BSAK2 TYPE TABLE OF BSAK.
DATA: IT_FIELDCAT TYPE SLIS_T_FIELDCAT_ALV,
WA_FIELDCAT TYPE SLIS_FIELDCAT_ALV.
DATA:LS_LFA1 TYPE LFA1,
LS_LFB1 TYPE LFB1,
LS_LFM1 TYPE LFM1,
LS_YLFA1 TYPE LFA1,
LS_YLFB1 TYPE LFB1,
LS_YLFM1 TYPE LFM1.
DATA: LT_XLFAS TYPE TABLE OF FLFAS,
LT_XLFB5 TYPE TABLE OF FLFB5,
LT_XLFBK TYPE TABLE OF FLFBK,
LT_XLFZA TYPE TABLE OF FLFZA,
LT_YLFAS TYPE TABLE OF FLFAS,
LT_YLFB5 TYPE TABLE OF FLFB5,
LT_YLFBK TYPE TABLE OF FLFBK,
LT_YLFZA TYPE TABLE OF FLFZA.
CURRENT_DATE = P_DATE. " Set system date
CONSTANTS:
C_X TYPE CHAR1 VALUE 'X' .
START-OF-SELECTION.
PERFORM FETCH_DATA.
PERFORM DISPLAY.
*&---------------------------------------------------------------------*
*& Form FETCH_DATA
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
*& --> p1 text
*& <-- p2 text
*&---------------------------------------------------------------------*
FORM FETCH_DATA .
SELECT
A~LIFNR,
B~KTOKK
FROM LFB1 AS A INNER JOIN LFA1 AS B ON A~LIFNR EQ B~LIFNR INTO TABLE @DATA(T_LFB1)
WHERE A~BUKRS = '1000' AND B~KTOKK NE 'E001' AND B~KTOKK NE 'E002' AND B~KTOKK NE 'E003' AND A~LIFNR = @P_LIFNR.
IF SY-SUBRC = 0.
SELECT
LIFNR,
BUDAT
FROM BSAK INTO TABLE @DATA(IT_BSAK) FOR ALL ENTRIES IN @T_LFB1 WHERE LIFNR = @T_LFB1-LIFNR AND BUKRS = '1000' .
SELECT
LIFNR,
BUDAT
FROM BSIK INTO TABLE @DATA(IT_BSIK) FOR ALL ENTRIES IN @T_LFB1 WHERE LIFNR = @T_LFB1-LIFNR AND BUKRS = '1000' .
LOOP AT IT_BSIK INTO DATA(WA_BSIK).
APPEND WA_BSIK TO IT_BSAK.
CLEAR: WA_BSIK.
ENDLOOP.
REFRESH IT_BSIK.
SORT IT_BSAK BY LIFNR DESCENDING BUDAT DESCENDING.
DELETE ADJACENT DUPLICATES FROM IT_BSAK COMPARING LIFNR.
ENDIF.
"""""""""""""""""""""""""""""""""""""""""""""""' CALCULATE DATE DIFFERENCE
LOOP AT IT_BSAK INTO DATA(WA_BSAK).
CALL FUNCTION 'HR_HK_DIFF_BT_2_DATES'
EXPORTING
DATE1 = CURRENT_DATE
DATE2 = WA_BSAK-BUDAT
OUTPUT_FORMAT = '02'
IMPORTING
* YEARS =
* MONTHS =
DAYS = DAYS
EXCEPTIONS
OVERFLOW_LONG_YEARS_BETWEEN = 1
INVALID_DATES_SPECIFIED = 2
OTHERS = 3.
IF SY-SUBRC <> 0.
* Implement suitable error handling here
ENDIF.
WA_FINAL-VENDOR = WA_BSAK-LIFNR.
WA_FINAL-LAST_TRANSACTION = WA_BSAK-BUDAT.
WA_FINAL-DAYS = DAYS.
APPEND WA_FINAL TO IT_FINAL.
CLEAR: WA_FINAL, DAYS, WA_BSAK.
ENDLOOP.
IF R1 = 'X'.
LOOP AT IT_FINAL INTO WA_FINAL.
IF WA_FINAL-DAYS > 90.
WA_FINAL-FLAG = 'X'.
MODIFY IT_FINAL INDEX SY-TABIX FROM WA_FINAL TRANSPORTING FLAG.
CLEAR WA_FINAL.
ENDIF.
ENDLOOP.
ELSEIF R2 = 'X'.
LOOP AT IT_FINAL INTO WA_FINAL.
IF WA_FINAL-DAYS > 180.
WA_FINAL-FLAG = 'X'.
MODIFY IT_FINAL INDEX SY-TABIX FROM WA_FINAL TRANSPORTING FLAG.
CLEAR WA_FINAL.
ENDIF.
ENDLOOP.
ELSEIF R3 = 'X'.
LOOP AT IT_FINAL INTO WA_FINAL.
IF WA_FINAL-DAYS > 270.
WA_FINAL-FLAG = 'X'.
MODIFY IT_FINAL INDEX SY-TABIX FROM WA_FINAL TRANSPORTING FLAG.
CLEAR WA_FINAL.
ENDIF.
ENDLOOP.
ELSEIF R4 = 'X'.
LOOP AT IT_FINAL INTO WA_FINAL.
IF WA_FINAL-DAYS > 360.
WA_FINAL-FLAG = 'X'.
MODIFY IT_FINAL INDEX SY-TABIX FROM WA_FINAL TRANSPORTING FLAG.
CLEAR WA_FINAL.
ENDIF.
ENDLOOP.
ENDIF.
DELETE IT_FINAL WHERE FLAG NE 'X'.
DATA: IT_LFA1 TYPE TABLE OF LFA1, WA_LFA1 TYPE LFA1,
IT_LFB1 TYPE TABLE OF LFB1, WA_LFB1 TYPE LFB1,
IT_LFM1 TYPE TABLE OF LFM1, WA_LFM1 TYPE LFM1.
SELECT * FROM LFA1 INTO TABLE IT_LFA1 FOR ALL ENTRIES IN IT_FINAL WHERE LIFNR = IT_FINAL-VENDOR .
SELECT * FROM LFB1 INTO TABLE IT_LFB1 FOR ALL ENTRIES IN IT_FINAL WHERE LIFNR = IT_FINAL-VENDOR .
SELECT * FROM LFM1 INTO TABLE IT_LFM1 FOR ALL ENTRIES IN IT_FINAL WHERE LIFNR = IT_FINAL-VENDOR .
LOOP AT IT_FINAL INTO WA_FINAL.
READ TABLE IT_LFA1 INTO WA_LFA1 WITH KEY LIFNR = WA_FINAL-VENDOR.
IF SY-SUBRC = 0.
WA_LFA1-SPERR = 'X'.
WA_LFA1-SPERM = 'X'.
MOVE-CORRESPONDING WA_LFA1 TO LS_LFA1.
ENDIF.
READ TABLE IT_LFB1 INTO WA_LFB1 WITH KEY LIFNR = WA_FINAL-VENDOR.
IF SY-SUBRC = 0.
WA_LFB1-SPERR = 'X'.
* WA_LFB1-SPERM = 'X'.
MOVE-CORRESPONDING WA_LFB1 TO LS_LFB1.
ENDIF.
READ TABLE IT_LFM1 INTO WA_LFM1 WITH KEY LIFNR = WA_FINAL-VENDOR.
IF SY-SUBRC = 0.
* WA_LFM1-SPERR = 'X'.
WA_LFM1-SPERM = 'X'.
MOVE-CORRESPONDING WA_LFM1 TO LS_LFM1.
ENDIF.
CALL FUNCTION 'VENDOR_UPDATE'
EXPORTING
I_LFA1 = LS_LFA1
I_LFB1 = LS_LFB1
I_LFM1 = LS_LFM1
I_YLFA1 = LS_YLFA1
I_YLFB1 = LS_YLFB1
I_YLFM1 = LS_YLFM1
TABLES
T_XLFAS = LT_XLFAS
T_XLFB5 = LT_XLFB5
T_XLFBK = LT_XLFBK
T_XLFZA = LT_XLFZA
T_YLFAS = LT_YLFAS
T_YLFB5 = LT_YLFB5
T_YLFBK = LT_YLFBK
T_YLFZA = LT_YLFZA
* T_XKNVK =
* T_YKNVK =
.
CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'
EXPORTING
WAIT = C_X.
IF SY-SUBRC = 0.
WA_FINAL-STATUS = 'BLOCKED'.
MODIFY IT_FINAL INDEX SY-TABIX FROM WA_FINAL TRANSPORTING STATUS.
ENDIF.
CLEAR : WA_FINAL, WA_LFA1, WA_LFB1, WA_LFM1 .
ENDLOOP.
ENDFORM.
*&---------------------------------------------------------------------*
*& Form DISPLAY
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
*& --> p1 text
*& <-- p2 text
*&---------------------------------------------------------------------*
FORM DISPLAY .
PERFORM CREATE_FCAT.
CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
EXPORTING
I_CALLBACK_PROGRAM = SY-REPID
IT_FIELDCAT = IT_FIELDCAT
I_DEFAULT = 'X'
I_SAVE = 'X'
* IS_LAYOUT = IT_LAYOUT
TABLES
T_OUTTAB = IT_FINAL[]
EXCEPTIONS
PROGRAM_ERROR = 1
OTHERS = 2.
IF SY-SUBRC <> 0.
* Implement suitable error handling here
ENDIF.
ENDFORM.
*&---------------------------------------------------------------------*
*& Form CREATE_FCAT
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
*& --> p1 text
*& <-- p2 text
*&---------------------------------------------------------------------*
FORM CREATE_FCAT .
WA_FIELDCAT-FIELDNAME = 'VENDOR'.
WA_FIELDCAT-TABNAME = 'IT_FINAL'.
WA_FIELDCAT-OUTPUTLEN = '15'.
WA_FIELDCAT-SELTEXT_M = 'VENDOR'.
APPEND WA_FIELDCAT TO IT_FIELDCAT.
CLEAR WA_FIELDCAT.
WA_FIELDCAT-FIELDNAME = 'LAST_TRANSACTION'.
WA_FIELDCAT-TABNAME = 'IT_FINAL'.
WA_FIELDCAT-OUTPUTLEN = '25'.
WA_FIELDCAT-SELTEXT_M = 'LAST TRANSACTION'.
APPEND WA_FIELDCAT TO IT_FIELDCAT.
CLEAR WA_FIELDCAT.
WA_FIELDCAT-FIELDNAME = 'DAYS'.
WA_FIELDCAT-TABNAME = 'IT_FINAL'.
WA_FIELDCAT-OUTPUTLEN = '15'.
WA_FIELDCAT-SELTEXT_M = 'DAYS'.
APPEND WA_FIELDCAT TO IT_FIELDCAT.
CLEAR WA_FIELDCAT.
WA_FIELDCAT-FIELDNAME = 'STATUS'.
WA_FIELDCAT-TABNAME = 'IT_FINAL'.
WA_FIELDCAT-OUTPUTLEN = '15'.
WA_FIELDCAT-SELTEXT_M = 'STATUS'.
APPEND WA_FIELDCAT TO IT_FIELDCAT.
CLEAR WA_FIELDCAT.
ENDFORM.
No comments:
Post a Comment