*&---------------------------------------------------------------------*
*& Report  ZBAPI_SALESORDER_CREATE
*&
*&---------------------------------------------------------------------*
*&
*& Author : Karthik
*&---------------------------------------------------------------------*

REPORT  ZBAPI_SALESORDER_CREATE.

data : ORDER_HEADER_IN like BAPISDHD1.
data : ORDER_ITEMS_IN like BAPISDITM occurs 0 with
header line.
data : RETURN like BAPIRET2 occurs 0 with header line.
data : ORDER_PARTNERS like BAPIPARNR occurs 0 with
header line.
DATA : ORDER_KEYS LIKE BAPISDKEY OCCURS 0 WITH HEADER
LINE.
data : ORDER_SCHEDULES_IN like BAPISCHDL occurs 0 with
header line.
data : BEGIN OF TAB OCCURS 0,
        SRNO(4),
        DOC_TYPE like ORDER_HEADER_IN-dOC_TYPE,
        SALES_ORG LIKE ORDER_HEADER_IN-SALES_ORG,
        DISTR_CHAN LIKE ORDER_HEADER_IN-DISTR_CHAN,
        DIVISION like ORDER_HEADER_IN-DIVISION,
*        REQ_DATE_H(10),
        PURCH_DATE(10),
        PMNTTRMS LIKE ORDER_HEADER_IN-PMNTTRMS,
        PURCH_NO_C LIKE ORDER_HEADER_IN-PURCH_NO_C,
        ITM_NUMBER like BAPISDITM-ITM_NUMBER,
        CUST_MAT22 LIKE ORDER_ITEMS_IN-CUST_MAT22,
        PLANT LIKE ORDER_ITEMS_IN-PLANT,
        TARGET_QTY LIKE ORDER_ITEMS_IN-TARGET_QTY,
        PARTN_ROLE LIKE ORDER_PARTNERS-PARTN_ROLE,
        PARTN_NUMB LIKE ORDER_PARTNERS-PARTN_NUMB,
      END OF TAB.

data: itab1 like alsmex_tabline occurs 0 with header
line.
DATA: gd_currentrow type i.
data : PURCHASEORDER like ekko-ebeln.
Data: tot_rec type i,     "Total Records
     gd_update type i,   "Main Table Increement Counter
     gd_lines type i,    "Success Table increement Counter
     w_textout like t100-text. "VARIABLE TO GET ERRORLOG
data : begin of it_success occurs 0,
        SALESDOCUMENT LIKE BAPIVBELN-VBELN,  "PROJECT
      end of it_success.

data : begin of it_error occurs 0,
        srno(4),
        err_msg(73) TYPE c,    "TO RETREIVE ERROR MESSAGES
     end of it_error.
data : srno(4).
DATA : SALESDOCUMENT LIKE  BAPIVBELN-VBELN.
selection-screen begin of block b1 with frame.
skip 3.
parameter:p_infile like rlgrap-filename obligatory.
skip 3.
selection-screen end  of block b1.

AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_infile.
  PERFORM value_help.

start-of-selection.

  call function 'ALSM_EXCEL_TO_INTERNAL_TABLE'
    EXPORTING
      filename                = p_infile
      i_begin_col             = '1'
      i_begin_row             = '2' "Do not require
headings
      i_end_col               = '22'
      i_end_row               = '10000'
    TABLES
      intern                  = itab1
    EXCEPTIONS
      inconsistent_parameters = 1
      upload_ole              = 2
      others                  = 3.
  if sy-subrc <> 0.
    message e010(zz) with text-001. "Problem uploading
Excel Spreadsheet
  endif.

*perform open_group.

  sort itab1 by row col.

* Get first row retrieved
  read table itab1 index 1.

* Set first row retrieved to current row
  gd_currentrow = itab1-row.

  loop at itab1.

* Reset values for next row
    if itab1-row ne gd_currentrow.
      append tab .
      clear tab.
      gd_currentrow = itab1-row.
    endif.
    SHIFT ITAB1-VALUE LEFT DELETING LEADING SPACE.
    case itab1-col.

      when '0001'.
        TAB-SRNO = itab1-value.
      when '0002'.
        TAB-DOC_TYPE = itab1-value.
      when '0003'.
        TAB-SALES_ORG = itab1-value.
      when '0004'.
        TAB-DISTR_CHAN = itab1-value.
      when '0005'.
        TAB-DIVISION = itab1-value.
