Create Invoice Using Material and Customer Specific Data With or Without Using Delivery

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 14

Create Invoice using Material and customer specific data with or without using Delivery.

Business Requirements:
As part of the KCP implementation of the PROS PPSS pricing suite, a number of new interfaces are required from/to our existing applications. This document covers the requirements for simulated CO-PA values for chargeback credits to be sent from SAP system to a staging area, for transformation and loading in to the Scientific Analytics (SA) module of PPSS. The simulated CO-PA values are required in order for a complete analysis to be possible for chargebacks in the SA module. The required CO-PA values are not captured as part of the normal chargeback process in SAP and the SA module cannot get them in the standard way, which would be by doing a look up for the chargeback customer and finding actual CO-PA postings on invoices for the corresponding customer/material/date combinations. This is because a large number of KCP chargeback customers are only used for chargebacks, and an unrelated customer number is used for sales orders/invoices.

Reason: Try to simulate the creation of CO-PA posting values for a billing document, based on the
entered selection criteria. On entering the required values (such as customer, material, quantity, pricing date etc.) the program will calculate and output the CO-PA posting values that would be generated if a billing document was to be created with the entered selection criteria. The output from the program will be added to a custom table. Use this function module GN_INVOICE_CREATE to get the correct values for sap system.

SAP Code : (This is demo program if anyone interested then I will provide original program )
*&---------------------------------------------------------------------* *& Report ZFIEUR_PRICING_SIMULATION *& *&---------------------------------------------------------------------* REPORT ZFIEUR_PRICING_SIMULATION NO STANDARD PAGE HEADING. *TYPE-POOLS: slis. tables: lips,tvko, vbak, vbrk. DATA S_knvv TYPE knvv. DATA S_mara TYPE mara. DATA S_mvke TYPE mvke.

DATA: BEGIN OF it_customer OCCURS 0, kunag TYPE kunag, vwerk TYPE dwerk, kalks TYPE kalks, END OF it_customer.

DATA: BEGIN OF it_material OCCURS 0, matnr TYPE matnr, END OF it_material. DATA: BEGIN OF it_mara OCCURS 0, matnr TYPE matnr, mstae TYPE mstae, normt TYPE normt, END OF it_mara. DATA: BEGIN OF it_error OCCURS 0, message(100) TYPE c, END OF it_error. DATA: BEGIN OF it_condition OCCURS 0, kschl TYPE kschl, END OF it_condition. data : con_x(1) value 'X', iv_relevant(1), rc_ok(1), sel_tabix like sy-tabix, con_posting, retcode like sy-subrc. data : loc_iv_fkara like komfkgn-fkara, loc_fkara_iva like komfkgn-fkara value 'IVA', loc_fkara_iga like komfkgn-fkara value 'IGA'. * data : begin of include data : end of * data : begin of include data : end of

sel_vbrk_tab occurs 0. structure vbrk. sel_vbrk_tab. loc_vbrk_tab occurs 0. structure vbrk. loc_vbrk_tab.

data : begin of loc_vbrp_tab occurs 0. include structure vbrp. data : end of loc_vbrp_tab. RANGES: gr_auart FOR vbak-auart. include rvafssli. " fr GN_INVOICE_CREATE include rvvbtyp. * selection screen ----------------------------------------------------* SELECTION-SCREEN BEGIN OF BLOCK bl_org WITH FRAME TITLE text-b01. PARAMETERS: p_vkorg TYPE vkorg ,

p_vtweg p_spart p_PSTYV p_auart P_fkart p_prsdt

TYPE TYPE type TYPE TYPE TYPE

vtweg , spart , PSTYV, auart OBLIGATORY , VBRK-FKART obligatory Default 'ZF2', prsdt OBLIGATORY DEFAULT sy-datlo.

SELECT-OPTIONS: s_vbeln for vbak-vbeln, s_vgbel FOR lips-vgbel, s_vbels for vbrk-vbeln, s_kunag for S_knvv-kunnr , s_matnr FOR S_mara-matnr . SELECTION-SCREEN END OF BLOCK bl_org.

