Você está na página 1de 21

*&--------------------------------------------------------------------* *& Report ZATUA_MATERIAIS * *& * *&--------------------------------------------------------------------* *& DATA : 10/12/2012 * *& Autor : Hermiro Manoel da Silva Carvalho

* *& Verso : 1.0.0 * *& Objetivo: Atualizar a quantidade de materiais em estoque, * *& atrves da transao MBRL por batch input. * *& * *&--------------------------------------------------------------------* REPORT zatua_materiais NO STANDARD PAGE HEADING. TABLES: ekko, ekpo, mseg, mkpf, rseg. TYPE-POOLS slis. *********************************************************************** ********************* Definio de tipos ****************************** *********************************************************************** TYPES: BEGIN OF ty_saida, checkbox ebeln_migo zeile_migo mblnr_migo xblnr_migo ebelp_migo quant_migo ebeln_miro ebelp_miro menge_miro diferenca END OF ty_saida, BEGIN OF ty_saida_aux, ebeln TYPE ebelp TYPE mblnr TYPE belnr TYPE bukrs TYPE gjahr TYPE END OF ty_saida_aux, BEGIN OF ty_ekko, ebeln TYPE ekko-ebeln, END OF ty_ekko, BEGIN OF ty_saida_excel, linha_1(10) TYPE c, linha_2(10) TYPE c, linha_3(10) TYPE c, linha_4(10) TYPE c, linha_5(10) TYPE c, linha_6(10) TYPE c, END OF ty_saida_excel, BEGIN OF ty_ekpo, ebeln TYPE ekpo-ebeln, "N do documento de compras TYPE TYPE TYPE TYPE TYPE TYPE TYPE TYPE TYPE TYPE TYPE xflag, ekpo-ebeln, mseg-zeile, mseg-mblnr, mkpf-xblnr, mseg-ebelp, mseg-erfmg, rseg-belnr, rseg-ebelp, rseg-menge, i, "Checkbox "Pedido de Compra "Item no doc. de material "Num. documento(Migo) "Nota de remessa "Item "Quantidade UM Registro "Num. documento(Miro) "Item (Miro) "Quantidade "Diferena de quantidade

ekpo-ebeln, ekpo-ebelp, mseg-mblnr, bseg-belnr, bseg-bukrs, bseg-gjahr,

"N do documento de compras "Item "Num. documento "N doc. de um doc. contbil "Empresa "Exercicio

ebelp bukrs werks lgort END OF ty_ekpo,

TYPE TYPE TYPE TYPE

ekpo-ebelp,"N item do doc. de compra ekpo-bukrs, "Empresa ekpo-werks, "Centro ekpo-lgort, "Depsito

BEGIN OF ty_mkpf, mblnr TYPE mkpf-mblnr, "N documento de material mjahr TYPE mkpf-mjahr, "Ano do doc. do material xblnr TYPE mkpf-xblnr, "N documento de referncia END OF ty_mkpf, BEGIN OF ty_mseg, mblnr TYPE zeile TYPE bukrs TYPE werks TYPE lgort TYPE erfmg TYPE ebeln TYPE ebelp TYPE END OF ty_mseg, BEGIN OF ty_bkpf, bukrs TYPE belnr TYPE gjahr TYPE xblnr TYPE END OF ty_bkpf, BEGIN OF ty_bseg, bukrs TYPE belnr TYPE gjahr TYPE ebeln TYPE ebelp TYPE END OF ty_bseg, BEGIN OF ty_rseg, belnr TYPE gjahr TYPE ebeln TYPE ebelp TYPE werks TYPE shkzg TYPE menge TYPE END OF ty_rseg, BEGIN OF ty_rbkp, belnr TYPE gjahr TYPE bldat TYPE budat TYPE xblnr TYPE END OF ty_rbkp, mseg-mblnr, mseg-zeile, mseg-bukrs, mseg-werks, mseg-lgort, mseg-erfmg, mseg-ebeln, mseg-ebelp, "N documento de material "Item no doc. de material "Empresa "Centro "Depsito "Quant. de medida do registro "N item do doc. de compra

bkpf-bukrs,"Empresa bkpf-belnr,"N doc. de um doc. contbil bkpf-gjahr,"Exercicio bkpf-xblnr,

bseg-bukrs, bseg-belnr, bseg-gjahr, bseg-ebeln, bseg-ebelp,

"Empresa "N doc. de um doc. contbil "Exerccio "N do documento de compras

rseg-belnr, rseg-gjahr, rseg-ebeln, rseg-ebelp, rseg-werks, rseg-shkzg, rseg-menge,

"N doc. de um doc. contbil " Exerccio "N do doc. de compras "Item "Centro "Debit/Credit Indicator "Quantidade

rbkp-belnr,"N de um doc. de faturamento rbkp-gjahr,"Exerccio rbkp-bldat,"Data no documento rbkp-budat,"Data de lanamento no documento rbkp-xblnr,"N documento de referncia

*BEGIN OF ty_zhist_materiais, * line(100) TYPE c, * END OF ty_saida_batch,

BEGIN OF ty_message, ebeln TYPE mblnr TYPE xblnr TYPE ebelp TYPE msgty TYPE msgno TYPE msgtx TYPE END OF ty_message .

ekpo-ebeln, "Pedido de compra mseg-mblnr, "Num. documento(Migo) mkpf-xblnr, "Nota de remessa mseg-ebelp, "Item message-msgty," Tipo da mensagem message-msgno," Numero da mensagem message-msgtx," Descrio da mensagem

