SAP R/3 форум ABAP консультантов
Russian ABAP Developer's Club

Home - FAQ - Search - Memberlist - Usergroups - Profile - Log in to check your private messages - Register - Log in - English
Blogs - Weblogs News

BAPI_REQUIREMENTS_CHANGE



 
Post new topic   Reply to topic    Russian ABAP Developer's Club Forum Index -> ММ
View previous topic :: View next topic  
Author Message
admin
Администратор
Администратор



Joined: 01 Sep 2007
Posts: 1636

PostPosted: Sat Sep 22, 2007 4:37 pm    Post subject: BAPI_REQUIREMENTS_CHANGE Reply with quote

This program allows us to update existing requirements as well as to create new ones.
Code:

* Author: Rosie Brent, Darlington
report zppbapi_upload_indep_reqs message-id zpp
                                 line-size 132
                                 line-count 65.
*
tables: mara.
*
data: w_t247 like t247 occurs 0 with header line,
      w_excel_tab type alsmex_tabline occurs 0 with header line,
      w_matnr type matnr.
data: begin of col_months occurs 0,
        colpos type i,
        mnr type fcmnr,
        gjahr type gjahr,
      end of col_months.
data: begin of w_input_tab occurs 0,
        matnr type matnr,
        gjahr type gjahr,
        monat type am_monat,
        kunnr type kunnr,
        prctr type prctr,
        volum type volum,
        meins type meins,
        flag type c,
      end of w_input_tab,
      begin of w_work_tab occurs 0,
        matnr type matnr,
        gjahr type gjahr,
        monat type am_monat,
        volum type volum,
      end of w_work_tab.
data: begin of wa_rejections,
        matnr like mara-matnr,
        kunnr like kna1-kunnr,
        volum like zcobudgetvol-volum,
        comment(80) type c,
      end of wa_rejections,
      tab_rejections like standard table of wa_rejections.
data: w_bapi_item like bapisitemr,
      requirements_out like bapisitmeo occurs 0 with header line,
      return like bapireturn1 occurs 0 with header line,
     requirements_schedule_in like bapisshdin occurs 0 with header line,
     t_cals like vtbfcal occurs 0 with header line,
      w_mode(20) type c,
      w_date like sy-datum,
      w_accepted type i,
      rows type i,
      colpos type i,
      w_len type i,
      w_off like w_len,
      w_cend like w_len.
************************************************************************
*                      SELECTION-SCREEN DEFINITION                     *
************************************************************************
selection-screen begin of block s01 with frame title text-s01.
selection-screen skip.
*
selection-screen begin of line.
selection-screen position 3.
selection-screen comment (20) text-001.
selection-screen position 27.
select-options s_mtart for mara-mtart.
selection-screen end of line.
*
selection-screen begin of line.
selection-screen position 3.
selection-screen comment (20) text-002.
selection-screen position 30.
parameters p_werks like mard-werks default '1500' obligatory.
selection-screen end of line.
*
selection-screen begin of line.
selection-screen position 3.
selection-screen comment (20) text-003.
selection-screen position 30.
parameters p_bedae like pbid-bedae default 'VSF' obligatory.
selection-screen end of line.
*
selection-screen begin of line.
selection-screen position 3.
selection-screen comment (20) text-004.
selection-screen position 30.
parameters p_versb like pbid-versb default 'ST' obligatory.
selection-screen end of line.
*
selection-screen begin of line.
selection-screen position 3.
selection-screen comment (20) text-005.
selection-screen position 30.
parameters p_ident like tfacs-ident obligatory.
selection-screen end of line.
*
selection-screen begin of line.
selection-screen position 3.
selection-screen comment (20) text-009.
selection-screen position 30.
parameters p_uom like mara-meins default 'EA'.
selection-screen end of line.
*
selection-screen skip.
selection-screen end of block s01.
*
selection-screen begin of block s02 with frame title text-s02.
selection-screen skip.
*
selection-screen begin of line.
selection-screen position 3.
selection-screen comment (25) text-006.
selection-screen position 30.
parameters p_file type localfile obligatory.
selection-screen end of line.
*
selection-screen begin of line.
selection-screen position 3.
selection-screen comment (25) text-008.
selection-screen position 30.
parameters p_fend type i obligatory.
selection-screen end of line.
*
selection-screen begin of line.
selection-screen position 3.
selection-screen comment (25) text-010.
selection-screen position 30.
parameters p_cend(2) type c obligatory.
selection-screen end of line.
*
selection-screen skip.
selection-screen end of block s02.
************************************************************************
*                         INITIALIZATION                               *
************************************************************************
initialization.
move 'FERT' to s_mtart-low.
move 'I' to s_mtart-sign.
move 'EQ' to s_mtart-option.
append s_mtart.
************************************************************************
*                   SELECTION-SCREEN VALIDATION                        *
************************************************************************
at selection-screen on value-request for p_file.
*
  call function 'WS_FILENAME_GET'
    exporting