*      when '0006'.
*        TAB-REQ_DATE_H =  itab1-value.
      when '0006'.
        TAB-PURCH_DATE = itab1-value.
      when '0007'.
        TAB-PMNTTRMS = itab1-value.
      when '0008'.
        TAB-PURCH_NO_C = itab1-value.
        when '0009'.
        TAB-ITM_NUMBER = itab1-value.
       when '0010'.
        TAB-CUST_MAT22 = itab1-value.
      when '0011'.
        TAB-PLANT  = itab1-value.
      when '0012'.
        TAB-TARGET_QTY = itab1-value.
      when '0013'.
        TAB-PARTN_ROLE = itab1-value.
      when '0014'.
        TAB-PARTN_NUMB = itab1-value.
    endcase.
  endloop.
  append tab.
  clear tab.

  sort tab by SRNO.

  LOOP AT TAB.
*    concatenate tab-REQ_DATE_H+4(4)
tab-REQ_DATE_H+2(2) tab-REQ_DATE_H+0(2) into
tab-REQ_DATE_H.
    concatenate tab-PURCH_DATE+4(4)
tab-PURCH_DATE+2(2) tab-PURCH_DATE+0(2) into
tab-PURCH_DATE.

    SRNO = TAB-SRNO.

    CALL FUNCTION 'CONVERSION_EXIT_ALPHA_INPUT'
      EXPORTING
        INPUT  = tab-PARTN_NUMB
      IMPORTING
        OUTPUT = tab-PARTN_NUMB.

    CALL FUNCTION 'CONVERSION_EXIT_ALPHA_INPUT'
      EXPORTING
        INPUT  = tab-CUST_MAT22
      IMPORTING
        OUTPUT = tab-CUST_MAT22.

    IF TAB-SRNO = SRNO.
      ORDER_HEADER_IN-DOC_TYPE = TAB-DOC_TYPE.
      ORDER_HEADER_IN-SALES_ORG = TAB-SALES_ORG.
      ORDER_HEADER_IN-DISTR_CHAN = TAB-DISTR_CHAN.
*      ORDER_HEADER_IN-REQ_DATE_H = TAB-REQ_DATE_H.
      ORDER_HEADER_IN-PURCH_DATE = TAB-PURCH_DATE.
      ORDER_HEADER_IN-PMNTTRMS = TAB-PMNTTRMS.
      ORDER_HEADER_IN-PURCH_NO_C = TAB-PURCH_NO_C.
      ORDER_HEADER_IN-DIVISION  = tab-DIVISION.
      ORDER_ITEMS_IN-ITM_NUMBER = tab-ITM_NUMBER.
      ORDER_ITEMS_IN-material = TAB-CUST_MAT22.
      ORDER_ITEMS_IN-PLANT      = TAB-PLANT.
      APPEND ORDER_ITEMS_IN.

      ORDER_PARTNERS-PARTN_ROLE = TAB-PARTN_ROLE.
      ORDER_PARTNERS-PARTN_NUMB = TAB-PARTN_NUMB.
      APPEND ORDER_PARTNERS.

      ORDER_SCHEDULES_IN-ITM_NUMBER = tab-ITM_NUMBER.
      ORDER_SCHEDULES_IN-REQ_QTY = tab-TARGET_QTY.
      append ORDER_SCHEDULES_IN.
    ENDIF.

    AT END OF SRNO.
      CALL FUNCTION 'BAPI_SALESORDER_CREATEFROMDAT2'
        EXPORTING
*            SALESDOCUMENTIN               =
             ORDER_HEADER_IN               = ORDER_HEADER_IN
*            ORDER_HEADER_INX              =
*            SENDER                        =
*            BINARY_RELATIONSHIPTYPE       =
*            INT_NUMBER_ASSIGNMENT         =
*            BEHAVE_WHEN_ERROR             =
*            LOGIC_SWITCH                  =
*            TESTRUN                       =
*            CONVERT                       = ' '
       IMPORTING
         SALESDOCUMENT                 = SALESDOCUMENT
        TABLES
         RETURN                        = RETURN
             ORDER_ITEMS_IN                = ORDER_ITEMS_IN
*            ORDER_ITEMS_INX               =
             ORDER_PARTNERS                = ORDER_PARTNERS
             ORDER_SCHEDULES_IN            = ORDER_SCHEDULES_IN
*            ORDER_SCHEDULES_INX           =
*            ORDER_CONDITIONS_IN           =
*            ORDER_CONDITIONS_INX          =
*            ORDER_CFGS_REF                =
*            ORDER_CFGS_INST               =
*            ORDER_CFGS_PART_OF            =
*            ORDER_CFGS_VALUE              =
*            ORDER_CFGS_BLOB               =
*            ORDER_CFGS_VK                 =
*            ORDER_CFGS_REFINST            =
*            ORDER_CCARD                   =
*            ORDER_TEXT                    =
         ORDER_KEYS                    = ORDER_KEYS
