‎2008 May 06 4:02 PM
Hi All,
I want to create a vendor invoice using BAPI_INCOMINGINVOICE_CREATE. The main problem i am facing is that after the invoice has been posted, when i check the created invoice, the G/L account is taken from the material not the one associated with the PO and the posting key is not 40.
I want the G/L account be taken from the PO and have posting key as 40.
Kindly let me know how do i that....if anyone has a sample code...it is highly appreciated...
Thanks..
Preetham S
‎2008 May 06 5:13 PM
Here is a sample code to copy a document.
*&---------------------------------------------------------------------*
*& Form f300_duplicate
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
FORM f300_duplicate .
LOOP AT t_bseg.
MOVE-CORRESPONDING t_bseg TO t_bseg2.
APPEND t_bseg2.
ENDLOOP.
SORT t_bseg2 BY belnr bschl.
LOOP AT t_bseg2.
MOVE-CORRESPONDING t_bseg2 TO w_bseg2.
AT NEW belnr.
* Invoice or Credit memo ?
IF w_bseg2-bschl = k_31.
w_headerdata-invoice_ind = k_x.
* Full structure W_HEADERDATA
PERFORM f310_header_data.
ELSE.
w_headerdata-invoice_ind = space.
PERFORM f310_header_data.
ENDIF.
ENDAT.
* Full table T_ITEMDATA
IF t_bseg2-bschl = k_81 OR t_bseg2-bschl = k_91.
PERFORM f320_item_data.
ENDIF.
* If item 40 or 50 : + or - amnt on the hight amount of line in 81 or 91
IF w_headerdata-invoice_ind = k_x.
CLEAR w_wrbtr2.
w_wrbtr2 = t_bseg2-wrbtr.
IF t_bseg2-bschl = k_40.
w_wrbtr = w_wrbtr + w_wrbtr2.
SORT t_itemdata BY item_amount DESCENDING.
READ TABLE t_itemdata INDEX 1.
t_itemdata-item_amount = t_itemdata-item_amount + w_wrbtr2.
MODIFY t_itemdata INDEX 1.
ELSEIF t_bseg2-bschl = k_50.
w_wrbtr = w_wrbtr - w_wrbtr2.
SORT t_itemdata BY item_amount DESCENDING.
READ TABLE t_itemdata INDEX 1.
t_itemdata-item_amount = t_itemdata-item_amount - w_wrbtr2.
MODIFY t_itemdata INDEX 1.
ENDIF.
ELSE.
CLEAR w_wrbtr2.
w_wrbtr2 = t_bseg2-wrbtr.
IF t_bseg2-bschl = k_40.
w_wrbtr = w_wrbtr - w_wrbtr2.
SORT t_itemdata BY item_amount DESCENDING.
READ TABLE t_itemdata INDEX 1.
t_itemdata-item_amount = t_itemdata-item_amount - w_wrbtr2.
MODIFY t_itemdata INDEX 1.
ELSEIF t_bseg2-bschl = k_50.
w_wrbtr = w_wrbtr + w_wrbtr2.
SORT t_itemdata BY item_amount DESCENDING.
READ TABLE t_itemdata INDEX 1.
t_itemdata-item_amount = t_itemdata-item_amount + w_wrbtr2.
MODIFY t_itemdata INDEX 1.
ENDIF.
ENDIF.
AT END OF belnr.
IF w_wrbtr = w_amount_lifnr.
w_headerdata-gross_amount = w_wrbtr.
ELSE.
w_headerdata-gross_amount = w_amount_lifnr.
ENDIF.
t_taxdata-tax_code = k_v1.
APPEND t_taxdata.
*** Check if doc already exist only when online
IF sy-batch = space.
SELECT bukrs belnr gjahr UP TO 1 ROWS FROM bkpf
INTO (bkpf-bukrs, bkpf-belnr, bkpf-gjahr)
WHERE bukrs = w_headerdata-comp_code
AND gjahr = p_gjahr
AND bldat = w_headerdata-doc_date
AND blart = w_headerdata-doc_type
AND xblnr = w_headerdata-ref_doc_no.
ENDSELECT.
IF sy-subrc = 0.
*** BEGIN ADD LCO020108
w_message1 = text-007.
REPLACE '&1' WITH bkpf-belnr INTO w_message1.
*** END ADD
CALL FUNCTION 'FI_WT_POPUP_TO_CONFIRM_STEP'
EXPORTING
defaultoption = 'Y'
textline1 = w_message1
textline2 = text-008
titel = text-009
start_column = 25
start_row = 6
IMPORTING
answer = w_answer.
ENDIF.
ENDIF.
IF ( sy-batch = space AND w_answer = 'J' ) OR sy-batch = k_x
OR sy-subrc <> 0.
*----- call BAPI Create -----------------------------------------------*
CALL FUNCTION 'BAPI_INCOMINGINVOICE_CREATE'
EXPORTING
headerdata = w_headerdata
addressdata = w_addressdata
IMPORTING
invoicedocnumber = w_invoicedocnumber
fiscalyear = w_fiscalyear
TABLES
itemdata = t_itemdata
taxdata = t_taxdata
return = t_return.
IF NOT t_return[] IS INITIAL.
ROLLBACK WORK.
CLEAR : w_headerdata, w_addressdata,
w_invoicedocnumber, w_fiscalyear,
t_itemdata, t_taxdata.
REFRESH : t_itemdata, t_taxdata.
LOOP AT t_return.
MOVE t_return-message TO t_error-message.
MOVE t_bseg2-belnr TO t_error-belnr.
APPEND t_error.
ENDLOOP.
*** BEGIN ADD LCO020108
IF ( sy-batch = space AND w_answer = 'J' ).
CALL FUNCTION 'POPUP_TO_INFORM'
EXPORTING
titel = text-014
txt1 = text-013
txt2 = text-016.
ENDIF.
*** END ADD
ELSE.
COMMIT WORK AND WAIT.
MOVE w_headerdata-comp_code TO t_report2-bukrs.
MOVE w_invoicedocnumber TO t_report2-nb.
MOVE w_fiscalyear TO t_report2-year.
APPEND t_report2.
*** BEGIN ADD LCO020108
IF ( sy-batch = space AND w_answer = 'J' ).
CALL FUNCTION 'POPUP_TO_INFORM'
EXPORTING
titel = text-014
txt1 = text-015
txt2 = space.
ENDIF.
*** END ADD
CLEAR : w_headerdata, w_addressdata, w_wrbtr,
w_invoicedocnumber, w_fiscalyear,
t_itemdata, t_taxdata.
REFRESH : t_itemdata, t_taxdata.
ENDIF.
Regards,
Christophe
Please reward if helpful