Make your own free website on Tripod.com

Table of Contents:



REPORT zf_order_budgetload
  NO STANDARD PAGE HEADING LINE-SIZE 255.

*@----------------------------------------------------------------------
*@ Program id    : zf_order_budgetload
*@ Program Desc  : This program uploads budget/bid for "order" to
*@                 the PA system.
*@
*@
*@ Transaction Code - zf_order_budgetload
*@ Input files   : User specified tab delimited text file
*@ Output files  : BDC session
*@
*@ Tables Updated: Table - none
*@
*@
*@ Author      : Ibrahim Jadalowen     (LABK935027)
*@ Date        : May 29, 2003
*@ Requested by: Joyce Homeniuk
*@----------------------------------------------------------------------
*@ Algorithm: The data is read from a tab delimited text file and
*@            loaded through a BDC session as per transaction code
*@            KO12.
*@            First we have to change the controlling area to ACAD
*@            and planning profile to SAP101.
*@            The GL accounts' information is updated, Revenue
*@            GL accounts and Expenses GL accounts
*@----------------------------------------------------------------------

INCLUDE zsmaster.

TABLES: cskb.

CONSTANTS: c_company(4) VALUE '6231',
           c_control_area(4) VALUE 'ACAD',
           c_profile(6) VALUE 'SAP101',
           c_min_rev(10) VALUE '40000',
           c_max_rev(10) VALUE '49999',
           c_min_exp(10) VALUE '50000',
           c_max_exp(10) VALUE '89999'.

DATA: g_field_year(15),
      error(1)    TYPE c VALUE '',
      lin         TYPE i.

DATA: BEGIN OF datatab OCCURS 0,
        glaccount(10),
        amount(15) TYPE p DECIMALS 2,
END OF datatab.

*/ This is the format of the input file:
TYPES: BEGIN OF intype,
        description(30),
        glaccount(10),
        amount(17),
END OF intype.

DATA: rc TYPE STANDARD TABLE OF intype WITH HEADER LINE.

SELECTION-SCREEN BEGIN OF BLOCK blk1 WITH FRAME TITLE text-101.
PARAMETERS: p_year(4) DEFAULT sy-datum(4).
PARAMETERS: p_order LIKE codia-aufnr.
SELECTION-SCREEN SKIP.

PARAMETERS: datafile LIKE rlgrap-filename
                     DEFAULT 'C:\'.
SELECTION-SCREEN BEGIN OF LINE.
SELECTION-SCREEN COMMENT 5(25) text-002.
SELECTION-SCREEN END OF LINE.
SELECTION-SCREEN SKIP.
PARAMETERS: session(12) TYPE c DEFAULT ''.
SELECTION-SCREEN END OF BLOCK blk1.

*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
START-OF-SELECTION.

  WRITE: / '*** LOG *** Report -',sy-repid COLOR COL_HEADING.
  WRITE: sy-datum, sy-uzeit.
  SKIP.

  PERFORM load_datatab.

  IF NOT error IS INITIAL.
    WRITE: / 'No batch session created.'.
    WRITE: / 'Programme ended.'.
    EXIT.
  ENDIF.

  IF session IS INITIAL.
    session = sy-repid.
  ENDIF.

  DESCRIBE TABLE datatab LINES lin.
  IF lin > 0.

    PERFORM create_bdc_session.

    SKIP.
    WRITE: / 'Batch session',session,'was created.'.

  ELSE.
    WRITE: / 'No data for processing.  No batch session created.'.
    WRITE: / 'Programme ended.'.
  ENDIF.
*/-------------------------------------------------------------------*\
AT SELECTION-SCREEN ON BLOCK blk1.
  DATA: result(14).

  SELECT SINGLE aufnr
    FROM aufk
    INTO result
    WHERE aufnr = p_order.

  IF sy-subrc <> 0.
    MESSAGE e001(zz) WITH 'Order' p_order
                          'doesnt exist in order master table'.
  ENDIF.

