***INCLUDE MMWE0F01 .
* 88951 26.11.97 4.0B KB: Preis黚ergabe an ME_CREATE_PO_ITEM
* 94702 04.02.98 4.0B KB: Verbuchungsproblem Bestellung/Kundenauftrag
*102133 24.04.98 4.0C KB: Preis黚ernahme in Bestellung
*106225 09.06.98 4.5A KB: Bestellpreismengeneinheit in Bestellposition
*134329 15.01.99 4.5B RB: WE/RE-Kennzeichen
*167369 12.08.99 4.6B RB: Positionsreihenfolge in erzeugten Objekten
*&---------------------------------------------------------------------*
*&      Hinzuf黦en neue Position aus dem Katalog in BAN
*&---------------------------------------------------------------------*
form ban_store_cat.
  clear ban.
  ban-preq_item = h_banpos + 1.
  clear bankey.
  bankey-preq_item = ban-preq_item.
* lesen BAN - merken Indizes
  read table ban with key bankey binary search.
  h_index = sy-tabix.
  h_subrc = sy-subrc.
* 黚ernehmen globale Daten in BAN
  move-corresponding global to ban.
  if ban-preq_name is initial.
    ban-preq_name = sy-uname.
  endif.
* 黚ernehmen Katalog-Daten in BAN
  ban-material     = new_item-matnr.
  ban-pur_mat      = new_item-matnr.
  ban-quantity     = new_item-quantity.
  if new_item-unit is initial.
    message s064 with new_item-unit.
    exit.
  endif.
  perform unit_of_measure_to_sap using new_item-unit.
  if exitflag ne space.
    message s064 with new_item-unit.
    exit.
  endif.
  ban-unit         = new_item-unit.
  ban-short_text   = new_item-description.
  if not new_item-currency is initial and
     not new_item-price is initial.
    perform value_to_sap using new_item-currency new_item-price.
    if exitflag ne space.
      message s065 with new_item-price new_item-currency.
      exit.
    endif.
  endif.
  ban-preis        = new_item-price.
  ban-c_amt_bapi   = new_item-price.
  ban-price_unit   = new_item-priceunit.
  if not ban-preis is initial.
*  BAN-PO_PRICE = '2'.                                "102133/KB
    ban-po_price = '1'.                "102133/KB
  endif.
  if not new_item-currency is initial.
    perform currency_to_sap using new_item-currency.
    if exitflag ne space.
      message s066 with new_item-currency.
      exit.
    endif.
  endif.
  ban-currency     = new_item-currency.
  ban-waers        = ban-currency.
  ban-deliv_date   = sy-datlo + new_item-leadtime.
  ban-vend_mat     = new_item-vendormat.
  ban-manu_mat     = new_item-manufactmat.
  ban-mfr_no_ext   = new_item-manufactcode.
  if h_catalogs-lifnr ne space.
    ban-fixed_vend = h_catalogs-lifnr.
    ban-purch_org  = h_catalogs-ekorg.
  else.
    ban-fixed_vend = new_item-vendor.
    if not h_catalogs-ekorg is initial and
       ban-purch_org is initial.
      ban-purch_org = h_catalogs-ekorg.
    endif.
  endif.
* Materialnummer von Katalog 黚ergeben - Pr黤en Mat. und 黚ern. Daten
  if ban-pur_mat  ne space.
    perform material_get using ban-pur_mat.
    if no_material ne space or
       no_status_b ne space.
      message s050 with ban-pur_mat.
      clear ban-pur_mat.
      clear ban-material.
    else.
      move-corresponding mat_tab to ban.
      ban-preis = mat_tab-c_amt_bapi.
      perform material_availability_check.
    endif.
  endif.
* Materialnummer 黚er Infosatz suchen, wenn Lieferantenmatnr. sitzt
  if ban-pur_mat eq space.
    if ban-vend_mat ne space.
      perform determine_material_inforecord using new_item-vendormat
                                                  ban-fixed_vend
                                                  ban-pur_mat
                                                  ban-info_rec.
* falls Materialnummer gefunden wurde, so weiter, als sei sie eingegeben
      if ban-pur_mat ne space.
        perform material_get using ban-pur_mat.
        if no_material ne space or
           no_status_b ne space.
          clear ban-pur_mat.
          clear ban-material.
        else.
          move-corresponding mat_tab to ban.
          ban-preis = mat_tab-c_amt_bapi.
          perform material_availability_check.
        endif.
      endif.
    endif.
* Materialnummer 黚er Hersteller und Herstellerteilenummer
    if new_item-manufactmat ne space and
       new_item-manufactcode ne space.
      perform determine_material_manufact using new_item-manufactmat
                                                new_item-manufactcode
                                                ban-pur_mat.
* falls Materialnummer gefunden wurde, so weiter, als sei sie eingegeben
      if ban-pur_mat ne space.
        perform material_get using ban-pur_mat.
        if no_material ne space or
           no_status_b ne space.
          clear ban-pur_mat.
          clear ban-material.
        else.
          move-corresponding mat_tab to ban.
          ban-preis = mat_tab-c_amt_bapi.
          perform material_availability_check.
        endif.
      endif.
    endif.
  endif.
* lesen lieferantenstamm und ermitteln EKORG
  if ban-fixed_vend ne space and
     ban-purch_org  eq space.
  endif.
* Festwerte
  if ban-price_unit is initial.
    ban-price_unit = 1.
  endif.
  ban-del_datcat = '1'.
  ban-item_cat   = '0'.
  ban-update = 'I'.
  perform check_mandantory_fields.
* WE/RE-Kennzeichen setzen                                    "134329/RB
  select single * from t163  where pstyp = ban-item_cat.      "134329/RB
  select single * from t163k where knttp = ban-acctasscat.    "134329/RB
                                       "134329/RB
  ban-gr_ind = t163-wepos.             "134329/RB
  ban-gr_non_val = t163-weunb.         "134329/RB
  ban-ir_ind = t163-repos.             "134329/RB
                                       "134329/RB
  if ban-acctasscat ne space.          "134329/RB
    ban-consumpt = t163k-kzvbr.        "134329/RB
    if t163-wepov eq space.            "134329/RB
      ban-gr_ind = t163k-wepos.        "134329/RB
    endif.                             "134329/RB
    if t163-weunv eq space.            "134329/RB
      ban-gr_non_val = t163k-weunb.    "134329/RB
    endif.                             "134329/RB
    if t163-repov eq space.            "134329/RB
      ban-ir_ind = t163k-repos.        "134329/RB
    endif.                             "134329/RB
  endif.                               "134329/RB
* merken redirectURL f黵 SD-scenario
  ban-katalog = h_catalogs-katalog.
  ban-sd      = sel_sd.
  if sel_sd ne space.
    read table redirect with key ban-katalog.
    if sy-subrc ne 0.
      redirect-katalog = ban-katalog.
      field-get 'SD_HOOK_URL' 1 h_redirect h_redirect_length.
      move h_redirect to redirect-url.
      append redirect.
    endif.
  endif.
  if h_subrc eq 0.
    modify ban index h_index.
  else.
    insert ban index h_index.
  endif.
  h_banpos = ban-preq_item.
* erzeugen Kontierungszeile
  perform init_bsc.
* erzeugen Textzeilen
  perform init_bantx.
  clear ban.
endform.
*&---------------------------------------------------------------------*
*&      膎dern Position 黚er das Detailbild - Grunddaten
*&---------------------------------------------------------------------*
form ban_store_core.
  clear bankey.
  bankey-preq_item = bapieban-preq_item.
  read table ban with key bankey binary search.
  h_index = sy-tabix.
  h_subrc = sy-subrc.
* unerhebliche Felder - einfach 黚ernehmen
  ban-doc_type     = bapieban-doc_type.
  ban-pur_group    = bapieban-pur_group.
  ban-short_text   = bapieban-short_text.
  ban-store_loc    = bapieban-store_loc.
  ban-preq_name    = bapieban-preq_name.
  ban-trackingno   = bapieban-trackingno.
  ban-preis        = eban-preis.
  ban-price_unit   = bapieban-price_unit.
  ban-currency     = bapieban-currency.
  ban-vend_mat     = bapieban-vend_mat.
* L鰏chen Bezugsquellendaten bei bestimmten 膎derungen
  if ban-plant ne bapieban-plant or
     ban-mat_grp ne bapieban-mat_grp or
     ban-unit ne bapieban-unit or
     ban-item_cat ne bapieban-item_cat.
    clear next.
    loop at sos where preq_item eq ban-preq_item.
      delete sos.
      next = next + 1.
    endloop.
    if next > 0.
      clear next.
      message s132.
      clear: ban-fixed_vend,
             ban-purch_org,
             ban-agreement,
             ban-agmt_item,
             ban-info_rec.
    endif.
  endif.
* 黚ernehmen Felder, wenn Nichtlagerposition
  if ban-pur_mat  eq space.
    ban-mat_grp      = bapieban-mat_grp.
    ban-plant        = bapieban-plant.
    ban-unit         = bapieban-unit.
    ban-c_amt_bapi   = bapieban-c_amt_bapi.
    ban-price_unit   = bapieban-price_unit.
    ban-currency     = bapieban-currency.
    if not ban-preis is initial.
*     BAN-PO_PRICE = '2'.                                 "102133/KB
      ban-po_price = '1'.              "102133/KB
    endif.
  else.
*  IF NOT BAN-PREIS IS INITIAL.                           "102133/KB
*     BAN-PO_PRICE = '1'.                                 "102133/KB
*  ENDIF.                                                 "102133/KB
  endif.
* Anstossen Verf黦barkeit bei 膎derung Menge oder Termin
  if ban-material ne space.
    if ban-quantity ne bapieban-quantity or
       ban-deliv_date ne bapieban-deliv_date.
      ban-quantity     = bapieban-quantity.
      ban-deliv_date   = bapieban-deliv_date.
      perform material_availability_check.
      message s047.
    endif.
  endif.
  ban-quantity     = bapieban-quantity.
  ban-deliv_date   = bapieban-deliv_date.
* Festwerte
  if ban-price_unit is initial.
    ban-price_unit = 1.
  endif.
  ban-del_datcat = '1'.
  ban-item_cat   = '0'.
  ban-update = 'I'.
  perform check_mandantory_fields.
  modify ban index h_index.
endform.
*&---------------------------------------------------------------------*
*&      Sichern neue oder ge鋘derte Daten in interner Tabelle BAN
*&---------------------------------------------------------------------*
form ban_store_loop.
  clear bankey.
  bankey-preq_item = banwa-preq_item.
  read table ban with key bankey binary search.
  h_index = sy-tabix.
  if sy-subrc eq 0.
* Position ver鋘dert
    if ban-update ne space.
* Daten der Verf黦barkeit haben sich unter Umst鋘den ge鋘dert
      if ban-quantity ne banwa-quantity or
         ban-deliv_date ne banwa-deliv_date.
        ban-quantity   = banwa-quantity.
        ban-deliv_date = banwa-deliv_date.
        if ban-material ne space.
          perform material_availability_check.
          message s047.
        endif.
      endif.
* L鰏chen Bezugsquellendaten bei bestimmten 膎derungen
      if ban-unit ne banwa-unit.
        clear next.
        loop at sos where preq_item eq ban-preq_item.
          delete sos.
          next = next + 1.
        endloop.
        if next > 0.
          clear next.
          message s132.
          clear: ban-fixed_vend,
                 ban-purch_org,
                 ban-agreement,
                 ban-agmt_item,
                 ban-info_rec.
        endif.
      endif.
      ban-unit  = banwa-unit.
      ban-short_text = banwa-short_text.
      perform check_mandantory_fields.
      modify ban index h_index.
* Position ver鋘dert, aber Dummyposition
    else.
      move-corresponding global to ban.
      move-corresponding banwa  to ban.
      if ban-preq_name is initial.
        ban-preq_name = sy-uname.
      endif.
      if ban-pur_mat ne space.
        perform material_get using ban-pur_mat.
        if no_material ne space or
           no_status_b ne space.
          message s050 with ban-pur_mat.
          prepare_item = 'X'.
          delete ban index h_index.
          exit.
        else.
          move-corresponding mat_tab to ban.
          ban-preis = mat_tab-c_amt_bapi.
*           IF NOT BAN-PREIS IS INITIAL.                    "102133/KB
*              BAN-PO_PRICE = '1'.                          "102133/KB
*           ENDIF.                                          "102133/KB
          perform material_availability_check.
        endif.
      else.                            "102133/KB
        if not ban-preis is initial.   "102133/KB
          ban-po_price = '1'.          "102133/KB
        endif.                         "102133/KB
      endif.
      if ban-price_unit is initial.
        ban-price_unit = 1.
      endif.
      ban-del_datcat = '1'.
      ban-item_cat   = '0'.
      ban-update = 'I'.
      h_banpos = ban-preq_item.
      perform check_mandantory_fields.
* WE/RE-Kennzeichen setzen                                    "134329/RB
      select single * from t163  where pstyp = ban-item_cat.  "134329/RB
      select single * from t163k where knttp = ban-acctasscat."134329/RB
                                       "134329/RB
      ban-gr_ind = t163-wepos.         "134329/RB
      ban-gr_non_val = t163-weunb.     "134329/RB
      ban-ir_ind = t163-repos.         "134329/RB
                                       "134329/RB
      if ban-acctasscat ne space.      "134329/RB
        ban-consumpt = t163k-kzvbr.    "134329/RB
        if t163-wepov eq space.        "134329/RB
          ban-gr_ind = t163k-wepos.    "134329/RB
        endif.                         "134329/RB
        if t163-weunv eq space.        "134329/RB
          ban-gr_non_val = t163k-weunb."134329/RB
        endif.                         "134329/RB
        if t163-repov eq space.        "134329/RB
          ban-ir_ind = t163k-repos.    "134329/RB
        endif.                         "134329/RB
      endif.                           "134329/RB
      modify ban index h_index.
* erzeugen Kontierungszeile
      perform init_bsc.
* erzeugen Textzeilen
      perform init_bantx.
    endif.
* neue Position
  else.
    clear ban.
    move-corresponding global to ban.
    move-corresponding banwa  to ban.
    if ban-preq_name is initial.
      ban-preq_name = sy-uname.
    endif.
    if ban-pur_mat ne space.
      perform material_get using ban-pur_mat.
      if no_material ne space or
         no_status_b ne space.
        message s050 with ban-pur_mat.
        prepare_item = 'X'.
        delete ban index h_index.
        exit.
      else.
        move-corresponding mat_tab to ban.
        ban-preis = mat_tab-c_amt_bapi.
*        IF NOT BAN-PREIS IS INITIAL.                       "102133/KB
*           BAN-PO_PRICE = '1'.                             "102133/KB
*        ENDIF.                                             "102133/KB
        perform material_availability_check.
      endif.
    else.                              "102133/KB
      if not ban-preis is initial.     "102133/KB
        ban-po_price = '1'.            "102133/KB
      endif.                           "102133/KB
    endif.
    if ban-price_unit is initial.
      ban-price_unit = 1.
    endif.
    ban-update = 'I'.
    perform check_mandantory_fields.
    ban-item_cat = '0'.
    ban-del_datcat = '1'.
    h_banpos = ban-preq_item.
* WE/RE-Kennzeichen setzen                                    "134329/RB
    select single * from t163  where pstyp = ban-item_cat.    "134329/RB
    select single * from t163k where knttp = ban-acctasscat.  "134329/RB
                                       "134329/RB
    ban-gr_ind = t163-wepos.           "134329/RB
    ban-gr_non_val = t163-weunb.       "134329/RB
    ban-ir_ind = t163-repos.           "134329/RB
                                       "134329/RB
    if ban-acctasscat ne space.        "134329/RB
      ban-consumpt = t163k-kzvbr.      "134329/RB
      if t163-wepov eq space.          "134329/RB
        ban-gr_ind = t163k-wepos.      "134329/RB
      endif.                           "134329/RB
      if t163-weunv eq space.          "134329/RB
        ban-gr_non_val = t163k-weunb.  "134329/RB
      endif.                           "134329/RB
      if t163-repov eq space.          "134329/RB
        ban-ir_ind = t163k-repos.      "134329/RB
      endif.                           "134329/RB
    endif.                             "134329/RB
    insert ban index h_index.
* erzeugen Kontierungszeile
    perform init_bsc.
* erzeugen Textzeilen
    perform init_bantx.
  endif.
  clear ban.
endform.
*&---------------------------------------------------------------------*
*&      膎dern Position - Kontierungen 黚er das Detailbild
*&---------------------------------------------------------------------*
form bsc_store_account.
  clear bankey.
  bankey-preq_item = bapieban-preq_item.
  read table ban with key bankey binary search.
* bsc lesen und Daten fortschreiben
  clear bsckey.
  bsckey-preq_item = bapieban-preq_item.
  bsckey-serial_no = '01'.
  read table bsc with key bsckey binary search.
  h_index1 = sy-tabix.
  if sy-subrc eq 0.
    move-corresponding bapiebkn to bapipogna.
    perform check_account_assignment.
    check exitflag eq space.
    move-corresponding bapipogna to bapiebkn.
    move-corresponding bapiebkn to bsc.
    move-corresponding bsckey to bsc.
    modify bsc index h_index1.
  endif.
endform.
*&---------------------------------------------------------------------*
*&      Form  CHECK_MANDANTORY_FIELDS
*&---------------------------------------------------------------------*
form check_mandantory_fields.
  if ban-quantity is initial.
    message e094.
  endif.
  if ban-deliv_date is initial.
    message e095.
  endif.
  if ban-unit is initial.
    message e096.
  endif.
endform.                               " CHECK_MANDANTORY_FIELDS
*&---------------------------------------------------------------------*
*&      Aufruf fremder Katalog
*&---------------------------------------------------------------------*
form call_catalog.
  loop at catalogs where katalog eq select_cat.
    exit.
  endloop.
  h_catalogs = catalogs.
* merken Katalog-Id und Lieferant in Hilfsfeld
  sel_sd = catalogs-sd_catalog.
  sel_vendor = catalogs-lifnr.
* im Falle SD, Kundennummer besorgen
  if sel_sd ne space.
    perform get_company_code.
    perform get_customer_number.
    field-set 'CUSTOMER_NO' 1 customer_no.
  endif.
* Aufruf Katalog
* wenn vorher bereits aufgerufen - URL aus interner Tabelle
  read table redirect with key catalogs-katalog.
  if sy-subrc eq 0.
    clear my_hook_url.
    its-wgateurl my_hook_url.
    field-set 'MM_HOOK_URL' 1 my_hook_url.
    field-set '~OkCode' 1 'SHPS'.
    its-browser_redirect redirect-url.
  else.
* wenn noch nicht aufgerufen - URL 黚er Customizingtabelle aufbauen
    perform catalog_browser_redirect using catalogs-katalog.
  endif.
endform.
*&---------------------------------------------------------------------*
*&      Form  CATALOG_BROWSER_REDIRECT
*&---------------------------------------------------------------------*
*       Aufruf externe Kataloge 黚er Customizingtabelle
*----------------------------------------------------------------------*
form catalog_browser_redirect using cbr_katalog.
  data: url like savwctxt-fieldcont,
        hook like savwctxt-fieldcont,
        name like savwctxt-fieldname,
        cont like savwctxt-fieldcont,
        hook_name like savwctxt-fieldname,
        hook_index like sy-tabix,
        sep.
  refresh: xmw03.
  clear: xmw03.
* Katalog-Parameter besorgen
  select * from tmw03
           into table xmw03
           where katalog = cbr_katalog.
  sort xmw03.
* 1. Runde: URL des Katalogs merken
  loop at xmw03.
    hook_index = sy-tabix.
* Parameter merken bei Parameter-Wechsel
    if xmw03-fieldnam ne space.
      if name ne space or cont ne space.
        field-set name 0 cont.
        clear: name, cont.
      endif.
* Parameter-Name
      name = xmw03-fieldnam.
    endif.
* Parameter-Wert analysieren
    case xmw03-valuetyp.
* URL
      when '0'.
        concatenate url xmw03-fieldval into url.
* Festwert
      when '2'.
        concatenate cont xmw03-fieldval into cont.
* ABAP-Feld
      when '1'.
        write (xmw03-fieldval) to cont.
* Hook-Url
      when '4'.
* Ende 1.Runde
        exit.
    endcase.
  endloop.
* Fester Teil der R點kgabe-URL vom ITS
  its-wgateurl hook.
* 2. Runde: R點kgabe-URL merken
  loop at xmw03 from hook_index.
* Erster Parameter: Trennzeichen '?'
    if not hook cs '?'.
      sep = '?'.
* Folgeparameter: Trennzeichen '&'
    else.
      sep = '&'.
    endif.
* Parameter-Wert analysieren
    case xmw03-valuetyp.
* Festwert
      when '2'.
       concatenate hook sep xmw03-fieldnam '=' xmw03-fieldval into hook.
* ABAP-Feld
      when '1'.
        write (xmw03-fieldval) to cont.
        concatenate hook sep xmw03-fieldnam '=' cont into hook.
* Hook-Url
      when '4'.
        hook_name = xmw03-fieldnam.
    endcase.
  endloop.
* Komplette R點kgabe-URL als Parameter merken
  field-set hook_name 0 hook.
* URL zusammenbauen, encoden und an ITS 黚ergeben
  its-browser_redirect url.
endform.
*&---------------------------------------------------------------------*
*&      Pr黤en Kontierungsdaten
*&---------------------------------------------------------------------*
form check_account_assignment.
  perform fill_bapicobl.
  refresh return1.
  clear   return1.
  clear   exitflag.
* Aufruf Kontierungspr黤ung und -erg鋘zung
  call function 'BAPI_ACCSERV_CHECKACCASSIGNMT'
       exporting
            check_codingblock   = bapicobl
       importing
            changed_codingblock = bapicobl
       tables
            return              = return1
       exceptions
            error_message       = 1.
* keine Fehler
  if return1[] is initial and
     sy-subrc eq 0.
    perform fill_bapipogna.
  else.
* Fehler ausgeben
    loop at return1 where type eq 'E'.
*     MESSAGE ID RETURN1-ID TYPE RETURN1-TYPE NUMBER RETURN1-NUMBER
      message id return1-id type 'S' number return1-number
              with return1-message_v1 return1-message_v2
                   return1-message_v3 return1-message_v4.
      exitflag = 'X'.
      exit.
    endloop.
    loop at return1 where type ne 'E'.
      message id return1-id type 'S' number return1-number
              with return1-message_v1 return1-message_v2
                   return1-message_v3 return1-message_v4.
    endloop.
  endif.
endform.
*&---------------------------------------------------------------------*
*&      Pr黤en Globale Daten auf Vollst鋘digkeit
*&---------------------------------------------------------------------*
form check_global_data.
  clear global_data_missing.
  if bapipogn-doc_type is initial.
    global_data_missing = 'X'.
  endif.
  if bapipogn-pur_group is initial.
    global_data_missing = 'X'.
  endif.
  if bapipogn-mat_grp is initial.
    global_data_missing = 'X'.
  endif.
  if bapipogn-plant is initial.
    global_data_missing = 'X'.
  endif.
  if bapipogn-acctasscat is initial.
    global_data_missing = 'X'.
  endif.
  if bapipogna-cost_ctr is initial.
    global_data_missing = 'X'.
  endif.
endform.
*&---------------------------------------------------------------------
*       Erzeugen Bestellung
*----------------------------------------------------------------------
form create_po.
 data: transaction_id like arfctid,    "94702/KB
       commit_work(15) value 'COMMIT_WORK',                    "94702/KB
       status_of_transaction(15).      "94702/KB
  clear return.
  refresh return.
 clear transaction_id.                 "94702/KB
* Verbuchung 'normal' wenn SD nicht im Spiel                  "94702/KB
 if sd_katalog is initial.             "94702/KB
* Aufruf BAPI zum Erzeugen der Bestellung
    call function 'BAPI_PO_CREATE'
         exporting
              po_header                  = po_head
         importing
              purchaseorder              = h_ebeln
         tables
              po_items                   = po_item
              po_item_schedules          = po_sched
              po_item_account_assignment = po_acct
              po_item_text               = po_text
              return                     = return
         exceptions
              others                     = 0.
* Verbuchung mit commit and wait, wenn SD im Spiel            "94702/KB
 else.                                 "94702/KB
    call function 'TRANSACTION_BEGIN'  "94702/KB
         importing                     "94702/KB
              transaction_id = transaction_id                  "94702/KB
         exceptions                    "94702/KB
              others         = 1.      "94702/KB
    call function 'BAPI_PO_CREATE'     "94702/KB
         exporting                     "94702/KB
              po_header                  = po_head             "94702/KB
         importing                     "94702/KB
              purchaseorder              = h_ebeln             "94702/KB
         tables                        "94702/KB
              po_items                   = po_item             "94702/KB
              po_item_schedules          = po_sched            "94702/KB
              po_item_account_assignment = po_acct             "94702/KB
              po_item_text               = po_text             "94702/KB
              return                     = return              "94702/KB
         exceptions                    "94702/KB
              others                     = 0.                  "94702/KB
    call function 'TRANSACTION_STATUS' "94702/KB
         importing                     "94702/KB
              status  = status_of_transaction                  "94702/KB
         exceptions                    "94702/KB
              others  = 1.             "94702/KB
    if status_of_transaction = commit_work.                    "94702/KB
      commit work and wait.            "94702/KB
    endif.                             "94702/KB
    call function 'TRANSACTION_END'    "94702/KB
         exporting                     "94702/KB
              transaction_id = transaction_id                  "94702/KB
         exceptions                    "94702/KB
              others         = 1.      "94702/KB
 endif.                                "94702/KB
* Auswertung der aufgetretenen Fehler und Fortschreiben Protokoll
  loop at return.
    error_po = 'X'.
    move-corresponding return to prot_po.
    prot_po-vendor = po_head-vendor.
    append prot_po.
  endloop.
* Merken SD-Katalog in PO-Tabelle um den erneuten Aufruf zu erm鰃lichen
  clear object_po.
  if h_ebeln ne space.
    po_exist = 'X'.
    object_po-number = h_ebeln.
    if not sd_katalog is initial.
      object_po-katalog = sd_katalog.
    endif.
    append object_po.
  endif.
  clear: po_head, po_item, po_acct, po_text, return.
  refresh: po_item, po_acct, po_text, return.
endform.
*&---------------------------------------------------------------------*
*&      Form  CREATE_REQUISITION
*&---------------------------------------------------------------------*
*       Anlegen Bestellanforderung 黚er BAPI_REQUISITION_CREATE
*----------------------------------------------------------------------*
form create_requisition.
  clear error_rq.
  clear rq_exist.
  clear return.
  refresh return.
* Aufruf BAPI zum Erzeugen der Bestellanforderung
  call function 'BAPI_REQUISITION_CREATE'
       exporting
            skip_items_with_error          = 'X'
       importing
            number                         = h_banfn
       tables
            requisition_items              = req_item
            requisition_account_assignment = req_acct
            requisition_item_text          = req_text
            return                         = return
       exceptions
            others                         = 0.
* Auswertung der aufgetretenen Fehler und Fortschreiben Protokoll
  loop at return.
    error_rq = 'X'.
    move-corresponding return to prot_rq.
    append prot_rq.
  endloop.
* Banfnummer merken
  if h_banfn ne space.
    rq_exist = 'X'.
    object_rq-number = h_banfn.
    append object_rq.
  endif.
  clear: req_item, req_acct, req_text, return.
  refresh: req_item, req_acct, req_text, return.
endform.
*&---------------------------------------------------------------------*
*&      Form  CREATE_RESERVATION
*&---------------------------------------------------------------------*
*       Anlegen Reservierung 黚er BAPI_RESERVATION_CREATE
*----------------------------------------------------------------------*
form create_reservation.
  clear return.
  refresh return.
* Aufruf BAPI zum Erzeugen der Reservierungen
  call function 'BAPI_RESERVATION_CREATE'
       exporting
            reservation_header = res_head
       importing
            reservation        = h_rsnum
       tables
            reservation_items  = res_item
            return             = return
       exceptions
            others             = 0.
* Auswertung der aufgetretenen Fehler und Fortschreiben Protokoll
  loop at return.
    error_rs = 'X'.
    move-corresponding return to prot_rs.
    prot_rs-material = res_item-material.
    append prot_rs.
  endloop.
* Reservierungsnummer merken
  if h_rsnum ne space.
    rs_exist = 'X'.
    object_rs-number = h_rsnum.
    append object_rs.
  endif.
  clear: res_item, res_head, return.
  refresh: res_item, return.
endform.
*&---------------------------------------------------------------------
*       Erzeugen Kundenauftrag
*----------------------------------------------------------------------
form create_sd_order.
  sort object_po by katalog number.
  loop at object_po where katalog ne space.
* f黮len 躡ergabefelder / - tabelle f黵 SD
    sd_index = sy-tabix.
    field-set 'PO_NUMBER' 1 object_po-number.
    next = 0.
    loop at ban where katalog eq object_po-katalog.
      add 1 to next.
      edi_quan = ban-quantity.
      condense edi_quan.
      edi_date = ban-deliv_date.
      perform unit_of_measure_to_iso using ban-unit.
      field-set 'ORDER_ITEM-QUANTITY'   next edi_quan.
      field-set 'ORDER_ITEM-MATERIAL'   next ban-vend_mat.
      field-set 'ORDER_ITEM-UNIT_ISO'   next ban-unit.
      field-set 'ORDER_ITEM-DELIV_DATE' next edi_date.
    endloop.
* zur點ksetzen Katalog in OBJECT_PO -> diese PO ist bereits weg
    sd_katalog = object_po-katalog.
    clear object_po-katalog.
    modify object_po index sd_index.
    perform sd_browser_redirect_post.
  endloop.
* keine weiteren POs mehr ans SD zu 黚ergeben
  if sy-subrc ne 0.
    no_more_po = 'X'.
  endif.
endform.
*&---------------------------------------------------------------------*
*&      Konvertieren ISO-Currency in internes SAP-Format
*&---------------------------------------------------------------------*
form currency_to_sap using cts_currency.
  data: h_waers like ekko-waers.
  data: h_currency(3).
  h_currency = cts_currency.
  clear exitflag.
  call function 'CURRENCY_CODE_ISO_TO_SAP'
       exporting
            iso_code  = h_currency
       importing
            sap_code  = h_waers
       exceptions
            not_found = 1
            others    = 2.
* Konvertierung erfolgreich - Feld umsetzen
  if sy-subrc eq 0.
    cts_currency = h_waers.
  else.
    exitflag = 'X'.
  endif.
endform.
*&---------------------------------------------------------------------*
*&      Form  DETERMINE_MATERIAL_INFORECORD
*&---------------------------------------------------------------------*
*       Ermitteln Materialstamm 黚er Lieferantenmaterial aus Infosatz
*----------------------------------------------------------------------*
form determine_material_inforecord using dmi_vendormat
                                         dmi_vendor
                                         dmi_material
                                         dmi_info_rec.
* local table
  data heina like bapieina occurs 1 with header line.
  clear: dmi_material, dmi_info_rec.
* determine material via vendor material number and vendor number
  call function 'BAPI_INFORECORD_GETLIST'
       exporting
            vendor             = dmi_vendor
            vend_mat           = dmi_vendormat
            purchorg_data      = ' '
            general_data       = 'X'
       tables
            inforecord_general = heina
            return             = return
       exceptions
            others             = 0.
  read table heina index 1.
  check sy-subrc eq 0.
  dmi_material = heina-material.
  dmi_info_rec = heina-info_rec.
endform.
*&---------------------------------------------------------------------*
*&      Form  DETERMINE_MATERIAL_MANUFACT
*&---------------------------------------------------------------------*
*       Ermitteln Materialstamm 黚er Herstellerteilenummer und -Code
*----------------------------------------------------------------------*
form determine_material_manufact using dmm_manufactmat
                                       dmm_manufactcode
                                       dmm_material.
* local tables
  data hmfrpn like bapimatmfrpn occurs 1 with header line.
  data hmat like bapimatlst occurs 1 with header line.
* determine vendor via the manufacturer code
  clear: cred_exp, cred_imp.
  refresh: cred_exp, cred_imp.
  cred_exp-comp_code  = global-co_code.
  cred_exp-tabname    = 'LFA1'.
  cred_exp-fieldname  = 'EMNFR'.
  cred_exp-fieldvalue = dmm_manufactcode.
  append cred_exp.
* determine internal manufacturer
  call function 'BAPI_CREDITOR_FIND'
       tables
            selopt_tab = cred_exp
            result_tab = cred_imp
       exceptions
            others     = 0.
  read table cred_imp index 1.
  check sy-subrc eq 0.
  check not cred_imp-vendor_no is initial.
* determine material master with the vendornumber and manufactmat
  refresh hmfrpn.
  hmfrpn-manu_mat = dmm_manufactmat.
  hmfrpn-mfr_no   = cred_imp-vendor_no.
  append hmfrpn.
  ban-mfr_no = cred_imp-vendor_no.
  call function 'BAPI_MATERIAL_GETLIST'
       tables
            manufacturerpartnumb = hmfrpn
            matnrlist            = hmat
       exceptions
            others               = 0.
  read table hmat index 1.
  check sy-subrc eq 0.
  dmm_material = hmat-material.
endform.
*&---------------------------------------------------------------------
*       F黮len Kontierungsblockdaten zur Pr黤ung
*----------------------------------------------------------------------
form fill_bapicobl.
  clear bapicobl.
  bapicobl-pstng_date    = sy-datlo.
  bapicobl-doc_date      = sy-datlo.
  bapicobl-comp_code     = global-co_code.
  bapicobl-bus_area      = bapipogna-bus_area.
  bapicobl-gl_account    = bapipogna-g_l_acct.
  bapicobl-func_area     = bapipogna-func_area.
  bapicobl-func_area_long = bapipogna-func_area_long.
  bapicobl-co_area       = bapipogna-co_area.
  bapicobl-costcenter    = bapipogna-cost_ctr.
  bapicobl-orderid       = bapipogna-order_no.
  bapicobl-cost_obj      = bapipogna-cost_obj.
  bapicobl-profit_ctr    = bapipogna-profit_ctr.
  bapicobl-wbs_element   = bapipogna-wbs_elem_e.
  bapicobl-network       = bapipogna-network.
  bapicobl-assetmainno   = bapipogna-asset_no.
  bapicobl-assetsubno    = bapipogna-sub_number.
  bapicobl-plant         = global-plant.
*BAPICOBL-material     = bapieban-material.  "nur in Position
  bapicobl-sales_ord     = bapipogna-sd_doc.
  bapicobl-sched_line    = bapipogna-sdoc_item.
  bapicobl-s_ord_item    = bapipogna-sched_line.
  bapicobl-rec_ind       = bapipogna-rec_ind.
  bapicobl-part_acct     = bapipogna-part_acct.
  bapicobl-funds_ctr     = bapipogna-funds_ctr.
  bapicobl-cmmt_item     = bapipogna-cmmt_item.
  bapicobl-cmmt_item_long = bapipogna-cmmt_item_long.
  bapicobl-fund          = bapipogna-fund.
endform.
*&---------------------------------------------------------------------
*       躡ernehmen Kontierungsblockdaten nach der Pr黤ung
*----------------------------------------------------------------------
form fill_bapipogna.
  bapipogna-bus_area   = bapicobl-bus_area.
  bapipogna-g_l_acct   = bapicobl-gl_account.
  bapipogna-func_area  = bapicobl-func_area.
  bapipogna-func_area_long = bapicobl-func_area_long.
  bapipogna-co_area    = bapicobl-co_area.
  bapipogna-cost_ctr   = bapicobl-costcenter.
  bapipogna-order_no   = bapicobl-orderid.
  bapipogna-cost_obj   = bapicobl-cost_obj.
  bapipogna-profit_ctr = bapicobl-profit_ctr.
  bapipogna-wbs_elem_e = bapicobl-wbs_element.
  bapipogna-network    = bapicobl-network.
  bapipogna-asset_no   = bapicobl-assetmainno.
  bapipogna-sub_number = bapicobl-assetsubno.
  bapipogna-sd_doc     = bapicobl-sales_ord.
  bapipogna-sdoc_item  = bapicobl-sched_line.
  bapipogna-sched_line = bapicobl-s_ord_item.
  bapipogna-rec_ind    = bapicobl-rec_ind.
  bapipogna-part_acct  = bapicobl-part_acct.
  bapipogna-funds_ctr  = bapicobl-funds_ctr.
  bapipogna-cmmt_item  = bapicobl-cmmt_item.
  bapipogna-cmmt_item_long = bapicobl-cmmt_item_long.
  bapipogna-fund       = bapicobl-fund.
endform.
*&---------------------------------------------------------------------
*       F黮len Bestellkopfdaten
*----------------------------------------------------------------------
form fill_po_head.
  po_head-doc_type  = zug-doc_type.
  po_head-purch_org = zug-purch_org.
  po_head-pur_group = ban-pur_group.
  po_head-agreement = zug-agreement.
  po_head-vendor    = zug-fixed_vend.
  po_head-created_by = ban-preq_name.
  if po_head-created_by eq space.
    po_head-created_by = sy-uname.
  endif.
endform.
*&---------------------------------------------------------------------
*       F黮len Bestellpositionsdaten
*----------------------------------------------------------------------
form fill_po_item.
  move-corresponding ban to po_item.
  if ban-po_price ne space.            "88951/KB
    po_item-po_price = 'X'.            "88951/KB
  endif.                               "88951/KB
  po_item-po_item = ban-preq_item.
  po_item-agreement =  zug-agreement.
  po_item-agmt_item =  zug-agmt_item.
  po_item-info_rec =   zug-info_rec.
  po_item-orderpr_un = po_item-unit.   "106225/KB
  perform value_to_bapicurr using ban-preis po_item-net_price
                                  ban-currency.
  append po_item.
endform.
*&---------------------------------------------------------------------
*       F黮len Bestelleinteilungsdaten
*----------------------------------------------------------------------
form fill_po_sched.
  po_sched-po_item = ban-preq_item.
  po_sched-serial_no = '01'.
  po_sched-del_datcat = ban-del_datcat.
  po_sched-deliv_date = ban-deliv_date.
  po_sched-quantity   = ban-quantity.
  append po_sched.
endform.
*&---------------------------------------------------------------------
*       F黮len Bestellkontierungsdaten
*----------------------------------------------------------------------
form fill_po_acct.
  move-corresponding bsc to po_acct.
  po_acct-po_item = ban-preq_item.
  po_acct-quantity = ban-quantity.
  append po_acct.
endform.
*&---------------------------------------------------------------------
*       F黮len Bestelltexte
*----------------------------------------------------------------------
form fill_po_text.
  move-corresponding bantx to po_text.
  po_text-po_item = ban-preq_item.
  append po_text.
endform.
*&---------------------------------------------------------------------*
*&      Form  FILL_REQ_ITEM
*&---------------------------------------------------------------------*
*       F黮len Positionsdaten in 躡ergabetabellen f黵 BAPI..CREATE
*----------------------------------------------------------------------*
form fill_req_item.
  move-corresponding ban to req_item.
  if req_item-manuf_prof is initial.
    clear req_item-pur_mat.
  endif.
  if req_item-preq_name eq space.
    req_item-preq_name = sy-uname.
  endif.
  perform value_to_bapicurr using ban-preis req_item-c_amt_bapi
                                  ban-currency.
  append req_item.
endform.
*&---------------------------------------------------------------------*
*&      Form  FILL_REQ_ACCT
*&---------------------------------------------------------------------*
*       F黮len Kontierungsdaten Banf in 躡ergabetabelle f黵 BAPI..CREATE
*----------------------------------------------------------------------*
form fill_req_acct.
  move-corresponding bsc to req_acct.
  append req_acct.
endform.
*&---------------------------------------------------------------------*
*&      Form  FILL_REQ_TEXT
*&---------------------------------------------------------------------*
*       F黮len Banftexte in 躡ergabetabelle f黵 BAPI..CREATE
*----------------------------------------------------------------------*
form fill_req_text.
  move-corresponding bantx to req_text.
  append req_text.
endform.
*&---------------------------------------------------------------------*
*&      Form  FILL_RES_HEAD
*&---------------------------------------------------------------------*
*       F黮len Kopfdaten Reservierung in 躡ergabestruktur f黵 CREATE
*----------------------------------------------------------------------*
form fill_res_head.
  move-corresponding bsc to res_head.
  res_head-created_by = ban-preq_name.
  res_head-res_no     = ban-preq_no.
  res_head-move_type  = global-move_type.
  res_head-res_date   = sy-datlo.
  res_head-plant      = ban-plant.
  if res_head-created_by eq space.
    res_head-created_by = sy-uname.
  endif.
endform.
*&---------------------------------------------------------------------*
*&      Form  FILL_RES_ITEM
*&---------------------------------------------------------------------*
*       F黮len Positionsdaten Reservierung in 躡ergabetabelle f黵 CREATE
*----------------------------------------------------------------------*
form fill_res_item.
  move-corresponding bsc to res_item.
  res_item-material   = ban-material.
  res_item-short_text = ban-short_text.
  res_item-plant      = ban-plant.
  res_item-store_loc  = ban-store_loc.
  res_item-batch      = ban-batch.
  res_item-req_date   = ban-deliv_date.
  res_item-quantity   = ban-quantity.
  res_item-unit       = ban-unit.
  if res_item-gr_rcpt eq space.
    res_item-gr_rcpt = sy-uname.
  endif.
  append res_item.
endform.
*&---------------------------------------------------------------------*
*&      Form  FILL_XEKPO
*&---------------------------------------------------------------------*
*       versorgen interne Tabelle XEKPO mit internen Werten
*----------------------------------------------------------------------*
form fill_xekpo tables   po_itemdet structure bapiekpo
                         xekpo structure ekpo
                using    object_po-salesdocument.
  xekpo-mandt = sy-mandt.
  xekpo-ebeln = po_itemdet-po_number.
  xekpo-ebelp = po_itemdet-po_item.
  xekpo-labnr = object_po-salesdocument.
  xekpo-idnlf = po_itemdet-vend_mat.
  append xekpo.
endform.
*&---------------------------------------------------------------------*
*&      Ermitteln g黮tige Kataloge
*&---------------------------------------------------------------------*
form get_catalogs using gca_mat_grp gca_plant.
  refresh: h_cat, catalogs.
  clear  : h_cat, catalogs.
  call function 'BAPI_PROCOPERATION_GETCATALOGS'
       exporting
            material_group = gca_mat_grp
            plant          = gca_plant
       tables
            valid_catalogs = h_cat
            return         = return.
  loop at h_cat.
    catalogs-katalog = h_cat-catalog.
    catalogs-lifnr   = h_cat-vendor.
    catalogs-ekorg   = h_cat-purch_org.
    catalogs-sd_catalog = h_cat-sd_catalog.
    catalogs-katbez  = h_cat-cat_text.
    append catalogs.
  endloop.
endform.
*&---------------------------------------------------------------------*
*&      Ermitteln Hausw鋒rung als Vorschlagsw鋒rung
*&---------------------------------------------------------------------*
form get_currency.
  data: begin of compdeta.
          include structure bapi0002_2.
  data: end of compdeta.
  check global-currency is initial.
  check not global-co_code is initial.
  call function 'BAPI_COMPANYCODE_GETDETAIL'
       exporting
            companycodeid      = global-co_code
       importing
            companycode_detail = compdeta.
  global-currency = compdeta-currency.
endform.
*&---------------------------------------------------------------------*
*&      Ermitteln Buchungskreis aus GLOBAL-PLANT
*&---------------------------------------------------------------------*
form get_company_code.
  check global-co_code is initial.
  if global-plant ne t001w-werks.
    select single * from t001w where werks eq global-plant.
    check sy-subrc eq 0.
  endif.
  if t001k-bwkey ne t001w-bwkey.
    select single * from t001k where bwkey eq t001w-bwkey.
    check sy-subrc eq 0.
  endif.
  global-co_code = t001k-bukrs.
endform.
*&---------------------------------------------------------------------*
*&      Ermitteln Kundennummer f黵 SD-Katalog
*&---------------------------------------------------------------------*
form get_customer_number.
  clear: bapi1008_5, bapireturn, customer_no.
* read the company code level date for the customer number at vendor
  call function 'BAPI_CREDITOR_GETDETAIL'
       exporting
            creditorid              = sel_vendor
            companycode             = global-co_code
       importing
            creditor_company_detail = bapi1008_5
            return                  = bapireturn
       exceptions
            others                  = 0.
* if no error occured - move customer number in the internal field
  check bapireturn-type ne 'E'.
  customer_no = bapi1008_5-act_at_ven.
endform.
*&---------------------------------------------------------------------*
*&      Ermitteln F4-Hilfen f黵 versch. Felder
*&---------------------------------------------------------------------*
form get_helpvalues tables f4_tab1 structure bapif4b
                           f4_tab2 structure bapif4c
                           f4_tab3 structure bapif4d
                           f4_tab4 structure bapif4e
                    using  ghv_objname
                           ghv_method
                           ghv_parameter
                           ghv_field.
* call BAPI for the helpvalues
  call function 'BAPI_HELPVALUES_GET'
       exporting
            objname                    = ghv_objname
            method                     = ghv_method
            parameter                  = ghv_parameter
            field                      = ghv_field
       tables
            selection_for_helpvalues   = f4_tab1
            helpvalues                 = f4_tab2
            values_for_field           = f4_tab3
            description_for_helpvalues = f4_tab4
       exceptions
            others                     = 0.
endform.
*&---------------------------------------------------------------------*
*&      Erzeugen Textzeilen f黵 neue Position
*&---------------------------------------------------------------------*
form init_bantx.
  clear bantx.
  do 3 times.
    bantx-preq_item = ban-preq_item.
    append bantx.
  enddo.
endform.
*&---------------------------------------------------------------------*
*&      Erzeugen Kontierungszeile f黵 neue Position
*&---------------------------------------------------------------------*
form init_bsc.
  clear bankey.
  bankey-preq_item = ban-preq_item.
  read table ban with key bankey binary search.
  check sy-subrc eq 0.
  clear bsckey.
  bsckey-preq_item = ban-preq_item.
  bsckey-serial_no = '01'.
  read table bsc with key bsckey binary search.
  h_index1 = sy-tabix.
  if sy-subrc ne 0.
    move-corresponding bsckey to bsc.
    move-corresponding global to bsc.
    if global-g_l_acct is initial.
      clear h_wbs_elem.
* Aufruf zu einem sp鋞eren Release auf BAPI umstellen - mom. nicht da
      call function 'ME_ACCOUNT_ASSIGNMENT'
           exporting
                i_matnr      = ban-pur_mat
                i_werks      = ban-plant
                i_bwtar      = space
                i_sobkz      = space
                i_vbeln      = space
                i_vbelp      = '000000'
                i_ps_psp_pnr = h_wbs_elem
                i_kzbws      = space
                i_knttp      = ban-acctasscat
                i_matkl      = ban-mat_grp
           importing
                e_sakto      = bsc-g_l_acct
           exceptions
                others       = 0.
    endif.
    bsc-preq_qty = ban-quantity.
    insert bsc index h_index1.
  endif.
endform.
*&---------------------------------------------------------------------
*       Globale Daten initialisieren
*----------------------------------------------------------------------
form init_global_data.
  get parameter id 'WRK' field h_werks.
  if bapipogn-plant is initial.
    bapipogn-plant = h_werks.
  endif.
  get parameter id 'EKG' field h_ekgrp.
  if bapipogn-pur_group is initial.
    bapipogn-pur_group = h_ekgrp.
  endif.
  get parameter id 'WGR' field h_matkl.
  if bapipogn-mat_grp is initial.
    bapipogn-mat_grp = h_matkl.
  endif.
  get parameter id 'LAG' field h_lgort.
  if bapipogn-store_loc is initial.
    bapipogn-store_loc = h_lgort.
  endif.
  get parameter id 'CNT' field h_kostl.
  if bapipogna-cost_ctr is initial.
    bapipogna-cost_ctr = h_kostl.
  endif.
  get parameter id 'BSA' field h_bsart.
  if bapipogn-doc_type is initial.
    bapipogn-doc_type = h_bsart.
  endif.
  get parameter id 'BWA' field h_bwart.
  if bapipogn-move_type is initial.
    bapipogn-move_type = h_bwart.
  endif.
  if bapipogn-acctasscat is initial.
    bapipogn-acctasscat = 'K'.
  endif.
  if bapipogn-preq_name is initial.
    bapipogn-preq_name = sy-uname.
  endif.
  if bapipogna-gr_rcpt is initial.
    bapipogna-gr_rcpt = sy-uname.
  endif.
  if bapipogna-g_l_acct is initial.
    clear h_wbs_elem.
* Aufruf zu einem sp鋞eren Release auf BAPI umstellen - mom. nicht da
    call function 'ME_ACCOUNT_ASSIGNMENT'
         exporting
              i_matnr      = space
              i_werks      = bapipogn-plant
              i_bwtar      = space
              i_sobkz      = space
              i_vbeln      = space
              i_vbelp      = '000000'
              i_ps_psp_pnr = h_wbs_elem
              i_kzbws      = space
              i_knttp      = bapipogn-acctasscat
              i_matkl      = bapipogn-mat_grp
         importing
              e_sakto      = bapipogna-g_l_acct
         exceptions
              others       = 0.
  endif.
endform.
*&---------------------------------------------------------------------*
*&      Verf黦barkeitspr黤ung Material anstossen
*&---------------------------------------------------------------------*
form material_availability using maa_item
                                 maa_material
                                 maa_plant
                                 maa_quantity
                                 maa_unit
                                 maa_delivdate.
  clear: xwmdvsx, xwmdvex.
  refresh: xwmdvsx, xwmdvex.
* Zur點ksetzen AVAIL zur Position
  loop at avail where preq_item eq maa_item.
    delete avail.
  endloop.
* F黮len 躡ergabestrukturen
  xwmdvsx-req_date = maa_delivdate.
  xwmdvsx-req_qty  = maa_quantity.
  append xwmdvsx.
* Aufruf Verf黦barkeitspr黤ung
  call function 'BAPI_MATERIAL_AVAILABILITY'
       exporting
            plant         = maa_plant
            material      = maa_material
            unit          = maa_unit
            check_rule    = '03'
       importing
            endleadtme    = end_rlt
            av_qty_plt    = avail_qty_plant
            dialogflag    = not_available
       tables
            wmdvsx        = xwmdvsx
            wmdvex        = xwmdvex
       exceptions
            error_message = 1.
* zuerst angeforderte Menge in Tabelle eintragen, bzw. 1. Eintrag mod.
  loop at avail where preq_item eq maa_item.
    avail-preq_item = maa_item.
    avail-select = 'X'.
    avail-menge = maa_quantity.
    avail-lfdat = maa_delivdate.
    avail-available = 'No'.
    avail-unit = maa_unit.
    modify avail index sy-tabix.
    exit.
  endloop.
  if sy-subrc ne 0.
    avail-preq_item = maa_item.
    avail-select = 'X'.
    avail-menge = maa_quantity.
    avail-lfdat = maa_delivdate.
    avail-available = 'No'.
    avail-unit = maa_unit.
    append avail.
  endif.
  clear h_menge.
* nun die Daten aus der Verf黦barkeit hinten anh鋘gen
  loop at xwmdvex.
    avail_key-preq_item = maa_item.
    avail_key-select = 'X'.
    avail_key-menge = xwmdvex-com_qty + h_menge.
    avail_key-lfdat = xwmdvex-com_date.
    read table avail with key avail_key.
    if sy-subrc ne 0.
      move-corresponding avail_key to avail.
      clear avail-select.
      avail-available = 'Yes'.
      if not avail-menge is initial.
        avail-unit = maa_unit.
        append avail.
      endif.
    else.
      avail-available = 'Yes'.
      modify avail index sy-tabix.
    endif.
    h_menge = avail_key-menge.
  endloop.
endform.
*&---------------------------------------------------------------------*
*&      Verf黦barkeitspr黤ung Material anstossen und Daten 黚ernehmen
*&---------------------------------------------------------------------*
form material_availability_check.
* Aufruf Verf黦barkeit mit ge鋘derter Menge oder Termin
  perform material_availability using ban-preq_item
                                      ban-material
                                      ban-plant
                                      ban-quantity
                                      ban-unit
                                      ban-deliv_date.
* Pr黤en, ob diese Menge zum gew黱schten Termin verf黦bar -> Ampel
  avail_key-preq_item = ban-preq_item.
  avail_key-select = 'X'.
  avail_key-menge = ban-quantity.
  avail_key-lfdat = ban-deliv_date.
  read table avail with key avail_key.
  if sy-subrc eq 0 and
     avail-available eq 'Yes'.
    ban-available = 'X'.
    ban-avail_qty = ban-quantity.
  endif.
endform.
*&---------------------------------------------------------------------*
*&      Lesen Material mit Materialnummer
*&---------------------------------------------------------------------*
form material_get using h_matnr.
  clear: no_material, no_status_b.
* pr黤en, ob Material bereits gelesen
  read table mat_tab with key h_matnr binary search.
* wenn nicht, nachlesen
  if sy-subrc ne 0.
    h_tabix = sy-tabix.
    clear: mtcom, mt06e, mtcor.
    mtcom-kenng = 'MT06E'.
    mtcom-matnr = h_matnr.
    mtcom-werks = global-plant.
    mtcom-spras = sy-langu.
    mtcom-pstat = 'EBD'.
    mtcom-kzspr = 'X'.
    mtcom-spr_meins = 'X'.
    mtcom-kzmpn = 'X'.
    call function 'MATERIAL_READ'
         exporting
              schluessel    = mtcom
         importing
              matdaten      = mt06e
              return        = mtcor
         tables
              seqmat01      = tdummy
         exceptions
              error_message = 01.
* Material nicht vorhanden
    if mt06e is initial or
       sy-subrc ne 0.
      no_material = 'X'.
      exit.
    endif.
* Buchhaltungssicht ist Minimum
    if mtcom-pstat ca 'B' and mtcor-fstat ca 'B'.
      no_status_b = 'X'.
      exit.
    endif.
* sichern Daten in interner Tabelle
    mat_tab-pur_mat  = h_matnr.
    mat_tab-material = mt06e-bmatn.
    if mt06e-mprof is initial.
      mat_tab-material = h_matnr.
    endif.
    mat_tab-manuf_prof = mt06e-mprof.
    mat_tab-short_text = mt06e-maktx.
    mat_tab-mat_grp = mt06e-matkl.
    mat_tab-unit = mt06e-meins.
    if mt06e-vprsv ne space.
      mat_tab-c_amt_bapi = mt06e-verpr.
      if mt06e-vprsv eq 'S'.
        mat_tab-c_amt_bapi = mt06e-stprs.
      endif.
      mat_tab-price_unit = mt06e-peinh.
    endif.
    mat_tab-plant = global-plant.
    mat_tab-pur_group = mt06e-ekgrp.
    if mat_tab-pur_group eq space.
      mat_tab-pur_group = global-pur_group.
    endif.
    insert mat_tab index h_tabix.
  endif.
endform.
*&---------------------------------------------------------------------
*       Erzeugen neue leere Positionszeilen
*----------------------------------------------------------------------
form new_items.
  sort ban by preq_no preq_item.
  clear ban.
  h-tabix = 1.
  h_bnfpo = 0.
* diese Abfrage nur sicherheitshalber
  if ban[] is initial.
    do 10 times.
      h_bnfpo   = h_bnfpo + 1.
      ban-preq_item = h_bnfpo.
      ban-quantity  = 1.
      ban-c_amt_bapi = 1.
      ban-preis      = 1.
      ban-deliv_date = sy-datlo.
      ban-del_datcat = '1'.
      append ban.
    enddo.
  else.
* ab 50 Positionen geht nix mehr - Fehler und tsch
    describe table ban lines count1.
    if count1 ge 50.
      message s067.
      clear ok-code.
      exit.
    endif.
    read table ban index count1.
    h_bnfpo = ban-preq_item.
    clear ban.
* wenn noch was geht, werden einfach 5 neue aufgemacht...
    do 5 times.
      h_bnfpo   = h_bnfpo + 1.
      ban-preq_item = h_bnfpo.
      ban-quantity  = 1.
      ban-c_amt_bapi = 1.
      ban-preis      = 1.
      ban-deliv_date = sy-datlo.
      ban-del_datcat = '1'.
      append ban.
    enddo.
  endif.
endform.
*&---------------------------------------------------------------------
*       Fortschreiben Kundenauftragsnummer in den Bestellungen
*----------------------------------------------------------------------
form po_confirmation.
  loop at object_po where salesdocument ne space.
    clear: po_itemdet, xekpo.
    refresh: po_itemdet, xekpo.
* sperren Bestellung
    call function 'MM_ENQUEUE_DOCUMENT'
         exporting
              i_ebeln = object_po-number
              i_bstyp = 'F'.
* besorgen Positionsdaten
    call function 'BAPI_PO_GETDETAIL'
         exporting
              purchaseorder = object_po-number
         tables
              po_items      = po_itemdet
              return        = return.
* f黮len interne Struktur f黵 Position plus f黮len SD-Auftragsnummer
    loop at po_itemdet.
      perform fill_xekpo tables po_itemdet
                                xekpo
                         using  object_po-salesdocument.
    endloop.
    if not xekpo[] is initial.
* verbuchen 膎derung aber ohne 膎derungsbeleg
      call function 'ME_UPDATE_DOCUMENT_RESPONSE' "IN UPDATE TASK
           tables
                xekpo          = xekpo
           exceptions
                update_mistake = 01.
    else.
* falls keine Positionen vorhanden, entsperren Bestellung
      call function 'DEQUEUE_EMEKKOE'
           exporting
                ebeln = object_po-number.
    endif.
  endloop.
endform.
*&---------------------------------------------------------------------
*       Neuvergeben Positionsnummern nach L鰏chen einer Position
*----------------------------------------------------------------------
form prepare_preq_item.
  data: h_preqitem like bapieban-preq_item.
  sort ban by preq_no preq_item.
  next = 0.
  loop at ban.
    add 1 to next.
    check ban-preq_item ne next.
    h_preqitem = ban-preq_item.
    ban-preq_item = next.
    modify ban index sy-tabix.
    loop at bsc where preq_item eq h_preqitem.
      bsc-preq_item = ban-preq_item.
      modify bsc index sy-tabix.
    endloop.
    loop at bantx where preq_item eq h_preqitem.
      bantx-preq_item = ban-preq_item.
      modify bantx index sy-tabix.
    endloop.
    if ban-update ne space.
      h_banpos = ban-preq_item.
    endif.
  endloop.
endform.
*&---------------------------------------------------------------------
*       Lesen interne Tabelle BAN mit Key
*----------------------------------------------------------------------
form read_ban_with_key using rbw_item.
  clear bankey.
  bankey-preq_item = rbw_item.
  read table ban with key bankey.
endform.
*&---------------------------------------------------------------------
*       Lesen interne Tabelle BSC mit Key
*----------------------------------------------------------------------
form read_bsc_with_key using rbw_item.
  clear bsckey.
  bsckey-preq_item = rbw_item.
  bsckey-serial_no = '01'.
  read table bsc with key bsckey.
endform.
*&---------------------------------------------------------------------
*       Lesen vorhandene Bestellungen zum User f黵 Status
*----------------------------------------------------------------------
form read_po_items.
  check read_po ne space.
* zur點ksetzen alle betroffenen Tabellen
  clear: spo_head, spo_item, po_item, spo_acct, spo_sched, spo_hist,
         spo_hist_sum.
  refresh: spo_head, spo_item, po_item, spo_acct, spo_sched, spo_hist,
           spo_hist_sum.
* lesen alle PO-items zum User
  call function 'BAPI_PO_GETITEMS'
       exporting
            doc_date        = sel_date
            preq_name       = sy-uname
            with_po_headers = 'X'
       tables
            po_headers      = spo_head
            po_items        = po_item
            return          = return
       exceptions
            others          = 0.
* l鰏chen der Dienstleistungen
  loop at po_item.
    if po_item-item_cat = '9'.
      delete po_item.
    endif.
  endloop.
  sort spo_head by po_number.
  clear:  h_ebeln,
          po_details.
  refresh: po_details.
* Aufruf GETDETAIL wegen Ampel auf 躡ersichtsbild - Historie
* Wird auf dem 躡ersichtsbild das Detailbild zur Bestellung gew鋒lt,
* mu?nicht noch der GETDETAIL aufgerufen werden - merken in int. Tab.
  loop at po_item.
* nur beim Gruppenwechsel den GETDETAIL aufrufen
    if po_item-po_number ne h_ebeln and
       h_ebeln ne space.
      clear: po_itemdet, po_acct, po_sched, po_hist, po_hist_sum.
      refresh: po_itemdet, po_acct, po_sched, po_hist, po_hist_sum.
      call function 'BAPI_PO_GETDETAIL'
           exporting
                purchaseorder              = h_ebeln
                items                      = 'X'
                account_assignment         = 'X'
                schedules                  = 'X'
                history                    = 'X'
           tables
                po_items                   = po_itemdet
                po_item_account_assignment = po_acct
                po_item_schedules          = po_sched
                po_item_history            = po_hist
                po_item_history_totals     = po_hist_sum
                return                     = return
           exceptions
                others                     = 0.
* merken aller Daten zur Bestellung in internen Tabellen f黵 Detailbild
      read table spo_head with key h_ebeln binary search.
      move-corresponding spo_head to spo_item.
      loop at po_itemdet.
        detailkey-number = po_itemdet-po_number.
        detailkey-position = po_itemdet-po_item.
        read table po_details with key detailkey.
        if sy-subrc eq 0.
          move-corresponding po_itemdet to spo_item.
          spo_item-preq_name = sy-uname.
          append spo_item.
        endif.
      endloop.
      loop at po_acct.
        spo_acct-po_number = po_itemdet-po_number.
        detailkey-number = po_itemdet-po_number.
        detailkey-position = po_acct-po_item.
        read table po_details with key detailkey.
        if sy-subrc eq 0.
          move-corresponding po_acct to spo_acct.
          append spo_acct.
        endif.
      endloop.
      loop at po_sched.
        spo_sched-po_number = po_itemdet-po_number.
        detailkey-number = po_itemdet-po_number.
        detailkey-position = po_sched-po_item.
        read table po_details with key detailkey.
        if sy-subrc eq 0.
          move-corresponding po_sched to spo_sched.
          append spo_sched.
        endif.
      endloop.
      loop at po_hist.
        spo_hist-po_number = po_itemdet-po_number.
        detailkey-number = po_itemdet-po_number.
        detailkey-position = po_hist-po_item.
        read table po_details with key detailkey.
        if sy-subrc eq 0.
          move-corresponding po_hist to spo_hist.
          append spo_hist.
        endif.
      endloop.
      loop at po_hist_sum.
        spo_hist_sum-po_number = po_itemdet-po_number.
        detailkey-number = po_itemdet-po_number.
        detailkey-position = po_hist_sum-po_item.
        read table po_details with key detailkey.
        if sy-subrc eq 0.
          move-corresponding po_hist_sum to spo_hist_sum.
          append spo_hist_sum.
        endif.
      endloop.
    endif.
    h_ebeln = po_item-po_number.
    po_details-po_number = po_item-po_number.
    po_details-po_item = po_item-po_item.
    append po_details.
  endloop.
* Nochmal mit letzter Bestellung
  if h_ebeln ne space.
    clear: po_itemdet, po_acct, po_sched, po_hist, po_hist_sum.
    refresh: po_itemdet, po_acct, po_sched, po_hist, po_hist_sum.
    call function 'BAPI_PO_GETDETAIL'
         exporting
              purchaseorder              = h_ebeln
              items                      = 'X'
              account_assignment         = 'X'
              schedules                  = 'X'
              history                    = 'X'
         tables
              po_items                   = po_itemdet
              po_item_account_assignment = po_acct
              po_item_schedules          = po_sched
              po_item_history            = po_hist
              po_item_history_totals     = po_hist_sum
              return                     = return
         exceptions
              others                     = 0.
* merken aller Daten zur Bestellung in internen Tabellen f黵 Detailbild
    read table spo_head with key h_ebeln binary search.
    move-corresponding spo_head to spo_item.
    loop at po_itemdet.
      detailkey-number = po_itemdet-po_number.
      detailkey-position = po_itemdet-po_item.
      read table po_item with key detailkey.
      if sy-subrc eq 0.
        move-corresponding po_itemdet to spo_item.
        spo_item-preq_name = sy-uname.
        append spo_item.
      endif.
    endloop.
    loop at po_acct.
      spo_acct-po_number = po_itemdet-po_number.
      detailkey-number = po_itemdet-po_number.
      detailkey-position = po_acct-po_item.
      read table po_item with key detailkey.
      if sy-subrc eq 0.
        move-corresponding po_acct to spo_acct.
        append spo_acct.
      endif.
    endloop.
    loop at po_sched.
      spo_sched-po_number = po_itemdet-po_number.
      detailkey-number = po_itemdet-po_number.
      detailkey-position = po_sched-po_item.
      read table po_item with key detailkey.
      if sy-subrc eq 0.
        move-corresponding po_sched to spo_sched.
        append spo_sched.
      endif.
    endloop.
    loop at po_hist.
      spo_hist-po_number = po_itemdet-po_number.
      detailkey-number = po_itemdet-po_number.
      detailkey-position = po_hist-po_item.
      read table po_item with key detailkey.
      if sy-subrc eq 0.
        move-corresponding po_hist to spo_hist.
        append spo_hist.
      endif.
    endloop.
    loop at po_hist_sum.
      spo_hist_sum-po_number = po_itemdet-po_number.
      detailkey-number = po_itemdet-po_number.
      detailkey-position = po_hist_sum-po_item.
      read table po_item with key detailkey.
      if sy-subrc eq 0.
        move-corresponding po_hist_sum to spo_hist_sum.
        append spo_hist_sum.
      endif.
    endloop.
  endif.
* Auswerten f黵 Ampelanzeige (hier wird vorerst nur die WEMNG und WESPS
* in die Auswertung genommen)
  sort spo_hist_sum by po_number po_item.
  loop at spo_item.
    h_index = sy-tabix.
    clear pokey.
    pokey-po_number = spo_item-po_number.
    pokey-po_item   = spo_item-po_item.
    read table spo_hist_sum with key pokey binary search.
    if sy-subrc ne 0.
      spo_item-cl_ind = red.
    else.
      if spo_hist_sum-deliv_qty ge spo_item-quantity or
         spo_hist_sum-blocked_qy ge spo_item-quantity.
        spo_item-cl_ind = green.
      else.
        if spo_hist_sum-deliv_qty eq 0 and
           spo_hist_sum-blocked_qy eq 0.
          spo_item-cl_ind = red.
        else.
          spo_item-cl_ind = yellow.
        endif.
      endif.
    endif.
    modify spo_item index h_index.
  endloop.
  if spo_item[] is initial.
    clear read_po.
  endif.
endform.
*&---------------------------------------------------------------------
*       Lesen Bestellposition f黵  Statusdetailbild
*----------------------------------------------------------------------
form read_po_detail.
* Position mu?in internen Tabellen vorhanden sein - Daten bereitstellen
  sort spo_item by po_number po_item.
  sort spo_head by po_number.
  sort spo_hist_sum by po_number po_item.
  sort spo_acct by po_number po_item.
  sort spo_sched by po_number po_item.
  clear: spo_item, spo_head, spo_hist_sum, spo_sched, spo_acct,
         h_meins1, h_meins2.
  read table spo_item with key pokey binary search.
  h_meins1 = spo_item-unit.
  h_meins2 = spo_item-unit.
  read table spo_head with key pokey-po_number binary search.
  read table spo_hist_sum with key pokey binary search.
  if sy-subrc ne 0.
    clear spo_hist_sum.
  endif.
  read table spo_sched with key pokey binary search.
  if sy-subrc ne 0.
    clear spo_sched.
  endif.
  read table spo_acct with key pokey binary search.
  if sy-subrc ne 0.
    clear spo_acct.
  endif.
endform.
*&---------------------------------------------------------------------
*       Lesen vorhandene Bestellanforderungen zum User f黵 Status
*----------------------------------------------------------------------
form read_requisitions.
  check read_rq ne space.
  clear: req_stat, req_item.
  refresh: req_stat, req_item.
* lesen Positionen
  call function 'BAPI_REQUISITION_GETITEMS'
       exporting
            preq_name               = sy-uname
            preq_date               = sel_date
            closed_items            = closed_items
            partially_ordered_items = 'X'
            assigned_items          = 'X'
       tables
            requisition_items       = req_item
            return                  = return
       exceptions
            others                  = 0.
* f黮len Statustabelle
  loop at req_item.
    move-corresponding req_item to req_stat.
    if req_item-ordered ge req_item-quantity.
      req_stat-cl_ind = green.
    else.
      if req_item-ordered = 0.
        req_stat-cl_ind = red.
      else.
        req_stat-cl_ind = yellow.
      endif.
    endif.
    append req_stat.
  endloop.
  if req_stat[] is initial.
    clear read_rq.
  endif.
endform.
*&---------------------------------------------------------------------
*       Lesen Detail Bestellanforderung f黵 Statusdetailbild
*----------------------------------------------------------------------
form read_requisitions_detail.
* zuerst lesen, ob GETDETAIL bereits aufgerufen
  read table sreq_item with key bankey binary search.
  clear: h_meins1, h_meins2.
  if sy-subrc ne 0.
* falls nicht - nachlesen und in SREQ_ITEM merken
    clear: req_itemdet, req_acct.
    refresh: req_itemdet, req_acct.
    call function 'BAPI_REQUISITION_GETDETAIL'
         exporting
              number                         = req_stat-preq_no
              account_assignment             = 'X'
         tables
              requisition_items              = req_itemdet
              requisition_account_assignment = req_acct
              return                         = return
         exceptions
              others                         = 0.
    loop at req_itemdet.
      read table sreq_item with key bankey binary search.
      if sy-subrc ne 0.
        move-corresponding req_itemdet to sreq_item.
        if not sreq_item-fixed_vend is initial.
          clear bapi1008_4.
          call function 'BAPI_CREDITOR_GETDETAIL'
               exporting
                    creditorid              = sreq_item-fixed_vend
               importing
                    creditor_general_detail = bapi1008_4.
          sreq_item-name = bapi1008_4-name.
        endif.
        insert sreq_item index sy-tabix.
      endif.
    endloop.
    clear sreq_acct.
    loop at req_acct.
      move-corresponding req_acct to sreq_acct.
      append sreq_acct.
    endloop.
    loop at sreq_acct where preq_no eq sreq_item-preq_no
                        and preq_item eq sreq_item-preq_item.
      exit.
    endloop.
    read table sreq_item with key bankey binary search.
* Banf bereits gelesen
  else.
* Kontierung dazulesen - Ausgehend von Einfachkontierung
    clear sreq_acct.
    loop at sreq_acct where preq_no eq sreq_item-preq_no
                        and preq_item eq sreq_item-preq_item.
      exit.
    endloop.
  endif.
  h_meins1 = sreq_item-unit.
  h_meins2 = sreq_item-unit.

endform.
*&---------------------------------------------------------------------
*       Lesen vorhandene Reservierungen zum User f黵 Status
*----------------------------------------------------------------------
form read_reservations.
  check read_rs ne space.
  clear: res_stat, res_stat1.
  refresh: res_stat, res_stat1.
* lesen Reservierungen
  call function 'BAPI_RESERVATION_GETITEMS'
       exporting
            gr_rcpt           = sy-uname
            req_date          = sel_date
            closed_items      = closed_items
       tables
            reservation_items = res_stat1
            return            = return
       exceptions
            others            = 0.
* f黮len Statustabelle
  loop at res_stat1.
    move-corresponding res_stat1 to res_stat.
    if res_stat1-withd_quan ge res_stat1-quantity.
      res_stat-cl_ind = green.
    else.
      if res_stat-withd_quan = 0.
        res_stat-cl_ind = red.
      else.
        res_stat-cl_ind = yellow.
      endif.
    endif.
    append res_stat.
  endloop.
  if res_stat[] is initial.
    clear read_rs.
  endif.
endform.
*&---------------------------------------------------------------------
*       Lesen Detail Reservierung f黵 Statusdetailbild
*----------------------------------------------------------------------
form read_reservations_detail.
* zuerst lesen, ob GETDETAIL bereits aufgerufen
  read table sres_item with key reskey binary search.
  clear h_meins1.
  if sy-subrc ne 0.
* falls nicht - nachlesen und in SRES_ITEM merken
    clear: res_itemdet, bapirkpf.
    refresh: res_itemdet.
    call function 'BAPI_RESERVATION_GETDETAIL'
         exporting
              reservation        = reskey-res_no
         importing
              reservation_header = bapirkpf
         tables
              reservation_items  = res_itemdet
              return             = return
         exceptions
              others             = 0.
    loop at res_itemdet.
      read table sres_item with key reskey binary search.
      if sy-subrc ne 0.
        move-corresponding bapirkpf to sres_item.
        move-corresponding res_itemdet to sres_item.
        insert sres_item index sy-tabix.
      endif.
    endloop.
  endif.
  h_meins1 = sres_item-unit.
endform.
*&---------------------------------------------------------------------
*       Refresh all internal tables and structures
*----------------------------------------------------------------------
form refresh_all.
  clear: mtcom, mtcor, mt06e, bapieban, bapipogn, bapipogna, bapirkpf,
         eban, ebkn, ban, bankey, bsc, bsckey, bantx, bantxweb,
         avail, availweb, avail_key, banwa, new_item, global, material,
         mat_tab, req_item, sreq_item, req_itemdet, req_stat, req_acct,
        sreq_acct, req_text, res_head, res_item, res_itemdet, sres_item,
         res_stat1, res_stat, reskey, po_head, spo_head, po_item,
         spo_item, po_itemdet, po_hist, spo_hist, po_hist_sum,
         spo_hist_sum, xekpo, po_sched, spo_sched, po_acct, spo_acct,
         po_text, pokey, proc, proc_acct, proc_prot, prot_po, prot_rs,
         prot_rq, object_po, object_rs, object_rq, catalogs, h_cat,
         xmw03, h_catalogs, zug, xwmdvsx, xwmdvex, return, redirect,
         h_redirect, sd, sder, f4_bsart, f4_bwart, f4_ekgrp, f4_ekorg,
         f4_matkl, f4_werks, f4_lgort, f4_knttp, f4_meins, f4_waers,
         po_number, salesdocument, my_hook_url, sd_hook_url,
         h_redirect_length, sd_katalog, h_banpos.
  refresh:
         ban, bsc, bantx, bantxweb,
         avail, availweb, new_item,
         mat_tab, req_item, sreq_item, req_itemdet, req_stat, req_acct,
         sreq_acct, req_text, res_item, res_itemdet, sres_item,
         res_stat1, res_stat, spo_head, po_item,
         spo_item, po_itemdet, po_hist, spo_hist, po_hist_sum,
         spo_hist_sum, xekpo, po_sched, spo_sched, po_acct, spo_acct,
         po_text, proc, proc_acct, proc_prot, prot_po, prot_rs,
         prot_rq, object_po, object_rs, object_rq, catalogs, h_cat,
         xmw03, xwmdvsx, xwmdvex, return, redirect,
         h_redirect, sd, sder, f4_bsart, f4_bwart, f4_ekgrp, f4_ekorg,
         f4_matkl, f4_werks, f4_lgort, f4_knttp, f4_meins, f4_waers.
endform.
*&---------------------------------------------------------------------
*       Sichern Daten
*----------------------------------------------------------------------
form save_objects.
* reset interne Tabellen
  clear: proc, proc_acct, object_po, object_rs, object_rq,
         prot_po, prot_rs, prot_rq, sd_katalog.
  refresh: proc, proc_acct, object_po, object_rs, object_rq,
           prot_po, prot_rs, prot_rq.
* f黮len der 躡ergabetabellen f黵 'Entscheidungsbapi'
  loop at ban where update ne space.
    move-corresponding ban to proc.
    proc-catalog = ban-katalog.
    perform value_to_bapicurr using ban-preis proc-c_amt_bapi
                                    ban-currency.
    append proc.
  endloop.
  loop at bsc.
    move-corresponding bsc to proc_acct.
    append proc_acct.
  endloop.
* Analysieren Positionen nach Customizing und Positionsdaten
  if not proc[] is initial.
    call function 'BAPI_PROCOPERATION_GETINFO'
         tables
              procurement_item    = proc
              procurement_account = proc_acct
              return              = return
         exceptions
              others              = 0.
  endif.
* Auswerten Tabellen Proc/Ban und f黮len der einzelnen Objekttabellen
  clear: po_head, po_item, po_sched, po_acct, po_text, return, zug,
         req_item, req_acct, req_text, res_head, res_item, proc_prot,
         error_pr.
  refresh: po_item, po_sched, po_acct, po_text, return,
           req_item, req_acct, req_text, res_item, proc_prot.
  sort proc by obj_to_gen fixed_vend purch_org doc_type agreement
               preq_item.
* Zuerst alle Positionen ohne Objektzuordnung in Protokolltabelle
  loop at proc where obj_to_gen ne req and
                     obj_to_gen ne res and
                     obj_to_gen ne po.
    error_pr = 'X'.
    move-corresponding proc to proc_prot.
    append proc_prot.
  endloop.
* Dann alle Banfpositionen
  loop at proc where obj_to_gen eq req.
    perform read_ban_with_key using proc-preq_item.
    perform fill_req_item.
    loop at bsc where preq_item eq proc-preq_item.
      perform fill_req_acct.
    endloop.
    loop at bantx where preq_item eq proc-preq_item
                  and text_line ne space.
      perform fill_req_text.
    endloop.
  endloop.
  if not req_item[] is initial.
    perform create_requisition.
  endif.
  clear error_rs.
  clear rs_exist.
* Dann alle Reservierungen
  loop at proc where obj_to_gen eq res.
    perform read_ban_with_key using proc-preq_item.
    perform read_bsc_with_key using proc-preq_item.
    perform fill_res_head.
    perform fill_res_item.
* Reservierung pro Position anlegen, wegen der Kontierung im Kopf
    perform create_reservation.
  endloop.
  clear error_po.
  clear po_exist.
* Zuletzt alle Bestellungen abarbeiten
  loop at proc where obj_to_gen eq po.
* Gruppenwechsel in Bezugsquelle abfragen - nur einmal Kopfdaten
    if zug-fixed_vend ne proc-fixed_vend or
       zug-agreement ne proc-agreement or
       zug-purch_org ne proc-purch_org or
       zug-doc_type ne proc-doc_type.
      if not zug is initial.
        clear sd_katalog.
        perform read_ban_with_key using zug-preq_item.
        perform fill_po_head.
        if ban-sd ne space.
          sd_katalog = ban-katalog.
        endif.
        perform create_po.
        clear sd_katalog.
      endif.
    endif.
    move-corresponding proc to zug.
    perform read_ban_with_key using proc-preq_item.
    perform fill_po_item.
    perform fill_po_sched.
    loop at bsc where preq_item eq proc-preq_item.
      perform fill_po_acct.
    endloop.
    loop at bantx where preq_item eq proc-preq_item
                    and text_line ne space.
      perform fill_po_text.
    endloop.
  endloop.
* letzte Bestellung sichern
  check not zug is initial.
  clear sd_katalog.
  perform read_ban_with_key using zug-preq_item.
  perform fill_po_head.
  if ban-sd ne space.
    sd_katalog = ban-katalog.
  endif.
  perform create_po.
  clear sd_katalog.
* Aufruf Kundenauftrag f黵 alle Bestellungen aus SD/Retail
  perform create_sd_order.
endform.
*&---------------------------------------------------------------------
*       Setzen HOOK-URL und rufen SD-Katalog f黵 Buchen
*----------------------------------------------------------------------
form sd_browser_redirect_post.
  clear my_hook_url.
  its-wgateurl my_hook_url.
  field-set 'MM_HOOK_URL' 1 my_hook_url.
  read table redirect with key sd_katalog.
  field-set '~OkCode' 1 'ORMM'.
  its-browser_post redirect-url.
endform.
*&---------------------------------------------------------------------
*       Setzen Subscreens
*----------------------------------------------------------------------
form set_subscreen using sss_frame0 sss_frame1 sss_frame2.
  aw-dynnr_frame_0 = sss_frame0.
  aw-dynnr_frame_1 = sss_frame1.
  aw-dynnr_frame_2 = sss_frame2.
endform.
*&---------------------------------------------------------------------*
*&      膎dern Position 黚er das Detailbild - Verf黦barkeitsdaten
*&---------------------------------------------------------------------*
form store_availability.
  clear bankey.
  bankey-preq_item = bapieban-preq_item.
  read table ban with key bankey binary search.
  h_index = sy-tabix.

* Anstossen Verf黦barkeit bei 膎derung Menge oder Termin
  if ban-material ne space.
    if ban-quantity ne bapieban-quantity or
       ban-deliv_date ne bapieban-deliv_date.
      ban-quantity     = bapieban-quantity.
      ban-deliv_date   = bapieban-deliv_date.
      perform material_availability_check.
      message s047.
    endif.
  endif.
  ban-quantity     = bapieban-quantity.
  ban-deliv_date   = bapieban-deliv_date.
  modify ban index h_index.
endform.
*&---------------------------------------------------------------------
*       Globale Daten merken und in der Struktur GLOBAL sichern
*----------------------------------------------------------------------
form store_global_data.
  move-corresponding bapipogn to global.
  perform get_catalogs using global-mat_grp global-plant.
  perform get_company_code.
  perform get_currency.
  perform check_account_assignment.
  check exitflag eq space.
  move-corresponding bapipogna to global.
  loop at ban where update ne space.
* bei Materialposition Warengruppe nicht aus Global 黚erschreiben
    if ban-pur_mat ne space.
      h_matkl = ban-mat_grp.
    endif.
    move-corresponding global to ban.
    if ban-pur_mat ne space.
      ban-mat_grp = h_matkl.
    endif.
    modify ban.
  endloop.
  loop at bsc.
    move-corresponding global to bsc.
    modify bsc.
  endloop.
endform.
*&---------------------------------------------------------------------*
*&      膎dern Position 黚er das Detailbild - Bezugsquellen
*&---------------------------------------------------------------------*
form store_source_of_supply.
  read table sosweb index select_sos.
  check sy-subrc eq 0.
  clear bankey.
  bankey-preq_item = bapieban-preq_item.
  read table ban with key bankey binary search.
  h_index = sy-tabix.
  ban-fixed_vend   = sosweb-fixed_vend.
  ban-purch_org    = sosweb-purch_org.
  ban-agreement    = sosweb-agreement.
  ban-agmt_item    = sosweb-agmt_item.
  ban-info_rec     = sosweb-info_rec.
* zur點ksetzen Preis黚ernahme in PO, wenn pos. nicht aus Katalog,
* aber nun g黮tige Bezugsquelle sitzt
* bei Katalogpositionen werden nur zus鋞zl. Konditionen aus der
* Bezugsquelle 黚ernommen, aber nicht der Bruttopreis - der aus Katalog
  if not ban-po_price is initial and
     ban-katalog is initial and
     ( not ban-agreement is initial or
       not ban-info_rec is initial ).
    clear ban-po_price.
  endif.
  modify ban index h_index.
endform.
*&---------------------------------------------------------------------*
*&      Form  REFRESH_STATUS_TABLES for new selection
*&---------------------------------------------------------------------*
form refresh_status_tables.
* zur點ksetzen alle betroffenen Tabellen
  clear: spo_head, spo_item, po_item, spo_acct, spo_sched, spo_hist,
         spo_hist_sum, req_stat, req_item, res_stat, res_stat1.
  refresh: spo_head, spo_item, po_item, spo_acct, spo_sched, spo_hist,
           spo_hist_sum, req_stat, req_item, res_stat, res_stat1.
endform.
*&---------------------------------------------------------------------*
*&      Zur點ksetzen  Bezugsquellen
*&---------------------------------------------------------------------*
form reset_source_of_supply.
  clear bankey.
  bankey-preq_item = bapieban-preq_item.
  read table ban with key bankey binary search.
  h_index = sy-tabix.
  clear: ban-fixed_vend,
         ban-purch_org,
         ban-agreement,
         ban-agmt_item,
         ban-info_rec.
  modify ban index h_index.
  clear: bapieban-fixed_vend,
         bapieban-purch_org,
         bapieban-agreement,
         bapieban-agmt_item,
         bapieban-info_rec,
         lfa1-name1.
endform.
*&---------------------------------------------------------------------*
*&      Konvertieren SAP-UNIT in ISO-Unit
*&---------------------------------------------------------------------*
form unit_of_measure_to_iso using uof_unit.
  data: h_unit_iso(3).
  call function 'UNIT_OF_MEASURE_SAP_TO_ISO'
       exporting
            sap_code    = uof_unit
       importing
            iso_code    = h_unit_iso
       exceptions
            not_found   = 1
            no_iso_code = 2
            others      = 3.
* Konvertierung erfolgreich - Feld umsetzen
* fall Konvertierung nicht erfolgreich mit Katalogunit einfach weiter
  if sy-subrc eq 0.
    uof_unit = h_unit_iso.
  endif.
endform.
*&---------------------------------------------------------------------*
*&      Konvertieren ISO-UNIT in internes SAP-Format
*&---------------------------------------------------------------------*
form unit_of_measure_to_sap using uof_unit.
  data: h_unit like t006-msehi.
  clear exitflag.
  call function 'UNIT_OF_MEASURE_ISO_TO_SAP'
       exporting
            iso_code  = uof_unit
       importing
            sap_code  = h_unit
       exceptions
            not_found = 1
            others    = 2.
* Konvertierung erfolgreich - Feld umsetzen
* fall Konvertierung nicht erfolgreich mit Katalogunit einfach weiter
  if sy-subrc eq 0.
    uof_unit = h_unit.
  else.
    exitflag = 'X'.
  endif.
endform.
*&---------------------------------------------------------------------*
*&      Konvertieren interner Wert in BAPICURR-Format
*&---------------------------------------------------------------------*
form value_to_bapicurr using vtb_preis vtb_preqprice vtb_currency.
  call function 'BAPI_CURRENCY_CONV_TO_EXTERNAL'
       exporting
            currency        = vtb_currency
            amount_internal = vtb_preis
       importing
            amount_external = vtb_preqprice
       exceptions
            others          = 1.

endform.
*&---------------------------------------------------------------------*
*&      Konvertieren externer Wert in internes SAP-Format
*&---------------------------------------------------------------------*
form value_to_sap using vts_currency vts_value.
  data: h_price like ekpo-netpr.
  clear exitflag.
  call function 'CURRENCY_AMOUNT_IDOC_TO_SAP'
       exporting
            currency    = vts_currency
            idoc_amount = vts_value
       importing
            sap_amount  = h_price
       exceptions
            others      = 1.
  if sy-subrc eq 0.
    vts_value = h_price.
  else.
    exitflag = 'X'.
  endif.
endform.