[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

SF.net SVN: ledger-smb:[3896] trunk



Revision: 3896
          http://ledger-smb.svn.sourceforge.net/ledger-smb/?rev=3896&view=rev
Author:   einhverfr
Date:     2011-10-20 08:23:37 +0000 (Thu, 20 Oct 2011)
Log Message:
-----------
Wiring all number formatting through new amount class PGNumeric

Modified Paths:
--------------
    trunk/LedgerSMB/Form.pm
    trunk/LedgerSMB/PGNumber.pm
    trunk/LedgerSMB.pm

Modified: trunk/LedgerSMB/Form.pm
===================================================================
--- trunk/LedgerSMB/Form.pm	2011-10-20 07:56:45 UTC (rev 3895)
+++ trunk/LedgerSMB/Form.pm	2011-10-20 08:23:37 UTC (rev 3896)
@@ -789,99 +789,11 @@
        $places= $self->{money_precision};
     }
     $amount = $self->parse_amount( $myconfig, $amount );
-    $negative = ( $amount < 0 );
-    $amount =~ s/-//;
-
-    if ( $places =~ /\d+/ ) {
-
-        #$places = 4 if $places == 2;
-        $amount = $self->round_amount( $amount, $places );
-    }
-
-    # is the amount negative
-
-    # Parse $myconfig->{numberformat}
-
-    my ( $ts, $ds ) = ( $1, $2 );
-
-    if ($amount) {
-
-        if ( $myconfig->{numberformat} ) {
-
-            my ( $whole, $dec ) = split /\./, "$amount";
-
-            $dec = "" unless defined $dec;
-
-            $amount = join '', reverse split //, $whole;
-
-            if ($places) {
-                $dec .= "0" x $places;
-                $dec = substr( $dec, 0, $places );
-            }
-
-            if ( $myconfig->{numberformat} eq '1,000.00' ) {
-                $amount =~ s/\d{3,}?/$&,/g;
-                $amount =~ s/,$//;
-                $amount = join '', reverse split //, $amount;
-                $amount .= "\.$dec" if ( $dec ne "" );
-            } 
-	    elsif ( $myconfig->{numberformat} eq '1 000.00' ) {
-                $amount =~ s/\d{3,}?/$& /g;
-                $amount =~ s/\s$//;
-                $amount = join '', reverse split //, $amount;
-                $amount .= "\.$dec" if ( $dec ne "" );
-            } 
-	    elsif ( $myconfig->{numberformat} eq "1'000.00" ) {
-                $amount =~ s/\d{3,}?/$&'/g;
-                $amount =~ s/'$//;
-                $amount = join '', reverse split //, $amount;
-                $amount .= "\.$dec" if ( $dec ne "" );
-            } 
-	    elsif ( $myconfig->{numberformat} eq '1.000,00' ) {
-                $amount =~ s/\d{3,}?/$&./g;
-                $amount =~ s/\.$//;
-                $amount = join '', reverse split //, $amount;
-                $amount .= ",$dec" if ( $dec ne "" );
-            } 
-	    elsif ( $myconfig->{numberformat} eq '1000,00' ) {
-                $amount = "$whole";
-                $amount .= ",$dec" if ( $dec ne "" );
-            } 
-	    elsif ( $myconfig->{numberformat} eq '1000.00' ) {
-                $amount = "$whole";
-                $amount .= ".$dec" if ( $dec ne "" );
-            }
-
-            if ( $dash =~ /-/ ) {
-                $amount = ($negative) ? "($amount)" : "$amount";
-            }
-            elsif ( $dash =~ /DRCR/ ) {
-                $amount = ($negative) ? "$amount DR" : "$amount CR";
-            }
-            else {
-                $amount = ($negative) ? "-$amount" : "$amount";
-            }
-        }
-
-    }
-    else {
-
-        if ( $dash eq "0" && $places ) {
-
-            if ( $myconfig->{numberformat} =~ /0,00$/ ) {
-                $amount = "0" . "," . "0" x $places;
-            }
-            else {
-                $amount = "0" . "." . "0" x $places;
-            }
-
-        }
-        else {
-            $amount = ( $dash ne "" ) ? "$dash" : "";
-        }
-    }
-
-    $amount;
+    return $amount->to_output({
+               places => $places,
+                money => $self->{money_precision},
+           neg_format => $dash,
+    });
 }
 
 =item $form->parse_amount($myconfig, $amount);
@@ -901,49 +813,14 @@
 
     #if ( ( $amount eq '' ) or ( ! defined $amount ) ) {
     if ( ( ! defined $amount ) or ( $amount eq '' ) ) {
-        $amount = Math::BigFloat->bzero();
+        $amount = '0';
     }
 
-    if ( UNIVERSAL::isa( $amount, 'Math::BigFloat' ) )
+    if ( UNIVERSAL::isa( $amount, 'LedgerSMB::PGNumeric' ) )
     {    # Amount may not be an object
         return $amount;
     }