*   DEF_FILENAME           = ' '
*   DEF_PATH               = ' '
      mask                   = ',*.*,*.xls.'
      mode                   = 'O'
*   TITLE                  = ' '
    importing
      filename               = p_file
*   RC                     =
    exceptions
      inv_winsys             = 1
      no_batch               = 2
      selection_cancel       = 3
      selection_error        = 4
      others                 = 5
            .
  if sy-subrc <> 0.
    check not sy-subrc eq 3.
    message e999 with 'Problem selecting file'.
  endif.
************************************************************************
*                      START OF SELECTION                              *
************************************************************************
start-of-selection.
* Translate P_CEND into an integer to be used later.
w_len = strlen( p_cend ).
*
w_off = 0.
*
do w_len times.
  search sy-abcde for p_cend+w_off(1).
  if sy-subrc eq 0.
    w_cend = w_cend + sy-fdpos + 1.
    if w_len > 1 and w_off = 0.
      w_cend = w_cend * 26.
    endif.
  endif.
  w_off = w_off + 1.
enddo.
* Move Factory Calendar for use later
t_cals-ident = p_ident.
append t_cals.
clear t_cals.
* Get month information from T247 for use in translating column headers
select *
  into table w_t247
  from t247
  where spras = sy-langu.
* Upload the Excel file using the inputs from the user and the known
* layout of the file template.
p_fend = p_fend + 1.
*
call function 'ALSM_EXCEL_TO_INTERNAL_TABLE'
  exporting
    filename                      = p_file
    i_begin_col                   = 1
    i_begin_row                   = 1
    i_end_col                     = w_cend
    i_end_row                     = p_fend
  tables
    intern                        = w_excel_tab
  exceptions
    inconsistent_parameters       = 1
    upload_ole                    = 2
    others                        = 3.
if sy-subrc <> 0.
  message id sy-msgid type sy-msgty number sy-msgno
          with sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
endif.
* Re-format input file in order to use it effectively.
sort w_excel_tab by row col.
colpos = 5.
* Read headers and translate them into months.
loop at w_excel_tab where row = 1.
  check w_excel_tab-col eq colpos.
  split w_excel_tab-value at '/'
    into col_months-mnr col_months-gjahr.
  col_months-colpos = colpos.
  read table w_t247 with key mnr = col_months-mnr.
  if sy-subrc eq 0.
    append col_months.
  endif.
  colpos = colpos + 1.
endloop.
*
delete w_excel_tab where row = 1.
*
loop at w_excel_tab.
  case w_excel_tab-col.
    when 1.
      clear w_input_tab.
      move w_excel_tab-value to w_input_tab-prctr.
    when 2.
      move w_excel_tab-value to w_input_tab-matnr.
    when 3.
* Do nothing - this is the description field
    when 4.
      move w_excel_tab-value to w_input_tab-kunnr.
      call function 'CONVERSION_EXIT_ALPHA_INPUT'
        exporting
          input         = w_input_tab-kunnr
         importing
          output        = w_input_tab-kunnr.
    when others.
      read table col_months with key colpos = w_excel_tab-col.
      move col_months-mnr to w_input_tab-monat.
      move col_months-gjahr to w_input_tab-gjahr.
      move 'EA' to w_input_tab-meins.
      translate w_excel_tab-value using ', '.
      condense w_excel_tab-value no-gaps.
      move w_excel_tab-value to w_input_tab-volum.
      append w_input_tab.
      clear w_input_tab-volum.
  endcase.
