SAP R/3 форум ABAP консультантов
Russian ABAP Developer's Club

Home - FAQ - Search - Memberlist - Usergroups - Profile - Log in to check your private messages - Register - Log in - English
Blogs - Weblogs News

Automatic generation of credit notes



 
Post new topic   Reply to topic    Russian ABAP Developer's Club Forum Index -> FI
View previous topic :: View next topic  
Author Message
admin
Администратор
Администратор



Joined: 01 Sep 2007
Posts: 1640

PostPosted: Sat Oct 27, 2007 4:04 pm    Post subject: Automatic generation of credit notes Reply with quote

I am here by sending the full code of my program. But since my program is automatic generation of credit notes, there is no file format, instead processing logic is based on data stored from the FI tables. Otherwise yours is a very similar requirement to mine. Only diffrent is I use BDC's for F-43 and F-03 together in my program.

Code:
REPORT ZCREDITNOTE_GEN.

*********************************************************
*********************************************************
***Module - FI Finance & Accounting
***program - ZCREDITNOTE_GEN
***Author - Lakshminarayanan
***Date - 30.03.2006
***Request No - D62K909833
***Specification given by- Girish
*******************************************************
*******************************************************

*****************************************************
***Table Decleration
*******************************************************

TABLES: KNA1, VBRK.

*********************************************************
***Variable Decleration
**********************************************************

DATA: W_MONTH LIKE BOITEM-SPMON,
W_PERIOD(2),
G_KBETR TYPE KONV-KBETR,
DD(2) TYPE C,
MM(2) TYPE C,
YY(4) TYPE C,
L_DATE TYPE DATE,
BDC_PAR LIKE CTU_PARAMS,
W_KWERT(20),
W_TAX(20),
W_TOTAL(20),
W_DIFFRENCE(20),
FLDNAME(30),
W_NUM(2),
W_CRNO TYPE STRING,
W_FILTER(4),
W_FILTER1(4),
W_FILTER2(4).

************************************************************************
***Constants Decleration
************************************************************************

CONSTANTS: C_CASE(4) VALUE 'FULL',
C_CASE1(4) VALUE 'NOAC',
C_CASE2(4) VALUE 'PAPY'.

************************************************************************
***Internal Table Decleration
************************************************************************

DATA: BEGIN OF WA_INVOICES,
VBELN TYPE VBRK-VBELN, " Invoice No
VKORG TYPE VBRK-VKORG, " Sales Organisation
KUNRG TYPE VBRK-KUNRG, " Payer
KNUMV TYPE VBRK-KNUMV, " Condition Record No
FKIMG TYPE P, " Invoice Qty
VTWEG TYPE VBRK-VTWEG, " Distribution Channel
SPART TYPE VBRK-SPART, " Division
FKDAT TYPE VBRK-FKDAT, " Invoice Date
POSNR TYPE VBRP-POSNR, " Item No
MATNR TYPE VBRP-MATNR, " Material No
PRDHA(7), " Product Hierarchy
UMREN TYPE MARM-UMREN,
UMREZ TYPE MARM-UMREZ,
BOX TYPE P DECIMALS 2,
KSCHL TYPE KONV-KSCHL, " Condition Type
KWERT TYPE KONV-KWERT, " Condition Value
END OF WA_INVOICES.

DATA: BEGIN OF WA_INV,
VKORG TYPE VBRK-VKORG, " Sales Organisation
KUNRG TYPE VBRK-KUNRG, " Payer
FKIMG TYPE P, " Invoice Qty
VTWEG TYPE VBRK-VTWEG, " Distribution Channel
SPART TYPE VBRK-SPART, " Division
KSCHL TYPE KONV-KSCHL,
KWERT TYPE KONV-KWERT,
BOX TYPE P DECIMALS 2,
VKBUR TYPE KNVV-VKBUR,
KDGRP TYPE KNVV-KDGRP,
ZTGTQTY TYPE P,
ZMINQTY TYPE P,
ZTGTRATE TYPE KONP-KBETR,
SALETAX TYPE KONV-KWERT, " Sales Tax
TOTAL TYPE KONV-KWERT, " Total Amount
END OF WA_INV.

DATA: BEGIN OF WA_ACCOUNTING,
KUNNR TYPE BSID-KUNNR, " Customer Code
BELNR TYPE BSID-BELNR, " Accounting Document
VBELN TYPE BSID-VBELN, " Invoice No
END OF WA_ACCOUNTING.

DATA: BEGIN OF WA_VAT,
KBETR TYPE KONV-KBETR, " Condition Type
END OF WA_VAT.

DATA: BEGIN OF WA_CRN,
CRN_NO TYPE BSID-BELNR, " Credit Note No
KUNNR TYPE KNA1-KUNNR, " Customer Code
CRN_AMT TYPE ZCRN-CRN_AMT,
CREDIT TYPE ZCRN-CRN_AMT,
DIFFRENCE TYPE ZCRN-CRN_AMT,
END OF WA_CRN.

DATA: BEGIN OF WA_UPDATE,
HKONT TYPE BSIS-HKONT,
ZUONR TYPE BSIS-ZUONR,
BELNR TYPE BSIS-BELNR,
KUNNR TYPE VBRK-KUNRG,
VBELN TYPE VBRK-VBELN,
END OF WA_UPDATE.

DATA: BEGIN OF WA_ACCOUNT,
BELNR TYPE BSID-BELNR,
KUNNR TYPE BSID-KUNNR,
KWERT TYPE KONV-KWERT,
END OF WA_ACCOUNT.

DATA: BEGIN OF WA_MARA,
MATNR TYPE MARA-MATNR,
PRDHA TYPE MARA-PRDHA,
END OF WA_MARA.

DATA: BEGIN OF WA_MARM,
MATNR TYPE MARM-MATNR,
UMREN TYPE MARM-UMREN,
UMREZ TYPE MARM-UMREZ,
END OF WA_MARM.

DATA: BEGIN OF WA_VALUE,
VBELN TYPE VBRK-VBELN,
BUZEI TYPE BUZEI,
VKORG LIKE VBAK-VKORG,
KUNNR TYPE KNA1-KUNNR,
VTWEG TYPE VBRK-VTWEG,
SPART TYPE VBAK-SPART,
MONTH TYPE BOITEM-SPMON,
KSCHL TYPE KONP-KSCHL,
BOX TYPE P DECIMALS 2,
VKBUR TYPE KNVV-VKBUR,
RATE TYPE KONP-KBETR,
KWERT TYPE KONV-KWERT,
END OF WA_VALUE.

DATA: T_INVOICES LIKE STANDARD TABLE OF WA_INVOICES WITH HEADER LINE,
T_MATERIAL LIKE STANDARD TABLE OF WA_MARA WITH HEADER LINE,
T_MARM LIKE STANDARD TABLE OF WA_MARM WITH HEADER LINE,
T_INV LIKE STANDARD TABLE OF WA_INV WITH HEADER LINE,
T_SUMMINV LIKE STANDARD TABLE OF WA_INV WITH HEADER LINE,
T_SUMMINV1 LIKE STANDARD TABLE OF WA_INV WITH HEADER LINE,
T_SUMMINV2 LIKE STANDARD TABLE OF WA_INV WITH HEADER LINE,
T_ACCOUNTING LIKE STANDARD TABLE OF WA_ACCOUNTING WITH HEADER LINE,
T_TARGET LIKE STANDARD TABLE OF ZTARGET WITH HEADER LINE,
T_VAT LIKE STANDARD TABLE OF WA_VAT WITH HEADER LINE,
T_CRN LIKE STANDARD TABLE OF WA_CRN WITH HEADER LINE,
BDCDATA LIKE BDCDATA OCCURS 0 WITH HEADER LINE,
T_UPDATE LIKE STANDARD TABLE OF WA_UPDATE WITH HEADER LINE,
T_NEW LIKE STANDARD TABLE OF ZCRN WITH HEADER LINE,
T_CRNO LIKE STANDARD TABLE OF WA_CRN WITH HEADER LINE,
T_HEADER LIKE STANDARD TABLE OF WA_CRN WITH HEADER LINE,
T_HEADER1 LIKE STANDARD TABLE OF WA_CRN WITH HEADER LINE,
T_HEADER2 LIKE STANDARD TABLE OF WA_ACCOUNT WITH HEADER LINE,
T_HEADER3 LIKE STANDARD TABLE OF WA_ACCOUNT WITH HEADER LINE,
T_ACCOUNT LIKE STANDARD TABLE OF WA_ACCOUNT WITH HEADER LINE,
T_ACC LIKE STANDARD TABLE OF WA_ACCOUNT WITH HEADER LINE,
T_VALUE LIKE STANDARD TABLE OF WA_VALUE WITH HEADER LINE,
MESSTAB LIKE BDCMSGCOLL OCCURS 0 WITH HEADER LINE.

