ODATA~CREATE_DEEP_ENTITY 应用方法

应用场景介绍:该方法常用于Fiori 端多维度分析报表数据获取,传入多个变量可以返回多个维度的Table数据
例如:

file

一、方法路径

file
file

二、测试URL 通过?$expand=TOSUM,TOUTAXITEM 方法返回多个Table

/sap/opu/odata/SAP/ZMASTERODATA_SRV/USEDTAXSet(Taxsdate=datetime'2019-07-01T00:00:00',Taxedate=datetime'2019-07-31T00:00:00',Sdate=datetime'1999-01-01T00:00:00',Edate=datetime'2019-12-15T00:00:00',ProPersnBukrs='',Skfid='',Taxnum='',Zwrbtrlow='',Zzsfp='1',Ifdeduc='',Ifele='')?$expand=TOSUM,TOUTAXITEM

三、实施方法
1、新建Entity Types ,其次参照下图创建关联关系

file
file

2、编写后台代码

  METHOD /IWBEP/IF_MGW_APPL_SRV_RUNTIME~GET_EXPANDED_ENTITY.

    CASE IO_TECH_REQUEST_CONTEXT->GET_ENTITY_TYPE_NAME( ).
      WHEN 'USEDTAX'.
        DATA: BEGIN OF LS_KEYS,
                TAXSDATE              TYPE DATUM, "发票开始日期
                TAXEDATE              TYPE DATUM, "发票结束日期
                SDATE                 TYPE DATUM, "记账开始日期
                EDATE                 TYPE DATUM, "记账结束日期
                ZZSFP(20), " 增税发票类型
                IFDEDUC(1), "可否抵扣
                IFELE(1),"是否电子发票
                PRO_PERSN_BUKRS(1000), "单位代码
                TAXNUM(10),"发票号码
                SKFID(10),"开票方
                ZWRBTRLOW(17), "金额下限
              END OF LS_KEYS.

        DATA:BEGIN OF LS_USEDTAX.
               INCLUDE TYPE ZSUTAXHEAD.
               DATA:TOSUM      TYPE TABLE OF ZSUTAXSUM,
               TOUTAXITEM TYPE TABLE OF ZSUTAXITEM,
             END OF LS_USEDTAX.

        TYPES: BEGIN OF TY_TAXSUM,"合计表
                 ZZSFP(10),         "增税发票类型
                 ZZSFPTXT(20),       "描述
                 IFDEDUC(4),       "可否抵扣
                 IFELE        TYPE ZSUTAXSUM-IFELE,         "是否电子发票
                 ZNUM         TYPE I,         "数量
                 WRBTR        TYPE ZSUTAXSUM-WRBTR,         "含税金额
                 PROBTR       TYPE ZSUTAXSUM-PROBTR,         "不含税金额
                 TAXBTR       TYPE ZSUTAXSUM-TAXBTR,         "税额
               END OF TY_TAXSUM.

        DATA:LT_TAXSUM   TYPE TABLE OF TY_TAXSUM,
             LS_TAXSUM   LIKE LINE OF  LT_TAXSUM,
             LT_TAXSUM_T TYPE TABLE OF ZSUTAXSUM,
             LS_TAXSUM_T LIKE LINE OF  LT_TAXSUM_T.

        DATA: LT_BUKRS      TYPE RANGE OF ZPRO_PERSN_BUKRS,
              LRT_SKFID     TYPE RANGE OF ZPRO_INSTANCEID,
              LRT_TAXNUM    TYPE RANGE OF ZSUTAXHEAD-TAXNUM,
              LRT_ZWRBTRLOW TYPE RANGE OF ZSUTAXHEAD-ZWRBTRLOW,
              LRT_ZZSFP     TYPE RANGE OF ZSUTAXHEAD-ZZSFP,
              LRT_IFDEDUC   TYPE RANGE OF ZSUTAXHEAD-IFDEDUC,
              LRT_IFELE     TYPE RANGE OF ZSUTAXHEAD-IFELE.

        DATA:LT_BUKRS1    TYPE RANGE OF ZPRO_PERSN_BUKRS,
             LS_BUKRS1    LIKE LINE OF LT_BUKRS1,
             LRT_TAX_DATE TYPE RANGE OF DATUM,
             LRT_DATE     TYPE RANGE OF DATUM,
             LV_N         TYPE I,
             LT_TAXITEM   TYPE TABLE OF ZSUTAXITEM.

        DATA: W_TZONE TYPE TZONREF-TZONE.
        DATA: GV_DATE_TIME_BEGIN TYPE ZTPROCESS-YVOUCHER_TIME,
              GV_DATE_TIME_END   TYPE ZTPROCESS-YVOUCHER_TIME,
              P_TIM_B            TYPE SY-UZEIT VALUE '000000',
              P_TIM_E            TYPE SY-UZEIT VALUE '235959'.

        IO_TECH_REQUEST_CONTEXT->GET_CONVERTED_KEYS(
           IMPORTING
             ES_KEY_VALUES = LS_KEYS
                ).

        "传入参数
        IF LS_KEYS-PRO_PERSN_BUKRS IS NOT INITIAL.
          SPLIT LS_KEYS-PRO_PERSN_BUKRS AT ',' INTO TABLE LT_BUKRS.
          LOOP AT LT_BUKRS INTO DATA(LS_BUKRS).
            LS_BUKRS1-SIGN = 'I'.
            LS_BUKRS1-OPTION  = 'EQ'.
            LS_BUKRS1-LOW = LS_BUKRS.
            APPEND LS_BUKRS1 TO LT_BUKRS1.
          ENDLOOP.
        ENDIF.

        IF LS_KEYS-SKFID IS NOT INITIAL.
          LRT_SKFID = VALUE #( SIGN = 'I' OPTION = 'EQ' ( LOW = LS_KEYS-SKFID ) ).
        ENDIF.

        IF LS_KEYS-TAXNUM IS NOT INITIAL.
          LRT_TAXNUM = VALUE #( SIGN = 'I' OPTION = 'EQ' ( LOW = LS_KEYS-TAXNUM ) ).
        ENDIF.

        IF LS_KEYS-ZWRBTRLOW IS NOT INITIAL.
          LRT_ZWRBTRLOW = VALUE #( SIGN = 'I' OPTION = 'GE' ( LOW =  LS_KEYS-ZWRBTRLOW ) ).
        ENDIF.

        IF LS_KEYS-ZZSFP IS NOT INITIAL.
          LRT_ZZSFP = VALUE #( SIGN = 'I' OPTION = 'EQ' ( LOW = LS_KEYS-ZZSFP ) ).
        ENDIF.

        IF LS_KEYS-IFDEDUC IS NOT INITIAL.
          LRT_IFDEDUC = VALUE #( SIGN = 'I' OPTION = 'EQ' ( LOW = LS_KEYS-IFDEDUC ) ).
        ENDIF.

        IF LS_KEYS-IFELE IS NOT INITIAL.
          LRT_IFELE = VALUE #( SIGN = 'I' OPTION = 'EQ' ( LOW = LS_KEYS-IFELE ) ).
        ENDIF.

