ABAP DEMO so批量导入

亡梦爱人 提交于 2019-12-06 11:05:38
*&---------------------------------------------------------------------*
*& Report  YDEMO_015
*&
*&---------------------------------------------------------------------*
*&
*&
*&---------------------------------------------------------------------*
REPORT  YDEMO_015.

*&---------------------------------------------------------------------*
*&                 TOP
*&---------------------------------------------------------------------*
"相关表引用声明
TABLES:VBAK, VBAP,
       EKKO, EKPO,
       EKET, MEAN,
       MARA, MAKT,
       MARD, MARC,
       EORD, LFA1,
       EINA, T023T,
       T001W,T001L,
       KNA1, TVKO,
       TVKOV,TVAK,
       TVBUR.
TABLES:SSCRFIELDS.


*----------------------------------------------------------------------
*                          Internal table
*----------------------------------------------------------------------
*&EXCEL上传数据结构
TYPES:BEGIN OF TY_UP,
        BSTNK       LIKE VBAK-BSTNK, "客户订单号码
        KUNNR       LIKE VBAK-KUNNR, "客户代码
        NAME1       LIKE KNA1-NAME1, "客户名称1
        STREET      LIKE ADRC-STREET,"送货地址(KNA1-ADRNR->ADRC)
        AUART       LIKE VBAK-AUART, "SAP销售订单类型
        VKORG       LIKE VBAK-VKORG, "销售组织
        VKBUR       LIKE VBAK-VKBUR, "销售部门
        MATNR       LIKE VBAP-MATNR, "商品编码
        MAKTX       LIKE MAKT-MAKTX, "商品名称
        KWMENG      LIKE VBAP-KWMENG,"销售数量
        VRKME       LIKE VBAP-VRKME, "销售单位

        KBETR       LIKE KONV-KBETR, "单价
        WAERK       LIKE VBAP-WAERK, "币种
        KMEIN       LIKE VBAP-KMEIN, "EKPO-PEINH, "价格单位
        VDATU       LIKE VBAK-VDATU, "交货日期
        WERKS       LIKE VBAP-WERKS, "发货地点
        VTWEG       LIKE VBAK-VTWEG, "分销渠道
        SPART       LIKE VBAK-SPART, "产品组

        VBELN       LIKE VBAK-VBELN,
        FLAG(1),
        ICON        TYPE C,          "图标
        SEL         TYPE C,
        LIN(08)     TYPE N,          "LIKE SY-TABIX,
        REASON(500),

      END OF TY_UP.

TYPES:BEGIN OF TY_HA,   "EXCEL上传数据结构
        BSTNK       LIKE VBAK-BSTNK, "客户订单号码
        KUNNR       LIKE VBAK-KUNNR, "客户
        NAME1       LIKE KNA1-NAME1, "客户名称1
        STREET      LIKE ADRC-STREET,"送货地址(KNA1-ADRNR->ADRC)
        AUART       LIKE VBAK-AUART, "SAP销售订单类型
        VKORG       LIKE VBAK-VKORG, "销售组织
        VKBUR       LIKE VBAK-VKBUR, "销售部门
        VTWEG       LIKE VBAK-VTWEG, "销售渠道
        SPART       LIKE VBAK-SPART, "产品组
        VDATU       LIKE VBAK-VDATU, "交货日期

        VBELN       LIKE VBAK-VBELN,
        FLAG(1),
        ICON        TYPE C,          "图标
        REASON(500),
        LIN         TYPE SY-TABIX,
        SEL         TYPE C,
      END OF TY_HA.

DATA:GT_UP   TYPE STANDARD TABLE OF TY_UP,
     WA_UP   TYPE TY_UP,
*&so抬头数据
     GT_HA   TYPE STANDARD TABLE OF TY_HA,
     WA_HA   TYPE TY_HA,
*&比对重复记录
     GT_UPE  TYPE STANDARD TABLE OF TY_HA,
     WA_UPE  TYPE TY_HA,
*&创建so
     GT_HEAD TYPE STANDARD TABLE OF TY_HA WITH HEADER LINE,
     WA_HEAD TYPE TY_HA,
     LT_HEAD LIKE TABLE OF GT_HEAD,
     GT_ITAB LIKE STANDARD TABLE OF ALSMEX_TABLINE WITH HEADER LINE,
     G_TABIX LIKE SY-TABIX.
* GT_TVAK LIKE TVAK OCCURS 0 WITH HEADER LINE,
FIELD-SYMBOLS : <FS> TYPE ANY.

*----------------------------------------------------------------------
*                          Variants
*----------------------------------------------------------------------
DATA: G_LIN      TYPE I,
      G_SLIN     TYPE I,
      G_FLIN     TYPE I,
      G_TEXT(80).
DATA: C_ECOL TYPE I ,
      G_FILE TYPE CHAR20,
      G_NAME TYPE CHAR40.