*********************************************************************** ********************* Definio de tabelas **************************** *********************************************************************** DATA: t_saida t_saida_aux t_saida_excel t_ekpo t_mseg t_mseg_102 t_mkpf t_rseg t_bkpf t_bseg t_ekko t_rbkp t_saida_batch t_listheader t_fieldcat t_field_cat t_sort t_toolbar t_bdc t_return_bdc t_message t_log_batch TYPE TYPE TYPE TYPE TYPE TYPE TYPE TYPE TYPE TYPE TYPE TYPE TYPE TYPE TYPE TYPE TYPE TYPE TYPE TYPE TYPE TYPE TABLE OF ty_saida, TABLE OF ty_saida_aux, TABLE OF ty_saida_excel, TABLE OF ty_ekpo, TABLE OF ty_mseg, mseg OCCURS 0 WITH HEADER LINE, TABLE OF ty_mkpf, TABLE OF ty_rseg, TABLE OF ty_bkpf, TABLE OF ty_bseg, TABLE OF ty_ekko, TABLE OF ty_rbkp, TABLE OF zhist_materiais, slis_t_listheader, lvc_t_fcat, lvc_t_fcat, lvc_t_sort, ui_functions, TABLE OF bdcdata, TABLE OF bdcmsgcoll, TABLE OF ty_message, zslog_batch_migo OCCURS 0 WITH HEADER LINE.

*********************************************************************** ********************** Definio de ranges **************************** *********************************************************************** DATA: r_mkpf_mblnr TYPE RANGE OF mkpf-mblnr, r_mseg_bukrs TYPE RANGE OF mseg-bukrs, r_ekpo_ebeln TYPE RANGE OF ekpo-ebeln. *********************************************************************** ********************* Definio de work areas ************************* *********************************************************************** DATA: wa_saida wa_saida_ant wa_saida_aux wa_saida_excel wa_ekpo wa_ekko TYPE TYPE TYPE TYPE TYPE TYPE ty_saida, ty_saida, ty_saida_aux, ty_saida_excel, ty_ekpo, ty_ekko,

wa_mseg wa_mkpf wa_rseg wa_bkpf wa_rbkp wa_bseg wa_dados_fieldcat wa_saida_batch wa_fieldcat wa_field_cat wa_sort wa_toolbar wa_bdc wa_return_bdc wa_zhist_materiais wa_message wa_rmkpf_mblnr wa_mseg_bukrs wa_rekpo_ebeln wa_log_batch

TYPE TYPE TYPE TYPE TYPE TYPE TYPE TYPE TYPE TYPE TYPE TYPE TYPE TYPE TYPE TYPE LIKE LIKE LIKE LIKE

ty_mseg, ty_mkpf, ty_rseg, ty_bkpf, ty_rbkp, ty_bseg, lvc_s_fcat, zhist_materiais, slis_fieldcat_alv, lvc_s_fcat, lvc_s_sort, ui_func, bdcdata, bdcmsgcoll, zhist_materiais, ty_message, LINE OF r_mkpf_mblnr, LINE OF r_mseg_bukrs, LINE OF r_ekpo_ebeln, LINE OF t_log_batch.

*********************************************************************** **************************** Constantes ******************************* *********************************************************************** CONSTANTS: c_x c_e c_s c_h c_n c_003(3) c_101(3) c_122(3) c_we(2) c_1201(4) c_tcode(4) c_0002(4) c_title c_extensao c_diretorio c_lines_batch TYPE TYPE TYPE TYPE TYPE TYPE TYPE TYPE TYPE TYPE TYPE TYPE TYPE TYPE TYPE TYPE c c c c c c c c c c c c string string string i VALUE VALUE VALUE VALUE VALUE VALUE VALUE VALUE VALUE VALUE VALUE VALUE VALUE VALUE VALUE VALUE 'X', 'E', 'S', 'H', 'N', '003', '101', '122', 'WE', '1201', 'MBRL', '0002', 'Salvar arquivo em:', '.xls', 'C:/', 9.

*********************************************************************** **************************** Variveis ******************************** *********************************************************************** DATA: v_rc v_result v_mode v_nomefile v_path v_fullpath v_mensage v_mesgno v_layout v_repid v_layout_alv v_bdc_cursor(17) v_diferenca(20) v_mblnr_ant TYPE TYPE TYPE TYPE TYPE TYPE LIKE TYPE TYPE TYPE TYPE TYPE TYPE TYPE i, c, c, string, string, string, message, sy-msgno, lvc_s_layo, sy-repid, slis_layout_alv, c, c, mseg-mblnr,

v_cont v_contc(20) v_cont_batch v_qtd_mblnr v_cont_mblnr v_aux v_item_shdb v_erro_batch v_menge_h v_menge_s

TYPE TYPE TYPE TYPE TYPE TYPE TYPE TYPE TYPE TYPE

i, c, c, i, i, i, mseg-zeile, c, rseg-menge, rseg-menge.

*********************************************************************** ******************* DEFINIO DE CONTROLES **************************** *********************************************************************** CONTROLS: tc_logs_batch TYPE TABLEVIEW USING SCREEN '0110'. *********************************************************************** ************************** OBJETOS *********************************** *********************************************************************** DATA: go_container go_alv go_alv_hist go_cont_hist TYPE TYPE TYPE TYPE REF REF REF REF TO TO TO TO cl_gui_custom_container, cl_gui_alv_grid, cl_gui_alv_grid, cl_gui_custom_container.

" Declarao do table control. CONTROLS : ztab_ctrl TYPE TABLEVIEW USING SCREEN 0200. *********************************************************************** *************************** Tela de seleo *************************** *********************************************************************** SELECTION-SCREEN BEGIN OF BLOCK 001 WITH FRAME TITLE text-001. SELECT-OPTIONS: s_ebeln FOR ekpo-ebeln, s_werks FOR ekpo-werks OBLIGATORY, s_lgort FOR mseg-lgort, s_mjahr FOR mkpf-mjahr OBLIGATORY. * s_bldat FOR mkpf-bldat OBLIGATORY, * s_budat FOR mkpf-budat. SELECTION-SCREEN END OF BLOCK 001. *********************************************************************** *********************** Start of selection **************************** *********************************************************************** START-OF-SELECTION. *** Inicia a pesquisa de dados pela tabela ekpo, caso no exista *** dados o programa termina. * SELECT ebeln * INTO TABLE t_ekko * FROM ekko * WHERE bukrs EQ c_1201. * * IF sy-subrc IS NOT INITIAL. * MESSAGE text-002 TYPE c_s DISPLAY LIKE c_e. * EXIT. * ENDIF.

