blib/lib/Embperl/Form/Control/price.pm untested

filter: Statementstmt 0.0% 0 / 61 Branchbran 0.0% 0 / 30 Conditioncond 0.0% 0 / 8 MC/DCmcdc 0.0% 0 / 5 Subroutinesub 0.0% 0 / 8 Podpod 0.0% 0 / 4 totaltotal 0.0% 0 / 116 CC 16 SCAR 37.6
CC
16
Cov
0%
get_display_text
90.0
init
20.0
prepare_fdat
20.0
CRAP 43.0
1  
2 ###################################################################################
3 #
4 # Embperl - Copyright (c) 1997-2008 Gerald Richter / ecos gmbh www.ecos.de
5 # Embperl - Copyright (c) 2008-2015 Gerald Richter
6 # Embperl - Copyright (c) 2015-2023 actevy.io
7 #
8 # You may distribute under the terms of either the GNU General Public
9 # License or the Artistic License, as specified in the Perl README file.
10 #
11 # THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR
12 # IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
13 # WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
14 #
15 # $Id$
16 #
17 ###################################################################################
18  
19 package Embperl::Form::Control::price ;
20  
21 use strict ;
22 use base 'Embperl::Form::Control::number' ;
23  
24 use Embperl::Inline ;
25  
26 use vars qw{%fdat} ;
27  
28 # ---------------------------------------------------------------------------
29 #
30 # init - init the new control
31 #
32  
33 sub init
34  
35 {
36 my ($self) = @_ ;
37  
38 $self -> {use_comma} = 1 if (!defined $self -> {use_comma}) ;
39 $self -> {unit} = 'euro' if (!defined ($self->{unit} ));
40 $self -> {decimals} = 2 if (!defined ($self->{decimals} ));
41
42 return $self ;
43 }
44  
45 # ------------------------------------------------------------------------------------------
46 #
47 # get_display_text - returns the text that should be displayed
48 #
49  
50 sub get_display_text
51 {
52 my ($self, $req, $val, $compact) = @_ ;
53
54 $val = $self -> get_value ($req) if (!defined ($val)) ;
55
56 my $decimals = $self -> {decimals} ;
57 my $sep ;
58 my $dec ;
59 my $int ;
60 my @int ;
61 my $frac ;
62 my $minus = $val =~ s/^-// ;
63 ($int, $frac) = split (/\./, $val, 2) ;
64 if ($self -> {use_comma})
65 {
66 $sep = '.' ;
67 $dec = ',' ;
68 }
69 else
70 {
71 $sep = ',' ;
72 $dec = '.' ;
73 }
74
75 $int = '0' x ((3 - length($int)) % 3) . $int;
76  
77 while ($int =~ /(...)/g)
78 {
79 push @int, $1 ;
80 }
81
82 $int[0] =~ s/^0+// ;
83 $int[0] = '0' if (@int == 1 && !$int[0]) ;
84 $frac = substr ($frac . '00000', 0, $decimals) ;
85 my $result = ($minus?'-':'') . join ($sep, @int) . ( $decimals ? $dec . $frac : '') ;
86 return $result if ($compact || $val eq '') ;
87
88 my $unit = $self->{unit} ;
89 my $unittext = !$unit?'':$self -> form -> convert_text ($self, ($unit =~ /:/)?$unit:'unit:' . lc($unit), $unit, $req) ;
90 $unittext =~ s/^unit:// ;
91  
92 return $result . ' ' . $unittext ;
93 }
94
95
96  
97  
98 # ------------------------------------------------------------------------------------------
99 #
100 # prepare_fdat - daten zusammenfuehren
101 #
102  
103 sub prepare_fdat
104 {
105 my ($self, $req) = @_ ;
106  
107 my $fdat = $req -> {form} || \%fdat ;
108 my $name = $self->{name} ;
109 return if (!exists $fdat->{$name}) ;
110 my $val = $fdat->{$name} ;
111 return if ($val eq '') ;
112
113 $val =~ s/\s+//g ;
114 if ($self -> {use_comma})
115 {
116 $val =~ s/\.//g ;
117 $val =~ s/\,/./ ;
118 }
119
120 $fdat->{$name} = $val + 0 ;
121 }
122
123 # ---------------------------------------------------------------------------
124 #
125 # get_validate_auto_rules - get rules for validation, in case user did
126 # not specify any
127 #
128  
129 sub get_validate_auto_rules
130 {
131 my ($self, $req) = @_ ;
132
133 return [ $self -> {required}?(required => 1):(emptyok => 1), -type => 'Number' ] ;
134 }
135  
136 1 ;
137  
138 __EMBPERL__
139  
140  
141 __END__