欢迎来到萧静默的博客

书山有路勤为径,学海无涯苦作舟。

创建有WBS分配规则的生产订单

有时候的生产订单是需要挂WBS的,但是使用如下代码创建不了

DATA:LS_ORDERDATA TYPE BAPI_PP_ORDER_CREATE.
  DATA:ls_return         TYPE bapiret2,
       LV_ORDER_NUMBER TYPE BAPI_ORDER_KEY-ORDER_NUMBER.
  LS_ORDERDATA-MATERIAL_LONG = IS_INPUT-MATNR."物料号
  LS_ORDERDATA-PLANT = IS_INPUT-WERKS.   "工厂
  LS_ORDERDATA-ORDER_TYPE = IS_INPUT-auart.    "订单类型
  LS_ORDERDATA-QUANTITY = IS_INPUT-gamng.   " 订单数量
  LS_ORDERDATA-QUANTITY_uom = IS_INPUT-GMEIN.   "计量单位
  LS_ORDERDATA-PROD_VERSION = IS_INPUT-VERID.   "订单类型
  LS_ORDERDATA-BASIC_START_DATE = SY-DATUM ."SY-DATUM. "      基本开始日期
  LS_ORDERDATA-BASIC_END_DATE = IS_INPUT-GLTRP.           "基本结束日期
  LS_ORDERDATA-WBS_ELEMENT = IS_INPUT-PS_PSP_PNR.           "wbs
  CALL FUNCTION 'CONVERSION_EXIT_CUNIT_INPUT' "单位
       EXPORTING
         input          = iS_input-gmein
*        LANGUAGE       = SY-LANGU
       IMPORTING
         output         = LS_ORDERDATA-quantity_uom
       EXCEPTIONS
         unit_not_found = 1
         OTHERS         = 2.
     IF sy-subrc <> 0.
     ENDIF.

  CALL FUNCTION 'BAPI_PRODORD_CREATE'
    EXPORTING
      ORDERDATA    = LS_ORDERDATA
*     FSH_PRODORD_SEASON       =
    IMPORTING
      RETURN       = ls_return
      ORDER_NUMBER = LV_ORDER_NUMBER
*     ORDER_TYPE   =
    .
 IF ls_return-TYPE = 'E'.
    CALL FUNCTION 'BAPI_TRANSACTION_ROLLBACK'.
    ROLLBACK WORK.
    CONCATENATE '创建生产订单报错:' ls_return-MESSAGE INTO EV_MESSAGE.
   ev_status = 'E'.
  ELSE.
    CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'
      EXPORTING
        WAIT = 'X'.
    ev_status = 'S'.
    ev_AUFNR = LV_ORDER_NUMBER.
    MOVE  '生产订单创建成功' TO EV_message.
  ENDIF.

此时想到使用录频来实现

FUNCTION ZFM_ORDER_WBS_CREATE.
*"----------------------------------------------------------------------
*"*"本地接口:
*"  IMPORTING
*"     VALUE(MATNR) TYPE  MATNR OPTIONAL
*"     VALUE(WERKS) TYPE  WERKS_D OPTIONAL
*"     VALUE(AUART) TYPE  CAUFVD-AUART OPTIONAL
*"     VALUE(GAMNG) TYPE  CAUFVD-GAMNG OPTIONAL
*"     VALUE(GMEIN) TYPE  CO_GMEIN OPTIONAL
*"     VALUE(GLTRP) TYPE  CO_GSTRP OPTIONAL
*"     VALUE(GSTRP) TYPE  CO_GSTRP OPTIONAL
*"     VALUE(ZWBS) TYPE  PS_POSID OPTIONAL
*"  TABLES
*"      TT_RETURN STRUCTURE  BAPIRET1
*"----------------------------------------------------------------------

DATA:ZUNIT TYPE LAGME.

  CALL FUNCTION 'CONVERSION_EXIT_CUNIT_OUTPUT' "单位
    EXPORTING
      INPUT          = GMEIN
*     LANGUAGE       = 'ZH'
    IMPORTING
      OUTPUT         = GMEIN
    EXCEPTIONS
      UNIT_NOT_FOUND = 1
      OTHERS         = 2.
  IF SY-SUBRC <> 0.
  ENDIF.

  PERFORM FRM_CREAT_WBS_ORDER TABLES TT_RETURN USING  MATNR WERKS AUART GAMNG GMEIN GLTRP GSTRP ZWBS.

