Wednesday, September 23, 2020

BAPI Sales Order Cerate Program in SAP ABAP

BAPI Sales Order Cerate  Program.



BAPI Sales Order Process.

REPORT zbapi_sales_order NO STANDARD PAGE HEADING.

include zbapi_sales_order_top.
include zbapi_sales_order_sel.
include zbapi_sales_order_f01.

*----------------------------------------------*
* I N I T I A L I Z A T I O N                  *
*----------------------------------------------*
INITIALIZATION.


AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_file.

  PERFORM f4_file_name.

*&---------------------------------------------------------------------*
*& Start of Selection Event                                            *
*&---------------------------------------------------------------------*
START-OF-SELECTION.

  PERFORM data_process.      " CONVERTING DATA FROM EXCEL TO INTERNAL TABLE


  CLEAR gcl_app_log.
*****Application Log
  gcl_app_log =  zca_cl_application_log=>get_instance( ).

  CALL METHOD gcl_app_log->create_app_log_handle(
    EXPORTING
      i_object     = 'ZRKPAN'         " Object In SLGO
      i_subobject  = 'ZRKSUB'     " Sub Object In SLGO
      i_tcode      = sy-tcode
      i_repid      = sy-cprog
    RECEIVING
      r_log_handle = gs_log_handle ).
  PERFORM uplode_data.

END-OF-SELECTION.
************************* Save and Display Application Log *************************
  CALL METHOD gcl_app_log->save_and_display_app_log
    EXPORTING
      i_log_handle = gs_log_handle
      i_flg_save   = abap_false.


*Selection texts
*----------------------------------------------------------
* P_FILE D       .
=============================================================================================
*&---------------------------------------------------------------------*
*&  Include           ZBAPI_SALES_ORDER_TOP
*&---------------------------------------------------------------------*

TYPES: BEGIN OF  ty_file,
        doc_type  TYPE auart,    "  DOCUMENT TYPE
        sales_org  TYPE vkorg,   "  SALES ORGNIZATIN
        distr_chan TYPE vtweg,   "  DISTRIBUATION CHANNEL
        division   TYPE spart,   "  DIVISION
        partn_role TYPE parvw,   " SOLD TO PARTY
        partn_numb TYPE kunnr,   " SHIP TO PARTY
        po_itm_no  TYPE posex,   " ITEM
        material   TYPE matnr,   " MATERIAL
        target_qty TYPE dzmeng,  " QUANTITY
        plant      TYPE plant,   " PALNT
END OF ty_file.

DATA: gs_order_header_in      TYPE              bapisdhd1,             " Header data
      gs_order_header_inx     TYPE              bapisdhd1x,            " header datax
      gs_salesdocument        TYPE              bapivbeln-vbeln,       " document number
      gt_order_items_in	      TYPE TABLE OF    	bapisditm,             " item data internal table
      gs_order_items_in       TYPE              bapisditm,             " item data work area
      gt_order_items_inx      TYPE TABLE OF     bapisditmx,            " item datax internla tabel
      gs_order_items_inx      TYPE              bapisditmx,            " item data work area
      gt_order_partners	      TYPE TABLE OF     bapiparnr,             " sold to party
      gs_order_partners	      TYPE              bapiparnr.             " ship to party
***** Internal Table
DATA : gt_final  TYPE STANDARD TABLE OF ty_file,
       gs_final  TYPE                   ty_file.
*       gt_return TYPE STANDARD TABLE OF bapireturn.

DATA : gt_return TYPE STANDARD TABLE OF bapiret2,
      gs_return TYPE bapiret2.


DATA : gt_type  TYPE truxs_t_text_data.


* passing constan for values
DATA : gc_x TYPE c VALUE 'X'.

***** Declaration for application log
DATA: gt_return_log TYPE bapiret2_t.
****************** Class/Objects***************************
DATA: gcl_app_log   TYPE REF TO zca_cl_application_log,
****************** Internal Structure *********************
      gs_log_handle TYPE balloghndl.

"Local table
DATA: gt_return1 TYPE STANDARD TABLE OF bapiret2,
      gs_return1 TYPE bapiret2.

DATA: bapiret2 TYPE STANDARD TABLE OF bapiret2.
=======================================================================
*&---------------------------------------------------------------------*
*&  Include           ZBAPI_SALES_ORDER_SEL
*&---------------------------------------------------------------------*
SELECTION-SCREEN BEGIN OF BLOCK a1 WITH FRAME TITLE TEXT-t01.

  PARAMETER p_file TYPE rlgrap-filename.

SELECTION-SCREEN END OF BLOCK a1.
==========================================================================================
*&---------------------------------------------------------------------*
*&  Include           ZBAPI_SALES_ORDER_F01
*&---------------------------------------------------------------------*
*&---------------------------------------------------------------------*
*&      Form  F4_FILE_NAME
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM f4_file_name .
  CALL FUNCTION 'F4_FILENAME'
    EXPORTING
      field_name = 'P_FILE'
    IMPORTING
      file_name  = p_file.