************************************************************************
***Selection-Screen layout
************************************************************************

SELECTION-SCREEN BEGIN OF BLOCK IPDATA WITH FRAME TITLE TEXT-001.
PARAMETERS: P_BUKRS LIKE T001-BUKRS OBLIGATORY DEFAULT '1000',
P_HKONT LIKE SKB1-SAKNR.

SELECT-OPTIONS: SO_KUNNR FOR KNA1-KUNNR,
SO_FKDAT FOR VBRK-FKDAT.
SELECTION-SCREEN END OF BLOCK IPDATA.

************************************************************************
***Selection Events
************************************************************************

START-OF-SELECTION.

***To get the Month from the Invoice date entered in the selection
***screen. Once after getting the month it is passed on to the ZTARGET
***table for comparison of the target qty

W_MONTH = SY-DATUM+0(6).
W_PERIOD = SY-DATUM+4(2).

PERFORM F001_GETDATA.

IF SY-DBCNT EQ 0.
MESSAGE S003(ZRAV).
EXIT.
ENDIF.

IF P_HKONT = '0000220043'.
PERFORM F002_ADDITIONALDATA.
PERFORM F003_MOVEDATA.
PERFORM F004_TARGETCOMPAREA.
PERFORM F005_CHECKINGA.
IF W_FILTER = C_CASE.
PERFORM F006_UPLOAD.
PERFORM F007_READCRNO.
PERFORM F008_CLEARING.
ENDIF.
IF W_FILTER1 = C_CASE1.
PERFORM F008_CLEARINGC.
ENDIF.
ELSEIF P_HKONT = '0000220044'.
PERFORM F002_ADDITIONALDATA.
PERFORM F003_MOVEDATA.
PERFORM F004_TARGETCOMPARE.
PERFORM F005_CHECKING.
IF W_FILTER = C_CASE.
PERFORM F006_UPLOAD.
PERFORM F007_READCRNO.
PERFORM F008_CLEARING.
ENDIF.
IF W_FILTER2 = C_CASE2.
PERFORM F006_UPLOADA.
PERFORM F007_READCRNOA.
PERFORM F008_CLEARINGB.
ENDIF.
IF W_FILTER1 = C_CASE1.
PERFORM F008_CLEARINGC.
ENDIF.
ENDIF.

*&---------------------------------------------------------------------*
*& Form F001_GETDATA
*&---------------------------------------------------------------------*
*& This Form is basically used for getting the list of invoices
*& from VBRK table based on the Date and customer given in the
*& selection screen
*----------------------------------------------------------------------*
FORM F001_GETDATA.


SELECT A~VBELN A~VKORG A~KUNRG KNUMV FKIMG VTWEG A~SPART A~FKDAT
B~POSNR B~MATNR
FROM VBRK AS A INNER JOIN VBRP AS B ON A~VBELN EQ B~VBELN
INTO TABLE T_INVOICES
WHERE KUNRG IN SO_KUNNR
AND FKDAT IN SO_FKDAT
AND FKSTO NE 'X'
AND FKART NOT IN ('S1','S2','S3')
AND RFBSK NE 'E'.

*To retrieve the Product hierarchy from the material master.
IF NOT T_INVOICES[] IS INITIAL.
SELECT MATNR PRDHA
INTO TABLE T_MATERIAL
FROM MARA
FOR ALL ENTRIES IN T_INVOICES
WHERE MATNR = T_INVOICES-MATNR.
ENDIF.

IF NOT T_MATERIAL[] IS INITIAL.
**To retrieve the conversion factors for the materials
SELECT MATNR UMREN UMREZ
INTO TABLE T_MARM
FROM MARM
FOR ALL ENTRIES IN T_MATERIAL
WHERE MATNR = T_MATERIAL-MATNR
AND MEINH = 'TR'.

ENDIF.

LOOP AT T_INVOICES.
READ TABLE T_MATERIAL WITH KEY MATNR = T_INVOICES-MATNR.
IF SY-SUBRC EQ 0.
T_INVOICES-PRDHA = T_MATERIAL-PRDHA.
ENDIF.

READ TABLE T_MARM WITH KEY MATNR = T_MATERIAL-MATNR.
IF SY-SUBRC EQ 0.
T_INVOICES-UMREN = T_MARM-UMREN.
T_INVOICES-UMREZ = T_MARM-UMREZ.
ENDIF.

CASE T_INVOICES-PRDHA.
WHEN '0010001'.
T_INVOICES-BOX = T_INVOICES-FKIMG.
WHEN '0010002'.
T_INVOICES-BOX = ( T_INVOICES-UMREN / T_INVOICES-UMREZ ) *
T_INVOICES-FKIMG.
WHEN '0010003'.
T_INVOICES-BOX = ( T_INVOICES-UMREN / T_INVOICES-UMREZ ) *
T_INVOICES-FKIMG.
ENDCASE.

MODIFY T_INVOICES TRANSPORTING PRDHA UMREN UMREZ BOX.
CLEAR: T_INVOICES, T_MATERIAL, T_MARM.
ENDLOOP.

*This select is to check that only RV documents are eligible for credit
*notes generation

SELECT KUNNR BELNR VBELN FROM
BSID
INTO TABLE T_ACCOUNTING
FOR ALL ENTRIES IN T_INVOICES
WHERE VBELN = T_INVOICES-VBELN
AND KUNNR = T_INVOICES-KUNRG
AND BLART = 'RV'.

LOOP AT T_INVOICES.
READ TABLE T_ACCOUNTING WITH KEY KUNNR = T_INVOICES-KUNRG
VBELN = T_INVOICES-VBELN.

IF SY-SUBRC <> 0.
DELETE T_INVOICES.
CLEAR: T_INVOICES, T_ACCOUNTING.
ENDIF.
ENDLOOP.

ENDFORM. " F001_GETDATA

*&---------------------------------------------------------------------*
*& Form F002_ADDITIONALDATA
*&---------------------------------------------------------------------*
*& Based on the invoices from the above internal table we need to
*& retrieve the Condition value for both ZMSR/ZQTR condition type
*----------------------------------------------------------------------*
FORM F002_ADDITIONALDATA.


IF NOT T_INVOICES[] IS INITIAL.

LOOP AT T_INVOICES.
IF P_HKONT = '0000220043'.

SELECT SINGLE KSCHL KWERT
FROM KONV
INTO (T_INVOICES-KSCHL,T_INVOICES-KWERT)
WHERE KNUMV = T_INVOICES-KNUMV
AND KPOSN = T_INVOICES-POSNR
AND KSCHL = 'ZMSR'
AND KAPPL = 'V'.

IF SY-SUBRC EQ 0.
MODIFY T_INVOICES TRANSPORTING KSCHL KWERT.
CLEAR T_INVOICES.
CONTINUE.
ENDIF.
ENDIF.

IF P_HKONT = '0000220044'.

SELECT SINGLE KSCHL KWERT
FROM KONV
INTO (T_INVOICES-KSCHL,T_INVOICES-KWERT)
WHERE KNUMV = T_INVOICES-KNUMV
AND KPOSN = T_INVOICES-POSNR
AND KSCHL = 'ZQTR'
AND KAPPL = 'V'.

IF SY-SUBRC EQ 0.
MODIFY T_INVOICES TRANSPORTING KSCHL KWERT.
CLEAR T_INVOICES.
CONTINUE.
ENDIF.
ENDIF.
ENDLOOP.

ENDIF.

SORT T_INVOICES BY KUNRG.

*This select is to check whether ZMSR or ZQTR condition type records are
*maintained in the ZTARGET table against the customer codes entered in
*the selection-screen. If the data is not maintained then those
*customers invoice details has to be deleted from the selection for
*further process.Done on 05.04.2006

SELECT * FROM ZTARGET
INTO TABLE T_TARGET
FOR ALL ENTRIES IN T_INVOICES
WHERE KUNNR IN SO_KUNNR
AND VTWEG EQ T_INVOICES-VTWEG
AND SPART EQ T_INVOICES-SPART
AND ZCONTYPE EQ T_INVOICES-KSCHL
AND ZFM_MTHYR EQ W_MONTH.

LOOP AT T_INVOICES.
READ TABLE T_TARGET WITH KEY KUNNR = T_INVOICES-KUNRG
VTWEG = T_INVOICES-VTWEG
SPART = T_INVOICES-SPART
ZCONTYPE = T_INVOICES-KSCHL ZFM_MTHYR = W_MONTH.

IF SY-SUBRC <> 0.
DELETE T_INVOICES.
CLEAR: T_INVOICES, T_TARGET.
ENDIF.
ENDLOOP.
*End of changes done by JLN on 05.04.2006