*        "记账开始日期
*        IF LS_KEYS-SDATE IS NOT INITIAL AND LS_KEYS-EDATE IS NOT INITIAL.
*          LRT_DATE =
*            VALUE #( SIGN = 'I' OPTION = 'BT' ( LOW = LS_KEYS-SDATE HIGH = LS_KEYS-EDATE  ) ).
*        ELSEIF LS_KEYS-SDATE IS NOT INITIAL.
*          LRT_DATE =
*            VALUE #( SIGN = 'I' OPTION = 'EQ' ( LOW = LS_KEYS-SDATE  ) ).
*        ELSEIF LS_KEYS-EDATE IS NOT INITIAL.
*          LRT_DATE =
*            VALUE #( SIGN = 'I' OPTION = 'LE' ( LOW = LS_KEYS-EDATE  ) ).
*        ENDIF.
*
        CONVERT DATE LS_KEYS-SDATE TIME P_TIM_B INTO TIME STAMP GV_DATE_TIME_BEGIN TIME ZONE  W_TZONE.
        CONVERT DATE LS_KEYS-EDATE TIME P_TIM_E INTO TIME STAMP GV_DATE_TIME_END TIME ZONE  W_TZONE.
*
        "发票开始日期
        IF LS_KEYS-TAXSDATE IS NOT INITIAL AND LS_KEYS-TAXEDATE IS NOT INITIAL.
          LRT_DATE =
            VALUE #( SIGN = 'I' OPTION = 'BT' ( LOW = LS_KEYS-TAXSDATE HIGH = LS_KEYS-TAXEDATE  ) ).
        ELSEIF LS_KEYS-TAXSDATE IS NOT INITIAL.
          LRT_DATE =
            VALUE #( SIGN = 'I' OPTION = 'EQ' ( LOW = LS_KEYS-TAXSDATE  ) ).
        ELSEIF LS_KEYS-TAXEDATE IS NOT INITIAL.
          LRT_DATE =
            VALUE #( SIGN = 'I' OPTION = 'LE' ( LOW = LS_KEYS-TAXEDATE  ) ).
        ENDIF.

        SELECT * FROM  ZTSJIT INTO TABLE @DATA(LT_ZTSJIT) WHERE ZZSFP NOT IN ( '1' ,'0' ) AND EXTD4 <> ''.
        SELECT * INTO TABLE @DATA(LT_DD07T) FROM DD07T WHERE DOMNAME = 'ZZSFP'.
        SELECT * INTO TABLE @DATA(LT_UNIT) FROM ZTUNIT WHERE IFAVA <> 'X'.

        SELECT PRO_INSID,PRO_PERSN_BUKRS,YVOUCHER_BELNR,YVOUCHER_TIME,
               BXDNR,ZZSFP,IFDEDUC,IFELE,WRBTR,PROBTR,TAXBTR,TAXDATE,TAXNUM,TAXCODE,SKFID,SKFXT
                              INTO TABLE @DATA(LT_TAX)
                              FROM ZTPROCESS AS A
                              INNER JOIN ZTSJIT AS B
                              ON A~BDHNR = B~BXDNR
                              WHERE  B~ZZSFP  IN @LRT_ZZSFP
                                 AND B~IFDEDUC  IN @LRT_IFDEDUC
                                 AND B~TAXDATE  IN @LRT_DATE"发票日期
                                 AND B~SKFID  IN @LRT_SKFID
                                 AND B~TAXNUM  IN @LRT_TAXNUM
                                 AND B~WRBTR  IN @LRT_ZWRBTRLOW
                                 AND B~IFELE  IN @LRT_IFELE
                                 AND A~PRO_PERSN_BUKRS  IN @LT_BUKRS1
                                 AND A~YVOUCHER_TIME BETWEEN @GV_DATE_TIME_BEGIN AND @GV_DATE_TIME_END"记账日期
                                 AND A~PRO_STATUS  = '9'.

        IF SY-SUBRC = 0.

          MOVE-CORRESPONDING LS_KEYS TO LS_USEDTAX."查询
          "MOVE-CORRESPONDING LT_TAX TO LT_TAXSUM."合计
          MOVE-CORRESPONDING LT_TAX TO LT_TAXITEM."明细

