Quantcast
Channel: ABAP Development
Viewing all articles
Browse latest Browse all 948

Excel Upload Quotation - BAPI_QUOTATION_CREATEFROMDATA

$
0
0

There's no real documentation on this BAPI or on BAPI_QUOTATION_CREATEFROMDATA2. Had a lot of trouble with it considering that there was no test data in the development box. As a result I thought I'd share this.


This sample uses Conditions and Characteristics, which turned out to be very difficult to find working information on. Unfortunately no clue on how the sub-items work as it wasn't part of my requirement.


A very useful transaction to analyze any errors with is CUTRC which I never knew about. Definitely going to start using it more.


Another interesting thing that I figured out is dynamically assigning table structures to FieldSymbols. I had heard that you couldn't pass a dynamically assigned field symbol (table) to a function but mine works perfectly.


*&---------------------------------------------------------------------*
*& Report  ZUPL_QUOTATION
*&
*&---------------------------------------------------------------------*
*&
*&
*& Should've used the second BAPI. More info around on it.
*&---------------------------------------------------------------------*

REPORT zupl_quotation.
"Parameters for screen
SELECTION-SCREEN BEGIN OF BLOCK c1  WITH FRAME TITLE text-002.
* Sold-to-party, ship-to-party, Sales Organization, Distribution Channel, Division and a Sales office
PARAMETERS: p_sld_p TYPE vbak-kunnr,
             p_shp_p TYPE vbpa-kunnr,
             p_slorg TYPE vbak-vkorg,
             p_distr TYPE vbak-vtweg,
             p_div   TYPE vbak-spart,
             p_sloff TYPE vbak-vkbur,
             p_dfrom TYPE datum,
             p_dto   TYPE datum.

SELECTION-SCREEN END OF BLOCK c1.

SELECTION-SCREEN BEGIN OF BLOCK b1  WITH FRAME TITLE text-001.
PARAMETERS: r_sect RADIOBUTTON GROUP rad1 DEFAULT 'X',
             r_plat RADIOBUTTON GROUP rad1.
PARAMETERS: p_file TYPE ibipparms-path.
SELECTION-SCREEN END OF BLOCK b1.

AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_file.

   CALL FUNCTION 'F4_FILENAME'
     IMPORTING
       file_name = p_file.