LOOP AT T_INVOICES.
T_INV-VKORG = T_INVOICES-VKORG.
T_INV-KUNRG = T_INVOICES-KUNRG.
T_INV-FKIMG = T_INVOICES-FKIMG.
T_INV-VTWEG = T_INVOICES-VTWEG.
T_INV-SPART = T_INVOICES-SPART.
T_INV-KSCHL = T_INVOICES-KSCHL.
T_INV-KWERT = T_INVOICES-KWERT.
T_INV-BOX = T_INVOICES-BOX.

APPEND T_INV.
CLEAR: T_INV, T_INVOICES.
ENDLOOP.

LOOP AT T_INV.
COLLECT T_INV INTO T_SUMMINV.
ENDLOOP.

ENDFORM. " F002_ADDITIONALDATA

*&---------------------------------------------------------------------*
*& Form F003_MOVEDATA
*&---------------------------------------------------------------------*
*& This Form is used for moving data into a new internal table
*----------------------------------------------------------------------*
FORM F003_MOVEDATA.

**To Bring in the Sales Office Field into the Base table.

LOOP AT T_SUMMINV.
SELECT SINGLE VKBUR KDGRP INTO (T_SUMMINV-VKBUR,T_SUMMINV-KDGRP)
FROM KNVV
WHERE KUNNR IN SO_KUNNR
AND VKORG EQ T_SUMMINV-VKORG
AND VTWEG EQ T_SUMMINV-VTWEG
AND SPART EQ T_SUMMINV-SPART.

IF SY-SUBRC EQ 0.
MODIFY T_SUMMINV TRANSPORTING VKBUR KDGRP.
CLEAR T_SUMMINV.
ENDIF.
ENDLOOP.

ENDFORM. " F003_MOVEDATA

*&---------------------------------------------------------------------*
*& Form F004_TARGETCOMPARE
*&---------------------------------------------------------------------*
*& This Form is for Comparing the Base internal table with the
*& ZTARGET table
*----------------------------------------------------------------------*
FORM F004_TARGETCOMPARE.

REFRESH T_TARGET.
CLEAR T_TARGET.

SELECT * FROM ZTARGET
INTO TABLE T_TARGET
FOR ALL ENTRIES IN T_SUMMINV
WHERE KUNNR IN SO_KUNNR
AND VTWEG EQ T_SUMMINV-VTWEG
AND SPART EQ T_SUMMINV-SPART
AND ZCONTYPE EQ T_SUMMINV-KSCHL
AND ZFM_MTHYR EQ W_MONTH.

LOOP AT T_SUMMINV.
READ TABLE T_TARGET WITH KEY KUNNR = T_SUMMINV-KUNRG
VTWEG = T_SUMMINV-VTWEG
SPART = T_SUMMINV-SPART VKBUR = T_SUMMINV-VKBUR
ZCONTYPE = T_SUMMINV-KSCHL ZFM_MTHYR = W_MONTH.

IF SY-SUBRC EQ 0.
T_SUMMINV-ZTGTQTY = T_TARGET-ZTGTQTY.
T_SUMMINV-ZMINQTY = T_TARGET-ZMINQTY.
T_SUMMINV-ZTGTRATE = T_TARGET-ZTGTRATE.

MODIFY T_SUMMINV TRANSPORTING ZTGTQTY ZMINQTY ZTGTRATE.
CLEAR T_SUMMINV.
ENDIF.
ENDLOOP.

ENDFORM. " F004_TARGETCOMPARE

*&---------------------------------------------------------------------*
*& Form F005_CHECKING
*&---------------------------------------------------------------------*
*& This form is basically decide which accruel will decide the
*& credit note generation
*----------------------------------------------------------------------*
FORM F005_CHECKING.

SELECT KBETR
INTO TABLE T_VAT
FROM KONV
FOR ALL ENTRIES IN T_INVOICES
WHERE KNUMV = T_INVOICES-KNUMV
AND KSCHL IN ('JIN6','JIN7')
AND KAPPL = 'TX'.

DELETE ADJACENT DUPLICATES FROM T_VAT COMPARING KBETR.

LOOP AT T_VAT.
G_KBETR = T_VAT-KBETR / 10.
ENDLOOP.

LOOP AT T_SUMMINV.

IF T_SUMMINV-BOX GE T_SUMMINV-ZTGTQTY.

T_SUMMINV-SALETAX = ( T_SUMMINV-KWERT * G_KBETR ) / 100.
T_SUMMINV-TOTAL = T_SUMMINV-KWERT + T_SUMMINV-SALETAX.

MODIFY T_SUMMINV TRANSPORTING SALETAX TOTAL.
CLEAR T_SUMMINV.
W_FILTER = C_CASE.
CONTINUE.
ELSEIF T_SUMMINV-BOX GE T_SUMMINV-ZMINQTY AND T_SUMMINV-BOX LT
T_SUMMINV-ZTGTQTY.

MOVE-CORRESPONDING T_SUMMINV TO T_SUMMINV2.

T_SUMMINV2-KWERT = 0.
T_SUMMINV2-KWERT = T_SUMMINV2-BOX * T_SUMMINV2-ZTGTRATE.

T_SUMMINV2-SALETAX = ( T_SUMMINV2-KWERT * G_KBETR ) / 100.
T_SUMMINV2-TOTAL = T_SUMMINV2-KWERT + T_SUMMINV2-SALETAX.

APPEND T_SUMMINV2.
DELETE T_SUMMINV.
CLEAR : T_SUMMINV, T_SUMMINV2.
W_FILTER2 = C_CASE2.
CONTINUE.
ELSEIF T_SUMMINV-BOX LT T_SUMMINV-ZMINQTY.

MOVE-CORRESPONDING T_SUMMINV TO T_SUMMINV1.

T_SUMMINV1-SALETAX = ( T_SUMMINV1-KWERT * G_KBETR ) / 100.
T_SUMMINV1-TOTAL = T_SUMMINV1-KWERT + T_SUMMINV1-SALETAX.

APPEND T_SUMMINV1.
DELETE T_SUMMINV.
CLEAR : T_SUMMINV, T_SUMMINV1.
W_FILTER1 = C_CASE1.
CONTINUE.
ENDIF.
ENDLOOP.

ENDFORM. " F005_CHECKING

*&---------------------------------------------------------------------*
*& Form F006_UPLOAD
*&---------------------------------------------------------------------*
*& This form is basically for uploading the data into the F-43
*& for generating Credit Note No.
*----------------------------------------------------------------------*
FORM F006_UPLOAD.

DD = SY-DATUM+6(2).
MM = SY-DATUM+4(2).
YY = SY-DATUM+0(4).

CONCATENATE DD MM YY INTO L_DATE.

LOOP AT T_SUMMINV.

WRITE T_SUMMINV-KWERT TO W_KWERT NO-SIGN CURRENCY 'INR'.
CONDENSE W_KWERT NO-GAPS.
WRITE T_SUMMINV-SALETAX TO W_TAX NO-SIGN CURRENCY 'INR'.
CONDENSE W_TAX NO-GAPS.
WRITE T_SUMMINV-TOTAL TO W_TOTAL NO-SIGN CURRENCY 'INR'.
CONDENSE W_TOTAL NO-GAPS.

PERFORM BDC_DYNPRO USING 'SAPMF05A' '0100'.
PERFORM BDC_FIELD USING 'BDC_CURSOR'
'RF05A-NEWKO'.
PERFORM BDC_FIELD USING 'BDC_OKCODE'
'/00'.

PERFORM BDC_FIELD USING 'BKPF-BLDAT' L_DATE.
PERFORM BDC_FIELD USING 'BKPF-BLART' 'DC'.
PERFORM BDC_FIELD USING 'BKPF-BUKRS' P_BUKRS.
PERFORM BDC_FIELD USING 'BKPF-BUDAT' L_DATE.
PERFORM BDC_FIELD USING 'BKPF-MONAT' W_PERIOD.
PERFORM BDC_FIELD USING 'BKPF-WAERS' 'INR'.
PERFORM BDC_FIELD USING 'RF05A-NEWBS' '40'.
PERFORM BDC_FIELD USING 'RF05A-NEWKO' P_HKONT.

PERFORM BDC_DYNPRO USING 'SAPMF05A' '0300'.
PERFORM BDC_FIELD USING 'BDC_CURSOR'
'RF05A-NEWKO'.
PERFORM BDC_FIELD USING 'BDC_OKCODE'
'/00'.

