Posted: Wed Sep 26, 2007 7:28 pm Post subject: Create PO regardless the Purchase Requisition
Create PO regardless the Purchase Requisition based on the formula:
If (Open Qty + Current Stock) < Safety Stock => THEN A PO NEEDS TO BE CREATED
Code:
*&---------------------------------------------------------------------*
*Report Z_STO_SAFETY_STOCK *
*& *
*----------------------------------------------------------------------*
* Program Name : Z_STO_SAFETY_STOCK *
* *
* Date : 09.03.2007 *
* Programmer : Rajdeep Kumar
*----------------------------------------------------------------------*
* Description : Create STO requirement for Plant
*----------------------------------------------------------------------*
REPORT z_sto_safety_stock NO STANDARD PAGE HEADING.
*&---------------------------------------------------------------------*
*& TABLES
*&---------------------------------------------------------------------*
*&---------------------------------------------------------------------*
*& DATA DECLARATION
*&---------------------------------------------------------------------*
DATA: BEGIN OF it_marc OCCURS 0,
matnr LIKE marc-matnr,
werks LIKE marc-werks,
eisbe LIKE marc-eisbe,
* bstmi LIKE marc-bstmi,
bstrf LIKE marc-bstrf,
beskz LIKE marc-beskz,
sobsl LIKE marc-sobsl,
END OF it_marc.
DATA: BEGIN OF it_mard OCCURS 0,
matnr LIKE mard-matnr,
werks LIKE mard-werks,
labst LIKE mard-labst,
END OF it_mard.
DATA: BEGIN OF it_po OCCURS 0,
ebeln LIKE ekko-ebeln,
werks LIKE ekpo-werks,
ebelp LIKE ekpo-ebelp,
loekz LIKE ekpo-loekz,
etenr LIKE eket-etenr,
matnr LIKE ekpo-matnr,
menge LIKE eket-menge,
wemng LIKE eket-wemng,
END OF it_po.
DATA: BEGIN OF it_po_2 OCCURS 0,
matnr LIKE ekpo-matnr,
werks LIKE ekpo-werks,
ebeln LIKE ekko-ebeln,
ebelp LIKE ekpo-ebelp,
loekz LIKE ekpo-loekz,
etenr LIKE eket-etenr,
menge LIKE eket-menge,
wemng LIKE eket-wemng,
END OF it_po_2.
DATA: BEGIN OF it_po_pend OCCURS 0,
matnr LIKE ekpo-matnr,
werks LIKE ekpo-werks,
pendi LIKE eket-menge,
END OF it_po_pend.
DATA: BEGIN OF wa_join OCCURS 0,
ebeln LIKE ekko-ebeln,
werks LIKE ekpo-werks,
ebelp LIKE ekpo-ebelp,
loekz LIKE ekpo-loekz,
etenr LIKE eket-etenr,
matnr LIKE ekpo-matnr,
menge LIKE eket-menge,
wemng LIKE eket-wemng,
END OF wa_join.
DATA : BEGIN OF itab_marc_mard OCCURS 0,
werks LIKE marc-werks,
matnr LIKE marc-matnr,
eisbe LIKE marc-eisbe,
* bstmi LIKE marc-bstmi,
bstrf LIKE marc-bstrf,
beskz LIKE marc-beskz,
sobsl LIKE marc-sobsl,
labst LIKE mard-labst,
menge LIKE ekpo-menge,
ordqty LIKE ekpo-menge,
END OF itab_marc_mard.
DATA: BEGIN OF it_sto OCCURS 0,
werks LIKE mard-werks,
matnr LIKE mard-matnr,
orqty LIKE ekpo-menge,
eisbe LIKE marc-eisbe,
* bstmi LIKE marc-bstmi,
bstrf LIKE marc-bstrf,
beskz LIKE marc-beskz,
sobsl LIKE marc-sobsl,
labst LIKE mard-labst,
pendi LIKE eket-menge,
END OF it_sto.
DATA: ws_poheader LIKE bapimepoheader.
DATA: ws_poheaderx LIKE bapimepoheaderx .
DATA: ws_ebeln TYPE mepoheader-ebeln.
DATA: ws_posnr LIKE ekpo-ebelp.
DATA: ws_etenr LIKE eket-etenr VALUE '0001'.
DATA: ws_purchaseorder LIKE ekko-ebeln.
DATA: ws_flg1 TYPE c. " VALUE 'X'.
DATA: ws_flg2 TYPE c.
DATA: ws_flg3 TYPE c.
*-- For Handling Error Messages in BAPI
DATA: tbl_return LIKE bapiret2 OCCURS 0 WITH HEADER LINE.
*-- For Item Details
DATA: tbl_poitem LIKE bapimepoitem OCCURS 0 WITH HEADER LINE.
DATA: tbl_poitemx LIKE bapimepoitemx OCCURS 0 WITH HEADER LINE.
DATA: tbl_posched LIKE bapimeposchedule OCCURS 0 WITH HEADER LINE.
DATA: tbl_poschedx LIKE bapimeposchedulx OCCURS 0 WITH HEADER LINE.
SELECTION-SCREEN : BEGIN OF BLOCK b1 WITH FRAME TITLE text-001.
SELECT-OPTIONS: s_werks FOR t001w-werks,
* s_matnr FOR marc-matnr. " M01
s_matnr FOR marc-matnr, " M01
s_matkl FOR mara-matkl, " M01
s_beskz FOR marc-beskz,
s_sobsl FOR marc-sobsl.
PARAMETERS: p_test TYPE check.
SELECTION-SCREEN : END OF BLOCK b1.
*&---------------------------------------------------------------------
*& START OF SELECTION EVENT
*&---------------------------------------------------------------------
START-OF-SELECTION.
* Get the Safety Stock & Minimum Lot Size for the materials
PERFORM get_plant_data.
* Get the Quantities in stock
PERFORM get_stock_data.
* Determine the open quantities for the materials concerned
* [EKKO, EKET, EKPO]
PERFORM get_open_quantities.
* PERFORM open_quantities.
* Determine overall the quantities required for each material
PERFORM determine_mat_qty_required.
* Print the details
PERFORM print_details.
IF p_test EQ space.
* PO is created for each plant.
* Create the PO required.
PERFORM create_po.
** Create the PO's for each plant with the materials required
* PERFORM create_header_item.
*
** Bapi call to create a Purchase order
* PERFORM create_purhase_order.
ENDIF.
END-OF-SELECTION.
TOP-OF-PAGE.
PERFORM print_header.
*&---------------------------------------------------------------------*
*& Form get_plant_data
*&---------------------------------------------------------------------*
* To get the Safety Stock
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
FORM get_plant_data .
* start " M01
* SELECT matnr werks eisbe bstmi
* INTO TABLE it_marc
* FROM marc
* WHERE matnr IN s_matnr AND
* werks IN s_werks AND
* eisbe GT 0.
* end " M01
* start " M01
SELECT marc~matnr werks eisbe bstrf beskz sobsl
* " bstmi
INTO TABLE it_marc
FROM marc AS marc
INNER JOIN mara AS mara
ON mara~matnr EQ marc~matnr
WHERE marc~matnr IN s_matnr AND
werks IN s_werks AND
matkl IN s_matkl AND
beskz IN s_beskz AND
sobsl IN s_sobsl AND
eisbe GT 0.
* end " M01
ENDFORM. " get_plant_data
*&---------------------------------------------------------------------*
*& Form get_stock_data
*&---------------------------------------------------------------------*
* To get the Current Stock
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
FORM get_stock_data .
DATA: BEGIN OF lt_mard OCCURS 0,
matnr LIKE mard-matnr,
werks LIKE mard-werks,
lgort LIKE mard-lgort,
labst LIKE mard-labst,
END OF lt_mard.
CHECK NOT it_marc[] IS INITIAL.
SELECT matnr werks lgort labst
INTO TABLE lt_mard
FROM mard
FOR ALL ENTRIES IN it_marc
WHERE matnr EQ it_marc-matnr AND
werks EQ it_marc-werks.
SORT lt_mard BY matnr werks.
LOOP AT lt_mard.
AT NEW werks.
it_mard-matnr = lt_mard-matnr.
it_mard-werks = lt_mard-werks.
ENDAT.
it_mard-labst = it_mard-labst + lt_mard-labst.
AT END OF werks.
APPEND it_mard.
CLEAR it_mard.
ENDAT.
ENDLOOP.
ENDFORM. " get_stock_data
*&---------------------------------------------------------------------*
*& Form open_quantities
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
FORM open_quantities .
* Storing MARC and MARD data in an internal table to find the open
* quantities
SELECT a~ebeln
b~ebelp
b~werks
b~matnr
b~menge
c~etenr
c~wamng
INTO CORRESPONDING FIELDS OF TABLE it_po
FROM ekpo AS b
INNER JOIN ekko AS a
ON a~ebeln = b~ebeln
INNER JOIN eket AS c
ON b~ebeln = c~ebeln AND b~ebelp = c~ebelp
FOR ALL ENTRIES IN itab_marc_mard
WHERE b~matnr = itab_marc_mard-matnr AND
b~werks IN s_werks .
DATA : t_menge TYPE ekpo-menge . "local variable
*Store the contents of it_po to the work area to get the minimum order
*quantity
wa_join[] = it_po[].
LOOP AT it_po.
* delete the entries where MENGE = WAMNG
DELETE it_po WHERE menge = it_po-wemng.
LOOP AT wa_join WHERE matnr = it_po-matnr
AND werks = it_po-werks.
* Move all the contents to the internal table it_sto ie Plant,
* Material and the Min Order Qty
* so that we can create PO based on this internal table
*&---------------------------------------------------------------------*
*& Form CREATE_HEADER_ITEM
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
FORM create_header_item.
LOOP AT it_sto.
*--- Document date with flag
ws_poheader-doc_date = sy-datum.
ws_poheaderx-doc_date = ws_flg.
*--- Document type with flag
ws_poheader-doc_type = 'Z3'(c01).
ws_poheaderx-doc_type = ws_flg.
*--- Purchasing group with Flag
ws_poheader-pur_group = '005'. " hardcoded
ws_poheaderx-pur_group = ws_flg.
*--- Purchasing Organization with flag
ws_poheader-purch_org = '2000'.
ws_poheaderx-purch_org = ws_flg.
*--- Supply plant with flag
ws_poheader-suppl_plnt = '2000'.
ws_poheaderx-suppl_plnt = ws_flg.
*--- Company Code with flag
ws_poheader-comp_code = '2000'.
ws_poheaderx-comp_code = ws_flg.
*<--- assinging new item No.
ws_posnr = ws_posnr + 10.
ENDFORM. "CREATE_PO
*&---------------------------------------------------------------------*
*& Form
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
FORM create_purhase_order .
*<--- Bapi to commit the changes
IF sy-subrc <> 0.
CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'
EXPORTING
wait = 'X'.
ENDIF.
ENDIF.
LOOP AT tbl_return.
*<--- Ignore the warning messages
IF tbl_return-type = 'W'.
CONTINUE.
ENDIF.
FORMAT COLOR COL_NORMAL INTENSIFIED OFF.
IF tbl_return-type = 'S'.
FORMAT COLOR COL_POSITIVE ON.
ELSEIF tbl_return-type = 'W'.
FORMAT COLOR COL_TOTAL INTENSIFIED OFF.
ELSEIF tbl_return-type = 'E'.
FORMAT COLOR COL_NEGATIVE INTENSIFIED OFF.
ENDIF.
WRITE:/3 tbl_return-type,12 tbl_return-id,20(85) tbl_return-message
,
86(10) tbl_return-field,95(20) tbl_return-system.
FORMAT COLOR OFF.
ENDLOOP.
ENDFORM. " create_purhase_order
*&---------------------------------------------------------------------*
*& Form get_open_quantities
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
FORM get_open_quantities .
CHECK NOT it_mard[] IS INITIAL.
SELECT ekko~ebeln ekpo~ebelp ekpo~werks ekpo~matnr
ekpo~menge eket~etenr eket~wemng
INTO CORRESPONDING FIELDS OF TABLE it_po
FROM ekko AS ekko
INNER JOIN ekpo AS ekpo
ON ekpo~ebeln EQ ekko~ebeln
INNER JOIN eket AS eket
ON eket~ebeln EQ ekpo~ebeln AND
eket~ebelp EQ ekpo~ebelp
FOR ALL ENTRIES IN it_mard
WHERE ekko~bstyp EQ 'F' AND
ekko~bsart EQ 'Z3' AND
ekko~bsakz EQ 'T' AND
ekko~loekz EQ space AND
ekpo~matnr EQ it_mard-matnr AND
ekpo~werks EQ it_mard-werks AND
ekpo~loekz EQ space AND
ekpo~elikz EQ space.
SORT it_po BY ebeln ebelp etenr.
* delete entries where the schedule is complete, else copy to new table
LOOP AT it_po.
IF it_po-wemng GE it_po-menge.
DELETE it_po.
ELSE.
MOVE-CORRESPONDING it_po TO it_po_2.
APPEND it_po_2.
CLEAR it_po_2.
ENDIF.
ENDLOOP.
SORT it_po_2 BY matnr werks.
* To determine the pending quantities of the materials at plant level
LOOP AT it_po_2.
AT NEW werks.
it_po_pend-matnr = it_po_2-matnr.
it_po_pend-werks = it_po_2-werks.
ENDAT.
AT END OF werks.
APPEND it_po_pend.
CLEAR it_po_pend.
ENDAT.
ENDLOOP.
ENDFORM. " get_open_quantities
*&---------------------------------------------------------------------*
*& Form determine_mat_qty_required
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
FORM determine_mat_qty_required .
DATA: lv_eisbe LIKE marc-eisbe,
lv_labst LIKE mard-labst,
lv_menge LIKE eket-menge,
* lv_bstmi_old LIKE marc-bstmi,
lv_bstrf_old LIKE marc-bstrf,
lv_beskz_old LIKE marc-beskz,
lv_sobsl_old LIKE marc-sobsl,
lv_pendi LIKE eket-menge,
lv_total_conf LIKE ekpo-menge,
* lv_bstmi LIKE marc-bstmi,
* lv_min_qty LIKE marc-bstmi,
lv_bstrf LIKE marc-bstrf,
lv_min_qty LIKE marc-bstrf,
lv_qty_req LIKE mard-labst,
lv_remainder TYPE i.
IF sy-subrc EQ 0.
* PO pending qty
lv_pendi = it_po_pend-pendi.
ELSE.
CLEAR lv_pendi.
ENDIF.
* Total qty on hand & PO qty pending
lv_total_conf = lv_labst + lv_pendi.
* Now determine if a PO is required for the material concerned.
IF lv_total_conf < lv_eisbe.
it_sto-matnr = it_marc-matnr.
it_sto-werks = it_marc-werks.
it_sto-eisbe = lv_eisbe.
* it_sto-bstmi = lv_bstmi_old.
it_sto-bstrf = lv_bstrf_old.
it_sto-beskz = lv_beskz_old.
it_sto-sobsl = lv_sobsl_old.
it_sto-labst = lv_labst.
it_sto-pendi = lv_pendi.
* Actual Qty Required
lv_qty_req = lv_eisbe - lv_total_conf.
* IF it_marc-bstmi IS INITIAL.
IF it_marc-bstrf IS INITIAL.
it_sto-orqty = lv_qty_req.
ELSE.
* lv_bstmi = it_marc-bstmi.
* lv_min_qty = it_marc-bstmi.
lv_bstrf = it_marc-bstrf.
lv_min_qty = it_marc-bstrf.
ENDFORM. " determine_mat_qty_required
*&---------------------------------------------------------------------*
*& Form print_details
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
FORM print_details .
LOOP AT it_sto.
WRITE:/ it_sto-werks UNDER text-h01,
it_sto-matnr UNDER text-h02,
it_sto-orqty UNDER text-h03,
it_sto-eisbe UNDER text-h04,
* it_sto-bstmi UNDER text-h05,
it_sto-bstrf UNDER text-h05,
it_sto-labst UNDER text-h06,
it_sto-pendi UNDER text-h07.
ENDLOOP.
ENDFORM. " print_details
*&---------------------------------------------------------------------*
*& Form print_header
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
FORM print_header .
ENDFORM. " print_header
*&---------------------------------------------------------------------*
*& Form create_po
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
FORM create_po .
*-Document date with flag
ws_poheader-doc_date = sy-datum.
ws_poheaderx-doc_date = ws_flg.
*-Document type with flag
ws_poheader-doc_type = 'Z3'(c01).
ws_poheaderx-doc_type = ws_flg.
*-Purchasing group with Flag
ws_poheader-pur_group = '005'. " hardcoded
ws_poheaderx-pur_group = ws_flg.
*-Purchasing Organization with flag
ws_poheader-purch_org = '2000'.
ws_poheaderx-purch_org = ws_flg.
*-Supply plant with flag
ws_poheader-suppl_plnt = '2000'.
ws_poheaderx-suppl_plnt = ws_flg.
*-Company Code with flag
ws_poheader-comp_code = '2000'.
ws_poheaderx-comp_code = ws_flg.
LOOP AT it_sto.
AT NEW werks.
CLEAR tbl_poitem.
CLEAR tbl_poitemx.
CLEAR tbl_return.
CLEAR tbl_posched.
CLEAR tbl_poschedx.
REFRESH tbl_poitem.
REFRESH tbl_poitemx.
REFRESH tbl_return.
REFRESH tbl_posched.
REFRESH tbl_poschedx.
CLEAR ws_posnr.
ENDAT.
AT END OF werks.
* Create the PO
CALL FUNCTION 'BAPI_PO_CREATE1'
EXPORTING
poheader = ws_poheader
poheaderx = ws_poheaderx
IMPORTING
exppurchaseorder = ws_purchaseorder
TABLES
return = tbl_return
poitem = tbl_poitem
poitemx = tbl_poitemx
poschedule = tbl_posched
poschedulex = tbl_poschedx.
LOOP AT tbl_return WHERE type EQ 'E' OR type EQ 'A'.
EXIT.
ENDLOOP.
* If Loop is false - then no error occurred
IF sy-subrc NE 0.
* No Errors - Commit
CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'
EXPORTING
wait = 'X'.
ENDIF.
SKIP.
LOOP AT tbl_return.
FORMAT COLOR COL_NORMAL INTENSIFIED OFF.
IF tbl_return-type = 'E' OR tbl_return-type = 'A'.
FORMAT COLOR COL_NEGATIVE INTENSIFIED OFF.
ELSEIF tbl_return-type = 'W'.
FORMAT COLOR COL_TOTAL INTENSIFIED OFF.
ELSE.
FORMAT COLOR COL_POSITIVE ON.
ENDIF.
WRITE:/3 tbl_return-type,12 tbl_return-id,20(85)
tbl_return-message,
86(10) tbl_return-field,95(20) tbl_return-system.
FORMAT COLOR OFF.
ENDLOOP.
ENDAT.
ENDLOOP.
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum You cannot attach files in this forum You cannot download files in this forum
All product names are trademarks of their respective companies. SAPNET.RU websites are in no way affiliated with SAP AG. SAP, SAP R/3, R/3 software, mySAP, ABAP, BAPI, xApps, SAP NetWeaver and any other are registered trademarks of SAP AG. Every effort is made to ensure content integrity. Use information on this site at your own risk.