-    my $numberformat = $myconfig->{numberformat};
-    $numberformat = "" unless defined $numberformat;
-
-    if (   ( $numberformat eq '1.000,00' )
-        || ( $numberformat eq '1000,00' ) )
-    {
-
-        $amount =~ s/\.//g;
-        $amount =~ s/,/./;
-    }
-    elsif ( $numberformat eq '1 000.00' ) {
-        $amount =~ s/\s//g;
-    }
-    elsif ( $numberformat eq "1'000.00" ) {
-        $amount =~ s/'//g;
-    }
-
-    $amount =~ s/,//g;
-    if ( $amount =~ s/\((\d*\.?\d*)\)/$1/ ) {
-        $amount = $1 * -1;
-    }
-    elsif ( $amount =~ s/(\d*\.?\d*)\s?DR/$1/ ) {
-        $amount = $1 * -1;
-    }
-    $amount =~ s/\s?CR//;
-
-    $amount =~ /(\d*)\.(\d*)/;
-
-    #my $decimalplaces = length $1 + length $2;
-
-    $amount = new Math::BigFloat($amount);
-    if ($amount->is_nan){
-        $self->error("Invalid number detected during parsing");
-    }
-
-    return ( $amount * 1 );
+    return LedgerSMB::PGNumeric->from_input($amount);
 }
 
 =item $form->round_amount($amount, $places);

Modified: trunk/LedgerSMB/PGNumber.pm
===================================================================
--- trunk/LedgerSMB/PGNumber.pm	2011-10-20 07:56:45 UTC (rev 3895)
+++ trunk/LedgerSMB/PGNumber.pm	2011-10-20 08:23:37 UTC (rev 3896)
@@ -128,11 +128,6 @@
     $format = ($args{format}) ? $args{format}
                               : $LedgerSMB::App_State::User->{numberformat};
 
-    my $places = ($args{places}) ? $args{places} : undef;
-    $places = $LedgerSMB::Sysconfig::decimal_places if $args{money};
-
-    $DECIMAL_FILL    = 0;
-    $DECIMAL_DIGITS  = $places if defined $places;
     $THOUSANDS_SEP   = $lsmb_formats->{format}->{thousands_sep};
     $DECIMAL_POINT   = $lsmb_formats->{format}->{decimal_sep};
     my $pgnum = $self->new(unformat_number($string));
@@ -172,12 +167,17 @@
     my ($self) = shift;
     my %args  = (ref($_[0]) eq 'HASH')? %{$_[0]}: @_;  
     my $is_neg = $self->is_neg;
-    $self->bmul(-1) if $is_neg;
+    $self->babs;
 
     my $str = $self->bstr;
     $format = ($args{format}) ? $args{format}
                               : $LedgerSMB::App_State::User->{numberformat};
 
+    my $places = $LedgerSMB::Sysconfig::decimal_places if $args{money};
+    $places = ($args{places}) ? $args{places} : $places;
+
+    $DECIMAL_FILL    = 0;
+    $DECIMAL_DIGITS  = $places if defined $places;
     $THOUSANDS_SEP   = $lsmb_formats->{format}->{thousands_sep};
     $DECIMAL_POINT   = $lsmb_formats->{format}->{decimal_sep};
     $str = format_number($str);

Modified: trunk/LedgerSMB.pm
===================================================================
--- trunk/LedgerSMB.pm	2011-10-20 07:56:45 UTC (rev 3895)
+++ trunk/LedgerSMB.pm	2011-10-20 08:23:37 UTC (rev 3896)
@@ -203,7 +203,7 @@
 
 use CGI::Simple;
 $CGI::Simple::DISABLE_UPLOADS = 0;
-use Math::BigFloat;
+use LedgerSMB::PGNumeric;
 use LedgerSMB::Sysconfig;
 use Data::Dumper;
 use Error;
@@ -531,7 +531,10 @@
     my $dash     = $args{neg_format};
     my $format   = $args{format};
 
-    $dash = "" unless defined $dash;
+    if (defined $amount and ! UNIVERSAL::isa($amount, 'LedgerSMB::PGNumeric' )) {
+        $amount = $self->parse_amount('user' => $myconfig, 'amount' => $amount);
+    }
+    $dash = undef unless defined $dash;
 
     if (!defined $format){
        $format = $myconfig->{numberformat}
@@ -543,149 +546,22 @@
        $places = $LedgerSMB::Sysconfig::decimal_places;
     }
 
-    my $negative;
-    if (defined $amount and ! UNIVERSAL::isa($amount, 'Math::BigFloat' )) {
-        $amount = $self->parse_amount( 'user' => $myconfig, 'amount' => $amount );
-    }
-    $negative = ( $amount < 0 );
-    $amount->babs();
-
-    $places = "" unless defined $places;
-    if ( $places =~ /\d+/ ) {
-
-        #$places = 4 if $places == 2;
-        $amount = $self->round_amount( $amount, $places );
-    }
-
-    # is the amount negative
-
-    # Parse $myconfig->{numberformat}
-
-    my ( $ts, $ds ) = ( $1, $2 );
-
-    if (defined $amount) {
-
-        if ( $format ) {
-
-            my ( $whole, $dec ) = split /\./, "$amount";
-            $dec = "" unless defined $dec;
-            $amount = join '', reverse split //, $whole;
-
-            if ($places) {
-                $dec .= "0" x $places;
-                $dec = substr( $dec, 0, $places );
-            }
-
-            if ( $format eq '1,000.00' ) {
-                $amount =~ s/\d{3,}?/$&,/g;
-                $amount =~ s/,$//;
-                $amount = join '', reverse split //, $amount;
-                $amount .= "\.$dec" if ( $dec ne "" );
-            } 
-	    elsif ( $format eq '1 000.00' ) {
-                $amount =~ s/\d{3,}?/$& /g;
-                $amount =~ s/\s$//;
-                $amount = join '', reverse split //, $amount;
-                $amount .= "\.$dec" if ( $dec ne "" );
-            } 
-	    elsif ( $format eq "1'000.00" ) {
-                $amount =~ s/\d{3,}?/$&'/g;
-                $amount =~ s/'$//;
-                $amount = join '', reverse split //, $amount;
-                $amount .= "\.$dec" if ( $dec ne "" );
-            } 
-	    elsif ( $format eq '1.000,00' ) {
-                $amount =~ s/\d{3,}?/$&./g;
-                $amount =~ s/\.$//;
-                $amount = join '', reverse split //, $amount;
-                $amount .= ",$dec" if ( $dec ne "" );
-            } 
-	    elsif ( $format eq '1000,00' ) {
-                $amount = "$whole";
-                $amount .= ",$dec" if ( $dec ne "" );
-            } 
-	    elsif ( $format eq '1000.00' ) {
-                $amount = "$whole";
-                $amount .= ".$dec" if ( $dec ne "" );
-            }
-
-            if ( $dash =~ /-/ ) {
-                $amount = ($negative) ? "($amount)" : "$amount";
-            }
-            elsif ( $dash =~ /DRCR/ ) {
-                $amount = ($negative) ? "$amount DR" : "$amount CR";
-            }
-            else {
-                $amount = ($negative) ? "-$amount" : "$amount";
-            }
-        }
-
-    }
-    else {
-
-        if ( $dash eq "0" && $places ) {
-
-            if ( $format =~ /0,00$/ ) {
-                $amount = "0" . "," . "0" x $places;
-            }
-            else {
-                $amount = "0" . "." . "0" x $places;
-            }
-
-        }
-        else {
-            $amount = ( $dash ne "" ) ? "$dash" : "";
-        }
-    }
-
-    $amount;
+    return $amount->to_output({format => $format, 
+                           neg_format => $dash, 
+                               places => $precision,
+                                money => $args{money},
+           });
 }
 
-# This should probably go to the User object too.
+# For backwards compatibility only
 sub parse_amount {
     my $self     = shift @_;
     my %args     = @_;
-    my $myconfig = $args{user} || $self->{_user};
     my $amount   = $args{amount};
-
-    if ( ! defined $amount or $amount eq '' ) {
-        return Math::BigFloat->bzero();
-    }
-
-    if ( UNIVERSAL::isa( $amount, 'Math::BigFloat' ) )
-    {   #Avoiding double-parse issues 
+    if (UNIVERSAL::isa($amount, 'LedgerSMB::PGNumeric')){
         return $amount;
-    }
-    my $numberformat = $myconfig->{numberformat};
-    $numberformat = "" unless defined $numberformat;
-
-    if (   ( $numberformat eq '1.000,00' )
-        || ( $numberformat eq '1000,00' ) )
-    {
-
-        $amount =~ s/\.//g;
-        $amount =~ s/,/./;
-    }
-    elsif ( $numberformat eq '1 000.00' ) {
-        $amount =~ s/\s//g;
-    }
-    elsif ( $numberformat eq "1'000.00" ) {
-        $amount =~ s/'//g;
-    }
-
-    $amount =~ s/,//g;
-    if ( $amount =~ s/\((\d*\.?\d*)\)/$1/ ) {
-        $amount = $1 * -1;
-    }
-    elsif ( $amount =~ s/(\d*\.?\d*)\s?DR/$1/ ) {
-        $amount = $1 * -1;
-    }
-    $amount =~ s/\s?CR//;
-    $amount = new Math::BigFloat($amount);
-    if ($amount->is_nan){
-        $self->error("Invalid number detected during parsing");
-    }
-    return ( $amount * 1 );
+    } 
+    return LedgerSMB::PGNumeric->from_inout($amount); 
 }
 
 sub round_amount {

This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.