PERFORM BDC_FIELD USING 'BSEG-WRBTR' W_KWERT.
PERFORM BDC_FIELD USING 'BSEG-ZUONR' T_SUMMINV-KUNRG.
PERFORM BDC_FIELD USING 'RF05A-NEWBS' '40'.
PERFORM BDC_FIELD USING 'RF05A-NEWKO' '214116'.

PERFORM BDC_DYNPRO USING 'SAPLKACB' '0002'.
PERFORM BDC_FIELD USING 'BDC_OKCODE'
'/EENTE'.
PERFORM BDC_FIELD USING 'BDC_CURSOR'
'COBL-GSBER'.

PERFORM BDC_DYNPRO USING 'SAPMF05A' '0300'.
PERFORM BDC_FIELD USING 'BDC_CURSOR'
'RF05A-NEWKO'.
PERFORM BDC_FIELD USING 'BDC_OKCODE'
'/00'.

PERFORM BDC_FIELD USING 'BSEG-WRBTR' W_TAX.
PERFORM BDC_FIELD USING 'BSEG-ZUONR' T_SUMMINV-KUNRG.
PERFORM BDC_FIELD USING 'RF05A-NEWBS' '31'.
PERFORM BDC_FIELD USING 'RF05A-NEWKO' T_SUMMINV-KUNRG.

PERFORM BDC_DYNPRO USING 'SAPLKACB' '0002'.
PERFORM BDC_FIELD USING 'BDC_OKCODE'
'/EENTE'.
PERFORM BDC_FIELD USING 'BDC_CURSOR'
'COBL-GSBER'.

PERFORM BDC_DYNPRO USING 'SAPMF05A' '0302'.
PERFORM BDC_FIELD USING 'BDC_CURSOR'
'BSEG-ZUONR'.
PERFORM BDC_FIELD USING 'BDC_OKCODE'
'/00'.

PERFORM BDC_FIELD USING 'BSEG-WRBTR' W_TOTAL.
PERFORM BDC_FIELD USING 'BSEG-ZUONR' T_SUMMINV-KUNRG.

PERFORM BDC_DYNPRO USING 'SAPMF05A' '0302'.
PERFORM BDC_FIELD USING 'BDC_CURSOR'
'BSEG-WRBTR'.
PERFORM BDC_FIELD USING 'BDC_OKCODE'
'=BS'.

PERFORM BDC_DYNPRO USING 'SAPMF05A' '0700'.
PERFORM BDC_FIELD USING 'BDC_CURSOR'
'RF05A-NEWBS'.
PERFORM BDC_FIELD USING 'BDC_OKCODE'
'=BU'.
PERFORM BDC_TRANSACTION USING 'F-43'.

READ TABLE MESSTAB WITH KEY MSGID = 'F5' MSGNR = '312'.
IF SY-SUBRC EQ 0.

T_CRN-CRN_NO = MESSTAB-MSGV1.

APPEND T_CRN.
CLEAR T_CRN.
ENDIF.

ENDLOOP.

ENDFORM. " F006_UPLOAD

*&---------------------------------------------------------------------*
*& Form BDC_DYNPRO
*&---------------------------------------------------------------------*

FORM BDC_DYNPRO USING PROGRAM DYNPRO.

CLEAR BDCDATA.
BDCDATA-PROGRAM = PROGRAM.
BDCDATA-DYNPRO = DYNPRO.
BDCDATA-DYNBEGIN = 'X'.
APPEND BDCDATA.

ENDFORM. " BDC_DYNPRO

*&---------------------------------------------------------------------*
*& Form BDC_FIELD
*&---------------------------------------------------------------------*

FORM BDC_FIELD USING FNAM FVAL.
CLEAR BDCDATA.
BDCDATA-FNAM = FNAM.
BDCDATA-FVAL = FVAL.
APPEND BDCDATA.
ENDFORM. " BDC_FIELD

*&---------------------------------------------------------------------*
*& Form BDC_TRANSACTION
*&---------------------------------------------------------------------*

FORM BDC_TRANSACTION USING TCODE.

CLEAR BDC_PAR.

BDC_PAR-DISMODE = 'E'.
BDC_PAR-UPDMODE = 'S'.
BDC_PAR-CATTMODE = ' '.
BDC_PAR-DEFSIZE = 'X'.
BDC_PAR-RACOMMIT = 'X'.
BDC_PAR-NOBINPT = ' '.
BDC_PAR-NOBIEND = ' '.

REFRESH: MESSTAB.

CALL TRANSACTION TCODE USING BDCDATA
OPTIONS FROM BDC_PAR
MESSAGES INTO MESSTAB.

REFRESH: BDCDATA.

ENDFORM. " BDC_TRANSACTION

*----------------------------------------------------------------------*
* create batchinput session *
* (not for call transaction using...) *
*----------------------------------------------------------------------*
FORM OPEN_GROUP.
* open batchinput group
CALL FUNCTION 'BDC_OPEN_GROUP'
EXPORTING
CLIENT = SY-MANDT
GROUP = 'DLR_COMM'
USER = SY-UNAME
KEEP = 'X'
* HOLDDATE = HOLDDATE
EXCEPTIONS
CLIENT_INVALID
DESTINATION_INVALID
GROUP_INVALID
GROUP_IS_LOCKED
HOLDDATE_INVALID
INTERNAL_ERROR
QUEUE_ERROR
RUNNING
SYSTEM_LOCK_ERROR
USER_INVALID.

ENDFORM.

*----------------------------------------------------------------------*
* end batchinput session *
* (call transaction using...: error session) *
*----------------------------------------------------------------------*
FORM CLOSE_GROUP.
* close batchinput group
CALL FUNCTION 'BDC_CLOSE_GROUP'
EXCEPTIONS
NOT_OPEN
QUEUE_ERROR.

ENDFORM.

*&---------------------------------------------------------------------*
*& Form F007_READCRNO
*&---------------------------------------------------------------------*
*& This Form is basically used to get the Credit Notes created
*----------------------------------------------------------------------*
FORM F007_READCRNO.

LOOP AT T_CRN.

CALL FUNCTION 'CONVERSION_EXIT_ALPHA_INPUT'
EXPORTING
INPUT = T_CRN-CRN_NO
IMPORTING
OUTPUT = T_CRN-CRN_NO.

MODIFY T_CRN TRANSPORTING CRN_NO.
CLEAR T_CRN.
ENDLOOP.

SELECT HKONT ZUONR BELNR
FROM BSIS
INTO TABLE T_UPDATE
FOR ALL ENTRIES IN T_CRN
WHERE BELNR = T_CRN-CRN_NO
AND HKONT = P_HKONT
AND BUKRS = P_BUKRS.

LOOP AT T_UPDATE.
T_UPDATE-KUNNR = T_UPDATE-ZUONR+0(10).

MODIFY T_UPDATE TRANSPORTING KUNNR.
CLEAR T_UPDATE.
ENDLOOP.

LOOP AT T_UPDATE.
LOOP AT T_INVOICES WHERE KUNRG = T_UPDATE-KUNNR.
IF SY-SUBRC = 0.
T_NEW-MANDT = SY-MANDT.
T_NEW-CRN_NO = T_UPDATE-BELNR.
T_NEW-HKONT = P_HKONT.
T_NEW-VENDOR = T_INVOICES-KUNRG.
T_NEW-KUNNR = T_INVOICES-KUNRG.
T_NEW-INVOICE = T_INVOICES-VBELN.
T_NEW-POSNR = T_INVOICES-POSNR.
T_NEW-BUZEI = SY-TABIX.
T_NEW-CRN_AMT = T_INVOICES-KWERT.
T_NEW-INVDT = T_INVOICES-FKDAT.

APPEND T_NEW.
CLEAR : T_NEW, T_INVOICES.
ENDIF.
ENDLOOP.
CLEAR: T_UPDATE.
ENDLOOP.

LOOP AT T_NEW.
SELECT SINGLE BELNR FROM BSID INTO T_NEW-DOC_NO WHERE VBELN =
T_NEW-INVOICE AND KUNNR = T_NEW-KUNNR AND BUKRS = P_BUKRS.

SELECT SINGLE BLDAT FROM BKPF INTO T_NEW-CRN_DT
WHERE BUKRS = P_BUKRS AND BELNR = T_NEW-CRN_NO
AND GJAHR = SY-DATUM+0(4).

T_NEW-PROV_AMT = T_NEW-CRN_AMT.
T_NEW-AUGDT = T_NEW-CRN_DT.

SELECT SINGLE PRCTR FROM VBRP INTO T_NEW-PRCTR WHERE VBELN =
T_NEW-INVOICE.

IF SY-SUBRC EQ 0.
MODIFY T_NEW TRANSPORTING DOC_NO CRN_DT PROV_AMT AUGDT PRCTR.
CLEAR T_NEW.
ENDIF.
ENDLOOP.

LOOP AT T_NEW.
READ TABLE T_SUMMINV WITH KEY KUNRG = T_NEW-KUNNR.
IF SY-SUBRC EQ 0.
T_NEW-KDGRP = T_SUMMINV-KDGRP.

MODIFY T_NEW TRANSPORTING KDGRP.
CLEAR : T_NEW, T_SUMMINV.
ENDIF.
ENDLOOP.

LOOP AT T_NEW.
MODIFY ZCRN FROM T_NEW.
ENDLOOP.

ENDFORM. " F007_READCRNO

*&---------------------------------------------------------------------*
*& Form F008_CLEARING
*&---------------------------------------------------------------------*
*& This Form is used for clearing the documents through F-03
*----------------------------------------------------------------------*
FORM F008_CLEARING.

DATA: W_NO TYPE I.

CLEAR : DD,MM,YY,L_DATE.

DD = SY-DATUM+6(2).
MM = SY-DATUM+4(2).
YY = SY-DATUM+0(4).

CONCATENATE DD MM YY INTO L_DATE.

LOOP AT T_NEW.
MOVE : T_NEW-CRN_NO TO T_CRNO-CRN_NO.
MOVE : T_NEW-KUNNR TO T_CRNO-KUNNR.
MOVE : T_NEW-CRN_NO TO T_HEADER-CRN_NO.
MOVE : T_NEW-KUNNR TO T_HEADER-KUNNR.

APPEND : T_CRNO, T_HEADER.
CLEAR: T_CRNO, T_HEADER, T_NEW.
ENDLOOP.


SORT T_CRNO BY CRN_NO.
SORT T_HEADER BY CRN_NO.
DELETE ADJACENT DUPLICATES FROM T_CRNO COMPARING CRN_NO.
DELETE ADJACENT DUPLICATES FROM T_HEADER COMPARING CRN_NO.

LOOP AT T_CRNO.
LOOP AT T_NEW WHERE CRN_NO = T_CRNO-CRN_NO.
IF SY-SUBRC EQ 0.
T_CRNO-CRN_NO = T_NEW-DOC_NO.
T_CRNO-KUNNR = T_NEW-KUNNR.

APPEND T_CRNO.
CLEAR: T_CRNO, T_NEW.
ENDIF.
ENDLOOP.
CLEAR: T_CRNO.
ENDLOOP.

DESCRIBE TABLE T_CRNO LINES W_NO.
SORT T_CRNO BY KUNNR.

LOOP AT T_HEADER.
PERFORM BDC_DYNPRO USING 'SAPMF05A' '0131'.
PERFORM BDC_FIELD USING 'BDC_CURSOR' 'RF05A-XPOS1(03)'.
PERFORM BDC_FIELD USING 'BDC_OKCODE' '/00'.

PERFORM BDC_FIELD USING 'RF05A-AGKON' P_HKONT.
PERFORM BDC_FIELD USING 'BKPF-BUDAT' L_DATE.
PERFORM BDC_FIELD USING 'BKPF-MONAT' W_PERIOD.
PERFORM BDC_FIELD USING 'BKPF-BUKRS' P_BUKRS.
PERFORM BDC_FIELD USING 'BKPF-WAERS' 'INR'.

PERFORM BDC_FIELD USING 'RF05A-XPOS1(01)' ''.
PERFORM BDC_FIELD USING 'RF05A-XPOS1(03)' 'X'.
PERFORM BDC_DYNPRO USING 'SAPMF05A' '0731'.
PERFORM BDC_FIELD USING 'BDC_CURSOR' 'RF05A-SEL01(03)'.
PERFORM BDC_FIELD USING 'BDC_OKCODE' '/00'.

W_NUM = 1.
LOOP AT T_CRNO WHERE KUNNR = T_HEADER-KUNNR.

CONCATENATE 'RF05A-SEL01(' W_NUM ')' INTO W_CRNO.

PERFORM BDC_FIELD USING W_CRNO T_CRNO-CRN_NO.

W_NUM = W_NUM + 1.

ENDLOOP.

PERFORM BDC_DYNPRO USING 'SAPMF05A' '0731'.
PERFORM BDC_FIELD USING 'BDC_CURSOR' 'RF05A-SEL01(01)'.
PERFORM BDC_FIELD USING 'BDC_OKCODE' '=PA'.
PERFORM BDC_DYNPRO USING 'SAPDF05X' '3100'.
PERFORM BDC_FIELD USING 'BDC_OKCODE' '=BU'.
PERFORM BDC_FIELD USING 'BDC_CURSOR' 'RF05A-ABPOS'.
PERFORM BDC_TRANSACTION USING 'F-03'.

ENDLOOP.

ENDFORM. " F008_CLEARING


*&---------------------------------------------------------------------*
*& Form F008_CLEARINGB
*&---------------------------------------------------------------------*
*& This Form is used to RUN F-03 BDC if the credit note for the
*& customer if the sum of invoice qty is between Max and Min target
*&---------------------------------------------------------------------*
FORM F008_CLEARINGB.

DATA: W_NO TYPE I.

CLEAR : DD,MM,YY,L_DATE.

DD = SY-DATUM+6(2).
MM = SY-DATUM+4(2).
YY = SY-DATUM+0(4).

CONCATENATE DD MM YY INTO L_DATE.

REFRESH: T_CRNO, T_HEADER.
CLEAR : T_CRNO, T_HEADER, W_NUM.

LOOP AT T_NEW.
MOVE : T_NEW-CRN_NO TO T_CRNO-CRN_NO.
MOVE : T_NEW-KUNNR TO T_CRNO-KUNNR.

MOVE : T_NEW-CRN_NO TO T_HEADER-CRN_NO.
MOVE : T_NEW-KUNNR TO T_HEADER-KUNNR.
MOVE : T_NEW-CRN_AMT TO T_HEADER-CRN_AMT.

APPEND : T_CRNO, T_HEADER.
CLEAR: T_CRNO, T_HEADER, T_NEW.
ENDLOOP.


SORT T_CRNO BY CRN_NO.
SORT T_HEADER BY CRN_NO.

LOOP AT T_HEADER.
COLLECT T_HEADER INTO T_HEADER1.
ENDLOOP.

LOOP AT T_INVOICES.
READ TABLE T_HEADER1 WITH KEY KUNNR = T_INVOICES-KUNRG.
IF SY-SUBRC EQ 0.
T_HEADER2-KUNNR = T_INVOICES-KUNRG.
T_HEADER2-KWERT = T_INVOICES-KWERT.

APPEND T_HEADER2.
CLEAR : T_HEADER2, T_INVOICES.
ENDIF.
ENDLOOP.

LOOP AT T_HEADER2.
COLLECT T_HEADER2 INTO T_HEADER3.
ENDLOOP.

LOOP AT T_HEADER1.
READ TABLE T_HEADER3 WITH KEY KUNNR = T_HEADER1-KUNNR.

IF SY-SUBRC EQ 0.
T_HEADER1-CREDIT = T_HEADER3-KWERT.

MODIFY T_HEADER1 TRANSPORTING CREDIT.
CLEAR: T_HEADER1, T_HEADER3.
ENDIF.
ENDLOOP.

LOOP AT T_HEADER1.

T_HEADER1-DIFFRENCE = ( T_HEADER1-CREDIT * -1 ) - T_HEADER1-CRN_AMT.

IF SY-SUBRC EQ 0.
MODIFY T_HEADER1 TRANSPORTING DIFFRENCE.
CLEAR T_HEADER1.
ENDIF.
ENDLOOP.

DELETE ADJACENT DUPLICATES FROM T_CRNO COMPARING CRN_NO.

LOOP AT T_CRNO.
LOOP AT T_NEW WHERE CRN_NO = T_CRNO-CRN_NO.
IF SY-SUBRC EQ 0.
T_CRNO-CRN_NO = T_NEW-DOC_NO.
T_CRNO-KUNNR = T_NEW-KUNNR.

APPEND T_CRNO.
CLEAR: T_CRNO, T_NEW.
ENDIF.
ENDLOOP.
ENDLOOP.

DESCRIBE TABLE T_CRNO LINES W_NO.
SORT T_CRNO BY KUNNR.

LOOP AT T_HEADER1.

WRITE T_HEADER1-DIFFRENCE TO W_DIFFRENCE NO-SIGN CURRENCY 'INR'.
CONDENSE W_DIFFRENCE NO-GAPS.


PERFORM BDC_DYNPRO USING 'SAPMF05A' '0131'.
PERFORM BDC_FIELD USING 'BDC_CURSOR' 'RF05A-XPOS1(03)'.
PERFORM BDC_FIELD USING 'BDC_OKCODE' '/00'.

