REPORT zrfi050 NO STANDARD PAGE HEADING LINE-COUNT 65 LINE-SIZE  255.

*Declaration tables for this program
TABLES: usr01,vbkd,bsid,bkpf.
*Data declaration: ALV parameters
TYPE-POOLS: slis.
 
DATA: gt_fieldcat  TYPE slis_t_fieldcat_alv,"Field catalogue
      gt_sp_group  TYPE slis_t_sp_group_alv."Groups for list display
DATA: gt_layout TYPE slis_layout_alv.
DATA: gt_events TYPE slis_t_event.
 
DATA: es_excluding TYPE slis_t_extab.
*Constants for ALV
DATA: g_boxnam     TYPE slis_fieldname VALUE 'CHECK'.
DATA: g_repid      LIKE sy-repid.
DATA: g_variant    LIKE disvariant.
DATA: t_fieldcat   TYPE slis_t_fieldcat_alv,
      w_filedcat   LIKE LINE OF t_fieldcat.
DATA: t_listheader TYPE slis_t_listheader,
      w_listheader LIKE LINE OF t_listheader.
DATA: t_event      TYPE slis_t_event,
      w_event      LIKE LINE OF t_event.
DATA: t_sort       TYPE slis_t_sortinfo_alv.
DATA: x_sort       LIKE LINE OF t_sort.
DATA: gs_layout       TYPE slis_layout_alv.
*DATA  g_status        TYPE slis_formname VALUE 'STANDARD'.
DATA: g_user_command  TYPE slis_formname VALUE 'MYCOMMAND'.
 
DATA: l_itcpo LIKE itcpo.
* printer control setting
DATA: l_prn_comp TYPE ssfcompop,
      l_prn_ctrlop TYPE ssfctrlop,
      job_output_options TYPE ssfcresop,
      job_output_info TYPE ssfcrescl.
 
DATA: BEGIN OF it_company OCCURS 0,
  bukrs LIKE t001-bukrs,
  adrnr LIKE t001-adrnr,
  name1 LIKE adrc-name1,
  name2 LIKE adrc-name2,
  name3 LIKE adrc-name3,
  name4 LIKE adrc-name4,
  street LIKE adrc-street,
  str_suppl1 LIKE adrc-str_suppl1,
  str_suppl2 LIKE adrc-str_suppl2,
  str_suppl3 LIKE adrc-str_suppl3,
  tel_number LIKE adrc-tel_number,
  fax_number LIKE adrc-fax_number,
END OF it_company.
 
DATA: BEGIN OF it_customer OCCURS 0,
  kunnr LIKE kna1-kunnr,
  adrnr LIKE kna1-adrnr,
  name1 LIKE adrc-name1,
  name2 LIKE adrc-name2,
  name3 LIKE adrc-name3,
  name4 LIKE adrc-name4,
  street LIKE adrc-street,
  str_suppl1 LIKE adrc-str_suppl1,
  str_suppl2 LIKE adrc-str_suppl2,
  str_suppl3 LIKE adrc-str_suppl3,
  tel_number LIKE adrc-tel_number,
  fax_number LIKE adrc-fax_number,
END OF it_customer.
 
DATA: BEGIN OF gt_result OCCURS 0,
  check TYPE c,
  bukrs LIKE bsid-bukrs,
  gjahr LIKE bsid-gjahr,
  buzei LIKE bsid-buzei,
  kunnr LIKE bsid-kunnr,
  belnr LIKE bsid-belnr,
  xblnr LIKE bkpf-xblnr,
  budat LIKE bkpf-budat,
  zfbdt LIKE bsid-zfbdt, "用于到期日计算的基准日期
  rebzg LIKE bsid-rebzg,
  rebzj LIKE bsid-rebzj,
  rebzz LIKE bsid-rebzz,
  waers LIKE bsid-waers,
  wrbtr LIKE bsid-wrbtr,
  outst LIKE bsid-wrbtr,
  zterm LIKE bsid-zterm,
  ztag1 LIKE t052-ztag1,
  text1 LIKE t052u-text1,
  awkey LIKE bkpf-awkey,
  vbeln LIKE vbrk-vbeln,
  fkdat LIKE vbrk-fkdat,
  vbelv LIKE vbfa-vbelv,
  bstkd LIKE vbkd-bstkd,
  sbino TYPE string,
  total TYPE bsid-wrbtr,
  remark TYPE string,
  shkzg LIKE bsid-shkzg,
  c_wrbtr TYPE string,
  c_outst TYPE string,
 
  com_name1 LIKE adrc-name1,
  com_street LIKE adrc-street,
  com_str_suppl1 LIKE adrc-str_suppl1,
  com_str_suppl2 LIKE adrc-str_suppl2,
  com_str_suppl3 LIKE adrc-str_suppl3,
  com_tel_number LIKE adrc-tel_number,
  com_fax_number LIKE adrc-fax_number,
 
  cus_contact LIKE knvk-name1,
  cus_name1 LIKE adrc-name1,
  cus_street LIKE adrc-street,
  cus_str_suppl1 LIKE adrc-str_suppl1,
  cus_str_suppl2 LIKE adrc-str_suppl2,
  cus_str_suppl3 LIKE adrc-str_suppl3,
  cus_tel_number LIKE adrc-tel_number,
  cus_fax_number LIKE adrc-fax_number,
 
