ABAP 销售订单BAPI创建批导程序

1、效果图

1.1筛选界面截图

ABAP 销售订单BAPI创建批导程序_第1张图片

1.2模板数据上传预览图

ABAP 销售订单BAPI创建批导程序_第2张图片

1.3导入效果图

ABAP 销售订单BAPI创建批导程序_第3张图片
ABAP 销售订单BAPI创建批导程序_第4张图片

2、附赠源代码

*&---------------------------------------------------------------------*
*& Report ZVA01
*&---------------------------------------------------------------------*
*&
*&---------------------------------------------------------------------*
report zva01.
include: zotnp001."EXCEL封装的对象,及相关的操作方法
tables: sscrfields."定义按钮
type-pools: slis.
***********************************************************************
*
*数据类型定义逻辑块
*
************************************************************************
data: gw_textfield like smp_dyntxt.
data:   bdcdata like bdcdata    occurs 0 with header line.
data:   messtab like bdcmsgcoll occurs 0 with header line.
*data:  messtab2 like TABLE OF bdcmsgcoll .
data:   ctumode like ctu_params-dismode.
data:   g_dcount(5) type n value 0. "记录数
data:   ref_grid    type ref to cl_gui_alv_grid.
data:it_fieldcat type lvc_t_fcat,
     wa_fieldcat like line of it_fieldcat.
data:l_kalks type  knvv-kalks , "定价过程
     l_kschl type a904-kschl. "条件类型
"导入数据
data:begin of gt_tab occurs 0,
       txt     type string,
       index   type string, "销售订单
       auart   type VBAK-auart, "销售凭证类型
       vkorg   type VBAK-vkorg, "销售组织
       vtweg   type VBAK-vtweg, "分销渠道
       spart   type VBAK-spart, "产品组
       vkbur   type VBAK-vkbur, "销售办事处
       kunnr   type string, "售达方
       kunag   type string, "送达方
       bstnk   type string, "客户参考
       posnr   type string, "行项目
       matnr   type string, "物料
       kwmeng  type string, "订单数量
       werks   type string, "工厂
       lgort   type string, "库存地点
       augru   type string, "订货原因
       remarks type string, "抬头备注
       lin     type string, "行项目备注
       prsdt   type string, "行项目定价日期
     end of gt_tab.
data: begin of gt_out occurs 0, "显示结果
        txt     type string,
        index   type string, "销售订单
        auart   type VBAK-auart, "销售凭证类型
        vkorg   type VBAK-vkorg, "销售组织
        vtweg   type VBAK-vtweg, "分销渠道
        spart   type VBAK-spart, "产品组
        vkbur   type VBAK-vkbur, "销售办事处
        kunnr   type vbak-kunnr, "售达方
        kunag   type likp-kunag, "送达方
        bstnk   type string, "客户参考
        posnr   type vbap-posnr, "行项目
        matnr   type vbap-matnr, "物料
        kwmeng2 type string, "订单数量
        werks   type string, "工厂
        lgort   type string, "库存地点
        augru   type string, "订货原因
        remarks type string, "抬头备注
        lin     type string, "行项目备注
        prsdt   type sy-datum, "行项目定价日期
        box(1),
        text    type icons-text, "标识
        tabix   type string, "文本
        kschl   type konp-kschl, "条件类型
        kbetr   type konp-kbetr, "条件金额
        konwa   type konp-konwa, "条件货币
        kpein   type konp-kpein, "条件单位
        kmein   type konp-kmein, "计量单位
        kwmeng  type vbap-kwmeng, "订单数量
      end of gt_out.
data:begin of gs_a904,
       kschl type konp-kschl, "条件类型
       kbetr type konp-kbetr, "条件金额
       konwa type konp-konwa, "条件货币
       kpein type konp-kpein, "条件单位
       kmein type konp-kmein, "计量单位
     end of gs_a904.
define init_fieldcat.
  CLEAR WA_FIELDCAT.
  WA_FIELDCAT-FIELDNAME = &1.
  WA_FIELDCAT-COLTEXT = &2.
  APPEND WA_FIELDCAT TO IT_FIELDCAT.
end-of-definition.

************************************************************************
*
*选择界面程序逻辑块
*
************************************************************************
selection-screen begin of block blk1 with frame title text-100.
parameters : p_file like rlgrap-filename memory id file.
selection-screen end of block blk1.

selection-screen function key 1.

initialization.
  gw_textfield-icon_id = '@EZ@'.
  gw_textfield-icon_text = text-001.
  sscrfields-functxt_01 = gw_textfield.
  "点击下载模板

at selection-screen.
  case sscrfields-ucomm.
    when 'FC01'.
      perform frm_get_excel_templet.
  endcase.
  "选择文件路径

at selection-screen output.

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

start-of-selection.
  if p_file is initial.
    message '请选择导入模板路径' type 'S' display like 'E'.
    exit.
  endif.

************************************************************************
*
*数据处理逻辑块
*
************************************************************************
  "获取到上传文档的数据
  perform frm_upload_data.
  "处理上传文档的数据
  perform frm_deal_data.
  "AVL输出
  perform frm_output.


















