Wednesday, 10 April 2019

Automation of GR Against PO and Picking, PGI for Sales Order Delivery

Requirement: An automation requested from client for posting GR against purchase order of Delivery at PGI and delivery creation, picking for the sales order. The following is the flow chart of the requirement.

SAP SD, SAP ABAP Development, SAP ABAP Guides, SAP ABAP Certifications

First, we must create an output type for our delivery in our case we can consider the output type as ZXXX.

In the processing routine of the output type medium should be “Special function”, give the program name and the form name.

Create a routine,  VOFM->Output control and assign it for V2 in sales and activate it.

we need to change the dispatch time of our output type ZXXX to “Send with periodically scheduled job” avoid lock object issue.

As this is an automation process we must create a periodical job which should execute at the time of triggering an event.

We need to create an event for this using the transaction SM62.

Create an RFC functional module which contains the FM to trigger the event, this RFC FM will be called in the routine at the time of PGI.

Once the PGI is done the output type ZXXX will be in ready to process state. The RFC FM triggers the event which in turn executes the BG job and the output is processed successfully.

The following are the required screen shots.

Logic in VOFM routine:

SAP SD, SAP ABAP Development, SAP ABAP Guides, SAP ABAP Certifications

RFC FM which is called in the routine

SAP SD, SAP ABAP Development, SAP ABAP Guides, SAP ABAP Certifications

SAP SD, SAP ABAP Development, SAP ABAP Guides, SAP ABAP Certifications

Use Job wizard to create a BG Job.

Give the Job name

SAP SD, SAP ABAP Development, SAP ABAP Guides, SAP ABAP Certifications

Select ABAP program step.

SAP SD, SAP ABAP Development, SAP ABAP Guides, SAP ABAP Certifications

Give program name as RSNAT00 and saved variant name

SAP SD, SAP ABAP Development, SAP ABAP Guides, SAP ABAP Certifications

SAP SD, SAP ABAP Development, SAP ABAP Guides, SAP ABAP Certifications

Select After event option

SAP SD, SAP ABAP Development, SAP ABAP Guides, SAP ABAP Certifications

Give Event name created in SM62 and check periodically option.

The periodically option is selected to trigger the Job whenever the event is triggered.

SAP SD, SAP ABAP Development, SAP ABAP Guides, SAP ABAP Certifications

Complete the process.

SAP SD, SAP ABAP Development, SAP ABAP Guides, SAP ABAP Certifications

Now we must write the logic to do GR and create delivery for SO and do picking and PGI

