Professional Documents
Culture Documents
*----------------------------------------------------------------------*
* TABLES *
*----------------------------------------------------------------------*
* tables ---------------------------------------------------------------
TABLES: j_1bnfdoc,
vbrk, " billing document header
bkpf, " financial document header
j_1bnfe_active,
j_1b_nfe_access_key.
*----------------------------------------------------------------------*
* INCLUDES *
*----------------------------------------------------------------------*
* INCLUDE for General Table Descriptions for Print Programs ------------
INCLUDE rvadtabl.
INCLUDE ZNFE_J_1BNFPR_PRINTINC.
*----------------------------------------------------------------------*
* STRUCTURES *
*----------------------------------------------------------------------*
* Nota Fiscal header structure -----------------------------------------
DATA: BEGIN OF wk_header.
INCLUDE STRUCTURE j_1bnfdoc.
DATA: END OF wk_header.
*----------------------------------------------------------------------*
* DATA AND CONSTANTS *
*----------------------------------------------------------------------*
DATA: wk_docnum TYPE j_1bnfdoc-docnum,
retcode TYPE sy-subrc,
xscreen,
wk_xblnr TYPE bkpf-xblnr,
subrc_upd_bi TYPE sy-subrc.
DATA: bi_subrc TYPE sy-subrc,
fi_subrc TYPE sy-subrc.
*======================================================================*
* PROGRAM *
*======================================================================*
*&---------------------------------------------------------------------*
*& FORM ENTRY (MAIN FORM) *
*&---------------------------------------------------------------------*
* Form for Message Control *
*----------------------------------------------------------------------*
FORM entry USING return_code us_screen.
CLEAR: retcode.
xscreen = us_screen.
IF sy-subrc EQ 0.
IF NOT v_sform IS INITIAL.
tnapr-sform = v_sform.
CLEAR tnapr-funcname.
CLEAR tnapr-fonam.
ENDIF.
ENDIF.
IF lr_badi IS INITIAL.
ENDIF.
PERFORM smart_sub_printing.
* main -----------------------------------------------------------------
*&---------------------------------------------------------------------*
*& Form NOTA_FISCAL_READ
*&---------------------------------------------------------------------*
* Read the Nota Fiscal based in the key giving by Message *
* Control. *
*----------------------------------------------------------------------*
FORM nota_fiscal_read.
PERFORM check_error.
*&---------------------------------------------------------------------*
*& Form NOTA_FISCAL_UPDATE
*&---------------------------------------------------------------------*
* Update NF date and number *
*----------------------------------------------------------------------*
FORM nota_fiscal_update.
wk_header-printd = 'X'.
IF sy-subrc <> 0.
retcode = sy-subrc.
syst-msgid = '8B'.
syst-msgno = '107'.
syst-msgty = 'E'.
syst-msgv1 = wk_header-docnum.
PERFORM protocol_update.
ENDIF.
IF sy-subrc <> 0.
retcode = sy-subrc.
PERFORM protocol_update.
ENDIF.
SORT wk_item.
READ TABLE wk_item INDEX 1.
* get the type of the document and update the documents ----------------
CASE wk_item-reftyp.
WHEN 'BI'.
MOVE wk_item-refkey TO key_vbrk.
PERFORM read_bi_document.
CLEAR bkpf.
IF NOT vbrk IS INITIAL. " if find VBRK (Billing document)
PERFORM get_fi_number.
ENDIF.
IF bkpf-belnr IS INITIAL. " there is not FI document
IF NOT vbrk IS INITIAL. " if find VBRK (Billing document)
PERFORM update_bi_document.
ENDIF.
ELSE. " there is FI document
PERFORM update_bi_document.
IF subrc_upd_bi IS INITIAL. " update in billing ok.
PERFORM update_fi_nf_document
USING bkpf-bukrs bkpf-belnr bkpf-gjahr.
PERFORM update_bsid_nf_document
USING bkpf-bukrs bkpf-belnr bkpf-gjahr.
ENDIF.
ENDIF.
*&---------------------------------------------------------------------*
*& Form READ_BI_DOCUMENT
*&---------------------------------------------------------------------*
* This form read the billing document *
*----------------------------------------------------------------------*
FORM read_bi_document.
IF sy-subrc <> 0.
CLEAR vbrk.
ENDIF.
*&---------------------------------------------------------------------*
*& Form READ_FI_DOCUMENT
*&---------------------------------------------------------------------*
* read the fi_document *
*----------------------------------------------------------------------*
FORM read_fi_document USING xbukrs xbelnr xgjahr.
IF sy-subrc <> 0.
CLEAR bkpf.
ENDIF.
*&---------------------------------------------------------------------*
*& Form UPDATE_BI_DOCUMENT
*&---------------------------------------------------------------------*
* Update billing document *
*----------------------------------------------------------------------*
FORM update_bi_document.
PERFORM check_error.
subrc_upd_bi = sy-subrc.
subrc_upd_bi = sy-subrc.
ENDIF.
*&---------------------------------------------------------------------*
*& Form UPDATE_FI_NF_DOCUMENT
*&---------------------------------------------------------------------*
* Update financial and nota fiscal document *
*----------------------------------------------------------------------*
FORM update_fi_nf_document USING xbukrs xbelnr xgjahr.
PERFORM check_error.
wk_header-follow = 'X'.
ENDIF.
*&---------------------------------------------------------------------*
*& Form GET_FI_NUMBER
*&---------------------------------------------------------------------*
* Read financial document via Billing document number *
*----------------------------------------------------------------------*
FORM get_fi_number.
IF sy-subrc <> 0.
CLEAR bkpf.
ENDIF.
*&---------------------------------------------------------------------*
*& Form UPDATE_BSID_NF_DOCUMENT
*&---------------------------------------------------------------------*
* update table BSID with external Nota Fiscal number - KI3K050466
* change 23.01.97
* Change 28.06.2000:
* update also BSIS if G/L account with line item display exists
* Change 09.08.2000: if cust account already cleared
* (e.g. credit card sales) update BSAD instead of BSID
*----------------------------------------------------------------------*
FORM update_bsid_nf_document USING xbukrs xbelnr xgjahr.
TABLES: bseg,
bsid,
bsis,
bsad.
*---------------------------------------------------------------------*
* FORM ENQUEUE_BI_FI *
*---------------------------------------------------------------------*
* ........ *
*---------------------------------------------------------------------*
FORM enqueue_bi_fi.
CLEAR bi_subrc.
PERFORM read_bi_document.
bi_subrc = sy-subrc.
PERFORM check_error.
IF bi_subrc = 0. "BI document not locked
CLEAR bkpf.
PERFORM get_fi_number.
IF NOT bkpf-belnr IS INITIAL.
CALL FUNCTION 'ENQUEUE_EFBKPF'
EXPORTING
bukrs = bkpf-bukrs
belnr = bkpf-belnr
gjahr = bkpf-gjahr
EXCEPTIONS
foreign_lock = 1
system_failure = 2
OTHERS = 3.
fi_subrc = sy-subrc.
PERFORM check_error.
IF fi_subrc <> 0. "FI lock not successful -> release BI lock
CALL FUNCTION 'DEQUEUE_EVVBRKE'
EXPORTING
mandt = sy-mandt
vbeln = key_vbrk-vbeln
EXCEPTIONS
OTHERS = 1.
PERFORM check_error.
ENDIF.
ENDIF.
ENDIF.
ENDIF.
PERFORM check_error.
IF sy-batch IS INITIAL. " corr. of note 442570
MESSAGE e678 WITH wk_header-docnum.
ENDIF. " corr. of note 442570
ENDIF.
ENDFORM. " check_nf_canceled
*&---------------------------------------------------------------------*
*& Form check_nfe_authorized
*&---------------------------------------------------------------------*
FORM check_nfe_authorized.
DATA: lv_dummy TYPE c,
lv_subrc TYPE sy-subrc,
obj_ref TYPE REF TO if_ex_cl_nfe_print.
CLEAR gs_nfeactive.
* only NFes
CHECK wk_header-nfe = 'X'.
IF gs_nfeactive-code IS INITIAL.
ENDIF.
j_1bnfe_active = gs_nfeactive.
lv_subrc = 1.
ELSE.
IF obj_ref IS INITIAL.
IF sy-subrc IS INITIAL.
*- nothing to do
ENDIF.
ENDIF.
IF obj_ref IS BOUND.
CALL METHOD obj_ref->reset_subrc
EXPORTING
is_nfdoc = wk_header
CHANGING
ch_subrc = lv_subrc.
ENDIF.
sy-subrc = lv_subrc.
IF sy-subrc <> 0.
* MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
* WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
ENDIF.
REFRESH: my_items.
ENDIF.
* for NFe the DANFE is printed. Numbering has already taken place
* before sending teh XML document to SEFAZ.
* Billing document is updated for NFe with the 9 digit NFe number
IF wk_header-entrad = 'X' OR "update only entradas or outgoing NF
wk_header-direct = '2'.
IF wk_header-printd IS INITIAL AND " not printed.
wk_header-nfnum IS INITIAL AND " without NF number
nast-nacha = '1'. " sent to printer
PERFORM enqueue_bi_fi.
CHECK retcode IS INITIAL.
* get NF number only for "normal NFs" NFe has already the number
IF wk_header-nfe IS INITIAL.
PERFORM nota_fiscal_number. " get the next number
ENDIF.
IF retcode IS INITIAL.
PERFORM financial_doc_update. " update in database
PERFORM nota_fiscal_update. " update in database
IF NOT gs_nfeactive IS INITIAL.
PERFORM active_update ON COMMIT.
ENDIF.
ENDIF.
ENDIF.
ENDIF.
IF retcode IS INITIAL.
ELSE.
MESSAGE a114 WITH '01' 'J_1BNFNUMB'.
ENDIF.
*----------------------------------------------------------------------*
* read tax types into internal buffer table *
*----------------------------------------------------------------------*
SELECT * FROM j_1baj INTO TABLE tax_types ORDER BY PRIMARY KEY.
CLEAR w_danfe.
CLEAR: w_danfe-issuer,
w_danfe-destination,
w_danfe-carrier,
w_danfe-nota_fiscal,
w_danfe-others,
w_danfe-nfe,
w_danfe-observ1,
w_danfe-observ2,
w_danfe-item,
w_danfe-invoice.
*----------------------------------------------------------------------*
* fill header data into communication structure *
*----------------------------------------------------------------------*
MOVE-CORRESPONDING wk_header TO w_danfe-nota_fiscal.
SELECT SINGLE *
FROM j_1bnfe_active
INTO w_danfe-nfe
WHERE docnum EQ wk_header-docnum.
LOOP AT wk_item.
CONCATENATE wk_item-cfop(3) '0' wk_item-cfop+4(2) INTO v_cfop.
wk_item-cfop = v_cfop.
WRITE wk_item-cfop TO help_cfop.
help_cfop = help_cfop(cfop_length).
CASE extension_length.
WHEN 1.
IF ( wk_item-cfop+1(3) = '991' OR wk_item-cfop+1(3) = '999' )
AND issuer_region = 'SP'.
CONCATENATE help_cfop '.' wk_item-cfop+3(1) INTO help_cfop.
ENDIF.
WHEN 2.
IF wk_item-cfop+1(2) = '99' AND issuer_region = 'SC'.
CONCATENATE help_cfop '.' wk_item-cfop+3(2) INTO help_cfop.
ENDIF.
ENDCASE.
*----------------------------------------------------------------------*
* determine issuer and destination (only for test) *
*----------------------------------------------------------------------*
IF wk_header-direct = '1' AND
wk_header-entrad = ' '.
issuer-partner_type = wk_header-partyp.
issuer-partner_id = wk_header-parid.
issuer-partner_function = wk_header-parvw.
destination-partner_type = 'B'.
destination-partner_id = wk_header-bukrs.
destination-partner_id+4 = wk_header-branch.
ELSE.
issuer-partner_type = 'B'.
issuer-partner_id = wk_header-bukrs.
issuer-partner_id+4 = wk_header-branch.
destination-partner_type = wk_header-partyp.
destination-partner_id = wk_header-parid.
destination-partner_function = wk_header-parvw.
ENDIF.
*----------------------------------------------------------------------*
* read branch data (issuer) *
*----------------------------------------------------------------------*
CLEAR j_1binnad.
*----------------------------------------------------------------------*
* read destination data *
*----------------------------------------------------------------------*
CLEAR j_1binnad.
CLEAR v_linha.
IF wk_header-fatura = 'X'.
IF wk_header-zterm NE space.
SELECT *
FROM t052
WHERE zterm = wk_header-zterm
ORDER BY PRIMARY KEY.
EXIT.
ENDSELECT.
SELECT *
FROM t052s
INTO TABLE int_t052s
WHERE zterm = wk_header-zterm
ORDER BY PRIMARY KEY.
IF t052slines > 3.
t052slines = 3. "max. number of holdbacks printed on NF
ENDIF.
DO v_loops TIMES.
ADD 1 TO v_linha.
LOOP AT ztext.
CASE sy-tabix.
WHEN 1.
j_1bprnffa-txt02 = ztext-text1.
WHEN 2.
j_1bprnffa-txt03 = ztext-text1.
WHEN 3.
j_1bprnffa-txt04 = ztext-text1.
WHEN 4.
j_1bprnffa-txt01 = ztext-text1.
ENDCASE.
ENDLOOP.
APPEND j_1bprnffa TO w_danfe-invoice.
ENDIF.
ENDIF.
ENDIF.
*----------------------------------------------------------------------*
* read carrier data *
*----------------------------------------------------------------------*
IF wk_header-doctyp NE '2'. "no carrier for Complementars
CLEAR j_1binnad.
CALL FUNCTION 'J_1B_NF_PARTNER_READ'
EXPORTING
partner_type = wk_partner-partyp
partner_id = wk_partner-parid
partner_function = wk_partner-parvw
doc_number = wk_header-docnum
IMPORTING
parnad = j_1binnad
EXCEPTIONS
partner_not_found = 1
partner_type_not_found = 2
OTHERS = 3.
MOVE-CORRESPONDING j_1binnad TO w_danfe-carrier.
ENDIF.
*----------------------------------------------------------------------*
* read reference NF *
*----------------------------------------------------------------------*
IF w_danfe-nota_fiscal-docref <> space.
SELECT SINGLE * FROM j_1bnfdoc INTO *j_1bnfdoc
WHERE docnum = w_danfe-nota_fiscal-docref.
w_danfe-nota_fiscal-nf_docref = *j_1bnfdoc-nfnum.
w_danfe-nota_fiscal-nf_serref = *j_1bnfdoc-series.
w_danfe-nota_fiscal-nf_subref = *j_1bnfdoc-subser.
w_danfe-nota_fiscal-nf_datref = *j_1bnfdoc-docdat.
ENDIF.
*----------------------------------------------------------------------*
* get information about form *
*----------------------------------------------------------------------*
PERFORM check_error.
CHECK retcode IS INITIAL.
*----------------------------------------------------------------------*
* write texts to TEXTS window *
*----------------------------------------------------------------------*
DATA: w_line TYPE tline.
LOOP AT wk_header_msg.
w_line-tdline = wk_header_msg-message.
IF sy-index LT istart.
IF sy-index EQ 1.
w_danfe-observ1 = wk_header_msg-message.
ELSE.
CONCATENATE w_danfe-observ1
cl_abap_char_utilities=>cr_lf
wk_header_msg-message
INTO w_danfe-observ1.
ENDIF.
APPEND w_line TO w_danfe-text1.
ELSE.
IF sy-index EQ istart.
w_danfe-observ2 = wk_header_msg-message.
ELSE.
CONCATENATE w_danfe-observ2
cl_abap_char_utilities=>cr_lf
wk_header_msg-message
INTO w_danfe-observ2.
ENDIF.
APPEND w_line TO w_danfe-text2.
ENDIF.
ENDLOOP.
LOOP AT wk_item.
CLEAR j_1bprnfli.
MOVE-CORRESPONDING wk_item TO j_1bprnfli.
MOVE-CORRESPONDING wk_item_add TO j_1bprnfli.
ENDLOOP.
PERFORM call_smartform.
ENDFORM. "smart_sub_printing
*&---------------------------------------------------------------------
*& Form GET_CFOP_LENGTH_SMART
*&---------------------------------------------------------------------
* text
*----------------------------------------------------------------------
FORM get_cfop_length_smart USING p_bukrs
p_branch
p_pstdat
CHANGING p_version " note 593218
p_clength
p_elength
p_text
p_region. " note 593218
*&---------------------------------------------------------------------
*& Form ENCODING_CFOP_SMART
*&---------------------------------------------------------------------
* encode the CFOP
* 51234 => 51234
* 5123A => 5123A
* 512345 => 512345
* 51234A => 51234A
* 5123B4 => 5123B4
* 5123BA => 5123BA
*----------------------------------------------------------------------
FORM encoding_cfop_smart CHANGING p_cfop.
helpstring =
'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ[-<>=!?]'.
output_options-tdimmed = nast-dimme.
output_options-tddest = nast-ldest.
control_parameters-no_dialog = 'X'.
IF sy-subrc <> 0.
ENDIF.
ENDFORM. "call_smartform