ABAP:VF02过账增强,向BSEG表中插入或修改科目数据

SMOD:SDVFX008

 

 

 

*&---------------------------------------------------------------------*
*&  Include           ZXVVFU08
*&---------------------------------------------------------------------*
*& 变更记录
*&    日期          修改者           传输请求号           修改内容及原因
*&---------------  ----------  --------------------  ------------------*
*    20151210       AB_LEI1          DEVK906098
*    公司代码=1150增强不需要生效
*    20151211      AB_LEI1           DEVK906102
*    如果无对应的SO,则增强不需要生效
*&---------------------------------------------------------------------*

LOOP AT xaccit WHERE aubel <> space AND aupos <> space.
  SELECT COUNT(*)
    FROM vbap
    WHERE vbeln = xaccit-aubel
    AND   posnr = xaccit-aupos.
  IF sy-subrc = 0.
    xaccit-kdauf = xaccit-aubel.
    xaccit-kdpos = xaccit-aupos.

    MODIFY xaccit.
  ENDIF.
ENDLOOP.

****ADD BY ZJ 20230307 S SO2过账增强
IF sy-uname = 'BASIS2' AND
     sy-tcode = 'VF02' AND
     sy-ucomm = 'FKFR'.

  DATA:ls_zsdt051 TYPE zsdt051,
       lt_zsdt051 TYPE TABLE OF zsdt051,
       lv_fkimg   TYPE fkimg.

  DATA:BEGIN OF ls_zsdt051_col,
         vbeln_vl TYPE zsdt051-vbeln_vl,
         posnr_vl TYPE zsdt051-posnr_vl,
         fmeng    TYPE zsdt051-fmeng,
       END OF ls_zsdt051_col.

  DATA:BEGIN OF ls_zsdt051_3.
          INCLUDE TYPE zsdt051.
  DATA:  cut TYPE zsdt051-fmeng,
         END OF ls_zsdt051_3.

  DATA:lt_zsdt051_col LIKE TABLE OF ls_zsdt051_col,
       lt_zsdt051_3   LIKE TABLE OF ls_zsdt051_3.

  LOOP AT cvbrp INTO DATA(ls_vbrp2).
    "查询SO2是否在交货单的增强字段中
    SELECT a~vbeln,a~posnr,a~lfimg,a~uecha,a~matnr
      FROM lips AS a
     INNER JOIN vbuk AS b
        ON a~vbeln EQ b~vbeln
      INTO TABLE @DATA(lt_lips)
     WHERE a~zvbeln EQ @ls_vbrp2-vgbel
       AND a~zposnr EQ @ls_vbrp2-vgpos
       AND b~wbstk EQ 'C'.

    IF sy-subrc EQ 0.

      LOOP AT lt_lips INTO DATA(ls_lips).

        "SO2订单号
        ls_zsdt051_3-vbeln2_va = ls_vbrp2-vgbel.
        "SO2行项目号
        ls_zsdt051_3-posnr2_va = ls_vbrp2-vgpos.
        "交货单
        ls_zsdt051_3-vbeln_vl = ls_lips-vbeln.
        "交货单行项目
        IF ls_lips-uecha IS NOT INITIAL.
          ls_zsdt051_3-posnr_vl = ls_lips-uecha.
        ELSE.
          ls_zsdt051_3-posnr_vl = ls_lips-posnr.
        ENDIF.
        "已交货数量
        ls_zsdt051_3-lfimg = ls_lips-lfimg.
        "过账日期
        SELECT SINGLE wadat_ist
          INTO ls_zsdt051_3-wadat_ist
          FROM likp
         WHERE vbeln EQ ls_lips-vbeln.
        "物料
        ls_zsdt051_3-matnr = ls_lips-matnr.
        APPEND ls_zsdt051_3 TO lt_zsdt051_3.
        CLEAR:ls_zsdt051_3.
        CLEAR:ls_lips.
      ENDLOOP.
      CLEAR:lt_lips.

      "查询已开票数据
      SELECT vbeln_vl,posnr_vl,fmeng
        FROM zsdt051
        INTO TABLE @DATA(lt_zsdt051_2)
       WHERE vbeln2_va EQ @ls_vbrp2-vgbel
         AND posnr2_va EQ @ls_vbrp2-vgpos
         AND fksto EQ ''.

      "合计相同交货单的已开票数量
      LOOP AT lt_zsdt051_2 INTO DATA(ls_zsdt051_2).

        COLLECT ls_zsdt051_2 INTO lt_zsdt051_col.
        CLEAR:ls_zsdt051_2.
      ENDLOOP.

      CLEAR:lt_zsdt051_2.
      LOOP AT lt_zsdt051_3 INTO ls_zsdt051_3.

        READ TABLE lt_zsdt051_col INTO ls_zsdt051_col
                                  WITH KEY vbeln_vl = ls_zsdt051_3-vbeln_vl
                                           posnr_vl = ls_zsdt051_3-posnr_vl.
        IF sy-subrc EQ 0.
          "可开票数量
          ls_zsdt051_3-cut = ls_zsdt051_3-lfimg - ls_zsdt051_col-fmeng.
        ELSE.
          "可开票数量
          ls_zsdt051_3-cut = ls_zsdt051_3-lfimg.
        ENDIF.

        IF ls_zsdt051_3-cut <= 0.
          DELETE lt_zsdt051_3.
        ELSE.
          MODIFY lt_zsdt051_3 FROM ls_zsdt051_3 TRANSPORTING cut.
        ENDIF.
      ENDLOOP.
      CLEAR:lt_zsdt051_col.

      lv_fkimg = ls_vbrp2-fkimg.

      SORT lt_zsdt051_3 BY wadat_ist.
      LOOP AT lt_zsdt051_3 INTO ls_zsdt051_3.

        IF lv_fkimg - ls_zsdt051_3-cut > 0.
          "开票数量
          ls_zsdt051_3-fmeng = ls_zsdt051_3-cut.

          lv_fkimg = lv_fkimg - ls_zsdt051_3-cut.

          MOVE-CORRESPONDING ls_zsdt051_3 TO ls_zsdt051.
          "发票
          ls_zsdt051-vbeln_vf = ls_vbrp2-vbeln.
          "项目
          ls_zsdt051-posnr_vf = ls_vbrp2-posnr.
          ls_zsdt051-erdat = sy-datum.
          ls_zsdt051-erzet = sy-uzeit.
          ls_zsdt051-ernam = sy-uname.
          APPEND ls_zsdt051 TO lt_zsdt051.
          CLEAR:ls_zsdt051.
        ELSE.
          "开票数量
          ls_zsdt051_3-fmeng = lv_fkimg.

          MOVE-CORRESPONDING ls_zsdt051_3 TO ls_zsdt051.
          "发票
          ls_zsdt051-vbeln_vf = ls_vbrp2-vbeln.
          "项目
          ls_zsdt051-posnr_vf = ls_vbrp2-posnr.
          ls_zsdt051-erdat = sy-datum.
          ls_zsdt051-erzet = sy-uzeit.
          ls_zsdt051-ernam = sy-uname.
          APPEND ls_zsdt051 TO lt_zsdt051.
          CLEAR:ls_zsdt051.
          EXIT.
        ENDIF.
        CLEAR:ls_zsdt051_3.
      ENDLOOP.
      CLEAR:lt_zsdt051_3,lv_fkimg.
      "更新数据库表
      MODIFY zsdt051 FROM TABLE lt_zsdt051.
      COMMIT WORK AND WAIT.
    ELSE.
      CONTINUE.
    ENDIF.
    CLEAR:ls_vbrp2.
  ENDLOOP.



  DATA:lv_dmbtr TYPE p LENGTH 10 DECIMALS 6.
  "取ZSDT051数据
  SELECT *
    FROM zsdt051
    INTO TABLE @DATA(lt_zsdt051_5)
   WHERE vbeln_vf EQ @cvbrk-vbeln
     AND fksto EQ ''.
  IF sy-subrc = 0.
    SORT lt_zsdt051_5 BY vbeln_vf posnr_vf.
    DELETE ADJACENT DUPLICATES FROM lt_zsdt051_5 COMPARING vbeln_vf posnr_vf.

    LOOP AT lt_zsdt051_5 INTO DATA(ls_zsdt051_5).

      SELECT *
        FROM zsdt051
        INTO TABLE @DATA(lt_zsdt051_4)
       WHERE vbeln_vf EQ @ls_zsdt051_5-vbeln_vf
         AND posnr_vf EQ @ls_zsdt051_5-posnr_vf
         AND fksto EQ ''.

      IF lt_zsdt051_4 IS NOT INITIAL.
        "查询对应的物料,工厂
        SELECT a~vbeln,a~posnr,a~matnr,a~werks,b~wadat_ist
          FROM lips AS a
         INNER JOIN likp AS b
            ON a~vbeln EQ b~vbeln
          INTO TABLE @DATA(lt_lips3)
           FOR ALL ENTRIES IN @lt_zsdt051_4
         WHERE a~vbeln EQ @lt_zsdt051_4-vbeln_vl
           AND a~posnr EQ @lt_zsdt051_4-posnr_vl.

        SORT lt_lips3 BY vbeln posnr.

        IF lt_lips3 IS NOT INITIAL.
          "查询成本和价格单位
          SELECT matnr,bwkey,stprs,peinh,
                 lfgja,lfmon,vprsv,verpr
            FROM mbewh
            INTO TABLE @DATA(lt_mbew2)
             FOR ALL ENTRIES IN @lt_lips3
           WHERE matnr EQ @lt_lips3-matnr
             AND bwkey EQ @lt_lips3-werks
             AND lfgja EQ @lt_lips3-wadat_ist+0(4)
             AND lfmon EQ @lt_lips3-wadat_ist+4(2).

          SORT lt_mbew2 BY matnr bwkey lfgja lfmon.

          SELECT matnr,bwkey,stprs,peinh,
                 lfgja,lfmon,vprsv,verpr
            FROM mbew
            INTO TABLE @DATA(lt_mbew3)
             FOR ALL ENTRIES IN @lt_lips3
           WHERE matnr EQ @lt_lips3-matnr
             AND bwkey EQ @lt_lips3-werks.

          SORT lt_mbew3 BY matnr bwkey.
          "计算成本价格
          LOOP AT lt_zsdt051_4 INTO DATA(ls_zsdt051_4).

            READ TABLE lt_lips3 INTO DATA(ls_lips3)
                               WITH KEY vbeln = ls_zsdt051_4-vbeln_vl
                                        posnr = ls_zsdt051_4-posnr_vl
                               BINARY SEARCH.
            IF sy-subrc EQ 0.

              SELECT SINGLE bukrs
                INTO @DATA(lv_bukrs)
                FROM t001k
               WHERE bwkey EQ @ls_lips3-werks.

              IF sy-subrc EQ 0.
                SELECT SINGLE waers
                  INTO @DATA(lv_waers)
                  FROM t001
                 WHERE bukrs EQ @lv_bukrs.
              ENDIF.

              READ TABLE lt_mbew2 INTO DATA(ls_mbew2)
                                 WITH KEY matnr = ls_lips3-matnr
                                          bwkey = ls_lips3-werks
                                          lfgja = ls_lips3-wadat_ist+0(4)
                                          lfmon = ls_lips3-wadat_ist+4(2)
                                 BINARY SEARCH.
              IF sy-subrc EQ 0.

                IF ls_mbew2-peinh = 0.
                  ls_mbew2-peinh = 1.
                ENDIF.

                IF ls_mbew2-vprsv EQ 'V'.
                  lv_dmbtr = lv_dmbtr + ls_zsdt051_4-fmeng * ls_mbew2-verpr / ls_mbew2-peinh.
                  ls_zsdt051_4-dmbtr = ls_mbew2-verpr.
                  ls_zsdt051_4-kpein = ls_mbew2-peinh.
                ELSEIF ls_mbew2-vprsv EQ 'S'.
                  lv_dmbtr = lv_dmbtr + ls_zsdt051_4-fmeng * ls_mbew2-stprs / ls_mbew2-peinh.
                  ls_zsdt051_4-dmbtr = ls_mbew2-stprs.
                  ls_zsdt051_4-kpein = ls_mbew2-peinh.
                ENDIF.

                CLEAR:ls_mbew2.
              ELSE.
                READ TABLE lt_mbew3 INTO DATA(ls_mbew3)
                                 WITH KEY matnr = ls_lips3-matnr
                                          bwkey = ls_lips3-werks
                                 BINARY SEARCH.
                IF sy-subrc EQ 0.

                  IF ls_mbew3-peinh = 0.
                    ls_mbew3-peinh = 1.
                  ENDIF.

                  IF ls_mbew3-vprsv EQ 'V'.
                    lv_dmbtr = lv_dmbtr + ls_zsdt051_4-fmeng * ls_mbew3-verpr / ls_mbew3-peinh.
                    ls_zsdt051_4-dmbtr = ls_mbew3-verpr.
                    ls_zsdt051_4-kpein = ls_mbew3-peinh.
                  ELSEIF ls_mbew3-vprsv EQ 'S'.
                    lv_dmbtr = lv_dmbtr + ls_zsdt051_4-fmeng * ls_mbew3-stprs / ls_mbew3-peinh.
                    ls_zsdt051_4-dmbtr = ls_mbew3-stprs.
                    ls_zsdt051_4-kpein = ls_mbew3-peinh.
                  ENDIF.

                  CLEAR:ls_mbew3.
                ENDIF.
              ENDIF.
              CLEAR:ls_lips3.
            ENDIF.
            MODIFY zsdt051 FROM ls_zsdt051_4.
            COMMIT WORK.
            CLEAR:ls_zsdt051_4.
          ENDLOOP.
        ENDIF.

        "xaccit中追加科目数据
        DATA:ls_xaccit  LIKE xaccit,
             ls_xaccit2 LIKE xaccit,
             lt_xaccit2 LIKE TABLE OF xaccit,
             ls_xacccr  LIKE xacccr,
             lt_xacccr  LIKE TABLE OF xacccr,
             lt_xacccr2 LIKE TABLE OF xacccr,
             lv_kursf   TYPE kursf.
        DATA(lt_xaccit) = xaccit[].
        DATA:lv_posnr TYPE posnr.

        SORT lt_xaccit BY posnr DESCENDING.
        READ TABLE lt_xaccit INTO ls_xaccit INDEX 1.
        IF sy-subrc EQ 0.
          lv_posnr = ls_xaccit-posnr.
          CLEAR:ls_xaccit.
        ENDIF.


        READ TABLE lt_xaccit INTO ls_xaccit2 WITH KEY hkont = '0610100000'
                                                      aubel = ls_zsdt051_5-vbeln2_va
                                                      aupos = ls_zsdt051_5-posnr2_va.
        DO 2 TIMES.
          lv_posnr = lv_posnr + 1.
          ls_xaccit-mandt = ls_xaccit2-mandt.
          ls_xaccit-awtyp = ls_xaccit2-awtyp.
          ls_xaccit-awref = ls_xaccit2-awref.
          ls_xaccit-posnr = lv_posnr.
          ls_xaccit-bukrs = ls_xaccit2-bukrs.
          ls_xaccit-belnr = ls_xaccit2-belnr.
          ls_xaccit-bldat = ls_xaccit2-bldat.
          ls_xaccit-budat = ls_xaccit2-budat.
          ls_xaccit-wwert = ls_xaccit2-wwert.
          ls_xaccit-blart = ls_xaccit2-blart.
          ls_xaccit-xblnr = ls_xaccit2-xblnr.
          ls_xaccit-xskrl = 'X'.
          ls_xaccit-kokrs = ls_xaccit2-kokrs.
          ls_xaccit-matnr = ls_xaccit2-matnr.
          ls_xaccit-menge = ls_xaccit2-menge.
          ls_xaccit-meins = ls_xaccit2-meins.
          ls_xaccit-werks = ls_xaccit2-werks.
          ls_xaccit-paobjnr = ls_xaccit2-paobjnr.
          ls_xaccit-prctr = ls_xaccit2-prctr.
          ls_xaccit-posnr_sd = ls_xaccit2-posnr_sd.
          ls_xaccit-stunr = '930'.
          ls_xaccit-zaehk = ls_xaccit2-zaehk.
          ls_xaccit-fkart = ls_xaccit2-fkart.
          ls_xaccit-vtweg = ls_xaccit2-vtweg.
          ls_xaccit-kunag = ls_xaccit2-kunag.
          ls_xaccit-spart = ls_xaccit2-spart.
          ls_xaccit-vkorg = ls_xaccit2-vkorg.
          ls_xaccit-fkdat = ls_xaccit2-fkdat.
          ls_xaccit-fktyp = ls_xaccit2-fktyp.
          ls_xaccit-fbuda = ls_xaccit2-fbuda.
          ls_xaccit-aubel = ls_xaccit2-aubel.
          ls_xaccit-aupos = ls_xaccit2-aupos.
          ls_xaccit-vbel2 = ls_xaccit2-aubel.
          ls_xaccit-posn2 = ls_xaccit2-aupos.
          ls_xaccit-fkimg = ls_xaccit2-fkimg.
          ls_xaccit-vrkme = ls_xaccit2-vrkme.
          ls_xaccit-fklmg = ls_xaccit2-fklmg.
          ls_xaccit-kappl = ls_xaccit2-kappl.
          ls_xaccit-kntyp = 'G'.
          ls_xaccit-kursk = ls_xaccit2-kursk.
          ls_xaccit-kzzuab = ls_xaccit2-kzzuab.
          ls_xaccit-ntgew = ls_xaccit2-ntgew.
          ls_xaccit-brgew = ls_xaccit2-brgew.
          ls_xaccit-gewei = ls_xaccit2-gewei.
          ls_xaccit-volum = ls_xaccit2-volum.
          ls_xaccit-voleh = ls_xaccit2-voleh.
          ls_xaccit-vbtyp = ls_xaccit2-vbtyp.
          ls_xaccit-koaid = ls_xaccit2-koaid.
          ls_xaccit-koaid = ls_xaccit2-kidno.
          ls_xaccit-kruek = 'X'.
          IF sy-index = 1.
            ls_xaccit-hkont = '0220401010'.
            ls_xaccit-bschl = 50.
            ls_xaccit-kschl = 'VPRS'.
          ELSE.
            ls_xaccit-hkont = '0710110000'.
            ls_xaccit-bschl = 40.
            ls_xaccit-kschl = 'VPRS'.
          ENDIF.
          APPEND ls_xaccit TO lt_xaccit2.
          CLEAR:ls_xaccit.
        ENDDO.

        CLEAR:lv_posnr.
        lt_xacccr2 = xacccr[].
        SORT lt_xacccr2 BY posnr DESCENDING.
        READ TABLE lt_xacccr2 INTO ls_xacccr INDEX 1.
        IF sy-subrc EQ 0.
          lv_posnr = ls_xacccr-posnr.
          lv_kursf = ls_xacccr-kursf.
          CLEAR:ls_xacccr.
        ENDIF.

        DATA:lv_rate TYPE string,
             lv_wrbtr TYPE string,
             lv_wrbtr2 TYPE string,
             lv_waers_2 TYPE waers.
        SELECT SINGLE waers
          FROM t001
          INTO @DATA(lv_waers2)
         WHERE bukrs EQ @cvbrk-bukrs.

        DO 2 TIMES.
          lv_posnr = lv_posnr + 1.
          CLEAR:ls_xacccr.
          ls_xacccr-mandt = sy-mandt.
          ls_xacccr-awtyp = 'VBRK'.
          ls_xacccr-awref = cvbrk-vbeln.
          ls_xacccr-posnr = lv_posnr.
          ls_xacccr-curtp = 10.
          ls_xacccr-waers = lv_waers2.
          ls_xacccr-kursf = lv_kursf.
          "汇率转换
          CALL FUNCTION 'CONVERT_TO_LOCAL_CURRENCY'
            EXPORTING
              date             = cvbrk-fkdat
              foreign_amount   = lv_dmbtr
              foreign_currency = lv_waers
              local_currency   = ls_xacccr-waers
            IMPORTING
              exchange_rate    = lv_rate.
          IF sy-index = 1.
            ls_xacccr-wrbtr = lv_dmbtr * lv_rate * -1.
          ELSE.
            ls_xacccr-wrbtr = lv_dmbtr * lv_rate.
          ENDIF.
          lv_waers_2 = ls_xacccr-waers.
          lv_wrbtr2 = ls_xacccr-wrbtr.
          APPEND ls_xacccr TO lt_xacccr.
          CLEAR:ls_xacccr,lv_wrbtr,lv_rate.
        ENDDO.

        lv_posnr = lv_posnr - 2.
        DO 2 TIMES.
          lv_posnr = lv_posnr + 1.
          CLEAR:ls_xacccr.
          ls_xacccr-mandt = sy-mandt.
          ls_xacccr-awtyp = 'VBRK'.
          ls_xacccr-awref = cvbrk-vbeln.
          ls_xacccr-posnr = lv_posnr.
          ls_xacccr-curtp = '00'.
          ls_xacccr-waers = cvbrk-waerk.
          ls_xacccr-kursf = lv_kursf.
          "汇率转换
          CALL FUNCTION 'CONVERT_TO_LOCAL_CURRENCY'
            EXPORTING
              date             = cvbrk-fkdat
              foreign_amount   = lv_wrbtr2
              foreign_currency = lv_waers_2
              local_currency   = ls_xacccr-waers
            IMPORTING
              exchange_rate    = lv_rate.
          IF sy-index = 1.
            ls_xacccr-wrbtr = lv_wrbtr2 * lv_rate * -1.
          ELSE.
            ls_xacccr-wrbtr = lv_wrbtr2 * lv_rate.
          ENDIF.
          APPEND ls_xacccr TO lt_xacccr.
          CLEAR:ls_xacccr.
        ENDDO.
        APPEND LINES OF lt_xaccit2 TO xaccit[].
        APPEND LINES OF lt_xacccr TO xacccr[].

      ENDIF.

      CLEAR:ls_zsdt051_5,lv_dmbtr,lt_zsdt051_4,lt_lips3,lt_mbew2,
            lt_mbew3,lv_bukrs,lv_waers,lt_xaccit,lv_posnr,ls_xaccit,
            ls_xaccit2,lt_xaccit2,ls_xacccr,lt_xacccr,lv_kursf,lv_wrbtr2,lv_waers.
    ENDLOOP.

  ENDIF.
ENDIF.
****ADD BY ZJ 20230307 E SO2过账增强

 

posted @ 2023-03-15 20:50  阿胖的阿多  阅读(988)  评论(0编辑  收藏  举报