form frm_upload_data .
  data :lt_excel like alsmex_tabline occurs 0 with header line,
        l_col    type i.

  field-symbols <fs>.
  field-symbols: <fs_tab>  type standard table,
                 <fs_line> type any.

  data: dref_table type ref to data,
        dref_line  type ref to data.

  create data dref_table like table of gt_tab.
  assign dref_table->* to <fs_tab>.

  create data dref_line like gt_tab.
  assign dref_line->* to <fs_line>.


  call function 'ALSM_EXCEL_TO_INTERNAL_TABLE'
    exporting
      filename                = p_file
      i_begin_col             = 1
      i_begin_row             = 2
      i_end_col               = 50
      i_end_row               = 20000
    tables
      intern                  = lt_excel
    exceptions
      inconsistent_parameters = 1
      upload_ole              = 2
      others                  = 3.
************把 表 LT_TAB 的内容给到UPD_TAB1
  clear : gt_tab. refresh :gt_tab.
  delete lt_excel where row = '0001'.
  loop at lt_excel.
    l_col = lt_excel-col.
    assign component l_col of structure <fs_line> to <fs>.
    if <fs> is assigned.
      <fs> = lt_excel-value.
    endif.

    at end of row.
      append <fs_line> to <fs_tab>.
      clear <fs_line>.
    endat.
  endloop.
  append lines of <fs_tab> to gt_tab.


  g_dcount = lines( gt_tab ).  "导入的记录数
  unassign: <fs>,<fs_tab>,<fs_line>.
  clear: lt_excel[],lt_excel,
         l_col,
         dref_table,
         dref_line.
endform.                    " FRM_UPLOAD_DATA



form frm_va01_bapi.
  data:l_order_header_in      like bapisdhd1, "销售订单抬头
       l_order_header_inx     like bapisdhd1x, "销售订单抬头更新
       gt_return              like table of bapiret2 with header line, "返回参数
       gt_order_items_in      like table of bapisditm with header line, "销售订单项目
       gt_order_items_inx     like table of bapisditmx with header line, "销售订单项目更新
       gt_order_partners      like table of bapiparnr with header line, "销售订单合作伙伴
       l_sender               like bapi_sender, "发送人逻辑系统
       l_salesdocumentin      like bapivbeln-vbeln, "销售订单
       l_logic_switch         like bapisdls, "逻辑切换的 SD 复选框
       l_order_conditions_in  like table of bapicond with header line, "条件类型
       l_order_conditions_inx like table of bapicondx with header line, "条件类型更新
       l_salesdocument        like bapivbeln-vbeln, "BAPI 销售订单
       gt_order_schedules_in  like table of bapischdl with header line, "计划行
       gt_order_schedules_inx like table of bapischdlx with header line, "计划行更新。
       GT_ORDER_TEXT LIKE TABLE OF BAPISDTEXT WITH HEADER LINE."文本
  data:cp_eind type string,
       l_w     type string,
       i_mes   type char200.


  "记录销售订单抬头,用作循环使用
  data:begin of gt_head occurs 0,
         index type string, "销售订单
         auart type string, "销售凭证类型
         vkorg type string, "销售组织
         vtweg type string, "分销渠道
         spart type string, "产品组
         vkbur type string, "销售办事处
         bstnk type string, "客户参考
         augru type string, "订货原因
         kunnr type string, "售达方
         kunag type string, "送达方
          Remarks TYPE STRING,
       end of gt_head.

  "获取到选中的订单
  loop at gt_out where box = 'X'.
    gt_head-index = gt_out-index.
    gt_head-auart = gt_out-auart.
    gt_head-vkorg = gt_out-vkorg.
    gt_head-vtweg = gt_out-vtweg.
    gt_head-spart = gt_out-spart.
    gt_head-vkbur = gt_out-vkbur.
    gt_head-bstnk = gt_out-bstnk.
    gt_head-augru = gt_out-augru.
    gt_head-kunnr = gt_out-kunnr .
    gt_head-kunag = gt_out-kunag.
    GT_HEAD-Remarks = GT_OUT-Remarks.
    append gt_head.
  endloop.
  "删除重复项
  delete adjacent duplicates from gt_head comparing all fields.
  sort gt_head.
*  break k99997.

  "进行数据处理
  loop at gt_head.
    "销售订单
    clear l_salesdocumentin.
*  L_SALESDOCUMENTIN = GT_HEAD-INDEX.
    l_logic_switch-pricing = 'G'.
    "销售订单抬头
    clear l_order_header_in.
    l_order_header_in-doc_type = gt_head-auart."销售订单凭证类型
    l_order_header_in-sales_org = gt_head-vkorg."销售组织
    l_order_header_in-distr_chan = gt_head-vtweg."分销渠道
    l_order_header_in-division = gt_head-spart."产品组
    l_order_header_in-sales_off = gt_head-vkbur."销售办事处
    l_order_header_in-purch_no_c = gt_head-bstnk."客户参考
    l_order_header_in-req_date_h = sy-datum."要求的交货日期
    l_order_header_in-doc_date = sy-datum."凭证日期(接收/发送)

    if gt_head-auart = 'ZRE' or gt_head-auart = 'ZFR'.
      l_order_header_in-ord_reason = gt_head-augru."订单原因
    endif.

    "抬头更新
    clear l_order_header_inx.
    l_order_header_inx-doc_type = 'X'.
    l_order_header_inx-sales_org = 'X'.
    l_order_header_inx-distr_chan = 'X'.
    l_order_header_inx-division = 'X'.
    l_order_header_inx-sales_off = 'X'.
    l_order_header_inx-purch_no_c = 'X'.
    l_order_header_inx-req_date_h = 'X'.
    l_order_header_inx-doc_date = 'X'.
    if gt_out-auart = 'ZRE'.
      l_order_header_inx-ord_reason = 'X'.
    endif.
    "销售合作伙伴

    gt_order_partners-partn_role = 'AG'."售达方
    gt_order_partners-partn_numb = gt_head-kunnr."售达方编号
