Registration

Dear SAP Community Member,
In order to fully benefit from what the SAP Community has to offer, please register at:
http://scn.sap.com
Thank you,
The SAP Community team.
Skip to end of metadata
Go to start of metadata

Description: Tronicus sales order interface:
--------------------------------------------------------------------------------

Error rendering macro 'code': Invalid value specified for parameter 'lang'
************************************************************************
* Program: ZVTRCREATE
* Desc:    Tronicus sales order interface:
*          Allows user to create sales orders from data uploaded into
*          SAP from Excel.
*          The user selects a set of uploaded Excel data for processing
*          (identified by the Excel file name and date).
*          The user is warned if a set has already been processed
*          successfully but may choose to continue.
*          If an error is encountered, no sales orders are created from
*          the set. Instead,
*          - errors are reported
*          - entries with errors are marked with an 'error' status
*          User should correct entries with errors and reset status
*          before attempting to reprocess the set.
************************************************************************
REPORT ZVTRCREATE NO STANDARD PAGE HEADING
                  LINE-SIZE 132.
*include zvtrconstants.
* Result message (as entries may not be processed in numerical sequence,
* the results must be stored for writing out at the end of the process)
TYPES: BEGIN OF TY_RESULT,
         ROWNO     LIKE AZVTRDATA-ROWNO,
         SEQNO     TYPE I,
         VBELN     LIKE VBAK-VBELN,
         TEXT(100)  TYPE C,
       END OF TY_RESULT.
DATA: T_RESULT          TYPE STANDARD TABLE OF TY_RESULT
                             WITH HEADER LINE.
DATA: T_ZVTRDATA       TYPE STANDARD TABLE OF ZVTRDATA
                            WITH HEADER LINE.
DATA: W_AEDAT           LIKE ZVTRDATA-AEDAT,
      W_AEDAT_C(10)     TYPE C,
      W_AENAM           LIKE ZVTRDATA-AENAM,
      W_ANSWER          TYPE C,
      W_COMMIT_OR_ROLLBACK_TXT(100) TYPE C,
      W_MESSAGE(100)    TYPE C,
      W_STATUS          LIKE ZVTRDATA-STATUS,
      W_ZVTRFILE        LIKE ZVTRFILE.
* Booleans
DATA: BEGIN OF W_FLAG,
        COMMIT          TYPE C, "updates should be committed
        DONE_PROCESSED  TYPE C, "S.Ords already created for this rawdata
        DONE_WITH_ERROR TYPE C, "rawdata already processed with errors
        GOOD_CALL       TYPE C, "BAPI called w/o errors
        RAWDATA         TYPE C, "rawdata read successfully
        TESTRUN         TYPE C, "test run - do not update database
        XREFS_OK        TYPE C, "x-referencing complete for raw entry
        DUPLICATE       TYPE C, "duplicate sales order exists
      END OF W_FLAG.
* Runtime measurements
DATA: BEGIN OF W_BAPI,
        TIME1           TYPE I,
        TIME2           TYPE I,
      END OF W_BAPI.
* Record counts
DATA: BEGIN OF W_COUNT,
        BAD_XREF(5)     TYPE N, "no. of entries where xref failed
        ERRORS(5)       TYPE N, "no. of BAPI calls with errors
        RAWDATA_READ(5) TYPE N, "no. of rawdata entries read
        RAWDATA_UPD(5)  TYPE N, "no. of rawdata entries updated
        SUCCESS(5)      TYPE N, "no. of successful BAPI calls
      END OF W_COUNT.
* For progress indicator
DATA: BEGIN OF W_PROGRESS,
        TOTAL(4)        TYPE N,
        CURRENT(4)      TYPE N,
        PERCENT(4)      TYPE N,
        TEXT(20)        TYPE C,
      END OF W_PROGRESS.
* Texts (initialised in form "initialization")...
* Texts in "processed already" popup
DATA: BEGIN OF W_PROC,
        TEXT1(65)                      TYPE C,
        TEXT2(65)                      TYPE C,
        QUESTION(65)                   TYPE C,
        OPTION_YES(5)                  TYPE C,
        OPTION_NO(5)                   TYPE C,
        TITLE(20)                      TYPE C,
        HAS_ERRORS(30)                 TYPE C,
        SALES_ORDERS_EXIST(30)         TYPE C,
      END OF W_PROC.
* Texts in report header
DATA: BEGIN OF W_REPORT,
        FILE(5)                        TYPE C,
        PRODRUN(80)                    TYPE C,
        TESTRUN(80)                    TYPE C,
        TITLE1(80)                     TYPE C,
        TITLE2(80)                     TYPE C,
      END OF W_REPORT.
* Column headings
DATA: BEGIN OF W_COL,
        ROWNO(10)                      TYPE C,
        SALESORDER(11)                 TYPE C,
        MESSAGES(8)                    TYPE C,
      END OF W_COL.
* Texts in report body
DATA: BEGIN OF W_TXT,
        BAD_XREF(100)                  TYPE C,
        BAPI_SUCCESS(100)              TYPE C,
        BAPI_ERRORS(100)               TYPE C,
        COMMITTED(100)                 TYPE C,
        NO_FURTHER_ACTION(100)         TYPE C,
        RAWDATA_READ(100)              TYPE C,
        RAWDATA_NOT_READ(100)          TYPE C,
        RAWDATA_PROCESSED_ALREADY(100) TYPE C,
        RAWDATA_UPD(100)               TYPE C,
        ROLLED_BACK(100)               TYPE C,
        TEST_SUCCESSFUL(100)           TYPE C,
        DUPLICATE_SALES(100)           TYPE C,
      END OF W_TXT.
* Texts in report body for xref results
DATA: BEGIN OF W_XREF,
        NO_CHANNEL(100)                TYPE C,
        NO_GRP_OFFICE(100)             TYPE C,
        NO_KUNNR(100)                  TYPE C,
        BAD_MATNR(100)                 TYPE C,
        NO_MATNR(100)                  TYPE C,
        NO_VSART(100)                  TYPE C,
        NO_ORDER_QTY(100)              TYPE C,
        FOR_CUSTOMER(100)              TYPE C,
        NO_POSID(100)                  TYPE C,
        NO_ORIGLAND(100)               TYPE C,
        NO_DESTLAND(100)               TYPE C,
        NO_SCHED_ID(100)               TYPE C,
        BLANK_POSID(100)               TYPE C,
      END OF W_XREF.
* Column positions (should match column headings)
DATA: BEGIN OF W_POS,
        ROWNO                          TYPE I VALUE 1,
        SALESORDER                     TYPE I VALUE 12,
        MESSAGES                       TYPE I VALUE 25,
      END OF W_POS.
* Tables and structures for BAPI_SALESORDER_CREATEFROMDAT2
DATA: W_HEADER       LIKE BAPISDHD1,
      W_HEADERX      LIKE BAPISDHD1X,
      W_SALESORDER   LIKE BAPIVBELN-VBELN.
DATA: T_BAPE_VBAP    LIKE STANDARD TABLE OF BAPE_VBAP
                          WITH HEADER LINE,
      T_BAPE_VBAPX   LIKE STANDARD TABLE OF BAPE_VBAPX
                          WITH HEADER LINE,
      T_EXTENSIONIN  LIKE STANDARD TABLE OF BAPIPAREX
                          WITH HEADER LINE,
      T_ITEM         LIKE STANDARD TABLE OF BAPISDITM
                          WITH HEADER LINE,
      T_ITEMX        LIKE STANDARD TABLE OF BAPISDITMX
                          WITH HEADER LINE,
      T_PARTNER      LIKE STANDARD TABLE OF BAPIPARNR
                          WITH HEADER LINE,
      T_RETURN       LIKE STANDARD TABLE OF BAPIRET2
                          WITH HEADER LINE,
      T_SCHEDULE     LIKE STANDARD TABLE OF BAPISCHDL
                          WITH HEADER LINE,
      T_SCHEDULEX    LIKE STANDARD TABLE OF BAPISCHDLX
                          WITH HEADER LINE,
      T_TEXT         LIKE STANDARD TABLE OF BAPISDTEXT
                          WITH HEADER LINE.
