Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
60 views43 pages

RPFIEU SAFT EXT SDPROC Franca

1. The document describes processes for extracting tax and invoice data from custom tables and logs for auditing purposes. 2. It extracts data from tables into internal tables, processes country-specific extensions, and deletes pro-forma documents. 3. It then gets additional related data like condition records, discounts, accounting documents and delivery documents to build structures for database updating.

Uploaded by

Daniel
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)
60 views43 pages

RPFIEU SAFT EXT SDPROC Franca

1. The document describes processes for extracting tax and invoice data from custom tables and logs for auditing purposes. 2. It extracts data from tables into internal tables, processes country-specific extensions, and deletes pro-forma documents. 3. It then gets additional related data like condition records, discounts, accounting documents and delivery documents to build structures for database updating.

Uploaded by

Daniel
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/ 43

**&--------------------------------------------------------------------*

*& Include RPFIEU_SAFT_EXT_SDPROC


*&---------------------------------------------------------------------*
*&---------------------------------------------------------------------*
*& Form RETRIEVE_TAX_DATA
*&---------------------------------------------------------------------*
*Involved process
*----------------
*1. Extract data from customization table
*2. Generate approprate log
*
*1. Extract data from customization table
*-----------------------------------------
*Retreive the tax data that has been maintained in the customisaiton
*table and retain it for further extraction process. IT_TAX_DATA to be
*cleared off contents only after the full SD/FI extraction is over.
*
* Log generation to provide following information
*------------------------------------------------
* 1.No of tax information present
* 2.If no tax information is maintained,appropriate message to be issued
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
FORM retrieve_tax_data .

SELECT * FROM saft_pt_tax_data


CLIENT SPECIFIED
INTO CORRESPONDING FIELDS OF TABLE it_tax_data
WHERE mandt = sy-mandt .

SELECT * FROM saftpt_imat INTO TABLE it_tax_material


WHERE bukrs = p_bukrs.

ENDFORM. " RETRIEVE_TAX_DATA


*&---------------------------------------------------------------------*
*& Form READ_INV_HEADER_INFO
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
FORM read_inv_header_info .
DATA :lv_count_inv TYPE i.
DATA : lv_inv_cnt TYPE i.
DATA : lv_inv_idx TYPE sy-tabix.

gv_land_exit = gv_bukrs_land.
TRY.
GET BADI gv_usr_exit_badi
FILTERS
land1 = gv_land_exit.

CATCH cx_badi_not_implemented.
ENDTRY.

CLEAR gv_usr_exit_impl.
IF gv_usr_exit_badi IS NOT INITIAL.
TRY.
CALL METHOD cl_badi_query=>number_of_implementations
EXPORTING
badi = gv_usr_exit_badi
RECEIVING
num = gv_usr_exit_impl.

CATCH cx_badi_not_implemented.
ENDTRY.
ENDIF.

"on HOLD added as cursor was getting closed automatically after 2 iterations -
20_F
OPEN CURSOR WITH HOLD cur_vbrk FOR SELECT
vbeln
fkart
fkdat
vbtyp
sfakn
fksto
erdat
erzet
zterm
bukrs
belnr
gjahr
waerk
fktyp
kunrg
kunag
stceg
kurrf
knumv
netwr
zlsch
vkorg
vtweg
ernam
mwsbk
rfbsk
* Selecting country information in case it needs to be appended
stceg_l
land1
fkdat_rl " invoice list 20_F
fkart_rl " invoice list 20_F
FROM vbrk
WHERE bukrs = p_bukrs
AND fkdat GE gv_first_date
AND fkdat LE gv_end_date
AND sfakn = '' . "#EC CI_NOFIELD
DO .
FETCH NEXT CURSOR cur_vbrk
INTO CORRESPONDING FIELDS OF TABLE it_inv_vbrk
PACKAGE SIZE 300.

IF sy-subrc <> 0.
EXIT.
ELSE.
*Country specific extension on header level - 19_f
PERFORM check_country_specific_exten.

IF it_sales_offices[] IS NOT INITIAL.


LOOP AT it_inv_vbrk INTO wa_inv_vbrk.
lv_inv_idx = sy-tabix.
CLEAR : wa_sales_offices.
READ TABLE it_sales_offices INTO wa_sales_offices WITH KEY vkorg =
wa_inv_vbrk-vkorg.
IF sy-subrc <> 0.
DELETE it_inv_vbrk INDEX lv_inv_idx .
ENDIF.
CLEAR : wa_inv_vbrk.

ENDLOOP.
ENDIF.

"Delete pro-forma invoice and internal billing documents


DELETE it_inv_vbrk WHERE vbtyp EQ 'U' OR vbtyp EQ 'Y'.
CLEAR :lv_count_inv.
DESCRIBE TABLE it_inv_vbrk LINES lv_count_inv.
* Calculate number of invoices being considerd for the current data extraction run
.
gv_invoice_no = gv_invoice_no + lv_count_inv.

SORT it_inv_vbrk .
IF it_inv_vbrk[] IS NOT INITIAL.

**Get condition record information


PERFORM get_condition_rec_info.

*get discount information linked


PERFORM get_discount_info.

* Accounting document information pertaining to the invoice documents


PERFORM get_acc_doc_info.

*Delivery document related information


CLEAR : it_inv_del_cmemo[].

*Select all relevant settlement discount information


SELECT *
FROM t052
INTO TABLE lt_zterm_all
FOR ALL ENTRIES IN it_inv_vbrk
WHERE zterm EQ it_inv_vbrk-zterm.
SORT lt_zterm_all BY zterm ztagg.

*Invoice Line item information


PERFORM get_line_item_info.

*withholding tax information.

* Assign all valid variables to the structure for database updationg


PERFORM assign_data_for_tbl_update.

PERFORM update_summary_run.

*Update header table


* set UPDATE TASK LOCAL.
PERFORM update_saft_tables.

*Clear internal tables for further data processing


CLEAR it_inv_hd[].
CLEAR it_inv_vbrk[].
CLEAR it_inv_del_cmemo[].
CLEAR it_inv_lnes[].
ENDIF.
CLEAR it_extra_fields[]. " 19_f " signature information is missed out from
2nd iteration
ENDIF.
CLEAR wa_inv_lnes. " 20_f " clearing was missing
ENDDO.
PERFORM update_sd_summary.
PERFORM display_log.
CLOSE CURSOR cur_vbrk.
* Total number of invoices selected for SAF-T File creation
DESCRIBE TABLE it_inv_vbrk LINES lv_inv_cnt .

ENDFORM. " READ_INV_HEADER_INFO


*&---------------------------------------------------------------------*
*& Form GET_CONDITION_REC_INFO
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
FORM get_condition_rec_info .

CLEAR : it_inv_cond_rec[].
OPEN CURSOR cur_konv
FOR SELECT knumv
kposn
mwsk1
FROM konv
FOR ALL ENTRIES IN it_inv_vbrk
WHERE knumv = it_inv_vbrk-knumv
AND kappl = 'V'
AND koaid = 'D'. " tax information

DO .
FETCH NEXT CURSOR cur_konv
APPENDING CORRESPONDING FIELDS OF TABLE it_inv_cond_rec
PACKAGE SIZE 1000.
IF sy-subrc <> 0.
EXIT.
ENDIF.
ENDDO.

CLOSE CURSOR cur_konv.

*-----------------Reading archiving data for the condition records already archived


for live documents*----------*
* READ THE CONDITION RECORDS ARCHIVED WHOSE BILLING DOCUNMENTS ARE STILL LIVE .....
* THE INVOICE NUMBER HAS THE CONDITION RECORD NUMBER .
* WITH THE CONDITION RECORD NUMBER , SEE IF THERE ARE ANY CONDITION RECORDS IN
ARCHIVE ..
*----------------------------------------------------------------------------------
------------------------------*
IF p_sysarc IS NOT INITIAL.

* find the condition record information that is not residing in the database.
PERFORM map_inv_condn_exists.

IF lt_condn_rec_arc[] IS NOT INITIAL.


gv_arc_tbl_read = 'KONV'.
gv_arch_oper = 'D'. "to read archoved data also when main extraction is done
for documents in database.

PERFORM set_selection_criteria USING gv_arch_oper.

PERFORM initiate_arch_read_vbak.
CLEAR gt_ais_read_result[].

gt_ais_read_result[] = gt_ais_read_result_vbak[].
IF gt_ais_read_result[] IS NOT INITIAL.
gv_arch_obj = 'SD_VBAK'.

PERFORM get_archandle USING gv_arch_obj.


CLEAR : ls_arch_handle.

LOOP AT lt_arch_handle INTO ls_arch_handle.


PERFORM read_data_from_handle.

CHECK flg_read_success = 'X'.


LOOP AT gt_arch_obj_data ASSIGNING <fs_arch_obj_data>. " 25092014
PERFORM assign_data_to_inttble .
ENDLOOP. "25092014

ENDLOOP.
ENDIF.
ENDIF.

ENDIF.
ENDFORM. " GET_CONDITION_REC_INFO
*&---------------------------------------------------------------------*
*& Form GET_DISCOUNT_INFO
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
FORM get_discount_info .
OPEN CURSOR cur_dis
FOR SELECT knumv
kposn
kschl
mwsk1
kwert
FROM konv
FOR ALL ENTRIES IN it_inv_vbrk
WHERE knumv = it_inv_vbrk-knumv.
DO .
FETCH NEXT CURSOR cur_dis
APPENDING CORRESPONDING FIELDS OF TABLE it_inv_discounts
PACKAGE SIZE 500.
IF sy-subrc <> 0.
EXIT.
ENDIF.

ENDDO.
CLOSE CURSOR cur_dis.

ENDFORM. " GET_DISCOUNT_INFO


*&---------------------------------------------------------------------*
*& Form GET_ACC_DOC_INFO
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
FORM get_acc_doc_info .
DATA : lv_awkey_vbeln TYPE vbrk-vbeln.
DATA : lt_acc_doc TYPE TABLE OF acc_doc. "16_f
DATA : ls_acc_doc LIKE LINE OF lt_acc_doc."16_f
DATA : l_space TYPE boole VALUE ' '."16_f

LOOP AT it_inv_vbrk INTO wa_inv_vbrk.

CLEAR lt_acc_doc[]. "17_f


CLEAR lv_awkey_vbeln.
CLEAR gv_idx_vbrk.

gv_idx_vbrk = sy-tabix.
lv_awkey_vbeln = wa_inv_vbrk-vbeln.

SELECT SINGLE belnr


gjahr
blart
budat
bldat
monat
kursf
FROM bkpf
INTO (wa_inv_vbrk-belnr,
wa_inv_vbrk-gjahr,
wa_inv_vbrk-blart,
wa_inv_vbrk-budat,
wa_inv_vbrk-bldat,
wa_inv_vbrk-monat,
wa_inv_vbrk-kurrf
)
WHERE bukrs = p_bukrs
AND gjahr = p_gjahr
AND awkey = lv_awkey_vbeln
AND awtyp = 'VBRK'.
CLEAR: wa_inv_vbrk-transid.
IF sy-subrc = 0.
PERFORM build_transaction_id.
ELSE.
CLEAR :lt_acc_doc[].
CALL FUNCTION 'AC_DOCUMENT_RECORD'
EXPORTING
i_awtyp = 'VBRK'
i_awref = lv_awkey_vbeln
i_bukrs = p_bukrs
x_dialog = l_space
TABLES
t_documents = lt_acc_doc
EXCEPTIONS
no_reference = 1
no_document = 2
OTHERS = 3.
IF sy-subrc <> 0.
* MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
* WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
ENDIF.

* check if the corresponding accounting document is in archive


IF p_sysarc IS NOT INITIAL AND wa_inv_vbrk-rfbsk <> 'A'.

gv_awkey = lv_awkey_vbeln.
CLEAR :ls_acc_doc.
CLEAR : lv_belnr ."17_f
CLEAR :lv_gjahr. "17_f
READ TABLE lt_acc_doc INTO ls_acc_doc WITH KEY awtyp = 'BKPF'.
IF sy-subrc = 0.
MOVE ls_acc_doc-docnr+0(10) TO lv_belnr.
MOVE ls_acc_doc-ac_gjahr TO lv_gjahr .

ENDIF.
* check if the reading from archive is simultaneously selected .
IF lv_belnr IS NOT INITIAL."19_F
PERFORM set_selivno_foracc_fiarch.
ENDIF."19_f
*23092014--------------------------------------------------*
PERFORM map_fi_header_archival.
* PERFORM map_signature_data .
*23092014--------------------------------------------------*

PERFORM read_acc_data_sdlink ."11_f


ENDIF.

IF wa_inv_vbrk-transid IS NOT INITIAL.


gv_appln_msg-msgty = 'W'. " Mandatory field missing
gv_appln_msg-msgid = gc_message_class .
gv_appln_msg-msgno = '055'.
gv_appln_msg-msgv1 = lv_awkey_vbeln.
gv_appln_msg-probclass = '3'.

