BAPI:创建销售订单(VA01)

业务:如果用户自己填写了销售价格,根据填写的价格维护销售订单定价条件,如果没填则根据配置的定价条件自动带出。
BAPI:创建销售订单(VA01)_第1张图片

*&---------------------------------------------------------------------*
*&      Form  FRM_SALESDOCUMENT_CREATE
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
 FORM FRM_SALESDOCUMENT_CREATE .
   DATA: LW_SALES_HEADER_IN      LIKE BAPISDHD1,
         LW_SALES_HEADER_INX     LIKE BAPISDHD1X,
         LW_LOGIC_SWITCH         TYPE BAPISDLS,
         LV_SALESDOCUMENT_EX     LIKE BAPIVBELN-VBELN,
         LT_SALES_ITEMS_IN       LIKE TABLE OF BAPISDITM  WITH HEADER LINE,
         LT_SALES_ITEMS_INX      LIKE TABLE OF BAPISDITMX WITH HEADER LINE,
         LT_SALES_SCHEDULES_IN   LIKE TABLE OF BAPISCHDL  WITH HEADER LINE,
         LT_SALES_SCHEDULES_INX  LIKE TABLE OF BAPISCHDLX WITH HEADER LINE,
         LT_SALES_CONDITIONS_IN  LIKE TABLE OF BAPICOND   WITH HEADER LINE,
         LT_SALES_CONDITIONS_INX LIKE TABLE OF BAPICONDX  WITH HEADER LINE,
         LT_SALES_TEXT           LIKE TABLE OF BAPISDTEXT WITH HEADER LINE,
         LT_SALES_PARTNERS_IN    LIKE TABLE OF BAPIPARNR  WITH HEADER LINE,
         LT_RETURN               LIKE TABLE OF BAPIRET2   WITH HEADER LINE.
   DATA: L_ANSWER TYPE C,
         LW_KWMENG TYPE KWMENG,
         LW_COND_VALUE TYPE BAPIKBETR1,
         LW_COND_VALUE1 TYPE BAPIKBETR1,
         LW_SCHED_LINE TYPE ETENR VALUE '0001',
         LW_ITM_NUM TYPE POSNR_VA .

   CLEAR: LT_SALES_ITEMS_IN,     LT_SALES_ITEMS_INX,
          LT_SALES_SCHEDULES_IN, LT_SALES_SCHEDULES_INX,
          LT_SALES_CONDITIONS_IN,LT_SALES_CONDITIONS_INX,
          LT_SALES_PARTNERS_IN,
          LT_RETURN.
   CLEAR :GT_ZTSD003[],
          GT_YTLIST1[].

   GT_YTLIST1[] = GT_YTLIST[].
   SORT GT_YTLIST1 BY VBELN.
   DELETE GT_YTLIST1 WHERE FLAG <> ABAP_ON.

   SELECT *
     INTO CORRESPONDING FIELDS OF TABLE GT_ZTSD003
     FROM ZTSD003
     FOR ALL ENTRIES IN GT_YTLIST1
     WHERE VBELN = GT_YTLIST1-VBELN.

   SORT GT_ZTSD003 BY MATNR.