************************************************************************
* Selection screen
************************************************************************
SELECTION-SCREEN: BEGIN OF BLOCK ZDATASET WITH FRAME TITLE COMM_TDA.
* File name
SELECTION-SCREEN: BEGIN OF LINE,
                  COMMENT 1(30) COMM_FIL,
                  POSITION POS_LOW.
PARAMETERS: P_FNAME LIKE ZVTRFILE-FILENAME LOWER CASE
                         MATCHCODE OBJECT ZVTRFILENAME.
SELECTION-SCREEN: END OF LINE.
* File number
SELECTION-SCREEN: BEGIN OF LINE,
                  COMMENT 1(30) COMM_FNO,
                  POSITION POS_LOW.
PARAMETERS: P_FNUM LIKE ZVTRFILE-FILENO
                        MATCHCODE OBJECT ZVTRFILENO.
SELECTION-SCREEN: END OF LINE.
* Test run
SELECTION-SCREEN: BEGIN OF LINE,
                  COMMENT 1(30) COMM_TST,
                  POSITION POS_LOW.
PARAMETERS: P_TEST AS CHECKBOX DEFAULT 'X'.
SELECTION-SCREEN: END OF LINE.
SELECTION-SCREEN: END OF BLOCK ZDATASET.
************************************************************************
* Events
************************************************************************
INITIALIZATION.
  PERFORM INITIALIZATION.
AT SELECTION-SCREEN.
  PERFORM CHECK_SELECTIONS.
AT SELECTION-SCREEN ON P_FNAME.
  PERFORM CHECK_FNAME.
AT SELECTION-SCREEN ON P_FNUM.
  PERFORM CHECK_FNUM.
START-OF-SELECTION.
  PERFORM STORE_PARAMETERS.
  PERFORM LOCK_TABLES.
  PERFORM READ_RAWDATA.
  PERFORM CREATE_SALES_ORDERS.
  PERFORM COMMIT_OR_ROLLBACK.
  PERFORM UNLOCK_TABLES.
END-OF-SELECTION.
  PERFORM REPORT_WHAT_HAPPENED.
TOP-OF-PAGE.
  PERFORM TOP_OF_PAGE.
************************************************************************
* Forms
************************************************************************
* Form check_fname
************************************************************************
FORM CHECK_FNAME.
  CHECK NOT P_FNAME IS INITIAL.
* If supplied, file name must exist
  SELECT FILENAME
         INTO P_FNAME
         FROM ZVTRFILE UP TO 1 ROWS
         WHERE FILENAME EQ P_FNAME.
  ENDSELECT.
  IF SY-SUBRC NE 0.
    MESSAGE E001(ZTRON).
*   File not found
  ENDIF.
ENDFORM.                    "check_fname
************************************************************************
* Form check_fnum
************************************************************************
FORM CHECK_FNUM.
  CHECK NOT P_FNUM IS INITIAL.
* If supplied, file number must exist
  SELECT SINGLE FILENO
         INTO P_FNUM
         FROM ZVTRFILE
         WHERE FILENO EQ P_FNUM.
  IF SY-SUBRC NE 0.
    MESSAGE E001(ZTRON).
*   File not found
  ENDIF.
ENDFORM.                    "check_fnum
************************************************************************
* Form check_if_processed_already
************************************************************************
FORM CHECK_IF_PROCESSED_ALREADY.
  DATA:  L_TEXT(100)  TYPE C,
         L_VBELN      LIKE VBAK-VBELN,
         L_CUSTOMER   LIKE ZVTRSOLDTO-KUNNR.
* Has this data been processed already?
  W_FLAG-DONE_PROCESSED = W_FLAG-DONE_WITH_ERROR =
  W_FLAG-DUPLICATE = C_FALSE.
  LOOP AT T_ZVTRDATA.
    W_AENAM = T_ZVTRDATA-AENAM.
    W_AEDAT = T_ZVTRDATA-AEDAT.
    WRITE W_AEDAT TO W_AEDAT_C.
    CLEAR L_CUSTOMER.
    IF T_ZVTRDATA-STATUS NE C_STATUS-PROCESSED.
* IF NOT ALREADY PROCESSED CHECK TO SEE IF DUPLICATE SALES ORDER
* ALREADY EXISTS IN SAP
* - Get SAP Customer
      SELECT SINGLE KUNNR
         INTO L_CUSTOMER
         FROM ZVTRSOLDTO
         WHERE MANU_CUST EQ T_ZVTRDATA-CUSTOMER.
* CHECK IF SALES DOCUMENT ALREADY EXISTS IN SAP
* Z2 INDEX ADDED TO VBAK FOR THIS SELECTION
      CLEAR L_VBELN.
      SELECT VBELN INTO L_VBELN
        UP TO 1 ROWS
        FROM VBAK
        WHERE KUNNR   = L_CUSTOMER
          AND VSNMR_V = T_ZVTRDATA-MANU_ORDER.
      ENDSELECT.
      IF NOT L_VBELN IS INITIAL.
        PERFORM REPLACE_STRING USING '&1'
                               L_VBELN
                               W_TXT-DUPLICATE_SALES
                      CHANGING L_TEXT.
        PERFORM REPLACE_STRING USING '&2'
                               T_ZVTRDATA-CUSTOMER
                               L_TEXT
                      CHANGING L_TEXT.
        PERFORM REPLACE_STRING USING '&3'
                               T_ZVTRDATA-MANU_ORDER
                               L_TEXT
                      CHANGING L_TEXT.
        PERFORM ADD_RESULT_MESSAGE USING T_ZVTRDATA-ROWNO
                                       SPACE
                                       L_TEXT.
        W_FLAG-DUPLICATE = C_TRUE.
        T_ZVTRDATA-STATUS = C_STATUS-DUPLICATE.
        MODIFY T_ZVTRDATA.
      ENDIF.
    ENDIF.
    CHECK T_ZVTRDATA-STATUS NE C_STATUS-NEW.
    CASE T_ZVTRDATA-STATUS.
      WHEN C_STATUS-PROCESSED.
        W_FLAG-DONE_PROCESSED = C_TRUE.
      WHEN C_STATUS-DUPLICATE.
      WHEN OTHERS.
        W_FLAG-DONE_WITH_ERROR = C_TRUE.
    ENDCASE.
    IF W_FLAG-DONE_PROCESSED = C_TRUE.
      EXIT.
    ENDIF.
  ENDLOOP.
* If it's marked complete, don't re-process it.
* If it has errors, does the user want to re-process it?
* If it's processed already, don't re-process it.
  IF W_ZVTRFILE-VOLLST EQ C_TRUE.
    MESSAGE I026(ZTRON) WITH W_ZVTRFILE-FILENO.
*   File &1 has been marked complete. You cannot re-process it.
    W_FLAG-RAWDATA = C_FALSE.
  ELSEIF W_FLAG-DUPLICATE EQ C_TRUE.
    MESSAGE I030(ZTRON) WITH W_ZVTRFILE-FILENO.
*   File &1 has duplicates. You cannot re-process it.
    W_FLAG-RAWDATA = C_FALSE.
  ELSEIF W_FLAG-DONE_WITH_ERROR EQ C_TRUE.
    PERFORM REPLACE_STRING USING '&1'
                                 W_AENAM
                                 W_PROC-TEXT1
                        CHANGING W_PROC-TEXT1.
    PERFORM REPLACE_STRING USING '&2'
                                 W_AEDAT_C
                                 W_PROC-TEXT1
                        CHANGING W_PROC-TEXT1.
    PERFORM REPLACE_STRING USING '&1'
                                 W_PROC-HAS_ERRORS
                                 W_PROC-TEXT2
                        CHANGING W_PROC-TEXT2.
    CALL FUNCTION 'POPUP_TO_DECIDE'
      EXPORTING
        DEFAULTOPTION           = '2'
        TEXTLINE1               = W_PROC-TEXT1
        TEXTLINE2               = W_PROC-TEXT2
        TEXTLINE3               = W_PROC-QUESTION
        TEXT_OPTION1            = W_PROC-OPTION_YES
        TEXT_OPTION2            = W_PROC-OPTION_NO
        ICON_TEXT_OPTION1       = 'ICON_OKAY'
        ICON_TEXT_OPTION2       = 'ICON_STOP'
        TITEL                   = W_PROC-TITLE
*       START_COLUMN            = 25
*       START_ROW               = 6
*       CANCEL_DISPLAY          = 'X'
      IMPORTING
        ANSWER                  = W_ANSWER.
*   if yes, set statuses to "new" and proceed as usual
*   if no, don't process any data
    CASE W_ANSWER.
      WHEN '1'.
        T_ZVTRDATA-STATUS = C_STATUS-NEW.
        MODIFY T_ZVTRDATA TRANSPORTING STATUS
                           WHERE STATUS NE C_STATUS-NEW.
      WHEN OTHERS.
        W_FLAG-RAWDATA = C_FALSE.
    ENDCASE.
  ELSEIF W_FLAG-DONE_PROCESSED EQ C_TRUE.
    MESSAGE I025(ZTRON) WITH W_ZVTRFILE-FILENO.
*   File &1 has been processed already. You cannot re-process it.
    W_FLAG-RAWDATA = C_FALSE.
  ENDIF.
ENDFORM.                    "check_if_processed_already
************************************************************************
* Form check_selections
************************************************************************
FORM CHECK_SELECTIONS.
  IF ( NOT P_FNAME IS INITIAL AND NOT P_FNUM IS INITIAL )
  OR (     P_FNAME IS INITIAL AND     P_FNUM IS INITIAL ).
    MESSAGE E011(ZTRON).
*   Enter either a file name or number
  ENDIF.
ENDFORM.                    "check_selections
************************************************************************
* Form call_sales_bapi
************************************************************************
FORM CALL_SALES_BAPI.
  DATA: L_FILENO(20) TYPE C,      "File no. for header text
        L_MSG_NO LIKE T100-MSGNR,
        L_MSG_TEXT(100) TYPE C.
* Fields for data derived from cross-reference tables
  DATA: L_MATNR      LIKE BAPISDITM-MATERIAL,   "material
        L_MATNR_TMP  LIKE BAPISDITM-MATERIAL,   "material
        L_POSID      LIKE ZVTRWBS-POSID,        "WBS element
        L_REQ_QTY    LIKE BAPISCHDL-REQ_QTY,    "order quantity
        L_SALES_UNIT LIKE BAPISDITM-SALES_UNIT, "sales uom
        L_VSART      LIKE ZVTRCOMMTYPE-VSART,   "shipping type
        L_ZZDESTLAND LIKE VBAP-ZZDESTLAND,      "destination ctry
        L_ZZORIGLAND LIKE VBAP-ZZORIGLAND.      "origin country
  DATA: BEGIN OF L_CUST,
          KUNNR      LIKE ZVTRSOLDTO-KUNNR,     "customer number
          VKGRP      LIKE KNVV-VKGRP,           "sales group
          VKBUR      LIKE KNVV-VKBUR,           "sales office
          VTWEG      LIKE KNVV-VTWEG,           "dist. channel
          KONDA      LIKE KNVV-KONDA,           "pricing group
          MANU_CUST  LIKE ZVTRSOLDTO-MANU_CUST, "Manugistics customer
        END OF L_CUST.
  DATA: BEGIN OF L_FLAG,
          KUNNR_FOUND TYPE C, "Found the SAP customer master
        END OF L_FLAG.
* Derive certain data from cross-reference tables
  W_FLAG-XREFS_OK = C_TRUE.
* - Customer
  SELECT SINGLE KUNNR MANU_CUST
         INTO (L_CUST-KUNNR, L_CUST-MANU_CUST)
         FROM ZVTRSOLDTO
         WHERE MANU_CUST EQ T_ZVTRDATA-CUSTOMER.
* If we found a customer number and the Manu. customer is actually the
* one on ZVTRDATA (the customer field being longer than manu_cust)...
  IF  SY-SUBRC         EQ 0
  AND L_CUST-MANU_CUST EQ T_ZVTRDATA-CUSTOMER.
    L_FLAG-KUNNR_FOUND = C_TRUE.
* - Distribution channel, Sales group, Sales office
    SELECT VTWEG VKGRP VKBUR KONDA
           INTO CORRESPONDING FIELDS OF L_CUST
           FROM KNVV UP TO 1 ROWS
           WHERE KUNNR EQ L_CUST-KUNNR
           AND   VKORG EQ T_ZVTRDATA-VKORG
           AND   SPART EQ T_ZVTRDATA-SPART.
    ENDSELECT.
    IF SY-SUBRC NE 0.
      PERFORM SET_FLAG_AND_NEWLINE_IF_NEEDED.
      PERFORM REPLACE_STRING USING '&1'
                                   L_CUST-KUNNR
                                   W_XREF-NO_CHANNEL
                          CHANGING W_MESSAGE.
      PERFORM REPLACE_STRING USING '&2'
                                   T_ZVTRDATA-VKORG
                                   W_MESSAGE
                          CHANGING W_MESSAGE.
      PERFORM REPLACE_STRING USING '&3'
                                   T_ZVTRDATA-SPART
                                   W_MESSAGE
                          CHANGING W_MESSAGE.
      WRITE: AT W_POS-MESSAGES W_MESSAGE.
      WRITE: AT /W_POS-MESSAGES W_XREF-NO_GRP_OFFICE.
    ENDIF.
  ELSE.
    L_FLAG-KUNNR_FOUND = C_FALSE.
    PERFORM SET_FLAG_AND_NEWLINE_IF_NEEDED.
    PERFORM REPLACE_STRING USING '&1'
                                 T_ZVTRDATA-CUSTOMER
                                 W_XREF-NO_KUNNR
                        CHANGING W_MESSAGE.
    WRITE: AT W_POS-MESSAGES W_MESSAGE.
  ENDIF.
* - Material
  SELECT SINGLE VALUE
         INTO L_MATNR_TMP
         FROM ZMTRDEFAULTS
         WHERE ROLLNAME EQ 'MATNR'.
  IF SY-SUBRC EQ 0.
    CALL FUNCTION 'CONVERSION_EXIT_MATN1_INPUT'
      EXPORTING
        INPUT        = L_MATNR_TMP
      IMPORTING
        OUTPUT       = L_MATNR
      EXCEPTIONS
        LENGTH_ERROR = 1
        OTHERS       = 2.
    IF SY-SUBRC NE 0.
      PERFORM REPLACE_STRING USING '&1'
                                   L_MATNR
                                   W_XREF-BAD_MATNR
                          CHANGING W_MESSAGE.
      PERFORM SET_FLAG_AND_NEWLINE_IF_NEEDED.
      WRITE: AT W_POS-MESSAGES W_MESSAGE.
    ENDIF.
  ELSE.
    PERFORM SET_FLAG_AND_NEWLINE_IF_NEEDED.
    WRITE: AT W_POS-MESSAGES W_XREF-NO_MATNR.
  ENDIF.
* - Shipping type (Manugistics commodity type)
  SELECT SINGLE VSART
         INTO L_VSART
         FROM ZVTRCOMMTYPE
         WHERE MANU_COMMTYP EQ T_ZVTRDATA-MANU_COMMTYP.
  IF SY-SUBRC NE 0.
    PERFORM SET_FLAG_AND_NEWLINE_IF_NEEDED.
    PERFORM REPLACE_STRING USING '&1'
                                 T_ZVTRDATA-MANU_COMMTYP
                                 W_XREF-NO_VSART
                        CHANGING W_MESSAGE.
    WRITE: AT W_POS-MESSAGES W_MESSAGE.
  ENDIF.
* - Required quantity and Sales unit (derived via customer pricing
*   group on customer master)
  IF L_FLAG-KUNNR_FOUND EQ C_TRUE.
    CASE L_CUST-KONDA.
      WHEN '40'.                         "pallet-based pricing
        L_REQ_QTY    = T_ZVTRDATA-PALLETQTY.
        L_SALES_UNIT = T_ZVTRDATA-PALLETUOM.
      WHEN '50'.                         "weight-based pricing
        L_REQ_QTY    = T_ZVTRDATA-NTGEW.
        L_SALES_UNIT = T_ZVTRDATA-GEWEI.
      WHEN OTHERS.
        PERFORM SET_FLAG_AND_NEWLINE_IF_NEEDED.
        PERFORM REPLACE_STRING USING '&1'
                                     L_CUST-KONDA
                                     W_XREF-NO_ORDER_QTY
                            CHANGING W_MESSAGE.
        WRITE: AT W_POS-MESSAGES W_MESSAGE.
        PERFORM SET_FLAG_AND_NEWLINE_IF_NEEDED.
        PERFORM REPLACE_STRING USING '&1'
                                     L_CUST-KUNNR
                                     W_XREF-FOR_CUSTOMER
                            CHANGING W_MESSAGE.
        PERFORM REPLACE_STRING USING '&2'
                                     T_ZVTRDATA-CUSTOMER
                                     W_MESSAGE
                            CHANGING W_MESSAGE.
        WRITE: AT W_POS-MESSAGES W_MESSAGE.
    ENDCASE.
  ENDIF.
* - WBS element
  IF T_ZVTRDATA-POSID IS INITIAL.
*    if l_flag-kunnr_found eq c_true.
    CLEAR L_POSID.
    SELECT SINGLE POSID
         INTO L_POSID
         FROM ZVTRWBS
         WHERE EMPST EQ T_ZVTRDATA-SCHED_ID
         AND   KUNNR EQ L_CUST-KUNNR.
    IF SY-SUBRC NE 0.
      PERFORM SET_FLAG_AND_NEWLINE_IF_NEEDED.
      PERFORM REPLACE_STRING USING '&1'
                                 T_ZVTRDATA-SCHED_ID
                                 W_XREF-NO_POSID
                        CHANGING W_MESSAGE.
      PERFORM REPLACE_STRING USING '&2'
                                 L_CUST-KUNNR
                                 W_MESSAGE
                        CHANGING W_MESSAGE.
      WRITE: AT W_POS-MESSAGES W_MESSAGE.
    ENDIF.
  ELSE.
*     Use WBS element on spreadsheet
    L_POSID = T_ZVTRDATA-POSID.
    IF L_POSID IS INITIAL.
*       Value missing - report error
      PERFORM SET_FLAG_AND_NEWLINE_IF_NEEDED.
      WRITE: AT W_POS-MESSAGES W_XREF-BLANK_POSID.
    ENDIF.
  ENDIF.
* - Origin country
  SELECT SINGLE LAND1
         INTO L_ZZORIGLAND
         FROM ZMTRLAND
         WHERE MANU_LAND EQ T_ZVTRDATA-ORIGLAND.
  IF SY-SUBRC NE 0.
    PERFORM SET_FLAG_AND_NEWLINE_IF_NEEDED.
    PERFORM REPLACE_STRING USING '&1'
                                 T_ZVTRDATA-ORIGLAND
                                 W_XREF-NO_ORIGLAND
                        CHANGING W_MESSAGE.
    WRITE: AT W_POS-MESSAGES W_MESSAGE.
  ENDIF.
* - Destination country
  SELECT SINGLE LAND1
         INTO L_ZZDESTLAND
         FROM ZMTRLAND
         WHERE MANU_LAND EQ T_ZVTRDATA-DESTLAND.
  IF SY-SUBRC NE 0.
    PERFORM SET_FLAG_AND_NEWLINE_IF_NEEDED.
    PERFORM REPLACE_STRING USING '&1'
                                 T_ZVTRDATA-DESTLAND
                                 W_XREF-NO_DESTLAND
                        CHANGING W_MESSAGE.
    WRITE: AT W_POS-MESSAGES W_MESSAGE.
  ENDIF.
* - Schedule ID (should be non-blank)
  IF T_ZVTRDATA-SCHED_ID EQ SPACE.
    PERFORM SET_FLAG_AND_NEWLINE_IF_NEEDED.
    WRITE: AT W_POS-MESSAGES W_XREF-NO_SCHED_ID.
  ENDIF.
* If any xref failed, do not call BAPI
  IF W_FLAG-XREFS_OK EQ C_FALSE.
    W_FLAG-COMMIT = C_FALSE.
    W_STATUS      = C_STATUS-ERROR. "Rawdata status - with errors
    ADD 1 TO W_COUNT-BAD_XREF.
    EXIT.
  ENDIF.
* Header
  W_HEADER-DOC_TYPE          = T_ZVTRDATA-AUART.
  W_HEADER-SALES_ORG         = T_ZVTRDATA-VKORG.
  W_HEADER-DISTR_CHAN        = L_CUST-VTWEG.
  W_HEADER-DIVISION          = T_ZVTRDATA-SPART.
  W_HEADER-SALES_OFF         = L_CUST-VKBUR.
  W_HEADER-SALES_GRP         = L_CUST-VKGRP.
*  w_header-ref_1             = t_zvtrdata-master_bol.
*  w_header-ref_1_s           = t_zvtrdata-sub_bol.
  W_HEADER-REF_1             = T_ZVTRDATA-RECEIVER_PO.
  W_HEADER-PURCH_NO_C        = T_ZVTRDATA-SHIPPER_REF.
  W_HEADER-VERSION           = T_ZVTRDATA-MANU_ORDER.
  W_HEADER-DOC_DATE          = T_ZVTRDATA-SHIPMENT_DATE.
  W_HEADERX-UPDATEFLAG       = 'I'.  "create sales document
  MOVE 'X' TO:
       W_HEADERX-DOC_TYPE,
       W_HEADERX-SALES_ORG,
       W_HEADERX-DISTR_CHAN,
       W_HEADERX-DIVISION,
       W_HEADERX-SALES_OFF,
       W_HEADERX-SALES_GRP,
       W_HEADERX-REF_1,
       W_HEADERX-REF_1_S,
       W_HEADERX-PURCH_NO_C,
       W_HEADERX-VERSION,
       W_HEADERX-DOC_DATE.
* Header texts
  T_TEXT-ITM_NUMBER          = SPACE.
  T_TEXT-TEXT_ID             = 'Z023'.
  T_TEXT-LANGU               = SY-LANGU.
  T_TEXT-FORMAT_COL          = '*'.
  CONCATENATE 'File no.' W_ZVTRFILE-FILENO
         INTO L_FILENO SEPARATED BY SPACE.
  T_TEXT-TEXT_LINE           = L_FILENO.
  APPEND T_TEXT.
  T_TEXT-TEXT_LINE           = W_ZVTRFILE-FILENAME.
  APPEND T_TEXT.
* Partner
  T_PARTNER-PARTN_ROLE       = 'AG'.
  T_PARTNER-PARTN_NUMB       = L_CUST-KUNNR.
  APPEND T_PARTNER.
* Item
  T_ITEM-ITM_NUMBER          = '000010'.
  T_ITEM-MATERIAL            = L_MATNR.
  T_ITEM-NET_WEIGHT          = T_ZVTRDATA-NTGEW.
  T_ITEM-UNTOF_WGHT          = T_ZVTRDATA-GEWEI.
  T_ITEM-VOLUME              = T_ZVTRDATA-VOLUM.
  T_ITEM-VOLUNIT             = T_ZVTRDATA-VOLEH.
  T_ITEM-SALES_UNIT          = L_SALES_UNIT.
  T_ITEM-PLANT               = T_ZVTRDATA-WERKS.
  T_ITEM-WBS_ELEM            = L_POSID.
  T_ITEM-REC_POINT           = T_ZVTRDATA-SCHED_ID.
  T_ITEM-SHIP_TYPE           = L_VSART.
  APPEND T_ITEM.
  T_ITEMX-ITM_NUMBER         = T_ITEM-ITM_NUMBER.
  T_ITEMX-UPDATEFLAG         = 'I'.  "create sales doc. item
  MOVE 'X' TO:
       T_ITEMX-ITM_NUMBER,
       T_ITEMX-MATERIAL,
       T_ITEMX-NET_WEIGHT,
       T_ITEMX-UNTOF_WGHT,
       T_ITEMX-VOLUME,
       T_ITEMX-VOLUNIT,
       T_ITEMX-SALES_UNIT,
       T_ITEMX-PLANT,
       T_ITEMX-WBS_ELEM,
       T_ITEMX-REC_POINT,
       T_ITEMX-SHIP_TYPE.
  APPEND T_ITEMX.
* Item - custom fields
* (See OSS 143580 for info on custom fields in sales doc BAPIs)
  T_BAPE_VBAP-POSNR          = T_ITEM-ITM_NUMBER.
  T_BAPE_VBAP-ZZORIGCITY     = T_ZVTRDATA-ORIGCITY.
  T_BAPE_VBAP-ZZORIGREGIO    = T_ZVTRDATA-ORIGREGIO.
  T_BAPE_VBAP-ZZORIGLAND     = L_ZZORIGLAND.
  T_BAPE_VBAP-ZZDESTCITY     = T_ZVTRDATA-DESTCITY.
  T_BAPE_VBAP-ZZDESTREGIO    = T_ZVTRDATA-DESTREGIO.
  T_BAPE_VBAP-ZZDESTLAND     = L_ZZDESTLAND.
  T_BAPE_VBAP-ZZPALLETQTY    = T_ZVTRDATA-PALLETQTY.
  T_BAPE_VBAP-ZZPALLETUOM    = T_ZVTRDATA-PALLETUOM.
  T_BAPE_VBAP-ZZPIECEQTY     = T_ZVTRDATA-PIECEQTY.
  T_BAPE_VBAP-ZZPIECEUOM     = T_ZVTRDATA-PIECEUOM.
  APPEND T_BAPE_VBAP.
  T_BAPE_VBAPX-POSNR         = T_ITEM-ITM_NUMBER.
  MOVE 'X' TO:
       T_BAPE_VBAPX-ZZORIGCITY,
       T_BAPE_VBAPX-ZZORIGREGIO,
       T_BAPE_VBAPX-ZZORIGLAND,
       T_BAPE_VBAPX-ZZDESTCITY,
       T_BAPE_VBAPX-ZZDESTREGIO,
       T_BAPE_VBAPX-ZZDESTLAND,
       T_BAPE_VBAPX-ZZPALLETQTY,
       T_BAPE_VBAPX-ZZPALLETUOM,
       T_BAPE_VBAPX-ZZPIECEQTY,
       T_BAPE_VBAPX-ZZPIECEUOM.
  APPEND T_BAPE_VBAPX.
  LOOP AT T_BAPE_VBAP.
    T_EXTENSIONIN-STRUCTURE  = 'BAPE_VBAP'.
*   If sending field > 240, remember to split data
*   into valuepart2 to valuepart4.
    T_EXTENSIONIN-VALUEPART1 = T_BAPE_VBAP.
    APPEND T_EXTENSIONIN.
  ENDLOOP.
  LOOP AT T_BAPE_VBAPX.
    T_EXTENSIONIN-STRUCTURE  = 'BAPE_VBAPX'.
*   If sending field > 240, remember to split data
*   into valuepart2 to valuepart4.
    T_EXTENSIONIN-VALUEPART1 = T_BAPE_VBAPX.
    APPEND T_EXTENSIONIN.
  ENDLOOP.
* Item texts
  IF NOT T_ZVTRDATA-LINETEXT IS INITIAL.
    T_TEXT-ITM_NUMBER        = T_ITEM-ITM_NUMBER.
    T_TEXT-TEXT_ID           = 'Z022'.
    T_TEXT-LANGU             = SY-LANGU.
    T_TEXT-FORMAT_COL        = '*'.
    T_TEXT-TEXT_LINE         = T_ZVTRDATA-LINETEXT. "max 32 chars
    APPEND T_TEXT.
  ENDIF.
* Schedule lines
  T_SCHEDULE-ITM_NUMBER      = T_ITEM-ITM_NUMBER.
  T_SCHEDULE-SCHED_LINE      = '0010'.
  T_SCHEDULE-REQ_QTY         = L_REQ_QTY.
  APPEND T_SCHEDULE.
  T_SCHEDULEX-ITM_NUMBER     = T_ITEM-ITM_NUMBER.
  T_SCHEDULEX-UPDATEFLAG     = 'I'.            "create schedule line
  MOVE 'X' TO:
       T_SCHEDULEX-REQ_QTY.
* BAPI call
  CALL FUNCTION 'BAPI_SALESORDER_CREATEFROMDAT2'
    EXPORTING
*     SALESDOCUMENTIN               =
      ORDER_HEADER_IN               = W_HEADER
      ORDER_HEADER_INX              = W_HEADERX
*     SENDER                        =
*     BINARY_RELATIONSHIPTYPE       =
*     INT_NUMBER_ASSIGNMENT         =
*     BEHAVE_WHEN_ERROR             =
*     LOGIC_SWITCH                  =
      TESTRUN                       = W_FLAG-TESTRUN
*     CONVERT                       = ' '
    IMPORTING
      SALESDOCUMENT                 = W_SALESORDER
    TABLES
      RETURN                        = T_RETURN
      ORDER_ITEMS_IN                = T_ITEM
      ORDER_ITEMS_INX               = T_ITEMX
      ORDER_PARTNERS                = T_PARTNER
      ORDER_SCHEDULES_IN            = T_SCHEDULE
      ORDER_SCHEDULES_INX           = T_SCHEDULEX
*     ORDER_CONDITIONS_IN           =
*     ORDER_CFGS_REF                =
*     ORDER_CFGS_INST               =
*     ORDER_CFGS_PART_OF            =
*     ORDER_CFGS_VALUE              =
*     ORDER_CFGS_BLOB               =
*     ORDER_CFGS_VK                 =
*     ORDER_CFGS_REFINST            =
*     ORDER_CCARD                   =
      ORDER_TEXT                    = T_TEXT
*     ORDER_KEYS                    =
      EXTENSIONIN                   = T_EXTENSIONIN
*     PARTNERADDRESSES              =
            .
* Check return structure
* If this was a test run, W_SALESORDER will be initial
  W_FLAG-GOOD_CALL = C_FALSE.
  IF NOT W_SALESORDER   IS INITIAL
  OR     W_FLAG-TESTRUN EQ C_TRUE.
    W_FLAG-GOOD_CALL = C_TRUE.
    LOOP AT T_RETURN.
      IF T_RETURN-TYPE EQ 'E'
      OR T_RETURN-TYPE EQ 'A'.
        W_FLAG-GOOD_CALL = C_FALSE.
      ENDIF.
    ENDLOOP.
  ENDIF.
* Display results
  IF W_FLAG-GOOD_CALL EQ C_TRUE.
    IF W_FLAG-TESTRUN EQ C_TRUE.
      POSITION W_POS-MESSAGES.
      WRITE: W_TXT-TEST_SUCCESSFUL.
    ELSE.
      POSITION W_POS-SALESORDER.
      WRITE: W_SALESORDER.
    ENDIF.
    W_STATUS = C_STATUS-OKAY. "Rawdata status - no errors
    ADD 1 TO W_COUNT-SUCCESS.
  ELSE.
    W_FLAG-COMMIT = C_FALSE.
    W_STATUS      = C_STATUS-ERROR. "Rawdata status - with errors
    ADD 1 TO W_COUNT-ERRORS.
    LOOP AT T_RETURN WHERE TYPE EQ 'E'
                     OR    TYPE EQ 'A'
                     OR    TYPE EQ ' '.
      IF T_RETURN-TYPE EQ SPACE.
        L_MSG_TEXT = T_RETURN-MESSAGE.
      ELSE.
        L_MSG_NO = T_RETURN-NUMBER.
        CALL FUNCTION 'MESSAGE_PREPARE'
          EXPORTING
*           LANGUAGE                     = ' '
            MSG_ID                       = T_RETURN-ID
            MSG_NO                       = L_MSG_NO
            MSG_VAR1                     = T_RETURN-MESSAGE_V1
            MSG_VAR2                     = T_RETURN-MESSAGE_V2
            MSG_VAR3                     = T_RETURN-MESSAGE_V3
            MSG_VAR4                     = T_RETURN-MESSAGE_V4
          IMPORTING
            MSG_TEXT                     = L_MSG_TEXT
          EXCEPTIONS
            FUNCTION_NOT_COMPLETED       = 1
            MESSAGE_NOT_FOUND            = 2
            OTHERS                       = 3.
      ENDIF.
*      position w_pos-messages.
*       write: t_return-type,
*              t_return-id,
*              t_return-number.
      POSITION W_POS-MESSAGES.
      WRITE: L_MSG_TEXT.
      NEW-LINE.
    ENDLOOP.
  ENDIF.
* Clear fields for next call
  CLEAR:   W_HEADER,
           W_HEADERX,
           T_BAPE_VBAP,
           T_BAPE_VBAPX,
           T_EXTENSIONIN,
           T_ITEM,
           T_ITEMX,
           T_PARTNER,
           T_RETURN,
           T_SCHEDULE,
           T_SCHEDULEX,
           T_TEXT.
  REFRESH: T_BAPE_VBAP,
           T_BAPE_VBAPX,
           T_EXTENSIONIN,
           T_ITEM,
           T_ITEMX,
           T_PARTNER,
           T_RETURN,
           T_SCHEDULE,
           T_SCHEDULEX,
           T_TEXT.
ENDFORM.                    "call_sales_bapi
************************************************************************
* Form commit_or_rollback
************************************************************************
FORM COMMIT_OR_ROLLBACK.
  SKIP.
  IF  W_FLAG-TESTRUN EQ C_FALSE
  AND W_FLAG-COMMIT  EQ C_TRUE.
*   Apply new statuses to rawdata, then commit
    T_ZVTRDATA-STATUS = C_STATUS-PROCESSED.
    MODIFY T_ZVTRDATA FROM T_ZVTRDATA
                      TRANSPORTING STATUS
                      WHERE STATUS = C_STATUS-OKAY.
    PERFORM UPDATE_RAWDATA.
    CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'.
    W_COMMIT_OR_ROLLBACK_TXT = W_TXT-COMMITTED.
    WRITE: / W_COMMIT_OR_ROLLBACK_TXT.
  ELSE.
*   Roll everything back, then apply new statuses to rawdata and commit
    CALL FUNCTION 'BAPI_TRANSACTION_ROLLBACK'.
    W_COMMIT_OR_ROLLBACK_TXT = W_TXT-ROLLED_BACK.
    WRITE: / W_COMMIT_OR_ROLLBACK_TXT.
    IF W_FLAG-TESTRUN EQ C_FALSE.
*     Since we're not saving anything, we shouldn't record any sales
*     order numbers on ZVTRDATA
      CLEAR T_ZVTRDATA-VBELN.
      MODIFY T_ZVTRDATA FROM T_ZVTRDATA TRANSPORTING VBELN
                                        WHERE ROWNO GT 0.
      PERFORM UPDATE_RAWDATA.
      COMMIT WORK.
    ENDIF.
  ENDIF.
ENDFORM.                    "commit_or_rollback
************************************************************************
* Form create_sales_orders
************************************************************************
FORM CREATE_SALES_ORDERS.
* Proceed only if there's rawdata to work with
  IF W_FLAG-RAWDATA EQ C_FALSE.
    W_FLAG-COMMIT = C_FALSE.
    EXIT.
  ENDIF.
* For each line in the rawdata table, create a sales order
* (simulate if test run)
  W_FLAG-COMMIT = C_TRUE.
  GET RUN TIME FIELD W_BAPI-TIME1.
  DESCRIBE TABLE T_ZVTRDATA LINES W_PROGRESS-TOTAL.
  LOOP AT T_ZVTRDATA.
    WRITE: AT /W_POS-ROWNO T_ZVTRDATA-ROWNO.
    PERFORM SET_PROGRESS_INDICATOR USING SY-TABIX.
*   Attempt to create sales order
    PERFORM CALL_SALES_BAPI.
*   Set status of rawdata entry
    T_ZVTRDATA-STATUS = W_STATUS.
    T_ZVTRDATA-VBELN  = W_SALESORDER.
    T_ZVTRDATA-AENAM  = SY-UNAME.
    T_ZVTRDATA-AEDAT  = SY-DATUM.
    MODIFY T_ZVTRDATA.
    ADD 1 TO W_COUNT-RAWDATA_UPD.
  ENDLOOP.
  GET RUN TIME FIELD W_BAPI-TIME2.
  SUBTRACT W_BAPI-TIME1 FROM W_BAPI-TIME2.
ENDFORM.                    "create_sales_orders
************************************************************************
* Form initialization
************************************************************************
FORM INITIALIZATION.
* Selection screen texts
  COMM_TDA = 'Excel data set'(S01).
  COMM_FIL = 'Filename'(S02).
  COMM_FNO = 'File number'(S03).
  COMM_TST = 'Test run'(S04).
* Texts for 'processed already' popup (note '&1' as a placeholder)
  W_PROC-TEXT1        = 'This data has been processed by &1 on &2'(P01).
  W_PROC-TEXT2           = 'and &1.'(P02).
  W_PROC-QUESTION        = 'Do you wish to re-process this data?'(P03).
  W_PROC-OPTION_YES      = 'Yes'(P04).
  W_PROC-OPTION_NO       = 'No'(P05).
  W_PROC-TITLE           = 'Create sales orders'(P06).
  W_PROC-HAS_ERRORS      = 'contains errors'(P07).
  W_PROC-SALES_ORDERS_EXIST = 'sales orders have been created'(P08).
* Report header
  W_REPORT-TITLE1        = 'Tronicus: Sales Order interface'(H01).
  W_REPORT-TITLE2        = 'Create sales orders'(H02).
  W_REPORT-PRODRUN       = 'P r o d u c t i o n   R u n'(H03).
  W_REPORT-TESTRUN       = 'T E S T   R U N   (no updates)'(H04).
  W_REPORT-FILE          = 'File:'(H05).
* Column headings
  W_COL-ROWNO            = 'Excel row'(C01).
  W_COL-SALESORDER       = 'Sales order'(C02).
  W_COL-MESSAGES         = 'Messages'(C03).
* Report body (note '&1' etc as a placeholder in some texts)
  W_TXT-RAWDATA_READ    = 'No. of entries successfully read:   &1'(T01).
  W_TXT-RAWDATA_NOT_READ = 'No entries read'(T02).
  W_TXT-RAWDATA_PROCESSED_ALREADY =
     'The data has been processed previously by &1 on &2 &3'(T03).
  W_TXT-NO_FURTHER_ACTION = '- no further action'(T04).
  W_TXT-COMMITTED       = 'All updates committed to database'(T05).
  W_TXT-ROLLED_BACK     = 'No changes applied to database'(T06).
  W_TXT-BAD_XREF        = 'No. of entries with xref failures:  &1'(T07).
  W_TXT-BAPI_SUCCESS    = 'No. of successful BAPI calls:       &1'(T08).
  W_TXT-BAPI_ERRORS     = 'No. of BAPI calls with errors:      &1'(T09).
  W_TXT-RAWDATA_UPD     = 'No. of entries with changed status: &1'(T10).
  W_TXT-TEST_SUCCESSFUL = 'Can create sales order for this row'(T11).
  W_TXT-DUPLICATE_SALES = 'Sales Order &1 exists for &2 / &3'(T12).
* Report body (results from cross-referencing - note '&1', '&2', etc.
* as placeholders in some texts)
  W_XREF-NO_CHANNEL =
      'Cannot find DChannel for customer &1 in SlsOrg &2, Div &3'(X01).
  W_XREF-NO_GRP_OFFICE =
      '(hence SlsGrp and SlsOffice also missing)'(X02).
  W_XREF-NO_KUNNR =
      'Cannot find SAP customer number for customer &1'(X03).
  W_XREF-NO_MATNR =
      'Cannot find default material for Manugistics sales orders'(X04).
  W_XREF-BAD_MATNR =
      '&1 is not a valid material number'(X05).
  W_XREF-NO_VSART =
      'Cannot find SAP shipping type for commodity type &1'(X06).
  W_XREF-NO_ORDER_QTY =
      'Cannot determine order qty/uom from cust. pricing group &1'(X07).
* "for_customer" is continuation of "no_order_qty", hence indentation
  W_XREF-FOR_CUSTOMER = '   for customer &1 (&2)'(X08).
  W_XREF-NO_POSID =
      'Cannot find WBS element for schedule id &1 and customer &2'(X09).
  W_XREF-NO_ORIGLAND =
      'Cannot find SAP country code for origin &1'(X10).
  W_XREF-NO_DESTLAND =
      'Cannot find SAP country code for destination &1'(X11).
  W_XREF-NO_SCHED_ID =
      'Schedule ID is blank (columns may be out of alignment)'(X12).
  W_XREF-BLANK_POSID =
      'WBS element is missing on this row'(X13).
* Text for SAPGUI_PROGRESS_INDICATOR
  W_PROGRESS-TEXT = '&1 of &2 rows'(G01).
ENDFORM.                    "initialization
************************************************************************
* Form lock_tables
************************************************************************
FORM LOCK_TABLES.
  DATA: L_MSGV1 LIKE SY-MSGV1.
  CALL FUNCTION 'ENQUEUE_EZ_ZVTRFILE'
    EXPORTING
      MODE_ZVTRFILE        = 'E'
      MANDT                = SY-MANDT
*     FILENO               =
*     X_FILENO             = ' '
*     _SCOPE               = '2'
*     _WAIT                = ' '
*     _COLLECT             = ' '
    EXCEPTIONS
      FOREIGN_LOCK         = 1
      SYSTEM_FAILURE       = 2
      OTHERS               = 3.
  CASE SY-SUBRC.
    WHEN 0.
*     Locked successfully
    WHEN 1.
      L_MSGV1 = SY-MSGV1.
      MESSAGE E016(ZTRON) WITH 'ZVTRFILE' L_MSGV1.
*     Table &1 is locked by &2. Cannot proceed
    WHEN OTHERS.
      MESSAGE E023(ZTRON) WITH 'ZVTRFILE' SY-SUBRC.
*     Unable to lock table &1 (sy-subrc = &2). Cannot proceed
  ENDCASE.
  CALL FUNCTION 'ENQUEUE_EZ_ZVTRDATA'
    EXPORTING
      MODE_ZVTRDATA        = 'E'
      MANDT                = SY-MANDT
*     FILENO               =
*     ROWNO                =
*     X_FILENO             = ' '
*     X_ROWNO              = ' '
*     _SCOPE               = '2'
*     _WAIT                = ' '
*     _COLLECT             = ' '
    EXCEPTIONS
      FOREIGN_LOCK         = 1
      SYSTEM_FAILURE       = 2
      OTHERS               = 3.
  CASE SY-SUBRC.
    WHEN 0.
*     Locked successfully
    WHEN 1.
      L_MSGV1 = SY-MSGV1.
      MESSAGE E016(ZTRON) WITH 'ZVTRDATA' L_MSGV1.
*     Table &1 is locked by &2. Cannot proceed
    WHEN OTHERS.
      MESSAGE E023(ZTRON) WITH 'ZVTRDATA' SY-SUBRC.
*     Unable to lock table &1 (sy-subrc = &2). Cannot proceed
  ENDCASE.
ENDFORM.                    "lock_tables
************************************************************************
* Form read_rawdata
************************************************************************
FORM READ_RAWDATA.
* Get relevant data from zvtrdata
  SELECT * FROM ZVTRDATA
         INTO CORRESPONDING FIELDS OF TABLE T_ZVTRDATA
         WHERE FILENO EQ W_ZVTRFILE-FILENO.
  IF SY-SUBRC EQ 0.
    W_FLAG-RAWDATA = C_TRUE.
    DESCRIBE TABLE T_ZVTRDATA LINES W_COUNT-RAWDATA_READ.
    PERFORM CHECK_IF_PROCESSED_ALREADY.
  ELSE.
    W_FLAG-RAWDATA = C_FALSE.
  ENDIF.
ENDFORM.                    "read_rawdata
************************************************************************
* Form replace_string
************************************************************************
FORM REPLACE_STRING USING P_OLD
                          P_NEW
                          P_SOURCE
                 CHANGING P_TARGET.
  DATA: L_LENGTH TYPE I.
  P_TARGET = P_SOURCE.
  IF P_NEW EQ SPACE.
    REPLACE P_OLD WITH P_NEW INTO P_TARGET.
  ELSE.
    L_LENGTH = STRLEN( P_NEW ).
    REPLACE P_OLD WITH P_NEW+0(L_LENGTH) INTO P_TARGET.
  ENDIF.
ENDFORM.                    "replace_string
************************************************************************
* Form report_what_happened
************************************************************************
FORM REPORT_WHAT_HAPPENED.
  SKIP.
  ULINE.
* Reading rawdata
  IF W_FLAG-RAWDATA EQ C_TRUE.
*   Rawdata was read successfully
    PERFORM REPLACE_STRING USING '&1'
                                 W_COUNT-RAWDATA_READ
                                 W_TXT-RAWDATA_READ
                        CHANGING W_TXT-RAWDATA_READ.
    WRITE: / W_TXT-RAWDATA_READ.
*   Rawdata was re-processed
    IF W_FLAG-DONE_PROCESSED  EQ C_TRUE
    OR W_FLAG-DONE_WITH_ERROR EQ C_TRUE.
      PERFORM REPLACE_STRING USING '&1'
                                   W_AENAM
                                   W_TXT-RAWDATA_PROCESSED_ALREADY
                          CHANGING W_TXT-RAWDATA_PROCESSED_ALREADY.
      PERFORM REPLACE_STRING USING '&2'
                                   W_AEDAT_C
                                   W_TXT-RAWDATA_PROCESSED_ALREADY
                          CHANGING W_TXT-RAWDATA_PROCESSED_ALREADY.
      PERFORM REPLACE_STRING USING '&3'
                                   SPACE
                                   W_TXT-RAWDATA_PROCESSED_ALREADY
                          CHANGING W_TXT-RAWDATA_PROCESSED_ALREADY.
      WRITE: / W_TXT-RAWDATA_PROCESSED_ALREADY.
    ENDIF.
  ELSE.
*   Rawdata was not read successfully or user chose not
*   to re-process data
    IF W_FLAG-DONE_PROCESSED  EQ C_TRUE
    OR W_FLAG-DONE_WITH_ERROR EQ C_TRUE.
      PERFORM REPLACE_STRING USING '&1'
                                   W_AENAM
                                   W_TXT-RAWDATA_PROCESSED_ALREADY
                          CHANGING W_TXT-RAWDATA_PROCESSED_ALREADY.
      PERFORM REPLACE_STRING USING '&2'
                                   W_AEDAT_C
                                   W_TXT-RAWDATA_PROCESSED_ALREADY
                          CHANGING W_TXT-RAWDATA_PROCESSED_ALREADY.
      PERFORM REPLACE_STRING USING '&3'
                                   W_TXT-NO_FURTHER_ACTION
                                   W_TXT-RAWDATA_PROCESSED_ALREADY
                          CHANGING W_TXT-RAWDATA_PROCESSED_ALREADY.
      WRITE: / W_TXT-RAWDATA_PROCESSED_ALREADY.
    ELSEIF W_FLAG-DUPLICATE EQ C_TRUE.
      SORT T_RESULT BY ROWNO SEQNO.
      NEW-LINE.
      LOOP AT T_RESULT.
        AT NEW ROWNO.
          WRITE: AT W_POS-ROWNO T_RESULT-ROWNO.
        ENDAT.
        PERFORM REPORT_RESULT_FIELD USING:
                                  W_POS-SALESORDER    T_RESULT-VBELN,
                                  W_POS-MESSAGES T_RESULT-TEXT.
        NEW-LINE.
      ENDLOOP.
      SKIP.
    ELSE.
      WRITE: / W_TXT-RAWDATA_NOT_READ.
    ENDIF.
  ENDIF.