DATA:L_VBELN   LIKE BAPIVBELN-VBELN.
DATA:WA_HEADER  TYPE BAPISDHD1,  "
     WA_HEADERX TYPE BAPISDHD1X, "
     WA_PARTNER TYPE BAPIPARNR,  "
     WA_ITEM    TYPE BAPISDITM,
     WA_ITEMX   TYPE BAPISDITMX,
     WA_COND    TYPE BAPICOND,
     WA_CONDX   TYPE BAPICONDX,
     WA_SCH     TYPE BAPISCHDL,
     WA_BAPE    TYPE BAPE_VBAK,
     WA_EX      TYPE BAPIPAREX,
     WA_TEXT    TYPE BAPISDTEXT,
     LT_PART    TYPE STANDARD TABLE OF BAPIPARNR,
     LT_ITEM    TYPE STANDARD TABLE OF BAPISDITM,
     LT_ITEMX   TYPE STANDARD TABLE OF BAPISDITMX,
     LT_SCH     TYPE STANDARD TABLE OF BAPISCHDL,
     LT_SCHX    TYPE STANDARD TABLE OF BAPISCHDLX,
     L_RETURN   LIKE BAPIRET2 OCCURS 0 WITH HEADER LINE,
     LT_COND    TYPE STANDARD TABLE OF BAPICOND,
     LT_CONDX   TYPE STANDARD TABLE OF BAPICONDX,
     LT_TEXT    LIKE STANDARD TABLE OF BAPISDTEXT,
     LT_EX      TYPE STANDARD TABLE OF BAPIPAREX.
*---------------------------------------------------------------------*
*  Data parameters for alv report use
*---------------------------------------------------------------------*
DATA: G_PROGRAM    TYPE SY-REPID,
      GS_LAYOUT    TYPE SLIS_LAYOUT_ALV,
      GS_LAYOUT1   TYPE SLIS_LAYOUT_ALV,
      GT_FIELDCAT  TYPE SLIS_T_FIELDCAT_ALV,
      GS_FIELDCAT  LIKE LINE OF GT_FIELDCAT,
      GT_FIELDCAT1 TYPE SLIS_T_FIELDCAT_ALV,
      GS_FIELDCAT1 LIKE LINE OF GT_FIELDCAT,
      WA_FIELDCAT  TYPE SLIS_FIELDCAT_ALV,
      WA_FIELDCAT1 TYPE SLIS_FIELDCAT_ALV,
      IT_EVENTS    TYPE SLIS_T_EVENT WITH HEADER LINE,
      GT_EVENT     TYPE SLIS_T_EVENT,
      WA_EVENT     TYPE SLIS_ALV_EVENT.
DATA: GT_LIST_TOP_OF_PAGE     TYPE SLIS_T_LISTHEADER.
DATA: GC_FORMNAME_TOP_OF_PAGE TYPE SLIS_FORMNAME
                              VALUE 'ALV_TOP_OF_PAGE'.

RANGES:R_BSTNK FOR VBAK-BSTNK."客户订单号码


************************************************************************
*             Select Screen                                            *
************************************************************************
SELECTION-SCREEN BEGIN OF BLOCK BLK1 WITH FRAME TITLE TEXT-001.

PARAMETERS:P_FNAME LIKE RLGRAP-FILENAME MEMORY ID M01. "OBLIGATORY.TYPE STRING

SELECTION-SCREEN END OF BLOCK BLK1.

SELECTION-SCREEN BEGIN OF BLOCK B2 WITH FRAME TITLE TEXT-002.

PARAMETERS:P_TEST   RADIOBUTTON GROUP RAD1 DEFAULT 'X',
           P_EXCUTE RADIOBUTTON GROUP RAD1.

SELECTION-SCREEN FUNCTION KEY 1.

SELECTION-SCREEN : END OF BLOCK B2.



*&------------------------------------------------------------------*
*&              INITIALIZATION
*&------------------------------------------------------------------*
INITIALIZATION.
  PERFORM FRM_INIT_FIELD.

AT SELECTION-SCREEN ON VALUE-REQUEST FOR P_FNAME.
  CALL FUNCTION 'KD_GET_FILENAME_ON_F4'
    EXPORTING
      MASK      = '*.xls'
      STATIC    = 'X'
    CHANGING
      FILE_NAME = P_FNAME.

  IF SY-SUBRC <> 0.
* Implement suitable error handling here
  ENDIF.


AT SELECTION-SCREEN.

  PERFORM FRM_CHECK_AUTHOR.
  PERFORM FRM_DOWN_EXCEL.


START-OF-SELECTION.

  PERFORM FRM_GET_DATA.
*&test run
  IF P_TEST EQ 'X'.
    PERFORM FRM_TEST_RUN.
  ENDIF.

*&---------------------------------------------------------------------*
*&      Form  FRM_INIT_FIELD
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM FRM_INIT_FIELD .

  SSCRFIELDS-FUNCTXT_01 = '下载EXCEL模板'.
  C_ECOL = 16.
  G_FILE = 'ZDQT005'.
  G_NAME = '\电器采销售订单导入模板.XLS'.
ENDFORM.                    " FRM_INIT_FIELD
*&---------------------------------------------------------------------*
*&      Form  FRM_CHECK_AUTHOR
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM FRM_CHECK_AUTHOR .

