#=====================================================================
# LedgerSMB Small Medium Business Accounting
# Copyright (C) 2005
#
#  Author: Dieter Simader
#     Web: http://www.ledgersmb.org/
#
#  Contributors: Vladimir Khaimin <vkhai@yandex.ru>
#
# 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 => '����',
                    1 => '����',
                    2 => '���',
	            3 => '���',
		    4 => '������',
		    5 => '����',
		    6 => '�����',
		    7 => '����',
		    8 => '������',
		    9 => '������',
		   10 => '������',
		   11 => '����������',
		   12 => '����������',
		   13 => '����������',
		   14 => '������������',
		   15 => '����������',
		   16 => '�����������',
		   17 => '����������',
		   18 => '������������',
		   19 => '������������',
		   20 => '��������',
		   30 => '��������',
		   40 => '�����',
		   50 => '���������',
		   60 => '����������',
		   70 => '���������',
		   80 => '�����������',
		   90 => '���������',
                10**2 => '���',
                10**3 => '������',
		10**6 => '�������',
		10**9 => '��������',
	       10**12 => '��������'
		);

}


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;