* * * *

SELECT ebeln ebelp bukrs werks lgort INTO TABLE t_ekpo FROM ekpo FOR ALL ENTRIES IN t_ekko WHERE ebeln EQ t_ekko-ebeln AND ebeln IN s_ebeln WHERE ebeln IN s_ebeln AND aedat IN s_bldat AND bukrs EQ c_1201 AND werks IN s_werks. IF sy-subrc IS NOT INITIAL. MESSAGE text-002 TYPE c_s DISPLAY LIKE c_e. EXIT. ENDIF.

***Para efeito de performance buscaremos alguns dados na MKPF. SELECT mblnr mjahr xblnr INTO TABLE t_mkpf FROM mkpf WHERE mjahr IN s_mjahr AND vgart EQ c_we. * AND bldat IN s_bldat * AND budat IN s_budat. IF sy-subrc IS NOT INITIAL. MESSAGE text-002 TYPE c_s DISPLAY LIKE c_e. EXIT. ENDIF. ***Monta um range com os documentos de materiais da mkpf. LOOP AT t_mkpf INTO wa_mkpf. wa_rmkpf_mblnr-low = wa_mkpf-mblnr. wa_rmkpf_mblnr-sign = 'I'. wa_rmkpf_mblnr-option = 'EQ'. APPEND wa_rmkpf_mblnr TO r_mkpf_mblnr. CLEAR wa_mkpf. ENDLOOP. ***Monta um range com os pedidos de compras da ekpo . LOOP AT t_ekpo INTO wa_ekpo. wa_rekpo_ebeln-low = wa_ekpo-ebeln. wa_rekpo_ebeln-sign = 'I'. wa_rekpo_ebeln-option = 'EQ'. APPEND wa_rekpo_ebeln TO r_ekpo_ebeln. CLEAR wa_ekpo. ENDLOOP. DELETE ADJACENT DUPLICATES FROM r_ekpo_ebeln COMPARING ALL FIELDS. ***Seleo de dados na tabela MSEG SELECT mblnr zeile bukrs werks lgort erfmg ebeln ebelp FROM mseg INTO TABLE t_mseg FOR ALL ENTRIES IN t_mkpf WHERE mblnr EQ t_mkpf-mblnr AND mjahr EQ s_mjahr AND bwart EQ c_101

AND werks IN s_werks AND lgort IN s_lgort AND ebeln IN r_ekpo_ebeln . IF sy-subrc IS NOT INITIAL. MESSAGE text-002 TYPE c_s DISPLAY LIKE c_e. EXIT. ENDIF. ***Seleo dos estornados SELECT sjahr smbln smblp INTO CORRESPONDING FIELDS OF TABLE t_mseg_102 FROM mseg FOR ALL ENTRIES IN t_mseg WHERE sjahr = s_mjahr AND smbln = t_mseg-mblnr AND smblp = t_mseg-zeile. ***Seleo de dados na tabela RSEG SELECT belnr gjahr ebeln ebelp werks shkzg menge INTO TABLE t_rseg FROM rseg FOR ALL ENTRIES IN t_mseg WHERE ebeln EQ t_mseg-ebeln AND gjahr IN s_mjahr AND werks IN s_werks. IF sy-subrc IS NOT INITIAL. MESSAGE text-002 TYPE c_s DISPLAY LIKE c_e. EXIT. ENDIF. SELECT belnr gjahr bldat budat xblnr INTO TABLE t_rbkp FROM rbkp FOR ALL ENTRIES IN t_rseg WHERE belnr EQ t_rseg-belnr AND gjahr EQ t_rseg-gjahr. * AND bldat IN s_bldat * AND budat IN s_budat. IF sy-subrc IS NOT INITIAL. MESSAGE text-002 TYPE c_s DISPLAY LIKE c_e. EXIT. ENDIF. SORT: t_ekpo t_mkpf t_mseg t_rseg t_rbkp BY BY BY BY BY ebeln ebelp, mblnr, mblnr zeile, ebeln ebelp, belnr.

****Prepara a tabela t_saida para ser usada no alv. LOOP AT t_mseg INTO wa_mseg. READ TABLE t_mseg_102 WITH KEY sjahr = s_mjahr smbln = wa_mseg-mblnr smblp = wa_mseg-zeile.

