其他分享
首页 > 其他分享> > SAP 交货单开票(VF01)

SAP 交货单开票(VF01)

作者:互联网

**常规开票使用bapi,抬头定价条件使用bdc

 

FUNCTION ZCRM0200_IDOC_PROCESS .

*"----------------------------------------------------------------------

*"*"本地接口:

*"  IMPORTING

*"     VALUE(INPUT_METHOD) LIKE  BDWFAP_PAR-INPUTMETHD

*"     VALUE(MASS_PROCESSING) LIKE  BDWFAP_PAR-MASS_PROC

*"  EXPORTING

*"     VALUE(WORKFLOW_RESULT) LIKE  BDWFAP_PAR-RESULT

*"     VALUE(APPLICATION_VARIABLE) LIKE  BDWFAP_PAR-APPL_VAR

*"     VALUE(IN_UPDATE_TASK) LIKE  BDWFAP_PAR-UPDATETASK

*"     VALUE(CALL_TRANSACTION_DONE) LIKE  BDWFAP_PAR-CALLTRANS

*"  TABLES

*"      IDOC_CONTRL STRUCTURE  EDIDC

*"      IDOC_DATA STRUCTURE  EDIDD

*"      IDOC_STATUS STRUCTURE  BDIDOCSTAT

*"      RETURN_VARIABLES STRUCTURE  BDWFRETVAR

*"      SERIALIZATION_INFO STRUCTURE  BDI_SER

*"      EDI_TEXT STRUCTURE  EDIORDTXT1 OPTIONAL

*"      EDI_TEXT_LINES STRUCTURE  EDIORDTXT2 OPTIONAL

*"----------------------------------------------------------------------

  "bapi定义

  DATA:

    LT_BILLING          TYPE STANDARD TABLE OF BAPIVBRK,

    LT_RETURN1          TYPE STANDARD TABLE OF BAPIRETURN1,

    LT_SUCCESS          TYPE STANDARD TABLE OF BAPIVBRKSUCCESS,

    LT_ERRORS           TYPE STANDARD TABLE OF  BAPIVBRKERRORS,

    LT_CONDITIONDATAIN  TYPE STANDARD TABLE OF BAPIKOMV,

    LT_CONDITIONDATAIN1 TYPE STANDARD TABLE OF BAPIKOMV,

    LS_BILLING          TYPE BAPIVBRK,

    LS_RETURN1          TYPE BAPIRETURN1,

    LS_ERRORS           TYPE  BAPIVBRKERRORS,

    LS_CONDITIONDATAIN  TYPE  BAPIKOMV,

    LS_SUCCESS          TYPE BAPIVBRKSUCCESS.

 

  DATA: IT_SEG_INFO TYPE STANDARD TABLE OF ZCRM0200_INFO,

        LW_SEG_INFO TYPE ZCRM0200_INFO.

 

  DATA: IT_VBELN_INFO TYPE STANDARD TABLE OF ZCRM0200_VBELN_INFO,

        LW_VBELN_INFO TYPE ZCRM0200_VBELN_INFO.

 

  DATA: LV_ZCK5 TYPE ZCRM0200_INFO-ZYBH, "金额比率

 

        LV_CURR TYPE KWAEH.     "货币

 

  DATA:   LV_BUKRS1 TYPE T001K-BUKRS, "工厂对应公司

          LV_WERKS  TYPE LIPS-WERKS, "工厂

          LV_BUKRS  TYPE BKPF-BUKRS.  "发票号对应公司

 

 

  DATA:LV_VKORG TYPE VBAK-VKORG.  "销售组织

  DATA:WA_VBAK TYPE VBAK.

 

  DATA:LT_MESSTAB LIKE BDCMSGCOLL OCCURS 0 WITH HEADER LINE.

  CLEAR: LV_ZCK5.

*********获取段数据**************

 

  LOOP AT IDOC_CONTRL.

    IF IDOC_CONTRL-MESTYP NE 'ZCRM0200_MSG'.

      RAISE WRONG_FUNCTION_CALLED.

    ENDIF.

 

    LOOP AT IDOC_DATA WHERE DOCNUM EQ IDOC_CONTRL-DOCNUM.

      CASE IDOC_DATA-SEGNAM.

        WHEN 'ZCRM0200_INFO'.

 

          LW_SEG_INFO = IDOC_DATA-SDATA.

 

          APPEND LW_SEG_INFO TO IT_SEG_INFO.

          CLEAR:LW_SEG_INFO.

        WHEN 'ZCRM0200_VBELN_INFO'.

 

          LW_VBELN_INFO = IDOC_DATA-SDATA.

 

          APPEND LW_VBELN_INFO TO IT_VBELN_INFO.

          CLEAR:LW_VBELN_INFO.

      ENDCASE.

    ENDLOOP.

  ENDLOOP.

 

  DATA:

        WA_LIKP   TYPE LIKP.

 

  DATA: IT_LIPS TYPE STANDARD TABLE OF LIPS,

        WA_LIPS TYPE LIPS.

 

  DATA: KUNNR_SD TYPE VBPA-KUNNR, "售达方      PARVW  合作伙伴功能  SP

        KUNNR_FS TYPE VBPA-KUNNR, "送达方      SH

        KUNNR_FK TYPE KUNNR,      "付款方      PY

        KUNNR_SP TYPE KUNNR.      "收取发票方  PI

 

  DATA: VBELN_VF TYPE VBELN_VF,   "开票凭证

        STATUS   TYPE CHAR1,

        MESSAGE  TYPE CHAR64.

 

  DATA:IM_VGBEL TYPE VBELN_VL.  "交货单

 

  DATA: LV_SUM_LFIMG TYPE LIPS-LFIMG.

  DATA: LV_KBETR TYPE KBETR.

  DATA: LV_LEN TYPE I.

  DATA: LV_NUM TYPE I.

 

  DATA: LV_ZYFY TYPE KBETR,

        LV_ZYBH TYPE KBETR,

        LV_ZYFB TYPE KBETR.

 

  DATA:TMP_FLAG TYPE C.

 

