Monday 15 November 2021

Integration Program With PI and Automatic creation of Goods Receipt by using a BAPI for MIGO Transaction and sending the details to third party system

This Program will help to upload the data for MIGO Transaction and also to send the data to third party system by using PI

1. Creation of PI

2. Need to display the excel file as ALV output.

iii. In MIGO there are two operations that are performed to post a GR.

iv. a) Check Operation

v. b) Post Operation.

There are enhancements that are implemented during check and Post operations.

During the manual operations all the validations that are given in check operation and

post operation performed and GR will be posted. The same thing need to be performed

during the upload program also.

SAP ABAP Development, SAP ABAP Connectivity, SAP ABAP Exam, SAP ABAP Exam Prep, SAP ABAP Certification, SAP ABAP Career, SAP ABAP Guides

SAP ABAP Development, SAP ABAP Connectivity, SAP ABAP Exam, SAP ABAP Exam Prep, SAP ABAP Certification, SAP ABAP Career, SAP ABAP Guides
PI – Function module

Coding for DATA transfer from SAP to third Party System..

Can write the required validation and pass the values to Header and item table.

            CALL METHOD zcl_grn=>zif_grn~send_data
              EXPORTING
                lt_header = ls_header
*               lt_item1  = lt_item1.
                lt_item1  = lt_item2.
            CLEAR ls_header.
*          ENDIF.
            CLEAR lt_item2[].
            IF lt_item1[] IS NOT INITIAL AND lt_header[] IS NOT INITIAL.
              MODIFY zmti_grn_hdr FROM TABLE lt_header.
              MODIFY zmti_grn_itm FROM TABLE lt_item1.
              CLEAR : lt_item1[],lt_header[].
            ENDIF.
          ENDIF.

SAP ABAP Development, SAP ABAP Connectivity, SAP ABAP Exam, SAP ABAP Exam Prep, SAP ABAP Certification, SAP ABAP Career, SAP ABAP Guides

TYPE-POOLS: slis.
TYPE-POOLS: truxs,esp1.
TABLES: mkpf , zmt_033_mrp.

   """""""""""   Structure declaration for MIGO details""""
TYPES: BEGIN OF ty_field,
         bldat     TYPE mkpf-bldat,
         budat     TYPE imkpf-budat,
         ebeln     TYPE mseg-ebeln,
         ebelp     TYPE mseg-ebelp,
         lifex     TYPE imkpf-lifex,
         frbnr     TYPE imkpf-frbnr,
         matnr     TYPE mseg-matnr,
         menge     TYPE mseg-menge,
         licha     TYPE imseg-licha,
         vfdat     TYPE mseg-vfdat,
         exvkw     TYPE mseg-exvkw,
         zzrecdate TYPE mkpf-zzrecdate,
         zzreason  TYPE mkpf-zzreason,
         grn       TYPE mseg-exvkw,
       END OF ty_field.

TYPES: BEGIN OF ty_ekpo,
         ebeln TYPE ekpo-ebeln,
         ebelp TYPE ekpo-ebelp,
         txz01 TYPE ekpo-txz01,
         matnr TYPE ekpo-matnr,
         werks TYPE ekpo-werks,
         lgort TYPE ekpo-lgort,
         meins TYPE ekpo-meins,
         netpr TYPE ekpo-netpr,
       END OF ty_ekpo.

TYPES: BEGIN OF ty_final,
         bldat         TYPE imkpf-bldat,
         budat         TYPE imkpf-budat,
         ebeln         TYPE mseg-ebeln,
         ebelp         TYPE mseg-ebelp,
         lifex         TYPE imkpf-lifex,
         frbnr         TYPE imkpf-frbnr,
         matnr         TYPE zmt_033_mrp-zzmatnr,
         menge         TYPE mseg-menge,
         meins         TYPE mseg-meins,
         werks         TYPE mseg-werks,
         lgort         TYPE mseg-lgort,
         licha         TYPE imseg-licha,
         vfdat         TYPE mseg-vfdat,
         exvkw         TYPE mseg-exvkw,
         zzrecdate     TYPE mkpf-zzrecdate,
         zzreason      TYPE mkpf-zzreason,
         txz01         TYPE ekpo-txz01,
         grn           TYPE mseg-exvkw,
         check_flag(1) TYPE c,
       END OF ty_final.

TYPES: BEGIN OF ty_header.
        INCLUDE STRUCTURE bapi2017_gm_head_01.
TYPES: END OF ty_header.

TYPES: BEGIN OF ty_item.
        INCLUDE STRUCTURE bapi2017_gm_item_create.
TYPES:  END OF ty_item.

TYPES : BEGIN OF ty_errors,
          msgty(1)   TYPE c,
          ebeln      TYPE ebeln,
          ebelp      TYPE ebelp,
          matnr(18)  TYPE c,
          msgv1(120) TYPE c,
        END OF ty_errors.

TYPES : BEGIN OF ty_success,
          msg(1)        TYPE c,
          ebeln         TYPE ebeln,
          mblnr         TYPE mkpf-mblnr,
          mjahr         TYPE mkpf-mjahr,
          msg_text(120) TYPE c,
 END OF ty_success.
  TYPES: BEGIN OF gs_ekko
           ebeln TYPE c LENGTH 10, 
           waers TYPE ekko-waers,
           knumv TYPE ekko-knumv,  
         END OF gs_ekko.

  TYPES: BEGIN OF gs_ekpo,
           ebeln TYPE c LENGTH 10, "ekpo-ebeln,
           ebelp TYPE c LENGTH 5, 
           matnr TYPE ekpo-matnr,
           menge TYPE ekpo-menge, 
           netwr TYPE ekpo-netwr,
         END OF gs_ekpo.

  DATA: ht_ekko  TYPE TABLE OF gs_ekko,
        ht_ekko1 TYPE TABLE OF gs_ekko,

        hs_ekko  TYPE gs_ekko, 
        hs_ekko1 TYPE gs_ekko, 

        ht_ekpo  TYPE TABLE OF gs_ekpo,
        ht_ekpo1 TYPE TABLE OF gs_ekpo,

        ht_ekpo2 TYPE TABLE OF gs_ekpo,
        hs_ekpo  TYPE  gs_ekpo,

        hs_ekpo1 TYPE gs_ekpo,
        hs_ekpo2 TYPE gs_ekpo.

  TYPES: BEGIN OF ty_konv,
           knumv TYPE ekko-knumv,
           kposn TYPE konv-kposn,
           kschl TYPE konv-kschl,
           kwert TYPE konv-kwert,
         END OF ty_konv.

  DATA: lt_konv   TYPE TABLE OF ty_konv,
        lt_konv_c TYPE TABLE OF ty_konv,
        ls_konv   TYPE ty_konv.

  DATA: lv_knumv TYPE konv-knumv,
        lv_kposn TYPE konv-kposn.

  DATA:   lv_netwr  TYPE netwr,
          lv_po_val TYPE p DECIMALS 6, 
          lv_gr_val TYPE p DECIMALS 6.