IF sy-subrc IS INITIAL. " Desconsidera documento estornado. CONTINUE. ENDIF. READ TABLE t_ekpo INTO wa_ekpo WITH KEY ebeln = wa_mseg-ebeln ebelp = wa_mseg-ebelp BINARY SEARCH. IF sy-subrc IS INITIAL. wa_saida-ebeln_migo = wa_ekpo-ebeln. "Pedido de compra wa_saida-mblnr_migo = wa_mseg-mblnr. "Doc. Migo wa_saida-zeile_migo = wa_mseg-zeile."Item no doc. de mat. ENDIF. READ TABLE t_mkpf INTO wa_mkpf WITH KEY mblnr = wa_mseg-mblnr BINARY SEARCH. IF sy-subrc IS INITIAL. wa_saida-ebelp_migo = wa_mseg-ebelp."Item migo wa_saida-quant_migo = wa_mseg-erfmg."Quant. Migo ENDIF. READ TABLE t_rseg INTO wa_rseg WITH KEY ebeln = wa_mseg-ebeln ebelp = wa_mseg-ebelp BINARY SEARCH. IF sy-subrc IS INITIAL. wa_saida-ebeln_miro = wa_rseg-belnr."Doc. Miro wa_saida-ebelp_miro = wa_rseg-ebelp."Item miro CLEAR: wa_rseg, v_menge_h, v_menge_s. ***Verifica se houve alguma devoluo na MIRO LOOP AT t_rseg INTO wa_rseg WHERE ebeln = wa_mseg-ebeln AND ebelp = wa_mseg-ebelp. IF wa_rseg-shkzg EQ c_h."Verifica se dbito ou crdito v_menge_h = v_menge_h + wa_rseg-menge. ENDIF."Verifica se dbito ou crdito IF wa_rseg-shkzg EQ c_s."Verifica se dbito ou crdito v_menge_s = v_menge_s + wa_rseg-menge. ENDIF."Verifica se dbito ou crdito ENDLOOP. wa_rseg-menge = v_menge_s - v_menge_h. wa_saida-menge_miro = wa_rseg-menge."Quant. faturada IF wa_mseg-erfmg > wa_rseg-menge. "Verifica se diferena wa_saida-diferenca = wa_mseg-erfmg - wa_rseg-menge. ENDIF."Verifica se existe diferena READ TABLE t_rbkp INTO wa_rbkp WITH KEY belnr = wa_rseg-belnr BINARY SEARCH. IF sy-subrc IS INITIAL. wa_saida-xblnr_migo = wa_rbkp-xblnr. ENDIF. ENDIF. APPEND wa_saida TO t_saida. CLEAR: wa_saida, wa_ekpo, wa_mkpf, wa_mseg, wa_rseg. ENDLOOP. *** PARMETROS PARA O ALV. MOVE: c_x TO v_layout-zebra,

c_x TO v_layout-cwidth_opt. ***PARMETROS PARA A TABELA MERGE (DADOS DA COLUNA) wa_dados_fieldcat-fieldname = 'CHECKBOX'. wa_dados_fieldcat-checkbox = c_x. wa_dados_fieldcat-edit = c_x. APPEND wa_dados_fieldcat TO t_fieldcat. CLEAR wa_dados_fieldcat. wa_dados_fieldcat-fieldname = 'EBELN_MIGO'. wa_dados_fieldcat-coltext = 'Pedido Compra'. APPEND wa_dados_fieldcat TO t_fieldcat. wa_dados_fieldcat-fieldname = 'MBLNR_MIGO'. wa_dados_fieldcat-coltext = 'Documento - Migo'. APPEND wa_dados_fieldcat TO t_fieldcat. wa_dados_fieldcat-fieldname wa_dados_fieldcat-coltext = wa_dados_fieldcat-no_out = APPEND wa_dados_fieldcat TO CLEAR wa_dados_fieldcat. = 'ZEILE_MIGO'. ''. c_x. t_fieldcat.

wa_dados_fieldcat-fieldname = 'XBLNR_MIGO'. wa_dados_fieldcat-coltext = 'Nota Remessa'. APPEND wa_dados_fieldcat TO t_fieldcat. wa_dados_fieldcat-fieldname = 'EBELP_MIGO'. wa_dados_fieldcat-coltext = 'Item - Migo'. APPEND wa_dados_fieldcat TO t_fieldcat. wa_dados_fieldcat-fieldname = 'QUANT_MIGO'. wa_dados_fieldcat-coltext = 'Quantidade - Migo'. APPEND wa_dados_fieldcat TO t_fieldcat. wa_dados_fieldcat-fieldname = 'EBELN_MIRO'. wa_dados_fieldcat-coltext = 'Documento - Miro'. APPEND wa_dados_fieldcat TO t_fieldcat. wa_dados_fieldcat-fieldname = 'EBELP_MIRO'. wa_dados_fieldcat-coltext = 'Item - Miro'. APPEND wa_dados_fieldcat TO t_fieldcat. wa_dados_fieldcat-fieldname = 'MENGE_MIRO'. wa_dados_fieldcat-coltext = 'Quantidade - Miro'. APPEND wa_dados_fieldcat TO t_fieldcat. wa_dados_fieldcat-fieldname = 'DIFERENCA'. wa_dados_fieldcat-coltext = 'Diferena de Quantidade'. wa_dados_fieldcat-edit = c_x. wa_dados_fieldcat-decimals_o = c_003. APPEND wa_dados_fieldcat TO t_fieldcat. CLEAR wa_dados_fieldcat. CALL SCREEN 100. *&---------------------------------------------------------------------* *& Module STATUS_0100 OUTPUT *&---------------------------------------------------------------------* * text

*----------------------------------------------------------------------* MODULE status_0100 OUTPUT. SET PF-STATUS 'MENU_100'. SET TITLEBAR 'TITLE_0100'. ***cria objeto do alv. CREATE OBJECT go_alv EXPORTING i_parent = go_container EXCEPTIONS error_cntl_create = 1 error_cntl_init = 2 error_cntl_link = 3 error_dp_create = 4 OTHERS = 5. IF sy-subrc <> 0. MESSAGE s077(/bdl/bdl3) DISPLAY LIKE c_e. EXIT. ENDIF. ***CRIA OBJETO DO CONTAINER. CREATE OBJECT go_container EXPORTING container_name = 'ALV_CONTAINER' EXCEPTIONS cntl_error = 1 cntl_system_error = 2 create_error = 3 lifetime_error = 4 lifetime_dynpro_dynpro_link = 5 OTHERS = 6. IF sy-subrc <> 0. MESSAGE s077(/bdl/bdl3) DISPLAY LIKE c_e. EXIT. ENDIF. ***PARMETROS PARA A ORDENAR A TABELA. wa_sort-fieldname = 'EBELN_MIGO'. wa_sort-spos = '1'. APPEND wa_sort TO t_sort. ***REMOVE OS ALGUNS BOTES DA TOOLBAR. wa_toolbar = cl_gui_alv_grid=>mc_fc_loc_append_row. APPEND wa_toolbar TO t_toolbar. CLEAR: wa_toolbar. wa_toolbar = cl_gui_alv_grid=>mc_fc_loc_copy. APPEND wa_toolbar TO t_toolbar. CLEAR: wa_toolbar. wa_toolbar = cl_gui_alv_grid=>mc_fc_loc_copy_row. APPEND wa_toolbar TO t_toolbar. CLEAR: wa_toolbar. wa_toolbar = cl_gui_alv_grid=>mc_fc_loc_cut. APPEND wa_toolbar TO t_toolbar. CLEAR: wa_toolbar. wa_toolbar = cl_gui_alv_grid=>mc_fc_loc_delete_row. APPEND wa_toolbar TO t_toolbar. CLEAR: wa_toolbar. wa_toolbar = cl_gui_alv_grid=>mc_fc_loc_insert_row.

