随笔 - 14, 文章 - 2, 评论 - 7
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

工艺路线的导入程序

Posted on 2007-09-29 10:09  wiele  阅读(8790)  评论(2编辑  收藏
这是项目里刚用到的一个工艺路线导入程序,做了一些修改。这个程序相较于我在很多博客中见到的类似程序在导入过程中添加了很多的判定,并能将旧版本的工艺路线自动判定并删除。贴在这里供大家共享。

*&---------------------------------------------------------------------*
*& Report  ZPPPR002
*&
*----------------------------------------------------------------------*
* Identification
*   Modul-Name      : ZPPPR002
*   Author          : Samuel Liu Atos Origin    Modified by:wiele
*   Creation date   : 17.05.2006                Modified on:17.09.2007
*   Owner           : CO module
*   Dev. under SAP  : ECC6.0
*   Description     : 导入工艺路线主数##
*----------------------------------------------------------------------*
REPORT  zpppr002.
************************************************************************
*DDIC
************************************************************************
TABLES:mara,makt,mapl.
************************************************************************
*DATA
*Modified: add group,group_counter into wa_stru.
************************************************************************
TYPES: BEGIN OF ws_stru,
       matnr(018)             TYPE c,"物料编码
       werks(004)             TYPE c,"工厂
       group(008)             TYPE c,"任务清单组码
       groupcounter(002)      TYPE c,"组计数器

*       task_list_usage(003)   TYPE c,"用##
*       task_list_status(003)  TYPE c,"状##
       activity(004)          TYPE c,"作业序号
       control_key(004)       TYPE c,"控制##
       work_cntr(008)         TYPE c,"工作中心
*      STANDARD_TEXT_KEY(007) TYPE C,"标准文本##
       description(40)        TYPE c,"工序描述
       base_quantity(13)      TYPE c,"基本数量
       prepare(009)           TYPE c,"准备时间
       labor(009)             TYPE c,"人工工时-排产时间
       machine(009)           TYPE c,"机器工时-能力时间
       power(009)             TYPE c,"动力
       maintain(009)          TYPE c,"维修
       other(009)             TYPE c,"其他
       split(001)             TYPE c,"所需分解
       split_num(003)         TYPE c,"分解数
       keyword(007)           TYPE c,"字段码
       workshop(20)           TYPE c,"车间
       class1(20)             TYPE c,"班组1
       class2(10)             TYPE c,"班组2
       class3(10)             TYPE c,"班组3
       convers(13)            TYPE c,"转换率


*       sorted_by(010)         TYPE c,"无库存信息记录的排序字符串
*       cost_elem(010)         TYPE c,"成本要素
*       purch_org(004)         TYPE c,"采购组织
*       purch_group(003)       TYPE c,"外协加工的采购组
*       matl_group(009)        TYPE c,"物料组

   END OF ws_stru.
DATA: wt_stru TYPE ws_stru,
      wt_itab TYPE TABLE OF ws_stru WITH HEADER LINE.

DATA: wv_string TYPE string.
DATA: BEGIN OF wt_out OCCURS 0,
         line(600),
      END OF wt_out.
*创建BAPI
DATA: BEGIN OF task OCCURS 0.
        INCLUDE STRUCTURE bapi1012_tsk_c.
DATA: END OF task.

DATA: BEGIN OF materialtaskallocation OCCURS 0.
        INCLUDE STRUCTURE bapi1012_mtk_c.
DATA: END OF materialtaskallocation.

DATA: BEGIN OF sequence OCCURS 0.
        INCLUDE STRUCTURE bapi1012_seq_c.
DATA: END OF sequence.

DATA: BEGIN OF operation OCCURS 0.
        INCLUDE STRUCTURE bapi1012_opr_c.
DATA: END OF operation.

DATA: BEGIN OF componentallocation OCCURS 0.
        INCLUDE STRUCTURE bapi1012_com_c.
DATA: END OF componentallocation.

DATA: BEGIN OF return OCCURS 0.
        INCLUDE STRUCTURE bapiret2.
DATA: END OF return.

DATA: group TYPE bapi1012_tsk_c-task_list_group,
      groupcounter TYPE bapi1012_tsk_c-group_counter,
      testrun TYPE bapiflag,
      profile LIKE bapi1012_control_data-profile,
      bomusage LIKE bapi1012_control_data-bom_usage,
      application LIKE bapi1012_control_data-application.
DATA: line TYPE i.
DATA:   bdcdata LIKE bdcdata    OCCURS 0 WITH HEADER LINE.
DATA:   messtab LIKE bdcmsgcoll OCCURS 0 WITH HEADER LINE.
************************************************************************
* Selection Screen
************************************************************************
SELECTION-SCREEN BEGIN OF BLOCK block3 WITH FRAME TITLE text-b02.
PARAMETER:  p_file LIKE konsparam-pcu40_file OBLIGATORY,
            begcol TYPE i DEFAULT 1 NO-DISPLAY,
            begrow TYPE i DEFAULT 1 NO-DISPLAY,
            endcol TYPE i DEFAULT 100 NO-DISPLAY,
            endrow TYPE i DEFAULT 32000 NO-DISPLAY.
SELECTION-SCREEN END OF BLOCK block3.
************************************************************************
* At Selection Screen
************************************************************************
AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_file.
*获得导入数据的文本名
  PERFORM get_file.

TOP-OF-PAGE.
************************************************************************
* Start of Selection
************************************************************************
START-OF-SELECTION.

* 获取数据从EXCEL文件
  PERFORM get_txt.       "Upload data into itab
* 创建工艺路线
  PERFORM bapi_create TABLES wt_itab.

END-OF-SELECTION.

*&---------------------------------------------------------------------*
*&      Form  GET_TXT
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM get_txt .
  DATA: BEGIN OF wlt_intab OCCURS 0.
          INCLUDE STRUCTURE  alsmex_tabline.
  DATA: END OF wlt_intab.
* No of columns
*  DATA: BEGIN OF WLT_EXTAB OCCURS 0,
*            VALUE_0001(50),
*            VALUE_0002(50),
*            VALUE_0003(50),
*            VALUE_0004(50),
*            VALUE_0005(50),
*            VALUE_0006(50),
*            VALUE_0007(50),
*            VALUE_0008(50),
*            VALUE_0009(50),
*            VALUE_0010(50),
*            VALUE_0011(50),
*            VALUE_0012(50),
*            VALUE_0013(50),
*        END OF WLT_EXTAB.
*
*  DATA: ZWFELD(20).
*  FIELD-SYMBOLS: <FS1>.
*  DATA: TIND(4) TYPE N.
*
**接收excel文件
*  CALL FUNCTION 'ALSM_EXCEL_TO_INTERNAL_TABLE'
*    EXPORTING
*      FILENAME                = P_FILE
*      I_BEGIN_COL             = BEGCOL
*      I_BEGIN_ROW             = BEGROW
*      I_END_COL               = ENDCOL
*      I_END_ROW               = ENDROW
*    TABLES
*      INTERN                  = WLT_INTAB
*    EXCEPTIONS
*      INCONSISTENT_PARAMETERS = 1
*      UPLOAD_OLE              = 2
*      OTHERS                  = 3.
*
*  IF SY-SUBRC <> 0.
*
*    WV_STRING = '上载数据出错'.
**    perform record_log using WV_String.
*
*  ENDIF.
*
*  SORT WLT_INTAB BY ROW COL.
*
*  LOOP AT WLT_INTAB.
*    TIND = WLT_INTAB-COL.
*    CONCATENATE 'WLT_EXTAB-VALUE_' TIND INTO ZWFELD.
*    ASSIGN (ZWFELD) TO <FS1>.
*    <FS1> = WLT_INTAB-VALUE.
*    AT END OF ROW.
*      APPEND WLT_EXTAB.
*      CLEAR  WLT_EXTAB.
*    ENDAT.
*  ENDLOOP.
**把数据赋##
*  LOOP AT WLT_EXTAB.
*
*    WT_ITAB-MATNR            = WLT_EXTAB-VALUE_0001."工厂
*    WT_ITAB-WERKS            = WLT_EXTAB-VALUE_0002."物料编码
*    WT_ITAB-TASK_LIST_USAGE  = WLT_EXTAB-VALUE_0003."用##
*    WT_ITAB-TASK_LIST_STATUS = WLT_EXTAB-VALUE_0004."状##
*
*    WT_ITAB-ACTIVITY         = WLT_EXTAB-VALUE_0005."工序序号
*    WT_ITAB-WORK_CNTR        = WLT_EXTAB-VALUE_0006."工作中心
*    WT_ITAB-CONTROL_KEY      = WLT_EXTAB-VALUE_0007."控制##
*    WT_ITAB-DESCRIPTION      = WLT_EXTAB-VALUE_0008."工序描述
*    WT_ITAB-BASE_QUANTITY    = WLT_EXTAB-VALUE_0009."基本数量
*    WT_ITAB-MACHINE          = WLT_EXTAB-VALUE_0010."机器工时
*    WT_ITAB-LABOR            = WLT_EXTAB-VALUE_0011."人工工时
*
*    APPEND WT_ITAB.
*  ENDLOOP.

  CALL FUNCTION 'UPLOAD'
    EXPORTING
      filename                = p_file
      filetype                = 'DAT'
    TABLES
      data_tab                = wt_itab
    EXCEPTIONS
      conversion_error        = 1
      invalid_table_width     = 2
      invalid_type            = 3
      no_batch                = 4
      unknown_error           = 5
      gui_refuse_filetransfer = 6
      OTHERS                  = 7.

  IF sy-subrc <> 0.
    WRITE:/ ,'上载数据出错'.
  ENDIF.

*按工厂与物料排序
  SORT wt_itab BY matnr werks.

ENDFORM.                    " GET_TXT
*&---------------------------------------------------------------------*
*&      Form  GET_FILE
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM get_file .
  CALL FUNCTION 'WS_FILENAME_GET'             "Get file name
            EXPORTING
*            DEF_FILENAME     = ' '
*            DEF_PATH         = ' '
                mask             = ',*.* ,*.*.'
                mode             = '0'
                title            = 'Get File name'
           IMPORTING
                filename         = p_file
*            RC               =
           EXCEPTIONS
                inv_winsys       = 1
                no_batch         = 2
                selection_cancel = 3
                selection_error  = 4
                OTHERS           = 5.
  IF sy-subrc <> 0.
*            MESSAGE E016 WITH P_PATH.
  ENDIF.

ENDFORM.                    " GET_FILE
*&---------------------------------------------------------------------*
*&      Form  GET_DATA
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*      -->P_WT_ITAB  text
*----------------------------------------------------------------------*
FORM bapi_create  TABLES   p_wt_itab STRUCTURE wt_itab.
  DATA: unit LIKE mara-meins,
        maktx LIKE makt-maktx,
        matnr LIKE mara-matnr.


  LOOP AT p_wt_itab.

    AT NEW werks.
      CLEAR: unit,maktx,matnr.

      FREE: task,materialtaskallocation,sequence,operation.

      SELECT SINGLE meins matnr
      INTO (unit, matnr)
      FROM mara
      WHERE matnr = p_wt_itab-matnr.

      SELECT SINGLE maktx
      INTO maktx
      FROM makt
      WHERE matnr = p_wt_itab-matnr
            AND spras = '1'.

      READ TABLE p_wt_itab INDEX sy-tabix.

      task-valid_from            = sy-datum.
      task-valid_to_date         = '99991231'.
      task-task_list_group       = p_wt_itab-group.
      task-group_counter         = p_wt_itab-groupcounter.
      task-task_list_usage       = '1'.
      task-plant                 = p_wt_itab-werks.
      task-task_list_status      = '4'.
      task-lot_size_from         = 0.
      task-lot_size_to           = 999999999.
      task-task_measure_unit     = unit.
      task-task_measure_unit_iso = unit.
      task-description           = maktx.
      APPEND task.


      materialtaskallocation-plant         = p_wt_itab-werks.
      materialtaskallocation-valid_to_date = '99991231'.
      materialtaskallocation-valid_from    = sy-datum.
      materialtaskallocation-material      = p_wt_itab-matnr.
      APPEND materialtaskallocation.

      sequence-sequence_no                  = '000000'.
      sequence-valid_from                   = sy-datum.
      sequence-valid_to_date                = '99991231'.
      sequence-sequence_category            = '0'.
      sequence-description                  = '标准工序'.
      sequence-lot_sz_min                   = 0.
      sequence-lot_sz_max                   = 999999999.
      sequence-alignment_key_for_scheduling = '2'.
      sequence-task_measure_unit            = unit.
      sequence-task_measure_unit_iso        = unit.
      APPEND sequence.

    ENDAT.

    operation-sequence_no = '000000'.
    operation-valid_from = sy-datum.
    operation-valid_to_date = '99991231'.
    line = STRLEN( p_wt_itab-activity ).
    CASE line.
      WHEN 1.
        CONCATENATE '000' p_wt_itab-activity
                     INTO p_wt_itab-activity.
      WHEN 2.
        CONCATENATE '00' p_wt_itab-activity
                     INTO p_wt_itab-activity.
      WHEN 3.
        CONCATENATE '0' p_wt_itab-activity
                     INTO p_wt_itab-activity.
    ENDCASE.

    operation-activity          = p_wt_itab-activity.
*     OPERATION-OPERATION_ID = F_DATA_TAB-OPERATION.
    operation-control_key       = p_wt_itab-control_key.
*    OPERATION-STANDARD_TEXT_KEY = P_WT_ITAB-STANDTEXT.
    operation-work_cntr         = p_wt_itab-work_cntr.
    operation-plant             = p_wt_itab-werks.
    operation-description       = p_wt_itab-description.
    operation-denominator       = 1.
    operation-nominator         = 1.
    operation-cost_relevant     = 'X'.
    operation-base_quantity     = p_wt_itab-base_quantity.
    operation-std_value_01      = p_wt_itab-prepare.
    operation-std_value_02      = p_wt_itab-labor.
    operation-std_value_03      = p_wt_itab-machine.
    operation-std_value_04      = p_wt_itab-power.
    operation-std_value_05      = p_wt_itab-maintain.
    operation-std_value_06      = p_wt_itab-other.
    operation-ind_splitting_reqrd = p_wt_itab-split.
    operation-max_no_of_splits  = p_wt_itab-split_num.
    operation-userfields_keyword_id = p_wt_itab-keyword.
    operation-userfield_ch20_00 = p_wt_itab-workshop.
    operation-userfield_ch20_01 = p_wt_itab-class1.
    operation-userfield_ch10_02 = p_wt_itab-class2.
    operation-userfield_ch10_03 = p_wt_itab-class3.
    operation-userfield_quan_04 = p_wt_itab-convers.


*   OPERATION-ACTTYPE_02 = '1420'.
*   OPERATION-ACTTYPE_03 = '1421'.
*    IF operation-control_key       = 'PP02'.
*
*      operation-sorted_by   = p_wt_itab-sorted_by. "无库存信息记录的排序字符串
*      operation-cost_elem   = p_wt_itab-cost_elem.      "成本要素
*      operation-purch_org   = p_wt_itab-purch_org. "采购组织
*      operation-purch_group = p_wt_itab-purch_group. "外协加工的采购组
*      operation-matl_group  = p_wt_itab-matl_group. "物料组
*
*    ENDIF.

    APPEND operation.
    CLEAR operation.

    AT END OF werks.

      PERFORM delete_routing USING p_wt_itab-matnr p_wt_itab-werks p_wt_itab-groupcounter .

      PERFORM creat_routing USING p_wt_itab-matnr p_wt_itab-werks .
    ENDAT.


  ENDLOOP.
ENDFORM.                    " GET_DATA
*&---------------------------------------------------------------------*
*&      Form  CREAT_ROUTING
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM creat_routing USING  matnr
                          werks.
  CALL FUNCTION 'BAPI_ROUTING_CREATE'
    EXPORTING
      testrun                = testrun
      profile                = profile
      bomusage               = bomusage
      application            = application
    IMPORTING
      group                  = group
      groupcounter           = groupcounter
    TABLES
      task                   = task
      materialtaskallocation = materialtaskallocation
      sequence               = sequence
      operation              = operation
      componentallocation    = componentallocation
      return                 = return.
  COMMIT WORK.
  DESCRIBE TABLE return LINES line.
  IF line = 1.
    READ TABLE return INDEX 1.
    IF return-type = 'S' AND return-id = 'BAPI' AND
       return-number = '000'.
      WRITE:/ matnr,return-message_v2,'SUCCEED'.
    ELSE.
      WRITE:/ matnr,'Fail,Please check the material'.
    ENDIF.
  ELSE.
    WRITE:/ matnr COLOR COL_NEGATIVE,
           'Fail,Please check the material',
          / return-fields.
  ENDIF.
ENDFORM.                    " CREAT_ROUTING

*&---------------------------------------------------------------------*
*&      Form  DELETE_ROUTING
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM delete_routing USING matnr
                    werks
                    groupcounter.
  CLEAR  bdcdata.
  REFRESH bdcdata.

  CLEAR messtab.
  REFRESH messtab.

  SELECT SINGLE * FROM mapl WHERE matnr = matnr AND werks = werks AND plnal = groupcounter
                                 AND loekz <> 'X'  AND plnty = 'N'.
  IF sy-subrc = 0.
    PERFORM bdc_dynpro      USING 'SAPLCPDI' '1010'.
    PERFORM bdc_field       USING 'BDC_OKCODE' '=ALUE'.
    PERFORM bdc_field       USING 'RC27M-MATNR' matnr.
    PERFORM bdc_field       USING 'RC27M-WERKS' werks.
    PERFORM bdc_field       USING 'RC271-PLNNR' mapl-plnnr.

    PERFORM bdc_dynpro      USING 'SAPLCPDI' '1200'.
    PERFORM bdc_field       USING 'BDC_OKCODE' '=LOE'.
    PERFORM bdc_field       USING 'RC27X-ENTRY_ACT' '1'.

    PERFORM bdc_dynpro      USING 'SAPLSPO1' '0100'.
    PERFORM bdc_field       USING 'BDC_OKCODE' '=YES'.

    PERFORM bdc_dynpro      USING 'SAPLCPDI' '1200'.
    PERFORM bdc_field       USING 'BDC_OKCODE' '=BU'.

    CALL TRANSACTION 'CA02' USING bdcdata
                   MODE   'N'
                   UPDATE 'L'
                   MESSAGES INTO messtab.
    IF sy-subrc <> 0.
      WRITE:/ matnr COLOR COL_NEGATIVE, 'have not deleted'.
    ENDIF.
  ENDIF.
ENDFORM.                    " DELETE_ROUTING
*----------------------------------------------------------------------*
*        Start new screen                                              *
*----------------------------------------------------------------------*
FORM bdc_dynpro USING program dynpro.
  CLEAR bdcdata.
  bdcdata-program  = program.
  bdcdata-dynpro   = dynpro.
  bdcdata-dynbegin = 'X'.
  APPEND bdcdata.
ENDFORM.                    "BDC_DYNPRO
*----------------------------------------------------------------------*
*        Insert field                                                  *
*----------------------------------------------------------------------*
FORM bdc_field USING fnam fval.
  CLEAR bdcdata.
  bdcdata-fnam = fnam.
  bdcdata-fval = fval.
  APPEND bdcdata.
ENDFORM.                    "BDC_FIELD