ABAP 预制发票BAPI BAPI_INCOMINGINVOICE_CREATE1详解编程语言

FORM frm_batch_invocice . 
  DATA: lt_alv TYPE STANDARD TABLE OF gty_alv, 
        ls_alv LIKE LINE OF lt_alv. 
  DATA: ls_head LIKE bapi_incinv_create_header. 
  DATA: lt_item TYPE STANDARD TABLE OF bapi_incinv_create_item, 
        ls_item LIKE LINE OF lt_item. 
  DATA: lt_return TYPE STANDARD TABLE OF bapiret2, 
        ls_return LIKE LINE OF lt_return. 
  DATA: lv_number LIKE bapi_incinv_fld-inv_doc_no. 
  DATA: lv_field TYPE c LENGTH 100, 
        lv_flag  TYPE c. 
  DATA: lv_index TYPE i. 
  DATA: lt_tax TYPE STANDARD TABLE OF bapi_incinv_create_tax, 
        ls_tax LIKE LINE OF lt_tax. 
  DATA: lv_no TYPE i. 
  READ TABLE gt_alv WITH KEY iconid = icon_led_red TRANSPORTING NO FIELDS. 
  IF sy-subrc = 0. 
    MESSAGE '请检查数据后,重新上传文件!' TYPE 'S' DISPLAY LIKE 'E'. 
    RETURN. 
  ENDIF. 
  lt_alv = gt_alv. 
  DELETE ADJACENT DUPLICATES FROM lt_alv COMPARING hbbj. 
  LOOP AT lt_alv INTO ls_alv. 
    CLEAR: ls_head,lv_index,lt_item,lt_return,ls_tax,lt_tax. 
    lv_no = lv_no + 1. 
    ls_head-gross_amount = ls_alv-wrbtr3. 
    ls_head-invoice_ind = 'X'. 
    "ls_head-calc_tax_ind = 'X'. 
    ls_head-doc_date = ls_alv-bldat. "凭证日期 
    ls_head-pstng_date = ls_alv-budat. "过账日期 
    ls_head-bline_date = ls_alv-zfbdt.  "基准日期 
    ls_head-comp_code = ls_alv-bukrs. "公司代码 
    ls_head-diff_inv = ls_alv-lifnr.  "供应商 
    ls_head-deliv_posting = 'S'. 
    ls_head-return_posting = 'H'. 
    "ls_head-ref_doc_no = ls_alv-xblnr.  "交货单 
    ls_head-doc_type = 'RE'. "凭证类型 
    ls_head-currency = ls_alv-waers. 
    ls_tax-tax_amount = ls_head-gross_amount. 
    LOOP AT gt_alv INTO gs_alv WHERE hbbj = ls_alv-hbbj. 
      CLEAR ls_item. 
      lv_index = lv_index + 1. 
      ls_item-sheet_no = lv_no. 
      ls_item-sheet_item = lv_index. 
      ls_item-invoice_doc_item = lv_index. 
      "ls_head-gross_amount = ls_head-gross_amount + gs_alv-wrbtr2 . "凭证货币的总发票金额 
      ls_tax-tax_amount = ls_tax-tax_amount - gs_alv-wrbtr1. "税额 
 
      ls_item-po_number = gs_alv-ebeln. "采购凭证 
      ls_item-po_item = gs_alv-ebelp. "采购行项目 
 
      ls_item-ref_doc = gs_alv-lfbnr. "参考物料凭证 
      ls_item-ref_doc_year = gs_alv-mjahr.  "参考年份 
      ls_item-ref_doc_it = gs_alv-lfpos.  "参考凭证行 
      ls_item-tax_code = gs_alv-mwskz.  "税码 
      IF gs_alv-menge < 0. 
        ls_item-quantity = gs_alv-menge * -1.  "数量 
      ELSE. 
        ls_item-quantity = gs_alv-menge.  "数量 
      ENDIF. 
 
      IF gs_alv-wrbtr1 < 0. 
        ls_item-retention_docu_currency = gs_alv-wrbtr1 * -1.  "保留金额 
        ls_item-item_amount = gs_alv-wrbtr1 * -1.  "凭证货币金额 
      ELSE. 
        ls_item-retention_docu_currency = gs_alv-wrbtr1.  "保留金额 
        ls_item-item_amount = gs_alv-wrbtr1.  "凭证货币金额 
      ENDIF. 
 
      ls_item-po_unit = gs_alv-meins. "单位 
      APPEND ls_item TO lt_item. 
    ENDLOOP. 
*    LOOP AT lt_item ASSIGNING FIELD-SYMBOL(<fs_item>). 
*      lv_index = lv_index + 1. 
*      <fs_item>-invoice_doc_item = lv_index. 
*    ENDLOOP. 
    APPEND ls_tax TO lt_tax. 
    CALL FUNCTION 'BAPI_INCOMINGINVOICE_CREATE1' 
      EXPORTING 
        headerdata       = ls_head 
        invoicestatus    = 'A' 
      IMPORTING 
        invoicedocnumber = lv_number 
      TABLES 
        itemdata         = lt_item 
        taxdata          = lt_tax 
        return           = lt_return. 
 
    CLEAR lv_field. 
    LOOP AT lt_return INTO ls_return WHERE type CA 'EAX'. 
      CALL FUNCTION 'MESSAGE_TEXT_BUILD' 
        EXPORTING 
          msgid               = ls_return-id 
          msgnr               = ls_return-number 
          msgv1               = ls_return-message_v1 
          msgv2               = ls_return-message_v2 
          msgv3               = ls_return-message_v3 
          msgv4               = ls_return-message_v4 
        IMPORTING 
          message_text_output = lv_field. 
      lv_flag = 'X'. 
    ENDLOOP. 
 
    IF lv_flag IS INITIAL. 
      CALL FUNCTION 'BAPI_TRANSACTION_COMMIT' 
        EXPORTING 
          wait = 'X'. 
      lv_field = '过账成功,过账凭证:'. 
      CONCATENATE lv_field lv_number INTO lv_field SEPARATED BY ' '. 
      LOOP AT gt_alv ASSIGNING FIELD-SYMBOL(<fs_alv>)  WHERE hbbj = ls_alv-hbbj. 
        <fs_alv>-iconid = icon_led_green. 
        <fs_alv>-msg = lv_field. 
      ENDLOOP. 
    ELSE. 
      CALL FUNCTION 'BAPI_TRANSACTION_ROLLBACK'. 
      CONCATENATE '过账失败,失败原因:' lv_field lv_number INTO lv_field SEPARATED BY ' '. 
      LOOP AT gt_alv ASSIGNING FIELD-SYMBOL(<fs_alv1>)  WHERE hbbj = ls_alv-hbbj. 
        <fs_alv1>-iconid = icon_led_red. 
        <fs_alv1>-msg = lv_field. 
      ENDLOOP. 
    ENDIF. 
  ENDLOOP. 
ENDFORM.
FORM frm_check_alv . 
  DATA: lt_alv TYPE STANDARD TABLE OF gty_alv, 
        ls_alv LIKE LINE OF lt_alv. 
  DATA: lv_num1 TYPE i, 
        lv_num2 TYPE i. 
  DATA: lv_flag TYPE c. 
  DATA: l_t_qty LIKE ekbe-menge. 
  DATA: lv_menge LIKE mseg-menge. 
 
  READ TABLE gt_alv INTO gs_alv WITH KEY lfbnr = ''. 
 
  IF sy-subrc = 0. 
    lv_flag = 'X'. 
    gv_check_flag = 'E'. 
    LOOP AT gt_alv ASSIGNING FIELD-SYMBOL(<fs_alv>) WHERE ebeln = gs_alv-ebeln AND ebelp = gs_alv-ebelp . 
      <fs_alv>-iconid = icon_led_red. 
      <fs_alv>-msg = <fs_alv>-msg && '参考凭证有为空,请检查;'. 
    ENDLOOP. 
  ENDIF. 
 
  LOOP AT gt_alv ASSIGNING <fs_alv>. 
    CLEAR: lv_menge. 
    l_t_qty = 0. 
    SELECT SUM( CASE shkzg WHEN 'S' THEN menge ELSE -1 * menge END ) 
                INTO @l_t_qty 
                FROM ekbe 
    WHERE ebeln = @<fs_alv>-ebeln 
    AND   ebelp = @<fs_alv>-ebelp 
    AND   bewtp IN ('T','Q') 
    AND   lfgja = @<fs_alv>-mjahr 
    AND   lfbnr = @<fs_alv>-lfbnr 
    AND   lfpos = @<fs_alv>-lfpos. 
 
    lv_menge = <fs_alv>-menge1 - l_t_qty. 
 
    IF lv_menge <= 0 . 
      lv_flag = 'X'. 
      gv_check_flag = 'E'. 
      <fs_alv>-msg = <fs_alv>-msg && '订单已无发票校验,数量为0,请检查;'. 
      <fs_alv>-menge = <fs_alv>-menge - l_t_qty. 
    ELSEIF lv_menge < <fs_alv>-menge. 
      lv_flag = 'X'. 
      gv_check_flag = 'E'. 
      <fs_alv>-msg = <fs_alv>-msg && '订单可预制数量小于当前数量,请检查;'. 
      <fs_alv>-menge = <fs_alv>-menge - l_t_qty. 
    ENDIF. 
 
  ENDLOOP. 
 
  lt_alv = gt_alv. 
  DELETE ADJACENT DUPLICATES FROM lt_alv COMPARING hbbj. 
  LOOP AT lt_alv INTO ls_alv. 
    SELECT 
      COUNT(*) 
      FROM @gt_alv AS ga 
      WHERE hbbj = @ls_alv-hbbj 
      INTO @lv_num1. 
 
    SELECT 
     COUNT(*) 
     FROM @gt_alv AS ga 
     WHERE hbbj = @ls_alv-hbbj AND 
           budat = @ls_alv-budat AND 
           zfbdt = @ls_alv-zfbdt AND 
           bukrs = @ls_alv-bukrs AND 
           lifnr = @ls_alv-lifnr 
     INTO @lv_num2. 
 
    IF lv_num1 <> lv_num2. 
      lv_flag = 'X'. 
      gv_check_flag = 'E'. 
      LOOP AT gt_alv ASSIGNING <fs_alv> WHERE hbbj = ls_alv-hbbj . 
        <fs_alv>-iconid = icon_led_red. 
        <fs_alv>-msg = <fs_alv>-msg && '一个合并标识内出现不同的抬头字段,请检查;'. 
      ENDLOOP. 
    ENDIF. 
 
    IF ls_alv-wrbtr3 IS INITIAL. 
      lv_flag = 'X'. 
      gv_check_flag = 'E'. 
      LOOP AT gt_alv ASSIGNING <fs_alv> WHERE hbbj = ls_alv-hbbj . 
        <fs_alv>-iconid = icon_led_red. 
        <fs_alv>-msg = <fs_alv>-msg && '一个合并标识内第一行必须输入合并总金额,请检查;'. 
      ENDLOOP. 
    ENDIF. 
  ENDLOOP. 
 
  IF lv_flag IS INITIAL. 
    MESSAGE '校验成功,请过账' TYPE 'S'. 
    gv_check_flag = 'S'. 
  ELSE. 
    MESSAGE '校验失败,部分数据有问题请检查后重新上传' TYPE 'S' DISPLAY LIKE 'E'. 
    gv_check_flag = 'E'. 
    DATA:ls_stable TYPE lvc_s_stbl. 
    ls_stable-col = 'X'. 
    ls_stable-row = 'X'. 
    CALL METHOD gc_alv->refresh_table_display 
      EXPORTING 
        is_stable = ls_stable 
      EXCEPTIONS 
        finished  = 1 
        OTHERS    = 2. 
  ENDIF. 
 
 
ENDFORM.

原创文章,作者:Maggie-Hunter,如若转载,请注明出处:https://blog.ytso.com/18010.html

(0)
上一篇 2021年7月19日
下一篇 2021年7月19日

相关推荐

发表回复

登录后才能评论