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

BAPI_INCOMINGINVOICE_CANCEL



 
Post new topic   Reply to topic    Russian ABAP Developer's Club Forum Index -> Interfaces | Интерфейсы -> BAPI
View previous topic :: View next topic  
Author Message
admin
Администратор
Администратор



Joined: 01 Sep 2007
Posts: 1639

PostPosted: Sat Apr 12, 2008 6:19 pm    Post subject: BAPI_INCOMINGINVOICE_CANCEL Reply with quote

BAPI_INCOMINGINVOICE_CANCEL - Invoice Verification: Reverse Invoice

Code:

tables: vbak,
        adr6,
        trdyse01cm.
---------------------------------------------------------------------
*INCLUDE
---------------------------------------------------------------------
*INCLUDE ZZUTI_STD_HEADER_FOOTER. "Standard header and footer
----------------------------------------------------------------------
*TYPES DECLARATION
----------------------------------------------------------------------
types:
  begin of t_crmemo,
    vbeln type vbak-vbeln,
    posnr type vbap-posnr,
    erdat type vbak-erdat,
    auart type vbak-auart,
    augru type vbak-augru,
    waerk type vbak-waerk,
    vkorg type vbak-vkorg,
    vtweg type vbak-vtweg,
    spart type vbak-spart,
    vdatu type vbak-vdatu,
    bstnk type vbak-bstnk,
    bsark type vbak-bsark,
    bstdk type vbak-bstdk,
    aedat type vbak-aedat,
    bukrs_vf type vbak-bukrs_vf,
    matnr type vbap-matnr,
    zmeng type vbap-zmeng,
    zieme type vbap-zieme,
  end of t_crmemo,
  begin of t_crmemo_change,
    vbeln(90) type c,
    posnr type vbap-posnr,
    erdat type vbak-erdat,
    auart type vbak-auart,
    augru type vbak-augru,
    waerk type vbak-waerk,
    vkorg type vbak-vkorg,
    vtweg type vbak-vtweg,
    spart type vbak-spart,
    vdatu type vbak-vdatu,
    bstnk type vbak-bstnk,
    bsark type vbak-bsark,
    bstdk type vbak-bstdk,
    aedat type vbak-aedat,
    bukrs_vf type vbak-bukrs_vf,
    matnr type vbap-matnr,
    zmeng type vbap-zmeng,
    zieme type vbap-zieme,
  end of t_crmemo_change,
  begin of t_mvke,
    matnr  type mvke-matnr,
    vkorg  type mvke-vkorg,
    vtweg  type mvke-vtweg,
    mtpos  type mvke-mtpos,
  end of t_mvke,
  begin of t_orders,
    vbelv   type vbfa-vbelv,
    posnv   type vbfa-posnv,
    vbeln   type vbfa-vbeln,
    posnn   type vbfa-posnn,
    vbtyp_n type vbfa-vbtyp_n,
  end of t_orders,
  begin of t_ekpo,
    ebeln   type ekko-ebeln,
    ebelp   type ekpo-ebelp,
    waers   type ekko-waers,
    menge   type ekpo-menge,
    meins   type ekpo-meins,
    netpr   type ekpo-netpr,
    peinh   type ekpo-peinh,
    mwskz   type ekpo-mwskz,
    txjcd   type ekpo-txjcd,
  end of t_ekpo,
  begin of t_ekbe,
    ebeln   type ekbe-ebeln,
    ebelp   type ekbe-ebelp,
    zekkn   type ekbe-zekkn,
    vgabe   type ekbe-vgabe,
    gjahr   type ekbe-gjahr,
    belnr   type ekbe-belnr,
    buzei   type ekbe-buzei,
  end of t_ekbe,
  begin of t_rbkp,
    belnr   type rbkp-belnr,
    gjahr   type rbkp-gjahr,
    bldat   type rbkp-bldat,
    budat   type rbkp-budat,
    xblnr   type rbkp-xblnr,
    lifnr   type rbkp-lifnr,
    rmwwr   type rbkp-rmwwr,
    wmwst1  type rbkp-wmwst1,
    xrech   type rbkp-xrech,
    stblg   type rbkp-stblg,
  end of t_rbkp,
  begin of t_cdhdr,
    objectclas type cdhdr-objectclas,
    objectid   type cdhdr-objectid,
    changenr   type cdhdr-changenr,
    udate      type cdhdr-udate,
    utime      type cdhdr-utime,
  end of t_cdhdr,
  begin of t_cdpos,
    objectclas type cdhdr-objectclas,
    objectid   type cdhdr-objectid,
    changenr   type cdhdr-changenr,
    tabname    type cdpos-tabname,
    tabkey     type cdpos-tabkey,
    fname      type cdpos-fname,
    chngind    type cdpos-chngind,
    value_new  type cdpos-value_new,
    value_old  type cdpos-value_old,
  end of t_cdpos,
  begin of t_change_docs,
    objectclas type cdhdr-objectclas,
    objectid   type cdhdr-objectid,
    changenr   type cdhdr-changenr,
    udate      type cdhdr-udate,
    utime      type cdhdr-utime,
    tabname    type cdpos-tabname,
    tabkey     type cdpos-tabkey,
    fname      type cdpos-fname,
    chngind    type cdpos-chngind,
    value_new  type cdpos-value_new,
    value_old  type cdpos-value_old,
  end of t_change_docs,
  begin of t_success,
    crmemo    type vbak-vbeln,
    cmemo     type vbak-vbeln,
    orders    type vbak-vbeln,
    po        type vbak-vbeln,
    miro_no   type vbak-vbeln,
    text(20)  type c,
  end of t_success,
  begin of t_error,
    crmemo  type vbak-vbeln,
    cmemo     type vbak-vbeln,
    orders    type vbak-vbeln,
    po        type vbak-vbeln,
    miro_no   type vbak-vbeln,
    text(100) type c,
  end of t_error.
----------------------------------------------------------------------
*WORKAREA DECLARATION
----------------------------------------------------------------------
data:
  wa_crmemo  type t_crmemo,
  wa_crmemo_change type t_crmemo_change,
  wa_mvke    type t_mvke,
  wa_orders  type t_orders,
  wa_cmemo   type  t_orders,
  wa_po      type t_orders,
  wa_ekpo    type t_ekpo,
  wa_ekbe    type t_ekbe,
  wa_ekbe_temp type t_ekbe,
  wa_rbkp    type t_rbkp,
  wa_rbkp_tmp type t_rbkp,
  wa_cdhdr   type t_cdhdr,
  wa_cdpos   type t_cdpos,
  wa_change_docs type t_change_docs,
  wa_crmemo_mr8m  type t_crmemo,
  wa_success type t_success,
  wa_error   type t_error,
  wa_success_mr8m type t_success,
  wa_error_mr8m   type t_error.
*--Work area for all the internal tables used for Sending Mail
data :
  wa_objpack type sopcklsti1,
  wa_objhead type solisti1,
  wa_objbin type solisti1,
  wa_objtxt type solisti1,
  wa_reclist type somlreci1 .
data:
*
*
*Structure to hold Invoice Header Data


  x_docheader like bapi_incinv_create_header,
*
*
*Structure to hold Reversal Posting Data


  x_rev_post like bapi_incinv_fld.
----------------------------------------------------------------------
*INTERNAL TAABLE DECLARATION
----------------------------------------------------------------------
data:
  it_crmemo      type standard table of t_crmemo   with header line,
  it_crmemo_change type standard table of t_crmemo_change with header