*--------------------------------------------------------------------*
*合计数据
          SORT LT_TAX BY ZZSFP IFDEDUC IFELE.

          LOOP AT LT_TAX ASSIGNING FIELD-SYMBOL(<FS_LTTAX>).
            LS_TAXSUM_T-ZZSFP = <FS_LTTAX>-ZZSFP.
*            READ TABLE LT_DD07T ASSIGNING FIELD-SYMBOL(<LT_DD07T>) WITH  KEY VALPOS = LS_TAXSUM_T-ZZSFP.
*            IF SY-SUBRC = 0.
*              LS_TAXSUM_T-ZZSFPTXT = <LT_DD07T>-DDTEXT.
*            ENDIF.
            LS_TAXSUM_T-IFDEDUC = <FS_LTTAX>-IFDEDUC.
            LS_TAXSUM_T-IFELE = <FS_LTTAX>-IFELE.
            LS_TAXSUM_T-WRBTR  = <FS_LTTAX>-WRBTR.
            LS_TAXSUM_T-PROBTR = <FS_LTTAX>-PROBTR.
            LS_TAXSUM_T-TAXBTR = <FS_LTTAX>-TAXBTR.

*            LS_TAXSUM_T-TAXSDATE = LS_KEYS-TAXSDATE.
*            LS_TAXSUM_T-TAXEDATE = LS_KEYS-TAXEDATE.
*            LS_TAXSUM_T-SDATE = LS_KEYS-SDATE.
*            LS_TAXSUM_T-EDATE = LS_KEYS-EDATE.
*            IF LS_TAXSUM_T-IFELE = '1'."是否电子发票
*              LS_TAXSUM_T-IFELE = '是'.
*            ELSEIF LS_TAXSUM_T-IFELE = '0'.
*              LS_TAXSUM_T-IFELE = '否'.
*            ENDIF.
*            IF LS_TAXSUM_T-IFDEDUC = '1'."可否抵扣
*              LS_TAXSUM_T-IFDEDUC = '可抵扣 '.
*            ELSEIF LS_TAXSUM_T-IFDEDUC = '0'.
*              LS_TAXSUM_T-IFDEDUC = '不可抵扣'.
*            ENDIF.
            LS_TAXSUM_T-ZNUM = '1'.
            COLLECT LS_TAXSUM_T INTO LT_TAXSUM_T.