*            EXTENSIONIN                   =
*            PARTNERADDRESSES              =
                .

      IF SALESDOCUMENT <> SPACE.
        commit work.
        ADD 1 TO gd_update.
        it_success-SALESDOCUMENT = SALESDOCUMENT.
        append it_success.
        CLEAR :SALESDOCUMENT,ORDER_HEADER_IN.
        REFRESH : RETURN,ORDER_ITEMS_IN,ORDER_PARTNERS.
      ELSE.
        loop at return.
          it_error-SRNO = tab-SRNO.
          it_error-err_msg = return-MESSAGE .
          Append it_error.
        ENDLOOP.
        CLEAR :SALESDOCUMENT,ORDER_HEADER_IN.
        REFRESH : RETURN,ORDER_ITEMS_IN,ORDER_PARTNERS.
      ENDIF.
    endat.
  endloop.
  DESCRIBE TABLE it_success LINES gd_lines.
  IF gd_lines GT 0.
*     Display result report column headings
    PERFORM display_column_headings.
*     Display result report
    PERFORM DISPLAY_SUCESS.
  ENDIF.

* IF SUCESS FAILS Display Error Report
  DESCRIBE TABLE it_error LINES gd_lines.
  IF gd_lines GT 0.
    PERFORM errorheadings.
    PERFORM errorreport.
  ENDIF.
*&---------------------------------------------------------------------*
*&      Form  display_column_headings
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
FORM display_column_headings.
  WRITE:2 ' Success Report '(014) COLOR COL_POSITIVE.
  SKIP.
  WRITE:2 'The following records inserted
successfully:'(013).
  WRITE:/ sy-uline(15).

  FORMAT COLOR COL_HEADING.
  WRITE:/      sy-vline,
           (10) 'Sales order'(004), sy-vline.

  WRITE:/ sy-uline(15).
ENDFORM.                    "display_column_headings
*Subroutine to display SUCESS REPORT
FORM DISPLAY_SUCESS.
  FORMAT COLOR COL_NORMAL.
  LOOP AT it_success.
    WRITE:/      sy-vline,
        (10)  it_success-SALESDOCUMENT, sy-vline.

    CLEAR it_success.
  ENDLOOP.
  WRITE:/ sy-uline(15).
  REFRESH: it_success.
  FORMAT COLOR COL_BACKGROUND.
ENDFORM.                               "
DISPLAY_REPORT
*&---------------------------------------------------------------------*
*&      Form  errorreport
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
FORM errorreport.
  LOOP AT it_error.
    WRITE:/      sy-vline,
            (10) it_error-SRNO, sy-vline,
             (40) it_error-err_msg, sy-vline.

  ENDLOOP.
  WRITE:/ sy-uline(104).
  REFRESH: it_error.

endform.                    "errorreport

*&---------------------------------------------------------------------*
*&      Form  ERRORHEADINGS
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
FORM ERRORHEADINGS.
  SKIP.
  WRITE:2 ' Error Report '(007) COLOR COL_NEGATIVE.
  SKIP.
  WRITE:2 'The following records failed during
update:'(008).
  WRITE:/ sy-uline(104).
  FORMAT COLOR COL_HEADING.
  WRITE:/      sy-vline,
          (10) 'ERROR.'(009), sy-vline.

  WRITE:/ sy-uline(104).
  FORMAT COLOR COL_NORMAL.
ENDFORM.                    "ERRORHEADINGS
**&---------------------------------------------------------------------
**
**&      Form  value_help
**&---------------------------------------------------------------------
**
**       text
**----------------------------------------------------------------------
**
**  -->  p1        text
**  <--  p2        text
**----------------------------------------------------------------------
**
FORM value_help .
  CALL FUNCTION 'DSVAS_DOC_WS_FILENAME_GET_50'
    EXPORTING
      DEF_FILENAME     = ' '
      DEF_PATH         = ' '
      MASK             = ',*.*,*.*.'
      MODE             = 'O'
      TITLE            = ' '
    IMPORTING
      FILENAME         = p_infile
    EXCEPTIONS
      INV_WINSYS       = 1
      NO_BATCH         = 2
      SELECTION_CANCEL = 3
      SELECTION_ERROR  = 4
      OTHERS           = 5.
  IF sy-subrc <> 0.
* MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
*         WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
  ENDIF.


endform.                    "value_help
 
 
 
Serkan AKKAVAK
Computer Engineer
ABAP Developer & SAP MM SD Consultant
Contact : serkurumsal@yandex.com