CALL METHOD cl_generic_obj->gen_appl_log


EXPORTING
iv_appl_msg = gv_appln_msg
iv_land = gv_bukrs_land
iv_display = '-'
iv_handle = gv_appln_handle.
CLEAR : gv_appln_msg.
ENDIF.
ENDIF.
CLEAR : wa_inv_vbrk. "17_f
ENDLOOP.
ENDFORM. " GET_ACC_DOC_INFO
*&---------------------------------------------------------------------*
*& Form GET_LINE_ITEM_INFO
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
FORM get_line_item_info .
DATA : lv_tax_payable TYPE vbrp-mwsbp .
DATA : lv_unitprice TYPE vbrp-netwr.
DATA : lv_count_lninv TYPE i.
DATA : lv_augru TYPE vbak-augru.
DATA : lv_doc_tax_new TYPE vbrp-mwsbp.

DATA : lv_afs_field_string TYPE string,


lo_struct TYPE REF TO cl_abap_structdescr,
lt_ddfields TYPE ddfields.

SORT it_inv_vbrk.
CLEAR: lv_unitprice.

*Code to take care of AFS solution subitems.


*If there is no AFS solution the respective fields won't be available.
*So no fields will be added to the select.
lo_struct ?= cl_abap_typedescr=>describe_by_name( 'VBRP' ).
lt_ddfields = lo_struct->get_ddic_field_list( ).

READ TABLE lt_ddfields WITH KEY fieldname = 'J_3AETENR' TRANSPORTING NO FIELDS.


IF sy-subrc = 0.
lv_afs_field_string = 'j_3aetenr = ''0000'''.
ELSE.
lv_afs_field_string = ''.
ENDIF.

LOOP AT it_inv_vbrk INTO wa_inv_vbrk.


CLEAR: lv_unitprice.
MOVE sy-mandt TO wa_inv_lnes-mandt.
MOVE wa_inv_vbrk-bukrs TO wa_inv_lnes-bukrs.
MOVE wa_inv_vbrk-vbeln TO wa_inv_lnes-invoice_no.
MOVE wa_inv_vbrk-fkdat TO wa_inv_lnes-inv_date.
MOVE p_gjahr TO wa_inv_lnes-inv_year.
MOVE wa_inv_vbrk-knumv TO wa_inv_lnes-knumv.
*Invoke further checks for reporting documents like invoice list which will not
have any line item in VBRP_20_F
IF gv_extn_ext_badi IS BOUND.
CALL BADI gv_extn_ext_badi->read_futher_checks
EXPORTING
iv_condn = lv_afs_field_string
CHANGING
ct_inv_data = wa_inv_vbrk
ct_doc_data = it_vbrp.
ENDIF.

SELECT vbeln
posnr
matnr
fkimg
vrkme
mwskz
werks
netwr
mwsbp
arktx
erdat
aubel
vkbur
FROM vbrp APPENDING CORRESPONDING FIELDS OF TABLE it_vbrp
WHERE vbeln = wa_inv_vbrk-vbeln
AND kowrr = ' '
AND posar <> 'B'
AND (lv_afs_field_string).

IF sy-subrc = 0 OR it_vbrp IS NOT INITIAL. " CONDITION UPDATED SO AS TO


CONTINUE THE PROCESSING , EVEN IN CASE THE LINE ITEMS ARE NOT THERE IN vbrp
LOOP AT it_vbrp INTO wa_vbrp.
MOVE wa_vbrp-aubel TO wa_so_data-vbeln.
APPEND wa_so_data TO it_so_data.
*DELETE INVOICES LINES AND ITS CORRESPONDING HEADER IF THEY ARE NOT BELONGING TO
THE CUSTOMISED SALES OFFICES
IF it_sales_offices[] IS NOT INITIAL.

READ TABLE it_sales_offices INTO wa_sales_offices WITH KEY vkbur =


wa_vbrp-vkbur.

IF sy-subrc <> 0.
DELETE it_vbrp WHERE vbeln = wa_vbrp-vbeln.
IF sy-subrc = 0.
DELETE it_inv_vbrk WHERE vbeln = wa_vbrp-vbeln.
IF sy-subrc = 0.
gv_invoice_no = gv_invoice_no - 1.
ENDIF.
ENDIF.
CLEAR : wa_vbrp.
CLEAR : wa_sales_offices.
ELSE.
wa_inv_vbrk-vkbur = wa_vbrp-vkbur.
MODIFY it_inv_vbrk FROM wa_inv_vbrk TRANSPORTING vkbur WHERE vbeln =
wa_vbrp-vbeln.
CLEAR : wa_inv_vbrk-vkbur.

ENDIF.
ENDIF.
ENDLOOP.
SORT it_so_data .
DELETE ADJACENT DUPLICATES FROM it_so_data COMPARING ALL FIELDS.
IF it_so_data[] IS NOT INITIAL.
PERFORM identify_sales_order_info.
ENDIF.

*perform get payment mechanism info


* PERFORM get_payment_mechansim. "20_F

* lines items per invoice processed


CLEAR lv_count_lninv.
CLEAR lv_tax_payable.
DESCRIBE TABLE it_vbrp LINES lv_count_lninv.
gv_entries_lnescount = gv_entries_lnescount + lv_count_lninv.
LOOP AT it_vbrp INTO wa_vbrp.
MOVE wa_inv_vbrk-waerk TO wa_inv_lnes-currency.
MOVE wa_vbrp-posnr TO wa_inv_lnes-posnr.
MOVE wa_vbrp-posnr TO wa_inv_lnes-kposn.
MOVE wa_vbrp-aubel TO wa_inv_lnes-org_on.
IF wa_vbrp-aubel IS NOT INITIAL.
CLEAR : wa_so_data.
READ TABLE it_so_data INTO wa_so_data
WITH KEY vbeln = wa_vbrp-aubel .
IF sy-subrc = 0.
MOVE wa_so_data-audat TO wa_inv_lnes-org_date.
*payment terms info missing in the extract as the data was retrieved based on
billing data "17_f
IF p_sysarc IS NOT INITIAL OR p_archvl IS NOT INITIAL.
CLEAR : wa_inv_pay_mech.

READ TABLE it_inv_pay_mech INTO wa_inv_pay_mech


WITH KEY vbeln = wa_so_data-vbeln
posnr = wa_vbrp-posnr. " 18_f
IF sy-subrc = 0.
MOVE wa_inv_pay_mech-text1 TO wa_inv_lnes-paymentterm.
MOVE wa_inv_pay_mech-pay_date TO wa_inv_lnes-paymentdate.
ENDIF.
ENDIF.
*payment terms info missing in the extract as the data was retireved based on
billing data "17_f
ENDIF.
ENDIF.

MOVE wa_vbrp-vbtyp TO wa_inv_lnes-documnttype.

*Tax amount in line item


MOVE wa_vbrp-mwsbp TO wa_inv_lnes-taxamount.
lv_tax_payable = lv_tax_payable + wa_inv_lnes-taxamount.

*Product code and description


PERFORM get_material_info.

*Tax point Date


MOVE wa_inv_vbrk-del_date TO wa_inv_lnes-taxpointdate.
IF wa_inv_vbrk-del_date IS INITIAL.
MOVE wa_inv_vbrk-fkdat TO wa_inv_lnes-taxpointdate.
ENDIF.

*quantity & unit of measure - if unit


MOVE wa_vbrp-fkimg TO wa_inv_lnes-quantity.
MOVE wa_vbrp-vrkme TO wa_inv_lnes-unitofmeasure.

* IF wa_inv_lnes-quantity IS INITIAL OR
* wa_inv_lnes-quantity = '0.00'.
* wa_inv_lnes-quantity = '1.00'.
* gv_appln_msg-msgty = 'W'. " Mandatory field missing
* gv_appln_msg-msgid = gc_message_class .
* gv_appln_msg-msgno = '057'.
* gv_appln_msg-msgv3 = wa_vbrp-posnr.
* gv_appln_msg-msgv2 = wa_vbrp-vbeln.
* gv_appln_msg-msgv1 = wa_vbrp-matnr.
* gv_appln_msg-probclass = '3'.
*
* CALL METHOD cl_generic_obj->gen_appl_log
* EXPORTING
* iv_appl_msg = gv_appln_msg
* iv_land = gv_bukrs_land
* iv_display = '-'
* iv_handle = gv_appln_handle.
*
* CLEAR : gv_appln_msg.
* ENDIF.
IF wa_vbrp-fkimg GT 0.
lv_unitprice = wa_vbrp-netwr / wa_vbrp-fkimg .
lv_unitprice = abs( lv_unitprice ).
MOVE lv_unitprice TO wa_inv_lnes-unitprice.
ELSE.
MOVE wa_vbrp-netwr TO wa_inv_lnes-unitprice.
wa_inv_lnes-unitprice = abs( wa_inv_lnes-unitprice ).
ENDIF.
*Line Document Description
MOVE wa_vbrp-arktx TO wa_inv_lnes-description_line.
IF wa_vbrp-arktx IS INITIAL .
gv_appln_msg-msgty = 'W'. " Mandatory field missing
gv_appln_msg-msgid = gc_message_class .
gv_appln_msg-msgno = '056'.
gv_appln_msg-msgv2 = wa_vbrp-posnr.
gv_appln_msg-msgv1 = wa_vbrp-vbeln.
gv_appln_msg-probclass = '3'.

CALL METHOD cl_generic_obj->gen_appl_log


EXPORTING
iv_appl_msg = gv_appln_msg
iv_land = gv_bukrs_land
iv_display = '-'
iv_handle = gv_appln_handle.

CLEAR : gv_appln_msg.
ENDIF.
*Plant information
MOVE wa_vbrp-werks TO wa_inv_lnes-plant.
* perform get_plant_address.

*Tax code and settlement amount information


PERFORM map_discount_info.

IF gv_usr_exit_impl GT 0.
CALL BADI gv_usr_exit_badi->identify_discount_amt
EXPORTING
iv_invoice_num = wa_inv_vbrk-vbeln
iv_posnr = wa_vbrp-posnr
iv_inv_date = wa_inv_vbrk-fkdat
iv_bukrs = wa_inv_vbrk-bukrs
iv_fkart = wa_inv_vbrk-fkart
iv_customer = wa_inv_vbrk-kunag
iv_vkorg = wa_inv_vbrk-vkorg
iv_knumv = wa_inv_vbrk-knumv
CHANGING
cv_kschl = wa_inv_lnes-kschl
cv_discount_amt = wa_inv_lnes-discount_amt.
ENDIF.

**Tax information
PERFORM get_tax_info.

*Debit/Credit amounts and exchange rate information for invoices posted other than
EUR
PERFORM fill_inv_lne_amounts.

*Get Settlement/Discount related info


PERFORM get_settlement_info.
MOVE gv_uuid TO wa_inv_lnes-ext_uuid.

CLEAR: gs_doc_flow, gt_doc_flow.


CALL FUNCTION 'SD_DOCUMENT_FLOW_GET'
EXPORTING
iv_docnum = wa_vbrp-vbeln
iv_itemnum = wa_vbrp-posnr
* IV_ALL_ITEMS =
* IV_SELF_IF_EMPTY = ' '
IMPORTING
et_docflow = gt_doc_flow
EXCEPTIONS
error_message = 4
OTHERS = 5.
IF sy-subrc <> 0.

ENDIF.
IF gv_bukrs_land = 'PT'.
IF wa_inv_vbrk-vbtyp = 'O' OR wa_inv_vbrk-vbtyp = 'P'.
READ TABLE gt_doc_flow INTO gs_doc_flow WITH KEY vbtyp_n = 'M'.
IF sy-subrc = 0.
wa_inv_lnes-cred_note_ref = gs_doc_flow-vbeln.
SELECT SINGLE augru_auft FROM vbrp INTO lv_augru WHERE vbeln =
wa_vbrp-vbeln AND posnr = wa_vbrp-posnr.
IF sy-subrc = 0.
SELECT SINGLE bezei FROM tvaut INTO wa_inv_lnes-cred_note_rea WHERE
augru = lv_augru AND spras = 'PT'.
IF sy-subrc <> 0.
SELECT SINGLE bezei FROM tvaut INTO wa_inv_lnes-cred_note_rea
WHERE augru = lv_augru AND spras = 'EN'.
IF sy-subrc <> 0.
SELECT SINGLE bezei FROM tvaut INTO wa_inv_lnes-cred_note_rea
WHERE augru = lv_augru AND spras = sy-langu.
ENDIF.
ENDIF.
ENDIF.
ENDIF.
ENDIF.
ELSEIF gv_bukrs_land = 'LU'.
IF wa_inv_vbrk-vbtyp = 'O' OR wa_inv_vbrk-vbtyp = 'P'.
READ TABLE gt_doc_flow INTO gs_doc_flow WITH KEY vbtyp_n = 'M'.
IF sy-subrc = 0.
wa_inv_lnes-cred_note_ref = gs_doc_flow-vbeln.
ENDIF.
READ TABLE gt_doc_flow INTO gs_doc_flow WITH KEY vbtyp_n = 'K'.
IF sy-subrc = 0.
SELECT SINGLE augru FROM vbak INTO lv_augru WHERE vbeln =
gs_doc_flow-docnum.
IF sy-subrc = 0.
SELECT SINGLE bezei FROM tvaut INTO wa_inv_lnes-cred_note_rea WHERE
augru = lv_augru AND spras = gv_langs.
IF sy-subrc <> 0.
SELECT SINGLE bezei FROM tvaut INTO wa_inv_lnes-cred_note_rea
WHERE augru = lv_augru AND spras = 'EN'.
IF sy-subrc <> 0.
SELECT SINGLE bezei FROM tvaut INTO wa_inv_lnes-cred_note_rea
WHERE augru = lv_augru AND spras = sy-langu.
ENDIF.
ENDIF.
ENDIF.
ENDIF.
ENDIF.
ENDIF.