**=> sales order header
   IF LW_SALES_HEADER_IN IS INITIAL.
     LW_SALES_HEADER_IN-DOC_TYPE    = GS_XSBC-AUART.    " 销售凭证类型
     LW_SALES_HEADER_IN-SALES_ORG   = GS_XSBC-VKORG.    " 销售机构
     LW_SALES_HEADER_IN-DISTR_CHAN  = GS_XSBC-VTWEG.    " 分销渠道
     LW_SALES_HEADER_IN-DIVISION    = GS_XSBC-SPART.    " 产品组

     LW_SALES_HEADER_INX-DOC_TYPE   = ABAP_TRUE.
     LW_SALES_HEADER_INX-SALES_ORG  = ABAP_TRUE.
     LW_SALES_HEADER_INX-DISTR_CHAN = ABAP_TRUE.
     LW_SALES_HEADER_INX-DIVISION   = ABAP_TRUE.

   ENDIF.

   IF LT_SALES_PARTNERS_IN[] IS INITIAL.
     CLEAR : LT_SALES_PARTNERS_IN.
     LT_SALES_PARTNERS_IN-PARTN_ROLE = 'AG'.         "
     LT_SALES_PARTNERS_IN-PARTN_NUMB = GS_XSBC-KUNAG. " 售达方
     APPEND LT_SALES_PARTNERS_IN.
     CLEAR LT_SALES_PARTNERS_IN.

     LT_SALES_PARTNERS_IN-PARTN_ROLE = 'WE'.         "
     LT_SALES_PARTNERS_IN-PARTN_NUMB = GS_XSBC-KUNWE." 送达方
     APPEND LT_SALES_PARTNERS_IN.
     CLEAR LT_SALES_PARTNERS_IN.
   ENDIF.

   LOOP AT GT_ZTSD003 INTO GS_ZTSD003.

     LW_KWMENG     = GS_ZTSD003-KWMENG + LW_KWMENG.
     LW_COND_VALUE = GS_ZTSD003-ZKBETR_XS.
     LW_COND_VALUE1 = GS_ZTSD003-ZKBETR_ZD.

**=> sales order item
     LT_SALES_ITEMS_IN-MATERIAL   = GS_ZTSD003-MATNR.        " 物料
     LT_SALES_ITEMS_IN-PLANT      = GS_ZTSD003-WERKS.        " 工厂
     LT_SALES_ITEMS_IN-STORE_LOC  = GS_ZTSD003-LGORT.        " 库存地点
     LT_SALES_ITEMS_INX-MATERIAL    = ABAP_TRUE.
     LT_SALES_ITEMS_INX-PLANT       = ABAP_TRUE.
     LT_SALES_ITEMS_INX-STORE_LOC   = ABAP_TRUE.
     AT END OF MATNR.
       LW_ITM_NUM = LW_ITM_NUM + 10.
**=> sales order item
       LT_SALES_ITEMS_IN-ITM_NUMBER = LW_ITM_NUM.
       LT_SALES_ITEMS_INX-ITM_NUMBER  = LW_ITM_NUM.
*
       APPEND: LT_SALES_ITEMS_IN, LT_SALES_ITEMS_INX.
       CLEAR:  LT_SALES_ITEMS_IN, LT_SALES_ITEMS_INX.

       " 计划行号
       LT_SALES_SCHEDULES_IN-ITM_NUMBER = LW_ITM_NUM.
       LT_SALES_SCHEDULES_IN-SCHED_LINE = LW_SCHED_LINE.       " L_TABIX.
       LT_SALES_SCHEDULES_IN-REQ_DATE   = SY-DATUM.     " 计划行日期
       LT_SALES_SCHEDULES_IN-REQ_QTY    = LW_KWMENG.    " 以销售单位计的 订单数量

       LT_SALES_SCHEDULES_INX-ITM_NUMBER = LW_ITM_NUM.
       LT_SALES_SCHEDULES_INX-SCHED_LINE = LW_SCHED_LINE.
       LT_SALES_SCHEDULES_INX-REQ_DATE   = ABAP_TRUE.
       LT_SALES_SCHEDULES_INX-REQ_QTY    = ABAP_TRUE.

       APPEND: LT_SALES_SCHEDULES_IN, LT_SALES_SCHEDULES_INX.
       CLEAR:  LT_SALES_SCHEDULES_IN, LT_SALES_SCHEDULES_INX,LW_KWMENG.

*----- 条件
       IF LW_COND_VALUE NE 0 AND LW_COND_VALUE1 NE 0.