END OF gt_result.
 
DATA:it_kunnr LIKE gt_result OCCURS 0 WITH HEADER LINE.
DATA:it_bsid LIKE gt_result OCCURS 0 WITH HEADER LINE.
DATA:it_result LIKE gt_result OCCURS 0 WITH HEADER LINE.
DATA:gt_result_print LIKE gt_result OCCURS 0 WITH HEADER LINE.
DATA:gt_result_print_tmp LIKE gt_result OCCURS 0 WITH HEADER LINE.
*----------------------------------------------------------------------*
*Define the selection-screen
SELECTION-SCREEN BEGIN OF BLOCK b1 WITH FRAME TITLE text-001.
PARAMETERS:     p_bukrs LIKE bsid-bukrs OBLIGATORY DEFAULT '3230'.
SELECT-OPTIONS: s_kunnr FOR bsid-kunnr.
SELECT-OPTIONS: s_budat FOR bkpf-budat.
SELECT-OPTIONS: s_belnr FOR bsid-belnr.
SELECTION-SCREEN END OF BLOCK b1.
*----------------------------------------------------------------------*
* INITIALIZATION
*----------------------------------------------------------------------*
INITIALIZATION.
 
AT SELECTION-SCREEN.
 
TOP-OF-PAGE.
 
END-OF-PAGE.
*----------------------------------------------------------------------*
* START-OF-SELECTION
*----------------------------------------------------------------------*
START-OF-SELECTION.
  PERFORM get_data.
 
END-OF-SELECTION.
  PERFORM output_data.
*&---------------------------------------------------------------------*
*&      Form  GET_DATA
*&---------------------------------------------------------------------*
FORM get_data.
  SELECT a~bukrs a~gjahr a~buzei a~kunnr a~belnr a~zfbdt b~xblnr b~budat a~waers a~wrbtr a~zterm b~awkey a~shkzg INTO CORRESPONDING FIELDS OF TABLE gt_result
    FROM bsid AS a INNER JOIN bkpf AS b ON a~bukrs = b~bukrs AND a~belnr = b~belnr AND a~gjahr = b~gjahr
    WHERE a~bukrs = p_bukrs AND a~kunnr IN s_kunnr AND a~belnr IN s_belnr AND b~budat IN s_budat AND a~zumsk = ''.