ENDFORM.                    " FRM_CHECK_AUTHOR
*&---------------------------------------------------------------------*
*&      Form  FRM_DOWN_EXCEL
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM FRM_DOWN_EXCEL .

  DATA:LW_PATH TYPE STRING.
  DATA:L_FORMKEY LIKE WWWDATATAB,
       L_FILE    TYPE CHAR20,
       L_PATH    TYPE LOCALFILE,
       L_TYPE    TYPE FILEFORMAT,
       L_NAME    TYPE CHAR40.

  IF SSCRFIELDS-UCOMM = 'FC01'.
*&S1.选择文件路径
*  CALL FUNCTION 'KD_GET_FILENAME_ON_F4'
*    EXPORTING
*      MASK      = '*.xls'
*      STATIC    = 'X'
*    CHANGING
*      FILE_NAME = P_FNAME.
*  IF SY-SUBRC <> 0.
*    MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
*               WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
*  ENDIF.
*    L_TYPE = 'XLS'.
*&S2.下载文件
    CONCATENATE 'MI' G_FILE INTO L_FORMKEY.
* CONCATENATE P_FNAME G_NAME INTO L_PATH.
    CALL FUNCTION 'DOWNLOAD_WEB_OBJECT'
      EXPORTING
        KEY         = L_FORMKEY
        DESTINATION = L_PATH
      CHANGING
        TEMP        = L_PATH.
  ELSE.
    IF P_FNAME IS INITIAL.
      MESSAGE E001(00) WITH '请输入文件路径!'.
      STOP.
    ENDIF.
  ENDIF.

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

*&一致性检查:
  DATA:L_INDEX TYPE I,
       LR_OREF TYPE REF TO CX_ROOT,
       LV_TEXT TYPE STRING.
  DATA:LT_RAW TYPE TRUXS_T_TEXT_DATA.

  CLEAR:GT_UP[],GT_HA[],
        WA_UP,  WA_HEAD,
        GT_ITAB[], GT_ITAB.
*&S1.读取上传excel到内表
*  ( CALL FUNCTION 'ALSM_EXCEL_TO_INTERNAL_TABLE' )"要进行行列转换 限制1W行,
*  ( CALL FUNCTION 'TEXT_CONVERT_XLS_TO_SAP' )"直接转换到目标内表
  CALL FUNCTION 'TEXT_CONVERT_XLS_TO_SAP'
    EXPORTING
*     I_FIELD_SEPERATOR    =
      I_LINE_HEADER        = 'X'
      I_TAB_RAW_DATA       = LT_RAW
      I_FILENAME           = P_FNAME
    TABLES
      I_TAB_CONVERTED_DATA = GT_UP
    EXCEPTIONS
      CONVERSION_FAILED    = 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.

*&S2.内表单元格检查
  LOOP AT GT_UP INTO WA_UP.
    G_TABIX = SY-TABIX.
    WA_UP-LIN = SY-INDEX.
*&2.1客户订单号码
    IF WA_UP-BSTNK IS INITIAL.
      WA_UP-FLAG = 'E'. WA_UP-ICON = 1.
      WA_UP-REASON = '客户订单号码必输!'.

    ELSE.
      SELECT SINGLE *
               FROM VBAK
              WHERE BSTNK = WA_UP-BSTNK.
      IF SY-SUBRC = 0.
        WA_UP-FLAG = 'E'. WA_UP-ICON = 1.
        CONCATENATE '客户订单:' WA_UP-BSTNK '已导入'  ' /' WA_UP-REASON INTO

WA_UP-REASON.
      ENDIF.

    ENDIF.

*&S2.1.1栏位格式转换
    CONDENSE WA_UP-KUNNR NO-GAPS.
    CONDENSE WA_UP-MATNR NO-GAPS.
    CALL FUNCTION 'CONVERSION_EXIT_ALPHA_INPUT'
      EXPORTING
        INPUT  = WA_UP-KUNNR
      IMPORTING
        OUTPUT = WA_UP-KUNNR.
    CALL FUNCTION 'CONVERSION_EXIT_MATN1_INPUT'
      EXPORTING
        INPUT  = WA_UP-MATNR
      IMPORTING
        OUTPUT = WA_UP-MATNR.

*&2.2客户代码检查
    IF WA_UP-KUNNR IS NOT INITIAL.
      SELECT SINGLE *
               FROM KNA1
              WHERE KUNNR = WA_UP-KUNNR.
      IF SY-SUBRC NE 0.
        WA_UP-FLAG = 'E'. WA_UP-ICON = 1.
        CONCATENATE '客户代码错误' ' /' WA_UP-REASON INTO WA_UP-REASON.
      ENDIF.

    ELSE.
*& KNVV里找默认的零售的客户代码
      SELECT SINGLE KUNNR
        INTO WA_UP-KUNNR
        FROM KNVV
       WHERE VKORG EQ WA_UP-VKORG
         AND VTWEG EQ WA_UP-VTWEG
         AND SPART EQ WA_UP-SPART.
      IF SY-SUBRC NE 0.
        WA_UP-FLAG = 'E'. WA_UP-ICON = 1.
        CONCATENATE '找不到默认零售客户,请检查!'  ' /' WA_UP-REASON INTO

WA_UP-REASON.
      ENDIF.

    ENDIF.
