2011年6月14日 星期二

[Code] Bills of material in the BAPI for create SO


使用 BAPI 建立含 BOM 的銷售訂單,重點在於先使用 BAPI_SALESORDER_SIMULATE 算出所有的 item no, 另外就是呼叫 BAPI_SALESORDER_CREATEFROMDAT2 時要使用 Exporting 參數 int_number_assignment = 'X'

BAPI Define:
DATA: gw_si_header          LIKE bapisdhead,
      gw_si_return          LIKE bapireturn,
      gt_si_items_in        LIKE bapiitemin  OCCURS 0 WITH HEADER LINE,
      gt_si_items_out       LIKE bapiitemex  OCCURS 0 WITH HEADER LINE,
      gt_si_schedules_ex    LIKE bapisdhedu  OCCURS 0 WITH HEADER LINE,
      gt_si_condition_ex    LIKE bapicond    OCCURS 0 WITH HEADER LINE,
      gt_si_partners        LIKE bapipartnr  OCCURS 0 WITH HEADER LINE.

DATA: gw_so_header          LIKE bapisdhd1,
      gw_so_headerx         LIKE bapisdhd1x,
      gv_so_doc_num         TYPE bapivbeln-vbeln,
      gt_so_return2         TYPE bapiret2,
      gt_so_partners        TYPE bapiparnr  OCCURS 0 WITH HEADER LINE,
      gt_so_items_in        TYPE bapisditm  OCCURS 0 WITH HEADER LINE,
      gt_so_items_inx       TYPE bapisditmx OCCURS 0 WITH HEADER LINE,
      gt_so_schedules_in    TYPE bapischdl  OCCURS 0 WITH HEADER LINE,
      gt_so_schedules_inx   TYPE bapischdlx OCCURS 0 WITH HEADER LINE,
      gt_so_return1         TYPE bapiret2   OCCURS 0 WITH HEADER LINE,
      gt_so_conditions_in   TYPE bapicond   OCCURS 0 WITH HEADER LINE,
      gt_so_conditions_inx  TYPE bapicondx  OCCURS 0 WITH HEADER LINE.
GT_ITAB contents so data:
LOOP AT gt_itab WHERE value = 'X' AND
                      rfstk = 'A'.
  lv_count1 = lv_count1 + 1.
  MOVE gt_itab TO lw_itab.
  AT NEW pos_no.
*   Customer
    CLEAR: gt_so_partners, gt_so_partners[],
           gt_si_partners, gt_si_partners[].
    gt_so_partners-partn_role  = 'AG'.
    gt_so_partners-partn_numb  = lw_itab-store_no.
    gt_si_partners-partn_role  = 'AG'.
    gt_si_partners-partn_numb  = lw_itab-store_no.
    APPEND: gt_so_partners, gt_si_partners.
    gt_so_partners-partn_role  = 'WE'.
    gt_si_partners-partn_role  = 'WE'.
    APPEND: gt_so_partners, gt_si_partners.
*   Header
    CLEAR: gw_so_header, gt_so_items_in[], gt_so_schedules_in[],
           gt_so_conditions_in[], gt_so_conditions_inx[],
           gw_si_header, gw_si_return, gt_si_items_in[].
    gw_so_header-doc_type   = lw_itab-fodtype.
    gw_so_header-sales_org  = '1000'.
    gw_so_header-distr_chan = lw_itab-cha_no.
    gw_so_header-division   = lw_itab-sal_no.
    gw_so_header-purch_no_c = lw_itab-pos_no.
    REPLACE ALL OCCURRENCES OF '/' IN lw_itab-pos_date WITH ''.
    gw_so_header-purch_date = lw_itab-pos_date.
    IF lw_itab-fodtype+3(1) EQ 'U' OR
       lw_itab-fodtype+3(1) EQ 'V'.
      gw_so_header-ord_reason = lw_itab-bktype.
    ENDIF.
    MOVE-CORRESPONDING gw_so_header TO gw_si_header.
  ENDAT.

* Items
*-------------------
  CLEAR: gt_so_items_in, gt_so_schedules_in,
         gt_si_items_in, gt_so_schedules_inx.
  gt_so_items_in-itm_number    = lw_itab-item_no.
  gt_so_items_in-po_itm_no     = lw_itab-item_no.
  gt_so_items_in-material      = lw_itab-plu_no.
  gt_so_items_in-target_qty    = lw_itab-pos_qty.
  IF lw_itab-fodtype+3(1) NE 'S'.
    gt_so_items_in-batch       = lw_itab-charg.
  ENDIF.
  gt_so_items_in-plant         = lw_itab-wh_no.
  gt_so_items_in-ship_point    = lw_itab-bk_no.
  MOVE-CORRESPONDING gt_so_items_in TO gt_si_items_in.
  APPEND: gt_so_items_in, gt_si_items_in.