END-OF-SELECTION.
   "Section table
   TYPES: BEGIN OF t_datatab_s,
             quan              TYPE string,
             vc                TYPE string,
             su                TYPE string,
             ch_cu_quality     TYPE string,
             ch_cu_size        TYPE string,
             ch_cu_component   TYPE string,
             ch_zz_length      TYPE string,
             ch_zz_scrap       TYPE string,
             ch_cu_prc_opt     TYPE string,
             ch_drawing_number TYPE string,
             ch_mark_number    TYPE string,
             ch_zz_bundle_size TYPE string,
             ch_cu_time        TYPE string,
             kbetr             TYPE string,
             kpein             TYPE string,
             kmein             TYPE string,
          END OF t_datatab_s.

   "Plates table
   TYPES: BEGIN OF t_datatab_p,
             quan              TYPE string,
             vc                TYPE string,
             su                TYPE string,
             ch_ps_quality     TYPE string,
             ch_ps_size        TYPE string,
             ch_ps_component   TYPE string,
             ch_zz_length      TYPE string,
             ch_zz_width       TYPE string,
             ch_zz_scrap       TYPE string,
             ch_pl_prc_opt     TYPE string,
             ch_drawing_number TYPE string,
             ch_mark_number    TYPE string,
             ch_zz_bundle_size TYPE string,
             ch_mass_item_sn   TYPE string,
             ch_cu_time        TYPE string,
             kbetr             TYPE string,
             kpein             TYPE string,
             kmein             TYPE string,
          END OF t_datatab_p.

   "Excel Variables
   DATA: lt_filestable TYPE STANDARD TABLE OF t_datatab_s,
         lt_fileptable TYPE STANDARD TABLE OF t_datatab_p,
         ls_file_s LIKE LINE OF lt_filestable,
         ls_file_p LIKE LINE OF lt_fileptable,
         lv_file TYPE string,
         lv_rc TYPE sy-subrc,
         lv_path TYPE char255,
         it_raw TYPE truxs_t_text_data.
   "Quoatation Variables
   DATA: order_header_in TYPE bapisdhead,
         salesdocument   TYPE bapivbeln-vbeln,
         sold_to_party   TYPE bapisoldto,
         ship_to_party   TYPE bapishipto,
         billing_party   TYPE bapipayer,
         order_items_in  TYPE STANDARD TABLE OF bapiitemin WITH HEADER LINE,
         order_partners  TYPE STANDARD TABLE OF bapipartnr WITH HEADER LINE,
         order_items_out TYPE STANDARD TABLE OF bapiitemex WITH HEADER LINE,
         order_cfgs_ref  TYPE STANDARD TABLE OF bapicucfg WITH HEADER LINE,
         order_cfgs_inst TYPE STANDARD TABLE OF bapicuins WITH HEADER LINE,
         order_cfgs_part_of TYPE STANDARD TABLE OF bapicuprt WITH HEADER LINE,
         order_cfgs_value TYPE STANDARD TABLE OF bapicuval WITH HEADER LINE,
         lv_instid TYPE cu_inst_id,
         lv_itemid TYPE posnr_va,
         lv_configid TYPE cux_cfg_id.

   DATA: BEGIN OF li_return OCCURS 1.
           INCLUDE STRUCTURE bapiret1.
   DATA: END OF li_return.

   FIELD-SYMBOLS: <f_table> TYPE STANDARD TABLE,
                  <f_wa>    TYPE any,
                  <field>   TYPE any.
   DATA: it_knvp TYPE TABLE OF knvp,
         lv_parvw TYPE parvw.
   TABLES: knvp.

   lv_parvw = 'SP'.

   CALL FUNCTION 'CONVERSION_EXIT_PARVW_INPUT'
     EXPORTING
       input         = lv_parvw
     IMPORTING
       OUTPUT        = lv_parvw.

   SELECT * FROM knvp
            WHERE kunnr = p_sld_p
              AND kunn2 = p_shp_p
              AND parvw = lv_parvw.
   ENDSELECT.
   IF sy-subrc = 4.
     MESSAGE E000(ZATS_MSG) WITH 'The Ship-to-Party is not linked' 'to the selected Sold-to-Party'.
   ENDIF.
   "Radio buttons
   IF r_sect = 'X'. "Sections File
     ASSIGN lt_filestable TO <f_table>.
     ASSIGN ls_file_s TO <f_wa>.
   ELSE. "Plates File
     ASSIGN lt_fileptable TO <f_table>.
     ASSIGN ls_file_p TO <f_wa>.
   ENDIF.

   "Initialize values.
   lv_itemid = '000010'.
   lv_configid = '000001'.
   lv_instid = '00000001'.

   "Import data
   CALL FUNCTION 'TEXT_CONVERT_XLS_TO_SAP'
     EXPORTING
       i_line_header        = 'X'
       i_tab_raw_data       = it_raw
       i_filename           = p_file
     TABLES
       i_tab_converted_data = <f_table>
     EXCEPTIONS
       conversion_failed    = 1
       OTHERS               = 2.

   IF sy-subrc <> 0.
     MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
             WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
   ENDIF.

   "Mapping section from Excel file to Quoatation data.
   order_header_in-doc_type = 'ZQT1'.
   order_header_in-sales_org = p_slorg.
   order_header_in-distr_chan = p_distr.
   order_header_in-division p_div.