ENDFUNCTION.
*----------------------------------------------------------------------*
***INCLUDE LZFM_ORDER_WBS_CREATEF01.
*----------------------------------------------------------------------*
*&---------------------------------------------------------------------*
*& Form FRM_CREAT_WBS_ORDER
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
*      -->P_TT_RETURN  text
*      -->P_MATNR  text
*      -->P_WERKS  text
*      -->P_AUART  text
*      -->P_GAMNG  text
*      -->P_GMEIN  text
*      -->P_GLTRP  text
*      -->P_GSTRP  text
*      -->P_ZWBS  text
*&---------------------------------------------------------------------*
FORM FRM_CREAT_WBS_ORDER  TABLES   P_TT_RETURN STRUCTURE GT_RETURN
                          USING    P_MATNR
                                   P_WERKS
                                   P_AUART
                                   P_GAMNG
                                   P_GMEIN
                                   P_GLTRP
                                   P_GSTRP
                                   P_ZWBS.
  DATA: ZMENGE    LIKE AFPO-PSMNG,
        FVAL2(13) TYPE C.
  DATA: LV_MES1 TYPE CHAR100.
  DATA: EV_INFNR TYPE CHAR10.
  PERFORM BDC_DYNPRO      USING 'SAPLCOKO1' '0100'.
  PERFORM BDC_FIELD       USING 'BDC_CURSOR'
        'AFPOD-PWERK'.
  PERFORM BDC_FIELD       USING 'BDC_OKCODE'
        '=ENTK'.
  PERFORM BDC_FIELD       USING 'CAUFVD-MATNR'
        P_MATNR. "物料编码
  PERFORM BDC_FIELD       USING 'CAUFVD-WERKS'
        P_WERKS.    "工厂
  PERFORM BDC_FIELD       USING 'AUFPAR-PP_AUFART'
        P_AUART.    "订单类型
  PERFORM BDC_DYNPRO      USING 'SAPLCOKO1' '0115'.
  PERFORM BDC_FIELD       USING 'BDC_OKCODE'
        '/00'.
  PERFORM BDC_FIELD       USING 'BDC_CURSOR'
        'CAUFVD-GSTRP'.

  PERFORM BDC_FIELD       USING 'CAUFVD-GAMNG'
        P_GAMNG.    "订单数量
  PERFORM BDC_FIELD       USING 'CAUFVD-GMEIN'
        P_GMEIN.     "订单基本单位
  PERFORM BDC_FIELD       USING 'CAUFVD-GLTRP'
        P_GLTRP.           "结束日期
  PERFORM BDC_FIELD       USING 'CAUFVD-GSTRP'
        P_GSTRP.     "开始日期
  PERFORM BDC_FIELD       USING 'CAUFVD-TERKZ'
        '2'.      "类型2向后  固定
  PERFORM BDC_FIELD       USING 'CAUFVD-FHORI'
        '000'.      "排产时间容余码  固定
  PERFORM BDC_DYNPRO      USING 'SAPLKOBS' '0130'.
  PERFORM BDC_FIELD       USING 'BDC_CURSOR'
        'DKOBR-EMPGE(02)'.
  PERFORM BDC_FIELD       USING 'BDC_OKCODE'
        '/00'.
  PERFORM BDC_FIELD       USING 'DKOBR-EMPGE(02)'
        P_ZWBS.      "WBS
  PERFORM BDC_DYNPRO      USING 'SAPLKOBS' '0130'.
  PERFORM BDC_FIELD       USING 'BDC_CURSOR'
        'COBRB-PROZS(01)'.
  PERFORM BDC_FIELD       USING 'BDC_OKCODE'
        '/00'.
  PERFORM BDC_FIELD       USING 'COBRB-PROZS(01)'
        '0'.     "分配比例?固定0.00
  PERFORM BDC_DYNPRO      USING 'SAPLKOBS' '0130'.
  PERFORM BDC_FIELD       USING 'BDC_CURSOR'
        'COBRB-KONTY(01)'.
  PERFORM BDC_FIELD       USING 'BDC_OKCODE'
        '=BACK'.
  PERFORM BDC_DYNPRO      USING 'SAPMSSY0' '0120'.
  PERFORM BDC_FIELD       USING 'BDC_OKCODE'
        '=&ONT'.
  PERFORM BDC_DYNPRO      USING 'SAPLCOKO1' '0115'.
  PERFORM BDC_FIELD       USING 'BDC_OKCODE'
        '=BU'.
  PERFORM BDC_FIELD       USING 'BDC_CURSOR'
        'CAUFVD-GAMNG'.
  PERFORM BDC_FIELD       USING 'CAUFVD-GAMNG'
        P_GAMNG.
  PERFORM BDC_FIELD       USING 'CAUFVD-GLTRP'
        P_GLTRP.
  PERFORM BDC_FIELD       USING 'CAUFVD-GSTRP'
        P_GSTRP.
  PERFORM BDC_FIELD       USING 'CAUFVD-TERKZ'
        '2'.
  PERFORM BDC_FIELD       USING 'CAUFVD-FHORI'
        '000'.
