Print total amount in words

This script can be used to print total amount in words

Administrator Menu > Maintenance > Resources
1. In Ticket.Buttons add below line:

<event key="ticket.close" code="Amount.Letter"/>

2. Add a new resource Amount.Letter and add below code:

private static String[] _groups = 
   { "", "Millon","Billon","Trillon"};
 
private static String[] _units = 
   {"", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine"};
 
private static String[] _ten1 = 
 {"", "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen"};
 
private static String[] _tens = 
 {"", "Ten", "Twenty", "Thirty", "Fourty", "Fifty", "Sixty", "Seventy", "Eighty", "Ninety"};
 
private static String[] _hundreds = 
 {"", "One Hundred", "Two Hundred", "Three Hundred", "Four Hundred", "Five Hundred", "Six Hundred", "Seven Hundred"
, "Eight Hundred", "Nine Hundred"};
 
public static String thousandText( int n ) {
   if (n == 0)
      return "";
 
   int hundreds = n / 100;
   n = n % 100;
   int tens = n / 10;
   int units = n % 10;
    
   String suffix = "";
    
   if ( tens == 0 && units != 0 ) 
      suffix = _units[units];
    
   if ( tens == 1 && units != 0 )
      suffix = _ten1[units];
    
   if ( tens == 2 && units != 0 )
      suffix = "Twenty"+_units[units];
    
   if ( units == 0) 
      suffix = _tens[tens];
    
   if ( tens > 2 && units != 0)
      suffix = _tens[tens] + " and " + _units[units];
    
   if (hundreds != 1)
      return _hundreds[hundreds] + " " + suffix;
    
   if ( units == 0 && tens == 0)
      return "Hundred";
    
   return "Hundred "+suffix; 
}
 
public static String NumberToLetter( long n  ){
   String result = "";
   int group = 0;
   while ( n != 0 && group < _groups.length ) {
      long fragment = n % 1000000;
      int millarAlto = (int) (fragment / 1000);
      int underathousand = (int) (fragment % 1000);
      n = n / 1000000;
      
      String groupname = _groups[group];
      if (fragment > 1 && group > 0)
         groupname += "es";
      
      if ((millarAlto != 0) || (underathousand != 0)) {
         if (millarAlto > 1)
            result = thousandText(millarAlto) + " thousand " + 
                        thousandText(underathousand) + " " +
                        groupname + " " +
                        result;
 
         if (millarAlto == 0) 
            result = thousandText(underathousand) + " " +
                        groupname + " "+
                        result;
                      
         if (millarAlto == 1)
            result = "thousand " + thousandText(underathousand) + " " +
                        groupname + " " +
                        result;
      }
      group++;
   }
   return result;
}
 
number = ticket.getTotal(); 
number = Math.round(number*Math.pow(10,2))/Math.pow(10,2);
number_whole=(int)number;
number_decimal=(int)((number*100)-(number_whole*100));
value1 = NumberToLetter(number_whole);
value2 = NumberToLetter(number_decimal);
ticket.setProperty("amount", value1);
ticket.setProperty("decimal", value2);

3. In Printer.Ticket and Printer.TicketPreview use below code to print amount in words:

<line>
   <text align="left">${ticket.getProperty("amount")} ${ticket.getProperty("decimal")}</text>
</line>

Save

0 Comments

Leave a Reply

Arrow-up