0% found this document useful (0 votes)
151 views6 pages

Funcao Estorno

This document contains code for importing data from a Brazilian tax document (NF-e) into SAP. It extracts item and tax information from the NF-e, calculates unit cost values, and inserts new condition records into table 373 with the calculated unit costs for conditions ZCT0 and ZCUS. Various data types are defined to store the extracted NF-e data and calculated values. Loops are used to extract the item and tax lines, lookup reference data, perform the cost calculations, and insert the new condition records.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
151 views6 pages

Funcao Estorno

This document contains code for importing data from a Brazilian tax document (NF-e) into SAP. It extracts item and tax information from the NF-e, calculates unit cost values, and inserts new condition records into table 373 with the calculated unit costs for conditions ZCT0 and ZCUS. Various data types are defined to store the extracted NF-e data and calculated values. Loops are used to extract the item and tax lines, lookup reference data, perform the cost calculations, and insert the new condition records.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 6

ENHANCEMENT 1 ZESSD_I_NF_DOC_INSERT_FROM_OBJ.

"active version
TYPES: BEGIN OF ty_refkey,
mblnr TYPE zmmt215-mblnr,
mjahr TYPE zmmt215-mjahr,
END OF ty_refkey.
DATA: vl_ztpcc TYPE zsdt101-ztpcc,
vl_custo TYPE j_1bnflin-netwrt,
vl_bicms TYPE j_1bnfstx-base,
vl_icms TYPE j_1bnfstx-taxval,
vl_ipi TYPE j_1bnfstx-taxval,
vl_pis TYPE j_1bnfstx-taxval,
vl_cof TYPE j_1bnfstx-taxval,
wl_j1bnfstx TYPE j_1bnfstx,
wl_item TYPE j_1bnflin,
wl_header TYPE j_1bnfdoc,
wl_item_tax TYPE j_1bnfstx,
wl_key TYPE komg,
vl_date_from TYPE rv13a-datab,
vl_date_to TYPE rv13a-datbi,
wl_zsdt101 TYPE zsdt101,
wl_refkey TYPE ty_refkey.
DATA: lt_copy_records TYPE TABLE OF komv WITH HEADER LINE,
lt_key TYPE STANDARD TABLE OF komg,
lt_zsdt101 TYPE TABLE OF zsdt101,
lt_item TYPE TABLE OF j_1bnflin,
lt_header TYPE TABLE OF j_1bnfdoc,
lt_item_tax TYPE TABLE OF j_1bnfstx,
lt_j1baj TYPE TABLE OF j_1baj WITH HEADER LINE,
t_zmmt215 TYPE TABLE OF zmmt215,
lt_refkey TYPE TABLE OF ty_refkey.
FIELD-SYMBOLS: <zmmt215> LIKE LINE OF t_zmmt215.
"Se a nota for de estorno, deve-se pegar a nota no estornada mais recente.
IF lv_nfe_cancel_docnum IS NOT INITIAL.
CALL FUNCTION 'ZMMFM_ULTIMA_NF_NAO_ESTORNADA'
EXPORTING
iv_nfe_cancel_docnum = lv_nfe_cancel_docnum
TABLES
it_item = wk_item
et_header = lt_header
et_item = lt_item
et_item_tax = lt_item_tax.
IF lt_item[] IS NOT INITIAL.
LOOP AT lt_item INTO wl_item.
wl_refkey-mblnr = wl_item-refkey(10).
wl_refkey-mjahr = wl_item-refkey+10(4).
APPEND wl_refkey TO lt_refkey.
ENDLOOP.
SELECT *
INTO TABLE t_zmmt215
FROM zmmt215 FOR ALL ENTRIES IN lt_refkey
WHERE mblnr = lt_refkey-mblnr
AND mjahr = lt_refkey-mjahr.
ENDIF.
ELSE.
"Limpa o docnum, para fazer o link com os itens sem docnum
wl_header = wk_header.
READ TABLE wk_item INTO wl_item INDEX 1.
IF sy-subrc IS INITIAL AND wl_item-docnum IS INITIAL.
CLEAR wl_header-docnum.
ENDIF.
APPEND wl_header TO lt_header.
lt_item[] = wk_item[].
lt_item_tax[] = wk_item_tax[].
"Importar os dados de importao.
IMPORT t_zmmt215 = t_zmmt215 FROM DATABASE indx(st) ID 'ZMMT215'.
ENDIF.
"Remove o parmetro
DELETE FROM DATABASE indx(st) ID 'ZMMT215'.
* Selecionar informaes de controle de categoria de nota fiscal para atualizao do cus
to
SELECT *
INTO TABLE lt_zsdt101
FROM zsdt101 FOR ALL ENTRIES IN lt_header
WHERE nftype = lt_header-nftype.
* Selecionar grupos de impostos para cada tipo de imposto da NF
SELECT *
INTO TABLE lt_j1baj
FROM j_1baj.
CLEAR: vl_custo, vl_bicms, vl_icms, vl_ipi, vl_pis, vl_cof.
IF NOT lt_zsdt101[] IS INITIAL.
* Elimina itens duplicados prevalecendo o ltimo item da NF
SORT lt_item BY matnr itmnum DESCENDING.
DELETE ADJACENT DUPLICATES FROM lt_item COMPARING matnr.
SORT lt_item BY docnum itmnum.
SORT lt_zsdt101 BY nftype cfop.
SORT lt_item_tax BY docnum itmnum.
SORT lt_j1baj BY taxtyp.
SORT t_zmmt215 BY zeile.
LOOP AT lt_header INTO wl_header.
READ TABLE lt_item TRANSPORTING NO FIELDS
WITH KEY docnum = wl_header-docnum.
LOOP AT lt_item INTO wl_item FROM sy-tabix.
"Para item no estornado, docnum no estar preenchido
IF wl_item-docnum <> wl_header-docnum.
EXIT.
ENDIF.
READ TABLE lt_zsdt101 INTO wl_zsdt101
WITH KEY nftype = wl_header-nftype
cfop = wl_item-cfop BINARY SEARCH.
IF sy-subrc = 0.
CLEAR vl_icms. "JGMO 23/05/2013
IF t_zmmt215[] IS INITIAL.
READ TABLE lt_item_tax TRANSPORTING NO FIELDS
WITH KEY docnum = wl_item-docnum
itmnum = wl_item-itmnum BINARY SEARCH.
LOOP AT lt_item_tax INTO wl_item_tax FROM sy-tabix.
IF NOT ( wl_item_tax-docnum = wl_item-docnum AND
wl_item_tax-itmnum = wl_item-itmnum ).
EXIT.
ENDIF.
READ TABLE lt_j1baj WITH KEY taxtyp = wl_item_tax-taxtyp BINARY S
EARCH.
IF SY-SUBRC IS INITIAL.
CASE lt_j1baj-taxgrp.
WHEN 'ICMS'.
vl_icms = vl_icms + wl_item_tax-taxval. "Acumular o ICMS
do Benefcio fiscal
vl_bicms = wl_item_tax-base + wl_item_tax-othbas.
WHEN 'IPI'.
vl_ipi = wl_item_tax-taxval.
WHEN 'PIS'.
vl_pis = wl_item_tax-taxval.
WHEN 'COFI'.
vl_cof = wl_item_tax-taxval.
ENDCASE.
ENDIF.
ENDLOOP.
ELSE.
READ TABLE t_zmmt215 TRANSPORTING NO FIELDS
WITH KEY zeile = wl_item-refitm BINARY SEARCH.