*创建发票

  LOOP AT IT_SEG_INFO INTO LW_SEG_INFO.

 

    CALL FUNCTION 'CONVERSION_EXIT_ALPHA_INPUT'

      EXPORTING

        INPUT  = LW_SEG_INFO-VBELN

      IMPORTING

        OUTPUT = LW_SEG_INFO-VBELN.

 

    IM_VGBEL = LW_SEG_INFO-VBELN.

 

    SELECT  * INTO CORRESPONDING FIELDS OF TABLE IT_LIPS FROM LIPS

        WHERE VBELN = LW_SEG_INFO-VBELN.

 

    "获取销售订单

    READ TABLE IT_LIPS INTO WA_LIPS INDEX 1.

    SELECT SINGLE * FROM VBAK INTO CORRESPONDING FIELDS OF WA_VBAK WHERE VBELN = WA_LIPS-VGBEL.

 

    DATA: LV_COUNT TYPE I.

    CLEAR:LV_COUNT,LV_CURR,LV_BUKRS1,LV_BUKRS.

    LV_COUNT = 0.

    SORT  IT_LIPS BY  MATNR  POSNR.

 

 

    SELECT SINGLE FKDAT VBTYP INTO CORRESPONDING FIELDS OF WA_LIKP FROM LIKP WHERE VBELN = LW_SEG_INFO-VBELN.

 

 

 

    CLEAR: LV_SUM_LFIMG,LV_LEN.

    LOOP AT IT_LIPS INTO WA_LIPS .

      LV_LEN = LV_LEN + 1.

      LV_SUM_LFIMG =  LV_SUM_LFIMG + WA_LIPS-LFIMG.

      CLEAR:WA_LIPS.

    ENDLOOP.

 

    LOOP AT IT_LIPS INTO WA_LIPS .

      LV_COUNT = LV_COUNT + 1.

 

      SELECT SINGLE  BUKRS INTO LV_BUKRS1 FROM T001K WHERE BWKEY = WA_LIPS-WERKS.

 

 

 

 

      SELECT SINGLE * INTO WA_VBAK  FROM VBAK WHERE VBELN = WA_LIPS-VGBEL.

      "凭证货币

      LV_CURR = WA_VBAK-WAERK.

      "销售组织

      LV_VKORG = WA_VBAK-VKORG.

 

 

      "尾差

      LOOP AT IT_VBELN_INFO INTO LW_VBELN_INFO WHERE POSNR = WA_LIPS-POSNR.

 

 

        IF  LW_VBELN_INFO-ZDIC IS NOT INITIAL.

          LS_CONDITIONDATAIN-DATA_INDEX = LV_COUNT.

          "LS_CONDITIONDATAIN-DATA_INDEX =  '2'.

          LS_CONDITIONDATAIN-COND_TYPE  = 'ZDIC'.

          LS_CONDITIONDATAIN-COND_VALUE = LW_VBELN_INFO-ZDIC.

          LS_CONDITIONDATAIN-COND_CURR = LV_CURR.

 

          APPEND LS_CONDITIONDATAIN TO LT_CONDITIONDATAIN1.

          APPEND LS_CONDITIONDATAIN TO LT_CONDITIONDATAIN.

          CLEAR:LS_CONDITIONDATAIN.

        ENDIF.

        CLEAR:LW_VBELN_INFO.

      ENDLOOP.

 

 

      "交货单 + 行项目

      IF LW_SEG_INFO-ZBILLTY IS NOT INITIAL.

        LS_BILLING-ZBILLTY  = LW_SEG_INFO-ZBILLTY.  "金税开票类型

      ENDIF.

 

      LS_BILLING-REF_DOC  = LW_SEG_INFO-VBELN.

      LS_BILLING-REF_ITEM = WA_LIPS-POSNR."参考项目的项目号

 

      IF LW_SEG_INFO-FKDAT IS INITIAL.

        LS_BILLING-BILL_DATE = WA_LIKP-FKDAT.

      ELSE.

        LS_BILLING-BILL_DATE  = LW_SEG_INFO-FKDAT.

      ENDIF.

      LS_BILLING-REF_DOC_CA = WA_LIKP-VBTYP.    "以交货单

 

      APPEND LS_BILLING TO LT_BILLING.

      CLEAR :LS_BILLING.

      CLEAR:WA_LIPS.

    ENDLOOP.

 

 

 

 

    "销售组织5000/5010  运保费等放在公司间开票,对外开票不需要

    IF LV_VKORG = '5000' OR LV_VKORG = '5010'..

      CALL FUNCTION 'BAPI_BILLINGDOC_CREATEMULTIPLE'

        TABLES

          BILLINGDATAIN   = LT_BILLING

          CONDITIONDATAIN = LT_CONDITIONDATAIN1

          ERRORS          = LT_ERRORS

          RETURN          = LT_RETURN1

          SUCCESS         = LT_SUCCESS.

      TMP_FLAG = 'S'.

    ELSE.

 

      "是否有尾差

      IF IT_VBELN_INFO[] IS NOT INITIAL.

 

        CALL FUNCTION 'BAPI_BILLINGDOC_CREATEMULTIPLE'

          TABLES

            BILLINGDATAIN   = LT_BILLING

            CONDITIONDATAIN = LT_CONDITIONDATAIN

            ERRORS          = LT_ERRORS

            RETURN          = LT_RETURN1

            SUCCESS         = LT_SUCCESS.

 

        TMP_FLAG = 'S'.

      ELSE.

        "普通开票

        IF LW_SEG_INFO-ZYBH = 0 AND  LW_SEG_INFO-ZYFB = 0 AND LW_SEG_INFO-ZYFY = 0.

          TMP_FLAG = 'S'.

          CALL FUNCTION 'BAPI_BILLINGDOC_CREATEMULTIPLE'

            TABLES

              BILLINGDATAIN = LT_BILLING

              ERRORS        = LT_ERRORS

              RETURN        = LT_RETURN1

              SUCCESS       = LT_SUCCESS.

 

        ELSE.

          TMP_FLAG = 'E'.

          CLEAR:GT_BDCDATA[].

          "运保费走BDC

          PERFORM FRM_BDC_DYNPRO  USING 'SAPMV60A'    '0102'.

          PERFORM FRM_BDC_FIELD   USING 'BDC_CURSOR'  'KOMFK-VBELN(01)'.

          PERFORM FRM_BDC_FIELD   USING 'BDC_OKCODE'  '/00'.

          PERFORM FRM_BDC_FIELD   USING 'KOMFK-VBELN(01)'  LW_SEG_INFO-VBELN. "交货单

 

 

          PERFORM FRM_BDC_DYNPRO  USING 'SAPMV60A'    '0104'..

          PERFORM FRM_BDC_FIELD   USING 'BDC_CURSOR'  'VBRK-FKART'.

          PERFORM FRM_BDC_FIELD   USING 'BDC_OKCODE'  '=KFDE'.

 

 

 

          PERFORM FRM_BDC_DYNPRO  USING 'SAPMV60A'    '6001'..

          PERFORM FRM_BDC_FIELD   USING 'BDC_OKCODE'  '=KFKO'.

          PERFORM FRM_BDC_FIELD   USING 'BDC_CURSOR'  'VBRK-FKDAT'.

 

          IF LW_SEG_INFO-FKDAT IS INITIAL.

            PERFORM FRM_BDC_FIELD   USING 'VBRK-FKDAT' WA_LIKP-FKDAT.

          ELSE.

            PERFORM FRM_BDC_FIELD   USING 'VBRK-FKDAT' LW_SEG_INFO-FKDAT.

          ENDIF.

 

 

 

          IF  LW_SEG_INFO-ZYBH IS NOT INITIAL.

            PERFORM FRM_BDC_DYNPRO  USING 'SAPMV60A'    '6001'.

            PERFORM FRM_BDC_FIELD   USING 'BDC_OKCODE'  '=V69A_KOAN'.

            PERFORM FRM_BDC_FIELD   USING 'BDC_CURSOR'  'KOMV-KSCHL(01)'.

 

 

            PERFORM FRM_BDC_DYNPRO  USING 'SAPMV60A'    '6001'.

            PERFORM FRM_BDC_FIELD   USING 'BDC_OKCODE'  '/00'.

            PERFORM FRM_BDC_FIELD   USING 'BDC_CURSOR'  'KOMV-KBETR(02)'.

            PERFORM FRM_BDC_FIELD   USING 'KOMV-KSCHL(02)'  'ZYBH'.

            PERFORM FRM_BDC_FIELD   USING 'KOMV-KBETR(02)'  LW_SEG_INFO-ZYBH.

          ENDIF.

 

 

          IF  LW_SEG_INFO-ZYFB IS NOT INITIAL.

            PERFORM FRM_BDC_DYNPRO  USING 'SAPMV60A'    '6001'.

            PERFORM FRM_BDC_FIELD   USING 'BDC_OKCODE'  '=V69A_KOAN'.

            PERFORM FRM_BDC_FIELD   USING 'BDC_CURSOR'  'KOMV-KSCHL(01)'.

 

            PERFORM FRM_BDC_DYNPRO  USING 'SAPMV60A'    '6001'.

            PERFORM FRM_BDC_FIELD   USING 'BDC_OKCODE'  '/00'.

            PERFORM FRM_BDC_FIELD   USING 'BDC_CURSOR'  'KOMV-KBETR(02)'.

            PERFORM FRM_BDC_FIELD   USING 'KOMV-KSCHL(02)'  'ZYFB'.

            PERFORM FRM_BDC_FIELD   USING 'KOMV-KBETR(02)' LW_SEG_INFO-ZYFB.

            LV_ZCK5 = LW_SEG_INFO-ZYFB.

          ENDIF.

 

          IF  LW_SEG_INFO-ZYFY IS NOT INITIAL.

 

 

            PERFORM FRM_BDC_DYNPRO  USING 'SAPMV60A'    '6001'.

            PERFORM FRM_BDC_FIELD   USING 'BDC_OKCODE'  '=V69A_KOAN'.

            PERFORM FRM_BDC_FIELD   USING 'BDC_CURSOR'  'KOMV-KSCHL(03)'.

 

            PERFORM FRM_BDC_DYNPRO  USING 'SAPMV60A'    '6001'.

            PERFORM FRM_BDC_FIELD   USING 'BDC_OKCODE'  '/00'.

            PERFORM FRM_BDC_FIELD   USING 'BDC_CURSOR'  'KOMV-KBETR(02)'.

            PERFORM FRM_BDC_FIELD   USING 'KOMV-KSCHL(02)'  'ZYFY'.

            PERFORM FRM_BDC_FIELD   USING 'KOMV-KBETR(02)'  LW_SEG_INFO-ZYFY.

            LV_ZCK5 = LV_ZCK5 + LW_SEG_INFO-ZYFY.

          ENDIF.

 

          IF LW_SEG_INFO-ZCK5 IS NOT INITIAL.

 

            PERFORM FRM_BDC_DYNPRO  USING 'SAPMV60A'    '6001'.

            PERFORM FRM_BDC_FIELD   USING 'BDC_OKCODE'  '=V69A_KOAN'.

            PERFORM FRM_BDC_FIELD   USING 'BDC_CURSOR'  'KOMV-KSCHL(03)'.

 

            PERFORM FRM_BDC_DYNPRO  USING 'SAPMV60A'    '6001'.

            PERFORM FRM_BDC_FIELD   USING 'BDC_OKCODE'  '/00'.

            PERFORM FRM_BDC_FIELD   USING 'BDC_CURSOR'  'KOMV-KBETR(02)'.

            PERFORM FRM_BDC_FIELD   USING 'KOMV-KSCHL(02)'  'ZCK5'.

            PERFORM FRM_BDC_FIELD   USING 'KOMV-KBETR(02)'  LW_SEG_INFO-ZCK5.

 

          ENDIF.

 

 

 

          PERFORM FRM_BDC_DYNPRO  USING 'SAPMV60A'    '6001'.

          PERFORM FRM_BDC_FIELD   USING 'BDC_OKCODE'  '=KFCU'.

          PERFORM FRM_BDC_FIELD   USING 'BDC_CURSOR'  'KOMV-KSCHL(01)'.

 

          PERFORM FRM_BDC_DYNPRO  USING 'SAPMV60A'    '6001'.

 

          PERFORM FRM_BDC_FIELD   USING 'BDC_OKCODE'  '=SICH'.

          PERFORM FRM_BDC_FIELD   USING 'BDC_CURSOR'  'VBRK-ZBILLTY'.

          PERFORM FRM_BDC_FIELD   USING 'VBRK-ZBILLTY' LW_SEG_INFO-ZBILLTY.

 

 

          CALL TRANSACTION 'VF01' USING GT_BDCDATA

                        MODE  'N' "N不显示  A调试

                        UPDATE 'S'

                   MESSAGES INTO LT_MESSTAB.

          .

 

          READ TABLE LT_MESSTAB WITH KEY MSGTYP = 'E'.

          IF SY-SUBRC = 0.

            STATUS = 'E'.

            LOOP AT LT_MESSTAB WHERE MSGTYP = 'E'.

              MESSAGE ID     LT_MESSTAB-MSGID

                      TYPE   LT_MESSTAB-MSGTYP

                      NUMBER LT_MESSTAB-MSGNR

                      INTO MESSAGE

                      WITH LT_MESSTAB-MSGV1

                           LT_MESSTAB-MSGV2

                           LT_MESSTAB-MSGV3

                           LT_MESSTAB-MSGV4.

            ENDLOOP.

 

            CLEAR IDOC_STATUS.

            IDOC_STATUS-STATUS = '51'.

            IDOC_STATUS-MSGTY  = 'E'.

            IDOC_STATUS-MSGID  = ''.

            IDOC_STATUS-MSGNO  = ''.

            IDOC_STATUS-DOCNUM = IDOC_CONTRL-DOCNUM.

            IDOC_STATUS-MSGV1 = MESSAGE.

            APPEND IDOC_STATUS.

 

          ELSE.

            COMMIT WORK AND WAIT.

            READ TABLE LT_MESSTAB WITH KEY MSGTYP = 'S'  MSGNR = '311'.

 

            VBELN_VF = LT_MESSTAB-MSGV1.."发票凭证, BDC返回的发票凭证没有前导0

            STATUS = 'S'.

            MESSAGE = '交货单开票成功'.

 

            IDOC_STATUS-STATUS = '53'.

            IDOC_STATUS-MSGTY  = 'S' .

            IDOC_STATUS-DOCNUM = IDOC_CONTRL-DOCNUM.

            IDOC_STATUS-MSGV1 = '交货单开票成功'.

            APPEND IDOC_STATUS.

 

 

          ENDIF.

        ENDIF.  "运保费bdc

 

      ENDIF.

 

    ENDIF.

 

 

    IF TMP_FLAG = 'S'.

 

 

      IF LT_SUCCESS[] IS NOT INITIAL.

        READ TABLE LT_SUCCESS INTO LS_SUCCESS INDEX 1.

 

        IF SY-SUBRC EQ 0.

          VBELN_VF =  LS_SUCCESS-BILL_DOC."发票凭证

          STATUS = 'S'.

          MESSAGE = '交货单开票成功'.

          CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'

            EXPORTING

              WAIT = 'X'.

          COMMIT WORK AND WAIT.

 

          CLEAR IDOC_STATUS.

 

          IDOC_STATUS-STATUS = '53'.

          IDOC_STATUS-MSGTY  = 'S' .

          IDOC_STATUS-DOCNUM = IDOC_CONTRL-DOCNUM.

          IDOC_STATUS-MSGV1 = '交货单开票成功'.

          APPEND IDOC_STATUS.

 

 

 

        ENDIF.

      ELSE.

 

        CALL FUNCTION 'BAPI_TRANSACTION_ROLLBACK'.

 

        STATUS = 'E'.

 

        LOOP AT LT_RETURN1 INTO LS_RETURN1 WHERE TYPE = 'E' .

 

          MESSAGE = LS_RETURN1-MESSAGE.

 

        ENDLOOP.

 

        LOOP AT LT_ERRORS INTO LS_ERRORS WHERE TYPE = 'E' OR TYPE = 'A'.

 

          CONCATENATE MESSAGE LS_ERRORS-MESSAGE INTO MESSAGE.

          CLEAR LS_ERRORS.

        ENDLOOP.

 

        CLEAR IDOC_STATUS.

        IDOC_STATUS-STATUS = '51'.

        IDOC_STATUS-MSGTY  = 'E'.

        IDOC_STATUS-MSGID  = ''.

        IDOC_STATUS-MSGNO  = ''.

        IDOC_STATUS-DOCNUM = IDOC_CONTRL-DOCNUM.

        IDOC_STATUS-MSGV1 = MESSAGE.

        APPEND IDOC_STATUS.

 

      ENDIF.

 

 

    ENDIF.

 

 

    CALL FUNCTION 'CONVERSION_EXIT_ALPHA_INPUT'

      EXPORTING

        INPUT  = VBELN_VF

      IMPORTING

        OUTPUT = VBELN_VF.

 

    CALL FUNCTION 'ZCRM0201_OUTPUT_IDOC' STARTING NEW TASK 'ZCRM0201_OUTPUT_IDOC'

      EXPORTING

        IM_VBELN = VBELN_VF

        MESSAGE  = MESSAGE

        ETYPE    = STATUS

        IM_VGBEL = IM_VGBEL

