#=====================================================================
# 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;