Sunteți pe pagina 1din 11

*&---------------------------------------------------------------------*

*& Report ZEP_TEXT_TMP


*&
*&---------------------------------------------------------------------*
*&
*&
*&---------------------------------------------------------------------*
REPORT zep_text_tmp.

*REPORT zexportincasari.
TABLES: bkpf, bsid.
TYPE-POOLS list.
* selection-screen
*{ REPLACE
RLQK900067
1
*\SELECT-OPTIONS s_cpudt FOR bkpf-cpudt DEFAULT sy-datum OBLIGATORY.
SELECT-OPTIONS s_budat FOR bkpf-budat DEFAULT sy-datum OBLIGATORY. "Posting Dat
e in the Document
*} REPLACE
PARAMETERS p_bukrs TYPE bkpf-bukrs MEMORY ID buk OBLIGATORY. "Company Code
SELECT-OPTIONS s_blart FOR bkpf-blart." OBLIGATORY NO INTERVALS. "Document Type
SELECTION-SCREEN SKIP.
SELECT-OPTIONS s_kunnr FOR bsid-kunnr. "Customer Number 1
SELECTION-SCREEN SKIP.
PARAMETERS p_disp RADIOBUTTON GROUP gr1 DEFAULT 'X' USER-COMMAND rb.
PARAMETERS p_export RADIOBUTTON GROUP gr1.
PARAMETERS p_file TYPE rlgrap-filename MODIF ID fil.
"bkpf - Accounting Document Header
TYPES: BEGIN OF ty_bkpf,
belnr TYPE bkpf-belnr, "Accounting Document Number
gjahr TYPE bkpf-gjahr, "Fiscal Year
cpudt TYPE bkpf-cpudt, "Day On Which Accounting Document Was Entered
budat TYPE bkpf-budat, "Posting Date in the Document
xblnr TYPE bkpf-xblnr, "Reference Document Number
waers TYPE bkpf-waers, "Currency Key
END OF ty_bkpf,
ty_t_bkpf TYPE STANDARD TABLE OF ty_bkpf.
"bseg - Accounting Document Segment
TYPES: BEGIN OF ty_bseg,
buzei TYPE bseg-buzei, "Number of Line Item Within Accounting Document
augbl TYPE bseg-augbl, "Document Number of the Clearing Document
auggj TYPE bseg-auggj, "Fiscal Year of Clearing Document
rebzg TYPE bseg-rebzg, "Number of the Invoice the Transaction Belongs t
o
rebzj TYPE bseg-rebzj, "Fiscal Year of the Relevant Invoice (for Credit
Memo)
dmbtr TYPE bseg-dmbtr, "Amount in Local Currency
wrbtr TYPE bseg-wrbtr, "Amount in document currency
kunnr TYPE bseg-kunnr, "Customer Number 1
umsks TYPE bseg-umsks, "Special G/L Transaction Type
umskz TYPE bseg-umskz, "Special G/L Indicator
augdt TYPE bseg-augdt, "Clearing Date
shkzg TYPE bseg-shkzg, "Debit/Credit Indicator
END OF ty_bseg.

TYPES: BEGIN OF ty_doc.