START-OF-SELECTION. PERFORM get_data. END-OF-SELECTION. PERFORM simulate_invoice. READ TABLE it_error INDEX 1. IF sy-subrc EQ 0. PERFORM write_errors. ENDIF. *&---------------------------------------------------------------------* *& Form get_data *&---------------------------------------------------------------------* FORM get_data. DATA: BEGIN OF lt_material OCCURS 0, matnr TYPE matnr, mstae TYPE mstae, END OF lt_material. if s_kunag[] is not initial. * select customer based on selection parameters SELECT kunnr vwerk kalks FROM knvv INTO TABLE it_customer WHERE kunnr IN s_kunag AND vkorg EQ p_vkorg AND vtweg EQ p_vtweg AND spart EQ p_spart. * select materials based on selection parameters SELECT matnr WHERE AND AND FROM mvke INTO TABLE it_material matnr IN s_matnr vkorg EQ p_vkorg vtweg EQ p_vtweg.

** *** get additional material master fields SELECT matnr mstae normt FROM mara INTO TABLE it_mara FOR ALL ENTRIES IN it_material WHERE matnr EQ it_material-matnr. IF sy-subrc EQ 0. SORT it_mara BY matnr. ENDIF. endif. ENDFORM. " get_data *&---------------------------------------------------------------------* *& Form simulate_invoice *&---------------------------------------------------------------------* FORM simulate_invoice. DATA: ls_vbsk TYPE vbsk, lt_komfk TYPE komfk OCCURS 0 WITH HEADER LINE, lt_komfkgn TYPE komfkgn OCCURS 0 WITH HEADER LINE, lt_komfkko TYPE komv OCCURS 0 WITH HEADER LINE, lt_komv TYPE komv OCCURS 0 WITH HEADER LINE, lt_thead TYPE theadvb OCCURS 0 WITH HEADER LINE, lt_vbfs TYPE vbfs OCCURS 0 WITH HEADER LINE, lt_vbpa TYPE vbpavb OCCURS 0 WITH HEADER LINE, lt_vbrk TYPE vbrkvb OCCURS 0 WITH HEADER LINE, lt_vbrp TYPE vbrpvb OCCURS 0 WITH HEADER LINE, lt_t001k type t001k occurs 0 with header line, lt_t001w type t001w occurs 0 with header line, lt_vbss TYPE vbss OCCURS 0 WITH HEADER LINE. if It_customer[] is not initial and s_kunag[] is not initial. LOOP AT it_customer. LOOP AT it_material. * get additional fields from table MARA CLEAR it_mara. READ TABLE it_mara WITH KEY matnr = it_material-matnr BINARY SEARCH. lt_komfkgn-mandt = sy-mandt. lt_komfkgn-auart = p_auart. lt_komfkgn-fkara = P_fkart. lt_komfkgn-vkorg = p_vkorg. lt_komfkgn-vtweg = p_vtweg. lt_komfkgn-spart = p_spart. lt_komfkgn-vgbel = sy-uzeit. lt_komfkgn-fkdat = p_prsdt. lt_komfkgn-kunag lt_komfkgn-kunwe lt_komfkgn-pstyv = it_customer-kunag. = it_customer-kunag. = p_PSTYV.

lt_komfkgn-kwmeng lt_komfkgn-vrkme lt_komfkgn-werks lt_komfkgn-matnr APPEND lt_komfkgn. CLEAR lt_komfkgn.

= = = =

1. 'CS'. it_customer-vwerk. it_material-matnr.

CALL FUNCTION 'GN_INVOICE_CREATE' EXPORTING invoice_date = p_prsdt invoice_type = P_fkart pricing_date = p_prsdt vbsk_i = ls_vbsk id_kvorg = '08' I_WITHOUT_REFRESH = 'X' id_no_dialog = 'X' IMPORTING vbsk_e = ls_vbsk TABLES xkomfk = lt_komfk xkomfkgn = lt_komfkgn xkomfkko = lt_komfkko xkomv = lt_komv xthead = lt_thead xvbfs = lt_vbfs xvbpa = lt_vbpa xvbrk = lt_vbrk xvbrp = lt_vbrp xvbss = lt_vbss.