line,
  it_mvke        type standard table of t_mvke     with header line,
  it_crmemo_miro type standard table of t_crmemo   with header line,
  it_crmemo_mr8m type standard table of t_crmemo   with header line,
  it_orders      type standard table of t_orders   with header line,
  it_cmemo       type standard table of t_orders   with header line,
  it_po          type standard table of t_orders   with header line,
  it_ekpo        type standard table of t_ekpo     with header line,
  it_ekbe        type standard table of t_ekbe     with header line,
  it_ekbe_temp    type standard table of t_ekbe    with header line,
  it_rbkp        type standard table of t_rbkp     with header line,
  it_cdhdr       type standard table of t_cdhdr    with header line,
  it_cdpos       type standard table of t_cdpos    with header line,
  it_change_docs type standard table of t_change_docs with header line,
  it_success     type standard table of t_success  with header line,
  it_error       type standard table of t_error    with header line,
  it_success_mr8m type standard table of t_success with header line,
  it_error_mr8m  type standard table of t_error    with header line.

*
*
*Internal table to hold Invoice Item Data


data:begin of it_itemdata occurs 0.
       include structure bapi_incinv_create_item.
data:end of it_itemdata.

*
*
*Internal table to hold Invoice Acct Assignment Data


data:begin of it_accountingdata occurs 0.
       include structure bapi_incinv_create_account.
data:end of it_accountingdata.

*
*
*Internal table to hold BAPI return data


data: begin of it_return occurs 0.
        include structure bapiret2.  "Return Parameter.
data: end of it_return.

*
*
*Internal table to hold BAPI return data


data: begin of it_return_mr8m occurs 0.
        include structure bapiret2.  "Return Parameter.
data: end of it_return_mr8m.
*--MAIL related Internal tables
data: x_doc_chng type sodocchgi1,                      " document
*attributes
      it_objpack type standard table of sopcklsti1,    " attachment
*table
      it_objhead type standard table of solisti1,      " object header
*table
      it_objbin type standard table of solisti1,       " binary table
      it_objtxt type standard table of solisti1,       " object text
      it_reclist type standard table of somlreci1,     " mail recipients
      it_compressed_list type standard table of soli,
      it_pdf_list type standard table of tline,
      it_pdf  type tline occurs 10 with header line,
      it_html  type solisti1   occurs 10  with header line,
*
*
*Internal table for MIME data (in CHAR format)


      it_temp   type bapiqcmime occurs 10 with header line,
*
*
*Internal table for storing data


      it_mime(255) type c occurs 10 with header line.
----------------------------------------------------------------------
*GLOBAL VARIABLES                                                     *
----------------------------------------------------------------------
data: v_pgm_id      type sy-repid,                    " Program id
      v_zterm       type lfb1-zterm,                  " Payment Terms
      v_text(75)    type c,           " To store error or success text
      v_itemno(6)   type n,           " To Increment Item No. for BAPI
      v_docnr       like bapi_incinv_fld-inv_doc_no,
                                      " For MIRO document number
      v_year1       like bapi_incinv_fld-fisc_year,
                                      " For MIRO Fiscal year
      v_spono       type tsp01-rqident,               " For Spool No.
      v_type        type soodk-objtp,                 " For Spool Type
      v_crmemo_qty  type vbap-zmeng,                  " For CR Memo Qty.
      v_line_amount type ekpo-netpr,             " For CR Memo Item Qty.
      v_grossamt    type ekpo-netpr,            " For CR Memo Grand Qty.
      v_tax_per(3)  type n,                   " For CR Memo Tax amount %
      v_tax_amt     type ekpo-netpr,           " For CR Memo Tax amount
      v_flag(1)     type c,                   " Flag to process fruther
      v_tot_s_mr8m  type i,                   " For MR8M Success Records
      v_tot_e_mr8m  type i,                   " For MR8M Error Records
      v_date        type sy-datum,            " For Sel. Screen Date
      v_date2(10)   type c,                           " For EMail Date
      v_time(10)    type c,                           " For EMail Time
      v_time2(10)   type c,                           " For EMail Time
      v_info(100)   type c,                  " For EMail Body Text
      v_table_lines type sy-tabix,           " To Increment Email Count
      v_text2       type string   ,          " For EMail Id text
      v_lines       type i,                  " To Store PDF Lines
      v_temp(500)   type c,              " To Convert to 255 char format
      v_offset      type p,              " To Convert to 255 char format
      v_lineslen    type i,              " To Convert to 255 char format
      v_mimelen     type i,             " To Convert to 255 char format
      v_tabix       type sy-tabix.      " To Convert to 255 char format
----------------------------------------------------------------------
*CONSTANTS                                                            *
----------------------------------------------------------------------
constants:
  c_x(1)            type c          value 'X',    " Constant X or Flag
  c_e(1)            type c          value 'E',       " For Error check
  c_v(1)            type c          value 'V',       " For PO Docs
  c_o(1)            type c          value 'O',       " For Order Docs
  c_c(1)            type c          value 'C',    " For Credit Memo Docs
  c_k(1)            type c          value 'K',       " For CR Memo Docs
  c_u(1)            type c          value 'U', " For Change tables
  c_ali(3)          type c          value 'ALI',     " For ALI Format
  c_vbak(4)         type c          value 'VBAK',    " For Change docs
  c_01(2)           type c          value '01',      " For Serial No. &
  c_02(2)           type c          value '02',      " For Reversal
  c_blart           type bkpf-blart value 'RE',      " Document type
  c_mtpos           type mvke-mtpos value 'BANS',    " Item Category
  c_augru(5)        type c          value 'AUGRU',   " Reason Code
  c_0000500205(10)  type c          value '0000500205', " GL Account #
  c_verkbeleg(9)    type c          value 'VERKBELEG'.
                                                  " For Object Class
*
*
*Horizontal tab & line feed constants

class cl_abap_char_utilities definition load.
constants:
  c_tab  type c value cl_abap_char_utilities=>horizontal_tab,
  c_cr_lf type c value cl_abap_char_utilities=>cr_lf .
----------------------------------------------------------------------
*SELECTION SCREEN
----------------------------------------------------------------------
*
*
*Select Optons


selection-screen: begin of block b01 with frame title text-001.
  select-options: s_auart for vbak-auart obligatory no intervals,
                                                  " Sales Document Type
                  s_augru for vbak-augru obligatory no intervals,
                                                  " Order reason
                  s_erdat for vbak-erdat obligatory.
                                  " Date on which the record was created
selection-screen: end of block b01.

*
*
*Mail Sending Options


selection-screen begin of block b2 with frame title text-004.
  parameters:p_email as checkbox default space user-command v_com .
  parameters : p_mode type so_escape   default 'U' modif id us1.
  select-options: s_email for adr6-smtp_addr
                                      no intervals modif id us2.
*SELECT-OPTIONS: S_EMAIL FOR TRDYSE01CM-USERNAME
*NO INTERVALS MODIF ID US2.

  selection-screen: begin of line.
    selection-screen: comment 55(40) text-019 modif id us2.
  selection-screen: end of line.
selection-screen end of block b2.
----------------------------------------------------------------------
*WORK AREA for SELECT-OPTIONS
----------------------------------------------------------------------
data :  x_email like s_email.   "Work area for EMAIL select-options
----------------------------------------------------------------------
*INITIALIZATION
----------------------------------------------------------------------
initialization.
*
*
*Default values for Sales Document Type


  clear s_auart[].
  s_auart-sign = 'I'.
  s_auart-option = 'EQ'.
  s_auart-low = 'CR'.
  append s_auart.
  clear s_auart.

*
*
*Default values for Date on which the record was created


  v_date = ( sy-datum - 1 ).
  clear s_erdat[].
  s_erdat-sign = 'I'.
  s_erdat-option = 'EQ'.
  s_erdat-low =  sy-datum.
  s_erdat-high =  sy-datum.
  append s_erdat.
  clear s_erdat.
----------------------------------------------------------------------
*TOP OF PAGE                                       *
----------------------------------------------------------------------
top-of-page.
*
*
*Displaying standard header.


*PERFORM std_top_of_page USING sy-title.

----------------------------------------------------------------------
*A T  S E L E C T I O N   S C R E E N  O U T P U T
----------------------------------------------------------------------
*
*
*For modifying the selection screen layout


at selection-screen output.
  perform f_modify_screen.
----------------------------------------------------------------------
*AT SELECTION SCREEN.                              *
----------------------------------------------------------------------
at selection-screen.
*
*
*Subroutine to Validate selection screen fields


  perform f_validate.
----------------------------------------------------------------------
*START-OF-SELECTION                                                  *
----------------------------------------------------------------------
start-of-selection.
*
*
*Fetch the Credit Memo Requests documents


  perform f_get_credit_memo_request.
  if not it_crmemo[] is initial.
*
*
*Fetch Item Category Group


    perform f_get_mvke_docs.

*
*
*Fetch Sales Order documents, PO Documents and Credit Memos


    perform f_get_so_po_docs.
  endif.

*
*
*If Credit memo request internal table have an change records then


*fetch the change documents from CDHDR and CDPOS
  if not it_crmemo_change[] is initial.
*
*
*Fetch Change Documents for CR Memos from CDHDR and CDPOS tables


    perform f_get_changed_docs.
  endif.

*
*
*Fetch Purchasing Document Item Details


  if not it_po[] is initial.
    perform f_get_ekpo.
  endif.

*
*
*Fetch MIRO Vendor based on the PO Details


  if not it_ekpo[] is initial.
    perform f_get_vendor.
  endif.
----------------------------------------------------------------------
*END-OF-SELECTION                                                    *
----------------------------------------------------------------------
  if v_flag is initial.
*
*
*Process Vendor Debit Postings


    perform f_process_miro.

*Process Reversal Postings of Vendor Automatic Debits using TCODE MR8M
    perform f_process_mr8m.

*
*
*Send mail,if user enters Email id's and Email Mode is not initial


    if p_email = c_x and p_mode is not initial and s_email[] is not
initial.

*
*
*Get spool id


      perform f_spool_id.

*
*
*EMail Logic


      perform f_mail_logic.
    endif.
*display the EMail RSCONN01 and Sussess and Error records of MIRO and
*MR8M
    perform f_display_report.
  else.
*
*
*Else exit the process


    exit.
  endif.
&---------------------------------------------------------------------
*&      Form  F_MODIFY_SCREEN
&---------------------------------------------------------------------
*A Form routine to modify the screen
----------------------------------------------------------------------
form f_modify_screen .

*
*
*To Modify Mail Sending Options


  loop at screen.
    if p_email = c_x.
      if screen-group1 = 'US1' .
        screen-invisible = 0.
        screen-active = 1.
        screen-input = 0.
        modify screen.
      endif.
      if screen-group2 = 'US2' .
        screen-invisible = 0.
        screen-active = 1.
        modify screen.
      endif.
    elseif p_email = space.
      if screen-group1 = 'US1' .
        screen-invisible = 1.
        screen-active = 0.
        modify screen.
      endif.
      if screen-group1 = 'US2' .
        screen-invisible = 1.
        screen-active = 0.
        modify screen.
      endif.
    endif.
  endloop.
endform.                    " F_MODIFY_SCREEN
&---------------------------------------------------------------------
*&      Form  f_validate
&---------------------------------------------------------------------
*Validation of Selection-Screen Inputs
----------------------------------------------------------------------
form f_validate .

*
*
*Validate Sales Document Type


  if not s_auart[] is initial.
    select  auart
      into s_auart-low
      from tvak up to 1 rows
      where auart in s_auart.
    endselect.
    if sy-subrc  0.
      message e000 with 'Invalid Sales Document Type'(e01) .
    endif.
  endif.

*
*
*Validate Order reason (reason for the business transaction)


  if not s_augru[] is initial.
    select  augru
      into s_augru-low
      from tvau up to 1 rows
      where augru in s_augru.
    endselect.
    if sy-subrc  0.
      message e000 with 'Invalid Order reason'(e02) .
    endif.
  endif.
endform.                    " f_validate
&---------------------------------------------------------------------
*&      Form  F_GET_CREDIT_MEMO_REQUEST
&---------------------------------------------------------------------
*Fetch Credit memo requests from VBAK & VBAP
----------------------------------------------------------------------
form f_get_credit_memo_request .
  select a~vbeln
         b~posnr
         a~erdat
         a~auart
         a~augru
         a~waerk
         a~vkorg
         a~vtweg
         a~spart
         a~vdatu
         a~bstnk
         a~bsark
         a~bstdk
         a~aedat
         a~bukrs_vf
         b~matnr
         b~zmeng
         b~zieme
    into table it_crmemo
    from ( vbak as a
    inner join vbap as b on a~vbeln = b~vbeln )
    where ( a~erdat in s_erdat
      or  a~aedat in s_erdat )
      and a~auart in s_auart.
  if sy-subrc = 0.
    sort it_crmemo by vbeln posnr.
  else.
    message i000 with 'No Credit Memo Request selected'(002).
    v_flag = c_x.
    stop.
  endif.
endform.                    " F_GET_CREDIT_MEMO_REQUEST
&---------------------------------------------------------------------
*&      Form  F_GET_MVKE_DOCS
&---------------------------------------------------------------------
*Fetch Sales Data for Material
----------------------------------------------------------------------
form f_get_mvke_docs .
  data: lv_tabix type sy-tabix.

*
*
*Fetch Sales Data for Material


  select matnr      " Material Number
         vkorg      " Sales Organization
         vtweg      " Distribution Channel
         mtpos      " Item category group from material master
    from mvke       " Sales Data for Material
    into table it_mvke
    for all entries in it_crmemo
    where matnr = it_crmemo-matnr
      and vkorg = it_crmemo-vkorg
      and vtweg = it_crmemo-vtweg.
  if sy-subrc = 0.
    sort it_mvke by matnr vkorg vtweg mtpos.
  endif.

*
*
*Validate Item category group if MVKE-MTOPS  'BANS' then ignore CM


*Request line item
  loop at it_crmemo into wa_crmemo.
    lv_tabix = sy-tabix.
    read table it_mvke into wa_mvke with key matnr = wa_crmemo-matnr
                                             vkorg = wa_crmemo-vkorg
                                             vtweg = wa_crmemo-vtweg
                                             mtpos = c_mtpos
                                             binary search.
    if sy-subrc  0.
      delete it_crmemo index lv_tabix.    " Delete CR memo records
    else.
*
*
*Else check for credit memo request change date is not initial, and


*append those records to IT_CRMEMO_CHANGE internal table to retrive
*change documents
      if not wa_crmemo-aedat is initial.
        wa_crmemo_change-vbeln = wa_crmemo-vbeln.
        wa_crmemo_change-posnr = wa_crmemo-posnr.
        wa_crmemo_change-erdat = wa_crmemo-erdat.
        wa_crmemo_change-auart = wa_crmemo-auart.
        wa_crmemo_change-augru = wa_crmemo-augru.
        wa_crmemo_change-waerk = wa_crmemo-waerk.
        wa_crmemo_change-vkorg = wa_crmemo-vkorg.
        wa_crmemo_change-vtweg = wa_crmemo-vtweg.
        wa_crmemo_change-spart = wa_crmemo-spart.
        wa_crmemo_change-vdatu = wa_crmemo-vdatu.
        wa_crmemo_change-bstnk = wa_crmemo-bstnk.
        wa_crmemo_change-bsark = wa_crmemo-bsark.
        wa_crmemo_change-bstdk = wa_crmemo-bstdk.
        wa_crmemo_change-aedat = wa_crmemo-aedat.
        wa_crmemo_change-bukrs_vf = wa_crmemo-bukrs_vf.
        wa_crmemo_change-matnr = wa_crmemo-matnr.
        wa_crmemo_change-zmeng = wa_crmemo-zmeng.
        wa_crmemo_change-zieme = wa_crmemo-zieme.
        append wa_crmemo_change to it_crmemo_change.
        clear wa_crmemo_change.
      endif.
    endif.
  endloop.
endform.                    " F_GET_MVKE_DOCS
&---------------------------------------------------------------------
*&      Form  F_GET_SO_PO_DOCS
&---------------------------------------------------------------------
*Fetch the Sales Order documents, PO documents, & Credit Memo
----------------------------------------------------------------------
form f_get_so_po_docs.

*
*
*Fetch Preceding sales orders based on the credit memo request


  select vbelv
         posnv
         vbeln
         posnn
         vbtyp_n
    from vbfa
    into table it_orders
    for all entries in it_crmemo
    where vbeln = it_crmemo-vbeln
      and posnn = it_crmemo-posnr
      and vbtyp_n = c_k
      and vbtyp_v = c_c.
  if sy-subrc = 0.
    sort it_orders by vbelv posnv.

*
*
*Fetch Subsequent PO documents & Credit Memo based on the sales


*orders
    select vbelv
           posnv
           vbeln
           posnn
           vbtyp_n
      from vbfa
      into table it_po
      for all entries in it_orders
      where vbelv = it_orders-vbelv
        and posnv = it_orders-posnv
        and vbtyp_n in (c_v,c_o)
        and vbtyp_v = c_c.
    if sy-subrc = 0.
      sort it_po by vbtyp_n.

