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

Cost Center report



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



Joined: 01 Sep 2007
Posts: 1640

PostPosted: Sun Oct 14, 2007 8:47 pm    Post subject: Cost Center report Reply with quote

Code:
REPORT ZFO_SAPTOHR_COSTCENTERS NO STANDARD PAGE HEADING
MESSAGE-ID ZZ
LINE-SIZE 132
LINE-COUNT 65.
*----------------------------------------------------------------------*
* TABLES
*----------------------------------------------------------------------

TABLES :SSCRFIELDS, " Fields on selection screens
CSKS, " Cost Center Master Data
CSKT, " Cost Center Texts
T001, " Company Codes
ZXMW_XREF_GNRIC. " X-Ref tables for Mapping App - Generic table

*----------------------------------------------------------------------*
* TYPES
*----------------------------------------------------------------------*

TYPES : BEGIN OF TY_CSKS,
KOKRS LIKE CSKS-KOKRS, " Controlling Area
KOSTL LIKE CSKS-KOSTL, " Cost Center
DATBI LIKE CSKS-DATBI, " To-Date
DATAB LIKE CSKS-DATAB, " From-date
BUKRS LIKE CSKS-BUKRS, " Company code
KOSAR LIKE CSKS-KOSAR, " Headcount Indicator
VERAK LIKE CSKS-VERAK, " Person Responsible
BKZKP LIKE CSKS-BKZKP, " Lock Indocator for Actual
" Primiary Postings
ERSDA LIKE CSKS-ERSDA, " Date Created
BUTXT LIKE T001-BUTXT, " Company name
SKUID LIKE ZXMW_XREF_GNRIC-SKUID," Old cost center
KTEXT LIKE CSKT-KTEXT, " Short Name
LTEXT LIKE CSKT-LTEXT, " Long Name
TGVAL LIKE ZXMW_XREF_GNRIC-TGVAL,
END OF TY_CSKS.

************************************************************************
* CONSTANTS
************************************************************************
DATA : C_SELECTED TYPE C VALUE 'X', "Field for constant value 'X'
W_MESSAGE TYPE c, "ERROR MESSAGE
C_SRCID LIKE ZXMW_XREF_GNRIC-SRCID VALUE '3329',
C_ENTID LIKE ZXMW_XREF_GNRIC-ENTID VALUE '0062',
"MW Master Data Entity ID
C_DVERS LIKE ZXMW_XREF_GNRIC-DVERS VALUE '01',
" Definition Version for MW
" Mapping App

W_SKEY(22) TYPE N ,
W_KOSTL LIKE CSKS-KOSTL.


*----------------------------------------------------------------------*
* INTERNAL TABLES
*----------------------------------------------------------------------*

*Internal table for getting the Cost centers
DATA : IT_CSKS TYPE TY_CSKS OCCURS 0 WITH HEADER LINE.


DATA: IT_COSTCENTERS LIKE BAPI0012_CCLIST OCCURS 100 WITH HEADER LINE,
IT_RETURN LIKE BAPIRET2 OCCURS 0 WITH HEADER LINE,
IT_COSTCENTERDETAIL LIKE BAPI0012_CCOUTPUTLIST OCCURS 0 WITH
HEADER LINE.

*Internal table for final data
DATA : BEGIN OF IT_FINAL_DATA OCCURS 0,
TEXT(170) TYPE C,
END OF IT_FINAL_DATA.

*Internal table for old cost centers .
DATA :IT_ZXMW_XREF_GNRIC LIKE ZXMW_XREF_GNRIC OCCURS 0 WITH HEADER LINE.

*Internal table for costcenter Text.
DATA :BEGIN OF IT_CSKT OCCURS 0,
KOSTL LIKE CSKT-KOSTL,
DATBI LIKE CSKT-DATBI,
KTEXT LIKE CSKT-KTEXT,
LTEXT LIKE CSKT-LTEXT,
END OF IT_CSKT.

*----------------------------------------------------------------------*
* SELECTION-SCREEN
*----------------------------------------------------------------------*
*--Block for File path.
SELECTION-SCREEN BEGIN OF BLOCK B1 WITH FRAME TITLE TEXT-001.
PARAMETERS: P_KOKRS LIKE CSKS-KOKRS DEFAULT '1000'.
PARAMETERS: P_KOSAR LIKE CSKS-KOSAR DEFAULT 'Y'.

PARAMETERS :R1 RADIOBUTTON GROUP GRP1 DEFAULT 'X'.
PARAMETERS: P_UNIX LIKE RLGRAP-FILENAME DEFAULT
'/tmp/ZFO_SAPTOHR_COSTCENTERS.TXT'.
PARAMETERS :R2 RADIOBUTTON GROUP GRP1.
PARAMETERS: P_FILE LIKE RLGRAP-FILENAME DEFAULT 'C:\'.