REPORT  yxxxxxxxxxxxx
*&———————————————————————*
*&      Form  f_bapi_call
*&———————————————————————*
*       EDI Special function Form
*———————————————————————-*
FORM f_bapi_call USING rc us_screen.
*-Table is declared to extract NAST values from Global
TABLES: nast.
*-types for delivery
TYPES: BEGIN OF lty_lips,
vbeln  TYPE vbeln_vl,
posnr  TYPE posnr_vl,
matnr  TYPE matnr,
werks  TYPE werks_d,
lgort  TYPE lgort_d,
charg  TYPE charg_d,
lfimg  TYPE lfimg,
meins  TYPE meins,
vbelv  TYPE vbelv,
posnv  TYPE posnv,
vgbel  TYPE vgbel,
vgpos  TYPE vgpos,
END    OF   lty_lips,
*-types for  PO
BEGIN  OF   lty_ekpo,
ebeln  TYPE ebeln,
ebelp  TYPE ebelp,
matnr  TYPE matnr,
werks  TYPE ewerk,
lgort  TYPE lgort_d,
menge  TYPE bstmg,
meins  TYPE bstme,
END    OF   lty_ekpo,
*-Sale order data
BEGIN OF lty_vbap,
vbeln  TYPE vbeln_va,
posnr  TYPE posnr_va,
matnr  TYPE matnr,
uepos  TYPE uepos,
kwmeng TYPE kwmeng,
werks  TYPE werks_ext,
lgort  TYPE lgort_d,
vstel  TYPE vstel,
END OF lty_vbap.
*-local variables, workarea and internal table declarations
DATA: lv_wbstk      TYPE wbstk,
ls_mvt_header TYPE bapi2017_gm_head_01,
lv_gr_mblnr   TYPE mblnr,
lv_gr_mjahr   TYPE mjahr,
lt_mvt_item   TYPE STANDARD TABLE OF bapi2017_gm_item_create ,
ls_mvt_item   TYPE bapi2017_gm_item_create,
lt_lips       TYPE STANDARD TABLE OF lty_lips,
ls_lips       TYPE lty_lips,
lt_ekpo       TYPE STANDARD TABLE OF lty_ekpo,
ls_ekpo       TYPE lty_ekpo,
ls_vbkok      TYPE vbkok,
lt_vbpok      TYPE STANDARD TABLE OF vbpok,
ls_vbpok      TYPE vbpok,
lt_prott      TYPE STANDARD TABLE OF prott,
ls_prott      TYPE prott,
lt_request    TYPE STANDARD TABLE OF bapideliciousrequest,
lt_created    TYPE STANDARD TABLE OF bapideliciouscreateditems,
lt_return     TYPE STANDARD TABLE OF bapiret2,
lt_vbap       TYPE STANDARD TABLE OF lty_vbap,
lv_msgv1      TYPE char50,
lv_msgv2      TYPE char50,
ls_vbap       TYPE lty_vbap,
ls_request    TYPE bapideliciousrequest,
ls_created    TYPE bapideliciouscreateditems,
ls_return     TYPE bapiret2,
lv_vbeln      TYPE vbeln_vl,
lv_check_gi   TYPE xfeld,
lv_msgno      TYPE sy-msgno,
lv_check_fi   TYPE xfeld.
*-constants
CONSTANTS:
lc_wbstk      TYPE wbstk   VALUE ‘C’,
lc_mvtyp      TYPE bwart   VALUE ‘101’,
lc_mvind      TYPE char1   VALUE ‘B’,
lc_mvcod      TYPE bapi2017_gm_code
VALUE ’01’,
lc_s          TYPE char1   VALUE ‘S’,
lc_w          TYPE char1   VALUE ‘W’,
lc_a          TYPE char1   VALUE ‘A’,
lc_e          TYPE char1   VALUE ‘E’,
lc_msgid      TYPE symsgid VALUE ‘YSD_IDOC’,
lc_msgno      TYPE numc3   VALUE ‘000’.
*- check PGI is done for the delivery, if done proceed further
SELECT SINGLE wbstk FROM vbuk INTO lv_wbstk WHERE vbeln = nast-objky.
IF sy-subrc EQ 0.
*-check the PGI status EQ ‘C’ which means completed
IF lv_wbstk EQ lc_wbstk.
*- if the PGI is done for the delivery fetch the delivery item details
SELECT vbeln
posnr
matnr
werks
lgort
charg
lfimg
meins
vbelv
posnv
vgbel
vgpos FROM lips INTO TABLE lt_lips
WHERE vbeln EQ nast-objky.
IF sy-subrc EQ 0.
SORT lt_lips.
*-fetch the PO item details based on PO number from delivery
SELECT ebeln
ebelp
matnr
werks
lgort
menge
meins FROM ekpo INTO TABLE lt_ekpo
FOR ALL ENTRIES IN lt_lips
WHERE ebeln = lt_lips-vgbel
AND   ebelp = lt_lips-vgpos+1(5).
IF sy-subrc EQ 0.
*-Material document  header details
*          ls_mvt_header-pr_uname   = sy-uname.   “user name
ls_mvt_header-pstng_date = sy-datum.   “current date as posting date
ls_mvt_header-doc_date   = sy-datum.   “current date as document date
ls_mvt_header-ref_doc_no = nast-objky. ” delivery number as reference document number
LOOP AT lt_lips INTO ls_lips.
READ TABLE lt_ekpo INTO ls_ekpo WITH KEY ebeln = ls_lips-vgbel
ebelp = ls_lips-vgpos+1(5).
IF sy-subrc EQ 0.
ls_mvt_item-material  = ls_ekpo-matnr. “Material number from PO
ls_mvt_item-plant     = ls_ekpo-werks. “Plant from PO
ls_mvt_item-stge_loc  = ls_ekpo-lgort. “Storage loc from PO
ls_mvt_item-entry_qnt = ls_ekpo-menge. “Quantity from PO
ls_mvt_item-entry_uom = ls_ekpo-meins. “UOM from PO
ls_mvt_item-po_number = ls_ekpo-ebeln. “PO number
ls_mvt_item-po_item   = ls_ekpo-ebelp. “PO line item
ls_mvt_item-move_type = lc_mvtyp.      “Movement type is 101
ls_mvt_item-mvt_ind   = lc_mvind.      “Movement indicator “B” means GR for PO
APPEND ls_mvt_item TO lt_mvt_item.
CLEAR: ls_mvt_item, ls_lips.
ENDIF.
ENDLOOP.
IF lt_mvt_item IS NOT INITIAL.
*-call BAPI to create GR for PO
CLEAR: lt_return.
CALL FUNCTION ‘BAPI_GOODSMVT_CREATE’
EXPORTING
goodsmvt_header  = ls_mvt_header
goodsmvt_code    = lc_mvcod ” GR for PO
IMPORTING
materialdocument = lv_gr_mblnr
matdocumentyear  = lv_gr_mjahr
TABLES
goodsmvt_item    = lt_mvt_item
return           = lt_return.
IF lv_gr_mblnr IS NOT INITIAL.
*-commit the data
CALL FUNCTION ‘BAPI_TRANSACTION_COMMIT’
EXPORTING
wait = abap_true.
rc = 0.
lv_msgv1 = lv_gr_mblnr.
lv_msgv2 = text-001.
*-success message
PERFORM f_nast_update USING lc_msgid lc_msgno lc_s
lv_msgv2 lv_msgv1 space space.
CLEAR:lv_msgv1,lv_msgv2.
ELSE.
*-error message
rc = 4.
LOOP AT lt_return INTO ls_return.
PERFORM f_nast_update USING ls_return-id ls_return-number ls_return-type
ls_return-message_v1 ls_return-message_v2
ls_return-message_v3 ls_return-message_v4.
ENDLOOP.
ENDIF.
ENDIF.
ENDIF.
ENDIF.
ELSE.
rc = 4.
lv_msgv1 = nast-objky.
lv_msgv2 = text-002.
PERFORM f_nast_update USING lc_msgid lc_msgno lc_e
lv_msgv2 lv_msgv1 space space.
CLEAR:lv_msgv1.
ENDIF.
ENDIF.
IF rc = 0.
*-if GR is done successfully
*-fetch sales order details
SELECT vbeln
posnr
matnr
uepos
kwmeng
werks
lgort
vstel FROM vbap INTO TABLE lt_vbap
FOR ALL ENTRIES IN lt_lips
WHERE vbeln = lt_lips-vbelv
AND   posnr = lt_lips-posnv.
IF sy-subrc EQ 0.
LOOP AT lt_vbap INTO ls_vbap.
ls_request-document_numb      = ls_vbap-vbeln.
ls_request-document_item      = ls_vbap-posnr.
ls_request-quantity_sales_uom = ls_vbap-kwmeng.
ls_request-quantity_base__uom = ls_vbap-kwmeng.
ls_request-document_type      = lc_a.
ls_request-delivery_date      = sy-datum.
ls_request-quan_no_check      = lc_w.
ls_request-material           = ls_vbap-matnr.
ls_request-plant              = ls_vbap-werks.
ls_request-date               = sy-datum.
ls_request-goods_issue_date   = sy-datum.
ls_request-goods_issue_time   = sy-uzeit.
APPEND ls_request TO lt_request.
CLEAR: ls_request.
ENDLOOP.
CLEAR:lt_return.
*-create delivery for sales order
CALL FUNCTION ‘BAPI_DELIVERYPROCESSING_EXEC’
TABLES
request      = lt_request
createditems = lt_created
return       = lt_return.
READ TABLE lt_return INTO ls_return WITH KEY type = lc_e.
IF sy-subrc NE 0.
CALL FUNCTION ‘BAPI_TRANSACTION_COMMIT’
EXPORTING
wait = abap_true.
rc = 0.
READ TABLE lt_created INTO ls_created INDEX 1.
IF sy-subrc EQ 0.
lv_msgv1 = ls_created-document_numb.
ENDIF.
lv_msgv2 = text-003.
PERFORM f_nast_update USING lc_msgid lc_msgno lc_s
lv_msgv2 lv_msgv1 space space.
ELSE.
rc = 4.
LOOP AT lt_return INTO ls_return.
PERFORM f_nast_update USING ls_return-id ls_return-number ls_return-type
ls_return-message_v1 ls_return-message_v2
ls_return-message_v3 ls_return-message_v4.
ENDLOOP.
ENDIF.
ENDIF.
ENDIF.
*-if delivery is created successfully
IF rc = 0.
LOOP AT lt_created INTO ls_created.
lv_vbeln = ls_created-document_numb.
ls_vbkok-vbeln_vl = ls_created-document_numb.
ls_vbkok-wabuc    = abap_true.
ls_vbpok-vbeln_vl = ls_created-document_numb.
ls_vbpok-posnr_vl = ls_created-document_item.
ls_vbpok-posnn    = ls_created-document_item.
ls_vbpok-vbeln    = ls_created-document_numb.
ls_vbpok-pikmg    = ls_created-quantity_sales_uom.
ls_vbpok-lfimg    = ls_created-quantity_sales_uom.
ls_vbpok-lgmng    = ls_created-quantity_sales_uom.
ls_vbpok-meins    = ls_created-sales_unit.
ls_vbpok-matnr    = ls_created-material.
APPEND ls_vbpok TO lt_vbpok.
CLEAR: ls_vbpok.
ENDLOOP.
*-update piking and do PGI
CALL FUNCTION ‘WS_DELIVERY_UPDATE’
EXPORTING
vbkok_wa                  = ls_vbkok
synchron                  = abap_true
commit                    = abap_true
delivery                  = lv_vbeln
update_picking            = abap_true
nicht_sperren             = ‘X’“abap_true
if_database_update        = ‘1’
IMPORTING
ef_error_in_goods_issue_0 = lv_check_gi
ef_error_in_final_check_0 = lv_check_fi
TABLES
vbpok_tab                 = lt_vbpok
prot                      = lt_prott.
READ TABLE lt_prott INTO ls_prott WITH KEY msgty = lc_e.
IF sy-subrc EQ 0.
rc = 4.
ELSEIF lv_check_gi EQ abap_true OR lv_check_fi EQ abap_true.
rc = 4.
ELSE.
CALL FUNCTION ‘BAPI_TRANSACTION_COMMIT’
EXPORTING
wait = abap_true.
rc = 0.
ENDIF.
LOOP AT lt_prott INTO ls_prott.
CLEAR lv_msgno.
lv_msgno = ls_prott-msgno.
PERFORM f_nast_update USING  ls_prott-msgid lv_msgno ls_prott-msgty
ls_prott-msgv1 ls_prott-msgv2
ls_prott-msgv3 ls_prott-msgv4.
ENDLOOP.
ENDIF.
ENDFORM.                    “f_bapi_call


*&———————————————————————*
*&      Form  F_NAST_UPDATE
*&———————————————————————*
*       text
*———————————————————————-*
FORM f_nast_update USING i_msgid TYPE sy-msgid
i_msgno TYPE sy-msgno
i_msgty TYPE sy-msgty
i_msgv1 TYPE sy-msgv1
i_msgv2 TYPE sy-msgv2
i_msgv3 TYPE sy-msgv3
i_msgv4 TYPE sy-msgv4.
CALL FUNCTION ‘NAST_PROTOCOL_UPDATE’
EXPORTING
msg_arbgb              = i_msgid
msg_nr                 = i_msgno
msg_ty                 = i_msgty
msg_v1                 = i_msgv1
msg_v2                 = i_msgv2
msg_v3                 = i_msgv3
msg_v4                 = i_msgv4
EXCEPTIONS
message_type_not_valid = 1
no_sy_message          = 2
OTHERS                 = 3.
IF sy-subrc <> 0.
* Implement suitable error handling here
ENDIF.
ENDFORM.                    “F_NAST_UPDATE

Requested output is achieved.

SAP SD, SAP ABAP Development, SAP ABAP Guides, SAP ABAP Certifications

No comments:

Post a Comment