BAPI criar Ordem de VendaDATA: tbl_return_chg TYPE STANDARD TABLE OF bapiret2 WITH HEADER LINE.* Tratar mensagens de retorno da BAPIDATA: tbl_return_bapi TYPE STANDARD TABLE OF bapiret2 WITH HEADER LINE, bdcdata LIKE bdcdata OCCURS 0 WITH HEADER LINE, bdcmsg TYPE TABLE OF bdcmsgcoll WITH HEADER LINE. "Mensagens*-----------------------------------------------------------------------* Variaveis*-----------------------------------------------------------------------DATA: g_vbeln_criado LIKE vbak-vbeln, g_cont_price_date TYPE d.*-----------------------------------------------------------------------* Subrotinas*-----------------------------------------------------------------------*&---------------------------------------------------------------------** Subtotais*----------------------------------------------------------------------*FORM montar_subtotais. PERFORM limpar_estruturas_bapi. PERFORM popular_tabelas_bapi. PERFORM chamar_calculo_offset.ENDFORM. " Subtotais*&---------------------------------------------------------------------** Criar ordem de venda*----------------------------------------------------------------------*FORM criar_ordem_devolucao. PERFORM limpar_estruturas_bapi. PERFORM popular_tabelas_bapi. PERFORM chamar_bapi_ordem_devolucao.ENDFORM. " criar_ordem_devolucao*&---------------------------------------------------------------------** Limpar todas estruturas alimentadas para BAPI*----------------------------------------------------------------------*FORM limpar_estruturas_bapi. FREE: tbl_bapisditm, tbl_bapisditmx, tbl_bapicond_ex, tbl_bapischdl, tbl_bapischdlx, tbl_return_chg, tbl_bapicond, tbl_bapisdcond, tbl_bapicondx, tbl_return_bapi, tbl_return, tbl_bapiparnr. CLEAR g_vbeln_criado.ENDFORM.*&---------------------------------------------------------------------** Popular tabelas da BAPI*----------------------------------------------------------------------*FORM popular_tabelas_bapi . PERFORM popular_cabecalho_bapi. PERFORM montar_parceiro_bapi. PERFORM montar_itens_bapi.ENDFORM. " popular_tabelas_bapi*&---------------------------------------------------------------------** Construir cabecalho da BAPI ordem de venda*----------------------------------------------------------------------*FORM popular_cabecalho_bapi . CLEAR st_bapisdhd1. IF gw_pre_registro-tipo = 'C'. IF gw_pre_registro-serie = space. gw_pre_registro-serie = 0. ENDIF. st_bapisdhd1-doc_type = 'ZDCE'. "Tipo ordem st_bapisdhd1-ord_reason = gw_pre_registro-abgru. CONCATENATE gw_pre_registro-nfenum '-' gw_pre_registro-serie(1) INTO st_bapisdhd1-ref_doc_l. st_bapisdhd1-purch_date = gw_pre_registro-docdat. st_bapisdhd1x-updateflag = 'I'. st_bapisdhd1x-doc_type = 'X'. st_bapisdhd1x-ord_reason = 'X'. st_bapisdhd1x-ref_doc_l = 'X'. st_bapisdhd1x-purch_date = 'X'. ENDIF.ENDFORM. " popular_cabecalho_bapi*&---------------------------------------------------------------------** Montar parceiros do cliente*----------------------------------------------------------------------*FORM montar_parceiro_bapi . CLEAR tbl_bapiparnr. tbl_bapiparnr-partn_role = 'AG'. tbl_bapiparnr-partn_numb = gw_pre_registro-kunnr. APPEND tbl_bapiparnr. CLEAR tbl_bapiparnr. tbl_bapiparnr-partn_role = 'WE'. tbl_bapiparnr-partn_numb = gw_pre_registro-kunnr. APPEND tbl_bapiparnr.ENDFORM. " montar_bapi_parceiro*&---------------------------------------------------------------------** Montar Itens da BAPI*----------------------------------------------------------------------*FORM montar_itens_bapi . DATA: l_matnr LIKE mara-matnr, lv_item TYPE i. " Itens de devolucao validados LOOP AT gt_itens_valida INTO gs_itens_valida. CALL FUNCTION 'CONVERSION_EXIT_ALPHA_INPUT' EXPORTING input = gs_itens_valida-matnr IMPORTING output = l_matnr. CLEAR tbl_bapisditm. ADD 10 TO lv_item. tbl_bapisditm-itm_number = lv_item. tbl_bapisditm-ref_doc = gs_itens_valida-refkey. tbl_bapisditm-ref_doc_it = gs_itens_valida-refitm. tbl_bapisditm-ref_doc_ca = 'M'. "Fatura tbl_bapisditm-material = l_matnr. tbl_bapisditm-target_qty = gs_itens_valida-menge.* tbl_bapisditm-target_qu = gs_itens_valida-meins. tbl_bapisditm-sales_unit = gs_itens_valida-meins. tbl_bapisditm-price_date = gs_itens_valida-prsdt. APPEND tbl_bapisditm. CLEAR tbl_bapisditmx. tbl_bapisditmx-itm_number = lv_item. tbl_bapisditmx-ref_doc = 'X'. tbl_bapisditmx-ref_doc_it = 'X'. tbl_bapisditmx-ref_doc_ca = 'X'. tbl_bapisditmx-updateflag = 'I'. tbl_bapisditmx-material = 'X'. tbl_bapisditmx-target_qty = 'X'.* tbl_bapisditmx-target_qu = 'X'. tbl_bapisditmx-sales_unit = 'X'. tbl_bapisditmx-price_date = 'X'. APPEND tbl_bapisditmx. " Monta divisao de remessa PERFORM montar_divisao_remessa_bapi USING lv_item. PERFORM montar_condicoes_bapi USING lv_item. gs_itens_valida-kposn = lv_item. MODIFY gt_itens_valida FROM gs_itens_valida. ENDLOOP.ENDFORM. " montar_bapi_itens*&---------------------------------------------------------------------** Montar BAPI divisao de remessas*----------------------------------------------------------------------*FORM montar_divisao_remessa_bapi USING us_item. CLEAR tbl_bapischdl. tbl_bapischdl-itm_number = us_item. tbl_bapischdl-req_qty = gs_itens_valida-menge. tbl_bapischdl-sched_line = '0001'. tbl_bapischdl-req_date = sy-datum. APPEND tbl_bapischdl. CLEAR tbl_bapischdlx. tbl_bapischdlx-itm_number = us_item. tbl_bapischdlx-sched_line = '0001'. tbl_bapischdlx-req_qty = 'X'. tbl_bapischdlx-req_date = 'X'. APPEND tbl_bapischdlx.ENDFORM. " montar_bapi_divisao_remessa*&---------------------------------------------------------------------** Montar CONDICOES*----------------------------------------------------------------------*FORM montar_condicoes_bapi USING us_item. CLEAR tbl_bapicond. tbl_bapicond-itm_number = us_item. tbl_bapicond-cond_st_no = 10. tbl_bapicond-cond_count = 1. tbl_bapicond-cond_type = 'PR00'. tbl_bapicond-cond_updat = 'X'. tbl_bapicond-cond_value = gs_itens_valida-sap_netpr * '0.1'. tbl_bapicond-cond_unit = gs_itens_valida-sap_meins. APPEND tbl_bapicond. CLEAR tbl_bapicondx. tbl_bapicondx-itm_number = us_item. tbl_bapicondx-cond_st_no = 10. tbl_bapicondx-cond_count = 1. tbl_bapicondx-cond_type = 'PR00'. tbl_bapicondx-updateflag = 'X'. tbl_bapicondx-cond_value = 'X'. APPEND tbl_bapicondx.ENDFORM.*&---------------------------------------------------------------------** Chamar a BAPI para Ordem de Vendas*----------------------------------------------------------------------*FORM chamar_bapi_ordem_devolucao . DATA lv_tabix TYPE sy-index. DATA: st_head_chg LIKE bapisdh1x, st_logic_switch TYPE bapisdls. st_logic_switch-pricing = 'C'.* st_logic_switch-cond_handl = 'X'. CLEAR g_vbeln_criado. FREE tbl_return. " Mostrar andamento na barra de status CALL FUNCTION 'SAPGUI_PROGRESS_INDICATOR' EXPORTING percentage = 25 text = 'Gerando ordem de devolucao'. CALL FUNCTION 'SD_SALESDOCUMENT_CREATE' EXPORTING sales_header_in = st_bapisdhd1 sales_header_inx = st_bapisdhd1x logic_switch = st_logic_switch business_object = 'BUS2102' IMPORTING salesdocument_ex = g_vbeln_criado TABLES return = tbl_return sales_items_in = tbl_bapisditm sales_items_inx = tbl_bapisditmx sales_partners = tbl_bapiparnr sales_schedules_in = tbl_bapischdl sales_schedules_inx = tbl_bapischdlx sales_conditions_in