*&2.3客户名称
    IF WA_UP-KUNNR IS INITIAL.
      IF WA_UP-NAME1 IS INITIAL.
        WA_UP-FLAG = 'E'. WA_UP-ICON = 1.
        CONCATENATE '客户名称必输!' ' /' WA_UP-REASON INTO WA_UP-REASON.
      ENDIF.
    ENDIF.
*&2.4送货地址
    IF WA_UP-KUNNR IS INITIAL.
      IF WA_UP-STREET IS INITIAL.
        WA_UP-FLAG = 'E'. WA_UP-ICON = 1.
        CONCATENATE '送货地址必输!' ' /' WA_UP-REASON INTO WA_UP-REASON.
      ENDIF.
    ENDIF.
*&2.5订单类型
    IF WA_UP-AUART IS INITIAL.
      WA_UP-FLAG = 'E'. WA_UP-ICON = 1.
      CONCATENATE '订单类型必输!' ' /' WA_UP-REASON INTO WA_UP-REASON.

    ELSE.
*&订单类型检查
      SELECT SINGLE *
               FROM TVAK
              WHERE AUART = WA_UP-AUART.
      IF SY-SUBRC NE 0.
        WA_UP-FLAG = 'E'. WA_UP-ICON = 1.
        CONCATENATE '订单类型错误' ' /' WA_UP-REASON INTO WA_UP-REASON.
      ENDIF.

    ENDIF.
*&2.6销售组织
    IF WA_UP-VKORG IS INITIAL.
      WA_UP-FLAG = 'E'. WA_UP-ICON = 1.
      CONCATENATE '销售组织必输!' ' /' WA_UP-REASON INTO WA_UP-REASON.

    ELSE.
*&销售组织检查
      SELECT SINGLE *
               FROM TVKO
              WHERE VKORG = WA_UP-VKORG.
      IF SY-SUBRC NE 0.
        WA_UP-FLAG = 'E'. WA_UP-ICON = 1.
        CONCATENATE '销售组织错误' ' /' WA_UP-REASON INTO WA_UP-REASON.
      ENDIF.

    ENDIF.
*&2.7销售部门
    IF WA_UP-VKBUR IS INITIAL.
      WA_UP-FLAG = 'E'. WA_UP-ICON = 1.
      CONCATENATE '销售部门必输!' ' /' WA_UP-REASON INTO WA_UP-REASON.

    ELSE.
*&销售部门检查
      SELECT SINGLE *
               FROM TVBUR
              WHERE VKBUR = WA_UP-VKBUR.
      IF SY-SUBRC NE 0.
        WA_UP-FLAG = 'E'. WA_UP-ICON = 1.
        CONCATENATE '销售部门错误' ' /' WA_UP-REASON INTO WA_UP-REASON.
      ENDIF.

    ENDIF.
*&2.8商品编码
    IF WA_UP-MATNR IS INITIAL.
      WA_UP-FLAG = 'E'. WA_UP-ICON = 1.
      CONCATENATE '商品编码必输!' ' /' WA_UP-REASON INTO WA_UP-REASON.

    ELSE.
*&商品检查
      SELECT SINGLE *
               FROM MARA
              WHERE MATNR = WA_UP-MATNR.
      IF SY-SUBRC NE 0.
        WA_UP-FLAG = 'E'. WA_UP-ICON = 1.
        CONCATENATE '商品不存在' ' /'  WA_UP-REASON INTO WA_UP-REASON.
      ENDIF.

    ENDIF.
*&2.9商品名称

*&S2.10销售数量
    CLEAR LV_TEXT.
    TRY.
        IF WA_UP-KWMENG IS INITIAL.
          WA_UP-FLAG = 'E'. WA_UP-ICON = 1.
          CONCATENATE '销售数量必输!' ' /' WA_UP-REASON INTO WA_UP-REASON.
        ENDIF.
      CATCH CX_SY_CONVERSION_ERROR INTO LR_OREF.
        LV_TEXT = LR_OREF->GET_TEXT( ).
    ENDTRY.
    IF LV_TEXT IS NOT INITIAL.
      WA_UP-FLAG = 'E'. WA_UP-ICON = 1.
      CONCATENATE '数量包含无效字符' ' /'  WA_UP-REASON INTO WA_UP-REASON.

    ELSE.
*&数量检查
      IF WA_UP-KWMENG <= 0.
        WA_UP-FLAG = 'E'. WA_UP-ICON = 1.
        CONCATENATE '数量不能小于0' ' /' WA_UP-REASON INTO WA_UP-REASON.
      ENDIF.

    ENDIF.
*&2.11销售单位
    IF WA_UP-VRKME IS INITIAL.
      WA_UP-FLAG = 'E'. WA_UP-ICON = 1.
      CONCATENATE '销售单位必输!' ' /'  WA_UP-REASON INTO WA_UP-REASON.
    ENDIF.
