#=====================================================================
# SQL-Ledger Accounting
# Copyright (C) 2001
#
#  Author: Dieter Simader
#   Email: dsimader@sql-ledger.org
#     Web: http://www.sql-ledger.org
#
#  Language: Hungarian
#  Contributors: Medgyesi Aniko (MEA1)
#
# 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.
#=====================================================================

sub init {
  my $self = shift;
  
  %{ $self->{numbername} } =
           (0 => 'Nulla',
            1 => 'egy',
            2 => 'kett',
	    3 => 'hrom',
	    4 => 'ngy',
	    5 => 't',
	    6 => 'hat',
	    7 => 'ht',
	    8 => 'nyolc',
	    9 => 'kilenc',
	   10 => 'tz',
	   11 => 'tizenegy',
	   12 => 'tizenkett',
	   13 => 'tizenhrom',
	   14 => 'tizenngy',
	   15 => 'tizent',
	   16 => 'tizenhat',
	   17 => 'tizenht',
	   18 => 'tizennyolc',
	   19 => 'tizenkilenc',
	   20 => 'hsz',
	   21 => 'huszonegy',
	   22 => 'huszonkett',
	   23 => 'huszonhrom',
	   24 => 'huszonngy',
	   25 => 'huszont',
	   26 => 'huszonhat',
	   27 => 'huszonht',
	   28 => 'huszonnyolc',
	   29 => 'huszonkilenc',
	   30 => 'harminc',
	   40 => 'negyven',
	   50 => 'tven',
	   60 => 'hatvan',
	   70 => 'hetven',
	   80 => 'nyolcvan',
	   90 => 'kilencven',
        10**2 => 'szz',
        10**3 => 'ezer',
	10**6 => 'milli',
	10**9 => 'millird',
	10**12 => 'billi',
	);

}


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

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

  my @textnumber = ();

  # split amount into chunks of 3
  my @num = reverse split //, $amount;
  my @numblock = ();
  my @a;
  my $i;
  
  while (@num) {
    @a = ();
    for (1 .. 3) {
      push @a, shift @num;
    }
    push @numblock, join / /, reverse @a;
  }
  while (@numblock) {
    $i = $#numblock;
    @num = split //, $numblock[$i];

    if ($numblock[$i] == 0) {
      pop @numblock;
      next;
    }
    if ($numblock[$i] > 99) {
      push @textnumber, $self->{numbername}{$num[0]};

      # add hundred designation
      push @textnumber, $self->{numbername}{10**2};

      # reduce numblock
      $numblock[$i] -= $num[0] * 100;

    }

    $numblock[$i] *= 1;
    if ($numblock[$i] > 9) {
      # tens
      push @textnumber, $self->format_ten($numblock[$i]);
    } elsif ($numblock[$i] > 0) {
      # ones
      push @textnumber, $self->{numbername}{$numblock[$i]};
    }

    # add thousand, million
    if ($i) {
      # above 2000 need hyphen between treegroups
      if ($i == 1 && $amount < 2000){

        $num = 10**($i * 3);
        push @textnumber, $self->{numbername}{$num};
      } else  {

        $num = 10**($i * 3);
        push @textnumber, $self->{numbername}{$num}."-";
      }
    }

    pop @numblock;

  }

  # First charachter is uppercase
  my $textnumber = ucfirst join '', @textnumber;

  # remove last hyphen
  $textnumber =~ s/(\-)$//;
  
  return $textnumber;

}


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

  my $textnumber = "";
  my @num = split //, $amount;

  # above 30
  if ($amount > 30) {
    $textnumber = $self->{numbername}{$num[0]*10};
    $amount = $num[1];
  } else {
    $textnumber = $self->{numbername}{$amount};
    $amount = 0;
  }

  $textnumber .= "".$self->{numbername}{$amount} if $amount;

  $textnumber;

}


1;