APPEND wa_inv_lnes TO it_inv_lnes.


CLEAR :lv_unitprice.

ENDLOOP.
CLEAR wa_inv_lnes.
CLEAR wa_vbrp.

IF ls_bcv-tax_line_mat_ind = 'X'.
PERFORM tax_like_material.
ENDIF.

CLEAR it_vbrp[].
CLEAR it_so_data[].
CLEAR wa_so_data.
ELSE.
DELETE it_inv_vbrk WHERE vbeln = wa_inv_vbrk-vbeln.
gv_invoice_no = gv_invoice_no - 1.
* CLEAR : wa_inv_lnes.
ENDIF.
IF lv_doc_tax_new IS NOT INITIAL.
READ TABLE it_inv_vbrk INTO wa_inv_vbrk WITH KEY vbeln = wa_inv_vbrk-vbeln.
IF sy-subrc = 0.
MOVE lv_doc_tax_new TO wa_inv_vbrk-mwsbk.
MODIFY it_inv_vbrk FROM wa_inv_vbrk TRANSPORTING mwsbk.
ENDIF.
ENDIF.
CLEAR : lv_doc_tax_new, wa_inv_vbrk.
CLEAR : it_so_data[]. " 17_f
CLEAR : it_so_data. "17_f
CLEAR : it_inv_pay_mech[]. " 17_f
CLEAR : wa_inv_pay_mech. " 17_f
ENDLOOP.
* CLEAR it_inv_vbrk[].
SORT it_inv_lnes BY bukrs invoice_no posnr.
ENDFORM. " GET_LINE_ITEM_INFO
*&---------------------------------------------------------------------*
*& Form GET_MATERIAL_INFO
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
FORM get_material_info .
DATA : lv_sylangu TYPE sy-langu.

SORT it_material BY matnr maktx ASCENDING .

*Product Code
MOVE wa_vbrp-matnr TO wa_inv_lnes-productcode.

* Product Description
READ TABLE it_material INTO wa_material
WITH KEY matnr = wa_vbrp-matnr
BINARY SEARCH.

IF sy-subrc = 0.
MOVE wa_material-maktx TO wa_inv_lnes-productdesc.
ELSE.
lv_sylangu = sy-langu.
CLEAR : it_material_tmp[].
CLEAR : wa_material_tmp.
SELECT matnr
maktx
spras
FROM makt
INTO CORRESPONDING FIELDS OF TABLE it_material_tmp
WHERE matnr = wa_vbrp-matnr
AND spras IN ('EN','DE', gv_bukrs_land, lv_sylangu).

SORT it_material_tmp BY matnr spras ASCENDING.


* Pref 1: Fill with description in extraction language , if available
IF sy-subrc = 0.
READ TABLE it_material_tmp INTO wa_material_tmp
WITH KEY matnr = wa_vbrp-matnr
spras = gv_langs
BINARY SEARCH.
IF sy-subrc = 0.
MOVE wa_material_tmp-maktx TO wa_inv_lnes-productdesc.
MOVE wa_material_tmp-maktx TO wa_material-maktx .
APPEND wa_material TO it_material.
CLEAR wa_material.
CLEAR it_material[].
ELSE.

* Pref 2: Fill with description in EN , if available


READ TABLE it_material_tmp INTO wa_material_tmp
WITH KEY matnr = wa_vbrp-matnr
spras = 'EN' BINARY SEARCH.
IF sy-subrc = 0.
MOVE wa_material_tmp-maktx TO wa_inv_lnes-productdesc.
MOVE wa_material_tmp-maktx TO wa_material-maktx .
APPEND wa_material TO it_material.
CLEAR wa_material.
CLEAR it_material[].
ELSE.

*Pref 3: Fill with description in DE , if available


READ TABLE it_material_tmp
INTO wa_material_tmp
WITH KEY matnr = wa_vbrp-matnr
spras = 'DE' BINARY SEARCH.
IF sy-subrc = 0.
MOVE wa_material_tmp-maktx TO wa_inv_lnes-productdesc.
MOVE wa_material_tmp-maktx TO wa_material-maktx .
APPEND wa_material TO it_material.
CLEAR wa_material.
CLEAR it_material[].
ELSE.

*Pref 4: Fill with description in Logon language if not in extraction


language,'EN', 'DE' , if available
IF lv_sylangu <> gv_bukrs_land OR
lv_sylangu <> 'EN' OR
lv_sylangu <> 'DE'.

READ TABLE it_material_tmp


INTO wa_material_tmp
WITH KEY matnr = wa_vbrp-matnr
spras = lv_sylangu BINARY SEARCH.
IF sy-subrc = 0.
MOVE wa_material_tmp-maktx TO wa_inv_lnes-productdesc.
MOVE wa_material_tmp-maktx TO wa_material-maktx .
APPEND wa_material TO it_material.
CLEAR wa_material.
CLEAR it_material[].
ELSE.
gv_appln_msg-msgty = 'W'. " Mandatory field missing
gv_appln_msg-msgid = gc_message_class .
gv_appln_msg-msgno = '051'.
gv_appln_msg-msgv1 = wa_vbrp-matnr.
gv_appln_msg-msgv2 = wa_vbrp-vbeln.

CALL METHOD cl_generic_obj->gen_appl_log


EXPORTING
iv_appl_msg = gv_appln_msg
iv_land = gv_bukrs_land
iv_display = '-'
iv_handle = gv_appln_handle.

CLEAR : gv_appln_msg.

ENDIF.
ENDIF.
ENDIF.
ENDIF.
ENDIF.
ENDIF.
ENDIF.

ENDFORM. " GET_MATERIAL_INFO


*&---------------------------------------------------------------------*
*& Form GET_TAX_INFO
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
FORM get_tax_info .
DATA : lv_no_of_implementation TYPE i.
DATA : lv_land TYPE string.
DATA : lv_badi_updt_sd_inv TYPE REF TO fieusaft_update_sdinvoices.
lv_land = gv_bukrs_land.
TRY.
GET BADI lv_badi_updt_sd_inv
FILTERS
land1 = lv_land.
CATCH cx_badi_unknown_error.
CATCH cx_badi_not_implemented.

ENDTRY.
IF lv_badi_updt_sd_inv IS NOT INITIAL.
TRY.
CALL METHOD cl_badi_query=>number_of_implementations
EXPORTING
badi = lv_badi_updt_sd_inv
RECEIVING
num = lv_no_of_implementation.
CATCH cx_badi_not_implemented.
ENDTRY.
ENDIF.

CLEAR wa_tax_data.
CLEAR wa_inv_cond_rec.
READ TABLE it_inv_vbrk INTO wa_inv_vbrk WITH KEY vbeln = wa_vbrp-vbeln.
CHECK sy-subrc = 0.

SELECT SINGLE mwsk1 "kbetr


FROM konv
INTO wa_inv_cond_rec-mwsk1 " ,wa_inv_lnes-taxpercent)
WHERE knumv = wa_inv_vbrk-knumv
AND kposn = wa_vbrp-posnr
AND kappl = 'V'
AND koaid = 'D'
AND kstat = ' '.

IF ( sy-subrc <> 0 OR wa_inv_cond_rec-mwsk1 IS INITIAL ).

IF lv_no_of_implementation GT 0.

CALL BADI lv_badi_updt_sd_inv->populate_tax_info_ext_taxengn


EXPORTING
iv_land1 = gv_bukrs_land
iv_bukrs = p_bukrs
iv_vbeln = wa_vbrp-vbeln
iv_fkdat = wa_inv_vbrk-fkdat
iv_posnr = wa_vbrp-posnr
CHANGING
ev_taxcode = wa_inv_lnes-taxcode
ev_taxamount = wa_inv_lnes-taxamount
ev_taxpercent = wa_inv_lnes-taxpercent
EXCEPTIONS
OTHERS = 1.
IF sy-subrc <> 0.
* Implement suitable error handling here
ENDIF.
ENDIF.
CLEAR lv_no_of_implementation.
IF wa_inv_lnes-taxcode IS INITIAL.
MOVE 'NS' TO wa_inv_lnes-taxcode.
MOVE '0.00' TO wa_inv_lnes-taxamount.
MOVE '0.00' TO wa_inv_lnes-taxpercent.
ENDIF.
gv_appln_msg-msgty = 'W'. " Mandatory field missing
gv_appln_msg-msgid = gc_message_class .
gv_appln_msg-msgno = '052'.
gv_appln_msg-msgv2 = wa_vbrp-posnr.
gv_appln_msg-msgv1 = wa_vbrp-vbeln.

CALL METHOD cl_generic_obj->gen_appl_log


EXPORTING
iv_appl_msg = gv_appln_msg
iv_land = gv_bukrs_land
iv_display = '-'
iv_handle = gv_appln_handle.

CLEAR : gv_appln_msg.

ELSE.
PERFORM map_tax_info_inv_lines USING wa_inv_cond_rec-mwsk1.
ENDIF.

ENDFORM. " GET_TAX_INFO


*&---------------------------------------------------------------------*
*& Form FILL_INV_LNE_AMOUNTS
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
FORM fill_inv_lne_amounts .

DATA : lv_inv_line_amt TYPE netwr.


CLEAR : wa_inv_vbrk-kursf.
MOVE wa_inv_vbrk-kurrf TO wa_inv_vbrk-kursf.
PERFORM set_invoice_status."6_f
CLEAR: wa_inv_lnes-curr_credit_amt, wa_inv_lnes-currr_debit_amt,
wa_inv_lnes-debit_amount,wa_inv_lnes-credit_amount.
CASE wa_inv_vbrk-vbtyp.
WHEN 'N' OR 'O' OR 'L' OR 'T' OR 'H' OR '6'.
IF wa_inv_vbrk-waerk = gv_t001_waers."4_F
IF wa_vbrp-netwr LT 0.
MOVE wa_vbrp-netwr TO wa_inv_lnes-credit_amount.
ELSE.
MOVE wa_vbrp-netwr TO wa_inv_lnes-debit_amount.
ENDIF.

IF gv_usr_exit_impl GT 0.
CALL BADI gv_usr_exit_badi->set_amounts_credit_debit
EXPORTING
iv_invoice_num = wa_inv_vbrk-vbeln
iv_inv_date = wa_inv_vbrk-fkdat
iv_inv_pos = wa_vbrp-posnr
iv_inv_amt = wa_vbrp-netwr
iv_inv_currency = wa_inv_vbrk-waerk
iv_inv_status = wa_inv_hd-inv_status
CHANGING
cv_inv_debit_amt = wa_inv_lnes-debit_amount
cv_inv_credit_amt = wa_inv_lnes-credit_amount
cv_unit_price = wa_inv_lnes-unitprice.
ENDIF.
*User exit invocation ends

IF wa_inv_hd-inv_status <> 'A' AND wa_inv_hd-inv_status <> 'F'.


IF NOT wa_inv_lnes-debit_amount IS INITIAL.
gv_total_debit_sd = gv_total_debit_sd + wa_inv_lnes-debit_amount.
ELSEIF NOT wa_inv_lnes-credit_amount IS INITIAL.
gv_total_credit_sd = gv_total_credit_sd + wa_inv_lnes-credit_amount.
ENDIF.
ENDIF.

MOVE wa_inv_vbrk-waerk TO wa_inv_lnes-currency.


MOVE wa_inv_vbrk-waerk TO wa_inv_lnes-document_waers.
ELSE.
* invoice in non EURO currency
IF wa_vbrp-netwr GE 0.
MOVE wa_vbrp-netwr TO wa_inv_lnes-currr_debit_amt.
CALL FUNCTION 'CONVERT_TO_LOCAL_CURRENCY'
EXPORTING
date = wa_inv_vbrk-fkdat
foreign_amount = wa_vbrp-netwr
foreign_currency = wa_inv_vbrk-waerk
local_currency = gv_t001_waers
rate = wa_inv_vbrk-kursf
type_of_rate = 'M'
read_tcurr = ' '
IMPORTING
local_amount = wa_inv_lnes-debit_amount.
ELSE.
MOVE wa_vbrp-netwr TO wa_inv_lnes-curr_credit_amt.
CALL FUNCTION 'CONVERT_TO_LOCAL_CURRENCY'
EXPORTING
date = wa_inv_vbrk-fkdat
foreign_amount = wa_vbrp-netwr
foreign_currency = wa_inv_vbrk-waerk
local_currency = gv_t001_waers
rate = wa_inv_vbrk-kursf
type_of_rate = 'M'
read_tcurr = ' '
IMPORTING
local_amount = wa_inv_lnes-credit_amount.
ENDIF.

