ABAP:CO01批量创建工单BAPI/CO40计划工单转生产订单BDC

BAPI:BAPI_PRODORD_CREATE

FORM frm_post_data .

  DATA:lt_rows TYPE lvc_t_roid,
       ls_rows TYPE lvc_s_roid.
  DATA:ls_orderdata TYPE bapi_pp_order_create.
  DATA:ls_return       TYPE bapiret2,
       lv_order_number TYPE bapi_order_key-order_number,
       ls_zppt025      TYPE zppt025.

  "获取选中行
  CALL METHOD go_grid->get_selected_rows
    IMPORTING
      et_row_no = lt_rows.
  IF lt_rows IS INITIAL.
    MESSAGE '没有选中行' TYPE 'S' DISPLAY LIKE 'E'.
    RETURN.
  ELSE.

    LOOP AT lt_rows INTO ls_rows.

      READ TABLE gt_data INTO gs_data INDEX ls_rows-row_id.
      IF sy-subrc EQ 0 AND gs_data-icon IS INITIAL.
        CLEAR:ls_orderdata,ls_return.
        "物料编码
        ls_orderdata-material_long = gs_data-matnr.
        "交货工厂
        ls_orderdata-plant = gs_data-werks_jh.
        "订单类型
        ls_orderdata-order_type = gs_data-auart.
        "数量
        ls_orderdata-quantity = gs_data-gamng.
        "订单开始日期
        ls_orderdata-basic_start_date = gs_data-gstrp.
        "订单结束日期
        ls_orderdata-basic_end_date = gs_data-gltrp.

        "调用BAPI创建工单
        CALL FUNCTION 'BAPI_PRODORD_CREATE'
          EXPORTING
            orderdata    = ls_orderdata
          IMPORTING
            return       = ls_return
            order_number = gs_data-aufnr.

        IF ls_return-type = 'E'.
          CALL FUNCTION 'BAPI_TRANSACTION_ROLLBACK'.

          gs_data-icon = icon_red_light.
          gs_data-msg = ls_return-message.

        ELSE.
          CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'
            EXPORTING
              wait = 'X'.

          "工单创建成功后,更新采购订单和行项目
          DO 50 TIMES.
            SELECT COUNT(*)
              FROM aufk
             WHERE aufnr EQ gs_data-aufnr.
            IF sy-subrc EQ 0.
              EXIT.
            ELSE.
              WAIT UP TO '0.3' SECONDS.
            ENDIF.
          ENDDO.

          "更新采购订单和行
          UPDATE aufk SET /zpc1/zglxd = gs_data-/zpc1/zglxd
                          /zpc1/zglhh = gs_data-/zpc1/zglhh
                    WHERE aufnr = gs_data-aufnr.
          IF sy-subrc EQ 0.
            COMMIT WORK AND WAIT.
          ENDIF.

          ls_zppt025 = CORRESPONDING #( gs_data ).

          "更新自建表数据
          MODIFY zppt025 FROM ls_zppt025.
          COMMIT WORK AND WAIT.

          gs_data-icon = icon_green_light.
          gs_data-msg = '工单创建成功'.

        ENDIF.
        MODIFY gt_data FROM gs_data INDEX ls_rows-row_id TRANSPORTING icon msg aufnr.
        CLEAR:gs_data.
      ENDIF.
      CLEAR:ls_rows.
    ENDLOOP.
  ENDIF.
ENDFORM.

 计划工单转生产订单 采用BDC

DATA:   bdcdata LIKE bdcdata    OCCURS WITH HEADER LINE.
DATA:   messtab LIKE bdcmsgcoll OCCURS WITH HEADER LINE.