*    GT_ORDER_PARTNERS-ITM_NUMBER = GT_OUT-POSNR."销售订单项目
    append gt_order_partners.
    clear gt_order_partners.

    gt_order_partners-partn_role = 'WE'."送达方
    gt_order_partners-partn_numb = gt_head-kunag."送达方编号
*    GT_ORDER_PARTNERS-ITM_NUMBER = GT_OUT-POSNR."销售订单项目
    append gt_order_partners.
    clear gt_order_partners.

    "抬头备注
    GT_ORDER_TEXT-TEXT_ID = 'Z003'."文本ID
    GT_ORDER_TEXT-LANGU = '1'."语言
    GT_ORDER_TEXT-TEXT_LINE = gt_head-Remarks."文本
    APPEND GT_ORDER_TEXT.
    CLEAR GT_ORDER_TEXT.



    loop at gt_out where box = 'X' and index = gt_head-index.
      call function 'CONVERSION_EXIT_CUNIT_INPUT'
        exporting
          input  = gt_out-kmein
        importing
          output = gt_out-kmein.



      "销售订单项目
      gt_order_items_in-itm_number = gt_out-posnr."销售订单项目
      gt_order_items_in-material = gt_out-matnr."物料编码
      gt_order_items_in-plant = gt_out-werks."工厂
      gt_order_items_in-store_loc = gt_out-lgort."库存地点
      gt_order_items_in-target_qty = gt_out-kwmeng."目标数量
      gt_order_items_in-price_date = gt_out-prsdt."行项目定价日期
      append gt_order_items_in.
      clear gt_order_items_in.
      "销售订单项目更新

      gt_order_items_inx-itm_number = gt_out-posnr.
      gt_order_items_inx-material = 'X'.
      gt_order_items_inx-plant = 'X'.
      gt_order_items_inx-store_loc = 'X'.
      gt_order_items_inx-target_qty = 'X'.
      gt_order_items_inx-price_date = 'X'.
      append gt_order_items_inx.
      clear gt_order_items_inx.

      "计划行
      gt_order_schedules_in-itm_number = gt_out-posnr."销售凭证项目
      gt_order_schedules_in-req_qty = gt_out-kwmeng."各销售单位订单数量
      append gt_order_schedules_in.
      clear gt_order_schedules_in.
      gt_order_schedules_inx-itm_number = gt_out-posnr."销售凭证项目
      gt_order_schedules_inx-req_qty = 'X'."各销售单位订单数量
      append gt_order_schedules_inx.
      clear gt_order_schedules_inx.

      "条件类型
      l_order_conditions_in-itm_number = gt_out-posnr."销售订单项目
      l_order_conditions_in-cond_type = gt_out-kschl."条件类型
      l_order_conditions_in-cond_value = gt_out-kbetr."条件金额
      l_order_conditions_in-currency = gt_out-konwa."条件货币
      l_order_conditions_in-cond_unit = gt_out-kmein ."条件单位
      l_order_conditions_in-cond_p_unt = gt_out-kpein."条件定价单位
      l_order_conditions_in-curr_iso = gt_out-konwa."IOS代码货币
      append l_order_conditions_in.
      clear l_order_conditions_in.
      "条件类型更新
      l_order_conditions_inx-itm_number = gt_out-posnr."销售订单项目
      l_order_conditions_inx-cond_type = gt_out-kschl."条件类型
      l_order_conditions_inx-cond_value = 'X'."条件金额
      l_order_conditions_inx-currency = 'X'."条件货币
      l_order_conditions_inx-cond_unit = 'X' ."条件单位
      l_order_conditions_inx-cond_p_unt = 'X'."条件定价单位
      append l_order_conditions_inx.
      clear l_order_conditions_inx.

          "项目备注
    GT_ORDER_TEXT-TEXT_ID = 'Z005'."文本 ID
    GT_ORDER_TEXT-ITM_NUMBER = gt_out-posnr."销售订单项目
    GT_ORDER_TEXT-LANGU = '1'."语言代码
    GT_ORDER_TEXT-TEXT_LINE = gt_out-LIN."文本行
    APPEND GT_ORDER_TEXT.
    CLEAR GT_ORDER_TEXT.


    endloop.

    "调用销售订单创建BAPI
    if gt_head-auart = 'ZRE'.
      "由于BAPI:BAPI_SALESORDER_CREATEFROMDAT2把业务对象定义死了, 所以退货订单用此BAPI进行创建
      call function 'SD_SALESDOCUMENT_CREATE'
        exporting
*         salesdocument        = salesdocumentin
          sales_header_in      = l_order_header_in
          sales_header_inx     = l_order_header_inx
*         sender               = sender
*         binary_relationshiptype = binary_relationshiptype
*         int_number_assignment   = int_number_assignment
*         behave_when_error    = behave_when_error
          logic_switch         = l_logic_switch
