BAPI_PO_CREATE1 采购订单批量导入

在创建无信息记录的采购订单时,采购订单条件中自定义PB00,bapimepocond-change_id参数的设置:有信息记录则设置为'U',否值设置为'I'.
不然会报消息 06 218 '净价格必须大于0’错误

 

*&---------------------------------------------------------------------*
*& Report  ZRPMM_0103
*&
*&---------------------------------------------------------------------*
*&Desc.:采购订单批量导入
*&---------------------------------------------------------------------*

report zrpmm_0103.
tables:sscrfields.

data:gt_upload type alsmex_tabline occurs 0 with header line.
field-symbols:<fs_upload> like line of gt_upload.
data:zdcno(10) type c.

data:begin of gt_out occurs 0,
       zdcno(10) type c,            "号码
       bsart     like ekko-bsart, "订单类型
       lifnr     like ekko-lifnr, "供应商
       ekorg     like ekko-ekorg, "采购组织
       bukrs     like ekko-bukrs, "公司代码
       ekgrp     like ekko-ekgrp, "采购组
       bedat     like ekko-bedat, "采购订单日期
*       ebelp     like ekpo-ebelp, "项目号
       matnr     like ekpo-matnr, "物料编码
       txz01     like ekpo-txz01, "物料描述
       matkl     like ekpo-matkl, "物料组
       werks     like ekpo-werks, "工厂
       lgort     like ekpo-lgort, "库位
       pstyp     like ekpo-pstyp, "项目类别
       knttp     like ekpo-knttp, "科目分配类别
       kostl     type kostl,      "成本中心
       anln1     type anln1,      "固定资产号
       eindt     like eket-eindt, "交货日期
       menge     like ekpo-menge, "采购订单数量
       meins     like ekpo-meins, "采购订单单位
       netpr     like ekpo-netpr,  "单价
       peinh     like ekpo-peinh,  "价格单位
       waers     like ekko-waers,  "币别
       mwskz     type mwskz,      "税码
       txt20     type tdline,       "行项目文本
       ihrez     like ekko-ihrez, "原采购订单号
       change_id type c,  "有信息记录U,无信息记录I
       zstat(30) type c,
       msgty     type c,
       msg(200)  type c,
     end of gt_out.

field-symbols:<fs_out> like gt_out.
***alv data define
data:gv_repid  like sy-repid value sy-repid,
     gs_layout type slis_layout_alv,
     gt_fcat   type slis_t_fieldcat_alv with header line.


data:lt_t163y like t163y occurs 0 with header line. "ADD BY XULE 20150427

define mcr_build_fieldcat.
  gt_fcat-fieldname = &1.
  gt_fcat-seltext_l = &2.
  gt_fcat-seltext_m = &2.
  gt_fcat-seltext_s = &2.
  gt_fcat-ddictxt = 'L'.
  gt_fcat-inttype = &3.
  gt_fcat-intlen = &4.
  gt_fcat-outputlen = &5.
  if &1 = 'LIFNR' OR &1 = 'MATNR' OR &1 = 'INFNR'.
    gt_fcat-NO_ZERO = 'X'.
  ENDIF.
  APPEND gt_fcat.
  CLEAR gt_fcat.
end-of-definition.

constants:c_pf_status    type slis_formname value 'FRM_SET_PF_STATUS',
          c_user_command type slis_formname value 'FRM_USER_COMMAND'.

data:gv_flag type c.

***selection screen define
selection-screen function key 1. "工具条按钮 最多为5个.

selection-screen begin of block blc with frame title text-001.
parameters:p_file type rlgrap-filename.
selection-screen end of block blc .


at selection-screen on value-request for p_file.
  perform frm_select_file.

initialization.
  sscrfields-functxt_01 = '模板下载'.
  sscrfields-ucomm      = 'FC01'.

at selection-screen.
  case sscrfields-ucomm.
    when 'ONLI'.
      perform frm_check_file_exist.
    when 'FC01'.
      perform frm_generate_template.
    when others.
  endcase.

start-of-selection.
  perform frm_upload_data.
  perform frm_get_fieldcat.
  perform frm_output_data.
*&---------------------------------------------------------------------*
*&      Form  FRM_CHECK_FILE_EXIST
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
form frm_check_file_exist .
  data:lv_file   type string,
       lv_result type abap_bool.

  data:lv_name(255) type c,
       lv_path(255) type c.

  data:lv_lenth type i.

  check sy-ucomm = 'ONLI'.

  if p_file is initial.
    message '请输入文件路径' type 'E'.
  endif.

  lv_file = p_file.
  call method cl_gui_frontend_services=>file_exist
    exporting
      file                 = lv_file
    receiving
      result               = lv_result
    exceptions
      cntl_error           = 1
      error_no_gui         = 2
      wrong_parameter      = 3
      not_supported_by_gui = 4
      others               = 5.
  if sy-subrc <> 0.
    message '文件不存在' type 'E'.
  endif.

***检查文件格式
  call function 'TRINT_SPLIT_FILE_AND_PATH'
    exporting
      full_name     = p_file
    importing
      stripped_name = lv_name
      file_path     = lv_path
    exceptions
      x_error       = 1
      others        = 2.
  if sy-subrc = 0.
    lv_lenth = strlen( lv_name ).
    if lv_lenth > 4.
      lv_lenth = lv_lenth - 4.
    endif.
    lv_name = lv_name+lv_lenth.
    translate lv_name to upper case.
    if lv_name ne '.XLS' and lv_name ne 'XLSX'.
      message '文件不是EXCEL格式文件,请检查' type 'E'.
    endif.
  endif.
endform.                    " FRM_CHECK_FILE_EXIST
*&---------------------------------------------------------------------*
*&      Form  FRM_GENERATE_TEMPLATE
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
form frm_generate_template .
  data:ls_object like wwwdatatab,
       lv_rc     type sy-subrc,
       lt_ft     type filetable,
       ls_ft     type file_table,
       lv_ua     type i.

  data:lwk_fn type rlgrap-filename.
  data:lv_objid type w3objid.

****查询模板
  lv_objid = 'ZRPMM_0105'.
  select single relid objid
    from wwwdata
    into corresponding fields of ls_object
    where relid = 'MI'
      and srtf2 = 0
      and objid = lv_objid.                                  "smw0里对象名称
  if sy-subrc <> 0 or ls_object-objid = space .
    message s002(zmm_rpt_msg) with 'ZRPMM_0105' display like 'E'. "未知的Excel模板:ZRPMM_0103
    exit.
  endif.

  call method cl_gui_frontend_services=>file_open_dialog
    exporting
      file_filter             = cl_gui_frontend_services=>filetype_excel
    changing
      file_table              = lt_ft
      rc                      = lv_rc
      user_action             = lv_ua
    exceptions
      file_open_dialog_failed = 1
      cntl_error              = 2
      error_no_gui            = 3
      not_supported_by_gui    = 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.
  else.
    check lv_ua = 0.
    read table lt_ft into ls_ft index 1.
    lwk_fn  = ls_ft-filename.
***下载模板
    call function 'DOWNLOAD_WEB_OBJECT'
      exporting
        key         = ls_object
        destination = lwk_fn
      importing
        rc          = lv_rc.
    if lv_rc <> 0.
      message s003(zmm_rpt_msg) display like 'E'. "下载模板失败!
      exit.
    endif.
  endif.
endform.                    " FRM_GENERATE_TEMPLATE
*&---------------------------------------------------------------------*
*&      Form  FRM_SELECT_FILE
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
form frm_select_file .
  data:lv_window_title type string,
       lv_file_filter  type string value 'Excel Files(*.xls;*.xlsx)|*.xls;*.xlsx|All Files(*.*)|*.*',
       lt_filetable    type filetable with header line,
       lv_rc           type i.
***选择文件
  lv_window_title  = '文件选择'(004).
  call method cl_gui_frontend_services=>file_open_dialog
    exporting
      window_title            = lv_window_title
*     default_extension       =
*     default_filename        =
      file_filter             = lv_file_filter
*     with_encoding           =
      initial_directory       = 'C:\'
*     multiselection          =
    changing
      file_table              = lt_filetable[]
      rc                      = lv_rc
*     user_action             =
*     file_encoding           =
    exceptions
      file_open_dialog_failed = 1
      cntl_error              = 2
      error_no_gui            = 3
      not_supported_by_gui    = 4
      others                  = 5.
  if sy-subrc = 0.
    read table lt_filetable index 1.
    if sy-subrc = 0.
      p_file = lt_filetable-filename.
    endif.
  endif.
endform.                    " FRM_SELECT_FILE
*&---------------------------------------------------------------------*
*&      Form  FRM_UPLOAD_DATA
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
form frm_upload_data .
  data:lv_no(10) type c value '1000000000'.

  define mcr_add_zero.
    if &1 is NOT INITIAL.
    CALL FUNCTION 'CONVERSION_EXIT_ALPHA_INPUT'
      EXPORTING
        input         = &1
     IMPORTING
       OUTPUT        = &1.
    endif.
  end-of-definition.

  call function 'ALSM_EXCEL_TO_INTERNAL_TABLE'
    exporting
      filename                = p_file
      i_begin_col             = '2'
      i_begin_row             = '7'
      i_end_col               = '27'
      i_end_row               = '9999'
    tables
      intern                  = gt_upload[]
    exceptions
      inconsistent_parameters = 1
      upload_ole              = 2
      others                  = 3.
  if sy-subrc <> 0.
* Implement suitable error handling here
  endif.

  if gt_upload[] is initial.
    message '上传文档为空' type 'I'.
    exit.
  endif.

  loop at gt_upload assigning <fs_upload>.
    translate <fs_upload>-value to upper case.
    case <fs_upload>-col.
      when '1'.
        gt_out-bsart = <fs_upload>-value. "订单类型
      when '2'.
        gt_out-lifnr = <fs_upload>-value. "供应商
      when '3'.
        gt_out-ekorg = <fs_upload>-value. "采购组织
      when '4'.
        gt_out-ekgrp  = <fs_upload>-value."采购组
      when '5'.
        gt_out-bukrs = <fs_upload>-value. "公司代码
      when '6'.
        gt_out-bedat = <fs_upload>-value. "采购订单日期
      when '7'.
        gt_out-matnr = <fs_upload>-value. "物料编码
      when '8'.
        gt_out-txz01 = <fs_upload>-value. "物料编码描述
      when '9'.
        gt_out-matkl = <fs_upload>-value. "物料组
      when '10'.
        gt_out-werks  = <fs_upload>-value."工厂
      when '11'.
        gt_out-lgort = <fs_upload>-value. "库位
      when '12'.
        gt_out-pstyp = <fs_upload>-value. "项目类别
      when '13'.
        gt_out-knttp = <fs_upload>-value. "科目分配类别
      when '14'.
        gt_out-kostl = <fs_upload>-value. "成本中心
      when '15'.
        gt_out-anln1 = <fs_upload>-value. "固定资产号
      when '16'.
        gt_out-eindt = <fs_upload>-value. "交货日期
      when '17'.
        gt_out-menge = <fs_upload>-value. "采购订单数量
      when '18'.
        gt_out-meins  = <fs_upload>-value."采购订单单位
      when '19'.
        gt_out-netpr = <fs_upload>-value. "单价
      when '20'.
        gt_out-peinh  = <fs_upload>-value."价格单位
      when '21'.
        gt_out-waers = <fs_upload>-value. "币别
      when '22'.
        gt_out-mwskz = <fs_upload>-value. "税码
      when '23'.
        gt_out-txt20 = <fs_upload>-value. "行项目文本
      when '24'.
        gt_out-ihrez = <fs_upload>-value. "原采购订单号
      when others.
    endcase.
    at end of row.
      if gt_out-bsart is not initial and gt_out-lifnr is not initial and gt_out-ekorg is not initial
          and gt_out-ekgrp is not initial and gt_out-bukrs is not initial .
        add 1 to lv_no.
      endif.
      gt_out-zdcno = lv_no.
      mcr_add_zero:gt_out-lifnr,gt_out-matnr.
      if gt_out-bedat = '00000000'.
        gt_out-bedat = sy-datum.
      endif.
      if gt_out-txz01 = ''.
        select single maktx into gt_out-txz01 from makt where matnr = gt_out-matnr and spras = sy-langu.
      endif.
      append gt_out.
      clear gt_out.
    endat.

  endloop.
***check上传的数据
  perform frm_check_data.
endform.                    " FRM_UPLOAD_DATA
*&---------------------------------------------------------------------*
*&      Form  FRM_CHECK_DATA
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
form frm_check_data .
  data:lv_no1(10) type c.

  data:lt_t161  like t161 occurs 0 with header line,
       lt_t024e like t024e occurs 0 with header line,
       lt_t024  like t024 occurs 0 with header line.

  data:begin of lt_lfa1 occurs 0,
         lifnr like lfa1-lifnr,
       end of lt_lfa1.

  data:begin of lt_t001 occurs 0,
         bukrs like t001-bukrs,
       end of lt_t001.

  data:begin of lt_matnr occurs 0,
         matnr like mara-matnr,
         matkl like mara-matkl,
       end of lt_matnr.

  data:begin of lt_t001w occurs 0,
         werks like t001w-werks,
       end of lt_t001w.

  data:begin of lt_t001l occurs 0,
         werks like t001l-werks,
         lgort like t001l-lgort,
       end of lt_t001l.

  data:lt_t163i like t163i occurs 0 with header line.

  data:begin of lt_vbap occurs 0,
         vbeln like vbap-vbeln,
         posnr like vbap-posnr,
       end of lt_vbap.

  data:begin of lt_marc occurs 0,
         matnr like marc-matnr,
         werks like marc-werks,
         lgfsb like marc-lgfsb,
         lgpro like marc-lgpro,
       end of lt_marc.

  data:lv_banfn like eban-banfn,
       lv_pstyp like eban-pstyp.

  data:begin of lt_ekpo occurs 0,
         ebeln like ekpo-ebeln,
         ebelp like ekpo-ebelp,
         banfn like ekpo-banfn,
         bnfpo like ekpo-bnfpo,
       end of lt_ekpo.

  data:begin of lt_rfq occurs 0,
         ebeln like ekko-ebeln,
         ebelp like ekpo-ebelp,
       end of lt_rfq.

  data:lv_subrc type sy-subrc.
  data:lv_index like sy-tabix.

  define  mcr_add_msg.
    if gt_out-msg is INITIAL.
      gt_out-msg = &1.
    else.
      CONCATENATE gt_out-msg &1 INTO gt_out-msg SEPARATED BY ','.
    endif.
  end-of-definition.

  check gt_out[] is not initial.
***抓取采购订单类型
  select * into table lt_t161 from t161 where bstyp eq 'F'.
  if sy-subrc = 0.
    sort lt_t161 by bsart.
  endif.

***抓取供应商信息
  select lifnr
    into table lt_lfa1
    from lfa1
    for all entries in gt_out
    where lifnr = gt_out-lifnr.
  if sy-subrc = 0.
    sort lt_lfa1 by lifnr.
  endif.

***抓取采购组织
  select * into table lt_t024e from t024e.
  if sy-subrc = 0.
    sort lt_t024e.
  endif.

***采购组
  select * into table lt_t024 from t024.
  if sy-subrc = 0.
    sort lt_t024.
  endif.

***抓取公司
  select bukrs into corresponding fields of table lt_t001 from t001.
  if sy-subrc = 0.
    sort lt_t001.
  endif.

***抓取物料编码
  select matnr matkl into table lt_matnr from mara for all entries in gt_out where matnr = gt_out-matnr.
  if sy-subrc = 0.
    sort lt_matnr by matnr.
  endif.

***抓取工厂
  select werks into table lt_t001w from t001w.
  if sy-subrc = 0.
    sort lt_t001w.
  endif.

***抓取库位信息
  select werks lgort
    into corresponding fields of table lt_t001l
    from t001l
     for all entries in gt_out
   where werks = gt_out-werks
     and lgort = gt_out-lgort.
  if sy-subrc = 0.
    sort lt_t001l by werks lgort.
  endif.

  select matnr werks lgfsb lgpro
    into corresponding fields of table lt_marc
    from marc
    for all entries in gt_out
    where matnr = gt_out-matnr
      and werks = gt_out-werks.
  sort lt_marc by matnr werks.

  loop at gt_out.
    lv_index = sy-tabix.
***采购订单类型
    at new zdcno.
      lv_no1 = gt_out-zdcno.
      read table gt_out into data(gs) with key zdcno = lv_no1.
      if gt_out-bsart is initial.
        mcr_add_msg:'采购订单类型为空'.
      else.
        read table lt_t161 with key bsart = gt_out-bsart binary search.
        if sy-subrc <> 0.
          mcr_add_msg:'采购订单类型不存在'.
        endif.
      endif.
    endat.
***凭证日期
    at new zdcno.
      if gt_out-bedat is initial.
*        mcr_add_msg:'凭证日期为空'.
      else.
        perform frm_date_check using gt_out-bedat changing lv_subrc.
        if lv_subrc ne 0.
          mcr_add_msg:'凭证日期无效'.
        endif.
      endif.
    endat.
***K3采购订单号
*    if gt_out-ihrez is initial.
*      mcr_add_msg:'原采购订单号'.
*    endif.

***供应商
    at new zdcno.
      if gt_out-lifnr is initial.
        mcr_add_msg:'供应商编码为空'.
      else.
        read table lt_lfa1 with key lifnr = gt_out-lifnr binary search.
        if sy-subrc <> 0.
          mcr_add_msg:'供应商不存在'.
        endif.
      endif.
    endat.
***采购组织
    at new zdcno.
      if gt_out-ekorg is initial.
        mcr_add_msg:'采购组织为空'.
      else.
        read table lt_t024e with key ekorg = gt_out-ekorg binary search.
        if sy-subrc <> 0.
          mcr_add_msg:'采购组织不存在'.
        endif.
      endif.
    endat.
***采购组
    at new zdcno.
      if gt_out-ekgrp is initial.
        mcr_add_msg:'采购组为空'.
      else.
        read table lt_t024 with key ekgrp = gt_out-ekgrp binary search.
        if sy-subrc <> 0.
          mcr_add_msg:'采购组不存在'.
        endif.
      endif.
    endat.
***公司代码
    at new zdcno.
      if gt_out-bukrs is initial.
        mcr_add_msg:'公司代码为空'.
      else.
        read table lt_t001 with key bukrs  = gt_out-bukrs binary search.
        if sy-subrc <> 0.
          mcr_add_msg:'公司代码不存在'.
        endif.
      endif.
    endat.
***物料编码
    if gt_out-matkl = ''.
      if gt_out-matnr is initial.
        mcr_add_msg:'物料编码为空'.
      else.
        read table lt_matnr with key matnr = gt_out-matnr binary search.
        if sy-subrc <> 0.
          mcr_add_msg:'物料编码不存在'.
        else.
          gt_out-matkl = lt_matnr-matkl.
        endif.
      endif.
    endif.

***未清采购订单数量
    if gt_out-menge is initial.
      mcr_add_msg:'采购订单数量为空'.
    endif.

***计划交货日期
    if gt_out-eindt is initial.
*      mcr_add_msg:'计划交货日期为空'.
    else.
      perform frm_date_check using gt_out-eindt changing lv_subrc.
      if lv_subrc ne 0.
        mcr_add_msg:'计划交货日期无效'.
      endif.
    endif.

***工厂
    if gt_out-werks is initial.
      mcr_add_msg:'工厂为空'.
    else.
      read table lt_t001w with key werks = gt_out-werks binary search.
      if sy-subrc <> 0.
        mcr_add_msg:'工厂不存在'.
      endif.
    endif.

***库存地点
    if gt_out-lgort is initial.
      select single lgfsb into gt_out-lgort from marc where matnr = gt_out-matnr and werks = gt_out-werks.
    endif.
    if gt_out-lgort is initial and gt_out-werks is not initial.
      read table lt_t001l with key werks = gt_out-werks lgort = gt_out-lgort binary search.
      if sy-subrc <> 0.
        mcr_add_msg:'工厂与库存地点不匹配'.
      endif.
    endif.

***订单单位
    if gt_out-meins is initial.
      select single meins into gt_out-meins from mara where matnr = gt_out-matnr.
    endif.

***科目分配类别
    case gt_out-knttp.
      when 'K'.
        if gt_out-kostl = ''.
          mcr_add_msg:'成本中心不能为空'.
        endif.
      when 'A'.
        if gt_out-anln1 = ''.
          mcr_add_msg:'固定资产号不能为空'.
        endif.
      when others.
    endcase.

***含税单价 价格单位  税码
    if gt_out-netpr is initial.
      gt_out-change_id = 'U'.
      select single konp~kbetr into gt_out-netpr from a017
        inner join konp on konp~knumh = a017~knumh and konp~kopos = 1
        where a017~kappl = 'M'
        and a017~kschl = 'PB00'
        and a017~lifnr = gs-lifnr
        and a017~matnr = gt_out-matnr
        and a017~ekorg = gs-ekorg
        and a017~werks = gs-werks
        and a017~esokz = '0'
        and a017~datab <= sy-datum
        and a017~datbi >= sy-datum.
      if sy-subrc <> 0.
        mcr_add_msg:'信息记录中无有效价格'.
      endif.
    else.
      gt_out-change_id = 'I'.
    endif.

    if gt_out-mwskz is initial.
      select single eine~peinh eine~mwskz into (gt_out-peinh,gt_out-mwskz)
         from eine
        inner join eina on eina~infnr = eine~infnr
        where eina~matnr = gt_out-matnr
        and eina~lifnr = gs-lifnr
        and eine~ekorg = gs-ekorg
        and eine~esokz = '0'
        and eine~werks = gs-werks.

      if gt_out-mwskz = ''.
        mcr_add_msg:'信息记录中无税码'.
      endif.
    endif.


    if gt_out-msg is initial.
      gt_out-zstat = icon_light_out.
    else.
      gt_out-zstat = icon_red_light.
      gt_out-msgty = 'E'.
    endif.

    modify gt_out index lv_index.
    clear gt_out.
  endloop.


endform.                    " FRM_CHECK_DATA

*&---------------------------------------------------------------------*
*&      Form  FRM_DATE_CHECK
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*      -->P_GT_OUT_BEDAT  text
*      <--P_LV_SUBRC  text
*----------------------------------------------------------------------*
form frm_date_check  using   u_date like sy-datum
                     changing c_subrc like sy-subrc.
  clear : c_subrc.

  call function 'DATE_CHECK_PLAUSIBILITY'
    exporting
      date                      = u_date
    exceptions
      plausibility_check_failed = 1
      others                    = 2.
  if sy-subrc <> 0.
    c_subrc = sy-subrc.
  endif.
endform.                    " FRM_DATE_CHECK
*&---------------------------------------------------------------------*
*&      Form  FRM_GET_FIELDCAT
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
form frm_get_fieldcat .

  mcr_build_fieldcat:'ZSTAT'   '状态'           'C'    '30'   '5',
                     'MSG'     '消息'           'C'    '200'  '30',
                     'BSART'   '订单类型'       'C'    '4'    '6',
                     'LIFNR'   '供应商'         'C'    '10'   '10',
                     'EKORG'   '采购组织'       'C'    '4'    '6',
                     'EKGRP'   '采购组'         'C'    '3'    '6',
                     'BUKRS'   '公司代码'       'C'    '4'    '6',
                     'BEDAT'   '采购订单日期'   'D'    '8'    '10',
                     'MATNR'   '物料编码'       'C'    '18'   '15',
                     'TXZ01'   '物料描述'       'C'    '40'   '30',
                     'MATKL'   '物料组'         'C'    '9'    '8',
                     'WERKS'   '工厂'           'C'    '4'    '4',
                     'LGORT'   '库位'           'C'    '4'    '4',
                     'PSTYP'   '项目类别'       'C'    '1'    '4',
                     'KNTTP'   '科目分配类别'   'C'    '1'    '4',
                     'KOSTL'   '成本中心'       'C'    '10'    '10',
                     'ANLN1'   '固定资产号'     'C'    '12'    '12',
                     'EINDT'   '交货日期'       'D'    '8'    '10',
                     'MENGE'   '采购订单数量'   'P'    '15'   '12',
                     'MEINS'   '采购订单单位'   'C'    '3'    '3',
                     'NETPR'   '单价'           'P'   '11'   '11',
                     'PEINH'   '价格单位'       'C'    '8'   '8',
                     'WAERS'   '币别'           'C'    '5'    '5',
                     'MWSKZ'   '税码'           'C'    '2'    '2',
                     'TXT20'   '行项目文本'     'C'    '20'    '15',
                     'IHREZ'   '原采购订单号'   'C'    '12'   '10'.
  gs_layout-zebra = 'X'.
*  gs_layout-colwidth_optimize = 'X'.

endform.                    " FRM_GET_FIELDCAT
*&---------------------------------------------------------------------*
*&      Form  FRM_OUTPUT_DATA
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
form frm_output_data .
  call function 'REUSE_ALV_GRID_DISPLAY'
    exporting
*     I_INTERFACE_CHECK        = ' '
*     I_BYPASSING_BUFFER       = ' '
*     I_BUFFER_ACTIVE          = ' '
      i_callback_program       = gv_repid
      i_callback_pf_status_set = c_pf_status
      i_callback_user_command  = c_user_command
*     I_CALLBACK_TOP_OF_PAGE   = ' '
*     I_CALLBACK_HTML_TOP_OF_PAGE       = ' '
*     I_CALLBACK_HTML_END_OF_LIST       = ' '
*     I_STRUCTURE_NAME         =
*     I_BACKGROUND_ID          = ' '
*     I_GRID_TITLE             =
*     I_GRID_SETTINGS          =
      is_layout                = gs_layout
      it_fieldcat              = gt_fcat[]
*     IT_EXCLUDING             =
*     IT_SPECIAL_GROUPS        =
*     IT_SORT                  =
*     IT_FILTER                =
*     IS_SEL_HIDE              =
      i_default                = 'X'
      i_save                   = ' '
*     IS_VARIANT               =
*     IT_EVENTS                =
*     IT_EVENT_EXIT            =
*     IS_PRINT                 =
*     IS_REPREP_ID             =
*     I_SCREEN_START_COLUMN    = 0
*     I_SCREEN_START_LINE      = 0
*     I_SCREEN_END_COLUMN      = 0
*     I_SCREEN_END_LINE        = 0
*     I_HTML_HEIGHT_TOP        = 0
*     I_HTML_HEIGHT_END        = 0
*     IT_ALV_GRAPHICS          =
*     IT_HYPERLINK             =
*     IT_ADD_FIELDCAT          =
*     IT_EXCEPT_QINFO          =
*     IR_SALV_FULLSCREEN_ADAPTER        =
* IMPORTING
*     E_EXIT_CAUSED_BY_CALLER  =
*     ES_EXIT_CAUSED_BY_USER   =
    tables
      t_outtab                 = gt_out[]
    exceptions
      program_error            = 1
      others                   = 2.
  if sy-subrc <> 0.
* Implement suitable error handling here
  endif.

endform.                    " FRM_OUTPUT_DATA

*&---------------------------------------------------------------------*
*&      Form  FRM_SET_PF_STATUS
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
form frm_set_pf_status using rt_extab type slis_t_extab.
  set pf-status 'S0100'.
endform.                    " FRM_SET_PF_STATUS

*&---------------------------------------------------------------------*
*&      Form  FRM_USER_COMMAND
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
form frm_user_command  using r_ucomm like sy-ucomm
                         rs_selfield type slis_selfield.

  rs_selfield-refresh = 'X'.
  rs_selfield-col_stable = 'X'.
  rs_selfield-row_stable = 'X'.

  case r_ucomm.
    when 'ZUPLOAD'.
      perform frm_create_po.
    when others.
  endcase.
endform.                    " FRM_USER_COMMAND
*&---------------------------------------------------------------------*
*&      Form  FRM_CREATE_BOM
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
form frm_create_po .
  data:lt_out like gt_out occurs 0 with header line.

  data:ls_poheader    like bapimepoheader,
       ls_poheaderx   like bapimepoheaderx,
       lt_poitem      like bapimepoitem occurs 0 with header line,
       lt_poitemx     like bapimepoitemx occurs 0 with header line,
       lt_poschedule  like bapimeposchedule occurs 0 with header line,
       lt_poschedulex like bapimeposchedulx occurs 0 with header line,
       lt_poaccount   like bapimepoaccount occurs 0 with header line,
       lt_poaccountx  like bapimepoaccountx occurs 0 with header line,
       lt_pocond      like bapimepocond occurs 0 with header line,
       lt_pocondx     like bapimepocondx occurs 0 with header line,
       lt_potextitem  like bapimepotext occurs 0 with header line,
       lt_return      like bapiret2 occurs 0 with header line.
  data:lv_ebelp like ekpo-ebelp.
  data:lv_po_number like ekko-ebeln,
       lv_type      type c,
       lv_msg       type string,
       lv_msg_sum   type string.

  if gv_flag is initial.
    gv_flag = 'X'.
  else.
    message '请不要重复导入数据' type 'E'.
  endif.

  read table gt_out with key msgty = 'E'.
  if sy-subrc = 0.
    message '上载数据存在错误,请先修正' type 'E'.
  endif.

  append lines of gt_out to lt_out.

  loop at lt_out assigning <fs_out>.
    at new zdcno.
      zdcno = <fs_out>-zdcno.
      clear:lv_ebelp,ls_poheader,ls_poheaderx,lv_msg_sum,lv_po_number,lt_poitem,
            lt_poitemx,lt_poschedule,lt_poschedulex,lt_poaccount,lt_poaccountx,
            lt_pocond,lt_pocondx,lt_return,lv_ebelp,lt_potextitem,lt_potextitem[].
      refresh:lt_poitem,lt_poitemx,lt_poschedule,lt_poschedulex,
              lt_poaccount,lt_poaccountx,lt_pocond,lt_pocondx,lt_return.

      ls_poheader-doc_type  = <fs_out>-bsart.
      ls_poheader-vendor    = <fs_out>-lifnr.
      ls_poheader-purch_org = <fs_out>-ekorg.
      ls_poheader-pur_group = <fs_out>-ekgrp.
      ls_poheader-comp_code = <fs_out>-bukrs.
      if <fs_out>-bedat = '00000000'.
        ls_poheader-doc_date  = sy-datum.
      else.
        ls_poheader-doc_date  = <fs_out>-bedat.
      endif.
      select single zterm
        into ls_poheader-pmnttrms
        from lfb1
       where lifnr = <fs_out>-lifnr
         and bukrs = <fs_out>-bukrs.
      ls_poheader-ref_1 = <fs_out>-ihrez. "旧物料

      ls_poheaderx-doc_type  = 'X'.
      ls_poheaderx-doc_date  = 'X'.
      ls_poheaderx-vendor    = 'X'.
      ls_poheaderx-purch_org = 'X'.
      ls_poheaderx-pur_group = 'X'.
      ls_poheaderx-comp_code = 'X'.
      ls_poheaderx-pmnttrms  = 'X'.
      ls_poheaderx-ref_1  = 'X'.
    endat.

***行项目数据
    lv_ebelp = lv_ebelp + 10.
    lt_poitem-po_item = lv_ebelp. "项目
    lt_poitem-material = <fs_out>-matnr.  "物料
    if <fs_out>-txz01 <> ''.
      lt_poitem-short_text = <fs_out>-txz01.  "物料描述
    endif.

    lt_poitem-matl_group = <fs_out>-matkl.  "物料组
    lt_poitem-plant = <fs_out>-werks.     "工厂
    lt_poitem-stge_loc = <fs_out>-lgort.  "库存地点
    lt_poitem-item_cat = <fs_out>-pstyp.  "项目类别
    lt_poitem-acctasscat = <fs_out>-knttp.  "科目分配类别
    lt_poitem-quantity = <fs_out>-menge.  "数量
    lt_poitem-po_unit  = <fs_out>-meins.  " 采购订单单位
    lt_poitem-tax_code  = <fs_out>-mwskz.  " 税码

    call function 'CONVERSION_EXIT_CUNIT_INPUT'
      exporting
        input          = lt_poitem-po_unit
        language       = sy-langu
      importing
        output         = lt_poitem-po_unit
      exceptions
        unit_not_found = 1
        others         = 2.
    lt_poitem-gr_basediv = 'X'. "标识:基于收货的发票验证
    lt_poitem-ir_ind = 'X'.     "发票收据标识
    lt_poitem-gr_ind = 'X'.     "货物收据标识
    lt_pocond-cond_unit  = lt_poitem-po_unit. "条件单位
    append lt_poitem.
    clear lt_poitem.

    lt_poitemx-po_item = lv_ebelp.
    lt_poitemx-material = 'X'.
    if <fs_out>-txz01 <> ''.
      lt_poitemx-short_text = 'X'.
    endif.
    lt_poitemx-matl_group = 'X'.
    lt_poitemx-plant = 'X'.
    lt_poitemx-stge_loc = 'X'.
    lt_poitemx-item_cat = 'X'.
    lt_poitemx-acctasscat = 'X'.
    lt_poitemx-quantity = 'X'.
    lt_poitemx-po_unit  = 'X'.
    lt_poitemx-tax_code  = 'X'.

    lt_poitemx-net_price = 'X'.
    lt_poitemx-po_price = 'X'.
    lt_poitemx-info_rec = 'X'.
    lt_poitemx-info_upd = 'X'.
    lt_poitemx-orderpr_un = 'X'.

    lt_poitemx-gr_basediv = 'X'.
    lt_poitemx-ir_ind = 'X'.
    lt_poitemx-gr_ind = 'X'.
    append lt_poitemx.
    clear lt_poitemx.

***采购价格
    lt_pocond-itm_number = lv_ebelp.
    lt_pocond-cond_type = 'PB00'.
    lt_pocond-cond_value = <fs_out>-netpr.    "价格
    "lt_pocond-COND_UNIT  = lt_poitem-po_unit. "'PC'.
    lt_pocond-cond_p_unt = <fs_out>-peinh."GS_EXCEL_RAW0-PEINH.    "价格单位
    lt_pocond-currency = <fs_out>-waers.
    lt_pocond-change_id    = <fs_out>-change_id.
    append lt_pocond.
    clear lt_pocond.
    lt_pocondx-itm_number = lv_ebelp.
    lt_pocondx-cond_type = 'X'.
    lt_pocondx-cond_value = 'X'.
    lt_pocondx-cond_unit = 'X'.    "条件单位
    lt_pocondx-cond_p_unt = 'X'.    "价格单位
    lt_pocondx-currency = 'X'.
    lt_pocondx-change_id    = 'X'.
    append lt_pocondx.
    clear lt_pocondx.
***计划交货日期
    lt_poschedule-po_item = lv_ebelp.
    lt_poschedule-sched_line = 1.
    lt_poschedule-delivery_date = <fs_out>-eindt.
    lt_poschedule-quantity = <fs_out>-menge.
    append lt_poschedule.
    clear lt_poschedule.

    lt_poschedulex-po_item = lv_ebelp.
    lt_poschedulex-sched_line = 1.
    lt_poschedulex-delivery_date = 'X'.
    lt_poschedulex-quantity = 'X'.
    append lt_poschedulex.
    clear lt_poschedulex.
***科目分配
    lt_poaccount-po_item = lv_ebelp.
    lt_poaccount-serial_no = 1.
    if <fs_out>-knttp = 'K'.    "科目分配类别
      mcr_add_zero <fs_out>-kostl.
      lt_poaccount-costcenter = <fs_out>-kostl. "成本中心
    elseif <fs_out>-knttp = 'A'.
      lt_poaccount-asset_no = <fs_out>-anln1. "固定资产号
    endif.
    append lt_poaccount.
    clear lt_poaccount.

    lt_poaccountx-po_item = lv_ebelp.
    lt_poaccountx-serial_no = 1.
    if <fs_out>-knttp = 'K'.    "科目分配类别
      lt_poaccountx-costcenter = 'X'.
    elseif <fs_out>-knttp = 'A'.
      lt_poaccountx-asset_no = 'X'.
    endif.
    append lt_poaccountx.
    clear lt_poaccountx.
***行项目文本
    lt_potextitem-po_item =  lv_ebelp.
    lt_potextitem-text_id = 'F01'.
    lt_potextitem-text_form = 'X'.
    lt_potextitem-text_line = <fs_out>-txt20.
    append  lt_potextitem.
    clear  lt_potextitem.


***调用BAPI 创建PO
    at end of zdcno.
      call function 'BAPI_PO_CREATE1'
        exporting
          poheader         = ls_poheader
          poheaderx        = ls_poheaderx
*         no_price_from_po = 'X'
        importing
          exppurchaseorder = lv_po_number
        tables
          return           = lt_return
          poitem           = lt_poitem
          poitemx          = lt_poitemx
*         POADDRDELIVERY   =
          poschedule       = lt_poschedule
          poschedulex      = lt_poschedulex
*         POEXPIMPITEM     =
*         POEXPIMPITEMX    =
*         POTEXTHEADER     =
          potextitem       = lt_potextitem
          pocond           = lt_pocond
          pocondx          = lt_pocondx
          poaccount        = lt_poaccount
          poaccountx       = lt_poaccountx.

      if lv_po_number is initial. "存在错误
        lv_type = 'E'.
        call function 'BAPI_TRANSACTION_ROLLBACK'.
        loop at lt_return where type = 'A' or type = 'E'.
          perform frm_get_msg using lt_return changing lv_msg.
          concatenate  lv_msg_sum lv_msg into lv_msg_sum .
          condense lv_msg_sum .
        endloop.
      else.
        lv_type = 'S'.
        call function 'BAPI_TRANSACTION_COMMIT'
          exporting
            wait = 'X'.
        concatenate '创建成功:'(041) lv_po_number into lv_msg_sum .
      endif.
      loop at gt_out where zdcno = <fs_out>-zdcno.
        gt_out-msgty = lv_type.
        case lv_type.
          when 'S'.
            gt_out-zstat = icon_green_light.
          when 'E'.
            gt_out-zstat = icon_red_light.
        endcase.
        gt_out-msg = lv_msg_sum .
        modify gt_out index sy-tabix..
      endloop.
    endat.
  endloop.
endform.                    " FRM_CREATE_BOM

*&---------------------------------------------------------------------*
*&      Form  FRM_GET_MSG
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*      -->P_LT_RETURN  text
*      <--P_LV_MSG  text
*----------------------------------------------------------------------*
form frm_get_msg  using    u_return like bapiret2
                  changing c_message type string.
  clear c_message.
  call function 'MESSAGE_TEXT_BUILD'
    exporting
      msgid               = u_return-id
      msgnr               = u_return-number
      msgv1               = u_return-message_v1
      msgv2               = u_return-message_v2
      msgv3               = u_return-message_v3
      msgv4               = u_return-message_v4
    importing
      message_text_output = c_message.
endform.                    " FRM_GET_MSG

 

posted @ 2021-04-13 09:55  肆意点  阅读(1351)  评论(0编辑  收藏  举报