I would like to share a simple program that can be used to upload Planned Orders Massively using a txt file.

The program uses BAPI: BAPI_PLANNEDORDER_CREATE.

This is the selection screen of the program:

MASS.jpg

This is the Excel file used to generate the txt file. Note that you should not include the header line in the txt:

MASS2.jpg

This is the ABAP code:

*=======================================================================

*———————————————————————–
*  Consulting2b
*———————————————————————–
*   Program:     ZPLANNEDORDERS
*   Type:        Report
*———————————————————————–
*   Created By:     Mariano Cabalen
*   Creation Date:  MAY 09 2006
*———————————————————————–
*   Description
*   Massive Creation of Planned Orders
*=======================================================================
* Change Log
*———————————————————————–
* DATE         AUTHOR      TRANSPORT#  DESCRIPTION
*———————————————————————–
*
*———————————————————————–

REPORT ZPLANNEDORDERS   NO STANDARD PAGE HEADING
                                   LINE-SIZE 132
                                   LINE-COUNT 65
                                   MESSAGE-ID PP.

************************************************************************
* TYPES and TYPE-POOLS                                                 *
************************************************************************
TYPES: BEGIN OF ty_data,
        pasch   LIKE t460c-pasch,           ” Planned Order Type
        plscn   LIKE plaf-plscn,            ” Planning Version
        matnr   LIKE plaf-matnr,            ” Planning material
        plwrk   LIKE plaf-plwrk,            ” Planning Plant
        pwwrk   LIKE plaf-pwwrk,            ” Production plant in planned order
        gsmng   LIKE plaf-gsmng,            ” Total planned order quantity
        avmng   LIKE plaf-avmng,            ” Fixed quantity of scrap from production
        psttr   LIKE plaf-psttr,            ” Order start date in planned order
        pedtr   LIKE plaf-pedtr,            ” Order finish date in the planned order
        pertr   LIKE plaf-pertr,            ” Planned opening date in planned order
        umskz   LIKE plaf-umskz,            ” Conversion indicator for planned order
        auffx   LIKE plaf-auffx,            ” Firming indicator for planned order data
        verid   LIKE plaf-verid,            ” Production Version
        term1   LIKE tcx00-term1.           ” Lead time scheduling
TYPES: END OF ty_data.

TYPES: BEGIN OF ty_log,
        description(300),
        err.
TYPES: END OF ty_log.

TYPES: BEGIN OF ty_pv_data,
        matnr LIKE mkal-matnr,
        werks LIKE mkal-werks,
        verid LIKE mkal-verid,
        bdatu LIKE mkal-bdatu,
        adatu LIKE mkal-adatu,
        mksp  LIKE mkal-mksp,
       END OF ty_pv_data.

TYPES: tyt_pv_data TYPE TABLE OF ty_pv_data.

**********************************************************************
* CONSTANTS                                                            *
************************************************************************
CONSTANTS: c_x             VALUE ‘X’,
           c_i             VALUE ‘I’,
           c_bt(2)         VALUE ‘BT’,
           c_sep           VALUE ‘-‘,
           c_s             VALUE ‘S’,
           c_61(2)         VALUE ’61’,
           c_010(3)        VALUE ‘010’,
           c_1             LIKE mkal-mksp VALUE ‘1’.

************************************************************************
* Internal Tables                                                      *
************************************************************************
DATA: it_headerdata LIKE bapiplaf_i1 OCCURS 0 WITH HEADER LINE,
      it_data  TYPE STANDARD TABLE OF ty_data WITH HEADER LINE,
      it_log   TYPE STANDARD TABLE OF ty_log  WITH HEADER LINE.

************************************************************************
* DATA Declarations                                                    *
************************************************************************
RANGES: r_qa_valid FOR sy-datum,
        r_pv_valid FOR sy-datum.

************************************************************************
* SELECTION-SCREEN
************************************************************************
SELECTION-SCREEN BEGIN OF BLOCK a1 WITH FRAME TITLE text-001.
PARAMETERS: p_file      LIKE rlgrap-filename.
SELECTION-SCREEN END OF BLOCK a1.

************************************************************************
* AT SELECTION-SCREEN ON VALUE
************************************************************************
AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_file.
  PERFORM f_get_file.

************************************************************************
* AT SELECTION-SCREEN
************************************************************************
*AT SELECTION-SCREEN.

************************************************************************
* AT SELECTION-SCREEN OUTPUT
************************************************************************
*AT SELECTION-SCREEN OUTPUT.

************************************************************************
* START-OF-SELECTION Event                                             *
***********************************************************************
START-OF-SELECTION.
* Upload file and put in internal table
  PERFORM     get_file    TABLES it_data
                          USING  p_file.
** Complete the structure for Header BAPI with fields of file input.
  PERFORM   f_prepare_data TABLES it_headerdata
                                  it_data.

* Create massive planned orders and load log
  PERFORM create_planned_orders TABLES it_headerdata
                                       it_log.