*         business_object      = 'BUS2032'
*         testrun              = testrun
*         convert_parvw_auart  = convert
        importing
          salesdocument_ex     = l_salesdocument
        tables
          return               = gt_return[]
          sales_items_in       = gt_order_items_in[]
          sales_items_inx      = gt_order_items_inx[]
          sales_partners       = gt_order_partners[]
          sales_schedules_in   = gt_order_schedules_in[]
          sales_schedules_inx  = gt_order_schedules_inx[]
          sales_conditions_in  = l_order_conditions_in[]
          sales_conditions_inx = l_order_conditions_inx[]
*         sales_cfgs_ref       = order_cfgs_ref
*         sales_cfgs_inst      = order_cfgs_inst
*         sales_cfgs_part_of   = order_cfgs_part_of
*         sales_cfgs_value     = order_cfgs_value
*         sales_cfgs_blob      = order_cfgs_blob
*         sales_cfgs_vk        = order_cfgs_vk
*         sales_cfgs_refinst   = order_cfgs_refinst
*         sales_ccard          = order_ccard
         sales_text           = GT_ORDER_TEXT[]
*         sales_keys           = order_keys
*         extensionin          = extensionin
*         partneraddresses     = partneraddresses
*         extensionex          = extensionex
        .
    else.
      clear l_salesdocument.
      call function 'BAPI_SALESORDER_CREATEFROMDAT2'
        exporting
*         SALESDOCUMENTIN      = L_SALESDOCUMENTIN
          order_header_in      = l_order_header_in
          order_header_inx     = l_order_header_inx
*         SENDER               = L_SENDER
*         BINARY_RELATIONSHIPTYPE       =
*         INT_NUMBER_ASSIGNMENT         =
*         BEHAVE_WHEN_ERROR    =
          logic_switch         = l_logic_switch
*         TESTRUN              =
*         CONVERT              = ' '
        importing
          salesdocument        = l_salesdocument
        tables
          return               = gt_return[]
          order_items_in       = gt_order_items_in[]
          order_items_inx      = gt_order_items_inx[]
          order_partners       = gt_order_partners[]
          order_schedules_in   = gt_order_schedules_in[]
          order_schedules_inx  = gt_order_schedules_inx[]
          order_conditions_in  = l_order_conditions_in[]
          order_conditions_inx = l_order_conditions_inx[]
*         ORDER_CFGS_REF       =
*         ORDER_CFGS_INST      =
*         ORDER_CFGS_PART_OF   =
*         ORDER_CFGS_VALUE     =
*         ORDER_CFGS_BLOB      =
*         ORDER_CFGS_VK        =
*         ORDER_CFGS_REFINST   =
*         ORDER_CCARD          =
         ORDER_TEXT           = GT_ORDER_TEXT[]
*         ORDER_KEYS           =
*         EXTENSIONIN          =
*         PARTNERADDRESSES     =
*         EXTENSIONEX          =
*         NFMETALLITMS         =
        .
    endif.


* 处理错误消息:通过判断消息的类型,来判断BAPI是否成功
    clear i_mes.
    loop at gt_return.
      i_mes = i_mes && '*' &&  gt_return-type && gt_return-number && gt_return-message.
      if gt_return-type eq 'E' or
          gt_return-type = 'A' or
          gt_return = 'X'.
        cp_eind = 'X'.  "失败
      endif.

      if gt_return-type eq 'W'.
        l_w = 'X'.
      endif.
    endloop.

    if cp_eind ne 'X'.
      if l_w <>  'X'.
        "存在警告信息
        loop at gt_out where index = gt_head-index and box = 'X'.
          gt_out-tabix = i_mes.
          gt_out-text = icon_green_light .
          gt_out-index = l_salesdocument.
          modify gt_out.
        endloop.
      else.
        "成功生成订单
        loop at gt_out where index = gt_head-index and box = 'X'.
          gt_out-tabix = i_mes.
          gt_out-text = icon_yellow_light  .
          gt_out-index = l_salesdocument.
          modify gt_out.
        endloop.
      endif.

      "提交事务
      call function 'BAPI_TRANSACTION_COMMIT'
        exporting
          wait = 'X'.
      commit work and wait.
    else.
      "存在错误,创建失败
      loop at gt_out where index = gt_head-index and box = 'X'.
        gt_out-tabix = i_mes.
        gt_out-text = icon_red_light .
        modify gt_out.
      endloop.
      "回滚事务
      call function 'BAPI_TRANSACTION_ROLLBACK' .
    endif.

    clear l_order_header_in.
    clear l_order_header_inx.
    clear l_logic_switch.
    clear l_salesdocument.
    clear gt_return[].
    clear gt_order_items_in[].
    clear gt_order_items_inx[].
    clear gt_order_partners[].
    clear l_order_conditions_in[].
    clear l_order_conditions_inx[].
    clear gt_order_schedules_in[].
    clear gt_order_schedules_inx[].
    clear cp_eind.
    clear l_w.
  endloop.







endform.
form bdc_dynpro using program dynpro.
  clear bdcdata.
  bdcdata-program  = program.
  bdcdata-dynpro   = dynpro.
  bdcdata-dynbegin = 'X'.
  append bdcdata.
endform.
form bdc_field using fnam fval.
  clear bdcdata.
  bdcdata-fnam = fnam.
  bdcdata-fval = fval.
  append bdcdata.