PERFORM BDC_FIELD USING 'RF05A-AGKON' P_HKONT.
PERFORM BDC_FIELD USING 'BKPF-BUDAT' L_DATE.
PERFORM BDC_FIELD USING 'BKPF-MONAT' W_PERIOD.
PERFORM BDC_FIELD USING 'BKPF-BUKRS' P_BUKRS.
PERFORM BDC_FIELD USING 'BKPF-WAERS' 'INR'.

PERFORM BDC_FIELD USING 'RF05A-XPOS1(01)' ''.
PERFORM BDC_FIELD USING 'RF05A-XPOS1(03)' 'X'.
PERFORM BDC_DYNPRO USING 'SAPMF05A' '0731'.
PERFORM BDC_FIELD USING 'BDC_CURSOR' 'RF05A-SEL01(03)'.
PERFORM BDC_FIELD USING 'BDC_OKCODE' '/00'.

W_NUM = 1.
LOOP AT T_CRNO WHERE KUNNR = T_HEADER1-KUNNR.

CONCATENATE 'RF05A-SEL01(' W_NUM ')' INTO W_CRNO.

PERFORM BDC_FIELD USING W_CRNO T_CRNO-CRN_NO.

W_NUM = W_NUM + 1.

ENDLOOP.

PERFORM BDC_DYNPRO USING 'SAPMF05A' '0731'.
PERFORM BDC_FIELD USING 'BDC_CURSOR' 'RF05A-SEL01(01)'.
PERFORM BDC_FIELD USING 'BDC_OKCODE' '=PA'.

PERFORM BDC_DYNPRO USING 'SAPDF05X' '3100'.
PERFORM BDC_FIELD USING 'BDC_OKCODE' '=OSE'.
PERFORM BDC_FIELD USING 'BDC_CURSOR' 'RF05A-ABPOS'.

PERFORM BDC_DYNPRO USING 'SAPMF05A' '0700'.
PERFORM BDC_FIELD USING 'BDC_CURSOR' 'RF05A-NEWKO'.
PERFORM BDC_FIELD USING 'BDC_OKCODE' '/00'.

PERFORM BDC_FIELD USING 'RF05A-NEWBS' '50'.
PERFORM BDC_FIELD USING 'RF05A-NEWKO' '220950'.

PERFORM BDC_DYNPRO USING 'SAPMF05A' '0300'.
PERFORM BDC_FIELD USING 'BDC_CURSOR' 'BSEG-ZUONR'.
PERFORM BDC_FIELD USING 'BDC_OKCODE' '/00'.

PERFORM BDC_FIELD USING 'BSEG-WRBTR' W_DIFFRENCE.
PERFORM BDC_FIELD USING 'BSEG-ZUONR' T_HEADER1-KUNNR.

PERFORM BDC_DYNPRO USING 'SAPLKACB' '0002'.
PERFORM BDC_FIELD USING 'BDC_OKCODE' '/EENTE'.
PERFORM BDC_FIELD USING 'BDC_CURSOR' 'COBL-GSBER'.

PERFORM BDC_DYNPRO USING 'SAPMF05A' '0300'.
PERFORM BDC_FIELD USING 'BDC_CURSOR' 'BSEG-WRBTR'.
PERFORM BDC_FIELD USING 'BDC_OKCODE' '=BS'.

PERFORM BDC_DYNPRO USING 'SAPLKACB' '0002'.
PERFORM BDC_FIELD USING 'BDC_OKCODE' '/EENTE'.
PERFORM BDC_FIELD USING 'BDC_CURSOR' 'COBL-GSBER'.

PERFORM BDC_DYNPRO USING 'SAPMF05A' '0700'.
PERFORM BDC_FIELD USING 'BDC_CURSOR' 'RF05A-NEWBS'.
PERFORM BDC_FIELD USING 'BDC_OKCODE' '=BU'.

PERFORM BDC_TRANSACTION USING 'F-03'.

CLEAR: W_DIFFRENCE.
ENDLOOP.

ENDFORM. " F008_CLEARINGB

*&---------------------------------------------------------------------*
*& Form F008_CLEARINGC
*&---------------------------------------------------------------------*
*& This form is for running BDC for F-03 if even the Minimum target
*& Qty is not met
*----------------------------------------------------------------------*
FORM F008_CLEARINGC.

LOOP AT T_ACCOUNTING.
MOVE : T_ACCOUNTING-BELNR TO T_ACCOUNT-BELNR.
MOVE : T_ACCOUNTING-KUNNR TO T_ACCOUNT-KUNNR.
MOVE : T_ACCOUNTING-KUNNR TO T_ACC-KUNNR.

APPEND : T_ACCOUNT, T_ACC.
CLEAR : T_ACCOUNT, T_ACC, T_ACCOUNTING.
ENDLOOP.

SORT : T_ACCOUNT, T_ACC BY KUNNR.
DELETE ADJACENT DUPLICATES FROM T_ACC COMPARING KUNNR.
SORT T_ACCOUNT BY KUNNR.

CLEAR : DD,MM,YY,L_DATE, W_NUM, W_DIFFRENCE.

DD = SY-DATUM+6(2).
MM = SY-DATUM+4(2).
YY = SY-DATUM+0(4).

CONCATENATE DD MM YY INTO L_DATE.

LOOP AT T_ACC.
READ TABLE T_SUMMINV1 WITH KEY KUNRG = T_ACC-KUNNR.
IF SY-SUBRC EQ 0.
T_ACC-KWERT = T_SUMMINV1-KWERT.

MODIFY T_ACC TRANSPORTING KWERT.
CLEAR: T_ACC, T_SUMMINV1.
ELSE.
DELETE T_ACC.
CLEAR T_ACC.
ENDIF.
ENDLOOP.

LOOP AT T_ACCOUNT.
READ TABLE T_ACC WITH KEY KUNNR = T_ACCOUNT-KUNNR.
IF SY-SUBRC <> 0.
DELETE T_ACCOUNT.
CLEAR: T_ACCOUNT, T_ACC.
ENDIF.
CLEAR: T_ACCOUNT.
ENDLOOP.

SORT T_ACCOUNT BY KUNNR.

LOOP AT T_ACC.

WRITE T_ACC-KWERT TO W_DIFFRENCE NO-SIGN CURRENCY 'INR'.
CONDENSE W_DIFFRENCE NO-GAPS.

PERFORM BDC_DYNPRO USING 'SAPMF05A' '0131'.
PERFORM BDC_FIELD USING 'BDC_CURSOR' 'RF05A-XPOS1(03)'.
PERFORM BDC_FIELD USING 'BDC_OKCODE' '/00'.

PERFORM BDC_FIELD USING 'RF05A-AGKON' P_HKONT.
PERFORM BDC_FIELD USING 'BKPF-BUDAT' L_DATE.
PERFORM BDC_FIELD USING 'BKPF-MONAT' W_PERIOD.
PERFORM BDC_FIELD USING 'BKPF-BUKRS' P_BUKRS.
PERFORM BDC_FIELD USING 'BKPF-WAERS' 'INR'.

PERFORM BDC_FIELD USING 'RF05A-XPOS1(01)' ''.
PERFORM BDC_FIELD USING 'RF05A-XPOS1(03)' 'X'.
PERFORM BDC_DYNPRO USING 'SAPMF05A' '0731'.
PERFORM BDC_FIELD USING 'BDC_CURSOR' 'RF05A-SEL01(02)'.
PERFORM BDC_FIELD USING 'BDC_OKCODE' '/00'.

W_NUM = 1.
LOOP AT T_ACCOUNT WHERE KUNNR = T_ACC-KUNNR.

CONCATENATE 'RF05A-SEL01(' W_NUM ')' INTO W_CRNO.

PERFORM BDC_FIELD USING W_CRNO T_ACCOUNT-BELNR.

W_NUM = W_NUM + 1.

ENDLOOP.

PERFORM BDC_DYNPRO USING 'SAPMF05A' '0731'.
PERFORM BDC_FIELD USING 'BDC_CURSOR' 'RF05A-SEL01(01)'.
PERFORM BDC_FIELD USING 'BDC_OKCODE' '=PA'.

PERFORM BDC_DYNPRO USING 'SAPDF05X' '3100'.
PERFORM BDC_FIELD USING 'BDC_OKCODE' '=OSE'.
PERFORM BDC_FIELD USING 'BDC_CURSOR' 'RF05A-ABPOS'.

PERFORM BDC_DYNPRO USING 'SAPMF05A' '0700'.
PERFORM BDC_FIELD USING 'BDC_CURSOR' 'RF05A-NEWKO'.
PERFORM BDC_FIELD USING 'BDC_OKCODE' '/00'.