SELECTION-SCREEN END OF BLOCK B1.

*----------------------------------------------------------------------*
* AT SELECTION-SCREEN ON
*----------------------------------------------------------------------*
AT SELECTION-SCREEN ON BLOCK B1.
* Form to Validate data specified on Selection Screen
CHECK SSCRFIELDS-UCOMM EQ 'ONLI'.
PERFORM SELECTIONSCREEN_VALIDATIONS.

AT SELECTION-SCREEN ON VALUE-REQUEST FOR P_FILE.

* Form to get file path to download the data
PERFORM GET_FILE_PATH USING P_FILE.


*----------------------------------------------------------------------*
* START-OF-SELECTION.
*----------------------------------------------------------------------*

START-OF-SELECTION.

*Form to get the costcenters.
PERFORM GETCOSTCENTERS.

*Form to get the costcenter details.
PERFORM GETCOSTDETAILS.

*Form to find the corresponding Cost Centers in Legacy system
* PERFORM GET_LEGACY_COSTCENTERS.

*----------------------------------------------------------------------*
* END-OF-SELECTION
*----------------------------------------------------------------------*

END-OF-SELECTION.

IF R1 = 'X'.
PERFORM DOWNLOAD_UNIXFILE.
ELSE.
PERFORM DOWNLOAD_LOCALFILE.
ENDIF.


*&---------------------------------------------------------------------*
*& Form GETCOSTCENTERS
*&---------------------------------------------------------------------*
* LIST OF COST CENTERS.
*----------------------------------------------------------------------*
FORM GETCOSTCENTERS .
CALL FUNCTION 'BAPI_COSTCENTER_GETLIST1'
EXPORTING
CONTROLLINGAREA = P_KOKRS
* COSTCENTER_FROM =
* COSTCENTER_TO =
* COMPANYCODE_FROM =
* COMPANYCODE_TO =
* PERSON_IN_CHARGE_FROM =
* PERSON_IN_CHARGE_TO =
* DATE_FROM = SY-DATUM
* DATE_TO =
* COSTCENTERGROUP =
* BUSINESS_AREA_FROM =
* BUSINESS_AREA_TO =
* MASTER_DATA_INACTIVE =
TABLES
COSTCENTERLIST = IT_COSTCENTERS
RETURN = IT_RETURN.
* EXTENSIONIN =
* EXTENSIONOUT =


IF SY-SUBRC <> 0.
MESSAGE E002 WITH 'NO COST CENTERS FOUND'(002).
ENDIF.
ENDFORM. " GETCOSTCENTERS


*&---------------------------------------------------------------------*
*& Form GETCOSTDETAILS
*&---------------------------------------------------------------------*
* Get the Costcenter Details
*----------------------------------------------------------------------*
FORM GETCOSTDETAILS .

IF NOT IT_COSTCENTERS[] IS INITIAL.

SELECT KOKRS
KOSTL
DATBI
DATAB
BUKRS
KOSAR
VERAK
BKZKP
ERSDA
FROM CSKS
INTO TABLE IT_CSKS
FOR ALL ENTRIES IN IT_COSTCENTERS
WHERE KOKRS = IT_COSTCENTERS-CO_AREA AND
KOSAR = P_KOSAR AND
BKZKP = SPACE.
ENDIF.


IF NOT IT_CSKS[] IS INITIAL.

SELECT KOSTL
DATBI
KTEXT
LTEXT
FROM CSKT
INTO TABLE IT_CSKT
FOR ALL ENTRIES IN IT_CSKS
WHERE SPRAS = SY-LANGU AND
KOKRS = IT_CSKS-KOKRS AND
KOSTL = IT_CSKS-KOSTL AND
DATBI = IT_CSKS-DATBI.

SORT IT_CSKT BY KOSTL DATBI.

ENDIF.


LOOP AT IT_CSKS.

READ TABLE IT_CSKT WITH KEY KOSTL = IT_CSKS-KOSTL
DATBI = IT_CSKS-DATBI
BINARY SEARCH.
IF SY-SUBRC = 0.
IT_CSKS-KTEXT = IT_CSKT-KTEXT.
IT_CSKS-LTEXT = IT_CSKT-LTEXT.
ENDIF.


SELECT SINGLE BUTXT
FROM T001
INTO IT_CSKS-BUTXT
WHERE BUKRS = IT_CSKS-BUKRS.

CLEAR : W_KOSTL.
W_KOSTL = IT_CSKS-KOSTL.