wa_inv_lnes-exchangerate = wa_inv_vbrk-kursf.
wa_inv_lnes-currency = wa_inv_vbrk-waerk.
wa_inv_lnes-document_waers = gv_t001_waers.

*User specific enhancments for the adjustment of tax payable and credit amounts
IF gv_usr_exit_impl GT 0.
CLEAR : lv_inv_line_amt.
MOVE wa_inv_lnes-debit_amount TO lv_inv_line_amt.

CALL BADI gv_usr_exit_badi->set_amounts_credit_debit


EXPORTING
iv_invoice_num = wa_inv_vbrk-vbeln
iv_inv_date = wa_inv_vbrk-fkdat
iv_inv_pos = wa_vbrp-posnr
iv_inv_amt = lv_inv_line_amt
iv_inv_currency = wa_inv_vbrk-waerk
iv_inv_status = wa_inv_hd-inv_status
CHANGING
cv_inv_debit_amt = wa_inv_lnes-debit_amount
cv_inv_credit_amt = wa_inv_lnes-credit_amount
cv_curr_debti_amt = wa_inv_lnes-currr_debit_amt
cv_curr_credit_amt = wa_inv_lnes-curr_credit_amt
cv_unit_price = wa_inv_lnes-unitprice.
ENDIF.

*User exit invocation ends

IF wa_inv_hd-inv_status <> 'A' AND wa_inv_hd-inv_status <> 'F'.


IF NOT wa_inv_lnes-debit_amount IS INITIAL.
gv_total_debit_sd = gv_total_debit_sd + wa_inv_lnes-debit_amount.
ELSEIF NOT wa_inv_lnes-credit_amount IS INITIAL.
gv_total_credit_sd = gv_total_credit_sd + wa_inv_lnes-credit_amount.
ENDIF.
ENDIF.
ENDIF.

WHEN 'S'.
IF wa_inv_vbrk-waerk = gv_t001_waers."4_F
MOVE wa_vbrp-netwr TO wa_inv_lnes-credit_amount.
CLEAR : lv_inv_line_amt.
MOVE wa_inv_lnes-credit_amount TO lv_inv_line_amt.

IF gv_usr_exit_impl GT 0.
CALL BADI gv_usr_exit_badi->set_amounts_credit_debit
EXPORTING
iv_invoice_num = wa_inv_vbrk-vbeln
iv_inv_date = wa_inv_vbrk-fkdat
iv_inv_pos = wa_vbrp-posnr
iv_inv_amt = lv_inv_line_amt
iv_inv_currency = wa_inv_vbrk-waerk
iv_inv_status = wa_inv_hd-inv_status
CHANGING
cv_inv_debit_amt = wa_inv_lnes-debit_amount
cv_inv_credit_amt = wa_inv_lnes-credit_amount
cv_curr_debti_amt = wa_inv_lnes-currr_debit_amt
cv_curr_credit_amt = wa_inv_lnes-curr_credit_amt
cv_unit_price = wa_inv_lnes-unitprice.
ENDIF.
*User exit invocation ends

IF wa_inv_hd-inv_status <> 'A' AND wa_inv_hd-inv_status <> 'F'.


gv_total_credit_sd = gv_total_credit_sd + wa_inv_lnes-credit_amount.
ENDIF.
MOVE wa_inv_vbrk-waerk TO wa_inv_lnes-currency.
MOVE wa_inv_vbrk-waerk TO wa_inv_lnes-document_waers.
ELSE.
MOVE wa_vbrp-netwr TO wa_inv_lnes-curr_credit_amt.
CALL FUNCTION 'CONVERT_TO_LOCAL_CURRENCY'
EXPORTING
date = wa_inv_vbrk-fkdat
foreign_amount = wa_vbrp-netwr
foreign_currency = wa_inv_vbrk-waerk
local_currency = gv_t001_waers
rate = wa_inv_vbrk-kursf
type_of_rate = 'M'
read_tcurr = ' '
IMPORTING
local_amount = wa_inv_lnes-curr_credit_amt.

wa_inv_lnes-exchangerate = wa_inv_vbrk-kursf.
wa_inv_lnes-currency = wa_inv_vbrk-waerk.
wa_inv_lnes-document_waers = gv_t001_waers.

IF gv_usr_exit_impl GT 0.
CLEAR : lv_inv_line_amt.
MOVE wa_inv_lnes-debit_amount TO lv_inv_line_amt.
CALL BADI gv_usr_exit_badi->set_amounts_credit_debit
EXPORTING
iv_invoice_num = wa_inv_vbrk-vbeln
iv_inv_date = wa_inv_vbrk-fkdat
iv_inv_pos = wa_vbrp-posnr
iv_inv_amt = lv_inv_line_amt
iv_inv_currency = wa_inv_vbrk-waerk
iv_inv_status = wa_inv_hd-inv_status
CHANGING
cv_inv_debit_amt = wa_inv_lnes-debit_amount
cv_inv_credit_amt = wa_inv_lnes-credit_amount
cv_curr_debti_amt = wa_inv_lnes-currr_debit_amt
cv_curr_credit_amt = wa_inv_lnes-curr_credit_amt
cv_unit_price = wa_inv_lnes-unitprice.
ENDIF.
*User exit invocation ends

IF wa_inv_hd-inv_status <> 'A' AND wa_inv_hd-inv_status <> 'F'.


gv_total_credit_sd = gv_total_credit_sd + wa_inv_lnes-credit_amount.
ENDIF.
ENDIF.

WHEN OTHERS.

* in case of document being in non EURO currency

IF wa_inv_vbrk-waerk <> gv_t001_waers." 4_F

IF wa_vbrp-netwr LT 0.
MOVE wa_vbrp-netwr TO wa_inv_lnes-currr_debit_amt.
ELSE.
MOVE wa_vbrp-netwr TO wa_inv_lnes-curr_credit_amt.
ENDIF.

CALL FUNCTION 'CONVERT_TO_LOCAL_CURRENCY'


EXPORTING
date = wa_inv_vbrk-fkdat
foreign_amount = wa_vbrp-netwr
foreign_currency = wa_inv_vbrk-waerk
local_currency = gv_t001_waers
rate = wa_inv_vbrk-kursf
type_of_rate = 'M'
read_tcurr = ' '
IMPORTING
local_amount = wa_vbrp-netwr.

wa_inv_lnes-exchangerate = wa_inv_vbrk-kursf.
wa_inv_lnes-currency = wa_inv_vbrk-waerk.
wa_inv_lnes-document_waers = gv_t001_waers.
* map amounts as per the requirement
IF wa_vbrp-netwr LT 0.
MOVE wa_vbrp-netwr TO wa_inv_lnes-debit_amount.
ELSE.
MOVE wa_vbrp-netwr TO wa_inv_lnes-credit_amount.
ENDIF.

IF gv_usr_exit_impl GT 0.
CLEAR : lv_inv_line_amt.
MOVE wa_inv_lnes-debit_amount TO lv_inv_line_amt.

CALL BADI gv_usr_exit_badi->set_amounts_credit_debit


EXPORTING
iv_invoice_num = wa_inv_vbrk-vbeln
iv_inv_date = wa_inv_vbrk-fkdat
iv_inv_pos = wa_vbrp-posnr
iv_inv_amt = lv_inv_line_amt
iv_inv_currency = wa_inv_vbrk-waerk
iv_inv_status = wa_inv_hd-inv_status
CHANGING
cv_inv_debit_amt = wa_inv_lnes-debit_amount
cv_inv_credit_amt = wa_inv_lnes-credit_amount
cv_curr_debti_amt = wa_inv_lnes-currr_debit_amt
cv_curr_credit_amt = wa_inv_lnes-curr_credit_amt
cv_unit_price = wa_inv_lnes-unitprice.
ENDIF.
*User exit invocation ends

IF wa_inv_hd-inv_status <> 'A' AND wa_inv_hd-inv_status <> 'F'.


IF wa_inv_lnes-credit_amount IS NOT INITIAL OR wa_inv_lnes-credit_amount
<> '0.00'.
wa_inv_lnes-credit_amount = abs( wa_inv_lnes-credit_amount ).
gv_total_credit_sd = gv_total_credit_sd + wa_inv_lnes-credit_amount.
ELSEIF wa_inv_lnes-debit_amount IS NOT INITIAL OR wa_inv_lnes-
debit_amount <> '0.00'.
wa_inv_lnes-debit_amount = abs( wa_inv_lnes-debit_amount ).
gv_total_debit_sd = gv_total_debit_sd + wa_inv_lnes-debit_amount.
ENDIF.
ENDIF.
ENDIF.

IF wa_inv_vbrk-waerk = gv_t001_waers."4_F
IF wa_vbrp-netwr LT 0.
MOVE wa_vbrp-netwr TO wa_inv_lnes-debit_amount.
ELSE.
MOVE wa_vbrp-netwr TO wa_inv_lnes-credit_amount.
ENDIF.

IF gv_usr_exit_impl GT 0.

CLEAR : lv_inv_line_amt.
MOVE wa_inv_lnes-debit_amount TO lv_inv_line_amt.
CALL BADI gv_usr_exit_badi->set_amounts_credit_debit
EXPORTING
iv_invoice_num = wa_inv_vbrk-vbeln
iv_inv_date = wa_inv_vbrk-fkdat
iv_inv_pos = wa_vbrp-posnr
iv_inv_amt = lv_inv_line_amt
iv_inv_currency = wa_inv_vbrk-waerk
iv_inv_status = wa_inv_hd-inv_status
CHANGING
cv_inv_debit_amt = wa_inv_lnes-debit_amount
cv_inv_credit_amt = wa_inv_lnes-credit_amount
cv_curr_debti_amt = wa_inv_lnes-currr_debit_amt
cv_curr_credit_amt = wa_inv_lnes-curr_credit_amt
cv_unit_price = wa_inv_lnes-unitprice.
ENDIF.
*User exit invocation ends

MOVE wa_inv_vbrk-waerk TO wa_inv_lnes-currency.


MOVE wa_inv_vbrk-waerk TO wa_inv_lnes-document_waers.

IF wa_inv_hd-inv_status <> 'A' AND wa_inv_hd-inv_status <> 'F'.


IF wa_inv_lnes-credit_amount IS NOT INITIAL OR wa_inv_lnes-credit_amount
<> '0.00'.
wa_inv_lnes-credit_amount = abs( wa_inv_lnes-credit_amount ).
gv_total_credit_sd = gv_total_credit_sd + wa_inv_lnes-credit_amount.
ELSEIF wa_inv_lnes-debit_amount IS NOT INITIAL OR wa_inv_lnes-
debit_amount <> '0.00'.
wa_inv_lnes-debit_amount = abs( wa_inv_lnes-debit_amount ).
gv_total_debit_sd = gv_total_debit_sd + wa_inv_lnes-debit_amount.
ENDIF.
ENDIF.
ENDIF.
ENDCASE.
ENDFORM. " FILL_INV_LNE_AMOUNTS
*&---------------------------------------------------------------------*
*& Form GET_SETTLEMENT_INFO
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
FORM get_settlement_info .
DATA : lt_top_text TYPE TABLE OF vtopis.
DATA : wa_top_text LIKE LINE OF lt_top_text .
CLEAR : wa_inv_pay_mech.
DATA : ls_vbkd LIKE LINE OF gt_vbkd.

LOOP AT gt_vbkd INTO ls_vbkd.


wa_inv_pay_mech-vbeln = ls_vbkd-vbeln.
wa_inv_pay_mech-zterm = ls_vbkd-zterm.
APPEND wa_inv_pay_mech TO it_inv_pay_mech .
ENDLOOP.

READ TABLE it_inv_pay_mech INTO wa_inv_pay_mech