*&2.12单价必输
    CLEAR LV_TEXT.
    TRY.
        IF WA_UP-KBETR IS INITIAL.
          WA_UP-FLAG = 'E'. WA_UP-ICON = 1.
          CONCATENATE '单价必输!' ' /'  WA_UP-REASON INTO WA_UP-REASON.
        ENDIF.
      CATCH CX_SY_CONVERSION_ERROR INTO LR_OREF.
        LV_TEXT = LR_OREF->GET_TEXT( ).
    ENDTRY.
    IF LV_TEXT IS NOT INITIAL.
      WA_UP-FLAG = 'E'. WA_UP-ICON = 1.
      CONCATENATE '单价包含无效字符' ' /'  WA_UP-REASON INTO WA_UP-REASON.
    ENDIF.
*&2.13币种(如果为空,默认为CNY)
    IF WA_UP-WAERK IS INITIAL.
      WA_UP-WAERK = 'CNY'.
    ENDIF.
*&2.14价格单位(如果为空,默认为销售单位)
    IF WA_UP-KMEIN IS INITIAL.
      WA_UP-KMEIN =  WA_UP-VRKME.
    ENDIF.
*&2.15交货日期
    IF WA_UP-VDATU IS INITIAL.
      WA_UP-FLAG = 'E'. WA_UP-ICON = 1.
      CONCATENATE '交货日期必输!' ' /' WA_UP-REASON INTO WA_UP-REASON.

    ELSE.
*&日期栏位格式检查
      CALL FUNCTION 'DATE_CHECK_PLAUSIBILITY'
        EXPORTING
          DATE                      = WA_UP-VDATU
        EXCEPTIONS
          PLAUSIBILITY_CHECK_FAILED = 1
          OTHERS                    = 2.
      IF SY-SUBRC <> 0.
        WA_UP-FLAG = 'E'. WA_UP-ICON = 1.
        CONCATENATE '日期栏位格式不对' ' /' WA_UP-REASON INTO WA_UP-REASON.
      ENDIF.

    ENDIF.
*&2.16发货地点
    IF WA_UP-WERKS IS INITIAL.
* WA_UP-REASON = '发货地点必输!'.
      WA_UP-FLAG = 'E'. WA_UP-ICON = 1.
      CONCATENATE '发货地点必输!' ' /' WA_UP-REASON INTO WA_UP-REASON.

    ELSE.
*&地点检查
      SELECT SINGLE *
               FROM T001W
              WHERE WERKS = WA_UP-WERKS.
      IF SY-SUBRC = 0.
      ELSE.
        WA_UP-FLAG = 'E'. WA_UP-ICON = 1.
        CONCATENATE '地点不存在' ' /' WA_UP-REASON INTO WA_UP-REASON.
      ENDIF.

    ENDIF.

*&S3.1产品组 默认为:  10
    WA_UP-SPART = 10.
*&S3.2销售渠道默认为: 40
    WA_UP-VTWEG = 40.
*&-----------------------------
*&S3.4抬头栏位检查
*    PERFORM FRM_PROC_CHECK.
*&S3.5ITEM信息检查
*    PERFORM FRM_ITEM_CHECK.
*&S3.6判断是否有条目重
* (允许重复)
*&4.error log
    IF WA_UP-FLAG NE 'E'.
      WA_UP-FLAG = 'S'.
*     WA_UP-ICON = 2.
    ELSE.
*&取得错误数据
      R_BSTNK-SIGN   = 'I'.
      R_BSTNK-OPTION = 'EQ'.
      R_BSTNK-LOW    = WA_UP-BSTNK.
      COLLECT R_BSTNK.
      CLEAR R_BSTNK.
    ENDIF.

*&4.1excel栏位赋值内表
    MODIFY GT_UP FROM WA_UP INDEX G_TABIX.
    MOVE-CORRESPONDING WA_UP TO WA_HA.

*&4.2赋值GT_HA.
    CLEAR:WA_HA-FLAG, WA_HA-REASON, WA_HA-ICON.
**&A.判断重复:
*    READ TABLE GT_UPE INTO WA_UPE WITH KEY BSTNK = WA_HA-BSTNK
*                                           AUART = WA_HA-AUART
*                                           KUNNR = WA_HA-KUNNR
*                                           VKORG = WA_HA-VKORG
*                                           VKBUR = WA_HA-VKBUR.
*    IF SY-SUBRC EQ 0.
*      G_TABIX = SY-TABIX.
*      WA_UP-FLAG = 'E'. WA_UP-ICON = 1.
*      CONCATENATE '采购订单号' WA_UPE-BSTNK '有重复' '/' WA_HA-REASON INTO WA_HA-REASON.
*      MODIFY GT_HA FROM WA_HA INDEX G_TABIX.
*    ENDIF.

    COLLECT WA_HA INTO GT_HA.
*&clear 变量:
    CLEAR:WA_UP, WA_HEAD.
*&Q1.判断内表是否重复
    GT_UPE[] = GT_HA[].

  ENDLOOP.

  SORT GT_HA BY BSTNK KUNNR AUART VKORG VKBUR.