* Calling BAPI
  PERFORM REPLACE_STRING USING '&1'
                               W_COUNT-BAD_XREF
                               W_TXT-BAD_XREF
                      CHANGING W_TXT-BAD_XREF.
  PERFORM REPLACE_STRING USING '&1'
                               W_COUNT-SUCCESS
                               W_TXT-BAPI_SUCCESS
                      CHANGING W_TXT-BAPI_SUCCESS.
  PERFORM REPLACE_STRING USING '&1'
                               W_COUNT-ERRORS
                               W_TXT-BAPI_ERRORS
                      CHANGING W_TXT-BAPI_ERRORS.
  SKIP.
  WRITE: / W_TXT-BAD_XREF,
         / W_TXT-BAPI_SUCCESS,
         / W_TXT-BAPI_ERRORS.
* Updating rawdata
  IF W_FLAG-TESTRUN EQ C_FALSE.
    PERFORM REPLACE_STRING USING '&1'
                                 W_COUNT-RAWDATA_UPD
                                 W_TXT-RAWDATA_UPD
                        CHANGING W_TXT-RAWDATA_UPD.
    SKIP.
    WRITE: / W_TXT-RAWDATA_UPD.
  ENDIF.
  SKIP.
  WRITE: / W_COMMIT_OR_ROLLBACK_TXT.
ENDFORM.                    "report_what_happened
************************************************************************
* Form set_flag_and_newline_if_needed
************************************************************************
FORM SET_FLAG_AND_NEWLINE_IF_NEEDED.
  IF W_FLAG-XREFS_OK EQ C_FALSE.
    NEW-LINE.
  ENDIF.
  W_FLAG-XREFS_OK = C_FALSE.
ENDFORM.                    "set_flag_and_newline_if_needed
************************************************************************
* Form set_progress_indicator
************************************************************************
FORM SET_PROGRESS_INDICATOR USING P_TABIX.
  DATA: L_TEXT LIKE W_PROGRESS-TEXT.
* Only relevant in foreground
  CHECK SY-BATCH EQ SPACE.
  W_PROGRESS-CURRENT = P_TABIX.
  W_PROGRESS-PERCENT = 100 * W_PROGRESS-CURRENT / W_PROGRESS-TOTAL.
  L_TEXT = W_PROGRESS-TEXT.
  REPLACE '&1' WITH W_PROGRESS-CURRENT INTO L_TEXT.
  REPLACE '&2' WITH W_PROGRESS-TOTAL   INTO L_TEXT.
  CALL FUNCTION 'SAPGUI_PROGRESS_INDICATOR'
    EXPORTING
      PERCENTAGE = W_PROGRESS-PERCENT
      TEXT       = L_TEXT.
ENDFORM.                    "set_progress_indicator
************************************************************************
* Form store_parameters
************************************************************************
FORM STORE_PARAMETERS.
  IF P_FNUM IS INITIAL.
*   get file number for this file
    SELECT * INTO W_ZVTRFILE
           FROM ZVTRFILE UP TO 1 ROWS
           WHERE FILENAME EQ P_FNAME.
    ENDSELECT.
  ELSE.
    SELECT * INTO W_ZVTRFILE
           FROM ZVTRFILE UP TO 1 ROWS
           WHERE FILENO EQ P_FNUM.
    ENDSELECT.
  ENDIF.
  W_FLAG-TESTRUN = P_TEST.
ENDFORM.                    "store_parameters
************************************************************************
* Form top_of_page
************************************************************************
FORM TOP_OF_PAGE.
  FORMAT INTENSIFIED.
* Report title
  WRITE: / SY-REPID,
           W_REPORT-TITLE1,
           SY-DATUM,
         / W_REPORT-TITLE2 UNDER W_REPORT-TITLE1,
           SY-UZEIT UNDER SY-DATUM.
  SKIP.
* Test run indicator
  IF W_FLAG-TESTRUN EQ C_TRUE.
    WRITE: / W_REPORT-TESTRUN UNDER W_REPORT-TITLE1.
  ELSE.
    WRITE: / W_REPORT-PRODRUN UNDER W_REPORT-TITLE1.
  ENDIF.
  WRITE: / W_REPORT-FILE,
           W_ZVTRFILE-FILENO,
         / W_ZVTRFILE-FILENAME UNDER W_ZVTRFILE-FILENO.
  SKIP.
* Column headings
  WRITE: AT /W_POS-ROWNO      W_COL-ROWNO,
         AT  W_POS-SALESORDER W_COL-SALESORDER,
         AT  W_POS-MESSAGES   W_COL-MESSAGES.
  FORMAT INTENSIFIED OFF.
  ULINE.
ENDFORM.                    "top_of_page
************************************************************************
* Form update_rawdata
************************************************************************
FORM UPDATE_RAWDATA.
* The rawdata entries are now marked 'error' or 'okay', if there were
* failed BAPI calls, or 'processed', if everything worked. These new
* statuses should be stored on the database, unless we failed to read
* any rawdata in the first place.
  CHECK W_FLAG-RAWDATA EQ C_TRUE.
  MODIFY ZVTRDATA FROM TABLE T_ZVTRDATA.
ENDFORM.                    "update_rawdata
************************************************************************
* Form unlock_tables
************************************************************************
FORM UNLOCK_TABLES.
  CALL FUNCTION 'DEQUEUE_EZ_ZVTRFILE'
    EXPORTING
      MODE_ZVTRFILE       = 'E'
      MANDT               = SY-MANDT
*     FILENO              =
*     X_FILENO            = ' '
*     _SCOPE              = '3'
*     _SYNCHRON           = ' '
*     _COLLECT            = ' '
            .
  CALL FUNCTION 'DEQUEUE_EZ_ZVTRDATA'
    EXPORTING
      MODE_ZVTRDATA       = 'E'
      MANDT               = SY-MANDT
*     FILENO              =
*     ROWNO               =
*     X_FILENO            = ' '
*     X_ROWNO             = ' '
*     _SCOPE              = '3'
*     _SYNCHRON           = ' '
*     _COLLECT            = ' '
            .
ENDFORM.                    "unlock_tables
************************************************************************
* Form add_result_message
************************************************************************
FORM ADD_RESULT_MESSAGE USING P_ROWNO LIKE ZVTRDATA-ROWNO
                              P_VBELN
                              P_TEXT.
  DATA: L_SEQNO TYPE I.
* Get sequential number for new entry in table
  LOOP AT T_RESULT WHERE ROWNO EQ P_ROWNO.
    IF T_RESULT-SEQNO GT L_SEQNO.
      L_SEQNO = T_RESULT-SEQNO.
    ENDIF.
  ENDLOOP.
  ADD 1 TO L_SEQNO.
  T_RESULT-ROWNO = P_ROWNO.
  T_RESULT-SEQNO = L_SEQNO.
  T_RESULT-VBELN = P_VBELN.
  T_RESULT-TEXT  = P_TEXT.
  APPEND T_RESULT.
ENDFORM.                    "add_result_message
************************************************************************
* Form report_result_field
************************************************************************
FORM REPORT_RESULT_FIELD USING P_POS
                               P_FIELD.
  IF NOT P_FIELD IS INITIAL.
    WRITE: AT P_POS P_FIELD.
  ENDIF.
ENDFORM.                    "report_result_field
*Messages
*----------------------------------------------------------
*
* Message class: ZTRON
*001
*011
*016
*023
*025
*026
*030
--------------------------------------------------------------------------------
  • No labels

1 Comment

  1. Hi,
    How to get zvtrdata & ZVTRSOLDTO table?

    Please help.

     

    Thanks

    Akbar