endloop.
*
free w_excel_tab.
*
loop at w_input_tab.
  if w_input_tab-volum is initial.
    w_input_tab-flag = 'X'.
    modify w_input_tab.
    continue.
  endif.
  select single matnr
    into w_matnr
    from mara
    where matnr = w_input_tab-matnr
      and mtart in s_mtart.
  if sy-subrc ne 0.
    w_input_tab-flag = 'X'.
    modify w_input_tab.
    move-corresponding w_input_tab to wa_rejections.
    wa_rejections-comment = 'Material is not a valid Part Type'.
    append wa_rejections to tab_rejections.
    clear wa_rejections.
    continue.
  endif.
  clear w_matnr.
  select single matnr
    into w_matnr
    from mard
    where matnr = w_input_tab-matnr
      and werks = p_werks.
  if sy-subrc ne 0.
    w_input_tab-flag = 'X'.
    modify w_input_tab.
    move-corresponding w_input_tab to wa_rejections.
    wa_rejections-comment = 'Material does not exist in Plant'.
    concatenate wa_rejections-comment p_werks into wa_rejections-comment
      separated by space.
    append wa_rejections to tab_rejections.
    clear wa_rejections.
    continue.
  endif.
endloop.
*
delete w_input_tab where flag = 'X'.
*
loop at w_input_tab.
  w_work_tab-matnr = w_input_tab-matnr.
  w_work_tab-monat = w_input_tab-monat.
  w_work_tab-gjahr = w_input_tab-gjahr.
  w_work_tab-volum = w_input_tab-volum.
  collect w_work_tab.
  clear w_work_tab.
endloop.
*
free w_input_tab.
*
loop at w_work_tab.
  perform calc_working_days.
  modify w_work_tab.
endloop.
*
sort w_work_tab by matnr.
*
loop at w_work_tab.
*
  at new matnr.
    clear w_mode.
    clear: w_bapi_item, requirements_out, requirements_schedule_in,
return.
    refresh: requirements_out, requirements_schedule_in, return.
    move w_work_tab-matnr to w_bapi_item-material.
    move p_werks to w_bapi_item-plant.
    move p_bedae to w_bapi_item-requ_type.
    move p_versb to w_bapi_item-version.
    move ' ' to w_bapi_item-vers_activ.
*
    call function 'BAPI_REQUIREMENTS_GETDETAIL'
      exporting
        material               = w_bapi_item-material
        plant                  = w_bapi_item-plant
        requirementstype       = w_bapi_item-requ_type
        version                = w_bapi_item-version
        reqmtsplannumber       = w_bapi_item-req_number
*       MRP_AREA               =
      tables
        requirements_out       = requirements_out
        return                 = return
              .
*
    if not requirements_out[] is initial.
      w_mode = 'CHANGE'.
      loop at requirements_out.
        if requirements_schedule_in-req_date is initial.
          continue.
        else.
          requirements_schedule_in-date_type = '1'.
          requirements_schedule_in-req_date = requirements_out-req_date.
          requirements_schedule_in-req_qty = requirements_out-req_qty.
          requirements_schedule_in-unit = requirements_out-unit.
          append requirements_schedule_in.
          clear requirements_schedule_in.
        endif.
      endloop.
    else.
      w_mode = 'CREATE'.
    endif.
    clear return.
    refresh return.
  endat.
* Process line volumes
  w_date(4) = w_work_tab-gjahr.
  w_date+4(2) = w_work_tab-monat.
  w_date+6(2) = '01'.
*
  call function 'RE_ADD_MONTH_TO_DATE'
    exporting
      months    = 1
      olddate   = w_date
    importing
      newdate   = w_date.
*
  w_date = w_date - 1.
*
  call function 'DATE_CONVERT_TO_WORKINGDAY'
    exporting
      date                    = w_date
      direction               = '-'
    importing
      workingday              = w_date
    tables
      factory_calendars       = t_cals.
*
  requirements_schedule_in-date_type = '1'.
  requirements_schedule_in-req_date = w_date.
  requirements_schedule_in-req_qty = w_work_tab-volum.
  requirements_schedule_in-unit = p_uom.
  append requirements_schedule_in.
  clear requirements_schedule_in.
* Process Material completely
  at end of matnr.
    if w_mode = 'CHANGE'.
      call function 'BAPI_REQUIREMENTS_CHANGE'
        exporting
          material                       = w_bapi_item-material
          plant                          = w_bapi_item-plant
          requirementstype               = w_bapi_item-requ_type
          version                        = w_bapi_item-version
          reqmtsplannumber               = w_bapi_item-req_number
          vers_activ                     = w_bapi_item-vers_activ
*         REQUIREMENT_PARAM              =
*         MRP_AREA                       =
*       IMPORTING
*         REQUIREMENT_ITEM_OUT           =
        tables
          requirements_schedule_in       = requirements_schedule_in
*         REQUIREMENTS_CHAR_IN           =
          return                         = return
                .
      if not return[] is initial.
        loop at return where type = 'E'.
        endloop.
        if sy-subrc eq 0.
          wa_rejections-matnr = w_bapi_item-material.
        wa_rejections-comment = 'Failed to change planned requirements'.
          append wa_rejections to tab_rejections.
          clear wa_rejections.
          clear return.
          refresh return.
        endif.
      else.
        w_accepted = w_accepted + 1.
        clear return.
        refresh return.
      endif.
    elseif w_mode = 'CREATE'.
      call function 'BAPI_REQUIREMENTS_CREATE'
        exporting
          requirements_item              = w_bapi_item
*         REQUIREMENT_PARAM              =
*       IMPORTING
*         MATERIAL                       =
*         PLANT                          =
*         REQUIREMENTSTYPE               =
*         VERSION                        =
*         REQMTSPLANNUMBER               =
*         MRP_AREA                       =
        tables
          requirements_schedule_in       = requirements_schedule_in
*         REQUIREMENTS_CHAR_IN           =
          return                         = return
                .
      if not return[] is initial.
        loop at return where type = 'E'.
        endloop.
        if sy-subrc eq 0.
          wa_rejections-matnr = w_bapi_item-material.
        wa_rejections-comment = 'Failed to create planned requirements'.
          append wa_rejections to tab_rejections.
          clear wa_rejections.
          clear return.
          refresh return.
        endif.
      else.
        w_accepted = w_accepted + 1.
        clear return.
        refresh return.
      endif.
    endif.
  endat.
endloop.

write: / 'Accepted Records: ', w_accepted.

if not tab_rejections is initial.
  describe table tab_rejections lines rows.
  skip 2.
  write: / 'Rejected Records: ', rows.
  skip.
  loop at tab_rejections into wa_rejections.
    write: / wa_rejections-matnr,
             wa_rejections-comment.
  endloop.
endif.
*&---------------------------------------------------------------------*
*&      Form  CALC_WORKING_DAYS
*&---------------------------------------------------------------------*
form calc_working_days.
data: w_date like sy-datum,
      w_no_days type i,
      w_days like t009b-butag,
      w_rc like sy-subrc.
*
call function 'NUMBER_OF_DAYS_PER_MONTH_GET'
  exporting
    par_month       = w_work_tab-monat
    par_year        = w_work_tab-gjahr
  importing
    par_days        = w_days.
          .
*
w_date(4) = w_work_tab-gjahr.
w_date+4(2) = w_work_tab-monat.
w_date+6(2) = '01'.
*W_DAYS = W_DAYS - 1.
*
do w_days times.
  call function 'Z_IS_WORKDAY_2'
    exporting
      datin         = w_date
      facal         = p_ident
    importing
      rc            = w_rc
            .
  if w_rc = 0.
    w_no_days = w_no_days + 1.
  endif.
  w_date = w_date + 1.
enddo.
*
w_work_tab-volum = w_work_tab-volum * w_no_days.
*
endform.                    " CALC_WORKING_DAYS
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    Russian ABAP Developer's Club Forum Index -> ММ All times are GMT + 4 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You cannot download files in this forum


All product names are trademarks of their respective companies. SAPNET.RU websites are in no way affiliated with SAP AG.
SAP, SAP R/3, R/3 software, mySAP, ABAP, BAPI, xApps, SAP NetWeaver and any other are registered trademarks of SAP AG.
Every effort is made to ensure content integrity. Use information on this site at your own risk.