*&A1.判断重复(客户订单)
*  LOOP AT GT_HEAD.
*    AT NEW BSTNK.
*      WA_HEAD-LIN = 1.
*      CONTINUE.
*    ENDAT.
*      WA_HEAD-LIN = WA_HEAD-LIN + 1.
**    AT END OF BSTNK.
**      WA_HEAD-LIN = WA_HEAD-LIN + 1.
**    ENDAT.
*    IF WA_HEAD-LIN LT 1.
*      CONCATENATE '客户订单号:' GT_HEAD-BSTNK '出现重复,请检查!' INTO WA_HEAD-REASON.
*    ENDIF.
*    MODIFY GT_HEAD FROM WA_HEAD TRANSPORTING LIN REASON.
*    CLEAR GT_HEAD.
*  ENDLOOP.
*  DATA:G_DAY TYPE D.
*  GET TIME. "STAMP FIELD G_DAY.
*  LOOP AT GT_HEAD WHERE LIN LT 1.
*    LOOP AT GT_UP INTO WA_UP WHERE BSTNK EQ GT_HEAD-BSTNK
*                    AND AUART EQ GT_HEAD-AUART
*                    AND KUNNR EQ GT_HEAD-KUNNR
*                    AND VKORG EQ GT_HEAD-VKORG.
*      CONCATENATE WA_HEAD-REASON '/' WA_UP-REASON INTO WA_UP-REASON.
*      WA_UP-FLAG = 'E'. WA_UP-ICON = 1.
*      MODIFY GT_UP FROM WA_UP TRANSPORTING FLAG ICON REASON.
*      CLEAR WA_UP.
*
*    ENDLOOP.
*
*  ENDLOOP.

  CLEAR:GT_UPE[].
  SORT GT_UP BY BSTNK KUNNR AUART VKORG VKBUR MATNR.
*&S3.
  G_LIN = LINES( GT_UP[] ).
  IF G_LIN EQ 0.
    MESSAGE E001(00) WITH '没有任何数据!'.
  ELSE.
    MESSAGE S001(00) WITH '导入数据' G_LIN '条!'.
  ENDIF.

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

*&S4对于ok的客户订单号 test CHECK
*&C1.获取ok的客户订单号
*CLEAR: GT_HEAD[].
  GT_HEAD[] = GT_HA[].
  IF R_BSTNK[] IS NOT INITIAL.
    DELETE GT_HEAD WHERE BSTNK IN R_BSTNK.
  ENDIF.

*&C2.
  PERFORM FRM_FOR_SO.

*&C3.返回信息显示明细
  LOOP AT GT_HEAD INTO WA_HEAD WHERE FLAG = 'E'.
    LOOP AT GT_UP INTO WA_UP WHERE AUART = WA_HEAD-AUART
                                AND VKORG = WA_HEAD-VKORG
                                AND VTWEG = WA_HEAD-VTWEG
                                AND KUNNR = WA_HEAD-KUNNR
                                AND BSTNK = WA_HEAD-BSTNK
                                AND VKBUR = WA_HEAD-VKBUR .
      WA_UP-VBELN = WA_HEAD-VBELN.
      WA_UP-FLAG  = WA_HEAD-FLAG.
      WA_UP-ICON  = WA_HEAD-ICON.
      WA_UP-REASON = WA_HEAD-REASON.
*CONCATENATE WA_UP-REASON '-' WA_UP-REASON INTO WA_UP-REASON.
      MODIFY GT_UP FROM WA_UP TRANSPORTING FLAG ICON REASON.
    ENDLOOP.
  ENDLOOP.

ENDFORM.                    " FRM_TEST_RUN
*&---------------------------------------------------------------------*
*&      Form  FRM_FOR_SO
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM FRM_FOR_SO .
  DATA:BEGIN OF LT_BSTNR OCCURS 0,
         VBELN LIKE VBAK-VBELN,
       END OF LT_BSTNR.
*&A.判断客户订单号是否重复
  LOOP AT GT_HEAD INTO WA_HEAD.
    CLEAR:LT_ITEM[],WA_HEADER,
          LT_SCH[], LT_EX[].
*&1HEAD参数设置
    PERFORM FRM_SET_HEAD.
*&2ITEM参数设置
    PERFORM FRM_SET_ITEM.
*&3BAPI创建so
    PERFORM FRM_CALL_BAPI.

  ENDLOOP.

ENDFORM.                    " FRM_FOR_SO
*&---------------------------------------------------------------------*
*&      Form  FRM_SET_HEAD
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM FRM_SET_HEAD .
 CLEAR:WA_HEADER,WA_HEADERX.
*&H1.抬头必输
  WA_HEADER-PURCH_NO_C = WA_HEAD-BSTNK."客户订单号码
  WA_HEADER-DOC_TYPE   = WA_HEAD-AUART."订单类型
  WA_HEADER-SALES_ORG  = WA_HEAD-VKORG."销售组织
  WA_HEADER-SALES_OFF  = WA_HEAD-VKBUR."销售部门
  WA_HEADER-DISTR_CHAN = WA_HEAD-VTWEG."分销渠道
  WA_HEADER-DIVISION   = WA_HEAD-SPART."产品组
  WA_HEADER-REQ_DATE_H = WA_HEAD-VDATU."请求交货日期