endform.
*&---------------------------------------------------------------------*
*&      FORM  FRM_GET_EXCEL_TEMPLET
*&---------------------------------------------------------------------*
*       下载EXCEL模版
*----------------------------------------------------------------------*
*  -->  P1        TEXT
*  <--  P2        TEXT
*----------------------------------------------------------------------*
form frm_get_excel_templet .
  data: l_excel_control type ref to excel_control,
        l_objid         type string value 'ZVA01',  "模板对象名
        l_filename      type string value '销售订单导入模板',  "模板的EXCEL文件名
        l_br,               "是否显示路径选择
        l_dst           type string value 'D:\'.    " 路径

  "创建对象
  create object l_excel_control.

  "下载相关的EXCEL模板
  call method l_excel_control->get_excel_templet
    exporting
      i_objid                  = l_objid
      i_filename               = l_filename
      is_back_run              = l_br
    changing
      c_destination            = l_dst
    exceptions
      templet_is_not_fount     = 1
      can_not_save_file        = 2
      templet_download_failure = 3
      others                   = 4.
  if sy-subrc ne 0.
    if sy-subrc eq 1.
      message '没有找到相应的模板!' type 'S' display like 'E'.
      stop.
    elseif sy-subrc eq 2.
      stop.
    elseif sy-subrc eq 3.
      message '模板下载失败!' type 'S' display like 'E'.
      stop.
    else.
      message '取得模板错误!' type 'S' display like 'E'.
      stop.
    endif.
  endif.

  clear: l_excel_control,
         l_objid,  "模板对象名
         l_filename,  "模板的EXCEL文件名
         l_br,               "是否显示路径选择
         l_dst.    " 路径
endform.                    " FRM_GET_EXCEL_TEMPLET
*&---------------------------------------------------------------------*
*&      FORM  GET_TEMPATH
*&---------------------------------------------------------------------*
*       TEXT
*----------------------------------------------------------------------*
*  -->  P1        TEXT
*  <--  P2        TEXT
*----------------------------------------------------------------------*
form get_tempath .
  call function 'WS_FILENAME_GET'
    exporting
      def_filename     = ' '
      def_path         = 'C:\'
      mask             = 'EXCEL 文件 (*.XLS)|*.XLS;*.XLSX|'
      mode             = '0'
      title            = 'FIND THE TEXT FILE FOR UPLOAD'
    importing
      filename         = p_file
    exceptions
      inv_winsys       = 1
      no_batch         = 2
      selection_cancel = 3
      selection_error  = 4
      others           = 5.

endform.                    " GET_TEMPATH
*&---------------------------------------------------------------------*
*& Form FRM_DEAL_DATA
*&---------------------------------------------------------------------*
*& 处理文档数据
*&---------------------------------------------------------------------*
*& -->  p1        text
*& <--  p2        text
*&---------------------------------------------------------------------*
form frm_deal_data .
  "记录销售订单抬头,用作循环使用
  data:begin of gt_head occurs 0,
         index   type string, "销售订单
         auart   type string, "销售凭证类型
         vkorg   type string, "销售组织
         vtweg   type string, "分销渠道
         spart   type string, "产品组
         vkbur   type string, "销售办事处
         bstnk   type string, "客户参考
         augru   type string, "订货原因
         kunnr   type string, "售达方
         kunag   type string, "送达方
         remarks type string, "抬头备注
       end of gt_head.
  data:l_index type i.
  data:matcher type ref to cl_abap_matcher.
  data:match   type c length 1.
  DATA:L_QX TYPE CHAR1."记录是否有权限
  "获取到选中的订单
  loop at gt_tab.
    gt_head-index = gt_tab-index.
    gt_head-auart = gt_tab-auart.
    gt_head-vkorg = gt_tab-vkorg.
    gt_head-vtweg = gt_tab-vtweg.
    gt_head-spart = gt_tab-spart.
    gt_head-vkbur = gt_tab-vkbur.
    gt_head-bstnk = gt_tab-bstnk.
    gt_head-augru = gt_tab-augru.
    gt_head-kunnr = gt_tab-kunnr .
    gt_head-kunag = gt_tab-kunag.
    gt_head-remarks = gt_tab-remarks .
    append gt_head.
  endloop.
  "删除重复项
  delete adjacent duplicates from gt_head comparing all fields.
  sort gt_head.



  loop at gt_tab.
    move-corresponding gt_tab to gt_out.
    clear l_kalks.
    call function 'CONVERSION_EXIT_ALPHA_INPUT'
      exporting
        input  = gt_out-kunnr
      importing
        output = gt_out-kunnr.
    call function 'CONVERSION_EXIT_ALPHA_INPUT'
      exporting
        input  = gt_out-kunag
      importing
        output = gt_out-kunag.
    call function 'CONVERSION_EXIT_ALPHA_INPUT'
      exporting
        input  = gt_out-posnr
      importing
        output = gt_out-posnr.
    call function 'CONVERSION_EXIT_MATN1_INPUT'
      exporting
        input  = gt_out-matnr
      importing
        output = gt_out-matnr.

    "权限校验
    CLEAR L_QX.
*    BREAK-POINT.
    "销售部门权限
      authority-check object 'ZVKBUR1'
          id 'ZVKBUR'
          field GT_OUT-VKBUR.
  if sy-subrc <> 0.
      gt_out-tabix = gt_out-tabix && '*没有销售部门' && GT_OUT-VKBUR && '的权限' .
      gt_out-text = icon_red_light.
      L_QX = 'X'.
  ENDIF.
  "凭证类型权限
 authority-check object 'V_VBAK_AAT'
          id 'AUART'
          field GT_OUT-AUART
          id 'ACTVT'
          FIELD '01'.
   if sy-subrc <> 0.
      gt_out-tabix = gt_out-tabix && '*没有凭证类型' && GT_OUT-AUART && '创建的权限' .
      gt_out-text = icon_red_light.
      L_QX = 'X'.
  ENDIF.
"销售范围权限
   authority-check object 'V_VBAK_VKO'
          id 'VKORG'
          field GT_OUT-VKORG
          id 'VTWEG'
          FIELD GT_OUT-VTWEG
          ID 'SPART'
          FIELD GT_OUT-SPART
          ID 'ACTVT'
          FIELD '01'.
   if sy-subrc <> 0.
      gt_out-tabix = gt_out-tabix && '*没有销售范围' && GT_OUT-VKORG && '创建的权限' .
      gt_out-text = icon_red_light.
      L_QX = 'X'.
  ENDIF.




IF L_QX <> 'X'.
  "有权限
      "检查必填字段
    if gt_out-index is initial or gt_out-auart is initial or gt_out-vkorg is initial or gt_out-vtweg is initial or
       gt_out-spart is initial or  gt_out-vkbur is initial or gt_out-kunnr is initial or gt_out-kunag is initial or
       gt_out-posnr is initial or gt_out-matnr is initial or gt_out-kwmeng is initial or gt_out-werks is initial or
       gt_out-lgort is initial.
      gt_out-tabix = gt_out-tabix && '*存在必填字段为空值,请检查!' .
      gt_out-text = icon_red_light.
    endif.
    if gt_out-auart = 'ZOR' or gt_out-auart = 'ZYR'.
      if gt_out-bstnk is initial.
        gt_out-tabix = gt_out-tabix && '*客户参考必填,请检查!'   .
        gt_out-text = icon_red_light.
      endif.
    elseif gt_out-auart = 'ZRE' or gt_out-auart = 'ZFR'.
      if gt_out-augru is initial.
        gt_out-tabix =  gt_out-tabix &&  '*订货原因必填,请检查!' .
        gt_out-text = icon_red_light.
      endif.

    endif.


    "校验抬头数据一致性
    clear l_index.
    loop  at gt_head where index = gt_tab-index.
      l_index = l_index + 1.
    endloop.
    if l_index > 1.
      gt_out-tabix =  gt_out-tabix &&  '*该订单抬头数据不一致,请检查!' .
      gt_out-text = icon_red_light.
    endif.

    "校验订单类型是否正确
    if gt_out-auart = 'ZOR' or gt_out-auart = 'ZYR' or gt_out-auart = 'ZRE' or gt_out-auart = 'ZFR'.
    else.
      gt_out-tabix =  gt_out-tabix &&  '*不能导入此' && gt_out-auart && '订单类型,请检查!' .
      gt_out-text = icon_red_light.
    endif.

    "校验销售订单、行项目是否为数字
    clear: matcher , match .
    "正则表达式
    matcher = cl_abap_matcher=>create( pattern = '\d+' text = gt_out-index ).
    match = matcher->match( ).
    if match <> 'X'.
      gt_out-tabix =  gt_out-tabix &&  '*销售订单' && gt_out-index  && '不是数字类型,请填写数字!' .
      gt_out-text = icon_red_light.
    endif.
    clear: matcher , match .
    "正则表达式
    matcher = cl_abap_matcher=>create( pattern = '\d+' text = gt_out-posnr ).
    match = matcher->match( ).
    if match <> 'X'.
      gt_out-tabix =  gt_out-tabix &&  '*行项目' && gt_out-posnr  && '不是数字类型,请填写数字!' .
      gt_out-text = icon_red_light.
    endif.



    "校验售达方和送达方
    select
        *
        from knvv
        into table @data(gt_knvv)
        where kunnr = @gt_out-kunnr
        and vkorg = @gt_out-vkorg
        and vtweg = '00'
        and spart = '00'.
    if gt_knvv is initial.
      gt_out-tabix =  gt_out-tabix &&  '*请BP维护售达方' && gt_out-kunnr  && '的'  && gt_out-vkorg && '销售视图!'.
      gt_out-text = icon_red_light.
    endif.
    clear gt_knvv.
    if gt_out-kunnr <> gt_out-kunag.
      "售达方与送达方不一样的时候,检查送达方
      select
      *
      from knvv
      into table gt_knvv
      where kunnr = gt_out-kunag
      and vkorg = gt_out-vkorg
      and vtweg = '00'
      and spart = '00'.
      if gt_knvv is initial.
        gt_out-tabix =  gt_out-tabix &&  '*请BP维护送达方' && gt_out-kunag  && '的'  && gt_out-vkorg && '销售视图!'.
        gt_out-text = icon_red_light.
      endif.
      clear gt_knvv.
    endif.

    "校验行项目
    gt_tab-posnr = gt_tab-posnr mod 10.
    if gt_tab-posnr <> 0.
      gt_out-tabix =  gt_out-tabix &&  '*行项目,请以10为单位递增!'.
      gt_out-text = icon_red_light.
    endif.

    "物料工厂校验
    select
      *
      from mara
      into table @data(gt_mara)
      where matnr = @gt_out-matnr.
    if gt_mara is initial.
      gt_out-tabix =  gt_out-tabix &&  '*该' && gt_out-matnr &&  '物料不存在'.
      gt_out-text = icon_red_light.

    else.
      "物料存在
      "工厂检验
      select
  *
  from marc
  into table @data(gt_marc)
  where matnr = @gt_out-matnr
  and werks = @gt_out-werks.
      if gt_marc is initial.
        gt_out-tabix =  gt_out-tabix &&  '*该' && gt_out-matnr && '物料不存在' && gt_out-werks && '工厂视图!'.
        gt_out-text = icon_red_light.
      else.
        "工厂存在
        "库存地点检验
        select
       *
       from mard
       into table @data(gt_mard)
       where matnr = @gt_out-matnr
       and werks = @gt_out-werks
       and lgort = @gt_out-lgort.
          IF gt_mard is INITIAL.
                    gt_out-tabix =  gt_out-tabix &&  '*该' && gt_out-matnr && '物料不存在' && gt_out-werks && '工厂下的' && gt_out-lgort && '库存地点!'.
                    gt_out-text = icon_red_light.
          ENDIF.
      endif.

      "销售视图校验
      SELECT
        *
        FROM MVKE
        INTO TABLE @DATA(GT_MVKE)
        WHERE matnr = @gt_out-matnr
        AND VKORG = @GT_OUT-VKORG
        AND VTWEG = '00'.
        IF  GT_MVKE IS INITIAL.
            gt_out-tabix =  gt_out-tabix &&  '*该' && gt_out-matnr && '物料不存在' && GT_OUT-VKORG && '销售视图!' .
            gt_out-text = icon_red_light.
        ENDIF.
    endif.
    clear gt_mara.
    clear gt_marc.
    clear gt_mard.
    CLEAR GT_MVKE.
      IF GT_OUT-AUGRU is NOT INITIAL.
        "订货原因检查
    SELECT
      *
      FROM TVAU
      INTO TABLE @DATA(GT_TVAU)
      WHERE AUGRU = @GT_OUT-AUGRU.
      IF GT_TVAU IS INITIAL.
          gt_out-tabix =  gt_out-tabix &&  '*该订货原因不存在,请填写正确的订货原因编码!' .
         gt_out-text = icon_red_light.
      ENDIF.
      ENDIF.

      CLEAR GT_TVAU.


    "条件类型
    if gt_out-prsdt is initial.
      gt_out-prsdt = sy-datum.
    endif.

    if gt_out-auart <> 'ZFR'.
      select
        kalks
        from knvv
        into l_kalks
        where kunnr = gt_out-kunnr
        and vkorg = gt_out-vkorg
        and vtweg = '00'
        and spart = '00'.
      endselect.
      if l_kalks = 'Z1'.
        "不含税
        if gt_out-auart = 'ZYR'.
          gt_out-kschl = 'ZPR5'.
        else.
          gt_out-kschl = 'ZPR1'.
        endif.
      elseif l_kalks = 'Z2'.
        "含税
        if gt_out-auart = 'ZYR'.
          gt_out-kschl = 'ZPR4'.
        else.
          gt_out-kschl = 'ZPR0'.
        endif.
      endif.



      select
        a904~kschl
        kbetr
        konwa
        kpein
        kmein
        from a904
        join konp on konp~knumh = a904~knumh
        into gs_a904
        where a904~kschl = gt_out-kschl
        and kbstat = '02'
        and a904~kfrst = ''
        and a904~vkorg = gt_out-vkorg
        and a904~kunnr = gt_out-kunnr
        and a904~vkbur = gt_out-vkbur
        and a904~matnr = gt_out-matnr
        and a904~datbi >= gt_out-prsdt
        and a904~datab <= gt_out-prsdt.
      endselect.

      if gs_a904 is initial.
        gt_out-tabix =  gt_out-tabix && '*不存在有效价格,请检查!'.
        gt_out-text = icon_red_light.
      endif.
      gt_out-kschl = gs_a904-kschl.
      gt_out-kbetr = gs_a904-kbetr.
      gt_out-konwa = gs_a904-konwa.
      gt_out-kpein = gs_a904-kpein.
      gt_out-kmein = gs_a904-kmein.
      clear gs_a904.
    endif.


    call function 'CONVERSION_EXIT_CUNIT_OUTPUT'
      exporting
        input  = gt_out-kmein
      importing
        output = gt_out-kmein.


ENDIF.


    gt_out-kwmeng  = gt_tab-kwmeng .
    if gt_out-txt is initial.
      append gt_out.
    endif.
    clear gt_out.
  endloop.


