Wednesday, November 30, 2016

send pdf file to ftp server

REPORT  zpdfsendtoftp.


DATA BEGIN OF t_vbak OCCURS 0,
         vbeln TYPE vbrk-vbeln,
         vkorg TYPE vbak-vkorg,
         vkbur TYPE vbak-vkbur,
       END OF t_vbak.

DATAfm_name TYPE rs38l_fnam,
            p_form  TYPE tdsfname   VALUE 'ZTEST1'.   "smartforms name

DATAcarr_id       TYPE sbook-carrid,
           cparam        TYPE ssfctrlop,
           outop         TYPE ssfcompop,
           tab_otf_data  TYPE ssfcrescl,
           pdf_tab       LIKE tline OCCURS WITH HEADER LINE,
           tab_otf_final TYPE itcoo OCCURS WITH HEADER LINE,
          file_size     TYPE i,
          bin_filesize  TYPE i,
          file_name     TYPE string,
         file_path     TYPE string,
         full_path     TYPE string.


PARAMETERS vbeln TYPE vbak-vbeln.   " input parameter

SELECT vbeln vkorg vkbur FROM vbak INTO TABLE t_vbak WHERE vbeln vbeln.

outop-tddest 'LP01'.
cparam-no_dialog 'X'.
cparam-preview space.
cparam-getotf 'X'.


CALL FUNCTION 'SSF_FUNCTION_MODULE_NAME'
  EXPORTING
    formname           p_form
  IMPORTING
    fm_name            fm_name
  EXCEPTIONS
    no_form            1
    no_function_module 2
    OTHERS             3.

CALL FUNCTION fm_name
  EXPORTING
    control_parameters cparam
    output_options     outop
    user_settings      space
    vbeln              vbeln
  IMPORTING
*   DOCUMENT_OUTPUT_INFO       =
    job_output_info    tab_otf_data
  TABLES
    t_vbak             t_vbak.


tab_otf_final[] tab_otf_data-otfdata[].

CALL FUNCTION 'CONVERT_OTF'
  EXPORTING
    format                'PDF'
    max_linewidth         132
  IMPORTING
    bin_filesize          bin_filesize
  TABLES
    otf                   tab_otf_final
    lines                 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.


"""""""""""" send pdf file to server """""""""""
DATA i_tab_converted_data  TYPE STANDARD TABLE OF char200 WITH HEADER LINE.

TYPES:
  BEGIN OF ty_ftp,
    zcountry  TYPE zftpi-zcountry,
    zuser     TYPE zftpi-zuser,
    zpasswd   TYPE zftpi-zpasswd,
    zhost     TYPE zftpi-zhost,
    zrfc_dest TYPE zftpi-zrfc_dest,
    zapp      TYPE zftpi-zapp,
    zftp      TYPE zftpi-zftp,
  END OF ty_ftp,
  BEGIN OF xml_line,
    data(256TYPE x,
  END OF xml_line.

DATAlt_xml_table      TYPE TABLE OF xml_line,
      l_xml_size        TYPE i,
      l_rc              TYPE i,
      lv_success        TYPE VALUE 1,
      l_element_package TYPE REF TO if_ixml_element,
      l_element_details TYPE REF TO if_ixml_element,
      l_value           TYPE string.


DATAresult   TYPE TABLE OF text WITH HEADER LINE,
      commands TYPE TABLE OF text WITH HEADER LINE.

DATATYPE likp-ablad.
DATAcountry TYPE char2.
DATA :rec                          TYPE xml_line."Adjust as per requirement/OS limits
DATA filepath(128)                TYPE c.

DATAw_hdl         TYPE i,
      c_key         TYPE VALUE 26101957,
      l_slen        TYPE i,
      command_index TYPE i.
DATA l_interface_key TYPE char30,
       l_filename      TYPE char200,
       l_content_type  TYPE char10,
       lv_command(200TYPE c.

DATAt_time  TYPE sy-uzeit,
      lv_move TYPE c,
      lv_pick TYPE c,
      lv_pack TYPE c.


DATA:  l_user(30TYPE VALUE 'ftptest123'"user name of ftp server
       l_pwd(30)  TYPE VALUE 'pass123'"password of ftp server

       l_host(64TYPE VALUE 'enter host'"ip address of FTP server
       l_dest     LIKE rfcdes-rfcdest VALUE 'SAPFTPA'"Background RFC destination

filepath '/home'.
OPEN DATASET filepath FOR OUTPUT IN BINARY MODE.
IF sy-subrc 0.
  LOOP AT lt_xml_table INTO rec.
    TRANSFER rec TO filepath.
  ENDLOOP.
ENDIF.
CLOSE DATASET filepath.

CONCATENATE '/' 'test' '.pdf' INTO l_filename.  " file name

CONCATENATE 'put' filepath l_filename INTO lv_command SEPARATED BY space.
*HTTP_SCRAMBLE: used to scramble the password provided in a format recognized by SAP.
SET EXTENDED CHECK OFF.
l_slen strlenl_pwd ).

CALL FUNCTION 'HTTP_SCRAMBLE'
  EXPORTING
    source      l_pwd
    sourcelen   l_slen
    key         c_key
  IMPORTING
    destination l_pwd.

* To Connect to the Server using FTP
CALL FUNCTION 'FTP_CONNECT'
  EXPORTING
    user            l_user
    password        l_pwd
    host            l_host
    rfc_destination l_dest
  IMPORTING
    handle          w_hdl
  EXCEPTIONS
    OTHERS          1.

IF sy-subrc <> 0.
  lv_success 0.
  EXIT.
ENDIF.


REFRESH commands.

*  ***connecting to the ftp server
CALL FUNCTION 'FTP_R3_TO_SERVER'
  EXPORTING
    handle        w_hdl
    fname         l_filename
    blob_length   bin_filesize
  TABLES
    blob          pdf_tab
  EXCEPTIONS
    tcpip_error   1
    command_error 2
    data_error    3
    OTHERS        4.

* To disconnect the FTP
CALL FUNCTION 'FTP_DISCONNECT'
  EXPORTING
    handle w_hdl.

*RFC_CONNECTION_CLOSE:This is used to disconnect the RFC connection between SAP and other system.

CALL FUNCTION 'RFC_CONNECTION_CLOSE'
  EXPORTING
    destination l_dest
  EXCEPTIONS
    OTHERS      1.


WRITE'pdf file send successfully'.

Wednesday, November 2, 2016

How to Color row in ALV report



REPORT  ZROWCOLOR.

TABLES:     vbak.

TYPE-poolsslis.                                 "ALV Declarations
TYPESBEGIN OF t_vbak,
  vbeln TYPE vbak-vbeln,
  erdat TYPE vbak-erdat,
  netwr TYPE vbak-netwr,
 CELLCOLOR TYPE LVC_T_SCOL,
END OF t_vbak.

DATAit_vbak TYPE STANDARD TABLE OF t_vbak INITIAL SIZE 0,
      wa_vbak TYPE t_vbak.

*ALV data declarations
DATAfieldcatalog TYPE slis_t_fieldcat_alv WITH HEADER LINE,
      gd_tab_group TYPE slis_t_sp_group_alv,
      gd_layout    TYPE slis_layout_alv,
      gd_repid     LIKE sy-repid,
      gt_events     TYPE slis_t_event,
      gd_prntparams TYPE slis_print_alv.

START-OF-SELECTION.

PERFORM fill_data.
PERFORM build_fieldcatalog.
PERFORM build_layout.
PERFORM set_cell_colours.
PERFORM display_alv_report.

*&---------------------------------------------------------------------*
*&      Form  BUILD_FIELDCATALOG
*&---------------------------------------------------------------------*
*       Build Fieldcatalog for ALV Report
*----------------------------------------------------------------------*
FORM build_fieldcatalog.

  fieldcatalog-fieldname   'VBELN'.
  fieldcatalog-seltext_m   'Sales Order'.
  fieldcatalog-col_pos     0.
  fieldcatalog-outputlen   10.
  fieldcatalog-emphasize   'X'.
  fieldcatalog-KEY         'X'.
  APPEND fieldcatalog TO fieldcatalog.
  CLEAR  fieldcatalog.

  fieldcatalog-fieldname   'ERDAT'.
  fieldcatalog-seltext_m   'Created Date  '.
  fieldcatalog-col_pos     1.
  APPEND fieldcatalog TO fieldcatalog.
  CLEAR  fieldcatalog.

  fieldcatalog-fieldname   'NETWR'.
  fieldcatalog-seltext_m   'Document Value'.
  fieldcatalog-col_pos     2.
  APPEND fieldcatalog TO fieldcatalog.
  CLEAR  fieldcatalog.

ENDFORM.                    " BUILD_FIELDCATALOG

*&---------------------------------------------------------------------*
*&      Form  BUILD_LAYOUT
*&---------------------------------------------------------------------*
*       Build layout for ALV grid report
*----------------------------------------------------------------------*
FORM build_layout.
  gd_layout-no_input          'X'.
  gd_layout-colwidth_optimize 'X'.
  gd_layout-totals_text       'Totals'(201).
  gd_LAYOUT-coltab_fieldname 'CELLCOLOR'.  "CTAB_FNAME
ENDFORM.                    " BUILD_LAYOUT


*&---------------------------------------------------------------------*
*&      Form  DISPLAY_ALV_REPORT
*&---------------------------------------------------------------------*
*       Display report using ALV grid
*----------------------------------------------------------------------*
FORM display_alv_report.
  gd_repid sy-repid.
  CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
  EXPORTING
    i_callback_program      gd_repid
    i_callback_top_of_page   'TOP-OF-PAGE'  "see FORM
    is_layout               gd_layout
    it_fieldcat             fieldcatalog[]
    i_save                  'X'

  TABLES
    t_outtab                it_vbak
  EXCEPTIONS
    program_error           1
    OTHERS                  2.
  IF sy-subrc <> 0.

  ENDIF.
ENDFORM.                    " DISPLAY_ALV_REPORT

*&---------------------------------------------------------------------*
*&      Form  DATA_RETRIEVAL
*&---------------------------------------------------------------------*
*       Retrieve data form vbak table and populate itab it_vbak
*----------------------------------------------------------------------*
FORM fill_data.

  SELECT vbeln erdat netwr
  UP TO 10 ROWS
  FROM vbak
  INTO CORRESPONDING FIELDS OF TABLE it_vbak.
ENDFORM.                    " DATA_RETRIEVAL


*-------------------------------------------------------------------*
* Form  TOP-OF-PAGE                                                 *
*-------------------------------------------------------------------*
* ALV Report Header                                                 *
*-------------------------------------------------------------------*
FORM top-OF-PAGE.
*ALV Header declarations
  DATAt_header TYPE slis_t_listheader,
        wa_header TYPE slis_listheader,
        t_line LIKE wa_header-info,
        ld_lines TYPE I,
        ld_linesc(10TYPE C.

* Title
  wa_header-typ  'H'.
  wa_header-info 'Sales Order Report'.
  APPEND wa_header TO t_header.
  CLEAR wa_header.

* Date
  wa_header-typ  'S'.
  wa_header-KEY 'Date: '.
  CONCATENATE  sy-datum+6(2'.'
  sy-datum+4(2'.'
  sy-datum(4INTO wa_header-info.   "todays date
  APPEND wa_header TO t_header.
  CLEARwa_header.

* Total No. of Records Selected
  DESCRIBE TABLE it_vbak LINES ld_lines.
  ld_linesc ld_lines.
  CONCATENATE 'Total No. of Records Selected: ' ld_linesc
  INTO t_line SEPARATED BY space.
  wa_header-typ  'A'.
  wa_header-info t_line.
  APPEND wa_header TO t_header.
  CLEARwa_headert_line.

  CALL FUNCTION 'REUSE_ALV_COMMENTARY_WRITE'
  EXPORTING
    it_list_commentary t_header.
ENDFORM.                    "top-of-page


*&---------------------------------------------------------------------*
*&      Form  SET_CELL_COLOURS
*&---------------------------------------------------------------------*
*       Set colour of individual ALV cell, field
*----------------------------------------------------------------------*
FORM SET_CELL_COLOURS .
  DATAWA_CELLCOLOR TYPE LVC_S_SCOL.
  DATAld_index TYPE SY-TABIX.

  LOOP AT IT_VBAK INTO wa_vbak.
    LD_INDEX SY-TABIX.

    WA_CELLCOLOR-FNAME 'VBELN'.
    WA_CELLCOLOR-COLOR-COL sy-tabix.
    WA_CELLCOLOR-COLOR-INT '1'.  "1 = Intensified on, 0 = Intensified off
    WA_CELLCOLOR-COLOR-INV '0'.  "1 = text colour, 0 = background colour
    APPEND WA_CELLCOLOR TO wa_vbak-CELLCOLOR.
    MODIFY it_vbak FROM wa_vbak INDEX ld_index TRANSPORTING CELLCOLOR.

    IF wa_vbak-netwr GT 0.
      WA_CELLCOLOR-FNAME 'NETWR'.
      WA_CELLCOLOR-COLOR-COL 4.
      WA_CELLCOLOR-COLOR-INT '0'.
      WA_CELLCOLOR-COLOR-INV '0'.
      APPEND WA_CELLCOLOR TO wa_vbak-CELLCOLOR.
      MODIFY it_vbak FROM wa_vbak INDEX ld_index TRANSPORTING CELLCOLOR.
    ENDIF.


    WA_CELLCOLOR-FNAME 'ERDAT'.
    WA_CELLCOLOR-COLOR-COL 6.
    WA_CELLCOLOR-COLOR-INT '0'.
    WA_CELLCOLOR-COLOR-INV '1'.
    APPEND WA_CELLCOLOR TO wa_vbak-CELLCOLOR.
    MODIFY it_vbak FROM wa_vbak INDEX ld_index TRANSPORTING CELLCOLOR.
  ENDLOOP.

ENDFORM.


outputs: