Enterprise Resource Planning Blogs by Members
Gain new perspectives and knowledge about enterprise resource planning in blog posts from community members. Share your own comments and ERP insights today!
cancel
Showing results for 
Search instead for 
Did you mean: 
NIZAR_KHELIFI
Explorer
Introduction:

Hi SAP MASTERS,

The objective of this blog post is to create a custom Function Module in an SAP S/4HANA system that converts numeric amounts into Arabic words. This is particularly useful for invoice printing where the amount needs to be represented in Arabic text.

While SAP does offer standard Function Modules for this purpose 'SPELL_AMOUNT', but they may have limitations that don't fully align with specific requirements.

(read the folowing Note for more info 2677439 - FAQ - Module fonction SPELL_AMOUNT)

I'm trying to implement some package for the Arabic language, and I even have to add entries to the table T015Z. However, the amount in words is not displaying correctly.

Exemple:


spell amount input



result


 

Therefore, the goal here is to develop a custom Function Module tailored to meet these unique needs.

To achieve this, we will follow these essential steps:

Process:

To create a function module that converts an amount into Arabic words, you can follow these steps in an SAP S/4HANA system:

1. **Create a Function Module**:

- Open the SAP GUI and go to the ABAP Development Workbench (Transaction `SE80`).

- In the Repository Browser, navigate to the Function Group where you want to create the Function Module. If the Function Group doesn't exist, you can create one.


Create Function Group


- Right-click on the Function Group and choose "Create" > "Function Module."

- Define the Function Module's interface, including the input and output parameters. You will need an input parameter for the numeric amount and an output parameter for the Arabic text representation of the amount.
IMPORTING
*" REFERENCE(AMT_IN_NUM) LIKE PC207-BETRG
*" EXPORTING
*" REFERENCE(AMT_IN_WORDS) TYPE C


Import


 

 


Export


2. **Implement the Function Module**:

- In the Function Module editor, implement the ABAP code to convert the numeric amount into Arabic words.
- You can write the code for the conversion logic. This code will take the numeric amount as input and generate the Arabic text representation.

Here's a simplified example of how you can convert a numeric amount to Arabic words. Please note that this is a basic example, and you may need to adapt it to your specific requirements:

 
FUNCTION zhr_ar_chg_nb_wrds.
*"----------------------------------------------------------------------
*"*"Interface locale :
*" IMPORTING
*" REFERENCE(AMT_IN_NUM) LIKE PC207-BETRG
*" EXPORTING
*" REFERENCE(AMT_IN_WORDS) TYPE C
*" EXCEPTIONS
*" DATA_TYPE_MISMATCH
*"----------------------------------------------------------------------




*&----------------------------------------------------------------------------------------------*
*==---------------------------------------------------------------------------------------------*
*== Client : ######### ==*
*== Auteur Technique : KHELIFI Nizar ==*
*== Date de création : 05/09/2023 ==*
*== Version : 0.0.1 ==*
*==---------------------------------------------------------------------------------------------*
*== Description : Module fonction to spell amount in arabic ==*
*===============================================================================================*
*-----------------------------------------------------------------------------------------------
* HISTORIQUE MODIFICATIONS
* Date Auteur N° ticket Description
*
*-----------------------------------------------------------------------------------------------
DATA: maxno TYPE p.
maxno = 10 ** 9.
IF ( amt_in_num >= maxno ).
RAISE data_type_mismatch.
ENDIF.
*data declaration-------------------------------------------------*
DATA: ten(10),single(6),final(130),dec(20),res TYPE i,rp(7).
DATA: a1 TYPE i,a2 TYPE i,str(20),d TYPE p,m TYPE i,wrdrep(20).
DATA: cntr TYPE i,f1 TYPE i,f2 TYPE i,f3 TYPE i,f4 TYPE i,f5 TYPE i.
DATA: f6 TYPE i,f7 TYPE i,f8 TYPE i,f9 TYPE i.

d = ( amt_in_num * 100 ) DIV 100.
res = ( amt_in_num * 100 ) MOD 100.