CALL FUNCTION 'CONVERSION_EXIT_ALPHA_OUTPUT'
EXPORTING
INPUT = W_KOSTL
IMPORTING
OUTPUT = W_KOSTL.





CONCATENATE IT_CSKS-KOKRS W_KOSTL IT_CSKS-DATBI INTO W_SKEY.

SELECT SINGLE TGVAL
FROM ZXMW_XREF_GNRIC
INTO IT_CSKS-TGVAL
WHERE SRCID = C_SRCID AND
ENTID = C_ENTID AND
DVERS = C_DVERS AND
EFFDT <= SY-DATUM AND
EXPDT GT SY-DATUM AND
SKVAL = W_SKEY AND
STATUS <> 'U' .

* IF SY-SUBRC = 0.
* MODIFY IT_CSKS.
* ENDIF.
*
* CONCATENATE IT_CSKS-BUKRS IT_CSKS-BUTXT IT_CSKS-KOSTL IT_CSKS-TGVAL
* IT_CSKS-KTEXT IT_CSKS-LTEXT IT_CSKS-BKZKP IT_CSKS-KOSAR
* IT_CSKS-VERAK IT_CSKS-ERSDA IT_CSKS-DATBI IT_CSKS-DATAB
* INTO IT_FINAL_DATA SEPARATED BY '~'.
*
* CONCATENATE IT_FINAL_DATA '~' INTO IT_FINAL_DATA .
* APPEND IT_FINAL_DATA.
* CLEAR : IT_FINAL_DATA,
* IT_CSKS.

MODIFY IT_CSKS.

CONCATENATE IT_CSKS-BUKRS IT_CSKS-BUTXT IT_CSKS-KOSTL IT_CSKS-TGVAL
IT_CSKS-KTEXT IT_CSKS-LTEXT IT_CSKS-BKZKP IT_CSKS-KOSAR
IT_CSKS-VERAK IT_CSKS-ERSDA IT_CSKS-DATBI IT_CSKS-DATAB
INTO IT_FINAL_DATA SEPARATED BY '~'.

CONCATENATE IT_FINAL_DATA '~' INTO IT_FINAL_DATA .
APPEND IT_FINAL_DATA.
CLEAR :IT_CSKS.
ENDLOOP.
ENDFORM. " GETCOSTDETAILS

*
**&---------------------------------------------------------------------
*
**& Form DOWNLOAD_UNIXFILE
**&---------------------------------------------------------------------
*
** Placing file in Application server
**----------------------------------------------------------------------
FORM DOWNLOAD_UNIXFILE .


OPEN DATASET P_UNIX FOR OUTPUT IN TEXT MODE ENCODING DEFAULT.
IF SY-SUBRC NE 0.
MESSAGE E001(0) WITH 'Unable to open file'.
ENDIF.
DO.
READ TABLE IT_FINAL_DATA INDEX SY-INDEX.
IF SY-SUBRC EQ 0.
TRANSFER IT_FINAL_DATA TO P_UNIX.
ELSE.
MESSAGE S001 WITH 'File' P_UNIX 'Successfully Downloaded'.
EXIT.
ENDIF.
ENDDO.

CLOSE DATASET P_UNIX.

ENDFORM. "DOWNLOAD_UNIXFILE

*&---------------------------------------------------------------------*
*& Form DOWNLOAD_LOCALFILE
*&---------------------------------------------------------------------*
* Placing file in Presentation server
*----------------------------------------------------------------------*
FORM DOWNLOAD_LOCALFILE .

DATA:L_FILE TYPE STRING.

L_FILE = P_FILE.

CALL FUNCTION 'GUI_DOWNLOAD'
EXPORTING
FILENAME = L_FILE
FILETYPE = 'ASC'
TABLES
DATA_TAB = IT_FINAL_DATA
EXCEPTIONS
FILE_WRITE_ERROR = 1
NO_BATCH = 2
GUI_REFUSE_FILETRANSFER = 3
INVALID_TYPE = 4
NO_AUTHORITY = 5
UNKNOWN_ERROR = 6
HEADER_NOT_ALLOWED = 7
SEPARATOR_NOT_ALLOWED = 8
FILESIZE_NOT_ALLOWED = 9
HEADER_TOO_LONG = 10
DP_ERROR_CREATE = 11
DP_ERROR_SEND = 12
DP_ERROR_WRITE = 13
UNKNOWN_DP_ERROR = 14
ACCESS_DENIED = 15
DP_OUT_OF_MEMORY = 16
DISK_FULL = 17
DP_TIMEOUT = 18
FILE_NOT_FOUND = 19
DATAPROVIDER_EXCEPTION = 20
CONTROL_FLUSH_ERROR = 21
OTHERS = 22.
IF SY-SUBRC <> 0.
MESSAGE E001 WITH 'Unable to open the file'.
ELSE.
MESSAGE S001 WITH 'File' L_FILE 'Successfully Downloaded'.
ENDIF.