INCLUDE TYPE ty_bkpf.
INCLUDE TYPE ty_bseg.
TYPES: END OF ty_doc,
ty_t_doc TYPE STANDARD TABLE OF ty_doc.
TYPES: BEGIN OF ty_data,
invoice TYPE ty_doc,
payment TYPE ty_doc,
END OF ty_data,
ty_t_data TYPE STANDARD TABLE OF ty_data.
CONSTANTS yes VALUE 'X'.
CONSTANTS no VALUE 'X'.
DATA gt_data TYPE ty_t_data.
AT SELECTION-SCREEN OUTPUT.
LOOP AT SCREEN.
IF screen-group1 = 'FIL' AND p_export = ''.
screen-active = 0.
MODIFY SCREEN.
ENDIF.
ENDLOOP.
AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_file.
PERFORM get_file CHANGING p_file.
INITIALIZATION.
s_blart-sign = 'I'.
s_blart-option = 'EQ'.
s_blart-low = 'RP'. APPEND s_blart.
s_blart-low = 'RV'. APPEND s_blart.
s_blart-low = 'RS'. APPEND s_blart.
START-OF-SELECTION.
PERFORM select_data.
IF gt_data IS INITIAL.
MESSAGE i721(fr).
* No data was selected that meets the selection parameters
EXIT.
ENDIF.
IF p_export = 'X'.
PERFORM export_data USING p_file.
ELSE.
PERFORM display_data.
ENDIF.
*----------------------------------------------------------------------*
*
CLASS lcl_disp_alv DEFINITION
*----------------------------------------------------------------------*
CLASS lcl_disp_alv DEFINITION.
PUBLIC SECTION.
CONSTANTS: yes VALUE 'X',
no VALUE ' '.
METHODS:
init_table CHANGING t_table TYPE table,
display,

hide_columns,
color_columns,
set_status,
activate_all_functions,
col_text IMPORTING col_name TYPE c
plen TYPE lvc_outlen OPTIONAL
text TYPE c,
activate_layout IMPORTING handle TYPE salv_s_layout_key-handle.
DATA o_cols TYPE REF TO cl_salv_columns_table.
PROTECTED SECTION.
METHODS:
on_user FOR EVENT added_function OF cl_salv_events_table
IMPORTING e_salv_function,
is_column_to_be_hidden IMPORTING colname TYPE lvc_fname
RETURNING value(hide) TYPE flag.
DATA lo_alv TYPE REF TO cl_salv_table.
DATA lo_cx_salv TYPE REF TO cx_salv_error.
ENDCLASS. "lcl_disp_alv DEFINITION
*---------------------------------------------------------------------*
* CLASS lcl_disp_alv IMPLEMENTATION
*---------------------------------------------------------------------*
CLASS lcl_disp_alv IMPLEMENTATION.
METHOD init_table.
TRY.
CALL METHOD cl_salv_table=>factory
IMPORTING
r_salv_table = lo_alv
CHANGING
t_table
= t_table.
CATCH cx_salv_msg INTO lo_cx_salv.
MESSAGE lo_cx_salv TYPE 'E'.
ENDTRY.
o_cols = lo_alv->get_columns( ).
ENDMETHOD.
"know_alv
METHOD display.
lo_alv->display( ).
ENDMETHOD.
"display
METHOD hide_columns.
DATA t_col TYPE salv_t_column_ref.
DATA s_col TYPE salv_s_column_ref.
t_col = o_cols->get( ).
LOOP AT t_col INTO s_col.
IF is_column_to_be_hidden( s_col-columnname ) = 'X'.
s_col-r_column->set_visible( space ).
ENDIF.
ENDLOOP.
ENDMETHOD.
"hide_columns
METHOD color_columns.
DATA t_col TYPE salv_t_column_ref.
DATA s_col TYPE salv_s_column_ref.
DATA r_col TYPE REF TO cl_salv_column_table.
DATA s_color_payment TYPE lvc_s_colo.
DATA s_color_invoice TYPE lvc_s_colo.
s_color_payment-col = col_group.
s_color_invoice-col = col_normal.
t_col = o_cols->get( ).
LOOP AT t_col INTO s_col.
r_col ?= s_col-r_column.
IF s_col-columnname(8) = 'PAYMENT-'.