*WA_HEADER-PO_METHOD = WA_HEAD-BSARK."客户采购订单类型
*WA_HEADER-REF_1     = WA_HEAD-BSTNK."参考
  WA_HEADERX-PURCH_NO_C = WA_HEAD-BSTNK."客户订单号码

  WA_HEADERX-DOC_TYPE   = 'X'."订单类型
  WA_HEADERX-SALES_ORG  = 'X'."销售组织
  WA_HEADERX-SALES_OFF  = 'X'."销售部门
  WA_HEADERX-DISTR_CHAN = 'X'."分销渠道
  WA_HEADERX-DIVISION   = 'X'."产品组
  WA_HEADERX-REQ_DATE_H = 'X'."请求交货日期
*WA_HEADERX-PO_METHOD = 'X'."客户采购订单类型
*WA_HEADERX-REF_1     = 'X'."参考

*&H2.Partners(?一次性客户处理是否有差别(一次性客户有无标识))
  CLEAR: WA_PARTNER, LT_PART[].
*&通讯字段: SD单据合作伙伴:WWW(WA_PARTNER)
* AG-售达方
* WE-送达方
*'RG'-付款方
*'RE'-开票方
  WA_PARTNER-PARTN_ROLE = 'AG'.         "售达方
  WA_PARTNER-PARTN_NUMB = WA_HEAD-KUNNR.
*&"一次性客户
  READ TABLE GT_UP INTO WA_UP WITH KEY AUART = WA_HEAD-AUART
                                       VKORG = WA_HEAD-VKORG
                                       VTWEG = WA_HEAD-VTWEG
                                       KUNNR = WA_HEAD-KUNNR
                                       BSTNK = WA_HEAD-BSTNK
                                       VKBUR = WA_HEAD-VKBUR.

  IF WA_UP-NAME1 IS NOT INITIAL.
    WA_PARTNER-NAME   = WA_UP-NAME1. "名称
    WA_PARTNER-STREET = WA_UP-STREET."地址
  ENDIF.
  WA_PARTNER-COUNTRY = 'CN'.
  WA_PARTNER-LANGU = SY-LANGU.
  APPEND WA_PARTNER TO LT_PART.
  CLEAR WA_PARTNER.

*&H4.送达方
  IF  WA_UP-KUNNR IS NOT INITIAL.
    CLEAR WA_PARTNER.
    WA_PARTNER-PARTN_ROLE = 'WE'.          "送达方
    WA_PARTNER-PARTN_NUMB = WA_HEAD-KUNNR. "客户
    WA_PARTNER-NAME       = WA_HEAD-NAME1. "名称
    WA_PARTNER-STREET     = WA_HEAD-STREET."地址
    WA_PARTNER-COUNTRY    = 'CN'.
    APPEND WA_PARTNER TO LT_PART.
    CLEAR WA_PARTNER.
  ENDIF.

*&ADD.
*  "RG 付款方
*  WA_PARTNER-PARTN_ROLE = 'RG'.
*  WA_PARTNER-PARTN_NUMB = WA_HEAD-KUNNR.
**  IF UP_HEAD-NAME1_RG IS NOT INITIAL.  "一次性客户
**    WA_PARTNER-NAME     = UP_HEAD-NAME1_RG.
**    WA_PARTNER-COUNTRY  = 'CN'.
**  ENDIF.
*  APPEND WA_PARTNER TO LT_PART.
*  CLEAR WA_PARTNER.

*&订单条件的通讯字段
    CLEAR:LT_COND[], LT_COND,
          LT_CONDX[],LT_CONDX.
**&抬头赋值定价条件类型---条件ZPB0不允许作为抬头条件
*  LOOP AT GT_UP INTO WA_UP  WHERE BSTNK = WA_HEAD-BSTNK
*                               AND KUNNR = WA_HEAD-KUNNR
*                               AND AUART = WA_HEAD-AUART
*                               AND VKORG = WA_HEAD-VKORG
*                               AND VKBUR = WA_HEAD-VKBUR
*                               AND VDATU = WA_HEAD-VDATU.
**AND VTWEG = WA_HEAD-VTWEG
**&H3定价控制
*    IF NOT WA_UP-KBETR IS INITIAL.
*      WA_COND-COND_TYPE  = 'ZPB0'.     "定价条件
*      WA_COND-COND_VALUE = WA_UP-KBETR."价格
*      WA_COND-CURRENCY   = WA_UP-WAERK."货币或%\
** WA_COND-COND_P_UNT = 1."条件定价单位
*      APPEND WA_COND TO LT_COND.
*      CLEAR WA_COND.
*
*      WA_CONDX-COND_TYPE  = 'ZPB0'."定价条件
*      WA_CONDX-COND_VALUE = 'X'.   "价格
*      WA_CONDX-CURRENCY   = 'X'.   "货币或%
**      WA_CONDX-UPDATEFLAG = 'U'.
*      APPEND WA_CONDX TO LT_CONDX.
*      CLEAR: WA_CONDX.
*    ENDIF.
*  ENDLOOP.
  EXIT.

ENDFORM.                    " FRM_SET_HEAD
*&---------------------------------------------------------------------*
*&      Form  FRM_SET_ITEM
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM FRM_SET_ITEM .
  DATA: L_POSNR LIKE VBAP-POSNR,
        L_LP    LIKE VBAP-POSNR VALUE '000010'.
  CLEAR:LT_ITEM[],
        LT_SCH[].
*        LT_COND[].