WITH KEY vbeln = wa_inv_hd-invoice_no.
* CALL FUNCTION 'SD_PRINT_TERMS_OF_PAYMENT'
* EXPORTING
* bldat = wa_vbrp-erdat
* budat = wa_inv_vbrk-fkdat
* cpudt = wa_vbrp-erdat
* terms_of_payment = wa_inv_vbrk-zterm
* TABLES
* top_text = lt_top_text
* EXCEPTIONS
* terms_of_payment_not_in_t052 = 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.
* SELECT SINGLE text1 FROM t052u INTO wa_inv_lnes-setlmnt_agrmnt
* WHERE spras = sy-langu
* AND zterm = wa_inv_vbrk-zterm.
* READ TABLE lt_top_text INTO wa_top_text INDEX 1.
* IF sy-subrc <> 0.
* MOVE wa_top_text-line TO wa_inv_lnes-paymentterm.
* ENDIF.
* IF wa_inv_lnes-paymentterm IS INITIAL.
* gv_appln_msg-msgty = 'W'. " Mandatory field missing
* gv_appln_msg-msgid = gc_message_class .
* gv_appln_msg-msgno = '054'.
* gv_appln_msg-msgv1 = wa_inv_vbrk-zterm.
*
* CALL METHOD cl_generic_obj->gen_appl_log
* EXPORTING
* iv_appl_msg = gv_appln_msg
* iv_land = gv_bukrs_land
* iv_display = '-'
* iv_handle = gv_appln_handle.
*
* CLEAR : gv_appln_msg.
*
* ENDIF.
ENDFORM. " GET_SETTLEMENT_INFO
*&---------------------------------------------------------------------*
*& Form ASSIGN_DATA_FOR_TBL_UPDATE
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
FORM assign_data_for_tbl_update .
DATA : wa_inv_vbrk_cancel LIKE LINE OF it_inv_vbrk . " to identify the source
idea of the original doc if current inv is a cancellation document
DATA : ls_cancel_source_id LIKE vbrk-ernam.
DATA : wa_inv_vbrk_tmp LIKE LINE OF it_inv_vbrk.
DATA : ls_inv_ctime TYPE vbrk-erzet.
DATA : ls_inv_cdate TYPE vbrk-erdat .
DATA : ls_inv_cname TYPE vbrk-ernam .
DATA : lv_del_mvesdate TYPE likp-lfdat.
DATA : lv_del_mvestme TYPE likp-lfuhr .
DATA : lv_del_mveedate TYPE likp-wadat.
DATA : lv_del_mvedtime TYPE likp-wauhr.
DATA : lv_stceg TYPE kna1-stceg.
DATA : lv_stcd1 TYPE kna1-stcd1.
DATA : lv_xcpdk TYPE kna1-xcpdk.
DATA : ls_invstatdate TYPE fieud_sdinv_h-inv_status_date.
DATA : lv_vbeln TYPE vbrk-vbeln.
DATA : lv_likp_erdat TYPE likp-erdat.
DATA : wa_zterm_selection TYPE t052,
lt_zterm_texts LIKE TABLE OF ttext.
DATA : lv_doc_date TYPE d.
DATA : lv_zterm TYPE dzterm.
DATA : lv_ttext TYPE ttext.
DATA : lv_sy_lang TYPE spras.

CLEAR: lv_vbeln, gs_doc_flow, gt_doc_flow.


LOOP AT it_inv_vbrk INTO wa_inv_vbrk.
CLEAR gv_ext_sys_ind_sd. " 20_F " correction of variables not cleared
CLEAR wa_inv_hd. " 20_F " correction of variables not cleared
IF p_archvl = 'X'.
MOVE 'X' TO wa_inv_hd-inv_archive.
ENDIF.
MOVE sy-mandt TO wa_inv_hd-mandt.
MOVE wa_inv_vbrk-bukrs TO wa_inv_hd-bukrs.
MOVE wa_inv_vbrk-fkdat TO wa_inv_hd-inv_date .
MOVE wa_inv_vbrk-vbeln TO wa_inv_hd-invoice_no.
MOVE wa_inv_vbrk-vkbur TO wa_inv_hd-sales_office. "20_f
MOVE wa_inv_vbrk-transid TO wa_inv_hd-transactionid." SEPARATED BY ''.
*---------------------------------*User Exit
invocation*----------------------------*
* Update transaction id for rebate documents
IF gv_usr_exit_impl GT 0.
CALL BADI gv_usr_exit_badi->update_transid_rebate
EXPORTING
iv_bukrs = wa_inv_hd-bukrs
iv_land = gv_bukrs_land
iv_vbeln = wa_inv_vbrk-vbeln
iv_vbtyp = wa_inv_vbrk-vbtyp
iv_fkdat = wa_inv_vbrk-fkdat
CHANGING
cs_transaid = wa_inv_hd-transactionid.

CLEAR gv_ext_sys_ind_sd.
IF ls_bcv-ext_sd_ind EQ 'X'.
CALL BADI gv_usr_exit_badi->identify_ext_invoices_sd
EXPORTING
iv_invoice_num = wa_inv_vbrk-vbeln
iv_inv_date = wa_inv_vbrk-fkdat
iv_bukrs = wa_inv_vbrk-bukrs
iv_fkart = wa_inv_vbrk-fkart
iv_customer = wa_inv_vbrk-kunag
iv_vkorg = wa_inv_vbrk-vkorg
CHANGING
ev_ext_ind = gv_ext_sys_ind_sd.
ENDIF.
ENDIF.

* "Set Source Billing Value as I for External System.


IF NOT gv_ext_sys_ind_sd IS INITIAL .
wa_inv_hd-sourcebilling = 'I'.
ENDIF.
*User exit invocation ends
*---------------------------------------"22092014----------------------------------
------------*
IF p_archvl = 'X'. "22092014
MOVE 'SA' TO wa_inv_hd-invoice_source. " Invoice is from SD
module .
ELSE.
MOVE 'SD' TO wa_inv_hd-invoice_source. " Invoice is from SD
module .
ENDIF.
*---------------------------------------"22092014----------------------------------
------------*
MOVE '0' TO wa_inv_hd-ind_selfbillng.
MOVE wa_inv_vbrk-vkorg TO wa_inv_hd-sales_org.
MOVE wa_inv_vbrk-vtweg TO wa_inv_hd-division.
SELECT SINGLE vkbur
FROM tvkbz
INTO wa_inv_hd-sales_office
WHERE vkorg = wa_inv_hd-sales_office
AND vtweg = wa_inv_hd-division.

* CHECK wa_inv_hd IS NOT INITIAL. " 20_f

CALL FUNCTION 'DETERMINE_PERIOD'


EXPORTING
date = wa_inv_vbrk-fkdat
* PERIOD_IN = '000'
version = gv_t001_periv
IMPORTING
period = wa_inv_hd-inv_period
"year = wa_inv_hd-inv_year
EXCEPTIONS
period_in_not_valid = 1
period_not_assigned = 2
version_undefined = 3
OTHERS = 4.
IF sy-subrc <> 0.
* Implement suitable error handling here
ENDIF.
MOVE p_gjahr TO wa_inv_hd-inv_year.

IF wa_inv_hd-inv_period IS INITIAL.
MOVE wa_inv_vbrk-monat TO wa_inv_hd-inv_period.
IF wa_inv_hd-inv_period GT 12. " for year end adjustment posting made to
special posting periods.
MOVE '12' TO wa_inv_hd-inv_period.
ENDIF.
ENDIF.
MOVE wa_inv_vbrk-sfakn TO wa_inv_hd-cancel_doc.
CLEAR wa_inv_vbrk_tmp .
*----------------------------------------------------------------------------------
--*
*Invoice Status Date -> invoice date if invoice is not cancelled
* if invoice is cancelled , then invoice status date = invoice date of original
date
*----------------------------------------------------------------------------------
--*
CLEAR : ls_invstatdate.
IF wa_inv_vbrk-sfakn IS NOT INITIAL.
READ TABLE it_inv_vbrk INTO wa_inv_vbrk_tmp
WITH KEY bukrs = p_bukrs
vbeln = wa_inv_vbrk-sfakn.
.
IF sy-subrc = 0.
CONCATENATE wa_inv_vbrk_tmp-erdat+0(4)'-' wa_inv_vbrk_tmp-erdat+4(2)
'-'wa_inv_vbrk_tmp-erdat+6(2)
'T' wa_inv_vbrk_tmp-erzet+0(2)':'wa_inv_vbrk_tmp-
erzet+2(2)':'wa_inv_vbrk_tmp-erzet+4(2) INTO
ls_invstatdate.
MOVE ls_invstatdate TO wa_inv_hd-inv_status_date.
ELSE.
CLEAR wa_inv_hd-inv_status_date .
SELECT SINGLE erdat erzet ernam
FROM vbrk
INTO (ls_inv_cdate, ls_inv_ctime,ls_inv_cname )
WHERE vbeln = wa_inv_vbrk-vbeln.
IF sy-subrc <> 0..
CLEAR wa_inv_hd-inv_status_date .
ELSE.
CLEAR ls_invstatdate.
CONCATENATE ls_inv_cdate+0(4)'-' ls_inv_cdate+4(2) '-'ls_inv_cdate+6(2)
'T' ls_inv_ctime+0(2)':'ls_inv_ctime+2(2)':'ls_inv_ctime+4(2)
INTO
ls_invstatdate.
CLEAR wa_inv_hd-inv_status_date.
MOVE ls_invstatdate TO wa_inv_hd-inv_status_date.
MOVE ls_inv_cname TO wa_inv_hd-source_id.
ENDIF.

ENDIF.
ELSE.
CONCATENATE wa_inv_vbrk-erdat+0(4) '-'
wa_inv_vbrk-erdat+4(2) '-'
wa_inv_vbrk-erdat+6(2)
'T'
wa_inv_vbrk-erzet+0(2) ':'
wa_inv_vbrk-erzet+2(2) ':'
wa_inv_vbrk-erzet+4(2)
INTO
wa_inv_hd-inv_status_date.
ENDIF.

IF wa_inv_vbrk-sfakn IS INITIAL.
MOVE wa_inv_vbrk-ernam TO wa_inv_hd-source_id.
ELSE.
READ TABLE it_inv_vbrk INTO wa_inv_vbrk_cancel WITH KEY vbeln =
wa_inv_vbrk-sfakn.
IF sy-subrc = 0.
MOVE wa_inv_vbrk_cancel-ernam TO wa_inv_hd-inv_stat_user.
ELSE.
CLEAR ls_cancel_source_id .
CLEAR wa_inv_hd-source_id.
SELECT SINGLE ernam FROM vbrk INTO ls_cancel_source_id WHERE vbeln =
wa_inv_vbrk-sfakn.
IF sy-subrc = 0.
MOVE ls_cancel_source_id TO wa_inv_hd-inv_stat_user.
ENDIF.

ENDIF.
ENDIF.

MOVE wa_inv_vbrk-waerk TO wa_inv_hd-currency.


MOVE wa_inv_vbrk-netwr TO wa_inv_hd-doc_total_gross.
MOVE wa_inv_vbrk-kurrf TO wa_inv_hd-exchange_rate.
IF wa_inv_vbrk-waerk = gv_t001_waers AND wa_inv_vbrk-sfakn IS INITIAL ."and
wa_inv_vbrk-fksto IS INITIAL4_F
gv_debit_amount = gv_debit_amount + wa_inv_vbrk-netwr.
ELSEIF
wa_inv_vbrk-waerk = gv_t001_waers AND wa_inv_vbrk-sfakn IS NOT INITIAL ."4_F
gv_credit_amount = gv_credit_amount + wa_inv_vbrk-netwr.
ENDIF.

PERFORM set_invoice_status.

CLEAR : lv_xcpdk,lv_stceg,lv_stcd1.
MOVE wa_inv_vbrk-fkart TO wa_inv_hd-inv_doctype.

MOVE wa_inv_vbrk-vbtyp TO wa_inv_hd-inv_category.


MOVE wa_inv_vbrk-kunrg TO wa_inv_hd-customerid.
MOVE wa_inv_vbrk-kunrg TO wa_inv_hd-customer.
MOVE wa_inv_vbrk-stceg TO lv_stceg.

"Get stceg for one time customer.


IF NOT wa_inv_hd-customerid IS INITIAL.
IF lv_stceg IS INITIAL.
SELECT SINGLE xcpdk stceg stcd1 INTO (lv_xcpdk,lv_stceg,lv_stcd1) FROM kna1
WHERE kunnr = wa_inv_hd-customer.
ELSE.
SELECT SINGLE xcpdk stcd1 INTO (lv_xcpdk,lv_stcd1) FROM kna1 WHERE kunnr =
wa_inv_hd-customer.
ENDIF.

IF lv_xcpdk EQ 'X'.
IF lv_stceg IS INITIAL AND lv_stcd1 IS INITIAL.
SELECT SINGLE stcd1 INTO lv_stcd1 FROM bsec WHERE bukrs = wa_inv_vbrk-
bukrs
AND belnr = wa_inv_vbrk-belnr AND gjahr = wa_inv_vbrk-
gjahr.
ENDIF.
IF NOT lv_stceg IS INITIAL.
lv_stceg = lv_stceg+2.
CONCATENATE wa_inv_hd-customerid lv_stceg INTO wa_inv_hd-customerid
SEPARATED BY '/'.
ELSEIF NOT lv_stcd1 IS INITIAL.
CONCATENATE wa_inv_hd-customerid lv_stcd1 INTO wa_inv_hd-customerid
SEPARATED BY '/'.
ELSEIF lv_stceg IS INITIAL AND lv_stcd1 IS INITIAL .
CONCATENATE wa_inv_hd-customerid '999999990' INTO wa_inv_hd-customerid
SEPARATED BY '/'.
ENDIF.
wa_inv_hd-onetime_ind = 'X'.
ELSE.
* If it isn't a onetime customer, we check if the customer has multiple VAT numbers
and add the country data from the invoice if needed
* Preference is given to the VAT country, destiny country used it the first doesn't
exist
SELECT COUNT( * )
FROM knas
WHERE knas~kunnr = wa_inv_hd-customerid.