*合计
            CLEAR:LS_TAXSUM_T-ZZSFP,LS_TAXSUM_T-IFDEDUC,LS_TAXSUM_T-IFELE.
            LS_TAXSUM_T-ZZSFPTXT = '合计'.
            LS_TAXSUM_T-WRBTR  = <FS_LTTAX>-WRBTR.
            LS_TAXSUM_T-PROBTR = <FS_LTTAX>-PROBTR.
            LS_TAXSUM_T-TAXBTR = <FS_LTTAX>-TAXBTR.
            LS_TAXSUM_T-ZNUM = '1'.
            COLLECT LS_TAXSUM_T INTO LT_TAXSUM_T.
            CLEAR LS_TAXSUM_T.
          ENDLOOP.
          SORT LT_TAXSUM_T BY ZNUM ZZSFP IFDEDUC IFELE.
*          DESCRIBE TABLE LT_TAXSUM_T LINES LV_N.
*          APPEND INITIAL LINE TO LT_TAXSUM_T ASSIGNING FIELD-SYMBOL(<FS_TAXSUM_T>).

*          LOOP AT LT_TAXSUM_T INTO LS_TAXSUM_T.
*            IF SY-TABIX <= LV_N.
*              <FS_TAXSUM_T>-ZZSFP = '合计'.
*              <FS_TAXSUM_T>-ZNUM = <FS_TAXSUM_T>-ZNUM + LS_TAXSUM_T-ZNUM.
*              <FS_TAXSUM_T>-WRBTR = <FS_TAXSUM_T>-WRBTR + LS_TAXSUM_T-WRBTR.
*              <FS_TAXSUM_T>-PROBTR = <FS_TAXSUM_T>-PROBTR + LS_TAXSUM_T-PROBTR.
*              <FS_TAXSUM_T>-TAXBTR = <FS_TAXSUM_T>-TAXBTR + LS_TAXSUM_T-TAXBTR.
*            ENDIF.
*          ENDLOOP.
*          UNASSIGN <FS_TAXSUM_T>.

          MOVE-CORRESPONDING LT_TAXSUM_T TO LS_USEDTAX-TOSUM.

          "LOOP AT LS_USEDTAX-TOSUM ASSIGNING FIELD-SYMBOL(<LS_SUM>).