* Prices
  IF lw_itab-fodtype+3(1) = 'T'.  " consign
    gt_so_conditions_in-itm_number = lw_itab-item_no.
    gt_so_conditions_in-cond_type  = 'PR01'.
    gt_so_conditions_in-cond_value = lw_itab-pos_prc.
    gt_so_conditions_in-currency   = 'TWD'.
    APPEND gt_so_conditions_in.

    gt_so_conditions_inx-itm_number = lw_itab-item_no.
    gt_so_conditions_inx-cond_type  = 'PR01'.
    gt_so_conditions_inx-updateflag = 'I'.
    gt_so_conditions_inx-cond_value = 'X'.
    gt_so_conditions_inx-currency   = 'X'.
    APPEND gt_so_conditions_inx.
  ENDIF.

* Scheduling
*-------------------
  gt_so_schedules_in-itm_number   = lw_itab-item_no.
  gt_so_schedules_in-req_qty      = lw_itab-pos_qty.
  APPEND gt_so_schedules_in.

* Creating SO
*-------------------
  AT END OF pos_no.
*
    CLEAR:   gt_si_items_out, gt_si_schedules_ex,
             gv_so_doc_num.
    REFRESH: gt_si_items_out, gt_si_schedules_ex.

    IF lw_itab-fodtype+3(1) EQ 'U'.
      CALL FUNCTION 'BAPI_CUSTOMERRETURN_CREATE'
        EXPORTING
          return_header_in      = gw_so_header
          int_number_assignment = 'X'
        IMPORTING
          salesdocument         = gv_so_doc_num
        TABLES
          return                = gt_so_return1
          return_items_in       = gt_so_items_in
          return_partners       = gt_so_partners
          return_schedules_in   = gt_so_schedules_in.
    ELSE.
      CALL FUNCTION 'BAPI_SALESORDER_SIMULATE'
        EXPORTING
          order_header_in    = gw_si_header
        IMPORTING
          return             = gw_si_return
        TABLES
          order_items_in     = gt_si_items_in
          order_partners     = gt_si_partners
          order_schedule_in  = gt_so_schedules_in
          order_items_out    = gt_si_items_out
          order_schedule_ex  = gt_si_schedules_ex
          order_condition_ex = gt_si_condition_ex.
*
      IF gw_si_return-type = 'E'.
        gt_itab-rfstk = 'B'.
        gt_itab-rmark = gw_si_return-message.
        MODIFY gt_itab TRANSPORTING rfstk rmark
            WHERE pos_no = lw_itab-pos_no.
      ELSE.
        REFRESH: gt_so_items_in,  gt_so_schedules_in,
                 gt_so_items_inx, gt_so_schedules_inx.
        LOOP AT gt_si_items_out.
          IF gt_si_items_out-po_itm_no <> ''.
*           update items
            MOVE-CORRESPONDING gt_si_items_out TO gt_so_items_in.
            gt_so_items_inx-itm_number = gt_so_items_in-itm_number.
            APPEND: gt_so_items_in, gt_so_items_inx.
*           update conditions
            LOOP AT gt_so_conditions_in
                WHERE itm_number = gt_si_items_out-po_itm_no.
              gt_so_conditions_in-itm_number = gt_so_items_in-itm_number.
              MODIFY gt_so_conditions_in.
            ENDLOOP.
            LOOP AT gt_so_conditions_inx
                WHERE itm_number = gt_si_items_out-po_itm_no.
              gt_so_conditions_inx-itm_number = gt_so_items_in-itm_number.
              MODIFY gt_so_conditions_inx.
            ENDLOOP.
          ENDIF.
*         Schedules
          READ TABLE gt_si_schedules_ex
              WITH KEY itm_number = gt_si_items_out-itm_number.
          MOVE-CORRESPONDING gt_si_schedules_ex TO gt_so_schedules_in.
          gt_so_schedules_inx-itm_number = gt_si_schedules_ex-itm_number.
          gt_so_schedules_inx-req_qty    = 'X'.
          APPEND: gt_so_schedules_in, gt_so_schedules_inx.
        ENDLOOP.
*
        CALL FUNCTION 'BAPI_SALESORDER_CREATEFROMDAT2'
          EXPORTING
            order_header_in       = gw_so_header
            int_number_assignment = 'X'
          IMPORTING
            salesdocument         = gv_so_doc_num
          TABLES
            return                = gt_so_return1
            order_items_in        = gt_so_items_in
            order_partners        = gt_so_partners
            order_schedules_in    = gt_so_schedules_in
            order_conditions_in   = gt_so_conditions_in
            order_conditions_inx  = gt_so_conditions_inx.
      ENDIF.
    ENDIF.
  ENDAT.
ENDLOOP.

沒有留言:

張貼留言