IF sy-dbcnt > 0.
IF wa_inv_vbrk-stceg_l IS INITIAL.
CONCATENATE wa_inv_hd-customerid '/' wa_inv_vbrk-land1 INTO wa_inv_hd-
customerid.
ELSE.
CONCATENATE wa_inv_hd-customerid '/' wa_inv_vbrk-stceg_l INTO
wa_inv_hd-customerid.
ENDIF.
ENDIF.
ENDIF.
ENDIF.

CONCATENATE wa_inv_vbrk-erdat+0(4) '-'


wa_inv_vbrk-erdat+4(2) '-'
wa_inv_vbrk-erdat+6(2)
'T'
wa_inv_vbrk-erzet+0(2) ':'
wa_inv_vbrk-erzet+2(2) ':'
wa_inv_vbrk-erzet+4(2)
INTO
wa_inv_hd-sys_entry_date.

IF lv_vbeln <> wa_inv_vbrk-vbeln.


CLEAR: gs_doc_flow, gt_doc_flow.
CALL FUNCTION 'SD_DOCUMENT_FLOW_GET'
EXPORTING
iv_docnum = wa_inv_vbrk-vbeln
IMPORTING
et_docflow = gt_doc_flow
EXCEPTIONS
error_message = 4
OTHERS = 5.
IF sy-subrc <> 0. ENDIF.
lv_vbeln = wa_inv_vbrk-vbeln.
ENDIF.
* archival impact - 20_f
IF gt_doc_flow IS INITIAL AND lt_likp_vbrp_link[] IS NOT INITIAL.
READ TABLE lt_likp_vbrp_link INTO ls_likp_vbrp_link INDEX 1.
MOVE ls_likp_vbrp_link-vbeln TO gs_doc_flow-vbelv.
ENDIF.

READ TABLE gt_doc_flow INTO gs_doc_flow WITH KEY vbtyp_n = 'M'


vbtyp_v = 'J'.
IF sy-subrc <> 0.
READ TABLE gt_doc_flow INTO gs_doc_flow WITH KEY vbtyp_n = 'M'
vbtyp_v = 'T'.
IF sy-subrc <> 0.
READ TABLE gt_doc_flow INTO gs_doc_flow WITH KEY vbtyp_n = 'J'
vbtyp_v = 'V'.
IF sy-subrc = 0.
gs_doc_flow-vbelv = gs_doc_flow-vbeln.
ENDIF.
ENDIF.
ENDIF.
IF gs_doc_flow IS NOT INITIAL.
CLEAR :lv_likp_erdat.
SELECT SINGLE fkdat erdat lfdat lfuhr wadat_ist wauhr
INTO (wa_inv_vbrk-del_date,
lv_likp_erdat,
lv_del_mvesdate,
lv_del_mvestme,
lv_del_mveedate,
lv_del_mvedtime)
FROM likp WHERE vbeln = gs_doc_flow-vbelv.
IF sy-subrc = 0.
*Movement Start Time
IF lv_del_mvesdate IS NOT INITIAL.
CONCATENATE lv_del_mvesdate+0(4) '-'lv_del_mvesdate+4(2)'-'
lv_del_mvesdate+6(2) 'T' INTO wa_inv_vbrk-move_sdate.
IF ( lv_del_mvestme IS NOT INITIAL AND lv_del_mvestme+0(2) <> '00').
CONCATENATE wa_inv_vbrk-move_sdate lv_del_mvestme+0(2) ':' INTO
wa_inv_vbrk-move_sdate .
ELSEIF ( lv_del_mvestme+0(2) = '00' OR lv_del_mvestme IS INITIAL ).
CONCATENATE wa_inv_vbrk-move_sdate '00' ':' INTO wa_inv_vbrk-
move_sdate.
ENDIF.
IF ( lv_del_mvestme+2(2) <> '00' OR lv_del_mvestme IS NOT INITIAL ).
CONCATENATE wa_inv_vbrk-move_sdate lv_del_mvestme+2(2) ':' INTO
wa_inv_vbrk-move_sdate.
ELSEIF ( lv_del_mvestme+2(2) = '00' OR lv_del_mvestme IS INITIAL ).
CONCATENATE wa_inv_vbrk-move_sdate '00' ':' INTO wa_inv_vbrk-
move_sdate.
ENDIF.
IF ( lv_del_mvestme+4(2) <> '00' OR lv_del_mvestme IS NOT INITIAL ).
CONCATENATE wa_inv_vbrk-move_sdate lv_del_mvestme+4(2) INTO
wa_inv_vbrk-move_sdate.
ELSEIF ( lv_del_mvestme+4(2) = '00' OR lv_del_mvestme IS INITIAL ).
CONCATENATE wa_inv_vbrk-move_sdate '00' INTO wa_inv_vbrk-move_sdate.
ENDIF.
ENDIF.
*MovementEndTime and delviery data from signature tables moved to BAdI in 19_f
* FOR READIND SIGNED DELIVERY DOCUMENT FOR PT .
IF wa_inv_hd-st_deliveryid IS INITIAL.
MOVE gs_doc_flow-vbelv TO wa_inv_hd-st_deliveryid.
ENDIF.
IF wa_inv_hd-st_deliveryid IS INITIAL.
READ TABLE gt_vbrp INTO gs_vbrp WITH KEY vbeln = wa_inv_vbrk-vbeln.
IF sy-subrc = 0.
MOVE gs_vbrp-vgbel TO wa_inv_hd-st_deliveryid.
ENDIF.
ENDIF.
MOVE wa_inv_vbrk-del_date TO wa_inv_hd-st_deliverydate.
MOVE wa_inv_vbrk-move_sdate TO wa_inv_hd-move_stime.
*-------------------------"22092014-------------------------------------------*
* DELIVERY DOCUMENT ARCHIVED "22092014; DOCUMENT NUMNER AVAILABLE IN VBFA in
database .
ELSE.
IF gs_doc_flow-vbelv IS NOT INITIAL. "22092014
MOVE gs_doc_flow-vbelv TO wa_inv_hd-st_deliveryid.
MOVE gs_doc_flow-vbelv TO wa_likp_vbeln-vbeln.
APPEND wa_likp_vbeln TO it_likp_vbeln.
ENDIF.
*-------------------------"22092014-------------------------------------------*

*23092014------------DELIVERY DOC INFO UNAVAILABLE IN VBFA/LIKP In


database------------
IF gs_doc_flow-vbelv IS INITIAL.
MOVE wa_inv_vbrk-vbeln TO wa_del_arch-vbeln.
MOVE wa_inv_vbrk-vkorg TO wa_del_arch-vkorg.
APPEND wa_del_arch TO it_del_arch.
ENDIF.
*23092014------------DELIVERY DOC INFO UNAVAILABLE IN VBFA/LIKP In
database------------
ENDIF.
ENDIF.
IF wa_inv_hd-st_deliveryid IS INITIAL.
IF it_likp_vbeln_det[] IS NOT INITIAL.
READ TABLE it_likp_vbeln_det INTO wa_likp_vbeln_det INDEX 1.
MOVE wa_likp_vbeln_det-vbeln TO wa_inv_hd-st_deliveryid.
MOVE wa_likp_vbeln_det-fkdat TO wa_inv_hd-st_deliverydate.
ENDIF.
CLEAR : wa_likp_vbeln_det.

ENDIF.
*Country specific enhancements for Delivery document - 19_f
* ENDIF.
IF wa_inv_hd-st_deliveryid IS INITIAL.
gv_appln_msg-msgty = 'W'. " Mandatory field missing
gv_appln_msg-msgid = gc_message_class .
gv_appln_msg-msgno = '047'.
gv_appln_msg-msgv1 = wa_inv_hd-invoice_no..

CALL METHOD cl_generic_obj->gen_appl_log


EXPORTING
iv_appl_msg = gv_appln_msg
iv_land = gv_bukrs_land
iv_display = '-'
iv_handle = gv_appln_handle.

CLEAR : gv_appln_msg.
ENDIF.

*Set invoice type


CASE wa_inv_vbrk-vbtyp.
WHEN 'M' OR 'N'.
wa_inv_hd-inv_type = 'FT'.
WHEN 'O' OR 'S'.
wa_inv_hd-inv_type = 'NC'.
WHEN 'P'.
wa_inv_hd-inv_type = 'ND'.
WHEN OTHERS.

ENDCASE.

"GET DATA from cutomizing table for target invoice type

READ TABLE gt_fieud_sdgen INTO wa_fieud_sdgen WITH KEY


vbtype = wa_inv_vbrk-vbtyp .
IF sy-subrc EQ 0 AND NOT wa_fieud_sdgen-tagt_inv_type IS INITIAL .
wa_inv_hd-inv_type = wa_fieud_sdgen-tagt_inv_type.
ENDIF.

IF gv_usr_exit_impl GT 0.
CALL BADI gv_usr_exit_badi->set_invoice_type
EXPORTING
iv_bukrs = wa_inv_hd-bukrs
iv_land = gv_bukrs_land
iv_vbeln = wa_inv_vbrk-vbeln
iv_vbtyp = wa_inv_vbrk-vbtyp
iv_fkart = wa_inv_vbrk-fkart
CHANGING
cv_inv_type = wa_inv_hd-inv_type.
ENDIF.

gv_entries_count = gv_entries_count + 1.

*Set plant information on header


PERFORM set_ship_from.

*Set invoice status - Normal/ anulado


" PERFORM set_invoice_status. " Commented by 20_F " No longer needed

*Get document totals.


PERFORM get_doc_totals_sd.

"EXTRACTION OF DOCUMENT TOTALS SETTLEMENT INFORMATION


CLEAR wa_zterm_selection.
CLEAR lt_zterm_texts.
* CLEAR lt_bill_inv_settl.
CLEAR lv_sy_lang.

IF wa_inv_vbrk-zterm IS NOT INITIAL. "IF A ZTERM EXISTS FOR THE DOC NUMBER
EXECUTES THE CODE BELOW

lv_doc_date = wa_inv_vbrk-fkdat.

LOOP AT lt_zterm_all INTO wa_zterm_selection WHERE zterm = wa_inv_vbrk-zterm.


IF lv_doc_date+6(2) <= wa_zterm_selection-ztagg.
EXIT.
ENDIF.
ENDLOOP.

"FIRST DISCOUNT
IF wa_zterm_selection-zprz1 IS NOT INITIAL.
"AJUST DATE
CALL FUNCTION 'FIEU_SAFT_SETTL_CHANGE_DATE'
EXPORTING
iv_original_date = lv_doc_date
iv_months = wa_zterm_selection-zmona
iv_fixed_day = wa_zterm_selection-zfael
IMPORTING
ev_changed_date = lv_doc_date.

"CHANGE TEXT LANG TO COUNTRY SPECIFIC


lv_sy_lang = sy-langu.

SET LOCALE LANGUAGE gv_langs.


"FETCH PAYMENT TERMS TEXTS
CALL FUNCTION 'FI_TEXT_ZTERM'
EXPORTING
i_t052 = wa_zterm_selection
TABLES
t_ztext = lt_zterm_texts.
"CHANGE TEXT BACK TO ORIGINAL
SET LOCALE LANGUAGE lv_sy_lang.

LOOP AT lt_zterm_texts INTO lv_ttext. "REMOVE TEXTS WHICH ARENT USEFUL FOR
SAFT
IF lv_ttext NA '%'.
DELETE lt_zterm_texts INDEX sy-tabix.
ENDIF.
ENDLOOP.

"FETCH SETTLEMENT AGREEMENT TEXT


* CLEAR ls_bill_inv_settl-setlmnt_agrmnt.
SELECT SINGLE vtext FROM tvzbt INTO wa_inv_hd-settlmnt_agrmnt
WHERE zterm = wa_zterm_selection-zterm AND spras = gv_langs.
IF sy-subrc <> 0 OR wa_inv_hd-settlmnt_agrmnt IS INITIAL.
SELECT SINGLE vtext FROM tvzbt INTO wa_inv_hd-settlmnt_agrmnt
WHERE zterm = wa_zterm_selection-zterm AND spras = 'EN'.
IF sy-subrc <> 0 OR wa_inv_hd-settlmnt_agrmnt IS INITIAL.
SELECT SINGLE vtext FROM tvzbt INTO wa_inv_hd-settlmnt_agrmnt
WHERE zterm = wa_zterm_selection-zterm AND spras = sy-langu.
ENDIF.
ENDIF.