READ TABLE lt_vbrk INDEX 1. IF sy-subrc EQ 0. * get condition values write: 'LT_KOMV Table data in output...'. write: sy-uline. write: / sy-vline,'Material Number', sy-vline,'Document condition no.',sy-vline, 'Item Number',sy-vline, 'Condition type',sy-vline,'Condition base value',sy-vline,'Rate',sy-vline,'Condition Value '. write: sy-uline. loop at lt_komv . Write: / sy-vline,it_material-matnr ,sy-vline, lt_komv-knumv, syvline,lt_komv-Kposn , sy-vline,lt_komv-Kschl,sy-vline,lt_komv-Kawrt,syvline,lt_komv-kbetr,sy-vline,lt_komv-KWERT. endloop. ELSE. LOOP AT lt_vbfs. CALL FUNCTION 'MESSAGE_TEXT_BUILD' EXPORTING

msgid msgnr msgv1 msgv2 msgv3 msgv4 IMPORTING message_text_output APPEND it_error. CLEAR it_error. ENDLOOP. ENDIF.

= = = = = =

lt_vbfs-msgid lt_vbfs-msgno lt_vbfs-msgv1 lt_vbfs-msgv2 lt_vbfs-msgv3 lt_vbfs-msgv4

= it_error-message.

REFRESH: lt_komfk, lt_komfkgn, lt_komfkko, lt_komv, lt_thead, lt_vbfs, lt_vbpa, lt_vbrk, lt_vbrp, lt_vbss. CLEAR: lt_komfk, lt_komfkgn, lt_komfkko, lt_komv, lt_thead, lt_vbfs, lt_vbpa, lt_vbrk, lt_vbrp, lt_vbss, ls_vbsk.

ENDLOOP. ENDLOOP. elseif s_vgbel[] is not initial. loop at s_vgbel. * populate table with material for price simulation lt_komfkgn-mandt = sy-mandt. lt_komfkgn-auart = p_auart. lt_komfkgn-fkara = P_fkart. lt_komfkgn-vgtyp = p_PSTYV. lt_komfkgn-vgbel = s_vgbel-low. APPEND lt_komfkgn. CLEAR lt_komfkgn. CALL FUNCTION 'GN_INVOICE_CREATE' EXPORTING invoice_date = p_prsdt invoice_type = P_fkart pricing_date = p_prsdt vbsk_i = ls_vbsk id_kvorg = '08' I_WITHOUT_REFRESH = 'X' id_no_dialog = 'X' IMPORTING vbsk_e = ls_vbsk TABLES xkomfk = lt_komfk xkomfkgn = lt_komfkgn xkomfkko = lt_komfkko xkomv = lt_komv

xthead xvbfs xvbpa xvbrk xvbrp xvbss

= = = = = =

lt_thead lt_vbfs lt_vbpa lt_vbrk lt_vbrp lt_vbss.

READ TABLE lt_vbrk INDEX 1. IF sy-subrc EQ 0. * get condition values write: 'LT_KOMV Table data in output...'. write: sy-uline. write: / sy-vline,'Delivery Number', sy-vline,'Document condition no.',sy-vline, 'Item Number',sy-vline, 'Condition type',sy-vline,'Condition base value',sy-vline,'Rate',sy-vline,'Condition Value '. write: sy-uline. loop at lt_komv . Write: / sy-vline,s_vgbel-low ,sy-vline, lt_komv-knumv, syvline,lt_komv-Kposn , sy-vline,lt_komv-Kschl,sy-vline,lt_komv-Kawrt,syvline,lt_komv-kbetr,sy-vline,lt_komv-KWERT. endloop. ELSE. LOOP AT lt_vbfs. CALL FUNCTION 'MESSAGE_TEXT_BUILD' EXPORTING msgid = lt_vbfs-msgid msgnr = lt_vbfs-msgno msgv1 = lt_vbfs-msgv1 msgv2 = lt_vbfs-msgv2 msgv3 = lt_vbfs-msgv3 msgv4 = lt_vbfs-msgv4 IMPORTING message_text_output = it_error-message. APPEND it_error. CLEAR it_error. ENDLOOP. ENDIF. REFRESH: lt_komfk, lt_komfkgn, lt_komfkko, lt_komv, lt_thead, lt_vbfs, lt_vbpa, lt_vbrk, lt_vbrp, lt_vbss. CLEAR: lt_komfk, lt_komfkgn, lt_komfkko, lt_komv, lt_thead, lt_vbfs, lt_vbpa, lt_vbrk, lt_vbrp, lt_vbss, ls_vbsk.

