RDBO Support.
[kivitendo-erp.git] / SL / DB / Helpers / AttrPercent.pm
1 package SL::DB::Helpers::AttrPercent;
2
3 use strict;
4
5 use Carp;
6 use English;
7
8 sub define {
9   my $package     = shift;
10   my $attribute   = shift;
11   my %params      = @_;
12
13   $params{places} = 2 if !defined($params{places});
14
15   my $code        = <<CODE;
16 package ${package};
17
18 sub ${attribute}_as_percent {
19   my \$self = shift;
20
21   if (scalar \@_) {
22     \$self->${attribute}(\$::form->parse_amount(\\\%::myconfig, \$_[0]) / 100);
23   }
24
25   return \$::form->format_amount(\\\%::myconfig, 100 * \$self->${attribute}, $params{places});
26 }
27
28 1;
29 CODE
30
31   eval $code;
32   croak "Defining '${attribute}_as_number' failed: $EVAL_ERROR" if $EVAL_ERROR;
33
34   return 1;
35 }
36
37 1;