* IMPORTING

*       O_DOCNUM =

      .

    IF STATUS = 'S'.

 

      SELECT SINGLE BUKRS INTO LV_BUKRS FROM BKPF WHERE BELNR = VBELN_VF.

 

      IF LV_BUKRS NE LV_BUKRS1.

 

 

        CLEAR:LT_SUCCESS[],LT_RETURN1,LT_ERRORS[], VBELN_VF, MESSAGE,

                 STATUS.

 

        IF LV_VKORG = '5000' OR LV_VKORG = '5010'.

 

          "5000尾差放对外,运保费公司间

*          CALL FUNCTION 'BAPI_BILLINGDOC_CREATEMULTIPLE'

*            TABLES

*              BILLINGDATAIN   = LT_BILLING

*              CONDITIONDATAIN = LT_CONDITIONDATAIN

*              ERRORS          = LT_ERRORS

*              RETURN          = LT_RETURN1

*              SUCCESS         = LT_SUCCESS.

          "普通开票

          IF LW_SEG_INFO-ZYBH = 0 AND  LW_SEG_INFO-ZYFB = 0 AND LW_SEG_INFO-ZYFY = 0.

            TMP_FLAG = 'S'.

            CALL FUNCTION 'BAPI_BILLINGDOC_CREATEMULTIPLE'

              TABLES

                BILLINGDATAIN = LT_BILLING

                ERRORS        = LT_ERRORS

                RETURN        = LT_RETURN1

                SUCCESS       = LT_SUCCESS.

 

          ELSE.

            TMP_FLAG = 'E'.

            CLEAR:GT_BDCDATA[].

            "运保费走BDC

            PERFORM FRM_BDC_DYNPRO  USING 'SAPMV60A'    '0102'.

            PERFORM FRM_BDC_FIELD   USING 'BDC_CURSOR'  'KOMFK-VBELN(01)'.

            PERFORM FRM_BDC_FIELD   USING 'BDC_OKCODE'  '/00'.

            PERFORM FRM_BDC_FIELD   USING 'KOMFK-VBELN(01)'  LW_SEG_INFO-VBELN. "交货单

 

 

            PERFORM FRM_BDC_DYNPRO  USING 'SAPMV60A'    '0104'..

            PERFORM FRM_BDC_FIELD   USING 'BDC_CURSOR'  'VBRK-FKART'.

            PERFORM FRM_BDC_FIELD   USING 'BDC_OKCODE'  '=KFDE'.

 

 

 

            PERFORM FRM_BDC_DYNPRO  USING 'SAPMV60A'    '6001'..

            PERFORM FRM_BDC_FIELD   USING 'BDC_OKCODE'  '=KFKO'.

            PERFORM FRM_BDC_FIELD   USING 'BDC_CURSOR'  'VBRK-FKDAT'.

 

            IF LW_SEG_INFO-FKDAT IS INITIAL.

              PERFORM FRM_BDC_FIELD   USING 'VBRK-FKDAT' WA_LIKP-FKDAT.

            ELSE.

              PERFORM FRM_BDC_FIELD   USING 'VBRK-FKDAT' LW_SEG_INFO-FKDAT.

            ENDIF.

 

 

 

            IF  LW_SEG_INFO-ZYBH IS NOT INITIAL.

              PERFORM FRM_BDC_DYNPRO  USING 'SAPMV60A'    '6001'.

              PERFORM FRM_BDC_FIELD   USING 'BDC_OKCODE'  '=V69A_KOAN'.

              PERFORM FRM_BDC_FIELD   USING 'BDC_CURSOR'  'KOMV-KSCHL(01)'.

 

 

              PERFORM FRM_BDC_DYNPRO  USING 'SAPMV60A'    '6001'.

              PERFORM FRM_BDC_FIELD   USING 'BDC_OKCODE'  '/00'.

              PERFORM FRM_BDC_FIELD   USING 'BDC_CURSOR'  'KOMV-KBETR(02)'.

              PERFORM FRM_BDC_FIELD   USING 'KOMV-KSCHL(02)'  'ZYJH'.

              PERFORM FRM_BDC_FIELD   USING 'KOMV-KBETR(02)'  LW_SEG_INFO-ZYBH.

            ENDIF.

 

 

            IF  LW_SEG_INFO-ZYFB IS NOT INITIAL.

              PERFORM FRM_BDC_DYNPRO  USING 'SAPMV60A'    '6001'.

              PERFORM FRM_BDC_FIELD   USING 'BDC_OKCODE'  '=V69A_KOAN'.

              PERFORM FRM_BDC_FIELD   USING 'BDC_CURSOR'  'KOMV-KSCHL(01)'.

 

              PERFORM FRM_BDC_DYNPRO  USING 'SAPMV60A'    '6001'.

              PERFORM FRM_BDC_FIELD   USING 'BDC_OKCODE'  '/00'.

              PERFORM FRM_BDC_FIELD   USING 'BDC_CURSOR'  'KOMV-KBETR(02)'.

              PERFORM FRM_BDC_FIELD   USING 'KOMV-KSCHL(02)'  'ZYFB'.

              PERFORM FRM_BDC_FIELD   USING 'KOMV-KBETR(02)' LW_SEG_INFO-ZYFB.

              LV_ZCK5 = LW_SEG_INFO-ZYFB.

            ENDIF.

 

            IF  LW_SEG_INFO-ZYFY IS NOT INITIAL.

 

 

              PERFORM FRM_BDC_DYNPRO  USING 'SAPMV60A'    '6001'.

              PERFORM FRM_BDC_FIELD   USING 'BDC_OKCODE'  '=V69A_KOAN'.

              PERFORM FRM_BDC_FIELD   USING 'BDC_CURSOR'  'KOMV-KSCHL(03)'.

 

              PERFORM FRM_BDC_DYNPRO  USING 'SAPMV60A'    '6001'.

              PERFORM FRM_BDC_FIELD   USING 'BDC_OKCODE'  '/00'.

              PERFORM FRM_BDC_FIELD   USING 'BDC_CURSOR'  'KOMV-KBETR(02)'.

              PERFORM FRM_BDC_FIELD   USING 'KOMV-KSCHL(02)'  'ZYFY'.

              PERFORM FRM_BDC_FIELD   USING 'KOMV-KBETR(02)'  LW_SEG_INFO-ZYFY.

              LV_ZCK5 = LV_ZCK5 + LW_SEG_INFO-ZYFY.

            ENDIF.

 

            IF LW_SEG_INFO-ZCK5 IS NOT INITIAL.

 

              PERFORM FRM_BDC_DYNPRO  USING 'SAPMV60A'    '6001'.

              PERFORM FRM_BDC_FIELD   USING 'BDC_OKCODE'  '=V69A_KOAN'.

              PERFORM FRM_BDC_FIELD   USING 'BDC_CURSOR'  'KOMV-KSCHL(03)'.

 

              PERFORM FRM_BDC_DYNPRO  USING 'SAPMV60A'    '6001'.

              PERFORM FRM_BDC_FIELD   USING 'BDC_OKCODE'  '/00'.

              PERFORM FRM_BDC_FIELD   USING 'BDC_CURSOR'  'KOMV-KBETR(02)'.

              PERFORM FRM_BDC_FIELD   USING 'KOMV-KSCHL(02)'  'ZCK5'.

              PERFORM FRM_BDC_FIELD   USING 'KOMV-KBETR(02)'  LW_SEG_INFO-ZCK5.

 

            ENDIF.

 

 

            PERFORM FRM_BDC_DYNPRO  USING 'SAPMV60A'    '6001'.

            PERFORM FRM_BDC_FIELD   USING 'BDC_OKCODE'  '=KFCU'.

            PERFORM FRM_BDC_FIELD   USING 'BDC_CURSOR'  'KOMV-KSCHL(01)'.

 

            PERFORM FRM_BDC_DYNPRO  USING 'SAPMV60A'    '6001'.

 

            PERFORM FRM_BDC_FIELD   USING 'BDC_OKCODE'  '=SICH'.

            PERFORM FRM_BDC_FIELD   USING 'BDC_CURSOR'  'VBRK-ZBILLTY'.

            PERFORM FRM_BDC_FIELD   USING 'VBRK-ZBILLTY' LW_SEG_INFO-ZBILLTY.

 

 

            CALL TRANSACTION 'VF01' USING GT_BDCDATA

                          MODE  'N' "N不显示  A调试

                          UPDATE 'S'

                     MESSAGES INTO LT_MESSTAB.

            .

 

            READ TABLE LT_MESSTAB WITH KEY MSGTYP = 'E'.

            IF SY-SUBRC = 0.

              STATUS = 'E'.

              LOOP AT LT_MESSTAB WHERE MSGTYP = 'E'.

                MESSAGE ID     LT_MESSTAB-MSGID

                        TYPE   LT_MESSTAB-MSGTYP

                        NUMBER LT_MESSTAB-MSGNR

                        INTO MESSAGE

                        WITH LT_MESSTAB-MSGV1

                             LT_MESSTAB-MSGV2

                             LT_MESSTAB-MSGV3

                             LT_MESSTAB-MSGV4.

              ENDLOOP.

 

            ELSE.

              COMMIT WORK AND WAIT.

              READ TABLE LT_MESSTAB WITH KEY MSGTYP = 'S'  MSGNR = '311'.

 

              VBELN_VF = LT_MESSTAB-MSGV1.."发票凭证

              STATUS = 'S'.

              MESSAGE = '交货单开票成功'.

            ENDIF.

 

 

 

 

            TMP_FLAG = 'E'.

          ENDIF.

        ELSE.

          TMP_FLAG = 'S'.

          CALL FUNCTION 'BAPI_BILLINGDOC_CREATEMULTIPLE'

            TABLES

              BILLINGDATAIN = LT_BILLING

              ERRORS        = LT_ERRORS

              RETURN        = LT_RETURN1

              SUCCESS       = LT_SUCCESS.

        ENDIF.

 

 

        IF TMP_FLAG = 'S'.

 

 

          IF LT_SUCCESS[] IS NOT INITIAL.

            READ TABLE LT_SUCCESS INTO LS_SUCCESS INDEX 1.

 

            IF SY-SUBRC EQ 0.

              VBELN_VF =  LS_SUCCESS-BILL_DOC."发票凭证

              STATUS = 'S'.

              MESSAGE = '交货单开票成功'.

 

              CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'

                EXPORTING

                  WAIT = 'X'.

              COMMIT WORK AND WAIT.

 

 

            ENDIF.

          ELSE.

 

            CALL FUNCTION 'BAPI_TRANSACTION_ROLLBACK'.

 

            STATUS = 'E'.

 

            LOOP AT LT_RETURN1 INTO LS_RETURN1 WHERE TYPE = 'E' .

 

              MESSAGE = LS_RETURN1-MESSAGE.

 

            ENDLOOP.

 

            LOOP AT LT_ERRORS INTO LS_ERRORS WHERE TYPE = 'E' OR TYPE = 'A'.

 

              CONCATENATE MESSAGE LS_ERRORS-MESSAGE INTO MESSAGE.

              CLEAR LS_ERRORS.

            ENDLOOP.

 

          ENDIF.

        ENDIF.

 

 

 

  "接口调用返回”

        CALL FUNCTION 'ZCRM0201_OUTPUT_IDOC' STARTING NEW TASK 'ZCRM0201_OUTPUT_IDOC'

          EXPORTING

            IM_VBELN = VBELN_VF

            MESSAGE  = MESSAGE

            ETYPE    = STATUS

            IM_VGBEL = IM_VGBEL

            INV_TYPE = 'A'.

      ENDIF.

 

    ENDIF.

    CLEAR:LW_SEG_INFO.

  ENDLOOP.

ENDFUNCTION.

FUNCTION ZCRM0200_IDOC_PROCESS .
*"----------------------------------------------------------------------
*"*"本地接口:
*"  IMPORTING
*"     VALUE(INPUT_METHOD) LIKE  BDWFAP_PAR-INPUTMETHD
*"     VALUE(MASS_PROCESSING) LIKE  BDWFAP_PAR-MASS_PROC
*"  EXPORTING
*"     VALUE(WORKFLOW_RESULT) LIKE  BDWFAP_PAR-RESULT
*"     VALUE(APPLICATION_VARIABLE) LIKE  BDWFAP_PAR-APPL_VAR
*"     VALUE(IN_UPDATE_TASK) LIKE  BDWFAP_PAR-UPDATETASK
*"     VALUE(CALL_TRANSACTION_DONE) LIKE  BDWFAP_PAR-CALLTRANS
*"  TABLES
*"      IDOC_CONTRL STRUCTURE  EDIDC
*"      IDOC_DATA STRUCTURE  EDIDD
*"      IDOC_STATUS STRUCTURE  BDIDOCSTAT
*"      RETURN_VARIABLES STRUCTURE  BDWFRETVAR
*"      SERIALIZATION_INFO STRUCTURE  BDI_SER
*"      EDI_TEXT STRUCTURE  EDIORDTXT1 OPTIONAL
*"      EDI_TEXT_LINES STRUCTURE  EDIORDTXT2 OPTIONAL
*"----------------------------------------------------------------------
  "bapi定义
  DATA:
    LT_BILLING          TYPE STANDARD TABLE OF BAPIVBRK,
    LT_RETURN1          TYPE STANDARD TABLE OF BAPIRETURN1,
    LT_SUCCESS          TYPE STANDARD TABLE OF BAPIVBRKSUCCESS,
    LT_ERRORS           TYPE STANDARD TABLE OF  BAPIVBRKERRORS,
    LT_CONDITIONDATAIN  TYPE STANDARD TABLE OF BAPIKOMV,
    LT_CONDITIONDATAIN1 TYPE STANDARD TABLE OF BAPIKOMV,
    LS_BILLING          TYPE BAPIVBRK,
    LS_RETURN1          TYPE BAPIRETURN1,
    LS_ERRORS           TYPE  BAPIVBRKERRORS,
    LS_CONDITIONDATAIN  TYPE  BAPIKOMV,
    LS_SUCCESS          TYPE BAPIVBRKSUCCESS.

  DATA: IT_SEG_INFO TYPE STANDARD TABLE OF ZCRM0200_INFO,
        LW_SEG_INFO TYPE ZCRM0200_INFO.

  DATA: IT_VBELN_INFO TYPE STANDARD TABLE OF ZCRM0200_VBELN_INFO,
        LW_VBELN_INFO TYPE ZCRM0200_VBELN_INFO.

  DATA: LV_ZCK5 TYPE ZCRM0200_INFO-ZYBH, "金额比率

        LV_CURR TYPE KWAEH.     "货币

  DATA:   LV_BUKRS1 TYPE T001K-BUKRS, "工厂对应公司
          LV_WERKS  TYPE LIPS-WERKS, "工厂
          LV_BUKRS  TYPE BKPF-BUKRS.  "发票号对应公司


  DATA:LV_VKORG TYPE VBAK-VKORG.  "销售组织
  DATA:WA_VBAK TYPE VBAK.

  DATA:LT_MESSTAB LIKE BDCMSGCOLL OCCURS 0 WITH HEADER LINE.
  CLEAR: LV_ZCK5.
*********获取段数据**************

  LOOP AT IDOC_CONTRL.
    IF IDOC_CONTRL-MESTYP NE 'ZCRM0200_MSG'.
      RAISE WRONG_FUNCTION_CALLED.
    ENDIF.

    LOOP AT IDOC_DATA WHERE DOCNUM EQ IDOC_CONTRL-DOCNUM.
      CASE IDOC_DATA-SEGNAM.
        WHEN 'ZCRM0200_INFO'.

          LW_SEG_INFO = IDOC_DATA-SDATA.

          APPEND LW_SEG_INFO TO IT_SEG_INFO.
          CLEAR:LW_SEG_INFO.
        WHEN 'ZCRM0200_VBELN_INFO'.

          LW_VBELN_INFO = IDOC_DATA-SDATA.

          APPEND LW_VBELN_INFO TO IT_VBELN_INFO.
          CLEAR:LW_VBELN_INFO.
      ENDCASE.
    ENDLOOP.
  ENDLOOP.

  DATA:
        WA_LIKP   TYPE LIKP.

  DATA: IT_LIPS TYPE STANDARD TABLE OF LIPS,
        WA_LIPS TYPE LIPS.

  DATA: KUNNR_SD TYPE VBPA-KUNNR, "售达方      PARVW  合作伙伴功能  SP
        KUNNR_FS TYPE VBPA-KUNNR, "送达方      SH
        KUNNR_FK TYPE KUNNR,      "付款方      PY
        KUNNR_SP TYPE KUNNR.      "收取发票方  PI

  DATA: VBELN_VF TYPE VBELN_VF,   "开票凭证
        STATUS   TYPE CHAR1,
        MESSAGE  TYPE CHAR64.

  DATA:IM_VGBEL TYPE VBELN_VL.  "交货单

  DATA: LV_SUM_LFIMG TYPE LIPS-LFIMG.
  DATA: LV_KBETR TYPE KBETR.
  DATA: LV_LEN TYPE I.
  DATA: LV_NUM TYPE I.

  DATA: LV_ZYFY TYPE KBETR,
        LV_ZYBH TYPE KBETR,
        LV_ZYFB TYPE KBETR.

  DATA:TMP_FLAG TYPE C.

*创建发票
  LOOP AT IT_SEG_INFO INTO LW_SEG_INFO.

    CALL FUNCTION 'CONVERSION_EXIT_ALPHA_INPUT'
      EXPORTING
        INPUT  = LW_SEG_INFO-VBELN
      IMPORTING
        OUTPUT = LW_SEG_INFO-VBELN.

    IM_VGBEL = LW_SEG_INFO-VBELN.

    SELECT  * INTO CORRESPONDING FIELDS OF TABLE IT_LIPS FROM LIPS
        WHERE VBELN = LW_SEG_INFO-VBELN.

    "获取销售订单
    READ TABLE IT_LIPS INTO WA_LIPS INDEX 1.
    SELECT SINGLE * FROM VBAK INTO CORRESPONDING FIELDS OF WA_VBAK WHERE VBELN = WA_LIPS-VGBEL.

* LS_BILLING-SALESORG = WA_VBAK-VKORG."销售组织     vbak
*LS_BILLING-DISTR_CHAN = WA_VBAK-VTWEG."分销渠道
*LS_BILLING-DIVISION = WA_VBAK-SPART."产品组
*LS_BILLING-DOC_TYPE = WA_VBAK-AUART."销售凭证类型     vbak-AUART 'ZOR'

*LS_BILLING-ORDBILLTYP = 'ZF2'."与订单有关的出具发票凭证的建议出具发票类型
*LS_BILLING-BILL_DATE = LW_SEG_INFO-FKDAT."出具发票索引和打印的出具发票日期

*LS_BILLING-SOLD_TO = '0000100521'."售达方


*LS_BILLING-PRICE_DATE = '20161008'."定价日期和汇率

*LS_BILLING-PLANT = '1020'."工厂
*LS_BILLING-BILL_TO = '0000100521'."收取发票方
*LS_BILLING-PAYER = '0000100521'."付款方
*LS_BILLING-SHIP_TO = '0000100521'."送达方
*LS_BILLING-REF_DOC = '0080009971'."参考单据的单据编号    交货单
*LS_BILLING-REF_ITEM = '000010'."参考项目的项目号
*
*LS_BILLING-DOC_NUMBER = '0080009971'."销售凭证
*LS_BILLING-ITM_NUMBER = '000010'."销售凭证项目
*LS_BILLING-MATERIAL = '000000000044007359'."物料号


    "LS_BILLING-PAYMENT_TERMS = 'D000'."付款条1
    "LS_BILLING-INCOTERMS1 = 'ZZZ'."国际贸易条款 (部分1)
    "LS_BILLING-INCOTERMS2 = '国内交货'."国际贸易条件(部分2)

*LS_BILLING-REF_DOC_CA = 'J'."销售凭证类型       vbak-VBTYP
*LS_BILLING-ITEM_CATEG = 'ZTAN'."销售凭证项目类别


    DATA: LV_COUNT TYPE I.
    CLEAR:LV_COUNT,LV_CURR,LV_BUKRS1,LV_BUKRS.
    LV_COUNT = 0.
    SORT  IT_LIPS BY  MATNR  POSNR.


    SELECT SINGLE FKDAT VBTYP INTO CORRESPONDING FIELDS OF WA_LIKP FROM LIKP WHERE VBELN = LW_SEG_INFO-VBELN.


    "只放交货单
*    IF LW_SEG_INFO-ZBILLTY IS NOT INITIAL.
*      LS_BILLING-ZBILLTY  = LW_SEG_INFO-ZBILLTY.  "金税开票类型
*    ENDIF.
*
*    LS_BILLING-REF_DOC  = LW_SEG_INFO-VBELN.
*    LS_BILLING-REF_ITEM = WA_LIPS-POSNR."参考项目的项目号
*
*    IF LW_SEG_INFO-FKDAT IS INITIAL.
*      LS_BILLING-BILL_DATE = WA_LIKP-FKDAT.
*    ELSE.
*      LS_BILLING-BILL_DATE  = LW_SEG_INFO-FKDAT.
*    ENDIF.
*    LS_BILLING-REF_DOC_CA = WA_LIKP-VBTYP.    "以交货单
*
*    APPEND LS_BILLING TO LT_BILLING.
*    CLEAR :LS_BILLING.



    CLEAR: LV_SUM_LFIMG,LV_LEN.
    LOOP AT IT_LIPS INTO WA_LIPS .
      LV_LEN = LV_LEN + 1.
      LV_SUM_LFIMG =  LV_SUM_LFIMG + WA_LIPS-LFIMG.
      CLEAR:WA_LIPS.
    ENDLOOP.

    LOOP AT IT_LIPS INTO WA_LIPS .
      LV_COUNT = LV_COUNT + 1.

      SELECT SINGLE  BUKRS INTO LV_BUKRS1 FROM T001K WHERE BWKEY = WA_LIPS-WERKS.




      SELECT SINGLE * INTO WA_VBAK  FROM VBAK WHERE VBELN = WA_LIPS-VGBEL.
      "凭证货币
      LV_CURR = WA_VBAK-WAERK.
      "销售组织
      LV_VKORG = WA_VBAK-VKORG.


      "尾差
      LOOP AT IT_VBELN_INFO INTO LW_VBELN_INFO WHERE POSNR = WA_LIPS-POSNR.


        IF  LW_VBELN_INFO-ZDIC IS NOT INITIAL.
          LS_CONDITIONDATAIN-DATA_INDEX = LV_COUNT.
          "LS_CONDITIONDATAIN-DATA_INDEX =  '2'.
          LS_CONDITIONDATAIN-COND_TYPE  = 'ZDIC'.
          LS_CONDITIONDATAIN-COND_VALUE = LW_VBELN_INFO-ZDIC.
          LS_CONDITIONDATAIN-COND_CURR = LV_CURR.

          APPEND LS_CONDITIONDATAIN TO LT_CONDITIONDATAIN1.
          APPEND LS_CONDITIONDATAIN TO LT_CONDITIONDATAIN.
          CLEAR:LS_CONDITIONDATAIN.
        ENDIF.
        CLEAR:LW_VBELN_INFO.
      ENDLOOP.


      "交货单 + 行项目
      IF LW_SEG_INFO-ZBILLTY IS NOT INITIAL.
        LS_BILLING-ZBILLTY  = LW_SEG_INFO-ZBILLTY.  "金税开票类型
      ENDIF.

      LS_BILLING-REF_DOC  = LW_SEG_INFO-VBELN.
      LS_BILLING-REF_ITEM = WA_LIPS-POSNR."参考项目的项目号

      IF LW_SEG_INFO-FKDAT IS INITIAL.
        LS_BILLING-BILL_DATE = WA_LIKP-FKDAT.
      ELSE.
        LS_BILLING-BILL_DATE  = LW_SEG_INFO-FKDAT.
      ENDIF.
      LS_BILLING-REF_DOC_CA = WA_LIKP-VBTYP.    "以交货单

      APPEND LS_BILLING TO LT_BILLING.
      CLEAR :LS_BILLING.
      CLEAR:WA_LIPS.
    ENDLOOP.




    "销售组织5000/5010  运保费等放在公司间开票,对外开票不需要
    IF LV_VKORG = '5000' OR LV_VKORG = '5010'..
      CALL FUNCTION 'BAPI_BILLINGDOC_CREATEMULTIPLE'
        TABLES
          BILLINGDATAIN   = LT_BILLING
          CONDITIONDATAIN = LT_CONDITIONDATAIN1
          ERRORS          = LT_ERRORS
          RETURN          = LT_RETURN1
          SUCCESS         = LT_SUCCESS.
      TMP_FLAG = 'S'.
    ELSE.

      "是否有尾差
      IF IT_VBELN_INFO[] IS NOT INITIAL.

        CALL FUNCTION 'BAPI_BILLINGDOC_CREATEMULTIPLE'
          TABLES
            BILLINGDATAIN   = LT_BILLING
            CONDITIONDATAIN = LT_CONDITIONDATAIN
            ERRORS          = LT_ERRORS
            RETURN          = LT_RETURN1
            SUCCESS         = LT_SUCCESS.

        TMP_FLAG = 'S'.
      ELSE.
        "普通开票
        IF LW_SEG_INFO-ZYBH = 0 AND  LW_SEG_INFO-ZYFB = 0 AND LW_SEG_INFO-ZYFY = 0.
          TMP_FLAG = 'S'.
          CALL FUNCTION 'BAPI_BILLINGDOC_CREATEMULTIPLE'
            TABLES
              BILLINGDATAIN = LT_BILLING
              ERRORS        = LT_ERRORS
              RETURN        = LT_RETURN1
              SUCCESS       = LT_SUCCESS.

        ELSE.
          TMP_FLAG = 'E'.
          CLEAR:GT_BDCDATA[].
          "运保费走BDC
          PERFORM FRM_BDC_DYNPRO  USING 'SAPMV60A'    '0102'.
          PERFORM FRM_BDC_FIELD   USING 'BDC_CURSOR'  'KOMFK-VBELN(01)'.
          PERFORM FRM_BDC_FIELD   USING 'BDC_OKCODE'  '/00'.
          PERFORM FRM_BDC_FIELD   USING 'KOMFK-VBELN(01)'  LW_SEG_INFO-VBELN. "交货单


          PERFORM FRM_BDC_DYNPRO  USING 'SAPMV60A'    '0104'..
          PERFORM FRM_BDC_FIELD   USING 'BDC_CURSOR'  'VBRK-FKART'.
          PERFORM FRM_BDC_FIELD   USING 'BDC_OKCODE'  '=KFDE'.



          PERFORM FRM_BDC_DYNPRO  USING 'SAPMV60A'    '6001'..
          PERFORM FRM_BDC_FIELD   USING 'BDC_OKCODE'  '=KFKO'.
          PERFORM FRM_BDC_FIELD   USING 'BDC_CURSOR'  'VBRK-FKDAT'.

          IF LW_SEG_INFO-FKDAT IS INITIAL.
            PERFORM FRM_BDC_FIELD   USING 'VBRK-FKDAT' WA_LIKP-FKDAT.
          ELSE.
            PERFORM FRM_BDC_FIELD   USING 'VBRK-FKDAT' LW_SEG_INFO-FKDAT.
          ENDIF.



          IF  LW_SEG_INFO-ZYBH IS NOT INITIAL.
            PERFORM FRM_BDC_DYNPRO  USING 'SAPMV60A'    '6001'.
            PERFORM FRM_BDC_FIELD   USING 'BDC_OKCODE'  '=V69A_KOAN'.
            PERFORM FRM_BDC_FIELD   USING 'BDC_CURSOR'  'KOMV-KSCHL(01)'.


            PERFORM FRM_BDC_DYNPRO  USING 'SAPMV60A'    '6001'.
            PERFORM FRM_BDC_FIELD   USING 'BDC_OKCODE'  '/00'.
            PERFORM FRM_BDC_FIELD   USING 'BDC_CURSOR'  'KOMV-KBETR(02)'.
            PERFORM FRM_BDC_FIELD   USING 'KOMV-KSCHL(02)'  'ZYBH'.
            PERFORM FRM_BDC_FIELD   USING 'KOMV-KBETR(02)'  LW_SEG_INFO-ZYBH.
          ENDIF.


          IF  LW_SEG_INFO-ZYFB IS NOT INITIAL.
            PERFORM FRM_BDC_DYNPRO  USING 'SAPMV60A'    '6001'.
            PERFORM FRM_BDC_FIELD   USING 'BDC_OKCODE'  '=V69A_KOAN'.
            PERFORM FRM_BDC_FIELD   USING 'BDC_CURSOR'  'KOMV-KSCHL(01)'.

            PERFORM FRM_BDC_DYNPRO  USING 'SAPMV60A'    '6001'.
            PERFORM FRM_BDC_FIELD   USING 'BDC_OKCODE'  '/00'.
            PERFORM FRM_BDC_FIELD   USING 'BDC_CURSOR'  'KOMV-KBETR(02)'.
            PERFORM FRM_BDC_FIELD   USING 'KOMV-KSCHL(02)'  'ZYFB'.
            PERFORM FRM_BDC_FIELD   USING 'KOMV-KBETR(02)' LW_SEG_INFO-ZYFB.
            LV_ZCK5 = LW_SEG_INFO-ZYFB.
          ENDIF.

          IF  LW_SEG_INFO-ZYFY IS NOT INITIAL.


            PERFORM FRM_BDC_DYNPRO  USING 'SAPMV60A'    '6001'.
            PERFORM FRM_BDC_FIELD   USING 'BDC_OKCODE'  '=V69A_KOAN'.
            PERFORM FRM_BDC_FIELD   USING 'BDC_CURSOR'  'KOMV-KSCHL(03)'.

            PERFORM FRM_BDC_DYNPRO  USING 'SAPMV60A'    '6001'.
            PERFORM FRM_BDC_FIELD   USING 'BDC_OKCODE'  '/00'.
            PERFORM FRM_BDC_FIELD   USING 'BDC_CURSOR'  'KOMV-KBETR(02)'.
            PERFORM FRM_BDC_FIELD   USING 'KOMV-KSCHL(02)'  'ZYFY'.
            PERFORM FRM_BDC_FIELD   USING 'KOMV-KBETR(02)'  LW_SEG_INFO-ZYFY.
            LV_ZCK5 = LV_ZCK5 + LW_SEG_INFO-ZYFY.
          ENDIF.

          IF LW_SEG_INFO-ZCK5 IS NOT INITIAL.

            PERFORM FRM_BDC_DYNPRO  USING 'SAPMV60A'    '6001'.
            PERFORM FRM_BDC_FIELD   USING 'BDC_OKCODE'  '=V69A_KOAN'.
            PERFORM FRM_BDC_FIELD   USING 'BDC_CURSOR'  'KOMV-KSCHL(03)'.

            PERFORM FRM_BDC_DYNPRO  USING 'SAPMV60A'    '6001'.
            PERFORM FRM_BDC_FIELD   USING 'BDC_OKCODE'  '/00'.
            PERFORM FRM_BDC_FIELD   USING 'BDC_CURSOR'  'KOMV-KBETR(02)'.
            PERFORM FRM_BDC_FIELD   USING 'KOMV-KSCHL(02)'  'ZCK5'.
            PERFORM FRM_BDC_FIELD   USING 'KOMV-KBETR(02)'  LW_SEG_INFO-ZCK5.

          ENDIF.



          PERFORM FRM_BDC_DYNPRO  USING 'SAPMV60A'    '6001'.
          PERFORM FRM_BDC_FIELD   USING 'BDC_OKCODE'  '=KFCU'.
          PERFORM FRM_BDC_FIELD   USING 'BDC_CURSOR'  'KOMV-KSCHL(01)'.

          PERFORM FRM_BDC_DYNPRO  USING 'SAPMV60A'    '6001'.

          PERFORM FRM_BDC_FIELD   USING 'BDC_OKCODE'  '=SICH'.
          PERFORM FRM_BDC_FIELD   USING 'BDC_CURSOR'  'VBRK-ZBILLTY'.
          PERFORM FRM_BDC_FIELD   USING 'VBRK-ZBILLTY' LW_SEG_INFO-ZBILLTY.


          CALL TRANSACTION 'VF01' USING GT_BDCDATA
                        MODE  'N' "N不显示  A调试
                        UPDATE 'S'
                   MESSAGES INTO LT_MESSTAB.
          .

          READ TABLE LT_MESSTAB WITH KEY MSGTYP = 'E'.
          IF SY-SUBRC = 0.
            STATUS = 'E'.
            LOOP AT LT_MESSTAB WHERE MSGTYP = 'E'.
              MESSAGE ID     LT_MESSTAB-MSGID
                      TYPE   LT_MESSTAB-MSGTYP
                      NUMBER LT_MESSTAB-MSGNR
                      INTO MESSAGE
                      WITH LT_MESSTAB-MSGV1
                           LT_MESSTAB-MSGV2
                           LT_MESSTAB-MSGV3
                           LT_MESSTAB-MSGV4.
            ENDLOOP.

            CLEAR IDOC_STATUS.
            IDOC_STATUS-STATUS = '51'.
            IDOC_STATUS-MSGTY  = 'E'.
            IDOC_STATUS-MSGID  = ''.
            IDOC_STATUS-MSGNO  = ''.
            IDOC_STATUS-DOCNUM = IDOC_CONTRL-DOCNUM.
            IDOC_STATUS-MSGV1 = MESSAGE.
            APPEND IDOC_STATUS.

          ELSE.
            COMMIT WORK AND WAIT.
            READ TABLE LT_MESSTAB WITH KEY MSGTYP = 'S'  MSGNR = '311'.

            VBELN_VF = LT_MESSTAB-MSGV1.."发票凭证
            STATUS = 'S'.
            MESSAGE = '交货单开票成功'.

            IDOC_STATUS-STATUS = '53'.
            IDOC_STATUS-MSGTY  = 'S' .
            IDOC_STATUS-DOCNUM = IDOC_CONTRL-DOCNUM.
            IDOC_STATUS-MSGV1 = '交货单开票成功'.
            APPEND IDOC_STATUS.


          ENDIF.
        ENDIF.  "运保费bdc

      ENDIF.

    ENDIF.


    IF TMP_FLAG = 'S'.


      IF LT_SUCCESS[] IS NOT INITIAL.
        READ TABLE LT_SUCCESS INTO LS_SUCCESS INDEX 1.

        IF SY-SUBRC EQ 0.
          VBELN_VF =  LS_SUCCESS-BILL_DOC."发票凭证
          STATUS = 'S'.
          MESSAGE = '交货单开票成功'.
          CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'
            EXPORTING
              WAIT = 'X'.
          COMMIT WORK AND WAIT.

          CLEAR IDOC_STATUS.

          IDOC_STATUS-STATUS = '53'.
          IDOC_STATUS-MSGTY  = 'S' .
          IDOC_STATUS-DOCNUM = IDOC_CONTRL-DOCNUM.
          IDOC_STATUS-MSGV1 = '交货单开票成功'.
          APPEND IDOC_STATUS.



        ENDIF.
      ELSE.

        CALL FUNCTION 'BAPI_TRANSACTION_ROLLBACK'.

        STATUS = 'E'.

        LOOP AT LT_RETURN1 INTO LS_RETURN1 WHERE TYPE = 'E' .

          MESSAGE = LS_RETURN1-MESSAGE.

        ENDLOOP.

        LOOP AT LT_ERRORS INTO LS_ERRORS WHERE TYPE = 'E' OR TYPE = 'A'.

          CONCATENATE MESSAGE LS_ERRORS-MESSAGE INTO MESSAGE.
          CLEAR LS_ERRORS.
        ENDLOOP.

        CLEAR IDOC_STATUS.
        IDOC_STATUS-STATUS = '51'.
        IDOC_STATUS-MSGTY  = 'E'.
        IDOC_STATUS-MSGID  = ''.
        IDOC_STATUS-MSGNO  = ''.
        IDOC_STATUS-DOCNUM = IDOC_CONTRL-DOCNUM.
        IDOC_STATUS-MSGV1 = MESSAGE.
        APPEND IDOC_STATUS.

      ENDIF.


    ENDIF.


    CALL FUNCTION 'CONVERSION_EXIT_ALPHA_INPUT'
      EXPORTING
        INPUT  = VBELN_VF
      IMPORTING
        OUTPUT = VBELN_VF.

    CALL FUNCTION 'ZCRM0201_OUTPUT_IDOC' STARTING NEW TASK 'ZCRM0201_OUTPUT_IDOC'
      EXPORTING
        IM_VBELN = VBELN_VF
        MESSAGE  = MESSAGE
        ETYPE    = STATUS
        IM_VGBEL = IM_VGBEL
* IMPORTING
*       O_DOCNUM =
      .
    IF STATUS = 'S'.

      SELECT SINGLE BUKRS INTO LV_BUKRS FROM BKPF WHERE BELNR = VBELN_VF.

      IF LV_BUKRS NE LV_BUKRS1.


        CLEAR:LT_SUCCESS[],LT_RETURN1,LT_ERRORS[], VBELN_VF, MESSAGE,
                 STATUS.

        IF LV_VKORG = '5000' OR LV_VKORG = '5010'.

          "5000尾差放对外,运保费公司间
*          CALL FUNCTION 'BAPI_BILLINGDOC_CREATEMULTIPLE'
*            TABLES
*              BILLINGDATAIN   = LT_BILLING
*              CONDITIONDATAIN = LT_CONDITIONDATAIN
*              ERRORS          = LT_ERRORS
*              RETURN          = LT_RETURN1
*              SUCCESS         = LT_SUCCESS.
          "普通开票
          IF LW_SEG_INFO-ZYBH = 0 AND  LW_SEG_INFO-ZYFB = 0 AND LW_SEG_INFO-ZYFY = 0.
            TMP_FLAG = 'S'.
            CALL FUNCTION 'BAPI_BILLINGDOC_CREATEMULTIPLE'
              TABLES
                BILLINGDATAIN = LT_BILLING
                ERRORS        = LT_ERRORS
                RETURN        = LT_RETURN1
                SUCCESS       = LT_SUCCESS.

          ELSE.
            TMP_FLAG = 'E'.
            CLEAR:GT_BDCDATA[].
            "运保费走BDC
            PERFORM FRM_BDC_DYNPRO  USING 'SAPMV60A'    '0102'.
            PERFORM FRM_BDC_FIELD   USING 'BDC_CURSOR'  'KOMFK-VBELN(01)'.
            PERFORM FRM_BDC_FIELD   USING 'BDC_OKCODE'  '/00'.
            PERFORM FRM_BDC_FIELD   USING 'KOMFK-VBELN(01)'  LW_SEG_INFO-VBELN. "交货单


            PERFORM FRM_BDC_DYNPRO  USING 'SAPMV60A'    '0104'..
            PERFORM FRM_BDC_FIELD   USING 'BDC_CURSOR'  'VBRK-FKART'.
            PERFORM FRM_BDC_FIELD   USING 'BDC_OKCODE'  '=KFDE'.



            PERFORM FRM_BDC_DYNPRO  USING 'SAPMV60A'    '6001'..
            PERFORM FRM_BDC_FIELD   USING 'BDC_OKCODE'  '=KFKO'.
            PERFORM FRM_BDC_FIELD   USING 'BDC_CURSOR'  'VBRK-FKDAT'.

            IF LW_SEG_INFO-FKDAT IS INITIAL.
              PERFORM FRM_BDC_FIELD   USING 'VBRK-FKDAT' WA_LIKP-FKDAT.
            ELSE.
              PERFORM FRM_BDC_FIELD   USING 'VBRK-FKDAT' LW_SEG_INFO-FKDAT.
            ENDIF.



            IF  LW_SEG_INFO-ZYBH IS NOT INITIAL.
              PERFORM FRM_BDC_DYNPRO  USING 'SAPMV60A'    '6001'.
              PERFORM FRM_BDC_FIELD   USING 'BDC_OKCODE'  '=V69A_KOAN'.
              PERFORM FRM_BDC_FIELD   USING 'BDC_CURSOR'  'KOMV-KSCHL(01)'.


              PERFORM FRM_BDC_DYNPRO  USING 'SAPMV60A'    '6001'.
              PERFORM FRM_BDC_FIELD   USING 'BDC_OKCODE'  '/00'.
              PERFORM FRM_BDC_FIELD   USING 'BDC_CURSOR'  'KOMV-KBETR(02)'.
              PERFORM FRM_BDC_FIELD   USING 'KOMV-KSCHL(02)'  'ZYJH'.
              PERFORM FRM_BDC_FIELD   USING 'KOMV-KBETR(02)'  LW_SEG_INFO-ZYBH.
            ENDIF.


            IF  LW_SEG_INFO-ZYFB IS NOT INITIAL.
              PERFORM FRM_BDC_DYNPRO  USING 'SAPMV60A'    '6001'.
              PERFORM FRM_BDC_FIELD   USING 'BDC_OKCODE'  '=V69A_KOAN'.
              PERFORM FRM_BDC_FIELD   USING 'BDC_CURSOR'  'KOMV-KSCHL(01)'.

              PERFORM FRM_BDC_DYNPRO  USING 'SAPMV60A'    '6001'.
              PERFORM FRM_BDC_FIELD   USING 'BDC_OKCODE'  '/00'.
              PERFORM FRM_BDC_FIELD   USING 'BDC_CURSOR'  'KOMV-KBETR(02)'.
              PERFORM FRM_BDC_FIELD   USING 'KOMV-KSCHL(02)'  'ZYFB'.
              PERFORM FRM_BDC_FIELD   USING 'KOMV-KBETR(02)' LW_SEG_INFO-ZYFB.
              LV_ZCK5 = LW_SEG_INFO-ZYFB.
            ENDIF.

            IF  LW_SEG_INFO-ZYFY IS NOT INITIAL.


              PERFORM FRM_BDC_DYNPRO  USING 'SAPMV60A'    '6001'.
              PERFORM FRM_BDC_FIELD   USING 'BDC_OKCODE'  '=V69A_KOAN'.
              PERFORM FRM_BDC_FIELD   USING 'BDC_CURSOR'  'KOMV-KSCHL(03)'.

              PERFORM FRM_BDC_DYNPRO  USING 'SAPMV60A'    '6001'.
              PERFORM FRM_BDC_FIELD   USING 'BDC_OKCODE'  '/00'.
              PERFORM FRM_BDC_FIELD   USING 'BDC_CURSOR'  'KOMV-KBETR(02)'.
              PERFORM FRM_BDC_FIELD   USING 'KOMV-KSCHL(02)'  'ZYFY'.
              PERFORM FRM_BDC_FIELD   USING 'KOMV-KBETR(02)'  LW_SEG_INFO-ZYFY.
              LV_ZCK5 = LV_ZCK5 + LW_SEG_INFO-ZYFY.
            ENDIF.

            IF LW_SEG_INFO-ZCK5 IS NOT INITIAL.

              PERFORM FRM_BDC_DYNPRO  USING 'SAPMV60A'    '6001'.
              PERFORM FRM_BDC_FIELD   USING 'BDC_OKCODE'  '=V69A_KOAN'.
              PERFORM FRM_BDC_FIELD   USING 'BDC_CURSOR'  'KOMV-KSCHL(03)'.

              PERFORM FRM_BDC_DYNPRO  USING 'SAPMV60A'    '6001'.
              PERFORM FRM_BDC_FIELD   USING 'BDC_OKCODE'  '/00'.
              PERFORM FRM_BDC_FIELD   USING 'BDC_CURSOR'  'KOMV-KBETR(02)'.
              PERFORM FRM_BDC_FIELD   USING 'KOMV-KSCHL(02)'  'ZCK5'.
              PERFORM FRM_BDC_FIELD   USING 'KOMV-KBETR(02)'  LW_SEG_INFO-ZCK5.

            ENDIF.


            PERFORM FRM_BDC_DYNPRO  USING 'SAPMV60A'    '6001'.
            PERFORM FRM_BDC_FIELD   USING 'BDC_OKCODE'  '=KFCU'.
            PERFORM FRM_BDC_FIELD   USING 'BDC_CURSOR'  'KOMV-KSCHL(01)'.

            PERFORM FRM_BDC_DYNPRO  USING 'SAPMV60A'    '6001'.

            PERFORM FRM_BDC_FIELD   USING 'BDC_OKCODE'  '=SICH'.
            PERFORM FRM_BDC_FIELD   USING 'BDC_CURSOR'  'VBRK-ZBILLTY'.
            PERFORM FRM_BDC_FIELD   USING 'VBRK-ZBILLTY' LW_SEG_INFO-ZBILLTY.


            CALL TRANSACTION 'VF01' USING GT_BDCDATA
                          MODE  'N' "N不显示  A调试
                          UPDATE 'S'
                     MESSAGES INTO LT_MESSTAB.
            .

            READ TABLE LT_MESSTAB WITH KEY MSGTYP = 'E'.
            IF SY-SUBRC = 0.
              STATUS = 'E'.
              LOOP AT LT_MESSTAB WHERE MSGTYP = 'E'.
                MESSAGE ID     LT_MESSTAB-MSGID
                        TYPE   LT_MESSTAB-MSGTYP
                        NUMBER LT_MESSTAB-MSGNR
                        INTO MESSAGE
                        WITH LT_MESSTAB-MSGV1
                             LT_MESSTAB-MSGV2
                             LT_MESSTAB-MSGV3
                             LT_MESSTAB-MSGV4.
              ENDLOOP.

            ELSE.
              COMMIT WORK AND WAIT.
              READ TABLE LT_MESSTAB WITH KEY MSGTYP = 'S'  MSGNR = '311'.

              VBELN_VF = LT_MESSTAB-MSGV1.."发票凭证
              STATUS = 'S'.
              MESSAGE = '交货单开票成功'.
            ENDIF.




            TMP_FLAG = 'E'.
          ENDIF.
        ELSE.
          TMP_FLAG = 'S'.
          CALL FUNCTION 'BAPI_BILLINGDOC_CREATEMULTIPLE'
            TABLES
              BILLINGDATAIN = LT_BILLING
              ERRORS        = LT_ERRORS
              RETURN        = LT_RETURN1
              SUCCESS       = LT_SUCCESS.
        ENDIF.


        IF TMP_FLAG = 'S'.


          IF LT_SUCCESS[] IS NOT INITIAL.
            READ TABLE LT_SUCCESS INTO LS_SUCCESS INDEX 1.

            IF SY-SUBRC EQ 0.
              VBELN_VF =  LS_SUCCESS-BILL_DOC."发票凭证
              STATUS = 'S'.
              MESSAGE = '交货单开票成功'.

              CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'
                EXPORTING
                  WAIT = 'X'.
              COMMIT WORK AND WAIT.


            ENDIF.
          ELSE.

            CALL FUNCTION 'BAPI_TRANSACTION_ROLLBACK'.

            STATUS = 'E'.

            LOOP AT LT_RETURN1 INTO LS_RETURN1 WHERE TYPE = 'E' .

              MESSAGE = LS_RETURN1-MESSAGE.

            ENDLOOP.

            LOOP AT LT_ERRORS INTO LS_ERRORS WHERE TYPE = 'E' OR TYPE = 'A'.

              CONCATENATE MESSAGE LS_ERRORS-MESSAGE INTO MESSAGE.
              CLEAR LS_ERRORS.
            ENDLOOP.

          ENDIF.
        ENDIF.




        CALL FUNCTION 'ZCRM0201_OUTPUT_IDOC' STARTING NEW TASK 'ZCRM0201_OUTPUT_IDOC'
          EXPORTING
            IM_VBELN = VBELN_VF
            MESSAGE  = MESSAGE
            ETYPE    = STATUS
            IM_VGBEL = IM_VGBEL
            INV_TYPE = 'A'.
      ENDIF.

    ENDIF.
    CLEAR:LW_SEG_INFO.
  ENDLOOP.
ENDFUNCTION.

标签:INFO,VF01,BDC,PERFORM,FRM,FIELD,交货单,USING,SAP
来源: https://www.cnblogs.com/liu1838998131/p/14390753.html