Tuesday, May 8, 2018

SAP ABAP-BAPI SALES ORDER CREATION REPORT

BAPI SALES ORDER CREATION


REPORT  zbapi_sales_order_creation.


INCLUDE zbapi_sales_order_top.
INCLUDE zbapi_sales_order_sel.
INCLUDE zbapi_sales_order_sub.



START-OF-SELECTION.
  
IF p_file IS NOT INITIAL.
    
PERFORM getdata.

    
PERFORM call_log.

    
REFRESH it_return_log.

    
PERFORM get_bapi.

    
PERFORM log_handle.

  
ELSE.
    
MESSAGE 'Please Select The File' TYPE 'S'.
  
ENDIF.

TOP
*&---------------------------------------------------------------------*
*&  Include           ZBAPI_sales_order_TOP
*&---------------------------------------------------------------------*

TYPE-POOLS truxs.

TYPESBEGIN OF ty_final,
        counter   
TYPE i ,
        doc_type  
TYPE  auart,
        sales_org  
TYPE vkorg,
        distr_chan 
TYPE vtweg,
        division   
TYPE spart,
        partn_role 
TYPE char6,
        partn_numb 
TYPE kunnr,
        po_itm_no  
TYPE posex,
        material   
TYPE matnr,
        req_qty 
TYPE    wmeng,
        target_qty 
TYPE dzmeng,
        plant      
TYPE plant,
       
END OF ty_final.

DATA lv_sonumber TYPE bapivbeln-vbeln.

DATAit_final TYPE TABLE OF ty_final,
      wa_final 
TYPE ty_final.


DATA:  lv_ponumber LIKE  bapimepoheader-po_number.

DATAit_return TYPE STANDARD TABLE OF bapiret2,
      wa_return 
TYPE bapiret2.

DATAit_return1 TYPE STANDARD TABLE OF bapiret2,
      wa_return1 
TYPE bapiret2.

DATA it_headdata TYPE STANDARD TABLE OF bapisdhd1,
       wa_headdata 
TYPE bapisdhd1,
       it_headdatax 
TYPE STANDARD TABLE OF bapisdhd1x,
       wa_headdatax 
TYPE bapisdhd1x.

DATA :it_itemdata TYPE STANDARD TABLE OF bapisditm,
      wa_itemdata 
TYPE bapisditm,
      it_itemdatax 
TYPE STANDARD TABLE OF bapisditmx,
      wa_itemdatax 
TYPE bapisditmx,
      it_partner 
TYPE STANDARD TABLE OF bapiparnr,
      wa_partner 
TYPE bapiparnr,
      it_order_schedules_in 
TYPE STANDARD TABLE OF bapischdl,
      wa_order_schedules_in 
TYPE bapischdl,
      it_order_schedules_inx 
TYPE STANDARD TABLE OF bapischdlx,
      wa_order_schedules_inx 
TYPE bapischdlx.


DATAit_type TYPE truxs_t_text_data.

**** Declaration for application loG
DATAit_return_log TYPE bapiret2_t.
* Class/Objects
DATAgcl_app_log   TYPE REF TO zca_cl_application_log,
* Internal Structure
      gs_log_handle 
TYPE balloghndl.

SELECTION SCREEEN
*&---------------------------------------------------------------------*
*&  Include           ZBAPI_sales_order_SEL
*&---------------------------------------------------------------------*

SELECTION-SCREEN BEGIN OF BLOCK b1 WITH FRAME TITLE text-001.

PARAMETERS p_file TYPE rlgrap-filename.

SELECTION-SCREEN END OF BLOCK b1.


AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_file.
  
PERFORM f4_help.
SUB-----------------------------------

*&---------------------------------------------------------------------*
*&  Include           ZBAPI_sales_order_SUB
*&---------------------------------------------------------------------*


FORM f4_help .

  
CALL FUNCTION 'F4_FILENAME'
    
EXPORTING
      field_name 
'P_FILE'
    
IMPORTING
      file_name  
p_file.

ENDFORM.                    " F4_HELP
*&---------------------------------------------------------------------*
*&      Form  GETDATA
*&---------------------------------------------------------------------*

FORM getdata .
  
DATAloc_filename TYPE string.
  loc_filename 
p_file.

  
CALL FUNCTION 'GUI_UPLOAD'
    
EXPORTING
      filename                
loc_filename
      filetype                
'DAT' "'ASC'
      has_field_separator     
'X'
    
TABLES
      data_tab                
it_final[]
    
EXCEPTIONS
      file_open_error         
1
      file_read_error         
2
      no_batch                
3
      gui_refuse_filetransfer 
4
      invalid_type            
5
      no_authority            
6
      unknown_error           
7
      bad_data_format         
8
      header_not_allowed      
9
      separator_not_allowed   
10
      header_too_long         
11
      unknown_dp_error        
12
      access_denied           
13
      dp_out_of_memory        
14
      disk_full               
15
      dp_timeout              
16
      
OTHERS                  17.

  
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.                    " GETDATA
*&---------------------------------------------------------------------*
*&      Form  CALL_LOG
*&---------------------------------------------------------------------*

FORM call_log .

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

******Create Application Log
  