endloop. elseif s_vbeln[] is not initial. perform basedate_create2. else.

select * from vbrk into table sel_vbrk_tab where vbeln in s_vbels. * describe table sel_vbrk_tab lines sel_tabix. clear : loc_vbrk_tab, loc_vbrp_tab. refresh : loc_vbrk_tab, loc_vbrp_tab. if sel_tabix ne 0. loop at sel_vbrk_tab. clear iv_relevant. select * from vbrp into lt_vbrp where vbeln eq sel_vbrk_tab-vbeln. select single * from t001w into lt_t001w where werks eq lt_vbrp-werks. select single * from t001k into lt_t001k where bwkey eq lt_t001w-bwkey. if lt_t001k-bukrs ne sel_vbrk_tab-bukrs. clear rc_ok. perform check_iv_faktura_exist changing rc_ok. if rc_ok is initial. * prepare material number if not lt_vbrp-matnr is initial. call function 'CONVERSION_EXIT_MATN1_OUTPUT' EXPORTING input = lt_vbrp-matnr IMPORTING output = lt_vbrp-matnr. endif. move-corresponding lt_vbrp to loc_vbrp_tab. append loc_vbrp_tab. iv_relevant = con_x. else. retcode = 3. endif. else. retcode = 2. endif. endselect. if iv_relevant eq con_x. move-corresponding sel_vbrk_tab to loc_vbrk_tab. append loc_vbrk_tab. endif. endloop. endif. describe table loc_vbrk_tab lines sel_tabix.

if sel_tabix ne 0. clear retcode. perform basedate_create. else.

endif. endif. ENDFORM. " simulate_invoice *&---------------------------------------------------------------------* *& Form WRITE_ERRORS *&---------------------------------------------------------------------* FORM WRITE_ERRORS . LOOP AT it_error. WRITE: / it_error-message. ENDLOOP. ENDFORM. " WRITE_ERRORS *&---------------------------------------------------------------------* *& Form CHECK_IV_FAKTURA_EXIST *&---------------------------------------------------------------------* * text *----------------------------------------------------------------------* * <--P_RC_OK text *----------------------------------------------------------------------* FORM CHECK_IV_FAKTURA_EXIST CHANGING P_RC_OK. ENDFORM. " CHECK_IV_FAKTURA_EXIST *&---------------------------------------------------------------------* *& Form BASEDATE_CREATE *&---------------------------------------------------------------------* * text *----------------------------------------------------------------------* * --> p1 text * <-- p2 text *----------------------------------------------------------------------* FORM BASEDATE_CREATE . loop at loc_vbrk_tab. clear : xkomfkgn, xkomfkko. refresh : xkomfkgn, xkomfkko. if loc_vbrk_tab-vbtyp ca 'MNOPSU'. xkomfkgn-fkara = p_fkart. loop at loc_vbrp_tab where vbeln eq loc_vbrk_tab-vbeln.

perform iv_xkomfkgn_fill.

perform iv_xkomfkko_fill. endloop. perform gn_invoice_create. endif. endloop.

ENDFORM. " BASEDATE_CREATE *&---------------------------------------------------------------------* *& Form IV_XKOMFKGN_FILL *&---------------------------------------------------------------------* * text *----------------------------------------------------------------------* * --> p1 text * <-- p2 text *----------------------------------------------------------------------* FORM IV_XKOMFKGN_FILL . data: lt_t001w like t001w occurs 0 with header line, lt_vbap like vbap occurs 0 with header line. xkomfkgn-waerk = loc_vbrk_tab-waerk. xkomfkgn-mandt = sy-mandt. select single * from vbak where vbeln eq loc_vbrp_tab-aubel. check sy-subrc eq 0 and loc_vbrp_tab-aubel in s_vbeln[]. xkomfkgn-auart = vbak-auart. xkomfkgn-werks = loc_vbrp_tab-werks. select single * from t001w into lt_t001w where werks eq loc_vbrp_tab-werks. if p_vkorg is not initial. xkomfkgn-vkorg = p_vkorg. else. xkomfkgn-vkorg = lt_t001w-vkorg. endif. if p_vtweg is not initial. xkomfkgn-vtweg = p_vtweg. else. xkomfkgn-vtweg = lt_t001w-vtweg. endif. if p_spart is not initial. xkomfkgn-spart = p_spart. else. xkomfkgn-spart = lt_t001w-spart. endif. xkomfkgn-kunag = vbak-kunnr. xkomfkgn-vgbel = loc_vbrp_tab-vbeln. xkomfkgn-vgpos = loc_vbrp_tab-posnr. xkomfkgn-vgueb = loc_vbrp_tab-uepos.