*  order_header_in-ref_1 = 'Your Reference Number'.
   order_header_in-qt_valid_f = p_dfrom"Quotation start date
   order_header_in-qt_valid_t = p_dto.   "Quotation end date

   order_partners-partn_role = 'SP'.
   order_partners-partn_numb = p_sld_p. "Sold to party
   CALL FUNCTION 'CONVERSION_EXIT_PARVW_INPUT' "Required as the data is stored different on the database.
     EXPORTING
       input  = order_partners-partn_role
     IMPORTING
       output = order_partners-partn_role.
   APPEND order_partners.

   order_partners-partn_role = 'SH'. "Ship to party
   order_partners-partn_numb = p_shp_p.
   CALL FUNCTION 'CONVERSION_EXIT_PARVW_INPUT' "Required as the data is stored different on the database.
     EXPORTING
       input  = order_partners-partn_role
     IMPORTING
       output = order_partners-partn_role.
   APPEND order_partners.

   LOOP AT <f_table> INTO <f_wa>.
     order_items_in-itm_number = lv_itemid. "Item Number
     order_items_in-po_itm_no = lv_itemid. "Points to the Characteristics... Without it none are populated.
     "Don't exist in this structure.
*    ORDER_ITEMS_IN-CONFIG_ID = lv_configid.
*    ORDER_ITEMS_IN-INST_ID = lv_instid.

     ASSIGN COMPONENT 'VC' OF STRUCTURE <f_wa> TO <field>.
     order_items_in-material = <field>. "Material Number
     CALL FUNCTION 'CONVERSION_EXIT_ALPHA_INPUT'
       EXPORTING
         input  = order_items_in-material
       IMPORTING
         output = order_items_in-material.

     ASSIGN COMPONENT 'QUAN' OF STRUCTURE <f_wa> TO <field>.
     order_items_in-req_qty = <field>. "Quantity
     MULTIPLY order_items_in-req_qty BY 1000.
     ASSIGN COMPONENT 'SU' OF STRUCTURE <f_wa> TO <field>.
     order_items_in-target_qu = <field>. "Measurement

     order_items_in-COND_TYPE = 'ZPRM'. "Should this be hardcoded to work?? Apparently so.
     ASSIGN COMPONENT 'KBETR' OF STRUCTURE <f_wa> TO <field>.
     order_items_in-cond_value = <field>. "Rate
     DIVIDE order_items_in-cond_value BY 10.
     ASSIGN COMPONENT 'KPEIN' OF STRUCTURE <f_wa> TO <field>.
     order_items_in-cond_p_unt = <field>. "Condition Pricing Unit
     ASSIGN COMPONENT 'KMEIN' OF STRUCTURE <f_wa> TO <field>.
     order_items_in-cond_d_unt = <field>. "Condition Unit

     APPEND order_items_in.

     "Header texts section.
     order_cfgs_value-config_id = lv_configid.
     order_cfgs_value-inst_id = lv_instid.
     order_cfgs_value-charc = 'CH_ZZ_LENGTH'.
     ASSIGN COMPONENT 'CH_ZZ_LENGTH' OF STRUCTURE <f_wa> TO <field>.
     order_cfgs_value-value = <field>.
     APPEND order_cfgs_value.

     order_cfgs_value-config_id = lv_configid.
     order_cfgs_value-inst_id = lv_instid.
     order_cfgs_value-charc = 'CH_ZZ_SCRAP'.
     ASSIGN COMPONENT 'CH_ZZ_SCRAP' OF STRUCTURE <f_wa> TO <field>.
     order_cfgs_value-value = <field>.
     APPEND order_cfgs_value.

     order_cfgs_value-config_id = lv_configid.
     order_cfgs_value-inst_id = lv_instid.
     order_cfgs_value-charc = 'CH_DRAWING_NUMBER'.
     ASSIGN COMPONENT 'CH_DRAWING_NUMBER' OF STRUCTURE <f_wa> TO <field>.
     order_cfgs_value-value = <field>.
     APPEND order_cfgs_value.

     order_cfgs_value-config_id = lv_configid.
     order_cfgs_value-inst_id = lv_instid.
     order_cfgs_value-charc = 'CH_MARK_NUMBER'.
     ASSIGN COMPONENT 'CH_MARK_NUMBER' OF STRUCTURE <f_wa> TO <field>.
     order_cfgs_value-value = <field>.
     APPEND order_cfgs_value.

     order_cfgs_value-config_id = lv_configid.
     order_cfgs_value-inst_id = lv_instid.
     order_cfgs_value-charc = 'CH_ZZ_BUNDLE_SIZE'.
     ASSIGN COMPONENT 'CH_ZZ_BUNDLE_SIZE' OF STRUCTURE <f_wa> TO <field>.
     order_cfgs_value-value = <field>.
     APPEND order_cfgs_value.

     order_cfgs_value-config_id = lv_configid.
     order_cfgs_value-inst_id = lv_instid.
     order_cfgs_value-charc = 'CH_CU_TIME'.
     ASSIGN COMPONENT 'CH_CU_TIME' OF STRUCTURE <f_wa> TO <field>.
     order_cfgs_value-value = <field>.
     APPEND order_cfgs_value.

     IF r_sect = 'X'. "Section File
       order_cfgs_value-config_id = lv_configid.
       order_cfgs_value-inst_id = lv_instid.
       order_cfgs_value-charc = 'CH_CU_COMPONENT'.
       ASSIGN COMPONENT 'CH_CU_COMPONENT' OF STRUCTURE <f_wa> TO <field>.
       order_cfgs_value-value = <field>.
       APPEND order_cfgs_value.

       order_cfgs_value-config_id = lv_configid.
       order_cfgs_value-inst_id = lv_instid.
       order_cfgs_value-charc = 'CH_CU_PRC_OPT'.
       ASSIGN COMPONENT 'CH_CU_PRC_OPT' OF STRUCTURE <f_wa> TO <field>.
       order_cfgs_value-value = <field>.
       APPEND order_cfgs_value.

       order_cfgs_value-config_id = lv_configid.
       order_cfgs_value-inst_id = lv_instid.
       order_cfgs_value-charc = 'CH_CU_QUALITY'.
       ASSIGN COMPONENT 'CH_CU_QUALITY' OF STRUCTURE <f_wa> TO <field>.
       order_cfgs_value-value = <field>.
       APPEND order_cfgs_value.

       order_cfgs_value-config_id = lv_configid.
       order_cfgs_value-inst_id = lv_instid.
       order_cfgs_value-charc = 'CH_CU_SIZE'.
       ASSIGN COMPONENT 'CH_CU_SIZE' OF STRUCTURE <f_wa> TO <field>.
       order_cfgs_value-value = <field>.
       APPEND order_cfgs_value.

     ELSEIF r_plat = 'X'. "Plates File
       order_cfgs_value-config_id = lv_configid.
       order_cfgs_value-inst_id = lv_instid.
       order_cfgs_value-charc = 'CH_PS_COMPONENT'.
       ASSIGN COMPONENT 'CH_PS_COMPONENT' OF STRUCTURE <f_wa> TO <field>.
       order_cfgs_value-value = <field>.
       APPEND order_cfgs_value.

       order_cfgs_value-config_id = lv_configid.
       order_cfgs_value-inst_id = lv_instid.
       order_cfgs_value-charc = 'CH_PL_PRC_OPT'.
       ASSIGN COMPONENT 'CH_PL_PRC_OPT' OF STRUCTURE <f_wa> TO <field>.
       order_cfgs_value-value = <field>.
       APPEND order_cfgs_value.

       order_cfgs_value-config_id = lv_configid.
       order_cfgs_value-inst_id = lv_instid.
       order_cfgs_value-charc = 'CH_PS_QUALITY'.
       ASSIGN COMPONENT 'CH_PS_QUALITY' OF STRUCTURE <f_wa> TO <field>.
       order_cfgs_value-value = <field>.
       APPEND order_cfgs_value.

       order_cfgs_value-config_id = lv_configid.
       order_cfgs_value-inst_id = lv_instid.
       order_cfgs_value-charc = 'CH_PS_SIZE'.
       ASSIGN COMPONENT 'CH_PS_SIZE' OF STRUCTURE <f_wa> TO <field>.
       order_cfgs_value-value = <field>.
       APPEND order_cfgs_value.

       order_cfgs_value-charc = 'CH_ZZ_WIDTH'.
       ASSIGN COMPONENT 'CH_ZZ_WIDTH' OF STRUCTURE <f_wa> TO <field>.
       order_cfgs_value-value = <field>.
       APPEND order_cfgs_value.

       order_cfgs_value-charc = 'CH_MASS_ITEM_SN'.
       ASSIGN COMPONENT 'CH_MASS_ITEM_SN' OF STRUCTURE <f_wa> TO <field>.
       order_cfgs_value-value = <field>.
       APPEND order_cfgs_value.
     ENDIF.
     "End of texts (Characteristics)

     "Need to fill in order_cfgs_ref & order_cfgs_inst and possibly order_cfgs_part_of too.
     "Got the information from an example using BAPI_QUOTATION_GETDETAILBOS
     order_cfgs_ref-posex  = lv_itemid.
     order_cfgs_ref-config_id = lv_configid.
     order_cfgs_ref-root_id = lv_instid. "The type is inst_id.
     APPEND order_cfgs_ref.

     order_cfgs_inst-config_id = lv_configid.
     order_cfgs_inst-inst_id  = lv_instid.
     order_cfgs_inst-obj_type = 'MARA'.
     order_cfgs_inst-class_type = '300'.
     order_cfgs_inst-obj_key = order_items_in-material.
     order_cfgs_inst-quantity = order_items_in-target_qty.
     APPEND order_cfgs_inst.

     "Only for configurable sub-items..
