#=====================================================================
# LedgerSMB Small Medium Business Accounting
# Copyright (C) 2002
#
#  Author: Dieter Simader
#   Email: dsimader@sql-ledger.org
#     Web: http://sourceforge.net/projects/ledger-smb/
#
#  Contributors:
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#======================================================================
#
# this is a variation of the Lingua package
# written for check and receipt printing
# it returns a properly formatted text string
# for a number up to 10**12

sub init {
  my $self = shift;

  %{ $self->{numbername} } =
                   (0 => 'Null',
                    1 => 'ein',
                    2 => 'zwei',
	            3 => 'drei',
		    4 => 'vier',
		    5 => 'fünf',
		    6 => 'sechs',
		    7 => 'sieben',
		    8 => 'acht',
		    9 => 'neun',
		   10 => 'zehn',
		   11 => 'elf',
		   12 => 'zwölf',
		   13 => 'dreizehn',
		   14 => 'vierzehn',
		   15 => 'fünfzehn',
		   16 => 'sechzehn',
		   17 => 'siebzehn',
		   18 => 'achtzehn',
		   19 => 'neunzehn',
		   20 => 'zwanzig',
		   30 => 'dreissig',
		   40 => 'vierzig',
		   50 => 'fünfzig',
		   60 => 'sechzig',
		   70 => 'siebzig',
		   80 => 'achtzig',
		   90 => 'neunzig',
                10**2 => 'hundert',
                10**3 => 'tausend',
		10**6 => 'million',
		10**9 => 'milliarde',
	       10**12 => 'billion'
		);

}


sub num2text {
  my ($self, $amount) = @_;

  return $self->{numbername}{0} unless $amount;

  my @textnumber = ();

  # split amount into chunks of 3
  my @num = reverse split //, abs($amount);
  my @numblock = ();
  my ($i, $appendn);
  my @a = ();

  while (@num) {
    @a = ();
    for (1 .. 3) {
      push @a, shift @num;
    }
    push @numblock, join / /, reverse @a;
  }
  
  my $belowhundred = !$#numblock;
  
  while (@numblock) {

    $i = $#numblock;
    @num = split //, $numblock[$i];
    $appendn = "";
    
    $numblock[$i] *= 1;
    
    if ($numblock[$i] == 0) {
      pop @numblock;
      next;
    }
   
    if ($numblock[$i] > 99) {
      # the one from hundreds
      push @textnumber, $self->{numbername}{$num[0]};
     
      # add hundred designation
      push @textnumber, $self->{numbername}{10**2};

      # reduce numblock
      $numblock[$i] -= $num[0] * 100;
    }
    
    $appendn = 'en' if ($i == 2);
    $appendn = 'n' if ($i > 2);

    if ($numblock[$i] > 9) {
      # tens
      push @textnumber, $self->format_ten($numblock[$i], $belowhundred);
    } elsif ($numblock[$i] > 1) {
      # ones
      push @textnumber, $self->{numbername}{$numblock[$i]};
    } elsif ($numblock[$i] == 1) {
      if ($i == 0) {
	push @textnumber, $self->{numbername}{$numblock[$i]}.'s';
      } else {
	if ($i >= 2) {
	  push @textnumber, $self->{numbername}{$numblock[$i]}.'e';
	} else {
	  push @textnumber, $self->{numbername}{$numblock[$i]};
	}
      }
      $appendn = "";
    }
    
    # add thousand, million
    if ($i) {
      $amount = 10**($i * 3);
      push @textnumber, $self->{numbername}{$amount}.$appendn;
    }
    
    pop @numblock;
    
  }

  join '', @textnumber;

}


sub format_ten {
  my ($self, $amount, $belowhundred) = @_;
  
  my $textnumber = "";
  my @num = split //, $amount;

  if ($amount > 20) {
    if ($num[1] == 0) { 
      $textnumber = $self->{numbername}{$amount}; 
    } else {
      if ($belowhundred) {
	$amount = $num[0] * 10;
	$textnumber = $self->{numbername}{$num[1]}.'und'.$self->{numbername}{$amount};
      } else {
	$amount = $num[0] * 10;
	$textnumber = $self->{numbername}{$amount}.$self->{numbername}{$num[1]};
	$textnumber .= 's' if ($num[1] == 1);
      }
    }
  } else {
    $textnumber = $self->{numbername}{$amount};
  }
  
  $textnumber;
  
}


1;