2019年12月19日星期四

PIR

report ZYPB840.

types:
  begin of TYP_PIR_KEY,
    MATNR type MATNR,
    PBDNR type PBIM-PBDNR,
  end of TYP_PIR_KEY,
  begin of TYP_FILE,
    ROW type STRING,
  end of TYP_FILE,
  I_TYP_FILE type standard table of TYP_FILE.

data:
  G_KUNNR type ZXP24-KUNNR,
  G_KBN   type ZXP24-ZZKUBUN,
  G_KDMAT type ZXP24-KDMAT,
  G_NOUKI type ZXP24-ZZNOUKI,
  GI_SYS  type I_TYP_FILE,
  GI_TRN  type I_TYP_FILE,
  GI_END  type I_TYP_FILE.


select-options:
  S_KUNNR for G_KUNNR,
  S_KBN   for G_KBN,
  S_KDMAT for G_KDMAT,
  S_NOUKI for G_NOUKI obligatory.

selection-screen:skip 2.

parameters:
  P_FOLD1 type  FILEPATH-PATHINTERN obligatory,
  P_FOLD2 type  FILEPATH-PATHINTERN obligatory,
  P_FOLD3 type  FILEPATH-PATHINTERN obligatory.

selection-screen:skip 1.

parameters:
  P_WAIT1 type I,
  P_WAIT2 type I.



initialization.
  S_NOUKI-SIGN 'I'.
  S_NOUKI-OPTION 'EQ'.
  S_NOUKI-LOW SY-DATUM 30.
  append S_NOUKI.
  P_WAIT1 60.
  P_WAIT2 300.


at selection-screen.

start-of-selection.
  perform F_MAIN.

end-of-selection.
  if GI_SYS is not initial.
    perform F_WRITE_LOG using '1' GI_SYS.
  endif.
  perform F_WRITE_LOG using '2' GI_TRN.
  perform F_WRITE_LOG using '3' GI_END.
  if GI_SYS is initial and LINESGI_TRN <= .
    message '正常終了' type 'S'.
  else.
    message 'エラー発生、ログ参照' type 'S' display like 'E'.
  endif.


*&---------------------------------------------------------------------*
*& Form f_main 主処理
*&---------------------------------------------------------------------*
form F_MAIN.
  data:
    LI_ZXP24   type standard table of ZXP24,
    LW_ZXP24   type ZXP24,
    LW_PIR_KEY type TYP_PIR_KEY,
    LI_PIR_KEY type standard table of TYP_PIR_KEY,
    LI_PBIM    type standard table of PBIM,
    LV_JOBNAME type TBTCO-JOBNAME,
    LW_PBIM    type PBIM,
    LW_TRN     type TYP_FILE,
    LW_SYS     type TYP_FILE,
    LV_FLG     type CHAR01,
    LV_INDEX   type I.

* mrp実行確認
  clearLV_FLG,
         GI_SYS,
         GI_TRN.

  do times.
    LV_INDEX SY-INDEX.
    select single JOBNAME into LV_JOBNAME
    from TBTCO where JOBNAME like '%MRP%'.
    if SY-SUBRC <> 0.
      LV_FLG ABAP_ON.
      exit.
    else.
      if LV_INDEX < 3.
        wait up to P_WAIT1 seconds.
      endif.
    endif.
  enddo.

  if LV_FLG is initial.
    LW_SYS-ROW 'MRP実行中。。。。。。'.
    append LW_SYS to GI_SYS.
    return.
  endif.




* テーブルロック処理
  do times.
    LV_INDEX SY-INDEX.
    call function 'ENQUEUE_E_TABLE'
      exporting
        MODE_RSTABLE   'E'
        TABNAME        'ZXP24'
      exceptions
        FOREIGN_LOCK   1
        SYSTEM_FAILURE 2
        others         3.
    if SY-SUBRC 0.
      LV_FLG ABAP_ON.
      exit.
    else.
      if LV_INDEX < 3.
        wait up to P_WAIT2 seconds.
      endif.
    endif.
  enddo.

  if LV_FLG is initial.
    LW_SYS-ROW 'ロックエラー。。。。。'.
    append LW_SYS to GI_SYS.
    return.
  endif.