r_col->set_color( s_color_payment ).
ELSEIF s_col-columnname(8) = 'INVOICE-'.
r_col->set_color( s_color_invoice ).
ENDIF.
ENDLOOP.
ENDMETHOD.
"color_columns
METHOD is_column_to_be_hidden.
IF colname(8) = 'PAYMENT-'.
CASE colname+8.
WHEN 'BUZEI' OR
'AUGBL' OR
'AUGGJ' OR
'REBZJ' OR
'UMSKS' OR
'UMSKZ' OR
'DMBTR' OR
'SHKZG' OR
'AUGDT'.
hide = yes.
WHEN OTHERS.
hide = no.
ENDCASE.
ELSEIF colname(8) = 'INVOICE-'.
CASE colname+8.
WHEN 'BUZEI' OR
'AUGBL' OR
'AUGGJ' OR
'REBZJ' OR
'UMSKS' OR
'UMSKZ' OR
'DMBTR' OR
'SHKZG' OR
'AUGDT'.
hide = yes.
WHEN OTHERS.
hide = no.
ENDCASE.
ELSE.
hide = no.
ENDIF.
ENDMETHOD.
"is_column_to_be_hidden
METHOD set_status.
DATA o_events TYPE REF TO cl_salv_events_table.
lo_alv->set_screen_status( report = sy-repid pfstatus = 'SALV' ).
o_events = lo_alv->get_event( ).
SET HANDLER me->on_user FOR o_events.
ENDMETHOD.
"set_status
METHOD col_text.
DATA: l_short TYPE scrtext_s,
l_med TYPE scrtext_m,
l_long TYPE scrtext_l.
DATA: o_col TYPE REF TO cl_salv_column_table.
TRY.
o_col ?= o_cols->get_column( col_name ).
CATCH cx_salv_not_found INTO lo_cx_salv.
MESSAGE lo_cx_salv TYPE 'E'.
ENDTRY.
l_short = l_med = l_long = text.
o_col->set_short_text( l_short ).

o_col->set_medium_text( l_med ).
o_col->set_long_text( l_long ).
IF plen > 0.
o_col->set_output_length( plen ).
ENDIF.
ENDMETHOD.
"col_text
METHOD on_user.
DATA l_file TYPE rlgrap-filename.
CASE e_salv_function.
WHEN 'EXPORT'.
PERFORM get_file CHANGING l_file.
IF l_file IS INITIAL.
EXIT.
ENDIF.
PERFORM export_data USING l_file.
ENDCASE.
ENDMETHOD.
"on_user
METHOD activate_all_functions.
DATA o_func TYPE REF TO cl_salv_functions_list.
o_func = lo_alv->get_functions( ).
o_func->set_all( ).
ENDMETHOD.
"activate_all_functions
METHOD activate_layout.
DATA: o_layout TYPE REF TO cl_salv_layout,
l_layout_key TYPE salv_s_layout_key.
o_layout = lo_alv->get_layout( ).
l_layout_key-report = sy-repid.
l_layout_key-handle = handle.
o_layout->set_key( l_layout_key ).
o_layout->set_save_restriction( if_salv_c_layout=>restrict_none ).
o_layout->set_default( 'X' ).
ENDMETHOD.
"activate_layout
ENDCLASS.
"lcl_disp_alv IMPLEMENTATION
*&---------------------------------------------------------------------*
*&
Form select_data
*&---------------------------------------------------------------------*
FORM select_data.
DATA t_bkpf TYPE ty_t_bkpf.
DATA t_doc_opened TYPE ty_t_doc.
DATA t_doc_closed TYPE ty_t_doc.
PERFORM read_today_bkpf CHANGING t_bkpf.
PERFORM split_bkpf USING t_bkpf CHANGING t_doc_opened t_doc_closed.
IF t_doc_opened IS INITIAL AND
t_doc_closed IS INITIAL.
EXIT.
ENDIF.
PERFORM analyze_closed_items USING t_doc_closed.
PERFORM analyze_open_items USING t_doc_opened.
ENDFORM.
"select_data
*&---------------------------------------------------------------------*
*&
Form split_bkpf
*&---------------------------------------------------------------------*
FORM split_bkpf USING t_bkpf TYPE ty_t_bkpf
CHANGING t_doc_opened TYPE ty_t_doc

t_doc_closed TYPE ty_t_doc.


DATA s_bkpf TYPE ty_bkpf.
DATA s_bseg TYPE ty_bseg.
DATA s_doc TYPE ty_doc.
LOOP AT t_bkpf INTO s_bkpf.
SELECT SINGLE * FROM bseg
INTO CORRESPONDING FIELDS OF s_bseg
WHERE bukrs = p_bukrs AND
"Company Code
belnr = s_bkpf-belnr AND "Accounting Document Number
gjahr = s_bkpf-gjahr AND "Fiscal Year
kunnr IN s_kunnr AND
koart = 'D' AND "Customers
shkzg = 'H'.
"Credit
IF sy-subrc <> 0.
CONTINUE.
ENDIF.
CLEAR s_doc.
MOVE-CORRESPONDING s_bkpf TO s_doc.
MOVE-CORRESPONDING s_bseg TO s_doc.
IF s_doc-augbl IS INITIAL.
APPEND s_doc TO t_doc_opened.
ELSE.
APPEND s_doc TO t_doc_closed.
ENDIF.
ENDLOOP.
ENDFORM.
"split_bkpf
*&---------------------------------------------------------------------*
*&
Form read_today_bkpf
*&---------------------------------------------------------------------*
FORM read_today_bkpf CHANGING t_bkpf TYPE ty_t_bkpf.
SELECT *
FROM bkpf
INTO CORRESPONDING FIELDS OF TABLE t_bkpf
WHERE bukrs = p_bukrs AND "Company Code
*{ REPLACE
RLQK900067
1
*\
cpudt IN s_cpudt.
budat IN s_budat. ""Posting Date in the Document
*} REPLACE
ENDFORM.
"read_today_bkpf
*&---------------------------------------------------------------------*
*&
Form analyze_closed_items
*&---------------------------------------------------------------------*
FORM analyze_closed_items USING t_doc TYPE ty_t_doc.
DATA s_doc TYPE ty_doc.
LOOP AT t_doc INTO s_doc.
PERFORM analyze_one_closed_item USING s_doc.
ENDLOOP.
ENDFORM.
"analyze_closed_items
*&---------------------------------------------------------------------*
*&
Form analyze_one_closed_item
*&---------------------------------------------------------------------*
FORM analyze_one_closed_item USING s_doc TYPE ty_doc.
DATA t_invoice_doc TYPE ty_t_doc.
DATA s_invoice_doc TYPE ty_doc.
DATA s_data TYPE ty_data.
SELECT * FROM bsad
INTO CORRESPONDING FIELDS OF TABLE t_invoice_doc
WHERE bukrs = p_bukrs AND
"Company Code
kunnr = s_doc-kunnr AND "Customer Number 1

umsks
umskz
augdt
augbl

=
=
=
=

s_doc-umsks
s_doc-umskz
s_doc-augdt
s_doc-augbl

AND
AND
AND
AND

"Special G/L Transaction Type


"Special G/L Indicator
"Clearing Date
"Document Number of the Clearin

g Document
blart IN s_blart.
"Document Type
DELETE t_invoice_doc WHERE belnr = s_doc-belnr AND "Accounting Document Numb
er
gjahr = s_doc-gjahr AND "Fiscal Year
buzei = s_doc-buzei.
"Number of Line Item With
in
*
*
*
*

Accounting Document.
IF t_invoice_doc IS INITIAL.
EXIT.
ENDIF.
ASSERT ID ZEP_TEXT_TMP CONDITION LINES( t_invoice_doc ) = 1.

LOOP AT t_invoice_doc INTO s_invoice_doc WHERE waers <> s_doc-waers.