APPEND wa_toolbar TO t_toolbar. CLEAR: wa_toolbar. wa_toolbar = cl_gui_alv_grid=>mc_fc_loc_move_row. APPEND wa_toolbar TO t_toolbar. CLEAR: wa_toolbar. wa_toolbar = cl_gui_alv_grid=>mc_fc_loc_paste. APPEND wa_toolbar TO t_toolbar. CLEAR: wa_toolbar. wa_toolbar = cl_gui_alv_grid=>mc_fc_loc_paste_new_row. APPEND wa_toolbar TO t_toolbar. CLEAR: wa_toolbar. wa_toolbar = cl_gui_alv_grid=>mc_fc_loc_undo. APPEND wa_toolbar TO t_toolbar. CLEAR: wa_toolbar. wa_toolbar = cl_gui_alv_grid=>mc_fc_maintain_variant. APPEND wa_toolbar TO t_toolbar. CLEAR: wa_toolbar. wa_toolbar = cl_gui_alv_grid=>mc_fc_print. APPEND wa_toolbar TO t_toolbar. CLEAR: wa_toolbar. wa_toolbar = cl_gui_alv_grid=>mc_fc_print_back. APPEND wa_toolbar TO t_toolbar. CLEAR: wa_toolbar. wa_toolbar = cl_gui_alv_grid=>mc_fc_print_prev. APPEND wa_toolbar TO t_toolbar. CLEAR: wa_toolbar. wa_toolbar = cl_gui_alv_grid=>mc_fc_refresh. APPEND wa_toolbar TO t_toolbar. CLEAR: wa_toolbar. wa_toolbar = cl_gui_alv_grid=>mc_fc_reprep. APPEND wa_toolbar TO t_toolbar. CLEAR: wa_toolbar. wa_toolbar = cl_gui_alv_grid=>mc_fc_save_variant. APPEND wa_toolbar TO t_toolbar. CLEAR: wa_toolbar. wa_toolbar = cl_gui_alv_grid=>mc_fc_send. APPEND wa_toolbar TO t_toolbar. CLEAR: wa_toolbar. wa_toolbar = cl_gui_alv_grid=>mc_fc_separator. APPEND wa_toolbar TO t_toolbar. CLEAR: wa_toolbar. wa_toolbar = cl_gui_alv_grid=>mc_fc_subtot. APPEND wa_toolbar TO t_toolbar. CLEAR: wa_toolbar. wa_toolbar = cl_gui_alv_grid=>mc_fc_sum. APPEND wa_toolbar TO t_toolbar. CLEAR: wa_toolbar. wa_toolbar = cl_gui_alv_grid=>mc_fc_to_office. APPEND wa_toolbar TO t_toolbar. CLEAR: wa_toolbar. wa_toolbar = cl_gui_alv_grid=>mc_fc_to_rep_tree. APPEND wa_toolbar TO t_toolbar. CLEAR: wa_toolbar. wa_toolbar = cl_gui_alv_grid=>mc_fc_unfix_columns. APPEND wa_toolbar TO t_toolbar. CLEAR: wa_toolbar. wa_toolbar = cl_gui_alv_grid=>mc_fc_url_copy_to_clipboard. APPEND wa_toolbar TO t_toolbar. CLEAR: wa_toolbar. wa_toolbar = cl_gui_alv_grid=>mc_fc_variant_admin.

APPEND wa_toolbar TO t_toolbar. CLEAR: wa_toolbar. wa_toolbar = cl_gui_alv_grid=>mc_fc_views. APPEND wa_toolbar TO t_toolbar. CLEAR: wa_toolbar. wa_toolbar = cl_gui_alv_grid=>mc_fc_view_crystal. APPEND wa_toolbar TO t_toolbar. CLEAR: wa_toolbar. wa_toolbar = cl_gui_alv_grid=>mc_fc_view_excel. APPEND wa_toolbar TO t_toolbar. CLEAR: wa_toolbar. wa_toolbar = cl_gui_alv_grid=>mc_fc_view_grid. APPEND wa_toolbar TO t_toolbar. CLEAR: wa_toolbar. wa_toolbar = cl_gui_alv_grid=>mc_fc_view_lotus. APPEND wa_toolbar TO t_toolbar. CLEAR: wa_toolbar. wa_toolbar = cl_gui_alv_grid=>mc_fc_word_processor. APPEND wa_toolbar TO t_toolbar. CLEAR: wa_toolbar.

CALL METHOD go_alv->set_table_for_first_display EXPORTING i_structure_name = 'WA_SAIDA' is_layout = v_layout it_toolbar_excluding = t_toolbar CHANGING it_fieldcatalog = t_fieldcat it_outtab = t_saida it_sort = t_sort EXCEPTIONS invalid_parameter_combination = 1 program_error = 2 too_many_lines = 3 OTHERS = 4. ENDMODULE. " STATUS_0100 OUTPUT *&---------------------------------------------------------------------* *& Module USER_COMMAND_0100 INPUT *&---------------------------------------------------------------------* * text *----------------------------------------------------------------------* MODULE user_command_0100 INPUT. CASE sy-ucomm. WHEN 'BACK'. LEAVE TO SCREEN 0. WHEN 'CANC' OR 'EXIT'. LEAVE PROGRAM. WHEN 'SELT'. CLEAR wa_saida. ***Seleciona TODOS os registros da tela de uma vez. LOOP AT t_saida INTO wa_saida.