PERFORM BDC_FIELD USING 'RF05A-NEWBS' '50'.
PERFORM BDC_FIELD USING 'RF05A-NEWKO' '220950'.

PERFORM BDC_DYNPRO USING 'SAPMF05A' '0300'.
PERFORM BDC_FIELD USING 'BDC_CURSOR' 'BSEG-ZUONR'.
PERFORM BDC_FIELD USING 'BDC_OKCODE' '/00'.

PERFORM BDC_FIELD USING 'BSEG-WRBTR' W_DIFFRENCE.
PERFORM BDC_FIELD USING 'BSEG-ZUONR' T_ACC-KUNNR.

PERFORM BDC_DYNPRO USING 'SAPLKACB' '0002'.
PERFORM BDC_FIELD USING 'BDC_OKCODE' '/EENTE'.
PERFORM BDC_FIELD USING 'BDC_CURSOR' 'COBL-GSBER'.

PERFORM BDC_DYNPRO USING 'SAPMF05A' '0300'.
PERFORM BDC_FIELD USING 'BDC_CURSOR' 'BSEG-WRBTR'.
PERFORM BDC_FIELD USING 'BDC_OKCODE' '=BS'.

PERFORM BDC_DYNPRO USING 'SAPLKACB' '0002'.
PERFORM BDC_FIELD USING 'BDC_OKCODE' '/EENTE'.
PERFORM BDC_FIELD USING 'BDC_CURSOR' 'COBL-GSBER'.

PERFORM BDC_DYNPRO USING 'SAPMF05A' '0700'.
PERFORM BDC_FIELD USING 'BDC_CURSOR' 'RF05A-NEWBS'.
PERFORM BDC_FIELD USING 'BDC_OKCODE' '=BU'.

PERFORM BDC_TRANSACTION USING 'F-03'.

ENDLOOP.

ENDFORM. " F008_CLEARINGC

*&---------------------------------------------------------------------*
*& Form F005_CHECKINGA
*&---------------------------------------------------------------------*
*& This Form is used for checking 220043 G/L Account
*----------------------------------------------------------------------*
FORM F005_CHECKINGA.

SELECT KBETR
INTO TABLE T_VAT
FROM KONV
FOR ALL ENTRIES IN T_INVOICES
WHERE KNUMV = T_INVOICES-KNUMV
AND KSCHL IN ('JIN6','JIN7')
AND KAPPL = 'TX'.

DELETE ADJACENT DUPLICATES FROM T_VAT COMPARING KBETR.

LOOP AT T_VAT.
G_KBETR = T_VAT-KBETR / 10.
ENDLOOP.

LOOP AT T_SUMMINV.

IF T_SUMMINV-BOX GE T_SUMMINV-ZMINQTY.

T_SUMMINV-SALETAX = ( T_SUMMINV-KWERT * G_KBETR ) / 100.
T_SUMMINV-TOTAL = T_SUMMINV-KWERT + T_SUMMINV-SALETAX.

MODIFY T_SUMMINV TRANSPORTING SALETAX TOTAL.
CLEAR T_SUMMINV.
W_FILTER = C_CASE.
CONTINUE.
ELSEIF T_SUMMINV-BOX LT T_SUMMINV-ZMINQTY.

MOVE-CORRESPONDING T_SUMMINV TO T_SUMMINV1.

T_SUMMINV1-SALETAX = ( T_SUMMINV1-KWERT * G_KBETR ) / 100.
T_SUMMINV1-TOTAL = T_SUMMINV1-KWERT + T_SUMMINV1-SALETAX.

APPEND T_SUMMINV1.
DELETE T_SUMMINV.
CLEAR: T_SUMMINV, T_SUMMINV1.
W_FILTER1 = C_CASE1.
ENDIF.
ENDLOOP.

ENDFORM. " F005_CHECKINGA

*&---------------------------------------------------------------------*
*& Form F004_TARGETCOMPAREA
*&---------------------------------------------------------------------*
*& This Form is used for checking 220043 G/L Account
*----------------------------------------------------------------------*
FORM F004_TARGETCOMPAREA.

SELECT * FROM ZTARGET
INTO TABLE T_TARGET
FOR ALL ENTRIES IN T_SUMMINV
WHERE KUNNR IN SO_KUNNR
AND VTWEG EQ T_SUMMINV-VTWEG
AND SPART EQ T_SUMMINV-SPART
AND ZCONTYPE EQ T_SUMMINV-KSCHL
AND ZFM_MTHYR EQ W_MONTH.

LOOP AT T_SUMMINV.
READ TABLE T_TARGET WITH KEY KUNNR = T_SUMMINV-KUNRG
VTWEG = T_SUMMINV-VTWEG
SPART = T_SUMMINV-SPART VKBUR = T_SUMMINV-VKBUR
ZCONTYPE = T_SUMMINV-KSCHL ZFM_MTHYR = W_MONTH.

IF SY-SUBRC EQ 0.
T_SUMMINV-ZMINQTY = T_TARGET-ZMINQTY.

MODIFY T_SUMMINV TRANSPORTING ZMINQTY.
CLEAR T_SUMMINV.
ENDIF.
ENDLOOP.


ENDFORM. " F004_TARGETCOMPAREA

*&---------------------------------------------------------------------*
*& Form F006_UPLOADA
*&---------------------------------------------------------------------*
*& This Form is used for F-43 BDC for partial target qty
*----------------------------------------------------------------------*
FORM F006_UPLOADA.

DD = SY-DATUM+6(2).
MM = SY-DATUM+4(2).
YY = SY-DATUM+0(4).

CONCATENATE DD MM YY INTO L_DATE.

LOOP AT T_SUMMINV2.

WRITE T_SUMMINV2-KWERT TO W_KWERT NO-SIGN CURRENCY 'INR'.
CONDENSE W_KWERT NO-GAPS.
WRITE T_SUMMINV2-SALETAX TO W_TAX NO-SIGN CURRENCY 'INR'.
CONDENSE W_TAX NO-GAPS.
WRITE T_SUMMINV2-TOTAL TO W_TOTAL NO-SIGN CURRENCY 'INR'.
CONDENSE W_TOTAL NO-GAPS.

PERFORM BDC_DYNPRO USING 'SAPMF05A' '0100'.
PERFORM BDC_FIELD USING 'BDC_CURSOR'
'RF05A-NEWKO'.
PERFORM BDC_FIELD USING 'BDC_OKCODE'
'/00'.

PERFORM BDC_FIELD USING 'BKPF-BLDAT' L_DATE.
PERFORM BDC_FIELD USING 'BKPF-BLART' 'DC'.
PERFORM BDC_FIELD USING 'BKPF-BUKRS' P_BUKRS.
PERFORM BDC_FIELD USING 'BKPF-BUDAT' L_DATE.
PERFORM BDC_FIELD USING 'BKPF-MONAT' W_PERIOD.
PERFORM BDC_FIELD USING 'BKPF-WAERS' 'INR'.
PERFORM BDC_FIELD USING 'RF05A-NEWBS' '40'.
PERFORM BDC_FIELD USING 'RF05A-NEWKO' P_HKONT.

PERFORM BDC_DYNPRO USING 'SAPMF05A' '0300'.
PERFORM BDC_FIELD USING 'BDC_CURSOR'
'RF05A-NEWKO'.
PERFORM BDC_FIELD USING 'BDC_OKCODE'
'/00'.

PERFORM BDC_FIELD USING 'BSEG-WRBTR' W_KWERT.
PERFORM BDC_FIELD USING 'BSEG-ZUONR' T_SUMMINV2-KUNRG.
PERFORM BDC_FIELD USING 'RF05A-NEWBS' '40'.
PERFORM BDC_FIELD USING 'RF05A-NEWKO' '214116'.

PERFORM BDC_DYNPRO USING 'SAPLKACB' '0002'.
PERFORM BDC_FIELD USING 'BDC_OKCODE'
'/EENTE'.
PERFORM BDC_FIELD USING 'BDC_CURSOR'
'COBL-GSBER'.

PERFORM BDC_DYNPRO USING 'SAPMF05A' '0300'.
PERFORM BDC_FIELD USING 'BDC_CURSOR'
'RF05A-NEWKO'.
PERFORM BDC_FIELD USING 'BDC_OKCODE'
'/00'.

PERFORM BDC_FIELD USING 'BSEG-WRBTR' W_TAX.
PERFORM BDC_FIELD USING 'BSEG-ZUONR' T_SUMMINV2-KUNRG.
PERFORM BDC_FIELD USING 'RF05A-NEWBS' '31'.
PERFORM BDC_FIELD USING 'RF05A-NEWKO' T_SUMMINV2-KUNRG.

