SAP创建工艺路线BAPI封装为FUNCTION接口

FUNCTION zbapi_capp_create_task_list .
*"----------------------------------------------------------------------
*"*"Local interface:
*"  EXPORTING
*"     VALUE(E_PLNNR) TYPE  PLNNR
*"     VALUE(E_PLNAL) TYPE  PLNAL
*"  TABLES
*"      IT_ROUTING STRUCTURE  ZBAPI_ST_CAPP_ROUTING
*"      ET_GROUP STRUCTURE  ZBAPI_ST_CAPP_GROUP
*"      ET_RESULT STRUCTURE  ZBAPI_ST_CAPP_LOGDETAIL
*"----------------------------------------------------------------------


**********************************************************************
* Types Declarations
**********************************************************************

   TYPES  BEGIN  OF ty_routing ,
               material   LIKE  bapi1012_mtk_c -material , "物料
               plant  LIKE  bapi1012_tsk_c -plant , "工厂
               tsk_desc  LIKE  bapi1012_tsk_c -description , "任务清单描述
               task_list_usage  LIKE  bapi1012_tsk_c -task_list_usage , "任务清单用途-默认为生产
               task_list_status   LIKE  bapi1012_tsk_c -task_list_status , "状态
               ident_key          LIKE  bapi1012_tsk_c -ident_key , "检验点字段合并的标识-100,自由定义检验点
          "Identification for the Inspection Point Field Combination