wa_saida-checkbox = c_x. MODIFY t_saida FROM wa_saida. ENDLOOP. CALL METHOD go_alv->refresh_table_display. ***Executa transao batch input WHEN 'CHANGE'. CLEAR: wa_saida,v_cont. CALL METHOD go_alv->check_changed_data. CLEAR: v_cont, v_cont_mblnr, v_cont_batch, wa_saida_ant. REFRESH: t_log_batch. ***Se alguma linha foi selecionada, ela ser usada no batch input. SORT: t_saida BY mblnr_migo ASCENDING zeile_migo ASCENDING. LOOP AT t_saida INTO wa_saida." WHERE checkbox EQ c_x. ***Verifica se o documento e a remessa esto preenchidos. IF wa_saida-mblnr_migo IS NOT INITIAL AND wa_saida-xblnr_migo IS NOT INITIAL. IF v_mblnr_ant NE wa_saida-mblnr_migo. IF v_mblnr_ant IS NOT INITIAL AND v_cont_batch IS NOT INITIAL. v_mode = 'N'. ***Insere apenas o boto salvar no batch input PERFORM insere_bdc USING: "'X' 'SAPMM07M' '0221', '' 'BDC_OKCODE' '=BU'. CALL TRANSACTION 'MBRL' USING t_bdc MODE v_mode MESSAGES INTO t_return_bdc. CLEAR v_mesgno. CLEAR: v_erro_batch, wa_log_batch. LOOP AT t_return_bdc INTO wa_return_bdc. v_mesgno = wa_return_bdc-msgnr.

CALL FUNCTION 'WRITE_MESSAGE' EXPORTING msgid = wa_return_bdc-msgid msgno = v_mesgno msgty = wa_return_bdc-msgtyp msgv1 = wa_return_bdc-msgv1 msgv2 = wa_return_bdc-msgv2 msgv3 = wa_return_bdc-msgv3 msgv4 = wa_return_bdc-msgv4 IMPORTING messg = v_mensage. wa_log_batch-ebeln_migo = wa_saida_ant-ebeln_migo. wa_log_batch-mblnr_migo = wa_saida_ant-mblnr_migo. wa_log_batch-xblnr_migo = wa_saida_ant-xblnr_migo. wa_log_batch-mensagem = v_mensage-msgtx. MOVE v_mensage-msgtx TO wa_saida_batch-mensag_part1.

APPEND wa_saida_batch TO t_saida_batch. IF wa_return_bdc-msgtyp EQ c_e. v_erro_batch = 'X'. wa_log_batch-status = 'ERRO'. APPEND wa_log_batch TO t_log_batch. ELSE. wa_log_batch-status = 'SUCESSO'. ENDIF. ENDLOOP. IF NOT wa_log_batch IS INITIAL AND v_erro_batch IS INITIAL. APPEND wa_log_batch TO t_log_batch. ENDIF.

REFRESH t_bdc. MESSAGE text-004 TYPE c_s DISPLAY LIKE c_s. CLEAR: v_cont_mblnr, v_cont_batch, v_cont. REFRESH t_bdc. ELSE. REFRESH t_bdc. CLEAR: v_cont_mblnr, v_cont_batch, v_cont. ENDIF.

***Tela inicial da transao MBLR * REFRESH t_bdc. PERFORM insere_bdc USING: 'X' 'SAPMM07M' '0460', '' 'BDC_CURSOR' 'RM07M-GRUND', '' 'BDC_OKCODE' '/00', '' 'RM07M-MBLNR' wa_saida-mblnr_migo, '' 'RM07M-MJAHR' s_mjahr-low, '' 'RM07M-LFSNR' wa_saida-xblnr_migo, '' 'RM07M-LFSNR' wa_saida-xblnr_rbkp, '' 'XFULL' c_x, '' 'RM07M-GRUND' c_0002, '' 'RM07M-WVERS2' c_x. PERFORM insere_bdc USING: 'X' 'SAPMM07M' '0221'. ENDIF. * ADD 1 TO v_cont_mblnr. v_mblnr_ant = wa_saida-mblnr_migo. SELECT COUNT(*) INTO v_qtd_mblnr FROM mseg WHERE mblnr = wa_saida-mblnr_migo. wa_saida_ant = wa_saida. v_mblnr_ant = wa_saida-mblnr_migo. ADD 1 TO v_cont. CASE wa_saida-checkbox. WHEN c_x.

***Concatena a posio do cursor com o item selecionado na tela. CLEAR: v_diferenca, v_item_shdb, v_bdc_cursor, v_contc. WRITE: wa_saida-diferenca TO v_diferenca. WRITE v_cont TO v_contc. SHIFT v_contc LEFT DELETING LEADING space. CONCATENATE 'MSEG-ERFMG(' v_contc ')' INTO v_bdc_cursor. SHIFT v_diferenca LEFT DELETING LEADING space. ***Tela seguinte da transao MBLR PERFORM insere_bdc USING: "'X' 'SAPMM07M' '0221', "'' 'BDC_CURSOR' v_bdc_cursor, '' v_bdc_cursor v_diferenca. v_cont_batch = c_x. WHEN OTHERS. CLEAR: v_item_shdb, v_bdc_cursor, v_contc. WRITE v_cont TO v_contc. SHIFT v_contc LEFT DELETING LEADING space. CONCATENATE 'RM07M-XSELK(' v_contc ')' INTO v_bdc_cursor. CONDENSE v_bdc_cursor. ***Tela seguinte da transao MBLR - Desmarca item PERFORM insere_bdc USING: "'X' 'SAPMM07M' '0221', "'' 'BDC_CURSOR' v_bdc_cursor, '' v_bdc_cursor ''. ENDCASE. IF v_cont = c_lines_batch. CLEAR v_cont. v_aux = v_qtd_mblnr - v_cont_mblnr. IF v_aux < c_lines_batch. v_cont = c_lines_batch - v_aux. ENDIF. IF v_qtd_mblnr > v_cont_mblnr. PERFORM insere_bdc USING: "'X' 'SAPMM07M' '0221', '' 'BDC_OKCODE' '=P+', 'X' 'SAPMM07M' '0221'. ENDIF. ENDIF. ENDIF. CLEAR: v_bdc_cursor. "Olhar essa linha. ENDLOOP. IF v_mblnr_ant IS NOT INITIAL AND v_cont_batch IS NOT INITIAL. v_mode = 'N'. ***Insere apenas o boto salvar no batch input PERFORM insere_bdc USING: "'X' 'SAPMM07M' '0221', '' 'BDC_OKCODE' '=BU'. CALL TRANSACTION 'MBRL' USING t_bdc MODE v_mode MESSAGES INTO t_return_bdc. CLEAR: v_cont_batch, v_bdc_cursor."Olhar v_bdc_cursor CLEAR v_mesgno.