CALL METHOD gcl_app_log->create_app_log_handle(
    
EXPORTING
      i_object     
'ZAK_SO_HEADER'       " Object In SLGO
      i_subobject  
'ZAK_SO_SUB'    " Sub Object In SLGO
      i_tcode      
sy-tcode
      i_repid      
sy-cprog
    RECEIVING
      r_log_handle 
gs_log_handle ).


ENDFORM.                    " CALL_LOG
*&---------------------------------------------------------------------*
*&      Form  GET_BAPI
*&---------------------------------------------------------------------*

FORM get_bapi .

  
DATA lv_x TYPE c VALUE 'X'.


  
IF  it_final[] IS  NOT INITIAL.
    
LOOP AT it_final[] INTO wa_final.
      
IF wa_headdata-doc_type IS INITIAL.

        
CALL FUNCTION 'CONVERSION_EXIT_AUART_INPUT'
          
EXPORTING
            
input  wa_final-doc_type
          
IMPORTING
            
output wa_final-doc_type.


        wa_headdata
-doc_type wa_final-doc_type.
        wa_headdatax
-doc_type  'X'.

        wa_headdata
-sales_org     wa_final-sales_org.
        wa_headdatax
-sales_org     'X'.


        wa_headdata
-distr_chan     wa_final-distr_chan.
        wa_headdatax
-distr_chan     'X'.

        wa_headdata
-division     wa_final-division.
        wa_headdatax
-division     =  'X'.

      
ENDIF.

      wa_itemdata
-po_itm_no  wa_final-po_itm_no.
      wa_itemdatax
-po_itm_no  wa_final-po_itm_no.
      wa_itemdatax
-po_itm_no 'X'.

      
CALL FUNCTION 'CONVERSION_EXIT_MATN1_INPUT'
        
EXPORTING
          
input  wa_final-material
        
IMPORTING
          
output wa_final-material.
      
.
      wa_itemdata
-material  wa_final-material.
      wa_itemdatax
-material 'X'.

      wa_itemdata
-target_qty  wa_final-target_qty.
      wa_itemdatax
-target_qty 'X'.

      wa_itemdata
-plant wa_final-plant.
      wa_itemdatax
-plant 'X'.



      
APPEND  wa_itemdata TO it_itemdata.
      
APPEND  wa_itemdatax TO it_itemdatax.
      
CLEAR wa_itemdatawa_itemdatax.
*
      
CALL FUNCTION 'CONVERSION_EXIT_PARVW_INPUT'
        
EXPORTING
          
input  wa_final-partn_role
        
IMPORTING
          
output wa_final-partn_role.

      wa_partner
-partn_role  wa_final-partn_role." Sold-To-Party
      
CALL FUNCTION 'CONVERSION_EXIT_ALPHA_INPUT'
        
EXPORTING
          
input  wa_final-partn_numb
        
IMPORTING
          
output wa_final-partn_numb.

      wa_partner
-partn_numb  wa_final-partn_numb." Ship-To-Party
      
APPEND  wa_partner TO it_partner.
      
CLEAR wa_partner.

      wa_order_schedules_in
-req_qty  wa_final-req_qty." Customer Number
      wa_order_schedules_inx
-req_qty 'X'.
      
APPEND  wa_order_schedules_in TO it_order_schedules_in.
      
APPEND  wa_order_schedules_inx TO it_order_schedules_inx.
      
CLEAR wa_order_schedules_in,wa_order_schedules_inx.

      
REFRESH it_return[]it_return1[].

      
AT END OF  counter .
        
CALL FUNCTION 'BAPI_SALESORDER_CREATEFROMDAT2'
          
EXPORTING
            order_header_in     
wa_headdata
            order_header_inx    
wa_headdatax
          
IMPORTING
            salesdocument       
lv_sonumber
          
TABLES
            
return              it_return
            order_items_in      
it_itemdata
            order_items_inx     
it_itemdatax
            order_partners      
it_partner
            order_schedules_in  
it_order_schedules_in
            order_schedules_inx 
it_order_schedules_inx.

        
IF it_return[] IS NOT INITIAL.
          
READ TABLE it_return INTO wa_return WITH KEY type 'S'
                                                       
id 'V1'.
          
IF sy-subrc .
            
APPEND wa_return TO it_return1.
            
CLEAR wa_return1,wa_return.

            
CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'
              
EXPORTING
                
wait 'X'.
          
ELSE.
            
CALL FUNCTION 'BAPI_TRANSACTION_ROLLBACK'
              
IMPORTING
                
return wa_return.
            
APPEND wa_return TO it_return1.
            
CLEAR wa_return1wa_return.
          
ENDIF.
        
ENDIF.
        
IF it_return1[] IS NOT INITIAL" this is for app_log
          
CALL METHOD gcl_app_log->add_message_to_app_log
            
EXPORTING
              i_log_handle 
gs_log_handle
              i_tab_return 
it_return1.
        
ENDIF.
      
ENDAT.
    
ENDLOOP.
  
ENDIF.
ENDFORM.                    " GET_BAPI
*&---------------------------------------------------------------------*
*&      Form  LOG_HANDLE
*&---------------------------------------------------------------------*

FORM log_handle .

  
CALL METHOD gcl_app_log->save_and_display_app_log
    
EXPORTING
      i_log_handle 
gs_log_handle
      i_flg_save   
abap_false.

ENDFORM.                    " LOG_HANDLE

2 comments: