SAP BAPI_SALESORDER_CREATEFROMDAT2 创建销售订单

模板样式
在这里插入图片描述
小心得:
1.添加抬头条件时将BAPI表参数参数ORDER_CONDITIONS_IN中ITM_NUMBER设置为‘000000’即可。

REPORT ZSDC0004.
TABLES:VBPA,VBAK,MAKT,SSCRFIELDS.
*模板上传结构 ZSDC0002_DRJG
TYPES:
BEGIN OF TY_S_ALV_PUBLIC,

  • selected TYPE c,
    SEL TYPE C, "选择框
    ICON TYPE C LENGTH 4, "图案
    MESSAGE TYPE STRING, "报错
  • status TYPE status_alv,
  • locked TYPE c,
  • celltab TYPE lvc_t_styl,
  • colinfo TYPE lvc_t_scol,
  • err TYPE c,
  • msg TYPE string,
  • done TYPE c,
    END OF TY_S_ALV_PUBLIC.
    TYPES:BEGIN OF TY_DR,
    BS TYPE STRING, "标识相同的创一个销售订单
    AUART TYPE STRING,
    VKORG TYPE STRING,
    VTWEG TYPE STRING,
    SPART TYPE STRING,
    VKBUR TYPE STRING,
    VKGRP TYPE STRING,
    KUNNR TYPE STRING,
    BSTKD TYPE STRING,
    ERDAT TYPE STRING,
    MATNR TYPE STRING,
    KWMENG TYPE STRING,
    REQ_DATE TYPE STRING,
    BATCH TYPE STRING,
    LGORT TYPE STRING,
    KSCHL TYPE STRING,
    NETPR TYPE STRING,
    END OF TY_DR.
    TYPES: BEGIN OF TY_ALV.
    INCLUDE TYPE TY_S_ALV_PUBLIC.
    INCLUDE TYPE ZSDC0002_DRJG.
    TYPES END OF TY_ALV.

DATA: GT_DR TYPE TABLE OF TY_DR,
GS_DR TYPE TY_DR,
GT_ALV TYPE TABLE OF TY_ALV,
GS_ALV TYPE TY_ALV,
GT_ALVB TYPE TABLE OF ZSDC0002_JOB.
DATA: GT_EXCEL_TAB TYPE STANDARD TABLE OF ALSMEX_TABLINE "ALSMEX_TABLINE是系统预先已定义的一个结构,表示具有 Excel 数据的表行
.
DATA: WA_FIELDCATALOG TYPE LVC_S_FCAT, "显示数据列内表工作区域
IT_FIELDCATALOG TYPE LVC_T_FCAT, "显示数据列内表
GS_LAYOUT TYPE LVC_S_LAYO.
*======================================================================*

  • Constants *
    *======================================================================*
    CONSTANTS:
    C_SEPERATOR TYPE CHAR1 VALUE ‘/’.
    CONSTANTS:
    C_OK LIKE SY-SUBRC VALUE 0,
    C_NOT_OK LIKE SY-SUBRC VALUE 1,
    GC_MSG_AEX TYPE STRING VALUE ‘AEX’,
    C_MSG_ISW TYPE STRING VALUE ‘ISW’,
    GC_MSG_S TYPE CHAR1 VALUE ‘S’,
    GC_MSG_E TYPE CHAR1 VALUE ‘E’,
    GC_MSG_W TYPE CHAR1 VALUE ‘W’,
    GC_SEL_MODE TYPE CHAR1 VALUE ‘D’,
    GC_CELLTAB TYPE CHAR20 VALUE ‘CELLTAB’,
    GC_SAVE TYPE CHAR1 VALUE ‘A’,
    GC_WIDTH_OPT TYPE CHAR1 VALUE ‘A’,
    GC_CHAR_0 TYPE CHAR1 VALUE ‘0’,

GC_CHAR_M TYPE CHAR1 VALUE ‘M’,
GC_CHAR_K TYPE CHAR1 VALUE ‘K’,
GC_FDNAM_ERR TYPE CHAR30 VALUE ‘ERR’,
GC_FDNAM_STATUS TYPE CHAR30 VALUE ‘STATUS’,
GC_FDNAM_MATNR TYPE CHAR30 VALUE ‘MATNR’,
GC_FDNAM_IDNRK TYPE CHAR30 VALUE ‘IDNRK’,
GC_FCODE_UPD TYPE GUI_CODE VALUE ‘FC_UPD’,
GC_ALVSTATUS TYPE SLIS_FORMNAME VALUE ‘FRM_ALV_STATUS’,
GC_USERCOMMAND TYPE SLIS_FORMNAME VALUE ‘FRM_USER_COMMAND’.

DEFINE SET_RB01."创建宏(数据导入)

CLEAR wa_fieldcatalog.
wa_fieldcatalog-fieldname = &1.
wa_fieldcatalog-scrtext_m = &2.

IF wa_fieldcatalog-fieldname = ‘ICON’.
wa_fieldcatalog-icon = ‘X’.
ENDIF.
IF &1 EQ ‘SEL’.
wa_fieldcatalog-checkbox = ‘X’.
wa_fieldcatalog-edit = ‘X’.
ENDIF.

APPEND wa_fieldcatalog TO IT_FIELDCATALOG.
CLEAR wa_fieldcatalog.

END-OF-DEFINITION.
DEFINE SET_RB02."创建宏(数据查询)

CLEAR wa_fieldcatalog.
wa_fieldcatalog-fieldname = &1.
wa_fieldcatalog-scrtext_m = &2.

APPEND wa_fieldcatalog TO IT_FIELDCATALOG.
CLEAR wa_fieldcatalog.

END-OF-DEFINITION.
*======================================================================*

  • Begin of S E L E C T I O N - S C R E E N *
    *======================================================================*

SELECTION-SCREEN BEGIN OF BLOCK BLK_SCR WITH FRAME TITLE TEXT-T01.
SELECTION-SCREEN BEGIN OF LINE.
SELECTION-SCREEN POSITION 1.
SELECTION-SCREEN COMMENT 1(31) TEXT-T02 FOR FIELD P_FILE MODIF ID M01.
PARAMETERS:

  P_FILE TYPE RLGRAP-FILENAME OBLIGATORY  MODIF ID M01 DEFAULT '/D:'.

*SELECTION-SCREEN COMMENT (3) TEXT-t04 MODIF ID m01.
*SELECTION-SCREEN PUSHBUTTON (10) TEXT-t03 USER-COMMAND fc01 MODIF ID m01.
SELECTION-SCREEN END OF LINE.
*PARAMETERS:

  • P_BEROW TYPE INT4 OBLIGATORY DEFAULT 2 MODIF ID M01,
  • P_ENDROW TYPE INT4 OBLIGATORY DEFAULT 99999 MODIF ID M01.
  • SELECT-OPTIONS:
    ** s_aennr FOR gs_selscr-aennr MODIF ID m02 ,
  • S_KUNNR FOR VBPA-KUNNR MODIF ID M02,"售达方
  • S_ERDAT FOR VBAK-ERDAT MODIF ID M02 , "销售订单日期
  • S_MATNR FOR MAKT-MATNR MODIF ID M02 . "物料
    **SELECTION-SCREEN COMMENT /3(79) TEXT-s00 MODIF ID m02 .
    SELECTION-SCREEN END OF BLOCK BLK_SCR.
    *SELECTION-SCREEN BEGIN OF BLOCK BLK_01 WITH FRAME TITLE TEXT-B01.
  • PARAMETERS:
  • P_RB03 RADIOBUTTON GROUP RG2 DEFAULT ‘X’ USER-COMMAND RG2 MODIF ID M02,
  • P_RB04 RADIOBUTTON GROUP RG2 MODIF ID M02.
    *SELECTION-SCREEN END OF BLOCK BLK_01.

*SELECTION-SCREEN BEGIN OF BLOCK BLK_02 WITH FRAME TITLE TEXT-B02.

  • PARAMETERS:
  • P_RB01 RADIOBUTTON GROUP RG1 DEFAULT ‘X’ USER-COMMAND RG1,
  • P_RB02 RADIOBUTTON GROUP RG1.
    *SELECTION-SCREEN END OF BLOCK BLK_02.

DATA G_FUNCTION TYPE SMP_DYNTXT. "功能按钮
CONSTANTS:
GC_TEMPLATE TYPE CHAR4 VALUE ‘模板下载’.
SELECTION-SCREEN FUNCTION KEY 1.

INITIALIZATION.

  • 工具栏添加下载模板按钮
    CLEAR G_FUNCTION.
    G_FUNCTION-ICON_ID = ICON_POSITION.
    G_FUNCTION-ICON_TEXT = GC_TEMPLATE.
    G_FUNCTION-QUICKINFO = GC_TEMPLATE.
    SSCRFIELDS-FUNCTXT_01 = G_FUNCTION.
    *AT SELECTION-SCREEN.
  • 下载数据导入模板
  • CASE sscrfields-ucomm.
  • WHEN ‘FC01’.
  •  MESSAGE '流批' TYPE 'I'.
    
  •  sscrfields-ucomm = ''.
    
  • ENDCASE.

AT SELECTION-SCREEN OUTPUT.

*分组显示屏幕

  • PERFORM FRM_EVENT_AT_SEL_SCREEN_OUT.

AT SELECTION-SCREEN ON VALUE-REQUEST FOR P_FILE.
"获取文件路径
PERFORM SUB_FILE_OPEN_DIALOG CHANGING P_FILE."打开文件浏览框

AT SELECTION-SCREEN.

CASE SSCRFIELDS-UCOMM.
WHEN ‘FC01’.
SSCRFIELDS-UCOMM = ‘’.
PERFORM TEMP_EXCEL_GET USING ‘ZSDC0002’.
ENDCASE.

START-OF-SELECTION.

*取数
PERFORM FRM_GET_DATA.

*ALV显示
PERFORM FRM_OUTPUT_DATA.

END-OF-SELECTION.

*FORM FRM_EVENT_AT_SEL_SCREEN_OUT .

  • LOOP AT SCREEN.
  • CASE ABAP_TRUE.
  •  WHEN P_RB01.
    
  •    IF SCREEN-GROUP1 EQ 'M02'.
    
  •      SCREEN-ACTIVE = 0.
    
  •    ENDIF.
    

** MODIFY SCREEN.

  •  WHEN P_RB02.
    
  •    IF SCREEN-GROUP1 EQ 'M01'.
    
  •      SCREEN-ACTIVE = 0.
    
  •    ENDIF.
    

** MODIFY SCREEN.

  •  WHEN OTHERS.
    
  • ENDCASE.
  • IF SCREEN-NAME = ‘S_ZZCMCC-LOW’ OR SCREEN-NAME = ‘S_MATNR-LOW’.
  •  SCREEN-REQUIRED = '2'.
    
  •  MODIFY SCREEN.
    
  • ENDIF.
    ** CASE SCREEN-NAME .
    ** WHEN ‘P_RB01’ .
    ** SCREEN-OUTPUT = 0 .
    ** WHEN ‘P_RB02’ .
    ** SCREEN-OUTPUT = 0 .
    ** ENDCASE.
  • MODIFY SCREEN.
  • ENDLOOP.
    *ENDFORM.
    &---------------------------------------------------------------------
    *& Form sub_file_open_dialog
    &---------------------------------------------------------------------
    *& text
    &---------------------------------------------------------------------
    *& <-- PR_UPATH
    &---------------------------------------------------------------------
    FORM SUB_FILE_OPEN_DIALOG CHANGING PR_UPATH.

DATA: LT_FILE TYPE STANDARD TABLE OF FILE_TABLE,
LW_FILE TYPE FILE_TABLE,
LV_FILTER TYPE STRING,
LV_RC TYPE I.

LV_FILTER = ‘Excel files(.xlsx)|.xlsx|Excel files(.xls)|.xls’.

CALL METHOD CL_GUI_FRONTEND_SERVICES=>FILE_OPEN_DIALOG
EXPORTING

  • window_title            =
    
  • DEFAULT_EXTENSION       =
    
  • DEFAULT_FILENAME        =
    FILE_FILTER             = LV_FILTER
    
  • initial_directory       =
    
  • MULTISELECTION          =
    
    CHANGING
    FILE_TABLE = LT_FILE
    RC = LV_RC
  • USER_ACTION             =
    
    EXCEPTIONS
    FILE_OPEN_DIALOG_FAILED = 1
    CNTL_ERROR = 2
    ERROR_NO_GUI = 3
    OTHERS = 4.
    IF SY-SUBRC <> 0.
  • If open file with error
    MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
    WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
    ELSE.
  • If open file successfully
    READ TABLE LT_FILE INTO LW_FILE INDEX 1.
    IF SY-SUBRC = 0 .
  • If user chooses a file
    PR_UPATH = LW_FILE-FILENAME.
    
    ENDIF.
    ENDIF.

ENDFORM.
&---------------------------------------------------------------------
*& Form FRM_GET_DATA
&---------------------------------------------------------------------
*& text
&---------------------------------------------------------------------
*& --> p1 text
*& <-- p2 text
&---------------------------------------------------------------------
FORM FRM_GET_DATA.

  • CASE ABAP_TRUE.

  • WHEN P_RB01."导入创建
    PERFORM SUB_UPLOAD_FILE. "数据导入
    PERFORM SUB_PROCESS_DATA ."数据整理
    PERFORM FRM_CHECK_DATA ."数据检测

  • WHEN P_RB02."查询

  •  PERFORM FRM_SELECT .  "取数
    
  • ENDCASE.

ENDFORM.
FORM SUB_UPLOAD_FILE ."上传数据
DATA LV_PATH TYPE RLGRAP-FILENAME.
LV_PATH = P_FILE.

REFRESH GT_EXCEL_TAB.

CALL FUNCTION ‘ALSM_EXCEL_TO_INTERNAL_TABLE’
EXPORTING
FILENAME = LV_PATH
I_BEGIN_COL = 1
I_BEGIN_ROW = 1
I_END_COL = 99
I_END_ROW = 59999
TABLES
INTERN = GT_EXCEL_TAB
EXCEPTIONS
INCONSISTENT_PARAMETERS = 1
UPLOAD_OLE = 2
OTHERS = 3.
IF SY-SUBRC <> 0.

  • 读取EXCEL文件出错
    MESSAGE ‘读取EXCEL文件出错’ TYPE ‘S’ DISPLAY LIKE ‘E’.
    STOP.

  • MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno

  •        WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
    

    ENDIF.
    ENDFORM.
    FORM SUB_PROCESS_DATA ."数据转换

    DATA:LW_EXCEL_TAB TYPE ALSMEX_TABLINE,
    LV_FIELDNAME TYPE CHAR10.

  •   LW_RECORD    TYPE TY_RECORD.
    

    FIELD-SYMBOLS: TYPE ANY.

  • 删除第一个0001行数据(表头)/ 0002行数据/0003行数据
    LOOP AT GT_EXCEL_TAB INTO LW_EXCEL_TAB.
    IF LW_EXCEL_TAB-ROW = ‘0001’ ."OR LW_EXCEL_TAB-ROW = ‘0002’ OR LW_EXCEL_TAB-ROW = ‘0003’ OR LW_EXCEL_TAB-ROW = ‘0004’.
    DELETE TABLE GT_EXCEL_TAB FROM LW_EXCEL_TAB.
    ELSE.
    EXIT.
    ENDIF.
    ENDLOOP.

    IF GT_EXCEL_TAB[] IS INITIAL.
    MESSAGE ‘上载文件为空,请检查!’ TYPE ‘S’ DISPLAY LIKE ‘E’.
    ENDIF.

*----------------------------------------------------------------------

LOOP AT GT_EXCEL_TAB INTO LW_EXCEL_TAB.

ASSIGN COMPONENT LW_EXCEL_TAB-COL OF STRUCTURE GS_DR TO .
IF  IS ASSIGNED.
   = LW_EXCEL_TAB-VALUE.
  UNASSIGN .
ENDIF.
AT END OF ROW.
  APPEND GS_DR TO GT_DR.
  CLEAR GS_DR.
ENDAT.

ENDLOOP.
MOVE-CORRESPONDING GT_DR TO GT_ALV .
WITH +DATA AS ( SELECT VKORG FROM @GT_ALV AS GT_ALV )
SELECT B~VKORG ,
B~WAERS
FROM TVKO AS B
INNER JOIN +DATA AS A ON A~VKORG = B~VKORG
INTO TABLE @DATA(GT_TVTO).
LOOP AT GT_ALV ASSIGNING FIELD-SYMBOL() .
READ TABLE GT_TVTO ASSIGNING FIELD-SYMBOL() WITH KEY VKORG = -VKORG.
IF SY-SUBRC = 0 .
-WAERS = -WAERS .
ELSE.
-MESSAGE = ‘销售组织没有维护对应的统计货币’ .
-ICON = ICON_LED_RED .
ENDIF.
ENDLOOP.
SORT GT_ALV BY BS .
*----------------------------------------------------------------------

ENDFORM.
FORM FRM_CHECK_DATA ."数据检查

ENDFORM.
*FORM FRM_SELECT ."取数
*

  • SELECT * FROM ZSDC0002_JOB
  • WHERE KUNNR IN @S_KUNNR
  •  AND ERDAT IN @S_ERDAT
    
  •  AND MATNR IN @S_MATNR
    
  • INTO TABLE @GT_ALVB.

*ENDFORM.
FORM FRM_OUTPUT_DATA .
PERFORM SUB_SET_LAYOUT.
PERFORM SUB_SET_FIELDCAT.

*函数ALV的调用

  • CASE ABAP_TRUE .
  • WHEN P_RB01.
    CALL FUNCTION ‘REUSE_ALV_GRID_DISPLAY_LVC’
    EXPORTING
    I_CALLBACK_PROGRAM = SY-REPID
  • it_sort_lvc              = it_sort
    I_CALLBACK_PF_STATUS_SET = 'SUB_SET_STATUS'
    
  • I_CALLBACK_HTML_TOP_OF_PAGE = 'SUB_ALV_TOP_OF_PAGE'
    I_CALLBACK_USER_COMMAND  = 'SUB_USER_COMMAND'
    IS_LAYOUT_LVC            = GS_LAYOUT
    IT_FIELDCAT_LVC          = IT_FIELDCATALOG
    
  • i_save                   = 'A'
    
  • i_default                = 'X'
    
  • i_html_height_top        = 18               "可根据表头宽度适当调整大小
    
    " IT_EVENTS = IT_EVENTS
    TABLES
    T_OUTTAB = GT_ALV.
  • WHEN P_RB02.
  •  CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY_LVC'
    
  •    EXPORTING
    
  •      I_CALLBACK_PROGRAM = SY-REPID
    

** it_sort_lvc = it_sort
** I_CALLBACK_PF_STATUS_SET = ‘SUB_SET_STATUS’
** I_CALLBACK_HTML_TOP_OF_PAGE = ‘SUB_ALV_TOP_OF_PAGE’
** I_CALLBACK_USER_COMMAND = ‘SUB_USER_COMMAND’

  •      IS_LAYOUT_LVC      = GS_LAYOUT
    
  •      IT_FIELDCAT_LVC    = IT_FIELDCATALOG
    

** i_save = ‘A’
** i_default = ‘X’
** i_html_height_top = 18 "可根据表头宽度适当调整大小

  •   "  IT_EVENTS          = IT_EVENTS
    
  •    TABLES
    
  •      T_OUTTAB           = GT_ALVB.
    
  • ENDCASE.
    ENDFORM.
    FORM SUB_SET_LAYOUT .
    GS_LAYOUT-ZEBRA = ‘X’.
    GS_LAYOUT-CWIDTH_OPT = ‘X’.
    ENDFORM.
    FORM SUB_SET_FIELDCAT .

  • CASE ABAP_TRUE .

  • WHEN P_RB01.
    SET_RB01: ‘SEL’ ‘选择框’,
    ‘ICON’ ‘消息’,
    ‘MESSAGE’ ‘消息文本’,

  •                 'BS'           ''
               'AUART'        'SAP订单类型',
               'VKORG'        '销售机构',
               'VTWEG'        '分销渠道',
               'SPART'        '产品组',
               'VKBUR'        '销售部门',
               'VKGRP'        'SAP销售组',
               'KUNNR'        'SAP客户号码',
               'BSTKD'        '平台销售单号',
               'ERDAT'        '创建日期',
               'MATNR'        'SAP物料编号',
               'KWMENG'       '销售数量',
               'REQ_DATE'     '交货日期',
               'BATCH'        '批次',
               'LGORT'        '存储地点',
               'KSCHL'        '价格条件',
               'NETPR'        '净价'.
    
  • WHEN P_RB02.

  •  SET_RB02:
    
  •                 'KUNNR'   'SAP客户号码',
    
  •                 'MATNR'   'SAP物料编号',
    
  •                 'KWMENG'  '销售数量',
    
  •                 'VRKME'   '销售单位',
    
  •                 'NETPR'   '销售价格',
    
  •                 'KMEIN'   '价格销售单位',
    
  •                 'NETWR'   '此物料销售金额',
    
  •                 'BSTKD'   '旺店通订单号码',
    
  •                 'ERDAT'   '销售订单日期时间',
    
  •                 'VBELNA'  '销售订单号码',
    
  •                 'POSNRA'  '销售订单行项目号',
    
  •                 'VBELNB'  '交货单号',
    
  •                 'POSNRB'  '交货单行项目',
    
  •                 'VBELNC'  '系统发票',
    
  •                 'POSNRC'  '系统发票号码',
    
  •                 'ZISON'   '标识是否完成系统发票',
    
  •                 'ZMESS'   '记录系统执行错误信息',
    
  •                 'ZDATE'   '数据导入日期',
    
  •                 'YDATE'   '数据导入时间',
    
  •                 'ZTIME'   '数据导入日期',
    
  •                 'YTIME'   '数据导入时间 '.
    
  •  .
    
  • ENDCASE.
    ENDFORM.
    FORM SUB_SET_STATUS USING PT_EXTAB TYPE SLIS_T_EXTAB.

*局部对象定义
DATA:LT_ECODE TYPE TABLE OF SY-UCOMM.

*gui status的定义
SET PF-STATUS ‘1000’ EXCLUDING LT_ECODE.

ENDFORM.
FORM SUB_USER_COMMAND USING PT_UCOMM LIKE SY-UCOMM
RS_SELFIELD TYPE SLIS_SELFIELD.
DATA:LO_GRID TYPE REF TO CL_GUI_ALV_GRID.
DATA:LV_FLAG TYPE C .
DATA:LV_TABIX TYPE SYTABIX .
CALL FUNCTION ‘GET_GLOBALS_FROM_SLVC_FULLSCR’
IMPORTING
E_GRID = LO_GRID.
IF LO_GRID IS NOT INITIAL .
LO_GRID->CHECK_CHANGED_DATA( ).
ENDIF .

*功能代码实现
CASE PT_UCOMM.
WHEN ‘QX’.

  LOOP AT GT_ALV INTO GS_ALV.
    GS_ALV-SEL = 'X'.
    MODIFY GT_ALV FROM GS_ALV .
  ENDLOOP.
  •  PERFORM SUB_DO_MARK USING  'X'.
    

    WHEN ‘BX’.
    LOOP AT GT_ALV INTO GS_ALV.
    GS_ALV-SEL = ‘’.
    MODIFY GT_ALV FROM GS_ALV .
    ENDLOOP.
    WHEN ‘&IC1’.
    WHEN ‘CREIT’ .

  •  MESSAGE '嗯哼' TYPE 'S'.
    PERFORM FRM_CREIT.
    

    ENDCASE.

    RS_SELFIELD-REFRESH = ‘X’.
    RS_SELFIELD-COL_STABLE = ‘X’ .
    RS_SELFIELD-ROW_STABLE = ‘X’.
    ENDFORM.
    FORM SUB_DO_MARK USING PU_MARK.
    CLEAR:GS_ALV .

    GS_ALV-SEL = PU_MARK.
    MODIFY GT_ALV FROM GS_ALV TRANSPORTING SEL
    WHERE SEL NE PU_MARK.

ENDFORM.
FORM FRM_CREIT.
CLEAR:GS_ALV .
PERFORM FRM_XS.

  • PERFORM FRM_JH.
  • PERFORM FRM_FP.