* データ抽出処理
  select from ZXP24
  into table LI_ZXP24
  where KUNNR     in S_KUNNR  and
        ZZKUBUN   in S_KBN    and
        KDMAT     in S_KDMAT  and
        ZZNOUKI   in S_NOUKI  and
        ZZUPDDATE_FLG 'X'   and

        ZZDELETE_FLG SPACE.

  check LI_ZXP24 is not initial.

  loop at LI_ZXP24 into LW_ZXP24.
    LW_PIR_KEY-MATNR LW_ZXP24-MATNR.
    LW_PIR_KEY-PBDNR LW_ZXP24-KUNNR+5(5&& LW_ZXP24-ZZKUBUN.
    append LW_PIR_KEY to LI_PIR_KEY.
  endloop.
  sort LI_PIR_KEY by MATNR ascending
                     PBDNR ascending.

  delete adjacent duplicates from LI_PIR_KEY.

*(2)独立所要量データ
* 内示データに該当独立所要量データを取得する
  select from PBIM into table LI_PBIM
  for all entries in  LI_PIR_KEY
  where MATNR LI_PIR_KEY-MATNR and
        PBDNR LI_PIR_KEY-PBDNR.

  concatenate TEXT-H01
              TEXT-H02
              TEXT-H03
              TEXT-H04
              TEXT-H05
   into LW_TRN-ROW separated by CL_ABAP_CHAR_UTILITIES=>HORIZONTAL_TAB.
  append LW_TRN to GI_TRN.

*(4) 独立所要量登録更新処理
  loop at LI_ZXP24 into LW_ZXP24.

    read table LI_PBIM into LW_PBIM
    with key MATNR LW_ZXP24-MATNR
             VERSB =  LW_ZXP24-KUNNR+5(5&& LW_ZXP24-ZZKUBUN.
    if SY-SUBRC <> 0.
      perform F_CREATE using LW_ZXP24.
    else.
      perform F_CHANGE using LW_ZXP24.
    endif.
  endloop.


* テーブルアンロック処理
  call function 'DEQUEUE_E_TABLE'
    exporting
      MODE_RSTABLE 'E'
      TABNAME      'ZXP24'.


endform.



*&---------------------------------------------------------------------*
*& Form CREATE 登録処理
*&---------------------------------------------------------------------*
form F_CREATE using UV_ZXP24 type ZXP24.

  data:
    LW_BAPISITEMR type BAPISITEMR,
    LI_SCHEDULE   type standard table of BAPISSHDIN,
    LW_SCHEDULE   type BAPISSHDIN,
    LI_RETURN     type standard table of BAPIRETURN1,
    LW_RETURN     type BAPIRETURN1,
    LW_ZXP24      type ZXP24,
    LV_ERRFLG     type CHAR01,
    LW_TRN        type TYP_FILE.

  "品目
  LW_BAPISITEMR-MATERIAL UV_ZXP24-MATNR.
  "プラント
  LW_BAPISITEMR-PLANT '1000'.
  "所要量タイプ
  LW_BAPISITEMR-REQU_TYPE 'LSF'.
  "所要量のバージョン番号
  LW_BAPISITEMR-VERSION '01'.
  "バージョン有効フラグ
  LW_BAPISITEMR-VERS_ACTIV 'X'.
  LW_BAPISITEMR-REQ_NUMBER UV_ZXP24-KUNNR+5(5&& UV_ZXP24-ZZKUBUN.


  clear LI_SCHEDULE.
  LW_SCHEDULE-DATE_TYPE '1'.   "day
  LW_SCHEDULE-REQ_DATE  UV_ZXP24-ZZNOUKI.
  LW_SCHEDULE-REQ_QTY   UV_ZXP24-ZZSUU.
*  LW_SCHEDULE-unit     = c_unit.
  append LW_SCHEDULE to LI_SCHEDULE.


  call function 'BAPI_REQUIREMENTS_CREATE' " Bapi to Create Planned Independent Reqmts
    exporting
      REQUIREMENTS_ITEM        LW_BAPISITEMR
      DO_COMMIT                'X'
      UPDATE_MODE              'X'
    tables
      REQUIREMENTS_SCHEDULE_IN LI_SCHEDULE
      RETURN                   LI_RETURN.

  loop at LI_RETURN into LW_RETURN
    where TYPE 'A' or
          TYPE 'E'.
    LV_ERRFLG 'X'.
  endloop.
  if LV_ERRFLG  is initial.
    LW_ZXP24 UV_ZXP24.
    clear LW_ZXP24-ZZUPDDATE_FLG.
    LW_ZXP24-AEDAT SY-DATUM.
    LW_ZXP24-AENAM SY-UNAME.
    LW_ZXP24-UDLTIME SY-UZEIT.
    modify ZXP24 from LW_ZXP24.
    commit work.
  else.
    concatenate UV_ZXP24-KUNNR
                UV_ZXP24-ZZKUBUN
                UV_ZXP24-KDMAT
                UV_ZXP24-ZZNOUKI
                LW_RETURN-MESSAGE
    into LW_TRN-ROW separated by CL_ABAP_CHAR_UTILITIES=>HORIZONTAL_TAB.
    append LW_TRN to GI_TRN.


  endif.

endform.



*&---------------------------------------------------------------------*
*& Form change 更新処理
*&---------------------------------------------------------------------*

form F_CHANGE using UV_ZXP24 type ZXP24.

  data:
    LW_BAPISITEMR type BAPISITEMR,
    LI_SCHEDULE   type standard table of BAPISSHDIN,
    LW_SCHEDULE   type BAPISSHDIN,
    LI_RETURN     type standard table of BAPIRETURN1,
    LW_RETURN     type BAPIRETURN1,
    LV_ERRFLG     type CHAR01,
    LW_ZXP24      type ZXP24,
    LW_TRN        type TYP_FILE.


  clear LI_SCHEDULE.
  LW_SCHEDULE-DATE_TYPE '1'.   "day
  LW_SCHEDULE-REQ_DATE  UV_ZXP24-ZZNOUKI.
  LW_SCHEDULE-REQ_QTY   UV_ZXP24-ZZSUU.
*  LW_SCHEDULE-unit     = c_unit.
  append LW_SCHEDULE to LI_SCHEDULE.


  call function 'BAPI_REQUIREMENTS_CHANGE'
    exporting
      MATERIAL                 UV_ZXP24-MATNR
      PLANT                    '1000'
      REQUIREMENTSTYPE         'LSF'
      VERSION                  UV_ZXP24-KUNNR+5(5&& UV_ZXP24-ZZKUBUN
      REQMTSPLANNUMBER         ''
      VERS_ACTIV               'X'
      DO_COMMIT                'X'
      UPDATE_MODE              'X'
    tables
      REQUIREMENTS_SCHEDULE_IN LI_SCHEDULE
      RETURN                   LI_RETURN.

  loop at LI_RETURN into LW_RETURN
     where TYPE 'A' or
           TYPE 'E'.
    LV_ERRFLG 'X'.
  endloop.
  if LV_ERRFLG  is initial.
    LW_ZXP24 UV_ZXP24.
    clear LW_ZXP24-ZZUPDDATE_FLG.
    LW_ZXP24-AEDAT SY-DATUM.
    LW_ZXP24-AENAM SY-UNAME.
    LW_ZXP24-UDLTIME SY-UZEIT.
    modify ZXP24 from LW_ZXP24.
    commit work.
  else.
    concatenate UV_ZXP24-KUNNR
          UV_ZXP24-ZZKUBUN
          UV_ZXP24-KDMAT
          UV_ZXP24-ZZNOUKI
          LW_RETURN-MESSAGE
    into LW_TRN-ROW separated by CL_ABAP_CHAR_UTILITIES=>HORIZONTAL_TAB.
    append LW_TRN to GI_TRN.

  endif.

endform.
*&---------------------------------------------------------------------*
*& Form F_WRITE_LOG サーバ7ファイル作成処理
*&---------------------------------------------------------------------*
form F_WRITE_LOG using UV_TYPE type CHAR01
                       UI_CONTENTS type I_TYP_FILE.
  data:LV_PATH   type FILEPATH-PATHINTERN,
       LV_FILE   type STRING,
       LV_FILENM type STRING,
       LV_ROW    type TYP_FILE.

  if UV_TYPE 1.
    LV_PATH P_FOLD1.
    LV_FILE TEXT-T01 && SY-DATUM && SY-UZEIT && '.txt'.
  elseif UV_TYPE 2.

    LV_PATH P_FOLD2.
    LV_FILE TEXT-T02 && SY-DATUM && SY-UZEIT && '.txt'.
  else.
    LV_PATH P_FOLD3.
    LV_FILE TEXT-T03 && SY-DATUM && SY-UZEIT && '.txt'.
  endif.


  call function 'FILE_GET_NAME_USING_PATH'
    exporting
      LOGICAL_PATH               LV_PATH
      FILE_NAME                  LV_FILE
    importing
      FILE_NAME_WITH_PATH        LV_FILENM
    exceptions
      PATH_NOT_FOUND             1
      MISSING_PARAMETER          2
      OPERATING_SYSTEM_NOT_FOUND 3
      FILE_SYSTEM_NOT_FOUND      4
      others                     5.
  if SY-SUBRC <> 0.
* Implement suitable error handling here
  endif.

  open dataset LV_FILENM for output
    in legacy text mode  code page '8000'
     with windows linefeed .
  loop at UI_CONTENTS into LV_ROW.
    transfer LV_ROW-ROW to  LV_FILENM.
  endloop.
  close dataset  LV_FILENM.

endform.

2019年12月18日星期三

VA01 BAPI EXTENSIONS

FUNCTION:SD_SALESDOCUMENT_CREATE
Append fiels to Structure below.
BAPE_VBAP
BAPE_VBAPX
VBAPKOZ
VBAPKOZX
No need to append fields to Structure VBAPKOM and VBAPKOMX.
(VBAPKOM ----include VBAPKOZ 
 VBAPKOMX ---include VBAPKOZX) 


1) VBAP: In your append structure you specify your ZZFIELD with the data type as needed

2) BAPE_VBAP: In the append structure here also add the ZZFIELD with the data type as needed with limitations. No decimals. Try and stick to char characters

3) BAPE_VBAPX: In the append structure add the field ZZFIELD of type BAPIUPDATE

(NOTE: (2) and (3) must have the same number of fields in the same order)

4) VBAPKOZ: In the append structure here also add the ZZFIELD with the data type as needed with limitations. No decimals. Try and stick to char characters