PERFORM BDC_DYNPRO USING 'SAPLKACB' '0002'.
PERFORM BDC_FIELD USING 'BDC_OKCODE'
'/EENTE'.
PERFORM BDC_FIELD USING 'BDC_CURSOR'
'COBL-GSBER'.

PERFORM BDC_DYNPRO USING 'SAPMF05A' '0302'.
PERFORM BDC_FIELD USING 'BDC_CURSOR'
'BSEG-ZUONR'.
PERFORM BDC_FIELD USING 'BDC_OKCODE'
'/00'.

PERFORM BDC_FIELD USING 'BSEG-WRBTR' W_TOTAL.
PERFORM BDC_FIELD USING 'BSEG-ZUONR' T_SUMMINV2-KUNRG.

PERFORM BDC_DYNPRO USING 'SAPMF05A' '0302'.
PERFORM BDC_FIELD USING 'BDC_CURSOR'
'BSEG-WRBTR'.
PERFORM BDC_FIELD USING 'BDC_OKCODE'
'=BS'.

PERFORM BDC_DYNPRO USING 'SAPMF05A' '0700'.
PERFORM BDC_FIELD USING 'BDC_CURSOR'
'RF05A-NEWBS'.
PERFORM BDC_FIELD USING 'BDC_OKCODE'
'=BU'.
PERFORM BDC_TRANSACTION USING 'F-43'.

READ TABLE MESSTAB WITH KEY MSGID = 'F5' MSGNR = '312'.
IF SY-SUBRC EQ 0.

T_CRN-CRN_NO = MESSTAB-MSGV1.

APPEND T_CRN.
CLEAR T_CRN.
ENDIF.

ENDLOOP.

ENDFORM. " F006_UPLOADA

*&---------------------------------------------------------------------*
*& Form F007_READCRNOA
*&---------------------------------------------------------------------*
*& This Form is basically used to get the Credit Notes created
*----------------------------------------------------------------------*
FORM F007_READCRNOA.

LOOP AT T_CRN.

CALL FUNCTION 'CONVERSION_EXIT_ALPHA_INPUT'
EXPORTING
INPUT = T_CRN-CRN_NO
IMPORTING
OUTPUT = T_CRN-CRN_NO.

MODIFY T_CRN TRANSPORTING CRN_NO.
CLEAR T_CRN.
ENDLOOP.

REFRESH: T_UPDATE.
CLEAR: T_UPDATE.

SELECT HKONT ZUONR BELNR
FROM BSIS
INTO TABLE T_UPDATE
FOR ALL ENTRIES IN T_CRN
WHERE BELNR = T_CRN-CRN_NO
AND HKONT = P_HKONT
AND BUKRS = P_BUKRS.

LOOP AT T_UPDATE.
T_UPDATE-KUNNR = T_UPDATE-ZUONR+0(10).

MODIFY T_UPDATE TRANSPORTING KUNNR.
CLEAR T_UPDATE.
ENDLOOP.

REFRESH: T_NEW.
CLEAR: T_NEW.

LOOP AT T_UPDATE.
LOOP AT T_INVOICES WHERE KUNRG = T_UPDATE-KUNNR.
IF SY-SUBRC = 0.
T_NEW-MANDT = SY-MANDT.
T_NEW-CRN_NO = T_UPDATE-BELNR.
T_NEW-HKONT = P_HKONT.
T_NEW-VENDOR = T_INVOICES-KUNRG.
T_NEW-KUNNR = T_INVOICES-KUNRG.
T_NEW-INVOICE = T_INVOICES-VBELN.
T_NEW-POSNR = T_INVOICES-POSNR.
T_NEW-CRN_AMT = T_INVOICES-KWERT.
T_NEW-INVDT = T_INVOICES-FKDAT.

APPEND T_NEW.
CLEAR : T_NEW, T_INVOICES.
ENDIF.
ENDLOOP.
CLEAR: T_UPDATE.
ENDLOOP.

**While a partial payout is happening the Values which are posted in
**F-43 based on the rate maintained in ZTARGET table has to flow in the
**ZCRN table

SORT T_INVOICES BY KUNRG VBELN POSNR.
LOOP AT T_INVOICES.
T_VALUE-VBELN = T_INVOICES-VBELN.
T_VALUE-BUZEI = SY-TABIX.
T_VALUE-VKORG = T_INVOICES-VKORG.
T_VALUE-KUNNR = T_INVOICES-KUNRG.
T_VALUE-VTWEG = T_INVOICES-VTWEG.
T_VALUE-SPART = T_INVOICES-SPART.
T_VALUE-MONTH = W_MONTH.
T_VALUE-KSCHL = T_INVOICES-KSCHL.
T_VALUE-BOX = T_INVOICES-BOX.

APPEND T_VALUE.
CLEAR: T_VALUE, T_INVOICES.
ENDLOOP.

LOOP AT T_VALUE.
SELECT SINGLE VKBUR INTO T_VALUE-VKBUR
FROM KNVV
WHERE KUNNR EQ T_VALUE-KUNNR
AND VKORG EQ T_VALUE-VKORG
AND VTWEG EQ T_VALUE-VTWEG
AND SPART EQ T_VALUE-SPART.

SELECT SINGLE ZTGTRATE INTO T_VALUE-RATE FROM ZTARGET WHERE KUNNR =
T_VALUE-KUNNR AND VTWEG = T_VALUE-VTWEG
AND SPART = T_VALUE-SPART AND VKBUR = T_VALUE-VKBUR
AND ZCONTYPE = T_VALUE-KSCHL AND ZFM_MTHYR = W_MONTH.

IF SY-SUBRC EQ 0.
T_VALUE-KWERT = T_VALUE-BOX * T_VALUE-RATE.
ENDIF.

MODIFY T_VALUE TRANSPORTING VKBUR RATE KWERT.
CLEAR T_VALUE.
ENDLOOP.

SORT T_NEW BY KUNNR INVOICE.
LOOP AT T_NEW.
T_NEW-BUZEI = SY-TABIX.

MODIFY T_NEW TRANSPORTING BUZEI.
CLEAR T_NEW.
ENDLOOP.

SORT T_NEW BY KUNNR INVOICE BUZEI.
SORT T_VALUE BY KUNNR VBELN BUZEI.

LOOP AT T_NEW.
READ TABLE T_VALUE WITH KEY VBELN = T_NEW-INVOICE BUZEI =
T_NEW-BUZEI.
IF SY-SUBRC EQ 0.
T_NEW-CRN_AMT = 0.
T_NEW-CRN_AMT = T_VALUE-KWERT.

MODIFY T_NEW TRANSPORTING CRN_AMT.
CLEAR :T_NEW, T_VALUE.
ENDIF.
ENDLOOP.
**End of changes done by JLN on 06.04.2006

LOOP AT T_NEW.
SELECT SINGLE BELNR FROM BSID INTO T_NEW-DOC_NO WHERE VBELN =
T_NEW-INVOICE AND KUNNR = T_NEW-KUNNR AND BUKRS = P_BUKRS.

SELECT SINGLE BLDAT FROM BKPF INTO T_NEW-CRN_DT
WHERE BUKRS = P_BUKRS AND BELNR = T_NEW-CRN_NO
AND GJAHR = SY-DATUM+0(4).

T_NEW-PROV_AMT = T_NEW-CRN_AMT.
T_NEW-AUGDT = T_NEW-CRN_DT.

SELECT SINGLE PRCTR FROM VBRP INTO T_NEW-PRCTR WHERE VBELN =
T_NEW-INVOICE.

IF SY-SUBRC EQ 0.
MODIFY T_NEW TRANSPORTING DOC_NO CRN_DT PROV_AMT AUGDT PRCTR.
CLEAR T_NEW.
ENDIF.
ENDLOOP.

LOOP AT T_NEW.
READ TABLE T_SUMMINV2 WITH KEY KUNRG = T_NEW-KUNNR.
IF SY-SUBRC EQ 0.
T_NEW-KDGRP = T_SUMMINV2-KDGRP.

MODIFY T_NEW TRANSPORTING KDGRP.
CLEAR : T_NEW, T_SUMMINV2.
ENDIF.
ENDLOOP.

LOOP AT T_NEW.
MODIFY ZCRN FROM T_NEW.
ENDLOOP.

ENDFORM. " F007_READCRNOA

Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    Russian ABAP Developer's Club Forum Index -> FI All times are GMT + 4 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You cannot download files in this forum


All product names are trademarks of their respective companies. SAPNET.RU websites are in no way affiliated with SAP AG.
SAP, SAP R/3, R/3 software, mySAP, ABAP, BAPI, xApps, SAP NetWeaver and any other are registered trademarks of SAP AG.
Every effort is made to ensure content integrity. Use information on this site at your own risk.