ENDFORM.
&---------------------------------------------------------------------
*& Form frm_xs
&---------------------------------------------------------------------
*& text
&---------------------------------------------------------------------
*& --> p1 text
*& <-- p2 text
&---------------------------------------------------------------------
FORM FRM_XS .
DATA: LW_HEADER TYPE BAPISDHD1,
LW_HEADERX TYPE BAPISDHD1X,
LT_RETURN TYPE TABLE OF BAPIRET2 WITH HEADER LINE,
LT_ITEM TYPE TABLE OF BAPISDITM,
LW_ITEM TYPE BAPISDITM,
LT_ITEMX TYPE TABLE OF BAPISDITMX,
LW_ITEMX TYPE BAPISDITMX,
LT_PARNR TYPE TABLE OF BAPIPARNR,
LW_PARNR TYPE BAPIPARNR,
LT_SCHDL TYPE TABLE OF BAPISCHDL, "计划行
LW_SCHDL TYPE BAPISCHDL,
LT_SCHDLX TYPE TABLE OF BAPISCHDLX,
LW_SCHDLX TYPE BAPISCHDLX,
LT_COND TYPE TABLE OF BAPICOND, "订单条件
LS_COND TYPE BAPICOND,
LT_CONDX TYPE TABLE OF BAPICONDX,
LS_CONDX TYPE BAPICONDX.
DATA: L_POSNR TYPE VBAP-POSNR. "行项目号
DATA: LV_VBELN TYPE BAPIVBELN-VBELN. "行项目号
DATA:LV_MESSAGE TYPE STRING . "消息汇总
DATA :LV_SUMVERPR TYPE VERPR .
DATA LV_MATNR TYPE MATNR .

READ TABLE GT_ALV TRANSPORTING NO FIELDS WITH KEY SEL = ‘X’ .
IF SY-SUBRC NE 0.
MESSAGE ‘请选择要创建销售订单的数据’ TYPE ‘E’.
ENDIF.
DATA(GT_ALVA) = GT_ALV .
DATA GT_ITEM TYPE TABLE OF TY_ALV .
CLEAR:GS_ALV.
LOOP AT GT_ALVA ASSIGNING FIELD-SYMBOL()
GROUP BY ( BS = -BS ) "物料
ASCENDING REFERENCE INTO DATA(LS_SAP_GRP). "分组放入工作区
CLEAR :GT_ITEM[],LV_SUMVERPR.
GT_ITEM[] = VALUE #( BASE GT_ITEM
FOR WA IN GROUP LS_SAP_GRP ( WA ) ).
CLEAR: L_POSNR,LW_HEADER,LW_HEADERX,LW_PARNR,LT_PARNR,LW_ITEM ,
LT_ITEM,LS_COND , LT_COND,LW_SCHDLX ,LT_SCHDLX,LW_ITEMX ,
LT_ITEMX,LS_CONDX , LT_CONDX,LT_RETURN,LV_VBELN,LT_SCHDL.
LOOP AT GT_ITEM INTO GS_ALV WHERE SEL = ‘X’ .
DATA(LV_TABIX) = SY-TABIX .
*表头数据
LW_HEADER-DOC_TYPE = GS_ALV-AUART. "销售订单类型
LW_HEADER-SALES_ORG = GS_ALV-VKORG. "销售机构
LW_HEADER-DISTR_CHAN = GS_ALV-VTWEG. "分销渠道
LW_HEADER-DIVISION = GS_ALV-SPART. "产品组
LW_HEADER-SALES_OFF = GS_ALV-VKBUR. "销售部门
LW_HEADER-SALES_GRP = GS_ALV-VKGRP. "SAP销售组
LW_HEADER-PURCH_NO_C = GS_ALV-BSTKD. "SAP客户参考
IF GS_ALV-ERDAT IS NOT INITIAL.
LW_HEADER-REQ_DATE_H = GS_ALV-ERDAT . "销售日期
ELSE.
LW_HEADER-REQ_DATE_H = SY-DATUM. "销售日期
ENDIF.

*确认
LW_HEADERX-DOC_TYPE = ‘X’.
LW_HEADERX-SALES_ORG = ‘X’.
LW_HEADERX-DISTR_CHAN = ‘X’.
LW_HEADERX-DIVISION = ‘X’.
*合作伙伴数据
CLEAR LW_PARNR.
LW_PARNR-PARTN_ROLE = ‘AG’. "售达方
CALL FUNCTION ‘CONVERSION_EXIT_ALPHA_INPUT’
EXPORTING
INPUT = GS_ALV-KUNNR
IMPORTING
OUTPUT = LW_PARNR-PARTN_NUMB.
APPEND LW_PARNR TO LT_PARNR.
CLEAR LW_PARNR.

  • LW_PARNR-PARTN_ROLE = ‘WE’."合作伙伴功能 送达方
  • CALL FUNCTION ‘CONVERSION_EXIT_ALPHA_INPUT’
  • EXPORTING
  •  INPUT  =
    
  • IMPORTING
  •  OUTPUT =
    
  •  APPEND LW_PARNR TO LT_PARNR.
    
  • ‘RE’."合作伙伴功能 受票方
  • ‘RG’."合作伙伴功能 受票方
  • CLEAR L_POSNR.
    L_POSNR = L_POSNR + 10.