ENDFORM. " DOWNLOAD_LOCALFILE
*&---------------------------------------------------------------------*
*& Form GET_FILE_PATH
*&---------------------------------------------------------------------*
* F4 HELP to get the File Path
*----------------------------------------------------------------------*
FORM GET_FILE_PATH USING P_P_FILE.

CALL FUNCTION 'F4_FILENAME'
IMPORTING
FILE_NAME = P_P_FILE.

ENDFORM. " GET_FILE_PATH
*&---------------------------------------------------------------------*
*& Form GET_LEGACY_COSTCENTERS
*&---------------------------------------------------------------------*
* Legacy system Cost centers
*----------------------------------------------------------------------*
*FORM GET_LEGACY_COSTCENTERS .
*
* LOOP AT IT_CSKS.
*
* CONCATENATE IT_CSKS-KOKRS IT_CSKS-KOSTL IT_CSKS-DATBI INTO W_SKEY.
*
* SELECT SINGLE TGVAL
* FROM ZXMW_XREF_GNRIC
* INTO IT_CSKS-TGVAL
* WHERE SRCID = C_SRCID AND
* ENTID = C_ENTID AND
* DVERS = C_DVERS AND
* EFFDT <= SY-DATUM AND
* EXPDT GT SY-DATUM AND
* TGVAL = IT_CSKS-KOSTL AND
* STATUS <> 'U' .
*
* IF SY-SUBRC = 0.
* MODIFY IT_CSKS.
* ENDIF.
*
* CONCATENATE IT_CSKS-BUKRS IT_CSKS-BUTXT IT_CSKS-KOSTL IT_CSKS-SKUID
* IT_CSKS-KTEXT IT_CSKS-LTEXT IT_CSKS-BKZKP IT_CSKS-KOSAR
* IT_CSKS-VERAK IT_CSKS-ERSDA IT_CSKS-DATBI IT_CSKS-DATAB
* INTO IT_FINAL_DATA SEPARATED BY '~'.
*
* CONCATENATE IT_FINAL_DATA '~' INTO IT_FINAL_DATA .
* APPEND IT_FINAL_DATA.
* CLEAR : IT_FINAL_DATA,
* IT_CSKS.
* ENDLOOP.
*
*ENDFORM. " GET_LEGACY_COSTCENTERS
*&---------------------------------------------------------------------*
*& Form SELECTIONSCREEN_VALIDATIONS
*&---------------------------------------------------------------------*
* Form used for selection screen validations
*----------------------------------------------------------------------*
FORM SELECTIONSCREEN_VALIDATIONS .
DATA : L_LEN TYPE I,
L_LEN1 TYPE I.
*If Presentation Server file path is not entered throw a Error message
IF P_FILE IS INITIAL AND R2 = C_SELECTED.
CLEAR SSCRFIELDS-UCOMM.
MESSAGE E001(ZZ) WITH TEXT-012.
ENDIF.
*If Application Server file path is not entered throw a Error message
IF P_UNIX IS INITIAL AND R1 = C_SELECTED.
CLEAR SSCRFIELDS-UCOMM.
MESSAGE E001(ZZ) WITH TEXT-021.
ENDIF.
* In Presentation server path,If second character is not ':' throw a
* Error message
IF R2 = C_SELECTED.
IF P_FILE+1(1) NE ':'.
MESSAGE E001(ZZ) WITH TEXT-003.
ENDIF.
ENDIF.
* In Presentation server path,If the file path starts with '/' or '\'
* throw a Error message
IF R2 = C_SELECTED.
IF P_FILE+0(1) EQ '\' OR P_FILE+0(1) EQ '/'.
MESSAGE E001(ZZ) WITH TEXT-003.
ENDIF.
ENDIF.

* Checking whether the Application Server Path is valid or not
IF R1 = 'X'.
CLEAR W_Message.
OPEN DATASET P_UNIX FOR INPUT IN TEXT MODE
MESSAGE W_Message ENCODING DEFAULT.
IF SY-SUBRC <> 0.
MESSAGE E001(ZZ) WITH P_UNIX W_Message.
STOP.
ELSE.
CLOSE DATASET P_UNIX.
ENDIF.
ENDIF.


ENDFORM. " SELECTIONSCREEN_VALIDATIONS
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 -> CO 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.