*          lot_size_from         LIKE  bapi1012_tsk_c-lot_size_from,
*          lot_size_to         LIKE  bapi1012_tsk_c-lot_size_to,
*          task_measure_unit LIKE  bapi1012_tsk_c-task_measure_unit,
               op_activity          LIKE  bapi1012_opr_c -activity , "操作/活动编号
               uvorn ( 4 TYPE  c ,          "子工序编号
*     planning_work_center  LIKE  bapi1012_tsk_c-planning_work_center,
               control_key          LIKE  bapi1012_opr_c -control_key , "控制码
               work_cntr          LIKE  bapi1012_opr_c -work_cntr , "工作中心
               op_description   LIKE  bapi1012_opr_c -description , "工序短文本
* operation_measure_unit  LIKE  bapi1012_opr_c-operation_measure_unit,
               std_value_01  LIKE bapi1012_opr_c -std_value_01 , "标准值,准备工时
               std_value_02  LIKE bapi1012_opr_c -std_value_02 , "机器工时
               std_value_03  LIKE bapi1012_opr_c -std_value_03 , "人工工时
               std_value_04  LIKE bapi1012_opr_c -std_value_04 , "计划周期
               std_value_05  LIKE bapi1012_opr_c -std_value_05 ,
               std_value_06  LIKE bapi1012_opr_c -std_value_06 ,
               std_unit_01  LIKE bapi1012_opr_c -std_unit_01 , "工时标准单位
               std_unit_02  LIKE bapi1012_opr_c -std_unit_02 , "工时标准单位
               std_unit_03  LIKE bapi1012_opr_c -std_unit_03 , "工时标准单位
               std_unit_04  LIKE bapi1012_opr_c -std_unit_04 , "工时标准单位
               std_unit_05  LIKE bapi1012_opr_c -std_unit_05 , "工时标准单位
               std_unit_06  LIKE bapi1012_opr_c -std_unit_06 , "工时标准单位
               vendor_no  LIKE bapi1012_opr_c -vendor_no , "供应商编号
               plnd_delry  LIKE bapi1012_opr_c -plnd_delry , "计划交货天数
               cost_elem  LIKE bapi1012_opr_c -cost_elem , "成本要素-5001040000
               purch_org  LIKE bapi1012_opr_c -purch_org , "采购组织-默认2000
               purch_group  LIKE bapi1012_opr_c -purch_group "采购组-251
               matl_group  LIKE bapi1012_opr_c -matl_group ,    "物料组-908930
               price_unit  LIKE bapi1012_opr_c -price_unit ,    "价格单位-1
                currency  LIKE bapi1012_opr_c - currency , "货币码-CNY
               cost_relevant  LIKE bapi1012_opr_c -cost_relevant , "成本核算标识
               info_rec_net_price  LIKE bapi1012_opr_c -info_rec_net_price , "净价
               ztext  TYPE sldstring , "长文本
*          ins_activity         LIKE bapi1012_cha_c-activity,
*          inspchar         LIKE bapi1012_cha_c-inspchar,
*          quantitative_ind  LIKE  bapi1012_cha_c-quantitative_ind,
               mstr_char          LIKE  bapi1012_cha_c -mstr_char , "主文件检验特性
               quantitative_ind   LIKE  bapi1012_cha_c -quantitative_ind , "数量特性
* cha_master_import_modus LIKE bapi1012_cha_c-cha_master_import_modus,
*          char_descr         LIKE bapi1012_cha_c-char_descr,
                method   LIKE  bapi1012_cha_c - method , "检验方法
               smpl_procedure   LIKE  bapi1012_cha_c -smpl_procedure , "检验特性中的采样程序
*          smpl_unit         LIKE  bapi1012_cha_c-smpl_unit,
*          smpl_quant         LIKE bapi1012_cha_c-smpl_quant,
*          formula_ind        LIKE bapi1012_cha_c-formula_ind,
*          lsl_ind        LIKE bapi1012_cha_c-lw_tol_lmt_ind,
*          usl_ind        LIKE bapi1012_cha_c-up_tol_lmt_ind,
*          tgt_ind        LIKE bapi1012_cha_c-target_val_check_ind,
               summ_rec ( 1 TYPE  c , " '' 汇总记录 ; +  单值记录 ;* 分类记录; - 无结果记录
               sing_rec ( 1 TYPE  c , " '' 汇总记录 ; +  单值记录 ;* 分类记录; - 无结果记录
               target_val          LIKE bapi1012_cha_c -target_val , "一个数量特性的目标值
               lw_tol_lmt          LIKE bapi1012_cha_c -lw_tol_lmt , "规范下限
               up_tol_lmt          LIKE bapi1012_cha_c -up_tol_lmt , "规范上限
               formula1          LIKE bapi1012_cha_c -formula_field_1 , "公式字段
               formula2          LIKE bapi1012_cha_c -formula_field_1 , "公式字段
              END  OF ty_routing .

**********************************************************************
*                Constants Declarations
**********************************************************************

**********************************************************************
* Global variables
**********************************************************************
   DATA :  gv_group        TYPE bapi1012_tsk_c -task_list_group ,
         gv_gr_counter   TYPE  bapi1012_tsk_c -group_counter ,
         gv_row  TYPE n ,
         gv_plant  LIKE bapi1012_tsk_c -plant ,
         gv_lines  TYPE  i .

**********************************************************************
*                Internal Table Declarations
**********************************************************************
* Data Declarations for import internal table parameters to BAPI

   DATA  it_task  TYPE  TABLE  OF bapi1012_tsk_c  WITH  HEADER  LINE ,
         it_mat_task_allo  TYPE  TABLE  OF bapi1012_mtk_c  WITH  HEADER  LINE ,
         it_inspchar  TYPE  TABLE  OF bapi1012_cha_c   WITH  HEADER  LINE ,
         it_op  TYPE  TABLE  OF bapi1012_opr_c   WITH  HEADER  LINE ,
         it_ret  TYPE  TABLE  OF bapiret2  WITH  HEADER  LINE ,
         it_textallocation  TYPE bapi1012_txt_hdr_c  OCCURS  WITH  HEADER  LINE ,
         it_text  TYPE bapi1012_txt_c  OCCURS  WITH  HEADER  LINE ,
          BEGIN  OF li_ltext  OCCURS  0 ,
          ltext  TYPE char255 ,
          END  OF li_ltext .

   DATA str_length  TYPE  VALUE  0 ,
        line_all  TYPE  VALUE  0 ,
        curr_line  TYPE  VALUE  0 ,
        curr_pos  TYPE  VALUE  0 .

* Declare Internal table for Message Header for ALV Display

   DATA BEGIN  OF it_loghead   OCCURS  0 ,
           material  LIKE bapi1012_mtk_c -material ,
           plant  LIKE bapi1012_tsk_c -plant ,
           msgtype  LIKE bapiret2 - type ,
           msg ( 100 TYPE  c ,
           expand  TYPE  c ,
          END  OF it_loghead  .

* Declare Internal table for Message Details for ALV Display

   DATA BEGIN  OF it_logdetail  OCCURS  0 ,
           material  LIKE bapi1012_mtk_c -material ,
           plant  LIKE bapi1012_tsk_c -plant ,
           msgtype  LIKE bapiret2 - type ,
           msg ( 100 TYPE  c ,
          END  OF it_logdetail .

* Internal Table to Load and Hold Data from the Text File

*  DATA : it_routing TYPE TABLE OF ty_routing WITH HEADER LINE.

**********************************************************************
*                Work Area Declarations
**********************************************************************
   DATA  wa_routing  TYPE ty_routing .

   CLEAR :it_loghead[] ,
        it_logdetail[] .

**********************************************************************
*      populate_bapi_tables start
**********************************************************************
* Determine the number of records in the internal table
   DESCRIBE  TABLE it_routing  LINES gv_lines .
   IF gv_lines <>  0 .

     DATA  lv_act ( 4 TYPE n ,
           lv_opr ( 4 TYPE n .
* Loop at internal table to load all data

     LOOP  AT it_routing  WHERE material  IS  NOT  INITIAL .
       CALL  FUNCTION  'CONVERSION_EXIT_ALPHA_INPUT'
         EXPORTING
           input   it_routing -material
         IMPORTING
           output  it_routing -material .
       MODIFY it_routing  TRANSPORTING material .
     ENDLOOP .

*    IF it_routing-ins_activity IS INITIAL AND it_task IS NOT INITIAL.
     IF it_routing[]  IS  NOT  INITIAL  AND it_task[]  IS  NOT  INITIAL .

*   Re-initialise the internal tables
       CLEAR it_mat_task_allo[] .

       CLEAR it_task[] .

       CLEAR it_op[] .

       CLEAR it_inspchar[] .

       CLEAR it_ret[] .

       CLEAR it_textallocation[] .

       CLEAR it_text[] .

     ENDIF .

*   删除已存在的工艺路线分配
     LOOP  AT it_routing .
       AT  END  OF plant .
         CALL  FUNCTION  'ZBAPI_CAPP_DEL_TASK_LIST'
           EXPORTING
            i_matnr  it_routing -material
            i_plant  it_routing -plant .
       ENDAT .
     ENDLOOP .

     LOOP  AT it_routing .

       IF it_routing -plant  IS  NOT  INITIAL .
        gv_plant  it_routing -plant .
       ENDIF .

       IF it_routing -op_activity  IS  NOT  INITIAL .
        lv_opr  it_routing -op_activity .
         CLEAR lv_act .
       ENDIF .

       IF it_routing -plant  NE space .
         CLEAR  lv_act ,
                it_mat_task_allo .

* Moving Corresponding fields to Operation internal table
         CLEAR it_op .
        it_op -valid_from  sy -datum . "有效起始日期,默认为当前时间
        it_op -valid_to_date  '99991231' .
        it_op -activity  lv_opr . "道序号
        it_op -plant  gv_plant .
        it_op -control_key  it_routing -control_key . "控制码
        it_op -work_cntr  =  it_routing -work_cntr . "工作中心

        it_op-description it_routing-op_description."段文本

*工序计量单位,如果已经维护抬头中的计量单位,则将该单位用作工序计量单位的缺省值。

*以更改工序中的计量单位。

*  it_op-operation_measure_unit   = it_routing-operation_measure_unit.
        it_op -operation_measure_unit    it_task -task_measure_unit .

        it_op -denominator   '1' . "用于转换工艺路线和工序单位的分母
        it_op -nominator     '1' . "用于转换任务清单和工序计量单位的计数器
        it_op -base_quantity  '1.000' . "基本数量
*      it_op-insp_point_complt_flow_variant = '3'.
        it_op -free_grid  = 'X' . "间隔无时间或数量参考
        it_op -std_value_01  it_routing -std_value_01 .
        it_op -std_value_02  it_routing -std_value_02 .
        it_op -std_value_03  it_routing -std_value_03 .
        it_op -std_value_04  it_routing -std_value_04 .
        it_op -std_value_05  it_routing -std_value_05 .
        it_op -std_value_06  it_routing -std_value_06 .

        it_op -acttype_01  '300' . "准备工时的活动类型不会自己默认填入????

**********************************************************************
*       附加信息:工时单位,计划交货时间,成本要素,采购组织,物料组,成本核算标识
*       1、工时单位为H小时
*       2、当道序为委外道序时,即控制码为**02,需补充供应商编号,计划交货时间(天),成本要素-5001040000
*          采购组织-2000,采购组-251
*       3、成本核算标识:除PP06外全部需要打上成本核算标识
**********************************************************************
         IF it_routing -std_unit_01  IS  INITIAL .
          it_op -std_unit_01  'H' .
         ELSE .
          it_op -std_unit_01  it_routing -std_unit_01 .
         ENDIF .
         IF it_routing -std_unit_02  IS  INITIAL .
          it_op -std_unit_02  'H' .
         ELSE .
          it_op -std_unit_02  it_routing -std_unit_02 .
         ENDIF .
         IF it_routing -std_unit_03  IS  INITIAL .
          it_op -std_unit_03  'H' .
         ELSE .
          it_op -std_unit_03  it_routing -std_unit_03 .
         ENDIF .
         IF it_routing -std_unit_04  IS  INITIAL .
          it_op -std_unit_04  'H' .
         ELSE .
          it_op -std_unit_04  it_routing -std_unit_04 .
         ENDIF .
         IF it_routing -std_unit_05  IS  INITIAL .
          it_op -std_unit_05  'H' .
         ELSE .
          it_op -std_unit_05  it_routing -std_unit_05 .
         ENDIF .
         IF it_routing -std_unit_06  IS  INITIAL .
          it_op -std_unit_06  'H' .
         ELSE .
          it_op -std_unit_06  it_routing -std_unit_06 .
         ENDIF .

         IF it_op -control_key  'ZQ02'  OR it_op -control_key  'PP02' .
          it_op -vendor_no  it_routing -vendor_no . "供应商编号
          it_op -plnd_delry  it_routing -plnd_delry . "计划交货天数
           IF it_routing -cost_elem  IS  INITIAL .
            it_op -cost_elem  '5001040000' . "成本要素
           ELSE .
            it_op -cost_elem  it_routing -cost_elem . "成本要素
           ENDIF .
           IF it_routing -purch_org  IS  INITIAL .
            it_op -purch_org  '2000' . ""采购组织-默认2000
           ELSE .
            it_op -purch_org  it_routing -purch_org . ""采购组织-默认2000
           ENDIF .
           IF it_routing -purch_group  IS  INITIAL .
            it_op -purch_group  '251' . "采购组,不是采购组织
           ELSE .
            it_op -purch_group  it_routing -purch_group . "采购组,不是采购组织
           ENDIF .
           IF it_routing -matl_group  IS  INITIAL .
            it_op -matl_group  '908930' . "物料组
           ELSE .
            it_op -matl_group  it_routing -matl_group . "物料组
           ENDIF .
           IF it_routing -price_unit  IS  INITIAL .
            it_op -price_unit  '1' . "价格单位
           ELSE .
            it_op -price_unit  it_routing -price_unit . "价格单位
           ENDIF .
           IF it_routing - currency  IS  INITIAL .
            it_op - currency  'CNY' . "货币码
           ELSE .
            it_op - currency  it_routing - currency . "货币码
           ENDIF .

          it_op -info_rec_net_price  it_routing -info_rec_net_price . "净价
         ENDIF .

         IF it_routing -cost_relevant  IS  INITIAL . "成本核算标识.
           IF it_op -control_key  'PP06' .
            it_op -cost_relevant  ' ' . "成本核算标识
           ELSE .
            it_op -cost_relevant  'X' . "成本核算标识
           ENDIF .
         ELSE .
          it_op -cost_relevant  it_routing -cost_relevant .
         ENDIF .

         APPEND it_op .


*长文本导入
        str_length  STRLEN it_routing -ztext  ) .

         IF str_length >  0 .
          curr_line  curr_line +  1 .
           IF it_routing -uvorn  IS  INITIAL .
            it_textallocation -object_type  '13' .
           ELSE .
            it_textallocation -object_type  '14' .
           ENDIF .
          it_textallocation -valid_from  it_op -valid_from .
          it_textallocation -activity  it_routing -op_activity .
          it_textallocation -valid_to_date  '99991231' .
          it_textallocation -sub_activity  it_routing -uvorn .

          it_textallocation -langu  sy -langu .
          it_textallocation -line_from  curr_line .

           CLEAR it_text .
          it_text -format_col  '*' .
          it_text -text_line  it_routing -op_description .
           APPEND it_text .

           REFRESH :li_ltext .
           SPLIT it_routing -ztext  AT  '@'  INTO  TABLE li_ltext .
           LOOP  AT li_ltext .
            curr_line  curr_line +  1 .
             CLEAR it_text .
            it_text -format_col  '*' .
            it_text -text_line   li_ltext -ltext .
             APPEND it_text .
           ENDLOOP .

          it_textallocation -line_to  curr_line .
           APPEND it_textallocation .
         ENDIF .

*在AT NEW OF xxx事件发生后,xxx字段右侧的数字和字符串字段都会被初始化为0和*,
*因此,此处需要重读表头数据到工作区
         MOVE-CORRESPONDING it_routing  TO wa_routing .
         AT  END  OF plant . "以material和plant作为分组条件,在每组的最后一条记录时执行

*工艺路线分配表头中一个工厂下的一个物料只能出现一次,
*否则会出现“不存在有效的工艺路线分配” 的错误
*Moving Corresponding fields to Material internal table
          it_mat_task_allo -material  it_routing -material .
          it_mat_task_allo -plant  it_routing -plant . "gv_plant.
          it_mat_task_allo -valid_from  sy -datum .
          it_mat_task_allo -valid_to_date  '99991231' .
           APPEND it_mat_task_allo .

*工艺路线清单表头中一个工厂下的一个物料只能出现一次,
*否则会出现“不存在有效的工艺路线分配” 的错误
*Moving Corresponding fields to task internal table
           CLEAR it_task .
          it_task -valid_from  sy -datum .
          it_task -valid_to_date  '99991231' .
          it_task -description  wa_routing -tsk_desc .
           IF wa_routing -task_list_usage  IS  INITIAL .
            it_task -task_list_usage  '1' . "任务清单用途:生产-1
           ELSE .
            it_task -task_list_usage  wa_routing -task_list_usage .
           ENDIF .
           IF wa_routing -task_list_status  IS  INITIAL .
            it_task -task_list_status  '4' . "任务清单状态:下达的(通用)-4
           ELSE .
            it_task -task_list_status  wa_routing -task_list_status .
           ENDIF .
          it_task -lot_size_from  '0' .
*      it_task-lot_size_to = it_routing-lot_size_to.
          it_task -lot_size_to  '99999999' .
*      it_task-planning_work_center = it_routing-planning_work_center.
          it_task -plant  it_routing -plant . "gv_plant.
*      it_task-task_measure_unit = it_routing-task_measure_unit.
           SELECT  SINGLE meins  FROM mara  INTO it_task -task_measure_unit
             WHERE matnr  it_routing -material .
           IF wa_routing -ident_key  IS  INITIAL .
            it_task -ident_key  '100' . "自由定义检验点
           ELSE .
            it_task -ident_key  wa_routing -ident_key .
           ENDIF .
           APPEND it_task .

**********************************************************************
*      call_bapi_and_commit start
**********************************************************************
           IF it_task  IS  NOT  INITIAL .

*    SORT it_task BY valid_from.
*    DELETE ADJACENT DUPLICATES FROM it_task COMPARING ALL FIELDS.
*
*    SORT it_mat_task_allo BY material.
*    DELETE ADJACENT DUPLICATES FROM it_mat_task_allo COMPARING ALL FIELDS.


*   Call Subroutine to upload last routing details

* Initialise internal table

             REFRESH it_ret .

* Call Function to Create Routing
             CALL  FUNCTION  'BAPI_ROUTING_CREATE'
*   EXPORTING
*     TESTRUN                      = ' '
*     PROFILE                      =
*     BOMUSAGE                     =
*     APPLICATION                  =
               IMPORTING
                 group                         gv_group
                groupcounter                  gv_gr_counter
               TABLES
                 task                          it_task
                materialtaskallocation        it_mat_task_allo
*     SEQUENCE                     =
                operation                     it_op
*     SUBOPERATION                 =
*     REFERENCEOPERATION           =
*     WORKCENTERREFERENCE          =
*     COMPONENTALLOCATION          =
*     PRODUCTIONRESOURCE           =
                inspcharacteristic            it_inspchar
                textallocation                it_textallocation
                 text                          it_text
                 return                        it_ret .

             READ  TABLE it_ret  INDEX  1 .
             DESCRIBE  TABLE it_ret .

* Check for success message and then Commit, otherwise dont commit
             IF it_ret - type  EQ  'S'  AND sy -tfill  EQ  1 .
               CALL  FUNCTION  'BAPI_TRANSACTION_COMMIT'
*     EXPORTING
*       WAIT          =
*     IMPORTING
*       RETURN        =
                         .

* Populate Success message in internal table for ALV Log display
              it_loghead -plant  gv_plant .
              it_loghead -material  it_mat_task_allo -material .
              it_loghead -msgtype  'S' .
               CONCATENATE  'Routing Created Successfully' gv_group
                 INTO it_loghead -msg  SEPARATED  BY space .
               APPEND it_loghead .

              e_plnnr  gv_group .
              e_plnal  gv_gr_counter .

              et_group -material  it_mat_task_allo -material .
              et_group - group  gv_group .
              et_group -group_counter  gv_gr_counter .
               APPEND et_group .

               "et_result = it_ret.

             ELSE .
               ROLLBACK  WORK .
* Populate Error message in internal table for ALV Log display
              it_loghead -plant  gv_plant .
              it_loghead -material  it_mat_task_allo -material .
              it_loghead -msgtype  'E' .
              it_loghead -msg  'Routing Not Created' .
               APPEND it_loghead .
* Retrieve and populate Error Details for Log Display
               CLEAR it_ret .
               LOOP  AT it_ret .
                it_logdetail -plant  gv_plant .
                it_logdetail -material  it_mat_task_allo -material .
                it_logdetail -msgtype  it_ret - type .
                it_logdetail -msg  it_ret - message .
                 IF it_ret - parameter  IS  NOT  INITIAL .
                   CONCATENATE it_logdetail -msg  '- Error in parameter'
                  it_ret - parameter  INTO it_logdetail -msg  SEPARATED  BY space .
                   IF it_ret -row  IS  NOT  INITIAL .
                    gv_row  it_ret -row .
                     CONCATENATE it_logdetail -msg  '- In Row' gv_row
                     INTO it_logdetail -msg  SEPARATED  BY space .
                   ENDIF .
                 ENDIF .
                 APPEND it_logdetail .
               ENDLOOP .

               REFRESH it_ret .
             ENDIF .
           ENDIF .

           CLEAR :it_mat_task_allo[] ,
                 "sequence[],
                it_task[] ,
                it_op[] ,
                 "suboperation[],
                 "referenceoperation[],
                 "workcenterreference[],
                 "componentallocation[],
                 "productionresource[],
                it_inspchar[] ,
                it_textallocation[] ,
                it_text[] ,
                it_ret[] .

           CLEAR curr_line .
         ENDAT .
         CLEAR wa_routing ,gv_group gv_gr_counter .
**********************************************************************
*      call_bapi_and_commit end
**********************************************************************
     
       ENDIF .
     ENDLOOP .
   ENDIF .

  et_result[]  it_logdetail[] .

**********************************************************************
*      populate_bapi_tables end
**********************************************************************
ENDFUNCTION .
  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值