DATA: it_field  TYPE TABLE OF ty_field,
      it_field1 TYPE TABLE OF ty_field,
      wa_field  TYPE ty_field,
      wa_field1 TYPE ty_field.

DATA: it_final TYPE TABLE OF ty_field,
      wa_final TYPE ty_field.
DATA: it_ekpo TYPE TABLE OF ty_ekpo,
      wa_ekpo TYPE ty_ekpo.
DATA: it_final_alv TYPE TABLE OF ty_final,
      wa_final_alv TYPE ty_final.


DATA : it_final_alv1 TYPE TABLE OF ty_final,
       wa_final_alv1 TYPE ty_final.

DATA : bapita-wait TYPE bapiwait,
       bapiret2    TYPE bapiret2,
       ret         TYPE TABLE OF  bapiret2,
       wa_ret      TYPE bapiret2.

DATA: p_date TYPE sy-datum.
DATA: c_date TYPE sy-datum.
DATA:  it_type   TYPE truxs_t_text_data.
CONSTANTS: cns_x TYPE char1 VALUE 'X'.

DATA: it_fcat       TYPE slis_t_fieldcat_alv,
      it_fcat_tab   TYPE slis_t_fieldcat_alv,
      wa_fcat       TYPE slis_fieldcat_alv,
      wa_fcat_tab   TYPE slis_fieldcat_alv,
      wa_layout     TYPE slis_layout_alv,
      wa_layout_tab TYPE slis_layout_alv.

DATA: it_sort TYPE slis_t_sortinfo_alv,
      wa_sort TYPE slis_sortinfo_alv..

TYPES: ty_t_field TYPE STANDARD TABLE OF ty_field.

DATA: lt_tab TYPE TABLE OF ty_errors,
      ls_tab TYPE ty_errors,
      lt_msg TYPE TABLE OF ty_success,
      ls_msg TYPE ty_success.

SELECTION-SCREEN BEGIN OF BLOCK b1 WITH FRAME TITLE text-001.
PARAMETERS : p_file TYPE rlgrap-filename OBLIGATORY.
PARAMETERS: p_zrecda TYPE mkpf-zzrecdate OBLIGATORY,
            p_zreaso TYPE  mkpf-zzreason MATCHCODE OBJECT zreason.
SELECTION-SCREEN END OF BLOCK b1.

INITIALIZATION.

