ABAP-公式拆分

  call function 'ZFI_SPLIT_FORMULA'
              EXPORTING
                I_FORMULA = LW_FIELD-FORMULA
              TABLES
                OT_LINES  = LT_OT_LINES.



FUNCTION ZFI_SPLIT_FORMULA.

*"----------------------------------------------------------------------
*"*"本地接口:
*" IMPORTING
*" VALUE(I_FORMULA) TYPE ZZT_FI_003-FORMULA OPTIONAL
*" TABLES
*" OT_LINES STRUCTURE ZZSFORMULA_LINE OPTIONAL
*"----------------------------------------------------------------------

* [Global data declarations](global-zfi_split_formula.html)

  TYPES: BEGIN OF TY_DATA,
           STR TYPE ZZT_FI_003-FORMULA,
         END OF TY_DATA.

*--------------------------------------------------------------------*
*
*--------------------------------------------------------------------*

  DATA: GT_DATA TYPE STANDARD TABLE OF TY_DATA.
  FIELD-SYMBOLS:  TYPE TY_DATA.

  DATA: TEMP_LG TYPE STANDARD TABLE OF TY_DATA.
  FIELD-SYMBOLS:  TYPE TY_DATA.

  DATA: GS_LINEN TYPE ZZSFORMULA_LINE.

  DATA: GV_FORMULA LIKE I_FORMULA. "公式

  DATA: GV_CONDITION TYPE CHAR1.

  DATA: G_TEMP_FORMULA1 LIKE I_FORMULA,
        G_TEMP_FORMULA2 LIKE I_FORMULA,
        G_FLINE         TYPE ZZSFORMULA_LINE-REPT_ITEM,
        G_TLINE         TYPE ZZSFORMULA_LINE-REPT_ITEM.

  DATA: GV_LINE_POS TYPE ZZSFORMULA_LINE-REPT_ITEM.

  DATA: GV_SIGN TYPE CHAR1.

*--------------------------------------------------------------------*
*
*--------------------------------------------------------------------*

  CLEAR: GV_FORMULA,OT_LINES.
  GV_FORMULA = I_FORMULA.

* 分两种情况

  SEARCH GV_FORMULA FOR 'SUM('.
  IF SY-SUBRC = 0.
    GV_CONDITION = 'X'.
  ELSE.
    GV_CONDITION = SPACE.
  ENDIF.

  IF GV_CONDITION EQ SPACE.

* 去掉=

    REPLACE '=' WITH SPACE INTO GV_FORMULA.

* 先通过+来拆分

    SPLIT GV_FORMULA AT '+' INTO TABLE GT_DATA.

* 再通过-来拆分

    LOOP AT GT_DATA ASSIGNING .
      IF GV_FORMULA CA ( '+-').
        SPLIT -STR AT '-' INTO TABLE TEMP_LG.

* IF LINES( TEMP_LG ) > 1.

        LOOP AT TEMP_LG ASSIGNING .
          IF SY-TABIX = 1.
            GS_LINEN-SIGN = '+'.
          ELSE.
            GS_LINEN-SIGN = '-'.
          ENDIF.
          GS_LINEN-REPT_ITEM = -STR.
          IF GS_LINEN-SIGN IS NOT INITIAL AND GS_LINEN-REPT_ITEM IS NOT INITIAL.
            APPEND GS_LINEN TO OT_LINES.
          ENDIF.
        ENDLOOP.

* ENDIF.

      ENDIF.
    ENDLOOP.

* 再通过 / 来拆分

    LOOP AT GT_DATA ASSIGNING .
      SPLIT -STR AT '/' INTO TABLE TEMP_LG.
      IF LINES( TEMP_LG ) > 1.
        LOOP AT TEMP_LG ASSIGNING .
          IF SY-TABIX = 1.
            GS_LINEN-SIGN = '+'.
          ELSE.
            GS_LINEN-SIGN = '/'.
          ENDIF.
          GS_LINEN-REPT_ITEM = -STR.
          IF GS_LINEN-SIGN IS NOT INITIAL AND GS_LINEN-REPT_ITEM IS NOT INITIAL.
            APPEND GS_LINEN TO OT_LINES.
          ENDIF.
        ENDLOOP.
      ENDIF.
    ENDLOOP.

* 再通过 *来拆分

    LOOP AT GT_DATA ASSIGNING .
      SPLIT -STR AT '*' INTO TABLE TEMP_LG.
      IF LINES( TEMP_LG ) > 1.
        LOOP AT TEMP_LG ASSIGNING .
          IF SY-TABIX = 1.
            GS_LINEN-SIGN = '+'.
          ELSE.
            GS_LINEN-SIGN = '*'.
          ENDIF.
          GS_LINEN-REPT_ITEM = -STR.
          IF GS_LINEN-SIGN IS NOT INITIAL AND GS_LINEN-REPT_ITEM IS NOT INITIAL.
            APPEND GS_LINEN TO OT_LINES.
          ENDIF.
        ENDLOOP.
      ENDIF.
    ENDLOOP.

  ELSE.

    SPLIT GV_FORMULA AT '(' INTO G_TEMP_FORMULA1 G_TEMP_FORMULA2. " 用(区分
    REPLACE ')' WITH SPACE INTO G_TEMP_FORMULA2." 去掉)
    SPLIT G_TEMP_FORMULA2 AT ':' INTO G_TEMP_FORMULA1 G_TEMP_FORMULA2."用:区分

    G_FLINE = G_TEMP_FORMULA1.
    G_TLINE =  G_TEMP_FORMULA2.
    IF G_FLINE > G_TLINE.
      RAISE LOW_GT_HIGH.
    ENDIF.

    CLEAR: GV_LINE_POS.
    GV_LINE_POS = G_FLINE." 起始位置赋值给g_line_pos

    IF GV_FORMULA+1(1) = '-'.
      GV_SIGN = '-'.
    ELSE.
      GV_SIGN = '+'.
    ENDIF.

    WHILE GV_LINE_POS LE G_TLINE.

      CLEAR: GS_LINEN.
      GS_LINEN-SIGN = GV_SIGN.
      GS_LINEN-REPT_ITEM = GV_LINE_POS.
      APPEND GS_LINEN TO OT_LINES.

      ADD 1 TO GV_LINE_POS.

    ENDWHILE.

  ENDIF.

ENDFUNCTION.

你可能感兴趣的:(ABAP-公式拆分)