EXIT.
ENDLOOP.
IF sy-subrc = 0.
WRITE : / 'Incasarea', s_doc-belnr,'are alta moneda', s_doc-waers,'decat fac
tura',s_invoice_doc-belnr,'(',s_invoice_doc-waers,')'.
EXIT.
ENDIF.
DATA l_remaining TYPE ty_doc-wrbtr.
l_remaining = s_doc-wrbtr.
LOOP AT t_invoice_doc INTO s_invoice_doc WHERE shkzg = 'S'. "Debit
s_data-invoice = s_invoice_doc.
s_data-payment = s_doc.
IF l_remaining > s_invoice_doc-wrbtr.
s_data-payment-wrbtr = s_invoice_doc-wrbtr.
ELSE.
s_data-payment-wrbtr = l_remaining.
ENDIF.
APPEND s_data TO gt_data.
l_remaining = l_remaining - s_data-payment-wrbtr.
IF l_remaining = 0.
EXIT.
ENDIF.
ENDLOOP.
IF l_remaining > 0.
CLEAR s_data.
s_data-payment = s_doc.
s_data-payment-wrbtr = l_remaining.
s_data-invoice-belnr = '??'.
APPEND s_data TO gt_data.
ENDIF.
ENDFORM.
"analyze_closed_items
*&---------------------------------------------------------------------*
*&
Form analyze_open_items
*&---------------------------------------------------------------------*
FORM analyze_open_items USING t_doc TYPE ty_t_doc.
DATA s_doc TYPE ty_doc.
LOOP AT t_doc INTO s_doc WHERE rebzg <> ''.
PERFORM analyze_one_open_item USING s_doc.
ENDLOOP.
ENDFORM.
"analyze_open_items

*&---------------------------------------------------------------------*
*&
Form analyze_one_open_item
*&---------------------------------------------------------------------*
FORM analyze_one_open_item USING s_paym TYPE ty_doc.
DATA s_invoice TYPE ty_doc.
DATA s_data TYPE ty_data.
SELECT SINGLE * FROM bsid
INTO CORRESPONDING FIELDS OF s_invoice
WHERE bukrs = p_bukrs AND
"Company Code
kunnr = s_paym-kunnr AND "Customer Number 1
belnr = s_paym-rebzg AND "Accounting Document Number
gjahr = s_paym-rebzj AND "Fiscal Year
umsks = s_paym-umsks AND "Special G/L Transaction Type
umskz = s_paym-umskz AND "Special G/L Indicator
blart IN s_blart.
"Document Type
IF sy-subrc <> 0.
EXIT.
ENDIF.
s_data-invoice = s_invoice.
s_data-payment = s_paym.
APPEND s_data TO gt_data.
ENDFORM.
"analyze_one_open_item
*&---------------------------------------------------------------------*
*&
Form get_file
*&---------------------------------------------------------------------*
FORM get_file CHANGING file TYPE rlgrap-filename.
DATA l_fullpath TYPE string.
DATA l_path
TYPE string.
DATA l_filename TYPE string.
CALL METHOD cl_gui_frontend_services=>file_save_dialog
EXPORTING
window_title
= 'Selectati fisierul cu date'
default_extension
= 'TXT'
default_file_name
= 'INCASARI'
*
with_encoding
=
*
file_filter
=
*
initial_directory
=
*
prompt_on_overwrite = 'X'
CHANGING
filename
= l_filename
path
= l_path
fullpath
= l_fullpath
EXCEPTIONS
cntl_error
= 1
error_no_gui
= 2
not_supported_by_gui = 3
OTHERS
= 4
.
IF sy-subrc <> 0 OR l_fullpath IS INITIAL.
EXIT.
ENDIF.
file = l_fullpath.
ENDFORM.

"get_file

*&---------------------------------------------------------------------*
*&
Form export_data
*&---------------------------------------------------------------------*

FORM export_data USING file TYPE rlgrap-filename.