*行项目数据
CLEAR LW_ITEM.
LW_ITEM-ITM_NUMBER = L_POSNR. "项目编号
"物料
CALL FUNCTION ‘CONVERSION_EXIT_MATN1_INPUT’
EXPORTING
INPUT = GS_ALV-MATNR
IMPORTING
OUTPUT = LW_ITEM-MATERIAL
EXCEPTIONS
LENGTH_ERROR = 1
OTHERS = 2.
"数量
LW_ITEM-TARGET_QTY = GS_ALV-KWMENG."物料数量 .
LW_ITEM-PLANT = GS_ALV-VKORG."销售机构与工厂是相同的,使用一下
LW_ITEM-STORE_LOC = GS_ALV-LGORT.
LW_ITEM-BATCH = GS_ALV-BATCH."批次
APPEND LW_ITEM TO LT_ITEM.
*确认
CLEAR: LW_ITEMX,LT_ITEMX…
LW_ITEMX-MATERIAL = ‘X’.
LW_ITEMX-TARGET_QU = ‘X’.
LW_ITEMX-PLANT = ‘X’.
LW_ITEMX-STORE_LOC = ‘X’.
LW_ITEMX-BATCH = ‘X’."批次
APPEND LW_ITEMX TO LT_ITEMX.
*数量
CLEAR: LW_SCHDL.
LW_SCHDL-REQ_QTY = GS_ALV-KWMENG.
LW_SCHDL-ITM_NUMBER = L_POSNR.
LW_SCHDL-REQ_DATE = GS_ALV-REQ_DATE ."交货日期
APPEND LW_SCHDL TO LT_SCHDL.
CLEAR: LW_SCHDLX.
LW_SCHDLX-REQ_QTY = ‘X’.
LW_SCHDLX-ITM_NUMBER = L_POSNR.
LW_SCHDLX-REQ_DATE = ‘X’.
APPEND LW_SCHDLX TO LT_SCHDLX.

  CLEAR:LS_COND,LS_CONDX.
  LS_COND-ITM_NUMBER = L_POSNR .
  LS_COND-COND_ST_NO = '010'.
  LS_COND-COND_COUNT = '01'.
  LS_COND-COND_TYPE = GS_ALV-KSCHL. "价格条件
  LS_COND-COND_VALUE = GS_ALV-NETPR. "销售单价
  IF LS_COND-COND_VALUE IS INITIAL .
    CLEAR:LV_MATNR.
    CALL FUNCTION 'CONVERSION_EXIT_MATN1_INPUT'
      EXPORTING
        INPUT        = GS_ALV-MATNR
      IMPORTING
        OUTPUT       = LV_MATNR
      EXCEPTIONS
        LENGTH_ERROR = 1
        OTHERS       = 2.
    BREAK-POINT.
    SELECT SINGLE VERPR FROM MBEW  WHERE MATNR = @LV_MATNR AND VPRSV = 'V' INTO @DATA(LV_VERPR).
    LV_SUMVERPR = LV_VERPR + LV_SUMVERPR .
    DATA(LV_WAERS) = GS_ALV-WAERS .
    LS_COND-COND_VALUE = LV_VERPR. "销售单价
  ENDIF.
  LS_COND-CURRENCY = GS_ALV-WAERS. "统计货币
  • LT_COND-CURRENCY =‘CNY’
    LS_CONDX-COND_ST_NO = ‘010’.
    LS_CONDX-COND_COUNT = ‘01’.
    LS_CONDX-COND_TYPE = ‘X’.
    LS_CONDX-COND_VALUE = ‘X’.
    LS_CONDX-CURRENCY = ‘X’.
  • LT_CONDX-CURRENCY =‘X’
    APPEND LS_COND TO LT_COND.
    APPEND LS_CONDX TO LT_CONDX.
    ENDLOOP.
    IF LV_SUMVERPR IS NOT INITIAL .
    LS_COND-ITM_NUMBER = ‘000000’ .
    LS_COND-COND_ST_NO = ‘000’.
    LS_COND-COND_COUNT = ‘00’.
    LS_COND-COND_TYPE = ‘ZR03’. "价格条件
    LS_COND-COND_VALUE = LV_SUMVERPR. "销售单价
    LS_COND-CURRENCY = LV_WAERS. "统计货币
  • LT_COND-CURRENCY =‘CNY’
    LS_COND-ITM_NUMBER = ‘000000’ .
    LS_COND-COND_ST_NO = ‘000’.
    LS_COND-COND_COUNT = ‘00’.
    LS_CONDX-COND_TYPE = ‘X’.
    LS_CONDX-COND_VALUE = ‘X’.
    LS_CONDX-CURRENCY = ‘X’.
  • LT_CONDX-CURRENCY =‘X’
    APPEND LS_COND TO LT_COND.
    APPEND LS_CONDX TO LT_CONDX.
    ENDIF.
