Thursday, February 6, 2020

Using ABAP Classes to Modify Purchase Order Item Conditions

I found a document on scn which used Classes to  get Purchase order details here

http://wiki.sdn.sap.com/wiki/display/ABAP/Purchase+Order+Data+Extraction+using+ABAP+Classes

On the same line, I have created a program which shows how to modify Item Conditions using classes.
Same concept can be applied to modify other data.
***&---------------------------------------------------------------------*
***& Report  ZPO_CHANGE
***&
***&---------------------------------------------------------------------*
***&
***&
***&---------------------------------------------------------------------*
**
REPORT ZPO_CHANGE
**


TABLES : ekko.
TYPE-POOLS : mmpur, abap.

DATA : zcl_po TYPE REF TO cl_po_header_handle_mm.
DATA : zcl_item TYPE REF TO CL_PO_ITEM_HANDLE_MM.
DATA : ord_changed type mmpur_bool.
* Definition for header *
DATA : ls_document TYPE mepo_document,
        lv_result   TYPE mmpur_bool,
        ls_mepoheader TYPE mepoheader.

* Definition for Item *
DATA : lt_items      TYPE purchase_order_items,
        ls_items      TYPE purchase_order_item,
        lt_mepoitem   TYPE STANDARD TABLE OF mepoitem,
        ls_mepoitem   TYPE mepoitem.
DATA : l_item_conditions_changed TYPE mmpur_bool.

* Definition for Item Conditions *
DATA : lt_conditions TYPE mmpur_tkomv,
       lt_conditions1 TYPE mmpur_tkomv,
        ls_conditions TYPE komv,
        lt_mepocond   TYPE mmpur_tkomv.
DATA : lt_models    TYPE mmpur_models,
       ls_model     LIKE LINE OF lt_models.

PARAMETERS : p_ebeln TYPE ekko-ebeln.

MOVE mmpur_po_process TO ls_document-process.
MOVE 'V' TO ls_document-trtyp. " edit mode of Po
MOVE p_ebeln TO ls_document-doc_key(10).
MOVE mmpur_initiator_call TO ls_document-initiator-initiator.

* Create Object of Purchase Order Class *
CREATE OBJECT zcl_po
   EXPORTING
     im_po_number = p_ebeln
   EXCEPTIONS
     failure = 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.


IF zcl_po IS NOT INITIAL.


* Set the state to existing Purchase Order *
   zcl_po->set_state( im_state = cl_po_header_handle_mm=>c_available ).

   MOVE mmpur_yes TO zcl_po->for_bapi.
* Initialize the environment *
   zcl_po->po_initialize( im_document = ls_document ).


* Read PO document *
   zcl_po->po_read(
     EXPORTING
       im_tcode     = 'ME22N'
       im_trtyp     = ls_document-trtyp
       im_aktyp     = ls_document-trtyp
       im_po_number = p_ebeln
       im_document  = ls_document
     IMPORTING
       ex_result    = lv_result ).

   IF lv_result = abap_true.

* --------------------------------------------------*
* Extract PO Item Information *
* --------------------------------------------------*
     REFRESH : lt_items.
     lt_items = zcl_po->if_purchase_order_mm~get_items( ).

     LOOP AT lt_items INTO ls_items .

      zcl_item ?= ls_items-item.
      Zcl_item->my_parent ?= zcl_po.
  .
* -------------------------------------------------*
* Extract PO Item Conditions *
* --------------------------------------------------*
       CALL METHOD zcl_item->IF_PURCHASE_ORDER_ITEM_MM~GET_CONDITIONS(
         IMPORTING
           ex_conditions = lt_conditions ).
*Here if you delete a condition from the internal table it will get deleted in the PO as well.
       LOOP AT lt_conditions INTO ls_conditions WHERE KSCHL = 'YFR1'.
         ls_conditions-kwert = '1234'.
         MODIFY lt_conditions FROM ls_conditions TRANSPORTING kwert.

       ENDLOOP.

*     set  parameters for conditions
       zcl_po->my_ibs_firewall_on = mmpur_yes.


            CALL METHOD zcl_item->IF_PURCHASE_ORDER_ITEM_MM~SET_CONDITIONS(
               EXPORTING
               im_conditions = lt_conditions ).



** Add the Instance to the Generic MM Model Object
ls_model-model ?= zcl_item.
APPEND ls_model TO lt_models.

       ENDIF.

    ENDLOOP.



    
IF lt_models[] IS NOT INITIAL.



       CALL METHOD ZCL_PO->IF_FLUSH_TRANSPORT_MM~START
           EXPORTING
             IM_MODELS    = lt_models[]
           EXCEPTIONS
             ILLEGAL_CALL = 1
             ERROR        = 2
             OTHERS       = 3
              .
      IF SY-SUBRC <> 0.
       MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
                  WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
      ENDIF.

    ENDIF         .



               CALL METHOD ZCL_PO->PO_POST
                 EXPORTING
                   IM_UNCOMPLETE  = MMPUR_NO
                   IM_NO_COMMIT   = MMPUR_NO
                   IM_COMMIT_WAIT = MMPUR_yes
                 EXCEPTIONS
                   FAILURE        = 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 METHOD ZCL_PO->PO_CLOSE
               .


   ENDIF.

   ENDIF.