ABAP-复制采购订单行项目到新的行 原行数量递减 新行更新数量和价格

FUNCTION zmm_fm_copy2new.
*"----------------------------------------------------------------------
*"*"本地接口:
*"  IMPORTING
*"     VALUE(IN_EBELN) TYPE  EBELN
*"     VALUE(IN_EBELP) TYPE  EBELP
*"     VALUE(IN_QUANTITY) TYPE  BSTMG
*"     VALUE(IN_PRICE) TYPE  BPREI
*"  EXPORTING
*"     VALUE(STATUS) TYPE  CHAR1
*"     VALUE(OUT_EBELP) TYPE  EBELP
*"     REFERENCE(OUT_MESSAGE) TYPE  CHAR200
*"----------------------------------------------------------------------

  DATA
:gs_poheader           TYPE                   bapimepoheader,
       gs_poheaderx          TYPE                   bapimepoheaderx,
       gt_return             TYPE STANDARD TABLE OF bapiret2,
       gwa_return            TYPE                   bapiret2,
       gt_item               TYPE STANDARD TABLE OF bapimepoitem,
       gwa_item              TYPE                   bapimepoitem,
       lwa_item              TYPE                   bapimepoitem,
       gt_itemx              TYPE STANDARD TABLE OF bapimepoitemx,
       gwa_itemx             TYPE                   bapimepoitemx,
       gt_poschedule         TYPE STANDARD TABLE OF bapimeposchedule,
       gwa_poschedule        TYPE                   bapimeposchedule,
       lwa_poschedule        TYPE                   bapimeposchedule,
       gt_poschedulex        TYPE STANDARD TABLE OF bapimeposchedulx,
       gwa_poschedulex       TYPE                   bapimeposchedulx,
       gt_pocond             TYPE STANDARD TABLE OF bapimepocond,
       gwa_pocond            TYPE                   bapimepocond,
       lwa_pocond            TYPE                   bapimepocond,
       gt_pocondx            TYPE STANDARD TABLE OF bapimepocondx,
       gwa_pocondx           TYPE                   bapimepocondx,
       gt_pocomponents       TYPE STANDARD TABLE OF bapimepocomponent,
       gwa_pocomponents      TYPE                   bapimepocomponent,
       gwa_pocomponents_temp TYPE                   bapimepocomponent,
       gt_pocomponentsx      TYPE STANDARD TABLE OF bapimepocomponentx,
       gwa_pocomponentsx     TYPE                   bapimepocomponentx.
  DATA:lv_num TYPE bapimepoheader-po_number.

  CALL FUNCTION 'BAPI_PO_GETDETAIL1'
    EXPORTING
      purchaseorder = in_ebeln
    IMPORTING
      poheader      = gs_poheader
    TABLES
      poitem        = gt_item
      poschedule    = gt_poschedule
      pocond        = gt_pocond
      return        = gt_return.

  gs_poheaderx-doc_type = 'X'.
  gs_poheaderx-vendor = 'X'.
  gs_poheaderx-purch_org = 'X'.
  gs_poheaderx-pur_group = 'X'.
  gs_poheaderx-comp_code = 'X'.
  gs_poheaderx-doc_date = 'X'.
  gs_poheaderx-langu = 'X'.
  gs_poheaderx-currency = 'X'."货币
  gs_poheaderx-status = 'X'."货币



  SORT gt_item BY po_item DESCENDING."按行项目从高到低排序
  DATA:new_po_item TYPE ebelp.
  READ TABLE gt_item INTO DATA(wa) INDEX 1."获取最大行项目值
  IF sy-subrc EQ 0.
    new_po_item = wa-po_item + 10."产生新行号
  ENDIF.

  LOOP AT gt_item INTO gwa_item WHERE po_item EQ in_ebelp.
    MOVE-CORRESPONDING gwa_item TO lwa_item.
    lwa_item-po_item = new_po_item.
    lwa_item-net_price = in_price.
    lwa_item-quantity = in_quantity.
    APPEND lwa_item TO gt_item.

    gwa_item-quantity = gwa_item-quantity - in_quantity.
    MODIFY TABLE gt_item FROM gwa_item.

    gwa_itemx-po_item = new_po_item. "采购凭证的项目编号
    gwa_itemx-po_itemx = 'X'. "采购凭证的项目编号
    gwa_itemx-material = 'X'.
    gwa_itemx-net_price = 'X'.
    gwa_itemx-short_text = 'X'."短文本
    gwa_itemx-quantity = 'X'."采购订单数量
    gwa_itemx-po_unit = 'X' ."采购订单的计量单位
    gwa_itemx-plant = 'X'."工厂
    gwa_itemx-stge_loc = 'X'. "库存地点
    gwa_itemx-item_cat = 'X'.
    gwa_itemx-tax_code = 'X'.
    gwa_itemx-date_qty_fixed = 'X'.
    gwa_itemx-unlimited_dlv = 'X'.

    APPEND gwa_itemx TO gt_itemx.

    READ TABLE gt_itemx INTO DATA(wax) WITH KEY po_item = in_ebelp.
    IF sy-subrc EQ 0.
      wax-quantity = 'X'.
      MODIFY TABLE gt_itemx FROM wax.
      FREE wax.
    ELSE.
      wax-po_item = in_ebelp.
      wax-quantity = 'X'.
      APPEND wax TO gt_itemx.
      FREE wax.
    ENDIF.

  ENDLOOP.

  DELETE gt_item WHERE po_item GT in_ebelp AND po_item LT new_po_item .

  LOOP AT gt_poschedule INTO gwa_poschedule WHERE po_item EQ in_ebelp.
    MOVE-CORRESPONDING gwa_poschedule TO lwa_poschedule.
    lwa_poschedule-po_item = new_po_item.
    lwa_poschedule-quantity = in_quantity.
    lwa_poschedule-delivery_date = sy-datum+0(4) &&'.'&& sy-datum+4(2) &&'.' && sy-datum+6(2).
    lwa_poschedule-stat_date = sy-datum.
    lwa_poschedule-po_date = sy-datum.
    APPEND lwa_poschedule TO gt_poschedule.

    gwa_poschedule-quantity = gwa_poschedule-quantity - in_quantity.
    MODIFY TABLE gt_poschedule FROM gwa_poschedule.

    gwa_poschedulex-po_item = new_po_item. "采购凭证的项目编号
    gwa_poschedulex-sched_line = 0001. "采购凭证的项目编号
    gwa_poschedulex-del_datcat_ext = 'X'."交货日期的类别
    gwa_poschedulex-delivery_date = 'X'."交货日期
    gwa_poschedulex-quantity = 'X'."采购订单数量
    gwa_poschedulex-po_itemx = 'X'."采购订单数量

    APPEND gwa_poschedulex TO gt_poschedulex.

    READ TABLE gt_poschedulex INTO DATA(wax1) WITH KEY po_item = in_ebelp.
    IF  sy-subrc EQ 0.
      wax-quantity = 'X'.
      wax-po_itemx = 'X'.
      MODIFY TABLE gt_poschedulex FROM wax1.
      FREE wax1.
    ELSE.
      wax1-quantity = 'X'.
      wax1-po_item = in_ebelp.
      wax1-po_itemx = 'X'.
      wax1-sched_line = 0001.
      APPEND wax1 TO gt_poschedulex.
    ENDIF.

  ENDLOOP.

  DELETE gt_poschedule WHERE po_item GT in_ebelp AND po_item LT new_po_item .

  LOOP AT gt_pocond INTO gwa_pocond WHERE itm_number EQ in_ebelp.
    MOVE-CORRESPONDING gwa_pocond TO lwa_pocond.
    CLEAR lwa_pocond-cond_no.
    lwa_pocond-itm_number = new_po_item.
    IF lwa_pocond-cond_type EQ 'PBXX'.
      lwa_pocond-cond_p_unt = 1.
      lwa_pocond-cond_value = in_price.
    ENDIF.

    lwa_pocond-change_id = 'U'."增加新行,条件不需要用“I”,只需要用“U”即可,要不会出现新的无用条件类型
    lwa_pocond-conpricdat = sy-datum.
    lwa_pocond-conbaseval = in_quantity.
    APPEND lwa_pocond TO gt_pocond.

    gwa_pocondx-itm_number = new_po_item.
    gwa_pocondx-itm_numberx = 'X'.     gwa_pocondx
