about calculate tax for customize segments at IDOC FIDCC2

One requirement is to append customized segments (which store header details and item details separately) at IDOC FIDCC2 (with Basic type FIDCCP02) which will generate synchronized when customer invoice been created.

All those customized segments and their values can be calculated and populated by using enhancement F050S001 (FIDCMT, FIDCC1, FIDCC2: Edit user-defined IDOC segment) which is function module ‘EXIT_SAPLF050_001’.

The important signature of this user exit:

  • inputs parameter table FI_DOCUMENT_HEADER store data like temp table of BKPF
  • inputs parameter table FI_DOCUMENT_ITEM store data like temp table of BSEG

Header level tax calculation

To calculate the total tax at header level only while FI_DOCUMENT_HEADER-XMWST EQ ‘X’ AND FI_DOCUMENT_ITEMKOART = ‘D’:

 IF fi_document_header-xmwst EQ 'X'. REFRESH : lt_tax. CALL FUNCTION 'CALCULATE_TAX_FROM_GROSSAMOUNT' EXPORTING i_bukrs = fi_document_header-bukrs i_mwskz = fi_document_item-mwskz i_waers = fi_document_item-pswsl i_wrbtr = fi_document_item-wrbtr TABLES t_mwdat = lt_tax. CLEAR : lv_tax. LOOP AT lt_tax ASSIGNING FIELD-SYMBOL(<fs_tax>). CASE <fs_tax>-ktosl. WHEN 'XXX'. ... IF fi_document_header-kursf IS INITIAL. lv_amt = <fs_tax>-wmwst. ELSE. lv_amt = <fs_tax>-wmwst * fi_document_header-kursf. ENDIF. lv_total_tax = lv_total_tax + lv_amt. WHEN 'XXX'. ... ENDCASE. ENDLOOP. ENDIF.

Item level tax calculation

To calculate tax amount per item while FI_DOCUMENT_HEADER-XMWST EQ ‘X’ AND FI_DOCUMENT_ITEMKOART= ‘S’:

 IF fi_document_header-xmwst EQ 'X'. REFRESH : lt_tax. CALL FUNCTION 'CALCULATE_TAX_FROM_NET_AMOUNT' EXPORTING i_bukrs = fi_document_header-bukrs i_mwskz = fi_document_item-mwskz i_waers = fi_document_item-pswsl i_wrbtr = fi_document_item-wrbtr TABLES t_mwdat = lt_tax. CLEAR : lv_tax. LOOP AT lt_tax ASSIGNING FIELD-SYMBOL(<fs_tax>). CASE <fs_tax>-ktosl. WHEN 'XXX'. ... IF fi_document_header-kursf IS INITIAL. lv_amt = <fs_tax>-wmwst. ELSE. lv_amt = <fs_tax>-wmwst * fi_document_header-kursf. ENDIF. lv_total_tax = lv_total_tax + lv_amt. WHEN 'XXX'. ... ENDCASE. ENDLOOP. ENDIF.

The deviation between header and total of items

Unfortunately, user feedback sometimes there’s a mismatch between the tax amounts at the header level at customized segment compared with the sum of the items tax amount. The items tax and its total amount is the perfect match with BSET tables but has a very tiny offset compared with the tax amount at the header (like 9379.02 VS 9738.38).

Not sure yet but this deviation very likely comes from the round-off during the sum of calculation as those two function modules calculate the value with only 2 decimal places, let alone the exchange rate using 4 decimal places. But when the amount of total invoice is a huge number, the offset will be a big gap as been multiplied several places.

  1. Why not use BSET results as it’s exactly the same as item tax calculation? Pity that the BSET table has not been calculated and updated yet~
  2. Why not use the sum of item tax as the total tax amount at the header level? The user exit ‘EXIT_SAPLF050_001′ lies in routine ‘AUFBAU_USERPOS’ which inside the loop of BSEG table. Can’t save the temporary value per loop and update the value at the head segment unless at a global level.
  3. Currently about to wait for all calculations been done at user exit 001, then collect all items tax amount to overwrite the incorrect header tax amount inside exit 007 (Enhancement F050S003, FIDCC2: Change IDoc/do not send). Here the main FM to generate this Idoc is FI_IDOC_CREATE_FIDCC2.

There must exist a better approach to achieve this, please kindly let me know if we’re facing the same issue! And besides, I haven’t found how to debug the Idoc creation process during this FB70. will update.