CALL FUNCTION 'BAPI_SALESORDER_CREATEFROMDAT2'
  EXPORTING
    ORDER_HEADER_IN      = LW_HEADER
    ORDER_HEADER_INX     = LW_HEADERX
  IMPORTING
    SALESDOCUMENT        = LV_VBELN
  TABLES
    RETURN               = LT_RETURN
    ORDER_ITEMS_IN       = LT_ITEM
    ORDER_ITEMS_INX      = LT_ITEMX
    ORDER_PARTNERS       = LT_PARNR
    ORDER_SCHEDULES_IN   = LT_SCHDL
    ORDER_SCHEDULES_INX  = LT_SCHDLX
    ORDER_CONDITIONS_IN  = LT_COND
    ORDER_CONDITIONS_INX = LT_CONDX.
"判断是否返回有错误消息
CLEAR:LV_MESSAGE.
LOOP AT LT_RETURN .
  IF LT_RETURN-TYPE CA 'EAW'.
    LV_MESSAGE = LV_MESSAGE && LT_RETURN-MESSAGE .
    DATA(LV_ICON) = ICON_LED_RED .
  ENDIF.
ENDLOOP.
IF LV_ICON IS INITIAL .
  LV_MESSAGE = '创建成功:' &&  LV_VBELN .
  LV_ICON = ICON_LED_GREEN .
ENDIF.
GS_ALV-MESSAGE = LV_MESSAGE .
GS_ALV-ICON    = LV_ICON .
MODIFY GT_ALV FROM GS_ALV TRANSPORTING MESSAGE ICON
WHERE BS = GS_ALV-BS AND SEL IS NOT INITIAL .
  • BREAK-POINT.
    CLEAR:GS_ALV,LV_ICON ,LV_MESSAGE.
    CALL FUNCTION ‘BAPI_TRANSACTION_COMMIT’
    EXPORTING
    WAIT = ‘X’.
    ENDLOOP.

ENDFORM.
&---------------------------------------------------------------------
*& Form temp_excel_get
&---------------------------------------------------------------------
*& text
&---------------------------------------------------------------------
*& --> P_
&---------------------------------------------------------------------
FORM TEMP_EXCEL_GET USING P_OBJID TYPE WWWDATA-OBJID.
DATA:L_OBJDATA LIKE WWWDATATAB,
L_RC LIKE SY-SUBRC.

DATA: L_FULLPATH TYPE STRING,
L_EXTENSION TYPE STRING,
L_FNAME LIKE RLGRAP-FILENAME.

L_EXTENSION = P_OBJID.

PERFORM GET_FILE_NAME USING ‘.xlsx’
‘Excel|*.xlsx;’
CHANGING L_FULLPATH.
IF L_FULLPATH = SPACE.
MESSAGE ‘请选择下载文件名’ TYPE ‘E’.
ELSE.
L_FNAME = L_FULLPATH.
SELECT SINGLE RELID OBJID
FROM WWWDATA
INTO CORRESPONDING FIELDS OF L_OBJDATA
WHERE RELID = ‘MI’
AND OBJID = P_OBJID .

IF SY-SUBRC NE 0 OR L_OBJDATA-OBJID = SPACE.
  MESSAGE E001(00) WITH '文件不存在!'.
ELSE.
  CALL FUNCTION 'DOWNLOAD_WEB_OBJECT'
    EXPORTING
      KEY         = L_OBJDATA
      DESTINATION = L_FNAME
    IMPORTING
      RC          = L_RC
    CHANGING
      TEMP        = L_FNAME.
  IF L_RC NE 0.
    MESSAGE E001(00) WITH '文件下载失败!'.
  ENDIF.
ENDIF.

ENDIF.

ENDFORM. "TEMP_EXCEL_GET
&---------------------------------------------------------------------
*& Form get_file_name
&---------------------------------------------------------------------
*& text
&---------------------------------------------------------------------
*& --> P_
*& --> P_
*& <-- L_FULLPATH
&---------------------------------------------------------------------
FORM GET_FILE_NAME USING P_EXTENSION
P_FILE_FILTER
CHANGING P_FULLPATH.
DATA: L_FILENAME TYPE STRING,
L_PATH TYPE STRING,
L_FULLPATH TYPE STRING,
L_TITILE TYPE STRING,
L_INIT_DIR TYPE STRING.

CLEAR P_FULLPATH.

L_TITILE = TEXT-T04.
L_INIT_DIR = TEXT-T03.
CALL METHOD CL_GUI_FRONTEND_SERVICES=>FILE_SAVE_DIALOG
EXPORTING
WINDOW_TITLE = L_TITILE
DEFAULT_EXTENSION = ‘.xls’
INITIAL_DIRECTORY = L_INIT_DIR
PROMPT_ON_OVERWRITE = ‘X’
FILE_FILTER = P_FILE_FILTER
CHANGING
FILENAME = L_FILENAME
PATH = L_PATH
FULLPATH = L_FULLPATH
EXCEPTIONS
CNTL_ERROR = 1
ERROR_NO_GUI = 2
NOT_SUPPORTED_BY_GUI = 3
OTHERS = 4.
IF SY-SUBRC <> 0.
MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
ENDIF.

P_FULLPATH = L_FULLPATH.
ENDFORM.

你可能感兴趣的:(创建VA01销售订单,添加销售订单条件类型,abap)