*            READ TABLE LT_DD07T ASSIGNING FIELD-SYMBOL(<LT_DD07T>) WITH  KEY VALPOS = <LS_SUM>-ZZSFP.
*            IF SY-SUBRC = 0.
*              <LS_SUM>-ZZSFPTXT = <LT_DD07T>-DDTEXT.
*            ENDIF.
*            <LS_SUM>-TAXSDATE = LS_KEYS-TAXSDATE.
*            <LS_SUM>-TAXEDATE = LS_KEYS-TAXEDATE.
*            <LS_SUM>-SDATE = LS_KEYS-SDATE.
*            <LS_SUM>-EDATE = LS_KEYS-EDATE.
*            IF <LS_SUM>-IFELE = '1'."是否电子发票
*              <LS_SUM>-IFELE = '是'.
*            ELSEIF <LS_SUM>-IFELE = '0'.
*              <LS_SUM>-IFELE = '否'.
*            ENDIF.
*            IF <LS_SUM>-IFDEDUC = '1'."可否抵扣
*              <LS_SUM>-IFDEDUC = '可抵扣 '.
*            ELSEIF <LS_SUM>-IFDEDUC = '0'.
*              <LS_SUM>-IFDEDUC = '不可抵扣'.
*            ENDIF.
          " ENDLOOP.

*---------------------------------------------------------------------*
*明细数据
          MOVE-CORRESPONDING LT_TAXITEM TO LS_USEDTAX-TOUTAXITEM.

          LOOP AT LS_USEDTAX-TOUTAXITEM ASSIGNING FIELD-SYMBOL(<LS_ITEM>).
            <LS_ITEM>-TAXSDATE = LS_KEYS-TAXSDATE.
            <LS_ITEM>-TAXEDATE = LS_KEYS-TAXEDATE.
            <LS_ITEM>-SDATE = LS_KEYS-SDATE.
            <LS_ITEM>-EDATE = LS_KEYS-EDATE.

            READ TABLE LT_ZTSJIT ASSIGNING FIELD-SYMBOL(<FS_ZTSJIT>) WITH  KEY BXDNR = <LS_ITEM>-BXDNR.
            IF SY-SUBRC = 0.
              <LS_ITEM>-EXTD4 = <FS_ZTSJIT>-EXTD4."乘车人
            ENDIF.

            CONVERT TIME STAMP  <LS_ITEM>-YVOUCHER_TIME TIME ZONE W_TZONE INTO DATE <LS_ITEM>-BUDATE."记账日期

            IF <LS_ITEM>-IFELE = '1'."是否电子发票
              <LS_ITEM>-IFELE = '是'.
            ELSEIF <LS_ITEM>-IFELE = '0'.
              <LS_ITEM>-IFELE = '否'.
            ENDIF.
            IF <LS_ITEM>-IFDEDUC = '1'."可否抵扣
              <LS_ITEM>-IFDEDUC = '可抵扣 '.
            ELSEIF <LS_ITEM>-IFDEDUC = '0'.
              <LS_ITEM>-IFDEDUC = '不可抵扣'.
            ENDIF.
          ENDLOOP.

          APPEND INITIAL LINE TO ET_EXPANDED_TECH_CLAUSES ASSIGNING FIELD-SYMBOL(<LS_CLAUSES>).
          IF <LS_CLAUSES> IS ASSIGNED.
            <LS_CLAUSES> = 'TOSUM'.
            UNASSIGN <LS_CLAUSES>.
          ENDIF.

          APPEND INITIAL LINE TO  ET_EXPANDED_TECH_CLAUSES ASSIGNING FIELD-SYMBOL(<LS_CLAUSES1>).
          IF <LS_CLAUSES1> IS ASSIGNED.
            <LS_CLAUSES1> = 'TOUTAXITEM'.
            UNASSIGN <LS_CLAUSES1>.
          ENDIF.

          COPY_DATA_TO_REF(
                 EXPORTING
                   IS_DATA = LS_USEDTAX
                 CHANGING
                   CR_DATA = ER_ENTITY
                      ).

        ENDIF.
      WHEN OTHERS.
    ENDCASE.
  ENDMETHOD.
RIPRO主题是一个优秀的主题,极致后台体验,无插件,集成会员系统
SapiBook » ODATA~CREATE_DEEP_ENTITY 应用方法

1 评论

发表评论