* Show log
  PERFORM   show_log  TABLES it_log.

*———————————————————————*
*       FORM f_get_file                                               *
*———————————————————————*
*       ……..                                                      *
*———————————————————————*
FORM f_get_file.

  CALL FUNCTION ‘KD_GET_FILENAME_ON_F4’
       CHANGING
            file_name     = p_file
       EXCEPTIONS
            mask_too_long = 1
            OTHERS        = 2.

ENDFORM.

*&———————————————————————*
*&      Form  get_file
*&———————————————————————*
*       text
*———————————————————————-*
FORM get_file TABLES   pit_data STRUCTURE  it_data
              USING    p_p_file.

  DATA: l_file TYPE string.

  l_file = p_p_file.
* Upload the field
  CALL FUNCTION ‘GUI_UPLOAD’
       EXPORTING
            filename                = l_file
            filetype                = ‘ASC’
            has_field_separator     = ‘X’
       TABLES
            data_tab                = pit_data
       EXCEPTIONS
            file_open_error         = 1
            file_read_error         = 2
            no_batch                = 3
            gui_refuse_filetransfer = 4
            invalid_type            = 5
            no_authority            = 6
            unknown_error           = 7
            bad_data_format         = 8
            header_not_allowed      = 9
            separator_not_allowed   = 10
            header_too_long         = 11
            unknown_dp_error        = 12
            access_denied           = 13
            dp_out_of_memory        = 14
            disk_full               = 15
            dp_timeout              = 16
            OTHERS                  = 17.

  IF sy-subrc <> 0.
    MESSAGE e000 WITH text-E01.
  ENDIF.

ENDFORM.                    ” get_file

*&———————————————————————*
*&      Form  f_prepare_data
*&———————————————————————*
*       text
*———————————————————————-*
FORM f_prepare_data TABLES   pit_headerdata STRUCTURE it_headerdata
                             pit_data STRUCTURE it_data.
*Prepare header data for Bapi
  LOOP AT it_data.
    CLEAR pit_headerdata.
* Convert material number
    CALL FUNCTION ‘CONVERSION_EXIT_ALPHA_INPUT’
         EXPORTING
              input  = pit_data-matnr
         IMPORTING
              output = pit_headerdata-material.

    pit_headerdata-pldord_profile   = pit_data-pasch.
    pit_headerdata-plng_scenario_lt = pit_data-plscn.
    pit_headerdata-plan_plant       = pit_data-plwrk.
    pit_headerdata-prod_plant       = pit_data-pwwrk.
    pit_headerdata-total_plord_qty  = pit_data-gsmng.
    pit_headerdata-fixed_scrap_qty  = pit_data-avmng.
    pit_headerdata-order_start_date = pit_data-psttr.
    pit_headerdata-order_fin_date   = pit_data-pedtr.
    pit_headerdata-plan_open_date   = pit_data-pertr.
    pit_headerdata-conversion_ind   = pit_data-umskz.
    pit_headerdata-firming_ind      = pit_data-auffx.
    pit_headerdata-version          = pit_data-verid.
    pit_headerdata-det_schedule     = pit_data-term1.
    APPEND pit_headerdata.
  ENDLOOP.

ENDFORM.                    ” f_prepare_data

*&———————————————————————*
*&      Form  create_planned_orders
*&———————————————————————*
*       text
*———————————————————————-*
FORM create_planned_orders TABLES   pit_headerdata STRUCTURE
                                    it_headerdata
                                    pit_log STRUCTURE it_log.

  DATA : wa_headerdata   LIKE bapiplaf_i1,
         lt_return        LIKE TABLE OF bapireturn1 WITH HEADER LINE,
         lt_commit_return LIKE TABLE OF bapiret2 WITH HEADER LINE,
         l_plannedorder  LIKE bapi_pldord-pldord_num,
         l_qty_aux(17).

  DATA : lt_comdata LIKE TABLE OF bapi_pldordcomp_i1 WITH HEADER LINE .

  LOOP AT  pit_headerdata INTO wa_headerdata.

    REFRESH lt_comdata.
    CLEAR lt_comdata.

    l_qty_aux = wa_headerdata-total_plord_qty.

    MOVE wa_headerdata-material TO lt_comdata-material.
    APPEND lt_comdata.

*Create planned order
    CALL FUNCTION ‘BAPI_PLANNEDORDER_CREATE’
         EXPORTING
              headerdata     = wa_headerdata
         IMPORTING
              return         = lt_return
              plannedorder   = l_plannedorder
         TABLES
              componentsdata = lt_comdata.

    IF lt_return-type = c_s AND lt_return-id   = c_61
        AND lt_return-number = c_010.
*If create was successfully run commit
      CALL FUNCTION ‘BAPI_TRANSACTION_COMMIT’
           IMPORTING
                return = lt_commit_return.