* CLEAR ls_bill_inv_settl-settle_date.
* CLEAR ls_bill_inv_settl-tot_settle_amount.
* CLEAR ls_bill_inv_settl-paym_terms.
IF wa_zterm_selection-ztag1 IS NOT INITIAL.
wa_inv_hd-settle_date1 = lv_doc_date + wa_zterm_selection-ztag1.
ELSEIF wa_zterm_selection-zstg1 IS NOT INITIAL.
CALL FUNCTION 'FIEU_SAFT_SETTL_CHANGE_DATE'
EXPORTING
iv_original_date = lv_doc_date
iv_months = wa_zterm_selection-zsmn1
iv_fixed_day = wa_zterm_selection-zstg1
IMPORTING
ev_changed_date = wa_inv_hd-settle_date1.
ENDIF.

wa_inv_hd-settle_amount_1 = ( wa_inv_hd-doc_total_gross *
wa_zterm_selection-zprz1 ) / 100.
READ TABLE lt_zterm_texts INTO wa_inv_hd-paym_terms_1 INDEX 1.
* APPEND ls_bill_inv_settl TO lt_bill_inv_settl.

"SECOND DISCOUNT
IF wa_zterm_selection-zprz2 IS NOT INITIAL.
* CLEAR ls_bill_inv_settl-settle_date.
* CLEAR ls_bill_inv_settl-tot_settle_amount.
* CLEAR ls_bill_inv_settl-paym_terms.
IF wa_zterm_selection-ztag2 IS NOT INITIAL.
wa_inv_hd-settle_date_2 = lv_doc_date + wa_zterm_selection-ztag2.

ELSEIF wa_zterm_selection-zstg2 IS NOT INITIAL.


CALL FUNCTION 'FIEU_SAFT_SETTL_CHANGE_DATE'
EXPORTING
iv_original_date = lv_doc_date
iv_months = wa_zterm_selection-zsmn2
iv_fixed_day = wa_zterm_selection-zstg2
IMPORTING
ev_changed_date = wa_inv_hd-settle_date_2.
ENDIF.

wa_inv_hd-settle_amount_2 = ( wa_inv_hd-doc_total_gross *
wa_zterm_selection-zprz2 ) / 100.
READ TABLE lt_zterm_texts INTO wa_inv_hd-paym_terms_2 INDEX 2.
* APPEND ls_bill_inv_settl TO lt_bill_inv_settl.
ENDIF.
ENDIF.
ENDIF.

* perform get_customer_address.

MOVE gv_uuid TO wa_inv_hd-ext_uuid.


*Country specific extension - additional data
PERFORM map_additional_data.

APPEND wa_inv_hd TO it_inv_hd.

*set source id
CLEAR: wa_inv_hd-source_id, wa_inv_hd-onetime_ind, wa_inv_hd-st_deliveryid,
wa_inv_hd-st_deliverydate, wa_inv_hd-move_etime, wa_inv_hd-move_stime.
CLEAR wa_inv_vbrk_cancel .
CLEAR wa_inv_hd-sourcebilling.
CLEAR : wa_inv_hd-inv_archive.
CLEAR ls_cancel_source_id .
CLEAR wa_inv_hd. " 20_F " Cleaning variables between iterations
ENDLOOP.
*----------------------22092014--------------------------------------*
* read delivery document date from archive when delivery document is archived;
* but VBFA is still not archived fully
IF it_likp_vbeln[] IS NOT INITIAL AND p_sysarc IS NOT INITIAL.
PERFORM read_rv_likp_archive.
PERFORM map_archived_likp.
ENDIF.
*----------------------22092014--------------------------------------*
SORT it_inv_hd.

ENDFORM. " ASSIGN_DATA_FOR_TBL_UPDATE


*&---------------------------------------------------------------------*
*& Form SET_INVOICE_STATUS
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
FORM set_invoice_status .
DATA : ls_inv_vbrk LIKE LINE OF it_inv_vbrk.
DATA : ls_inv_can_period TYPE monat.
DATA : ls_inv_can_year TYPE gjahr.
IF wa_inv_vbrk-sfakn IS INITIAL AND wa_inv_vbrk-fksto IS INITIAL.
wa_inv_hd-inv_status = 'N'.

ENDIF.
CLEAR : ls_inv_vbrk.
IF wa_inv_vbrk-sfakn IS NOT INITIAL OR wa_inv_vbrk-fksto IS NOT INITIAL.
READ TABLE it_inv_vbrk
INTO ls_inv_vbrk
WITH KEY vbeln = wa_inv_vbrk-sfakn.
IF sy-subrc = 0.
CALL FUNCTION 'DETERMINE_PERIOD'
EXPORTING
date = ls_inv_vbrk-fkdat
* PERIOD_IN = '000'
version = gv_t001_periv
IMPORTING
period = ls_inv_can_period
year = ls_inv_can_year
EXCEPTIONS
period_in_not_valid = 1
period_not_assigned = 2
version_undefined = 3
OTHERS = 4.
ENDIF.

IF ls_inv_can_year = wa_inv_hd-inv_year OR
ls_inv_can_period = wa_inv_hd-inv_period OR
wa_inv_vbrk-fksto EQ 'X'. " Set Status A for Cancelled Document.
wa_inv_hd-inv_status = 'A'.
ELSE.
IF ls_inv_can_year <> wa_inv_hd-inv_year OR
ls_inv_can_period <> wa_inv_hd-inv_period.
wa_inv_hd-inv_status = 'N'.
ENDIF.
ENDIF.
ENDIF.

*Country specific setting - Invoice List- 20_F


IF gv_extn_ext_badi IS BOUND.
CALL BADI gv_extn_ext_badi->get_invoice_status
EXPORTING
iv_land1 = gv_bukrs_land
iv_bukrs = wa_inv_vbrk-bukrs
iv_vbeln = wa_inv_vbrk-vbeln
iv_fkdat = wa_inv_vbrk-fkdat
CHANGING
cv_inv_status = wa_inv_hd-inv_status
ct_inv_data = wa_inv_vbrk.

ENDIF.

IF gv_usr_exit_impl GT 0.
CALL BADI gv_usr_exit_badi->set_invoice_status_sd
EXPORTING
iv_invoice_num = wa_inv_vbrk-vbeln
iv_inv_date = wa_inv_vbrk-fkdat
iv_bukrs = wa_inv_vbrk-bukrs
iv_fkart = wa_inv_vbrk-fkart
iv_kunrg = wa_inv_vbrk-kunrg
iv_kunag = wa_inv_vbrk-kunag
iv_vbtyp = wa_inv_vbrk-vbtyp
iv_sfakn = wa_inv_vbrk-sfakn
iv_fksto = wa_inv_vbrk-fksto
CHANGING
cv_inv_status = wa_inv_hd-inv_status.
ENDIF.

ENDFORM. " SET_INVOICE_STATUS


*&---------------------------------------------------------------------*
*& Form UPDATE_SAFT_TABLES
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
FORM update_saft_tables .

IF it_inv_hd[] IS NOT INITIAL.


IF p_archvl IS NOT INITIAL. " if same invoice document is included in multiple
archive file
SORT it_inv_hd .
DELETE ADJACENT DUPLICATES FROM it_inv_hd COMPARING ALL FIELDS .
ENDIF. "20_f
INSERT fieud_sdinv_h FROM TABLE it_inv_hd.
INSERT fieud_sdinv_i FROM TABLE it_inv_lnes.

ENDIF.
ENDFORM. " UPDATE_SAFT_TABLES
*&---------------------------------------------------------------------*
*& Form UPDATE_SUMMARY_RUN
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
FORM update_summary_run .
CLEAR : wa_summary_run.
CLEAR : it_summary_run[].
IF gv_invoice_no_arch IS NOT INITIAL.
gv_invoice_no = gv_invoice_no_arch.
ENDIF.
IF gv_invoice_no GT 0.

MOVE sy-mandt TO wa_summary_run-mandt.


MOVE p_bukrs TO wa_summary_run-bukrs.
MOVE p_gjahr TO wa_summary_run-gjahr.
MOVE s_date-low TO wa_summary_run-from_date.
MOVE s_date-high TO wa_summary_run-to_date.
MOVE gv_invoice_no TO wa_summary_run-total_no_docs.
IF p_archvl IS INITIAL.
MOVE 'SD' TO wa_summary_run-inv_category.
MOVE gv_invoice_no TO wa_summary_run-total_no_docs.
ELSE.
MOVE 'SA' TO wa_summary_run-inv_category.
MOVE gv_invoice_no_arch TO wa_summary_run-total_no_docs.
ENDIF.
MOVE gv_total_debit_sd TO wa_summary_run-total_debit_amt.
MOVE gv_total_credit_sd TO wa_summary_run-total_credit_amt.
MOVE gv_t001_waers TO wa_summary_run-currency."4_F
MOVE gv_uuid TO wa_summary_run-ext_uuid.

APPEND wa_summary_run TO it_summary_run.


gv_appln_msg-msgty = 'I'. " Mandatory field missing
gv_appln_msg-msgid = gc_message_class .
gv_appln_msg-msgno = '061'.
gv_appln_msg-msgv1 = 'SD'.
gv_appln_msg-msgv2 = wa_summary_run-total_debit_amt.
gv_appln_msg-msgv3 = wa_summary_run-total_credit_amt.
CALL METHOD cl_generic_obj->gen_appl_log
EXPORTING
iv_appl_msg = gv_appln_msg
iv_land = gv_bukrs_land
iv_display = '-'
iv_handle = gv_appln_handle.

CLEAR : gv_appln_msg.

ENDIF.
ENDFORM. " UPDATE_SUMMARY_RUN
" GET_DIGITAL_SIGNATURE
*&---------------------------------------------------------------------*
*& Form SET_SHIP_FROM
*&---------------------------------------------------------------------*
*normal invoices- Case 1
*--------------
* Read and maintain the ship from address of the parties involved in the invoice.
* in case of normal invoice, ship from would be the plant from which the invoices
are
* shipped to the customer and Ship to would be the sold-to-party maintained in the
* billing document

*Cancellation and return of goods - Case 2


*---------------------------------
*in case of return of goods or cancellation of invoices, the ship from would be the
*customer who has sent the good back to the company

*----------------------------------------------------------------------*

FORM set_ship_from .

DATA : lv_werks_addrnr TYPE t001w-adrnr.


* assign the plant of the first line item to the header as ship from plant - in
archived data
CLEAR : gs_vbrp."18_F
READ TABLE gt_vbrp INTO gs_vbrp WITH KEY vbeln = wa_inv_vbrk-vbeln . " 18_f
IF sy-subrc = 0."18_f
MOVE gs_vbrp-werks TO wa_inv_hd-sf_werks . " 18_f
ENDIF."18_f
CLEAR :gs_vbrp."18_f

*Case 1

IF wa_inv_vbrk-sfakn IS INITIAL AND wa_inv_vbrk-fksto IS INITIAL. .


READ TABLE it_inv_lnes INTO wa_inv_lnes WITH KEY invoice_no = wa_inv_hd-
invoice_no.
IF wa_inv_hd-sf_werks IS INITIAL.
READ TABLE it_inv_lnes INTO wa_inv_lnes WITH KEY invoice_no = wa_inv_hd-
invoice_no.
IF sy-subrc = 0.
MOVE wa_inv_lnes-plant TO wa_inv_hd-sf_werks .
ENDIF.
ENDIF.
IF wa_inv_hd-sf_werks IS NOT INITIAL.
READ TABLE it_plant_address
INTO wa_plant_address
WITH KEY werks = wa_inv_hd-sf_werks.
IF sy-subrc <> 0.
SELECT SINGLE
stras
pstlz
ort01
land1
adrnr
FROM t001w
INTO (wa_inv_hd-sf_address,
wa_inv_hd-sf_postal,
wa_inv_hd-sf_city,
wa_inv_hd-sf_country,
lv_werks_addrnr )
WHERE werks = wa_inv_hd-sf_werks.
IF wa_inv_hd-sf_address IS NOT INITIAL.
MOVE wa_inv_hd-sf_werks TO wa_plant_address-werks.
MOVE wa_inv_hd-sf_address TO wa_plant_address-stras.
MOVE wa_inv_hd-sf_postal TO wa_plant_address-pstlz.
MOVE wa_inv_hd-sf_city TO wa_plant_address-ort01.
MOVE wa_inv_hd-sf_country TO wa_plant_address-land1.
MOVE lv_werks_addrnr TO wa_plant_address-adrnr.
APPEND wa_plant_address TO it_plant_address.

ENDIF.
ELSE.
MOVE wa_plant_address-werks TO wa_inv_hd-sf_werks .
MOVE wa_plant_address-stras TO wa_inv_hd-sf_address .
MOVE wa_plant_address-pstlz TO wa_inv_hd-sf_postal.
MOVE wa_plant_address-ort01 TO wa_inv_hd-sf_city.
MOVE wa_plant_address-land1 TO wa_inv_hd-sf_country.

ENDIF.
ENDIF.
IF wa_inv_hd-sf_address IS INITIAL OR
wa_inv_hd-sf_postal IS INITIAL OR
wa_inv_hd-sf_city IS INITIAL
.

gv_appln_msg-msgty = 'I'. " Mandatory field missing