*
*
*Copy the IT_PO internal table contents to IT_CMEMO internal table


      it_cmemo] = it_po[.

*
*
*Delete PO documents from IT_CMEMO internal table


      delete it_cmemo where vbtyp_n = c_v.
      sort it_cmemo by vbeln posnn.
      if it_cmemo[] is initial.
      message i000 with 'No Credit Memo are available for CM
Request'(003).
*v_flag = c_x.
*stop.
      endif.

*
*
*Delete Credit Memo documents from IT_PO internal table


      delete it_po where vbtyp_n = c_o.
      sort it_po by vbeln posnn.
    endif.
  endif.
endform.                    " F_GET_SO_PO_DOCS
&---------------------------------------------------------------------
*&      Form  F_GET_CHANGED_DOCS
&---------------------------------------------------------------------
*Fetch Change Documents for CR Memos from CDHDR and CDPOS tables
----------------------------------------------------------------------
form f_get_changed_docs .

*
*
*Fetch Change Documents for CR Memos from CDHDR


  select objectclas
         objectid
         changenr
         udate
         utime
  from cdhdr
  into table it_cdhdr
  for all entries in it_crmemo_change
  where objectclas = c_verkbeleg
    and objectid   = it_crmemo_change-vbeln
*AND UDATE      = IT_CRMEMO_CHANGE-AEDAT
    .
  if sy-subrc = 0.
    sort it_cdhdr by objectclas objectid changenr.

*
*
*Fetch Change Documents for CR Memos from CDPOS


    select objectclas
           objectid
           changenr
           tabname
           tabkey
           fname
           chngind
           value_new
           value_old
      from cdpos
      into table it_cdpos
      for all entries in it_cdhdr
      where objectclas = c_verkbeleg
        and objectid   = it_cdhdr-objectid
        and changenr   = it_cdhdr-changenr
        and tabname    = c_vbak
        and fname      = c_augru
        and chngind    = c_u.
    if sy-subrc = 0.
      sort it_cdpos by objectclas objectid changenr.
    endif.
  endif.
endform.                    " F_GET_CHANGED_DOCS
&---------------------------------------------------------------------
*&      Form  F_GET_EKPO
&---------------------------------------------------------------------
*Fetch Purchasing Document Item Details
----------------------------------------------------------------------
form f_get_ekpo .

*
*
*Fetch Purchasing Document Item Details


  select a~ebeln
         b~ebelp
         a~waers
         b~menge
         b~meins
         b~netpr
         b~peinh
         b~mwskz
         b~txjcd
  into table it_ekpo
  from ( ekko as a
  inner join ekpo as b on a~ebeln = b~ebeln )
  for all entries in it_po
  where a~ebeln = it_po-vbeln
    and b~ebelp = it_po-posnn+1(5).
  if sy-subrc = 0.
    sort it_ekpo by ebeln ebelp.
  endif.
endform.                    " F_GET_EKPO
&---------------------------------------------------------------------
*&      Form  F_GET_VENDOR
&---------------------------------------------------------------------
*Fetch MIRO Vendor details based on the PO Details
----------------------------------------------------------------------
form f_get_vendor .
data: lv_tabix type sy-tabix.

*
*
*Fetch History per Purchasing Document records for all the PO records


  select ebeln
         ebelp
         zekkn
         vgabe
         gjahr
         belnr
         buzei
  from ekbe
  into table it_ekbe
  for all entries in it_ekpo
  where ebeln = it_ekpo-ebeln
    and ebelp = it_ekpo-ebelp
    and vgabe = '2'.
*and STBLG = space.
  if sy-subrc = 0.
    sort it_ekbe by belnr gjahr.

*
*
*Fetch the vendor details based on table IT_EKBE internal table


    select belnr
           gjahr
           bldat
           budat
           xblnr
           lifnr
           rmwwr
           wmwst1
           xrech
           stblg
    from rbkp
    into table it_rbkp
    for all entries in it_ekbe
    where belnr = it_ekbe-belnr
      and gjahr = it_ekbe-gjahr.
    if sy-subrc = 0.
      sort it_rbkp by belnr gjahr.
    endif.
  endif.

*
*
*Sort PO History documents based on PO # and PO Item


  sort it_ekbe by ebeln ebelp.

*
*
*Copy the PO History documents into IT_EKBE_TEMP internal table


  it_ekbe_temp] = it_ekbe[.

*
*
*Delete PO History documents from IT_EKBE_TEMP internal table


*comparing with IT_RBKP where (STBLG) Reversal document number
*is initial
  loop at it_ekbe_temp into wa_ekbe_temp.
    lv_tabix = sy-tabix.
    read table it_rbkp into wa_rbkp with key belnr = wa_ekbe_temp-belnr
                                             gjahr = wa_ekbe_temp-gjahr
*XRECH = SPACE
                                             stblg = space.
    if sy-subrc  0.
      delete it_ekbe_temp index lv_tabix.
    endif.
    clear: wa_ekbe_temp, wa_rbkp.
  endloop.
endform.                    " F_GET_VENDOR
&---------------------------------------------------------------------
*&      Form  F_PROCESS_MIRO
&---------------------------------------------------------------------
*Process MIRO
----------------------------------------------------------------------
form f_process_miro .
  data:
    lv_itemno         type i,
    lv_tabix          type sy-index,
    lc_check(1)       type c value 'X',
    v_fill_item,
    lv_rbkp_bldat type sy-datum.
  clear : it_itemdata, it_accountingdata, it_crmemo_miro, it_crmemo_mr8m
.
  refresh : it_itemdata, it_accountingdata, it_crmemo_miro,
it_crmemo_mr8m.

*
*
*Copy all the CR Memo Docs and delete order reason not in S_AUGRU


  it_crmemo_mr8m] = it_crmemo[.

*
*
*Delete Reversal postings which are not in S_AUGRU


  delete it_crmemo_mr8m where augru in s_augru.

*
*
*Delete Reversal postings where AEDAT is initial


  delete it_crmemo_mr8m where aedat is initial.

*
*
*Delete order reason which are not in S_AUGRU


  delete it_crmemo where augru not in s_augru.

*
*
*Loop through the credit memo request records


  loop at it_crmemo into wa_crmemo.
    lv_tabix = sy-tabix.

*
*
*If credit memo request change date is not initial then


*send change date (AEDAT) to lv_rbkp_bldat
    if not wa_crmemo-aedat is initial.
      lv_rbkp_bldat =  wa_crmemo-aedat.
    else.
*
*
*If credit memo request change date is initial then


*send create date (ERDAT) to lv_rbkp_bldat
      lv_rbkp_bldat =  wa_crmemo-erdat.
    endif.
    read table it_orders into wa_orders with key vbeln = wa_crmemo-vbeln
                                                 posnn = wa_crmemo-posnr
.
    if sy-subrc = 0.
      read table it_po into wa_po with key vbelv = wa_orders-vbelv
                                           posnv = wa_orders-posnv.
      if sy-subrc = 0.
        read table it_ekpo into wa_ekpo with key ebeln = wa_po-vbeln
                                                 ebelp = wa_po-posnn.
        if sy-subrc = 0.
*
*
*Read IT_EKBE_TEMP internal table which contains records which are


*not reversed
          read table it_ekbe_temp into wa_ekbe_temp with key ebeln =
wa_ekpo-ebeln
                                                             ebelp =
wa_ekpo-ebelp.
          if sy-subrc = 0.
*
*
*Read IT_RBKP internal table with XRECH (Indicator: post invoice) =


*'X' ,
*check for invoice records for the correspnding CR Memo records
            read table it_rbkp into wa_rbkp with key belnr =
wa_ekbe_temp-belnr
                                                     gjahr =
wa_ekbe_temp-gjahr
                                                     xrech = c_x.
            if sy-subrc = 0.
*
*
*Read IT_RBKP internal table with XRECH (Indicator: post invoice) =


*SPACE,
*and document date = CR Memo creation date
*and reference document number = PO number
              read table it_rbkp into wa_rbkp_tmp with key belnr =
wa_ekbe_temp-belnr
                                                           gjahr =
wa_ekbe_temp-gjahr
                                                           bldat =
wa_crmemo-erdat
                                                           xrech = ' '
                                                           xblnr+0(10) =
 wa_ekbe_temp-ebeln.
*STBLG =
*SPACE.
              if sy-subrc = 0.
                continue.
              else.
*
*
*Quantity Conversion (Get the Credit Memo Quantity by passing CMR


*unit per one PO unit)
                perform f_quantity_conv.

*
*
*Filling the Line items data



*
*
*Incrementing the item no


                lv_itemno = lv_itemno + 1.
                v_itemno  = lv_itemno.
                it_itemdata-invoice_doc_item  = v_itemno.
                it_itemdata-po_number         = wa_ekpo-ebeln.
                it_itemdata-po_item           = wa_ekpo-ebelp.
                it_itemdata-tax_code          = wa_ekpo-mwskz.
                it_itemdata-taxjurcode        = wa_ekpo-txjcd.

*
*
*Converting SAP amount to BAPI format by passing line item amount and


*currency
                perform f_currency_amount_sap_to_bapi using
wa_ekpo-waers
v_line_amount
                                                      changing
it_itemdata-item_amount .
                it_itemdata-quantity          = ( wa_crmemo-zmeng *
v_crmemo_qty ).
                it_itemdata-po_unit           = wa_ekpo-meins.
                append it_itemdata.

*
*
*Populate Account Assignment data


                it_accountingdata-invoice_doc_item = v_itemno.
                it_accountingdata-xunpl       = space.
                it_accountingdata-serial_no   = c_01.
                it_accountingdata-tax_code    = it_itemdata-tax_code.
                it_accountingdata-taxjurcode  = it_itemdata-taxjurcode.
                it_accountingdata-item_amount = it_itemdata-item_amount.
                it_accountingdata-quantity    = it_itemdata-quantity.
                it_accountingdata-po_unit     = it_itemdata-po_unit.
                it_accountingdata-gl_account  = c_0000500205.
                append it_accountingdata.
                clear it_accountingdata.
                clear it_itemdata.
              endif.
            endif.
          endif.
        endif.
      endif.
    endif.
*
*
*At end of each credit memo request fill the header details of BAPI


*FM and
*
*
*post the BAPI


    at end of vbeln.
      read table it_crmemo into wa_crmemo index lv_tabix.

*
*
*Populate the document Header
*Getting the header data for invoice


      x_docheader-invoice_ind  = space.
      x_docheader-doc_type     = c_blart.
      x_docheader-doc_date     = lv_rbkp_bldat.
      x_docheader-pstng_date   = lv_rbkp_bldat.
      x_docheader-ref_doc_no   = wa_rbkp-xblnr.
      x_docheader-comp_code    = wa_crmemo-bukrs_vf.
      x_docheader-currency     = wa_crmemo-waerk.
      x_docheader-calc_tax_ind = lc_check.

*
*
*Logic to find the Tax %


      v_tax_per = ( wa_rbkp-wmwst1 / ( wa_rbkp-rmwwr - wa_rbkp-wmwst1 )
) * 100.
      v_tax_amt = ( v_grossamt *  v_tax_per ) / 100.
      v_grossamt =  ( v_grossamt + v_tax_amt ).
      x_docheader-gross_amount = v_grossamt.
      clear:v_docnr, v_year1.

*
*
*Calling BAPI to create PO invoice document


      check not it_itemdata[] is initial.
      call function 'BAPI_INCOMINGINVOICE_CREATE'
        exporting
          headerdata       = x_docheader
        importing
          invoicedocnumber = v_docnr
          fiscalyear       = v_year1
        tables
          itemdata         = it_itemdata[]
          accountingdata    = it_accountingdata[]
*GLACCOUNTDATA    =
*taxdata          =
          return           = it_return.

*
*
*Commit work


      if not v_docnr is initial.
        call function 'BAPI_TRANSACTION_COMMIT'
          exporting
            wait = 'X'.
*
*
*Move the data to Success Internal Table


        wa_success-crmemo  = wa_crmemo-vbeln.
        wa_success-cmemo   = wa_cmemo-vbeln.
        wa_success-orders  = wa_orders-vbelv.
        wa_success-po      = wa_po-vbeln.
        wa_success-miro_no = v_docnr.
        wa_success-text    = 'MIRO is Posted'.
        append wa_success to it_success.
        clear wa_success.
      endif.
*
*
*Read the values in the table IT_RETURN.


      clear it_return.
      read table it_return index 1.
*If message type is error then roll back the work
      if it_return-type = c_e.
*
*
*Roll back


        call function 'BAPI_TRANSACTION_ROLLBACK'.

*
*
*Move the data to Error Internal Table


        wa_error-crmemo = wa_crmemo-vbeln.
        wa_error-cmemo  = wa_cmemo-vbeln.
        wa_error-orders = wa_orders-vbelv.
        wa_error-po     = wa_po-vbeln.
        wa_error-text   = it_return-message.
        append wa_error to it_error.
        clear wa_error.
      endif.
      refresh: it_return, it_itemdata, it_accountingdata.
      clear: v_grossamt, it_return, it_itemdata, wa_crmemo, wa_orders,
wa_po,
             wa_ekpo, wa_ekbe_temp, wa_rbkp, wa_rbkp_tmp, v_docnr,
v_year1, wa_success,
             wa_error, it_accountingdata, v_tax_per, v_tax_amt,
v_fill_item, lv_rbkp_bldat.
    endat.
  endloop.
endform.                    " F_PROCESS_MIRO
&---------------------------------------------------------------------
*&      Form  f_quantity_conv
&---------------------------------------------------------------------
*Quantity Conversion
----------------------------------------------------------------------
form f_quantity_conv .

*
*
*Quantity Conversion (Get the Credit Memo Quantity by passing CMR


*unit per one PO unit)
  clear: v_line_amount, v_crmemo_qty.
  call function 'MC_UNIT_CONVERSION'
    exporting
      matnr                = wa_crmemo-matnr
      nach_meins           = wa_ekpo-meins
      von_meins            = wa_crmemo-zieme
    importing
      umref                = v_crmemo_qty
    exceptions
      conversion_not_found = 1
      material_not_found   = 2
      nach_meins_missing   = 3
      overflow             = 4
      von_meins_missing    = 5
      others               = 6.
  if sy-subrc  0.
    message id sy-msgid type sy-msgty number sy-msgno
      with sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
  else.
    v_line_amount = ( v_crmemo_qty  * wa_crmemo-zmeng * wa_ekpo-peinh *
wa_ekpo-netpr ).
    v_grossamt    = v_grossamt + v_line_amount.
  endif.
endform.                    " f_quantity_conv
&---------------------------------------------------------------------
*&      Form  f_currency_amount_sap_to_bapi
&---------------------------------------------------------------------
*SAP Amount to BAPI Amount
----------------------------------------------------------------------
form f_currency_amount_sap_to_bapi  using    p_waers
                                             p_kbetr
                                    changing p_amt_doccur.
  data : lv_waers like  tcurc-waers,
         lv_sapamount like bapicurr-bapicurr,
         lv_bapi_amount like bapicurr-bapicurr.
  lv_waers = p_waers.
  lv_sapamount = p_kbetr.
  call function 'CURRENCY_AMOUNT_SAP_TO_BAPI'
    exporting
      currency    = lv_waers
      sap_amount  = lv_sapamount
    importing
      bapi_amount = lv_bapi_amount.
  p_amt_doccur = lv_bapi_amount.
endform.                    " f_currency_amount_sap_to_bapi
&---------------------------------------------------------------------
*&      Form  f_display_report
&---------------------------------------------------------------------
*Display Report
----------------------------------------------------------------------
form f_display_report .

*
*
*Success Report


  if not it_success[] is initial.
    uline at /1(129).
    write:/1 sy-vline, 'CR Memo',
           19 sy-vline, 'Credit Memo',
           39 sy-vline, 'Sales Order',
           59 sy-vline, 'Purchase Order',
           79 sy-vline, 'MIRO Number',
           99 sy-vline, 'Text',
           129 sy-vline.
    uline at /1(129).
    loop at it_success into wa_success.
      write:/1 sy-vline, wa_success-crmemo,
             19 sy-vline, wa_success-cmemo,
             39 sy-vline, wa_success-orders,
             59 sy-vline, wa_success-po,
             79 sy-vline, wa_success-miro_no,
             99 sy-vline, wa_success-text,
             129 sy-vline.
    endloop.
    uline at /1(129).
    skip 2.
  endif.

*
*
*Error Report


  if not it_error[] is initial.
    uline at /1(179).
    write:/1 sy-vline,  'CR Memo',
           19 sy-vline, 'Credit Memo',
           39 sy-vline, 'Sales Order',
           59 sy-vline, 'Purchase Order',
           79 sy-vline, 'Error Text',
           179 sy-vline.
    uline at /1(179).
    loop at it_error into wa_error.
      write:/1 sy-vline, wa_error-crmemo,
             19 sy-vline, wa_error-cmemo,
             39 sy-vline, wa_error-orders,
             59 sy-vline, wa_error-po,
             79 sy-vline, wa_error-text,
             179 sy-vline.
    endloop.
    uline at /1(179).
  endif.
  if not it_success_mr8m[] is initial.
    uline at /1(129).
    write:/1 sy-vline, 'CR Memo',
           19 sy-vline, 'Credit Memo',
           39 sy-vline, 'Sales Order',
           59 sy-vline, 'Purchase Order',
           79 sy-vline, 'Reversal Posting No.',
           99 sy-vline, 'Text',
           129 sy-vline.
    uline at /1(129).
    loop at it_success_mr8m into wa_success_mr8m.
      write:/1 sy-vline, wa_success_mr8m-crmemo,
             19 sy-vline, wa_success_mr8m-cmemo,
             39 sy-vline, wa_success_mr8m-orders,
             59 sy-vline, wa_success_mr8m-po,
             79 sy-vline, wa_success_mr8m-miro_no,
             99 sy-vline, wa_success_mr8m-text,
             129 sy-vline.
    endloop.
    uline at /1(129).
    skip 2.
  endif.

*
*
*Error Report


  if not it_error_mr8m[] is initial.
    uline at /1(179).
    write:/1 sy-vline,  'CR Memo',
           19 sy-vline, 'Credit Memo',
           39 sy-vline, 'Sales Order',
           59 sy-vline, 'Purchase Order',
           79 sy-vline, 'Error Text',
           179 sy-vline.
    uline at /1(179).
    loop at it_error_mr8m into wa_error_mr8m.
      write:/1 sy-vline, wa_error_mr8m-crmemo,
             19 sy-vline, wa_error_mr8m-cmemo,
             39 sy-vline, wa_error_mr8m-orders,
             59 sy-vline, wa_error_mr8m-po,
             79 sy-vline, wa_error_mr8m-text,
             179 sy-vline.
    endloop.
    uline at /1(179).
  endif.
endform.                    " f_display_report
&---------------------------------------------------------------------
*&      Form  F_PROCESS_MR8M
&---------------------------------------------------------------------
*Process Reversal postings
----------------------------------------------------------------------
form f_process_mr8m .
  clear: wa_crmemo_mr8m.
  loop at it_crmemo_mr8m into wa_crmemo_mr8m.
   read table it_cdpos into wa_cdpos with key objectid =
wa_crmemo_mr8m-vbeln.
   if sy-subrc  0.
     continue.
   else.
   if wa_cdpos-value_old in s_augru.
    read table it_orders into wa_orders with key vbeln =
wa_crmemo_mr8m-vbeln
                                                 posnn =
wa_crmemo_mr8m-posnr.
    if sy-subrc = 0.
*READ TABLE IT_CMEMO INTO WA_CMEMO WITH KEY VBELV =
*WA_ORDERS-VBELV
*POSNV =
*WA_ORDERS-POSNV.
*IF SY-SUBRC = 0.
        read table it_po into wa_po with key vbelv = wa_orders-vbelv
                                             posnv = wa_orders-posnv.
        if sy-subrc = 0.
          read table it_ekpo into wa_ekpo with key ebeln = wa_po-vbeln
                                                   ebelp = wa_po-posnn.
          if sy-subrc = 0.
*READ TABLE IT_EKBE INTO WA_EKBE WITH KEY EBELN =
*WA_EKPO-EBELN
*EBELP = WA_EKPO-EBELP.
*IF SY-SUBRC = 0.
            loop at it_ekbe into wa_ekbe where ebeln = wa_ekpo-ebeln
                                         and   ebelp = wa_ekpo-ebelp.
              read table it_rbkp into wa_rbkp with key
                                               belnr = wa_ekbe-belnr
                                               gjahr = wa_ekbe-gjahr
                                            bldat = wa_crmemo_mr8m-aedat
                                            xblnr+0(10) = wa_ekbe-ebeln
                                                       xrech = space
                                                       stblg = space.
              if sy-subrc = 0.
                move: wa_rbkp-belnr   to x_rev_post-inv_doc_no,
                      wa_rbkp-gjahr   to x_rev_post-fisc_year,
                      wa_rbkp-budat   to x_rev_post-pstng_date.
                if wa_rbkp-budat+4(2) = sy-datum+4(2).
                  x_rev_post-reason_rev = c_01.
                else.
                  x_rev_post-reason_rev = c_02.
                endif.
              endif.
            endloop.
*
*
*Call the FM for MIRO reversal


              call function 'BAPI_INCOMINGINVOICE_CANCEL'
              exporting
                invoicedocnumber          = x_rev_post-inv_doc_no
                fiscalyear                = x_rev_post-fisc_year
                reasonreversal            = x_rev_post-reason_rev
                postingdate               = x_rev_post-pstng_date
              importing
                invoicedocnumber_reversal = x_rev_post-inv_doc_no
                fiscalyear_reversal       = x_rev_post-fisc_year
              tables
                return                    = it_return_mr8m.
*
*
*Commit work


            if not x_rev_post-inv_doc_no is initial.
              call function 'BAPI_TRANSACTION_COMMIT'
                exporting
                  wait = 'X'.
*
*
*Move the data to Success Internal Table


              wa_success_mr8m-crmemo  = wa_crmemo-vbeln.
              wa_success_mr8m-cmemo   = wa_cmemo-vbeln.
              wa_success_mr8m-orders  = wa_orders-vbelv.
              wa_success_mr8m-po      = wa_po-vbeln.
              wa_success_mr8m-text    = 'MR8M Done'.
              append wa_success_mr8m to it_success_mr8m.
              clear wa_success_mr8m.
            endif.
*
*
*Read the values in the table IT_RETURN.


            clear it_return_mr8m.
            read table it_return_mr8m index 1.
*
*
*If message type is error then roll back the work


            if it_return_mr8m-type = 'E' .
*
*
*Roll back


              call function 'BAPI_TRANSACTION_ROLLBACK'.
*
*
*Move the data to Error Internal Table


              wa_error_mr8m-crmemo = wa_crmemo-vbeln.
              wa_error_mr8m-cmemo  = wa_cmemo-vbeln.
              wa_error_mr8m-orders = wa_orders-vbelv.
              wa_error_mr8m-po     = wa_po-vbeln.
              wa_error_mr8m-text   = it_return_mr8m-message.
              append wa_error_mr8m to it_error_mr8m.
              clear wa_error_mr8m.
            endif.
*ENDIF.
*ENDIF.
          endif.
        endif.
*ENDIF.
    endif.
    endif.
    endif.
    clear: wa_error_mr8m, wa_success_mr8m, x_rev_post, wa_crmemo_mr8m,
           wa_orders, wa_po, wa_ekpo, wa_ekbe, wa_rbkp, wa_cmemo.
    refresh: it_return_mr8m.
  endloop.
endform.                    " F_PROCESS_MR8M
&---------------------------------------------------------------------
*&      Form  F_spool_id
&---------------------------------------------------------------------
*Create a new spool ID
----------------------------------------------------------------------
form f_spool_id .

*
*
*Create a new spool from the data in data_tab
*new-page line-size sy-linsz print on


  new-page line-size 255 print on
  destination 'LOCL'(p19) "<<== my printer
*
*
*receiver 'TJOSVOB'


  cover text 'Spool title'(p20) " <<== Title or Spool req. name
  list name 'List name'(p21)
  list dataset 'List Dataset'(p22)
  immediately ' '                  " X means print now
  keep in spool c_x                " X means keep spool
  new list identification c_x
  no dialog .

*
*
*Writing Report
*In this  perform you will write the code for displaying the list


  perform f_display_report.

*
*
*new-page print off


  new-page print off.
endform.                    " F_spool_id
&---------------------------------------------------------------------
*&      Form  F_MAIL_LOGIC
&---------------------------------------------------------------------
*Logic to send a mail for the given Email ID's
----------------------------------------------------------------------
form f_mail_logic .
  data : wa_tsp01sys type tsp01sys.
  clear v_spono.
  v_spono = sy-spono.
*v_attach_desc = sy-title.
*
*Convert the Spool ID into PDF format


  perform f_pdf_conversion tables it_compressed_list
                                  it_pdf_list
                                  using  v_spono.

*
*
*Send the Mail in PDF Format


  perform f_send_mail.
endform.                    " F_MAIL_LOGIC
&---------------------------------------------------------------------
*&      Form  f_pdf_conversion
&---------------------------------------------------------------------
*Read the spool and convert into PDF format
----------------------------------------------------------------------
*-->P_ITAB_COMPRESSED_LIST  text
*-->P_ITAB_PDF_LIST  text
*-->P_V_SPONO  text
----------------------------------------------------------------------
form f_pdf_conversion  tables   it_compressed_list structure soli
                                it_pdf_list structure tline
                       using    lv_spooln type tsp01-rqident.
clear: it_compressed_list, it_pdf_list.
refresh: it_compressed_list, it_pdf_list.

*
*
*Call the FM "RSPO_RETURN_SPOOLJOB" to read the spool by passing the


*spool number
  call function 'RSPO_RETURN_SPOOLJOB'
       exporting
            rqident              = lv_spooln
            desired_type         = v_type
       importing
            real_type            = v_type
       tables
            buffer               = it_compressed_list
            buffer_pdf           = it_pdf_list
       exceptions
            no_such_job          = 1
            job_contains_no_data = 2
            selection_empty      = 3
            no_permission        = 4
            can_not_access       = 5
            read_error           = 6
            type_no_match        = 7
            others               = 8.
  if sy-subrc  0.
    message i000 with 'Error in spool reading'(017).
  endif.

*
*
*If the List type is ALI call the FM "CONVERT_ABAPSPOOLJOB_2_PDF"


*to convert list into PDF Format
  if  v_type = c_ali.                          " for List it is ALI
    call function 'CONVERT_ABAPSPOOLJOB_2_PDF'
         exporting
              src_spoolid              = lv_spooln
         tables
              pdf                      = it_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
              others                   = 12.
    if sy-subrc  0.
      message e000
              with'Error in convertion of list into PDF format'(018).
    endif.

*
*
*Logic to convert the given format into 255 character format in


*IT_PDF
    describe table it_pdf lines v_lines.
    describe field it_pdf length v_lineslen  in character mode.
    describe field it_temp length v_mimelen in character mode.
    loop at it_pdf.
      v_tabix = sy-tabix.
      move it_pdf to v_temp+v_offset.
      if v_tabix = v_lines.
        v_lineslen = strlen( it_pdf ).
      endif.
      v_offset = v_offset + v_lineslen.
      if v_offset ge v_mimelen.
        clear it_temp.
        it_temp = v_temp(v_mimelen).
        append it_temp.
        shift v_temp by v_mimelen places.
        v_offset = v_offset - v_mimelen.
      endif.
      if v_tabix = v_lines.
        if v_offset gt 0.
          clear it_temp.
          it_temp = v_temp(v_offset).
          append it_temp.
        endif.
      endif.
    endloop.
    loop at it_temp.
      it_mime(255) = it_temp-line.
      append it_mime.
    endloop.

*
*
*Final Data to send to EMail


    it_html] = it_mime[.
  endif.
endform.                    " f_pdf_conversion
&---------------------------------------------------------------------
*&      Form  f_send_mail
&---------------------------------------------------------------------
*Send EMail Logic
----------------------------------------------------------------------
form f_send_mail .
data : wa_tsp01sys type tsp01sys.

*
*
*Populate Mail Id's


  wa_reclist-rec_type = p_mode.
  wa_reclist-express  = c_x.

*
*
*Loop through the Emial select options


  loop at s_email into x_email.
    wa_reclist-receiver = x_email-low.
*
*
*append receiver table


    append wa_reclist to it_reclist.
  endloop.

*
*
*If receiver table is not initial


  check it_reclist[] is not initial.
*--To check Mail ID's internal table should not be Empty.
*--populate document attributes

*
*
*Mail subject line


  clear: x_doc_chng.
  x_doc_chng-obj_name = 'HEADING'.
  concatenate 'Automatic Vendor Dedit Postings'(005)
              sy-datum
              into x_doc_chng-obj_descr separated by space.

*
*
*Mail body


  concatenate 'Automatic Vendor Dedit Postings'(005)
             ' is attached.'(006)
             into wa_objtxt separated by space.
  append wa_objtxt to it_objtxt.

*
*
*Append Date and Time into Body of email.


  move 'File is generated on'(007)
        to v_info.
  v_time = sy-uzeit.
  concatenate v_time+0(2)
              v_time+2(2)
              v_time+4(2)
              into
              v_time2 separated by ':'.
  concatenate sy-datum+4(2)
              '/'
              sy-datum+6(2)
              '/'
              sy-datum+0(4)
              into v_date2.
  concatenate v_info
              v_date2
              'At'
              v_time2
              into v_info
              separated by space.
  wa_objtxt = v_info.
  append wa_objtxt to it_objtxt.

*
*
*Document size


  clear : v_table_lines.
  describe table it_objtxt lines v_table_lines.
  read table it_objtxt into wa_objtxt index v_table_lines .
  x_doc_chng-doc_size =
                 ( v_table_lines - 1 ) * 255 + strlen( wa_objtxt ).

*
*
*Populate packing list for body text


  wa_objpack-head_start = 1.
  wa_objpack-head_num = 0.
  wa_objpack-body_start = 1.
  wa_objpack-body_num = v_table_lines.
  wa_objpack-doc_type = 'RAW'.
  append wa_objpack to it_objpack.
  clear wa_objpack.
  if v_type = c_ali.
    loop at it_html.
      wa_objbin-line = it_html-line.
    append wa_objbin to it_objbin.
    clear  wa_objbin.
    endloop.
    v_type = 'PDF'.
  endif.

*
*
*Get total no.of lines of Object table(attachment)


  clear : v_table_lines.
  describe table it_objbin lines v_table_lines.

*
*
*Populate object header(attachment name)


  wa_objhead = 'Automatic Vendor Dedit Postings log'(014).
  append wa_objhead to it_objhead.
  clear  wa_objhead.

*
*
*Packing list for attachment


  wa_objpack-transf_bin = c_x.
  wa_objpack-head_start = 1.
  wa_objpack-head_num = 1.
  wa_objpack-body_start = 1.
  wa_objpack-body_num = v_table_lines .
  wa_objpack-doc_type = v_type .
  wa_objpack-obj_name = 'Auto_Debit'.
  wa_objpack-obj_descr = 'attachment'.
  wa_objpack-doc_size = v_table_lines * 255.
  append wa_objpack to it_objpack.
  clear  wa_objpack.

*
*
*Sending the EMail document in given format


  call function 'SO_NEW_DOCUMENT_ATT_SEND_API1'
    exporting
      document_data              = x_doc_chng
      put_in_outbox              = c_x
      commit_work                = c_x
    tables
      packing_list               = it_objpack
      object_header              = it_objhead
      contents_bin               = it_objbin
      contents_txt               = it_objtxt
      receivers                  = it_reclist
    exceptions
      too_many_receivers         = 1
      document_not_sent          = 2
      document_type_not_exist    = 3
      operation_no_authorization = 4
      parameter_error            = 5
      x_error                    = 6
      enqueue_error              = 7
      others                     = 8.
  if sy-subrc  0.
    message e000 with 'Error occurred in sending mail'(015).
  else.
*
*
*Wait upto 2 secounds


    wait up to 2 seconds.

*
*
*Call the RSCONN01 (SAPconnect: Start Send Process)


    submit rsconn01 with mode = 'INT'
*WITH output = 'X'
    and return.
    perform f_display_sent_mail_status.
  endif.

*To delete the spool request
  wa_tsp01sys-rqident = sy-spono.
  call function 'RSPO_IDELETE_SPOOLREQ'
       exporting
            spoolreq = wa_tsp01sys.
  if sy-subrc  0.
  endif.
endform.                    " f_send_mail
&---------------------------------------------------------------------
*&      Form  F_DISPLAY_SENT_MAIL_STATUS
&---------------------------------------------------------------------
*Display Mail Sent status to the user
----------------------------------------------------------------------
form f_display_sent_mail_status .
  uline at /1(60).
  write :/ sy-vline,
          'Mail sent Successfully to'(016)
           color 5 intensified on,
           60 sy-vline.
  loop at s_email into x_email.
    v_table_lines = strlen( x_email-low ).
    move x_email-low+0(v_table_lines) to v_text2.
    write :/ sy-vline,
             2 sy-tabix left-justified,
             5 ')',
               v_text2,
             60 sy-vline.
  endloop.
    uline at /1(60).
endform.                    " F_DISPLAY_SENT_MAIL_STATUS
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 -> Interfaces | Интерфейсы -> BAPI 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 can 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.