*----- 复制不变定价因素且重定税款
         LW_LOGIC_SWITCH-PRICING = 'G'.

         LT_SALES_CONDITIONS_IN-ITM_NUMBER  = LW_ITM_NUM.
         LT_SALES_CONDITIONS_IN-COND_TYPE   = 'ZPR1'.           " 条件类型 定价条件
         LT_SALES_CONDITIONS_IN-COND_VALUE  = LW_COND_VALUE.    " 价格     定价比率
         LT_SALES_CONDITIONS_IN-CURRENCY    = 'CNY'.            " 货币码

         LT_SALES_CONDITIONS_INX-ITM_NUMBER = LW_ITM_NUM.
         LT_SALES_CONDITIONS_INX-COND_TYPE  = 'ZPR1'.
         LT_SALES_CONDITIONS_INX-CURRENCY   = ABAP_TRUE.
         LT_SALES_CONDITIONS_INX-COND_VALUE = ABAP_TRUE.


         APPEND: LT_SALES_CONDITIONS_IN,LT_SALES_CONDITIONS_INX.
         CLEAR:  LT_SALES_CONDITIONS_IN,LT_SALES_CONDITIONS_INX,LW_COND_VALUE.

         LT_SALES_CONDITIONS_IN-ITM_NUMBER  = LW_ITM_NUM.
         LT_SALES_CONDITIONS_IN-COND_TYPE   = 'ZPR2'.           " 条件类型 定价条件
         LT_SALES_CONDITIONS_IN-COND_VALUE  = LW_COND_VALUE1.    " 价格     定价比率
         LT_SALES_CONDITIONS_IN-CURRENCY    = 'CNY'.            " 货币码

         LT_SALES_CONDITIONS_INX-ITM_NUMBER = LW_ITM_NUM.
         LT_SALES_CONDITIONS_INX-COND_TYPE  = 'ZPR2'.
         LT_SALES_CONDITIONS_INX-CURRENCY   = ABAP_TRUE.
         LT_SALES_CONDITIONS_INX-COND_VALUE = ABAP_TRUE.


         APPEND: LT_SALES_CONDITIONS_IN,LT_SALES_CONDITIONS_INX.
         CLEAR:  LT_SALES_CONDITIONS_IN,LT_SALES_CONDITIONS_INX,LW_COND_VALUE.

       ENDIF.

     ENDAT.

   ENDLOOP.

   CALL FUNCTION 'SD_SALESDOCUMENT_CREATE'
     EXPORTING
       SALES_HEADER_IN      = LW_SALES_HEADER_IN
       SALES_HEADER_INX     = LW_SALES_HEADER_INX
       CONVERT_PARVW_AUART  = ABAP_ON
       LOGIC_SWITCH         = LW_LOGIC_SWITCH
     IMPORTING
       SALESDOCUMENT_EX     = LV_SALESDOCUMENT_EX
     TABLES
       RETURN               = LT_RETURN
       SALES_ITEMS_IN       = LT_SALES_ITEMS_IN
       SALES_ITEMS_INX      = LT_SALES_ITEMS_INX
       SALES_SCHEDULES_IN   = LT_SALES_SCHEDULES_IN
       SALES_SCHEDULES_INX  = LT_SALES_SCHEDULES_INX
       SALES_CONDITIONS_IN  = LT_SALES_CONDITIONS_IN
       SALES_CONDITIONS_INX = LT_SALES_CONDITIONS_INX
       SALES_PARTNERS       = LT_SALES_PARTNERS_IN.

   IF SY-SUBRC EQ 0 AND LV_SALESDOCUMENT_EX IS NOT INITIAL.

     GT_YTLIST-ZVBELN_S = LV_SALESDOCUMENT_EX.
     MODIFY GT_YTLIST TRANSPORTING ZVBELN_S WHERE FLAG = ABAP_ON .
     LOOP AT GT_YTLIST WHERE FLAG = ABAP_ON.
       UPDATE ZTSD004 SET ZVBELN_S = LV_SALESDOCUMENT_EX
       WHERE VBELN = GT_YTLIST-VBELN.
     ENDLOOP.
     MESSAGE S020(ZMSG) WITH LV_SALESDOCUMENT_EX.
     COMMIT WORK.
   ELSE.

     LOOP AT LT_RETURN WHERE TYPE = 'E'.
       MESSAGE LT_RETURN-MESSAGE TYPE 'S' DISPLAY LIKE 'E'.
       EXIT.
     ENDLOOP.

     ROLLBACK WORK.
   ENDIF.
 ENDFORM.  

你可能感兴趣的:(ABAP:BAPI)