5) VBAPKOZX: In the append structure add the field ZZFIELD of type BAPIUPDATE

(NOTE: (4) and (5) must have the same number of fields in the same order)

(Note: A great tip from Hendrik Maas: Use Data element CHAR1 instead of BAPIUPDATE as it will not work for BAPI_SALESDOCUMENT_CHANGE)

Similarly do for VBAK, BAPE_VBAK, BAPE_VBAKX, VBAKKOZ and VBAKKOZX.

Next we get to the code to fill in the structure EXTENSIONIN
I will demonstrate how to call the create sales order BAPI with custom fields.

* Local definitions
  DATA: wa_extensionin TYPE bapiparex,
        wa_bape_vbap   TYPE bape_vbap,
        wa_bape_vbapx  TYPE bape_vbapx,
        wa_bape_vbak   TYPE bape_vbak,
        wa_bape_vbakx  TYPE bape_vbakx,
        lv_posnr       TYPE posnr.

* Processing the header extension
  CLEAR wa_bape_vbak.
  wa_bape_vbak-ZZFIELD     = ‘HDRTEST’.
  wa_extensionin-structure = ‘BAPE_VBAK’.
  wa_extensionin+30(960)   = wa_bape_vbak.
  append wa_extensionin to lt_extensionin.
  clear wa_extensionin.

