메뉴 건너뛰기

SAP 한국 커뮤니티



smartform to pdf

sapjoy 2007.05.22 13:29 조회 수 : 6069

*&---------------------------------------------------------------------*
*& report : zpdf_format
*& description : conversion of purchase order into pdf format
*& used in the workflow (do not change or delete).
*&---------------------------------------------------------------------*
REPORT zpdf MESSAGE-ID zmm.

*&---------------------------------------------------------------------*
*& tables used

*&---------------------------------------------------------------------*
TABLES: nast, tsp01, t024, spop, lfa1, tnapr.

*&---------------------------------------------------------------------*
*& data declaration

*&---------------------------------------------------------------------*
DATA: spoolno LIKE tsp01-rqident.
DATA: rcode LIKE sy-subrc.
*data: doc_auth like zoutput-ztag.
DATA: cancel.
DATA: pdf LIKE tline OCCURS 0 WITH HEADER LINE.
DATA: numbytes TYPE i,
pdfspoolid LIKE tsp01-rqident,
jobname LIKE tbtcjob-jobname,
jobcount LIKE tbtcjob-jobcount,
is_otf.
DATA: client LIKE tst01-dclient,
name LIKE tst01-dname,
objtype LIKE rststype-type,
type LIKE rststype-type.
DATA: dir_loc(3).
DATA: t_docno LIKE vbak-vbeln..
DATA: spoolreq1 LIKE tsp01sys.
DATA: spoolreq LIKE rsporq OCCURS 0 WITH HEADER LINE.
DATA: t_frgke LIKE ekko-frgke.
DATA: okcode(10),
flag,
t_ekgrp LIKE ekko-ekgrp,
t_lifnr LIKE ekko-lifnr,
s_mail.
* data declartion for mailing system - start.
DATA: objpack LIKE sopcklsti1 OCCURS 0 WITH HEADER LINE.
DATA: objhead LIKE solisti1 OCCURS 0 WITH HEADER LINE.
DATA: objbin LIKE solisti1 OCCURS 0 WITH HEADER LINE.
DATA: objtxt LIKE solisti1 OCCURS 0 WITH HEADER LINE.
DATA: reclist1 LIKE somlreci1 OCCURS 500 WITH HEADER LINE.
DATA: reclist LIKE somlreci1 OCCURS 500 WITH HEADER LINE.
DATA: itab LIKE somlreci1 OCCURS 50 WITH HEADER LINE.       "RKU 220802
DATA: doc_chng LIKE sodocchgi1.
DATA: BEGIN OF bdcdata OCCURS 0.
        INCLUDE STRUCTURE bdcdata.
DATA: END OF bdcdata.
DATA: tab_lines LIKE sy-tabix.
DATA: verkf LIKE ekko-verkf.
DATA: bemail(250), vemail(250).

*&---------------------------------------------------------------------*
* data acceptance
*&---------------------------------------------------------------------*
SELECTION-SCREEN BEGIN OF BLOCK blk0_input WITH FRAME .
PARAMETERS: docno LIKE ekko-ebeln OBLIGATORY LOWER CASE.
SELECTION-SCREEN SKIP.
SELECTION-SCREEN END OF BLOCK blk0_input.

*&---------------------------------------------------------------------*
* initialisation
*&---------------------------------------------------------------------*
INITIALIZATION.

*&---------------------------------------------------------------------*
* screen parameters validation

*&---------------------------------------------------------------------*
*at selection-screen.

AT SELECTION-SCREEN.
  SELECT SINGLE frgke INTO t_frgke FROM ekko
  WHERE ebeln = docno
  AND frgke = 'R'.
  IF sy-subrc <> 0.
*    MESSAGE 'DOCUMENT NOT RELEASED' type 'E'.
  ENDIF.

*&---------------------------------------------------------------------*
* execution part

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

* for po related operation
* dislay buyer group, name and email id
  SELECT SINGLE
  ekgrp
  lifnr
  verkf
  INTO (t_ekgrp, t_lifnr, verkf)
  FROM ekko
  WHERE ekko~mandt = sy-mandt
  AND ebeln = docno.
  IF sy-subrc = 0.
    SELECT SINGLE * FROM lfa1
    WHERE lfa1~mandt = sy-mandt
    AND lifnr = t_lifnr.
  ENDIF.

* get message status from nast or assign msg status nast stru
* pass the message status and get spool data
  PERFORM check_output_create_spool.
  IF rcode NE 0.
    IF rcode = 9.
* message s185 with text-e04.
    ELSE.
* message s185 with text-e01.
    ENDIF.
    EXIT.
  ENDIF.

* find the spool
  PERFORM find_spool_request_id.
  IF sy-subrc <> 0.
* message s185 with text-003.
    EXIT.
  ENDIF.
* read table spoolreq
* with key rq0name = nast-dsnam
** rq1name = 'LP01'
* rqclient = '800'
** rq2name = sy-uname
* rqowner = sy-uname.
* if sy-subrc <> 0.
* case sy-subrc.
* when 1.
** message s185 with text-e03.
* when others.
** message s185 with text-e02.
* endcase.
* exit.
* endif.
  spoolno = spoolreq-rqident.

* convert spool job to pdf
  PERFORM convert_spool_to_pdf.
* send through mail
  PERFORM assign_data_4_mail.
  PERFORM send_mail_with_attachment.
* delete created spool request
  spoolreq1-rqident = spoolno.
* perform delete_spool_job.

*&---------------------------------------------------------------------*
*& form check_output_create_spool
*&---------------------------------------------------------------------*
FORM check_output_create_spool.
  SELECT SINGLE * FROM nast WHERE objky = docno
  AND kappl = 'EF'
  AND kschl = 'NEU'
  AND aktiv = space
  AND nacha = '1'.
  IF sy-subrc EQ 0.
* count = count + 1.
    PERFORM mssage_status_field_value.
    nast-vsztp = '4'.
    PERFORM einzelnachricht_dialog(rsnast00) USING rcode.
  ELSE.
    CLEAR nast-uhrvr.
    CLEAR nast-cmfpnr.
    CLEAR nast-datvr.
    PERFORM mssage_status_field_value.
    nast-vsztp = '4'.
    PERFORM einzelnachricht_dialog(rsnast00) USING rcode.
* else.
* rcode = 9.
  ENDIF.
ENDFORM. " check_output_create_spool

*&---------------------------------------------------------------------*
*& form mssage_status_field_value
*----------------------------------------------------------------------*
FORM mssage_status_field_value .
  nast-mandt = '800'.
  nast-kappl = 'EF'.
  nast-kschl = 'NEU'.
  nast-objky = docno.
  nast-ldest = 'LP01'.
  nast-anzal = 1.
  nast-dimme = 'X'.
  nast-delet = 'X'.
  nast-nacha = '1'.
  nast-vsztp = '2'.
  nast-spras = 'E'.
  nast-vstat = '0'.
  nast-manue = 'X'.
  nast-erdat = sy-datum.
  nast-eruhr = sy-uzeit.
  nast-usnam = sy-uname.
  nast-tdreceiver = sy-uname.
  CONCATENATE sy-uzeit+2(4) 'PDF' INTO nast-dsnam.
ENDFORM. " mssage_status_field_value

*&---------------------------------------------------------------------*
*& form find_spool_request_id
*----------------------------------------------------------------------*
FORM find_spool_request_id.
  CALL FUNCTION 'RSPO_FIND_SPOOL_REQUESTS'
       EXPORTING
            allclients          = '800'
*           authority           = ' '
            datatype            = '*'
            has_output_requests = '*'
            rq0name             = nast-dsnam  "'*'
            rq1name             = '*'
            rq2name             = '*'
            rqdest              = 'LP01'
*           rqident             = 0
            rqowner             = sy-uname
       TABLES
            spoolrequests       = spoolreq
       EXCEPTIONS
            no_permission       = 1
            OTHERS              = 2.
ENDFORM. " find_spool_request_id

*&---------------------------------------------------------------------*
*& form convert_spool_to_pdf
*----------------------------------------------------------------------*
FORM convert_spool_to_pdf.
  SELECT SINGLE * FROM tsp01 WHERE rqident = spoolno.
  IF sy-subrc <> 0.
    WRITE: / 'Spool order does not exist'
    COLOR COL_NEGATIVE.
    EXIT.
  ENDIF.
  client = tsp01-rqclient.
  name = tsp01-rqo1name.
  CALL FUNCTION 'RSTS_GET_ATTRIBUTES'
       EXPORTING
            authority     = 'SP01'
            client        = client
            name          = name
            part          = 1
       IMPORTING
            type          = type
            objtype       = objtype
       EXCEPTIONS
            fb_error      = 1
            fb_rsts_other = 2
            no_object     = 3
            no_permission = 4.
  IF objtype(3) = 'OTF'.
    is_otf = 'X'.
  ELSE.
    is_otf = space.
  ENDIF.
  IF is_otf = 'X'.
    CALL FUNCTION 'CONVERT_OTFSPOOLJOB_2_PDF'
         EXPORTING
              src_spoolid              = spoolno
              no_dialog                = ' '
         IMPORTING
              pdf_bytecount            = numbytes
              pdf_spoolid              = pdfspoolid
              btc_jobname              = jobname
              btc_jobcount             = jobcount
         TABLES
              pdf                      = pdf
         EXCEPTIONS
              err_no_otf_spooljob      = 1
              err_no_spooljob          = 2
              err_no_permission        = 3
              err_conv_not_possible    = 4
              err_bad_dstdevice        = 5
              user_cancelled           = 6
              err_spoolerror           = 7
              err_temseerror           = 8
              err_btcjob_open_failed   = 9
              err_btcjob_submit_failed = 10
              err_btcjob_close_failed  = 11.
* if sy-subrc <> 0.
* case sy-subrc.
* when 1.
* write: / text-001 color col_positive.
* when 2.
* write: / text-002 color col_negative.
* exit.
* when 3.
* write: / text-003 color col_negative.
* exit.
* when 4.
* write: / text-004 color col_negative.
* exit.
* when others.
* write: / text-005 color col_negative.
* exit.
* endcase.
* endif.
  ELSE.
    CALL FUNCTION 'CONVERT_ABAPSPOOLJOB_2_PDF'
         EXPORTING
              src_spoolid              = spoolno
              no_dialog                = ' '
*             dst_device               =
*             pdf_destination          =
        IMPORTING
              pdf_bytecount            = numbytes
              pdf_spoolid              = pdfspoolid
*             list_pagecount           =
              btc_jobname              = jobname
              btc_jobcount             = jobcount
         TABLES
              pdf                      = pdf
         EXCEPTIONS
              err_no_abap_spooljob     = 1
              err_no_spooljob          = 2
              err_no_permission        = 3
              err_conv_not_possible    = 4
              err_bad_destdevice       = 5
              user_cancelled           = 6
              err_spoolerror           = 7
              err_temseerror           = 8
              err_btcjob_open_failed   = 9
              err_btcjob_submit_failed = 10
              err_btcjob_close_failed  = 11.
* case sy-subrc.
* when 0.
*write: / 'Funktion CONVERT_ABAPSPOOLJOB_2_PDF erfolgreich
*(successful)'.
** color col_positive.
* when 1.
* write: / text-001 color col_positive.
* when 2.
* write: / text-002 color col_negative.
* exit.
* when 3.
* write: / text-003 color col_negative.
* exit.
* when 4.
* write: / text-004 color col_negative.
* exit.
* when others.
* write: / text-005 color col_negative.
* exit.
* endcase.
  ENDIF.
ENDFORM. " convert_spool_to_pdf

*&---------------------------------------------------------------------*
*& form delete_spool_job
*----------------------------------------------------------------------*
FORM delete_spool_job .
  CALL FUNCTION 'RSPO_IDELETE_SPOOLREQ'
  EXPORTING
      spoolreq = spoolreq1
*  IMPORTING
*    rc       =
*      status   =
  EXCEPTIONS
      error    = 1
      OTHERS   = 2.
  IF sy-subrc <> 0.
* message id sy-msgid type sy-msgty number sy-msgno
* with sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
  ENDIF.
ENDFORM. " delete_spool_job

*&---------------------------------------------------------------------*
*& form assign_data_4_mail
*----------------------------------------------------------------------*
FORM assign_data_4_mail .
* text content of the mail
* move 'Purchase order: ' to objtxt.
* append objtxt.
* move verkf to objtxt.
* append objtxt.
* move lfa1-name1 to objtxt.
* append objtxt.
* concatenate lfa1-ort01 lfa1-pstlz
* into objtxt separated by space.
* append objtxt.
* clear: objtxt.
* append objtxt.
* append objtxt.
*
  CLEAR objtxt.
  CONCATENATE 'Purchase Order - ' docno
  ' has been released.'
  INTO objtxt SEPARATED BY space.
  APPEND objtxt.

* clear: objtxt.
* append objtxt.
* append objtxt.

  MOVE ' ' TO objtxt.
  APPEND objtxt.
* clear: objtxt.
* append objtxt.
* append objtxt.
* concatenate 'Note:- This is an automatic mail sender.'
* ' Please do not reply to this mail ID.'
* into objtxt.
* append objtxt.
* concatenate
* ' Any query, please send the mail to the respective buyer
* 's mail id.' into objtxt.
* append objtxt.

  CALL FUNCTION 'CONVERSION_EXIT_ALPHA_OUTPUT'
       EXPORTING
            input  = docno
       IMPORTING
            output = docno.
* tline format will be coverted as 255 char foramt
  CALL FUNCTION 'QCE1_CONVERT'
       TABLES
            t_source_tab         = pdf
            t_target_tab         = objbin
       EXCEPTIONS
            convert_not_possible = 1
            OTHERS               = 2.
  DESCRIBE TABLE objtxt LINES tab_lines.
* creation of the entry for the compressed document
  CLEAR objpack.
  objpack-transf_bin = .
  objpack-head_start = 1.
  objpack-head_num = 0.
  objpack-body_start = 1.
  objpack-body_num = tab_lines.
  objpack-doc_type = 'RAW'.
  objpack-doc_size = ( tab_lines - 1 ) * 255 + STRLEN( objtxt ).
  APPEND objpack.