LOOP AT t_zmmt215 ASSIGNING <zmmt215> FROM sy-tabix.
IF <zmmt215>-zeile <> wl_item-refitm.
EXIT.
ENDIF.
wl_item-netwr = <zmmt215>-vmerc.
wl_item-netfre = <zmmt215>-vfrete.
wl_item-netins = <zmmt215>-vseg.
wl_item-netoth = <zmmt215>-vdesp + <zmmt215>-vsiscomex + <zmmt215>-
vii + <zmmt215>-viof.
vl_icms = <zmmt215>-vicms.
vl_ipi = <zmmt215>-vipi.
vl_pis = <zmmt215>-vpis.
vl_cof = <zmmt215>-vcof.
ENDLOOP.
ENDIF.
CASE wl_zsdt101-ztpcc.
* Se tipo de clculo: A (Clculo com o valor total do item da NF menos o valor
da ST)
WHEN 'A'.
vl_custo = ( ( wl_item-netwr +
"Preo lquido da mercad.
wl_item-netfre + wl_item-netins + wl_item-netoth + w
l_item-netdis + "Frete, Seguro, Outros e Desc.
vl_icms + vl_ipi + vl_pis + vl_cof ) /
"ICMS+IPI+PIS+COFINS
wl_item-menge * 10000 ).
"Custo unitrio X 10000.
* Se tipo de clculo: B (Clculo com a base de clculo do ICMS)
WHEN 'B'.
vl_custo = ( vl_bicms / wl_item-menge ) * 10000. "Base do
ICMS unitria X 10000.
WHEN OTHERS.
ENDCASE.
READ TABLE lt_key TRANSPORTING NO FIELDS WITH KEY werks = wl_item-werks
matnr = wl_item-matnr.
check sy-subrc <> 0.
* Preparar para cadastro da condio ZCT0
CLEAR: wl_key, vl_date_from, vl_date_to.
REFRESH lt_copy_records.
wl_key-aland = 'BR'.
wl_key-werks = wl_item-werks.
wl_key-matnr = wl_item-matnr.
vl_date_from = '20120101'.
vl_date_to = '99991231'.
lt_copy_records-kappl = 'V'.
lt_copy_records-kschl = 'ZCT0'.
lt_copy_records-waers = 'BRL'.
lt_copy_records-kmein = wl_item-meins.
lt_copy_records-kpein = '10000'.
lt_copy_records-krech = 'C'.
lt_copy_records-kbetr = vl_custo.
APPEND lt_copy_records.
append wl_key to lt_key.
SET UPDATE TASK LOCAL.
CALL FUNCTION 'RV_CONDITION_COPY'
EXPORTING
application = lt_copy_records-kappl
condition_table = '373'
condition_type = lt_copy_records-kschl
date_from = vl_date_from
key_fields = wl_key
maintain_mode = 'A'
no_authority_check = 'X'
keep_old_records = 'X'
overlap_confirmed = 'X'
no_db_update = space
TABLES
copy_records = lt_copy_records
EXCEPTIONS
enqueue_on_record = 1
invalid_application = 2
invalid_condition_number = 3
invalid_condition_type = 4
no_authority_ekorg = 5
no_authority_kschl = 6
no_authority_vkorg = 7
no_selection = 8
table_not_valid = 9
no_material_for_settlement = 10
no_unit_for_period_cond = 11
no_unit_reference_magnitude = 12
invalid_condition_table = 13
OTHERS = 14.
IF sy-subrc <> 0.
* Implement suitable error handling here
ELSE.
CALL FUNCTION 'RV_CONDITION_SAVE'.
CALL FUNCTION 'RV_CONDITION_RESET'.
ENDIF.
* Preparar para cadastro da condio ZCUS
CLEAR: wl_key, vl_date_from, vl_date_to.
REFRESH lt_copy_records.
wl_key-aland = 'BR'.
wl_key-werks = wl_item-werks.
wl_key-matnr = wl_item-matnr.
vl_date_from = sy-datum + 1.
vl_date_to = '99991231'.
lt_copy_records-kappl = 'V'.
lt_copy_records-kschl = 'ZCUS'.
lt_copy_records-waers = 'BRL'.
lt_copy_records-kmein = wl_item-meins.
lt_copy_records-kpein = '10000'.
lt_copy_records-krech = 'C'.
lt_copy_records-kbetr = vl_custo.
APPEND lt_copy_records.
CALL FUNCTION 'RV_CONDITION_COPY'
EXPORTING
application = lt_copy_records-kappl
condition_table = '373'
condition_type = lt_copy_records-kschl
date_from = vl_date_from
key_fields = wl_key
maintain_mode = 'A'
no_authority_check = 'X'
keep_old_records = 'X'
overlap_confirmed = 'X'
no_db_update = space
TABLES
copy_records = lt_copy_records
EXCEPTIONS
enqueue_on_record = 1
invalid_application = 2
invalid_condition_number = 3
invalid_condition_type = 4
no_authority_ekorg = 5
no_authority_kschl = 6
no_authority_vkorg = 7
no_selection = 8
table_not_valid = 9
no_material_for_settlement = 10
no_unit_for_period_cond = 11
no_unit_reference_magnitude = 12
invalid_condition_table = 13
OTHERS = 14.
IF sy-subrc <> 0.
* Implement suitable error handling here
ELSE.
CALL FUNCTION 'RV_CONDITION_SAVE'.
CALL FUNCTION 'RV_CONDITION_RESET'.
ENDIF.
ENDIF.
ENDLOOP.
ENDLOOP.
ENDIF.
ENDENHANCEMENT.

You might also like