ENDFORM.                    " F4_FILE_NAME
*&---------------------------------------------------------------------*
*&      Form  DATA_PROCESS
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM data_process .
****** Uploading the data in the file into internal table

  CALL FUNCTION 'TEXT_CONVERT_XLS_TO_SAP'
    EXPORTING
      i_field_seperator    = 'X'
      i_line_header        = 'X'
      i_tab_raw_data       = gt_type
      i_filename           = p_file
    TABLES
      i_tab_converted_data = gt_final[]
    EXCEPTIONS
      conversion_failed    = 1
      OTHERS               = 2.
  IF sy-subrc NE 0.
    MESSAGE ID sy-msgid
    TYPE sy-msgty
    NUMBER sy-msgno
    WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
  ENDIF.
ENDFORM.                    " DATA_PROCESS
*&---------------------------------------------------------------------*
*&      Form  UPLODE_DATA
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM uplode_data .
  DATA : lv_log(100) TYPE c.
  READ TABLE gt_final INTO gs_final INDEX 1.

  DATA: lt_order_schedules_in TYPE STANDARD TABLE OF  bapischdl,
        lt_order_schedules_inx  TYPE STANDARD TABLE OF  bapischdlx,
        ls_order_schedules_in TYPE   bapischdl,
        ls_order_schedules_inx  TYPE   bapischdlx.
*
*  gs_order_header_in- = gs_final-
* header data for sales order creation
  PERFORM f_converstion_exit.
  gs_order_header_in-doc_type   = gs_final-doc_type.
  gs_order_header_in-sales_org  = gs_final-sales_org.
  gs_order_header_in-distr_chan = gs_final-distr_chan.
  gs_order_header_in-division   = gs_final-division.

  gs_order_header_inx-doc_type   = gc_x.
  gs_order_header_inx-sales_org  = gc_x.
  gs_order_header_inx-distr_chan = gc_x.
  gs_order_header_inx-division   = gc_x.

* sales item data
  gs_order_items_in-po_itm_no  = gs_final-po_itm_no.
  gs_order_items_in-material   = gs_final-material.
  gs_order_items_in-target_qty = gs_final-target_qty.
  gs_order_items_in-plant      = gs_final-plant.

  gs_order_items_inx-po_itm_no = gc_x.
  gs_order_items_inx-material = gc_x.
  gs_order_items_inx-target_qty = gc_x.
  gs_order_items_inx-plant = gc_x.
  gs_order_items_inx-updateflag = gc_x.
  gs_order_items_inx-reqmts_typ = gc_x.

  APPEND gs_order_items_in TO gt_order_items_in.
  APPEND gs_order_items_inx TO gt_order_items_inx.
* SHEDULE QUANITITY
  ls_order_schedules_in-req_qty = gs_final-target_qty.
  ls_order_schedules_in-itm_number = gs_final-po_itm_no.
  ls_order_schedules_inx-req_qty = gc_x.
  ls_order_schedules_inx-itm_number = gc_x.
  APPEND ls_order_schedules_in TO lt_order_schedules_in.
  APPEND ls_order_schedules_inx TO lt_order_schedules_inx.
* partner of
  PERFORM f_partn_role.
  gs_order_partners-partn_role =  gs_final-partn_role.
  gs_order_partners-partn_numb =  gs_final-partn_numb.
  APPEND gs_order_partners TO gt_order_partners.
  CALL FUNCTION 'BAPI_SALESORDER_CREATEFROMDAT2'
    EXPORTING
      order_header_in  = gs_order_header_in
      order_header_inx = gs_order_header_inx
    IMPORTING
      salesdocument    = gs_salesdocument
    TABLES
      return           = gt_return1
      order_items_in   = gt_order_items_in
      order_items_inx  = gt_order_items_inx
      order_partners   = gt_order_partners
  order_schedules_in            = lt_order_schedules_in
  order_schedules_inx           = lt_order_schedules_inx.
if gt_return1[] IS NOT INITIAL.
  LOOP AT gt_return1 INTO gs_return1. "data(gs_return1).
    gs_return-type       = gs_return1-type.
    gs_return-id         = gs_return1-id.
    CONCATENATE gs_salesdocument gs_return1-message
    INTO lv_log SEPARATED BY ' - '.
    gs_return-message_v1 = lv_log.
    APPEND gs_return TO gt_return.
    CLEAR : gs_return1.
  ENDLOOP.
ENDIF.
CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'
  EXPORTING
    wait = 'X'.
CHECK sy-subrc EQ 0.
CALL METHOD gcl_app_log->add_message_to_app_log
  EXPORTING
    i_log_handle = gs_log_handle
    i_tab_return = gt_return.

ENDFORM.                    " UPLODE_DATA
*&---------------------------------------------------------------------*
*&      Form  F_CONVERSTION_EXIT
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM f_converstion_exit .
  CALL FUNCTION 'CONVERSION_EXIT_AUART_INPUT'
    EXPORTING
      input  = gs_final-doc_type
    IMPORTING
      output = gs_final-doc_type.


ENDFORM.                    " F_CONVERSTION_EXIT
*&---------------------------------------------------------------------*
*&      Form  F_PARTN_ROLE
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM f_partn_role .
  CALL FUNCTION 'CONVERSION_EXIT_PARVW_INPUT'
    EXPORTING
      input  = gs_final-partn_role
    IMPORTING
      output = gs_final-partn_role.


ENDFORM.                    " F_PARTN_ROLE

No comments:

Post a Comment