LOOP AT t_return_bdc INTO wa_return_bdc. v_mesgno = wa_return_bdc-msgnr.

CALL FUNCTION 'WRITE_MESSAGE' EXPORTING msgid = wa_return_bdc-msgid msgno = v_mesgno msgty = wa_return_bdc-msgtyp msgv1 = wa_return_bdc-msgv1 msgv2 = wa_return_bdc-msgv2 msgv3 = wa_return_bdc-msgv3 msgv4 = wa_return_bdc-msgv4 IMPORTING messg = v_mensage. wa_log_batch-ebeln_migo = wa_saida-ebeln_migo. wa_log_batch-mblnr_migo = wa_saida-mblnr_migo. wa_log_batch-xblnr_migo = wa_saida-xblnr_migo. wa_log_batch-mensagem = v_mensage-msgtx. MOVE v_mensage-msgtx TO wa_saida_batch-mensag_part1. APPEND wa_saida_batch TO t_saida_batch. IF wa_return_bdc-msgtyp EQ c_e. v_erro_batch = 'X'. wa_log_batch-status = 'ERRO'. APPEND wa_log_batch TO t_log_batch. ELSE. wa_log_batch-status = 'SUCESSO'. ENDIF. ENDLOOP. IF NOT wa_log_batch IS INITIAL AND v_erro_batch IS INITIAL. APPEND wa_log_batch TO t_log_batch. ENDIF. REFRESH t_bdc. CLEAR: v_cont_mblnr, v_cont_batch, v_bdc_cursor. MESSAGE text-004 TYPE c_s DISPLAY LIKE c_s. ELSE. REFRESH t_bdc. CLEAR: v_cont_mblnr, v_cont_batch, v_bdc_cursor. ENDIF. IF NOT t_log_batch[] IS INITIAL. CALL SCREEN '0110' STARTING AT 10 5 ENDING AT 130 17. ENDIF. WHEN 'AJUS'. CALL TRANSACTION 'ZAJUSTE_MAT'.

* *

CLEAR: t_ekko[],t_ekpo[], t_mseg[], t_mkpf[], wa_ekpo, wa_mseg, wa_mkpf, wa_bkpf. ***Para efeito de performance e atualizao de tabelas internas. SELECT ebeln INTO TABLE t_ekko FROM ekko WHERE ebeln IN s_ebeln AND bukrs EQ c_1201. * AND aedat IN s_bldat. IF sy-subrc IS NOT INITIAL. MESSAGE text-002 TYPE c_s DISPLAY LIKE c_e. EXIT. ENDIF. SELECT ebeln ebelp bukrs werks lgort INTO TABLE t_ekpo FROM ekpo FOR ALL ENTRIES IN t_ekko WHERE ebeln EQ t_ekko-ebeln AND bukrs EQ c_1201 AND werks IN s_werks. IF sy-subrc IS NOT INITIAL. MESSAGE text-002 TYPE c_s DISPLAY LIKE c_e. EXIT. ENDIF. SELECT mblnr zeile bukrs ebeln ebelp INTO CORRESPONDING FIELDS OF TABLE t_mseg FROM mseg FOR ALL ENTRIES IN t_ekko WHERE ebeln EQ t_ekko-ebeln AND gjahr = '2012'"IN s_mjahr AND bwart EQ c_122. IF sy-subrc IS NOT INITIAL. MESSAGE text-002 TYPE c_s DISPLAY LIKE c_e. EXIT. ENDIF. SELECT mblnr mjahr xblnr INTO TABLE t_mkpf FROM mkpf FOR ALL ENTRIES IN t_mseg WHERE mblnr EQ t_mseg-mblnr AND mjahr IN s_mjahr. IF sy-subrc IS NOT INITIAL. MESSAGE text-002 TYPE c_s DISPLAY LIKE c_e. EXIT. ENDIF. SELECT bukrs belnr gjahr xblnr INTO TABLE t_bkpf FROM bkpf FOR ALL ENTRIES IN t_mkpf WHERE bukrs EQ c_1201 AND gjahr IN s_mjahr

AND xblnr EQ t_mkpf-xblnr AND tcode EQ c_tcode. IF sy-subrc IS NOT INITIAL. MESSAGE text-002 TYPE c_s DISPLAY LIKE c_e. EXIT. ENDIF. SELECT bukrs belnr gjahr ebeln ebelp INTO TABLE t_bseg FROM bseg FOR ALL ENTRIES IN t_bkpf WHERE bukrs EQ c_1201 AND belnr EQ t_bkpf-belnr AND gjahr EQ t_bkpf-gjahr. IF sy-subrc IS NOT INITIAL. MESSAGE text-002 TYPE c_s DISPLAY LIKE c_e. EXIT. ENDIF. SORT: t_ekpo t_mseg t_mkpf t_bkpf t_bseg BY BY BY BY BY ebeln ebelp, mblnr, mblnr, xblnr, ebeln ebelp.