*---------------------------------------------------------------------*
*       FORM LOAD_DATATAB                                             *
*---------------------------------------------------------------------*
*       ........                                                      *
*---------------------------------------------------------------------*
FORM load_datatab.

  DATA: len TYPE i,
        diff TYPE i,
        ddate(10).

  CALL FUNCTION 'WS_UPLOAD'
       EXPORTING
            filename                = datafile
            filetype                = 'DAT'
       TABLES
            data_tab                = rc
       EXCEPTIONS
            conversion_error        = 1
            file_open_error         = 2
            file_read_error         = 3
            invalid_type            = 4
            no_batch                = 5
            unknown_error           = 6
            invalid_table_width     = 7
            gui_refuse_filetransfer = 8
            customer_error          = 9
            OTHERS                  = 10.

  IF sy-subrc <> 0.
    len = strlen( datafile ).
    WRITE: / 'Error opening file:' COLOR COL_NEGATIVE INVERSE ON,
             datafile(len) COLOR COL_NEGATIVE INVERSE ON.
    error = 'Y'.
    EXIT.
  ENDIF.

  DATA: l_glaccount(10).

  LOOP AT rc.

    CLEAR datatab.

    IF rc-glaccount NA 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
       AND rc-glaccount <> ''.

      l_glaccount = rc-glaccount.

      len = strlen( rc-glaccount ).
      diff = 10 - len.
      SHIFT l_glaccount RIGHT BY diff PLACES.
      DO diff TIMES.
        REPLACE '' WITH '0' INTO l_glaccount.
      ENDDO.

      MOVE-CORRESPONDING rc TO datatab.

      SELECT SINGLE * FROM cskb
        WHERE kokrs = c_control_area
          AND kstar = l_glaccount
          AND datbi >= sy-datum.

      IF sy-subrc = 0.
        COLLECT datatab.
      ELSE.
        WRITE: / 'GL Account #',
                 datatab-glaccount COLOR COL_NEGATIVE INVERSE,
                 'does not exist for controlling area',
                 c_control_area,
                 '. Values not accepted.'.
        error = 'Y'.
      ENDIF.
    ENDIF.

  ENDLOOP.

  SKIP 1.
  CHECK error IS INITIAL.
  WRITE: / 'Fiscal Year : ' COLOR COL_NORMAL, p_year.
  WRITE: / 'Order Number: ' COLOR COL_NORMAL, p_order.
  SKIP 1.
  WRITE: / 'Records added to the BDC session:' COLOR COL_HEADING.
  WRITE: /2(12) 'GL Account #' CENTERED COLOR COL_HEADING,
          20(20) 'Amount' CENTERED COLOR COL_HEADING.

ENDFORM.                               " LOAD_DATATAB

*---------------------------------------------------------------------*
*       FORM create_bdc_session                                       *
*---------------------------------------------------------------------*
*       ........                                                      *
*---------------------------------------------------------------------*
FORM create_bdc_session.

  DATA: l_session LIKE apqi-groupid.

  mac_set_status_bar 'Creating BDC Session......'.

  SORT datatab BY glaccount.

  PERFORM find_year_position.

  l_session = session.
  mac_bdc_open_group l_session.

* Set Controlling Area to 'ACAD'
  mac_bdc_refresh_data.
  mac_bdc_screen 'SAPLSPO4' '0300'.
  mac_bdc_position 'SVALD-VALUE(01)' ' '.
  mac_bdc_set_value: 'SVALD-VALUE(01)' 'ACAD'.
  mac_bdc_ok_code '=FURT'.
  mac_bdc_insert 'OKKS'.

* Set Planner profile to 'SAP101'
  mac_bdc_refresh_data.
  mac_bdc_screen 'SAPLKPP6' '1007'.
  mac_bdc_position 'RKPP6-PLPROF' ' '.
  mac_bdc_set_value: 'RKPP6-PLPROF' 'SAP101'.
  mac_bdc_ok_code '=CWEI'.
  mac_bdc_insert 'KP04'.

  mac_bdc_refresh_data.

