I am trying to fix a certain already developed function with the goal that the SD Invoice with amount 0 EUR should not be transmitted to FI. As I understood, the below code is used to select the data from FI and SD:
* select order-related invoices
SELECT * FROM vbfa AS v INTO TABLE gt_vbfa_inv
FOR ALL ENTRIES IN gt_vbak
WHERE vbelv = gt_vbak-vbeln
AND vbtyp_n IN ('M', 'O', 'P', '5', '6')
AND stufe = '00'
AND NOT EXISTS ( SELECT * FROM vbfa
WHERE vbelv = v~vbeln
AND posnv = v~posnn
AND vbtyp_n IN ('N', 'S')
AND stufe = '00' ) .
IF sy-subrc = 0.
* select invoice head status
SELECT DISTINCT * FROM vbuk APPENDING TABLE gt_vbuk_inv
FOR ALL ENTRIES IN gt_vbfa_inv
WHERE vbeln = gt_vbfa_inv-vbeln. "#EC CI_SUBRC
ENDIF.
SORT gt_vbuk_inv BY vbeln.
DELETE ADJACENT DUPLICATES FROM gt_vbuk_inv COMPARING vbeln.
IF me->gv_items = abap_true AND gt_vbuk_inv IS NOT INITIAL.
SELECT * FROM vbrp INTO TABLE gt_vbrp
FOR ALL ENTRIES IN gt_vbuk_inv
WHERE vbeln = gt_vbuk_inv-vbeln. "#EC CI_SUBRC
ENDIF.
As far as I can understand from the above written code, is that the table VBFA is used to get the data for FI, while the table VBRP is used to get the data for SD. What I want to achieve is that when the invoice number does not have a FI document, then the invoice number will be empty.
If the tables BKPF(for the FI) and VBRK(for the SD) would be used, then I could have tried the relation:
vbrk-xblnr=bkpf-xblnr.
However, those tables are not used in the function. May I please ask you, how can I fix the code so that when the invoice number does not have a FI document, thus the invoices with a value of 0 EUR will not generate an FI document, then the invoice number will be empty.
Thank you all in advance!
CodePudding user response:
Since the goal is
the SD Invoice with amount 0 EUR should not be transmitted to FI
I suppose your code is in some user-exit or standard program modification when releasing the SD invoice to Accounting. If so, the BKPF
is not created yet and there's no reason in selecting it.
The select from VBFA
is not extracting data from FI. Starting from the Sales Order it is extracting the following SD documents (first document flow level only)
M Invoice
N Invoice Cancellation
P Debit Memo
5 Intercompany Invoice
6 Intercompany Credit Memo
And excluding those invoices that have a subsequent cancellation
N Invoice Cancellation
S Credit Memo Cancellation
Those documents can be found in VBRK
table (SD invoice header) with the following select
SELECT DISTINCT * FROM vbrk APPENDING TABLE gt_vbrk
FOR ALL ENTRIES IN gt_vbfa_inv
WHERE vbeln = gt_vbfa_inv-vbeln.
Btw: I don't know the reason for the VBUK
select since you're not using any document status information
If you're asking for the SD invoices with zero amount with the purpose to not release them to accounting (since otherwise they would produce an error in FI) you don't have to select BKPF
but check VBRK-NETWR = 0
for every entry in gt_vbrk
table