xkomfkgn-kwmeng = loc_vbrp_tab-fkimg. xkomfkgn-vrkme = loc_vbrp_tab-vrkme. xkomfkgn-aubel = loc_vbrp_tab-aubel. xkomfkgn-aupos = loc_vbrp_tab-aupos. xkomfkgn-xblnr = loc_vbrk_tab-xblnr. xkomfkgn-matnr = loc_vbrp_tab-matnr. xkomfkgn-shkzg = loc_vbrp_tab-shkzg. select single * from vbap into lt_vbap where vbeln eq loc_vbrp_tab-aubel and posnr eq loc_vbrp_tab-aupos. check sy-subrc eq 0. xkomfkgn-pstyv = lt_vbap-pstyv. xkomfkgn-vbelv = lt_vbap-vbelv. xkomfkgn-posnv = lt_vbap-posnv. if not xkomfkgn-vbelv is initial. xkomfkgn-kzvbr = 'E'. endif. xkomfkgn-aufnr = lt_vbap-aufnr. append xkomfkgn. ENDFORM. " IV_XKOMFKGN_FILL *&---------------------------------------------------------------------* *& Form IV_XKOMFKKO_FILL *&---------------------------------------------------------------------* * text *----------------------------------------------------------------------* * --> p1 text * <-- p2 text *----------------------------------------------------------------------* FORM IV_XKOMFKKO_FILL . data:lt_konv like konv occurs 0 with header line. select * from konv into lt_konv where knumv eq loc_vbrk_tab-knumv and kposn eq loc_vbrp_tab-posnr. xkomfkko = lt_konv. xkomfkko-mandt = xkomfkgn-mandt. xkomfkko-knumv = xkomfkgn-vgbel. xkomfkko-kposn = xkomfkgn-vgpos. clear xkomfkko-kkurs. if xkomfkko-ksteu = 'E'. xkomfkko-kbetr = lt_konv-kwert. if loc_vbrk_tab-vbtyp ca vbtyp_retour. if loc_vbrp_tab-shkzg ca ' A'. xkomfkko-kbetr = xkomfkko-kbetr * -1. endif. else. if loc_vbrp_tab-shkzg ca 'BX'.

xkomfkko-kbetr = xkomfkko-kbetr * -1. endif. endif. xkomfkko-waers = loc_vbrk_tab-waerk. endif. if xkomfkko-kherk = 'G'. xkomfkko-kherk = 'C'. endif. append xkomfkko. endselect. ENDFORM. " IV_XKOMFKKO_FILL *&---------------------------------------------------------------------* *& Form GN_INVOICE_CREATE *&---------------------------------------------------------------------* * text *----------------------------------------------------------------------* * --> p1 text * <-- p2 text *----------------------------------------------------------------------* FORM GN_INVOICE_CREATE . DATA: ls_vbsk TYPE vbsk, lt_komfk TYPE komfk OCCURS 0 WITH HEADER LINE, lt_komfkgn TYPE komfkgn OCCURS 0 WITH HEADER LINE, lt_komfkko TYPE komv OCCURS 0 WITH HEADER LINE, lt_komv TYPE komv OCCURS 0 WITH HEADER LINE, lt_thead TYPE theadvb OCCURS 0 WITH HEADER LINE, lt_vbfs TYPE vbfs OCCURS 0 WITH HEADER LINE, lt_vbpa TYPE vbpavb OCCURS 0 WITH HEADER LINE, lt_vbrk TYPE vbrkvb OCCURS 0 WITH HEADER LINE, lt_vbrp TYPE vbrpvb OCCURS 0 WITH HEADER LINE, lt_t001k type t001k occurs 0 with header line, lt_t001w type t001w occurs 0 with header line, lt_vbss TYPE vbss OCCURS 0 WITH HEADER LINE. clear : xkomfk, xkomv, xthead, xvbfs, xvbpa, xvbrk, xvbrp, xvbss. refresh : xkomfk, xkomv, xthead, xvbfs, xvbpa, xvbrk, xvbrp, xvbss.

