Neue Version podchecker erkennt leere Setions und fehlende Leerzeilen
[kivitendo-erp.git] / SL / DB / Helper / Attr.pm
1 package SL::DB::Helper::Attr;
2
3 use strict;
4
5 sub auto_make {
6   my ($package, %params) = @_;
7
8   for my $col ($package->meta->columns) {
9     next if $col->primary_key_position; # don't make attr helper for primary keys
10     _make_by_type($package, $col->name, $col->type);
11   }
12
13   return $package;
14 }
15
16 sub make {
17   my ($package, %params) = @_;
18
19   for my $name (keys %params) {
20     my @types = ref $params{$name} eq 'ARRAY' ? @{ $params{$name} } : ($params{$name});
21     for my $type (@types) {
22       _make_by_type($package, $name, $type);
23     }
24   }
25   return $package;
26 }
27
28
29
30 sub _make_by_type {
31   my ($package, $name, $type) = @_;
32   _as_number ($package, $name, places => -2) if $type =~ /numeric | real | float/xi;
33   _as_percent($package, $name, places =>  2) if $type =~ /numeric | real | float/xi;
34   _as_number ($package, $name, places =>  0) if $type =~ /int/xi;
35   _as_date   ($package, $name)               if $type =~ /date | timestamp/xi;
36   _as_bool_yn($package, $name)               if $type =~ /bool/xi;
37 }
38
39 sub _as_number {
40   my $package     = shift;
41   my $attribute   = shift;
42   my %params      = @_;
43
44   $params{places} = 2 if !defined($params{places});
45
46   no strict 'refs';
47   *{ $package . '::' . $attribute . '_as_number' } = sub {
48     my ($self, $string) = @_;
49
50     $self->$attribute($::form->parse_amount(\%::myconfig, $string)) if @_ > 1;
51
52     return $::form->format_amount(\%::myconfig, $self->$attribute, $params{places});
53   };
54 }
55
56 sub _as_percent {
57   my $package     = shift;
58   my $attribute   = shift;
59   my %params      = @_;
60
61   $params{places} = 2 if !defined($params{places});
62
63   no strict 'refs';
64   *{ $package . '::' . $attribute . '_as_percent' } = sub {
65     my ($self, $string) = @_;
66
67     $self->$attribute($::form->parse_amount(\%::myconfig, $string) / 100) if @_ > 1;
68
69     return $::form->format_amount(\%::myconfig, 100 * $self->$attribute, $params{places});
70   };
71
72   return 1;
73 }
74
75 sub _as_date {
76   my $package     = shift;
77   my $attribute   = shift;
78   my %params      = @_;
79
80   no strict 'refs';
81   *{ $package . '::' . $attribute . '_as_date' } = sub {
82     my ($self, $string) = @_;
83
84     if (@_ > 1) {
85       if ($string) {
86         my ($yy, $mm, $dd) = $::locale->parse_date(\%::myconfig, $string);
87         $self->$attribute(DateTime->new(year => $yy, month => $mm, day => $dd));
88       } else {
89         $self->$attribute(undef);
90       }
91     }
92
93     return $self->$attribute
94       ? $::locale->reformat_date(
95           { dateformat => 'yy-mm-dd' },
96           ( $self->$attribute eq 'now'
97              ? DateTime->now
98              : $self->$attribute
99           )->ymd,
100           $::myconfig{dateformat}
101         )
102       : undef;
103   };
104
105   return 1;
106 }
107
108 sub _as_bool_yn {
109   my ($package, $attribute, %params) = @_;
110
111   no strict 'refs';
112   *{ $package . '::' . $attribute . '_as_bool_yn' } = sub {
113     my ($self) = @_;
114
115     if (@_ > 1) {
116       die 'not an accessor';
117     }
118
119     return !defined $self->$attribute ? ''
120          :          $self->$attribute ? $::locale->text('Yes')
121          :                              $::locale->text('No');
122   }
123 }
124
125 1;
126
127
128 1;
129
130 __END__
131
132 =encoding utf-8
133
134 =head1 NAME
135
136 SL::DB::Helper::Attr - attribute helpers
137
138 =head1 SYNOPSIS
139
140   use SL::DB::Helper::Attr;
141   SL::DB::Helper::Attr::make($class,
142     method_name => 'numeric(15,5)',
143     datemethod  => 'date'
144   );
145   SL::DB::Helper::Attr::auto_make($class);
146
147 =head1 DESCRIPTION
148
149 Makes attribute helpers.
150
151 =head1 FUNCTIONS
152
153 see for yourself.
154
155 =head1 BUGS
156
157 None yet.
158
159 =head1 AUTHOR
160
161 Sven Schöling <s.schoeling@linet-services.de>
162
163 =cut