"""""""Once GR is created on the double click of the genereated GR it will go
  to MIGO transaction in display mode""""

CLASS cl_handler DEFINITION.

  PUBLIC SECTION.
    METHODS on_double_click FOR EVENT double_click OF cl_salv_events_table
      IMPORTING row column.

ENDCLASS.

CLASS cl_handler IMPLEMENTATION.
  METHOD on_double_click.
    IF column = 'MBLNR'.
      CLEAR: ls_msg.
      READ TABLE lt_msg INTO ls_msg INDEX row.
      IF sy-subrc = 0.
        SET PARAMETER ID 'MBN' FIELD ls_msg-mblnr.
        DATA(gv_mblnr) = ls_msg-mblnr.
        DATA(gv_mjahr) = ls_msg-mjahr.
        CALL FUNCTION 'MIGO_DIALOG'
          EXPORTING
            i_action            = 'A04'
            i_refdoc            = 'R02'
            i_notree            = 'X'
            i_no_auth_check     = ' '
            i_skip_first_screen = 'X'
            i_deadend           = 'X'
            i_okcode            = 'OK_GO'
            i_leave_after_post  = ' '
            i_new_rollarea      = 'X'
*           I_SYTCODE           =
*           I_EBELN             =
*           I_EBELP             =
            i_mblnr             = gv_mblnr
            i_mjahr             = gv_mjahr
          EXCEPTIONS
            illegal_combination = 1
            OTHERS              = 2.
        IF sy-subrc <> 0.
* Implement suitable error handling here
        ENDIF.
      ENDIF.
    ENDIF.
  ENDMETHOD.
ENDCLASS.
*

"""""Selection screen to upload the excel file""""

AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_file.
  CALL FUNCTION 'F4_FILENAME'
    EXPORTING
      field_name = 'P_FILE'
    IMPORTING
      file_name  = p_file.

START-OF-SELECTION.
* Uploading the data in the file into internal table
  CALL FUNCTION 'TEXT_CONVERT_XLS_TO_SAP'
    EXPORTING
      i_line_header        = 'X'
      i_tab_raw_data       = it_type
      i_filename           = p_file
    TABLES
      i_tab_converted_data = it_field[]
    EXCEPTIONS
      conversion_failed    = 1
      OTHERS               = 2.
  IF sy-subrc <> 0.
* Implement suitable error handling here
    MESSAGE ID sy-msgid
TYPE sy-msgty
NUMBER sy-msgno
WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
  ENDIF.

  IF it_field IS NOT INITIAL.

    SELECT ebeln
           ebelp
           txz01
           matnr
           werks
           lgort
           meins
           netpr FROM ekpo INTO TABLE it_ekpo FOR ALL ENTRIES IN it_field
    WHERE ebeln = it_field-ebeln. "
  ENDIF.
  SORT it_ekpo BY ebeln ebelp.
  
 SELECT  ebeln
          waers
          knumv FROM ekko INTO TABLE ht_ekko FOR ALL ENTRIES IN it_field
                                             WHERE ebeln = it_field-ebeln.

  SELECT ebeln
         ebelp
         matnr
         menge
         netwr FROM ekpo INTO TABLE ht_ekpo FOR ALL ENTRIES IN it_field WHERE         ebeln = it_field-ebeln.

  REFRESH: it_field1, ht_ekko1, ht_ekpo2.
  CLEAR: wa_field1, hs_ekko1, hs_ekpo2.

  it_field1 = it_field.
  ht_ekko1  = ht_ekko.
  ht_ekpo2  = ht_ekpo.

 LOOP AT  it_field INTO wa_field.
    CLEAR: lv_knumv, lv_kposn.

    READ TABLE ht_ekko INTO hs_ekko WITH KEY ebeln = wa_field-ebeln.
    lv_knumv = hs_ekko-knumv.
  
  DATA: ht_matnr TYPE mara-matnr.
    ht_matnr = wa_field-matnr.

    REPLACE ALL OCCURRENCES OF REGEX '-' IN ht_matnr WITH ''.
    CALL FUNCTION 'CONVERSION_EXIT_ALPHA_INPUT'
      EXPORTING
        input  = ht_matnr 
      IMPORTING
        output = ht_matnr. 

    READ TABLE ht_ekpo INTO hs_ekpo WITH KEY ebeln = wa_field-ebeln
                                             ebelp = wa_field-ebelp
                                             matnr = ht_matnr
    IF hs_ekpo IS INITIAL.

      READ TABLE ht_ekpo INTO hs_ekpo1 WITH KEY ebeln = wa_field-ebeln
                                               matnr = ht_matnr
    ENDIF.

    wa_final_alv-bldat         =  wa_field-bldat.
    wa_final_alv-budat         =  wa_field-budat.
    wa_final_alv-ebeln         =  wa_field-ebeln.

    IF hs_ekpo IS NOT INITIAL.
      wa_final_alv-ebelp         =  hs_ekpo-ebelp."wa_field-ebelp.
    ELSE.
      wa_final_alv-ebelp =        hs_ekpo1-ebelp.
    ENDIF.

    wa_final_alv-lifex         =  wa_field-lifex.
    wa_final_alv-frbnr         =  wa_field-frbnr.
    wa_final_alv-matnr         =  wa_field-matnr.
    wa_final_alv-menge         =  wa_field-menge.
    wa_final_alv-licha         =  wa_field-licha.
    wa_final_alv-vfdat         =  wa_field-vfdat.
    wa_final_alv-exvkw         =  wa_field-exvkw.
    wa_final_alv-zzrecdate     =  p_zrecda.

    c_date = sy-datum.
    p_date = c_date - 3.
    IF p_zrecda < p_date.
      wa_final_alv-zzreason      =  p_zreaso.
    ENDIF.

    READ TABLE it_ekpo INTO wa_ekpo WITH KEY ebeln = wa_final_alv-ebeln
                                             ebelp = wa_final_alv-ebelp.
    wa_final_alv-txz01  =  wa_ekpo-txz01.

    wa_final_alv-meins  =  wa_ekpo-meins.
    wa_final_alv-werks  =  wa_ekpo-werks.
    wa_final_alv-lgort  =  wa_ekpo-lgort.
    APPEND wa_final_alv TO it_final_alv.
    CLEAR: wa_final_alv, hs_ekpo, hs_ekpo1 .
  ENDLOOP.

  LOOP AT it_final_alv INTO wa_final_alv.
    READ TABLE ht_ekko1 INTO hs_ekko1 WITH KEY ebeln = wa_final_alv-ebeln.
    IF sy-subrc EQ 0.
      READ TABLE ht_ekpo2 INTO hs_ekpo2 WITH KEY ebeln = wa_final_alv-ebeln
                                                 ebelp = wa_final_alv-ebelp.
      IF  sy-subrc EQ 0.
        ls_konv-knumv = hs_ekko1-knumv.
        ls_konv-kposn = hs_ekpo2-ebelp.
        APPEND ls_konv TO lt_konv_c.
        CLEAR ls_konv.

      ENDIF.
    ENDIF.
  ENDLOOP.
  SORT lt_konv_c BY knumv kposn.
  DELETE ADJACENT DUPLICATES FROM lt_konv_c COMPARING knumv kposn.
  SELECT knumv
         kposn
         kschl
         kwert
    FROM konv
    INTO TABLE lt_konv
    FOR ALL ENTRIES IN lt_konv_c
    WHERE knumv = lt_konv_c-knumv
      AND kposn = lt_konv_c-kposn.

  LOOP AT it_final_alv INTO wa_final_alv.
    READ TABLE ht_ekpo2 INTO hs_ekpo2 WITH KEY ebeln = wa_final_alv-ebeln
                                             ebelp = wa_final_alv-ebelp.
    IF sy-subrc = 0.
      CLEAR hs_ekko1.
      READ TABLE ht_ekko1 INTO hs_ekko1 WITH KEY ebeln = hs_ekpo2-ebeln.
      IF sy-subrc = 0.
        ls_konv-kposn = wa_final_alv-ebelp.
        CLEAR : hs_ekpo2-netwr,
                lv_po_val.
        LOOP AT lt_konv INTO ls_konv WHERE knumv = hs_ekko1-knumv
                                       AND kposn = hs_ekpo2-ebelp.
          hs_ekpo2-netwr = hs_ekpo2-netwr + ls_konv-kwert.
        ENDLOOP.
        IF hs_ekko1-waers NE 'INR'.
          CALL FUNCTION 'CONVERT_AMOUNT_TO_CURRENCY'
            EXPORTING
              date             = wa_final_alv-budat
              foreign_currency = hs_ekko1-waers
              foreign_amount   = hs_ekpo2-netwr
              local_currency   = 'INR'
            IMPORTING
              local_amount     = lv_netwr
            EXCEPTIONS
              error            = 1
              OTHERS           = 2.
          IF sy-subrc NE 0.
            MOVE: hs_ekpo2-netwr TO lv_netwr.
          ENDIF.
        ELSE.
          MOVE: hs_ekpo2-netwr TO lv_netwr.
        ENDIF.
        lv_po_val = lv_netwr / hs_ekpo2-menge.
        lv_po_val = lv_po_val * wa_final_alv-menge.
        wa_final_alv-grn = wa_final_alv-grn + lv_po_val.

        CLEAR lv_po_val.

        MODIFY it_final_alv FROM wa_final_alv.
        CLEAR: wa_final_alv.
      ENDIF.
    ENDIF.
  ENDLOOP.

  """""""Field catelog for ALV output"""""

  IF it_final_alv IS NOT INITIAL.
    wa_layout-colwidth_optimize = 'X'.
    wa_fcat-fieldname = 'BLDAT'.
    wa_fcat-tabname = 'IT_FINAL_ALV'.
    wa_fcat-col_pos = 2.
    wa_fcat-seltext_m = 'Document Date'.
    APPEND wa_fcat TO it_fcat.
    CLEAR wa_fcat.

    wa_fcat-fieldname = 'BUDAT'.
    wa_fcat-tabname = 'IT_FINAL_ALV'.
    wa_fcat-col_pos = 3.
    wa_fcat-seltext_m = 'Posting Date'.
    APPEND wa_fcat TO it_fcat.
    CLEAR wa_fcat.

    wa_fcat-fieldname = 'EBELN'.
    wa_fcat-tabname = 'IT_FINAL_ALV'.
    wa_fcat-col_pos = 4.
    wa_fcat-seltext_m = 'Purchase Doc Number'.
    APPEND wa_fcat TO it_fcat.
    CLEAR wa_fcat.

    wa_fcat-fieldname = 'EBELP'.
    wa_fcat-tabname = 'IT_FINAL_ALV'.
    wa_fcat-col_pos = 5.
    wa_fcat-seltext_m = 'Purchase Doc Item'.
    APPEND wa_fcat TO it_fcat.
    CLEAR wa_fcat.

    wa_fcat-fieldname = 'LIFEX'.
    wa_fcat-tabname = 'IT_FINAL_ALV'.
    wa_fcat-col_pos = 6.
    wa_fcat-seltext_m = 'Delivery Note'.
    APPEND wa_fcat TO it_fcat.
    CLEAR wa_fcat.

    wa_fcat-fieldname = 'FRBNR'.
    wa_fcat-tabname = 'IT_FINAL_ALV'.
    wa_fcat-col_pos = 7.
    wa_fcat-seltext_m = 'Bill of Landing'.
    APPEND wa_fcat TO it_fcat.
    CLEAR wa_fcat.

    wa_fcat-fieldname = 'MATNR'.
    wa_fcat-tabname = 'IT_FINAL_ALV'.
    wa_fcat-col_pos = 8.
    wa_fcat-seltext_m = 'Material Number'.
    APPEND wa_fcat TO it_fcat.
    CLEAR wa_fcat.

    wa_fcat-fieldname = 'TXZ01'.
    wa_fcat-tabname = 'IT_FINAL_ALV'.
    wa_fcat-col_pos = 9.
    wa_fcat-seltext_m = 'Material Description'.
    APPEND wa_fcat TO it_fcat.
    CLEAR wa_fcat.

    wa_fcat-fieldname = 'MENGE'.
    wa_fcat-tabname = 'IT_FINAL_ALV'.
    wa_fcat-col_pos = 10.
    wa_fcat-seltext_m = 'Qty'.
    APPEND wa_fcat TO it_fcat.
    CLEAR wa_fcat.

    wa_fcat-fieldname = 'MEINS'.
    wa_fcat-tabname = 'IT_FINAL_ALV'.
    wa_fcat-col_pos = 11.
    wa_fcat-seltext_m = 'Units'.
    APPEND wa_fcat TO it_fcat.
    CLEAR wa_fcat.

    wa_fcat-fieldname = 'WERKS'.
    wa_fcat-tabname = 'IT_FINAL_ALV'.
    wa_fcat-col_pos = 12.
    wa_fcat-seltext_m = 'Plant'.
    APPEND wa_fcat TO it_fcat.
    CLEAR wa_fcat.

    wa_fcat-fieldname = 'LGORT'.
    wa_fcat-tabname = 'IT_FINAL_ALV'.
    wa_fcat-col_pos = 13.
    wa_fcat-seltext_m = 'Storage Location'.
    APPEND wa_fcat TO it_fcat.
    CLEAR wa_fcat.

    wa_fcat-fieldname = 'LICHA'.
    wa_fcat-tabname = 'IT_FINAL_ALV'.
    wa_fcat-col_pos = 14.
    wa_fcat-seltext_m = 'Vendor Batch'.
    APPEND wa_fcat TO it_fcat.
    CLEAR wa_fcat.

    wa_fcat-fieldname = 'VFDAT'.
    wa_fcat-tabname = 'IT_FINAL_ALV'.
    wa_fcat-col_pos = 15.
    wa_fcat-seltext_m = 'Shelf Date'.
    APPEND wa_fcat TO it_fcat.
    CLEAR wa_fcat.

    wa_fcat-fieldname = 'EXVKW'.
    wa_fcat-tabname = 'IT_FINAL_ALV'.
    wa_fcat-col_pos = 16.