*  PERFORM bdc_transaction USING 'CO01'.

  CALL TRANSACTION 'CO01' USING BDCDATA
        MODE P_MODE
        UPDATE P_UPDA
        MESSAGES INTO MESSTAB.

  LOOP AT MESSTAB WHERE MSGTYP = 'A' OR MSGTYP = 'E'.
  ENDLOOP.

  IF SY-SUBRC EQ 0.
    LOOP AT MESSTAB  WHERE MSGTYP = 'A' OR MSGTYP = 'E'.
      MESSAGE ID     MESSTAB-MSGID
      TYPE   MESSTAB-MSGTYP
      NUMBER MESSTAB-MSGNR
      INTO   LV_MES1
      WITH   MESSTAB-MSGV1 MESSTAB-MSGV2
      MESSTAB-MSGV3 MESSTAB-MSGV4.
      CLEAR:MESSTAB.
    ENDLOOP.
    TT_RETURN-TYPE = 'E'.
    TT_RETURN-MESSAGE = '创建失败!' && LV_MES1.
    APPEND TT_RETURN TO P_TT_RETURN.
    CLEAR:TT_RETURN.
  ELSE.
    LOOP AT MESSTAB WHERE MSGID = 'CO' AND MSGNR = '100'.
      EV_INFNR = MESSTAB-MSGV1+0(10).
    ENDLOOP.
    TT_RETURN-TYPE = 'S'.
    TT_RETURN-MESSAGE = '创建成功' && EV_INFNR.
    APPEND TT_RETURN TO P_TT_RETURN.
    CLEAR:TT_RETURN.
  ENDIF.

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.
  DATA: IT        LIKE AFPO-PSMNG,
        FVAL2(13) TYPE C.
  IF FNAM = 'CAUFVD-GAMNG'.
    CLEAR BDCDATA.
    BDCDATA-FNAM = FNAM.
    BDCDATA-FVAL = FVAL.
    CONDENSE BDCDATA-FVAL NO-GAPS.
    APPEND BDCDATA.
  ELSE.
    CLEAR BDCDATA.

    BDCDATA-FNAM = FNAM.
    BDCDATA-FVAL = FVAL.
    APPEND BDCDATA.
  ENDIF.
ENDFORM.

函数里有个单位转换问题:

SAP中的单位转换有时候弄的有点不顺手。在调用BDC的时候,发现了这个问题。如果输入英文单位,会提示单位在中文中没有创建。但是在表T006和T006A中均能找到此单位。可能是后台配置的问题。至今未找到在哪里可以解决这个问题。经过同事帮助,用这两个函数,解决了在调用BDC的时候出现的单位在中文中没有定义的问题,也可以解决单位显示为英文的问题。在此贴出来,希望对后来者有所帮助。 

可以通过SE37来测试。  
CALL FUNCTION 'CONVERSION_EXIT_CUNIT_OUTPUT'
      EXPORTING
        input      =   “输入的英文单位
      IMPORTING
        output =      “输出的中文单位

还会遇到金额和数量长度问题

往往会出现错误提示:输入的字段比实际字段长。这时我们想明明输入的值和字段字义是一样的,为什么会出现这种错误提示呢?
其实这是做BDC的时候最需要注意的事,因为字段类型虽然一样,但是我们保存到BDCDaTA中的时候,数值等是按照默认长度保存的,但是我们输入的时候需要都这些多余的字符串删除掉,这时我们就需要对数据进行处理,有两种处理方法,一种就是用SHIFT移位操作删除多余的字符串,也可以通过修改子例程BDC_FIELD来实现。作者使用的是去除空格的方式。

还有如果发现不了问题,可以改BDC调试模式:

DATA:P_MODE TYPE CHAR1 VALUE 'N',
     P_UPDA TYPE CHAR1 VALUE 'L'.
*DATA:P_MODE TYPE CHAR1 VALUE 'A',  
*     P_UPDA TYPE CHAR1 VALUE 'A'.

具体可以按F1查看

Addition 1

... MODE mode

Effect

The addition MODE determines the processing mode for processing. mode can be specified as a character-like data object whose content and purpose are shown in the following table. If one of the additions MODE or OPTIONS FROM is not used, the effect is the same as if mode had the content "A".

mode Effect
"A" Processed with displayed screens
"E" Screens displayed only if an error occurs
"N" Processed without displayed screens. If a breakpoint is reached in one of the called transactions, processing is terminated with sy-subrc equal to 1001. The field sy-msgty contains "S", sy-msgid contains "00", sy-msgno contains "344", sy-msgv1 contains "SAPMSSY3", and sy-msgv2 contains "0131".
"P" Processed without displayed screens. If a breakpoint is reached in one of the called transactions, the system branches to the ABAP Debugger.
Others As for "A".


Addition 2

... UPDATE upd

Effect

The addition UPDATE determines the update mode for processing. upd can be specified as a character-like data object whose content and purpose are shown in the following table. If one of the additions UPDATE or OPTIONS FROM is not used, the effect is the same as if upd had the content "A".

upd Effect
"A" Asynchronous update. Updates of called programs are executed in the same way as if the addition AND WAIT were not specified in the statement COMMIT WORK.
"S" Synchronous update. Updates of the called programs are executed in the same way as if the addition AND WAIT were specified in the statement COMMIT WORK.
"L" Local updates. Updates of the called program are executed in the same way as if the statement SET UPDATE TASK LOCAL were executed in the program.
Others As for "A".

 

Note

This option is not available for execution of batch input sessions in batch input. Updates are always synchronous. 

posted @ 2023-04-26 17:00  萧静默  阅读(251)  评论(0编辑  收藏  举报