*
&---------------------------------------------------------------------* *& Form frm_PRODORD_FROM_PLORD *&---------------------------------------------------------------------* *& text *&---------------------------------------------------------------------* *& --> GS_DATA *&---------------------------------------------------------------------* FORM frm_prodord_from_plord USING ps_data TYPE ty_data. DATA: lt_return TYPE TABLE of BAPIRET2. DATA:ls_return TYPE bapiret2. DATA:lv_mode TYPE c. REFRESH : bdcdata, messtab. PERFORM bdc_dynpro USING 'SAPLCOKO1' '0150'. PERFORM bdc_field USING 'BDC_OKCODE' '=ENTK'. PERFORM bdc_field USING 'AFPOD-PLNUM' ps_data-plnum. PERFORM bdc_field USING 'AFPOD-TPAUF' 'X'. PERFORM bdc_field USING 'AUFPAR-PP_AUFART' ps_data-auart. PERFORM bdc_dynpro USING 'SAPLCOUP' '1000'. PERFORM bdc_field USING 'BDC_OKCODE' '/00'. PERFORM bdc_field USING 'ABBT-GSMNG(01)' ps_data-gamng. IF ps_data-gstrp is NOT INITIAL. PERFORM bdc_field USING 'ABBT-PSTTR(01)' ps_data-gstrp. ENDIF. IF ps_data-gltrp is NOT INITIAL. PERFORM bdc_field USING 'ABBT-PEDTR(01)' ps_data-gltrp. ENDIF. PERFORM bdc_dynpro USING 'SAPLCOUP' '1000'. PERFORM bdc_field USING 'BDC_OKCODE' '=GEN'. PERFORM bdc_dynpro USING 'SAPLCOUP' '1000'. PERFORM bdc_field USING 'BDC_OKCODE' '=BU'. lv_mode = 'N'. CALL TRANSACTION 'CO40' USING bdcdata MODE lv_mode UPDATE 'S' MESSAGES INTO messtab. CALL FUNCTION 'CONVERT_BDCMSGCOLL_TO_BAPIRET2' TABLES imt_bdcmsgcoll = messtab ext_return = lt_return. LOOP AT lt_return INTO ls_return WHERE type = 'E' OR TYPE = 'A'. ps_data-zmsg = ps_data-zmsg && ls_return-message. ENDLOOP. IF ps_data-zmsg IS NOT INITIAL. ps_data-type = 'E'. ELSEIF ps_data-zmsg IS INITIAL. READ TABLE lt_return INTO LS_RETURN WITH KEY TYPE = 'S' ID = 'COTU' NUMBER = '014' . IF SY-subrc = 0. ps_data-TYPE = LS_RETURN-TYPE. ps_data-zmsg = LS_RETURN-MESSAGE. ps_data-aufnr = LS_RETURN-message_v2. "工单创建成功后,添加工单的长文本 PERFORM frm_save_text USING ps_data-aufnr ps_data-ZBZZD. ENDIF. ENDIF. ENDFORM.
*----------------------------------------------------------------------*
*        Start new screen                                              *
*----------------------------------------------------------------------*
FORM bdc_dynpro USING program dynpro.
  CLEAR bdcdata.
  bdcdata-program  program.
  bdcdata-dynpro   dynpro.
  bdcdata-dynbegin 'X'.
  APPEND bdcdata.
ENDFORM.


*----------------------------------------------------------------------*
*        Insert field                                                  *
*----------------------------------------------------------------------*
FORM bdc_field USING fnam fval.
  " IF FVAL <> NODATA.
  CLEAR bdcdata.
  bdcdata-fnam = fnam.

  bdcdata-fval = fval.
  IF FNAM 'ABBT-GSMNG(01)'.
    CONDENSE bdcdata-fval.
  ENDIF.
  APPEND bdcdata.
  "  ENDIF.
ENDFORM.


*&---------------------------------------------------------------------*
*& Form frm_save_text
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
*&      --> PS_DATA_AUFNR
*&---------------------------------------------------------------------*
FORM frm_save_text  USING    pv_aufnr pv_zbzzd.

  DATA: lv_tdname TYPE THEAD-TDNAME,
        lt_lines  TYPE TABLE of TLINE,
        ls_lines  TYPE tline.

  DATA:LV_ZBZZD TYPE char300.

  CHECK pv_zbzzd is NOT INITIAL.

  LV_ZBZZD = pv_zbzzd.

  lv_tdname = sy-mandt && pv_aufnr.

  WHILE LV_ZBZZD <> '' .
    CLEAR ls_lineS .
    ls_lineS-tdformat '*'.
    ls_lineS-tdline = LV_ZBZZD+0(132).
    APPEND ls_lineS TO lt_lineS .
    LV_ZBZZD  = LV_ZBZZD+132(132.
  ENDWHILE .


  WAIT UP TO '0.5' SECONDS.

  CALL FUNCTION 'CREATE_TEXT'
  EXPORTING
    fid       'KOPF'
    flanguage = sy-langu
    fname     = lv_tdname
    fobject   'AUFK'
    SAVE_DIRECT 'X'
  TABLES
    flines    = lt_lines
  EXCEPTIONS
    no_init   1
    no_save   2
    OTHERS    3.

  IF sy-subrc 0.
    "SAP bug,需暴力更新值才能CO03显示长文本
    UPDATE aufk SET ltext = sy-langu WHERE aufnr EQ pv_aufnr "CO03 显示长文本.
  ENDIF.

ENDFORM.


 

posted @ 2024-01-19 13:28  阿胖的阿多  阅读(392)  评论(0编辑  收藏  举报