f1 = res DIV 10.
f2 = res MOD 10.
PERFORM setnum USING f1 f2 CHANGING wrdrep.
f1 = 0. f2 = 0.
dec = wrdrep.
cntr = 1.
*Go in a loop dividing the numbers by 10 and store the
*residues as a digit in f1 .... f9
WHILE ( d > 0 ).
m = d MOD 10.
d = d DIV 10.
CASE cntr.
WHEN 1. f1 = m.
WHEN 2. f2 = m.
WHEN 3. f3 = m.
WHEN 4. f4 = m.
WHEN 5. f5 = m.
WHEN 6. f6 = m.
WHEN 7. f7 = m.
WHEN 8. f8 = m.
WHEN 9. f9 = m.
ENDCASE.
cntr = cntr + 1.
ENDWHILE.
cntr = cntr - 1.
*Going in loop and sending pair of digits to function setnum to get
*the standing value of digits in words
WHILE ( cntr > 0 ).

IF ( cntr <= 2 ).
PERFORM setnum USING f2 f1 CHANGING wrdrep.
IF f3 <> 0 AND wrdrep <> ''.
CONCATENATE final'و' wrdrep INTO final SEPARATED BY ' '.
ELSEIF f4 <> 0 AND wrdrep <> ''.
CONCATENATE final'و' wrdrep INTO final SEPARATED BY ' '.
ELSEIF f3 ='' AND f1 <> 0 AND ( f4 <> 0 OR f5 <> 0 OR f6 <> 0 ) .
CONCATENATE final 'و' wrdrep INTO final SEPARATED BY ' '.
ELSEIF f2 ='' .
CONCATENATE final wrdrep INTO final SEPARATED BY ' '.
ELSEIF f3 IS NOT INITIAL.
CONCATENATE final wrdrep INTO final SEPARATED BY ' و '.
ELSE.
CONCATENATE final wrdrep INTO final SEPARATED BY ' '.
ENDIF.
ELSEIF ( cntr = 3 ).
IF ( f3 <> 0 ).
PERFORM setnum USING 0 f3 CHANGING wrdrep.
IF wrdrep = 'واحد' AND f4 <> ''.
wrdrep = ' '.
CONCATENATE wrdrep final 'و' 'مائة' INTO final SEPARATED BY ' '.
ELSEIF wrdrep = 'واحد' AND f4 = 0 AND f5 <> ''.
wrdrep = ' '.
CONCATENATE wrdrep final 'و' 'مائة' INTO final SEPARATED BY ' '.

ELSEIF wrdrep = 'واحد' AND f6 <> '' .
wrdrep = ' '.
CONCATENATE final 'و' 'مائة' INTO final SEPARATED BY ' '.
ELSEIF wrdrep = 'واحد'.
wrdrep = ' '.
CONCATENATE 'مائة' wrdrep final INTO final SEPARATED BY ' '.

ELSEIF wrdrep = 'إثنان' AND f4 <> ''.
wrdrep = ' '.
CONCATENATE wrdrep final 'و' 'مِائَتَان' INTO final SEPARATED BY ' '.
ELSEIF wrdrep = 'إثنان' AND f4 = 0 AND f5 <> ''.
wrdrep = ' '.
CONCATENATE wrdrep final 'و' 'مِائَتَان' INTO final SEPARATED BY ' '.
ELSEIF wrdrep = 'إثنان' AND f6 <> '' .
wrdrep = ' '.
CONCATENATE final 'و' 'مِائَتَان' INTO final SEPARATED BY ' '.
ELSEIF wrdrep = 'إثنان'.
wrdrep = ' '.
CONCATENATE 'مِائَتَان' wrdrep final INTO final SEPARATED BY ' '.
ELSEIF f4 <> ''.
CONCATENATE final 'و' wrdrep 'مائة' INTO final SEPARATED BY ' '.
ELSEIF f5 <> ''.
CONCATENATE final 'و' wrdrep 'مائة' INTO final SEPARATED BY ' '.
ELSEIF f6 <> ''.
CONCATENATE final 'و' wrdrep 'مائة' INTO final SEPARATED BY ' '.
ELSE.
CONCATENATE final wrdrep 'مائة' INTO final SEPARATED BY ' '.
ENDIF.
ENDIF.

ELSEIF ( cntr <= 5 ).
IF ( f5 <> 0 ) OR ( f4 <> 0 ).
PERFORM setnum USING f5 f4 CHANGING wrdrep.
IF wrdrep = 'واحد' AND f6 <> 0 . " a verifier demain
wrdrep = ' '.
CONCATENATE final 'و' 'ألف' INTO final SEPARATED BY ' '.
ELSEIF wrdrep = 'واحد'.
wrdrep = ' '.
CONCATENATE 'ألف' wrdrep final INTO final SEPARATED BY ' '.

ELSEIF wrdrep = 'إثنان' AND f6 <> 0 AND f5 = 0. " a verifier demain
wrdrep = ' '.
CONCATENATE final 'و' wrdrep 'ألف' INTO final SEPARATED BY ' '.

ELSEIF wrdrep = 'إثنان'.
wrdrep = ' '.
CONCATENATE 'ألفين' wrdrep final INTO final SEPARATED BY ' '.
ELSEIF f5 = 0 AND f4 <> 0 .
CONCATENATE wrdrep 'ألاف' final INTO final SEPARATED BY ''.
* CONCATENATE wrdrep 'ألف' final INTO final SEPARATED BY ''.
ELSEIF f5 = 1 AND f4 = 0 .
CONCATENATE wrdrep 'ألاف' final INTO final SEPARATED BY ''.
ELSE.
* CONCATENATE wrdrep 'ألاف' final INTO final SEPARATED BY ''.
CONCATENATE wrdrep 'ألف' final INTO final SEPARATED BY ''.

ENDIF.
ENDIF.
IF ( cntr = 4 ).
cntr = 5.
ENDIF.
ELSEIF ( cntr < 7 ).
IF ( f6 <> 0 ).
PERFORM setnum USING 0 f6 CHANGING wrdrep.
IF wrdrep = 'واحد' AND f4 = 0 AND f5 = 0 AND f6 <> 0.
wrdrep = 'ألف'.
CONCATENATE final 'مائة' wrdrep INTO final SEPARATED BY ' '.

ELSEIF wrdrep = 'واحد' AND ( f5 <> 0 OR f4 <> 0 ).
wrdrep = ' '.
CONCATENATE final 'مائة' INTO final SEPARATED BY ' '.


ELSEIF wrdrep = 'إثنان' AND f4 = 0 AND f5 = 0 AND f6 <> 0.
wrdrep = 'ألف'.
CONCATENATE final 'مِائَتَان' wrdrep INTO final SEPARATED BY ' '.

ELSEIF wrdrep = 'إثنان' AND ( f5 <> 0 OR f4 <> 0 ).
wrdrep = ' '.
CONCATENATE final 'مِائَتَان' INTO final SEPARATED BY ' '.
ELSEIF f6 <> 0 AND f4 = 0 AND f5 = 0.
CONCATENATE final wrdrep 'مائة' 'ألف' INTO final SEPARATED BY ' '.
ELSE.
CONCATENATE final wrdrep 'مائة' INTO final SEPARATED BY ' '.
ENDIF.
ELSE.
PERFORM setnum USING f5 f4 CHANGING wrdrep.
CONCATENATE final 'و' wrdrep 'ألف' INTO final SEPARATED BY ' ' .
ENDIF.

IF f6 <> 0 AND ( f5 <> 0 OR f4 <> 0 ) .
PERFORM setnum USING f5 f4 CHANGING wrdrep.
CONCATENATE final 'و' wrdrep 'ألف' INTO final SEPARATED BY ' ' .
ENDIF.
ELSEIF ( cntr <= 9 ).

IF ( f9 <> 0 ) .
PERFORM setnum USING 0 f9 CHANGING wrdrep.
IF wrdrep = 'واحد'.
wrdrep = ''.
CONCATENATE 'مائة' final INTO final SEPARATED BY ' '.
ELSEIF wrdrep = 'إثنان' .
wrdrep = ' '.
CONCATENATE 'مِائَتَان' final INTO final SEPARATED BY ' '.
ELSE.
CONCATENATE wrdrep 'مائة' INTO final SEPARATED BY ' '.


ENDIF.

PERFORM setnum USING f8 f7 CHANGING wrdrep.
CONCATENATE final 'و' wrdrep 'مليون' INTO final SEPARATED BY ' ' .

* ELSEIF cntr = 8 . "( f9 = 0 ) .
* PERFORM setnum USING f8 f7 CHANGING wrdrep.
* CONCATENATE wrdrep 'مليون' INTO final SEPARATED BY ' ' .
*
ELSEIF ( cntr <= 8 ). "( f9 = 0 ) .

PERFORM setnum USING f8 f7 CHANGING wrdrep.

* CONCATENATE final 'و' wrdrep 'مليون' INTO final SEPARATED BY ' ' .

CONCATENATE final wrdrep 'مليون' INTO final SEPARATED BY ' ' .


ENDIF.
ENDIF.



IF ( cntr = 6 ).
cntr = 3.
ELSEIF ( cntr = 7 ).
cntr = cntr - 1.
ELSEIF ( cntr = 9 ).
cntr = 6.

ELSE.
cntr = cntr - 2.
ENDIF.
ENDWHILE.
*Output the final
IF ( final = 'واحد' ) OR ( final = 'إثنان' ) .
rp = 'دولار'(003).
ELSEIF ( final = 'ثلاثة' ) OR ( final = 'أربعة' ) OR ( final = 'خمسة' )
OR ( final = 'ستة' ) OR ( final = 'سبعة' ) OR ( final = 'ثمانية' ) OR ( final = 'تسعة' ).
rp = 'دولارات'(003).
ELSE.
rp = 'دولار'(001).
ENDIF
.
IF ( final = '' ) AND ( dec = '' ).
final = 'صفر'.
ELSEIF ( final = '' ).
CONCATENATE dec 'سنت'(002) INTO final SEPARATED BY ' ' .
ELSEIF ( dec = '' ).

CONCATENATE final rp INTO final SEPARATED BY ' ' .
ELSE.
CONCATENATE final rp ' و ' dec 'سنت'(002) INTO final SEPARATED BY ' ' .
ENDIF.

amt_in_words = final.

ENDFUNCTION.

*&---------------------------------------------------------------------*
*& Form SETNUM
*&---------------------------------------------------------------------*
* converts a number into words *
*----------------------------------------------------------------------*
* --> a1,a2 two digits for 2nd and 1st place
* <-- str outpur in words
*----------------------------------------------------------------------*
DATA: ten(10),single(6),str(20).
*
FORM setnum USING a1 a2 CHANGING str.
ten = ''.single = ''.
IF ( a1 = 1 ).

CASE a2.
WHEN 0.
ten = 'عشرة'.
WHEN 1.
ten = 'احد عشر'.
WHEN 2.
ten = 'اثنا عشر'.
WHEN 3.
ten = 'ثلاثة عشر'.
WHEN 4.
ten = 'اربعة عشر'.
WHEN 5.
ten = 'خمسة عشر'.
WHEN 6.
ten = 'ستة عشر'.
WHEN 7.
ten = 'سبعة عشر'.
WHEN 8.
ten = 'ثمانية عشر'.
WHEN 9.
ten = 'تسعة عشر'.
ENDCASE.
ELSE.

CASE a2.
WHEN 1.
single = 'واحد'.
WHEN 2.
single = 'إثنان'.
WHEN 3.
single = 'ثلاثة'.
WHEN 4.
single = 'أربعة'.
WHEN 5.
single = 'خمسة'.
WHEN 6.
single = 'ستة'.
WHEN 7.
single = 'سبعة'.
WHEN 8.
single = 'ثمانية'.
WHEN 9.
single = 'تسعة'.
ENDCASE.
CASE a1.
WHEN 2.
ten = 'عشرون'.
WHEN 3.
ten = 'ثلاثون'.
WHEN 4.
ten = 'أربعون'.
WHEN 5.
ten = 'خمسون'.
WHEN 6.
ten = 'ستون'.
WHEN 7.
ten = 'سبعون'.
WHEN 8.
ten = 'ثمانون'.
WHEN 9.
ten = 'تسعون'.
ENDCASE.

ENDIF.
IF ( single <> '' ) AND ( ten <> '' ).
CONCATENATE single ten INTO str SEPARATED BY ' و '.
ELSEIF single = ''.
str = ten.
ELSE.
str = single.
ENDIF.

ENDFORM. " SETNUM

 

In the example above, the function module `ZHR_AR_CHG_NB_WRDS` takes a numeric amount as input (`AMT_IN_NUM`) and converts it into Arabic words, storing the result in the output parameter `AMT_IN_WORDS`.

Please note that this is a simplified example, and you may need a more comprehensive solution for handling larger numbers, decimals, and various currencies. Consider using specialized libraries or functions for Arabic number-to-word conversion to handle a wide range of scenarios accurately. Additionally, you might need to consider language variations in Arabic, depending on your specific localization requirements.

Result : 


NB : You can change the currency for the output at line 239, adjust the decimal number at line 249, and keep in mind that the limit number for this function module is 999.999.999,99.

 

 

Hope this helps.

 

Regards

Nizar

 

 
1 Comment
Labels in this area