*Prepare the log
      CLEAR: pit_log.
      CONCATENATE  lt_return-message c_sep text-007
                   wa_headerdata-material c_sep text-008
                   wa_headerdata-plan_plant c_sep text-009
                   l_qty_aux
              INTO pit_log-description SEPARATED BY space.
      APPEND pit_log.
    ELSE.
      CLEAR: pit_log.
      CONCATENATE  text-007 wa_headerdata-material c_sep text-008
                   wa_headerdata-plan_plant c_sep text-009 l_qty_aux
                   text-010 lt_return-message
              INTO pit_log-description SEPARATED BY space.

      MOVE c_x TO pit_log-err.

      APPEND pit_log.
    ENDIF.
    CLEAR wa_headerdata.
  ENDLOOP.
ENDFORM.                    ” create_planned_orders

*&———————————————————————*
*&      Form  SHOW_LOG
*&———————————————————————*
*       text
*———————————————————————-*
*      –>P_IT_LOG  text
*———————————————————————-*
FORM show_log TABLES   pit_log STRUCTURE it_log.

  DATA: info(20) VALUE ‘Information’.

  WRITE :/5 text-011.
  SKIP 2.
  LOOP AT pit_log.
    IF pit_log-err EQ c_x.
      FORMAT INVERSE ON COLOR 6 .
      WRITE: /5  pit_log-description .
    ELSE.
      FORMAT INVERSE ON COLOR 1.
      WRITE: /5  pit_log-description .
    ENDIF.

  ENDLOOP.

ENDFORM.                    ” SHOW_LOG

*&———————————————————————*
*&      Form  create_date_range
*&———————————————————————*
*       text
*———————————————————————-*
FORM create_date_range TABLES p_date_range STRUCTURE r_qa_valid
                       USING p_date_from
                             p_date_to.

  REFRESH p_date_range.
  CLEAR p_date_range.

  p_date_range-sign = c_i.
  p_date_range-option = c_bt.
  p_date_range-low = p_date_from.
  p_date_range-high = p_date_to.

  APPEND p_date_range.

ENDFORM.                    ” create_date_range

*&———————————————————————*
*&      Form  check_pv
*&———————————————————————*
*       Check product version in MKAL table
*———————————————————————-*
FORM check_pv USING pit_headerdata TYPE bapiplaf_i1
                    p_mod_index.

  DATA: lwa_pv_data TYPE ty_pv_data,
        lt_pv_data TYPE tyt_pv_data.

  DATA: wl_order_date LIKE sy-datum,
        wl_pv_error.

  wl_pv_error = c_x.

  IF it_headerdata-order_start_date IS INITIAL
  OR it_headerdata-order_start_date EQ space.
    IF it_headerdata-order_fin_date IS INITIAL
    OR it_headerdata-order_fin_date EQ space.
      IF it_headerdata-plan_open_date IS INITIAL
      OR it_headerdata-plan_open_date EQ space.
      ELSE.
        wl_order_date = it_headerdata-plan_open_date.
      ENDIF.
    ELSE.
      wl_order_date = it_headerdata-order_fin_date.
    ENDIF.
  ELSE.
    wl_order_date = it_headerdata-order_start_date.
  ENDIF.

  SELECT matnr werks verid bdatu adatu mksp
  FROM mkal
  INTO TABLE lt_pv_data
  WHERE matnr = pit_headerdata-material
    AND werks = pit_headerdata-plan_plant.

  IF sy-subrc = 0.
    LOOP AT lt_pv_data INTO lwa_pv_data.

      PERFORM create_date_range TABLES r_pv_valid
                                USING lwa_pv_data-adatu
                                      lwa_pv_data-bdatu.
      IF wl_order_date IN r_pv_valid.
        IF lwa_pv_data-mksp NE c_1.
          pit_headerdata-version = lwa_pv_data-verid.
          MODIFY it_headerdata FROM pit_headerdata INDEX p_mod_index.
          CLEAR wl_pv_error.
          EXIT.
        ENDIF.
      ENDIF.

    ENDLOOP.
  ENDIF.

  IF wl_pv_error = c_x.
    PERFORM throw_pv_error USING pit_headerdata-material
                                 pit_headerdata-plan_plant.
  ENDIF.

ENDFORM.                    ” check_pv

*&———————————————————————*
*&      Form  throw_pv_error
*&———————————————————————*
*      Throw Prod Version error and atacch it to final log
*———————————————————————-*
FORM throw_pv_error USING p_material
                          p_plan_plant.

  MESSAGE i000 WITH p_material
                            p_plan_plant.
  CLEAR it_log.
  CONCATENATE text-012 p_material
              text-013 p_plan_plant text-016
        INTO it_log-description SEPARATED BY space.

  MOVE c_x TO it_log-err.

  APPEND it_log.

ENDFORM.                    ” throw_pv_error

To report this post you need to login first.

Be the first to leave a comment

You must be Logged on to comment or reply to a post.

Leave a Reply