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

SF.net SVN: ledger-smb:[6286] addons/1.3/wxPOS/WXPOS/Number.pm



Revision: 6286
          http://sourceforge.net/p/ledger-smb/code/6286
Author:   einhverfr
Date:     2013-11-14 09:48:53 +0000 (Thu, 14 Nov 2013)
Log Message:
-----------
Adding number formatting module. 
I can't fully test it yet because I want to wait for the new PGObject::Type::BigFloat to come through CPAN first.

Added Paths:
-----------
    addons/1.3/wxPOS/WXPOS/Number.pm

Added: addons/1.3/wxPOS/WXPOS/Number.pm
===================================================================
--- addons/1.3/wxPOS/WXPOS/Number.pm	                        (rev 0)
+++ addons/1.3/wxPOS/WXPOS/Number.pm	2013-11-14 09:48:53 UTC (rev 6286)
@@ -0,0 +1,141 @@
+=head1 NAME
+
+WXPOS::Number - Number handling and formatting for WXPOS
+
+=cut
+
+package WXPOS::Number;
+use base "PGObject::Type::BigFloat";
+use WXPOS::Sysconfig;
+
+=head1 SYNPOSIS
+
+  my $number = WXPOS::Number->from_input('1.000.000,01');
+  print $number->to_db; # prints 1000000.01, if 1.000,00 number format used
+
+  my $number = WXPOS::Number->new('1000000');
+  print $number->to_output; # prints in localized numer format, eg.
+                            # 1.000.000 or 1,000,000.  This is reversible
+                            # regarding from_input
+
+And if you want all numbers from the db to be of this object:
+
+  WXPOS::Number->register();
+
+=head1 SUPPORTED NUMBER FORMATS
+
+=over
+
+=item 1000.00 (default)
+
+=item 1000,00
+
+=item 1 000.00
+
+=item 1 000,00
+
+=item 1,000.00
+
+=item 1.000,00
+
+=item 1'000,00
+
+=item 1'000.00
+
+=back
+
+=cut
+
+my $formats = {
+      "1000.00" => { -decimal_sep => '.' },
+
+      "1000,00" => { -decimal_sep => ',' },
+     "1 000.00" => { -thousands_sep => ' ', -decimal_sep => '.' },
+     "1 000,00" => { -thousands_sep => ' ', -decimal_sep => ',' },
+     "1,000.00" => { -thousands_sep => ',', -decimal_sep => '.' },
+     "1.000,00" => { -thousands_sep => '.', -decimal_sep => ',' },
+     "1'000,00" => { -thousands_sep => "'", -decimal_sep => ',' },
+     "1'000.00" => { -thousands_sep => "'", -decimal_sep => '.' },
+
+};
+
+
+
+=head1 METHODS
+
+=head2 from_input
+
+Parses a number from input according to the locally set number format.
+
+=cut
+
+sub from_input {
+    my $self   = shift @_;
+    my $string = shift @_;
+
+    # DB NULL if undef
+    if(!defined $string || $string eq ''){
+       my $obj = __PACKAGE__->new('NaN');
+       $obj->is_null(1);
+       return $obj;
+    }
+
+    my %args   = @_;  
+    my $format = ($args{format}) ? $args{format}
+                              : $WXPOS::Sysconfig::number_format;
+    die 'WXPOS::PGNumber No Format Set' if !$format;
+    #return undef if !defined $string;
+    if ( UNIVERSAL::isa( $string, 'WXPOS::Number' ) )
+    {    
+        return $string;
+    }
+    if (UNIVERSAL::isa( $string, 'Math::BigFloat' ) ) {
+        die 'LedgerSMB::PGNumber Invalid Number' if $string->is_nan();
+        return __PACKAGE__->new($string); 
+    } else {
+        my $formatter = new Number::Format(%{$lsmb_formats->{$format}});
+        $newval = $formatter->unformat_number($string);
+        $self = __PACKAGE__->new($newval);
+        $self->round_mode('+inf');
+        die 'LedgerSMB::PGNumber Invalid Number' if $pgnum->is_nan();
+        return $self;
+    } 
+}
+
+=head2 to_output
+
+Formats the number and returns the string.
+
+=cut
+
+sub to_output {
+    my $self = shift @_;
+    my %args  = @_;
+    return undef if $self->is_null;
+    my $format = ($args{format}) ? $args{format}
+                              : $WXPOS::Sysconfig::number_format;
+
+    $places = $WXPOS::Sysconfig::number_format if $args{money};
+    $places = ($args{places}) ? $args{places} : $places;
+    my $str = $self->bstr;
+    my $dplaces = $places;
+    $places = 0 unless defined $places and ($places > 0);
+    my $zfill = ($places > 0) ? 1 : 0;
+    my $format_args  = $formats->{$format};
+    $format_args->{'-decimal_fill'} = $zfill;
+    $format_args->{'-neg_format'} = 'x';
+    my $formatter = new Number::Format(%$format_args);;
+    $str = $formatter->format_number($str, $places);
+    return $str;
+}
+
+
+=head1 COPYRIGHT
+
+Copyright(C) 2013 The LedgerSMB Core Team.  This file may be used subject to the
+GNU General Public License version 2 or at your option any later version. Please
+see the enclosed license.txt for more information.
+
+=cut
+
+1;

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


------------------------------------------------------------------------------
DreamFactory - Open Source REST & JSON Services for HTML5 & Native Apps
OAuth, Users, Roles, SQL, NoSQL, BLOB Storage and External API Access
Free app hosting. Or install the open source package on any LAMP server.
Sign up and see examples for AngularJS, jQuery, Sencha Touch and Native!
http://pubads.g.doubleclick.net/gampad/clk?id=63469471&iu=/4140/ostg.clktrk
_______________________________________________
Ledger-smb-commits mailing list
..hidden..
https://lists.sourceforge.net/lists/listinfo/ledger-smb-commits