IF file IS INITIAL.
EXIT.
ENDIF.
TYPES:
BEGIN OF ty_export,
invoice
TYPE bkpf-belnr,
ref_invoice TYPE bkpf-xblnr,
customer
TYPE bseg-kunnr,
paym_amount TYPE bseg-dmbtr,
paym_date TYPE bkpf-budat,
cry
TYPE t001-waers,
END OF ty_export.
DATA s_export TYPE ty_export.
DATA lt_export TYPE STANDARD TABLE OF ty_export.
DATA s_data TYPE ty_data.
* SELECT SINGLE waers FROM t001 INTO s_export-cry WHERE bukrs = p_bukrs.
LOOP AT gt_data INTO s_data.
s_export-invoice = s_data-invoice-belnr.
s_export-ref_invoice = s_data-invoice-xblnr.
s_export-customer = s_data-payment-kunnr.
s_export-paym_amount = s_data-payment-wrbtr.
s_export-cry = s_data-payment-waers.
*{ REPLACE
RLQK900067
*\
s_export-paym_date = s_data-payment-cpudt.
s_export-paym_date = s_data-payment-budat.
*} REPLACE
APPEND s_export TO lt_export.
ENDLOOP.

DATA l_filestring TYPE string.


l_filestring = file.
DATA o_typedescr TYPE REF TO cl_abap_structdescr.
o_typedescr ?= cl_abap_structdescr=>describe_by_data( s_export ).
DATA t_comp TYPE cl_abap_structdescr=>included_view. "standard table of char30
.
DATA s_comp LIKE LINE OF t_comp.
DATA: BEGIN OF s_fields,
fieldname TYPE text30,
END OF s_fields.
DATA t_fields LIKE STANDARD TABLE OF s_fields.
t_comp = o_typedescr->get_included_view( 2 ).
LOOP AT t_comp INTO s_comp.
s_fields-fieldname = s_comp-name.
APPEND s_fields TO t_fields.
ENDLOOP.
CALL FUNCTION 'GUI_DOWNLOAD'
EXPORTING
*
BIN_FILESIZE
filename
filetype
*
APPEND

=
= l_filestring
= 'DAT'
= ' '

*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*

WRITE_FIELD_SEPARATOR
= ' '
HEADER
= '00'
TRUNC_TRAILING_BLANKS
= ' '
WRITE_LF
= 'X'
COL_SELECT
= ' '
COL_SELECT_MASK
= ' '
DAT_MODE
= ' '
CONFIRM_OVERWRITE
= ' '
NO_AUTH_CHECK
= ' '
CODEPAGE
= ' '
IGNORE_CERR
= ABAP_TRUE
REPLACEMENT
= '#'
WRITE_BOM
= ' '
TRUNC_TRAILING_BLANKS_EOL
= 'X'
WK1_N_FORMAT
= ' '
WK1_N_SIZE
= ' '
WK1_T_FORMAT
= ' '
WK1_T_SIZE
= ' '
WRITE_LF_AFTER_LAST_LINE
= ABAP_TRUE
SHOW_TRANSFER_STATUS
= ABAP_TRUE
IMPORTING
FILELENGTH
=
TABLES
data_tab
= lt_export
fieldnames
= t_fields
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 ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
EXIT.
ENDIF.

ENDFORM.

"export_data

*&---------------------------------------------------------------------*
*&
Form display_data
*&---------------------------------------------------------------------*

FORM display_data.
DATA o_disp TYPE REF TO lcl_disp_alv.
SET PF-STATUS 'MAIN'.
CREATE OBJECT o_disp.
o_disp->init_table( CHANGING t_table = gt_data ).
o_disp->hide_columns( ).
o_disp->color_columns( ).
o_disp->activate_all_functions( ).
o_disp->set_status( ).
o_disp->col_text( col_name = 'INVOICE-BELNR' text = 'NrDocFactura' ).
o_disp->col_text( col_name = 'PAYMENT-BELNR' text = 'NrDocIncas.' ).
o_disp->display( ).
ENDFORM.
"display_data