* Processing the line extension
LOOP AT line_items INTO wa_lineitems.

  ADD 10 TO lv_posnr.   
  CLEAR wa_bape_vbap.
  wa_bape_vbap-ZZFIELD     = ‘TEST’.
  wa_extensionin-structure = ‘BAPE_VBAP’.
  wa_bape_vbap-posnr       = lv_posnr.
  wa_extensionin+30(960)   = wa_bape_vbap.
  append wa_extensionin to lt_extensionin.
  clear wa_extensionin.

  CLEAR wa_bape_vbapx.
  wa_bape_vbapx-ZZFIELD    = ‘X’.
  wa_extensionin-structure = ‘BAPE_VBAPX’.
  wa_bape_vbapx-posnr      = lv_posnr.
  wa_extensionin+30(960)   = wa_bape_vbapx.
  append wa_extensionin to lt_extensionin.
  clear wa_extensionin.

ENDLOOP.

* Then the call to the BAPI
      CALL FUNCTION ‘BAPI_SALESORDER_CREATEFROMDAT2’
        EXPORTING
          order_header_in  = ls_order_header_in
          order_header_inx = ls_order_header_inx
        IMPORTING
          salesdocument    = lv_salesdocument
        TABLES
          return           = lt_ret2
          order_items_in   = lt_order_items_in
          order_items_inx  = lt_order_items_inx
          order_partners   = lt_order_partners
          order_keys       = lt_order_keys
          extensionin      = lt_extensionin.