*    WHERE a~bukrs = p_bukrs AND a~kunnr IN s_kunnr AND a~belnr IN s_belnr AND b~budat IN s_budat AND a~belnr NOT LIKE '14%' AND a~zumsk = ''.
 
  SELECT bukrs gjahr buzei belnr budat wrbtr rebzg rebzj rebzz shkzg INTO CORRESPONDING FIELDS OF TABLE it_bsid FROM bsid
    FOR ALL ENTRIES IN gt_result
    WHERE bukrs = gt_result-bukrs AND rebzg = gt_result-belnr AND rebzj = gt_result-gjahr AND rebzz = gt_result-buzei.
 
  SELECT a~bukrs b~name1 b~street b~str_suppl1 b~str_suppl2 b~str_suppl3 b~tel_number b~fax_number
     INTO CORRESPONDING FIELDS OF TABLE it_company
    FROM t001 AS a INNER JOIN adrc AS b ON a~adrnr = b~addrnumber
    WHERE a~bukrs = p_bukrs.
  READ TABLE it_company INDEX 1.
 
  APPEND LINES OF gt_result TO it_kunnr.
  DELETE ADJACENT DUPLICATES FROM it_kunnr COMPARING kunnr.
  SELECT a~kunnr b~name1 b~street b~str_suppl1 b~str_suppl2 b~str_suppl3 b~tel_number b~fax_number
     INTO CORRESPONDING FIELDS OF TABLE it_customer
    FROM kna1 AS a INNER JOIN adrc AS b ON a~adrnr = b~addrnumber
    FOR ALL ENTRIES IN it_kunnr
    WHERE kunnr = it_kunnr-kunnr.
 
  LOOP AT gt_result.
    SELECT SINGLE text1 INTO gt_result-text1 FROM t052u WHERE zterm = gt_result-zterm AND spras = '1'.
    SELECT SINGLE fkdat INTO gt_result-fkdat FROM vbrk WHERE vbeln = gt_result-awkey.
    IF sy-subrc EQ 0.
      SELECT SINGLE vbelv INTO gt_result-vbelv FROM vbfa WHERE vbeln = gt_result-awkey AND vbtyp_v = 'C'.
      IF sy-subrc EQ 0.
        SELECT SINGLE bstkd INTO gt_result-bstkd FROM vbkd WHERE vbeln = gt_result-vbelv.
        IF gt_result-bukrs = '3130'.
          PERFORM sub_get_sbi USING gt_result-awkey CHANGING gt_result-sbino.
        ENDIF.
      ENDIF.
    ELSE.
      gt_result-fkdat = gt_result-budat.
    ENDIF.
 
    IF gt_result-shkzg EQ 'H'.
      gt_result-wrbtr = gt_result-wrbtr * -1.
    ENDIF.
 
    READ TABLE it_bsid WITH KEY bukrs = gt_result-bukrs rebzg = gt_result-belnr rebzj = gt_result-gjahr rebzz = gt_result-buzei.
    IF sy-subrc EQ 0.
      IF it_bsid-shkzg EQ 'H'.
        it_bsid-wrbtr = it_bsid-wrbtr * -1.
      ENDIF.
      gt_result-outst = gt_result-wrbtr + it_bsid-wrbtr.
      DELETE gt_result WHERE bukrs = it_bsid-bukrs AND belnr = it_bsid-belnr AND gjahr = it_bsid-gjahr AND buzei = it_bsid-buzei.
    ELSE.
      gt_result-outst = gt_result-wrbtr.
    ENDIF.
 
    CALL FUNCTION 'ZFI_GET_DUE_DATE'
      EXPORTING
        p_bukrs = gt_result-bukrs
        p_belnr = gt_result-belnr
        p_gjahr = gt_result-gjahr
        p_koart = 'D'
      IMPORTING
        p_faedt = gt_result-zfbdt.
 
    gt_result-com_name1 = it_company-name1.
    gt_result-com_street = it_company-street.
    gt_result-com_str_suppl1 = it_company-str_suppl1.
    gt_result-com_str_suppl2 = it_company-str_suppl2.
    gt_result-com_str_suppl3 = it_company-str_suppl3.
    gt_result-com_tel_number = it_company-tel_number.
    gt_result-com_fax_number = it_company-fax_number.
 
    READ TABLE it_customer WITH KEY kunnr = gt_result-kunnr BINARY SEARCH.
    IF sy-subrc EQ 0.
      SELECT SINGLE name1 INTO gt_result-cus_contact FROM knvk WHERE kunnr = gt_result-kunnr.
      gt_result-cus_name1 = it_customer-name1.
      gt_result-cus_street = it_customer-street.
      gt_result-cus_str_suppl1 = it_customer-str_suppl1.
      gt_result-cus_str_suppl2 = it_customer-str_suppl2.
      gt_result-cus_str_suppl3 = it_customer-str_suppl3.
      gt_result-cus_tel_number = it_customer-tel_number.
      gt_result-cus_fax_number = it_customer-fax_number.
    ENDIF.
 
    gt_result-c_wrbtr = gt_result-wrbtr.
    gt_result-c_outst = gt_result-outst.
 
    CALL FUNCTION 'CLOI_PUT_SIGN_IN_FRONT'
      CHANGING
        value = gt_result-c_wrbtr.
 
    CALL FUNCTION 'CLOI_PUT_SIGN_IN_FRONT'
      CHANGING
        value = gt_result-c_outst.
 
    MODIFY gt_result.
 
  ENDLOOP.
*  SORT gt_result BY vbeln posnr.
*  DELETE gt_result WHERE k_qty = 0.
*  APPEND LINES OF gt_result TO it_result.
ENDFORM.                    "GET_DATA
 
 
*&---------------------------------------------------------------------*
*&      Form  OUTPUT_DATA
*&---------------------------------------------------------------------*
FORM output_data.
  IF gt_result[] IS INITIAL.
    MESSAGE 'No data!' TYPE 'S'.
    STOP.
  ELSE.
    CLEAR gt_fieldcat.
    PERFORM get_fields_attribute USING 'GT_RESULT' .
    PERFORM get_alv_fields USING 'GT_RESULT'.
    PERFORM output_detail_alv.
  ENDIF.
ENDFORM.                    "OUTPUT_DATA
 
*&---------------------------------------------------------------------*
*&      Form  GET_FIELDS_ATTRIBUTE
*&---------------------------------------------------------------------*
FORM get_fields_attribute USING p_table .
  PERFORM change_field_name USING 'CHECK'  p_table 'Print' '' '' 'X' 'X' '' '' ''.
  PERFORM change_field_name USING 'BUKRS'  p_table 'Company Code' '' '' '' '' '' '' ''.
  PERFORM change_field_name USING 'KUNNR'  p_table 'Customer' '' '' '' '' '' '' ''.
  PERFORM change_field_name USING 'BELNR'  p_table 'Doc No.' '' '' '' '' '' '' ''.
  PERFORM change_field_name USING 'XBLNR'  p_table 'Inv. No.' '' '' '' '' '' '' ''.
  PERFORM change_field_name USING 'SBINO'  p_table 'SBI No.' '' '' '' '' '' '' ''.
  PERFORM change_field_name USING 'FKDAT'  p_table 'Inv. Date' '' '' '' '' '' '' ''.
  PERFORM change_field_name USING 'TEXT1'  p_table 'Payment Terms' '' '' '' '' '' '' ''.
  PERFORM change_field_name USING 'ZFBDT'  p_table 'Due date ' '' '' '' '' '' '' ''.
  PERFORM change_field_name USING 'WAERS'  p_table 'Cur.' '' '' '' '' '' '' ''.
  PERFORM change_field_name USING 'WRBTR'  p_table 'Inv. Amount' '' '' '' '' '' '' ''.
  PERFORM change_field_name USING 'OUTST'  p_table 'Outstanding Amount' '' '' '' '' '' '' ''.
  PERFORM change_field_name USING 'VBELV'  p_table 'SO No.' '' '' '' '' '' '' ''.
  PERFORM change_field_name USING 'BSTKD'  p_table 'PO No.' '' '' '' '' '' '' ''.
  PERFORM change_field_name USING 'REMARK'  p_table '   Remark   ' '' '' 'X' '' '' '' ''.
 
  PERFORM build_alv_sort USING:  'BUKRS' p_table 'X' '' 'X' ''.
  PERFORM build_alv_sort USING:  'KUNNR' p_table 'X' '' 'X' ''.
ENDFORM.                    "GET_FIELDS_ATTRIBUTE
 
*&---------------------------------------------------------------------*
*&      Form  build_alv_sort
*&---------------------------------------------------------------------*
FORM build_alv_sort USING fname fitab fup fdown fsubt fsum.
  DATA: zsort TYPE slis_sortinfo_alv.
  CLEAR zsort.
  zsort-fieldname = fname.
  zsort-tabname   = fitab.
  zsort-up = fup.
  zsort-down = fdown.
  zsort-subtot = fsubt.
  APPEND zsort TO t_sort.
ENDFORM.                    "build_alv_sort
 
*&---------------------------------------------------------------------*
*&      Form  CHANGE_FIELD_NAME
*&---------------------------------------------------------------------*
FORM change_field_name  USING pl_field pl_itab   pl_text pl_sum pl_fix pl_edit  pl_checkbox pl_input  pl_ref_fieldname  pl_ref_tabname.
  DATA  lt_fieldcat TYPE slis_fieldcat_alv.
  lt_fieldcat-fieldname    = pl_field.
  lt_fieldcat-tabname      = pl_itab.
  lt_fieldcat-reptext_ddic   = pl_text.
  lt_fieldcat-seltext_l      = pl_text.
  lt_fieldcat-seltext_m      = pl_text.
  lt_fieldcat-seltext_s      = pl_text.
  lt_fieldcat-do_sum         = pl_sum.
  lt_fieldcat-fix_column     = pl_fix.
  lt_fieldcat-edit_mask      = ''.
  lt_fieldcat-edit           = pl_edit.
  lt_fieldcat-checkbox       = pl_checkbox. "the column is checkbox
  lt_fieldcat-input          = pl_input.    "the column can be edited in ALV
  lt_fieldcat-ddictxt        = 'L'.
  lt_fieldcat-ref_fieldname           = pl_ref_fieldname.
  lt_fieldcat-ref_tabname          = pl_ref_tabname.
*  lt_fieldcat-no_zero = '1' .
  APPEND lt_fieldcat TO gt_fieldcat.
  CLEAR: lt_fieldcat.
ENDFORM.                    "CHANGE_FIELD_NAME
 
*&---------------------------------------------------------------------*
*&      Form  GET_ALV_FIELDS
*&---------------------------------------------------------------------*
FORM get_alv_fields  USING    p_itabn.
  CALL FUNCTION 'REUSE_ALV_FIELDCATALOG_MERGE'
    EXPORTING
      i_program_name         = sy-repid
      i_internal_tabname     = p_itabn
    CHANGING
      ct_fieldcat            = gt_fieldcat
    EXCEPTIONS
      inconsistent_interface = 1
      program_error          = 2
      OTHERS                 = 3.
ENDFORM.                    " GET_ALV_FIELDS
 
*&---------------------------------------------------------------------*
*&      Form  OUTPUT_DETAIL_ALV
*&---------------------------------------------------------------------*
FORM output_detail_alv .
 
  " for print button
  DATA  i_event_exit TYPE slis_t_event_exit.
  DATA gw_event_exit       LIKE LINE OF i_event_exit.
  DATA gt_event_exit TYPE slis_t_event_exit.
  gw_event_exit-ucomm = '&ALL'. "User command for print.
  gw_event_exit-before = 'X'.
  APPEND gw_event_exit TO gt_event_exit.
  gw_event_exit-ucomm = '&SAL'. "User command for print.
 
  APPEND gw_event_exit TO gt_event_exit.
 
  gt_layout-zebra = 'X'.
  gt_layout-colwidth_optimize = 'X'.
  gt_layout-box_fieldname = g_boxnam.
 
*  DELETE ADJACENT DUPLICATES FROM gt_result COMPARING mblnr matnr.
 
  CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
    EXPORTING
      i_callback_program      = sy-cprog
      i_callback_user_command = 'USER_COMMAND'
      i_callback_pf_status_set   = 'SET_PF_STATUS'
*      i_structure_name        = 'GT_RESULT'
      it_fieldcat             = gt_fieldcat[]
      it_sort                 = t_sort
*I_DEFAULT                      = 'X'
      i_save                  = 'A'
      is_layout               = gt_layout
      it_events               = gt_events[]
    it_event_exit             = gt_event_exit
    TABLES
      t_outtab                = gt_result
    EXCEPTIONS
      program_error           = 1
      OTHERS                  = 2.
ENDFORM.                    "OUTPUT_DETAIL_ALV
 
*&---------------------------------------------------------------------*
*&      Form  SET_PF_STATUS1
*&---------------------------------------------------------------------*
FORM set_pf_status  USING  extab TYPE slis_t_extab.
  DATA: r_ucomm LIKE sy-ucomm.
  SET PF-STATUS 'ZSTANDARD'   ."EXCLUDING ES_EXCLUDING.
ENDFORM.                    "SET_PF_STATUS
 
*&---------------------------------------------------------------------*
*&      Form  sub_call_openform
*&---------------------------------------------------------------------*
FORM sub_call_openform .
  CALL FUNCTION 'SSF_OPEN'
    EXPORTING
*    ARCHIVE_PARAMETERS       = ARCHIVE_PARAMETERS
     user_settings            = ' '
     output_options           = l_prn_comp
     control_parameters       = l_prn_ctrlop
   IMPORTING
     job_output_options       = job_output_options
   EXCEPTIONS
     formatting_error         = 1
     internal_error           = 2
     send_error               = 3
     user_canceled            = 4
     OTHERS                   = 5.
  IF sy-subrc <> 0.
*    MESSAGE 'User Cancelled' TYPE 'W'.
  ENDIF.
ENDFORM.                    " sub_call_openform
 
*&---------------------------------------------------------------------*
*&      Form  sub_call_closeform
*&---------------------------------------------------------------------*
FORM sub_call_closeform .
  CALL FUNCTION 'SSF_CLOSE'
    IMPORTING
      job_output_info  = job_output_info
    EXCEPTIONS
      formatting_error = 1
      internal_error   = 2
      send_error       = 3
      OTHERS           = 4.
ENDFORM.                    " sub_call_closeform
 
*&---------------------------------------------------------------------*
*&      Form  USER_COMMAND
*&---------------------------------------------------------------------*
FORM user_command USING rf_ucomm    LIKE sy-ucomm
                        rs_selfield TYPE slis_selfield .
  FIELD-SYMBOLS: <fs>,<fs1>,<fs2>.
  ASSIGN COMPONENT  6 OF STRUCTURE rs_selfield TO <fs>.
  ASSIGN COMPONENT 14 OF STRUCTURE rs_selfield TO <fs1>.
  ASSIGN COMPONENT  2 OF STRUCTURE rs_selfield TO <fs2>.
  DATA: wa_analy LIKE gt_result.
  READ TABLE gt_result INDEX <fs2> INTO wa_analy.
  DATA: index        TYPE i.
  DATA: answer       TYPE c.
  DATA: refresh_list TYPE c.
  DATA: cursor_pos   LIKE sy-tabix.
  rs_selfield-tabindex = sy-tabix.
 
*---------------------------------------------------------------------------
  DATA ref TYPE REF TO cl_gui_alv_grid.
  CALL FUNCTION 'GET_GLOBALS_FROM_SLVC_FULLSCR'
    IMPORTING
      e_grid = ref.
  CALL METHOD ref->check_changed_data.
  rs_selfield-refresh = 'X'.
*--------------------------------------------------------------------------
 
  CASE rf_ucomm.
    WHEN 'ZPRINT'.
      PERFORM set_data_to_print.
      PERFORM print_form.
    WHEN 'ZPRINTPDF'.
      PERFORM set_data_to_print.
      PERFORM download_pdf.
    WHEN 'RETURN' OR 'BACK' OR 'CANCE'.
      LEAVE TO SCREEN 0.
  ENDCASE.
ENDFORM.                    "USER_COMMAND
 
 
*&---------------------------------------------------------------------*
*&      Form  set_data_to_print
*&---------------------------------------------------------------------*
FORM set_data_to_print.
  CLEAR:  gt_result_print.
  REFRESH:gt_result_print.
 
  LOOP AT gt_result WHERE check EQ 'X'.
*    gt_result-zneed = gt_result-bdmng - gt_result-enmng.
    MOVE-CORRESPONDING gt_result TO gt_result_print.
    APPEND gt_result_print.
  ENDLOOP.
 
*  remove the select when you go back.  added by mansontang20090409
  LOOP AT gt_result WHERE check NE ''.
    gt_result-check = ''.
    MODIFY gt_result.
  ENDLOOP.
 
* --------------Delete the DUPLICATES data
*  DELETE ADJACENT DUPLICATES FROM gt_result_print.
 
  IF gt_result_print[] IS INITIAL.
    MESSAGE 'Please choose!' TYPE 'E'.
  ELSE." choosed print
 
    PERFORM sub_set_printer_control.
    l_prn_ctrlop-no_open = 'X'.
    l_prn_ctrlop-no_close = 'X'.
 
    REFRESH : it_result , gt_result_print_tmp.
    APPEND LINES OF gt_result_print TO it_result.
    APPEND LINES OF gt_result_print TO gt_result_print_tmp.
    DELETE ADJACENT DUPLICATES FROM gt_result_print_tmp COMPARING kunnr.
 
  ENDIF.
ENDFORM.                    "set_data_to_print
 
*&---------------------------------------------------------------------*
*&      Form  print_form
*&---------------------------------------------------------------------*
FORM print_form.
  DATA l_function TYPE rs38l_fnam.
  CALL FUNCTION 'SSF_FUNCTION_MODULE_NAME'
    EXPORTING
      formname = 'ZRFI050'
    IMPORTING
      fm_name  = l_function.
  PERFORM sub_call_openform.
 
 
  REFRESH gt_result_print.
  LOOP AT gt_result_print_tmp.
    REFRESH gt_result_print.
    LOOP AT it_result WHERE kunnr = gt_result_print_tmp-kunnr.
      APPEND it_result TO gt_result_print.
    ENDLOOP.
 
    CALL FUNCTION l_function
      EXPORTING
        control_parameters = l_prn_ctrlop
        output_options     = l_prn_comp
        user_settings      = ''        " using current setting
      EXCEPTIONS
        formatting_error   = 1
        internal_error     = 2
        send_error         = 3
        user_canceled      = 4
        OTHERS             = 5.
    IF sy-subrc <> 0.ENDIF.
 
  ENDLOOP.
 
 
  PERFORM sub_call_closeform.
ENDFORM.                    "print_form
 
*&---------------------------------------------------------------------*
*&      Form  sub_set_printer_control
*&---------------------------------------------------------------------*
FORM sub_set_printer_control .
  CLEAR usr01.
  SELECT SINGLE * FROM usr01 WHERE bname = sy-uname.
  l_prn_ctrlop-no_open = 'X'.
*  l_prn_ctrlop-no_close = 'X'.
* control setting
  l_prn_ctrlop-preview = 'X'.
  l_prn_ctrlop-no_dialog = 'X'.
 
*  IF sy-langu = '1' OR sy-langu = 'M'.
  l_prn_ctrlop-langu = '1'.
*  ENDIF.
  l_prn_ctrlop-device = 'PRINTER'.
* printer setting
  l_prn_comp-tdcopies = '1'.
  l_prn_comp-tdsuffix2 = sy-repid.
  l_prn_comp-tddest = usr01-spld.
***  l_prn_comp-tdprinter = 'PRINTER'.
  l_prn_comp-tdnewid = 'X'.
  l_prn_comp-tdimmed = 'X'. "print it now.
  l_prn_comp-tdfinal = 'X'.
* l_prn_comp-tddelete = 'X'.
ENDFORM.                    " sub_set_printer_control
 
 
*&---------------------------------------------------------------------*
*&      Form  download_pdf
*&---------------------------------------------------------------------*
FORM download_pdf.
* variable declarations
  DATA:
  w_cparam TYPE ssfctrlop,
  w_outoptions TYPE ssfcompop,
  w_bin_filesize TYPE i," Binary File Size
  w_filename TYPE string,
  w_file_path TYPE string,
  w_full_path TYPE string.
 
  CONCATENATE sy-cprog '-' sy-datum '-' sy-uzeit '.PDF' INTO w_filename.
* Internal tables declaration
* Internal table to hold the OTF data
  DATA:
  t_otf TYPE itcoo OCCURS 0 WITH HEADER LINE,
* Internal table to hold OTF data recd from the SMARTFORM
  t_otf_from_fm TYPE ssfcrescl,
* Internal table to hold the data from the FM CONVERT_OTF
  t_pdf_tab LIKE tline OCCURS 0 WITH HEADER LINE.
 
* Calling the SMARTFORM using the function module retrieved above
* GET_OTF parameter in the CONTROL_PARAMETERS is set to get the OTF
* format of the output
  w_cparam-no_dialog = 'X'.
  w_cparam-preview = space. " Suppressing the dialog box
  w_cparam-getotf = 'X'.                                      " for print preview
  w_cparam-langu = '1'.
* Printer name to be used is provided in the export parameter
* OUTPUT_OPTIONS
*  w_outoptions-tddest = 'LP01'.
  w_outoptions-tddest = 'DEFAULT'.
 
*&---------------------------------------------------------------------*
  DATA l_function TYPE rs38l_fnam.
  CALL FUNCTION 'SSF_FUNCTION_MODULE_NAME'
    EXPORTING
      formname = 'ZRFI050'
    IMPORTING
      fm_name  = l_function.
 
*&---------------------------------------------------------------------*
  REFRESH gt_result_print.
  LOOP AT gt_result_print_tmp.
    REFRESH gt_result_print.
    LOOP AT it_result WHERE kunnr = gt_result_print_tmp-kunnr.
      APPEND it_result TO gt_result_print.
    ENDLOOP.
 
    CALL FUNCTION l_function
      EXPORTING
        control_parameters = w_cparam
        output_options     = w_outoptions
      IMPORTING
        job_output_info    = t_otf_from_fm
      EXCEPTIONS
        formatting_error   = 1
        internal_error     = 2
        send_error         = 3
        user_canceled      = 4
        OTHERS             = 5.
    IF sy-subrc <> 0.
      MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
      WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
    ENDIF.
 
    APPEND LINES OF t_otf_from_fm-otfdata TO t_otf.
 
  ENDLOOP.
*  t_otf[] = t_otf_from_fm-otfdata[].
 
 
* Function Module CONVERT_OTF is used to convert the OTF format to PDF
  CALL FUNCTION 'CONVERT_OTF'
    EXPORTING
      format                = 'PDF'
    IMPORTING
      bin_filesize          = w_bin_filesize
    TABLES
      otf                   = t_otf
      lines                 = t_pdf_tab
    EXCEPTIONS
      err_max_linewidth     = 1
      err_format            = 2
      err_conv_not_possible = 3
      err_bad_otf           = 4
      OTHERS                = 5.
  IF sy-subrc <> 0.
    MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
    WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
  ENDIF.
 
* To display File SAVE dialog window
  CALL METHOD cl_gui_frontend_services=>file_save_dialog
    EXPORTING
      default_file_name    = w_filename
    CHANGING
      filename             = w_filename
      path                 = w_file_path
      fullpath             = w_full_path
    EXCEPTIONS
      cntl_error           = 1
      error_no_gui         = 2
      not_supported_by_gui = 3
      OTHERS               = 4.
  IF sy-subrc <> 0.
    MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
    WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
  ENDIF.
 
* Use the FM GUI_DOWNLOAD to download the generated PDF file onto the
* presentation server
  CALL FUNCTION 'GUI_DOWNLOAD'
    EXPORTING
      bin_filesize = w_bin_filesize
      filename     = w_full_path
      filetype     = 'BIN'
    TABLES
      data_tab     = t_pdf_tab.
  IF sy-subrc <> 0.
  ENDIF.
 
ENDFORM.                    "download_pdf
 
*&---------------------------------------------------------------------*
*&      Form  data_alpha_conversion
*&---------------------------------------------------------------------*
FORM data_alpha_conversion  USING  p_alpha.
  CALL FUNCTION 'CONVERSION_EXIT_ALPHA_INPUT'
    EXPORTING
      input  = p_alpha
    IMPORTING
      output = p_alpha.
ENDFORM.                    "data_alpha_conversion
 
*&---------------------------------------------------------------------*
*&      Form  GET_TEXT
*&---------------------------------------------------------------------*
FORM get_text  USING im_object TYPE thead-tdobject
                     im_id     TYPE thead-tdid
                     im_tdname  TYPE thead-tdname
                     CHANGING   p_ltext.
 
  DATA:    i_line    LIKE tline OCCURS 0 WITH HEADER LINE.
  DATA:    l_langu LIKE sy-langu.
  l_langu = 'E'.
  CLEAR:p_ltext.
  CALL FUNCTION 'READ_TEXT'
    EXPORTING
      id                      = im_id
      language                = l_langu
      name                    = im_tdname
      object                  = im_object
    TABLES
      lines                   = i_line
    EXCEPTIONS
      id                      = 1
      language                = 2
      name                    = 3
      not_found               = 4
      object                  = 5
      reference_check         = 6
      wrong_access_to_archive = 7
      OTHERS                  = 8.
 
  CLEAR: i_line.
  p_ltext = ''.
  LOOP AT i_line.
    CONCATENATE p_ltext i_line-tdline+0(50) INTO p_ltext SEPARATED BY space.
    CONDENSE p_ltext.
  ENDLOOP.
ENDFORM.                    "GET_TEXT
 
*&---------------------------------------------------------------------*
*&      Form  SUB_GET_SBI
*&---------------------------------------------------------------------*
FORM sub_get_sbi USING p_billing CHANGING p_sbi.
  DATA:l_vbelv LIKE vbfa-vbelv.
  DATA: l_tname LIKE thead-tdname.
  SELECT SINGLE vbelv INTO l_vbelv FROM vbfa WHERE vbeln = p_billing AND vbtyp_v = 'C'.
  l_tname = l_vbelv.
  IF l_vbelv IS NOT INITIAL.
    PERFORM get_text USING 'VBBK' 'S039' l_tname CHANGING p_sbi.
  ENDIF.
ENDFORM.                    "SUB_GET_SBI