FUNCTION zmm_po_send_email.

function zmm_po_send_email.
*"----------------------------------------------------------------------
*"*"區域介面:
*"  IMPORTING
*"     VALUE(OLD_EKKO) TYPE  EKKO
*"     VALUE(NEW_EKKO) TYPE  EKKO
*"     VALUE(IM_STATUS) TYPE  MEREP_TSALEORDER-STATUS
*"----------------------------------------------------------------------

*如果是取消核發,則不觸發郵件功能
data:v_level      type i,    "核發級別,如四級審批
     v_offst      type i,    "偏移量
     v_index      type i,    "索引號
     v_tabix      type i,    "內表行
     v_olddc      type c,    "核發前狀態
     v_newwc      type c.    "核發后狀態
data:i_codes      like table of merel_s_grid1 with header line,
     i_codesall   like table of merel_s_grid1 with header line,
     i_codesno    like table of merel_s_grid1 with header line,
     w_code1      like merel_s_grid1,
     w_code2      like merel_s_grid1.

*等待3秒鐘,其目的是讓PO核發處理完畢,再執行下面的,因為執行太快了,下面讀取核發代碼很有可能不全面
*該FM必須異步調用才能有效處理
*WAIT UP TO 10 SECONDS.         "DEVK908804
break-point id zmm_email.
*FRGKE   核發指示碼A表示已完成核發,
*FRGZU   核發狀態,X表示級別上已核發
*V_LEVEL = STRLEN( NEW_EKKO-FRGZU ).
*BREAK CP900.
call function 'ZMM_PO_RELEASE_STRATEGY_DATA'
  exporting
    ebeln             = new_ekko-ebeln
  importing
    e_level           = v_level
  tables
    t_relcodes        = i_codes
    t_relcodesall     = i_codesall
    t_relcodesno      = i_codesno.

*DESCRIBE TABLE I_CODES LINES V_LEVEL.
perform set_rel_info(saplzxwms_mast) if found tables i_codesno.

condense old_ekko-frgzu no-gaps.
condense new_ekko-frgzu no-gaps.

if im_status = 'CRT'.             "從建立或更新觸發

elseif im_status = 'REL'.         "核發時觸發
  while v_level gt 0.
    v_index = v_index + 1.
    v_olddc = old_ekko-frgzu+v_offst(1).
    v_newwc = new_ekko-frgzu+v_offst(1).
    if v_olddc eq '' and v_newwc = 'X'.             "核發情況
      clear:w_code1,w_code2.
      read table i_codesall into w_code1 index v_index.
      if new_ekko-frgke = 'A'.                      "表明核發已完成,發送郵件通知采購群組
        w_code2-description = new_ekko-ekgrp.
        perform check_po_recipient_address using new_ekko-ekorg new_ekko-frggr w_code2 'COM' changing sy-subrc.
        if sy-subrc eq 0.
          perform ready_po_send_email using new_ekko 'COM' w_code1 w_code2.
        endif.
          perform send_po_to_vendor using new_ekko.

      else.                                                           "表明部分核發,發送郵件通知下一位核發者
        v_tabix = v_index + 1.
        read table i_codesall into w_code2 index v_tabix.
        perform check_po_recipient_address using new_ekko-ekorg new_ekko-frggr w_code2 im_status changing sy-subrc.
        if sy-subrc eq 0.
          perform ready_po_send_email using new_ekko im_status w_code1 w_code2.
        endif.
      endif.
    endif.
    v_level = v_level - 1.
    v_offst = v_offst + 1.
  endwhile.
endif.
endfunction.
*&这个代码还会调很多方法的