endform.
*&---------------------------------------------------------------------*
*&      FORM  FRM_OUTPUT
*&---------------------------------------------------------------------*
*       TEXT
*----------------------------------------------------------------------*
*  -->  P1        TEXT
*  <--  P2        TEXT
*----------------------------------------------------------------------*
form frm_output .
  data: l_title     type lvc_title,
        lt_layout   type lvc_s_layo,
        l_colpos    like sy-cucol value 1,
        lt_fieldcat type slis_t_fieldcat_alv.

  lt_layout-cwidth_opt = 'X'.
  lt_layout-box_fname = 'BOX'.

  refresh : lt_fieldcat.
  """" 凭证抬头部份
  init_fieldcat 'INDEX'  '销售订单'  .
  init_fieldcat 'TEXT' '标识符' .
  init_fieldcat 'TABIX' '错误信息'.
  init_fieldcat 'AUART'  '销售凭证类型'  .
  init_fieldcat 'VKORG'  '销售组织'  .
  init_fieldcat 'VTWEG'  '分销渠道' .
  init_fieldcat 'SPART'  '产品组'  .
  init_fieldcat 'VKBUR'  '销售办事处'    .
  init_fieldcat 'KUNNR'  '售达方'  .
  init_fieldcat 'KUNAG'  '送达方'  .
  init_fieldcat 'BSTNK'  '客户参考'  .
  init_fieldcat 'POSNR'  '行项目'  .
  init_fieldcat 'MATNR'  '物料'  .
  init_fieldcat 'KWMENG'  '订单数量'  .
  init_fieldcat 'WERKS'  '工厂'  .
  init_fieldcat 'LGORT'  '库存地点'  .
  init_fieldcat 'AUGRU'  '订货原因'  .
  init_fieldcat 'REMARKS'  '抬头备注'  .
  init_fieldcat 'LIN'  '行项目备注'  .
  init_fieldcat 'PRSDT'  '行项目定价日期' .
  init_fieldcat 'KSCHL'  '条件类型' .
  init_fieldcat 'KBETR'  '条件金额' .
  init_fieldcat 'KONWA'  '条件货币' .
  init_fieldcat 'KPEIN'  '定价单位' .
  init_fieldcat 'KMEIN'  '计量单位' .


  call function 'REUSE_ALV_GRID_DISPLAY_LVC'
    exporting
      i_callback_program       = sy-repid
      i_callback_pf_status_set = 'ALV_PF_STATUS'
      it_fieldcat_lvc          = it_fieldcat
      is_layout_lvc            = lt_layout
      i_callback_user_command  = 'USER_COMMAND'
*     I_GRID_TITLE             = L_TITLE
*     I_DEFAULT                = 'X'
      i_save                   = 'A'
    tables
      t_outtab                 = gt_out
    exceptions
      program_error            = 1
      others                   = 2.
  if sy-subrc <> 0.
    message id sy-msgid type sy-msgty number sy-msgno
            with sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
  endif.
endform.                    " FRM_OUTPUT
**************设置状态栏******************************
form alv_pf_status using rt_extab type slis_t_extab.
  data: begin of ex_tab occurs 0,
          fcode like sy-ucomm,
        end of ex_tab.

  set pf-status 'ZSD_VA01' excluding ex_tab.
endform.
"ADD BY KDL_D02 20190624
form user_command using r_ucomm like sy-ucomm
                     rs_selfield type slis_selfield.
  data: l_rsnum(10),   "字符型预留号
  l_rspos(4),    "字符型预留行号
  l_msg type string.   "存放消息
  "声明一个ALV实例
  "取得当前的ALV传给声明的实例,其目的就是取当ALV的对象。
  call function 'GET_GLOBALS_FROM_SLVC_FULLSCR'
    importing
      e_grid = ref_grid.
  "从ALV实例出来的数据更新ALV对应的内表
  call method ref_grid->check_changed_data.
  case r_ucomm.
    when 'INPUT'.
      data:l_x.
      loop at gt_out where  box = 'X'.
        if gt_out-text = icon_red_light.
          l_x = 'X'.
        elseif gt_out-text = icon_yellow_light or gt_out-text = icon_green_light .
          l_x = 'S'.
        endif.

      endloop.
      if l_x = 'X'.
        message '存在错误数据,不能进行导入!' type 'E'.
      elseif l_x = 'S'.
        message '请勿重复导入数据!!!' type 'E'.
      else.
        data:answer  type char2.
        call function 'POPUP_TO_CONFIRM_STEP'
                exporting
                  defaultoption  = 'Y'
                  textline1      = '警告:'
                  textline2      = '是否需要进行导入!(PS:导入前请认真核查数据是否有误,如未核查,可以点击否或者取消来取消导入操作)'
                  titel          = '销售订单导入'
                  start_column   = 25
                  start_row      = 6
                  cancel_display = 'X'
                importing
                  answer         = answer.
        if  answer = 'J'.
                  perform frm_va01_bapi.
          elseif answer = 'N'  or answer = 'A' .
                message '操作已取消!!!' type  'S'.
              endif.

      endif.
      clear l_x.
      perform frm_refresh_alv.

    when '&IC1'.
      read table gt_out index rs_selfield-tabindex into data(wa_sel_temp).
      if rs_selfield-value is not initial.
          IF wa_sel_temp-text = icon_yellow_light or wa_sel_temp-text = icon_green_light.
            if rs_selfield-fieldname = 'INDEX'.
          "VA03跳转
          set parameter id 'AUN' field rs_selfield-value.
          call transaction 'VA03' and skip first screen.
        endif.
          ENDIF.

      endif.

    when others.
      exit.
  endcase.
endform.
form frm_refresh_alv .
  data: gr_alvgrid type ref to cl_gui_alv_grid.
  call function 'GET_GLOBALS_FROM_SLVC_FULLSCR'
    importing
      e_grid = gr_alvgrid.
  call method gr_alvgrid->refresh_table_display
    exporting
      i_soft_refresh = 'X'
    exceptions
      finished       = 1
      others         = 2.
endform.

你可能感兴趣的:(ABAP,SD开发,abap,sap,java,sql)