describe table xkomfkgn lines sel_tabix. check not sel_tabix is initial. call function 'GN_INVOICE_CREATE' exporting invoice_date = p_prsdt invoice_type = P_fkart pricing_date = p_prsdt vbsk_i = vbsk I_WITHOUT_REFRESH = 'X' id_no_dialog = 'X'

importing vbsk_e = vbsk tables xkomfk = xkomfk xkomfkgn = xkomfkgn xkomfkko = xkomfkko xkomv = lt_komv xthead = xthead xvbfs = lt_vbfs xvbpa = xvbpa xvbrk = xvbrk xvbrp = xvbrp xvbss = xvbss. if sy-subrc eq 0. READ TABLE xvbrk INDEX 1. IF sy-subrc EQ 0. * get condition values write: 'LT_KOMV Table data in output...'. write: sy-uline. write: / sy-vline,'Billing Number', sy-vline,'Document condition no.',sy-vline, 'Item Number',sy-vline, 'Condition type',sy-vline,'Condition base value',sy-vline,'Rate',sy-vline,'Condition Value '. write: sy-uline. loop at lt_komv . Write: / sy-vline,xvbrk-vbeln ,sy-vline, lt_komv-knumv, syvline,lt_komv-Kposn , sy-vline,lt_komv-Kschl,sy-vline,lt_komv-Kawrt,syvline,lt_komv-kbetr,sy-vline,lt_komv-KWERT. endloop. ELSE. LOOP AT lt_vbfs. CALL FUNCTION 'MESSAGE_TEXT_BUILD' EXPORTING msgid = lt_vbfs-msgid msgnr = lt_vbfs-msgno msgv1 = lt_vbfs-msgv1 msgv2 = lt_vbfs-msgv2 msgv3 = lt_vbfs-msgv3 msgv4 = lt_vbfs-msgv4 IMPORTING message_text_output = it_error-message. APPEND it_error. CLEAR it_error. ENDLOOP. ENDIF. endif. ENDFORM. " GN_INVOICE_CREATE *&---------------------------------------------------------------------*

*& Form BASEDATE_CREATE2 *&---------------------------------------------------------------------* FORM BASEDATE_CREATE2 . data: lt_t001w like t001w occurs 0 with header line, lt_vbap like vbap occurs 0 with header line. xkomfkgn-vgpos = 0. xkomfkgn-mandt = sy-mandt. select single * from vbak where vbeln in s_vbeln. if sy-subrc is initial. select single * from vbap into lt_vbap where vbeln eq vbak-vbeln. * check sy-subrc eq 0 and loc_vbrp_tab-aubel in s_vbeln[]. xkomfkgn-fkara = p_fkart. xkomfkgn-auart = vbak-auart. xkomfkgn-vgtyp = lt_vbap-PSTYV. xkomfkgn-werks = lt_vbap-werks. select single * from t001w into lt_t001w where werks eq lt_vbap-werks. if p_vkorg is not initial. xkomfkgn-vkorg = p_vkorg. else. xkomfkgn-vkorg = lt_t001w-vkorg. endif. if p_vtweg is not initial. xkomfkgn-vtweg = p_vtweg. else. xkomfkgn-vtweg = lt_t001w-vtweg. endif. if p_spart is not initial. xkomfkgn-spart = p_spart. else. xkomfkgn-spart = lt_t001w-spart. endif. xkomfkgn-kunag = vbak-kunnr. xkomfkgn-waerk = lt_vbap-waerk. xkomfkgn-kwmeng = lt_vbap-zmeng. xkomfkgn-vrkme = lt_vbap-vrkme. xkomfkgn-aubel = lt_vbap-vbeln. xkomfkgn-aupos = lt_vbap-posnr. xkomfkgn-matnr = lt_vbap-matnr. xkomfkgn-vgpos = xkomfkgn-vgpos + 1. append xkomfkgn. endif. perform gn_invoice_create. ENDFORM.

You might also like