Note: If you have a need to force a different Business Object type or wish to see the extension return fields then use the function SD_SALESDOCUMENT_CREATE instead.

      CALL FUNCTION ‘SD_SALESDOCUMENT_CREATE’
        EXPORTING
          sales_header_in     = ls_order_header_in
          sales_header_inx    = ls_order_header_inx
          business_object     = ‘BUS2032’
        IMPORTING
          salesdocument_ex    = lv_salesdocument
          sales_header_out    = lv_sales_header_out
          sales_header_status = lv_sales_header_status
        TABLES
          return              = lt_ret2
          sales_items_in      = lt_order_items_in
          sales_items_inx     = lt_order_items_inx
          sales_partners      = lt_order_partners
          sales_keys          = lt_order_keys
          extensionin         = lt_extensionin
          incomplete_log      = lt_incomplete_log
          extensionex         = lt_extensionex.


2019年12月5日星期四

SD Transport Route Determination

1. Get Shipping point from TVSTZ table by Shipping Cond. of Customer:Ship-to and Loading Group
of Material:Sales Data.

2.Get the country (ALAND) and Departure zone(AZONE) from TVST table  by Shippping point acquirred from step 1.

3.Get destination country(LLAND) and Transport.zone(LZONE) from  KNA1 by customer:ship-to.

4.Get Actual route(ROUTE) from TROLZ table by
①Country(ALAND) and Departure zone(AZONE) acquired in step 2,
②Shipping Cond (VSBED) of Customer:Ship-to,
③Trans. Group(TRAGR) of Material:Sales Data
destination country(LLAND) and Transport.zone(LZONE) of Ship-to acquired in step 3.

Customer Characteristic

  Characteristic Descriptions are stored in the table - CABNT - Language - SPRAS - Characteristic description - ATBEZ Characteristic Values ...