-cond_st_no = lwa_pocond-cond_st_no.     gwa_pocondx

-cond_type = 'X'.     gwa_pocondx
-cond_value = 'X'.     gwa_pocondx
-cond_unit = 'X'.    "条件单位     gwa_pocondx
-cond_p_unt = 'X'.   "价格单位     gwa_pocondx
-currency = 'X'.     gwa_pocondx
-change_id    = 'X'.     gwa_pocondx
-conbaseval    = 'X'.     gwa_pocondx
-conpricdat = 'X'.     gwa_pocondx
-condisacti = 'X'.     


APPEND gwa_pocondx TO gt_pocondx.   


ENDLOOP.   
DELETE  gt_pocond WHERE itm_number NE new_po_item.   
SORT gt_item BY po_item.   
SORT gt_itemx BY po_item.   
SORT gt_poschedulex BY po_item.   
CALL FUNCTION 'BAPI_PO_CHANGE'     
EXPORTING       purchaseorder 
= gs_poheader-po_number       poheader      
= gs_poheader       poheaderx     
= gs_poheaderx
*     POADDRVENDOR  =
*     TESTRUN       =
*     MEMORY_UNCOMPLETE            =
*     MEMORY_COMPLETE  =
*     POEXPIMPHEADER   =
*     POEXPIMPHEADERX  =
*     VERSIONS      =
*     NO_MESSAGING  =
*     NO_MESSAGE_REQ   =
*     NO_AUTHORITY  =
*     NO_PRICE_FROM_PO =
*     PARK_COMPLETE =
*     PARK_UNCOMPLETE  =
*    IMPORTING
*     exppurchaseorder = lv_num
*     EXPHEADER     =
*     EXPPOEXPIMPHEADER            =     
TABLES       
return        = gt_return       poitem        
= gt_item       poitemx       
= gt_itemx
*     POADDRDELIVERY   =       poschedule    
= gt_poschedule       poschedulex   
= gt_poschedulex
*     POACCOUNT     =
*     POACCOUNTPROFITSEGMENT       =
*     POACCOUNTX    =
*     POCONDHEADER  =
*     POCONDHEADERX =       pocond        
= gt_pocond       pocondx       
= gt_pocondx
*     POLIMITS      =
*     POCONTRACTLIMITS =
*     POSERVICES    =
*     POSRVACCESSVALUES            =
*     POSERVICESTEXT   =
*     EXTENSIONIN   =
*     EXTENSIONOUT  =
*     POEXPIMPITEM  =
*     POEXPIMPITEMX =
*     POTEXTHEADER  =
*     POTEXTITEM    =
*     ALLVERSIONS   =
*     POPARTNER     =
*     POCOMPONENTS  = GT_POCOMPONENTS
*     POCOMPONENTSX = GT_POCOMPONENTSX .     
.   


READ TABLE gt_return INTO gwa_return WITH KEY type = 'E'.   
IF sy-subrc <> 0.     
CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'       
EXPORTING         
wait = 'X'.     out_ebelp 

= new_po_item.     status 
= 'S'.   

ELSE.     
CALL FUNCTION 'BAPI_TRANSACTION_ROLLBACK'.     status 
= 'E'.     
LOOP AT gt_return INTO gwa_return WHERE type EQ 'E'.       out_message 
= out_message && gwa_return-message.     
ENDLOOP.   
ENDIF.



ENDFUNCTION.

posted @ 2019-08-23 09:15  東東東東  阅读(608)  评论(0编辑  收藏  举报