*&item 参数赋值
  LOOP AT GT_UP INTO WA_UP  WHERE BSTNK = WA_HEAD-BSTNK
                               AND KUNNR = WA_HEAD-KUNNR
                               AND AUART = WA_HEAD-AUART
                               AND VKORG = WA_HEAD-VKORG
                               AND VKBUR = WA_HEAD-VKBUR
                               AND VDATU = WA_HEAD-VDATU.

    READ TABLE LT_ITEM INTO WA_ITEM  WITH KEY MATERIAL = WA_UP-MATNR.
    IF SY-SUBRC NE 0.
*&i1.line赋值
      ADD L_LP TO L_POSNR.
      CLEAR WA_ITEM.
      WA_ITEM-ITM_NUMBER = L_POSNR.
      WA_ITEM-MATERIAL   = WA_UP-MATNR. "商品
      WA_ITEM-SALES_UNIT = WA_UP-VRKME. "计量单位
      WA_ITEM-PLANT      = WA_UP-WERKS. "工厂
*& WA_ITEM-STORE_LOC  = WA_SO-LGORT. "库存地
      APPEND WA_ITEM TO LT_ITEM.

*&i2.计划行数据
      CLEAR WA_SCH.
      WA_SCH-ITM_NUMBER = L_POSNR.
      WA_SCH-REQ_QTY    = WA_UP-KWMENG."数量
*WA_SCH-REQ_DATE   = WA_UP-VDATU. "计划行日期
      APPEND WA_SCH TO LT_SCH.
*&i3.订单条件的通讯字段
      CLEAR: WA_COND.
      WA_COND-ITM_NUMBER = L_POSNR.
      WA_COND-COND_TYPE  = 'ZPB0'.     "定价条件
*&->ADD(解决价格扩大十倍问题)
      WA_COND-COND_VALUE = WA_UP-KBETR / 10."价格
      WA_COND-COND_UNIT  = WA_UP-KMEIN."条件单位
*WA_COND-COND_P_UNT = WA_UP-KMEIN.  "条件定价单位
*WA_COND-CURRENCY   = LS_COND-KOEIN."货币或%
      WA_CONDX-UPDATEFLAG = 'I'.
      WA_CONDX-COND_TYPE  = 'ZPB0'."定价条件
      WA_CONDX-COND_VALUE = 'X'.   "价格
      WA_CONDX-COND_UNIT  = 'X'."条件单位
*      WA_CONDX-CURRENCY   = 'X'.   "货币或%
*      APPEND WA_CONDX TO LT_CONDX.
      APPEND WA_COND TO LT_COND.

    ENDIF.
  ENDLOOP.


ENDFORM.                    " FRM_SET_ITEM
*&---------------------------------------------------------------------*
*&      Form  FRM_CALL_BAPI
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM FRM_CALL_BAPI .
*&只需创建正常SO,
  DATA:L_TEST LIKE BAPIFLAG-BAPIFLAG.

  CLEAR :L_VBELN,
         L_RETURN[],
         L_TEST,
         L_RETURN[].

  IF P_TEST EQ 'X'.
    L_TEST = 'X'.
  ENDIF.

*&正常SO:C,调用BAPI_SALESORDER_CREATEFROMDAT2
  CALL FUNCTION 'BAPI_SALESORDER_CREATEFROMDAT2'
    EXPORTING
      ORDER_HEADER_IN      = WA_HEADER
      TESTRUN              = L_TEST
    IMPORTING
      SALESDOCUMENT        = L_VBELN
    TABLES
      RETURN               = L_RETURN
      ORDER_ITEMS_IN       = LT_ITEM
      ORDER_PARTNERS       = LT_PART
      ORDER_SCHEDULES_IN   = LT_SCH
      ORDER_CONDITIONS_IN  = LT_COND
      ORDER_CONDITIONS_INX = LT_CONDX
      ORDER_TEXT           = LT_TEXT
      EXTENSIONIN          = LT_EX.

  READ TABLE L_RETURN WITH KEY TYPE = 'E'.
  IF SY-SUBRC NE 0.
    IF P_EXCUTE EQ 'X'.
      CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'.
      WA_HEAD-ICON = 3.
    ENDIF.

    G_SLIN = G_SLIN + 1.
    WA_HEAD-VBELN = L_VBELN.
    WA_HEAD-FLAG = 'S'.
    CONCATENATE '销售订单:' L_VBELN '成功创建' INTO WA_HEAD-REASON.
    MODIFY GT_HEAD FROM WA_HEAD TRANSPORTING VBELN FLAG ICON REASON.
*&Q3.TEST.
  ELSE.
    G_FLIN = G_FLIN + 1.
    IF P_EXCUTE EQ 'X'.
      CALL FUNCTION 'BAPI_TRANSACTION_ROLLBACK'.
    ENDIF.

    WA_HEAD-FLAG = 'E'.
    WA_HEAD-ICON = 1.
    READ TABLE L_RETURN WITH KEY TYPE = 'E'.
    WA_HEAD-REASON = L_RETURN-MESSAGE.
    MODIFY GT_HEAD FROM WA_HEAD TRANSPORTING FLAG ICON REASON.
  ENDIF.

ENDFORM.                    " FRM_CALL_BAPI

 

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!