gv_appln_msg-msgid = gc_message_class .
gv_appln_msg-msgno = '048'.
gv_appln_msg-msgv1 = wa_inv_hd-invoice_no..

CALL METHOD cl_generic_obj->gen_appl_log


EXPORTING
iv_appl_msg = gv_appln_msg
iv_land = gv_bukrs_land
iv_display = '-'
iv_handle = gv_appln_handle.

CLEAR : gv_appln_msg.
ENDIF.
ENDIF.
ENDFORM. " SET_SHIP_FROM
*&---------------------------------------------------------------------*
*& Form MAP_DISCOUNT_INFO
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
FORM map_discount_info .
CLEAR wa_inv_discounts.
CHECK it_inv_dis IS NOT INITIAL.
LOOP AT it_inv_discounts INTO wa_inv_discounts.
READ TABLE it_inv_dis INTO wa_inv_dis WITH KEY kschl = 'SKTO'
kposn = wa_inv_lnes-posnr.
IF sy-subrc = 0.
MOVE wa_inv_dis-kschl TO wa_inv_lnes-kschl.
MOVE wa_inv_dis-kwert TO wa_inv_lnes-discount_amt.
ELSE.
* INVOKE BADI

ENDIF.
ENDLOOP.
ENDFORM. " MAP_DISCOUNT_INFO
*&---------------------------------------------------------------------*
*& Form GET_DOC_TOTALS_SD
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
FORM get_doc_totals_sd .
CLEAR:wa_inv_hd-doc_total_gross,
wa_inv_hd-doc_total_net,
wa_inv_hd-doc_total_tax.
MOVE wa_inv_vbrk-netwr TO wa_inv_hd-doc_total_net.
MOVE wa_inv_vbrk-mwsbk TO wa_inv_hd-doc_total_tax.

IF gv_usr_exit_impl GT 0.
CALL BADI gv_usr_exit_badi->set_net_tax
EXPORTING
iv_invoice_num = wa_inv_vbrk-vbeln
iv_inv_date = wa_inv_vbrk-fkdat
iv_inv_currency = wa_inv_vbrk-waerk
CHANGING
cv_inv_net_amt = wa_inv_hd-doc_total_net
cv_inv_tax_amt = wa_inv_hd-doc_total_tax.
ENDIF.

*conditiona added to adjust as per the country specific adjustments - 19_F


IF wa_inv_hd-doc_total_gross IS INITIAL.
wa_inv_hd-doc_total_gross = wa_inv_hd-doc_total_net + wa_inv_hd-doc_total_tax.
ENDIF.
*conditiona added to adjust as per the country specific adjustments - 19_F
ENDFORM. " GET_DOC_TOTALS_SD
*&---------------------------------------------------------------------*
*& Form UPDATE_SD_SUMMARY
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
FORM update_sd_summary .
IF it_summary_run[] IS NOT INITIAL.
INSERT fieud_invsummary FROM TABLE it_summary_run.
ENDIF .
CLEAR it_summary_run[].
ENDFORM. " UPDATE_SD_SUMMARY
*&---------------------------------------------------------------------*
*& Form IDENTIFY_SALES_ORDER_INFO
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
FORM identify_sales_order_info .
DATA : lt_so_data TYPE TABLE OF st_so_data.

SELECT vbeln audat FROM vbak


INTO CORRESPONDING FIELDS OF TABLE lt_so_data
FOR ALL ENTRIES IN it_so_data
WHERE vbeln = it_so_data-vbeln.
IF sy-subrc = 0.
it_so_data[] = lt_so_data[].
ENDIF.
* Sales order information archived for live invoice documents
IF p_sysarc IS NOT INITIAL OR p_sysarc = 'X'.
PERFORM identify_salesorder_missing_db.

PERFORM read_salesorder_from_archives.

ENDIF.

ENDFORM. " IDENTIFY_SALES_ORDER_INFO


*&---------------------------------------------------------------------*
*& Form MAP_TAX_INFO_INV_LINES
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* -->P_WA_INV_COND_REC_MWSK1 text
* -->P_ENDIF text
*----------------------------------------------------------------------*
FORM map_tax_info_inv_lines USING mwsk1
.
MOVE mwsk1 TO wa_inv_cond_rec-mwsk1.
READ TABLE it_tax_data
INTO wa_tax_data
WITH KEY taxcode = wa_inv_cond_rec-mwsk1..
IF sy-subrc = 0.
*Tax code as per the invoice line item condition record
IF wa_inv_cond_rec-mwsk1 IS NOT INITIAL.
MOVE wa_inv_cond_rec-mwsk1 TO wa_inv_lnes-taxcode.
ENDIF.

MOVE wa_tax_data-taxpercentage TO wa_inv_lnes-taxpercent.


*TaxExemptionReason
IF wa_inv_lnes-taxpercent IS INITIAL OR
wa_inv_lnes-taxpercent = '0.00' OR
wa_inv_lnes-taxpercent = 0.

ENDIF.
IF ( wa_inv_lnes-taxamount = '0.00' OR wa_inv_lnes-taxamount IS INITIAL ).
ENDIF.

ENDIF.

ENDFORM. " MAP_TAX_INFO_INV_LINES


*&---------------------------------------------------------------------*
*& Form GET_BILLING_HISTORY
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
FORM get_billing_history .
DATA: ls_vbfa_archived LIKE LINE OF gt_vbfa.
* CLEAR :it_inv_del_cmemo[].

LOOP AT gt_vbfa INTO ls_vbfa_archived .


IF ls_vbfa_archived = 'J' OR ls_vbfa_archived = 'T' OR ls_vbfa_archived = 'O'.
wa_inv_del_cmemo-vbelv = ls_vbfa_archived-vbelv .
wa_inv_del_cmemo-posnv = ls_vbfa_archived-posnv .
wa_inv_del_cmemo-vbeln = ls_vbfa_archived-vbeln .
wa_inv_del_cmemo-posnn = ls_vbfa_archived-posnn .
wa_inv_del_cmemo-vbtyp_v = ls_vbfa_archived-vbtyp_v .
APPEND wa_inv_del_cmemo TO it_inv_del_cmemo.
CLEAR wa_inv_del_cmemo.
ENDIF.
CLEAR ls_vbfa_archived.
ENDLOOP.

ENDFORM. " GET_BILLING_HISTORY


*&---------------------------------------------------------------------*
*& Form TAX_LIKE_MATERIAL
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
FORM tax_like_material .
CLEAR gv_tax_mat.
SORT it_vbrp BY vbeln posnr DESCENDING.
READ TABLE it_vbrp INTO wa_vbrp INDEX 1.
IF sy-subrc = 0.
gv_tax_mat = wa_vbrp-posnr.
ELSE.
DESCRIBE TABLE it_vbrp LINES gv_tax_mat.
ENDIF.

MOVE gv_bukrs_land TO gv_land_exit.

TRY.
GET BADI gv_usr_exit_badi
FILTERS
land1 = gv_land_exit.
CATCH cx_badi_not_implemented .

ENDTRY.
CLEAR : gv_usr_exit_impl.
IF gv_usr_exit_badi IS NOT INITIAL.
TRY .

CALL METHOD cl_badi_query=>number_of_implementations


EXPORTING
badi = gv_usr_exit_badi
RECEIVING
num = gv_usr_exit_impl.

CATCH cx_badi_not_implemented.

ENDTRY.
ENDIF.

LOOP AT it_inv_discounts INTO wa_inv_discounts WHERE knumv = wa_inv_vbrk-knumv.


READ TABLE it_tax_material INTO wa_tax_material
WITH KEY taxlikemat = wa_inv_discounts-kschl.
IF sy-subrc = 0.
gv_tax_mat = gv_tax_mat + 1.
wa_inv_lnes-bukrs = p_bukrs.
wa_inv_lnes-invoice_no = wa_inv_vbrk-vbeln.
wa_inv_lnes-inv_year = p_gjahr.
wa_inv_lnes-posnr = gv_tax_mat.
wa_inv_lnes-inv_date = wa_inv_vbrk-fkdat.
wa_inv_lnes-productcode = wa_inv_discounts-kschl.
wa_inv_lnes-taxlike_mat_ind = 'X'.
wa_inv_lnes-taxpointdate = wa_inv_vbrk-fkdat.
READ TABLE it_vbrp INTO wa_vbrp WITH KEY vbeln = wa_inv_vbrk-vbeln
posnr = wa_inv_discounts-kposn.
IF sy-subrc = 0.
wa_inv_lnes-quantity = wa_vbrp-fkimg.
wa_inv_lnes-unitprice = wa_inv_discounts-kwert / wa_vbrp-fkimg.
ENDIF.

wa_inv_lnes-unitofmeasure = 'UN'.

IF wa_inv_vbrk-vbtyp = 'N' OR wa_inv_vbrk-vbtyp = 'O'.


IF wa_inv_discounts-kwert GE 0.
wa_inv_lnes-debit_amount = wa_inv_discounts-kwert.
ELSE.
wa_inv_lnes-credit_amount = wa_inv_discounts-kwert.
ENDIF.
ELSE.
IF wa_inv_discounts-kwert GE 0.
wa_inv_lnes-credit_amount = wa_inv_discounts-kwert.
ELSE.
wa_inv_lnes-debit_amount = wa_inv_discounts-kwert.
ENDIF.
ENDIF.
wa_inv_lnes-currency = wa_inv_vbrk-waerk.

IF gv_usr_exit_impl GT 0.
CALL BADI gv_usr_exit_badi->set_amounts_credit_debit
EXPORTING
iv_invoice_num = wa_inv_vbrk-vbeln
iv_inv_date = wa_inv_vbrk-fkdat
iv_inv_currency = wa_inv_vbrk-waerk
iv_inv_status = wa_inv_hd-inv_status
CHANGING
cv_inv_debit_amt = wa_inv_lnes-debit_amount
cv_inv_credit_amt = wa_inv_lnes-credit_amount
cv_unit_price = wa_inv_lnes-unitprice.
ENDIF.

SELECT SINGLE vtext FROM t685t INTO wa_inv_lnes-description_line "#EC


CI_GENBUFF
WHERE spras = gv_langs
AND kappl = 'V'
AND kschl = wa_inv_discounts-kschl.
IF sy-subrc <> 0.
SELECT SINGLE vtext FROM t685t INTO wa_inv_lnes-description_line "#EC
CI_GENBUFF
WHERE spras = 'EN'
AND kappl = 'V'
AND kschl = wa_inv_discounts-kschl.
IF sy-subrc <> 0.
SELECT SINGLE vtext FROM t685t INTO wa_inv_lnes-description_line "#EC
CI_GENBUFF
WHERE spras = sy-langu
AND kappl = 'V'
AND kschl = wa_inv_discounts-kschl.
ENDIF.
ENDIF.

wa_inv_lnes-productdesc = wa_inv_lnes-description_line.
wa_inv_lnes-taxcode = wa_inv_discounts-mwsk1.
MOVE gv_uuid TO wa_inv_lnes-ext_uuid.
wa_inv_lnes-knumv = wa_inv_discounts-knumv.
wa_inv_lnes-kposn = wa_inv_discounts-kposn.
wa_inv_lnes-kschl = wa_inv_discounts-kschl.
wa_inv_lnes-kappl = 'V'.
wa_inv_lnes-mandt = sy-mandt.
* wa_inv_discounts-kwert.
APPEND wa_inv_lnes TO it_inv_lnes.
CLEAR wa_inv_lnes.
ENDIF.
ENDLOOP.

ENDFORM. " TAX_LIKE_MATERIAL


*&---------------------------------------------------------------------*
*& Form RETRIEVE_SALES_OFFICE
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
FORM retrieve_sales_office .
DATA : lv_count_so TYPE i. " 12_F

SELECT * FROM fieuc_salesofc


INTO CORRESPONDING FIELDS OF TABLE it_sales_offices
WHERE bukrs = p_bukrs.
SORT it_sales_offices.
DESCRIBE TABLE it_sales_offices LINES lv_count_so.

IF sy-subrc = 0 .
gv_appln_msg-msgty = 'I'. " Mandatory field missing
gv_appln_msg-msgid = gc_message_class .
gv_appln_msg-msgno = '094'. "12_F
gv_appln_msg-msgv1 = p_bukrs.

CALL METHOD cl_generic_obj->gen_appl_log


EXPORTING
iv_appl_msg = gv_appln_msg
iv_land = gv_bukrs_land
iv_display = '-'
iv_handle = gv_appln_handle.
gv_so_cust_exists_flg = 'X'.
gv_appln_msg-msgty = 'I'. " Mandatory field missing
gv_appln_msg-msgid = gc_message_class .
gv_appln_msg-msgno = '095'.
gv_appln_msg-msgv1 = lv_count_so.

CALL METHOD cl_generic_obj->gen_appl_log


EXPORTING
iv_appl_msg = gv_appln_msg
iv_land = gv_bukrs_land
iv_display = '-'
iv_handle = gv_appln_handle.

ENDIF.

ENDFORM. " RETRIEVE_SALES_OFFICE

You might also like