*    order_cfgs_part_of-config_id = lv_configid.
*    order_cfgs_part_of-parent_id = lv_itemid.
*    order_cfgs_part_of-inst_id = lv_instid.
*    order_cfgs_part_of-class_type = '300'.
*    order_cfgs_part_of-obj_type = 'MARA'.
*    order_cfgs_part_of-obj_key = order_items_in-material.
*    APPEND order_cfgs_part_of.

     ADD 10 TO lv_itemid. "Items count in tens. Will be derived though if left open apparently.
     ADD 1 TO lv_configid. "Appears to increase per line item.
     CALL FUNCTION 'CONVERSION_EXIT_ALPHA_INPUT'
       EXPORTING
         input  = lv_itemid
       IMPORTING
         output = lv_itemid.

     CALL FUNCTION 'CONVERSION_EXIT_ALPHA_INPUT'
       EXPORTING
         input  = lv_configid
       IMPORTING
         output = lv_configid.

*    lv_instid = lv_instid + 1. "Apparently this stays 1 as per the GETDETAILBOS example.
   ENDLOOP.

   CALL FUNCTION 'BAPI_QUOTATION_CREATEFROMDATA'
     EXPORTING
       order_header_in     = order_header_in
*     without_commit      = ' '
*     convert_parvw_auart = ' '
     IMPORTING
       salesdocument       = salesdocument
       sold_to_party       = sold_to_party
       ship_to_party       = ship_to_party
*     billing_party       =
       return              = li_return
     TABLES
       order_items_in      = order_items_in
       order_partners      = order_partners
       order_items_out     = order_items_out
       order_cfgs_ref      = order_cfgs_ref
       order_cfgs_inst     = order_cfgs_inst
       order_cfgs_part_of  = order_cfgs_part_of
       order_cfgs_value    = order_cfgs_value.

   IF NOT li_return IS INITIAL.
     WRITE:/ li_return-type,
             li_return-message.
   ELSE.
     WRITE:/ 'Quotation ', salesdocument, ' created successfully.'.
   ENDIF.


Viewing all articles
Browse latest Browse all 948

Trending Articles