*    wa_fcat-do_sum  = 'X'.
    wa_fcat-seltext_m = 'MRP'.
    APPEND wa_fcat TO it_fcat.
    CLEAR wa_fcat.

    wa_fcat-fieldname = 'ZZRECDATE'.
    wa_fcat-tabname = 'IT_FINAL_ALV'.
    wa_fcat-col_pos = 17.
    wa_fcat-seltext_m = 'Security Date'.
    APPEND wa_fcat TO it_fcat.
    CLEAR wa_fcat.

    wa_fcat-fieldname = 'ZZREASON'.
    wa_fcat-tabname = 'IT_FINAL_ALV'.
    wa_fcat-col_pos = 18.
    wa_fcat-seltext_m = 'Reason'.
    APPEND wa_fcat TO it_fcat.
    CLEAR wa_fcat.

    wa_fcat-fieldname = 'GRN'.
    wa_fcat-tabname = 'IT_FINAL_ALV'.
    wa_fcat-col_pos = 18.
    wa_fcat-do_sum  = 'X'.
    wa_fcat-seltext_m = 'GRN Value'.
    APPEND wa_fcat TO it_fcat.
    CLEAR wa_fcat.

    wa_sort-spos = '01' .
    wa_sort-fieldname = 'EBELN'.
    wa_sort-tabname = 'IT_FINAL_ALV'.
    wa_sort-up = 'X'.
    wa_sort-subtot = 'X'.
    APPEND wa_sort TO it_sort .
    CLEAR wa_sort.

    DEFINE add_fcat.
      wa_fcat-col_pos = &1.
      wa_fcat-fieldname = &2.
      wa_fcat-outputlen = &3.
      wa_fcat-seltext_s =
      wa_fcat-seltext_m =
      wa_fcat-seltext_l = &4.
      wa_fcat-checkbox  = &5.
      wa_fcat-edit = &6.
      APPEND wa_fcat to it_fcat.
      CLEAR wa_fcat.
    END-OF-DEFINITION.
    CALL FUNCTION 'REUSE_ALV_FIELDCATALOG_MERGE'
      EXPORTING
        i_structure_name       = 'IT_FINAL_ALV'
      CHANGING
        ct_fieldcat            = it_fcat
*       it_sort                = it_sort
      EXCEPTIONS
        inconsistent_interface = 1
        program_error          = 2
        OTHERS                 = 3.
    IF sy-subrc = 0.
      add_fcat 1 'CHECK_FLAG' 10 'Check' 'X' 'X'.
    ENDIF.
  ENDIF.
""""""""Displaying the ALV output """"""""

  IF it_final_alv IS NOT INITIAL.
    CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
      EXPORTING
        i_callback_program       = sy-cprog
        i_callback_pf_status_set = 'PF_STATUS'
        i_callback_user_command  = 'ACTION'
        is_layout                = wa_layout
        it_fieldcat              = it_fcat
        it_sort                  = it_sort
      TABLES
        t_outtab                 = it_final_alv
      EXCEPTIONS
        program_error            = 1
        OTHERS                   = 2.
    IF sy-subrc <> 0.
* Implement suitable error handling here
    ENDIF.
  ENDIF.
FORM pf_status USING rt_exclude TYPE kkblo_t_extab..
  SET PF-STATUS 'ALV_STAT'.

ENDFORM. "SET_PF_STATUS1

FORM action USING r_ucomm LIKE sy-ucomm
                  rs_selfield TYPE slis_selfield.

  CLEAR: lt_tab.
  DATA : lr_grid TYPE REF TO cl_gui_alv_grid.

  CALL FUNCTION 'GET_GLOBALS_FROM_SLVC_FULLSCR'
    IMPORTING
      e_grid = lr_grid.

  CALL METHOD lr_grid->check_changed_data.

