Tuesday, May 8, 2018

SAP ABAP-BAPI PO CREATION REPORT

BAPI PO CREATION REPORT




REPORT  zbapi_po_creation.

INCLUDE  zbapi_po_creation_top.
INCLUDE  zbapi_po_creation_sel.
INCLUDE  zbapi_po_creation_sub.


INITIALIZATION.


AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_file.

    
PERFORM f4_file_name.

START-OF-SELECTION.
IF p_file IS NOT INITIAL.
  
PERFORM data_process.      " CONVERTING DATA FROM EXCEL TO INTERNAL TABLE
*PERFORM uplode_data.



  
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     
'ZAU_LOG'         " Object In SLGO
      i_subobject  
'ZAU_SUB'     " Sub Object In SLGO
      i_tcode      
sy-tcode
      i_repid      
sy-cprog
    RECEIVING
      r_log_handle 
gs_log_handle ).
  
PERFORM uplode_data.
** 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.
ELSE.
  
MESSAGE 'Please Select filepath' TYPE 'I' DISPLAY LIKE 'E'.
ENDIF.

PO CREATION IN BAPI TOP
*&---------------------------------------------------------------------*
*&  Include           ZBAPI_PO_CREATION_TOP
*&---------------------------------------------------------------------*


TYPESBEGIN OF ty_file,
  counter   
TYPE i ,
  vendor     
TYPE elifn,        " Vendor
  co_code    
TYPE  bukrs,       " company code
  doc_type   
TYPE  esart,       " document type
  purch_org  
TYPE  ekorg,       " purchase Organization
  pur_group  
TYPE  bkgrp,       " purchase group
  po_item    
TYPE  ebelp,       " po item
  material   
TYPE  matnr,       " material number
*  short_text TYPE  txz01,       " short text
  quantity   
TYPE  bstmg,       " quantity
  net_price  
TYPE  bapicurext,  " net price
  plant       
TYPE   ewerk,       " plant
  
END OF ty_file.

DATApo_number LIKE  bapimepoheader-po_number.

* ***** Internal Table
DATA it_final  TYPE STANDARD TABLE OF ty_file,
       wa_final  
TYPE                   ty_file.
*
DATA it_return TYPE STANDARD TABLE OF bapiret2,
       wa_return 
TYPE bapiret2.
DATA lv_type  TYPE truxs_t_text_data.
DATA gc_x TYPE c VALUE 'X'.


DATAwa_po_header TYPE  bapimepoheader,
      wa_poheaderx 
TYPE  bapimepoheaderx,
      it_po_items 
TYPE TABLE OF bapimepoitem,
      it_poitemx  
TYPE TABLE OF bapimepoitemx,
      wa_po_items 
TYPE bapimepoitem,
      wa_poitemx 
TYPE bapimepoitemx.

DATAit_return1 TYPE STANDARD TABLE OF bapiret2,
      wa_return1 
TYPE bapiret2.

****************** Class/Objects***************************
DATAgcl_app_log   TYPE REF TO zca_cl_application_log,
****************** Internal Structure *********************
      gs_log_handle 
TYPE balloghndl.
SELECTION SCREEN:

SELECTION-SCREEN BEGIN OF BLOCK a1 WITH FRAME TITLE text-t01.
  
PARAMETER p_file TYPE rlgrap-filename.
SELECTION-SCREEN END OF BLOCK a1.

SUB*****************************************************************
*&---------------------------------------------------------------------*
*&  Include           ZBAPI_PO_CREATION_SUB
*&---------------------------------------------------------------------*

*&---------------------------------------------------------------------*
*&      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 .
**  CALL FUNCTION 'TEXT_CONVERT_XLS_TO_SAP'
**    EXPORTING
**      i_field_seperator    = 'X'
**      i_line_header        = 'X'
**      i_tab_raw_data       = lv_type
**      i_filename           = p_file
**    TABLES
**      i_tab_converted_data = it_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.
  
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.
* Implement suitable error handling here
  
ENDIF.


ENDFORM.                    " DATA_PROCESS
*&---------------------------------------------------------------------*
*&      Form  UPLODE_DATA
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM uplode_data .
  
DATA lv_log(100TYPE c.
***** Uploading the data into the database table
  
READ TABLE it_final INTO wa_final INDEX 1.
****** header data in po creation
*  CALL FUNCTION 'CONVERSION_EXIT_ALPHA_INPUT'
*    EXPORTING
*      input  = wa_final-vendor
*    IMPORTING
*      output = wa_final-vendor.
  wa_po_header
-vendor     wa_final-vendor.

  
.


  wa_po_header
-purch_org  wa_final-purch_org.
  wa_po_header
-doc_type   wa_final-doc_type.
  wa_po_header
-pur_group  wa_final-pur_group.
  wa_po_header
-comp_code  wa_final-co_code.

** passing x  values in po item
  wa_poheaderx
-vendor    gc_x.
  wa_poheaderx
-purch_org gc_x.
  wa_poheaderx
-doc_type  gc_x.
  wa_poheaderx
-pur_group gc_x.
  wa_poheaderx
-comp_code gc_x.


  
LOOP AT it_final INTO wa_final.
    wa_po_items
-po_item    wa_final-po_item.
    
CALL FUNCTION 'CONVERSION_EXIT_ALPHA_INPUT'
      
EXPORTING
        
input  wa_final-material
      
IMPORTING
        
output wa_final-material.


    wa_po_items
-material   wa_final-material.
*    wa_po_items-short_text = wa_final-short_text.
    wa_po_items
-quantity   wa_final-quantity.
    wa_po_items
-net_price  wa_final-net_price.
    wa_po_items
-plant      wa_final-plant.

* passing x value in po item
    wa_poitemx
-po_item    wa_final-po_item.
    wa_poitemx
-po_itemx   gc_x.
    wa_poitemx
-material   gc_x.
*    wa_poitemx-short_text = gc_x.
    wa_poitemx
-quantity   gc_x.
    wa_poitemx
-po_price   gc_x.
    wa_poitemx
-plant      gc_x.

    
APPEND wa_poitemx TO it_poitemx.
    
APPEND wa_po_items TO it_po_items.


    
AT END OF  counter .

      
CALL FUNCTION 'BAPI_PO_CREATE1'
        
EXPORTING
          poheader         
wa_po_header
          poheaderx        
wa_poheaderx
        
IMPORTING
          exppurchaseorder 
po_number
        
TABLES
          
return           it_return1
          poitem           
it_po_items
          poitemx          
it_poitemx.

      
IF it_return1[] IS NOT INITIAL.
**        LOOP AT it_return1 INTO wa_return1. "data(wa_return1).
**          wa_return-type       = wa_return1-type.
**          wa_return-id         = wa_return1-id.
**          CONCATENATE po_number wa_return1-message
**          INTO lv_log SEPARATED BY ' - '.
**          wa_return-message_v1 = lv_log.
**          APPEND wa_return TO it_return.
**          CLEAR : wa_return1.
**        ENDLOOP.

        
READ TABLE it_return1 INTO wa_return1 WITH KEY type 'S'.
          
IF sy-subrc .
            
APPEND wa_return1 TO it_return.
            
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_return1 TO it_return.
            
CLEAR wa_return1wa_return.
          
ENDIF.



      
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 
it_return.
    
ENDAT.
  
ENDLOOP.
ENDFORM.                    " UPLODE_DATA