* creation of the entry for the attachemnt
  DESCRIBE TABLE objbin LINES tab_lines.
  objpack-transf_bin = 'X'.
  objpack-head_start = 1.
  objpack-head_num = 1.
  objpack-body_start = 1.
  objpack-body_num = tab_lines.
  objpack-doc_type = 'PDF'.
  CONCATENATE docno '.PDF' INTO objpack-obj_name.
  objpack-obj_descr = objpack-obj_name.
  objpack-doc_size = ( tab_lines - 1 ) * 255 + STRLEN( objbin ).
  APPEND objpack.
* for object header
  CONCATENATE docno '.PDF' INTO objhead.
  APPEND objhead.
  doc_chng-doc_size = ( tab_lines - 1 ) * 255 + STRLEN( objbin ).
  CONCATENATE 'Purchase Order - ' docno ' has been released'
  INTO doc_chng-obj_descr.
  doc_chng-obj_prio = 1.
* recipient details
  CLEAR reclist1.
  CLEAR reclist.
  REFRESH reclist1.
  REFRESH reclist.
* select single * from t024
* where t024~mandt = sy-mandt
* and ekgrp = t_ekgrp.
* reclist1-receiver = t024-smtp_addr.
  reclist1-rec_type = 'U'.
  reclist1-com_type = 'INT'.
  reclist1-receiver = 'mukundhan.kn@lntinfotech.com'.
* modify table reclist transporting rec_type receiver com_type .
* where rec_type is initial.
  APPEND reclist1 TO reclist.
ENDFORM. " ASSIGN_DATA_4_MAIL

*&---------------------------------------------------------------------*
*& form send_mail_with_attachment
*----------------------------------------------------------------------*
FORM send_mail_with_attachment.
* sending the document
  CALL FUNCTION 'SO_NEW_DOCUMENT_ATT_SEND_API1'
       EXPORTING
            document_data              = doc_chng
            put_in_outbox              = 'X'
            commit_work                = 'X'
       TABLES
            packing_list               = objpack
            object_header              = objhead
            contents_bin               = objbin
            contents_txt               = objtxt
            receivers                  = reclist
       EXCEPTIONS
            too_many_receivers         = 1
            document_not_sent          = 2
            operation_no_authorization = 4
            OTHERS                     = 99.
  CASE sy-subrc.
    WHEN 0.
      PERFORM flush_mail.
* message 'Mail sent successfully' type 'I'.
* when 1.
* message 'No authorization for sending to the specified number'
*type 'E'.
* when 2.
* message 'Document could not be sent to any recipient' type 'E'.
* when 4.
* message 'No send authorization' type 'E'.
* when others.
* message 'Error occurred while sending' type 'E'.
  ENDCASE.
ENDFORM. " send_mail_with_attachment

*----------------------------------------------------------------------*
* start new screen *
*----------------------------------------------------------------------*
FORM bdc_dynpro USING program dynpro.
  CLEAR bdcdata.
  bdcdata-program = program.
  bdcdata-dynpro = dynpro.
  bdcdata-dynbegin = 'X'.
  APPEND bdcdata.
ENDFORM. "bdc_dynpro

*----------------------------------------------------------------------*
* insert field *
*----------------------------------------------------------------------*
FORM bdc_field USING fnam fval.
  CLEAR bdcdata.
  bdcdata-fnam = fnam.
  bdcdata-fval = fval.
  APPEND bdcdata.
ENDFORM. "bdc_field

*&---------------------------------------------------------------------*
*& form automate
*----------------------------------------------------------------------*
FORM flush_mail .
  PERFORM bdc_dynpro USING 'SAPMSSY0' '0120'.
  PERFORM bdc_field USING 'BDC_OKCODE'
  '=PDIA'.
  PERFORM bdc_dynpro USING 'SAPLSPO4' '0300'.
  PERFORM bdc_field USING 'BDC_OKCODE'
  '=FURT'.
  PERFORM bdc_field USING 'BDC_CURSOR'
  'SVALD-VALUE(01)'.
  PERFORM bdc_field USING 'SVALD-VALUE(01)'
  'int'.
  PERFORM bdc_dynpro USING 'SAPMSSY0' '0120'.
  PERFORM bdc_field USING 'BDC_OKCODE'
  '=BACK'.
  PERFORM bdc_dynpro USING 'SAPMSSY0' '0120'.
  PERFORM bdc_field USING 'BDC_OKCODE'
  '=BACK'.
  CALL TRANSACTION 'SCOT' USING bdcdata
  MODE 'N'
  UPDATE 'S'.
ENDFORM. " AUTOMATE