"""""""" Validations for POST Operations""""
  IF r_ucomm = 'POST'.
      
     Can write all the validations required during the POST operation
Note:: Please add the validations during the check operations and post operations here because if the user is giving directly the post operation  the also it needs to check the validations given in the check operation

""""""""""" BAPI to Create the GR if there are no errors""""
        CALL FUNCTION 'BAPI_GOODSMVT_CREATE'
          EXPORTING
            goodsmvt_header  = wa_header
            goodsmvt_code    = '01'
*           TESTRUN          = 'X'  
          IMPORTING
            materialdocument = lv_mblnr
            matdocumentyear  = lv_mjahr
          TABLES
            goodsmvt_item    = it_item
            return           = ret
            extensionin      = it_exn.

        CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'
          EXPORTING
            wait   = bapita-wait
          IMPORTING
            return = bapiret2.
 
TYPE-POOLS: slis.
TYPE-POOLS: truxs,esp1.
TABLES: mkpf , zmt_033_mrp.

   """""""""""   Structure declaration for MIGO details""""
TYPES: BEGIN OF ty_field,
         bldat     TYPE mkpf-bldat,
         budat     TYPE imkpf-budat,
         ebeln     TYPE mseg-ebeln,
         ebelp     TYPE mseg-ebelp,
         lifex     TYPE imkpf-lifex,
         frbnr     TYPE imkpf-frbnr,
         matnr     TYPE mseg-matnr,
         menge     TYPE mseg-menge,
         licha     TYPE imseg-licha,
         vfdat     TYPE mseg-vfdat,
         exvkw     TYPE mseg-exvkw,
         zzrecdate TYPE mkpf-zzrecdate,
         zzreason  TYPE mkpf-zzreason,
         grn       TYPE mseg-exvkw,
       END OF ty_field.

TYPES: BEGIN OF ty_ekpo,
         ebeln TYPE ekpo-ebeln,
         ebelp TYPE ekpo-ebelp,
         txz01 TYPE ekpo-txz01,
         matnr TYPE ekpo-matnr,
         werks TYPE ekpo-werks,
         lgort TYPE ekpo-lgort,
         meins TYPE ekpo-meins,
         netpr TYPE ekpo-netpr,
       END OF ty_ekpo.

TYPES: BEGIN OF ty_final,
         bldat         TYPE imkpf-bldat,
         budat         TYPE imkpf-budat,
         ebeln         TYPE mseg-ebeln,
         ebelp         TYPE mseg-ebelp,
         lifex         TYPE imkpf-lifex,
         frbnr         TYPE imkpf-frbnr,
         matnr         TYPE zmt_033_mrp-zzmatnr,
         menge         TYPE mseg-menge,
         meins         TYPE mseg-meins,
         werks         TYPE mseg-werks,
         lgort         TYPE mseg-lgort,
         licha         TYPE imseg-licha,
         vfdat         TYPE mseg-vfdat,
         exvkw         TYPE mseg-exvkw,
         zzrecdate     TYPE mkpf-zzrecdate,
         zzreason      TYPE mkpf-zzreason,
         txz01         TYPE ekpo-txz01,
         grn           TYPE mseg-exvkw,
         check_flag(1) TYPE c,
       END OF ty_final.

TYPES: BEGIN OF ty_header.
        INCLUDE STRUCTURE bapi2017_gm_head_01.
TYPES: END OF ty_header.

TYPES: BEGIN OF ty_item.
        INCLUDE STRUCTURE bapi2017_gm_item_create.
TYPES:  END OF ty_item.

TYPES : BEGIN OF ty_errors,
          msgty(1)   TYPE c,
          ebeln      TYPE ebeln,
          ebelp      TYPE ebelp,
          matnr(18)  TYPE c,
          msgv1(120) TYPE c,
        END OF ty_errors.

TYPES : BEGIN OF ty_success,
          msg(1)        TYPE c,
          ebeln         TYPE ebeln,
          mblnr         TYPE mkpf-mblnr,
          mjahr         TYPE mkpf-mjahr,
          msg_text(120) TYPE c,
 END OF ty_success.
  TYPES: BEGIN OF gs_ekko
           ebeln TYPE c LENGTH 10, 
           waers TYPE ekko-waers,
           knumv TYPE ekko-knumv,  
         END OF gs_ekko.

  TYPES: BEGIN OF gs_ekpo,
           ebeln TYPE c LENGTH 10, "ekpo-ebeln,
           ebelp TYPE c LENGTH 5, 
           matnr TYPE ekpo-matnr,
           menge TYPE ekpo-menge, 
           netwr TYPE ekpo-netwr,
         END OF gs_ekpo.

  DATA: ht_ekko  TYPE TABLE OF gs_ekko,
        ht_ekko1 TYPE TABLE OF gs_ekko,

        hs_ekko  TYPE gs_ekko, 
        hs_ekko1 TYPE gs_ekko, 

        ht_ekpo  TYPE TABLE OF gs_ekpo,
        ht_ekpo1 TYPE TABLE OF gs_ekpo,

        ht_ekpo2 TYPE TABLE OF gs_ekpo,
        hs_ekpo  TYPE  gs_ekpo,

        hs_ekpo1 TYPE gs_ekpo,
        hs_ekpo2 TYPE gs_ekpo.

  TYPES: BEGIN OF ty_konv,
           knumv TYPE ekko-knumv,
           kposn TYPE konv-kposn,
           kschl TYPE konv-kschl,
           kwert TYPE konv-kwert,
         END OF ty_konv.

  DATA: lt_konv   TYPE TABLE OF ty_konv,
        lt_konv_c TYPE TABLE OF ty_konv,
        ls_konv   TYPE ty_konv.

  DATA: lv_knumv TYPE konv-knumv,
        lv_kposn TYPE konv-kposn.

  DATA:   lv_netwr  TYPE netwr,
          lv_po_val TYPE p DECIMALS 6, 
          lv_gr_val TYPE p DECIMALS 6.

DATA: it_field  TYPE TABLE OF ty_field,
      it_field1 TYPE TABLE OF ty_field,
      wa_field  TYPE ty_field,
      wa_field1 TYPE ty_field.

DATA: it_final TYPE TABLE OF ty_field,
      wa_final TYPE ty_field.
DATA: it_ekpo TYPE TABLE OF ty_ekpo,
      wa_ekpo TYPE ty_ekpo.
DATA: it_final_alv TYPE TABLE OF ty_final,
      wa_final_alv TYPE ty_final.

DATA : it_final_alv1 TYPE TABLE OF ty_final,
       wa_final_alv1 TYPE ty_final.

DATA : bapita-wait TYPE bapiwait,
       bapiret2    TYPE bapiret2,
       ret         TYPE TABLE OF  bapiret2,
       wa_ret      TYPE bapiret2.

DATA: p_date TYPE sy-datum.
DATA: c_date TYPE sy-datum.
DATA:  it_type   TYPE truxs_t_text_data.
CONSTANTS: cns_x TYPE char1 VALUE 'X'.

DATA: it_fcat       TYPE slis_t_fieldcat_alv,
      it_fcat_tab   TYPE slis_t_fieldcat_alv,
      wa_fcat       TYPE slis_fieldcat_alv,
      wa_fcat_tab   TYPE slis_fieldcat_alv,
      wa_layout     TYPE slis_layout_alv,
      wa_layout_tab TYPE slis_layout_alv.

DATA: it_sort TYPE slis_t_sortinfo_alv,
      wa_sort TYPE slis_sortinfo_alv..

TYPES: ty_t_field TYPE STANDARD TABLE OF ty_field.

DATA: lt_tab TYPE TABLE OF ty_errors,
      ls_tab TYPE ty_errors,
      lt_msg TYPE TABLE OF ty_success,
      ls_msg TYPE ty_success.

SELECTION-SCREEN BEGIN OF BLOCK b1 WITH FRAME TITLE text-001.
PARAMETERS : p_file TYPE rlgrap-filename OBLIGATORY.
PARAMETERS: p_zrecda TYPE mkpf-zzrecdate OBLIGATORY,
            p_zreaso TYPE  mkpf-zzreason MATCHCODE OBJECT zreason.
SELECTION-SCREEN END OF BLOCK b1.

INITIALIZATION.

"""""""Once GR is created on the double click of the genereated GR it will go
  to MIGO transaction in display mode""""

CLASS cl_handler DEFINITION.

  PUBLIC SECTION.
    METHODS on_double_click FOR EVENT double_click OF cl_salv_events_table
      IMPORTING row column.

ENDCLASS.

CLASS cl_handler IMPLEMENTATION.
  METHOD on_double_click.
    IF column = 'MBLNR'.
      CLEAR: ls_msg.
      READ TABLE lt_msg INTO ls_msg INDEX row.
      IF sy-subrc = 0.
        SET PARAMETER ID 'MBN' FIELD ls_msg-mblnr.
        DATA(gv_mblnr) = ls_msg-mblnr.
        DATA(gv_mjahr) = ls_msg-mjahr.
        CALL FUNCTION 'MIGO_DIALOG'
          EXPORTING
            i_action            = 'A04'
            i_refdoc            = 'R02'
            i_notree            = 'X'
            i_no_auth_check     = ' '
            i_skip_first_screen = 'X'
            i_deadend           = 'X'
            i_okcode            = 'OK_GO'
            i_leave_after_post  = ' '
            i_new_rollarea      = 'X'
*           I_SYTCODE           =
*           I_EBELN             =
*           I_EBELP             =
            i_mblnr             = gv_mblnr
            i_mjahr             = gv_mjahr
          EXCEPTIONS
            illegal_combination = 1
            OTHERS              = 2.
        IF sy-subrc <> 0.
* Implement suitable error handling here
        ENDIF.
      ENDIF.
    ENDIF.
  ENDMETHOD.
ENDCLASS.
*

"""""Selection screen to upload the excel file""""

AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_file.
  CALL FUNCTION 'F4_FILENAME'
    EXPORTING
      field_name = 'P_FILE'
    IMPORTING
      file_name  = p_file.

START-OF-SELECTION.
* Uploading the data in the file into internal table
  CALL FUNCTION 'TEXT_CONVERT_XLS_TO_SAP'
    EXPORTING
      i_line_header        = 'X'
      i_tab_raw_data       = it_type
      i_filename           = p_file
    TABLES
      i_tab_converted_data = it_field[]
    EXCEPTIONS
      conversion_failed    = 1
      OTHERS               = 2.
  IF sy-subrc <> 0.
* Implement suitable error handling here
    MESSAGE ID sy-msgid