* load the budget
  mac_bdc_screen 'SAPMKBUD' '0300'.
  mac_bdc_position 'CODIA-AUFNR' ' '.
  mac_bdc_set_value: 'CODIA-AUFNR' p_order.
  mac_bdc_set_value: 'BPDY-VERSN' '0'.
  mac_bdc_ok_code '/00'.

  mac_bdc_screen 'SAPLKBPP' '0300'.
  mac_bdc_position 'BPDY-WERT1(01)' ' '.
  mac_bdc_ok_code '=FULL'.

  mac_bdc_screen 'SAPLKBPP' '0200'.
  mac_bdc_position g_field_year ' '.
  mac_bdc_ok_code '=KAER'.

  LOOP AT datatab WHERE glaccount BETWEEN c_min_rev and c_max_rev.
* Position in revenue list
    mac_bdc_screen 'SAPLKPP2' '0112'.
    mac_bdc_position 'BK_0101' ' '.
    mac_bdc_ok_code  '=CPOZ'.

    mac_bdc_screen 'SAPLKPP4' '0100'.
    mac_bdc_position 'KPP0B-VALUE(01)' ' '.
    mac_bdc_set_value 'KPP0B-VALUE(01)' datatab-glaccount.
    mac_bdc_ok_code '=CSPA'.

* Enter Revenue Amount
    mac_bdc_screen 'SAPLKPP2' '0112'.
    mac_bdc_position 'BDC03(01)' ' '.
    mac_bdc_set_value: 'BDC03(01)' datatab-amount.

    WRITE: /02(12) datatab-glaccount,
            20(20) datatab-amount.
  ENDLOOP.

  mac_bdc_ok_code '=CBUC'.

* Save
  mac_bdc_screen 'SAPLKBPP' '0200'.
  mac_bdc_position g_field_year ' '.
  mac_bdc_ok_code '=POST'.

  mac_bdc_insert 'KO12'.

* Enter Expenses
  mac_bdc_refresh_data.

  mac_bdc_screen 'SAPMKBUD' '0300'.
  mac_bdc_position 'CODIA-AUFNR' ' '.
  mac_bdc_set_value: 'CODIA-AUFNR' p_order.
  mac_bdc_set_value: 'BPDY-VERSN' '0'.
  mac_bdc_ok_code '/00'.

  mac_bdc_screen 'SAPLKBPP' '0300'.
  mac_bdc_position 'BPDY-WERT1(01)' ' '.
  mac_bdc_ok_code '=FULL'.

* Call the Expenses Screen
  mac_bdc_screen 'SAPLKBPP' '0200'.
  mac_bdc_position g_field_year ' '.
  mac_bdc_ok_code '=KAPR'.

  LOOP AT datatab WHERE glaccount BETWEEN c_min_exp and c_max_exp.
* Position in Expenses list
    mac_bdc_screen 'SAPLKPP2' '0112'.
    mac_bdc_position 'BK_0101' ' '.
    mac_bdc_ok_code  '=CPOZ'.

    mac_bdc_screen 'SAPLKPP4' '0100'.
    mac_bdc_position 'KPP0B-VALUE(01)' ' '.
    mac_bdc_set_value 'KPP0B-VALUE(01)' datatab-glaccount.
    mac_bdc_ok_code '=CSPA'.

* Enter Expense Amount
    mac_bdc_screen 'SAPLKPP2' '0112'.
    mac_bdc_position 'BDC03(01)' ' '.
    mac_bdc_set_value: 'BDC03(01)' datatab-amount.

    WRITE: /02(12) datatab-glaccount,
            20(20) datatab-amount.
  ENDLOOP.

* Save
  mac_bdc_ok_code '=CBUC'.

* Save
  mac_bdc_screen 'SAPLKBPP' '0200'.
  mac_bdc_position g_field_year ' '.
  mac_bdc_ok_code '=POST'.

  mac_bdc_insert 'KO12'.

  mac_bdc_close_group.

ENDFORM.                  "CREATE_BDC_SESSION

*&---------------------------------------------------------------------*
*&      Form  FIND_YEAR_POSITION
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM find_year_position.

  DATA: base_year(4),
        position(2).

  base_year = sy-datum(4) - 4.

  position = p_year - base_year.

  CONCATENATE 'BPDY-WERT1(' position ')'
    INTO g_field_year.
ENDFORM.                    " FIND_YEAR_POSITION