LOOP AT t_mseg INTO wa_mseg. READ TABLE t_ekpo INTO wa_ekpo WITH KEY ebeln = wa_mseg-ebeln ebelp = wa_mseg-ebelp BINARY SEARCH. IF sy-subrc IS INITIAL. wa_saida_aux-ebeln = wa_ekpo-ebeln. wa_saida_aux-ebelp = wa_ekpo-ebelp. wa_saida_aux-mblnr = wa_mseg-mblnr. ENDIF. READ TABLE t_mkpf INTO wa_mkpf WITH KEY mblnr = wa_mseg-mblnr BINARY SEARCH. IF sy-subrc IS INITIAL. READ TABLE t_bkpf INTO wa_bkpf WITH KEY xblnr = wa_mkpf-xblnr BINARY SEARCH. IF sy-subrc IS INITIAL. wa_saida_aux-belnr = wa_bkpf-belnr. wa_saida_aux-bukrs = wa_bkpf-bukrs. wa_saida_aux-gjahr = wa_bkpf-gjahr. ENDIF. ENDIF. APPEND wa_saida_aux TO t_saida_aux. ENDLOOP. WHILE v_fullpath IS INITIAL. CALL METHOD cl_gui_frontend_services=>file_save_dialog EXPORTING window_title = c_title default_extension = c_extensao file_filter = c_extensao initial_directory = c_diretorio CHANGING

filename = v_nomefile path = v_path fullpath = v_fullpath EXCEPTIONS cntl_error = 1 error_no_gui = 2 not_supported_by_gui = 3 OTHERS = 4. IF sy-subrc <> 0. * MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO * WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4. ENDIF. ENDWHILE. ***Formata o cabealho do excel CLEAR wa_saida_excel. wa_saida_excel-linha_1 = 'Ped. Compra'. wa_saida_excel-linha_2 = 'Item'. wa_saida_excel-linha_3 = 'Doc. Materi'. wa_saida_excel-linha_4 = 'Doc. Contab'. wa_saida_excel-linha_5 = 'Empresa'. wa_saida_excel-linha_6 = 'Exercicio'. APPEND wa_saida_excel TO t_saida_excel. CLEAR wa_saida_excel. LOOP AT t_saida_aux INTO wa_saida_aux. wa_saida_excel-linha_1 = wa_saida_aux-ebeln. wa_saida_excel-linha_2 = wa_saida_aux-ebelp. wa_saida_excel-linha_3 = wa_saida_aux-mblnr. wa_saida_excel-linha_4 = wa_saida_aux-belnr. wa_saida_excel-linha_5 = wa_saida_aux-bukrs. wa_saida_excel-linha_6 = wa_saida_aux-gjahr. APPEND wa_saida_excel TO t_saida_excel. ENDLOOP. CALL METHOD cl_gui_frontend_services=>gui_download EXPORTING filename = v_fullpath filetype = 'DAT' CHANGING data_tab = t_saida_excel EXCEPTIONS file_write_error = 1 no_batch = 2 gui_refuse_filetransfer = 3 invalid_type = 4 no_authority = 5 unknown_error = 6 header_not_allowed = 7 separator_not_allowed = 8 filesize_not_allowed = 9 header_too_long = 10 dp_error_create = 11 dp_error_send = 12 dp_error_write = 13 unknown_dp_error = 14 access_denied = 15 dp_out_of_memory = 16 disk_full = 17 dp_timeout = 18 file_not_found = 19

dataprovider_exception control_flush_error not_supported_by_gui error_no_gui OTHERS

= = = = =

20 21 22 23 24.

ENDCASE. ENDMODULE. " USER_COMMAND_0100 INPUT *&---------------------------------------------------------------------* *& Form INSERE_BDC *&---------------------------------------------------------------------* * Processa o dados para enviar a transao MBRL, via bath. *----------------------------------------------------------------------* * -->p_dynbegin Tela * -->p_field Campo * -->p_value Valor *----------------------------------------------------------------------* FORM insere_bdc USING p_dynbegin p_field p_value. CLEAR wa_bdc. IF p_dynbegin EQ 'X'. wa_bdc-dynbegin = 'X'. wa_bdc-program = p_field. wa_bdc-dynpro = p_value. ELSE. wa_bdc-fnam = p_field. wa_bdc-fval = p_value. ENDIF. APPEND wa_bdc TO t_bdc. ENDFORM. " INSERE_BDC *&---------------------------------------------------------------------* *& Module USER_COMMAND_0200 INPUT *&---------------------------------------------------------------------* * text *----------------------------------------------------------------------* MODULE user_command_0200 INPUT. CASE sy-ucomm. WHEN 'BACK'. LEAVE SCREEN. WHEN 'CANC' OR 'EXIT'. LEAVE PROGRAM. ENDCASE. *&---------------------------------------------------------------------* *& Module STATUS_0110 OUTPUT *&---------------------------------------------------------------------* * text *----------------------------------------------------------------------* MODULE status_0110 OUTPUT. SET PF-STATUS '0110'.

SET TITLEBAR '0110'. ENDMODULE. " STATUS_0110 OUTPUT *&---------------------------------------------------------------------* *& Module USER_COMMAND_0110 INPUT *&---------------------------------------------------------------------* * text *----------------------------------------------------------------------* MODULE user_command_0110 INPUT. ok_code = sy-ucomm. CASE ok_code. WHEN 'BACK'. LEAVE TO SCREEN 0. WHEN OTHERS. ENDCASE. ENDMODULE. " USER_COMMAND_0110 INPUT *&---------------------------------------------------------------------* *& Module TRANS_TO_ITAB INPUT *&---------------------------------------------------------------------* * text *----------------------------------------------------------------------* MODULE trans_to_itab INPUT. ENDMODULE. " TRANS_TO_ITAB INPUT