TYPE sy-msgty
NUMBER sy-msgno
WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
  ENDIF.

  IF it_field IS NOT INITIAL.

    SELECT ebeln
           ebelp
           txz01
           matnr
           werks
           lgort
           meins
           netpr FROM ekpo INTO TABLE it_ekpo FOR ALL ENTRIES IN it_field
    WHERE ebeln = it_field-ebeln. "
  ENDIF.
  SORT it_ekpo BY ebeln ebelp.
  
 SELECT  ebeln
          waers
          knumv FROM ekko INTO TABLE ht_ekko FOR ALL ENTRIES IN it_field
                                             WHERE ebeln = it_field-ebeln.

  SELECT ebeln
         ebelp
         matnr
         menge
         netwr FROM ekpo INTO TABLE ht_ekpo FOR ALL ENTRIES IN it_field WHERE         ebeln = it_field-ebeln.

  REFRESH: it_field1, ht_ekko1, ht_ekpo2.
  CLEAR: wa_field1, hs_ekko1, hs_ekpo2.

  it_field1 = it_field.
  ht_ekko1  = ht_ekko.
  ht_ekpo2  = ht_ekpo.

 LOOP AT  it_field INTO wa_field.
    CLEAR: lv_knumv, lv_kposn.

    READ TABLE ht_ekko INTO hs_ekko WITH KEY ebeln = wa_field-ebeln.
    lv_knumv = hs_ekko-knumv.
  
  DATA: ht_matnr TYPE mara-matnr.
    ht_matnr = wa_field-matnr.

    REPLACE ALL OCCURRENCES OF REGEX '-' IN ht_matnr WITH ''.
    CALL FUNCTION 'CONVERSION_EXIT_ALPHA_INPUT'
      EXPORTING
        input  = ht_matnr 
      IMPORTING
        output = ht_matnr. 

    READ TABLE ht_ekpo INTO hs_ekpo WITH KEY ebeln = wa_field-ebeln
                                             ebelp = wa_field-ebelp
                                             matnr = ht_matnr
    IF hs_ekpo IS INITIAL.

      READ TABLE ht_ekpo INTO hs_ekpo1 WITH KEY ebeln = wa_field-ebeln
                                               matnr = ht_matnr
    ENDIF.

    wa_final_alv-bldat         =  wa_field-bldat.
    wa_final_alv-budat         =  wa_field-budat.
    wa_final_alv-ebeln         =  wa_field-ebeln.

    IF hs_ekpo IS NOT INITIAL.
      wa_final_alv-ebelp         =  hs_ekpo-ebelp."wa_field-ebelp.
    ELSE.
      wa_final_alv-ebelp =        hs_ekpo1-ebelp.
    ENDIF.

    wa_final_alv-lifex         =  wa_field-lifex.
    wa_final_alv-frbnr         =  wa_field-frbnr.
    wa_final_alv-matnr         =  wa_field-matnr.
    wa_final_alv-menge         =  wa_field-menge.
    wa_final_alv-licha         =  wa_field-licha.
    wa_final_alv-vfdat         =  wa_field-vfdat.
    wa_final_alv-exvkw         =  wa_field-exvkw.
    wa_final_alv-zzrecdate     =  p_zrecda.

    c_date = sy-datum.
    p_date = c_date - 3.
    IF p_zrecda < p_date.
      wa_final_alv-zzreason      =  p_zreaso.
    ENDIF.

    READ TABLE it_ekpo INTO wa_ekpo WITH KEY ebeln = wa_final_alv-ebeln
                                             ebelp = wa_final_alv-ebelp.
    wa_final_alv-txz01  =  wa_ekpo-txz01.

    wa_final_alv-meins  =  wa_ekpo-meins.
    wa_final_alv-werks  =  wa_ekpo-werks.
    wa_final_alv-lgort  =  wa_ekpo-lgort.
    APPEND wa_final_alv TO it_final_alv.
    CLEAR: wa_final_alv, hs_ekpo, hs_ekpo1 .
  ENDLOOP.

  LOOP AT it_final_alv INTO wa_final_alv.
    READ TABLE ht_ekko1 INTO hs_ekko1 WITH KEY ebeln = wa_final_alv-ebeln.
    IF sy-subrc EQ 0.
      READ TABLE ht_ekpo2 INTO hs_ekpo2 WITH KEY ebeln = wa_final_alv-ebeln
                                                 ebelp = wa_final_alv-ebelp.
      IF  sy-subrc EQ 0.
        ls_konv-knumv = hs_ekko1-knumv.
        ls_konv-kposn = hs_ekpo2-ebelp.
        APPEND ls_konv TO lt_konv_c.
        CLEAR ls_konv.

      ENDIF.
    ENDIF.
  ENDLOOP.
  SORT lt_konv_c BY knumv kposn.
  DELETE ADJACENT DUPLICATES FROM lt_konv_c COMPARING knumv kposn.
  SELECT knumv
         kposn
         kschl
         kwert
    FROM konv
    INTO TABLE lt_konv
    FOR ALL ENTRIES IN lt_konv_c
    WHERE knumv = lt_konv_c-knumv
      AND kposn = lt_konv_c-kposn.

  LOOP AT it_final_alv INTO wa_final_alv.
    READ TABLE ht_ekpo2 INTO hs_ekpo2 WITH KEY ebeln = wa_final_alv-ebeln
                                             ebelp = wa_final_alv-ebelp.
    IF sy-subrc = 0.
      CLEAR hs_ekko1.
      READ TABLE ht_ekko1 INTO hs_ekko1 WITH KEY ebeln = hs_ekpo2-ebeln.
      IF sy-subrc = 0.
        ls_konv-kposn = wa_final_alv-ebelp.
        CLEAR : hs_ekpo2-netwr,
                lv_po_val.
        LOOP AT lt_konv INTO ls_konv WHERE knumv = hs_ekko1-knumv
                                       AND kposn = hs_ekpo2-ebelp.
          hs_ekpo2-netwr = hs_ekpo2-netwr + ls_konv-kwert.
        ENDLOOP.
        IF hs_ekko1-waers NE 'INR'.
          CALL FUNCTION 'CONVERT_AMOUNT_TO_CURRENCY'
            EXPORTING
              date             = wa_final_alv-budat
              foreign_currency = hs_ekko1-waers
              foreign_amount   = hs_ekpo2-netwr
              local_currency   = 'INR'
            IMPORTING
              local_amount     = lv_netwr
            EXCEPTIONS
              error            = 1
              OTHERS           = 2.
          IF sy-subrc NE 0.
            MOVE: hs_ekpo2-netwr TO lv_netwr.
          ENDIF.
        ELSE.
          MOVE: hs_ekpo2-netwr TO lv_netwr.
        ENDIF.
        lv_po_val = lv_netwr / hs_ekpo2-menge.
        lv_po_val = lv_po_val * wa_final_alv-menge.
        wa_final_alv-grn = wa_final_alv-grn + lv_po_val.

        CLEAR lv_po_val.

        MODIFY it_final_alv FROM wa_final_alv.
        CLEAR: wa_final_alv.
      ENDIF.
    ENDIF.
  ENDLOOP.

  """""""Field catelog for ALV output"""""

  IF it_final_alv IS NOT INITIAL.
    wa_layout-colwidth_optimize = 'X'.
    wa_fcat-fieldname = 'BLDAT'.
    wa_fcat-tabname = 'IT_FINAL_ALV'.
    wa_fcat-col_pos = 2.
    wa_fcat-seltext_m = 'Document Date'.
    APPEND wa_fcat TO it_fcat.
    CLEAR wa_fcat.

    wa_fcat-fieldname = 'BUDAT'.
    wa_fcat-tabname = 'IT_FINAL_ALV'.
    wa_fcat-col_pos = 3.
    wa_fcat-seltext_m = 'Posting Date'.
    APPEND wa_fcat TO it_fcat.
    CLEAR wa_fcat.

    wa_fcat-fieldname = 'EBELN'.
    wa_fcat-tabname = 'IT_FINAL_ALV'.
    wa_fcat-col_pos = 4.
    wa_fcat-seltext_m = 'Purchase Doc Number'.
    APPEND wa_fcat TO it_fcat.
    CLEAR wa_fcat.

    wa_fcat-fieldname = 'EBELP'.
    wa_fcat-tabname = 'IT_FINAL_ALV'.
    wa_fcat-col_pos = 5.
    wa_fcat-seltext_m = 'Purchase Doc Item'.
    APPEND wa_fcat TO it_fcat.
    CLEAR wa_fcat.

    wa_fcat-fieldname = 'LIFEX'.
    wa_fcat-tabname = 'IT_FINAL_ALV'.
    wa_fcat-col_pos = 6.
    wa_fcat-seltext_m = 'Delivery Note'.
    APPEND wa_fcat TO it_fcat.
    CLEAR wa_fcat.

    wa_fcat-fieldname = 'FRBNR'.
    wa_fcat-tabname = 'IT_FINAL_ALV'.
    wa_fcat-col_pos = 7.
    wa_fcat-seltext_m = 'Bill of Landing'.
    APPEND wa_fcat TO it_fcat.
    CLEAR wa_fcat.

    wa_fcat-fieldname = 'MATNR'.
    wa_fcat-tabname = 'IT_FINAL_ALV'.
    wa_fcat-col_pos = 8.
    wa_fcat-seltext_m = 'Material Number'.
    APPEND wa_fcat TO it_fcat.
    CLEAR wa_fcat.

    wa_fcat-fieldname = 'TXZ01'.
    wa_fcat-tabname = 'IT_FINAL_ALV'.
    wa_fcat-col_pos = 9.
    wa_fcat-seltext_m = 'Material Description'.
    APPEND wa_fcat TO it_fcat.
    CLEAR wa_fcat.

    wa_fcat-fieldname = 'MENGE'.
    wa_fcat-tabname = 'IT_FINAL_ALV'.
    wa_fcat-col_pos = 10.
    wa_fcat-seltext_m = 'Qty'.
    APPEND wa_fcat TO it_fcat.
    CLEAR wa_fcat.

    wa_fcat-fieldname = 'MEINS'.
    wa_fcat-tabname = 'IT_FINAL_ALV'.
    wa_fcat-col_pos = 11.
    wa_fcat-seltext_m = 'Units'.
    APPEND wa_fcat TO it_fcat.
    CLEAR wa_fcat.

    wa_fcat-fieldname = 'WERKS'.
    wa_fcat-tabname = 'IT_FINAL_ALV'.
    wa_fcat-col_pos = 12.
    wa_fcat-seltext_m = 'Plant'.
    APPEND wa_fcat TO it_fcat.
    CLEAR wa_fcat.

    wa_fcat-fieldname = 'LGORT'.
    wa_fcat-tabname = 'IT_FINAL_ALV'.
    wa_fcat-col_pos = 13.
    wa_fcat-seltext_m = 'Storage Location'.
    APPEND wa_fcat TO it_fcat.
    CLEAR wa_fcat.

    wa_fcat-fieldname = 'LICHA'.
    wa_fcat-tabname = 'IT_FINAL_ALV'.
    wa_fcat-col_pos = 14.
    wa_fcat-seltext_m = 'Vendor Batch'.
    APPEND wa_fcat TO it_fcat.
    CLEAR wa_fcat.

    wa_fcat-fieldname = 'VFDAT'.
    wa_fcat-tabname = 'IT_FINAL_ALV'.
    wa_fcat-col_pos = 15.
    wa_fcat-seltext_m = 'Shelf Date'.
    APPEND wa_fcat TO it_fcat.
    CLEAR wa_fcat.

    wa_fcat-fieldname = 'EXVKW'.
    wa_fcat-tabname = 'IT_FINAL_ALV'.
    wa_fcat-col_pos = 16.
*    wa_fcat-do_sum  = 'X'.
    wa_fcat-seltext_m = 'MRP'.
    APPEND wa_fcat TO it_fcat.
    CLEAR wa_fcat.

    wa_fcat-fieldname = 'ZZRECDATE'.
    wa_fcat-tabname = 'IT_FINAL_ALV'.
    wa_fcat-col_pos = 17.
    wa_fcat-seltext_m = 'Security Date'.
    APPEND wa_fcat TO it_fcat.
    CLEAR wa_fcat.

    wa_fcat-fieldname = 'ZZREASON'.
    wa_fcat-tabname = 'IT_FINAL_ALV'.
    wa_fcat-col_pos = 18.
    wa_fcat-seltext_m = 'Reason'.
    APPEND wa_fcat TO it_fcat.
    CLEAR wa_fcat.

    wa_fcat-fieldname = 'GRN'.
    wa_fcat-tabname = 'IT_FINAL_ALV'.
    wa_fcat-col_pos = 18.
    wa_fcat-do_sum  = 'X'.
    wa_fcat-seltext_m = 'GRN Value'.
    APPEND wa_fcat TO it_fcat.
    CLEAR wa_fcat.

    wa_sort-spos = '01' .
    wa_sort-fieldname = 'EBELN'.
    wa_sort-tabname = 'IT_FINAL_ALV'.
    wa_sort-up = 'X'.
    wa_sort-subtot = 'X'.
    APPEND wa_sort TO it_sort .
    CLEAR wa_sort.

    DEFINE add_fcat.
      wa_fcat-col_pos = &1.
      wa_fcat-fieldname = &2.
      wa_fcat-outputlen = &3.
      wa_fcat-seltext_s =
      wa_fcat-seltext_m =
      wa_fcat-seltext_l = &4.
      wa_fcat-checkbox  = &5.
      wa_fcat-edit = &6.
      APPEND wa_fcat to it_fcat.
      CLEAR wa_fcat.
    END-OF-DEFINITION.
    CALL FUNCTION 'REUSE_ALV_FIELDCATALOG_MERGE'
      EXPORTING
        i_structure_name       = 'IT_FINAL_ALV'
      CHANGING
        ct_fieldcat            = it_fcat
*       it_sort                = it_sort
      EXCEPTIONS
        inconsistent_interface = 1
        program_error          = 2
        OTHERS                 = 3.
    IF sy-subrc = 0.
      add_fcat 1 'CHECK_FLAG' 10 'Check' 'X' 'X'.
    ENDIF.
  ENDIF.
""""""""Displaying the ALV output """"""""

  IF it_final_alv IS NOT INITIAL.
    CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
      EXPORTING
        i_callback_program       = sy-cprog
        i_callback_pf_status_set = 'PF_STATUS'
        i_callback_user_command  = 'ACTION'
        is_layout                = wa_layout
        it_fieldcat              = it_fcat
        it_sort                  = it_sort
      TABLES
        t_outtab                 = it_final_alv
      EXCEPTIONS
        program_error            = 1
        OTHERS                   = 2.
    IF sy-subrc <> 0.
* Implement suitable error handling here
    ENDIF.
  ENDIF.
FORM pf_status USING rt_exclude TYPE kkblo_t_extab..
  SET PF-STATUS 'ALV_STAT'.

ENDFORM. "SET_PF_STATUS1

FORM action USING r_ucomm LIKE sy-ucomm
                  rs_selfield TYPE slis_selfield.

  CLEAR: lt_tab.
  DATA : lr_grid TYPE REF TO cl_gui_alv_grid.

  CALL FUNCTION 'GET_GLOBALS_FROM_SLVC_FULLSCR'
    IMPORTING
      e_grid = lr_grid.

  CALL METHOD lr_grid->check_changed_data.

"""""""" Validations for POST Operations""""
  IF r_ucomm = 'POST'.
      
     Can write all the validations required during the POST operation
Note:: Please add the validations during the check operations and post operations here because if the user is giving directly the post operation  the also it needs to check the validations given in the check operation

""""""""""" BAPI to Create the GR if there are no errors""""
        CALL FUNCTION 'BAPI_GOODSMVT_CREATE'
          EXPORTING
            goodsmvt_header  = wa_header
            goodsmvt_code    = '01'
*           TESTRUN          = 'X'  
          IMPORTING
            materialdocument = lv_mblnr
            matdocumentyear  = lv_mjahr
          TABLES
            goodsmvt_item    = it_item
            return           = ret
            extensionin      = it_exn.

        CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'
          EXPORTING
            wait   = bapita-wait
          IMPORTING
            return = bapiret2.

SAP ABAP Development, SAP ABAP Connectivity, SAP ABAP Exam, SAP ABAP Exam Prep, SAP ABAP Certification, SAP ABAP Career, SAP ABAP Guides

Source: sap.com

No comments:

Post a Comment